xref: /titanic_41/usr/src/cmd/sgs/liblddbg/common/elf.c (revision 10a4fa49f51ed9ae1c857a626de6ce9ebf41661a)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include	<sgs.h>
29 #include	<_debug.h>
30 #include	<conv.h>
31 #include	<msg.h>
32 
33 void
34 Elf_ehdr(Lm_list *lml, Ehdr *ehdr, Shdr *shdr0)
35 {
36 	Byte		*byte =	&(ehdr->e_ident[0]);
37 	const char	*flgs;
38 	int		xshdr = 0;
39 
40 	dbg_print(lml, MSG_ORIG(MSG_STR_EMPTY));
41 	dbg_print(lml, MSG_INTL(MSG_ELF_HEADER));
42 
43 	dbg_print(lml, MSG_ORIG(MSG_ELF_MAGIC), byte[EI_MAG0],
44 	    (byte[EI_MAG1] ? byte[EI_MAG1] : '0'),
45 	    (byte[EI_MAG2] ? byte[EI_MAG2] : '0'),
46 	    (byte[EI_MAG3] ? byte[EI_MAG3] : '0'));
47 	dbg_print(lml, MSG_ORIG(MSG_ELF_CLASS),
48 	    conv_ehdr_class(ehdr->e_ident[EI_CLASS]),
49 	    conv_ehdr_data(ehdr->e_ident[EI_DATA]));
50 	dbg_print(lml, MSG_ORIG(MSG_ELF_MACHINE),
51 	    conv_ehdr_mach(ehdr->e_machine), conv_ehdr_vers(ehdr->e_version));
52 	dbg_print(lml, MSG_ORIG(MSG_ELF_TYPE), conv_ehdr_type(ehdr->e_type));
53 
54 	/*
55 	 * Line up the flags differently depending on whether we received a
56 	 * numeric (e.g. "0x200") or text representation (e.g.
57 	 * "[ EF_SPARC_SUN_US1 ]").
58 	 */
59 	flgs = conv_ehdr_flags(ehdr->e_machine, ehdr->e_flags);
60 	if (flgs[0] == '[')
61 		dbg_print(lml, MSG_ORIG(MSG_ELF_FLAGS_FMT), flgs);
62 	else
63 		dbg_print(lml, MSG_ORIG(MSG_ELF_FLAGS), flgs);
64 
65 	/*
66 	 * The e_shnum, e_shstrndx and e_phnum entries may have a different
67 	 * meaning if extended sections exist.
68 	 */
69 	if ((ehdr->e_shnum == 0) && (ehdr->e_shstrndx == SHN_XINDEX)) {
70 		dbg_print(lml, MSG_ORIG(MSG_ELFX_ESIZE),
71 		    EC_ADDR(ehdr->e_entry), ehdr->e_ehsize);
72 		xshdr++;
73 	} else
74 		dbg_print(lml, MSG_ORIG(MSG_ELF_ESIZE), EC_ADDR(ehdr->e_entry),
75 		    ehdr->e_ehsize, ehdr->e_shstrndx);
76 
77 	if ((ehdr->e_shnum == 0) && (shdr0 != NULL) && (shdr0->sh_size != 0)) {
78 		dbg_print(lml, MSG_ORIG(MSG_ELFX_SHOFF), EC_OFF(ehdr->e_shoff),
79 		    ehdr->e_shentsize);
80 		xshdr++;
81 	} else
82 		dbg_print(lml, MSG_ORIG(MSG_ELF_SHOFF), EC_OFF(ehdr->e_shoff),
83 		    ehdr->e_shentsize, ehdr->e_shnum);
84 
85 	if (ehdr->e_phoff == PN_XNUM) {
86 		dbg_print(lml, MSG_ORIG(MSG_ELFX_PHOFF), EC_OFF(ehdr->e_phoff),
87 		    ehdr->e_phentsize);
88 		xshdr++;
89 	} else
90 		dbg_print(lml, MSG_ORIG(MSG_ELF_PHOFF), EC_OFF(ehdr->e_phoff),
91 		    ehdr->e_phentsize, ehdr->e_phnum);
92 
93 	if ((xshdr == 0) || (shdr0 == NULL))
94 		return;
95 
96 	/*
97 	 * If we have Extended ELF headers - print shdr[0].
98 	 */
99 	dbg_print(lml, MSG_ORIG(MSG_STR_EMPTY));
100 	dbg_print(lml, MSG_ORIG(MSG_SHD0_TITLE));
101 	dbg_print(lml, MSG_ORIG(MSG_SHD0_ADDR), EC_ADDR(shdr0->sh_addr),
102 	    conv_sec_flags(shdr0->sh_flags));
103 	dbg_print(lml, MSG_ORIG(MSG_SHD0_SIZE), EC_XWORD(shdr0->sh_size),
104 	    conv_sec_type(ehdr->e_machine, shdr0->sh_type));
105 	dbg_print(lml, MSG_ORIG(MSG_SHD0_OFFSET), EC_OFF(shdr0->sh_offset),
106 	    EC_XWORD(shdr0->sh_entsize));
107 	dbg_print(lml, MSG_ORIG(MSG_SHD0_LINK), EC_WORD(shdr0->sh_link),
108 	    EC_WORD(shdr0->sh_info));
109 	dbg_print(lml, MSG_ORIG(MSG_SHD0_ALIGN), EC_XWORD(shdr0->sh_addralign));
110 }
111