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/Object/COFF.h"
20 #include "llvm/Object/ELFObjectFile.h"
21 #include "llvm/Object/MachO.h"
22 #include "llvm/Object/ObjectFile.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ELF.h"
25 #include "llvm/Support/MachO.h"
26 #include "llvm/Support/raw_ostream.h"
32 // The computed value after applying the relevant relocations.
35 // The width of the value; how many bytes to touch when applying the
38 RelocToApply(int64_t Value, char Width) : Value(Value), Width(Width) {}
39 RelocToApply() : Value(0), Width(0) {}
42 /// @brief Base class for object file relocation visitors.
45 explicit RelocVisitor(const ObjectFile &Obj)
46 : ObjToVisit(Obj), HasError(false) {}
48 // TODO: Should handle multiple applied relocations via either passing in the
49 // previously computed value or just count paired relocations as a single
51 RelocToApply visit(uint32_t RelocType, RelocationRef R, uint64_t Value = 0) {
52 if (isa<ELFObjectFileBase>(ObjToVisit))
53 return visitELF(RelocType, R, Value);
54 if (isa<COFFObjectFile>(ObjToVisit))
55 return visitCOFF(RelocType, R, Value);
56 if (isa<MachOObjectFile>(ObjToVisit))
57 return visitMachO(RelocType, R, Value);
60 return RelocToApply();
63 bool error() { return HasError; }
66 const ObjectFile &ObjToVisit;
69 RelocToApply visitELF(uint32_t RelocType, RelocationRef R, uint64_t Value) {
70 if (ObjToVisit.getBytesInAddress() == 8) { // 64-bit object file
71 switch (ObjToVisit.getArch()) {
74 case llvm::ELF::R_X86_64_NONE:
75 return visitELF_X86_64_NONE(R);
76 case llvm::ELF::R_X86_64_64:
77 return visitELF_X86_64_64(R, Value);
78 case llvm::ELF::R_X86_64_PC32:
79 return visitELF_X86_64_PC32(R, Value);
80 case llvm::ELF::R_X86_64_32:
81 return visitELF_X86_64_32(R, Value);
82 case llvm::ELF::R_X86_64_32S:
83 return visitELF_X86_64_32S(R, Value);
86 return RelocToApply();
89 case Triple::aarch64_be:
91 case llvm::ELF::R_AARCH64_ABS32:
92 return visitELF_AARCH64_ABS32(R, Value);
93 case llvm::ELF::R_AARCH64_ABS64:
94 return visitELF_AARCH64_ABS64(R, Value);
97 return RelocToApply();
102 case llvm::ELF::R_BPF_64_64:
103 return visitELF_BPF_64_64(R, Value);
104 case llvm::ELF::R_BPF_64_32:
105 return visitELF_BPF_64_32(R, Value);
108 return RelocToApply();
110 case Triple::mips64el:
113 case llvm::ELF::R_MIPS_32:
114 return visitELF_MIPS64_32(R, Value);
115 case llvm::ELF::R_MIPS_64:
116 return visitELF_MIPS64_64(R, Value);
119 return RelocToApply();
121 case Triple::ppc64le:
124 case llvm::ELF::R_PPC64_ADDR32:
125 return visitELF_PPC64_ADDR32(R, Value);
126 case llvm::ELF::R_PPC64_ADDR64:
127 return visitELF_PPC64_ADDR64(R, Value);
130 return RelocToApply();
132 case Triple::systemz:
134 case llvm::ELF::R_390_32:
135 return visitELF_390_32(R, Value);
136 case llvm::ELF::R_390_64:
137 return visitELF_390_64(R, Value);
140 return RelocToApply();
142 case Triple::sparcv9:
144 case llvm::ELF::R_SPARC_32:
145 case llvm::ELF::R_SPARC_UA32:
146 return visitELF_SPARCV9_32(R, Value);
147 case llvm::ELF::R_SPARC_64:
148 case llvm::ELF::R_SPARC_UA64:
149 return visitELF_SPARCV9_64(R, Value);
152 return RelocToApply();
156 case llvm::ELF::R_AMDGPU_ABS32:
157 return visitELF_AMDGPU_ABS32(R, Value);
160 return RelocToApply();
164 return RelocToApply();
166 } else if (ObjToVisit.getBytesInAddress() == 4) { // 32-bit object file
167 switch (ObjToVisit.getArch()) {
170 case llvm::ELF::R_386_NONE:
171 return visitELF_386_NONE(R);
172 case llvm::ELF::R_386_32:
173 return visitELF_386_32(R, Value);
174 case llvm::ELF::R_386_PC32:
175 return visitELF_386_PC32(R, Value);
178 return RelocToApply();
182 case llvm::ELF::R_PPC_ADDR32:
183 return visitELF_PPC_ADDR32(R, Value);
186 return RelocToApply();
193 return RelocToApply();
194 case llvm::ELF::R_ARM_ABS32:
195 return visitELF_ARM_ABS32(R, Value);
199 case llvm::ELF::R_LANAI_32:
200 return visitELF_Lanai_32(R, Value);
203 return RelocToApply();
208 case llvm::ELF::R_MIPS_32:
209 return visitELF_MIPS_32(R, Value);
212 return RelocToApply();
216 case llvm::ELF::R_SPARC_32:
217 case llvm::ELF::R_SPARC_UA32:
218 return visitELF_SPARC_32(R, Value);
221 return RelocToApply();
223 case Triple::hexagon:
225 case llvm::ELF::R_HEX_32:
226 return visitELF_HEX_32(R, Value);
229 return RelocToApply();
233 return RelocToApply();
236 report_fatal_error("Invalid word size in object file");
240 RelocToApply visitCOFF(uint32_t RelocType, RelocationRef R, uint64_t Value) {
241 switch (ObjToVisit.getArch()) {
244 case COFF::IMAGE_REL_I386_SECREL:
245 return visitCOFF_I386_SECREL(R, Value);
246 case COFF::IMAGE_REL_I386_DIR32:
247 return visitCOFF_I386_DIR32(R, Value);
252 case COFF::IMAGE_REL_AMD64_SECREL:
253 return visitCOFF_AMD64_SECREL(R, Value);
254 case COFF::IMAGE_REL_AMD64_ADDR64:
255 return visitCOFF_AMD64_ADDR64(R, Value);
260 return RelocToApply();
263 RelocToApply visitMachO(uint32_t RelocType, RelocationRef R, uint64_t Value) {
264 switch (ObjToVisit.getArch()) {
269 case MachO::X86_64_RELOC_UNSIGNED:
270 return visitMACHO_X86_64_UNSIGNED(R, Value);
274 return RelocToApply();
277 int64_t getELFAddend(RelocationRef R) {
278 ErrorOr<int64_t> AddendOrErr = ELFRelocationRef(R).getAddend();
279 if (std::error_code EC = AddendOrErr.getError())
280 report_fatal_error(EC.message());
284 uint8_t getLengthMachO64(RelocationRef R) {
285 const MachOObjectFile *Obj = cast<MachOObjectFile>(R.getObject());
286 return Obj->getRelocationLength(R.getRawDataRefImpl());
292 RelocToApply visitELF_386_NONE(RelocationRef R) {
293 return RelocToApply(0, 0);
296 // Ideally the Addend here will be the addend in the data for
297 // the relocation. It's not actually the case for Rel relocations.
298 RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) {
299 return RelocToApply(Value, 4);
302 RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value) {
303 uint64_t Address = R.getOffset();
304 return RelocToApply(Value - Address, 4);
308 RelocToApply visitELF_X86_64_NONE(RelocationRef R) {
309 return RelocToApply(0, 0);
311 RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) {
312 int64_t Addend = getELFAddend(R);
313 return RelocToApply(Value + Addend, 8);
315 RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value) {
316 int64_t Addend = getELFAddend(R);
317 uint64_t Address = R.getOffset();
318 return RelocToApply(Value + Addend - Address, 4);
320 RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
321 int64_t Addend = getELFAddend(R);
322 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
323 return RelocToApply(Res, 4);
325 RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) {
326 int64_t Addend = getELFAddend(R);
327 int32_t Res = (Value + Addend) & 0xFFFFFFFF;
328 return RelocToApply(Res, 4);
332 RelocToApply visitELF_BPF_64_32(RelocationRef R, uint64_t Value) {
333 uint32_t Res = Value & 0xFFFFFFFF;
334 return RelocToApply(Res, 4);
336 RelocToApply visitELF_BPF_64_64(RelocationRef R, uint64_t Value) {
337 return RelocToApply(Value, 8);
341 RelocToApply visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) {
342 int64_t Addend = getELFAddend(R);
343 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
344 return RelocToApply(Res, 4);
346 RelocToApply visitELF_PPC64_ADDR64(RelocationRef R, uint64_t Value) {
347 int64_t Addend = getELFAddend(R);
348 return RelocToApply(Value + Addend, 8);
352 RelocToApply visitELF_PPC_ADDR32(RelocationRef R, uint64_t Value) {
353 int64_t Addend = getELFAddend(R);
354 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
355 return RelocToApply(Res, 4);
359 RelocToApply visitELF_Lanai_32(RelocationRef R, uint64_t Value) {
360 int64_t Addend = getELFAddend(R);
361 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
362 return RelocToApply(Res, 4);
366 RelocToApply visitELF_MIPS_32(RelocationRef R, uint64_t Value) {
367 uint32_t Res = Value & 0xFFFFFFFF;
368 return RelocToApply(Res, 4);
372 RelocToApply visitELF_MIPS64_32(RelocationRef R, uint64_t Value) {
373 int64_t Addend = getELFAddend(R);
374 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
375 return RelocToApply(Res, 4);
378 RelocToApply visitELF_MIPS64_64(RelocationRef R, uint64_t Value) {
379 int64_t Addend = getELFAddend(R);
380 uint64_t Res = (Value + Addend);
381 return RelocToApply(Res, 8);
385 RelocToApply visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) {
386 int64_t Addend = getELFAddend(R);
387 int64_t Res = Value + Addend;
389 // Overflow check allows for both signed and unsigned interpretation.
390 if (Res < INT32_MIN || Res > UINT32_MAX)
393 return RelocToApply(static_cast<uint32_t>(Res), 4);
396 RelocToApply visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) {
397 int64_t Addend = getELFAddend(R);
398 return RelocToApply(Value + Addend, 8);
402 RelocToApply visitELF_390_32(RelocationRef R, uint64_t Value) {
403 int64_t Addend = getELFAddend(R);
404 int64_t Res = Value + Addend;
406 // Overflow check allows for both signed and unsigned interpretation.
407 if (Res < INT32_MIN || Res > UINT32_MAX)
410 return RelocToApply(static_cast<uint32_t>(Res), 4);
413 RelocToApply visitELF_390_64(RelocationRef R, uint64_t Value) {
414 int64_t Addend = getELFAddend(R);
415 return RelocToApply(Value + Addend, 8);
418 RelocToApply visitELF_SPARC_32(RelocationRef R, uint32_t Value) {
419 int32_t Addend = getELFAddend(R);
420 return RelocToApply(Value + Addend, 4);
423 RelocToApply visitELF_SPARCV9_32(RelocationRef R, uint64_t Value) {
424 int32_t Addend = getELFAddend(R);
425 return RelocToApply(Value + Addend, 4);
428 RelocToApply visitELF_SPARCV9_64(RelocationRef R, uint64_t Value) {
429 int64_t Addend = getELFAddend(R);
430 return RelocToApply(Value + Addend, 8);
433 RelocToApply visitELF_ARM_ABS32(RelocationRef R, uint64_t Value) {
436 // Overflow check allows for both signed and unsigned interpretation.
437 if (Res < INT32_MIN || Res > UINT32_MAX)
440 return RelocToApply(static_cast<uint32_t>(Res), 4);
443 RelocToApply visitELF_HEX_32(RelocationRef R, uint64_t Value) {
444 int64_t Addend = getELFAddend(R);
445 return RelocToApply(Value + Addend, 4);
448 RelocToApply visitELF_AMDGPU_ABS32(RelocationRef R, uint64_t Value) {
449 int64_t Addend = getELFAddend(R);
450 return RelocToApply(Value + Addend, 4);
454 RelocToApply visitCOFF_I386_SECREL(RelocationRef R, uint64_t Value) {
455 return RelocToApply(static_cast<uint32_t>(Value), /*Width=*/4);
458 RelocToApply visitCOFF_I386_DIR32(RelocationRef R, uint64_t Value) {
459 return RelocToApply(static_cast<uint32_t>(Value), /*Width=*/4);
463 RelocToApply visitCOFF_AMD64_SECREL(RelocationRef R, uint64_t Value) {
464 return RelocToApply(static_cast<uint32_t>(Value), /*Width=*/4);
467 RelocToApply visitCOFF_AMD64_ADDR64(RelocationRef R, uint64_t Value) {
468 return RelocToApply(Value, /*Width=*/8);
472 RelocToApply visitMACHO_X86_64_UNSIGNED(RelocationRef R, uint64_t Value) {
473 uint8_t Length = getLengthMachO64(R);
475 return RelocToApply(Value, Length);