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/StringRef.h"
20 #include "llvm/Object/COFF.h"
21 #include "llvm/Object/ELFObjectFile.h"
22 #include "llvm/Object/MachO.h"
23 #include "llvm/Object/ObjectFile.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ELF.h"
26 #include "llvm/Support/MachO.h"
27 #include "llvm/Support/raw_ostream.h"
33 // The computed value after applying the relevant relocations.
36 // The width of the value; how many bytes to touch when applying the
39 RelocToApply(int64_t Value, char Width) : Value(Value), Width(Width) {}
40 RelocToApply() : Value(0), Width(0) {}
43 /// @brief Base class for object file relocation visitors.
46 explicit RelocVisitor(const ObjectFile &Obj)
47 : ObjToVisit(Obj), HasError(false) {}
49 // TODO: Should handle multiple applied relocations via either passing in the
50 // previously computed value or just count paired relocations as a single
52 RelocToApply visit(uint32_t RelocType, RelocationRef R, uint64_t Value = 0) {
53 if (isa<ELFObjectFileBase>(ObjToVisit))
54 return visitELF(RelocType, R, Value);
55 if (isa<COFFObjectFile>(ObjToVisit))
56 return visitCOFF(RelocType, R, Value);
57 if (isa<MachOObjectFile>(ObjToVisit))
58 return visitMachO(RelocType, R, Value);
61 return RelocToApply();
64 bool error() { return HasError; }
67 const ObjectFile &ObjToVisit;
70 RelocToApply visitELF(uint32_t RelocType, RelocationRef R, uint64_t Value) {
71 if (ObjToVisit.getBytesInAddress() == 8) { // 64-bit object file
72 switch (ObjToVisit.getArch()) {
75 case llvm::ELF::R_X86_64_NONE:
76 return visitELF_X86_64_NONE(R);
77 case llvm::ELF::R_X86_64_64:
78 return visitELF_X86_64_64(R, Value);
79 case llvm::ELF::R_X86_64_PC32:
80 return visitELF_X86_64_PC32(R, Value);
81 case llvm::ELF::R_X86_64_32:
82 return visitELF_X86_64_32(R, Value);
83 case llvm::ELF::R_X86_64_32S:
84 return visitELF_X86_64_32S(R, Value);
87 return RelocToApply();
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();
99 case Triple::mips64el:
102 case llvm::ELF::R_MIPS_32:
103 return visitELF_MIPS_32(R, Value);
104 case llvm::ELF::R_MIPS_64:
105 return visitELF_MIPS_64(R, Value);
108 return RelocToApply();
110 case Triple::ppc64le:
113 case llvm::ELF::R_PPC64_ADDR32:
114 return visitELF_PPC64_ADDR32(R, Value);
115 case llvm::ELF::R_PPC64_ADDR64:
116 return visitELF_PPC64_ADDR64(R, Value);
119 return RelocToApply();
121 case Triple::systemz:
123 case llvm::ELF::R_390_32:
124 return visitELF_390_32(R, Value);
125 case llvm::ELF::R_390_64:
126 return visitELF_390_64(R, Value);
129 return RelocToApply();
131 case Triple::sparcv9:
133 case llvm::ELF::R_SPARC_32:
134 case llvm::ELF::R_SPARC_UA32:
135 return visitELF_SPARCV9_32(R, Value);
136 case llvm::ELF::R_SPARC_64:
137 case llvm::ELF::R_SPARC_UA64:
138 return visitELF_SPARCV9_64(R, Value);
141 return RelocToApply();
145 return RelocToApply();
147 } else if (ObjToVisit.getBytesInAddress() == 4) { // 32-bit object file
148 switch (ObjToVisit.getArch()) {
151 case llvm::ELF::R_386_NONE:
152 return visitELF_386_NONE(R);
153 case llvm::ELF::R_386_32:
154 return visitELF_386_32(R, Value);
155 case llvm::ELF::R_386_PC32:
156 return visitELF_386_PC32(R, Value);
159 return RelocToApply();
163 case llvm::ELF::R_PPC_ADDR32:
164 return visitELF_PPC_ADDR32(R, Value);
167 return RelocToApply();
174 return RelocToApply();
175 case llvm::ELF::R_ARM_ABS32:
176 return visitELF_ARM_ABS32(R, Value);
181 case llvm::ELF::R_MIPS_32:
182 return visitELF_MIPS_32(R, Value);
185 return RelocToApply();
189 case llvm::ELF::R_SPARC_32:
190 case llvm::ELF::R_SPARC_UA32:
191 return visitELF_SPARC_32(R, Value);
194 return RelocToApply();
198 return RelocToApply();
201 report_fatal_error("Invalid word size in object file");
205 RelocToApply visitCOFF(uint32_t RelocType, RelocationRef R, uint64_t Value) {
206 switch (ObjToVisit.getArch()) {
209 case COFF::IMAGE_REL_I386_SECREL:
210 return visitCOFF_I386_SECREL(R, Value);
211 case COFF::IMAGE_REL_I386_DIR32:
212 return visitCOFF_I386_DIR32(R, Value);
217 case COFF::IMAGE_REL_AMD64_SECREL:
218 return visitCOFF_AMD64_SECREL(R, Value);
219 case COFF::IMAGE_REL_AMD64_ADDR64:
220 return visitCOFF_AMD64_ADDR64(R, Value);
225 return RelocToApply();
228 RelocToApply visitMachO(uint32_t RelocType, RelocationRef R, uint64_t Value) {
229 switch (ObjToVisit.getArch()) {
234 case MachO::X86_64_RELOC_UNSIGNED:
235 return visitMACHO_X86_64_UNSIGNED(R, Value);
239 return RelocToApply();
242 int64_t getELFAddend(RelocationRef R) {
243 const auto *Obj = cast<ELFObjectFileBase>(R.getObject());
244 DataRefImpl DRI = R.getRawDataRefImpl();
245 ErrorOr<int64_t> AddendOrErr = Obj->getRelocationAddend(DRI);
246 if (std::error_code EC = AddendOrErr.getError())
247 report_fatal_error(EC.message());
251 uint8_t getLengthMachO64(RelocationRef R) {
252 const MachOObjectFile *Obj = cast<MachOObjectFile>(R.getObject());
253 return Obj->getRelocationLength(R.getRawDataRefImpl());
259 RelocToApply visitELF_386_NONE(RelocationRef R) {
260 return RelocToApply(0, 0);
263 // Ideally the Addend here will be the addend in the data for
264 // the relocation. It's not actually the case for Rel relocations.
265 RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) {
266 return RelocToApply(Value, 4);
269 RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value) {
271 R.getOffset(Address);
272 return RelocToApply(Value - Address, 4);
276 RelocToApply visitELF_X86_64_NONE(RelocationRef R) {
277 return RelocToApply(0, 0);
279 RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) {
280 int64_t Addend = getELFAddend(R);
281 return RelocToApply(Value + Addend, 8);
283 RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value) {
284 int64_t Addend = getELFAddend(R);
286 R.getOffset(Address);
287 return RelocToApply(Value + Addend - Address, 4);
289 RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
290 int64_t Addend = getELFAddend(R);
291 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
292 return RelocToApply(Res, 4);
294 RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) {
295 int64_t Addend = getELFAddend(R);
296 int32_t Res = (Value + Addend) & 0xFFFFFFFF;
297 return RelocToApply(Res, 4);
301 RelocToApply visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) {
302 int64_t Addend = getELFAddend(R);
303 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
304 return RelocToApply(Res, 4);
306 RelocToApply visitELF_PPC64_ADDR64(RelocationRef R, uint64_t Value) {
307 int64_t Addend = getELFAddend(R);
308 return RelocToApply(Value + Addend, 8);
312 RelocToApply visitELF_PPC_ADDR32(RelocationRef R, uint64_t Value) {
313 int64_t Addend = getELFAddend(R);
314 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
315 return RelocToApply(Res, 4);
319 RelocToApply visitELF_MIPS_32(RelocationRef R, uint64_t Value) {
320 uint32_t Res = (Value)&0xFFFFFFFF;
321 return RelocToApply(Res, 4);
324 RelocToApply visitELF_MIPS_64(RelocationRef R, uint64_t Value) {
325 int64_t Addend = getELFAddend(R);
326 uint64_t Res = (Value + Addend);
327 return RelocToApply(Res, 8);
331 RelocToApply visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) {
332 int64_t Addend = getELFAddend(R);
333 int64_t Res = Value + Addend;
335 // Overflow check allows for both signed and unsigned interpretation.
336 if (Res < INT32_MIN || Res > UINT32_MAX)
339 return RelocToApply(static_cast<uint32_t>(Res), 4);
342 RelocToApply visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) {
343 int64_t Addend = getELFAddend(R);
344 return RelocToApply(Value + Addend, 8);
348 RelocToApply visitELF_390_32(RelocationRef R, uint64_t Value) {
349 int64_t Addend = getELFAddend(R);
350 int64_t Res = Value + Addend;
352 // Overflow check allows for both signed and unsigned interpretation.
353 if (Res < INT32_MIN || Res > UINT32_MAX)
356 return RelocToApply(static_cast<uint32_t>(Res), 4);
359 RelocToApply visitELF_390_64(RelocationRef R, uint64_t Value) {
360 int64_t Addend = getELFAddend(R);
361 return RelocToApply(Value + Addend, 8);
364 RelocToApply visitELF_SPARC_32(RelocationRef R, uint32_t Value) {
365 int32_t Addend = getELFAddend(R);
366 return RelocToApply(Value + Addend, 4);
369 RelocToApply visitELF_SPARCV9_32(RelocationRef R, uint64_t Value) {
370 int32_t Addend = getELFAddend(R);
371 return RelocToApply(Value + Addend, 4);
374 RelocToApply visitELF_SPARCV9_64(RelocationRef R, uint64_t Value) {
375 int64_t Addend = getELFAddend(R);
376 return RelocToApply(Value + Addend, 8);
379 RelocToApply visitELF_ARM_ABS32(RelocationRef R, uint64_t Value) {
382 // Overflow check allows for both signed and unsigned interpretation.
383 if (Res < INT32_MIN || Res > UINT32_MAX)
386 return RelocToApply(static_cast<uint32_t>(Res), 4);
390 RelocToApply visitCOFF_I386_SECREL(RelocationRef R, uint64_t Value) {
391 return RelocToApply(static_cast<uint32_t>(Value), /*Width=*/4);
394 RelocToApply visitCOFF_I386_DIR32(RelocationRef R, uint64_t Value) {
395 return RelocToApply(static_cast<uint32_t>(Value), /*Width=*/4);
399 RelocToApply visitCOFF_AMD64_SECREL(RelocationRef R, uint64_t Value) {
400 return RelocToApply(static_cast<uint32_t>(Value), /*Width=*/4);
403 RelocToApply visitCOFF_AMD64_ADDR64(RelocationRef R, uint64_t Value) {
404 return RelocToApply(Value, /*Width=*/8);
408 RelocToApply visitMACHO_X86_64_UNSIGNED(RelocationRef R, uint64_t Value) {
409 uint8_t Length = getLengthMachO64(R);
411 return RelocToApply(Value, Length);