main.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibC/sys/internals.h>
  7. #include <LibC/unistd.h>
  8. #include <LibELF/AuxiliaryVector.h>
  9. #include <LibELF/DynamicLinker.h>
  10. #include <LibELF/Relocation.h>
  11. char* __static_environ[] = { nullptr }; // We don't get the environment without some libc workarounds..
  12. static void init_libc()
  13. {
  14. environ = __static_environ;
  15. __environ_is_malloced = false;
  16. __stdio_is_initialized = false;
  17. // Initialise the copy of libc included statically in Loader.so,
  18. // initialisation of the dynamic libc.so is done by the DynamicLinker
  19. __libc_init();
  20. }
  21. static void perform_self_relocations(auxv_t* auxvp)
  22. {
  23. // We need to relocate ourselves.
  24. // (these relocations seem to be generated because of our vtables)
  25. FlatPtr base_address = 0;
  26. bool found_base_address = false;
  27. for (; auxvp->a_type != AT_NULL; ++auxvp) {
  28. if (auxvp->a_type == ELF::AuxiliaryValue::BaseAddress) {
  29. base_address = auxvp->a_un.a_val;
  30. found_base_address = true;
  31. }
  32. }
  33. VERIFY(found_base_address);
  34. if (!ELF::perform_relative_relocations(base_address))
  35. exit(1);
  36. }
  37. static void display_help()
  38. {
  39. const char message[] =
  40. R"(You have invoked `Loader.so'. This is the helper program for programs that
  41. use shared libraries. Special directives embedded in executables tell the
  42. kernel to load this program.
  43. This helper program loads the shared libraries needed by the program,
  44. prepares the program to run, and runs it. You do not need to invoke
  45. this helper program directly.
  46. )";
  47. fprintf(stderr, "%s", message);
  48. }
  49. extern "C" {
  50. // The compiler expects a previous declaration
  51. void _start(int, char**, char**) __attribute__((used));
  52. void _entry(int, char**, char**) __attribute__((used));
  53. NAKED void _start(int, char**, char**)
  54. {
  55. asm(
  56. "push $0\n"
  57. "jmp _entry@plt\n");
  58. }
  59. void _entry(int argc, char** argv, char** envp)
  60. {
  61. char** env;
  62. for (env = envp; *env; ++env) {
  63. }
  64. auxv_t* auxvp = (auxv_t*)++env;
  65. perform_self_relocations(auxvp);
  66. init_libc();
  67. int main_program_fd = -1;
  68. String main_program_name;
  69. bool is_secure = false;
  70. for (; auxvp->a_type != AT_NULL; ++auxvp) {
  71. if (auxvp->a_type == ELF::AuxiliaryValue::ExecFileDescriptor) {
  72. main_program_fd = auxvp->a_un.a_val;
  73. }
  74. if (auxvp->a_type == ELF::AuxiliaryValue::ExecFilename) {
  75. main_program_name = (const char*)auxvp->a_un.a_ptr;
  76. }
  77. if (auxvp->a_type == ELF::AuxiliaryValue::Secure) {
  78. is_secure = auxvp->a_un.a_val == 1;
  79. }
  80. }
  81. if (main_program_name == "/usr/lib/Loader.so"sv) {
  82. // We've been invoked directly as an executable rather than as the
  83. // ELF interpreter for some other binary. In the future we may want
  84. // to support launching a program directly from the dynamic loader
  85. // like ld.so on Linux.
  86. display_help();
  87. _exit(1);
  88. }
  89. VERIFY(main_program_fd >= 0);
  90. VERIFY(!main_program_name.is_empty());
  91. ELF::DynamicLinker::linker_main(move(main_program_name), main_program_fd, is_secure, argc, argv, envp);
  92. VERIFY_NOT_REACHED();
  93. }
  94. }