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