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