blob: 6278170cd88aec9c1085e0909983149a2b19b4f6 [file] [log] [blame]
Jim Grosbach06594e12012-01-16 23:50:58 +00001//===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===//
Eli Bendersky4c647582012-01-16 08:56:09 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Implementation of ELF support for the MC-JIT runtime dynamic linker.
11//
12//===----------------------------------------------------------------------===//
13
Andrew Kayloradc70562012-10-02 21:18:39 +000014#include "RuntimeDyldELF.h"
Eli Bendersky4c647582012-01-16 08:56:09 +000015#include "llvm/ADT/IntervalMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/StringRef.h"
Eli Bendersky4c647582012-01-16 08:56:09 +000018#include "llvm/ADT/Triple.h"
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000019#include "llvm/MC/MCStreamer.h"
Michael J. Spencer126973b2013-08-08 22:27:13 +000020#include "llvm/Object/ELFObjectFile.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Object/ObjectFile.h"
22#include "llvm/Support/ELF.h"
Alexey Samsonova8d2f812014-08-27 23:06:08 +000023#include "llvm/Support/Endian.h"
Lang Hames173c69f2014-01-08 04:09:09 +000024#include "llvm/Support/MemoryBuffer.h"
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000025#include "llvm/Support/TargetRegistry.h"
Lang Hames173c69f2014-01-08 04:09:09 +000026
Eli Bendersky4c647582012-01-16 08:56:09 +000027using namespace llvm;
28using namespace llvm::object;
29
Chandler Carruthf58e3762014-04-22 03:04:17 +000030#define DEBUG_TYPE "dyld"
31
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000032static inline std::error_code check(std::error_code Err) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +000033 if (Err) {
34 report_fatal_error(Err.message());
35 }
36 return Err;
37}
38
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000039namespace {
40
Juergen Ributzka7608dc02014-03-21 20:28:42 +000041template <class ELFT> class DyldELFObject : public ELFObjectFile<ELFT> {
Rafael Espindola035b4162013-04-17 21:20:55 +000042 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Preston Gurdcc31af92012-04-16 22:12:58 +000043
Michael J. Spencer1a791612013-01-15 07:44:25 +000044 typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
45 typedef Elf_Sym_Impl<ELFT> Elf_Sym;
Juergen Ributzka7608dc02014-03-21 20:28:42 +000046 typedef Elf_Rel_Impl<ELFT, false> Elf_Rel;
47 typedef Elf_Rel_Impl<ELFT, true> Elf_Rela;
Preston Gurdcc31af92012-04-16 22:12:58 +000048
Michael J. Spencer1a791612013-01-15 07:44:25 +000049 typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
Preston Gurdcc31af92012-04-16 22:12:58 +000050
Juergen Ributzka7608dc02014-03-21 20:28:42 +000051 typedef typename ELFDataTypeTypedefHelper<ELFT>::value_type addr_type;
Preston Gurdcc31af92012-04-16 22:12:58 +000052
Preston Gurdcc31af92012-04-16 22:12:58 +000053public:
Rafael Espindola48af1c22014-08-19 18:44:46 +000054 DyldELFObject(MemoryBufferRef Wrapper, std::error_code &ec);
Preston Gurdcc31af92012-04-16 22:12:58 +000055
56 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000057
58 void updateSymbolAddress(const SymbolRef &SymRef, uint64_t Addr);
Preston Gurdcc31af92012-04-16 22:12:58 +000059
Andrew Kaylor5c010902012-07-27 17:52:42 +000060 // Methods for type inquiry through isa, cast and dyn_cast
Preston Gurdcc31af92012-04-16 22:12:58 +000061 static inline bool classof(const Binary *v) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +000062 return (isa<ELFObjectFile<ELFT>>(v) &&
63 classof(cast<ELFObjectFile<ELFT>>(v)));
Preston Gurdcc31af92012-04-16 22:12:58 +000064 }
Juergen Ributzka7608dc02014-03-21 20:28:42 +000065 static inline bool classof(const ELFObjectFile<ELFT> *v) {
Preston Gurdcc31af92012-04-16 22:12:58 +000066 return v->isDyldType();
67 }
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000068
Preston Gurdcc31af92012-04-16 22:12:58 +000069};
70
Preston Gurdcc31af92012-04-16 22:12:58 +000071
Preston Gurdcc31af92012-04-16 22:12:58 +000072
Andrew Kayloradc70562012-10-02 21:18:39 +000073// The MemoryBuffer passed into this constructor is just a wrapper around the
74// actual memory. Ultimately, the Binary parent class will take ownership of
75// this MemoryBuffer object but not the underlying memory.
Juergen Ributzka7608dc02014-03-21 20:28:42 +000076template <class ELFT>
Rafael Espindola48af1c22014-08-19 18:44:46 +000077DyldELFObject<ELFT>::DyldELFObject(MemoryBufferRef Wrapper, std::error_code &EC)
78 : ELFObjectFile<ELFT>(Wrapper, EC) {
Preston Gurdcc31af92012-04-16 22:12:58 +000079 this->isDyldELFObject = true;
80}
81
Juergen Ributzka7608dc02014-03-21 20:28:42 +000082template <class ELFT>
Michael J. Spencer1a791612013-01-15 07:44:25 +000083void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec,
84 uint64_t Addr) {
Preston Gurdcc31af92012-04-16 22:12:58 +000085 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
Juergen Ributzka7608dc02014-03-21 20:28:42 +000086 Elf_Shdr *shdr =
87 const_cast<Elf_Shdr *>(reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
Preston Gurdcc31af92012-04-16 22:12:58 +000088
89 // This assumes the address passed in matches the target address bitness
90 // The template-based type cast handles everything else.
91 shdr->sh_addr = static_cast<addr_type>(Addr);
92}
93
Juergen Ributzka7608dc02014-03-21 20:28:42 +000094template <class ELFT>
Michael J. Spencer1a791612013-01-15 07:44:25 +000095void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef,
96 uint64_t Addr) {
Preston Gurdcc31af92012-04-16 22:12:58 +000097
Juergen Ributzka7608dc02014-03-21 20:28:42 +000098 Elf_Sym *sym = const_cast<Elf_Sym *>(
99 ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl()));
Preston Gurdcc31af92012-04-16 22:12:58 +0000100
101 // This assumes the address passed in matches the target address bitness
102 // The template-based type cast handles everything else.
103 sym->st_value = static_cast<addr_type>(Addr);
104}
105
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000106class LoadedELFObjectInfo : public RuntimeDyld::LoadedObjectInfo {
107public:
108 LoadedELFObjectInfo(RuntimeDyldImpl &RTDyld, unsigned BeginIdx,
109 unsigned EndIdx)
110 : RuntimeDyld::LoadedObjectInfo(RTDyld, BeginIdx, EndIdx) {}
111
112 OwningBinary<ObjectFile>
113 getObjectForDebug(const ObjectFile &Obj) const override;
114};
115
116template <typename ELFT>
117std::unique_ptr<DyldELFObject<ELFT>>
118createRTDyldELFObject(MemoryBufferRef Buffer,
119 const LoadedELFObjectInfo &L,
120 std::error_code &ec) {
121 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
122 typedef typename ELFDataTypeTypedefHelper<ELFT>::value_type addr_type;
123
124 std::unique_ptr<DyldELFObject<ELFT>> Obj =
125 llvm::make_unique<DyldELFObject<ELFT>>(Buffer, ec);
126
127 // Iterate over all sections in the object.
128 for (const auto &Sec : Obj->sections()) {
129 StringRef SectionName;
130 Sec.getName(SectionName);
131 if (SectionName != "") {
132 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
133 Elf_Shdr *shdr = const_cast<Elf_Shdr *>(
134 reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
135
136 if (uint64_t SecLoadAddr = L.getSectionLoadAddress(SectionName)) {
137 // This assumes that the address passed in matches the target address
138 // bitness. The template-based type cast handles everything else.
139 shdr->sh_addr = static_cast<addr_type>(SecLoadAddr);
140 }
141 }
142 }
143
144 return Obj;
145}
146
147OwningBinary<ObjectFile> createELFDebugObject(const ObjectFile &Obj,
148 const LoadedELFObjectInfo &L) {
149 assert(Obj.isELF() && "Not an ELF object file.");
150
151 std::unique_ptr<MemoryBuffer> Buffer =
152 MemoryBuffer::getMemBufferCopy(Obj.getData(), Obj.getFileName());
153
154 std::error_code ec;
155
156 std::unique_ptr<ObjectFile> DebugObj;
157 if (Obj.getBytesInAddress() == 4 && Obj.isLittleEndian()) {
158 typedef ELFType<support::little, 2, false> ELF32LE;
159 DebugObj = createRTDyldELFObject<ELF32LE>(Buffer->getMemBufferRef(), L, ec);
160 } else if (Obj.getBytesInAddress() == 4 && !Obj.isLittleEndian()) {
161 typedef ELFType<support::big, 2, false> ELF32BE;
162 DebugObj = createRTDyldELFObject<ELF32BE>(Buffer->getMemBufferRef(), L, ec);
163 } else if (Obj.getBytesInAddress() == 8 && !Obj.isLittleEndian()) {
164 typedef ELFType<support::big, 2, true> ELF64BE;
165 DebugObj = createRTDyldELFObject<ELF64BE>(Buffer->getMemBufferRef(), L, ec);
166 } else if (Obj.getBytesInAddress() == 8 && Obj.isLittleEndian()) {
167 typedef ELFType<support::little, 2, true> ELF64LE;
168 DebugObj = createRTDyldELFObject<ELF64LE>(Buffer->getMemBufferRef(), L, ec);
169 } else
170 llvm_unreachable("Unexpected ELF format");
171
172 assert(!ec && "Could not construct copy ELF object file");
173
174 return OwningBinary<ObjectFile>(std::move(DebugObj), std::move(Buffer));
175}
176
177OwningBinary<ObjectFile>
178LoadedELFObjectInfo::getObjectForDebug(const ObjectFile &Obj) const {
179 return createELFDebugObject(Obj, *this);
180}
181
Preston Gurdcc31af92012-04-16 22:12:58 +0000182} // namespace
183
Eli Bendersky4c647582012-01-16 08:56:09 +0000184namespace llvm {
185
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000186RuntimeDyldELF::RuntimeDyldELF(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
187RuntimeDyldELF::~RuntimeDyldELF() {}
188
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000189void RuntimeDyldELF::registerEHFrames() {
190 if (!MemMgr)
191 return;
192 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
193 SID EHFrameSID = UnregisteredEHFrameSections[i];
194 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
195 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
196 size_t EHFrameSize = Sections[EHFrameSID].Size;
197 MemMgr->registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
Andrew Kaylorc442a762013-10-16 00:14:21 +0000198 RegisteredEHFrameSections.push_back(EHFrameSID);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000199 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000200 UnregisteredEHFrameSections.clear();
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000201}
202
Andrew Kaylorc442a762013-10-16 00:14:21 +0000203void RuntimeDyldELF::deregisterEHFrames() {
204 if (!MemMgr)
205 return;
206 for (int i = 0, e = RegisteredEHFrameSections.size(); i != e; ++i) {
207 SID EHFrameSID = RegisteredEHFrameSections[i];
208 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
209 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
210 size_t EHFrameSize = Sections[EHFrameSID].Size;
211 MemMgr->deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
212 }
213 RegisteredEHFrameSections.clear();
214}
215
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000216std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
217RuntimeDyldELF::loadObject(const object::ObjectFile &O) {
218 unsigned SectionStartIdx, SectionEndIdx;
219 std::tie(SectionStartIdx, SectionEndIdx) = loadObjectImpl(O);
220 return llvm::make_unique<LoadedELFObjectInfo>(*this, SectionStartIdx,
221 SectionEndIdx);
Lang Hames173c69f2014-01-08 04:09:09 +0000222}
223
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000224void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000225 uint64_t Offset, uint64_t Value,
226 uint32_t Type, int64_t Addend,
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000227 uint64_t SymOffset) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000228 switch (Type) {
229 default:
230 llvm_unreachable("Relocation type not implemented yet!");
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000231 break;
Eli Bendersky4c647582012-01-16 08:56:09 +0000232 case ELF::R_X86_64_64: {
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000233 support::ulittle64_t::ref(Section.Address + Offset) = Value + Addend;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000234 DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000235 << format("%p\n", Section.Address + Offset));
Eli Bendersky4c647582012-01-16 08:56:09 +0000236 break;
237 }
238 case ELF::R_X86_64_32:
239 case ELF::R_X86_64_32S: {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000240 Value += Addend;
Andrew Kaylor8e87a752012-07-27 20:30:12 +0000241 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000242 (Type == ELF::R_X86_64_32S &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000243 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
Eli Bendersky4c647582012-01-16 08:56:09 +0000244 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000245 support::ulittle32_t::ref(Section.Address + Offset) = TruncatedAddr;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000246 DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at "
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000247 << format("%p\n", Section.Address + Offset));
Eli Bendersky4c647582012-01-16 08:56:09 +0000248 break;
249 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000250 case ELF::R_X86_64_GOTPCREL: {
251 // findGOTEntry returns the 'G + GOT' part of the relocation calculation
252 // based on the load/target address of the GOT (not the current/local addr).
253 uint64_t GOTAddr = findGOTEntry(Value, SymOffset);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000254 uint64_t FinalAddress = Section.LoadAddress + Offset;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000255 // The processRelocationRef method combines the symbol offset and the addend
256 // and in most cases that's what we want. For this relocation type, we need
257 // the raw addend, so we subtract the symbol offset to get it.
258 int64_t RealOffset = GOTAddr + Addend - SymOffset - FinalAddress;
259 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
260 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000261 support::ulittle32_t::ref(Section.Address + Offset) = TruncOffset;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000262 break;
263 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000264 case ELF::R_X86_64_PC32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000265 // Get the placeholder value from the generated object since
266 // a previous relocation attempt may have overwritten the loaded version
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000267 support::ulittle32_t::ref Placeholder(
268 (void *)(Section.ObjAddress + Offset));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000269 uint64_t FinalAddress = Section.LoadAddress + Offset;
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000270 int64_t RealOffset = Placeholder + Value + Addend - FinalAddress;
Andrew Kaylor8e87a752012-07-27 20:30:12 +0000271 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000272 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000273 support::ulittle32_t::ref(Section.Address + Offset) = TruncOffset;
Eli Bendersky4c647582012-01-16 08:56:09 +0000274 break;
275 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000276 case ELF::R_X86_64_PC64: {
277 // Get the placeholder value from the generated object since
278 // a previous relocation attempt may have overwritten the loaded version
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000279 support::ulittle64_t::ref Placeholder(
280 (void *)(Section.ObjAddress + Offset));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000281 uint64_t FinalAddress = Section.LoadAddress + Offset;
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000282 support::ulittle64_t::ref(Section.Address + Offset) =
283 Placeholder + Value + Addend - FinalAddress;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000284 break;
285 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000286 }
287}
288
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000289void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000290 uint64_t Offset, uint32_t Value,
291 uint32_t Type, int32_t Addend) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000292 switch (Type) {
Eli Bendersky4c647582012-01-16 08:56:09 +0000293 case ELF::R_386_32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000294 // Get the placeholder value from the generated object since
295 // a previous relocation attempt may have overwritten the loaded version
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000296 support::ulittle32_t::ref Placeholder(
297 (void *)(Section.ObjAddress + Offset));
298 support::ulittle32_t::ref(Section.Address + Offset) =
299 Placeholder + Value + Addend;
Eli Bendersky4c647582012-01-16 08:56:09 +0000300 break;
301 }
302 case ELF::R_386_PC32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000303 // Get the placeholder value from the generated object since
304 // a previous relocation attempt may have overwritten the loaded version
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000305 support::ulittle32_t::ref Placeholder(
306 (void *)(Section.ObjAddress + Offset));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000307 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000308 uint32_t RealOffset = Placeholder + Value + Addend - FinalAddress;
309 support::ulittle32_t::ref(Section.Address + Offset) = RealOffset;
Eli Bendersky4c647582012-01-16 08:56:09 +0000310 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000311 }
312 default:
313 // There are other relocation types, but it appears these are the
314 // only ones currently used by the LLVM ELF object writer
315 llvm_unreachable("Relocation type not implemented yet!");
316 break;
Eli Bendersky4c647582012-01-16 08:56:09 +0000317 }
318}
319
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000320void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000321 uint64_t Offset, uint64_t Value,
322 uint32_t Type, int64_t Addend) {
323 uint32_t *TargetPtr = reinterpret_cast<uint32_t *>(Section.Address + Offset);
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000324 uint64_t FinalAddress = Section.LoadAddress + Offset;
325
326 DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
327 << format("%llx", Section.Address + Offset)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000328 << " FinalAddress: 0x" << format("%llx", FinalAddress)
329 << " Value: 0x" << format("%llx", Value) << " Type: 0x"
330 << format("%x", Type) << " Addend: 0x" << format("%llx", Addend)
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000331 << "\n");
332
333 switch (Type) {
334 default:
335 llvm_unreachable("Relocation type not implemented yet!");
336 break;
Tim Northoverb23d8db2013-05-04 20:14:14 +0000337 case ELF::R_AARCH64_ABS64: {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000338 uint64_t *TargetPtr =
339 reinterpret_cast<uint64_t *>(Section.Address + Offset);
Tim Northoverb23d8db2013-05-04 20:14:14 +0000340 *TargetPtr = Value + Addend;
341 break;
342 }
Tim Northover5959ea32013-05-19 15:39:03 +0000343 case ELF::R_AARCH64_PREL32: {
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000344 uint64_t Result = Value + Addend - FinalAddress;
Michael J. Spencer126973b2013-08-08 22:27:13 +0000345 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000346 static_cast<int64_t>(Result) <= UINT32_MAX);
347 *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU);
348 break;
349 }
Tim Northover37cde972013-05-04 20:14:09 +0000350 case ELF::R_AARCH64_CALL26: // fallthrough
351 case ELF::R_AARCH64_JUMP26: {
352 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
353 // calculation.
354 uint64_t BranchImm = Value + Addend - FinalAddress;
355
356 // "Check that -2^27 <= result < 2^27".
Michael J. Spencer126973b2013-08-08 22:27:13 +0000357 assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) &&
Tim Northover37cde972013-05-04 20:14:09 +0000358 static_cast<int64_t>(BranchImm) < (1LL << 27));
Tim Northover5959ea32013-05-19 15:39:03 +0000359
360 // AArch64 code is emitted with .rela relocations. The data already in any
361 // bits affected by the relocation on entry is garbage.
362 *TargetPtr &= 0xfc000000U;
Tim Northover37cde972013-05-04 20:14:09 +0000363 // Immediate goes in bits 25:0 of B and BL.
364 *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2;
365 break;
366 }
Tim Northover4d01c1e2013-05-04 20:14:04 +0000367 case ELF::R_AARCH64_MOVW_UABS_G3: {
368 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000369
370 // AArch64 code is emitted with .rela relocations. The data already in any
371 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000372 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000373 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
374 *TargetPtr |= Result >> (48 - 5);
Tim Northover8625fd82013-07-01 19:23:10 +0000375 // Shift must be "lsl #48", in bits 22:21
376 assert((*TargetPtr >> 21 & 0x3) == 3 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000377 break;
378 }
379 case ELF::R_AARCH64_MOVW_UABS_G2_NC: {
380 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000381
Tim Northover5959ea32013-05-19 15:39:03 +0000382 // AArch64 code is emitted with .rela relocations. The data already in any
383 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000384 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000385 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
386 *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
Tim Northover8625fd82013-07-01 19:23:10 +0000387 // Shift must be "lsl #32", in bits 22:21
388 assert((*TargetPtr >> 21 & 0x3) == 2 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000389 break;
390 }
391 case ELF::R_AARCH64_MOVW_UABS_G1_NC: {
392 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000393
394 // AArch64 code is emitted with .rela relocations. The data already in any
395 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000396 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000397 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
398 *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
Tim Northover8625fd82013-07-01 19:23:10 +0000399 // Shift must be "lsl #16", in bits 22:2
400 assert((*TargetPtr >> 21 & 0x3) == 1 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000401 break;
402 }
403 case ELF::R_AARCH64_MOVW_UABS_G0_NC: {
404 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000405
406 // AArch64 code is emitted with .rela relocations. The data already in any
407 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000408 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000409 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
410 *TargetPtr |= ((Result & 0xffffU) << 5);
Tim Northover8625fd82013-07-01 19:23:10 +0000411 // Shift must be "lsl #0", in bits 22:21.
412 assert((*TargetPtr >> 21 & 0x3) == 0 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000413 break;
414 }
Bradley Smith9d808492014-02-11 12:59:09 +0000415 case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
416 // Operation: Page(S+A) - Page(P)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000417 uint64_t Result =
418 ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
Bradley Smith9d808492014-02-11 12:59:09 +0000419
420 // Check that -2^32 <= X < 2^32
421 assert(static_cast<int64_t>(Result) >= (-1LL << 32) &&
422 static_cast<int64_t>(Result) < (1LL << 32) &&
423 "overflow check failed for relocation");
424
425 // AArch64 code is emitted with .rela relocations. The data already in any
426 // bits affected by the relocation on entry is garbage.
427 *TargetPtr &= 0x9f00001fU;
428 // Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken
429 // from bits 32:12 of X.
430 *TargetPtr |= ((Result & 0x3000U) << (29 - 12));
431 *TargetPtr |= ((Result & 0x1ffffc000ULL) >> (14 - 5));
432 break;
433 }
434 case ELF::R_AARCH64_LDST32_ABS_LO12_NC: {
435 // Operation: S + A
436 uint64_t Result = Value + Addend;
437
438 // AArch64 code is emitted with .rela relocations. The data already in any
439 // bits affected by the relocation on entry is garbage.
440 *TargetPtr &= 0xffc003ffU;
441 // Immediate goes in bits 21:10 of LD/ST instruction, taken
442 // from bits 11:2 of X
443 *TargetPtr |= ((Result & 0xffc) << (10 - 2));
444 break;
445 }
446 case ELF::R_AARCH64_LDST64_ABS_LO12_NC: {
447 // Operation: S + A
448 uint64_t Result = Value + Addend;
449
450 // AArch64 code is emitted with .rela relocations. The data already in any
451 // bits affected by the relocation on entry is garbage.
452 *TargetPtr &= 0xffc003ffU;
453 // Immediate goes in bits 21:10 of LD/ST instruction, taken
454 // from bits 11:3 of X
455 *TargetPtr |= ((Result & 0xff8) << (10 - 3));
456 break;
457 }
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000458 }
459}
460
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000461void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000462 uint64_t Offset, uint32_t Value,
463 uint32_t Type, int32_t Addend) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000464 // TODO: Add Thumb relocations.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000465 uint32_t *Placeholder =
466 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
467 uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset);
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000468 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000469 Value += Addend;
470
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000471 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
472 << Section.Address + Offset
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000473 << " FinalAddress: " << format("%p", FinalAddress) << " Value: "
474 << format("%x", Value) << " Type: " << format("%x", Type)
475 << " Addend: " << format("%x", Addend) << "\n");
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000476
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000477 switch (Type) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000478 default:
479 llvm_unreachable("Not implemented relocation type!");
480
Renato Golin8cea6e82014-01-29 11:50:56 +0000481 case ELF::R_ARM_NONE:
482 break;
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000483 // Write a 32bit value to relocation address, taking into account the
Tim Northover471cbb72012-10-03 16:29:42 +0000484 // implicit addend encoded in the target.
Renato Golin8cea6e82014-01-29 11:50:56 +0000485 case ELF::R_ARM_PREL31:
Tim Northover3b684d82013-05-28 19:48:19 +0000486 case ELF::R_ARM_TARGET1:
487 case ELF::R_ARM_ABS32:
488 *TargetPtr = *Placeholder + Value;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000489 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000490 // Write first 16 bit of 32 bit value to the mov instruction.
491 // Last 4 bit should be shifted.
Tim Northover3b684d82013-05-28 19:48:19 +0000492 case ELF::R_ARM_MOVW_ABS_NC:
Tim Northover471cbb72012-10-03 16:29:42 +0000493 // We are not expecting any other addend in the relocation address.
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000494 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
Tim Northover471cbb72012-10-03 16:29:42 +0000495 // non-contiguous fields.
Tim Northover3b684d82013-05-28 19:48:19 +0000496 assert((*Placeholder & 0x000F0FFF) == 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000497 Value = Value & 0xFFFF;
Tim Northover3b684d82013-05-28 19:48:19 +0000498 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000499 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
500 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000501 // Write last 16 bit of 32 bit value to the mov instruction.
502 // Last 4 bit should be shifted.
Tim Northover3b684d82013-05-28 19:48:19 +0000503 case ELF::R_ARM_MOVT_ABS:
Tim Northover471cbb72012-10-03 16:29:42 +0000504 // We are not expecting any other addend in the relocation address.
505 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
Tim Northover3b684d82013-05-28 19:48:19 +0000506 assert((*Placeholder & 0x000F0FFF) == 0);
507
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000508 Value = (Value >> 16) & 0xFFFF;
Tim Northover3b684d82013-05-28 19:48:19 +0000509 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000510 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
511 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000512 // Write 24 bit relative value to the branch instruction.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000513 case ELF::R_ARM_PC24: // Fall through.
514 case ELF::R_ARM_CALL: // Fall through.
Tim Northover3b684d82013-05-28 19:48:19 +0000515 case ELF::R_ARM_JUMP24: {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000516 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
517 RelValue = (RelValue & 0x03FFFFFC) >> 2;
Tim Northover3b684d82013-05-28 19:48:19 +0000518 assert((*TargetPtr & 0xFFFFFF) == 0xFFFFFE);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000519 *TargetPtr &= 0xFF000000;
520 *TargetPtr |= RelValue;
521 break;
522 }
Tim Northover3b684d82013-05-28 19:48:19 +0000523 case ELF::R_ARM_PRIVATE_0:
524 // This relocation is reserved by the ARM ELF ABI for internal use. We
525 // appropriate it here to act as an R_ARM_ABS32 without any addend for use
526 // in the stubs created during JIT (which can't put an addend into the
527 // original object file).
528 *TargetPtr = Value;
529 break;
530 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000531}
532
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000533void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000534 uint64_t Offset, uint32_t Value,
535 uint32_t Type, int32_t Addend) {
536 uint32_t *Placeholder =
537 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
538 uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000539 Value += Addend;
540
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000541 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000542 << Section.Address + Offset << " FinalAddress: "
543 << format("%p", Section.LoadAddress + Offset) << " Value: "
544 << format("%x", Value) << " Type: " << format("%x", Type)
545 << " Addend: " << format("%x", Addend) << "\n");
Akira Hatanaka111174b2012-08-17 21:28:04 +0000546
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000547 switch (Type) {
Akira Hatanaka111174b2012-08-17 21:28:04 +0000548 default:
549 llvm_unreachable("Not implemented relocation type!");
550 break;
551 case ELF::R_MIPS_32:
Akira Hatanaka2e236242013-07-24 01:58:40 +0000552 *TargetPtr = Value + (*Placeholder);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000553 break;
554 case ELF::R_MIPS_26:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000555 *TargetPtr = ((*Placeholder) & 0xfc000000) | ((Value & 0x0fffffff) >> 2);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000556 break;
557 case ELF::R_MIPS_HI16:
558 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
Akira Hatanaka2e236242013-07-24 01:58:40 +0000559 Value += ((*Placeholder) & 0x0000ffff) << 16;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000560 *TargetPtr =
561 ((*Placeholder) & 0xffff0000) | (((Value + 0x8000) >> 16) & 0xffff);
Akira Hatanaka2e236242013-07-24 01:58:40 +0000562 break;
563 case ELF::R_MIPS_LO16:
564 Value += ((*Placeholder) & 0x0000ffff);
565 *TargetPtr = ((*Placeholder) & 0xffff0000) | (Value & 0xffff);
566 break;
567 case ELF::R_MIPS_UNUSED1:
568 // Similar to ELF::R_ARM_PRIVATE_0, R_MIPS_UNUSED1 and R_MIPS_UNUSED2
569 // are used for internal JIT purpose. These relocations are similar to
570 // R_MIPS_HI16 and R_MIPS_LO16, but they do not take any addend into
571 // account.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000572 *TargetPtr =
573 ((*TargetPtr) & 0xffff0000) | (((Value + 0x8000) >> 16) & 0xffff);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000574 break;
Akira Hatanaka2e236242013-07-24 01:58:40 +0000575 case ELF::R_MIPS_UNUSED2:
Akira Hatanaka111174b2012-08-17 21:28:04 +0000576 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
577 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000578 }
Akira Hatanaka111174b2012-08-17 21:28:04 +0000579}
580
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000581// Return the .TOC. section and offset.
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000582void RuntimeDyldELF::findPPC64TOCSection(const ObjectFile &Obj,
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000583 ObjSectionToIDMap &LocalSections,
584 RelocationValueRef &Rel) {
585 // Set a default SectionID in case we do not find a TOC section below.
586 // This may happen for references to TOC base base (sym@toc, .odp
587 // relocation) without a .toc directive. In this case just use the
588 // first section (which is usually the .odp) since the code won't
589 // reference the .toc base directly.
590 Rel.SymbolName = NULL;
591 Rel.SectionID = 0;
592
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000593 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
594 // order. The TOC starts where the first of these sections starts.
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000595 for (section_iterator si = Obj.section_begin(), se = Obj.section_end();
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000596 si != se; ++si) {
597
598 StringRef SectionName;
599 check(si->getName(SectionName));
600
601 if (SectionName == ".got"
602 || SectionName == ".toc"
603 || SectionName == ".tocbss"
604 || SectionName == ".plt") {
605 Rel.SectionID = findOrEmitSection(Obj, *si, false, LocalSections);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000606 break;
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000607 }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000608 }
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000609
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000610 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
611 // thus permitting a full 64 Kbytes segment.
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000612 Rel.Addend = 0x8000;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000613}
614
615// Returns the sections and offset associated with the ODP entry referenced
616// by Symbol.
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000617void RuntimeDyldELF::findOPDEntrySection(const ObjectFile &Obj,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000618 ObjSectionToIDMap &LocalSections,
619 RelocationValueRef &Rel) {
620 // Get the ELF symbol value (st_value) to compare with Relocation offset in
621 // .opd entries
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000622 for (section_iterator si = Obj.section_begin(), se = Obj.section_end();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000623 si != se; ++si) {
Rafael Espindolaa61f1e92013-06-03 19:37:34 +0000624 section_iterator RelSecI = si->getRelocatedSection();
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000625 if (RelSecI == Obj.section_end())
Rafael Espindolaa61f1e92013-06-03 19:37:34 +0000626 continue;
627
628 StringRef RelSectionName;
629 check(RelSecI->getName(RelSectionName));
630 if (RelSectionName != ".opd")
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000631 continue;
632
Rafael Espindolab5155a52014-02-10 20:24:04 +0000633 for (relocation_iterator i = si->relocation_begin(),
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000634 e = si->relocation_end();
635 i != e;) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000636 // The R_PPC64_ADDR64 relocation indicates the first field
637 // of a .opd entry
638 uint64_t TypeFunc;
639 check(i->getType(TypeFunc));
640 if (TypeFunc != ELF::R_PPC64_ADDR64) {
Rafael Espindola5e812af2014-01-30 02:49:50 +0000641 ++i;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000642 continue;
643 }
644
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000645 uint64_t TargetSymbolOffset;
Rafael Espindola806f0062013-06-05 01:33:53 +0000646 symbol_iterator TargetSymbol = i->getSymbol();
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000647 check(i->getOffset(TargetSymbolOffset));
Rafael Espindola0d15f732013-05-09 03:39:05 +0000648 int64_t Addend;
649 check(getELFRelocationAddend(*i, Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000650
Rafael Espindola5e812af2014-01-30 02:49:50 +0000651 ++i;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000652 if (i == e)
653 break;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000654
655 // Just check if following relocation is a R_PPC64_TOC
656 uint64_t TypeTOC;
657 check(i->getType(TypeTOC));
658 if (TypeTOC != ELF::R_PPC64_TOC)
659 continue;
660
661 // Finally compares the Symbol value and the target symbol offset
662 // to check if this .opd entry refers to the symbol the relocation
663 // points to.
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000664 if (Rel.Addend != (int64_t)TargetSymbolOffset)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000665 continue;
666
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000667 section_iterator tsi(Obj.section_end());
Rafael Espindola806f0062013-06-05 01:33:53 +0000668 check(TargetSymbol->getSection(tsi));
Rafael Espindola80291272014-10-08 15:28:58 +0000669 bool IsCode = tsi->isText();
Lang Hames9b2dc932014-02-18 21:46:39 +0000670 Rel.SectionID = findOrEmitSection(Obj, (*tsi), IsCode, LocalSections);
Rafael Espindola0d15f732013-05-09 03:39:05 +0000671 Rel.Addend = (intptr_t)Addend;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000672 return;
673 }
674 }
675 llvm_unreachable("Attempting to get address of ODP entry!");
676}
677
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000678// Relocation masks following the #lo(value), #hi(value), #ha(value),
679// #higher(value), #highera(value), #highest(value), and #highesta(value)
680// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
681// document.
682
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000683static inline uint16_t applyPPClo(uint64_t value) { return value & 0xffff; }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000684
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000685static inline uint16_t applyPPChi(uint64_t value) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000686 return (value >> 16) & 0xffff;
687}
688
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000689static inline uint16_t applyPPCha (uint64_t value) {
690 return ((value + 0x8000) >> 16) & 0xffff;
691}
692
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000693static inline uint16_t applyPPChigher(uint64_t value) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000694 return (value >> 32) & 0xffff;
695}
696
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000697static inline uint16_t applyPPChighera (uint64_t value) {
698 return ((value + 0x8000) >> 32) & 0xffff;
699}
700
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000701static inline uint16_t applyPPChighest(uint64_t value) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000702 return (value >> 48) & 0xffff;
703}
704
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000705static inline uint16_t applyPPChighesta (uint64_t value) {
706 return ((value + 0x8000) >> 48) & 0xffff;
707}
708
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000709void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000710 uint64_t Offset, uint64_t Value,
711 uint32_t Type, int64_t Addend) {
712 uint8_t *LocalAddress = Section.Address + Offset;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000713 switch (Type) {
714 default:
715 llvm_unreachable("Relocation type not implemented yet!");
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000716 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000717 case ELF::R_PPC64_ADDR16:
718 writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
719 break;
720 case ELF::R_PPC64_ADDR16_DS:
721 writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
722 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000723 case ELF::R_PPC64_ADDR16_LO:
724 writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000725 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000726 case ELF::R_PPC64_ADDR16_LO_DS:
727 writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
728 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000729 case ELF::R_PPC64_ADDR16_HI:
730 writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000731 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000732 case ELF::R_PPC64_ADDR16_HA:
733 writeInt16BE(LocalAddress, applyPPCha(Value + Addend));
734 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000735 case ELF::R_PPC64_ADDR16_HIGHER:
736 writeInt16BE(LocalAddress, applyPPChigher(Value + Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000737 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000738 case ELF::R_PPC64_ADDR16_HIGHERA:
739 writeInt16BE(LocalAddress, applyPPChighera(Value + Addend));
740 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000741 case ELF::R_PPC64_ADDR16_HIGHEST:
742 writeInt16BE(LocalAddress, applyPPChighest(Value + Addend));
743 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000744 case ELF::R_PPC64_ADDR16_HIGHESTA:
745 writeInt16BE(LocalAddress, applyPPChighesta(Value + Addend));
746 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000747 case ELF::R_PPC64_ADDR14: {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000748 assert(((Value + Addend) & 3) == 0);
749 // Preserve the AA/LK bits in the branch instruction
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000750 uint8_t aalk = *(LocalAddress + 3);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000751 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
752 } break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000753 case ELF::R_PPC64_REL16_LO: {
754 uint64_t FinalAddress = (Section.LoadAddress + Offset);
755 uint64_t Delta = Value - FinalAddress + Addend;
756 writeInt16BE(LocalAddress, applyPPClo(Delta));
757 } break;
758 case ELF::R_PPC64_REL16_HI: {
759 uint64_t FinalAddress = (Section.LoadAddress + Offset);
760 uint64_t Delta = Value - FinalAddress + Addend;
761 writeInt16BE(LocalAddress, applyPPChi(Delta));
762 } break;
763 case ELF::R_PPC64_REL16_HA: {
764 uint64_t FinalAddress = (Section.LoadAddress + Offset);
765 uint64_t Delta = Value - FinalAddress + Addend;
766 writeInt16BE(LocalAddress, applyPPCha(Delta));
767 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000768 case ELF::R_PPC64_ADDR32: {
Adhemerval Zanella9b0b7812013-01-04 19:08:13 +0000769 int32_t Result = static_cast<int32_t>(Value + Addend);
770 if (SignExtend32<32>(Result) != Result)
Adhemerval Zanella1ae22482013-01-09 17:08:15 +0000771 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
Adhemerval Zanella9b0b7812013-01-04 19:08:13 +0000772 writeInt32BE(LocalAddress, Result);
773 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000774 case ELF::R_PPC64_REL24: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000775 uint64_t FinalAddress = (Section.LoadAddress + Offset);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000776 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
777 if (SignExtend32<24>(delta) != delta)
778 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
779 // Generates a 'bl <address>' instruction
780 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
781 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000782 case ELF::R_PPC64_REL32: {
Adhemerval Zanella1ae22482013-01-09 17:08:15 +0000783 uint64_t FinalAddress = (Section.LoadAddress + Offset);
784 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
785 if (SignExtend32<32>(delta) != delta)
786 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
787 writeInt32BE(LocalAddress, delta);
788 } break;
Adhemerval Zanellae8bd03d2013-05-06 17:21:23 +0000789 case ELF::R_PPC64_REL64: {
790 uint64_t FinalAddress = (Section.LoadAddress + Offset);
791 uint64_t Delta = Value - FinalAddress + Addend;
792 writeInt64BE(LocalAddress, Delta);
793 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000794 case ELF::R_PPC64_ADDR64:
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000795 writeInt64BE(LocalAddress, Value + Addend);
796 break;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000797 }
798}
799
Richard Sandifordca044082013-05-03 14:15:35 +0000800void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000801 uint64_t Offset, uint64_t Value,
802 uint32_t Type, int64_t Addend) {
Richard Sandifordca044082013-05-03 14:15:35 +0000803 uint8_t *LocalAddress = Section.Address + Offset;
804 switch (Type) {
805 default:
806 llvm_unreachable("Relocation type not implemented yet!");
807 break;
808 case ELF::R_390_PC16DBL:
809 case ELF::R_390_PLT16DBL: {
810 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
811 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
812 writeInt16BE(LocalAddress, Delta / 2);
813 break;
814 }
815 case ELF::R_390_PC32DBL:
816 case ELF::R_390_PLT32DBL: {
817 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
818 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
819 writeInt32BE(LocalAddress, Delta / 2);
820 break;
821 }
822 case ELF::R_390_PC32: {
823 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
824 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
825 writeInt32BE(LocalAddress, Delta);
826 break;
827 }
828 case ELF::R_390_64:
829 writeInt64BE(LocalAddress, Value + Addend);
830 break;
831 }
832}
833
Andrew Kaylor5f3a9982013-08-19 19:38:06 +0000834// The target location for the relocation is described by RE.SectionID and
835// RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
836// SectionEntry has three members describing its location.
837// SectionEntry::Address is the address at which the section has been loaded
838// into memory in the current (host) process. SectionEntry::LoadAddress is the
839// address that the section will have in the target process.
840// SectionEntry::ObjAddress is the address of the bits for this section in the
841// original emitted object image (also in the current address space).
842//
843// Relocations will be applied as if the section were loaded at
844// SectionEntry::LoadAddress, but they will be applied at an address based
845// on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
846// Target memory contents if they are required for value calculations.
847//
848// The Value parameter here is the load address of the symbol for the
849// relocation to be applied. For relocations which refer to symbols in the
850// current object Value will be the LoadAddress of the section in which
851// the symbol resides (RE.Addend provides additional information about the
852// symbol location). For external symbols, Value will be the address of the
853// symbol in the target address space.
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000854void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
Andrew Kaylor5f3a9982013-08-19 19:38:06 +0000855 uint64_t Value) {
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000856 const SectionEntry &Section = Sections[RE.SectionID];
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000857 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
858 RE.SymOffset);
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000859}
860
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000861void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000862 uint64_t Offset, uint64_t Value,
863 uint32_t Type, int64_t Addend,
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000864 uint64_t SymOffset) {
Eli Bendersky4c647582012-01-16 08:56:09 +0000865 switch (Arch) {
866 case Triple::x86_64:
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000867 resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
Eli Bendersky4c647582012-01-16 08:56:09 +0000868 break;
869 case Triple::x86:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000870 resolveX86Relocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000871 (uint32_t)(Addend & 0xffffffffL));
Eli Bendersky4c647582012-01-16 08:56:09 +0000872 break;
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000873 case Triple::aarch64:
Christian Pirker99974c72014-03-26 14:57:32 +0000874 case Triple::aarch64_be:
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000875 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
876 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000877 case Triple::arm: // Fall through.
Christian Pirker2a111602014-03-28 14:35:30 +0000878 case Triple::armeb:
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000879 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000880 case Triple::thumbeb:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000881 resolveARMRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000882 (uint32_t)(Addend & 0xffffffffL));
Eli Bendersky4c647582012-01-16 08:56:09 +0000883 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000884 case Triple::mips: // Fall through.
Akira Hatanaka111174b2012-08-17 21:28:04 +0000885 case Triple::mipsel:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000886 resolveMIPSRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL),
887 Type, (uint32_t)(Addend & 0xffffffffL));
Akira Hatanaka111174b2012-08-17 21:28:04 +0000888 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000889 case Triple::ppc64: // Fall through.
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000890 case Triple::ppc64le:
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000891 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000892 break;
Richard Sandifordca044082013-05-03 14:15:35 +0000893 case Triple::systemz:
894 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
895 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000896 default:
897 llvm_unreachable("Unsupported CPU type!");
Eli Bendersky4c647582012-01-16 08:56:09 +0000898 }
899}
900
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000901relocation_iterator RuntimeDyldELF::processRelocationRef(
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000902 unsigned SectionID, relocation_iterator RelI,
903 const ObjectFile &Obj,
Lang Hamesa5cd9502014-11-27 05:40:13 +0000904 ObjSectionToIDMap &ObjSectionToID,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000905 StubMap &Stubs) {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000906 uint64_t RelType;
Juergen Ributzka046709f2014-03-21 07:26:41 +0000907 Check(RelI->getType(RelType));
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000908 int64_t Addend;
Juergen Ributzka046709f2014-03-21 07:26:41 +0000909 Check(getELFRelocationAddend(*RelI, Addend));
910 symbol_iterator Symbol = RelI->getSymbol();
Eli Bendersky667b8792012-05-01 10:41:12 +0000911
912 // Obtain the symbol name which is referenced in the relocation
913 StringRef TargetName;
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000914 if (Symbol != Obj.symbol_end())
Rafael Espindola75954472013-06-05 02:55:01 +0000915 Symbol->getName(TargetName);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000916 DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend
917 << " TargetName: " << TargetName << "\n");
Eli Bendersky667b8792012-05-01 10:41:12 +0000918 RelocationValueRef Value;
919 // First search for the symbol in the local symbol table
Rafael Espindola75954472013-06-05 02:55:01 +0000920 SymbolRef::Type SymType = SymbolRef::ST_Unknown;
Lang Hamesa5cd9502014-11-27 05:40:13 +0000921
922 // Search for the symbol in the global symbol table
Lang Hames6bfd3982015-01-16 23:13:56 +0000923 RTDyldSymbolTable::const_iterator gsi = GlobalSymbolTable.end();
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000924 if (Symbol != Obj.symbol_end()) {
Lang Hamesa5cd9502014-11-27 05:40:13 +0000925 gsi = GlobalSymbolTable.find(TargetName.data());
Rafael Espindola75954472013-06-05 02:55:01 +0000926 Symbol->getType(SymType);
927 }
Lang Hamesa5cd9502014-11-27 05:40:13 +0000928 if (gsi != GlobalSymbolTable.end()) {
Lang Hames6bfd3982015-01-16 23:13:56 +0000929 const auto &SymInfo = gsi->second;
930 Value.SectionID = SymInfo.getSectionID();
931 Value.Offset = SymInfo.getOffset();
932 Value.Addend = SymInfo.getOffset() + Addend;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000933 } else {
Lang Hamesa5cd9502014-11-27 05:40:13 +0000934 switch (SymType) {
935 case SymbolRef::ST_Debug: {
936 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
937 // and can be changed by another developers. Maybe best way is add
938 // a new symbol type ST_Section to SymbolRef and use it.
939 section_iterator si(Obj.section_end());
940 Symbol->getSection(si);
941 if (si == Obj.section_end())
942 llvm_unreachable("Symbol section not found, bad object file format!");
943 DEBUG(dbgs() << "\t\tThis is section symbol\n");
944 bool isCode = si->isText();
945 Value.SectionID = findOrEmitSection(Obj, (*si), isCode, ObjSectionToID);
946 Value.Addend = Addend;
947 break;
948 }
949 case SymbolRef::ST_Data:
950 case SymbolRef::ST_Unknown: {
951 Value.SymbolName = TargetName.data();
952 Value.Addend = Addend;
Richard Mittonad6d3492013-08-16 18:54:26 +0000953
Lang Hamesa5cd9502014-11-27 05:40:13 +0000954 // Absolute relocations will have a zero symbol ID (STN_UNDEF), which
955 // will manifest here as a NULL symbol name.
956 // We can set this as a valid (but empty) symbol name, and rely
957 // on addRelocationForSymbol to handle this.
958 if (!Value.SymbolName)
959 Value.SymbolName = "";
960 break;
961 }
962 default:
963 llvm_unreachable("Unresolved symbol type!");
964 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000965 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000966 }
Lang Hamesa5cd9502014-11-27 05:40:13 +0000967
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000968 uint64_t Offset;
Juergen Ributzka046709f2014-03-21 07:26:41 +0000969 Check(RelI->getOffset(Offset));
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000970
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000971 DEBUG(dbgs() << "\t\tSectionID: " << SectionID << " Offset: " << Offset
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000972 << "\n");
Tim Northovere19bed72014-07-23 12:32:47 +0000973 if ((Arch == Triple::aarch64 || Arch == Triple::aarch64_be) &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000974 (RelType == ELF::R_AARCH64_CALL26 || RelType == ELF::R_AARCH64_JUMP26)) {
Tim Northover37cde972013-05-04 20:14:09 +0000975 // This is an AArch64 branch relocation, need to use a stub function.
976 DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
977 SectionEntry &Section = Sections[SectionID];
978
979 // Look for an existing stub.
980 StubMap::const_iterator i = Stubs.find(Value);
981 if (i != Stubs.end()) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000982 resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second,
983 RelType, 0);
Tim Northover37cde972013-05-04 20:14:09 +0000984 DEBUG(dbgs() << " Stub function found\n");
985 } else {
986 // Create a new stub function.
987 DEBUG(dbgs() << " Create a new stub function\n");
988 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000989 uint8_t *StubTargetAddr =
990 createStubFunction(Section.Address + Section.StubOffset);
Tim Northover37cde972013-05-04 20:14:09 +0000991
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000992 RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section.Address,
Lang Hames9a891052014-09-07 04:13:13 +0000993 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000994 RelocationEntry REmovk_g2(SectionID, StubTargetAddr - Section.Address + 4,
Lang Hames9a891052014-09-07 04:13:13 +0000995 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000996 RelocationEntry REmovk_g1(SectionID, StubTargetAddr - Section.Address + 8,
Lang Hames9a891052014-09-07 04:13:13 +0000997 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
Tim Northover37cde972013-05-04 20:14:09 +0000998 RelocationEntry REmovk_g0(SectionID,
999 StubTargetAddr - Section.Address + 12,
1000 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
1001
1002 if (Value.SymbolName) {
1003 addRelocationForSymbol(REmovz_g3, Value.SymbolName);
1004 addRelocationForSymbol(REmovk_g2, Value.SymbolName);
1005 addRelocationForSymbol(REmovk_g1, Value.SymbolName);
1006 addRelocationForSymbol(REmovk_g0, Value.SymbolName);
1007 } else {
1008 addRelocationForSection(REmovz_g3, Value.SectionID);
1009 addRelocationForSection(REmovk_g2, Value.SectionID);
1010 addRelocationForSection(REmovk_g1, Value.SectionID);
1011 addRelocationForSection(REmovk_g0, Value.SectionID);
1012 }
1013 resolveRelocation(Section, Offset,
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001014 (uint64_t)Section.Address + Section.StubOffset, RelType,
1015 0);
Tim Northover37cde972013-05-04 20:14:09 +00001016 Section.StubOffset += getMaxStubSize();
1017 }
1018 } else if (Arch == Triple::arm &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001019 (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL ||
1020 RelType == ELF::R_ARM_JUMP24)) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001021 // This is an ARM branch relocation, need to use a stub function.
1022 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001023 SectionEntry &Section = Sections[SectionID];
Eli Bendersky4c647582012-01-16 08:56:09 +00001024
Eric Christopherc33f6222012-10-23 17:19:15 +00001025 // Look for an existing stub.
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001026 StubMap::const_iterator i = Stubs.find(Value);
1027 if (i != Stubs.end()) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001028 resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second,
1029 RelType, 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001030 DEBUG(dbgs() << " Stub function found\n");
1031 } else {
1032 // Create a new stub function.
1033 DEBUG(dbgs() << " Create a new stub function\n");
1034 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001035 uint8_t *StubTargetAddr =
1036 createStubFunction(Section.Address + Section.StubOffset);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001037 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Tim Northover3b684d82013-05-28 19:48:19 +00001038 ELF::R_ARM_PRIVATE_0, Value.Addend);
Eli Bendersky667b8792012-05-01 10:41:12 +00001039 if (Value.SymbolName)
1040 addRelocationForSymbol(RE, Value.SymbolName);
1041 else
1042 addRelocationForSection(RE, Value.SectionID);
1043
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001044 resolveRelocation(Section, Offset,
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001045 (uint64_t)Section.Address + Section.StubOffset, RelType,
1046 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001047 Section.StubOffset += getMaxStubSize();
1048 }
Akira Hatanakaa667aad2012-12-03 23:12:19 +00001049 } else if ((Arch == Triple::mipsel || Arch == Triple::mips) &&
1050 RelType == ELF::R_MIPS_26) {
Akira Hatanaka111174b2012-08-17 21:28:04 +00001051 // This is an Mips branch relocation, need to use a stub function.
1052 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001053 SectionEntry &Section = Sections[SectionID];
1054 uint8_t *Target = Section.Address + Offset;
Akira Hatanaka111174b2012-08-17 21:28:04 +00001055 uint32_t *TargetAddress = (uint32_t *)Target;
1056
1057 // Extract the addend from the instruction.
1058 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
1059
1060 Value.Addend += Addend;
1061
1062 // Look up for existing stub.
1063 StubMap::const_iterator i = Stubs.find(Value);
1064 if (i != Stubs.end()) {
Petar Jovanovic45115f82013-11-19 21:56:00 +00001065 RelocationEntry RE(SectionID, Offset, RelType, i->second);
1066 addRelocationForSection(RE, SectionID);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001067 DEBUG(dbgs() << " Stub function found\n");
1068 } else {
1069 // Create a new stub function.
1070 DEBUG(dbgs() << " Create a new stub function\n");
1071 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001072 uint8_t *StubTargetAddr =
1073 createStubFunction(Section.Address + Section.StubOffset);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001074
1075 // Creating Hi and Lo relocations for the filled stub instructions.
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001076 RelocationEntry REHi(SectionID, StubTargetAddr - Section.Address,
Akira Hatanaka2e236242013-07-24 01:58:40 +00001077 ELF::R_MIPS_UNUSED1, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001078 RelocationEntry RELo(SectionID, StubTargetAddr - Section.Address + 4,
Akira Hatanaka2e236242013-07-24 01:58:40 +00001079 ELF::R_MIPS_UNUSED2, Value.Addend);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001080
1081 if (Value.SymbolName) {
1082 addRelocationForSymbol(REHi, Value.SymbolName);
1083 addRelocationForSymbol(RELo, Value.SymbolName);
1084 } else {
1085 addRelocationForSection(REHi, Value.SectionID);
1086 addRelocationForSection(RELo, Value.SectionID);
1087 }
1088
Petar Jovanovic45115f82013-11-19 21:56:00 +00001089 RelocationEntry RE(SectionID, Offset, RelType, Section.StubOffset);
1090 addRelocationForSection(RE, SectionID);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001091 Section.StubOffset += getMaxStubSize();
1092 }
Bill Schmidt0a9170d2013-07-26 01:35:43 +00001093 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001094 if (RelType == ELF::R_PPC64_REL24) {
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001095 // Determine ABI variant in use for this object.
1096 unsigned AbiVariant;
Lang Hamesb5c7b1f2014-11-26 16:54:40 +00001097 Obj.getPlatformFlags(AbiVariant);
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001098 AbiVariant &= ELF::EF_PPC64_ABI;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001099 // A PPC branch relocation will need a stub function if the target is
1100 // an external symbol (Symbol::ST_Unknown) or if the target address
1101 // is not within the signed 24-bits branch address.
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001102 SectionEntry &Section = Sections[SectionID];
1103 uint8_t *Target = Section.Address + Offset;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001104 bool RangeOverflow = false;
1105 if (SymType != SymbolRef::ST_Unknown) {
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001106 if (AbiVariant != 2) {
1107 // In the ELFv1 ABI, a function call may point to the .opd entry,
1108 // so the final symbol value is calculated based on the relocation
1109 // values in the .opd section.
1110 findOPDEntrySection(Obj, ObjSectionToID, Value);
1111 } else {
1112 // In the ELFv2 ABI, a function symbol may provide a local entry
1113 // point, which must be used for direct calls.
1114 uint8_t SymOther;
1115 Symbol->getOther(SymOther);
1116 Value.Addend += ELF::decodePPC64LocalEntryOffset(SymOther);
1117 }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001118 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
1119 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
1120 // If it is within 24-bits branch range, just set the branch target
1121 if (SignExtend32<24>(delta) == delta) {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001122 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001123 if (Value.SymbolName)
1124 addRelocationForSymbol(RE, Value.SymbolName);
1125 else
1126 addRelocationForSection(RE, Value.SectionID);
1127 } else {
1128 RangeOverflow = true;
1129 }
1130 }
David Blaikiedc3f01e2015-03-09 01:57:13 +00001131 if (SymType == SymbolRef::ST_Unknown || RangeOverflow) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001132 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
1133 // larger than 24-bits.
1134 StubMap::const_iterator i = Stubs.find(Value);
1135 if (i != Stubs.end()) {
1136 // Symbol function stub already created, just relocate to it
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001137 resolveRelocation(Section, Offset,
Andrew Kaylorfb05a502012-11-02 19:45:23 +00001138 (uint64_t)Section.Address + i->second, RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001139 DEBUG(dbgs() << " Stub function found\n");
1140 } else {
1141 // Create a new stub function.
1142 DEBUG(dbgs() << " Create a new stub function\n");
1143 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001144 uint8_t *StubTargetAddr =
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001145 createStubFunction(Section.Address + Section.StubOffset,
1146 AbiVariant);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001147 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001148 ELF::R_PPC64_ADDR64, Value.Addend);
1149
1150 // Generates the 64-bits address loads as exemplified in section
Ulrich Weigand32626012014-06-20 18:17:56 +00001151 // 4.5.1 in PPC64 ELF ABI. Note that the relocations need to
1152 // apply to the low part of the instructions, so we have to update
1153 // the offset according to the target endianness.
1154 uint64_t StubRelocOffset = StubTargetAddr - Section.Address;
1155 if (!IsTargetLittleEndian)
1156 StubRelocOffset += 2;
1157
1158 RelocationEntry REhst(SectionID, StubRelocOffset + 0,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001159 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
Ulrich Weigand32626012014-06-20 18:17:56 +00001160 RelocationEntry REhr(SectionID, StubRelocOffset + 4,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001161 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
Ulrich Weigand32626012014-06-20 18:17:56 +00001162 RelocationEntry REh(SectionID, StubRelocOffset + 12,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001163 ELF::R_PPC64_ADDR16_HI, Value.Addend);
Ulrich Weigand32626012014-06-20 18:17:56 +00001164 RelocationEntry REl(SectionID, StubRelocOffset + 16,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001165 ELF::R_PPC64_ADDR16_LO, Value.Addend);
1166
1167 if (Value.SymbolName) {
1168 addRelocationForSymbol(REhst, Value.SymbolName);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001169 addRelocationForSymbol(REhr, Value.SymbolName);
1170 addRelocationForSymbol(REh, Value.SymbolName);
1171 addRelocationForSymbol(REl, Value.SymbolName);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001172 } else {
1173 addRelocationForSection(REhst, Value.SectionID);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001174 addRelocationForSection(REhr, Value.SectionID);
1175 addRelocationForSection(REh, Value.SectionID);
1176 addRelocationForSection(REl, Value.SectionID);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001177 }
1178
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001179 resolveRelocation(Section, Offset,
Andrew Kaylorfb05a502012-11-02 19:45:23 +00001180 (uint64_t)Section.Address + Section.StubOffset,
1181 RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001182 Section.StubOffset += getMaxStubSize();
1183 }
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001184 if (SymType == SymbolRef::ST_Unknown) {
Ulrich Weigandfa84ac92014-03-11 15:26:27 +00001185 // Restore the TOC for external calls
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001186 if (AbiVariant == 2)
1187 writeInt32BE(Target + 4, 0xE8410018); // ld r2,28(r1)
1188 else
1189 writeInt32BE(Target + 4, 0xE8410028); // ld r2,40(r1)
1190 }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001191 }
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +00001192 } else if (RelType == ELF::R_PPC64_TOC16 ||
1193 RelType == ELF::R_PPC64_TOC16_DS ||
1194 RelType == ELF::R_PPC64_TOC16_LO ||
1195 RelType == ELF::R_PPC64_TOC16_LO_DS ||
1196 RelType == ELF::R_PPC64_TOC16_HI ||
1197 RelType == ELF::R_PPC64_TOC16_HA) {
1198 // These relocations are supposed to subtract the TOC address from
1199 // the final value. This does not fit cleanly into the RuntimeDyld
1200 // scheme, since there may be *two* sections involved in determining
1201 // the relocation value (the section of the symbol refered to by the
1202 // relocation, and the TOC section associated with the current module).
1203 //
1204 // Fortunately, these relocations are currently only ever generated
1205 // refering to symbols that themselves reside in the TOC, which means
1206 // that the two sections are actually the same. Thus they cancel out
1207 // and we can immediately resolve the relocation right now.
1208 switch (RelType) {
1209 case ELF::R_PPC64_TOC16: RelType = ELF::R_PPC64_ADDR16; break;
1210 case ELF::R_PPC64_TOC16_DS: RelType = ELF::R_PPC64_ADDR16_DS; break;
1211 case ELF::R_PPC64_TOC16_LO: RelType = ELF::R_PPC64_ADDR16_LO; break;
1212 case ELF::R_PPC64_TOC16_LO_DS: RelType = ELF::R_PPC64_ADDR16_LO_DS; break;
1213 case ELF::R_PPC64_TOC16_HI: RelType = ELF::R_PPC64_ADDR16_HI; break;
1214 case ELF::R_PPC64_TOC16_HA: RelType = ELF::R_PPC64_ADDR16_HA; break;
1215 default: llvm_unreachable("Wrong relocation type.");
1216 }
1217
1218 RelocationValueRef TOCValue;
1219 findPPC64TOCSection(Obj, ObjSectionToID, TOCValue);
1220 if (Value.SymbolName || Value.SectionID != TOCValue.SectionID)
1221 llvm_unreachable("Unsupported TOC relocation.");
1222 Value.Addend -= TOCValue.Addend;
1223 resolveRelocation(Sections[SectionID], Offset, Value.Addend, RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001224 } else {
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +00001225 // There are two ways to refer to the TOC address directly: either
1226 // via a ELF::R_PPC64_TOC relocation (where both symbol and addend are
1227 // ignored), or via any relocation that refers to the magic ".TOC."
1228 // symbols (in which case the addend is respected).
1229 if (RelType == ELF::R_PPC64_TOC) {
1230 RelType = ELF::R_PPC64_ADDR64;
1231 findPPC64TOCSection(Obj, ObjSectionToID, Value);
1232 } else if (TargetName == ".TOC.") {
1233 findPPC64TOCSection(Obj, ObjSectionToID, Value);
1234 Value.Addend += Addend;
1235 }
1236
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001237 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Richard Mittonad6d3492013-08-16 18:54:26 +00001238
1239 if (Value.SymbolName)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001240 addRelocationForSymbol(RE, Value.SymbolName);
1241 else
1242 addRelocationForSection(RE, Value.SectionID);
1243 }
Richard Sandifordca044082013-05-03 14:15:35 +00001244 } else if (Arch == Triple::systemz &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001245 (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) {
Richard Sandifordca044082013-05-03 14:15:35 +00001246 // Create function stubs for both PLT and GOT references, regardless of
1247 // whether the GOT reference is to data or code. The stub contains the
1248 // full address of the symbol, as needed by GOT references, and the
1249 // executable part only adds an overhead of 8 bytes.
1250 //
1251 // We could try to conserve space by allocating the code and data
1252 // parts of the stub separately. However, as things stand, we allocate
1253 // a stub for every relocation, so using a GOT in JIT code should be
1254 // no less space efficient than using an explicit constant pool.
1255 DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
1256 SectionEntry &Section = Sections[SectionID];
1257
1258 // Look for an existing stub.
1259 StubMap::const_iterator i = Stubs.find(Value);
1260 uintptr_t StubAddress;
1261 if (i != Stubs.end()) {
1262 StubAddress = uintptr_t(Section.Address) + i->second;
1263 DEBUG(dbgs() << " Stub function found\n");
1264 } else {
1265 // Create a new stub function.
1266 DEBUG(dbgs() << " Create a new stub function\n");
1267
1268 uintptr_t BaseAddress = uintptr_t(Section.Address);
1269 uintptr_t StubAlignment = getStubAlignment();
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001270 StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) &
1271 -StubAlignment;
Richard Sandifordca044082013-05-03 14:15:35 +00001272 unsigned StubOffset = StubAddress - BaseAddress;
1273
1274 Stubs[Value] = StubOffset;
1275 createStubFunction((uint8_t *)StubAddress);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001276 RelocationEntry RE(SectionID, StubOffset + 8, ELF::R_390_64,
Lang Hames40e200e2014-08-25 23:33:48 +00001277 Value.Offset);
Richard Sandifordca044082013-05-03 14:15:35 +00001278 if (Value.SymbolName)
1279 addRelocationForSymbol(RE, Value.SymbolName);
1280 else
1281 addRelocationForSection(RE, Value.SectionID);
1282 Section.StubOffset = StubOffset + getMaxStubSize();
1283 }
1284
1285 if (RelType == ELF::R_390_GOTENT)
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001286 resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL,
1287 Addend);
Richard Sandifordca044082013-05-03 14:15:35 +00001288 else
1289 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001290 } else if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_PLT32) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001291 // The way the PLT relocations normally work is that the linker allocates
1292 // the
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001293 // PLT and this relocation makes a PC-relative call into the PLT. The PLT
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001294 // entry will then jump to an address provided by the GOT. On first call,
1295 // the
1296 // GOT address will point back into PLT code that resolves the symbol. After
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001297 // the first call, the GOT entry points to the actual function.
1298 //
1299 // For local functions we're ignoring all of that here and just replacing
1300 // the PLT32 relocation type with PC32, which will translate the relocation
1301 // into a PC-relative call directly to the function. For external symbols we
1302 // can't be sure the function will be within 2^32 bytes of the call site, so
1303 // we need to create a stub, which calls into the GOT. This case is
1304 // equivalent to the usual PLT implementation except that we use the stub
1305 // mechanism in RuntimeDyld (which puts stubs at the end of the section)
1306 // rather than allocating a PLT section.
1307 if (Value.SymbolName) {
1308 // This is a call to an external function.
1309 // Look for an existing stub.
1310 SectionEntry &Section = Sections[SectionID];
1311 StubMap::const_iterator i = Stubs.find(Value);
1312 uintptr_t StubAddress;
1313 if (i != Stubs.end()) {
1314 StubAddress = uintptr_t(Section.Address) + i->second;
1315 DEBUG(dbgs() << " Stub function found\n");
1316 } else {
1317 // Create a new stub function (equivalent to a PLT entry).
1318 DEBUG(dbgs() << " Create a new stub function\n");
1319
1320 uintptr_t BaseAddress = uintptr_t(Section.Address);
1321 uintptr_t StubAlignment = getStubAlignment();
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001322 StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) &
1323 -StubAlignment;
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001324 unsigned StubOffset = StubAddress - BaseAddress;
1325 Stubs[Value] = StubOffset;
1326 createStubFunction((uint8_t *)StubAddress);
1327
1328 // Create a GOT entry for the external function.
1329 GOTEntries.push_back(Value);
1330
1331 // Make our stub function a relative call to the GOT entry.
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001332 RelocationEntry RE(SectionID, StubOffset + 2, ELF::R_X86_64_GOTPCREL,
1333 -4);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001334 addRelocationForSymbol(RE, Value.SymbolName);
1335
1336 // Bump our stub offset counter
1337 Section.StubOffset = StubOffset + getMaxStubSize();
1338 }
1339
1340 // Make the target call a call into the stub table.
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001341 resolveRelocation(Section, Offset, StubAddress, ELF::R_X86_64_PC32,
1342 Addend);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001343 } else {
1344 RelocationEntry RE(SectionID, Offset, ELF::R_X86_64_PC32, Value.Addend,
1345 Value.Offset);
1346 addRelocationForSection(RE, Value.SectionID);
1347 }
Eli Bendersky667b8792012-05-01 10:41:12 +00001348 } else {
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001349 if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_GOTPCREL) {
1350 GOTEntries.push_back(Value);
1351 }
1352 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset);
Eli Bendersky667b8792012-05-01 10:41:12 +00001353 if (Value.SymbolName)
1354 addRelocationForSymbol(RE, Value.SymbolName);
1355 else
1356 addRelocationForSection(RE, Value.SectionID);
1357 }
Juergen Ributzka046709f2014-03-21 07:26:41 +00001358 return ++RelI;
Jim Grosbacheff0a402012-01-16 22:26:39 +00001359}
1360
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001361void RuntimeDyldELF::updateGOTEntries(StringRef Name, uint64_t Addr) {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001362
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001363 SmallVectorImpl<std::pair<SID, GOTRelocations>>::iterator it;
1364 SmallVectorImpl<std::pair<SID, GOTRelocations>>::iterator end = GOTs.end();
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001365
1366 for (it = GOTs.begin(); it != end; ++it) {
1367 GOTRelocations &GOTEntries = it->second;
1368 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
Craig Topper353eda42014-04-24 06:44:33 +00001369 if (GOTEntries[i].SymbolName != nullptr &&
1370 GOTEntries[i].SymbolName == Name) {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001371 GOTEntries[i].Offset = Addr;
1372 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001373 }
1374 }
1375}
1376
1377size_t RuntimeDyldELF::getGOTEntrySize() {
1378 // We don't use the GOT in all of these cases, but it's essentially free
1379 // to put them all here.
1380 size_t Result = 0;
1381 switch (Arch) {
1382 case Triple::x86_64:
1383 case Triple::aarch64:
James Molloybd2ffa02014-04-30 10:15:41 +00001384 case Triple::aarch64_be:
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001385 case Triple::ppc64:
1386 case Triple::ppc64le:
1387 case Triple::systemz:
1388 Result = sizeof(uint64_t);
1389 break;
1390 case Triple::x86:
1391 case Triple::arm:
1392 case Triple::thumb:
1393 case Triple::mips:
1394 case Triple::mipsel:
1395 Result = sizeof(uint32_t);
1396 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001397 default:
1398 llvm_unreachable("Unsupported CPU type!");
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001399 }
1400 return Result;
1401}
1402
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001403uint64_t RuntimeDyldELF::findGOTEntry(uint64_t LoadAddress, uint64_t Offset) {
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001404
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001405 const size_t GOTEntrySize = getGOTEntrySize();
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001406
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001407 SmallVectorImpl<std::pair<SID, GOTRelocations>>::const_iterator it;
1408 SmallVectorImpl<std::pair<SID, GOTRelocations>>::const_iterator end =
1409 GOTs.end();
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001410
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001411 int GOTIndex = -1;
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001412 for (it = GOTs.begin(); it != end; ++it) {
1413 SID GOTSectionID = it->first;
1414 const GOTRelocations &GOTEntries = it->second;
1415
1416 // Find the matching entry in our vector.
1417 uint64_t SymbolOffset = 0;
1418 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
Craig Topper353eda42014-04-24 06:44:33 +00001419 if (!GOTEntries[i].SymbolName) {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001420 if (getSectionLoadAddress(GOTEntries[i].SectionID) == LoadAddress &&
1421 GOTEntries[i].Offset == Offset) {
1422 GOTIndex = i;
1423 SymbolOffset = GOTEntries[i].Offset;
1424 break;
1425 }
1426 } else {
1427 // GOT entries for external symbols use the addend as the address when
1428 // the external symbol has been resolved.
1429 if (GOTEntries[i].Offset == LoadAddress) {
1430 GOTIndex = i;
1431 // Don't use the Addend here. The relocation handler will use it.
1432 break;
1433 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001434 }
1435 }
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001436
1437 if (GOTIndex != -1) {
1438 if (GOTEntrySize == sizeof(uint64_t)) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001439 uint64_t *LocalGOTAddr = (uint64_t *)getSectionAddress(GOTSectionID);
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001440 // Fill in this entry with the address of the symbol being referenced.
1441 LocalGOTAddr[GOTIndex] = LoadAddress + SymbolOffset;
1442 } else {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001443 uint32_t *LocalGOTAddr = (uint32_t *)getSectionAddress(GOTSectionID);
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001444 // Fill in this entry with the address of the symbol being referenced.
1445 LocalGOTAddr[GOTIndex] = (uint32_t)(LoadAddress + SymbolOffset);
1446 }
1447
1448 // Calculate the load address of this entry
1449 return getSectionLoadAddress(GOTSectionID) + (GOTIndex * GOTEntrySize);
1450 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001451 }
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001452
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001453 assert(GOTIndex != -1 && "Unable to find requested GOT entry.");
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001454 return 0;
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001455}
1456
Lang Hamesb5c7b1f2014-11-26 16:54:40 +00001457void RuntimeDyldELF::finalizeLoad(const ObjectFile &Obj,
Lang Hames36072da2014-05-12 21:39:59 +00001458 ObjSectionToIDMap &SectionMap) {
Andrew Kaylor7bb13442013-10-11 21:25:48 +00001459 // If necessary, allocate the global offset table
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001460 if (MemMgr) {
1461 // Allocate the GOT if necessary
1462 size_t numGOTEntries = GOTEntries.size();
1463 if (numGOTEntries != 0) {
1464 // Allocate memory for the section
1465 unsigned SectionID = Sections.size();
1466 size_t TotalSize = numGOTEntries * getGOTEntrySize();
1467 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, getGOTEntrySize(),
1468 SectionID, ".got", false);
1469 if (!Addr)
1470 report_fatal_error("Unable to allocate memory for GOT!");
1471
1472 GOTs.push_back(std::make_pair(SectionID, GOTEntries));
1473 Sections.push_back(SectionEntry(".got", Addr, TotalSize, 0));
1474 // For now, initialize all GOT entries to zero. We'll fill them in as
1475 // needed when GOT-based relocations are applied.
1476 memset(Addr, 0, TotalSize);
1477 }
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001478 } else {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001479 report_fatal_error("Unable to allocate memory for GOT!");
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001480 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +00001481
1482 // Look for and record the EH frame section.
1483 ObjSectionToIDMap::iterator i, e;
1484 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
1485 const SectionRef &Section = i->first;
1486 StringRef Name;
1487 Section.getName(Name);
1488 if (Name == ".eh_frame") {
1489 UnregisteredEHFrameSections.push_back(i->second);
1490 break;
1491 }
1492 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001493}
1494
Lang Hamesb5c7b1f2014-11-26 16:54:40 +00001495bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile &Obj) const {
1496 return Obj.isELF();
Lang Hames173c69f2014-01-08 04:09:09 +00001497}
1498
Eli Bendersky4c647582012-01-16 08:56:09 +00001499} // namespace llvm