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/Support/Debug.h"
22 #include "llvm/Support/ELF.h"
23 #include "llvm/Support/raw_ostream.h"
29 // The computed value after applying the relevant relocations.
32 // The width of the value; how many bytes to touch when applying the
35 RelocToApply(const RelocToApply &In) : Value(In.Value), Width(In.Width) {}
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);
85 return RelocToApply();
87 } else if (FileFormat == "ELF32-mips") {
89 case llvm::ELF::R_MIPS_32:
90 return visitELF_MIPS_32(R, Value);
93 return RelocToApply();
95 } else if (FileFormat == "ELF64-aarch64") {
97 case llvm::ELF::R_AARCH64_ABS32:
98 return visitELF_AARCH64_ABS32(R, Value);
99 case llvm::ELF::R_AARCH64_ABS64:
100 return visitELF_AARCH64_ABS64(R, Value);
103 return RelocToApply();
107 return RelocToApply();
110 bool error() { return HasError; }
113 StringRef FileFormat;
119 RelocToApply visitELF_386_NONE(RelocationRef R) {
120 return RelocToApply(0, 0);
123 // Ideally the Addend here will be the addend in the data for
124 // the relocation. It's not actually the case for Rel relocations.
125 RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) {
127 R.getAdditionalInfo(Addend);
128 return RelocToApply(Value + Addend, 4);
131 RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value,
134 R.getAdditionalInfo(Addend);
136 R.getAddress(Address);
137 return RelocToApply(Value + Addend - Address, 4);
141 RelocToApply visitELF_X86_64_NONE(RelocationRef R) {
142 return RelocToApply(0, 0);
144 RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) {
146 R.getAdditionalInfo(Addend);
147 return RelocToApply(Value + Addend, 8);
149 RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value,
152 R.getAdditionalInfo(Addend);
154 R.getAddress(Address);
155 return RelocToApply(Value + Addend - Address, 4);
157 RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
159 R.getAdditionalInfo(Addend);
160 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
161 return RelocToApply(Res, 4);
163 RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) {
165 R.getAdditionalInfo(Addend);
166 int32_t Res = (Value + Addend) & 0xFFFFFFFF;
167 return RelocToApply(Res, 4);
171 RelocToApply visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) {
173 R.getAdditionalInfo(Addend);
174 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
175 return RelocToApply(Res, 4);
179 RelocToApply visitELF_MIPS_32(RelocationRef R, uint64_t Value) {
181 R.getAdditionalInfo(Addend);
182 uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
183 return RelocToApply(Res, 4);
187 RelocToApply visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) {
189 R.getAdditionalInfo(Addend);
190 int64_t Res = Value + Addend;
192 // Overflow check allows for both signed and unsigned interpretation.
193 if (Res < INT32_MIN || Res > UINT32_MAX)
196 return RelocToApply(static_cast<uint32_t>(Res), 4);
199 RelocToApply visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) {
201 R.getAdditionalInfo(Addend);
202 return RelocToApply(Value + Addend, 8);