OLD | NEW |
(Empty) | |
| 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_RELOCATOR_H |
| 6 #define CRAZY_LINKER_ELF_RELOCATOR_H |
| 7 |
| 8 #include "elf_traits.h" |
| 9 #include "crazy_linker_error.h" |
| 10 |
| 11 namespace crazy { |
| 12 |
| 13 // A class used to perform ELF relocations in a library. |
| 14 // Usage is: |
| 15 // 1/ Create new instance. |
| 16 // 2/ Call ParseDynamicTable() |
| 17 // 3/ Call Apply() to apply all relocations. |
| 18 // |
| 19 // If any of these steps fail, the library will be in inconsistent |
| 20 // state and must be unloaded/unmapped as soon as possible by the |
| 21 // caller to avoid any issue. |
| 22 class ElfRelocator { |
| 23 public: |
| 24 // Abstract class used to resolve symbol names into addresses. |
| 25 // Callers of ::Apply() should pass the address of a derived class |
| 26 // that properly implements the Lookup() method. |
| 27 class SymbolResolver { |
| 28 public: |
| 29 SymbolResolver() {} |
| 30 ~SymbolResolver() {} |
| 31 virtual void* Lookup(const char* symbol_name) = 0; |
| 32 }; |
| 33 |
| 34 ElfRelocator(); |
| 35 |
| 36 // Initialize the relocator. This will parse the library's dynamic |
| 37 // table to populate the structure. |
| 38 // |phdr| is the library's program header address. |
| 39 // |phdr_count| is the number of program header entries. |
| 40 // |load_bias| is the library load bias. |
| 41 // |dynamic| is the library's dynamic table. |
| 42 // |dyn_count| is the number of dynamic table entries. |
| 43 // On failure, return false and sets the |error| message. |
| 44 bool Init(const ELF::Phdr* phdr, |
| 45 size_t phdr_count, |
| 46 size_t load_bias, |
| 47 const ELF::Dyn* dynamic, |
| 48 size_t dyn_count, |
| 49 Error* error); |
| 50 |
| 51 // After initialization, apply all relocations in the library. |
| 52 // |resolver| must be an non-abstract instance of SymbolResolver |
| 53 // that will be used to resolve symbol lookups during relocations. |
| 54 // |string_table| is the library's string table. |
| 55 // |symbol_table| is the library's symbol table. |
| 56 // On failure, returns false and sets the |error| message. |
| 57 bool Apply(SymbolResolver* resolver, |
| 58 const char* string_table, |
| 59 const ELF::Sym* symbol_table, |
| 60 Error* error); |
| 61 |
| 62 private: |
| 63 bool ApplyRelocs(const ELF::Rel* rel, size_t rel_count, Error* error); |
| 64 |
| 65 #ifdef __mips__ |
| 66 bool RelocateMipsGot(Error* error); |
| 67 #endif |
| 68 |
| 69 // Program header table |
| 70 const ELF::Phdr* phdr_; |
| 71 size_t phdr_count_; |
| 72 |
| 73 // Load bias to apply to all virtual address in the library. |
| 74 size_t load_bias_; |
| 75 |
| 76 // Relocations for the GOT and the PLT section that contains |
| 77 // the trampolines to call external symbols. |
| 78 unsigned* plt_got_; |
| 79 ELF::Rel* plt_rel_; |
| 80 size_t plt_rel_count_; |
| 81 |
| 82 // Relocations for the rest of the library. |
| 83 ELF::Rel* rel_; |
| 84 size_t rel_count_; |
| 85 |
| 86 // MIPS-specific relocation fields. |
| 87 #if defined(__mips__) |
| 88 unsigned mips_symtabno_; |
| 89 unsigned mips_local_gotno_; |
| 90 unsigned mips_gotsym_; |
| 91 #endif |
| 92 |
| 93 bool has_text_relocations_; |
| 94 bool has_symbolic_; |
| 95 |
| 96 // Used only during Apply(). |
| 97 SymbolResolver* resolver_; |
| 98 const char* string_table_; |
| 99 const ELF::Sym* symbol_table_; |
| 100 }; |
| 101 |
| 102 } // namespace crazy |
| 103 |
| 104 |
| 105 #endif // CRAZY_LINKER_ELF_RELOCATOR_H |
OLD | NEW |