path_from_handle.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright 2014-2015 Renato Tegon Forti, Antony Polukhin.
  2. // Copyright Antony Polukhin, 2016-2025.
  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) 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, std::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 uint32_t count = _dyld_image_count(); // not thread safe: other thread my [un]load images
  31. for (uint32_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 = std::make_error_code(
  46. std::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, std::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. #elif BOOST_OS_CYGWIN
  84. // Cygwin returns the opaque pointer-sized handle of type `HMODULE` on the invoke of `dlopen`,
  85. // which cannot be interpreted. As GCC on Cygwin always links to KERNEL32.DLL, we can use the
  86. // standard Win32 API `GetModuleFileNameW` to implement `path_from_handle`
  87. //
  88. // Introduce the Win32 API `GetModuleFileNameW` here
  89. extern "C" void GetModuleFileNameW(void*, wchar_t*, unsigned long long);
  90. // Introduce the Win32 API `GetLastError` here
  91. extern "C" unsigned long long GetLastError();
  92. #else
  93. # include <link.h> // struct link_map
  94. #endif
  95. namespace boost { namespace dll { namespace detail {
  96. #if BOOST_OS_QNX
  97. // Android and QNX miss struct link_map. QNX misses ElfW macro, so avoiding it.
  98. struct link_map {
  99. void *l_addr; // Base address shared object is loaded at
  100. char *l_name; // Absolute file name object was found in
  101. // ... // Ignoring remaning parts of the structure
  102. };
  103. #endif // #if BOOST_OS_QNX
  104. inline boost::dll::fs::path path_from_handle(void* handle, std::error_code &ec) {
  105. // RTLD_DI_LINKMAP (RTLD_DI_ORIGIN returns only folder and is not suitable for this case)
  106. // Obtain the Link_map for the handle that is specified.
  107. // The p argument points to a Link_map pointer (Link_map
  108. // **p). The actual storage for the Link_map structure is
  109. // maintained by ld.so.1.
  110. //
  111. // Unfortunately we can not use `dlinfo(handle, RTLD_DI_LINKMAP, &link_map) < 0`
  112. // because it is not supported on MacOS X 10.3, NetBSD 3.0, OpenBSD 3.8, AIX 5.1,
  113. // HP-UX 11, IRIX 6.5, OSF/1 5.1, Cygwin, mingw, Interix 3.5, BeOS.
  114. // Fortunately, investigating the sources of open source projects brought the understanding, that
  115. // `handle` is just a `struct link_map*` that contains full library name.
  116. #if BOOST_OS_CYGWIN
  117. // Cygwin doesn't have <link.h> header
  118. unsigned long long buffer_size = 4096;
  119. std::vector<wchar_t> buffer;
  120. do
  121. {
  122. buffer.resize(buffer_size);
  123. GetModuleFileNameW(handle, buffer.data(), buffer.size());
  124. buffer_size *= 2;
  125. } while (GetLastError() == 122 /* ERROR_INSUFFICIENT_BUFFER */);
  126. if (GetLastError() == 0)
  127. {
  128. return boost::filesystem::path(buffer.data());
  129. } else
  130. {
  131. boost::dll::detail::reset_dlerror();
  132. ec = std::make_error_code(std::errc::bad_file_descriptor);
  133. return boost::filesystem::path();
  134. }
  135. #else
  136. const struct link_map* link_map = 0;
  137. #if BOOST_OS_BSD_FREE
  138. // FreeBSD has it's own logic http://code.metager.de/source/xref/freebsd/libexec/rtld-elf/rtld.c
  139. // Fortunately it has the dlinfo call.
  140. if (dlinfo(handle, RTLD_DI_LINKMAP, &link_map) < 0) {
  141. link_map = 0;
  142. }
  143. #else
  144. link_map = static_cast<const struct link_map*>(handle);
  145. #endif
  146. if (!link_map) {
  147. boost::dll::detail::reset_dlerror();
  148. ec = std::make_error_code(
  149. std::errc::bad_file_descriptor
  150. );
  151. return boost::dll::fs::path();
  152. }
  153. if (!link_map->l_name || *link_map->l_name == '\0') {
  154. return program_location_impl(ec);
  155. }
  156. return boost::dll::fs::path(link_map->l_name);
  157. #endif
  158. }
  159. }}} // namespace boost::dll::detail
  160. #endif // #if BOOST_OS_MACOS || BOOST_OS_IOS
  161. #endif // BOOST_DLL_DETAIL_POSIX_PATH_FROM_HANDLE_HPP