module.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/FileSystem/FileDescription.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. if (!is_superuser())
  17. return EPERM;
  18. REQUIRE_NO_PROMISES;
  19. auto path = get_syscall_path_argument(user_path, path_length);
  20. if (path.is_error())
  21. return path.error();
  22. auto description_or_error = VFS::the().open(path.value()->view(), O_RDONLY, 0, current_directory());
  23. if (description_or_error.is_error())
  24. return description_or_error.error();
  25. auto& description = description_or_error.value();
  26. auto payload_or_error = description->read_entire_file();
  27. if (payload_or_error.is_error())
  28. return payload_or_error.error();
  29. auto& payload = *payload_or_error.value();
  30. auto storage = KBuffer::create_with_size(payload.size());
  31. memcpy(storage.data(), payload.data(), payload.size());
  32. auto elf_image = try_make<ELF::Image>(storage.data(), storage.size());
  33. if (!elf_image)
  34. return ENOMEM;
  35. if (!elf_image->parse())
  36. return ENOEXEC;
  37. HashMap<String, u8*> section_storage_by_name;
  38. auto module = try_make<Module>();
  39. if (!module)
  40. return ENOMEM;
  41. elf_image->for_each_section_of_type(SHT_PROGBITS, [&](const ELF::Image::Section& section) {
  42. if (!section.size())
  43. return;
  44. auto section_storage = KBuffer::copy(section.raw_data(), section.size(), Region::Access::Read | Region::Access::Write | Region::Access::Execute);
  45. section_storage_by_name.set(section.name(), section_storage.data());
  46. module->sections.append(move(section_storage));
  47. });
  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. if (!is_superuser())
  126. return EPERM;
  127. REQUIRE_NO_PROMISES;
  128. auto module_name = copy_string_from_user(user_name, name_length);
  129. if (module_name.is_null())
  130. return EFAULT;
  131. auto it = g_modules->find(module_name);
  132. if (it == g_modules->end())
  133. return ENOENT;
  134. if (it->value->module_fini)
  135. it->value->module_fini();
  136. g_modules->remove(it);
  137. return 0;
  138. }
  139. }