1 //===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implementation of ELF support for the MC-JIT runtime dynamic linker.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "dyld"
15 #include "RuntimeDyldELF.h"
16 #include "JITRegistrar.h"
17 #include "ObjectImageCommon.h"
18 #include "llvm/ADT/IntervalMap.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/ExecutionEngine/ObjectBuffer.h"
23 #include "llvm/ExecutionEngine/ObjectImage.h"
24 #include "llvm/Object/ELFObjectFile.h"
25 #include "llvm/Object/ObjectFile.h"
26 #include "llvm/Support/ELF.h"
27 #include "llvm/Support/MemoryBuffer.h"
30 using namespace llvm::object;
35 error_code check(error_code Err) {
37 report_fatal_error(Err.message());
44 : public ELFObjectFile<ELFT> {
45 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
47 typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
48 typedef Elf_Sym_Impl<ELFT> Elf_Sym;
50 Elf_Rel_Impl<ELFT, false> Elf_Rel;
52 Elf_Rel_Impl<ELFT, true> Elf_Rela;
54 typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
56 typedef typename ELFDataTypeTypedefHelper<
57 ELFT>::value_type addr_type;
60 DyldELFObject(MemoryBuffer *Wrapper, error_code &ec);
62 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
63 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr);
65 // Methods for type inquiry through isa, cast and dyn_cast
66 static inline bool classof(const Binary *v) {
67 return (isa<ELFObjectFile<ELFT> >(v)
68 && classof(cast<ELFObjectFile
71 static inline bool classof(
72 const ELFObjectFile<ELFT> *v) {
73 return v->isDyldType();
78 class ELFObjectImage : public ObjectImageCommon {
80 DyldELFObject<ELFT> *DyldObj;
84 ELFObjectImage(ObjectBuffer *Input,
85 DyldELFObject<ELFT> *Obj)
86 : ObjectImageCommon(Input, Obj),
90 virtual ~ELFObjectImage() {
92 deregisterWithDebugger();
95 // Subclasses can override these methods to update the image with loaded
96 // addresses for sections and common symbols
97 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr) override {
98 DyldObj->updateSectionAddress(Sec, Addr);
101 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr) override {
102 DyldObj->updateSymbolAddress(Sym, Addr);
105 void registerWithDebugger() override {
106 JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
109 void deregisterWithDebugger() override {
110 JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer);
114 // The MemoryBuffer passed into this constructor is just a wrapper around the
115 // actual memory. Ultimately, the Binary parent class will take ownership of
116 // this MemoryBuffer object but not the underlying memory.
118 DyldELFObject<ELFT>::DyldELFObject(MemoryBuffer *Wrapper, error_code &ec)
119 : ELFObjectFile<ELFT>(Wrapper, ec) {
120 this->isDyldELFObject = true;
124 void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec,
126 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
127 Elf_Shdr *shdr = const_cast<Elf_Shdr*>(
128 reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
130 // This assumes the address passed in matches the target address bitness
131 // The template-based type cast handles everything else.
132 shdr->sh_addr = static_cast<addr_type>(Addr);
136 void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef,
139 Elf_Sym *sym = const_cast<Elf_Sym*>(
140 ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl()));
142 // This assumes the address passed in matches the target address bitness
143 // The template-based type cast handles everything else.
144 sym->st_value = static_cast<addr_type>(Addr);
151 void RuntimeDyldELF::registerEHFrames() {
154 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
155 SID EHFrameSID = UnregisteredEHFrameSections[i];
156 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
157 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
158 size_t EHFrameSize = Sections[EHFrameSID].Size;
159 MemMgr->registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
160 RegisteredEHFrameSections.push_back(EHFrameSID);
162 UnregisteredEHFrameSections.clear();
165 void RuntimeDyldELF::deregisterEHFrames() {
168 for (int i = 0, e = RegisteredEHFrameSections.size(); i != e; ++i) {
169 SID EHFrameSID = RegisteredEHFrameSections[i];
170 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
171 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
172 size_t EHFrameSize = Sections[EHFrameSID].Size;
173 MemMgr->deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
175 RegisteredEHFrameSections.clear();
178 ObjectImage *RuntimeDyldELF::createObjectImageFromFile(object::ObjectFile *ObjFile) {
183 MemoryBuffer* Buffer = MemoryBuffer::getMemBuffer(ObjFile->getData(),
187 if (ObjFile->getBytesInAddress() == 4 && ObjFile->isLittleEndian()) {
188 DyldELFObject<ELFType<support::little, 2, false> > *Obj =
189 new DyldELFObject<ELFType<support::little, 2, false> >(Buffer, ec);
190 return new ELFObjectImage<ELFType<support::little, 2, false> >(NULL, Obj);
192 else if (ObjFile->getBytesInAddress() == 4 && !ObjFile->isLittleEndian()) {
193 DyldELFObject<ELFType<support::big, 2, false> > *Obj =
194 new DyldELFObject<ELFType<support::big, 2, false> >(Buffer, ec);
195 return new ELFObjectImage<ELFType<support::big, 2, false> >(NULL, Obj);
197 else if (ObjFile->getBytesInAddress() == 8 && !ObjFile->isLittleEndian()) {
198 DyldELFObject<ELFType<support::big, 2, true> > *Obj =
199 new DyldELFObject<ELFType<support::big, 2, true> >(Buffer, ec);
200 return new ELFObjectImage<ELFType<support::big, 2, true> >(NULL, Obj);
202 else if (ObjFile->getBytesInAddress() == 8 && ObjFile->isLittleEndian()) {
203 DyldELFObject<ELFType<support::little, 2, true> > *Obj =
204 new DyldELFObject<ELFType<support::little, 2, true> >(Buffer, ec);
205 return new ELFObjectImage<ELFType<support::little, 2, true> >(NULL, Obj);
208 llvm_unreachable("Unexpected ELF format");
211 ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) {
212 if (Buffer->getBufferSize() < ELF::EI_NIDENT)
213 llvm_unreachable("Unexpected ELF object size");
214 std::pair<unsigned char, unsigned char> Ident = std::make_pair(
215 (uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS],
216 (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]);
219 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
220 DyldELFObject<ELFType<support::little, 4, false> > *Obj =
221 new DyldELFObject<ELFType<support::little, 4, false> >(
222 Buffer->getMemBuffer(), ec);
223 return new ELFObjectImage<ELFType<support::little, 4, false> >(Buffer, Obj);
225 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
226 DyldELFObject<ELFType<support::big, 4, false> > *Obj =
227 new DyldELFObject<ELFType<support::big, 4, false> >(
228 Buffer->getMemBuffer(), ec);
229 return new ELFObjectImage<ELFType<support::big, 4, false> >(Buffer, Obj);
231 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
232 DyldELFObject<ELFType<support::big, 8, true> > *Obj =
233 new DyldELFObject<ELFType<support::big, 8, true> >(
234 Buffer->getMemBuffer(), ec);
235 return new ELFObjectImage<ELFType<support::big, 8, true> >(Buffer, Obj);
237 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
238 DyldELFObject<ELFType<support::little, 8, true> > *Obj =
239 new DyldELFObject<ELFType<support::little, 8, true> >(
240 Buffer->getMemBuffer(), ec);
241 return new ELFObjectImage<ELFType<support::little, 8, true> >(Buffer, Obj);
244 llvm_unreachable("Unexpected ELF format");
247 RuntimeDyldELF::~RuntimeDyldELF() {
250 void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
255 uint64_t SymOffset) {
258 llvm_unreachable("Relocation type not implemented yet!");
260 case ELF::R_X86_64_64: {
261 uint64_t *Target = reinterpret_cast<uint64_t*>(Section.Address + Offset);
262 *Target = Value + Addend;
263 DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend))
264 << " at " << format("%p\n",Target));
267 case ELF::R_X86_64_32:
268 case ELF::R_X86_64_32S: {
270 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
271 (Type == ELF::R_X86_64_32S &&
272 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
273 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
274 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
275 *Target = TruncatedAddr;
276 DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr)
277 << " at " << format("%p\n",Target));
280 case ELF::R_X86_64_GOTPCREL: {
281 // findGOTEntry returns the 'G + GOT' part of the relocation calculation
282 // based on the load/target address of the GOT (not the current/local addr).
283 uint64_t GOTAddr = findGOTEntry(Value, SymOffset);
284 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
285 uint64_t FinalAddress = Section.LoadAddress + Offset;
286 // The processRelocationRef method combines the symbol offset and the addend
287 // and in most cases that's what we want. For this relocation type, we need
288 // the raw addend, so we subtract the symbol offset to get it.
289 int64_t RealOffset = GOTAddr + Addend - SymOffset - FinalAddress;
290 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
291 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
292 *Target = TruncOffset;
295 case ELF::R_X86_64_PC32: {
296 // Get the placeholder value from the generated object since
297 // a previous relocation attempt may have overwritten the loaded version
298 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
300 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
301 uint64_t FinalAddress = Section.LoadAddress + Offset;
302 int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
303 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
304 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
305 *Target = TruncOffset;
308 case ELF::R_X86_64_PC64: {
309 // Get the placeholder value from the generated object since
310 // a previous relocation attempt may have overwritten the loaded version
311 uint64_t *Placeholder = reinterpret_cast<uint64_t*>(Section.ObjAddress
313 uint64_t *Target = reinterpret_cast<uint64_t*>(Section.Address + Offset);
314 uint64_t FinalAddress = Section.LoadAddress + Offset;
315 *Target = *Placeholder + Value + Addend - FinalAddress;
321 void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
327 case ELF::R_386_32: {
328 // Get the placeholder value from the generated object since
329 // a previous relocation attempt may have overwritten the loaded version
330 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
332 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
333 *Target = *Placeholder + Value + Addend;
336 case ELF::R_386_PC32: {
337 // Get the placeholder value from the generated object since
338 // a previous relocation attempt may have overwritten the loaded version
339 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
341 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
342 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
343 uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
344 *Target = RealOffset;
348 // There are other relocation types, but it appears these are the
349 // only ones currently used by the LLVM ELF object writer
350 llvm_unreachable("Relocation type not implemented yet!");
355 void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
360 uint32_t *TargetPtr = reinterpret_cast<uint32_t*>(Section.Address + Offset);
361 uint64_t FinalAddress = Section.LoadAddress + Offset;
363 DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
364 << format("%llx", Section.Address + Offset)
365 << " FinalAddress: 0x" << format("%llx",FinalAddress)
366 << " Value: 0x" << format("%llx",Value)
367 << " Type: 0x" << format("%x",Type)
368 << " Addend: 0x" << format("%llx",Addend)
373 llvm_unreachable("Relocation type not implemented yet!");
375 case ELF::R_AARCH64_ABS64: {
376 uint64_t *TargetPtr = reinterpret_cast<uint64_t*>(Section.Address + Offset);
377 *TargetPtr = Value + Addend;
380 case ELF::R_AARCH64_PREL32: {
381 uint64_t Result = Value + Addend - FinalAddress;
382 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
383 static_cast<int64_t>(Result) <= UINT32_MAX);
384 *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU);
387 case ELF::R_AARCH64_CALL26: // fallthrough
388 case ELF::R_AARCH64_JUMP26: {
389 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
391 uint64_t BranchImm = Value + Addend - FinalAddress;
393 // "Check that -2^27 <= result < 2^27".
394 assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) &&
395 static_cast<int64_t>(BranchImm) < (1LL << 27));
397 // AArch64 code is emitted with .rela relocations. The data already in any
398 // bits affected by the relocation on entry is garbage.
399 *TargetPtr &= 0xfc000000U;
400 // Immediate goes in bits 25:0 of B and BL.
401 *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2;
404 case ELF::R_AARCH64_MOVW_UABS_G3: {
405 uint64_t Result = Value + Addend;
407 // AArch64 code is emitted with .rela relocations. The data already in any
408 // bits affected by the relocation on entry is garbage.
409 *TargetPtr &= 0xffe0001fU;
410 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
411 *TargetPtr |= Result >> (48 - 5);
412 // Shift must be "lsl #48", in bits 22:21
413 assert((*TargetPtr >> 21 & 0x3) == 3 && "invalid shift for relocation");
416 case ELF::R_AARCH64_MOVW_UABS_G2_NC: {
417 uint64_t Result = Value + Addend;
419 // AArch64 code is emitted with .rela relocations. The data already in any
420 // bits affected by the relocation on entry is garbage.
421 *TargetPtr &= 0xffe0001fU;
422 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
423 *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
424 // Shift must be "lsl #32", in bits 22:21
425 assert((*TargetPtr >> 21 & 0x3) == 2 && "invalid shift for relocation");
428 case ELF::R_AARCH64_MOVW_UABS_G1_NC: {
429 uint64_t Result = Value + Addend;
431 // AArch64 code is emitted with .rela relocations. The data already in any
432 // bits affected by the relocation on entry is garbage.
433 *TargetPtr &= 0xffe0001fU;
434 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
435 *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
436 // Shift must be "lsl #16", in bits 22:2
437 assert((*TargetPtr >> 21 & 0x3) == 1 && "invalid shift for relocation");
440 case ELF::R_AARCH64_MOVW_UABS_G0_NC: {
441 uint64_t Result = Value + Addend;
443 // AArch64 code is emitted with .rela relocations. The data already in any
444 // bits affected by the relocation on entry is garbage.
445 *TargetPtr &= 0xffe0001fU;
446 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
447 *TargetPtr |= ((Result & 0xffffU) << 5);
448 // Shift must be "lsl #0", in bits 22:21.
449 assert((*TargetPtr >> 21 & 0x3) == 0 && "invalid shift for relocation");
452 case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
453 // Operation: Page(S+A) - Page(P)
454 uint64_t Result = ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
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");
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));
470 case ELF::R_AARCH64_LDST32_ABS_LO12_NC: {
472 uint64_t Result = Value + Addend;
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));
482 case ELF::R_AARCH64_LDST64_ABS_LO12_NC: {
484 uint64_t Result = Value + Addend;
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));
497 void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
502 // TODO: Add Thumb relocations.
503 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress +
505 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
506 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
509 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
510 << Section.Address + Offset
511 << " FinalAddress: " << format("%p",FinalAddress)
512 << " Value: " << format("%x",Value)
513 << " Type: " << format("%x",Type)
514 << " Addend: " << format("%x",Addend)
519 llvm_unreachable("Not implemented relocation type!");
521 case ELF::R_ARM_NONE:
523 // Write a 32bit value to relocation address, taking into account the
524 // implicit addend encoded in the target.
525 case ELF::R_ARM_PREL31:
526 case ELF::R_ARM_TARGET1:
527 case ELF::R_ARM_ABS32:
528 *TargetPtr = *Placeholder + Value;
530 // Write first 16 bit of 32 bit value to the mov instruction.
531 // Last 4 bit should be shifted.
532 case ELF::R_ARM_MOVW_ABS_NC:
533 // We are not expecting any other addend in the relocation address.
534 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
535 // non-contiguous fields.
536 assert((*Placeholder & 0x000F0FFF) == 0);
537 Value = Value & 0xFFFF;
538 *TargetPtr = *Placeholder | (Value & 0xFFF);
539 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
541 // Write last 16 bit of 32 bit value to the mov instruction.
542 // Last 4 bit should be shifted.
543 case ELF::R_ARM_MOVT_ABS:
544 // We are not expecting any other addend in the relocation address.
545 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
546 assert((*Placeholder & 0x000F0FFF) == 0);
548 Value = (Value >> 16) & 0xFFFF;
549 *TargetPtr = *Placeholder | (Value & 0xFFF);
550 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
552 // Write 24 bit relative value to the branch instruction.
553 case ELF::R_ARM_PC24 : // Fall through.
554 case ELF::R_ARM_CALL : // Fall through.
555 case ELF::R_ARM_JUMP24: {
556 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
557 RelValue = (RelValue & 0x03FFFFFC) >> 2;
558 assert((*TargetPtr & 0xFFFFFF) == 0xFFFFFE);
559 *TargetPtr &= 0xFF000000;
560 *TargetPtr |= RelValue;
563 case ELF::R_ARM_PRIVATE_0:
564 // This relocation is reserved by the ARM ELF ABI for internal use. We
565 // appropriate it here to act as an R_ARM_ABS32 without any addend for use
566 // in the stubs created during JIT (which can't put an addend into the
567 // original object file).
573 void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
578 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress +
580 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
583 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
584 << Section.Address + Offset
586 << format("%p",Section.LoadAddress + Offset)
587 << " Value: " << format("%x",Value)
588 << " Type: " << format("%x",Type)
589 << " Addend: " << format("%x",Addend)
594 llvm_unreachable("Not implemented relocation type!");
597 *TargetPtr = Value + (*Placeholder);
600 *TargetPtr = ((*Placeholder) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
602 case ELF::R_MIPS_HI16:
603 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
604 Value += ((*Placeholder) & 0x0000ffff) << 16;
605 *TargetPtr = ((*Placeholder) & 0xffff0000) |
606 (((Value + 0x8000) >> 16) & 0xffff);
608 case ELF::R_MIPS_LO16:
609 Value += ((*Placeholder) & 0x0000ffff);
610 *TargetPtr = ((*Placeholder) & 0xffff0000) | (Value & 0xffff);
612 case ELF::R_MIPS_UNUSED1:
613 // Similar to ELF::R_ARM_PRIVATE_0, R_MIPS_UNUSED1 and R_MIPS_UNUSED2
614 // are used for internal JIT purpose. These relocations are similar to
615 // R_MIPS_HI16 and R_MIPS_LO16, but they do not take any addend into
617 *TargetPtr = ((*TargetPtr) & 0xffff0000) |
618 (((Value + 0x8000) >> 16) & 0xffff);
620 case ELF::R_MIPS_UNUSED2:
621 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
626 // Return the .TOC. section address to R_PPC64_TOC relocations.
627 uint64_t RuntimeDyldELF::findPPC64TOC() const {
628 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
629 // order. The TOC starts where the first of these sections starts.
630 SectionList::const_iterator it = Sections.begin();
631 SectionList::const_iterator ite = Sections.end();
632 for (; it != ite; ++it) {
633 if (it->Name == ".got" ||
634 it->Name == ".toc" ||
635 it->Name == ".tocbss" ||
640 // This may happen for
641 // * references to TOC base base (sym@toc, .odp relocation) without
643 // In this case just use the first section (which is usually
644 // the .odp) since the code won't reference the .toc base
646 it = Sections.begin();
649 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
650 // thus permitting a full 64 Kbytes segment.
651 return it->LoadAddress + 0x8000;
654 // Returns the sections and offset associated with the ODP entry referenced
656 void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
657 ObjSectionToIDMap &LocalSections,
658 RelocationValueRef &Rel) {
659 // Get the ELF symbol value (st_value) to compare with Relocation offset in
661 for (section_iterator si = Obj.begin_sections(), se = Obj.end_sections();
663 section_iterator RelSecI = si->getRelocatedSection();
664 if (RelSecI == Obj.end_sections())
667 StringRef RelSectionName;
668 check(RelSecI->getName(RelSectionName));
669 if (RelSectionName != ".opd")
672 for (relocation_iterator i = si->relocation_begin(),
673 e = si->relocation_end(); i != e;) {
674 // The R_PPC64_ADDR64 relocation indicates the first field
677 check(i->getType(TypeFunc));
678 if (TypeFunc != ELF::R_PPC64_ADDR64) {
683 uint64_t TargetSymbolOffset;
684 symbol_iterator TargetSymbol = i->getSymbol();
685 check(i->getOffset(TargetSymbolOffset));
687 check(getELFRelocationAddend(*i, Addend));
693 // Just check if following relocation is a R_PPC64_TOC
695 check(i->getType(TypeTOC));
696 if (TypeTOC != ELF::R_PPC64_TOC)
699 // Finally compares the Symbol value and the target symbol offset
700 // to check if this .opd entry refers to the symbol the relocation
702 if (Rel.Addend != (int64_t)TargetSymbolOffset)
705 section_iterator tsi(Obj.end_sections());
706 check(TargetSymbol->getSection(tsi));
709 Rel.SectionID = findOrEmitSection(Obj, (*tsi), IsCode, LocalSections);
710 Rel.Addend = (intptr_t)Addend;
714 llvm_unreachable("Attempting to get address of ODP entry!");
717 // Relocation masks following the #lo(value), #hi(value), #higher(value),
718 // and #highest(value) macros defined in section 4.5.1. Relocation Types
719 // in PPC-elf64abi document.
722 uint16_t applyPPClo (uint64_t value)
724 return value & 0xffff;
728 uint16_t applyPPChi (uint64_t value)
730 return (value >> 16) & 0xffff;
734 uint16_t applyPPChigher (uint64_t value)
736 return (value >> 32) & 0xffff;
740 uint16_t applyPPChighest (uint64_t value)
742 return (value >> 48) & 0xffff;
745 void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
750 uint8_t* LocalAddress = Section.Address + Offset;
753 llvm_unreachable("Relocation type not implemented yet!");
755 case ELF::R_PPC64_ADDR16_LO :
756 writeInt16BE(LocalAddress, applyPPClo (Value + Addend));
758 case ELF::R_PPC64_ADDR16_HI :
759 writeInt16BE(LocalAddress, applyPPChi (Value + Addend));
761 case ELF::R_PPC64_ADDR16_HIGHER :
762 writeInt16BE(LocalAddress, applyPPChigher (Value + Addend));
764 case ELF::R_PPC64_ADDR16_HIGHEST :
765 writeInt16BE(LocalAddress, applyPPChighest (Value + Addend));
767 case ELF::R_PPC64_ADDR14 : {
768 assert(((Value + Addend) & 3) == 0);
769 // Preserve the AA/LK bits in the branch instruction
770 uint8_t aalk = *(LocalAddress+3);
771 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
773 case ELF::R_PPC64_ADDR32 : {
774 int32_t Result = static_cast<int32_t>(Value + Addend);
775 if (SignExtend32<32>(Result) != Result)
776 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
777 writeInt32BE(LocalAddress, Result);
779 case ELF::R_PPC64_REL24 : {
780 uint64_t FinalAddress = (Section.LoadAddress + Offset);
781 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
782 if (SignExtend32<24>(delta) != delta)
783 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
784 // Generates a 'bl <address>' instruction
785 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
787 case ELF::R_PPC64_REL32 : {
788 uint64_t FinalAddress = (Section.LoadAddress + Offset);
789 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
790 if (SignExtend32<32>(delta) != delta)
791 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
792 writeInt32BE(LocalAddress, delta);
794 case ELF::R_PPC64_REL64: {
795 uint64_t FinalAddress = (Section.LoadAddress + Offset);
796 uint64_t Delta = Value - FinalAddress + Addend;
797 writeInt64BE(LocalAddress, Delta);
799 case ELF::R_PPC64_ADDR64 :
800 writeInt64BE(LocalAddress, Value + Addend);
802 case ELF::R_PPC64_TOC :
803 writeInt64BE(LocalAddress, findPPC64TOC());
805 case ELF::R_PPC64_TOC16 : {
806 uint64_t TOCStart = findPPC64TOC();
807 Value = applyPPClo((Value + Addend) - TOCStart);
808 writeInt16BE(LocalAddress, applyPPClo(Value));
810 case ELF::R_PPC64_TOC16_DS : {
811 uint64_t TOCStart = findPPC64TOC();
812 Value = ((Value + Addend) - TOCStart);
813 writeInt16BE(LocalAddress, applyPPClo(Value));
818 void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
823 uint8_t *LocalAddress = Section.Address + Offset;
826 llvm_unreachable("Relocation type not implemented yet!");
828 case ELF::R_390_PC16DBL:
829 case ELF::R_390_PLT16DBL: {
830 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
831 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
832 writeInt16BE(LocalAddress, Delta / 2);
835 case ELF::R_390_PC32DBL:
836 case ELF::R_390_PLT32DBL: {
837 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
838 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
839 writeInt32BE(LocalAddress, Delta / 2);
842 case ELF::R_390_PC32: {
843 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
844 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
845 writeInt32BE(LocalAddress, Delta);
849 writeInt64BE(LocalAddress, Value + Addend);
854 // The target location for the relocation is described by RE.SectionID and
855 // RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
856 // SectionEntry has three members describing its location.
857 // SectionEntry::Address is the address at which the section has been loaded
858 // into memory in the current (host) process. SectionEntry::LoadAddress is the
859 // address that the section will have in the target process.
860 // SectionEntry::ObjAddress is the address of the bits for this section in the
861 // original emitted object image (also in the current address space).
863 // Relocations will be applied as if the section were loaded at
864 // SectionEntry::LoadAddress, but they will be applied at an address based
865 // on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
866 // Target memory contents if they are required for value calculations.
868 // The Value parameter here is the load address of the symbol for the
869 // relocation to be applied. For relocations which refer to symbols in the
870 // current object Value will be the LoadAddress of the section in which
871 // the symbol resides (RE.Addend provides additional information about the
872 // symbol location). For external symbols, Value will be the address of the
873 // symbol in the target address space.
874 void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
876 const SectionEntry &Section = Sections[RE.SectionID];
877 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
881 void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
886 uint64_t SymOffset) {
889 resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
892 resolveX86Relocation(Section, Offset,
893 (uint32_t)(Value & 0xffffffffL), Type,
894 (uint32_t)(Addend & 0xffffffffL));
896 case Triple::aarch64:
897 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
899 case Triple::arm: // Fall through.
901 resolveARMRelocation(Section, Offset,
902 (uint32_t)(Value & 0xffffffffL), Type,
903 (uint32_t)(Addend & 0xffffffffL));
905 case Triple::mips: // Fall through.
907 resolveMIPSRelocation(Section, Offset,
908 (uint32_t)(Value & 0xffffffffL), Type,
909 (uint32_t)(Addend & 0xffffffffL));
911 case Triple::ppc64: // Fall through.
912 case Triple::ppc64le:
913 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
915 case Triple::systemz:
916 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
918 default: llvm_unreachable("Unsupported CPU type!");
922 void RuntimeDyldELF::processRelocationRef(unsigned SectionID,
925 ObjSectionToIDMap &ObjSectionToID,
926 const SymbolTableMap &Symbols,
929 Check(RelI.getType(RelType));
931 Check(getELFRelocationAddend(RelI, Addend));
932 symbol_iterator Symbol = RelI.getSymbol();
934 // Obtain the symbol name which is referenced in the relocation
935 StringRef TargetName;
936 if (Symbol != Obj.end_symbols())
937 Symbol->getName(TargetName);
938 DEBUG(dbgs() << "\t\tRelType: " << RelType
939 << " Addend: " << Addend
940 << " TargetName: " << TargetName
942 RelocationValueRef Value;
943 // First search for the symbol in the local symbol table
944 SymbolTableMap::const_iterator lsi = Symbols.end();
945 SymbolRef::Type SymType = SymbolRef::ST_Unknown;
946 if (Symbol != Obj.end_symbols()) {
947 lsi = Symbols.find(TargetName.data());
948 Symbol->getType(SymType);
950 if (lsi != Symbols.end()) {
951 Value.SectionID = lsi->second.first;
952 Value.Offset = lsi->second.second;
953 Value.Addend = lsi->second.second + Addend;
955 // Search for the symbol in the global symbol table
956 SymbolTableMap::const_iterator gsi = GlobalSymbolTable.end();
957 if (Symbol != Obj.end_symbols())
958 gsi = GlobalSymbolTable.find(TargetName.data());
959 if (gsi != GlobalSymbolTable.end()) {
960 Value.SectionID = gsi->second.first;
961 Value.Offset = gsi->second.second;
962 Value.Addend = gsi->second.second + Addend;
965 case SymbolRef::ST_Debug: {
966 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
967 // and can be changed by another developers. Maybe best way is add
968 // a new symbol type ST_Section to SymbolRef and use it.
969 section_iterator si(Obj.end_sections());
970 Symbol->getSection(si);
971 if (si == Obj.end_sections())
972 llvm_unreachable("Symbol section not found, bad object file format!");
973 DEBUG(dbgs() << "\t\tThis is section symbol\n");
974 // Default to 'true' in case isText fails (though it never does).
977 Value.SectionID = findOrEmitSection(Obj,
981 Value.Addend = Addend;
984 case SymbolRef::ST_Data:
985 case SymbolRef::ST_Unknown: {
986 Value.SymbolName = TargetName.data();
987 Value.Addend = Addend;
989 // Absolute relocations will have a zero symbol ID (STN_UNDEF), which
990 // will manifest here as a NULL symbol name.
991 // We can set this as a valid (but empty) symbol name, and rely
992 // on addRelocationForSymbol to handle this.
993 if (!Value.SymbolName)
994 Value.SymbolName = "";
998 llvm_unreachable("Unresolved symbol type!");
1004 Check(RelI.getOffset(Offset));
1006 DEBUG(dbgs() << "\t\tSectionID: " << SectionID
1007 << " Offset: " << Offset
1009 if (Arch == Triple::aarch64 &&
1010 (RelType == ELF::R_AARCH64_CALL26 ||
1011 RelType == ELF::R_AARCH64_JUMP26)) {
1012 // This is an AArch64 branch relocation, need to use a stub function.
1013 DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
1014 SectionEntry &Section = Sections[SectionID];
1016 // Look for an existing stub.
1017 StubMap::const_iterator i = Stubs.find(Value);
1018 if (i != Stubs.end()) {
1019 resolveRelocation(Section, Offset,
1020 (uint64_t)Section.Address + i->second, RelType, 0);
1021 DEBUG(dbgs() << " Stub function found\n");
1023 // Create a new stub function.
1024 DEBUG(dbgs() << " Create a new stub function\n");
1025 Stubs[Value] = Section.StubOffset;
1026 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
1027 Section.StubOffset);
1029 RelocationEntry REmovz_g3(SectionID,
1030 StubTargetAddr - Section.Address,
1031 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
1032 RelocationEntry REmovk_g2(SectionID,
1033 StubTargetAddr - Section.Address + 4,
1034 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
1035 RelocationEntry REmovk_g1(SectionID,
1036 StubTargetAddr - Section.Address + 8,
1037 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
1038 RelocationEntry REmovk_g0(SectionID,
1039 StubTargetAddr - Section.Address + 12,
1040 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
1042 if (Value.SymbolName) {
1043 addRelocationForSymbol(REmovz_g3, Value.SymbolName);
1044 addRelocationForSymbol(REmovk_g2, Value.SymbolName);
1045 addRelocationForSymbol(REmovk_g1, Value.SymbolName);
1046 addRelocationForSymbol(REmovk_g0, Value.SymbolName);
1048 addRelocationForSection(REmovz_g3, Value.SectionID);
1049 addRelocationForSection(REmovk_g2, Value.SectionID);
1050 addRelocationForSection(REmovk_g1, Value.SectionID);
1051 addRelocationForSection(REmovk_g0, Value.SectionID);
1053 resolveRelocation(Section, Offset,
1054 (uint64_t)Section.Address + Section.StubOffset,
1056 Section.StubOffset += getMaxStubSize();
1058 } else if (Arch == Triple::arm &&
1059 (RelType == ELF::R_ARM_PC24 ||
1060 RelType == ELF::R_ARM_CALL ||
1061 RelType == ELF::R_ARM_JUMP24)) {
1062 // This is an ARM branch relocation, need to use a stub function.
1063 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
1064 SectionEntry &Section = Sections[SectionID];
1066 // Look for an existing stub.
1067 StubMap::const_iterator i = Stubs.find(Value);
1068 if (i != Stubs.end()) {
1069 resolveRelocation(Section, Offset,
1070 (uint64_t)Section.Address + i->second, RelType, 0);
1071 DEBUG(dbgs() << " Stub function found\n");
1073 // Create a new stub function.
1074 DEBUG(dbgs() << " Create a new stub function\n");
1075 Stubs[Value] = Section.StubOffset;
1076 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
1077 Section.StubOffset);
1078 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
1079 ELF::R_ARM_PRIVATE_0, Value.Addend);
1080 if (Value.SymbolName)
1081 addRelocationForSymbol(RE, Value.SymbolName);
1083 addRelocationForSection(RE, Value.SectionID);
1085 resolveRelocation(Section, Offset,
1086 (uint64_t)Section.Address + Section.StubOffset,
1088 Section.StubOffset += getMaxStubSize();
1090 } else if ((Arch == Triple::mipsel || Arch == Triple::mips) &&
1091 RelType == ELF::R_MIPS_26) {
1092 // This is an Mips branch relocation, need to use a stub function.
1093 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
1094 SectionEntry &Section = Sections[SectionID];
1095 uint8_t *Target = Section.Address + Offset;
1096 uint32_t *TargetAddress = (uint32_t *)Target;
1098 // Extract the addend from the instruction.
1099 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
1101 Value.Addend += Addend;
1103 // Look up for existing stub.
1104 StubMap::const_iterator i = Stubs.find(Value);
1105 if (i != Stubs.end()) {
1106 RelocationEntry RE(SectionID, Offset, RelType, i->second);
1107 addRelocationForSection(RE, SectionID);
1108 DEBUG(dbgs() << " Stub function found\n");
1110 // Create a new stub function.
1111 DEBUG(dbgs() << " Create a new stub function\n");
1112 Stubs[Value] = Section.StubOffset;
1113 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
1114 Section.StubOffset);
1116 // Creating Hi and Lo relocations for the filled stub instructions.
1117 RelocationEntry REHi(SectionID,
1118 StubTargetAddr - Section.Address,
1119 ELF::R_MIPS_UNUSED1, Value.Addend);
1120 RelocationEntry RELo(SectionID,
1121 StubTargetAddr - Section.Address + 4,
1122 ELF::R_MIPS_UNUSED2, Value.Addend);
1124 if (Value.SymbolName) {
1125 addRelocationForSymbol(REHi, Value.SymbolName);
1126 addRelocationForSymbol(RELo, Value.SymbolName);
1128 addRelocationForSection(REHi, Value.SectionID);
1129 addRelocationForSection(RELo, Value.SectionID);
1132 RelocationEntry RE(SectionID, Offset, RelType, Section.StubOffset);
1133 addRelocationForSection(RE, SectionID);
1134 Section.StubOffset += getMaxStubSize();
1136 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
1137 if (RelType == ELF::R_PPC64_REL24) {
1138 // A PPC branch relocation will need a stub function if the target is
1139 // an external symbol (Symbol::ST_Unknown) or if the target address
1140 // is not within the signed 24-bits branch address.
1141 SectionEntry &Section = Sections[SectionID];
1142 uint8_t *Target = Section.Address + Offset;
1143 bool RangeOverflow = false;
1144 if (SymType != SymbolRef::ST_Unknown) {
1145 // A function call may points to the .opd entry, so the final symbol value
1146 // in calculated based in the relocation values in .opd section.
1147 findOPDEntrySection(Obj, ObjSectionToID, Value);
1148 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
1149 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
1150 // If it is within 24-bits branch range, just set the branch target
1151 if (SignExtend32<24>(delta) == delta) {
1152 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
1153 if (Value.SymbolName)
1154 addRelocationForSymbol(RE, Value.SymbolName);
1156 addRelocationForSection(RE, Value.SectionID);
1158 RangeOverflow = true;
1161 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
1162 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
1163 // larger than 24-bits.
1164 StubMap::const_iterator i = Stubs.find(Value);
1165 if (i != Stubs.end()) {
1166 // Symbol function stub already created, just relocate to it
1167 resolveRelocation(Section, Offset,
1168 (uint64_t)Section.Address + i->second, RelType, 0);
1169 DEBUG(dbgs() << " Stub function found\n");
1171 // Create a new stub function.
1172 DEBUG(dbgs() << " Create a new stub function\n");
1173 Stubs[Value] = Section.StubOffset;
1174 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
1175 Section.StubOffset);
1176 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
1177 ELF::R_PPC64_ADDR64, Value.Addend);
1179 // Generates the 64-bits address loads as exemplified in section
1180 // 4.5.1 in PPC64 ELF ABI.
1181 RelocationEntry REhst(SectionID,
1182 StubTargetAddr - Section.Address + 2,
1183 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
1184 RelocationEntry REhr(SectionID,
1185 StubTargetAddr - Section.Address + 6,
1186 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
1187 RelocationEntry REh(SectionID,
1188 StubTargetAddr - Section.Address + 14,
1189 ELF::R_PPC64_ADDR16_HI, Value.Addend);
1190 RelocationEntry REl(SectionID,
1191 StubTargetAddr - Section.Address + 18,
1192 ELF::R_PPC64_ADDR16_LO, Value.Addend);
1194 if (Value.SymbolName) {
1195 addRelocationForSymbol(REhst, Value.SymbolName);
1196 addRelocationForSymbol(REhr, Value.SymbolName);
1197 addRelocationForSymbol(REh, Value.SymbolName);
1198 addRelocationForSymbol(REl, Value.SymbolName);
1200 addRelocationForSection(REhst, Value.SectionID);
1201 addRelocationForSection(REhr, Value.SectionID);
1202 addRelocationForSection(REh, Value.SectionID);
1203 addRelocationForSection(REl, Value.SectionID);
1206 resolveRelocation(Section, Offset,
1207 (uint64_t)Section.Address + Section.StubOffset,
1209 if (SymType == SymbolRef::ST_Unknown)
1210 // Restore the TOC for external calls
1211 writeInt32BE(Target+4, 0xE8410028); // ld r2,40(r1)
1212 Section.StubOffset += getMaxStubSize();
1216 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
1217 // Extra check to avoid relocation againt empty symbols (usually
1218 // the R_PPC64_TOC).
1219 if (SymType != SymbolRef::ST_Unknown && TargetName.empty())
1220 Value.SymbolName = NULL;
1222 if (Value.SymbolName)
1223 addRelocationForSymbol(RE, Value.SymbolName);
1225 addRelocationForSection(RE, Value.SectionID);
1227 } else if (Arch == Triple::systemz &&
1228 (RelType == ELF::R_390_PLT32DBL ||
1229 RelType == ELF::R_390_GOTENT)) {
1230 // Create function stubs for both PLT and GOT references, regardless of
1231 // whether the GOT reference is to data or code. The stub contains the
1232 // full address of the symbol, as needed by GOT references, and the
1233 // executable part only adds an overhead of 8 bytes.
1235 // We could try to conserve space by allocating the code and data
1236 // parts of the stub separately. However, as things stand, we allocate
1237 // a stub for every relocation, so using a GOT in JIT code should be
1238 // no less space efficient than using an explicit constant pool.
1239 DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
1240 SectionEntry &Section = Sections[SectionID];
1242 // Look for an existing stub.
1243 StubMap::const_iterator i = Stubs.find(Value);
1244 uintptr_t StubAddress;
1245 if (i != Stubs.end()) {
1246 StubAddress = uintptr_t(Section.Address) + i->second;
1247 DEBUG(dbgs() << " Stub function found\n");
1249 // Create a new stub function.
1250 DEBUG(dbgs() << " Create a new stub function\n");
1252 uintptr_t BaseAddress = uintptr_t(Section.Address);
1253 uintptr_t StubAlignment = getStubAlignment();
1254 StubAddress = (BaseAddress + Section.StubOffset +
1255 StubAlignment - 1) & -StubAlignment;
1256 unsigned StubOffset = StubAddress - BaseAddress;
1258 Stubs[Value] = StubOffset;
1259 createStubFunction((uint8_t *)StubAddress);
1260 RelocationEntry RE(SectionID, StubOffset + 8,
1261 ELF::R_390_64, Value.Addend - Addend);
1262 if (Value.SymbolName)
1263 addRelocationForSymbol(RE, Value.SymbolName);
1265 addRelocationForSection(RE, Value.SectionID);
1266 Section.StubOffset = StubOffset + getMaxStubSize();
1269 if (RelType == ELF::R_390_GOTENT)
1270 resolveRelocation(Section, Offset, StubAddress + 8,
1271 ELF::R_390_PC32DBL, Addend);
1273 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
1274 } else if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_PLT32) {
1275 // The way the PLT relocations normally work is that the linker allocates the
1276 // PLT and this relocation makes a PC-relative call into the PLT. The PLT
1277 // entry will then jump to an address provided by the GOT. On first call, the
1278 // GOT address will point back into PLT code that resolves the symbol. After
1279 // the first call, the GOT entry points to the actual function.
1281 // For local functions we're ignoring all of that here and just replacing
1282 // the PLT32 relocation type with PC32, which will translate the relocation
1283 // into a PC-relative call directly to the function. For external symbols we
1284 // can't be sure the function will be within 2^32 bytes of the call site, so
1285 // we need to create a stub, which calls into the GOT. This case is
1286 // equivalent to the usual PLT implementation except that we use the stub
1287 // mechanism in RuntimeDyld (which puts stubs at the end of the section)
1288 // rather than allocating a PLT section.
1289 if (Value.SymbolName) {
1290 // This is a call to an external function.
1291 // Look for an existing stub.
1292 SectionEntry &Section = Sections[SectionID];
1293 StubMap::const_iterator i = Stubs.find(Value);
1294 uintptr_t StubAddress;
1295 if (i != Stubs.end()) {
1296 StubAddress = uintptr_t(Section.Address) + i->second;
1297 DEBUG(dbgs() << " Stub function found\n");
1299 // Create a new stub function (equivalent to a PLT entry).
1300 DEBUG(dbgs() << " Create a new stub function\n");
1302 uintptr_t BaseAddress = uintptr_t(Section.Address);
1303 uintptr_t StubAlignment = getStubAlignment();
1304 StubAddress = (BaseAddress + Section.StubOffset +
1305 StubAlignment - 1) & -StubAlignment;
1306 unsigned StubOffset = StubAddress - BaseAddress;
1307 Stubs[Value] = StubOffset;
1308 createStubFunction((uint8_t *)StubAddress);
1310 // Create a GOT entry for the external function.
1311 GOTEntries.push_back(Value);
1313 // Make our stub function a relative call to the GOT entry.
1314 RelocationEntry RE(SectionID, StubOffset + 2,
1315 ELF::R_X86_64_GOTPCREL, -4);
1316 addRelocationForSymbol(RE, Value.SymbolName);
1318 // Bump our stub offset counter
1319 Section.StubOffset = StubOffset + getMaxStubSize();
1322 // Make the target call a call into the stub table.
1323 resolveRelocation(Section, Offset, StubAddress,
1324 ELF::R_X86_64_PC32, Addend);
1326 RelocationEntry RE(SectionID, Offset, ELF::R_X86_64_PC32, Value.Addend,
1328 addRelocationForSection(RE, Value.SectionID);
1331 if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_GOTPCREL) {
1332 GOTEntries.push_back(Value);
1334 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset);
1335 if (Value.SymbolName)
1336 addRelocationForSymbol(RE, Value.SymbolName);
1338 addRelocationForSection(RE, Value.SectionID);
1342 void RuntimeDyldELF::updateGOTEntries(StringRef Name, uint64_t Addr) {
1344 SmallVectorImpl<std::pair<SID, GOTRelocations> >::iterator it;
1345 SmallVectorImpl<std::pair<SID, GOTRelocations> >::iterator end = GOTs.end();
1347 for (it = GOTs.begin(); it != end; ++it) {
1348 GOTRelocations &GOTEntries = it->second;
1349 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
1350 if (GOTEntries[i].SymbolName != 0 && GOTEntries[i].SymbolName == Name) {
1351 GOTEntries[i].Offset = Addr;
1357 size_t RuntimeDyldELF::getGOTEntrySize() {
1358 // We don't use the GOT in all of these cases, but it's essentially free
1359 // to put them all here.
1362 case Triple::x86_64:
1363 case Triple::aarch64:
1365 case Triple::ppc64le:
1366 case Triple::systemz:
1367 Result = sizeof(uint64_t);
1373 case Triple::mipsel:
1374 Result = sizeof(uint32_t);
1376 default: llvm_unreachable("Unsupported CPU type!");
1381 uint64_t RuntimeDyldELF::findGOTEntry(uint64_t LoadAddress,
1384 const size_t GOTEntrySize = getGOTEntrySize();
1386 SmallVectorImpl<std::pair<SID, GOTRelocations> >::const_iterator it;
1387 SmallVectorImpl<std::pair<SID, GOTRelocations> >::const_iterator end = GOTs.end();
1390 for (it = GOTs.begin(); it != end; ++it) {
1391 SID GOTSectionID = it->first;
1392 const GOTRelocations &GOTEntries = it->second;
1394 // Find the matching entry in our vector.
1395 uint64_t SymbolOffset = 0;
1396 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
1397 if (GOTEntries[i].SymbolName == 0) {
1398 if (getSectionLoadAddress(GOTEntries[i].SectionID) == LoadAddress &&
1399 GOTEntries[i].Offset == Offset) {
1401 SymbolOffset = GOTEntries[i].Offset;
1405 // GOT entries for external symbols use the addend as the address when
1406 // the external symbol has been resolved.
1407 if (GOTEntries[i].Offset == LoadAddress) {
1409 // Don't use the Addend here. The relocation handler will use it.
1415 if (GOTIndex != -1) {
1416 if (GOTEntrySize == sizeof(uint64_t)) {
1417 uint64_t *LocalGOTAddr = (uint64_t*)getSectionAddress(GOTSectionID);
1418 // Fill in this entry with the address of the symbol being referenced.
1419 LocalGOTAddr[GOTIndex] = LoadAddress + SymbolOffset;
1421 uint32_t *LocalGOTAddr = (uint32_t*)getSectionAddress(GOTSectionID);
1422 // Fill in this entry with the address of the symbol being referenced.
1423 LocalGOTAddr[GOTIndex] = (uint32_t)(LoadAddress + SymbolOffset);
1426 // Calculate the load address of this entry
1427 return getSectionLoadAddress(GOTSectionID) + (GOTIndex * GOTEntrySize);
1431 assert(GOTIndex != -1 && "Unable to find requested GOT entry.");
1435 void RuntimeDyldELF::finalizeLoad(ObjSectionToIDMap &SectionMap) {
1436 // If necessary, allocate the global offset table
1438 // Allocate the GOT if necessary
1439 size_t numGOTEntries = GOTEntries.size();
1440 if (numGOTEntries != 0) {
1441 // Allocate memory for the section
1442 unsigned SectionID = Sections.size();
1443 size_t TotalSize = numGOTEntries * getGOTEntrySize();
1444 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, getGOTEntrySize(),
1445 SectionID, ".got", false);
1447 report_fatal_error("Unable to allocate memory for GOT!");
1449 GOTs.push_back(std::make_pair(SectionID, GOTEntries));
1450 Sections.push_back(SectionEntry(".got", Addr, TotalSize, 0));
1451 // For now, initialize all GOT entries to zero. We'll fill them in as
1452 // needed when GOT-based relocations are applied.
1453 memset(Addr, 0, TotalSize);
1457 report_fatal_error("Unable to allocate memory for GOT!");
1460 // Look for and record the EH frame section.
1461 ObjSectionToIDMap::iterator i, e;
1462 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
1463 const SectionRef &Section = i->first;
1465 Section.getName(Name);
1466 if (Name == ".eh_frame") {
1467 UnregisteredEHFrameSections.push_back(i->second);
1473 bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
1474 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
1476 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
1479 bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile *Obj) const {
1480 return Obj->isELF();