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::resolveARMRelocation(const SectionEntry &Section,
277 // TODO: Add Thumb relocations.
278 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
279 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
282 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
283 << Section.Address + Offset
284 << " FinalAddress: " << format("%p",FinalAddress)
285 << " Value: " << format("%x",Value)
286 << " Type: " << format("%x",Type)
287 << " Addend: " << format("%x",Addend)
292 llvm_unreachable("Not implemented relocation type!");
294 // Write a 32bit value to relocation address, taking into account the
295 // implicit addend encoded in the target.
296 case ELF::R_ARM_TARGET1 :
297 case ELF::R_ARM_ABS32 :
301 // Write first 16 bit of 32 bit value to the mov instruction.
302 // Last 4 bit should be shifted.
303 case ELF::R_ARM_MOVW_ABS_NC :
304 // We are not expecting any other addend in the relocation address.
305 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
306 // non-contiguous fields.
307 assert((*TargetPtr & 0x000F0FFF) == 0);
308 Value = Value & 0xFFFF;
309 *TargetPtr |= Value & 0xFFF;
310 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
313 // Write last 16 bit of 32 bit value to the mov instruction.
314 // Last 4 bit should be shifted.
315 case ELF::R_ARM_MOVT_ABS :
316 // We are not expecting any other addend in the relocation address.
317 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
318 assert((*TargetPtr & 0x000F0FFF) == 0);
319 Value = (Value >> 16) & 0xFFFF;
320 *TargetPtr |= Value & 0xFFF;
321 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
324 // Write 24 bit relative value to the branch instruction.
325 case ELF::R_ARM_PC24 : // Fall through.
326 case ELF::R_ARM_CALL : // Fall through.
327 case ELF::R_ARM_JUMP24 :
328 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
329 RelValue = (RelValue & 0x03FFFFFC) >> 2;
330 *TargetPtr &= 0xFF000000;
331 *TargetPtr |= RelValue;
336 void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
341 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
344 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
345 << Section.Address + Offset
347 << format("%p",Section.LoadAddress + Offset)
348 << " Value: " << format("%x",Value)
349 << " Type: " << format("%x",Type)
350 << " Addend: " << format("%x",Addend)
355 llvm_unreachable("Not implemented relocation type!");
358 *TargetPtr = Value + (*TargetPtr);
361 *TargetPtr = ((*TargetPtr) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
363 case ELF::R_MIPS_HI16:
364 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
365 Value += ((*TargetPtr) & 0x0000ffff) << 16;
366 *TargetPtr = ((*TargetPtr) & 0xffff0000) |
367 (((Value + 0x8000) >> 16) & 0xffff);
369 case ELF::R_MIPS_LO16:
370 Value += ((*TargetPtr) & 0x0000ffff);
371 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
376 // Return the .TOC. section address to R_PPC64_TOC relocations.
377 uint64_t RuntimeDyldELF::findPPC64TOC() const {
378 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
379 // order. The TOC starts where the first of these sections starts.
380 SectionList::const_iterator it = Sections.begin();
381 SectionList::const_iterator ite = Sections.end();
382 for (; it != ite; ++it) {
383 if (it->Name == ".got" ||
384 it->Name == ".toc" ||
385 it->Name == ".tocbss" ||
390 // This may happen for
391 // * references to TOC base base (sym@toc, .odp relocation) without
393 // In this case just use the first section (which is usually
394 // the .odp) since the code won't reference the .toc base
396 it = Sections.begin();
399 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
400 // thus permitting a full 64 Kbytes segment.
401 return it->LoadAddress + 0x8000;
404 // Returns the sections and offset associated with the ODP entry referenced
406 void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
407 ObjSectionToIDMap &LocalSections,
408 RelocationValueRef &Rel) {
409 // Get the ELF symbol value (st_value) to compare with Relocation offset in
413 for (section_iterator si = Obj.begin_sections(),
414 se = Obj.end_sections(); si != se; si.increment(err)) {
415 StringRef SectionName;
416 check(si->getName(SectionName));
417 if (SectionName != ".opd")
420 for (relocation_iterator i = si->begin_relocations(),
421 e = si->end_relocations(); i != e;) {
424 // The R_PPC64_ADDR64 relocation indicates the first field
427 check(i->getType(TypeFunc));
428 if (TypeFunc != ELF::R_PPC64_ADDR64) {
433 SymbolRef TargetSymbol;
434 uint64_t TargetSymbolOffset;
435 int64_t TargetAdditionalInfo;
436 check(i->getSymbol(TargetSymbol));
437 check(i->getOffset(TargetSymbolOffset));
438 check(i->getAdditionalInfo(TargetAdditionalInfo));
440 i = i.increment(err);
445 // Just check if following relocation is a R_PPC64_TOC
447 check(i->getType(TypeTOC));
448 if (TypeTOC != ELF::R_PPC64_TOC)
451 // Finally compares the Symbol value and the target symbol offset
452 // to check if this .opd entry refers to the symbol the relocation
454 if (Rel.Addend != (intptr_t)TargetSymbolOffset)
457 section_iterator tsi(Obj.end_sections());
458 check(TargetSymbol.getSection(tsi));
459 Rel.SectionID = findOrEmitSection(Obj, (*tsi), true, LocalSections);
460 Rel.Addend = (intptr_t)TargetAdditionalInfo;
464 llvm_unreachable("Attempting to get address of ODP entry!");
467 // Relocation masks following the #lo(value), #hi(value), #higher(value),
468 // and #highest(value) macros defined in section 4.5.1. Relocation Types
469 // in PPC-elf64abi document.
472 uint16_t applyPPClo (uint64_t value)
474 return value & 0xffff;
478 uint16_t applyPPChi (uint64_t value)
480 return (value >> 16) & 0xffff;
484 uint16_t applyPPChigher (uint64_t value)
486 return (value >> 32) & 0xffff;
490 uint16_t applyPPChighest (uint64_t value)
492 return (value >> 48) & 0xffff;
495 void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
500 uint8_t* LocalAddress = Section.Address + Offset;
503 llvm_unreachable("Relocation type not implemented yet!");
505 case ELF::R_PPC64_ADDR16_LO :
506 writeInt16BE(LocalAddress, applyPPClo (Value + Addend));
508 case ELF::R_PPC64_ADDR16_HI :
509 writeInt16BE(LocalAddress, applyPPChi (Value + Addend));
511 case ELF::R_PPC64_ADDR16_HIGHER :
512 writeInt16BE(LocalAddress, applyPPChigher (Value + Addend));
514 case ELF::R_PPC64_ADDR16_HIGHEST :
515 writeInt16BE(LocalAddress, applyPPChighest (Value + Addend));
517 case ELF::R_PPC64_ADDR14 : {
518 assert(((Value + Addend) & 3) == 0);
519 // Preserve the AA/LK bits in the branch instruction
520 uint8_t aalk = *(LocalAddress+3);
521 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
523 case ELF::R_PPC64_ADDR32 : {
524 int32_t Result = static_cast<int32_t>(Value + Addend);
525 if (SignExtend32<32>(Result) != Result)
526 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
527 writeInt32BE(LocalAddress, Result);
529 case ELF::R_PPC64_REL24 : {
530 uint64_t FinalAddress = (Section.LoadAddress + Offset);
531 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
532 if (SignExtend32<24>(delta) != delta)
533 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
534 // Generates a 'bl <address>' instruction
535 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
537 case ELF::R_PPC64_REL32 : {
538 uint64_t FinalAddress = (Section.LoadAddress + Offset);
539 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
540 if (SignExtend32<32>(delta) != delta)
541 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
542 writeInt32BE(LocalAddress, delta);
544 case ELF::R_PPC64_ADDR64 :
545 writeInt64BE(LocalAddress, Value + Addend);
547 case ELF::R_PPC64_TOC :
548 writeInt64BE(LocalAddress, findPPC64TOC());
550 case ELF::R_PPC64_TOC16 : {
551 uint64_t TOCStart = findPPC64TOC();
552 Value = applyPPClo((Value + Addend) - TOCStart);
553 writeInt16BE(LocalAddress, applyPPClo(Value));
555 case ELF::R_PPC64_TOC16_DS : {
556 uint64_t TOCStart = findPPC64TOC();
557 Value = ((Value + Addend) - TOCStart);
558 writeInt16BE(LocalAddress, applyPPClo(Value));
563 void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
570 resolveX86_64Relocation(Section, Offset, Value, Type, Addend);
573 resolveX86Relocation(Section, Offset,
574 (uint32_t)(Value & 0xffffffffL), Type,
575 (uint32_t)(Addend & 0xffffffffL));
577 case Triple::arm: // Fall through.
579 resolveARMRelocation(Section, Offset,
580 (uint32_t)(Value & 0xffffffffL), Type,
581 (uint32_t)(Addend & 0xffffffffL));
583 case Triple::mips: // Fall through.
585 resolveMIPSRelocation(Section, Offset,
586 (uint32_t)(Value & 0xffffffffL), Type,
587 (uint32_t)(Addend & 0xffffffffL));
590 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
592 default: llvm_unreachable("Unsupported CPU type!");
596 void RuntimeDyldELF::processRelocationRef(unsigned SectionID,
597 relocation_iterator RelI,
599 ObjSectionToIDMap &ObjSectionToID,
600 const SymbolTableMap &Symbols,
603 Check(RelI->getType(RelType));
605 Check(RelI->getAdditionalInfo(Addend));
607 Check(RelI->getSymbol(Symbol));
609 // Obtain the symbol name which is referenced in the relocation
610 StringRef TargetName;
611 Symbol.getName(TargetName);
612 DEBUG(dbgs() << "\t\tRelType: " << RelType
613 << " Addend: " << Addend
614 << " TargetName: " << TargetName
616 RelocationValueRef Value;
617 // First search for the symbol in the local symbol table
618 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
619 SymbolRef::Type SymType;
620 Symbol.getType(SymType);
621 if (lsi != Symbols.end()) {
622 Value.SectionID = lsi->second.first;
623 Value.Addend = lsi->second.second + Addend;
625 // Search for the symbol in the global symbol table
626 SymbolTableMap::const_iterator gsi =
627 GlobalSymbolTable.find(TargetName.data());
628 if (gsi != GlobalSymbolTable.end()) {
629 Value.SectionID = gsi->second.first;
630 Value.Addend = gsi->second.second + Addend;
633 case SymbolRef::ST_Debug: {
634 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
635 // and can be changed by another developers. Maybe best way is add
636 // a new symbol type ST_Section to SymbolRef and use it.
637 section_iterator si(Obj.end_sections());
638 Symbol.getSection(si);
639 if (si == Obj.end_sections())
640 llvm_unreachable("Symbol section not found, bad object file format!");
641 DEBUG(dbgs() << "\t\tThis is section symbol\n");
642 // Default to 'true' in case isText fails (though it never does).
645 Value.SectionID = findOrEmitSection(Obj,
649 Value.Addend = Addend;
652 case SymbolRef::ST_Unknown: {
653 Value.SymbolName = TargetName.data();
654 Value.Addend = Addend;
658 llvm_unreachable("Unresolved symbol type!");
664 Check(RelI->getOffset(Offset));
666 DEBUG(dbgs() << "\t\tSectionID: " << SectionID
667 << " Offset: " << Offset
669 if (Arch == Triple::arm &&
670 (RelType == ELF::R_ARM_PC24 ||
671 RelType == ELF::R_ARM_CALL ||
672 RelType == ELF::R_ARM_JUMP24)) {
673 // This is an ARM branch relocation, need to use a stub function.
674 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
675 SectionEntry &Section = Sections[SectionID];
677 // Look for an existing stub.
678 StubMap::const_iterator i = Stubs.find(Value);
679 if (i != Stubs.end()) {
680 resolveRelocation(Section, Offset,
681 (uint64_t)Section.Address + i->second, RelType, 0);
682 DEBUG(dbgs() << " Stub function found\n");
684 // Create a new stub function.
685 DEBUG(dbgs() << " Create a new stub function\n");
686 Stubs[Value] = Section.StubOffset;
687 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
689 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
690 ELF::R_ARM_ABS32, Value.Addend);
691 if (Value.SymbolName)
692 addRelocationForSymbol(RE, Value.SymbolName);
694 addRelocationForSection(RE, Value.SectionID);
696 resolveRelocation(Section, Offset,
697 (uint64_t)Section.Address + Section.StubOffset,
699 Section.StubOffset += getMaxStubSize();
701 } else if ((Arch == Triple::mipsel || Arch == Triple::mips) &&
702 RelType == ELF::R_MIPS_26) {
703 // This is an Mips branch relocation, need to use a stub function.
704 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
705 SectionEntry &Section = Sections[SectionID];
706 uint8_t *Target = Section.Address + Offset;
707 uint32_t *TargetAddress = (uint32_t *)Target;
709 // Extract the addend from the instruction.
710 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
712 Value.Addend += Addend;
714 // Look up for existing stub.
715 StubMap::const_iterator i = Stubs.find(Value);
716 if (i != Stubs.end()) {
717 resolveRelocation(Section, Offset,
718 (uint64_t)Section.Address + i->second, RelType, 0);
719 DEBUG(dbgs() << " Stub function found\n");
721 // Create a new stub function.
722 DEBUG(dbgs() << " Create a new stub function\n");
723 Stubs[Value] = Section.StubOffset;
724 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
727 // Creating Hi and Lo relocations for the filled stub instructions.
728 RelocationEntry REHi(SectionID,
729 StubTargetAddr - Section.Address,
730 ELF::R_MIPS_HI16, Value.Addend);
731 RelocationEntry RELo(SectionID,
732 StubTargetAddr - Section.Address + 4,
733 ELF::R_MIPS_LO16, Value.Addend);
735 if (Value.SymbolName) {
736 addRelocationForSymbol(REHi, Value.SymbolName);
737 addRelocationForSymbol(RELo, Value.SymbolName);
739 addRelocationForSection(REHi, Value.SectionID);
740 addRelocationForSection(RELo, Value.SectionID);
743 resolveRelocation(Section, Offset,
744 (uint64_t)Section.Address + Section.StubOffset,
746 Section.StubOffset += getMaxStubSize();
748 } else if (Arch == Triple::ppc64) {
749 if (RelType == ELF::R_PPC64_REL24) {
750 // A PPC branch relocation will need a stub function if the target is
751 // an external symbol (Symbol::ST_Unknown) or if the target address
752 // is not within the signed 24-bits branch address.
753 SectionEntry &Section = Sections[SectionID];
754 uint8_t *Target = Section.Address + Offset;
755 bool RangeOverflow = false;
756 if (SymType != SymbolRef::ST_Unknown) {
757 // A function call may points to the .opd entry, so the final symbol value
758 // in calculated based in the relocation values in .opd section.
759 findOPDEntrySection(Obj, ObjSectionToID, Value);
760 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
761 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
762 // If it is within 24-bits branch range, just set the branch target
763 if (SignExtend32<24>(delta) == delta) {
764 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
765 if (Value.SymbolName)
766 addRelocationForSymbol(RE, Value.SymbolName);
768 addRelocationForSection(RE, Value.SectionID);
770 RangeOverflow = true;
773 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
774 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
775 // larger than 24-bits.
776 StubMap::const_iterator i = Stubs.find(Value);
777 if (i != Stubs.end()) {
778 // Symbol function stub already created, just relocate to it
779 resolveRelocation(Section, Offset,
780 (uint64_t)Section.Address + i->second, RelType, 0);
781 DEBUG(dbgs() << " Stub function found\n");
783 // Create a new stub function.
784 DEBUG(dbgs() << " Create a new stub function\n");
785 Stubs[Value] = Section.StubOffset;
786 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
788 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
789 ELF::R_PPC64_ADDR64, Value.Addend);
791 // Generates the 64-bits address loads as exemplified in section
792 // 4.5.1 in PPC64 ELF ABI.
793 RelocationEntry REhst(SectionID,
794 StubTargetAddr - Section.Address + 2,
795 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
796 RelocationEntry REhr(SectionID,
797 StubTargetAddr - Section.Address + 6,
798 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
799 RelocationEntry REh(SectionID,
800 StubTargetAddr - Section.Address + 14,
801 ELF::R_PPC64_ADDR16_HI, Value.Addend);
802 RelocationEntry REl(SectionID,
803 StubTargetAddr - Section.Address + 18,
804 ELF::R_PPC64_ADDR16_LO, Value.Addend);
806 if (Value.SymbolName) {
807 addRelocationForSymbol(REhst, Value.SymbolName);
808 addRelocationForSymbol(REhr, Value.SymbolName);
809 addRelocationForSymbol(REh, Value.SymbolName);
810 addRelocationForSymbol(REl, Value.SymbolName);
812 addRelocationForSection(REhst, Value.SectionID);
813 addRelocationForSection(REhr, Value.SectionID);
814 addRelocationForSection(REh, Value.SectionID);
815 addRelocationForSection(REl, Value.SectionID);
818 resolveRelocation(Section, Offset,
819 (uint64_t)Section.Address + Section.StubOffset,
821 if (SymType == SymbolRef::ST_Unknown)
822 // Restore the TOC for external calls
823 writeInt32BE(Target+4, 0xE8410028); // ld r2,40(r1)
824 Section.StubOffset += getMaxStubSize();
828 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
829 // Extra check to avoid relocation againt empty symbols (usually
831 if (Value.SymbolName && !TargetName.empty())
832 addRelocationForSymbol(RE, Value.SymbolName);
834 addRelocationForSection(RE, Value.SectionID);
837 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
838 if (Value.SymbolName)
839 addRelocationForSymbol(RE, Value.SymbolName);
841 addRelocationForSection(RE, Value.SectionID);
845 unsigned RuntimeDyldELF::getCommonSymbolAlignment(const SymbolRef &Sym) {
846 // In ELF, the value of an SHN_COMMON symbol is its alignment requirement.
848 Check(Sym.getValue(Align));
852 bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
853 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
855 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;