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 51618Srie * Common Development and Distribution License (the "License"). 61618Srie * 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 */ 211618Srie 220Sstevel@tonic-gate /* 230Sstevel@tonic-gate * Copyright (c) 1988 AT&T 240Sstevel@tonic-gate * All Rights Reserved 250Sstevel@tonic-gate * 26*3492Sab196087 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 270Sstevel@tonic-gate * Use is subject to license terms. 280Sstevel@tonic-gate */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 310Sstevel@tonic-gate 321976Sab196087 /* Get definitions for the relocation types supported. */ 331976Sab196087 #define ELF_TARGET_ALL 341976Sab196087 350Sstevel@tonic-gate #include <stdio.h> 360Sstevel@tonic-gate #include <stdlib.h> 370Sstevel@tonic-gate #include <locale.h> 380Sstevel@tonic-gate #include <unistd.h> 390Sstevel@tonic-gate #include <libelf.h> 400Sstevel@tonic-gate #include <link.h> 411976Sab196087 #include <sys/elf.h> 420Sstevel@tonic-gate #include <sys/machelf.h> 430Sstevel@tonic-gate #include <fcntl.h> 440Sstevel@tonic-gate #include <sys/stat.h> 450Sstevel@tonic-gate #include <errno.h> 460Sstevel@tonic-gate #include <string.h> 470Sstevel@tonic-gate #include "sgs.h" 480Sstevel@tonic-gate #include "conv.h" 490Sstevel@tonic-gate #include "dump.h" 500Sstevel@tonic-gate 510Sstevel@tonic-gate 520Sstevel@tonic-gate #define OPTSTR "agcd:fhn:oprstvCLT:V?" /* option string for getopt() */ 530Sstevel@tonic-gate 541976Sab196087 /* 551976Sab196087 * DUMP_CONVFMT defines the libconv formatting options we want to use: 561976Sab196087 * - Unknown items to be printed as integers using decimal formatting 571976Sab196087 * - The "Dump Style" versions of strings. 581976Sab196087 */ 591976Sab196087 #define DUMP_CONVFMT (CONV_FMT_DECIMAL|CONV_FMT_ALTDUMP) 601976Sab196087 610Sstevel@tonic-gate const char *UNKNOWN = "<unknown>"; 620Sstevel@tonic-gate 630Sstevel@tonic-gate static SCNTAB *p_symtab, *p_head_scns, *p_dynsym; 640Sstevel@tonic-gate 650Sstevel@tonic-gate static int 660Sstevel@tonic-gate x_flag = 0, /* option requires section header table */ 670Sstevel@tonic-gate z_flag = 0, /* process files within an archive */ 680Sstevel@tonic-gate rn_flag = 0; /* dump named relocation information */ 690Sstevel@tonic-gate 700Sstevel@tonic-gate static int 710Sstevel@tonic-gate /* flags: ?_flag corresponds to ? option */ 720Sstevel@tonic-gate a_flag = 0, /* dump archive header of each member of archive */ 730Sstevel@tonic-gate g_flag = 0, /* dump archive symbol table */ 740Sstevel@tonic-gate c_flag = 0, /* dump the string table */ 750Sstevel@tonic-gate d_flag = 0, /* dump range of sections */ 760Sstevel@tonic-gate f_flag = 0, /* dump each file header */ 770Sstevel@tonic-gate h_flag = 0, /* dump section headers */ 780Sstevel@tonic-gate n_flag = 0, /* dump named section */ 790Sstevel@tonic-gate o_flag = 0, /* dump each program execution header */ 800Sstevel@tonic-gate r_flag = 0, /* dump relocation information */ 810Sstevel@tonic-gate s_flag = 0, /* dump section contents */ 820Sstevel@tonic-gate t_flag = 0, /* dump symbol table entries */ 830Sstevel@tonic-gate C_flag = 0, /* dump decoded C++ symbol names */ 840Sstevel@tonic-gate L_flag = 0, /* dump dynamic linking information */ 850Sstevel@tonic-gate T_flag = 0, /* dump symbol table range */ 860Sstevel@tonic-gate V_flag = 0; /* dump version information */ 870Sstevel@tonic-gate 880Sstevel@tonic-gate int p_flag = 0, /* suppress printing of headings */ 890Sstevel@tonic-gate v_flag = 0; /* print information in verbose form */ 900Sstevel@tonic-gate 910Sstevel@tonic-gate static int 920Sstevel@tonic-gate d_low = 0, /* range for use with -d */ 930Sstevel@tonic-gate d_hi = 0, 940Sstevel@tonic-gate d_num = 0; 950Sstevel@tonic-gate 960Sstevel@tonic-gate static int 970Sstevel@tonic-gate T_low = 0, /* range for use with -T */ 980Sstevel@tonic-gate T_hi = 0, 990Sstevel@tonic-gate T_num = 0; 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate static char *name = NULL; /* for use with -n option */ 1020Sstevel@tonic-gate char *prog_name; 1030Sstevel@tonic-gate static int errflag = 0; 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate static struct stab_list_s { 1060Sstevel@tonic-gate struct stab_list_s *next; 1070Sstevel@tonic-gate char *strings; 1080Sstevel@tonic-gate size_t size; 1090Sstevel@tonic-gate } *StringTableList = (void *)0; 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate extern void ar_sym_read(); 1120Sstevel@tonic-gate extern void dump_exec_header(); 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate /* 1160Sstevel@tonic-gate * Get the section descriptor and set the size of the 1170Sstevel@tonic-gate * data returned. Data is byte-order converted. 1180Sstevel@tonic-gate */ 1190Sstevel@tonic-gate void * 1200Sstevel@tonic-gate get_scndata(Elf_Scn *fd_scn, size_t *size) 1210Sstevel@tonic-gate { 1220Sstevel@tonic-gate Elf_Data *p_data; 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate p_data = 0; 1250Sstevel@tonic-gate if ((p_data = elf_getdata(fd_scn, p_data)) == 0 || 1260Sstevel@tonic-gate p_data->d_size == 0) { 1270Sstevel@tonic-gate return (NULL); 1280Sstevel@tonic-gate } 1290Sstevel@tonic-gate *size = p_data->d_size; 1300Sstevel@tonic-gate return (p_data->d_buf); 1310Sstevel@tonic-gate } 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate /* 1340Sstevel@tonic-gate * Get the section descriptor and set the size of the 1350Sstevel@tonic-gate * data returned. Data is raw (i.e., not byte-order converted). 1360Sstevel@tonic-gate */ 1370Sstevel@tonic-gate static void * 1380Sstevel@tonic-gate get_rawscn(Elf_Scn *fd_scn, size_t *size) 1390Sstevel@tonic-gate { 1400Sstevel@tonic-gate Elf_Data *p_data; 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate p_data = 0; 1430Sstevel@tonic-gate if ((p_data = elf_rawdata(fd_scn, p_data)) == 0 || 1440Sstevel@tonic-gate p_data->d_size == 0) { 1450Sstevel@tonic-gate return (NULL); 1460Sstevel@tonic-gate } 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate *size = p_data->d_size; 1490Sstevel@tonic-gate return (p_data->d_buf); 1500Sstevel@tonic-gate } 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate /* 1530Sstevel@tonic-gate * Print out a usage message in short form when program is invoked 1540Sstevel@tonic-gate * with insufficient or no arguments, and in long form when given 1550Sstevel@tonic-gate * either a ? or an invalid option. 1560Sstevel@tonic-gate */ 1570Sstevel@tonic-gate static void 1580Sstevel@tonic-gate usage() 1590Sstevel@tonic-gate { 1600Sstevel@tonic-gate (void) fprintf(stderr, 1610Sstevel@tonic-gate "Usage: %s [-%s] file(s) ...\n", prog_name, OPTSTR); 1620Sstevel@tonic-gate if (errflag) { 1630Sstevel@tonic-gate (void) fprintf(stderr, 1640Sstevel@tonic-gate "\t\t[-a dump archive header of each member of archive]\n\ 1650Sstevel@tonic-gate [-g dump archive global symbol table]\n\ 1660Sstevel@tonic-gate [-c dump the string table]\n\ 1670Sstevel@tonic-gate [-d dump range of sections]\n\ 1680Sstevel@tonic-gate [-f dump each file header]\n\ 1690Sstevel@tonic-gate [-h dump section headers]\n\ 1700Sstevel@tonic-gate [-n dump named section]\n\ 1710Sstevel@tonic-gate [-o dump each program execution header]\n\ 1720Sstevel@tonic-gate [-p suppress printing of headings]\n\ 1730Sstevel@tonic-gate [-r dump relocation information]\n\ 1740Sstevel@tonic-gate [-s dump section contents]\n\ 1750Sstevel@tonic-gate [-t dump symbol table entries]\n\ 1760Sstevel@tonic-gate [-v print information in verbose form]\n\ 1770Sstevel@tonic-gate [-C dump decoded C++ symbol names]\n\ 1780Sstevel@tonic-gate [-L dump the .dynamic structure]\n\ 1790Sstevel@tonic-gate [-T dump symbol table range]\n\ 1800Sstevel@tonic-gate [-V dump version information]\n"); 1810Sstevel@tonic-gate } 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate /* 1850Sstevel@tonic-gate * Set a range. Input is a character string, a lower 1860Sstevel@tonic-gate * bound and an upper bound. This function converts 1870Sstevel@tonic-gate * a character string into its correct integer values, 1880Sstevel@tonic-gate * setting the first value as the lower bound, and 1890Sstevel@tonic-gate * the second value as the upper bound. If more values 1900Sstevel@tonic-gate * are given they are ignored with a warning. 1910Sstevel@tonic-gate */ 1920Sstevel@tonic-gate static void 1930Sstevel@tonic-gate set_range(char *s, int *low, int *high) 1940Sstevel@tonic-gate { 1950Sstevel@tonic-gate char *w; 1960Sstevel@tonic-gate char *lasts; 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate while ((w = strtok_r(s, ",", &lasts)) != NULL) { 1990Sstevel@tonic-gate if (!(*low)) 2000Sstevel@tonic-gate /* LINTED */ 2010Sstevel@tonic-gate *low = (int)atol(w); 2020Sstevel@tonic-gate else 2030Sstevel@tonic-gate if (!(*high)) 2040Sstevel@tonic-gate /* LINTED */ 2050Sstevel@tonic-gate *high = (int)atol(w); 2060Sstevel@tonic-gate else { 2070Sstevel@tonic-gate (void) fprintf(stderr, 2080Sstevel@tonic-gate "%s: too many arguments - %s ignored\n", 2090Sstevel@tonic-gate prog_name, w); 2100Sstevel@tonic-gate return; 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate s = NULL; 2130Sstevel@tonic-gate } /* end while */ 2140Sstevel@tonic-gate } 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate /* 2180Sstevel@tonic-gate * Print static shared library information. 2190Sstevel@tonic-gate */ 2200Sstevel@tonic-gate static void 2210Sstevel@tonic-gate print_static(SCNTAB *l_scns, char *filename) 2220Sstevel@tonic-gate { 2230Sstevel@tonic-gate size_t section_size; 2240Sstevel@tonic-gate unsigned char *strtab; 2250Sstevel@tonic-gate unsigned char *path, buf[1024]; 2260Sstevel@tonic-gate unsigned long *temp; 2270Sstevel@tonic-gate unsigned long total, topath; 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate (void) printf("\n **** STATIC SHARED LIBRARY INFORMATION ****\n"); 2300Sstevel@tonic-gate (void) printf("\n%s:\n", filename); 2310Sstevel@tonic-gate (void) printf("\t"); 2320Sstevel@tonic-gate section_size = 0; 2330Sstevel@tonic-gate if ((strtab = (unsigned char *) 2340Sstevel@tonic-gate get_scndata(l_scns->p_sd, §ion_size)) == NULL) { 2350Sstevel@tonic-gate return; 2360Sstevel@tonic-gate } 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate while (section_size != 0) { 2390Sstevel@tonic-gate /* LINTED */ 2400Sstevel@tonic-gate temp = (unsigned long *)strtab; 2410Sstevel@tonic-gate total = temp[0]; 2420Sstevel@tonic-gate topath = temp[1]; 2430Sstevel@tonic-gate path = strtab + (topath*sizeof (long)); 2440Sstevel@tonic-gate (void) strncpy((char *)buf, (char *)path, 2450Sstevel@tonic-gate (total - topath)*sizeof (long)); 2460Sstevel@tonic-gate (void) fprintf(stdout, "%s\n", buf); 2470Sstevel@tonic-gate strtab += total*sizeof (long); 2480Sstevel@tonic-gate section_size -= (total*sizeof (long)); 2490Sstevel@tonic-gate } 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate /* 2530Sstevel@tonic-gate * Print raw data in hexidecimal. Input is the section data to 2540Sstevel@tonic-gate * be printed out and the size of the data. Output is relative 2550Sstevel@tonic-gate * to a table lookup in dumpmap.h. 2560Sstevel@tonic-gate */ 2570Sstevel@tonic-gate static void 2580Sstevel@tonic-gate print_rawdata(unsigned char *p_sec, size_t size) 2590Sstevel@tonic-gate { 2600Sstevel@tonic-gate size_t j; 2610Sstevel@tonic-gate size_t count; 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate count = 1; 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate (void) printf("\t"); 2660Sstevel@tonic-gate for (j = size/sizeof (short); j != 0; --j, ++count) { 2670Sstevel@tonic-gate (void) printf("%.2x %.2x ", p_sec[0], p_sec[1]); 2680Sstevel@tonic-gate p_sec += 2; 2690Sstevel@tonic-gate if (count == 12) { 2700Sstevel@tonic-gate (void) printf("\n\t"); 2710Sstevel@tonic-gate count = 0; 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate } 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate /* 2760Sstevel@tonic-gate * take care of last byte if odd byte section 2770Sstevel@tonic-gate */ 2780Sstevel@tonic-gate if ((size & 0x1L) == 1L) 2790Sstevel@tonic-gate (void) printf("%.2x", *p_sec); 2800Sstevel@tonic-gate (void) printf("\n"); 2810Sstevel@tonic-gate } 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate /* 2860Sstevel@tonic-gate * Print relocation data of type SHT_RELA 2870Sstevel@tonic-gate * If d_flag, print data corresponding only to 2880Sstevel@tonic-gate * the section or range of sections specified. 2890Sstevel@tonic-gate * If n_flag, print data corresponding only to 2900Sstevel@tonic-gate * the named section. 2910Sstevel@tonic-gate */ 2920Sstevel@tonic-gate static void 2930Sstevel@tonic-gate print_rela(Elf *elf_file, SCNTAB *p_scns, Elf_Data *rdata, Elf_Data *sym_data, 2940Sstevel@tonic-gate GElf_Ehdr * p_ehdr, size_t reloc_size, size_t sym_size, char *filename, 2950Sstevel@tonic-gate SCNTAB *reloc_symtab) 2960Sstevel@tonic-gate { 2970Sstevel@tonic-gate GElf_Rela rela; 2980Sstevel@tonic-gate GElf_Sym sym; 2990Sstevel@tonic-gate size_t no_entries; 3000Sstevel@tonic-gate size_t rel_entsize; 3010Sstevel@tonic-gate size_t no_syms; 3020Sstevel@tonic-gate int type, symid; 3030Sstevel@tonic-gate static int n_title = 0; 3040Sstevel@tonic-gate int ndx = 0; 3050Sstevel@tonic-gate char *sym_name; 3060Sstevel@tonic-gate int adj = 0; 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate if (gelf_getclass(elf_file) == ELFCLASS64) 3091618Srie adj = 8; 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate rel_entsize = p_scns->p_shdr.sh_entsize; 3120Sstevel@tonic-gate if ((rel_entsize == 0) || 3130Sstevel@tonic-gate (rel_entsize > p_scns->p_shdr.sh_size)) { 3140Sstevel@tonic-gate rel_entsize = gelf_fsize(elf_file, ELF_T_RELA, 1, 3150Sstevel@tonic-gate EV_CURRENT); 3160Sstevel@tonic-gate } 3170Sstevel@tonic-gate no_entries = reloc_size / rel_entsize; 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate no_syms = sym_size / gelf_fsize(elf_file, ELF_T_SYM, 1, EV_CURRENT); 3200Sstevel@tonic-gate while (no_entries--) { 3210Sstevel@tonic-gate (void) gelf_getrela(rdata, ndx, &rela); 3220Sstevel@tonic-gate /* LINTED */ 3230Sstevel@tonic-gate type = (int)GELF_R_TYPE(rela.r_info); 3240Sstevel@tonic-gate /* LINTED */ 3250Sstevel@tonic-gate symid = (int)GELF_R_SYM(rela.r_info); 3260Sstevel@tonic-gate /* LINTED */ 3270Sstevel@tonic-gate if ((symid > (no_syms - 1)) || (symid < 0)) { 3280Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: invalid symbol table " 3290Sstevel@tonic-gate "offset - %d - in %s\n", prog_name, filename, 3300Sstevel@tonic-gate symid, p_scns->scn_name); 3310Sstevel@tonic-gate ndx++; 3320Sstevel@tonic-gate continue; 3330Sstevel@tonic-gate } 3340Sstevel@tonic-gate (void) gelf_getsym(sym_data, symid, &sym); 3350Sstevel@tonic-gate sym_name = (char *)elf_strptr(elf_file, 3360Sstevel@tonic-gate reloc_symtab->p_shdr.sh_link, sym.st_name); 3370Sstevel@tonic-gate if (sym_name == NULL) 3380Sstevel@tonic-gate sym_name = (char *)UNKNOWN; 3390Sstevel@tonic-gate if (r_flag && rn_flag) { 3400Sstevel@tonic-gate if (strcmp(name, p_scns->scn_name) != 0) { 3410Sstevel@tonic-gate ndx++; 3420Sstevel@tonic-gate continue; 3430Sstevel@tonic-gate } 3440Sstevel@tonic-gate if (!n_title) { 3450Sstevel@tonic-gate (void) printf("\n%s:\n", p_scns->scn_name); 3460Sstevel@tonic-gate (void) printf("%-*s%-*s%-*s%s\n\n", 3470Sstevel@tonic-gate 12 + adj, "Offset", 22, "Symndx", 3480Sstevel@tonic-gate 16, "Type", "Addend"); 3490Sstevel@tonic-gate n_title = 1; 3500Sstevel@tonic-gate } 3510Sstevel@tonic-gate } 3520Sstevel@tonic-gate if (d_flag) { 3530Sstevel@tonic-gate if (!d_hi) 3540Sstevel@tonic-gate d_hi = d_low; 3550Sstevel@tonic-gate if ((symid < d_low) || (symid > d_hi)) { 3560Sstevel@tonic-gate ndx++; 3570Sstevel@tonic-gate continue; 3580Sstevel@tonic-gate } 3590Sstevel@tonic-gate } 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate (void) printf("%-#*llx", 12 + adj, EC_XWORD(rela.r_offset)); 3620Sstevel@tonic-gate if (!v_flag) { 3630Sstevel@tonic-gate (void) printf("%-22d%-18d", symid, type); 3640Sstevel@tonic-gate } else { 3650Sstevel@tonic-gate if (strlen(sym_name)) { 3660Sstevel@tonic-gate size_t len = strlen(sym_name) + 1; 3670Sstevel@tonic-gate char tmpstr[10]; 3680Sstevel@tonic-gate if (len > 22) { 3690Sstevel@tonic-gate (void) sprintf(tmpstr, "%%-%ds", 3700Sstevel@tonic-gate /* LINTED */ 3710Sstevel@tonic-gate (int)len); 3722352Sab196087 /*LINTED: E_SEC_PRINTF_VAR_FMT*/ 3730Sstevel@tonic-gate (void) printf(tmpstr, sym_name); 3740Sstevel@tonic-gate } else 3750Sstevel@tonic-gate (void) printf("%-22s", sym_name); 3761976Sab196087 } else { 3770Sstevel@tonic-gate (void) printf("%-22d", symid); 3781976Sab196087 } 3791976Sab196087 (void) printf("%-20s", 3801976Sab196087 conv_reloc_type(p_ehdr->e_machine, 3811976Sab196087 type, DUMP_CONVFMT)); 3820Sstevel@tonic-gate } 3830Sstevel@tonic-gate (void) printf("%lld\n", EC_SXWORD(rela.r_addend)); 3840Sstevel@tonic-gate ndx++; 3850Sstevel@tonic-gate } 3860Sstevel@tonic-gate } 3870Sstevel@tonic-gate 3880Sstevel@tonic-gate /* 3890Sstevel@tonic-gate * Print relocation data of type SHT_REL. 3900Sstevel@tonic-gate * If d_flag, print data corresponding only to 3910Sstevel@tonic-gate * the section or range of sections specified. 3920Sstevel@tonic-gate * If n_flag, print data corresponding only to 3930Sstevel@tonic-gate * the named section. 3940Sstevel@tonic-gate */ 3950Sstevel@tonic-gate static void 3960Sstevel@tonic-gate print_rel(Elf *elf_file, SCNTAB *p_scns, Elf_Data *rdata, Elf_Data *sym_data, 3970Sstevel@tonic-gate GElf_Ehdr *p_ehdr, size_t reloc_size, size_t sym_size, char *filename, 3980Sstevel@tonic-gate SCNTAB *reloc_symtab) 3990Sstevel@tonic-gate { 4000Sstevel@tonic-gate GElf_Rel rel; 4010Sstevel@tonic-gate GElf_Sym sym; 4020Sstevel@tonic-gate size_t no_entries; 4030Sstevel@tonic-gate size_t rel_entsize; 4040Sstevel@tonic-gate int type, symid; 4050Sstevel@tonic-gate size_t no_syms; 4060Sstevel@tonic-gate static int n_title = 0; 4070Sstevel@tonic-gate int ndx = 0; 4080Sstevel@tonic-gate char *sym_name; 4090Sstevel@tonic-gate int adj = 0; 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate if (gelf_getclass(elf_file) == ELFCLASS64) 4121618Srie adj = 8; 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate rel_entsize = p_scns->p_shdr.sh_entsize; 4150Sstevel@tonic-gate if ((rel_entsize == 0) || 4160Sstevel@tonic-gate (rel_entsize > p_scns->p_shdr.sh_size)) { 4170Sstevel@tonic-gate rel_entsize = gelf_fsize(elf_file, ELF_T_REL, 1, 4180Sstevel@tonic-gate EV_CURRENT); 4190Sstevel@tonic-gate } 4200Sstevel@tonic-gate no_entries = reloc_size / rel_entsize; 4210Sstevel@tonic-gate 4220Sstevel@tonic-gate no_syms = sym_size / gelf_fsize(elf_file, ELF_T_SYM, 1, EV_CURRENT); 4230Sstevel@tonic-gate while (no_entries--) { 4240Sstevel@tonic-gate (void) gelf_getrel(rdata, ndx, &rel); 4250Sstevel@tonic-gate /* LINTED */ 4260Sstevel@tonic-gate type = (int)GELF_R_TYPE(rel.r_info); 4270Sstevel@tonic-gate /* LINTED */ 4280Sstevel@tonic-gate symid = (int)GELF_R_SYM(rel.r_info); 4290Sstevel@tonic-gate /* LINTED */ 4300Sstevel@tonic-gate if ((symid > (no_syms - 1)) || (symid < 0)) { 4310Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: invalid symbol table " 4320Sstevel@tonic-gate "offset - %d - in %s\n", prog_name, filename, 4330Sstevel@tonic-gate symid, p_scns->scn_name); 4340Sstevel@tonic-gate ndx++; 4350Sstevel@tonic-gate continue; 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate (void) gelf_getsym(sym_data, symid, &sym); 4380Sstevel@tonic-gate sym_name = (char *)elf_strptr(elf_file, 4390Sstevel@tonic-gate reloc_symtab->p_shdr.sh_link, sym.st_name); 4400Sstevel@tonic-gate if (sym_name == NULL) 4410Sstevel@tonic-gate sym_name = (char *)UNKNOWN; 4420Sstevel@tonic-gate if (r_flag && rn_flag) { 4430Sstevel@tonic-gate if (strcmp(name, p_scns->scn_name) != 0) { 4440Sstevel@tonic-gate ndx++; 4450Sstevel@tonic-gate continue; 4460Sstevel@tonic-gate } 4470Sstevel@tonic-gate if (!n_title) { 4480Sstevel@tonic-gate (void) printf("\n%s:\n", p_scns->scn_name); 4490Sstevel@tonic-gate (void) printf("%-*s%-*s%s\n\n", 4500Sstevel@tonic-gate 12 + adj, "Offset", 20, "Symndx", "Type"); 4510Sstevel@tonic-gate n_title = 1; 4520Sstevel@tonic-gate } 4530Sstevel@tonic-gate } 4540Sstevel@tonic-gate if (d_flag) { 4550Sstevel@tonic-gate if (!d_hi) 4560Sstevel@tonic-gate d_hi = d_low; 4570Sstevel@tonic-gate if ((symid < d_low) || (symid > d_hi)) { 4580Sstevel@tonic-gate ndx++; 4590Sstevel@tonic-gate continue; 4600Sstevel@tonic-gate } 4610Sstevel@tonic-gate } 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate (void) printf("%-#*llx", 12 + adj, EC_ADDR(rel.r_offset)); 4640Sstevel@tonic-gate if (!v_flag) { 4650Sstevel@tonic-gate (void) printf("%-20d%-18d", symid, type); 4660Sstevel@tonic-gate } else { 4670Sstevel@tonic-gate if (strlen(sym_name)) 4680Sstevel@tonic-gate (void) printf("%-20s", sym_name); 4691976Sab196087 else { 4700Sstevel@tonic-gate (void) printf("%-20d", sym.st_name); 4711976Sab196087 } 4721976Sab196087 (void) printf("%-20s", 4731976Sab196087 conv_reloc_type(p_ehdr->e_machine, 4741976Sab196087 type, DUMP_CONVFMT)); 4750Sstevel@tonic-gate } 4760Sstevel@tonic-gate (void) printf("\n"); 4770Sstevel@tonic-gate ndx++; 4780Sstevel@tonic-gate } 4790Sstevel@tonic-gate } 4800Sstevel@tonic-gate 4810Sstevel@tonic-gate /* demangle C++ names */ 4820Sstevel@tonic-gate static char * 4830Sstevel@tonic-gate demangled_name(char *s) 4840Sstevel@tonic-gate { 4851618Srie static char *buf = NULL; 4861618Srie char *dn; 4871618Srie size_t len; 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate dn = sgs_demangle(s); 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate /* 4920Sstevel@tonic-gate * If not demangled, just return the symbol name 4930Sstevel@tonic-gate */ 4940Sstevel@tonic-gate if (strcmp(s, dn) == 0) 4950Sstevel@tonic-gate return (s); 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate /* 4980Sstevel@tonic-gate * Demangled. Format it 4990Sstevel@tonic-gate */ 5000Sstevel@tonic-gate if (buf != NULL) 5010Sstevel@tonic-gate free(buf); 5020Sstevel@tonic-gate 5031618Srie len = strlen(dn) + strlen(s) + 4; 5041618Srie if ((buf = malloc(len)) == NULL) 5050Sstevel@tonic-gate return (s); 5060Sstevel@tonic-gate 5071618Srie (void) snprintf(buf, len, "%s\t[%s]", dn, s); 5080Sstevel@tonic-gate return (buf); 5090Sstevel@tonic-gate } 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate /* 5120Sstevel@tonic-gate * Print the symbol table. Input is an ELF file descriptor, a 5130Sstevel@tonic-gate * pointer to the symbol table SCNTAB structure, 5140Sstevel@tonic-gate * the number of symbols, a range of symbols to print, 5150Sstevel@tonic-gate * an index which is the number of the 5160Sstevel@tonic-gate * section in the file, and the filename. The number of sections, 5170Sstevel@tonic-gate * the range, and the index are set in 5180Sstevel@tonic-gate * dump_symbol_table, depending on whether -n or -T were set. 5190Sstevel@tonic-gate */ 5200Sstevel@tonic-gate static void 5210Sstevel@tonic-gate print_symtab(Elf *elf_file, SCNTAB *p_symtab, Elf_Data *sym_data, 5220Sstevel@tonic-gate long range, int index) 5230Sstevel@tonic-gate { 5240Sstevel@tonic-gate GElf_Sym sym; 5250Sstevel@tonic-gate int adj = 0; /* field adjustment for elf64 */ 5260Sstevel@tonic-gate Elf32_Word *symshndx = 0; 5270Sstevel@tonic-gate unsigned int nosymshndx = 0; 5280Sstevel@tonic-gate 5290Sstevel@tonic-gate if (gelf_getclass(elf_file) == ELFCLASS64) 5301618Srie adj = 8; 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate while (range > 0) { 5330Sstevel@tonic-gate char *sym_name = (char *)0; 5340Sstevel@tonic-gate int type, bind; 5350Sstevel@tonic-gate int specsec; 5360Sstevel@tonic-gate unsigned int shndx; 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate (void) gelf_getsym(sym_data, index, &sym); 5390Sstevel@tonic-gate type = (int)GELF_ST_TYPE(sym.st_info); 5400Sstevel@tonic-gate bind = (int)GELF_ST_BIND(sym.st_info); 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate if ((sym.st_shndx == SHN_XINDEX) && 5430Sstevel@tonic-gate (symshndx == 0) && (nosymshndx == 0)) { 5440Sstevel@tonic-gate Elf_Scn *_scn; 5450Sstevel@tonic-gate GElf_Shdr _shdr; 5460Sstevel@tonic-gate size_t symscnndx; 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate symscnndx = elf_ndxscn(p_symtab->p_sd); 5490Sstevel@tonic-gate _scn = 0; 5500Sstevel@tonic-gate while ((_scn = elf_nextscn(elf_file, _scn)) != 0) { 5510Sstevel@tonic-gate if (gelf_getshdr(_scn, &_shdr) == 0) 5520Sstevel@tonic-gate break; 5530Sstevel@tonic-gate if ((_shdr.sh_type == SHT_SYMTAB_SHNDX) && 5540Sstevel@tonic-gate /* LINTED */ 5550Sstevel@tonic-gate (_shdr.sh_link == (GElf_Word)symscnndx)) { 5560Sstevel@tonic-gate Elf_Data *_data; 5570Sstevel@tonic-gate 5580Sstevel@tonic-gate if ((_data = elf_getdata(_scn, 0)) == 0) 5590Sstevel@tonic-gate continue; 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate symshndx = (Elf32_Word *)_data->d_buf; 5620Sstevel@tonic-gate nosymshndx = 0; 5630Sstevel@tonic-gate break; 5640Sstevel@tonic-gate } 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate nosymshndx = 1; 5670Sstevel@tonic-gate } 5680Sstevel@tonic-gate 5690Sstevel@tonic-gate if ((symshndx) && (sym.st_shndx == SHN_XINDEX)) { 5700Sstevel@tonic-gate shndx = symshndx[index]; 5710Sstevel@tonic-gate specsec = 0; 5720Sstevel@tonic-gate } else { 5730Sstevel@tonic-gate shndx = sym.st_shndx; 5740Sstevel@tonic-gate if ((sym.st_shndx == SHN_UNDEF) || 5750Sstevel@tonic-gate (sym.st_shndx >= SHN_LORESERVE)) 5760Sstevel@tonic-gate specsec = 1; 5770Sstevel@tonic-gate else 5780Sstevel@tonic-gate specsec = 0; 5790Sstevel@tonic-gate } 5800Sstevel@tonic-gate 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate (void) printf("[%d]\t ", index++); 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate if (v_flag && (type == STT_SPARC_REGISTER)) { 5850Sstevel@tonic-gate /* 5860Sstevel@tonic-gate * The strings "REG_G1" through "REG_G7" are intended 5870Sstevel@tonic-gate * to be consistent with output from elfdump(1). 5880Sstevel@tonic-gate */ 5891976Sab196087 (void) printf("%-*s", 12 + adj, 5901976Sab196087 conv_sym_SPARC_value(sym.st_value, DUMP_CONVFMT)); 5911976Sab196087 } else { 5920Sstevel@tonic-gate (void) printf("0x%-*llx", 10 + adj, 5930Sstevel@tonic-gate EC_ADDR(sym.st_value)); 5941976Sab196087 } 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate (void) printf("%-*lld", 9 + adj, EC_XWORD(sym.st_size)); 5970Sstevel@tonic-gate 5980Sstevel@tonic-gate if (!v_flag) { 5990Sstevel@tonic-gate (void) printf("%d\t\t%d\t%d\t%#x\t", 6000Sstevel@tonic-gate type, bind, (int)sym.st_other, (int)shndx); 6010Sstevel@tonic-gate } else { 6021976Sab196087 GElf_Ehdr p_ehdr; 6031976Sab196087 (void) gelf_getehdr(elf_file, &p_ehdr); 6041976Sab196087 (void) printf("%s\t", 6051976Sab196087 conv_sym_info_type(p_ehdr.e_machine, type, 6061976Sab196087 DUMP_CONVFMT)); 6071976Sab196087 (void) printf("%s", 6081976Sab196087 conv_sym_info_bind(bind, DUMP_CONVFMT)); 6090Sstevel@tonic-gate (void) printf("\t %d\t", EC_WORD(sym.st_other)); 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate if (specsec) { 6121976Sab196087 (void) printf("%s", conv_sym_shndx(shndx)); 6130Sstevel@tonic-gate } else 6140Sstevel@tonic-gate (void) printf("%d", EC_WORD(shndx)); 6150Sstevel@tonic-gate (void) printf("\t"); 6160Sstevel@tonic-gate } 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate /* support machines where NULL-deref causes core dump */ 6190Sstevel@tonic-gate if (sym.st_name == 0) 6200Sstevel@tonic-gate sym_name = (char *)UNKNOWN; 6210Sstevel@tonic-gate else 6220Sstevel@tonic-gate if (C_flag) 6230Sstevel@tonic-gate sym_name = demangled_name( 6240Sstevel@tonic-gate (char *)elf_strptr(elf_file, 6250Sstevel@tonic-gate p_symtab->p_shdr.sh_link, 6260Sstevel@tonic-gate sym.st_name)); 6270Sstevel@tonic-gate else 6280Sstevel@tonic-gate sym_name = (char *)elf_strptr(elf_file, 6290Sstevel@tonic-gate p_symtab->p_shdr.sh_link, 6300Sstevel@tonic-gate sym.st_name); 6310Sstevel@tonic-gate if (sym_name == NULL) 6320Sstevel@tonic-gate sym_name = (char *)UNKNOWN; 6330Sstevel@tonic-gate (void) printf("%s\n", sym_name); 6340Sstevel@tonic-gate 6350Sstevel@tonic-gate range--; 6360Sstevel@tonic-gate } /* end while */ 6370Sstevel@tonic-gate } 6380Sstevel@tonic-gate 6390Sstevel@tonic-gate /* 6400Sstevel@tonic-gate * Print the section header table. Input is the SCNTAB structure, 6410Sstevel@tonic-gate * the number of sections, an index which is the number of the 6420Sstevel@tonic-gate * section in the file, and the filename. The values of the SCNTAB 6430Sstevel@tonic-gate * structure, the number of sections, and the index are set in 6440Sstevel@tonic-gate * dump_shdr depending on whether the -n or -d modifiers were set. 6450Sstevel@tonic-gate */ 6460Sstevel@tonic-gate static void 6470Sstevel@tonic-gate print_shdr(Elf *elf_file, SCNTAB *s, int num_scns, int index) 6480Sstevel@tonic-gate { 6490Sstevel@tonic-gate SCNTAB *p; 6500Sstevel@tonic-gate int num; 6510Sstevel@tonic-gate int field; 6521976Sab196087 GElf_Ehdr p_ehdr; 6530Sstevel@tonic-gate 6540Sstevel@tonic-gate if (gelf_getclass(elf_file) == ELFCLASS64) 6551618Srie field = 21; 6560Sstevel@tonic-gate else 6570Sstevel@tonic-gate field = 13; 6580Sstevel@tonic-gate 6590Sstevel@tonic-gate p = s; 6601976Sab196087 (void) gelf_getehdr(elf_file, &p_ehdr); 6610Sstevel@tonic-gate 6620Sstevel@tonic-gate for (num = 0; num < num_scns; num++, p++) { 6630Sstevel@tonic-gate (void) printf("[%d]\t", index++); 6640Sstevel@tonic-gate if (!v_flag) { 6650Sstevel@tonic-gate (void) printf("%u\t%llu\t", 6660Sstevel@tonic-gate EC_WORD(p->p_shdr.sh_type), 6670Sstevel@tonic-gate EC_XWORD(p->p_shdr.sh_flags)); 6680Sstevel@tonic-gate } else { 6692352Sab196087 /*LINTED: E_SEC_PRINTF_VAR_FMT*/ 6701976Sab196087 (void) printf(conv_sec_type(p_ehdr.e_machine, 6711976Sab196087 p->p_shdr.sh_type, DUMP_CONVFMT)); 6720Sstevel@tonic-gate (void) printf(" "); 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate if (p->p_shdr.sh_flags & SHF_WRITE) 6750Sstevel@tonic-gate (void) printf("W"); 6760Sstevel@tonic-gate else 6770Sstevel@tonic-gate (void) printf("-"); 6780Sstevel@tonic-gate if (p->p_shdr.sh_flags & SHF_ALLOC) 6790Sstevel@tonic-gate (void) printf("A"); 6800Sstevel@tonic-gate else 6810Sstevel@tonic-gate (void) printf("-"); 6820Sstevel@tonic-gate if (p->p_shdr.sh_flags & SHF_EXECINSTR) 6830Sstevel@tonic-gate (void) printf("I"); 6840Sstevel@tonic-gate else 6850Sstevel@tonic-gate (void) printf("-"); 6860Sstevel@tonic-gate 6870Sstevel@tonic-gate if (p->p_shdr.sh_flags & SHF_ORDERED) 6880Sstevel@tonic-gate (void) printf("O"); 6890Sstevel@tonic-gate if (p->p_shdr.sh_flags & SHF_EXCLUDE) 6900Sstevel@tonic-gate (void) printf("E"); 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate (void) printf("\t"); 6930Sstevel@tonic-gate 6940Sstevel@tonic-gate } 6950Sstevel@tonic-gate (void) printf("%-#*llx%-#*llx%-#*llx%s%s\n", 6960Sstevel@tonic-gate field, EC_ADDR(p->p_shdr.sh_addr), 6970Sstevel@tonic-gate field, EC_OFF(p->p_shdr.sh_offset), 6980Sstevel@tonic-gate field, EC_XWORD(p->p_shdr.sh_size), 6990Sstevel@tonic-gate /* compatibility: tab for elf32 */ 7000Sstevel@tonic-gate (field == 13) ? "\t" : " ", p->scn_name); 7010Sstevel@tonic-gate 7020Sstevel@tonic-gate (void) printf("\t%u\t%u\t%-#*llx%-#*llx\n\n", 7030Sstevel@tonic-gate EC_WORD(p->p_shdr.sh_link), 7040Sstevel@tonic-gate EC_WORD(p->p_shdr.sh_info), 7050Sstevel@tonic-gate field, EC_XWORD(p->p_shdr.sh_addralign), 7060Sstevel@tonic-gate field, EC_XWORD(p->p_shdr.sh_entsize)); 7070Sstevel@tonic-gate } 7080Sstevel@tonic-gate } 7090Sstevel@tonic-gate 7100Sstevel@tonic-gate /* 7110Sstevel@tonic-gate * Check that a range of numbers is valid. Input is 7120Sstevel@tonic-gate * a lower bound, an upper bound, a boundary condition, 7130Sstevel@tonic-gate * and the filename. Negative numbers and numbers greater 7140Sstevel@tonic-gate * than the bound are invalid. low must be smaller than hi. 7150Sstevel@tonic-gate * The returned integer is the number of items in the 7160Sstevel@tonic-gate * range if it is valid and -1 otherwise. 7170Sstevel@tonic-gate */ 7180Sstevel@tonic-gate static int 7190Sstevel@tonic-gate check_range(int low, int hi, size_t bound, char *filename) 7200Sstevel@tonic-gate { 7210Sstevel@tonic-gate if (((size_t)low > bound) || (low <= 0)) { 7220Sstevel@tonic-gate (void) fprintf(stderr, 7230Sstevel@tonic-gate "%s: %s: number out of range, %d\n", 7240Sstevel@tonic-gate prog_name, filename, low); 7250Sstevel@tonic-gate return (-1); 7260Sstevel@tonic-gate } 7270Sstevel@tonic-gate if (((size_t)hi > bound) || (hi < 0)) { 7280Sstevel@tonic-gate (void) fprintf(stderr, 7290Sstevel@tonic-gate "%s: %s: number out of range, %d\n", 7300Sstevel@tonic-gate prog_name, filename, hi); 7310Sstevel@tonic-gate return (-1); 7320Sstevel@tonic-gate } 7330Sstevel@tonic-gate 7340Sstevel@tonic-gate if (hi && (low > hi)) { 7350Sstevel@tonic-gate (void) fprintf(stderr, 7360Sstevel@tonic-gate "%s: %s: invalid range, %d,%d\n", 7370Sstevel@tonic-gate prog_name, filename, low, hi); 7380Sstevel@tonic-gate return (-1); 7390Sstevel@tonic-gate } 7400Sstevel@tonic-gate if (hi) 7410Sstevel@tonic-gate return (hi - low + 1); 7420Sstevel@tonic-gate else 7430Sstevel@tonic-gate return (1); 7440Sstevel@tonic-gate } 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate /* 7470Sstevel@tonic-gate * Print relocation information. Since this information is 7480Sstevel@tonic-gate * machine dependent, new sections must be added for each machine 7490Sstevel@tonic-gate * that is supported. Input is an ELF file descriptor, the ELF header, 7500Sstevel@tonic-gate * the SCNTAB structure, the number of sections, and a filename. 7510Sstevel@tonic-gate * Set up necessary information to print relocation information 7520Sstevel@tonic-gate * and call the appropriate print function depending on the 7530Sstevel@tonic-gate * type of relocation information. If the symbol table is 7540Sstevel@tonic-gate * absent, no relocation data is processed. Input is an 7550Sstevel@tonic-gate * ELF file descriptor, the ELF header, the SCNTAB structure, 7560Sstevel@tonic-gate * and the filename. Set range of d_flag and name if n_flag. 7570Sstevel@tonic-gate */ 7580Sstevel@tonic-gate static void 7590Sstevel@tonic-gate dump_reloc_table(Elf *elf_file, GElf_Ehdr *p_ehdr, 7600Sstevel@tonic-gate SCNTAB *p_scns, int num_scns, char *filename) 7610Sstevel@tonic-gate { 7620Sstevel@tonic-gate Elf_Data *rel_data; 7630Sstevel@tonic-gate Elf_Data *sym_data; 7640Sstevel@tonic-gate size_t sym_size; 7650Sstevel@tonic-gate size_t reloc_size; 7660Sstevel@tonic-gate SCNTAB *reloc_symtab; 7670Sstevel@tonic-gate SCNTAB *head_scns; 7680Sstevel@tonic-gate int r_title = 0; 7690Sstevel@tonic-gate int adj = 0; 7700Sstevel@tonic-gate size_t shnum; 7710Sstevel@tonic-gate 7720Sstevel@tonic-gate if (gelf_getclass(elf_file) == ELFCLASS64) 7731618Srie adj = 8; 7740Sstevel@tonic-gate 7750Sstevel@tonic-gate if ((!p_flag) && (!r_title)) { 7760Sstevel@tonic-gate (void) printf("\n **** RELOCATION INFORMATION ****\n"); 7770Sstevel@tonic-gate r_title = 1; 7780Sstevel@tonic-gate } 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate while (num_scns-- > 0) { 7810Sstevel@tonic-gate if ((p_scns->p_shdr.sh_type != SHT_RELA) && 7820Sstevel@tonic-gate (p_scns->p_shdr.sh_type != SHT_REL)) { 7830Sstevel@tonic-gate p_scns++; 7840Sstevel@tonic-gate continue; 7850Sstevel@tonic-gate } 7860Sstevel@tonic-gate 7870Sstevel@tonic-gate head_scns = p_head_scns; 7880Sstevel@tonic-gate 7890Sstevel@tonic-gate if (elf_getshnum(elf_file, &shnum) == 0) { 7900Sstevel@tonic-gate (void) fprintf(stderr, 7910Sstevel@tonic-gate "%s: %s: elf_getshnum failed: %s\n", 7920Sstevel@tonic-gate prog_name, filename, elf_errmsg(-1)); 7930Sstevel@tonic-gate return; 7940Sstevel@tonic-gate } 7950Sstevel@tonic-gate 7960Sstevel@tonic-gate if ((p_scns->p_shdr.sh_link == 0) || 7970Sstevel@tonic-gate /* LINTED */ 7980Sstevel@tonic-gate (p_scns->p_shdr.sh_link >= (GElf_Word)shnum)) { 7990Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: invalid sh_link field: " 8000Sstevel@tonic-gate "section #: %d sh_link: %d\n", 8010Sstevel@tonic-gate /* LINTED */ 8020Sstevel@tonic-gate prog_name, filename, (int)elf_ndxscn(p_scns->p_sd), 8030Sstevel@tonic-gate (int)p_scns->p_shdr.sh_link); 8040Sstevel@tonic-gate return; 8050Sstevel@tonic-gate } 8060Sstevel@tonic-gate head_scns += (p_scns->p_shdr.sh_link -1); 8070Sstevel@tonic-gate 8080Sstevel@tonic-gate if (head_scns->p_shdr.sh_type == SHT_SYMTAB) { 8090Sstevel@tonic-gate reloc_symtab = p_symtab; 8100Sstevel@tonic-gate } else if (head_scns->p_shdr.sh_type == SHT_DYNSYM) { 8110Sstevel@tonic-gate reloc_symtab = p_dynsym; 8120Sstevel@tonic-gate } else { 8130Sstevel@tonic-gate (void) fprintf(stderr, 8140Sstevel@tonic-gate "%s: %s: could not get symbol table\n", prog_name, filename); 8150Sstevel@tonic-gate return; 8160Sstevel@tonic-gate } 8170Sstevel@tonic-gate 8180Sstevel@tonic-gate sym_data = NULL; 8190Sstevel@tonic-gate sym_size = 0; 8200Sstevel@tonic-gate reloc_size = 0; 8210Sstevel@tonic-gate 8220Sstevel@tonic-gate if ((sym_data = elf_getdata(reloc_symtab->p_sd, NULL)) == NULL) { 8230Sstevel@tonic-gate (void) fprintf(stderr, 8240Sstevel@tonic-gate "%s: %s: no symbol table data\n", prog_name, filename); 8250Sstevel@tonic-gate return; 8260Sstevel@tonic-gate } 8270Sstevel@tonic-gate sym_size = sym_data->d_size; 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate if (p_scns == NULL) { 8300Sstevel@tonic-gate (void) fprintf(stderr, 8310Sstevel@tonic-gate "%s: %s: no section table data\n", prog_name, filename); 8320Sstevel@tonic-gate return; 8330Sstevel@tonic-gate } 8340Sstevel@tonic-gate 8350Sstevel@tonic-gate if (p_scns->p_shdr.sh_type == SHT_RELA) { 8360Sstevel@tonic-gate if (!n_flag && r_flag) 8370Sstevel@tonic-gate (void) printf("\n%s:\n", p_scns->scn_name); 8380Sstevel@tonic-gate if (!p_flag && (!n_flag && r_flag)) 8390Sstevel@tonic-gate (void) printf("%-*s%-*s%-*s%s\n\n", 8400Sstevel@tonic-gate 12 + adj, "Offset", 22, "Symndx", 8410Sstevel@tonic-gate 18, "Type", "Addend"); 8420Sstevel@tonic-gate if ((rel_data = elf_getdata(p_scns->p_sd, NULL)) == NULL) { 8430Sstevel@tonic-gate (void) fprintf(stderr, 8440Sstevel@tonic-gate "%s: %s: no relocation information\n", prog_name, filename); 8450Sstevel@tonic-gate return; 8460Sstevel@tonic-gate } 8470Sstevel@tonic-gate reloc_size = rel_data->d_size; 8480Sstevel@tonic-gate 8490Sstevel@tonic-gate if (n_flag) { 8500Sstevel@tonic-gate rn_flag = 1; 8510Sstevel@tonic-gate print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr, 8520Sstevel@tonic-gate reloc_size, sym_size, filename, reloc_symtab); 8530Sstevel@tonic-gate } 8540Sstevel@tonic-gate if (d_flag) { 8550Sstevel@tonic-gate rn_flag = 0; 8560Sstevel@tonic-gate print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr, 8570Sstevel@tonic-gate reloc_size, sym_size, filename, reloc_symtab); 8580Sstevel@tonic-gate } 8590Sstevel@tonic-gate if (!n_flag && !d_flag) 8600Sstevel@tonic-gate print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr, 8610Sstevel@tonic-gate reloc_size, sym_size, filename, reloc_symtab); 8620Sstevel@tonic-gate } else { 8630Sstevel@tonic-gate if (p_scns->p_shdr.sh_type == SHT_REL) { 8640Sstevel@tonic-gate if (!n_flag && r_flag) 8650Sstevel@tonic-gate (void) printf("\n%s:\n", p_scns->scn_name); 8660Sstevel@tonic-gate if (!p_flag && (!n_flag && r_flag)) { 8670Sstevel@tonic-gate (void) printf("%-*s%-*s%s\n\n", 8680Sstevel@tonic-gate 12 + adj, "Offset", 20, "Symndx", "Type"); 8690Sstevel@tonic-gate } 8700Sstevel@tonic-gate if ((rel_data = elf_getdata(p_scns->p_sd, NULL)) 8710Sstevel@tonic-gate == NULL) { 8720Sstevel@tonic-gate (void) fprintf(stderr, 8730Sstevel@tonic-gate "%s: %s: no relocation information\n", prog_name, filename); 8740Sstevel@tonic-gate return; 8750Sstevel@tonic-gate } 8760Sstevel@tonic-gate reloc_size = rel_data->d_size; 8770Sstevel@tonic-gate if (n_flag) { 8780Sstevel@tonic-gate rn_flag = 1; 8790Sstevel@tonic-gate print_rel(elf_file, p_scns, rel_data, sym_data, 8800Sstevel@tonic-gate p_ehdr, reloc_size, sym_size, 8810Sstevel@tonic-gate filename, reloc_symtab); 8820Sstevel@tonic-gate } 8830Sstevel@tonic-gate if (d_flag) { 8840Sstevel@tonic-gate rn_flag = 0; 8850Sstevel@tonic-gate print_rel(elf_file, p_scns, rel_data, sym_data, 8860Sstevel@tonic-gate p_ehdr, reloc_size, sym_size, 8870Sstevel@tonic-gate filename, reloc_symtab); 8880Sstevel@tonic-gate } 8890Sstevel@tonic-gate if (!n_flag && !d_flag) 8900Sstevel@tonic-gate print_rel(elf_file, p_scns, rel_data, sym_data, 8910Sstevel@tonic-gate p_ehdr, reloc_size, sym_size, 8920Sstevel@tonic-gate filename, reloc_symtab); 8930Sstevel@tonic-gate } 8940Sstevel@tonic-gate } 8950Sstevel@tonic-gate p_scns++; 8960Sstevel@tonic-gate } 8970Sstevel@tonic-gate } 8980Sstevel@tonic-gate 8990Sstevel@tonic-gate /* 9000Sstevel@tonic-gate * Print out the string tables. Input is an opened ELF file, 9010Sstevel@tonic-gate * the SCNTAB structure, the number of sections, and the filename. 9020Sstevel@tonic-gate * Since there can be more than one string table, all sections are 9030Sstevel@tonic-gate * examined and any with the correct type are printed out. 9040Sstevel@tonic-gate */ 9050Sstevel@tonic-gate static void 9060Sstevel@tonic-gate dump_string_table(SCNTAB *s, int num_scns) 9070Sstevel@tonic-gate { 9080Sstevel@tonic-gate size_t section_size; 9090Sstevel@tonic-gate unsigned char *strtab; 9100Sstevel@tonic-gate int beg_of_string; 9110Sstevel@tonic-gate int counter = 0; 9120Sstevel@tonic-gate int str_off; 9130Sstevel@tonic-gate int i; 9140Sstevel@tonic-gate 9150Sstevel@tonic-gate if (!p_flag) { 9160Sstevel@tonic-gate (void) printf("\n **** STRING TABLE INFORMATION ****\n"); 9170Sstevel@tonic-gate } 9180Sstevel@tonic-gate 9190Sstevel@tonic-gate for (i = 0; i < num_scns; i++, s++) { 9200Sstevel@tonic-gate if (s->p_shdr.sh_type != SHT_STRTAB) 9210Sstevel@tonic-gate continue; 9220Sstevel@tonic-gate 9230Sstevel@tonic-gate str_off = 0; 9240Sstevel@tonic-gate 9250Sstevel@tonic-gate if (!p_flag) { 9260Sstevel@tonic-gate (void) printf("\n%s:\n", s->scn_name); 9270Sstevel@tonic-gate (void) printf(" <offset> \tName\n"); 9280Sstevel@tonic-gate } 9290Sstevel@tonic-gate section_size = 0; 9300Sstevel@tonic-gate if ((strtab = (unsigned char *) 9310Sstevel@tonic-gate get_scndata(s->p_sd, §ion_size)) == NULL) { 9320Sstevel@tonic-gate continue; 9330Sstevel@tonic-gate } 9340Sstevel@tonic-gate 9350Sstevel@tonic-gate if (section_size != 0) { 9360Sstevel@tonic-gate (void) printf(" <%d> \t", str_off); 9370Sstevel@tonic-gate beg_of_string = 0; 9380Sstevel@tonic-gate while (section_size--) { 9390Sstevel@tonic-gate unsigned char c = *strtab++; 9400Sstevel@tonic-gate 9410Sstevel@tonic-gate if (beg_of_string) { 9420Sstevel@tonic-gate (void) printf(" <%d> \t", str_off); 9430Sstevel@tonic-gate counter++; 9440Sstevel@tonic-gate beg_of_string = 0; 9450Sstevel@tonic-gate } 9460Sstevel@tonic-gate str_off++; 9470Sstevel@tonic-gate switch (c) { 9480Sstevel@tonic-gate case '\0': 9490Sstevel@tonic-gate (void) printf("\n"); 9500Sstevel@tonic-gate beg_of_string = 1; 9510Sstevel@tonic-gate break; 9520Sstevel@tonic-gate default: 9530Sstevel@tonic-gate (void) putchar(c); 9540Sstevel@tonic-gate } 9550Sstevel@tonic-gate } 9560Sstevel@tonic-gate } 9570Sstevel@tonic-gate } 9580Sstevel@tonic-gate (void) printf("\n"); 9590Sstevel@tonic-gate } 9600Sstevel@tonic-gate 9610Sstevel@tonic-gate /* 9620Sstevel@tonic-gate * Print the symbol table. This function does not print the contents 9630Sstevel@tonic-gate * of the symbol table but sets up the parameters and then calls 9640Sstevel@tonic-gate * print_symtab to print the symbols. Calling another function to print 9650Sstevel@tonic-gate * the symbols allows both -T and -n to work correctly 9660Sstevel@tonic-gate * simultaneously. Input is an opened ELF file, a pointer to the 9670Sstevel@tonic-gate * symbol table SCNTAB structure, and the filename. 9680Sstevel@tonic-gate * Set the range of symbols to print if T_flag, and set 9690Sstevel@tonic-gate * name of symbol to print if n_flag. 9700Sstevel@tonic-gate */ 9710Sstevel@tonic-gate static void 9720Sstevel@tonic-gate dump_symbol_table(Elf *elf_file, SCNTAB *p_symtab, char *filename) 9730Sstevel@tonic-gate { 9740Sstevel@tonic-gate Elf_Data *sym_data; 9750Sstevel@tonic-gate GElf_Sym T_range, n_range; /* for use with -T and -n */ 9760Sstevel@tonic-gate size_t count = 0; 9770Sstevel@tonic-gate size_t sym_size; 9780Sstevel@tonic-gate int index = 1; 9790Sstevel@tonic-gate int found_it = 0; 9800Sstevel@tonic-gate int i; 9810Sstevel@tonic-gate int adj = 0; /* field adjustment for elf64 */ 9820Sstevel@tonic-gate 9830Sstevel@tonic-gate if (gelf_getclass(elf_file) == ELFCLASS64) 9841618Srie adj = 8; 9850Sstevel@tonic-gate 9860Sstevel@tonic-gate if (p_symtab == NULL) { 9870Sstevel@tonic-gate (void) fprintf(stderr, 9880Sstevel@tonic-gate "%s: %s: could not get symbol table\n", prog_name, filename); 9890Sstevel@tonic-gate return; 9900Sstevel@tonic-gate } 9910Sstevel@tonic-gate 9920Sstevel@tonic-gate /* get symbol table data */ 9930Sstevel@tonic-gate sym_data = NULL; 9940Sstevel@tonic-gate sym_size = 0; 9950Sstevel@tonic-gate if ((sym_data = 9960Sstevel@tonic-gate elf_getdata(p_symtab->p_sd, NULL)) == NULL) { 9970Sstevel@tonic-gate (void) printf("\n%s:\n", p_symtab->scn_name); 9980Sstevel@tonic-gate (void) printf("No symbol table data\n"); 9990Sstevel@tonic-gate return; 10000Sstevel@tonic-gate } 10010Sstevel@tonic-gate sym_size = sym_data->d_size; 10020Sstevel@tonic-gate 10030Sstevel@tonic-gate count = sym_size / p_symtab->p_shdr.sh_entsize; 10040Sstevel@tonic-gate 10050Sstevel@tonic-gate if (n_flag && t_flag && !T_flag) { 10060Sstevel@tonic-gate /* LINTED */ 10070Sstevel@tonic-gate for (i = 1; i < count; i++) { 10080Sstevel@tonic-gate (void) gelf_getsym(sym_data, i, &n_range); 10090Sstevel@tonic-gate if (strcmp(name, (char *) 10100Sstevel@tonic-gate elf_strptr(elf_file, 10110Sstevel@tonic-gate p_symtab->p_shdr.sh_link, 10120Sstevel@tonic-gate n_range.st_name)) != 0) { 10130Sstevel@tonic-gate continue; 10140Sstevel@tonic-gate } else { 10150Sstevel@tonic-gate found_it = 1; 10160Sstevel@tonic-gate if (!p_flag) { 10170Sstevel@tonic-gate (void) printf( 10180Sstevel@tonic-gate "\n ***** SYMBOL TABLE INFORMATION *****\n"); 10190Sstevel@tonic-gate (void) printf( 10200Sstevel@tonic-gate "[Index] %-*s%-*sType\tBind\tOther\tShndx\tName", 10210Sstevel@tonic-gate 12 + adj, "Value", 9 + adj, "Size"); 10220Sstevel@tonic-gate } 10230Sstevel@tonic-gate (void) printf("\n%s:\n", p_symtab->scn_name); 10240Sstevel@tonic-gate print_symtab(elf_file, p_symtab, sym_data, 10250Sstevel@tonic-gate 1, i); 10260Sstevel@tonic-gate } 10270Sstevel@tonic-gate } /* end for */ 10280Sstevel@tonic-gate if (!found_it) { 10290Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s not found\n", 10300Sstevel@tonic-gate prog_name, filename, name); 10310Sstevel@tonic-gate } 10320Sstevel@tonic-gate } else if (T_flag) { 10330Sstevel@tonic-gate T_num = check_range(T_low, T_hi, count, filename); 10340Sstevel@tonic-gate if (T_num < 0) 10350Sstevel@tonic-gate return; 10360Sstevel@tonic-gate 10370Sstevel@tonic-gate (void) gelf_getsym(sym_data, T_low-1, &T_range); 10380Sstevel@tonic-gate index = T_low; 10390Sstevel@tonic-gate 10400Sstevel@tonic-gate if (!p_flag) { 10410Sstevel@tonic-gate (void) printf( 10420Sstevel@tonic-gate "\n ***** SYMBOL TABLE INFORMATION *****\n"); 10430Sstevel@tonic-gate (void) printf( 10440Sstevel@tonic-gate "[Index] %-*s%-*sType\tBind\tOther\tShndx\tName", 10450Sstevel@tonic-gate 12 + adj, "Value", 9 + adj, "Size"); 10460Sstevel@tonic-gate } 10470Sstevel@tonic-gate (void) printf("\n%s:\n", p_symtab->scn_name); 10480Sstevel@tonic-gate print_symtab(elf_file, p_symtab, sym_data, T_num, index); 10490Sstevel@tonic-gate } else { 10500Sstevel@tonic-gate if (!p_flag) { 10510Sstevel@tonic-gate (void) printf( 10520Sstevel@tonic-gate "\n ***** SYMBOL TABLE INFORMATION *****\n"); 10530Sstevel@tonic-gate (void) printf( 10540Sstevel@tonic-gate "[Index] %-*s%-*sType\tBind\tOther\tShndx\tName", 10550Sstevel@tonic-gate 12 + adj, "Value", 9 + adj, "Size"); 10560Sstevel@tonic-gate } 10570Sstevel@tonic-gate (void) printf("\n%s:\n", p_symtab->scn_name); 10580Sstevel@tonic-gate print_symtab(elf_file, p_symtab, sym_data, count-1, 1); 10590Sstevel@tonic-gate } 10600Sstevel@tonic-gate } 10610Sstevel@tonic-gate 10621698Sab196087 10631698Sab196087 /* 10640Sstevel@tonic-gate * Print dynamic linking information. Input is an ELF 10650Sstevel@tonic-gate * file descriptor, the SCNTAB structure, the number of 10660Sstevel@tonic-gate * sections, and the filename. 10670Sstevel@tonic-gate */ 10680Sstevel@tonic-gate static void 10690Sstevel@tonic-gate dump_dynamic(Elf *elf_file, SCNTAB *p_scns, int num_scns, char *filename) 10700Sstevel@tonic-gate { 10712352Sab196087 #define pdyn_Fmtptr "%#llx" 10721698Sab196087 10730Sstevel@tonic-gate Elf_Data *dyn_data; 10740Sstevel@tonic-gate GElf_Dyn p_dyn; 10750Sstevel@tonic-gate GElf_Phdr p_phdr; 10760Sstevel@tonic-gate GElf_Ehdr p_ehdr; 10770Sstevel@tonic-gate int index = 1; 10780Sstevel@tonic-gate int lib_scns = num_scns; 10790Sstevel@tonic-gate SCNTAB *l_scns = p_scns; 10800Sstevel@tonic-gate int header_num = 0; 10812352Sab196087 const char *str; 10822352Sab196087 10832352Sab196087 (void) gelf_getehdr(elf_file, &p_ehdr); 10840Sstevel@tonic-gate 10850Sstevel@tonic-gate if (!p_flag) 10860Sstevel@tonic-gate (void) printf("\n **** DYNAMIC SECTION INFORMATION ****\n"); 10870Sstevel@tonic-gate 10880Sstevel@tonic-gate for (; num_scns > 0; num_scns--, p_scns++) { 10890Sstevel@tonic-gate GElf_Word link; 10900Sstevel@tonic-gate int ii; 10910Sstevel@tonic-gate 10920Sstevel@tonic-gate 10930Sstevel@tonic-gate if (p_scns->p_shdr.sh_type != SHT_DYNAMIC) 10940Sstevel@tonic-gate continue; 10950Sstevel@tonic-gate 10960Sstevel@tonic-gate if (!p_flag) { 10970Sstevel@tonic-gate (void) printf("%s:\n", p_scns->scn_name); 10980Sstevel@tonic-gate (void) printf("[INDEX]\tTag Value\n"); 10990Sstevel@tonic-gate } 11000Sstevel@tonic-gate 11010Sstevel@tonic-gate if ((dyn_data = elf_getdata(p_scns->p_sd, NULL)) == 0) { 11020Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: no data in " 11030Sstevel@tonic-gate "%s section\n", prog_name, filename, 11040Sstevel@tonic-gate p_scns->scn_name); 11050Sstevel@tonic-gate return; 11060Sstevel@tonic-gate } 11070Sstevel@tonic-gate 11080Sstevel@tonic-gate link = p_scns->p_shdr.sh_link; 11090Sstevel@tonic-gate ii = 0; 11100Sstevel@tonic-gate 11110Sstevel@tonic-gate (void) gelf_getdyn(dyn_data, ii++, &p_dyn); 11120Sstevel@tonic-gate while (p_dyn.d_tag != DT_NULL) { 11131976Sab196087 (void) printf("[%d]\t%-15.15s ", index++, 11141976Sab196087 conv_dyn_tag(p_dyn.d_tag, p_ehdr.e_machine, 11151976Sab196087 DUMP_CONVFMT)); 11160Sstevel@tonic-gate 11171698Sab196087 /* 11181698Sab196087 * It would be nice to use a table driven loop 11191698Sab196087 * here, but the address space is too sparse 11201698Sab196087 * and irregular. A switch is simple and robust. 11211698Sab196087 */ 11220Sstevel@tonic-gate switch (p_dyn.d_tag) { 11230Sstevel@tonic-gate /* 11241976Sab196087 * Items with an address value 11250Sstevel@tonic-gate */ 11261976Sab196087 case DT_PLTGOT: 11271976Sab196087 case DT_HASH: 11281976Sab196087 case DT_STRTAB: 11291976Sab196087 case DT_RELA: 11301976Sab196087 case DT_SYMTAB: 11311976Sab196087 case DT_INIT: 11321976Sab196087 case DT_FINI: 11331976Sab196087 case DT_REL: 11341976Sab196087 case DT_DEBUG: 11351976Sab196087 case DT_TEXTREL: 11361976Sab196087 case DT_JMPREL: 11371976Sab196087 case DT_INIT_ARRAY: 11381976Sab196087 case DT_FINI_ARRAY: 11391976Sab196087 case DT_INIT_ARRAYSZ: 11401976Sab196087 case DT_FINI_ARRAYSZ: 11411976Sab196087 case DT_PREINIT_ARRAY: 11421976Sab196087 case DT_PREINIT_ARRAYSZ: 11431976Sab196087 case DT_SUNW_RTLDINF: 11441976Sab196087 case DT_SUNW_CAP: 11452766Sab196087 case DT_SUNW_SYMTAB: 1146*3492Sab196087 case DT_SUNW_SYMSORT: 1147*3492Sab196087 case DT_SUNW_TLSSORT: 11481976Sab196087 case DT_PLTPAD: 11491976Sab196087 case DT_MOVETAB: 11501976Sab196087 case DT_SYMINFO: 11511976Sab196087 case DT_RELACOUNT: 11521976Sab196087 case DT_RELCOUNT: 11531976Sab196087 case DT_VERSYM: 11541976Sab196087 case DT_VERDEF: 11551976Sab196087 case DT_VERDEFNUM: 11561976Sab196087 case DT_VERNEED: 11571976Sab196087 (void) printf(pdyn_Fmtptr, 11581976Sab196087 EC_ADDR(p_dyn.d_un.d_ptr)); 11590Sstevel@tonic-gate break; 11600Sstevel@tonic-gate 11610Sstevel@tonic-gate /* 11621976Sab196087 * Items with a string value 11630Sstevel@tonic-gate */ 11641976Sab196087 case DT_NEEDED: 11651976Sab196087 case DT_SONAME: 11661976Sab196087 case DT_RPATH: 11671976Sab196087 case DT_RUNPATH: 11681976Sab196087 case DT_SUNW_AUXILIARY: 11691976Sab196087 case DT_SUNW_FILTER: 11701976Sab196087 case DT_CONFIG: 11711976Sab196087 case DT_DEPAUDIT: 11721976Sab196087 case DT_AUDIT: 11731976Sab196087 case DT_AUXILIARY: 11741976Sab196087 case DT_USED: 11751976Sab196087 case DT_FILTER: 11761976Sab196087 if (v_flag) { /* Look up the string */ 11771976Sab196087 str = (char *)elf_strptr(elf_file, link, 11781976Sab196087 p_dyn.d_un.d_ptr); 11791976Sab196087 if (!(str && *str)) 11801976Sab196087 str = (char *)UNKNOWN; 11811976Sab196087 (void) printf("%s", str); 11821976Sab196087 } else { /* Show the address */ 11831976Sab196087 (void) printf(pdyn_Fmtptr, 11841976Sab196087 EC_ADDR(p_dyn.d_un.d_ptr)); 11851976Sab196087 } 11860Sstevel@tonic-gate break; 11870Sstevel@tonic-gate 11880Sstevel@tonic-gate /* 11891976Sab196087 * Items with a literal value 11900Sstevel@tonic-gate */ 11911976Sab196087 case DT_PLTRELSZ: 11921976Sab196087 case DT_RELASZ: 11931976Sab196087 case DT_RELAENT: 11941976Sab196087 case DT_STRSZ: 11951976Sab196087 case DT_SYMENT: 11961976Sab196087 case DT_RELSZ: 11971976Sab196087 case DT_RELENT: 11981976Sab196087 case DT_PLTREL: 11991976Sab196087 case DT_BIND_NOW: 12001976Sab196087 case DT_CHECKSUM: 12011976Sab196087 case DT_PLTPADSZ: 12021976Sab196087 case DT_MOVEENT: 12031976Sab196087 case DT_MOVESZ: 12041976Sab196087 case DT_SYMINSZ: 12051976Sab196087 case DT_SYMINENT: 12061976Sab196087 case DT_VERNEEDNUM: 12071976Sab196087 case DT_SPARC_REGISTER: 12082766Sab196087 case DT_SUNW_SYMSZ: 1209*3492Sab196087 case DT_SUNW_SORTENT: 1210*3492Sab196087 case DT_SUNW_SYMSORTSZ: 1211*3492Sab196087 case DT_SUNW_TLSSORTSZ: 12121976Sab196087 (void) printf(pdyn_Fmtptr, 12131976Sab196087 EC_XWORD(p_dyn.d_un.d_val)); 12140Sstevel@tonic-gate break; 12150Sstevel@tonic-gate 12160Sstevel@tonic-gate /* 12171976Sab196087 * Items that are bitmasks 12180Sstevel@tonic-gate */ 12191976Sab196087 case DT_FLAGS: 12201976Sab196087 case DT_FEATURE_1: 12211976Sab196087 case DT_POSFLAG_1: 12221976Sab196087 case DT_FLAGS_1: 12232352Sab196087 str = NULL; 12242352Sab196087 if (v_flag) { 12252352Sab196087 switch (p_dyn.d_tag) { 12262352Sab196087 case DT_FLAGS: 12272352Sab196087 str = conv_dyn_flag( 12282352Sab196087 p_dyn.d_un.d_val, 12292352Sab196087 DUMP_CONVFMT); 12302352Sab196087 break; 12312352Sab196087 case DT_FEATURE_1: 12322352Sab196087 str = conv_dyn_feature1( 12332352Sab196087 p_dyn.d_un.d_val, 12342352Sab196087 DUMP_CONVFMT); 12352352Sab196087 break; 12362352Sab196087 case DT_POSFLAG_1: 12372352Sab196087 str = conv_dyn_posflag1( 12382352Sab196087 p_dyn.d_un.d_val, 12392352Sab196087 DUMP_CONVFMT); 12402352Sab196087 break; 12412352Sab196087 case DT_FLAGS_1: 12422352Sab196087 str = conv_dyn_flag1( 12432352Sab196087 p_dyn.d_un.d_val); 12442352Sab196087 break; 12452352Sab196087 } 12462352Sab196087 } 12472352Sab196087 if (str) { /* Show as string */ 12482352Sab196087 (void) printf("%s", str); 12492352Sab196087 } else { /* Numeric form */ 12502352Sab196087 (void) printf(pdyn_Fmtptr, 12512352Sab196087 EC_ADDR(p_dyn.d_un.d_ptr)); 12522352Sab196087 } 12530Sstevel@tonic-gate break; 12540Sstevel@tonic-gate 12550Sstevel@tonic-gate /* 12561976Sab196087 * Depreciated items with a literal value 12570Sstevel@tonic-gate */ 12581976Sab196087 case DT_DEPRECATED_SPARC_REGISTER: 12591976Sab196087 (void) printf(pdyn_Fmtptr 12601976Sab196087 " (deprecated value)", 12611976Sab196087 EC_XWORD(p_dyn.d_un.d_val)); 12620Sstevel@tonic-gate break; 12631976Sab196087 12641976Sab196087 /* Ignored items */ 12651976Sab196087 case DT_SYMBOLIC: 12661976Sab196087 (void) printf("(ignored)"); 12670Sstevel@tonic-gate break; 12680Sstevel@tonic-gate } 12690Sstevel@tonic-gate (void) printf("\n"); 12700Sstevel@tonic-gate (void) gelf_getdyn(dyn_data, ii++, &p_dyn); 12710Sstevel@tonic-gate } 12720Sstevel@tonic-gate } 12730Sstevel@tonic-gate 12740Sstevel@tonic-gate /* 12750Sstevel@tonic-gate * Check for existence of static shared library information. 12760Sstevel@tonic-gate */ 12770Sstevel@tonic-gate while (header_num < p_ehdr.e_phnum) { 12780Sstevel@tonic-gate (void) gelf_getphdr(elf_file, header_num, &p_phdr); 12790Sstevel@tonic-gate if (p_phdr.p_type == PT_SHLIB) { 12800Sstevel@tonic-gate while (--lib_scns > 0) { 12810Sstevel@tonic-gate if (strcmp(l_scns->scn_name, ".lib") == 0) { 12820Sstevel@tonic-gate print_static(l_scns, filename); 12830Sstevel@tonic-gate } 12840Sstevel@tonic-gate l_scns++; 12850Sstevel@tonic-gate } 12860Sstevel@tonic-gate } 12870Sstevel@tonic-gate header_num++; 12880Sstevel@tonic-gate } 12892352Sab196087 #undef pdyn_Fmtptr 12900Sstevel@tonic-gate } 12910Sstevel@tonic-gate 12920Sstevel@tonic-gate /* 12930Sstevel@tonic-gate * Print the ELF header. Input is an ELF file descriptor 12940Sstevel@tonic-gate * and the filename. If f_flag is set, the ELF header is 12950Sstevel@tonic-gate * printed to stdout, otherwise the function returns after 12960Sstevel@tonic-gate * setting the pointer to the ELF header. Any values which 12970Sstevel@tonic-gate * are not known are printed in decimal. Fields must be updated 12980Sstevel@tonic-gate * as new values are added. 12990Sstevel@tonic-gate */ 13000Sstevel@tonic-gate static GElf_Ehdr * 13010Sstevel@tonic-gate dump_elf_header(Elf *elf_file, char *filename, GElf_Ehdr * elf_head_p) 13020Sstevel@tonic-gate { 13031976Sab196087 int class; 13040Sstevel@tonic-gate int field; 13050Sstevel@tonic-gate 13060Sstevel@tonic-gate if (gelf_getehdr(elf_file, elf_head_p) == NULL) { 13071618Srie (void) fprintf(stderr, "%s: %s: %s\n", prog_name, filename, 13081618Srie elf_errmsg(-1)); 13090Sstevel@tonic-gate return (NULL); 13100Sstevel@tonic-gate } 13110Sstevel@tonic-gate 13120Sstevel@tonic-gate class = (int)elf_head_p->e_ident[4]; 13130Sstevel@tonic-gate 13140Sstevel@tonic-gate if (class == ELFCLASS64) 13151618Srie field = 21; 13160Sstevel@tonic-gate else 13170Sstevel@tonic-gate field = 13; 13180Sstevel@tonic-gate 13190Sstevel@tonic-gate if (!f_flag) 13200Sstevel@tonic-gate return (elf_head_p); 13210Sstevel@tonic-gate 13220Sstevel@tonic-gate if (!p_flag) { 13231618Srie (void) printf("\n **** ELF HEADER ****\n"); 13241618Srie (void) printf("%-*s%-11s%-*sMachine Version\n", 13251618Srie field, "Class", "Data", field, "Type"); 13261618Srie (void) printf("%-*s%-11s%-*sFlags Ehsize\n", 13271618Srie field, "Entry", "Phoff", field, "Shoff"); 13281618Srie (void) printf("%-*s%-11s%-*sShnum Shstrndx\n\n", 13291618Srie field, "Phentsize", "Phnum", field, "Shentsz"); 13300Sstevel@tonic-gate } 13310Sstevel@tonic-gate 13320Sstevel@tonic-gate if (!v_flag) { 13330Sstevel@tonic-gate (void) printf("%-*d%-11d%-*d%-12d%d\n", 13340Sstevel@tonic-gate field, elf_head_p->e_ident[4], 13350Sstevel@tonic-gate elf_head_p->e_ident[5], 13360Sstevel@tonic-gate field, (int)elf_head_p->e_type, 13370Sstevel@tonic-gate (int)elf_head_p->e_machine, 13380Sstevel@tonic-gate elf_head_p->e_version); 13390Sstevel@tonic-gate } else { 13401976Sab196087 (void) printf("%-*s", field, 13411976Sab196087 conv_ehdr_class(class, DUMP_CONVFMT)); 13421976Sab196087 (void) printf("%-11s", 13431976Sab196087 conv_ehdr_data(elf_head_p->e_ident[5], DUMP_CONVFMT)); 13441976Sab196087 (void) printf("%-*s", field, 13451976Sab196087 conv_ehdr_type(elf_head_p->e_type, DUMP_CONVFMT)); 13461976Sab196087 (void) printf("%-12s", 13471976Sab196087 conv_ehdr_mach(elf_head_p->e_machine, DUMP_CONVFMT)); 13481976Sab196087 (void) printf("%s\n", 13491976Sab196087 conv_ehdr_vers(elf_head_p->e_version, DUMP_CONVFMT)); 13500Sstevel@tonic-gate } 13510Sstevel@tonic-gate (void) printf("%-#*llx%-#11llx%-#*llx%-#12x%#x\n", 13520Sstevel@tonic-gate field, EC_ADDR(elf_head_p->e_entry), 13530Sstevel@tonic-gate EC_OFF(elf_head_p->e_phoff), 13540Sstevel@tonic-gate field, EC_OFF(elf_head_p->e_shoff), 13550Sstevel@tonic-gate EC_WORD(elf_head_p->e_flags), 13560Sstevel@tonic-gate EC_WORD(elf_head_p->e_ehsize)); 13570Sstevel@tonic-gate if (!v_flag || (elf_head_p->e_shstrndx != SHN_XINDEX)) { 13580Sstevel@tonic-gate (void) printf("%-#*x%-11u%-#*x%-12u%u\n", 13590Sstevel@tonic-gate field, EC_WORD(elf_head_p->e_phentsize), 13600Sstevel@tonic-gate EC_WORD(elf_head_p->e_phnum), 13610Sstevel@tonic-gate field, EC_WORD(elf_head_p->e_shentsize), 13620Sstevel@tonic-gate EC_WORD(elf_head_p->e_shnum), 13630Sstevel@tonic-gate EC_WORD(elf_head_p->e_shstrndx)); 13640Sstevel@tonic-gate } else { 13650Sstevel@tonic-gate (void) printf("%-#*x%-11u%-#*x%-12uXINDEX\n", 13660Sstevel@tonic-gate field, EC_WORD(elf_head_p->e_phentsize), 13670Sstevel@tonic-gate EC_WORD(elf_head_p->e_phnum), 13680Sstevel@tonic-gate field, EC_WORD(elf_head_p->e_shentsize), 13690Sstevel@tonic-gate EC_WORD(elf_head_p->e_shnum)); 13700Sstevel@tonic-gate } 13710Sstevel@tonic-gate if ((elf_head_p->e_shnum == 0) && (elf_head_p->e_shoff > 0)) { 13720Sstevel@tonic-gate Elf_Scn *scn; 13730Sstevel@tonic-gate GElf_Shdr shdr0; 13740Sstevel@tonic-gate int field; 13750Sstevel@tonic-gate 13760Sstevel@tonic-gate if (gelf_getclass(elf_file) == ELFCLASS64) 13771618Srie field = 21; 13780Sstevel@tonic-gate else 13791618Srie field = 13; 13800Sstevel@tonic-gate if (!p_flag) { 13810Sstevel@tonic-gate (void) printf("\n **** SECTION HEADER[0] " 13820Sstevel@tonic-gate "{Elf Extensions} ****\n"); 13830Sstevel@tonic-gate (void) printf( 13840Sstevel@tonic-gate "[No]\tType\tFlags\t%-*s %-*s%-*s%sName\n", 13850Sstevel@tonic-gate field, "Addr", field, "Offset", field, 13860Sstevel@tonic-gate "Size(shnum)", 13870Sstevel@tonic-gate /* compatibility: tab for elf32 */ 13881618Srie (field == 13) ? "\t" : " "); 13890Sstevel@tonic-gate (void) printf("\tLn(strndx) Info\t%-*s Entsize\n", 13900Sstevel@tonic-gate field, "Adralgn"); 13910Sstevel@tonic-gate } 13920Sstevel@tonic-gate if ((scn = elf_getscn(elf_file, 0)) == NULL) { 13930Sstevel@tonic-gate (void) fprintf(stderr, 13940Sstevel@tonic-gate "%s: %s: elf_getscn failed: %s\n", 13950Sstevel@tonic-gate prog_name, filename, elf_errmsg(-1)); 13960Sstevel@tonic-gate return (NULL); 13970Sstevel@tonic-gate } 13980Sstevel@tonic-gate if (gelf_getshdr(scn, &shdr0) == 0) { 13990Sstevel@tonic-gate (void) fprintf(stderr, 14000Sstevel@tonic-gate "%s: %s: gelf_getshdr: %s\n", 14010Sstevel@tonic-gate prog_name, filename, elf_errmsg(-1)); 14020Sstevel@tonic-gate return (NULL); 14030Sstevel@tonic-gate } 14040Sstevel@tonic-gate (void) printf("[0]\t%u\t%llu\t", EC_WORD(shdr0.sh_type), 14050Sstevel@tonic-gate EC_XWORD(shdr0.sh_flags)); 14060Sstevel@tonic-gate 14072352Sab196087 (void) printf("%-#*llx %-#*llx%-*llu%s%-*u\n", 14080Sstevel@tonic-gate field, EC_ADDR(shdr0.sh_addr), 14090Sstevel@tonic-gate field, EC_OFF(shdr0.sh_offset), 14100Sstevel@tonic-gate field, EC_XWORD(shdr0.sh_size), 14110Sstevel@tonic-gate /* compatibility: tab for elf32 */ 14121618Srie ((field == 13) ? "\t" : " "), 14130Sstevel@tonic-gate field, EC_WORD(shdr0.sh_name)); 14140Sstevel@tonic-gate 14150Sstevel@tonic-gate (void) printf("\t%u\t%u\t%-#*llx %-#*llx\n", 14160Sstevel@tonic-gate EC_WORD(shdr0.sh_link), 14170Sstevel@tonic-gate EC_WORD(shdr0.sh_info), 14180Sstevel@tonic-gate field, EC_XWORD(shdr0.sh_addralign), 14190Sstevel@tonic-gate field, EC_XWORD(shdr0.sh_entsize)); 14200Sstevel@tonic-gate } 14210Sstevel@tonic-gate (void) printf("\n"); 14220Sstevel@tonic-gate 14230Sstevel@tonic-gate return (elf_head_p); 14240Sstevel@tonic-gate } 14250Sstevel@tonic-gate 14260Sstevel@tonic-gate /* 14270Sstevel@tonic-gate * Print section contents. Input is an ELF file descriptor, 14280Sstevel@tonic-gate * the ELF header, the SCNTAB structure, 14290Sstevel@tonic-gate * the number of symbols, and the filename. 14300Sstevel@tonic-gate * The number of sections, 14310Sstevel@tonic-gate * and the offset into the SCNTAB structure will be 14320Sstevel@tonic-gate * set in dump_section if d_flag or n_flag are set. 14330Sstevel@tonic-gate * If v_flag is set, sections which can be interpreted will 14340Sstevel@tonic-gate * be interpreted, otherwise raw data will be output in hexidecimal. 14350Sstevel@tonic-gate */ 14360Sstevel@tonic-gate static void 14370Sstevel@tonic-gate print_section(Elf *elf_file, 14380Sstevel@tonic-gate GElf_Ehdr *p_ehdr, SCNTAB *p, int num_scns, char *filename) 14390Sstevel@tonic-gate { 14400Sstevel@tonic-gate unsigned char *p_sec; 14410Sstevel@tonic-gate int i; 14420Sstevel@tonic-gate size_t size; 14430Sstevel@tonic-gate 14440Sstevel@tonic-gate for (i = 0; i < num_scns; i++, p++) { 14450Sstevel@tonic-gate GElf_Shdr shdr; 14460Sstevel@tonic-gate 14470Sstevel@tonic-gate size = 0; 14480Sstevel@tonic-gate if (s_flag && !v_flag) 14490Sstevel@tonic-gate p_sec = (unsigned char *)get_rawscn(p->p_sd, &size); 14500Sstevel@tonic-gate else 14510Sstevel@tonic-gate p_sec = (unsigned char *)get_scndata(p->p_sd, &size); 14520Sstevel@tonic-gate 14530Sstevel@tonic-gate if ((gelf_getshdr(p->p_sd, &shdr) != NULL) && 14540Sstevel@tonic-gate (shdr.sh_type == SHT_NOBITS)) { 14550Sstevel@tonic-gate continue; 14560Sstevel@tonic-gate } 14570Sstevel@tonic-gate if (s_flag && !v_flag) { 14580Sstevel@tonic-gate (void) printf("\n%s:\n", p->scn_name); 14590Sstevel@tonic-gate print_rawdata(p_sec, size); 14600Sstevel@tonic-gate continue; 14610Sstevel@tonic-gate } 14620Sstevel@tonic-gate if (shdr.sh_type == SHT_SYMTAB) { 14630Sstevel@tonic-gate dump_symbol_table(elf_file, p, filename); 14640Sstevel@tonic-gate continue; 14650Sstevel@tonic-gate } 14660Sstevel@tonic-gate if (shdr.sh_type == SHT_DYNSYM) { 14670Sstevel@tonic-gate dump_symbol_table(elf_file, p, filename); 14680Sstevel@tonic-gate continue; 14690Sstevel@tonic-gate } 14700Sstevel@tonic-gate if (shdr.sh_type == SHT_STRTAB) { 14710Sstevel@tonic-gate dump_string_table(p, 1); 14720Sstevel@tonic-gate continue; 14730Sstevel@tonic-gate } 14740Sstevel@tonic-gate if (shdr.sh_type == SHT_RELA) { 14750Sstevel@tonic-gate dump_reloc_table(elf_file, p_ehdr, p, 1, filename); 14760Sstevel@tonic-gate continue; 14770Sstevel@tonic-gate } 14780Sstevel@tonic-gate if (shdr.sh_type == SHT_REL) { 14790Sstevel@tonic-gate dump_reloc_table(elf_file, p_ehdr, p, 1, filename); 14800Sstevel@tonic-gate continue; 14810Sstevel@tonic-gate } 14820Sstevel@tonic-gate if (shdr.sh_type == SHT_DYNAMIC) { 14830Sstevel@tonic-gate dump_dynamic(elf_file, p, 1, filename); 14840Sstevel@tonic-gate continue; 14850Sstevel@tonic-gate } 14860Sstevel@tonic-gate 14870Sstevel@tonic-gate (void) printf("\n%s:\n", p->scn_name); 14880Sstevel@tonic-gate print_rawdata(p_sec, size); 14890Sstevel@tonic-gate } 14900Sstevel@tonic-gate (void) printf("\n"); 14910Sstevel@tonic-gate } 14920Sstevel@tonic-gate 14930Sstevel@tonic-gate /* 14940Sstevel@tonic-gate * Print section contents. This function does not print the contents 14950Sstevel@tonic-gate * of the sections but sets up the parameters and then calls 14960Sstevel@tonic-gate * print_section to print the contents. Calling another function to print 14970Sstevel@tonic-gate * the contents allows both -d and -n to work correctly 14980Sstevel@tonic-gate * simultaneously. Input is an ELF file descriptor, the ELF header, 14990Sstevel@tonic-gate * the SCNTAB structure, the number of sections, and the filename. 15000Sstevel@tonic-gate * Set the range of sections if d_flag, and set section name if 15010Sstevel@tonic-gate * n_flag. 15020Sstevel@tonic-gate */ 15030Sstevel@tonic-gate static void 15040Sstevel@tonic-gate dump_section(Elf *elf_file, 15050Sstevel@tonic-gate GElf_Ehdr *p_ehdr, SCNTAB *s, int num_scns, char *filename) 15060Sstevel@tonic-gate { 15070Sstevel@tonic-gate SCNTAB *n_range, *d_range; /* for use with -n and -d modifiers */ 15080Sstevel@tonic-gate int i; 15090Sstevel@tonic-gate int found_it = 0; /* for use with -n section_name */ 15100Sstevel@tonic-gate 15110Sstevel@tonic-gate if (n_flag) { 15120Sstevel@tonic-gate n_range = s; 15130Sstevel@tonic-gate 15140Sstevel@tonic-gate for (i = 0; i < num_scns; i++, n_range++) { 15150Sstevel@tonic-gate if ((strcmp(name, n_range->scn_name)) != 0) 15160Sstevel@tonic-gate continue; 15170Sstevel@tonic-gate else { 15180Sstevel@tonic-gate found_it = 1; 15190Sstevel@tonic-gate print_section(elf_file, p_ehdr, 15200Sstevel@tonic-gate n_range, 1, filename); 15210Sstevel@tonic-gate } 15220Sstevel@tonic-gate } 15230Sstevel@tonic-gate 15240Sstevel@tonic-gate if (!found_it) { 15250Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s not found\n", 15260Sstevel@tonic-gate prog_name, filename, name); 15270Sstevel@tonic-gate } 15280Sstevel@tonic-gate } /* end n_flag */ 15290Sstevel@tonic-gate 15300Sstevel@tonic-gate if (d_flag) { 15310Sstevel@tonic-gate d_range = s; 15320Sstevel@tonic-gate d_num = check_range(d_low, d_hi, num_scns, filename); 15330Sstevel@tonic-gate if (d_num < 0) 15340Sstevel@tonic-gate return; 15350Sstevel@tonic-gate d_range += d_low - 1; 15360Sstevel@tonic-gate 15370Sstevel@tonic-gate print_section(elf_file, p_ehdr, d_range, d_num, filename); 15380Sstevel@tonic-gate } /* end d_flag */ 15390Sstevel@tonic-gate 15400Sstevel@tonic-gate if (!n_flag && !d_flag) 15410Sstevel@tonic-gate print_section(elf_file, p_ehdr, s, num_scns, filename); 15420Sstevel@tonic-gate } 15430Sstevel@tonic-gate 15440Sstevel@tonic-gate /* 15450Sstevel@tonic-gate * Print the section header table. This function does not print the contents 15460Sstevel@tonic-gate * of the section headers but sets up the parameters and then calls 15470Sstevel@tonic-gate * print_shdr to print the contents. Calling another function to print 15480Sstevel@tonic-gate * the contents allows both -d and -n to work correctly 15490Sstevel@tonic-gate * simultaneously. Input is the SCNTAB structure, 15500Sstevel@tonic-gate * the number of sections from the ELF header, and the filename. 15510Sstevel@tonic-gate * Set the range of section headers to print if d_flag, and set 15520Sstevel@tonic-gate * name of section header to print if n_flag. 15530Sstevel@tonic-gate */ 15540Sstevel@tonic-gate static void 15550Sstevel@tonic-gate dump_shdr(Elf *elf_file, SCNTAB *s, int num_scns, char *filename) 15560Sstevel@tonic-gate { 15570Sstevel@tonic-gate 15580Sstevel@tonic-gate SCNTAB *n_range, *d_range; /* for use with -n and -d modifiers */ 15590Sstevel@tonic-gate int field; 15600Sstevel@tonic-gate int i; 15610Sstevel@tonic-gate int found_it = 0; /* for use with -n section_name */ 15620Sstevel@tonic-gate 15630Sstevel@tonic-gate if (gelf_getclass(elf_file) == ELFCLASS64) 15641618Srie field = 21; 15650Sstevel@tonic-gate else 15661618Srie field = 13; 15670Sstevel@tonic-gate 15680Sstevel@tonic-gate if (!p_flag) { 15690Sstevel@tonic-gate (void) printf("\n **** SECTION HEADER TABLE ****\n"); 15700Sstevel@tonic-gate (void) printf("[No]\tType\tFlags\t%-*s %-*s %-*s%sName\n", 15710Sstevel@tonic-gate field, "Addr", field, "Offset", field, "Size", 15720Sstevel@tonic-gate /* compatibility: tab for elf32 */ 15731618Srie (field == 13) ? "\t" : " "); 15740Sstevel@tonic-gate (void) printf("\tLink\tInfo\t%-*s Entsize\n\n", 15750Sstevel@tonic-gate field, "Adralgn"); 15760Sstevel@tonic-gate } 15770Sstevel@tonic-gate 15780Sstevel@tonic-gate if (n_flag) { 15790Sstevel@tonic-gate n_range = s; 15800Sstevel@tonic-gate 15810Sstevel@tonic-gate for (i = 1; i <= num_scns; i++, n_range++) { 15820Sstevel@tonic-gate if ((strcmp(name, n_range->scn_name)) != 0) 15830Sstevel@tonic-gate continue; 15840Sstevel@tonic-gate else { 15850Sstevel@tonic-gate found_it = 1; 15860Sstevel@tonic-gate print_shdr(elf_file, n_range, 1, i); 15870Sstevel@tonic-gate } 15880Sstevel@tonic-gate } 15890Sstevel@tonic-gate 15900Sstevel@tonic-gate if (!found_it) { 15910Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s not found\n", 15920Sstevel@tonic-gate prog_name, filename, name); 15930Sstevel@tonic-gate } 15940Sstevel@tonic-gate } /* end n_flag */ 15950Sstevel@tonic-gate 15960Sstevel@tonic-gate if (d_flag) { 15970Sstevel@tonic-gate d_range = s; 15980Sstevel@tonic-gate d_num = check_range(d_low, d_hi, num_scns, filename); 15990Sstevel@tonic-gate if (d_num < 0) 16000Sstevel@tonic-gate return; 16010Sstevel@tonic-gate d_range += d_low - 1; 16020Sstevel@tonic-gate 16030Sstevel@tonic-gate print_shdr(elf_file, d_range, d_num, d_low); 16040Sstevel@tonic-gate } /* end d_flag */ 16050Sstevel@tonic-gate 16060Sstevel@tonic-gate if (!n_flag && !d_flag) 16070Sstevel@tonic-gate print_shdr(elf_file, s, num_scns, 1); 16080Sstevel@tonic-gate } 16090Sstevel@tonic-gate 16100Sstevel@tonic-gate /* 16110Sstevel@tonic-gate * Process all of the command line options (except 16120Sstevel@tonic-gate * for -a, -g, -f, and -o). All of the options processed 16130Sstevel@tonic-gate * by this function require the presence of the section 16140Sstevel@tonic-gate * header table and will not be processed if it is not present. 16150Sstevel@tonic-gate * Set up a buffer containing section name, section header, 16160Sstevel@tonic-gate * and section descriptor for each section in the file. This 16170Sstevel@tonic-gate * structure is used to avoid duplicate calls to libelf functions. 16180Sstevel@tonic-gate * Structure members for the symbol table, the debugging information, 16190Sstevel@tonic-gate * and the line number information are global. All of the 16200Sstevel@tonic-gate * rest are local. 16210Sstevel@tonic-gate */ 16220Sstevel@tonic-gate static void 16230Sstevel@tonic-gate dump_section_table(Elf *elf_file, GElf_Ehdr *elf_head_p, char *filename) 16240Sstevel@tonic-gate { 16250Sstevel@tonic-gate 16260Sstevel@tonic-gate static SCNTAB *buffer, *p_scns; 16270Sstevel@tonic-gate Elf_Scn *scn = 0; 16280Sstevel@tonic-gate char *s_name = NULL; 16290Sstevel@tonic-gate int found = 0; 16300Sstevel@tonic-gate unsigned int num_scns; 16310Sstevel@tonic-gate size_t shstrndx; 16320Sstevel@tonic-gate size_t shnum; 16330Sstevel@tonic-gate 16340Sstevel@tonic-gate 16350Sstevel@tonic-gate if (elf_getshnum(elf_file, &shnum) == 0) { 16360Sstevel@tonic-gate (void) fprintf(stderr, 16370Sstevel@tonic-gate "%s: %s: elf_getshnum failed: %s\n", 16380Sstevel@tonic-gate prog_name, filename, elf_errmsg(-1)); 16390Sstevel@tonic-gate return; 16400Sstevel@tonic-gate } 16410Sstevel@tonic-gate if (elf_getshstrndx(elf_file, &shstrndx) == 0) { 16420Sstevel@tonic-gate (void) fprintf(stderr, 16430Sstevel@tonic-gate "%s: %s: elf_getshstrndx failed: %s\n", 16440Sstevel@tonic-gate prog_name, filename, elf_errmsg(-1)); 16450Sstevel@tonic-gate return; 16460Sstevel@tonic-gate } 16470Sstevel@tonic-gate 16480Sstevel@tonic-gate if ((buffer = calloc(shnum, sizeof (SCNTAB))) == NULL) { 16490Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: cannot calloc space\n", 16500Sstevel@tonic-gate prog_name, filename); 16510Sstevel@tonic-gate return; 16520Sstevel@tonic-gate } 16530Sstevel@tonic-gate /* LINTED */ 16540Sstevel@tonic-gate num_scns = (int)shnum - 1; 16550Sstevel@tonic-gate 16560Sstevel@tonic-gate p_symtab = (SCNTAB *)0; 16570Sstevel@tonic-gate p_dynsym = (SCNTAB *)0; 16580Sstevel@tonic-gate p_scns = buffer; 16590Sstevel@tonic-gate p_head_scns = buffer; 16600Sstevel@tonic-gate 16610Sstevel@tonic-gate while ((scn = elf_nextscn(elf_file, scn)) != 0) { 16620Sstevel@tonic-gate if ((gelf_getshdr(scn, &buffer->p_shdr)) == 0) { 16630Sstevel@tonic-gate (void) fprintf(stderr, 16640Sstevel@tonic-gate "%s: %s: %s\n", prog_name, filename, elf_errmsg(-1)); 16650Sstevel@tonic-gate return; 16660Sstevel@tonic-gate } 16670Sstevel@tonic-gate s_name = (char *)elf_strptr(elf_file, 16680Sstevel@tonic-gate shstrndx, buffer->p_shdr.sh_name); 16690Sstevel@tonic-gate buffer->scn_name = s_name ? s_name : (char *)UNKNOWN; 16700Sstevel@tonic-gate buffer->p_sd = scn; 16710Sstevel@tonic-gate 16720Sstevel@tonic-gate if (buffer->p_shdr.sh_type == SHT_SYMTAB) { 16730Sstevel@tonic-gate found += 1; 16740Sstevel@tonic-gate p_symtab = buffer; 16750Sstevel@tonic-gate } 16760Sstevel@tonic-gate if (buffer->p_shdr.sh_type == SHT_DYNSYM) 16770Sstevel@tonic-gate p_dynsym = buffer; 16780Sstevel@tonic-gate buffer++; 16790Sstevel@tonic-gate } 16800Sstevel@tonic-gate 16810Sstevel@tonic-gate /* 16820Sstevel@tonic-gate * These functions depend upon the presence of the section header table 16830Sstevel@tonic-gate * and will not be invoked in its absence 16840Sstevel@tonic-gate */ 16850Sstevel@tonic-gate if (h_flag) { 16860Sstevel@tonic-gate dump_shdr(elf_file, p_scns, num_scns, filename); 16870Sstevel@tonic-gate } 16880Sstevel@tonic-gate if (p_symtab && (t_flag || T_flag)) { 16890Sstevel@tonic-gate dump_symbol_table(elf_file, p_symtab, filename); 16900Sstevel@tonic-gate } 16910Sstevel@tonic-gate if (c_flag) { 16920Sstevel@tonic-gate dump_string_table(p_scns, num_scns); 16930Sstevel@tonic-gate } 16940Sstevel@tonic-gate if (r_flag) { 16950Sstevel@tonic-gate dump_reloc_table(elf_file, elf_head_p, 16960Sstevel@tonic-gate p_scns, num_scns, filename); 16970Sstevel@tonic-gate } 16980Sstevel@tonic-gate if (L_flag) { 16990Sstevel@tonic-gate dump_dynamic(elf_file, p_scns, num_scns, filename); 17000Sstevel@tonic-gate } 17010Sstevel@tonic-gate if (s_flag) { 17020Sstevel@tonic-gate dump_section(elf_file, elf_head_p, p_scns, 17030Sstevel@tonic-gate num_scns, filename); 17040Sstevel@tonic-gate } 17050Sstevel@tonic-gate } 17060Sstevel@tonic-gate 17070Sstevel@tonic-gate /* 17080Sstevel@tonic-gate * Load the archive string table(s) (for extended-length strings) 17090Sstevel@tonic-gate * into an in-core table/list 17100Sstevel@tonic-gate */ 17110Sstevel@tonic-gate static struct stab_list_s * 17120Sstevel@tonic-gate load_arstring_table(struct stab_list_s *STabList, 17130Sstevel@tonic-gate int fd, Elf *elf_file, Elf_Arhdr *p_ar, char *filename) 17140Sstevel@tonic-gate { 17150Sstevel@tonic-gate off_t here; 17160Sstevel@tonic-gate struct stab_list_s *STL_entry, *STL_next; 17170Sstevel@tonic-gate 17180Sstevel@tonic-gate if (p_ar) { 17190Sstevel@tonic-gate STL_entry = malloc(sizeof (struct stab_list_s)); 17200Sstevel@tonic-gate STL_entry->next = 0; 17210Sstevel@tonic-gate STL_entry->strings = 0; 17220Sstevel@tonic-gate STL_entry->size = 0; 17230Sstevel@tonic-gate 17240Sstevel@tonic-gate if (!STabList) 17250Sstevel@tonic-gate STabList = STL_entry; 17260Sstevel@tonic-gate else { 17270Sstevel@tonic-gate STL_next = STabList; 17280Sstevel@tonic-gate while (STL_next->next != (void *)0) 17290Sstevel@tonic-gate STL_next = STL_next->next; 17300Sstevel@tonic-gate STL_next->next = STL_entry; 17310Sstevel@tonic-gate } 17320Sstevel@tonic-gate 17330Sstevel@tonic-gate STL_entry->size = p_ar->ar_size; 17340Sstevel@tonic-gate STL_entry->strings = malloc(p_ar->ar_size); 17350Sstevel@tonic-gate here = elf_getbase(elf_file); 17360Sstevel@tonic-gate if ((lseek(fd, here, 0)) != here) { 17370Sstevel@tonic-gate (void) fprintf(stderr, 17380Sstevel@tonic-gate "%s: %s: could not lseek\n", prog_name, filename); 17390Sstevel@tonic-gate } 17400Sstevel@tonic-gate 17410Sstevel@tonic-gate if ((read(fd, STL_entry->strings, p_ar->ar_size)) == -1) { 17420Sstevel@tonic-gate (void) fprintf(stderr, 17430Sstevel@tonic-gate "%s: %s: could not read\n", prog_name, filename); 17440Sstevel@tonic-gate } 17450Sstevel@tonic-gate } 17460Sstevel@tonic-gate return (STabList); 17470Sstevel@tonic-gate } 17480Sstevel@tonic-gate 17490Sstevel@tonic-gate /* 17500Sstevel@tonic-gate * Print the archive header for each member of an archive. 17510Sstevel@tonic-gate * Also call ar_sym_read to print the symbols in the 17520Sstevel@tonic-gate * archive symbol table if g_flag. Input is a file descriptor, 17530Sstevel@tonic-gate * an ELF file descriptor, and the filename. Putting the call 17540Sstevel@tonic-gate * to dump the archive symbol table in this function is more 17550Sstevel@tonic-gate * efficient since it is necessary to examine the archive member 17560Sstevel@tonic-gate * name in the archive header to determine which member is the 17570Sstevel@tonic-gate * symbol table. 17580Sstevel@tonic-gate */ 17590Sstevel@tonic-gate static void 17600Sstevel@tonic-gate dump_ar_hdr(int fd, Elf *elf_file, char *filename) 17610Sstevel@tonic-gate { 17620Sstevel@tonic-gate extern int v_flag, g_flag, a_flag, p_flag; 17630Sstevel@tonic-gate Elf_Arhdr *p_ar; 17640Sstevel@tonic-gate Elf *arf; 17650Sstevel@tonic-gate Elf_Cmd cmd; 17660Sstevel@tonic-gate int title = 0; 17670Sstevel@tonic-gate int err = 0; 17680Sstevel@tonic-gate 17690Sstevel@tonic-gate char buf[DATESIZE]; 17700Sstevel@tonic-gate 17710Sstevel@tonic-gate cmd = ELF_C_READ; 17720Sstevel@tonic-gate while ((arf = elf_begin(fd, cmd, elf_file)) != 0) { 17730Sstevel@tonic-gate p_ar = elf_getarhdr(arf); 17740Sstevel@tonic-gate if (p_ar == NULL) { 17750Sstevel@tonic-gate (void) fprintf(stderr, 17760Sstevel@tonic-gate "%s: %s: %s\n", prog_name, filename, elf_errmsg(-1)); 17770Sstevel@tonic-gate continue; 17780Sstevel@tonic-gate } 17790Sstevel@tonic-gate if (strcmp(p_ar->ar_name, "/") == 0) { 17800Sstevel@tonic-gate if (g_flag) 17810Sstevel@tonic-gate ar_sym_read(elf_file, filename); 17820Sstevel@tonic-gate } else if (strcmp(p_ar->ar_name, "//") == 0) { 17830Sstevel@tonic-gate StringTableList = load_arstring_table( 17840Sstevel@tonic-gate StringTableList, fd, arf, p_ar, 17850Sstevel@tonic-gate filename); 17860Sstevel@tonic-gate cmd = elf_next(arf); 17870Sstevel@tonic-gate (void) elf_end(arf); 17880Sstevel@tonic-gate continue; 17890Sstevel@tonic-gate } else { 17900Sstevel@tonic-gate if (a_flag) { 17910Sstevel@tonic-gate (void) printf("%s[%s]:\n", filename, 17920Sstevel@tonic-gate p_ar->ar_name); 17930Sstevel@tonic-gate if (!p_flag && title == 0) { 17940Sstevel@tonic-gate if (!v_flag) 17950Sstevel@tonic-gate (void) printf( 17960Sstevel@tonic-gate "\n\n\t\t\t***ARCHIVE HEADER***" 17970Sstevel@tonic-gate "\n Date Uid Gid Mode Size Member Name\n\n"); 17980Sstevel@tonic-gate else 17990Sstevel@tonic-gate (void) printf( 18000Sstevel@tonic-gate "\n\n\t\t\t***ARCHIVE HEADER***" 18010Sstevel@tonic-gate "\n Date Uid Gid Mode Size Member Name\n\n"); 18020Sstevel@tonic-gate title = 1; 18030Sstevel@tonic-gate } 18040Sstevel@tonic-gate if (!v_flag) { 18050Sstevel@tonic-gate (void) printf( 18060Sstevel@tonic-gate "\t0x%.8lx %6d %6d 0%.6ho 0x%.8lx %-s\n\n", 18070Sstevel@tonic-gate p_ar->ar_date, 18080Sstevel@tonic-gate (int)p_ar->ar_uid, 18090Sstevel@tonic-gate (int)p_ar->ar_gid, 18100Sstevel@tonic-gate (int)p_ar->ar_mode, 18110Sstevel@tonic-gate p_ar->ar_size, 18120Sstevel@tonic-gate p_ar->ar_name); 18130Sstevel@tonic-gate } else { 18140Sstevel@tonic-gate if ((strftime(buf, DATESIZE, 18150Sstevel@tonic-gate "%b %d %H:%M:%S %Y", 18160Sstevel@tonic-gate localtime( 18170Sstevel@tonic-gate &(p_ar->ar_date)))) == 0) { 18180Sstevel@tonic-gate (void) fprintf(stderr, 18190Sstevel@tonic-gate "%s: %s: don't have enough space to store the date\n", prog_name, filename); 18200Sstevel@tonic-gate exit(1); 18210Sstevel@tonic-gate } 18220Sstevel@tonic-gate (void) printf( 18230Sstevel@tonic-gate "\t%s %6d %6d 0%.6ho 0x%.8lx %-s\n\n", 18240Sstevel@tonic-gate buf, 18250Sstevel@tonic-gate (int)p_ar->ar_uid, 18260Sstevel@tonic-gate (int)p_ar->ar_gid, 18270Sstevel@tonic-gate (int)p_ar->ar_mode, 18280Sstevel@tonic-gate p_ar->ar_size, 18290Sstevel@tonic-gate p_ar->ar_name); 18300Sstevel@tonic-gate } 18310Sstevel@tonic-gate } 18320Sstevel@tonic-gate } 18330Sstevel@tonic-gate cmd = elf_next(arf); 18340Sstevel@tonic-gate (void) elf_end(arf); 18350Sstevel@tonic-gate } /* end while */ 18360Sstevel@tonic-gate 18370Sstevel@tonic-gate err = elf_errno(); 18380Sstevel@tonic-gate if (err != 0) { 18390Sstevel@tonic-gate (void) fprintf(stderr, 18400Sstevel@tonic-gate "%s: %s: %s\n", prog_name, filename, elf_errmsg(err)); 18410Sstevel@tonic-gate } 18420Sstevel@tonic-gate } 18430Sstevel@tonic-gate 18440Sstevel@tonic-gate /* 18450Sstevel@tonic-gate * Process member files of an archive. This function provides 18460Sstevel@tonic-gate * a loop through an archive equivalent the processing of 18470Sstevel@tonic-gate * each_file for individual object files. 18480Sstevel@tonic-gate */ 18490Sstevel@tonic-gate static void 18500Sstevel@tonic-gate dump_ar_files(int fd, Elf *elf_file, char *filename) 18510Sstevel@tonic-gate { 18520Sstevel@tonic-gate Elf_Arhdr *p_ar; 18530Sstevel@tonic-gate Elf *arf; 18540Sstevel@tonic-gate Elf_Cmd cmd; 18550Sstevel@tonic-gate Elf_Kind file_type; 18560Sstevel@tonic-gate GElf_Ehdr elf_head; 18570Sstevel@tonic-gate char *fullname; 18580Sstevel@tonic-gate 18590Sstevel@tonic-gate cmd = ELF_C_READ; 18600Sstevel@tonic-gate while ((arf = elf_begin(fd, cmd, elf_file)) != 0) { 18611618Srie size_t len; 18621618Srie 18630Sstevel@tonic-gate p_ar = elf_getarhdr(arf); 18640Sstevel@tonic-gate if (p_ar == NULL) { 18650Sstevel@tonic-gate (void) fprintf(stderr, 18660Sstevel@tonic-gate "%s: %s: %s\n", 18670Sstevel@tonic-gate prog_name, filename, elf_errmsg(-1)); 18680Sstevel@tonic-gate return; 18690Sstevel@tonic-gate } 18700Sstevel@tonic-gate if ((strcmp(p_ar->ar_name, "/") == 0) || 18710Sstevel@tonic-gate (strcmp(p_ar->ar_name, "//") == 0)) { 18720Sstevel@tonic-gate cmd = elf_next(arf); 18730Sstevel@tonic-gate (void) elf_end(arf); 18740Sstevel@tonic-gate continue; 18750Sstevel@tonic-gate } 18760Sstevel@tonic-gate 18771618Srie len = strlen(filename) + strlen(p_ar->ar_name) + 3; 18781618Srie if ((fullname = malloc(len)) == NULL) 18791618Srie return; 18801618Srie (void) snprintf(fullname, len, "%s[%s]", filename, 18811618Srie p_ar->ar_name); 18820Sstevel@tonic-gate (void) printf("\n%s:\n", fullname); 18830Sstevel@tonic-gate file_type = elf_kind(arf); 18840Sstevel@tonic-gate if (file_type == ELF_K_ELF) { 18850Sstevel@tonic-gate if (dump_elf_header(arf, fullname, &elf_head) == NULL) 18860Sstevel@tonic-gate return; 18870Sstevel@tonic-gate if (o_flag) 18880Sstevel@tonic-gate dump_exec_header(arf, 18890Sstevel@tonic-gate (unsigned)elf_head.e_phnum, fullname); 18900Sstevel@tonic-gate if (x_flag) 18910Sstevel@tonic-gate dump_section_table(arf, &elf_head, fullname); 18920Sstevel@tonic-gate } else { 18930Sstevel@tonic-gate (void) fprintf(stderr, 18940Sstevel@tonic-gate "%s: %s: invalid file type\n", 18950Sstevel@tonic-gate prog_name, fullname); 18960Sstevel@tonic-gate cmd = elf_next(arf); 18970Sstevel@tonic-gate (void) elf_end(arf); 18980Sstevel@tonic-gate continue; 18990Sstevel@tonic-gate } 19000Sstevel@tonic-gate 19010Sstevel@tonic-gate cmd = elf_next(arf); 19020Sstevel@tonic-gate (void) elf_end(arf); 19030Sstevel@tonic-gate } /* end while */ 19040Sstevel@tonic-gate } 19050Sstevel@tonic-gate 19060Sstevel@tonic-gate /* 19070Sstevel@tonic-gate * Takes a filename as input. Test first for a valid version 19080Sstevel@tonic-gate * of libelf.a and exit on error. Process each valid file 19090Sstevel@tonic-gate * or archive given as input on the command line. Check 19100Sstevel@tonic-gate * for file type. If it is an archive, process the archive- 19110Sstevel@tonic-gate * specific options first, then files within the archive. 19120Sstevel@tonic-gate * If it is an ELF object file, process it; otherwise 19130Sstevel@tonic-gate * warn that it is an invalid file type. 19140Sstevel@tonic-gate * All options except the archive-specific and program 19150Sstevel@tonic-gate * execution header are processed in the function, dump_section_table. 19160Sstevel@tonic-gate */ 19170Sstevel@tonic-gate static void 19180Sstevel@tonic-gate each_file(char *filename) 19190Sstevel@tonic-gate { 19200Sstevel@tonic-gate Elf *elf_file; 19210Sstevel@tonic-gate GElf_Ehdr elf_head; 19220Sstevel@tonic-gate int fd; 19230Sstevel@tonic-gate Elf_Kind file_type; 19240Sstevel@tonic-gate 19250Sstevel@tonic-gate struct stat buf; 19260Sstevel@tonic-gate 19270Sstevel@tonic-gate Elf_Cmd cmd; 19280Sstevel@tonic-gate errno = 0; 19290Sstevel@tonic-gate 19300Sstevel@tonic-gate if (stat(filename, &buf) == -1) { 19311618Srie int err = errno; 19321618Srie (void) fprintf(stderr, "%s: %s: %s", prog_name, filename, 19331618Srie strerror(err)); 19340Sstevel@tonic-gate return; 19350Sstevel@tonic-gate } 19360Sstevel@tonic-gate 19370Sstevel@tonic-gate if ((fd = open((filename), O_RDONLY)) == -1) { 19381618Srie (void) fprintf(stderr, "%s: %s: cannot read\n", prog_name, 19391618Srie filename); 19400Sstevel@tonic-gate return; 19410Sstevel@tonic-gate } 19420Sstevel@tonic-gate cmd = ELF_C_READ; 19430Sstevel@tonic-gate if ((elf_file = elf_begin(fd, cmd, (Elf *)0)) == NULL) { 19441618Srie (void) fprintf(stderr, "%s: %s: %s\n", prog_name, filename, 19451618Srie elf_errmsg(-1)); 19460Sstevel@tonic-gate return; 19470Sstevel@tonic-gate } 19480Sstevel@tonic-gate 19490Sstevel@tonic-gate file_type = elf_kind(elf_file); 19500Sstevel@tonic-gate if (file_type == ELF_K_AR) { 19510Sstevel@tonic-gate if (a_flag || g_flag) { 19520Sstevel@tonic-gate dump_ar_hdr(fd, elf_file, filename); 19530Sstevel@tonic-gate elf_file = elf_begin(fd, cmd, (Elf *)0); 19540Sstevel@tonic-gate } 19550Sstevel@tonic-gate if (z_flag) 19560Sstevel@tonic-gate dump_ar_files(fd, elf_file, filename); 19570Sstevel@tonic-gate } else { 19580Sstevel@tonic-gate if (file_type == ELF_K_ELF) { 19590Sstevel@tonic-gate (void) printf("\n%s:\n", filename); 19601618Srie if (dump_elf_header(elf_file, filename, &elf_head)) { 19611618Srie if (o_flag) 19621618Srie dump_exec_header(elf_file, 19631618Srie (unsigned)elf_head.e_phnum, 19641618Srie filename); 19651618Srie if (x_flag) 19661618Srie dump_section_table(elf_file, 19671618Srie &elf_head, filename); 19680Sstevel@tonic-gate } 19690Sstevel@tonic-gate } else { 19700Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: invalid file type\n", 19711618Srie prog_name, filename); 19720Sstevel@tonic-gate } 19730Sstevel@tonic-gate } 19740Sstevel@tonic-gate (void) elf_end(elf_file); 19750Sstevel@tonic-gate (void) close(fd); 19760Sstevel@tonic-gate } 19770Sstevel@tonic-gate 19780Sstevel@tonic-gate /* 19790Sstevel@tonic-gate * Sets up flags for command line options given and then 19800Sstevel@tonic-gate * calls each_file() to process each file. 19810Sstevel@tonic-gate */ 19820Sstevel@tonic-gate int 19830Sstevel@tonic-gate main(int argc, char *argv[], char *envp[]) 19840Sstevel@tonic-gate { 19850Sstevel@tonic-gate char *optstr = OPTSTR; /* option string used by getopt() */ 19860Sstevel@tonic-gate int optchar; 19870Sstevel@tonic-gate 19880Sstevel@tonic-gate /* 19890Sstevel@tonic-gate * Check for a binary that better fits this architecture. 19900Sstevel@tonic-gate */ 19912647Srie (void) conv_check_native(argv, envp); 19920Sstevel@tonic-gate 19930Sstevel@tonic-gate prog_name = argv[0]; 19940Sstevel@tonic-gate 19950Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 19960Sstevel@tonic-gate while ((optchar = getopt(argc, argv, optstr)) != -1) { 19970Sstevel@tonic-gate switch (optchar) { 19980Sstevel@tonic-gate case 'a': 19990Sstevel@tonic-gate a_flag = 1; 20000Sstevel@tonic-gate x_flag = 1; 20010Sstevel@tonic-gate break; 20020Sstevel@tonic-gate case 'g': 20030Sstevel@tonic-gate g_flag = 1; 20040Sstevel@tonic-gate x_flag = 1; 20050Sstevel@tonic-gate break; 20060Sstevel@tonic-gate case 'v': 20070Sstevel@tonic-gate v_flag = 1; 20080Sstevel@tonic-gate break; 20090Sstevel@tonic-gate case 'p': 20100Sstevel@tonic-gate p_flag = 1; 20110Sstevel@tonic-gate break; 20120Sstevel@tonic-gate case 'f': 20130Sstevel@tonic-gate f_flag = 1; 20140Sstevel@tonic-gate z_flag = 1; 20150Sstevel@tonic-gate break; 20160Sstevel@tonic-gate case 'o': 20170Sstevel@tonic-gate o_flag = 1; 20180Sstevel@tonic-gate z_flag = 1; 20190Sstevel@tonic-gate break; 20200Sstevel@tonic-gate case 'h': 20210Sstevel@tonic-gate h_flag = 1; 20220Sstevel@tonic-gate x_flag = 1; 20230Sstevel@tonic-gate z_flag = 1; 20240Sstevel@tonic-gate break; 20250Sstevel@tonic-gate case 's': 20260Sstevel@tonic-gate s_flag = 1; 20270Sstevel@tonic-gate x_flag = 1; 20280Sstevel@tonic-gate z_flag = 1; 20290Sstevel@tonic-gate break; 20300Sstevel@tonic-gate case 'd': 20310Sstevel@tonic-gate d_flag = 1; 20320Sstevel@tonic-gate x_flag = 1; 20330Sstevel@tonic-gate z_flag = 1; 20340Sstevel@tonic-gate set_range(optarg, &d_low, &d_hi); 20350Sstevel@tonic-gate break; 20360Sstevel@tonic-gate case 'n': 20370Sstevel@tonic-gate n_flag++; 20380Sstevel@tonic-gate x_flag = 1; 20390Sstevel@tonic-gate z_flag = 1; 20400Sstevel@tonic-gate name = optarg; 20410Sstevel@tonic-gate break; 20420Sstevel@tonic-gate case 'r': 20430Sstevel@tonic-gate r_flag = 1; 20440Sstevel@tonic-gate x_flag = 1; 20450Sstevel@tonic-gate z_flag = 1; 20460Sstevel@tonic-gate break; 20470Sstevel@tonic-gate case 't': 20480Sstevel@tonic-gate t_flag = 1; 20490Sstevel@tonic-gate x_flag = 1; 20500Sstevel@tonic-gate z_flag = 1; 20510Sstevel@tonic-gate break; 20520Sstevel@tonic-gate case 'C': 20530Sstevel@tonic-gate C_flag = 1; 20540Sstevel@tonic-gate t_flag = 1; 20550Sstevel@tonic-gate x_flag = 1; 20560Sstevel@tonic-gate z_flag = 1; 20570Sstevel@tonic-gate break; 20580Sstevel@tonic-gate case 'T': 20590Sstevel@tonic-gate T_flag = 1; 20600Sstevel@tonic-gate x_flag = 1; 20610Sstevel@tonic-gate z_flag = 1; 20620Sstevel@tonic-gate set_range(optarg, &T_low, &T_hi); 20630Sstevel@tonic-gate break; 20640Sstevel@tonic-gate case 'c': 20650Sstevel@tonic-gate c_flag = 1; 20660Sstevel@tonic-gate x_flag = 1; 20670Sstevel@tonic-gate z_flag = 1; 20680Sstevel@tonic-gate break; 20690Sstevel@tonic-gate case 'L': 20700Sstevel@tonic-gate L_flag = 1; 20710Sstevel@tonic-gate x_flag = 1; 20720Sstevel@tonic-gate z_flag = 1; 20730Sstevel@tonic-gate break; 20740Sstevel@tonic-gate case 'V': 20750Sstevel@tonic-gate V_flag = 1; 20760Sstevel@tonic-gate (void) fprintf(stderr, "dump: %s %s\n", 20770Sstevel@tonic-gate (const char *)SGU_PKG, 20780Sstevel@tonic-gate (const char *)SGU_REL); 20790Sstevel@tonic-gate break; 20800Sstevel@tonic-gate case '?': 20810Sstevel@tonic-gate errflag += 1; 20820Sstevel@tonic-gate break; 20830Sstevel@tonic-gate default: 20840Sstevel@tonic-gate break; 20850Sstevel@tonic-gate } 20860Sstevel@tonic-gate } 20870Sstevel@tonic-gate 20880Sstevel@tonic-gate if (errflag || (optind >= argc) || (!z_flag && !x_flag)) { 20890Sstevel@tonic-gate if (!(V_flag && (argc == 2))) { 20900Sstevel@tonic-gate usage(); 20910Sstevel@tonic-gate exit(269); 20920Sstevel@tonic-gate } 20930Sstevel@tonic-gate } 20940Sstevel@tonic-gate 20951618Srie if (elf_version(EV_CURRENT) == EV_NONE) { 20961618Srie (void) fprintf(stderr, "%s: libelf is out of date\n", 20971618Srie prog_name); 20981618Srie exit(101); 20991618Srie } 21001618Srie 21010Sstevel@tonic-gate while (optind < argc) { 21020Sstevel@tonic-gate each_file(argv[optind]); 21030Sstevel@tonic-gate optind++; 21040Sstevel@tonic-gate } 21050Sstevel@tonic-gate return (0); 21060Sstevel@tonic-gate } 2107