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

1.3     ! mickey      1: /*     $OpenBSD: elf.c,v 1.2 2004/01/13 17:32:32 mickey 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:
                     29: #if ELF_TARG_CLASS == ELFCLASS32
                     30: #define        swap_addr       swap32
                     31: #define        swap_off        swap32
                     32: #define        swap_sword      swap32
                     33: #define        swap_word       swap32
                     34: #define        swap_sxword     swap32
                     35: #define        swap_xword      swap32
                     36: #define        swap_half       swap16
                     37: #define        swap_quarter    swap16
                     38: #elif ELF_TARG_CLASS == ELFCLASS64
                     39: #define        swap_addr       swap64
                     40: #define        swap_off        swap64
                     41: #ifdef __alpha__
                     42: #define        swap_sword      swap64
                     43: #define        swap_word       swap64
                     44: #else
                     45: #define        swap_sword      swap32
                     46: #define        swap_word       swap32
                     47: #endif
                     48: #define        swap_sxword     swap64
                     49: #define        swap_xword      swap64
                     50: #define        swap_half       swap64
                     51: #define        swap_quarter    swap16
                     52: #else
                     53: #error "Unsupported ELF class"
                     54: #endif
                     55:
1.3     ! mickey     56: #define        ELF_SBSS        ".sbss"
        !            57:
1.1       mickey     58: int
                     59: elf_fix_header(Elf_Ehdr *eh)
                     60: {
                     61:        /* nothing to do */
                     62:        if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
                     63:                return (0);
                     64:
                     65:        eh->e_type = swap16(eh->e_type);
                     66:        eh->e_machine = swap16(eh->e_machine);
                     67:        eh->e_version = swap32(eh->e_version);
                     68:        eh->e_entry = swap_addr(eh->e_entry);
                     69:        eh->e_phoff = swap_off(eh->e_phoff);
                     70:        eh->e_shoff = swap_off(eh->e_shoff);
                     71:        eh->e_flags = swap32(eh->e_flags);
                     72:        eh->e_ehsize = swap16(eh->e_ehsize);
                     73:        eh->e_phentsize = swap16(eh->e_phentsize);
                     74:        eh->e_phnum = swap16(eh->e_phnum);
                     75:        eh->e_shentsize = swap16(eh->e_shentsize);
                     76:        eh->e_shnum = swap16(eh->e_shnum);
                     77:        eh->e_shstrndx = swap16(eh->e_shstrndx);
                     78:
                     79:        return (1);
                     80: }
                     81:
                     82: int
                     83: elf_fix_shdrs(Elf_Ehdr *eh, Elf_Shdr *shdr)
                     84: {
                     85:        int i;
                     86:
                     87:        /* nothing to do */
                     88:        if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
                     89:                return (0);
                     90:
                     91:        for (i = eh->e_shnum; i--; shdr++) {
                     92:                shdr->sh_name = swap32(shdr->sh_name);
                     93:                shdr->sh_type = swap32(shdr->sh_type);
                     94:                shdr->sh_flags = swap_xword(shdr->sh_flags);
                     95:                shdr->sh_addr = swap_addr(shdr->sh_addr);
                     96:                shdr->sh_offset = swap_off(shdr->sh_offset);
                     97:                shdr->sh_size = swap_xword(shdr->sh_size);
                     98:                shdr->sh_link = swap32(shdr->sh_link);
                     99:                shdr->sh_info = swap32(shdr->sh_info);
                    100:                shdr->sh_addralign = swap_xword(shdr->sh_addralign);
                    101:                shdr->sh_entsize = swap_xword(shdr->sh_entsize);
                    102:        }
                    103:
                    104:        return (1);
                    105: }
                    106:
                    107: int
                    108: elf_fix_phdrs(Elf_Ehdr *eh, Elf_Phdr *phdr)
                    109: {
                    110:        int i;
                    111:
                    112:        /* nothing to do */
                    113:        if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
                    114:                return (0);
                    115:
                    116:        for (i = eh->e_phnum; i--; phdr++) {
                    117:                phdr->p_type = swap32(phdr->p_type);
                    118:                phdr->p_flags = swap32(phdr->p_flags);
                    119:                phdr->p_offset = swap_off(phdr->p_offset);
                    120:                phdr->p_vaddr = swap_addr(phdr->p_vaddr);
                    121:                phdr->p_paddr = swap_addr(phdr->p_paddr);
                    122:                phdr->p_filesz = swap_xword(phdr->p_filesz);
                    123:                phdr->p_memsz = swap_xword(phdr->p_memsz);
                    124:                phdr->p_align = swap_xword(phdr->p_align);
                    125:        }
                    126:
                    127:        return (1);
                    128: }
                    129:
                    130: int
                    131: elf_fix_sym(Elf_Ehdr *eh, Elf_Sym *sym)
                    132: {
                    133:        /* nothing to do */
                    134:        if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
                    135:                return (0);
                    136:
                    137:        sym->st_name = swap32(sym->st_name);
                    138:        sym->st_shndx = swap16(sym->st_shndx);
                    139:        sym->st_value = swap_addr(sym->st_value);
                    140:        sym->st_size = swap_xword(sym->st_size);
                    141:
                    142:        return (1);
                    143: }
                    144:
                    145: /*
                    146:  * Devise nlist's type from Elf_Sym.
                    147:  * XXX this task is done as well in libc and kvm_mkdb.
                    148:  */
                    149: int
                    150: elf2nlist(Elf_Sym *sym, Elf_Ehdr *eh, Elf_Shdr *shdr, char *shstr, struct nlist *np)
                    151: {
                    152:        /* extern char *stab; */
                    153:        const char *sn;
                    154:
                    155:        if (sym->st_shndx < eh->e_shnum)
                    156:                sn = shstr + shdr[sym->st_shndx].sh_name;
                    157:        else
                    158:                sn = "";
                    159:
                    160: /* printf("%s 0x%x %s(0x%x)\n", stab + sym->st_name, sym->st_info, sn, sym->st_shndx); */
                    161:        switch(ELF_ST_TYPE(sym->st_info)) {
                    162:        case STT_NOTYPE:
                    163:                switch (sym->st_shndx) {
                    164:                case SHN_UNDEF:
                    165:                        np->n_type = N_UNDF | N_EXT;
                    166:                        break;
                    167:                case SHN_ABS:
                    168:                        np->n_type = N_ABS;
                    169:                        break;
                    170:                case SHN_COMMON:
                    171:                        np->n_type = N_COMM;
                    172:                        break;
                    173:                default:
                    174:                        if (sym->st_shndx >= eh->e_shnum)
                    175:                                np->n_type = N_COMM | N_EXT;
                    176:                        else if (!strcmp(sn, ELF_TEXT))
                    177:                                np->n_type = N_TEXT;
                    178:                        else if (!strcmp(sn, ELF_RODATA)) {
                    179:                                np->n_type = N_DATA;
                    180:                                np->n_other = 'r';
                    181:                        } else if (!strcmp(sn, ELF_DATA))
                    182:                                np->n_type = N_DATA;
                    183:                        else if (!strcmp(sn, ELF_BSS))
1.3     ! mickey    184:                                np->n_type = N_BSS | N_EXT;
        !           185:                        else if (!strcmp(sn, ELF_SBSS))
1.1       mickey    186:                                np->n_type = N_BSS;
                    187:                        else
                    188:                                np->n_other = '?';
                    189:                        break;
                    190:                }
                    191:                break;
                    192:
                    193:        case STT_FUNC:
                    194:                np->n_type = N_TEXT;
                    195:                if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
                    196:                        np->n_type = N_INDR;
                    197:                        np->n_other = 'W';
                    198:                } else if (sym->st_shndx == SHN_UNDEF)
                    199:                        np->n_type = N_UNDF | N_EXT;
                    200:                else if (strcmp(sn, ELF_TEXT))  /* XXX GNU compat */
                    201:                        np->n_other = '?';
                    202:                break;
                    203:
                    204:        case STT_OBJECT:
                    205:                np->n_type = N_DATA;
                    206:                if (sym->st_shndx == SHN_ABS)
                    207:                        np->n_type = N_ABS;
                    208:                if (sym->st_shndx == SHN_COMMON)
                    209:                        np->n_type = N_COMM;
                    210:                else if (sym->st_shndx >= eh->e_shnum)
                    211:                        break;
                    212:                else if (!strcmp(sn, ELF_BSS))
                    213:                        np->n_type = N_BSS;
                    214:                else if (!strcmp(sn, ELF_RODATA))
                    215:                        np->n_other = 'r';
                    216:                break;
                    217:
                    218:        case STT_FILE:
                    219:                np->n_type = N_FN | N_EXT;
                    220:                break;
                    221:
                    222:        /* XXX how about cross-nm then ? */
                    223: #ifdef STT_PARISC_MILLI
                    224:        case STT_PARISC_MILLI:
                    225:                np->n_type = N_TEXT;
                    226:                break;
                    227: #endif
                    228:        default:
                    229:                np->n_other = '?';
                    230:                break;
                    231:        }
                    232:        if (np->n_type != N_UNDF && ELF_ST_BIND(sym->st_info) != STB_LOCAL) {
                    233:                np->n_type |= N_EXT;
                    234:                if (np->n_other)
                    235:                        np->n_other = toupper(np->n_other);
                    236:        }
                    237:
                    238:        return (0);
                    239: }