Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
0002 // Copyright Antony Polukhin, 2015-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_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 #include <boost/type_traits/integral_constant.hpp>
0018 
0019 #include <fstream>
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 /// \file boost/dll/library_info.hpp
0030 /// \brief Contains only the boost::dll::library_info class that is capable of
0031 /// extracting different information from binaries.
0032 
0033 namespace boost { namespace dll {
0034 
0035 /*!
0036 * \brief Class that is capable of extracting different information from a library or binary file.
0037 * Currently understands ELF, MACH-O and PE formats on all the platforms.
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     /// @cond
0053     inline static void throw_if_in_32bit_impl(boost::true_type /* is_32bit_platform */) {
0054         boost::throw_exception(std::runtime_error("Not native format: 64bit binary"));
0055     }
0056 
0057     inline static void throw_if_in_32bit_impl(boost::false_type /* is_32bit_platform */) BOOST_NOEXCEPT {}
0058 
0059 
0060     inline static void throw_if_in_32bit() {
0061         throw_if_in_32bit_impl( boost::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     /// @endcond
0112 
0113 public:
0114     /*!
0115     * Opens file with specified path and prepares for information extraction.
0116     * \param library_path Path to the binary file from which the info must be extracted.
0117     * \param throw_if_not_native_format Throw an exception if this file format is not
0118     * supported by OS.
0119     */
0120     explicit library_info(const boost::dll::fs::path& library_path, bool throw_if_not_native_format = true)
0121         : f_(
0122         #ifdef BOOST_DLL_USE_STD_FS
0123             library_path,
0124         //  Copied from boost/filesystem/fstream.hpp
0125         #elif defined(BOOST_WINDOWS_API)  && (!defined(_CPPLIB_VER) || _CPPLIB_VER < 405 || defined(_STLPORT_VERSION))
0126             // !Dinkumware || early Dinkumware || STLPort masquerading as Dinkumware
0127             library_path.string().c_str(),  // use narrow, since wide not available
0128         #else  // use the native c_str, which will be narrow on POSIX, wide on Windows
0129             library_path.c_str(),
0130         #endif
0131             std::ios_base::in | std::ios_base::binary
0132         )
0133     {
0134         f_.exceptions(
0135             std::ios_base::failbit
0136             | std::ifstream::badbit
0137             | std::ifstream::eofbit
0138         );
0139 
0140         init(throw_if_not_native_format);
0141     }
0142 
0143     /*!
0144     * \return List of sections that exist in binary file.
0145     */
0146     std::vector<std::string> sections() {
0147         switch (fmt_) {
0148         case fmt_elf_info32:   return boost::dll::detail::elf_info32::sections(f_);
0149         case fmt_elf_info64:   return boost::dll::detail::elf_info64::sections(f_);
0150         case fmt_pe_info32:    return boost::dll::detail::pe_info32::sections(f_);
0151         case fmt_pe_info64:    return boost::dll::detail::pe_info64::sections(f_);
0152         case fmt_macho_info32: return boost::dll::detail::macho_info32::sections(f_);
0153         case fmt_macho_info64: return boost::dll::detail::macho_info64::sections(f_);
0154         };
0155         BOOST_ASSERT(false);
0156         BOOST_UNREACHABLE_RETURN(std::vector<std::string>())
0157     }
0158 
0159     /*!
0160     * \return List of all the exportable symbols from all the sections that exist in binary file.
0161     */
0162     std::vector<std::string> symbols() {
0163         switch (fmt_) {
0164         case fmt_elf_info32:   return boost::dll::detail::elf_info32::symbols(f_);
0165         case fmt_elf_info64:   return boost::dll::detail::elf_info64::symbols(f_);
0166         case fmt_pe_info32:    return boost::dll::detail::pe_info32::symbols(f_);
0167         case fmt_pe_info64:    return boost::dll::detail::pe_info64::symbols(f_);
0168         case fmt_macho_info32: return boost::dll::detail::macho_info32::symbols(f_);
0169         case fmt_macho_info64: return boost::dll::detail::macho_info64::symbols(f_);
0170         };
0171         BOOST_ASSERT(false);
0172         BOOST_UNREACHABLE_RETURN(std::vector<std::string>())
0173     }
0174 
0175     /*!
0176     * \param section_name Name of the section from which symbol names must be returned.
0177     * \return List of symbols from the specified section.
0178     */
0179     std::vector<std::string> symbols(const char* section_name) {
0180         switch (fmt_) {
0181         case fmt_elf_info32:   return boost::dll::detail::elf_info32::symbols(f_, section_name);
0182         case fmt_elf_info64:   return boost::dll::detail::elf_info64::symbols(f_, section_name);
0183         case fmt_pe_info32:    return boost::dll::detail::pe_info32::symbols(f_, section_name);
0184         case fmt_pe_info64:    return boost::dll::detail::pe_info64::symbols(f_, section_name);
0185         case fmt_macho_info32: return boost::dll::detail::macho_info32::symbols(f_, section_name);
0186         case fmt_macho_info64: return boost::dll::detail::macho_info64::symbols(f_, section_name);
0187         };
0188         BOOST_ASSERT(false);
0189         BOOST_UNREACHABLE_RETURN(std::vector<std::string>())
0190     }
0191 
0192 
0193     //! \overload std::vector<std::string> symbols(const char* section_name)
0194     std::vector<std::string> symbols(const std::string& section_name) {
0195         switch (fmt_) {
0196         case fmt_elf_info32:   return boost::dll::detail::elf_info32::symbols(f_, section_name.c_str());
0197         case fmt_elf_info64:   return boost::dll::detail::elf_info64::symbols(f_, section_name.c_str());
0198         case fmt_pe_info32:    return boost::dll::detail::pe_info32::symbols(f_, section_name.c_str());
0199         case fmt_pe_info64:    return boost::dll::detail::pe_info64::symbols(f_, section_name.c_str());
0200         case fmt_macho_info32: return boost::dll::detail::macho_info32::symbols(f_, section_name.c_str());
0201         case fmt_macho_info64: return boost::dll::detail::macho_info64::symbols(f_, section_name.c_str());
0202         };
0203         BOOST_ASSERT(false);
0204         BOOST_UNREACHABLE_RETURN(std::vector<std::string>())
0205     }
0206 };
0207 
0208 }} // namespace boost::dll
0209 #endif // BOOST_DLL_LIBRARY_INFO_HPP