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 3261618Srie sections(const char *file, Cache *cache, Word shnum, Ehdr *ehdr, 3271618Srie const char *name) 3280Sstevel@tonic-gate { 3291618Srie size_t seccnt; 3300Sstevel@tonic-gate 3311618Srie for (seccnt = 1; seccnt < shnum; seccnt++) { 3321618Srie Cache *_cache = &cache[seccnt]; 3331618Srie Shdr *shdr = _cache->c_shdr; 3341618Srie const char *secname = _cache->c_name; 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate /* 3370Sstevel@tonic-gate * Although numerous section header entries can be zero, it's 3383862Srie * usually a sign of trouble if the type is zero. 3390Sstevel@tonic-gate */ 3400Sstevel@tonic-gate if (shdr->sh_type == 0) { 3410Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHTYPE), 3421618Srie file, secname, EC_WORD(shdr->sh_type)); 3430Sstevel@tonic-gate } 3443862Srie 3453862Srie if (name && strcmp(name, secname)) 3463862Srie continue; 3470Sstevel@tonic-gate 3481324Srie /* 3491324Srie * Identify any sections that are suspicious. A .got section 3501324Srie * shouldn't exist in a relocatable object. 3511324Srie */ 3521324Srie if (ehdr->e_type == ET_REL) { 3531618Srie if (strncmp(secname, MSG_ORIG(MSG_ELF_GOT), 3541324Srie MSG_ELF_GOT_SIZE) == 0) { 3551324Srie (void) fprintf(stderr, 3561618Srie MSG_INTL(MSG_GOT_UNEXPECTED), file, 3571618Srie secname); 3581324Srie } 3591324Srie } 3601324Srie 3611618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 3621618Srie dbg_print(0, MSG_INTL(MSG_ELF_SHDR), EC_WORD(seccnt), secname); 3631618Srie Elf_shdr(0, ehdr->e_machine, shdr); 3640Sstevel@tonic-gate } 3650Sstevel@tonic-gate } 3660Sstevel@tonic-gate 3671618Srie /* 3681618Srie * A couple of instances of unwind data are printed as tables of 8 data items 3691618Srie * expressed as 0x?? integers. 3701618Srie */ 3711618Srie #define UNWINDTBLSZ 10 + (8 * 5) + 1 3721618Srie 3731618Srie static void 3741618Srie unwindtbl(uint64_t *ndx, uint_t len, uchar_t *data, uint64_t doff, 3751618Srie const char *msg, const char *pre, size_t plen) 3761618Srie { 3771618Srie char buffer[UNWINDTBLSZ]; 3781618Srie uint_t boff = plen, cnt = 0; 3791618Srie 3801618Srie dbg_print(0, msg); 3811618Srie (void) strncpy(buffer, pre, UNWINDTBLSZ); 3821618Srie 3831618Srie while (*ndx < (len + 4)) { 3841618Srie if (cnt == 8) { 3851618Srie dbg_print(0, buffer); 3861618Srie boff = plen; 3871618Srie cnt = 0; 3881618Srie } 3891618Srie (void) snprintf(&buffer[boff], UNWINDTBLSZ - boff, 3901618Srie MSG_ORIG(MSG_UNW_TBLENTRY), data[doff + (*ndx)++]); 3911618Srie boff += 5; 3921618Srie cnt++; 3931618Srie } 3941618Srie if (cnt) 3951618Srie dbg_print(0, buffer); 3961618Srie } 3971618Srie 3981618Srie /* 3991618Srie * Obtain a specified Phdr entry. 4001618Srie */ 4011618Srie static Phdr * 4021618Srie getphdr(Word phnum, Word type, const char *file, Elf *elf) 4031618Srie { 4041618Srie Word cnt; 4051618Srie Phdr *phdr; 4061618Srie 4071618Srie if ((phdr = elf_getphdr(elf)) == NULL) { 4081618Srie failure(file, MSG_ORIG(MSG_ELF_GETPHDR)); 4091618Srie return (0); 4101618Srie } 4111618Srie 4121618Srie for (cnt = 0; cnt < phnum; phdr++, cnt++) { 4131618Srie if (phdr->p_type == type) 4141618Srie return (phdr); 4151618Srie } 4161618Srie return (0); 4171618Srie } 4181618Srie 4190Sstevel@tonic-gate static void 4201618Srie unwind(Cache *cache, Word shnum, Word phnum, Ehdr *ehdr, const char *name, 4211618Srie const char *file, Elf *elf) 4220Sstevel@tonic-gate { 4231618Srie Word cnt; 4241618Srie Phdr *uphdr = 0; 4251618Srie 4260Sstevel@tonic-gate /* 4271618Srie * For the moment - UNWIND is only relevant for a AMD64 object. 4280Sstevel@tonic-gate */ 4290Sstevel@tonic-gate if (ehdr->e_machine != EM_AMD64) 4301618Srie return; 4310Sstevel@tonic-gate 4321618Srie if (phnum) 4331618Srie uphdr = getphdr(phnum, PT_SUNW_UNWIND, file, elf); 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 4361618Srie Cache *_cache = &cache[cnt]; 4371618Srie Shdr *shdr = _cache->c_shdr; 4381618Srie uchar_t *data; 4390Sstevel@tonic-gate size_t datasize; 4401618Srie uint64_t off, ndx, frame_ptr, fde_cnt, tabndx; 4411618Srie uint_t vers, frame_ptr_enc, fde_cnt_enc, table_enc; 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate /* 4441618Srie * AMD64 - this is a strmcp() just to find the gcc produced 4451618Srie * sections. Soon gcc should be setting the section type - and 4461618Srie * we'll not need this strcmp(). 4470Sstevel@tonic-gate */ 4480Sstevel@tonic-gate if ((shdr->sh_type != SHT_AMD64_UNWIND) && 4490Sstevel@tonic-gate (strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRM), 4500Sstevel@tonic-gate MSG_SCN_FRM_SIZE) != 0) && 4510Sstevel@tonic-gate (strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRMHDR), 4520Sstevel@tonic-gate MSG_SCN_FRMHDR_SIZE) != 0)) 4530Sstevel@tonic-gate continue; 4540Sstevel@tonic-gate if (name && strcmp(name, _cache->c_name)) 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) { 5140Sstevel@tonic-gate uint_t cieid, cielength, cieversion, 5150Sstevel@tonic-gate 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]) 5681618Srie dbg_print(0, MSG_ORIG(MSG_UNW_CIEAUXVAL)); 5691618Srie 5700Sstevel@tonic-gate for (cieaugndx = 0; cieaugstr[cieaugndx]; 5710Sstevel@tonic-gate cieaugndx++) { 5720Sstevel@tonic-gate uint_t val; 5731618Srie 5740Sstevel@tonic-gate switch (cieaugstr[cieaugndx]) { 5750Sstevel@tonic-gate case 'z': 5760Sstevel@tonic-gate val = uleb_extract(&data[off], 5770Sstevel@tonic-gate &ndx); 5781618Srie dbg_print(0, 5790Sstevel@tonic-gate MSG_ORIG(MSG_UNW_CIEAUXSIZE), 5800Sstevel@tonic-gate val); 5810Sstevel@tonic-gate cieZflag = 1; 5820Sstevel@tonic-gate break; 5830Sstevel@tonic-gate case 'P': 5840Sstevel@tonic-gate ciePflag = data[off + ndx]; 5850Sstevel@tonic-gate ndx += 1; 5860Sstevel@tonic-gate 5870Sstevel@tonic-gate persVal = dwarf_ehe_extract( 5880Sstevel@tonic-gate &data[off], 5890Sstevel@tonic-gate &ndx, ciePflag, ehdr->e_ident, 5900Sstevel@tonic-gate shdr->sh_addr + off + ndx); 5911618Srie dbg_print(0, 5920Sstevel@tonic-gate MSG_ORIG(MSG_UNW_CIEAUXPERS), 5930Sstevel@tonic-gate ciePflag, 5941618Srie conv_dwarf_ehe(ciePflag), 5950Sstevel@tonic-gate EC_XWORD(persVal)); 5960Sstevel@tonic-gate break; 5970Sstevel@tonic-gate case 'R': 5980Sstevel@tonic-gate val = data[off + ndx]; 5990Sstevel@tonic-gate ndx += 1; 6001618Srie dbg_print(0, 6010Sstevel@tonic-gate MSG_ORIG(MSG_UNW_CIEAUXCENC), 6021618Srie val, conv_dwarf_ehe(val)); 6030Sstevel@tonic-gate cieRflag = val; 6040Sstevel@tonic-gate break; 6050Sstevel@tonic-gate case 'L': 6060Sstevel@tonic-gate val = data[off + ndx]; 6070Sstevel@tonic-gate ndx += 1; 6081618Srie dbg_print(0, 6090Sstevel@tonic-gate MSG_ORIG(MSG_UNW_CIEAUXLSDA), 6101618Srie val, conv_dwarf_ehe(val)); 6110Sstevel@tonic-gate cieLflag = val; 6120Sstevel@tonic-gate break; 6130Sstevel@tonic-gate default: 6141618Srie dbg_print(0, 6150Sstevel@tonic-gate MSG_ORIG(MSG_UNW_CIEAUXUNEC), 6160Sstevel@tonic-gate cieaugstr[cieaugndx]); 6170Sstevel@tonic-gate break; 6180Sstevel@tonic-gate } 6190Sstevel@tonic-gate } 6201618Srie if ((cielength + 4) > ndx) 6211618Srie unwindtbl(&ndx, cielength, data, off, 6221618Srie MSG_ORIG(MSG_UNW_CIECFI), 6231618Srie MSG_ORIG(MSG_UNW_CIEPRE), 6241618Srie MSG_UNW_CIEPRE_SIZE); 6250Sstevel@tonic-gate off += cielength + 4; 6261618Srie 6270Sstevel@tonic-gate } else { 6280Sstevel@tonic-gate uint_t fdelength = length; 6290Sstevel@tonic-gate int fdecieptr = id; 6300Sstevel@tonic-gate uint64_t fdeinitloc, fdeaddrrange; 6310Sstevel@tonic-gate 6321618Srie dbg_print(0, MSG_ORIG(MSG_UNW_FDE), 6331618Srie EC_XWORD(shdr->sh_addr + off)); 6341618Srie dbg_print(0, MSG_ORIG(MSG_UNW_FDELNGTH), 6350Sstevel@tonic-gate fdelength, fdecieptr); 6361618Srie 6370Sstevel@tonic-gate fdeinitloc = dwarf_ehe_extract(&data[off], 6380Sstevel@tonic-gate &ndx, cieRflag, ehdr->e_ident, 6390Sstevel@tonic-gate shdr->sh_addr + off + ndx); 6400Sstevel@tonic-gate fdeaddrrange = dwarf_ehe_extract(&data[off], 6410Sstevel@tonic-gate &ndx, (cieRflag & ~DW_EH_PE_pcrel), 6420Sstevel@tonic-gate ehdr->e_ident, 6430Sstevel@tonic-gate shdr->sh_addr + off + ndx); 6441618Srie 6451618Srie dbg_print(0, MSG_ORIG(MSG_UNW_FDEINITLOC), 6461618Srie EC_XWORD(fdeinitloc), 6471618Srie EC_XWORD(fdeaddrrange)); 6481618Srie 6490Sstevel@tonic-gate if (cieaugstr[0]) 6501618Srie dbg_print(0, 6511618Srie MSG_ORIG(MSG_UNW_FDEAUXVAL)); 6520Sstevel@tonic-gate if (cieZflag) { 6530Sstevel@tonic-gate uint64_t val; 6540Sstevel@tonic-gate val = uleb_extract(&data[off], &ndx); 6551618Srie dbg_print(0, 6561618Srie MSG_ORIG(MSG_UNW_FDEAUXSIZE), 6571618Srie EC_XWORD(val)); 6580Sstevel@tonic-gate if (val & cieLflag) { 6590Sstevel@tonic-gate fdeinitloc = dwarf_ehe_extract( 6600Sstevel@tonic-gate &data[off], &ndx, cieLflag, 6610Sstevel@tonic-gate ehdr->e_ident, 6620Sstevel@tonic-gate shdr->sh_addr + off + ndx); 6631618Srie dbg_print(0, 6640Sstevel@tonic-gate MSG_ORIG(MSG_UNW_FDEAUXLSDA), 6651618Srie EC_XWORD(val)); 6660Sstevel@tonic-gate } 6670Sstevel@tonic-gate } 6681618Srie if ((fdelength + 4) > ndx) 6691618Srie unwindtbl(&ndx, fdelength, data, off, 6701618Srie MSG_ORIG(MSG_UNW_FDECFI), 6711618Srie MSG_ORIG(MSG_UNW_FDEPRE), 6721618Srie MSG_UNW_FDEPRE_SIZE); 6730Sstevel@tonic-gate off += fdelength + 4; 6740Sstevel@tonic-gate } 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate } 6770Sstevel@tonic-gate } 6780Sstevel@tonic-gate 6790Sstevel@tonic-gate /* 6800Sstevel@tonic-gate * Print the hardware/software capabilities. For executables and shared objects 6810Sstevel@tonic-gate * this should be accompanied with a program header. 6820Sstevel@tonic-gate */ 6830Sstevel@tonic-gate static void 6841618Srie cap(const char *file, Cache *cache, Word shnum, Word phnum, Ehdr *ehdr, 6851618Srie Elf *elf) 6860Sstevel@tonic-gate { 6871618Srie Word cnt; 6883466Srie Shdr *cshdr = 0; 6893466Srie Cache *ccache; 6901618Srie Off cphdr_off = 0; 6911618Srie Xword cphdr_sz; 6920Sstevel@tonic-gate 6930Sstevel@tonic-gate /* 6940Sstevel@tonic-gate * Determine if a hardware/software capabilities header exists. 6950Sstevel@tonic-gate */ 6961618Srie if (phnum) { 6971618Srie Phdr *phdr; 6980Sstevel@tonic-gate 6991618Srie if ((phdr = elf_getphdr(elf)) == NULL) { 7000Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETPHDR)); 7010Sstevel@tonic-gate return; 7020Sstevel@tonic-gate } 7030Sstevel@tonic-gate 7041618Srie for (cnt = 0; cnt < phnum; phdr++, cnt++) { 7051618Srie if (phdr->p_type == PT_SUNWCAP) { 7061618Srie cphdr_off = phdr->p_offset; 7071618Srie cphdr_sz = phdr->p_filesz; 7081618Srie break; 7091618Srie } 7100Sstevel@tonic-gate } 7110Sstevel@tonic-gate } 7120Sstevel@tonic-gate 7130Sstevel@tonic-gate /* 7140Sstevel@tonic-gate * Determine if a hardware/software capabilities section exists. 7150Sstevel@tonic-gate */ 7160Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 7171618Srie Cache *_cache = &cache[cnt]; 7181618Srie Shdr *shdr = _cache->c_shdr; 7190Sstevel@tonic-gate 7200Sstevel@tonic-gate if (shdr->sh_type != SHT_SUNW_cap) 7210Sstevel@tonic-gate continue; 7220Sstevel@tonic-gate 7230Sstevel@tonic-gate if (cphdr_off && ((cphdr_off < shdr->sh_offset) || 7240Sstevel@tonic-gate (cphdr_off + cphdr_sz) > (shdr->sh_offset + shdr->sh_size))) 7250Sstevel@tonic-gate continue; 7260Sstevel@tonic-gate 7273466Srie if (_cache->c_data == NULL) 7283466Srie continue; 7293466Srie 7300Sstevel@tonic-gate ccache = _cache; 7310Sstevel@tonic-gate cshdr = shdr; 7320Sstevel@tonic-gate break; 7330Sstevel@tonic-gate } 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate if ((cshdr == 0) && (cphdr_off == 0)) 7360Sstevel@tonic-gate return; 7370Sstevel@tonic-gate 7383466Srie if ((cshdr->sh_entsize == 0) || (cshdr->sh_size == 0)) { 7393466Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 7403466Srie file, ccache->c_name); 7413466Srie return; 7423466Srie } 7433466Srie 7440Sstevel@tonic-gate /* 7450Sstevel@tonic-gate * Print the hardware/software capabilities section. 7460Sstevel@tonic-gate */ 7470Sstevel@tonic-gate if (cshdr) { 7481618Srie Word ndx, capn; 7493492Sab196087 Cap *cap = (Cap *)ccache->c_data->d_buf; 7500Sstevel@tonic-gate 7511618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 7521618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_CAP), ccache->c_name); 7530Sstevel@tonic-gate 7541618Srie Elf_cap_title(0); 7551618Srie 7561618Srie capn = (Word)(cshdr->sh_size / cshdr->sh_entsize); 7570Sstevel@tonic-gate 7581618Srie for (ndx = 0; ndx < capn; cap++, ndx++) { 7591618Srie if (cap->c_tag != CA_SUNW_NULL) 7601618Srie Elf_cap_entry(0, cap, ndx, ehdr->e_machine); 7610Sstevel@tonic-gate } 7620Sstevel@tonic-gate } else 7630Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP1), file); 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate /* 7660Sstevel@tonic-gate * If this object is an executable or shared object, then the 7670Sstevel@tonic-gate * hardware/software capabilities section should have an accompanying 7680Sstevel@tonic-gate * program header. 7690Sstevel@tonic-gate */ 7700Sstevel@tonic-gate if (cshdr && ((ehdr->e_type == ET_EXEC) || (ehdr->e_type == ET_DYN))) { 7710Sstevel@tonic-gate if (cphdr_off == 0) 7720Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP2), 7730Sstevel@tonic-gate file, ccache->c_name); 7740Sstevel@tonic-gate else if ((cphdr_off != cshdr->sh_offset) || 7750Sstevel@tonic-gate (cphdr_sz != cshdr->sh_size)) 7760Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP3), 7770Sstevel@tonic-gate file, ccache->c_name); 7780Sstevel@tonic-gate } 7790Sstevel@tonic-gate } 7800Sstevel@tonic-gate 7810Sstevel@tonic-gate /* 7820Sstevel@tonic-gate * Print the interpretor. 7830Sstevel@tonic-gate */ 7840Sstevel@tonic-gate static void 7851618Srie interp(const char *file, Cache *cache, Word shnum, Word phnum, Elf *elf) 7860Sstevel@tonic-gate { 7871618Srie Word cnt; 7881618Srie Shdr *ishdr = 0; 7891618Srie Cache *icache; 7901618Srie Off iphdr_off = 0; 7911618Srie Xword iphdr_fsz; 7920Sstevel@tonic-gate 7930Sstevel@tonic-gate /* 7940Sstevel@tonic-gate * Determine if an interp header exists. 7950Sstevel@tonic-gate */ 7961618Srie if (phnum) { 7971618Srie Phdr *phdr; 7980Sstevel@tonic-gate 7991618Srie if ((phdr = getphdr(phnum, PT_INTERP, file, elf)) != 0) { 8001618Srie iphdr_off = phdr->p_offset; 8011618Srie iphdr_fsz = phdr->p_filesz; 8020Sstevel@tonic-gate } 8030Sstevel@tonic-gate } 8040Sstevel@tonic-gate 8050Sstevel@tonic-gate if (iphdr_off == 0) 8060Sstevel@tonic-gate return; 8070Sstevel@tonic-gate 8080Sstevel@tonic-gate /* 8090Sstevel@tonic-gate * Determine if an interp section exists. 8100Sstevel@tonic-gate */ 8110Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 8121618Srie Cache *_cache = &cache[cnt]; 8131618Srie Shdr *shdr = _cache->c_shdr; 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate /* 8160Sstevel@tonic-gate * Scan sections to find a section which contains the PT_INTERP 8170Sstevel@tonic-gate * string. The target section can't be in a NOBITS section. 8180Sstevel@tonic-gate */ 8190Sstevel@tonic-gate if ((shdr->sh_type == SHT_NOBITS) || 8200Sstevel@tonic-gate (iphdr_off < shdr->sh_offset) || 8211618Srie (iphdr_off + iphdr_fsz) > (shdr->sh_offset + shdr->sh_size)) 8220Sstevel@tonic-gate continue; 8230Sstevel@tonic-gate 8240Sstevel@tonic-gate icache = _cache; 8250Sstevel@tonic-gate ishdr = shdr; 8260Sstevel@tonic-gate break; 8270Sstevel@tonic-gate } 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate /* 8300Sstevel@tonic-gate * Print the interpreter string based on the offset defined in the 8310Sstevel@tonic-gate * program header, as this is the offset used by the kernel. 8320Sstevel@tonic-gate */ 8333466Srie if (ishdr && icache->c_data) { 8341618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 8351618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_INTERP), icache->c_name); 8361618Srie dbg_print(0, MSG_ORIG(MSG_FMT_INDENT), 8370Sstevel@tonic-gate (char *)icache->c_data->d_buf + 8380Sstevel@tonic-gate (iphdr_off - ishdr->sh_offset)); 8390Sstevel@tonic-gate } else 8400Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_WARN_INVINTERP1), file); 8410Sstevel@tonic-gate 8420Sstevel@tonic-gate /* 8430Sstevel@tonic-gate * If there are any inconsistences between the program header and 8440Sstevel@tonic-gate * section information, flag them. 8450Sstevel@tonic-gate */ 8460Sstevel@tonic-gate if (ishdr && ((iphdr_off != ishdr->sh_offset) || 8471618Srie (iphdr_fsz != ishdr->sh_size))) { 8480Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_WARN_INVINTERP2), file, 8490Sstevel@tonic-gate icache->c_name); 8500Sstevel@tonic-gate } 8510Sstevel@tonic-gate } 8520Sstevel@tonic-gate 8530Sstevel@tonic-gate /* 8540Sstevel@tonic-gate * Print the syminfo section. 8550Sstevel@tonic-gate */ 8560Sstevel@tonic-gate static void 8571618Srie syminfo(Cache *cache, Word shnum, const char *file) 8580Sstevel@tonic-gate { 8591618Srie Shdr *infoshdr; 8601618Srie Syminfo *info; 8611618Srie Sym *syms; 8621618Srie Dyn *dyns; 8631618Srie Word infonum, cnt, ndx, symnum; 8641618Srie Cache *infocache = 0, *symsec, *strsec; 8650Sstevel@tonic-gate 8660Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 8671618Srie if (cache[cnt].c_shdr->sh_type == SHT_SUNW_syminfo) { 8681618Srie infocache = &cache[cnt]; 8690Sstevel@tonic-gate break; 8700Sstevel@tonic-gate } 8710Sstevel@tonic-gate } 8721618Srie if (infocache == 0) 8730Sstevel@tonic-gate return; 8740Sstevel@tonic-gate 8751618Srie infoshdr = infocache->c_shdr; 8761618Srie if ((infoshdr->sh_entsize == 0) || (infoshdr->sh_size == 0)) { 8770Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 8781618Srie file, infocache->c_name); 8790Sstevel@tonic-gate return; 8800Sstevel@tonic-gate } 8813466Srie if (infocache->c_data == NULL) 8823466Srie return; 8833466Srie 8841618Srie infonum = (Word)(infoshdr->sh_size / infoshdr->sh_entsize); 8851618Srie info = (Syminfo *)infocache->c_data->d_buf; 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate /* 8880Sstevel@tonic-gate * Get the data buffer of the associated dynamic section. 8890Sstevel@tonic-gate */ 8901618Srie if ((infoshdr->sh_info == 0) || (infoshdr->sh_info >= shnum)) { 8910Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO), 8921618Srie file, infocache->c_name, EC_WORD(infoshdr->sh_info)); 8930Sstevel@tonic-gate return; 8940Sstevel@tonic-gate } 8953466Srie if (cache[infoshdr->sh_info].c_data == NULL) 8963466Srie return; 8973466Srie 8981618Srie dyns = cache[infoshdr->sh_info].c_data->d_buf; 8991618Srie if (dyns == 0) { 9000Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 9011618Srie file, cache[infoshdr->sh_info].c_name); 9020Sstevel@tonic-gate return; 9030Sstevel@tonic-gate } 9040Sstevel@tonic-gate 9050Sstevel@tonic-gate /* 9061618Srie * Get the data buffer for the associated symbol table and string table. 9070Sstevel@tonic-gate */ 9081618Srie if (stringtbl(cache, 1, cnt, shnum, file, 9091618Srie &symnum, &symsec, &strsec) == 0) 9100Sstevel@tonic-gate return; 9110Sstevel@tonic-gate 9121618Srie syms = symsec->c_data->d_buf; 9130Sstevel@tonic-gate 9141618Srie /* 9151618Srie * Loop through the syminfo entries. 9161618Srie */ 9171618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 9181618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMINFO), infocache->c_name); 9191618Srie Elf_syminfo_title(0); 9200Sstevel@tonic-gate 9211618Srie for (ndx = 1, info++; ndx < infonum; ndx++, info++) { 9221618Srie Sym *sym; 9231618Srie const char *needed = 0, *name; 9241618Srie 9251618Srie if ((info->si_flags == 0) && (info->si_boundto == 0)) 9260Sstevel@tonic-gate continue; 9270Sstevel@tonic-gate 9281618Srie sym = &syms[ndx]; 9291618Srie name = string(infocache, ndx, strsec, file, sym->st_name); 9300Sstevel@tonic-gate 9311618Srie if (info->si_boundto < SYMINFO_BT_LOWRESERVE) { 9321618Srie Dyn *dyn = &dyns[info->si_boundto]; 9331618Srie 9341618Srie needed = string(infocache, info->si_boundto, 9351618Srie strsec, file, dyn->d_un.d_val); 9360Sstevel@tonic-gate } 9371618Srie Elf_syminfo_entry(0, ndx, info, name, needed); 9380Sstevel@tonic-gate } 9390Sstevel@tonic-gate } 9400Sstevel@tonic-gate 9410Sstevel@tonic-gate /* 9420Sstevel@tonic-gate * Print version definition section entries. 9430Sstevel@tonic-gate */ 9440Sstevel@tonic-gate static void 9451618Srie version_def(Verdef *vdf, Word shnum, Cache *vcache, Cache *scache, 9460Sstevel@tonic-gate const char *file) 9470Sstevel@tonic-gate { 9481618Srie Word cnt; 9491618Srie char index[MAXNDXSIZE]; 9500Sstevel@tonic-gate 9511618Srie Elf_ver_def_title(0); 9520Sstevel@tonic-gate 9530Sstevel@tonic-gate for (cnt = 1; cnt <= shnum; cnt++, 9541618Srie vdf = (Verdef *)((uintptr_t)vdf + vdf->vd_next)) { 9550Sstevel@tonic-gate const char *name, *dep; 9561618Srie Half vcnt = vdf->vd_cnt - 1; 9571618Srie Half ndx = vdf->vd_ndx; 9581618Srie Verdaux *vdap = (Verdaux *)((uintptr_t)vdf + 9591618Srie 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; 10071618Srie Vernaux *vnap = (Vernaux *)((uintptr_t)vnd + 10081618Srie vnd->vn_aux); 10090Sstevel@tonic-gate 10100Sstevel@tonic-gate /* 10110Sstevel@tonic-gate * Obtain the name of the needed file and the version name 10120Sstevel@tonic-gate * within it that we're dependent on. Note that the count 10130Sstevel@tonic-gate * should be at least one, otherwise this is a pretty bogus 10140Sstevel@tonic-gate * entry. 10150Sstevel@tonic-gate */ 10160Sstevel@tonic-gate name = string(vcache, cnt, scache, file, vnd->vn_file); 10170Sstevel@tonic-gate if (vcnt) 10180Sstevel@tonic-gate dep = string(vcache, cnt, scache, file, vnap->vna_name); 10190Sstevel@tonic-gate else 10200Sstevel@tonic-gate dep = MSG_INTL(MSG_STR_NULL); 10210Sstevel@tonic-gate 10221618Srie Elf_ver_line_1(0, MSG_ORIG(MSG_STR_EMPTY), name, dep, 10231618Srie conv_ver_flags(vnap->vna_flags)); 10240Sstevel@tonic-gate 10250Sstevel@tonic-gate /* 10260Sstevel@tonic-gate * Print any additional version dependencies. 10270Sstevel@tonic-gate */ 10280Sstevel@tonic-gate if (vcnt) { 10291618Srie vnap = (Vernaux *)((uintptr_t)vnap + vnap->vna_next); 10300Sstevel@tonic-gate for (vcnt--; vcnt; vcnt--, 10311618Srie vnap = (Vernaux *)((uintptr_t)vnap + 10320Sstevel@tonic-gate vnap->vna_next)) { 10330Sstevel@tonic-gate dep = string(vcache, cnt, scache, file, 10340Sstevel@tonic-gate vnap->vna_name); 10351618Srie Elf_ver_line_3(0, MSG_ORIG(MSG_STR_EMPTY), dep, 10361618Srie conv_ver_flags(vnap->vna_flags)); 10370Sstevel@tonic-gate } 10380Sstevel@tonic-gate } 10390Sstevel@tonic-gate } 10400Sstevel@tonic-gate } 10410Sstevel@tonic-gate 10420Sstevel@tonic-gate /* 10433875Sab196087 * Display version section information if the flags require it. 10443875Sab196087 * Return version information needed by other output. 10453875Sab196087 * 10463875Sab196087 * entry: 10473875Sab196087 * cache - Cache of all section headers 10483875Sab196087 * shnum - # of sections in cache 10493875Sab196087 * file - Name of file 10503875Sab196087 * flags - Command line option flags 10513875Sab196087 * versym - VERSYM_STATE block to be filled in. 10520Sstevel@tonic-gate */ 10533875Sab196087 static void 10543875Sab196087 versions(Cache *cache, Word shnum, const char *file, uint_t flags, 10553875Sab196087 VERSYM_STATE *versym) 10560Sstevel@tonic-gate { 10570Sstevel@tonic-gate GElf_Word cnt; 10583875Sab196087 const char *gnu_prefix; 10593875Sab196087 size_t gnu_prefix_len; 10603875Sab196087 10613875Sab196087 bzero(versym, sizeof (*versym)); 10623875Sab196087 gnu_prefix = MSG_ORIG(MSG_GNU_VERNAMPREFIX); 10633875Sab196087 gnu_prefix_len = strlen(gnu_prefix); 10640Sstevel@tonic-gate 10650Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 10661618Srie void *ver; 10670Sstevel@tonic-gate uint_t num; 10681618Srie Cache *_cache = &cache[cnt]; 10691618Srie Shdr *shdr = _cache->c_shdr; 10701618Srie const char *secname = _cache->c_name; 10710Sstevel@tonic-gate 10720Sstevel@tonic-gate /* 10733875Sab196087 * If the section names starts with the .gnu.version prefix, 10743875Sab196087 * then this object was almost certainly produced by the 10753875Sab196087 * GNU ld and not the native Solaris ld. 10760Sstevel@tonic-gate */ 10773875Sab196087 if (strncmp(gnu_prefix, secname, gnu_prefix_len) == 0) 10783875Sab196087 versym->gnu = 1; 10793875Sab196087 10803875Sab196087 /* 10813875Sab196087 * If this is the version symbol table record its data 10823875Sab196087 * address for later symbol processing. 10833875Sab196087 */ 10843875Sab196087 if ((shdr->sh_type == SHT_SUNW_versym) && 10853875Sab196087 (_cache->c_data != NULL)) { 10863875Sab196087 versym->cache = _cache; 10873875Sab196087 versym->data = _cache->c_data->d_buf; 10880Sstevel@tonic-gate continue; 10890Sstevel@tonic-gate } 10900Sstevel@tonic-gate 10913875Sab196087 /* 10923875Sab196087 * If this is a version definition section, retain # of 10933875Sab196087 * version definitions for later symbol processing. 10943875Sab196087 */ 10953875Sab196087 if (shdr->sh_type == SHT_SUNW_verdef) 10963875Sab196087 versym->num_verdef = shdr->sh_info; 10973875Sab196087 10980Sstevel@tonic-gate if ((flags & FLG_VERSIONS) == 0) 10990Sstevel@tonic-gate continue; 11000Sstevel@tonic-gate 11010Sstevel@tonic-gate if ((shdr->sh_type != SHT_SUNW_verdef) && 11020Sstevel@tonic-gate (shdr->sh_type != SHT_SUNW_verneed)) 11030Sstevel@tonic-gate continue; 11040Sstevel@tonic-gate 11050Sstevel@tonic-gate /* 11060Sstevel@tonic-gate * Determine the version section data and number. 11070Sstevel@tonic-gate */ 11083466Srie if ((_cache->c_data == NULL) || 11093466Srie ((ver = (void *)_cache->c_data->d_buf) == NULL)) { 11100Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 11111618Srie file, secname); 11120Sstevel@tonic-gate continue; 11130Sstevel@tonic-gate } 11140Sstevel@tonic-gate if ((num = shdr->sh_info) == 0) { 11150Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO), 11161618Srie file, secname, EC_WORD(shdr->sh_info)); 11170Sstevel@tonic-gate continue; 11180Sstevel@tonic-gate } 11190Sstevel@tonic-gate 11200Sstevel@tonic-gate /* 11210Sstevel@tonic-gate * Get the data buffer for the associated string table. 11220Sstevel@tonic-gate */ 11230Sstevel@tonic-gate if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) { 11240Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK), 11251618Srie file, secname, EC_WORD(shdr->sh_link)); 11260Sstevel@tonic-gate continue; 11270Sstevel@tonic-gate } 11280Sstevel@tonic-gate 11291618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 11300Sstevel@tonic-gate if (shdr->sh_type == SHT_SUNW_verdef) { 11311618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_VERDEF), secname); 11321618Srie version_def((Verdef *)ver, num, _cache, 11330Sstevel@tonic-gate &cache[shdr->sh_link], file); 11340Sstevel@tonic-gate } else if (shdr->sh_type == SHT_SUNW_verneed) { 11351618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_VERNEED), secname); 11361618Srie version_need((Verneed *)ver, num, _cache, 11370Sstevel@tonic-gate &cache[shdr->sh_link], file); 11380Sstevel@tonic-gate } 11390Sstevel@tonic-gate } 11400Sstevel@tonic-gate } 11410Sstevel@tonic-gate 11420Sstevel@tonic-gate /* 11433492Sab196087 * Initialize a symbol table state structure 11443492Sab196087 * 11453492Sab196087 * entry: 11463492Sab196087 * state - State structure to be initialized 11473492Sab196087 * cache - Cache of all section headers 11483492Sab196087 * shnum - # of sections in cache 11493492Sab196087 * secndx - Index of symbol table section 11503492Sab196087 * ehdr - ELF header for file 11513875Sab196087 * versym - Information about versym section 11523492Sab196087 * file - Name of file 11533492Sab196087 * flags - Command line option flags 11541618Srie */ 11551618Srie static int 11563492Sab196087 init_symtbl_state(SYMTBL_STATE *state, Cache *cache, Word shnum, Word secndx, 11573875Sab196087 Ehdr *ehdr, VERSYM_STATE *versym, const char *file, uint_t flags) 11583492Sab196087 { 11593492Sab196087 Shdr *shdr; 11603492Sab196087 11613492Sab196087 state->file = file; 11623492Sab196087 state->ehdr = ehdr; 11633492Sab196087 state->cache = cache; 11643492Sab196087 state->shnum = shnum; 11653492Sab196087 state->seccache = &cache[secndx]; 11663492Sab196087 state->secndx = secndx; 11673492Sab196087 state->secname = state->seccache->c_name; 11683492Sab196087 state->flags = flags; 11693492Sab196087 state->shxndx.checked = 0; 11703492Sab196087 state->shxndx.data = NULL; 11713492Sab196087 state->shxndx.n = 0; 11723492Sab196087 11733492Sab196087 shdr = state->seccache->c_shdr; 11743492Sab196087 11753492Sab196087 /* 11763492Sab196087 * Check the symbol data and per-item size. 11773492Sab196087 */ 11783492Sab196087 if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) { 11793492Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 11803492Sab196087 file, state->secname); 11813492Sab196087 return (0); 11823492Sab196087 } 11833492Sab196087 if (state->seccache->c_data == NULL) 11843492Sab196087 return (0); 11853492Sab196087 11863492Sab196087 /* LINTED */ 11873492Sab196087 state->symn = (Word)(shdr->sh_size / shdr->sh_entsize); 11883492Sab196087 state->sym = (Sym *)state->seccache->c_data->d_buf; 11893492Sab196087 11903492Sab196087 /* 11913492Sab196087 * Check associated string table section. 11923492Sab196087 */ 11933492Sab196087 if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) { 11943492Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK), 11953492Sab196087 file, state->secname, EC_WORD(shdr->sh_link)); 11963492Sab196087 return (0); 11973492Sab196087 } 11983492Sab196087 11993492Sab196087 /* 12003492Sab196087 * Determine if there is a associated Versym section 12013492Sab196087 * with this Symbol Table. 12023492Sab196087 */ 12033875Sab196087 if (versym->cache && 12043875Sab196087 (versym->cache->c_shdr->sh_link == state->secndx)) 12053875Sab196087 state->versym = versym; 12063492Sab196087 else 12073492Sab196087 state->versym = NULL; 12083492Sab196087 12093492Sab196087 12103492Sab196087 return (1); 12113492Sab196087 } 12123492Sab196087 12133492Sab196087 /* 12143492Sab196087 * Determine the extended section index used for symbol tables entries. 12153492Sab196087 */ 12163492Sab196087 static void 12173492Sab196087 symbols_getxindex(SYMTBL_STATE * state) 12181618Srie { 12191618Srie uint_t symn; 12201618Srie Word symcnt; 12211618Srie 12223492Sab196087 state->shxndx.checked = 1; /* Note that we've been called */ 12233492Sab196087 for (symcnt = 1; symcnt < state->shnum; symcnt++) { 12243492Sab196087 Cache *_cache = &state->cache[symcnt]; 12251618Srie Shdr *shdr = _cache->c_shdr; 12261618Srie 12271618Srie if ((shdr->sh_type != SHT_SYMTAB_SHNDX) || 12283492Sab196087 (shdr->sh_link != state->secndx)) 12291618Srie continue; 12301618Srie 12311618Srie if ((shdr->sh_entsize) && 12321618Srie /* LINTED */ 12331618Srie ((symn = (uint_t)(shdr->sh_size / shdr->sh_entsize)) == 0)) 12341618Srie continue; 12351618Srie 12363466Srie if (_cache->c_data == NULL) 12373466Srie continue; 12383466Srie 12393492Sab196087 state->shxndx.data = _cache->c_data->d_buf; 12403492Sab196087 state->shxndx.n = symn; 12413492Sab196087 return; 12421618Srie } 12431618Srie } 12441618Srie 12451618Srie /* 12463492Sab196087 * Produce a line of output for the given symbol 12473492Sab196087 * 12483492Sab196087 * entry: 12493875Sab196087 * state - Symbol table state 12503492Sab196087 * symndx - Index of symbol within the table 12513492Sab196087 * symndx_disp - Index to display. This may not be the same 12523492Sab196087 * as symndx if the display is relative to the logical 12533492Sab196087 * combination of the SUNW_ldynsym/dynsym tables. 12543492Sab196087 * sym - Symbol to display 12550Sstevel@tonic-gate */ 12563492Sab196087 static void 12573492Sab196087 output_symbol(SYMTBL_STATE *state, Word symndx, Word disp_symndx, Sym *sym) 12580Sstevel@tonic-gate { 12593118Sab196087 /* 12603118Sab196087 * Symbol types for which we check that the specified 12613118Sab196087 * address/size land inside the target section. 12623118Sab196087 */ 12633492Sab196087 static const int addr_symtype[STT_NUM] = { 12643118Sab196087 0, /* STT_NOTYPE */ 12653118Sab196087 1, /* STT_OBJECT */ 12663118Sab196087 1, /* STT_FUNC */ 12673118Sab196087 0, /* STT_SECTION */ 12683118Sab196087 0, /* STT_FILE */ 12693118Sab196087 1, /* STT_COMMON */ 12703118Sab196087 0, /* STT_TLS */ 12713118Sab196087 }; 12723118Sab196087 #if STT_NUM != (STT_TLS + 1) 12733492Sab196087 #error "STT_NUM has grown. Update addr_symtype[]" 12743118Sab196087 #endif 12753118Sab196087 12763492Sab196087 char index[MAXNDXSIZE], *sec; 12773492Sab196087 const char *symname; 12783875Sab196087 Versym verndx; 12793492Sab196087 uchar_t type; 12803492Sab196087 Shdr *tshdr; 12813492Sab196087 Word shndx; 12823492Sab196087 12833492Sab196087 /* Ensure symbol index is in range */ 12843492Sab196087 if (symndx >= state->symn) { 12853492Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSORTNDX), 12863492Sab196087 state->file, state->secname, EC_WORD(symndx)); 12873492Sab196087 return; 12883492Sab196087 } 12893492Sab196087 12903492Sab196087 /* 12913492Sab196087 * If we are using extended symbol indexes, find the 12923492Sab196087 * corresponding SHN_SYMTAB_SHNDX table. 12933492Sab196087 */ 12943492Sab196087 if ((sym->st_shndx == SHN_XINDEX) && (state->shxndx.checked == 0)) 12953492Sab196087 symbols_getxindex(state); 12963492Sab196087 12973492Sab196087 /* LINTED */ 12983492Sab196087 symname = string(state->seccache, symndx, 12993492Sab196087 &state->cache[state->seccache->c_shdr->sh_link], state->file, 13003492Sab196087 sym->st_name); 13013492Sab196087 13023492Sab196087 tshdr = 0; 13033492Sab196087 sec = NULL; 13043492Sab196087 13053492Sab196087 if ((state->ehdr->e_type == ET_CORE)) 13063492Sab196087 sec = (char *)MSG_INTL(MSG_STR_UNKNOWN); 13073492Sab196087 else if ((sym->st_shndx < SHN_LORESERVE) && 13083492Sab196087 (sym->st_shndx < state->shnum)) { 13093492Sab196087 shndx = sym->st_shndx; 13103492Sab196087 tshdr = state->cache[shndx].c_shdr; 13113492Sab196087 sec = state->cache[shndx].c_name; 13123492Sab196087 } else if (sym->st_shndx == SHN_XINDEX) { 13133492Sab196087 if (state->shxndx.data) { 13143492Sab196087 Word _shxndx; 13153492Sab196087 13163492Sab196087 if (symndx > state->shxndx.n) { 13173492Sab196087 (void) fprintf(stderr, 13183492Sab196087 MSG_INTL(MSG_ERR_BADSYMXINDEX1), 13193492Sab196087 state->file, state->secname, EC_WORD(symndx)); 13203492Sab196087 } else if ((_shxndx = 13213492Sab196087 state->shxndx.data[symndx]) > state->shnum) { 13223492Sab196087 (void) fprintf(stderr, 13233492Sab196087 MSG_INTL(MSG_ERR_BADSYMXINDEX2), 13243492Sab196087 state->file, state->secname, EC_WORD(symndx), 13253492Sab196087 EC_WORD(_shxndx)); 13263492Sab196087 } else { 13273492Sab196087 shndx = _shxndx; 13283492Sab196087 tshdr = state->cache[shndx].c_shdr; 13293492Sab196087 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 */ 13653875Sab196087 if (VERNDX_INVALID(sym->st_shndx, state->versym->num_verdef, 13663875Sab196087 state->versym->data, verndx)) { 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 14453492Sab196087 symbols(Cache *cache, Word shnum, Ehdr *ehdr, const char *name, 14463875Sab196087 VERSYM_STATE *versym, 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; 14633492Sab196087 if (name && strcmp(name, _cache->c_name)) 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 14873492Sab196087 sunw_sort(Cache *cache, Word shnum, Ehdr *ehdr, const char *name, 14883875Sab196087 VERSYM_STATE *versym, 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; 15073492Sab196087 if (name && strcmp(name, sortcache->c_name)) 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 16201618Srie reloc(Cache *cache, Word shnum, Ehdr *ehdr, const char *name, 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; 16381618Srie if (name && strcmp(name, relname)) 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)) && 18033850Sab196087 ((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 18601618Srie move(Cache *cache, Word shnum, const char *name, 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; 18750Sstevel@tonic-gate if (name && strcmp(name, _cache->c_name)) 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 21161618Srie note(Cache *cache, Word shnum, const char *name, 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; 21290Sstevel@tonic-gate if (name && strcmp(name, _cache->c_name)) 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 22021618Srie hash(Cache *cache, Word shnum, const char *name, 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; 22191618Srie if (name && strcmp(name, hsecname)) 22200Sstevel@tonic-gate continue; 22210Sstevel@tonic-gate 22220Sstevel@tonic-gate /* 22230Sstevel@tonic-gate * Determine the hash table data and size. 22240Sstevel@tonic-gate */ 22251618Srie if ((hshdr->sh_entsize == 0) || (hshdr->sh_size == 0)) { 22260Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 22271618Srie file, hsecname); 22280Sstevel@tonic-gate continue; 22290Sstevel@tonic-gate } 22303466Srie if (_cache->c_data == NULL) 22313466Srie continue; 22323466Srie 22330Sstevel@tonic-gate hash = (uint_t *)_cache->c_data->d_buf; 22340Sstevel@tonic-gate bkts = *hash; 22350Sstevel@tonic-gate chain = hash + 2 + bkts; 22360Sstevel@tonic-gate hash += 2; 22370Sstevel@tonic-gate 22380Sstevel@tonic-gate /* 22390Sstevel@tonic-gate * Get the data buffer for the associated symbol table. 22400Sstevel@tonic-gate */ 22411618Srie if ((hshdr->sh_link == 0) || (hshdr->sh_link >= shnum)) { 22420Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK), 22431618Srie file, hsecname, EC_WORD(hshdr->sh_link)); 22440Sstevel@tonic-gate continue; 22450Sstevel@tonic-gate } 22461618Srie 22471618Srie _cache = &cache[hshdr->sh_link]; 22481618Srie ssecname = _cache->c_name; 22491618Srie 22503466Srie if (_cache->c_data == NULL) 22513466Srie continue; 22523466Srie 22533466Srie if ((syms = (Sym *)_cache->c_data->d_buf) == NULL) { 22540Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 22551618Srie file, ssecname); 22560Sstevel@tonic-gate continue; 22570Sstevel@tonic-gate } 22580Sstevel@tonic-gate 22591618Srie sshdr = _cache->c_shdr; 22601618Srie /* LINTED */ 22611618Srie symn = (Word)(sshdr->sh_size / sshdr->sh_entsize); 22621618Srie 22630Sstevel@tonic-gate /* 22640Sstevel@tonic-gate * Get the associated string table section. 22650Sstevel@tonic-gate */ 22661618Srie if ((sshdr->sh_link == 0) || (sshdr->sh_link >= shnum)) { 22670Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK), 22681618Srie file, ssecname, EC_WORD(sshdr->sh_link)); 22690Sstevel@tonic-gate continue; 22700Sstevel@tonic-gate } 22710Sstevel@tonic-gate 22721618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 22731618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_HASH), hsecname); 22741618Srie dbg_print(0, MSG_INTL(MSG_ELF_HASH_INFO)); 22750Sstevel@tonic-gate 22760Sstevel@tonic-gate /* 22770Sstevel@tonic-gate * Loop through the hash buckets, printing the appropriate 22780Sstevel@tonic-gate * symbols. 22790Sstevel@tonic-gate */ 22800Sstevel@tonic-gate for (ndx = 0; ndx < bkts; ndx++, hash++) { 22811618Srie Word _ndx, _cnt; 22820Sstevel@tonic-gate 22830Sstevel@tonic-gate if (*hash == 0) { 22840Sstevel@tonic-gate count[0]++; 22850Sstevel@tonic-gate continue; 22860Sstevel@tonic-gate } 22870Sstevel@tonic-gate 22881618Srie hash_entry(_cache, &cache[sshdr->sh_link], hsecname, 22891618Srie ndx, *hash, symn, syms, file, bkts, flags, 0); 22900Sstevel@tonic-gate 22910Sstevel@tonic-gate /* 22920Sstevel@tonic-gate * Determine if any other symbols are chained to this 22930Sstevel@tonic-gate * bucket. 22940Sstevel@tonic-gate */ 22950Sstevel@tonic-gate _ndx = chain[*hash]; 22960Sstevel@tonic-gate _cnt = 1; 22970Sstevel@tonic-gate while (_ndx) { 22981618Srie hash_entry(_cache, &cache[sshdr->sh_link], 22991618Srie hsecname, ndx, _ndx, symn, syms, file, 23001618Srie bkts, flags, 1); 23010Sstevel@tonic-gate _ndx = chain[_ndx]; 23020Sstevel@tonic-gate _cnt++; 23030Sstevel@tonic-gate } 23040Sstevel@tonic-gate 23050Sstevel@tonic-gate if (_cnt >= MAXCOUNT) { 23060Sstevel@tonic-gate (void) fprintf(stderr, 23071324Srie MSG_INTL(MSG_HASH_OVERFLW), file, 23081618Srie _cache->c_name, EC_WORD(ndx), 23091618Srie EC_WORD(_cnt)); 23100Sstevel@tonic-gate } else 23110Sstevel@tonic-gate count[_cnt]++; 23120Sstevel@tonic-gate } 23130Sstevel@tonic-gate break; 23140Sstevel@tonic-gate } 23150Sstevel@tonic-gate 23160Sstevel@tonic-gate /* 23170Sstevel@tonic-gate * Print out the count information. 23180Sstevel@tonic-gate */ 23190Sstevel@tonic-gate bkts = cnt = 0; 23201618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 23211618Srie 23220Sstevel@tonic-gate for (ndx = 0; ndx < MAXCOUNT; ndx++) { 23231618Srie Word _cnt; 23240Sstevel@tonic-gate 23250Sstevel@tonic-gate if ((_cnt = count[ndx]) == 0) 23260Sstevel@tonic-gate continue; 23270Sstevel@tonic-gate 23281618Srie (void) snprintf(number, MAXNDXSIZE, 23291618Srie MSG_ORIG(MSG_FMT_INTEGER), _cnt); 23301618Srie dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS1), number, 23311618Srie EC_WORD(ndx)); 23320Sstevel@tonic-gate bkts += _cnt; 23331618Srie cnt += (Word)(ndx * _cnt); 23340Sstevel@tonic-gate } 23350Sstevel@tonic-gate if (cnt) { 23360Sstevel@tonic-gate (void) snprintf(number, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER), 23371618Srie bkts); 23381618Srie dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS2), number, 23391618Srie EC_WORD(cnt)); 23400Sstevel@tonic-gate } 23410Sstevel@tonic-gate } 23420Sstevel@tonic-gate 23430Sstevel@tonic-gate static void 23441618Srie group(Cache *cache, Word shnum, const char *name, const char *file, 23451618Srie uint_t flags) 23460Sstevel@tonic-gate { 23471618Srie Word scnt; 23480Sstevel@tonic-gate 23491618Srie for (scnt = 1; scnt < shnum; scnt++) { 23501618Srie Cache *_cache = &cache[scnt]; 23511618Srie Shdr *shdr = _cache->c_shdr; 23521618Srie Word *grpdata, gcnt, grpcnt, symnum, unknown; 23531618Srie Cache *symsec, *strsec; 23541618Srie Sym *syms, *sym; 23551618Srie char flgstrbuf[MSG_GRP_COMDAT_SIZE + 10]; 23560Sstevel@tonic-gate 23570Sstevel@tonic-gate if (shdr->sh_type != SHT_GROUP) 23580Sstevel@tonic-gate continue; 23590Sstevel@tonic-gate if (name && strcmp(name, _cache->c_name)) 23600Sstevel@tonic-gate continue; 23613466Srie if ((_cache->c_data == NULL) || 23623466Srie ((grpdata = (Word *)_cache->c_data->d_buf) == NULL)) 23630Sstevel@tonic-gate continue; 23641618Srie grpcnt = shdr->sh_size / sizeof (Word); 23651618Srie 23661618Srie /* 23671618Srie * Get the data buffer for the associated symbol table and 23681618Srie * string table. 23691618Srie */ 23701618Srie if (stringtbl(cache, 1, scnt, shnum, file, 23711618Srie &symnum, &symsec, &strsec) == 0) 23721618Srie return; 23731618Srie 23741618Srie syms = symsec->c_data->d_buf; 23751618Srie 23761618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 23771618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_GRP), _cache->c_name); 23781618Srie dbg_print(0, MSG_INTL(MSG_GRP_TITLE)); 23791618Srie 23801618Srie /* 23811618Srie * The first element of the group defines the group. The 23821618Srie * associated symbol is defined by the sh_link field. 23831618Srie */ 23841618Srie if ((shdr->sh_info == SHN_UNDEF) || (shdr->sh_info > symnum)) { 23851618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO), 23861618Srie file, _cache->c_name, EC_WORD(shdr->sh_info)); 23871618Srie return; 23880Sstevel@tonic-gate } 23890Sstevel@tonic-gate 23901618Srie (void) strcpy(flgstrbuf, MSG_ORIG(MSG_STR_OSQBRKT)); 23911618Srie if (grpdata[0] & GRP_COMDAT) { 23921618Srie (void) strcat(flgstrbuf, MSG_ORIG(MSG_GRP_COMDAT)); 23930Sstevel@tonic-gate } 23941618Srie if ((unknown = (grpdata[0] & ~GRP_COMDAT)) != 0) { 23951618Srie size_t len = strlen(flgstrbuf); 23961618Srie 23971618Srie (void) snprintf(&flgstrbuf[len], 23981618Srie (MSG_GRP_COMDAT_SIZE + 10 - len), 23991618Srie MSG_ORIG(MSG_GRP_UNKNOWN), unknown); 24000Sstevel@tonic-gate } 24011618Srie (void) strcat(flgstrbuf, MSG_ORIG(MSG_STR_CSQBRKT)); 24021618Srie sym = (Sym *)(syms + shdr->sh_info); 24030Sstevel@tonic-gate 24041618Srie dbg_print(0, MSG_INTL(MSG_GRP_SIGNATURE), flgstrbuf, 24051618Srie demangle(string(_cache, 0, strsec, file, sym->st_name), 24061618Srie flags)); 24071618Srie 24081618Srie for (gcnt = 1; gcnt < grpcnt; gcnt++) { 24090Sstevel@tonic-gate char index[MAXNDXSIZE]; 24101618Srie const char *name; 24110Sstevel@tonic-gate 24120Sstevel@tonic-gate (void) snprintf(index, MAXNDXSIZE, 24131618Srie MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(gcnt)); 24141618Srie 24151618Srie if (grpdata[gcnt] >= shnum) 24161618Srie name = MSG_INTL(MSG_GRP_INVALSCN); 24171618Srie else 24181618Srie name = cache[grpdata[gcnt]].c_name; 24191618Srie 24201618Srie (void) printf(MSG_ORIG(MSG_GRP_ENTRY), index, name, 24211618Srie EC_XWORD(grpdata[gcnt])); 24220Sstevel@tonic-gate } 24230Sstevel@tonic-gate } 24240Sstevel@tonic-gate } 24250Sstevel@tonic-gate 24260Sstevel@tonic-gate static void 24271618Srie got(Cache *cache, Word shnum, Ehdr *ehdr, const char *file, uint_t flags) 24280Sstevel@tonic-gate { 24290Sstevel@tonic-gate Cache *gotcache = 0, *symtab = 0, *_cache; 24301618Srie Addr gotbgn, gotend; 24311618Srie Shdr *gotshdr; 24321618Srie Word cnt, gotents, gotndx; 24330Sstevel@tonic-gate size_t gentsize; 24340Sstevel@tonic-gate Got_info *gottable; 24350Sstevel@tonic-gate char *gotdata; 24361618Srie Sym *gotsym; 24371618Srie Xword gotsymaddr; 24380Sstevel@tonic-gate 24390Sstevel@tonic-gate /* 24401324Srie * First, find the got. 24410Sstevel@tonic-gate */ 24420Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 24430Sstevel@tonic-gate _cache = &cache[cnt]; 24441324Srie if (strncmp(_cache->c_name, MSG_ORIG(MSG_ELF_GOT), 24451324Srie MSG_ELF_GOT_SIZE) == 0) { 24460Sstevel@tonic-gate gotcache = _cache; 24470Sstevel@tonic-gate break; 24480Sstevel@tonic-gate } 24490Sstevel@tonic-gate } 24501618Srie if (gotcache == 0) 24510Sstevel@tonic-gate return; 24521324Srie 24531324Srie /* 24541324Srie * A got section within a relocatable object is suspicious. 24551324Srie */ 24561324Srie if (ehdr->e_type == ET_REL) { 24571324Srie (void) fprintf(stderr, MSG_INTL(MSG_GOT_UNEXPECTED), file, 24581324Srie _cache->c_name); 24591324Srie } 24601324Srie 24611618Srie gotshdr = gotcache->c_shdr; 24620Sstevel@tonic-gate if (gotshdr->sh_size == 0) { 24630Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 24640Sstevel@tonic-gate file, gotcache->c_name); 24650Sstevel@tonic-gate return; 24660Sstevel@tonic-gate } 24671618Srie 24681618Srie gotbgn = gotshdr->sh_addr; 24690Sstevel@tonic-gate gotend = gotbgn + gotshdr->sh_size; 24700Sstevel@tonic-gate 24710Sstevel@tonic-gate /* 24721618Srie * Some architectures don't properly set the sh_entsize for the GOT 24731618Srie * table. If it's not set, default to a size of a pointer. 24740Sstevel@tonic-gate */ 24751618Srie if ((gentsize = gotshdr->sh_entsize) == 0) 24761618Srie gentsize = sizeof (Xword); 24771618Srie 24783466Srie if (gotcache->c_data == NULL) 24793466Srie return; 24803466Srie 24810Sstevel@tonic-gate /* LINTED */ 24821618Srie gotents = (Word)(gotshdr->sh_size / gentsize); 24830Sstevel@tonic-gate gotdata = gotcache->c_data->d_buf; 24840Sstevel@tonic-gate 24850Sstevel@tonic-gate if ((gottable = calloc(gotents, sizeof (Got_info))) == 0) { 24860Sstevel@tonic-gate int err = errno; 24871618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC), file, 24881618Srie strerror(err)); 24890Sstevel@tonic-gate return; 24900Sstevel@tonic-gate } 24910Sstevel@tonic-gate 24920Sstevel@tonic-gate /* 24930Sstevel@tonic-gate * Now we scan through all the sections looking for any relocations 24940Sstevel@tonic-gate * that may be against the GOT. Since these may not be isolated to a 24950Sstevel@tonic-gate * .rel[a].got section we check them all. 24960Sstevel@tonic-gate * While scanning sections save the symbol table entry (a symtab 24970Sstevel@tonic-gate * overriding a dynsym) so that we can lookup _GLOBAL_OFFSET_TABLE_. 24980Sstevel@tonic-gate */ 24990Sstevel@tonic-gate for (cnt = 1; cnt < shnum; cnt++) { 25001618Srie Word type, symnum; 25011618Srie Xword relndx, relnum, relsize; 25021618Srie void *rels; 25031618Srie Sym *syms; 25041618Srie Cache *symsec, *strsec; 25051618Srie Cache *_cache = &cache[cnt]; 25061618Srie Shdr *shdr; 25070Sstevel@tonic-gate 25081618Srie shdr = _cache->c_shdr; 25091618Srie type = shdr->sh_type; 25100Sstevel@tonic-gate 25111618Srie if ((symtab == 0) && (type == SHT_DYNSYM)) { 25120Sstevel@tonic-gate symtab = _cache; 25130Sstevel@tonic-gate continue; 25140Sstevel@tonic-gate } 25151618Srie if (type == SHT_SYMTAB) { 25160Sstevel@tonic-gate symtab = _cache; 25170Sstevel@tonic-gate continue; 25180Sstevel@tonic-gate } 25191618Srie if ((type != SHT_RELA) && (type != SHT_REL)) 25200Sstevel@tonic-gate continue; 25210Sstevel@tonic-gate 25220Sstevel@tonic-gate /* 25231618Srie * Decide entry size. 25240Sstevel@tonic-gate */ 25251618Srie if (((relsize = shdr->sh_entsize) == 0) || 25261618Srie (relsize > shdr->sh_size)) { 25271618Srie if (type == SHT_RELA) 25281618Srie relsize = sizeof (Rela); 25291618Srie else 25301618Srie relsize = sizeof (Rel); 25310Sstevel@tonic-gate } 25320Sstevel@tonic-gate 25330Sstevel@tonic-gate /* 25341618Srie * Determine the number of relocations available. 25350Sstevel@tonic-gate */ 25361618Srie if (shdr->sh_size == 0) { 25371618Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ), 25381618Srie file, _cache->c_name); 25390Sstevel@tonic-gate continue; 25400Sstevel@tonic-gate } 25413466Srie if (_cache->c_data == NULL) 25423466Srie continue; 25433466Srie 25441618Srie rels = _cache->c_data->d_buf; 25451618Srie relnum = shdr->sh_size / relsize; 25460Sstevel@tonic-gate 25471618Srie /* 25481618Srie * Get the data buffer for the associated symbol table and 25491618Srie * string table. 25501618Srie */ 25511618Srie if (stringtbl(cache, 1, cnt, shnum, file, 25521618Srie &symnum, &symsec, &strsec) == 0) 25531618Srie continue; 25541618Srie 25551618Srie syms = symsec->c_data->d_buf; 25561618Srie 25571618Srie /* 25581618Srie * Loop through the relocation entries. 25591618Srie */ 25601618Srie for (relndx = 0; relndx < relnum; relndx++, 25611618Srie rels = (void *)((char *)rels + relsize)) { 25621618Srie char section[BUFSIZ]; 25631618Srie Addr offset; 25640Sstevel@tonic-gate Got_info *gip; 25651618Srie Word symndx, reltype; 25661618Srie Rela *rela; 25671618Srie Rel *rel; 25680Sstevel@tonic-gate 25691618Srie /* 25701618Srie * Unravel the relocation. 25711618Srie */ 25721618Srie if (type == SHT_RELA) { 25731618Srie rela = (Rela *)rels; 25741618Srie symndx = ELF_R_SYM(rela->r_info); 25751618Srie reltype = ELF_R_TYPE(rela->r_info); 25761618Srie offset = rela->r_offset; 25770Sstevel@tonic-gate } else { 25781618Srie rel = (Rel *)rels; 25791618Srie symndx = ELF_R_SYM(rel->r_info); 25801618Srie reltype = ELF_R_TYPE(rel->r_info); 25811618Srie offset = rel->r_offset; 25820Sstevel@tonic-gate } 25830Sstevel@tonic-gate 25840Sstevel@tonic-gate /* 25850Sstevel@tonic-gate * Only pay attention to relocations against the GOT. 25860Sstevel@tonic-gate */ 25874146Sab196087 if ((offset < gotbgn) || (offset >= gotend)) 25880Sstevel@tonic-gate continue; 25890Sstevel@tonic-gate 25900Sstevel@tonic-gate /* LINTED */ 25911618Srie gotndx = (Word)((offset - gotbgn) / 25920Sstevel@tonic-gate gotshdr->sh_entsize); 25930Sstevel@tonic-gate gip = &gottable[gotndx]; 25941618Srie 25951618Srie if (gip->g_reltype != 0) { 25960Sstevel@tonic-gate (void) fprintf(stderr, 25970Sstevel@tonic-gate MSG_INTL(MSG_GOT_MULTIPLE), file, 25981618Srie EC_WORD(gotndx), EC_ADDR(offset)); 25990Sstevel@tonic-gate continue; 26000Sstevel@tonic-gate } 26010Sstevel@tonic-gate 26021618Srie if (symndx) 26031618Srie gip->g_symname = relsymname(cache, _cache, 26041618Srie strsec, symndx, symnum, relndx, syms, 26051618Srie section, BUFSIZ, file, flags); 26061618Srie gip->g_reltype = reltype; 26071618Srie gip->g_rel = rels; 26080Sstevel@tonic-gate } 26090Sstevel@tonic-gate } 26100Sstevel@tonic-gate 26111618Srie if (symlookup(MSG_ORIG(MSG_GOT_SYM), cache, shnum, &gotsym, symtab, 26121618Srie file)) 26131618Srie gotsymaddr = gotsym->st_value; 26140Sstevel@tonic-gate else 26151618Srie gotsymaddr = gotbgn; 26160Sstevel@tonic-gate 26171618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 26181618Srie dbg_print(0, MSG_INTL(MSG_ELF_SCN_GOT), gotcache->c_name); 26191618Srie Elf_got_title(0); 26200Sstevel@tonic-gate 26210Sstevel@tonic-gate for (gotndx = 0; gotndx < gotents; gotndx++) { 26220Sstevel@tonic-gate Got_info *gip; 26230Sstevel@tonic-gate Sword gindex; 26241618Srie Addr gaddr; 26251618Srie Xword gotentry; 26260Sstevel@tonic-gate 26270Sstevel@tonic-gate gip = &gottable[gotndx]; 26280Sstevel@tonic-gate 26290Sstevel@tonic-gate gaddr = gotbgn + (gotndx * gentsize); 26301618Srie gindex = (Sword)(gaddr - gotsymaddr) / (Sword)gentsize; 26310Sstevel@tonic-gate 26321618Srie if (gentsize == sizeof (Word)) 26330Sstevel@tonic-gate /* LINTED */ 26341618Srie gotentry = (Xword)(*((Word *)(gotdata) + gotndx)); 26350Sstevel@tonic-gate else 26360Sstevel@tonic-gate /* LINTED */ 26371618Srie gotentry = *((Xword *)(gotdata) + gotndx); 26380Sstevel@tonic-gate 26391618Srie Elf_got_entry(0, gindex, gaddr, gotentry, ehdr->e_machine, 26401618Srie gip->g_reltype, gip->g_rel, gip->g_symname); 26410Sstevel@tonic-gate } 26420Sstevel@tonic-gate free(gottable); 26430Sstevel@tonic-gate } 26440Sstevel@tonic-gate 26450Sstevel@tonic-gate void 26460Sstevel@tonic-gate checksum(Elf *elf) 26470Sstevel@tonic-gate { 26481618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 26491618Srie dbg_print(0, MSG_INTL(MSG_STR_CHECKSUM), elf_checksum(elf)); 26500Sstevel@tonic-gate } 26510Sstevel@tonic-gate 26521618Srie void 26531618Srie regular(const char *file, Elf *elf, uint_t flags, char *Nname, int wfd) 26540Sstevel@tonic-gate { 26550Sstevel@tonic-gate Elf_Scn *scn; 26561618Srie Ehdr *ehdr; 26570Sstevel@tonic-gate Elf_Data *data; 26581618Srie size_t cnt, shstrndx, shnum, phnum; 26591618Srie Shdr *nameshdr, *shdr; 26600Sstevel@tonic-gate char *names = 0; 26610Sstevel@tonic-gate Cache *cache, *_cache; 26623875Sab196087 VERSYM_STATE versym; 26630Sstevel@tonic-gate 26641618Srie if ((ehdr = elf_getehdr(elf)) == NULL) { 26650Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETEHDR)); 26660Sstevel@tonic-gate return; 26670Sstevel@tonic-gate } 26680Sstevel@tonic-gate 26691618Srie if (elf_getshnum(elf, &shnum) == 0) { 26700Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETSHNUM)); 26710Sstevel@tonic-gate return; 26720Sstevel@tonic-gate } 26730Sstevel@tonic-gate 2674942Sahl if (elf_getshstrndx(elf, &shstrndx) == 0) { 26750Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETSHSTRNDX)); 26760Sstevel@tonic-gate return; 26770Sstevel@tonic-gate } 2678942Sahl 26791618Srie if (elf_getphnum(elf, &phnum) == 0) { 2680942Sahl failure(file, MSG_ORIG(MSG_ELF_GETPHNUM)); 2681942Sahl return; 2682942Sahl } 2683942Sahl 26840Sstevel@tonic-gate if ((scn = elf_getscn(elf, 0)) != NULL) { 26851618Srie if ((shdr = elf_getshdr(scn)) == NULL) { 26860Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETSHDR)); 26870Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN), 0); 26880Sstevel@tonic-gate return; 26890Sstevel@tonic-gate } 26900Sstevel@tonic-gate } else 26911618Srie shdr = 0; 26920Sstevel@tonic-gate 26930Sstevel@tonic-gate /* 26940Sstevel@tonic-gate * Print the elf header. 26950Sstevel@tonic-gate */ 26960Sstevel@tonic-gate if (flags & FLG_EHDR) 26971618Srie Elf_ehdr(0, ehdr, shdr); 26980Sstevel@tonic-gate 26990Sstevel@tonic-gate /* 27004146Sab196087 * If the section headers or program headers have inadequate 27014146Sab196087 * alignment for the class of object, print a warning. libelf 27024146Sab196087 * can handle such files, but programs that use them can crash 27034146Sab196087 * when they dereference unaligned items. 27044146Sab196087 */ 27054146Sab196087 if (ehdr->e_phoff & (sizeof (Addr) - 1)) 27064146Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADPHDRALIGN), file); 27074146Sab196087 if (ehdr->e_shoff & (sizeof (Addr) - 1)) 27084146Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHDRALIGN), file); 27094146Sab196087 27104146Sab196087 /* 27110Sstevel@tonic-gate * Print the program headers. 27120Sstevel@tonic-gate */ 27131618Srie if ((flags & FLG_PHDR) && (phnum != 0)) { 27141618Srie Phdr *phdr; 27150Sstevel@tonic-gate 27161618Srie if ((phdr = elf_getphdr(elf)) == NULL) { 27171618Srie failure(file, MSG_ORIG(MSG_ELF_GETPHDR)); 27181618Srie return; 27191618Srie } 27200Sstevel@tonic-gate 27211618Srie for (cnt = 0; cnt < phnum; phdr++, cnt++) { 27223862Srie 27234031Srie if (Nname && 27244031Srie (strcmp(Nname, conv_phdr_type(ehdr->e_machine, 27254031Srie phdr->p_type, CONV_FMT_ALTFILE)) != 0)) 27263862Srie continue; 27273862Srie 27281618Srie dbg_print(0, MSG_ORIG(MSG_STR_EMPTY)); 27291618Srie dbg_print(0, MSG_INTL(MSG_ELF_PHDR), EC_WORD(cnt)); 27301618Srie Elf_phdr(0, ehdr->e_machine, phdr); 27310Sstevel@tonic-gate } 27320Sstevel@tonic-gate } 27330Sstevel@tonic-gate 27340Sstevel@tonic-gate /* 2735942Sahl * Return now if there are no section, if there's just one section to 2736942Sahl * act as an extension of the ELF header, or if on section information 2737942Sahl * was requested. 27380Sstevel@tonic-gate */ 2739942Sahl if ((shnum <= 1) || (flags && (flags & ~(FLG_EHDR | FLG_PHDR)) == 0)) { 27401618Srie if ((ehdr->e_type == ET_CORE) && (flags & FLG_NOTE)) 27410Sstevel@tonic-gate note(0, shnum, 0, file); 27420Sstevel@tonic-gate return; 27430Sstevel@tonic-gate } 27440Sstevel@tonic-gate 27450Sstevel@tonic-gate /* 27460Sstevel@tonic-gate * Obtain the .shstrtab data buffer to provide the required section 27470Sstevel@tonic-gate * name strings. 27480Sstevel@tonic-gate */ 2749*4156Sab196087 if (shstrndx == SHN_UNDEF) { 2750*4156Sab196087 /* 2751*4156Sab196087 * It is rare, but legal, for an object to lack a 2752*4156Sab196087 * header string table section. 2753*4156Sab196087 */ 2754*4156Sab196087 names = NULL; 2755*4156Sab196087 (void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHSTRSEC), file); 2756*4156Sab196087 } else if ((scn = elf_getscn(elf, shstrndx)) == NULL) { 27570Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETSCN)); 27580Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SHDR), 27590Sstevel@tonic-gate EC_XWORD(shstrndx)); 27601618Srie 27610Sstevel@tonic-gate } else if ((data = elf_getdata(scn, NULL)) == NULL) { 27620Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETDATA)); 27630Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_DATA), 27640Sstevel@tonic-gate EC_XWORD(shstrndx)); 27651618Srie 27661618Srie } else if ((nameshdr = elf_getshdr(scn)) == NULL) { 27670Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETSHDR)); 27680Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN), 27693862Srie EC_WORD(elf_ndxscn(scn))); 27701618Srie 27711618Srie } else if ((names = data->d_buf) == 0) 27720Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_SHSTRNULL), file); 27730Sstevel@tonic-gate 27740Sstevel@tonic-gate /* 27753862Srie * Allocate a cache to maintain a descriptor for each section. 27760Sstevel@tonic-gate */ 27770Sstevel@tonic-gate if ((cache = malloc(shnum * sizeof (Cache))) == 0) { 27780Sstevel@tonic-gate int err = errno; 27790Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC), 27800Sstevel@tonic-gate file, strerror(err)); 27810Sstevel@tonic-gate return; 27820Sstevel@tonic-gate } 27830Sstevel@tonic-gate 27841618Srie *cache = cache_init; 27850Sstevel@tonic-gate _cache = cache; 27860Sstevel@tonic-gate _cache++; 27870Sstevel@tonic-gate 27883862Srie /* 27893862Srie * Traverse the sections of the file. This gathering of data is 27903862Srie * carried out in two passes. First, the section headers are captured 27913862Srie * and the section header names are evaluated. A verification pass is 27923862Srie * then carried out over the section information. Files have been 27933862Srie * known to exhibit overlapping (and hence erroneous) section header 27943862Srie * information. 27953862Srie * 27963862Srie * Finally, the data for each section is obtained. This processing is 27973862Srie * carried out after section verification because should any section 27983862Srie * header overlap occur, and a file needs translating (ie. xlate'ing 27993862Srie * information from a non-native architecture file), then the process 28003862Srie * of translation can corrupt the section header information. Of 28013862Srie * course, if there is any section overlap, the data related to the 28023862Srie * sections is going to be compromised. However, it is the translation 28033862Srie * of this data that has caused problems with elfdump()'s ability to 28043862Srie * extract the data. 28053862Srie */ 28060Sstevel@tonic-gate for (cnt = 1, scn = NULL; scn = elf_nextscn(elf, scn); 28070Sstevel@tonic-gate cnt++, _cache++) { 28083862Srie char scnndxnm[100]; 28093862Srie 28104063Sab196087 _cache->c_ndx = cnt; 28113862Srie _cache->c_scn = scn; 28123862Srie 28131618Srie if ((_cache->c_shdr = elf_getshdr(scn)) == NULL) { 28140Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETSHDR)); 28150Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN), 28163862Srie EC_WORD(elf_ndxscn(scn))); 28170Sstevel@tonic-gate } 28180Sstevel@tonic-gate 28193862Srie /* 28203862Srie * If a shstrtab exists, assign the section name. 28213862Srie */ 28223862Srie if (names && _cache->c_shdr) { 28233862Srie if (_cache->c_shdr->sh_name && 28243862Srie /* LINTED */ 28253862Srie (nameshdr->sh_size > _cache->c_shdr->sh_name)) { 28263862Srie _cache->c_name = 28273862Srie names + _cache->c_shdr->sh_name; 28283862Srie continue; 28293862Srie } 28300Sstevel@tonic-gate 28310Sstevel@tonic-gate /* 28323862Srie * Generate an error if the section name index is zero 28333862Srie * or exceeds the shstrtab data. Fall through to 28343862Srie * fabricate a section name. 28350Sstevel@tonic-gate */ 28363862Srie if ((_cache->c_shdr->sh_name == 0) || 28370Sstevel@tonic-gate /* LINTED */ 28381618Srie (nameshdr->sh_size <= _cache->c_shdr->sh_name)) { 28390Sstevel@tonic-gate (void) fprintf(stderr, 28400Sstevel@tonic-gate MSG_INTL(MSG_ERR_BADSHNAME), file, 28413862Srie EC_WORD(cnt), 28421618Srie EC_XWORD(_cache->c_shdr->sh_name)); 28430Sstevel@tonic-gate } 28443862Srie } 28453862Srie 28463862Srie /* 28473862Srie * If there exists no shstrtab data, or a section header has no 28483862Srie * name (an invalid index of 0), then compose a name for the 28493862Srie * section. 28503862Srie */ 28513862Srie (void) snprintf(scnndxnm, sizeof (scnndxnm), 28523862Srie MSG_INTL(MSG_FMT_SCNNDX), cnt); 28533862Srie 28543862Srie if ((_cache->c_name = malloc(strlen(scnndxnm) + 1)) == 0) { 28553862Srie int err = errno; 28563862Srie (void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC), 28573862Srie file, strerror(err)); 28583862Srie return; 28593862Srie } 28603862Srie (void) strcpy(_cache->c_name, scnndxnm); 28613862Srie } 28623862Srie 28633862Srie /* 28643862Srie * Having collected all the sections, validate their address range. 28653862Srie * Cases have existed where the section information has been invalid. 28663862Srie * This can lead to all sorts of other, hard to diagnose errors, as 28673862Srie * each section is processed individually (ie. with elf_getdata()). 28683862Srie * Here, we carry out some address comparisons to catch a family of 28693862Srie * overlapping memory issues we have observed (likely, there are others 28703862Srie * that we have yet to discover). 28713862Srie * 28723862Srie * Note, should any memory overlap occur, obtaining any additional 28733862Srie * data from the file is questionable. However, it might still be 28743862Srie * possible to inspect the ELF header, Programs headers, or individual 28753862Srie * sections, so rather than bailing on an error condition, continue 28763862Srie * processing to see if any data can be salvaged. 28773862Srie */ 28783862Srie for (cnt = 1; cnt < shnum; cnt++) { 28793862Srie Cache *_cache = &cache[cnt]; 28803862Srie Shdr *shdr = _cache->c_shdr; 28813862Srie Off bgn1, bgn = shdr->sh_offset; 28823862Srie Off end1, end = shdr->sh_offset + shdr->sh_size; 28833862Srie int cnt1; 28843862Srie 28853862Srie if ((shdr->sh_size == 0) || (shdr->sh_type == SHT_NOBITS)) 28863862Srie continue; 28873862Srie 28883862Srie for (cnt1 = 1; cnt1 < shnum; cnt1++) { 28893862Srie Cache *_cache1 = &cache[cnt1]; 28903862Srie Shdr *shdr1 = _cache1->c_shdr; 28913862Srie 28923862Srie bgn1 = shdr1->sh_offset; 28933862Srie end1 = shdr1->sh_offset + shdr1->sh_size; 28943862Srie 28953862Srie if ((cnt1 == cnt) || (shdr->sh_size == 0) || 28963862Srie (shdr1->sh_type == SHT_NOBITS)) 28973862Srie continue; 28983862Srie 28993862Srie if (((bgn1 <= bgn) && (end1 > bgn)) || 29003862Srie ((bgn1 < end) && (end1 >= end))) { 29013862Srie (void) fprintf(stderr, 29023862Srie MSG_INTL(MSG_ERR_SECMEMOVER), file, 29033862Srie EC_WORD(elf_ndxscn(_cache1->c_scn)), 29043862Srie _cache1->c_name, EC_OFF(bgn1), EC_OFF(end1), 29053862Srie EC_WORD(elf_ndxscn(_cache->c_scn)), 29063862Srie _cache->c_name, EC_OFF(bgn), EC_OFF(end)); 29070Sstevel@tonic-gate } 29080Sstevel@tonic-gate } 29090Sstevel@tonic-gate 29103862Srie /* 29113862Srie * And finally, make sure this section doesn't overlap the 29123862Srie * section header itself. 29133862Srie */ 29143862Srie bgn1 = ehdr->e_shoff; 29153862Srie end1 = ehdr->e_shoff + (ehdr->e_shentsize * ehdr->e_shnum); 29163862Srie 29173862Srie if (((bgn1 <= bgn) && (end1 > bgn)) || 29183862Srie ((bgn1 < end) && (end1 >= end))) { 29193862Srie (void) fprintf(stderr, 29203862Srie MSG_INTL(MSG_ERR_SHDRMEMOVER), file, EC_OFF(bgn1), 29213862Srie EC_OFF(end1), 29223862Srie EC_WORD(elf_ndxscn(_cache->c_scn)), 29233862Srie _cache->c_name, EC_OFF(bgn), EC_OFF(end)); 29243862Srie } 29253862Srie } 29263862Srie 29273862Srie /* 29283862Srie * Finally, obtain the data for each section. 29293862Srie */ 29303862Srie for (cnt = 1; cnt < shnum; cnt++) { 29313862Srie Cache *_cache = &cache[cnt]; 29323862Srie Elf_Scn *scn = _cache->c_scn; 29333862Srie 29340Sstevel@tonic-gate if ((_cache->c_data = elf_getdata(scn, NULL)) == NULL) { 29350Sstevel@tonic-gate failure(file, MSG_ORIG(MSG_ELF_GETDATA)); 29360Sstevel@tonic-gate (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCNDATA), 29373862Srie EC_WORD(elf_ndxscn(scn))); 29380Sstevel@tonic-gate } 29390Sstevel@tonic-gate 29400Sstevel@tonic-gate /* 29410Sstevel@tonic-gate * Do we wish to write the section out? 29420Sstevel@tonic-gate */ 29433466Srie if (wfd && Nname && (strcmp(Nname, _cache->c_name) == 0) && 29443466Srie _cache->c_data) { 29450Sstevel@tonic-gate (void) write(wfd, _cache->c_data->d_buf, 29460Sstevel@tonic-gate _cache->c_data->d_size); 29470Sstevel@tonic-gate } 29480Sstevel@tonic-gate } 29490Sstevel@tonic-gate 29500Sstevel@tonic-gate if (flags & FLG_SHDR) 29511618Srie sections(file, cache, shnum, ehdr, Nname); 29520Sstevel@tonic-gate 29530Sstevel@tonic-gate if (flags & FLG_INTERP) 29541618Srie interp(file, cache, shnum, phnum, elf); 29550Sstevel@tonic-gate 29563875Sab196087 versions(cache, shnum, file, flags, &versym); 29570Sstevel@tonic-gate 29580Sstevel@tonic-gate if (flags & FLG_SYMBOLS) 29593875Sab196087 symbols(cache, shnum, ehdr, Nname, &versym, file, flags); 29600Sstevel@tonic-gate 29613492Sab196087 if (flags & FLG_SORT) 29623875Sab196087 sunw_sort(cache, shnum, ehdr, Nname, &versym, file, flags); 29633492Sab196087 29640Sstevel@tonic-gate if (flags & FLG_HASH) 29650Sstevel@tonic-gate hash(cache, shnum, Nname, file, flags); 29660Sstevel@tonic-gate 29670Sstevel@tonic-gate if (flags & FLG_GOT) 29681618Srie got(cache, shnum, ehdr, file, flags); 29690Sstevel@tonic-gate 29700Sstevel@tonic-gate if (flags & FLG_GROUP) 29710Sstevel@tonic-gate group(cache, shnum, Nname, file, flags); 29720Sstevel@tonic-gate 29730Sstevel@tonic-gate if (flags & FLG_SYMINFO) 29740Sstevel@tonic-gate syminfo(cache, shnum, file); 29750Sstevel@tonic-gate 29760Sstevel@tonic-gate if (flags & FLG_RELOC) 29771618Srie reloc(cache, shnum, ehdr, Nname, file, flags); 29780Sstevel@tonic-gate 29790Sstevel@tonic-gate if (flags & FLG_DYNAMIC) 29801618Srie dynamic(cache, shnum, ehdr, file); 29810Sstevel@tonic-gate 29820Sstevel@tonic-gate if (flags & FLG_NOTE) 29830Sstevel@tonic-gate note(cache, shnum, Nname, file); 29840Sstevel@tonic-gate 29850Sstevel@tonic-gate if (flags & FLG_MOVE) 29860Sstevel@tonic-gate move(cache, shnum, Nname, file, flags); 29870Sstevel@tonic-gate 29880Sstevel@tonic-gate if (flags & FLG_CHECKSUM) 29890Sstevel@tonic-gate checksum(elf); 29900Sstevel@tonic-gate 29910Sstevel@tonic-gate if (flags & FLG_CAP) 29921618Srie cap(file, cache, shnum, phnum, ehdr, elf); 29930Sstevel@tonic-gate 29940Sstevel@tonic-gate if (flags & FLG_UNWIND) 29951618Srie unwind(cache, shnum, phnum, ehdr, Nname, file, elf); 29960Sstevel@tonic-gate 29970Sstevel@tonic-gate free(cache); 29980Sstevel@tonic-gate } 2999