main.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ScopeGuard.h>
  7. #include <Kernel/API/POSIX/sys/stat.h>
  8. #include <LibELF/AuxiliaryVector.h>
  9. #include <LibELF/DynamicLinker.h>
  10. #include <LibELF/Relocation.h>
  11. #include <fcntl.h>
  12. #include <sys/internals.h>
  13. #include <syscall.h>
  14. #include <unistd.h>
  15. char* __static_environ[] = { nullptr }; // We don't get the environment without some libc workarounds..
  16. char** environ = __static_environ;
  17. uintptr_t __stack_chk_guard = 0;
  18. static void perform_self_relocations(auxv_t* auxvp)
  19. {
  20. // We need to relocate ourselves.
  21. // (these relocations seem to be generated because of our vtables)
  22. FlatPtr base_address = 0;
  23. bool found_base_address = false;
  24. for (; auxvp->a_type != AT_NULL; ++auxvp) {
  25. if (auxvp->a_type == ELF::AuxiliaryValue::BaseAddress) {
  26. base_address = auxvp->a_un.a_val;
  27. found_base_address = true;
  28. }
  29. }
  30. VERIFY(found_base_address);
  31. if (!ELF::perform_relative_relocations(base_address))
  32. exit(1);
  33. }
  34. static void display_help()
  35. {
  36. char const message[] =
  37. R"(You have invoked `Loader.so'. This is the helper program for programs that
  38. use shared libraries. Special directives embedded in executables tell the
  39. kernel to load this program.
  40. This helper program loads the shared libraries needed by the program,
  41. prepares the program to run, and runs it. You do not need to invoke
  42. this helper program directly. If you still want to run a program with the loader,
  43. run: /usr/lib/Loader.so [ELF_BINARY])";
  44. fprintf(stderr, "%s", message);
  45. }
  46. extern "C" {
  47. // The compiler expects a previous declaration
  48. void _start(int, char**, char**) __attribute__((used));
  49. void _entry(int, char**, char**) __attribute__((used));
  50. NAKED void _start(int, char**, char**)
  51. {
  52. #if ARCH(AARCH64)
  53. // Make sure backtrace computation stops here by setting FP and LR to 0.
  54. // FIXME: The kernel should ensure that registers are zeroed on program start
  55. asm(
  56. "mov x29, 0\n"
  57. "mov x30, 0\n"
  58. "bl _entry\n");
  59. #elif ARCH(RISCV64)
  60. asm(
  61. "li fp, 0\n"
  62. "li ra, 0\n"
  63. "tail _entry@plt\n");
  64. #elif ARCH(X86_64)
  65. asm(
  66. "push $0\n"
  67. "jmp _entry@plt\n");
  68. #else
  69. # error "Unknown architecture"
  70. #endif
  71. }
  72. static ErrorOr<int> open_executable(char const* path)
  73. {
  74. int rc = open(path, O_RDONLY | O_EXEC);
  75. if (rc < 0)
  76. return Error::from_errno(errno);
  77. int checked_fd = rc;
  78. ArmedScopeGuard close_on_failure([checked_fd] {
  79. close(checked_fd);
  80. });
  81. struct stat executable_stat = {};
  82. rc = fstat(checked_fd, &executable_stat);
  83. if (rc < 0)
  84. return Error::from_errno(errno);
  85. if (!S_ISREG(executable_stat.st_mode)) {
  86. if (S_ISDIR(executable_stat.st_mode))
  87. return Error::from_errno(EISDIR);
  88. return Error::from_errno(EINVAL);
  89. }
  90. close_on_failure.disarm();
  91. return checked_fd;
  92. }
  93. ALWAYS_INLINE static void optimizer_fence()
  94. {
  95. asm("" ::: "memory");
  96. }
  97. void _entry(int argc, char** argv, char** envp)
  98. {
  99. char** env;
  100. for (env = envp; *env; ++env) {
  101. }
  102. auxv_t* auxvp = (auxv_t*)++env;
  103. bool at_random_found = false;
  104. for (auxv_t* entry = auxvp; entry->a_type != AT_NULL; ++entry) {
  105. if (entry->a_type == AT_RANDOM) {
  106. at_random_found = true;
  107. __stack_chk_guard = *reinterpret_cast<u64*>(entry->a_un.a_ptr);
  108. break;
  109. }
  110. }
  111. VERIFY(at_random_found);
  112. // Make sure compiler won't move any functions calls above __stack_chk_guard initialization even
  113. // if their definitions somehow become available.
  114. optimizer_fence();
  115. perform_self_relocations(auxvp);
  116. // Similarly, make sure no non-offset-agnostic language features are used above this point.
  117. optimizer_fence();
  118. // Initialize the copy of libc included statically in Loader.so,
  119. // initialization of the dynamic libc.so is done by the DynamicLinker
  120. __libc_init();
  121. int main_program_fd = -1;
  122. ByteString main_program_path;
  123. bool is_secure = false;
  124. for (; auxvp->a_type != AT_NULL; ++auxvp) {
  125. if (auxvp->a_type == ELF::AuxiliaryValue::ExecFileDescriptor) {
  126. main_program_fd = auxvp->a_un.a_val;
  127. }
  128. if (auxvp->a_type == ELF::AuxiliaryValue::ExecFilename) {
  129. main_program_path = (char const*)auxvp->a_un.a_ptr;
  130. }
  131. if (auxvp->a_type == ELF::AuxiliaryValue::Secure) {
  132. is_secure = auxvp->a_un.a_val == 1;
  133. }
  134. }
  135. if (main_program_fd == -1) {
  136. // We've been invoked directly as an executable rather than as the
  137. // ELF interpreter for some other binary. The second argv string should
  138. // be the path to the ELF executable, and if we don't have enough strings in argv
  139. // (argc < 2), just fail with message to stderr about this.
  140. if (argc < 2) {
  141. display_help();
  142. _exit(1);
  143. }
  144. auto error_or_fd = open_executable(argv[1]);
  145. if (error_or_fd.is_error()) {
  146. warnln("Loader.so: Loading {} failed: {}", argv[1], strerror(error_or_fd.error().code()));
  147. _exit(1);
  148. }
  149. main_program_fd = error_or_fd.release_value();
  150. main_program_path = argv[1];
  151. argv++;
  152. argc--;
  153. }
  154. VERIFY(main_program_fd >= 0);
  155. VERIFY(!main_program_path.is_empty());
  156. ELF::DynamicLinker::linker_main(move(main_program_path), main_program_fd, is_secure, argc, argv, envp);
  157. VERIFY_NOT_REACHED();
  158. }
  159. }