]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/dll/detail/elf_info.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / dll / detail / elf_info.hpp
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
8 #ifndef BOOST_DLL_DETAIL_POSIX_ELF_INFO_HPP
9 #define BOOST_DLL_DETAIL_POSIX_ELF_INFO_HPP
10
11 #include <boost/dll/config.hpp>
12
13 #ifdef BOOST_HAS_PRAGMA_ONCE
14 # pragma once
15 #endif
16
17 #include <cstring>
18 #include <fstream>
19
20 #include <boost/cstdint.hpp>
21 #include <boost/dll/detail/x_info_interface.hpp>
22
23 namespace boost { namespace dll { namespace detail {
24
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 */
41 };
42
43 typedef Elf_Ehdr_template<boost::uint32_t> Elf32_Ehdr_;
44 typedef Elf_Ehdr_template<boost::uint64_t> Elf64_Ehdr_;
45
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 */
58 };
59
60 typedef Elf_Shdr_template<boost::uint32_t> Elf32_Shdr_;
61 typedef Elf_Shdr_template<boost::uint64_t> Elf64_Shdr_;
62
63 template <class AddressOffsetT>
64 struct Elf_Sym_template;
65
66 template <>
67 struct Elf_Sym_template<boost::uint32_t> {
68 typedef boost::uint32_t AddressOffsetT;
69
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 */
76 };
77
78 template <>
79 struct Elf_Sym_template<boost::uint64_t> {
80 typedef boost::uint64_t AddressOffsetT;
81
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 */
88 };
89
90
91 typedef Elf_Sym_template<boost::uint32_t> Elf32_Sym_;
92 typedef Elf_Sym_template<boost::uint64_t> Elf64_Sym_;
93
94 template <class AddressOffsetT>
95 class elf_info: public x_info_interface {
96 std::ifstream& f_;
97
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;
101
102 BOOST_STATIC_CONSTANT(boost::uint32_t, SHT_SYMTAB_ = 2);
103 BOOST_STATIC_CONSTANT(boost::uint32_t, SHT_STRTAB_ = 3);
104
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 */
108
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 */
114
115 public:
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
119 };
120
121 unsigned char ch;
122 f.seekg(0);
123 for (std::size_t i = 0; i < sizeof(magic_bytes); ++i) {
124 f >> ch;
125 if (ch != magic_bytes[i]) {
126 return false;
127 }
128 }
129
130 return true;
131 }
132
133 explicit elf_info(std::ifstream& f) BOOST_NOEXCEPT
134 : f_(f)
135 {}
136
137 std::vector<std::string> sections() {
138 std::vector<std::string> ret;
139 std::vector<char> names;
140 sections_names_raw(names);
141
142 const char* name_begin = &names[0];
143 const char* const name_end = name_begin + names.size();
144 ret.reserve(header().e_shnum);
145 do {
146 ret.push_back(name_begin);
147 name_begin += ret.back().size() + 1;
148 } while (name_begin != name_end);
149
150 return ret;
151 }
152
153 private:
154 template <class T>
155 inline void read_raw(T& value, std::size_t size = sizeof(T)) const {
156 f_.read(reinterpret_cast<char*>(&value), size);
157 }
158
159 inline header_t header() {
160 header_t elf;
161
162 f_.seekg(0);
163 read_raw(elf);
164
165 return elf;
166 }
167
168 void sections_names_raw(std::vector<char>& sections) {
169 const header_t elf = header();
170
171 section_t section_names_section;
172 f_.seekg(elf.e_shoff + elf.e_shstrndx * sizeof(section_t));
173 read_raw(section_names_section);
174
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));
178 }
179
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);
183
184 for (std::size_t i = 0; i < elf.e_shnum; ++i) {
185 section_t section;
186 read_raw(section);
187
188 if (section.sh_type == SHT_SYMTAB_) {
189 symbols.resize(static_cast<std::size_t>(section.sh_size / sizeof(symbol_t)));
190
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))) );
194 f_.seekg(pos);
195 } else if (section.sh_type == SHT_STRTAB_) {
196 text.resize(static_cast<std::size_t>(section.sh_size));
197
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));
201 f_.seekg(pos);
202 }
203 }
204 }
205
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;
210 }
211
212 public:
213 std::vector<std::string> symbols() {
214 std::vector<std::string> ret;
215
216 std::vector<symbol_t> symbols;
217 std::vector<char> text;
218 symbols_text(symbols, text);
219
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
226 }
227 }
228 }
229
230 return ret;
231 }
232
233 std::vector<std::string> symbols(const char* section_name) {
234 std::vector<std::string> ret;
235
236 std::size_t index = 0;
237 std::size_t ptrs_in_section_count = 0;
238 {
239 std::vector<char> names;
240 sections_names_raw(names);
241
242 const header_t elf = header();
243
244 for (; index < elf.e_shnum; ++index) {
245 section_t section;
246 f_.seekg(elf.e_shoff + index * sizeof(section_t));
247 read_raw(section);
248
249 if (!std::strcmp(&names[0] + section.sh_name, section_name)) {
250 if (!section.sh_entsize) {
251 section.sh_entsize = 1;
252 }
253 ptrs_in_section_count = static_cast<std::size_t>(section.sh_size / section.sh_entsize);
254 break;
255 }
256 }
257 }
258
259 std::vector<symbol_t> symbols;
260 std::vector<char> text;
261 symbols_text(symbols, text);
262
263 if (ptrs_in_section_count < symbols.size()) {
264 ret.reserve(ptrs_in_section_count);
265 } else {
266 ret.reserve(symbols.size());
267 }
268
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
274 }
275 }
276 }
277
278 return ret;
279 }
280 };
281
282 typedef elf_info<boost::uint32_t> elf_info32;
283 typedef elf_info<boost::uint64_t> elf_info64;
284
285 }}} // namespace boost::dll::detail
286
287 #endif // BOOST_DLL_DETAIL_POSIX_ELF_INFO_HPP