blob: adea5ad22412e16b190e8d74f5838f7b0cace824 [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"
Alexey Samsonova8d2f812014-08-27 23:06:08 +000026#include "llvm/Support/Endian.h"
Lang Hames173c69f2014-01-08 04:09:09 +000027#include "llvm/Support/MemoryBuffer.h"
28
Eli Bendersky4c647582012-01-16 08:56:09 +000029using namespace llvm;
30using namespace llvm::object;
31
Chandler Carruthf58e3762014-04-22 03:04:17 +000032#define DEBUG_TYPE "dyld"
33
Preston Gurdcc31af92012-04-16 22:12:58 +000034namespace {
35
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000036static inline std::error_code check(std::error_code Err) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +000037 if (Err) {
38 report_fatal_error(Err.message());
39 }
40 return Err;
41}
42
Juergen Ributzka7608dc02014-03-21 20:28:42 +000043template <class ELFT> class DyldELFObject : public ELFObjectFile<ELFT> {
Rafael Espindola035b4162013-04-17 21:20:55 +000044 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Preston Gurdcc31af92012-04-16 22:12:58 +000045
Michael J. Spencer1a791612013-01-15 07:44:25 +000046 typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
47 typedef Elf_Sym_Impl<ELFT> Elf_Sym;
Juergen Ributzka7608dc02014-03-21 20:28:42 +000048 typedef Elf_Rel_Impl<ELFT, false> Elf_Rel;
49 typedef Elf_Rel_Impl<ELFT, true> Elf_Rela;
Preston Gurdcc31af92012-04-16 22:12:58 +000050
Michael J. Spencer1a791612013-01-15 07:44:25 +000051 typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
Preston Gurdcc31af92012-04-16 22:12:58 +000052
Juergen Ributzka7608dc02014-03-21 20:28:42 +000053 typedef typename ELFDataTypeTypedefHelper<ELFT>::value_type addr_type;
Preston Gurdcc31af92012-04-16 22:12:58 +000054
David Blaikie7a1e7752014-04-29 21:52:46 +000055 std::unique_ptr<ObjectFile> UnderlyingFile;
56
Preston Gurdcc31af92012-04-16 22:12:58 +000057public:
David Blaikie7a1e7752014-04-29 21:52:46 +000058 DyldELFObject(std::unique_ptr<ObjectFile> UnderlyingFile,
Rafael Espindola48af1c22014-08-19 18:44:46 +000059 MemoryBufferRef Wrapper, std::error_code &ec);
David Blaikie7a1e7752014-04-29 21:52:46 +000060
Rafael Espindola48af1c22014-08-19 18:44:46 +000061 DyldELFObject(MemoryBufferRef Wrapper, std::error_code &ec);
Preston Gurdcc31af92012-04-16 22:12:58 +000062
63 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
64 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr);
65
Andrew Kaylor5c010902012-07-27 17:52:42 +000066 // Methods for type inquiry through isa, cast and dyn_cast
Preston Gurdcc31af92012-04-16 22:12:58 +000067 static inline bool classof(const Binary *v) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +000068 return (isa<ELFObjectFile<ELFT>>(v) &&
69 classof(cast<ELFObjectFile<ELFT>>(v)));
Preston Gurdcc31af92012-04-16 22:12:58 +000070 }
Juergen Ributzka7608dc02014-03-21 20:28:42 +000071 static inline bool classof(const ELFObjectFile<ELFT> *v) {
Preston Gurdcc31af92012-04-16 22:12:58 +000072 return v->isDyldType();
73 }
Preston Gurdcc31af92012-04-16 22:12:58 +000074};
75
Juergen Ributzka7608dc02014-03-21 20:28:42 +000076template <class ELFT> class ELFObjectImage : public ObjectImageCommon {
Juergen Ributzka7608dc02014-03-21 20:28:42 +000077 bool Registered;
Preston Gurdcc31af92012-04-16 22:12:58 +000078
Juergen Ributzka7608dc02014-03-21 20:28:42 +000079public:
David Blaikie7a1e7752014-04-29 21:52:46 +000080 ELFObjectImage(ObjectBuffer *Input, std::unique_ptr<DyldELFObject<ELFT>> Obj)
81 : ObjectImageCommon(Input, std::move(Obj)), Registered(false) {}
Preston Gurdcc31af92012-04-16 22:12:58 +000082
Juergen Ributzka7608dc02014-03-21 20:28:42 +000083 virtual ~ELFObjectImage() {
84 if (Registered)
85 deregisterWithDebugger();
86 }
Preston Gurdcc31af92012-04-16 22:12:58 +000087
Juergen Ributzka7608dc02014-03-21 20:28:42 +000088 // Subclasses can override these methods to update the image with loaded
89 // addresses for sections and common symbols
90 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr) override {
David Blaikie7a1e7752014-04-29 21:52:46 +000091 static_cast<DyldELFObject<ELFT>*>(getObjectFile())
92 ->updateSectionAddress(Sec, Addr);
Juergen Ributzka7608dc02014-03-21 20:28:42 +000093 }
Preston Gurdcc31af92012-04-16 22:12:58 +000094
Juergen Ributzka7608dc02014-03-21 20:28:42 +000095 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr) override {
David Blaikie7a1e7752014-04-29 21:52:46 +000096 static_cast<DyldELFObject<ELFT>*>(getObjectFile())
97 ->updateSymbolAddress(Sym, Addr);
Juergen Ributzka7608dc02014-03-21 20:28:42 +000098 }
Preston Gurdcc31af92012-04-16 22:12:58 +000099
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000100 void registerWithDebugger() override {
101 JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
102 Registered = true;
103 }
104 void deregisterWithDebugger() override {
105 JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer);
106 }
Preston Gurdcc31af92012-04-16 22:12:58 +0000107};
108
Andrew Kayloradc70562012-10-02 21:18:39 +0000109// The MemoryBuffer passed into this constructor is just a wrapper around the
110// actual memory. Ultimately, the Binary parent class will take ownership of
111// this MemoryBuffer object but not the underlying memory.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000112template <class ELFT>
Rafael Espindola48af1c22014-08-19 18:44:46 +0000113DyldELFObject<ELFT>::DyldELFObject(MemoryBufferRef Wrapper, std::error_code &EC)
114 : ELFObjectFile<ELFT>(Wrapper, EC) {
Preston Gurdcc31af92012-04-16 22:12:58 +0000115 this->isDyldELFObject = true;
116}
117
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000118template <class ELFT>
David Blaikie7a1e7752014-04-29 21:52:46 +0000119DyldELFObject<ELFT>::DyldELFObject(std::unique_ptr<ObjectFile> UnderlyingFile,
Rafael Espindola48af1c22014-08-19 18:44:46 +0000120 MemoryBufferRef Wrapper, std::error_code &EC)
121 : ELFObjectFile<ELFT>(Wrapper, EC),
David Blaikie7a1e7752014-04-29 21:52:46 +0000122 UnderlyingFile(std::move(UnderlyingFile)) {
123 this->isDyldELFObject = true;
124}
125
126template <class ELFT>
Michael J. Spencer1a791612013-01-15 07:44:25 +0000127void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec,
128 uint64_t Addr) {
Preston Gurdcc31af92012-04-16 22:12:58 +0000129 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000130 Elf_Shdr *shdr =
131 const_cast<Elf_Shdr *>(reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
Preston Gurdcc31af92012-04-16 22:12:58 +0000132
133 // This assumes the address passed in matches the target address bitness
134 // The template-based type cast handles everything else.
135 shdr->sh_addr = static_cast<addr_type>(Addr);
136}
137
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000138template <class ELFT>
Michael J. Spencer1a791612013-01-15 07:44:25 +0000139void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef,
140 uint64_t Addr) {
Preston Gurdcc31af92012-04-16 22:12:58 +0000141
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000142 Elf_Sym *sym = const_cast<Elf_Sym *>(
143 ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl()));
Preston Gurdcc31af92012-04-16 22:12:58 +0000144
145 // This assumes the address passed in matches the target address bitness
146 // The template-based type cast handles everything else.
147 sym->st_value = static_cast<addr_type>(Addr);
148}
149
150} // namespace
151
Eli Bendersky4c647582012-01-16 08:56:09 +0000152namespace llvm {
153
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000154void RuntimeDyldELF::registerEHFrames() {
155 if (!MemMgr)
156 return;
157 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
158 SID EHFrameSID = UnregisteredEHFrameSections[i];
159 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
160 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
161 size_t EHFrameSize = Sections[EHFrameSID].Size;
162 MemMgr->registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
Andrew Kaylorc442a762013-10-16 00:14:21 +0000163 RegisteredEHFrameSections.push_back(EHFrameSID);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000164 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000165 UnregisteredEHFrameSections.clear();
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000166}
167
Andrew Kaylorc442a762013-10-16 00:14:21 +0000168void RuntimeDyldELF::deregisterEHFrames() {
169 if (!MemMgr)
170 return;
171 for (int i = 0, e = RegisteredEHFrameSections.size(); i != e; ++i) {
172 SID EHFrameSID = RegisteredEHFrameSections[i];
173 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
174 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
175 size_t EHFrameSize = Sections[EHFrameSID].Size;
176 MemMgr->deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
177 }
178 RegisteredEHFrameSections.clear();
179}
180
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000181ObjectImage *
David Blaikie7a1e7752014-04-29 21:52:46 +0000182RuntimeDyldELF::createObjectImageFromFile(std::unique_ptr<object::ObjectFile> ObjFile) {
Lang Hames173c69f2014-01-08 04:09:09 +0000183 if (!ObjFile)
Craig Topper353eda42014-04-24 06:44:33 +0000184 return nullptr;
Lang Hames173c69f2014-01-08 04:09:09 +0000185
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000186 std::error_code ec;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000187 MemoryBufferRef Buffer = ObjFile->getMemoryBufferRef();
Lang Hames173c69f2014-01-08 04:09:09 +0000188
189 if (ObjFile->getBytesInAddress() == 4 && ObjFile->isLittleEndian()) {
Reid Kleckner7aeb9052014-04-29 23:54:52 +0000190 auto Obj =
191 llvm::make_unique<DyldELFObject<ELFType<support::little, 2, false>>>(
Rafael Espindola48af1c22014-08-19 18:44:46 +0000192 std::move(ObjFile), Buffer, ec);
David Blaikie7a1e7752014-04-29 21:52:46 +0000193 return new ELFObjectImage<ELFType<support::little, 2, false>>(
194 nullptr, std::move(Obj));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000195 } else if (ObjFile->getBytesInAddress() == 4 && !ObjFile->isLittleEndian()) {
Reid Kleckner7aeb9052014-04-29 23:54:52 +0000196 auto Obj =
197 llvm::make_unique<DyldELFObject<ELFType<support::big, 2, false>>>(
Rafael Espindola48af1c22014-08-19 18:44:46 +0000198 std::move(ObjFile), Buffer, ec);
David Blaikie7a1e7752014-04-29 21:52:46 +0000199 return new ELFObjectImage<ELFType<support::big, 2, false>>(nullptr, std::move(Obj));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000200 } else if (ObjFile->getBytesInAddress() == 8 && !ObjFile->isLittleEndian()) {
Reid Kleckner7aeb9052014-04-29 23:54:52 +0000201 auto Obj = llvm::make_unique<DyldELFObject<ELFType<support::big, 2, true>>>(
Rafael Espindola48af1c22014-08-19 18:44:46 +0000202 std::move(ObjFile), Buffer, ec);
David Blaikie7a1e7752014-04-29 21:52:46 +0000203 return new ELFObjectImage<ELFType<support::big, 2, true>>(nullptr,
204 std::move(Obj));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000205 } else if (ObjFile->getBytesInAddress() == 8 && ObjFile->isLittleEndian()) {
Reid Kleckner7aeb9052014-04-29 23:54:52 +0000206 auto Obj =
207 llvm::make_unique<DyldELFObject<ELFType<support::little, 2, true>>>(
Rafael Espindola48af1c22014-08-19 18:44:46 +0000208 std::move(ObjFile), Buffer, ec);
David Blaikie7a1e7752014-04-29 21:52:46 +0000209 return new ELFObjectImage<ELFType<support::little, 2, true>>(
210 nullptr, std::move(Obj));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000211 } else
Lang Hames173c69f2014-01-08 04:09:09 +0000212 llvm_unreachable("Unexpected ELF format");
213}
214
Andrew Kayloradc70562012-10-02 21:18:39 +0000215ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) {
216 if (Buffer->getBufferSize() < ELF::EI_NIDENT)
217 llvm_unreachable("Unexpected ELF object size");
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000218 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]);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000221 std::error_code ec;
Preston Gurdcc31af92012-04-16 22:12:58 +0000222
Rafael Espindola48af1c22014-08-19 18:44:46 +0000223 MemoryBufferRef Buf = Buffer->getMemBuffer();
Rafael Espindola2e60ca92014-06-24 13:56:32 +0000224
Preston Gurdcc31af92012-04-16 22:12:58 +0000225 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
Reid Kleckner7aeb9052014-04-29 23:54:52 +0000226 auto Obj =
227 llvm::make_unique<DyldELFObject<ELFType<support::little, 4, false>>>(
Rafael Espindola48af1c22014-08-19 18:44:46 +0000228 Buf, ec);
David Blaikie7a1e7752014-04-29 21:52:46 +0000229 return new ELFObjectImage<ELFType<support::little, 4, false>>(
230 Buffer, std::move(Obj));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000231 } else if (Ident.first == ELF::ELFCLASS32 &&
232 Ident.second == ELF::ELFDATA2MSB) {
David Blaikie7a1e7752014-04-29 21:52:46 +0000233 auto Obj =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000234 llvm::make_unique<DyldELFObject<ELFType<support::big, 4, false>>>(Buf,
235 ec);
David Blaikie7a1e7752014-04-29 21:52:46 +0000236 return new ELFObjectImage<ELFType<support::big, 4, false>>(Buffer,
237 std::move(Obj));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000238 } else if (Ident.first == ELF::ELFCLASS64 &&
239 Ident.second == ELF::ELFDATA2MSB) {
Reid Kleckner7aeb9052014-04-29 23:54:52 +0000240 auto Obj = llvm::make_unique<DyldELFObject<ELFType<support::big, 8, true>>>(
Rafael Espindola48af1c22014-08-19 18:44:46 +0000241 Buf, ec);
David Blaikie7a1e7752014-04-29 21:52:46 +0000242 return new ELFObjectImage<ELFType<support::big, 8, true>>(Buffer, std::move(Obj));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000243 } else if (Ident.first == ELF::ELFCLASS64 &&
244 Ident.second == ELF::ELFDATA2LSB) {
David Blaikie7a1e7752014-04-29 21:52:46 +0000245 auto Obj =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000246 llvm::make_unique<DyldELFObject<ELFType<support::little, 8, true>>>(Buf,
247 ec);
David Blaikie7a1e7752014-04-29 21:52:46 +0000248 return new ELFObjectImage<ELFType<support::little, 8, true>>(Buffer, std::move(Obj));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000249 } else
Preston Gurdcc31af92012-04-16 22:12:58 +0000250 llvm_unreachable("Unexpected ELF format");
251}
252
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000253RuntimeDyldELF::~RuntimeDyldELF() {}
Eli Bendersky4c647582012-01-16 08:56:09 +0000254
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000255void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000256 uint64_t Offset, uint64_t Value,
257 uint32_t Type, int64_t Addend,
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000258 uint64_t SymOffset) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000259 switch (Type) {
260 default:
261 llvm_unreachable("Relocation type not implemented yet!");
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000262 break;
Eli Bendersky4c647582012-01-16 08:56:09 +0000263 case ELF::R_X86_64_64: {
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000264 support::ulittle64_t::ref(Section.Address + Offset) = Value + Addend;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000265 DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000266 << format("%p\n", Section.Address + Offset));
Eli Bendersky4c647582012-01-16 08:56:09 +0000267 break;
268 }
269 case ELF::R_X86_64_32:
270 case ELF::R_X86_64_32S: {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000271 Value += Addend;
Andrew Kaylor8e87a752012-07-27 20:30:12 +0000272 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000273 (Type == ELF::R_X86_64_32S &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000274 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
Eli Bendersky4c647582012-01-16 08:56:09 +0000275 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000276 support::ulittle32_t::ref(Section.Address + Offset) = TruncatedAddr;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000277 DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at "
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000278 << format("%p\n", Section.Address + Offset));
Eli Bendersky4c647582012-01-16 08:56:09 +0000279 break;
280 }
Andrew Kaylor4612fed2013-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);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000285 uint64_t FinalAddress = Section.LoadAddress + Offset;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000286 // The processRelocationRef method combines the symbol offset and the addend
287 // and in most cases that's what we want. For this relocation type, we need
288 // the raw addend, so we subtract the symbol offset to get it.
289 int64_t RealOffset = GOTAddr + Addend - SymOffset - FinalAddress;
290 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
291 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000292 support::ulittle32_t::ref(Section.Address + Offset) = TruncOffset;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000293 break;
294 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000295 case ELF::R_X86_64_PC32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000296 // Get the placeholder value from the generated object since
297 // a previous relocation attempt may have overwritten the loaded version
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000298 support::ulittle32_t::ref Placeholder(
299 (void *)(Section.ObjAddress + Offset));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000300 uint64_t FinalAddress = Section.LoadAddress + Offset;
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000301 int64_t RealOffset = Placeholder + Value + Addend - FinalAddress;
Andrew Kaylor8e87a752012-07-27 20:30:12 +0000302 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000303 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000304 support::ulittle32_t::ref(Section.Address + Offset) = TruncOffset;
Eli Bendersky4c647582012-01-16 08:56:09 +0000305 break;
306 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000307 case ELF::R_X86_64_PC64: {
308 // Get the placeholder value from the generated object since
309 // a previous relocation attempt may have overwritten the loaded version
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000310 support::ulittle64_t::ref Placeholder(
311 (void *)(Section.ObjAddress + Offset));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000312 uint64_t FinalAddress = Section.LoadAddress + Offset;
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000313 support::ulittle64_t::ref(Section.Address + Offset) =
314 Placeholder + Value + Addend - FinalAddress;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000315 break;
316 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000317 }
318}
319
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000320void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000321 uint64_t Offset, uint32_t Value,
322 uint32_t Type, int32_t Addend) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000323 switch (Type) {
Eli Bendersky4c647582012-01-16 08:56:09 +0000324 case ELF::R_386_32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000325 // Get the placeholder value from the generated object since
326 // a previous relocation attempt may have overwritten the loaded version
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000327 support::ulittle32_t::ref Placeholder(
328 (void *)(Section.ObjAddress + Offset));
329 support::ulittle32_t::ref(Section.Address + Offset) =
330 Placeholder + Value + Addend;
Eli Bendersky4c647582012-01-16 08:56:09 +0000331 break;
332 }
333 case ELF::R_386_PC32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000334 // Get the placeholder value from the generated object since
335 // a previous relocation attempt may have overwritten the loaded version
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000336 support::ulittle32_t::ref Placeholder(
337 (void *)(Section.ObjAddress + Offset));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000338 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000339 uint32_t RealOffset = Placeholder + Value + Addend - FinalAddress;
340 support::ulittle32_t::ref(Section.Address + Offset) = RealOffset;
Eli Bendersky4c647582012-01-16 08:56:09 +0000341 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000342 }
343 default:
344 // There are other relocation types, but it appears these are the
345 // only ones currently used by the LLVM ELF object writer
346 llvm_unreachable("Relocation type not implemented yet!");
347 break;
Eli Bendersky4c647582012-01-16 08:56:09 +0000348 }
349}
350
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000351void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000352 uint64_t Offset, uint64_t Value,
353 uint32_t Type, int64_t Addend) {
354 uint32_t *TargetPtr = reinterpret_cast<uint32_t *>(Section.Address + Offset);
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000355 uint64_t FinalAddress = Section.LoadAddress + Offset;
356
357 DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
358 << format("%llx", Section.Address + Offset)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000359 << " FinalAddress: 0x" << format("%llx", FinalAddress)
360 << " Value: 0x" << format("%llx", Value) << " Type: 0x"
361 << format("%x", Type) << " Addend: 0x" << format("%llx", Addend)
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000362 << "\n");
363
364 switch (Type) {
365 default:
366 llvm_unreachable("Relocation type not implemented yet!");
367 break;
Tim Northoverb23d8db2013-05-04 20:14:14 +0000368 case ELF::R_AARCH64_ABS64: {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000369 uint64_t *TargetPtr =
370 reinterpret_cast<uint64_t *>(Section.Address + Offset);
Tim Northoverb23d8db2013-05-04 20:14:14 +0000371 *TargetPtr = Value + Addend;
372 break;
373 }
Tim Northover5959ea32013-05-19 15:39:03 +0000374 case ELF::R_AARCH64_PREL32: {
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000375 uint64_t Result = Value + Addend - FinalAddress;
Michael J. Spencer126973b2013-08-08 22:27:13 +0000376 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000377 static_cast<int64_t>(Result) <= UINT32_MAX);
378 *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU);
379 break;
380 }
Tim Northover37cde972013-05-04 20:14:09 +0000381 case ELF::R_AARCH64_CALL26: // fallthrough
382 case ELF::R_AARCH64_JUMP26: {
383 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
384 // calculation.
385 uint64_t BranchImm = Value + Addend - FinalAddress;
386
387 // "Check that -2^27 <= result < 2^27".
Michael J. Spencer126973b2013-08-08 22:27:13 +0000388 assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) &&
Tim Northover37cde972013-05-04 20:14:09 +0000389 static_cast<int64_t>(BranchImm) < (1LL << 27));
Tim Northover5959ea32013-05-19 15:39:03 +0000390
391 // AArch64 code is emitted with .rela relocations. The data already in any
392 // bits affected by the relocation on entry is garbage.
393 *TargetPtr &= 0xfc000000U;
Tim Northover37cde972013-05-04 20:14:09 +0000394 // Immediate goes in bits 25:0 of B and BL.
395 *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2;
396 break;
397 }
Tim Northover4d01c1e2013-05-04 20:14:04 +0000398 case ELF::R_AARCH64_MOVW_UABS_G3: {
399 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000400
401 // AArch64 code is emitted with .rela relocations. The data already in any
402 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000403 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000404 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
405 *TargetPtr |= Result >> (48 - 5);
Tim Northover8625fd82013-07-01 19:23:10 +0000406 // Shift must be "lsl #48", in bits 22:21
407 assert((*TargetPtr >> 21 & 0x3) == 3 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000408 break;
409 }
410 case ELF::R_AARCH64_MOVW_UABS_G2_NC: {
411 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000412
Tim Northover5959ea32013-05-19 15:39:03 +0000413 // AArch64 code is emitted with .rela relocations. The data already in any
414 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000415 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000416 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
417 *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
Tim Northover8625fd82013-07-01 19:23:10 +0000418 // Shift must be "lsl #32", in bits 22:21
419 assert((*TargetPtr >> 21 & 0x3) == 2 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000420 break;
421 }
422 case ELF::R_AARCH64_MOVW_UABS_G1_NC: {
423 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000424
425 // AArch64 code is emitted with .rela relocations. The data already in any
426 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000427 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000428 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
429 *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
Tim Northover8625fd82013-07-01 19:23:10 +0000430 // Shift must be "lsl #16", in bits 22:2
431 assert((*TargetPtr >> 21 & 0x3) == 1 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000432 break;
433 }
434 case ELF::R_AARCH64_MOVW_UABS_G0_NC: {
435 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000436
437 // AArch64 code is emitted with .rela relocations. The data already in any
438 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000439 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000440 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
441 *TargetPtr |= ((Result & 0xffffU) << 5);
Tim Northover8625fd82013-07-01 19:23:10 +0000442 // Shift must be "lsl #0", in bits 22:21.
443 assert((*TargetPtr >> 21 & 0x3) == 0 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000444 break;
445 }
Bradley Smith9d808492014-02-11 12:59:09 +0000446 case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
447 // Operation: Page(S+A) - Page(P)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000448 uint64_t Result =
449 ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
Bradley Smith9d808492014-02-11 12:59:09 +0000450
451 // Check that -2^32 <= X < 2^32
452 assert(static_cast<int64_t>(Result) >= (-1LL << 32) &&
453 static_cast<int64_t>(Result) < (1LL << 32) &&
454 "overflow check failed for relocation");
455
456 // AArch64 code is emitted with .rela relocations. The data already in any
457 // bits affected by the relocation on entry is garbage.
458 *TargetPtr &= 0x9f00001fU;
459 // Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken
460 // from bits 32:12 of X.
461 *TargetPtr |= ((Result & 0x3000U) << (29 - 12));
462 *TargetPtr |= ((Result & 0x1ffffc000ULL) >> (14 - 5));
463 break;
464 }
465 case ELF::R_AARCH64_LDST32_ABS_LO12_NC: {
466 // Operation: S + A
467 uint64_t Result = Value + Addend;
468
469 // AArch64 code is emitted with .rela relocations. The data already in any
470 // bits affected by the relocation on entry is garbage.
471 *TargetPtr &= 0xffc003ffU;
472 // Immediate goes in bits 21:10 of LD/ST instruction, taken
473 // from bits 11:2 of X
474 *TargetPtr |= ((Result & 0xffc) << (10 - 2));
475 break;
476 }
477 case ELF::R_AARCH64_LDST64_ABS_LO12_NC: {
478 // Operation: S + A
479 uint64_t Result = Value + Addend;
480
481 // AArch64 code is emitted with .rela relocations. The data already in any
482 // bits affected by the relocation on entry is garbage.
483 *TargetPtr &= 0xffc003ffU;
484 // Immediate goes in bits 21:10 of LD/ST instruction, taken
485 // from bits 11:3 of X
486 *TargetPtr |= ((Result & 0xff8) << (10 - 3));
487 break;
488 }
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000489 }
490}
491
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000492void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000493 uint64_t Offset, uint32_t Value,
494 uint32_t Type, int32_t Addend) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000495 // TODO: Add Thumb relocations.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000496 uint32_t *Placeholder =
497 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
498 uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset);
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000499 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000500 Value += Addend;
501
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000502 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
503 << Section.Address + Offset
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000504 << " FinalAddress: " << format("%p", FinalAddress) << " Value: "
505 << format("%x", Value) << " Type: " << format("%x", Type)
506 << " Addend: " << format("%x", Addend) << "\n");
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000507
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000508 switch (Type) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000509 default:
510 llvm_unreachable("Not implemented relocation type!");
511
Renato Golin8cea6e82014-01-29 11:50:56 +0000512 case ELF::R_ARM_NONE:
513 break;
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000514 // Write a 32bit value to relocation address, taking into account the
Tim Northover471cbb72012-10-03 16:29:42 +0000515 // implicit addend encoded in the target.
Renato Golin8cea6e82014-01-29 11:50:56 +0000516 case ELF::R_ARM_PREL31:
Tim Northover3b684d82013-05-28 19:48:19 +0000517 case ELF::R_ARM_TARGET1:
518 case ELF::R_ARM_ABS32:
519 *TargetPtr = *Placeholder + Value;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000520 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000521 // Write first 16 bit of 32 bit value to the mov instruction.
522 // Last 4 bit should be shifted.
Tim Northover3b684d82013-05-28 19:48:19 +0000523 case ELF::R_ARM_MOVW_ABS_NC:
Tim Northover471cbb72012-10-03 16:29:42 +0000524 // We are not expecting any other addend in the relocation address.
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000525 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
Tim Northover471cbb72012-10-03 16:29:42 +0000526 // non-contiguous fields.
Tim Northover3b684d82013-05-28 19:48:19 +0000527 assert((*Placeholder & 0x000F0FFF) == 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000528 Value = Value & 0xFFFF;
Tim Northover3b684d82013-05-28 19:48:19 +0000529 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000530 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
531 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000532 // Write last 16 bit of 32 bit value to the mov instruction.
533 // Last 4 bit should be shifted.
Tim Northover3b684d82013-05-28 19:48:19 +0000534 case ELF::R_ARM_MOVT_ABS:
Tim Northover471cbb72012-10-03 16:29:42 +0000535 // We are not expecting any other addend in the relocation address.
536 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
Tim Northover3b684d82013-05-28 19:48:19 +0000537 assert((*Placeholder & 0x000F0FFF) == 0);
538
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000539 Value = (Value >> 16) & 0xFFFF;
Tim Northover3b684d82013-05-28 19:48:19 +0000540 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000541 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
542 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000543 // Write 24 bit relative value to the branch instruction.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000544 case ELF::R_ARM_PC24: // Fall through.
545 case ELF::R_ARM_CALL: // Fall through.
Tim Northover3b684d82013-05-28 19:48:19 +0000546 case ELF::R_ARM_JUMP24: {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000547 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
548 RelValue = (RelValue & 0x03FFFFFC) >> 2;
Tim Northover3b684d82013-05-28 19:48:19 +0000549 assert((*TargetPtr & 0xFFFFFF) == 0xFFFFFE);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000550 *TargetPtr &= 0xFF000000;
551 *TargetPtr |= RelValue;
552 break;
553 }
Tim Northover3b684d82013-05-28 19:48:19 +0000554 case ELF::R_ARM_PRIVATE_0:
555 // This relocation is reserved by the ARM ELF ABI for internal use. We
556 // appropriate it here to act as an R_ARM_ABS32 without any addend for use
557 // in the stubs created during JIT (which can't put an addend into the
558 // original object file).
559 *TargetPtr = Value;
560 break;
561 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000562}
563
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000564void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000565 uint64_t Offset, uint32_t Value,
566 uint32_t Type, int32_t Addend) {
567 uint32_t *Placeholder =
568 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
569 uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000570 Value += Addend;
571
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000572 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000573 << Section.Address + Offset << " FinalAddress: "
574 << format("%p", Section.LoadAddress + Offset) << " Value: "
575 << format("%x", Value) << " Type: " << format("%x", Type)
576 << " Addend: " << format("%x", Addend) << "\n");
Akira Hatanaka111174b2012-08-17 21:28:04 +0000577
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000578 switch (Type) {
Akira Hatanaka111174b2012-08-17 21:28:04 +0000579 default:
580 llvm_unreachable("Not implemented relocation type!");
581 break;
582 case ELF::R_MIPS_32:
Akira Hatanaka2e236242013-07-24 01:58:40 +0000583 *TargetPtr = Value + (*Placeholder);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000584 break;
585 case ELF::R_MIPS_26:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000586 *TargetPtr = ((*Placeholder) & 0xfc000000) | ((Value & 0x0fffffff) >> 2);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000587 break;
588 case ELF::R_MIPS_HI16:
589 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
Akira Hatanaka2e236242013-07-24 01:58:40 +0000590 Value += ((*Placeholder) & 0x0000ffff) << 16;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000591 *TargetPtr =
592 ((*Placeholder) & 0xffff0000) | (((Value + 0x8000) >> 16) & 0xffff);
Akira Hatanaka2e236242013-07-24 01:58:40 +0000593 break;
594 case ELF::R_MIPS_LO16:
595 Value += ((*Placeholder) & 0x0000ffff);
596 *TargetPtr = ((*Placeholder) & 0xffff0000) | (Value & 0xffff);
597 break;
598 case ELF::R_MIPS_UNUSED1:
599 // Similar to ELF::R_ARM_PRIVATE_0, R_MIPS_UNUSED1 and R_MIPS_UNUSED2
600 // are used for internal JIT purpose. These relocations are similar to
601 // R_MIPS_HI16 and R_MIPS_LO16, but they do not take any addend into
602 // account.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000603 *TargetPtr =
604 ((*TargetPtr) & 0xffff0000) | (((Value + 0x8000) >> 16) & 0xffff);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000605 break;
Akira Hatanaka2e236242013-07-24 01:58:40 +0000606 case ELF::R_MIPS_UNUSED2:
Akira Hatanaka111174b2012-08-17 21:28:04 +0000607 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
608 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000609 }
Akira Hatanaka111174b2012-08-17 21:28:04 +0000610}
611
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000612// Return the .TOC. section and offset.
613void RuntimeDyldELF::findPPC64TOCSection(ObjectImage &Obj,
614 ObjSectionToIDMap &LocalSections,
615 RelocationValueRef &Rel) {
616 // Set a default SectionID in case we do not find a TOC section below.
617 // This may happen for references to TOC base base (sym@toc, .odp
618 // relocation) without a .toc directive. In this case just use the
619 // first section (which is usually the .odp) since the code won't
620 // reference the .toc base directly.
621 Rel.SymbolName = NULL;
622 Rel.SectionID = 0;
623
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000624 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
625 // order. The TOC starts where the first of these sections starts.
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000626 for (section_iterator si = Obj.begin_sections(), se = Obj.end_sections();
627 si != se; ++si) {
628
629 StringRef SectionName;
630 check(si->getName(SectionName));
631
632 if (SectionName == ".got"
633 || SectionName == ".toc"
634 || SectionName == ".tocbss"
635 || SectionName == ".plt") {
636 Rel.SectionID = findOrEmitSection(Obj, *si, false, LocalSections);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000637 break;
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000638 }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000639 }
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000640
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000641 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
642 // thus permitting a full 64 Kbytes segment.
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000643 Rel.Addend = 0x8000;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000644}
645
646// Returns the sections and offset associated with the ODP entry referenced
647// by Symbol.
648void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
649 ObjSectionToIDMap &LocalSections,
650 RelocationValueRef &Rel) {
651 // Get the ELF symbol value (st_value) to compare with Relocation offset in
652 // .opd entries
Rafael Espindola5e812af2014-01-30 02:49:50 +0000653 for (section_iterator si = Obj.begin_sections(), se = Obj.end_sections();
654 si != se; ++si) {
Rafael Espindolaa61f1e92013-06-03 19:37:34 +0000655 section_iterator RelSecI = si->getRelocatedSection();
656 if (RelSecI == Obj.end_sections())
657 continue;
658
659 StringRef RelSectionName;
660 check(RelSecI->getName(RelSectionName));
661 if (RelSectionName != ".opd")
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000662 continue;
663
Rafael Espindolab5155a52014-02-10 20:24:04 +0000664 for (relocation_iterator i = si->relocation_begin(),
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000665 e = si->relocation_end();
666 i != e;) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000667 // The R_PPC64_ADDR64 relocation indicates the first field
668 // of a .opd entry
669 uint64_t TypeFunc;
670 check(i->getType(TypeFunc));
671 if (TypeFunc != ELF::R_PPC64_ADDR64) {
Rafael Espindola5e812af2014-01-30 02:49:50 +0000672 ++i;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000673 continue;
674 }
675
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000676 uint64_t TargetSymbolOffset;
Rafael Espindola806f0062013-06-05 01:33:53 +0000677 symbol_iterator TargetSymbol = i->getSymbol();
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000678 check(i->getOffset(TargetSymbolOffset));
Rafael Espindola0d15f732013-05-09 03:39:05 +0000679 int64_t Addend;
680 check(getELFRelocationAddend(*i, Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000681
Rafael Espindola5e812af2014-01-30 02:49:50 +0000682 ++i;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000683 if (i == e)
684 break;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000685
686 // Just check if following relocation is a R_PPC64_TOC
687 uint64_t TypeTOC;
688 check(i->getType(TypeTOC));
689 if (TypeTOC != ELF::R_PPC64_TOC)
690 continue;
691
692 // Finally compares the Symbol value and the target symbol offset
693 // to check if this .opd entry refers to the symbol the relocation
694 // points to.
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000695 if (Rel.Addend != (int64_t)TargetSymbolOffset)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000696 continue;
697
698 section_iterator tsi(Obj.end_sections());
Rafael Espindola806f0062013-06-05 01:33:53 +0000699 check(TargetSymbol->getSection(tsi));
Lang Hames9b2dc932014-02-18 21:46:39 +0000700 bool IsCode = false;
701 tsi->isText(IsCode);
702 Rel.SectionID = findOrEmitSection(Obj, (*tsi), IsCode, LocalSections);
Rafael Espindola0d15f732013-05-09 03:39:05 +0000703 Rel.Addend = (intptr_t)Addend;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000704 return;
705 }
706 }
707 llvm_unreachable("Attempting to get address of ODP entry!");
708}
709
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000710// Relocation masks following the #lo(value), #hi(value), #ha(value),
711// #higher(value), #highera(value), #highest(value), and #highesta(value)
712// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
713// document.
714
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000715static inline uint16_t applyPPClo(uint64_t value) { return value & 0xffff; }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000716
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000717static inline uint16_t applyPPChi(uint64_t value) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000718 return (value >> 16) & 0xffff;
719}
720
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000721static inline uint16_t applyPPCha (uint64_t value) {
722 return ((value + 0x8000) >> 16) & 0xffff;
723}
724
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000725static inline uint16_t applyPPChigher(uint64_t value) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000726 return (value >> 32) & 0xffff;
727}
728
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000729static inline uint16_t applyPPChighera (uint64_t value) {
730 return ((value + 0x8000) >> 32) & 0xffff;
731}
732
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000733static inline uint16_t applyPPChighest(uint64_t value) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000734 return (value >> 48) & 0xffff;
735}
736
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000737static inline uint16_t applyPPChighesta (uint64_t value) {
738 return ((value + 0x8000) >> 48) & 0xffff;
739}
740
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000741void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000742 uint64_t Offset, uint64_t Value,
743 uint32_t Type, int64_t Addend) {
744 uint8_t *LocalAddress = Section.Address + Offset;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000745 switch (Type) {
746 default:
747 llvm_unreachable("Relocation type not implemented yet!");
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000748 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000749 case ELF::R_PPC64_ADDR16:
750 writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
751 break;
752 case ELF::R_PPC64_ADDR16_DS:
753 writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
754 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000755 case ELF::R_PPC64_ADDR16_LO:
756 writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000757 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000758 case ELF::R_PPC64_ADDR16_LO_DS:
759 writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
760 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000761 case ELF::R_PPC64_ADDR16_HI:
762 writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000763 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000764 case ELF::R_PPC64_ADDR16_HA:
765 writeInt16BE(LocalAddress, applyPPCha(Value + Addend));
766 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000767 case ELF::R_PPC64_ADDR16_HIGHER:
768 writeInt16BE(LocalAddress, applyPPChigher(Value + Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000769 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000770 case ELF::R_PPC64_ADDR16_HIGHERA:
771 writeInt16BE(LocalAddress, applyPPChighera(Value + Addend));
772 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000773 case ELF::R_PPC64_ADDR16_HIGHEST:
774 writeInt16BE(LocalAddress, applyPPChighest(Value + Addend));
775 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000776 case ELF::R_PPC64_ADDR16_HIGHESTA:
777 writeInt16BE(LocalAddress, applyPPChighesta(Value + Addend));
778 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000779 case ELF::R_PPC64_ADDR14: {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000780 assert(((Value + Addend) & 3) == 0);
781 // Preserve the AA/LK bits in the branch instruction
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000782 uint8_t aalk = *(LocalAddress + 3);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000783 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
784 } break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000785 case ELF::R_PPC64_REL16_LO: {
786 uint64_t FinalAddress = (Section.LoadAddress + Offset);
787 uint64_t Delta = Value - FinalAddress + Addend;
788 writeInt16BE(LocalAddress, applyPPClo(Delta));
789 } break;
790 case ELF::R_PPC64_REL16_HI: {
791 uint64_t FinalAddress = (Section.LoadAddress + Offset);
792 uint64_t Delta = Value - FinalAddress + Addend;
793 writeInt16BE(LocalAddress, applyPPChi(Delta));
794 } break;
795 case ELF::R_PPC64_REL16_HA: {
796 uint64_t FinalAddress = (Section.LoadAddress + Offset);
797 uint64_t Delta = Value - FinalAddress + Addend;
798 writeInt16BE(LocalAddress, applyPPCha(Delta));
799 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000800 case ELF::R_PPC64_ADDR32: {
Adhemerval Zanella9b0b7812013-01-04 19:08:13 +0000801 int32_t Result = static_cast<int32_t>(Value + Addend);
802 if (SignExtend32<32>(Result) != Result)
Adhemerval Zanella1ae22482013-01-09 17:08:15 +0000803 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
Adhemerval Zanella9b0b7812013-01-04 19:08:13 +0000804 writeInt32BE(LocalAddress, Result);
805 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000806 case ELF::R_PPC64_REL24: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000807 uint64_t FinalAddress = (Section.LoadAddress + Offset);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000808 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
809 if (SignExtend32<24>(delta) != delta)
810 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
811 // Generates a 'bl <address>' instruction
812 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
813 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000814 case ELF::R_PPC64_REL32: {
Adhemerval Zanella1ae22482013-01-09 17:08:15 +0000815 uint64_t FinalAddress = (Section.LoadAddress + Offset);
816 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
817 if (SignExtend32<32>(delta) != delta)
818 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
819 writeInt32BE(LocalAddress, delta);
820 } break;
Adhemerval Zanellae8bd03d2013-05-06 17:21:23 +0000821 case ELF::R_PPC64_REL64: {
822 uint64_t FinalAddress = (Section.LoadAddress + Offset);
823 uint64_t Delta = Value - FinalAddress + Addend;
824 writeInt64BE(LocalAddress, Delta);
825 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000826 case ELF::R_PPC64_ADDR64:
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000827 writeInt64BE(LocalAddress, Value + Addend);
828 break;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000829 }
830}
831
Richard Sandifordca044082013-05-03 14:15:35 +0000832void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000833 uint64_t Offset, uint64_t Value,
834 uint32_t Type, int64_t Addend) {
Richard Sandifordca044082013-05-03 14:15:35 +0000835 uint8_t *LocalAddress = Section.Address + Offset;
836 switch (Type) {
837 default:
838 llvm_unreachable("Relocation type not implemented yet!");
839 break;
840 case ELF::R_390_PC16DBL:
841 case ELF::R_390_PLT16DBL: {
842 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
843 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
844 writeInt16BE(LocalAddress, Delta / 2);
845 break;
846 }
847 case ELF::R_390_PC32DBL:
848 case ELF::R_390_PLT32DBL: {
849 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
850 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
851 writeInt32BE(LocalAddress, Delta / 2);
852 break;
853 }
854 case ELF::R_390_PC32: {
855 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
856 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
857 writeInt32BE(LocalAddress, Delta);
858 break;
859 }
860 case ELF::R_390_64:
861 writeInt64BE(LocalAddress, Value + Addend);
862 break;
863 }
864}
865
Andrew Kaylor5f3a9982013-08-19 19:38:06 +0000866// The target location for the relocation is described by RE.SectionID and
867// RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
868// SectionEntry has three members describing its location.
869// SectionEntry::Address is the address at which the section has been loaded
870// into memory in the current (host) process. SectionEntry::LoadAddress is the
871// address that the section will have in the target process.
872// SectionEntry::ObjAddress is the address of the bits for this section in the
873// original emitted object image (also in the current address space).
874//
875// Relocations will be applied as if the section were loaded at
876// SectionEntry::LoadAddress, but they will be applied at an address based
877// on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
878// Target memory contents if they are required for value calculations.
879//
880// The Value parameter here is the load address of the symbol for the
881// relocation to be applied. For relocations which refer to symbols in the
882// current object Value will be the LoadAddress of the section in which
883// the symbol resides (RE.Addend provides additional information about the
884// symbol location). For external symbols, Value will be the address of the
885// symbol in the target address space.
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000886void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
Andrew Kaylor5f3a9982013-08-19 19:38:06 +0000887 uint64_t Value) {
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000888 const SectionEntry &Section = Sections[RE.SectionID];
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000889 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
890 RE.SymOffset);
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000891}
892
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000893void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000894 uint64_t Offset, uint64_t Value,
895 uint32_t Type, int64_t Addend,
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000896 uint64_t SymOffset) {
Eli Bendersky4c647582012-01-16 08:56:09 +0000897 switch (Arch) {
898 case Triple::x86_64:
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000899 resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
Eli Bendersky4c647582012-01-16 08:56:09 +0000900 break;
901 case Triple::x86:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000902 resolveX86Relocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000903 (uint32_t)(Addend & 0xffffffffL));
Eli Bendersky4c647582012-01-16 08:56:09 +0000904 break;
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000905 case Triple::aarch64:
Christian Pirker99974c72014-03-26 14:57:32 +0000906 case Triple::aarch64_be:
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000907 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
908 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000909 case Triple::arm: // Fall through.
Christian Pirker2a111602014-03-28 14:35:30 +0000910 case Triple::armeb:
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000911 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000912 case Triple::thumbeb:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000913 resolveARMRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000914 (uint32_t)(Addend & 0xffffffffL));
Eli Bendersky4c647582012-01-16 08:56:09 +0000915 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000916 case Triple::mips: // Fall through.
Akira Hatanaka111174b2012-08-17 21:28:04 +0000917 case Triple::mipsel:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000918 resolveMIPSRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL),
919 Type, (uint32_t)(Addend & 0xffffffffL));
Akira Hatanaka111174b2012-08-17 21:28:04 +0000920 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000921 case Triple::ppc64: // Fall through.
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000922 case Triple::ppc64le:
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000923 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000924 break;
Richard Sandifordca044082013-05-03 14:15:35 +0000925 case Triple::systemz:
926 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
927 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000928 default:
929 llvm_unreachable("Unsupported CPU type!");
Eli Bendersky4c647582012-01-16 08:56:09 +0000930 }
931}
932
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000933relocation_iterator RuntimeDyldELF::processRelocationRef(
934 unsigned SectionID, relocation_iterator RelI, ObjectImage &Obj,
935 ObjSectionToIDMap &ObjSectionToID, const SymbolTableMap &Symbols,
936 StubMap &Stubs) {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000937 uint64_t RelType;
Juergen Ributzka046709f2014-03-21 07:26:41 +0000938 Check(RelI->getType(RelType));
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000939 int64_t Addend;
Juergen Ributzka046709f2014-03-21 07:26:41 +0000940 Check(getELFRelocationAddend(*RelI, Addend));
941 symbol_iterator Symbol = RelI->getSymbol();
Eli Bendersky667b8792012-05-01 10:41:12 +0000942
943 // Obtain the symbol name which is referenced in the relocation
944 StringRef TargetName;
Rafael Espindola75954472013-06-05 02:55:01 +0000945 if (Symbol != Obj.end_symbols())
946 Symbol->getName(TargetName);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000947 DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend
948 << " TargetName: " << TargetName << "\n");
Eli Bendersky667b8792012-05-01 10:41:12 +0000949 RelocationValueRef Value;
950 // First search for the symbol in the local symbol table
Rafael Espindola75954472013-06-05 02:55:01 +0000951 SymbolTableMap::const_iterator lsi = Symbols.end();
952 SymbolRef::Type SymType = SymbolRef::ST_Unknown;
953 if (Symbol != Obj.end_symbols()) {
954 lsi = Symbols.find(TargetName.data());
955 Symbol->getType(SymType);
956 }
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000957 if (lsi != Symbols.end()) {
958 Value.SectionID = lsi->second.first;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000959 Value.Offset = lsi->second.second;
Ulrich Weigand78e97652013-04-05 13:29:04 +0000960 Value.Addend = lsi->second.second + Addend;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000961 } else {
Eli Bendersky667b8792012-05-01 10:41:12 +0000962 // Search for the symbol in the global symbol table
Rafael Espindola75954472013-06-05 02:55:01 +0000963 SymbolTableMap::const_iterator gsi = GlobalSymbolTable.end();
964 if (Symbol != Obj.end_symbols())
965 gsi = GlobalSymbolTable.find(TargetName.data());
Eli Benderskyfc079082012-05-01 06:58:59 +0000966 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000967 Value.SectionID = gsi->second.first;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000968 Value.Offset = gsi->second.second;
Ulrich Weigand78e97652013-04-05 13:29:04 +0000969 Value.Addend = gsi->second.second + Addend;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000970 } else {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000971 switch (SymType) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000972 case SymbolRef::ST_Debug: {
973 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
974 // and can be changed by another developers. Maybe best way is add
975 // a new symbol type ST_Section to SymbolRef and use it.
976 section_iterator si(Obj.end_sections());
977 Symbol->getSection(si);
978 if (si == Obj.end_sections())
979 llvm_unreachable("Symbol section not found, bad object file format!");
980 DEBUG(dbgs() << "\t\tThis is section symbol\n");
981 // Default to 'true' in case isText fails (though it never does).
982 bool isCode = true;
983 si->isText(isCode);
984 Value.SectionID = findOrEmitSection(Obj, (*si), isCode, ObjSectionToID);
985 Value.Addend = Addend;
986 break;
987 }
988 case SymbolRef::ST_Data:
989 case SymbolRef::ST_Unknown: {
990 Value.SymbolName = TargetName.data();
991 Value.Addend = Addend;
Richard Mittonad6d3492013-08-16 18:54:26 +0000992
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000993 // Absolute relocations will have a zero symbol ID (STN_UNDEF), which
994 // will manifest here as a NULL symbol name.
995 // We can set this as a valid (but empty) symbol name, and rely
996 // on addRelocationForSymbol to handle this.
997 if (!Value.SymbolName)
998 Value.SymbolName = "";
999 break;
1000 }
1001 default:
1002 llvm_unreachable("Unresolved symbol type!");
1003 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001004 }
1005 }
Eli Bendersky4c647582012-01-16 08:56:09 +00001006 }
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001007 uint64_t Offset;
Juergen Ributzka046709f2014-03-21 07:26:41 +00001008 Check(RelI->getOffset(Offset));
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001009
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001010 DEBUG(dbgs() << "\t\tSectionID: " << SectionID << " Offset: " << Offset
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001011 << "\n");
Tim Northovere19bed72014-07-23 12:32:47 +00001012 if ((Arch == Triple::aarch64 || Arch == Triple::aarch64_be) &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001013 (RelType == ELF::R_AARCH64_CALL26 || RelType == ELF::R_AARCH64_JUMP26)) {
Tim Northover37cde972013-05-04 20:14:09 +00001014 // This is an AArch64 branch relocation, need to use a stub function.
1015 DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
1016 SectionEntry &Section = Sections[SectionID];
1017
1018 // Look for an existing stub.
1019 StubMap::const_iterator i = Stubs.find(Value);
1020 if (i != Stubs.end()) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001021 resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second,
1022 RelType, 0);
Tim Northover37cde972013-05-04 20:14:09 +00001023 DEBUG(dbgs() << " Stub function found\n");
1024 } else {
1025 // Create a new stub function.
1026 DEBUG(dbgs() << " Create a new stub function\n");
1027 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001028 uint8_t *StubTargetAddr =
1029 createStubFunction(Section.Address + Section.StubOffset);
Tim Northover37cde972013-05-04 20:14:09 +00001030
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001031 RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section.Address,
Tim Northover37cde972013-05-04 20:14:09 +00001032 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001033 RelocationEntry REmovk_g2(SectionID, StubTargetAddr - Section.Address + 4,
Tim Northover37cde972013-05-04 20:14:09 +00001034 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001035 RelocationEntry REmovk_g1(SectionID, StubTargetAddr - Section.Address + 8,
Tim Northover37cde972013-05-04 20:14:09 +00001036 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
1037 RelocationEntry REmovk_g0(SectionID,
1038 StubTargetAddr - Section.Address + 12,
1039 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
1040
1041 if (Value.SymbolName) {
1042 addRelocationForSymbol(REmovz_g3, Value.SymbolName);
1043 addRelocationForSymbol(REmovk_g2, Value.SymbolName);
1044 addRelocationForSymbol(REmovk_g1, Value.SymbolName);
1045 addRelocationForSymbol(REmovk_g0, Value.SymbolName);
1046 } else {
1047 addRelocationForSection(REmovz_g3, Value.SectionID);
1048 addRelocationForSection(REmovk_g2, Value.SectionID);
1049 addRelocationForSection(REmovk_g1, Value.SectionID);
1050 addRelocationForSection(REmovk_g0, Value.SectionID);
1051 }
1052 resolveRelocation(Section, Offset,
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001053 (uint64_t)Section.Address + Section.StubOffset, RelType,
1054 0);
Tim Northover37cde972013-05-04 20:14:09 +00001055 Section.StubOffset += getMaxStubSize();
1056 }
1057 } else if (Arch == Triple::arm &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001058 (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL ||
1059 RelType == ELF::R_ARM_JUMP24)) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001060 // This is an ARM branch relocation, need to use a stub function.
1061 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001062 SectionEntry &Section = Sections[SectionID];
Eli Bendersky4c647582012-01-16 08:56:09 +00001063
Eric Christopherc33f6222012-10-23 17:19:15 +00001064 // Look for an existing stub.
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001065 StubMap::const_iterator i = Stubs.find(Value);
1066 if (i != Stubs.end()) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001067 resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second,
1068 RelType, 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001069 DEBUG(dbgs() << " Stub function found\n");
1070 } else {
1071 // Create a new stub function.
1072 DEBUG(dbgs() << " Create a new stub function\n");
1073 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001074 uint8_t *StubTargetAddr =
1075 createStubFunction(Section.Address + Section.StubOffset);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001076 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Tim Northover3b684d82013-05-28 19:48:19 +00001077 ELF::R_ARM_PRIVATE_0, Value.Addend);
Eli Bendersky667b8792012-05-01 10:41:12 +00001078 if (Value.SymbolName)
1079 addRelocationForSymbol(RE, Value.SymbolName);
1080 else
1081 addRelocationForSection(RE, Value.SectionID);
1082
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001083 resolveRelocation(Section, Offset,
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001084 (uint64_t)Section.Address + Section.StubOffset, RelType,
1085 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001086 Section.StubOffset += getMaxStubSize();
1087 }
Akira Hatanakaa667aad2012-12-03 23:12:19 +00001088 } else if ((Arch == Triple::mipsel || Arch == Triple::mips) &&
1089 RelType == ELF::R_MIPS_26) {
Akira Hatanaka111174b2012-08-17 21:28:04 +00001090 // This is an Mips branch relocation, need to use a stub function.
1091 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001092 SectionEntry &Section = Sections[SectionID];
1093 uint8_t *Target = Section.Address + Offset;
Akira Hatanaka111174b2012-08-17 21:28:04 +00001094 uint32_t *TargetAddress = (uint32_t *)Target;
1095
1096 // Extract the addend from the instruction.
1097 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
1098
1099 Value.Addend += Addend;
1100
1101 // Look up for existing stub.
1102 StubMap::const_iterator i = Stubs.find(Value);
1103 if (i != Stubs.end()) {
Petar Jovanovic45115f82013-11-19 21:56:00 +00001104 RelocationEntry RE(SectionID, Offset, RelType, i->second);
1105 addRelocationForSection(RE, SectionID);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001106 DEBUG(dbgs() << " Stub function found\n");
1107 } else {
1108 // Create a new stub function.
1109 DEBUG(dbgs() << " Create a new stub function\n");
1110 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001111 uint8_t *StubTargetAddr =
1112 createStubFunction(Section.Address + Section.StubOffset);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001113
1114 // Creating Hi and Lo relocations for the filled stub instructions.
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001115 RelocationEntry REHi(SectionID, StubTargetAddr - Section.Address,
Akira Hatanaka2e236242013-07-24 01:58:40 +00001116 ELF::R_MIPS_UNUSED1, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001117 RelocationEntry RELo(SectionID, StubTargetAddr - Section.Address + 4,
Akira Hatanaka2e236242013-07-24 01:58:40 +00001118 ELF::R_MIPS_UNUSED2, Value.Addend);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001119
1120 if (Value.SymbolName) {
1121 addRelocationForSymbol(REHi, Value.SymbolName);
1122 addRelocationForSymbol(RELo, Value.SymbolName);
1123 } else {
1124 addRelocationForSection(REHi, Value.SectionID);
1125 addRelocationForSection(RELo, Value.SectionID);
1126 }
1127
Petar Jovanovic45115f82013-11-19 21:56:00 +00001128 RelocationEntry RE(SectionID, Offset, RelType, Section.StubOffset);
1129 addRelocationForSection(RE, SectionID);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001130 Section.StubOffset += getMaxStubSize();
1131 }
Bill Schmidt0a9170d2013-07-26 01:35:43 +00001132 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001133 if (RelType == ELF::R_PPC64_REL24) {
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001134 // Determine ABI variant in use for this object.
1135 unsigned AbiVariant;
1136 Obj.getObjectFile()->getPlatformFlags(AbiVariant);
1137 AbiVariant &= ELF::EF_PPC64_ABI;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001138 // A PPC branch relocation will need a stub function if the target is
1139 // an external symbol (Symbol::ST_Unknown) or if the target address
1140 // is not within the signed 24-bits branch address.
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001141 SectionEntry &Section = Sections[SectionID];
1142 uint8_t *Target = Section.Address + Offset;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001143 bool RangeOverflow = false;
1144 if (SymType != SymbolRef::ST_Unknown) {
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001145 if (AbiVariant != 2) {
1146 // In the ELFv1 ABI, a function call may point to the .opd entry,
1147 // so the final symbol value is calculated based on the relocation
1148 // values in the .opd section.
1149 findOPDEntrySection(Obj, ObjSectionToID, Value);
1150 } else {
1151 // In the ELFv2 ABI, a function symbol may provide a local entry
1152 // point, which must be used for direct calls.
1153 uint8_t SymOther;
1154 Symbol->getOther(SymOther);
1155 Value.Addend += ELF::decodePPC64LocalEntryOffset(SymOther);
1156 }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001157 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
1158 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
1159 // If it is within 24-bits branch range, just set the branch target
1160 if (SignExtend32<24>(delta) == delta) {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001161 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001162 if (Value.SymbolName)
1163 addRelocationForSymbol(RE, Value.SymbolName);
1164 else
1165 addRelocationForSection(RE, Value.SectionID);
1166 } else {
1167 RangeOverflow = true;
1168 }
1169 }
1170 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
1171 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
1172 // larger than 24-bits.
1173 StubMap::const_iterator i = Stubs.find(Value);
1174 if (i != Stubs.end()) {
1175 // Symbol function stub already created, just relocate to it
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001176 resolveRelocation(Section, Offset,
Andrew Kaylorfb05a502012-11-02 19:45:23 +00001177 (uint64_t)Section.Address + i->second, RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001178 DEBUG(dbgs() << " Stub function found\n");
1179 } else {
1180 // Create a new stub function.
1181 DEBUG(dbgs() << " Create a new stub function\n");
1182 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001183 uint8_t *StubTargetAddr =
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001184 createStubFunction(Section.Address + Section.StubOffset,
1185 AbiVariant);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001186 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001187 ELF::R_PPC64_ADDR64, Value.Addend);
1188
1189 // Generates the 64-bits address loads as exemplified in section
Ulrich Weigand32626012014-06-20 18:17:56 +00001190 // 4.5.1 in PPC64 ELF ABI. Note that the relocations need to
1191 // apply to the low part of the instructions, so we have to update
1192 // the offset according to the target endianness.
1193 uint64_t StubRelocOffset = StubTargetAddr - Section.Address;
1194 if (!IsTargetLittleEndian)
1195 StubRelocOffset += 2;
1196
1197 RelocationEntry REhst(SectionID, StubRelocOffset + 0,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001198 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
Ulrich Weigand32626012014-06-20 18:17:56 +00001199 RelocationEntry REhr(SectionID, StubRelocOffset + 4,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001200 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
Ulrich Weigand32626012014-06-20 18:17:56 +00001201 RelocationEntry REh(SectionID, StubRelocOffset + 12,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001202 ELF::R_PPC64_ADDR16_HI, Value.Addend);
Ulrich Weigand32626012014-06-20 18:17:56 +00001203 RelocationEntry REl(SectionID, StubRelocOffset + 16,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001204 ELF::R_PPC64_ADDR16_LO, Value.Addend);
1205
1206 if (Value.SymbolName) {
1207 addRelocationForSymbol(REhst, Value.SymbolName);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001208 addRelocationForSymbol(REhr, Value.SymbolName);
1209 addRelocationForSymbol(REh, Value.SymbolName);
1210 addRelocationForSymbol(REl, Value.SymbolName);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001211 } else {
1212 addRelocationForSection(REhst, Value.SectionID);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001213 addRelocationForSection(REhr, Value.SectionID);
1214 addRelocationForSection(REh, Value.SectionID);
1215 addRelocationForSection(REl, Value.SectionID);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001216 }
1217
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001218 resolveRelocation(Section, Offset,
Andrew Kaylorfb05a502012-11-02 19:45:23 +00001219 (uint64_t)Section.Address + Section.StubOffset,
1220 RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001221 Section.StubOffset += getMaxStubSize();
1222 }
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001223 if (SymType == SymbolRef::ST_Unknown) {
Ulrich Weigandfa84ac92014-03-11 15:26:27 +00001224 // Restore the TOC for external calls
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001225 if (AbiVariant == 2)
1226 writeInt32BE(Target + 4, 0xE8410018); // ld r2,28(r1)
1227 else
1228 writeInt32BE(Target + 4, 0xE8410028); // ld r2,40(r1)
1229 }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001230 }
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +00001231 } else if (RelType == ELF::R_PPC64_TOC16 ||
1232 RelType == ELF::R_PPC64_TOC16_DS ||
1233 RelType == ELF::R_PPC64_TOC16_LO ||
1234 RelType == ELF::R_PPC64_TOC16_LO_DS ||
1235 RelType == ELF::R_PPC64_TOC16_HI ||
1236 RelType == ELF::R_PPC64_TOC16_HA) {
1237 // These relocations are supposed to subtract the TOC address from
1238 // the final value. This does not fit cleanly into the RuntimeDyld
1239 // scheme, since there may be *two* sections involved in determining
1240 // the relocation value (the section of the symbol refered to by the
1241 // relocation, and the TOC section associated with the current module).
1242 //
1243 // Fortunately, these relocations are currently only ever generated
1244 // refering to symbols that themselves reside in the TOC, which means
1245 // that the two sections are actually the same. Thus they cancel out
1246 // and we can immediately resolve the relocation right now.
1247 switch (RelType) {
1248 case ELF::R_PPC64_TOC16: RelType = ELF::R_PPC64_ADDR16; break;
1249 case ELF::R_PPC64_TOC16_DS: RelType = ELF::R_PPC64_ADDR16_DS; break;
1250 case ELF::R_PPC64_TOC16_LO: RelType = ELF::R_PPC64_ADDR16_LO; break;
1251 case ELF::R_PPC64_TOC16_LO_DS: RelType = ELF::R_PPC64_ADDR16_LO_DS; break;
1252 case ELF::R_PPC64_TOC16_HI: RelType = ELF::R_PPC64_ADDR16_HI; break;
1253 case ELF::R_PPC64_TOC16_HA: RelType = ELF::R_PPC64_ADDR16_HA; break;
1254 default: llvm_unreachable("Wrong relocation type.");
1255 }
1256
1257 RelocationValueRef TOCValue;
1258 findPPC64TOCSection(Obj, ObjSectionToID, TOCValue);
1259 if (Value.SymbolName || Value.SectionID != TOCValue.SectionID)
1260 llvm_unreachable("Unsupported TOC relocation.");
1261 Value.Addend -= TOCValue.Addend;
1262 resolveRelocation(Sections[SectionID], Offset, Value.Addend, RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001263 } else {
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +00001264 // There are two ways to refer to the TOC address directly: either
1265 // via a ELF::R_PPC64_TOC relocation (where both symbol and addend are
1266 // ignored), or via any relocation that refers to the magic ".TOC."
1267 // symbols (in which case the addend is respected).
1268 if (RelType == ELF::R_PPC64_TOC) {
1269 RelType = ELF::R_PPC64_ADDR64;
1270 findPPC64TOCSection(Obj, ObjSectionToID, Value);
1271 } else if (TargetName == ".TOC.") {
1272 findPPC64TOCSection(Obj, ObjSectionToID, Value);
1273 Value.Addend += Addend;
1274 }
1275
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001276 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Richard Mittonad6d3492013-08-16 18:54:26 +00001277
1278 if (Value.SymbolName)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001279 addRelocationForSymbol(RE, Value.SymbolName);
1280 else
1281 addRelocationForSection(RE, Value.SectionID);
1282 }
Richard Sandifordca044082013-05-03 14:15:35 +00001283 } else if (Arch == Triple::systemz &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001284 (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) {
Richard Sandifordca044082013-05-03 14:15:35 +00001285 // Create function stubs for both PLT and GOT references, regardless of
1286 // whether the GOT reference is to data or code. The stub contains the
1287 // full address of the symbol, as needed by GOT references, and the
1288 // executable part only adds an overhead of 8 bytes.
1289 //
1290 // We could try to conserve space by allocating the code and data
1291 // parts of the stub separately. However, as things stand, we allocate
1292 // a stub for every relocation, so using a GOT in JIT code should be
1293 // no less space efficient than using an explicit constant pool.
1294 DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
1295 SectionEntry &Section = Sections[SectionID];
1296
1297 // Look for an existing stub.
1298 StubMap::const_iterator i = Stubs.find(Value);
1299 uintptr_t StubAddress;
1300 if (i != Stubs.end()) {
1301 StubAddress = uintptr_t(Section.Address) + i->second;
1302 DEBUG(dbgs() << " Stub function found\n");
1303 } else {
1304 // Create a new stub function.
1305 DEBUG(dbgs() << " Create a new stub function\n");
1306
1307 uintptr_t BaseAddress = uintptr_t(Section.Address);
1308 uintptr_t StubAlignment = getStubAlignment();
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001309 StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) &
1310 -StubAlignment;
Richard Sandifordca044082013-05-03 14:15:35 +00001311 unsigned StubOffset = StubAddress - BaseAddress;
1312
1313 Stubs[Value] = StubOffset;
1314 createStubFunction((uint8_t *)StubAddress);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001315 RelocationEntry RE(SectionID, StubOffset + 8, ELF::R_390_64,
Lang Hames40e200e2014-08-25 23:33:48 +00001316 Value.Offset);
Richard Sandifordca044082013-05-03 14:15:35 +00001317 if (Value.SymbolName)
1318 addRelocationForSymbol(RE, Value.SymbolName);
1319 else
1320 addRelocationForSection(RE, Value.SectionID);
1321 Section.StubOffset = StubOffset + getMaxStubSize();
1322 }
1323
1324 if (RelType == ELF::R_390_GOTENT)
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001325 resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL,
1326 Addend);
Richard Sandifordca044082013-05-03 14:15:35 +00001327 else
1328 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001329 } else if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_PLT32) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001330 // The way the PLT relocations normally work is that the linker allocates
1331 // the
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001332 // PLT and this relocation makes a PC-relative call into the PLT. The PLT
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001333 // entry will then jump to an address provided by the GOT. On first call,
1334 // the
1335 // GOT address will point back into PLT code that resolves the symbol. After
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001336 // the first call, the GOT entry points to the actual function.
1337 //
1338 // For local functions we're ignoring all of that here and just replacing
1339 // the PLT32 relocation type with PC32, which will translate the relocation
1340 // into a PC-relative call directly to the function. For external symbols we
1341 // can't be sure the function will be within 2^32 bytes of the call site, so
1342 // we need to create a stub, which calls into the GOT. This case is
1343 // equivalent to the usual PLT implementation except that we use the stub
1344 // mechanism in RuntimeDyld (which puts stubs at the end of the section)
1345 // rather than allocating a PLT section.
1346 if (Value.SymbolName) {
1347 // This is a call to an external function.
1348 // Look for an existing stub.
1349 SectionEntry &Section = Sections[SectionID];
1350 StubMap::const_iterator i = Stubs.find(Value);
1351 uintptr_t StubAddress;
1352 if (i != Stubs.end()) {
1353 StubAddress = uintptr_t(Section.Address) + i->second;
1354 DEBUG(dbgs() << " Stub function found\n");
1355 } else {
1356 // Create a new stub function (equivalent to a PLT entry).
1357 DEBUG(dbgs() << " Create a new stub function\n");
1358
1359 uintptr_t BaseAddress = uintptr_t(Section.Address);
1360 uintptr_t StubAlignment = getStubAlignment();
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001361 StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) &
1362 -StubAlignment;
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001363 unsigned StubOffset = StubAddress - BaseAddress;
1364 Stubs[Value] = StubOffset;
1365 createStubFunction((uint8_t *)StubAddress);
1366
1367 // Create a GOT entry for the external function.
1368 GOTEntries.push_back(Value);
1369
1370 // Make our stub function a relative call to the GOT entry.
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001371 RelocationEntry RE(SectionID, StubOffset + 2, ELF::R_X86_64_GOTPCREL,
1372 -4);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001373 addRelocationForSymbol(RE, Value.SymbolName);
1374
1375 // Bump our stub offset counter
1376 Section.StubOffset = StubOffset + getMaxStubSize();
1377 }
1378
1379 // Make the target call a call into the stub table.
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001380 resolveRelocation(Section, Offset, StubAddress, ELF::R_X86_64_PC32,
1381 Addend);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001382 } else {
1383 RelocationEntry RE(SectionID, Offset, ELF::R_X86_64_PC32, Value.Addend,
1384 Value.Offset);
1385 addRelocationForSection(RE, Value.SectionID);
1386 }
Eli Bendersky667b8792012-05-01 10:41:12 +00001387 } else {
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001388 if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_GOTPCREL) {
1389 GOTEntries.push_back(Value);
1390 }
1391 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset);
Eli Bendersky667b8792012-05-01 10:41:12 +00001392 if (Value.SymbolName)
1393 addRelocationForSymbol(RE, Value.SymbolName);
1394 else
1395 addRelocationForSection(RE, Value.SectionID);
1396 }
Juergen Ributzka046709f2014-03-21 07:26:41 +00001397 return ++RelI;
Jim Grosbacheff0a402012-01-16 22:26:39 +00001398}
1399
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001400void RuntimeDyldELF::updateGOTEntries(StringRef Name, uint64_t Addr) {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001401
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001402 SmallVectorImpl<std::pair<SID, GOTRelocations>>::iterator it;
1403 SmallVectorImpl<std::pair<SID, GOTRelocations>>::iterator end = GOTs.end();
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001404
1405 for (it = GOTs.begin(); it != end; ++it) {
1406 GOTRelocations &GOTEntries = it->second;
1407 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
Craig Topper353eda42014-04-24 06:44:33 +00001408 if (GOTEntries[i].SymbolName != nullptr &&
1409 GOTEntries[i].SymbolName == Name) {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001410 GOTEntries[i].Offset = Addr;
1411 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001412 }
1413 }
1414}
1415
1416size_t RuntimeDyldELF::getGOTEntrySize() {
1417 // We don't use the GOT in all of these cases, but it's essentially free
1418 // to put them all here.
1419 size_t Result = 0;
1420 switch (Arch) {
1421 case Triple::x86_64:
1422 case Triple::aarch64:
James Molloybd2ffa02014-04-30 10:15:41 +00001423 case Triple::aarch64_be:
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001424 case Triple::ppc64:
1425 case Triple::ppc64le:
1426 case Triple::systemz:
1427 Result = sizeof(uint64_t);
1428 break;
1429 case Triple::x86:
1430 case Triple::arm:
1431 case Triple::thumb:
1432 case Triple::mips:
1433 case Triple::mipsel:
1434 Result = sizeof(uint32_t);
1435 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001436 default:
1437 llvm_unreachable("Unsupported CPU type!");
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001438 }
1439 return Result;
1440}
1441
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001442uint64_t RuntimeDyldELF::findGOTEntry(uint64_t LoadAddress, uint64_t Offset) {
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001443
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001444 const size_t GOTEntrySize = getGOTEntrySize();
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001445
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001446 SmallVectorImpl<std::pair<SID, GOTRelocations>>::const_iterator it;
1447 SmallVectorImpl<std::pair<SID, GOTRelocations>>::const_iterator end =
1448 GOTs.end();
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001449
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001450 int GOTIndex = -1;
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001451 for (it = GOTs.begin(); it != end; ++it) {
1452 SID GOTSectionID = it->first;
1453 const GOTRelocations &GOTEntries = it->second;
1454
1455 // Find the matching entry in our vector.
1456 uint64_t SymbolOffset = 0;
1457 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
Craig Topper353eda42014-04-24 06:44:33 +00001458 if (!GOTEntries[i].SymbolName) {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001459 if (getSectionLoadAddress(GOTEntries[i].SectionID) == LoadAddress &&
1460 GOTEntries[i].Offset == Offset) {
1461 GOTIndex = i;
1462 SymbolOffset = GOTEntries[i].Offset;
1463 break;
1464 }
1465 } else {
1466 // GOT entries for external symbols use the addend as the address when
1467 // the external symbol has been resolved.
1468 if (GOTEntries[i].Offset == LoadAddress) {
1469 GOTIndex = i;
1470 // Don't use the Addend here. The relocation handler will use it.
1471 break;
1472 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001473 }
1474 }
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001475
1476 if (GOTIndex != -1) {
1477 if (GOTEntrySize == sizeof(uint64_t)) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001478 uint64_t *LocalGOTAddr = (uint64_t *)getSectionAddress(GOTSectionID);
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001479 // Fill in this entry with the address of the symbol being referenced.
1480 LocalGOTAddr[GOTIndex] = LoadAddress + SymbolOffset;
1481 } else {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001482 uint32_t *LocalGOTAddr = (uint32_t *)getSectionAddress(GOTSectionID);
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001483 // Fill in this entry with the address of the symbol being referenced.
1484 LocalGOTAddr[GOTIndex] = (uint32_t)(LoadAddress + SymbolOffset);
1485 }
1486
1487 // Calculate the load address of this entry
1488 return getSectionLoadAddress(GOTSectionID) + (GOTIndex * GOTEntrySize);
1489 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001490 }
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001491
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001492 assert(GOTIndex != -1 && "Unable to find requested GOT entry.");
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001493 return 0;
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001494}
1495
Lang Hames36072da2014-05-12 21:39:59 +00001496void RuntimeDyldELF::finalizeLoad(ObjectImage &ObjImg,
1497 ObjSectionToIDMap &SectionMap) {
Andrew Kaylor7bb13442013-10-11 21:25:48 +00001498 // If necessary, allocate the global offset table
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001499 if (MemMgr) {
1500 // Allocate the GOT if necessary
1501 size_t numGOTEntries = GOTEntries.size();
1502 if (numGOTEntries != 0) {
1503 // Allocate memory for the section
1504 unsigned SectionID = Sections.size();
1505 size_t TotalSize = numGOTEntries * getGOTEntrySize();
1506 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, getGOTEntrySize(),
1507 SectionID, ".got", false);
1508 if (!Addr)
1509 report_fatal_error("Unable to allocate memory for GOT!");
1510
1511 GOTs.push_back(std::make_pair(SectionID, GOTEntries));
1512 Sections.push_back(SectionEntry(".got", Addr, TotalSize, 0));
1513 // For now, initialize all GOT entries to zero. We'll fill them in as
1514 // needed when GOT-based relocations are applied.
1515 memset(Addr, 0, TotalSize);
1516 }
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001517 } else {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001518 report_fatal_error("Unable to allocate memory for GOT!");
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001519 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +00001520
1521 // Look for and record the EH frame section.
1522 ObjSectionToIDMap::iterator i, e;
1523 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
1524 const SectionRef &Section = i->first;
1525 StringRef Name;
1526 Section.getName(Name);
1527 if (Name == ".eh_frame") {
1528 UnregisteredEHFrameSections.push_back(i->second);
1529 break;
1530 }
1531 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001532}
1533
Andrew Kayloradc70562012-10-02 21:18:39 +00001534bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
1535 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
1536 return false;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001537 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic,
1538 strlen(ELF::ElfMagic))) == 0;
Eli Bendersky4c647582012-01-16 08:56:09 +00001539}
Lang Hames173c69f2014-01-08 04:09:09 +00001540
1541bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile *Obj) const {
1542 return Obj->isELF();
1543}
1544
Eli Bendersky4c647582012-01-16 08:56:09 +00001545} // namespace llvm