dlfcn.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/HashMap.h>
  27. #include <AK/LexicalPath.h>
  28. #include <AK/RefPtr.h>
  29. #include <AK/ScopeGuard.h>
  30. #include <AK/String.h>
  31. #include <AK/StringBuilder.h>
  32. #include <LibELF/DynamicLoader.h>
  33. #include <assert.h>
  34. #include <dlfcn.h>
  35. #include <fcntl.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. // NOTE: The string here should never include a trailing newline (according to POSIX)
  39. String g_dlerror_msg;
  40. HashMap<String, RefPtr<ELF::DynamicLoader>> g_elf_objects;
  41. extern "C" {
  42. int dlclose(void*)
  43. {
  44. g_dlerror_msg = "dlclose not implemented!";
  45. return -1;
  46. }
  47. char* dlerror()
  48. {
  49. return const_cast<char*>(g_dlerror_msg.characters());
  50. }
  51. void* dlopen(const char* filename, int flags)
  52. {
  53. // FIXME: Create a global mutex/semaphore/lock for dlopen/dlclose/dlsym and (?) dlerror
  54. // FIXME: refcount?
  55. if (!filename) {
  56. // FIXME: Return the handle for "the main executable"
  57. // The Serenity Kernel will keep a mapping of the main elf binary resident in memory,
  58. // But a future dynamic loader might have a different idea/way of letting us access this information
  59. TODO();
  60. }
  61. auto basename = LexicalPath(filename).basename();
  62. auto existing_elf_object = g_elf_objects.get(basename);
  63. if (existing_elf_object.has_value()) {
  64. return const_cast<ELF::DynamicLoader*>(existing_elf_object.value());
  65. }
  66. int fd = open(filename, O_RDONLY);
  67. if (fd < 0) {
  68. g_dlerror_msg = String::formatted("Unable to open file {}", filename);
  69. return nullptr;
  70. }
  71. ScopeGuard close_fd_guard([fd]() { close(fd); });
  72. auto loader = ELF::DynamicLoader::try_create(fd, filename);
  73. if (!loader || !loader->is_valid()) {
  74. g_dlerror_msg = String::formatted("{} is not a valid ELF dynamic shared object!", filename);
  75. return nullptr;
  76. }
  77. if (!loader->load_from_image(flags,
  78. 0 // total_tls_size = 0, FIXME: Support TLS when using dlopen()
  79. )) {
  80. g_dlerror_msg = String::formatted("Failed to load ELF object {}", filename);
  81. return nullptr;
  82. }
  83. g_elf_objects.set(basename, move(loader));
  84. g_dlerror_msg = "Successfully loaded ELF object.";
  85. // we have one refcount already
  86. return const_cast<ELF::DynamicLoader*>(g_elf_objects.get(basename).value());
  87. }
  88. void* dlsym(void* handle, const char* symbol_name)
  89. {
  90. // FIXME: When called with a NULL handle we're supposed to search every dso in the process... that'll get expensive
  91. ASSERT(handle);
  92. auto* dso = reinterpret_cast<ELF::DynamicLoader*>(handle);
  93. void* symbol = dso->symbol_for_name(symbol_name);
  94. if (!symbol) {
  95. g_dlerror_msg = "Symbol not found";
  96. return nullptr;
  97. }
  98. return symbol;
  99. }
  100. } // extern "C"