blob: 803e5cb781cc033965de2d2b87811138ffa9c061 [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 Blaikieed9709d2014-09-03 19:48:09 +000080 ELFObjectImage(std::unique_ptr<ObjectBuffer> Input,
81 std::unique_ptr<DyldELFObject<ELFT>> Obj)
82 : ObjectImageCommon(std::move(Input), std::move(Obj)), Registered(false) {
83 }
Preston Gurdcc31af92012-04-16 22:12:58 +000084
Juergen Ributzka7608dc02014-03-21 20:28:42 +000085 virtual ~ELFObjectImage() {
86 if (Registered)
87 deregisterWithDebugger();
88 }
Preston Gurdcc31af92012-04-16 22:12:58 +000089
Juergen Ributzka7608dc02014-03-21 20:28:42 +000090 // Subclasses can override these methods to update the image with loaded
91 // addresses for sections and common symbols
92 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr) override {
David Blaikie7a1e7752014-04-29 21:52:46 +000093 static_cast<DyldELFObject<ELFT>*>(getObjectFile())
94 ->updateSectionAddress(Sec, Addr);
Juergen Ributzka7608dc02014-03-21 20:28:42 +000095 }
Preston Gurdcc31af92012-04-16 22:12:58 +000096
Juergen Ributzka7608dc02014-03-21 20:28:42 +000097 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr) override {
David Blaikie7a1e7752014-04-29 21:52:46 +000098 static_cast<DyldELFObject<ELFT>*>(getObjectFile())
99 ->updateSymbolAddress(Sym, Addr);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000100 }
Preston Gurdcc31af92012-04-16 22:12:58 +0000101
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000102 void registerWithDebugger() override {
103 JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
104 Registered = true;
105 }
106 void deregisterWithDebugger() override {
107 JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer);
108 }
Preston Gurdcc31af92012-04-16 22:12:58 +0000109};
110
Andrew Kayloradc70562012-10-02 21:18:39 +0000111// The MemoryBuffer passed into this constructor is just a wrapper around the
112// actual memory. Ultimately, the Binary parent class will take ownership of
113// this MemoryBuffer object but not the underlying memory.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000114template <class ELFT>
Rafael Espindola48af1c22014-08-19 18:44:46 +0000115DyldELFObject<ELFT>::DyldELFObject(MemoryBufferRef Wrapper, std::error_code &EC)
116 : ELFObjectFile<ELFT>(Wrapper, EC) {
Preston Gurdcc31af92012-04-16 22:12:58 +0000117 this->isDyldELFObject = true;
118}
119
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000120template <class ELFT>
David Blaikie7a1e7752014-04-29 21:52:46 +0000121DyldELFObject<ELFT>::DyldELFObject(std::unique_ptr<ObjectFile> UnderlyingFile,
Rafael Espindola48af1c22014-08-19 18:44:46 +0000122 MemoryBufferRef Wrapper, std::error_code &EC)
123 : ELFObjectFile<ELFT>(Wrapper, EC),
David Blaikie7a1e7752014-04-29 21:52:46 +0000124 UnderlyingFile(std::move(UnderlyingFile)) {
125 this->isDyldELFObject = true;
126}
127
128template <class ELFT>
Michael J. Spencer1a791612013-01-15 07:44:25 +0000129void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec,
130 uint64_t Addr) {
Preston Gurdcc31af92012-04-16 22:12:58 +0000131 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000132 Elf_Shdr *shdr =
133 const_cast<Elf_Shdr *>(reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
Preston Gurdcc31af92012-04-16 22:12:58 +0000134
135 // This assumes the address passed in matches the target address bitness
136 // The template-based type cast handles everything else.
137 shdr->sh_addr = static_cast<addr_type>(Addr);
138}
139
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000140template <class ELFT>
Michael J. Spencer1a791612013-01-15 07:44:25 +0000141void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef,
142 uint64_t Addr) {
Preston Gurdcc31af92012-04-16 22:12:58 +0000143
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000144 Elf_Sym *sym = const_cast<Elf_Sym *>(
145 ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl()));
Preston Gurdcc31af92012-04-16 22:12:58 +0000146
147 // This assumes the address passed in matches the target address bitness
148 // The template-based type cast handles everything else.
149 sym->st_value = static_cast<addr_type>(Addr);
150}
151
152} // namespace
153
Eli Bendersky4c647582012-01-16 08:56:09 +0000154namespace llvm {
155
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000156void RuntimeDyldELF::registerEHFrames() {
157 if (!MemMgr)
158 return;
159 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
160 SID EHFrameSID = UnregisteredEHFrameSections[i];
161 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
162 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
163 size_t EHFrameSize = Sections[EHFrameSID].Size;
164 MemMgr->registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
Andrew Kaylorc442a762013-10-16 00:14:21 +0000165 RegisteredEHFrameSections.push_back(EHFrameSID);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000166 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000167 UnregisteredEHFrameSections.clear();
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000168}
169
Andrew Kaylorc442a762013-10-16 00:14:21 +0000170void RuntimeDyldELF::deregisterEHFrames() {
171 if (!MemMgr)
172 return;
173 for (int i = 0, e = RegisteredEHFrameSections.size(); i != e; ++i) {
174 SID EHFrameSID = RegisteredEHFrameSections[i];
175 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
176 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
177 size_t EHFrameSize = Sections[EHFrameSID].Size;
178 MemMgr->deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
179 }
180 RegisteredEHFrameSections.clear();
181}
182
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000183ObjectImage *
David Blaikie7a1e7752014-04-29 21:52:46 +0000184RuntimeDyldELF::createObjectImageFromFile(std::unique_ptr<object::ObjectFile> ObjFile) {
Lang Hames173c69f2014-01-08 04:09:09 +0000185 if (!ObjFile)
Craig Topper353eda42014-04-24 06:44:33 +0000186 return nullptr;
Lang Hames173c69f2014-01-08 04:09:09 +0000187
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000188 std::error_code ec;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000189 MemoryBufferRef Buffer = ObjFile->getMemoryBufferRef();
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 Espindola48af1c22014-08-19 18:44:46 +0000194 std::move(ObjFile), 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 Espindola48af1c22014-08-19 18:44:46 +0000200 std::move(ObjFile), 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 Espindola48af1c22014-08-19 18:44:46 +0000204 std::move(ObjFile), 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 Espindola48af1c22014-08-19 18:44:46 +0000210 std::move(ObjFile), 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
David Blaikieed9709d2014-09-03 19:48:09 +0000217std::unique_ptr<ObjectImage>
218RuntimeDyldELF::createObjectImage(std::unique_ptr<ObjectBuffer> Buffer) {
Andrew Kayloradc70562012-10-02 21:18:39 +0000219 if (Buffer->getBufferSize() < ELF::EI_NIDENT)
220 llvm_unreachable("Unexpected ELF object size");
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000221 std::pair<unsigned char, unsigned char> Ident =
222 std::make_pair((uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS],
223 (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000224 std::error_code ec;
Preston Gurdcc31af92012-04-16 22:12:58 +0000225
Rafael Espindola48af1c22014-08-19 18:44:46 +0000226 MemoryBufferRef Buf = Buffer->getMemBuffer();
Rafael Espindola2e60ca92014-06-24 13:56:32 +0000227
Preston Gurdcc31af92012-04-16 22:12:58 +0000228 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
Reid Kleckner7aeb9052014-04-29 23:54:52 +0000229 auto Obj =
230 llvm::make_unique<DyldELFObject<ELFType<support::little, 4, false>>>(
Rafael Espindola48af1c22014-08-19 18:44:46 +0000231 Buf, ec);
David Blaikieed9709d2014-09-03 19:48:09 +0000232 return llvm::make_unique<
233 ELFObjectImage<ELFType<support::little, 4, false>>>(std::move(Buffer),
234 std::move(Obj));
235 }
236 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
David Blaikie7a1e7752014-04-29 21:52:46 +0000237 auto Obj =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000238 llvm::make_unique<DyldELFObject<ELFType<support::big, 4, false>>>(Buf,
239 ec);
David Blaikieed9709d2014-09-03 19:48:09 +0000240 return llvm::make_unique<ELFObjectImage<ELFType<support::big, 4, false>>>(
241 std::move(Buffer), std::move(Obj));
242 }
243 if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
Reid Kleckner7aeb9052014-04-29 23:54:52 +0000244 auto Obj = llvm::make_unique<DyldELFObject<ELFType<support::big, 8, true>>>(
Rafael Espindola48af1c22014-08-19 18:44:46 +0000245 Buf, ec);
David Blaikieed9709d2014-09-03 19:48:09 +0000246 return llvm::make_unique<ELFObjectImage<ELFType<support::big, 8, true>>>(
247 std::move(Buffer), std::move(Obj));
248 }
249 assert(Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB &&
250 "Unexpected ELF format");
251 auto Obj =
252 llvm::make_unique<DyldELFObject<ELFType<support::little, 8, true>>>(Buf,
253 ec);
254 return llvm::make_unique<ELFObjectImage<ELFType<support::little, 8, true>>>(
255 std::move(Buffer), std::move(Obj));
Preston Gurdcc31af92012-04-16 22:12:58 +0000256}
257
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000258RuntimeDyldELF::~RuntimeDyldELF() {}
Eli Bendersky4c647582012-01-16 08:56:09 +0000259
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000260void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000261 uint64_t Offset, uint64_t Value,
262 uint32_t Type, int64_t Addend,
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000263 uint64_t SymOffset) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000264 switch (Type) {
265 default:
266 llvm_unreachable("Relocation type not implemented yet!");
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000267 break;
Eli Bendersky4c647582012-01-16 08:56:09 +0000268 case ELF::R_X86_64_64: {
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000269 support::ulittle64_t::ref(Section.Address + Offset) = Value + Addend;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000270 DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000271 << format("%p\n", Section.Address + Offset));
Eli Bendersky4c647582012-01-16 08:56:09 +0000272 break;
273 }
274 case ELF::R_X86_64_32:
275 case ELF::R_X86_64_32S: {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000276 Value += Addend;
Andrew Kaylor8e87a752012-07-27 20:30:12 +0000277 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000278 (Type == ELF::R_X86_64_32S &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000279 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
Eli Bendersky4c647582012-01-16 08:56:09 +0000280 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000281 support::ulittle32_t::ref(Section.Address + Offset) = TruncatedAddr;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000282 DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at "
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000283 << format("%p\n", Section.Address + Offset));
Eli Bendersky4c647582012-01-16 08:56:09 +0000284 break;
285 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000286 case ELF::R_X86_64_GOTPCREL: {
287 // findGOTEntry returns the 'G + GOT' part of the relocation calculation
288 // based on the load/target address of the GOT (not the current/local addr).
289 uint64_t GOTAddr = findGOTEntry(Value, SymOffset);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000290 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);
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000297 support::ulittle32_t::ref(Section.Address + Offset) = TruncOffset;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000298 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
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000303 support::ulittle32_t::ref Placeholder(
304 (void *)(Section.ObjAddress + Offset));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000305 uint64_t FinalAddress = Section.LoadAddress + Offset;
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000306 int64_t RealOffset = Placeholder + Value + Addend - FinalAddress;
Andrew Kaylor8e87a752012-07-27 20:30:12 +0000307 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000308 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000309 support::ulittle32_t::ref(Section.Address + Offset) = TruncOffset;
Eli Bendersky4c647582012-01-16 08:56:09 +0000310 break;
311 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000312 case ELF::R_X86_64_PC64: {
313 // Get the placeholder value from the generated object since
314 // a previous relocation attempt may have overwritten the loaded version
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000315 support::ulittle64_t::ref Placeholder(
316 (void *)(Section.ObjAddress + Offset));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000317 uint64_t FinalAddress = Section.LoadAddress + Offset;
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000318 support::ulittle64_t::ref(Section.Address + Offset) =
319 Placeholder + Value + Addend - FinalAddress;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000320 break;
321 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000322 }
323}
324
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000325void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000326 uint64_t Offset, uint32_t Value,
327 uint32_t Type, int32_t Addend) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000328 switch (Type) {
Eli Bendersky4c647582012-01-16 08:56:09 +0000329 case ELF::R_386_32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000330 // Get the placeholder value from the generated object since
331 // a previous relocation attempt may have overwritten the loaded version
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000332 support::ulittle32_t::ref Placeholder(
333 (void *)(Section.ObjAddress + Offset));
334 support::ulittle32_t::ref(Section.Address + Offset) =
335 Placeholder + Value + Addend;
Eli Bendersky4c647582012-01-16 08:56:09 +0000336 break;
337 }
338 case ELF::R_386_PC32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000339 // Get the placeholder value from the generated object since
340 // a previous relocation attempt may have overwritten the loaded version
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000341 support::ulittle32_t::ref Placeholder(
342 (void *)(Section.ObjAddress + Offset));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000343 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000344 uint32_t RealOffset = Placeholder + Value + Addend - FinalAddress;
345 support::ulittle32_t::ref(Section.Address + Offset) = RealOffset;
Eli Bendersky4c647582012-01-16 08:56:09 +0000346 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000347 }
348 default:
349 // There are other relocation types, but it appears these are the
350 // only ones currently used by the LLVM ELF object writer
351 llvm_unreachable("Relocation type not implemented yet!");
352 break;
Eli Bendersky4c647582012-01-16 08:56:09 +0000353 }
354}
355
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000356void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000357 uint64_t Offset, uint64_t Value,
358 uint32_t Type, int64_t Addend) {
359 uint32_t *TargetPtr = reinterpret_cast<uint32_t *>(Section.Address + Offset);
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000360 uint64_t FinalAddress = Section.LoadAddress + Offset;
361
362 DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
363 << format("%llx", Section.Address + Offset)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000364 << " FinalAddress: 0x" << format("%llx", FinalAddress)
365 << " Value: 0x" << format("%llx", Value) << " Type: 0x"
366 << format("%x", Type) << " Addend: 0x" << format("%llx", Addend)
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000367 << "\n");
368
369 switch (Type) {
370 default:
371 llvm_unreachable("Relocation type not implemented yet!");
372 break;
Tim Northoverb23d8db2013-05-04 20:14:14 +0000373 case ELF::R_AARCH64_ABS64: {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000374 uint64_t *TargetPtr =
375 reinterpret_cast<uint64_t *>(Section.Address + Offset);
Tim Northoverb23d8db2013-05-04 20:14:14 +0000376 *TargetPtr = Value + Addend;
377 break;
378 }
Tim Northover5959ea32013-05-19 15:39:03 +0000379 case ELF::R_AARCH64_PREL32: {
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000380 uint64_t Result = Value + Addend - FinalAddress;
Michael J. Spencer126973b2013-08-08 22:27:13 +0000381 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000382 static_cast<int64_t>(Result) <= UINT32_MAX);
383 *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU);
384 break;
385 }
Tim Northover37cde972013-05-04 20:14:09 +0000386 case ELF::R_AARCH64_CALL26: // fallthrough
387 case ELF::R_AARCH64_JUMP26: {
388 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
389 // calculation.
390 uint64_t BranchImm = Value + Addend - FinalAddress;
391
392 // "Check that -2^27 <= result < 2^27".
Michael J. Spencer126973b2013-08-08 22:27:13 +0000393 assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) &&
Tim Northover37cde972013-05-04 20:14:09 +0000394 static_cast<int64_t>(BranchImm) < (1LL << 27));
Tim Northover5959ea32013-05-19 15:39:03 +0000395
396 // AArch64 code is emitted with .rela relocations. The data already in any
397 // bits affected by the relocation on entry is garbage.
398 *TargetPtr &= 0xfc000000U;
Tim Northover37cde972013-05-04 20:14:09 +0000399 // Immediate goes in bits 25:0 of B and BL.
400 *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2;
401 break;
402 }
Tim Northover4d01c1e2013-05-04 20:14:04 +0000403 case ELF::R_AARCH64_MOVW_UABS_G3: {
404 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000405
406 // AArch64 code is emitted with .rela relocations. The data already in any
407 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000408 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000409 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
410 *TargetPtr |= Result >> (48 - 5);
Tim Northover8625fd82013-07-01 19:23:10 +0000411 // Shift must be "lsl #48", in bits 22:21
412 assert((*TargetPtr >> 21 & 0x3) == 3 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000413 break;
414 }
415 case ELF::R_AARCH64_MOVW_UABS_G2_NC: {
416 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000417
Tim Northover5959ea32013-05-19 15:39:03 +0000418 // AArch64 code is emitted with .rela relocations. The data already in any
419 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000420 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000421 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
422 *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
Tim Northover8625fd82013-07-01 19:23:10 +0000423 // Shift must be "lsl #32", in bits 22:21
424 assert((*TargetPtr >> 21 & 0x3) == 2 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000425 break;
426 }
427 case ELF::R_AARCH64_MOVW_UABS_G1_NC: {
428 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000429
430 // AArch64 code is emitted with .rela relocations. The data already in any
431 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000432 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000433 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
434 *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
Tim Northover8625fd82013-07-01 19:23:10 +0000435 // Shift must be "lsl #16", in bits 22:2
436 assert((*TargetPtr >> 21 & 0x3) == 1 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000437 break;
438 }
439 case ELF::R_AARCH64_MOVW_UABS_G0_NC: {
440 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000441
442 // AArch64 code is emitted with .rela relocations. The data already in any
443 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000444 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000445 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
446 *TargetPtr |= ((Result & 0xffffU) << 5);
Tim Northover8625fd82013-07-01 19:23:10 +0000447 // Shift must be "lsl #0", in bits 22:21.
448 assert((*TargetPtr >> 21 & 0x3) == 0 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000449 break;
450 }
Bradley Smith9d808492014-02-11 12:59:09 +0000451 case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
452 // Operation: Page(S+A) - Page(P)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000453 uint64_t Result =
454 ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
Bradley Smith9d808492014-02-11 12:59:09 +0000455
456 // Check that -2^32 <= X < 2^32
457 assert(static_cast<int64_t>(Result) >= (-1LL << 32) &&
458 static_cast<int64_t>(Result) < (1LL << 32) &&
459 "overflow check failed for relocation");
460
461 // AArch64 code is emitted with .rela relocations. The data already in any
462 // bits affected by the relocation on entry is garbage.
463 *TargetPtr &= 0x9f00001fU;
464 // Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken
465 // from bits 32:12 of X.
466 *TargetPtr |= ((Result & 0x3000U) << (29 - 12));
467 *TargetPtr |= ((Result & 0x1ffffc000ULL) >> (14 - 5));
468 break;
469 }
470 case ELF::R_AARCH64_LDST32_ABS_LO12_NC: {
471 // Operation: S + A
472 uint64_t Result = Value + Addend;
473
474 // AArch64 code is emitted with .rela relocations. The data already in any
475 // bits affected by the relocation on entry is garbage.
476 *TargetPtr &= 0xffc003ffU;
477 // Immediate goes in bits 21:10 of LD/ST instruction, taken
478 // from bits 11:2 of X
479 *TargetPtr |= ((Result & 0xffc) << (10 - 2));
480 break;
481 }
482 case ELF::R_AARCH64_LDST64_ABS_LO12_NC: {
483 // Operation: S + A
484 uint64_t Result = Value + Addend;
485
486 // AArch64 code is emitted with .rela relocations. The data already in any
487 // bits affected by the relocation on entry is garbage.
488 *TargetPtr &= 0xffc003ffU;
489 // Immediate goes in bits 21:10 of LD/ST instruction, taken
490 // from bits 11:3 of X
491 *TargetPtr |= ((Result & 0xff8) << (10 - 3));
492 break;
493 }
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000494 }
495}
496
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000497void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000498 uint64_t Offset, uint32_t Value,
499 uint32_t Type, int32_t Addend) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000500 // TODO: Add Thumb relocations.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000501 uint32_t *Placeholder =
502 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
503 uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset);
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000504 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000505 Value += Addend;
506
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000507 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
508 << Section.Address + Offset
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000509 << " FinalAddress: " << format("%p", FinalAddress) << " Value: "
510 << format("%x", Value) << " Type: " << format("%x", Type)
511 << " Addend: " << format("%x", Addend) << "\n");
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000512
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000513 switch (Type) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000514 default:
515 llvm_unreachable("Not implemented relocation type!");
516
Renato Golin8cea6e82014-01-29 11:50:56 +0000517 case ELF::R_ARM_NONE:
518 break;
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000519 // Write a 32bit value to relocation address, taking into account the
Tim Northover471cbb72012-10-03 16:29:42 +0000520 // implicit addend encoded in the target.
Renato Golin8cea6e82014-01-29 11:50:56 +0000521 case ELF::R_ARM_PREL31:
Tim Northover3b684d82013-05-28 19:48:19 +0000522 case ELF::R_ARM_TARGET1:
523 case ELF::R_ARM_ABS32:
524 *TargetPtr = *Placeholder + Value;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000525 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000526 // Write first 16 bit of 32 bit value to the mov instruction.
527 // Last 4 bit should be shifted.
Tim Northover3b684d82013-05-28 19:48:19 +0000528 case ELF::R_ARM_MOVW_ABS_NC:
Tim Northover471cbb72012-10-03 16:29:42 +0000529 // We are not expecting any other addend in the relocation address.
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000530 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
Tim Northover471cbb72012-10-03 16:29:42 +0000531 // non-contiguous fields.
Tim Northover3b684d82013-05-28 19:48:19 +0000532 assert((*Placeholder & 0x000F0FFF) == 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000533 Value = Value & 0xFFFF;
Tim Northover3b684d82013-05-28 19:48:19 +0000534 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000535 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
536 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000537 // Write last 16 bit of 32 bit value to the mov instruction.
538 // Last 4 bit should be shifted.
Tim Northover3b684d82013-05-28 19:48:19 +0000539 case ELF::R_ARM_MOVT_ABS:
Tim Northover471cbb72012-10-03 16:29:42 +0000540 // We are not expecting any other addend in the relocation address.
541 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
Tim Northover3b684d82013-05-28 19:48:19 +0000542 assert((*Placeholder & 0x000F0FFF) == 0);
543
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000544 Value = (Value >> 16) & 0xFFFF;
Tim Northover3b684d82013-05-28 19:48:19 +0000545 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000546 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
547 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000548 // Write 24 bit relative value to the branch instruction.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000549 case ELF::R_ARM_PC24: // Fall through.
550 case ELF::R_ARM_CALL: // Fall through.
Tim Northover3b684d82013-05-28 19:48:19 +0000551 case ELF::R_ARM_JUMP24: {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000552 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
553 RelValue = (RelValue & 0x03FFFFFC) >> 2;
Tim Northover3b684d82013-05-28 19:48:19 +0000554 assert((*TargetPtr & 0xFFFFFF) == 0xFFFFFE);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000555 *TargetPtr &= 0xFF000000;
556 *TargetPtr |= RelValue;
557 break;
558 }
Tim Northover3b684d82013-05-28 19:48:19 +0000559 case ELF::R_ARM_PRIVATE_0:
560 // This relocation is reserved by the ARM ELF ABI for internal use. We
561 // appropriate it here to act as an R_ARM_ABS32 without any addend for use
562 // in the stubs created during JIT (which can't put an addend into the
563 // original object file).
564 *TargetPtr = Value;
565 break;
566 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000567}
568
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000569void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000570 uint64_t Offset, uint32_t Value,
571 uint32_t Type, int32_t Addend) {
572 uint32_t *Placeholder =
573 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
574 uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000575 Value += Addend;
576
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000577 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000578 << Section.Address + Offset << " FinalAddress: "
579 << format("%p", Section.LoadAddress + Offset) << " Value: "
580 << format("%x", Value) << " Type: " << format("%x", Type)
581 << " Addend: " << format("%x", Addend) << "\n");
Akira Hatanaka111174b2012-08-17 21:28:04 +0000582
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000583 switch (Type) {
Akira Hatanaka111174b2012-08-17 21:28:04 +0000584 default:
585 llvm_unreachable("Not implemented relocation type!");
586 break;
587 case ELF::R_MIPS_32:
Akira Hatanaka2e236242013-07-24 01:58:40 +0000588 *TargetPtr = Value + (*Placeholder);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000589 break;
590 case ELF::R_MIPS_26:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000591 *TargetPtr = ((*Placeholder) & 0xfc000000) | ((Value & 0x0fffffff) >> 2);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000592 break;
593 case ELF::R_MIPS_HI16:
594 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
Akira Hatanaka2e236242013-07-24 01:58:40 +0000595 Value += ((*Placeholder) & 0x0000ffff) << 16;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000596 *TargetPtr =
597 ((*Placeholder) & 0xffff0000) | (((Value + 0x8000) >> 16) & 0xffff);
Akira Hatanaka2e236242013-07-24 01:58:40 +0000598 break;
599 case ELF::R_MIPS_LO16:
600 Value += ((*Placeholder) & 0x0000ffff);
601 *TargetPtr = ((*Placeholder) & 0xffff0000) | (Value & 0xffff);
602 break;
603 case ELF::R_MIPS_UNUSED1:
604 // Similar to ELF::R_ARM_PRIVATE_0, R_MIPS_UNUSED1 and R_MIPS_UNUSED2
605 // are used for internal JIT purpose. These relocations are similar to
606 // R_MIPS_HI16 and R_MIPS_LO16, but they do not take any addend into
607 // account.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000608 *TargetPtr =
609 ((*TargetPtr) & 0xffff0000) | (((Value + 0x8000) >> 16) & 0xffff);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000610 break;
Akira Hatanaka2e236242013-07-24 01:58:40 +0000611 case ELF::R_MIPS_UNUSED2:
Akira Hatanaka111174b2012-08-17 21:28:04 +0000612 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
613 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000614 }
Akira Hatanaka111174b2012-08-17 21:28:04 +0000615}
616
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000617// Return the .TOC. section and offset.
618void RuntimeDyldELF::findPPC64TOCSection(ObjectImage &Obj,
619 ObjSectionToIDMap &LocalSections,
620 RelocationValueRef &Rel) {
621 // Set a default SectionID in case we do not find a TOC section below.
622 // This may happen for references to TOC base base (sym@toc, .odp
623 // relocation) without a .toc directive. In this case just use the
624 // first section (which is usually the .odp) since the code won't
625 // reference the .toc base directly.
626 Rel.SymbolName = NULL;
627 Rel.SectionID = 0;
628
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000629 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
630 // order. The TOC starts where the first of these sections starts.
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000631 for (section_iterator si = Obj.begin_sections(), se = Obj.end_sections();
632 si != se; ++si) {
633
634 StringRef SectionName;
635 check(si->getName(SectionName));
636
637 if (SectionName == ".got"
638 || SectionName == ".toc"
639 || SectionName == ".tocbss"
640 || SectionName == ".plt") {
641 Rel.SectionID = findOrEmitSection(Obj, *si, false, LocalSections);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000642 break;
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000643 }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000644 }
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000645
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000646 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
647 // thus permitting a full 64 Kbytes segment.
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000648 Rel.Addend = 0x8000;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000649}
650
651// Returns the sections and offset associated with the ODP entry referenced
652// by Symbol.
653void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
654 ObjSectionToIDMap &LocalSections,
655 RelocationValueRef &Rel) {
656 // Get the ELF symbol value (st_value) to compare with Relocation offset in
657 // .opd entries
Rafael Espindola5e812af2014-01-30 02:49:50 +0000658 for (section_iterator si = Obj.begin_sections(), se = Obj.end_sections();
659 si != se; ++si) {
Rafael Espindolaa61f1e92013-06-03 19:37:34 +0000660 section_iterator RelSecI = si->getRelocatedSection();
661 if (RelSecI == Obj.end_sections())
662 continue;
663
664 StringRef RelSectionName;
665 check(RelSecI->getName(RelSectionName));
666 if (RelSectionName != ".opd")
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000667 continue;
668
Rafael Espindolab5155a52014-02-10 20:24:04 +0000669 for (relocation_iterator i = si->relocation_begin(),
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000670 e = si->relocation_end();
671 i != e;) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000672 // The R_PPC64_ADDR64 relocation indicates the first field
673 // of a .opd entry
674 uint64_t TypeFunc;
675 check(i->getType(TypeFunc));
676 if (TypeFunc != ELF::R_PPC64_ADDR64) {
Rafael Espindola5e812af2014-01-30 02:49:50 +0000677 ++i;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000678 continue;
679 }
680
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000681 uint64_t TargetSymbolOffset;
Rafael Espindola806f0062013-06-05 01:33:53 +0000682 symbol_iterator TargetSymbol = i->getSymbol();
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000683 check(i->getOffset(TargetSymbolOffset));
Rafael Espindola0d15f732013-05-09 03:39:05 +0000684 int64_t Addend;
685 check(getELFRelocationAddend(*i, Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000686
Rafael Espindola5e812af2014-01-30 02:49:50 +0000687 ++i;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000688 if (i == e)
689 break;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000690
691 // Just check if following relocation is a R_PPC64_TOC
692 uint64_t TypeTOC;
693 check(i->getType(TypeTOC));
694 if (TypeTOC != ELF::R_PPC64_TOC)
695 continue;
696
697 // Finally compares the Symbol value and the target symbol offset
698 // to check if this .opd entry refers to the symbol the relocation
699 // points to.
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000700 if (Rel.Addend != (int64_t)TargetSymbolOffset)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000701 continue;
702
703 section_iterator tsi(Obj.end_sections());
Rafael Espindola806f0062013-06-05 01:33:53 +0000704 check(TargetSymbol->getSection(tsi));
Lang Hames9b2dc932014-02-18 21:46:39 +0000705 bool IsCode = false;
706 tsi->isText(IsCode);
707 Rel.SectionID = findOrEmitSection(Obj, (*tsi), IsCode, LocalSections);
Rafael Espindola0d15f732013-05-09 03:39:05 +0000708 Rel.Addend = (intptr_t)Addend;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000709 return;
710 }
711 }
712 llvm_unreachable("Attempting to get address of ODP entry!");
713}
714
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000715// Relocation masks following the #lo(value), #hi(value), #ha(value),
716// #higher(value), #highera(value), #highest(value), and #highesta(value)
717// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
718// document.
719
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000720static inline uint16_t applyPPClo(uint64_t value) { return value & 0xffff; }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000721
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000722static inline uint16_t applyPPChi(uint64_t value) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000723 return (value >> 16) & 0xffff;
724}
725
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000726static inline uint16_t applyPPCha (uint64_t value) {
727 return ((value + 0x8000) >> 16) & 0xffff;
728}
729
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000730static inline uint16_t applyPPChigher(uint64_t value) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000731 return (value >> 32) & 0xffff;
732}
733
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000734static inline uint16_t applyPPChighera (uint64_t value) {
735 return ((value + 0x8000) >> 32) & 0xffff;
736}
737
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000738static inline uint16_t applyPPChighest(uint64_t value) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000739 return (value >> 48) & 0xffff;
740}
741
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000742static inline uint16_t applyPPChighesta (uint64_t value) {
743 return ((value + 0x8000) >> 48) & 0xffff;
744}
745
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000746void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000747 uint64_t Offset, uint64_t Value,
748 uint32_t Type, int64_t Addend) {
749 uint8_t *LocalAddress = Section.Address + Offset;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000750 switch (Type) {
751 default:
752 llvm_unreachable("Relocation type not implemented yet!");
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000753 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000754 case ELF::R_PPC64_ADDR16:
755 writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
756 break;
757 case ELF::R_PPC64_ADDR16_DS:
758 writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
759 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000760 case ELF::R_PPC64_ADDR16_LO:
761 writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000762 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000763 case ELF::R_PPC64_ADDR16_LO_DS:
764 writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
765 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000766 case ELF::R_PPC64_ADDR16_HI:
767 writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000768 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000769 case ELF::R_PPC64_ADDR16_HA:
770 writeInt16BE(LocalAddress, applyPPCha(Value + Addend));
771 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000772 case ELF::R_PPC64_ADDR16_HIGHER:
773 writeInt16BE(LocalAddress, applyPPChigher(Value + Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000774 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000775 case ELF::R_PPC64_ADDR16_HIGHERA:
776 writeInt16BE(LocalAddress, applyPPChighera(Value + Addend));
777 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000778 case ELF::R_PPC64_ADDR16_HIGHEST:
779 writeInt16BE(LocalAddress, applyPPChighest(Value + Addend));
780 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000781 case ELF::R_PPC64_ADDR16_HIGHESTA:
782 writeInt16BE(LocalAddress, applyPPChighesta(Value + Addend));
783 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000784 case ELF::R_PPC64_ADDR14: {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000785 assert(((Value + Addend) & 3) == 0);
786 // Preserve the AA/LK bits in the branch instruction
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000787 uint8_t aalk = *(LocalAddress + 3);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000788 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
789 } break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000790 case ELF::R_PPC64_REL16_LO: {
791 uint64_t FinalAddress = (Section.LoadAddress + Offset);
792 uint64_t Delta = Value - FinalAddress + Addend;
793 writeInt16BE(LocalAddress, applyPPClo(Delta));
794 } break;
795 case ELF::R_PPC64_REL16_HI: {
796 uint64_t FinalAddress = (Section.LoadAddress + Offset);
797 uint64_t Delta = Value - FinalAddress + Addend;
798 writeInt16BE(LocalAddress, applyPPChi(Delta));
799 } break;
800 case ELF::R_PPC64_REL16_HA: {
801 uint64_t FinalAddress = (Section.LoadAddress + Offset);
802 uint64_t Delta = Value - FinalAddress + Addend;
803 writeInt16BE(LocalAddress, applyPPCha(Delta));
804 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000805 case ELF::R_PPC64_ADDR32: {
Adhemerval Zanella9b0b7812013-01-04 19:08:13 +0000806 int32_t Result = static_cast<int32_t>(Value + Addend);
807 if (SignExtend32<32>(Result) != Result)
Adhemerval Zanella1ae22482013-01-09 17:08:15 +0000808 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
Adhemerval Zanella9b0b7812013-01-04 19:08:13 +0000809 writeInt32BE(LocalAddress, Result);
810 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000811 case ELF::R_PPC64_REL24: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000812 uint64_t FinalAddress = (Section.LoadAddress + Offset);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000813 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
814 if (SignExtend32<24>(delta) != delta)
815 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
816 // Generates a 'bl <address>' instruction
817 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
818 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000819 case ELF::R_PPC64_REL32: {
Adhemerval Zanella1ae22482013-01-09 17:08:15 +0000820 uint64_t FinalAddress = (Section.LoadAddress + Offset);
821 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
822 if (SignExtend32<32>(delta) != delta)
823 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
824 writeInt32BE(LocalAddress, delta);
825 } break;
Adhemerval Zanellae8bd03d2013-05-06 17:21:23 +0000826 case ELF::R_PPC64_REL64: {
827 uint64_t FinalAddress = (Section.LoadAddress + Offset);
828 uint64_t Delta = Value - FinalAddress + Addend;
829 writeInt64BE(LocalAddress, Delta);
830 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000831 case ELF::R_PPC64_ADDR64:
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000832 writeInt64BE(LocalAddress, Value + Addend);
833 break;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000834 }
835}
836
Richard Sandifordca044082013-05-03 14:15:35 +0000837void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000838 uint64_t Offset, uint64_t Value,
839 uint32_t Type, int64_t Addend) {
Richard Sandifordca044082013-05-03 14:15:35 +0000840 uint8_t *LocalAddress = Section.Address + Offset;
841 switch (Type) {
842 default:
843 llvm_unreachable("Relocation type not implemented yet!");
844 break;
845 case ELF::R_390_PC16DBL:
846 case ELF::R_390_PLT16DBL: {
847 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
848 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
849 writeInt16BE(LocalAddress, Delta / 2);
850 break;
851 }
852 case ELF::R_390_PC32DBL:
853 case ELF::R_390_PLT32DBL: {
854 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
855 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
856 writeInt32BE(LocalAddress, Delta / 2);
857 break;
858 }
859 case ELF::R_390_PC32: {
860 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
861 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
862 writeInt32BE(LocalAddress, Delta);
863 break;
864 }
865 case ELF::R_390_64:
866 writeInt64BE(LocalAddress, Value + Addend);
867 break;
868 }
869}
870
Andrew Kaylor5f3a9982013-08-19 19:38:06 +0000871// The target location for the relocation is described by RE.SectionID and
872// RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
873// SectionEntry has three members describing its location.
874// SectionEntry::Address is the address at which the section has been loaded
875// into memory in the current (host) process. SectionEntry::LoadAddress is the
876// address that the section will have in the target process.
877// SectionEntry::ObjAddress is the address of the bits for this section in the
878// original emitted object image (also in the current address space).
879//
880// Relocations will be applied as if the section were loaded at
881// SectionEntry::LoadAddress, but they will be applied at an address based
882// on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
883// Target memory contents if they are required for value calculations.
884//
885// The Value parameter here is the load address of the symbol for the
886// relocation to be applied. For relocations which refer to symbols in the
887// current object Value will be the LoadAddress of the section in which
888// the symbol resides (RE.Addend provides additional information about the
889// symbol location). For external symbols, Value will be the address of the
890// symbol in the target address space.
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000891void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
Andrew Kaylor5f3a9982013-08-19 19:38:06 +0000892 uint64_t Value) {
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000893 const SectionEntry &Section = Sections[RE.SectionID];
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000894 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
895 RE.SymOffset);
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000896}
897
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000898void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000899 uint64_t Offset, uint64_t Value,
900 uint32_t Type, int64_t Addend,
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000901 uint64_t SymOffset) {
Eli Bendersky4c647582012-01-16 08:56:09 +0000902 switch (Arch) {
903 case Triple::x86_64:
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000904 resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
Eli Bendersky4c647582012-01-16 08:56:09 +0000905 break;
906 case Triple::x86:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000907 resolveX86Relocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000908 (uint32_t)(Addend & 0xffffffffL));
Eli Bendersky4c647582012-01-16 08:56:09 +0000909 break;
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000910 case Triple::aarch64:
Christian Pirker99974c72014-03-26 14:57:32 +0000911 case Triple::aarch64_be:
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000912 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
913 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000914 case Triple::arm: // Fall through.
Christian Pirker2a111602014-03-28 14:35:30 +0000915 case Triple::armeb:
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000916 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000917 case Triple::thumbeb:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000918 resolveARMRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000919 (uint32_t)(Addend & 0xffffffffL));
Eli Bendersky4c647582012-01-16 08:56:09 +0000920 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000921 case Triple::mips: // Fall through.
Akira Hatanaka111174b2012-08-17 21:28:04 +0000922 case Triple::mipsel:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000923 resolveMIPSRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL),
924 Type, (uint32_t)(Addend & 0xffffffffL));
Akira Hatanaka111174b2012-08-17 21:28:04 +0000925 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000926 case Triple::ppc64: // Fall through.
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000927 case Triple::ppc64le:
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000928 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000929 break;
Richard Sandifordca044082013-05-03 14:15:35 +0000930 case Triple::systemz:
931 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
932 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000933 default:
934 llvm_unreachable("Unsupported CPU type!");
Eli Bendersky4c647582012-01-16 08:56:09 +0000935 }
936}
937
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000938relocation_iterator RuntimeDyldELF::processRelocationRef(
939 unsigned SectionID, relocation_iterator RelI, ObjectImage &Obj,
940 ObjSectionToIDMap &ObjSectionToID, const SymbolTableMap &Symbols,
941 StubMap &Stubs) {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000942 uint64_t RelType;
Juergen Ributzka046709f2014-03-21 07:26:41 +0000943 Check(RelI->getType(RelType));
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000944 int64_t Addend;
Juergen Ributzka046709f2014-03-21 07:26:41 +0000945 Check(getELFRelocationAddend(*RelI, Addend));
946 symbol_iterator Symbol = RelI->getSymbol();
Eli Bendersky667b8792012-05-01 10:41:12 +0000947
948 // Obtain the symbol name which is referenced in the relocation
949 StringRef TargetName;
Rafael Espindola75954472013-06-05 02:55:01 +0000950 if (Symbol != Obj.end_symbols())
951 Symbol->getName(TargetName);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000952 DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend
953 << " TargetName: " << TargetName << "\n");
Eli Bendersky667b8792012-05-01 10:41:12 +0000954 RelocationValueRef Value;
955 // First search for the symbol in the local symbol table
Rafael Espindola75954472013-06-05 02:55:01 +0000956 SymbolTableMap::const_iterator lsi = Symbols.end();
957 SymbolRef::Type SymType = SymbolRef::ST_Unknown;
958 if (Symbol != Obj.end_symbols()) {
959 lsi = Symbols.find(TargetName.data());
960 Symbol->getType(SymType);
961 }
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000962 if (lsi != Symbols.end()) {
963 Value.SectionID = lsi->second.first;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000964 Value.Offset = lsi->second.second;
Ulrich Weigand78e97652013-04-05 13:29:04 +0000965 Value.Addend = lsi->second.second + Addend;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000966 } else {
Eli Bendersky667b8792012-05-01 10:41:12 +0000967 // Search for the symbol in the global symbol table
Rafael Espindola75954472013-06-05 02:55:01 +0000968 SymbolTableMap::const_iterator gsi = GlobalSymbolTable.end();
969 if (Symbol != Obj.end_symbols())
970 gsi = GlobalSymbolTable.find(TargetName.data());
Eli Benderskyfc079082012-05-01 06:58:59 +0000971 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000972 Value.SectionID = gsi->second.first;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000973 Value.Offset = gsi->second.second;
Ulrich Weigand78e97652013-04-05 13:29:04 +0000974 Value.Addend = gsi->second.second + Addend;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000975 } else {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000976 switch (SymType) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000977 case SymbolRef::ST_Debug: {
978 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
979 // and can be changed by another developers. Maybe best way is add
980 // a new symbol type ST_Section to SymbolRef and use it.
981 section_iterator si(Obj.end_sections());
982 Symbol->getSection(si);
983 if (si == Obj.end_sections())
984 llvm_unreachable("Symbol section not found, bad object file format!");
985 DEBUG(dbgs() << "\t\tThis is section symbol\n");
986 // Default to 'true' in case isText fails (though it never does).
987 bool isCode = true;
988 si->isText(isCode);
989 Value.SectionID = findOrEmitSection(Obj, (*si), isCode, ObjSectionToID);
990 Value.Addend = Addend;
991 break;
992 }
993 case SymbolRef::ST_Data:
994 case SymbolRef::ST_Unknown: {
995 Value.SymbolName = TargetName.data();
996 Value.Addend = Addend;
Richard Mittonad6d3492013-08-16 18:54:26 +0000997
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000998 // Absolute relocations will have a zero symbol ID (STN_UNDEF), which
999 // will manifest here as a NULL symbol name.
1000 // We can set this as a valid (but empty) symbol name, and rely
1001 // on addRelocationForSymbol to handle this.
1002 if (!Value.SymbolName)
1003 Value.SymbolName = "";
1004 break;
1005 }
1006 default:
1007 llvm_unreachable("Unresolved symbol type!");
1008 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001009 }
1010 }
Eli Bendersky4c647582012-01-16 08:56:09 +00001011 }
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001012 uint64_t Offset;
Juergen Ributzka046709f2014-03-21 07:26:41 +00001013 Check(RelI->getOffset(Offset));
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001014
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001015 DEBUG(dbgs() << "\t\tSectionID: " << SectionID << " Offset: " << Offset
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001016 << "\n");
Tim Northovere19bed72014-07-23 12:32:47 +00001017 if ((Arch == Triple::aarch64 || Arch == Triple::aarch64_be) &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001018 (RelType == ELF::R_AARCH64_CALL26 || RelType == ELF::R_AARCH64_JUMP26)) {
Tim Northover37cde972013-05-04 20:14:09 +00001019 // This is an AArch64 branch relocation, need to use a stub function.
1020 DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
1021 SectionEntry &Section = Sections[SectionID];
1022
1023 // Look for an existing stub.
1024 StubMap::const_iterator i = Stubs.find(Value);
1025 if (i != Stubs.end()) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001026 resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second,
1027 RelType, 0);
Tim Northover37cde972013-05-04 20:14:09 +00001028 DEBUG(dbgs() << " Stub function found\n");
1029 } else {
1030 // Create a new stub function.
1031 DEBUG(dbgs() << " Create a new stub function\n");
1032 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001033 uint8_t *StubTargetAddr =
1034 createStubFunction(Section.Address + Section.StubOffset);
Tim Northover37cde972013-05-04 20:14:09 +00001035
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001036 RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section.Address,
Tim Northover37cde972013-05-04 20:14:09 +00001037 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001038 RelocationEntry REmovk_g2(SectionID, StubTargetAddr - Section.Address + 4,
Tim Northover37cde972013-05-04 20:14:09 +00001039 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001040 RelocationEntry REmovk_g1(SectionID, StubTargetAddr - Section.Address + 8,
Tim Northover37cde972013-05-04 20:14:09 +00001041 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
1042 RelocationEntry REmovk_g0(SectionID,
1043 StubTargetAddr - Section.Address + 12,
1044 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
1045
1046 if (Value.SymbolName) {
1047 addRelocationForSymbol(REmovz_g3, Value.SymbolName);
1048 addRelocationForSymbol(REmovk_g2, Value.SymbolName);
1049 addRelocationForSymbol(REmovk_g1, Value.SymbolName);
1050 addRelocationForSymbol(REmovk_g0, Value.SymbolName);
1051 } else {
1052 addRelocationForSection(REmovz_g3, Value.SectionID);
1053 addRelocationForSection(REmovk_g2, Value.SectionID);
1054 addRelocationForSection(REmovk_g1, Value.SectionID);
1055 addRelocationForSection(REmovk_g0, Value.SectionID);
1056 }
1057 resolveRelocation(Section, Offset,
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001058 (uint64_t)Section.Address + Section.StubOffset, RelType,
1059 0);
Tim Northover37cde972013-05-04 20:14:09 +00001060 Section.StubOffset += getMaxStubSize();
1061 }
1062 } else if (Arch == Triple::arm &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001063 (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL ||
1064 RelType == ELF::R_ARM_JUMP24)) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001065 // This is an ARM branch relocation, need to use a stub function.
1066 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001067 SectionEntry &Section = Sections[SectionID];
Eli Bendersky4c647582012-01-16 08:56:09 +00001068
Eric Christopherc33f6222012-10-23 17:19:15 +00001069 // Look for an existing stub.
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001070 StubMap::const_iterator i = Stubs.find(Value);
1071 if (i != Stubs.end()) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001072 resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second,
1073 RelType, 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001074 DEBUG(dbgs() << " Stub function found\n");
1075 } else {
1076 // Create a new stub function.
1077 DEBUG(dbgs() << " Create a new stub function\n");
1078 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001079 uint8_t *StubTargetAddr =
1080 createStubFunction(Section.Address + Section.StubOffset);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001081 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Tim Northover3b684d82013-05-28 19:48:19 +00001082 ELF::R_ARM_PRIVATE_0, Value.Addend);
Eli Bendersky667b8792012-05-01 10:41:12 +00001083 if (Value.SymbolName)
1084 addRelocationForSymbol(RE, Value.SymbolName);
1085 else
1086 addRelocationForSection(RE, Value.SectionID);
1087
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001088 resolveRelocation(Section, Offset,
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001089 (uint64_t)Section.Address + Section.StubOffset, RelType,
1090 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001091 Section.StubOffset += getMaxStubSize();
1092 }
Akira Hatanakaa667aad2012-12-03 23:12:19 +00001093 } else if ((Arch == Triple::mipsel || Arch == Triple::mips) &&
1094 RelType == ELF::R_MIPS_26) {
Akira Hatanaka111174b2012-08-17 21:28:04 +00001095 // This is an Mips branch relocation, need to use a stub function.
1096 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001097 SectionEntry &Section = Sections[SectionID];
1098 uint8_t *Target = Section.Address + Offset;
Akira Hatanaka111174b2012-08-17 21:28:04 +00001099 uint32_t *TargetAddress = (uint32_t *)Target;
1100
1101 // Extract the addend from the instruction.
1102 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
1103
1104 Value.Addend += Addend;
1105
1106 // Look up for existing stub.
1107 StubMap::const_iterator i = Stubs.find(Value);
1108 if (i != Stubs.end()) {
Petar Jovanovic45115f82013-11-19 21:56:00 +00001109 RelocationEntry RE(SectionID, Offset, RelType, i->second);
1110 addRelocationForSection(RE, SectionID);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001111 DEBUG(dbgs() << " Stub function found\n");
1112 } else {
1113 // Create a new stub function.
1114 DEBUG(dbgs() << " Create a new stub function\n");
1115 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001116 uint8_t *StubTargetAddr =
1117 createStubFunction(Section.Address + Section.StubOffset);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001118
1119 // Creating Hi and Lo relocations for the filled stub instructions.
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001120 RelocationEntry REHi(SectionID, StubTargetAddr - Section.Address,
Akira Hatanaka2e236242013-07-24 01:58:40 +00001121 ELF::R_MIPS_UNUSED1, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001122 RelocationEntry RELo(SectionID, StubTargetAddr - Section.Address + 4,
Akira Hatanaka2e236242013-07-24 01:58:40 +00001123 ELF::R_MIPS_UNUSED2, Value.Addend);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001124
1125 if (Value.SymbolName) {
1126 addRelocationForSymbol(REHi, Value.SymbolName);
1127 addRelocationForSymbol(RELo, Value.SymbolName);
1128 } else {
1129 addRelocationForSection(REHi, Value.SectionID);
1130 addRelocationForSection(RELo, Value.SectionID);
1131 }
1132
Petar Jovanovic45115f82013-11-19 21:56:00 +00001133 RelocationEntry RE(SectionID, Offset, RelType, Section.StubOffset);
1134 addRelocationForSection(RE, SectionID);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001135 Section.StubOffset += getMaxStubSize();
1136 }
Bill Schmidt0a9170d2013-07-26 01:35:43 +00001137 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001138 if (RelType == ELF::R_PPC64_REL24) {
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001139 // Determine ABI variant in use for this object.
1140 unsigned AbiVariant;
1141 Obj.getObjectFile()->getPlatformFlags(AbiVariant);
1142 AbiVariant &= ELF::EF_PPC64_ABI;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001143 // A PPC branch relocation will need a stub function if the target is
1144 // an external symbol (Symbol::ST_Unknown) or if the target address
1145 // is not within the signed 24-bits branch address.
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001146 SectionEntry &Section = Sections[SectionID];
1147 uint8_t *Target = Section.Address + Offset;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001148 bool RangeOverflow = false;
1149 if (SymType != SymbolRef::ST_Unknown) {
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001150 if (AbiVariant != 2) {
1151 // In the ELFv1 ABI, a function call may point to the .opd entry,
1152 // so the final symbol value is calculated based on the relocation
1153 // values in the .opd section.
1154 findOPDEntrySection(Obj, ObjSectionToID, Value);
1155 } else {
1156 // In the ELFv2 ABI, a function symbol may provide a local entry
1157 // point, which must be used for direct calls.
1158 uint8_t SymOther;
1159 Symbol->getOther(SymOther);
1160 Value.Addend += ELF::decodePPC64LocalEntryOffset(SymOther);
1161 }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001162 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
1163 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
1164 // If it is within 24-bits branch range, just set the branch target
1165 if (SignExtend32<24>(delta) == delta) {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001166 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001167 if (Value.SymbolName)
1168 addRelocationForSymbol(RE, Value.SymbolName);
1169 else
1170 addRelocationForSection(RE, Value.SectionID);
1171 } else {
1172 RangeOverflow = true;
1173 }
1174 }
1175 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
1176 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
1177 // larger than 24-bits.
1178 StubMap::const_iterator i = Stubs.find(Value);
1179 if (i != Stubs.end()) {
1180 // Symbol function stub already created, just relocate to it
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001181 resolveRelocation(Section, Offset,
Andrew Kaylorfb05a502012-11-02 19:45:23 +00001182 (uint64_t)Section.Address + i->second, RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001183 DEBUG(dbgs() << " Stub function found\n");
1184 } else {
1185 // Create a new stub function.
1186 DEBUG(dbgs() << " Create a new stub function\n");
1187 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001188 uint8_t *StubTargetAddr =
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001189 createStubFunction(Section.Address + Section.StubOffset,
1190 AbiVariant);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001191 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001192 ELF::R_PPC64_ADDR64, Value.Addend);
1193
1194 // Generates the 64-bits address loads as exemplified in section
Ulrich Weigand32626012014-06-20 18:17:56 +00001195 // 4.5.1 in PPC64 ELF ABI. Note that the relocations need to
1196 // apply to the low part of the instructions, so we have to update
1197 // the offset according to the target endianness.
1198 uint64_t StubRelocOffset = StubTargetAddr - Section.Address;
1199 if (!IsTargetLittleEndian)
1200 StubRelocOffset += 2;
1201
1202 RelocationEntry REhst(SectionID, StubRelocOffset + 0,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001203 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
Ulrich Weigand32626012014-06-20 18:17:56 +00001204 RelocationEntry REhr(SectionID, StubRelocOffset + 4,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001205 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
Ulrich Weigand32626012014-06-20 18:17:56 +00001206 RelocationEntry REh(SectionID, StubRelocOffset + 12,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001207 ELF::R_PPC64_ADDR16_HI, Value.Addend);
Ulrich Weigand32626012014-06-20 18:17:56 +00001208 RelocationEntry REl(SectionID, StubRelocOffset + 16,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001209 ELF::R_PPC64_ADDR16_LO, Value.Addend);
1210
1211 if (Value.SymbolName) {
1212 addRelocationForSymbol(REhst, Value.SymbolName);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001213 addRelocationForSymbol(REhr, Value.SymbolName);
1214 addRelocationForSymbol(REh, Value.SymbolName);
1215 addRelocationForSymbol(REl, Value.SymbolName);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001216 } else {
1217 addRelocationForSection(REhst, Value.SectionID);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001218 addRelocationForSection(REhr, Value.SectionID);
1219 addRelocationForSection(REh, Value.SectionID);
1220 addRelocationForSection(REl, Value.SectionID);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001221 }
1222
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001223 resolveRelocation(Section, Offset,
Andrew Kaylorfb05a502012-11-02 19:45:23 +00001224 (uint64_t)Section.Address + Section.StubOffset,
1225 RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001226 Section.StubOffset += getMaxStubSize();
1227 }
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001228 if (SymType == SymbolRef::ST_Unknown) {
Ulrich Weigandfa84ac92014-03-11 15:26:27 +00001229 // Restore the TOC for external calls
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001230 if (AbiVariant == 2)
1231 writeInt32BE(Target + 4, 0xE8410018); // ld r2,28(r1)
1232 else
1233 writeInt32BE(Target + 4, 0xE8410028); // ld r2,40(r1)
1234 }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001235 }
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +00001236 } else if (RelType == ELF::R_PPC64_TOC16 ||
1237 RelType == ELF::R_PPC64_TOC16_DS ||
1238 RelType == ELF::R_PPC64_TOC16_LO ||
1239 RelType == ELF::R_PPC64_TOC16_LO_DS ||
1240 RelType == ELF::R_PPC64_TOC16_HI ||
1241 RelType == ELF::R_PPC64_TOC16_HA) {
1242 // These relocations are supposed to subtract the TOC address from
1243 // the final value. This does not fit cleanly into the RuntimeDyld
1244 // scheme, since there may be *two* sections involved in determining
1245 // the relocation value (the section of the symbol refered to by the
1246 // relocation, and the TOC section associated with the current module).
1247 //
1248 // Fortunately, these relocations are currently only ever generated
1249 // refering to symbols that themselves reside in the TOC, which means
1250 // that the two sections are actually the same. Thus they cancel out
1251 // and we can immediately resolve the relocation right now.
1252 switch (RelType) {
1253 case ELF::R_PPC64_TOC16: RelType = ELF::R_PPC64_ADDR16; break;
1254 case ELF::R_PPC64_TOC16_DS: RelType = ELF::R_PPC64_ADDR16_DS; break;
1255 case ELF::R_PPC64_TOC16_LO: RelType = ELF::R_PPC64_ADDR16_LO; break;
1256 case ELF::R_PPC64_TOC16_LO_DS: RelType = ELF::R_PPC64_ADDR16_LO_DS; break;
1257 case ELF::R_PPC64_TOC16_HI: RelType = ELF::R_PPC64_ADDR16_HI; break;
1258 case ELF::R_PPC64_TOC16_HA: RelType = ELF::R_PPC64_ADDR16_HA; break;
1259 default: llvm_unreachable("Wrong relocation type.");
1260 }
1261
1262 RelocationValueRef TOCValue;
1263 findPPC64TOCSection(Obj, ObjSectionToID, TOCValue);
1264 if (Value.SymbolName || Value.SectionID != TOCValue.SectionID)
1265 llvm_unreachable("Unsupported TOC relocation.");
1266 Value.Addend -= TOCValue.Addend;
1267 resolveRelocation(Sections[SectionID], Offset, Value.Addend, RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001268 } else {
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +00001269 // There are two ways to refer to the TOC address directly: either
1270 // via a ELF::R_PPC64_TOC relocation (where both symbol and addend are
1271 // ignored), or via any relocation that refers to the magic ".TOC."
1272 // symbols (in which case the addend is respected).
1273 if (RelType == ELF::R_PPC64_TOC) {
1274 RelType = ELF::R_PPC64_ADDR64;
1275 findPPC64TOCSection(Obj, ObjSectionToID, Value);
1276 } else if (TargetName == ".TOC.") {
1277 findPPC64TOCSection(Obj, ObjSectionToID, Value);
1278 Value.Addend += Addend;
1279 }
1280
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001281 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Richard Mittonad6d3492013-08-16 18:54:26 +00001282
1283 if (Value.SymbolName)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001284 addRelocationForSymbol(RE, Value.SymbolName);
1285 else
1286 addRelocationForSection(RE, Value.SectionID);
1287 }
Richard Sandifordca044082013-05-03 14:15:35 +00001288 } else if (Arch == Triple::systemz &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001289 (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) {
Richard Sandifordca044082013-05-03 14:15:35 +00001290 // Create function stubs for both PLT and GOT references, regardless of
1291 // whether the GOT reference is to data or code. The stub contains the
1292 // full address of the symbol, as needed by GOT references, and the
1293 // executable part only adds an overhead of 8 bytes.
1294 //
1295 // We could try to conserve space by allocating the code and data
1296 // parts of the stub separately. However, as things stand, we allocate
1297 // a stub for every relocation, so using a GOT in JIT code should be
1298 // no less space efficient than using an explicit constant pool.
1299 DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
1300 SectionEntry &Section = Sections[SectionID];
1301
1302 // Look for an existing stub.
1303 StubMap::const_iterator i = Stubs.find(Value);
1304 uintptr_t StubAddress;
1305 if (i != Stubs.end()) {
1306 StubAddress = uintptr_t(Section.Address) + i->second;
1307 DEBUG(dbgs() << " Stub function found\n");
1308 } else {
1309 // Create a new stub function.
1310 DEBUG(dbgs() << " Create a new stub function\n");
1311
1312 uintptr_t BaseAddress = uintptr_t(Section.Address);
1313 uintptr_t StubAlignment = getStubAlignment();
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001314 StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) &
1315 -StubAlignment;
Richard Sandifordca044082013-05-03 14:15:35 +00001316 unsigned StubOffset = StubAddress - BaseAddress;
1317
1318 Stubs[Value] = StubOffset;
1319 createStubFunction((uint8_t *)StubAddress);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001320 RelocationEntry RE(SectionID, StubOffset + 8, ELF::R_390_64,
Lang Hames40e200e2014-08-25 23:33:48 +00001321 Value.Offset);
Richard Sandifordca044082013-05-03 14:15:35 +00001322 if (Value.SymbolName)
1323 addRelocationForSymbol(RE, Value.SymbolName);
1324 else
1325 addRelocationForSection(RE, Value.SectionID);
1326 Section.StubOffset = StubOffset + getMaxStubSize();
1327 }
1328
1329 if (RelType == ELF::R_390_GOTENT)
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001330 resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL,
1331 Addend);
Richard Sandifordca044082013-05-03 14:15:35 +00001332 else
1333 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001334 } else if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_PLT32) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001335 // The way the PLT relocations normally work is that the linker allocates
1336 // the
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001337 // PLT and this relocation makes a PC-relative call into the PLT. The PLT
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001338 // entry will then jump to an address provided by the GOT. On first call,
1339 // the
1340 // GOT address will point back into PLT code that resolves the symbol. After
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001341 // the first call, the GOT entry points to the actual function.
1342 //
1343 // For local functions we're ignoring all of that here and just replacing
1344 // the PLT32 relocation type with PC32, which will translate the relocation
1345 // into a PC-relative call directly to the function. For external symbols we
1346 // can't be sure the function will be within 2^32 bytes of the call site, so
1347 // we need to create a stub, which calls into the GOT. This case is
1348 // equivalent to the usual PLT implementation except that we use the stub
1349 // mechanism in RuntimeDyld (which puts stubs at the end of the section)
1350 // rather than allocating a PLT section.
1351 if (Value.SymbolName) {
1352 // This is a call to an external function.
1353 // Look for an existing stub.
1354 SectionEntry &Section = Sections[SectionID];
1355 StubMap::const_iterator i = Stubs.find(Value);
1356 uintptr_t StubAddress;
1357 if (i != Stubs.end()) {
1358 StubAddress = uintptr_t(Section.Address) + i->second;
1359 DEBUG(dbgs() << " Stub function found\n");
1360 } else {
1361 // Create a new stub function (equivalent to a PLT entry).
1362 DEBUG(dbgs() << " Create a new stub function\n");
1363
1364 uintptr_t BaseAddress = uintptr_t(Section.Address);
1365 uintptr_t StubAlignment = getStubAlignment();
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001366 StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) &
1367 -StubAlignment;
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001368 unsigned StubOffset = StubAddress - BaseAddress;
1369 Stubs[Value] = StubOffset;
1370 createStubFunction((uint8_t *)StubAddress);
1371
1372 // Create a GOT entry for the external function.
1373 GOTEntries.push_back(Value);
1374
1375 // Make our stub function a relative call to the GOT entry.
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001376 RelocationEntry RE(SectionID, StubOffset + 2, ELF::R_X86_64_GOTPCREL,
1377 -4);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001378 addRelocationForSymbol(RE, Value.SymbolName);
1379
1380 // Bump our stub offset counter
1381 Section.StubOffset = StubOffset + getMaxStubSize();
1382 }
1383
1384 // Make the target call a call into the stub table.
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001385 resolveRelocation(Section, Offset, StubAddress, ELF::R_X86_64_PC32,
1386 Addend);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001387 } else {
1388 RelocationEntry RE(SectionID, Offset, ELF::R_X86_64_PC32, Value.Addend,
1389 Value.Offset);
1390 addRelocationForSection(RE, Value.SectionID);
1391 }
Eli Bendersky667b8792012-05-01 10:41:12 +00001392 } else {
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001393 if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_GOTPCREL) {
1394 GOTEntries.push_back(Value);
1395 }
1396 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset);
Eli Bendersky667b8792012-05-01 10:41:12 +00001397 if (Value.SymbolName)
1398 addRelocationForSymbol(RE, Value.SymbolName);
1399 else
1400 addRelocationForSection(RE, Value.SectionID);
1401 }
Juergen Ributzka046709f2014-03-21 07:26:41 +00001402 return ++RelI;
Jim Grosbacheff0a402012-01-16 22:26:39 +00001403}
1404
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001405void RuntimeDyldELF::updateGOTEntries(StringRef Name, uint64_t Addr) {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001406
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001407 SmallVectorImpl<std::pair<SID, GOTRelocations>>::iterator it;
1408 SmallVectorImpl<std::pair<SID, GOTRelocations>>::iterator end = GOTs.end();
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001409
1410 for (it = GOTs.begin(); it != end; ++it) {
1411 GOTRelocations &GOTEntries = it->second;
1412 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
Craig Topper353eda42014-04-24 06:44:33 +00001413 if (GOTEntries[i].SymbolName != nullptr &&
1414 GOTEntries[i].SymbolName == Name) {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001415 GOTEntries[i].Offset = Addr;
1416 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001417 }
1418 }
1419}
1420
1421size_t RuntimeDyldELF::getGOTEntrySize() {
1422 // We don't use the GOT in all of these cases, but it's essentially free
1423 // to put them all here.
1424 size_t Result = 0;
1425 switch (Arch) {
1426 case Triple::x86_64:
1427 case Triple::aarch64:
James Molloybd2ffa02014-04-30 10:15:41 +00001428 case Triple::aarch64_be:
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001429 case Triple::ppc64:
1430 case Triple::ppc64le:
1431 case Triple::systemz:
1432 Result = sizeof(uint64_t);
1433 break;
1434 case Triple::x86:
1435 case Triple::arm:
1436 case Triple::thumb:
1437 case Triple::mips:
1438 case Triple::mipsel:
1439 Result = sizeof(uint32_t);
1440 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001441 default:
1442 llvm_unreachable("Unsupported CPU type!");
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001443 }
1444 return Result;
1445}
1446
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001447uint64_t RuntimeDyldELF::findGOTEntry(uint64_t LoadAddress, uint64_t Offset) {
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001448
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001449 const size_t GOTEntrySize = getGOTEntrySize();
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001450
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001451 SmallVectorImpl<std::pair<SID, GOTRelocations>>::const_iterator it;
1452 SmallVectorImpl<std::pair<SID, GOTRelocations>>::const_iterator end =
1453 GOTs.end();
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001454
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001455 int GOTIndex = -1;
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001456 for (it = GOTs.begin(); it != end; ++it) {
1457 SID GOTSectionID = it->first;
1458 const GOTRelocations &GOTEntries = it->second;
1459
1460 // Find the matching entry in our vector.
1461 uint64_t SymbolOffset = 0;
1462 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
Craig Topper353eda42014-04-24 06:44:33 +00001463 if (!GOTEntries[i].SymbolName) {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001464 if (getSectionLoadAddress(GOTEntries[i].SectionID) == LoadAddress &&
1465 GOTEntries[i].Offset == Offset) {
1466 GOTIndex = i;
1467 SymbolOffset = GOTEntries[i].Offset;
1468 break;
1469 }
1470 } else {
1471 // GOT entries for external symbols use the addend as the address when
1472 // the external symbol has been resolved.
1473 if (GOTEntries[i].Offset == LoadAddress) {
1474 GOTIndex = i;
1475 // Don't use the Addend here. The relocation handler will use it.
1476 break;
1477 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001478 }
1479 }
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001480
1481 if (GOTIndex != -1) {
1482 if (GOTEntrySize == sizeof(uint64_t)) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001483 uint64_t *LocalGOTAddr = (uint64_t *)getSectionAddress(GOTSectionID);
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001484 // Fill in this entry with the address of the symbol being referenced.
1485 LocalGOTAddr[GOTIndex] = LoadAddress + SymbolOffset;
1486 } else {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001487 uint32_t *LocalGOTAddr = (uint32_t *)getSectionAddress(GOTSectionID);
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001488 // Fill in this entry with the address of the symbol being referenced.
1489 LocalGOTAddr[GOTIndex] = (uint32_t)(LoadAddress + SymbolOffset);
1490 }
1491
1492 // Calculate the load address of this entry
1493 return getSectionLoadAddress(GOTSectionID) + (GOTIndex * GOTEntrySize);
1494 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001495 }
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001496
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001497 assert(GOTIndex != -1 && "Unable to find requested GOT entry.");
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001498 return 0;
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001499}
1500
Lang Hames36072da2014-05-12 21:39:59 +00001501void RuntimeDyldELF::finalizeLoad(ObjectImage &ObjImg,
1502 ObjSectionToIDMap &SectionMap) {
Andrew Kaylor7bb13442013-10-11 21:25:48 +00001503 // If necessary, allocate the global offset table
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001504 if (MemMgr) {
1505 // Allocate the GOT if necessary
1506 size_t numGOTEntries = GOTEntries.size();
1507 if (numGOTEntries != 0) {
1508 // Allocate memory for the section
1509 unsigned SectionID = Sections.size();
1510 size_t TotalSize = numGOTEntries * getGOTEntrySize();
1511 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, getGOTEntrySize(),
1512 SectionID, ".got", false);
1513 if (!Addr)
1514 report_fatal_error("Unable to allocate memory for GOT!");
1515
1516 GOTs.push_back(std::make_pair(SectionID, GOTEntries));
1517 Sections.push_back(SectionEntry(".got", Addr, TotalSize, 0));
1518 // For now, initialize all GOT entries to zero. We'll fill them in as
1519 // needed when GOT-based relocations are applied.
1520 memset(Addr, 0, TotalSize);
1521 }
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001522 } else {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001523 report_fatal_error("Unable to allocate memory for GOT!");
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001524 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +00001525
1526 // Look for and record the EH frame section.
1527 ObjSectionToIDMap::iterator i, e;
1528 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
1529 const SectionRef &Section = i->first;
1530 StringRef Name;
1531 Section.getName(Name);
1532 if (Name == ".eh_frame") {
1533 UnregisteredEHFrameSections.push_back(i->second);
1534 break;
1535 }
1536 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001537}
1538
Andrew Kayloradc70562012-10-02 21:18:39 +00001539bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
1540 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
1541 return false;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001542 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic,
1543 strlen(ELF::ElfMagic))) == 0;
Eli Bendersky4c647582012-01-16 08:56:09 +00001544}
Lang Hames173c69f2014-01-08 04:09:09 +00001545
1546bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile *Obj) const {
1547 return Obj->isELF();
1548}
1549
Eli Bendersky4c647582012-01-16 08:56:09 +00001550} // namespace llvm