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 return Value + getELFAddend(R);
133 case ELF::R_X86_64_PC32:
134 return Value + getELFAddend(R) - R.getOffset();
135 case ELF::R_X86_64_32:
136 case ELF::R_X86_64_32S:
137 return (Value + getELFAddend(R)) & 0xFFFFFFFF;
143 uint64_t visitAarch64(uint32_t Rel, RelocationRef R, uint64_t Value) {
145 case ELF::R_AARCH64_ABS32: {
146 int64_t Res = Value + getELFAddend(R);
147 if (Res < INT32_MIN || Res > UINT32_MAX)
149 return static_cast<uint32_t>(Res);
151 case ELF::R_AARCH64_ABS64:
152 return Value + getELFAddend(R);
158 uint64_t visitBpf(uint32_t Rel, RelocationRef R, uint64_t Value) {
160 case ELF::R_BPF_64_32:
161 return Value & 0xFFFFFFFF;
162 case ELF::R_BPF_64_64:
169 uint64_t visitMips64(uint32_t Rel, RelocationRef R, uint64_t Value) {
172 return (Value + getELFAddend(R)) & 0xFFFFFFFF;
174 return Value + getELFAddend(R);
175 case ELF::R_MIPS_TLS_DTPREL64:
176 return Value + getELFAddend(R) - 0x8000;
182 uint64_t visitPPC64(uint32_t Rel, RelocationRef R, uint64_t Value) {
184 case ELF::R_PPC64_ADDR32:
185 return (Value + getELFAddend(R)) & 0xFFFFFFFF;
186 case ELF::R_PPC64_ADDR64:
187 return Value + getELFAddend(R);
193 uint64_t visitSystemz(uint32_t Rel, RelocationRef R, uint64_t Value) {
195 case ELF::R_390_32: {
196 int64_t Res = Value + getELFAddend(R);
197 if (Res < INT32_MIN || Res > UINT32_MAX)
199 return static_cast<uint32_t>(Res);
202 return Value + getELFAddend(R);
208 uint64_t visitSparc64(uint32_t Rel, RelocationRef R, uint64_t Value) {
210 case ELF::R_SPARC_32:
211 case ELF::R_SPARC_64:
212 case ELF::R_SPARC_UA32:
213 case ELF::R_SPARC_UA64:
214 return Value + getELFAddend(R);
220 uint64_t visitAmdgpu(uint32_t Rel, RelocationRef R, uint64_t Value) {
222 case ELF::R_AMDGPU_ABS32:
223 case ELF::R_AMDGPU_ABS64:
224 return Value + getELFAddend(R);
230 uint64_t visitX86(uint32_t Rel, RelocationRef R, uint64_t Value) {
232 case ELF::R_386_NONE:
236 case ELF::R_386_PC32:
237 return Value - R.getOffset();
243 uint64_t visitPPC32(uint32_t Rel, RelocationRef R, uint64_t Value) {
244 if (Rel == ELF::R_PPC_ADDR32)
245 return (Value + getELFAddend(R)) & 0xFFFFFFFF;
250 uint64_t visitARM(uint32_t Rel, RelocationRef R, uint64_t Value) {
251 if (Rel == ELF::R_ARM_ABS32) {
252 if ((int64_t)Value < INT32_MIN || (int64_t)Value > UINT32_MAX)
254 return static_cast<uint32_t>(Value);
260 uint64_t visitLanai(uint32_t Rel, RelocationRef R, uint64_t Value) {
261 if (Rel == ELF::R_LANAI_32)
262 return (Value + getELFAddend(R)) & 0xFFFFFFFF;
267 uint64_t visitMips32(uint32_t Rel, RelocationRef R, uint64_t Value) {
268 // FIXME: Take in account implicit addends to get correct results.
269 if (Rel == ELF::R_MIPS_32)
270 return Value & 0xFFFFFFFF;
271 if (Rel == ELF::R_MIPS_TLS_DTPREL32)
272 return Value & 0xFFFFFFFF;
277 uint64_t visitSparc32(uint32_t Rel, RelocationRef R, uint64_t Value) {
278 if (Rel == ELF::R_SPARC_32 || Rel == ELF::R_SPARC_UA32)
279 return Value + getELFAddend(R);
284 uint64_t visitHexagon(uint32_t Rel, RelocationRef R, uint64_t Value) {
285 if (Rel == ELF::R_HEX_32)
286 return Value + getELFAddend(R);
291 uint64_t visitCOFF(uint32_t Rel, RelocationRef R, uint64_t Value) {
292 switch (ObjToVisit.getArch()) {
295 case COFF::IMAGE_REL_I386_SECREL:
296 case COFF::IMAGE_REL_I386_DIR32:
297 return static_cast<uint32_t>(Value);
302 case COFF::IMAGE_REL_AMD64_SECREL:
303 return static_cast<uint32_t>(Value);
304 case COFF::IMAGE_REL_AMD64_ADDR64:
315 uint64_t visitMachO(uint32_t Rel, RelocationRef R, uint64_t Value) {
316 if (ObjToVisit.getArch() == Triple::x86_64 &&
317 Rel == MachO::X86_64_RELOC_UNSIGNED)
323 uint64_t visitWasm(uint32_t Rel, RelocationRef R, uint64_t Value) {
324 if (ObjToVisit.getArch() == Triple::wasm32) {
326 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
327 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
328 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
329 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
330 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
331 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
332 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
333 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
334 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
335 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
336 // For wasm section, its offset at 0 -- ignoring Value
345 } // end namespace object
346 } // end namespace llvm
348 #endif // LLVM_OBJECT_RELOCVISITOR_H