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/ELFObjectFile.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ELF.h"
24 #include "llvm/Support/raw_ostream.h"
30 // The computed value after applying the relevant relocations.
33 // The width of the value; how many bytes to touch when applying the
36 RelocToApply(int64_t Value, char Width) : Value(Value), Width(Width) {}
37 RelocToApply() : Value(0), Width(0) {}
40 /// @brief Base class for object file relocation visitors.
43 explicit RelocVisitor(ObjectFile &Obj)
44 : ObjToVisit(Obj), HasError(false) {}
46 // TODO: Should handle multiple applied relocations via either passing in the
47 // previously computed value or just count paired relocations as a single
49 RelocToApply visit(uint32_t RelocType, RelocationRef R, uint64_t SecAddr = 0,
51 if (ObjToVisit.getBytesInAddress() == 8) { // 64-bit object file
52 switch (ObjToVisit.getArch()) {
55 case llvm::ELF::R_X86_64_NONE:
56 return visitELF_X86_64_NONE(R);
57 case llvm::ELF::R_X86_64_64:
58 return visitELF_X86_64_64(R, Value);
59 case llvm::ELF::R_X86_64_PC32:
60 return visitELF_X86_64_PC32(R, Value, SecAddr);
61 case llvm::ELF::R_X86_64_32:
62 return visitELF_X86_64_32(R, Value);
63 case llvm::ELF::R_X86_64_32S:
64 return visitELF_X86_64_32S(R, Value);
67 return RelocToApply();
71 case llvm::ELF::R_AARCH64_ABS32:
72 return visitELF_AARCH64_ABS32(R, Value);
73 case llvm::ELF::R_AARCH64_ABS64:
74 return visitELF_AARCH64_ABS64(R, Value);
77 return RelocToApply();
79 case Triple::mips64el:
82 case llvm::ELF::R_MIPS_32:
83 return visitELF_MIPS_32(R, Value);
84 case llvm::ELF::R_MIPS_64:
85 return visitELF_MIPS_64(R, Value);
88 return RelocToApply();
93 case llvm::ELF::R_PPC64_ADDR32:
94 return visitELF_PPC64_ADDR32(R, Value);
95 case llvm::ELF::R_PPC64_ADDR64:
96 return visitELF_PPC64_ADDR64(R, Value);
99 return RelocToApply();
101 case Triple::systemz:
103 case llvm::ELF::R_390_32:
104 return visitELF_390_32(R, Value);
105 case llvm::ELF::R_390_64:
106 return visitELF_390_64(R, Value);
109 return RelocToApply();
111 case Triple::sparcv9:
113 case llvm::ELF::R_SPARC_32:
114 case llvm::ELF::R_SPARC_UA32:
115 return visitELF_SPARCV9_32(R, Value);
116 case llvm::ELF::R_SPARC_64:
117 case llvm::ELF::R_SPARC_UA64:
118 return visitELF_SPARCV9_64(R, Value);
121 return RelocToApply();
125 return RelocToApply();
127 } else if (ObjToVisit.getBytesInAddress() == 4) { // 32-bit object file
128 switch (ObjToVisit.getArch()) {
131 case llvm::ELF::R_386_NONE:
132 return visitELF_386_NONE(R);
133 case llvm::ELF::R_386_32:
134 return visitELF_386_32(R, Value);
135 case llvm::ELF::R_386_PC32:
136 return visitELF_386_PC32(R, Value, SecAddr);
139 return RelocToApply();
143 case llvm::ELF::R_PPC_ADDR32:
144 return visitELF_PPC_ADDR32(R, Value);
147 return RelocToApply();
154 return RelocToApply();
155 case llvm::ELF::R_ARM_ABS32:
156 return visitELF_ARM_ABS32(R, Value);
161 case llvm::ELF::R_MIPS_32:
162 return visitELF_MIPS_32(R, Value);
165 return RelocToApply();
169 case llvm::ELF::R_SPARC_32:
170 case llvm::ELF::R_SPARC_UA32:
171 return visitELF_SPARC_32(R, Value);
174 return RelocToApply();
178 return RelocToApply();
181 report_fatal_error("Invalid word size in object file");
185 bool error() { return HasError; }
188 ObjectFile &ObjToVisit;
191 int64_t getAddend32LE(RelocationRef R) {
192 const ELF32LEObjectFile *Obj = cast<ELF32LEObjectFile>(R.getObjectFile());
193 DataRefImpl DRI = R.getRawDataRefImpl();
195 Obj->getRelocationAddend(DRI, Addend);
199 int64_t getAddend64LE(RelocationRef R) {
200 const ELF64LEObjectFile *Obj = cast<ELF64LEObjectFile>(R.getObjectFile());
201 DataRefImpl DRI = R.getRawDataRefImpl();
203 Obj->getRelocationAddend(DRI, Addend);
207 int64_t getAddend32BE(RelocationRef R) {
208 const ELF32BEObjectFile *Obj = cast<ELF32BEObjectFile>(R.getObjectFile());
209 DataRefImpl DRI = R.getRawDataRefImpl();
211 Obj->getRelocationAddend(DRI, Addend);
215 int64_t getAddend64BE(RelocationRef R) {
216 const ELF64BEObjectFile *Obj = cast<ELF64BEObjectFile>(R.getObjectFile());
217 DataRefImpl DRI = R.getRawDataRefImpl();
219 Obj->getRelocationAddend(DRI, Addend);
225 RelocToApply visitELF_386_NONE(RelocationRef R) {
226 return RelocToApply(0, 0);
229 // Ideally the Addend here will be the addend in the data for
230 // the relocation. It's not actually the case for Rel relocations.
231 RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) {
232 int64_t Addend = getAddend32LE(R);
233 return RelocToApply(Value + Addend, 4);
236 RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value,
238 int64_t Addend = getAddend32LE(R);
240 R.getOffset(Address);
241 return RelocToApply(Value + Addend - Address, 4);
245 RelocToApply visitELF_X86_64_NONE(RelocationRef R) {
246 return RelocToApply(0, 0);
248 RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) {
249 int64_t Addend = getAddend64LE(R);
250 return RelocToApply(Value + Addend, 8);
252 RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value,
254 int64_t Addend = getAddend64LE(R);
256 R.getOffset(Address);
257 return RelocToApply(Value + Addend - Address, 4);
259 RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
260 int64_t Addend = getAddend64LE(R);
261 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
262 return RelocToApply(Res, 4);
264 RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) {
265 int64_t Addend = getAddend64LE(R);
266 int32_t Res = (Value + Addend) & 0xFFFFFFFF;
267 return RelocToApply(Res, 4);
271 RelocToApply visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) {
273 getELFRelocationAddend(R, Addend);
274 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
275 return RelocToApply(Res, 4);
277 RelocToApply visitELF_PPC64_ADDR64(RelocationRef R, uint64_t Value) {
279 getELFRelocationAddend(R, Addend);
280 return RelocToApply(Value + Addend, 8);
284 RelocToApply visitELF_PPC_ADDR32(RelocationRef R, uint64_t Value) {
285 int64_t Addend = getAddend32BE(R);
286 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
287 return RelocToApply(Res, 4);
291 RelocToApply visitELF_MIPS_32(RelocationRef R, uint64_t Value) {
293 getELFRelocationAddend(R, Addend);
294 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
295 return RelocToApply(Res, 4);
298 RelocToApply visitELF_MIPS_64(RelocationRef R, uint64_t Value) {
300 getELFRelocationAddend(R, Addend);
301 uint64_t Res = (Value + Addend);
302 return RelocToApply(Res, 8);
306 RelocToApply visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) {
307 int64_t Addend = getAddend64LE(R);
308 int64_t Res = Value + Addend;
310 // Overflow check allows for both signed and unsigned interpretation.
311 if (Res < INT32_MIN || Res > UINT32_MAX)
314 return RelocToApply(static_cast<uint32_t>(Res), 4);
317 RelocToApply visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) {
318 int64_t Addend = getAddend64LE(R);
319 return RelocToApply(Value + Addend, 8);
323 RelocToApply visitELF_390_32(RelocationRef R, uint64_t Value) {
324 int64_t Addend = getAddend64BE(R);
325 int64_t Res = Value + Addend;
327 // Overflow check allows for both signed and unsigned interpretation.
328 if (Res < INT32_MIN || Res > UINT32_MAX)
331 return RelocToApply(static_cast<uint32_t>(Res), 4);
334 RelocToApply visitELF_390_64(RelocationRef R, uint64_t Value) {
335 int64_t Addend = getAddend64BE(R);
336 return RelocToApply(Value + Addend, 8);
339 RelocToApply visitELF_SPARC_32(RelocationRef R, uint32_t Value) {
340 int32_t Addend = getAddend32BE(R);
341 return RelocToApply(Value + Addend, 4);
344 RelocToApply visitELF_SPARCV9_32(RelocationRef R, uint64_t Value) {
345 int32_t Addend = getAddend64BE(R);
346 return RelocToApply(Value + Addend, 4);
349 RelocToApply visitELF_SPARCV9_64(RelocationRef R, uint64_t Value) {
350 int64_t Addend = getAddend64BE(R);
351 return RelocToApply(Value + Addend, 8);
354 RelocToApply visitELF_ARM_ABS32(RelocationRef R, uint64_t Value) {
355 int64_t Addend = getAddend32LE(R);
356 return RelocToApply(Value + Addend, 4);