library_info.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
  2. // Copyright Antony Polukhin, 2015-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_LIBRARY_INFO_HPP
  8. #define BOOST_DLL_LIBRARY_INFO_HPP
  9. #include <boost/dll/config.hpp>
  10. #include <boost/assert.hpp>
  11. #include <boost/noncopyable.hpp>
  12. #include <boost/predef/os.h>
  13. #include <boost/predef/architecture.h>
  14. #include <boost/throw_exception.hpp>
  15. #include <fstream>
  16. #include <type_traits>
  17. #include <boost/dll/detail/pe_info.hpp>
  18. #include <boost/dll/detail/elf_info.hpp>
  19. #include <boost/dll/detail/macho_info.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. # pragma once
  22. #endif
  23. /// \file boost/dll/library_info.hpp
  24. /// \brief Contains only the boost::dll::library_info class that is capable of
  25. /// extracting different information from binaries.
  26. namespace boost { namespace dll {
  27. /*!
  28. * \brief Class that is capable of extracting different information from a library or binary file.
  29. * Currently understands ELF, MACH-O and PE formats on all the platforms.
  30. */
  31. class library_info: private boost::noncopyable {
  32. private:
  33. std::ifstream f_;
  34. enum {
  35. fmt_elf_info32,
  36. fmt_elf_info64,
  37. fmt_pe_info32,
  38. fmt_pe_info64,
  39. fmt_macho_info32,
  40. fmt_macho_info64
  41. } fmt_;
  42. /// @cond
  43. inline static void throw_if_in_32bit_impl(std::true_type /* is_32bit_platform */) {
  44. boost::throw_exception(std::runtime_error("Not native format: 64bit binary"));
  45. }
  46. inline static void throw_if_in_32bit_impl(std::false_type /* is_32bit_platform */) noexcept {}
  47. inline static void throw_if_in_32bit() {
  48. throw_if_in_32bit_impl( std::integral_constant<bool, (sizeof(void*) == 4)>() );
  49. }
  50. static void throw_if_in_windows() {
  51. #if BOOST_OS_WINDOWS
  52. boost::throw_exception(std::runtime_error("Not native format: not a PE binary"));
  53. #endif
  54. }
  55. static void throw_if_in_linux() {
  56. #if !BOOST_OS_WINDOWS && !BOOST_OS_MACOS && !BOOST_OS_IOS
  57. boost::throw_exception(std::runtime_error("Not native format: not an ELF binary"));
  58. #endif
  59. }
  60. static void throw_if_in_macos() {
  61. #if BOOST_OS_MACOS || BOOST_OS_IOS
  62. boost::throw_exception(std::runtime_error("Not native format: not an Mach-O binary"));
  63. #endif
  64. }
  65. void init(bool throw_if_not_native) {
  66. if (boost::dll::detail::elf_info32::parsing_supported(f_)) {
  67. if (throw_if_not_native) { throw_if_in_windows(); throw_if_in_macos(); }
  68. fmt_ = fmt_elf_info32;
  69. } else if (boost::dll::detail::elf_info64::parsing_supported(f_)) {
  70. if (throw_if_not_native) { throw_if_in_windows(); throw_if_in_macos(); throw_if_in_32bit(); }
  71. fmt_ = fmt_elf_info64;
  72. } else if (boost::dll::detail::pe_info32::parsing_supported(f_)) {
  73. if (throw_if_not_native) { throw_if_in_linux(); throw_if_in_macos(); }
  74. fmt_ = fmt_pe_info32;
  75. } else if (boost::dll::detail::pe_info64::parsing_supported(f_)) {
  76. if (throw_if_not_native) { throw_if_in_linux(); throw_if_in_macos(); throw_if_in_32bit(); }
  77. fmt_ = fmt_pe_info64;
  78. } else if (boost::dll::detail::macho_info32::parsing_supported(f_)) {
  79. if (throw_if_not_native) { throw_if_in_linux(); throw_if_in_windows(); }
  80. fmt_ = fmt_macho_info32;
  81. } else if (boost::dll::detail::macho_info64::parsing_supported(f_)) {
  82. if (throw_if_not_native) { throw_if_in_linux(); throw_if_in_windows(); throw_if_in_32bit(); }
  83. fmt_ = fmt_macho_info64;
  84. } else {
  85. boost::throw_exception(std::runtime_error("Unsupported binary format"));
  86. }
  87. }
  88. /// @endcond
  89. public:
  90. /*!
  91. * Opens file with specified path and prepares for information extraction.
  92. * \param library_path Path to the binary file from which the info must be extracted.
  93. * \param throw_if_not_native_format Throw an exception if this file format is not
  94. * supported by OS.
  95. * \throws std::exception based exceptions.
  96. */
  97. explicit library_info(const boost::dll::fs::path& library_path, bool throw_if_not_native_format = true)
  98. : f_(
  99. #ifdef BOOST_DLL_USE_STD_FS
  100. library_path,
  101. // Copied from boost/filesystem/fstream.hpp
  102. #elif defined(BOOST_WINDOWS_API) && (!defined(_CPPLIB_VER) || _CPPLIB_VER < 405 || defined(_STLPORT_VERSION))
  103. // !Dinkumware || early Dinkumware || STLPort masquerading as Dinkumware
  104. library_path.string().c_str(), // use narrow, since wide not available
  105. #else // use the native c_str, which will be narrow on POSIX, wide on Windows
  106. library_path.c_str(),
  107. #endif
  108. std::ios_base::in | std::ios_base::binary
  109. )
  110. {
  111. f_.exceptions(
  112. std::ios_base::failbit
  113. | std::ifstream::badbit
  114. | std::ifstream::eofbit
  115. );
  116. init(throw_if_not_native_format);
  117. }
  118. /*!
  119. * \return List of sections that exist in binary file.
  120. * \throws std::exception based exceptions.
  121. */
  122. std::vector<std::string> sections() {
  123. switch (fmt_) {
  124. case fmt_elf_info32: return boost::dll::detail::elf_info32::sections(f_);
  125. case fmt_elf_info64: return boost::dll::detail::elf_info64::sections(f_);
  126. case fmt_pe_info32: return boost::dll::detail::pe_info32::sections(f_);
  127. case fmt_pe_info64: return boost::dll::detail::pe_info64::sections(f_);
  128. case fmt_macho_info32: return boost::dll::detail::macho_info32::sections(f_);
  129. case fmt_macho_info64: return boost::dll::detail::macho_info64::sections(f_);
  130. };
  131. BOOST_ASSERT(false);
  132. BOOST_UNREACHABLE_RETURN(std::vector<std::string>())
  133. }
  134. /*!
  135. * \return List of all the exportable symbols from all the sections that exist in binary file.
  136. * \throws std::exception based exceptions.
  137. */
  138. std::vector<std::string> symbols() {
  139. switch (fmt_) {
  140. case fmt_elf_info32: return boost::dll::detail::elf_info32::symbols(f_);
  141. case fmt_elf_info64: return boost::dll::detail::elf_info64::symbols(f_);
  142. case fmt_pe_info32: return boost::dll::detail::pe_info32::symbols(f_);
  143. case fmt_pe_info64: return boost::dll::detail::pe_info64::symbols(f_);
  144. case fmt_macho_info32: return boost::dll::detail::macho_info32::symbols(f_);
  145. case fmt_macho_info64: return boost::dll::detail::macho_info64::symbols(f_);
  146. };
  147. BOOST_ASSERT(false);
  148. BOOST_UNREACHABLE_RETURN(std::vector<std::string>())
  149. }
  150. /*!
  151. * \param section_name Name of the section from which symbol names must be returned.
  152. * \return List of symbols from the specified section.
  153. * \throws std::exception based exceptions.
  154. */
  155. std::vector<std::string> symbols(const char* section_name) {
  156. switch (fmt_) {
  157. case fmt_elf_info32: return boost::dll::detail::elf_info32::symbols(f_, section_name);
  158. case fmt_elf_info64: return boost::dll::detail::elf_info64::symbols(f_, section_name);
  159. case fmt_pe_info32: return boost::dll::detail::pe_info32::symbols(f_, section_name);
  160. case fmt_pe_info64: return boost::dll::detail::pe_info64::symbols(f_, section_name);
  161. case fmt_macho_info32: return boost::dll::detail::macho_info32::symbols(f_, section_name);
  162. case fmt_macho_info64: return boost::dll::detail::macho_info64::symbols(f_, section_name);
  163. };
  164. BOOST_ASSERT(false);
  165. BOOST_UNREACHABLE_RETURN(std::vector<std::string>())
  166. }
  167. //! \overload std::vector<std::string> symbols(const char* section_name)
  168. std::vector<std::string> symbols(const std::string& section_name) {
  169. switch (fmt_) {
  170. case fmt_elf_info32: return boost::dll::detail::elf_info32::symbols(f_, section_name.c_str());
  171. case fmt_elf_info64: return boost::dll::detail::elf_info64::symbols(f_, section_name.c_str());
  172. case fmt_pe_info32: return boost::dll::detail::pe_info32::symbols(f_, section_name.c_str());
  173. case fmt_pe_info64: return boost::dll::detail::pe_info64::symbols(f_, section_name.c_str());
  174. case fmt_macho_info32: return boost::dll::detail::macho_info32::symbols(f_, section_name.c_str());
  175. case fmt_macho_info64: return boost::dll::detail::macho_info64::symbols(f_, section_name.c_str());
  176. };
  177. BOOST_ASSERT(false);
  178. BOOST_UNREACHABLE_RETURN(std::vector<std::string>())
  179. }
  180. };
  181. }} // namespace boost::dll
  182. #endif // BOOST_DLL_LIBRARY_INFO_HPP