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 */ 211324Srie 220Sstevel@tonic-gate /* 233466Srie * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * Dump an elf file. 300Sstevel@tonic-gate */ 311618Srie #include <machdep.h> 321618Srie #include <sys/elf_386.h> 331618Srie #include <sys/elf_amd64.h> 341618Srie #include <sys/elf_SPARC.h> 351618Srie #include <dwarf.h> 360Sstevel@tonic-gate #include <unistd.h> 370Sstevel@tonic-gate #include <errno.h> 380Sstevel@tonic-gate #include <strings.h> 390Sstevel@tonic-gate #include <debug.h> 400Sstevel@tonic-gate #include <conv.h> 410Sstevel@tonic-gate #include <msg.h> 421618Srie #include <_elfdump.h> 430Sstevel@tonic-gate 443875Sab196087 453875Sab196087 463875Sab196087 /* 473875Sab196087 * VERSYM_STATE is used to maintain information about the VERSYM section 483875Sab196087 * in the object being analyzed. It is filled in by versions(), and used 493875Sab196087 * by init_symtbl_state() when displaying symbol information. 503875Sab196087 * 514716Sab196087 * max_verndx contains the largest version index that can appear 524716Sab196087 * in a Versym entry. This can never be less than 1: In the case where 534716Sab196087 * there is no verdef/verneed sections, the [0] index is reserved 544716Sab196087 * for local symbols, and the [1] index for globals. If Solaris versioning 554716Sab196087 * rules are in effect and there is a verdef section, then the number 564716Sab196087 * of defined versions provides this number. If GNU versioning is in effect, 574716Sab196087 * then: 584716Sab196087 * - If there is no verneed section, it is the same as for 594716Sab196087 * Solaris versioning. 604716Sab196087 * - If there is a verneed section, the vna_other field of the 614716Sab196087 * Vernaux structs contain versions, and max_verndx is the 624716Sab196087 * largest such index. 634716Sab196087 * 644716Sab196087 * The value of the gnu field is based on the presence of 654716Sab196087 * a DT_VERSYM entry in the dynamic section: GNU ld produces these, and 664716Sab196087 * Solaris ld does not. 673875Sab196087 */ 683875Sab196087 typedef struct { 693875Sab196087 Cache *cache; /* Pointer to cache entry for VERSYM */ 703875Sab196087 Versym *data; /* Pointer to versym array */ 714716Sab196087 int gnu; /* True if object uses GNU versioning rules */ 724716Sab196087 int max_verndx; /* largest versym index value */ 733875Sab196087 } VERSYM_STATE; 743875Sab196087 753875Sab196087 /* 763875Sab196087 * SYMTBL_STATE is used to maintain information about a single symbol 773875Sab196087 * table section, for use by the routines that display symbol information. 783875Sab196087 */ 793875Sab196087 typedef struct { 803875Sab196087 const char *file; /* Name of file */ 813875Sab196087 Ehdr *ehdr; /* ELF header for file */ 823875Sab196087 Cache *cache; /* Cache of all section headers */ 833875Sab196087 Word shnum; /* # of sections in cache */ 843875Sab196087 Cache *seccache; /* Cache of symbol table section hdr */ 853875Sab196087 Word secndx; /* Index of symbol table section hdr */ 863875Sab196087 const char *secname; /* Name of section */ 873875Sab196087 uint_t flags; /* Command line option flags */ 883875Sab196087 struct { /* Extended section index data */ 893875Sab196087 int checked; /* TRUE if already checked for shxndx */ 903875Sab196087 Word *data; /* NULL, or extended section index */ 913875Sab196087 /* used for symbol table entries */ 923875Sab196087 uint_t n; /* # items in shxndx.data */ 933875Sab196087 } shxndx; 943875Sab196087 VERSYM_STATE *versym; /* NULL, or associated VERSYM section */ 953875Sab196087 Sym *sym; /* Array of symbols */ 963875Sab196087 Word symn; /* # of symbols */ 973875Sab196087 } SYMTBL_STATE; 983875Sab196087 993875Sab196087 1003875Sab196087 1010Sstevel@tonic-gate /* 1020Sstevel@tonic-gate * Focal point for verifying symbol names. 1030Sstevel@tonic-gate */ 1040Sstevel@tonic-gate static const char * 1051618Srie string(Cache *refsec, Word ndx, Cache *strsec, const char *file, Word name) 1060Sstevel@tonic-gate { 1074063Sab196087 /* 1084063Sab196087 * If an error in this routine is due to a property of the string 1094063Sab196087 * section, as opposed to a bad offset into the section (a property of 1104063Sab196087 * the referencing section), then we will detect the same error on 1114063Sab196087 * every call involving those sections. We use these static variables 1124063Sab196087 * to retain the information needed to only issue each such error once. 1134063Sab196087 */ 1144063Sab196087 static Cache *last_refsec; /* Last referencing section seen */ 1154063Sab196087 static int strsec_err; /* True if error issued */ 1164063Sab196087 1173492Sab196087 const char *strs; 1183492Sab196087 Word strn; 1190Sstevel@tonic-gate 1203466Srie if (strsec->c_data == NULL) 1213466Srie return (NULL); 1223466Srie 1233492Sab196087 strs = (char *)strsec->c_data->d_buf; 1243492Sab196087 strn = strsec->c_data->d_size; 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate /* 1274063Sab196087 * We only print a diagnostic regarding a bad string table once per 1284063Sab196087 * input section being processed. If the refsec has changed, reset 1294063Sab196087 * our retained error state. 1300Sstevel@tonic-gate */ 1314063Sab196087 if (last_refsec != refsec) { 1324063Sab196087 last_refsec = refsec; 1334063Sab196087 strsec_err = 0; 1344063Sab196087 } 1354063Sab196087 1364063Sab196087 /* Verify that strsec really is a string table */ 1374063Sab196087 if (strsec->c_shdr->sh_type != SHT_STRTAB) { 1384063Sab196087 if (!strsec_err) { 1394063Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_NOTSTRTAB), 1404063Sab196087 file, strsec->c_ndx, refsec->c_ndx); 1414063Sab196087 strsec_err = 1; 1424063Sab196087 } 1434063Sab196087 return (MSG_INTL(MSG_STR_UNKNOWN)); 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate /* 1470Sstevel@tonic-gate * Is the string table offset within range of the available strings? 1480Sstevel@tonic-gate */ 1490Sstevel@tonic-gate if (name >= strn) { 1500Sstevel@tonic-gate /* 1510Sstevel@tonic-gate * Do we have a empty string table? 1520Sstevel@tonic-gate */ 1530Sstevel@tonic-gate if (strs == 0) { 1544063Sab196087 if (!strsec_err) { 1550Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 1560Sstevel@tonic-gate file, strsec->c_name); 1574063Sab196087 strsec_err = 1; 1580Sstevel@tonic-gate } 1590Sstevel@tonic-gate } else { 1600Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSTOFF), 1611618Srie file, refsec->c_name, EC_WORD(ndx), strsec->c_name, 1621618Srie EC_WORD(name), EC_WORD(strn - 1)); 1630Sstevel@tonic-gate } 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate /* 1660Sstevel@tonic-gate * Return the empty string so that the calling function can 1670Sstevel@tonic-gate * continue it's output diagnostics. 1680Sstevel@tonic-gate */ 1690Sstevel@tonic-gate return (MSG_INTL(MSG_STR_UNKNOWN)); 1700Sstevel@tonic-gate } 1710Sstevel@tonic-gate return (strs + name); 1720Sstevel@tonic-gate } 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate /* 1751618Srie * Relocations can reference section symbols and standard symbols. If the 1761618Srie * former, establish the section name. 1771618Srie */ 1781618Srie static const char * 1791618Srie relsymname(Cache *cache, Cache *csec, Cache *strsec, Word symndx, Word symnum, 1801618Srie Word relndx, Sym *syms, char *secstr, size_t secsz, const char *file, 1811618Srie uint_t flags) 1821618Srie { 1831618Srie Sym *sym; 1841618Srie 1851618Srie if (symndx >= symnum) { 1861618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_RELBADSYMNDX), 1871618Srie file, EC_WORD(symndx), EC_WORD(relndx)); 1881618Srie return (MSG_INTL(MSG_STR_UNKNOWN)); 1891618Srie } 1901618Srie 1911618Srie sym = (Sym *)(syms + symndx); 1921618Srie 1931618Srie /* 1941618Srie * If the symbol represents a section offset construct an appropriate 1951618Srie * string. 1961618Srie */ 1971618Srie if ((ELF_ST_TYPE(sym->st_info) == STT_SECTION) && (sym->st_name == 0)) { 198*5411Sab196087 if (flags & FLG_CTL_LONGNAME) 1991618Srie (void) snprintf(secstr, secsz, 2001618Srie MSG_INTL(MSG_STR_L_SECTION), 2011618Srie cache[sym->st_shndx].c_name); 2021618Srie else 2031618Srie (void) snprintf(secstr, secsz, 2041618Srie MSG_INTL(MSG_STR_SECTION), 2051618Srie cache[sym->st_shndx].c_name); 2061618Srie return ((const char *)secstr); 2071618Srie } 2081618Srie 2091618Srie return (string(csec, symndx, strsec, file, sym->st_name)); 2101618Srie } 2111618Srie 2121618Srie /* 2131618Srie * Focal point for establishing a string table section. Data such as the 2141618Srie * dynamic information simply points to a string table. Data such as 2151618Srie * relocations, reference a symbol table, which in turn is associated with a 2161618Srie * string table. 2170Sstevel@tonic-gate */ 2180Sstevel@tonic-gate static int 2191618Srie stringtbl(Cache *cache, int symtab, Word ndx, Word shnum, const char *file, 2201618Srie Word *symnum, Cache **symsec, Cache **strsec) 2211618Srie { 2221618Srie Shdr *shdr = cache[ndx].c_shdr; 2231618Srie 2241618Srie if (symtab) { 2251618Srie /* 2261618Srie * Validate the symbol table section. 2271618Srie */ 2281618Srie if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) { 2291618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK), 2301618Srie file, cache[ndx].c_name, EC_WORD(shdr->sh_link)); 2311618Srie return (0); 2321618Srie } 2333466Srie if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) { 2343466Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 2353466Srie file, cache[ndx].c_name); 2363466Srie return (0); 2373466Srie } 2381618Srie 2391618Srie /* 2401618Srie * Obtain, and verify the symbol table data. 2411618Srie */ 2423466Srie if ((cache[ndx].c_data == NULL) || 2433466Srie (cache[ndx].c_data->d_buf == NULL)) { 2441618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 2451618Srie file, cache[ndx].c_name); 2461618Srie return (0); 2471618Srie } 2481618Srie 2491618Srie /* 2501618Srie * Establish the string table index. 2511618Srie */ 2521618Srie ndx = shdr->sh_link; 2531618Srie shdr = cache[ndx].c_shdr; 2541618Srie 2551618Srie /* 2561618Srie * Return symbol table information. 2571618Srie */ 2581618Srie if (symnum) 2591618Srie *symnum = (shdr->sh_size / shdr->sh_entsize); 2601618Srie if (symsec) 2611618Srie *symsec = &cache[ndx]; 2621618Srie } 2631618Srie 2641618Srie /* 2651618Srie * Validate the associated string table section. 2661618Srie */ 2671618Srie if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) { 2681618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK), 2691618Srie file, cache[ndx].c_name, EC_WORD(shdr->sh_link)); 2701618Srie return (0); 2711618Srie } 2721618Srie 2731618Srie if (strsec) 2741618Srie *strsec = &cache[shdr->sh_link]; 2751618Srie 2761618Srie return (1); 2771618Srie } 2781618Srie 2791618Srie /* 2801618Srie * Lookup a symbol and set Sym accordingly. 2811618Srie */ 2821618Srie static int 2831618Srie symlookup(const char *name, Cache *cache, Word shnum, Sym **sym, 2840Sstevel@tonic-gate Cache *symtab, const char *file) 2850Sstevel@tonic-gate { 2861618Srie Shdr *shdr; 2871618Srie Word symn, cnt; 2881618Srie Sym *syms; 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate if (symtab == 0) 2910Sstevel@tonic-gate return (0); 2920Sstevel@tonic-gate 2931618Srie shdr = symtab->c_shdr; 2941618Srie 2950Sstevel@tonic-gate /* 2960Sstevel@tonic-gate * Determine the symbol data and number. 2970Sstevel@tonic-gate */ 2980Sstevel@tonic-gate if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) { 2990Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 3000Sstevel@tonic-gate file, symtab->c_name); 3010Sstevel@tonic-gate return (0); 3020Sstevel@tonic-gate } 3033466Srie if (symtab->c_data == NULL) 3043466Srie return (0); 3053466Srie 3060Sstevel@tonic-gate /* LINTED */ 3071618Srie symn = (Word)(shdr->sh_size / shdr->sh_entsize); 3081618Srie syms = (Sym *)symtab->c_data->d_buf; 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate /* 3110Sstevel@tonic-gate * Get the associated string table section. 3120Sstevel@tonic-gate */ 3130Sstevel@tonic-gate if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) { 3140Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK), 3151618Srie file, symtab->c_name, EC_WORD(shdr->sh_link)); 3160Sstevel@tonic-gate return (0); 3170Sstevel@tonic-gate } 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate /* 3200Sstevel@tonic-gate * Loop through the symbol table to find a match. 3210Sstevel@tonic-gate */ 3221618Srie for (cnt = 0; cnt < symn; syms++, cnt++) { 3231618Srie const char *symname; 3240Sstevel@tonic-gate 3251618Srie symname = string(symtab, cnt, &cache[shdr->sh_link], file, 3261618Srie syms->st_name); 3270Sstevel@tonic-gate 3281618Srie if (symname && (strcmp(name, symname) == 0)) { 3291618Srie *sym = syms; 3300Sstevel@tonic-gate return (1); 3310Sstevel@tonic-gate } 3320Sstevel@tonic-gate } 3330Sstevel@tonic-gate return (0); 3340Sstevel@tonic-gate } 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate /* 3370Sstevel@tonic-gate * Print section headers. 3380Sstevel@tonic-gate */ 3390Sstevel@tonic-gate static void 3404168Sab196087 sections(const char *file, Cache *cache, Word shnum, Ehdr *ehdr) 3410Sstevel@tonic-gate { 3421618Srie size_t seccnt; 3430Sstevel@tonic-gate 3441618Srie for (seccnt = 1; seccnt < shnum; seccnt++) { 3451618Srie Cache *_cache = &cache[seccnt]; 3461618Srie Shdr *shdr = _cache->c_shdr; 3471618Srie const char *secname = _cache->c_name; 3480Sstevel@tonic-gate 3490Sstevel@tonic-gate /* 3500Sstevel@tonic-gate * Although numerous section header entries can be zero, it's 3513862Srie * usually a sign of trouble if the type is zero. 3520Sstevel@tonic-gate */ 3530Sstevel@tonic-gate if (shdr->sh_type == 0) { 3540Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHTYPE), 3551618Srie file, secname, EC_WORD(shdr->sh_type)); 3560Sstevel@tonic-gate } 3573862Srie 358*5411Sab196087 if (!match(MATCH_F_ALL, secname, seccnt, shdr->sh_type)) 3593862Srie continue; 3600Sstevel@tonic-gate 3611324Srie /* 3621324Srie * Identify any sections that are suspicious. A .got section 3631324Srie * shouldn't exist in a relocatable object. 3641324Srie */ 3651324Srie if (ehdr->e_type == ET_REL) { 3661618Srie if (strncmp(secname, MSG_ORIG(MSG_ELF_GOT), 3671324Srie MSG_ELF_GOT_SIZE) == 0) { 3681324Srie (void) fprintf(stderr, 3691618Srie MSG_INTL(MSG_GOT_UNEXPECTED), file, 3701618Srie secname); 3711324Srie } 3721324Srie } 3731324Srie 3741618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 3751618Srie dbg_print(0, MSG_INTL(MSG_ELF_SHDR), EC_WORD(seccnt), secname); 3761618Srie Elf_shdr(0, ehdr->e_machine, shdr); 3770Sstevel@tonic-gate } 3780Sstevel@tonic-gate } 3790Sstevel@tonic-gate 3801618Srie /* 3811618Srie * A couple of instances of unwind data are printed as tables of 8 data items 3821618Srie * expressed as 0x?? integers. 3831618Srie */ 3841618Srie #define UNWINDTBLSZ 10 + (8 * 5) + 1 3851618Srie 3861618Srie static void 3871618Srie unwindtbl(uint64_t *ndx, uint_t len, uchar_t *data, uint64_t doff, 3881618Srie const char *msg, const char *pre, size_t plen) 3891618Srie { 3901618Srie char buffer[UNWINDTBLSZ]; 3911618Srie uint_t boff = plen, cnt = 0; 3921618Srie 3931618Srie dbg_print(0, msg); 3941618Srie (void) strncpy(buffer, pre, UNWINDTBLSZ); 3951618Srie 3961618Srie while (*ndx < (len + 4)) { 3971618Srie if (cnt == 8) { 3981618Srie dbg_print(0, buffer); 3991618Srie boff = plen; 4001618Srie cnt = 0; 4011618Srie } 4021618Srie (void) snprintf(&buffer[boff], UNWINDTBLSZ - boff, 4031618Srie MSG_ORIG(MSG_UNW_TBLENTRY), data[doff + (*ndx)++]); 4041618Srie boff += 5; 4051618Srie cnt++; 4061618Srie } 4071618Srie if (cnt) 4081618Srie dbg_print(0, buffer); 4091618Srie } 4101618Srie 4111618Srie /* 4121618Srie * Obtain a specified Phdr entry. 4131618Srie */ 4141618Srie static Phdr * 4151618Srie getphdr(Word phnum, Word type, const char *file, Elf *elf) 4161618Srie { 4171618Srie Word cnt; 4181618Srie Phdr *phdr; 4191618Srie 4201618Srie if ((phdr = elf_getphdr(elf)) == NULL) { 4211618Srie failure(file, MSG_ORIG(MSG_ELF_GETPHDR)); 4221618Srie return (0); 4231618Srie } 4241618Srie 4251618Srie for (cnt = 0; cnt < phnum; phdr++, cnt++) { 4261618Srie if (phdr->p_type == type) 4271618Srie return (phdr); 4281618Srie } 4291618Srie return (0); 4301618Srie } 4311618Srie 4320Sstevel@tonic-gate static void 4334168Sab196087 unwind(Cache *cache, Word shnum, Word phnum, Ehdr *ehdr, const char *file, 4344168Sab196087 Elf *elf) 4350Sstevel@tonic-gate { 4364734Sab196087 Conv_dwarf_ehe_buf_t dwarf_ehe_buf; 4371618Srie Word cnt; 4381618Srie Phdr *uphdr = 0; 4391618Srie 4400Sstevel@tonic-gate /* 4411618Srie * For the moment - UNWIND is only relevant for a AMD64 object. 4420Sstevel@tonic-gate */ 4430Sstevel@tonic-gate if (ehdr->e_machine != EM_AMD64) 4441618Srie return; 4450Sstevel@tonic-gate 4461618Srie if (phnum) 4471618Srie uphdr = getphdr(phnum, PT_SUNW_UNWIND, file, elf); 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 4501618Srie Cache *_cache = &cache[cnt]; 4511618Srie Shdr *shdr = _cache->c_shdr; 4521618Srie uchar_t *data; 4530Sstevel@tonic-gate size_t datasize; 4541618Srie uint64_t off, ndx, frame_ptr, fde_cnt, tabndx; 4551618Srie uint_t vers, frame_ptr_enc, fde_cnt_enc, table_enc; 4560Sstevel@tonic-gate 4570Sstevel@tonic-gate /* 4581618Srie * AMD64 - this is a strmcp() just to find the gcc produced 4591618Srie * sections. Soon gcc should be setting the section type - and 4601618Srie * we'll not need this strcmp(). 4610Sstevel@tonic-gate */ 4620Sstevel@tonic-gate if ((shdr->sh_type != SHT_AMD64_UNWIND) && 4630Sstevel@tonic-gate (strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRM), 4640Sstevel@tonic-gate MSG_SCN_FRM_SIZE) != 0) && 4650Sstevel@tonic-gate (strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRMHDR), 4660Sstevel@tonic-gate MSG_SCN_FRMHDR_SIZE) != 0)) 4670Sstevel@tonic-gate continue; 4684168Sab196087 469*5411Sab196087 if (!match(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type)) 4700Sstevel@tonic-gate continue; 4710Sstevel@tonic-gate 4723466Srie if (_cache->c_data == NULL) 4733466Srie continue; 4743466Srie 4751618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 4761618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_UNWIND), _cache->c_name); 4770Sstevel@tonic-gate 4781618Srie data = (uchar_t *)(_cache->c_data->d_buf); 4790Sstevel@tonic-gate datasize = _cache->c_data->d_size; 4800Sstevel@tonic-gate off = 0; 4810Sstevel@tonic-gate 4820Sstevel@tonic-gate /* 4830Sstevel@tonic-gate * Is this a .eh_frame_hdr 4840Sstevel@tonic-gate */ 4851618Srie if ((uphdr && (shdr->sh_addr == uphdr->p_vaddr)) || 4860Sstevel@tonic-gate (strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRMHDR), 4871618Srie MSG_SCN_FRMHDR_SIZE) == 0)) { 4881618Srie dbg_print(0, MSG_ORIG(MSG_UNW_FRMHDR)); 4891618Srie ndx = 0; 4900Sstevel@tonic-gate 4911618Srie vers = data[ndx++]; 4921618Srie frame_ptr_enc = data[ndx++]; 4931618Srie fde_cnt_enc = data[ndx++]; 4941618Srie table_enc = data[ndx++]; 4950Sstevel@tonic-gate 4961618Srie dbg_print(0, MSG_ORIG(MSG_UNW_FRMVERS), vers); 4970Sstevel@tonic-gate 4981618Srie frame_ptr = dwarf_ehe_extract(data, &ndx, frame_ptr_enc, 4991618Srie ehdr->e_ident, shdr->sh_addr + ndx); 5000Sstevel@tonic-gate 5011618Srie dbg_print(0, MSG_ORIG(MSG_UNW_FRPTRENC), 5024734Sab196087 conv_dwarf_ehe(frame_ptr_enc, &dwarf_ehe_buf), 5034734Sab196087 EC_XWORD(frame_ptr)); 5041618Srie 5051618Srie fde_cnt = dwarf_ehe_extract(data, &ndx, fde_cnt_enc, 5061618Srie ehdr->e_ident, shdr->sh_addr + ndx); 5070Sstevel@tonic-gate 5081618Srie dbg_print(0, MSG_ORIG(MSG_UNW_FDCNENC), 5094734Sab196087 conv_dwarf_ehe(fde_cnt_enc, &dwarf_ehe_buf), 5104734Sab196087 EC_XWORD(fde_cnt)); 5111618Srie dbg_print(0, MSG_ORIG(MSG_UNW_TABENC), 5124734Sab196087 conv_dwarf_ehe(table_enc, &dwarf_ehe_buf)); 5131618Srie dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTAB1)); 5141618Srie dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTAB2)); 5150Sstevel@tonic-gate 5161618Srie for (tabndx = 0; tabndx < fde_cnt; tabndx++) { 5171618Srie dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTABENT), 5181618Srie EC_XWORD(dwarf_ehe_extract(data, &ndx, 5191618Srie table_enc, ehdr->e_ident, shdr->sh_addr)), 5201618Srie EC_XWORD(dwarf_ehe_extract(data, &ndx, 5211618Srie table_enc, ehdr->e_ident, shdr->sh_addr))); 5221618Srie } 5231618Srie continue; 5240Sstevel@tonic-gate } 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate /* 5270Sstevel@tonic-gate * Walk the Eh_frame's 5280Sstevel@tonic-gate */ 5290Sstevel@tonic-gate while (off < datasize) { 5304433Sab196087 uint_t cieid, cielength, cieversion; 5314433Sab196087 uint_t cieretaddr; 5321618Srie int cieRflag, cieLflag, ciePflag, cieZflag; 5331618Srie uint_t cieaugndx, length, id; 5340Sstevel@tonic-gate uint64_t ciecalign, ciedalign; 5350Sstevel@tonic-gate char *cieaugstr; 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate ndx = 0; 5380Sstevel@tonic-gate /* 5390Sstevel@tonic-gate * extract length in lsb format 5400Sstevel@tonic-gate */ 5410Sstevel@tonic-gate length = LSB32EXTRACT(data + off + ndx); 5420Sstevel@tonic-gate ndx += 4; 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate /* 5450Sstevel@tonic-gate * extract CIE id in lsb format 5460Sstevel@tonic-gate */ 5470Sstevel@tonic-gate id = LSB32EXTRACT(data + off + ndx); 5480Sstevel@tonic-gate ndx += 4; 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate /* 5511618Srie * A CIE record has a id of '0', otherwise this is a 5521618Srie * FDE entry and the 'id' is the CIE pointer. 5530Sstevel@tonic-gate */ 5540Sstevel@tonic-gate if (id == 0) { 5550Sstevel@tonic-gate uint64_t persVal; 5561618Srie 5570Sstevel@tonic-gate cielength = length; 5580Sstevel@tonic-gate cieid = id; 5591618Srie cieLflag = ciePflag = cieRflag = cieZflag = 0; 5600Sstevel@tonic-gate 5611618Srie dbg_print(0, MSG_ORIG(MSG_UNW_CIE), 5621618Srie EC_XWORD(shdr->sh_addr + off)); 5631618Srie dbg_print(0, MSG_ORIG(MSG_UNW_CIELNGTH), 5641618Srie cielength, cieid); 5650Sstevel@tonic-gate 5660Sstevel@tonic-gate cieversion = data[off + ndx]; 5670Sstevel@tonic-gate ndx += 1; 5680Sstevel@tonic-gate cieaugstr = (char *)(&data[off + ndx]); 5690Sstevel@tonic-gate ndx += strlen(cieaugstr) + 1; 5701618Srie 5711618Srie dbg_print(0, MSG_ORIG(MSG_UNW_CIEVERS), 5721618Srie cieversion, cieaugstr); 5731618Srie 5740Sstevel@tonic-gate ciecalign = uleb_extract(&data[off], &ndx); 5750Sstevel@tonic-gate ciedalign = sleb_extract(&data[off], &ndx); 5760Sstevel@tonic-gate cieretaddr = data[off + ndx]; 5770Sstevel@tonic-gate ndx += 1; 5781618Srie 5791618Srie dbg_print(0, MSG_ORIG(MSG_UNW_CIECALGN), 5801618Srie EC_XWORD(ciecalign), EC_XWORD(ciedalign), 5811618Srie cieretaddr); 5820Sstevel@tonic-gate 5830Sstevel@tonic-gate if (cieaugstr[0]) 5844433Sab196087 dbg_print(0, 5854433Sab196087 MSG_ORIG(MSG_UNW_CIEAXVAL)); 5861618Srie 5870Sstevel@tonic-gate for (cieaugndx = 0; cieaugstr[cieaugndx]; 5880Sstevel@tonic-gate cieaugndx++) { 5890Sstevel@tonic-gate uint_t val; 5901618Srie 5910Sstevel@tonic-gate switch (cieaugstr[cieaugndx]) { 5920Sstevel@tonic-gate case 'z': 5934433Sab196087 val = uleb_extract(&data[off], 5944433Sab196087 &ndx); 5954433Sab196087 dbg_print(0, 5964433Sab196087 MSG_ORIG(MSG_UNW_CIEAXSIZ), 5974433Sab196087 val); 5984433Sab196087 cieZflag = 1; 5994433Sab196087 break; 6000Sstevel@tonic-gate case 'P': 6014433Sab196087 ciePflag = data[off + ndx]; 6024433Sab196087 ndx += 1; 6034433Sab196087 6044433Sab196087 persVal = dwarf_ehe_extract( 6054433Sab196087 &data[off], &ndx, ciePflag, 6064433Sab196087 ehdr->e_ident, 6074433Sab196087 shdr->sh_addr + off + ndx); 6084433Sab196087 dbg_print(0, 6094433Sab196087 MSG_ORIG(MSG_UNW_CIEAXPERS), 6104433Sab196087 ciePflag, 6114734Sab196087 conv_dwarf_ehe(ciePflag, 6124734Sab196087 &dwarf_ehe_buf), 6134433Sab196087 EC_XWORD(persVal)); 6144433Sab196087 break; 6150Sstevel@tonic-gate case 'R': 6164433Sab196087 val = data[off + ndx]; 6174433Sab196087 ndx += 1; 6184433Sab196087 dbg_print(0, 6194433Sab196087 MSG_ORIG(MSG_UNW_CIEAXCENC), 6204734Sab196087 val, conv_dwarf_ehe(val, 6214734Sab196087 &dwarf_ehe_buf)); 6224433Sab196087 cieRflag = val; 6234433Sab196087 break; 6240Sstevel@tonic-gate case 'L': 6254433Sab196087 val = data[off + ndx]; 6264433Sab196087 ndx += 1; 6274433Sab196087 dbg_print(0, 6284433Sab196087 MSG_ORIG(MSG_UNW_CIEAXLSDA), 6294734Sab196087 val, conv_dwarf_ehe(val, 6304734Sab196087 &dwarf_ehe_buf)); 6314433Sab196087 cieLflag = val; 6324433Sab196087 break; 6330Sstevel@tonic-gate default: 6344433Sab196087 dbg_print(0, 6354433Sab196087 MSG_ORIG(MSG_UNW_CIEAXUNEC), 6364433Sab196087 cieaugstr[cieaugndx]); 6374433Sab196087 break; 6380Sstevel@tonic-gate } 6390Sstevel@tonic-gate } 6401618Srie if ((cielength + 4) > ndx) 6411618Srie unwindtbl(&ndx, cielength, data, off, 6421618Srie MSG_ORIG(MSG_UNW_CIECFI), 6431618Srie MSG_ORIG(MSG_UNW_CIEPRE), 6441618Srie MSG_UNW_CIEPRE_SIZE); 6450Sstevel@tonic-gate off += cielength + 4; 6461618Srie 6470Sstevel@tonic-gate } else { 6480Sstevel@tonic-gate uint_t fdelength = length; 6490Sstevel@tonic-gate int fdecieptr = id; 6500Sstevel@tonic-gate uint64_t fdeinitloc, fdeaddrrange; 6510Sstevel@tonic-gate 6521618Srie dbg_print(0, MSG_ORIG(MSG_UNW_FDE), 6531618Srie EC_XWORD(shdr->sh_addr + off)); 6541618Srie dbg_print(0, MSG_ORIG(MSG_UNW_FDELNGTH), 6550Sstevel@tonic-gate fdelength, fdecieptr); 6561618Srie 6570Sstevel@tonic-gate fdeinitloc = dwarf_ehe_extract(&data[off], 6580Sstevel@tonic-gate &ndx, cieRflag, ehdr->e_ident, 6590Sstevel@tonic-gate shdr->sh_addr + off + ndx); 6600Sstevel@tonic-gate fdeaddrrange = dwarf_ehe_extract(&data[off], 6610Sstevel@tonic-gate &ndx, (cieRflag & ~DW_EH_PE_pcrel), 6620Sstevel@tonic-gate ehdr->e_ident, 6630Sstevel@tonic-gate shdr->sh_addr + off + ndx); 6641618Srie 6651618Srie dbg_print(0, MSG_ORIG(MSG_UNW_FDEINITLOC), 6661618Srie EC_XWORD(fdeinitloc), 6671618Srie EC_XWORD(fdeaddrrange)); 6681618Srie 6690Sstevel@tonic-gate if (cieaugstr[0]) 6701618Srie dbg_print(0, 6714433Sab196087 MSG_ORIG(MSG_UNW_FDEAXVAL)); 6720Sstevel@tonic-gate if (cieZflag) { 6730Sstevel@tonic-gate uint64_t val; 6740Sstevel@tonic-gate val = uleb_extract(&data[off], &ndx); 6751618Srie dbg_print(0, 6764433Sab196087 MSG_ORIG(MSG_UNW_FDEAXSIZE), 6771618Srie EC_XWORD(val)); 6780Sstevel@tonic-gate if (val & cieLflag) { 6794433Sab196087 fdeinitloc = dwarf_ehe_extract( 6804433Sab196087 &data[off], &ndx, cieLflag, 6814433Sab196087 ehdr->e_ident, 6824433Sab196087 shdr->sh_addr + off + ndx); 6834433Sab196087 dbg_print(0, 6844433Sab196087 MSG_ORIG(MSG_UNW_FDEAXLSDA), 6854433Sab196087 EC_XWORD(val)); 6860Sstevel@tonic-gate } 6870Sstevel@tonic-gate } 6881618Srie if ((fdelength + 4) > ndx) 6891618Srie unwindtbl(&ndx, fdelength, data, off, 6901618Srie MSG_ORIG(MSG_UNW_FDECFI), 6911618Srie MSG_ORIG(MSG_UNW_FDEPRE), 6921618Srie MSG_UNW_FDEPRE_SIZE); 6930Sstevel@tonic-gate off += fdelength + 4; 6940Sstevel@tonic-gate } 6950Sstevel@tonic-gate } 6960Sstevel@tonic-gate } 6970Sstevel@tonic-gate } 6980Sstevel@tonic-gate 6990Sstevel@tonic-gate /* 7000Sstevel@tonic-gate * Print the hardware/software capabilities. For executables and shared objects 7010Sstevel@tonic-gate * this should be accompanied with a program header. 7020Sstevel@tonic-gate */ 7030Sstevel@tonic-gate static void 7041618Srie cap(const char *file, Cache *cache, Word shnum, Word phnum, Ehdr *ehdr, 7051618Srie Elf *elf) 7060Sstevel@tonic-gate { 7071618Srie Word cnt; 7083466Srie Shdr *cshdr = 0; 7093466Srie Cache *ccache; 7101618Srie Off cphdr_off = 0; 7111618Srie Xword cphdr_sz; 7120Sstevel@tonic-gate 7130Sstevel@tonic-gate /* 7140Sstevel@tonic-gate * Determine if a hardware/software capabilities header exists. 7150Sstevel@tonic-gate */ 7161618Srie if (phnum) { 7171618Srie Phdr *phdr; 7180Sstevel@tonic-gate 7191618Srie if ((phdr = elf_getphdr(elf)) == NULL) { 7200Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETPHDR)); 7210Sstevel@tonic-gate return; 7220Sstevel@tonic-gate } 7230Sstevel@tonic-gate 7241618Srie for (cnt = 0; cnt < phnum; phdr++, cnt++) { 7251618Srie if (phdr->p_type == PT_SUNWCAP) { 7261618Srie cphdr_off = phdr->p_offset; 7271618Srie cphdr_sz = phdr->p_filesz; 7281618Srie break; 7291618Srie } 7300Sstevel@tonic-gate } 7310Sstevel@tonic-gate } 7320Sstevel@tonic-gate 7330Sstevel@tonic-gate /* 7340Sstevel@tonic-gate * Determine if a hardware/software capabilities section exists. 7350Sstevel@tonic-gate */ 7360Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 7371618Srie Cache *_cache = &cache[cnt]; 7381618Srie Shdr *shdr = _cache->c_shdr; 7390Sstevel@tonic-gate 7400Sstevel@tonic-gate if (shdr->sh_type != SHT_SUNW_cap) 7410Sstevel@tonic-gate continue; 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate if (cphdr_off && ((cphdr_off < shdr->sh_offset) || 7440Sstevel@tonic-gate (cphdr_off + cphdr_sz) > (shdr->sh_offset + shdr->sh_size))) 7450Sstevel@tonic-gate continue; 7460Sstevel@tonic-gate 7473466Srie if (_cache->c_data == NULL) 7483466Srie continue; 7493466Srie 7500Sstevel@tonic-gate ccache = _cache; 7510Sstevel@tonic-gate cshdr = shdr; 7520Sstevel@tonic-gate break; 7530Sstevel@tonic-gate } 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate if ((cshdr == 0) && (cphdr_off == 0)) 7560Sstevel@tonic-gate return; 7570Sstevel@tonic-gate 7580Sstevel@tonic-gate /* 7590Sstevel@tonic-gate * Print the hardware/software capabilities section. 7600Sstevel@tonic-gate */ 7610Sstevel@tonic-gate if (cshdr) { 7621618Srie Word ndx, capn; 7633492Sab196087 Cap *cap = (Cap *)ccache->c_data->d_buf; 7640Sstevel@tonic-gate 7654665Sab196087 if ((cshdr->sh_entsize == 0) || (cshdr->sh_size == 0)) { 7664665Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 7674665Sab196087 file, ccache->c_name); 7684665Sab196087 return; 7694665Sab196087 } 7704665Sab196087 7711618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 7721618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_CAP), ccache->c_name); 7730Sstevel@tonic-gate 7741618Srie Elf_cap_title(0); 7751618Srie 7761618Srie capn = (Word)(cshdr->sh_size / cshdr->sh_entsize); 7770Sstevel@tonic-gate 7781618Srie for (ndx = 0; ndx < capn; cap++, ndx++) { 7791618Srie if (cap->c_tag != CA_SUNW_NULL) 7801618Srie Elf_cap_entry(0, cap, ndx, ehdr->e_machine); 7810Sstevel@tonic-gate } 7820Sstevel@tonic-gate } else 7830Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP1), file); 7840Sstevel@tonic-gate 7850Sstevel@tonic-gate /* 7860Sstevel@tonic-gate * If this object is an executable or shared object, then the 7870Sstevel@tonic-gate * hardware/software capabilities section should have an accompanying 7880Sstevel@tonic-gate * program header. 7890Sstevel@tonic-gate */ 7900Sstevel@tonic-gate if (cshdr && ((ehdr->e_type == ET_EXEC) || (ehdr->e_type == ET_DYN))) { 7910Sstevel@tonic-gate if (cphdr_off == 0) 7920Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP2), 7930Sstevel@tonic-gate file, ccache->c_name); 7940Sstevel@tonic-gate else if ((cphdr_off != cshdr->sh_offset) || 7950Sstevel@tonic-gate (cphdr_sz != cshdr->sh_size)) 7960Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP3), 7970Sstevel@tonic-gate file, ccache->c_name); 7980Sstevel@tonic-gate } 7990Sstevel@tonic-gate } 8000Sstevel@tonic-gate 8010Sstevel@tonic-gate /* 8020Sstevel@tonic-gate * Print the interpretor. 8030Sstevel@tonic-gate */ 8040Sstevel@tonic-gate static void 8051618Srie interp(const char *file, Cache *cache, Word shnum, Word phnum, Elf *elf) 8060Sstevel@tonic-gate { 8071618Srie Word cnt; 8081618Srie Shdr *ishdr = 0; 8091618Srie Cache *icache; 8101618Srie Off iphdr_off = 0; 8111618Srie Xword iphdr_fsz; 8120Sstevel@tonic-gate 8130Sstevel@tonic-gate /* 8140Sstevel@tonic-gate * Determine if an interp header exists. 8150Sstevel@tonic-gate */ 8161618Srie if (phnum) { 8171618Srie Phdr *phdr; 8180Sstevel@tonic-gate 8191618Srie if ((phdr = getphdr(phnum, PT_INTERP, file, elf)) != 0) { 8201618Srie iphdr_off = phdr->p_offset; 8211618Srie iphdr_fsz = phdr->p_filesz; 8220Sstevel@tonic-gate } 8230Sstevel@tonic-gate } 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate if (iphdr_off == 0) 8260Sstevel@tonic-gate return; 8270Sstevel@tonic-gate 8280Sstevel@tonic-gate /* 8290Sstevel@tonic-gate * Determine if an interp section exists. 8300Sstevel@tonic-gate */ 8310Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 8321618Srie Cache *_cache = &cache[cnt]; 8331618Srie Shdr *shdr = _cache->c_shdr; 8340Sstevel@tonic-gate 8350Sstevel@tonic-gate /* 8360Sstevel@tonic-gate * Scan sections to find a section which contains the PT_INTERP 8370Sstevel@tonic-gate * string. The target section can't be in a NOBITS section. 8380Sstevel@tonic-gate */ 8390Sstevel@tonic-gate if ((shdr->sh_type == SHT_NOBITS) || 8400Sstevel@tonic-gate (iphdr_off < shdr->sh_offset) || 8411618Srie (iphdr_off + iphdr_fsz) > (shdr->sh_offset + shdr->sh_size)) 8420Sstevel@tonic-gate continue; 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate icache = _cache; 8450Sstevel@tonic-gate ishdr = shdr; 8460Sstevel@tonic-gate break; 8470Sstevel@tonic-gate } 8480Sstevel@tonic-gate 8490Sstevel@tonic-gate /* 8500Sstevel@tonic-gate * Print the interpreter string based on the offset defined in the 8510Sstevel@tonic-gate * program header, as this is the offset used by the kernel. 8520Sstevel@tonic-gate */ 8533466Srie if (ishdr && icache->c_data) { 8541618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 8551618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_INTERP), icache->c_name); 8561618Srie dbg_print(0, MSG_ORIG(MSG_FMT_INDENT), 8570Sstevel@tonic-gate (char *)icache->c_data->d_buf + 8580Sstevel@tonic-gate (iphdr_off - ishdr->sh_offset)); 8590Sstevel@tonic-gate } else 8600Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_WARN_INVINTERP1), file); 8610Sstevel@tonic-gate 8620Sstevel@tonic-gate /* 8630Sstevel@tonic-gate * If there are any inconsistences between the program header and 8640Sstevel@tonic-gate * section information, flag them. 8650Sstevel@tonic-gate */ 8660Sstevel@tonic-gate if (ishdr && ((iphdr_off != ishdr->sh_offset) || 8671618Srie (iphdr_fsz != ishdr->sh_size))) { 8680Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_WARN_INVINTERP2), file, 8690Sstevel@tonic-gate icache->c_name); 8700Sstevel@tonic-gate } 8710Sstevel@tonic-gate } 8720Sstevel@tonic-gate 8730Sstevel@tonic-gate /* 8740Sstevel@tonic-gate * Print the syminfo section. 8750Sstevel@tonic-gate */ 8760Sstevel@tonic-gate static void 8771618Srie syminfo(Cache *cache, Word shnum, const char *file) 8780Sstevel@tonic-gate { 8791618Srie Shdr *infoshdr; 8801618Srie Syminfo *info; 8811618Srie Sym *syms; 8821618Srie Dyn *dyns; 8831618Srie Word infonum, cnt, ndx, symnum; 8841618Srie Cache *infocache = 0, *symsec, *strsec; 8850Sstevel@tonic-gate 8860Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 8871618Srie if (cache[cnt].c_shdr->sh_type == SHT_SUNW_syminfo) { 8881618Srie infocache = &cache[cnt]; 8890Sstevel@tonic-gate break; 8900Sstevel@tonic-gate } 8910Sstevel@tonic-gate } 8921618Srie if (infocache == 0) 8930Sstevel@tonic-gate return; 8940Sstevel@tonic-gate 8951618Srie infoshdr = infocache->c_shdr; 8961618Srie if ((infoshdr->sh_entsize == 0) || (infoshdr->sh_size == 0)) { 8970Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 8981618Srie file, infocache->c_name); 8990Sstevel@tonic-gate return; 9000Sstevel@tonic-gate } 9013466Srie if (infocache->c_data == NULL) 9023466Srie return; 9033466Srie 9041618Srie infonum = (Word)(infoshdr->sh_size / infoshdr->sh_entsize); 9051618Srie info = (Syminfo *)infocache->c_data->d_buf; 9060Sstevel@tonic-gate 9070Sstevel@tonic-gate /* 9080Sstevel@tonic-gate * Get the data buffer of the associated dynamic section. 9090Sstevel@tonic-gate */ 9101618Srie if ((infoshdr->sh_info == 0) || (infoshdr->sh_info >= shnum)) { 9110Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO), 9121618Srie file, infocache->c_name, EC_WORD(infoshdr->sh_info)); 9130Sstevel@tonic-gate return; 9140Sstevel@tonic-gate } 9153466Srie if (cache[infoshdr->sh_info].c_data == NULL) 9163466Srie return; 9173466Srie 9181618Srie dyns = cache[infoshdr->sh_info].c_data->d_buf; 9191618Srie if (dyns == 0) { 9200Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 9211618Srie file, cache[infoshdr->sh_info].c_name); 9220Sstevel@tonic-gate return; 9230Sstevel@tonic-gate } 9240Sstevel@tonic-gate 9250Sstevel@tonic-gate /* 9261618Srie * Get the data buffer for the associated symbol table and string table. 9270Sstevel@tonic-gate */ 9281618Srie if (stringtbl(cache, 1, cnt, shnum, file, 9291618Srie &symnum, &symsec, &strsec) == 0) 9300Sstevel@tonic-gate return; 9310Sstevel@tonic-gate 9321618Srie syms = symsec->c_data->d_buf; 9330Sstevel@tonic-gate 9341618Srie /* 9351618Srie * Loop through the syminfo entries. 9361618Srie */ 9371618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 9381618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMINFO), infocache->c_name); 9391618Srie Elf_syminfo_title(0); 9400Sstevel@tonic-gate 9411618Srie for (ndx = 1, info++; ndx < infonum; ndx++, info++) { 9421618Srie Sym *sym; 9431618Srie const char *needed = 0, *name; 9441618Srie 9451618Srie if ((info->si_flags == 0) && (info->si_boundto == 0)) 9460Sstevel@tonic-gate continue; 9470Sstevel@tonic-gate 9481618Srie sym = &syms[ndx]; 9491618Srie name = string(infocache, ndx, strsec, file, sym->st_name); 9500Sstevel@tonic-gate 9511618Srie if (info->si_boundto < SYMINFO_BT_LOWRESERVE) { 9521618Srie Dyn *dyn = &dyns[info->si_boundto]; 9531618Srie 9541618Srie needed = string(infocache, info->si_boundto, 9551618Srie strsec, file, dyn->d_un.d_val); 9560Sstevel@tonic-gate } 9571618Srie Elf_syminfo_entry(0, ndx, info, name, needed); 9580Sstevel@tonic-gate } 9590Sstevel@tonic-gate } 9600Sstevel@tonic-gate 9610Sstevel@tonic-gate /* 9620Sstevel@tonic-gate * Print version definition section entries. 9630Sstevel@tonic-gate */ 9640Sstevel@tonic-gate static void 9654716Sab196087 version_def(Verdef *vdf, Word vdf_num, Cache *vcache, Cache *scache, 9660Sstevel@tonic-gate const char *file) 9670Sstevel@tonic-gate { 9681618Srie Word cnt; 9691618Srie char index[MAXNDXSIZE]; 9700Sstevel@tonic-gate 9711618Srie Elf_ver_def_title(0); 9720Sstevel@tonic-gate 9734716Sab196087 for (cnt = 1; cnt <= vdf_num; cnt++, 9741618Srie vdf = (Verdef *)((uintptr_t)vdf + vdf->vd_next)) { 9750Sstevel@tonic-gate const char *name, *dep; 9761618Srie Half vcnt = vdf->vd_cnt - 1; 9771618Srie Half ndx = vdf->vd_ndx; 9784433Sab196087 Verdaux *vdap = (Verdaux *)((uintptr_t)vdf + vdf->vd_aux); 9790Sstevel@tonic-gate 9800Sstevel@tonic-gate /* 9810Sstevel@tonic-gate * Obtain the name and first dependency (if any). 9820Sstevel@tonic-gate */ 9830Sstevel@tonic-gate name = string(vcache, cnt, scache, file, vdap->vda_name); 9841618Srie vdap = (Verdaux *)((uintptr_t)vdap + vdap->vda_next); 9850Sstevel@tonic-gate if (vcnt) 9860Sstevel@tonic-gate dep = string(vcache, cnt, scache, file, vdap->vda_name); 9870Sstevel@tonic-gate else 9880Sstevel@tonic-gate dep = MSG_ORIG(MSG_STR_EMPTY); 9890Sstevel@tonic-gate 9900Sstevel@tonic-gate (void) snprintf(index, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX), 9910Sstevel@tonic-gate EC_XWORD(ndx)); 9921618Srie Elf_ver_line_1(0, index, name, dep, 9931618Srie conv_ver_flags(vdf->vd_flags)); 9940Sstevel@tonic-gate 9950Sstevel@tonic-gate /* 9960Sstevel@tonic-gate * Print any additional dependencies. 9970Sstevel@tonic-gate */ 9980Sstevel@tonic-gate if (vcnt) { 9991618Srie vdap = (Verdaux *)((uintptr_t)vdap + vdap->vda_next); 10000Sstevel@tonic-gate for (vcnt--; vcnt; vcnt--, 10011618Srie vdap = (Verdaux *)((uintptr_t)vdap + 10020Sstevel@tonic-gate vdap->vda_next)) { 10030Sstevel@tonic-gate dep = string(vcache, cnt, scache, file, 10040Sstevel@tonic-gate vdap->vda_name); 10051618Srie Elf_ver_line_2(0, MSG_ORIG(MSG_STR_EMPTY), dep); 10060Sstevel@tonic-gate } 10070Sstevel@tonic-gate } 10080Sstevel@tonic-gate } 10090Sstevel@tonic-gate } 10100Sstevel@tonic-gate 10110Sstevel@tonic-gate /* 10124716Sab196087 * Print version needed section entries. 10134716Sab196087 * 10144716Sab196087 * entry: 10154716Sab196087 * vnd - Address of verneed data 10164716Sab196087 * vnd_num - # of Verneed entries 10174716Sab196087 * vcache - Cache of verneed section being processed 10184716Sab196087 * scache - Cache of associated string table section 10194716Sab196087 * file - Name of object being processed. 10204716Sab196087 * versym - Information about versym section 10214716Sab196087 * 10224716Sab196087 * exit: 10234716Sab196087 * The versions have been printed. If GNU style versioning 10244716Sab196087 * is in effect, versym->max_verndx has been updated to 10254716Sab196087 * contain the largest version index seen. 10260Sstevel@tonic-gate */ 10270Sstevel@tonic-gate static void 10284716Sab196087 version_need(Verneed *vnd, Word vnd_num, Cache *vcache, Cache *scache, 10294716Sab196087 const char *file, VERSYM_STATE *versym) 10300Sstevel@tonic-gate { 10314716Sab196087 Word cnt; 10324716Sab196087 char index[MAXNDXSIZE]; 10334716Sab196087 const char *index_str; 10344716Sab196087 10354716Sab196087 Elf_ver_need_title(0, versym->gnu); 10364716Sab196087 10374716Sab196087 /* 10384716Sab196087 * The versym section in an object that follows Solaris versioning 10394716Sab196087 * rules contains indexes into the verdef section. Symbols defined 10404716Sab196087 * in other objects (UNDEF) are given a version of 0, indicating that 10414716Sab196087 * they are not defined by this file, and the Verneed entries do not 10424716Sab196087 * have associated version indexes. For these reasons, we do not 10434716Sab196087 * display a version index for Solaris Verneed sections. 10444716Sab196087 * 10454716Sab196087 * The GNU versioning rules are different: Symbols defined in other 10464716Sab196087 * objects receive a version index in the range above those defined 10474716Sab196087 * by the Verdef section, and the vna_other field of the Vernaux 10484716Sab196087 * structs inside the Verneed section contain the version index for 10494716Sab196087 * that item. We therefore display the index when showing the 10504716Sab196087 * contents of a GNU Verneed section. You should not expect these 10514716Sab196087 * indexes to appear in sorted order --- it seems that the GNU ld 10524716Sab196087 * assigns the versions as symbols are encountered during linking, 10534716Sab196087 * and then the results are assembled into the Verneed section 10544716Sab196087 * afterwards. 10554716Sab196087 */ 10564716Sab196087 if (versym->gnu) { 10574716Sab196087 index_str = index; 10584716Sab196087 } else { 10594716Sab196087 /* For Solaris versioning, display a NULL string */ 10604716Sab196087 index_str = MSG_ORIG(MSG_STR_EMPTY); 10614716Sab196087 } 10624716Sab196087 10634716Sab196087 for (cnt = 1; cnt <= vnd_num; cnt++, 10641618Srie vnd = (Verneed *)((uintptr_t)vnd + vnd->vn_next)) { 10650Sstevel@tonic-gate const char *name, *dep; 10661618Srie Half vcnt = vnd->vn_cnt; 10674433Sab196087 Vernaux *vnap = (Vernaux *)((uintptr_t)vnd + vnd->vn_aux); 10680Sstevel@tonic-gate 10690Sstevel@tonic-gate /* 10700Sstevel@tonic-gate * Obtain the name of the needed file and the version name 10710Sstevel@tonic-gate * within it that we're dependent on. Note that the count 10720Sstevel@tonic-gate * should be at least one, otherwise this is a pretty bogus 10730Sstevel@tonic-gate * entry. 10740Sstevel@tonic-gate */ 10750Sstevel@tonic-gate name = string(vcache, cnt, scache, file, vnd->vn_file); 10760Sstevel@tonic-gate if (vcnt) 10770Sstevel@tonic-gate dep = string(vcache, cnt, scache, file, vnap->vna_name); 10780Sstevel@tonic-gate else 10790Sstevel@tonic-gate dep = MSG_INTL(MSG_STR_NULL); 10800Sstevel@tonic-gate 10814716Sab196087 if (versym->gnu) { 10824716Sab196087 /* Format the version index value */ 10834716Sab196087 (void) snprintf(index, MAXNDXSIZE, 10844716Sab196087 MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(vnap->vna_other)); 10854716Sab196087 if (vnap->vna_other > versym->max_verndx) 10864716Sab196087 versym->max_verndx = vnap->vna_other; 10874716Sab196087 } 10884716Sab196087 Elf_ver_line_1(0, index_str, name, dep, 10891618Srie conv_ver_flags(vnap->vna_flags)); 10900Sstevel@tonic-gate 10910Sstevel@tonic-gate /* 10920Sstevel@tonic-gate * Print any additional version dependencies. 10930Sstevel@tonic-gate */ 10940Sstevel@tonic-gate if (vcnt) { 10951618Srie vnap = (Vernaux *)((uintptr_t)vnap + vnap->vna_next); 10960Sstevel@tonic-gate for (vcnt--; vcnt; vcnt--, 10971618Srie vnap = (Vernaux *)((uintptr_t)vnap + 10980Sstevel@tonic-gate vnap->vna_next)) { 10990Sstevel@tonic-gate dep = string(vcache, cnt, scache, file, 11000Sstevel@tonic-gate vnap->vna_name); 11014716Sab196087 if (versym->gnu) { 11024716Sab196087 /* Format the next index value */ 11034716Sab196087 (void) snprintf(index, MAXNDXSIZE, 11044716Sab196087 MSG_ORIG(MSG_FMT_INDEX), 11054716Sab196087 EC_XWORD(vnap->vna_other)); 11064716Sab196087 Elf_ver_line_1(0, index_str, 11074716Sab196087 MSG_ORIG(MSG_STR_EMPTY), dep, 11084716Sab196087 conv_ver_flags(vnap->vna_flags)); 11094716Sab196087 if (vnap->vna_other > 11104716Sab196087 versym->max_verndx) 11114716Sab196087 versym->max_verndx = 11124716Sab196087 vnap->vna_other; 11134716Sab196087 } else { 11144716Sab196087 Elf_ver_line_3(0, 11154716Sab196087 MSG_ORIG(MSG_STR_EMPTY), dep, 11164716Sab196087 conv_ver_flags(vnap->vna_flags)); 11174716Sab196087 } 11184716Sab196087 } 11194716Sab196087 } 11204716Sab196087 } 11214716Sab196087 } 11224716Sab196087 11234716Sab196087 /* 11244716Sab196087 * Compute the max_verndx value for a GNU style object with 11254716Sab196087 * a Verneed section. This is only needed if version_need() is not 11264716Sab196087 * called. 11274716Sab196087 * 11284716Sab196087 * entry: 11294716Sab196087 * vnd - Address of verneed data 11304716Sab196087 * vnd_num - # of Verneed entries 11314716Sab196087 * versym - Information about versym section 11324716Sab196087 * 11334716Sab196087 * exit: 11344716Sab196087 * versym->max_verndx has been updated to contain the largest 11354716Sab196087 * version index seen. 11364716Sab196087 */ 11374716Sab196087 static void 11384716Sab196087 update_gnu_max_verndx(Verneed *vnd, Word vnd_num, VERSYM_STATE *versym) 11394716Sab196087 { 11404716Sab196087 Word cnt; 11414716Sab196087 11424716Sab196087 for (cnt = 1; cnt <= vnd_num; cnt++, 11434716Sab196087 vnd = (Verneed *)((uintptr_t)vnd + vnd->vn_next)) { 11444716Sab196087 Half vcnt = vnd->vn_cnt; 11454716Sab196087 Vernaux *vnap = (Vernaux *)((uintptr_t)vnd + vnd->vn_aux); 11464716Sab196087 11474716Sab196087 if (vnap->vna_other > versym->max_verndx) 11484716Sab196087 versym->max_verndx = vnap->vna_other; 11494716Sab196087 11504716Sab196087 /* 11514716Sab196087 * Check any additional version dependencies. 11524716Sab196087 */ 11534716Sab196087 if (vcnt) { 11544716Sab196087 vnap = (Vernaux *)((uintptr_t)vnap + vnap->vna_next); 11554716Sab196087 for (vcnt--; vcnt; vcnt--, 11564716Sab196087 vnap = (Vernaux *)((uintptr_t)vnap + 11574716Sab196087 vnap->vna_next)) { 11584716Sab196087 if (vnap->vna_other > versym->max_verndx) 11594716Sab196087 versym->max_verndx = vnap->vna_other; 11600Sstevel@tonic-gate } 11610Sstevel@tonic-gate } 11620Sstevel@tonic-gate } 11630Sstevel@tonic-gate } 11640Sstevel@tonic-gate 11650Sstevel@tonic-gate /* 11663875Sab196087 * Display version section information if the flags require it. 11673875Sab196087 * Return version information needed by other output. 11683875Sab196087 * 11693875Sab196087 * entry: 11703875Sab196087 * cache - Cache of all section headers 11713875Sab196087 * shnum - # of sections in cache 11723875Sab196087 * file - Name of file 11733875Sab196087 * flags - Command line option flags 11743875Sab196087 * versym - VERSYM_STATE block to be filled in. 11750Sstevel@tonic-gate */ 11763875Sab196087 static void 11773875Sab196087 versions(Cache *cache, Word shnum, const char *file, uint_t flags, 11783875Sab196087 VERSYM_STATE *versym) 11790Sstevel@tonic-gate { 11800Sstevel@tonic-gate GElf_Word cnt; 11814716Sab196087 Cache *verdef_cache = NULL, *verneed_cache = NULL; 11824716Sab196087 11834716Sab196087 11844716Sab196087 /* Gather information about the version sections */ 11853875Sab196087 bzero(versym, sizeof (*versym)); 11864716Sab196087 versym->max_verndx = 1; 11870Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 11881618Srie Cache *_cache = &cache[cnt]; 11891618Srie Shdr *shdr = _cache->c_shdr; 11904716Sab196087 Dyn *dyn; 11914716Sab196087 ulong_t numdyn; 11924716Sab196087 11934716Sab196087 switch (shdr->sh_type) { 11944716Sab196087 case SHT_DYNAMIC: 11954716Sab196087 /* 11964716Sab196087 * The GNU ld puts a DT_VERSYM entry in the dynamic 11974716Sab196087 * section so that the runtime linker can use it to 11984716Sab196087 * implement their versioning rules. They allow multiple 11994716Sab196087 * incompatible functions with the same name to exist 12004716Sab196087 * in different versions. The Solaris ld does not 12014716Sab196087 * support this mechanism, and as such, does not 12024716Sab196087 * produce DT_VERSYM. We use this fact to determine 12034716Sab196087 * which ld produced this object, and how to interpret 12044716Sab196087 * the version values. 12054716Sab196087 */ 12064716Sab196087 if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0) || 12074716Sab196087 (_cache->c_data == NULL)) 12084716Sab196087 continue; 12094716Sab196087 numdyn = shdr->sh_size / shdr->sh_entsize; 12104716Sab196087 dyn = (Dyn *)_cache->c_data->d_buf; 12114716Sab196087 for (; numdyn-- > 0; dyn++) 12124716Sab196087 if (dyn->d_tag == DT_VERSYM) { 12134716Sab196087 versym->gnu = 1; 12144716Sab196087 break; 12154716Sab196087 } 12164716Sab196087 break; 12174716Sab196087 12184716Sab196087 case SHT_SUNW_versym: 12194716Sab196087 /* Record data address for later symbol processing */ 12204716Sab196087 if (_cache->c_data != NULL) { 12214716Sab196087 versym->cache = _cache; 12224716Sab196087 versym->data = _cache->c_data->d_buf; 12234716Sab196087 continue; 12244716Sab196087 } 12254716Sab196087 break; 12264716Sab196087 12274716Sab196087 case SHT_SUNW_verdef: 12284716Sab196087 case SHT_SUNW_verneed: 12294716Sab196087 /* 12304716Sab196087 * Ensure the data is non-NULL and the number 12314716Sab196087 * of items is non-zero. Otherwise, we don't 12324716Sab196087 * understand the section, and will not use it. 12334716Sab196087 */ 12344716Sab196087 if ((_cache->c_data == NULL) || 12354716Sab196087 (_cache->c_data->d_buf == NULL)) { 12364716Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 12374716Sab196087 file, _cache->c_name); 12384716Sab196087 continue; 12394716Sab196087 } 12404716Sab196087 if (shdr->sh_info == 0) { 12414716Sab196087 (void) fprintf(stderr, 12424716Sab196087 MSG_INTL(MSG_ERR_BADSHINFO), 12434716Sab196087 file, _cache->c_name, 12444716Sab196087 EC_WORD(shdr->sh_info)); 12454716Sab196087 continue; 12464716Sab196087 } 12474716Sab196087 12484716Sab196087 /* Make sure the string table index is in range */ 12494716Sab196087 if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) { 12504716Sab196087 (void) fprintf(stderr, 12514716Sab196087 MSG_INTL(MSG_ERR_BADSHLINK), file, 12524716Sab196087 _cache->c_name, EC_WORD(shdr->sh_link)); 12534716Sab196087 continue; 12544716Sab196087 } 12554716Sab196087 12564716Sab196087 /* 12574716Sab196087 * The section is usable. Save the cache entry. 12584716Sab196087 */ 12594716Sab196087 if (shdr->sh_type == SHT_SUNW_verdef) { 12604716Sab196087 verdef_cache = _cache; 12614716Sab196087 /* 12624716Sab196087 * Under Solaris rules, if there is a verdef 12634716Sab196087 * section, the max versym index is number 12644716Sab196087 * of version definitions it supplies. 12654716Sab196087 */ 12664716Sab196087 versym->max_verndx = shdr->sh_info; 12674716Sab196087 } else { 12684716Sab196087 verneed_cache = _cache; 12694716Sab196087 } 12704716Sab196087 break; 12714716Sab196087 } 12724716Sab196087 } 12734716Sab196087 1274*5411Sab196087 if ((flags & FLG_SHOW_VERSIONS) == 0) { 12753875Sab196087 /* 12764716Sab196087 * If GNU versioning applies to this object, and there 12774716Sab196087 * is a Verneed section, then examine it to determine 12784716Sab196087 * the maximum Versym version index for this file. 12793875Sab196087 */ 12804716Sab196087 if ((versym->gnu) && (verneed_cache != NULL)) 12814716Sab196087 update_gnu_max_verndx( 12824716Sab196087 (Verneed *)verneed_cache->c_data->d_buf, 12834716Sab196087 verneed_cache->c_shdr->sh_info, versym); 12844716Sab196087 return; 12854716Sab196087 } 12864716Sab196087 12874716Sab196087 /* 12884716Sab196087 * Now that all the information is available, display the 12894716Sab196087 * Verdef and Verneed section contents. 12904716Sab196087 */ 12914716Sab196087 if (verdef_cache != NULL) { 12924716Sab196087 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 12934716Sab196087 dbg_print(0, MSG_INTL(MSG_ELF_SCN_VERDEF), 12944716Sab196087 verdef_cache->c_name); 12954716Sab196087 version_def((Verdef *)verdef_cache->c_data->d_buf, 12964716Sab196087 verdef_cache->c_shdr->sh_info, verdef_cache, 12974716Sab196087 &cache[verdef_cache->c_shdr->sh_link], file); 12984716Sab196087 } 12994716Sab196087 if (verneed_cache != NULL) { 13004716Sab196087 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 13014716Sab196087 dbg_print(0, MSG_INTL(MSG_ELF_SCN_VERNEED), 13024716Sab196087 verneed_cache->c_name); 13030Sstevel@tonic-gate /* 13044716Sab196087 * If GNU versioning applies to this object, version_need() 13054716Sab196087 * will update versym->max_verndx, and it is not 13064716Sab196087 * necessary to call update_gnu_max_verndx(). 13070Sstevel@tonic-gate */ 13084716Sab196087 version_need((Verneed *)verneed_cache->c_data->d_buf, 13094716Sab196087 verneed_cache->c_shdr->sh_info, verneed_cache, 13104716Sab196087 &cache[verneed_cache->c_shdr->sh_link], file, versym); 13110Sstevel@tonic-gate } 13120Sstevel@tonic-gate } 13130Sstevel@tonic-gate 13140Sstevel@tonic-gate /* 13153492Sab196087 * Initialize a symbol table state structure 13163492Sab196087 * 13173492Sab196087 * entry: 13183492Sab196087 * state - State structure to be initialized 13193492Sab196087 * cache - Cache of all section headers 13203492Sab196087 * shnum - # of sections in cache 13213492Sab196087 * secndx - Index of symbol table section 13223492Sab196087 * ehdr - ELF header for file 13233875Sab196087 * versym - Information about versym section 13243492Sab196087 * file - Name of file 13253492Sab196087 * flags - Command line option flags 13261618Srie */ 13271618Srie static int 13283492Sab196087 init_symtbl_state(SYMTBL_STATE *state, Cache *cache, Word shnum, Word secndx, 13293875Sab196087 Ehdr *ehdr, VERSYM_STATE *versym, const char *file, uint_t flags) 13303492Sab196087 { 13313492Sab196087 Shdr *shdr; 13323492Sab196087 13333492Sab196087 state->file = file; 13343492Sab196087 state->ehdr = ehdr; 13353492Sab196087 state->cache = cache; 13363492Sab196087 state->shnum = shnum; 13373492Sab196087 state->seccache = &cache[secndx]; 13383492Sab196087 state->secndx = secndx; 13393492Sab196087 state->secname = state->seccache->c_name; 13403492Sab196087 state->flags = flags; 13413492Sab196087 state->shxndx.checked = 0; 13423492Sab196087 state->shxndx.data = NULL; 13433492Sab196087 state->shxndx.n = 0; 13443492Sab196087 13453492Sab196087 shdr = state->seccache->c_shdr; 13463492Sab196087 13473492Sab196087 /* 13483492Sab196087 * Check the symbol data and per-item size. 13493492Sab196087 */ 13503492Sab196087 if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) { 13513492Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 13523492Sab196087 file, state->secname); 13533492Sab196087 return (0); 13543492Sab196087 } 13553492Sab196087 if (state->seccache->c_data == NULL) 13563492Sab196087 return (0); 13573492Sab196087 13583492Sab196087 /* LINTED */ 13593492Sab196087 state->symn = (Word)(shdr->sh_size / shdr->sh_entsize); 13603492Sab196087 state->sym = (Sym *)state->seccache->c_data->d_buf; 13613492Sab196087 13623492Sab196087 /* 13633492Sab196087 * Check associated string table section. 13643492Sab196087 */ 13653492Sab196087 if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) { 13663492Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK), 13673492Sab196087 file, state->secname, EC_WORD(shdr->sh_link)); 13683492Sab196087 return (0); 13693492Sab196087 } 13703492Sab196087 13713492Sab196087 /* 13723492Sab196087 * Determine if there is a associated Versym section 13733492Sab196087 * with this Symbol Table. 13743492Sab196087 */ 13753875Sab196087 if (versym->cache && 13763875Sab196087 (versym->cache->c_shdr->sh_link == state->secndx)) 13773875Sab196087 state->versym = versym; 13783492Sab196087 else 13793492Sab196087 state->versym = NULL; 13803492Sab196087 13813492Sab196087 13823492Sab196087 return (1); 13833492Sab196087 } 13843492Sab196087 13853492Sab196087 /* 13863492Sab196087 * Determine the extended section index used for symbol tables entries. 13873492Sab196087 */ 13883492Sab196087 static void 13893492Sab196087 symbols_getxindex(SYMTBL_STATE * state) 13901618Srie { 13911618Srie uint_t symn; 13921618Srie Word symcnt; 13931618Srie 13943492Sab196087 state->shxndx.checked = 1; /* Note that we've been called */ 13953492Sab196087 for (symcnt = 1; symcnt < state->shnum; symcnt++) { 13963492Sab196087 Cache *_cache = &state->cache[symcnt]; 13971618Srie Shdr *shdr = _cache->c_shdr; 13981618Srie 13991618Srie if ((shdr->sh_type != SHT_SYMTAB_SHNDX) || 14003492Sab196087 (shdr->sh_link != state->secndx)) 14011618Srie continue; 14021618Srie 14031618Srie if ((shdr->sh_entsize) && 14041618Srie /* LINTED */ 14051618Srie ((symn = (uint_t)(shdr->sh_size / shdr->sh_entsize)) == 0)) 14061618Srie continue; 14071618Srie 14083466Srie if (_cache->c_data == NULL) 14093466Srie continue; 14103466Srie 14113492Sab196087 state->shxndx.data = _cache->c_data->d_buf; 14123492Sab196087 state->shxndx.n = symn; 14133492Sab196087 return; 14141618Srie } 14151618Srie } 14161618Srie 14171618Srie /* 14183492Sab196087 * Produce a line of output for the given symbol 14193492Sab196087 * 14203492Sab196087 * entry: 14213875Sab196087 * state - Symbol table state 14223492Sab196087 * symndx - Index of symbol within the table 14234832Srie * info - Value of st_info (indicates local/global range) 14243492Sab196087 * symndx_disp - Index to display. This may not be the same 14253492Sab196087 * as symndx if the display is relative to the logical 14263492Sab196087 * combination of the SUNW_ldynsym/dynsym tables. 14273492Sab196087 * sym - Symbol to display 14280Sstevel@tonic-gate */ 14293492Sab196087 static void 14304832Srie output_symbol(SYMTBL_STATE *state, Word symndx, Word info, Word disp_symndx, 14314832Srie Sym *sym) 14320Sstevel@tonic-gate { 14333118Sab196087 /* 14343118Sab196087 * Symbol types for which we check that the specified 14353118Sab196087 * address/size land inside the target section. 14363118Sab196087 */ 14373492Sab196087 static const int addr_symtype[STT_NUM] = { 14383118Sab196087 0, /* STT_NOTYPE */ 14393118Sab196087 1, /* STT_OBJECT */ 14403118Sab196087 1, /* STT_FUNC */ 14413118Sab196087 0, /* STT_SECTION */ 14423118Sab196087 0, /* STT_FILE */ 14433118Sab196087 1, /* STT_COMMON */ 14443118Sab196087 0, /* STT_TLS */ 14453118Sab196087 }; 14463118Sab196087 #if STT_NUM != (STT_TLS + 1) 14473492Sab196087 #error "STT_NUM has grown. Update addr_symtype[]" 14483118Sab196087 #endif 14493118Sab196087 14504665Sab196087 char index[MAXNDXSIZE]; 14514665Sab196087 const char *symname, *sec; 14523875Sab196087 Versym verndx; 14534716Sab196087 int gnuver; 14543492Sab196087 uchar_t type; 14553492Sab196087 Shdr *tshdr; 14563492Sab196087 Word shndx; 14574734Sab196087 Conv_inv_buf_t inv_buf; 14583492Sab196087 14593492Sab196087 /* Ensure symbol index is in range */ 14603492Sab196087 if (symndx >= state->symn) { 14613492Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSORTNDX), 14623492Sab196087 state->file, state->secname, EC_WORD(symndx)); 14633492Sab196087 return; 14643492Sab196087 } 14653492Sab196087 14663492Sab196087 /* 14673492Sab196087 * If we are using extended symbol indexes, find the 14683492Sab196087 * corresponding SHN_SYMTAB_SHNDX table. 14693492Sab196087 */ 14703492Sab196087 if ((sym->st_shndx == SHN_XINDEX) && (state->shxndx.checked == 0)) 14713492Sab196087 symbols_getxindex(state); 14723492Sab196087 14733492Sab196087 /* LINTED */ 14743492Sab196087 symname = string(state->seccache, symndx, 14753492Sab196087 &state->cache[state->seccache->c_shdr->sh_link], state->file, 14763492Sab196087 sym->st_name); 14773492Sab196087 14783492Sab196087 tshdr = 0; 14793492Sab196087 sec = NULL; 14803492Sab196087 14814665Sab196087 if (state->ehdr->e_type == ET_CORE) { 14823492Sab196087 sec = (char *)MSG_INTL(MSG_STR_UNKNOWN); 1483*5411Sab196087 } else if (state->flags & FLG_CTL_FAKESHDR) { 14844665Sab196087 /* 14854665Sab196087 * If we are using fake section headers derived from 14864665Sab196087 * the program headers, then the section indexes 14874665Sab196087 * in the symbols do not correspond to these headers. 14884665Sab196087 * The section names are not available, so all we can 14894665Sab196087 * do is to display them in numeric form. 14904665Sab196087 */ 14914734Sab196087 sec = conv_sym_shndx(sym->st_shndx, &inv_buf); 14924665Sab196087 } else if ((sym->st_shndx < SHN_LORESERVE) && 14933492Sab196087 (sym->st_shndx < state->shnum)) { 14943492Sab196087 shndx = sym->st_shndx; 14953492Sab196087 tshdr = state->cache[shndx].c_shdr; 14963492Sab196087 sec = state->cache[shndx].c_name; 14973492Sab196087 } else if (sym->st_shndx == SHN_XINDEX) { 14983492Sab196087 if (state->shxndx.data) { 14993492Sab196087 Word _shxndx; 15003492Sab196087 15013492Sab196087 if (symndx > state->shxndx.n) { 15024433Sab196087 (void) fprintf(stderr, 15034433Sab196087 MSG_INTL(MSG_ERR_BADSYMXINDEX1), 15044433Sab196087 state->file, state->secname, 15054433Sab196087 EC_WORD(symndx)); 15063492Sab196087 } else if ((_shxndx = 15073492Sab196087 state->shxndx.data[symndx]) > state->shnum) { 15084433Sab196087 (void) fprintf(stderr, 15094433Sab196087 MSG_INTL(MSG_ERR_BADSYMXINDEX2), 15104433Sab196087 state->file, state->secname, 15114433Sab196087 EC_WORD(symndx), EC_WORD(_shxndx)); 15123492Sab196087 } else { 15134433Sab196087 shndx = _shxndx; 15144433Sab196087 tshdr = state->cache[shndx].c_shdr; 15154433Sab196087 sec = state->cache[shndx].c_name; 15163492Sab196087 } 15173492Sab196087 } else { 15183492Sab196087 (void) fprintf(stderr, 15193492Sab196087 MSG_INTL(MSG_ERR_BADSYMXINDEX3), 15203492Sab196087 state->file, state->secname, EC_WORD(symndx)); 15213492Sab196087 } 15223492Sab196087 } else if ((sym->st_shndx < SHN_LORESERVE) && 15233492Sab196087 (sym->st_shndx >= state->shnum)) { 15243492Sab196087 (void) fprintf(stderr, 15253492Sab196087 MSG_INTL(MSG_ERR_BADSYM5), state->file, 15263492Sab196087 state->secname, demangle(symname, state->flags), 15273492Sab196087 sym->st_shndx); 15283492Sab196087 } 15290Sstevel@tonic-gate 15303492Sab196087 /* 15313492Sab196087 * If versioning is available display the 15323875Sab196087 * version index. If not, then use 0. 15333492Sab196087 */ 15343875Sab196087 if (state->versym) { 15354716Sab196087 Versym test_verndx; 15364716Sab196087 15374716Sab196087 verndx = test_verndx = state->versym->data[symndx]; 15384716Sab196087 gnuver = state->versym->gnu; 15393875Sab196087 15403875Sab196087 /* 15413875Sab196087 * Check to see if this is a defined symbol with a 15423875Sab196087 * version index that is outside the valid range for 15434716Sab196087 * the file. The interpretation of this depends on 15444716Sab196087 * the style of versioning used by the object. 15453875Sab196087 * 15464716Sab196087 * Versions >= VER_NDX_LORESERVE have special meanings, 15474716Sab196087 * and are exempt from this checking. 15484716Sab196087 * 15494716Sab196087 * GNU style version indexes use the top bit of the 15504716Sab196087 * 16-bit index value (0x8000) as the "hidden bit". 15514716Sab196087 * We must mask off this bit in order to compare 15524716Sab196087 * the version against the maximum value. 15533875Sab196087 */ 15544716Sab196087 if (gnuver) 15554716Sab196087 test_verndx &= ~0x8000; 15564716Sab196087 15574716Sab196087 if ((test_verndx > state->versym->max_verndx) && 15584716Sab196087 (verndx < VER_NDX_LORESERVE)) 15594716Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADVER), 15604716Sab196087 state->file, state->secname, EC_WORD(symndx), 15614716Sab196087 EC_HALF(test_verndx), state->versym->max_verndx); 15623875Sab196087 } else { 15633492Sab196087 verndx = 0; 15644716Sab196087 gnuver = 0; 15653875Sab196087 } 15663492Sab196087 15673492Sab196087 /* 15683492Sab196087 * Error checking for TLS. 15693492Sab196087 */ 15703492Sab196087 type = ELF_ST_TYPE(sym->st_info); 15713492Sab196087 if (type == STT_TLS) { 15723492Sab196087 if (tshdr && 15733492Sab196087 (sym->st_shndx != SHN_UNDEF) && 15743492Sab196087 ((tshdr->sh_flags & SHF_TLS) == 0)) { 15753492Sab196087 (void) fprintf(stderr, 15763492Sab196087 MSG_INTL(MSG_ERR_BADSYM3), state->file, 15773492Sab196087 state->secname, demangle(symname, state->flags)); 15783492Sab196087 } 15793492Sab196087 } else if ((type != STT_SECTION) && sym->st_size && 15803492Sab196087 tshdr && (tshdr->sh_flags & SHF_TLS)) { 15813492Sab196087 (void) fprintf(stderr, 15823492Sab196087 MSG_INTL(MSG_ERR_BADSYM4), state->file, 15833492Sab196087 state->secname, demangle(symname, state->flags)); 15843492Sab196087 } 15853492Sab196087 15863492Sab196087 /* 15873492Sab196087 * If a symbol with non-zero size has a type that 15883492Sab196087 * specifies an address, then make sure the location 15893492Sab196087 * it references is actually contained within the 15903492Sab196087 * section. UNDEF symbols don't count in this case, 15913492Sab196087 * so we ignore them. 15923492Sab196087 * 15933492Sab196087 * The meaning of the st_value field in a symbol 15943492Sab196087 * depends on the type of object. For a relocatable 15953492Sab196087 * object, it is the offset within the section. 15963492Sab196087 * For sharable objects, it is the offset relative to 15973492Sab196087 * the base of the object, and for other types, it is 15983492Sab196087 * the virtual address. To get an offset within the 15993492Sab196087 * section for non-ET_REL files, we subtract the 16003492Sab196087 * base address of the section. 16013492Sab196087 */ 16023492Sab196087 if (addr_symtype[type] && (sym->st_size > 0) && 16033492Sab196087 (sym->st_shndx != SHN_UNDEF) && ((sym->st_shndx < SHN_LORESERVE) || 16043492Sab196087 (sym->st_shndx == SHN_XINDEX)) && (tshdr != NULL)) { 16053492Sab196087 Word v = sym->st_value; 16063492Sab196087 if (state->ehdr->e_type != ET_REL) 16074832Srie v -= tshdr->sh_addr; 16083492Sab196087 if (((v + sym->st_size) > tshdr->sh_size)) { 16093492Sab196087 (void) fprintf(stderr, 16103492Sab196087 MSG_INTL(MSG_ERR_BADSYM6), state->file, 16113492Sab196087 state->secname, demangle(symname, state->flags), 16123492Sab196087 EC_WORD(shndx), EC_XWORD(tshdr->sh_size), 16133492Sab196087 EC_XWORD(sym->st_value), EC_XWORD(sym->st_size)); 16143492Sab196087 } 16153492Sab196087 } 16163492Sab196087 16174832Srie /* 16184832Srie * A typical symbol table uses the sh_info field to indicate one greater 16194832Srie * than the symbol table index of the last local symbol, STB_LOCAL. 16204832Srie * Therefore, symbol indexes less than sh_info should have local 16214832Srie * binding. Symbol indexes greater than, or equal to sh_info, should 16224832Srie * have global binding. Note, we exclude UNDEF/NOTY symbols with zero 16234832Srie * value and size, as these symbols may be the result of an mcs(1) 16244832Srie * section deletion. 16254832Srie */ 16264832Srie if (info) { 16274832Srie uchar_t bind = ELF_ST_BIND(sym->st_info); 16284832Srie 16294832Srie if ((symndx < info) && (bind != STB_LOCAL)) { 16304832Srie (void) fprintf(stderr, 16314832Srie MSG_INTL(MSG_ERR_BADSYM7), state->file, 16324832Srie state->secname, demangle(symname, state->flags), 16334832Srie EC_XWORD(info)); 16344832Srie 16354832Srie } else if ((symndx >= info) && (bind == STB_LOCAL) && 16364832Srie ((sym->st_shndx != SHN_UNDEF) || 16374832Srie (ELF_ST_TYPE(sym->st_info) != STT_NOTYPE) || 16384832Srie (sym->st_size != 0) || (sym->st_value != 0))) { 16394832Srie (void) fprintf(stderr, 16404832Srie MSG_INTL(MSG_ERR_BADSYM8), state->file, 16414832Srie state->secname, demangle(symname, state->flags), 16424832Srie EC_XWORD(info)); 16434832Srie } 16444832Srie } 16454832Srie 16463492Sab196087 (void) snprintf(index, MAXNDXSIZE, 16473492Sab196087 MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(disp_symndx)); 16483492Sab196087 Elf_syms_table_entry(0, ELF_DBG_ELFDUMP, index, 16494716Sab196087 state->ehdr->e_machine, sym, verndx, gnuver, sec, symname); 16503492Sab196087 } 16513492Sab196087 16523492Sab196087 /* 16533492Sab196087 * Search for and process any symbol tables. 16543492Sab196087 */ 16553492Sab196087 void 16564168Sab196087 symbols(Cache *cache, Word shnum, Ehdr *ehdr, VERSYM_STATE *versym, 16574168Sab196087 const char *file, uint_t flags) 16583492Sab196087 { 16593492Sab196087 SYMTBL_STATE state; 16603492Sab196087 Cache *_cache; 16613492Sab196087 Word secndx; 16623492Sab196087 16633492Sab196087 for (secndx = 1; secndx < shnum; secndx++) { 16643492Sab196087 Word symcnt; 16653492Sab196087 Shdr *shdr; 16663492Sab196087 16673492Sab196087 _cache = &cache[secndx]; 16683492Sab196087 shdr = _cache->c_shdr; 16690Sstevel@tonic-gate 16700Sstevel@tonic-gate if ((shdr->sh_type != SHT_SYMTAB) && 16712766Sab196087 (shdr->sh_type != SHT_DYNSYM) && 16722766Sab196087 (shdr->sh_type != SHT_SUNW_LDYNSYM)) 16730Sstevel@tonic-gate continue; 1674*5411Sab196087 if (!match(MATCH_F_ALL, _cache->c_name, secndx, shdr->sh_type)) 16753466Srie continue; 16763466Srie 16773492Sab196087 if (!init_symtbl_state(&state, cache, shnum, secndx, ehdr, 16783875Sab196087 versym, file, flags)) 16790Sstevel@tonic-gate continue; 16800Sstevel@tonic-gate /* 16810Sstevel@tonic-gate * Loop through the symbol tables entries. 16820Sstevel@tonic-gate */ 16831618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 16843492Sab196087 dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMTAB), state.secname); 16851618Srie Elf_syms_table_title(0, ELF_DBG_ELFDUMP); 16860Sstevel@tonic-gate 16873492Sab196087 for (symcnt = 0; symcnt < state.symn; symcnt++) 16884832Srie output_symbol(&state, symcnt, shdr->sh_info, symcnt, 16893492Sab196087 state.sym + symcnt); 16903492Sab196087 } 16913492Sab196087 } 16920Sstevel@tonic-gate 16933492Sab196087 /* 16943492Sab196087 * Search for and process any SHT_SUNW_symsort or SHT_SUNW_tlssort sections. 16953492Sab196087 * These sections are always associated with the .SUNW_ldynsym./.dynsym pair. 16963492Sab196087 */ 16973492Sab196087 static void 16984168Sab196087 sunw_sort(Cache *cache, Word shnum, Ehdr *ehdr, VERSYM_STATE *versym, 16994168Sab196087 const char *file, uint_t flags) 17003492Sab196087 { 17013492Sab196087 SYMTBL_STATE ldynsym_state, dynsym_state; 17023492Sab196087 Cache *sortcache, *symcache; 17033492Sab196087 Shdr *sortshdr, *symshdr; 17043492Sab196087 Word sortsecndx, symsecndx; 17053492Sab196087 Word ldynsym_cnt; 17063492Sab196087 Word *ndx; 17073492Sab196087 Word ndxn; 17083492Sab196087 int output_cnt = 0; 17094734Sab196087 Conv_inv_buf_t inv_buf; 17100Sstevel@tonic-gate 17113492Sab196087 for (sortsecndx = 1; sortsecndx < shnum; sortsecndx++) { 17120Sstevel@tonic-gate 17133492Sab196087 sortcache = &cache[sortsecndx]; 17143492Sab196087 sortshdr = sortcache->c_shdr; 17150Sstevel@tonic-gate 17163492Sab196087 if ((sortshdr->sh_type != SHT_SUNW_symsort) && 17173492Sab196087 (sortshdr->sh_type != SHT_SUNW_tlssort)) 17183492Sab196087 continue; 1719*5411Sab196087 if (!match(MATCH_F_ALL, sortcache->c_name, sortsecndx, 1720*5411Sab196087 sortshdr->sh_type)) 17213492Sab196087 continue; 17220Sstevel@tonic-gate 17233492Sab196087 /* 17243492Sab196087 * If the section references a SUNW_ldynsym, then we 17253492Sab196087 * expect to see the associated .dynsym immediately 17263492Sab196087 * following. If it references a .dynsym, there is no 17273492Sab196087 * SUNW_ldynsym. If it is any other type, then we don't 17283492Sab196087 * know what to do with it. 17293492Sab196087 */ 17303492Sab196087 if ((sortshdr->sh_link == 0) || (sortshdr->sh_link >= shnum)) { 17313492Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK), 17323492Sab196087 file, sortcache->c_name, 17333492Sab196087 EC_WORD(sortshdr->sh_link)); 17343492Sab196087 continue; 17353492Sab196087 } 17363492Sab196087 symcache = &cache[sortshdr->sh_link]; 17373492Sab196087 symshdr = symcache->c_shdr; 17383492Sab196087 symsecndx = sortshdr->sh_link; 17393492Sab196087 ldynsym_cnt = 0; 17403492Sab196087 switch (symshdr->sh_type) { 17413492Sab196087 case SHT_SUNW_LDYNSYM: 17423492Sab196087 if (!init_symtbl_state(&ldynsym_state, cache, shnum, 17433875Sab196087 symsecndx, ehdr, versym, file, flags)) 17443492Sab196087 continue; 17453492Sab196087 ldynsym_cnt = ldynsym_state.symn; 17460Sstevel@tonic-gate /* 17473492Sab196087 * We know that the dynsym follows immediately 17483492Sab196087 * after the SUNW_ldynsym, and so, should be at 17493492Sab196087 * (sortshdr->sh_link + 1). However, elfdump is a 17503492Sab196087 * diagnostic tool, so we do the full paranoid 17513492Sab196087 * search instead. 17520Sstevel@tonic-gate */ 17533492Sab196087 for (symsecndx = 1; symsecndx < shnum; symsecndx++) { 17543492Sab196087 symcache = &cache[symsecndx]; 17553492Sab196087 symshdr = symcache->c_shdr; 17563492Sab196087 if (symshdr->sh_type == SHT_DYNSYM) 17573492Sab196087 break; 17583492Sab196087 } 17593492Sab196087 if (symsecndx >= shnum) { /* Dynsym not found! */ 17600Sstevel@tonic-gate (void) fprintf(stderr, 17613492Sab196087 MSG_INTL(MSG_ERR_NODYNSYM), 17623492Sab196087 file, sortcache->c_name); 17633492Sab196087 continue; 17640Sstevel@tonic-gate } 17653492Sab196087 /* Fallthrough to process associated dynsym */ 17663492Sab196087 /*FALLTHROUGH*/ 17673492Sab196087 case SHT_DYNSYM: 17683492Sab196087 if (!init_symtbl_state(&dynsym_state, cache, shnum, 17693875Sab196087 symsecndx, ehdr, versym, file, flags)) 17703492Sab196087 continue; 17713492Sab196087 break; 17723492Sab196087 default: 17733492Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADNDXSEC), 17743492Sab196087 file, sortcache->c_name, conv_sec_type( 17754734Sab196087 ehdr->e_machine, symshdr->sh_type, 0, &inv_buf)); 17763492Sab196087 continue; 17773492Sab196087 } 17780Sstevel@tonic-gate 17793492Sab196087 /* 17803492Sab196087 * Output header 17813492Sab196087 */ 17823492Sab196087 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 17833492Sab196087 if (ldynsym_cnt > 0) { 17843492Sab196087 dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMSORT2), 17853492Sab196087 sortcache->c_name, ldynsym_state.secname, 17863492Sab196087 dynsym_state.secname); 17870Sstevel@tonic-gate /* 17883492Sab196087 * The data for .SUNW_ldynsym and dynsym sections 17893492Sab196087 * is supposed to be adjacent with SUNW_ldynsym coming 17903492Sab196087 * first. Check, and issue a warning if it isn't so. 17910Sstevel@tonic-gate */ 17924665Sab196087 if (((ldynsym_state.sym + ldynsym_state.symn) 17934665Sab196087 != dynsym_state.sym) && 1794*5411Sab196087 ((flags & FLG_CTL_FAKESHDR) == 0)) 17953492Sab196087 (void) fprintf(stderr, 17963492Sab196087 MSG_INTL(MSG_ERR_LDYNNOTADJ), file, 17973492Sab196087 ldynsym_state.secname, 17983492Sab196087 dynsym_state.secname); 17993492Sab196087 } else { 18003492Sab196087 dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMSORT1), 18013492Sab196087 sortcache->c_name, dynsym_state.secname); 18023492Sab196087 } 18033492Sab196087 Elf_syms_table_title(0, ELF_DBG_ELFDUMP); 18043492Sab196087 18053492Sab196087 /* If not first one, insert a line of whitespace */ 18063492Sab196087 if (output_cnt++ > 0) 18073492Sab196087 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 18083118Sab196087 18093492Sab196087 /* 18103492Sab196087 * SUNW_dynsymsort and SUNW_dyntlssort are arrays of 18113492Sab196087 * symbol indices. Iterate over the array entries, 18123492Sab196087 * dispaying the referenced symbols. 18133492Sab196087 */ 18143492Sab196087 ndxn = sortshdr->sh_size / sortshdr->sh_entsize; 18153492Sab196087 ndx = (Word *)sortcache->c_data->d_buf; 18163492Sab196087 for (; ndxn-- > 0; ndx++) { 18173492Sab196087 if (*ndx >= ldynsym_cnt) { 18183492Sab196087 Word sec_ndx = *ndx - ldynsym_cnt; 18193492Sab196087 18204832Srie output_symbol(&dynsym_state, sec_ndx, 0, 18213492Sab196087 *ndx, dynsym_state.sym + sec_ndx); 18223492Sab196087 } else { 18234832Srie output_symbol(&ldynsym_state, *ndx, 0, 18243492Sab196087 *ndx, ldynsym_state.sym + *ndx); 18250Sstevel@tonic-gate } 18260Sstevel@tonic-gate } 18270Sstevel@tonic-gate } 18280Sstevel@tonic-gate } 18290Sstevel@tonic-gate 18300Sstevel@tonic-gate /* 18310Sstevel@tonic-gate * Search for and process any relocation sections. 18320Sstevel@tonic-gate */ 18330Sstevel@tonic-gate static void 18344168Sab196087 reloc(Cache *cache, Word shnum, Ehdr *ehdr, const char *file, 18351618Srie uint_t flags) 18360Sstevel@tonic-gate { 18371618Srie Word cnt; 18380Sstevel@tonic-gate 18390Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 18401618Srie Word type, symnum; 18411618Srie Xword relndx, relnum, relsize; 18421618Srie void *rels; 18431618Srie Sym *syms; 18441618Srie Cache *symsec, *strsec; 18450Sstevel@tonic-gate Cache *_cache = &cache[cnt]; 18461618Srie Shdr *shdr = _cache->c_shdr; 18471618Srie char *relname = _cache->c_name; 18484734Sab196087 Conv_inv_buf_t inv_buf; 18490Sstevel@tonic-gate 18500Sstevel@tonic-gate if (((type = shdr->sh_type) != SHT_RELA) && 18510Sstevel@tonic-gate (type != SHT_REL)) 18520Sstevel@tonic-gate continue; 1853*5411Sab196087 if (!match(MATCH_F_ALL, relname, cnt, type)) 18540Sstevel@tonic-gate continue; 18550Sstevel@tonic-gate 18560Sstevel@tonic-gate /* 18571618Srie * Decide entry size. 18580Sstevel@tonic-gate */ 18591618Srie if (((relsize = shdr->sh_entsize) == 0) || 18601618Srie (relsize > shdr->sh_size)) { 18610Sstevel@tonic-gate if (type == SHT_RELA) 18621618Srie relsize = sizeof (Rela); 18630Sstevel@tonic-gate else 18641618Srie relsize = sizeof (Rel); 18650Sstevel@tonic-gate } 18660Sstevel@tonic-gate 18670Sstevel@tonic-gate /* 18680Sstevel@tonic-gate * Determine the number of relocations available. 18690Sstevel@tonic-gate */ 18700Sstevel@tonic-gate if (shdr->sh_size == 0) { 18710Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 18721618Srie file, relname); 18730Sstevel@tonic-gate continue; 18740Sstevel@tonic-gate } 18753466Srie if (_cache->c_data == NULL) 18763466Srie continue; 18773466Srie 18781618Srie rels = _cache->c_data->d_buf; 18791618Srie relnum = shdr->sh_size / relsize; 18800Sstevel@tonic-gate 18810Sstevel@tonic-gate /* 18821618Srie * Get the data buffer for the associated symbol table and 18831618Srie * string table. 18840Sstevel@tonic-gate */ 18851618Srie if (stringtbl(cache, 1, cnt, shnum, file, 18861618Srie &symnum, &symsec, &strsec) == 0) 18870Sstevel@tonic-gate continue; 18881618Srie 18891618Srie syms = symsec->c_data->d_buf; 18900Sstevel@tonic-gate 18910Sstevel@tonic-gate /* 18920Sstevel@tonic-gate * Loop through the relocation entries. 18930Sstevel@tonic-gate */ 18941618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 18951618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_RELOC), _cache->c_name); 18961618Srie Elf_reloc_title(0, ELF_DBG_ELFDUMP, type); 18970Sstevel@tonic-gate 18981618Srie for (relndx = 0; relndx < relnum; relndx++, 18991618Srie rels = (void *)((char *)rels + relsize)) { 19000Sstevel@tonic-gate char section[BUFSIZ]; 19011618Srie const char *symname; 19021618Srie Word symndx, reltype; 19031618Srie Rela *rela; 19041618Srie Rel *rel; 19050Sstevel@tonic-gate 19060Sstevel@tonic-gate /* 19071618Srie * Unravel the relocation and determine the symbol with 19081618Srie * which this relocation is associated. 19090Sstevel@tonic-gate */ 19100Sstevel@tonic-gate if (type == SHT_RELA) { 19111618Srie rela = (Rela *)rels; 19121618Srie symndx = ELF_R_SYM(rela->r_info); 19131618Srie reltype = ELF_R_TYPE(rela->r_info); 19140Sstevel@tonic-gate } else { 19151618Srie rel = (Rel *)rels; 19161618Srie symndx = ELF_R_SYM(rel->r_info); 19171618Srie reltype = ELF_R_TYPE(rel->r_info); 19180Sstevel@tonic-gate } 19191618Srie 19201618Srie symname = relsymname(cache, _cache, strsec, symndx, 19211618Srie symnum, relndx, syms, section, BUFSIZ, file, 19221618Srie flags); 19231618Srie 19241618Srie /* 19251618Srie * A zero symbol index is only valid for a few 19261618Srie * relocations. 19271618Srie */ 19281618Srie if (symndx == 0) { 19291618Srie Half mach = ehdr->e_machine; 19301618Srie int badrel = 0; 19310Sstevel@tonic-gate 19321618Srie if ((mach == EM_SPARC) || 19331618Srie (mach == EM_SPARC32PLUS) || 19341618Srie (mach == EM_SPARCV9)) { 19351618Srie if ((reltype != R_SPARC_NONE) && 19361618Srie (reltype != R_SPARC_REGISTER) && 19371618Srie (reltype != R_SPARC_RELATIVE)) 19381618Srie badrel++; 19391618Srie } else if (mach == EM_386) { 19401618Srie if ((reltype != R_386_NONE) && 19411618Srie (reltype != R_386_RELATIVE)) 19421618Srie badrel++; 19431618Srie } else if (mach == EM_AMD64) { 19441618Srie if ((reltype != R_AMD64_NONE) && 19451618Srie (reltype != R_AMD64_RELATIVE)) 19461618Srie badrel++; 19471618Srie } 19481618Srie 19491618Srie if (badrel) { 19501618Srie (void) fprintf(stderr, 19511618Srie MSG_INTL(MSG_ERR_BADREL1), file, 19524734Sab196087 conv_reloc_type(mach, reltype, 19534734Sab196087 0, &inv_buf)); 19540Sstevel@tonic-gate } 19550Sstevel@tonic-gate } 19560Sstevel@tonic-gate 19571618Srie Elf_reloc_entry_1(0, ELF_DBG_ELFDUMP, 19581618Srie MSG_ORIG(MSG_STR_EMPTY), ehdr->e_machine, type, 19591618Srie rels, relname, symname, 0); 19600Sstevel@tonic-gate } 19610Sstevel@tonic-gate } 19620Sstevel@tonic-gate } 19630Sstevel@tonic-gate 19645230Sab196087 19655230Sab196087 /* 19665230Sab196087 * This value controls which test dyn_test() performs. 19675230Sab196087 */ 19685230Sab196087 typedef enum { DYN_TEST_ADDR, DYN_TEST_SIZE, DYN_TEST_ENTSIZE } dyn_test_t; 19695230Sab196087 19705230Sab196087 /* 19715230Sab196087 * Used by dynamic() to compare the value of a dynamic element against 19725230Sab196087 * the starting address of the section it references. 19735230Sab196087 * 19745230Sab196087 * entry: 19755230Sab196087 * test_type - Specify which dyn item is being tested. 19765230Sab196087 * sh_type - SHT_* type value for required section. 19775230Sab196087 * sec_cache - Cache entry for section, or NULL if the object lacks 19785230Sab196087 * a section of this type. 19795230Sab196087 * dyn - Dyn entry to be tested 19805230Sab196087 * dynsec_cnt - # of dynamic section being examined. The first 19815230Sab196087 * dynamic section is 1, the next is 2, and so on... 19825230Sab196087 * ehdr - ELF header for file 19835230Sab196087 * file - Name of file 19845230Sab196087 */ 19855230Sab196087 static void 19865230Sab196087 dyn_test(dyn_test_t test_type, Word sh_type, Cache *sec_cache, Dyn *dyn, 19875230Sab196087 Word dynsec_cnt, Ehdr *ehdr, const char *file) 19885230Sab196087 { 19895230Sab196087 Conv_inv_buf_t buf1, buf2; 19905230Sab196087 19915230Sab196087 /* 19925230Sab196087 * These tests are based around the implicit assumption that 19935230Sab196087 * there is only one dynamic section in an object, and also only 19945230Sab196087 * one of the sections it references. We have therefore gathered 19955230Sab196087 * all of the necessary information to test this in a single pass 19965230Sab196087 * over the section headers, which is very efficient. We are not 19975230Sab196087 * aware of any case where more than one dynamic section would 19985230Sab196087 * be meaningful in an ELF object, so this is a reasonable solution. 19995230Sab196087 * 20005230Sab196087 * To test multiple dynamic sections correctly would be more 20015230Sab196087 * expensive in code and time. We would have to build a data structure 20025230Sab196087 * containing all the dynamic elements. Then, we would use the address 20035230Sab196087 * to locate the section it references and ensure the section is of 20045230Sab196087 * the right type and that the address in the dynamic element is 20055230Sab196087 * to the start of the section. Then, we could check the size and 20065230Sab196087 * entsize values against those same sections. This is O(n^2), and 20075230Sab196087 * also complicated. 20085230Sab196087 * 20095230Sab196087 * In the highly unlikely case that there is more than one dynamic 20105230Sab196087 * section, we only test the first one, and simply allow the values 20115230Sab196087 * of the subsequent one to be displayed unchallenged. 20125230Sab196087 */ 20135230Sab196087 if (dynsec_cnt != 1) 20145230Sab196087 return; 20155230Sab196087 20165230Sab196087 /* 20175230Sab196087 * A DT_ item that references a section address should always find 20185230Sab196087 * the section in the file. 20195230Sab196087 */ 20205230Sab196087 if (sec_cache == NULL) { 20215230Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_DYNNOBCKSEC), file, 20225230Sab196087 conv_sec_type(ehdr->e_machine, sh_type, 0, &buf1), 20235230Sab196087 conv_dyn_tag(dyn->d_tag, ehdr->e_machine, 0, &buf2)); 20245230Sab196087 return; 20255230Sab196087 } 20265230Sab196087 20275230Sab196087 20285230Sab196087 switch (test_type) { 20295230Sab196087 case DYN_TEST_ADDR: 20305230Sab196087 /* The section address should match the DT_ item value */ 20315230Sab196087 if (dyn->d_un.d_val != sec_cache->c_shdr->sh_addr) 20325230Sab196087 (void) fprintf(stderr, 20335230Sab196087 MSG_INTL(MSG_ERR_DYNBADADDR), file, 20345230Sab196087 conv_dyn_tag(dyn->d_tag, ehdr->e_machine, 0, &buf1), 20355230Sab196087 EC_ADDR(dyn->d_un.d_val), sec_cache->c_ndx, 20365230Sab196087 sec_cache->c_name, 20375230Sab196087 EC_ADDR(sec_cache->c_shdr->sh_addr)); 20385230Sab196087 break; 20395230Sab196087 20405230Sab196087 case DYN_TEST_SIZE: 20415230Sab196087 /* The section size should match the DT_ item value */ 20425230Sab196087 if (dyn->d_un.d_val != sec_cache->c_shdr->sh_size) 20435230Sab196087 (void) fprintf(stderr, 20445230Sab196087 MSG_INTL(MSG_ERR_DYNBADSIZE), file, 20455230Sab196087 conv_dyn_tag(dyn->d_tag, ehdr->e_machine, 0, &buf1), 20465230Sab196087 EC_XWORD(dyn->d_un.d_val), 20475230Sab196087 sec_cache->c_ndx, sec_cache->c_name, 20485230Sab196087 EC_XWORD(sec_cache->c_shdr->sh_size)); 20495230Sab196087 break; 20505230Sab196087 20515230Sab196087 case DYN_TEST_ENTSIZE: 20525230Sab196087 /* The sh_entsize value should match the DT_ item value */ 20535230Sab196087 if (dyn->d_un.d_val != sec_cache->c_shdr->sh_entsize) 20545230Sab196087 (void) fprintf(stderr, 20555230Sab196087 MSG_INTL(MSG_ERR_DYNBADENTSIZE), file, 20565230Sab196087 conv_dyn_tag(dyn->d_tag, ehdr->e_machine, 0, &buf1), 20575230Sab196087 EC_XWORD(dyn->d_un.d_val), 20585230Sab196087 sec_cache->c_ndx, sec_cache->c_name, 20595230Sab196087 EC_XWORD(sec_cache->c_shdr->sh_entsize)); 20605230Sab196087 break; 20615230Sab196087 } 20625230Sab196087 } 20635230Sab196087 20645230Sab196087 20650Sstevel@tonic-gate /* 20660Sstevel@tonic-gate * Search for and process a .dynamic section. 20670Sstevel@tonic-gate */ 20680Sstevel@tonic-gate static void 20691618Srie dynamic(Cache *cache, Word shnum, Ehdr *ehdr, const char *file) 20700Sstevel@tonic-gate { 20715230Sab196087 struct { 20725230Sab196087 Cache *dynstr; 20735230Sab196087 Cache *dynsym; 20745230Sab196087 Cache *hash; 20755230Sab196087 Cache *fini; 20765230Sab196087 Cache *fini_array; 20775230Sab196087 Cache *init; 20785230Sab196087 Cache *init_array; 20795230Sab196087 Cache *preinit_array; 20805230Sab196087 Cache *rel; 20815230Sab196087 Cache *rela; 20825230Sab196087 Cache *sunw_cap; 20835230Sab196087 Cache *sunw_ldynsym; 20845230Sab196087 Cache *sunw_move; 20855230Sab196087 Cache *sunw_syminfo; 20865230Sab196087 Cache *sunw_symsort; 20875230Sab196087 Cache *sunw_tlssort; 20885230Sab196087 Cache *sunw_verdef; 20895230Sab196087 Cache *sunw_verneed; 20905230Sab196087 Cache *sunw_versym; 20915230Sab196087 } sec; 20925230Sab196087 Word dynsec_ndx; 20935230Sab196087 Word dynsec_num; 20945230Sab196087 int dynsec_cnt; 20951618Srie Word cnt; 20960Sstevel@tonic-gate 20975230Sab196087 /* 20985230Sab196087 * Make a pass over all the sections, gathering section information 20995230Sab196087 * we'll need below. 21005230Sab196087 */ 21015230Sab196087 dynsec_num = 0; 21025230Sab196087 bzero(&sec, sizeof (sec)); 21030Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 21045230Sab196087 Cache *_cache = &cache[cnt]; 21055230Sab196087 21065230Sab196087 switch (_cache->c_shdr->sh_type) { 21075230Sab196087 case SHT_DYNAMIC: 21085230Sab196087 if (dynsec_num == 0) { 21095230Sab196087 dynsec_ndx = cnt; 21105230Sab196087 21115230Sab196087 /* Does it have a valid string table? */ 21125230Sab196087 (void) stringtbl(cache, 0, cnt, shnum, file, 21135230Sab196087 0, 0, &sec.dynstr); 21145230Sab196087 } 21155230Sab196087 dynsec_num++; 21165230Sab196087 break; 21175230Sab196087 21185230Sab196087 21195230Sab196087 case SHT_PROGBITS: 21205230Sab196087 /* 21215230Sab196087 * We want to detect the .init and .fini sections, 21225230Sab196087 * if present. These are SHT_PROGBITS, so all we 21235230Sab196087 * have to go on is the section name. Normally comparing 21245230Sab196087 * names is a bad idea, but there are some special 21255230Sab196087 * names (i.e. .init/.fini/.interp) that are very 21265230Sab196087 * difficult to use in any other context, and for 21275230Sab196087 * these symbols, we do the heuristic match. 21285230Sab196087 */ 21295230Sab196087 if (strcmp(_cache->c_name, 21305230Sab196087 MSG_ORIG(MSG_ELF_INIT)) == 0) { 21315230Sab196087 if (sec.init == NULL) 21325230Sab196087 sec.init = _cache; 21335230Sab196087 } else if (strcmp(_cache->c_name, 21345230Sab196087 MSG_ORIG(MSG_ELF_FINI)) == 0) { 21355230Sab196087 if (sec.fini == NULL) 21365230Sab196087 sec.fini = _cache; 21375230Sab196087 } 21385230Sab196087 break; 21395230Sab196087 21405230Sab196087 case SHT_REL: 21415230Sab196087 /* 21425230Sab196087 * We want the SHT_REL section with the lowest 21435230Sab196087 * offset. The linker gathers them together, 21445230Sab196087 * and puts the address of the first one 21455230Sab196087 * into the DT_REL dynamic element. 21465230Sab196087 */ 21475230Sab196087 if ((sec.rel == NULL) || 21485230Sab196087 (_cache->c_shdr->sh_offset < 21495230Sab196087 sec.rel->c_shdr->sh_offset)) 21505230Sab196087 sec.rel = _cache; 21515230Sab196087 break; 21525230Sab196087 21535230Sab196087 case SHT_RELA: 21545230Sab196087 /* RELA is handled just like RELA above */ 21555230Sab196087 if ((sec.rela == NULL) || 21565230Sab196087 (_cache->c_shdr->sh_offset < 21575230Sab196087 sec.rela->c_shdr->sh_offset)) 21585230Sab196087 sec.rela = _cache; 21595230Sab196087 break; 21605230Sab196087 21615230Sab196087 /* 21625230Sab196087 * The GRAB macro is used for the simple case in which 21635230Sab196087 * we simply grab the first section of the desired type. 21645230Sab196087 */ 21655230Sab196087 #define GRAB(_sec_type, _sec_field) \ 21665230Sab196087 case _sec_type: \ 21675230Sab196087 if (sec._sec_field == NULL) \ 21685230Sab196087 sec._sec_field = _cache; \ 21695230Sab196087 break 21705230Sab196087 GRAB(SHT_DYNSYM, dynsym); 21715230Sab196087 GRAB(SHT_FINI_ARRAY, fini_array); 21725230Sab196087 GRAB(SHT_HASH, hash); 21735230Sab196087 GRAB(SHT_INIT_ARRAY, init_array); 21745230Sab196087 GRAB(SHT_SUNW_move, sunw_move); 21755230Sab196087 GRAB(SHT_PREINIT_ARRAY, preinit_array); 21765230Sab196087 GRAB(SHT_SUNW_cap, sunw_cap); 21775230Sab196087 GRAB(SHT_SUNW_LDYNSYM, sunw_ldynsym); 21785230Sab196087 GRAB(SHT_SUNW_syminfo, sunw_syminfo); 21795230Sab196087 GRAB(SHT_SUNW_symsort, sunw_symsort); 21805230Sab196087 GRAB(SHT_SUNW_tlssort, sunw_tlssort); 21815230Sab196087 GRAB(SHT_SUNW_verdef, sunw_verdef); 21825230Sab196087 GRAB(SHT_SUNW_verneed, sunw_verneed); 21835230Sab196087 GRAB(SHT_SUNW_versym, sunw_versym); 21845230Sab196087 #undef GRAB 21855230Sab196087 } 21865230Sab196087 } 21875230Sab196087 21885230Sab196087 /* 21895230Sab196087 * If no dynamic section, return immediately. If more than one 21905230Sab196087 * dynamic section, then something odd is going on and an error 21915230Sab196087 * is in order, but then continue on and display them all. 21925230Sab196087 */ 21935230Sab196087 if (dynsec_num == 0) 21945230Sab196087 return; 21955230Sab196087 if (dynsec_num > 1) 21965230Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_MULTDYN), 21975230Sab196087 file, EC_WORD(dynsec_num)); 21985230Sab196087 21995230Sab196087 22005230Sab196087 dynsec_cnt = 0; 22015230Sab196087 for (cnt = dynsec_ndx; (cnt < shnum) && (dynsec_cnt < dynsec_num); 22025230Sab196087 cnt++) { 22031618Srie Dyn *dyn; 22041618Srie ulong_t numdyn; 22053850Sab196087 int ndx, end_ndx; 22061618Srie Cache *_cache = &cache[cnt], *strsec; 22071618Srie Shdr *shdr = _cache->c_shdr; 22085230Sab196087 int dumped = 0; 22090Sstevel@tonic-gate 22100Sstevel@tonic-gate if (shdr->sh_type != SHT_DYNAMIC) 22110Sstevel@tonic-gate continue; 22125230Sab196087 dynsec_cnt++; 22130Sstevel@tonic-gate 22140Sstevel@tonic-gate /* 22151618Srie * Verify the associated string table section. 22160Sstevel@tonic-gate */ 22171618Srie if (stringtbl(cache, 0, cnt, shnum, file, 0, 0, &strsec) == 0) 22180Sstevel@tonic-gate continue; 22191618Srie 22203466Srie if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) { 22213466Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 22223466Srie file, _cache->c_name); 22233466Srie continue; 22243466Srie } 22253466Srie if (_cache->c_data == NULL) 22263466Srie continue; 22273466Srie 22280Sstevel@tonic-gate numdyn = shdr->sh_size / shdr->sh_entsize; 22291618Srie dyn = (Dyn *)_cache->c_data->d_buf; 22300Sstevel@tonic-gate 22315230Sab196087 /* 22325230Sab196087 * We expect the REL/RELA entries to reference the reloc 22335230Sab196087 * section with the lowest address. However, this is 22345230Sab196087 * not true for dumped objects. Detect if this object has 22355230Sab196087 * been dumped so that we can skip the reloc address test 22365230Sab196087 * in that case. 22375230Sab196087 */ 22385230Sab196087 for (ndx = 0; ndx < numdyn; dyn++, ndx++) { 22395230Sab196087 if (dyn->d_tag == DT_FLAGS_1) { 22405230Sab196087 dumped = (dyn->d_un.d_val & DF_1_CONFALT) != 0; 22415230Sab196087 break; 22425230Sab196087 } 22435230Sab196087 } 22445230Sab196087 dyn = (Dyn *)_cache->c_data->d_buf; 22455230Sab196087 22461618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 22471618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_DYNAMIC), _cache->c_name); 22480Sstevel@tonic-gate 22491618Srie Elf_dyn_title(0); 22500Sstevel@tonic-gate 22511618Srie for (ndx = 0; ndx < numdyn; dyn++, ndx++) { 22524734Sab196087 union { 22534734Sab196087 Conv_dyn_flag_buf_t flag; 22544734Sab196087 Conv_dyn_flag1_buf_t flag1; 22554734Sab196087 Conv_dyn_posflag1_buf_t posflag1; 22564734Sab196087 Conv_dyn_feature1_buf_t feature1; 22574734Sab196087 } c_buf; 22585230Sab196087 const char *name = NULL; 22590Sstevel@tonic-gate 22600Sstevel@tonic-gate /* 22610Sstevel@tonic-gate * Print the information numerically, and if possible 22625230Sab196087 * as a string. If a string is available, name is 22635230Sab196087 * set to reference it. 22645230Sab196087 * 22655230Sab196087 * Also, take this opportunity to sanity check 22665230Sab196087 * the values of DT elements. In the code above, 22675230Sab196087 * we gathered information on sections that are 22685230Sab196087 * referenced by the dynamic section. Here, we 22695230Sab196087 * compare the attributes of those sections to 22705230Sab196087 * the DT_ items that reference them and report 22715230Sab196087 * on inconsistencies. 22725230Sab196087 * 22735230Sab196087 * Things not currently tested that could be improved 22745230Sab196087 * in later revisions include: 22755230Sab196087 * - We don't check PLT or GOT related items 22765230Sab196087 * - We don't handle computing the lengths of 22775230Sab196087 * relocation arrays. To handle this 22785230Sab196087 * requires examining data that spans 22795230Sab196087 * across sections, in a contiguous span 22805230Sab196087 * within a single segment. 22815230Sab196087 * - DT_VERDEFNUM and DT_VERNEEDNUM can't be 22825230Sab196087 * verified without parsing the sections. 22835230Sab196087 * - We don't handle DT_SUNW_SYMSZ, which would 22845230Sab196087 * be the sum of the lengths of .dynsym and 22855230Sab196087 * .SUNW_ldynsym 22865230Sab196087 * - DT_SUNW_STRPAD can't be verified other than 22875230Sab196087 * to check that it's not larger than 22885230Sab196087 * the string table. 22895230Sab196087 * - Some items come in "all or none" clusters 22905230Sab196087 * that give an address, element size, 22915230Sab196087 * and data length in bytes. We don't 22925230Sab196087 * verify that there are no missing items 22935230Sab196087 * in such groups. 22940Sstevel@tonic-gate */ 22953850Sab196087 switch (dyn->d_tag) { 22963850Sab196087 case DT_NULL: 22973850Sab196087 /* 22983850Sab196087 * Special case: DT_NULLs can come in groups 22993850Sab196087 * that we prefer to reduce to a single line. 23003850Sab196087 */ 23013850Sab196087 end_ndx = ndx; 23023850Sab196087 while ((end_ndx < (numdyn - 1)) && 23034433Sab196087 ((dyn + 1)->d_tag == DT_NULL)) { 23043850Sab196087 dyn++; 23053850Sab196087 end_ndx++; 23063850Sab196087 } 23073850Sab196087 Elf_dyn_null_entry(0, dyn, ndx, end_ndx); 23083850Sab196087 ndx = end_ndx; 23093850Sab196087 continue; 23103850Sab196087 23113850Sab196087 /* 23125230Sab196087 * String items all reference the dynstr. The string() 23135230Sab196087 * function does the necessary sanity checking. 23143850Sab196087 */ 23153850Sab196087 case DT_NEEDED: 23163850Sab196087 case DT_SONAME: 23173850Sab196087 case DT_FILTER: 23183850Sab196087 case DT_AUXILIARY: 23193850Sab196087 case DT_CONFIG: 23203850Sab196087 case DT_RPATH: 23213850Sab196087 case DT_RUNPATH: 23223850Sab196087 case DT_USED: 23233850Sab196087 case DT_DEPAUDIT: 23243850Sab196087 case DT_AUDIT: 23253850Sab196087 case DT_SUNW_AUXILIARY: 23263850Sab196087 case DT_SUNW_FILTER: 23271618Srie name = string(_cache, ndx, strsec, 23281618Srie file, dyn->d_un.d_ptr); 23293850Sab196087 break; 23303850Sab196087 23313850Sab196087 case DT_FLAGS: 23324734Sab196087 name = conv_dyn_flag(dyn->d_un.d_val, 23334734Sab196087 0, &c_buf.flag); 23343850Sab196087 break; 23353850Sab196087 case DT_FLAGS_1: 23365088Sab196087 name = conv_dyn_flag1(dyn->d_un.d_val, 0, 23374734Sab196087 &c_buf.flag1); 23383850Sab196087 break; 23393850Sab196087 case DT_POSFLAG_1: 23404734Sab196087 name = conv_dyn_posflag1(dyn->d_un.d_val, 0, 23414734Sab196087 &c_buf.posflag1); 23423850Sab196087 break; 23433850Sab196087 case DT_FEATURE_1: 23444734Sab196087 name = conv_dyn_feature1(dyn->d_un.d_val, 0, 23454734Sab196087 &c_buf.feature1); 23463850Sab196087 break; 23473850Sab196087 case DT_DEPRECATED_SPARC_REGISTER: 23481618Srie name = MSG_INTL(MSG_STR_DEPRECATED); 23493850Sab196087 break; 23505230Sab196087 23515230Sab196087 /* 23525230Sab196087 * Cases below this point are strictly sanity checking, 23535230Sab196087 * and do not generate a name string. The TEST_ macros 23545230Sab196087 * are used to hide the boilerplate arguments neeeded 23555230Sab196087 * by dyn_test(). 23565230Sab196087 */ 23575230Sab196087 #define TEST_ADDR(_sh_type, _sec_field) \ 23585230Sab196087 dyn_test(DYN_TEST_ADDR, _sh_type, \ 23595230Sab196087 sec._sec_field, dyn, dynsec_cnt, ehdr, file) 23605230Sab196087 #define TEST_SIZE(_sh_type, _sec_field) \ 23615230Sab196087 dyn_test(DYN_TEST_SIZE, _sh_type, \ 23625230Sab196087 sec._sec_field, dyn, dynsec_cnt, ehdr, file) 23635230Sab196087 #define TEST_ENTSIZE(_sh_type, _sec_field) \ 23645230Sab196087 dyn_test(DYN_TEST_ENTSIZE, _sh_type, \ 23655230Sab196087 sec._sec_field, dyn, dynsec_cnt, ehdr, file) 23665230Sab196087 23675230Sab196087 case DT_FINI: 23685230Sab196087 TEST_ADDR(SHT_PROGBITS, fini); 23695230Sab196087 break; 23705230Sab196087 23715230Sab196087 case DT_FINI_ARRAY: 23725230Sab196087 TEST_ADDR(SHT_FINI_ARRAY, fini_array); 23735230Sab196087 break; 23745230Sab196087 23755230Sab196087 case DT_FINI_ARRAYSZ: 23765230Sab196087 TEST_SIZE(SHT_FINI_ARRAY, fini_array); 23775230Sab196087 break; 23785230Sab196087 23795230Sab196087 case DT_HASH: 23805230Sab196087 TEST_ADDR(SHT_HASH, hash); 23815230Sab196087 break; 23825230Sab196087 23835230Sab196087 case DT_INIT: 23845230Sab196087 TEST_ADDR(SHT_PROGBITS, init); 23855230Sab196087 break; 23865230Sab196087 23875230Sab196087 case DT_INIT_ARRAY: 23885230Sab196087 TEST_ADDR(SHT_INIT_ARRAY, init_array); 23895230Sab196087 break; 23905230Sab196087 23915230Sab196087 case DT_INIT_ARRAYSZ: 23925230Sab196087 TEST_SIZE(SHT_INIT_ARRAY, init_array); 23935230Sab196087 break; 23945230Sab196087 23955230Sab196087 case DT_MOVEENT: 23965230Sab196087 TEST_ENTSIZE(SHT_SUNW_move, sunw_move); 23975230Sab196087 break; 23985230Sab196087 23995230Sab196087 case DT_MOVESZ: 24005230Sab196087 TEST_SIZE(SHT_SUNW_move, sunw_move); 24015230Sab196087 break; 24025230Sab196087 24035230Sab196087 case DT_MOVETAB: 24045230Sab196087 TEST_ADDR(SHT_SUNW_move, sunw_move); 24055230Sab196087 break; 24065230Sab196087 24075230Sab196087 case DT_PREINIT_ARRAY: 24085230Sab196087 TEST_ADDR(SHT_PREINIT_ARRAY, preinit_array); 24095230Sab196087 break; 24105230Sab196087 24115230Sab196087 case DT_PREINIT_ARRAYSZ: 24125230Sab196087 TEST_SIZE(SHT_PREINIT_ARRAY, preinit_array); 24135230Sab196087 break; 24145230Sab196087 24155230Sab196087 case DT_REL: 24165230Sab196087 if (!dumped) 24175230Sab196087 TEST_ADDR(SHT_REL, rel); 24185230Sab196087 break; 24195230Sab196087 24205230Sab196087 case DT_RELENT: 24215230Sab196087 TEST_ENTSIZE(SHT_REL, rel); 24225230Sab196087 break; 24235230Sab196087 24245230Sab196087 case DT_RELA: 24255230Sab196087 if (!dumped) 24265230Sab196087 TEST_ADDR(SHT_RELA, rela); 24275230Sab196087 break; 24285230Sab196087 24295230Sab196087 case DT_RELAENT: 24305230Sab196087 TEST_ENTSIZE(SHT_RELA, rela); 24315230Sab196087 break; 24325230Sab196087 24335230Sab196087 case DT_STRTAB: 24345230Sab196087 TEST_ADDR(SHT_STRTAB, dynstr); 24353850Sab196087 break; 24365230Sab196087 24375230Sab196087 case DT_STRSZ: 24385230Sab196087 TEST_SIZE(SHT_STRTAB, dynstr); 24395230Sab196087 break; 24405230Sab196087 24415230Sab196087 case DT_SUNW_CAP: 24425230Sab196087 TEST_ADDR(SHT_SUNW_cap, sunw_cap); 24435230Sab196087 break; 24445230Sab196087 24455230Sab196087 case DT_SUNW_SYMTAB: 24465230Sab196087 TEST_ADDR(SHT_SUNW_LDYNSYM, sunw_ldynsym); 24475230Sab196087 break; 24485230Sab196087 24495230Sab196087 case DT_SYMENT: 24505230Sab196087 TEST_ENTSIZE(SHT_DYNSYM, dynsym); 24515230Sab196087 break; 24525230Sab196087 24535230Sab196087 case DT_SYMINENT: 24545230Sab196087 TEST_ENTSIZE(SHT_SUNW_syminfo, sunw_syminfo); 24555230Sab196087 break; 24565230Sab196087 24575230Sab196087 case DT_SYMINFO: 24585230Sab196087 TEST_ADDR(SHT_SUNW_syminfo, sunw_syminfo); 24595230Sab196087 break; 24605230Sab196087 24615230Sab196087 case DT_SYMINSZ: 24625230Sab196087 TEST_SIZE(SHT_SUNW_syminfo, sunw_syminfo); 24635230Sab196087 break; 24645230Sab196087 24655230Sab196087 case DT_SYMTAB: 24665230Sab196087 TEST_ADDR(SHT_DYNSYM, dynsym); 24675230Sab196087 break; 24685230Sab196087 24695230Sab196087 case DT_SUNW_SORTENT: 24705230Sab196087 /* 24715230Sab196087 * This entry is related to both the symsort and 24725230Sab196087 * tlssort sections. 24735230Sab196087 */ 24745230Sab196087 { 24755230Sab196087 int test_tls = 24765230Sab196087 (sec.sunw_tlssort != NULL); 24775230Sab196087 int test_sym = 24785230Sab196087 (sec.sunw_symsort != NULL) || 24795230Sab196087 !test_tls; 24805230Sab196087 if (test_sym) 24815230Sab196087 TEST_ENTSIZE(SHT_SUNW_symsort, 24825230Sab196087 sunw_symsort); 24835230Sab196087 if (test_tls) 24845230Sab196087 TEST_ENTSIZE(SHT_SUNW_tlssort, 24855230Sab196087 sunw_tlssort); 24865230Sab196087 } 24875230Sab196087 break; 24885230Sab196087 24895230Sab196087 24905230Sab196087 case DT_SUNW_SYMSORT: 24915230Sab196087 TEST_ADDR(SHT_SUNW_symsort, sunw_symsort); 24925230Sab196087 break; 24935230Sab196087 24945230Sab196087 case DT_SUNW_SYMSORTSZ: 24955230Sab196087 TEST_SIZE(SHT_SUNW_symsort, sunw_symsort); 24965230Sab196087 break; 24975230Sab196087 24985230Sab196087 case DT_SUNW_TLSSORT: 24995230Sab196087 TEST_ADDR(SHT_SUNW_tlssort, sunw_tlssort); 25005230Sab196087 break; 25015230Sab196087 25025230Sab196087 case DT_SUNW_TLSSORTSZ: 25035230Sab196087 TEST_SIZE(SHT_SUNW_tlssort, sunw_tlssort); 25045230Sab196087 break; 25055230Sab196087 25065230Sab196087 case DT_VERDEF: 25075230Sab196087 TEST_ADDR(SHT_SUNW_verdef, sunw_verdef); 25085230Sab196087 break; 25095230Sab196087 25105230Sab196087 case DT_VERNEED: 25115230Sab196087 TEST_ADDR(SHT_SUNW_verneed, sunw_verneed); 25125230Sab196087 break; 25135230Sab196087 25145230Sab196087 case DT_VERSYM: 25155230Sab196087 TEST_ADDR(SHT_SUNW_versym, sunw_versym); 25165230Sab196087 break; 25175230Sab196087 #undef TEST_ADDR 25185230Sab196087 #undef TEST_SIZE 25195230Sab196087 #undef TEST_ENTSIZE 25203850Sab196087 } 25210Sstevel@tonic-gate 25225230Sab196087 if (name == NULL) 25235230Sab196087 name = MSG_ORIG(MSG_STR_EMPTY); 25241618Srie Elf_dyn_entry(0, dyn, ndx, name, ehdr->e_machine); 25250Sstevel@tonic-gate } 25260Sstevel@tonic-gate } 25270Sstevel@tonic-gate } 25280Sstevel@tonic-gate 25290Sstevel@tonic-gate /* 25300Sstevel@tonic-gate * Search for and process a MOVE section. 25310Sstevel@tonic-gate */ 25320Sstevel@tonic-gate static void 25334168Sab196087 move(Cache *cache, Word shnum, const char *file, uint_t flags) 25340Sstevel@tonic-gate { 25351618Srie Word cnt; 25361618Srie const char *fmt = 0; 25370Sstevel@tonic-gate 25380Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 25391618Srie Word movenum, symnum, ndx; 25401618Srie Sym *syms; 25411618Srie Cache *_cache = &cache[cnt]; 25421618Srie Shdr *shdr = _cache->c_shdr; 25431618Srie Cache *symsec, *strsec; 25441618Srie Move *move; 25450Sstevel@tonic-gate 25460Sstevel@tonic-gate if (shdr->sh_type != SHT_SUNW_move) 25470Sstevel@tonic-gate continue; 2548*5411Sab196087 if (!match(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type)) 25490Sstevel@tonic-gate continue; 25500Sstevel@tonic-gate 25510Sstevel@tonic-gate /* 25520Sstevel@tonic-gate * Determine the move data and number. 25530Sstevel@tonic-gate */ 25540Sstevel@tonic-gate if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) { 25550Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 25560Sstevel@tonic-gate file, _cache->c_name); 25570Sstevel@tonic-gate continue; 25580Sstevel@tonic-gate } 25593466Srie if (_cache->c_data == NULL) 25603466Srie continue; 25613466Srie 25621618Srie move = (Move *)_cache->c_data->d_buf; 25631618Srie movenum = shdr->sh_size / shdr->sh_entsize; 25640Sstevel@tonic-gate 25650Sstevel@tonic-gate /* 25661618Srie * Get the data buffer for the associated symbol table and 25671618Srie * string table. 25680Sstevel@tonic-gate */ 25691618Srie if (stringtbl(cache, 1, cnt, shnum, file, 25701618Srie &symnum, &symsec, &strsec) == 0) 25711618Srie return; 25721618Srie 25731618Srie syms = (Sym *)symsec->c_data->d_buf; 25740Sstevel@tonic-gate 25751618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 25761618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_MOVE), _cache->c_name); 25771618Srie dbg_print(0, MSG_INTL(MSG_MOVE_TITLE)); 25780Sstevel@tonic-gate 25791618Srie if (fmt == 0) 25801618Srie fmt = MSG_INTL(MSG_MOVE_ENTRY); 25810Sstevel@tonic-gate 25821618Srie for (ndx = 0; ndx < movenum; move++, ndx++) { 25831618Srie const char *symname; 25841618Srie char index[MAXNDXSIZE], section[BUFSIZ]; 25851618Srie Word symndx, shndx; 25861618Srie Sym *sym; 25870Sstevel@tonic-gate 25880Sstevel@tonic-gate /* 25890Sstevel@tonic-gate * Check for null entries 25900Sstevel@tonic-gate */ 25911618Srie if ((move->m_info == 0) && (move->m_value == 0) && 25921618Srie (move->m_poffset == 0) && (move->m_repeat == 0) && 25931618Srie (move->m_stride == 0)) { 25941618Srie dbg_print(0, fmt, MSG_ORIG(MSG_STR_EMPTY), 25951618Srie EC_XWORD(move->m_poffset), 0, 0, 0, 25961618Srie EC_LWORD(0), MSG_ORIG(MSG_STR_EMPTY)); 25970Sstevel@tonic-gate continue; 25980Sstevel@tonic-gate } 25991618Srie if (((symndx = ELF_M_SYM(move->m_info)) == 0) || 26001618Srie (symndx >= symnum)) { 26010Sstevel@tonic-gate (void) fprintf(stderr, 26020Sstevel@tonic-gate MSG_INTL(MSG_ERR_BADMINFO), file, 26031618Srie _cache->c_name, EC_XWORD(move->m_info)); 26041618Srie 26051618Srie (void) snprintf(index, MAXNDXSIZE, 26061618Srie MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx)); 26071618Srie dbg_print(0, fmt, index, 26081618Srie EC_XWORD(move->m_poffset), 26091618Srie ELF_M_SIZE(move->m_info), move->m_repeat, 26101618Srie move->m_stride, move->m_value, 26110Sstevel@tonic-gate MSG_INTL(MSG_STR_UNKNOWN)); 26120Sstevel@tonic-gate continue; 26130Sstevel@tonic-gate } 26140Sstevel@tonic-gate 26151618Srie symname = relsymname(cache, _cache, strsec, 26161618Srie symndx, symnum, ndx, syms, section, BUFSIZ, file, 26171618Srie flags); 26181618Srie sym = (Sym *)(syms + symndx); 26190Sstevel@tonic-gate 26200Sstevel@tonic-gate /* 26210Sstevel@tonic-gate * Additional sanity check. 26220Sstevel@tonic-gate */ 26231618Srie shndx = sym->st_shndx; 26240Sstevel@tonic-gate if (!((shndx == SHN_COMMON) || 26250Sstevel@tonic-gate (((shndx >= 1) && (shndx <= shnum)) && 26261618Srie (cache[shndx].c_shdr)->sh_type == SHT_NOBITS))) { 26270Sstevel@tonic-gate (void) fprintf(stderr, 26281618Srie MSG_INTL(MSG_ERR_BADSYM2), file, 26291618Srie _cache->c_name, demangle(symname, flags)); 26300Sstevel@tonic-gate } 26310Sstevel@tonic-gate 26321618Srie (void) snprintf(index, MAXNDXSIZE, 26331618Srie MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx)); 26341618Srie dbg_print(0, fmt, index, EC_XWORD(move->m_poffset), 26351618Srie ELF_M_SIZE(move->m_info), move->m_repeat, 26361618Srie move->m_stride, move->m_value, 26371618Srie demangle(symname, flags)); 26380Sstevel@tonic-gate } 26390Sstevel@tonic-gate } 26400Sstevel@tonic-gate } 26410Sstevel@tonic-gate 26420Sstevel@tonic-gate /* 26430Sstevel@tonic-gate * Traverse a note section analyzing each note information block. 26440Sstevel@tonic-gate * The data buffers size is used to validate references before they are made, 26450Sstevel@tonic-gate * and is decremented as each element is processed. 26460Sstevel@tonic-gate */ 26470Sstevel@tonic-gate void 26481618Srie note_entry(Cache *cache, Word *data, size_t size, const char *file) 26490Sstevel@tonic-gate { 26501618Srie size_t bsize = size; 26511618Srie 26520Sstevel@tonic-gate /* 26530Sstevel@tonic-gate * Print out a single `note' information block. 26540Sstevel@tonic-gate */ 26550Sstevel@tonic-gate while (size > 0) { 26561618Srie size_t namesz, descsz, type, pad, noteoff; 26570Sstevel@tonic-gate 26580Sstevel@tonic-gate noteoff = bsize - size; 26590Sstevel@tonic-gate /* 26600Sstevel@tonic-gate * Make sure we can at least reference the 3 initial entries 26610Sstevel@tonic-gate * (4-byte words) of the note information block. 26620Sstevel@tonic-gate */ 26631618Srie if (size >= (sizeof (Word) * 3)) 26641618Srie size -= (sizeof (Word) * 3); 26650Sstevel@tonic-gate else { 26661618Srie (void) fprintf(stderr, MSG_INTL(MSG_NOTE_BADDATASZ), 26671618Srie file, cache->c_name, EC_WORD(noteoff)); 26680Sstevel@tonic-gate return; 26690Sstevel@tonic-gate } 26700Sstevel@tonic-gate 26710Sstevel@tonic-gate /* 26720Sstevel@tonic-gate * Make sure any specified name string can be referenced. 26730Sstevel@tonic-gate */ 26740Sstevel@tonic-gate if ((namesz = *data++) != 0) { 26750Sstevel@tonic-gate if (size >= namesz) 26760Sstevel@tonic-gate size -= namesz; 26770Sstevel@tonic-gate else { 26780Sstevel@tonic-gate (void) fprintf(stderr, 26791618Srie MSG_INTL(MSG_NOTE_BADNMSZ), file, 26801618Srie cache->c_name, EC_WORD(noteoff), 26811618Srie EC_WORD(namesz)); 26820Sstevel@tonic-gate return; 26830Sstevel@tonic-gate } 26840Sstevel@tonic-gate } 26851618Srie 26860Sstevel@tonic-gate /* 26870Sstevel@tonic-gate * Make sure any specified descriptor can be referenced. 26880Sstevel@tonic-gate */ 26890Sstevel@tonic-gate if ((descsz = *data++) != 0) { 26900Sstevel@tonic-gate /* 26910Sstevel@tonic-gate * If namesz isn't a 4-byte multiple, account for any 26920Sstevel@tonic-gate * padding that must exist before the descriptor. 26930Sstevel@tonic-gate */ 26941618Srie if ((pad = (namesz & (sizeof (Word) - 1))) != 0) { 26951618Srie pad = sizeof (Word) - pad; 26960Sstevel@tonic-gate size -= pad; 26970Sstevel@tonic-gate } 26980Sstevel@tonic-gate if (size >= descsz) 26990Sstevel@tonic-gate size -= descsz; 27000Sstevel@tonic-gate else { 27010Sstevel@tonic-gate (void) fprintf(stderr, 27021618Srie MSG_INTL(MSG_NOTE_BADDESZ), file, 27031618Srie cache->c_name, EC_WORD(noteoff), 27041618Srie EC_WORD(namesz)); 27050Sstevel@tonic-gate return; 27060Sstevel@tonic-gate } 27070Sstevel@tonic-gate } 27080Sstevel@tonic-gate 27090Sstevel@tonic-gate type = *data++; 27100Sstevel@tonic-gate 27111618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 27121618Srie dbg_print(0, MSG_ORIG(MSG_NOTE_TYPE), EC_WORD(type)); 27130Sstevel@tonic-gate 27141618Srie dbg_print(0, MSG_ORIG(MSG_NOTE_NAMESZ), EC_WORD(namesz)); 27150Sstevel@tonic-gate if (namesz) { 27160Sstevel@tonic-gate char *name = (char *)data; 27170Sstevel@tonic-gate 27180Sstevel@tonic-gate /* 27190Sstevel@tonic-gate * Since the name string may have 'null' bytes 27200Sstevel@tonic-gate * in it (ia32 .string) - we just write the 27210Sstevel@tonic-gate * whole stream in a single fwrite. 27220Sstevel@tonic-gate */ 27230Sstevel@tonic-gate (void) fwrite(name, namesz, 1, stdout); 27240Sstevel@tonic-gate name = name + ((namesz + (sizeof (Word) - 1)) & 27250Sstevel@tonic-gate ~(sizeof (Word) - 1)); 27260Sstevel@tonic-gate /* LINTED */ 27270Sstevel@tonic-gate data = (Word *)name; 27281618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 27290Sstevel@tonic-gate } 27300Sstevel@tonic-gate 27310Sstevel@tonic-gate /* 27320Sstevel@tonic-gate * If multiple information blocks exist within a .note section 27330Sstevel@tonic-gate * account for any padding that must exist before the next 27340Sstevel@tonic-gate * information block. 27350Sstevel@tonic-gate */ 27361618Srie if ((pad = (descsz & (sizeof (Word) - 1))) != 0) { 27371618Srie pad = sizeof (Word) - pad; 27380Sstevel@tonic-gate if (size > pad) 27390Sstevel@tonic-gate size -= pad; 27400Sstevel@tonic-gate } 27410Sstevel@tonic-gate 27421618Srie dbg_print(0, MSG_ORIG(MSG_NOTE_DESCSZ), EC_WORD(descsz)); 27430Sstevel@tonic-gate if (descsz) { 27440Sstevel@tonic-gate int ndx, byte, word; 27451618Srie char string[58], *str = string; 27460Sstevel@tonic-gate uchar_t *desc = (uchar_t *)data; 27470Sstevel@tonic-gate 27480Sstevel@tonic-gate /* 27490Sstevel@tonic-gate * Dump descriptor bytes. 27500Sstevel@tonic-gate */ 27510Sstevel@tonic-gate for (ndx = byte = word = 0; descsz; descsz--, desc++) { 27520Sstevel@tonic-gate int tok = *desc; 27530Sstevel@tonic-gate 27540Sstevel@tonic-gate (void) snprintf(str, 58, MSG_ORIG(MSG_NOTE_TOK), 27550Sstevel@tonic-gate tok); 27560Sstevel@tonic-gate str += 3; 27570Sstevel@tonic-gate 27580Sstevel@tonic-gate if (++byte == 4) { 27590Sstevel@tonic-gate *str++ = ' ', *str++ = ' '; 27600Sstevel@tonic-gate word++; 27610Sstevel@tonic-gate byte = 0; 27620Sstevel@tonic-gate } 27630Sstevel@tonic-gate if (word == 4) { 27640Sstevel@tonic-gate *str = '\0'; 27651618Srie dbg_print(0, MSG_ORIG(MSG_NOTE_DESC), 27660Sstevel@tonic-gate ndx, string); 27670Sstevel@tonic-gate word = 0; 27680Sstevel@tonic-gate ndx += 16; 27690Sstevel@tonic-gate str = string; 27700Sstevel@tonic-gate } 27710Sstevel@tonic-gate } 27720Sstevel@tonic-gate if (byte || word) { 27730Sstevel@tonic-gate *str = '\0'; 27741618Srie dbg_print(0, MSG_ORIG(MSG_NOTE_DESC), 27751618Srie ndx, string); 27760Sstevel@tonic-gate } 27770Sstevel@tonic-gate 27780Sstevel@tonic-gate desc += pad; 27790Sstevel@tonic-gate /* LINTED */ 27800Sstevel@tonic-gate data = (Word *)desc; 27810Sstevel@tonic-gate } 27820Sstevel@tonic-gate } 27830Sstevel@tonic-gate } 27840Sstevel@tonic-gate 27850Sstevel@tonic-gate /* 27860Sstevel@tonic-gate * Search for and process a .note section. 27870Sstevel@tonic-gate */ 27880Sstevel@tonic-gate static void 27894168Sab196087 note(Cache *cache, Word shnum, const char *file) 27900Sstevel@tonic-gate { 27911618Srie Word cnt; 27920Sstevel@tonic-gate 27930Sstevel@tonic-gate /* 27940Sstevel@tonic-gate * Otherwise look for any .note sections. 27950Sstevel@tonic-gate */ 27960Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 27971618Srie Cache *_cache = &cache[cnt]; 27981618Srie Shdr *shdr = _cache->c_shdr; 27990Sstevel@tonic-gate 28000Sstevel@tonic-gate if (shdr->sh_type != SHT_NOTE) 28010Sstevel@tonic-gate continue; 2802*5411Sab196087 if (!match(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type)) 28030Sstevel@tonic-gate continue; 28040Sstevel@tonic-gate 28050Sstevel@tonic-gate /* 28060Sstevel@tonic-gate * As these sections are often hand rolled, make sure they're 28075230Sab196087 * properly aligned before proceeding, and issue an error 28085230Sab196087 * as necessary. 28095230Sab196087 * 28105230Sab196087 * Note that we will continue on to display the note even 28115230Sab196087 * if it has bad alignment. We can do this safely, because 28125230Sab196087 * libelf knows the alignment required for SHT_NOTE, and 28135230Sab196087 * takes steps to deliver a properly aligned buffer to us 28145230Sab196087 * even if the actual file is misaligned. 28150Sstevel@tonic-gate */ 28165230Sab196087 if (shdr->sh_offset & (sizeof (Word) - 1)) 28170Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADALIGN), 28180Sstevel@tonic-gate file, _cache->c_name); 28195230Sab196087 28203466Srie if (_cache->c_data == NULL) 28213466Srie continue; 28220Sstevel@tonic-gate 28231618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 28241618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_NOTE), _cache->c_name); 28250Sstevel@tonic-gate note_entry(_cache, (Word *)_cache->c_data->d_buf, 28260Sstevel@tonic-gate /* LINTED */ 28270Sstevel@tonic-gate (Word)_cache->c_data->d_size, file); 28280Sstevel@tonic-gate } 28290Sstevel@tonic-gate } 28300Sstevel@tonic-gate 28311618Srie /* 28321618Srie * Determine an individual hash entry. This may be the initial hash entry, 28331618Srie * or an associated chain entry. 28341618Srie */ 28351618Srie static void 28361618Srie hash_entry(Cache *refsec, Cache *strsec, const char *hsecname, Word hashndx, 28371618Srie Word symndx, Word symn, Sym *syms, const char *file, ulong_t bkts, 28381618Srie uint_t flags, int chain) 28391618Srie { 28401618Srie Sym *sym; 28411618Srie const char *symname, *str; 28421618Srie char _bucket[MAXNDXSIZE], _symndx[MAXNDXSIZE]; 28431618Srie ulong_t nbkt, nhash; 28441618Srie 28451618Srie if (symndx > symn) { 28461618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_HSBADSYMNDX), file, 28471618Srie EC_WORD(symndx), EC_WORD(hashndx)); 28481618Srie symname = MSG_INTL(MSG_STR_UNKNOWN); 28491618Srie } else { 28501618Srie sym = (Sym *)(syms + symndx); 28511618Srie symname = string(refsec, symndx, strsec, file, sym->st_name); 28521618Srie } 28531618Srie 28541618Srie if (chain == 0) { 28551618Srie (void) snprintf(_bucket, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER), 28561618Srie hashndx); 28571618Srie str = (const char *)_bucket; 28581618Srie } else 28591618Srie str = MSG_ORIG(MSG_STR_EMPTY); 28601618Srie 28611618Srie (void) snprintf(_symndx, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX2), 28621618Srie EC_WORD(symndx)); 28631618Srie dbg_print(0, MSG_ORIG(MSG_FMT_HASH_INFO), str, _symndx, 28641618Srie demangle(symname, flags)); 28651618Srie 28661618Srie /* 28671618Srie * Determine if this string is in the correct bucket. 28681618Srie */ 28691618Srie nhash = elf_hash(symname); 28701618Srie nbkt = nhash % bkts; 28711618Srie 28721618Srie if (nbkt != hashndx) { 28731618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADHASH), file, 28741618Srie hsecname, symname, EC_WORD(hashndx), nbkt); 28751618Srie } 28761618Srie } 28770Sstevel@tonic-gate 28780Sstevel@tonic-gate #define MAXCOUNT 500 28790Sstevel@tonic-gate 28800Sstevel@tonic-gate static void 28814168Sab196087 hash(Cache *cache, Word shnum, const char *file, uint_t flags) 28820Sstevel@tonic-gate { 28830Sstevel@tonic-gate static int count[MAXCOUNT]; 28841618Srie Word cnt; 28850Sstevel@tonic-gate ulong_t ndx, bkts; 28860Sstevel@tonic-gate char number[MAXNDXSIZE]; 28870Sstevel@tonic-gate 28880Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 28890Sstevel@tonic-gate uint_t *hash, *chain; 28900Sstevel@tonic-gate Cache *_cache = &cache[cnt]; 28911618Srie Shdr *sshdr, *hshdr = _cache->c_shdr; 28921618Srie char *ssecname, *hsecname = _cache->c_name; 28931618Srie Sym *syms; 28941618Srie Word symn; 28950Sstevel@tonic-gate 28961618Srie if (hshdr->sh_type != SHT_HASH) 28970Sstevel@tonic-gate continue; 28980Sstevel@tonic-gate 28990Sstevel@tonic-gate /* 29000Sstevel@tonic-gate * Determine the hash table data and size. 29010Sstevel@tonic-gate */ 29021618Srie if ((hshdr->sh_entsize == 0) || (hshdr->sh_size == 0)) { 29030Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 29041618Srie file, hsecname); 29050Sstevel@tonic-gate continue; 29060Sstevel@tonic-gate } 29073466Srie if (_cache->c_data == NULL) 29083466Srie continue; 29093466Srie 29100Sstevel@tonic-gate hash = (uint_t *)_cache->c_data->d_buf; 29110Sstevel@tonic-gate bkts = *hash; 29120Sstevel@tonic-gate chain = hash + 2 + bkts; 29130Sstevel@tonic-gate hash += 2; 29140Sstevel@tonic-gate 29150Sstevel@tonic-gate /* 29160Sstevel@tonic-gate * Get the data buffer for the associated symbol table. 29170Sstevel@tonic-gate */ 29181618Srie if ((hshdr->sh_link == 0) || (hshdr->sh_link >= shnum)) { 29190Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK), 29201618Srie file, hsecname, EC_WORD(hshdr->sh_link)); 29210Sstevel@tonic-gate continue; 29220Sstevel@tonic-gate } 29231618Srie 29241618Srie _cache = &cache[hshdr->sh_link]; 29251618Srie ssecname = _cache->c_name; 29261618Srie 29273466Srie if (_cache->c_data == NULL) 29283466Srie continue; 29293466Srie 29303466Srie if ((syms = (Sym *)_cache->c_data->d_buf) == NULL) { 29310Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 29321618Srie file, ssecname); 29330Sstevel@tonic-gate continue; 29340Sstevel@tonic-gate } 29350Sstevel@tonic-gate 29361618Srie sshdr = _cache->c_shdr; 29371618Srie /* LINTED */ 29381618Srie symn = (Word)(sshdr->sh_size / sshdr->sh_entsize); 29391618Srie 29400Sstevel@tonic-gate /* 29410Sstevel@tonic-gate * Get the associated string table section. 29420Sstevel@tonic-gate */ 29431618Srie if ((sshdr->sh_link == 0) || (sshdr->sh_link >= shnum)) { 29440Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK), 29451618Srie file, ssecname, EC_WORD(sshdr->sh_link)); 29460Sstevel@tonic-gate continue; 29470Sstevel@tonic-gate } 29480Sstevel@tonic-gate 29491618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 29501618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_HASH), hsecname); 29511618Srie dbg_print(0, MSG_INTL(MSG_ELF_HASH_INFO)); 29520Sstevel@tonic-gate 29530Sstevel@tonic-gate /* 29540Sstevel@tonic-gate * Loop through the hash buckets, printing the appropriate 29550Sstevel@tonic-gate * symbols. 29560Sstevel@tonic-gate */ 29570Sstevel@tonic-gate for (ndx = 0; ndx < bkts; ndx++, hash++) { 29581618Srie Word _ndx, _cnt; 29590Sstevel@tonic-gate 29600Sstevel@tonic-gate if (*hash == 0) { 29610Sstevel@tonic-gate count[0]++; 29620Sstevel@tonic-gate continue; 29630Sstevel@tonic-gate } 29640Sstevel@tonic-gate 29651618Srie hash_entry(_cache, &cache[sshdr->sh_link], hsecname, 29661618Srie ndx, *hash, symn, syms, file, bkts, flags, 0); 29670Sstevel@tonic-gate 29680Sstevel@tonic-gate /* 29690Sstevel@tonic-gate * Determine if any other symbols are chained to this 29700Sstevel@tonic-gate * bucket. 29710Sstevel@tonic-gate */ 29720Sstevel@tonic-gate _ndx = chain[*hash]; 29730Sstevel@tonic-gate _cnt = 1; 29740Sstevel@tonic-gate while (_ndx) { 29751618Srie hash_entry(_cache, &cache[sshdr->sh_link], 29761618Srie hsecname, ndx, _ndx, symn, syms, file, 29771618Srie bkts, flags, 1); 29780Sstevel@tonic-gate _ndx = chain[_ndx]; 29790Sstevel@tonic-gate _cnt++; 29800Sstevel@tonic-gate } 29810Sstevel@tonic-gate 29820Sstevel@tonic-gate if (_cnt >= MAXCOUNT) { 29830Sstevel@tonic-gate (void) fprintf(stderr, 29841324Srie MSG_INTL(MSG_HASH_OVERFLW), file, 29851618Srie _cache->c_name, EC_WORD(ndx), 29861618Srie EC_WORD(_cnt)); 29870Sstevel@tonic-gate } else 29880Sstevel@tonic-gate count[_cnt]++; 29890Sstevel@tonic-gate } 29900Sstevel@tonic-gate break; 29910Sstevel@tonic-gate } 29920Sstevel@tonic-gate 29930Sstevel@tonic-gate /* 29940Sstevel@tonic-gate * Print out the count information. 29950Sstevel@tonic-gate */ 29960Sstevel@tonic-gate bkts = cnt = 0; 29971618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 29981618Srie 29990Sstevel@tonic-gate for (ndx = 0; ndx < MAXCOUNT; ndx++) { 30001618Srie Word _cnt; 30010Sstevel@tonic-gate 30020Sstevel@tonic-gate if ((_cnt = count[ndx]) == 0) 30030Sstevel@tonic-gate continue; 30040Sstevel@tonic-gate 30051618Srie (void) snprintf(number, MAXNDXSIZE, 30061618Srie MSG_ORIG(MSG_FMT_INTEGER), _cnt); 30071618Srie dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS1), number, 30081618Srie EC_WORD(ndx)); 30090Sstevel@tonic-gate bkts += _cnt; 30101618Srie cnt += (Word)(ndx * _cnt); 30110Sstevel@tonic-gate } 30120Sstevel@tonic-gate if (cnt) { 30130Sstevel@tonic-gate (void) snprintf(number, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER), 30141618Srie bkts); 30151618Srie dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS2), number, 30161618Srie EC_WORD(cnt)); 30170Sstevel@tonic-gate } 30180Sstevel@tonic-gate } 30190Sstevel@tonic-gate 30200Sstevel@tonic-gate static void 30214168Sab196087 group(Cache *cache, Word shnum, const char *file, uint_t flags) 30220Sstevel@tonic-gate { 30231618Srie Word scnt; 30240Sstevel@tonic-gate 30251618Srie for (scnt = 1; scnt < shnum; scnt++) { 30261618Srie Cache *_cache = &cache[scnt]; 30271618Srie Shdr *shdr = _cache->c_shdr; 30281618Srie Word *grpdata, gcnt, grpcnt, symnum, unknown; 30291618Srie Cache *symsec, *strsec; 30301618Srie Sym *syms, *sym; 30311618Srie char flgstrbuf[MSG_GRP_COMDAT_SIZE + 10]; 30320Sstevel@tonic-gate 30330Sstevel@tonic-gate if (shdr->sh_type != SHT_GROUP) 30340Sstevel@tonic-gate continue; 3035*5411Sab196087 if (!match(MATCH_F_ALL, _cache->c_name, scnt, shdr->sh_type)) 30360Sstevel@tonic-gate continue; 30373466Srie if ((_cache->c_data == NULL) || 30383466Srie ((grpdata = (Word *)_cache->c_data->d_buf) == NULL)) 30390Sstevel@tonic-gate continue; 30401618Srie grpcnt = shdr->sh_size / sizeof (Word); 30411618Srie 30421618Srie /* 30431618Srie * Get the data buffer for the associated symbol table and 30441618Srie * string table. 30451618Srie */ 30461618Srie if (stringtbl(cache, 1, scnt, shnum, file, 30471618Srie &symnum, &symsec, &strsec) == 0) 30481618Srie return; 30491618Srie 30501618Srie syms = symsec->c_data->d_buf; 30511618Srie 30521618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 30531618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_GRP), _cache->c_name); 30541618Srie dbg_print(0, MSG_INTL(MSG_GRP_TITLE)); 30551618Srie 30561618Srie /* 30571618Srie * The first element of the group defines the group. The 30581618Srie * associated symbol is defined by the sh_link field. 30591618Srie */ 30601618Srie if ((shdr->sh_info == SHN_UNDEF) || (shdr->sh_info > symnum)) { 30611618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO), 30621618Srie file, _cache->c_name, EC_WORD(shdr->sh_info)); 30631618Srie return; 30640Sstevel@tonic-gate } 30650Sstevel@tonic-gate 30661618Srie (void) strcpy(flgstrbuf, MSG_ORIG(MSG_STR_OSQBRKT)); 30671618Srie if (grpdata[0] & GRP_COMDAT) { 30681618Srie (void) strcat(flgstrbuf, MSG_ORIG(MSG_GRP_COMDAT)); 30690Sstevel@tonic-gate } 30701618Srie if ((unknown = (grpdata[0] & ~GRP_COMDAT)) != 0) { 30711618Srie size_t len = strlen(flgstrbuf); 30721618Srie 30731618Srie (void) snprintf(&flgstrbuf[len], 30741618Srie (MSG_GRP_COMDAT_SIZE + 10 - len), 30751618Srie MSG_ORIG(MSG_GRP_UNKNOWN), unknown); 30760Sstevel@tonic-gate } 30771618Srie (void) strcat(flgstrbuf, MSG_ORIG(MSG_STR_CSQBRKT)); 30781618Srie sym = (Sym *)(syms + shdr->sh_info); 30790Sstevel@tonic-gate 30801618Srie dbg_print(0, MSG_INTL(MSG_GRP_SIGNATURE), flgstrbuf, 30811618Srie demangle(string(_cache, 0, strsec, file, sym->st_name), 30821618Srie flags)); 30831618Srie 30841618Srie for (gcnt = 1; gcnt < grpcnt; gcnt++) { 30850Sstevel@tonic-gate char index[MAXNDXSIZE]; 30861618Srie const char *name; 30870Sstevel@tonic-gate 30880Sstevel@tonic-gate (void) snprintf(index, MAXNDXSIZE, 30891618Srie MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(gcnt)); 30901618Srie 30911618Srie if (grpdata[gcnt] >= shnum) 30921618Srie name = MSG_INTL(MSG_GRP_INVALSCN); 30931618Srie else 30941618Srie name = cache[grpdata[gcnt]].c_name; 30951618Srie 30961618Srie (void) printf(MSG_ORIG(MSG_GRP_ENTRY), index, name, 30974433Sab196087 EC_XWORD(grpdata[gcnt])); 30980Sstevel@tonic-gate } 30990Sstevel@tonic-gate } 31000Sstevel@tonic-gate } 31010Sstevel@tonic-gate 31020Sstevel@tonic-gate static void 31031618Srie got(Cache *cache, Word shnum, Ehdr *ehdr, const char *file, uint_t flags) 31040Sstevel@tonic-gate { 31055230Sab196087 Cache *gotcache = NULL, *symtab = NULL; 31061618Srie Addr gotbgn, gotend; 31071618Srie Shdr *gotshdr; 31081618Srie Word cnt, gotents, gotndx; 31090Sstevel@tonic-gate size_t gentsize; 31100Sstevel@tonic-gate Got_info *gottable; 31110Sstevel@tonic-gate char *gotdata; 31121618Srie Sym *gotsym; 31131618Srie Xword gotsymaddr; 31140Sstevel@tonic-gate 31150Sstevel@tonic-gate /* 31161324Srie * First, find the got. 31170Sstevel@tonic-gate */ 31180Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 31195230Sab196087 if (strncmp(cache[cnt].c_name, MSG_ORIG(MSG_ELF_GOT), 31201324Srie MSG_ELF_GOT_SIZE) == 0) { 31215230Sab196087 gotcache = &cache[cnt]; 31220Sstevel@tonic-gate break; 31230Sstevel@tonic-gate } 31240Sstevel@tonic-gate } 31255230Sab196087 if (gotcache == NULL) 31260Sstevel@tonic-gate return; 31271324Srie 31281324Srie /* 31291324Srie * A got section within a relocatable object is suspicious. 31301324Srie */ 31311324Srie if (ehdr->e_type == ET_REL) { 31321324Srie (void) fprintf(stderr, MSG_INTL(MSG_GOT_UNEXPECTED), file, 31335230Sab196087 gotcache->c_name); 31341324Srie } 31351324Srie 31361618Srie gotshdr = gotcache->c_shdr; 31370Sstevel@tonic-gate if (gotshdr->sh_size == 0) { 31380Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 31390Sstevel@tonic-gate file, gotcache->c_name); 31400Sstevel@tonic-gate return; 31410Sstevel@tonic-gate } 31421618Srie 31431618Srie gotbgn = gotshdr->sh_addr; 31440Sstevel@tonic-gate gotend = gotbgn + gotshdr->sh_size; 31450Sstevel@tonic-gate 31460Sstevel@tonic-gate /* 31471618Srie * Some architectures don't properly set the sh_entsize for the GOT 31481618Srie * table. If it's not set, default to a size of a pointer. 31490Sstevel@tonic-gate */ 31501618Srie if ((gentsize = gotshdr->sh_entsize) == 0) 31511618Srie gentsize = sizeof (Xword); 31521618Srie 31533466Srie if (gotcache->c_data == NULL) 31543466Srie return; 31553466Srie 31560Sstevel@tonic-gate /* LINTED */ 31571618Srie gotents = (Word)(gotshdr->sh_size / gentsize); 31580Sstevel@tonic-gate gotdata = gotcache->c_data->d_buf; 31590Sstevel@tonic-gate 31600Sstevel@tonic-gate if ((gottable = calloc(gotents, sizeof (Got_info))) == 0) { 31610Sstevel@tonic-gate int err = errno; 31621618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC), file, 31631618Srie strerror(err)); 31640Sstevel@tonic-gate return; 31650Sstevel@tonic-gate } 31660Sstevel@tonic-gate 31670Sstevel@tonic-gate /* 31680Sstevel@tonic-gate * Now we scan through all the sections looking for any relocations 31690Sstevel@tonic-gate * that may be against the GOT. Since these may not be isolated to a 31700Sstevel@tonic-gate * .rel[a].got section we check them all. 31710Sstevel@tonic-gate * While scanning sections save the symbol table entry (a symtab 31720Sstevel@tonic-gate * overriding a dynsym) so that we can lookup _GLOBAL_OFFSET_TABLE_. 31730Sstevel@tonic-gate */ 31740Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 31751618Srie Word type, symnum; 31761618Srie Xword relndx, relnum, relsize; 31771618Srie void *rels; 31781618Srie Sym *syms; 31791618Srie Cache *symsec, *strsec; 31801618Srie Cache *_cache = &cache[cnt]; 31811618Srie Shdr *shdr; 31820Sstevel@tonic-gate 31831618Srie shdr = _cache->c_shdr; 31841618Srie type = shdr->sh_type; 31850Sstevel@tonic-gate 31861618Srie if ((symtab == 0) && (type == SHT_DYNSYM)) { 31870Sstevel@tonic-gate symtab = _cache; 31880Sstevel@tonic-gate continue; 31890Sstevel@tonic-gate } 31901618Srie if (type == SHT_SYMTAB) { 31910Sstevel@tonic-gate symtab = _cache; 31920Sstevel@tonic-gate continue; 31930Sstevel@tonic-gate } 31941618Srie if ((type != SHT_RELA) && (type != SHT_REL)) 31950Sstevel@tonic-gate continue; 31960Sstevel@tonic-gate 31970Sstevel@tonic-gate /* 31981618Srie * Decide entry size. 31990Sstevel@tonic-gate */ 32001618Srie if (((relsize = shdr->sh_entsize) == 0) || 32011618Srie (relsize > shdr->sh_size)) { 32021618Srie if (type == SHT_RELA) 32031618Srie relsize = sizeof (Rela); 32041618Srie else 32051618Srie relsize = sizeof (Rel); 32060Sstevel@tonic-gate } 32070Sstevel@tonic-gate 32080Sstevel@tonic-gate /* 32091618Srie * Determine the number of relocations available. 32100Sstevel@tonic-gate */ 32111618Srie if (shdr->sh_size == 0) { 32121618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 32131618Srie file, _cache->c_name); 32140Sstevel@tonic-gate continue; 32150Sstevel@tonic-gate } 32163466Srie if (_cache->c_data == NULL) 32173466Srie continue; 32183466Srie 32191618Srie rels = _cache->c_data->d_buf; 32201618Srie relnum = shdr->sh_size / relsize; 32210Sstevel@tonic-gate 32221618Srie /* 32231618Srie * Get the data buffer for the associated symbol table and 32241618Srie * string table. 32251618Srie */ 32261618Srie if (stringtbl(cache, 1, cnt, shnum, file, 32271618Srie &symnum, &symsec, &strsec) == 0) 32281618Srie continue; 32291618Srie 32301618Srie syms = symsec->c_data->d_buf; 32311618Srie 32321618Srie /* 32331618Srie * Loop through the relocation entries. 32341618Srie */ 32351618Srie for (relndx = 0; relndx < relnum; relndx++, 32361618Srie rels = (void *)((char *)rels + relsize)) { 32371618Srie char section[BUFSIZ]; 32381618Srie Addr offset; 32390Sstevel@tonic-gate Got_info *gip; 32401618Srie Word symndx, reltype; 32411618Srie Rela *rela; 32421618Srie Rel *rel; 32430Sstevel@tonic-gate 32441618Srie /* 32451618Srie * Unravel the relocation. 32461618Srie */ 32471618Srie if (type == SHT_RELA) { 32481618Srie rela = (Rela *)rels; 32491618Srie symndx = ELF_R_SYM(rela->r_info); 32501618Srie reltype = ELF_R_TYPE(rela->r_info); 32511618Srie offset = rela->r_offset; 32520Sstevel@tonic-gate } else { 32531618Srie rel = (Rel *)rels; 32541618Srie symndx = ELF_R_SYM(rel->r_info); 32551618Srie reltype = ELF_R_TYPE(rel->r_info); 32561618Srie offset = rel->r_offset; 32570Sstevel@tonic-gate } 32580Sstevel@tonic-gate 32590Sstevel@tonic-gate /* 32600Sstevel@tonic-gate * Only pay attention to relocations against the GOT. 32610Sstevel@tonic-gate */ 32624146Sab196087 if ((offset < gotbgn) || (offset >= gotend)) 32630Sstevel@tonic-gate continue; 32640Sstevel@tonic-gate 32650Sstevel@tonic-gate /* LINTED */ 32661618Srie gotndx = (Word)((offset - gotbgn) / 32670Sstevel@tonic-gate gotshdr->sh_entsize); 32680Sstevel@tonic-gate gip = &gottable[gotndx]; 32691618Srie 32701618Srie if (gip->g_reltype != 0) { 32710Sstevel@tonic-gate (void) fprintf(stderr, 32720Sstevel@tonic-gate MSG_INTL(MSG_GOT_MULTIPLE), file, 32731618Srie EC_WORD(gotndx), EC_ADDR(offset)); 32740Sstevel@tonic-gate continue; 32750Sstevel@tonic-gate } 32760Sstevel@tonic-gate 32771618Srie if (symndx) 32781618Srie gip->g_symname = relsymname(cache, _cache, 32791618Srie strsec, symndx, symnum, relndx, syms, 32801618Srie section, BUFSIZ, file, flags); 32811618Srie gip->g_reltype = reltype; 32821618Srie gip->g_rel = rels; 32830Sstevel@tonic-gate } 32840Sstevel@tonic-gate } 32850Sstevel@tonic-gate 32861618Srie if (symlookup(MSG_ORIG(MSG_GOT_SYM), cache, shnum, &gotsym, symtab, 32871618Srie file)) 32881618Srie gotsymaddr = gotsym->st_value; 32890Sstevel@tonic-gate else 32901618Srie gotsymaddr = gotbgn; 32910Sstevel@tonic-gate 32921618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 32931618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_GOT), gotcache->c_name); 32941618Srie Elf_got_title(0); 32950Sstevel@tonic-gate 32960Sstevel@tonic-gate for (gotndx = 0; gotndx < gotents; gotndx++) { 32970Sstevel@tonic-gate Got_info *gip; 32980Sstevel@tonic-gate Sword gindex; 32991618Srie Addr gaddr; 33001618Srie Xword gotentry; 33010Sstevel@tonic-gate 33020Sstevel@tonic-gate gip = &gottable[gotndx]; 33030Sstevel@tonic-gate 33040Sstevel@tonic-gate gaddr = gotbgn + (gotndx * gentsize); 33051618Srie gindex = (Sword)(gaddr - gotsymaddr) / (Sword)gentsize; 33060Sstevel@tonic-gate 33071618Srie if (gentsize == sizeof (Word)) 33080Sstevel@tonic-gate /* LINTED */ 33091618Srie gotentry = (Xword)(*((Word *)(gotdata) + gotndx)); 33100Sstevel@tonic-gate else 33110Sstevel@tonic-gate /* LINTED */ 33121618Srie gotentry = *((Xword *)(gotdata) + gotndx); 33130Sstevel@tonic-gate 33141618Srie Elf_got_entry(0, gindex, gaddr, gotentry, ehdr->e_machine, 33151618Srie gip->g_reltype, gip->g_rel, gip->g_symname); 33160Sstevel@tonic-gate } 33170Sstevel@tonic-gate free(gottable); 33180Sstevel@tonic-gate } 33190Sstevel@tonic-gate 33200Sstevel@tonic-gate void 33210Sstevel@tonic-gate checksum(Elf *elf) 33220Sstevel@tonic-gate { 33231618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 33241618Srie dbg_print(0, MSG_INTL(MSG_STR_CHECKSUM), elf_checksum(elf)); 33250Sstevel@tonic-gate } 33260Sstevel@tonic-gate 33274242Sab196087 /* 33284242Sab196087 * This variable is used by regular() to communicate the address of 33294242Sab196087 * the section header cache to sort_shdr_ndx_arr(). Unfortunately, 33304242Sab196087 * the qsort() interface does not include a userdata argument by which 33314242Sab196087 * such arbitrary data can be passed, so we are stuck using global data. 33324242Sab196087 */ 33334242Sab196087 static Cache *sort_shdr_ndx_arr_cache; 33344242Sab196087 33354242Sab196087 33364242Sab196087 /* 33374242Sab196087 * Used with qsort() to sort the section indices so that they can be 33384242Sab196087 * used to access the section headers in order of increasing data offset. 33394242Sab196087 * 33404242Sab196087 * entry: 33414242Sab196087 * sort_shdr_ndx_arr_cache - Contains address of 33424242Sab196087 * section header cache. 33434242Sab196087 * v1, v2 - Point at elements of sort_shdr_bits array to be compared. 33444242Sab196087 * 33454242Sab196087 * exit: 33464242Sab196087 * Returns -1 (less than), 0 (equal) or 1 (greater than). 33474242Sab196087 */ 33484242Sab196087 static int 33494242Sab196087 sort_shdr_ndx_arr(const void *v1, const void *v2) 33504242Sab196087 { 33514242Sab196087 Cache *cache1 = sort_shdr_ndx_arr_cache + *((size_t *)v1); 33524242Sab196087 Cache *cache2 = sort_shdr_ndx_arr_cache + *((size_t *)v2); 33534242Sab196087 33544242Sab196087 if (cache1->c_shdr->sh_offset < cache2->c_shdr->sh_offset) 33554242Sab196087 return (-1); 33564242Sab196087 33574242Sab196087 if (cache1->c_shdr->sh_offset > cache2->c_shdr->sh_offset) 33584242Sab196087 return (1); 33594242Sab196087 33604242Sab196087 return (0); 33614242Sab196087 } 33624242Sab196087 33634242Sab196087 33644665Sab196087 static int 33654665Sab196087 shdr_cache(const char *file, Elf *elf, Ehdr *ehdr, size_t shstrndx, 33664665Sab196087 size_t shnum, Cache **cache_ret) 33670Sstevel@tonic-gate { 33680Sstevel@tonic-gate Elf_Scn *scn; 33690Sstevel@tonic-gate Elf_Data *data; 33704665Sab196087 size_t ndx; 33714665Sab196087 Shdr *nameshdr; 33720Sstevel@tonic-gate char *names = 0; 33730Sstevel@tonic-gate Cache *cache, *_cache; 33744242Sab196087 size_t *shdr_ndx_arr, shdr_ndx_arr_cnt; 33750Sstevel@tonic-gate 33760Sstevel@tonic-gate 33770Sstevel@tonic-gate /* 33780Sstevel@tonic-gate * Obtain the .shstrtab data buffer to provide the required section 33790Sstevel@tonic-gate * name strings. 33800Sstevel@tonic-gate */ 33814156Sab196087 if (shstrndx == SHN_UNDEF) { 33824156Sab196087 /* 33834156Sab196087 * It is rare, but legal, for an object to lack a 33844156Sab196087 * header string table section. 33854156Sab196087 */ 33864156Sab196087 names = NULL; 33874156Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHSTRSEC), file); 33884156Sab196087 } else if ((scn = elf_getscn(elf, shstrndx)) == NULL) { 33890Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETSCN)); 33900Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SHDR), 33910Sstevel@tonic-gate EC_XWORD(shstrndx)); 33921618Srie 33930Sstevel@tonic-gate } else if ((data = elf_getdata(scn, NULL)) == NULL) { 33940Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETDATA)); 33950Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_DATA), 33960Sstevel@tonic-gate EC_XWORD(shstrndx)); 33971618Srie 33981618Srie } else if ((nameshdr = elf_getshdr(scn)) == NULL) { 33990Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETSHDR)); 34000Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN), 34013862Srie EC_WORD(elf_ndxscn(scn))); 34021618Srie 34031618Srie } else if ((names = data->d_buf) == 0) 34040Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_SHSTRNULL), file); 34050Sstevel@tonic-gate 34060Sstevel@tonic-gate /* 34073862Srie * Allocate a cache to maintain a descriptor for each section. 34080Sstevel@tonic-gate */ 34094665Sab196087 if ((*cache_ret = cache = malloc(shnum * sizeof (Cache))) == NULL) { 34100Sstevel@tonic-gate int err = errno; 34110Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC), 34120Sstevel@tonic-gate file, strerror(err)); 34134665Sab196087 return (0); 34140Sstevel@tonic-gate } 34150Sstevel@tonic-gate 34161618Srie *cache = cache_init; 34170Sstevel@tonic-gate _cache = cache; 34180Sstevel@tonic-gate _cache++; 34190Sstevel@tonic-gate 34203862Srie /* 34214242Sab196087 * Allocate an array that will hold the section index for 34224242Sab196087 * each section that has data in the ELF file: 34234242Sab196087 * 34244242Sab196087 * - Is not a NOBITS section 34254242Sab196087 * - Data has non-zero length 34264242Sab196087 * 34274242Sab196087 * Note that shnum is an upper bound on the size required. It 34284242Sab196087 * is likely that we won't use a few of these array elements. 34294242Sab196087 * Allocating a modest amount of extra memory in this case means 34304242Sab196087 * that we can avoid an extra loop to count the number of needed 34314242Sab196087 * items, and can fill this array immediately in the first loop 34324242Sab196087 * below. 34334242Sab196087 */ 34344242Sab196087 if ((shdr_ndx_arr = malloc(shnum * sizeof (*shdr_ndx_arr))) == NULL) { 34354242Sab196087 int err = errno; 34364242Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC), 34374242Sab196087 file, strerror(err)); 34384665Sab196087 return (0); 34394242Sab196087 } 34404242Sab196087 shdr_ndx_arr_cnt = 0; 34414242Sab196087 34424242Sab196087 /* 34433862Srie * Traverse the sections of the file. This gathering of data is 34443862Srie * carried out in two passes. First, the section headers are captured 34453862Srie * and the section header names are evaluated. A verification pass is 34463862Srie * then carried out over the section information. Files have been 34473862Srie * known to exhibit overlapping (and hence erroneous) section header 34483862Srie * information. 34493862Srie * 34503862Srie * Finally, the data for each section is obtained. This processing is 34513862Srie * carried out after section verification because should any section 34523862Srie * header overlap occur, and a file needs translating (ie. xlate'ing 34533862Srie * information from a non-native architecture file), then the process 34543862Srie * of translation can corrupt the section header information. Of 34553862Srie * course, if there is any section overlap, the data related to the 34563862Srie * sections is going to be compromised. However, it is the translation 34573862Srie * of this data that has caused problems with elfdump()'s ability to 34583862Srie * extract the data. 34593862Srie */ 34604242Sab196087 for (ndx = 1, scn = NULL; scn = elf_nextscn(elf, scn); 34614242Sab196087 ndx++, _cache++) { 34623862Srie char scnndxnm[100]; 34633862Srie 34644242Sab196087 _cache->c_ndx = ndx; 34653862Srie _cache->c_scn = scn; 34663862Srie 34671618Srie if ((_cache->c_shdr = elf_getshdr(scn)) == NULL) { 34680Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETSHDR)); 34690Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN), 34703862Srie EC_WORD(elf_ndxscn(scn))); 34710Sstevel@tonic-gate } 34720Sstevel@tonic-gate 34733862Srie /* 34744242Sab196087 * If this section has data in the file, include it in 34754242Sab196087 * the array of sections to check for address overlap. 34764242Sab196087 */ 34774242Sab196087 if ((_cache->c_shdr->sh_size != 0) && 34784242Sab196087 (_cache->c_shdr->sh_type != SHT_NOBITS)) 34794242Sab196087 shdr_ndx_arr[shdr_ndx_arr_cnt++] = ndx; 34804242Sab196087 34814242Sab196087 /* 34823862Srie * If a shstrtab exists, assign the section name. 34833862Srie */ 34843862Srie if (names && _cache->c_shdr) { 34853862Srie if (_cache->c_shdr->sh_name && 34863862Srie /* LINTED */ 34873862Srie (nameshdr->sh_size > _cache->c_shdr->sh_name)) { 34883862Srie _cache->c_name = 34893862Srie names + _cache->c_shdr->sh_name; 34903862Srie continue; 34913862Srie } 34920Sstevel@tonic-gate 34930Sstevel@tonic-gate /* 34943862Srie * Generate an error if the section name index is zero 34953862Srie * or exceeds the shstrtab data. Fall through to 34963862Srie * fabricate a section name. 34970Sstevel@tonic-gate */ 34983862Srie if ((_cache->c_shdr->sh_name == 0) || 34990Sstevel@tonic-gate /* LINTED */ 35001618Srie (nameshdr->sh_size <= _cache->c_shdr->sh_name)) { 35010Sstevel@tonic-gate (void) fprintf(stderr, 35020Sstevel@tonic-gate MSG_INTL(MSG_ERR_BADSHNAME), file, 35034242Sab196087 EC_WORD(ndx), 35041618Srie EC_XWORD(_cache->c_shdr->sh_name)); 35050Sstevel@tonic-gate } 35063862Srie } 35073862Srie 35083862Srie /* 35093862Srie * If there exists no shstrtab data, or a section header has no 35103862Srie * name (an invalid index of 0), then compose a name for the 35113862Srie * section. 35123862Srie */ 35133862Srie (void) snprintf(scnndxnm, sizeof (scnndxnm), 35144242Sab196087 MSG_INTL(MSG_FMT_SCNNDX), ndx); 35154242Sab196087 35164242Sab196087 if ((_cache->c_name = malloc(strlen(scnndxnm) + 1)) == NULL) { 35173862Srie int err = errno; 35183862Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC), 35193862Srie file, strerror(err)); 35204665Sab196087 return (0); 35213862Srie } 35223862Srie (void) strcpy(_cache->c_name, scnndxnm); 35233862Srie } 35243862Srie 35253862Srie /* 35263862Srie * Having collected all the sections, validate their address range. 35273862Srie * Cases have existed where the section information has been invalid. 35283862Srie * This can lead to all sorts of other, hard to diagnose errors, as 35293862Srie * each section is processed individually (ie. with elf_getdata()). 35303862Srie * Here, we carry out some address comparisons to catch a family of 35313862Srie * overlapping memory issues we have observed (likely, there are others 35323862Srie * that we have yet to discover). 35333862Srie * 35343862Srie * Note, should any memory overlap occur, obtaining any additional 35353862Srie * data from the file is questionable. However, it might still be 35363862Srie * possible to inspect the ELF header, Programs headers, or individual 35373862Srie * sections, so rather than bailing on an error condition, continue 35383862Srie * processing to see if any data can be salvaged. 35393862Srie */ 35404242Sab196087 if (shdr_ndx_arr_cnt > 1) { 35414242Sab196087 sort_shdr_ndx_arr_cache = cache; 35424242Sab196087 qsort(shdr_ndx_arr, shdr_ndx_arr_cnt, 35434242Sab196087 sizeof (*shdr_ndx_arr), sort_shdr_ndx_arr); 35444242Sab196087 } 35454242Sab196087 for (ndx = 0; ndx < shdr_ndx_arr_cnt; ndx++) { 35464242Sab196087 Cache *_cache = cache + shdr_ndx_arr[ndx]; 35473862Srie Shdr *shdr = _cache->c_shdr; 35483862Srie Off bgn1, bgn = shdr->sh_offset; 35493862Srie Off end1, end = shdr->sh_offset + shdr->sh_size; 35504242Sab196087 size_t ndx1; 35514242Sab196087 35524242Sab196087 /* 35534242Sab196087 * Check the section against all following ones, reporting 35544242Sab196087 * any overlaps. Since we've sorted the sections by offset, 35554242Sab196087 * we can stop after the first comparison that fails. There 35564242Sab196087 * are no overlaps in a properly formed ELF file, in which 35574242Sab196087 * case this algorithm runs in O(n) time. This will degenerate 35584242Sab196087 * to O(n^2) for a completely broken file. Such a file is 35594242Sab196087 * (1) highly unlikely, and (2) unusable, so it is reasonable 35604242Sab196087 * for the analysis to take longer. 35614242Sab196087 */ 35624242Sab196087 for (ndx1 = ndx + 1; ndx1 < shdr_ndx_arr_cnt; ndx1++) { 35634242Sab196087 Cache *_cache1 = cache + shdr_ndx_arr[ndx1]; 35643862Srie Shdr *shdr1 = _cache1->c_shdr; 35653862Srie 35663862Srie bgn1 = shdr1->sh_offset; 35673862Srie end1 = shdr1->sh_offset + shdr1->sh_size; 35683862Srie 35693862Srie if (((bgn1 <= bgn) && (end1 > bgn)) || 35703862Srie ((bgn1 < end) && (end1 >= end))) { 35713862Srie (void) fprintf(stderr, 35723862Srie MSG_INTL(MSG_ERR_SECMEMOVER), file, 35734242Sab196087 EC_WORD(elf_ndxscn(_cache->c_scn)), 35744242Sab196087 _cache->c_name, EC_OFF(bgn), EC_OFF(end), 35753862Srie EC_WORD(elf_ndxscn(_cache1->c_scn)), 35764242Sab196087 _cache1->c_name, EC_OFF(bgn1), 35774242Sab196087 EC_OFF(end1)); 35784242Sab196087 } else { /* No overlap, so can stop */ 35794242Sab196087 break; 35800Sstevel@tonic-gate } 35810Sstevel@tonic-gate } 35820Sstevel@tonic-gate 35833862Srie /* 35844242Sab196087 * In addition to checking for sections overlapping 35854242Sab196087 * each other (done above), we should also make sure 35864242Sab196087 * the section doesn't overlap the section header array. 35873862Srie */ 35883862Srie bgn1 = ehdr->e_shoff; 35893862Srie end1 = ehdr->e_shoff + (ehdr->e_shentsize * ehdr->e_shnum); 35903862Srie 35913862Srie if (((bgn1 <= bgn) && (end1 > bgn)) || 35923862Srie ((bgn1 < end) && (end1 >= end))) { 35933862Srie (void) fprintf(stderr, 35943862Srie MSG_INTL(MSG_ERR_SHDRMEMOVER), file, EC_OFF(bgn1), 35953862Srie EC_OFF(end1), 35963862Srie EC_WORD(elf_ndxscn(_cache->c_scn)), 35973862Srie _cache->c_name, EC_OFF(bgn), EC_OFF(end)); 35983862Srie } 35993862Srie } 36003862Srie 36013862Srie /* 36024242Sab196087 * Obtain the data for each section. 36033862Srie */ 36044242Sab196087 for (ndx = 1; ndx < shnum; ndx++) { 36054242Sab196087 Cache *_cache = &cache[ndx]; 36063862Srie Elf_Scn *scn = _cache->c_scn; 36073862Srie 36080Sstevel@tonic-gate if ((_cache->c_data = elf_getdata(scn, NULL)) == NULL) { 36090Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETDATA)); 36100Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCNDATA), 36113862Srie EC_WORD(elf_ndxscn(scn))); 36120Sstevel@tonic-gate } 36134665Sab196087 } 36144665Sab196087 36154665Sab196087 return (1); 36164665Sab196087 } 36174665Sab196087 36184665Sab196087 36194665Sab196087 3620*5411Sab196087 int 3621*5411Sab196087 regular(const char *file, int fd, Elf *elf, uint_t flags, 3622*5411Sab196087 const char *wname, int wfd) 36234665Sab196087 { 36244665Sab196087 Elf_Scn *scn; 36254665Sab196087 Ehdr *ehdr; 36264665Sab196087 size_t ndx, shstrndx, shnum, phnum; 36274665Sab196087 Shdr *shdr; 36284665Sab196087 Cache *cache; 36294665Sab196087 VERSYM_STATE versym; 3630*5411Sab196087 int ret = 0; 3631*5411Sab196087 int addr_align; 36324665Sab196087 36334665Sab196087 if ((ehdr = elf_getehdr(elf)) == NULL) { 36344665Sab196087 failure(file, MSG_ORIG(MSG_ELF_GETEHDR)); 3635*5411Sab196087 return (ret); 36364665Sab196087 } 36374665Sab196087 36384665Sab196087 if (elf_getshnum(elf, &shnum) == 0) { 36394665Sab196087 failure(file, MSG_ORIG(MSG_ELF_GETSHNUM)); 3640*5411Sab196087 return (ret); 36414665Sab196087 } 36424665Sab196087 36434665Sab196087 if (elf_getshstrndx(elf, &shstrndx) == 0) { 36444665Sab196087 failure(file, MSG_ORIG(MSG_ELF_GETSHSTRNDX)); 3645*5411Sab196087 return (ret); 36464665Sab196087 } 36474665Sab196087 36484665Sab196087 if (elf_getphnum(elf, &phnum) == 0) { 36494665Sab196087 failure(file, MSG_ORIG(MSG_ELF_GETPHNUM)); 3650*5411Sab196087 return (ret); 36514665Sab196087 } 36524665Sab196087 /* 36534665Sab196087 * If the user requested section headers derived from the 36544665Sab196087 * program headers (-P option) and this file doesn't have 36554665Sab196087 * any program headers (i.e. ET_REL), then we can't do it. 36564665Sab196087 */ 3657*5411Sab196087 if ((phnum == 0) && (flags & FLG_CTL_FAKESHDR)) { 36584665Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_PNEEDSPH), file); 3659*5411Sab196087 return (ret); 36604665Sab196087 } 36614665Sab196087 36624665Sab196087 36634665Sab196087 if ((scn = elf_getscn(elf, 0)) != NULL) { 36644665Sab196087 if ((shdr = elf_getshdr(scn)) == NULL) { 36654665Sab196087 failure(file, MSG_ORIG(MSG_ELF_GETSHDR)); 36664665Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN), 0); 3667*5411Sab196087 return (ret); 36684665Sab196087 } 36694665Sab196087 } else 36704665Sab196087 shdr = 0; 36714665Sab196087 36724665Sab196087 /* 36734665Sab196087 * Print the elf header. 36744665Sab196087 */ 3675*5411Sab196087 if (flags & FLG_SHOW_EHDR) 36764665Sab196087 Elf_ehdr(0, ehdr, shdr); 36774665Sab196087 36784665Sab196087 /* 36794665Sab196087 * If the section headers or program headers have inadequate 36804665Sab196087 * alignment for the class of object, print a warning. libelf 36814665Sab196087 * can handle such files, but programs that use them can crash 36824665Sab196087 * when they dereference unaligned items. 3683*5411Sab196087 * 3684*5411Sab196087 * Note that the AMD64 ABI, although it is a 64-bit architecture, 3685*5411Sab196087 * allows access to data types smaller than 128-bits to be on 3686*5411Sab196087 * word alignment. 36874665Sab196087 */ 3688*5411Sab196087 if (ehdr->e_machine == EM_AMD64) 3689*5411Sab196087 addr_align = sizeof (Word); 3690*5411Sab196087 else 3691*5411Sab196087 addr_align = sizeof (Addr); 3692*5411Sab196087 3693*5411Sab196087 if (ehdr->e_phoff & (addr_align - 1)) 36944665Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADPHDRALIGN), file); 3695*5411Sab196087 if (ehdr->e_shoff & (addr_align - 1)) 36964665Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHDRALIGN), file); 36974665Sab196087 36984665Sab196087 /* 36994665Sab196087 * Print the program headers. 37004665Sab196087 */ 3701*5411Sab196087 if ((flags & FLG_SHOW_PHDR) && (phnum != 0)) { 3702*5411Sab196087 Phdr *phdr; 37034665Sab196087 37044665Sab196087 if ((phdr = elf_getphdr(elf)) == NULL) { 37054665Sab196087 failure(file, MSG_ORIG(MSG_ELF_GETPHDR)); 3706*5411Sab196087 return (ret); 37074665Sab196087 } 37084665Sab196087 37094665Sab196087 for (ndx = 0; ndx < phnum; phdr++, ndx++) { 3710*5411Sab196087 if (!match(MATCH_F_PHDR| MATCH_F_NDX | MATCH_F_TYPE, 3711*5411Sab196087 NULL, ndx, phdr->p_type)) 37124665Sab196087 continue; 37134665Sab196087 37144665Sab196087 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 37154665Sab196087 dbg_print(0, MSG_INTL(MSG_ELF_PHDR), EC_WORD(ndx)); 37164665Sab196087 Elf_phdr(0, ehdr->e_machine, phdr); 37174665Sab196087 } 37184665Sab196087 } 37194665Sab196087 37204665Sab196087 /* 3721*5411Sab196087 * If we have flag bits set that explicitly require a show or calc 3722*5411Sab196087 * operation, but none of them require the section headers, then 3723*5411Sab196087 * we are done and can return now. 37244665Sab196087 */ 3725*5411Sab196087 if (((flags & (FLG_MASK_SHOW | FLG_MASK_CALC)) != 0) && 3726*5411Sab196087 ((flags & (FLG_MASK_SHOW_SHDR | FLG_MASK_CALC_SHDR)) == 0)) 3727*5411Sab196087 return (ret); 3728*5411Sab196087 3729*5411Sab196087 /* 3730*5411Sab196087 * If there are no section headers, then resort to synthesizing 3731*5411Sab196087 * section headers from the program headers. This is normally 3732*5411Sab196087 * only done by explicit request, but in this case there's no 3733*5411Sab196087 * reason not to go ahead, since the alternative is simply to quit. 3734*5411Sab196087 */ 3735*5411Sab196087 if ((shnum <= 1) && ((flags & FLG_CTL_FAKESHDR) == 0)) { 3736*5411Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHDR), file); 3737*5411Sab196087 flags |= FLG_CTL_FAKESHDR; 37384665Sab196087 } 37394665Sab196087 37404665Sab196087 /* 37414665Sab196087 * Generate a cache of section headers and related information 37424665Sab196087 * for use by the rest of elfdump. If requested (or the file 37434665Sab196087 * contains no section headers), we generate a fake set of 37444665Sab196087 * headers from the information accessible from the program headers. 37454665Sab196087 * Otherwise, we use the real section headers contained in the file. 37464665Sab196087 */ 37474665Sab196087 3748*5411Sab196087 if (flags & FLG_CTL_FAKESHDR) { 37494665Sab196087 if (fake_shdr_cache(file, fd, elf, ehdr, &cache, &shnum) == 0) 3750*5411Sab196087 return (ret); 37514665Sab196087 } else { 37524665Sab196087 if (shdr_cache(file, elf, ehdr, shstrndx, shnum, &cache) == 0) 3753*5411Sab196087 return (ret); 37544665Sab196087 } 37554665Sab196087 37564665Sab196087 /* 3757*5411Sab196087 * Everything from this point on requires section headers. 3758*5411Sab196087 * If we have no section headers, there is no reason to continue. 3759*5411Sab196087 */ 3760*5411Sab196087 if (shnum <= 1) 3761*5411Sab196087 goto done; 3762*5411Sab196087 3763*5411Sab196087 /* 37644665Sab196087 * If -w was specified, find and write out the section(s) data. 37654665Sab196087 */ 37664665Sab196087 if (wfd) { 37674665Sab196087 for (ndx = 1; ndx < shnum; ndx++) { 37684665Sab196087 Cache *_cache = &cache[ndx]; 37694665Sab196087 3770*5411Sab196087 if (match(MATCH_F_STRICT | MATCH_F_ALL, _cache->c_name, 3771*5411Sab196087 ndx, _cache->c_shdr->sh_type) && 3772*5411Sab196087 _cache->c_data && _cache->c_data->d_buf) { 3773*5411Sab196087 if (write(wfd, _cache->c_data->d_buf, 3774*5411Sab196087 _cache->c_data->d_size) != 3775*5411Sab196087 _cache->c_data->d_size) { 3776*5411Sab196087 int err = errno; 3777*5411Sab196087 (void) fprintf(stderr, 3778*5411Sab196087 MSG_INTL(MSG_ERR_WRITE), wname, 3779*5411Sab196087 strerror(err)); 3780*5411Sab196087 /* 3781*5411Sab196087 * Return an exit status of 1, because 3782*5411Sab196087 * the failure is not related to the 3783*5411Sab196087 * ELF file, but by system resources. 3784*5411Sab196087 */ 3785*5411Sab196087 ret = 1; 3786*5411Sab196087 goto done; 3787*5411Sab196087 } 37884665Sab196087 } 37890Sstevel@tonic-gate } 37900Sstevel@tonic-gate } 37910Sstevel@tonic-gate 3792*5411Sab196087 /* 3793*5411Sab196087 * If we have no flag bits set that explicitly require a show or calc 3794*5411Sab196087 * operation, but match options (-I, -N, -T) were used, then run 3795*5411Sab196087 * through the section headers and see if we can't deduce show flags 3796*5411Sab196087 * from the match options given. 3797*5411Sab196087 * 3798*5411Sab196087 * We don't do this if -w was specified, because (-I, -N, -T) used 3799*5411Sab196087 * with -w in lieu of some other option is supposed to be quiet. 3800*5411Sab196087 */ 3801*5411Sab196087 if ((wfd == 0) && (flags & FLG_CTL_MATCH) && 3802*5411Sab196087 ((flags & (FLG_MASK_SHOW | FLG_MASK_CALC)) == 0)) { 3803*5411Sab196087 for (ndx = 1; ndx < shnum; ndx++) { 3804*5411Sab196087 Cache *_cache = &cache[ndx]; 3805*5411Sab196087 3806*5411Sab196087 if (!match(MATCH_F_STRICT | MATCH_F_ALL, _cache->c_name, 3807*5411Sab196087 ndx, _cache->c_shdr->sh_type)) 3808*5411Sab196087 continue; 3809*5411Sab196087 3810*5411Sab196087 switch (_cache->c_shdr->sh_type) { 3811*5411Sab196087 case SHT_PROGBITS: 3812*5411Sab196087 /* 3813*5411Sab196087 * Heuristic time: It is usually bad form 3814*5411Sab196087 * to assume that specific section names 3815*5411Sab196087 * have a given meaning. However, the 3816*5411Sab196087 * ELF ABI does specify a few such names. Try 3817*5411Sab196087 * to match them: 3818*5411Sab196087 */ 3819*5411Sab196087 if (strcmp(_cache->c_name, 3820*5411Sab196087 MSG_ORIG(MSG_ELF_INTERP)) == 0) 3821*5411Sab196087 flags |= FLG_SHOW_INTERP; 3822*5411Sab196087 else if (strcmp(_cache->c_name, 3823*5411Sab196087 MSG_ORIG(MSG_ELF_GOT)) == 0) 3824*5411Sab196087 flags |= FLG_SHOW_GOT; 3825*5411Sab196087 break; 3826*5411Sab196087 3827*5411Sab196087 case SHT_SYMTAB: 3828*5411Sab196087 case SHT_DYNSYM: 3829*5411Sab196087 case SHT_SUNW_LDYNSYM: 3830*5411Sab196087 case SHT_SUNW_versym: 3831*5411Sab196087 case SHT_SYMTAB_SHNDX: 3832*5411Sab196087 flags |= FLG_SHOW_SYMBOLS; 3833*5411Sab196087 break; 3834*5411Sab196087 3835*5411Sab196087 case SHT_RELA: 3836*5411Sab196087 case SHT_REL: 3837*5411Sab196087 flags |= FLG_SHOW_RELOC; 3838*5411Sab196087 break; 3839*5411Sab196087 3840*5411Sab196087 case SHT_HASH: 3841*5411Sab196087 flags |= FLG_SHOW_HASH; 3842*5411Sab196087 break; 3843*5411Sab196087 3844*5411Sab196087 case SHT_DYNAMIC: 3845*5411Sab196087 flags |= FLG_SHOW_DYNAMIC; 3846*5411Sab196087 break; 3847*5411Sab196087 3848*5411Sab196087 case SHT_NOTE: 3849*5411Sab196087 flags |= FLG_SHOW_NOTE; 3850*5411Sab196087 break; 3851*5411Sab196087 3852*5411Sab196087 case SHT_GROUP: 3853*5411Sab196087 flags |= FLG_SHOW_GROUP; 3854*5411Sab196087 break; 3855*5411Sab196087 3856*5411Sab196087 case SHT_SUNW_symsort: 3857*5411Sab196087 case SHT_SUNW_tlssort: 3858*5411Sab196087 flags |= FLG_SHOW_SORT; 3859*5411Sab196087 break; 3860*5411Sab196087 3861*5411Sab196087 case SHT_SUNW_cap: 3862*5411Sab196087 flags |= FLG_SHOW_CAP; 3863*5411Sab196087 break; 3864*5411Sab196087 3865*5411Sab196087 case SHT_SUNW_move: 3866*5411Sab196087 flags |= FLG_SHOW_MOVE; 3867*5411Sab196087 break; 3868*5411Sab196087 3869*5411Sab196087 case SHT_SUNW_syminfo: 3870*5411Sab196087 flags |= FLG_SHOW_SYMINFO; 3871*5411Sab196087 break; 3872*5411Sab196087 3873*5411Sab196087 case SHT_SUNW_verdef: 3874*5411Sab196087 case SHT_SUNW_verneed: 3875*5411Sab196087 flags |= FLG_SHOW_VERSIONS; 3876*5411Sab196087 break; 3877*5411Sab196087 3878*5411Sab196087 case SHT_AMD64_UNWIND: 3879*5411Sab196087 flags |= FLG_SHOW_UNWIND; 3880*5411Sab196087 break; 3881*5411Sab196087 } 3882*5411Sab196087 } 3883*5411Sab196087 } 3884*5411Sab196087 3885*5411Sab196087 3886*5411Sab196087 if (flags & FLG_SHOW_SHDR) 38874168Sab196087 sections(file, cache, shnum, ehdr); 38880Sstevel@tonic-gate 3889*5411Sab196087 if (flags & FLG_SHOW_INTERP) 38901618Srie interp(file, cache, shnum, phnum, elf); 38910Sstevel@tonic-gate 38923875Sab196087 versions(cache, shnum, file, flags, &versym); 38930Sstevel@tonic-gate 3894*5411Sab196087 if (flags & FLG_SHOW_SYMBOLS) 38954168Sab196087 symbols(cache, shnum, ehdr, &versym, file, flags); 38960Sstevel@tonic-gate 3897*5411Sab196087 if (flags & FLG_SHOW_SORT) 38984168Sab196087 sunw_sort(cache, shnum, ehdr, &versym, file, flags); 38993492Sab196087 3900*5411Sab196087 if (flags & FLG_SHOW_HASH) 39014168Sab196087 hash(cache, shnum, file, flags); 39020Sstevel@tonic-gate 3903*5411Sab196087 if (flags & FLG_SHOW_GOT) 39041618Srie got(cache, shnum, ehdr, file, flags); 39050Sstevel@tonic-gate 3906*5411Sab196087 if (flags & FLG_SHOW_GROUP) 39074168Sab196087 group(cache, shnum, file, flags); 39080Sstevel@tonic-gate 3909*5411Sab196087 if (flags & FLG_SHOW_SYMINFO) 39100Sstevel@tonic-gate syminfo(cache, shnum, file); 39110Sstevel@tonic-gate 3912*5411Sab196087 if (flags & FLG_SHOW_RELOC) 39134168Sab196087 reloc(cache, shnum, ehdr, file, flags); 39140Sstevel@tonic-gate 3915*5411Sab196087 if (flags & FLG_SHOW_DYNAMIC) 39161618Srie dynamic(cache, shnum, ehdr, file); 39170Sstevel@tonic-gate 3918*5411Sab196087 if (flags & FLG_SHOW_NOTE) 39194168Sab196087 note(cache, shnum, file); 39200Sstevel@tonic-gate 3921*5411Sab196087 if (flags & FLG_SHOW_MOVE) 39224168Sab196087 move(cache, shnum, file, flags); 39230Sstevel@tonic-gate 3924*5411Sab196087 if (flags & FLG_CALC_CHECKSUM) 39250Sstevel@tonic-gate checksum(elf); 39260Sstevel@tonic-gate 3927*5411Sab196087 if (flags & FLG_SHOW_CAP) 39281618Srie cap(file, cache, shnum, phnum, ehdr, elf); 39290Sstevel@tonic-gate 3930*5411Sab196087 if (flags & FLG_SHOW_UNWIND) 39314168Sab196087 unwind(cache, shnum, phnum, ehdr, file, elf); 39320Sstevel@tonic-gate 39334665Sab196087 39344665Sab196087 /* Release the memory used to cache section headers */ 3935*5411Sab196087 done: 3936*5411Sab196087 if (flags & FLG_CTL_FAKESHDR) 39374665Sab196087 fake_shdr_cache_free(cache, shnum); 39384665Sab196087 else 39394665Sab196087 free(cache); 3940*5411Sab196087 3941*5411Sab196087 return (ret); 39420Sstevel@tonic-gate } 3943