blob: 434a1adf4f0fa63fd07105e3e16874bd253791eb [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"
15#include "JITRegistrar.h"
16#include "ObjectImageCommon.h"
Eli Bendersky4c647582012-01-16 08:56:09 +000017#include "llvm/ADT/IntervalMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/StringRef.h"
Eli Bendersky4c647582012-01-16 08:56:09 +000020#include "llvm/ADT/Triple.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/ExecutionEngine/ObjectBuffer.h"
22#include "llvm/ExecutionEngine/ObjectImage.h"
Michael J. Spencer126973b2013-08-08 22:27:13 +000023#include "llvm/Object/ELFObjectFile.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Object/ObjectFile.h"
25#include "llvm/Support/ELF.h"
Lang Hames173c69f2014-01-08 04:09:09 +000026#include "llvm/Support/MemoryBuffer.h"
27
Eli Bendersky4c647582012-01-16 08:56:09 +000028using namespace llvm;
29using namespace llvm::object;
30
Chandler Carruthf58e3762014-04-22 03:04:17 +000031#define DEBUG_TYPE "dyld"
32
Preston Gurdcc31af92012-04-16 22:12:58 +000033namespace {
34
Juergen Ributzka7608dc02014-03-21 20:28:42 +000035static inline error_code check(error_code Err) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +000036 if (Err) {
37 report_fatal_error(Err.message());
38 }
39 return Err;
40}
41
Juergen Ributzka7608dc02014-03-21 20:28:42 +000042template <class ELFT> class DyldELFObject : public ELFObjectFile<ELFT> {
Rafael Espindola035b4162013-04-17 21:20:55 +000043 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Preston Gurdcc31af92012-04-16 22:12:58 +000044
Michael J. Spencer1a791612013-01-15 07:44:25 +000045 typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
46 typedef Elf_Sym_Impl<ELFT> Elf_Sym;
Juergen Ributzka7608dc02014-03-21 20:28:42 +000047 typedef Elf_Rel_Impl<ELFT, false> Elf_Rel;
48 typedef Elf_Rel_Impl<ELFT, true> Elf_Rela;
Preston Gurdcc31af92012-04-16 22:12:58 +000049
Michael J. Spencer1a791612013-01-15 07:44:25 +000050 typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
Preston Gurdcc31af92012-04-16 22:12:58 +000051
Juergen Ributzka7608dc02014-03-21 20:28:42 +000052 typedef typename ELFDataTypeTypedefHelper<ELFT>::value_type addr_type;
Preston Gurdcc31af92012-04-16 22:12:58 +000053
Preston Gurdcc31af92012-04-16 22:12:58 +000054public:
Andrew Kayloradc70562012-10-02 21:18:39 +000055 DyldELFObject(MemoryBuffer *Wrapper, error_code &ec);
Preston Gurdcc31af92012-04-16 22:12:58 +000056
57 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
58 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr);
59
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 }
Preston Gurdcc31af92012-04-16 22:12:58 +000068};
69
Juergen Ributzka7608dc02014-03-21 20:28:42 +000070template <class ELFT> class ELFObjectImage : public ObjectImageCommon {
71protected:
72 DyldELFObject<ELFT> *DyldObj;
73 bool Registered;
Preston Gurdcc31af92012-04-16 22:12:58 +000074
Juergen Ributzka7608dc02014-03-21 20:28:42 +000075public:
76 ELFObjectImage(ObjectBuffer *Input, DyldELFObject<ELFT> *Obj)
77 : ObjectImageCommon(Input, Obj), DyldObj(Obj), Registered(false) {}
Preston Gurdcc31af92012-04-16 22:12:58 +000078
Juergen Ributzka7608dc02014-03-21 20:28:42 +000079 virtual ~ELFObjectImage() {
80 if (Registered)
81 deregisterWithDebugger();
82 }
Preston Gurdcc31af92012-04-16 22:12:58 +000083
Juergen Ributzka7608dc02014-03-21 20:28:42 +000084 // Subclasses can override these methods to update the image with loaded
85 // addresses for sections and common symbols
86 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr) override {
87 DyldObj->updateSectionAddress(Sec, Addr);
88 }
Preston Gurdcc31af92012-04-16 22:12:58 +000089
Juergen Ributzka7608dc02014-03-21 20:28:42 +000090 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr) override {
91 DyldObj->updateSymbolAddress(Sym, Addr);
92 }
Preston Gurdcc31af92012-04-16 22:12:58 +000093
Juergen Ributzka7608dc02014-03-21 20:28:42 +000094 void registerWithDebugger() override {
95 JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
96 Registered = true;
97 }
98 void deregisterWithDebugger() override {
99 JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer);
100 }
Preston Gurdcc31af92012-04-16 22:12:58 +0000101};
102
Andrew Kayloradc70562012-10-02 21:18:39 +0000103// The MemoryBuffer passed into this constructor is just a wrapper around the
104// actual memory. Ultimately, the Binary parent class will take ownership of
105// this MemoryBuffer object but not the underlying memory.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000106template <class ELFT>
Michael J. Spencer1a791612013-01-15 07:44:25 +0000107DyldELFObject<ELFT>::DyldELFObject(MemoryBuffer *Wrapper, error_code &ec)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000108 : ELFObjectFile<ELFT>(Wrapper, ec) {
Preston Gurdcc31af92012-04-16 22:12:58 +0000109 this->isDyldELFObject = true;
110}
111
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000112template <class ELFT>
Michael J. Spencer1a791612013-01-15 07:44:25 +0000113void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec,
114 uint64_t Addr) {
Preston Gurdcc31af92012-04-16 22:12:58 +0000115 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000116 Elf_Shdr *shdr =
117 const_cast<Elf_Shdr *>(reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
Preston Gurdcc31af92012-04-16 22:12:58 +0000118
119 // This assumes the address passed in matches the target address bitness
120 // The template-based type cast handles everything else.
121 shdr->sh_addr = static_cast<addr_type>(Addr);
122}
123
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000124template <class ELFT>
Michael J. Spencer1a791612013-01-15 07:44:25 +0000125void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef,
126 uint64_t Addr) {
Preston Gurdcc31af92012-04-16 22:12:58 +0000127
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000128 Elf_Sym *sym = const_cast<Elf_Sym *>(
129 ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl()));
Preston Gurdcc31af92012-04-16 22:12:58 +0000130
131 // This assumes the address passed in matches the target address bitness
132 // The template-based type cast handles everything else.
133 sym->st_value = static_cast<addr_type>(Addr);
134}
135
136} // namespace
137
Eli Bendersky4c647582012-01-16 08:56:09 +0000138namespace llvm {
139
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000140void RuntimeDyldELF::registerEHFrames() {
141 if (!MemMgr)
142 return;
143 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
144 SID EHFrameSID = UnregisteredEHFrameSections[i];
145 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
146 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
147 size_t EHFrameSize = Sections[EHFrameSID].Size;
148 MemMgr->registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
Andrew Kaylorc442a762013-10-16 00:14:21 +0000149 RegisteredEHFrameSections.push_back(EHFrameSID);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000150 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000151 UnregisteredEHFrameSections.clear();
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000152}
153
Andrew Kaylorc442a762013-10-16 00:14:21 +0000154void RuntimeDyldELF::deregisterEHFrames() {
155 if (!MemMgr)
156 return;
157 for (int i = 0, e = RegisteredEHFrameSections.size(); i != e; ++i) {
158 SID EHFrameSID = RegisteredEHFrameSections[i];
159 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
160 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
161 size_t EHFrameSize = Sections[EHFrameSID].Size;
162 MemMgr->deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
163 }
164 RegisteredEHFrameSections.clear();
165}
166
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000167ObjectImage *
168RuntimeDyldELF::createObjectImageFromFile(object::ObjectFile *ObjFile) {
Lang Hames173c69f2014-01-08 04:09:09 +0000169 if (!ObjFile)
170 return NULL;
171
172 error_code ec;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000173 MemoryBuffer *Buffer =
174 MemoryBuffer::getMemBuffer(ObjFile->getData(), "", false);
Lang Hames173c69f2014-01-08 04:09:09 +0000175
176 if (ObjFile->getBytesInAddress() == 4 && ObjFile->isLittleEndian()) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000177 DyldELFObject<ELFType<support::little, 2, false>> *Obj =
178 new DyldELFObject<ELFType<support::little, 2, false>>(Buffer, ec);
179 return new ELFObjectImage<ELFType<support::little, 2, false>>(NULL, Obj);
180 } else if (ObjFile->getBytesInAddress() == 4 && !ObjFile->isLittleEndian()) {
181 DyldELFObject<ELFType<support::big, 2, false>> *Obj =
182 new DyldELFObject<ELFType<support::big, 2, false>>(Buffer, ec);
183 return new ELFObjectImage<ELFType<support::big, 2, false>>(NULL, Obj);
184 } else if (ObjFile->getBytesInAddress() == 8 && !ObjFile->isLittleEndian()) {
185 DyldELFObject<ELFType<support::big, 2, true>> *Obj =
186 new DyldELFObject<ELFType<support::big, 2, true>>(Buffer, ec);
187 return new ELFObjectImage<ELFType<support::big, 2, true>>(NULL, Obj);
188 } else if (ObjFile->getBytesInAddress() == 8 && ObjFile->isLittleEndian()) {
189 DyldELFObject<ELFType<support::little, 2, true>> *Obj =
190 new DyldELFObject<ELFType<support::little, 2, true>>(Buffer, ec);
191 return new ELFObjectImage<ELFType<support::little, 2, true>>(NULL, Obj);
192 } else
Lang Hames173c69f2014-01-08 04:09:09 +0000193 llvm_unreachable("Unexpected ELF format");
194}
195
Andrew Kayloradc70562012-10-02 21:18:39 +0000196ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) {
197 if (Buffer->getBufferSize() < ELF::EI_NIDENT)
198 llvm_unreachable("Unexpected ELF object size");
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000199 std::pair<unsigned char, unsigned char> Ident =
200 std::make_pair((uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS],
201 (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]);
Preston Gurdcc31af92012-04-16 22:12:58 +0000202 error_code ec;
203
204 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000205 DyldELFObject<ELFType<support::little, 4, false>> *Obj =
206 new DyldELFObject<ELFType<support::little, 4, false>>(
207 Buffer->getMemBuffer(), ec);
208 return new ELFObjectImage<ELFType<support::little, 4, false>>(Buffer, Obj);
209 } else if (Ident.first == ELF::ELFCLASS32 &&
210 Ident.second == ELF::ELFDATA2MSB) {
211 DyldELFObject<ELFType<support::big, 4, false>> *Obj =
212 new DyldELFObject<ELFType<support::big, 4, false>>(
213 Buffer->getMemBuffer(), ec);
214 return new ELFObjectImage<ELFType<support::big, 4, false>>(Buffer, Obj);
215 } else if (Ident.first == ELF::ELFCLASS64 &&
216 Ident.second == ELF::ELFDATA2MSB) {
217 DyldELFObject<ELFType<support::big, 8, true>> *Obj =
218 new DyldELFObject<ELFType<support::big, 8, true>>(
219 Buffer->getMemBuffer(), ec);
220 return new ELFObjectImage<ELFType<support::big, 8, true>>(Buffer, Obj);
221 } else if (Ident.first == ELF::ELFCLASS64 &&
222 Ident.second == ELF::ELFDATA2LSB) {
223 DyldELFObject<ELFType<support::little, 8, true>> *Obj =
224 new DyldELFObject<ELFType<support::little, 8, true>>(
225 Buffer->getMemBuffer(), ec);
226 return new ELFObjectImage<ELFType<support::little, 8, true>>(Buffer, Obj);
227 } else
Preston Gurdcc31af92012-04-16 22:12:58 +0000228 llvm_unreachable("Unexpected ELF format");
229}
230
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000231RuntimeDyldELF::~RuntimeDyldELF() {}
Eli Bendersky4c647582012-01-16 08:56:09 +0000232
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000233void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000234 uint64_t Offset, uint64_t Value,
235 uint32_t Type, int64_t Addend,
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000236 uint64_t SymOffset) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000237 switch (Type) {
238 default:
239 llvm_unreachable("Relocation type not implemented yet!");
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000240 break;
Eli Bendersky4c647582012-01-16 08:56:09 +0000241 case ELF::R_X86_64_64: {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000242 uint64_t *Target = reinterpret_cast<uint64_t *>(Section.Address + Offset);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000243 *Target = Value + Addend;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000244 DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
245 << format("%p\n", Target));
Eli Bendersky4c647582012-01-16 08:56:09 +0000246 break;
247 }
248 case ELF::R_X86_64_32:
249 case ELF::R_X86_64_32S: {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000250 Value += Addend;
Andrew Kaylor8e87a752012-07-27 20:30:12 +0000251 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000252 (Type == ELF::R_X86_64_32S &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000253 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
Eli Bendersky4c647582012-01-16 08:56:09 +0000254 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000255 uint32_t *Target = reinterpret_cast<uint32_t *>(Section.Address + Offset);
Eli Bendersky4c647582012-01-16 08:56:09 +0000256 *Target = TruncatedAddr;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000257 DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at "
258 << format("%p\n", Target));
Eli Bendersky4c647582012-01-16 08:56:09 +0000259 break;
260 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000261 case ELF::R_X86_64_GOTPCREL: {
262 // findGOTEntry returns the 'G + GOT' part of the relocation calculation
263 // based on the load/target address of the GOT (not the current/local addr).
264 uint64_t GOTAddr = findGOTEntry(Value, SymOffset);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000265 uint32_t *Target = reinterpret_cast<uint32_t *>(Section.Address + Offset);
266 uint64_t FinalAddress = Section.LoadAddress + Offset;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000267 // The processRelocationRef method combines the symbol offset and the addend
268 // and in most cases that's what we want. For this relocation type, we need
269 // the raw addend, so we subtract the symbol offset to get it.
270 int64_t RealOffset = GOTAddr + Addend - SymOffset - FinalAddress;
271 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
272 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
273 *Target = TruncOffset;
274 break;
275 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000276 case ELF::R_X86_64_PC32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000277 // Get the placeholder value from the generated object since
278 // a previous relocation attempt may have overwritten the loaded version
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000279 uint32_t *Placeholder =
280 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
281 uint32_t *Target = reinterpret_cast<uint32_t *>(Section.Address + Offset);
282 uint64_t FinalAddress = Section.LoadAddress + Offset;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000283 int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylor8e87a752012-07-27 20:30:12 +0000284 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000285 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000286 *Target = TruncOffset;
Eli Bendersky4c647582012-01-16 08:56:09 +0000287 break;
288 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000289 case ELF::R_X86_64_PC64: {
290 // Get the placeholder value from the generated object since
291 // a previous relocation attempt may have overwritten the loaded version
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000292 uint64_t *Placeholder =
293 reinterpret_cast<uint64_t *>(Section.ObjAddress + Offset);
294 uint64_t *Target = reinterpret_cast<uint64_t *>(Section.Address + Offset);
295 uint64_t FinalAddress = Section.LoadAddress + Offset;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000296 *Target = *Placeholder + Value + Addend - FinalAddress;
297 break;
298 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000299 }
300}
301
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000302void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000303 uint64_t Offset, uint32_t Value,
304 uint32_t Type, int32_t Addend) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000305 switch (Type) {
Eli Bendersky4c647582012-01-16 08:56:09 +0000306 case ELF::R_386_32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000307 // Get the placeholder value from the generated object since
308 // a previous relocation attempt may have overwritten the loaded version
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000309 uint32_t *Placeholder =
310 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
311 uint32_t *Target = reinterpret_cast<uint32_t *>(Section.Address + Offset);
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000312 *Target = *Placeholder + Value + Addend;
Eli Bendersky4c647582012-01-16 08:56:09 +0000313 break;
314 }
315 case ELF::R_386_PC32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000316 // Get the placeholder value from the generated object since
317 // a previous relocation attempt may have overwritten the loaded version
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000318 uint32_t *Placeholder =
319 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
320 uint32_t *Target = reinterpret_cast<uint32_t *>(Section.Address + Offset);
321 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000322 uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000323 *Target = RealOffset;
Eli Bendersky4c647582012-01-16 08:56:09 +0000324 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000325 }
326 default:
327 // There are other relocation types, but it appears these are the
328 // only ones currently used by the LLVM ELF object writer
329 llvm_unreachable("Relocation type not implemented yet!");
330 break;
Eli Bendersky4c647582012-01-16 08:56:09 +0000331 }
332}
333
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000334void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000335 uint64_t Offset, uint64_t Value,
336 uint32_t Type, int64_t Addend) {
337 uint32_t *TargetPtr = reinterpret_cast<uint32_t *>(Section.Address + Offset);
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000338 uint64_t FinalAddress = Section.LoadAddress + Offset;
339
340 DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
341 << format("%llx", Section.Address + Offset)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000342 << " FinalAddress: 0x" << format("%llx", FinalAddress)
343 << " Value: 0x" << format("%llx", Value) << " Type: 0x"
344 << format("%x", Type) << " Addend: 0x" << format("%llx", Addend)
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000345 << "\n");
346
347 switch (Type) {
348 default:
349 llvm_unreachable("Relocation type not implemented yet!");
350 break;
Tim Northoverb23d8db2013-05-04 20:14:14 +0000351 case ELF::R_AARCH64_ABS64: {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000352 uint64_t *TargetPtr =
353 reinterpret_cast<uint64_t *>(Section.Address + Offset);
Tim Northoverb23d8db2013-05-04 20:14:14 +0000354 *TargetPtr = Value + Addend;
355 break;
356 }
Tim Northover5959ea32013-05-19 15:39:03 +0000357 case ELF::R_AARCH64_PREL32: {
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000358 uint64_t Result = Value + Addend - FinalAddress;
Michael J. Spencer126973b2013-08-08 22:27:13 +0000359 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000360 static_cast<int64_t>(Result) <= UINT32_MAX);
361 *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU);
362 break;
363 }
Tim Northover37cde972013-05-04 20:14:09 +0000364 case ELF::R_AARCH64_CALL26: // fallthrough
365 case ELF::R_AARCH64_JUMP26: {
366 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
367 // calculation.
368 uint64_t BranchImm = Value + Addend - FinalAddress;
369
370 // "Check that -2^27 <= result < 2^27".
Michael J. Spencer126973b2013-08-08 22:27:13 +0000371 assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) &&
Tim Northover37cde972013-05-04 20:14:09 +0000372 static_cast<int64_t>(BranchImm) < (1LL << 27));
Tim Northover5959ea32013-05-19 15:39:03 +0000373
374 // AArch64 code is emitted with .rela relocations. The data already in any
375 // bits affected by the relocation on entry is garbage.
376 *TargetPtr &= 0xfc000000U;
Tim Northover37cde972013-05-04 20:14:09 +0000377 // Immediate goes in bits 25:0 of B and BL.
378 *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2;
379 break;
380 }
Tim Northover4d01c1e2013-05-04 20:14:04 +0000381 case ELF::R_AARCH64_MOVW_UABS_G3: {
382 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000383
384 // AArch64 code is emitted with .rela relocations. The data already in any
385 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000386 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000387 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
388 *TargetPtr |= Result >> (48 - 5);
Tim Northover8625fd82013-07-01 19:23:10 +0000389 // Shift must be "lsl #48", in bits 22:21
390 assert((*TargetPtr >> 21 & 0x3) == 3 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000391 break;
392 }
393 case ELF::R_AARCH64_MOVW_UABS_G2_NC: {
394 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000395
Tim Northover5959ea32013-05-19 15:39:03 +0000396 // AArch64 code is emitted with .rela relocations. The data already in any
397 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000398 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000399 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
400 *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
Tim Northover8625fd82013-07-01 19:23:10 +0000401 // Shift must be "lsl #32", in bits 22:21
402 assert((*TargetPtr >> 21 & 0x3) == 2 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000403 break;
404 }
405 case ELF::R_AARCH64_MOVW_UABS_G1_NC: {
406 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000407
408 // AArch64 code is emitted with .rela relocations. The data already in any
409 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000410 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000411 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
412 *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
Tim Northover8625fd82013-07-01 19:23:10 +0000413 // Shift must be "lsl #16", in bits 22:2
414 assert((*TargetPtr >> 21 & 0x3) == 1 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000415 break;
416 }
417 case ELF::R_AARCH64_MOVW_UABS_G0_NC: {
418 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000419
420 // AArch64 code is emitted with .rela relocations. The data already in any
421 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000422 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000423 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
424 *TargetPtr |= ((Result & 0xffffU) << 5);
Tim Northover8625fd82013-07-01 19:23:10 +0000425 // Shift must be "lsl #0", in bits 22:21.
426 assert((*TargetPtr >> 21 & 0x3) == 0 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000427 break;
428 }
Bradley Smith9d808492014-02-11 12:59:09 +0000429 case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
430 // Operation: Page(S+A) - Page(P)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000431 uint64_t Result =
432 ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
Bradley Smith9d808492014-02-11 12:59:09 +0000433
434 // Check that -2^32 <= X < 2^32
435 assert(static_cast<int64_t>(Result) >= (-1LL << 32) &&
436 static_cast<int64_t>(Result) < (1LL << 32) &&
437 "overflow check failed for relocation");
438
439 // AArch64 code is emitted with .rela relocations. The data already in any
440 // bits affected by the relocation on entry is garbage.
441 *TargetPtr &= 0x9f00001fU;
442 // Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken
443 // from bits 32:12 of X.
444 *TargetPtr |= ((Result & 0x3000U) << (29 - 12));
445 *TargetPtr |= ((Result & 0x1ffffc000ULL) >> (14 - 5));
446 break;
447 }
448 case ELF::R_AARCH64_LDST32_ABS_LO12_NC: {
449 // Operation: S + A
450 uint64_t Result = Value + Addend;
451
452 // AArch64 code is emitted with .rela relocations. The data already in any
453 // bits affected by the relocation on entry is garbage.
454 *TargetPtr &= 0xffc003ffU;
455 // Immediate goes in bits 21:10 of LD/ST instruction, taken
456 // from bits 11:2 of X
457 *TargetPtr |= ((Result & 0xffc) << (10 - 2));
458 break;
459 }
460 case ELF::R_AARCH64_LDST64_ABS_LO12_NC: {
461 // Operation: S + A
462 uint64_t Result = Value + Addend;
463
464 // AArch64 code is emitted with .rela relocations. The data already in any
465 // bits affected by the relocation on entry is garbage.
466 *TargetPtr &= 0xffc003ffU;
467 // Immediate goes in bits 21:10 of LD/ST instruction, taken
468 // from bits 11:3 of X
469 *TargetPtr |= ((Result & 0xff8) << (10 - 3));
470 break;
471 }
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000472 }
473}
474
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000475void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000476 uint64_t Offset, uint32_t Value,
477 uint32_t Type, int32_t Addend) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000478 // TODO: Add Thumb relocations.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000479 uint32_t *Placeholder =
480 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
481 uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset);
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000482 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000483 Value += Addend;
484
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000485 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
486 << Section.Address + Offset
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000487 << " FinalAddress: " << format("%p", FinalAddress) << " Value: "
488 << format("%x", Value) << " Type: " << format("%x", Type)
489 << " Addend: " << format("%x", Addend) << "\n");
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000490
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000491 switch (Type) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000492 default:
493 llvm_unreachable("Not implemented relocation type!");
494
Renato Golin8cea6e82014-01-29 11:50:56 +0000495 case ELF::R_ARM_NONE:
496 break;
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000497 // Write a 32bit value to relocation address, taking into account the
Tim Northover471cbb72012-10-03 16:29:42 +0000498 // implicit addend encoded in the target.
Renato Golin8cea6e82014-01-29 11:50:56 +0000499 case ELF::R_ARM_PREL31:
Tim Northover3b684d82013-05-28 19:48:19 +0000500 case ELF::R_ARM_TARGET1:
501 case ELF::R_ARM_ABS32:
502 *TargetPtr = *Placeholder + Value;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000503 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000504 // Write first 16 bit of 32 bit value to the mov instruction.
505 // Last 4 bit should be shifted.
Tim Northover3b684d82013-05-28 19:48:19 +0000506 case ELF::R_ARM_MOVW_ABS_NC:
Tim Northover471cbb72012-10-03 16:29:42 +0000507 // We are not expecting any other addend in the relocation address.
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000508 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
Tim Northover471cbb72012-10-03 16:29:42 +0000509 // non-contiguous fields.
Tim Northover3b684d82013-05-28 19:48:19 +0000510 assert((*Placeholder & 0x000F0FFF) == 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000511 Value = Value & 0xFFFF;
Tim Northover3b684d82013-05-28 19:48:19 +0000512 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000513 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
514 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000515 // Write last 16 bit of 32 bit value to the mov instruction.
516 // Last 4 bit should be shifted.
Tim Northover3b684d82013-05-28 19:48:19 +0000517 case ELF::R_ARM_MOVT_ABS:
Tim Northover471cbb72012-10-03 16:29:42 +0000518 // We are not expecting any other addend in the relocation address.
519 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
Tim Northover3b684d82013-05-28 19:48:19 +0000520 assert((*Placeholder & 0x000F0FFF) == 0);
521
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000522 Value = (Value >> 16) & 0xFFFF;
Tim Northover3b684d82013-05-28 19:48:19 +0000523 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000524 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
525 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000526 // Write 24 bit relative value to the branch instruction.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000527 case ELF::R_ARM_PC24: // Fall through.
528 case ELF::R_ARM_CALL: // Fall through.
Tim Northover3b684d82013-05-28 19:48:19 +0000529 case ELF::R_ARM_JUMP24: {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000530 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
531 RelValue = (RelValue & 0x03FFFFFC) >> 2;
Tim Northover3b684d82013-05-28 19:48:19 +0000532 assert((*TargetPtr & 0xFFFFFF) == 0xFFFFFE);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000533 *TargetPtr &= 0xFF000000;
534 *TargetPtr |= RelValue;
535 break;
536 }
Tim Northover3b684d82013-05-28 19:48:19 +0000537 case ELF::R_ARM_PRIVATE_0:
538 // This relocation is reserved by the ARM ELF ABI for internal use. We
539 // appropriate it here to act as an R_ARM_ABS32 without any addend for use
540 // in the stubs created during JIT (which can't put an addend into the
541 // original object file).
542 *TargetPtr = Value;
543 break;
544 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000545}
546
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000547void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000548 uint64_t Offset, uint32_t Value,
549 uint32_t Type, int32_t Addend) {
550 uint32_t *Placeholder =
551 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
552 uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000553 Value += Addend;
554
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000555 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000556 << Section.Address + Offset << " FinalAddress: "
557 << format("%p", Section.LoadAddress + Offset) << " Value: "
558 << format("%x", Value) << " Type: " << format("%x", Type)
559 << " Addend: " << format("%x", Addend) << "\n");
Akira Hatanaka111174b2012-08-17 21:28:04 +0000560
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000561 switch (Type) {
Akira Hatanaka111174b2012-08-17 21:28:04 +0000562 default:
563 llvm_unreachable("Not implemented relocation type!");
564 break;
565 case ELF::R_MIPS_32:
Akira Hatanaka2e236242013-07-24 01:58:40 +0000566 *TargetPtr = Value + (*Placeholder);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000567 break;
568 case ELF::R_MIPS_26:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000569 *TargetPtr = ((*Placeholder) & 0xfc000000) | ((Value & 0x0fffffff) >> 2);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000570 break;
571 case ELF::R_MIPS_HI16:
572 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
Akira Hatanaka2e236242013-07-24 01:58:40 +0000573 Value += ((*Placeholder) & 0x0000ffff) << 16;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000574 *TargetPtr =
575 ((*Placeholder) & 0xffff0000) | (((Value + 0x8000) >> 16) & 0xffff);
Akira Hatanaka2e236242013-07-24 01:58:40 +0000576 break;
577 case ELF::R_MIPS_LO16:
578 Value += ((*Placeholder) & 0x0000ffff);
579 *TargetPtr = ((*Placeholder) & 0xffff0000) | (Value & 0xffff);
580 break;
581 case ELF::R_MIPS_UNUSED1:
582 // Similar to ELF::R_ARM_PRIVATE_0, R_MIPS_UNUSED1 and R_MIPS_UNUSED2
583 // are used for internal JIT purpose. These relocations are similar to
584 // R_MIPS_HI16 and R_MIPS_LO16, but they do not take any addend into
585 // account.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000586 *TargetPtr =
587 ((*TargetPtr) & 0xffff0000) | (((Value + 0x8000) >> 16) & 0xffff);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000588 break;
Akira Hatanaka2e236242013-07-24 01:58:40 +0000589 case ELF::R_MIPS_UNUSED2:
Akira Hatanaka111174b2012-08-17 21:28:04 +0000590 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
591 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000592 }
Akira Hatanaka111174b2012-08-17 21:28:04 +0000593}
594
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000595// Return the .TOC. section address to R_PPC64_TOC relocations.
596uint64_t RuntimeDyldELF::findPPC64TOC() const {
597 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
598 // order. The TOC starts where the first of these sections starts.
599 SectionList::const_iterator it = Sections.begin();
600 SectionList::const_iterator ite = Sections.end();
601 for (; it != ite; ++it) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000602 if (it->Name == ".got" || it->Name == ".toc" || it->Name == ".tocbss" ||
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000603 it->Name == ".plt")
604 break;
605 }
606 if (it == ite) {
607 // This may happen for
608 // * references to TOC base base (sym@toc, .odp relocation) without
609 // a .toc directive.
610 // In this case just use the first section (which is usually
611 // the .odp) since the code won't reference the .toc base
612 // directly.
613 it = Sections.begin();
614 }
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000615 assert(it != ite);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000616 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
617 // thus permitting a full 64 Kbytes segment.
618 return it->LoadAddress + 0x8000;
619}
620
621// Returns the sections and offset associated with the ODP entry referenced
622// by Symbol.
623void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
624 ObjSectionToIDMap &LocalSections,
625 RelocationValueRef &Rel) {
626 // Get the ELF symbol value (st_value) to compare with Relocation offset in
627 // .opd entries
Rafael Espindola5e812af2014-01-30 02:49:50 +0000628 for (section_iterator si = Obj.begin_sections(), se = Obj.end_sections();
629 si != se; ++si) {
Rafael Espindolaa61f1e92013-06-03 19:37:34 +0000630 section_iterator RelSecI = si->getRelocatedSection();
631 if (RelSecI == Obj.end_sections())
632 continue;
633
634 StringRef RelSectionName;
635 check(RelSecI->getName(RelSectionName));
636 if (RelSectionName != ".opd")
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000637 continue;
638
Rafael Espindolab5155a52014-02-10 20:24:04 +0000639 for (relocation_iterator i = si->relocation_begin(),
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000640 e = si->relocation_end();
641 i != e;) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000642 // The R_PPC64_ADDR64 relocation indicates the first field
643 // of a .opd entry
644 uint64_t TypeFunc;
645 check(i->getType(TypeFunc));
646 if (TypeFunc != ELF::R_PPC64_ADDR64) {
Rafael Espindola5e812af2014-01-30 02:49:50 +0000647 ++i;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000648 continue;
649 }
650
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000651 uint64_t TargetSymbolOffset;
Rafael Espindola806f0062013-06-05 01:33:53 +0000652 symbol_iterator TargetSymbol = i->getSymbol();
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000653 check(i->getOffset(TargetSymbolOffset));
Rafael Espindola0d15f732013-05-09 03:39:05 +0000654 int64_t Addend;
655 check(getELFRelocationAddend(*i, Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000656
Rafael Espindola5e812af2014-01-30 02:49:50 +0000657 ++i;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000658 if (i == e)
659 break;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000660
661 // Just check if following relocation is a R_PPC64_TOC
662 uint64_t TypeTOC;
663 check(i->getType(TypeTOC));
664 if (TypeTOC != ELF::R_PPC64_TOC)
665 continue;
666
667 // Finally compares the Symbol value and the target symbol offset
668 // to check if this .opd entry refers to the symbol the relocation
669 // points to.
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000670 if (Rel.Addend != (int64_t)TargetSymbolOffset)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000671 continue;
672
673 section_iterator tsi(Obj.end_sections());
Rafael Espindola806f0062013-06-05 01:33:53 +0000674 check(TargetSymbol->getSection(tsi));
Lang Hames9b2dc932014-02-18 21:46:39 +0000675 bool IsCode = false;
676 tsi->isText(IsCode);
677 Rel.SectionID = findOrEmitSection(Obj, (*tsi), IsCode, LocalSections);
Rafael Espindola0d15f732013-05-09 03:39:05 +0000678 Rel.Addend = (intptr_t)Addend;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000679 return;
680 }
681 }
682 llvm_unreachable("Attempting to get address of ODP entry!");
683}
684
685// Relocation masks following the #lo(value), #hi(value), #higher(value),
686// and #highest(value) macros defined in section 4.5.1. Relocation Types
687// in PPC-elf64abi document.
688//
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000689static inline uint16_t applyPPClo(uint64_t value) { return value & 0xffff; }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000690
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000691static inline uint16_t applyPPChi(uint64_t value) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000692 return (value >> 16) & 0xffff;
693}
694
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000695static inline uint16_t applyPPChigher(uint64_t value) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000696 return (value >> 32) & 0xffff;
697}
698
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000699static inline uint16_t applyPPChighest(uint64_t value) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000700 return (value >> 48) & 0xffff;
701}
702
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000703void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000704 uint64_t Offset, uint64_t Value,
705 uint32_t Type, int64_t Addend) {
706 uint8_t *LocalAddress = Section.Address + Offset;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000707 switch (Type) {
708 default:
709 llvm_unreachable("Relocation type not implemented yet!");
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000710 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000711 case ELF::R_PPC64_ADDR16_LO:
712 writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000713 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000714 case ELF::R_PPC64_ADDR16_HI:
715 writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000716 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000717 case ELF::R_PPC64_ADDR16_HIGHER:
718 writeInt16BE(LocalAddress, applyPPChigher(Value + Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000719 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000720 case ELF::R_PPC64_ADDR16_HIGHEST:
721 writeInt16BE(LocalAddress, applyPPChighest(Value + Addend));
722 break;
723 case ELF::R_PPC64_ADDR14: {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000724 assert(((Value + Addend) & 3) == 0);
725 // Preserve the AA/LK bits in the branch instruction
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000726 uint8_t aalk = *(LocalAddress + 3);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000727 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
728 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000729 case ELF::R_PPC64_ADDR32: {
Adhemerval Zanella9b0b7812013-01-04 19:08:13 +0000730 int32_t Result = static_cast<int32_t>(Value + Addend);
731 if (SignExtend32<32>(Result) != Result)
Adhemerval Zanella1ae22482013-01-09 17:08:15 +0000732 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
Adhemerval Zanella9b0b7812013-01-04 19:08:13 +0000733 writeInt32BE(LocalAddress, Result);
734 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000735 case ELF::R_PPC64_REL24: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000736 uint64_t FinalAddress = (Section.LoadAddress + Offset);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000737 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
738 if (SignExtend32<24>(delta) != delta)
739 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
740 // Generates a 'bl <address>' instruction
741 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
742 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000743 case ELF::R_PPC64_REL32: {
Adhemerval Zanella1ae22482013-01-09 17:08:15 +0000744 uint64_t FinalAddress = (Section.LoadAddress + Offset);
745 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
746 if (SignExtend32<32>(delta) != delta)
747 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
748 writeInt32BE(LocalAddress, delta);
749 } break;
Adhemerval Zanellae8bd03d2013-05-06 17:21:23 +0000750 case ELF::R_PPC64_REL64: {
751 uint64_t FinalAddress = (Section.LoadAddress + Offset);
752 uint64_t Delta = Value - FinalAddress + Addend;
753 writeInt64BE(LocalAddress, Delta);
754 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000755 case ELF::R_PPC64_ADDR64:
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000756 writeInt64BE(LocalAddress, Value + Addend);
757 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000758 case ELF::R_PPC64_TOC:
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000759 writeInt64BE(LocalAddress, findPPC64TOC());
760 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000761 case ELF::R_PPC64_TOC16: {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000762 uint64_t TOCStart = findPPC64TOC();
763 Value = applyPPClo((Value + Addend) - TOCStart);
764 writeInt16BE(LocalAddress, applyPPClo(Value));
765 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000766 case ELF::R_PPC64_TOC16_DS: {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000767 uint64_t TOCStart = findPPC64TOC();
768 Value = ((Value + Addend) - TOCStart);
769 writeInt16BE(LocalAddress, applyPPClo(Value));
770 } break;
771 }
772}
773
Richard Sandifordca044082013-05-03 14:15:35 +0000774void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000775 uint64_t Offset, uint64_t Value,
776 uint32_t Type, int64_t Addend) {
Richard Sandifordca044082013-05-03 14:15:35 +0000777 uint8_t *LocalAddress = Section.Address + Offset;
778 switch (Type) {
779 default:
780 llvm_unreachable("Relocation type not implemented yet!");
781 break;
782 case ELF::R_390_PC16DBL:
783 case ELF::R_390_PLT16DBL: {
784 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
785 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
786 writeInt16BE(LocalAddress, Delta / 2);
787 break;
788 }
789 case ELF::R_390_PC32DBL:
790 case ELF::R_390_PLT32DBL: {
791 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
792 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
793 writeInt32BE(LocalAddress, Delta / 2);
794 break;
795 }
796 case ELF::R_390_PC32: {
797 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
798 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
799 writeInt32BE(LocalAddress, Delta);
800 break;
801 }
802 case ELF::R_390_64:
803 writeInt64BE(LocalAddress, Value + Addend);
804 break;
805 }
806}
807
Andrew Kaylor5f3a9982013-08-19 19:38:06 +0000808// The target location for the relocation is described by RE.SectionID and
809// RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
810// SectionEntry has three members describing its location.
811// SectionEntry::Address is the address at which the section has been loaded
812// into memory in the current (host) process. SectionEntry::LoadAddress is the
813// address that the section will have in the target process.
814// SectionEntry::ObjAddress is the address of the bits for this section in the
815// original emitted object image (also in the current address space).
816//
817// Relocations will be applied as if the section were loaded at
818// SectionEntry::LoadAddress, but they will be applied at an address based
819// on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
820// Target memory contents if they are required for value calculations.
821//
822// The Value parameter here is the load address of the symbol for the
823// relocation to be applied. For relocations which refer to symbols in the
824// current object Value will be the LoadAddress of the section in which
825// the symbol resides (RE.Addend provides additional information about the
826// symbol location). For external symbols, Value will be the address of the
827// symbol in the target address space.
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000828void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
Andrew Kaylor5f3a9982013-08-19 19:38:06 +0000829 uint64_t Value) {
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000830 const SectionEntry &Section = Sections[RE.SectionID];
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000831 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
832 RE.SymOffset);
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000833}
834
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000835void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000836 uint64_t Offset, uint64_t Value,
837 uint32_t Type, int64_t Addend,
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000838 uint64_t SymOffset) {
Eli Bendersky4c647582012-01-16 08:56:09 +0000839 switch (Arch) {
840 case Triple::x86_64:
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000841 resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
Eli Bendersky4c647582012-01-16 08:56:09 +0000842 break;
843 case Triple::x86:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000844 resolveX86Relocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000845 (uint32_t)(Addend & 0xffffffffL));
Eli Bendersky4c647582012-01-16 08:56:09 +0000846 break;
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000847 case Triple::aarch64:
Christian Pirker99974c72014-03-26 14:57:32 +0000848 case Triple::aarch64_be:
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000849 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
850 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000851 case Triple::arm: // Fall through.
Christian Pirker2a111602014-03-28 14:35:30 +0000852 case Triple::armeb:
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000853 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000854 case Triple::thumbeb:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000855 resolveARMRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000856 (uint32_t)(Addend & 0xffffffffL));
Eli Bendersky4c647582012-01-16 08:56:09 +0000857 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000858 case Triple::mips: // Fall through.
Akira Hatanaka111174b2012-08-17 21:28:04 +0000859 case Triple::mipsel:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000860 resolveMIPSRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL),
861 Type, (uint32_t)(Addend & 0xffffffffL));
Akira Hatanaka111174b2012-08-17 21:28:04 +0000862 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000863 case Triple::ppc64: // Fall through.
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000864 case Triple::ppc64le:
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000865 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000866 break;
Richard Sandifordca044082013-05-03 14:15:35 +0000867 case Triple::systemz:
868 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
869 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000870 default:
871 llvm_unreachable("Unsupported CPU type!");
Eli Bendersky4c647582012-01-16 08:56:09 +0000872 }
873}
874
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000875relocation_iterator RuntimeDyldELF::processRelocationRef(
876 unsigned SectionID, relocation_iterator RelI, ObjectImage &Obj,
877 ObjSectionToIDMap &ObjSectionToID, const SymbolTableMap &Symbols,
878 StubMap &Stubs) {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000879 uint64_t RelType;
Juergen Ributzka046709f2014-03-21 07:26:41 +0000880 Check(RelI->getType(RelType));
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000881 int64_t Addend;
Juergen Ributzka046709f2014-03-21 07:26:41 +0000882 Check(getELFRelocationAddend(*RelI, Addend));
883 symbol_iterator Symbol = RelI->getSymbol();
Eli Bendersky667b8792012-05-01 10:41:12 +0000884
885 // Obtain the symbol name which is referenced in the relocation
886 StringRef TargetName;
Rafael Espindola75954472013-06-05 02:55:01 +0000887 if (Symbol != Obj.end_symbols())
888 Symbol->getName(TargetName);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000889 DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend
890 << " TargetName: " << TargetName << "\n");
Eli Bendersky667b8792012-05-01 10:41:12 +0000891 RelocationValueRef Value;
892 // First search for the symbol in the local symbol table
Rafael Espindola75954472013-06-05 02:55:01 +0000893 SymbolTableMap::const_iterator lsi = Symbols.end();
894 SymbolRef::Type SymType = SymbolRef::ST_Unknown;
895 if (Symbol != Obj.end_symbols()) {
896 lsi = Symbols.find(TargetName.data());
897 Symbol->getType(SymType);
898 }
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000899 if (lsi != Symbols.end()) {
900 Value.SectionID = lsi->second.first;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000901 Value.Offset = lsi->second.second;
Ulrich Weigand78e97652013-04-05 13:29:04 +0000902 Value.Addend = lsi->second.second + Addend;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000903 } else {
Eli Bendersky667b8792012-05-01 10:41:12 +0000904 // Search for the symbol in the global symbol table
Rafael Espindola75954472013-06-05 02:55:01 +0000905 SymbolTableMap::const_iterator gsi = GlobalSymbolTable.end();
906 if (Symbol != Obj.end_symbols())
907 gsi = GlobalSymbolTable.find(TargetName.data());
Eli Benderskyfc079082012-05-01 06:58:59 +0000908 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000909 Value.SectionID = gsi->second.first;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000910 Value.Offset = gsi->second.second;
Ulrich Weigand78e97652013-04-05 13:29:04 +0000911 Value.Addend = gsi->second.second + Addend;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000912 } else {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000913 switch (SymType) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000914 case SymbolRef::ST_Debug: {
915 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
916 // and can be changed by another developers. Maybe best way is add
917 // a new symbol type ST_Section to SymbolRef and use it.
918 section_iterator si(Obj.end_sections());
919 Symbol->getSection(si);
920 if (si == Obj.end_sections())
921 llvm_unreachable("Symbol section not found, bad object file format!");
922 DEBUG(dbgs() << "\t\tThis is section symbol\n");
923 // Default to 'true' in case isText fails (though it never does).
924 bool isCode = true;
925 si->isText(isCode);
926 Value.SectionID = findOrEmitSection(Obj, (*si), isCode, ObjSectionToID);
927 Value.Addend = Addend;
928 break;
929 }
930 case SymbolRef::ST_Data:
931 case SymbolRef::ST_Unknown: {
932 Value.SymbolName = TargetName.data();
933 Value.Addend = Addend;
Richard Mittonad6d3492013-08-16 18:54:26 +0000934
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000935 // Absolute relocations will have a zero symbol ID (STN_UNDEF), which
936 // will manifest here as a NULL symbol name.
937 // We can set this as a valid (but empty) symbol name, and rely
938 // on addRelocationForSymbol to handle this.
939 if (!Value.SymbolName)
940 Value.SymbolName = "";
941 break;
942 }
943 default:
944 llvm_unreachable("Unresolved symbol type!");
945 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000946 }
947 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000948 }
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000949 uint64_t Offset;
Juergen Ributzka046709f2014-03-21 07:26:41 +0000950 Check(RelI->getOffset(Offset));
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000951
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000952 DEBUG(dbgs() << "\t\tSectionID: " << SectionID << " Offset: " << Offset
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000953 << "\n");
Tim Northover37cde972013-05-04 20:14:09 +0000954 if (Arch == Triple::aarch64 &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000955 (RelType == ELF::R_AARCH64_CALL26 || RelType == ELF::R_AARCH64_JUMP26)) {
Tim Northover37cde972013-05-04 20:14:09 +0000956 // This is an AArch64 branch relocation, need to use a stub function.
957 DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
958 SectionEntry &Section = Sections[SectionID];
959
960 // Look for an existing stub.
961 StubMap::const_iterator i = Stubs.find(Value);
962 if (i != Stubs.end()) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000963 resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second,
964 RelType, 0);
Tim Northover37cde972013-05-04 20:14:09 +0000965 DEBUG(dbgs() << " Stub function found\n");
966 } else {
967 // Create a new stub function.
968 DEBUG(dbgs() << " Create a new stub function\n");
969 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000970 uint8_t *StubTargetAddr =
971 createStubFunction(Section.Address + Section.StubOffset);
Tim Northover37cde972013-05-04 20:14:09 +0000972
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000973 RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section.Address,
Tim Northover37cde972013-05-04 20:14:09 +0000974 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000975 RelocationEntry REmovk_g2(SectionID, StubTargetAddr - Section.Address + 4,
Tim Northover37cde972013-05-04 20:14:09 +0000976 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000977 RelocationEntry REmovk_g1(SectionID, StubTargetAddr - Section.Address + 8,
Tim Northover37cde972013-05-04 20:14:09 +0000978 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
979 RelocationEntry REmovk_g0(SectionID,
980 StubTargetAddr - Section.Address + 12,
981 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
982
983 if (Value.SymbolName) {
984 addRelocationForSymbol(REmovz_g3, Value.SymbolName);
985 addRelocationForSymbol(REmovk_g2, Value.SymbolName);
986 addRelocationForSymbol(REmovk_g1, Value.SymbolName);
987 addRelocationForSymbol(REmovk_g0, Value.SymbolName);
988 } else {
989 addRelocationForSection(REmovz_g3, Value.SectionID);
990 addRelocationForSection(REmovk_g2, Value.SectionID);
991 addRelocationForSection(REmovk_g1, Value.SectionID);
992 addRelocationForSection(REmovk_g0, Value.SectionID);
993 }
994 resolveRelocation(Section, Offset,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000995 (uint64_t)Section.Address + Section.StubOffset, RelType,
996 0);
Tim Northover37cde972013-05-04 20:14:09 +0000997 Section.StubOffset += getMaxStubSize();
998 }
999 } else if (Arch == Triple::arm &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001000 (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL ||
1001 RelType == ELF::R_ARM_JUMP24)) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001002 // This is an ARM branch relocation, need to use a stub function.
1003 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001004 SectionEntry &Section = Sections[SectionID];
Eli Bendersky4c647582012-01-16 08:56:09 +00001005
Eric Christopherc33f6222012-10-23 17:19:15 +00001006 // Look for an existing stub.
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001007 StubMap::const_iterator i = Stubs.find(Value);
1008 if (i != Stubs.end()) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001009 resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second,
1010 RelType, 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001011 DEBUG(dbgs() << " Stub function found\n");
1012 } else {
1013 // Create a new stub function.
1014 DEBUG(dbgs() << " Create a new stub function\n");
1015 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001016 uint8_t *StubTargetAddr =
1017 createStubFunction(Section.Address + Section.StubOffset);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001018 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Tim Northover3b684d82013-05-28 19:48:19 +00001019 ELF::R_ARM_PRIVATE_0, Value.Addend);
Eli Bendersky667b8792012-05-01 10:41:12 +00001020 if (Value.SymbolName)
1021 addRelocationForSymbol(RE, Value.SymbolName);
1022 else
1023 addRelocationForSection(RE, Value.SectionID);
1024
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001025 resolveRelocation(Section, Offset,
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001026 (uint64_t)Section.Address + Section.StubOffset, RelType,
1027 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001028 Section.StubOffset += getMaxStubSize();
1029 }
Akira Hatanakaa667aad2012-12-03 23:12:19 +00001030 } else if ((Arch == Triple::mipsel || Arch == Triple::mips) &&
1031 RelType == ELF::R_MIPS_26) {
Akira Hatanaka111174b2012-08-17 21:28:04 +00001032 // This is an Mips branch relocation, need to use a stub function.
1033 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001034 SectionEntry &Section = Sections[SectionID];
1035 uint8_t *Target = Section.Address + Offset;
Akira Hatanaka111174b2012-08-17 21:28:04 +00001036 uint32_t *TargetAddress = (uint32_t *)Target;
1037
1038 // Extract the addend from the instruction.
1039 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
1040
1041 Value.Addend += Addend;
1042
1043 // Look up for existing stub.
1044 StubMap::const_iterator i = Stubs.find(Value);
1045 if (i != Stubs.end()) {
Petar Jovanovic45115f82013-11-19 21:56:00 +00001046 RelocationEntry RE(SectionID, Offset, RelType, i->second);
1047 addRelocationForSection(RE, SectionID);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001048 DEBUG(dbgs() << " Stub function found\n");
1049 } else {
1050 // Create a new stub function.
1051 DEBUG(dbgs() << " Create a new stub function\n");
1052 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001053 uint8_t *StubTargetAddr =
1054 createStubFunction(Section.Address + Section.StubOffset);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001055
1056 // Creating Hi and Lo relocations for the filled stub instructions.
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001057 RelocationEntry REHi(SectionID, StubTargetAddr - Section.Address,
Akira Hatanaka2e236242013-07-24 01:58:40 +00001058 ELF::R_MIPS_UNUSED1, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001059 RelocationEntry RELo(SectionID, StubTargetAddr - Section.Address + 4,
Akira Hatanaka2e236242013-07-24 01:58:40 +00001060 ELF::R_MIPS_UNUSED2, Value.Addend);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001061
1062 if (Value.SymbolName) {
1063 addRelocationForSymbol(REHi, Value.SymbolName);
1064 addRelocationForSymbol(RELo, Value.SymbolName);
1065 } else {
1066 addRelocationForSection(REHi, Value.SectionID);
1067 addRelocationForSection(RELo, Value.SectionID);
1068 }
1069
Petar Jovanovic45115f82013-11-19 21:56:00 +00001070 RelocationEntry RE(SectionID, Offset, RelType, Section.StubOffset);
1071 addRelocationForSection(RE, SectionID);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001072 Section.StubOffset += getMaxStubSize();
1073 }
Bill Schmidt0a9170d2013-07-26 01:35:43 +00001074 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001075 if (RelType == ELF::R_PPC64_REL24) {
1076 // A PPC branch relocation will need a stub function if the target is
1077 // an external symbol (Symbol::ST_Unknown) or if the target address
1078 // is not within the signed 24-bits branch address.
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001079 SectionEntry &Section = Sections[SectionID];
1080 uint8_t *Target = Section.Address + Offset;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001081 bool RangeOverflow = false;
1082 if (SymType != SymbolRef::ST_Unknown) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001083 // A function call may points to the .opd entry, so the final symbol
1084 // value
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001085 // in calculated based in the relocation values in .opd section.
1086 findOPDEntrySection(Obj, ObjSectionToID, Value);
1087 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
1088 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
1089 // If it is within 24-bits branch range, just set the branch target
1090 if (SignExtend32<24>(delta) == delta) {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001091 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001092 if (Value.SymbolName)
1093 addRelocationForSymbol(RE, Value.SymbolName);
1094 else
1095 addRelocationForSection(RE, Value.SectionID);
1096 } else {
1097 RangeOverflow = true;
1098 }
1099 }
1100 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
1101 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
1102 // larger than 24-bits.
1103 StubMap::const_iterator i = Stubs.find(Value);
1104 if (i != Stubs.end()) {
1105 // Symbol function stub already created, just relocate to it
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001106 resolveRelocation(Section, Offset,
Andrew Kaylorfb05a502012-11-02 19:45:23 +00001107 (uint64_t)Section.Address + i->second, RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001108 DEBUG(dbgs() << " Stub function found\n");
1109 } else {
1110 // Create a new stub function.
1111 DEBUG(dbgs() << " Create a new stub function\n");
1112 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001113 uint8_t *StubTargetAddr =
1114 createStubFunction(Section.Address + Section.StubOffset);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001115 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001116 ELF::R_PPC64_ADDR64, Value.Addend);
1117
1118 // Generates the 64-bits address loads as exemplified in section
1119 // 4.5.1 in PPC64 ELF ABI.
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001120 RelocationEntry REhst(SectionID, StubTargetAddr - Section.Address + 2,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001121 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001122 RelocationEntry REhr(SectionID, StubTargetAddr - Section.Address + 6,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001123 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001124 RelocationEntry REh(SectionID, StubTargetAddr - Section.Address + 14,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001125 ELF::R_PPC64_ADDR16_HI, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001126 RelocationEntry REl(SectionID, StubTargetAddr - Section.Address + 18,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001127 ELF::R_PPC64_ADDR16_LO, Value.Addend);
1128
1129 if (Value.SymbolName) {
1130 addRelocationForSymbol(REhst, Value.SymbolName);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001131 addRelocationForSymbol(REhr, Value.SymbolName);
1132 addRelocationForSymbol(REh, Value.SymbolName);
1133 addRelocationForSymbol(REl, Value.SymbolName);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001134 } else {
1135 addRelocationForSection(REhst, Value.SectionID);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001136 addRelocationForSection(REhr, Value.SectionID);
1137 addRelocationForSection(REh, Value.SectionID);
1138 addRelocationForSection(REl, Value.SectionID);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001139 }
1140
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001141 resolveRelocation(Section, Offset,
Andrew Kaylorfb05a502012-11-02 19:45:23 +00001142 (uint64_t)Section.Address + Section.StubOffset,
1143 RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001144 Section.StubOffset += getMaxStubSize();
1145 }
Ulrich Weigandfa84ac92014-03-11 15:26:27 +00001146 if (SymType == SymbolRef::ST_Unknown)
1147 // Restore the TOC for external calls
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001148 writeInt32BE(Target + 4, 0xE8410028); // ld r2,40(r1)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001149 }
1150 } else {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001151 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001152 // Extra check to avoid relocation againt empty symbols (usually
1153 // the R_PPC64_TOC).
Richard Mittonad6d3492013-08-16 18:54:26 +00001154 if (SymType != SymbolRef::ST_Unknown && TargetName.empty())
1155 Value.SymbolName = NULL;
1156
1157 if (Value.SymbolName)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001158 addRelocationForSymbol(RE, Value.SymbolName);
1159 else
1160 addRelocationForSection(RE, Value.SectionID);
1161 }
Richard Sandifordca044082013-05-03 14:15:35 +00001162 } else if (Arch == Triple::systemz &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001163 (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) {
Richard Sandifordca044082013-05-03 14:15:35 +00001164 // Create function stubs for both PLT and GOT references, regardless of
1165 // whether the GOT reference is to data or code. The stub contains the
1166 // full address of the symbol, as needed by GOT references, and the
1167 // executable part only adds an overhead of 8 bytes.
1168 //
1169 // We could try to conserve space by allocating the code and data
1170 // parts of the stub separately. However, as things stand, we allocate
1171 // a stub for every relocation, so using a GOT in JIT code should be
1172 // no less space efficient than using an explicit constant pool.
1173 DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
1174 SectionEntry &Section = Sections[SectionID];
1175
1176 // Look for an existing stub.
1177 StubMap::const_iterator i = Stubs.find(Value);
1178 uintptr_t StubAddress;
1179 if (i != Stubs.end()) {
1180 StubAddress = uintptr_t(Section.Address) + i->second;
1181 DEBUG(dbgs() << " Stub function found\n");
1182 } else {
1183 // Create a new stub function.
1184 DEBUG(dbgs() << " Create a new stub function\n");
1185
1186 uintptr_t BaseAddress = uintptr_t(Section.Address);
1187 uintptr_t StubAlignment = getStubAlignment();
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001188 StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) &
1189 -StubAlignment;
Richard Sandifordca044082013-05-03 14:15:35 +00001190 unsigned StubOffset = StubAddress - BaseAddress;
1191
1192 Stubs[Value] = StubOffset;
1193 createStubFunction((uint8_t *)StubAddress);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001194 RelocationEntry RE(SectionID, StubOffset + 8, ELF::R_390_64,
1195 Value.Addend - Addend);
Richard Sandifordca044082013-05-03 14:15:35 +00001196 if (Value.SymbolName)
1197 addRelocationForSymbol(RE, Value.SymbolName);
1198 else
1199 addRelocationForSection(RE, Value.SectionID);
1200 Section.StubOffset = StubOffset + getMaxStubSize();
1201 }
1202
1203 if (RelType == ELF::R_390_GOTENT)
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001204 resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL,
1205 Addend);
Richard Sandifordca044082013-05-03 14:15:35 +00001206 else
1207 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001208 } else if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_PLT32) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001209 // The way the PLT relocations normally work is that the linker allocates
1210 // the
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001211 // PLT and this relocation makes a PC-relative call into the PLT. The PLT
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001212 // entry will then jump to an address provided by the GOT. On first call,
1213 // the
1214 // GOT address will point back into PLT code that resolves the symbol. After
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001215 // the first call, the GOT entry points to the actual function.
1216 //
1217 // For local functions we're ignoring all of that here and just replacing
1218 // the PLT32 relocation type with PC32, which will translate the relocation
1219 // into a PC-relative call directly to the function. For external symbols we
1220 // can't be sure the function will be within 2^32 bytes of the call site, so
1221 // we need to create a stub, which calls into the GOT. This case is
1222 // equivalent to the usual PLT implementation except that we use the stub
1223 // mechanism in RuntimeDyld (which puts stubs at the end of the section)
1224 // rather than allocating a PLT section.
1225 if (Value.SymbolName) {
1226 // This is a call to an external function.
1227 // Look for an existing stub.
1228 SectionEntry &Section = Sections[SectionID];
1229 StubMap::const_iterator i = Stubs.find(Value);
1230 uintptr_t StubAddress;
1231 if (i != Stubs.end()) {
1232 StubAddress = uintptr_t(Section.Address) + i->second;
1233 DEBUG(dbgs() << " Stub function found\n");
1234 } else {
1235 // Create a new stub function (equivalent to a PLT entry).
1236 DEBUG(dbgs() << " Create a new stub function\n");
1237
1238 uintptr_t BaseAddress = uintptr_t(Section.Address);
1239 uintptr_t StubAlignment = getStubAlignment();
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001240 StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) &
1241 -StubAlignment;
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001242 unsigned StubOffset = StubAddress - BaseAddress;
1243 Stubs[Value] = StubOffset;
1244 createStubFunction((uint8_t *)StubAddress);
1245
1246 // Create a GOT entry for the external function.
1247 GOTEntries.push_back(Value);
1248
1249 // Make our stub function a relative call to the GOT entry.
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001250 RelocationEntry RE(SectionID, StubOffset + 2, ELF::R_X86_64_GOTPCREL,
1251 -4);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001252 addRelocationForSymbol(RE, Value.SymbolName);
1253
1254 // Bump our stub offset counter
1255 Section.StubOffset = StubOffset + getMaxStubSize();
1256 }
1257
1258 // Make the target call a call into the stub table.
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001259 resolveRelocation(Section, Offset, StubAddress, ELF::R_X86_64_PC32,
1260 Addend);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001261 } else {
1262 RelocationEntry RE(SectionID, Offset, ELF::R_X86_64_PC32, Value.Addend,
1263 Value.Offset);
1264 addRelocationForSection(RE, Value.SectionID);
1265 }
Eli Bendersky667b8792012-05-01 10:41:12 +00001266 } else {
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001267 if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_GOTPCREL) {
1268 GOTEntries.push_back(Value);
1269 }
1270 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset);
Eli Bendersky667b8792012-05-01 10:41:12 +00001271 if (Value.SymbolName)
1272 addRelocationForSymbol(RE, Value.SymbolName);
1273 else
1274 addRelocationForSection(RE, Value.SectionID);
1275 }
Juergen Ributzka046709f2014-03-21 07:26:41 +00001276 return ++RelI;
Jim Grosbacheff0a402012-01-16 22:26:39 +00001277}
1278
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001279void RuntimeDyldELF::updateGOTEntries(StringRef Name, uint64_t Addr) {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001280
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001281 SmallVectorImpl<std::pair<SID, GOTRelocations>>::iterator it;
1282 SmallVectorImpl<std::pair<SID, GOTRelocations>>::iterator end = GOTs.end();
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001283
1284 for (it = GOTs.begin(); it != end; ++it) {
1285 GOTRelocations &GOTEntries = it->second;
1286 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
1287 if (GOTEntries[i].SymbolName != 0 && GOTEntries[i].SymbolName == Name) {
1288 GOTEntries[i].Offset = Addr;
1289 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001290 }
1291 }
1292}
1293
1294size_t RuntimeDyldELF::getGOTEntrySize() {
1295 // We don't use the GOT in all of these cases, but it's essentially free
1296 // to put them all here.
1297 size_t Result = 0;
1298 switch (Arch) {
1299 case Triple::x86_64:
1300 case Triple::aarch64:
1301 case Triple::ppc64:
1302 case Triple::ppc64le:
1303 case Triple::systemz:
1304 Result = sizeof(uint64_t);
1305 break;
1306 case Triple::x86:
1307 case Triple::arm:
1308 case Triple::thumb:
1309 case Triple::mips:
1310 case Triple::mipsel:
1311 Result = sizeof(uint32_t);
1312 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001313 default:
1314 llvm_unreachable("Unsupported CPU type!");
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001315 }
1316 return Result;
1317}
1318
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001319uint64_t RuntimeDyldELF::findGOTEntry(uint64_t LoadAddress, uint64_t Offset) {
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001320
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001321 const size_t GOTEntrySize = getGOTEntrySize();
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001322
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001323 SmallVectorImpl<std::pair<SID, GOTRelocations>>::const_iterator it;
1324 SmallVectorImpl<std::pair<SID, GOTRelocations>>::const_iterator end =
1325 GOTs.end();
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001326
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001327 int GOTIndex = -1;
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001328 for (it = GOTs.begin(); it != end; ++it) {
1329 SID GOTSectionID = it->first;
1330 const GOTRelocations &GOTEntries = it->second;
1331
1332 // Find the matching entry in our vector.
1333 uint64_t SymbolOffset = 0;
1334 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
1335 if (GOTEntries[i].SymbolName == 0) {
1336 if (getSectionLoadAddress(GOTEntries[i].SectionID) == LoadAddress &&
1337 GOTEntries[i].Offset == Offset) {
1338 GOTIndex = i;
1339 SymbolOffset = GOTEntries[i].Offset;
1340 break;
1341 }
1342 } else {
1343 // GOT entries for external symbols use the addend as the address when
1344 // the external symbol has been resolved.
1345 if (GOTEntries[i].Offset == LoadAddress) {
1346 GOTIndex = i;
1347 // Don't use the Addend here. The relocation handler will use it.
1348 break;
1349 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001350 }
1351 }
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001352
1353 if (GOTIndex != -1) {
1354 if (GOTEntrySize == sizeof(uint64_t)) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001355 uint64_t *LocalGOTAddr = (uint64_t *)getSectionAddress(GOTSectionID);
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001356 // Fill in this entry with the address of the symbol being referenced.
1357 LocalGOTAddr[GOTIndex] = LoadAddress + SymbolOffset;
1358 } else {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001359 uint32_t *LocalGOTAddr = (uint32_t *)getSectionAddress(GOTSectionID);
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001360 // Fill in this entry with the address of the symbol being referenced.
1361 LocalGOTAddr[GOTIndex] = (uint32_t)(LoadAddress + SymbolOffset);
1362 }
1363
1364 // Calculate the load address of this entry
1365 return getSectionLoadAddress(GOTSectionID) + (GOTIndex * GOTEntrySize);
1366 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001367 }
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001368
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001369 assert(GOTIndex != -1 && "Unable to find requested GOT entry.");
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001370 return 0;
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001371}
1372
Andrew Kaylor7bb13442013-10-11 21:25:48 +00001373void RuntimeDyldELF::finalizeLoad(ObjSectionToIDMap &SectionMap) {
1374 // If necessary, allocate the global offset table
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001375 if (MemMgr) {
1376 // Allocate the GOT if necessary
1377 size_t numGOTEntries = GOTEntries.size();
1378 if (numGOTEntries != 0) {
1379 // Allocate memory for the section
1380 unsigned SectionID = Sections.size();
1381 size_t TotalSize = numGOTEntries * getGOTEntrySize();
1382 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, getGOTEntrySize(),
1383 SectionID, ".got", false);
1384 if (!Addr)
1385 report_fatal_error("Unable to allocate memory for GOT!");
1386
1387 GOTs.push_back(std::make_pair(SectionID, GOTEntries));
1388 Sections.push_back(SectionEntry(".got", Addr, TotalSize, 0));
1389 // For now, initialize all GOT entries to zero. We'll fill them in as
1390 // needed when GOT-based relocations are applied.
1391 memset(Addr, 0, TotalSize);
1392 }
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001393 } else {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001394 report_fatal_error("Unable to allocate memory for GOT!");
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001395 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +00001396
1397 // Look for and record the EH frame section.
1398 ObjSectionToIDMap::iterator i, e;
1399 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
1400 const SectionRef &Section = i->first;
1401 StringRef Name;
1402 Section.getName(Name);
1403 if (Name == ".eh_frame") {
1404 UnregisteredEHFrameSections.push_back(i->second);
1405 break;
1406 }
1407 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001408}
1409
Andrew Kayloradc70562012-10-02 21:18:39 +00001410bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
1411 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
1412 return false;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001413 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic,
1414 strlen(ELF::ElfMagic))) == 0;
Eli Bendersky4c647582012-01-16 08:56:09 +00001415}
Lang Hames173c69f2014-01-08 04:09:09 +00001416
1417bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile *Obj) const {
1418 return Obj->isELF();
1419}
1420
Eli Bendersky4c647582012-01-16 08:56:09 +00001421} // namespace llvm