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