michael@0: /* -*- Mode: C++ -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef elf_symbol_table_h__ michael@0: #define elf_symbol_table_h__ michael@0: michael@0: /* michael@0: michael@0: Utility class for reading ELF symbol tables. michael@0: michael@0: */ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "interval_map.h" michael@0: michael@0: class elf_symbol_table { michael@0: protected: michael@0: int m_fd; michael@0: void *m_mapping; michael@0: size_t m_size; michael@0: const char *m_strtab; michael@0: const Elf32_Sym *m_symbols; michael@0: int m_nsymbols; michael@0: int m_text_shndx; michael@0: michael@0: typedef interval_map rsymtab_t; michael@0: rsymtab_t m_rsymtab; michael@0: michael@0: int verify_elf_header(const Elf32_Ehdr *hdr); michael@0: int finish(); michael@0: michael@0: public: michael@0: elf_symbol_table() michael@0: : m_fd(-1), michael@0: m_mapping(MAP_FAILED), michael@0: m_size(0), michael@0: m_strtab(0), michael@0: m_symbols(0), michael@0: m_nsymbols(0), michael@0: m_text_shndx(0) {} michael@0: michael@0: ~elf_symbol_table() { finish(); } michael@0: michael@0: /** michael@0: * Read the symbol table information from the specified file. (This will michael@0: * memory-map the file.) michael@0: * michael@0: * Currently, only symbols from the `.text' section are loaded. michael@0: */ michael@0: int init(const char *file); michael@0: michael@0: /** michael@0: * Determine what symbol the specified image address corresponds michael@0: * to. Addresses need not refer to a symbol's start address: michael@0: * symbol size information is used to reverse-map the address. michael@0: */ michael@0: const Elf32_Sym *lookup(unsigned int addr) const; michael@0: michael@0: /** michael@0: * Determine the symbol name for the specified symbol. michael@0: */ michael@0: const char *get_symbol_name(const Elf32_Sym *sym) const; michael@0: michael@0: /** michael@0: * Return `true' if the specified symbol is a function. michael@0: */ michael@0: bool is_function(const Elf32_Sym *sym) const { michael@0: return (sym->st_size > 0) && michael@0: (ELF32_ST_TYPE(sym->st_info) == STT_FUNC) && michael@0: (sym->st_shndx == m_text_shndx); } michael@0: michael@0: typedef const Elf32_Sym *const_iterator; michael@0: michael@0: const_iterator begin() const { return const_iterator(m_symbols); } michael@0: const_iterator end() const { return const_iterator(m_symbols + m_nsymbols); } michael@0: }; michael@0: michael@0: #endif // elf_symbol_table_h__