Annotation of src/usr.bin/nm/elf.c, Revision 1.9
1.9 ! mickey 1: /* $OpenBSD: elf.c,v 1.8 2004/08/20 04:42:51 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.9 ! mickey 56: #define ELF_SDATA ".sdata"
1.3 mickey 57: #define ELF_SBSS ".sbss"
1.6 mickey 58: #define ELF_PLT ".plt"
1.3 mickey 59:
1.1 mickey 60: int
61: elf_fix_header(Elf_Ehdr *eh)
62: {
63: /* nothing to do */
64: if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
65: return (0);
66:
67: eh->e_type = swap16(eh->e_type);
68: eh->e_machine = swap16(eh->e_machine);
69: eh->e_version = swap32(eh->e_version);
70: eh->e_entry = swap_addr(eh->e_entry);
71: eh->e_phoff = swap_off(eh->e_phoff);
72: eh->e_shoff = swap_off(eh->e_shoff);
73: eh->e_flags = swap32(eh->e_flags);
74: eh->e_ehsize = swap16(eh->e_ehsize);
75: eh->e_phentsize = swap16(eh->e_phentsize);
76: eh->e_phnum = swap16(eh->e_phnum);
77: eh->e_shentsize = swap16(eh->e_shentsize);
78: eh->e_shnum = swap16(eh->e_shnum);
79: eh->e_shstrndx = swap16(eh->e_shstrndx);
80:
81: return (1);
82: }
83:
84: int
85: elf_fix_shdrs(Elf_Ehdr *eh, Elf_Shdr *shdr)
86: {
87: int i;
88:
89: /* nothing to do */
90: if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
91: return (0);
92:
93: for (i = eh->e_shnum; i--; shdr++) {
94: shdr->sh_name = swap32(shdr->sh_name);
95: shdr->sh_type = swap32(shdr->sh_type);
96: shdr->sh_flags = swap_xword(shdr->sh_flags);
97: shdr->sh_addr = swap_addr(shdr->sh_addr);
98: shdr->sh_offset = swap_off(shdr->sh_offset);
99: shdr->sh_size = swap_xword(shdr->sh_size);
100: shdr->sh_link = swap32(shdr->sh_link);
101: shdr->sh_info = swap32(shdr->sh_info);
102: shdr->sh_addralign = swap_xword(shdr->sh_addralign);
103: shdr->sh_entsize = swap_xword(shdr->sh_entsize);
104: }
105:
106: return (1);
107: }
108:
109: int
110: elf_fix_phdrs(Elf_Ehdr *eh, Elf_Phdr *phdr)
111: {
112: int i;
113:
114: /* nothing to do */
115: if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
116: return (0);
117:
118: for (i = eh->e_phnum; i--; phdr++) {
119: phdr->p_type = swap32(phdr->p_type);
120: phdr->p_flags = swap32(phdr->p_flags);
121: phdr->p_offset = swap_off(phdr->p_offset);
122: phdr->p_vaddr = swap_addr(phdr->p_vaddr);
123: phdr->p_paddr = swap_addr(phdr->p_paddr);
124: phdr->p_filesz = swap_xword(phdr->p_filesz);
125: phdr->p_memsz = swap_xword(phdr->p_memsz);
126: phdr->p_align = swap_xword(phdr->p_align);
127: }
128:
129: return (1);
130: }
131:
132: int
133: elf_fix_sym(Elf_Ehdr *eh, Elf_Sym *sym)
134: {
135: /* nothing to do */
136: if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
137: return (0);
138:
139: sym->st_name = swap32(sym->st_name);
140: sym->st_shndx = swap16(sym->st_shndx);
141: sym->st_value = swap_addr(sym->st_value);
142: sym->st_size = swap_xword(sym->st_size);
143:
144: return (1);
145: }
146:
1.9 ! mickey 147: int
! 148: elf_shn2type(u_int shn, const char *sn)
! 149: {
! 150: switch (shn) {
! 151: #ifdef SHN_MIPS_SUNDEFINED
! 152: case SHN_MIPS_SUNDEFINED:
! 153: #endif
! 154: case SHN_UNDEF:
! 155: return (N_UNDF | N_EXT);
! 156: case SHN_ABS:
! 157: return (N_ABS);
! 158: #ifdef SHN_MIPS_ACOMMON
! 159: case SHN_MIPS_ACOMMON:
! 160: #endif
! 161: #ifdef SHN_MIPS_SCOMMON
! 162: case SHN_MIPS_SCOMMON:
! 163: #endif
! 164: case SHN_COMMON:
! 165: return (N_COMM);
! 166: #ifdef SHN_MIPS_TEXT
! 167: case SHN_MIPS_TEXT:
! 168: return (N_TEXT);
! 169: #endif
! 170: #ifdef SHN_MIPS_DATA
! 171: case SHN_MIPS_DATA:
! 172: return (N_DATA);
! 173: #endif
! 174: default:
! 175: /* beyond 8 a table-driven binsearch shall be used */
! 176: if (sn == NULL)
! 177: return (-1);
! 178: else if (!strcmp(sn, ELF_TEXT))
! 179: return (N_TEXT);
! 180: else if (!strcmp(sn, ELF_RODATA))
! 181: return (N_SIZE);
! 182: else if (!strcmp(sn, ELF_DATA))
! 183: return (N_DATA);
! 184: else if (!strcmp(sn, ELF_SDATA))
! 185: return (N_DATA);
! 186: else if (!strcmp(sn, ELF_BSS))
! 187: return (N_BSS);
! 188: else if (!strcmp(sn, ELF_SBSS))
! 189: return (N_BSS);
! 190: else if (!strncmp(sn, ELF_GOT, sizeof(ELF_GOT) - 1))
! 191: return (N_DATA);
! 192: else if (!strncmp(sn, ELF_PLT, sizeof(ELF_PLT) - 1))
! 193: return (N_DATA);
! 194: return (-1);
! 195: }
! 196: }
! 197:
1.1 mickey 198: /*
199: * Devise nlist's type from Elf_Sym.
200: * XXX this task is done as well in libc and kvm_mkdb.
201: */
202: int
203: elf2nlist(Elf_Sym *sym, Elf_Ehdr *eh, Elf_Shdr *shdr, char *shstr, struct nlist *np)
204: {
1.9 ! mickey 205: u_char stt;
1.1 mickey 206: const char *sn;
1.9 ! mickey 207: int type;
1.1 mickey 208:
209: if (sym->st_shndx < eh->e_shnum)
210: sn = shstr + shdr[sym->st_shndx].sh_name;
211: else
1.9 ! mickey 212: sn = NULL;
1.6 mickey 213: #if 0
214: {
215: extern char *stab;
1.9 ! mickey 216: printf("%d:%s %d %s\n", sym->st_shndx, sn? sn : "",
1.6 mickey 217: ELF_ST_TYPE(sym->st_info), stab + sym->st_name);
218: }
219: #endif
1.9 ! mickey 220:
! 221: switch (stt = ELF_ST_TYPE(sym->st_info)) {
1.1 mickey 222: case STT_NOTYPE:
1.9 ! mickey 223: case STT_OBJECT:
! 224: type = elf_shn2type(sym->st_shndx, sn);
! 225: if (type < 0) {
! 226: if (sn == NULL)
! 227: np->n_other = '?';
! 228: else
! 229: np->n_type = stt == STT_NOTYPE? N_COMM : N_DATA;
! 230: } else {
! 231: /* a hack for .rodata check (; */
! 232: if (type == N_SIZE) {
1.1 mickey 233: np->n_type = N_DATA;
1.8 mickey 234: np->n_other = 'r';
1.9 ! mickey 235: } else
! 236: np->n_type = type;
1.8 mickey 237: }
238: break;
239:
1.1 mickey 240: case STT_FUNC:
1.9 ! mickey 241: type = elf_shn2type(sym->st_shndx, NULL);
! 242: np->n_type = type < 0? N_TEXT : type;
1.1 mickey 243: if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
244: np->n_type = N_INDR;
245: np->n_other = 'W';
1.9 ! mickey 246: } else if (sn != NULL &&
! 247: strcmp(sn, ELF_INIT) &&
1.6 mickey 248: strcmp(sn, ELF_TEXT) &&
249: strcmp(sn, ELF_FINI)) /* XXX GNU compat */
1.1 mickey 250: np->n_other = '?';
251: break;
252:
1.8 mickey 253: case STT_SECTION:
1.9 ! mickey 254: type = elf_shn2type(sym->st_shndx, NULL);
! 255: if (type < 0)
1.8 mickey 256: np->n_other = '?';
1.9 ! mickey 257: else
! 258: np->n_type = type;
1.1 mickey 259: break;
260:
261: case STT_FILE:
262: np->n_type = N_FN | N_EXT;
263: break;
264:
265: /* XXX how about cross-nm then ? */
266: #ifdef STT_PARISC_MILLI
267: case STT_PARISC_MILLI:
268: np->n_type = N_TEXT;
269: break;
270: #endif
271: default:
272: np->n_other = '?';
273: break;
274: }
275: if (np->n_type != N_UNDF && ELF_ST_BIND(sym->st_info) != STB_LOCAL) {
276: np->n_type |= N_EXT;
277: if (np->n_other)
278: np->n_other = toupper(np->n_other);
279: }
280:
281: return (0);
282: }