1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/tools/reorder/elf_symbol_table.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,117 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include <string> 1.9 +#include <fstream> 1.10 +#include <errno.h> 1.11 +#include <fcntl.h> 1.12 +#include <stdlib.h> 1.13 +#include <stdio.h> 1.14 +#include <unistd.h> 1.15 +#include <sys/stat.h> 1.16 + 1.17 +#include "elf_symbol_table.h" 1.18 +#include "elf_utils.h" 1.19 + 1.20 +int 1.21 +elf_symbol_table::init(const char *name) 1.22 +{ 1.23 + // Open the file readonly. 1.24 + m_fd = open(name, O_RDONLY); 1.25 + if (m_fd < 0) { 1.26 + perror(name); 1.27 + return m_fd; 1.28 + } 1.29 + 1.30 + // Get its size. 1.31 + struct stat statbuf; 1.32 + if (fstat(m_fd, &statbuf) < 0) { 1.33 + perror(name); 1.34 + return -1; 1.35 + } 1.36 + 1.37 + m_size = statbuf.st_size; 1.38 + 1.39 + // Memory map it. 1.40 + m_mapping = mmap(0, m_size, PROT_READ, MAP_SHARED, m_fd, 0); 1.41 + if (m_mapping == MAP_FAILED) { 1.42 + perror(name); 1.43 + return -1; 1.44 + } 1.45 + 1.46 + // Make sure it's an ELF header. 1.47 + const Elf32_Ehdr *ehdr = reinterpret_cast<const Elf32_Ehdr *>(m_mapping); 1.48 + if (elf_verify_header(ehdr) < 0) 1.49 + return -1; 1.50 + 1.51 + const char *mapping = reinterpret_cast<const char *>(m_mapping); 1.52 + 1.53 + // Find the section headers 1.54 + const Elf32_Shdr *shdrs 1.55 + = reinterpret_cast<const Elf32_Shdr *>(mapping + ehdr->e_shoff); 1.56 + 1.57 + // find the section header string table, .shstrtab 1.58 + const Elf32_Shdr *shstrtabsh = shdrs + ehdr->e_shstrndx; 1.59 + const char *shstrtab = mapping + shstrtabsh->sh_offset; 1.60 + 1.61 + // parse the sections we care about 1.62 + int shndx = 0; 1.63 + const Elf32_Shdr *shlimit = shdrs + ehdr->e_shnum; 1.64 + for (const Elf32_Shdr *shdr = shdrs; shdr < shlimit; ++shdr, ++shndx) { 1.65 + basic_string<char> name(shstrtab + shdr->sh_name); 1.66 + if (name == ".symtab") { 1.67 + m_symbols = reinterpret_cast<const Elf32_Sym *>(mapping + shdr->sh_offset); 1.68 + m_nsymbols = shdr->sh_size / sizeof(Elf32_Sym); 1.69 + } 1.70 + else if (name == ".strtab") { 1.71 + m_strtab = mapping + shdr->sh_offset; 1.72 + } 1.73 + else if (name == ".text") { 1.74 + m_text_shndx = shndx; 1.75 + } 1.76 + } 1.77 + 1.78 + // Parse the symbol table 1.79 + const Elf32_Sym *limit = m_symbols + m_nsymbols; 1.80 + for (const Elf32_Sym *sym = m_symbols; sym < limit; ++sym) { 1.81 + if (is_function(sym)) { 1.82 +#ifdef DEBUG 1.83 + hex(cout); 1.84 + cout << sym->st_value << endl; 1.85 +#endif 1.86 + m_rsymtab.put(sym->st_value, sym->st_value + sym->st_size, sym); 1.87 + } 1.88 + } 1.89 + 1.90 + return 0; 1.91 +} 1.92 + 1.93 +int 1.94 +elf_symbol_table::finish() 1.95 +{ 1.96 + if (m_mapping != MAP_FAILED) { 1.97 + munmap(m_mapping, m_size); 1.98 + m_mapping = MAP_FAILED; 1.99 + } 1.100 + 1.101 + if (m_fd >= 0) { 1.102 + close(m_fd); 1.103 + m_fd = -1; 1.104 + } 1.105 + 1.106 + return 0; 1.107 +} 1.108 + 1.109 +const Elf32_Sym * 1.110 +elf_symbol_table::lookup(unsigned int addr) const 1.111 +{ 1.112 + rsymtab_t::const_iterator result = m_rsymtab.get(addr); 1.113 + return result != m_rsymtab.end() ? reinterpret_cast<const Elf32_Sym *>(*result) : 0; 1.114 +} 1.115 + 1.116 +const char * 1.117 +elf_symbol_table::get_symbol_name(const Elf32_Sym *sym) const 1.118 +{ 1.119 + return m_strtab + sym->st_name; 1.120 +}