1 // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
2 // Copyright 2015-2019 Antony Polukhin.
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)
8 #ifndef BOOST_DLL_DETAIL_POSIX_ELF_INFO_HPP
9 #define BOOST_DLL_DETAIL_POSIX_ELF_INFO_HPP
11 #include <boost/dll/config.hpp>
13 #ifdef BOOST_HAS_PRAGMA_ONCE
20 #include <boost/cstdint.hpp>
21 #include <boost/dll/detail/x_info_interface.hpp>
23 namespace boost { namespace dll { namespace detail {
25 template <class AddressOffsetT>
26 struct Elf_Ehdr_template {
27 unsigned char e_ident[16]; /* Magic number and other info */
28 boost::uint16_t e_type; /* Object file type */
29 boost::uint16_t e_machine; /* Architecture */
30 boost::uint32_t e_version; /* Object file version */
31 AddressOffsetT e_entry; /* Entry point virtual address */
32 AddressOffsetT e_phoff; /* Program header table file offset */
33 AddressOffsetT e_shoff; /* Section header table file offset */
34 boost::uint32_t e_flags; /* Processor-specific flags */
35 boost::uint16_t e_ehsize; /* ELF header size in bytes */
36 boost::uint16_t e_phentsize; /* Program header table entry size */
37 boost::uint16_t e_phnum; /* Program header table entry count */
38 boost::uint16_t e_shentsize; /* Section header table entry size */
39 boost::uint16_t e_shnum; /* Section header table entry count */
40 boost::uint16_t e_shstrndx; /* Section header string table index */
43 typedef Elf_Ehdr_template<boost::uint32_t> Elf32_Ehdr_;
44 typedef Elf_Ehdr_template<boost::uint64_t> Elf64_Ehdr_;
46 template <class AddressOffsetT>
47 struct Elf_Shdr_template {
48 boost::uint32_t sh_name; /* Section name (string tbl index) */
49 boost::uint32_t sh_type; /* Section type */
50 AddressOffsetT sh_flags; /* Section flags */
51 AddressOffsetT sh_addr; /* Section virtual addr at execution */
52 AddressOffsetT sh_offset; /* Section file offset */
53 AddressOffsetT sh_size; /* Section size in bytes */
54 boost::uint32_t sh_link; /* Link to another section */
55 boost::uint32_t sh_info; /* Additional section information */
56 AddressOffsetT sh_addralign; /* Section alignment */
57 AddressOffsetT sh_entsize; /* Entry size if section holds table */
60 typedef Elf_Shdr_template<boost::uint32_t> Elf32_Shdr_;
61 typedef Elf_Shdr_template<boost::uint64_t> Elf64_Shdr_;
63 template <class AddressOffsetT>
64 struct Elf_Sym_template;
67 struct Elf_Sym_template<boost::uint32_t> {
68 typedef boost::uint32_t AddressOffsetT;
70 boost::uint32_t st_name; /* Symbol name (string tbl index) */
71 AddressOffsetT st_value; /* Symbol value */
72 AddressOffsetT st_size; /* Symbol size */
73 unsigned char st_info; /* Symbol type and binding */
74 unsigned char st_other; /* Symbol visibility */
75 boost::uint16_t st_shndx; /* Section index */
79 struct Elf_Sym_template<boost::uint64_t> {
80 typedef boost::uint64_t AddressOffsetT;
82 boost::uint32_t st_name; /* Symbol name (string tbl index) */
83 unsigned char st_info; /* Symbol type and binding */
84 unsigned char st_other; /* Symbol visibility */
85 boost::uint16_t st_shndx; /* Section index */
86 AddressOffsetT st_value; /* Symbol value */
87 AddressOffsetT st_size; /* Symbol size */
91 typedef Elf_Sym_template<boost::uint32_t> Elf32_Sym_;
92 typedef Elf_Sym_template<boost::uint64_t> Elf64_Sym_;
94 template <class AddressOffsetT>
95 class elf_info: public x_info_interface {
98 typedef boost::dll::detail::Elf_Ehdr_template<AddressOffsetT> header_t;
99 typedef boost::dll::detail::Elf_Shdr_template<AddressOffsetT> section_t;
100 typedef boost::dll::detail::Elf_Sym_template<AddressOffsetT> symbol_t;
102 BOOST_STATIC_CONSTANT(boost::uint32_t, SHT_SYMTAB_ = 2);
103 BOOST_STATIC_CONSTANT(boost::uint32_t, SHT_STRTAB_ = 3);
105 BOOST_STATIC_CONSTANT(unsigned char, STB_LOCAL_ = 0); /* Local symbol */
106 BOOST_STATIC_CONSTANT(unsigned char, STB_GLOBAL_ = 1); /* Global symbol */
107 BOOST_STATIC_CONSTANT(unsigned char, STB_WEAK_ = 2); /* Weak symbol */
109 /* Symbol visibility specification encoded in the st_other field. */
110 BOOST_STATIC_CONSTANT(unsigned char, STV_DEFAULT_ = 0); /* Default symbol visibility rules */
111 BOOST_STATIC_CONSTANT(unsigned char, STV_INTERNAL_ = 1); /* Processor specific hidden class */
112 BOOST_STATIC_CONSTANT(unsigned char, STV_HIDDEN_ = 2); /* Sym unavailable in other modules */
113 BOOST_STATIC_CONSTANT(unsigned char, STV_PROTECTED_ = 3); /* Not preemptible, not exported */
116 static bool parsing_supported(std::ifstream& f) {
117 const unsigned char magic_bytes[5] = {
118 0x7f, 'E', 'L', 'F', sizeof(boost::uint32_t) == sizeof(AddressOffsetT) ? 1 : 2
123 for (std::size_t i = 0; i < sizeof(magic_bytes); ++i) {
125 if (ch != magic_bytes[i]) {
133 explicit elf_info(std::ifstream& f) BOOST_NOEXCEPT
137 std::vector<std::string> sections() {
138 std::vector<std::string> ret;
139 std::vector<char> names;
140 sections_names_raw(names);
142 const char* name_begin = &names[0];
143 const char* const name_end = name_begin + names.size();
144 ret.reserve(header().e_shnum);
146 ret.push_back(name_begin);
147 name_begin += ret.back().size() + 1;
148 } while (name_begin != name_end);
155 inline void read_raw(T& value, std::size_t size = sizeof(T)) const {
156 f_.read(reinterpret_cast<char*>(&value), size);
159 inline header_t header() {
168 void sections_names_raw(std::vector<char>& sections) {
169 const header_t elf = header();
171 section_t section_names_section;
172 f_.seekg(elf.e_shoff + elf.e_shstrndx * sizeof(section_t));
173 read_raw(section_names_section);
175 sections.resize(static_cast<std::size_t>(section_names_section.sh_size));
176 f_.seekg(section_names_section.sh_offset);
177 read_raw(sections[0], static_cast<std::size_t>(section_names_section.sh_size));
180 void symbols_text(std::vector<symbol_t>& symbols, std::vector<char>& text) {
181 const header_t elf = header();
182 f_.seekg(elf.e_shoff);
184 for (std::size_t i = 0; i < elf.e_shnum; ++i) {
188 if (section.sh_type == SHT_SYMTAB_) {
189 symbols.resize(static_cast<std::size_t>(section.sh_size / sizeof(symbol_t)));
191 const std::ifstream::pos_type pos = f_.tellg();
192 f_.seekg(section.sh_offset);
193 read_raw(symbols[0], static_cast<std::size_t>(section.sh_size - (section.sh_size % sizeof(symbol_t))) );
195 } else if (section.sh_type == SHT_STRTAB_) {
196 text.resize(static_cast<std::size_t>(section.sh_size));
198 const std::ifstream::pos_type pos = f_.tellg();
199 f_.seekg(section.sh_offset);
200 read_raw(text[0], static_cast<std::size_t>(section.sh_size));
206 static bool is_visible(const symbol_t& sym) BOOST_NOEXCEPT {
207 // `(sym.st_info >> 4) != STB_LOCAL_ && !!sym.st_size` check also workarounds the
208 // GCC's issue https://sourceware.org/bugzilla/show_bug.cgi?id=13621
209 return (sym.st_other & 0x03) == STV_DEFAULT_ && (sym.st_info >> 4) != STB_LOCAL_ && !!sym.st_size;
213 std::vector<std::string> symbols() {
214 std::vector<std::string> ret;
216 std::vector<symbol_t> symbols;
217 std::vector<char> text;
218 symbols_text(symbols, text);
220 ret.reserve(symbols.size());
221 for (std::size_t i = 0; i < symbols.size(); ++i) {
222 if (is_visible(symbols[i])) {
223 ret.push_back(&text[0] + symbols[i].st_name);
224 if (ret.back().empty()) {
225 ret.pop_back(); // Do not show empty names
233 std::vector<std::string> symbols(const char* section_name) {
234 std::vector<std::string> ret;
236 std::size_t index = 0;
237 std::size_t ptrs_in_section_count = 0;
239 std::vector<char> names;
240 sections_names_raw(names);
242 const header_t elf = header();
244 for (; index < elf.e_shnum; ++index) {
246 f_.seekg(elf.e_shoff + index * sizeof(section_t));
249 if (!std::strcmp(&names[0] + section.sh_name, section_name)) {
250 if (!section.sh_entsize) {
251 section.sh_entsize = 1;
253 ptrs_in_section_count = static_cast<std::size_t>(section.sh_size / section.sh_entsize);
259 std::vector<symbol_t> symbols;
260 std::vector<char> text;
261 symbols_text(symbols, text);
263 if (ptrs_in_section_count < symbols.size()) {
264 ret.reserve(ptrs_in_section_count);
266 ret.reserve(symbols.size());
269 for (std::size_t i = 0; i < symbols.size(); ++i) {
270 if (symbols[i].st_shndx == index && is_visible(symbols[i])) {
271 ret.push_back(&text[0] + symbols[i].st_name);
272 if (ret.back().empty()) {
273 ret.pop_back(); // Do not show empty names
282 typedef elf_info<boost::uint32_t> elf_info32;
283 typedef elf_info<boost::uint64_t> elf_info64;
285 }}} // namespace boost::dll::detail
287 #endif // BOOST_DLL_DETAIL_POSIX_ELF_INFO_HPP