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

1.4     ! mickey      1: /*     $OpenBSD: elf.c,v 1.3 2004/03/30 15:12:38 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:        switch(ELF_ST_TYPE(sym->st_info)) {
                    161:        case STT_NOTYPE:
                    162:                switch (sym->st_shndx) {
                    163:                case SHN_UNDEF:
                    164:                        np->n_type = N_UNDF | N_EXT;
                    165:                        break;
                    166:                case SHN_ABS:
                    167:                        np->n_type = N_ABS;
                    168:                        break;
                    169:                case SHN_COMMON:
                    170:                        np->n_type = N_COMM;
                    171:                        break;
                    172:                default:
                    173:                        if (sym->st_shndx >= eh->e_shnum)
                    174:                                np->n_type = N_COMM | N_EXT;
                    175:                        else if (!strcmp(sn, ELF_TEXT))
                    176:                                np->n_type = N_TEXT;
                    177:                        else if (!strcmp(sn, ELF_RODATA)) {
                    178:                                np->n_type = N_DATA;
                    179:                                np->n_other = 'r';
                    180:                        } else if (!strcmp(sn, ELF_DATA))
                    181:                                np->n_type = N_DATA;
                    182:                        else if (!strcmp(sn, ELF_BSS))
1.3       mickey    183:                                np->n_type = N_BSS | N_EXT;
                    184:                        else if (!strcmp(sn, ELF_SBSS))
1.1       mickey    185:                                np->n_type = N_BSS;
                    186:                        else
                    187:                                np->n_other = '?';
                    188:                        break;
                    189:                }
                    190:                break;
                    191:
                    192:        case STT_FUNC:
                    193:                np->n_type = N_TEXT;
                    194:                if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
                    195:                        np->n_type = N_INDR;
                    196:                        np->n_other = 'W';
                    197:                } else if (sym->st_shndx == SHN_UNDEF)
                    198:                        np->n_type = N_UNDF | N_EXT;
                    199:                else if (strcmp(sn, ELF_TEXT))  /* XXX GNU compat */
                    200:                        np->n_other = '?';
                    201:                break;
                    202:
                    203:        case STT_OBJECT:
                    204:                np->n_type = N_DATA;
                    205:                if (sym->st_shndx == SHN_ABS)
                    206:                        np->n_type = N_ABS;
                    207:                if (sym->st_shndx == SHN_COMMON)
                    208:                        np->n_type = N_COMM;
                    209:                else if (sym->st_shndx >= eh->e_shnum)
                    210:                        break;
1.4     ! mickey    211:                else if (!strcmp(sn, ELF_SBSS))
        !           212:                        np->n_type = N_BSS;
1.1       mickey    213:                else if (!strcmp(sn, ELF_BSS))
1.4     ! mickey    214:                        np->n_type = N_BSS | N_EXT;
1.1       mickey    215:                else if (!strcmp(sn, ELF_RODATA))
                    216:                        np->n_other = 'r';
                    217:                break;
                    218:
                    219:        case STT_FILE:
                    220:                np->n_type = N_FN | N_EXT;
                    221:                break;
                    222:
                    223:        /* XXX how about cross-nm then ? */
                    224: #ifdef STT_PARISC_MILLI
                    225:        case STT_PARISC_MILLI:
                    226:                np->n_type = N_TEXT;
                    227:                break;
                    228: #endif
                    229:        default:
                    230:                np->n_other = '?';
                    231:                break;
                    232:        }
                    233:        if (np->n_type != N_UNDF && ELF_ST_BIND(sym->st_info) != STB_LOCAL) {
                    234:                np->n_type |= N_EXT;
                    235:                if (np->n_other)
                    236:                        np->n_other = toupper(np->n_other);
                    237:        }
                    238:
                    239:        return (0);
                    240: }