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();
158 return RelocToApply();
161 bool error() { return HasError; }
164 StringRef FileFormat;
167 int64_t getAddend32LE(RelocationRef R) {
168 const ELF32LEObjectFile *Obj = cast<ELF32LEObjectFile>(R.getObjectFile());
169 DataRefImpl DRI = R.getRawDataRefImpl();
171 Obj->getRelocationAddend(DRI, Addend);
175 int64_t getAddend64LE(RelocationRef R) {
176 const ELF64LEObjectFile *Obj = cast<ELF64LEObjectFile>(R.getObjectFile());
177 DataRefImpl DRI = R.getRawDataRefImpl();
179 Obj->getRelocationAddend(DRI, Addend);
183 int64_t getAddend32BE(RelocationRef R) {
184 const ELF32BEObjectFile *Obj = cast<ELF32BEObjectFile>(R.getObjectFile());
185 DataRefImpl DRI = R.getRawDataRefImpl();
187 Obj->getRelocationAddend(DRI, Addend);
191 int64_t getAddend64BE(RelocationRef R) {
192 const ELF64BEObjectFile *Obj = cast<ELF64BEObjectFile>(R.getObjectFile());
193 DataRefImpl DRI = R.getRawDataRefImpl();
195 Obj->getRelocationAddend(DRI, Addend);
201 RelocToApply visitELF_386_NONE(RelocationRef R) {
202 return RelocToApply(0, 0);
205 // Ideally the Addend here will be the addend in the data for
206 // the relocation. It's not actually the case for Rel relocations.
207 RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) {
208 int64_t Addend = getAddend32LE(R);
209 return RelocToApply(Value + Addend, 4);
212 RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value,
214 int64_t Addend = getAddend32LE(R);
216 R.getOffset(Address);
217 return RelocToApply(Value + Addend - Address, 4);
221 RelocToApply visitELF_X86_64_NONE(RelocationRef R) {
222 return RelocToApply(0, 0);
224 RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) {
225 int64_t Addend = getAddend64LE(R);
226 return RelocToApply(Value + Addend, 8);
228 RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value,
230 int64_t Addend = getAddend64LE(R);
232 R.getOffset(Address);
233 return RelocToApply(Value + Addend - Address, 4);
235 RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
236 int64_t Addend = getAddend64LE(R);
237 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
238 return RelocToApply(Res, 4);
240 RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) {
241 int64_t Addend = getAddend64LE(R);
242 int32_t Res = (Value + Addend) & 0xFFFFFFFF;
243 return RelocToApply(Res, 4);
247 RelocToApply visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) {
248 int64_t Addend = getAddend64BE(R);
249 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
250 return RelocToApply(Res, 4);
252 RelocToApply visitELF_PPC64_ADDR64(RelocationRef R, uint64_t Value) {
253 int64_t Addend = getAddend64BE(R);
254 return RelocToApply(Value + Addend, 8);
258 RelocToApply visitELF_PPC_ADDR32(RelocationRef R, uint64_t Value) {
259 int64_t Addend = getAddend32BE(R);
260 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
261 return RelocToApply(Res, 4);
265 RelocToApply visitELF_MIPS_32(RelocationRef R, uint64_t Value) {
267 getELFRelocationAddend(R, Addend);
268 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
269 return RelocToApply(Res, 4);
272 RelocToApply visitELF_MIPS_64(RelocationRef R, uint64_t Value) {
274 getELFRelocationAddend(R, Addend);
275 uint64_t Res = (Value + Addend);
276 return RelocToApply(Res, 8);
280 RelocToApply visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) {
281 int64_t Addend = getAddend64LE(R);
282 int64_t Res = Value + Addend;
284 // Overflow check allows for both signed and unsigned interpretation.
285 if (Res < INT32_MIN || Res > UINT32_MAX)
288 return RelocToApply(static_cast<uint32_t>(Res), 4);
291 RelocToApply visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) {
292 int64_t Addend = getAddend64LE(R);
293 return RelocToApply(Value + Addend, 8);
297 RelocToApply visitELF_390_32(RelocationRef R, uint64_t Value) {
298 int64_t Addend = getAddend64BE(R);
299 int64_t Res = Value + Addend;
301 // Overflow check allows for both signed and unsigned interpretation.
302 if (Res < INT32_MIN || Res > UINT32_MAX)
305 return RelocToApply(static_cast<uint32_t>(Res), 4);
308 RelocToApply visitELF_390_64(RelocationRef R, uint64_t Value) {
309 int64_t Addend = getAddend64BE(R);
310 return RelocToApply(Value + Addend, 8);
313 RelocToApply visitELF_SPARC_32(RelocationRef R, uint32_t Value) {
314 int32_t Addend = getAddend32BE(R);
315 return RelocToApply(Value + Addend, 4);
318 RelocToApply visitELF_SPARCV9_32(RelocationRef R, uint64_t Value) {
319 int32_t Addend = getAddend64BE(R);
320 return RelocToApply(Value + Addend, 4);
323 RelocToApply visitELF_SPARCV9_64(RelocationRef R, uint64_t Value) {
324 int64_t Addend = getAddend64BE(R);
325 return RelocToApply(Value + Addend, 8);