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);
158 case Triple::hexagon:
159 llvm_unreachable("Unimplemented");
163 case llvm::ELF::R_MIPS_32:
164 return visitELF_MIPS_32(R, Value);
167 return RelocToApply();
171 case llvm::ELF::R_SPARC_32:
172 case llvm::ELF::R_SPARC_UA32:
173 return visitELF_SPARC_32(R, Value);
176 return RelocToApply();
180 return RelocToApply();
183 report_fatal_error("Invalid word size in object file");
187 bool error() { return HasError; }
190 ObjectFile &ObjToVisit;
193 int64_t getAddend32LE(RelocationRef R) {
194 const ELF32LEObjectFile *Obj = cast<ELF32LEObjectFile>(R.getObjectFile());
195 DataRefImpl DRI = R.getRawDataRefImpl();
197 Obj->getRelocationAddend(DRI, Addend);
201 int64_t getAddend64LE(RelocationRef R) {
202 const ELF64LEObjectFile *Obj = cast<ELF64LEObjectFile>(R.getObjectFile());
203 DataRefImpl DRI = R.getRawDataRefImpl();
205 Obj->getRelocationAddend(DRI, Addend);
209 int64_t getAddend32BE(RelocationRef R) {
210 const ELF32BEObjectFile *Obj = cast<ELF32BEObjectFile>(R.getObjectFile());
211 DataRefImpl DRI = R.getRawDataRefImpl();
213 Obj->getRelocationAddend(DRI, Addend);
217 int64_t getAddend64BE(RelocationRef R) {
218 const ELF64BEObjectFile *Obj = cast<ELF64BEObjectFile>(R.getObjectFile());
219 DataRefImpl DRI = R.getRawDataRefImpl();
221 Obj->getRelocationAddend(DRI, Addend);
227 RelocToApply visitELF_386_NONE(RelocationRef R) {
228 return RelocToApply(0, 0);
231 // Ideally the Addend here will be the addend in the data for
232 // the relocation. It's not actually the case for Rel relocations.
233 RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) {
234 int64_t Addend = getAddend32LE(R);
235 return RelocToApply(Value + Addend, 4);
238 RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value,
240 int64_t Addend = getAddend32LE(R);
242 R.getOffset(Address);
243 return RelocToApply(Value + Addend - Address, 4);
247 RelocToApply visitELF_X86_64_NONE(RelocationRef R) {
248 return RelocToApply(0, 0);
250 RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) {
251 int64_t Addend = getAddend64LE(R);
252 return RelocToApply(Value + Addend, 8);
254 RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value,
256 int64_t Addend = getAddend64LE(R);
258 R.getOffset(Address);
259 return RelocToApply(Value + Addend - Address, 4);
261 RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
262 int64_t Addend = getAddend64LE(R);
263 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
264 return RelocToApply(Res, 4);
266 RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) {
267 int64_t Addend = getAddend64LE(R);
268 int32_t Res = (Value + Addend) & 0xFFFFFFFF;
269 return RelocToApply(Res, 4);
273 RelocToApply visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) {
275 getELFRelocationAddend(R, Addend);
276 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
277 return RelocToApply(Res, 4);
279 RelocToApply visitELF_PPC64_ADDR64(RelocationRef R, uint64_t Value) {
281 getELFRelocationAddend(R, Addend);
282 return RelocToApply(Value + Addend, 8);
286 RelocToApply visitELF_PPC_ADDR32(RelocationRef R, uint64_t Value) {
287 int64_t Addend = getAddend32BE(R);
288 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
289 return RelocToApply(Res, 4);
293 RelocToApply visitELF_MIPS_32(RelocationRef R, uint64_t Value) {
295 getELFRelocationAddend(R, Addend);
296 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
297 return RelocToApply(Res, 4);
300 RelocToApply visitELF_MIPS_64(RelocationRef R, uint64_t Value) {
302 getELFRelocationAddend(R, Addend);
303 uint64_t Res = (Value + Addend);
304 return RelocToApply(Res, 8);
308 RelocToApply visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) {
310 getELFRelocationAddend(R, Addend);
311 int64_t Res = Value + Addend;
313 // Overflow check allows for both signed and unsigned interpretation.
314 if (Res < INT32_MIN || Res > UINT32_MAX)
317 return RelocToApply(static_cast<uint32_t>(Res), 4);
320 RelocToApply visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) {
322 getELFRelocationAddend(R, Addend);
323 return RelocToApply(Value + Addend, 8);
327 RelocToApply visitELF_390_32(RelocationRef R, uint64_t Value) {
328 int64_t Addend = getAddend64BE(R);
329 int64_t Res = Value + Addend;
331 // Overflow check allows for both signed and unsigned interpretation.
332 if (Res < INT32_MIN || Res > UINT32_MAX)
335 return RelocToApply(static_cast<uint32_t>(Res), 4);
338 RelocToApply visitELF_390_64(RelocationRef R, uint64_t Value) {
339 int64_t Addend = getAddend64BE(R);
340 return RelocToApply(Value + Addend, 8);
343 RelocToApply visitELF_SPARC_32(RelocationRef R, uint32_t Value) {
344 int32_t Addend = getAddend32BE(R);
345 return RelocToApply(Value + Addend, 4);
348 RelocToApply visitELF_SPARCV9_32(RelocationRef R, uint64_t Value) {
349 int32_t Addend = getAddend64BE(R);
350 return RelocToApply(Value + Addend, 4);
353 RelocToApply visitELF_SPARCV9_64(RelocationRef R, uint64_t Value) {
354 int64_t Addend = getAddend64BE(R);
355 return RelocToApply(Value + Addend, 8);
358 RelocToApply visitELF_ARM_ABS32(RelocationRef R, uint64_t Value) {
360 getELFRelocationAddend(R, Addend);
361 int64_t Res = Value + Addend;
363 // Overflow check allows for both signed and unsigned interpretation.
364 if (Res < INT32_MIN || Res > UINT32_MAX)
367 return RelocToApply(static_cast<uint32_t>(Res), 4);