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