Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:43

0001 // Copyright 2014-2015 Renato Tegon Forti, Antony Polukhin.
0002 // Copyright Antony Polukhin, 2016-2023.
0003 //
0004 // Distributed under the Boost Software License, Version 1.0.
0005 // (See accompanying file LICENSE_1_0.txt
0006 // or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 
0008 #ifndef BOOST_DLL_DETAIL_POSIX_PATH_FROM_HANDLE_HPP
0009 #define BOOST_DLL_DETAIL_POSIX_PATH_FROM_HANDLE_HPP
0010 
0011 #include <boost/dll/config.hpp>
0012 #include <boost/dll/detail/system_error.hpp>
0013 #include <boost/dll/detail/posix/program_location_impl.hpp>
0014 #include <boost/predef/os.h>
0015 
0016 #ifdef BOOST_HAS_PRAGMA_ONCE
0017 # pragma once
0018 #endif
0019 
0020 #if BOOST_OS_MACOS || BOOST_OS_IOS
0021 
0022 #   include <mach-o/dyld.h>
0023 #   include <mach-o/nlist.h>
0024 #   include <cstddef> // for std::ptrdiff_t
0025 
0026 namespace boost { namespace dll { namespace detail {
0027     inline void* strip_handle(void* handle) BOOST_NOEXCEPT {
0028         return reinterpret_cast<void*>(
0029             (reinterpret_cast<std::ptrdiff_t>(handle) >> 2) << 2
0030         );
0031     }
0032 
0033     inline boost::dll::fs::path path_from_handle(void* handle, boost::dll::fs::error_code &ec) {
0034         handle = strip_handle(handle);
0035 
0036         // Iterate through all images currently in memory
0037         // https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/dyld.3.html
0038         const uint32_t count = _dyld_image_count(); // not thread safe: other thread my [un]load images
0039         for (uint32_t i = 0; i <= count; ++i) {
0040             // on last iteration `i` is equal to `count` which is out of range, so `_dyld_get_image_name`
0041             // will return NULL. `dlopen(NULL, RTLD_LAZY)` call will open the current executable.
0042             const char* image_name = _dyld_get_image_name(i);
0043 
0044             // dlopen/dlclose must not affect `_dyld_image_count()`, because libraries are already loaded and only the internal counter is affected
0045             void* probe_handle = dlopen(image_name, RTLD_LAZY);
0046             dlclose(probe_handle);
0047 
0048             // If the handle is the same as what was passed in (modulo mode bits), return this image name
0049             if (handle == strip_handle(probe_handle)) {
0050                 boost::dll::detail::reset_dlerror();
0051                 return image_name;
0052             }
0053         }
0054 
0055         boost::dll::detail::reset_dlerror();
0056         ec = boost::dll::fs::make_error_code(
0057             boost::dll::fs::errc::bad_file_descriptor
0058         );
0059 
0060         return boost::dll::fs::path();
0061     }
0062 
0063 }}} // namespace boost::dll::detail
0064 
0065 #elif BOOST_OS_ANDROID
0066 
0067 #include <boost/dll/runtime_symbol_info.hpp>
0068 
0069 namespace boost { namespace dll { namespace detail {
0070 
0071     struct soinfo {
0072         // if defined(__work_around_b_24465209__), then an array of char[128] goes here.
0073         // Unfortunately, __work_around_b_24465209__ is visible only during compilation of Android's linker
0074         const void* phdr;
0075         size_t      phnum;
0076         void*       entry;
0077         void*       base;
0078         // ...          // Ignoring remaning parts of the structure
0079     };
0080 
0081     inline boost::dll::fs::path path_from_handle(const void* handle, boost::dll::fs::error_code &ec) {
0082         static const std::size_t work_around_b_24465209__offset = 128;
0083         const struct soinfo* si = reinterpret_cast<const struct soinfo*>(
0084             static_cast<const char*>(handle) + work_around_b_24465209__offset
0085         );
0086         boost::dll::fs::path ret = boost::dll::symbol_location_ptr(si->base, ec);
0087 
0088         if (ec) {
0089             ec.clear();
0090             si = static_cast<const struct soinfo*>(handle);
0091             return boost::dll::symbol_location_ptr(si->base, ec);
0092         }
0093 
0094         return ret;
0095     }
0096 
0097 }}} // namespace boost::dll::detail
0098 
0099 #else // #if BOOST_OS_MACOS || BOOST_OS_IOS || BOOST_OS_ANDROID
0100 
0101 // for dlinfo
0102 #include <dlfcn.h>
0103 
0104 #if BOOST_OS_QNX
0105 // QNX's copy of <elf.h> and <link.h> reside in sys folder
0106 #   include <sys/link.h>
0107 #else
0108 #   include <link.h>    // struct link_map
0109 #endif
0110 
0111 namespace boost { namespace dll { namespace detail {
0112 
0113 #if BOOST_OS_QNX
0114     // Android and QNX miss struct link_map. QNX misses ElfW macro, so avoiding it.
0115     struct link_map {
0116         void *l_addr;   // Base address shared object is loaded at
0117         char *l_name;   // Absolute file name object was found in
0118         // ...          // Ignoring remaning parts of the structure
0119     };
0120 #endif // #if BOOST_OS_QNX
0121 
0122     inline boost::dll::fs::path path_from_handle(void* handle, boost::dll::fs::error_code &ec) {
0123         // RTLD_DI_LINKMAP (RTLD_DI_ORIGIN returns only folder and is not suitable for this case)
0124         // Obtain the Link_map for the handle  that  is  specified.
0125         // The  p  argument  points to a Link_map pointer (Link_map
0126         // **p). The actual storage for the Link_map  structure  is
0127         // maintained by ld.so.1.
0128         //
0129         // Unfortunately we can not use `dlinfo(handle, RTLD_DI_LINKMAP, &link_map) < 0`
0130         // because it is not supported on MacOS X 10.3, NetBSD 3.0, OpenBSD 3.8, AIX 5.1,
0131         // HP-UX 11, IRIX 6.5, OSF/1 5.1, Cygwin, mingw, Interix 3.5, BeOS.
0132         // Fortunately investigating the sources of open source projects brought the understanding, that
0133         // `handle` is just a `struct link_map*` that contains full library name.
0134 
0135         const struct link_map* link_map = 0;
0136 #if BOOST_OS_BSD_FREE
0137         // FreeBSD has it's own logic http://code.metager.de/source/xref/freebsd/libexec/rtld-elf/rtld.c
0138         // Fortunately it has the dlinfo call.
0139         if (dlinfo(handle, RTLD_DI_LINKMAP, &link_map) < 0) {
0140             link_map = 0;
0141         }
0142 #else
0143         link_map = static_cast<const struct link_map*>(handle);
0144 #endif
0145         if (!link_map) {
0146             boost::dll::detail::reset_dlerror();
0147             ec = boost::dll::fs::make_error_code(
0148                 boost::dll::fs::errc::bad_file_descriptor
0149             );
0150 
0151             return boost::dll::fs::path();
0152         }
0153 
0154         if (!link_map->l_name || *link_map->l_name == '\0') {
0155             return program_location_impl(ec);
0156         }
0157 
0158         return boost::dll::fs::path(link_map->l_name);
0159     }
0160 
0161 }}} // namespace boost::dll::detail
0162 
0163 #endif // #if BOOST_OS_MACOS || BOOST_OS_IOS
0164 
0165 #endif // BOOST_DLL_DETAIL_POSIX_PATH_FROM_HANDLE_HPP
0166 
0167