elf_info.hpp 11 KB

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