[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.39

1.39    ! semarie     1: /*     $OpenBSD: elf.c,v 1.38 2020/11/22 10:40:39 jsg 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>
1.35      mpi        32: #include <elf.h>
1.10      mickey     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>
1.33      miod       40: #include "util.h"
1.10      mickey     41: #include "elfuncs.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
                     54: #define        elf_fix_shdrs   elf32_fix_shdrs
                     55: #define        elf_fix_sym     elf32_fix_sym
                     56: #define        elf_size        elf32_size
1.14      kettenis   57: #define        elf_symloadx    elf32_symloadx
1.10      mickey     58: #define        elf_symload     elf32_symload
                     59: #define        elf2nlist       elf32_2nlist
                     60: #define        elf_shn2type    elf32_shn2type
                     61: #elif ELFSIZE == 64
1.1       mickey     62: #define        swap_addr       swap64
                     63: #define        swap_off        swap64
                     64: #ifdef __alpha__
                     65: #define        swap_sword      swap64
                     66: #define        swap_word       swap64
                     67: #else
                     68: #define        swap_sword      swap32
                     69: #define        swap_word       swap32
                     70: #endif
                     71: #define        swap_sxword     swap64
                     72: #define        swap_xword      swap64
                     73: #define        swap_half       swap64
                     74: #define        swap_quarter    swap16
1.10      mickey     75: #define        elf_fix_header  elf64_fix_header
                     76: #define        elf_load_shdrs  elf64_load_shdrs
                     77: #define        elf_fix_shdrs   elf64_fix_shdrs
                     78: #define        elf_fix_sym     elf64_fix_sym
                     79: #define        elf_size        elf64_size
1.14      kettenis   80: #define        elf_symloadx    elf64_symloadx
1.10      mickey     81: #define        elf_symload     elf64_symload
                     82: #define        elf2nlist       elf64_2nlist
                     83: #define        elf_shn2type    elf64_shn2type
1.1       mickey     84: #else
                     85: #error "Unsupported ELF class"
                     86: #endif
                     87:
1.9       mickey     88: #define        ELF_SDATA       ".sdata"
1.20      uwe        89: #define        ELF_TDATA       ".tdata"
1.3       mickey     90: #define        ELF_SBSS        ".sbss"
1.20      uwe        91: #define        ELF_TBSS        ".tbss"
1.6       mickey     92: #define        ELF_PLT         ".plt"
1.3       mickey     93:
1.11      mickey     94: #ifndef        SHN_MIPS_ACOMMON
                     95: #define        SHN_MIPS_ACOMMON        SHN_LOPROC + 0
                     96: #endif
                     97: #ifndef        SHN_MIPS_TEXT
                     98: #define        SHN_MIPS_TEXT           SHN_LOPROC + 1
                     99: #endif
                    100: #ifndef        SHN_MIPS_DATA
                    101: #define        SHN_MIPS_DATA           SHN_LOPROC + 2
                    102: #endif
                    103: #ifndef        SHN_MIPS_SUNDEFINED
                    104: #define        SHN_MIPS_SUNDEFINED     SHN_LOPROC + 4
                    105: #endif
                    106: #ifndef        SHN_MIPS_SCOMMON
                    107: #define        SHN_MIPS_SCOMMON        SHN_LOPROC + 3
                    108: #endif
                    109:
                    110: #ifndef        STT_PARISC_MILLI
                    111: #define        STT_PARISC_MILLI        STT_LOPROC + 0
                    112: #endif
                    113:
1.37      guenther  114:
                    115: static int elf_fix_header(Elf_Ehdr *);
                    116: static int elf_fix_shdrs(Elf_Ehdr *, Elf_Shdr *);
                    117: static int elf_fix_sym(Elf_Ehdr *, Elf_Sym *);
                    118: static int elf_shn2type(Elf_Ehdr *, u_int _shn, const char *_sn);
                    119: static int elf2nlist(Elf_Sym *, Elf_Ehdr *, Elf_Shdr *, char *_shstr,
                    120:            struct xnlist *_np);
                    121: static int elf_symloadx(const char *_name, FILE *, off_t, Elf_Ehdr *,
                    122:            Elf_Shdr *, char *_shstr, long _shstrsize, struct xnlist **_pnames,
                    123:            struct xnlist ***_psnames, size_t *_pstabsize, int *_pnrawnames,
                    124:            const char *_strtab, const char *_symtab);
1.22      deraadt   125:
1.1       mickey    126: int
                    127: elf_fix_header(Elf_Ehdr *eh)
                    128: {
                    129:        /* nothing to do */
                    130:        if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
                    131:                return (0);
                    132:
                    133:        eh->e_type = swap16(eh->e_type);
                    134:        eh->e_machine = swap16(eh->e_machine);
                    135:        eh->e_version = swap32(eh->e_version);
                    136:        eh->e_entry = swap_addr(eh->e_entry);
                    137:        eh->e_phoff = swap_off(eh->e_phoff);
                    138:        eh->e_shoff = swap_off(eh->e_shoff);
                    139:        eh->e_flags = swap32(eh->e_flags);
                    140:        eh->e_ehsize = swap16(eh->e_ehsize);
                    141:        eh->e_phentsize = swap16(eh->e_phentsize);
                    142:        eh->e_phnum = swap16(eh->e_phnum);
                    143:        eh->e_shentsize = swap16(eh->e_shentsize);
                    144:        eh->e_shnum = swap16(eh->e_shnum);
                    145:        eh->e_shstrndx = swap16(eh->e_shstrndx);
                    146:
                    147:        return (1);
                    148: }
                    149:
1.10      mickey    150: Elf_Shdr *
                    151: elf_load_shdrs(const char *name, FILE *fp, off_t foff, Elf_Ehdr *head)
                    152: {
                    153:        Elf_Shdr *shdr;
                    154:
                    155:        elf_fix_header(head);
1.29      semarie   156:
                    157:        if (head->e_shnum == 0) {
                    158:                warnx("%s: no section header table", name);
                    159:                return (NULL);
                    160:        }
                    161:
                    162:        if (head->e_shstrndx >= head->e_shnum) {
                    163:                warnx("%s: inconsistent section header table", name);
                    164:                return (NULL);
                    165:        }
1.10      mickey    166:
1.31      semarie   167:        if (head->e_shentsize < sizeof(Elf_Shdr)) {
                    168:                warnx("%s: inconsistent section header size", name);
                    169:                return (NULL);
                    170:        }
                    171:
                    172:        if ((shdr = calloc(head->e_shnum, head->e_shentsize)) == NULL) {
1.10      mickey    173:                warn("%s: malloc shdr", name);
                    174:                return (NULL);
                    175:        }
                    176:
                    177:        if (fseeko(fp, foff + head->e_shoff, SEEK_SET)) {
                    178:                warn("%s: fseeko", name);
                    179:                free(shdr);
                    180:                return (NULL);
                    181:        }
                    182:
                    183:        if (fread(shdr, head->e_shentsize, head->e_shnum, fp) != head->e_shnum) {
                    184:                warnx("%s: premature EOF", name);
                    185:                free(shdr);
                    186:                return (NULL);
                    187:        }
                    188:
                    189:        elf_fix_shdrs(head, shdr);
                    190:        return (shdr);
1.13      grange    191: }
                    192:
1.1       mickey    193: int
                    194: elf_fix_shdrs(Elf_Ehdr *eh, Elf_Shdr *shdr)
                    195: {
                    196:        int i;
                    197:
                    198:        /* nothing to do */
                    199:        if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
                    200:                return (0);
                    201:
                    202:        for (i = eh->e_shnum; i--; shdr++) {
                    203:                shdr->sh_name = swap32(shdr->sh_name);
                    204:                shdr->sh_type = swap32(shdr->sh_type);
                    205:                shdr->sh_flags = swap_xword(shdr->sh_flags);
                    206:                shdr->sh_addr = swap_addr(shdr->sh_addr);
                    207:                shdr->sh_offset = swap_off(shdr->sh_offset);
                    208:                shdr->sh_size = swap_xword(shdr->sh_size);
                    209:                shdr->sh_link = swap32(shdr->sh_link);
                    210:                shdr->sh_info = swap32(shdr->sh_info);
                    211:                shdr->sh_addralign = swap_xword(shdr->sh_addralign);
                    212:                shdr->sh_entsize = swap_xword(shdr->sh_entsize);
                    213:        }
                    214:
                    215:        return (1);
                    216: }
                    217:
                    218: int
                    219: elf_fix_sym(Elf_Ehdr *eh, Elf_Sym *sym)
                    220: {
                    221:        /* nothing to do */
                    222:        if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
                    223:                return (0);
                    224:
                    225:        sym->st_name = swap32(sym->st_name);
                    226:        sym->st_shndx = swap16(sym->st_shndx);
                    227:        sym->st_value = swap_addr(sym->st_value);
                    228:        sym->st_size = swap_xword(sym->st_size);
                    229:
                    230:        return (1);
                    231: }
                    232:
1.9       mickey    233: int
1.11      mickey    234: elf_shn2type(Elf_Ehdr *eh, u_int shn, const char *sn)
1.9       mickey    235: {
                    236:        switch (shn) {
                    237:        case SHN_MIPS_SUNDEFINED:
1.11      mickey    238:                if (eh->e_machine == EM_MIPS)
                    239:                        return (N_UNDF | N_EXT);
                    240:                break;
                    241:
1.9       mickey    242:        case SHN_UNDEF:
                    243:                return (N_UNDF | N_EXT);
1.11      mickey    244:
1.9       mickey    245:        case SHN_ABS:
                    246:                return (N_ABS);
1.11      mickey    247:
1.9       mickey    248:        case SHN_MIPS_ACOMMON:
1.11      mickey    249:                if (eh->e_machine == EM_MIPS)
                    250:                        return (N_COMM);
                    251:                break;
                    252:
1.9       mickey    253:        case SHN_MIPS_SCOMMON:
1.11      mickey    254:                if (eh->e_machine == EM_MIPS)
                    255:                        return (N_COMM);
                    256:                break;
                    257:
1.9       mickey    258:        case SHN_COMMON:
                    259:                return (N_COMM);
1.11      mickey    260:
1.9       mickey    261:        case SHN_MIPS_TEXT:
1.11      mickey    262:                if (eh->e_machine == EM_MIPS)
                    263:                        return (N_TEXT);
                    264:                break;
                    265:
1.9       mickey    266:        case SHN_MIPS_DATA:
1.11      mickey    267:                if (eh->e_machine == EM_MIPS)
                    268:                        return (N_DATA);
                    269:                break;
                    270:
1.9       mickey    271:        default:
1.20      uwe       272:                /* TODO: beyond 8 a table-driven binsearch should be used */
1.9       mickey    273:                if (sn == NULL)
                    274:                        return (-1);
                    275:                else if (!strcmp(sn, ELF_TEXT))
                    276:                        return (N_TEXT);
1.39    ! semarie   277:                else if (!strncmp(sn, ".text.", 6))
        !           278:                        return (N_TEXT);
1.9       mickey    279:                else if (!strcmp(sn, ELF_RODATA))
1.36      deraadt   280:                        return (N_SIZE);
                    281:                else if (!strcmp(sn, ELF_OPENBSDRANDOMDATA))
1.9       mickey    282:                        return (N_SIZE);
                    283:                else if (!strcmp(sn, ELF_DATA))
                    284:                        return (N_DATA);
                    285:                else if (!strcmp(sn, ELF_SDATA))
                    286:                        return (N_DATA);
1.20      uwe       287:                else if (!strcmp(sn, ELF_TDATA))
                    288:                        return (N_DATA);
1.9       mickey    289:                else if (!strcmp(sn, ELF_BSS))
                    290:                        return (N_BSS);
                    291:                else if (!strcmp(sn, ELF_SBSS))
                    292:                        return (N_BSS);
1.20      uwe       293:                else if (!strcmp(sn, ELF_TBSS))
                    294:                        return (N_BSS);
1.9       mickey    295:                else if (!strncmp(sn, ELF_GOT, sizeof(ELF_GOT) - 1))
                    296:                        return (N_DATA);
                    297:                else if (!strncmp(sn, ELF_PLT, sizeof(ELF_PLT) - 1))
                    298:                        return (N_DATA);
                    299:        }
1.11      mickey    300:
                    301:        return (-1);
1.9       mickey    302: }
                    303:
1.1       mickey    304: /*
1.33      miod      305:  * Devise xnlist's type from Elf_Sym.
1.1       mickey    306:  * XXX this task is done as well in libc and kvm_mkdb.
                    307:  */
                    308: int
1.33      miod      309: elf2nlist(Elf_Sym *sym, Elf_Ehdr *eh, Elf_Shdr *shdr, char *shstr,
                    310:     struct xnlist *np)
1.1       mickey    311: {
1.9       mickey    312:        u_char stt;
1.1       mickey    313:        const char *sn;
1.9       mickey    314:        int type;
1.1       mickey    315:
                    316:        if (sym->st_shndx < eh->e_shnum)
                    317:                sn = shstr + shdr[sym->st_shndx].sh_name;
                    318:        else
1.9       mickey    319:                sn = NULL;
1.6       mickey    320: #if 0
                    321:        {
                    322:                extern char *stab;
1.21      miod      323:                printf("%d:%s %d %d %s\n", sym->st_shndx, sn? sn : "",
                    324:                    ELF_ST_TYPE(sym->st_info), ELF_ST_BIND(sym->st_info),
                    325:                    stab + sym->st_name);
1.6       mickey    326:        }
                    327: #endif
1.9       mickey    328:
                    329:        switch (stt = ELF_ST_TYPE(sym->st_info)) {
1.1       mickey    330:        case STT_NOTYPE:
1.9       mickey    331:        case STT_OBJECT:
1.20      uwe       332:        case STT_TLS:
1.11      mickey    333:                type = elf_shn2type(eh, sym->st_shndx, sn);
1.9       mickey    334:                if (type < 0) {
                    335:                        if (sn == NULL)
1.33      miod      336:                                np->nl.n_other = '?';
1.9       mickey    337:                        else
1.33      miod      338:                                np->nl.n_type = stt == STT_NOTYPE ?
                    339:                                    N_COMM : N_DATA;
1.9       mickey    340:                } else {
                    341:                        /* a hack for .rodata check (; */
                    342:                        if (type == N_SIZE) {
1.33      miod      343:                                np->nl.n_type = N_DATA;
                    344:                                np->nl.n_other = 'r';
1.9       mickey    345:                        } else
1.33      miod      346:                                np->nl.n_type = type;
1.8       mickey    347:                }
1.21      miod      348:                if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
1.33      miod      349:                        np->nl.n_other = 'W';
1.8       mickey    350:                break;
                    351:
1.1       mickey    352:        case STT_FUNC:
1.11      mickey    353:                type = elf_shn2type(eh, sym->st_shndx, NULL);
1.33      miod      354:                np->nl.n_type = type < 0? N_TEXT : type;
1.1       mickey    355:                if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1.33      miod      356:                        np->nl.n_other = 'W';
1.14      kettenis  357:                } else if (sn != NULL && *sn != 0 &&
1.9       mickey    358:                    strcmp(sn, ELF_INIT) &&
1.6       mickey    359:                    strcmp(sn, ELF_TEXT) &&
1.39    ! semarie   360:                    strncmp(sn, ".text.", 6) &&
1.6       mickey    361:                    strcmp(sn, ELF_FINI))       /* XXX GNU compat */
1.33      miod      362:                        np->nl.n_other = '?';
1.1       mickey    363:                break;
                    364:
1.8       mickey    365:        case STT_SECTION:
1.11      mickey    366:                type = elf_shn2type(eh, sym->st_shndx, NULL);
1.9       mickey    367:                if (type < 0)
1.33      miod      368:                        np->nl.n_other = '?';
1.9       mickey    369:                else
1.33      miod      370:                        np->nl.n_type = type;
1.1       mickey    371:                break;
                    372:
                    373:        case STT_FILE:
1.33      miod      374:                np->nl.n_type = N_FN | N_EXT;
1.1       mickey    375:                break;
                    376:
                    377:        case STT_PARISC_MILLI:
1.11      mickey    378:                if (eh->e_machine == EM_PARISC)
1.33      miod      379:                        np->nl.n_type = N_TEXT;
1.11      mickey    380:                else
1.33      miod      381:                        np->nl.n_other = '?';
1.1       mickey    382:                break;
1.11      mickey    383:
1.1       mickey    384:        default:
1.33      miod      385:                np->nl.n_other = '?';
1.1       mickey    386:                break;
                    387:        }
1.33      miod      388:        if (np->nl.n_type != N_UNDF && ELF_ST_BIND(sym->st_info) != STB_LOCAL) {
                    389:                np->nl.n_type |= N_EXT;
                    390:                if (np->nl.n_other)
                    391:                        np->nl.n_other = toupper((unsigned char)np->nl.n_other);
1.10      mickey    392:        }
                    393:
                    394:        return (0);
                    395: }
                    396:
                    397: int
                    398: elf_size(Elf_Ehdr *head, Elf_Shdr *shdr,
                    399:     u_long *ptext, u_long *pdata, u_long *pbss)
                    400: {
                    401:        int i;
                    402:
                    403:        *ptext = *pdata = *pbss = 0;
                    404:
                    405:        for (i = 0; i < head->e_shnum; i++) {
                    406:                if (!(shdr[i].sh_flags & SHF_ALLOC))
                    407:                        ;
                    408:                else if (shdr[i].sh_flags & SHF_EXECINSTR ||
                    409:                    !(shdr[i].sh_flags & SHF_WRITE))
                    410:                        *ptext += shdr[i].sh_size;
                    411:                else if (shdr[i].sh_type == SHT_NOBITS)
                    412:                        *pbss += shdr[i].sh_size;
                    413:                else
                    414:                        *pdata += shdr[i].sh_size;
                    415:        }
                    416:
                    417:        return (0);
                    418: }
                    419:
                    420: int
1.14      kettenis  421: elf_symloadx(const char *name, FILE *fp, off_t foff, Elf_Ehdr *eh,
1.33      miod      422:     Elf_Shdr *shdr, char *shstr, long shstrsize, struct xnlist **pnames,
                    423:     struct xnlist ***psnames, size_t *pstabsize, int *pnrawnames,
1.14      kettenis  424:     const char *strtab, const char *symtab)
1.10      mickey    425: {
1.14      kettenis  426:        long symsize;
1.33      miod      427:        struct xnlist *np;
1.10      mickey    428:        Elf_Sym sbuf;
                    429:        int i;
                    430:
                    431:        for (i = 0; i < eh->e_shnum; i++) {
1.30      semarie   432:                if (shdr[i].sh_name >= shstrsize) {
                    433:                        warnx("%s: corrupt file", name);
                    434:                        return (1);
                    435:                }
1.14      kettenis  436:                if (!strcmp(shstr + shdr[i].sh_name, strtab)) {
1.10      mickey    437:                        *pstabsize = shdr[i].sh_size;
1.26      millert   438:                        if (*pstabsize > SIZE_MAX) {
1.10      mickey    439:                                warnx("%s: corrupt file", name);
                    440:                                return (1);
                    441:                        }
                    442:
                    443:                        MMAP(stab, *pstabsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
                    444:                            fileno(fp), foff + shdr[i].sh_offset);
1.16      ray       445:                        if (stab == MAP_FAILED)
1.10      mickey    446:                                return (1);
                    447:                }
                    448:        }
                    449:        for (i = 0; i < eh->e_shnum; i++) {
1.14      kettenis  450:                if (!strcmp(shstr + shdr[i].sh_name, symtab)) {
1.10      mickey    451:                        symsize = shdr[i].sh_size;
                    452:                        if (fseeko(fp, foff + shdr[i].sh_offset, SEEK_SET)) {
                    453:                                warn("%s: fseeko", name);
                    454:                                if (stab)
                    455:                                        MUNMAP(stab, *pstabsize);
                    456:                                return (1);
                    457:                        }
                    458:
                    459:                        *pnrawnames = symsize / sizeof(sbuf);
                    460:                        if ((*pnames = calloc(*pnrawnames, sizeof(*np))) == NULL) {
                    461:                                warn("%s: malloc names", name);
                    462:                                if (stab)
                    463:                                        MUNMAP(stab, *pstabsize);
1.32      semarie   464:                                *pnrawnames = 0;
1.10      mickey    465:                                return (1);
                    466:                        }
1.18      deraadt   467:                        if ((*psnames = calloc(*pnrawnames, sizeof(np))) == NULL) {
1.10      mickey    468:                                warn("%s: malloc snames", name);
                    469:                                if (stab)
                    470:                                        MUNMAP(stab, *pstabsize);
                    471:                                free(*pnames);
1.32      semarie   472:                                *pnames = NULL;
                    473:                                *pnrawnames = 0;
1.10      mickey    474:                                return (1);
                    475:                        }
                    476:
                    477:                        for (np = *pnames; symsize > 0; symsize -= sizeof(sbuf)) {
                    478:                                if (fread(&sbuf, 1, sizeof(sbuf),
                    479:                                    fp) != sizeof(sbuf)) {
                    480:                                        warn("%s: read symbol", name);
                    481:                                        if (stab)
                    482:                                                MUNMAP(stab, *pstabsize);
                    483:                                        free(*pnames);
                    484:                                        free(*psnames);
1.32      semarie   485:                                        *pnames = NULL;
                    486:                                        *psnames = NULL;
                    487:                                        *pnrawnames = 0;
1.10      mickey    488:                                        return (1);
                    489:                                }
                    490:
                    491:                                elf_fix_sym(eh, &sbuf);
                    492:
1.15      mickey    493:                                if (!sbuf.st_name ||
                    494:                                    sbuf.st_name > *pstabsize)
1.10      mickey    495:                                        continue;
                    496:
                    497:                                elf2nlist(&sbuf, eh, shdr, shstr, np);
1.33      miod      498:                                np->nl.n_value = sbuf.st_value;
                    499:                                np->nl.n_un.n_strx = sbuf.st_name;
                    500:                                np->n_size = sbuf.st_size;
1.10      mickey    501:                                np++;
                    502:                        }
                    503:                        *pnrawnames = np - *pnames;
                    504:                }
1.14      kettenis  505:        }
1.22      deraadt   506:        return (0);
1.14      kettenis  507: }
                    508:
                    509: int
                    510: elf_symload(const char *name, FILE *fp, off_t foff, Elf_Ehdr *eh,
1.33      miod      511:     Elf_Shdr *shdr, struct xnlist **pnames, struct xnlist ***psnames,
1.14      kettenis  512:     size_t *pstabsize, int *pnrawnames)
                    513: {
                    514:        long shstrsize;
                    515:        char *shstr;
                    516:
                    517:        shstrsize = shdr[eh->e_shstrndx].sh_size;
1.17      miod      518:        if (shstrsize == 0) {
                    519:                warnx("%s: no name list", name);
                    520:                return (1);
                    521:        }
                    522:
1.14      kettenis  523:        if ((shstr = malloc(shstrsize)) == NULL) {
1.38      jsg       524:                warn("%s: malloc shstr", name);
1.14      kettenis  525:                return (1);
                    526:        }
                    527:
                    528:        if (fseeko(fp, foff + shdr[eh->e_shstrndx].sh_offset, SEEK_SET)) {
                    529:                warn("%s: fseeko", name);
                    530:                free(shstr);
                    531:                return (1);
                    532:        }
                    533:
                    534:        if (fread(shstr, 1, shstrsize, fp) != shstrsize) {
                    535:                warnx("%s: premature EOF", name);
                    536:                free(shstr);
                    537:                return(1);
                    538:        }
                    539:
                    540:        stab = NULL;
1.25      miod      541:        *pnames = NULL; *psnames = NULL; *pnrawnames = 0;
1.28      guenther  542:        if (!dynamic_only) {
1.30      semarie   543:                elf_symloadx(name, fp, foff, eh, shdr, shstr, shstrsize, pnames,
1.28      guenther  544:                    psnames, pstabsize, pnrawnames, ELF_STRTAB, ELF_SYMTAB);
                    545:        }
1.14      kettenis  546:        if (stab == NULL) {
1.30      semarie   547:                elf_symloadx(name, fp, foff, eh, shdr, shstr, shstrsize, pnames,
1.14      kettenis  548:                    psnames, pstabsize, pnrawnames, ELF_DYNSTR, ELF_DYNSYM);
1.10      mickey    549:        }
                    550:
                    551:        free(shstr);
                    552:        if (stab == NULL) {
                    553:                warnx("%s: no name list", name);
1.34      mmcc      554:                free(*pnames);
                    555:                free(*psnames);
1.10      mickey    556:                return (1);
1.1       mickey    557:        }
                    558:
                    559:        return (0);
                    560: }