elf.c revision 1618:8c9a4f31d225
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
33void
34Elf_ehdr(Lm_list *lml, Ehdr *ehdr, Shdr *shdr0)
35{
36	Byte		*byte =	&(ehdr->e_ident[0]);
37	const char	*flgs;
38
39	dbg_print(lml, MSG_ORIG(MSG_STR_EMPTY));
40	dbg_print(lml, MSG_INTL(MSG_ELF_HEADER));
41
42	dbg_print(lml, MSG_ORIG(MSG_ELF_MAGIC), byte[EI_MAG0],
43	    (byte[EI_MAG1] ? byte[EI_MAG1] : '0'),
44	    (byte[EI_MAG2] ? byte[EI_MAG2] : '0'),
45	    (byte[EI_MAG3] ? byte[EI_MAG3] : '0'));
46	dbg_print(lml, MSG_ORIG(MSG_ELF_CLASS),
47	    conv_ehdr_class(ehdr->e_ident[EI_CLASS]),
48	    conv_ehdr_data(ehdr->e_ident[EI_DATA]));
49	dbg_print(lml, MSG_ORIG(MSG_ELF_MACHINE),
50	    conv_ehdr_mach(ehdr->e_machine), conv_ehdr_vers(ehdr->e_version));
51	dbg_print(lml, MSG_ORIG(MSG_ELF_TYPE), conv_ehdr_type(ehdr->e_type));
52
53	/*
54	 * Line up the flags differently depending on wether we received a
55	 * numeric (e.g. "0x200") or text representation (e.g.
56	 * "[ EF_SPARC_SUN_US1 ]").
57	 */
58	flgs = conv_ehdr_flags(ehdr->e_machine, ehdr->e_flags);
59	if (flgs[0] == '[')
60		dbg_print(lml, MSG_ORIG(MSG_ELF_FLAGS_FMT), flgs);
61	else
62		dbg_print(lml, MSG_ORIG(MSG_ELF_FLAGS), flgs);
63
64	if ((ehdr->e_shnum == 0) && (ehdr->e_shstrndx == SHN_XINDEX))
65		dbg_print(lml, MSG_ORIG(MSG_ELFX_ESIZE),
66		    EC_ADDR(ehdr->e_entry), ehdr->e_ehsize);
67	else
68		dbg_print(lml, MSG_ORIG(MSG_ELF_ESIZE), EC_ADDR(ehdr->e_entry),
69		    ehdr->e_ehsize, ehdr->e_shstrndx);
70
71	if ((ehdr->e_shnum == 0) && shdr0 && (shdr0->sh_size != 0))
72		dbg_print(lml, MSG_ORIG(MSG_ELFX_SHOFF), EC_OFF(ehdr->e_shoff),
73		    ehdr->e_shentsize);
74	else
75		dbg_print(lml, MSG_ORIG(MSG_ELF_SHOFF), EC_OFF(ehdr->e_shoff),
76		    ehdr->e_shentsize, ehdr->e_shnum);
77
78	dbg_print(lml, MSG_ORIG(MSG_ELF_PHOFF), EC_OFF(ehdr->e_phoff),
79	    ehdr->e_phentsize, ehdr->e_phnum);
80
81	if ((ehdr->e_shnum != 0) || (shdr0 == NULL) ||
82	    (shdr0->sh_size == 0))
83		return;
84
85	/*
86	 * If we have Extended ELF headers - print shdr0
87	 */
88	dbg_print(lml, MSG_ORIG(MSG_SHD0_TITLE));
89	dbg_print(lml, MSG_ORIG(MSG_SHD_ADDR), EC_ADDR(shdr0->sh_addr),
90	    conv_sec_flags(shdr0->sh_flags));
91	dbg_print(lml, MSG_ORIG(MSG_SHD0_SIZE), EC_XWORD(shdr0->sh_size),
92	    conv_sec_type(ehdr->e_machine, shdr0->sh_type));
93	dbg_print(lml, MSG_ORIG(MSG_SHD_OFFSET), EC_OFF(shdr0->sh_offset),
94	    EC_XWORD(shdr0->sh_entsize));
95
96	if (ehdr->e_shstrndx == SHN_XINDEX)
97		dbg_print(lml, MSG_ORIG(MSG_SHD0_LINK),
98		    EC_WORD(shdr0->sh_link), conv_sec_info(shdr0->sh_info,
99		    shdr0->sh_flags));
100	else
101		dbg_print(lml, MSG_ORIG(MSG_SHD_LINK), EC_WORD(shdr0->sh_link),
102		    conv_sec_info(shdr0->sh_info, shdr0->sh_flags));
103
104	dbg_print(lml, MSG_ORIG(MSG_SHD_ALIGN), EC_XWORD(shdr0->sh_addralign));
105}
106