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/ObjectFile.h"
21 #include "llvm/Object/ELF.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(const RelocToApply &In) : Value(In.Value), Width(In.Width) {}
37 RelocToApply(int64_t Value, char Width) : Value(Value), Width(Width) {}
38 RelocToApply() : Value(0), Width(0) {}
41 /// @brief Base class for object file relocation visitors.
44 explicit RelocVisitor(StringRef FileFormat)
45 : FileFormat(FileFormat), HasError(false) {}
47 // TODO: Should handle multiple applied relocations via either passing in the
48 // previously computed value or just count paired relocations as a single
50 RelocToApply visit(uint32_t RelocType, RelocationRef R, uint64_t SecAddr = 0,
52 if (FileFormat == "ELF64-x86-64") {
54 case llvm::ELF::R_X86_64_NONE:
55 return visitELF_X86_64_NONE(R);
56 case llvm::ELF::R_X86_64_64:
57 return visitELF_X86_64_64(R, Value);
58 case llvm::ELF::R_X86_64_PC32:
59 return visitELF_X86_64_PC32(R, Value, SecAddr);
60 case llvm::ELF::R_X86_64_32:
61 return visitELF_X86_64_32(R, Value);
62 case llvm::ELF::R_X86_64_32S:
63 return visitELF_X86_64_32S(R, Value);
66 return RelocToApply();
68 } else if (FileFormat == "ELF32-i386") {
70 case llvm::ELF::R_386_NONE:
71 return visitELF_386_NONE(R);
72 case llvm::ELF::R_386_32:
73 return visitELF_386_32(R, Value);
74 case llvm::ELF::R_386_PC32:
75 return visitELF_386_PC32(R, Value, SecAddr);
78 return RelocToApply();
80 } else if (FileFormat == "ELF64-ppc64") {
82 case llvm::ELF::R_PPC64_ADDR32:
83 return visitELF_PPC64_ADDR32(R, Value);
86 return RelocToApply();
88 } else if (FileFormat == "ELF32-mips") {
90 case llvm::ELF::R_MIPS_32:
91 return visitELF_MIPS_32(R, Value);
94 return RelocToApply();
96 } else if (FileFormat == "ELF64-aarch64") {
98 case llvm::ELF::R_AARCH64_ABS32:
99 return visitELF_AARCH64_ABS32(R, Value);
100 case llvm::ELF::R_AARCH64_ABS64:
101 return visitELF_AARCH64_ABS64(R, Value);
104 return RelocToApply();
106 } else if (FileFormat == "ELF64-s390") {
108 case llvm::ELF::R_390_32:
109 return visitELF_390_32(R, Value);
110 case llvm::ELF::R_390_64:
111 return visitELF_390_64(R, Value);
114 return RelocToApply();
118 return RelocToApply();
121 bool error() { return HasError; }
124 StringRef FileFormat;
127 int64_t getAddend32LE(RelocationRef R) {
128 const ELF32LEObjectFile *Obj = cast<ELF32LEObjectFile>(R.getObjectFile());
129 DataRefImpl DRI = R.getRawDataRefImpl();
131 Obj->getRelocationAddend(DRI, Addend);
135 int64_t getAddend64LE(RelocationRef R) {
136 const ELF64LEObjectFile *Obj = cast<ELF64LEObjectFile>(R.getObjectFile());
137 DataRefImpl DRI = R.getRawDataRefImpl();
139 Obj->getRelocationAddend(DRI, Addend);
143 int64_t getAddend32BE(RelocationRef R) {
144 const ELF32BEObjectFile *Obj = cast<ELF32BEObjectFile>(R.getObjectFile());
145 DataRefImpl DRI = R.getRawDataRefImpl();
147 Obj->getRelocationAddend(DRI, Addend);
151 int64_t getAddend64BE(RelocationRef R) {
152 const ELF64BEObjectFile *Obj = cast<ELF64BEObjectFile>(R.getObjectFile());
153 DataRefImpl DRI = R.getRawDataRefImpl();
155 Obj->getRelocationAddend(DRI, Addend);
161 RelocToApply visitELF_386_NONE(RelocationRef R) {
162 return RelocToApply(0, 0);
165 // Ideally the Addend here will be the addend in the data for
166 // the relocation. It's not actually the case for Rel relocations.
167 RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) {
168 int64_t Addend = getAddend32LE(R);
169 return RelocToApply(Value + Addend, 4);
172 RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value,
174 int64_t Addend = getAddend32LE(R);
176 R.getOffset(Address);
177 return RelocToApply(Value + Addend - Address, 4);
181 RelocToApply visitELF_X86_64_NONE(RelocationRef R) {
182 return RelocToApply(0, 0);
184 RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) {
185 int64_t Addend = getAddend64LE(R);
186 return RelocToApply(Value + Addend, 8);
188 RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value,
190 int64_t Addend = getAddend64LE(R);
192 R.getOffset(Address);
193 return RelocToApply(Value + Addend - Address, 4);
195 RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
196 int64_t Addend = getAddend64LE(R);
197 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
198 return RelocToApply(Res, 4);
200 RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) {
201 int64_t Addend = getAddend64LE(R);
202 int32_t Res = (Value + Addend) & 0xFFFFFFFF;
203 return RelocToApply(Res, 4);
207 RelocToApply visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) {
208 int64_t Addend = getAddend64BE(R);
209 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
210 return RelocToApply(Res, 4);
214 RelocToApply visitELF_MIPS_32(RelocationRef R, uint64_t Value) {
216 getELFRelocationAddend(R, Addend);
217 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
218 return RelocToApply(Res, 4);
222 RelocToApply visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) {
223 int64_t Addend = getAddend64LE(R);
224 int64_t Res = Value + Addend;
226 // Overflow check allows for both signed and unsigned interpretation.
227 if (Res < INT32_MIN || Res > UINT32_MAX)
230 return RelocToApply(static_cast<uint32_t>(Res), 4);
233 RelocToApply visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) {
234 int64_t Addend = getAddend64LE(R);
235 return RelocToApply(Value + Addend, 8);
239 RelocToApply visitELF_390_32(RelocationRef R, uint64_t Value) {
240 int64_t Addend = getAddend64BE(R);
241 int64_t Res = Value + Addend;
243 // Overflow check allows for both signed and unsigned interpretation.
244 if (Res < INT32_MIN || Res > UINT32_MAX)
247 return RelocToApply(static_cast<uint32_t>(Res), 4);
250 RelocToApply visitELF_390_64(RelocationRef R, uint64_t Value) {
251 int64_t Addend = getAddend64BE(R);
252 return RelocToApply(Value + Addend, 8);