File indexing completed on 2025-09-16 08:30:36
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef BOOST_DLL_LIBRARY_INFO_HPP
0009 #define BOOST_DLL_LIBRARY_INFO_HPP
0010
0011 #include <boost/dll/config.hpp>
0012 #include <boost/assert.hpp>
0013 #include <boost/noncopyable.hpp>
0014 #include <boost/predef/os.h>
0015 #include <boost/predef/architecture.h>
0016 #include <boost/throw_exception.hpp>
0017
0018 #include <fstream>
0019 #include <type_traits>
0020
0021 #include <boost/dll/detail/pe_info.hpp>
0022 #include <boost/dll/detail/elf_info.hpp>
0023 #include <boost/dll/detail/macho_info.hpp>
0024
0025 #ifdef BOOST_HAS_PRAGMA_ONCE
0026 # pragma once
0027 #endif
0028
0029
0030
0031
0032
0033 namespace boost { namespace dll {
0034
0035
0036
0037
0038
0039 class library_info: private boost::noncopyable {
0040 private:
0041 std::ifstream f_;
0042
0043 enum {
0044 fmt_elf_info32,
0045 fmt_elf_info64,
0046 fmt_pe_info32,
0047 fmt_pe_info64,
0048 fmt_macho_info32,
0049 fmt_macho_info64
0050 } fmt_;
0051
0052
0053 inline static void throw_if_in_32bit_impl(std::true_type ) {
0054 boost::throw_exception(std::runtime_error("Not native format: 64bit binary"));
0055 }
0056
0057 inline static void throw_if_in_32bit_impl(std::false_type ) noexcept {}
0058
0059
0060 inline static void throw_if_in_32bit() {
0061 throw_if_in_32bit_impl( std::integral_constant<bool, (sizeof(void*) == 4)>() );
0062 }
0063
0064 static void throw_if_in_windows() {
0065 #if BOOST_OS_WINDOWS
0066 boost::throw_exception(std::runtime_error("Not native format: not a PE binary"));
0067 #endif
0068 }
0069
0070 static void throw_if_in_linux() {
0071 #if !BOOST_OS_WINDOWS && !BOOST_OS_MACOS && !BOOST_OS_IOS
0072 boost::throw_exception(std::runtime_error("Not native format: not an ELF binary"));
0073 #endif
0074 }
0075
0076 static void throw_if_in_macos() {
0077 #if BOOST_OS_MACOS || BOOST_OS_IOS
0078 boost::throw_exception(std::runtime_error("Not native format: not an Mach-O binary"));
0079 #endif
0080 }
0081
0082 void init(bool throw_if_not_native) {
0083 if (boost::dll::detail::elf_info32::parsing_supported(f_)) {
0084 if (throw_if_not_native) { throw_if_in_windows(); throw_if_in_macos(); }
0085
0086 fmt_ = fmt_elf_info32;
0087 } else if (boost::dll::detail::elf_info64::parsing_supported(f_)) {
0088 if (throw_if_not_native) { throw_if_in_windows(); throw_if_in_macos(); throw_if_in_32bit(); }
0089
0090 fmt_ = fmt_elf_info64;
0091 } else if (boost::dll::detail::pe_info32::parsing_supported(f_)) {
0092 if (throw_if_not_native) { throw_if_in_linux(); throw_if_in_macos(); }
0093
0094 fmt_ = fmt_pe_info32;
0095 } else if (boost::dll::detail::pe_info64::parsing_supported(f_)) {
0096 if (throw_if_not_native) { throw_if_in_linux(); throw_if_in_macos(); throw_if_in_32bit(); }
0097
0098 fmt_ = fmt_pe_info64;
0099 } else if (boost::dll::detail::macho_info32::parsing_supported(f_)) {
0100 if (throw_if_not_native) { throw_if_in_linux(); throw_if_in_windows(); }
0101
0102 fmt_ = fmt_macho_info32;
0103 } else if (boost::dll::detail::macho_info64::parsing_supported(f_)) {
0104 if (throw_if_not_native) { throw_if_in_linux(); throw_if_in_windows(); throw_if_in_32bit(); }
0105
0106 fmt_ = fmt_macho_info64;
0107 } else {
0108 boost::throw_exception(std::runtime_error("Unsupported binary format"));
0109 }
0110 }
0111
0112
0113 public:
0114
0115
0116
0117
0118
0119
0120
0121 explicit library_info(const boost::dll::fs::path& library_path, bool throw_if_not_native_format = true)
0122 : f_(
0123 #ifdef BOOST_DLL_USE_STD_FS
0124 library_path,
0125
0126 #elif defined(BOOST_WINDOWS_API) && (!defined(_CPPLIB_VER) || _CPPLIB_VER < 405 || defined(_STLPORT_VERSION))
0127
0128 library_path.string().c_str(),
0129 #else
0130 library_path.c_str(),
0131 #endif
0132 std::ios_base::in | std::ios_base::binary
0133 )
0134 {
0135 f_.exceptions(
0136 std::ios_base::failbit
0137 | std::ifstream::badbit
0138 | std::ifstream::eofbit
0139 );
0140
0141 init(throw_if_not_native_format);
0142 }
0143
0144
0145
0146
0147
0148 std::vector<std::string> sections() {
0149 switch (fmt_) {
0150 case fmt_elf_info32: return boost::dll::detail::elf_info32::sections(f_);
0151 case fmt_elf_info64: return boost::dll::detail::elf_info64::sections(f_);
0152 case fmt_pe_info32: return boost::dll::detail::pe_info32::sections(f_);
0153 case fmt_pe_info64: return boost::dll::detail::pe_info64::sections(f_);
0154 case fmt_macho_info32: return boost::dll::detail::macho_info32::sections(f_);
0155 case fmt_macho_info64: return boost::dll::detail::macho_info64::sections(f_);
0156 };
0157 BOOST_ASSERT(false);
0158 BOOST_UNREACHABLE_RETURN(std::vector<std::string>())
0159 }
0160
0161
0162
0163
0164
0165 std::vector<std::string> symbols() {
0166 switch (fmt_) {
0167 case fmt_elf_info32: return boost::dll::detail::elf_info32::symbols(f_);
0168 case fmt_elf_info64: return boost::dll::detail::elf_info64::symbols(f_);
0169 case fmt_pe_info32: return boost::dll::detail::pe_info32::symbols(f_);
0170 case fmt_pe_info64: return boost::dll::detail::pe_info64::symbols(f_);
0171 case fmt_macho_info32: return boost::dll::detail::macho_info32::symbols(f_);
0172 case fmt_macho_info64: return boost::dll::detail::macho_info64::symbols(f_);
0173 };
0174 BOOST_ASSERT(false);
0175 BOOST_UNREACHABLE_RETURN(std::vector<std::string>())
0176 }
0177
0178
0179
0180
0181
0182
0183 std::vector<std::string> symbols(const char* section_name) {
0184 switch (fmt_) {
0185 case fmt_elf_info32: return boost::dll::detail::elf_info32::symbols(f_, section_name);
0186 case fmt_elf_info64: return boost::dll::detail::elf_info64::symbols(f_, section_name);
0187 case fmt_pe_info32: return boost::dll::detail::pe_info32::symbols(f_, section_name);
0188 case fmt_pe_info64: return boost::dll::detail::pe_info64::symbols(f_, section_name);
0189 case fmt_macho_info32: return boost::dll::detail::macho_info32::symbols(f_, section_name);
0190 case fmt_macho_info64: return boost::dll::detail::macho_info64::symbols(f_, section_name);
0191 };
0192 BOOST_ASSERT(false);
0193 BOOST_UNREACHABLE_RETURN(std::vector<std::string>())
0194 }
0195
0196
0197
0198 std::vector<std::string> symbols(const std::string& section_name) {
0199 switch (fmt_) {
0200 case fmt_elf_info32: return boost::dll::detail::elf_info32::symbols(f_, section_name.c_str());
0201 case fmt_elf_info64: return boost::dll::detail::elf_info64::symbols(f_, section_name.c_str());
0202 case fmt_pe_info32: return boost::dll::detail::pe_info32::symbols(f_, section_name.c_str());
0203 case fmt_pe_info64: return boost::dll::detail::pe_info64::symbols(f_, section_name.c_str());
0204 case fmt_macho_info32: return boost::dll::detail::macho_info32::symbols(f_, section_name.c_str());
0205 case fmt_macho_info64: return boost::dll::detail::macho_info64::symbols(f_, section_name.c_str());
0206 };
0207 BOOST_ASSERT(false);
0208 BOOST_UNREACHABLE_RETURN(std::vector<std::string>())
0209 }
0210 };
0211
0212 }}
0213 #endif