1 /* 2 * Copyright 2005-2009, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 * 5 * Copyright 2002, Manuel J. Petit. All rights reserved. 6 * Distributed under the terms of the NewOS License. 7 */ 8 9 10 #include "runtime_loader_private.h" 11 12 #include <string.h> 13 #include <stdlib.h> 14 #include <sys/stat.h> 15 16 #include <algorithm> 17 18 #include <ByteOrder.h> 19 20 #include <directories.h> 21 #include <find_directory_private.h> 22 #include <image_defs.h> 23 #include <syscalls.h> 24 #include <user_runtime.h> 25 #include <vm_defs.h> 26 27 #include "elf_symbol_lookup.h" 28 #include "pe.h" 29 30 31 struct user_space_program_args *gProgramArgs; 32 void *__gCommPageAddress; 33 void *__dso_handle; 34 35 int32 __gCPUCount = 1; 36 37 const directory_which kLibraryDirectories[] = { 38 B_SYSTEM_LIB_DIRECTORY, 39 B_SYSTEM_NONPACKAGED_LIB_DIRECTORY, 40 B_USER_LIB_DIRECTORY, 41 B_USER_NONPACKAGED_LIB_DIRECTORY 42 }; 43 44 45 static const char * 46 search_path_for_type(image_type type) 47 { 48 const char *path = NULL; 49 50 // If "user add-ons" are disabled via safemode settings, we bypass the 51 // environment and defaults and return a different set of paths without 52 // the user or non-packaged ones. 53 if (gProgramArgs->disable_user_addons) { 54 switch (type) { 55 case B_APP_IMAGE: 56 return kGlobalBinDirectory 57 ":" kSystemAppsDirectory 58 ":" kSystemPreferencesDirectory; 59 60 case B_LIBRARY_IMAGE: 61 return kAppLocalLibDirectory 62 ":" kSystemLibDirectory; 63 64 case B_ADD_ON_IMAGE: 65 return kAppLocalAddonsDirectory 66 ":" kSystemAddonsDirectory; 67 68 default: 69 return NULL; 70 } 71 } 72 73 // TODO: The *PATH variables should not include the standard system paths. 74 // Instead those paths should always be used after the directories specified 75 // via the variables. 76 switch (type) { 77 case B_APP_IMAGE: 78 path = getenv("PATH"); 79 break; 80 case B_LIBRARY_IMAGE: 81 path = getenv("LIBRARY_PATH"); 82 break; 83 case B_ADD_ON_IMAGE: 84 path = getenv("ADDON_PATH"); 85 break; 86 87 default: 88 return NULL; 89 } 90 91 if (path != NULL) 92 return path; 93 94 // The environment variables may not have been set yet - in that case, 95 // we're returning some useful defaults. 96 // Since the kernel does not set any variables, this is also needed 97 // to start the root shell. 98 99 switch (type) { 100 case B_APP_IMAGE: 101 return kSystemNonpackagedBinDirectory 102 ":" kGlobalBinDirectory 103 ":" kSystemAppsDirectory 104 ":" kSystemPreferencesDirectory; 105 106 case B_LIBRARY_IMAGE: 107 return kAppLocalLibDirectory 108 ":" kSystemNonpackagedLibDirectory 109 ":" kSystemLibDirectory; 110 111 case B_ADD_ON_IMAGE: 112 return kAppLocalAddonsDirectory 113 ":" kSystemNonpackagedAddonsDirectory 114 ":" kSystemAddonsDirectory; 115 116 default: 117 return NULL; 118 } 119 } 120 121 122 static bool 123 replace_executable_path_placeholder(const char*& dir, int& dirLength, 124 const char* placeholder, size_t placeholderLength, 125 const char* replacementSubPath, char*& buffer, size_t& bufferSize, 126 status_t& _error) 127 { 128 if (dirLength < (int)placeholderLength 129 || strncmp(dir, placeholder, placeholderLength) != 0) { 130 return false; 131 } 132 133 if (replacementSubPath == NULL) { 134 _error = B_ENTRY_NOT_FOUND; 135 return true; 136 } 137 138 char* lastSlash = strrchr(replacementSubPath, '/'); 139 140 // Copy replacementSubPath without the last component (the application file 141 // name, respectively the requesting executable file name). 142 size_t toCopy; 143 if (lastSlash != NULL) { 144 toCopy = lastSlash - replacementSubPath; 145 strlcpy(buffer, replacementSubPath, 146 std::min((ssize_t)bufferSize, lastSlash + 1 - replacementSubPath)); 147 } else { 148 replacementSubPath = "."; 149 toCopy = 1; 150 strlcpy(buffer, ".", bufferSize); 151 } 152 153 if (toCopy >= bufferSize) { 154 _error = B_NAME_TOO_LONG; 155 return true; 156 } 157 158 memcpy(buffer, replacementSubPath, toCopy); 159 buffer[toCopy] = '\0'; 160 161 buffer += toCopy; 162 bufferSize -= toCopy; 163 dir += placeholderLength; 164 dirLength -= placeholderLength; 165 166 _error = B_OK; 167 return true; 168 } 169 170 171 static int 172 try_open_executable(const char *dir, int dirLength, const char *name, 173 const char *programPath, const char *requestingObjectPath, 174 const char *abiSpecificSubDir, char *path, size_t pathLength) 175 { 176 size_t nameLength = strlen(name); 177 struct stat stat; 178 status_t status; 179 180 // construct the path 181 if (dirLength > 0) { 182 char *buffer = path; 183 size_t subDirLen = 0; 184 185 if (programPath == NULL) 186 programPath = gProgramArgs->program_path; 187 188 if (replace_executable_path_placeholder(dir, dirLength, "%A", 2, 189 programPath, buffer, pathLength, status) 190 || replace_executable_path_placeholder(dir, dirLength, "$ORIGIN", 7, 191 requestingObjectPath, buffer, pathLength, status)) { 192 if (status != B_OK) 193 return status; 194 } else if (abiSpecificSubDir != NULL) { 195 // We're looking for a library or an add-on and the executable has 196 // not been compiled with a compiler using the same ABI as the one 197 // the OS has been built with. Thus we only look in subdirs 198 // specific to that ABI. 199 // However, only if it's a known library location 200 for (int i = 0; i < 4; ++i) { 201 char buffer[PATH_MAX]; 202 status_t result = __find_directory(kLibraryDirectories[i], -1, 203 false, buffer, PATH_MAX); 204 if (result == B_OK && strncmp(dir, buffer, dirLength) == 0) { 205 subDirLen = strlen(abiSpecificSubDir) + 1; 206 break; 207 } 208 } 209 } 210 211 if (dirLength + 1 + subDirLen + nameLength >= pathLength) 212 return B_NAME_TOO_LONG; 213 214 memcpy(buffer, dir, dirLength); 215 buffer[dirLength] = '/'; 216 if (subDirLen > 0) { 217 memcpy(buffer + dirLength + 1, abiSpecificSubDir, subDirLen - 1); 218 buffer[dirLength + subDirLen] = '/'; 219 } 220 strcpy(buffer + dirLength + 1 + subDirLen, name); 221 } else { 222 if (nameLength >= pathLength) 223 return B_NAME_TOO_LONG; 224 225 strcpy(path + dirLength + 1, name); 226 } 227 228 TRACE(("runtime_loader: try_open_container(): %s\n", path)); 229 230 // Test if the target is a symbolic link, and correct the path in this case 231 232 status = _kern_read_stat(-1, path, false, &stat, sizeof(struct stat)); 233 if (status < B_OK) 234 return status; 235 236 if (S_ISLNK(stat.st_mode)) { 237 char buffer[PATH_MAX]; 238 size_t length = PATH_MAX - 1; 239 char *lastSlash; 240 241 // it's a link, indeed 242 status = _kern_read_link(-1, path, buffer, &length); 243 if (status < B_OK) 244 return status; 245 buffer[length] = '\0'; 246 247 lastSlash = strrchr(path, '/'); 248 if (buffer[0] != '/' && lastSlash != NULL) { 249 // relative path 250 strlcpy(lastSlash + 1, buffer, lastSlash + 1 - path + pathLength); 251 } else 252 strlcpy(path, buffer, pathLength); 253 } 254 255 return _kern_open(-1, path, O_RDONLY, 0); 256 } 257 258 259 static int 260 search_executable_in_path_list(const char *name, const char *pathList, 261 int pathListLen, const char *programPath, const char *requestingObjectPath, 262 const char *abiSpecificSubDir, char *pathBuffer, size_t pathBufferLength) 263 { 264 const char *pathListEnd = pathList + pathListLen; 265 status_t status = B_ENTRY_NOT_FOUND; 266 267 TRACE(("runtime_loader: search_container_in_path_list() %s in %.*s\n", name, 268 pathListLen, pathList)); 269 270 while (pathListLen > 0) { 271 const char *pathEnd = pathList; 272 int fd; 273 274 // find the next ':' or run till the end of the string 275 while (pathEnd < pathListEnd && *pathEnd != ':') 276 pathEnd++; 277 278 fd = try_open_executable(pathList, pathEnd - pathList, name, 279 programPath, requestingObjectPath, abiSpecificSubDir, pathBuffer, 280 pathBufferLength); 281 if (fd >= 0) { 282 // see if it's a dir 283 struct stat stat; 284 status = _kern_read_stat(fd, NULL, true, &stat, sizeof(struct stat)); 285 if (status == B_OK) { 286 if (!S_ISDIR(stat.st_mode)) 287 return fd; 288 status = B_IS_A_DIRECTORY; 289 } 290 _kern_close(fd); 291 } 292 293 pathListLen = pathListEnd - pathEnd - 1; 294 pathList = pathEnd + 1; 295 } 296 297 return status; 298 } 299 300 301 int 302 open_executable(char *name, image_type type, const char *rpath, 303 const char *programPath, const char *requestingObjectPath, 304 const char *abiSpecificSubDir) 305 { 306 char buffer[PATH_MAX]; 307 int fd = B_ENTRY_NOT_FOUND; 308 309 if (strchr(name, '/')) { 310 // the name already contains a path, we don't have to search for it 311 fd = _kern_open(-1, name, O_RDONLY, 0); 312 if (fd >= 0 || type == B_APP_IMAGE) 313 return fd; 314 315 // can't search harder an absolute path add-on name! 316 if (type == B_ADD_ON_IMAGE && name[0] == '/') 317 return fd; 318 319 // Even though ELF specs don't say this, we give shared libraries 320 // and relative path based add-ons another chance and look 321 // them up in the usual search paths - at 322 // least that seems to be what BeOS does, and since it doesn't hurt... 323 if (type == B_LIBRARY_IMAGE) { 324 // For library (but not add-on), strip any path from name. 325 // Relative path of add-on is kept. 326 const char* paths = strrchr(name, '/') + 1; 327 memmove(name, paths, strlen(paths) + 1); 328 } 329 } 330 331 // try rpath (DT_RPATH) 332 if (rpath != NULL) { 333 // It consists of a colon-separated search path list. Optionally a 334 // second search path list follows, separated from the first by a 335 // semicolon. 336 const char *semicolon = strchr(rpath, ';'); 337 const char *firstList = (semicolon ? rpath : NULL); 338 const char *secondList = (semicolon ? semicolon + 1 : rpath); 339 // If there is no ';', we set only secondList to simplify things. 340 if (firstList) { 341 fd = search_executable_in_path_list(name, firstList, 342 semicolon - firstList, programPath, requestingObjectPath, NULL, 343 buffer, sizeof(buffer)); 344 } 345 if (fd < 0) { 346 fd = search_executable_in_path_list(name, secondList, 347 strlen(secondList), programPath, requestingObjectPath, NULL, 348 buffer, sizeof(buffer)); 349 } 350 } 351 352 // If not found yet, let's evaluate the system path variables to find the 353 // shared object. 354 if (fd < 0) { 355 if (const char *paths = search_path_for_type(type)) { 356 fd = search_executable_in_path_list(name, paths, strlen(paths), 357 programPath, NULL, abiSpecificSubDir, buffer, sizeof(buffer)); 358 } 359 } 360 361 if (fd >= 0) { 362 // we found it, copy path! 363 TRACE(("runtime_loader: open_executable(%s): found at %s\n", name, buffer)); 364 strlcpy(name, buffer, PATH_MAX); 365 } 366 367 return fd; 368 } 369 370 371 /*! 372 Applies haiku-specific fixes to a shebang line. 373 */ 374 static void 375 fixup_shebang(char *invoker) 376 { 377 while (*invoker == ' ' || *invoker == '\t') 378 ++invoker; 379 380 // replace /usr/bin/ with /bin/ 381 if (memcmp(invoker, "/usr/bin/", strlen("/usr/bin/")) == 0) 382 memmove(invoker, invoker + 4, strlen(invoker + 4) + 1); 383 } 384 385 386 /*! 387 Tests if there is an executable file at the provided path. It will 388 also test if the file has a valid ELF header or is a shell script. 389 Even if the runtime loader does not need to be able to deal with 390 both types, the caller will give scripts a proper treatment. 391 */ 392 status_t 393 test_executable(const char *name, char *invoker) 394 { 395 char path[B_PATH_NAME_LENGTH]; 396 char buffer[B_FILE_NAME_LENGTH]; 397 // must be large enough to hold the ELF header 398 status_t status; 399 ssize_t length; 400 int fd; 401 402 if (name == NULL) 403 return B_BAD_VALUE; 404 405 strlcpy(path, name, sizeof(path)); 406 407 fd = open_executable(path, B_APP_IMAGE, NULL, NULL, NULL, NULL); 408 if (fd < B_OK) 409 return fd; 410 411 // see if it's executable at all 412 status = _kern_access(-1, path, X_OK, false); 413 if (status != B_OK) 414 goto out; 415 416 // read and verify the ELF header 417 418 length = _kern_read(fd, 0, buffer, sizeof(buffer)); 419 if (length < 0) { 420 status = length; 421 goto out; 422 } 423 424 status = elf_verify_header(buffer, length); 425 #ifdef _COMPAT_MODE 426 #ifdef __x86_64__ 427 if (status == B_NOT_AN_EXECUTABLE) 428 status = elf32_verify_header(buffer, length); 429 #else 430 if (status == B_NOT_AN_EXECUTABLE) 431 status = elf64_verify_header(buffer, length); 432 #endif // __x86_64__ 433 #endif // _COMPAT_MODE 434 if (status == B_NOT_AN_EXECUTABLE) { 435 if (!strncmp(buffer, "#!", 2)) { 436 // test for shell scripts 437 char *end; 438 buffer[min_c((size_t)length, sizeof(buffer) - 1)] = '\0'; 439 440 end = strchr(buffer, '\n'); 441 if (end == NULL) { 442 status = E2BIG; 443 goto out; 444 } else 445 end[0] = '\0'; 446 447 if (invoker) { 448 strcpy(invoker, buffer + 2); 449 fixup_shebang(invoker); 450 } 451 452 status = B_OK; 453 } else { 454 // Something odd like a PE? 455 status = pe_verify_header(buffer, length); 456 457 // It is a PE, throw B_UNKNOWN_EXECUTABLE 458 // likely win32 at this point 459 if (status == B_OK) 460 status = B_UNKNOWN_EXECUTABLE; 461 } 462 } else if (status == B_OK) { 463 elf_ehdr *elfHeader = (elf_ehdr *)buffer; 464 if (elfHeader->e_entry == 0) { 465 // we don't like to open shared libraries 466 status = B_NOT_AN_EXECUTABLE; 467 } else if (invoker) 468 invoker[0] = '\0'; 469 } 470 471 out: 472 _kern_close(fd); 473 return status; 474 } 475 476 477 static bool 478 determine_x86_abi(int fd, const Elf32_Ehdr& elfHeader, bool& _isGcc2) 479 { 480 // Unless we're a little-endian CPU, don't bother. We're not x86, so it 481 // doesn't matter all that much whether we can determine the correct gcc 482 // ABI. This saves the code below from having to deal with endianess 483 // conversion. 484 #if B_HOST_IS_LENDIAN 485 486 // Since we don't want to load the complete image, we can't use the 487 // functions that normally determine the Haiku version and ABI. Instead 488 // we'll load the symbol and string tables and resolve the ABI symbol 489 // manually. 490 491 // map the file into memory 492 struct stat st; 493 if (_kern_read_stat(fd, NULL, true, &st, sizeof(st)) != B_OK) 494 return false; 495 496 void* fileBaseAddress; 497 area_id area = _kern_map_file("mapped file", &fileBaseAddress, 498 B_ANY_ADDRESS, st.st_size, B_READ_AREA, REGION_NO_PRIVATE_MAP, false, 499 fd, 0); 500 if (area < 0) 501 return false; 502 503 struct AreaDeleter { 504 AreaDeleter(area_id area) 505 : 506 fArea(area) 507 { 508 } 509 510 ~AreaDeleter() 511 { 512 _kern_delete_area(fArea); 513 } 514 515 private: 516 area_id fArea; 517 } areaDeleter(area); 518 519 // get the section headers 520 if (elfHeader.e_shoff == 0 || elfHeader.e_shentsize < sizeof(Elf32_Shdr)) 521 return false; 522 523 size_t sectionHeadersSize = elfHeader.e_shentsize * elfHeader.e_shnum; 524 if (elfHeader.e_shoff + (off_t)sectionHeadersSize > st.st_size) 525 return false; 526 527 void* sectionHeaders = (uint8*)fileBaseAddress + elfHeader.e_shoff; 528 529 // find the sections we need 530 uint32* symbolHash = NULL; 531 uint32 symbolHashSize = 0; 532 uint32 symbolHashChainSize = 0; 533 Elf32_Sym* symbolTable = NULL; 534 uint32 symbolTableSize = 0; 535 const char* stringTable = NULL; 536 off_t stringTableSize = 0; 537 538 for (int32 i = 0; i < elfHeader.e_shnum; i++) { 539 Elf32_Shdr* sectionHeader 540 = (Elf32_Shdr*)((uint8*)sectionHeaders + i * elfHeader.e_shentsize); 541 if ((off_t)sectionHeader->sh_offset + (off_t)sectionHeader->sh_size 542 > st.st_size) { 543 continue; 544 } 545 546 void* sectionAddress = (uint8*)fileBaseAddress 547 + sectionHeader->sh_offset; 548 549 switch (sectionHeader->sh_type) { 550 case SHT_HASH: 551 symbolHash = (uint32*)sectionAddress; 552 if (sectionHeader->sh_size < (off_t)sizeof(symbolHash[0])) 553 return false; 554 symbolHashSize = symbolHash[0]; 555 symbolHashChainSize 556 = sectionHeader->sh_size / sizeof(symbolHash[0]); 557 if (symbolHashChainSize < symbolHashSize + 2) 558 return false; 559 symbolHashChainSize -= symbolHashSize + 2; 560 break; 561 case SHT_DYNSYM: 562 symbolTable = (Elf32_Sym*)sectionAddress; 563 symbolTableSize = sectionHeader->sh_size; 564 break; 565 case SHT_STRTAB: 566 // .shstrtab has the same type as .dynstr, but it isn't loaded 567 // into memory. 568 if (sectionHeader->sh_addr == 0) 569 continue; 570 stringTable = (const char*)sectionAddress; 571 stringTableSize = (off_t)sectionHeader->sh_size; 572 break; 573 default: 574 continue; 575 } 576 } 577 578 if (symbolHash == NULL || symbolTable == NULL || stringTable == NULL) 579 return false; 580 uint32 symbolCount 581 = std::min(symbolTableSize / (uint32)sizeof(Elf32_Sym), 582 symbolHashChainSize); 583 if (symbolCount < symbolHashSize) 584 return false; 585 586 // look up the ABI symbol 587 const char* name = B_SHARED_OBJECT_HAIKU_ABI_VARIABLE_NAME; 588 size_t nameLength = strlen(name); 589 uint32 bucket = elf_hash(name) % symbolHashSize; 590 591 for (uint32 i = symbolHash[bucket + 2]; i < symbolCount && i != STN_UNDEF; 592 i = symbolHash[2 + symbolHashSize + i]) { 593 Elf32_Sym* symbol = symbolTable + i; 594 if (symbol->st_shndx != SHN_UNDEF 595 && ((symbol->Bind() == STB_GLOBAL) || (symbol->Bind() == STB_WEAK)) 596 && symbol->Type() == STT_OBJECT 597 && (off_t)symbol->st_name + (off_t)nameLength < stringTableSize 598 && strcmp(stringTable + symbol->st_name, name) == 0) { 599 if (symbol->st_value > 0 && symbol->st_size >= sizeof(uint32) 600 && symbol->st_shndx < elfHeader.e_shnum) { 601 Elf32_Shdr* sectionHeader = (Elf32_Shdr*)((uint8*)sectionHeaders 602 + symbol->st_shndx * elfHeader.e_shentsize); 603 if (symbol->st_value >= sectionHeader->sh_addr 604 && symbol->st_value 605 <= sectionHeader->sh_addr + sectionHeader->sh_size) { 606 off_t fileOffset = symbol->st_value - sectionHeader->sh_addr 607 + sectionHeader->sh_offset; 608 if (fileOffset + (off_t)sizeof(uint32) <= st.st_size) { 609 uint32 abi 610 = *(uint32*)((uint8*)fileBaseAddress + fileOffset); 611 _isGcc2 = (abi & B_HAIKU_ABI_MAJOR) 612 == B_HAIKU_ABI_GCC_2; 613 return true; 614 } 615 } 616 } 617 618 return false; 619 } 620 } 621 622 // ABI symbol not found. That means the object pre-dates its introduction 623 // in Haiku. So this is most likely gcc 2. We don't fall back to reading 624 // the comment sections to verify. 625 _isGcc2 = true; 626 return true; 627 #else // not little endian 628 return false; 629 #endif 630 } 631 632 633 static status_t 634 get_executable_architecture(int fd, const char** _architecture) 635 { 636 // Read the ELF header. We read the 32 bit header. Generally the e_machine 637 // field is the last one that interests us and the 64 bit header is still 638 // identical at that point. 639 Elf32_Ehdr elfHeader; 640 ssize_t bytesRead = _kern_read(fd, 0, &elfHeader, sizeof(elfHeader)); 641 if (bytesRead < 0) 642 return bytesRead; 643 if ((size_t)bytesRead != sizeof(elfHeader)) 644 return B_NOT_AN_EXECUTABLE; 645 646 // check whether this is indeed an ELF file 647 if (memcmp(elfHeader.e_ident, ELFMAG, 4) != 0) 648 return B_NOT_AN_EXECUTABLE; 649 650 // check the architecture 651 uint16 machine = elfHeader.e_machine; 652 if ((elfHeader.e_ident[EI_DATA] == ELFDATA2LSB) != (B_HOST_IS_LENDIAN != 0)) 653 machine = (machine >> 8) | (machine << 8); 654 655 const char* architecture = NULL; 656 switch (machine) { 657 case EM_386: 658 case EM_486: 659 { 660 bool isGcc2; 661 if (determine_x86_abi(fd, elfHeader, isGcc2) && isGcc2) 662 architecture = "x86_gcc2"; 663 else 664 architecture = "x86"; 665 break; 666 } 667 case EM_68K: 668 architecture = "m68k"; 669 break; 670 case EM_PPC: 671 architecture = "ppc"; 672 break; 673 case EM_ARM: 674 architecture = "arm"; 675 break; 676 case EM_ARM64: 677 architecture = "arm64"; 678 break; 679 case EM_X86_64: 680 architecture = "x86_64"; 681 break; 682 case EM_RISCV: 683 architecture = "riscv"; 684 break; 685 } 686 687 if (architecture == NULL) 688 return B_NOT_SUPPORTED; 689 690 *_architecture = architecture; 691 return B_OK; 692 } 693 694 695 status_t 696 get_executable_architecture(const char* path, const char** _architecture) 697 { 698 int fd = _kern_open(-1, path, O_RDONLY, 0); 699 if (fd < 0) 700 return fd; 701 702 status_t error = get_executable_architecture(fd, _architecture); 703 704 _kern_close(fd); 705 return error; 706 } 707 708 709 /*! 710 This is the main entry point of the runtime loader as 711 specified by its ld-script. 712 */ 713 int 714 runtime_loader(void* _args, void* commpage) 715 { 716 void *entry = NULL; 717 int returnCode; 718 719 gProgramArgs = (struct user_space_program_args *)_args; 720 __gCommPageAddress = commpage; 721 722 // Relocate the args and env arrays -- they are organized in a contiguous 723 // buffer which the kernel just copied into user space without adjusting the 724 // pointers. 725 { 726 int32 i; 727 addr_t relocationOffset = 0; 728 729 if (gProgramArgs->arg_count > 0) 730 relocationOffset = (addr_t)gProgramArgs->args[0]; 731 else if (gProgramArgs->env_count > 0) 732 relocationOffset = (addr_t)gProgramArgs->env[0]; 733 734 // That's basically: <new buffer address> - <old buffer address>. 735 // It looks a little complicated, since we don't have the latter one at 736 // hand and thus need to reconstruct it (<first string pointer> - 737 // <arguments + environment array sizes>). 738 relocationOffset = (addr_t)gProgramArgs->args - relocationOffset 739 + (gProgramArgs->arg_count + gProgramArgs->env_count + 2) 740 * sizeof(char*); 741 742 for (i = 0; i < gProgramArgs->arg_count; i++) 743 gProgramArgs->args[i] += relocationOffset; 744 745 for (i = 0; i < gProgramArgs->env_count; i++) 746 gProgramArgs->env[i] += relocationOffset; 747 } 748 749 #if DEBUG_RLD 750 close(0); open("/dev/console", 0); /* stdin */ 751 close(1); open("/dev/console", 0); /* stdout */ 752 close(2); open("/dev/console", 0); /* stderr */ 753 #endif 754 755 if (heap_init() < B_OK) 756 return 1; 757 758 rldexport_init(); 759 rldelf_init(); 760 761 load_program(gProgramArgs->program_path, &entry); 762 763 if (entry == NULL) 764 return -1; 765 766 // call the program entry point (usually _start()) 767 returnCode = ((int (*)(int, void *, void *))entry)(gProgramArgs->arg_count, 768 gProgramArgs->args, gProgramArgs->env); 769 770 terminate_program(); 771 772 return returnCode; 773 } 774