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 * 269273SAli.Bahrami@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 270Sstevel@tonic-gate * Use is subject to license terms. 280Sstevel@tonic-gate */ 290Sstevel@tonic-gate 301976Sab196087 /* Get definitions for the relocation types supported. */ 311976Sab196087 #define ELF_TARGET_ALL 321976Sab196087 330Sstevel@tonic-gate #include <stdio.h> 340Sstevel@tonic-gate #include <stdlib.h> 350Sstevel@tonic-gate #include <locale.h> 360Sstevel@tonic-gate #include <unistd.h> 370Sstevel@tonic-gate #include <libelf.h> 386206Sab196087 #include <sys/link.h> 391976Sab196087 #include <sys/elf.h> 400Sstevel@tonic-gate #include <sys/machelf.h> 410Sstevel@tonic-gate #include <fcntl.h> 420Sstevel@tonic-gate #include <sys/stat.h> 430Sstevel@tonic-gate #include <errno.h> 440Sstevel@tonic-gate #include <string.h> 450Sstevel@tonic-gate #include "sgs.h" 460Sstevel@tonic-gate #include "conv.h" 470Sstevel@tonic-gate #include "dump.h" 480Sstevel@tonic-gate 490Sstevel@tonic-gate 500Sstevel@tonic-gate #define OPTSTR "agcd:fhn:oprstvCLT:V?" /* option string for getopt() */ 510Sstevel@tonic-gate 520Sstevel@tonic-gate const char *UNKNOWN = "<unknown>"; 530Sstevel@tonic-gate 540Sstevel@tonic-gate static SCNTAB *p_symtab, *p_head_scns, *p_dynsym; 550Sstevel@tonic-gate 560Sstevel@tonic-gate static int 570Sstevel@tonic-gate x_flag = 0, /* option requires section header table */ 580Sstevel@tonic-gate z_flag = 0, /* process files within an archive */ 590Sstevel@tonic-gate rn_flag = 0; /* dump named relocation information */ 600Sstevel@tonic-gate 610Sstevel@tonic-gate static int 620Sstevel@tonic-gate /* flags: ?_flag corresponds to ? option */ 630Sstevel@tonic-gate a_flag = 0, /* dump archive header of each member of archive */ 640Sstevel@tonic-gate g_flag = 0, /* dump archive symbol table */ 650Sstevel@tonic-gate c_flag = 0, /* dump the string table */ 660Sstevel@tonic-gate d_flag = 0, /* dump range of sections */ 670Sstevel@tonic-gate f_flag = 0, /* dump each file header */ 680Sstevel@tonic-gate h_flag = 0, /* dump section headers */ 690Sstevel@tonic-gate n_flag = 0, /* dump named section */ 700Sstevel@tonic-gate o_flag = 0, /* dump each program execution header */ 710Sstevel@tonic-gate r_flag = 0, /* dump relocation information */ 720Sstevel@tonic-gate s_flag = 0, /* dump section contents */ 730Sstevel@tonic-gate t_flag = 0, /* dump symbol table entries */ 740Sstevel@tonic-gate C_flag = 0, /* dump decoded C++ symbol names */ 750Sstevel@tonic-gate L_flag = 0, /* dump dynamic linking information */ 760Sstevel@tonic-gate T_flag = 0, /* dump symbol table range */ 770Sstevel@tonic-gate V_flag = 0; /* dump version information */ 780Sstevel@tonic-gate 790Sstevel@tonic-gate int p_flag = 0, /* suppress printing of headings */ 800Sstevel@tonic-gate v_flag = 0; /* print information in verbose form */ 810Sstevel@tonic-gate 820Sstevel@tonic-gate static int 830Sstevel@tonic-gate d_low = 0, /* range for use with -d */ 840Sstevel@tonic-gate d_hi = 0, 850Sstevel@tonic-gate d_num = 0; 860Sstevel@tonic-gate 870Sstevel@tonic-gate static int 880Sstevel@tonic-gate T_low = 0, /* range for use with -T */ 890Sstevel@tonic-gate T_hi = 0, 900Sstevel@tonic-gate T_num = 0; 910Sstevel@tonic-gate 920Sstevel@tonic-gate static char *name = NULL; /* for use with -n option */ 930Sstevel@tonic-gate char *prog_name; 940Sstevel@tonic-gate static int errflag = 0; 950Sstevel@tonic-gate 960Sstevel@tonic-gate static struct stab_list_s { 970Sstevel@tonic-gate struct stab_list_s *next; 980Sstevel@tonic-gate char *strings; 990Sstevel@tonic-gate size_t size; 1000Sstevel@tonic-gate } *StringTableList = (void *)0; 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate extern void ar_sym_read(); 1030Sstevel@tonic-gate extern void dump_exec_header(); 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate /* 1070Sstevel@tonic-gate * Get the section descriptor and set the size of the 1080Sstevel@tonic-gate * data returned. Data is byte-order converted. 1090Sstevel@tonic-gate */ 1100Sstevel@tonic-gate void * 1110Sstevel@tonic-gate get_scndata(Elf_Scn *fd_scn, size_t *size) 1120Sstevel@tonic-gate { 1130Sstevel@tonic-gate Elf_Data *p_data; 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate p_data = 0; 1160Sstevel@tonic-gate if ((p_data = elf_getdata(fd_scn, p_data)) == 0 || 1170Sstevel@tonic-gate p_data->d_size == 0) { 1180Sstevel@tonic-gate return (NULL); 1190Sstevel@tonic-gate } 1200Sstevel@tonic-gate *size = p_data->d_size; 1210Sstevel@tonic-gate return (p_data->d_buf); 1220Sstevel@tonic-gate } 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate /* 1250Sstevel@tonic-gate * Get the section descriptor and set the size of the 1260Sstevel@tonic-gate * data returned. Data is raw (i.e., not byte-order converted). 1270Sstevel@tonic-gate */ 1280Sstevel@tonic-gate static void * 1290Sstevel@tonic-gate get_rawscn(Elf_Scn *fd_scn, size_t *size) 1300Sstevel@tonic-gate { 1310Sstevel@tonic-gate Elf_Data *p_data; 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate p_data = 0; 1340Sstevel@tonic-gate if ((p_data = elf_rawdata(fd_scn, p_data)) == 0 || 1350Sstevel@tonic-gate p_data->d_size == 0) { 1360Sstevel@tonic-gate return (NULL); 1370Sstevel@tonic-gate } 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate *size = p_data->d_size; 1400Sstevel@tonic-gate return (p_data->d_buf); 1410Sstevel@tonic-gate } 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate /* 1440Sstevel@tonic-gate * Print out a usage message in short form when program is invoked 1450Sstevel@tonic-gate * with insufficient or no arguments, and in long form when given 1460Sstevel@tonic-gate * either a ? or an invalid option. 1470Sstevel@tonic-gate */ 1480Sstevel@tonic-gate static void 1490Sstevel@tonic-gate usage() 1500Sstevel@tonic-gate { 1510Sstevel@tonic-gate (void) fprintf(stderr, 1520Sstevel@tonic-gate "Usage: %s [-%s] file(s) ...\n", prog_name, OPTSTR); 1530Sstevel@tonic-gate if (errflag) { 1540Sstevel@tonic-gate (void) fprintf(stderr, 1550Sstevel@tonic-gate "\t\t[-a dump archive header of each member of archive]\n\ 1560Sstevel@tonic-gate [-g dump archive global symbol table]\n\ 1570Sstevel@tonic-gate [-c dump the string table]\n\ 1580Sstevel@tonic-gate [-d dump range of sections]\n\ 1590Sstevel@tonic-gate [-f dump each file header]\n\ 1600Sstevel@tonic-gate [-h dump section headers]\n\ 1610Sstevel@tonic-gate [-n dump named section]\n\ 1620Sstevel@tonic-gate [-o dump each program execution header]\n\ 1630Sstevel@tonic-gate [-p suppress printing of headings]\n\ 1640Sstevel@tonic-gate [-r dump relocation information]\n\ 1650Sstevel@tonic-gate [-s dump section contents]\n\ 1660Sstevel@tonic-gate [-t dump symbol table entries]\n\ 1670Sstevel@tonic-gate [-v print information in verbose form]\n\ 1680Sstevel@tonic-gate [-C dump decoded C++ symbol names]\n\ 1690Sstevel@tonic-gate [-L dump the .dynamic structure]\n\ 1700Sstevel@tonic-gate [-T dump symbol table range]\n\ 1710Sstevel@tonic-gate [-V dump version information]\n"); 1720Sstevel@tonic-gate } 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate /* 1760Sstevel@tonic-gate * Set a range. Input is a character string, a lower 1770Sstevel@tonic-gate * bound and an upper bound. This function converts 1780Sstevel@tonic-gate * a character string into its correct integer values, 1790Sstevel@tonic-gate * setting the first value as the lower bound, and 1800Sstevel@tonic-gate * the second value as the upper bound. If more values 1810Sstevel@tonic-gate * are given they are ignored with a warning. 1820Sstevel@tonic-gate */ 1830Sstevel@tonic-gate static void 1840Sstevel@tonic-gate set_range(char *s, int *low, int *high) 1850Sstevel@tonic-gate { 1860Sstevel@tonic-gate char *w; 1870Sstevel@tonic-gate char *lasts; 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate while ((w = strtok_r(s, ",", &lasts)) != NULL) { 1900Sstevel@tonic-gate if (!(*low)) 1910Sstevel@tonic-gate /* LINTED */ 1920Sstevel@tonic-gate *low = (int)atol(w); 1930Sstevel@tonic-gate else 1940Sstevel@tonic-gate if (!(*high)) 1950Sstevel@tonic-gate /* LINTED */ 1960Sstevel@tonic-gate *high = (int)atol(w); 1970Sstevel@tonic-gate else { 1980Sstevel@tonic-gate (void) fprintf(stderr, 1994734Sab196087 "%s: too many arguments - %s ignored\n", 2004734Sab196087 prog_name, w); 2010Sstevel@tonic-gate return; 2020Sstevel@tonic-gate } 2030Sstevel@tonic-gate s = NULL; 2040Sstevel@tonic-gate } /* end while */ 2050Sstevel@tonic-gate } 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate /* 2090Sstevel@tonic-gate * Print static shared library information. 2100Sstevel@tonic-gate */ 2110Sstevel@tonic-gate static void 2120Sstevel@tonic-gate print_static(SCNTAB *l_scns, char *filename) 2130Sstevel@tonic-gate { 2140Sstevel@tonic-gate size_t section_size; 2150Sstevel@tonic-gate unsigned char *strtab; 2160Sstevel@tonic-gate unsigned char *path, buf[1024]; 2170Sstevel@tonic-gate unsigned long *temp; 2180Sstevel@tonic-gate unsigned long total, topath; 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate (void) printf("\n **** STATIC SHARED LIBRARY INFORMATION ****\n"); 2210Sstevel@tonic-gate (void) printf("\n%s:\n", filename); 2220Sstevel@tonic-gate (void) printf("\t"); 2230Sstevel@tonic-gate section_size = 0; 2240Sstevel@tonic-gate if ((strtab = (unsigned char *) 2250Sstevel@tonic-gate get_scndata(l_scns->p_sd, §ion_size)) == NULL) { 2260Sstevel@tonic-gate return; 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate while (section_size != 0) { 2300Sstevel@tonic-gate /* LINTED */ 2310Sstevel@tonic-gate temp = (unsigned long *)strtab; 2320Sstevel@tonic-gate total = temp[0]; 2330Sstevel@tonic-gate topath = temp[1]; 2340Sstevel@tonic-gate path = strtab + (topath*sizeof (long)); 2350Sstevel@tonic-gate (void) strncpy((char *)buf, (char *)path, 2364734Sab196087 (total - topath)*sizeof (long)); 2370Sstevel@tonic-gate (void) fprintf(stdout, "%s\n", buf); 2380Sstevel@tonic-gate strtab += total*sizeof (long); 2390Sstevel@tonic-gate section_size -= (total*sizeof (long)); 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate /* 2440Sstevel@tonic-gate * Print raw data in hexidecimal. Input is the section data to 2450Sstevel@tonic-gate * be printed out and the size of the data. Output is relative 2460Sstevel@tonic-gate * to a table lookup in dumpmap.h. 2470Sstevel@tonic-gate */ 2480Sstevel@tonic-gate static void 2490Sstevel@tonic-gate print_rawdata(unsigned char *p_sec, size_t size) 2500Sstevel@tonic-gate { 2510Sstevel@tonic-gate size_t j; 2520Sstevel@tonic-gate size_t count; 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate count = 1; 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate (void) printf("\t"); 2570Sstevel@tonic-gate for (j = size/sizeof (short); j != 0; --j, ++count) { 2580Sstevel@tonic-gate (void) printf("%.2x %.2x ", p_sec[0], p_sec[1]); 2590Sstevel@tonic-gate p_sec += 2; 2600Sstevel@tonic-gate if (count == 12) { 2610Sstevel@tonic-gate (void) printf("\n\t"); 2620Sstevel@tonic-gate count = 0; 2630Sstevel@tonic-gate } 2640Sstevel@tonic-gate } 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate /* 2670Sstevel@tonic-gate * take care of last byte if odd byte section 2680Sstevel@tonic-gate */ 2690Sstevel@tonic-gate if ((size & 0x1L) == 1L) 2700Sstevel@tonic-gate (void) printf("%.2x", *p_sec); 2710Sstevel@tonic-gate (void) printf("\n"); 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate /* 2770Sstevel@tonic-gate * Print relocation data of type SHT_RELA 2780Sstevel@tonic-gate * If d_flag, print data corresponding only to 2790Sstevel@tonic-gate * the section or range of sections specified. 2800Sstevel@tonic-gate * If n_flag, print data corresponding only to 2810Sstevel@tonic-gate * the named section. 2820Sstevel@tonic-gate */ 2830Sstevel@tonic-gate static void 2840Sstevel@tonic-gate print_rela(Elf *elf_file, SCNTAB *p_scns, Elf_Data *rdata, Elf_Data *sym_data, 2850Sstevel@tonic-gate GElf_Ehdr * p_ehdr, size_t reloc_size, size_t sym_size, char *filename, 2860Sstevel@tonic-gate SCNTAB *reloc_symtab) 2870Sstevel@tonic-gate { 2880Sstevel@tonic-gate GElf_Rela rela; 2890Sstevel@tonic-gate GElf_Sym sym; 2900Sstevel@tonic-gate size_t no_entries; 2910Sstevel@tonic-gate size_t rel_entsize; 2920Sstevel@tonic-gate size_t no_syms; 2930Sstevel@tonic-gate int type, symid; 2940Sstevel@tonic-gate static int n_title = 0; 2950Sstevel@tonic-gate int ndx = 0; 2960Sstevel@tonic-gate char *sym_name; 2970Sstevel@tonic-gate int adj = 0; 2980Sstevel@tonic-gate 2990Sstevel@tonic-gate if (gelf_getclass(elf_file) == ELFCLASS64) 3001618Srie adj = 8; 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate rel_entsize = p_scns->p_shdr.sh_entsize; 3030Sstevel@tonic-gate if ((rel_entsize == 0) || 3040Sstevel@tonic-gate (rel_entsize > p_scns->p_shdr.sh_size)) { 3050Sstevel@tonic-gate rel_entsize = gelf_fsize(elf_file, ELF_T_RELA, 1, 3060Sstevel@tonic-gate EV_CURRENT); 3070Sstevel@tonic-gate } 3080Sstevel@tonic-gate no_entries = reloc_size / rel_entsize; 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate no_syms = sym_size / gelf_fsize(elf_file, ELF_T_SYM, 1, EV_CURRENT); 3110Sstevel@tonic-gate while (no_entries--) { 3120Sstevel@tonic-gate (void) gelf_getrela(rdata, ndx, &rela); 3130Sstevel@tonic-gate /* LINTED */ 3140Sstevel@tonic-gate type = (int)GELF_R_TYPE(rela.r_info); 3150Sstevel@tonic-gate /* LINTED */ 3160Sstevel@tonic-gate symid = (int)GELF_R_SYM(rela.r_info); 3170Sstevel@tonic-gate /* LINTED */ 3180Sstevel@tonic-gate if ((symid > (no_syms - 1)) || (symid < 0)) { 3190Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: invalid symbol table " 3200Sstevel@tonic-gate "offset - %d - in %s\n", prog_name, filename, 3210Sstevel@tonic-gate symid, p_scns->scn_name); 3220Sstevel@tonic-gate ndx++; 3230Sstevel@tonic-gate continue; 3240Sstevel@tonic-gate } 3250Sstevel@tonic-gate (void) gelf_getsym(sym_data, symid, &sym); 3260Sstevel@tonic-gate sym_name = (char *)elf_strptr(elf_file, 3274734Sab196087 reloc_symtab->p_shdr.sh_link, sym.st_name); 3280Sstevel@tonic-gate if (sym_name == NULL) 3290Sstevel@tonic-gate sym_name = (char *)UNKNOWN; 3300Sstevel@tonic-gate if (r_flag && rn_flag) { 3310Sstevel@tonic-gate if (strcmp(name, p_scns->scn_name) != 0) { 3320Sstevel@tonic-gate ndx++; 3330Sstevel@tonic-gate continue; 3340Sstevel@tonic-gate } 3350Sstevel@tonic-gate if (!n_title) { 3360Sstevel@tonic-gate (void) printf("\n%s:\n", p_scns->scn_name); 3370Sstevel@tonic-gate (void) printf("%-*s%-*s%-*s%s\n\n", 3380Sstevel@tonic-gate 12 + adj, "Offset", 22, "Symndx", 3390Sstevel@tonic-gate 16, "Type", "Addend"); 3400Sstevel@tonic-gate n_title = 1; 3410Sstevel@tonic-gate } 3420Sstevel@tonic-gate } 3430Sstevel@tonic-gate if (d_flag) { 3440Sstevel@tonic-gate if (!d_hi) 3450Sstevel@tonic-gate d_hi = d_low; 3460Sstevel@tonic-gate if ((symid < d_low) || (symid > d_hi)) { 3470Sstevel@tonic-gate ndx++; 3480Sstevel@tonic-gate continue; 3490Sstevel@tonic-gate } 3500Sstevel@tonic-gate } 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate (void) printf("%-#*llx", 12 + adj, EC_XWORD(rela.r_offset)); 3530Sstevel@tonic-gate if (!v_flag) { 3540Sstevel@tonic-gate (void) printf("%-22d%-18d", symid, type); 3550Sstevel@tonic-gate } else { 3564734Sab196087 Conv_inv_buf_t inv_buf; 3574734Sab196087 3580Sstevel@tonic-gate if (strlen(sym_name)) { 3590Sstevel@tonic-gate size_t len = strlen(sym_name) + 1; 3600Sstevel@tonic-gate char tmpstr[10]; 3610Sstevel@tonic-gate if (len > 22) { 3620Sstevel@tonic-gate (void) sprintf(tmpstr, "%%-%ds", 3634734Sab196087 /* LINTED */ 3644734Sab196087 (int)len); 3652352Sab196087 /*LINTED: E_SEC_PRINTF_VAR_FMT*/ 3660Sstevel@tonic-gate (void) printf(tmpstr, sym_name); 3670Sstevel@tonic-gate } else 3680Sstevel@tonic-gate (void) printf("%-22s", sym_name); 3691976Sab196087 } else { 3700Sstevel@tonic-gate (void) printf("%-22d", symid); 3711976Sab196087 } 3721976Sab196087 (void) printf("%-20s", 3734734Sab196087 conv_reloc_type(p_ehdr->e_machine, 3744734Sab196087 type, DUMP_CONVFMT, &inv_buf)); 3750Sstevel@tonic-gate } 3760Sstevel@tonic-gate (void) printf("%lld\n", EC_SXWORD(rela.r_addend)); 3770Sstevel@tonic-gate ndx++; 3780Sstevel@tonic-gate } 3790Sstevel@tonic-gate } 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate /* 3820Sstevel@tonic-gate * Print relocation data of type SHT_REL. 3830Sstevel@tonic-gate * If d_flag, print data corresponding only to 3840Sstevel@tonic-gate * the section or range of sections specified. 3850Sstevel@tonic-gate * If n_flag, print data corresponding only to 3860Sstevel@tonic-gate * the named section. 3870Sstevel@tonic-gate */ 3880Sstevel@tonic-gate static void 3890Sstevel@tonic-gate print_rel(Elf *elf_file, SCNTAB *p_scns, Elf_Data *rdata, Elf_Data *sym_data, 3900Sstevel@tonic-gate GElf_Ehdr *p_ehdr, size_t reloc_size, size_t sym_size, char *filename, 3910Sstevel@tonic-gate SCNTAB *reloc_symtab) 3920Sstevel@tonic-gate { 3930Sstevel@tonic-gate GElf_Rel rel; 3940Sstevel@tonic-gate GElf_Sym sym; 3950Sstevel@tonic-gate size_t no_entries; 3960Sstevel@tonic-gate size_t rel_entsize; 3970Sstevel@tonic-gate int type, symid; 3980Sstevel@tonic-gate size_t no_syms; 3990Sstevel@tonic-gate static int n_title = 0; 4000Sstevel@tonic-gate int ndx = 0; 4010Sstevel@tonic-gate char *sym_name; 4020Sstevel@tonic-gate int adj = 0; 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate if (gelf_getclass(elf_file) == ELFCLASS64) 4051618Srie adj = 8; 4060Sstevel@tonic-gate 4070Sstevel@tonic-gate rel_entsize = p_scns->p_shdr.sh_entsize; 4080Sstevel@tonic-gate if ((rel_entsize == 0) || 4090Sstevel@tonic-gate (rel_entsize > p_scns->p_shdr.sh_size)) { 4100Sstevel@tonic-gate rel_entsize = gelf_fsize(elf_file, ELF_T_REL, 1, 4110Sstevel@tonic-gate EV_CURRENT); 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate no_entries = reloc_size / rel_entsize; 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate no_syms = sym_size / gelf_fsize(elf_file, ELF_T_SYM, 1, EV_CURRENT); 4160Sstevel@tonic-gate while (no_entries--) { 4170Sstevel@tonic-gate (void) gelf_getrel(rdata, ndx, &rel); 4180Sstevel@tonic-gate /* LINTED */ 4190Sstevel@tonic-gate type = (int)GELF_R_TYPE(rel.r_info); 4200Sstevel@tonic-gate /* LINTED */ 4210Sstevel@tonic-gate symid = (int)GELF_R_SYM(rel.r_info); 4220Sstevel@tonic-gate /* LINTED */ 4230Sstevel@tonic-gate if ((symid > (no_syms - 1)) || (symid < 0)) { 4240Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: invalid symbol table " 4250Sstevel@tonic-gate "offset - %d - in %s\n", prog_name, filename, 4260Sstevel@tonic-gate symid, p_scns->scn_name); 4270Sstevel@tonic-gate ndx++; 4280Sstevel@tonic-gate continue; 4290Sstevel@tonic-gate } 4300Sstevel@tonic-gate (void) gelf_getsym(sym_data, symid, &sym); 4310Sstevel@tonic-gate sym_name = (char *)elf_strptr(elf_file, 4324734Sab196087 reloc_symtab->p_shdr.sh_link, sym.st_name); 4330Sstevel@tonic-gate if (sym_name == NULL) 4340Sstevel@tonic-gate sym_name = (char *)UNKNOWN; 4350Sstevel@tonic-gate if (r_flag && rn_flag) { 4360Sstevel@tonic-gate if (strcmp(name, p_scns->scn_name) != 0) { 4370Sstevel@tonic-gate ndx++; 4380Sstevel@tonic-gate continue; 4390Sstevel@tonic-gate } 4400Sstevel@tonic-gate if (!n_title) { 4410Sstevel@tonic-gate (void) printf("\n%s:\n", p_scns->scn_name); 4420Sstevel@tonic-gate (void) printf("%-*s%-*s%s\n\n", 4430Sstevel@tonic-gate 12 + adj, "Offset", 20, "Symndx", "Type"); 4440Sstevel@tonic-gate n_title = 1; 4450Sstevel@tonic-gate } 4460Sstevel@tonic-gate } 4470Sstevel@tonic-gate if (d_flag) { 4480Sstevel@tonic-gate if (!d_hi) 4490Sstevel@tonic-gate d_hi = d_low; 4500Sstevel@tonic-gate if ((symid < d_low) || (symid > d_hi)) { 4510Sstevel@tonic-gate ndx++; 4520Sstevel@tonic-gate continue; 4530Sstevel@tonic-gate } 4540Sstevel@tonic-gate } 4550Sstevel@tonic-gate 4560Sstevel@tonic-gate (void) printf("%-#*llx", 12 + adj, EC_ADDR(rel.r_offset)); 4570Sstevel@tonic-gate if (!v_flag) { 4580Sstevel@tonic-gate (void) printf("%-20d%-18d", symid, type); 4590Sstevel@tonic-gate } else { 4604734Sab196087 Conv_inv_buf_t inv_buf; 4614734Sab196087 4620Sstevel@tonic-gate if (strlen(sym_name)) 4630Sstevel@tonic-gate (void) printf("%-20s", sym_name); 4641976Sab196087 else { 4650Sstevel@tonic-gate (void) printf("%-20d", sym.st_name); 4661976Sab196087 } 4671976Sab196087 (void) printf("%-20s", 4684734Sab196087 conv_reloc_type(p_ehdr->e_machine, 4694734Sab196087 type, DUMP_CONVFMT, &inv_buf)); 4700Sstevel@tonic-gate } 4710Sstevel@tonic-gate (void) printf("\n"); 4720Sstevel@tonic-gate ndx++; 4730Sstevel@tonic-gate } 4740Sstevel@tonic-gate } 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate /* demangle C++ names */ 4770Sstevel@tonic-gate static char * 4780Sstevel@tonic-gate demangled_name(char *s) 4790Sstevel@tonic-gate { 4801618Srie static char *buf = NULL; 4817008Sab196087 const char *dn; 4821618Srie size_t len; 4830Sstevel@tonic-gate 4847008Sab196087 dn = conv_demangle_name(s); 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate /* 4870Sstevel@tonic-gate * If not demangled, just return the symbol name 4880Sstevel@tonic-gate */ 4890Sstevel@tonic-gate if (strcmp(s, dn) == 0) 4900Sstevel@tonic-gate return (s); 4910Sstevel@tonic-gate 4920Sstevel@tonic-gate /* 4930Sstevel@tonic-gate * Demangled. Format it 4940Sstevel@tonic-gate */ 4950Sstevel@tonic-gate if (buf != NULL) 4960Sstevel@tonic-gate free(buf); 4970Sstevel@tonic-gate 4981618Srie len = strlen(dn) + strlen(s) + 4; 4991618Srie if ((buf = malloc(len)) == NULL) 5000Sstevel@tonic-gate return (s); 5010Sstevel@tonic-gate 5021618Srie (void) snprintf(buf, len, "%s\t[%s]", dn, s); 5030Sstevel@tonic-gate return (buf); 5040Sstevel@tonic-gate } 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate /* 5070Sstevel@tonic-gate * Print the symbol table. Input is an ELF file descriptor, a 5080Sstevel@tonic-gate * pointer to the symbol table SCNTAB structure, 5090Sstevel@tonic-gate * the number of symbols, a range of symbols to print, 5100Sstevel@tonic-gate * an index which is the number of the 5110Sstevel@tonic-gate * section in the file, and the filename. The number of sections, 5120Sstevel@tonic-gate * the range, and the index are set in 5130Sstevel@tonic-gate * dump_symbol_table, depending on whether -n or -T were set. 5140Sstevel@tonic-gate */ 5150Sstevel@tonic-gate static void 5160Sstevel@tonic-gate print_symtab(Elf *elf_file, SCNTAB *p_symtab, Elf_Data *sym_data, 5170Sstevel@tonic-gate long range, int index) 5180Sstevel@tonic-gate { 5190Sstevel@tonic-gate GElf_Sym sym; 5200Sstevel@tonic-gate int adj = 0; /* field adjustment for elf64 */ 5210Sstevel@tonic-gate Elf32_Word *symshndx = 0; 5220Sstevel@tonic-gate unsigned int nosymshndx = 0; 5234734Sab196087 Conv_inv_buf_t inv_buf; 5244734Sab196087 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate if (gelf_getclass(elf_file) == ELFCLASS64) 5271618Srie adj = 8; 5280Sstevel@tonic-gate 5290Sstevel@tonic-gate while (range > 0) { 5300Sstevel@tonic-gate char *sym_name = (char *)0; 5310Sstevel@tonic-gate int type, bind; 5320Sstevel@tonic-gate int specsec; 5330Sstevel@tonic-gate unsigned int shndx; 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate (void) gelf_getsym(sym_data, index, &sym); 5360Sstevel@tonic-gate type = (int)GELF_ST_TYPE(sym.st_info); 5370Sstevel@tonic-gate bind = (int)GELF_ST_BIND(sym.st_info); 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate if ((sym.st_shndx == SHN_XINDEX) && 5400Sstevel@tonic-gate (symshndx == 0) && (nosymshndx == 0)) { 5410Sstevel@tonic-gate Elf_Scn *_scn; 5420Sstevel@tonic-gate GElf_Shdr _shdr; 5430Sstevel@tonic-gate size_t symscnndx; 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate symscnndx = elf_ndxscn(p_symtab->p_sd); 5460Sstevel@tonic-gate _scn = 0; 5470Sstevel@tonic-gate while ((_scn = elf_nextscn(elf_file, _scn)) != 0) { 5480Sstevel@tonic-gate if (gelf_getshdr(_scn, &_shdr) == 0) 5490Sstevel@tonic-gate break; 5500Sstevel@tonic-gate if ((_shdr.sh_type == SHT_SYMTAB_SHNDX) && 5510Sstevel@tonic-gate /* LINTED */ 5520Sstevel@tonic-gate (_shdr.sh_link == (GElf_Word)symscnndx)) { 5530Sstevel@tonic-gate Elf_Data *_data; 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate if ((_data = elf_getdata(_scn, 0)) == 0) 5560Sstevel@tonic-gate continue; 5570Sstevel@tonic-gate 5580Sstevel@tonic-gate symshndx = (Elf32_Word *)_data->d_buf; 5590Sstevel@tonic-gate nosymshndx = 0; 5600Sstevel@tonic-gate break; 5610Sstevel@tonic-gate } 5620Sstevel@tonic-gate } 5630Sstevel@tonic-gate nosymshndx = 1; 5640Sstevel@tonic-gate } 5650Sstevel@tonic-gate 5660Sstevel@tonic-gate if ((symshndx) && (sym.st_shndx == SHN_XINDEX)) { 5670Sstevel@tonic-gate shndx = symshndx[index]; 5680Sstevel@tonic-gate specsec = 0; 5690Sstevel@tonic-gate } else { 5700Sstevel@tonic-gate shndx = sym.st_shndx; 5710Sstevel@tonic-gate if ((sym.st_shndx == SHN_UNDEF) || 5720Sstevel@tonic-gate (sym.st_shndx >= SHN_LORESERVE)) 5730Sstevel@tonic-gate specsec = 1; 5740Sstevel@tonic-gate else 5750Sstevel@tonic-gate specsec = 0; 5760Sstevel@tonic-gate } 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate 5790Sstevel@tonic-gate (void) printf("[%d]\t ", index++); 5800Sstevel@tonic-gate 5810Sstevel@tonic-gate if (v_flag && (type == STT_SPARC_REGISTER)) { 5820Sstevel@tonic-gate /* 5830Sstevel@tonic-gate * The strings "REG_G1" through "REG_G7" are intended 5840Sstevel@tonic-gate * to be consistent with output from elfdump(1). 5850Sstevel@tonic-gate */ 5861976Sab196087 (void) printf("%-*s", 12 + adj, 5874734Sab196087 conv_sym_SPARC_value(sym.st_value, 5884734Sab196087 DUMP_CONVFMT, &inv_buf)); 5891976Sab196087 } else { 5900Sstevel@tonic-gate (void) printf("0x%-*llx", 10 + adj, 5910Sstevel@tonic-gate EC_ADDR(sym.st_value)); 5921976Sab196087 } 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate (void) printf("%-*lld", 9 + adj, EC_XWORD(sym.st_size)); 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate if (!v_flag) { 5970Sstevel@tonic-gate (void) printf("%d\t\t%d\t%d\t%#x\t", 5980Sstevel@tonic-gate type, bind, (int)sym.st_other, (int)shndx); 5990Sstevel@tonic-gate } else { 6001976Sab196087 GElf_Ehdr p_ehdr; 6011976Sab196087 (void) gelf_getehdr(elf_file, &p_ehdr); 6021976Sab196087 (void) printf("%s\t", 6034734Sab196087 conv_sym_info_type(p_ehdr.e_machine, type, 6044734Sab196087 DUMP_CONVFMT, &inv_buf)); 6051976Sab196087 (void) printf("%s", 6064734Sab196087 conv_sym_info_bind(bind, DUMP_CONVFMT, &inv_buf)); 6070Sstevel@tonic-gate (void) printf("\t %d\t", EC_WORD(sym.st_other)); 6080Sstevel@tonic-gate 6094734Sab196087 if (specsec) 6104734Sab196087 (void) printf("%s", 6119273SAli.Bahrami@Sun.COM conv_sym_shndx(p_ehdr.e_ident[EI_OSABI], 6129273SAli.Bahrami@Sun.COM p_ehdr.e_machine, shndx, 6139273SAli.Bahrami@Sun.COM CONV_FMT_DECIMAL, &inv_buf)); 6144734Sab196087 else 6150Sstevel@tonic-gate (void) printf("%d", EC_WORD(shndx)); 6160Sstevel@tonic-gate (void) printf("\t"); 6170Sstevel@tonic-gate } 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate /* support machines where NULL-deref causes core dump */ 6200Sstevel@tonic-gate if (sym.st_name == 0) 6210Sstevel@tonic-gate sym_name = (char *)UNKNOWN; 6220Sstevel@tonic-gate else 6230Sstevel@tonic-gate if (C_flag) 6240Sstevel@tonic-gate sym_name = demangled_name( 6254734Sab196087 (char *)elf_strptr(elf_file, 6264734Sab196087 p_symtab->p_shdr.sh_link, 6274734Sab196087 sym.st_name)); 6280Sstevel@tonic-gate else 6290Sstevel@tonic-gate sym_name = (char *)elf_strptr(elf_file, 6304734Sab196087 p_symtab->p_shdr.sh_link, 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", 6664734Sab196087 EC_WORD(p->p_shdr.sh_type), 6674734Sab196087 EC_XWORD(p->p_shdr.sh_flags)); 6680Sstevel@tonic-gate } else { 6694734Sab196087 Conv_inv_buf_t inv_buf; 6704734Sab196087 6712352Sab196087 /*LINTED: E_SEC_PRINTF_VAR_FMT*/ 6729273SAli.Bahrami@Sun.COM (void) printf(conv_sec_type( 6739273SAli.Bahrami@Sun.COM p_ehdr.e_ident[EI_OSABI], p_ehdr.e_machine, 6744734Sab196087 p->p_shdr.sh_type, DUMP_CONVFMT, &inv_buf)); 6750Sstevel@tonic-gate (void) printf(" "); 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate if (p->p_shdr.sh_flags & SHF_WRITE) 6780Sstevel@tonic-gate (void) printf("W"); 6790Sstevel@tonic-gate else 6800Sstevel@tonic-gate (void) printf("-"); 6810Sstevel@tonic-gate if (p->p_shdr.sh_flags & SHF_ALLOC) 6820Sstevel@tonic-gate (void) printf("A"); 6830Sstevel@tonic-gate else 6840Sstevel@tonic-gate (void) printf("-"); 6850Sstevel@tonic-gate if (p->p_shdr.sh_flags & SHF_EXECINSTR) 6860Sstevel@tonic-gate (void) printf("I"); 6870Sstevel@tonic-gate else 6880Sstevel@tonic-gate (void) printf("-"); 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate if (p->p_shdr.sh_flags & SHF_ORDERED) 6910Sstevel@tonic-gate (void) printf("O"); 6920Sstevel@tonic-gate if (p->p_shdr.sh_flags & SHF_EXCLUDE) 6930Sstevel@tonic-gate (void) printf("E"); 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate (void) printf("\t"); 6960Sstevel@tonic-gate 6970Sstevel@tonic-gate } 6980Sstevel@tonic-gate (void) printf("%-#*llx%-#*llx%-#*llx%s%s\n", 6994734Sab196087 field, EC_ADDR(p->p_shdr.sh_addr), 7004734Sab196087 field, EC_OFF(p->p_shdr.sh_offset), 7014734Sab196087 field, EC_XWORD(p->p_shdr.sh_size), 7024734Sab196087 /* compatibility: tab for elf32 */ 7034734Sab196087 (field == 13) ? "\t" : " ", p->scn_name); 7040Sstevel@tonic-gate 7050Sstevel@tonic-gate (void) printf("\t%u\t%u\t%-#*llx%-#*llx\n\n", 7064734Sab196087 EC_WORD(p->p_shdr.sh_link), 7074734Sab196087 EC_WORD(p->p_shdr.sh_info), 7084734Sab196087 field, EC_XWORD(p->p_shdr.sh_addralign), 7094734Sab196087 field, EC_XWORD(p->p_shdr.sh_entsize)); 7100Sstevel@tonic-gate } 7110Sstevel@tonic-gate } 7120Sstevel@tonic-gate 7130Sstevel@tonic-gate /* 7140Sstevel@tonic-gate * Check that a range of numbers is valid. Input is 7150Sstevel@tonic-gate * a lower bound, an upper bound, a boundary condition, 7160Sstevel@tonic-gate * and the filename. Negative numbers and numbers greater 7170Sstevel@tonic-gate * than the bound are invalid. low must be smaller than hi. 7180Sstevel@tonic-gate * The returned integer is the number of items in the 7190Sstevel@tonic-gate * range if it is valid and -1 otherwise. 7200Sstevel@tonic-gate */ 7210Sstevel@tonic-gate static int 7220Sstevel@tonic-gate check_range(int low, int hi, size_t bound, char *filename) 7230Sstevel@tonic-gate { 7240Sstevel@tonic-gate if (((size_t)low > bound) || (low <= 0)) { 7250Sstevel@tonic-gate (void) fprintf(stderr, 7264734Sab196087 "%s: %s: number out of range, %d\n", 7274734Sab196087 prog_name, filename, low); 7280Sstevel@tonic-gate return (-1); 7290Sstevel@tonic-gate } 7300Sstevel@tonic-gate if (((size_t)hi > bound) || (hi < 0)) { 7310Sstevel@tonic-gate (void) fprintf(stderr, 7324734Sab196087 "%s: %s: number out of range, %d\n", 7334734Sab196087 prog_name, filename, hi); 7344734Sab196087 return (-1); 7350Sstevel@tonic-gate } 7360Sstevel@tonic-gate 7370Sstevel@tonic-gate if (hi && (low > hi)) { 7380Sstevel@tonic-gate (void) fprintf(stderr, 7394734Sab196087 "%s: %s: invalid range, %d,%d\n", 7404734Sab196087 prog_name, filename, low, hi); 7410Sstevel@tonic-gate return (-1); 7420Sstevel@tonic-gate } 7430Sstevel@tonic-gate if (hi) 7440Sstevel@tonic-gate return (hi - low + 1); 7450Sstevel@tonic-gate else 7460Sstevel@tonic-gate return (1); 7470Sstevel@tonic-gate } 7480Sstevel@tonic-gate 7490Sstevel@tonic-gate /* 7500Sstevel@tonic-gate * Print relocation information. Since this information is 7510Sstevel@tonic-gate * machine dependent, new sections must be added for each machine 7520Sstevel@tonic-gate * that is supported. Input is an ELF file descriptor, the ELF header, 7530Sstevel@tonic-gate * the SCNTAB structure, the number of sections, and a filename. 7540Sstevel@tonic-gate * Set up necessary information to print relocation information 7550Sstevel@tonic-gate * and call the appropriate print function depending on the 7560Sstevel@tonic-gate * type of relocation information. If the symbol table is 7570Sstevel@tonic-gate * absent, no relocation data is processed. Input is an 7580Sstevel@tonic-gate * ELF file descriptor, the ELF header, the SCNTAB structure, 7590Sstevel@tonic-gate * and the filename. Set range of d_flag and name if n_flag. 7600Sstevel@tonic-gate */ 7610Sstevel@tonic-gate static void 7620Sstevel@tonic-gate dump_reloc_table(Elf *elf_file, GElf_Ehdr *p_ehdr, 7630Sstevel@tonic-gate SCNTAB *p_scns, int num_scns, char *filename) 7640Sstevel@tonic-gate { 7650Sstevel@tonic-gate Elf_Data *rel_data; 7660Sstevel@tonic-gate Elf_Data *sym_data; 7670Sstevel@tonic-gate size_t sym_size; 7680Sstevel@tonic-gate size_t reloc_size; 7690Sstevel@tonic-gate SCNTAB *reloc_symtab; 7700Sstevel@tonic-gate SCNTAB *head_scns; 7710Sstevel@tonic-gate int r_title = 0; 7720Sstevel@tonic-gate int adj = 0; 7730Sstevel@tonic-gate size_t shnum; 7740Sstevel@tonic-gate 7750Sstevel@tonic-gate if (gelf_getclass(elf_file) == ELFCLASS64) 7761618Srie adj = 8; 7770Sstevel@tonic-gate 7780Sstevel@tonic-gate if ((!p_flag) && (!r_title)) { 7790Sstevel@tonic-gate (void) printf("\n **** RELOCATION INFORMATION ****\n"); 7800Sstevel@tonic-gate r_title = 1; 7810Sstevel@tonic-gate } 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate while (num_scns-- > 0) { 7840Sstevel@tonic-gate if ((p_scns->p_shdr.sh_type != SHT_RELA) && 7850Sstevel@tonic-gate (p_scns->p_shdr.sh_type != SHT_REL)) { 7860Sstevel@tonic-gate p_scns++; 7870Sstevel@tonic-gate continue; 7880Sstevel@tonic-gate } 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate head_scns = p_head_scns; 7910Sstevel@tonic-gate 792*9900SAli.Bahrami@Sun.COM if (elf_getshdrnum(elf_file, &shnum) == -1) { 7930Sstevel@tonic-gate (void) fprintf(stderr, 794*9900SAli.Bahrami@Sun.COM "%s: %s: elf_getshdrnum failed: %s\n", 7954734Sab196087 prog_name, filename, elf_errmsg(-1)); 7960Sstevel@tonic-gate return; 7970Sstevel@tonic-gate } 7980Sstevel@tonic-gate 7990Sstevel@tonic-gate if ((p_scns->p_shdr.sh_link == 0) || 8000Sstevel@tonic-gate /* LINTED */ 8010Sstevel@tonic-gate (p_scns->p_shdr.sh_link >= (GElf_Word)shnum)) { 8020Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: invalid sh_link field: " 8034734Sab196087 "section #: %d sh_link: %d\n", 8044734Sab196087 /* LINTED */ 8054734Sab196087 prog_name, filename, (int)elf_ndxscn(p_scns->p_sd), 8064734Sab196087 (int)p_scns->p_shdr.sh_link); 8070Sstevel@tonic-gate return; 8080Sstevel@tonic-gate } 8090Sstevel@tonic-gate head_scns += (p_scns->p_shdr.sh_link -1); 8100Sstevel@tonic-gate 8110Sstevel@tonic-gate if (head_scns->p_shdr.sh_type == SHT_SYMTAB) { 8120Sstevel@tonic-gate reloc_symtab = p_symtab; 8130Sstevel@tonic-gate } else if (head_scns->p_shdr.sh_type == SHT_DYNSYM) { 8140Sstevel@tonic-gate reloc_symtab = p_dynsym; 8150Sstevel@tonic-gate } else { 8160Sstevel@tonic-gate (void) fprintf(stderr, 8170Sstevel@tonic-gate "%s: %s: could not get symbol table\n", prog_name, filename); 8180Sstevel@tonic-gate return; 8190Sstevel@tonic-gate } 8200Sstevel@tonic-gate 8210Sstevel@tonic-gate sym_data = NULL; 8220Sstevel@tonic-gate sym_size = 0; 8230Sstevel@tonic-gate reloc_size = 0; 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate if ((sym_data = elf_getdata(reloc_symtab->p_sd, NULL)) == NULL) { 8260Sstevel@tonic-gate (void) fprintf(stderr, 8270Sstevel@tonic-gate "%s: %s: no symbol table data\n", prog_name, filename); 8280Sstevel@tonic-gate return; 8290Sstevel@tonic-gate } 8300Sstevel@tonic-gate sym_size = sym_data->d_size; 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate if (p_scns == NULL) { 8330Sstevel@tonic-gate (void) fprintf(stderr, 8340Sstevel@tonic-gate "%s: %s: no section table data\n", prog_name, filename); 8350Sstevel@tonic-gate return; 8360Sstevel@tonic-gate } 8370Sstevel@tonic-gate 8380Sstevel@tonic-gate if (p_scns->p_shdr.sh_type == SHT_RELA) { 8390Sstevel@tonic-gate if (!n_flag && r_flag) 8400Sstevel@tonic-gate (void) printf("\n%s:\n", p_scns->scn_name); 8410Sstevel@tonic-gate if (!p_flag && (!n_flag && r_flag)) 8420Sstevel@tonic-gate (void) printf("%-*s%-*s%-*s%s\n\n", 8430Sstevel@tonic-gate 12 + adj, "Offset", 22, "Symndx", 8440Sstevel@tonic-gate 18, "Type", "Addend"); 8450Sstevel@tonic-gate if ((rel_data = elf_getdata(p_scns->p_sd, NULL)) == NULL) { 8460Sstevel@tonic-gate (void) fprintf(stderr, 8470Sstevel@tonic-gate "%s: %s: no relocation information\n", prog_name, filename); 8480Sstevel@tonic-gate return; 8490Sstevel@tonic-gate } 8500Sstevel@tonic-gate reloc_size = rel_data->d_size; 8510Sstevel@tonic-gate 8520Sstevel@tonic-gate if (n_flag) { 8530Sstevel@tonic-gate rn_flag = 1; 8540Sstevel@tonic-gate print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr, 8554734Sab196087 reloc_size, sym_size, filename, reloc_symtab); 8560Sstevel@tonic-gate } 8570Sstevel@tonic-gate if (d_flag) { 8580Sstevel@tonic-gate rn_flag = 0; 8590Sstevel@tonic-gate print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr, 8604734Sab196087 reloc_size, sym_size, filename, reloc_symtab); 8610Sstevel@tonic-gate } 8620Sstevel@tonic-gate if (!n_flag && !d_flag) 8630Sstevel@tonic-gate print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr, 8644734Sab196087 reloc_size, sym_size, filename, reloc_symtab); 8650Sstevel@tonic-gate } else { 8660Sstevel@tonic-gate if (p_scns->p_shdr.sh_type == SHT_REL) { 8670Sstevel@tonic-gate if (!n_flag && r_flag) 8680Sstevel@tonic-gate (void) printf("\n%s:\n", p_scns->scn_name); 8690Sstevel@tonic-gate if (!p_flag && (!n_flag && r_flag)) { 8700Sstevel@tonic-gate (void) printf("%-*s%-*s%s\n\n", 8710Sstevel@tonic-gate 12 + adj, "Offset", 20, "Symndx", "Type"); 8720Sstevel@tonic-gate } 8730Sstevel@tonic-gate if ((rel_data = elf_getdata(p_scns->p_sd, NULL)) 8740Sstevel@tonic-gate == NULL) { 8750Sstevel@tonic-gate (void) fprintf(stderr, 8760Sstevel@tonic-gate "%s: %s: no relocation information\n", prog_name, filename); 8770Sstevel@tonic-gate return; 8780Sstevel@tonic-gate } 8790Sstevel@tonic-gate reloc_size = rel_data->d_size; 8800Sstevel@tonic-gate if (n_flag) { 8810Sstevel@tonic-gate rn_flag = 1; 8820Sstevel@tonic-gate print_rel(elf_file, p_scns, rel_data, sym_data, 8834734Sab196087 p_ehdr, reloc_size, sym_size, 8844734Sab196087 filename, reloc_symtab); 8850Sstevel@tonic-gate } 8860Sstevel@tonic-gate if (d_flag) { 8870Sstevel@tonic-gate rn_flag = 0; 8880Sstevel@tonic-gate print_rel(elf_file, p_scns, rel_data, sym_data, 8894734Sab196087 p_ehdr, reloc_size, sym_size, 8904734Sab196087 filename, reloc_symtab); 8910Sstevel@tonic-gate } 8920Sstevel@tonic-gate if (!n_flag && !d_flag) 8930Sstevel@tonic-gate print_rel(elf_file, p_scns, rel_data, sym_data, 8944734Sab196087 p_ehdr, reloc_size, sym_size, 8954734Sab196087 filename, reloc_symtab); 8960Sstevel@tonic-gate } 8970Sstevel@tonic-gate } 8980Sstevel@tonic-gate p_scns++; 8990Sstevel@tonic-gate } 9000Sstevel@tonic-gate } 9010Sstevel@tonic-gate 9020Sstevel@tonic-gate /* 9030Sstevel@tonic-gate * Print out the string tables. Input is an opened ELF file, 9040Sstevel@tonic-gate * the SCNTAB structure, the number of sections, and the filename. 9050Sstevel@tonic-gate * Since there can be more than one string table, all sections are 9060Sstevel@tonic-gate * examined and any with the correct type are printed out. 9070Sstevel@tonic-gate */ 9080Sstevel@tonic-gate static void 9090Sstevel@tonic-gate dump_string_table(SCNTAB *s, int num_scns) 9100Sstevel@tonic-gate { 9110Sstevel@tonic-gate size_t section_size; 9120Sstevel@tonic-gate unsigned char *strtab; 9130Sstevel@tonic-gate int beg_of_string; 9140Sstevel@tonic-gate int counter = 0; 9150Sstevel@tonic-gate int str_off; 9160Sstevel@tonic-gate int i; 9170Sstevel@tonic-gate 9180Sstevel@tonic-gate if (!p_flag) { 9190Sstevel@tonic-gate (void) printf("\n **** STRING TABLE INFORMATION ****\n"); 9200Sstevel@tonic-gate } 9210Sstevel@tonic-gate 9220Sstevel@tonic-gate for (i = 0; i < num_scns; i++, s++) { 9230Sstevel@tonic-gate if (s->p_shdr.sh_type != SHT_STRTAB) 9240Sstevel@tonic-gate continue; 9250Sstevel@tonic-gate 9260Sstevel@tonic-gate str_off = 0; 9270Sstevel@tonic-gate 9280Sstevel@tonic-gate if (!p_flag) { 9290Sstevel@tonic-gate (void) printf("\n%s:\n", s->scn_name); 9300Sstevel@tonic-gate (void) printf(" <offset> \tName\n"); 9310Sstevel@tonic-gate } 9320Sstevel@tonic-gate section_size = 0; 9330Sstevel@tonic-gate if ((strtab = (unsigned char *) 9340Sstevel@tonic-gate get_scndata(s->p_sd, §ion_size)) == NULL) { 9350Sstevel@tonic-gate continue; 9360Sstevel@tonic-gate } 9370Sstevel@tonic-gate 9380Sstevel@tonic-gate if (section_size != 0) { 9390Sstevel@tonic-gate (void) printf(" <%d> \t", str_off); 9400Sstevel@tonic-gate beg_of_string = 0; 9410Sstevel@tonic-gate while (section_size--) { 9420Sstevel@tonic-gate unsigned char c = *strtab++; 9430Sstevel@tonic-gate 9440Sstevel@tonic-gate if (beg_of_string) { 9450Sstevel@tonic-gate (void) printf(" <%d> \t", str_off); 9460Sstevel@tonic-gate counter++; 9470Sstevel@tonic-gate beg_of_string = 0; 9480Sstevel@tonic-gate } 9490Sstevel@tonic-gate str_off++; 9500Sstevel@tonic-gate switch (c) { 9510Sstevel@tonic-gate case '\0': 9520Sstevel@tonic-gate (void) printf("\n"); 9530Sstevel@tonic-gate beg_of_string = 1; 9540Sstevel@tonic-gate break; 9550Sstevel@tonic-gate default: 9560Sstevel@tonic-gate (void) putchar(c); 9570Sstevel@tonic-gate } 9580Sstevel@tonic-gate } 9590Sstevel@tonic-gate } 9600Sstevel@tonic-gate } 9610Sstevel@tonic-gate (void) printf("\n"); 9620Sstevel@tonic-gate } 9630Sstevel@tonic-gate 9640Sstevel@tonic-gate /* 9650Sstevel@tonic-gate * Print the symbol table. This function does not print the contents 9660Sstevel@tonic-gate * of the symbol table but sets up the parameters and then calls 9670Sstevel@tonic-gate * print_symtab to print the symbols. Calling another function to print 9680Sstevel@tonic-gate * the symbols allows both -T and -n to work correctly 9690Sstevel@tonic-gate * simultaneously. Input is an opened ELF file, a pointer to the 9700Sstevel@tonic-gate * symbol table SCNTAB structure, and the filename. 9710Sstevel@tonic-gate * Set the range of symbols to print if T_flag, and set 9720Sstevel@tonic-gate * name of symbol to print if n_flag. 9730Sstevel@tonic-gate */ 9740Sstevel@tonic-gate static void 9750Sstevel@tonic-gate dump_symbol_table(Elf *elf_file, SCNTAB *p_symtab, char *filename) 9760Sstevel@tonic-gate { 9770Sstevel@tonic-gate Elf_Data *sym_data; 9780Sstevel@tonic-gate GElf_Sym T_range, n_range; /* for use with -T and -n */ 9790Sstevel@tonic-gate size_t count = 0; 9800Sstevel@tonic-gate size_t sym_size; 9810Sstevel@tonic-gate int index = 1; 9820Sstevel@tonic-gate int found_it = 0; 9830Sstevel@tonic-gate int i; 9840Sstevel@tonic-gate int adj = 0; /* field adjustment for elf64 */ 9850Sstevel@tonic-gate 9860Sstevel@tonic-gate if (gelf_getclass(elf_file) == ELFCLASS64) 9871618Srie adj = 8; 9880Sstevel@tonic-gate 9890Sstevel@tonic-gate if (p_symtab == NULL) { 9900Sstevel@tonic-gate (void) fprintf(stderr, 9910Sstevel@tonic-gate "%s: %s: could not get symbol table\n", prog_name, filename); 9920Sstevel@tonic-gate return; 9930Sstevel@tonic-gate } 9940Sstevel@tonic-gate 9950Sstevel@tonic-gate /* get symbol table data */ 9960Sstevel@tonic-gate sym_data = NULL; 9970Sstevel@tonic-gate sym_size = 0; 9980Sstevel@tonic-gate if ((sym_data = 9990Sstevel@tonic-gate elf_getdata(p_symtab->p_sd, NULL)) == NULL) { 10000Sstevel@tonic-gate (void) printf("\n%s:\n", p_symtab->scn_name); 10010Sstevel@tonic-gate (void) printf("No symbol table data\n"); 10020Sstevel@tonic-gate return; 10030Sstevel@tonic-gate } 10040Sstevel@tonic-gate sym_size = sym_data->d_size; 10050Sstevel@tonic-gate 10060Sstevel@tonic-gate count = sym_size / p_symtab->p_shdr.sh_entsize; 10070Sstevel@tonic-gate 10080Sstevel@tonic-gate if (n_flag && t_flag && !T_flag) { 10090Sstevel@tonic-gate /* LINTED */ 10100Sstevel@tonic-gate for (i = 1; i < count; i++) { 10110Sstevel@tonic-gate (void) gelf_getsym(sym_data, i, &n_range); 10120Sstevel@tonic-gate if (strcmp(name, (char *) 10130Sstevel@tonic-gate elf_strptr(elf_file, 10140Sstevel@tonic-gate p_symtab->p_shdr.sh_link, 10150Sstevel@tonic-gate n_range.st_name)) != 0) { 10160Sstevel@tonic-gate continue; 10170Sstevel@tonic-gate } else { 10180Sstevel@tonic-gate found_it = 1; 10190Sstevel@tonic-gate if (!p_flag) { 10200Sstevel@tonic-gate (void) printf( 10210Sstevel@tonic-gate "\n ***** SYMBOL TABLE INFORMATION *****\n"); 10220Sstevel@tonic-gate (void) printf( 10230Sstevel@tonic-gate "[Index] %-*s%-*sType\tBind\tOther\tShndx\tName", 10244734Sab196087 12 + adj, "Value", 9 + adj, "Size"); 10250Sstevel@tonic-gate } 10260Sstevel@tonic-gate (void) printf("\n%s:\n", p_symtab->scn_name); 10270Sstevel@tonic-gate print_symtab(elf_file, p_symtab, sym_data, 10280Sstevel@tonic-gate 1, i); 10290Sstevel@tonic-gate } 10300Sstevel@tonic-gate } /* end for */ 10310Sstevel@tonic-gate if (!found_it) { 10320Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s not found\n", 10334734Sab196087 prog_name, filename, name); 10340Sstevel@tonic-gate } 10350Sstevel@tonic-gate } else if (T_flag) { 10360Sstevel@tonic-gate T_num = check_range(T_low, T_hi, count, filename); 10370Sstevel@tonic-gate if (T_num < 0) 10380Sstevel@tonic-gate return; 10390Sstevel@tonic-gate 10400Sstevel@tonic-gate (void) gelf_getsym(sym_data, T_low-1, &T_range); 10410Sstevel@tonic-gate index = T_low; 10420Sstevel@tonic-gate 10430Sstevel@tonic-gate if (!p_flag) { 10440Sstevel@tonic-gate (void) printf( 10450Sstevel@tonic-gate "\n ***** SYMBOL TABLE INFORMATION *****\n"); 10460Sstevel@tonic-gate (void) printf( 10470Sstevel@tonic-gate "[Index] %-*s%-*sType\tBind\tOther\tShndx\tName", 10480Sstevel@tonic-gate 12 + adj, "Value", 9 + adj, "Size"); 10490Sstevel@tonic-gate } 10500Sstevel@tonic-gate (void) printf("\n%s:\n", p_symtab->scn_name); 10510Sstevel@tonic-gate print_symtab(elf_file, p_symtab, sym_data, T_num, index); 10520Sstevel@tonic-gate } else { 10530Sstevel@tonic-gate if (!p_flag) { 10540Sstevel@tonic-gate (void) printf( 10550Sstevel@tonic-gate "\n ***** SYMBOL TABLE INFORMATION *****\n"); 10560Sstevel@tonic-gate (void) printf( 10570Sstevel@tonic-gate "[Index] %-*s%-*sType\tBind\tOther\tShndx\tName", 10580Sstevel@tonic-gate 12 + adj, "Value", 9 + adj, "Size"); 10590Sstevel@tonic-gate } 10600Sstevel@tonic-gate (void) printf("\n%s:\n", p_symtab->scn_name); 10610Sstevel@tonic-gate print_symtab(elf_file, p_symtab, sym_data, count-1, 1); 10620Sstevel@tonic-gate } 10630Sstevel@tonic-gate } 10640Sstevel@tonic-gate 10651698Sab196087 10661698Sab196087 /* 10670Sstevel@tonic-gate * Print dynamic linking information. Input is an ELF 10680Sstevel@tonic-gate * file descriptor, the SCNTAB structure, the number of 10690Sstevel@tonic-gate * sections, and the filename. 10700Sstevel@tonic-gate */ 10710Sstevel@tonic-gate static void 10720Sstevel@tonic-gate dump_dynamic(Elf *elf_file, SCNTAB *p_scns, int num_scns, char *filename) 10730Sstevel@tonic-gate { 10742352Sab196087 #define pdyn_Fmtptr "%#llx" 10751698Sab196087 10760Sstevel@tonic-gate Elf_Data *dyn_data; 10770Sstevel@tonic-gate GElf_Dyn p_dyn; 10780Sstevel@tonic-gate GElf_Phdr p_phdr; 10790Sstevel@tonic-gate GElf_Ehdr p_ehdr; 10800Sstevel@tonic-gate int index = 1; 10810Sstevel@tonic-gate int lib_scns = num_scns; 10820Sstevel@tonic-gate SCNTAB *l_scns = p_scns; 10830Sstevel@tonic-gate int header_num = 0; 10842352Sab196087 const char *str; 10852352Sab196087 10862352Sab196087 (void) gelf_getehdr(elf_file, &p_ehdr); 10870Sstevel@tonic-gate 10880Sstevel@tonic-gate if (!p_flag) 10890Sstevel@tonic-gate (void) printf("\n **** DYNAMIC SECTION INFORMATION ****\n"); 10900Sstevel@tonic-gate 10910Sstevel@tonic-gate for (; num_scns > 0; num_scns--, p_scns++) { 10920Sstevel@tonic-gate GElf_Word link; 10930Sstevel@tonic-gate int ii; 10940Sstevel@tonic-gate 10950Sstevel@tonic-gate 10960Sstevel@tonic-gate if (p_scns->p_shdr.sh_type != SHT_DYNAMIC) 10970Sstevel@tonic-gate continue; 10980Sstevel@tonic-gate 10990Sstevel@tonic-gate if (!p_flag) { 11000Sstevel@tonic-gate (void) printf("%s:\n", p_scns->scn_name); 11010Sstevel@tonic-gate (void) printf("[INDEX]\tTag Value\n"); 11020Sstevel@tonic-gate } 11030Sstevel@tonic-gate 11040Sstevel@tonic-gate if ((dyn_data = elf_getdata(p_scns->p_sd, NULL)) == 0) { 11050Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: no data in " 11060Sstevel@tonic-gate "%s section\n", prog_name, filename, 11070Sstevel@tonic-gate p_scns->scn_name); 11080Sstevel@tonic-gate return; 11090Sstevel@tonic-gate } 11100Sstevel@tonic-gate 11110Sstevel@tonic-gate link = p_scns->p_shdr.sh_link; 11120Sstevel@tonic-gate ii = 0; 11130Sstevel@tonic-gate 11140Sstevel@tonic-gate (void) gelf_getdyn(dyn_data, ii++, &p_dyn); 11150Sstevel@tonic-gate while (p_dyn.d_tag != DT_NULL) { 11164734Sab196087 union { 11174734Sab196087 Conv_inv_buf_t inv; 11184734Sab196087 Conv_dyn_flag_buf_t dyn_flag; 11194734Sab196087 Conv_dyn_flag1_buf_t dyn_flag1; 11204734Sab196087 Conv_dyn_feature1_buf_t dyn_feature1; 11214734Sab196087 Conv_dyn_posflag1_buf_t dyn_posflag1; 11224734Sab196087 } conv_buf; 11234734Sab196087 11241976Sab196087 (void) printf("[%d]\t%-15.15s ", index++, 11259273SAli.Bahrami@Sun.COM conv_dyn_tag(p_dyn.d_tag, 11269273SAli.Bahrami@Sun.COM p_ehdr.e_ident[EI_OSABI], p_ehdr.e_machine, 11274734Sab196087 DUMP_CONVFMT, &conv_buf.inv)); 11280Sstevel@tonic-gate 11291698Sab196087 /* 11301698Sab196087 * It would be nice to use a table driven loop 11311698Sab196087 * here, but the address space is too sparse 11321698Sab196087 * and irregular. A switch is simple and robust. 11331698Sab196087 */ 11340Sstevel@tonic-gate switch (p_dyn.d_tag) { 11350Sstevel@tonic-gate /* 11361976Sab196087 * Items with an address value 11370Sstevel@tonic-gate */ 11381976Sab196087 case DT_PLTGOT: 11391976Sab196087 case DT_HASH: 11401976Sab196087 case DT_STRTAB: 11411976Sab196087 case DT_RELA: 11421976Sab196087 case DT_SYMTAB: 11431976Sab196087 case DT_INIT: 11441976Sab196087 case DT_FINI: 11451976Sab196087 case DT_REL: 11461976Sab196087 case DT_DEBUG: 11471976Sab196087 case DT_TEXTREL: 11481976Sab196087 case DT_JMPREL: 11491976Sab196087 case DT_INIT_ARRAY: 11501976Sab196087 case DT_FINI_ARRAY: 11511976Sab196087 case DT_INIT_ARRAYSZ: 11521976Sab196087 case DT_FINI_ARRAYSZ: 11531976Sab196087 case DT_PREINIT_ARRAY: 11541976Sab196087 case DT_PREINIT_ARRAYSZ: 11551976Sab196087 case DT_SUNW_RTLDINF: 11561976Sab196087 case DT_SUNW_CAP: 11572766Sab196087 case DT_SUNW_SYMTAB: 11583492Sab196087 case DT_SUNW_SYMSORT: 11593492Sab196087 case DT_SUNW_TLSSORT: 11601976Sab196087 case DT_PLTPAD: 11611976Sab196087 case DT_MOVETAB: 11621976Sab196087 case DT_SYMINFO: 11631976Sab196087 case DT_RELACOUNT: 11641976Sab196087 case DT_RELCOUNT: 11651976Sab196087 case DT_VERSYM: 11661976Sab196087 case DT_VERDEF: 11671976Sab196087 case DT_VERDEFNUM: 11681976Sab196087 case DT_VERNEED: 11691976Sab196087 (void) printf(pdyn_Fmtptr, 11704734Sab196087 EC_ADDR(p_dyn.d_un.d_ptr)); 11710Sstevel@tonic-gate break; 11720Sstevel@tonic-gate 11730Sstevel@tonic-gate /* 11741976Sab196087 * Items with a string value 11750Sstevel@tonic-gate */ 11761976Sab196087 case DT_NEEDED: 11771976Sab196087 case DT_SONAME: 11781976Sab196087 case DT_RPATH: 11791976Sab196087 case DT_RUNPATH: 11801976Sab196087 case DT_SUNW_AUXILIARY: 11811976Sab196087 case DT_SUNW_FILTER: 11821976Sab196087 case DT_CONFIG: 11831976Sab196087 case DT_DEPAUDIT: 11841976Sab196087 case DT_AUDIT: 11851976Sab196087 case DT_AUXILIARY: 11861976Sab196087 case DT_USED: 11871976Sab196087 case DT_FILTER: 11881976Sab196087 if (v_flag) { /* Look up the string */ 11891976Sab196087 str = (char *)elf_strptr(elf_file, link, 11904734Sab196087 p_dyn.d_un.d_ptr); 11911976Sab196087 if (!(str && *str)) 11921976Sab196087 str = (char *)UNKNOWN; 11931976Sab196087 (void) printf("%s", str); 11941976Sab196087 } else { /* Show the address */ 11951976Sab196087 (void) printf(pdyn_Fmtptr, 11964734Sab196087 EC_ADDR(p_dyn.d_un.d_ptr)); 11971976Sab196087 } 11980Sstevel@tonic-gate break; 11990Sstevel@tonic-gate 12000Sstevel@tonic-gate /* 12011976Sab196087 * Items with a literal value 12020Sstevel@tonic-gate */ 12031976Sab196087 case DT_PLTRELSZ: 12041976Sab196087 case DT_RELASZ: 12051976Sab196087 case DT_RELAENT: 12061976Sab196087 case DT_STRSZ: 12071976Sab196087 case DT_SYMENT: 12081976Sab196087 case DT_RELSZ: 12091976Sab196087 case DT_RELENT: 12101976Sab196087 case DT_PLTREL: 12111976Sab196087 case DT_BIND_NOW: 12121976Sab196087 case DT_CHECKSUM: 12131976Sab196087 case DT_PLTPADSZ: 12141976Sab196087 case DT_MOVEENT: 12151976Sab196087 case DT_MOVESZ: 12161976Sab196087 case DT_SYMINSZ: 12171976Sab196087 case DT_SYMINENT: 12181976Sab196087 case DT_VERNEEDNUM: 12191976Sab196087 case DT_SPARC_REGISTER: 12202766Sab196087 case DT_SUNW_SYMSZ: 12213492Sab196087 case DT_SUNW_SORTENT: 12223492Sab196087 case DT_SUNW_SYMSORTSZ: 12233492Sab196087 case DT_SUNW_TLSSORTSZ: 12243850Sab196087 case DT_SUNW_STRPAD: 12251976Sab196087 (void) printf(pdyn_Fmtptr, 12264734Sab196087 EC_XWORD(p_dyn.d_un.d_val)); 12270Sstevel@tonic-gate break; 12280Sstevel@tonic-gate 12290Sstevel@tonic-gate /* 12306206Sab196087 * Integer items that are bitmasks, or which 12316206Sab196087 * can be otherwise formatted in symbolic form. 12320Sstevel@tonic-gate */ 12331976Sab196087 case DT_FLAGS: 12341976Sab196087 case DT_FEATURE_1: 12351976Sab196087 case DT_POSFLAG_1: 12361976Sab196087 case DT_FLAGS_1: 12376206Sab196087 case DT_SUNW_LDMACH: 12382352Sab196087 str = NULL; 12392352Sab196087 if (v_flag) { 12402352Sab196087 switch (p_dyn.d_tag) { 12412352Sab196087 case DT_FLAGS: 12422352Sab196087 str = conv_dyn_flag( 12434734Sab196087 p_dyn.d_un.d_val, 12444734Sab196087 DUMP_CONVFMT, 12454734Sab196087 &conv_buf.dyn_flag); 12464734Sab196087 break; 12472352Sab196087 case DT_FEATURE_1: 12482352Sab196087 str = conv_dyn_feature1( 12494734Sab196087 p_dyn.d_un.d_val, 12504734Sab196087 DUMP_CONVFMT, 12514734Sab196087 &conv_buf.dyn_feature1); 12522352Sab196087 break; 12532352Sab196087 case DT_POSFLAG_1: 12542352Sab196087 str = conv_dyn_posflag1( 12554734Sab196087 p_dyn.d_un.d_val, 12564734Sab196087 DUMP_CONVFMT, 12574734Sab196087 &conv_buf.dyn_posflag1); 12582352Sab196087 break; 12592352Sab196087 case DT_FLAGS_1: 12602352Sab196087 str = conv_dyn_flag1( 12615088Sab196087 p_dyn.d_un.d_val, 0, 12624734Sab196087 &conv_buf.dyn_flag1); 12632352Sab196087 break; 12646206Sab196087 case DT_SUNW_LDMACH: 12656206Sab196087 str = conv_ehdr_mach( 12666206Sab196087 p_dyn.d_un.d_val, 0, 12676206Sab196087 &conv_buf.inv); 12686206Sab196087 break; 12692352Sab196087 } 12702352Sab196087 } 12712352Sab196087 if (str) { /* Show as string */ 12722352Sab196087 (void) printf("%s", str); 12732352Sab196087 } else { /* Numeric form */ 12742352Sab196087 (void) printf(pdyn_Fmtptr, 12754734Sab196087 EC_ADDR(p_dyn.d_un.d_ptr)); 12762352Sab196087 } 12770Sstevel@tonic-gate break; 12780Sstevel@tonic-gate 12790Sstevel@tonic-gate /* 12801976Sab196087 * Depreciated items with a literal value 12810Sstevel@tonic-gate */ 12821976Sab196087 case DT_DEPRECATED_SPARC_REGISTER: 12831976Sab196087 (void) printf(pdyn_Fmtptr 12844734Sab196087 " (deprecated value)", 12854734Sab196087 EC_XWORD(p_dyn.d_un.d_val)); 12860Sstevel@tonic-gate break; 12871976Sab196087 12881976Sab196087 /* Ignored items */ 12891976Sab196087 case DT_SYMBOLIC: 12901976Sab196087 (void) printf("(ignored)"); 12910Sstevel@tonic-gate break; 12920Sstevel@tonic-gate } 12930Sstevel@tonic-gate (void) printf("\n"); 12940Sstevel@tonic-gate (void) gelf_getdyn(dyn_data, ii++, &p_dyn); 12950Sstevel@tonic-gate } 12960Sstevel@tonic-gate } 12970Sstevel@tonic-gate 12980Sstevel@tonic-gate /* 12990Sstevel@tonic-gate * Check for existence of static shared library information. 13000Sstevel@tonic-gate */ 13010Sstevel@tonic-gate while (header_num < p_ehdr.e_phnum) { 13020Sstevel@tonic-gate (void) gelf_getphdr(elf_file, header_num, &p_phdr); 13030Sstevel@tonic-gate if (p_phdr.p_type == PT_SHLIB) { 13040Sstevel@tonic-gate while (--lib_scns > 0) { 13050Sstevel@tonic-gate if (strcmp(l_scns->scn_name, ".lib") == 0) { 13060Sstevel@tonic-gate print_static(l_scns, filename); 13070Sstevel@tonic-gate } 13080Sstevel@tonic-gate l_scns++; 13090Sstevel@tonic-gate } 13100Sstevel@tonic-gate } 13110Sstevel@tonic-gate header_num++; 13120Sstevel@tonic-gate } 13132352Sab196087 #undef pdyn_Fmtptr 13140Sstevel@tonic-gate } 13150Sstevel@tonic-gate 13160Sstevel@tonic-gate /* 13170Sstevel@tonic-gate * Print the ELF header. Input is an ELF file descriptor 13180Sstevel@tonic-gate * and the filename. If f_flag is set, the ELF header is 13190Sstevel@tonic-gate * printed to stdout, otherwise the function returns after 13200Sstevel@tonic-gate * setting the pointer to the ELF header. Any values which 13210Sstevel@tonic-gate * are not known are printed in decimal. Fields must be updated 13220Sstevel@tonic-gate * as new values are added. 13230Sstevel@tonic-gate */ 13240Sstevel@tonic-gate static GElf_Ehdr * 13250Sstevel@tonic-gate dump_elf_header(Elf *elf_file, char *filename, GElf_Ehdr * elf_head_p) 13260Sstevel@tonic-gate { 13271976Sab196087 int class; 13280Sstevel@tonic-gate int field; 13290Sstevel@tonic-gate 13300Sstevel@tonic-gate if (gelf_getehdr(elf_file, elf_head_p) == NULL) { 13311618Srie (void) fprintf(stderr, "%s: %s: %s\n", prog_name, filename, 13321618Srie elf_errmsg(-1)); 13330Sstevel@tonic-gate return (NULL); 13340Sstevel@tonic-gate } 13350Sstevel@tonic-gate 13360Sstevel@tonic-gate class = (int)elf_head_p->e_ident[4]; 13370Sstevel@tonic-gate 13380Sstevel@tonic-gate if (class == ELFCLASS64) 13391618Srie field = 21; 13400Sstevel@tonic-gate else 13410Sstevel@tonic-gate field = 13; 13420Sstevel@tonic-gate 13430Sstevel@tonic-gate if (!f_flag) 13440Sstevel@tonic-gate return (elf_head_p); 13450Sstevel@tonic-gate 13460Sstevel@tonic-gate if (!p_flag) { 13471618Srie (void) printf("\n **** ELF HEADER ****\n"); 13481618Srie (void) printf("%-*s%-11s%-*sMachine Version\n", 13491618Srie field, "Class", "Data", field, "Type"); 13501618Srie (void) printf("%-*s%-11s%-*sFlags Ehsize\n", 13511618Srie field, "Entry", "Phoff", field, "Shoff"); 13521618Srie (void) printf("%-*s%-11s%-*sShnum Shstrndx\n\n", 13531618Srie field, "Phentsize", "Phnum", field, "Shentsz"); 13540Sstevel@tonic-gate } 13550Sstevel@tonic-gate 13560Sstevel@tonic-gate if (!v_flag) { 13570Sstevel@tonic-gate (void) printf("%-*d%-11d%-*d%-12d%d\n", 13584734Sab196087 field, elf_head_p->e_ident[4], elf_head_p->e_ident[5], 13594734Sab196087 field, (int)elf_head_p->e_type, (int)elf_head_p->e_machine, 13604734Sab196087 elf_head_p->e_version); 13610Sstevel@tonic-gate } else { 13624734Sab196087 Conv_inv_buf_t inv_buf; 13634734Sab196087 13641976Sab196087 (void) printf("%-*s", field, 13654734Sab196087 conv_ehdr_class(class, DUMP_CONVFMT, &inv_buf)); 13661976Sab196087 (void) printf("%-11s", 13674734Sab196087 conv_ehdr_data(elf_head_p->e_ident[5], DUMP_CONVFMT, 13684734Sab196087 &inv_buf)); 13691976Sab196087 (void) printf("%-*s", field, 13709273SAli.Bahrami@Sun.COM conv_ehdr_type(elf_head_p->e_ident[EI_OSABI], 13719273SAli.Bahrami@Sun.COM elf_head_p->e_type, DUMP_CONVFMT, &inv_buf)); 13721976Sab196087 (void) printf("%-12s", 13734734Sab196087 conv_ehdr_mach(elf_head_p->e_machine, DUMP_CONVFMT, 13744734Sab196087 &inv_buf)); 13751976Sab196087 (void) printf("%s\n", 13764734Sab196087 conv_ehdr_vers(elf_head_p->e_version, DUMP_CONVFMT, 13774734Sab196087 &inv_buf)); 13780Sstevel@tonic-gate } 13790Sstevel@tonic-gate (void) printf("%-#*llx%-#11llx%-#*llx%-#12x%#x\n", 13804734Sab196087 field, EC_ADDR(elf_head_p->e_entry), EC_OFF(elf_head_p->e_phoff), 13814734Sab196087 field, EC_OFF(elf_head_p->e_shoff), EC_WORD(elf_head_p->e_flags), 13824734Sab196087 EC_WORD(elf_head_p->e_ehsize)); 13830Sstevel@tonic-gate if (!v_flag || (elf_head_p->e_shstrndx != SHN_XINDEX)) { 13840Sstevel@tonic-gate (void) printf("%-#*x%-11u%-#*x%-12u%u\n", 13854734Sab196087 field, EC_WORD(elf_head_p->e_phentsize), 13864734Sab196087 EC_WORD(elf_head_p->e_phnum), 13874734Sab196087 field, EC_WORD(elf_head_p->e_shentsize), 13884734Sab196087 EC_WORD(elf_head_p->e_shnum), 13894734Sab196087 EC_WORD(elf_head_p->e_shstrndx)); 13900Sstevel@tonic-gate } else { 13910Sstevel@tonic-gate (void) printf("%-#*x%-11u%-#*x%-12uXINDEX\n", 13924734Sab196087 field, EC_WORD(elf_head_p->e_phentsize), 13934734Sab196087 EC_WORD(elf_head_p->e_phnum), 13944734Sab196087 field, EC_WORD(elf_head_p->e_shentsize), 13954734Sab196087 EC_WORD(elf_head_p->e_shnum)); 13960Sstevel@tonic-gate } 13970Sstevel@tonic-gate if ((elf_head_p->e_shnum == 0) && (elf_head_p->e_shoff > 0)) { 13980Sstevel@tonic-gate Elf_Scn *scn; 13990Sstevel@tonic-gate GElf_Shdr shdr0; 14000Sstevel@tonic-gate int field; 14010Sstevel@tonic-gate 14020Sstevel@tonic-gate if (gelf_getclass(elf_file) == ELFCLASS64) 14031618Srie field = 21; 14040Sstevel@tonic-gate else 14051618Srie field = 13; 14060Sstevel@tonic-gate if (!p_flag) { 14070Sstevel@tonic-gate (void) printf("\n **** SECTION HEADER[0] " 14080Sstevel@tonic-gate "{Elf Extensions} ****\n"); 14090Sstevel@tonic-gate (void) printf( 14100Sstevel@tonic-gate "[No]\tType\tFlags\t%-*s %-*s%-*s%sName\n", 14110Sstevel@tonic-gate field, "Addr", field, "Offset", field, 14120Sstevel@tonic-gate "Size(shnum)", 14130Sstevel@tonic-gate /* compatibility: tab for elf32 */ 14141618Srie (field == 13) ? "\t" : " "); 14150Sstevel@tonic-gate (void) printf("\tLn(strndx) Info\t%-*s Entsize\n", 14160Sstevel@tonic-gate field, "Adralgn"); 14170Sstevel@tonic-gate } 14180Sstevel@tonic-gate if ((scn = elf_getscn(elf_file, 0)) == NULL) { 14190Sstevel@tonic-gate (void) fprintf(stderr, 14204734Sab196087 "%s: %s: elf_getscn failed: %s\n", 14214734Sab196087 prog_name, filename, elf_errmsg(-1)); 14220Sstevel@tonic-gate return (NULL); 14230Sstevel@tonic-gate } 14240Sstevel@tonic-gate if (gelf_getshdr(scn, &shdr0) == 0) { 14250Sstevel@tonic-gate (void) fprintf(stderr, 14264734Sab196087 "%s: %s: gelf_getshdr: %s\n", 14274734Sab196087 prog_name, filename, elf_errmsg(-1)); 14280Sstevel@tonic-gate return (NULL); 14290Sstevel@tonic-gate } 14300Sstevel@tonic-gate (void) printf("[0]\t%u\t%llu\t", EC_WORD(shdr0.sh_type), 14314734Sab196087 EC_XWORD(shdr0.sh_flags)); 14320Sstevel@tonic-gate 14332352Sab196087 (void) printf("%-#*llx %-#*llx%-*llu%s%-*u\n", 14344734Sab196087 field, EC_ADDR(shdr0.sh_addr), 14354734Sab196087 field, EC_OFF(shdr0.sh_offset), 14364734Sab196087 field, EC_XWORD(shdr0.sh_size), 14374734Sab196087 /* compatibility: tab for elf32 */ 14384734Sab196087 ((field == 13) ? "\t" : " "), 14394734Sab196087 field, EC_WORD(shdr0.sh_name)); 14400Sstevel@tonic-gate 14410Sstevel@tonic-gate (void) printf("\t%u\t%u\t%-#*llx %-#*llx\n", 14424734Sab196087 EC_WORD(shdr0.sh_link), 14434734Sab196087 EC_WORD(shdr0.sh_info), 14444734Sab196087 field, EC_XWORD(shdr0.sh_addralign), 14454734Sab196087 field, EC_XWORD(shdr0.sh_entsize)); 14460Sstevel@tonic-gate } 14470Sstevel@tonic-gate (void) printf("\n"); 14480Sstevel@tonic-gate 14490Sstevel@tonic-gate return (elf_head_p); 14500Sstevel@tonic-gate } 14510Sstevel@tonic-gate 14520Sstevel@tonic-gate /* 14530Sstevel@tonic-gate * Print section contents. Input is an ELF file descriptor, 14540Sstevel@tonic-gate * the ELF header, the SCNTAB structure, 14550Sstevel@tonic-gate * the number of symbols, and the filename. 14560Sstevel@tonic-gate * The number of sections, 14570Sstevel@tonic-gate * and the offset into the SCNTAB structure will be 14580Sstevel@tonic-gate * set in dump_section if d_flag or n_flag are set. 14590Sstevel@tonic-gate * If v_flag is set, sections which can be interpreted will 14600Sstevel@tonic-gate * be interpreted, otherwise raw data will be output in hexidecimal. 14610Sstevel@tonic-gate */ 14620Sstevel@tonic-gate static void 14630Sstevel@tonic-gate print_section(Elf *elf_file, 14640Sstevel@tonic-gate GElf_Ehdr *p_ehdr, SCNTAB *p, int num_scns, char *filename) 14650Sstevel@tonic-gate { 14660Sstevel@tonic-gate unsigned char *p_sec; 14670Sstevel@tonic-gate int i; 14680Sstevel@tonic-gate size_t size; 14690Sstevel@tonic-gate 14700Sstevel@tonic-gate for (i = 0; i < num_scns; i++, p++) { 14710Sstevel@tonic-gate GElf_Shdr shdr; 14720Sstevel@tonic-gate 14730Sstevel@tonic-gate size = 0; 14740Sstevel@tonic-gate if (s_flag && !v_flag) 14750Sstevel@tonic-gate p_sec = (unsigned char *)get_rawscn(p->p_sd, &size); 14760Sstevel@tonic-gate else 14770Sstevel@tonic-gate p_sec = (unsigned char *)get_scndata(p->p_sd, &size); 14780Sstevel@tonic-gate 14790Sstevel@tonic-gate if ((gelf_getshdr(p->p_sd, &shdr) != NULL) && 14800Sstevel@tonic-gate (shdr.sh_type == SHT_NOBITS)) { 14810Sstevel@tonic-gate continue; 14820Sstevel@tonic-gate } 14830Sstevel@tonic-gate if (s_flag && !v_flag) { 14840Sstevel@tonic-gate (void) printf("\n%s:\n", p->scn_name); 14850Sstevel@tonic-gate print_rawdata(p_sec, size); 14860Sstevel@tonic-gate continue; 14870Sstevel@tonic-gate } 14880Sstevel@tonic-gate if (shdr.sh_type == SHT_SYMTAB) { 14890Sstevel@tonic-gate dump_symbol_table(elf_file, p, filename); 14900Sstevel@tonic-gate continue; 14910Sstevel@tonic-gate } 14920Sstevel@tonic-gate if (shdr.sh_type == SHT_DYNSYM) { 14930Sstevel@tonic-gate dump_symbol_table(elf_file, p, filename); 14940Sstevel@tonic-gate continue; 14950Sstevel@tonic-gate } 14960Sstevel@tonic-gate if (shdr.sh_type == SHT_STRTAB) { 14970Sstevel@tonic-gate dump_string_table(p, 1); 14980Sstevel@tonic-gate continue; 14990Sstevel@tonic-gate } 15000Sstevel@tonic-gate if (shdr.sh_type == SHT_RELA) { 15010Sstevel@tonic-gate dump_reloc_table(elf_file, p_ehdr, p, 1, filename); 15020Sstevel@tonic-gate continue; 15030Sstevel@tonic-gate } 15040Sstevel@tonic-gate if (shdr.sh_type == SHT_REL) { 15050Sstevel@tonic-gate dump_reloc_table(elf_file, p_ehdr, p, 1, filename); 15060Sstevel@tonic-gate continue; 15070Sstevel@tonic-gate } 15080Sstevel@tonic-gate if (shdr.sh_type == SHT_DYNAMIC) { 15090Sstevel@tonic-gate dump_dynamic(elf_file, p, 1, filename); 15100Sstevel@tonic-gate continue; 15110Sstevel@tonic-gate } 15120Sstevel@tonic-gate 15130Sstevel@tonic-gate (void) printf("\n%s:\n", p->scn_name); 15140Sstevel@tonic-gate print_rawdata(p_sec, size); 15150Sstevel@tonic-gate } 15160Sstevel@tonic-gate (void) printf("\n"); 15170Sstevel@tonic-gate } 15180Sstevel@tonic-gate 15190Sstevel@tonic-gate /* 15200Sstevel@tonic-gate * Print section contents. This function does not print the contents 15210Sstevel@tonic-gate * of the sections but sets up the parameters and then calls 15220Sstevel@tonic-gate * print_section to print the contents. Calling another function to print 15230Sstevel@tonic-gate * the contents allows both -d and -n to work correctly 15240Sstevel@tonic-gate * simultaneously. Input is an ELF file descriptor, the ELF header, 15250Sstevel@tonic-gate * the SCNTAB structure, the number of sections, and the filename. 15260Sstevel@tonic-gate * Set the range of sections if d_flag, and set section name if 15270Sstevel@tonic-gate * n_flag. 15280Sstevel@tonic-gate */ 15290Sstevel@tonic-gate static void 15300Sstevel@tonic-gate dump_section(Elf *elf_file, 15310Sstevel@tonic-gate GElf_Ehdr *p_ehdr, SCNTAB *s, int num_scns, char *filename) 15320Sstevel@tonic-gate { 15330Sstevel@tonic-gate SCNTAB *n_range, *d_range; /* for use with -n and -d modifiers */ 15340Sstevel@tonic-gate int i; 15350Sstevel@tonic-gate int found_it = 0; /* for use with -n section_name */ 15360Sstevel@tonic-gate 15370Sstevel@tonic-gate if (n_flag) { 15380Sstevel@tonic-gate n_range = s; 15390Sstevel@tonic-gate 15400Sstevel@tonic-gate for (i = 0; i < num_scns; i++, n_range++) { 15410Sstevel@tonic-gate if ((strcmp(name, n_range->scn_name)) != 0) 15420Sstevel@tonic-gate continue; 15430Sstevel@tonic-gate else { 15440Sstevel@tonic-gate found_it = 1; 15450Sstevel@tonic-gate print_section(elf_file, p_ehdr, 15464734Sab196087 n_range, 1, filename); 15470Sstevel@tonic-gate } 15480Sstevel@tonic-gate } 15490Sstevel@tonic-gate 15500Sstevel@tonic-gate if (!found_it) { 15510Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s not found\n", 15524734Sab196087 prog_name, filename, name); 15530Sstevel@tonic-gate } 15540Sstevel@tonic-gate } /* end n_flag */ 15550Sstevel@tonic-gate 15560Sstevel@tonic-gate if (d_flag) { 15570Sstevel@tonic-gate d_range = s; 15580Sstevel@tonic-gate d_num = check_range(d_low, d_hi, num_scns, filename); 15590Sstevel@tonic-gate if (d_num < 0) 15600Sstevel@tonic-gate return; 15610Sstevel@tonic-gate d_range += d_low - 1; 15620Sstevel@tonic-gate 15630Sstevel@tonic-gate print_section(elf_file, p_ehdr, d_range, d_num, filename); 15640Sstevel@tonic-gate } /* end d_flag */ 15650Sstevel@tonic-gate 15660Sstevel@tonic-gate if (!n_flag && !d_flag) 15670Sstevel@tonic-gate print_section(elf_file, p_ehdr, s, num_scns, filename); 15680Sstevel@tonic-gate } 15690Sstevel@tonic-gate 15700Sstevel@tonic-gate /* 15710Sstevel@tonic-gate * Print the section header table. This function does not print the contents 15720Sstevel@tonic-gate * of the section headers but sets up the parameters and then calls 15730Sstevel@tonic-gate * print_shdr to print the contents. Calling another function to print 15740Sstevel@tonic-gate * the contents allows both -d and -n to work correctly 15750Sstevel@tonic-gate * simultaneously. Input is the SCNTAB structure, 15760Sstevel@tonic-gate * the number of sections from the ELF header, and the filename. 15770Sstevel@tonic-gate * Set the range of section headers to print if d_flag, and set 15780Sstevel@tonic-gate * name of section header to print if n_flag. 15790Sstevel@tonic-gate */ 15800Sstevel@tonic-gate static void 15810Sstevel@tonic-gate dump_shdr(Elf *elf_file, SCNTAB *s, int num_scns, char *filename) 15820Sstevel@tonic-gate { 15830Sstevel@tonic-gate 15840Sstevel@tonic-gate SCNTAB *n_range, *d_range; /* for use with -n and -d modifiers */ 15850Sstevel@tonic-gate int field; 15860Sstevel@tonic-gate int i; 15870Sstevel@tonic-gate int found_it = 0; /* for use with -n section_name */ 15880Sstevel@tonic-gate 15890Sstevel@tonic-gate if (gelf_getclass(elf_file) == ELFCLASS64) 15901618Srie field = 21; 15910Sstevel@tonic-gate else 15921618Srie field = 13; 15930Sstevel@tonic-gate 15940Sstevel@tonic-gate if (!p_flag) { 15950Sstevel@tonic-gate (void) printf("\n **** SECTION HEADER TABLE ****\n"); 15960Sstevel@tonic-gate (void) printf("[No]\tType\tFlags\t%-*s %-*s %-*s%sName\n", 15970Sstevel@tonic-gate field, "Addr", field, "Offset", field, "Size", 15980Sstevel@tonic-gate /* compatibility: tab for elf32 */ 15991618Srie (field == 13) ? "\t" : " "); 16000Sstevel@tonic-gate (void) printf("\tLink\tInfo\t%-*s Entsize\n\n", 16010Sstevel@tonic-gate field, "Adralgn"); 16020Sstevel@tonic-gate } 16030Sstevel@tonic-gate 16040Sstevel@tonic-gate if (n_flag) { 16050Sstevel@tonic-gate n_range = s; 16060Sstevel@tonic-gate 16070Sstevel@tonic-gate for (i = 1; i <= num_scns; i++, n_range++) { 16080Sstevel@tonic-gate if ((strcmp(name, n_range->scn_name)) != 0) 16090Sstevel@tonic-gate continue; 16100Sstevel@tonic-gate else { 16110Sstevel@tonic-gate found_it = 1; 16120Sstevel@tonic-gate print_shdr(elf_file, n_range, 1, i); 16130Sstevel@tonic-gate } 16140Sstevel@tonic-gate } 16150Sstevel@tonic-gate 16160Sstevel@tonic-gate if (!found_it) { 16170Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s not found\n", 16184734Sab196087 prog_name, filename, name); 16190Sstevel@tonic-gate } 16200Sstevel@tonic-gate } /* end n_flag */ 16210Sstevel@tonic-gate 16220Sstevel@tonic-gate if (d_flag) { 16230Sstevel@tonic-gate d_range = s; 16240Sstevel@tonic-gate d_num = check_range(d_low, d_hi, num_scns, filename); 16250Sstevel@tonic-gate if (d_num < 0) 16260Sstevel@tonic-gate return; 16270Sstevel@tonic-gate d_range += d_low - 1; 16280Sstevel@tonic-gate 16290Sstevel@tonic-gate print_shdr(elf_file, d_range, d_num, d_low); 16300Sstevel@tonic-gate } /* end d_flag */ 16310Sstevel@tonic-gate 16320Sstevel@tonic-gate if (!n_flag && !d_flag) 16330Sstevel@tonic-gate print_shdr(elf_file, s, num_scns, 1); 16340Sstevel@tonic-gate } 16350Sstevel@tonic-gate 16360Sstevel@tonic-gate /* 16370Sstevel@tonic-gate * Process all of the command line options (except 16380Sstevel@tonic-gate * for -a, -g, -f, and -o). All of the options processed 16390Sstevel@tonic-gate * by this function require the presence of the section 16400Sstevel@tonic-gate * header table and will not be processed if it is not present. 16410Sstevel@tonic-gate * Set up a buffer containing section name, section header, 16420Sstevel@tonic-gate * and section descriptor for each section in the file. This 16430Sstevel@tonic-gate * structure is used to avoid duplicate calls to libelf functions. 16440Sstevel@tonic-gate * Structure members for the symbol table, the debugging information, 16450Sstevel@tonic-gate * and the line number information are global. All of the 16460Sstevel@tonic-gate * rest are local. 16470Sstevel@tonic-gate */ 16480Sstevel@tonic-gate static void 16490Sstevel@tonic-gate dump_section_table(Elf *elf_file, GElf_Ehdr *elf_head_p, char *filename) 16500Sstevel@tonic-gate { 16510Sstevel@tonic-gate 16520Sstevel@tonic-gate static SCNTAB *buffer, *p_scns; 16530Sstevel@tonic-gate Elf_Scn *scn = 0; 16540Sstevel@tonic-gate char *s_name = NULL; 16550Sstevel@tonic-gate int found = 0; 16560Sstevel@tonic-gate unsigned int num_scns; 16570Sstevel@tonic-gate size_t shstrndx; 16580Sstevel@tonic-gate size_t shnum; 16590Sstevel@tonic-gate 16600Sstevel@tonic-gate 1661*9900SAli.Bahrami@Sun.COM if (elf_getshdrnum(elf_file, &shnum) == -1) { 16620Sstevel@tonic-gate (void) fprintf(stderr, 1663*9900SAli.Bahrami@Sun.COM "%s: %s: elf_getshdrnum failed: %s\n", 16644734Sab196087 prog_name, filename, elf_errmsg(-1)); 16650Sstevel@tonic-gate return; 16660Sstevel@tonic-gate } 1667*9900SAli.Bahrami@Sun.COM if (elf_getshdrstrndx(elf_file, &shstrndx) == -1) { 16680Sstevel@tonic-gate (void) fprintf(stderr, 1669*9900SAli.Bahrami@Sun.COM "%s: %s: elf_getshdrstrndx failed: %s\n", 16704734Sab196087 prog_name, filename, elf_errmsg(-1)); 16710Sstevel@tonic-gate return; 16720Sstevel@tonic-gate } 16730Sstevel@tonic-gate 16740Sstevel@tonic-gate if ((buffer = calloc(shnum, sizeof (SCNTAB))) == NULL) { 16750Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: cannot calloc space\n", 16764734Sab196087 prog_name, filename); 16770Sstevel@tonic-gate return; 16780Sstevel@tonic-gate } 16790Sstevel@tonic-gate /* LINTED */ 16800Sstevel@tonic-gate num_scns = (int)shnum - 1; 16810Sstevel@tonic-gate 16820Sstevel@tonic-gate p_symtab = (SCNTAB *)0; 16830Sstevel@tonic-gate p_dynsym = (SCNTAB *)0; 16840Sstevel@tonic-gate p_scns = buffer; 16850Sstevel@tonic-gate p_head_scns = buffer; 16860Sstevel@tonic-gate 16870Sstevel@tonic-gate while ((scn = elf_nextscn(elf_file, scn)) != 0) { 16880Sstevel@tonic-gate if ((gelf_getshdr(scn, &buffer->p_shdr)) == 0) { 16890Sstevel@tonic-gate (void) fprintf(stderr, 16904734Sab196087 "%s: %s: %s\n", prog_name, filename, 16914734Sab196087 elf_errmsg(-1)); 16920Sstevel@tonic-gate return; 16930Sstevel@tonic-gate } 16944734Sab196087 s_name = (char *) 16954734Sab196087 elf_strptr(elf_file, shstrndx, buffer->p_shdr.sh_name); 16960Sstevel@tonic-gate buffer->scn_name = s_name ? s_name : (char *)UNKNOWN; 16970Sstevel@tonic-gate buffer->p_sd = scn; 16980Sstevel@tonic-gate 16990Sstevel@tonic-gate if (buffer->p_shdr.sh_type == SHT_SYMTAB) { 17000Sstevel@tonic-gate found += 1; 17010Sstevel@tonic-gate p_symtab = buffer; 17020Sstevel@tonic-gate } 17030Sstevel@tonic-gate if (buffer->p_shdr.sh_type == SHT_DYNSYM) 17040Sstevel@tonic-gate p_dynsym = buffer; 17050Sstevel@tonic-gate buffer++; 17060Sstevel@tonic-gate } 17070Sstevel@tonic-gate 17080Sstevel@tonic-gate /* 17090Sstevel@tonic-gate * These functions depend upon the presence of the section header table 17100Sstevel@tonic-gate * and will not be invoked in its absence 17110Sstevel@tonic-gate */ 17120Sstevel@tonic-gate if (h_flag) { 17130Sstevel@tonic-gate dump_shdr(elf_file, p_scns, num_scns, filename); 17140Sstevel@tonic-gate } 17150Sstevel@tonic-gate if (p_symtab && (t_flag || T_flag)) { 17160Sstevel@tonic-gate dump_symbol_table(elf_file, p_symtab, filename); 17170Sstevel@tonic-gate } 17180Sstevel@tonic-gate if (c_flag) { 17190Sstevel@tonic-gate dump_string_table(p_scns, num_scns); 17200Sstevel@tonic-gate } 17210Sstevel@tonic-gate if (r_flag) { 17220Sstevel@tonic-gate dump_reloc_table(elf_file, elf_head_p, 17234734Sab196087 p_scns, num_scns, filename); 17240Sstevel@tonic-gate } 17250Sstevel@tonic-gate if (L_flag) { 17260Sstevel@tonic-gate dump_dynamic(elf_file, p_scns, num_scns, filename); 17270Sstevel@tonic-gate } 17280Sstevel@tonic-gate if (s_flag) { 17290Sstevel@tonic-gate dump_section(elf_file, elf_head_p, p_scns, 17304734Sab196087 num_scns, filename); 17310Sstevel@tonic-gate } 17320Sstevel@tonic-gate } 17330Sstevel@tonic-gate 17340Sstevel@tonic-gate /* 17350Sstevel@tonic-gate * Load the archive string table(s) (for extended-length strings) 17360Sstevel@tonic-gate * into an in-core table/list 17370Sstevel@tonic-gate */ 17380Sstevel@tonic-gate static struct stab_list_s * 17390Sstevel@tonic-gate load_arstring_table(struct stab_list_s *STabList, 17400Sstevel@tonic-gate int fd, Elf *elf_file, Elf_Arhdr *p_ar, char *filename) 17410Sstevel@tonic-gate { 17420Sstevel@tonic-gate off_t here; 17430Sstevel@tonic-gate struct stab_list_s *STL_entry, *STL_next; 17440Sstevel@tonic-gate 17450Sstevel@tonic-gate if (p_ar) { 17460Sstevel@tonic-gate STL_entry = malloc(sizeof (struct stab_list_s)); 17470Sstevel@tonic-gate STL_entry->next = 0; 17480Sstevel@tonic-gate STL_entry->strings = 0; 17490Sstevel@tonic-gate STL_entry->size = 0; 17500Sstevel@tonic-gate 17510Sstevel@tonic-gate if (!STabList) 17520Sstevel@tonic-gate STabList = STL_entry; 17530Sstevel@tonic-gate else { 17540Sstevel@tonic-gate STL_next = STabList; 17550Sstevel@tonic-gate while (STL_next->next != (void *)0) 17560Sstevel@tonic-gate STL_next = STL_next->next; 17570Sstevel@tonic-gate STL_next->next = STL_entry; 17580Sstevel@tonic-gate } 17590Sstevel@tonic-gate 17600Sstevel@tonic-gate STL_entry->size = p_ar->ar_size; 17610Sstevel@tonic-gate STL_entry->strings = malloc(p_ar->ar_size); 17620Sstevel@tonic-gate here = elf_getbase(elf_file); 17630Sstevel@tonic-gate if ((lseek(fd, here, 0)) != here) { 17640Sstevel@tonic-gate (void) fprintf(stderr, 17654734Sab196087 "%s: %s: could not lseek\n", prog_name, filename); 17660Sstevel@tonic-gate } 17670Sstevel@tonic-gate 17680Sstevel@tonic-gate if ((read(fd, STL_entry->strings, p_ar->ar_size)) == -1) { 17690Sstevel@tonic-gate (void) fprintf(stderr, 17704734Sab196087 "%s: %s: could not read\n", prog_name, filename); 17710Sstevel@tonic-gate } 17720Sstevel@tonic-gate } 17730Sstevel@tonic-gate return (STabList); 17740Sstevel@tonic-gate } 17750Sstevel@tonic-gate 17760Sstevel@tonic-gate /* 17770Sstevel@tonic-gate * Print the archive header for each member of an archive. 17780Sstevel@tonic-gate * Also call ar_sym_read to print the symbols in the 17790Sstevel@tonic-gate * archive symbol table if g_flag. Input is a file descriptor, 17800Sstevel@tonic-gate * an ELF file descriptor, and the filename. Putting the call 17810Sstevel@tonic-gate * to dump the archive symbol table in this function is more 17820Sstevel@tonic-gate * efficient since it is necessary to examine the archive member 17830Sstevel@tonic-gate * name in the archive header to determine which member is the 17840Sstevel@tonic-gate * symbol table. 17850Sstevel@tonic-gate */ 17860Sstevel@tonic-gate static void 17870Sstevel@tonic-gate dump_ar_hdr(int fd, Elf *elf_file, char *filename) 17880Sstevel@tonic-gate { 17890Sstevel@tonic-gate extern int v_flag, g_flag, a_flag, p_flag; 17900Sstevel@tonic-gate Elf_Arhdr *p_ar; 17910Sstevel@tonic-gate Elf *arf; 17920Sstevel@tonic-gate Elf_Cmd cmd; 17930Sstevel@tonic-gate int title = 0; 17940Sstevel@tonic-gate int err = 0; 17950Sstevel@tonic-gate 17960Sstevel@tonic-gate char buf[DATESIZE]; 17970Sstevel@tonic-gate 17980Sstevel@tonic-gate cmd = ELF_C_READ; 17990Sstevel@tonic-gate while ((arf = elf_begin(fd, cmd, elf_file)) != 0) { 18000Sstevel@tonic-gate p_ar = elf_getarhdr(arf); 18010Sstevel@tonic-gate if (p_ar == NULL) { 18020Sstevel@tonic-gate (void) fprintf(stderr, 18034734Sab196087 "%s: %s: %s\n", prog_name, filename, 18044734Sab196087 elf_errmsg(-1)); 18050Sstevel@tonic-gate continue; 18060Sstevel@tonic-gate } 18070Sstevel@tonic-gate if (strcmp(p_ar->ar_name, "/") == 0) { 18080Sstevel@tonic-gate if (g_flag) 18090Sstevel@tonic-gate ar_sym_read(elf_file, filename); 18100Sstevel@tonic-gate } else if (strcmp(p_ar->ar_name, "//") == 0) { 18110Sstevel@tonic-gate StringTableList = load_arstring_table( 18124734Sab196087 StringTableList, fd, arf, p_ar, filename); 18130Sstevel@tonic-gate cmd = elf_next(arf); 18140Sstevel@tonic-gate (void) elf_end(arf); 18150Sstevel@tonic-gate continue; 18160Sstevel@tonic-gate } else { 18170Sstevel@tonic-gate if (a_flag) { 18180Sstevel@tonic-gate (void) printf("%s[%s]:\n", filename, 18194734Sab196087 p_ar->ar_name); 18200Sstevel@tonic-gate if (!p_flag && title == 0) { 18210Sstevel@tonic-gate if (!v_flag) 18220Sstevel@tonic-gate (void) printf( 18230Sstevel@tonic-gate "\n\n\t\t\t***ARCHIVE HEADER***" 18240Sstevel@tonic-gate "\n Date Uid Gid Mode Size Member Name\n\n"); 18250Sstevel@tonic-gate else 18260Sstevel@tonic-gate (void) printf( 18270Sstevel@tonic-gate "\n\n\t\t\t***ARCHIVE HEADER***" 18280Sstevel@tonic-gate "\n Date Uid Gid Mode Size Member Name\n\n"); 18290Sstevel@tonic-gate title = 1; 18300Sstevel@tonic-gate } 18310Sstevel@tonic-gate if (!v_flag) { 18320Sstevel@tonic-gate (void) printf( 18330Sstevel@tonic-gate "\t0x%.8lx %6d %6d 0%.6ho 0x%.8lx %-s\n\n", 18344734Sab196087 p_ar->ar_date, (int)p_ar->ar_uid, 18354734Sab196087 (int)p_ar->ar_gid, 18364734Sab196087 (int)p_ar->ar_mode, 18374734Sab196087 p_ar->ar_size, p_ar->ar_name); 18380Sstevel@tonic-gate } else { 18390Sstevel@tonic-gate if ((strftime(buf, DATESIZE, 18400Sstevel@tonic-gate "%b %d %H:%M:%S %Y", 18410Sstevel@tonic-gate localtime( 18420Sstevel@tonic-gate &(p_ar->ar_date)))) == 0) { 18430Sstevel@tonic-gate (void) fprintf(stderr, 18440Sstevel@tonic-gate "%s: %s: don't have enough space to store the date\n", prog_name, filename); 18450Sstevel@tonic-gate exit(1); 18460Sstevel@tonic-gate } 18470Sstevel@tonic-gate (void) printf( 18484734Sab196087 "\t%s %6d %6d 0%.6ho 0x%.8lx %-s\n\n", 18494734Sab196087 buf, (int)p_ar->ar_uid, 18504734Sab196087 (int)p_ar->ar_gid, 18514734Sab196087 (int)p_ar->ar_mode, 18524734Sab196087 p_ar->ar_size, p_ar->ar_name); 18530Sstevel@tonic-gate } 18540Sstevel@tonic-gate } 18550Sstevel@tonic-gate } 18560Sstevel@tonic-gate cmd = elf_next(arf); 18570Sstevel@tonic-gate (void) elf_end(arf); 18580Sstevel@tonic-gate } /* end while */ 18590Sstevel@tonic-gate 18600Sstevel@tonic-gate err = elf_errno(); 18610Sstevel@tonic-gate if (err != 0) { 18620Sstevel@tonic-gate (void) fprintf(stderr, 18634734Sab196087 "%s: %s: %s\n", prog_name, filename, elf_errmsg(err)); 18640Sstevel@tonic-gate } 18650Sstevel@tonic-gate } 18660Sstevel@tonic-gate 18670Sstevel@tonic-gate /* 18680Sstevel@tonic-gate * Process member files of an archive. This function provides 18690Sstevel@tonic-gate * a loop through an archive equivalent the processing of 18700Sstevel@tonic-gate * each_file for individual object files. 18710Sstevel@tonic-gate */ 18720Sstevel@tonic-gate static void 18730Sstevel@tonic-gate dump_ar_files(int fd, Elf *elf_file, char *filename) 18740Sstevel@tonic-gate { 18750Sstevel@tonic-gate Elf_Arhdr *p_ar; 18760Sstevel@tonic-gate Elf *arf; 18770Sstevel@tonic-gate Elf_Cmd cmd; 18780Sstevel@tonic-gate Elf_Kind file_type; 18790Sstevel@tonic-gate GElf_Ehdr elf_head; 18800Sstevel@tonic-gate char *fullname; 18810Sstevel@tonic-gate 18820Sstevel@tonic-gate cmd = ELF_C_READ; 18830Sstevel@tonic-gate while ((arf = elf_begin(fd, cmd, elf_file)) != 0) { 18841618Srie size_t len; 18851618Srie 18860Sstevel@tonic-gate p_ar = elf_getarhdr(arf); 18870Sstevel@tonic-gate if (p_ar == NULL) { 18884734Sab196087 (void) fprintf(stderr, "%s: %s: %s\n", 18894734Sab196087 prog_name, filename, elf_errmsg(-1)); 18900Sstevel@tonic-gate return; 18910Sstevel@tonic-gate } 18920Sstevel@tonic-gate if ((strcmp(p_ar->ar_name, "/") == 0) || 18934734Sab196087 (strcmp(p_ar->ar_name, "//") == 0)) { 18940Sstevel@tonic-gate cmd = elf_next(arf); 18950Sstevel@tonic-gate (void) elf_end(arf); 18960Sstevel@tonic-gate continue; 18970Sstevel@tonic-gate } 18980Sstevel@tonic-gate 18991618Srie len = strlen(filename) + strlen(p_ar->ar_name) + 3; 19001618Srie if ((fullname = malloc(len)) == NULL) 19011618Srie return; 19021618Srie (void) snprintf(fullname, len, "%s[%s]", filename, 19031618Srie p_ar->ar_name); 19040Sstevel@tonic-gate (void) printf("\n%s:\n", fullname); 19050Sstevel@tonic-gate file_type = elf_kind(arf); 19060Sstevel@tonic-gate if (file_type == ELF_K_ELF) { 19070Sstevel@tonic-gate if (dump_elf_header(arf, fullname, &elf_head) == NULL) 19080Sstevel@tonic-gate return; 19090Sstevel@tonic-gate if (o_flag) 19100Sstevel@tonic-gate dump_exec_header(arf, 19114734Sab196087 (unsigned)elf_head.e_phnum, fullname); 19120Sstevel@tonic-gate if (x_flag) 19130Sstevel@tonic-gate dump_section_table(arf, &elf_head, fullname); 19140Sstevel@tonic-gate } else { 19154734Sab196087 (void) fprintf(stderr, "%s: %s: invalid file type\n", 19164734Sab196087 prog_name, fullname); 19170Sstevel@tonic-gate cmd = elf_next(arf); 19180Sstevel@tonic-gate (void) elf_end(arf); 19190Sstevel@tonic-gate continue; 19200Sstevel@tonic-gate } 19210Sstevel@tonic-gate 19220Sstevel@tonic-gate cmd = elf_next(arf); 19230Sstevel@tonic-gate (void) elf_end(arf); 19240Sstevel@tonic-gate } /* end while */ 19250Sstevel@tonic-gate } 19260Sstevel@tonic-gate 19270Sstevel@tonic-gate /* 19280Sstevel@tonic-gate * Takes a filename as input. Test first for a valid version 19290Sstevel@tonic-gate * of libelf.a and exit on error. Process each valid file 19300Sstevel@tonic-gate * or archive given as input on the command line. Check 19310Sstevel@tonic-gate * for file type. If it is an archive, process the archive- 19320Sstevel@tonic-gate * specific options first, then files within the archive. 19330Sstevel@tonic-gate * If it is an ELF object file, process it; otherwise 19340Sstevel@tonic-gate * warn that it is an invalid file type. 19350Sstevel@tonic-gate * All options except the archive-specific and program 19360Sstevel@tonic-gate * execution header are processed in the function, dump_section_table. 19370Sstevel@tonic-gate */ 19380Sstevel@tonic-gate static void 19390Sstevel@tonic-gate each_file(char *filename) 19400Sstevel@tonic-gate { 19410Sstevel@tonic-gate Elf *elf_file; 19420Sstevel@tonic-gate GElf_Ehdr elf_head; 19430Sstevel@tonic-gate int fd; 19440Sstevel@tonic-gate Elf_Kind file_type; 19450Sstevel@tonic-gate 19460Sstevel@tonic-gate struct stat buf; 19470Sstevel@tonic-gate 19480Sstevel@tonic-gate Elf_Cmd cmd; 19490Sstevel@tonic-gate errno = 0; 19500Sstevel@tonic-gate 19510Sstevel@tonic-gate if (stat(filename, &buf) == -1) { 19521618Srie int err = errno; 19531618Srie (void) fprintf(stderr, "%s: %s: %s", prog_name, filename, 19541618Srie strerror(err)); 19550Sstevel@tonic-gate return; 19560Sstevel@tonic-gate } 19570Sstevel@tonic-gate 19580Sstevel@tonic-gate if ((fd = open((filename), O_RDONLY)) == -1) { 19591618Srie (void) fprintf(stderr, "%s: %s: cannot read\n", prog_name, 19601618Srie filename); 19610Sstevel@tonic-gate return; 19620Sstevel@tonic-gate } 19630Sstevel@tonic-gate cmd = ELF_C_READ; 19640Sstevel@tonic-gate if ((elf_file = elf_begin(fd, cmd, (Elf *)0)) == NULL) { 19651618Srie (void) fprintf(stderr, "%s: %s: %s\n", prog_name, filename, 19661618Srie elf_errmsg(-1)); 19670Sstevel@tonic-gate return; 19680Sstevel@tonic-gate } 19690Sstevel@tonic-gate 19700Sstevel@tonic-gate file_type = elf_kind(elf_file); 19710Sstevel@tonic-gate if (file_type == ELF_K_AR) { 19720Sstevel@tonic-gate if (a_flag || g_flag) { 19730Sstevel@tonic-gate dump_ar_hdr(fd, elf_file, filename); 19740Sstevel@tonic-gate elf_file = elf_begin(fd, cmd, (Elf *)0); 19750Sstevel@tonic-gate } 19760Sstevel@tonic-gate if (z_flag) 19770Sstevel@tonic-gate dump_ar_files(fd, elf_file, filename); 19780Sstevel@tonic-gate } else { 19790Sstevel@tonic-gate if (file_type == ELF_K_ELF) { 19800Sstevel@tonic-gate (void) printf("\n%s:\n", filename); 19811618Srie if (dump_elf_header(elf_file, filename, &elf_head)) { 19821618Srie if (o_flag) 19831618Srie dump_exec_header(elf_file, 19841618Srie (unsigned)elf_head.e_phnum, 19851618Srie filename); 19861618Srie if (x_flag) 19871618Srie dump_section_table(elf_file, 19881618Srie &elf_head, filename); 19890Sstevel@tonic-gate } 19900Sstevel@tonic-gate } else { 19910Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: invalid file type\n", 19921618Srie prog_name, filename); 19930Sstevel@tonic-gate } 19940Sstevel@tonic-gate } 19950Sstevel@tonic-gate (void) elf_end(elf_file); 19960Sstevel@tonic-gate (void) close(fd); 19970Sstevel@tonic-gate } 19980Sstevel@tonic-gate 19990Sstevel@tonic-gate /* 20000Sstevel@tonic-gate * Sets up flags for command line options given and then 20010Sstevel@tonic-gate * calls each_file() to process each file. 20020Sstevel@tonic-gate */ 20030Sstevel@tonic-gate int 20040Sstevel@tonic-gate main(int argc, char *argv[], char *envp[]) 20050Sstevel@tonic-gate { 20060Sstevel@tonic-gate char *optstr = OPTSTR; /* option string used by getopt() */ 20070Sstevel@tonic-gate int optchar; 20080Sstevel@tonic-gate 20090Sstevel@tonic-gate /* 20100Sstevel@tonic-gate * Check for a binary that better fits this architecture. 20110Sstevel@tonic-gate */ 20122647Srie (void) conv_check_native(argv, envp); 20130Sstevel@tonic-gate 20140Sstevel@tonic-gate prog_name = argv[0]; 20150Sstevel@tonic-gate 20160Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 20170Sstevel@tonic-gate while ((optchar = getopt(argc, argv, optstr)) != -1) { 20180Sstevel@tonic-gate switch (optchar) { 20190Sstevel@tonic-gate case 'a': 20200Sstevel@tonic-gate a_flag = 1; 20210Sstevel@tonic-gate x_flag = 1; 20220Sstevel@tonic-gate break; 20230Sstevel@tonic-gate case 'g': 20240Sstevel@tonic-gate g_flag = 1; 20250Sstevel@tonic-gate x_flag = 1; 20260Sstevel@tonic-gate break; 20270Sstevel@tonic-gate case 'v': 20280Sstevel@tonic-gate v_flag = 1; 20290Sstevel@tonic-gate break; 20300Sstevel@tonic-gate case 'p': 20310Sstevel@tonic-gate p_flag = 1; 20320Sstevel@tonic-gate break; 20330Sstevel@tonic-gate case 'f': 20340Sstevel@tonic-gate f_flag = 1; 20350Sstevel@tonic-gate z_flag = 1; 20360Sstevel@tonic-gate break; 20370Sstevel@tonic-gate case 'o': 20380Sstevel@tonic-gate o_flag = 1; 20390Sstevel@tonic-gate z_flag = 1; 20400Sstevel@tonic-gate break; 20410Sstevel@tonic-gate case 'h': 20420Sstevel@tonic-gate h_flag = 1; 20430Sstevel@tonic-gate x_flag = 1; 20440Sstevel@tonic-gate z_flag = 1; 20450Sstevel@tonic-gate break; 20460Sstevel@tonic-gate case 's': 20470Sstevel@tonic-gate s_flag = 1; 20480Sstevel@tonic-gate x_flag = 1; 20490Sstevel@tonic-gate z_flag = 1; 20500Sstevel@tonic-gate break; 20510Sstevel@tonic-gate case 'd': 20520Sstevel@tonic-gate d_flag = 1; 20530Sstevel@tonic-gate x_flag = 1; 20540Sstevel@tonic-gate z_flag = 1; 20550Sstevel@tonic-gate set_range(optarg, &d_low, &d_hi); 20560Sstevel@tonic-gate break; 20570Sstevel@tonic-gate case 'n': 20580Sstevel@tonic-gate n_flag++; 20590Sstevel@tonic-gate x_flag = 1; 20600Sstevel@tonic-gate z_flag = 1; 20610Sstevel@tonic-gate name = optarg; 20620Sstevel@tonic-gate break; 20630Sstevel@tonic-gate case 'r': 20640Sstevel@tonic-gate r_flag = 1; 20650Sstevel@tonic-gate x_flag = 1; 20660Sstevel@tonic-gate z_flag = 1; 20670Sstevel@tonic-gate break; 20680Sstevel@tonic-gate case 't': 20690Sstevel@tonic-gate t_flag = 1; 20700Sstevel@tonic-gate x_flag = 1; 20710Sstevel@tonic-gate z_flag = 1; 20720Sstevel@tonic-gate break; 20730Sstevel@tonic-gate case 'C': 20740Sstevel@tonic-gate C_flag = 1; 20750Sstevel@tonic-gate t_flag = 1; 20760Sstevel@tonic-gate x_flag = 1; 20770Sstevel@tonic-gate z_flag = 1; 20780Sstevel@tonic-gate break; 20790Sstevel@tonic-gate case 'T': 20800Sstevel@tonic-gate T_flag = 1; 20810Sstevel@tonic-gate x_flag = 1; 20820Sstevel@tonic-gate z_flag = 1; 20830Sstevel@tonic-gate set_range(optarg, &T_low, &T_hi); 20840Sstevel@tonic-gate break; 20850Sstevel@tonic-gate case 'c': 20860Sstevel@tonic-gate c_flag = 1; 20870Sstevel@tonic-gate x_flag = 1; 20880Sstevel@tonic-gate z_flag = 1; 20890Sstevel@tonic-gate break; 20900Sstevel@tonic-gate case 'L': 20910Sstevel@tonic-gate L_flag = 1; 20920Sstevel@tonic-gate x_flag = 1; 20930Sstevel@tonic-gate z_flag = 1; 20940Sstevel@tonic-gate break; 20950Sstevel@tonic-gate case 'V': 20960Sstevel@tonic-gate V_flag = 1; 20970Sstevel@tonic-gate (void) fprintf(stderr, "dump: %s %s\n", 20980Sstevel@tonic-gate (const char *)SGU_PKG, 20990Sstevel@tonic-gate (const char *)SGU_REL); 21000Sstevel@tonic-gate break; 21010Sstevel@tonic-gate case '?': 21020Sstevel@tonic-gate errflag += 1; 21030Sstevel@tonic-gate break; 21040Sstevel@tonic-gate default: 21050Sstevel@tonic-gate break; 21060Sstevel@tonic-gate } 21070Sstevel@tonic-gate } 21080Sstevel@tonic-gate 21090Sstevel@tonic-gate if (errflag || (optind >= argc) || (!z_flag && !x_flag)) { 21100Sstevel@tonic-gate if (!(V_flag && (argc == 2))) { 21110Sstevel@tonic-gate usage(); 21120Sstevel@tonic-gate exit(269); 21130Sstevel@tonic-gate } 21140Sstevel@tonic-gate } 21150Sstevel@tonic-gate 21161618Srie if (elf_version(EV_CURRENT) == EV_NONE) { 21171618Srie (void) fprintf(stderr, "%s: libelf is out of date\n", 21181618Srie prog_name); 21191618Srie exit(101); 21201618Srie } 21211618Srie 21220Sstevel@tonic-gate while (optind < argc) { 21230Sstevel@tonic-gate each_file(argv[optind]); 21240Sstevel@tonic-gate optind++; 21250Sstevel@tonic-gate } 21260Sstevel@tonic-gate return (0); 21270Sstevel@tonic-gate } 2128