xref: /titanic_41/usr/src/cmd/sgs/liblddbg/common/elf.c (revision 5dfd244acc8f144280c5bc8f69ed941185fc3ccc)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include	<sgs.h>
28 #include	<_debug.h>
29 #include	<conv.h>
30 #include	<msg.h>
31 
32 void
33 Elf_ehdr(Lm_list *lml, Ehdr *ehdr, Shdr *shdr0)
34 {
35 	Conv_inv_buf_t		inv_buf1, inv_buf2;
36 	Conv_ehdr_flags_buf_t	flags_buf;
37 	Conv_sec_flags_buf_t	sec_flags_buf;
38 	Byte			*byte =	&(ehdr->e_ident[0]);
39 	const char		*flgs;
40 	int			xshdr = 0;
41 	uchar_t			osabi = ehdr->e_ident[EI_OSABI];
42 	Half			mach = ehdr->e_machine;
43 
44 	dbg_print(lml, MSG_ORIG(MSG_STR_EMPTY));
45 	dbg_print(lml, MSG_INTL(MSG_ELF_HEADER));
46 
47 	dbg_print(lml, MSG_ORIG(MSG_ELF_MAGIC), byte[EI_MAG0],
48 	    (byte[EI_MAG1] ? byte[EI_MAG1] : '0'),
49 	    (byte[EI_MAG2] ? byte[EI_MAG2] : '0'),
50 	    (byte[EI_MAG3] ? byte[EI_MAG3] : '0'));
51 	dbg_print(lml, MSG_ORIG(MSG_ELF_CLASS),
52 	    conv_ehdr_class(ehdr->e_ident[EI_CLASS], 0, &inv_buf1),
53 	    conv_ehdr_data(ehdr->e_ident[EI_DATA], 0, &inv_buf2));
54 	dbg_print(lml, MSG_ORIG(MSG_ELF_OSABI),
55 	    conv_ehdr_osabi(ehdr->e_ident[EI_OSABI], 0, &inv_buf1),
56 	    conv_ehdr_abivers(ehdr->e_ident[EI_OSABI],
57 	    ehdr->e_ident[EI_ABIVERSION], CONV_FMT_DECIMAL, &inv_buf2));
58 	dbg_print(lml, MSG_ORIG(MSG_ELF_MACHINE),
59 	    conv_ehdr_mach(mach, 0, &inv_buf1),
60 	    conv_ehdr_vers(ehdr->e_version, 0, &inv_buf2));
61 	dbg_print(lml, MSG_ORIG(MSG_ELF_TYPE),
62 	    conv_ehdr_type(osabi, ehdr->e_type, 0, &inv_buf1));
63 
64 	/*
65 	 * Line up the flags differently depending on whether we received a
66 	 * numeric (e.g. "0x200") or text representation (e.g.
67 	 * "[ EF_SPARC_SUN_US1 ]").
68 	 */
69 	flgs = conv_ehdr_flags(mach, ehdr->e_flags, 0, &flags_buf);
70 	if (flgs[0] == '[')
71 		dbg_print(lml, MSG_ORIG(MSG_ELF_FLAGS_FMT), flgs);
72 	else
73 		dbg_print(lml, MSG_ORIG(MSG_ELF_FLAGS), flgs);
74 
75 	/*
76 	 * The e_shnum, e_shstrndx and e_phnum entries may have a different
77 	 * meaning if extended sections exist.
78 	 */
79 	if (ehdr->e_shstrndx == SHN_XINDEX) {
80 		dbg_print(lml, MSG_ORIG(MSG_ELFX_ESIZE),
81 		    EC_ADDR(ehdr->e_entry), ehdr->e_ehsize);
82 		xshdr++;
83 	} else
84 		dbg_print(lml, MSG_ORIG(MSG_ELF_ESIZE), EC_ADDR(ehdr->e_entry),
85 		    ehdr->e_ehsize, ehdr->e_shstrndx);
86 
87 	if (ehdr->e_shnum == 0) {
88 		dbg_print(lml, MSG_ORIG(MSG_ELFX_SHOFF), EC_OFF(ehdr->e_shoff),
89 		    ehdr->e_shentsize);
90 		xshdr++;
91 	} else
92 		dbg_print(lml, MSG_ORIG(MSG_ELF_SHOFF), EC_OFF(ehdr->e_shoff),
93 		    ehdr->e_shentsize, ehdr->e_shnum);
94 
95 	if (ehdr->e_phnum == PN_XNUM) {
96 		dbg_print(lml, MSG_ORIG(MSG_ELFX_PHOFF), EC_OFF(ehdr->e_phoff),
97 		    ehdr->e_phentsize);
98 		xshdr++;
99 	} else
100 		dbg_print(lml, MSG_ORIG(MSG_ELF_PHOFF), EC_OFF(ehdr->e_phoff),
101 		    ehdr->e_phentsize, ehdr->e_phnum);
102 
103 	if ((xshdr == 0) || (shdr0 == NULL))
104 		return;
105 
106 	/*
107 	 * If we have Extended ELF headers - print shdr[0].
108 	 */
109 	dbg_print(lml, MSG_ORIG(MSG_STR_EMPTY));
110 	dbg_print(lml, MSG_ORIG(MSG_SHD0_TITLE));
111 	dbg_print(lml, MSG_ORIG(MSG_SHD0_ADDR), EC_ADDR(shdr0->sh_addr),
112 	    conv_sec_flags(osabi, mach, shdr0->sh_flags, 0, &sec_flags_buf));
113 	dbg_print(lml, MSG_ORIG(MSG_SHD0_SIZE), EC_XWORD(shdr0->sh_size),
114 	    conv_sec_type(osabi, mach, shdr0->sh_type, 0, &inv_buf1));
115 	dbg_print(lml, MSG_ORIG(MSG_SHD0_OFFSET), EC_OFF(shdr0->sh_offset),
116 	    EC_XWORD(shdr0->sh_entsize));
117 	dbg_print(lml, MSG_ORIG(MSG_SHD0_LINK), EC_WORD(shdr0->sh_link),
118 	    EC_WORD(shdr0->sh_info));
119 	dbg_print(lml, MSG_ORIG(MSG_SHD0_ALIGN), EC_XWORD(shdr0->sh_addralign));
120 }
121