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: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "elf_symbol_table.h" michael@0: #include "elf_utils.h" michael@0: michael@0: int michael@0: elf_symbol_table::init(const char *name) michael@0: { michael@0: // Open the file readonly. michael@0: m_fd = open(name, O_RDONLY); michael@0: if (m_fd < 0) { michael@0: perror(name); michael@0: return m_fd; michael@0: } michael@0: michael@0: // Get its size. michael@0: struct stat statbuf; michael@0: if (fstat(m_fd, &statbuf) < 0) { michael@0: perror(name); michael@0: return -1; michael@0: } michael@0: michael@0: m_size = statbuf.st_size; michael@0: michael@0: // Memory map it. michael@0: m_mapping = mmap(0, m_size, PROT_READ, MAP_SHARED, m_fd, 0); michael@0: if (m_mapping == MAP_FAILED) { michael@0: perror(name); michael@0: return -1; michael@0: } michael@0: michael@0: // Make sure it's an ELF header. michael@0: const Elf32_Ehdr *ehdr = reinterpret_cast(m_mapping); michael@0: if (elf_verify_header(ehdr) < 0) michael@0: return -1; michael@0: michael@0: const char *mapping = reinterpret_cast(m_mapping); michael@0: michael@0: // Find the section headers michael@0: const Elf32_Shdr *shdrs michael@0: = reinterpret_cast(mapping + ehdr->e_shoff); michael@0: michael@0: // find the section header string table, .shstrtab michael@0: const Elf32_Shdr *shstrtabsh = shdrs + ehdr->e_shstrndx; michael@0: const char *shstrtab = mapping + shstrtabsh->sh_offset; michael@0: michael@0: // parse the sections we care about michael@0: int shndx = 0; michael@0: const Elf32_Shdr *shlimit = shdrs + ehdr->e_shnum; michael@0: for (const Elf32_Shdr *shdr = shdrs; shdr < shlimit; ++shdr, ++shndx) { michael@0: basic_string name(shstrtab + shdr->sh_name); michael@0: if (name == ".symtab") { michael@0: m_symbols = reinterpret_cast(mapping + shdr->sh_offset); michael@0: m_nsymbols = shdr->sh_size / sizeof(Elf32_Sym); michael@0: } michael@0: else if (name == ".strtab") { michael@0: m_strtab = mapping + shdr->sh_offset; michael@0: } michael@0: else if (name == ".text") { michael@0: m_text_shndx = shndx; michael@0: } michael@0: } michael@0: michael@0: // Parse the symbol table michael@0: const Elf32_Sym *limit = m_symbols + m_nsymbols; michael@0: for (const Elf32_Sym *sym = m_symbols; sym < limit; ++sym) { michael@0: if (is_function(sym)) { michael@0: #ifdef DEBUG michael@0: hex(cout); michael@0: cout << sym->st_value << endl; michael@0: #endif michael@0: m_rsymtab.put(sym->st_value, sym->st_value + sym->st_size, sym); michael@0: } michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: int michael@0: elf_symbol_table::finish() michael@0: { michael@0: if (m_mapping != MAP_FAILED) { michael@0: munmap(m_mapping, m_size); michael@0: m_mapping = MAP_FAILED; michael@0: } michael@0: michael@0: if (m_fd >= 0) { michael@0: close(m_fd); michael@0: m_fd = -1; michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: const Elf32_Sym * michael@0: elf_symbol_table::lookup(unsigned int addr) const michael@0: { michael@0: rsymtab_t::const_iterator result = m_rsymtab.get(addr); michael@0: return result != m_rsymtab.end() ? reinterpret_cast(*result) : 0; michael@0: } michael@0: michael@0: const char * michael@0: elf_symbol_table::get_symbol_name(const Elf32_Sym *sym) const michael@0: { michael@0: return m_strtab + sym->st_name; michael@0: }