tools/reorder/elf_symbol_table.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/reorder/elf_symbol_table.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,82 @@
     1.4 +/* -*- Mode: C++ -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef elf_symbol_table_h__
    1.10 +#define elf_symbol_table_h__
    1.11 +
    1.12 +/*
    1.13 +
    1.14 +  Utility class for reading ELF symbol tables.
    1.15 +
    1.16 + */
    1.17 +
    1.18 +#include <elf.h>
    1.19 +#include <sys/mman.h>
    1.20 +
    1.21 +#include "interval_map.h"
    1.22 +
    1.23 +class elf_symbol_table {
    1.24 +protected:
    1.25 +    int              m_fd;
    1.26 +    void            *m_mapping;
    1.27 +    size_t           m_size;
    1.28 +    const char      *m_strtab;
    1.29 +    const Elf32_Sym *m_symbols;
    1.30 +    int              m_nsymbols;
    1.31 +    int              m_text_shndx;
    1.32 +
    1.33 +    typedef interval_map<unsigned int, const Elf32_Sym *> rsymtab_t;
    1.34 +    rsymtab_t        m_rsymtab;
    1.35 +
    1.36 +    int verify_elf_header(const Elf32_Ehdr *hdr);
    1.37 +    int finish();
    1.38 +
    1.39 +public:
    1.40 +    elf_symbol_table()
    1.41 +        : m_fd(-1),
    1.42 +          m_mapping(MAP_FAILED),
    1.43 +          m_size(0),
    1.44 +          m_strtab(0),
    1.45 +          m_symbols(0),
    1.46 +          m_nsymbols(0),
    1.47 +          m_text_shndx(0) {}
    1.48 +
    1.49 +    ~elf_symbol_table() { finish(); }
    1.50 +
    1.51 +    /**
    1.52 +     * Read the symbol table information from the specified file. (This will
    1.53 +     * memory-map the file.)
    1.54 +     *
    1.55 +     * Currently, only symbols from the `.text' section are loaded.
    1.56 +     */
    1.57 +    int init(const char *file);
    1.58 +
    1.59 +    /**
    1.60 +     * Determine what symbol the specified image address corresponds
    1.61 +     * to. Addresses need not refer to a symbol's start address:
    1.62 +     * symbol size information is used to reverse-map the address.
    1.63 +     */
    1.64 +    const Elf32_Sym *lookup(unsigned int addr) const;
    1.65 +
    1.66 +    /**
    1.67 +     * Determine the symbol name for the specified symbol.
    1.68 +     */
    1.69 +    const char *get_symbol_name(const Elf32_Sym *sym) const;
    1.70 +
    1.71 +    /**
    1.72 +     * Return `true' if the specified symbol is a function.
    1.73 +     */
    1.74 +    bool is_function(const Elf32_Sym *sym) const {
    1.75 +        return (sym->st_size > 0) &&
    1.76 +            (ELF32_ST_TYPE(sym->st_info) == STT_FUNC) &&
    1.77 +            (sym->st_shndx == m_text_shndx); }
    1.78 +
    1.79 +    typedef const Elf32_Sym *const_iterator;
    1.80 +
    1.81 +    const_iterator begin() const { return const_iterator(m_symbols); }
    1.82 +    const_iterator end() const { return const_iterator(m_symbols + m_nsymbols); }
    1.83 +};
    1.84 +
    1.85 +#endif // elf_symbol_table_h__

mercurial