xref: /onnv-gate/usr/src/cmd/sgs/dump/common/fcns.c (revision 12792:1f56a791e275)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51976Sab196087  * Common Development and Distribution License (the "License").
61976Sab196087  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
220Sstevel@tonic-gate /*	  All Rights Reserved  	*/
230Sstevel@tonic-gate 
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
26*12792SAli.Bahrami@Oracle.COM  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include	<stdio.h>
300Sstevel@tonic-gate #include	<stdlib.h>
310Sstevel@tonic-gate #include	<unistd.h>
320Sstevel@tonic-gate #include	<string.h>
33*12792SAli.Bahrami@Oracle.COM #include	<_libelf.h>
340Sstevel@tonic-gate #include	<limits.h>
359273SAli.Bahrami@Sun.COM #include	"conv.h"
360Sstevel@tonic-gate #include	"dump.h"
370Sstevel@tonic-gate 
380Sstevel@tonic-gate extern int	p_flag;
391976Sab196087 extern char	*prog_name;
400Sstevel@tonic-gate 
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * Print the symbols in the archive symbol table.
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate void
ar_sym_read(Elf * elf,char * filename)461976Sab196087 ar_sym_read(Elf *elf, char *filename)
470Sstevel@tonic-gate {
480Sstevel@tonic-gate 	Elf_Arsym *	arsym;
49*12792SAli.Bahrami@Oracle.COM 	size_t		cnt, ptr, is64;
50*12792SAli.Bahrami@Oracle.COM 	const char	*fmt;
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 	if ((arsym = elf_getarsym(elf, &ptr)) == NULL) {
530Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: no archive symbol table\n",
549273SAli.Bahrami@Sun.COM 		    prog_name, filename);
550Sstevel@tonic-gate 		return;
560Sstevel@tonic-gate 	}
570Sstevel@tonic-gate 
58*12792SAli.Bahrami@Oracle.COM 	is64 = (_elf_getarsymwordsize(elf) == 8);
590Sstevel@tonic-gate 	(void) printf("%s:\n", filename);
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	if (!p_flag) {
620Sstevel@tonic-gate 		(void) printf("     **** ARCHIVE SYMBOL TABLE ****\n");
63*12792SAli.Bahrami@Oracle.COM 		if (is64) {
64*12792SAli.Bahrami@Oracle.COM 			(void) printf("%-8s         %s\n\n", "Offset", "Name");
65*12792SAli.Bahrami@Oracle.COM 			fmt = "%-16.16llx %s\n";
66*12792SAli.Bahrami@Oracle.COM 		} else {
67*12792SAli.Bahrami@Oracle.COM 			(void) printf("%-8s %s\n\n", "Offset", "Name");
68*12792SAli.Bahrami@Oracle.COM 			fmt = "%-8.8llx %s\n";
69*12792SAli.Bahrami@Oracle.COM 		}
700Sstevel@tonic-gate 	}
710Sstevel@tonic-gate 	for (cnt = 0; cnt < ptr; cnt++, arsym++) {
72*12792SAli.Bahrami@Oracle.COM 		if (arsym->as_off)
73*12792SAli.Bahrami@Oracle.COM 			(void) printf(fmt, EC_XWORD(arsym->as_off),
740Sstevel@tonic-gate 			    (arsym->as_name ? arsym->as_name : ""));
750Sstevel@tonic-gate 	}
760Sstevel@tonic-gate }
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate  * Print the program execution header.  Input is an opened ELF object file, the
800Sstevel@tonic-gate  * number of structure instances in the header as recorded in the ELF header,
810Sstevel@tonic-gate  * and the filename.
820Sstevel@tonic-gate  */
830Sstevel@tonic-gate void
dump_exec_header(Elf * elf_file,unsigned nseg,char * filename)840Sstevel@tonic-gate dump_exec_header(Elf *elf_file, unsigned nseg, char *filename)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate 	GElf_Ehdr ehdr;
870Sstevel@tonic-gate 	GElf_Phdr p_phdr;
880Sstevel@tonic-gate 	int counter;
890Sstevel@tonic-gate 	int field;
900Sstevel@tonic-gate 	extern int v_flag, p_flag;
910Sstevel@tonic-gate 	extern char *prog_name;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
940Sstevel@tonic-gate 		field = 16;
950Sstevel@tonic-gate 	else
960Sstevel@tonic-gate 		field = 12;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if (!p_flag) {
990Sstevel@tonic-gate 		(void) printf(" ***** PROGRAM EXECUTION HEADER *****\n");
1000Sstevel@tonic-gate 		(void) printf("%-*s%-*s%-*s%s\n",
1010Sstevel@tonic-gate 		    field, "Type", field, "Offset",
1020Sstevel@tonic-gate 		    field, "Vaddr", "Paddr");
1030Sstevel@tonic-gate 		(void) printf("%-*s%-*s%-*s%s\n\n",
1040Sstevel@tonic-gate 		    field, "Filesz", field, "Memsz",
1050Sstevel@tonic-gate 		    field, "Flags", "Align");
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	if ((gelf_getehdr(elf_file, &ehdr) == 0) || (ehdr.e_phnum == 0)) {
1090Sstevel@tonic-gate 		return;
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	for (counter = 0; counter < nseg; counter++) {
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 		if (gelf_getphdr(elf_file, counter, &p_phdr) == 0) {
1150Sstevel@tonic-gate 			(void) fprintf(stderr,
1169273SAli.Bahrami@Sun.COM 			    "%s: %s: premature EOF on program exec header\n",
1179273SAli.Bahrami@Sun.COM 			    prog_name, filename);
1180Sstevel@tonic-gate 			return;
1190Sstevel@tonic-gate 		}
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 		if (!v_flag) {
1220Sstevel@tonic-gate 			(void) printf(
1230Sstevel@tonic-gate 	"%-*d%-#*llx%-#*llx%-#*llx\n%-#*llx%-#*llx%-*u%-#*llx\n\n",
1249273SAli.Bahrami@Sun.COM 			    field, EC_WORD(p_phdr.p_type),
1259273SAli.Bahrami@Sun.COM 			    field, EC_OFF(p_phdr.p_offset),
1269273SAli.Bahrami@Sun.COM 			    field, EC_ADDR(p_phdr.p_vaddr),
1279273SAli.Bahrami@Sun.COM 			    field, EC_ADDR(p_phdr.p_paddr),
1289273SAli.Bahrami@Sun.COM 			    field, EC_XWORD(p_phdr.p_filesz),
1299273SAli.Bahrami@Sun.COM 			    field, EC_XWORD(p_phdr.p_memsz),
1309273SAli.Bahrami@Sun.COM 			    field, EC_WORD(p_phdr.p_flags),
1319273SAli.Bahrami@Sun.COM 			    field, EC_XWORD(p_phdr.p_align));
1320Sstevel@tonic-gate 		} else {
1339273SAli.Bahrami@Sun.COM 			Conv_inv_buf_t	inv_buf;
1349273SAli.Bahrami@Sun.COM 
1359273SAli.Bahrami@Sun.COM 			(void) printf("%-*s", field,
1369273SAli.Bahrami@Sun.COM 			    conv_phdr_type(ehdr.e_ident[EI_OSABI],
1379273SAli.Bahrami@Sun.COM 			    ehdr.e_machine, p_phdr.p_type, DUMP_CONVFMT,
1389273SAli.Bahrami@Sun.COM 			    &inv_buf));
1390Sstevel@tonic-gate 			(void) printf(
1409273SAli.Bahrami@Sun.COM 			    "%-#*llx%-#*llx%-#*llx\n%-#*llx%-#*llx",
1419273SAli.Bahrami@Sun.COM 			    field, EC_OFF(p_phdr.p_offset),
1429273SAli.Bahrami@Sun.COM 			    field, EC_ADDR(p_phdr.p_vaddr),
1439273SAli.Bahrami@Sun.COM 			    field, EC_ADDR(p_phdr.p_paddr),
1449273SAli.Bahrami@Sun.COM 			    field, EC_XWORD(p_phdr.p_filesz),
1459273SAli.Bahrami@Sun.COM 			    field, EC_XWORD(p_phdr.p_memsz));
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 			switch (p_phdr.p_flags) {
1480Sstevel@tonic-gate 			case 0: (void) printf("%-*s", field, "---"); break;
1490Sstevel@tonic-gate 			case PF_X:
1500Sstevel@tonic-gate 				(void) printf("%-*s", field, "--x");
1510Sstevel@tonic-gate 				break;
1520Sstevel@tonic-gate 			case PF_W:
1530Sstevel@tonic-gate 				(void) printf("%-*s", field, "-w-");
1540Sstevel@tonic-gate 				break;
1550Sstevel@tonic-gate 			case PF_W+PF_X:
1560Sstevel@tonic-gate 				(void) printf("%-*s", field, "-wx");
1570Sstevel@tonic-gate 				break;
1580Sstevel@tonic-gate 			case PF_R:
1590Sstevel@tonic-gate 				(void) printf("%-*s", field, "r--");
1600Sstevel@tonic-gate 				break;
1610Sstevel@tonic-gate 			case PF_R+PF_X:
1620Sstevel@tonic-gate 				(void) printf("%-*s", field, "r-x");
1630Sstevel@tonic-gate 				break;
1640Sstevel@tonic-gate 			case PF_R+PF_W:
1650Sstevel@tonic-gate 				(void) printf("%-*s", field, "rw-");
1660Sstevel@tonic-gate 				break;
1670Sstevel@tonic-gate 			case PF_R+PF_W+PF_X:
1680Sstevel@tonic-gate 				(void) printf("%-*s", field, "rwx");
1690Sstevel@tonic-gate 				break;
1700Sstevel@tonic-gate 			default:
1710Sstevel@tonic-gate 				(void) printf("%-*d", field, p_phdr.p_flags);
1720Sstevel@tonic-gate 				break;
1730Sstevel@tonic-gate 			}
1740Sstevel@tonic-gate 			(void) printf(
1759273SAli.Bahrami@Sun.COM 			    "%-#*llx\n\n", field, EC_XWORD(p_phdr.p_align));
1760Sstevel@tonic-gate 		}
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate }
179