• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CRAZY_LINKER_ELF_SYMBOLS_H
6 #define CRAZY_LINKER_ELF_SYMBOLS_H
7 
8 #include <string.h>
9 
10 #include "elf_traits.h"
11 
12 namespace crazy {
13 
14 class ElfView;
15 
16 // An ElfSymbols instance holds information about symbols in a mapped ELF
17 // binary.
18 class ElfSymbols {
19  public:
ElfSymbols()20   ElfSymbols() { ::memset(this, 0, sizeof(*this)); }
~ElfSymbols()21   ~ElfSymbols() {}
22 
23   bool Init(const ElfView* view);
24 
25   const ELF::Sym* LookupByName(const char* symbol_name) const;
26 
LookupById(size_t symbol_id)27   const ELF::Sym* LookupById(size_t symbol_id) const {
28     return &symbol_table_[symbol_id];
29   }
30 
31   const ELF::Sym* LookupByAddress(void* address, size_t load_bias) const;
32 
33   // Returns true iff symbol with id |symbol_id| is weak.
IsWeakById(size_t symbol_id)34   bool IsWeakById(size_t symbol_id) const {
35     return ELF_ST_BIND(symbol_table_[symbol_id].st_info) == STB_WEAK;
36   }
37 
LookupNameById(size_t symbol_id)38   const char* LookupNameById(size_t symbol_id) const {
39     const ELF::Sym* sym = LookupById(symbol_id);
40     if (!sym)
41       return NULL;
42     return string_table_ + sym->st_name;
43   }
44 
LookupAddressByName(const char * symbol_name,size_t load_bias)45   void* LookupAddressByName(const char* symbol_name, size_t load_bias) const {
46     const ELF::Sym* sym = LookupByName(symbol_name);
47     if (!sym)
48       return NULL;
49     return reinterpret_cast<void*>(load_bias + sym->st_value);
50   }
51 
52   bool LookupNearestByAddress(void* address,
53                               size_t load_bias,
54                               const char** sym_name,
55                               void** sym_addr,
56                               size_t* sym_size) const;
57 
GetStringById(size_t str_id)58   const char* GetStringById(size_t str_id) const {
59     return string_table_ + str_id;
60   }
61 
62   // TODO(digit): Remove this once ElfRelocator is gone.
symbol_table()63   const ELF::Sym* symbol_table() const { return symbol_table_; }
string_table()64   const char* string_table() const { return string_table_; }
65 
66  private:
67   const ELF::Sym* symbol_table_;
68   const char* string_table_;
69   ELF::Word* hash_bucket_;
70   size_t hash_bucket_size_;
71   ELF::Word* hash_chain_;
72   size_t hash_chain_size_;
73 };
74 
75 }  // namespace crazy
76 
77 #endif  // CRAZY_LINKER_ELF_SYMBOLS_H
78