library_info.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
  2. // Copyright Antony Polukhin, 2015-2024.
  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 <boost/type_traits/integral_constant.hpp>
  16. #include <fstream>
  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(boost::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(boost::false_type /* is_32bit_platform */) BOOST_NOEXCEPT {}
  47. inline static void throw_if_in_32bit() {
  48. throw_if_in_32bit_impl( boost::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. */
  96. explicit library_info(const boost::dll::fs::path& library_path, bool throw_if_not_native_format = true)
  97. : f_(
  98. #ifdef BOOST_DLL_USE_STD_FS
  99. library_path,
  100. // Copied from boost/filesystem/fstream.hpp
  101. #elif defined(BOOST_WINDOWS_API) && (!defined(_CPPLIB_VER) || _CPPLIB_VER < 405 || defined(_STLPORT_VERSION))
  102. // !Dinkumware || early Dinkumware || STLPort masquerading as Dinkumware
  103. library_path.string().c_str(), // use narrow, since wide not available
  104. #else // use the native c_str, which will be narrow on POSIX, wide on Windows
  105. library_path.c_str(),
  106. #endif
  107. std::ios_base::in | std::ios_base::binary
  108. )
  109. {
  110. f_.exceptions(
  111. std::ios_base::failbit
  112. | std::ifstream::badbit
  113. | std::ifstream::eofbit
  114. );
  115. init(throw_if_not_native_format);
  116. }
  117. /*!
  118. * \return List of sections that exist in binary file.
  119. */
  120. std::vector<std::string> sections() {
  121. switch (fmt_) {
  122. case fmt_elf_info32: return boost::dll::detail::elf_info32::sections(f_);
  123. case fmt_elf_info64: return boost::dll::detail::elf_info64::sections(f_);
  124. case fmt_pe_info32: return boost::dll::detail::pe_info32::sections(f_);
  125. case fmt_pe_info64: return boost::dll::detail::pe_info64::sections(f_);
  126. case fmt_macho_info32: return boost::dll::detail::macho_info32::sections(f_);
  127. case fmt_macho_info64: return boost::dll::detail::macho_info64::sections(f_);
  128. };
  129. BOOST_ASSERT(false);
  130. BOOST_UNREACHABLE_RETURN(std::vector<std::string>())
  131. }
  132. /*!
  133. * \return List of all the exportable symbols from all the sections that exist in binary file.
  134. */
  135. std::vector<std::string> symbols() {
  136. switch (fmt_) {
  137. case fmt_elf_info32: return boost::dll::detail::elf_info32::symbols(f_);
  138. case fmt_elf_info64: return boost::dll::detail::elf_info64::symbols(f_);
  139. case fmt_pe_info32: return boost::dll::detail::pe_info32::symbols(f_);
  140. case fmt_pe_info64: return boost::dll::detail::pe_info64::symbols(f_);
  141. case fmt_macho_info32: return boost::dll::detail::macho_info32::symbols(f_);
  142. case fmt_macho_info64: return boost::dll::detail::macho_info64::symbols(f_);
  143. };
  144. BOOST_ASSERT(false);
  145. BOOST_UNREACHABLE_RETURN(std::vector<std::string>())
  146. }
  147. /*!
  148. * \param section_name Name of the section from which symbol names must be returned.
  149. * \return List of symbols from the specified section.
  150. */
  151. std::vector<std::string> symbols(const char* section_name) {
  152. switch (fmt_) {
  153. case fmt_elf_info32: return boost::dll::detail::elf_info32::symbols(f_, section_name);
  154. case fmt_elf_info64: return boost::dll::detail::elf_info64::symbols(f_, section_name);
  155. case fmt_pe_info32: return boost::dll::detail::pe_info32::symbols(f_, section_name);
  156. case fmt_pe_info64: return boost::dll::detail::pe_info64::symbols(f_, section_name);
  157. case fmt_macho_info32: return boost::dll::detail::macho_info32::symbols(f_, section_name);
  158. case fmt_macho_info64: return boost::dll::detail::macho_info64::symbols(f_, section_name);
  159. };
  160. BOOST_ASSERT(false);
  161. BOOST_UNREACHABLE_RETURN(std::vector<std::string>())
  162. }
  163. //! \overload std::vector<std::string> symbols(const char* section_name)
  164. std::vector<std::string> symbols(const std::string& section_name) {
  165. switch (fmt_) {
  166. case fmt_elf_info32: return boost::dll::detail::elf_info32::symbols(f_, section_name.c_str());
  167. case fmt_elf_info64: return boost::dll::detail::elf_info64::symbols(f_, section_name.c_str());
  168. case fmt_pe_info32: return boost::dll::detail::pe_info32::symbols(f_, section_name.c_str());
  169. case fmt_pe_info64: return boost::dll::detail::pe_info64::symbols(f_, section_name.c_str());
  170. case fmt_macho_info32: return boost::dll::detail::macho_info32::symbols(f_, section_name.c_str());
  171. case fmt_macho_info64: return boost::dll::detail::macho_info64::symbols(f_, section_name.c_str());
  172. };
  173. BOOST_ASSERT(false);
  174. BOOST_UNREACHABLE_RETURN(std::vector<std::string>())
  175. }
  176. };
  177. }} // namespace boost::dll
  178. #endif // BOOST_DLL_LIBRARY_INFO_HPP