xref: /illumos-gate/usr/src/cmd/sgs/liblddbg/common/elf.c (revision 4f680cc6)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
215aefb655Srie 
227c478bd9Sstevel@tonic-gate /*
23*4f680cc6SAli Bahrami  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
2430da1432Sahl  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
275aefb655Srie #include	<sgs.h>
285aefb655Srie #include	<_debug.h>
295aefb655Srie #include	<conv.h>
305aefb655Srie #include	<msg.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate void
Elf_ehdr(Lm_list * lml,Ehdr * ehdr,Shdr * shdr0)335aefb655Srie Elf_ehdr(Lm_list *lml, Ehdr *ehdr, Shdr *shdr0)
347c478bd9Sstevel@tonic-gate {
35de777a60Sab 	Conv_inv_buf_t		inv_buf1, inv_buf2;
36de777a60Sab 	Conv_ehdr_flags_buf_t	flags_buf;
37de777a60Sab 	Conv_sec_flags_buf_t	sec_flags_buf;
38de777a60Sab 	Byte			*byte =	&(ehdr->e_ident[0]);
39de777a60Sab 	const char		*flgs;
40de777a60Sab 	int			xshdr = 0;
41*4f680cc6SAli Bahrami 	uchar_t			osabi = ehdr->e_ident[EI_OSABI];
42*4f680cc6SAli Bahrami 	Half			mach = ehdr->e_machine;
437c478bd9Sstevel@tonic-gate 
445aefb655Srie 	dbg_print(lml, MSG_ORIG(MSG_STR_EMPTY));
455aefb655Srie 	dbg_print(lml, MSG_INTL(MSG_ELF_HEADER));
467c478bd9Sstevel@tonic-gate 
475aefb655Srie 	dbg_print(lml, MSG_ORIG(MSG_ELF_MAGIC), byte[EI_MAG0],
487c478bd9Sstevel@tonic-gate 	    (byte[EI_MAG1] ? byte[EI_MAG1] : '0'),
497c478bd9Sstevel@tonic-gate 	    (byte[EI_MAG2] ? byte[EI_MAG2] : '0'),
507c478bd9Sstevel@tonic-gate 	    (byte[EI_MAG3] ? byte[EI_MAG3] : '0'));
515aefb655Srie 	dbg_print(lml, MSG_ORIG(MSG_ELF_CLASS),
52de777a60Sab 	    conv_ehdr_class(ehdr->e_ident[EI_CLASS], 0, &inv_buf1),
53de777a60Sab 	    conv_ehdr_data(ehdr->e_ident[EI_DATA], 0, &inv_buf2));
54*4f680cc6SAli Bahrami 	dbg_print(lml, MSG_ORIG(MSG_ELF_OSABI),
55*4f680cc6SAli Bahrami 	    conv_ehdr_osabi(ehdr->e_ident[EI_OSABI], 0, &inv_buf1),
56*4f680cc6SAli Bahrami 	    conv_ehdr_abivers(ehdr->e_ident[EI_OSABI],
57*4f680cc6SAli Bahrami 	    ehdr->e_ident[EI_ABIVERSION], CONV_FMT_DECIMAL, &inv_buf2));
585aefb655Srie 	dbg_print(lml, MSG_ORIG(MSG_ELF_MACHINE),
59*4f680cc6SAli Bahrami 	    conv_ehdr_mach(mach, 0, &inv_buf1),
60de777a60Sab 	    conv_ehdr_vers(ehdr->e_version, 0, &inv_buf2));
61c13de8f6Sab 	dbg_print(lml, MSG_ORIG(MSG_ELF_TYPE),
62*4f680cc6SAli Bahrami 	    conv_ehdr_type(osabi, ehdr->e_type, 0, &inv_buf1));
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/*
650bc07c75Srie 	 * Line up the flags differently depending on whether we received a
665aefb655Srie 	 * numeric (e.g. "0x200") or text representation (e.g.
675aefb655Srie 	 * "[ EF_SPARC_SUN_US1 ]").
687c478bd9Sstevel@tonic-gate 	 */
69*4f680cc6SAli Bahrami 	flgs = conv_ehdr_flags(mach, ehdr->e_flags, 0, &flags_buf);
707c478bd9Sstevel@tonic-gate 	if (flgs[0] == '[')
715aefb655Srie 		dbg_print(lml, MSG_ORIG(MSG_ELF_FLAGS_FMT), flgs);
727c478bd9Sstevel@tonic-gate 	else
735aefb655Srie 		dbg_print(lml, MSG_ORIG(MSG_ELF_FLAGS), flgs);
747c478bd9Sstevel@tonic-gate 
750bc07c75Srie 	/*
760bc07c75Srie 	 * The e_shnum, e_shstrndx and e_phnum entries may have a different
770bc07c75Srie 	 * meaning if extended sections exist.
780bc07c75Srie 	 */
79de777a60Sab 	if (ehdr->e_shstrndx == SHN_XINDEX) {
805aefb655Srie 		dbg_print(lml, MSG_ORIG(MSG_ELFX_ESIZE),
815aefb655Srie 		    EC_ADDR(ehdr->e_entry), ehdr->e_ehsize);
820bc07c75Srie 		xshdr++;
830bc07c75Srie 	} else
845aefb655Srie 		dbg_print(lml, MSG_ORIG(MSG_ELF_ESIZE), EC_ADDR(ehdr->e_entry),
857c478bd9Sstevel@tonic-gate 		    ehdr->e_ehsize, ehdr->e_shstrndx);
867c478bd9Sstevel@tonic-gate 
87de777a60Sab 	if (ehdr->e_shnum == 0) {
885aefb655Srie 		dbg_print(lml, MSG_ORIG(MSG_ELFX_SHOFF), EC_OFF(ehdr->e_shoff),
897c478bd9Sstevel@tonic-gate 		    ehdr->e_shentsize);
900bc07c75Srie 		xshdr++;
910bc07c75Srie 	} else
925aefb655Srie 		dbg_print(lml, MSG_ORIG(MSG_ELF_SHOFF), EC_OFF(ehdr->e_shoff),
937c478bd9Sstevel@tonic-gate 		    ehdr->e_shentsize, ehdr->e_shnum);
947c478bd9Sstevel@tonic-gate 
95de777a60Sab 	if (ehdr->e_phnum == PN_XNUM) {
960bc07c75Srie 		dbg_print(lml, MSG_ORIG(MSG_ELFX_PHOFF), EC_OFF(ehdr->e_phoff),
970bc07c75Srie 		    ehdr->e_phentsize);
980bc07c75Srie 		xshdr++;
990bc07c75Srie 	} else
1000bc07c75Srie 		dbg_print(lml, MSG_ORIG(MSG_ELF_PHOFF), EC_OFF(ehdr->e_phoff),
1010bc07c75Srie 		    ehdr->e_phentsize, ehdr->e_phnum);
1027c478bd9Sstevel@tonic-gate 
1030bc07c75Srie 	if ((xshdr == 0) || (shdr0 == NULL))
1047c478bd9Sstevel@tonic-gate 		return;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	/*
1070bc07c75Srie 	 * If we have Extended ELF headers - print shdr[0].
1087c478bd9Sstevel@tonic-gate 	 */
1090bc07c75Srie 	dbg_print(lml, MSG_ORIG(MSG_STR_EMPTY));
1105aefb655Srie 	dbg_print(lml, MSG_ORIG(MSG_SHD0_TITLE));
1110bc07c75Srie 	dbg_print(lml, MSG_ORIG(MSG_SHD0_ADDR), EC_ADDR(shdr0->sh_addr),
112*4f680cc6SAli Bahrami 	    conv_sec_flags(osabi, mach, shdr0->sh_flags, 0, &sec_flags_buf));
1135aefb655Srie 	dbg_print(lml, MSG_ORIG(MSG_SHD0_SIZE), EC_XWORD(shdr0->sh_size),
114*4f680cc6SAli Bahrami 	    conv_sec_type(osabi, mach, shdr0->sh_type, 0, &inv_buf1));
1150bc07c75Srie 	dbg_print(lml, MSG_ORIG(MSG_SHD0_OFFSET), EC_OFF(shdr0->sh_offset),
1167c478bd9Sstevel@tonic-gate 	    EC_XWORD(shdr0->sh_entsize));
1170bc07c75Srie 	dbg_print(lml, MSG_ORIG(MSG_SHD0_LINK), EC_WORD(shdr0->sh_link),
1180bc07c75Srie 	    EC_WORD(shdr0->sh_info));
1190bc07c75Srie 	dbg_print(lml, MSG_ORIG(MSG_SHD0_ALIGN), EC_XWORD(shdr0->sh_addralign));
1207c478bd9Sstevel@tonic-gate }
121