elf_info.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
  2. // Copyright 2015 Antony Polukhin.
  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_DETAIL_POSIX_ELF_INFO_HPP
  8. #define BOOST_DLL_DETAIL_POSIX_ELF_INFO_HPP
  9. #include <boost/config.hpp>
  10. #ifdef BOOST_HAS_PRAGMA_ONCE
  11. # pragma once
  12. #endif
  13. #include <cstring>
  14. #include <boost/filesystem/fstream.hpp>
  15. #include <boost/dll/detail/x_info_interface.hpp>
  16. namespace boost { namespace dll { namespace detail {
  17. template <class AddressOffsetT>
  18. struct Elf_Ehdr_template {
  19. unsigned char e_ident[16]; /* Magic number and other info */
  20. boost::uint16_t e_type; /* Object file type */
  21. boost::uint16_t e_machine; /* Architecture */
  22. boost::uint32_t e_version; /* Object file version */
  23. AddressOffsetT e_entry; /* Entry point virtual address */
  24. AddressOffsetT e_phoff; /* Program header table file offset */
  25. AddressOffsetT e_shoff; /* Section header table file offset */
  26. boost::uint32_t e_flags; /* Processor-specific flags */
  27. boost::uint16_t e_ehsize; /* ELF header size in bytes */
  28. boost::uint16_t e_phentsize; /* Program header table entry size */
  29. boost::uint16_t e_phnum; /* Program header table entry count */
  30. boost::uint16_t e_shentsize; /* Section header table entry size */
  31. boost::uint16_t e_shnum; /* Section header table entry count */
  32. boost::uint16_t e_shstrndx; /* Section header string table index */
  33. };
  34. typedef Elf_Ehdr_template<boost::uint32_t> Elf32_Ehdr_;
  35. typedef Elf_Ehdr_template<boost::uint64_t> Elf64_Ehdr_;
  36. template <class AddressOffsetT>
  37. struct Elf_Shdr_template {
  38. boost::uint32_t sh_name; /* Section name (string tbl index) */
  39. boost::uint32_t sh_type; /* Section type */
  40. AddressOffsetT sh_flags; /* Section flags */
  41. AddressOffsetT sh_addr; /* Section virtual addr at execution */
  42. AddressOffsetT sh_offset; /* Section file offset */
  43. AddressOffsetT sh_size; /* Section size in bytes */
  44. boost::uint32_t sh_link; /* Link to another section */
  45. boost::uint32_t sh_info; /* Additional section information */
  46. AddressOffsetT sh_addralign; /* Section alignment */
  47. AddressOffsetT sh_entsize; /* Entry size if section holds table */
  48. };
  49. typedef Elf_Shdr_template<boost::uint32_t> Elf32_Shdr_;
  50. typedef Elf_Shdr_template<boost::uint64_t> Elf64_Shdr_;
  51. template <class AddressOffsetT>
  52. struct Elf_Sym_template;
  53. template <>
  54. struct Elf_Sym_template<boost::uint32_t> {
  55. typedef boost::uint32_t AddressOffsetT;
  56. boost::uint32_t st_name; /* Symbol name (string tbl index) */
  57. AddressOffsetT st_value; /* Symbol value */
  58. AddressOffsetT st_size; /* Symbol size */
  59. unsigned char st_info; /* Symbol type and binding */
  60. unsigned char st_other; /* Symbol visibility */
  61. boost::uint16_t st_shndx; /* Section index */
  62. };
  63. template <>
  64. struct Elf_Sym_template<boost::uint64_t> {
  65. typedef boost::uint64_t AddressOffsetT;
  66. boost::uint32_t st_name; /* Symbol name (string tbl index) */
  67. unsigned char st_info; /* Symbol type and binding */
  68. unsigned char st_other; /* Symbol visibility */
  69. boost::uint16_t st_shndx; /* Section index */
  70. AddressOffsetT st_value; /* Symbol value */
  71. AddressOffsetT st_size; /* Symbol size */
  72. };
  73. typedef Elf_Sym_template<boost::uint32_t> Elf32_Sym_;
  74. typedef Elf_Sym_template<boost::uint64_t> Elf64_Sym_;
  75. template <class AddressOffsetT>
  76. class elf_info: public x_info_interface {
  77. boost::filesystem::ifstream& f_;
  78. typedef boost::dll::detail::Elf_Ehdr_template<AddressOffsetT> header_t;
  79. typedef boost::dll::detail::Elf_Shdr_template<AddressOffsetT> section_t;
  80. typedef boost::dll::detail::Elf_Sym_template<AddressOffsetT> symbol_t;
  81. BOOST_STATIC_CONSTANT(boost::uint32_t, SHT_SYMTAB_ = 2);
  82. BOOST_STATIC_CONSTANT(boost::uint32_t, SHT_STRTAB_ = 3);
  83. BOOST_STATIC_CONSTANT(unsigned char, STB_LOCAL_ = 0); /* Local symbol */
  84. BOOST_STATIC_CONSTANT(unsigned char, STB_GLOBAL_ = 1); /* Global symbol */
  85. BOOST_STATIC_CONSTANT(unsigned char, STB_WEAK_ = 2); /* Weak symbol */
  86. /* Symbol visibility specification encoded in the st_other field. */
  87. BOOST_STATIC_CONSTANT(unsigned char, STV_DEFAULT_ = 0); /* Default symbol visibility rules */
  88. BOOST_STATIC_CONSTANT(unsigned char, STV_INTERNAL_ = 1); /* Processor specific hidden class */
  89. BOOST_STATIC_CONSTANT(unsigned char, STV_HIDDEN_ = 2); /* Sym unavailable in other modules */
  90. BOOST_STATIC_CONSTANT(unsigned char, STV_PROTECTED_ = 3); /* Not preemptible, not exported */
  91. public:
  92. static bool parsing_supported(boost::filesystem::ifstream& f) {
  93. const unsigned char magic_bytes[5] = {
  94. 0x7f, 'E', 'L', 'F', sizeof(boost::uint32_t) == sizeof(AddressOffsetT) ? 1 : 2
  95. };
  96. unsigned char ch;
  97. f.seekg(0);
  98. for (std::size_t i = 0; i < sizeof(magic_bytes); ++i) {
  99. f >> ch;
  100. if (ch != magic_bytes[i]) {
  101. return false;
  102. }
  103. }
  104. return true;
  105. }
  106. explicit elf_info(boost::filesystem::ifstream& f) BOOST_NOEXCEPT
  107. : f_(f)
  108. {}
  109. std::vector<std::string> sections() {
  110. std::vector<std::string> ret;
  111. std::vector<char> names;
  112. sections_names_raw(names);
  113. const char* name_begin = &names[0];
  114. const char* const name_end = name_begin + names.size();
  115. ret.reserve(header().e_shnum);
  116. do {
  117. ret.push_back(name_begin);
  118. name_begin += ret.back().size() + 1;
  119. } while (name_begin != name_end);
  120. return ret;
  121. }
  122. private:
  123. template <class T>
  124. inline void read_raw(T& value, std::size_t size = sizeof(T)) const {
  125. f_.read(reinterpret_cast<char*>(&value), size);
  126. }
  127. inline header_t header() {
  128. header_t elf;
  129. f_.seekg(0);
  130. read_raw(elf);
  131. return elf;
  132. }
  133. void sections_names_raw(std::vector<char>& sections) {
  134. const header_t elf = header();
  135. section_t section_names_section;
  136. f_.seekg(elf.e_shoff + elf.e_shstrndx * sizeof(section_t));
  137. read_raw(section_names_section);
  138. sections.resize(static_cast<std::size_t>(section_names_section.sh_size));
  139. f_.seekg(section_names_section.sh_offset);
  140. read_raw(sections[0], static_cast<std::size_t>(section_names_section.sh_size));
  141. }
  142. void symbols_text(std::vector<symbol_t>& symbols, std::vector<char>& text) {
  143. const header_t elf = header();
  144. f_.seekg(elf.e_shoff);
  145. for (std::size_t i = 0; i < elf.e_shnum; ++i) {
  146. section_t section;
  147. read_raw(section);
  148. if (section.sh_type == SHT_SYMTAB_) {
  149. symbols.resize(static_cast<std::size_t>(section.sh_size / sizeof(symbol_t)));
  150. const boost::filesystem::ifstream::pos_type pos = f_.tellg();
  151. f_.seekg(section.sh_offset);
  152. read_raw(symbols[0], static_cast<std::size_t>(section.sh_size - (section.sh_size % sizeof(symbol_t))) );
  153. f_.seekg(pos);
  154. } else if (section.sh_type == SHT_STRTAB_) {
  155. text.resize(static_cast<std::size_t>(section.sh_size));
  156. const boost::filesystem::ifstream::pos_type pos = f_.tellg();
  157. f_.seekg(section.sh_offset);
  158. read_raw(text[0], static_cast<std::size_t>(section.sh_size));
  159. f_.seekg(pos);
  160. }
  161. }
  162. }
  163. static bool is_visible(const symbol_t& sym) BOOST_NOEXCEPT {
  164. // `(sym.st_info >> 4) != STB_LOCAL_ && !!sym.st_size` check also workarounds the
  165. // GCC's issue https://sourceware.org/bugzilla/show_bug.cgi?id=13621
  166. return (sym.st_other & 0x03) == STV_DEFAULT_ && (sym.st_info >> 4) != STB_LOCAL_ && !!sym.st_size;
  167. }
  168. public:
  169. std::vector<std::string> symbols() {
  170. std::vector<std::string> ret;
  171. std::vector<symbol_t> symbols;
  172. std::vector<char> text;
  173. symbols_text(symbols, text);
  174. ret.reserve(symbols.size());
  175. for (std::size_t i = 0; i < symbols.size(); ++i) {
  176. if (is_visible(symbols[i])) {
  177. ret.push_back(&text[0] + symbols[i].st_name);
  178. if (ret.back().empty()) {
  179. ret.pop_back(); // Do not show empty names
  180. }
  181. }
  182. }
  183. return ret;
  184. }
  185. std::vector<std::string> symbols(const char* section_name) {
  186. std::vector<std::string> ret;
  187. std::size_t index = 0;
  188. std::size_t ptrs_in_section_count = 0;
  189. {
  190. std::vector<char> names;
  191. sections_names_raw(names);
  192. const header_t elf = header();
  193. for (; index < elf.e_shnum; ++index) {
  194. section_t section;
  195. f_.seekg(elf.e_shoff + index * sizeof(section_t));
  196. read_raw(section);
  197. if (!std::strcmp(&names[0] + section.sh_name, section_name)) {
  198. if (!section.sh_entsize) {
  199. section.sh_entsize = 1;
  200. }
  201. ptrs_in_section_count = static_cast<std::size_t>(section.sh_size / section.sh_entsize);
  202. break;
  203. }
  204. }
  205. }
  206. std::vector<symbol_t> symbols;
  207. std::vector<char> text;
  208. symbols_text(symbols, text);
  209. if (ptrs_in_section_count < symbols.size()) {
  210. ret.reserve(ptrs_in_section_count);
  211. } else {
  212. ret.reserve(symbols.size());
  213. }
  214. for (std::size_t i = 0; i < symbols.size(); ++i) {
  215. if (symbols[i].st_shndx == index && is_visible(symbols[i])) {
  216. ret.push_back(&text[0] + symbols[i].st_name);
  217. if (ret.back().empty()) {
  218. ret.pop_back(); // Do not show empty names
  219. }
  220. }
  221. }
  222. return ret;
  223. }
  224. };
  225. typedef elf_info<boost::uint32_t> elf_info32;
  226. typedef elf_info<boost::uint64_t> elf_info64;
  227. }}} // namespace boost::dll::detail
  228. #endif // BOOST_DLL_DETAIL_POSIX_ELF_INFO_HPP