elf_info.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
  2. // Copyright 2015-2019 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/dll/config.hpp>
  10. #ifdef BOOST_HAS_PRAGMA_ONCE
  11. # pragma once
  12. #endif
  13. #include <cstring>
  14. #include <fstream>
  15. #include <boost/cstdint.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 {
  77. typedef boost::dll::detail::Elf_Ehdr_template<AddressOffsetT> header_t;
  78. typedef boost::dll::detail::Elf_Shdr_template<AddressOffsetT> section_t;
  79. typedef boost::dll::detail::Elf_Sym_template<AddressOffsetT> symbol_t;
  80. BOOST_STATIC_CONSTANT(boost::uint32_t, SHT_SYMTAB_ = 2);
  81. BOOST_STATIC_CONSTANT(boost::uint32_t, SHT_STRTAB_ = 3);
  82. BOOST_STATIC_CONSTANT(unsigned char, STB_LOCAL_ = 0); /* Local symbol */
  83. BOOST_STATIC_CONSTANT(unsigned char, STB_GLOBAL_ = 1); /* Global symbol */
  84. BOOST_STATIC_CONSTANT(unsigned char, STB_WEAK_ = 2); /* Weak symbol */
  85. /* Symbol visibility specification encoded in the st_other field. */
  86. BOOST_STATIC_CONSTANT(unsigned char, STV_DEFAULT_ = 0); /* Default symbol visibility rules */
  87. BOOST_STATIC_CONSTANT(unsigned char, STV_INTERNAL_ = 1); /* Processor specific hidden class */
  88. BOOST_STATIC_CONSTANT(unsigned char, STV_HIDDEN_ = 2); /* Sym unavailable in other modules */
  89. BOOST_STATIC_CONSTANT(unsigned char, STV_PROTECTED_ = 3); /* Not preemptible, not exported */
  90. public:
  91. static bool parsing_supported(std::ifstream& fs) {
  92. const unsigned char magic_bytes[5] = {
  93. 0x7f, 'E', 'L', 'F', sizeof(boost::uint32_t) == sizeof(AddressOffsetT) ? 1 : 2
  94. };
  95. unsigned char ch;
  96. fs.seekg(0);
  97. for (std::size_t i = 0; i < sizeof(magic_bytes); ++i) {
  98. fs >> ch;
  99. if (ch != magic_bytes[i]) {
  100. return false;
  101. }
  102. }
  103. return true;
  104. }
  105. static std::vector<std::string> sections(std::ifstream& fs) {
  106. std::vector<std::string> ret;
  107. std::vector<char> names;
  108. sections_names_raw(fs, names);
  109. const char* name_begin = &names[0];
  110. const char* const name_end = name_begin + names.size();
  111. ret.reserve(header(fs).e_shnum);
  112. do {
  113. ret.push_back(name_begin);
  114. name_begin += ret.back().size() + 1;
  115. } while (name_begin != name_end);
  116. return ret;
  117. }
  118. private:
  119. template <class T>
  120. static void read_raw(std::ifstream& fs, T& value, std::size_t size = sizeof(T)) {
  121. fs.read(reinterpret_cast<char*>(&value), size);
  122. }
  123. static header_t header(std::ifstream& fs) {
  124. header_t elf;
  125. fs.seekg(0);
  126. read_raw(fs, elf);
  127. return elf;
  128. }
  129. static void sections_names_raw(std::ifstream& fs, std::vector<char>& sections) {
  130. const header_t elf = header(fs);
  131. section_t section_names_section;
  132. fs.seekg(elf.e_shoff + elf.e_shstrndx * sizeof(section_t));
  133. read_raw(fs, section_names_section);
  134. sections.resize(static_cast<std::size_t>(section_names_section.sh_size));
  135. fs.seekg(section_names_section.sh_offset);
  136. read_raw(fs, sections[0], static_cast<std::size_t>(section_names_section.sh_size));
  137. }
  138. static void symbols_text(std::ifstream& fs, std::vector<symbol_t>& symbols, std::vector<char>& text) {
  139. const header_t elf = header(fs);
  140. fs.seekg(elf.e_shoff);
  141. for (std::size_t i = 0; i < elf.e_shnum; ++i) {
  142. section_t section;
  143. read_raw(fs, section);
  144. if (section.sh_type == SHT_SYMTAB_) {
  145. symbols.resize(static_cast<std::size_t>(section.sh_size / sizeof(symbol_t)));
  146. const std::ifstream::pos_type pos = fs.tellg();
  147. fs.seekg(section.sh_offset);
  148. read_raw(fs, symbols[0], static_cast<std::size_t>(section.sh_size - (section.sh_size % sizeof(symbol_t))) );
  149. fs.seekg(pos);
  150. } else if (section.sh_type == SHT_STRTAB_) {
  151. text.resize(static_cast<std::size_t>(section.sh_size));
  152. const std::ifstream::pos_type pos = fs.tellg();
  153. fs.seekg(section.sh_offset);
  154. read_raw(fs, text[0], static_cast<std::size_t>(section.sh_size));
  155. fs.seekg(pos);
  156. }
  157. }
  158. }
  159. static bool is_visible(const symbol_t& sym) BOOST_NOEXCEPT {
  160. // `(sym.st_info >> 4) != STB_LOCAL_ && !!sym.st_size` check also workarounds the
  161. // GCC's issue https://sourceware.org/bugzilla/show_bug.cgi?id=13621
  162. return (sym.st_other & 0x03) == STV_DEFAULT_ && (sym.st_info >> 4) != STB_LOCAL_ && !!sym.st_size;
  163. }
  164. public:
  165. static std::vector<std::string> symbols(std::ifstream& fs) {
  166. std::vector<std::string> ret;
  167. std::vector<symbol_t> symbols;
  168. std::vector<char> text;
  169. symbols_text(fs, symbols, text);
  170. ret.reserve(symbols.size());
  171. for (std::size_t i = 0; i < symbols.size(); ++i) {
  172. if (is_visible(symbols[i])) {
  173. ret.push_back(&text[0] + symbols[i].st_name);
  174. if (ret.back().empty()) {
  175. ret.pop_back(); // Do not show empty names
  176. }
  177. }
  178. }
  179. return ret;
  180. }
  181. static std::vector<std::string> symbols(std::ifstream& fs, const char* section_name) {
  182. std::vector<std::string> ret;
  183. std::size_t index = 0;
  184. std::size_t ptrs_in_section_count = 0;
  185. {
  186. std::vector<char> names;
  187. sections_names_raw(fs, names);
  188. const header_t elf = header(fs);
  189. for (; index < elf.e_shnum; ++index) {
  190. section_t section;
  191. fs.seekg(elf.e_shoff + index * sizeof(section_t));
  192. read_raw(fs, section);
  193. if (!std::strcmp(&names[0] + section.sh_name, section_name)) {
  194. if (!section.sh_entsize) {
  195. section.sh_entsize = 1;
  196. }
  197. ptrs_in_section_count = static_cast<std::size_t>(section.sh_size / section.sh_entsize);
  198. break;
  199. }
  200. }
  201. }
  202. std::vector<symbol_t> symbols;
  203. std::vector<char> text;
  204. symbols_text(fs, symbols, text);
  205. if (ptrs_in_section_count < symbols.size()) {
  206. ret.reserve(ptrs_in_section_count);
  207. } else {
  208. ret.reserve(symbols.size());
  209. }
  210. for (std::size_t i = 0; i < symbols.size(); ++i) {
  211. if (symbols[i].st_shndx == index && is_visible(symbols[i])) {
  212. ret.push_back(&text[0] + symbols[i].st_name);
  213. if (ret.back().empty()) {
  214. ret.pop_back(); // Do not show empty names
  215. }
  216. }
  217. }
  218. return ret;
  219. }
  220. };
  221. typedef elf_info<boost::uint32_t> elf_info32;
  222. typedef elf_info<boost::uint64_t> elf_info64;
  223. }}} // namespace boost::dll::detail
  224. #endif // BOOST_DLL_DETAIL_POSIX_ELF_INFO_HPP