[BACK]Return to elf.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / nm

Annotation of src/usr.bin/nm/elf.c, Revision 1.30

1.30    ! semarie     1: /*     $OpenBSD: elf.c,v 1.29 2015/06/23 13:43:08 semarie Exp $        */
1.1       mickey      2:
                      3: /*
                      4:  * Copyright (c) 2003 Michael Shalayeff
                      5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  *
                     16:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     17:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     18:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     19:  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
                     20:  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
                     21:  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
                     22:  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     23:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
                     24:  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
                     25:  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
                     26:  * THE POSSIBILITY OF SUCH DAMAGE.
                     27:  */
                     28:
1.10      mickey     29: #include <sys/mman.h>
                     30: #include <unistd.h>
                     31: #include <a.out.h>
                     32: #include <elf_abi.h>
                     33: #include <errno.h>
                     34: #include <err.h>
1.26      millert    35: #include <stdint.h>
1.10      mickey     36: #include <stdio.h>
                     37: #include <stdlib.h>
                     38: #include <string.h>
                     39: #include <ctype.h>
                     40: #include "elfuncs.h"
                     41: #include "util.h"
                     42:
                     43: #if ELFSIZE == 32
1.1       mickey     44: #define        swap_addr       swap32
                     45: #define        swap_off        swap32
                     46: #define        swap_sword      swap32
                     47: #define        swap_word       swap32
                     48: #define        swap_sxword     swap32
                     49: #define        swap_xword      swap32
                     50: #define        swap_half       swap16
                     51: #define        swap_quarter    swap16
1.10      mickey     52: #define        elf_fix_header  elf32_fix_header
                     53: #define        elf_load_shdrs  elf32_load_shdrs
1.13      grange     54: #define        elf_load_phdrs  elf32_load_phdrs
1.10      mickey     55: #define        elf_fix_shdrs   elf32_fix_shdrs
                     56: #define        elf_fix_phdrs   elf32_fix_phdrs
                     57: #define        elf_fix_sym     elf32_fix_sym
                     58: #define        elf_size        elf32_size
1.14      kettenis   59: #define        elf_symloadx    elf32_symloadx
1.10      mickey     60: #define        elf_symload     elf32_symload
                     61: #define        elf2nlist       elf32_2nlist
                     62: #define        elf_shn2type    elf32_shn2type
                     63: #elif ELFSIZE == 64
1.1       mickey     64: #define        swap_addr       swap64
                     65: #define        swap_off        swap64
                     66: #ifdef __alpha__
                     67: #define        swap_sword      swap64
                     68: #define        swap_word       swap64
                     69: #else
                     70: #define        swap_sword      swap32
                     71: #define        swap_word       swap32
                     72: #endif
                     73: #define        swap_sxword     swap64
                     74: #define        swap_xword      swap64
                     75: #define        swap_half       swap64
                     76: #define        swap_quarter    swap16
1.10      mickey     77: #define        elf_fix_header  elf64_fix_header
                     78: #define        elf_load_shdrs  elf64_load_shdrs
1.13      grange     79: #define        elf_load_phdrs  elf64_load_phdrs
1.10      mickey     80: #define        elf_fix_shdrs   elf64_fix_shdrs
                     81: #define        elf_fix_phdrs   elf64_fix_phdrs
                     82: #define        elf_fix_sym     elf64_fix_sym
                     83: #define        elf_size        elf64_size
1.14      kettenis   84: #define        elf_symloadx    elf64_symloadx
1.10      mickey     85: #define        elf_symload     elf64_symload
                     86: #define        elf2nlist       elf64_2nlist
                     87: #define        elf_shn2type    elf64_shn2type
1.1       mickey     88: #else
                     89: #error "Unsupported ELF class"
                     90: #endif
                     91:
1.9       mickey     92: #define        ELF_SDATA       ".sdata"
1.20      uwe        93: #define        ELF_TDATA       ".tdata"
1.3       mickey     94: #define        ELF_SBSS        ".sbss"
1.20      uwe        95: #define        ELF_TBSS        ".tbss"
1.6       mickey     96: #define        ELF_PLT         ".plt"
1.3       mickey     97:
1.11      mickey     98: #ifndef        SHN_MIPS_ACOMMON
                     99: #define        SHN_MIPS_ACOMMON        SHN_LOPROC + 0
                    100: #endif
                    101: #ifndef        SHN_MIPS_TEXT
                    102: #define        SHN_MIPS_TEXT           SHN_LOPROC + 1
                    103: #endif
                    104: #ifndef        SHN_MIPS_DATA
                    105: #define        SHN_MIPS_DATA           SHN_LOPROC + 2
                    106: #endif
                    107: #ifndef        SHN_MIPS_SUNDEFINED
                    108: #define        SHN_MIPS_SUNDEFINED     SHN_LOPROC + 4
                    109: #endif
                    110: #ifndef        SHN_MIPS_SCOMMON
                    111: #define        SHN_MIPS_SCOMMON        SHN_LOPROC + 3
                    112: #endif
                    113:
                    114: #ifndef        STT_PARISC_MILLI
                    115: #define        STT_PARISC_MILLI        STT_LOPROC + 0
                    116: #endif
                    117:
1.22      deraadt   118: int elf_shn2type(Elf_Ehdr *, u_int, const char *);
                    119: int elf2nlist(Elf_Sym *, Elf_Ehdr *, Elf_Shdr *, char *, struct nlist *);
                    120:
1.1       mickey    121: int
                    122: elf_fix_header(Elf_Ehdr *eh)
                    123: {
                    124:        /* nothing to do */
                    125:        if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
                    126:                return (0);
                    127:
                    128:        eh->e_type = swap16(eh->e_type);
                    129:        eh->e_machine = swap16(eh->e_machine);
                    130:        eh->e_version = swap32(eh->e_version);
                    131:        eh->e_entry = swap_addr(eh->e_entry);
                    132:        eh->e_phoff = swap_off(eh->e_phoff);
                    133:        eh->e_shoff = swap_off(eh->e_shoff);
                    134:        eh->e_flags = swap32(eh->e_flags);
                    135:        eh->e_ehsize = swap16(eh->e_ehsize);
                    136:        eh->e_phentsize = swap16(eh->e_phentsize);
                    137:        eh->e_phnum = swap16(eh->e_phnum);
                    138:        eh->e_shentsize = swap16(eh->e_shentsize);
                    139:        eh->e_shnum = swap16(eh->e_shnum);
                    140:        eh->e_shstrndx = swap16(eh->e_shstrndx);
                    141:
                    142:        return (1);
                    143: }
                    144:
1.10      mickey    145: Elf_Shdr *
                    146: elf_load_shdrs(const char *name, FILE *fp, off_t foff, Elf_Ehdr *head)
                    147: {
                    148:        Elf_Shdr *shdr;
                    149:
                    150:        elf_fix_header(head);
1.29      semarie   151:
                    152:        if (head->e_shnum == 0) {
                    153:                warnx("%s: no section header table", name);
                    154:                return (NULL);
                    155:        }
                    156:
                    157:        if (head->e_shstrndx >= head->e_shnum) {
                    158:                warnx("%s: inconsistent section header table", name);
                    159:                return (NULL);
                    160:        }
1.10      mickey    161:
1.18      deraadt   162:        if ((shdr = calloc(head->e_shentsize, head->e_shnum)) == NULL) {
1.10      mickey    163:                warn("%s: malloc shdr", name);
                    164:                return (NULL);
                    165:        }
                    166:
                    167:        if (fseeko(fp, foff + head->e_shoff, SEEK_SET)) {
                    168:                warn("%s: fseeko", name);
                    169:                free(shdr);
                    170:                return (NULL);
                    171:        }
                    172:
                    173:        if (fread(shdr, head->e_shentsize, head->e_shnum, fp) != head->e_shnum) {
                    174:                warnx("%s: premature EOF", name);
                    175:                free(shdr);
                    176:                return (NULL);
                    177:        }
                    178:
                    179:        elf_fix_shdrs(head, shdr);
                    180:        return (shdr);
1.13      grange    181: }
                    182:
                    183: Elf_Phdr *
                    184: elf_load_phdrs(const char *name, FILE *fp, off_t foff, Elf_Ehdr *head)
                    185: {
                    186:        Elf_Phdr *phdr;
                    187:
1.18      deraadt   188:        if ((phdr = calloc(head->e_phentsize, head->e_phnum)) == NULL) {
1.13      grange    189:                warn("%s: malloc phdr", name);
                    190:                return (NULL);
                    191:        }
                    192:
                    193:        if (fseeko(fp, foff + head->e_phoff, SEEK_SET)) {
                    194:                warn("%s: fseeko", name);
                    195:                free(phdr);
                    196:                return (NULL);
                    197:        }
                    198:
                    199:        if (fread(phdr, head->e_phentsize, head->e_phnum, fp) != head->e_phnum) {
                    200:                warnx("%s: premature EOF", name);
                    201:                free(phdr);
                    202:                return (NULL);
                    203:        }
                    204:
                    205:        elf_fix_phdrs(head, phdr);
                    206:        return (phdr);
1.10      mickey    207: }
                    208:
1.1       mickey    209: int
                    210: elf_fix_shdrs(Elf_Ehdr *eh, Elf_Shdr *shdr)
                    211: {
                    212:        int i;
                    213:
                    214:        /* nothing to do */
                    215:        if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
                    216:                return (0);
                    217:
                    218:        for (i = eh->e_shnum; i--; shdr++) {
                    219:                shdr->sh_name = swap32(shdr->sh_name);
                    220:                shdr->sh_type = swap32(shdr->sh_type);
                    221:                shdr->sh_flags = swap_xword(shdr->sh_flags);
                    222:                shdr->sh_addr = swap_addr(shdr->sh_addr);
                    223:                shdr->sh_offset = swap_off(shdr->sh_offset);
                    224:                shdr->sh_size = swap_xword(shdr->sh_size);
                    225:                shdr->sh_link = swap32(shdr->sh_link);
                    226:                shdr->sh_info = swap32(shdr->sh_info);
                    227:                shdr->sh_addralign = swap_xword(shdr->sh_addralign);
                    228:                shdr->sh_entsize = swap_xword(shdr->sh_entsize);
                    229:        }
                    230:
                    231:        return (1);
                    232: }
                    233:
                    234: int
                    235: elf_fix_phdrs(Elf_Ehdr *eh, Elf_Phdr *phdr)
                    236: {
                    237:        int i;
                    238:
                    239:        /* nothing to do */
                    240:        if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
                    241:                return (0);
                    242:
                    243:        for (i = eh->e_phnum; i--; phdr++) {
                    244:                phdr->p_type = swap32(phdr->p_type);
                    245:                phdr->p_flags = swap32(phdr->p_flags);
                    246:                phdr->p_offset = swap_off(phdr->p_offset);
                    247:                phdr->p_vaddr = swap_addr(phdr->p_vaddr);
                    248:                phdr->p_paddr = swap_addr(phdr->p_paddr);
                    249:                phdr->p_filesz = swap_xword(phdr->p_filesz);
                    250:                phdr->p_memsz = swap_xword(phdr->p_memsz);
                    251:                phdr->p_align = swap_xword(phdr->p_align);
                    252:        }
                    253:
                    254:        return (1);
                    255: }
                    256:
                    257: int
                    258: elf_fix_sym(Elf_Ehdr *eh, Elf_Sym *sym)
                    259: {
                    260:        /* nothing to do */
                    261:        if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
                    262:                return (0);
                    263:
                    264:        sym->st_name = swap32(sym->st_name);
                    265:        sym->st_shndx = swap16(sym->st_shndx);
                    266:        sym->st_value = swap_addr(sym->st_value);
                    267:        sym->st_size = swap_xword(sym->st_size);
                    268:
                    269:        return (1);
                    270: }
                    271:
1.9       mickey    272: int
1.11      mickey    273: elf_shn2type(Elf_Ehdr *eh, u_int shn, const char *sn)
1.9       mickey    274: {
                    275:        switch (shn) {
                    276:        case SHN_MIPS_SUNDEFINED:
1.11      mickey    277:                if (eh->e_machine == EM_MIPS)
                    278:                        return (N_UNDF | N_EXT);
                    279:                break;
                    280:
1.9       mickey    281:        case SHN_UNDEF:
                    282:                return (N_UNDF | N_EXT);
1.11      mickey    283:
1.9       mickey    284:        case SHN_ABS:
                    285:                return (N_ABS);
1.11      mickey    286:
1.9       mickey    287:        case SHN_MIPS_ACOMMON:
1.11      mickey    288:                if (eh->e_machine == EM_MIPS)
                    289:                        return (N_COMM);
                    290:                break;
                    291:
1.9       mickey    292:        case SHN_MIPS_SCOMMON:
1.11      mickey    293:                if (eh->e_machine == EM_MIPS)
                    294:                        return (N_COMM);
                    295:                break;
                    296:
1.9       mickey    297:        case SHN_COMMON:
                    298:                return (N_COMM);
1.11      mickey    299:
1.9       mickey    300:        case SHN_MIPS_TEXT:
1.11      mickey    301:                if (eh->e_machine == EM_MIPS)
                    302:                        return (N_TEXT);
                    303:                break;
                    304:
1.9       mickey    305:        case SHN_MIPS_DATA:
1.11      mickey    306:                if (eh->e_machine == EM_MIPS)
                    307:                        return (N_DATA);
                    308:                break;
                    309:
1.9       mickey    310:        default:
1.20      uwe       311:                /* TODO: beyond 8 a table-driven binsearch should be used */
1.9       mickey    312:                if (sn == NULL)
                    313:                        return (-1);
                    314:                else if (!strcmp(sn, ELF_TEXT))
                    315:                        return (N_TEXT);
                    316:                else if (!strcmp(sn, ELF_RODATA))
                    317:                        return (N_SIZE);
                    318:                else if (!strcmp(sn, ELF_DATA))
                    319:                        return (N_DATA);
                    320:                else if (!strcmp(sn, ELF_SDATA))
                    321:                        return (N_DATA);
1.20      uwe       322:                else if (!strcmp(sn, ELF_TDATA))
                    323:                        return (N_DATA);
1.9       mickey    324:                else if (!strcmp(sn, ELF_BSS))
                    325:                        return (N_BSS);
                    326:                else if (!strcmp(sn, ELF_SBSS))
                    327:                        return (N_BSS);
1.20      uwe       328:                else if (!strcmp(sn, ELF_TBSS))
                    329:                        return (N_BSS);
1.9       mickey    330:                else if (!strncmp(sn, ELF_GOT, sizeof(ELF_GOT) - 1))
                    331:                        return (N_DATA);
                    332:                else if (!strncmp(sn, ELF_PLT, sizeof(ELF_PLT) - 1))
                    333:                        return (N_DATA);
                    334:        }
1.11      mickey    335:
                    336:        return (-1);
1.9       mickey    337: }
                    338:
1.1       mickey    339: /*
                    340:  * Devise nlist's type from Elf_Sym.
                    341:  * XXX this task is done as well in libc and kvm_mkdb.
                    342:  */
                    343: int
                    344: elf2nlist(Elf_Sym *sym, Elf_Ehdr *eh, Elf_Shdr *shdr, char *shstr, struct nlist *np)
                    345: {
1.9       mickey    346:        u_char stt;
1.1       mickey    347:        const char *sn;
1.9       mickey    348:        int type;
1.1       mickey    349:
                    350:        if (sym->st_shndx < eh->e_shnum)
                    351:                sn = shstr + shdr[sym->st_shndx].sh_name;
                    352:        else
1.9       mickey    353:                sn = NULL;
1.6       mickey    354: #if 0
                    355:        {
                    356:                extern char *stab;
1.21      miod      357:                printf("%d:%s %d %d %s\n", sym->st_shndx, sn? sn : "",
                    358:                    ELF_ST_TYPE(sym->st_info), ELF_ST_BIND(sym->st_info),
                    359:                    stab + sym->st_name);
1.6       mickey    360:        }
                    361: #endif
1.9       mickey    362:
                    363:        switch (stt = ELF_ST_TYPE(sym->st_info)) {
1.1       mickey    364:        case STT_NOTYPE:
1.9       mickey    365:        case STT_OBJECT:
1.20      uwe       366:        case STT_TLS:
1.11      mickey    367:                type = elf_shn2type(eh, sym->st_shndx, sn);
1.9       mickey    368:                if (type < 0) {
                    369:                        if (sn == NULL)
                    370:                                np->n_other = '?';
                    371:                        else
                    372:                                np->n_type = stt == STT_NOTYPE? N_COMM : N_DATA;
                    373:                } else {
                    374:                        /* a hack for .rodata check (; */
                    375:                        if (type == N_SIZE) {
1.1       mickey    376:                                np->n_type = N_DATA;
1.8       mickey    377:                                np->n_other = 'r';
1.9       mickey    378:                        } else
                    379:                                np->n_type = type;
1.8       mickey    380:                }
1.21      miod      381:                if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
                    382:                        np->n_other = 'W';
1.8       mickey    383:                break;
                    384:
1.1       mickey    385:        case STT_FUNC:
1.11      mickey    386:                type = elf_shn2type(eh, sym->st_shndx, NULL);
1.9       mickey    387:                np->n_type = type < 0? N_TEXT : type;
1.1       mickey    388:                if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
                    389:                        np->n_other = 'W';
1.14      kettenis  390:                } else if (sn != NULL && *sn != 0 &&
1.9       mickey    391:                    strcmp(sn, ELF_INIT) &&
1.6       mickey    392:                    strcmp(sn, ELF_TEXT) &&
                    393:                    strcmp(sn, ELF_FINI))       /* XXX GNU compat */
1.1       mickey    394:                        np->n_other = '?';
                    395:                break;
                    396:
1.8       mickey    397:        case STT_SECTION:
1.11      mickey    398:                type = elf_shn2type(eh, sym->st_shndx, NULL);
1.9       mickey    399:                if (type < 0)
1.8       mickey    400:                        np->n_other = '?';
1.9       mickey    401:                else
                    402:                        np->n_type = type;
1.1       mickey    403:                break;
                    404:
                    405:        case STT_FILE:
                    406:                np->n_type = N_FN | N_EXT;
                    407:                break;
                    408:
                    409:        case STT_PARISC_MILLI:
1.11      mickey    410:                if (eh->e_machine == EM_PARISC)
                    411:                        np->n_type = N_TEXT;
                    412:                else
                    413:                        np->n_other = '?';
1.1       mickey    414:                break;
1.11      mickey    415:
1.1       mickey    416:        default:
                    417:                np->n_other = '?';
                    418:                break;
                    419:        }
                    420:        if (np->n_type != N_UNDF && ELF_ST_BIND(sym->st_info) != STB_LOCAL) {
                    421:                np->n_type |= N_EXT;
                    422:                if (np->n_other)
1.23      deraadt   423:                        np->n_other = toupper((unsigned char)np->n_other);
1.10      mickey    424:        }
                    425:
                    426:        return (0);
                    427: }
                    428:
                    429: int
                    430: elf_size(Elf_Ehdr *head, Elf_Shdr *shdr,
                    431:     u_long *ptext, u_long *pdata, u_long *pbss)
                    432: {
                    433:        int i;
                    434:
                    435:        *ptext = *pdata = *pbss = 0;
                    436:
                    437:        for (i = 0; i < head->e_shnum; i++) {
                    438:                if (!(shdr[i].sh_flags & SHF_ALLOC))
                    439:                        ;
                    440:                else if (shdr[i].sh_flags & SHF_EXECINSTR ||
                    441:                    !(shdr[i].sh_flags & SHF_WRITE))
                    442:                        *ptext += shdr[i].sh_size;
                    443:                else if (shdr[i].sh_type == SHT_NOBITS)
                    444:                        *pbss += shdr[i].sh_size;
                    445:                else
                    446:                        *pdata += shdr[i].sh_size;
                    447:        }
                    448:
                    449:        return (0);
                    450: }
                    451:
                    452: int
1.14      kettenis  453: elf_symloadx(const char *name, FILE *fp, off_t foff, Elf_Ehdr *eh,
1.30    ! semarie   454:     Elf_Shdr *shdr, char *shstr, long shstrsize, struct nlist **pnames,
1.14      kettenis  455:     struct nlist ***psnames, size_t *pstabsize, int *pnrawnames,
                    456:     const char *strtab, const char *symtab)
1.10      mickey    457: {
1.14      kettenis  458:        long symsize;
1.10      mickey    459:        struct nlist *np;
                    460:        Elf_Sym sbuf;
                    461:        int i;
                    462:
                    463:        for (i = 0; i < eh->e_shnum; i++) {
1.30    ! semarie   464:                if (shdr[i].sh_name >= shstrsize) {
        !           465:                        warnx("%s: corrupt file", name);
        !           466:                        return (1);
        !           467:                }
1.14      kettenis  468:                if (!strcmp(shstr + shdr[i].sh_name, strtab)) {
1.10      mickey    469:                        *pstabsize = shdr[i].sh_size;
1.26      millert   470:                        if (*pstabsize > SIZE_MAX) {
1.10      mickey    471:                                warnx("%s: corrupt file", name);
                    472:                                return (1);
                    473:                        }
                    474:
                    475:                        MMAP(stab, *pstabsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
                    476:                            fileno(fp), foff + shdr[i].sh_offset);
1.16      ray       477:                        if (stab == MAP_FAILED)
1.10      mickey    478:                                return (1);
                    479:                }
                    480:        }
                    481:        for (i = 0; i < eh->e_shnum; i++) {
1.14      kettenis  482:                if (!strcmp(shstr + shdr[i].sh_name, symtab)) {
1.10      mickey    483:                        symsize = shdr[i].sh_size;
                    484:                        if (fseeko(fp, foff + shdr[i].sh_offset, SEEK_SET)) {
                    485:                                warn("%s: fseeko", name);
                    486:                                if (stab)
                    487:                                        MUNMAP(stab, *pstabsize);
                    488:                                return (1);
                    489:                        }
                    490:
                    491:                        *pnrawnames = symsize / sizeof(sbuf);
                    492:                        if ((*pnames = calloc(*pnrawnames, sizeof(*np))) == NULL) {
                    493:                                warn("%s: malloc names", name);
                    494:                                if (stab)
                    495:                                        MUNMAP(stab, *pstabsize);
                    496:                                return (1);
                    497:                        }
1.18      deraadt   498:                        if ((*psnames = calloc(*pnrawnames, sizeof(np))) == NULL) {
1.10      mickey    499:                                warn("%s: malloc snames", name);
                    500:                                if (stab)
                    501:                                        MUNMAP(stab, *pstabsize);
                    502:                                free(*pnames);
                    503:                                return (1);
                    504:                        }
                    505:
                    506:                        for (np = *pnames; symsize > 0; symsize -= sizeof(sbuf)) {
                    507:                                if (fread(&sbuf, 1, sizeof(sbuf),
                    508:                                    fp) != sizeof(sbuf)) {
                    509:                                        warn("%s: read symbol", name);
                    510:                                        if (stab)
                    511:                                                MUNMAP(stab, *pstabsize);
                    512:                                        free(*pnames);
                    513:                                        free(*psnames);
                    514:                                        return (1);
                    515:                                }
                    516:
                    517:                                elf_fix_sym(eh, &sbuf);
                    518:
1.15      mickey    519:                                if (!sbuf.st_name ||
                    520:                                    sbuf.st_name > *pstabsize)
1.10      mickey    521:                                        continue;
                    522:
                    523:                                elf2nlist(&sbuf, eh, shdr, shstr, np);
                    524:                                np->n_value = sbuf.st_value;
                    525:                                np->n_un.n_strx = sbuf.st_name;
                    526:                                np++;
                    527:                        }
                    528:                        *pnrawnames = np - *pnames;
                    529:                }
1.14      kettenis  530:        }
1.22      deraadt   531:        return (0);
1.14      kettenis  532: }
                    533:
                    534: int
                    535: elf_symload(const char *name, FILE *fp, off_t foff, Elf_Ehdr *eh,
                    536:     Elf_Shdr *shdr, struct nlist **pnames, struct nlist ***psnames,
                    537:     size_t *pstabsize, int *pnrawnames)
                    538: {
                    539:        long shstrsize;
                    540:        char *shstr;
                    541:
                    542:        shstrsize = shdr[eh->e_shstrndx].sh_size;
1.17      miod      543:        if (shstrsize == 0) {
                    544:                warnx("%s: no name list", name);
                    545:                return (1);
                    546:        }
                    547:
1.14      kettenis  548:        if ((shstr = malloc(shstrsize)) == NULL) {
                    549:                warn("%s: malloc shsrt", name);
                    550:                return (1);
                    551:        }
                    552:
                    553:        if (fseeko(fp, foff + shdr[eh->e_shstrndx].sh_offset, SEEK_SET)) {
                    554:                warn("%s: fseeko", name);
                    555:                free(shstr);
                    556:                return (1);
                    557:        }
                    558:
                    559:        if (fread(shstr, 1, shstrsize, fp) != shstrsize) {
                    560:                warnx("%s: premature EOF", name);
                    561:                free(shstr);
                    562:                return(1);
                    563:        }
                    564:
                    565:        stab = NULL;
1.25      miod      566:        *pnames = NULL; *psnames = NULL; *pnrawnames = 0;
1.28      guenther  567:        if (!dynamic_only) {
1.30    ! semarie   568:                elf_symloadx(name, fp, foff, eh, shdr, shstr, shstrsize, pnames,
1.28      guenther  569:                    psnames, pstabsize, pnrawnames, ELF_STRTAB, ELF_SYMTAB);
                    570:        }
1.14      kettenis  571:        if (stab == NULL) {
1.30    ! semarie   572:                elf_symloadx(name, fp, foff, eh, shdr, shstr, shstrsize, pnames,
1.14      kettenis  573:                    psnames, pstabsize, pnrawnames, ELF_DYNSTR, ELF_DYNSYM);
1.10      mickey    574:        }
                    575:
                    576:        free(shstr);
                    577:        if (stab == NULL) {
                    578:                warnx("%s: no name list", name);
                    579:                if (*pnames)
                    580:                        free(*pnames);
                    581:                if (*psnames)
                    582:                        free(*psnames);
                    583:                return (1);
1.1       mickey    584:        }
                    585:
                    586:        return (0);
                    587: }