1 // Copyright 2014 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.
5 #ifndef CRAZY_LINKER_ELF_RELOCATIONS_H
6 #define CRAZY_LINKER_ELF_RELOCATIONS_H
11 #include "elf_traits.h"
19 // An ElfRelocations instance holds information about relocations in a mapped
21 class ElfRelocations {
23 ElfRelocations() { ::memset(this, 0, sizeof(*this)); }
26 bool Init(const ElfView* view, Error* error);
28 // Abstract class used to resolve symbol names into addresses.
29 // Callers of ::ApplyAll() should pass the address of a derived class
30 // that properly implements the Lookup() method.
31 class SymbolResolver {
35 virtual void* Lookup(const char* symbol_name) = 0;
38 // Apply all relocations to the target mapped ELF binary. Must be called
40 // |symbols| maps to the symbol entries for the target library only.
41 // |resolver| can resolve symbols out of the current library.
42 // On error, return false and set |error| message.
43 bool ApplyAll(const ElfSymbols* symbols,
44 SymbolResolver* resolver,
48 // Register ARM packed relocations to apply.
49 // |arm_packed_relocs| is a pointer to packed relocations data.
50 void RegisterArmPackedRelocs(uint8_t* arm_packed_relocs);
53 // This function is used to adjust relocated addresses in a copy of an
54 // existing section of an ELF binary. I.e. |src_addr|...|src_addr + size|
55 // must be inside the mapped ELF binary, this function will first copy its
56 // content into |dst_addr|...|dst_addr + size|, then adjust all relocated
57 // addresses inside the destination section as if it was loaded/mapped
58 // at |map_addr|...|map_addr + size|. Only relative relocations are processed,
59 // symbolic ones are ignored.
60 void CopyAndRelocate(size_t src_addr,
66 bool ResolveSymbol(unsigned rel_type,
68 const ElfSymbols* symbols,
69 SymbolResolver* resolver,
73 bool ApplyRelaReloc(const ELF::Rela* rela,
77 bool ApplyRelReloc(const ELF::Rel* rel,
81 bool ApplyRelaRelocs(const ELF::Rela* relocs,
83 const ElfSymbols* symbols,
84 SymbolResolver* resolver,
86 bool ApplyRelRelocs(const ELF::Rel* relocs,
88 const ElfSymbols* symbols,
89 SymbolResolver* resolver,
91 void AdjustRelocation(ELF::Word rel_type,
95 void RelocateRela(size_t src_addr,
99 void RelocateRel(size_t src_addr,
105 // Apply ARM packed relocations.
106 // On error, return false and set |error| message. No-op if no packed
107 // relocations were registered.
108 bool ApplyArmPackedRelocs(Error* error);
111 #if defined(__mips__)
112 bool RelocateMipsGot(const ElfSymbols* symbols,
113 SymbolResolver* resolver,
117 const ELF::Phdr* phdr_;
121 ELF::Addr relocations_type_;
122 ELF::Addr plt_relocations_;
123 size_t plt_relocations_size_;
126 ELF::Addr relocations_;
127 size_t relocations_size_;
129 #if defined(__mips__)
130 // MIPS-specific relocation fields.
131 ELF::Word mips_symtab_count_;
132 ELF::Word mips_local_got_count_;
133 ELF::Word mips_gotsym_;
137 uint8_t* arm_packed_relocs_;
140 bool has_text_relocations_;
146 #endif // CRAZY_LINKER_ELF_RELOCATIONS_H