xref: /onnv-gate/usr/src/cmd/sgs/dump/common/fcns.c (revision 9273)
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*9273SAli.Bahrami@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
271976Sab196087  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include	<stdio.h>
310Sstevel@tonic-gate #include	<stdlib.h>
320Sstevel@tonic-gate #include	<unistd.h>
330Sstevel@tonic-gate #include	<string.h>
340Sstevel@tonic-gate #include	<libelf.h>
350Sstevel@tonic-gate #include	<limits.h>
36*9273SAli.Bahrami@Sun.COM #include	"conv.h"
370Sstevel@tonic-gate #include	"dump.h"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate extern int	p_flag;
401976Sab196087 extern char	*prog_name;
410Sstevel@tonic-gate 
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate  * Print the symbols in the archive symbol table.
450Sstevel@tonic-gate  */
460Sstevel@tonic-gate void
471976Sab196087 ar_sym_read(Elf *elf, char *filename)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate 	Elf_Arsym *	arsym;
500Sstevel@tonic-gate 	size_t		cnt, ptr;
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",
54*9273SAli.Bahrami@Sun.COM 		    prog_name, filename);
550Sstevel@tonic-gate 		return;
560Sstevel@tonic-gate 	}
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 	(void) printf("%s:\n", filename);
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	if (!p_flag) {
610Sstevel@tonic-gate 		(void) printf("     **** ARCHIVE SYMBOL TABLE ****\n");
620Sstevel@tonic-gate 		(void) printf("%-8s %s\n\n", "Offset", "Name");
630Sstevel@tonic-gate 	}
640Sstevel@tonic-gate 	for (cnt = 0; cnt < ptr; cnt++, arsym++) {
650Sstevel@tonic-gate 		if (arsym->as_off) {
660Sstevel@tonic-gate 			/* LINTED */
670Sstevel@tonic-gate 			(void) printf("%-8.8x %s\n", (int)arsym->as_off,
680Sstevel@tonic-gate 			    (arsym->as_name ? arsym->as_name : ""));
690Sstevel@tonic-gate 		}
700Sstevel@tonic-gate 	}
710Sstevel@tonic-gate }
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * Print the program execution header.  Input is an opened ELF object file, the
750Sstevel@tonic-gate  * number of structure instances in the header as recorded in the ELF header,
760Sstevel@tonic-gate  * and the filename.
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate void
790Sstevel@tonic-gate dump_exec_header(Elf *elf_file, unsigned nseg, char *filename)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	GElf_Ehdr ehdr;
820Sstevel@tonic-gate 	GElf_Phdr p_phdr;
830Sstevel@tonic-gate 	int counter;
840Sstevel@tonic-gate 	int field;
850Sstevel@tonic-gate 	extern int v_flag, p_flag;
860Sstevel@tonic-gate 	extern char *prog_name;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	if (gelf_getclass(elf_file) == ELFCLASS64)
890Sstevel@tonic-gate 		field = 16;
900Sstevel@tonic-gate 	else
910Sstevel@tonic-gate 		field = 12;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	if (!p_flag) {
940Sstevel@tonic-gate 		(void) printf(" ***** PROGRAM EXECUTION HEADER *****\n");
950Sstevel@tonic-gate 		(void) printf("%-*s%-*s%-*s%s\n",
960Sstevel@tonic-gate 		    field, "Type", field, "Offset",
970Sstevel@tonic-gate 		    field, "Vaddr", "Paddr");
980Sstevel@tonic-gate 		(void) printf("%-*s%-*s%-*s%s\n\n",
990Sstevel@tonic-gate 		    field, "Filesz", field, "Memsz",
1000Sstevel@tonic-gate 		    field, "Flags", "Align");
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	if ((gelf_getehdr(elf_file, &ehdr) == 0) || (ehdr.e_phnum == 0)) {
1040Sstevel@tonic-gate 		return;
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	for (counter = 0; counter < nseg; counter++) {
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 		if (gelf_getphdr(elf_file, counter, &p_phdr) == 0) {
1100Sstevel@tonic-gate 			(void) fprintf(stderr,
111*9273SAli.Bahrami@Sun.COM 			    "%s: %s: premature EOF on program exec header\n",
112*9273SAli.Bahrami@Sun.COM 			    prog_name, filename);
1130Sstevel@tonic-gate 			return;
1140Sstevel@tonic-gate 		}
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 		if (!v_flag) {
1170Sstevel@tonic-gate 			(void) printf(
1180Sstevel@tonic-gate 	"%-*d%-#*llx%-#*llx%-#*llx\n%-#*llx%-#*llx%-*u%-#*llx\n\n",
119*9273SAli.Bahrami@Sun.COM 			    field, EC_WORD(p_phdr.p_type),
120*9273SAli.Bahrami@Sun.COM 			    field, EC_OFF(p_phdr.p_offset),
121*9273SAli.Bahrami@Sun.COM 			    field, EC_ADDR(p_phdr.p_vaddr),
122*9273SAli.Bahrami@Sun.COM 			    field, EC_ADDR(p_phdr.p_paddr),
123*9273SAli.Bahrami@Sun.COM 			    field, EC_XWORD(p_phdr.p_filesz),
124*9273SAli.Bahrami@Sun.COM 			    field, EC_XWORD(p_phdr.p_memsz),
125*9273SAli.Bahrami@Sun.COM 			    field, EC_WORD(p_phdr.p_flags),
126*9273SAli.Bahrami@Sun.COM 			    field, EC_XWORD(p_phdr.p_align));
1270Sstevel@tonic-gate 		} else {
128*9273SAli.Bahrami@Sun.COM 			Conv_inv_buf_t	inv_buf;
129*9273SAli.Bahrami@Sun.COM 
130*9273SAli.Bahrami@Sun.COM 			(void) printf("%-*s", field,
131*9273SAli.Bahrami@Sun.COM 			    conv_phdr_type(ehdr.e_ident[EI_OSABI],
132*9273SAli.Bahrami@Sun.COM 			    ehdr.e_machine, p_phdr.p_type, DUMP_CONVFMT,
133*9273SAli.Bahrami@Sun.COM 			    &inv_buf));
1340Sstevel@tonic-gate 			(void) printf(
135*9273SAli.Bahrami@Sun.COM 			    "%-#*llx%-#*llx%-#*llx\n%-#*llx%-#*llx",
136*9273SAli.Bahrami@Sun.COM 			    field, EC_OFF(p_phdr.p_offset),
137*9273SAli.Bahrami@Sun.COM 			    field, EC_ADDR(p_phdr.p_vaddr),
138*9273SAli.Bahrami@Sun.COM 			    field, EC_ADDR(p_phdr.p_paddr),
139*9273SAli.Bahrami@Sun.COM 			    field, EC_XWORD(p_phdr.p_filesz),
140*9273SAli.Bahrami@Sun.COM 			    field, EC_XWORD(p_phdr.p_memsz));
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 			switch (p_phdr.p_flags) {
1430Sstevel@tonic-gate 			case 0: (void) printf("%-*s", field, "---"); break;
1440Sstevel@tonic-gate 			case PF_X:
1450Sstevel@tonic-gate 				(void) printf("%-*s", field, "--x");
1460Sstevel@tonic-gate 				break;
1470Sstevel@tonic-gate 			case PF_W:
1480Sstevel@tonic-gate 				(void) printf("%-*s", field, "-w-");
1490Sstevel@tonic-gate 				break;
1500Sstevel@tonic-gate 			case PF_W+PF_X:
1510Sstevel@tonic-gate 				(void) printf("%-*s", field, "-wx");
1520Sstevel@tonic-gate 				break;
1530Sstevel@tonic-gate 			case PF_R:
1540Sstevel@tonic-gate 				(void) printf("%-*s", field, "r--");
1550Sstevel@tonic-gate 				break;
1560Sstevel@tonic-gate 			case PF_R+PF_X:
1570Sstevel@tonic-gate 				(void) printf("%-*s", field, "r-x");
1580Sstevel@tonic-gate 				break;
1590Sstevel@tonic-gate 			case PF_R+PF_W:
1600Sstevel@tonic-gate 				(void) printf("%-*s", field, "rw-");
1610Sstevel@tonic-gate 				break;
1620Sstevel@tonic-gate 			case PF_R+PF_W+PF_X:
1630Sstevel@tonic-gate 				(void) printf("%-*s", field, "rwx");
1640Sstevel@tonic-gate 				break;
1650Sstevel@tonic-gate 			default:
1660Sstevel@tonic-gate 				(void) printf("%-*d", field, p_phdr.p_flags);
1670Sstevel@tonic-gate 				break;
1680Sstevel@tonic-gate 			}
1690Sstevel@tonic-gate 			(void) printf(
170*9273SAli.Bahrami@Sun.COM 			    "%-#*llx\n\n", field, EC_XWORD(p_phdr.p_align));
1710Sstevel@tonic-gate 		}
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate }
174