blob: 6ba24b9b59714a9192673f451597a1316174af3d [file] [log] [blame]
Jim Grosbache0934be2012-01-16 23:50:58 +00001//===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===//
Eli Benderskya66a1852012-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 Kaylor3f23cef2012-10-02 21:18:39 +000014#include "RuntimeDyldELF.h"
15#include "JITRegistrar.h"
16#include "ObjectImageCommon.h"
Eli Benderskya66a1852012-01-16 08:56:09 +000017#include "llvm/ADT/IntervalMap.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/StringRef.h"
Eli Benderskya66a1852012-01-16 08:56:09 +000020#include "llvm/ADT/Triple.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000021#include "llvm/ExecutionEngine/ObjectBuffer.h"
22#include "llvm/ExecutionEngine/ObjectImage.h"
Michael J. Spencer081a1942013-08-08 22:27:13 +000023#include "llvm/Object/ELFObjectFile.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/Object/ObjectFile.h"
25#include "llvm/Support/ELF.h"
Stephen Hines36b56882014-04-23 16:57:46 -070026#include "llvm/Support/MemoryBuffer.h"
27
Eli Benderskya66a1852012-01-16 08:56:09 +000028using namespace llvm;
29using namespace llvm::object;
30
Stephen Hinesdce4a402014-05-29 02:49:00 -070031#define DEBUG_TYPE "dyld"
32
Preston Gurd689ff9c2012-04-16 22:12:58 +000033namespace {
34
Stephen Hines36b56882014-04-23 16:57:46 -070035static inline error_code check(error_code Err) {
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +000036 if (Err) {
37 report_fatal_error(Err.message());
38 }
39 return Err;
40}
41
Stephen Hines36b56882014-04-23 16:57:46 -070042template <class ELFT> class DyldELFObject : public ELFObjectFile<ELFT> {
Rafael Espindola43239072013-04-17 21:20:55 +000043 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Preston Gurd689ff9c2012-04-16 22:12:58 +000044
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000045 typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
46 typedef Elf_Sym_Impl<ELFT> Elf_Sym;
Stephen Hines36b56882014-04-23 16:57:46 -070047 typedef Elf_Rel_Impl<ELFT, false> Elf_Rel;
48 typedef Elf_Rel_Impl<ELFT, true> Elf_Rela;
Preston Gurd689ff9c2012-04-16 22:12:58 +000049
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000050 typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
Preston Gurd689ff9c2012-04-16 22:12:58 +000051
Stephen Hines36b56882014-04-23 16:57:46 -070052 typedef typename ELFDataTypeTypedefHelper<ELFT>::value_type addr_type;
Preston Gurd689ff9c2012-04-16 22:12:58 +000053
Stephen Hinesdce4a402014-05-29 02:49:00 -070054 std::unique_ptr<ObjectFile> UnderlyingFile;
55
Preston Gurd689ff9c2012-04-16 22:12:58 +000056public:
Stephen Hinesdce4a402014-05-29 02:49:00 -070057 DyldELFObject(std::unique_ptr<ObjectFile> UnderlyingFile,
58 MemoryBuffer *Wrapper, error_code &ec);
59
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000060 DyldELFObject(MemoryBuffer *Wrapper, error_code &ec);
Preston Gurd689ff9c2012-04-16 22:12:58 +000061
62 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
63 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr);
64
Andrew Kaylor2e319872012-07-27 17:52:42 +000065 // Methods for type inquiry through isa, cast and dyn_cast
Preston Gurd689ff9c2012-04-16 22:12:58 +000066 static inline bool classof(const Binary *v) {
Stephen Hines36b56882014-04-23 16:57:46 -070067 return (isa<ELFObjectFile<ELFT>>(v) &&
68 classof(cast<ELFObjectFile<ELFT>>(v)));
Preston Gurd689ff9c2012-04-16 22:12:58 +000069 }
Stephen Hines36b56882014-04-23 16:57:46 -070070 static inline bool classof(const ELFObjectFile<ELFT> *v) {
Preston Gurd689ff9c2012-04-16 22:12:58 +000071 return v->isDyldType();
72 }
Preston Gurd689ff9c2012-04-16 22:12:58 +000073};
74
Stephen Hines36b56882014-04-23 16:57:46 -070075template <class ELFT> class ELFObjectImage : public ObjectImageCommon {
Stephen Hines36b56882014-04-23 16:57:46 -070076 bool Registered;
Preston Gurd689ff9c2012-04-16 22:12:58 +000077
Stephen Hines36b56882014-04-23 16:57:46 -070078public:
Stephen Hinesdce4a402014-05-29 02:49:00 -070079 ELFObjectImage(ObjectBuffer *Input, std::unique_ptr<DyldELFObject<ELFT>> Obj)
80 : ObjectImageCommon(Input, std::move(Obj)), Registered(false) {}
Preston Gurd689ff9c2012-04-16 22:12:58 +000081
Stephen Hines36b56882014-04-23 16:57:46 -070082 virtual ~ELFObjectImage() {
83 if (Registered)
84 deregisterWithDebugger();
85 }
Preston Gurd689ff9c2012-04-16 22:12:58 +000086
Stephen Hines36b56882014-04-23 16:57:46 -070087 // Subclasses can override these methods to update the image with loaded
88 // addresses for sections and common symbols
89 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr) override {
Stephen Hinesdce4a402014-05-29 02:49:00 -070090 static_cast<DyldELFObject<ELFT>*>(getObjectFile())
91 ->updateSectionAddress(Sec, Addr);
Stephen Hines36b56882014-04-23 16:57:46 -070092 }
Preston Gurd689ff9c2012-04-16 22:12:58 +000093
Stephen Hines36b56882014-04-23 16:57:46 -070094 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr) override {
Stephen Hinesdce4a402014-05-29 02:49:00 -070095 static_cast<DyldELFObject<ELFT>*>(getObjectFile())
96 ->updateSymbolAddress(Sym, Addr);
Stephen Hines36b56882014-04-23 16:57:46 -070097 }
Preston Gurd689ff9c2012-04-16 22:12:58 +000098
Stephen Hines36b56882014-04-23 16:57:46 -070099 void registerWithDebugger() override {
100 JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
101 Registered = true;
102 }
103 void deregisterWithDebugger() override {
104 JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer);
105 }
Preston Gurd689ff9c2012-04-16 22:12:58 +0000106};
107
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000108// The MemoryBuffer passed into this constructor is just a wrapper around the
109// actual memory. Ultimately, the Binary parent class will take ownership of
110// this MemoryBuffer object but not the underlying memory.
Stephen Hines36b56882014-04-23 16:57:46 -0700111template <class ELFT>
Michael J. Spencerac97f5c2013-01-15 07:44:25 +0000112DyldELFObject<ELFT>::DyldELFObject(MemoryBuffer *Wrapper, error_code &ec)
Stephen Hines36b56882014-04-23 16:57:46 -0700113 : ELFObjectFile<ELFT>(Wrapper, ec) {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000114 this->isDyldELFObject = true;
115}
116
Stephen Hines36b56882014-04-23 16:57:46 -0700117template <class ELFT>
Stephen Hinesdce4a402014-05-29 02:49:00 -0700118DyldELFObject<ELFT>::DyldELFObject(std::unique_ptr<ObjectFile> UnderlyingFile,
119 MemoryBuffer *Wrapper, error_code &ec)
120 : ELFObjectFile<ELFT>(Wrapper, ec),
121 UnderlyingFile(std::move(UnderlyingFile)) {
122 this->isDyldELFObject = true;
123}
124
125template <class ELFT>
Michael J. Spencerac97f5c2013-01-15 07:44:25 +0000126void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec,
127 uint64_t Addr) {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000128 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
Stephen Hines36b56882014-04-23 16:57:46 -0700129 Elf_Shdr *shdr =
130 const_cast<Elf_Shdr *>(reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
Preston Gurd689ff9c2012-04-16 22:12:58 +0000131
132 // This assumes the address passed in matches the target address bitness
133 // The template-based type cast handles everything else.
134 shdr->sh_addr = static_cast<addr_type>(Addr);
135}
136
Stephen Hines36b56882014-04-23 16:57:46 -0700137template <class ELFT>
Michael J. Spencerac97f5c2013-01-15 07:44:25 +0000138void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef,
139 uint64_t Addr) {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000140
Stephen Hines36b56882014-04-23 16:57:46 -0700141 Elf_Sym *sym = const_cast<Elf_Sym *>(
142 ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl()));
Preston Gurd689ff9c2012-04-16 22:12:58 +0000143
144 // This assumes the address passed in matches the target address bitness
145 // The template-based type cast handles everything else.
146 sym->st_value = static_cast<addr_type>(Addr);
147}
148
149} // namespace
150
Eli Benderskya66a1852012-01-16 08:56:09 +0000151namespace llvm {
152
Andrew Kaylor528f6d72013-10-11 21:25:48 +0000153void RuntimeDyldELF::registerEHFrames() {
154 if (!MemMgr)
155 return;
156 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
157 SID EHFrameSID = UnregisteredEHFrameSections[i];
158 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
159 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
160 size_t EHFrameSize = Sections[EHFrameSID].Size;
161 MemMgr->registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
Andrew Kaylor43507d02013-10-16 00:14:21 +0000162 RegisteredEHFrameSections.push_back(EHFrameSID);
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +0000163 }
Andrew Kaylor528f6d72013-10-11 21:25:48 +0000164 UnregisteredEHFrameSections.clear();
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +0000165}
166
Andrew Kaylor43507d02013-10-16 00:14:21 +0000167void RuntimeDyldELF::deregisterEHFrames() {
168 if (!MemMgr)
169 return;
170 for (int i = 0, e = RegisteredEHFrameSections.size(); i != e; ++i) {
171 SID EHFrameSID = RegisteredEHFrameSections[i];
172 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
173 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
174 size_t EHFrameSize = Sections[EHFrameSID].Size;
175 MemMgr->deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
176 }
177 RegisteredEHFrameSections.clear();
178}
179
Stephen Hines36b56882014-04-23 16:57:46 -0700180ObjectImage *
Stephen Hinesdce4a402014-05-29 02:49:00 -0700181RuntimeDyldELF::createObjectImageFromFile(std::unique_ptr<object::ObjectFile> ObjFile) {
Stephen Hines36b56882014-04-23 16:57:46 -0700182 if (!ObjFile)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700183 return nullptr;
Preston Gurd689ff9c2012-04-16 22:12:58 +0000184
Stephen Hines36b56882014-04-23 16:57:46 -0700185 error_code ec;
186 MemoryBuffer *Buffer =
187 MemoryBuffer::getMemBuffer(ObjFile->getData(), "", false);
188
189 if (ObjFile->getBytesInAddress() == 4 && ObjFile->isLittleEndian()) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700190 auto Obj =
191 llvm::make_unique<DyldELFObject<ELFType<support::little, 2, false>>>(
192 std::move(ObjFile), Buffer, ec);
193 return new ELFObjectImage<ELFType<support::little, 2, false>>(
194 nullptr, std::move(Obj));
Stephen Hines36b56882014-04-23 16:57:46 -0700195 } else if (ObjFile->getBytesInAddress() == 4 && !ObjFile->isLittleEndian()) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700196 auto Obj =
197 llvm::make_unique<DyldELFObject<ELFType<support::big, 2, false>>>(
198 std::move(ObjFile), Buffer, ec);
199 return new ELFObjectImage<ELFType<support::big, 2, false>>(nullptr, std::move(Obj));
Stephen Hines36b56882014-04-23 16:57:46 -0700200 } else if (ObjFile->getBytesInAddress() == 8 && !ObjFile->isLittleEndian()) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700201 auto Obj = llvm::make_unique<DyldELFObject<ELFType<support::big, 2, true>>>(
202 std::move(ObjFile), Buffer, ec);
203 return new ELFObjectImage<ELFType<support::big, 2, true>>(nullptr,
204 std::move(Obj));
Stephen Hines36b56882014-04-23 16:57:46 -0700205 } else if (ObjFile->getBytesInAddress() == 8 && ObjFile->isLittleEndian()) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700206 auto Obj =
207 llvm::make_unique<DyldELFObject<ELFType<support::little, 2, true>>>(
208 std::move(ObjFile), Buffer, ec);
209 return new ELFObjectImage<ELFType<support::little, 2, true>>(
210 nullptr, std::move(Obj));
Stephen Hines36b56882014-04-23 16:57:46 -0700211 } else
Preston Gurd689ff9c2012-04-16 22:12:58 +0000212 llvm_unreachable("Unexpected ELF format");
213}
214
Stephen Hines36b56882014-04-23 16:57:46 -0700215ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) {
216 if (Buffer->getBufferSize() < ELF::EI_NIDENT)
217 llvm_unreachable("Unexpected ELF object size");
218 std::pair<unsigned char, unsigned char> Ident =
219 std::make_pair((uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS],
220 (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]);
221 error_code ec;
222
223 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700224 auto Obj =
225 llvm::make_unique<DyldELFObject<ELFType<support::little, 4, false>>>(
Stephen Hines36b56882014-04-23 16:57:46 -0700226 Buffer->getMemBuffer(), ec);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700227 return new ELFObjectImage<ELFType<support::little, 4, false>>(
228 Buffer, std::move(Obj));
Stephen Hines36b56882014-04-23 16:57:46 -0700229 } else if (Ident.first == ELF::ELFCLASS32 &&
230 Ident.second == ELF::ELFDATA2MSB) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700231 auto Obj =
232 llvm::make_unique<DyldELFObject<ELFType<support::big, 4, false>>>(
Stephen Hines36b56882014-04-23 16:57:46 -0700233 Buffer->getMemBuffer(), ec);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700234 return new ELFObjectImage<ELFType<support::big, 4, false>>(Buffer,
235 std::move(Obj));
Stephen Hines36b56882014-04-23 16:57:46 -0700236 } else if (Ident.first == ELF::ELFCLASS64 &&
237 Ident.second == ELF::ELFDATA2MSB) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700238 auto Obj = llvm::make_unique<DyldELFObject<ELFType<support::big, 8, true>>>(
239 Buffer->getMemBuffer(), ec);
240 return new ELFObjectImage<ELFType<support::big, 8, true>>(Buffer, std::move(Obj));
Stephen Hines36b56882014-04-23 16:57:46 -0700241 } else if (Ident.first == ELF::ELFCLASS64 &&
242 Ident.second == ELF::ELFDATA2LSB) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700243 auto Obj =
244 llvm::make_unique<DyldELFObject<ELFType<support::little, 8, true>>>(
Stephen Hines36b56882014-04-23 16:57:46 -0700245 Buffer->getMemBuffer(), ec);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700246 return new ELFObjectImage<ELFType<support::little, 8, true>>(Buffer, std::move(Obj));
Stephen Hines36b56882014-04-23 16:57:46 -0700247 } else
248 llvm_unreachable("Unexpected ELF format");
Preston Gurd689ff9c2012-04-16 22:12:58 +0000249}
Eli Benderskya66a1852012-01-16 08:56:09 +0000250
Stephen Hines36b56882014-04-23 16:57:46 -0700251RuntimeDyldELF::~RuntimeDyldELF() {}
252
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000253void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
Stephen Hines36b56882014-04-23 16:57:46 -0700254 uint64_t Offset, uint64_t Value,
255 uint32_t Type, int64_t Addend,
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000256 uint64_t SymOffset) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000257 switch (Type) {
258 default:
259 llvm_unreachable("Relocation type not implemented yet!");
Stephen Hines36b56882014-04-23 16:57:46 -0700260 break;
Eli Benderskya66a1852012-01-16 08:56:09 +0000261 case ELF::R_X86_64_64: {
Stephen Hines36b56882014-04-23 16:57:46 -0700262 uint64_t *Target = reinterpret_cast<uint64_t *>(Section.Address + Offset);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000263 *Target = Value + Addend;
Stephen Hines36b56882014-04-23 16:57:46 -0700264 DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
265 << format("%p\n", Target));
Eli Benderskya66a1852012-01-16 08:56:09 +0000266 break;
267 }
268 case ELF::R_X86_64_32:
269 case ELF::R_X86_64_32S: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000270 Value += Addend;
Andrew Kaylord83a5472012-07-27 20:30:12 +0000271 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000272 (Type == ELF::R_X86_64_32S &&
Stephen Hines36b56882014-04-23 16:57:46 -0700273 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
Eli Benderskya66a1852012-01-16 08:56:09 +0000274 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
Stephen Hines36b56882014-04-23 16:57:46 -0700275 uint32_t *Target = reinterpret_cast<uint32_t *>(Section.Address + Offset);
Eli Benderskya66a1852012-01-16 08:56:09 +0000276 *Target = TruncatedAddr;
Stephen Hines36b56882014-04-23 16:57:46 -0700277 DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at "
278 << format("%p\n", Target));
Eli Benderskya66a1852012-01-16 08:56:09 +0000279 break;
280 }
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000281 case ELF::R_X86_64_GOTPCREL: {
282 // findGOTEntry returns the 'G + GOT' part of the relocation calculation
283 // based on the load/target address of the GOT (not the current/local addr).
284 uint64_t GOTAddr = findGOTEntry(Value, SymOffset);
Stephen Hines36b56882014-04-23 16:57:46 -0700285 uint32_t *Target = reinterpret_cast<uint32_t *>(Section.Address + Offset);
286 uint64_t FinalAddress = Section.LoadAddress + Offset;
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000287 // The processRelocationRef method combines the symbol offset and the addend
288 // and in most cases that's what we want. For this relocation type, we need
289 // the raw addend, so we subtract the symbol offset to get it.
290 int64_t RealOffset = GOTAddr + Addend - SymOffset - FinalAddress;
291 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
292 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
293 *Target = TruncOffset;
294 break;
295 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000296 case ELF::R_X86_64_PC32: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000297 // Get the placeholder value from the generated object since
298 // a previous relocation attempt may have overwritten the loaded version
Stephen Hines36b56882014-04-23 16:57:46 -0700299 uint32_t *Placeholder =
300 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
301 uint32_t *Target = reinterpret_cast<uint32_t *>(Section.Address + Offset);
302 uint64_t FinalAddress = Section.LoadAddress + Offset;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000303 int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylord83a5472012-07-27 20:30:12 +0000304 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000305 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000306 *Target = TruncOffset;
Eli Benderskya66a1852012-01-16 08:56:09 +0000307 break;
308 }
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000309 case ELF::R_X86_64_PC64: {
310 // Get the placeholder value from the generated object since
311 // a previous relocation attempt may have overwritten the loaded version
Stephen Hines36b56882014-04-23 16:57:46 -0700312 uint64_t *Placeholder =
313 reinterpret_cast<uint64_t *>(Section.ObjAddress + Offset);
314 uint64_t *Target = reinterpret_cast<uint64_t *>(Section.Address + Offset);
315 uint64_t FinalAddress = Section.LoadAddress + Offset;
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000316 *Target = *Placeholder + Value + Addend - FinalAddress;
317 break;
318 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000319 }
320}
321
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000322void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
Stephen Hines36b56882014-04-23 16:57:46 -0700323 uint64_t Offset, uint32_t Value,
324 uint32_t Type, int32_t Addend) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000325 switch (Type) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000326 case ELF::R_386_32: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000327 // Get the placeholder value from the generated object since
328 // a previous relocation attempt may have overwritten the loaded version
Stephen Hines36b56882014-04-23 16:57:46 -0700329 uint32_t *Placeholder =
330 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
331 uint32_t *Target = reinterpret_cast<uint32_t *>(Section.Address + Offset);
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000332 *Target = *Placeholder + Value + Addend;
Eli Benderskya66a1852012-01-16 08:56:09 +0000333 break;
334 }
335 case ELF::R_386_PC32: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000336 // Get the placeholder value from the generated object since
337 // a previous relocation attempt may have overwritten the loaded version
Stephen Hines36b56882014-04-23 16:57:46 -0700338 uint32_t *Placeholder =
339 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
340 uint32_t *Target = reinterpret_cast<uint32_t *>(Section.Address + Offset);
341 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000342 uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000343 *Target = RealOffset;
Eli Benderskya66a1852012-01-16 08:56:09 +0000344 break;
Stephen Hines36b56882014-04-23 16:57:46 -0700345 }
346 default:
347 // There are other relocation types, but it appears these are the
348 // only ones currently used by the LLVM ELF object writer
349 llvm_unreachable("Relocation type not implemented yet!");
350 break;
Eli Benderskya66a1852012-01-16 08:56:09 +0000351 }
352}
353
Tim Northover85829bb2013-05-04 20:13:59 +0000354void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
Stephen Hines36b56882014-04-23 16:57:46 -0700355 uint64_t Offset, uint64_t Value,
356 uint32_t Type, int64_t Addend) {
357 uint32_t *TargetPtr = reinterpret_cast<uint32_t *>(Section.Address + Offset);
Tim Northover85829bb2013-05-04 20:13:59 +0000358 uint64_t FinalAddress = Section.LoadAddress + Offset;
359
360 DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
361 << format("%llx", Section.Address + Offset)
Stephen Hines36b56882014-04-23 16:57:46 -0700362 << " FinalAddress: 0x" << format("%llx", FinalAddress)
363 << " Value: 0x" << format("%llx", Value) << " Type: 0x"
364 << format("%x", Type) << " Addend: 0x" << format("%llx", Addend)
Tim Northover85829bb2013-05-04 20:13:59 +0000365 << "\n");
366
367 switch (Type) {
368 default:
369 llvm_unreachable("Relocation type not implemented yet!");
370 break;
Tim Northoverd52eaae2013-05-04 20:14:14 +0000371 case ELF::R_AARCH64_ABS64: {
Stephen Hines36b56882014-04-23 16:57:46 -0700372 uint64_t *TargetPtr =
373 reinterpret_cast<uint64_t *>(Section.Address + Offset);
Tim Northoverd52eaae2013-05-04 20:14:14 +0000374 *TargetPtr = Value + Addend;
375 break;
376 }
Tim Northover675b9e92013-05-19 15:39:03 +0000377 case ELF::R_AARCH64_PREL32: {
Tim Northover85829bb2013-05-04 20:13:59 +0000378 uint64_t Result = Value + Addend - FinalAddress;
Michael J. Spencer081a1942013-08-08 22:27:13 +0000379 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
Tim Northover85829bb2013-05-04 20:13:59 +0000380 static_cast<int64_t>(Result) <= UINT32_MAX);
381 *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU);
382 break;
383 }
Tim Northover4a9b6b72013-05-04 20:14:09 +0000384 case ELF::R_AARCH64_CALL26: // fallthrough
385 case ELF::R_AARCH64_JUMP26: {
386 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
387 // calculation.
388 uint64_t BranchImm = Value + Addend - FinalAddress;
389
390 // "Check that -2^27 <= result < 2^27".
Michael J. Spencer081a1942013-08-08 22:27:13 +0000391 assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) &&
Tim Northover4a9b6b72013-05-04 20:14:09 +0000392 static_cast<int64_t>(BranchImm) < (1LL << 27));
Tim Northover675b9e92013-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.
396 *TargetPtr &= 0xfc000000U;
Tim Northover4a9b6b72013-05-04 20:14:09 +0000397 // Immediate goes in bits 25:0 of B and BL.
398 *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2;
399 break;
400 }
Tim Northover654c2d62013-05-04 20:14:04 +0000401 case ELF::R_AARCH64_MOVW_UABS_G3: {
402 uint64_t Result = Value + Addend;
Tim Northover675b9e92013-05-19 15:39:03 +0000403
404 // AArch64 code is emitted with .rela relocations. The data already in any
405 // bits affected by the relocation on entry is garbage.
Tim Northover107b2f22013-07-25 12:42:52 +0000406 *TargetPtr &= 0xffe0001fU;
Tim Northover654c2d62013-05-04 20:14:04 +0000407 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
408 *TargetPtr |= Result >> (48 - 5);
Tim Northover6711fc22013-07-01 19:23:10 +0000409 // Shift must be "lsl #48", in bits 22:21
410 assert((*TargetPtr >> 21 & 0x3) == 3 && "invalid shift for relocation");
Tim Northover654c2d62013-05-04 20:14:04 +0000411 break;
412 }
413 case ELF::R_AARCH64_MOVW_UABS_G2_NC: {
414 uint64_t Result = Value + Addend;
Tim Northover675b9e92013-05-19 15:39:03 +0000415
Tim Northover675b9e92013-05-19 15:39:03 +0000416 // AArch64 code is emitted with .rela relocations. The data already in any
417 // bits affected by the relocation on entry is garbage.
Tim Northover107b2f22013-07-25 12:42:52 +0000418 *TargetPtr &= 0xffe0001fU;
Tim Northover654c2d62013-05-04 20:14:04 +0000419 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
420 *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
Tim Northover6711fc22013-07-01 19:23:10 +0000421 // Shift must be "lsl #32", in bits 22:21
422 assert((*TargetPtr >> 21 & 0x3) == 2 && "invalid shift for relocation");
Tim Northover654c2d62013-05-04 20:14:04 +0000423 break;
424 }
425 case ELF::R_AARCH64_MOVW_UABS_G1_NC: {
426 uint64_t Result = Value + Addend;
Tim Northover675b9e92013-05-19 15:39:03 +0000427
428 // AArch64 code is emitted with .rela relocations. The data already in any
429 // bits affected by the relocation on entry is garbage.
Tim Northover107b2f22013-07-25 12:42:52 +0000430 *TargetPtr &= 0xffe0001fU;
Tim Northover654c2d62013-05-04 20:14:04 +0000431 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
432 *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
Tim Northover6711fc22013-07-01 19:23:10 +0000433 // Shift must be "lsl #16", in bits 22:2
434 assert((*TargetPtr >> 21 & 0x3) == 1 && "invalid shift for relocation");
Tim Northover654c2d62013-05-04 20:14:04 +0000435 break;
436 }
437 case ELF::R_AARCH64_MOVW_UABS_G0_NC: {
438 uint64_t Result = Value + Addend;
Tim Northover675b9e92013-05-19 15:39:03 +0000439
440 // AArch64 code is emitted with .rela relocations. The data already in any
441 // bits affected by the relocation on entry is garbage.
Tim Northover107b2f22013-07-25 12:42:52 +0000442 *TargetPtr &= 0xffe0001fU;
Tim Northover654c2d62013-05-04 20:14:04 +0000443 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
444 *TargetPtr |= ((Result & 0xffffU) << 5);
Tim Northover6711fc22013-07-01 19:23:10 +0000445 // Shift must be "lsl #0", in bits 22:21.
446 assert((*TargetPtr >> 21 & 0x3) == 0 && "invalid shift for relocation");
Tim Northover654c2d62013-05-04 20:14:04 +0000447 break;
448 }
Stephen Hines36b56882014-04-23 16:57:46 -0700449 case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
450 // Operation: Page(S+A) - Page(P)
451 uint64_t Result =
452 ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
453
454 // Check that -2^32 <= X < 2^32
455 assert(static_cast<int64_t>(Result) >= (-1LL << 32) &&
456 static_cast<int64_t>(Result) < (1LL << 32) &&
457 "overflow check failed for relocation");
458
459 // AArch64 code is emitted with .rela relocations. The data already in any
460 // bits affected by the relocation on entry is garbage.
461 *TargetPtr &= 0x9f00001fU;
462 // Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken
463 // from bits 32:12 of X.
464 *TargetPtr |= ((Result & 0x3000U) << (29 - 12));
465 *TargetPtr |= ((Result & 0x1ffffc000ULL) >> (14 - 5));
466 break;
467 }
468 case ELF::R_AARCH64_LDST32_ABS_LO12_NC: {
469 // Operation: S + A
470 uint64_t Result = Value + Addend;
471
472 // AArch64 code is emitted with .rela relocations. The data already in any
473 // bits affected by the relocation on entry is garbage.
474 *TargetPtr &= 0xffc003ffU;
475 // Immediate goes in bits 21:10 of LD/ST instruction, taken
476 // from bits 11:2 of X
477 *TargetPtr |= ((Result & 0xffc) << (10 - 2));
478 break;
479 }
480 case ELF::R_AARCH64_LDST64_ABS_LO12_NC: {
481 // Operation: S + A
482 uint64_t Result = Value + Addend;
483
484 // AArch64 code is emitted with .rela relocations. The data already in any
485 // bits affected by the relocation on entry is garbage.
486 *TargetPtr &= 0xffc003ffU;
487 // Immediate goes in bits 21:10 of LD/ST instruction, taken
488 // from bits 11:3 of X
489 *TargetPtr |= ((Result & 0xff8) << (10 - 3));
490 break;
491 }
Tim Northover85829bb2013-05-04 20:13:59 +0000492 }
493}
494
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000495void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
Stephen Hines36b56882014-04-23 16:57:46 -0700496 uint64_t Offset, uint32_t Value,
497 uint32_t Type, int32_t Addend) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000498 // TODO: Add Thumb relocations.
Stephen Hines36b56882014-04-23 16:57:46 -0700499 uint32_t *Placeholder =
500 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
501 uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset);
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000502 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000503 Value += Addend;
504
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000505 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
506 << Section.Address + Offset
Stephen Hines36b56882014-04-23 16:57:46 -0700507 << " FinalAddress: " << format("%p", FinalAddress) << " Value: "
508 << format("%x", Value) << " Type: " << format("%x", Type)
509 << " Addend: " << format("%x", Addend) << "\n");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000510
Stephen Hines36b56882014-04-23 16:57:46 -0700511 switch (Type) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000512 default:
513 llvm_unreachable("Not implemented relocation type!");
514
Stephen Hines36b56882014-04-23 16:57:46 -0700515 case ELF::R_ARM_NONE:
516 break;
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000517 // Write a 32bit value to relocation address, taking into account the
Tim Northover565ebde2012-10-03 16:29:42 +0000518 // implicit addend encoded in the target.
Stephen Hines36b56882014-04-23 16:57:46 -0700519 case ELF::R_ARM_PREL31:
Tim Northovere274b472013-05-28 19:48:19 +0000520 case ELF::R_ARM_TARGET1:
521 case ELF::R_ARM_ABS32:
522 *TargetPtr = *Placeholder + Value;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000523 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000524 // Write first 16 bit of 32 bit value to the mov instruction.
525 // Last 4 bit should be shifted.
Tim Northovere274b472013-05-28 19:48:19 +0000526 case ELF::R_ARM_MOVW_ABS_NC:
Tim Northover565ebde2012-10-03 16:29:42 +0000527 // We are not expecting any other addend in the relocation address.
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000528 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
Tim Northover565ebde2012-10-03 16:29:42 +0000529 // non-contiguous fields.
Tim Northovere274b472013-05-28 19:48:19 +0000530 assert((*Placeholder & 0x000F0FFF) == 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000531 Value = Value & 0xFFFF;
Tim Northovere274b472013-05-28 19:48:19 +0000532 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000533 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
534 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000535 // Write last 16 bit of 32 bit value to the mov instruction.
536 // Last 4 bit should be shifted.
Tim Northovere274b472013-05-28 19:48:19 +0000537 case ELF::R_ARM_MOVT_ABS:
Tim Northover565ebde2012-10-03 16:29:42 +0000538 // We are not expecting any other addend in the relocation address.
539 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
Tim Northovere274b472013-05-28 19:48:19 +0000540 assert((*Placeholder & 0x000F0FFF) == 0);
541
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000542 Value = (Value >> 16) & 0xFFFF;
Tim Northovere274b472013-05-28 19:48:19 +0000543 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000544 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
545 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000546 // Write 24 bit relative value to the branch instruction.
Stephen Hines36b56882014-04-23 16:57:46 -0700547 case ELF::R_ARM_PC24: // Fall through.
548 case ELF::R_ARM_CALL: // Fall through.
Tim Northovere274b472013-05-28 19:48:19 +0000549 case ELF::R_ARM_JUMP24: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000550 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
551 RelValue = (RelValue & 0x03FFFFFC) >> 2;
Tim Northovere274b472013-05-28 19:48:19 +0000552 assert((*TargetPtr & 0xFFFFFF) == 0xFFFFFE);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000553 *TargetPtr &= 0xFF000000;
554 *TargetPtr |= RelValue;
555 break;
556 }
Tim Northovere274b472013-05-28 19:48:19 +0000557 case ELF::R_ARM_PRIVATE_0:
558 // This relocation is reserved by the ARM ELF ABI for internal use. We
559 // appropriate it here to act as an R_ARM_ABS32 without any addend for use
560 // in the stubs created during JIT (which can't put an addend into the
561 // original object file).
562 *TargetPtr = Value;
563 break;
564 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000565}
566
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000567void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
Stephen Hines36b56882014-04-23 16:57:46 -0700568 uint64_t Offset, uint32_t Value,
569 uint32_t Type, int32_t Addend) {
570 uint32_t *Placeholder =
571 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
572 uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000573 Value += Addend;
574
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000575 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
Stephen Hines36b56882014-04-23 16:57:46 -0700576 << Section.Address + Offset << " FinalAddress: "
577 << format("%p", Section.LoadAddress + Offset) << " Value: "
578 << format("%x", Value) << " Type: " << format("%x", Type)
579 << " Addend: " << format("%x", Addend) << "\n");
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000580
Stephen Hines36b56882014-04-23 16:57:46 -0700581 switch (Type) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000582 default:
583 llvm_unreachable("Not implemented relocation type!");
584 break;
585 case ELF::R_MIPS_32:
Akira Hatanaka3af1c9d2013-07-24 01:58:40 +0000586 *TargetPtr = Value + (*Placeholder);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000587 break;
588 case ELF::R_MIPS_26:
Stephen Hines36b56882014-04-23 16:57:46 -0700589 *TargetPtr = ((*Placeholder) & 0xfc000000) | ((Value & 0x0fffffff) >> 2);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000590 break;
591 case ELF::R_MIPS_HI16:
592 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
Akira Hatanaka3af1c9d2013-07-24 01:58:40 +0000593 Value += ((*Placeholder) & 0x0000ffff) << 16;
Stephen Hines36b56882014-04-23 16:57:46 -0700594 *TargetPtr =
595 ((*Placeholder) & 0xffff0000) | (((Value + 0x8000) >> 16) & 0xffff);
Akira Hatanaka3af1c9d2013-07-24 01:58:40 +0000596 break;
597 case ELF::R_MIPS_LO16:
598 Value += ((*Placeholder) & 0x0000ffff);
599 *TargetPtr = ((*Placeholder) & 0xffff0000) | (Value & 0xffff);
600 break;
601 case ELF::R_MIPS_UNUSED1:
602 // Similar to ELF::R_ARM_PRIVATE_0, R_MIPS_UNUSED1 and R_MIPS_UNUSED2
603 // are used for internal JIT purpose. These relocations are similar to
604 // R_MIPS_HI16 and R_MIPS_LO16, but they do not take any addend into
605 // account.
Stephen Hines36b56882014-04-23 16:57:46 -0700606 *TargetPtr =
607 ((*TargetPtr) & 0xffff0000) | (((Value + 0x8000) >> 16) & 0xffff);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000608 break;
Akira Hatanaka3af1c9d2013-07-24 01:58:40 +0000609 case ELF::R_MIPS_UNUSED2:
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000610 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
611 break;
Stephen Hines36b56882014-04-23 16:57:46 -0700612 }
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000613}
614
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000615// Return the .TOC. section address to R_PPC64_TOC relocations.
616uint64_t RuntimeDyldELF::findPPC64TOC() const {
617 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
618 // order. The TOC starts where the first of these sections starts.
619 SectionList::const_iterator it = Sections.begin();
620 SectionList::const_iterator ite = Sections.end();
621 for (; it != ite; ++it) {
Stephen Hines36b56882014-04-23 16:57:46 -0700622 if (it->Name == ".got" || it->Name == ".toc" || it->Name == ".tocbss" ||
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000623 it->Name == ".plt")
624 break;
625 }
626 if (it == ite) {
627 // This may happen for
628 // * references to TOC base base (sym@toc, .odp relocation) without
629 // a .toc directive.
630 // In this case just use the first section (which is usually
631 // the .odp) since the code won't reference the .toc base
632 // directly.
633 it = Sections.begin();
634 }
Stephen Hines36b56882014-04-23 16:57:46 -0700635 assert(it != ite);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000636 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
637 // thus permitting a full 64 Kbytes segment.
638 return it->LoadAddress + 0x8000;
639}
640
641// Returns the sections and offset associated with the ODP entry referenced
642// by Symbol.
643void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
644 ObjSectionToIDMap &LocalSections,
645 RelocationValueRef &Rel) {
646 // Get the ELF symbol value (st_value) to compare with Relocation offset in
647 // .opd entries
Stephen Hines36b56882014-04-23 16:57:46 -0700648 for (section_iterator si = Obj.begin_sections(), se = Obj.end_sections();
649 si != se; ++si) {
Rafael Espindola15e5c462013-06-03 19:37:34 +0000650 section_iterator RelSecI = si->getRelocatedSection();
651 if (RelSecI == Obj.end_sections())
652 continue;
653
654 StringRef RelSectionName;
655 check(RelSecI->getName(RelSectionName));
656 if (RelSectionName != ".opd")
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000657 continue;
658
Stephen Hines36b56882014-04-23 16:57:46 -0700659 for (relocation_iterator i = si->relocation_begin(),
660 e = si->relocation_end();
661 i != e;) {
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000662 // The R_PPC64_ADDR64 relocation indicates the first field
663 // of a .opd entry
664 uint64_t TypeFunc;
665 check(i->getType(TypeFunc));
666 if (TypeFunc != ELF::R_PPC64_ADDR64) {
Stephen Hines36b56882014-04-23 16:57:46 -0700667 ++i;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000668 continue;
669 }
670
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000671 uint64_t TargetSymbolOffset;
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000672 symbol_iterator TargetSymbol = i->getSymbol();
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000673 check(i->getOffset(TargetSymbolOffset));
Rafael Espindola167957f2013-05-09 03:39:05 +0000674 int64_t Addend;
675 check(getELFRelocationAddend(*i, Addend));
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000676
Stephen Hines36b56882014-04-23 16:57:46 -0700677 ++i;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000678 if (i == e)
679 break;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000680
681 // Just check if following relocation is a R_PPC64_TOC
682 uint64_t TypeTOC;
683 check(i->getType(TypeTOC));
684 if (TypeTOC != ELF::R_PPC64_TOC)
685 continue;
686
687 // Finally compares the Symbol value and the target symbol offset
688 // to check if this .opd entry refers to the symbol the relocation
689 // points to.
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000690 if (Rel.Addend != (int64_t)TargetSymbolOffset)
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000691 continue;
692
693 section_iterator tsi(Obj.end_sections());
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000694 check(TargetSymbol->getSection(tsi));
Stephen Hines36b56882014-04-23 16:57:46 -0700695 bool IsCode = false;
696 tsi->isText(IsCode);
697 Rel.SectionID = findOrEmitSection(Obj, (*tsi), IsCode, LocalSections);
Rafael Espindola167957f2013-05-09 03:39:05 +0000698 Rel.Addend = (intptr_t)Addend;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000699 return;
700 }
701 }
702 llvm_unreachable("Attempting to get address of ODP entry!");
703}
704
705// Relocation masks following the #lo(value), #hi(value), #higher(value),
706// and #highest(value) macros defined in section 4.5.1. Relocation Types
707// in PPC-elf64abi document.
708//
Stephen Hines36b56882014-04-23 16:57:46 -0700709static inline uint16_t applyPPClo(uint64_t value) { return value & 0xffff; }
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000710
Stephen Hines36b56882014-04-23 16:57:46 -0700711static inline uint16_t applyPPChi(uint64_t value) {
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000712 return (value >> 16) & 0xffff;
713}
714
Stephen Hines36b56882014-04-23 16:57:46 -0700715static inline uint16_t applyPPChigher(uint64_t value) {
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000716 return (value >> 32) & 0xffff;
717}
718
Stephen Hines36b56882014-04-23 16:57:46 -0700719static inline uint16_t applyPPChighest(uint64_t value) {
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000720 return (value >> 48) & 0xffff;
721}
722
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000723void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
Stephen Hines36b56882014-04-23 16:57:46 -0700724 uint64_t Offset, uint64_t Value,
725 uint32_t Type, int64_t Addend) {
726 uint8_t *LocalAddress = Section.Address + Offset;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000727 switch (Type) {
728 default:
729 llvm_unreachable("Relocation type not implemented yet!");
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000730 break;
Stephen Hines36b56882014-04-23 16:57:46 -0700731 case ELF::R_PPC64_ADDR16_LO:
732 writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000733 break;
Stephen Hines36b56882014-04-23 16:57:46 -0700734 case ELF::R_PPC64_ADDR16_HI:
735 writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000736 break;
Stephen Hines36b56882014-04-23 16:57:46 -0700737 case ELF::R_PPC64_ADDR16_HIGHER:
738 writeInt16BE(LocalAddress, applyPPChigher(Value + Addend));
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000739 break;
Stephen Hines36b56882014-04-23 16:57:46 -0700740 case ELF::R_PPC64_ADDR16_HIGHEST:
741 writeInt16BE(LocalAddress, applyPPChighest(Value + Addend));
742 break;
743 case ELF::R_PPC64_ADDR14: {
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000744 assert(((Value + Addend) & 3) == 0);
745 // Preserve the AA/LK bits in the branch instruction
Stephen Hines36b56882014-04-23 16:57:46 -0700746 uint8_t aalk = *(LocalAddress + 3);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000747 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
748 } break;
Stephen Hines36b56882014-04-23 16:57:46 -0700749 case ELF::R_PPC64_ADDR32: {
Adhemerval Zanella7b449882013-01-04 19:08:13 +0000750 int32_t Result = static_cast<int32_t>(Value + Addend);
751 if (SignExtend32<32>(Result) != Result)
Adhemerval Zanellaa1db5de2013-01-09 17:08:15 +0000752 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
Adhemerval Zanella7b449882013-01-04 19:08:13 +0000753 writeInt32BE(LocalAddress, Result);
754 } break;
Stephen Hines36b56882014-04-23 16:57:46 -0700755 case ELF::R_PPC64_REL24: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000756 uint64_t FinalAddress = (Section.LoadAddress + Offset);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000757 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
758 if (SignExtend32<24>(delta) != delta)
759 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
760 // Generates a 'bl <address>' instruction
761 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
762 } break;
Stephen Hines36b56882014-04-23 16:57:46 -0700763 case ELF::R_PPC64_REL32: {
Adhemerval Zanellaa1db5de2013-01-09 17:08:15 +0000764 uint64_t FinalAddress = (Section.LoadAddress + Offset);
765 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
766 if (SignExtend32<32>(delta) != delta)
767 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
768 writeInt32BE(LocalAddress, delta);
769 } break;
Adhemerval Zanellaf51d7e72013-05-06 17:21:23 +0000770 case ELF::R_PPC64_REL64: {
771 uint64_t FinalAddress = (Section.LoadAddress + Offset);
772 uint64_t Delta = Value - FinalAddress + Addend;
773 writeInt64BE(LocalAddress, Delta);
774 } break;
Stephen Hines36b56882014-04-23 16:57:46 -0700775 case ELF::R_PPC64_ADDR64:
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000776 writeInt64BE(LocalAddress, Value + Addend);
777 break;
Stephen Hines36b56882014-04-23 16:57:46 -0700778 case ELF::R_PPC64_TOC:
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000779 writeInt64BE(LocalAddress, findPPC64TOC());
780 break;
Stephen Hines36b56882014-04-23 16:57:46 -0700781 case ELF::R_PPC64_TOC16: {
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000782 uint64_t TOCStart = findPPC64TOC();
783 Value = applyPPClo((Value + Addend) - TOCStart);
784 writeInt16BE(LocalAddress, applyPPClo(Value));
785 } break;
Stephen Hines36b56882014-04-23 16:57:46 -0700786 case ELF::R_PPC64_TOC16_DS: {
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000787 uint64_t TOCStart = findPPC64TOC();
788 Value = ((Value + Addend) - TOCStart);
789 writeInt16BE(LocalAddress, applyPPClo(Value));
790 } break;
791 }
792}
793
Richard Sandiford6fc2ad62013-05-03 14:15:35 +0000794void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
Stephen Hines36b56882014-04-23 16:57:46 -0700795 uint64_t Offset, uint64_t Value,
796 uint32_t Type, int64_t Addend) {
Richard Sandiford6fc2ad62013-05-03 14:15:35 +0000797 uint8_t *LocalAddress = Section.Address + Offset;
798 switch (Type) {
799 default:
800 llvm_unreachable("Relocation type not implemented yet!");
801 break;
802 case ELF::R_390_PC16DBL:
803 case ELF::R_390_PLT16DBL: {
804 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
805 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
806 writeInt16BE(LocalAddress, Delta / 2);
807 break;
808 }
809 case ELF::R_390_PC32DBL:
810 case ELF::R_390_PLT32DBL: {
811 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
812 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
813 writeInt32BE(LocalAddress, Delta / 2);
814 break;
815 }
816 case ELF::R_390_PC32: {
817 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
818 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
819 writeInt32BE(LocalAddress, Delta);
820 break;
821 }
822 case ELF::R_390_64:
823 writeInt64BE(LocalAddress, Value + Addend);
824 break;
825 }
826}
827
Andrew Kaylor32bd10b2013-08-19 19:38:06 +0000828// The target location for the relocation is described by RE.SectionID and
829// RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
830// SectionEntry has three members describing its location.
831// SectionEntry::Address is the address at which the section has been loaded
832// into memory in the current (host) process. SectionEntry::LoadAddress is the
833// address that the section will have in the target process.
834// SectionEntry::ObjAddress is the address of the bits for this section in the
835// original emitted object image (also in the current address space).
836//
837// Relocations will be applied as if the section were loaded at
838// SectionEntry::LoadAddress, but they will be applied at an address based
839// on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
840// Target memory contents if they are required for value calculations.
841//
842// The Value parameter here is the load address of the symbol for the
843// relocation to be applied. For relocations which refer to symbols in the
844// current object Value will be the LoadAddress of the section in which
845// the symbol resides (RE.Addend provides additional information about the
846// symbol location). For external symbols, Value will be the address of the
847// symbol in the target address space.
Rafael Espindola87b50172013-04-29 17:24:34 +0000848void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
Andrew Kaylor32bd10b2013-08-19 19:38:06 +0000849 uint64_t Value) {
Rafael Espindola87b50172013-04-29 17:24:34 +0000850 const SectionEntry &Section = Sections[RE.SectionID];
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000851 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
852 RE.SymOffset);
Rafael Espindola87b50172013-04-29 17:24:34 +0000853}
854
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000855void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
Stephen Hines36b56882014-04-23 16:57:46 -0700856 uint64_t Offset, uint64_t Value,
857 uint32_t Type, int64_t Addend,
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000858 uint64_t SymOffset) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000859 switch (Arch) {
860 case Triple::x86_64:
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000861 resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
Eli Benderskya66a1852012-01-16 08:56:09 +0000862 break;
863 case Triple::x86:
Stephen Hines36b56882014-04-23 16:57:46 -0700864 resolveX86Relocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000865 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000866 break;
Tim Northover85829bb2013-05-04 20:13:59 +0000867 case Triple::aarch64:
Stephen Hines36b56882014-04-23 16:57:46 -0700868 case Triple::aarch64_be:
Stephen Hinesdce4a402014-05-29 02:49:00 -0700869 case Triple::arm64:
870 case Triple::arm64_be:
Tim Northover85829bb2013-05-04 20:13:59 +0000871 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
872 break;
Stephen Hines36b56882014-04-23 16:57:46 -0700873 case Triple::arm: // Fall through.
874 case Triple::armeb:
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000875 case Triple::thumb:
Stephen Hines36b56882014-04-23 16:57:46 -0700876 case Triple::thumbeb:
877 resolveARMRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000878 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000879 break;
Stephen Hines36b56882014-04-23 16:57:46 -0700880 case Triple::mips: // Fall through.
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000881 case Triple::mipsel:
Stephen Hines36b56882014-04-23 16:57:46 -0700882 resolveMIPSRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL),
883 Type, (uint32_t)(Addend & 0xffffffffL));
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000884 break;
Stephen Hines36b56882014-04-23 16:57:46 -0700885 case Triple::ppc64: // Fall through.
Bill Schmidtf38cc382013-07-26 01:35:43 +0000886 case Triple::ppc64le:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000887 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000888 break;
Richard Sandiford6fc2ad62013-05-03 14:15:35 +0000889 case Triple::systemz:
890 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
891 break;
Stephen Hines36b56882014-04-23 16:57:46 -0700892 default:
893 llvm_unreachable("Unsupported CPU type!");
Eli Benderskya66a1852012-01-16 08:56:09 +0000894 }
895}
896
Stephen Hines36b56882014-04-23 16:57:46 -0700897relocation_iterator RuntimeDyldELF::processRelocationRef(
898 unsigned SectionID, relocation_iterator RelI, ObjectImage &Obj,
899 ObjSectionToIDMap &ObjSectionToID, const SymbolTableMap &Symbols,
900 StubMap &Stubs) {
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000901 uint64_t RelType;
Stephen Hines36b56882014-04-23 16:57:46 -0700902 Check(RelI->getType(RelType));
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000903 int64_t Addend;
Stephen Hines36b56882014-04-23 16:57:46 -0700904 Check(getELFRelocationAddend(*RelI, Addend));
905 symbol_iterator Symbol = RelI->getSymbol();
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000906
907 // Obtain the symbol name which is referenced in the relocation
908 StringRef TargetName;
Rafael Espindola0962b162013-06-05 02:55:01 +0000909 if (Symbol != Obj.end_symbols())
910 Symbol->getName(TargetName);
Stephen Hines36b56882014-04-23 16:57:46 -0700911 DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend
912 << " TargetName: " << TargetName << "\n");
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000913 RelocationValueRef Value;
914 // First search for the symbol in the local symbol table
Rafael Espindola0962b162013-06-05 02:55:01 +0000915 SymbolTableMap::const_iterator lsi = Symbols.end();
916 SymbolRef::Type SymType = SymbolRef::ST_Unknown;
917 if (Symbol != Obj.end_symbols()) {
918 lsi = Symbols.find(TargetName.data());
919 Symbol->getType(SymType);
920 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000921 if (lsi != Symbols.end()) {
922 Value.SectionID = lsi->second.first;
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000923 Value.Offset = lsi->second.second;
Ulrich Weigand03f018a2013-04-05 13:29:04 +0000924 Value.Addend = lsi->second.second + Addend;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000925 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000926 // Search for the symbol in the global symbol table
Rafael Espindola0962b162013-06-05 02:55:01 +0000927 SymbolTableMap::const_iterator gsi = GlobalSymbolTable.end();
928 if (Symbol != Obj.end_symbols())
929 gsi = GlobalSymbolTable.find(TargetName.data());
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000930 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000931 Value.SectionID = gsi->second.first;
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000932 Value.Offset = gsi->second.second;
Ulrich Weigand03f018a2013-04-05 13:29:04 +0000933 Value.Addend = gsi->second.second + Addend;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000934 } else {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000935 switch (SymType) {
Stephen Hines36b56882014-04-23 16:57:46 -0700936 case SymbolRef::ST_Debug: {
937 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
938 // and can be changed by another developers. Maybe best way is add
939 // a new symbol type ST_Section to SymbolRef and use it.
940 section_iterator si(Obj.end_sections());
941 Symbol->getSection(si);
942 if (si == Obj.end_sections())
943 llvm_unreachable("Symbol section not found, bad object file format!");
944 DEBUG(dbgs() << "\t\tThis is section symbol\n");
945 // Default to 'true' in case isText fails (though it never does).
946 bool isCode = true;
947 si->isText(isCode);
948 Value.SectionID = findOrEmitSection(Obj, (*si), isCode, ObjSectionToID);
949 Value.Addend = Addend;
950 break;
951 }
952 case SymbolRef::ST_Data:
953 case SymbolRef::ST_Unknown: {
954 Value.SymbolName = TargetName.data();
955 Value.Addend = Addend;
Richard Mittonb0f79292013-08-16 18:54:26 +0000956
Stephen Hines36b56882014-04-23 16:57:46 -0700957 // Absolute relocations will have a zero symbol ID (STN_UNDEF), which
958 // will manifest here as a NULL symbol name.
959 // We can set this as a valid (but empty) symbol name, and rely
960 // on addRelocationForSymbol to handle this.
961 if (!Value.SymbolName)
962 Value.SymbolName = "";
963 break;
964 }
965 default:
966 llvm_unreachable("Unresolved symbol type!");
967 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000968 }
969 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000970 }
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000971 uint64_t Offset;
Stephen Hines36b56882014-04-23 16:57:46 -0700972 Check(RelI->getOffset(Offset));
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000973
Stephen Hines36b56882014-04-23 16:57:46 -0700974 DEBUG(dbgs() << "\t\tSectionID: " << SectionID << " Offset: " << Offset
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000975 << "\n");
Stephen Hinesdce4a402014-05-29 02:49:00 -0700976 if ((Arch == Triple::aarch64 || Arch == Triple::aarch64_be ||
977 Arch == Triple::arm64 || Arch == Triple::arm64_be) &&
Stephen Hines36b56882014-04-23 16:57:46 -0700978 (RelType == ELF::R_AARCH64_CALL26 || RelType == ELF::R_AARCH64_JUMP26)) {
Tim Northover4a9b6b72013-05-04 20:14:09 +0000979 // This is an AArch64 branch relocation, need to use a stub function.
980 DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
981 SectionEntry &Section = Sections[SectionID];
982
983 // Look for an existing stub.
984 StubMap::const_iterator i = Stubs.find(Value);
985 if (i != Stubs.end()) {
Stephen Hines36b56882014-04-23 16:57:46 -0700986 resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second,
987 RelType, 0);
Tim Northover4a9b6b72013-05-04 20:14:09 +0000988 DEBUG(dbgs() << " Stub function found\n");
989 } else {
990 // Create a new stub function.
991 DEBUG(dbgs() << " Create a new stub function\n");
992 Stubs[Value] = Section.StubOffset;
Stephen Hines36b56882014-04-23 16:57:46 -0700993 uint8_t *StubTargetAddr =
994 createStubFunction(Section.Address + Section.StubOffset);
Tim Northover4a9b6b72013-05-04 20:14:09 +0000995
Stephen Hines36b56882014-04-23 16:57:46 -0700996 RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section.Address,
Tim Northover4a9b6b72013-05-04 20:14:09 +0000997 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
Stephen Hines36b56882014-04-23 16:57:46 -0700998 RelocationEntry REmovk_g2(SectionID, StubTargetAddr - Section.Address + 4,
Tim Northover4a9b6b72013-05-04 20:14:09 +0000999 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
Stephen Hines36b56882014-04-23 16:57:46 -07001000 RelocationEntry REmovk_g1(SectionID, StubTargetAddr - Section.Address + 8,
Tim Northover4a9b6b72013-05-04 20:14:09 +00001001 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
1002 RelocationEntry REmovk_g0(SectionID,
1003 StubTargetAddr - Section.Address + 12,
1004 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
1005
1006 if (Value.SymbolName) {
1007 addRelocationForSymbol(REmovz_g3, Value.SymbolName);
1008 addRelocationForSymbol(REmovk_g2, Value.SymbolName);
1009 addRelocationForSymbol(REmovk_g1, Value.SymbolName);
1010 addRelocationForSymbol(REmovk_g0, Value.SymbolName);
1011 } else {
1012 addRelocationForSection(REmovz_g3, Value.SectionID);
1013 addRelocationForSection(REmovk_g2, Value.SectionID);
1014 addRelocationForSection(REmovk_g1, Value.SectionID);
1015 addRelocationForSection(REmovk_g0, Value.SectionID);
1016 }
1017 resolveRelocation(Section, Offset,
Stephen Hines36b56882014-04-23 16:57:46 -07001018 (uint64_t)Section.Address + Section.StubOffset, RelType,
1019 0);
Tim Northover4a9b6b72013-05-04 20:14:09 +00001020 Section.StubOffset += getMaxStubSize();
1021 }
1022 } else if (Arch == Triple::arm &&
Stephen Hines36b56882014-04-23 16:57:46 -07001023 (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL ||
1024 RelType == ELF::R_ARM_JUMP24)) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +00001025 // This is an ARM branch relocation, need to use a stub function.
1026 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001027 SectionEntry &Section = Sections[SectionID];
Eli Benderskya66a1852012-01-16 08:56:09 +00001028
Eric Christopherbf261f12012-10-23 17:19:15 +00001029 // Look for an existing stub.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +00001030 StubMap::const_iterator i = Stubs.find(Value);
1031 if (i != Stubs.end()) {
Stephen Hines36b56882014-04-23 16:57:46 -07001032 resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second,
1033 RelType, 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +00001034 DEBUG(dbgs() << " Stub function found\n");
1035 } else {
1036 // Create a new stub function.
1037 DEBUG(dbgs() << " Create a new stub function\n");
1038 Stubs[Value] = Section.StubOffset;
Stephen Hines36b56882014-04-23 16:57:46 -07001039 uint8_t *StubTargetAddr =
1040 createStubFunction(Section.Address + Section.StubOffset);
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001041 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Tim Northovere274b472013-05-28 19:48:19 +00001042 ELF::R_ARM_PRIVATE_0, Value.Addend);
Eli Benderskyc201e6e2012-05-01 10:41:12 +00001043 if (Value.SymbolName)
1044 addRelocationForSymbol(RE, Value.SymbolName);
1045 else
1046 addRelocationForSection(RE, Value.SectionID);
1047
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001048 resolveRelocation(Section, Offset,
Stephen Hines36b56882014-04-23 16:57:46 -07001049 (uint64_t)Section.Address + Section.StubOffset, RelType,
1050 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +00001051 Section.StubOffset += getMaxStubSize();
1052 }
Akira Hatanakaade04742012-12-03 23:12:19 +00001053 } else if ((Arch == Triple::mipsel || Arch == Triple::mips) &&
1054 RelType == ELF::R_MIPS_26) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +00001055 // This is an Mips branch relocation, need to use a stub function.
1056 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001057 SectionEntry &Section = Sections[SectionID];
1058 uint8_t *Target = Section.Address + Offset;
Akira Hatanakab889e0c2012-08-17 21:28:04 +00001059 uint32_t *TargetAddress = (uint32_t *)Target;
1060
1061 // Extract the addend from the instruction.
1062 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
1063
1064 Value.Addend += Addend;
1065
1066 // Look up for existing stub.
1067 StubMap::const_iterator i = Stubs.find(Value);
1068 if (i != Stubs.end()) {
Petar Jovanovic0ff917e2013-11-21 00:52:34 +00001069 RelocationEntry RE(SectionID, Offset, RelType, i->second);
1070 addRelocationForSection(RE, SectionID);
Akira Hatanakab889e0c2012-08-17 21:28:04 +00001071 DEBUG(dbgs() << " Stub function found\n");
1072 } else {
1073 // Create a new stub function.
1074 DEBUG(dbgs() << " Create a new stub function\n");
1075 Stubs[Value] = Section.StubOffset;
Stephen Hines36b56882014-04-23 16:57:46 -07001076 uint8_t *StubTargetAddr =
1077 createStubFunction(Section.Address + Section.StubOffset);
Akira Hatanakab889e0c2012-08-17 21:28:04 +00001078
1079 // Creating Hi and Lo relocations for the filled stub instructions.
Stephen Hines36b56882014-04-23 16:57:46 -07001080 RelocationEntry REHi(SectionID, StubTargetAddr - Section.Address,
Akira Hatanaka3af1c9d2013-07-24 01:58:40 +00001081 ELF::R_MIPS_UNUSED1, Value.Addend);
Stephen Hines36b56882014-04-23 16:57:46 -07001082 RelocationEntry RELo(SectionID, StubTargetAddr - Section.Address + 4,
Akira Hatanaka3af1c9d2013-07-24 01:58:40 +00001083 ELF::R_MIPS_UNUSED2, Value.Addend);
Akira Hatanakab889e0c2012-08-17 21:28:04 +00001084
1085 if (Value.SymbolName) {
1086 addRelocationForSymbol(REHi, Value.SymbolName);
1087 addRelocationForSymbol(RELo, Value.SymbolName);
1088 } else {
1089 addRelocationForSection(REHi, Value.SectionID);
1090 addRelocationForSection(RELo, Value.SectionID);
1091 }
1092
Petar Jovanovic0ff917e2013-11-21 00:52:34 +00001093 RelocationEntry RE(SectionID, Offset, RelType, Section.StubOffset);
1094 addRelocationForSection(RE, SectionID);
Akira Hatanakab889e0c2012-08-17 21:28:04 +00001095 Section.StubOffset += getMaxStubSize();
1096 }
Bill Schmidtf38cc382013-07-26 01:35:43 +00001097 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001098 if (RelType == ELF::R_PPC64_REL24) {
1099 // 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 Espindolaefa91f62013-04-29 14:44:23 +00001102 SectionEntry &Section = Sections[SectionID];
1103 uint8_t *Target = Section.Address + Offset;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001104 bool RangeOverflow = false;
1105 if (SymType != SymbolRef::ST_Unknown) {
Stephen Hines36b56882014-04-23 16:57:46 -07001106 // A function call may points to the .opd entry, so the final symbol
1107 // value
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001108 // in calculated based in the relocation values in .opd section.
1109 findOPDEntrySection(Obj, ObjSectionToID, Value);
1110 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
1111 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
1112 // If it is within 24-bits branch range, just set the branch target
1113 if (SignExtend32<24>(delta) == delta) {
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001114 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001115 if (Value.SymbolName)
1116 addRelocationForSymbol(RE, Value.SymbolName);
1117 else
1118 addRelocationForSection(RE, Value.SectionID);
1119 } else {
1120 RangeOverflow = true;
1121 }
1122 }
1123 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
1124 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
1125 // larger than 24-bits.
1126 StubMap::const_iterator i = Stubs.find(Value);
1127 if (i != Stubs.end()) {
1128 // Symbol function stub already created, just relocate to it
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001129 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +00001130 (uint64_t)Section.Address + i->second, RelType, 0);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001131 DEBUG(dbgs() << " Stub function found\n");
1132 } else {
1133 // Create a new stub function.
1134 DEBUG(dbgs() << " Create a new stub function\n");
1135 Stubs[Value] = Section.StubOffset;
Stephen Hines36b56882014-04-23 16:57:46 -07001136 uint8_t *StubTargetAddr =
1137 createStubFunction(Section.Address + Section.StubOffset);
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001138 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001139 ELF::R_PPC64_ADDR64, Value.Addend);
1140
1141 // Generates the 64-bits address loads as exemplified in section
1142 // 4.5.1 in PPC64 ELF ABI.
Stephen Hines36b56882014-04-23 16:57:46 -07001143 RelocationEntry REhst(SectionID, StubTargetAddr - Section.Address + 2,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001144 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
Stephen Hines36b56882014-04-23 16:57:46 -07001145 RelocationEntry REhr(SectionID, StubTargetAddr - Section.Address + 6,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001146 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
Stephen Hines36b56882014-04-23 16:57:46 -07001147 RelocationEntry REh(SectionID, StubTargetAddr - Section.Address + 14,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001148 ELF::R_PPC64_ADDR16_HI, Value.Addend);
Stephen Hines36b56882014-04-23 16:57:46 -07001149 RelocationEntry REl(SectionID, StubTargetAddr - Section.Address + 18,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001150 ELF::R_PPC64_ADDR16_LO, Value.Addend);
1151
1152 if (Value.SymbolName) {
1153 addRelocationForSymbol(REhst, Value.SymbolName);
Stephen Hines36b56882014-04-23 16:57:46 -07001154 addRelocationForSymbol(REhr, Value.SymbolName);
1155 addRelocationForSymbol(REh, Value.SymbolName);
1156 addRelocationForSymbol(REl, Value.SymbolName);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001157 } else {
1158 addRelocationForSection(REhst, Value.SectionID);
Stephen Hines36b56882014-04-23 16:57:46 -07001159 addRelocationForSection(REhr, Value.SectionID);
1160 addRelocationForSection(REh, Value.SectionID);
1161 addRelocationForSection(REl, Value.SectionID);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001162 }
1163
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001164 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +00001165 (uint64_t)Section.Address + Section.StubOffset,
1166 RelType, 0);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001167 Section.StubOffset += getMaxStubSize();
1168 }
Stephen Hines36b56882014-04-23 16:57:46 -07001169 if (SymType == SymbolRef::ST_Unknown)
1170 // Restore the TOC for external calls
1171 writeInt32BE(Target + 4, 0xE8410028); // ld r2,40(r1)
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001172 }
1173 } else {
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001174 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001175 // Extra check to avoid relocation againt empty symbols (usually
1176 // the R_PPC64_TOC).
Richard Mittonb0f79292013-08-16 18:54:26 +00001177 if (SymType != SymbolRef::ST_Unknown && TargetName.empty())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001178 Value.SymbolName = nullptr;
Richard Mittonb0f79292013-08-16 18:54:26 +00001179
1180 if (Value.SymbolName)
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001181 addRelocationForSymbol(RE, Value.SymbolName);
1182 else
1183 addRelocationForSection(RE, Value.SectionID);
1184 }
Richard Sandiford6fc2ad62013-05-03 14:15:35 +00001185 } else if (Arch == Triple::systemz &&
Stephen Hines36b56882014-04-23 16:57:46 -07001186 (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) {
Richard Sandiford6fc2ad62013-05-03 14:15:35 +00001187 // Create function stubs for both PLT and GOT references, regardless of
1188 // whether the GOT reference is to data or code. The stub contains the
1189 // full address of the symbol, as needed by GOT references, and the
1190 // executable part only adds an overhead of 8 bytes.
1191 //
1192 // We could try to conserve space by allocating the code and data
1193 // parts of the stub separately. However, as things stand, we allocate
1194 // a stub for every relocation, so using a GOT in JIT code should be
1195 // no less space efficient than using an explicit constant pool.
1196 DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
1197 SectionEntry &Section = Sections[SectionID];
1198
1199 // Look for an existing stub.
1200 StubMap::const_iterator i = Stubs.find(Value);
1201 uintptr_t StubAddress;
1202 if (i != Stubs.end()) {
1203 StubAddress = uintptr_t(Section.Address) + i->second;
1204 DEBUG(dbgs() << " Stub function found\n");
1205 } else {
1206 // Create a new stub function.
1207 DEBUG(dbgs() << " Create a new stub function\n");
1208
1209 uintptr_t BaseAddress = uintptr_t(Section.Address);
1210 uintptr_t StubAlignment = getStubAlignment();
Stephen Hines36b56882014-04-23 16:57:46 -07001211 StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) &
1212 -StubAlignment;
Richard Sandiford6fc2ad62013-05-03 14:15:35 +00001213 unsigned StubOffset = StubAddress - BaseAddress;
1214
1215 Stubs[Value] = StubOffset;
1216 createStubFunction((uint8_t *)StubAddress);
Stephen Hines36b56882014-04-23 16:57:46 -07001217 RelocationEntry RE(SectionID, StubOffset + 8, ELF::R_390_64,
1218 Value.Addend - Addend);
Richard Sandiford6fc2ad62013-05-03 14:15:35 +00001219 if (Value.SymbolName)
1220 addRelocationForSymbol(RE, Value.SymbolName);
1221 else
1222 addRelocationForSection(RE, Value.SectionID);
1223 Section.StubOffset = StubOffset + getMaxStubSize();
1224 }
1225
1226 if (RelType == ELF::R_390_GOTENT)
Stephen Hines36b56882014-04-23 16:57:46 -07001227 resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL,
1228 Addend);
Richard Sandiford6fc2ad62013-05-03 14:15:35 +00001229 else
1230 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001231 } else if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_PLT32) {
Stephen Hines36b56882014-04-23 16:57:46 -07001232 // The way the PLT relocations normally work is that the linker allocates
1233 // the
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001234 // PLT and this relocation makes a PC-relative call into the PLT. The PLT
Stephen Hines36b56882014-04-23 16:57:46 -07001235 // entry will then jump to an address provided by the GOT. On first call,
1236 // the
1237 // GOT address will point back into PLT code that resolves the symbol. After
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001238 // the first call, the GOT entry points to the actual function.
1239 //
1240 // For local functions we're ignoring all of that here and just replacing
1241 // the PLT32 relocation type with PC32, which will translate the relocation
1242 // into a PC-relative call directly to the function. For external symbols we
1243 // can't be sure the function will be within 2^32 bytes of the call site, so
1244 // we need to create a stub, which calls into the GOT. This case is
1245 // equivalent to the usual PLT implementation except that we use the stub
1246 // mechanism in RuntimeDyld (which puts stubs at the end of the section)
1247 // rather than allocating a PLT section.
1248 if (Value.SymbolName) {
1249 // This is a call to an external function.
1250 // Look for an existing stub.
1251 SectionEntry &Section = Sections[SectionID];
1252 StubMap::const_iterator i = Stubs.find(Value);
1253 uintptr_t StubAddress;
1254 if (i != Stubs.end()) {
1255 StubAddress = uintptr_t(Section.Address) + i->second;
1256 DEBUG(dbgs() << " Stub function found\n");
1257 } else {
1258 // Create a new stub function (equivalent to a PLT entry).
1259 DEBUG(dbgs() << " Create a new stub function\n");
1260
1261 uintptr_t BaseAddress = uintptr_t(Section.Address);
1262 uintptr_t StubAlignment = getStubAlignment();
Stephen Hines36b56882014-04-23 16:57:46 -07001263 StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) &
1264 -StubAlignment;
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001265 unsigned StubOffset = StubAddress - BaseAddress;
1266 Stubs[Value] = StubOffset;
1267 createStubFunction((uint8_t *)StubAddress);
1268
1269 // Create a GOT entry for the external function.
1270 GOTEntries.push_back(Value);
1271
1272 // Make our stub function a relative call to the GOT entry.
Stephen Hines36b56882014-04-23 16:57:46 -07001273 RelocationEntry RE(SectionID, StubOffset + 2, ELF::R_X86_64_GOTPCREL,
1274 -4);
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001275 addRelocationForSymbol(RE, Value.SymbolName);
1276
1277 // Bump our stub offset counter
1278 Section.StubOffset = StubOffset + getMaxStubSize();
1279 }
1280
1281 // Make the target call a call into the stub table.
Stephen Hines36b56882014-04-23 16:57:46 -07001282 resolveRelocation(Section, Offset, StubAddress, ELF::R_X86_64_PC32,
1283 Addend);
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001284 } else {
1285 RelocationEntry RE(SectionID, Offset, ELF::R_X86_64_PC32, Value.Addend,
1286 Value.Offset);
1287 addRelocationForSection(RE, Value.SectionID);
1288 }
Eli Benderskyc201e6e2012-05-01 10:41:12 +00001289 } else {
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001290 if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_GOTPCREL) {
1291 GOTEntries.push_back(Value);
1292 }
1293 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset);
Eli Benderskyc201e6e2012-05-01 10:41:12 +00001294 if (Value.SymbolName)
1295 addRelocationForSymbol(RE, Value.SymbolName);
1296 else
1297 addRelocationForSection(RE, Value.SectionID);
1298 }
Stephen Hines36b56882014-04-23 16:57:46 -07001299 return ++RelI;
Jim Grosbach61425c02012-01-16 22:26:39 +00001300}
1301
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001302void RuntimeDyldELF::updateGOTEntries(StringRef Name, uint64_t Addr) {
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001303
Stephen Hines36b56882014-04-23 16:57:46 -07001304 SmallVectorImpl<std::pair<SID, GOTRelocations>>::iterator it;
1305 SmallVectorImpl<std::pair<SID, GOTRelocations>>::iterator end = GOTs.end();
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001306
1307 for (it = GOTs.begin(); it != end; ++it) {
1308 GOTRelocations &GOTEntries = it->second;
1309 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001310 if (GOTEntries[i].SymbolName != nullptr &&
1311 GOTEntries[i].SymbolName == Name) {
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001312 GOTEntries[i].Offset = Addr;
1313 }
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001314 }
1315 }
1316}
1317
1318size_t RuntimeDyldELF::getGOTEntrySize() {
1319 // We don't use the GOT in all of these cases, but it's essentially free
1320 // to put them all here.
1321 size_t Result = 0;
1322 switch (Arch) {
1323 case Triple::x86_64:
1324 case Triple::aarch64:
Stephen Hinesdce4a402014-05-29 02:49:00 -07001325 case Triple::aarch64_be:
1326 case Triple::arm64:
1327 case Triple::arm64_be:
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001328 case Triple::ppc64:
1329 case Triple::ppc64le:
1330 case Triple::systemz:
1331 Result = sizeof(uint64_t);
1332 break;
1333 case Triple::x86:
1334 case Triple::arm:
1335 case Triple::thumb:
1336 case Triple::mips:
1337 case Triple::mipsel:
1338 Result = sizeof(uint32_t);
1339 break;
Stephen Hines36b56882014-04-23 16:57:46 -07001340 default:
1341 llvm_unreachable("Unsupported CPU type!");
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001342 }
1343 return Result;
1344}
1345
Stephen Hines36b56882014-04-23 16:57:46 -07001346uint64_t RuntimeDyldELF::findGOTEntry(uint64_t LoadAddress, uint64_t Offset) {
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001347
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001348 const size_t GOTEntrySize = getGOTEntrySize();
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001349
Stephen Hines36b56882014-04-23 16:57:46 -07001350 SmallVectorImpl<std::pair<SID, GOTRelocations>>::const_iterator it;
1351 SmallVectorImpl<std::pair<SID, GOTRelocations>>::const_iterator end =
1352 GOTs.end();
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001353
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001354 int GOTIndex = -1;
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001355 for (it = GOTs.begin(); it != end; ++it) {
1356 SID GOTSectionID = it->first;
1357 const GOTRelocations &GOTEntries = it->second;
1358
1359 // Find the matching entry in our vector.
1360 uint64_t SymbolOffset = 0;
1361 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001362 if (!GOTEntries[i].SymbolName) {
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001363 if (getSectionLoadAddress(GOTEntries[i].SectionID) == LoadAddress &&
1364 GOTEntries[i].Offset == Offset) {
1365 GOTIndex = i;
1366 SymbolOffset = GOTEntries[i].Offset;
1367 break;
1368 }
1369 } else {
1370 // GOT entries for external symbols use the addend as the address when
1371 // the external symbol has been resolved.
1372 if (GOTEntries[i].Offset == LoadAddress) {
1373 GOTIndex = i;
1374 // Don't use the Addend here. The relocation handler will use it.
1375 break;
1376 }
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001377 }
1378 }
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001379
1380 if (GOTIndex != -1) {
1381 if (GOTEntrySize == sizeof(uint64_t)) {
Stephen Hines36b56882014-04-23 16:57:46 -07001382 uint64_t *LocalGOTAddr = (uint64_t *)getSectionAddress(GOTSectionID);
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001383 // Fill in this entry with the address of the symbol being referenced.
1384 LocalGOTAddr[GOTIndex] = LoadAddress + SymbolOffset;
1385 } else {
Stephen Hines36b56882014-04-23 16:57:46 -07001386 uint32_t *LocalGOTAddr = (uint32_t *)getSectionAddress(GOTSectionID);
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001387 // Fill in this entry with the address of the symbol being referenced.
1388 LocalGOTAddr[GOTIndex] = (uint32_t)(LoadAddress + SymbolOffset);
1389 }
1390
1391 // Calculate the load address of this entry
1392 return getSectionLoadAddress(GOTSectionID) + (GOTIndex * GOTEntrySize);
1393 }
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001394 }
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001395
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001396 assert(GOTIndex != -1 && "Unable to find requested GOT entry.");
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001397 return 0;
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001398}
1399
Stephen Hinesdce4a402014-05-29 02:49:00 -07001400void RuntimeDyldELF::finalizeLoad(ObjectImage &ObjImg,
1401 ObjSectionToIDMap &SectionMap) {
Andrew Kaylor528f6d72013-10-11 21:25:48 +00001402 // If necessary, allocate the global offset table
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001403 if (MemMgr) {
1404 // Allocate the GOT if necessary
1405 size_t numGOTEntries = GOTEntries.size();
1406 if (numGOTEntries != 0) {
1407 // Allocate memory for the section
1408 unsigned SectionID = Sections.size();
1409 size_t TotalSize = numGOTEntries * getGOTEntrySize();
1410 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, getGOTEntrySize(),
1411 SectionID, ".got", false);
1412 if (!Addr)
1413 report_fatal_error("Unable to allocate memory for GOT!");
1414
1415 GOTs.push_back(std::make_pair(SectionID, GOTEntries));
1416 Sections.push_back(SectionEntry(".got", Addr, TotalSize, 0));
1417 // For now, initialize all GOT entries to zero. We'll fill them in as
1418 // needed when GOT-based relocations are applied.
1419 memset(Addr, 0, TotalSize);
1420 }
Stephen Hines36b56882014-04-23 16:57:46 -07001421 } else {
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001422 report_fatal_error("Unable to allocate memory for GOT!");
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001423 }
Andrew Kaylor528f6d72013-10-11 21:25:48 +00001424
1425 // Look for and record the EH frame section.
1426 ObjSectionToIDMap::iterator i, e;
1427 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
1428 const SectionRef &Section = i->first;
1429 StringRef Name;
1430 Section.getName(Name);
1431 if (Name == ".eh_frame") {
1432 UnregisteredEHFrameSections.push_back(i->second);
1433 break;
1434 }
1435 }
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001436}
1437
Andrew Kaylor3f23cef2012-10-02 21:18:39 +00001438bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
1439 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
1440 return false;
Stephen Hines36b56882014-04-23 16:57:46 -07001441 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic,
1442 strlen(ELF::ElfMagic))) == 0;
Eli Benderskya66a1852012-01-16 08:56:09 +00001443}
Stephen Hines36b56882014-04-23 16:57:46 -07001444
1445bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile *Obj) const {
1446 return Obj->isELF();
1447}
1448
Eli Benderskya66a1852012-01-16 08:56:09 +00001449} // namespace llvm