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/OwningPtr.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Triple.h"
23 #include "llvm/ExecutionEngine/ObjectBuffer.h"
24 #include "llvm/ExecutionEngine/ObjectImage.h"
25 #include "llvm/Object/ELF.h"
26 #include "llvm/Object/ObjectFile.h"
27 #include "llvm/Support/ELF.h"
29 using namespace llvm::object;
34 error_code check(error_code Err) {
36 report_fatal_error(Err.message());
43 : public ELFObjectFile<ELFT> {
44 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
46 typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
47 typedef Elf_Sym_Impl<ELFT> Elf_Sym;
49 Elf_Rel_Impl<ELFT, false> Elf_Rel;
51 Elf_Rel_Impl<ELFT, true> Elf_Rela;
53 typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
55 typedef typename ELFDataTypeTypedefHelper<
56 ELFT>::value_type addr_type;
59 DyldELFObject(MemoryBuffer *Wrapper, error_code &ec);
61 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
62 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr);
64 // Methods for type inquiry through isa, cast and dyn_cast
65 static inline bool classof(const Binary *v) {
66 return (isa<ELFObjectFile<ELFT> >(v)
67 && classof(cast<ELFObjectFile
70 static inline bool classof(
71 const ELFObjectFile<ELFT> *v) {
72 return v->isDyldType();
77 class ELFObjectImage : public ObjectImageCommon {
79 DyldELFObject<ELFT> *DyldObj;
83 ELFObjectImage(ObjectBuffer *Input,
84 DyldELFObject<ELFT> *Obj)
85 : ObjectImageCommon(Input, Obj),
89 virtual ~ELFObjectImage() {
91 deregisterWithDebugger();
94 // Subclasses can override these methods to update the image with loaded
95 // addresses for sections and common symbols
96 virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr)
98 DyldObj->updateSectionAddress(Sec, Addr);
101 virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr)
103 DyldObj->updateSymbolAddress(Sym, Addr);
106 virtual void registerWithDebugger()
108 JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
111 virtual void deregisterWithDebugger()
113 JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer);
117 // The MemoryBuffer passed into this constructor is just a wrapper around the
118 // actual memory. Ultimately, the Binary parent class will take ownership of
119 // this MemoryBuffer object but not the underlying memory.
121 DyldELFObject<ELFT>::DyldELFObject(MemoryBuffer *Wrapper, error_code &ec)
122 : ELFObjectFile<ELFT>(Wrapper, ec) {
123 this->isDyldELFObject = true;
127 void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec,
129 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
130 Elf_Shdr *shdr = const_cast<Elf_Shdr*>(
131 reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
133 // This assumes the address passed in matches the target address bitness
134 // The template-based type cast handles everything else.
135 shdr->sh_addr = static_cast<addr_type>(Addr);
139 void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef,
142 Elf_Sym *sym = const_cast<Elf_Sym*>(
143 ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl()));
145 // This assumes the address passed in matches the target address bitness
146 // The template-based type cast handles everything else.
147 sym->st_value = static_cast<addr_type>(Addr);
154 ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) {
155 if (Buffer->getBufferSize() < ELF::EI_NIDENT)
156 llvm_unreachable("Unexpected ELF object size");
157 std::pair<unsigned char, unsigned char> Ident = std::make_pair(
158 (uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS],
159 (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]);
162 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
163 DyldELFObject<ELFType<support::little, 4, false> > *Obj =
164 new DyldELFObject<ELFType<support::little, 4, false> >(
165 Buffer->getMemBuffer(), ec);
166 return new ELFObjectImage<ELFType<support::little, 4, false> >(Buffer, Obj);
168 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
169 DyldELFObject<ELFType<support::big, 4, false> > *Obj =
170 new DyldELFObject<ELFType<support::big, 4, false> >(
171 Buffer->getMemBuffer(), ec);
172 return new ELFObjectImage<ELFType<support::big, 4, false> >(Buffer, Obj);
174 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
175 DyldELFObject<ELFType<support::big, 8, true> > *Obj =
176 new DyldELFObject<ELFType<support::big, 8, true> >(
177 Buffer->getMemBuffer(), ec);
178 return new ELFObjectImage<ELFType<support::big, 8, true> >(Buffer, Obj);
180 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
181 DyldELFObject<ELFType<support::little, 8, true> > *Obj =
182 new DyldELFObject<ELFType<support::little, 8, true> >(
183 Buffer->getMemBuffer(), ec);
184 return new ELFObjectImage<ELFType<support::little, 8, true> >(Buffer, Obj);
187 llvm_unreachable("Unexpected ELF format");
190 RuntimeDyldELF::~RuntimeDyldELF() {
193 void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
200 llvm_unreachable("Relocation type not implemented yet!");
202 case ELF::R_X86_64_64: {
203 uint64_t *Target = reinterpret_cast<uint64_t*>(Section.Address + Offset);
204 *Target = Value + Addend;
205 DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend))
206 << " at " << format("%p\n",Target));
209 case ELF::R_X86_64_32:
210 case ELF::R_X86_64_32S: {
212 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
213 (Type == ELF::R_X86_64_32S &&
214 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
215 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
216 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
217 *Target = TruncatedAddr;
218 DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr)
219 << " at " << format("%p\n",Target));
222 case ELF::R_X86_64_PC32: {
223 // Get the placeholder value from the generated object since
224 // a previous relocation attempt may have overwritten the loaded version
225 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
227 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
228 uint64_t FinalAddress = Section.LoadAddress + Offset;
229 int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
230 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
231 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
232 *Target = TruncOffset;
238 void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
244 case ELF::R_386_32: {
245 // Get the placeholder value from the generated object since
246 // a previous relocation attempt may have overwritten the loaded version
247 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
249 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
250 *Target = *Placeholder + Value + Addend;
253 case ELF::R_386_PC32: {
254 // Get the placeholder value from the generated object since
255 // a previous relocation attempt may have overwritten the loaded version
256 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
258 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
259 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
260 uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
261 *Target = RealOffset;
265 // There are other relocation types, but it appears these are the
266 // only ones currently used by the LLVM ELF object writer
267 llvm_unreachable("Relocation type not implemented yet!");
272 void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
277 uint32_t *TargetPtr = reinterpret_cast<uint32_t*>(Section.Address + Offset);
278 uint64_t FinalAddress = Section.LoadAddress + Offset;
280 DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
281 << format("%llx", Section.Address + Offset)
282 << " FinalAddress: 0x" << format("%llx",FinalAddress)
283 << " Value: 0x" << format("%llx",Value)
284 << " Type: 0x" << format("%x",Type)
285 << " Addend: 0x" << format("%llx",Addend)
290 llvm_unreachable("Relocation type not implemented yet!");
292 case ELF::R_AARCH64_PREL32: { // test-shift.ll (.eh_frame)
293 uint64_t Result = Value + Addend - FinalAddress;
294 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
295 static_cast<int64_t>(Result) <= UINT32_MAX);
296 *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU);
299 case ELF::R_AARCH64_MOVW_UABS_G3: {
300 uint64_t Result = Value + Addend;
301 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
302 *TargetPtr |= Result >> (48 - 5);
303 // Shift is "lsl #48", in bits 22:21
304 *TargetPtr |= 3 << 21;
307 case ELF::R_AARCH64_MOVW_UABS_G2_NC: {
308 uint64_t Result = Value + Addend;
309 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
310 *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
311 // Shift is "lsl #32", in bits 22:21
312 *TargetPtr |= 2 << 21;
315 case ELF::R_AARCH64_MOVW_UABS_G1_NC: {
316 uint64_t Result = Value + Addend;
317 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
318 *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
319 // Shift is "lsl #16", in bits 22:21
320 *TargetPtr |= 1 << 21;
323 case ELF::R_AARCH64_MOVW_UABS_G0_NC: {
324 uint64_t Result = Value + Addend;
325 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
326 *TargetPtr |= ((Result & 0xffffU) << 5);
327 // Shift is "lsl #0", in bits 22:21. No action needed.
333 void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
338 // TODO: Add Thumb relocations.
339 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
340 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
343 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
344 << Section.Address + Offset
345 << " FinalAddress: " << format("%p",FinalAddress)
346 << " Value: " << format("%x",Value)
347 << " Type: " << format("%x",Type)
348 << " Addend: " << format("%x",Addend)
353 llvm_unreachable("Not implemented relocation type!");
355 // Write a 32bit value to relocation address, taking into account the
356 // implicit addend encoded in the target.
357 case ELF::R_ARM_TARGET1 :
358 case ELF::R_ARM_ABS32 :
362 // Write first 16 bit of 32 bit value to the mov instruction.
363 // Last 4 bit should be shifted.
364 case ELF::R_ARM_MOVW_ABS_NC :
365 // We are not expecting any other addend in the relocation address.
366 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
367 // non-contiguous fields.
368 assert((*TargetPtr & 0x000F0FFF) == 0);
369 Value = Value & 0xFFFF;
370 *TargetPtr |= Value & 0xFFF;
371 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
374 // Write last 16 bit of 32 bit value to the mov instruction.
375 // Last 4 bit should be shifted.
376 case ELF::R_ARM_MOVT_ABS :
377 // We are not expecting any other addend in the relocation address.
378 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
379 assert((*TargetPtr & 0x000F0FFF) == 0);
380 Value = (Value >> 16) & 0xFFFF;
381 *TargetPtr |= Value & 0xFFF;
382 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
385 // Write 24 bit relative value to the branch instruction.
386 case ELF::R_ARM_PC24 : // Fall through.
387 case ELF::R_ARM_CALL : // Fall through.
388 case ELF::R_ARM_JUMP24 :
389 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
390 RelValue = (RelValue & 0x03FFFFFC) >> 2;
391 *TargetPtr &= 0xFF000000;
392 *TargetPtr |= RelValue;
397 void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
402 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
405 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
406 << Section.Address + Offset
408 << format("%p",Section.LoadAddress + Offset)
409 << " Value: " << format("%x",Value)
410 << " Type: " << format("%x",Type)
411 << " Addend: " << format("%x",Addend)
416 llvm_unreachable("Not implemented relocation type!");
419 *TargetPtr = Value + (*TargetPtr);
422 *TargetPtr = ((*TargetPtr) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
424 case ELF::R_MIPS_HI16:
425 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
426 Value += ((*TargetPtr) & 0x0000ffff) << 16;
427 *TargetPtr = ((*TargetPtr) & 0xffff0000) |
428 (((Value + 0x8000) >> 16) & 0xffff);
430 case ELF::R_MIPS_LO16:
431 Value += ((*TargetPtr) & 0x0000ffff);
432 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
437 // Return the .TOC. section address to R_PPC64_TOC relocations.
438 uint64_t RuntimeDyldELF::findPPC64TOC() const {
439 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
440 // order. The TOC starts where the first of these sections starts.
441 SectionList::const_iterator it = Sections.begin();
442 SectionList::const_iterator ite = Sections.end();
443 for (; it != ite; ++it) {
444 if (it->Name == ".got" ||
445 it->Name == ".toc" ||
446 it->Name == ".tocbss" ||
451 // This may happen for
452 // * references to TOC base base (sym@toc, .odp relocation) without
454 // In this case just use the first section (which is usually
455 // the .odp) since the code won't reference the .toc base
457 it = Sections.begin();
460 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
461 // thus permitting a full 64 Kbytes segment.
462 return it->LoadAddress + 0x8000;
465 // Returns the sections and offset associated with the ODP entry referenced
467 void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
468 ObjSectionToIDMap &LocalSections,
469 RelocationValueRef &Rel) {
470 // Get the ELF symbol value (st_value) to compare with Relocation offset in
474 for (section_iterator si = Obj.begin_sections(),
475 se = Obj.end_sections(); si != se; si.increment(err)) {
476 StringRef SectionName;
477 check(si->getName(SectionName));
478 if (SectionName != ".opd")
481 for (relocation_iterator i = si->begin_relocations(),
482 e = si->end_relocations(); i != e;) {
485 // The R_PPC64_ADDR64 relocation indicates the first field
488 check(i->getType(TypeFunc));
489 if (TypeFunc != ELF::R_PPC64_ADDR64) {
494 SymbolRef TargetSymbol;
495 uint64_t TargetSymbolOffset;
496 int64_t TargetAdditionalInfo;
497 check(i->getSymbol(TargetSymbol));
498 check(i->getOffset(TargetSymbolOffset));
499 check(i->getAdditionalInfo(TargetAdditionalInfo));
501 i = i.increment(err);
506 // Just check if following relocation is a R_PPC64_TOC
508 check(i->getType(TypeTOC));
509 if (TypeTOC != ELF::R_PPC64_TOC)
512 // Finally compares the Symbol value and the target symbol offset
513 // to check if this .opd entry refers to the symbol the relocation
515 if (Rel.Addend != (intptr_t)TargetSymbolOffset)
518 section_iterator tsi(Obj.end_sections());
519 check(TargetSymbol.getSection(tsi));
520 Rel.SectionID = findOrEmitSection(Obj, (*tsi), true, LocalSections);
521 Rel.Addend = (intptr_t)TargetAdditionalInfo;
525 llvm_unreachable("Attempting to get address of ODP entry!");
528 // Relocation masks following the #lo(value), #hi(value), #higher(value),
529 // and #highest(value) macros defined in section 4.5.1. Relocation Types
530 // in PPC-elf64abi document.
533 uint16_t applyPPClo (uint64_t value)
535 return value & 0xffff;
539 uint16_t applyPPChi (uint64_t value)
541 return (value >> 16) & 0xffff;
545 uint16_t applyPPChigher (uint64_t value)
547 return (value >> 32) & 0xffff;
551 uint16_t applyPPChighest (uint64_t value)
553 return (value >> 48) & 0xffff;
556 void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
561 uint8_t* LocalAddress = Section.Address + Offset;
564 llvm_unreachable("Relocation type not implemented yet!");
566 case ELF::R_PPC64_ADDR16_LO :
567 writeInt16BE(LocalAddress, applyPPClo (Value + Addend));
569 case ELF::R_PPC64_ADDR16_HI :
570 writeInt16BE(LocalAddress, applyPPChi (Value + Addend));
572 case ELF::R_PPC64_ADDR16_HIGHER :
573 writeInt16BE(LocalAddress, applyPPChigher (Value + Addend));
575 case ELF::R_PPC64_ADDR16_HIGHEST :
576 writeInt16BE(LocalAddress, applyPPChighest (Value + Addend));
578 case ELF::R_PPC64_ADDR14 : {
579 assert(((Value + Addend) & 3) == 0);
580 // Preserve the AA/LK bits in the branch instruction
581 uint8_t aalk = *(LocalAddress+3);
582 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
584 case ELF::R_PPC64_ADDR32 : {
585 int32_t Result = static_cast<int32_t>(Value + Addend);
586 if (SignExtend32<32>(Result) != Result)
587 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
588 writeInt32BE(LocalAddress, Result);
590 case ELF::R_PPC64_REL24 : {
591 uint64_t FinalAddress = (Section.LoadAddress + Offset);
592 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
593 if (SignExtend32<24>(delta) != delta)
594 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
595 // Generates a 'bl <address>' instruction
596 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
598 case ELF::R_PPC64_REL32 : {
599 uint64_t FinalAddress = (Section.LoadAddress + Offset);
600 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
601 if (SignExtend32<32>(delta) != delta)
602 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
603 writeInt32BE(LocalAddress, delta);
605 case ELF::R_PPC64_ADDR64 :
606 writeInt64BE(LocalAddress, Value + Addend);
608 case ELF::R_PPC64_TOC :
609 writeInt64BE(LocalAddress, findPPC64TOC());
611 case ELF::R_PPC64_TOC16 : {
612 uint64_t TOCStart = findPPC64TOC();
613 Value = applyPPClo((Value + Addend) - TOCStart);
614 writeInt16BE(LocalAddress, applyPPClo(Value));
616 case ELF::R_PPC64_TOC16_DS : {
617 uint64_t TOCStart = findPPC64TOC();
618 Value = ((Value + Addend) - TOCStart);
619 writeInt16BE(LocalAddress, applyPPClo(Value));
624 void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
629 uint8_t *LocalAddress = Section.Address + Offset;
632 llvm_unreachable("Relocation type not implemented yet!");
634 case ELF::R_390_PC16DBL:
635 case ELF::R_390_PLT16DBL: {
636 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
637 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
638 writeInt16BE(LocalAddress, Delta / 2);
641 case ELF::R_390_PC32DBL:
642 case ELF::R_390_PLT32DBL: {
643 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
644 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
645 writeInt32BE(LocalAddress, Delta / 2);
648 case ELF::R_390_PC32: {
649 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
650 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
651 writeInt32BE(LocalAddress, Delta);
655 writeInt64BE(LocalAddress, Value + Addend);
660 void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
662 const SectionEntry &Section = Sections[RE.SectionID];
663 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend);
666 void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
673 resolveX86_64Relocation(Section, Offset, Value, Type, Addend);
676 resolveX86Relocation(Section, Offset,
677 (uint32_t)(Value & 0xffffffffL), Type,
678 (uint32_t)(Addend & 0xffffffffL));
680 case Triple::aarch64:
681 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
683 case Triple::arm: // Fall through.
685 resolveARMRelocation(Section, Offset,
686 (uint32_t)(Value & 0xffffffffL), Type,
687 (uint32_t)(Addend & 0xffffffffL));
689 case Triple::mips: // Fall through.
691 resolveMIPSRelocation(Section, Offset,
692 (uint32_t)(Value & 0xffffffffL), Type,
693 (uint32_t)(Addend & 0xffffffffL));
696 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
698 case Triple::systemz:
699 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
701 default: llvm_unreachable("Unsupported CPU type!");
705 void RuntimeDyldELF::processRelocationRef(unsigned SectionID,
708 ObjSectionToIDMap &ObjSectionToID,
709 const SymbolTableMap &Symbols,
712 Check(RelI.getType(RelType));
714 Check(RelI.getAdditionalInfo(Addend));
716 Check(RelI.getSymbol(Symbol));
718 // Obtain the symbol name which is referenced in the relocation
719 StringRef TargetName;
720 Symbol.getName(TargetName);
721 DEBUG(dbgs() << "\t\tRelType: " << RelType
722 << " Addend: " << Addend
723 << " TargetName: " << TargetName
725 RelocationValueRef Value;
726 // First search for the symbol in the local symbol table
727 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
728 SymbolRef::Type SymType;
729 Symbol.getType(SymType);
730 if (lsi != Symbols.end()) {
731 Value.SectionID = lsi->second.first;
732 Value.Addend = lsi->second.second + Addend;
734 // Search for the symbol in the global symbol table
735 SymbolTableMap::const_iterator gsi =
736 GlobalSymbolTable.find(TargetName.data());
737 if (gsi != GlobalSymbolTable.end()) {
738 Value.SectionID = gsi->second.first;
739 Value.Addend = gsi->second.second + Addend;
742 case SymbolRef::ST_Debug: {
743 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
744 // and can be changed by another developers. Maybe best way is add
745 // a new symbol type ST_Section to SymbolRef and use it.
746 section_iterator si(Obj.end_sections());
747 Symbol.getSection(si);
748 if (si == Obj.end_sections())
749 llvm_unreachable("Symbol section not found, bad object file format!");
750 DEBUG(dbgs() << "\t\tThis is section symbol\n");
751 // Default to 'true' in case isText fails (though it never does).
754 Value.SectionID = findOrEmitSection(Obj,
758 Value.Addend = Addend;
761 case SymbolRef::ST_Unknown: {
762 Value.SymbolName = TargetName.data();
763 Value.Addend = Addend;
767 llvm_unreachable("Unresolved symbol type!");
773 Check(RelI.getOffset(Offset));
775 DEBUG(dbgs() << "\t\tSectionID: " << SectionID
776 << " Offset: " << Offset
778 if (Arch == Triple::arm &&
779 (RelType == ELF::R_ARM_PC24 ||
780 RelType == ELF::R_ARM_CALL ||
781 RelType == ELF::R_ARM_JUMP24)) {
782 // This is an ARM branch relocation, need to use a stub function.
783 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
784 SectionEntry &Section = Sections[SectionID];
786 // Look for an existing stub.
787 StubMap::const_iterator i = Stubs.find(Value);
788 if (i != Stubs.end()) {
789 resolveRelocation(Section, Offset,
790 (uint64_t)Section.Address + i->second, RelType, 0);
791 DEBUG(dbgs() << " Stub function found\n");
793 // Create a new stub function.
794 DEBUG(dbgs() << " Create a new stub function\n");
795 Stubs[Value] = Section.StubOffset;
796 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
798 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
799 ELF::R_ARM_ABS32, Value.Addend);
800 if (Value.SymbolName)
801 addRelocationForSymbol(RE, Value.SymbolName);
803 addRelocationForSection(RE, Value.SectionID);
805 resolveRelocation(Section, Offset,
806 (uint64_t)Section.Address + Section.StubOffset,
808 Section.StubOffset += getMaxStubSize();
810 } else if ((Arch == Triple::mipsel || Arch == Triple::mips) &&
811 RelType == ELF::R_MIPS_26) {
812 // This is an Mips branch relocation, need to use a stub function.
813 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
814 SectionEntry &Section = Sections[SectionID];
815 uint8_t *Target = Section.Address + Offset;
816 uint32_t *TargetAddress = (uint32_t *)Target;
818 // Extract the addend from the instruction.
819 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
821 Value.Addend += Addend;
823 // Look up for existing stub.
824 StubMap::const_iterator i = Stubs.find(Value);
825 if (i != Stubs.end()) {
826 resolveRelocation(Section, Offset,
827 (uint64_t)Section.Address + i->second, RelType, 0);
828 DEBUG(dbgs() << " Stub function found\n");
830 // Create a new stub function.
831 DEBUG(dbgs() << " Create a new stub function\n");
832 Stubs[Value] = Section.StubOffset;
833 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
836 // Creating Hi and Lo relocations for the filled stub instructions.
837 RelocationEntry REHi(SectionID,
838 StubTargetAddr - Section.Address,
839 ELF::R_MIPS_HI16, Value.Addend);
840 RelocationEntry RELo(SectionID,
841 StubTargetAddr - Section.Address + 4,
842 ELF::R_MIPS_LO16, Value.Addend);
844 if (Value.SymbolName) {
845 addRelocationForSymbol(REHi, Value.SymbolName);
846 addRelocationForSymbol(RELo, Value.SymbolName);
848 addRelocationForSection(REHi, Value.SectionID);
849 addRelocationForSection(RELo, Value.SectionID);
852 resolveRelocation(Section, Offset,
853 (uint64_t)Section.Address + Section.StubOffset,
855 Section.StubOffset += getMaxStubSize();
857 } else if (Arch == Triple::ppc64) {
858 if (RelType == ELF::R_PPC64_REL24) {
859 // A PPC branch relocation will need a stub function if the target is
860 // an external symbol (Symbol::ST_Unknown) or if the target address
861 // is not within the signed 24-bits branch address.
862 SectionEntry &Section = Sections[SectionID];
863 uint8_t *Target = Section.Address + Offset;
864 bool RangeOverflow = false;
865 if (SymType != SymbolRef::ST_Unknown) {
866 // A function call may points to the .opd entry, so the final symbol value
867 // in calculated based in the relocation values in .opd section.
868 findOPDEntrySection(Obj, ObjSectionToID, Value);
869 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
870 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
871 // If it is within 24-bits branch range, just set the branch target
872 if (SignExtend32<24>(delta) == delta) {
873 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
874 if (Value.SymbolName)
875 addRelocationForSymbol(RE, Value.SymbolName);
877 addRelocationForSection(RE, Value.SectionID);
879 RangeOverflow = true;
882 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
883 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
884 // larger than 24-bits.
885 StubMap::const_iterator i = Stubs.find(Value);
886 if (i != Stubs.end()) {
887 // Symbol function stub already created, just relocate to it
888 resolveRelocation(Section, Offset,
889 (uint64_t)Section.Address + i->second, RelType, 0);
890 DEBUG(dbgs() << " Stub function found\n");
892 // Create a new stub function.
893 DEBUG(dbgs() << " Create a new stub function\n");
894 Stubs[Value] = Section.StubOffset;
895 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
897 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
898 ELF::R_PPC64_ADDR64, Value.Addend);
900 // Generates the 64-bits address loads as exemplified in section
901 // 4.5.1 in PPC64 ELF ABI.
902 RelocationEntry REhst(SectionID,
903 StubTargetAddr - Section.Address + 2,
904 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
905 RelocationEntry REhr(SectionID,
906 StubTargetAddr - Section.Address + 6,
907 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
908 RelocationEntry REh(SectionID,
909 StubTargetAddr - Section.Address + 14,
910 ELF::R_PPC64_ADDR16_HI, Value.Addend);
911 RelocationEntry REl(SectionID,
912 StubTargetAddr - Section.Address + 18,
913 ELF::R_PPC64_ADDR16_LO, Value.Addend);
915 if (Value.SymbolName) {
916 addRelocationForSymbol(REhst, Value.SymbolName);
917 addRelocationForSymbol(REhr, Value.SymbolName);
918 addRelocationForSymbol(REh, Value.SymbolName);
919 addRelocationForSymbol(REl, Value.SymbolName);
921 addRelocationForSection(REhst, Value.SectionID);
922 addRelocationForSection(REhr, Value.SectionID);
923 addRelocationForSection(REh, Value.SectionID);
924 addRelocationForSection(REl, Value.SectionID);
927 resolveRelocation(Section, Offset,
928 (uint64_t)Section.Address + Section.StubOffset,
930 if (SymType == SymbolRef::ST_Unknown)
931 // Restore the TOC for external calls
932 writeInt32BE(Target+4, 0xE8410028); // ld r2,40(r1)
933 Section.StubOffset += getMaxStubSize();
937 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
938 // Extra check to avoid relocation againt empty symbols (usually
940 if (Value.SymbolName && !TargetName.empty())
941 addRelocationForSymbol(RE, Value.SymbolName);
943 addRelocationForSection(RE, Value.SectionID);
945 } else if (Arch == Triple::systemz &&
946 (RelType == ELF::R_390_PLT32DBL ||
947 RelType == ELF::R_390_GOTENT)) {
948 // Create function stubs for both PLT and GOT references, regardless of
949 // whether the GOT reference is to data or code. The stub contains the
950 // full address of the symbol, as needed by GOT references, and the
951 // executable part only adds an overhead of 8 bytes.
953 // We could try to conserve space by allocating the code and data
954 // parts of the stub separately. However, as things stand, we allocate
955 // a stub for every relocation, so using a GOT in JIT code should be
956 // no less space efficient than using an explicit constant pool.
957 DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
958 SectionEntry &Section = Sections[SectionID];
960 // Look for an existing stub.
961 StubMap::const_iterator i = Stubs.find(Value);
962 uintptr_t StubAddress;
963 if (i != Stubs.end()) {
964 StubAddress = uintptr_t(Section.Address) + i->second;
965 DEBUG(dbgs() << " Stub function found\n");
967 // Create a new stub function.
968 DEBUG(dbgs() << " Create a new stub function\n");
970 uintptr_t BaseAddress = uintptr_t(Section.Address);
971 uintptr_t StubAlignment = getStubAlignment();
972 StubAddress = (BaseAddress + Section.StubOffset +
973 StubAlignment - 1) & -StubAlignment;
974 unsigned StubOffset = StubAddress - BaseAddress;
976 Stubs[Value] = StubOffset;
977 createStubFunction((uint8_t *)StubAddress);
978 RelocationEntry RE(SectionID, StubOffset + 8,
979 ELF::R_390_64, Value.Addend - Addend);
980 if (Value.SymbolName)
981 addRelocationForSymbol(RE, Value.SymbolName);
983 addRelocationForSection(RE, Value.SectionID);
984 Section.StubOffset = StubOffset + getMaxStubSize();
987 if (RelType == ELF::R_390_GOTENT)
988 resolveRelocation(Section, Offset, StubAddress + 8,
989 ELF::R_390_PC32DBL, Addend);
991 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
993 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
994 if (Value.SymbolName)
995 addRelocationForSymbol(RE, Value.SymbolName);
997 addRelocationForSection(RE, Value.SectionID);
1001 bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
1002 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
1004 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;