dlfcn.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <assert.h>
  2. #include <dlfcn.h>
  3. #include <fcntl.h>
  4. #include <mman.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <sys/stat.h>
  8. #include <AK/FileSystemPath.h>
  9. #include <AK/HashMap.h>
  10. #include <AK/RefPtr.h>
  11. #include <AK/ScopeGuard.h>
  12. #include <AK/String.h>
  13. #include <AK/StringBuilder.h>
  14. #include <LibELF/ELFDynamicLoader.h>
  15. // NOTE: The string here should never include a trailing newline (according to POSIX)
  16. String g_dlerror_msg;
  17. HashMap<String, RefPtr<ELFDynamicLoader>> g_elf_objects;
  18. extern "C" {
  19. int dlclose(void*)
  20. {
  21. g_dlerror_msg = "dlclose not implemented!";
  22. return -1;
  23. }
  24. char* dlerror()
  25. {
  26. return const_cast<char*>(g_dlerror_msg.characters());
  27. }
  28. void* dlopen(const char* filename, int flags)
  29. {
  30. // FIXME: Create a global mutex/semaphore/lock for dlopen/dlclose/dlsym and (?) dlerror
  31. // FIXME: refcount?
  32. if (!filename) {
  33. // FIXME: Return the handle for "the main executable"
  34. // The Serenity Kernel will keep a mapping of the main elf binary resident in memory,
  35. // But a future dynamic loader might have a different idea/way of letting us access this information
  36. ASSERT_NOT_REACHED();
  37. }
  38. FileSystemPath file_path(filename);
  39. auto existing_elf_object = g_elf_objects.get(file_path.basename());
  40. if (existing_elf_object.has_value()) {
  41. void* referenced_object = existing_elf_object.value().leak_ref();
  42. return referenced_object;
  43. }
  44. int fd = open(filename, O_RDONLY);
  45. if (!fd) {
  46. g_dlerror_msg = String::format("Unable to open file %s", filename);
  47. return nullptr;
  48. }
  49. ScopeGuard close_fd_guard([fd]() { close(fd); });
  50. struct stat file_stats{};
  51. int ret = fstat(fd, &file_stats);
  52. if (ret < 0) {
  53. g_dlerror_msg = String::format("Unable to stat file %s", filename);
  54. return nullptr;
  55. }
  56. auto loader = ELFDynamicLoader::construct(filename, fd, file_stats.st_size);
  57. if (!loader->is_valid()) {
  58. g_dlerror_msg = String::format("%s is not a valid ELF dynamic shared object!", filename);
  59. return nullptr;
  60. }
  61. if (!loader->load_from_image(flags)) {
  62. g_dlerror_msg = String::format("Failed to load ELF object %s", filename);
  63. return nullptr;
  64. }
  65. g_elf_objects.set(file_path.basename(), move(loader));
  66. g_dlerror_msg = "Successfully loaded ELF object.";
  67. // we have one refcount already
  68. return g_elf_objects.get(file_path.basename()).value().ptr();
  69. }
  70. void* dlsym(void* handle, const char* symbol_name)
  71. {
  72. // FIXME: When called with a NULL handle we're supposed to search every dso in the process... that'll get expensive
  73. ASSERT(handle);
  74. auto* dso = reinterpret_cast<ELFDynamicLoader*>(handle);
  75. void* symbol = dso->symbol_for_name(symbol_name);
  76. if (!symbol) {
  77. g_dlerror_msg = "Symbol not found";
  78. return nullptr;
  79. }
  80. return symbol;
  81. }
  82. } // extern "C"