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(StringRef FileFormat)
44 : FileFormat(FileFormat), 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 (FileFormat == "ELF64-x86-64") {
53 case llvm::ELF::R_X86_64_NONE:
54 return visitELF_X86_64_NONE(R);
55 case llvm::ELF::R_X86_64_64:
56 return visitELF_X86_64_64(R, Value);
57 case llvm::ELF::R_X86_64_PC32:
58 return visitELF_X86_64_PC32(R, Value, SecAddr);
59 case llvm::ELF::R_X86_64_32:
60 return visitELF_X86_64_32(R, Value);
61 case llvm::ELF::R_X86_64_32S:
62 return visitELF_X86_64_32S(R, Value);
65 return RelocToApply();
67 } else if (FileFormat == "ELF32-i386") {
69 case llvm::ELF::R_386_NONE:
70 return visitELF_386_NONE(R);
71 case llvm::ELF::R_386_32:
72 return visitELF_386_32(R, Value);
73 case llvm::ELF::R_386_PC32:
74 return visitELF_386_PC32(R, Value, SecAddr);
77 return RelocToApply();
79 } else if (FileFormat == "ELF64-ppc64") {
81 case llvm::ELF::R_PPC64_ADDR32:
82 return visitELF_PPC64_ADDR32(R, Value);
83 case llvm::ELF::R_PPC64_ADDR64:
84 return visitELF_PPC64_ADDR64(R, Value);
87 return RelocToApply();
89 } else if (FileFormat == "ELF32-ppc") {
91 case llvm::ELF::R_PPC_ADDR32:
92 return visitELF_PPC_ADDR32(R, Value);
95 return RelocToApply();
97 } else if (FileFormat == "ELF32-mips") {
99 case llvm::ELF::R_MIPS_32:
100 return visitELF_MIPS_32(R, Value);
103 return RelocToApply();
105 } else if (FileFormat == "ELF64-mips") {
107 case llvm::ELF::R_MIPS_32:
108 return visitELF_MIPS_32(R, Value);
109 case llvm::ELF::R_MIPS_64:
110 return visitELF_MIPS_64(R, Value);
113 return RelocToApply();
115 } else if (FileFormat == "ELF64-aarch64") {
117 case llvm::ELF::R_AARCH64_ABS32:
118 return visitELF_AARCH64_ABS32(R, Value);
119 case llvm::ELF::R_AARCH64_ABS64:
120 return visitELF_AARCH64_ABS64(R, Value);
123 return RelocToApply();
125 } else if (FileFormat == "ELF64-s390") {
127 case llvm::ELF::R_390_32:
128 return visitELF_390_32(R, Value);
129 case llvm::ELF::R_390_64:
130 return visitELF_390_64(R, Value);
133 return RelocToApply();
135 } else if (FileFormat == "ELF32-sparc") {
137 case llvm::ELF::R_SPARC_32:
138 case llvm::ELF::R_SPARC_UA32:
139 return visitELF_SPARC_32(R, Value);
142 return RelocToApply();
144 } else if (FileFormat == "ELF64-sparc") {
146 case llvm::ELF::R_SPARC_32:
147 case llvm::ELF::R_SPARC_UA32:
148 return visitELF_SPARCV9_32(R, Value);
149 case llvm::ELF::R_SPARC_64:
150 case llvm::ELF::R_SPARC_UA64:
151 return visitELF_SPARCV9_64(R, Value);
154 return RelocToApply();
156 } else if (FileFormat == "ELF32-arm") {
160 return RelocToApply();
161 case llvm::ELF::R_ARM_ABS32:
162 return visitELF_ARM_ABS32(R, Value);
166 return RelocToApply();
169 bool error() { return HasError; }
172 StringRef FileFormat;
175 int64_t getAddend32LE(RelocationRef R) {
176 const ELF32LEObjectFile *Obj = cast<ELF32LEObjectFile>(R.getObjectFile());
177 DataRefImpl DRI = R.getRawDataRefImpl();
179 Obj->getRelocationAddend(DRI, Addend);
183 int64_t getAddend64LE(RelocationRef R) {
184 const ELF64LEObjectFile *Obj = cast<ELF64LEObjectFile>(R.getObjectFile());
185 DataRefImpl DRI = R.getRawDataRefImpl();
187 Obj->getRelocationAddend(DRI, Addend);
191 int64_t getAddend32BE(RelocationRef R) {
192 const ELF32BEObjectFile *Obj = cast<ELF32BEObjectFile>(R.getObjectFile());
193 DataRefImpl DRI = R.getRawDataRefImpl();
195 Obj->getRelocationAddend(DRI, Addend);
199 int64_t getAddend64BE(RelocationRef R) {
200 const ELF64BEObjectFile *Obj = cast<ELF64BEObjectFile>(R.getObjectFile());
201 DataRefImpl DRI = R.getRawDataRefImpl();
203 Obj->getRelocationAddend(DRI, Addend);
209 RelocToApply visitELF_386_NONE(RelocationRef R) {
210 return RelocToApply(0, 0);
213 // Ideally the Addend here will be the addend in the data for
214 // the relocation. It's not actually the case for Rel relocations.
215 RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) {
216 int64_t Addend = getAddend32LE(R);
217 return RelocToApply(Value + Addend, 4);
220 RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value,
222 int64_t Addend = getAddend32LE(R);
224 R.getOffset(Address);
225 return RelocToApply(Value + Addend - Address, 4);
229 RelocToApply visitELF_X86_64_NONE(RelocationRef R) {
230 return RelocToApply(0, 0);
232 RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) {
233 int64_t Addend = getAddend64LE(R);
234 return RelocToApply(Value + Addend, 8);
236 RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value,
238 int64_t Addend = getAddend64LE(R);
240 R.getOffset(Address);
241 return RelocToApply(Value + Addend - Address, 4);
243 RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
244 int64_t Addend = getAddend64LE(R);
245 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
246 return RelocToApply(Res, 4);
248 RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) {
249 int64_t Addend = getAddend64LE(R);
250 int32_t Res = (Value + Addend) & 0xFFFFFFFF;
251 return RelocToApply(Res, 4);
255 RelocToApply visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) {
256 int64_t Addend = getAddend64BE(R);
257 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
258 return RelocToApply(Res, 4);
260 RelocToApply visitELF_PPC64_ADDR64(RelocationRef R, uint64_t Value) {
261 int64_t Addend = getAddend64BE(R);
262 return RelocToApply(Value + Addend, 8);
266 RelocToApply visitELF_PPC_ADDR32(RelocationRef R, uint64_t Value) {
267 int64_t Addend = getAddend32BE(R);
268 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
269 return RelocToApply(Res, 4);
273 RelocToApply visitELF_MIPS_32(RelocationRef R, uint64_t Value) {
275 getELFRelocationAddend(R, Addend);
276 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
277 return RelocToApply(Res, 4);
280 RelocToApply visitELF_MIPS_64(RelocationRef R, uint64_t Value) {
282 getELFRelocationAddend(R, Addend);
283 uint64_t Res = (Value + Addend);
284 return RelocToApply(Res, 8);
288 RelocToApply visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) {
289 int64_t Addend = getAddend64LE(R);
290 int64_t Res = Value + Addend;
292 // Overflow check allows for both signed and unsigned interpretation.
293 if (Res < INT32_MIN || Res > UINT32_MAX)
296 return RelocToApply(static_cast<uint32_t>(Res), 4);
299 RelocToApply visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) {
300 int64_t Addend = getAddend64LE(R);
301 return RelocToApply(Value + Addend, 8);
305 RelocToApply visitELF_390_32(RelocationRef R, uint64_t Value) {
306 int64_t Addend = getAddend64BE(R);
307 int64_t Res = Value + Addend;
309 // Overflow check allows for both signed and unsigned interpretation.
310 if (Res < INT32_MIN || Res > UINT32_MAX)
313 return RelocToApply(static_cast<uint32_t>(Res), 4);
316 RelocToApply visitELF_390_64(RelocationRef R, uint64_t Value) {
317 int64_t Addend = getAddend64BE(R);
318 return RelocToApply(Value + Addend, 8);
321 RelocToApply visitELF_SPARC_32(RelocationRef R, uint32_t Value) {
322 int32_t Addend = getAddend32BE(R);
323 return RelocToApply(Value + Addend, 4);
326 RelocToApply visitELF_SPARCV9_32(RelocationRef R, uint64_t Value) {
327 int32_t Addend = getAddend64BE(R);
328 return RelocToApply(Value + Addend, 4);
331 RelocToApply visitELF_SPARCV9_64(RelocationRef R, uint64_t Value) {
332 int64_t Addend = getAddend64BE(R);
333 return RelocToApply(Value + Addend, 8);
336 RelocToApply visitELF_ARM_ABS32(RelocationRef R, uint64_t Value) {
337 int64_t Addend = getAddend32LE(R);
338 return RelocToApply(Value + Addend, 4);