16 #ifndef LLVM_OBJECT_RELOCVISITOR_H
17 #define LLVM_OBJECT_RELOCVISITOR_H
30 #include <system_error>
44 if (isa<ELFObjectFileBase>(ObjToVisit))
45 return visitELF(Rel, R,
Value);
46 if (isa<COFFObjectFile>(ObjToVisit))
47 return visitCOFF(Rel, R,
Value);
48 if (isa<MachOObjectFile>(ObjToVisit))
49 return visitMachO(Rel, R,
Value);
55 bool error() {
return HasError; }
59 bool HasError =
false;
65 return visitX86_64(Rel, R, Value);
68 return visitAarch64(Rel, R, Value);
71 return visitBpf(Rel, R, Value);
74 return visitMips64(Rel, R, Value);
77 return visitPPC64(Rel, R, Value);
79 return visitSystemz(Rel, R, Value);
81 return visitSparc64(Rel, R, Value);
83 return visitAmdgpu(Rel, R, Value);
92 "Invalid word size in object file");
96 return visitX86(Rel, R, Value);
98 return visitPPC32(Rel, R, Value);
101 return visitARM(Rel, R, Value);
103 return visitLanai(Rel, R, Value);
106 return visitMips32(Rel, R, Value);
108 return visitSparc32(Rel, R, Value);
110 return visitHexagon(Rel, R, Value);
117 int64_t getELFAddend(RelocationRef R) {
118 ErrorOr<int64_t> AddendOrErr = ELFRelocationRef(R).getAddend();
119 if (std::error_code EC = AddendOrErr.getError())
124 uint64_t visitX86_64(
uint32_t Rel, RelocationRef R, uint64_t Value) {
126 case ELF::R_X86_64_NONE:
128 case ELF::R_X86_64_64:
129 return Value + getELFAddend(R);
130 case ELF::R_X86_64_PC32:
131 return Value + getELFAddend(R) - R.getOffset();
132 case ELF::R_X86_64_32:
133 case ELF::R_X86_64_32S:
134 return (Value + getELFAddend(R)) & 0xFFFFFFFF;
140 uint64_t visitAarch64(
uint32_t Rel, RelocationRef R, uint64_t Value) {
142 case ELF::R_AARCH64_ABS32: {
143 int64_t Res = Value + getELFAddend(R);
144 if (Res < INT32_MIN || Res > UINT32_MAX)
148 case ELF::R_AARCH64_ABS64:
149 return Value + getELFAddend(R);
155 uint64_t visitBpf(
uint32_t Rel, RelocationRef R, uint64_t Value) {
157 case ELF::R_BPF_64_32:
158 return Value & 0xFFFFFFFF;
159 case ELF::R_BPF_64_64:
166 uint64_t visitMips64(
uint32_t Rel, RelocationRef R, uint64_t Value) {
169 return (Value + getELFAddend(R)) & 0xFFFFFFFF;
171 return Value + getELFAddend(R);
177 uint64_t visitPPC64(
uint32_t Rel, RelocationRef R, uint64_t Value) {
179 case ELF::R_PPC64_ADDR32:
180 return (Value + getELFAddend(R)) & 0xFFFFFFFF;
181 case ELF::R_PPC64_ADDR64:
182 return Value + getELFAddend(R);
188 uint64_t visitSystemz(
uint32_t Rel, RelocationRef R, uint64_t Value) {
190 case ELF::R_390_32: {
191 int64_t Res = Value + getELFAddend(R);
192 if (Res < INT32_MIN || Res > UINT32_MAX)
197 return Value + getELFAddend(R);
203 uint64_t visitSparc64(
uint32_t Rel, RelocationRef R, uint64_t Value) {
205 case ELF::R_SPARC_32:
206 case ELF::R_SPARC_64:
207 case ELF::R_SPARC_UA32:
208 case ELF::R_SPARC_UA64:
209 return Value + getELFAddend(R);
215 uint64_t visitAmdgpu(
uint32_t Rel, RelocationRef R, uint64_t Value) {
217 case ELF::R_AMDGPU_ABS32:
218 case ELF::R_AMDGPU_ABS64:
219 return Value + getELFAddend(R);
225 uint64_t visitX86(
uint32_t Rel, RelocationRef R, uint64_t Value) {
227 case ELF::R_386_NONE:
231 case ELF::R_386_PC32:
232 return Value - R.getOffset();
238 uint64_t visitPPC32(
uint32_t Rel, RelocationRef R, uint64_t Value) {
239 if (Rel == ELF::R_PPC_ADDR32)
240 return (Value + getELFAddend(R)) & 0xFFFFFFFF;
245 uint64_t visitARM(
uint32_t Rel, RelocationRef R, uint64_t Value) {
246 if (Rel == ELF::R_ARM_ABS32) {
247 if ((int64_t)Value < INT32_MIN || (int64_t)Value > UINT32_MAX)
249 return static_cast<uint32_t>(Value);
255 uint64_t visitLanai(
uint32_t Rel, RelocationRef R, uint64_t Value) {
256 if (Rel == ELF::R_LANAI_32)
257 return (Value + getELFAddend(R)) & 0xFFFFFFFF;
262 uint64_t visitMips32(
uint32_t Rel, RelocationRef R, uint64_t Value) {
263 if (Rel == ELF::R_MIPS_32)
264 return Value & 0xFFFFFFFF;
269 uint64_t visitSparc32(
uint32_t Rel, RelocationRef R, uint64_t Value) {
270 if (Rel == ELF::R_SPARC_32 || Rel == ELF::R_SPARC_UA32)
271 return Value + getELFAddend(R);
276 uint64_t visitHexagon(
uint32_t Rel, RelocationRef R, uint64_t Value) {
277 if (Rel == ELF::R_HEX_32)
278 return Value + getELFAddend(R);
283 uint64_t visitCOFF(
uint32_t Rel, RelocationRef R, uint64_t Value) {
284 switch (ObjToVisit.
getArch()) {
289 return static_cast<uint32_t>(Value);
295 return static_cast<uint32_t>(Value);
305 uint64_t visitMachO(
uint32_t Rel, RelocationRef R, uint64_t Value) {
317 #endif // LLVM_OBJECT_RELOCVISITOR_H
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
RelocVisitor(const ObjectFile &Obj)
This class is the base class for all object file types.
virtual unsigned getArch() const =0
This is a value type class that represents a single relocation in the list of relocations in the obje...
virtual uint8_t getBytesInAddress() const =0
The number of bytes used to represent an address in this object file format.
Provides ErrorOr<T> smart pointer.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
uint64_t visit(uint32_t Rel, RelocationRef R, uint64_t Value=0)
Base class for object file relocation visitors.