1 //===- RelocVisitor.h - Visitor for object file relocations -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides a wrapper around all the different types of relocations
11 // in different file formats, such that a client can handle them in a unified
12 // manner by only implementing a minimal number of functions.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_OBJECT_RELOCVISITOR_H
17 #define LLVM_OBJECT_RELOCVISITOR_H
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/BinaryFormat/ELF.h"
21 #include "llvm/BinaryFormat/MachO.h"
22 #include "llvm/Object/COFF.h"
23 #include "llvm/Object/ELFObjectFile.h"
24 #include "llvm/Object/MachO.h"
25 #include "llvm/Object/ObjectFile.h"
26 #include "llvm/Object/Wasm.h"
27 #include "llvm/Support/Casting.h"
28 #include "llvm/Support/ErrorHandling.h"
30 #include <system_error>
35 /// Base class for object file relocation visitors.
38 explicit RelocVisitor(const ObjectFile &Obj) : ObjToVisit(Obj) {}
40 // TODO: Should handle multiple applied relocations via either passing in the
41 // previously computed value or just count paired relocations as a single
43 uint64_t visit(uint32_t Rel, RelocationRef R, uint64_t Value = 0) {
44 if (isa<ELFObjectFileBase>(ObjToVisit))
45 return visitELF(Rel, R, Value);
46 if (isa<COFFObjectFile>(ObjToVisit))
47 return visitCOFF(Rel, R, Value);
48 if (isa<MachOObjectFile>(ObjToVisit))
49 return visitMachO(Rel, R, Value);
50 if (isa<WasmObjectFile>(ObjToVisit))
51 return visitWasm(Rel, R, Value);
57 bool error() { return HasError; }
60 const ObjectFile &ObjToVisit;
61 bool HasError = false;
63 uint64_t visitELF(uint32_t Rel, RelocationRef R, uint64_t Value) {
64 if (ObjToVisit.getBytesInAddress() == 8) { // 64-bit object file
65 switch (ObjToVisit.getArch()) {
67 return visitX86_64(Rel, R, Value);
69 case Triple::aarch64_be:
70 return visitAarch64(Rel, R, Value);
73 return visitBpf(Rel, R, Value);
74 case Triple::mips64el:
76 return visitMips64(Rel, R, Value);
79 return visitPPC64(Rel, R, Value);
81 return visitSystemz(Rel, R, Value);
83 return visitSparc64(Rel, R, Value);
85 return visitAmdgpu(Rel, R, Value);
93 assert(ObjToVisit.getBytesInAddress() == 4 &&
94 "Invalid word size in object file");
96 switch (ObjToVisit.getArch()) {
98 return visitX86(Rel, R, Value);
100 return visitPPC32(Rel, R, Value);
103 return visitARM(Rel, R, Value);
105 return visitLanai(Rel, R, Value);
108 return visitMips32(Rel, R, Value);
110 return visitSparc32(Rel, R, Value);
111 case Triple::hexagon:
112 return visitHexagon(Rel, R, Value);
119 int64_t getELFAddend(RelocationRef R) {
120 Expected<int64_t> AddendOrErr = ELFRelocationRef(R).getAddend();
121 handleAllErrors(AddendOrErr.takeError(), [](const ErrorInfoBase &EI) {
122 report_fatal_error(EI.message());
127 uint64_t visitX86_64(uint32_t Rel, RelocationRef R, uint64_t Value) {
129 case ELF::R_X86_64_NONE:
131 case ELF::R_X86_64_64:
132 case ELF::R_X86_64_DTPOFF32:
133 case ELF::R_X86_64_DTPOFF64:
134 return Value + getELFAddend(R);
135 case ELF::R_X86_64_PC32:
136 return Value + getELFAddend(R) - R.getOffset();
137 case ELF::R_X86_64_32:
138 case ELF::R_X86_64_32S:
139 return (Value + getELFAddend(R)) & 0xFFFFFFFF;
145 uint64_t visitAarch64(uint32_t Rel, RelocationRef R, uint64_t Value) {
147 case ELF::R_AARCH64_ABS32: {
148 int64_t Res = Value + getELFAddend(R);
149 if (Res < INT32_MIN || Res > UINT32_MAX)
151 return static_cast<uint32_t>(Res);
153 case ELF::R_AARCH64_ABS64:
154 return Value + getELFAddend(R);
160 uint64_t visitBpf(uint32_t Rel, RelocationRef R, uint64_t Value) {
162 case ELF::R_BPF_64_32:
163 return Value & 0xFFFFFFFF;
164 case ELF::R_BPF_64_64:
171 uint64_t visitMips64(uint32_t Rel, RelocationRef R, uint64_t Value) {
174 return (Value + getELFAddend(R)) & 0xFFFFFFFF;
176 return Value + getELFAddend(R);
177 case ELF::R_MIPS_TLS_DTPREL64:
178 return Value + getELFAddend(R) - 0x8000;
184 uint64_t visitPPC64(uint32_t Rel, RelocationRef R, uint64_t Value) {
186 case ELF::R_PPC64_ADDR32:
187 return (Value + getELFAddend(R)) & 0xFFFFFFFF;
188 case ELF::R_PPC64_ADDR64:
189 return Value + getELFAddend(R);
195 uint64_t visitSystemz(uint32_t Rel, RelocationRef R, uint64_t Value) {
197 case ELF::R_390_32: {
198 int64_t Res = Value + getELFAddend(R);
199 if (Res < INT32_MIN || Res > UINT32_MAX)
201 return static_cast<uint32_t>(Res);
204 return Value + getELFAddend(R);
210 uint64_t visitSparc64(uint32_t Rel, RelocationRef R, uint64_t Value) {
212 case ELF::R_SPARC_32:
213 case ELF::R_SPARC_64:
214 case ELF::R_SPARC_UA32:
215 case ELF::R_SPARC_UA64:
216 return Value + getELFAddend(R);
222 uint64_t visitAmdgpu(uint32_t Rel, RelocationRef R, uint64_t Value) {
224 case ELF::R_AMDGPU_ABS32:
225 case ELF::R_AMDGPU_ABS64:
226 return Value + getELFAddend(R);
232 uint64_t visitX86(uint32_t Rel, RelocationRef R, uint64_t Value) {
234 case ELF::R_386_NONE:
238 case ELF::R_386_PC32:
239 return Value - R.getOffset();
245 uint64_t visitPPC32(uint32_t Rel, RelocationRef R, uint64_t Value) {
246 if (Rel == ELF::R_PPC_ADDR32)
247 return (Value + getELFAddend(R)) & 0xFFFFFFFF;
252 uint64_t visitARM(uint32_t Rel, RelocationRef R, uint64_t Value) {
253 if (Rel == ELF::R_ARM_ABS32) {
254 if ((int64_t)Value < INT32_MIN || (int64_t)Value > UINT32_MAX)
256 return static_cast<uint32_t>(Value);
262 uint64_t visitLanai(uint32_t Rel, RelocationRef R, uint64_t Value) {
263 if (Rel == ELF::R_LANAI_32)
264 return (Value + getELFAddend(R)) & 0xFFFFFFFF;
269 uint64_t visitMips32(uint32_t Rel, RelocationRef R, uint64_t Value) {
270 // FIXME: Take in account implicit addends to get correct results.
271 if (Rel == ELF::R_MIPS_32)
272 return Value & 0xFFFFFFFF;
273 if (Rel == ELF::R_MIPS_TLS_DTPREL32)
274 return Value & 0xFFFFFFFF;
279 uint64_t visitSparc32(uint32_t Rel, RelocationRef R, uint64_t Value) {
280 if (Rel == ELF::R_SPARC_32 || Rel == ELF::R_SPARC_UA32)
281 return Value + getELFAddend(R);
286 uint64_t visitHexagon(uint32_t Rel, RelocationRef R, uint64_t Value) {
287 if (Rel == ELF::R_HEX_32)
288 return Value + getELFAddend(R);
293 uint64_t visitCOFF(uint32_t Rel, RelocationRef R, uint64_t Value) {
294 switch (ObjToVisit.getArch()) {
297 case COFF::IMAGE_REL_I386_SECREL:
298 case COFF::IMAGE_REL_I386_DIR32:
299 return static_cast<uint32_t>(Value);
304 case COFF::IMAGE_REL_AMD64_SECREL:
305 return static_cast<uint32_t>(Value);
306 case COFF::IMAGE_REL_AMD64_ADDR64:
317 uint64_t visitMachO(uint32_t Rel, RelocationRef R, uint64_t Value) {
318 if (ObjToVisit.getArch() == Triple::x86_64 &&
319 Rel == MachO::X86_64_RELOC_UNSIGNED)
325 uint64_t visitWasm(uint32_t Rel, RelocationRef R, uint64_t Value) {
326 if (ObjToVisit.getArch() == Triple::wasm32) {
328 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
329 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
330 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
331 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
332 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
333 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
334 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
335 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
336 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
337 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
338 case wasm::R_WEBASSEMBLY_EVENT_INDEX_LEB:
339 // For wasm section, its offset at 0 -- ignoring Value
348 } // end namespace object
349 } // end namespace llvm
351 #endif // LLVM_OBJECT_RELOCVISITOR_H