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

1.37    ! guenther    1: /*     $OpenBSD: elf.c,v 1.36 2017/12/09 06:39:04 deraadt 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);
                    277:                else if (!strcmp(sn, ELF_RODATA))
1.36      deraadt   278:                        return (N_SIZE);
                    279:                else if (!strcmp(sn, ELF_OPENBSDRANDOMDATA))
1.9       mickey    280:                        return (N_SIZE);
                    281:                else if (!strcmp(sn, ELF_DATA))
                    282:                        return (N_DATA);
                    283:                else if (!strcmp(sn, ELF_SDATA))
                    284:                        return (N_DATA);
1.20      uwe       285:                else if (!strcmp(sn, ELF_TDATA))
                    286:                        return (N_DATA);
1.9       mickey    287:                else if (!strcmp(sn, ELF_BSS))
                    288:                        return (N_BSS);
                    289:                else if (!strcmp(sn, ELF_SBSS))
                    290:                        return (N_BSS);
1.20      uwe       291:                else if (!strcmp(sn, ELF_TBSS))
                    292:                        return (N_BSS);
1.9       mickey    293:                else if (!strncmp(sn, ELF_GOT, sizeof(ELF_GOT) - 1))
                    294:                        return (N_DATA);
                    295:                else if (!strncmp(sn, ELF_PLT, sizeof(ELF_PLT) - 1))
                    296:                        return (N_DATA);
                    297:        }
1.11      mickey    298:
                    299:        return (-1);
1.9       mickey    300: }
                    301:
1.1       mickey    302: /*
1.33      miod      303:  * Devise xnlist's type from Elf_Sym.
1.1       mickey    304:  * XXX this task is done as well in libc and kvm_mkdb.
                    305:  */
                    306: int
1.33      miod      307: elf2nlist(Elf_Sym *sym, Elf_Ehdr *eh, Elf_Shdr *shdr, char *shstr,
                    308:     struct xnlist *np)
1.1       mickey    309: {
1.9       mickey    310:        u_char stt;
1.1       mickey    311:        const char *sn;
1.9       mickey    312:        int type;
1.1       mickey    313:
                    314:        if (sym->st_shndx < eh->e_shnum)
                    315:                sn = shstr + shdr[sym->st_shndx].sh_name;
                    316:        else
1.9       mickey    317:                sn = NULL;
1.6       mickey    318: #if 0
                    319:        {
                    320:                extern char *stab;
1.21      miod      321:                printf("%d:%s %d %d %s\n", sym->st_shndx, sn? sn : "",
                    322:                    ELF_ST_TYPE(sym->st_info), ELF_ST_BIND(sym->st_info),
                    323:                    stab + sym->st_name);
1.6       mickey    324:        }
                    325: #endif
1.9       mickey    326:
                    327:        switch (stt = ELF_ST_TYPE(sym->st_info)) {
1.1       mickey    328:        case STT_NOTYPE:
1.9       mickey    329:        case STT_OBJECT:
1.20      uwe       330:        case STT_TLS:
1.11      mickey    331:                type = elf_shn2type(eh, sym->st_shndx, sn);
1.9       mickey    332:                if (type < 0) {
                    333:                        if (sn == NULL)
1.33      miod      334:                                np->nl.n_other = '?';
1.9       mickey    335:                        else
1.33      miod      336:                                np->nl.n_type = stt == STT_NOTYPE ?
                    337:                                    N_COMM : N_DATA;
1.9       mickey    338:                } else {
                    339:                        /* a hack for .rodata check (; */
                    340:                        if (type == N_SIZE) {
1.33      miod      341:                                np->nl.n_type = N_DATA;
                    342:                                np->nl.n_other = 'r';
1.9       mickey    343:                        } else
1.33      miod      344:                                np->nl.n_type = type;
1.8       mickey    345:                }
1.21      miod      346:                if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
1.33      miod      347:                        np->nl.n_other = 'W';
1.8       mickey    348:                break;
                    349:
1.1       mickey    350:        case STT_FUNC:
1.11      mickey    351:                type = elf_shn2type(eh, sym->st_shndx, NULL);
1.33      miod      352:                np->nl.n_type = type < 0? N_TEXT : type;
1.1       mickey    353:                if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1.33      miod      354:                        np->nl.n_other = 'W';
1.14      kettenis  355:                } else if (sn != NULL && *sn != 0 &&
1.9       mickey    356:                    strcmp(sn, ELF_INIT) &&
1.6       mickey    357:                    strcmp(sn, ELF_TEXT) &&
                    358:                    strcmp(sn, ELF_FINI))       /* XXX GNU compat */
1.33      miod      359:                        np->nl.n_other = '?';
1.1       mickey    360:                break;
                    361:
1.8       mickey    362:        case STT_SECTION:
1.11      mickey    363:                type = elf_shn2type(eh, sym->st_shndx, NULL);
1.9       mickey    364:                if (type < 0)
1.33      miod      365:                        np->nl.n_other = '?';
1.9       mickey    366:                else
1.33      miod      367:                        np->nl.n_type = type;
1.1       mickey    368:                break;
                    369:
                    370:        case STT_FILE:
1.33      miod      371:                np->nl.n_type = N_FN | N_EXT;
1.1       mickey    372:                break;
                    373:
                    374:        case STT_PARISC_MILLI:
1.11      mickey    375:                if (eh->e_machine == EM_PARISC)
1.33      miod      376:                        np->nl.n_type = N_TEXT;
1.11      mickey    377:                else
1.33      miod      378:                        np->nl.n_other = '?';
1.1       mickey    379:                break;
1.11      mickey    380:
1.1       mickey    381:        default:
1.33      miod      382:                np->nl.n_other = '?';
1.1       mickey    383:                break;
                    384:        }
1.33      miod      385:        if (np->nl.n_type != N_UNDF && ELF_ST_BIND(sym->st_info) != STB_LOCAL) {
                    386:                np->nl.n_type |= N_EXT;
                    387:                if (np->nl.n_other)
                    388:                        np->nl.n_other = toupper((unsigned char)np->nl.n_other);
1.10      mickey    389:        }
                    390:
                    391:        return (0);
                    392: }
                    393:
                    394: int
                    395: elf_size(Elf_Ehdr *head, Elf_Shdr *shdr,
                    396:     u_long *ptext, u_long *pdata, u_long *pbss)
                    397: {
                    398:        int i;
                    399:
                    400:        *ptext = *pdata = *pbss = 0;
                    401:
                    402:        for (i = 0; i < head->e_shnum; i++) {
                    403:                if (!(shdr[i].sh_flags & SHF_ALLOC))
                    404:                        ;
                    405:                else if (shdr[i].sh_flags & SHF_EXECINSTR ||
                    406:                    !(shdr[i].sh_flags & SHF_WRITE))
                    407:                        *ptext += shdr[i].sh_size;
                    408:                else if (shdr[i].sh_type == SHT_NOBITS)
                    409:                        *pbss += shdr[i].sh_size;
                    410:                else
                    411:                        *pdata += shdr[i].sh_size;
                    412:        }
                    413:
                    414:        return (0);
                    415: }
                    416:
                    417: int
1.14      kettenis  418: elf_symloadx(const char *name, FILE *fp, off_t foff, Elf_Ehdr *eh,
1.33      miod      419:     Elf_Shdr *shdr, char *shstr, long shstrsize, struct xnlist **pnames,
                    420:     struct xnlist ***psnames, size_t *pstabsize, int *pnrawnames,
1.14      kettenis  421:     const char *strtab, const char *symtab)
1.10      mickey    422: {
1.14      kettenis  423:        long symsize;
1.33      miod      424:        struct xnlist *np;
1.10      mickey    425:        Elf_Sym sbuf;
                    426:        int i;
                    427:
                    428:        for (i = 0; i < eh->e_shnum; i++) {
1.30      semarie   429:                if (shdr[i].sh_name >= shstrsize) {
                    430:                        warnx("%s: corrupt file", name);
                    431:                        return (1);
                    432:                }
1.14      kettenis  433:                if (!strcmp(shstr + shdr[i].sh_name, strtab)) {
1.10      mickey    434:                        *pstabsize = shdr[i].sh_size;
1.26      millert   435:                        if (*pstabsize > SIZE_MAX) {
1.10      mickey    436:                                warnx("%s: corrupt file", name);
                    437:                                return (1);
                    438:                        }
                    439:
                    440:                        MMAP(stab, *pstabsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
                    441:                            fileno(fp), foff + shdr[i].sh_offset);
1.16      ray       442:                        if (stab == MAP_FAILED)
1.10      mickey    443:                                return (1);
                    444:                }
                    445:        }
                    446:        for (i = 0; i < eh->e_shnum; i++) {
1.14      kettenis  447:                if (!strcmp(shstr + shdr[i].sh_name, symtab)) {
1.10      mickey    448:                        symsize = shdr[i].sh_size;
                    449:                        if (fseeko(fp, foff + shdr[i].sh_offset, SEEK_SET)) {
                    450:                                warn("%s: fseeko", name);
                    451:                                if (stab)
                    452:                                        MUNMAP(stab, *pstabsize);
                    453:                                return (1);
                    454:                        }
                    455:
                    456:                        *pnrawnames = symsize / sizeof(sbuf);
                    457:                        if ((*pnames = calloc(*pnrawnames, sizeof(*np))) == NULL) {
                    458:                                warn("%s: malloc names", name);
                    459:                                if (stab)
                    460:                                        MUNMAP(stab, *pstabsize);
1.32      semarie   461:                                *pnrawnames = 0;
1.10      mickey    462:                                return (1);
                    463:                        }
1.18      deraadt   464:                        if ((*psnames = calloc(*pnrawnames, sizeof(np))) == NULL) {
1.10      mickey    465:                                warn("%s: malloc snames", name);
                    466:                                if (stab)
                    467:                                        MUNMAP(stab, *pstabsize);
                    468:                                free(*pnames);
1.32      semarie   469:                                *pnames = NULL;
                    470:                                *pnrawnames = 0;
1.10      mickey    471:                                return (1);
                    472:                        }
                    473:
                    474:                        for (np = *pnames; symsize > 0; symsize -= sizeof(sbuf)) {
                    475:                                if (fread(&sbuf, 1, sizeof(sbuf),
                    476:                                    fp) != sizeof(sbuf)) {
                    477:                                        warn("%s: read symbol", name);
                    478:                                        if (stab)
                    479:                                                MUNMAP(stab, *pstabsize);
                    480:                                        free(*pnames);
                    481:                                        free(*psnames);
1.32      semarie   482:                                        *pnames = NULL;
                    483:                                        *psnames = NULL;
                    484:                                        *pnrawnames = 0;
1.10      mickey    485:                                        return (1);
                    486:                                }
                    487:
                    488:                                elf_fix_sym(eh, &sbuf);
                    489:
1.15      mickey    490:                                if (!sbuf.st_name ||
                    491:                                    sbuf.st_name > *pstabsize)
1.10      mickey    492:                                        continue;
                    493:
                    494:                                elf2nlist(&sbuf, eh, shdr, shstr, np);
1.33      miod      495:                                np->nl.n_value = sbuf.st_value;
                    496:                                np->nl.n_un.n_strx = sbuf.st_name;
                    497:                                np->n_size = sbuf.st_size;
1.10      mickey    498:                                np++;
                    499:                        }
                    500:                        *pnrawnames = np - *pnames;
                    501:                }
1.14      kettenis  502:        }
1.22      deraadt   503:        return (0);
1.14      kettenis  504: }
                    505:
                    506: int
                    507: elf_symload(const char *name, FILE *fp, off_t foff, Elf_Ehdr *eh,
1.33      miod      508:     Elf_Shdr *shdr, struct xnlist **pnames, struct xnlist ***psnames,
1.14      kettenis  509:     size_t *pstabsize, int *pnrawnames)
                    510: {
                    511:        long shstrsize;
                    512:        char *shstr;
                    513:
                    514:        shstrsize = shdr[eh->e_shstrndx].sh_size;
1.17      miod      515:        if (shstrsize == 0) {
                    516:                warnx("%s: no name list", name);
                    517:                return (1);
                    518:        }
                    519:
1.14      kettenis  520:        if ((shstr = malloc(shstrsize)) == NULL) {
                    521:                warn("%s: malloc shsrt", name);
                    522:                return (1);
                    523:        }
                    524:
                    525:        if (fseeko(fp, foff + shdr[eh->e_shstrndx].sh_offset, SEEK_SET)) {
                    526:                warn("%s: fseeko", name);
                    527:                free(shstr);
                    528:                return (1);
                    529:        }
                    530:
                    531:        if (fread(shstr, 1, shstrsize, fp) != shstrsize) {
                    532:                warnx("%s: premature EOF", name);
                    533:                free(shstr);
                    534:                return(1);
                    535:        }
                    536:
                    537:        stab = NULL;
1.25      miod      538:        *pnames = NULL; *psnames = NULL; *pnrawnames = 0;
1.28      guenther  539:        if (!dynamic_only) {
1.30      semarie   540:                elf_symloadx(name, fp, foff, eh, shdr, shstr, shstrsize, pnames,
1.28      guenther  541:                    psnames, pstabsize, pnrawnames, ELF_STRTAB, ELF_SYMTAB);
                    542:        }
1.14      kettenis  543:        if (stab == NULL) {
1.30      semarie   544:                elf_symloadx(name, fp, foff, eh, shdr, shstr, shstrsize, pnames,
1.14      kettenis  545:                    psnames, pstabsize, pnrawnames, ELF_DYNSTR, ELF_DYNSYM);
1.10      mickey    546:        }
                    547:
                    548:        free(shstr);
                    549:        if (stab == NULL) {
                    550:                warnx("%s: no name list", name);
1.34      mmcc      551:                free(*pnames);
                    552:                free(*psnames);
1.10      mickey    553:                return (1);
1.1       mickey    554:        }
                    555:
                    556:        return (0);
                    557: }