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 * 513875Sab196087 * Note that the value of the gnu field is a hueristic guess, 523875Sab196087 * based on the section names. 533875Sab196087 */ 543875Sab196087 typedef struct { 553875Sab196087 Cache *cache; /* Pointer to cache entry for VERSYM */ 563875Sab196087 Versym *data; /* Pointer to versym array */ 573875Sab196087 int num_verdef; /* # of versions defined in object */ 583875Sab196087 int gnu; /* True if we think obj produced by GNU tools */ 593875Sab196087 } VERSYM_STATE; 603875Sab196087 613875Sab196087 /* 623875Sab196087 * SYMTBL_STATE is used to maintain information about a single symbol 633875Sab196087 * table section, for use by the routines that display symbol information. 643875Sab196087 */ 653875Sab196087 typedef struct { 663875Sab196087 const char *file; /* Name of file */ 673875Sab196087 Ehdr *ehdr; /* ELF header for file */ 683875Sab196087 Cache *cache; /* Cache of all section headers */ 693875Sab196087 Word shnum; /* # of sections in cache */ 703875Sab196087 Cache *seccache; /* Cache of symbol table section hdr */ 713875Sab196087 Word secndx; /* Index of symbol table section hdr */ 723875Sab196087 const char *secname; /* Name of section */ 733875Sab196087 uint_t flags; /* Command line option flags */ 743875Sab196087 struct { /* Extended section index data */ 753875Sab196087 int checked; /* TRUE if already checked for shxndx */ 763875Sab196087 Word *data; /* NULL, or extended section index */ 773875Sab196087 /* used for symbol table entries */ 783875Sab196087 uint_t n; /* # items in shxndx.data */ 793875Sab196087 } shxndx; 803875Sab196087 VERSYM_STATE *versym; /* NULL, or associated VERSYM section */ 813875Sab196087 Sym *sym; /* Array of symbols */ 823875Sab196087 Word symn; /* # of symbols */ 833875Sab196087 } SYMTBL_STATE; 843875Sab196087 853875Sab196087 863875Sab196087 870Sstevel@tonic-gate /* 880Sstevel@tonic-gate * Focal point for verifying symbol names. 890Sstevel@tonic-gate */ 900Sstevel@tonic-gate static const char * 911618Srie string(Cache *refsec, Word ndx, Cache *strsec, const char *file, Word name) 920Sstevel@tonic-gate { 934063Sab196087 /* 944063Sab196087 * If an error in this routine is due to a property of the string 954063Sab196087 * section, as opposed to a bad offset into the section (a property of 964063Sab196087 * the referencing section), then we will detect the same error on 974063Sab196087 * every call involving those sections. We use these static variables 984063Sab196087 * to retain the information needed to only issue each such error once. 994063Sab196087 */ 1004063Sab196087 static Cache *last_refsec; /* Last referencing section seen */ 1014063Sab196087 static int strsec_err; /* True if error issued */ 1024063Sab196087 1033492Sab196087 const char *strs; 1043492Sab196087 Word strn; 1050Sstevel@tonic-gate 1063466Srie if (strsec->c_data == NULL) 1073466Srie return (NULL); 1083466Srie 1093492Sab196087 strs = (char *)strsec->c_data->d_buf; 1103492Sab196087 strn = strsec->c_data->d_size; 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate /* 1134063Sab196087 * We only print a diagnostic regarding a bad string table once per 1144063Sab196087 * input section being processed. If the refsec has changed, reset 1154063Sab196087 * our retained error state. 1160Sstevel@tonic-gate */ 1174063Sab196087 if (last_refsec != refsec) { 1184063Sab196087 last_refsec = refsec; 1194063Sab196087 strsec_err = 0; 1204063Sab196087 } 1214063Sab196087 1224063Sab196087 /* Verify that strsec really is a string table */ 1234063Sab196087 if (strsec->c_shdr->sh_type != SHT_STRTAB) { 1244063Sab196087 if (!strsec_err) { 1254063Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_NOTSTRTAB), 1264063Sab196087 file, strsec->c_ndx, refsec->c_ndx); 1274063Sab196087 strsec_err = 1; 1284063Sab196087 } 1294063Sab196087 return (MSG_INTL(MSG_STR_UNKNOWN)); 1300Sstevel@tonic-gate } 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate /* 1330Sstevel@tonic-gate * Is the string table offset within range of the available strings? 1340Sstevel@tonic-gate */ 1350Sstevel@tonic-gate if (name >= strn) { 1360Sstevel@tonic-gate /* 1370Sstevel@tonic-gate * Do we have a empty string table? 1380Sstevel@tonic-gate */ 1390Sstevel@tonic-gate if (strs == 0) { 1404063Sab196087 if (!strsec_err) { 1410Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 1420Sstevel@tonic-gate file, strsec->c_name); 1434063Sab196087 strsec_err = 1; 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate } else { 1460Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSTOFF), 1471618Srie file, refsec->c_name, EC_WORD(ndx), strsec->c_name, 1481618Srie EC_WORD(name), EC_WORD(strn - 1)); 1490Sstevel@tonic-gate } 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate /* 1520Sstevel@tonic-gate * Return the empty string so that the calling function can 1530Sstevel@tonic-gate * continue it's output diagnostics. 1540Sstevel@tonic-gate */ 1550Sstevel@tonic-gate return (MSG_INTL(MSG_STR_UNKNOWN)); 1560Sstevel@tonic-gate } 1570Sstevel@tonic-gate return (strs + name); 1580Sstevel@tonic-gate } 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate /* 1611618Srie * Relocations can reference section symbols and standard symbols. If the 1621618Srie * former, establish the section name. 1631618Srie */ 1641618Srie static const char * 1651618Srie relsymname(Cache *cache, Cache *csec, Cache *strsec, Word symndx, Word symnum, 1661618Srie Word relndx, Sym *syms, char *secstr, size_t secsz, const char *file, 1671618Srie uint_t flags) 1681618Srie { 1691618Srie Sym *sym; 1701618Srie 1711618Srie if (symndx >= symnum) { 1721618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_RELBADSYMNDX), 1731618Srie file, EC_WORD(symndx), EC_WORD(relndx)); 1741618Srie return (MSG_INTL(MSG_STR_UNKNOWN)); 1751618Srie } 1761618Srie 1771618Srie sym = (Sym *)(syms + symndx); 1781618Srie 1791618Srie /* 1801618Srie * If the symbol represents a section offset construct an appropriate 1811618Srie * string. 1821618Srie */ 1831618Srie if ((ELF_ST_TYPE(sym->st_info) == STT_SECTION) && (sym->st_name == 0)) { 1841618Srie if (flags & FLG_LONGNAME) 1851618Srie (void) snprintf(secstr, secsz, 1861618Srie MSG_INTL(MSG_STR_L_SECTION), 1871618Srie cache[sym->st_shndx].c_name); 1881618Srie else 1891618Srie (void) snprintf(secstr, secsz, 1901618Srie MSG_INTL(MSG_STR_SECTION), 1911618Srie cache[sym->st_shndx].c_name); 1921618Srie return ((const char *)secstr); 1931618Srie } 1941618Srie 1951618Srie return (string(csec, symndx, strsec, file, sym->st_name)); 1961618Srie } 1971618Srie 1981618Srie /* 1991618Srie * Focal point for establishing a string table section. Data such as the 2001618Srie * dynamic information simply points to a string table. Data such as 2011618Srie * relocations, reference a symbol table, which in turn is associated with a 2021618Srie * string table. 2030Sstevel@tonic-gate */ 2040Sstevel@tonic-gate static int 2051618Srie stringtbl(Cache *cache, int symtab, Word ndx, Word shnum, const char *file, 2061618Srie Word *symnum, Cache **symsec, Cache **strsec) 2071618Srie { 2081618Srie Shdr *shdr = cache[ndx].c_shdr; 2091618Srie 2101618Srie if (symtab) { 2111618Srie /* 2121618Srie * Validate the symbol table section. 2131618Srie */ 2141618Srie if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) { 2151618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK), 2161618Srie file, cache[ndx].c_name, EC_WORD(shdr->sh_link)); 2171618Srie return (0); 2181618Srie } 2193466Srie if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) { 2203466Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 2213466Srie file, cache[ndx].c_name); 2223466Srie return (0); 2233466Srie } 2241618Srie 2251618Srie /* 2261618Srie * Obtain, and verify the symbol table data. 2271618Srie */ 2283466Srie if ((cache[ndx].c_data == NULL) || 2293466Srie (cache[ndx].c_data->d_buf == NULL)) { 2301618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 2311618Srie file, cache[ndx].c_name); 2321618Srie return (0); 2331618Srie } 2341618Srie 2351618Srie /* 2361618Srie * Establish the string table index. 2371618Srie */ 2381618Srie ndx = shdr->sh_link; 2391618Srie shdr = cache[ndx].c_shdr; 2401618Srie 2411618Srie /* 2421618Srie * Return symbol table information. 2431618Srie */ 2441618Srie if (symnum) 2451618Srie *symnum = (shdr->sh_size / shdr->sh_entsize); 2461618Srie if (symsec) 2471618Srie *symsec = &cache[ndx]; 2481618Srie } 2491618Srie 2501618Srie /* 2511618Srie * Validate the associated string table section. 2521618Srie */ 2531618Srie if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) { 2541618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK), 2551618Srie file, cache[ndx].c_name, EC_WORD(shdr->sh_link)); 2561618Srie return (0); 2571618Srie } 2581618Srie 2591618Srie if (strsec) 2601618Srie *strsec = &cache[shdr->sh_link]; 2611618Srie 2621618Srie return (1); 2631618Srie } 2641618Srie 2651618Srie /* 2661618Srie * Lookup a symbol and set Sym accordingly. 2671618Srie */ 2681618Srie static int 2691618Srie symlookup(const char *name, Cache *cache, Word shnum, Sym **sym, 2700Sstevel@tonic-gate Cache *symtab, const char *file) 2710Sstevel@tonic-gate { 2721618Srie Shdr *shdr; 2731618Srie Word symn, cnt; 2741618Srie Sym *syms; 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate if (symtab == 0) 2770Sstevel@tonic-gate return (0); 2780Sstevel@tonic-gate 2791618Srie shdr = symtab->c_shdr; 2801618Srie 2810Sstevel@tonic-gate /* 2820Sstevel@tonic-gate * Determine the symbol data and number. 2830Sstevel@tonic-gate */ 2840Sstevel@tonic-gate if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) { 2850Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 2860Sstevel@tonic-gate file, symtab->c_name); 2870Sstevel@tonic-gate return (0); 2880Sstevel@tonic-gate } 2893466Srie if (symtab->c_data == NULL) 2903466Srie return (0); 2913466Srie 2920Sstevel@tonic-gate /* LINTED */ 2931618Srie symn = (Word)(shdr->sh_size / shdr->sh_entsize); 2941618Srie syms = (Sym *)symtab->c_data->d_buf; 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate /* 2970Sstevel@tonic-gate * Get the associated string table section. 2980Sstevel@tonic-gate */ 2990Sstevel@tonic-gate if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) { 3000Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK), 3011618Srie file, symtab->c_name, EC_WORD(shdr->sh_link)); 3020Sstevel@tonic-gate return (0); 3030Sstevel@tonic-gate } 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate /* 3060Sstevel@tonic-gate * Loop through the symbol table to find a match. 3070Sstevel@tonic-gate */ 3081618Srie for (cnt = 0; cnt < symn; syms++, cnt++) { 3091618Srie const char *symname; 3100Sstevel@tonic-gate 3111618Srie symname = string(symtab, cnt, &cache[shdr->sh_link], file, 3121618Srie syms->st_name); 3130Sstevel@tonic-gate 3141618Srie if (symname && (strcmp(name, symname) == 0)) { 3151618Srie *sym = syms; 3160Sstevel@tonic-gate return (1); 3170Sstevel@tonic-gate } 3180Sstevel@tonic-gate } 3190Sstevel@tonic-gate return (0); 3200Sstevel@tonic-gate } 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate /* 3230Sstevel@tonic-gate * Print section headers. 3240Sstevel@tonic-gate */ 3250Sstevel@tonic-gate static void 3264168Sab196087 sections(const char *file, Cache *cache, Word shnum, Ehdr *ehdr) 3270Sstevel@tonic-gate { 3281618Srie size_t seccnt; 3290Sstevel@tonic-gate 3301618Srie for (seccnt = 1; seccnt < shnum; seccnt++) { 3311618Srie Cache *_cache = &cache[seccnt]; 3321618Srie Shdr *shdr = _cache->c_shdr; 3331618Srie const char *secname = _cache->c_name; 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate /* 3360Sstevel@tonic-gate * Although numerous section header entries can be zero, it's 3373862Srie * usually a sign of trouble if the type is zero. 3380Sstevel@tonic-gate */ 3390Sstevel@tonic-gate if (shdr->sh_type == 0) { 3400Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHTYPE), 3411618Srie file, secname, EC_WORD(shdr->sh_type)); 3420Sstevel@tonic-gate } 3433862Srie 3444168Sab196087 if (!match(0, secname, seccnt)) 3453862Srie continue; 3460Sstevel@tonic-gate 3471324Srie /* 3481324Srie * Identify any sections that are suspicious. A .got section 3491324Srie * shouldn't exist in a relocatable object. 3501324Srie */ 3511324Srie if (ehdr->e_type == ET_REL) { 3521618Srie if (strncmp(secname, MSG_ORIG(MSG_ELF_GOT), 3531324Srie MSG_ELF_GOT_SIZE) == 0) { 3541324Srie (void) fprintf(stderr, 3551618Srie MSG_INTL(MSG_GOT_UNEXPECTED), file, 3561618Srie secname); 3571324Srie } 3581324Srie } 3591324Srie 3601618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 3611618Srie dbg_print(0, MSG_INTL(MSG_ELF_SHDR), EC_WORD(seccnt), secname); 3621618Srie Elf_shdr(0, ehdr->e_machine, shdr); 3630Sstevel@tonic-gate } 3640Sstevel@tonic-gate } 3650Sstevel@tonic-gate 3661618Srie /* 3671618Srie * A couple of instances of unwind data are printed as tables of 8 data items 3681618Srie * expressed as 0x?? integers. 3691618Srie */ 3701618Srie #define UNWINDTBLSZ 10 + (8 * 5) + 1 3711618Srie 3721618Srie static void 3731618Srie unwindtbl(uint64_t *ndx, uint_t len, uchar_t *data, uint64_t doff, 3741618Srie const char *msg, const char *pre, size_t plen) 3751618Srie { 3761618Srie char buffer[UNWINDTBLSZ]; 3771618Srie uint_t boff = plen, cnt = 0; 3781618Srie 3791618Srie dbg_print(0, msg); 3801618Srie (void) strncpy(buffer, pre, UNWINDTBLSZ); 3811618Srie 3821618Srie while (*ndx < (len + 4)) { 3831618Srie if (cnt == 8) { 3841618Srie dbg_print(0, buffer); 3851618Srie boff = plen; 3861618Srie cnt = 0; 3871618Srie } 3881618Srie (void) snprintf(&buffer[boff], UNWINDTBLSZ - boff, 3891618Srie MSG_ORIG(MSG_UNW_TBLENTRY), data[doff + (*ndx)++]); 3901618Srie boff += 5; 3911618Srie cnt++; 3921618Srie } 3931618Srie if (cnt) 3941618Srie dbg_print(0, buffer); 3951618Srie } 3961618Srie 3971618Srie /* 3981618Srie * Obtain a specified Phdr entry. 3991618Srie */ 4001618Srie static Phdr * 4011618Srie getphdr(Word phnum, Word type, const char *file, Elf *elf) 4021618Srie { 4031618Srie Word cnt; 4041618Srie Phdr *phdr; 4051618Srie 4061618Srie if ((phdr = elf_getphdr(elf)) == NULL) { 4071618Srie failure(file, MSG_ORIG(MSG_ELF_GETPHDR)); 4081618Srie return (0); 4091618Srie } 4101618Srie 4111618Srie for (cnt = 0; cnt < phnum; phdr++, cnt++) { 4121618Srie if (phdr->p_type == type) 4131618Srie return (phdr); 4141618Srie } 4151618Srie return (0); 4161618Srie } 4171618Srie 4180Sstevel@tonic-gate static void 4194168Sab196087 unwind(Cache *cache, Word shnum, Word phnum, Ehdr *ehdr, const char *file, 4204168Sab196087 Elf *elf) 4210Sstevel@tonic-gate { 4221618Srie Word cnt; 4231618Srie Phdr *uphdr = 0; 4241618Srie 4250Sstevel@tonic-gate /* 4261618Srie * For the moment - UNWIND is only relevant for a AMD64 object. 4270Sstevel@tonic-gate */ 4280Sstevel@tonic-gate if (ehdr->e_machine != EM_AMD64) 4291618Srie return; 4300Sstevel@tonic-gate 4311618Srie if (phnum) 4321618Srie uphdr = getphdr(phnum, PT_SUNW_UNWIND, file, elf); 4330Sstevel@tonic-gate 4340Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 4351618Srie Cache *_cache = &cache[cnt]; 4361618Srie Shdr *shdr = _cache->c_shdr; 4371618Srie uchar_t *data; 4380Sstevel@tonic-gate size_t datasize; 4391618Srie uint64_t off, ndx, frame_ptr, fde_cnt, tabndx; 4401618Srie uint_t vers, frame_ptr_enc, fde_cnt_enc, table_enc; 4410Sstevel@tonic-gate 4420Sstevel@tonic-gate /* 4431618Srie * AMD64 - this is a strmcp() just to find the gcc produced 4441618Srie * sections. Soon gcc should be setting the section type - and 4451618Srie * we'll not need this strcmp(). 4460Sstevel@tonic-gate */ 4470Sstevel@tonic-gate if ((shdr->sh_type != SHT_AMD64_UNWIND) && 4480Sstevel@tonic-gate (strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRM), 4490Sstevel@tonic-gate MSG_SCN_FRM_SIZE) != 0) && 4500Sstevel@tonic-gate (strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRMHDR), 4510Sstevel@tonic-gate MSG_SCN_FRMHDR_SIZE) != 0)) 4520Sstevel@tonic-gate continue; 4534168Sab196087 4544168Sab196087 if (!match(0, _cache->c_name, cnt)) 4550Sstevel@tonic-gate continue; 4560Sstevel@tonic-gate 4573466Srie if (_cache->c_data == NULL) 4583466Srie continue; 4593466Srie 4601618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 4611618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_UNWIND), _cache->c_name); 4620Sstevel@tonic-gate 4631618Srie data = (uchar_t *)(_cache->c_data->d_buf); 4640Sstevel@tonic-gate datasize = _cache->c_data->d_size; 4650Sstevel@tonic-gate off = 0; 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate /* 4680Sstevel@tonic-gate * Is this a .eh_frame_hdr 4690Sstevel@tonic-gate */ 4701618Srie if ((uphdr && (shdr->sh_addr == uphdr->p_vaddr)) || 4710Sstevel@tonic-gate (strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRMHDR), 4721618Srie MSG_SCN_FRMHDR_SIZE) == 0)) { 4731618Srie 4741618Srie dbg_print(0, MSG_ORIG(MSG_UNW_FRMHDR)); 4751618Srie ndx = 0; 4760Sstevel@tonic-gate 4771618Srie vers = data[ndx++]; 4781618Srie frame_ptr_enc = data[ndx++]; 4791618Srie fde_cnt_enc = data[ndx++]; 4801618Srie table_enc = data[ndx++]; 4810Sstevel@tonic-gate 4821618Srie dbg_print(0, MSG_ORIG(MSG_UNW_FRMVERS), vers); 4830Sstevel@tonic-gate 4841618Srie frame_ptr = dwarf_ehe_extract(data, &ndx, frame_ptr_enc, 4851618Srie ehdr->e_ident, shdr->sh_addr + ndx); 4860Sstevel@tonic-gate 4871618Srie dbg_print(0, MSG_ORIG(MSG_UNW_FRPTRENC), 4881618Srie conv_dwarf_ehe(frame_ptr_enc), EC_XWORD(frame_ptr)); 4891618Srie 4901618Srie fde_cnt = dwarf_ehe_extract(data, &ndx, fde_cnt_enc, 4911618Srie ehdr->e_ident, shdr->sh_addr + ndx); 4920Sstevel@tonic-gate 4931618Srie dbg_print(0, MSG_ORIG(MSG_UNW_FDCNENC), 4941618Srie conv_dwarf_ehe(fde_cnt_enc), EC_XWORD(fde_cnt)); 4951618Srie dbg_print(0, MSG_ORIG(MSG_UNW_TABENC), 4961618Srie conv_dwarf_ehe(table_enc)); 4971618Srie dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTAB1)); 4981618Srie dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTAB2)); 4990Sstevel@tonic-gate 5001618Srie for (tabndx = 0; tabndx < fde_cnt; tabndx++) { 5011618Srie dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTABENT), 5021618Srie EC_XWORD(dwarf_ehe_extract(data, &ndx, 5031618Srie table_enc, ehdr->e_ident, shdr->sh_addr)), 5041618Srie EC_XWORD(dwarf_ehe_extract(data, &ndx, 5051618Srie table_enc, ehdr->e_ident, shdr->sh_addr))); 5061618Srie } 5071618Srie continue; 5080Sstevel@tonic-gate } 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate /* 5110Sstevel@tonic-gate * Walk the Eh_frame's 5120Sstevel@tonic-gate */ 5130Sstevel@tonic-gate while (off < datasize) { 514*4433Sab196087 uint_t cieid, cielength, cieversion; 515*4433Sab196087 uint_t cieretaddr; 5161618Srie int cieRflag, cieLflag, ciePflag, cieZflag; 5171618Srie uint_t cieaugndx, length, id; 5180Sstevel@tonic-gate uint64_t ciecalign, ciedalign; 5190Sstevel@tonic-gate char *cieaugstr; 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate ndx = 0; 5220Sstevel@tonic-gate /* 5230Sstevel@tonic-gate * extract length in lsb format 5240Sstevel@tonic-gate */ 5250Sstevel@tonic-gate length = LSB32EXTRACT(data + off + ndx); 5260Sstevel@tonic-gate ndx += 4; 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate /* 5290Sstevel@tonic-gate * extract CIE id in lsb format 5300Sstevel@tonic-gate */ 5310Sstevel@tonic-gate id = LSB32EXTRACT(data + off + ndx); 5320Sstevel@tonic-gate ndx += 4; 5330Sstevel@tonic-gate 5340Sstevel@tonic-gate /* 5351618Srie * A CIE record has a id of '0', otherwise this is a 5361618Srie * FDE entry and the 'id' is the CIE pointer. 5370Sstevel@tonic-gate */ 5380Sstevel@tonic-gate if (id == 0) { 5390Sstevel@tonic-gate uint64_t persVal; 5401618Srie 5410Sstevel@tonic-gate cielength = length; 5420Sstevel@tonic-gate cieid = id; 5431618Srie cieLflag = ciePflag = cieRflag = cieZflag = 0; 5440Sstevel@tonic-gate 5451618Srie dbg_print(0, MSG_ORIG(MSG_UNW_CIE), 5461618Srie EC_XWORD(shdr->sh_addr + off)); 5471618Srie dbg_print(0, MSG_ORIG(MSG_UNW_CIELNGTH), 5481618Srie cielength, cieid); 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate cieversion = data[off + ndx]; 5510Sstevel@tonic-gate ndx += 1; 5520Sstevel@tonic-gate cieaugstr = (char *)(&data[off + ndx]); 5530Sstevel@tonic-gate ndx += strlen(cieaugstr) + 1; 5541618Srie 5551618Srie dbg_print(0, MSG_ORIG(MSG_UNW_CIEVERS), 5561618Srie cieversion, cieaugstr); 5571618Srie 5580Sstevel@tonic-gate ciecalign = uleb_extract(&data[off], &ndx); 5590Sstevel@tonic-gate ciedalign = sleb_extract(&data[off], &ndx); 5600Sstevel@tonic-gate cieretaddr = data[off + ndx]; 5610Sstevel@tonic-gate ndx += 1; 5621618Srie 5631618Srie dbg_print(0, MSG_ORIG(MSG_UNW_CIECALGN), 5641618Srie EC_XWORD(ciecalign), EC_XWORD(ciedalign), 5651618Srie cieretaddr); 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate if (cieaugstr[0]) 568*4433Sab196087 dbg_print(0, 569*4433Sab196087 MSG_ORIG(MSG_UNW_CIEAXVAL)); 5701618Srie 5710Sstevel@tonic-gate for (cieaugndx = 0; cieaugstr[cieaugndx]; 5720Sstevel@tonic-gate cieaugndx++) { 5730Sstevel@tonic-gate uint_t val; 5741618Srie 5750Sstevel@tonic-gate switch (cieaugstr[cieaugndx]) { 5760Sstevel@tonic-gate case 'z': 577*4433Sab196087 val = uleb_extract(&data[off], 578*4433Sab196087 &ndx); 579*4433Sab196087 dbg_print(0, 580*4433Sab196087 MSG_ORIG(MSG_UNW_CIEAXSIZ), 581*4433Sab196087 val); 582*4433Sab196087 cieZflag = 1; 583*4433Sab196087 break; 5840Sstevel@tonic-gate case 'P': 585*4433Sab196087 ciePflag = data[off + ndx]; 586*4433Sab196087 ndx += 1; 587*4433Sab196087 588*4433Sab196087 persVal = dwarf_ehe_extract( 589*4433Sab196087 &data[off], &ndx, ciePflag, 590*4433Sab196087 ehdr->e_ident, 591*4433Sab196087 shdr->sh_addr + off + ndx); 592*4433Sab196087 dbg_print(0, 593*4433Sab196087 MSG_ORIG(MSG_UNW_CIEAXPERS), 594*4433Sab196087 ciePflag, 595*4433Sab196087 conv_dwarf_ehe(ciePflag), 596*4433Sab196087 EC_XWORD(persVal)); 597*4433Sab196087 break; 5980Sstevel@tonic-gate case 'R': 599*4433Sab196087 val = data[off + ndx]; 600*4433Sab196087 ndx += 1; 601*4433Sab196087 dbg_print(0, 602*4433Sab196087 MSG_ORIG(MSG_UNW_CIEAXCENC), 603*4433Sab196087 val, conv_dwarf_ehe(val)); 604*4433Sab196087 cieRflag = val; 605*4433Sab196087 break; 6060Sstevel@tonic-gate case 'L': 607*4433Sab196087 val = data[off + ndx]; 608*4433Sab196087 ndx += 1; 609*4433Sab196087 dbg_print(0, 610*4433Sab196087 MSG_ORIG(MSG_UNW_CIEAXLSDA), 611*4433Sab196087 val, conv_dwarf_ehe(val)); 612*4433Sab196087 cieLflag = val; 613*4433Sab196087 break; 6140Sstevel@tonic-gate default: 615*4433Sab196087 dbg_print(0, 616*4433Sab196087 MSG_ORIG(MSG_UNW_CIEAXUNEC), 617*4433Sab196087 cieaugstr[cieaugndx]); 618*4433Sab196087 break; 6190Sstevel@tonic-gate } 6200Sstevel@tonic-gate } 6211618Srie if ((cielength + 4) > ndx) 6221618Srie unwindtbl(&ndx, cielength, data, off, 6231618Srie MSG_ORIG(MSG_UNW_CIECFI), 6241618Srie MSG_ORIG(MSG_UNW_CIEPRE), 6251618Srie MSG_UNW_CIEPRE_SIZE); 6260Sstevel@tonic-gate off += cielength + 4; 6271618Srie 6280Sstevel@tonic-gate } else { 6290Sstevel@tonic-gate uint_t fdelength = length; 6300Sstevel@tonic-gate int fdecieptr = id; 6310Sstevel@tonic-gate uint64_t fdeinitloc, fdeaddrrange; 6320Sstevel@tonic-gate 6331618Srie dbg_print(0, MSG_ORIG(MSG_UNW_FDE), 6341618Srie EC_XWORD(shdr->sh_addr + off)); 6351618Srie dbg_print(0, MSG_ORIG(MSG_UNW_FDELNGTH), 6360Sstevel@tonic-gate fdelength, fdecieptr); 6371618Srie 6380Sstevel@tonic-gate fdeinitloc = dwarf_ehe_extract(&data[off], 6390Sstevel@tonic-gate &ndx, cieRflag, ehdr->e_ident, 6400Sstevel@tonic-gate shdr->sh_addr + off + ndx); 6410Sstevel@tonic-gate fdeaddrrange = dwarf_ehe_extract(&data[off], 6420Sstevel@tonic-gate &ndx, (cieRflag & ~DW_EH_PE_pcrel), 6430Sstevel@tonic-gate ehdr->e_ident, 6440Sstevel@tonic-gate shdr->sh_addr + off + ndx); 6451618Srie 6461618Srie dbg_print(0, MSG_ORIG(MSG_UNW_FDEINITLOC), 6471618Srie EC_XWORD(fdeinitloc), 6481618Srie EC_XWORD(fdeaddrrange)); 6491618Srie 6500Sstevel@tonic-gate if (cieaugstr[0]) 6511618Srie dbg_print(0, 652*4433Sab196087 MSG_ORIG(MSG_UNW_FDEAXVAL)); 6530Sstevel@tonic-gate if (cieZflag) { 6540Sstevel@tonic-gate uint64_t val; 6550Sstevel@tonic-gate val = uleb_extract(&data[off], &ndx); 6561618Srie dbg_print(0, 657*4433Sab196087 MSG_ORIG(MSG_UNW_FDEAXSIZE), 6581618Srie EC_XWORD(val)); 6590Sstevel@tonic-gate if (val & cieLflag) { 660*4433Sab196087 fdeinitloc = dwarf_ehe_extract( 661*4433Sab196087 &data[off], &ndx, cieLflag, 662*4433Sab196087 ehdr->e_ident, 663*4433Sab196087 shdr->sh_addr + off + ndx); 664*4433Sab196087 dbg_print(0, 665*4433Sab196087 MSG_ORIG(MSG_UNW_FDEAXLSDA), 666*4433Sab196087 EC_XWORD(val)); 6670Sstevel@tonic-gate } 6680Sstevel@tonic-gate } 6691618Srie if ((fdelength + 4) > ndx) 6701618Srie unwindtbl(&ndx, fdelength, data, off, 6711618Srie MSG_ORIG(MSG_UNW_FDECFI), 6721618Srie MSG_ORIG(MSG_UNW_FDEPRE), 6731618Srie MSG_UNW_FDEPRE_SIZE); 6740Sstevel@tonic-gate off += fdelength + 4; 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate } 6770Sstevel@tonic-gate } 6780Sstevel@tonic-gate } 6790Sstevel@tonic-gate 6800Sstevel@tonic-gate /* 6810Sstevel@tonic-gate * Print the hardware/software capabilities. For executables and shared objects 6820Sstevel@tonic-gate * this should be accompanied with a program header. 6830Sstevel@tonic-gate */ 6840Sstevel@tonic-gate static void 6851618Srie cap(const char *file, Cache *cache, Word shnum, Word phnum, Ehdr *ehdr, 6861618Srie Elf *elf) 6870Sstevel@tonic-gate { 6881618Srie Word cnt; 6893466Srie Shdr *cshdr = 0; 6903466Srie Cache *ccache; 6911618Srie Off cphdr_off = 0; 6921618Srie Xword cphdr_sz; 6930Sstevel@tonic-gate 6940Sstevel@tonic-gate /* 6950Sstevel@tonic-gate * Determine if a hardware/software capabilities header exists. 6960Sstevel@tonic-gate */ 6971618Srie if (phnum) { 6981618Srie Phdr *phdr; 6990Sstevel@tonic-gate 7001618Srie if ((phdr = elf_getphdr(elf)) == NULL) { 7010Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETPHDR)); 7020Sstevel@tonic-gate return; 7030Sstevel@tonic-gate } 7040Sstevel@tonic-gate 7051618Srie for (cnt = 0; cnt < phnum; phdr++, cnt++) { 7061618Srie if (phdr->p_type == PT_SUNWCAP) { 7071618Srie cphdr_off = phdr->p_offset; 7081618Srie cphdr_sz = phdr->p_filesz; 7091618Srie break; 7101618Srie } 7110Sstevel@tonic-gate } 7120Sstevel@tonic-gate } 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate /* 7150Sstevel@tonic-gate * Determine if a hardware/software capabilities section exists. 7160Sstevel@tonic-gate */ 7170Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 7181618Srie Cache *_cache = &cache[cnt]; 7191618Srie Shdr *shdr = _cache->c_shdr; 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate if (shdr->sh_type != SHT_SUNW_cap) 7220Sstevel@tonic-gate continue; 7230Sstevel@tonic-gate 7240Sstevel@tonic-gate if (cphdr_off && ((cphdr_off < shdr->sh_offset) || 7250Sstevel@tonic-gate (cphdr_off + cphdr_sz) > (shdr->sh_offset + shdr->sh_size))) 7260Sstevel@tonic-gate continue; 7270Sstevel@tonic-gate 7283466Srie if (_cache->c_data == NULL) 7293466Srie continue; 7303466Srie 7310Sstevel@tonic-gate ccache = _cache; 7320Sstevel@tonic-gate cshdr = shdr; 7330Sstevel@tonic-gate break; 7340Sstevel@tonic-gate } 7350Sstevel@tonic-gate 7360Sstevel@tonic-gate if ((cshdr == 0) && (cphdr_off == 0)) 7370Sstevel@tonic-gate return; 7380Sstevel@tonic-gate 7393466Srie if ((cshdr->sh_entsize == 0) || (cshdr->sh_size == 0)) { 7403466Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 7413466Srie file, ccache->c_name); 7423466Srie return; 7433466Srie } 7443466Srie 7450Sstevel@tonic-gate /* 7460Sstevel@tonic-gate * Print the hardware/software capabilities section. 7470Sstevel@tonic-gate */ 7480Sstevel@tonic-gate if (cshdr) { 7491618Srie Word ndx, capn; 7503492Sab196087 Cap *cap = (Cap *)ccache->c_data->d_buf; 7510Sstevel@tonic-gate 7521618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 7531618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_CAP), ccache->c_name); 7540Sstevel@tonic-gate 7551618Srie Elf_cap_title(0); 7561618Srie 7571618Srie capn = (Word)(cshdr->sh_size / cshdr->sh_entsize); 7580Sstevel@tonic-gate 7591618Srie for (ndx = 0; ndx < capn; cap++, ndx++) { 7601618Srie if (cap->c_tag != CA_SUNW_NULL) 7611618Srie Elf_cap_entry(0, cap, ndx, ehdr->e_machine); 7620Sstevel@tonic-gate } 7630Sstevel@tonic-gate } else 7640Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP1), file); 7650Sstevel@tonic-gate 7660Sstevel@tonic-gate /* 7670Sstevel@tonic-gate * If this object is an executable or shared object, then the 7680Sstevel@tonic-gate * hardware/software capabilities section should have an accompanying 7690Sstevel@tonic-gate * program header. 7700Sstevel@tonic-gate */ 7710Sstevel@tonic-gate if (cshdr && ((ehdr->e_type == ET_EXEC) || (ehdr->e_type == ET_DYN))) { 7720Sstevel@tonic-gate if (cphdr_off == 0) 7730Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP2), 7740Sstevel@tonic-gate file, ccache->c_name); 7750Sstevel@tonic-gate else if ((cphdr_off != cshdr->sh_offset) || 7760Sstevel@tonic-gate (cphdr_sz != cshdr->sh_size)) 7770Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP3), 7780Sstevel@tonic-gate file, ccache->c_name); 7790Sstevel@tonic-gate } 7800Sstevel@tonic-gate } 7810Sstevel@tonic-gate 7820Sstevel@tonic-gate /* 7830Sstevel@tonic-gate * Print the interpretor. 7840Sstevel@tonic-gate */ 7850Sstevel@tonic-gate static void 7861618Srie interp(const char *file, Cache *cache, Word shnum, Word phnum, Elf *elf) 7870Sstevel@tonic-gate { 7881618Srie Word cnt; 7891618Srie Shdr *ishdr = 0; 7901618Srie Cache *icache; 7911618Srie Off iphdr_off = 0; 7921618Srie Xword iphdr_fsz; 7930Sstevel@tonic-gate 7940Sstevel@tonic-gate /* 7950Sstevel@tonic-gate * Determine if an interp header exists. 7960Sstevel@tonic-gate */ 7971618Srie if (phnum) { 7981618Srie Phdr *phdr; 7990Sstevel@tonic-gate 8001618Srie if ((phdr = getphdr(phnum, PT_INTERP, file, elf)) != 0) { 8011618Srie iphdr_off = phdr->p_offset; 8021618Srie iphdr_fsz = phdr->p_filesz; 8030Sstevel@tonic-gate } 8040Sstevel@tonic-gate } 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate if (iphdr_off == 0) 8070Sstevel@tonic-gate return; 8080Sstevel@tonic-gate 8090Sstevel@tonic-gate /* 8100Sstevel@tonic-gate * Determine if an interp section exists. 8110Sstevel@tonic-gate */ 8120Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 8131618Srie Cache *_cache = &cache[cnt]; 8141618Srie Shdr *shdr = _cache->c_shdr; 8150Sstevel@tonic-gate 8160Sstevel@tonic-gate /* 8170Sstevel@tonic-gate * Scan sections to find a section which contains the PT_INTERP 8180Sstevel@tonic-gate * string. The target section can't be in a NOBITS section. 8190Sstevel@tonic-gate */ 8200Sstevel@tonic-gate if ((shdr->sh_type == SHT_NOBITS) || 8210Sstevel@tonic-gate (iphdr_off < shdr->sh_offset) || 8221618Srie (iphdr_off + iphdr_fsz) > (shdr->sh_offset + shdr->sh_size)) 8230Sstevel@tonic-gate continue; 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate icache = _cache; 8260Sstevel@tonic-gate ishdr = shdr; 8270Sstevel@tonic-gate break; 8280Sstevel@tonic-gate } 8290Sstevel@tonic-gate 8300Sstevel@tonic-gate /* 8310Sstevel@tonic-gate * Print the interpreter string based on the offset defined in the 8320Sstevel@tonic-gate * program header, as this is the offset used by the kernel. 8330Sstevel@tonic-gate */ 8343466Srie if (ishdr && icache->c_data) { 8351618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 8361618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_INTERP), icache->c_name); 8371618Srie dbg_print(0, MSG_ORIG(MSG_FMT_INDENT), 8380Sstevel@tonic-gate (char *)icache->c_data->d_buf + 8390Sstevel@tonic-gate (iphdr_off - ishdr->sh_offset)); 8400Sstevel@tonic-gate } else 8410Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_WARN_INVINTERP1), file); 8420Sstevel@tonic-gate 8430Sstevel@tonic-gate /* 8440Sstevel@tonic-gate * If there are any inconsistences between the program header and 8450Sstevel@tonic-gate * section information, flag them. 8460Sstevel@tonic-gate */ 8470Sstevel@tonic-gate if (ishdr && ((iphdr_off != ishdr->sh_offset) || 8481618Srie (iphdr_fsz != ishdr->sh_size))) { 8490Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_WARN_INVINTERP2), file, 8500Sstevel@tonic-gate icache->c_name); 8510Sstevel@tonic-gate } 8520Sstevel@tonic-gate } 8530Sstevel@tonic-gate 8540Sstevel@tonic-gate /* 8550Sstevel@tonic-gate * Print the syminfo section. 8560Sstevel@tonic-gate */ 8570Sstevel@tonic-gate static void 8581618Srie syminfo(Cache *cache, Word shnum, const char *file) 8590Sstevel@tonic-gate { 8601618Srie Shdr *infoshdr; 8611618Srie Syminfo *info; 8621618Srie Sym *syms; 8631618Srie Dyn *dyns; 8641618Srie Word infonum, cnt, ndx, symnum; 8651618Srie Cache *infocache = 0, *symsec, *strsec; 8660Sstevel@tonic-gate 8670Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 8681618Srie if (cache[cnt].c_shdr->sh_type == SHT_SUNW_syminfo) { 8691618Srie infocache = &cache[cnt]; 8700Sstevel@tonic-gate break; 8710Sstevel@tonic-gate } 8720Sstevel@tonic-gate } 8731618Srie if (infocache == 0) 8740Sstevel@tonic-gate return; 8750Sstevel@tonic-gate 8761618Srie infoshdr = infocache->c_shdr; 8771618Srie if ((infoshdr->sh_entsize == 0) || (infoshdr->sh_size == 0)) { 8780Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 8791618Srie file, infocache->c_name); 8800Sstevel@tonic-gate return; 8810Sstevel@tonic-gate } 8823466Srie if (infocache->c_data == NULL) 8833466Srie return; 8843466Srie 8851618Srie infonum = (Word)(infoshdr->sh_size / infoshdr->sh_entsize); 8861618Srie info = (Syminfo *)infocache->c_data->d_buf; 8870Sstevel@tonic-gate 8880Sstevel@tonic-gate /* 8890Sstevel@tonic-gate * Get the data buffer of the associated dynamic section. 8900Sstevel@tonic-gate */ 8911618Srie if ((infoshdr->sh_info == 0) || (infoshdr->sh_info >= shnum)) { 8920Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO), 8931618Srie file, infocache->c_name, EC_WORD(infoshdr->sh_info)); 8940Sstevel@tonic-gate return; 8950Sstevel@tonic-gate } 8963466Srie if (cache[infoshdr->sh_info].c_data == NULL) 8973466Srie return; 8983466Srie 8991618Srie dyns = cache[infoshdr->sh_info].c_data->d_buf; 9001618Srie if (dyns == 0) { 9010Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 9021618Srie file, cache[infoshdr->sh_info].c_name); 9030Sstevel@tonic-gate return; 9040Sstevel@tonic-gate } 9050Sstevel@tonic-gate 9060Sstevel@tonic-gate /* 9071618Srie * Get the data buffer for the associated symbol table and string table. 9080Sstevel@tonic-gate */ 9091618Srie if (stringtbl(cache, 1, cnt, shnum, file, 9101618Srie &symnum, &symsec, &strsec) == 0) 9110Sstevel@tonic-gate return; 9120Sstevel@tonic-gate 9131618Srie syms = symsec->c_data->d_buf; 9140Sstevel@tonic-gate 9151618Srie /* 9161618Srie * Loop through the syminfo entries. 9171618Srie */ 9181618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 9191618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMINFO), infocache->c_name); 9201618Srie Elf_syminfo_title(0); 9210Sstevel@tonic-gate 9221618Srie for (ndx = 1, info++; ndx < infonum; ndx++, info++) { 9231618Srie Sym *sym; 9241618Srie const char *needed = 0, *name; 9251618Srie 9261618Srie if ((info->si_flags == 0) && (info->si_boundto == 0)) 9270Sstevel@tonic-gate continue; 9280Sstevel@tonic-gate 9291618Srie sym = &syms[ndx]; 9301618Srie name = string(infocache, ndx, strsec, file, sym->st_name); 9310Sstevel@tonic-gate 9321618Srie if (info->si_boundto < SYMINFO_BT_LOWRESERVE) { 9331618Srie Dyn *dyn = &dyns[info->si_boundto]; 9341618Srie 9351618Srie needed = string(infocache, info->si_boundto, 9361618Srie strsec, file, dyn->d_un.d_val); 9370Sstevel@tonic-gate } 9381618Srie Elf_syminfo_entry(0, ndx, info, name, needed); 9390Sstevel@tonic-gate } 9400Sstevel@tonic-gate } 9410Sstevel@tonic-gate 9420Sstevel@tonic-gate /* 9430Sstevel@tonic-gate * Print version definition section entries. 9440Sstevel@tonic-gate */ 9450Sstevel@tonic-gate static void 9461618Srie version_def(Verdef *vdf, Word shnum, Cache *vcache, Cache *scache, 9470Sstevel@tonic-gate const char *file) 9480Sstevel@tonic-gate { 9491618Srie Word cnt; 9501618Srie char index[MAXNDXSIZE]; 9510Sstevel@tonic-gate 9521618Srie Elf_ver_def_title(0); 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate for (cnt = 1; cnt <= shnum; cnt++, 9551618Srie vdf = (Verdef *)((uintptr_t)vdf + vdf->vd_next)) { 9560Sstevel@tonic-gate const char *name, *dep; 9571618Srie Half vcnt = vdf->vd_cnt - 1; 9581618Srie Half ndx = vdf->vd_ndx; 959*4433Sab196087 Verdaux *vdap = (Verdaux *)((uintptr_t)vdf + vdf->vd_aux); 9600Sstevel@tonic-gate 9610Sstevel@tonic-gate /* 9620Sstevel@tonic-gate * Obtain the name and first dependency (if any). 9630Sstevel@tonic-gate */ 9640Sstevel@tonic-gate name = string(vcache, cnt, scache, file, vdap->vda_name); 9651618Srie vdap = (Verdaux *)((uintptr_t)vdap + vdap->vda_next); 9660Sstevel@tonic-gate if (vcnt) 9670Sstevel@tonic-gate dep = string(vcache, cnt, scache, file, vdap->vda_name); 9680Sstevel@tonic-gate else 9690Sstevel@tonic-gate dep = MSG_ORIG(MSG_STR_EMPTY); 9700Sstevel@tonic-gate 9710Sstevel@tonic-gate (void) snprintf(index, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX), 9720Sstevel@tonic-gate EC_XWORD(ndx)); 9731618Srie Elf_ver_line_1(0, index, name, dep, 9741618Srie conv_ver_flags(vdf->vd_flags)); 9750Sstevel@tonic-gate 9760Sstevel@tonic-gate /* 9770Sstevel@tonic-gate * Print any additional dependencies. 9780Sstevel@tonic-gate */ 9790Sstevel@tonic-gate if (vcnt) { 9801618Srie vdap = (Verdaux *)((uintptr_t)vdap + vdap->vda_next); 9810Sstevel@tonic-gate for (vcnt--; vcnt; vcnt--, 9821618Srie vdap = (Verdaux *)((uintptr_t)vdap + 9830Sstevel@tonic-gate vdap->vda_next)) { 9840Sstevel@tonic-gate dep = string(vcache, cnt, scache, file, 9850Sstevel@tonic-gate vdap->vda_name); 9861618Srie Elf_ver_line_2(0, MSG_ORIG(MSG_STR_EMPTY), dep); 9870Sstevel@tonic-gate } 9880Sstevel@tonic-gate } 9890Sstevel@tonic-gate } 9900Sstevel@tonic-gate } 9910Sstevel@tonic-gate 9920Sstevel@tonic-gate /* 9930Sstevel@tonic-gate * Print a version needed section entries. 9940Sstevel@tonic-gate */ 9950Sstevel@tonic-gate static void 9961618Srie version_need(Verneed *vnd, Word shnum, Cache *vcache, Cache *scache, 9970Sstevel@tonic-gate const char *file) 9980Sstevel@tonic-gate { 9991618Srie Word cnt; 10000Sstevel@tonic-gate 10011618Srie Elf_ver_need_title(0); 10020Sstevel@tonic-gate 10030Sstevel@tonic-gate for (cnt = 1; cnt <= shnum; cnt++, 10041618Srie vnd = (Verneed *)((uintptr_t)vnd + vnd->vn_next)) { 10050Sstevel@tonic-gate const char *name, *dep; 10061618Srie Half vcnt = vnd->vn_cnt; 1007*4433Sab196087 Vernaux *vnap = (Vernaux *)((uintptr_t)vnd + vnd->vn_aux); 10080Sstevel@tonic-gate 10090Sstevel@tonic-gate /* 10100Sstevel@tonic-gate * Obtain the name of the needed file and the version name 10110Sstevel@tonic-gate * within it that we're dependent on. Note that the count 10120Sstevel@tonic-gate * should be at least one, otherwise this is a pretty bogus 10130Sstevel@tonic-gate * entry. 10140Sstevel@tonic-gate */ 10150Sstevel@tonic-gate name = string(vcache, cnt, scache, file, vnd->vn_file); 10160Sstevel@tonic-gate if (vcnt) 10170Sstevel@tonic-gate dep = string(vcache, cnt, scache, file, vnap->vna_name); 10180Sstevel@tonic-gate else 10190Sstevel@tonic-gate dep = MSG_INTL(MSG_STR_NULL); 10200Sstevel@tonic-gate 10211618Srie Elf_ver_line_1(0, MSG_ORIG(MSG_STR_EMPTY), name, dep, 10221618Srie conv_ver_flags(vnap->vna_flags)); 10230Sstevel@tonic-gate 10240Sstevel@tonic-gate /* 10250Sstevel@tonic-gate * Print any additional version dependencies. 10260Sstevel@tonic-gate */ 10270Sstevel@tonic-gate if (vcnt) { 10281618Srie vnap = (Vernaux *)((uintptr_t)vnap + vnap->vna_next); 10290Sstevel@tonic-gate for (vcnt--; vcnt; vcnt--, 10301618Srie vnap = (Vernaux *)((uintptr_t)vnap + 10310Sstevel@tonic-gate vnap->vna_next)) { 10320Sstevel@tonic-gate dep = string(vcache, cnt, scache, file, 10330Sstevel@tonic-gate vnap->vna_name); 10341618Srie Elf_ver_line_3(0, MSG_ORIG(MSG_STR_EMPTY), dep, 10351618Srie conv_ver_flags(vnap->vna_flags)); 10360Sstevel@tonic-gate } 10370Sstevel@tonic-gate } 10380Sstevel@tonic-gate } 10390Sstevel@tonic-gate } 10400Sstevel@tonic-gate 10410Sstevel@tonic-gate /* 10423875Sab196087 * Display version section information if the flags require it. 10433875Sab196087 * Return version information needed by other output. 10443875Sab196087 * 10453875Sab196087 * entry: 10463875Sab196087 * cache - Cache of all section headers 10473875Sab196087 * shnum - # of sections in cache 10483875Sab196087 * file - Name of file 10493875Sab196087 * flags - Command line option flags 10503875Sab196087 * versym - VERSYM_STATE block to be filled in. 10510Sstevel@tonic-gate */ 10523875Sab196087 static void 10533875Sab196087 versions(Cache *cache, Word shnum, const char *file, uint_t flags, 10543875Sab196087 VERSYM_STATE *versym) 10550Sstevel@tonic-gate { 10560Sstevel@tonic-gate GElf_Word cnt; 10573875Sab196087 const char *gnu_prefix; 10583875Sab196087 size_t gnu_prefix_len; 10593875Sab196087 10603875Sab196087 bzero(versym, sizeof (*versym)); 10613875Sab196087 gnu_prefix = MSG_ORIG(MSG_GNU_VERNAMPREFIX); 10623875Sab196087 gnu_prefix_len = strlen(gnu_prefix); 10630Sstevel@tonic-gate 10640Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 10651618Srie void *ver; 10660Sstevel@tonic-gate uint_t num; 10671618Srie Cache *_cache = &cache[cnt]; 10681618Srie Shdr *shdr = _cache->c_shdr; 10691618Srie const char *secname = _cache->c_name; 10700Sstevel@tonic-gate 10710Sstevel@tonic-gate /* 10723875Sab196087 * If the section names starts with the .gnu.version prefix, 10733875Sab196087 * then this object was almost certainly produced by the 10743875Sab196087 * GNU ld and not the native Solaris ld. 10750Sstevel@tonic-gate */ 10763875Sab196087 if (strncmp(gnu_prefix, secname, gnu_prefix_len) == 0) 10773875Sab196087 versym->gnu = 1; 10783875Sab196087 10793875Sab196087 /* 10803875Sab196087 * If this is the version symbol table record its data 10813875Sab196087 * address for later symbol processing. 10823875Sab196087 */ 10833875Sab196087 if ((shdr->sh_type == SHT_SUNW_versym) && 10843875Sab196087 (_cache->c_data != NULL)) { 10853875Sab196087 versym->cache = _cache; 10863875Sab196087 versym->data = _cache->c_data->d_buf; 10870Sstevel@tonic-gate continue; 10880Sstevel@tonic-gate } 10890Sstevel@tonic-gate 10903875Sab196087 /* 10913875Sab196087 * If this is a version definition section, retain # of 10923875Sab196087 * version definitions for later symbol processing. 10933875Sab196087 */ 10943875Sab196087 if (shdr->sh_type == SHT_SUNW_verdef) 10953875Sab196087 versym->num_verdef = shdr->sh_info; 10963875Sab196087 10970Sstevel@tonic-gate if ((flags & FLG_VERSIONS) == 0) 10980Sstevel@tonic-gate continue; 10990Sstevel@tonic-gate 11000Sstevel@tonic-gate if ((shdr->sh_type != SHT_SUNW_verdef) && 11010Sstevel@tonic-gate (shdr->sh_type != SHT_SUNW_verneed)) 11020Sstevel@tonic-gate continue; 11030Sstevel@tonic-gate 11040Sstevel@tonic-gate /* 11050Sstevel@tonic-gate * Determine the version section data and number. 11060Sstevel@tonic-gate */ 11073466Srie if ((_cache->c_data == NULL) || 11083466Srie ((ver = (void *)_cache->c_data->d_buf) == NULL)) { 11090Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 11101618Srie file, secname); 11110Sstevel@tonic-gate continue; 11120Sstevel@tonic-gate } 11130Sstevel@tonic-gate if ((num = shdr->sh_info) == 0) { 11140Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO), 11151618Srie file, secname, EC_WORD(shdr->sh_info)); 11160Sstevel@tonic-gate continue; 11170Sstevel@tonic-gate } 11180Sstevel@tonic-gate 11190Sstevel@tonic-gate /* 11200Sstevel@tonic-gate * Get the data buffer for the associated string table. 11210Sstevel@tonic-gate */ 11220Sstevel@tonic-gate if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) { 11230Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK), 11241618Srie file, secname, EC_WORD(shdr->sh_link)); 11250Sstevel@tonic-gate continue; 11260Sstevel@tonic-gate } 11270Sstevel@tonic-gate 11281618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 11290Sstevel@tonic-gate if (shdr->sh_type == SHT_SUNW_verdef) { 11301618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_VERDEF), secname); 11311618Srie version_def((Verdef *)ver, num, _cache, 11320Sstevel@tonic-gate &cache[shdr->sh_link], file); 11330Sstevel@tonic-gate } else if (shdr->sh_type == SHT_SUNW_verneed) { 11341618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_VERNEED), secname); 11351618Srie version_need((Verneed *)ver, num, _cache, 11360Sstevel@tonic-gate &cache[shdr->sh_link], file); 11370Sstevel@tonic-gate } 11380Sstevel@tonic-gate } 11390Sstevel@tonic-gate } 11400Sstevel@tonic-gate 11410Sstevel@tonic-gate /* 11423492Sab196087 * Initialize a symbol table state structure 11433492Sab196087 * 11443492Sab196087 * entry: 11453492Sab196087 * state - State structure to be initialized 11463492Sab196087 * cache - Cache of all section headers 11473492Sab196087 * shnum - # of sections in cache 11483492Sab196087 * secndx - Index of symbol table section 11493492Sab196087 * ehdr - ELF header for file 11503875Sab196087 * versym - Information about versym section 11513492Sab196087 * file - Name of file 11523492Sab196087 * flags - Command line option flags 11531618Srie */ 11541618Srie static int 11553492Sab196087 init_symtbl_state(SYMTBL_STATE *state, Cache *cache, Word shnum, Word secndx, 11563875Sab196087 Ehdr *ehdr, VERSYM_STATE *versym, const char *file, uint_t flags) 11573492Sab196087 { 11583492Sab196087 Shdr *shdr; 11593492Sab196087 11603492Sab196087 state->file = file; 11613492Sab196087 state->ehdr = ehdr; 11623492Sab196087 state->cache = cache; 11633492Sab196087 state->shnum = shnum; 11643492Sab196087 state->seccache = &cache[secndx]; 11653492Sab196087 state->secndx = secndx; 11663492Sab196087 state->secname = state->seccache->c_name; 11673492Sab196087 state->flags = flags; 11683492Sab196087 state->shxndx.checked = 0; 11693492Sab196087 state->shxndx.data = NULL; 11703492Sab196087 state->shxndx.n = 0; 11713492Sab196087 11723492Sab196087 shdr = state->seccache->c_shdr; 11733492Sab196087 11743492Sab196087 /* 11753492Sab196087 * Check the symbol data and per-item size. 11763492Sab196087 */ 11773492Sab196087 if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) { 11783492Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 11793492Sab196087 file, state->secname); 11803492Sab196087 return (0); 11813492Sab196087 } 11823492Sab196087 if (state->seccache->c_data == NULL) 11833492Sab196087 return (0); 11843492Sab196087 11853492Sab196087 /* LINTED */ 11863492Sab196087 state->symn = (Word)(shdr->sh_size / shdr->sh_entsize); 11873492Sab196087 state->sym = (Sym *)state->seccache->c_data->d_buf; 11883492Sab196087 11893492Sab196087 /* 11903492Sab196087 * Check associated string table section. 11913492Sab196087 */ 11923492Sab196087 if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) { 11933492Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK), 11943492Sab196087 file, state->secname, EC_WORD(shdr->sh_link)); 11953492Sab196087 return (0); 11963492Sab196087 } 11973492Sab196087 11983492Sab196087 /* 11993492Sab196087 * Determine if there is a associated Versym section 12003492Sab196087 * with this Symbol Table. 12013492Sab196087 */ 12023875Sab196087 if (versym->cache && 12033875Sab196087 (versym->cache->c_shdr->sh_link == state->secndx)) 12043875Sab196087 state->versym = versym; 12053492Sab196087 else 12063492Sab196087 state->versym = NULL; 12073492Sab196087 12083492Sab196087 12093492Sab196087 return (1); 12103492Sab196087 } 12113492Sab196087 12123492Sab196087 /* 12133492Sab196087 * Determine the extended section index used for symbol tables entries. 12143492Sab196087 */ 12153492Sab196087 static void 12163492Sab196087 symbols_getxindex(SYMTBL_STATE * state) 12171618Srie { 12181618Srie uint_t symn; 12191618Srie Word symcnt; 12201618Srie 12213492Sab196087 state->shxndx.checked = 1; /* Note that we've been called */ 12223492Sab196087 for (symcnt = 1; symcnt < state->shnum; symcnt++) { 12233492Sab196087 Cache *_cache = &state->cache[symcnt]; 12241618Srie Shdr *shdr = _cache->c_shdr; 12251618Srie 12261618Srie if ((shdr->sh_type != SHT_SYMTAB_SHNDX) || 12273492Sab196087 (shdr->sh_link != state->secndx)) 12281618Srie continue; 12291618Srie 12301618Srie if ((shdr->sh_entsize) && 12311618Srie /* LINTED */ 12321618Srie ((symn = (uint_t)(shdr->sh_size / shdr->sh_entsize)) == 0)) 12331618Srie continue; 12341618Srie 12353466Srie if (_cache->c_data == NULL) 12363466Srie continue; 12373466Srie 12383492Sab196087 state->shxndx.data = _cache->c_data->d_buf; 12393492Sab196087 state->shxndx.n = symn; 12403492Sab196087 return; 12411618Srie } 12421618Srie } 12431618Srie 12441618Srie /* 12453492Sab196087 * Produce a line of output for the given symbol 12463492Sab196087 * 12473492Sab196087 * entry: 12483875Sab196087 * state - Symbol table state 12493492Sab196087 * symndx - Index of symbol within the table 12503492Sab196087 * symndx_disp - Index to display. This may not be the same 12513492Sab196087 * as symndx if the display is relative to the logical 12523492Sab196087 * combination of the SUNW_ldynsym/dynsym tables. 12533492Sab196087 * sym - Symbol to display 12540Sstevel@tonic-gate */ 12553492Sab196087 static void 12563492Sab196087 output_symbol(SYMTBL_STATE *state, Word symndx, Word disp_symndx, Sym *sym) 12570Sstevel@tonic-gate { 12583118Sab196087 /* 12593118Sab196087 * Symbol types for which we check that the specified 12603118Sab196087 * address/size land inside the target section. 12613118Sab196087 */ 12623492Sab196087 static const int addr_symtype[STT_NUM] = { 12633118Sab196087 0, /* STT_NOTYPE */ 12643118Sab196087 1, /* STT_OBJECT */ 12653118Sab196087 1, /* STT_FUNC */ 12663118Sab196087 0, /* STT_SECTION */ 12673118Sab196087 0, /* STT_FILE */ 12683118Sab196087 1, /* STT_COMMON */ 12693118Sab196087 0, /* STT_TLS */ 12703118Sab196087 }; 12713118Sab196087 #if STT_NUM != (STT_TLS + 1) 12723492Sab196087 #error "STT_NUM has grown. Update addr_symtype[]" 12733118Sab196087 #endif 12743118Sab196087 12753492Sab196087 char index[MAXNDXSIZE], *sec; 12763492Sab196087 const char *symname; 12773875Sab196087 Versym verndx; 12783492Sab196087 uchar_t type; 12793492Sab196087 Shdr *tshdr; 12803492Sab196087 Word shndx; 12813492Sab196087 12823492Sab196087 /* Ensure symbol index is in range */ 12833492Sab196087 if (symndx >= state->symn) { 12843492Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSORTNDX), 12853492Sab196087 state->file, state->secname, EC_WORD(symndx)); 12863492Sab196087 return; 12873492Sab196087 } 12883492Sab196087 12893492Sab196087 /* 12903492Sab196087 * If we are using extended symbol indexes, find the 12913492Sab196087 * corresponding SHN_SYMTAB_SHNDX table. 12923492Sab196087 */ 12933492Sab196087 if ((sym->st_shndx == SHN_XINDEX) && (state->shxndx.checked == 0)) 12943492Sab196087 symbols_getxindex(state); 12953492Sab196087 12963492Sab196087 /* LINTED */ 12973492Sab196087 symname = string(state->seccache, symndx, 12983492Sab196087 &state->cache[state->seccache->c_shdr->sh_link], state->file, 12993492Sab196087 sym->st_name); 13003492Sab196087 13013492Sab196087 tshdr = 0; 13023492Sab196087 sec = NULL; 13033492Sab196087 13043492Sab196087 if ((state->ehdr->e_type == ET_CORE)) 13053492Sab196087 sec = (char *)MSG_INTL(MSG_STR_UNKNOWN); 13063492Sab196087 else if ((sym->st_shndx < SHN_LORESERVE) && 13073492Sab196087 (sym->st_shndx < state->shnum)) { 13083492Sab196087 shndx = sym->st_shndx; 13093492Sab196087 tshdr = state->cache[shndx].c_shdr; 13103492Sab196087 sec = state->cache[shndx].c_name; 13113492Sab196087 } else if (sym->st_shndx == SHN_XINDEX) { 13123492Sab196087 if (state->shxndx.data) { 13133492Sab196087 Word _shxndx; 13143492Sab196087 13153492Sab196087 if (symndx > state->shxndx.n) { 1316*4433Sab196087 (void) fprintf(stderr, 1317*4433Sab196087 MSG_INTL(MSG_ERR_BADSYMXINDEX1), 1318*4433Sab196087 state->file, state->secname, 1319*4433Sab196087 EC_WORD(symndx)); 13203492Sab196087 } else if ((_shxndx = 13213492Sab196087 state->shxndx.data[symndx]) > state->shnum) { 1322*4433Sab196087 (void) fprintf(stderr, 1323*4433Sab196087 MSG_INTL(MSG_ERR_BADSYMXINDEX2), 1324*4433Sab196087 state->file, state->secname, 1325*4433Sab196087 EC_WORD(symndx), EC_WORD(_shxndx)); 13263492Sab196087 } else { 1327*4433Sab196087 shndx = _shxndx; 1328*4433Sab196087 tshdr = state->cache[shndx].c_shdr; 1329*4433Sab196087 sec = state->cache[shndx].c_name; 13303492Sab196087 } 13313492Sab196087 } else { 13323492Sab196087 (void) fprintf(stderr, 13333492Sab196087 MSG_INTL(MSG_ERR_BADSYMXINDEX3), 13343492Sab196087 state->file, state->secname, EC_WORD(symndx)); 13353492Sab196087 } 13363492Sab196087 } else if ((sym->st_shndx < SHN_LORESERVE) && 13373492Sab196087 (sym->st_shndx >= state->shnum)) { 13383492Sab196087 (void) fprintf(stderr, 13393492Sab196087 MSG_INTL(MSG_ERR_BADSYM5), state->file, 13403492Sab196087 state->secname, demangle(symname, state->flags), 13413492Sab196087 sym->st_shndx); 13423492Sab196087 } 13430Sstevel@tonic-gate 13443492Sab196087 /* 13453492Sab196087 * If versioning is available display the 13463875Sab196087 * version index. If not, then use 0. 13473492Sab196087 */ 13483875Sab196087 if (state->versym) { 13493875Sab196087 verndx = state->versym->data[symndx]; 13503875Sab196087 13513875Sab196087 /* 13523875Sab196087 * Check to see if this is a defined symbol with a 13533875Sab196087 * version index that is outside the valid range for 13543875Sab196087 * the file. If so, then there are two possiblities: 13553875Sab196087 * 13563875Sab196087 * - Files produced by the GNU ld use the top (16th) bit 13573875Sab196087 * as a "hidden symbol" marker. If we have 13583875Sab196087 * detected that this object comes from GNU ld, 13593875Sab196087 * then check to see if this is the case and that 13603875Sab196087 * the resulting masked version is in range. If so, 13613875Sab196087 * issue a warning describing it. 13623875Sab196087 * - If this is not a GNU "hidden bit" issue, then 13633875Sab196087 * issue a generic "out of range" error. 13643875Sab196087 */ 1365*4433Sab196087 if (VERNDX_INVALID_DIAG(sym->st_shndx, 1366*4433Sab196087 state->versym->num_verdef, state->versym->data, symndx)) { 13673875Sab196087 if (state->versym->gnu && (verndx & 0x8000) && 13683875Sab196087 ((verndx & ~0x8000) <= 13693875Sab196087 state->versym->num_verdef)) { 13703875Sab196087 (void) fprintf(stderr, 13713875Sab196087 MSG_INTL(MSG_WARN_GNUVER), state->file, 13723875Sab196087 state->secname, EC_WORD(symndx), 13733875Sab196087 EC_HALF(verndx & ~0x8000)); 13743875Sab196087 } else { /* Generic version range error */ 13753875Sab196087 (void) fprintf(stderr, 13763875Sab196087 MSG_INTL(MSG_ERR_BADVER), state->file, 13773875Sab196087 state->secname, EC_WORD(symndx), 13783875Sab196087 EC_HALF(verndx), state->versym->num_verdef); 13793875Sab196087 } 13803875Sab196087 } 13813875Sab196087 } else { 13823492Sab196087 verndx = 0; 13833875Sab196087 } 13843492Sab196087 13853492Sab196087 /* 13863492Sab196087 * Error checking for TLS. 13873492Sab196087 */ 13883492Sab196087 type = ELF_ST_TYPE(sym->st_info); 13893492Sab196087 if (type == STT_TLS) { 13903492Sab196087 if (tshdr && 13913492Sab196087 (sym->st_shndx != SHN_UNDEF) && 13923492Sab196087 ((tshdr->sh_flags & SHF_TLS) == 0)) { 13933492Sab196087 (void) fprintf(stderr, 13943492Sab196087 MSG_INTL(MSG_ERR_BADSYM3), state->file, 13953492Sab196087 state->secname, demangle(symname, state->flags)); 13963492Sab196087 } 13973492Sab196087 } else if ((type != STT_SECTION) && sym->st_size && 13983492Sab196087 tshdr && (tshdr->sh_flags & SHF_TLS)) { 13993492Sab196087 (void) fprintf(stderr, 14003492Sab196087 MSG_INTL(MSG_ERR_BADSYM4), state->file, 14013492Sab196087 state->secname, demangle(symname, state->flags)); 14023492Sab196087 } 14033492Sab196087 14043492Sab196087 /* 14053492Sab196087 * If a symbol with non-zero size has a type that 14063492Sab196087 * specifies an address, then make sure the location 14073492Sab196087 * it references is actually contained within the 14083492Sab196087 * section. UNDEF symbols don't count in this case, 14093492Sab196087 * so we ignore them. 14103492Sab196087 * 14113492Sab196087 * The meaning of the st_value field in a symbol 14123492Sab196087 * depends on the type of object. For a relocatable 14133492Sab196087 * object, it is the offset within the section. 14143492Sab196087 * For sharable objects, it is the offset relative to 14153492Sab196087 * the base of the object, and for other types, it is 14163492Sab196087 * the virtual address. To get an offset within the 14173492Sab196087 * section for non-ET_REL files, we subtract the 14183492Sab196087 * base address of the section. 14193492Sab196087 */ 14203492Sab196087 if (addr_symtype[type] && (sym->st_size > 0) && 14213492Sab196087 (sym->st_shndx != SHN_UNDEF) && ((sym->st_shndx < SHN_LORESERVE) || 14223492Sab196087 (sym->st_shndx == SHN_XINDEX)) && (tshdr != NULL)) { 14233492Sab196087 Word v = sym->st_value; 14243492Sab196087 if (state->ehdr->e_type != ET_REL) 14253492Sab196087 v -= tshdr->sh_addr; 14263492Sab196087 if (((v + sym->st_size) > tshdr->sh_size)) { 14273492Sab196087 (void) fprintf(stderr, 14283492Sab196087 MSG_INTL(MSG_ERR_BADSYM6), state->file, 14293492Sab196087 state->secname, demangle(symname, state->flags), 14303492Sab196087 EC_WORD(shndx), EC_XWORD(tshdr->sh_size), 14313492Sab196087 EC_XWORD(sym->st_value), EC_XWORD(sym->st_size)); 14323492Sab196087 } 14333492Sab196087 } 14343492Sab196087 14353492Sab196087 (void) snprintf(index, MAXNDXSIZE, 14363492Sab196087 MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(disp_symndx)); 14373492Sab196087 Elf_syms_table_entry(0, ELF_DBG_ELFDUMP, index, 14383492Sab196087 state->ehdr->e_machine, sym, verndx, sec, symname); 14393492Sab196087 } 14403492Sab196087 14413492Sab196087 /* 14423492Sab196087 * Search for and process any symbol tables. 14433492Sab196087 */ 14443492Sab196087 void 14454168Sab196087 symbols(Cache *cache, Word shnum, Ehdr *ehdr, VERSYM_STATE *versym, 14464168Sab196087 const char *file, uint_t flags) 14473492Sab196087 { 14483492Sab196087 SYMTBL_STATE state; 14493492Sab196087 Cache *_cache; 14503492Sab196087 Word secndx; 14513492Sab196087 14523492Sab196087 for (secndx = 1; secndx < shnum; secndx++) { 14533492Sab196087 Word symcnt; 14543492Sab196087 Shdr *shdr; 14553492Sab196087 14563492Sab196087 _cache = &cache[secndx]; 14573492Sab196087 shdr = _cache->c_shdr; 14580Sstevel@tonic-gate 14590Sstevel@tonic-gate if ((shdr->sh_type != SHT_SYMTAB) && 14602766Sab196087 (shdr->sh_type != SHT_DYNSYM) && 14612766Sab196087 (shdr->sh_type != SHT_SUNW_LDYNSYM)) 14620Sstevel@tonic-gate continue; 14634168Sab196087 if (!match(0, _cache->c_name, secndx)) 14643466Srie continue; 14653466Srie 14663492Sab196087 if (!init_symtbl_state(&state, cache, shnum, secndx, ehdr, 14673875Sab196087 versym, file, flags)) 14680Sstevel@tonic-gate continue; 14690Sstevel@tonic-gate /* 14700Sstevel@tonic-gate * Loop through the symbol tables entries. 14710Sstevel@tonic-gate */ 14721618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 14733492Sab196087 dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMTAB), state.secname); 14741618Srie Elf_syms_table_title(0, ELF_DBG_ELFDUMP); 14750Sstevel@tonic-gate 14763492Sab196087 for (symcnt = 0; symcnt < state.symn; symcnt++) 14773492Sab196087 output_symbol(&state, symcnt, symcnt, 14783492Sab196087 state.sym + symcnt); 14793492Sab196087 } 14803492Sab196087 } 14810Sstevel@tonic-gate 14823492Sab196087 /* 14833492Sab196087 * Search for and process any SHT_SUNW_symsort or SHT_SUNW_tlssort sections. 14843492Sab196087 * These sections are always associated with the .SUNW_ldynsym./.dynsym pair. 14853492Sab196087 */ 14863492Sab196087 static void 14874168Sab196087 sunw_sort(Cache *cache, Word shnum, Ehdr *ehdr, VERSYM_STATE *versym, 14884168Sab196087 const char *file, uint_t flags) 14893492Sab196087 { 14903492Sab196087 SYMTBL_STATE ldynsym_state, dynsym_state; 14913492Sab196087 Cache *sortcache, *symcache; 14923492Sab196087 Shdr *sortshdr, *symshdr; 14933492Sab196087 Word sortsecndx, symsecndx; 14943492Sab196087 Word ldynsym_cnt; 14953492Sab196087 Word *ndx; 14963492Sab196087 Word ndxn; 14973492Sab196087 int output_cnt = 0; 14980Sstevel@tonic-gate 14993492Sab196087 for (sortsecndx = 1; sortsecndx < shnum; sortsecndx++) { 15000Sstevel@tonic-gate 15013492Sab196087 sortcache = &cache[sortsecndx]; 15023492Sab196087 sortshdr = sortcache->c_shdr; 15030Sstevel@tonic-gate 15043492Sab196087 if ((sortshdr->sh_type != SHT_SUNW_symsort) && 15053492Sab196087 (sortshdr->sh_type != SHT_SUNW_tlssort)) 15063492Sab196087 continue; 15074168Sab196087 if (!match(0, sortcache->c_name, sortsecndx)) 15083492Sab196087 continue; 15090Sstevel@tonic-gate 15103492Sab196087 /* 15113492Sab196087 * If the section references a SUNW_ldynsym, then we 15123492Sab196087 * expect to see the associated .dynsym immediately 15133492Sab196087 * following. If it references a .dynsym, there is no 15143492Sab196087 * SUNW_ldynsym. If it is any other type, then we don't 15153492Sab196087 * know what to do with it. 15163492Sab196087 */ 15173492Sab196087 if ((sortshdr->sh_link == 0) || (sortshdr->sh_link >= shnum)) { 15183492Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK), 15193492Sab196087 file, sortcache->c_name, 15203492Sab196087 EC_WORD(sortshdr->sh_link)); 15213492Sab196087 continue; 15223492Sab196087 } 15233492Sab196087 symcache = &cache[sortshdr->sh_link]; 15243492Sab196087 symshdr = symcache->c_shdr; 15253492Sab196087 symsecndx = sortshdr->sh_link; 15263492Sab196087 ldynsym_cnt = 0; 15273492Sab196087 switch (symshdr->sh_type) { 15283492Sab196087 case SHT_SUNW_LDYNSYM: 15293492Sab196087 if (!init_symtbl_state(&ldynsym_state, cache, shnum, 15303875Sab196087 symsecndx, ehdr, versym, file, flags)) 15313492Sab196087 continue; 15323492Sab196087 ldynsym_cnt = ldynsym_state.symn; 15330Sstevel@tonic-gate /* 15343492Sab196087 * We know that the dynsym follows immediately 15353492Sab196087 * after the SUNW_ldynsym, and so, should be at 15363492Sab196087 * (sortshdr->sh_link + 1). However, elfdump is a 15373492Sab196087 * diagnostic tool, so we do the full paranoid 15383492Sab196087 * search instead. 15390Sstevel@tonic-gate */ 15403492Sab196087 for (symsecndx = 1; symsecndx < shnum; symsecndx++) { 15413492Sab196087 symcache = &cache[symsecndx]; 15423492Sab196087 symshdr = symcache->c_shdr; 15433492Sab196087 if (symshdr->sh_type == SHT_DYNSYM) 15443492Sab196087 break; 15453492Sab196087 } 15463492Sab196087 if (symsecndx >= shnum) { /* Dynsym not found! */ 15470Sstevel@tonic-gate (void) fprintf(stderr, 15483492Sab196087 MSG_INTL(MSG_ERR_NODYNSYM), 15493492Sab196087 file, sortcache->c_name); 15503492Sab196087 continue; 15510Sstevel@tonic-gate } 15523492Sab196087 /* Fallthrough to process associated dynsym */ 15533492Sab196087 /*FALLTHROUGH*/ 15543492Sab196087 case SHT_DYNSYM: 15553492Sab196087 if (!init_symtbl_state(&dynsym_state, cache, shnum, 15563875Sab196087 symsecndx, ehdr, versym, file, flags)) 15573492Sab196087 continue; 15583492Sab196087 break; 15593492Sab196087 default: 15603492Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADNDXSEC), 15613492Sab196087 file, sortcache->c_name, conv_sec_type( 15623492Sab196087 ehdr->e_machine, symshdr->sh_type, 0)); 15633492Sab196087 continue; 15643492Sab196087 } 15650Sstevel@tonic-gate 15663492Sab196087 /* 15673492Sab196087 * Output header 15683492Sab196087 */ 15693492Sab196087 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 15703492Sab196087 if (ldynsym_cnt > 0) { 15713492Sab196087 dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMSORT2), 15723492Sab196087 sortcache->c_name, ldynsym_state.secname, 15733492Sab196087 dynsym_state.secname); 15740Sstevel@tonic-gate /* 15753492Sab196087 * The data for .SUNW_ldynsym and dynsym sections 15763492Sab196087 * is supposed to be adjacent with SUNW_ldynsym coming 15773492Sab196087 * first. Check, and issue a warning if it isn't so. 15780Sstevel@tonic-gate */ 15793492Sab196087 if ((ldynsym_state.sym + ldynsym_state.symn) 15803492Sab196087 != dynsym_state.sym) 15813492Sab196087 (void) fprintf(stderr, 15823492Sab196087 MSG_INTL(MSG_ERR_LDYNNOTADJ), file, 15833492Sab196087 ldynsym_state.secname, 15843492Sab196087 dynsym_state.secname); 15853492Sab196087 } else { 15863492Sab196087 dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMSORT1), 15873492Sab196087 sortcache->c_name, dynsym_state.secname); 15883492Sab196087 } 15893492Sab196087 Elf_syms_table_title(0, ELF_DBG_ELFDUMP); 15903492Sab196087 15913492Sab196087 /* If not first one, insert a line of whitespace */ 15923492Sab196087 if (output_cnt++ > 0) 15933492Sab196087 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 15943118Sab196087 15953492Sab196087 /* 15963492Sab196087 * SUNW_dynsymsort and SUNW_dyntlssort are arrays of 15973492Sab196087 * symbol indices. Iterate over the array entries, 15983492Sab196087 * dispaying the referenced symbols. 15993492Sab196087 */ 16003492Sab196087 ndxn = sortshdr->sh_size / sortshdr->sh_entsize; 16013492Sab196087 ndx = (Word *)sortcache->c_data->d_buf; 16023492Sab196087 for (; ndxn-- > 0; ndx++) { 16033492Sab196087 if (*ndx >= ldynsym_cnt) { 16043492Sab196087 Word sec_ndx = *ndx - ldynsym_cnt; 16053492Sab196087 16063492Sab196087 output_symbol(&dynsym_state, sec_ndx, 16073492Sab196087 *ndx, dynsym_state.sym + sec_ndx); 16083492Sab196087 } else { 16093492Sab196087 output_symbol(&ldynsym_state, *ndx, 16103492Sab196087 *ndx, ldynsym_state.sym + *ndx); 16110Sstevel@tonic-gate } 16120Sstevel@tonic-gate } 16130Sstevel@tonic-gate } 16140Sstevel@tonic-gate } 16150Sstevel@tonic-gate 16160Sstevel@tonic-gate /* 16170Sstevel@tonic-gate * Search for and process any relocation sections. 16180Sstevel@tonic-gate */ 16190Sstevel@tonic-gate static void 16204168Sab196087 reloc(Cache *cache, Word shnum, Ehdr *ehdr, const char *file, 16211618Srie uint_t flags) 16220Sstevel@tonic-gate { 16231618Srie Word cnt; 16240Sstevel@tonic-gate 16250Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 16261618Srie Word type, symnum; 16271618Srie Xword relndx, relnum, relsize; 16281618Srie void *rels; 16291618Srie Sym *syms; 16301618Srie Cache *symsec, *strsec; 16310Sstevel@tonic-gate Cache *_cache = &cache[cnt]; 16321618Srie Shdr *shdr = _cache->c_shdr; 16331618Srie char *relname = _cache->c_name; 16340Sstevel@tonic-gate 16350Sstevel@tonic-gate if (((type = shdr->sh_type) != SHT_RELA) && 16360Sstevel@tonic-gate (type != SHT_REL)) 16370Sstevel@tonic-gate continue; 16384168Sab196087 if (!match(0, relname, cnt)) 16390Sstevel@tonic-gate continue; 16400Sstevel@tonic-gate 16410Sstevel@tonic-gate /* 16421618Srie * Decide entry size. 16430Sstevel@tonic-gate */ 16441618Srie if (((relsize = shdr->sh_entsize) == 0) || 16451618Srie (relsize > shdr->sh_size)) { 16460Sstevel@tonic-gate if (type == SHT_RELA) 16471618Srie relsize = sizeof (Rela); 16480Sstevel@tonic-gate else 16491618Srie relsize = sizeof (Rel); 16500Sstevel@tonic-gate } 16510Sstevel@tonic-gate 16520Sstevel@tonic-gate /* 16530Sstevel@tonic-gate * Determine the number of relocations available. 16540Sstevel@tonic-gate */ 16550Sstevel@tonic-gate if (shdr->sh_size == 0) { 16560Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 16571618Srie file, relname); 16580Sstevel@tonic-gate continue; 16590Sstevel@tonic-gate } 16603466Srie if (_cache->c_data == NULL) 16613466Srie continue; 16623466Srie 16631618Srie rels = _cache->c_data->d_buf; 16641618Srie relnum = shdr->sh_size / relsize; 16650Sstevel@tonic-gate 16660Sstevel@tonic-gate /* 16671618Srie * Get the data buffer for the associated symbol table and 16681618Srie * string table. 16690Sstevel@tonic-gate */ 16701618Srie if (stringtbl(cache, 1, cnt, shnum, file, 16711618Srie &symnum, &symsec, &strsec) == 0) 16720Sstevel@tonic-gate continue; 16731618Srie 16741618Srie syms = symsec->c_data->d_buf; 16750Sstevel@tonic-gate 16760Sstevel@tonic-gate /* 16770Sstevel@tonic-gate * Loop through the relocation entries. 16780Sstevel@tonic-gate */ 16791618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 16801618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_RELOC), _cache->c_name); 16811618Srie Elf_reloc_title(0, ELF_DBG_ELFDUMP, type); 16820Sstevel@tonic-gate 16831618Srie for (relndx = 0; relndx < relnum; relndx++, 16841618Srie rels = (void *)((char *)rels + relsize)) { 16850Sstevel@tonic-gate char section[BUFSIZ]; 16861618Srie const char *symname; 16871618Srie Word symndx, reltype; 16881618Srie Rela *rela; 16891618Srie Rel *rel; 16900Sstevel@tonic-gate 16910Sstevel@tonic-gate /* 16921618Srie * Unravel the relocation and determine the symbol with 16931618Srie * which this relocation is associated. 16940Sstevel@tonic-gate */ 16950Sstevel@tonic-gate if (type == SHT_RELA) { 16961618Srie rela = (Rela *)rels; 16971618Srie symndx = ELF_R_SYM(rela->r_info); 16981618Srie reltype = ELF_R_TYPE(rela->r_info); 16990Sstevel@tonic-gate } else { 17001618Srie rel = (Rel *)rels; 17011618Srie symndx = ELF_R_SYM(rel->r_info); 17021618Srie reltype = ELF_R_TYPE(rel->r_info); 17030Sstevel@tonic-gate } 17041618Srie 17051618Srie symname = relsymname(cache, _cache, strsec, symndx, 17061618Srie symnum, relndx, syms, section, BUFSIZ, file, 17071618Srie flags); 17081618Srie 17091618Srie /* 17101618Srie * A zero symbol index is only valid for a few 17111618Srie * relocations. 17121618Srie */ 17131618Srie if (symndx == 0) { 17141618Srie Half mach = ehdr->e_machine; 17151618Srie int badrel = 0; 17160Sstevel@tonic-gate 17171618Srie if ((mach == EM_SPARC) || 17181618Srie (mach == EM_SPARC32PLUS) || 17191618Srie (mach == EM_SPARCV9)) { 17201618Srie if ((reltype != R_SPARC_NONE) && 17211618Srie (reltype != R_SPARC_REGISTER) && 17221618Srie (reltype != R_SPARC_RELATIVE)) 17231618Srie badrel++; 17241618Srie } else if (mach == EM_386) { 17251618Srie if ((reltype != R_386_NONE) && 17261618Srie (reltype != R_386_RELATIVE)) 17271618Srie badrel++; 17281618Srie } else if (mach == EM_AMD64) { 17291618Srie if ((reltype != R_AMD64_NONE) && 17301618Srie (reltype != R_AMD64_RELATIVE)) 17311618Srie badrel++; 17321618Srie } 17331618Srie 17341618Srie if (badrel) { 17351618Srie (void) fprintf(stderr, 17361618Srie MSG_INTL(MSG_ERR_BADREL1), file, 17371976Sab196087 conv_reloc_type(mach, reltype, 0)); 17380Sstevel@tonic-gate } 17390Sstevel@tonic-gate } 17400Sstevel@tonic-gate 17411618Srie Elf_reloc_entry_1(0, ELF_DBG_ELFDUMP, 17421618Srie MSG_ORIG(MSG_STR_EMPTY), ehdr->e_machine, type, 17431618Srie rels, relname, symname, 0); 17440Sstevel@tonic-gate } 17450Sstevel@tonic-gate } 17460Sstevel@tonic-gate } 17470Sstevel@tonic-gate 17480Sstevel@tonic-gate /* 17490Sstevel@tonic-gate * Search for and process a .dynamic section. 17500Sstevel@tonic-gate */ 17510Sstevel@tonic-gate static void 17521618Srie dynamic(Cache *cache, Word shnum, Ehdr *ehdr, const char *file) 17530Sstevel@tonic-gate { 17541618Srie Word cnt; 17550Sstevel@tonic-gate 17560Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 17571618Srie Dyn *dyn; 17581618Srie ulong_t numdyn; 17593850Sab196087 int ndx, end_ndx; 17601618Srie Cache *_cache = &cache[cnt], *strsec; 17611618Srie Shdr *shdr = _cache->c_shdr; 17620Sstevel@tonic-gate 17630Sstevel@tonic-gate if (shdr->sh_type != SHT_DYNAMIC) 17640Sstevel@tonic-gate continue; 17650Sstevel@tonic-gate 17660Sstevel@tonic-gate /* 17671618Srie * Verify the associated string table section. 17680Sstevel@tonic-gate */ 17691618Srie if (stringtbl(cache, 0, cnt, shnum, file, 0, 0, &strsec) == 0) 17700Sstevel@tonic-gate continue; 17711618Srie 17723466Srie if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) { 17733466Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 17743466Srie file, _cache->c_name); 17753466Srie continue; 17763466Srie } 17773466Srie if (_cache->c_data == NULL) 17783466Srie continue; 17793466Srie 17800Sstevel@tonic-gate numdyn = shdr->sh_size / shdr->sh_entsize; 17811618Srie dyn = (Dyn *)_cache->c_data->d_buf; 17820Sstevel@tonic-gate 17831618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 17841618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_DYNAMIC), _cache->c_name); 17850Sstevel@tonic-gate 17861618Srie Elf_dyn_title(0); 17870Sstevel@tonic-gate 17881618Srie for (ndx = 0; ndx < numdyn; dyn++, ndx++) { 17891618Srie const char *name; 17900Sstevel@tonic-gate 17910Sstevel@tonic-gate /* 17920Sstevel@tonic-gate * Print the information numerically, and if possible 17930Sstevel@tonic-gate * as a string. 17940Sstevel@tonic-gate */ 17953850Sab196087 switch (dyn->d_tag) { 17963850Sab196087 case DT_NULL: 17973850Sab196087 /* 17983850Sab196087 * Special case: DT_NULLs can come in groups 17993850Sab196087 * that we prefer to reduce to a single line. 18003850Sab196087 */ 18013850Sab196087 end_ndx = ndx; 18023850Sab196087 while ((end_ndx < (numdyn - 1)) && 1803*4433Sab196087 ((dyn + 1)->d_tag == DT_NULL)) { 18043850Sab196087 dyn++; 18053850Sab196087 end_ndx++; 18063850Sab196087 } 18073850Sab196087 Elf_dyn_null_entry(0, dyn, ndx, end_ndx); 18083850Sab196087 ndx = end_ndx; 18093850Sab196087 continue; 18103850Sab196087 18113850Sab196087 /* 18123850Sab196087 * Print the information numerically, and if possible 18133850Sab196087 * as a string. 18143850Sab196087 */ 18153850Sab196087 case DT_NEEDED: 18163850Sab196087 case DT_SONAME: 18173850Sab196087 case DT_FILTER: 18183850Sab196087 case DT_AUXILIARY: 18193850Sab196087 case DT_CONFIG: 18203850Sab196087 case DT_RPATH: 18213850Sab196087 case DT_RUNPATH: 18223850Sab196087 case DT_USED: 18233850Sab196087 case DT_DEPAUDIT: 18243850Sab196087 case DT_AUDIT: 18253850Sab196087 case DT_SUNW_AUXILIARY: 18263850Sab196087 case DT_SUNW_FILTER: 18271618Srie name = string(_cache, ndx, strsec, 18281618Srie file, dyn->d_un.d_ptr); 18293850Sab196087 break; 18303850Sab196087 18313850Sab196087 case DT_FLAGS: 18322352Sab196087 name = conv_dyn_flag(dyn->d_un.d_val, 0); 18333850Sab196087 break; 18343850Sab196087 case DT_FLAGS_1: 18351618Srie name = conv_dyn_flag1(dyn->d_un.d_val); 18363850Sab196087 break; 18373850Sab196087 case DT_POSFLAG_1: 18382352Sab196087 name = conv_dyn_posflag1(dyn->d_un.d_val, 0); 18393850Sab196087 break; 18403850Sab196087 case DT_FEATURE_1: 18412352Sab196087 name = conv_dyn_feature1(dyn->d_un.d_val, 0); 18423850Sab196087 break; 18433850Sab196087 case DT_DEPRECATED_SPARC_REGISTER: 18441618Srie name = MSG_INTL(MSG_STR_DEPRECATED); 18453850Sab196087 break; 18463850Sab196087 default: 18471618Srie name = MSG_ORIG(MSG_STR_EMPTY); 18483850Sab196087 break; 18493850Sab196087 } 18500Sstevel@tonic-gate 18511618Srie Elf_dyn_entry(0, dyn, ndx, name, ehdr->e_machine); 18520Sstevel@tonic-gate } 18530Sstevel@tonic-gate } 18540Sstevel@tonic-gate } 18550Sstevel@tonic-gate 18560Sstevel@tonic-gate /* 18570Sstevel@tonic-gate * Search for and process a MOVE section. 18580Sstevel@tonic-gate */ 18590Sstevel@tonic-gate static void 18604168Sab196087 move(Cache *cache, Word shnum, const char *file, uint_t flags) 18610Sstevel@tonic-gate { 18621618Srie Word cnt; 18631618Srie const char *fmt = 0; 18640Sstevel@tonic-gate 18650Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 18661618Srie Word movenum, symnum, ndx; 18671618Srie Sym *syms; 18681618Srie Cache *_cache = &cache[cnt]; 18691618Srie Shdr *shdr = _cache->c_shdr; 18701618Srie Cache *symsec, *strsec; 18711618Srie Move *move; 18720Sstevel@tonic-gate 18730Sstevel@tonic-gate if (shdr->sh_type != SHT_SUNW_move) 18740Sstevel@tonic-gate continue; 18754168Sab196087 if (!match(0, _cache->c_name, cnt)) 18760Sstevel@tonic-gate continue; 18770Sstevel@tonic-gate 18780Sstevel@tonic-gate /* 18790Sstevel@tonic-gate * Determine the move data and number. 18800Sstevel@tonic-gate */ 18810Sstevel@tonic-gate if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) { 18820Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 18830Sstevel@tonic-gate file, _cache->c_name); 18840Sstevel@tonic-gate continue; 18850Sstevel@tonic-gate } 18863466Srie if (_cache->c_data == NULL) 18873466Srie continue; 18883466Srie 18891618Srie move = (Move *)_cache->c_data->d_buf; 18901618Srie movenum = shdr->sh_size / shdr->sh_entsize; 18910Sstevel@tonic-gate 18920Sstevel@tonic-gate /* 18931618Srie * Get the data buffer for the associated symbol table and 18941618Srie * string table. 18950Sstevel@tonic-gate */ 18961618Srie if (stringtbl(cache, 1, cnt, shnum, file, 18971618Srie &symnum, &symsec, &strsec) == 0) 18981618Srie return; 18991618Srie 19001618Srie syms = (Sym *)symsec->c_data->d_buf; 19010Sstevel@tonic-gate 19021618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 19031618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_MOVE), _cache->c_name); 19041618Srie dbg_print(0, MSG_INTL(MSG_MOVE_TITLE)); 19050Sstevel@tonic-gate 19061618Srie if (fmt == 0) 19071618Srie fmt = MSG_INTL(MSG_MOVE_ENTRY); 19080Sstevel@tonic-gate 19091618Srie for (ndx = 0; ndx < movenum; move++, ndx++) { 19101618Srie const char *symname; 19111618Srie char index[MAXNDXSIZE], section[BUFSIZ]; 19121618Srie Word symndx, shndx; 19131618Srie Sym *sym; 19140Sstevel@tonic-gate 19150Sstevel@tonic-gate /* 19160Sstevel@tonic-gate * Check for null entries 19170Sstevel@tonic-gate */ 19181618Srie if ((move->m_info == 0) && (move->m_value == 0) && 19191618Srie (move->m_poffset == 0) && (move->m_repeat == 0) && 19201618Srie (move->m_stride == 0)) { 19211618Srie dbg_print(0, fmt, MSG_ORIG(MSG_STR_EMPTY), 19221618Srie EC_XWORD(move->m_poffset), 0, 0, 0, 19231618Srie EC_LWORD(0), MSG_ORIG(MSG_STR_EMPTY)); 19240Sstevel@tonic-gate continue; 19250Sstevel@tonic-gate } 19261618Srie if (((symndx = ELF_M_SYM(move->m_info)) == 0) || 19271618Srie (symndx >= symnum)) { 19280Sstevel@tonic-gate (void) fprintf(stderr, 19290Sstevel@tonic-gate MSG_INTL(MSG_ERR_BADMINFO), file, 19301618Srie _cache->c_name, EC_XWORD(move->m_info)); 19311618Srie 19321618Srie (void) snprintf(index, MAXNDXSIZE, 19331618Srie MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx)); 19341618Srie dbg_print(0, fmt, index, 19351618Srie EC_XWORD(move->m_poffset), 19361618Srie ELF_M_SIZE(move->m_info), move->m_repeat, 19371618Srie move->m_stride, move->m_value, 19380Sstevel@tonic-gate MSG_INTL(MSG_STR_UNKNOWN)); 19390Sstevel@tonic-gate continue; 19400Sstevel@tonic-gate } 19410Sstevel@tonic-gate 19421618Srie symname = relsymname(cache, _cache, strsec, 19431618Srie symndx, symnum, ndx, syms, section, BUFSIZ, file, 19441618Srie flags); 19451618Srie sym = (Sym *)(syms + symndx); 19460Sstevel@tonic-gate 19470Sstevel@tonic-gate /* 19480Sstevel@tonic-gate * Additional sanity check. 19490Sstevel@tonic-gate */ 19501618Srie shndx = sym->st_shndx; 19510Sstevel@tonic-gate if (!((shndx == SHN_COMMON) || 19520Sstevel@tonic-gate (((shndx >= 1) && (shndx <= shnum)) && 19531618Srie (cache[shndx].c_shdr)->sh_type == SHT_NOBITS))) { 19540Sstevel@tonic-gate (void) fprintf(stderr, 19551618Srie MSG_INTL(MSG_ERR_BADSYM2), file, 19561618Srie _cache->c_name, demangle(symname, flags)); 19570Sstevel@tonic-gate } 19580Sstevel@tonic-gate 19591618Srie (void) snprintf(index, MAXNDXSIZE, 19601618Srie MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx)); 19611618Srie dbg_print(0, fmt, index, EC_XWORD(move->m_poffset), 19621618Srie ELF_M_SIZE(move->m_info), move->m_repeat, 19631618Srie move->m_stride, move->m_value, 19641618Srie demangle(symname, flags)); 19650Sstevel@tonic-gate } 19660Sstevel@tonic-gate } 19670Sstevel@tonic-gate } 19680Sstevel@tonic-gate 19690Sstevel@tonic-gate /* 19700Sstevel@tonic-gate * Traverse a note section analyzing each note information block. 19710Sstevel@tonic-gate * The data buffers size is used to validate references before they are made, 19720Sstevel@tonic-gate * and is decremented as each element is processed. 19730Sstevel@tonic-gate */ 19740Sstevel@tonic-gate void 19751618Srie note_entry(Cache *cache, Word *data, size_t size, const char *file) 19760Sstevel@tonic-gate { 19771618Srie size_t bsize = size; 19781618Srie 19790Sstevel@tonic-gate /* 19800Sstevel@tonic-gate * Print out a single `note' information block. 19810Sstevel@tonic-gate */ 19820Sstevel@tonic-gate while (size > 0) { 19831618Srie size_t namesz, descsz, type, pad, noteoff; 19840Sstevel@tonic-gate 19850Sstevel@tonic-gate noteoff = bsize - size; 19860Sstevel@tonic-gate /* 19870Sstevel@tonic-gate * Make sure we can at least reference the 3 initial entries 19880Sstevel@tonic-gate * (4-byte words) of the note information block. 19890Sstevel@tonic-gate */ 19901618Srie if (size >= (sizeof (Word) * 3)) 19911618Srie size -= (sizeof (Word) * 3); 19920Sstevel@tonic-gate else { 19931618Srie (void) fprintf(stderr, MSG_INTL(MSG_NOTE_BADDATASZ), 19941618Srie file, cache->c_name, EC_WORD(noteoff)); 19950Sstevel@tonic-gate return; 19960Sstevel@tonic-gate } 19970Sstevel@tonic-gate 19980Sstevel@tonic-gate /* 19990Sstevel@tonic-gate * Make sure any specified name string can be referenced. 20000Sstevel@tonic-gate */ 20010Sstevel@tonic-gate if ((namesz = *data++) != 0) { 20020Sstevel@tonic-gate if (size >= namesz) 20030Sstevel@tonic-gate size -= namesz; 20040Sstevel@tonic-gate else { 20050Sstevel@tonic-gate (void) fprintf(stderr, 20061618Srie MSG_INTL(MSG_NOTE_BADNMSZ), file, 20071618Srie cache->c_name, EC_WORD(noteoff), 20081618Srie EC_WORD(namesz)); 20090Sstevel@tonic-gate return; 20100Sstevel@tonic-gate } 20110Sstevel@tonic-gate } 20121618Srie 20130Sstevel@tonic-gate /* 20140Sstevel@tonic-gate * Make sure any specified descriptor can be referenced. 20150Sstevel@tonic-gate */ 20160Sstevel@tonic-gate if ((descsz = *data++) != 0) { 20170Sstevel@tonic-gate /* 20180Sstevel@tonic-gate * If namesz isn't a 4-byte multiple, account for any 20190Sstevel@tonic-gate * padding that must exist before the descriptor. 20200Sstevel@tonic-gate */ 20211618Srie if ((pad = (namesz & (sizeof (Word) - 1))) != 0) { 20221618Srie pad = sizeof (Word) - pad; 20230Sstevel@tonic-gate size -= pad; 20240Sstevel@tonic-gate } 20250Sstevel@tonic-gate if (size >= descsz) 20260Sstevel@tonic-gate size -= descsz; 20270Sstevel@tonic-gate else { 20280Sstevel@tonic-gate (void) fprintf(stderr, 20291618Srie MSG_INTL(MSG_NOTE_BADDESZ), file, 20301618Srie cache->c_name, EC_WORD(noteoff), 20311618Srie EC_WORD(namesz)); 20320Sstevel@tonic-gate return; 20330Sstevel@tonic-gate } 20340Sstevel@tonic-gate } 20350Sstevel@tonic-gate 20360Sstevel@tonic-gate type = *data++; 20370Sstevel@tonic-gate 20381618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 20391618Srie dbg_print(0, MSG_ORIG(MSG_NOTE_TYPE), EC_WORD(type)); 20400Sstevel@tonic-gate 20411618Srie dbg_print(0, MSG_ORIG(MSG_NOTE_NAMESZ), EC_WORD(namesz)); 20420Sstevel@tonic-gate if (namesz) { 20430Sstevel@tonic-gate char *name = (char *)data; 20440Sstevel@tonic-gate 20450Sstevel@tonic-gate /* 20460Sstevel@tonic-gate * Since the name string may have 'null' bytes 20470Sstevel@tonic-gate * in it (ia32 .string) - we just write the 20480Sstevel@tonic-gate * whole stream in a single fwrite. 20490Sstevel@tonic-gate */ 20500Sstevel@tonic-gate (void) fwrite(name, namesz, 1, stdout); 20510Sstevel@tonic-gate name = name + ((namesz + (sizeof (Word) - 1)) & 20520Sstevel@tonic-gate ~(sizeof (Word) - 1)); 20530Sstevel@tonic-gate /* LINTED */ 20540Sstevel@tonic-gate data = (Word *)name; 20551618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 20560Sstevel@tonic-gate } 20570Sstevel@tonic-gate 20580Sstevel@tonic-gate /* 20590Sstevel@tonic-gate * If multiple information blocks exist within a .note section 20600Sstevel@tonic-gate * account for any padding that must exist before the next 20610Sstevel@tonic-gate * information block. 20620Sstevel@tonic-gate */ 20631618Srie if ((pad = (descsz & (sizeof (Word) - 1))) != 0) { 20641618Srie pad = sizeof (Word) - pad; 20650Sstevel@tonic-gate if (size > pad) 20660Sstevel@tonic-gate size -= pad; 20670Sstevel@tonic-gate } 20680Sstevel@tonic-gate 20691618Srie dbg_print(0, MSG_ORIG(MSG_NOTE_DESCSZ), EC_WORD(descsz)); 20700Sstevel@tonic-gate if (descsz) { 20710Sstevel@tonic-gate int ndx, byte, word; 20721618Srie char string[58], *str = string; 20730Sstevel@tonic-gate uchar_t *desc = (uchar_t *)data; 20740Sstevel@tonic-gate 20750Sstevel@tonic-gate /* 20760Sstevel@tonic-gate * Dump descriptor bytes. 20770Sstevel@tonic-gate */ 20780Sstevel@tonic-gate for (ndx = byte = word = 0; descsz; descsz--, desc++) { 20790Sstevel@tonic-gate int tok = *desc; 20800Sstevel@tonic-gate 20810Sstevel@tonic-gate (void) snprintf(str, 58, MSG_ORIG(MSG_NOTE_TOK), 20820Sstevel@tonic-gate tok); 20830Sstevel@tonic-gate str += 3; 20840Sstevel@tonic-gate 20850Sstevel@tonic-gate if (++byte == 4) { 20860Sstevel@tonic-gate *str++ = ' ', *str++ = ' '; 20870Sstevel@tonic-gate word++; 20880Sstevel@tonic-gate byte = 0; 20890Sstevel@tonic-gate } 20900Sstevel@tonic-gate if (word == 4) { 20910Sstevel@tonic-gate *str = '\0'; 20921618Srie dbg_print(0, MSG_ORIG(MSG_NOTE_DESC), 20930Sstevel@tonic-gate ndx, string); 20940Sstevel@tonic-gate word = 0; 20950Sstevel@tonic-gate ndx += 16; 20960Sstevel@tonic-gate str = string; 20970Sstevel@tonic-gate } 20980Sstevel@tonic-gate } 20990Sstevel@tonic-gate if (byte || word) { 21000Sstevel@tonic-gate *str = '\0'; 21011618Srie dbg_print(0, MSG_ORIG(MSG_NOTE_DESC), 21021618Srie ndx, string); 21030Sstevel@tonic-gate } 21040Sstevel@tonic-gate 21050Sstevel@tonic-gate desc += pad; 21060Sstevel@tonic-gate /* LINTED */ 21070Sstevel@tonic-gate data = (Word *)desc; 21080Sstevel@tonic-gate } 21090Sstevel@tonic-gate } 21100Sstevel@tonic-gate } 21110Sstevel@tonic-gate 21120Sstevel@tonic-gate /* 21130Sstevel@tonic-gate * Search for and process a .note section. 21140Sstevel@tonic-gate */ 21150Sstevel@tonic-gate static void 21164168Sab196087 note(Cache *cache, Word shnum, const char *file) 21170Sstevel@tonic-gate { 21181618Srie Word cnt; 21190Sstevel@tonic-gate 21200Sstevel@tonic-gate /* 21210Sstevel@tonic-gate * Otherwise look for any .note sections. 21220Sstevel@tonic-gate */ 21230Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 21241618Srie Cache *_cache = &cache[cnt]; 21251618Srie Shdr *shdr = _cache->c_shdr; 21260Sstevel@tonic-gate 21270Sstevel@tonic-gate if (shdr->sh_type != SHT_NOTE) 21280Sstevel@tonic-gate continue; 21294168Sab196087 if (!match(0, _cache->c_name, cnt)) 21300Sstevel@tonic-gate continue; 21310Sstevel@tonic-gate 21320Sstevel@tonic-gate /* 21330Sstevel@tonic-gate * As these sections are often hand rolled, make sure they're 21340Sstevel@tonic-gate * properly aligned before proceeding. 21350Sstevel@tonic-gate */ 21360Sstevel@tonic-gate if (shdr->sh_offset & (sizeof (Word) - 1)) { 21370Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADALIGN), 21380Sstevel@tonic-gate file, _cache->c_name); 21390Sstevel@tonic-gate continue; 21400Sstevel@tonic-gate } 21413466Srie if (_cache->c_data == NULL) 21423466Srie continue; 21430Sstevel@tonic-gate 21441618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 21451618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_NOTE), _cache->c_name); 21460Sstevel@tonic-gate note_entry(_cache, (Word *)_cache->c_data->d_buf, 21470Sstevel@tonic-gate /* LINTED */ 21480Sstevel@tonic-gate (Word)_cache->c_data->d_size, file); 21490Sstevel@tonic-gate } 21500Sstevel@tonic-gate } 21510Sstevel@tonic-gate 21521618Srie /* 21531618Srie * Determine an individual hash entry. This may be the initial hash entry, 21541618Srie * or an associated chain entry. 21551618Srie */ 21561618Srie static void 21571618Srie hash_entry(Cache *refsec, Cache *strsec, const char *hsecname, Word hashndx, 21581618Srie Word symndx, Word symn, Sym *syms, const char *file, ulong_t bkts, 21591618Srie uint_t flags, int chain) 21601618Srie { 21611618Srie Sym *sym; 21621618Srie const char *symname, *str; 21631618Srie char _bucket[MAXNDXSIZE], _symndx[MAXNDXSIZE]; 21641618Srie ulong_t nbkt, nhash; 21651618Srie 21661618Srie if (symndx > symn) { 21671618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_HSBADSYMNDX), file, 21681618Srie EC_WORD(symndx), EC_WORD(hashndx)); 21691618Srie symname = MSG_INTL(MSG_STR_UNKNOWN); 21701618Srie } else { 21711618Srie sym = (Sym *)(syms + symndx); 21721618Srie symname = string(refsec, symndx, strsec, file, sym->st_name); 21731618Srie } 21741618Srie 21751618Srie if (chain == 0) { 21761618Srie (void) snprintf(_bucket, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER), 21771618Srie hashndx); 21781618Srie str = (const char *)_bucket; 21791618Srie } else 21801618Srie str = MSG_ORIG(MSG_STR_EMPTY); 21811618Srie 21821618Srie (void) snprintf(_symndx, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX2), 21831618Srie EC_WORD(symndx)); 21841618Srie dbg_print(0, MSG_ORIG(MSG_FMT_HASH_INFO), str, _symndx, 21851618Srie demangle(symname, flags)); 21861618Srie 21871618Srie /* 21881618Srie * Determine if this string is in the correct bucket. 21891618Srie */ 21901618Srie nhash = elf_hash(symname); 21911618Srie nbkt = nhash % bkts; 21921618Srie 21931618Srie if (nbkt != hashndx) { 21941618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADHASH), file, 21951618Srie hsecname, symname, EC_WORD(hashndx), nbkt); 21961618Srie } 21971618Srie } 21980Sstevel@tonic-gate 21990Sstevel@tonic-gate #define MAXCOUNT 500 22000Sstevel@tonic-gate 22010Sstevel@tonic-gate static void 22024168Sab196087 hash(Cache *cache, Word shnum, const char *file, uint_t flags) 22030Sstevel@tonic-gate { 22040Sstevel@tonic-gate static int count[MAXCOUNT]; 22051618Srie Word cnt; 22060Sstevel@tonic-gate ulong_t ndx, bkts; 22070Sstevel@tonic-gate char number[MAXNDXSIZE]; 22080Sstevel@tonic-gate 22090Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 22100Sstevel@tonic-gate uint_t *hash, *chain; 22110Sstevel@tonic-gate Cache *_cache = &cache[cnt]; 22121618Srie Shdr *sshdr, *hshdr = _cache->c_shdr; 22131618Srie char *ssecname, *hsecname = _cache->c_name; 22141618Srie Sym *syms; 22151618Srie Word symn; 22160Sstevel@tonic-gate 22171618Srie if (hshdr->sh_type != SHT_HASH) 22180Sstevel@tonic-gate continue; 22190Sstevel@tonic-gate 22200Sstevel@tonic-gate /* 22210Sstevel@tonic-gate * Determine the hash table data and size. 22220Sstevel@tonic-gate */ 22231618Srie if ((hshdr->sh_entsize == 0) || (hshdr->sh_size == 0)) { 22240Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 22251618Srie file, hsecname); 22260Sstevel@tonic-gate continue; 22270Sstevel@tonic-gate } 22283466Srie if (_cache->c_data == NULL) 22293466Srie continue; 22303466Srie 22310Sstevel@tonic-gate hash = (uint_t *)_cache->c_data->d_buf; 22320Sstevel@tonic-gate bkts = *hash; 22330Sstevel@tonic-gate chain = hash + 2 + bkts; 22340Sstevel@tonic-gate hash += 2; 22350Sstevel@tonic-gate 22360Sstevel@tonic-gate /* 22370Sstevel@tonic-gate * Get the data buffer for the associated symbol table. 22380Sstevel@tonic-gate */ 22391618Srie if ((hshdr->sh_link == 0) || (hshdr->sh_link >= shnum)) { 22400Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK), 22411618Srie file, hsecname, EC_WORD(hshdr->sh_link)); 22420Sstevel@tonic-gate continue; 22430Sstevel@tonic-gate } 22441618Srie 22451618Srie _cache = &cache[hshdr->sh_link]; 22461618Srie ssecname = _cache->c_name; 22471618Srie 22483466Srie if (_cache->c_data == NULL) 22493466Srie continue; 22503466Srie 22513466Srie if ((syms = (Sym *)_cache->c_data->d_buf) == NULL) { 22520Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 22531618Srie file, ssecname); 22540Sstevel@tonic-gate continue; 22550Sstevel@tonic-gate } 22560Sstevel@tonic-gate 22571618Srie sshdr = _cache->c_shdr; 22581618Srie /* LINTED */ 22591618Srie symn = (Word)(sshdr->sh_size / sshdr->sh_entsize); 22601618Srie 22610Sstevel@tonic-gate /* 22620Sstevel@tonic-gate * Get the associated string table section. 22630Sstevel@tonic-gate */ 22641618Srie if ((sshdr->sh_link == 0) || (sshdr->sh_link >= shnum)) { 22650Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK), 22661618Srie file, ssecname, EC_WORD(sshdr->sh_link)); 22670Sstevel@tonic-gate continue; 22680Sstevel@tonic-gate } 22690Sstevel@tonic-gate 22701618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 22711618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_HASH), hsecname); 22721618Srie dbg_print(0, MSG_INTL(MSG_ELF_HASH_INFO)); 22730Sstevel@tonic-gate 22740Sstevel@tonic-gate /* 22750Sstevel@tonic-gate * Loop through the hash buckets, printing the appropriate 22760Sstevel@tonic-gate * symbols. 22770Sstevel@tonic-gate */ 22780Sstevel@tonic-gate for (ndx = 0; ndx < bkts; ndx++, hash++) { 22791618Srie Word _ndx, _cnt; 22800Sstevel@tonic-gate 22810Sstevel@tonic-gate if (*hash == 0) { 22820Sstevel@tonic-gate count[0]++; 22830Sstevel@tonic-gate continue; 22840Sstevel@tonic-gate } 22850Sstevel@tonic-gate 22861618Srie hash_entry(_cache, &cache[sshdr->sh_link], hsecname, 22871618Srie ndx, *hash, symn, syms, file, bkts, flags, 0); 22880Sstevel@tonic-gate 22890Sstevel@tonic-gate /* 22900Sstevel@tonic-gate * Determine if any other symbols are chained to this 22910Sstevel@tonic-gate * bucket. 22920Sstevel@tonic-gate */ 22930Sstevel@tonic-gate _ndx = chain[*hash]; 22940Sstevel@tonic-gate _cnt = 1; 22950Sstevel@tonic-gate while (_ndx) { 22961618Srie hash_entry(_cache, &cache[sshdr->sh_link], 22971618Srie hsecname, ndx, _ndx, symn, syms, file, 22981618Srie bkts, flags, 1); 22990Sstevel@tonic-gate _ndx = chain[_ndx]; 23000Sstevel@tonic-gate _cnt++; 23010Sstevel@tonic-gate } 23020Sstevel@tonic-gate 23030Sstevel@tonic-gate if (_cnt >= MAXCOUNT) { 23040Sstevel@tonic-gate (void) fprintf(stderr, 23051324Srie MSG_INTL(MSG_HASH_OVERFLW), file, 23061618Srie _cache->c_name, EC_WORD(ndx), 23071618Srie EC_WORD(_cnt)); 23080Sstevel@tonic-gate } else 23090Sstevel@tonic-gate count[_cnt]++; 23100Sstevel@tonic-gate } 23110Sstevel@tonic-gate break; 23120Sstevel@tonic-gate } 23130Sstevel@tonic-gate 23140Sstevel@tonic-gate /* 23150Sstevel@tonic-gate * Print out the count information. 23160Sstevel@tonic-gate */ 23170Sstevel@tonic-gate bkts = cnt = 0; 23181618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 23191618Srie 23200Sstevel@tonic-gate for (ndx = 0; ndx < MAXCOUNT; ndx++) { 23211618Srie Word _cnt; 23220Sstevel@tonic-gate 23230Sstevel@tonic-gate if ((_cnt = count[ndx]) == 0) 23240Sstevel@tonic-gate continue; 23250Sstevel@tonic-gate 23261618Srie (void) snprintf(number, MAXNDXSIZE, 23271618Srie MSG_ORIG(MSG_FMT_INTEGER), _cnt); 23281618Srie dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS1), number, 23291618Srie EC_WORD(ndx)); 23300Sstevel@tonic-gate bkts += _cnt; 23311618Srie cnt += (Word)(ndx * _cnt); 23320Sstevel@tonic-gate } 23330Sstevel@tonic-gate if (cnt) { 23340Sstevel@tonic-gate (void) snprintf(number, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER), 23351618Srie bkts); 23361618Srie dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS2), number, 23371618Srie EC_WORD(cnt)); 23380Sstevel@tonic-gate } 23390Sstevel@tonic-gate } 23400Sstevel@tonic-gate 23410Sstevel@tonic-gate static void 23424168Sab196087 group(Cache *cache, Word shnum, const char *file, uint_t flags) 23430Sstevel@tonic-gate { 23441618Srie Word scnt; 23450Sstevel@tonic-gate 23461618Srie for (scnt = 1; scnt < shnum; scnt++) { 23471618Srie Cache *_cache = &cache[scnt]; 23481618Srie Shdr *shdr = _cache->c_shdr; 23491618Srie Word *grpdata, gcnt, grpcnt, symnum, unknown; 23501618Srie Cache *symsec, *strsec; 23511618Srie Sym *syms, *sym; 23521618Srie char flgstrbuf[MSG_GRP_COMDAT_SIZE + 10]; 23530Sstevel@tonic-gate 23540Sstevel@tonic-gate if (shdr->sh_type != SHT_GROUP) 23550Sstevel@tonic-gate continue; 23564168Sab196087 if (!match(0, _cache->c_name, scnt)) 23570Sstevel@tonic-gate continue; 23583466Srie if ((_cache->c_data == NULL) || 23593466Srie ((grpdata = (Word *)_cache->c_data->d_buf) == NULL)) 23600Sstevel@tonic-gate continue; 23611618Srie grpcnt = shdr->sh_size / sizeof (Word); 23621618Srie 23631618Srie /* 23641618Srie * Get the data buffer for the associated symbol table and 23651618Srie * string table. 23661618Srie */ 23671618Srie if (stringtbl(cache, 1, scnt, shnum, file, 23681618Srie &symnum, &symsec, &strsec) == 0) 23691618Srie return; 23701618Srie 23711618Srie syms = symsec->c_data->d_buf; 23721618Srie 23731618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 23741618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_GRP), _cache->c_name); 23751618Srie dbg_print(0, MSG_INTL(MSG_GRP_TITLE)); 23761618Srie 23771618Srie /* 23781618Srie * The first element of the group defines the group. The 23791618Srie * associated symbol is defined by the sh_link field. 23801618Srie */ 23811618Srie if ((shdr->sh_info == SHN_UNDEF) || (shdr->sh_info > symnum)) { 23821618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO), 23831618Srie file, _cache->c_name, EC_WORD(shdr->sh_info)); 23841618Srie return; 23850Sstevel@tonic-gate } 23860Sstevel@tonic-gate 23871618Srie (void) strcpy(flgstrbuf, MSG_ORIG(MSG_STR_OSQBRKT)); 23881618Srie if (grpdata[0] & GRP_COMDAT) { 23891618Srie (void) strcat(flgstrbuf, MSG_ORIG(MSG_GRP_COMDAT)); 23900Sstevel@tonic-gate } 23911618Srie if ((unknown = (grpdata[0] & ~GRP_COMDAT)) != 0) { 23921618Srie size_t len = strlen(flgstrbuf); 23931618Srie 23941618Srie (void) snprintf(&flgstrbuf[len], 23951618Srie (MSG_GRP_COMDAT_SIZE + 10 - len), 23961618Srie MSG_ORIG(MSG_GRP_UNKNOWN), unknown); 23970Sstevel@tonic-gate } 23981618Srie (void) strcat(flgstrbuf, MSG_ORIG(MSG_STR_CSQBRKT)); 23991618Srie sym = (Sym *)(syms + shdr->sh_info); 24000Sstevel@tonic-gate 24011618Srie dbg_print(0, MSG_INTL(MSG_GRP_SIGNATURE), flgstrbuf, 24021618Srie demangle(string(_cache, 0, strsec, file, sym->st_name), 24031618Srie flags)); 24041618Srie 24051618Srie for (gcnt = 1; gcnt < grpcnt; gcnt++) { 24060Sstevel@tonic-gate char index[MAXNDXSIZE]; 24071618Srie const char *name; 24080Sstevel@tonic-gate 24090Sstevel@tonic-gate (void) snprintf(index, MAXNDXSIZE, 24101618Srie MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(gcnt)); 24111618Srie 24121618Srie if (grpdata[gcnt] >= shnum) 24131618Srie name = MSG_INTL(MSG_GRP_INVALSCN); 24141618Srie else 24151618Srie name = cache[grpdata[gcnt]].c_name; 24161618Srie 24171618Srie (void) printf(MSG_ORIG(MSG_GRP_ENTRY), index, name, 2418*4433Sab196087 EC_XWORD(grpdata[gcnt])); 24190Sstevel@tonic-gate } 24200Sstevel@tonic-gate } 24210Sstevel@tonic-gate } 24220Sstevel@tonic-gate 24230Sstevel@tonic-gate static void 24241618Srie got(Cache *cache, Word shnum, Ehdr *ehdr, const char *file, uint_t flags) 24250Sstevel@tonic-gate { 24260Sstevel@tonic-gate Cache *gotcache = 0, *symtab = 0, *_cache; 24271618Srie Addr gotbgn, gotend; 24281618Srie Shdr *gotshdr; 24291618Srie Word cnt, gotents, gotndx; 24300Sstevel@tonic-gate size_t gentsize; 24310Sstevel@tonic-gate Got_info *gottable; 24320Sstevel@tonic-gate char *gotdata; 24331618Srie Sym *gotsym; 24341618Srie Xword gotsymaddr; 24350Sstevel@tonic-gate 24360Sstevel@tonic-gate /* 24371324Srie * First, find the got. 24380Sstevel@tonic-gate */ 24390Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 24400Sstevel@tonic-gate _cache = &cache[cnt]; 24411324Srie if (strncmp(_cache->c_name, MSG_ORIG(MSG_ELF_GOT), 24421324Srie MSG_ELF_GOT_SIZE) == 0) { 24430Sstevel@tonic-gate gotcache = _cache; 24440Sstevel@tonic-gate break; 24450Sstevel@tonic-gate } 24460Sstevel@tonic-gate } 24471618Srie if (gotcache == 0) 24480Sstevel@tonic-gate return; 24491324Srie 24501324Srie /* 24511324Srie * A got section within a relocatable object is suspicious. 24521324Srie */ 24531324Srie if (ehdr->e_type == ET_REL) { 24541324Srie (void) fprintf(stderr, MSG_INTL(MSG_GOT_UNEXPECTED), file, 24551324Srie _cache->c_name); 24561324Srie } 24571324Srie 24581618Srie gotshdr = gotcache->c_shdr; 24590Sstevel@tonic-gate if (gotshdr->sh_size == 0) { 24600Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 24610Sstevel@tonic-gate file, gotcache->c_name); 24620Sstevel@tonic-gate return; 24630Sstevel@tonic-gate } 24641618Srie 24651618Srie gotbgn = gotshdr->sh_addr; 24660Sstevel@tonic-gate gotend = gotbgn + gotshdr->sh_size; 24670Sstevel@tonic-gate 24680Sstevel@tonic-gate /* 24691618Srie * Some architectures don't properly set the sh_entsize for the GOT 24701618Srie * table. If it's not set, default to a size of a pointer. 24710Sstevel@tonic-gate */ 24721618Srie if ((gentsize = gotshdr->sh_entsize) == 0) 24731618Srie gentsize = sizeof (Xword); 24741618Srie 24753466Srie if (gotcache->c_data == NULL) 24763466Srie return; 24773466Srie 24780Sstevel@tonic-gate /* LINTED */ 24791618Srie gotents = (Word)(gotshdr->sh_size / gentsize); 24800Sstevel@tonic-gate gotdata = gotcache->c_data->d_buf; 24810Sstevel@tonic-gate 24820Sstevel@tonic-gate if ((gottable = calloc(gotents, sizeof (Got_info))) == 0) { 24830Sstevel@tonic-gate int err = errno; 24841618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC), file, 24851618Srie strerror(err)); 24860Sstevel@tonic-gate return; 24870Sstevel@tonic-gate } 24880Sstevel@tonic-gate 24890Sstevel@tonic-gate /* 24900Sstevel@tonic-gate * Now we scan through all the sections looking for any relocations 24910Sstevel@tonic-gate * that may be against the GOT. Since these may not be isolated to a 24920Sstevel@tonic-gate * .rel[a].got section we check them all. 24930Sstevel@tonic-gate * While scanning sections save the symbol table entry (a symtab 24940Sstevel@tonic-gate * overriding a dynsym) so that we can lookup _GLOBAL_OFFSET_TABLE_. 24950Sstevel@tonic-gate */ 24960Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 24971618Srie Word type, symnum; 24981618Srie Xword relndx, relnum, relsize; 24991618Srie void *rels; 25001618Srie Sym *syms; 25011618Srie Cache *symsec, *strsec; 25021618Srie Cache *_cache = &cache[cnt]; 25031618Srie Shdr *shdr; 25040Sstevel@tonic-gate 25051618Srie shdr = _cache->c_shdr; 25061618Srie type = shdr->sh_type; 25070Sstevel@tonic-gate 25081618Srie if ((symtab == 0) && (type == SHT_DYNSYM)) { 25090Sstevel@tonic-gate symtab = _cache; 25100Sstevel@tonic-gate continue; 25110Sstevel@tonic-gate } 25121618Srie if (type == SHT_SYMTAB) { 25130Sstevel@tonic-gate symtab = _cache; 25140Sstevel@tonic-gate continue; 25150Sstevel@tonic-gate } 25161618Srie if ((type != SHT_RELA) && (type != SHT_REL)) 25170Sstevel@tonic-gate continue; 25180Sstevel@tonic-gate 25190Sstevel@tonic-gate /* 25201618Srie * Decide entry size. 25210Sstevel@tonic-gate */ 25221618Srie if (((relsize = shdr->sh_entsize) == 0) || 25231618Srie (relsize > shdr->sh_size)) { 25241618Srie if (type == SHT_RELA) 25251618Srie relsize = sizeof (Rela); 25261618Srie else 25271618Srie relsize = sizeof (Rel); 25280Sstevel@tonic-gate } 25290Sstevel@tonic-gate 25300Sstevel@tonic-gate /* 25311618Srie * Determine the number of relocations available. 25320Sstevel@tonic-gate */ 25331618Srie if (shdr->sh_size == 0) { 25341618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 25351618Srie file, _cache->c_name); 25360Sstevel@tonic-gate continue; 25370Sstevel@tonic-gate } 25383466Srie if (_cache->c_data == NULL) 25393466Srie continue; 25403466Srie 25411618Srie rels = _cache->c_data->d_buf; 25421618Srie relnum = shdr->sh_size / relsize; 25430Sstevel@tonic-gate 25441618Srie /* 25451618Srie * Get the data buffer for the associated symbol table and 25461618Srie * string table. 25471618Srie */ 25481618Srie if (stringtbl(cache, 1, cnt, shnum, file, 25491618Srie &symnum, &symsec, &strsec) == 0) 25501618Srie continue; 25511618Srie 25521618Srie syms = symsec->c_data->d_buf; 25531618Srie 25541618Srie /* 25551618Srie * Loop through the relocation entries. 25561618Srie */ 25571618Srie for (relndx = 0; relndx < relnum; relndx++, 25581618Srie rels = (void *)((char *)rels + relsize)) { 25591618Srie char section[BUFSIZ]; 25601618Srie Addr offset; 25610Sstevel@tonic-gate Got_info *gip; 25621618Srie Word symndx, reltype; 25631618Srie Rela *rela; 25641618Srie Rel *rel; 25650Sstevel@tonic-gate 25661618Srie /* 25671618Srie * Unravel the relocation. 25681618Srie */ 25691618Srie if (type == SHT_RELA) { 25701618Srie rela = (Rela *)rels; 25711618Srie symndx = ELF_R_SYM(rela->r_info); 25721618Srie reltype = ELF_R_TYPE(rela->r_info); 25731618Srie offset = rela->r_offset; 25740Sstevel@tonic-gate } else { 25751618Srie rel = (Rel *)rels; 25761618Srie symndx = ELF_R_SYM(rel->r_info); 25771618Srie reltype = ELF_R_TYPE(rel->r_info); 25781618Srie offset = rel->r_offset; 25790Sstevel@tonic-gate } 25800Sstevel@tonic-gate 25810Sstevel@tonic-gate /* 25820Sstevel@tonic-gate * Only pay attention to relocations against the GOT. 25830Sstevel@tonic-gate */ 25844146Sab196087 if ((offset < gotbgn) || (offset >= gotend)) 25850Sstevel@tonic-gate continue; 25860Sstevel@tonic-gate 25870Sstevel@tonic-gate /* LINTED */ 25881618Srie gotndx = (Word)((offset - gotbgn) / 25890Sstevel@tonic-gate gotshdr->sh_entsize); 25900Sstevel@tonic-gate gip = &gottable[gotndx]; 25911618Srie 25921618Srie if (gip->g_reltype != 0) { 25930Sstevel@tonic-gate (void) fprintf(stderr, 25940Sstevel@tonic-gate MSG_INTL(MSG_GOT_MULTIPLE), file, 25951618Srie EC_WORD(gotndx), EC_ADDR(offset)); 25960Sstevel@tonic-gate continue; 25970Sstevel@tonic-gate } 25980Sstevel@tonic-gate 25991618Srie if (symndx) 26001618Srie gip->g_symname = relsymname(cache, _cache, 26011618Srie strsec, symndx, symnum, relndx, syms, 26021618Srie section, BUFSIZ, file, flags); 26031618Srie gip->g_reltype = reltype; 26041618Srie gip->g_rel = rels; 26050Sstevel@tonic-gate } 26060Sstevel@tonic-gate } 26070Sstevel@tonic-gate 26081618Srie if (symlookup(MSG_ORIG(MSG_GOT_SYM), cache, shnum, &gotsym, symtab, 26091618Srie file)) 26101618Srie gotsymaddr = gotsym->st_value; 26110Sstevel@tonic-gate else 26121618Srie gotsymaddr = gotbgn; 26130Sstevel@tonic-gate 26141618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 26151618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_GOT), gotcache->c_name); 26161618Srie Elf_got_title(0); 26170Sstevel@tonic-gate 26180Sstevel@tonic-gate for (gotndx = 0; gotndx < gotents; gotndx++) { 26190Sstevel@tonic-gate Got_info *gip; 26200Sstevel@tonic-gate Sword gindex; 26211618Srie Addr gaddr; 26221618Srie Xword gotentry; 26230Sstevel@tonic-gate 26240Sstevel@tonic-gate gip = &gottable[gotndx]; 26250Sstevel@tonic-gate 26260Sstevel@tonic-gate gaddr = gotbgn + (gotndx * gentsize); 26271618Srie gindex = (Sword)(gaddr - gotsymaddr) / (Sword)gentsize; 26280Sstevel@tonic-gate 26291618Srie if (gentsize == sizeof (Word)) 26300Sstevel@tonic-gate /* LINTED */ 26311618Srie gotentry = (Xword)(*((Word *)(gotdata) + gotndx)); 26320Sstevel@tonic-gate else 26330Sstevel@tonic-gate /* LINTED */ 26341618Srie gotentry = *((Xword *)(gotdata) + gotndx); 26350Sstevel@tonic-gate 26361618Srie Elf_got_entry(0, gindex, gaddr, gotentry, ehdr->e_machine, 26371618Srie gip->g_reltype, gip->g_rel, gip->g_symname); 26380Sstevel@tonic-gate } 26390Sstevel@tonic-gate free(gottable); 26400Sstevel@tonic-gate } 26410Sstevel@tonic-gate 26420Sstevel@tonic-gate void 26430Sstevel@tonic-gate checksum(Elf *elf) 26440Sstevel@tonic-gate { 26451618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 26461618Srie dbg_print(0, MSG_INTL(MSG_STR_CHECKSUM), elf_checksum(elf)); 26470Sstevel@tonic-gate } 26480Sstevel@tonic-gate 26494242Sab196087 /* 26504242Sab196087 * This variable is used by regular() to communicate the address of 26514242Sab196087 * the section header cache to sort_shdr_ndx_arr(). Unfortunately, 26524242Sab196087 * the qsort() interface does not include a userdata argument by which 26534242Sab196087 * such arbitrary data can be passed, so we are stuck using global data. 26544242Sab196087 */ 26554242Sab196087 static Cache *sort_shdr_ndx_arr_cache; 26564242Sab196087 26574242Sab196087 26584242Sab196087 /* 26594242Sab196087 * Used with qsort() to sort the section indices so that they can be 26604242Sab196087 * used to access the section headers in order of increasing data offset. 26614242Sab196087 * 26624242Sab196087 * entry: 26634242Sab196087 * sort_shdr_ndx_arr_cache - Contains address of 26644242Sab196087 * section header cache. 26654242Sab196087 * v1, v2 - Point at elements of sort_shdr_bits array to be compared. 26664242Sab196087 * 26674242Sab196087 * exit: 26684242Sab196087 * Returns -1 (less than), 0 (equal) or 1 (greater than). 26694242Sab196087 */ 26704242Sab196087 static int 26714242Sab196087 sort_shdr_ndx_arr(const void *v1, const void *v2) 26724242Sab196087 { 26734242Sab196087 Cache *cache1 = sort_shdr_ndx_arr_cache + *((size_t *)v1); 26744242Sab196087 Cache *cache2 = sort_shdr_ndx_arr_cache + *((size_t *)v2); 26754242Sab196087 26764242Sab196087 if (cache1->c_shdr->sh_offset < cache2->c_shdr->sh_offset) 26774242Sab196087 return (-1); 26784242Sab196087 26794242Sab196087 if (cache1->c_shdr->sh_offset > cache2->c_shdr->sh_offset) 26804242Sab196087 return (1); 26814242Sab196087 26824242Sab196087 return (0); 26834242Sab196087 } 26844242Sab196087 26854242Sab196087 26861618Srie void 26874168Sab196087 regular(const char *file, Elf *elf, uint_t flags, int wfd) 26880Sstevel@tonic-gate { 26890Sstevel@tonic-gate Elf_Scn *scn; 26901618Srie Ehdr *ehdr; 26910Sstevel@tonic-gate Elf_Data *data; 26924242Sab196087 size_t ndx, shstrndx, shnum, phnum; 26931618Srie Shdr *nameshdr, *shdr; 26940Sstevel@tonic-gate char *names = 0; 26950Sstevel@tonic-gate Cache *cache, *_cache; 26963875Sab196087 VERSYM_STATE versym; 26974242Sab196087 size_t *shdr_ndx_arr, shdr_ndx_arr_cnt; 26980Sstevel@tonic-gate 26991618Srie if ((ehdr = elf_getehdr(elf)) == NULL) { 27000Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETEHDR)); 27010Sstevel@tonic-gate return; 27020Sstevel@tonic-gate } 27030Sstevel@tonic-gate 27041618Srie if (elf_getshnum(elf, &shnum) == 0) { 27050Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETSHNUM)); 27060Sstevel@tonic-gate return; 27070Sstevel@tonic-gate } 27080Sstevel@tonic-gate 2709942Sahl if (elf_getshstrndx(elf, &shstrndx) == 0) { 27100Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETSHSTRNDX)); 27110Sstevel@tonic-gate return; 27120Sstevel@tonic-gate } 2713942Sahl 27141618Srie if (elf_getphnum(elf, &phnum) == 0) { 2715942Sahl failure(file, MSG_ORIG(MSG_ELF_GETPHNUM)); 2716942Sahl return; 2717942Sahl } 2718942Sahl 27190Sstevel@tonic-gate if ((scn = elf_getscn(elf, 0)) != NULL) { 27201618Srie if ((shdr = elf_getshdr(scn)) == NULL) { 27210Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETSHDR)); 27220Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN), 0); 27230Sstevel@tonic-gate return; 27240Sstevel@tonic-gate } 27250Sstevel@tonic-gate } else 27261618Srie shdr = 0; 27270Sstevel@tonic-gate 27280Sstevel@tonic-gate /* 27290Sstevel@tonic-gate * Print the elf header. 27300Sstevel@tonic-gate */ 27310Sstevel@tonic-gate if (flags & FLG_EHDR) 27321618Srie Elf_ehdr(0, ehdr, shdr); 27330Sstevel@tonic-gate 27340Sstevel@tonic-gate /* 27354146Sab196087 * If the section headers or program headers have inadequate 27364146Sab196087 * alignment for the class of object, print a warning. libelf 27374146Sab196087 * can handle such files, but programs that use them can crash 27384146Sab196087 * when they dereference unaligned items. 27394146Sab196087 */ 27404146Sab196087 if (ehdr->e_phoff & (sizeof (Addr) - 1)) 27414146Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADPHDRALIGN), file); 27424146Sab196087 if (ehdr->e_shoff & (sizeof (Addr) - 1)) 27434146Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHDRALIGN), file); 27444146Sab196087 27454146Sab196087 /* 27460Sstevel@tonic-gate * Print the program headers. 27470Sstevel@tonic-gate */ 27481618Srie if ((flags & FLG_PHDR) && (phnum != 0)) { 27491618Srie Phdr *phdr; 27500Sstevel@tonic-gate 27511618Srie if ((phdr = elf_getphdr(elf)) == NULL) { 27521618Srie failure(file, MSG_ORIG(MSG_ELF_GETPHDR)); 27531618Srie return; 27541618Srie } 27550Sstevel@tonic-gate 27564242Sab196087 for (ndx = 0; ndx < phnum; phdr++, ndx++) { 27574168Sab196087 if (!match(0, conv_phdr_type(ehdr->e_machine, 27584242Sab196087 phdr->p_type, CONV_FMT_ALTFILE), ndx)) 27593862Srie continue; 27603862Srie 27611618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 27624242Sab196087 dbg_print(0, MSG_INTL(MSG_ELF_PHDR), EC_WORD(ndx)); 27631618Srie Elf_phdr(0, ehdr->e_machine, phdr); 27640Sstevel@tonic-gate } 27650Sstevel@tonic-gate } 27660Sstevel@tonic-gate 27670Sstevel@tonic-gate /* 2768942Sahl * Return now if there are no section, if there's just one section to 27694168Sab196087 * act as an extension of the ELF header, or if only program header 27704168Sab196087 * information was requested. 27710Sstevel@tonic-gate */ 2772942Sahl if ((shnum <= 1) || (flags && (flags & ~(FLG_EHDR | FLG_PHDR)) == 0)) { 27731618Srie if ((ehdr->e_type == ET_CORE) && (flags & FLG_NOTE)) 27744168Sab196087 note(0, shnum, file); 27750Sstevel@tonic-gate return; 27760Sstevel@tonic-gate } 27770Sstevel@tonic-gate 27780Sstevel@tonic-gate /* 27790Sstevel@tonic-gate * Obtain the .shstrtab data buffer to provide the required section 27800Sstevel@tonic-gate * name strings. 27810Sstevel@tonic-gate */ 27824156Sab196087 if (shstrndx == SHN_UNDEF) { 27834156Sab196087 /* 27844156Sab196087 * It is rare, but legal, for an object to lack a 27854156Sab196087 * header string table section. 27864156Sab196087 */ 27874156Sab196087 names = NULL; 27884156Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHSTRSEC), file); 27894156Sab196087 } else if ((scn = elf_getscn(elf, shstrndx)) == NULL) { 27900Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETSCN)); 27910Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SHDR), 27920Sstevel@tonic-gate EC_XWORD(shstrndx)); 27931618Srie 27940Sstevel@tonic-gate } else if ((data = elf_getdata(scn, NULL)) == NULL) { 27950Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETDATA)); 27960Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_DATA), 27970Sstevel@tonic-gate EC_XWORD(shstrndx)); 27981618Srie 27991618Srie } else if ((nameshdr = elf_getshdr(scn)) == NULL) { 28000Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETSHDR)); 28010Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN), 28023862Srie EC_WORD(elf_ndxscn(scn))); 28031618Srie 28041618Srie } else if ((names = data->d_buf) == 0) 28050Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_SHSTRNULL), file); 28060Sstevel@tonic-gate 28070Sstevel@tonic-gate /* 28083862Srie * Allocate a cache to maintain a descriptor for each section. 28090Sstevel@tonic-gate */ 28104242Sab196087 if ((cache = malloc(shnum * sizeof (Cache))) == NULL) { 28110Sstevel@tonic-gate int err = errno; 28120Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC), 28130Sstevel@tonic-gate file, strerror(err)); 28140Sstevel@tonic-gate return; 28150Sstevel@tonic-gate } 28160Sstevel@tonic-gate 28171618Srie *cache = cache_init; 28180Sstevel@tonic-gate _cache = cache; 28190Sstevel@tonic-gate _cache++; 28200Sstevel@tonic-gate 28213862Srie /* 28224242Sab196087 * Allocate an array that will hold the section index for 28234242Sab196087 * each section that has data in the ELF file: 28244242Sab196087 * 28254242Sab196087 * - Is not a NOBITS section 28264242Sab196087 * - Data has non-zero length 28274242Sab196087 * 28284242Sab196087 * Note that shnum is an upper bound on the size required. It 28294242Sab196087 * is likely that we won't use a few of these array elements. 28304242Sab196087 * Allocating a modest amount of extra memory in this case means 28314242Sab196087 * that we can avoid an extra loop to count the number of needed 28324242Sab196087 * items, and can fill this array immediately in the first loop 28334242Sab196087 * below. 28344242Sab196087 */ 28354242Sab196087 if ((shdr_ndx_arr = malloc(shnum * sizeof (*shdr_ndx_arr))) == NULL) { 28364242Sab196087 int err = errno; 28374242Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC), 28384242Sab196087 file, strerror(err)); 28394242Sab196087 return; 28404242Sab196087 } 28414242Sab196087 shdr_ndx_arr_cnt = 0; 28424242Sab196087 28434242Sab196087 /* 28443862Srie * Traverse the sections of the file. This gathering of data is 28453862Srie * carried out in two passes. First, the section headers are captured 28463862Srie * and the section header names are evaluated. A verification pass is 28473862Srie * then carried out over the section information. Files have been 28483862Srie * known to exhibit overlapping (and hence erroneous) section header 28493862Srie * information. 28503862Srie * 28513862Srie * Finally, the data for each section is obtained. This processing is 28523862Srie * carried out after section verification because should any section 28533862Srie * header overlap occur, and a file needs translating (ie. xlate'ing 28543862Srie * information from a non-native architecture file), then the process 28553862Srie * of translation can corrupt the section header information. Of 28563862Srie * course, if there is any section overlap, the data related to the 28573862Srie * sections is going to be compromised. However, it is the translation 28583862Srie * of this data that has caused problems with elfdump()'s ability to 28593862Srie * extract the data. 28603862Srie */ 28614242Sab196087 for (ndx = 1, scn = NULL; scn = elf_nextscn(elf, scn); 28624242Sab196087 ndx++, _cache++) { 28633862Srie char scnndxnm[100]; 28643862Srie 28654242Sab196087 _cache->c_ndx = ndx; 28663862Srie _cache->c_scn = scn; 28673862Srie 28681618Srie if ((_cache->c_shdr = elf_getshdr(scn)) == NULL) { 28690Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETSHDR)); 28700Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN), 28713862Srie EC_WORD(elf_ndxscn(scn))); 28720Sstevel@tonic-gate } 28730Sstevel@tonic-gate 28743862Srie /* 28754242Sab196087 * If this section has data in the file, include it in 28764242Sab196087 * the array of sections to check for address overlap. 28774242Sab196087 */ 28784242Sab196087 if ((_cache->c_shdr->sh_size != 0) && 28794242Sab196087 (_cache->c_shdr->sh_type != SHT_NOBITS)) 28804242Sab196087 shdr_ndx_arr[shdr_ndx_arr_cnt++] = ndx; 28814242Sab196087 28824242Sab196087 /* 28833862Srie * If a shstrtab exists, assign the section name. 28843862Srie */ 28853862Srie if (names && _cache->c_shdr) { 28863862Srie if (_cache->c_shdr->sh_name && 28873862Srie /* LINTED */ 28883862Srie (nameshdr->sh_size > _cache->c_shdr->sh_name)) { 28893862Srie _cache->c_name = 28903862Srie names + _cache->c_shdr->sh_name; 28913862Srie continue; 28923862Srie } 28930Sstevel@tonic-gate 28940Sstevel@tonic-gate /* 28953862Srie * Generate an error if the section name index is zero 28963862Srie * or exceeds the shstrtab data. Fall through to 28973862Srie * fabricate a section name. 28980Sstevel@tonic-gate */ 28993862Srie if ((_cache->c_shdr->sh_name == 0) || 29000Sstevel@tonic-gate /* LINTED */ 29011618Srie (nameshdr->sh_size <= _cache->c_shdr->sh_name)) { 29020Sstevel@tonic-gate (void) fprintf(stderr, 29030Sstevel@tonic-gate MSG_INTL(MSG_ERR_BADSHNAME), file, 29044242Sab196087 EC_WORD(ndx), 29051618Srie EC_XWORD(_cache->c_shdr->sh_name)); 29060Sstevel@tonic-gate } 29073862Srie } 29083862Srie 29093862Srie /* 29103862Srie * If there exists no shstrtab data, or a section header has no 29113862Srie * name (an invalid index of 0), then compose a name for the 29123862Srie * section. 29133862Srie */ 29143862Srie (void) snprintf(scnndxnm, sizeof (scnndxnm), 29154242Sab196087 MSG_INTL(MSG_FMT_SCNNDX), ndx); 29164242Sab196087 29174242Sab196087 if ((_cache->c_name = malloc(strlen(scnndxnm) + 1)) == NULL) { 29183862Srie int err = errno; 29193862Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC), 29203862Srie file, strerror(err)); 29213862Srie return; 29223862Srie } 29233862Srie (void) strcpy(_cache->c_name, scnndxnm); 29243862Srie } 29253862Srie 29263862Srie /* 29273862Srie * Having collected all the sections, validate their address range. 29283862Srie * Cases have existed where the section information has been invalid. 29293862Srie * This can lead to all sorts of other, hard to diagnose errors, as 29303862Srie * each section is processed individually (ie. with elf_getdata()). 29313862Srie * Here, we carry out some address comparisons to catch a family of 29323862Srie * overlapping memory issues we have observed (likely, there are others 29333862Srie * that we have yet to discover). 29343862Srie * 29353862Srie * Note, should any memory overlap occur, obtaining any additional 29363862Srie * data from the file is questionable. However, it might still be 29373862Srie * possible to inspect the ELF header, Programs headers, or individual 29383862Srie * sections, so rather than bailing on an error condition, continue 29393862Srie * processing to see if any data can be salvaged. 29403862Srie */ 29414242Sab196087 if (shdr_ndx_arr_cnt > 1) { 29424242Sab196087 sort_shdr_ndx_arr_cache = cache; 29434242Sab196087 qsort(shdr_ndx_arr, shdr_ndx_arr_cnt, 29444242Sab196087 sizeof (*shdr_ndx_arr), sort_shdr_ndx_arr); 29454242Sab196087 } 29464242Sab196087 for (ndx = 0; ndx < shdr_ndx_arr_cnt; ndx++) { 29474242Sab196087 Cache *_cache = cache + shdr_ndx_arr[ndx]; 29483862Srie Shdr *shdr = _cache->c_shdr; 29493862Srie Off bgn1, bgn = shdr->sh_offset; 29503862Srie Off end1, end = shdr->sh_offset + shdr->sh_size; 29514242Sab196087 size_t ndx1; 29524242Sab196087 29534242Sab196087 /* 29544242Sab196087 * Check the section against all following ones, reporting 29554242Sab196087 * any overlaps. Since we've sorted the sections by offset, 29564242Sab196087 * we can stop after the first comparison that fails. There 29574242Sab196087 * are no overlaps in a properly formed ELF file, in which 29584242Sab196087 * case this algorithm runs in O(n) time. This will degenerate 29594242Sab196087 * to O(n^2) for a completely broken file. Such a file is 29604242Sab196087 * (1) highly unlikely, and (2) unusable, so it is reasonable 29614242Sab196087 * for the analysis to take longer. 29624242Sab196087 */ 29634242Sab196087 for (ndx1 = ndx + 1; ndx1 < shdr_ndx_arr_cnt; ndx1++) { 29644242Sab196087 Cache *_cache1 = cache + shdr_ndx_arr[ndx1]; 29653862Srie Shdr *shdr1 = _cache1->c_shdr; 29663862Srie 29673862Srie bgn1 = shdr1->sh_offset; 29683862Srie end1 = shdr1->sh_offset + shdr1->sh_size; 29693862Srie 29703862Srie if (((bgn1 <= bgn) && (end1 > bgn)) || 29713862Srie ((bgn1 < end) && (end1 >= end))) { 29723862Srie (void) fprintf(stderr, 29733862Srie MSG_INTL(MSG_ERR_SECMEMOVER), file, 29744242Sab196087 EC_WORD(elf_ndxscn(_cache->c_scn)), 29754242Sab196087 _cache->c_name, EC_OFF(bgn), EC_OFF(end), 29763862Srie EC_WORD(elf_ndxscn(_cache1->c_scn)), 29774242Sab196087 _cache1->c_name, EC_OFF(bgn1), 29784242Sab196087 EC_OFF(end1)); 29794242Sab196087 } else { /* No overlap, so can stop */ 29804242Sab196087 break; 29810Sstevel@tonic-gate } 29820Sstevel@tonic-gate } 29830Sstevel@tonic-gate 29843862Srie /* 29854242Sab196087 * In addition to checking for sections overlapping 29864242Sab196087 * each other (done above), we should also make sure 29874242Sab196087 * the section doesn't overlap the section header array. 29883862Srie */ 29893862Srie bgn1 = ehdr->e_shoff; 29903862Srie end1 = ehdr->e_shoff + (ehdr->e_shentsize * ehdr->e_shnum); 29913862Srie 29923862Srie if (((bgn1 <= bgn) && (end1 > bgn)) || 29933862Srie ((bgn1 < end) && (end1 >= end))) { 29943862Srie (void) fprintf(stderr, 29953862Srie MSG_INTL(MSG_ERR_SHDRMEMOVER), file, EC_OFF(bgn1), 29963862Srie EC_OFF(end1), 29973862Srie EC_WORD(elf_ndxscn(_cache->c_scn)), 29983862Srie _cache->c_name, EC_OFF(bgn), EC_OFF(end)); 29993862Srie } 30003862Srie } 30013862Srie 30023862Srie /* 30034242Sab196087 * Obtain the data for each section. 30043862Srie */ 30054242Sab196087 for (ndx = 1; ndx < shnum; ndx++) { 30064242Sab196087 Cache *_cache = &cache[ndx]; 30073862Srie Elf_Scn *scn = _cache->c_scn; 30083862Srie 30090Sstevel@tonic-gate if ((_cache->c_data = elf_getdata(scn, NULL)) == NULL) { 30100Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETDATA)); 30110Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCNDATA), 30123862Srie EC_WORD(elf_ndxscn(scn))); 30130Sstevel@tonic-gate } 30140Sstevel@tonic-gate 30150Sstevel@tonic-gate /* 30160Sstevel@tonic-gate * Do we wish to write the section out? 30170Sstevel@tonic-gate */ 30184242Sab196087 if (wfd && match(1, _cache->c_name, ndx) && _cache->c_data) { 30190Sstevel@tonic-gate (void) write(wfd, _cache->c_data->d_buf, 30200Sstevel@tonic-gate _cache->c_data->d_size); 30210Sstevel@tonic-gate } 30220Sstevel@tonic-gate } 30230Sstevel@tonic-gate 30240Sstevel@tonic-gate if (flags & FLG_SHDR) 30254168Sab196087 sections(file, cache, shnum, ehdr); 30260Sstevel@tonic-gate 30270Sstevel@tonic-gate if (flags & FLG_INTERP) 30281618Srie interp(file, cache, shnum, phnum, elf); 30290Sstevel@tonic-gate 30303875Sab196087 versions(cache, shnum, file, flags, &versym); 30310Sstevel@tonic-gate 30320Sstevel@tonic-gate if (flags & FLG_SYMBOLS) 30334168Sab196087 symbols(cache, shnum, ehdr, &versym, file, flags); 30340Sstevel@tonic-gate 30353492Sab196087 if (flags & FLG_SORT) 30364168Sab196087 sunw_sort(cache, shnum, ehdr, &versym, file, flags); 30373492Sab196087 30380Sstevel@tonic-gate if (flags & FLG_HASH) 30394168Sab196087 hash(cache, shnum, file, flags); 30400Sstevel@tonic-gate 30410Sstevel@tonic-gate if (flags & FLG_GOT) 30421618Srie got(cache, shnum, ehdr, file, flags); 30430Sstevel@tonic-gate 30440Sstevel@tonic-gate if (flags & FLG_GROUP) 30454168Sab196087 group(cache, shnum, file, flags); 30460Sstevel@tonic-gate 30470Sstevel@tonic-gate if (flags & FLG_SYMINFO) 30480Sstevel@tonic-gate syminfo(cache, shnum, file); 30490Sstevel@tonic-gate 30500Sstevel@tonic-gate if (flags & FLG_RELOC) 30514168Sab196087 reloc(cache, shnum, ehdr, file, flags); 30520Sstevel@tonic-gate 30530Sstevel@tonic-gate if (flags & FLG_DYNAMIC) 30541618Srie dynamic(cache, shnum, ehdr, file); 30550Sstevel@tonic-gate 30560Sstevel@tonic-gate if (flags & FLG_NOTE) 30574168Sab196087 note(cache, shnum, file); 30580Sstevel@tonic-gate 30590Sstevel@tonic-gate if (flags & FLG_MOVE) 30604168Sab196087 move(cache, shnum, file, flags); 30610Sstevel@tonic-gate 30620Sstevel@tonic-gate if (flags & FLG_CHECKSUM) 30630Sstevel@tonic-gate checksum(elf); 30640Sstevel@tonic-gate 30650Sstevel@tonic-gate if (flags & FLG_CAP) 30661618Srie cap(file, cache, shnum, phnum, ehdr, elf); 30670Sstevel@tonic-gate 30680Sstevel@tonic-gate if (flags & FLG_UNWIND) 30694168Sab196087 unwind(cache, shnum, phnum, ehdr, file, elf); 30700Sstevel@tonic-gate 30710Sstevel@tonic-gate free(cache); 30720Sstevel@tonic-gate } 3073