path_from_handle.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2014-2015 Renato Tegon Forti, Antony Polukhin.
  2. // Copyright 2016-2019 Antony Polukhin.
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_DLL_DETAIL_POSIX_PATH_FROM_HANDLE_HPP
  8. #define BOOST_DLL_DETAIL_POSIX_PATH_FROM_HANDLE_HPP
  9. #include <boost/dll/config.hpp>
  10. #include <boost/dll/detail/system_error.hpp>
  11. #include <boost/dll/detail/posix/program_location_impl.hpp>
  12. #include <boost/predef/os.h>
  13. #ifdef BOOST_HAS_PRAGMA_ONCE
  14. # pragma once
  15. #endif
  16. #if BOOST_OS_MACOS || BOOST_OS_IOS
  17. # include <mach-o/dyld.h>
  18. # include <mach-o/nlist.h>
  19. # include <cstddef> // for std::ptrdiff_t
  20. namespace boost { namespace dll { namespace detail {
  21. inline void* strip_handle(void* handle) BOOST_NOEXCEPT {
  22. return reinterpret_cast<void*>(
  23. (reinterpret_cast<std::ptrdiff_t>(handle) >> 2) << 2
  24. );
  25. }
  26. inline boost::dll::fs::path path_from_handle(void* handle, boost::dll::fs::error_code &ec) {
  27. handle = strip_handle(handle);
  28. // Iterate through all images currently in memory
  29. // https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/dyld.3.html
  30. const std::size_t count = _dyld_image_count(); // not thread safe: other thread my [un]load images
  31. for (std::size_t i = 0; i <= count; ++i) {
  32. // on last iteration `i` is equal to `count` which is out of range, so `_dyld_get_image_name`
  33. // will return NULL. `dlopen(NULL, RTLD_LAZY)` call will open the current executable.
  34. const char* image_name = _dyld_get_image_name(i);
  35. // dlopen/dlclose must not affect `_dyld_image_count()`, because libraries are already loaded and only the internal counter is affected
  36. void* probe_handle = dlopen(image_name, RTLD_LAZY);
  37. dlclose(probe_handle);
  38. // If the handle is the same as what was passed in (modulo mode bits), return this image name
  39. if (handle == strip_handle(probe_handle)) {
  40. boost::dll::detail::reset_dlerror();
  41. return image_name;
  42. }
  43. }
  44. boost::dll::detail::reset_dlerror();
  45. ec = boost::dll::fs::make_error_code(
  46. boost::dll::fs::errc::bad_file_descriptor
  47. );
  48. return boost::dll::fs::path();
  49. }
  50. }}} // namespace boost::dll::detail
  51. #elif BOOST_OS_ANDROID
  52. #include <boost/dll/runtime_symbol_info.hpp>
  53. namespace boost { namespace dll { namespace detail {
  54. struct soinfo {
  55. // if defined(__work_around_b_24465209__), then an array of char[128] goes here.
  56. // Unfortunately, __work_around_b_24465209__ is visible only during compilation of Android's linker
  57. const void* phdr;
  58. size_t phnum;
  59. void* entry;
  60. void* base;
  61. // ... // Ignoring remaning parts of the structure
  62. };
  63. inline boost::dll::fs::path path_from_handle(const void* handle, boost::dll::fs::error_code &ec) {
  64. static const std::size_t work_around_b_24465209__offset = 128;
  65. const struct soinfo* si = reinterpret_cast<const struct soinfo*>(
  66. static_cast<const char*>(handle) + work_around_b_24465209__offset
  67. );
  68. boost::dll::fs::path ret = boost::dll::symbol_location_ptr(si->base, ec);
  69. if (ec) {
  70. ec.clear();
  71. si = static_cast<const struct soinfo*>(handle);
  72. return boost::dll::symbol_location_ptr(si->base, ec);
  73. }
  74. return ret;
  75. }
  76. }}} // namespace boost::dll::detail
  77. #else // #if BOOST_OS_MACOS || BOOST_OS_IOS || BOOST_OS_ANDROID
  78. // for dlinfo
  79. #include <dlfcn.h>
  80. #if BOOST_OS_QNX
  81. // QNX's copy of <elf.h> and <link.h> reside in sys folder
  82. # include <sys/link.h>
  83. #else
  84. # include <link.h> // struct link_map
  85. #endif
  86. namespace boost { namespace dll { namespace detail {
  87. #if BOOST_OS_QNX
  88. // Android and QNX miss struct link_map. QNX misses ElfW macro, so avoiding it.
  89. struct link_map {
  90. void *l_addr; // Base address shared object is loaded at
  91. char *l_name; // Absolute file name object was found in
  92. // ... // Ignoring remaning parts of the structure
  93. };
  94. #endif // #if BOOST_OS_QNX
  95. inline boost::dll::fs::path path_from_handle(void* handle, boost::dll::fs::error_code &ec) {
  96. // RTLD_DI_LINKMAP (RTLD_DI_ORIGIN returns only folder and is not suitable for this case)
  97. // Obtain the Link_map for the handle that is specified.
  98. // The p argument points to a Link_map pointer (Link_map
  99. // **p). The actual storage for the Link_map structure is
  100. // maintained by ld.so.1.
  101. //
  102. // Unfortunately we can not use `dlinfo(handle, RTLD_DI_LINKMAP, &link_map) < 0`
  103. // because it is not supported on MacOS X 10.3, NetBSD 3.0, OpenBSD 3.8, AIX 5.1,
  104. // HP-UX 11, IRIX 6.5, OSF/1 5.1, Cygwin, mingw, Interix 3.5, BeOS.
  105. // Fortunately investigating the sources of open source projects brought the understanding, that
  106. // `handle` is just a `struct link_map*` that contains full library name.
  107. const struct link_map* link_map = 0;
  108. #if BOOST_OS_BSD_FREE
  109. // FreeBSD has it's own logic http://code.metager.de/source/xref/freebsd/libexec/rtld-elf/rtld.c
  110. // Fortunately it has the dlinfo call.
  111. if (dlinfo(handle, RTLD_DI_LINKMAP, &link_map) < 0) {
  112. link_map = 0;
  113. }
  114. #else
  115. link_map = static_cast<const struct link_map*>(handle);
  116. #endif
  117. if (!link_map) {
  118. boost::dll::detail::reset_dlerror();
  119. ec = boost::dll::fs::make_error_code(
  120. boost::dll::fs::errc::bad_file_descriptor
  121. );
  122. return boost::dll::fs::path();
  123. }
  124. if (!link_map->l_name || *link_map->l_name == '\0') {
  125. return program_location_impl(ec);
  126. }
  127. return boost::dll::fs::path(link_map->l_name);
  128. }
  129. }}} // namespace boost::dll::detail
  130. #endif // #if BOOST_OS_MACOS || BOOST_OS_IOS
  131. #endif // BOOST_DLL_DETAIL_POSIX_PATH_FROM_HANDLE_HPP