blob: 6ca7fd38d200e5071cb13c2bb537b86a6f31c96f [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
14#define DEBUG_TYPE "dyld"
Andrew Kayloradc70562012-10-02 21:18:39 +000015#include "RuntimeDyldELF.h"
16#include "JITRegistrar.h"
17#include "ObjectImageCommon.h"
Eli Bendersky4c647582012-01-16 08:56:09 +000018#include "llvm/ADT/IntervalMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/StringRef.h"
Eli Bendersky4c647582012-01-16 08:56:09 +000021#include "llvm/ADT/Triple.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/ExecutionEngine/ObjectBuffer.h"
23#include "llvm/ExecutionEngine/ObjectImage.h"
Michael J. Spencer126973b2013-08-08 22:27:13 +000024#include "llvm/Object/ELFObjectFile.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Object/ObjectFile.h"
26#include "llvm/Support/ELF.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
Preston Gurdcc31af92012-04-16 22:12:58 +000032namespace {
33
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +000034static inline
35error_code check(error_code Err) {
36 if (Err) {
37 report_fatal_error(Err.message());
38 }
39 return Err;
40}
41
Michael J. Spencer1a791612013-01-15 07:44:25 +000042template<class ELFT>
Michael J. Spencerbae14ce2013-01-04 20:36:28 +000043class DyldELFObject
Michael J. Spencer1a791612013-01-15 07:44:25 +000044 : public ELFObjectFile<ELFT> {
Rafael Espindola035b4162013-04-17 21:20:55 +000045 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Preston Gurdcc31af92012-04-16 22:12:58 +000046
Michael J. Spencer1a791612013-01-15 07:44:25 +000047 typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
48 typedef Elf_Sym_Impl<ELFT> Elf_Sym;
Michael J. Spencerbae14ce2013-01-04 20:36:28 +000049 typedef
Michael J. Spencer1a791612013-01-15 07:44:25 +000050 Elf_Rel_Impl<ELFT, false> Elf_Rel;
Michael J. Spencerbae14ce2013-01-04 20:36:28 +000051 typedef
Michael J. Spencer1a791612013-01-15 07:44:25 +000052 Elf_Rel_Impl<ELFT, true> Elf_Rela;
Preston Gurdcc31af92012-04-16 22:12:58 +000053
Michael J. Spencer1a791612013-01-15 07:44:25 +000054 typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
Preston Gurdcc31af92012-04-16 22:12:58 +000055
56 typedef typename ELFDataTypeTypedefHelper<
Michael J. Spencer1a791612013-01-15 07:44:25 +000057 ELFT>::value_type addr_type;
Preston Gurdcc31af92012-04-16 22:12:58 +000058
Preston Gurdcc31af92012-04-16 22:12:58 +000059public:
Andrew Kayloradc70562012-10-02 21:18:39 +000060 DyldELFObject(MemoryBuffer *Wrapper, 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) {
Michael J. Spencer1a791612013-01-15 07:44:25 +000067 return (isa<ELFObjectFile<ELFT> >(v)
Michael J. Spencerbae14ce2013-01-04 20:36:28 +000068 && classof(cast<ELFObjectFile
Michael J. Spencer1a791612013-01-15 07:44:25 +000069 <ELFT> >(v)));
Preston Gurdcc31af92012-04-16 22:12:58 +000070 }
71 static inline bool classof(
Michael J. Spencer1a791612013-01-15 07:44:25 +000072 const ELFObjectFile<ELFT> *v) {
Preston Gurdcc31af92012-04-16 22:12:58 +000073 return v->isDyldType();
74 }
Preston Gurdcc31af92012-04-16 22:12:58 +000075};
76
Michael J. Spencer1a791612013-01-15 07:44:25 +000077template<class ELFT>
Andrew Kayloradc70562012-10-02 21:18:39 +000078class ELFObjectImage : public ObjectImageCommon {
Preston Gurdcc31af92012-04-16 22:12:58 +000079 protected:
Michael J. Spencer1a791612013-01-15 07:44:25 +000080 DyldELFObject<ELFT> *DyldObj;
Preston Gurdcc31af92012-04-16 22:12:58 +000081 bool Registered;
82
83 public:
Andrew Kayloradc70562012-10-02 21:18:39 +000084 ELFObjectImage(ObjectBuffer *Input,
Michael J. Spencer1a791612013-01-15 07:44:25 +000085 DyldELFObject<ELFT> *Obj)
Andrew Kayloradc70562012-10-02 21:18:39 +000086 : ObjectImageCommon(Input, Obj),
Preston Gurdcc31af92012-04-16 22:12:58 +000087 DyldObj(Obj),
88 Registered(false) {}
89
90 virtual ~ELFObjectImage() {
91 if (Registered)
92 deregisterWithDebugger();
93 }
94
95 // Subclasses can override these methods to update the image with loaded
96 // addresses for sections and common symbols
97 virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr)
98 {
99 DyldObj->updateSectionAddress(Sec, Addr);
100 }
101
102 virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr)
103 {
104 DyldObj->updateSymbolAddress(Sym, Addr);
105 }
106
107 virtual void registerWithDebugger()
108 {
Andrew Kayloradc70562012-10-02 21:18:39 +0000109 JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
Preston Gurdcc31af92012-04-16 22:12:58 +0000110 Registered = true;
111 }
112 virtual void deregisterWithDebugger()
113 {
Andrew Kayloradc70562012-10-02 21:18:39 +0000114 JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer);
Preston Gurdcc31af92012-04-16 22:12:58 +0000115 }
116};
117
Andrew Kayloradc70562012-10-02 21:18:39 +0000118// The MemoryBuffer passed into this constructor is just a wrapper around the
119// actual memory. Ultimately, the Binary parent class will take ownership of
120// this MemoryBuffer object but not the underlying memory.
Michael J. Spencer1a791612013-01-15 07:44:25 +0000121template<class ELFT>
122DyldELFObject<ELFT>::DyldELFObject(MemoryBuffer *Wrapper, error_code &ec)
123 : ELFObjectFile<ELFT>(Wrapper, ec) {
Preston Gurdcc31af92012-04-16 22:12:58 +0000124 this->isDyldELFObject = true;
125}
126
Michael J. Spencer1a791612013-01-15 07:44:25 +0000127template<class ELFT>
128void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec,
129 uint64_t Addr) {
Preston Gurdcc31af92012-04-16 22:12:58 +0000130 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
131 Elf_Shdr *shdr = const_cast<Elf_Shdr*>(
132 reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
133
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
Michael J. Spencer1a791612013-01-15 07:44:25 +0000139template<class ELFT>
140void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef,
141 uint64_t Addr) {
Preston Gurdcc31af92012-04-16 22:12:58 +0000142
143 Elf_Sym *sym = const_cast<Elf_Sym*>(
Michael J. Spencer1a791612013-01-15 07:44:25 +0000144 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
Lang Hames173c69f2014-01-08 04:09:09 +0000182ObjectImage *RuntimeDyldELF::createObjectImageFromFile(object::ObjectFile *ObjFile) {
183 if (!ObjFile)
184 return NULL;
185
186 error_code ec;
187 MemoryBuffer* Buffer = MemoryBuffer::getMemBuffer(ObjFile->getData(),
188 "",
189 false);
190
191 if (ObjFile->getBytesInAddress() == 4 && ObjFile->isLittleEndian()) {
192 DyldELFObject<ELFType<support::little, 2, false> > *Obj =
193 new DyldELFObject<ELFType<support::little, 2, false> >(Buffer, ec);
194 return new ELFObjectImage<ELFType<support::little, 2, false> >(NULL, Obj);
195 }
196 else if (ObjFile->getBytesInAddress() == 4 && !ObjFile->isLittleEndian()) {
197 DyldELFObject<ELFType<support::big, 2, false> > *Obj =
198 new DyldELFObject<ELFType<support::big, 2, false> >(Buffer, ec);
199 return new ELFObjectImage<ELFType<support::big, 2, false> >(NULL, Obj);
200 }
201 else if (ObjFile->getBytesInAddress() == 8 && !ObjFile->isLittleEndian()) {
202 DyldELFObject<ELFType<support::big, 2, true> > *Obj =
203 new DyldELFObject<ELFType<support::big, 2, true> >(Buffer, ec);
204 return new ELFObjectImage<ELFType<support::big, 2, true> >(NULL, Obj);
205 }
206 else if (ObjFile->getBytesInAddress() == 8 && ObjFile->isLittleEndian()) {
207 DyldELFObject<ELFType<support::little, 2, true> > *Obj =
208 new DyldELFObject<ELFType<support::little, 2, true> >(Buffer, ec);
209 return new ELFObjectImage<ELFType<support::little, 2, true> >(NULL, Obj);
210 }
211 else
212 llvm_unreachable("Unexpected ELF format");
213}
214
Andrew Kayloradc70562012-10-02 21:18:39 +0000215ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) {
216 if (Buffer->getBufferSize() < ELF::EI_NIDENT)
217 llvm_unreachable("Unexpected ELF object size");
218 std::pair<unsigned char, unsigned char> Ident = std::make_pair(
219 (uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS],
220 (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]);
Preston Gurdcc31af92012-04-16 22:12:58 +0000221 error_code ec;
222
223 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
Michael J. Spencer1a791612013-01-15 07:44:25 +0000224 DyldELFObject<ELFType<support::little, 4, false> > *Obj =
225 new DyldELFObject<ELFType<support::little, 4, false> >(
226 Buffer->getMemBuffer(), ec);
227 return new ELFObjectImage<ELFType<support::little, 4, false> >(Buffer, Obj);
Preston Gurdcc31af92012-04-16 22:12:58 +0000228 }
229 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
Michael J. Spencer1a791612013-01-15 07:44:25 +0000230 DyldELFObject<ELFType<support::big, 4, false> > *Obj =
231 new DyldELFObject<ELFType<support::big, 4, false> >(
232 Buffer->getMemBuffer(), ec);
233 return new ELFObjectImage<ELFType<support::big, 4, false> >(Buffer, Obj);
Preston Gurdcc31af92012-04-16 22:12:58 +0000234 }
235 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
Michael J. Spencer1a791612013-01-15 07:44:25 +0000236 DyldELFObject<ELFType<support::big, 8, true> > *Obj =
237 new DyldELFObject<ELFType<support::big, 8, true> >(
238 Buffer->getMemBuffer(), ec);
239 return new ELFObjectImage<ELFType<support::big, 8, true> >(Buffer, Obj);
Preston Gurdcc31af92012-04-16 22:12:58 +0000240 }
241 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
Michael J. Spencer1a791612013-01-15 07:44:25 +0000242 DyldELFObject<ELFType<support::little, 8, true> > *Obj =
243 new DyldELFObject<ELFType<support::little, 8, true> >(
244 Buffer->getMemBuffer(), ec);
245 return new ELFObjectImage<ELFType<support::little, 8, true> >(Buffer, Obj);
Preston Gurdcc31af92012-04-16 22:12:58 +0000246 }
247 else
248 llvm_unreachable("Unexpected ELF format");
249}
250
Preston Gurdcc31af92012-04-16 22:12:58 +0000251RuntimeDyldELF::~RuntimeDyldELF() {
Preston Gurdcc31af92012-04-16 22:12:58 +0000252}
Eli Bendersky4c647582012-01-16 08:56:09 +0000253
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000254void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
255 uint64_t Offset,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000256 uint64_t Value,
257 uint32_t Type,
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000258 int64_t Addend,
259 uint64_t SymOffset) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000260 switch (Type) {
261 default:
262 llvm_unreachable("Relocation type not implemented yet!");
263 break;
Eli Bendersky4c647582012-01-16 08:56:09 +0000264 case ELF::R_X86_64_64: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000265 uint64_t *Target = reinterpret_cast<uint64_t*>(Section.Address + Offset);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000266 *Target = Value + Addend;
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000267 DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend))
268 << " at " << format("%p\n",Target));
Eli Bendersky4c647582012-01-16 08:56:09 +0000269 break;
270 }
271 case ELF::R_X86_64_32:
272 case ELF::R_X86_64_32S: {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000273 Value += Addend;
Andrew Kaylor8e87a752012-07-27 20:30:12 +0000274 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000275 (Type == ELF::R_X86_64_32S &&
Andrew Kaylor8e87a752012-07-27 20:30:12 +0000276 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
Eli Bendersky4c647582012-01-16 08:56:09 +0000277 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000278 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
Eli Bendersky4c647582012-01-16 08:56:09 +0000279 *Target = TruncatedAddr;
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000280 DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr)
281 << " at " << format("%p\n",Target));
Eli Bendersky4c647582012-01-16 08:56:09 +0000282 break;
283 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000284 case ELF::R_X86_64_GOTPCREL: {
285 // findGOTEntry returns the 'G + GOT' part of the relocation calculation
286 // based on the load/target address of the GOT (not the current/local addr).
287 uint64_t GOTAddr = findGOTEntry(Value, SymOffset);
288 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
289 uint64_t FinalAddress = Section.LoadAddress + Offset;
290 // The processRelocationRef method combines the symbol offset and the addend
291 // and in most cases that's what we want. For this relocation type, we need
292 // the raw addend, so we subtract the symbol offset to get it.
293 int64_t RealOffset = GOTAddr + Addend - SymOffset - FinalAddress;
294 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
295 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
296 *Target = TruncOffset;
297 break;
298 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000299 case ELF::R_X86_64_PC32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000300 // Get the placeholder value from the generated object since
301 // a previous relocation attempt may have overwritten the loaded version
302 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
303 + Offset);
304 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
305 uint64_t FinalAddress = Section.LoadAddress + Offset;
Danil Malyshev70d22cc2012-03-30 16:45:19 +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);
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000309 *Target = 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
315 uint64_t *Placeholder = reinterpret_cast<uint64_t*>(Section.ObjAddress
316 + Offset);
317 uint64_t *Target = reinterpret_cast<uint64_t*>(Section.Address + Offset);
318 uint64_t FinalAddress = Section.LoadAddress + Offset;
319 *Target = *Placeholder + Value + Addend - FinalAddress;
320 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,
326 uint64_t Offset,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000327 uint32_t Value,
328 uint32_t Type,
329 int32_t Addend) {
330 switch (Type) {
Eli Bendersky4c647582012-01-16 08:56:09 +0000331 case ELF::R_386_32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000332 // Get the placeholder value from the generated object since
333 // a previous relocation attempt may have overwritten the loaded version
334 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
335 + Offset);
336 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
337 *Target = *Placeholder + Value + Addend;
Eli Bendersky4c647582012-01-16 08:56:09 +0000338 break;
339 }
340 case ELF::R_386_PC32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000341 // Get the placeholder value from the generated object since
342 // a previous relocation attempt may have overwritten the loaded version
343 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
344 + Offset);
345 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
346 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000347 uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000348 *Target = RealOffset;
Eli Bendersky4c647582012-01-16 08:56:09 +0000349 break;
350 }
351 default:
352 // There are other relocation types, but it appears these are the
Andrew Kaylor782d5c42012-07-27 18:39:47 +0000353 // only ones currently used by the LLVM ELF object writer
Craig Toppera2886c22012-02-07 05:05:23 +0000354 llvm_unreachable("Relocation type not implemented yet!");
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000355 break;
Eli Bendersky4c647582012-01-16 08:56:09 +0000356 }
357}
358
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000359void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
360 uint64_t Offset,
361 uint64_t Value,
362 uint32_t Type,
363 int64_t Addend) {
364 uint32_t *TargetPtr = reinterpret_cast<uint32_t*>(Section.Address + Offset);
365 uint64_t FinalAddress = Section.LoadAddress + Offset;
366
367 DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
368 << format("%llx", Section.Address + Offset)
369 << " FinalAddress: 0x" << format("%llx",FinalAddress)
370 << " Value: 0x" << format("%llx",Value)
371 << " Type: 0x" << format("%x",Type)
372 << " Addend: 0x" << format("%llx",Addend)
373 << "\n");
374
375 switch (Type) {
376 default:
377 llvm_unreachable("Relocation type not implemented yet!");
378 break;
Tim Northoverb23d8db2013-05-04 20:14:14 +0000379 case ELF::R_AARCH64_ABS64: {
380 uint64_t *TargetPtr = reinterpret_cast<uint64_t*>(Section.Address + Offset);
381 *TargetPtr = Value + Addend;
382 break;
383 }
Tim Northover5959ea32013-05-19 15:39:03 +0000384 case ELF::R_AARCH64_PREL32: {
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000385 uint64_t Result = Value + Addend - FinalAddress;
Michael J. Spencer126973b2013-08-08 22:27:13 +0000386 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000387 static_cast<int64_t>(Result) <= UINT32_MAX);
388 *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU);
389 break;
390 }
Tim Northover37cde972013-05-04 20:14:09 +0000391 case ELF::R_AARCH64_CALL26: // fallthrough
392 case ELF::R_AARCH64_JUMP26: {
393 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
394 // calculation.
395 uint64_t BranchImm = Value + Addend - FinalAddress;
396
397 // "Check that -2^27 <= result < 2^27".
Michael J. Spencer126973b2013-08-08 22:27:13 +0000398 assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) &&
Tim Northover37cde972013-05-04 20:14:09 +0000399 static_cast<int64_t>(BranchImm) < (1LL << 27));
Tim Northover5959ea32013-05-19 15:39:03 +0000400
401 // AArch64 code is emitted with .rela relocations. The data already in any
402 // bits affected by the relocation on entry is garbage.
403 *TargetPtr &= 0xfc000000U;
Tim Northover37cde972013-05-04 20:14:09 +0000404 // Immediate goes in bits 25:0 of B and BL.
405 *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2;
406 break;
407 }
Tim Northover4d01c1e2013-05-04 20:14:04 +0000408 case ELF::R_AARCH64_MOVW_UABS_G3: {
409 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000410
411 // AArch64 code is emitted with .rela relocations. The data already in any
412 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000413 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000414 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
415 *TargetPtr |= Result >> (48 - 5);
Tim Northover8625fd82013-07-01 19:23:10 +0000416 // Shift must be "lsl #48", in bits 22:21
417 assert((*TargetPtr >> 21 & 0x3) == 3 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000418 break;
419 }
420 case ELF::R_AARCH64_MOVW_UABS_G2_NC: {
421 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000422
Tim Northover5959ea32013-05-19 15:39:03 +0000423 // AArch64 code is emitted with .rela relocations. The data already in any
424 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000425 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000426 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
427 *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
Tim Northover8625fd82013-07-01 19:23:10 +0000428 // Shift must be "lsl #32", in bits 22:21
429 assert((*TargetPtr >> 21 & 0x3) == 2 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000430 break;
431 }
432 case ELF::R_AARCH64_MOVW_UABS_G1_NC: {
433 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000434
435 // AArch64 code is emitted with .rela relocations. The data already in any
436 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000437 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000438 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
439 *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
Tim Northover8625fd82013-07-01 19:23:10 +0000440 // Shift must be "lsl #16", in bits 22:2
441 assert((*TargetPtr >> 21 & 0x3) == 1 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000442 break;
443 }
444 case ELF::R_AARCH64_MOVW_UABS_G0_NC: {
445 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000446
447 // AArch64 code is emitted with .rela relocations. The data already in any
448 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000449 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000450 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
451 *TargetPtr |= ((Result & 0xffffU) << 5);
Tim Northover8625fd82013-07-01 19:23:10 +0000452 // Shift must be "lsl #0", in bits 22:21.
453 assert((*TargetPtr >> 21 & 0x3) == 0 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000454 break;
455 }
Bradley Smith9d808492014-02-11 12:59:09 +0000456 case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
457 // Operation: Page(S+A) - Page(P)
458 uint64_t Result = ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
459
460 // Check that -2^32 <= X < 2^32
461 assert(static_cast<int64_t>(Result) >= (-1LL << 32) &&
462 static_cast<int64_t>(Result) < (1LL << 32) &&
463 "overflow check failed for relocation");
464
465 // AArch64 code is emitted with .rela relocations. The data already in any
466 // bits affected by the relocation on entry is garbage.
467 *TargetPtr &= 0x9f00001fU;
468 // Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken
469 // from bits 32:12 of X.
470 *TargetPtr |= ((Result & 0x3000U) << (29 - 12));
471 *TargetPtr |= ((Result & 0x1ffffc000ULL) >> (14 - 5));
472 break;
473 }
474 case ELF::R_AARCH64_LDST32_ABS_LO12_NC: {
475 // Operation: S + A
476 uint64_t Result = Value + Addend;
477
478 // AArch64 code is emitted with .rela relocations. The data already in any
479 // bits affected by the relocation on entry is garbage.
480 *TargetPtr &= 0xffc003ffU;
481 // Immediate goes in bits 21:10 of LD/ST instruction, taken
482 // from bits 11:2 of X
483 *TargetPtr |= ((Result & 0xffc) << (10 - 2));
484 break;
485 }
486 case ELF::R_AARCH64_LDST64_ABS_LO12_NC: {
487 // Operation: S + A
488 uint64_t Result = Value + Addend;
489
490 // AArch64 code is emitted with .rela relocations. The data already in any
491 // bits affected by the relocation on entry is garbage.
492 *TargetPtr &= 0xffc003ffU;
493 // Immediate goes in bits 21:10 of LD/ST instruction, taken
494 // from bits 11:3 of X
495 *TargetPtr |= ((Result & 0xff8) << (10 - 3));
496 break;
497 }
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000498 }
499}
500
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000501void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
502 uint64_t Offset,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000503 uint32_t Value,
504 uint32_t Type,
505 int32_t Addend) {
506 // TODO: Add Thumb relocations.
Tim Northover3b684d82013-05-28 19:48:19 +0000507 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress +
508 Offset);
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000509 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
510 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000511 Value += Addend;
512
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000513 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
514 << Section.Address + Offset
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000515 << " FinalAddress: " << format("%p",FinalAddress)
516 << " Value: " << format("%x",Value)
517 << " Type: " << format("%x",Type)
518 << " Addend: " << format("%x",Addend)
519 << "\n");
520
521 switch(Type) {
522 default:
523 llvm_unreachable("Not implemented relocation type!");
524
Renato Golin8cea6e82014-01-29 11:50:56 +0000525 case ELF::R_ARM_NONE:
526 break;
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000527 // Write a 32bit value to relocation address, taking into account the
Tim Northover471cbb72012-10-03 16:29:42 +0000528 // implicit addend encoded in the target.
Renato Golin8cea6e82014-01-29 11:50:56 +0000529 case ELF::R_ARM_PREL31:
Tim Northover3b684d82013-05-28 19:48:19 +0000530 case ELF::R_ARM_TARGET1:
531 case ELF::R_ARM_ABS32:
532 *TargetPtr = *Placeholder + Value;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000533 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000534 // Write first 16 bit of 32 bit value to the mov instruction.
535 // Last 4 bit should be shifted.
Tim Northover3b684d82013-05-28 19:48:19 +0000536 case ELF::R_ARM_MOVW_ABS_NC:
Tim Northover471cbb72012-10-03 16:29:42 +0000537 // We are not expecting any other addend in the relocation address.
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000538 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
Tim Northover471cbb72012-10-03 16:29:42 +0000539 // non-contiguous fields.
Tim Northover3b684d82013-05-28 19:48:19 +0000540 assert((*Placeholder & 0x000F0FFF) == 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000541 Value = Value & 0xFFFF;
Tim Northover3b684d82013-05-28 19:48:19 +0000542 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000543 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
544 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000545 // Write last 16 bit of 32 bit value to the mov instruction.
546 // Last 4 bit should be shifted.
Tim Northover3b684d82013-05-28 19:48:19 +0000547 case ELF::R_ARM_MOVT_ABS:
Tim Northover471cbb72012-10-03 16:29:42 +0000548 // We are not expecting any other addend in the relocation address.
549 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
Tim Northover3b684d82013-05-28 19:48:19 +0000550 assert((*Placeholder & 0x000F0FFF) == 0);
551
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000552 Value = (Value >> 16) & 0xFFFF;
Tim Northover3b684d82013-05-28 19:48:19 +0000553 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000554 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
555 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000556 // Write 24 bit relative value to the branch instruction.
557 case ELF::R_ARM_PC24 : // Fall through.
558 case ELF::R_ARM_CALL : // Fall through.
Tim Northover3b684d82013-05-28 19:48:19 +0000559 case ELF::R_ARM_JUMP24: {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000560 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
561 RelValue = (RelValue & 0x03FFFFFC) >> 2;
Tim Northover3b684d82013-05-28 19:48:19 +0000562 assert((*TargetPtr & 0xFFFFFF) == 0xFFFFFE);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000563 *TargetPtr &= 0xFF000000;
564 *TargetPtr |= RelValue;
565 break;
566 }
Tim Northover3b684d82013-05-28 19:48:19 +0000567 case ELF::R_ARM_PRIVATE_0:
568 // This relocation is reserved by the ARM ELF ABI for internal use. We
569 // appropriate it here to act as an R_ARM_ABS32 without any addend for use
570 // in the stubs created during JIT (which can't put an addend into the
571 // original object file).
572 *TargetPtr = Value;
573 break;
574 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000575}
576
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000577void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
578 uint64_t Offset,
Akira Hatanaka11dfbe12012-08-20 17:53:24 +0000579 uint32_t Value,
580 uint32_t Type,
581 int32_t Addend) {
Akira Hatanaka2e236242013-07-24 01:58:40 +0000582 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress +
583 Offset);
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000584 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000585 Value += Addend;
586
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000587 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
588 << Section.Address + Offset
589 << " FinalAddress: "
590 << format("%p",Section.LoadAddress + Offset)
Akira Hatanaka111174b2012-08-17 21:28:04 +0000591 << " Value: " << format("%x",Value)
592 << " Type: " << format("%x",Type)
593 << " Addend: " << format("%x",Addend)
594 << "\n");
595
596 switch(Type) {
597 default:
598 llvm_unreachable("Not implemented relocation type!");
599 break;
600 case ELF::R_MIPS_32:
Akira Hatanaka2e236242013-07-24 01:58:40 +0000601 *TargetPtr = Value + (*Placeholder);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000602 break;
603 case ELF::R_MIPS_26:
Akira Hatanaka2e236242013-07-24 01:58:40 +0000604 *TargetPtr = ((*Placeholder) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000605 break;
606 case ELF::R_MIPS_HI16:
607 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
Akira Hatanaka2e236242013-07-24 01:58:40 +0000608 Value += ((*Placeholder) & 0x0000ffff) << 16;
609 *TargetPtr = ((*Placeholder) & 0xffff0000) |
610 (((Value + 0x8000) >> 16) & 0xffff);
611 break;
612 case ELF::R_MIPS_LO16:
613 Value += ((*Placeholder) & 0x0000ffff);
614 *TargetPtr = ((*Placeholder) & 0xffff0000) | (Value & 0xffff);
615 break;
616 case ELF::R_MIPS_UNUSED1:
617 // Similar to ELF::R_ARM_PRIVATE_0, R_MIPS_UNUSED1 and R_MIPS_UNUSED2
618 // are used for internal JIT purpose. These relocations are similar to
619 // R_MIPS_HI16 and R_MIPS_LO16, but they do not take any addend into
620 // account.
Akira Hatanaka111174b2012-08-17 21:28:04 +0000621 *TargetPtr = ((*TargetPtr) & 0xffff0000) |
622 (((Value + 0x8000) >> 16) & 0xffff);
623 break;
Akira Hatanaka2e236242013-07-24 01:58:40 +0000624 case ELF::R_MIPS_UNUSED2:
Akira Hatanaka111174b2012-08-17 21:28:04 +0000625 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
626 break;
627 }
628}
629
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000630// Return the .TOC. section address to R_PPC64_TOC relocations.
631uint64_t RuntimeDyldELF::findPPC64TOC() const {
632 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
633 // order. The TOC starts where the first of these sections starts.
634 SectionList::const_iterator it = Sections.begin();
635 SectionList::const_iterator ite = Sections.end();
636 for (; it != ite; ++it) {
637 if (it->Name == ".got" ||
638 it->Name == ".toc" ||
639 it->Name == ".tocbss" ||
640 it->Name == ".plt")
641 break;
642 }
643 if (it == ite) {
644 // This may happen for
645 // * references to TOC base base (sym@toc, .odp relocation) without
646 // a .toc directive.
647 // In this case just use the first section (which is usually
648 // the .odp) since the code won't reference the .toc base
649 // directly.
650 it = Sections.begin();
651 }
652 assert (it != ite);
653 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
654 // thus permitting a full 64 Kbytes segment.
655 return it->LoadAddress + 0x8000;
656}
657
658// Returns the sections and offset associated with the ODP entry referenced
659// by Symbol.
660void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
661 ObjSectionToIDMap &LocalSections,
662 RelocationValueRef &Rel) {
663 // Get the ELF symbol value (st_value) to compare with Relocation offset in
664 // .opd entries
Rafael Espindola5e812af2014-01-30 02:49:50 +0000665 for (section_iterator si = Obj.begin_sections(), se = Obj.end_sections();
666 si != se; ++si) {
Rafael Espindolaa61f1e92013-06-03 19:37:34 +0000667 section_iterator RelSecI = si->getRelocatedSection();
668 if (RelSecI == Obj.end_sections())
669 continue;
670
671 StringRef RelSectionName;
672 check(RelSecI->getName(RelSectionName));
673 if (RelSectionName != ".opd")
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000674 continue;
675
Rafael Espindolab5155a52014-02-10 20:24:04 +0000676 for (relocation_iterator i = si->relocation_begin(),
677 e = si->relocation_end(); i != e;) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000678 // The R_PPC64_ADDR64 relocation indicates the first field
679 // of a .opd entry
680 uint64_t TypeFunc;
681 check(i->getType(TypeFunc));
682 if (TypeFunc != ELF::R_PPC64_ADDR64) {
Rafael Espindola5e812af2014-01-30 02:49:50 +0000683 ++i;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000684 continue;
685 }
686
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000687 uint64_t TargetSymbolOffset;
Rafael Espindola806f0062013-06-05 01:33:53 +0000688 symbol_iterator TargetSymbol = i->getSymbol();
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000689 check(i->getOffset(TargetSymbolOffset));
Rafael Espindola0d15f732013-05-09 03:39:05 +0000690 int64_t Addend;
691 check(getELFRelocationAddend(*i, Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000692
Rafael Espindola5e812af2014-01-30 02:49:50 +0000693 ++i;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000694 if (i == e)
695 break;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000696
697 // Just check if following relocation is a R_PPC64_TOC
698 uint64_t TypeTOC;
699 check(i->getType(TypeTOC));
700 if (TypeTOC != ELF::R_PPC64_TOC)
701 continue;
702
703 // Finally compares the Symbol value and the target symbol offset
704 // to check if this .opd entry refers to the symbol the relocation
705 // points to.
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000706 if (Rel.Addend != (int64_t)TargetSymbolOffset)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000707 continue;
708
709 section_iterator tsi(Obj.end_sections());
Rafael Espindola806f0062013-06-05 01:33:53 +0000710 check(TargetSymbol->getSection(tsi));
Lang Hames9b2dc932014-02-18 21:46:39 +0000711 bool IsCode = false;
712 tsi->isText(IsCode);
713 Rel.SectionID = findOrEmitSection(Obj, (*tsi), IsCode, LocalSections);
Rafael Espindola0d15f732013-05-09 03:39:05 +0000714 Rel.Addend = (intptr_t)Addend;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000715 return;
716 }
717 }
718 llvm_unreachable("Attempting to get address of ODP entry!");
719}
720
721// Relocation masks following the #lo(value), #hi(value), #higher(value),
722// and #highest(value) macros defined in section 4.5.1. Relocation Types
723// in PPC-elf64abi document.
724//
725static inline
726uint16_t applyPPClo (uint64_t value)
727{
728 return value & 0xffff;
729}
730
731static inline
732uint16_t applyPPChi (uint64_t value)
733{
734 return (value >> 16) & 0xffff;
735}
736
737static inline
738uint16_t applyPPChigher (uint64_t value)
739{
740 return (value >> 32) & 0xffff;
741}
742
743static inline
744uint16_t applyPPChighest (uint64_t value)
745{
746 return (value >> 48) & 0xffff;
747}
748
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000749void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
750 uint64_t Offset,
751 uint64_t Value,
752 uint32_t Type,
753 int64_t Addend) {
754 uint8_t* LocalAddress = Section.Address + Offset;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000755 switch (Type) {
756 default:
757 llvm_unreachable("Relocation type not implemented yet!");
758 break;
759 case ELF::R_PPC64_ADDR16_LO :
760 writeInt16BE(LocalAddress, applyPPClo (Value + Addend));
761 break;
762 case ELF::R_PPC64_ADDR16_HI :
763 writeInt16BE(LocalAddress, applyPPChi (Value + Addend));
764 break;
765 case ELF::R_PPC64_ADDR16_HIGHER :
766 writeInt16BE(LocalAddress, applyPPChigher (Value + Addend));
767 break;
768 case ELF::R_PPC64_ADDR16_HIGHEST :
769 writeInt16BE(LocalAddress, applyPPChighest (Value + Addend));
770 break;
771 case ELF::R_PPC64_ADDR14 : {
772 assert(((Value + Addend) & 3) == 0);
773 // Preserve the AA/LK bits in the branch instruction
774 uint8_t aalk = *(LocalAddress+3);
775 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
776 } break;
Adhemerval Zanella9b0b7812013-01-04 19:08:13 +0000777 case ELF::R_PPC64_ADDR32 : {
778 int32_t Result = static_cast<int32_t>(Value + Addend);
779 if (SignExtend32<32>(Result) != Result)
Adhemerval Zanella1ae22482013-01-09 17:08:15 +0000780 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
Adhemerval Zanella9b0b7812013-01-04 19:08:13 +0000781 writeInt32BE(LocalAddress, Result);
782 } break;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000783 case ELF::R_PPC64_REL24 : {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000784 uint64_t FinalAddress = (Section.LoadAddress + Offset);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000785 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
786 if (SignExtend32<24>(delta) != delta)
787 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
788 // Generates a 'bl <address>' instruction
789 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
790 } break;
Adhemerval Zanella1ae22482013-01-09 17:08:15 +0000791 case ELF::R_PPC64_REL32 : {
792 uint64_t FinalAddress = (Section.LoadAddress + Offset);
793 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
794 if (SignExtend32<32>(delta) != delta)
795 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
796 writeInt32BE(LocalAddress, delta);
797 } break;
Adhemerval Zanellae8bd03d2013-05-06 17:21:23 +0000798 case ELF::R_PPC64_REL64: {
799 uint64_t FinalAddress = (Section.LoadAddress + Offset);
800 uint64_t Delta = Value - FinalAddress + Addend;
801 writeInt64BE(LocalAddress, Delta);
802 } break;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000803 case ELF::R_PPC64_ADDR64 :
804 writeInt64BE(LocalAddress, Value + Addend);
805 break;
806 case ELF::R_PPC64_TOC :
807 writeInt64BE(LocalAddress, findPPC64TOC());
808 break;
809 case ELF::R_PPC64_TOC16 : {
810 uint64_t TOCStart = findPPC64TOC();
811 Value = applyPPClo((Value + Addend) - TOCStart);
812 writeInt16BE(LocalAddress, applyPPClo(Value));
813 } break;
814 case ELF::R_PPC64_TOC16_DS : {
815 uint64_t TOCStart = findPPC64TOC();
816 Value = ((Value + Addend) - TOCStart);
817 writeInt16BE(LocalAddress, applyPPClo(Value));
818 } break;
819 }
820}
821
Richard Sandifordca044082013-05-03 14:15:35 +0000822void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
823 uint64_t Offset,
824 uint64_t Value,
825 uint32_t Type,
826 int64_t Addend) {
827 uint8_t *LocalAddress = Section.Address + Offset;
828 switch (Type) {
829 default:
830 llvm_unreachable("Relocation type not implemented yet!");
831 break;
832 case ELF::R_390_PC16DBL:
833 case ELF::R_390_PLT16DBL: {
834 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
835 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
836 writeInt16BE(LocalAddress, Delta / 2);
837 break;
838 }
839 case ELF::R_390_PC32DBL:
840 case ELF::R_390_PLT32DBL: {
841 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
842 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
843 writeInt32BE(LocalAddress, Delta / 2);
844 break;
845 }
846 case ELF::R_390_PC32: {
847 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
848 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
849 writeInt32BE(LocalAddress, Delta);
850 break;
851 }
852 case ELF::R_390_64:
853 writeInt64BE(LocalAddress, Value + Addend);
854 break;
855 }
856}
857
Andrew Kaylor5f3a9982013-08-19 19:38:06 +0000858// The target location for the relocation is described by RE.SectionID and
859// RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
860// SectionEntry has three members describing its location.
861// SectionEntry::Address is the address at which the section has been loaded
862// into memory in the current (host) process. SectionEntry::LoadAddress is the
863// address that the section will have in the target process.
864// SectionEntry::ObjAddress is the address of the bits for this section in the
865// original emitted object image (also in the current address space).
866//
867// Relocations will be applied as if the section were loaded at
868// SectionEntry::LoadAddress, but they will be applied at an address based
869// on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
870// Target memory contents if they are required for value calculations.
871//
872// The Value parameter here is the load address of the symbol for the
873// relocation to be applied. For relocations which refer to symbols in the
874// current object Value will be the LoadAddress of the section in which
875// the symbol resides (RE.Addend provides additional information about the
876// symbol location). For external symbols, Value will be the address of the
877// symbol in the target address space.
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000878void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
Andrew Kaylor5f3a9982013-08-19 19:38:06 +0000879 uint64_t Value) {
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000880 const SectionEntry &Section = Sections[RE.SectionID];
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000881 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
882 RE.SymOffset);
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000883}
884
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000885void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
886 uint64_t Offset,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000887 uint64_t Value,
888 uint32_t Type,
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000889 int64_t Addend,
890 uint64_t SymOffset) {
Eli Bendersky4c647582012-01-16 08:56:09 +0000891 switch (Arch) {
892 case Triple::x86_64:
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000893 resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
Eli Bendersky4c647582012-01-16 08:56:09 +0000894 break;
895 case Triple::x86:
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000896 resolveX86Relocation(Section, Offset,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000897 (uint32_t)(Value & 0xffffffffL), Type,
898 (uint32_t)(Addend & 0xffffffffL));
Eli Bendersky4c647582012-01-16 08:56:09 +0000899 break;
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000900 case Triple::aarch64:
901 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
902 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000903 case Triple::arm: // Fall through.
904 case Triple::thumb:
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000905 resolveARMRelocation(Section, Offset,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000906 (uint32_t)(Value & 0xffffffffL), Type,
907 (uint32_t)(Addend & 0xffffffffL));
Eli Bendersky4c647582012-01-16 08:56:09 +0000908 break;
Akira Hatanaka111174b2012-08-17 21:28:04 +0000909 case Triple::mips: // Fall through.
910 case Triple::mipsel:
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000911 resolveMIPSRelocation(Section, Offset,
Akira Hatanaka11dfbe12012-08-20 17:53:24 +0000912 (uint32_t)(Value & 0xffffffffL), Type,
913 (uint32_t)(Addend & 0xffffffffL));
Akira Hatanaka111174b2012-08-17 21:28:04 +0000914 break;
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000915 case Triple::ppc64: // Fall through.
916 case Triple::ppc64le:
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000917 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000918 break;
Richard Sandifordca044082013-05-03 14:15:35 +0000919 case Triple::systemz:
920 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
921 break;
Craig Toppera2886c22012-02-07 05:05:23 +0000922 default: llvm_unreachable("Unsupported CPU type!");
Eli Bendersky4c647582012-01-16 08:56:09 +0000923 }
924}
925
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000926void RuntimeDyldELF::processRelocationRef(unsigned SectionID,
Rafael Espindola37008942013-04-29 19:03:21 +0000927 RelocationRef RelI,
Preston Gurdcc31af92012-04-16 22:12:58 +0000928 ObjectImage &Obj,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000929 ObjSectionToIDMap &ObjSectionToID,
Eli Benderskyfc079082012-05-01 06:58:59 +0000930 const SymbolTableMap &Symbols,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000931 StubMap &Stubs) {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000932 uint64_t RelType;
Rafael Espindola37008942013-04-29 19:03:21 +0000933 Check(RelI.getType(RelType));
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000934 int64_t Addend;
Rafael Espindola0d15f732013-05-09 03:39:05 +0000935 Check(getELFRelocationAddend(RelI, Addend));
Rafael Espindola806f0062013-06-05 01:33:53 +0000936 symbol_iterator Symbol = RelI.getSymbol();
Eli Bendersky667b8792012-05-01 10:41:12 +0000937
938 // Obtain the symbol name which is referenced in the relocation
939 StringRef TargetName;
Rafael Espindola75954472013-06-05 02:55:01 +0000940 if (Symbol != Obj.end_symbols())
941 Symbol->getName(TargetName);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000942 DEBUG(dbgs() << "\t\tRelType: " << RelType
943 << " Addend: " << Addend
944 << " TargetName: " << TargetName
945 << "\n");
Eli Bendersky667b8792012-05-01 10:41:12 +0000946 RelocationValueRef Value;
947 // First search for the symbol in the local symbol table
Rafael Espindola75954472013-06-05 02:55:01 +0000948 SymbolTableMap::const_iterator lsi = Symbols.end();
949 SymbolRef::Type SymType = SymbolRef::ST_Unknown;
950 if (Symbol != Obj.end_symbols()) {
951 lsi = Symbols.find(TargetName.data());
952 Symbol->getType(SymType);
953 }
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000954 if (lsi != Symbols.end()) {
955 Value.SectionID = lsi->second.first;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000956 Value.Offset = lsi->second.second;
Ulrich Weigand78e97652013-04-05 13:29:04 +0000957 Value.Addend = lsi->second.second + Addend;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000958 } else {
Eli Bendersky667b8792012-05-01 10:41:12 +0000959 // Search for the symbol in the global symbol table
Rafael Espindola75954472013-06-05 02:55:01 +0000960 SymbolTableMap::const_iterator gsi = GlobalSymbolTable.end();
961 if (Symbol != Obj.end_symbols())
962 gsi = GlobalSymbolTable.find(TargetName.data());
Eli Benderskyfc079082012-05-01 06:58:59 +0000963 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000964 Value.SectionID = gsi->second.first;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000965 Value.Offset = gsi->second.second;
Ulrich Weigand78e97652013-04-05 13:29:04 +0000966 Value.Addend = gsi->second.second + Addend;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000967 } else {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000968 switch (SymType) {
969 case SymbolRef::ST_Debug: {
970 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
971 // and can be changed by another developers. Maybe best way is add
972 // a new symbol type ST_Section to SymbolRef and use it.
Eli Bendersky667b8792012-05-01 10:41:12 +0000973 section_iterator si(Obj.end_sections());
Rafael Espindola806f0062013-06-05 01:33:53 +0000974 Symbol->getSection(si);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000975 if (si == Obj.end_sections())
976 llvm_unreachable("Symbol section not found, bad object file format!");
977 DEBUG(dbgs() << "\t\tThis is section symbol\n");
Andrew Kaylor47328722012-10-12 23:53:16 +0000978 // Default to 'true' in case isText fails (though it never does).
979 bool isCode = true;
980 si->isText(isCode);
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000981 Value.SectionID = findOrEmitSection(Obj,
982 (*si),
983 isCode,
Andrew Kaylor47328722012-10-12 23:53:16 +0000984 ObjSectionToID);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000985 Value.Addend = Addend;
986 break;
987 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000988 case SymbolRef::ST_Data:
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000989 case SymbolRef::ST_Unknown: {
990 Value.SymbolName = TargetName.data();
991 Value.Addend = Addend;
Richard Mittonad6d3492013-08-16 18:54:26 +0000992
993 // Absolute relocations will have a zero symbol ID (STN_UNDEF), which
994 // will manifest here as a NULL symbol name.
995 // We can set this as a valid (but empty) symbol name, and rely
996 // on addRelocationForSymbol to handle this.
997 if (!Value.SymbolName)
998 Value.SymbolName = "";
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000999 break;
1000 }
1001 default:
1002 llvm_unreachable("Unresolved symbol type!");
1003 break;
1004 }
1005 }
Eli Bendersky4c647582012-01-16 08:56:09 +00001006 }
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001007 uint64_t Offset;
Rafael Espindola37008942013-04-29 19:03:21 +00001008 Check(RelI.getOffset(Offset));
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001009
1010 DEBUG(dbgs() << "\t\tSectionID: " << SectionID
1011 << " Offset: " << Offset
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001012 << "\n");
Tim Northover37cde972013-05-04 20:14:09 +00001013 if (Arch == Triple::aarch64 &&
1014 (RelType == ELF::R_AARCH64_CALL26 ||
1015 RelType == ELF::R_AARCH64_JUMP26)) {
1016 // This is an AArch64 branch relocation, need to use a stub function.
1017 DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
1018 SectionEntry &Section = Sections[SectionID];
1019
1020 // Look for an existing stub.
1021 StubMap::const_iterator i = Stubs.find(Value);
1022 if (i != Stubs.end()) {
1023 resolveRelocation(Section, Offset,
1024 (uint64_t)Section.Address + i->second, RelType, 0);
1025 DEBUG(dbgs() << " Stub function found\n");
1026 } else {
1027 // Create a new stub function.
1028 DEBUG(dbgs() << " Create a new stub function\n");
1029 Stubs[Value] = Section.StubOffset;
1030 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
1031 Section.StubOffset);
1032
1033 RelocationEntry REmovz_g3(SectionID,
1034 StubTargetAddr - Section.Address,
1035 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
1036 RelocationEntry REmovk_g2(SectionID,
1037 StubTargetAddr - Section.Address + 4,
1038 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
1039 RelocationEntry REmovk_g1(SectionID,
1040 StubTargetAddr - Section.Address + 8,
1041 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,
1058 (uint64_t)Section.Address + Section.StubOffset,
1059 RelType, 0);
1060 Section.StubOffset += getMaxStubSize();
1061 }
1062 } else if (Arch == Triple::arm &&
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001063 (RelType == ELF::R_ARM_PC24 ||
1064 RelType == ELF::R_ARM_CALL ||
1065 RelType == ELF::R_ARM_JUMP24)) {
1066 // This is an ARM branch relocation, need to use a stub function.
1067 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001068 SectionEntry &Section = Sections[SectionID];
Eli Bendersky4c647582012-01-16 08:56:09 +00001069
Eric Christopherc33f6222012-10-23 17:19:15 +00001070 // Look for an existing stub.
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001071 StubMap::const_iterator i = Stubs.find(Value);
1072 if (i != Stubs.end()) {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001073 resolveRelocation(Section, Offset,
Andrew Kaylorfb05a502012-11-02 19:45:23 +00001074 (uint64_t)Section.Address + i->second, RelType, 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001075 DEBUG(dbgs() << " Stub function found\n");
1076 } else {
1077 // Create a new stub function.
1078 DEBUG(dbgs() << " Create a new stub function\n");
1079 Stubs[Value] = Section.StubOffset;
1080 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
1081 Section.StubOffset);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001082 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Tim Northover3b684d82013-05-28 19:48:19 +00001083 ELF::R_ARM_PRIVATE_0, Value.Addend);
Eli Bendersky667b8792012-05-01 10:41:12 +00001084 if (Value.SymbolName)
1085 addRelocationForSymbol(RE, Value.SymbolName);
1086 else
1087 addRelocationForSection(RE, Value.SectionID);
1088
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001089 resolveRelocation(Section, Offset,
Andrew Kaylorfb05a502012-11-02 19:45:23 +00001090 (uint64_t)Section.Address + Section.StubOffset,
1091 RelType, 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001092 Section.StubOffset += getMaxStubSize();
1093 }
Akira Hatanakaa667aad2012-12-03 23:12:19 +00001094 } else if ((Arch == Triple::mipsel || Arch == Triple::mips) &&
1095 RelType == ELF::R_MIPS_26) {
Akira Hatanaka111174b2012-08-17 21:28:04 +00001096 // This is an Mips branch relocation, need to use a stub function.
1097 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001098 SectionEntry &Section = Sections[SectionID];
1099 uint8_t *Target = Section.Address + Offset;
Akira Hatanaka111174b2012-08-17 21:28:04 +00001100 uint32_t *TargetAddress = (uint32_t *)Target;
1101
1102 // Extract the addend from the instruction.
1103 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
1104
1105 Value.Addend += Addend;
1106
1107 // Look up for existing stub.
1108 StubMap::const_iterator i = Stubs.find(Value);
1109 if (i != Stubs.end()) {
Petar Jovanovic45115f82013-11-19 21:56:00 +00001110 RelocationEntry RE(SectionID, Offset, RelType, i->second);
1111 addRelocationForSection(RE, SectionID);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001112 DEBUG(dbgs() << " Stub function found\n");
1113 } else {
1114 // Create a new stub function.
1115 DEBUG(dbgs() << " Create a new stub function\n");
1116 Stubs[Value] = Section.StubOffset;
1117 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
1118 Section.StubOffset);
1119
1120 // Creating Hi and Lo relocations for the filled stub instructions.
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001121 RelocationEntry REHi(SectionID,
Akira Hatanaka111174b2012-08-17 21:28:04 +00001122 StubTargetAddr - Section.Address,
Akira Hatanaka2e236242013-07-24 01:58:40 +00001123 ELF::R_MIPS_UNUSED1, Value.Addend);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001124 RelocationEntry RELo(SectionID,
Akira Hatanaka111174b2012-08-17 21:28:04 +00001125 StubTargetAddr - Section.Address + 4,
Akira Hatanaka2e236242013-07-24 01:58:40 +00001126 ELF::R_MIPS_UNUSED2, Value.Addend);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001127
1128 if (Value.SymbolName) {
1129 addRelocationForSymbol(REHi, Value.SymbolName);
1130 addRelocationForSymbol(RELo, Value.SymbolName);
1131 } else {
1132 addRelocationForSection(REHi, Value.SectionID);
1133 addRelocationForSection(RELo, Value.SectionID);
1134 }
1135
Petar Jovanovic45115f82013-11-19 21:56:00 +00001136 RelocationEntry RE(SectionID, Offset, RelType, Section.StubOffset);
1137 addRelocationForSection(RE, SectionID);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001138 Section.StubOffset += getMaxStubSize();
1139 }
Bill Schmidt0a9170d2013-07-26 01:35:43 +00001140 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001141 if (RelType == ELF::R_PPC64_REL24) {
1142 // A PPC branch relocation will need a stub function if the target is
1143 // an external symbol (Symbol::ST_Unknown) or if the target address
1144 // is not within the signed 24-bits branch address.
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001145 SectionEntry &Section = Sections[SectionID];
1146 uint8_t *Target = Section.Address + Offset;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001147 bool RangeOverflow = false;
1148 if (SymType != SymbolRef::ST_Unknown) {
1149 // A function call may points to the .opd entry, so the final symbol value
1150 // in calculated based in the relocation values in .opd section.
1151 findOPDEntrySection(Obj, ObjSectionToID, Value);
1152 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
1153 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
1154 // If it is within 24-bits branch range, just set the branch target
1155 if (SignExtend32<24>(delta) == delta) {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001156 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001157 if (Value.SymbolName)
1158 addRelocationForSymbol(RE, Value.SymbolName);
1159 else
1160 addRelocationForSection(RE, Value.SectionID);
1161 } else {
1162 RangeOverflow = true;
1163 }
1164 }
1165 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
1166 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
1167 // larger than 24-bits.
1168 StubMap::const_iterator i = Stubs.find(Value);
1169 if (i != Stubs.end()) {
1170 // Symbol function stub already created, just relocate to it
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001171 resolveRelocation(Section, Offset,
Andrew Kaylorfb05a502012-11-02 19:45:23 +00001172 (uint64_t)Section.Address + i->second, RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001173 DEBUG(dbgs() << " Stub function found\n");
1174 } else {
1175 // Create a new stub function.
1176 DEBUG(dbgs() << " Create a new stub function\n");
1177 Stubs[Value] = Section.StubOffset;
1178 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
1179 Section.StubOffset);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001180 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001181 ELF::R_PPC64_ADDR64, Value.Addend);
1182
1183 // Generates the 64-bits address loads as exemplified in section
1184 // 4.5.1 in PPC64 ELF ABI.
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001185 RelocationEntry REhst(SectionID,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001186 StubTargetAddr - Section.Address + 2,
1187 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001188 RelocationEntry REhr(SectionID,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001189 StubTargetAddr - Section.Address + 6,
1190 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001191 RelocationEntry REh(SectionID,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001192 StubTargetAddr - Section.Address + 14,
1193 ELF::R_PPC64_ADDR16_HI, Value.Addend);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001194 RelocationEntry REl(SectionID,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001195 StubTargetAddr - Section.Address + 18,
1196 ELF::R_PPC64_ADDR16_LO, Value.Addend);
1197
1198 if (Value.SymbolName) {
1199 addRelocationForSymbol(REhst, Value.SymbolName);
1200 addRelocationForSymbol(REhr, Value.SymbolName);
1201 addRelocationForSymbol(REh, Value.SymbolName);
1202 addRelocationForSymbol(REl, Value.SymbolName);
1203 } else {
1204 addRelocationForSection(REhst, Value.SectionID);
1205 addRelocationForSection(REhr, Value.SectionID);
1206 addRelocationForSection(REh, Value.SectionID);
1207 addRelocationForSection(REl, Value.SectionID);
1208 }
1209
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001210 resolveRelocation(Section, Offset,
Andrew Kaylorfb05a502012-11-02 19:45:23 +00001211 (uint64_t)Section.Address + Section.StubOffset,
1212 RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001213 if (SymType == SymbolRef::ST_Unknown)
1214 // Restore the TOC for external calls
1215 writeInt32BE(Target+4, 0xE8410028); // ld r2,40(r1)
1216 Section.StubOffset += getMaxStubSize();
1217 }
1218 }
1219 } else {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001220 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001221 // Extra check to avoid relocation againt empty symbols (usually
1222 // the R_PPC64_TOC).
Richard Mittonad6d3492013-08-16 18:54:26 +00001223 if (SymType != SymbolRef::ST_Unknown && TargetName.empty())
1224 Value.SymbolName = NULL;
1225
1226 if (Value.SymbolName)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001227 addRelocationForSymbol(RE, Value.SymbolName);
1228 else
1229 addRelocationForSection(RE, Value.SectionID);
1230 }
Richard Sandifordca044082013-05-03 14:15:35 +00001231 } else if (Arch == Triple::systemz &&
1232 (RelType == ELF::R_390_PLT32DBL ||
1233 RelType == ELF::R_390_GOTENT)) {
1234 // Create function stubs for both PLT and GOT references, regardless of
1235 // whether the GOT reference is to data or code. The stub contains the
1236 // full address of the symbol, as needed by GOT references, and the
1237 // executable part only adds an overhead of 8 bytes.
1238 //
1239 // We could try to conserve space by allocating the code and data
1240 // parts of the stub separately. However, as things stand, we allocate
1241 // a stub for every relocation, so using a GOT in JIT code should be
1242 // no less space efficient than using an explicit constant pool.
1243 DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
1244 SectionEntry &Section = Sections[SectionID];
1245
1246 // Look for an existing stub.
1247 StubMap::const_iterator i = Stubs.find(Value);
1248 uintptr_t StubAddress;
1249 if (i != Stubs.end()) {
1250 StubAddress = uintptr_t(Section.Address) + i->second;
1251 DEBUG(dbgs() << " Stub function found\n");
1252 } else {
1253 // Create a new stub function.
1254 DEBUG(dbgs() << " Create a new stub function\n");
1255
1256 uintptr_t BaseAddress = uintptr_t(Section.Address);
1257 uintptr_t StubAlignment = getStubAlignment();
1258 StubAddress = (BaseAddress + Section.StubOffset +
1259 StubAlignment - 1) & -StubAlignment;
1260 unsigned StubOffset = StubAddress - BaseAddress;
1261
1262 Stubs[Value] = StubOffset;
1263 createStubFunction((uint8_t *)StubAddress);
1264 RelocationEntry RE(SectionID, StubOffset + 8,
1265 ELF::R_390_64, Value.Addend - Addend);
1266 if (Value.SymbolName)
1267 addRelocationForSymbol(RE, Value.SymbolName);
1268 else
1269 addRelocationForSection(RE, Value.SectionID);
1270 Section.StubOffset = StubOffset + getMaxStubSize();
1271 }
1272
1273 if (RelType == ELF::R_390_GOTENT)
1274 resolveRelocation(Section, Offset, StubAddress + 8,
1275 ELF::R_390_PC32DBL, Addend);
1276 else
1277 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001278 } else if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_PLT32) {
1279 // The way the PLT relocations normally work is that the linker allocates the
1280 // PLT and this relocation makes a PC-relative call into the PLT. The PLT
1281 // entry will then jump to an address provided by the GOT. On first call, the
1282 // GOT address will point back into PLT code that resolves the symbol. After
1283 // the first call, the GOT entry points to the actual function.
1284 //
1285 // For local functions we're ignoring all of that here and just replacing
1286 // the PLT32 relocation type with PC32, which will translate the relocation
1287 // into a PC-relative call directly to the function. For external symbols we
1288 // can't be sure the function will be within 2^32 bytes of the call site, so
1289 // we need to create a stub, which calls into the GOT. This case is
1290 // equivalent to the usual PLT implementation except that we use the stub
1291 // mechanism in RuntimeDyld (which puts stubs at the end of the section)
1292 // rather than allocating a PLT section.
1293 if (Value.SymbolName) {
1294 // This is a call to an external function.
1295 // Look for an existing stub.
1296 SectionEntry &Section = Sections[SectionID];
1297 StubMap::const_iterator i = Stubs.find(Value);
1298 uintptr_t StubAddress;
1299 if (i != Stubs.end()) {
1300 StubAddress = uintptr_t(Section.Address) + i->second;
1301 DEBUG(dbgs() << " Stub function found\n");
1302 } else {
1303 // Create a new stub function (equivalent to a PLT entry).
1304 DEBUG(dbgs() << " Create a new stub function\n");
1305
1306 uintptr_t BaseAddress = uintptr_t(Section.Address);
1307 uintptr_t StubAlignment = getStubAlignment();
1308 StubAddress = (BaseAddress + Section.StubOffset +
1309 StubAlignment - 1) & -StubAlignment;
1310 unsigned StubOffset = StubAddress - BaseAddress;
1311 Stubs[Value] = StubOffset;
1312 createStubFunction((uint8_t *)StubAddress);
1313
1314 // Create a GOT entry for the external function.
1315 GOTEntries.push_back(Value);
1316
1317 // Make our stub function a relative call to the GOT entry.
1318 RelocationEntry RE(SectionID, StubOffset + 2,
1319 ELF::R_X86_64_GOTPCREL, -4);
1320 addRelocationForSymbol(RE, Value.SymbolName);
1321
1322 // Bump our stub offset counter
1323 Section.StubOffset = StubOffset + getMaxStubSize();
1324 }
1325
1326 // Make the target call a call into the stub table.
1327 resolveRelocation(Section, Offset, StubAddress,
1328 ELF::R_X86_64_PC32, Addend);
1329 } else {
1330 RelocationEntry RE(SectionID, Offset, ELF::R_X86_64_PC32, Value.Addend,
1331 Value.Offset);
1332 addRelocationForSection(RE, Value.SectionID);
1333 }
Eli Bendersky667b8792012-05-01 10:41:12 +00001334 } else {
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001335 if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_GOTPCREL) {
1336 GOTEntries.push_back(Value);
1337 }
1338 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset);
Eli Bendersky667b8792012-05-01 10:41:12 +00001339 if (Value.SymbolName)
1340 addRelocationForSymbol(RE, Value.SymbolName);
1341 else
1342 addRelocationForSection(RE, Value.SectionID);
1343 }
Jim Grosbacheff0a402012-01-16 22:26:39 +00001344}
1345
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001346void RuntimeDyldELF::updateGOTEntries(StringRef Name, uint64_t Addr) {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001347
1348 SmallVectorImpl<std::pair<SID, GOTRelocations> >::iterator it;
1349 SmallVectorImpl<std::pair<SID, GOTRelocations> >::iterator end = GOTs.end();
1350
1351 for (it = GOTs.begin(); it != end; ++it) {
1352 GOTRelocations &GOTEntries = it->second;
1353 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
1354 if (GOTEntries[i].SymbolName != 0 && GOTEntries[i].SymbolName == Name) {
1355 GOTEntries[i].Offset = Addr;
1356 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001357 }
1358 }
1359}
1360
1361size_t RuntimeDyldELF::getGOTEntrySize() {
1362 // We don't use the GOT in all of these cases, but it's essentially free
1363 // to put them all here.
1364 size_t Result = 0;
1365 switch (Arch) {
1366 case Triple::x86_64:
1367 case Triple::aarch64:
1368 case Triple::ppc64:
1369 case Triple::ppc64le:
1370 case Triple::systemz:
1371 Result = sizeof(uint64_t);
1372 break;
1373 case Triple::x86:
1374 case Triple::arm:
1375 case Triple::thumb:
1376 case Triple::mips:
1377 case Triple::mipsel:
1378 Result = sizeof(uint32_t);
1379 break;
1380 default: llvm_unreachable("Unsupported CPU type!");
1381 }
1382 return Result;
1383}
1384
1385uint64_t RuntimeDyldELF::findGOTEntry(uint64_t LoadAddress,
1386 uint64_t Offset) {
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001387
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001388 const size_t GOTEntrySize = getGOTEntrySize();
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001389
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001390 SmallVectorImpl<std::pair<SID, GOTRelocations> >::const_iterator it;
1391 SmallVectorImpl<std::pair<SID, GOTRelocations> >::const_iterator end = GOTs.end();
1392
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001393 int GOTIndex = -1;
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001394 for (it = GOTs.begin(); it != end; ++it) {
1395 SID GOTSectionID = it->first;
1396 const GOTRelocations &GOTEntries = it->second;
1397
1398 // Find the matching entry in our vector.
1399 uint64_t SymbolOffset = 0;
1400 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
1401 if (GOTEntries[i].SymbolName == 0) {
1402 if (getSectionLoadAddress(GOTEntries[i].SectionID) == LoadAddress &&
1403 GOTEntries[i].Offset == Offset) {
1404 GOTIndex = i;
1405 SymbolOffset = GOTEntries[i].Offset;
1406 break;
1407 }
1408 } else {
1409 // GOT entries for external symbols use the addend as the address when
1410 // the external symbol has been resolved.
1411 if (GOTEntries[i].Offset == LoadAddress) {
1412 GOTIndex = i;
1413 // Don't use the Addend here. The relocation handler will use it.
1414 break;
1415 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001416 }
1417 }
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001418
1419 if (GOTIndex != -1) {
1420 if (GOTEntrySize == sizeof(uint64_t)) {
1421 uint64_t *LocalGOTAddr = (uint64_t*)getSectionAddress(GOTSectionID);
1422 // Fill in this entry with the address of the symbol being referenced.
1423 LocalGOTAddr[GOTIndex] = LoadAddress + SymbolOffset;
1424 } else {
1425 uint32_t *LocalGOTAddr = (uint32_t*)getSectionAddress(GOTSectionID);
1426 // Fill in this entry with the address of the symbol being referenced.
1427 LocalGOTAddr[GOTIndex] = (uint32_t)(LoadAddress + SymbolOffset);
1428 }
1429
1430 // Calculate the load address of this entry
1431 return getSectionLoadAddress(GOTSectionID) + (GOTIndex * GOTEntrySize);
1432 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001433 }
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001434
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001435 assert(GOTIndex != -1 && "Unable to find requested GOT entry.");
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001436 return 0;
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001437}
1438
Andrew Kaylor7bb13442013-10-11 21:25:48 +00001439void RuntimeDyldELF::finalizeLoad(ObjSectionToIDMap &SectionMap) {
1440 // If necessary, allocate the global offset table
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001441 if (MemMgr) {
1442 // Allocate the GOT if necessary
1443 size_t numGOTEntries = GOTEntries.size();
1444 if (numGOTEntries != 0) {
1445 // Allocate memory for the section
1446 unsigned SectionID = Sections.size();
1447 size_t TotalSize = numGOTEntries * getGOTEntrySize();
1448 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, getGOTEntrySize(),
1449 SectionID, ".got", false);
1450 if (!Addr)
1451 report_fatal_error("Unable to allocate memory for GOT!");
1452
1453 GOTs.push_back(std::make_pair(SectionID, GOTEntries));
1454 Sections.push_back(SectionEntry(".got", Addr, TotalSize, 0));
1455 // For now, initialize all GOT entries to zero. We'll fill them in as
1456 // needed when GOT-based relocations are applied.
1457 memset(Addr, 0, TotalSize);
1458 }
1459 }
1460 else {
1461 report_fatal_error("Unable to allocate memory for GOT!");
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001462 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +00001463
1464 // Look for and record the EH frame section.
1465 ObjSectionToIDMap::iterator i, e;
1466 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
1467 const SectionRef &Section = i->first;
1468 StringRef Name;
1469 Section.getName(Name);
1470 if (Name == ".eh_frame") {
1471 UnregisteredEHFrameSections.push_back(i->second);
1472 break;
1473 }
1474 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001475}
1476
Andrew Kayloradc70562012-10-02 21:18:39 +00001477bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
1478 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
1479 return false;
1480 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
Eli Bendersky4c647582012-01-16 08:56:09 +00001481}
Lang Hames173c69f2014-01-08 04:09:09 +00001482
1483bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile *Obj) const {
1484 return Obj->isELF();
1485}
1486
Eli Bendersky4c647582012-01-16 08:56:09 +00001487} // namespace llvm