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/OwningPtr.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/IntervalMap.h"
22 #include "llvm/Object/ObjectFile.h"
23 #include "llvm/ExecutionEngine/ObjectImage.h"
24 #include "llvm/ExecutionEngine/ObjectBuffer.h"
25 #include "llvm/Support/ELF.h"
26 #include "llvm/ADT/Triple.h"
27 #include "llvm/Object/ELF.h"
29 using namespace llvm::object;
34 error_code check(error_code Err) {
36 report_fatal_error(Err.message());
41 template<support::endianness target_endianness, bool is64Bits>
42 class DyldELFObject : public ELFObjectFile<target_endianness, is64Bits> {
43 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
45 typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
46 typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
47 typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel;
48 typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela;
50 typedef Elf_Ehdr_Impl<target_endianness, is64Bits> Elf_Ehdr;
52 typedef typename ELFDataTypeTypedefHelper<
53 target_endianness, is64Bits>::value_type addr_type;
56 DyldELFObject(MemoryBuffer *Wrapper, error_code &ec);
58 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
59 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr);
61 // Methods for type inquiry through isa, cast and dyn_cast
62 static inline bool classof(const Binary *v) {
63 return (isa<ELFObjectFile<target_endianness, is64Bits> >(v)
64 && classof(cast<ELFObjectFile<target_endianness, is64Bits> >(v)));
66 static inline bool classof(
67 const ELFObjectFile<target_endianness, is64Bits> *v) {
68 return v->isDyldType();
72 template<support::endianness target_endianness, bool is64Bits>
73 class ELFObjectImage : public ObjectImageCommon {
75 DyldELFObject<target_endianness, is64Bits> *DyldObj;
79 ELFObjectImage(ObjectBuffer *Input,
80 DyldELFObject<target_endianness, is64Bits> *Obj)
81 : ObjectImageCommon(Input, Obj),
85 virtual ~ELFObjectImage() {
87 deregisterWithDebugger();
90 // Subclasses can override these methods to update the image with loaded
91 // addresses for sections and common symbols
92 virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr)
94 DyldObj->updateSectionAddress(Sec, Addr);
97 virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr)
99 DyldObj->updateSymbolAddress(Sym, Addr);
102 virtual void registerWithDebugger()
104 JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
107 virtual void deregisterWithDebugger()
109 JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer);
113 // The MemoryBuffer passed into this constructor is just a wrapper around the
114 // actual memory. Ultimately, the Binary parent class will take ownership of
115 // this MemoryBuffer object but not the underlying memory.
116 template<support::endianness target_endianness, bool is64Bits>
117 DyldELFObject<target_endianness, is64Bits>::DyldELFObject(MemoryBuffer *Wrapper,
119 : ELFObjectFile<target_endianness, is64Bits>(Wrapper, ec) {
120 this->isDyldELFObject = true;
123 template<support::endianness target_endianness, bool is64Bits>
124 void DyldELFObject<target_endianness, is64Bits>::updateSectionAddress(
125 const SectionRef &Sec,
127 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
128 Elf_Shdr *shdr = const_cast<Elf_Shdr*>(
129 reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
131 // This assumes the address passed in matches the target address bitness
132 // The template-based type cast handles everything else.
133 shdr->sh_addr = static_cast<addr_type>(Addr);
136 template<support::endianness target_endianness, bool is64Bits>
137 void DyldELFObject<target_endianness, is64Bits>::updateSymbolAddress(
138 const SymbolRef &SymRef,
141 Elf_Sym *sym = const_cast<Elf_Sym*>(
142 ELFObjectFile<target_endianness, is64Bits>::
143 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);
155 ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) {
156 if (Buffer->getBufferSize() < ELF::EI_NIDENT)
157 llvm_unreachable("Unexpected ELF object size");
158 std::pair<unsigned char, unsigned char> Ident = std::make_pair(
159 (uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS],
160 (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]);
163 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
164 DyldELFObject<support::little, false> *Obj =
165 new DyldELFObject<support::little, false>(Buffer->getMemBuffer(), ec);
166 return new ELFObjectImage<support::little, false>(Buffer, Obj);
168 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
169 DyldELFObject<support::big, false> *Obj =
170 new DyldELFObject<support::big, false>(Buffer->getMemBuffer(), ec);
171 return new ELFObjectImage<support::big, false>(Buffer, Obj);
173 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
174 DyldELFObject<support::big, true> *Obj =
175 new DyldELFObject<support::big, true>(Buffer->getMemBuffer(), ec);
176 return new ELFObjectImage<support::big, true>(Buffer, Obj);
178 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
179 DyldELFObject<support::little, true> *Obj =
180 new DyldELFObject<support::little, true>(Buffer->getMemBuffer(), ec);
181 return new ELFObjectImage<support::little, true>(Buffer, Obj);
184 llvm_unreachable("Unexpected ELF format");
187 RuntimeDyldELF::~RuntimeDyldELF() {
190 void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
197 llvm_unreachable("Relocation type not implemented yet!");
199 case ELF::R_X86_64_64: {
200 uint64_t *Target = reinterpret_cast<uint64_t*>(Section.Address + Offset);
201 *Target = Value + Addend;
202 DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend))
203 << " at " << format("%p\n",Target));
206 case ELF::R_X86_64_32:
207 case ELF::R_X86_64_32S: {
209 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
210 (Type == ELF::R_X86_64_32S &&
211 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
212 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
213 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
214 *Target = TruncatedAddr;
215 DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr)
216 << " at " << format("%p\n",Target));
219 case ELF::R_X86_64_PC32: {
220 // Get the placeholder value from the generated object since
221 // a previous relocation attempt may have overwritten the loaded version
222 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
224 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
225 uint64_t FinalAddress = Section.LoadAddress + Offset;
226 int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
227 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
228 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
229 *Target = TruncOffset;
235 void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
241 case ELF::R_386_32: {
242 // Get the placeholder value from the generated object since
243 // a previous relocation attempt may have overwritten the loaded version
244 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
246 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
247 *Target = *Placeholder + Value + Addend;
250 case ELF::R_386_PC32: {
251 // Get the placeholder value from the generated object since
252 // a previous relocation attempt may have overwritten the loaded version
253 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
255 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
256 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
257 uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
258 *Target = RealOffset;
262 // There are other relocation types, but it appears these are the
263 // only ones currently used by the LLVM ELF object writer
264 llvm_unreachable("Relocation type not implemented yet!");
269 void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
274 // TODO: Add Thumb relocations.
275 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
276 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
279 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
280 << Section.Address + Offset
281 << " FinalAddress: " << format("%p",FinalAddress)
282 << " Value: " << format("%x",Value)
283 << " Type: " << format("%x",Type)
284 << " Addend: " << format("%x",Addend)
289 llvm_unreachable("Not implemented relocation type!");
291 // Write a 32bit value to relocation address, taking into account the
292 // implicit addend encoded in the target.
293 case ELF::R_ARM_TARGET1 :
294 case ELF::R_ARM_ABS32 :
298 // Write first 16 bit of 32 bit value to the mov instruction.
299 // Last 4 bit should be shifted.
300 case ELF::R_ARM_MOVW_ABS_NC :
301 // We are not expecting any other addend in the relocation address.
302 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
303 // non-contiguous fields.
304 assert((*TargetPtr & 0x000F0FFF) == 0);
305 Value = Value & 0xFFFF;
306 *TargetPtr |= Value & 0xFFF;
307 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
310 // Write last 16 bit of 32 bit value to the mov instruction.
311 // Last 4 bit should be shifted.
312 case ELF::R_ARM_MOVT_ABS :
313 // We are not expecting any other addend in the relocation address.
314 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
315 assert((*TargetPtr & 0x000F0FFF) == 0);
316 Value = (Value >> 16) & 0xFFFF;
317 *TargetPtr |= Value & 0xFFF;
318 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
321 // Write 24 bit relative value to the branch instruction.
322 case ELF::R_ARM_PC24 : // Fall through.
323 case ELF::R_ARM_CALL : // Fall through.
324 case ELF::R_ARM_JUMP24 :
325 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
326 RelValue = (RelValue & 0x03FFFFFC) >> 2;
327 *TargetPtr &= 0xFF000000;
328 *TargetPtr |= RelValue;
333 void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
338 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
341 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
342 << Section.Address + Offset
344 << format("%p",Section.LoadAddress + Offset)
345 << " Value: " << format("%x",Value)
346 << " Type: " << format("%x",Type)
347 << " Addend: " << format("%x",Addend)
352 llvm_unreachable("Not implemented relocation type!");
355 *TargetPtr = Value + (*TargetPtr);
358 *TargetPtr = ((*TargetPtr) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
360 case ELF::R_MIPS_HI16:
361 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
362 Value += ((*TargetPtr) & 0x0000ffff) << 16;
363 *TargetPtr = ((*TargetPtr) & 0xffff0000) |
364 (((Value + 0x8000) >> 16) & 0xffff);
366 case ELF::R_MIPS_LO16:
367 Value += ((*TargetPtr) & 0x0000ffff);
368 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
373 // Return the .TOC. section address to R_PPC64_TOC relocations.
374 uint64_t RuntimeDyldELF::findPPC64TOC() const {
375 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
376 // order. The TOC starts where the first of these sections starts.
377 SectionList::const_iterator it = Sections.begin();
378 SectionList::const_iterator ite = Sections.end();
379 for (; it != ite; ++it) {
380 if (it->Name == ".got" ||
381 it->Name == ".toc" ||
382 it->Name == ".tocbss" ||
387 // This may happen for
388 // * references to TOC base base (sym@toc, .odp relocation) without
390 // In this case just use the first section (which is usually
391 // the .odp) since the code won't reference the .toc base
393 it = Sections.begin();
396 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
397 // thus permitting a full 64 Kbytes segment.
398 return it->LoadAddress + 0x8000;
401 // Returns the sections and offset associated with the ODP entry referenced
403 void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
404 ObjSectionToIDMap &LocalSections,
405 RelocationValueRef &Rel) {
406 // Get the ELF symbol value (st_value) to compare with Relocation offset in
410 for (section_iterator si = Obj.begin_sections(),
411 se = Obj.end_sections(); si != se; si.increment(err)) {
412 StringRef SectionName;
413 check(si->getName(SectionName));
414 if (SectionName != ".opd")
417 for (relocation_iterator i = si->begin_relocations(),
418 e = si->end_relocations(); i != e;) {
421 // The R_PPC64_ADDR64 relocation indicates the first field
424 check(i->getType(TypeFunc));
425 if (TypeFunc != ELF::R_PPC64_ADDR64) {
430 SymbolRef TargetSymbol;
431 uint64_t TargetSymbolOffset;
432 int64_t TargetAdditionalInfo;
433 check(i->getSymbol(TargetSymbol));
434 check(i->getOffset(TargetSymbolOffset));
435 check(i->getAdditionalInfo(TargetAdditionalInfo));
437 i = i.increment(err);
442 // Just check if following relocation is a R_PPC64_TOC
444 check(i->getType(TypeTOC));
445 if (TypeTOC != ELF::R_PPC64_TOC)
448 // Finally compares the Symbol value and the target symbol offset
449 // to check if this .opd entry refers to the symbol the relocation
451 if (Rel.Addend != (intptr_t)TargetSymbolOffset)
454 section_iterator tsi(Obj.end_sections());
455 check(TargetSymbol.getSection(tsi));
456 Rel.SectionID = findOrEmitSection(Obj, (*tsi), true, LocalSections);
457 Rel.Addend = (intptr_t)TargetAdditionalInfo;
461 llvm_unreachable("Attempting to get address of ODP entry!");
464 // Relocation masks following the #lo(value), #hi(value), #higher(value),
465 // and #highest(value) macros defined in section 4.5.1. Relocation Types
466 // in PPC-elf64abi document.
469 uint16_t applyPPClo (uint64_t value)
471 return value & 0xffff;
475 uint16_t applyPPChi (uint64_t value)
477 return (value >> 16) & 0xffff;
481 uint16_t applyPPChigher (uint64_t value)
483 return (value >> 32) & 0xffff;
487 uint16_t applyPPChighest (uint64_t value)
489 return (value >> 48) & 0xffff;
492 void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
497 uint8_t* LocalAddress = Section.Address + Offset;
500 llvm_unreachable("Relocation type not implemented yet!");
502 case ELF::R_PPC64_ADDR16_LO :
503 writeInt16BE(LocalAddress, applyPPClo (Value + Addend));
505 case ELF::R_PPC64_ADDR16_HI :
506 writeInt16BE(LocalAddress, applyPPChi (Value + Addend));
508 case ELF::R_PPC64_ADDR16_HIGHER :
509 writeInt16BE(LocalAddress, applyPPChigher (Value + Addend));
511 case ELF::R_PPC64_ADDR16_HIGHEST :
512 writeInt16BE(LocalAddress, applyPPChighest (Value + Addend));
514 case ELF::R_PPC64_ADDR14 : {
515 assert(((Value + Addend) & 3) == 0);
516 // Preserve the AA/LK bits in the branch instruction
517 uint8_t aalk = *(LocalAddress+3);
518 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
520 case ELF::R_PPC64_REL24 : {
521 uint64_t FinalAddress = (Section.LoadAddress + Offset);
522 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
523 if (SignExtend32<24>(delta) != delta)
524 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
525 // Generates a 'bl <address>' instruction
526 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
528 case ELF::R_PPC64_ADDR64 :
529 writeInt64BE(LocalAddress, Value + Addend);
531 case ELF::R_PPC64_TOC :
532 writeInt64BE(LocalAddress, findPPC64TOC());
534 case ELF::R_PPC64_TOC16 : {
535 uint64_t TOCStart = findPPC64TOC();
536 Value = applyPPClo((Value + Addend) - TOCStart);
537 writeInt16BE(LocalAddress, applyPPClo(Value));
539 case ELF::R_PPC64_TOC16_DS : {
540 uint64_t TOCStart = findPPC64TOC();
541 Value = ((Value + Addend) - TOCStart);
542 writeInt16BE(LocalAddress, applyPPClo(Value));
548 void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
555 resolveX86_64Relocation(Section, Offset, Value, Type, Addend);
558 resolveX86Relocation(Section, Offset,
559 (uint32_t)(Value & 0xffffffffL), Type,
560 (uint32_t)(Addend & 0xffffffffL));
562 case Triple::arm: // Fall through.
564 resolveARMRelocation(Section, Offset,
565 (uint32_t)(Value & 0xffffffffL), Type,
566 (uint32_t)(Addend & 0xffffffffL));
568 case Triple::mips: // Fall through.
570 resolveMIPSRelocation(Section, Offset,
571 (uint32_t)(Value & 0xffffffffL), Type,
572 (uint32_t)(Addend & 0xffffffffL));
575 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
577 default: llvm_unreachable("Unsupported CPU type!");
581 void RuntimeDyldELF::processRelocationRef(const ObjRelocationInfo &Rel,
583 ObjSectionToIDMap &ObjSectionToID,
584 const SymbolTableMap &Symbols,
587 uint32_t RelType = (uint32_t)(Rel.Type & 0xffffffffL);
588 intptr_t Addend = (intptr_t)Rel.AdditionalInfo;
589 const SymbolRef &Symbol = Rel.Symbol;
591 // Obtain the symbol name which is referenced in the relocation
592 StringRef TargetName;
593 Symbol.getName(TargetName);
594 DEBUG(dbgs() << "\t\tRelType: " << RelType
595 << " Addend: " << Addend
596 << " TargetName: " << TargetName
598 RelocationValueRef Value;
599 // First search for the symbol in the local symbol table
600 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
601 SymbolRef::Type SymType;
602 Symbol.getType(SymType);
603 if (lsi != Symbols.end()) {
604 Value.SectionID = lsi->second.first;
605 Value.Addend = lsi->second.second;
607 // Search for the symbol in the global symbol table
608 SymbolTableMap::const_iterator gsi =
609 GlobalSymbolTable.find(TargetName.data());
610 if (gsi != GlobalSymbolTable.end()) {
611 Value.SectionID = gsi->second.first;
612 Value.Addend = gsi->second.second;
615 case SymbolRef::ST_Debug: {
616 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
617 // and can be changed by another developers. Maybe best way is add
618 // a new symbol type ST_Section to SymbolRef and use it.
619 section_iterator si(Obj.end_sections());
620 Symbol.getSection(si);
621 if (si == Obj.end_sections())
622 llvm_unreachable("Symbol section not found, bad object file format!");
623 DEBUG(dbgs() << "\t\tThis is section symbol\n");
624 // Default to 'true' in case isText fails (though it never does).
627 Value.SectionID = findOrEmitSection(Obj,
631 Value.Addend = Addend;
634 case SymbolRef::ST_Unknown: {
635 Value.SymbolName = TargetName.data();
636 Value.Addend = Addend;
640 llvm_unreachable("Unresolved symbol type!");
645 DEBUG(dbgs() << "\t\tRel.SectionID: " << Rel.SectionID
646 << " Rel.Offset: " << Rel.Offset
648 if (Arch == Triple::arm &&
649 (RelType == ELF::R_ARM_PC24 ||
650 RelType == ELF::R_ARM_CALL ||
651 RelType == ELF::R_ARM_JUMP24)) {
652 // This is an ARM branch relocation, need to use a stub function.
653 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
654 SectionEntry &Section = Sections[Rel.SectionID];
656 // Look for an existing stub.
657 StubMap::const_iterator i = Stubs.find(Value);
658 if (i != Stubs.end()) {
659 resolveRelocation(Section, Rel.Offset,
660 (uint64_t)Section.Address + i->second, RelType, 0);
661 DEBUG(dbgs() << " Stub function found\n");
663 // Create a new stub function.
664 DEBUG(dbgs() << " Create a new stub function\n");
665 Stubs[Value] = Section.StubOffset;
666 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
668 RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address,
669 ELF::R_ARM_ABS32, Value.Addend);
670 if (Value.SymbolName)
671 addRelocationForSymbol(RE, Value.SymbolName);
673 addRelocationForSection(RE, Value.SectionID);
675 resolveRelocation(Section, Rel.Offset,
676 (uint64_t)Section.Address + Section.StubOffset,
678 Section.StubOffset += getMaxStubSize();
680 } else if (Arch == Triple::mipsel && RelType == ELF::R_MIPS_26) {
681 // This is an Mips branch relocation, need to use a stub function.
682 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
683 SectionEntry &Section = Sections[Rel.SectionID];
684 uint8_t *Target = Section.Address + Rel.Offset;
685 uint32_t *TargetAddress = (uint32_t *)Target;
687 // Extract the addend from the instruction.
688 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
690 Value.Addend += Addend;
692 // Look up for existing stub.
693 StubMap::const_iterator i = Stubs.find(Value);
694 if (i != Stubs.end()) {
695 resolveRelocation(Section, Rel.Offset,
696 (uint64_t)Section.Address + i->second, RelType, 0);
697 DEBUG(dbgs() << " Stub function found\n");
699 // Create a new stub function.
700 DEBUG(dbgs() << " Create a new stub function\n");
701 Stubs[Value] = Section.StubOffset;
702 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
705 // Creating Hi and Lo relocations for the filled stub instructions.
706 RelocationEntry REHi(Rel.SectionID,
707 StubTargetAddr - Section.Address,
708 ELF::R_MIPS_HI16, Value.Addend);
709 RelocationEntry RELo(Rel.SectionID,
710 StubTargetAddr - Section.Address + 4,
711 ELF::R_MIPS_LO16, Value.Addend);
713 if (Value.SymbolName) {
714 addRelocationForSymbol(REHi, Value.SymbolName);
715 addRelocationForSymbol(RELo, Value.SymbolName);
717 addRelocationForSection(REHi, Value.SectionID);
718 addRelocationForSection(RELo, Value.SectionID);
721 resolveRelocation(Section, Rel.Offset,
722 (uint64_t)Section.Address + Section.StubOffset,
724 Section.StubOffset += getMaxStubSize();
726 } else if (Arch == Triple::ppc64) {
727 if (RelType == ELF::R_PPC64_REL24) {
728 // A PPC branch relocation will need a stub function if the target is
729 // an external symbol (Symbol::ST_Unknown) or if the target address
730 // is not within the signed 24-bits branch address.
731 SectionEntry &Section = Sections[Rel.SectionID];
732 uint8_t *Target = Section.Address + Rel.Offset;
733 bool RangeOverflow = false;
734 if (SymType != SymbolRef::ST_Unknown) {
735 // A function call may points to the .opd entry, so the final symbol value
736 // in calculated based in the relocation values in .opd section.
737 findOPDEntrySection(Obj, ObjSectionToID, Value);
738 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
739 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
740 // If it is within 24-bits branch range, just set the branch target
741 if (SignExtend32<24>(delta) == delta) {
742 RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
743 if (Value.SymbolName)
744 addRelocationForSymbol(RE, Value.SymbolName);
746 addRelocationForSection(RE, Value.SectionID);
748 RangeOverflow = true;
751 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
752 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
753 // larger than 24-bits.
754 StubMap::const_iterator i = Stubs.find(Value);
755 if (i != Stubs.end()) {
756 // Symbol function stub already created, just relocate to it
757 resolveRelocation(Section, Rel.Offset,
758 (uint64_t)Section.Address + i->second, RelType, 0);
759 DEBUG(dbgs() << " Stub function found\n");
761 // Create a new stub function.
762 DEBUG(dbgs() << " Create a new stub function\n");
763 Stubs[Value] = Section.StubOffset;
764 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
766 RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address,
767 ELF::R_PPC64_ADDR64, Value.Addend);
769 // Generates the 64-bits address loads as exemplified in section
770 // 4.5.1 in PPC64 ELF ABI.
771 RelocationEntry REhst(Rel.SectionID,
772 StubTargetAddr - Section.Address + 2,
773 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
774 RelocationEntry REhr(Rel.SectionID,
775 StubTargetAddr - Section.Address + 6,
776 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
777 RelocationEntry REh(Rel.SectionID,
778 StubTargetAddr - Section.Address + 14,
779 ELF::R_PPC64_ADDR16_HI, Value.Addend);
780 RelocationEntry REl(Rel.SectionID,
781 StubTargetAddr - Section.Address + 18,
782 ELF::R_PPC64_ADDR16_LO, Value.Addend);
784 if (Value.SymbolName) {
785 addRelocationForSymbol(REhst, Value.SymbolName);
786 addRelocationForSymbol(REhr, Value.SymbolName);
787 addRelocationForSymbol(REh, Value.SymbolName);
788 addRelocationForSymbol(REl, Value.SymbolName);
790 addRelocationForSection(REhst, Value.SectionID);
791 addRelocationForSection(REhr, Value.SectionID);
792 addRelocationForSection(REh, Value.SectionID);
793 addRelocationForSection(REl, Value.SectionID);
796 resolveRelocation(Section, Rel.Offset,
797 (uint64_t)Section.Address + Section.StubOffset,
799 if (SymType == SymbolRef::ST_Unknown)
800 // Restore the TOC for external calls
801 writeInt32BE(Target+4, 0xE8410028); // ld r2,40(r1)
802 Section.StubOffset += getMaxStubSize();
806 RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
807 // Extra check to avoid relocation againt empty symbols (usually
809 if (Value.SymbolName && !TargetName.empty())
810 addRelocationForSymbol(RE, Value.SymbolName);
812 addRelocationForSection(RE, Value.SectionID);
815 RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
816 if (Value.SymbolName)
817 addRelocationForSymbol(RE, Value.SymbolName);
819 addRelocationForSection(RE, Value.SectionID);
823 unsigned RuntimeDyldELF::getCommonSymbolAlignment(const SymbolRef &Sym) {
824 // In ELF, the value of an SHN_COMMON symbol is its alignment requirement.
826 Check(Sym.getValue(Align));
830 bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
831 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
833 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;