blob: 526fe55a70a757ccc770cf90164b8b39364df119 [file] [log] [blame]
Jim Grosbach06594e12012-01-16 23:50:58 +00001//===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===//
Eli Bendersky4c647582012-01-16 08:56:09 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Implementation of ELF support for the MC-JIT runtime dynamic linker.
11//
12//===----------------------------------------------------------------------===//
13
Andrew Kayloradc70562012-10-02 21:18:39 +000014#include "RuntimeDyldELF.h"
15#include "JITRegistrar.h"
16#include "ObjectImageCommon.h"
Eli Bendersky4c647582012-01-16 08:56:09 +000017#include "llvm/ADT/IntervalMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/StringRef.h"
Eli Bendersky4c647582012-01-16 08:56:09 +000020#include "llvm/ADT/Triple.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/ExecutionEngine/ObjectBuffer.h"
22#include "llvm/ExecutionEngine/ObjectImage.h"
Michael J. Spencer126973b2013-08-08 22:27:13 +000023#include "llvm/Object/ELFObjectFile.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Object/ObjectFile.h"
25#include "llvm/Support/ELF.h"
Lang Hames173c69f2014-01-08 04:09:09 +000026#include "llvm/Support/MemoryBuffer.h"
27
Eli Bendersky4c647582012-01-16 08:56:09 +000028using namespace llvm;
29using namespace llvm::object;
30
Chandler Carruthf58e3762014-04-22 03:04:17 +000031#define DEBUG_TYPE "dyld"
32
Preston Gurdcc31af92012-04-16 22:12:58 +000033namespace {
34
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000035static inline std::error_code check(std::error_code Err) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +000036 if (Err) {
37 report_fatal_error(Err.message());
38 }
39 return Err;
40}
41
Juergen Ributzka7608dc02014-03-21 20:28:42 +000042template <class ELFT> class DyldELFObject : public ELFObjectFile<ELFT> {
Rafael Espindola035b4162013-04-17 21:20:55 +000043 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Preston Gurdcc31af92012-04-16 22:12:58 +000044
Michael J. Spencer1a791612013-01-15 07:44:25 +000045 typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
46 typedef Elf_Sym_Impl<ELFT> Elf_Sym;
Juergen Ributzka7608dc02014-03-21 20:28:42 +000047 typedef Elf_Rel_Impl<ELFT, false> Elf_Rel;
48 typedef Elf_Rel_Impl<ELFT, true> Elf_Rela;
Preston Gurdcc31af92012-04-16 22:12:58 +000049
Michael J. Spencer1a791612013-01-15 07:44:25 +000050 typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
Preston Gurdcc31af92012-04-16 22:12:58 +000051
Juergen Ributzka7608dc02014-03-21 20:28:42 +000052 typedef typename ELFDataTypeTypedefHelper<ELFT>::value_type addr_type;
Preston Gurdcc31af92012-04-16 22:12:58 +000053
David Blaikie7a1e7752014-04-29 21:52:46 +000054 std::unique_ptr<ObjectFile> UnderlyingFile;
55
Preston Gurdcc31af92012-04-16 22:12:58 +000056public:
David Blaikie7a1e7752014-04-29 21:52:46 +000057 DyldELFObject(std::unique_ptr<ObjectFile> UnderlyingFile,
Rafael Espindola2e60ca92014-06-24 13:56:32 +000058 std::unique_ptr<MemoryBuffer> Wrapper, std::error_code &ec);
David Blaikie7a1e7752014-04-29 21:52:46 +000059
Rafael Espindola2e60ca92014-06-24 13:56:32 +000060 DyldELFObject(std::unique_ptr<MemoryBuffer> Wrapper, std::error_code &ec);
Preston Gurdcc31af92012-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 Kaylor5c010902012-07-27 17:52:42 +000065 // Methods for type inquiry through isa, cast and dyn_cast
Preston Gurdcc31af92012-04-16 22:12:58 +000066 static inline bool classof(const Binary *v) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +000067 return (isa<ELFObjectFile<ELFT>>(v) &&
68 classof(cast<ELFObjectFile<ELFT>>(v)));
Preston Gurdcc31af92012-04-16 22:12:58 +000069 }
Juergen Ributzka7608dc02014-03-21 20:28:42 +000070 static inline bool classof(const ELFObjectFile<ELFT> *v) {
Preston Gurdcc31af92012-04-16 22:12:58 +000071 return v->isDyldType();
72 }
Preston Gurdcc31af92012-04-16 22:12:58 +000073};
74
Juergen Ributzka7608dc02014-03-21 20:28:42 +000075template <class ELFT> class ELFObjectImage : public ObjectImageCommon {
Juergen Ributzka7608dc02014-03-21 20:28:42 +000076 bool Registered;
Preston Gurdcc31af92012-04-16 22:12:58 +000077
Juergen Ributzka7608dc02014-03-21 20:28:42 +000078public:
David Blaikie7a1e7752014-04-29 21:52:46 +000079 ELFObjectImage(ObjectBuffer *Input, std::unique_ptr<DyldELFObject<ELFT>> Obj)
80 : ObjectImageCommon(Input, std::move(Obj)), Registered(false) {}
Preston Gurdcc31af92012-04-16 22:12:58 +000081
Juergen Ributzka7608dc02014-03-21 20:28:42 +000082 virtual ~ELFObjectImage() {
83 if (Registered)
84 deregisterWithDebugger();
85 }
Preston Gurdcc31af92012-04-16 22:12:58 +000086
Juergen Ributzka7608dc02014-03-21 20:28:42 +000087 // 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 {
David Blaikie7a1e7752014-04-29 21:52:46 +000090 static_cast<DyldELFObject<ELFT>*>(getObjectFile())
91 ->updateSectionAddress(Sec, Addr);
Juergen Ributzka7608dc02014-03-21 20:28:42 +000092 }
Preston Gurdcc31af92012-04-16 22:12:58 +000093
Juergen Ributzka7608dc02014-03-21 20:28:42 +000094 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr) override {
David Blaikie7a1e7752014-04-29 21:52:46 +000095 static_cast<DyldELFObject<ELFT>*>(getObjectFile())
96 ->updateSymbolAddress(Sym, Addr);
Juergen Ributzka7608dc02014-03-21 20:28:42 +000097 }
Preston Gurdcc31af92012-04-16 22:12:58 +000098
Juergen Ributzka7608dc02014-03-21 20:28:42 +000099 void registerWithDebugger() override {
100 JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
101 Registered = true;
102 }
103 void deregisterWithDebugger() override {
104 JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer);
105 }
Preston Gurdcc31af92012-04-16 22:12:58 +0000106};
107
Andrew Kayloradc70562012-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.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000111template <class ELFT>
Rafael Espindola2e60ca92014-06-24 13:56:32 +0000112DyldELFObject<ELFT>::DyldELFObject(std::unique_ptr<MemoryBuffer> Wrapper,
113 std::error_code &EC)
114 : ELFObjectFile<ELFT>(std::move(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 Espindola2e60ca92014-06-24 13:56:32 +0000120 std::unique_ptr<MemoryBuffer> Wrapper,
121 std::error_code &EC)
122 : ELFObjectFile<ELFT>(std::move(Wrapper), EC),
David Blaikie7a1e7752014-04-29 21:52:46 +0000123 UnderlyingFile(std::move(UnderlyingFile)) {
124 this->isDyldELFObject = true;
125}
126
127template <class ELFT>
Michael J. Spencer1a791612013-01-15 07:44:25 +0000128void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec,
129 uint64_t Addr) {
Preston Gurdcc31af92012-04-16 22:12:58 +0000130 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000131 Elf_Shdr *shdr =
132 const_cast<Elf_Shdr *>(reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
Preston Gurdcc31af92012-04-16 22:12:58 +0000133
134 // This assumes the address passed in matches the target address bitness
135 // The template-based type cast handles everything else.
136 shdr->sh_addr = static_cast<addr_type>(Addr);
137}
138
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000139template <class ELFT>
Michael J. Spencer1a791612013-01-15 07:44:25 +0000140void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef,
141 uint64_t Addr) {
Preston Gurdcc31af92012-04-16 22:12:58 +0000142
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000143 Elf_Sym *sym = const_cast<Elf_Sym *>(
144 ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl()));
Preston Gurdcc31af92012-04-16 22:12:58 +0000145
146 // This assumes the address passed in matches the target address bitness
147 // The template-based type cast handles everything else.
148 sym->st_value = static_cast<addr_type>(Addr);
149}
150
151} // namespace
152
Eli Bendersky4c647582012-01-16 08:56:09 +0000153namespace llvm {
154
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000155void RuntimeDyldELF::registerEHFrames() {
156 if (!MemMgr)
157 return;
158 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
159 SID EHFrameSID = UnregisteredEHFrameSections[i];
160 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
161 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
162 size_t EHFrameSize = Sections[EHFrameSID].Size;
163 MemMgr->registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
Andrew Kaylorc442a762013-10-16 00:14:21 +0000164 RegisteredEHFrameSections.push_back(EHFrameSID);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000165 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000166 UnregisteredEHFrameSections.clear();
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000167}
168
Andrew Kaylorc442a762013-10-16 00:14:21 +0000169void RuntimeDyldELF::deregisterEHFrames() {
170 if (!MemMgr)
171 return;
172 for (int i = 0, e = RegisteredEHFrameSections.size(); i != e; ++i) {
173 SID EHFrameSID = RegisteredEHFrameSections[i];
174 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
175 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
176 size_t EHFrameSize = Sections[EHFrameSID].Size;
177 MemMgr->deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
178 }
179 RegisteredEHFrameSections.clear();
180}
181
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000182ObjectImage *
David Blaikie7a1e7752014-04-29 21:52:46 +0000183RuntimeDyldELF::createObjectImageFromFile(std::unique_ptr<object::ObjectFile> ObjFile) {
Lang Hames173c69f2014-01-08 04:09:09 +0000184 if (!ObjFile)
Craig Topper353eda42014-04-24 06:44:33 +0000185 return nullptr;
Lang Hames173c69f2014-01-08 04:09:09 +0000186
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000187 std::error_code ec;
Rafael Espindola2e60ca92014-06-24 13:56:32 +0000188 std::unique_ptr<MemoryBuffer> Buffer(
189 MemoryBuffer::getMemBuffer(ObjFile->getData(), "", false));
Lang Hames173c69f2014-01-08 04:09:09 +0000190
191 if (ObjFile->getBytesInAddress() == 4 && ObjFile->isLittleEndian()) {
Reid Kleckner7aeb9052014-04-29 23:54:52 +0000192 auto Obj =
193 llvm::make_unique<DyldELFObject<ELFType<support::little, 2, false>>>(
Rafael Espindola2e60ca92014-06-24 13:56:32 +0000194 std::move(ObjFile), std::move(Buffer), ec);
David Blaikie7a1e7752014-04-29 21:52:46 +0000195 return new ELFObjectImage<ELFType<support::little, 2, false>>(
196 nullptr, std::move(Obj));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000197 } else if (ObjFile->getBytesInAddress() == 4 && !ObjFile->isLittleEndian()) {
Reid Kleckner7aeb9052014-04-29 23:54:52 +0000198 auto Obj =
199 llvm::make_unique<DyldELFObject<ELFType<support::big, 2, false>>>(
Rafael Espindola2e60ca92014-06-24 13:56:32 +0000200 std::move(ObjFile), std::move(Buffer), ec);
David Blaikie7a1e7752014-04-29 21:52:46 +0000201 return new ELFObjectImage<ELFType<support::big, 2, false>>(nullptr, std::move(Obj));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000202 } else if (ObjFile->getBytesInAddress() == 8 && !ObjFile->isLittleEndian()) {
Reid Kleckner7aeb9052014-04-29 23:54:52 +0000203 auto Obj = llvm::make_unique<DyldELFObject<ELFType<support::big, 2, true>>>(
Rafael Espindola2e60ca92014-06-24 13:56:32 +0000204 std::move(ObjFile), std::move(Buffer), ec);
David Blaikie7a1e7752014-04-29 21:52:46 +0000205 return new ELFObjectImage<ELFType<support::big, 2, true>>(nullptr,
206 std::move(Obj));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000207 } else if (ObjFile->getBytesInAddress() == 8 && ObjFile->isLittleEndian()) {
Reid Kleckner7aeb9052014-04-29 23:54:52 +0000208 auto Obj =
209 llvm::make_unique<DyldELFObject<ELFType<support::little, 2, true>>>(
Rafael Espindola2e60ca92014-06-24 13:56:32 +0000210 std::move(ObjFile), std::move(Buffer), ec);
David Blaikie7a1e7752014-04-29 21:52:46 +0000211 return new ELFObjectImage<ELFType<support::little, 2, true>>(
212 nullptr, std::move(Obj));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000213 } else
Lang Hames173c69f2014-01-08 04:09:09 +0000214 llvm_unreachable("Unexpected ELF format");
215}
216
Andrew Kayloradc70562012-10-02 21:18:39 +0000217ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) {
218 if (Buffer->getBufferSize() < ELF::EI_NIDENT)
219 llvm_unreachable("Unexpected ELF object size");
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000220 std::pair<unsigned char, unsigned char> Ident =
221 std::make_pair((uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS],
222 (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000223 std::error_code ec;
Preston Gurdcc31af92012-04-16 22:12:58 +0000224
Rafael Espindola2e60ca92014-06-24 13:56:32 +0000225 std::unique_ptr<MemoryBuffer> Buf(Buffer->getMemBuffer());
226
Preston Gurdcc31af92012-04-16 22:12:58 +0000227 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
Reid Kleckner7aeb9052014-04-29 23:54:52 +0000228 auto Obj =
229 llvm::make_unique<DyldELFObject<ELFType<support::little, 4, false>>>(
Rafael Espindola2e60ca92014-06-24 13:56:32 +0000230 std::move(Buf), ec);
David Blaikie7a1e7752014-04-29 21:52:46 +0000231 return new ELFObjectImage<ELFType<support::little, 4, false>>(
232 Buffer, std::move(Obj));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000233 } else if (Ident.first == ELF::ELFCLASS32 &&
234 Ident.second == ELF::ELFDATA2MSB) {
David Blaikie7a1e7752014-04-29 21:52:46 +0000235 auto Obj =
Reid Kleckner7aeb9052014-04-29 23:54:52 +0000236 llvm::make_unique<DyldELFObject<ELFType<support::big, 4, false>>>(
Rafael Espindola2e60ca92014-06-24 13:56:32 +0000237 std::move(Buf), ec);
David Blaikie7a1e7752014-04-29 21:52:46 +0000238 return new ELFObjectImage<ELFType<support::big, 4, false>>(Buffer,
239 std::move(Obj));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000240 } else if (Ident.first == ELF::ELFCLASS64 &&
241 Ident.second == ELF::ELFDATA2MSB) {
Reid Kleckner7aeb9052014-04-29 23:54:52 +0000242 auto Obj = llvm::make_unique<DyldELFObject<ELFType<support::big, 8, true>>>(
Rafael Espindola2e60ca92014-06-24 13:56:32 +0000243 std::move(Buf), ec);
David Blaikie7a1e7752014-04-29 21:52:46 +0000244 return new ELFObjectImage<ELFType<support::big, 8, true>>(Buffer, std::move(Obj));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000245 } else if (Ident.first == ELF::ELFCLASS64 &&
246 Ident.second == ELF::ELFDATA2LSB) {
David Blaikie7a1e7752014-04-29 21:52:46 +0000247 auto Obj =
Reid Kleckner7aeb9052014-04-29 23:54:52 +0000248 llvm::make_unique<DyldELFObject<ELFType<support::little, 8, true>>>(
Rafael Espindola2e60ca92014-06-24 13:56:32 +0000249 std::move(Buf), ec);
David Blaikie7a1e7752014-04-29 21:52:46 +0000250 return new ELFObjectImage<ELFType<support::little, 8, true>>(Buffer, std::move(Obj));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000251 } else
Preston Gurdcc31af92012-04-16 22:12:58 +0000252 llvm_unreachable("Unexpected ELF format");
253}
254
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000255RuntimeDyldELF::~RuntimeDyldELF() {}
Eli Bendersky4c647582012-01-16 08:56:09 +0000256
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000257void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000258 uint64_t Offset, uint64_t Value,
259 uint32_t Type, int64_t Addend,
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000260 uint64_t SymOffset) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000261 switch (Type) {
262 default:
263 llvm_unreachable("Relocation type not implemented yet!");
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000264 break;
Eli Bendersky4c647582012-01-16 08:56:09 +0000265 case ELF::R_X86_64_64: {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000266 uint64_t *Target = reinterpret_cast<uint64_t *>(Section.Address + Offset);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000267 *Target = Value + Addend;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000268 DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
269 << format("%p\n", Target));
Eli Bendersky4c647582012-01-16 08:56:09 +0000270 break;
271 }
272 case ELF::R_X86_64_32:
273 case ELF::R_X86_64_32S: {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000274 Value += Addend;
Andrew Kaylor8e87a752012-07-27 20:30:12 +0000275 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000276 (Type == ELF::R_X86_64_32S &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000277 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
Eli Bendersky4c647582012-01-16 08:56:09 +0000278 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000279 uint32_t *Target = reinterpret_cast<uint32_t *>(Section.Address + Offset);
Eli Bendersky4c647582012-01-16 08:56:09 +0000280 *Target = TruncatedAddr;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000281 DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at "
282 << format("%p\n", Target));
Eli Bendersky4c647582012-01-16 08:56:09 +0000283 break;
284 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000285 case ELF::R_X86_64_GOTPCREL: {
286 // findGOTEntry returns the 'G + GOT' part of the relocation calculation
287 // based on the load/target address of the GOT (not the current/local addr).
288 uint64_t GOTAddr = findGOTEntry(Value, SymOffset);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000289 uint32_t *Target = reinterpret_cast<uint32_t *>(Section.Address + Offset);
290 uint64_t FinalAddress = Section.LoadAddress + Offset;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000291 // The processRelocationRef method combines the symbol offset and the addend
292 // and in most cases that's what we want. For this relocation type, we need
293 // the raw addend, so we subtract the symbol offset to get it.
294 int64_t RealOffset = GOTAddr + Addend - SymOffset - FinalAddress;
295 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
296 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
297 *Target = TruncOffset;
298 break;
299 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000300 case ELF::R_X86_64_PC32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000301 // Get the placeholder value from the generated object since
302 // a previous relocation attempt may have overwritten the loaded version
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000303 uint32_t *Placeholder =
304 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
305 uint32_t *Target = reinterpret_cast<uint32_t *>(Section.Address + Offset);
306 uint64_t FinalAddress = Section.LoadAddress + Offset;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000307 int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylor8e87a752012-07-27 20:30:12 +0000308 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000309 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000310 *Target = TruncOffset;
Eli Bendersky4c647582012-01-16 08:56:09 +0000311 break;
312 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000313 case ELF::R_X86_64_PC64: {
314 // Get the placeholder value from the generated object since
315 // a previous relocation attempt may have overwritten the loaded version
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000316 uint64_t *Placeholder =
317 reinterpret_cast<uint64_t *>(Section.ObjAddress + Offset);
318 uint64_t *Target = reinterpret_cast<uint64_t *>(Section.Address + Offset);
319 uint64_t FinalAddress = Section.LoadAddress + Offset;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000320 *Target = *Placeholder + Value + Addend - FinalAddress;
321 break;
322 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000323 }
324}
325
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000326void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000327 uint64_t Offset, uint32_t Value,
328 uint32_t Type, int32_t Addend) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000329 switch (Type) {
Eli Bendersky4c647582012-01-16 08:56:09 +0000330 case ELF::R_386_32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000331 // Get the placeholder value from the generated object since
332 // a previous relocation attempt may have overwritten the loaded version
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000333 uint32_t *Placeholder =
334 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
335 uint32_t *Target = reinterpret_cast<uint32_t *>(Section.Address + Offset);
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000336 *Target = *Placeholder + Value + Addend;
Eli Bendersky4c647582012-01-16 08:56:09 +0000337 break;
338 }
339 case ELF::R_386_PC32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000340 // Get the placeholder value from the generated object since
341 // a previous relocation attempt may have overwritten the loaded version
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000342 uint32_t *Placeholder =
343 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
344 uint32_t *Target = reinterpret_cast<uint32_t *>(Section.Address + Offset);
345 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000346 uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000347 *Target = RealOffset;
Eli Bendersky4c647582012-01-16 08:56:09 +0000348 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000349 }
350 default:
351 // There are other relocation types, but it appears these are the
352 // only ones currently used by the LLVM ELF object writer
353 llvm_unreachable("Relocation type not implemented yet!");
354 break;
Eli Bendersky4c647582012-01-16 08:56:09 +0000355 }
356}
357
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000358void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000359 uint64_t Offset, uint64_t Value,
360 uint32_t Type, int64_t Addend) {
361 uint32_t *TargetPtr = reinterpret_cast<uint32_t *>(Section.Address + Offset);
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000362 uint64_t FinalAddress = Section.LoadAddress + Offset;
363
364 DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
365 << format("%llx", Section.Address + Offset)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000366 << " FinalAddress: 0x" << format("%llx", FinalAddress)
367 << " Value: 0x" << format("%llx", Value) << " Type: 0x"
368 << format("%x", Type) << " Addend: 0x" << format("%llx", Addend)
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000369 << "\n");
370
371 switch (Type) {
372 default:
373 llvm_unreachable("Relocation type not implemented yet!");
374 break;
Tim Northoverb23d8db2013-05-04 20:14:14 +0000375 case ELF::R_AARCH64_ABS64: {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000376 uint64_t *TargetPtr =
377 reinterpret_cast<uint64_t *>(Section.Address + Offset);
Tim Northoverb23d8db2013-05-04 20:14:14 +0000378 *TargetPtr = Value + Addend;
379 break;
380 }
Tim Northover5959ea32013-05-19 15:39:03 +0000381 case ELF::R_AARCH64_PREL32: {
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000382 uint64_t Result = Value + Addend - FinalAddress;
Michael J. Spencer126973b2013-08-08 22:27:13 +0000383 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000384 static_cast<int64_t>(Result) <= UINT32_MAX);
385 *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU);
386 break;
387 }
Tim Northover37cde972013-05-04 20:14:09 +0000388 case ELF::R_AARCH64_CALL26: // fallthrough
389 case ELF::R_AARCH64_JUMP26: {
390 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
391 // calculation.
392 uint64_t BranchImm = Value + Addend - FinalAddress;
393
394 // "Check that -2^27 <= result < 2^27".
Michael J. Spencer126973b2013-08-08 22:27:13 +0000395 assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) &&
Tim Northover37cde972013-05-04 20:14:09 +0000396 static_cast<int64_t>(BranchImm) < (1LL << 27));
Tim Northover5959ea32013-05-19 15:39:03 +0000397
398 // AArch64 code is emitted with .rela relocations. The data already in any
399 // bits affected by the relocation on entry is garbage.
400 *TargetPtr &= 0xfc000000U;
Tim Northover37cde972013-05-04 20:14:09 +0000401 // Immediate goes in bits 25:0 of B and BL.
402 *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2;
403 break;
404 }
Tim Northover4d01c1e2013-05-04 20:14:04 +0000405 case ELF::R_AARCH64_MOVW_UABS_G3: {
406 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000407
408 // AArch64 code is emitted with .rela relocations. The data already in any
409 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000410 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000411 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
412 *TargetPtr |= Result >> (48 - 5);
Tim Northover8625fd82013-07-01 19:23:10 +0000413 // Shift must be "lsl #48", in bits 22:21
414 assert((*TargetPtr >> 21 & 0x3) == 3 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000415 break;
416 }
417 case ELF::R_AARCH64_MOVW_UABS_G2_NC: {
418 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000419
Tim Northover5959ea32013-05-19 15:39:03 +0000420 // AArch64 code is emitted with .rela relocations. The data already in any
421 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000422 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000423 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
424 *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
Tim Northover8625fd82013-07-01 19:23:10 +0000425 // Shift must be "lsl #32", in bits 22:21
426 assert((*TargetPtr >> 21 & 0x3) == 2 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000427 break;
428 }
429 case ELF::R_AARCH64_MOVW_UABS_G1_NC: {
430 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000431
432 // AArch64 code is emitted with .rela relocations. The data already in any
433 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000434 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000435 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
436 *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
Tim Northover8625fd82013-07-01 19:23:10 +0000437 // Shift must be "lsl #16", in bits 22:2
438 assert((*TargetPtr >> 21 & 0x3) == 1 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000439 break;
440 }
441 case ELF::R_AARCH64_MOVW_UABS_G0_NC: {
442 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000443
444 // AArch64 code is emitted with .rela relocations. The data already in any
445 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000446 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000447 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
448 *TargetPtr |= ((Result & 0xffffU) << 5);
Tim Northover8625fd82013-07-01 19:23:10 +0000449 // Shift must be "lsl #0", in bits 22:21.
450 assert((*TargetPtr >> 21 & 0x3) == 0 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000451 break;
452 }
Bradley Smith9d808492014-02-11 12:59:09 +0000453 case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
454 // Operation: Page(S+A) - Page(P)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000455 uint64_t Result =
456 ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
Bradley Smith9d808492014-02-11 12:59:09 +0000457
458 // Check that -2^32 <= X < 2^32
459 assert(static_cast<int64_t>(Result) >= (-1LL << 32) &&
460 static_cast<int64_t>(Result) < (1LL << 32) &&
461 "overflow check failed for relocation");
462
463 // AArch64 code is emitted with .rela relocations. The data already in any
464 // bits affected by the relocation on entry is garbage.
465 *TargetPtr &= 0x9f00001fU;
466 // Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken
467 // from bits 32:12 of X.
468 *TargetPtr |= ((Result & 0x3000U) << (29 - 12));
469 *TargetPtr |= ((Result & 0x1ffffc000ULL) >> (14 - 5));
470 break;
471 }
472 case ELF::R_AARCH64_LDST32_ABS_LO12_NC: {
473 // Operation: S + A
474 uint64_t Result = Value + Addend;
475
476 // AArch64 code is emitted with .rela relocations. The data already in any
477 // bits affected by the relocation on entry is garbage.
478 *TargetPtr &= 0xffc003ffU;
479 // Immediate goes in bits 21:10 of LD/ST instruction, taken
480 // from bits 11:2 of X
481 *TargetPtr |= ((Result & 0xffc) << (10 - 2));
482 break;
483 }
484 case ELF::R_AARCH64_LDST64_ABS_LO12_NC: {
485 // Operation: S + A
486 uint64_t Result = Value + Addend;
487
488 // AArch64 code is emitted with .rela relocations. The data already in any
489 // bits affected by the relocation on entry is garbage.
490 *TargetPtr &= 0xffc003ffU;
491 // Immediate goes in bits 21:10 of LD/ST instruction, taken
492 // from bits 11:3 of X
493 *TargetPtr |= ((Result & 0xff8) << (10 - 3));
494 break;
495 }
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000496 }
497}
498
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000499void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000500 uint64_t Offset, uint32_t Value,
501 uint32_t Type, int32_t Addend) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000502 // TODO: Add Thumb relocations.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000503 uint32_t *Placeholder =
504 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
505 uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset);
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000506 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000507 Value += Addend;
508
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000509 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
510 << Section.Address + Offset
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000511 << " FinalAddress: " << format("%p", FinalAddress) << " Value: "
512 << format("%x", Value) << " Type: " << format("%x", Type)
513 << " Addend: " << format("%x", Addend) << "\n");
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000514
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000515 switch (Type) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000516 default:
517 llvm_unreachable("Not implemented relocation type!");
518
Renato Golin8cea6e82014-01-29 11:50:56 +0000519 case ELF::R_ARM_NONE:
520 break;
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000521 // Write a 32bit value to relocation address, taking into account the
Tim Northover471cbb72012-10-03 16:29:42 +0000522 // implicit addend encoded in the target.
Renato Golin8cea6e82014-01-29 11:50:56 +0000523 case ELF::R_ARM_PREL31:
Tim Northover3b684d82013-05-28 19:48:19 +0000524 case ELF::R_ARM_TARGET1:
525 case ELF::R_ARM_ABS32:
526 *TargetPtr = *Placeholder + Value;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000527 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000528 // Write first 16 bit of 32 bit value to the mov instruction.
529 // Last 4 bit should be shifted.
Tim Northover3b684d82013-05-28 19:48:19 +0000530 case ELF::R_ARM_MOVW_ABS_NC:
Tim Northover471cbb72012-10-03 16:29:42 +0000531 // We are not expecting any other addend in the relocation address.
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000532 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
Tim Northover471cbb72012-10-03 16:29:42 +0000533 // non-contiguous fields.
Tim Northover3b684d82013-05-28 19:48:19 +0000534 assert((*Placeholder & 0x000F0FFF) == 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000535 Value = Value & 0xFFFF;
Tim Northover3b684d82013-05-28 19:48:19 +0000536 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000537 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
538 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000539 // Write last 16 bit of 32 bit value to the mov instruction.
540 // Last 4 bit should be shifted.
Tim Northover3b684d82013-05-28 19:48:19 +0000541 case ELF::R_ARM_MOVT_ABS:
Tim Northover471cbb72012-10-03 16:29:42 +0000542 // We are not expecting any other addend in the relocation address.
543 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
Tim Northover3b684d82013-05-28 19:48:19 +0000544 assert((*Placeholder & 0x000F0FFF) == 0);
545
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000546 Value = (Value >> 16) & 0xFFFF;
Tim Northover3b684d82013-05-28 19:48:19 +0000547 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000548 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
549 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000550 // Write 24 bit relative value to the branch instruction.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000551 case ELF::R_ARM_PC24: // Fall through.
552 case ELF::R_ARM_CALL: // Fall through.
Tim Northover3b684d82013-05-28 19:48:19 +0000553 case ELF::R_ARM_JUMP24: {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000554 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
555 RelValue = (RelValue & 0x03FFFFFC) >> 2;
Tim Northover3b684d82013-05-28 19:48:19 +0000556 assert((*TargetPtr & 0xFFFFFF) == 0xFFFFFE);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000557 *TargetPtr &= 0xFF000000;
558 *TargetPtr |= RelValue;
559 break;
560 }
Tim Northover3b684d82013-05-28 19:48:19 +0000561 case ELF::R_ARM_PRIVATE_0:
562 // This relocation is reserved by the ARM ELF ABI for internal use. We
563 // appropriate it here to act as an R_ARM_ABS32 without any addend for use
564 // in the stubs created during JIT (which can't put an addend into the
565 // original object file).
566 *TargetPtr = Value;
567 break;
568 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000569}
570
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000571void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000572 uint64_t Offset, uint32_t Value,
573 uint32_t Type, int32_t Addend) {
574 uint32_t *Placeholder =
575 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
576 uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000577 Value += Addend;
578
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000579 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000580 << Section.Address + Offset << " FinalAddress: "
581 << format("%p", Section.LoadAddress + Offset) << " Value: "
582 << format("%x", Value) << " Type: " << format("%x", Type)
583 << " Addend: " << format("%x", Addend) << "\n");
Akira Hatanaka111174b2012-08-17 21:28:04 +0000584
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000585 switch (Type) {
Akira Hatanaka111174b2012-08-17 21:28:04 +0000586 default:
587 llvm_unreachable("Not implemented relocation type!");
588 break;
589 case ELF::R_MIPS_32:
Akira Hatanaka2e236242013-07-24 01:58:40 +0000590 *TargetPtr = Value + (*Placeholder);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000591 break;
592 case ELF::R_MIPS_26:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000593 *TargetPtr = ((*Placeholder) & 0xfc000000) | ((Value & 0x0fffffff) >> 2);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000594 break;
595 case ELF::R_MIPS_HI16:
596 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
Akira Hatanaka2e236242013-07-24 01:58:40 +0000597 Value += ((*Placeholder) & 0x0000ffff) << 16;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000598 *TargetPtr =
599 ((*Placeholder) & 0xffff0000) | (((Value + 0x8000) >> 16) & 0xffff);
Akira Hatanaka2e236242013-07-24 01:58:40 +0000600 break;
601 case ELF::R_MIPS_LO16:
602 Value += ((*Placeholder) & 0x0000ffff);
603 *TargetPtr = ((*Placeholder) & 0xffff0000) | (Value & 0xffff);
604 break;
605 case ELF::R_MIPS_UNUSED1:
606 // Similar to ELF::R_ARM_PRIVATE_0, R_MIPS_UNUSED1 and R_MIPS_UNUSED2
607 // are used for internal JIT purpose. These relocations are similar to
608 // R_MIPS_HI16 and R_MIPS_LO16, but they do not take any addend into
609 // account.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000610 *TargetPtr =
611 ((*TargetPtr) & 0xffff0000) | (((Value + 0x8000) >> 16) & 0xffff);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000612 break;
Akira Hatanaka2e236242013-07-24 01:58:40 +0000613 case ELF::R_MIPS_UNUSED2:
Akira Hatanaka111174b2012-08-17 21:28:04 +0000614 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
615 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000616 }
Akira Hatanaka111174b2012-08-17 21:28:04 +0000617}
618
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000619// Return the .TOC. section and offset.
620void RuntimeDyldELF::findPPC64TOCSection(ObjectImage &Obj,
621 ObjSectionToIDMap &LocalSections,
622 RelocationValueRef &Rel) {
623 // Set a default SectionID in case we do not find a TOC section below.
624 // This may happen for references to TOC base base (sym@toc, .odp
625 // relocation) without a .toc directive. In this case just use the
626 // first section (which is usually the .odp) since the code won't
627 // reference the .toc base directly.
628 Rel.SymbolName = NULL;
629 Rel.SectionID = 0;
630
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000631 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
632 // order. The TOC starts where the first of these sections starts.
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000633 for (section_iterator si = Obj.begin_sections(), se = Obj.end_sections();
634 si != se; ++si) {
635
636 StringRef SectionName;
637 check(si->getName(SectionName));
638
639 if (SectionName == ".got"
640 || SectionName == ".toc"
641 || SectionName == ".tocbss"
642 || SectionName == ".plt") {
643 Rel.SectionID = findOrEmitSection(Obj, *si, false, LocalSections);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000644 break;
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000645 }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000646 }
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000647
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000648 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
649 // thus permitting a full 64 Kbytes segment.
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000650 Rel.Addend = 0x8000;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000651}
652
653// Returns the sections and offset associated with the ODP entry referenced
654// by Symbol.
655void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
656 ObjSectionToIDMap &LocalSections,
657 RelocationValueRef &Rel) {
658 // Get the ELF symbol value (st_value) to compare with Relocation offset in
659 // .opd entries
Rafael Espindola5e812af2014-01-30 02:49:50 +0000660 for (section_iterator si = Obj.begin_sections(), se = Obj.end_sections();
661 si != se; ++si) {
Rafael Espindolaa61f1e92013-06-03 19:37:34 +0000662 section_iterator RelSecI = si->getRelocatedSection();
663 if (RelSecI == Obj.end_sections())
664 continue;
665
666 StringRef RelSectionName;
667 check(RelSecI->getName(RelSectionName));
668 if (RelSectionName != ".opd")
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000669 continue;
670
Rafael Espindolab5155a52014-02-10 20:24:04 +0000671 for (relocation_iterator i = si->relocation_begin(),
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000672 e = si->relocation_end();
673 i != e;) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000674 // The R_PPC64_ADDR64 relocation indicates the first field
675 // of a .opd entry
676 uint64_t TypeFunc;
677 check(i->getType(TypeFunc));
678 if (TypeFunc != ELF::R_PPC64_ADDR64) {
Rafael Espindola5e812af2014-01-30 02:49:50 +0000679 ++i;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000680 continue;
681 }
682
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000683 uint64_t TargetSymbolOffset;
Rafael Espindola806f0062013-06-05 01:33:53 +0000684 symbol_iterator TargetSymbol = i->getSymbol();
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000685 check(i->getOffset(TargetSymbolOffset));
Rafael Espindola0d15f732013-05-09 03:39:05 +0000686 int64_t Addend;
687 check(getELFRelocationAddend(*i, Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000688
Rafael Espindola5e812af2014-01-30 02:49:50 +0000689 ++i;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000690 if (i == e)
691 break;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000692
693 // Just check if following relocation is a R_PPC64_TOC
694 uint64_t TypeTOC;
695 check(i->getType(TypeTOC));
696 if (TypeTOC != ELF::R_PPC64_TOC)
697 continue;
698
699 // Finally compares the Symbol value and the target symbol offset
700 // to check if this .opd entry refers to the symbol the relocation
701 // points to.
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000702 if (Rel.Addend != (int64_t)TargetSymbolOffset)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000703 continue;
704
705 section_iterator tsi(Obj.end_sections());
Rafael Espindola806f0062013-06-05 01:33:53 +0000706 check(TargetSymbol->getSection(tsi));
Lang Hames9b2dc932014-02-18 21:46:39 +0000707 bool IsCode = false;
708 tsi->isText(IsCode);
709 Rel.SectionID = findOrEmitSection(Obj, (*tsi), IsCode, LocalSections);
Rafael Espindola0d15f732013-05-09 03:39:05 +0000710 Rel.Addend = (intptr_t)Addend;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000711 return;
712 }
713 }
714 llvm_unreachable("Attempting to get address of ODP entry!");
715}
716
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000717// Relocation masks following the #lo(value), #hi(value), #ha(value),
718// #higher(value), #highera(value), #highest(value), and #highesta(value)
719// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
720// document.
721
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000722static inline uint16_t applyPPClo(uint64_t value) { return value & 0xffff; }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000723
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000724static inline uint16_t applyPPChi(uint64_t value) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000725 return (value >> 16) & 0xffff;
726}
727
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000728static inline uint16_t applyPPCha (uint64_t value) {
729 return ((value + 0x8000) >> 16) & 0xffff;
730}
731
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000732static inline uint16_t applyPPChigher(uint64_t value) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000733 return (value >> 32) & 0xffff;
734}
735
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000736static inline uint16_t applyPPChighera (uint64_t value) {
737 return ((value + 0x8000) >> 32) & 0xffff;
738}
739
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000740static inline uint16_t applyPPChighest(uint64_t value) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000741 return (value >> 48) & 0xffff;
742}
743
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000744static inline uint16_t applyPPChighesta (uint64_t value) {
745 return ((value + 0x8000) >> 48) & 0xffff;
746}
747
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000748void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000749 uint64_t Offset, uint64_t Value,
750 uint32_t Type, int64_t Addend) {
751 uint8_t *LocalAddress = Section.Address + Offset;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000752 switch (Type) {
753 default:
754 llvm_unreachable("Relocation type not implemented yet!");
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000755 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000756 case ELF::R_PPC64_ADDR16:
757 writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
758 break;
759 case ELF::R_PPC64_ADDR16_DS:
760 writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
761 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000762 case ELF::R_PPC64_ADDR16_LO:
763 writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000764 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000765 case ELF::R_PPC64_ADDR16_LO_DS:
766 writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
767 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000768 case ELF::R_PPC64_ADDR16_HI:
769 writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000770 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000771 case ELF::R_PPC64_ADDR16_HA:
772 writeInt16BE(LocalAddress, applyPPCha(Value + Addend));
773 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000774 case ELF::R_PPC64_ADDR16_HIGHER:
775 writeInt16BE(LocalAddress, applyPPChigher(Value + Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000776 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000777 case ELF::R_PPC64_ADDR16_HIGHERA:
778 writeInt16BE(LocalAddress, applyPPChighera(Value + Addend));
779 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000780 case ELF::R_PPC64_ADDR16_HIGHEST:
781 writeInt16BE(LocalAddress, applyPPChighest(Value + Addend));
782 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000783 case ELF::R_PPC64_ADDR16_HIGHESTA:
784 writeInt16BE(LocalAddress, applyPPChighesta(Value + Addend));
785 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000786 case ELF::R_PPC64_ADDR14: {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000787 assert(((Value + Addend) & 3) == 0);
788 // Preserve the AA/LK bits in the branch instruction
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000789 uint8_t aalk = *(LocalAddress + 3);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000790 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
791 } break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000792 case ELF::R_PPC64_REL16_LO: {
793 uint64_t FinalAddress = (Section.LoadAddress + Offset);
794 uint64_t Delta = Value - FinalAddress + Addend;
795 writeInt16BE(LocalAddress, applyPPClo(Delta));
796 } break;
797 case ELF::R_PPC64_REL16_HI: {
798 uint64_t FinalAddress = (Section.LoadAddress + Offset);
799 uint64_t Delta = Value - FinalAddress + Addend;
800 writeInt16BE(LocalAddress, applyPPChi(Delta));
801 } break;
802 case ELF::R_PPC64_REL16_HA: {
803 uint64_t FinalAddress = (Section.LoadAddress + Offset);
804 uint64_t Delta = Value - FinalAddress + Addend;
805 writeInt16BE(LocalAddress, applyPPCha(Delta));
806 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000807 case ELF::R_PPC64_ADDR32: {
Adhemerval Zanella9b0b7812013-01-04 19:08:13 +0000808 int32_t Result = static_cast<int32_t>(Value + Addend);
809 if (SignExtend32<32>(Result) != Result)
Adhemerval Zanella1ae22482013-01-09 17:08:15 +0000810 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
Adhemerval Zanella9b0b7812013-01-04 19:08:13 +0000811 writeInt32BE(LocalAddress, Result);
812 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000813 case ELF::R_PPC64_REL24: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000814 uint64_t FinalAddress = (Section.LoadAddress + Offset);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000815 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
816 if (SignExtend32<24>(delta) != delta)
817 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
818 // Generates a 'bl <address>' instruction
819 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
820 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000821 case ELF::R_PPC64_REL32: {
Adhemerval Zanella1ae22482013-01-09 17:08:15 +0000822 uint64_t FinalAddress = (Section.LoadAddress + Offset);
823 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
824 if (SignExtend32<32>(delta) != delta)
825 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
826 writeInt32BE(LocalAddress, delta);
827 } break;
Adhemerval Zanellae8bd03d2013-05-06 17:21:23 +0000828 case ELF::R_PPC64_REL64: {
829 uint64_t FinalAddress = (Section.LoadAddress + Offset);
830 uint64_t Delta = Value - FinalAddress + Addend;
831 writeInt64BE(LocalAddress, Delta);
832 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000833 case ELF::R_PPC64_ADDR64:
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000834 writeInt64BE(LocalAddress, Value + Addend);
835 break;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000836 }
837}
838
Richard Sandifordca044082013-05-03 14:15:35 +0000839void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000840 uint64_t Offset, uint64_t Value,
841 uint32_t Type, int64_t Addend) {
Richard Sandifordca044082013-05-03 14:15:35 +0000842 uint8_t *LocalAddress = Section.Address + Offset;
843 switch (Type) {
844 default:
845 llvm_unreachable("Relocation type not implemented yet!");
846 break;
847 case ELF::R_390_PC16DBL:
848 case ELF::R_390_PLT16DBL: {
849 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
850 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
851 writeInt16BE(LocalAddress, Delta / 2);
852 break;
853 }
854 case ELF::R_390_PC32DBL:
855 case ELF::R_390_PLT32DBL: {
856 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
857 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
858 writeInt32BE(LocalAddress, Delta / 2);
859 break;
860 }
861 case ELF::R_390_PC32: {
862 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
863 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
864 writeInt32BE(LocalAddress, Delta);
865 break;
866 }
867 case ELF::R_390_64:
868 writeInt64BE(LocalAddress, Value + Addend);
869 break;
870 }
871}
872
Andrew Kaylor5f3a9982013-08-19 19:38:06 +0000873// The target location for the relocation is described by RE.SectionID and
874// RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
875// SectionEntry has three members describing its location.
876// SectionEntry::Address is the address at which the section has been loaded
877// into memory in the current (host) process. SectionEntry::LoadAddress is the
878// address that the section will have in the target process.
879// SectionEntry::ObjAddress is the address of the bits for this section in the
880// original emitted object image (also in the current address space).
881//
882// Relocations will be applied as if the section were loaded at
883// SectionEntry::LoadAddress, but they will be applied at an address based
884// on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
885// Target memory contents if they are required for value calculations.
886//
887// The Value parameter here is the load address of the symbol for the
888// relocation to be applied. For relocations which refer to symbols in the
889// current object Value will be the LoadAddress of the section in which
890// the symbol resides (RE.Addend provides additional information about the
891// symbol location). For external symbols, Value will be the address of the
892// symbol in the target address space.
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000893void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
Andrew Kaylor5f3a9982013-08-19 19:38:06 +0000894 uint64_t Value) {
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000895 const SectionEntry &Section = Sections[RE.SectionID];
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000896 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
897 RE.SymOffset);
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000898}
899
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000900void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000901 uint64_t Offset, uint64_t Value,
902 uint32_t Type, int64_t Addend,
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000903 uint64_t SymOffset) {
Eli Bendersky4c647582012-01-16 08:56:09 +0000904 switch (Arch) {
905 case Triple::x86_64:
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000906 resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
Eli Bendersky4c647582012-01-16 08:56:09 +0000907 break;
908 case Triple::x86:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000909 resolveX86Relocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000910 (uint32_t)(Addend & 0xffffffffL));
Eli Bendersky4c647582012-01-16 08:56:09 +0000911 break;
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000912 case Triple::aarch64:
Christian Pirker99974c72014-03-26 14:57:32 +0000913 case Triple::aarch64_be:
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000914 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
915 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000916 case Triple::arm: // Fall through.
Christian Pirker2a111602014-03-28 14:35:30 +0000917 case Triple::armeb:
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000918 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000919 case Triple::thumbeb:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000920 resolveARMRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000921 (uint32_t)(Addend & 0xffffffffL));
Eli Bendersky4c647582012-01-16 08:56:09 +0000922 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000923 case Triple::mips: // Fall through.
Akira Hatanaka111174b2012-08-17 21:28:04 +0000924 case Triple::mipsel:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000925 resolveMIPSRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL),
926 Type, (uint32_t)(Addend & 0xffffffffL));
Akira Hatanaka111174b2012-08-17 21:28:04 +0000927 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000928 case Triple::ppc64: // Fall through.
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000929 case Triple::ppc64le:
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000930 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000931 break;
Richard Sandifordca044082013-05-03 14:15:35 +0000932 case Triple::systemz:
933 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
934 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000935 default:
936 llvm_unreachable("Unsupported CPU type!");
Eli Bendersky4c647582012-01-16 08:56:09 +0000937 }
938}
939
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000940relocation_iterator RuntimeDyldELF::processRelocationRef(
941 unsigned SectionID, relocation_iterator RelI, ObjectImage &Obj,
942 ObjSectionToIDMap &ObjSectionToID, const SymbolTableMap &Symbols,
943 StubMap &Stubs) {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000944 uint64_t RelType;
Juergen Ributzka046709f2014-03-21 07:26:41 +0000945 Check(RelI->getType(RelType));
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000946 int64_t Addend;
Juergen Ributzka046709f2014-03-21 07:26:41 +0000947 Check(getELFRelocationAddend(*RelI, Addend));
948 symbol_iterator Symbol = RelI->getSymbol();
Eli Bendersky667b8792012-05-01 10:41:12 +0000949
950 // Obtain the symbol name which is referenced in the relocation
951 StringRef TargetName;
Rafael Espindola75954472013-06-05 02:55:01 +0000952 if (Symbol != Obj.end_symbols())
953 Symbol->getName(TargetName);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000954 DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend
955 << " TargetName: " << TargetName << "\n");
Eli Bendersky667b8792012-05-01 10:41:12 +0000956 RelocationValueRef Value;
957 // First search for the symbol in the local symbol table
Rafael Espindola75954472013-06-05 02:55:01 +0000958 SymbolTableMap::const_iterator lsi = Symbols.end();
959 SymbolRef::Type SymType = SymbolRef::ST_Unknown;
960 if (Symbol != Obj.end_symbols()) {
961 lsi = Symbols.find(TargetName.data());
962 Symbol->getType(SymType);
963 }
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000964 if (lsi != Symbols.end()) {
965 Value.SectionID = lsi->second.first;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000966 Value.Offset = lsi->second.second;
Ulrich Weigand78e97652013-04-05 13:29:04 +0000967 Value.Addend = lsi->second.second + Addend;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000968 } else {
Eli Bendersky667b8792012-05-01 10:41:12 +0000969 // Search for the symbol in the global symbol table
Rafael Espindola75954472013-06-05 02:55:01 +0000970 SymbolTableMap::const_iterator gsi = GlobalSymbolTable.end();
971 if (Symbol != Obj.end_symbols())
972 gsi = GlobalSymbolTable.find(TargetName.data());
Eli Benderskyfc079082012-05-01 06:58:59 +0000973 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000974 Value.SectionID = gsi->second.first;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000975 Value.Offset = gsi->second.second;
Ulrich Weigand78e97652013-04-05 13:29:04 +0000976 Value.Addend = gsi->second.second + Addend;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000977 } else {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000978 switch (SymType) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000979 case SymbolRef::ST_Debug: {
980 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
981 // and can be changed by another developers. Maybe best way is add
982 // a new symbol type ST_Section to SymbolRef and use it.
983 section_iterator si(Obj.end_sections());
984 Symbol->getSection(si);
985 if (si == Obj.end_sections())
986 llvm_unreachable("Symbol section not found, bad object file format!");
987 DEBUG(dbgs() << "\t\tThis is section symbol\n");
988 // Default to 'true' in case isText fails (though it never does).
989 bool isCode = true;
990 si->isText(isCode);
991 Value.SectionID = findOrEmitSection(Obj, (*si), isCode, ObjSectionToID);
992 Value.Addend = Addend;
993 break;
994 }
995 case SymbolRef::ST_Data:
996 case SymbolRef::ST_Unknown: {
997 Value.SymbolName = TargetName.data();
998 Value.Addend = Addend;
Richard Mittonad6d3492013-08-16 18:54:26 +0000999
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001000 // Absolute relocations will have a zero symbol ID (STN_UNDEF), which
1001 // will manifest here as a NULL symbol name.
1002 // We can set this as a valid (but empty) symbol name, and rely
1003 // on addRelocationForSymbol to handle this.
1004 if (!Value.SymbolName)
1005 Value.SymbolName = "";
1006 break;
1007 }
1008 default:
1009 llvm_unreachable("Unresolved symbol type!");
1010 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001011 }
1012 }
Eli Bendersky4c647582012-01-16 08:56:09 +00001013 }
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001014 uint64_t Offset;
Juergen Ributzka046709f2014-03-21 07:26:41 +00001015 Check(RelI->getOffset(Offset));
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001016
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001017 DEBUG(dbgs() << "\t\tSectionID: " << SectionID << " Offset: " << Offset
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001018 << "\n");
Tim Northovere19bed72014-07-23 12:32:47 +00001019 if ((Arch == Triple::aarch64 || Arch == Triple::aarch64_be) &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001020 (RelType == ELF::R_AARCH64_CALL26 || RelType == ELF::R_AARCH64_JUMP26)) {
Tim Northover37cde972013-05-04 20:14:09 +00001021 // This is an AArch64 branch relocation, need to use a stub function.
1022 DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
1023 SectionEntry &Section = Sections[SectionID];
1024
1025 // Look for an existing stub.
1026 StubMap::const_iterator i = Stubs.find(Value);
1027 if (i != Stubs.end()) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001028 resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second,
1029 RelType, 0);
Tim Northover37cde972013-05-04 20:14:09 +00001030 DEBUG(dbgs() << " Stub function found\n");
1031 } else {
1032 // Create a new stub function.
1033 DEBUG(dbgs() << " Create a new stub function\n");
1034 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001035 uint8_t *StubTargetAddr =
1036 createStubFunction(Section.Address + Section.StubOffset);
Tim Northover37cde972013-05-04 20:14:09 +00001037
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001038 RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section.Address,
Tim Northover37cde972013-05-04 20:14:09 +00001039 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001040 RelocationEntry REmovk_g2(SectionID, StubTargetAddr - Section.Address + 4,
Tim Northover37cde972013-05-04 20:14:09 +00001041 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001042 RelocationEntry REmovk_g1(SectionID, StubTargetAddr - Section.Address + 8,
Tim Northover37cde972013-05-04 20:14:09 +00001043 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
1044 RelocationEntry REmovk_g0(SectionID,
1045 StubTargetAddr - Section.Address + 12,
1046 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
1047
1048 if (Value.SymbolName) {
1049 addRelocationForSymbol(REmovz_g3, Value.SymbolName);
1050 addRelocationForSymbol(REmovk_g2, Value.SymbolName);
1051 addRelocationForSymbol(REmovk_g1, Value.SymbolName);
1052 addRelocationForSymbol(REmovk_g0, Value.SymbolName);
1053 } else {
1054 addRelocationForSection(REmovz_g3, Value.SectionID);
1055 addRelocationForSection(REmovk_g2, Value.SectionID);
1056 addRelocationForSection(REmovk_g1, Value.SectionID);
1057 addRelocationForSection(REmovk_g0, Value.SectionID);
1058 }
1059 resolveRelocation(Section, Offset,
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001060 (uint64_t)Section.Address + Section.StubOffset, RelType,
1061 0);
Tim Northover37cde972013-05-04 20:14:09 +00001062 Section.StubOffset += getMaxStubSize();
1063 }
1064 } else if (Arch == Triple::arm &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001065 (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL ||
1066 RelType == ELF::R_ARM_JUMP24)) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001067 // This is an ARM branch relocation, need to use a stub function.
1068 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001069 SectionEntry &Section = Sections[SectionID];
Eli Bendersky4c647582012-01-16 08:56:09 +00001070
Eric Christopherc33f6222012-10-23 17:19:15 +00001071 // Look for an existing stub.
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001072 StubMap::const_iterator i = Stubs.find(Value);
1073 if (i != Stubs.end()) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001074 resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second,
1075 RelType, 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001076 DEBUG(dbgs() << " Stub function found\n");
1077 } else {
1078 // Create a new stub function.
1079 DEBUG(dbgs() << " Create a new stub function\n");
1080 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001081 uint8_t *StubTargetAddr =
1082 createStubFunction(Section.Address + Section.StubOffset);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001083 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Tim Northover3b684d82013-05-28 19:48:19 +00001084 ELF::R_ARM_PRIVATE_0, Value.Addend);
Eli Bendersky667b8792012-05-01 10:41:12 +00001085 if (Value.SymbolName)
1086 addRelocationForSymbol(RE, Value.SymbolName);
1087 else
1088 addRelocationForSection(RE, Value.SectionID);
1089
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001090 resolveRelocation(Section, Offset,
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001091 (uint64_t)Section.Address + Section.StubOffset, RelType,
1092 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001093 Section.StubOffset += getMaxStubSize();
1094 }
Akira Hatanakaa667aad2012-12-03 23:12:19 +00001095 } else if ((Arch == Triple::mipsel || Arch == Triple::mips) &&
1096 RelType == ELF::R_MIPS_26) {
Akira Hatanaka111174b2012-08-17 21:28:04 +00001097 // This is an Mips branch relocation, need to use a stub function.
1098 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001099 SectionEntry &Section = Sections[SectionID];
1100 uint8_t *Target = Section.Address + Offset;
Akira Hatanaka111174b2012-08-17 21:28:04 +00001101 uint32_t *TargetAddress = (uint32_t *)Target;
1102
1103 // Extract the addend from the instruction.
1104 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
1105
1106 Value.Addend += Addend;
1107
1108 // Look up for existing stub.
1109 StubMap::const_iterator i = Stubs.find(Value);
1110 if (i != Stubs.end()) {
Petar Jovanovic45115f82013-11-19 21:56:00 +00001111 RelocationEntry RE(SectionID, Offset, RelType, i->second);
1112 addRelocationForSection(RE, SectionID);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001113 DEBUG(dbgs() << " Stub function found\n");
1114 } else {
1115 // Create a new stub function.
1116 DEBUG(dbgs() << " Create a new stub function\n");
1117 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001118 uint8_t *StubTargetAddr =
1119 createStubFunction(Section.Address + Section.StubOffset);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001120
1121 // Creating Hi and Lo relocations for the filled stub instructions.
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001122 RelocationEntry REHi(SectionID, StubTargetAddr - Section.Address,
Akira Hatanaka2e236242013-07-24 01:58:40 +00001123 ELF::R_MIPS_UNUSED1, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001124 RelocationEntry RELo(SectionID, StubTargetAddr - Section.Address + 4,
Akira Hatanaka2e236242013-07-24 01:58:40 +00001125 ELF::R_MIPS_UNUSED2, Value.Addend);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001126
1127 if (Value.SymbolName) {
1128 addRelocationForSymbol(REHi, Value.SymbolName);
1129 addRelocationForSymbol(RELo, Value.SymbolName);
1130 } else {
1131 addRelocationForSection(REHi, Value.SectionID);
1132 addRelocationForSection(RELo, Value.SectionID);
1133 }
1134
Petar Jovanovic45115f82013-11-19 21:56:00 +00001135 RelocationEntry RE(SectionID, Offset, RelType, Section.StubOffset);
1136 addRelocationForSection(RE, SectionID);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001137 Section.StubOffset += getMaxStubSize();
1138 }
Bill Schmidt0a9170d2013-07-26 01:35:43 +00001139 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001140 if (RelType == ELF::R_PPC64_REL24) {
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001141 // Determine ABI variant in use for this object.
1142 unsigned AbiVariant;
1143 Obj.getObjectFile()->getPlatformFlags(AbiVariant);
1144 AbiVariant &= ELF::EF_PPC64_ABI;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001145 // A PPC branch relocation will need a stub function if the target is
1146 // an external symbol (Symbol::ST_Unknown) or if the target address
1147 // is not within the signed 24-bits branch address.
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001148 SectionEntry &Section = Sections[SectionID];
1149 uint8_t *Target = Section.Address + Offset;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001150 bool RangeOverflow = false;
1151 if (SymType != SymbolRef::ST_Unknown) {
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001152 if (AbiVariant != 2) {
1153 // In the ELFv1 ABI, a function call may point to the .opd entry,
1154 // so the final symbol value is calculated based on the relocation
1155 // values in the .opd section.
1156 findOPDEntrySection(Obj, ObjSectionToID, Value);
1157 } else {
1158 // In the ELFv2 ABI, a function symbol may provide a local entry
1159 // point, which must be used for direct calls.
1160 uint8_t SymOther;
1161 Symbol->getOther(SymOther);
1162 Value.Addend += ELF::decodePPC64LocalEntryOffset(SymOther);
1163 }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001164 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
1165 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
1166 // If it is within 24-bits branch range, just set the branch target
1167 if (SignExtend32<24>(delta) == delta) {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001168 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001169 if (Value.SymbolName)
1170 addRelocationForSymbol(RE, Value.SymbolName);
1171 else
1172 addRelocationForSection(RE, Value.SectionID);
1173 } else {
1174 RangeOverflow = true;
1175 }
1176 }
1177 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
1178 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
1179 // larger than 24-bits.
1180 StubMap::const_iterator i = Stubs.find(Value);
1181 if (i != Stubs.end()) {
1182 // Symbol function stub already created, just relocate to it
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001183 resolveRelocation(Section, Offset,
Andrew Kaylorfb05a502012-11-02 19:45:23 +00001184 (uint64_t)Section.Address + i->second, RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001185 DEBUG(dbgs() << " Stub function found\n");
1186 } else {
1187 // Create a new stub function.
1188 DEBUG(dbgs() << " Create a new stub function\n");
1189 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001190 uint8_t *StubTargetAddr =
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001191 createStubFunction(Section.Address + Section.StubOffset,
1192 AbiVariant);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001193 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001194 ELF::R_PPC64_ADDR64, Value.Addend);
1195
1196 // Generates the 64-bits address loads as exemplified in section
Ulrich Weigand32626012014-06-20 18:17:56 +00001197 // 4.5.1 in PPC64 ELF ABI. Note that the relocations need to
1198 // apply to the low part of the instructions, so we have to update
1199 // the offset according to the target endianness.
1200 uint64_t StubRelocOffset = StubTargetAddr - Section.Address;
1201 if (!IsTargetLittleEndian)
1202 StubRelocOffset += 2;
1203
1204 RelocationEntry REhst(SectionID, StubRelocOffset + 0,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001205 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
Ulrich Weigand32626012014-06-20 18:17:56 +00001206 RelocationEntry REhr(SectionID, StubRelocOffset + 4,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001207 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
Ulrich Weigand32626012014-06-20 18:17:56 +00001208 RelocationEntry REh(SectionID, StubRelocOffset + 12,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001209 ELF::R_PPC64_ADDR16_HI, Value.Addend);
Ulrich Weigand32626012014-06-20 18:17:56 +00001210 RelocationEntry REl(SectionID, StubRelocOffset + 16,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001211 ELF::R_PPC64_ADDR16_LO, Value.Addend);
1212
1213 if (Value.SymbolName) {
1214 addRelocationForSymbol(REhst, Value.SymbolName);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001215 addRelocationForSymbol(REhr, Value.SymbolName);
1216 addRelocationForSymbol(REh, Value.SymbolName);
1217 addRelocationForSymbol(REl, Value.SymbolName);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001218 } else {
1219 addRelocationForSection(REhst, Value.SectionID);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001220 addRelocationForSection(REhr, Value.SectionID);
1221 addRelocationForSection(REh, Value.SectionID);
1222 addRelocationForSection(REl, Value.SectionID);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001223 }
1224
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001225 resolveRelocation(Section, Offset,
Andrew Kaylorfb05a502012-11-02 19:45:23 +00001226 (uint64_t)Section.Address + Section.StubOffset,
1227 RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001228 Section.StubOffset += getMaxStubSize();
1229 }
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001230 if (SymType == SymbolRef::ST_Unknown) {
Ulrich Weigandfa84ac92014-03-11 15:26:27 +00001231 // Restore the TOC for external calls
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001232 if (AbiVariant == 2)
1233 writeInt32BE(Target + 4, 0xE8410018); // ld r2,28(r1)
1234 else
1235 writeInt32BE(Target + 4, 0xE8410028); // ld r2,40(r1)
1236 }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001237 }
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +00001238 } else if (RelType == ELF::R_PPC64_TOC16 ||
1239 RelType == ELF::R_PPC64_TOC16_DS ||
1240 RelType == ELF::R_PPC64_TOC16_LO ||
1241 RelType == ELF::R_PPC64_TOC16_LO_DS ||
1242 RelType == ELF::R_PPC64_TOC16_HI ||
1243 RelType == ELF::R_PPC64_TOC16_HA) {
1244 // These relocations are supposed to subtract the TOC address from
1245 // the final value. This does not fit cleanly into the RuntimeDyld
1246 // scheme, since there may be *two* sections involved in determining
1247 // the relocation value (the section of the symbol refered to by the
1248 // relocation, and the TOC section associated with the current module).
1249 //
1250 // Fortunately, these relocations are currently only ever generated
1251 // refering to symbols that themselves reside in the TOC, which means
1252 // that the two sections are actually the same. Thus they cancel out
1253 // and we can immediately resolve the relocation right now.
1254 switch (RelType) {
1255 case ELF::R_PPC64_TOC16: RelType = ELF::R_PPC64_ADDR16; break;
1256 case ELF::R_PPC64_TOC16_DS: RelType = ELF::R_PPC64_ADDR16_DS; break;
1257 case ELF::R_PPC64_TOC16_LO: RelType = ELF::R_PPC64_ADDR16_LO; break;
1258 case ELF::R_PPC64_TOC16_LO_DS: RelType = ELF::R_PPC64_ADDR16_LO_DS; break;
1259 case ELF::R_PPC64_TOC16_HI: RelType = ELF::R_PPC64_ADDR16_HI; break;
1260 case ELF::R_PPC64_TOC16_HA: RelType = ELF::R_PPC64_ADDR16_HA; break;
1261 default: llvm_unreachable("Wrong relocation type.");
1262 }
1263
1264 RelocationValueRef TOCValue;
1265 findPPC64TOCSection(Obj, ObjSectionToID, TOCValue);
1266 if (Value.SymbolName || Value.SectionID != TOCValue.SectionID)
1267 llvm_unreachable("Unsupported TOC relocation.");
1268 Value.Addend -= TOCValue.Addend;
1269 resolveRelocation(Sections[SectionID], Offset, Value.Addend, RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001270 } else {
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +00001271 // There are two ways to refer to the TOC address directly: either
1272 // via a ELF::R_PPC64_TOC relocation (where both symbol and addend are
1273 // ignored), or via any relocation that refers to the magic ".TOC."
1274 // symbols (in which case the addend is respected).
1275 if (RelType == ELF::R_PPC64_TOC) {
1276 RelType = ELF::R_PPC64_ADDR64;
1277 findPPC64TOCSection(Obj, ObjSectionToID, Value);
1278 } else if (TargetName == ".TOC.") {
1279 findPPC64TOCSection(Obj, ObjSectionToID, Value);
1280 Value.Addend += Addend;
1281 }
1282
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001283 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Richard Mittonad6d3492013-08-16 18:54:26 +00001284
1285 if (Value.SymbolName)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001286 addRelocationForSymbol(RE, Value.SymbolName);
1287 else
1288 addRelocationForSection(RE, Value.SectionID);
1289 }
Richard Sandifordca044082013-05-03 14:15:35 +00001290 } else if (Arch == Triple::systemz &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001291 (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) {
Richard Sandifordca044082013-05-03 14:15:35 +00001292 // Create function stubs for both PLT and GOT references, regardless of
1293 // whether the GOT reference is to data or code. The stub contains the
1294 // full address of the symbol, as needed by GOT references, and the
1295 // executable part only adds an overhead of 8 bytes.
1296 //
1297 // We could try to conserve space by allocating the code and data
1298 // parts of the stub separately. However, as things stand, we allocate
1299 // a stub for every relocation, so using a GOT in JIT code should be
1300 // no less space efficient than using an explicit constant pool.
1301 DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
1302 SectionEntry &Section = Sections[SectionID];
1303
1304 // Look for an existing stub.
1305 StubMap::const_iterator i = Stubs.find(Value);
1306 uintptr_t StubAddress;
1307 if (i != Stubs.end()) {
1308 StubAddress = uintptr_t(Section.Address) + i->second;
1309 DEBUG(dbgs() << " Stub function found\n");
1310 } else {
1311 // Create a new stub function.
1312 DEBUG(dbgs() << " Create a new stub function\n");
1313
1314 uintptr_t BaseAddress = uintptr_t(Section.Address);
1315 uintptr_t StubAlignment = getStubAlignment();
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001316 StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) &
1317 -StubAlignment;
Richard Sandifordca044082013-05-03 14:15:35 +00001318 unsigned StubOffset = StubAddress - BaseAddress;
1319
1320 Stubs[Value] = StubOffset;
1321 createStubFunction((uint8_t *)StubAddress);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001322 RelocationEntry RE(SectionID, StubOffset + 8, ELF::R_390_64,
1323 Value.Addend - Addend);
Richard Sandifordca044082013-05-03 14:15:35 +00001324 if (Value.SymbolName)
1325 addRelocationForSymbol(RE, Value.SymbolName);
1326 else
1327 addRelocationForSection(RE, Value.SectionID);
1328 Section.StubOffset = StubOffset + getMaxStubSize();
1329 }
1330
1331 if (RelType == ELF::R_390_GOTENT)
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001332 resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL,
1333 Addend);
Richard Sandifordca044082013-05-03 14:15:35 +00001334 else
1335 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001336 } else if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_PLT32) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001337 // The way the PLT relocations normally work is that the linker allocates
1338 // the
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001339 // PLT and this relocation makes a PC-relative call into the PLT. The PLT
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001340 // entry will then jump to an address provided by the GOT. On first call,
1341 // the
1342 // GOT address will point back into PLT code that resolves the symbol. After
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001343 // the first call, the GOT entry points to the actual function.
1344 //
1345 // For local functions we're ignoring all of that here and just replacing
1346 // the PLT32 relocation type with PC32, which will translate the relocation
1347 // into a PC-relative call directly to the function. For external symbols we
1348 // can't be sure the function will be within 2^32 bytes of the call site, so
1349 // we need to create a stub, which calls into the GOT. This case is
1350 // equivalent to the usual PLT implementation except that we use the stub
1351 // mechanism in RuntimeDyld (which puts stubs at the end of the section)
1352 // rather than allocating a PLT section.
1353 if (Value.SymbolName) {
1354 // This is a call to an external function.
1355 // Look for an existing stub.
1356 SectionEntry &Section = Sections[SectionID];
1357 StubMap::const_iterator i = Stubs.find(Value);
1358 uintptr_t StubAddress;
1359 if (i != Stubs.end()) {
1360 StubAddress = uintptr_t(Section.Address) + i->second;
1361 DEBUG(dbgs() << " Stub function found\n");
1362 } else {
1363 // Create a new stub function (equivalent to a PLT entry).
1364 DEBUG(dbgs() << " Create a new stub function\n");
1365
1366 uintptr_t BaseAddress = uintptr_t(Section.Address);
1367 uintptr_t StubAlignment = getStubAlignment();
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001368 StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) &
1369 -StubAlignment;
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001370 unsigned StubOffset = StubAddress - BaseAddress;
1371 Stubs[Value] = StubOffset;
1372 createStubFunction((uint8_t *)StubAddress);
1373
1374 // Create a GOT entry for the external function.
1375 GOTEntries.push_back(Value);
1376
1377 // Make our stub function a relative call to the GOT entry.
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001378 RelocationEntry RE(SectionID, StubOffset + 2, ELF::R_X86_64_GOTPCREL,
1379 -4);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001380 addRelocationForSymbol(RE, Value.SymbolName);
1381
1382 // Bump our stub offset counter
1383 Section.StubOffset = StubOffset + getMaxStubSize();
1384 }
1385
1386 // Make the target call a call into the stub table.
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001387 resolveRelocation(Section, Offset, StubAddress, ELF::R_X86_64_PC32,
1388 Addend);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001389 } else {
1390 RelocationEntry RE(SectionID, Offset, ELF::R_X86_64_PC32, Value.Addend,
1391 Value.Offset);
1392 addRelocationForSection(RE, Value.SectionID);
1393 }
Eli Bendersky667b8792012-05-01 10:41:12 +00001394 } else {
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001395 if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_GOTPCREL) {
1396 GOTEntries.push_back(Value);
1397 }
1398 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset);
Eli Bendersky667b8792012-05-01 10:41:12 +00001399 if (Value.SymbolName)
1400 addRelocationForSymbol(RE, Value.SymbolName);
1401 else
1402 addRelocationForSection(RE, Value.SectionID);
1403 }
Juergen Ributzka046709f2014-03-21 07:26:41 +00001404 return ++RelI;
Jim Grosbacheff0a402012-01-16 22:26:39 +00001405}
1406
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001407void RuntimeDyldELF::updateGOTEntries(StringRef Name, uint64_t Addr) {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001408
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001409 SmallVectorImpl<std::pair<SID, GOTRelocations>>::iterator it;
1410 SmallVectorImpl<std::pair<SID, GOTRelocations>>::iterator end = GOTs.end();
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001411
1412 for (it = GOTs.begin(); it != end; ++it) {
1413 GOTRelocations &GOTEntries = it->second;
1414 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
Craig Topper353eda42014-04-24 06:44:33 +00001415 if (GOTEntries[i].SymbolName != nullptr &&
1416 GOTEntries[i].SymbolName == Name) {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001417 GOTEntries[i].Offset = Addr;
1418 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001419 }
1420 }
1421}
1422
1423size_t RuntimeDyldELF::getGOTEntrySize() {
1424 // We don't use the GOT in all of these cases, but it's essentially free
1425 // to put them all here.
1426 size_t Result = 0;
1427 switch (Arch) {
1428 case Triple::x86_64:
1429 case Triple::aarch64:
James Molloybd2ffa02014-04-30 10:15:41 +00001430 case Triple::aarch64_be:
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001431 case Triple::ppc64:
1432 case Triple::ppc64le:
1433 case Triple::systemz:
1434 Result = sizeof(uint64_t);
1435 break;
1436 case Triple::x86:
1437 case Triple::arm:
1438 case Triple::thumb:
1439 case Triple::mips:
1440 case Triple::mipsel:
1441 Result = sizeof(uint32_t);
1442 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001443 default:
1444 llvm_unreachable("Unsupported CPU type!");
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001445 }
1446 return Result;
1447}
1448
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001449uint64_t RuntimeDyldELF::findGOTEntry(uint64_t LoadAddress, uint64_t Offset) {
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001450
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001451 const size_t GOTEntrySize = getGOTEntrySize();
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001452
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001453 SmallVectorImpl<std::pair<SID, GOTRelocations>>::const_iterator it;
1454 SmallVectorImpl<std::pair<SID, GOTRelocations>>::const_iterator end =
1455 GOTs.end();
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001456
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001457 int GOTIndex = -1;
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001458 for (it = GOTs.begin(); it != end; ++it) {
1459 SID GOTSectionID = it->first;
1460 const GOTRelocations &GOTEntries = it->second;
1461
1462 // Find the matching entry in our vector.
1463 uint64_t SymbolOffset = 0;
1464 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
Craig Topper353eda42014-04-24 06:44:33 +00001465 if (!GOTEntries[i].SymbolName) {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001466 if (getSectionLoadAddress(GOTEntries[i].SectionID) == LoadAddress &&
1467 GOTEntries[i].Offset == Offset) {
1468 GOTIndex = i;
1469 SymbolOffset = GOTEntries[i].Offset;
1470 break;
1471 }
1472 } else {
1473 // GOT entries for external symbols use the addend as the address when
1474 // the external symbol has been resolved.
1475 if (GOTEntries[i].Offset == LoadAddress) {
1476 GOTIndex = i;
1477 // Don't use the Addend here. The relocation handler will use it.
1478 break;
1479 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001480 }
1481 }
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001482
1483 if (GOTIndex != -1) {
1484 if (GOTEntrySize == sizeof(uint64_t)) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001485 uint64_t *LocalGOTAddr = (uint64_t *)getSectionAddress(GOTSectionID);
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001486 // Fill in this entry with the address of the symbol being referenced.
1487 LocalGOTAddr[GOTIndex] = LoadAddress + SymbolOffset;
1488 } else {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001489 uint32_t *LocalGOTAddr = (uint32_t *)getSectionAddress(GOTSectionID);
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001490 // Fill in this entry with the address of the symbol being referenced.
1491 LocalGOTAddr[GOTIndex] = (uint32_t)(LoadAddress + SymbolOffset);
1492 }
1493
1494 // Calculate the load address of this entry
1495 return getSectionLoadAddress(GOTSectionID) + (GOTIndex * GOTEntrySize);
1496 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001497 }
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001498
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001499 assert(GOTIndex != -1 && "Unable to find requested GOT entry.");
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001500 return 0;
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001501}
1502
Lang Hames36072da2014-05-12 21:39:59 +00001503void RuntimeDyldELF::finalizeLoad(ObjectImage &ObjImg,
1504 ObjSectionToIDMap &SectionMap) {
Andrew Kaylor7bb13442013-10-11 21:25:48 +00001505 // If necessary, allocate the global offset table
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001506 if (MemMgr) {
1507 // Allocate the GOT if necessary
1508 size_t numGOTEntries = GOTEntries.size();
1509 if (numGOTEntries != 0) {
1510 // Allocate memory for the section
1511 unsigned SectionID = Sections.size();
1512 size_t TotalSize = numGOTEntries * getGOTEntrySize();
1513 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, getGOTEntrySize(),
1514 SectionID, ".got", false);
1515 if (!Addr)
1516 report_fatal_error("Unable to allocate memory for GOT!");
1517
1518 GOTs.push_back(std::make_pair(SectionID, GOTEntries));
1519 Sections.push_back(SectionEntry(".got", Addr, TotalSize, 0));
1520 // For now, initialize all GOT entries to zero. We'll fill them in as
1521 // needed when GOT-based relocations are applied.
1522 memset(Addr, 0, TotalSize);
1523 }
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001524 } else {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001525 report_fatal_error("Unable to allocate memory for GOT!");
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001526 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +00001527
1528 // Look for and record the EH frame section.
1529 ObjSectionToIDMap::iterator i, e;
1530 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
1531 const SectionRef &Section = i->first;
1532 StringRef Name;
1533 Section.getName(Name);
1534 if (Name == ".eh_frame") {
1535 UnregisteredEHFrameSections.push_back(i->second);
1536 break;
1537 }
1538 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001539}
1540
Andrew Kayloradc70562012-10-02 21:18:39 +00001541bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
1542 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
1543 return false;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001544 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic,
1545 strlen(ELF::ElfMagic))) == 0;
Eli Bendersky4c647582012-01-16 08:56:09 +00001546}
Lang Hames173c69f2014-01-08 04:09:09 +00001547
1548bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile *Obj) const {
1549 return Obj->isELF();
1550}
1551
Eli Bendersky4c647582012-01-16 08:56:09 +00001552} // namespace llvm