|
1 /* -*- Mode: C++ -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef elf_symbol_table_h__ |
|
7 #define elf_symbol_table_h__ |
|
8 |
|
9 /* |
|
10 |
|
11 Utility class for reading ELF symbol tables. |
|
12 |
|
13 */ |
|
14 |
|
15 #include <elf.h> |
|
16 #include <sys/mman.h> |
|
17 |
|
18 #include "interval_map.h" |
|
19 |
|
20 class elf_symbol_table { |
|
21 protected: |
|
22 int m_fd; |
|
23 void *m_mapping; |
|
24 size_t m_size; |
|
25 const char *m_strtab; |
|
26 const Elf32_Sym *m_symbols; |
|
27 int m_nsymbols; |
|
28 int m_text_shndx; |
|
29 |
|
30 typedef interval_map<unsigned int, const Elf32_Sym *> rsymtab_t; |
|
31 rsymtab_t m_rsymtab; |
|
32 |
|
33 int verify_elf_header(const Elf32_Ehdr *hdr); |
|
34 int finish(); |
|
35 |
|
36 public: |
|
37 elf_symbol_table() |
|
38 : m_fd(-1), |
|
39 m_mapping(MAP_FAILED), |
|
40 m_size(0), |
|
41 m_strtab(0), |
|
42 m_symbols(0), |
|
43 m_nsymbols(0), |
|
44 m_text_shndx(0) {} |
|
45 |
|
46 ~elf_symbol_table() { finish(); } |
|
47 |
|
48 /** |
|
49 * Read the symbol table information from the specified file. (This will |
|
50 * memory-map the file.) |
|
51 * |
|
52 * Currently, only symbols from the `.text' section are loaded. |
|
53 */ |
|
54 int init(const char *file); |
|
55 |
|
56 /** |
|
57 * Determine what symbol the specified image address corresponds |
|
58 * to. Addresses need not refer to a symbol's start address: |
|
59 * symbol size information is used to reverse-map the address. |
|
60 */ |
|
61 const Elf32_Sym *lookup(unsigned int addr) const; |
|
62 |
|
63 /** |
|
64 * Determine the symbol name for the specified symbol. |
|
65 */ |
|
66 const char *get_symbol_name(const Elf32_Sym *sym) const; |
|
67 |
|
68 /** |
|
69 * Return `true' if the specified symbol is a function. |
|
70 */ |
|
71 bool is_function(const Elf32_Sym *sym) const { |
|
72 return (sym->st_size > 0) && |
|
73 (ELF32_ST_TYPE(sym->st_info) == STT_FUNC) && |
|
74 (sym->st_shndx == m_text_shndx); } |
|
75 |
|
76 typedef const Elf32_Sym *const_iterator; |
|
77 |
|
78 const_iterator begin() const { return const_iterator(m_symbols); } |
|
79 const_iterator end() const { return const_iterator(m_symbols + m_nsymbols); } |
|
80 }; |
|
81 |
|
82 #endif // elf_symbol_table_h__ |