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
5c13de8f6Sab196087 * Common Development and Distribution License (the "License").
6c13de8f6Sab196087 * 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 */
217c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
227c478bd9Sstevel@tonic-gate /* All Rights Reserved */
237c478bd9Sstevel@tonic-gate
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate /*
26*ba7866cdSAli Bahrami * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
277c478bd9Sstevel@tonic-gate */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
33*ba7866cdSAli Bahrami #include <_libelf.h>
347c478bd9Sstevel@tonic-gate #include <limits.h>
354f680cc6SAli Bahrami #include "conv.h"
367c478bd9Sstevel@tonic-gate #include "dump.h"
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate extern int p_flag;
397c478bd9Sstevel@tonic-gate extern char *prog_name;
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate * Print the symbols in the archive symbol table.
447c478bd9Sstevel@tonic-gate */
457c478bd9Sstevel@tonic-gate void
ar_sym_read(Elf * elf,char * filename)467c478bd9Sstevel@tonic-gate ar_sym_read(Elf *elf, char *filename)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate Elf_Arsym * arsym;
49*ba7866cdSAli Bahrami size_t cnt, ptr, is64;
50*ba7866cdSAli Bahrami const char *fmt;
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate if ((arsym = elf_getarsym(elf, &ptr)) == NULL) {
537c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: no archive symbol table\n",
547c478bd9Sstevel@tonic-gate prog_name, filename);
557c478bd9Sstevel@tonic-gate return;
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate
58*ba7866cdSAli Bahrami is64 = (_elf_getarsymwordsize(elf) == 8);
597c478bd9Sstevel@tonic-gate (void) printf("%s:\n", filename);
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate if (!p_flag) {
627c478bd9Sstevel@tonic-gate (void) printf(" **** ARCHIVE SYMBOL TABLE ****\n");
63*ba7866cdSAli Bahrami if (is64) {
647c478bd9Sstevel@tonic-gate (void) printf("%-8s %s\n\n", "Offset", "Name");
65*ba7866cdSAli Bahrami fmt = "%-16.16llx %s\n";
66*ba7866cdSAli Bahrami } else {
67*ba7866cdSAli Bahrami (void) printf("%-8s %s\n\n", "Offset", "Name");
68*ba7866cdSAli Bahrami fmt = "%-8.8llx %s\n";
69*ba7866cdSAli Bahrami }
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate for (cnt = 0; cnt < ptr; cnt++, arsym++) {
72*ba7866cdSAli Bahrami if (arsym->as_off)
73*ba7866cdSAli Bahrami (void) printf(fmt, EC_XWORD(arsym->as_off),
747c478bd9Sstevel@tonic-gate (arsym->as_name ? arsym->as_name : ""));
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate * Print the program execution header. Input is an opened ELF object file, the
807c478bd9Sstevel@tonic-gate * number of structure instances in the header as recorded in the ELF header,
817c478bd9Sstevel@tonic-gate * and the filename.
827c478bd9Sstevel@tonic-gate */
837c478bd9Sstevel@tonic-gate void
dump_exec_header(Elf * elf_file,unsigned nseg,char * filename)847c478bd9Sstevel@tonic-gate dump_exec_header(Elf *elf_file, unsigned nseg, char *filename)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate GElf_Ehdr ehdr;
877c478bd9Sstevel@tonic-gate GElf_Phdr p_phdr;
887c478bd9Sstevel@tonic-gate int counter;
897c478bd9Sstevel@tonic-gate int field;
907c478bd9Sstevel@tonic-gate extern int v_flag, p_flag;
917c478bd9Sstevel@tonic-gate extern char *prog_name;
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate if (gelf_getclass(elf_file) == ELFCLASS64)
947c478bd9Sstevel@tonic-gate field = 16;
957c478bd9Sstevel@tonic-gate else
967c478bd9Sstevel@tonic-gate field = 12;
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate if (!p_flag) {
997c478bd9Sstevel@tonic-gate (void) printf(" ***** PROGRAM EXECUTION HEADER *****\n");
1007c478bd9Sstevel@tonic-gate (void) printf("%-*s%-*s%-*s%s\n",
1017c478bd9Sstevel@tonic-gate field, "Type", field, "Offset",
1027c478bd9Sstevel@tonic-gate field, "Vaddr", "Paddr");
1037c478bd9Sstevel@tonic-gate (void) printf("%-*s%-*s%-*s%s\n\n",
1047c478bd9Sstevel@tonic-gate field, "Filesz", field, "Memsz",
1057c478bd9Sstevel@tonic-gate field, "Flags", "Align");
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate if ((gelf_getehdr(elf_file, &ehdr) == 0) || (ehdr.e_phnum == 0)) {
1097c478bd9Sstevel@tonic-gate return;
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate for (counter = 0; counter < nseg; counter++) {
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate if (gelf_getphdr(elf_file, counter, &p_phdr) == 0) {
1157c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1167c478bd9Sstevel@tonic-gate "%s: %s: premature EOF on program exec header\n",
1177c478bd9Sstevel@tonic-gate prog_name, filename);
1187c478bd9Sstevel@tonic-gate return;
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate if (!v_flag) {
1227c478bd9Sstevel@tonic-gate (void) printf(
1237c478bd9Sstevel@tonic-gate "%-*d%-#*llx%-#*llx%-#*llx\n%-#*llx%-#*llx%-*u%-#*llx\n\n",
1247c478bd9Sstevel@tonic-gate field, EC_WORD(p_phdr.p_type),
1257c478bd9Sstevel@tonic-gate field, EC_OFF(p_phdr.p_offset),
1267c478bd9Sstevel@tonic-gate field, EC_ADDR(p_phdr.p_vaddr),
1277c478bd9Sstevel@tonic-gate field, EC_ADDR(p_phdr.p_paddr),
1287c478bd9Sstevel@tonic-gate field, EC_XWORD(p_phdr.p_filesz),
1297c478bd9Sstevel@tonic-gate field, EC_XWORD(p_phdr.p_memsz),
1307c478bd9Sstevel@tonic-gate field, EC_WORD(p_phdr.p_flags),
1317c478bd9Sstevel@tonic-gate field, EC_XWORD(p_phdr.p_align));
1327c478bd9Sstevel@tonic-gate } else {
1334f680cc6SAli Bahrami Conv_inv_buf_t inv_buf;
1344f680cc6SAli Bahrami
1354f680cc6SAli Bahrami (void) printf("%-*s", field,
1364f680cc6SAli Bahrami conv_phdr_type(ehdr.e_ident[EI_OSABI],
1374f680cc6SAli Bahrami ehdr.e_machine, p_phdr.p_type, DUMP_CONVFMT,
1384f680cc6SAli Bahrami &inv_buf));
1397c478bd9Sstevel@tonic-gate (void) printf(
1407c478bd9Sstevel@tonic-gate "%-#*llx%-#*llx%-#*llx\n%-#*llx%-#*llx",
1417c478bd9Sstevel@tonic-gate field, EC_OFF(p_phdr.p_offset),
1427c478bd9Sstevel@tonic-gate field, EC_ADDR(p_phdr.p_vaddr),
1437c478bd9Sstevel@tonic-gate field, EC_ADDR(p_phdr.p_paddr),
1447c478bd9Sstevel@tonic-gate field, EC_XWORD(p_phdr.p_filesz),
1457c478bd9Sstevel@tonic-gate field, EC_XWORD(p_phdr.p_memsz));
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate switch (p_phdr.p_flags) {
1487c478bd9Sstevel@tonic-gate case 0: (void) printf("%-*s", field, "---"); break;
1497c478bd9Sstevel@tonic-gate case PF_X:
1507c478bd9Sstevel@tonic-gate (void) printf("%-*s", field, "--x");
1517c478bd9Sstevel@tonic-gate break;
1527c478bd9Sstevel@tonic-gate case PF_W:
1537c478bd9Sstevel@tonic-gate (void) printf("%-*s", field, "-w-");
1547c478bd9Sstevel@tonic-gate break;
1557c478bd9Sstevel@tonic-gate case PF_W+PF_X:
1567c478bd9Sstevel@tonic-gate (void) printf("%-*s", field, "-wx");
1577c478bd9Sstevel@tonic-gate break;
1587c478bd9Sstevel@tonic-gate case PF_R:
1597c478bd9Sstevel@tonic-gate (void) printf("%-*s", field, "r--");
1607c478bd9Sstevel@tonic-gate break;
1617c478bd9Sstevel@tonic-gate case PF_R+PF_X:
1627c478bd9Sstevel@tonic-gate (void) printf("%-*s", field, "r-x");
1637c478bd9Sstevel@tonic-gate break;
1647c478bd9Sstevel@tonic-gate case PF_R+PF_W:
1657c478bd9Sstevel@tonic-gate (void) printf("%-*s", field, "rw-");
1667c478bd9Sstevel@tonic-gate break;
1677c478bd9Sstevel@tonic-gate case PF_R+PF_W+PF_X:
1687c478bd9Sstevel@tonic-gate (void) printf("%-*s", field, "rwx");
1697c478bd9Sstevel@tonic-gate break;
1707c478bd9Sstevel@tonic-gate default:
1717c478bd9Sstevel@tonic-gate (void) printf("%-*d", field, p_phdr.p_flags);
1727c478bd9Sstevel@tonic-gate break;
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate (void) printf(
1757c478bd9Sstevel@tonic-gate "%-#*llx\n\n", field, EC_XWORD(p_phdr.p_align));
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate }
179