path_from_handle.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2014-2015 Renato Tegon Forti, Antony Polukhin.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_DLL_DETAIL_POSIX_PATH_FROM_HANDLE_HPP
  7. #define BOOST_DLL_DETAIL_POSIX_PATH_FROM_HANDLE_HPP
  8. #include <boost/config.hpp>
  9. #include <boost/dll/detail/system_error.hpp>
  10. #include <boost/dll/detail/posix/program_location_impl.hpp>
  11. #include <boost/filesystem/path.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::filesystem::path path_from_handle(void* handle, boost::system::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::system::error_code(
  46. boost::system::errc::bad_file_descriptor,
  47. boost::system::generic_category()
  48. );
  49. return boost::filesystem::path();
  50. }
  51. }}} // namespace boost::dll::detail
  52. #elif BOOST_OS_ANDROID
  53. #include <boost/dll/runtime_symbol_info.hpp>
  54. namespace boost { namespace dll { namespace detail {
  55. struct soinfo {
  56. // if defined(__work_around_b_24465209__), then an array of char[128] goes here.
  57. // Unfortunately, __work_around_b_24465209__ is visible only during compilation of Android's linker
  58. const void* phdr;
  59. size_t phnum;
  60. void* entry;
  61. void* base;
  62. // ... // Ignoring remaning parts of the structure
  63. };
  64. inline boost::filesystem::path path_from_handle(const void* handle, boost::system::error_code &ec) {
  65. static const std::size_t work_around_b_24465209__offset = 128;
  66. const struct soinfo* si = reinterpret_cast<const struct soinfo*>(
  67. static_cast<const char*>(handle) + work_around_b_24465209__offset
  68. );
  69. boost::filesystem::path ret = boost::dll::symbol_location_ptr(si->base, ec);
  70. if (ec) {
  71. ec.clear();
  72. si = static_cast<const struct soinfo*>(handle);
  73. return boost::dll::symbol_location_ptr(si->base, ec);
  74. }
  75. return ret;
  76. }
  77. }}} // namespace boost::dll::detail
  78. #else // #if BOOST_OS_MACOS || BOOST_OS_IOS || BOOST_OS_ANDROID
  79. // for dlinfo
  80. #include <dlfcn.h>
  81. #if BOOST_OS_QNX
  82. // QNX's copy of <elf.h> and <link.h> reside in sys folder
  83. # include <sys/link.h>
  84. #else
  85. # include <link.h> // struct link_map
  86. #endif
  87. namespace boost { namespace dll { namespace detail {
  88. #if BOOST_OS_QNX
  89. // Android and QNX miss struct link_map. QNX misses ElfW macro, so avoiding it.
  90. struct link_map {
  91. void *l_addr; // Base address shared object is loaded at
  92. char *l_name; // Absolute file name object was found in
  93. // ... // Ignoring remaning parts of the structure
  94. };
  95. #endif // #if BOOST_OS_QNX
  96. inline boost::filesystem::path path_from_handle(void* handle, boost::system::error_code &ec) {
  97. // RTLD_DI_LINKMAP (RTLD_DI_ORIGIN returns only folder and is not suitable for this case)
  98. // Obtain the Link_map for the handle that is specified.
  99. // The p argument points to a Link_map pointer (Link_map
  100. // **p). The actual storage for the Link_map structure is
  101. // maintained by ld.so.1.
  102. //
  103. // Unfortunately we can not use `dlinfo(handle, RTLD_DI_LINKMAP, &link_map) < 0`
  104. // because it is not supported on MacOS X 10.3, NetBSD 3.0, OpenBSD 3.8, AIX 5.1,
  105. // HP-UX 11, IRIX 6.5, OSF/1 5.1, Cygwin, mingw, Interix 3.5, BeOS.
  106. // Fortunately investigating the sources of open source projects brought the understanding, that
  107. // `handle` is just a `struct link_map*` that contains full library name.
  108. const struct link_map* link_map = 0;
  109. #if BOOST_OS_BSD_FREE
  110. // FreeBSD has it's own logic http://code.metager.de/source/xref/freebsd/libexec/rtld-elf/rtld.c
  111. // Fortunately it has the dlinfo call.
  112. if (dlinfo(handle, RTLD_DI_LINKMAP, &link_map) < 0) {
  113. link_map = 0;
  114. }
  115. #else
  116. link_map = static_cast<const struct link_map*>(handle);
  117. #endif
  118. if (!link_map) {
  119. boost::dll::detail::reset_dlerror();
  120. ec = boost::system::error_code(
  121. boost::system::errc::bad_file_descriptor,
  122. boost::system::generic_category()
  123. );
  124. return boost::filesystem::path();
  125. }
  126. if (!link_map->l_name || *link_map->l_name == '\0') {
  127. return program_location_impl(ec);
  128. }
  129. return boost::filesystem::path(link_map->l_name);
  130. }
  131. }}} // namespace boost::dll::detail
  132. #endif // #if BOOST_OS_MACOS || BOOST_OS_IOS
  133. #endif // BOOST_DLL_DETAIL_POSIX_PATH_FROM_HANDLE_HPP