tools/reorder/elf_symbol_table.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial