1 /* 2 * Copyright 2008-2009, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Copyright 2003-2012, Axel Dörfler, axeld@pinc-software.de. 4 * Distributed under the terms of the MIT License. 5 * 6 * Copyright 2002, Manuel J. Petit. All rights reserved. 7 * Copyright 2001, Travis Geiselbrecht. All rights reserved. 8 * Distributed under the terms of the NewOS License. 9 */ 10 11 #include "elf_load_image.h" 12 13 #include <stdio.h> 14 #include <string.h> 15 16 #include <syscalls.h> 17 18 #include "add_ons.h" 19 #include "elf_haiku_version.h" 20 #include "elf_symbol_lookup.h" 21 #include "elf_tls.h" 22 #include "elf_versioning.h" 23 #include "images.h" 24 #include "runtime_loader_private.h" 25 26 27 static const char* sSearchPathSubDir = NULL; 28 29 30 static const char* 31 get_program_path() 32 { 33 return gProgramImage != NULL ? gProgramImage->path : NULL; 34 } 35 36 37 static int32 38 count_regions(const char* imagePath, char const* buff, int phnum, int phentsize) 39 { 40 elf_phdr* pheaders; 41 int32 count = 0; 42 int i; 43 44 for (i = 0; i < phnum; i++) { 45 pheaders = (elf_phdr*)(buff + i * phentsize); 46 47 switch (pheaders->p_type) { 48 case PT_NULL: 49 // NOP header 50 break; 51 case PT_LOAD: 52 count += 1; 53 if (pheaders->p_memsz != pheaders->p_filesz) { 54 addr_t A = TO_PAGE_SIZE(pheaders->p_vaddr 55 + pheaders->p_memsz); 56 addr_t B = TO_PAGE_SIZE(pheaders->p_vaddr 57 + pheaders->p_filesz); 58 59 if (A != B) 60 count += 1; 61 } 62 break; 63 case PT_DYNAMIC: 64 // will be handled at some other place 65 break; 66 case PT_INTERP: 67 // should check here for appropriate interpreter 68 break; 69 case PT_NOTE: 70 // unsupported 71 break; 72 case PT_SHLIB: 73 // undefined semantics 74 break; 75 case PT_PHDR: 76 // we don't use it 77 break; 78 case PT_EH_FRAME: 79 case PT_RELRO: 80 // not implemented yet, but can be ignored 81 break; 82 case PT_STACK: 83 // we don't use it 84 break; 85 case PT_TLS: 86 // will be handled at some other place 87 break; 88 case PT_ARM_UNWIND: 89 // will be handled in libgcc_s.so.1 90 break; 91 default: 92 FATAL("%s: Unhandled pheader type in count 0x%" B_PRIx32 "\n", 93 imagePath, pheaders->p_type); 94 break; 95 } 96 } 97 98 return count; 99 } 100 101 102 static status_t 103 parse_program_headers(image_t* image, char* buff, int phnum, int phentsize) 104 { 105 elf_phdr* pheader; 106 int regcount; 107 int i; 108 109 image->dso_tls_id = unsigned(-1); 110 111 regcount = 0; 112 for (i = 0; i < phnum; i++) { 113 pheader = (elf_phdr*)(buff + i * phentsize); 114 115 switch (pheader->p_type) { 116 case PT_NULL: 117 /* NOP header */ 118 break; 119 case PT_LOAD: 120 if (pheader->p_memsz == pheader->p_filesz) { 121 /* 122 * everything in one area 123 */ 124 image->regions[regcount].start = pheader->p_vaddr; 125 image->regions[regcount].size = pheader->p_memsz; 126 image->regions[regcount].vmstart 127 = PAGE_BASE(pheader->p_vaddr); 128 image->regions[regcount].vmsize 129 = TO_PAGE_SIZE(pheader->p_memsz 130 + PAGE_OFFSET(pheader->p_vaddr)); 131 image->regions[regcount].fdstart = pheader->p_offset; 132 image->regions[regcount].fdsize = pheader->p_filesz; 133 image->regions[regcount].delta = 0; 134 image->regions[regcount].flags = 0; 135 if (pheader->p_flags & PF_WRITE) { 136 // this is a writable segment 137 image->regions[regcount].flags |= RFLAG_RW; 138 } 139 } else { 140 /* 141 * may require splitting 142 */ 143 addr_t A = TO_PAGE_SIZE(pheader->p_vaddr 144 + pheader->p_memsz); 145 addr_t B = TO_PAGE_SIZE(pheader->p_vaddr 146 + pheader->p_filesz); 147 148 image->regions[regcount].start = pheader->p_vaddr; 149 image->regions[regcount].size = pheader->p_filesz; 150 image->regions[regcount].vmstart 151 = PAGE_BASE(pheader->p_vaddr); 152 image->regions[regcount].vmsize 153 = TO_PAGE_SIZE(pheader->p_filesz 154 + PAGE_OFFSET(pheader->p_vaddr)); 155 image->regions[regcount].fdstart = pheader->p_offset; 156 image->regions[regcount].fdsize = pheader->p_filesz; 157 image->regions[regcount].delta = 0; 158 image->regions[regcount].flags = 0; 159 if (pheader->p_flags & PF_WRITE) { 160 // this is a writable segment 161 image->regions[regcount].flags |= RFLAG_RW; 162 } 163 164 if (A != B) { 165 /* 166 * yeah, it requires splitting 167 */ 168 regcount += 1; 169 image->regions[regcount].start = pheader->p_vaddr; 170 image->regions[regcount].size 171 = pheader->p_memsz - pheader->p_filesz; 172 image->regions[regcount].vmstart 173 = image->regions[regcount-1].vmstart 174 + image->regions[regcount-1].vmsize; 175 image->regions[regcount].vmsize 176 = TO_PAGE_SIZE(pheader->p_memsz 177 + PAGE_OFFSET(pheader->p_vaddr)) 178 - image->regions[regcount-1].vmsize; 179 image->regions[regcount].fdstart = 0; 180 image->regions[regcount].fdsize = 0; 181 image->regions[regcount].delta = 0; 182 image->regions[regcount].flags = RFLAG_ANON; 183 if (pheader->p_flags & PF_WRITE) { 184 // this is a writable segment 185 image->regions[regcount].flags |= RFLAG_RW; 186 } 187 } 188 } 189 regcount += 1; 190 break; 191 case PT_DYNAMIC: 192 image->dynamic_ptr = pheader->p_vaddr; 193 break; 194 case PT_INTERP: 195 // should check here for appropiate interpreter 196 break; 197 case PT_NOTE: 198 // unsupported 199 break; 200 case PT_SHLIB: 201 // undefined semantics 202 break; 203 case PT_PHDR: 204 // we don't use it 205 break; 206 case PT_EH_FRAME: 207 case PT_RELRO: 208 // not implemented yet, but can be ignored 209 break; 210 case PT_STACK: 211 // we don't use it 212 break; 213 case PT_TLS: 214 image->dso_tls_id 215 = TLSBlockTemplates::Get().Register( 216 TLSBlockTemplate((void*)pheader->p_vaddr, 217 pheader->p_filesz, pheader->p_memsz)); 218 break; 219 case PT_ARM_UNWIND: 220 // will be handled in libgcc_s.so.1 221 break; 222 default: 223 FATAL("%s: Unhandled pheader type in parse 0x%" B_PRIx32 "\n", 224 image->path, pheader->p_type); 225 return B_BAD_DATA; 226 } 227 } 228 229 return B_OK; 230 } 231 232 233 static bool 234 assert_dynamic_loadable(image_t* image) 235 { 236 uint32 i; 237 238 if (!image->dynamic_ptr) 239 return true; 240 241 for (i = 0; i < image->num_regions; i++) { 242 if (image->dynamic_ptr >= image->regions[i].start 243 && image->dynamic_ptr 244 < image->regions[i].start + image->regions[i].size) { 245 return true; 246 } 247 } 248 249 return false; 250 } 251 252 253 static bool 254 parse_dynamic_segment(image_t* image) 255 { 256 elf_dyn* d; 257 int i; 258 int sonameOffset = -1; 259 260 image->symhash = 0; 261 image->syms = 0; 262 image->strtab = 0; 263 264 d = (elf_dyn*)image->dynamic_ptr; 265 if (!d) 266 return true; 267 268 for (i = 0; d[i].d_tag != DT_NULL; i++) { 269 switch (d[i].d_tag) { 270 case DT_NEEDED: 271 image->num_needed += 1; 272 break; 273 case DT_HASH: 274 image->symhash 275 = (uint32*)(d[i].d_un.d_ptr + image->regions[0].delta); 276 break; 277 case DT_STRTAB: 278 image->strtab 279 = (char*)(d[i].d_un.d_ptr + image->regions[0].delta); 280 break; 281 case DT_SYMTAB: 282 image->syms = (elf_sym*) 283 (d[i].d_un.d_ptr + image->regions[0].delta); 284 break; 285 case DT_REL: 286 image->rel = (elf_rel*) 287 (d[i].d_un.d_ptr + image->regions[0].delta); 288 break; 289 case DT_RELSZ: 290 image->rel_len = d[i].d_un.d_val; 291 break; 292 case DT_RELA: 293 image->rela = (elf_rela*) 294 (d[i].d_un.d_ptr + image->regions[0].delta); 295 break; 296 case DT_RELASZ: 297 image->rela_len = d[i].d_un.d_val; 298 break; 299 case DT_JMPREL: 300 // procedure linkage table relocations 301 image->pltrel = (elf_rel*) 302 (d[i].d_un.d_ptr + image->regions[0].delta); 303 break; 304 case DT_PLTRELSZ: 305 image->pltrel_len = d[i].d_un.d_val; 306 break; 307 case DT_INIT: 308 image->init_routine 309 = (d[i].d_un.d_ptr + image->regions[0].delta); 310 break; 311 case DT_FINI: 312 image->term_routine 313 = (d[i].d_un.d_ptr + image->regions[0].delta); 314 break; 315 case DT_SONAME: 316 sonameOffset = d[i].d_un.d_val; 317 break; 318 case DT_VERSYM: 319 image->symbol_versions = (elf_versym*) 320 (d[i].d_un.d_ptr + image->regions[0].delta); 321 break; 322 case DT_VERDEF: 323 image->version_definitions = (elf_verdef*) 324 (d[i].d_un.d_ptr + image->regions[0].delta); 325 break; 326 case DT_VERDEFNUM: 327 image->num_version_definitions = d[i].d_un.d_val; 328 break; 329 case DT_VERNEED: 330 image->needed_versions = (elf_verneed*) 331 (d[i].d_un.d_ptr + image->regions[0].delta); 332 break; 333 case DT_VERNEEDNUM: 334 image->num_needed_versions = d[i].d_un.d_val; 335 break; 336 case DT_SYMBOLIC: 337 image->flags |= RFLAG_SYMBOLIC; 338 break; 339 case DT_FLAGS: 340 { 341 uint32 flags = d[i].d_un.d_val; 342 if ((flags & DF_SYMBOLIC) != 0) 343 image->flags |= RFLAG_SYMBOLIC; 344 if ((flags & DF_STATIC_TLS) != 0) { 345 FATAL("Static TLS model is not supported.\n"); 346 return false; 347 } 348 break; 349 } 350 case DT_INIT_ARRAY: 351 // array of pointers to initialization functions 352 image->init_array = (addr_t*) 353 (d[i].d_un.d_ptr + image->regions[0].delta); 354 break; 355 case DT_INIT_ARRAYSZ: 356 // size in bytes of the array of initialization functions 357 image->init_array_len = d[i].d_un.d_val; 358 break; 359 case DT_PREINIT_ARRAY: 360 // array of pointers to pre-initialization functions 361 image->preinit_array = (addr_t*) 362 (d[i].d_un.d_ptr + image->regions[0].delta); 363 break; 364 case DT_PREINIT_ARRAYSZ: 365 // size in bytes of the array of pre-initialization functions 366 image->preinit_array_len = d[i].d_un.d_val; 367 break; 368 case DT_FINI_ARRAY: 369 // array of pointers to termination functions 370 image->term_array = (addr_t*) 371 (d[i].d_un.d_ptr + image->regions[0].delta); 372 break; 373 case DT_FINI_ARRAYSZ: 374 // size in bytes of the array of termination functions 375 image->term_array_len = d[i].d_un.d_val; 376 break; 377 default: 378 continue; 379 380 // TODO: Implement: 381 // DT_RELENT: The size of a DT_REL entry. 382 // DT_RELAENT: The size of a DT_RELA entry. 383 // DT_SYMENT: The size of a symbol table entry. 384 // DT_PLTREL: The type of the PLT relocation entries (DT_JMPREL). 385 // DT_BIND_NOW/DF_BIND_NOW: No lazy binding allowed. 386 // DT_RUNPATH: Library search path (supersedes DT_RPATH). 387 // DT_TEXTREL/DF_TEXTREL: Indicates whether text relocations are 388 // required (for optimization purposes only). 389 } 390 } 391 392 // lets make sure we found all the required sections 393 if (!image->symhash || !image->syms || !image->strtab) 394 return false; 395 396 if (sonameOffset >= 0) 397 strlcpy(image->name, STRING(image, sonameOffset), sizeof(image->name)); 398 399 return true; 400 } 401 402 403 // #pragma mark - 404 405 406 status_t 407 parse_elf_header(elf_ehdr* eheader, int32* _pheaderSize, 408 int32* _sheaderSize) 409 { 410 if (memcmp(eheader->e_ident, ELFMAG, 4) != 0) 411 return B_NOT_AN_EXECUTABLE; 412 413 if (eheader->e_ident[4] != ELF_CLASS) 414 return B_NOT_AN_EXECUTABLE; 415 416 if (eheader->e_phoff == 0) 417 return B_NOT_AN_EXECUTABLE; 418 419 if (eheader->e_phentsize < sizeof(elf_phdr)) 420 return B_NOT_AN_EXECUTABLE; 421 422 *_pheaderSize = eheader->e_phentsize * eheader->e_phnum; 423 *_sheaderSize = eheader->e_shentsize * eheader->e_shnum; 424 425 if (*_pheaderSize <= 0) 426 return B_NOT_AN_EXECUTABLE; 427 428 return B_OK; 429 } 430 431 432 #if defined(_COMPAT_MODE) 433 #if defined(__x86_64__) 434 status_t 435 parse_elf32_header(Elf32_Ehdr* eheader, int32* _pheaderSize, 436 int32* _sheaderSize) 437 { 438 if (memcmp(eheader->e_ident, ELFMAG, 4) != 0) 439 return B_NOT_AN_EXECUTABLE; 440 441 if (eheader->e_ident[4] != ELFCLASS32) 442 return B_NOT_AN_EXECUTABLE; 443 444 if (eheader->e_phoff == 0) 445 return B_NOT_AN_EXECUTABLE; 446 447 if (eheader->e_phentsize < sizeof(Elf32_Phdr)) 448 return B_NOT_AN_EXECUTABLE; 449 450 *_pheaderSize = eheader->e_phentsize * eheader->e_phnum; 451 *_sheaderSize = eheader->e_shentsize * eheader->e_shnum; 452 453 if (*_pheaderSize <= 0) 454 return B_NOT_AN_EXECUTABLE; 455 456 return B_OK; 457 } 458 #else 459 status_t 460 parse_elf64_header(Elf64_Ehdr* eheader, int32* _pheaderSize, 461 int32* _sheaderSize) 462 { 463 if (memcmp(eheader->e_ident, ELFMAG, 4) != 0) 464 return B_NOT_AN_EXECUTABLE; 465 466 if (eheader->e_ident[4] != ELFCLASS64) 467 return B_NOT_AN_EXECUTABLE; 468 469 if (eheader->e_phoff == 0) 470 return B_NOT_AN_EXECUTABLE; 471 472 if (eheader->e_phentsize < sizeof(Elf64_Phdr)) 473 return B_NOT_AN_EXECUTABLE; 474 475 *_pheaderSize = eheader->e_phentsize * eheader->e_phnum; 476 *_sheaderSize = eheader->e_shentsize * eheader->e_shnum; 477 478 if (*_pheaderSize <= 0) 479 return B_NOT_AN_EXECUTABLE; 480 481 return B_OK; 482 } 483 #endif // __x86_64__ 484 #endif // _COMPAT_MODE 485 486 487 status_t 488 load_image(char const* name, image_type type, const char* rpath, 489 const char* requestingObjectPath, image_t** _image) 490 { 491 int32 pheaderSize, sheaderSize; 492 char path[PATH_MAX]; 493 ssize_t length; 494 char pheaderBuffer[4096]; 495 int32 numRegions; 496 image_t* found; 497 image_t* image; 498 status_t status; 499 int fd; 500 501 elf_ehdr eheader; 502 503 // Have we already loaded that image? Don't check for add-ons -- we always 504 // reload them. 505 if (type != B_ADD_ON_IMAGE) { 506 found = find_loaded_image_by_name(name, APP_OR_LIBRARY_TYPE); 507 508 if (found == NULL && type != B_APP_IMAGE && gProgramImage != NULL) { 509 // Special case for add-ons that link against the application 510 // executable, with the executable not having a soname set. 511 if (const char* lastSlash = strrchr(name, '/')) { 512 if (strcmp(gProgramImage->name, lastSlash + 1) == 0) 513 found = gProgramImage; 514 } 515 } 516 517 if (found) { 518 atomic_add(&found->ref_count, 1); 519 *_image = found; 520 KTRACE("rld: load_container(\"%s\", type: %d, rpath: \"%s\") " 521 "already loaded", name, type, rpath); 522 return B_OK; 523 } 524 } 525 526 KTRACE("rld: load_container(\"%s\", type: %d, rpath: \"%s\")", name, type, 527 rpath); 528 529 strlcpy(path, name, sizeof(path)); 530 531 // find and open the file 532 fd = open_executable(path, type, rpath, get_program_path(), 533 requestingObjectPath, sSearchPathSubDir); 534 if (fd < 0) { 535 FATAL("Cannot open file %s (needed by %s): %s\n", name, requestingObjectPath, strerror(fd)); 536 KTRACE("rld: load_container(\"%s\"): failed to open file", name); 537 return fd; 538 } 539 540 // normalize the image path 541 status = _kern_normalize_path(path, true, path); 542 if (status != B_OK) 543 goto err1; 544 545 // Test again if this image has been registered already - this time, 546 // we can check the full path, not just its name as noted. 547 // You could end up loading an image twice with symbolic links, else. 548 if (type != B_ADD_ON_IMAGE) { 549 found = find_loaded_image_by_name(path, APP_OR_LIBRARY_TYPE); 550 if (found) { 551 atomic_add(&found->ref_count, 1); 552 *_image = found; 553 _kern_close(fd); 554 KTRACE("rld: load_container(\"%s\"): already loaded after all", 555 name); 556 return B_OK; 557 } 558 } 559 560 length = _kern_read(fd, 0, &eheader, sizeof(eheader)); 561 if (length != sizeof(eheader)) { 562 status = B_NOT_AN_EXECUTABLE; 563 FATAL("%s: Troubles reading ELF header\n", path); 564 goto err1; 565 } 566 567 status = parse_elf_header(&eheader, &pheaderSize, &sheaderSize); 568 if (status < B_OK) { 569 FATAL("%s: Incorrect ELF header\n", path); 570 goto err1; 571 } 572 573 // ToDo: what to do about this restriction?? 574 if (pheaderSize > (int)sizeof(pheaderBuffer)) { 575 FATAL("%s: Cannot handle program headers bigger than %lu\n", 576 path, sizeof(pheaderBuffer)); 577 status = B_UNSUPPORTED; 578 goto err1; 579 } 580 581 length = _kern_read(fd, eheader.e_phoff, pheaderBuffer, pheaderSize); 582 if (length != pheaderSize) { 583 FATAL("%s: Could not read program headers: %s\n", path, 584 strerror(length)); 585 status = B_BAD_DATA; 586 goto err1; 587 } 588 589 numRegions = count_regions(path, pheaderBuffer, eheader.e_phnum, 590 eheader.e_phentsize); 591 if (numRegions <= 0) { 592 FATAL("%s: Troubles parsing Program headers, numRegions = %" B_PRId32 593 "\n", path, numRegions); 594 status = B_BAD_DATA; 595 goto err1; 596 } 597 598 image = create_image(name, path, numRegions); 599 if (image == NULL) { 600 FATAL("%s: Failed to allocate image_t object\n", path); 601 status = B_NO_MEMORY; 602 goto err1; 603 } 604 605 status = parse_program_headers(image, pheaderBuffer, eheader.e_phnum, 606 eheader.e_phentsize); 607 if (status < B_OK) 608 goto err2; 609 610 if (!assert_dynamic_loadable(image)) { 611 FATAL("%s: Dynamic segment must be loadable (implementation " 612 "restriction)\n", image->path); 613 status = B_UNSUPPORTED; 614 goto err2; 615 } 616 617 status = map_image(fd, path, image, eheader.e_type == ET_EXEC); 618 if (status < B_OK) { 619 FATAL("%s: Could not map image: %s\n", image->path, strerror(status)); 620 status = B_ERROR; 621 goto err2; 622 } 623 624 if (!parse_dynamic_segment(image)) { 625 FATAL("%s: Troubles handling dynamic section\n", image->path); 626 status = B_BAD_DATA; 627 goto err3; 628 } 629 630 if (eheader.e_entry != 0) 631 image->entry_point = eheader.e_entry + image->regions[0].delta; 632 633 analyze_image_haiku_version_and_abi(fd, image, eheader, sheaderSize, 634 pheaderBuffer, sizeof(pheaderBuffer)); 635 636 // If sSearchPathSubDir is unset (meaning, this is the first image we're 637 // loading) we init the search path subdir if the compiler version doesn't 638 // match ours. 639 if (sSearchPathSubDir == NULL) { 640 #if __GNUC__ == 2 || (defined(_COMPAT_MODE) && !defined(__x86_64__)) 641 if ((image->abi & B_HAIKU_ABI_MAJOR) == B_HAIKU_ABI_GCC_4) 642 sSearchPathSubDir = "x86"; 643 #endif 644 #if __GNUC__ >= 4 || (defined(_COMPAT_MODE) && !defined(__x86_64__)) 645 if ((image->abi & B_HAIKU_ABI_MAJOR) == B_HAIKU_ABI_GCC_2) 646 sSearchPathSubDir = "x86_gcc2"; 647 #endif 648 } 649 650 set_abi_api_version(image->abi, image->api_version); 651 652 // init gcc version dependent image flags 653 // symbol resolution strategy 654 if (image->abi == B_HAIKU_ABI_GCC_2_ANCIENT) 655 image->find_undefined_symbol = find_undefined_symbol_beos; 656 657 // init version infos 658 status = init_image_version_infos(image); 659 660 image->type = type; 661 register_image(image, fd, path); 662 image_event(image, IMAGE_EVENT_LOADED); 663 664 _kern_close(fd); 665 666 enqueue_loaded_image(image); 667 668 *_image = image; 669 670 KTRACE("rld: load_container(\"%s\"): done: id: %" B_PRId32 " (ABI: %#" 671 B_PRIx32 ")", name, image->id, image->abi); 672 673 return B_OK; 674 675 err3: 676 unmap_image(image); 677 err2: 678 delete_image_struct(image); 679 err1: 680 _kern_close(fd); 681 682 KTRACE("rld: load_container(\"%s\"): failed: %s", name, 683 strerror(status)); 684 685 return status; 686 } 687