Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 1 | //===- Target.cpp ---------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "Target.h" |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 11 | #include "Error.h" |
Rui Ueyama | af21d92 | 2015-10-08 20:06:07 +0000 | [diff] [blame] | 12 | #include "OutputSections.h" |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 13 | #include "Symbols.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 14 | |
| 15 | #include "llvm/ADT/ArrayRef.h" |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 16 | #include "llvm/Object/ELF.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Endian.h" |
| 18 | #include "llvm/Support/ELF.h" |
| 19 | |
| 20 | using namespace llvm; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 21 | using namespace llvm::object; |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 22 | using namespace llvm::support::endian; |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 23 | using namespace llvm::ELF; |
| 24 | |
| 25 | namespace lld { |
| 26 | namespace elf2 { |
| 27 | |
| 28 | std::unique_ptr<TargetInfo> Target; |
| 29 | |
Rui Ueyama | 9100439 | 2015-10-13 16:08:15 +0000 | [diff] [blame] | 30 | TargetInfo *createTarget() { |
| 31 | switch (Config->EMachine) { |
| 32 | case EM_386: |
| 33 | return new X86TargetInfo(); |
| 34 | case EM_AARCH64: |
| 35 | return new AArch64TargetInfo(); |
| 36 | case EM_MIPS: |
| 37 | return new MipsTargetInfo(); |
| 38 | case EM_PPC: |
| 39 | return new PPCTargetInfo(); |
| 40 | case EM_PPC64: |
| 41 | return new PPC64TargetInfo(); |
| 42 | case EM_X86_64: |
| 43 | return new X86_64TargetInfo(); |
| 44 | } |
| 45 | error("Unknown target machine"); |
| 46 | } |
| 47 | |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 48 | TargetInfo::~TargetInfo() {} |
| 49 | |
Rafael Espindola | 6d7fcdb | 2015-09-29 13:36:32 +0000 | [diff] [blame] | 50 | bool TargetInfo::relocPointsToGot(uint32_t Type) const { return false; } |
| 51 | |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 52 | bool TargetInfo::isRelRelative(uint32_t Type) const { return true; } |
| 53 | |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 54 | X86TargetInfo::X86TargetInfo() { |
| 55 | PCRelReloc = R_386_PC32; |
| 56 | GotReloc = R_386_GLOB_DAT; |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 57 | GotRefReloc = R_386_GOT32; |
Hal Finkel | 4729064 | 2015-10-08 21:25:04 +0000 | [diff] [blame] | 58 | VAStart = 0x10000; |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 59 | } |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 60 | |
| 61 | void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame^] | 62 | uint64_t PltEntryAddr) const { |
| 63 | // jmpl *val; nop; nop |
| 64 | const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90}; |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame] | 65 | memcpy(Buf, Inst, sizeof(Inst)); |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame^] | 66 | assert(isUInt<32>(GotEntryAddr)); |
| 67 | write32le(Buf + 2, GotEntryAddr); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 70 | bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
Rui Ueyama | 5ba3ac4 | 2015-09-30 01:40:08 +0000 | [diff] [blame] | 71 | return Type == R_386_GOT32 || relocNeedsPlt(Type, S); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Rafael Espindola | 6d7fcdb | 2015-09-29 13:36:32 +0000 | [diff] [blame] | 74 | bool X86TargetInfo::relocPointsToGot(uint32_t Type) const { |
| 75 | return Type == R_386_GOTPC; |
| 76 | } |
| 77 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 78 | bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
George Rimar | 730c278 | 2015-10-07 18:46:13 +0000 | [diff] [blame] | 79 | return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared()); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 82 | static void add32le(uint8_t *L, int32_t V) { write32le(L, read32le(L) + V); } |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 83 | static void or32le(uint8_t *L, int32_t V) { write32le(L, read32le(L) | V); } |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 84 | |
Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 85 | void X86TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP, |
| 86 | uint32_t Type, uint64_t BaseAddr, |
| 87 | uint64_t SymVA) const { |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 88 | typedef ELFFile<ELF32LE>::Elf_Rel Elf_Rel; |
| 89 | auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP); |
| 90 | |
| 91 | uint32_t Offset = Rel.r_offset; |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 92 | uint8_t *Loc = Buf + Offset; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 93 | switch (Type) { |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 94 | case R_386_GOT32: |
Rui Ueyama | af21d92 | 2015-10-08 20:06:07 +0000 | [diff] [blame] | 95 | add32le(Loc, SymVA - Out<ELF32LE>::Got->getVA()); |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 96 | break; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 97 | case R_386_PC32: |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 98 | add32le(Loc, SymVA - (BaseAddr + Offset)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 99 | break; |
| 100 | case R_386_32: |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 101 | add32le(Loc, SymVA); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 102 | break; |
| 103 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 104 | error("unrecognized reloc " + Twine(Type)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 108 | X86_64TargetInfo::X86_64TargetInfo() { |
| 109 | PCRelReloc = R_X86_64_PC32; |
| 110 | GotReloc = R_X86_64_GLOB_DAT; |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 111 | GotRefReloc = R_X86_64_PC32; |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 112 | RelativeReloc = R_X86_64_RELATIVE; |
Hal Finkel | 4729064 | 2015-10-08 21:25:04 +0000 | [diff] [blame] | 113 | |
| 114 | // On freebsd x86_64 the first page cannot be mmaped. |
| 115 | // On linux that is controled by vm.mmap_min_addr. At least on some x86_64 |
| 116 | // installs that is 65536, so the first 15 pages cannot be used. |
| 117 | // Given that, the smallest value that can be used in here is 0x10000. |
| 118 | // If using 2MB pages, the smallest page aligned address that works is |
| 119 | // 0x200000, but it looks like every OS uses 4k pages for executables. |
| 120 | VAStart = 0x10000; |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 121 | } |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 122 | |
| 123 | void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame^] | 124 | uint64_t PltEntryAddr) const { |
| 125 | // jmpq *val(%rip); nop; nop |
| 126 | const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90}; |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame] | 127 | memcpy(Buf, Inst, sizeof(Inst)); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 128 | |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame^] | 129 | uint64_t NextPC = PltEntryAddr + 6; |
| 130 | int64_t Delta = GotEntryAddr - NextPC; |
| 131 | assert(isInt<32>(Delta)); |
| 132 | write32le(Buf + 2, Delta); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 135 | bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
Rui Ueyama | 5ba3ac4 | 2015-09-30 01:40:08 +0000 | [diff] [blame] | 136 | return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 139 | bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 140 | switch (Type) { |
| 141 | default: |
| 142 | return false; |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 143 | case R_X86_64_PC32: |
| 144 | // This relocation is defined to have a value of (S + A - P). |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 145 | // The problems start when a non PIC program calls a function in a shared |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 146 | // library. |
Rafael Espindola | 9a0db7c | 2015-09-29 23:23:53 +0000 | [diff] [blame] | 147 | // In an ideal world, we could just report an error saying the relocation |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 148 | // can overflow at runtime. |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 149 | // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to |
| 150 | // libc.so. |
| 151 | // |
| 152 | // The general idea on how to handle such cases is to create a PLT entry |
| 153 | // and use that as the function value. |
| 154 | // |
| 155 | // For the static linking part, we just return true and everything else |
| 156 | // will use the the PLT entry as the address. |
| 157 | // |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 158 | // The remaining (unimplemented) problem is making sure pointer equality |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 159 | // still works. We need the help of the dynamic linker for that. We |
| 160 | // let it know that we have a direct reference to a so symbol by creating |
| 161 | // an undefined symbol with a non zero st_value. Seeing that, the |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 162 | // dynamic linker resolves the symbol to the value of the symbol we created. |
| 163 | // This is true even for got entries, so pointer equality is maintained. |
| 164 | // To avoid an infinite loop, the only entry that points to the |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 165 | // real function is a dedicated got entry used by the plt. That is |
| 166 | // identified by special relocation types (R_X86_64_JUMP_SLOT, |
| 167 | // R_386_JMP_SLOT, etc). |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 168 | return S.isShared(); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 169 | case R_X86_64_PLT32: |
| 170 | return true; |
| 171 | } |
| 172 | } |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 173 | |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 174 | bool X86_64TargetInfo::isRelRelative(uint32_t Type) const { |
| 175 | switch (Type) { |
| 176 | default: |
| 177 | return false; |
| 178 | case R_X86_64_PC64: |
| 179 | case R_X86_64_PC32: |
| 180 | case R_X86_64_PC16: |
| 181 | case R_X86_64_PC8: |
| 182 | return true; |
| 183 | } |
| 184 | } |
| 185 | |
Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 186 | void X86_64TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd, |
| 187 | const void *RelP, uint32_t Type, |
| 188 | uint64_t BaseAddr, uint64_t SymVA) const { |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 189 | typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela; |
| 190 | auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP); |
| 191 | |
| 192 | uint64_t Offset = Rel.r_offset; |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 193 | uint8_t *Loc = Buf + Offset; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 194 | switch (Type) { |
| 195 | case R_X86_64_PC32: |
Rafael Espindola | cdfecff | 2015-09-23 20:08:25 +0000 | [diff] [blame] | 196 | case R_X86_64_GOTPCREL: |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 197 | write32le(Loc, SymVA + Rel.r_addend - (BaseAddr + Offset)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 198 | break; |
| 199 | case R_X86_64_64: |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 200 | write64le(Loc, SymVA + Rel.r_addend); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 201 | break; |
| 202 | case R_X86_64_32: { |
| 203 | case R_X86_64_32S: |
| 204 | uint64_t VA = SymVA + Rel.r_addend; |
| 205 | if (Type == R_X86_64_32 && !isUInt<32>(VA)) |
| 206 | error("R_X86_64_32 out of range"); |
| 207 | else if (!isInt<32>(VA)) |
| 208 | error("R_X86_64_32S out of range"); |
| 209 | |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 210 | write32le(Loc, VA); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 211 | break; |
| 212 | } |
| 213 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 214 | error("unrecognized reloc " + Twine(Type)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 218 | // Relocation masks following the #lo(value), #hi(value), #ha(value), |
| 219 | // #higher(value), #highera(value), #highest(value), and #highesta(value) |
| 220 | // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi |
| 221 | // document. |
| 222 | |
| 223 | static uint16_t applyPPCLo(uint64_t V) { return V & 0xffff; } |
| 224 | |
| 225 | static uint16_t applyPPCHi(uint64_t V) { return (V >> 16) & 0xffff; } |
| 226 | |
| 227 | static uint16_t applyPPCHa(uint64_t V) { return ((V + 0x8000) >> 16) & 0xffff; } |
| 228 | |
| 229 | static uint16_t applyPPCHigher(uint64_t V) { return (V >> 32) & 0xffff; } |
| 230 | |
| 231 | static uint16_t applyPPCHighera(uint64_t V) { |
| 232 | return ((V + 0x8000) >> 32) & 0xffff; |
| 233 | } |
| 234 | |
| 235 | static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; } |
| 236 | |
| 237 | static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; } |
| 238 | |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 239 | PPC64TargetInfo::PPC64TargetInfo() { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 240 | PCRelReloc = R_PPC64_REL24; |
| 241 | GotReloc = R_PPC64_GLOB_DAT; |
| 242 | GotRefReloc = R_PPC64_REL64; |
Hal Finkel | be0823d | 2015-10-12 20:58:52 +0000 | [diff] [blame] | 243 | RelativeReloc = R_PPC64_RELATIVE; |
Hal Finkel | 6c2a3b8 | 2015-10-08 21:51:31 +0000 | [diff] [blame] | 244 | PltEntrySize = 32; |
Hal Finkel | c848b32 | 2015-10-12 19:34:29 +0000 | [diff] [blame] | 245 | |
| 246 | // We need 64K pages (at least under glibc/Linux, the loader won't |
| 247 | // set different permissions on a finer granularity than that). |
Hal Finkel | e3c2626 | 2015-10-08 22:23:54 +0000 | [diff] [blame] | 248 | PageSize = 65536; |
Hal Finkel | c848b32 | 2015-10-12 19:34:29 +0000 | [diff] [blame] | 249 | |
Hal Finkel | 4729064 | 2015-10-08 21:25:04 +0000 | [diff] [blame] | 250 | VAStart = 0x10000000; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 251 | } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 252 | |
| 253 | static uint64_t getPPC64TocBase() { |
| 254 | // The TOC consists of sections .got, .toc, .tocbss, .plt in that |
| 255 | // order. The TOC starts where the first of these sections starts. |
| 256 | |
| 257 | // FIXME: This obviously does not do the right thing when there is no .got |
| 258 | // section, but there is a .toc or .tocbss section. |
| 259 | uint64_t TocVA = Out<ELF64BE>::Got->getVA(); |
| 260 | if (!TocVA) |
| 261 | TocVA = Out<ELF64BE>::Plt->getVA(); |
| 262 | |
| 263 | // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000 |
| 264 | // thus permitting a full 64 Kbytes segment. Note that the glibc startup |
| 265 | // code (crt1.o) assumes that you can get from the TOC base to the |
| 266 | // start of the .toc section with only a single (signed) 16-bit relocation. |
| 267 | return TocVA + 0x8000; |
| 268 | } |
| 269 | |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 270 | void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame^] | 271 | uint64_t PltEntryAddr) const { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 272 | uint64_t Off = GotEntryAddr - getPPC64TocBase(); |
| 273 | |
| 274 | // FIXME: What we should do, in theory, is get the offset of the function |
| 275 | // descriptor in the .opd section, and use that as the offset from %r2 (the |
| 276 | // TOC-base pointer). Instead, we have the GOT-entry offset, and that will |
| 277 | // be a pointer to the function descriptor in the .opd section. Using |
| 278 | // this scheme is simpler, but requires an extra indirection per PLT dispatch. |
| 279 | |
| 280 | write32be(Buf, 0xf8410000); // std %r2, 40(%r1) |
| 281 | write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha |
| 282 | write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11) |
| 283 | write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12) |
| 284 | write32be(Buf + 16, 0x7d6903a6); // mtctr %r11 |
| 285 | write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12) |
| 286 | write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12) |
| 287 | write32be(Buf + 28, 0x4e800420); // bctr |
| 288 | } |
| 289 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 290 | bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 291 | if (relocNeedsPlt(Type, S)) |
| 292 | return true; |
| 293 | |
| 294 | switch (Type) { |
| 295 | default: return false; |
| 296 | case R_PPC64_GOT16: |
| 297 | case R_PPC64_GOT16_LO: |
| 298 | case R_PPC64_GOT16_HI: |
| 299 | case R_PPC64_GOT16_HA: |
| 300 | case R_PPC64_GOT16_DS: |
| 301 | case R_PPC64_GOT16_LO_DS: |
| 302 | return true; |
| 303 | } |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 304 | } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 305 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 306 | bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 307 | if (Type != R_PPC64_REL24) |
| 308 | return false; |
| 309 | |
| 310 | // These are function calls that need to be redirected through a PLT stub. |
| 311 | return S.isShared() || (S.isUndefined() && S.isWeak()); |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 312 | } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 313 | |
Hal Finkel | be0823d | 2015-10-12 20:58:52 +0000 | [diff] [blame] | 314 | bool PPC64TargetInfo::isRelRelative(uint32_t Type) const { |
| 315 | switch (Type) { |
| 316 | default: |
| 317 | return false; |
| 318 | case R_PPC64_REL24: |
| 319 | case R_PPC64_REL14: |
| 320 | case R_PPC64_REL14_BRTAKEN: |
| 321 | case R_PPC64_REL14_BRNTAKEN: |
| 322 | case R_PPC64_REL32: |
| 323 | case R_PPC64_REL64: |
| 324 | return true; |
| 325 | } |
| 326 | } |
| 327 | |
Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 328 | void PPC64TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd, |
| 329 | const void *RelP, uint32_t Type, |
Rui Ueyama | af21d92 | 2015-10-08 20:06:07 +0000 | [diff] [blame] | 330 | uint64_t BaseAddr, uint64_t SymVA) const { |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 331 | typedef ELFFile<ELF64BE>::Elf_Rela Elf_Rela; |
| 332 | auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP); |
| 333 | |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 334 | uint8_t *L = Buf + Rel.r_offset; |
| 335 | uint64_t S = SymVA; |
| 336 | int64_t A = Rel.r_addend; |
| 337 | uint64_t P = BaseAddr + Rel.r_offset; |
| 338 | uint64_t TB = getPPC64TocBase(); |
| 339 | |
| 340 | if (Type == R_PPC64_TOC) { |
| 341 | write64be(L, TB); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | // For a TOC-relative relocation, adjust the addend and proceed in terms of |
| 346 | // the corresponding ADDR16 relocation type. |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 347 | switch (Type) { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 348 | case R_PPC64_TOC16: Type = R_PPC64_ADDR16; A -= TB; break; |
| 349 | case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; A -= TB; break; |
| 350 | case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; A -= TB; break; |
| 351 | case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; A -= TB; break; |
| 352 | case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; A -= TB; break; |
| 353 | case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; A -= TB; break; |
| 354 | default: break; |
| 355 | } |
| 356 | |
| 357 | uint64_t R = S + A; |
| 358 | |
| 359 | switch (Type) { |
| 360 | case R_PPC64_ADDR16: |
| 361 | write16be(L, applyPPCLo(R)); |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 362 | break; |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 363 | case R_PPC64_ADDR16_DS: |
| 364 | if (!isInt<16>(R)) |
| 365 | error("Relocation R_PPC64_ADDR16_DS overflow"); |
| 366 | write16be(L, (read16be(L) & 3) | (R & ~3)); |
| 367 | break; |
| 368 | case R_PPC64_ADDR16_LO: |
| 369 | write16be(L, applyPPCLo(R)); |
| 370 | break; |
| 371 | case R_PPC64_ADDR16_LO_DS: |
| 372 | write16be(L, (read16be(L) & 3) | (applyPPCLo(R) & ~3)); |
| 373 | break; |
| 374 | case R_PPC64_ADDR16_HI: |
| 375 | write16be(L, applyPPCHi(R)); |
| 376 | break; |
| 377 | case R_PPC64_ADDR16_HA: |
| 378 | write16be(L, applyPPCHa(R)); |
| 379 | break; |
| 380 | case R_PPC64_ADDR16_HIGHER: |
| 381 | write16be(L, applyPPCHigher(R)); |
| 382 | break; |
| 383 | case R_PPC64_ADDR16_HIGHERA: |
| 384 | write16be(L, applyPPCHighera(R)); |
| 385 | break; |
| 386 | case R_PPC64_ADDR16_HIGHEST: |
| 387 | write16be(L, applyPPCHighest(R)); |
| 388 | break; |
| 389 | case R_PPC64_ADDR16_HIGHESTA: |
| 390 | write16be(L, applyPPCHighesta(R)); |
| 391 | break; |
| 392 | case R_PPC64_ADDR14: { |
| 393 | if ((R & 3) != 0) |
| 394 | error("Improper alignment for relocation R_PPC64_ADDR14"); |
| 395 | |
| 396 | // Preserve the AA/LK bits in the branch instruction |
| 397 | uint8_t AALK = L[3]; |
| 398 | write16be(L + 2, (AALK & 3) | (R & 0xfffc)); |
| 399 | break; |
| 400 | } |
| 401 | case R_PPC64_REL16_LO: |
| 402 | write16be(L, applyPPCLo(R - P)); |
| 403 | break; |
| 404 | case R_PPC64_REL16_HI: |
| 405 | write16be(L, applyPPCHi(R - P)); |
| 406 | break; |
| 407 | case R_PPC64_REL16_HA: |
| 408 | write16be(L, applyPPCHa(R - P)); |
| 409 | break; |
| 410 | case R_PPC64_ADDR32: |
| 411 | if (!isInt<32>(R)) |
| 412 | error("Relocation R_PPC64_ADDR32 overflow"); |
| 413 | write32be(L, R); |
| 414 | break; |
| 415 | case R_PPC64_REL24: { |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 416 | uint64_t PltStart = Out<ELF64BE>::Plt->getVA(); |
| 417 | uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize(); |
| 418 | bool InPlt = PltStart <= S + A && S + A < PltEnd; |
| 419 | |
| 420 | if (!InPlt && Out<ELF64BE>::Opd) { |
| 421 | // If this is a local call, and we currently have the address of a |
| 422 | // function-descriptor, get the underlying code address instead. |
| 423 | uint64_t OpdStart = Out<ELF64BE>::Opd->getVA(); |
| 424 | uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize(); |
| 425 | bool InOpd = OpdStart <= S + A && S + A < OpdEnd; |
| 426 | |
| 427 | if (InOpd) |
| 428 | R = read64be(&Out<ELF64BE>::OpdBuf[S + A - OpdStart]); |
| 429 | } |
| 430 | |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 431 | uint32_t Mask = 0x03FFFFFC; |
| 432 | if (!isInt<24>(R - P)) |
| 433 | error("Relocation R_PPC64_REL24 overflow"); |
| 434 | write32be(L, (read32be(L) & ~Mask) | ((R - P) & Mask)); |
Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 435 | |
Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 436 | if (InPlt && L + 8 < BufEnd && |
| 437 | read32be(L + 4) == 0x60000000 /* nop */) |
| 438 | write32be(L + 4, 0xe8410028); // ld %r2, 40(%r1) |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 439 | break; |
| 440 | } |
| 441 | case R_PPC64_REL32: |
| 442 | if (!isInt<32>(R - P)) |
| 443 | error("Relocation R_PPC64_REL32 overflow"); |
| 444 | write32be(L, R - P); |
| 445 | break; |
| 446 | case R_PPC64_REL64: |
| 447 | write64be(L, R - P); |
| 448 | break; |
| 449 | case R_PPC64_ADDR64: |
| 450 | write64be(L, R); |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 451 | break; |
| 452 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 453 | error("unrecognized reloc " + Twine(Type)); |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 454 | } |
| 455 | } |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 456 | |
| 457 | PPCTargetInfo::PPCTargetInfo() { |
| 458 | // PCRelReloc = FIXME |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 459 | // GotReloc = FIXME |
Hal Finkel | e3c2626 | 2015-10-08 22:23:54 +0000 | [diff] [blame] | 460 | PageSize = 65536; |
Hal Finkel | 4729064 | 2015-10-08 21:25:04 +0000 | [diff] [blame] | 461 | VAStart = 0x10000000; |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 462 | } |
| 463 | void PPCTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame^] | 464 | uint64_t PltEntryAddr) const {} |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 465 | bool PPCTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
| 466 | return false; |
| 467 | } |
| 468 | bool PPCTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
| 469 | return false; |
| 470 | } |
Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 471 | void PPCTargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd, |
| 472 | const void *RelP, uint32_t Type, |
Rui Ueyama | af21d92 | 2015-10-08 20:06:07 +0000 | [diff] [blame] | 473 | uint64_t BaseAddr, uint64_t SymVA) const {} |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 474 | |
Davide Italiano | cde9336 | 2015-09-26 00:32:04 +0000 | [diff] [blame] | 475 | AArch64TargetInfo::AArch64TargetInfo() { |
| 476 | // PCRelReloc = FIXME |
| 477 | // GotReloc = FIXME |
Hal Finkel | 4729064 | 2015-10-08 21:25:04 +0000 | [diff] [blame] | 478 | VAStart = 0x400000; |
Davide Italiano | cde9336 | 2015-09-26 00:32:04 +0000 | [diff] [blame] | 479 | } |
| 480 | void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame^] | 481 | uint64_t PltEntryAddr) const {} |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 482 | bool AArch64TargetInfo::relocNeedsGot(uint32_t Type, |
| 483 | const SymbolBody &S) const { |
| 484 | return false; |
| 485 | } |
| 486 | bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type, |
| 487 | const SymbolBody &S) const { |
| 488 | return false; |
| 489 | } |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 490 | |
Davide Italiano | ef4be6b | 2015-10-06 19:01:32 +0000 | [diff] [blame] | 491 | static void updateAArch64Adr(uint8_t *L, uint64_t Imm) { |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 492 | uint32_t ImmLo = (Imm & 0x3) << 29; |
| 493 | uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5; |
| 494 | uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5); |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 495 | write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi); |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Davide Italiano | 318ca22 | 2015-10-02 22:13:51 +0000 | [diff] [blame] | 498 | // Page(Expr) is the page address of the expression Expr, defined |
| 499 | // as (Expr & ~0xFFF). (This applies even if the machine page size |
Davide Italiano | d9b5be4 | 2015-10-02 22:17:09 +0000 | [diff] [blame] | 500 | // supported by the platform has a different value.) |
Davide Italiano | ef4be6b | 2015-10-06 19:01:32 +0000 | [diff] [blame] | 501 | static uint64_t getAArch64Page(uint64_t Expr) { |
Davide Italiano | 318ca22 | 2015-10-02 22:13:51 +0000 | [diff] [blame] | 502 | return Expr & (~static_cast<uint64_t>(0xFFF)); |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 505 | void AArch64TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd, |
| 506 | const void *RelP, uint32_t Type, |
| 507 | uint64_t BaseAddr, uint64_t SymVA) const { |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 508 | typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela; |
| 509 | auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP); |
| 510 | |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 511 | uint8_t *L = Buf + Rel.r_offset; |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 512 | uint64_t S = SymVA; |
| 513 | int64_t A = Rel.r_addend; |
| 514 | uint64_t P = BaseAddr + Rel.r_offset; |
| 515 | switch (Type) { |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 516 | case R_AARCH64_ABS16: |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 517 | if (!isInt<16>(S + A)) |
| 518 | error("Relocation R_AARCH64_ABS16 out of range"); |
Davide Italiano | 06d8432 | 2015-10-07 22:10:02 +0000 | [diff] [blame] | 519 | write16le(L, S + A); |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 520 | break; |
| 521 | case R_AARCH64_ABS32: |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 522 | if (!isInt<32>(S + A)) |
| 523 | error("Relocation R_AARCH64_ABS32 out of range"); |
Davide Italiano | 06d8432 | 2015-10-07 22:10:02 +0000 | [diff] [blame] | 524 | write32le(L, S + A); |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 525 | break; |
| 526 | case R_AARCH64_ABS64: |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 527 | // No overflow check needed. |
Davide Italiano | 06d8432 | 2015-10-07 22:10:02 +0000 | [diff] [blame] | 528 | write64le(L, S + A); |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 529 | break; |
Davide Italiano | 0b6974b | 2015-10-03 19:56:07 +0000 | [diff] [blame] | 530 | case R_AARCH64_ADD_ABS_LO12_NC: |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 531 | // No overflow check needed. |
| 532 | or32le(L, ((S + A) & 0xFFF) << 10); |
Davide Italiano | 0b6974b | 2015-10-03 19:56:07 +0000 | [diff] [blame] | 533 | break; |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 534 | case R_AARCH64_ADR_PREL_LO21: { |
| 535 | uint64_t X = S + A - P; |
| 536 | if (!isInt<21>(X)) |
| 537 | error("Relocation R_AARCH64_ADR_PREL_LO21 out of range"); |
| 538 | updateAArch64Adr(L, X & 0x1FFFFF); |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 539 | break; |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 540 | } |
| 541 | case R_AARCH64_ADR_PREL_PG_HI21: { |
| 542 | uint64_t X = getAArch64Page(S + A) - getAArch64Page(P); |
| 543 | if (!isInt<33>(X)) |
| 544 | error("Relocation R_AARCH64_ADR_PREL_PG_HI21 out of range"); |
| 545 | updateAArch64Adr(L, (X >> 12) & 0x1FFFFF); // X[32:12] |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 546 | break; |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 547 | } |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 548 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 549 | error("unrecognized reloc " + Twine(Type)); |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 552 | |
| 553 | MipsTargetInfo::MipsTargetInfo() { |
| 554 | // PCRelReloc = FIXME |
| 555 | // GotReloc = FIXME |
Hal Finkel | e3c2626 | 2015-10-08 22:23:54 +0000 | [diff] [blame] | 556 | PageSize = 65536; |
Hal Finkel | 4729064 | 2015-10-08 21:25:04 +0000 | [diff] [blame] | 557 | VAStart = 0x400000; |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | void MipsTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame^] | 561 | uint64_t PltEntryAddr) const {} |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 562 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 563 | bool MipsTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
| 564 | return false; |
| 565 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 566 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 567 | bool MipsTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
| 568 | return false; |
| 569 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 570 | |
Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 571 | void MipsTargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd, |
| 572 | const void *RelP, uint32_t Type, |
Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 573 | uint64_t BaseAddr, uint64_t SymVA) const { |
| 574 | typedef ELFFile<ELF32LE>::Elf_Rel Elf_Rel; |
| 575 | auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP); |
| 576 | |
| 577 | switch (Type) { |
| 578 | case R_MIPS_32: |
| 579 | add32le(Buf + Rel.r_offset, SymVA); |
| 580 | break; |
| 581 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 582 | error("unrecognized reloc " + Twine(Type)); |
Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 583 | } |
| 584 | } |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 585 | } |
| 586 | } |