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