module.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/FileSystem/OpenFileDescription.h>
  7. #include <Kernel/FileSystem/VirtualFileSystem.h>
  8. #include <Kernel/KSyms.h>
  9. #include <Kernel/Module.h>
  10. #include <Kernel/Process.h>
  11. #include <LibELF/Image.h>
  12. namespace Kernel {
  13. extern HashMap<String, OwnPtr<Module>>* g_modules;
  14. KResultOr<FlatPtr> Process::sys$module_load(Userspace<const char*> user_path, size_t path_length)
  15. {
  16. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  17. if (!is_superuser())
  18. return EPERM;
  19. REQUIRE_NO_PROMISES;
  20. auto path = TRY(get_syscall_path_argument(user_path, path_length));
  21. auto description = TRY(VirtualFileSystem::the().open(path->view(), O_RDONLY, 0, current_directory()));
  22. auto payload = TRY(description->read_entire_file());
  23. auto storage = TRY(KBuffer::try_create_with_bytes(ReadonlyBytes { payload->data(), payload->size() }));
  24. auto elf_image = try_make<ELF::Image>(storage->data(), storage->size());
  25. if (!elf_image)
  26. return ENOMEM;
  27. if (!elf_image->parse())
  28. return ENOEXEC;
  29. HashMap<String, u8*> section_storage_by_name;
  30. auto module = try_make<Module>();
  31. if (!module)
  32. return ENOMEM;
  33. KResult section_loading_result = KSuccess;
  34. elf_image->for_each_section_of_type(SHT_PROGBITS, [&](const ELF::Image::Section& section) {
  35. if (!section.size() || !section_loading_result.is_error())
  36. return;
  37. auto section_storage_or_error = KBuffer::try_create_with_bytes(ReadonlyBytes { section.raw_data(), section.size() }, Memory::Region::Access::ReadWriteExecute);
  38. if (section_storage_or_error.is_error()) {
  39. section_loading_result = section_storage_or_error.error();
  40. return;
  41. }
  42. auto section_storage = section_storage_or_error.release_value();
  43. section_storage_by_name.set(section.name(), section_storage->data());
  44. module->sections.append(move(section_storage));
  45. });
  46. if (section_loading_result.is_error())
  47. return section_loading_result;
  48. bool missing_symbols = false;
  49. elf_image->for_each_section_of_type(SHT_PROGBITS, [&](const ELF::Image::Section& section) {
  50. if (!section.size())
  51. return;
  52. auto* section_storage = section_storage_by_name.get(section.name()).value_or(nullptr);
  53. VERIFY(section_storage);
  54. auto relocations = section.relocations();
  55. VERIFY(relocations.has_value());
  56. relocations->for_each_relocation([&](const ELF::Image::Relocation& relocation) {
  57. auto& patch_ptr = *reinterpret_cast<ptrdiff_t*>(section_storage + relocation.offset());
  58. switch (relocation.type()) {
  59. case R_386_PC32: {
  60. // PC-relative relocation
  61. dbgln("PC-relative relocation: {}", relocation.symbol().name());
  62. auto symbol_address = address_for_kernel_symbol(relocation.symbol().name());
  63. if (symbol_address == 0)
  64. missing_symbols = true;
  65. dbgln(" Symbol address: {:p}", symbol_address);
  66. ptrdiff_t relative_offset = (FlatPtr)symbol_address - ((FlatPtr)&patch_ptr + 4);
  67. patch_ptr = relative_offset;
  68. break;
  69. }
  70. case R_386_32: // Absolute relocation
  71. dbgln("Absolute relocation: '{}' value={}, index={}", relocation.symbol().name(), relocation.symbol().value(), relocation.symbol_index());
  72. if (relocation.symbol().bind() == STB_LOCAL) {
  73. auto* section_storage_containing_symbol = section_storage_by_name.get(relocation.symbol().section().name()).value_or(nullptr);
  74. VERIFY(section_storage_containing_symbol);
  75. u32 symbol_address = (ptrdiff_t)(section_storage_containing_symbol + relocation.symbol().value());
  76. if (symbol_address == 0)
  77. missing_symbols = true;
  78. dbgln(" Symbol address: {:p}", symbol_address);
  79. patch_ptr += symbol_address;
  80. } else if (relocation.symbol().bind() == STB_GLOBAL) {
  81. u32 symbol_address = address_for_kernel_symbol(relocation.symbol().name());
  82. if (symbol_address == 0)
  83. missing_symbols = true;
  84. dbgln(" Symbol address: {:p}", symbol_address);
  85. patch_ptr += symbol_address;
  86. } else {
  87. VERIFY_NOT_REACHED();
  88. }
  89. break;
  90. }
  91. });
  92. });
  93. if (missing_symbols)
  94. return EINVAL;
  95. auto* text_base = section_storage_by_name.get(".text").value_or(nullptr);
  96. if (!text_base) {
  97. dbgln("No .text section found in module!");
  98. return EINVAL;
  99. }
  100. elf_image->for_each_symbol([&](const ELF::Image::Symbol& symbol) {
  101. dbgln(" - {} '{}' @ {:p}, size={}", symbol.type(), symbol.name(), symbol.value(), symbol.size());
  102. if (symbol.name() == "module_init") {
  103. module->module_init = (ModuleInitPtr)(text_base + symbol.value());
  104. } else if (symbol.name() == "module_fini") {
  105. module->module_fini = (ModuleFiniPtr)(text_base + symbol.value());
  106. } else if (symbol.name() == "module_name") {
  107. const u8* storage = section_storage_by_name.get(symbol.section().name()).value_or(nullptr);
  108. if (storage)
  109. module->name = String((const char*)(storage + symbol.value()));
  110. }
  111. });
  112. if (!module->module_init)
  113. return EINVAL;
  114. if (g_modules->contains(module->name)) {
  115. dbgln("a module with the name {} is already loaded; please unload it first", module->name);
  116. return EEXIST;
  117. }
  118. module->module_init();
  119. auto name = module->name;
  120. g_modules->set(name, move(module));
  121. return 0;
  122. }
  123. KResultOr<FlatPtr> Process::sys$module_unload(Userspace<const char*> user_name, size_t name_length)
  124. {
  125. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  126. if (!is_superuser())
  127. return EPERM;
  128. REQUIRE_NO_PROMISES;
  129. auto module_name = TRY(try_copy_kstring_from_user(user_name, name_length));
  130. auto it = g_modules->find(module_name->view());
  131. if (it == g_modules->end())
  132. return ENOENT;
  133. if (it->value->module_fini)
  134. it->value->module_fini();
  135. g_modules->remove(it);
  136. return 0;
  137. }
  138. }