xref: /titanic_44/usr/src/cmd/sgs/liblddbg/common/elf.c (revision ee519a1f9541a20bb76ef306dfc8e5616f8a5e26)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 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	"msg.h"
29 #include	"_debug.h"
30 #include	"libld.h"
31 
32 
33 static void
34 Elf_elf_data(const char *str1, Addr addr, Elf_Data *data, const char *file,
35     const char *str2)
36 {
37 	dbg_print(MSG_INTL(MSG_ELF_ENTRY), str1, EC_ADDR(addr),
38 	    conv_d_type_str(data->d_type), EC_XWORD(data->d_size),
39 	    EC_OFF(data->d_off), EC_XWORD(data->d_align), file, str2);
40 }
41 
42 void
43 Gelf_elf_data_title()
44 {
45 	dbg_print(MSG_INTL(MSG_ELF_TITLE));
46 }
47 
48 void
49 _Dbg_elf_data_in(Os_desc *osp, Is_desc *isp)
50 {
51 	Shdr		*shdr = osp->os_shdr;
52 	Elf_Data	*data = isp->is_indata;
53 	const char	*file, *str;
54 	Addr		addr;
55 
56 	if (isp->is_flags & FLG_IS_DISCARD) {
57 		str = MSG_INTL(MSG_ELF_IGNSCN);
58 		addr = 0;
59 	} else {
60 		str = MSG_ORIG(MSG_STR_EMPTY);
61 		addr = (Addr)(shdr->sh_addr + data->d_off);
62 	}
63 
64 	if (isp->is_file && isp->is_file->ifl_name)
65 		file = isp->is_file->ifl_name;
66 	else
67 		file = MSG_ORIG(MSG_STR_EMPTY);
68 
69 	Elf_elf_data(MSG_INTL(MSG_STR_IN), addr, data, file, str);
70 }
71 
72 void
73 _Dbg_elf_data_out(Os_desc *osp)
74 {
75 	Shdr		*shdr = osp->os_shdr;
76 	Elf_Data	*data = osp->os_outdata;
77 
78 	Elf_elf_data(MSG_INTL(MSG_STR_OUT), shdr->sh_addr,
79 	    data, MSG_ORIG(MSG_STR_EMPTY), MSG_ORIG(MSG_STR_EMPTY));
80 }
81 
82 void
83 Gelf_elf_header(GElf_Ehdr *ehdr, GElf_Shdr *shdr0)
84 {
85 	Byte		*byte =	&(ehdr->e_ident[0]);
86 	const char	*flgs;
87 
88 	dbg_print(MSG_ORIG(MSG_STR_EMPTY));
89 	dbg_print(MSG_INTL(MSG_ELF_HEADER));
90 
91 	dbg_print(MSG_ORIG(MSG_ELF_MAGIC), byte[EI_MAG0],
92 	    (byte[EI_MAG1] ? byte[EI_MAG1] : '0'),
93 	    (byte[EI_MAG2] ? byte[EI_MAG2] : '0'),
94 	    (byte[EI_MAG3] ? byte[EI_MAG3] : '0'));
95 	dbg_print(MSG_ORIG(MSG_ELF_CLASS),
96 	    conv_eclass_str(ehdr->e_ident[EI_CLASS]),
97 	    conv_edata_str(ehdr->e_ident[EI_DATA]));
98 	dbg_print(MSG_ORIG(MSG_ELF_MACHINE),
99 	    conv_emach_str(ehdr->e_machine), conv_ever_str(ehdr->e_version));
100 	dbg_print(MSG_ORIG(MSG_ELF_TYPE), conv_etype_str(ehdr->e_type));
101 
102 	/*
103 	 * Line up the flags differently depending on wether we
104 	 * received a numeric (e.g. "0x200") or text represent-
105 	 * ation (e.g. "[ EF_SPARC_SUN_US1 ]").
106 	 */
107 	flgs = conv_eflags_str(ehdr->e_machine, ehdr->e_flags);
108 	if (flgs[0] == '[')
109 		dbg_print(MSG_ORIG(MSG_ELF_FLAGS_FMT), flgs);
110 	else
111 		dbg_print(MSG_ORIG(MSG_ELF_FLAGS), flgs);
112 
113 	if ((ehdr->e_shnum == 0) && (ehdr->e_shstrndx == SHN_XINDEX))
114 		dbg_print(MSG_ORIG(MSG_ELFX_ESIZE), EC_ADDR(ehdr->e_entry),
115 		    ehdr->e_ehsize);
116 	else
117 		dbg_print(MSG_ORIG(MSG_ELF_ESIZE), EC_ADDR(ehdr->e_entry),
118 		    ehdr->e_ehsize, ehdr->e_shstrndx);
119 
120 	if ((ehdr->e_shnum == 0) && shdr0 && (shdr0->sh_size != 0))
121 		dbg_print(MSG_ORIG(MSG_ELFX_SHOFF), EC_OFF(ehdr->e_shoff),
122 		    ehdr->e_shentsize);
123 	else
124 		dbg_print(MSG_ORIG(MSG_ELF_SHOFF), EC_OFF(ehdr->e_shoff),
125 		    ehdr->e_shentsize, ehdr->e_shnum);
126 
127 	if (ehdr->e_phnum == PN_XNUM)
128 		dbg_print(MSG_ORIG(MSG_ELFX_PHOFF), EC_OFF(ehdr->e_phoff),
129 		    ehdr->e_phentsize);
130 	else
131 		dbg_print(MSG_ORIG(MSG_ELF_PHOFF), EC_OFF(ehdr->e_phoff),
132 		    ehdr->e_phentsize, ehdr->e_phnum);
133 
134 	if (shdr0 == NULL ||
135 	    (ehdr->e_phnum != PN_XNUM &&
136 	    (ehdr->e_shnum != 0 || shdr0->sh_size)))
137 		return;
138 
139 	/*
140 	 * If we have Extended ELF headers - print shdr0
141 	 */
142 	dbg_print(MSG_ORIG(MSG_SHD0_TITLE));
143 	dbg_print(MSG_ORIG(MSG_SHD_ADDR), EC_ADDR(shdr0->sh_addr),
144 	    /* LINTED */
145 	    conv_secflg_str(ehdr->e_machine, shdr0->sh_flags));
146 	if (ehdr->e_shnum == 0)
147 		dbg_print(MSG_ORIG(MSG_SHD0_SIZE), EC_XWORD(shdr0->sh_size),
148 		    conv_sectyp_str(ehdr->e_machine, shdr0->sh_type));
149 	else
150 		dbg_print(MSG_ORIG(MSG_SHD_SIZE), EC_XWORD(shdr0->sh_size),
151 		    conv_sectyp_str(ehdr->e_machine, shdr0->sh_type));
152 	dbg_print(MSG_ORIG(MSG_SHD_OFFSET), EC_OFF(shdr0->sh_offset),
153 	    EC_XWORD(shdr0->sh_entsize));
154 
155 	if (ehdr->e_shstrndx == SHN_XINDEX && ehdr->e_phnum == PN_XNUM)
156 		dbg_print(MSG_ORIG(MSG_SHD0_LINK1), EC_WORD(shdr0->sh_link),
157 		    EC_WORD(shdr0->sh_info));
158 	else if (ehdr->e_shstrndx == SHN_XINDEX)
159 		dbg_print(MSG_ORIG(MSG_SHD0_LINK2), EC_WORD(shdr0->sh_link),
160 		    EC_WORD(shdr0->sh_info));
161 	else if (ehdr->e_phnum == PN_XNUM)
162 		dbg_print(MSG_ORIG(MSG_SHD0_LINK3), EC_WORD(shdr0->sh_link),
163 		    EC_WORD(shdr0->sh_info));
164 	else
165 		dbg_print(MSG_ORIG(MSG_SHD0_LINK4), EC_WORD(shdr0->sh_link),
166 		    EC_WORD(shdr0->sh_info));
167 
168 	dbg_print(MSG_ORIG(MSG_SHD_ALIGN), EC_XWORD(shdr0->sh_addralign));
169 }
170