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 "llvm/ADT/OwningPtr.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/IntervalMap.h"
19 #include "RuntimeDyldELF.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/Support/ELF.h"
22 #include "llvm/ADT/Triple.h"
23 #include "llvm/Object/ELF.h"
24 #include "JITRegistrar.h"
26 using namespace llvm::object;
30 template<support::endianness target_endianness, bool is64Bits>
31 class DyldELFObject : public ELFObjectFile<target_endianness, is64Bits> {
32 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
34 typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
35 typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
36 typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel;
37 typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela;
39 typedef Elf_Ehdr_Impl<target_endianness, is64Bits> Elf_Ehdr;
41 typedef typename ELFDataTypeTypedefHelper<
42 target_endianness, is64Bits>::value_type addr_type;
45 // This duplicates the 'Data' member in the 'Binary' base class
46 // but it is necessary to workaround a bug in gcc 4.2
47 MemoryBuffer *InputData;
50 DyldELFObject(MemoryBuffer *Object, error_code &ec);
52 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
53 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr);
55 const MemoryBuffer& getBuffer() const { return *InputData; }
57 // Methods for type inquiry through isa, cast and dyn_cast
58 static inline bool classof(const Binary *v) {
59 return (isa<ELFObjectFile<target_endianness, is64Bits> >(v)
60 && classof(cast<ELFObjectFile<target_endianness, is64Bits> >(v)));
62 static inline bool classof(
63 const ELFObjectFile<target_endianness, is64Bits> *v) {
64 return v->isDyldType();
66 static inline bool classof(const DyldELFObject *v) {
71 template<support::endianness target_endianness, bool is64Bits>
72 class ELFObjectImage : public ObjectImage {
74 DyldELFObject<target_endianness, is64Bits> *DyldObj;
78 ELFObjectImage(DyldELFObject<target_endianness, is64Bits> *Obj)
83 virtual ~ELFObjectImage() {
85 deregisterWithDebugger();
88 // Subclasses can override these methods to update the image with loaded
89 // addresses for sections and common symbols
90 virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr)
92 DyldObj->updateSectionAddress(Sec, Addr);
95 virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr)
97 DyldObj->updateSymbolAddress(Sym, Addr);
100 virtual void registerWithDebugger()
102 JITRegistrar::getGDBRegistrar().registerObject(DyldObj->getBuffer());
105 virtual void deregisterWithDebugger()
107 JITRegistrar::getGDBRegistrar().deregisterObject(DyldObj->getBuffer());
111 template<support::endianness target_endianness, bool is64Bits>
112 DyldELFObject<target_endianness, is64Bits>::DyldELFObject(MemoryBuffer *Object,
114 : ELFObjectFile<target_endianness, is64Bits>(Object, ec),
116 this->isDyldELFObject = true;
119 template<support::endianness target_endianness, bool is64Bits>
120 void DyldELFObject<target_endianness, is64Bits>::updateSectionAddress(
121 const SectionRef &Sec,
123 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
124 Elf_Shdr *shdr = const_cast<Elf_Shdr*>(
125 reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
127 // This assumes the address passed in matches the target address bitness
128 // The template-based type cast handles everything else.
129 shdr->sh_addr = static_cast<addr_type>(Addr);
132 template<support::endianness target_endianness, bool is64Bits>
133 void DyldELFObject<target_endianness, is64Bits>::updateSymbolAddress(
134 const SymbolRef &SymRef,
137 Elf_Sym *sym = const_cast<Elf_Sym*>(
138 ELFObjectFile<target_endianness, is64Bits>::
139 getSymbol(SymRef.getRawDataRefImpl()));
141 // This assumes the address passed in matches the target address bitness
142 // The template-based type cast handles everything else.
143 sym->st_value = static_cast<addr_type>(Addr);
151 ObjectImage *RuntimeDyldELF::createObjectImage(
152 const MemoryBuffer *ConstInputBuffer) {
153 MemoryBuffer *InputBuffer = const_cast<MemoryBuffer*>(ConstInputBuffer);
154 std::pair<unsigned char, unsigned char> Ident = getElfArchType(InputBuffer);
157 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
158 DyldELFObject<support::little, false> *Obj =
159 new DyldELFObject<support::little, false>(InputBuffer, ec);
160 return new ELFObjectImage<support::little, false>(Obj);
162 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
163 DyldELFObject<support::big, false> *Obj =
164 new DyldELFObject<support::big, false>(InputBuffer, ec);
165 return new ELFObjectImage<support::big, false>(Obj);
167 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
168 DyldELFObject<support::big, true> *Obj =
169 new DyldELFObject<support::big, true>(InputBuffer, ec);
170 return new ELFObjectImage<support::big, true>(Obj);
172 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
173 DyldELFObject<support::little, true> *Obj =
174 new DyldELFObject<support::little, true>(InputBuffer, ec);
175 return new ELFObjectImage<support::little, true>(Obj);
178 llvm_unreachable("Unexpected ELF format");
181 void RuntimeDyldELF::handleObjectLoaded(ObjectImage *Obj)
183 Obj->registerWithDebugger();
184 // Save the loaded object. It will deregister itself when deleted
188 RuntimeDyldELF::~RuntimeDyldELF() {
193 void RuntimeDyldELF::resolveX86_64Relocation(uint8_t *LocalAddress,
194 uint64_t FinalAddress,
200 llvm_unreachable("Relocation type not implemented yet!");
202 case ELF::R_X86_64_64: {
203 uint64_t *Target = (uint64_t*)(LocalAddress);
204 *Target = Value + Addend;
207 case ELF::R_X86_64_32:
208 case ELF::R_X86_64_32S: {
210 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
211 (Type == ELF::R_X86_64_32S &&
212 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
213 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
214 uint32_t *Target = reinterpret_cast<uint32_t*>(LocalAddress);
215 *Target = TruncatedAddr;
218 case ELF::R_X86_64_PC32: {
219 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(LocalAddress);
220 int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
221 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
222 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
223 *Placeholder = TruncOffset;
229 void RuntimeDyldELF::resolveX86Relocation(uint8_t *LocalAddress,
230 uint32_t FinalAddress,
235 case ELF::R_386_32: {
236 uint32_t *Target = (uint32_t*)(LocalAddress);
237 uint32_t Placeholder = *Target;
238 *Target = Placeholder + Value + Addend;
241 case ELF::R_386_PC32: {
242 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(LocalAddress);
243 uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
244 *Placeholder = RealOffset;
248 // There are other relocation types, but it appears these are the
249 // only ones currently used by the LLVM ELF object writer
250 llvm_unreachable("Relocation type not implemented yet!");
255 void RuntimeDyldELF::resolveARMRelocation(uint8_t *LocalAddress,
256 uint32_t FinalAddress,
260 // TODO: Add Thumb relocations.
261 uint32_t* TargetPtr = (uint32_t*)LocalAddress;
264 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: " << LocalAddress
265 << " FinalAddress: " << format("%p",FinalAddress)
266 << " Value: " << format("%x",Value)
267 << " Type: " << format("%x",Type)
268 << " Addend: " << format("%x",Addend)
273 llvm_unreachable("Not implemented relocation type!");
275 // Just write 32bit value to relocation address
276 case ELF::R_ARM_ABS32 :
280 // Write first 16 bit of 32 bit value to the mov instruction.
281 // Last 4 bit should be shifted.
282 case ELF::R_ARM_MOVW_ABS_NC :
283 Value = Value & 0xFFFF;
284 *TargetPtr |= Value & 0xFFF;
285 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
288 // Write last 16 bit of 32 bit value to the mov instruction.
289 // Last 4 bit should be shifted.
290 case ELF::R_ARM_MOVT_ABS :
291 Value = (Value >> 16) & 0xFFFF;
292 *TargetPtr |= Value & 0xFFF;
293 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
296 // Write 24 bit relative value to the branch instruction.
297 case ELF::R_ARM_PC24 : // Fall through.
298 case ELF::R_ARM_CALL : // Fall through.
299 case ELF::R_ARM_JUMP24 :
300 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
301 RelValue = (RelValue & 0x03FFFFFC) >> 2;
302 *TargetPtr &= 0xFF000000;
303 *TargetPtr |= RelValue;
308 void RuntimeDyldELF::resolveMIPSRelocation(uint8_t *LocalAddress,
309 uint32_t FinalAddress,
313 uint32_t* TargetPtr = (uint32_t*)LocalAddress;
316 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: " << LocalAddress
317 << " FinalAddress: " << format("%p",FinalAddress)
318 << " Value: " << format("%x",Value)
319 << " Type: " << format("%x",Type)
320 << " Addend: " << format("%x",Addend)
325 llvm_unreachable("Not implemented relocation type!");
328 *TargetPtr = Value + (*TargetPtr);
331 *TargetPtr = ((*TargetPtr) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
333 case ELF::R_MIPS_HI16:
334 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
335 Value += ((*TargetPtr) & 0x0000ffff) << 16;
336 *TargetPtr = ((*TargetPtr) & 0xffff0000) |
337 (((Value + 0x8000) >> 16) & 0xffff);
339 case ELF::R_MIPS_LO16:
340 Value += ((*TargetPtr) & 0x0000ffff);
341 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
346 void RuntimeDyldELF::resolveRelocation(uint8_t *LocalAddress,
347 uint64_t FinalAddress,
353 resolveX86_64Relocation(LocalAddress, FinalAddress, Value, Type, Addend);
356 resolveX86Relocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL),
357 (uint32_t)(Value & 0xffffffffL), Type,
358 (uint32_t)(Addend & 0xffffffffL));
360 case Triple::arm: // Fall through.
362 resolveARMRelocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL),
363 (uint32_t)(Value & 0xffffffffL), Type,
364 (uint32_t)(Addend & 0xffffffffL));
366 case Triple::mips: // Fall through.
368 resolveMIPSRelocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL),
369 (uint32_t)(Value & 0xffffffffL), Type,
370 (uint32_t)(Addend & 0xffffffffL));
372 default: llvm_unreachable("Unsupported CPU type!");
376 void RuntimeDyldELF::processRelocationRef(const ObjRelocationInfo &Rel,
378 ObjSectionToIDMap &ObjSectionToID,
379 const SymbolTableMap &Symbols,
382 uint32_t RelType = (uint32_t)(Rel.Type & 0xffffffffL);
383 intptr_t Addend = (intptr_t)Rel.AdditionalInfo;
384 const SymbolRef &Symbol = Rel.Symbol;
386 // Obtain the symbol name which is referenced in the relocation
387 StringRef TargetName;
388 Symbol.getName(TargetName);
389 DEBUG(dbgs() << "\t\tRelType: " << RelType
390 << " Addend: " << Addend
391 << " TargetName: " << TargetName
393 RelocationValueRef Value;
394 // First search for the symbol in the local symbol table
395 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
396 if (lsi != Symbols.end()) {
397 Value.SectionID = lsi->second.first;
398 Value.Addend = lsi->second.second;
400 // Search for the symbol in the global symbol table
401 SymbolTableMap::const_iterator gsi =
402 GlobalSymbolTable.find(TargetName.data());
403 if (gsi != GlobalSymbolTable.end()) {
404 Value.SectionID = gsi->second.first;
405 Value.Addend = gsi->second.second;
407 SymbolRef::Type SymType;
408 Symbol.getType(SymType);
410 case SymbolRef::ST_Debug: {
411 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
412 // and can be changed by another developers. Maybe best way is add
413 // a new symbol type ST_Section to SymbolRef and use it.
414 section_iterator si(Obj.end_sections());
415 Symbol.getSection(si);
416 if (si == Obj.end_sections())
417 llvm_unreachable("Symbol section not found, bad object file format!");
418 DEBUG(dbgs() << "\t\tThis is section symbol\n");
419 Value.SectionID = findOrEmitSection(Obj, (*si), true, ObjSectionToID);
420 Value.Addend = Addend;
423 case SymbolRef::ST_Unknown: {
424 Value.SymbolName = TargetName.data();
425 Value.Addend = Addend;
429 llvm_unreachable("Unresolved symbol type!");
434 DEBUG(dbgs() << "\t\tRel.SectionID: " << Rel.SectionID
435 << " Rel.Offset: " << Rel.Offset
437 if (Arch == Triple::arm &&
438 (RelType == ELF::R_ARM_PC24 ||
439 RelType == ELF::R_ARM_CALL ||
440 RelType == ELF::R_ARM_JUMP24)) {
441 // This is an ARM branch relocation, need to use a stub function.
442 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
443 SectionEntry &Section = Sections[Rel.SectionID];
444 uint8_t *Target = Section.Address + Rel.Offset;
446 // Look up for existing stub.
447 StubMap::const_iterator i = Stubs.find(Value);
448 if (i != Stubs.end()) {
449 resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address +
450 i->second, RelType, 0);
451 DEBUG(dbgs() << " Stub function found\n");
453 // Create a new stub function.
454 DEBUG(dbgs() << " Create a new stub function\n");
455 Stubs[Value] = Section.StubOffset;
456 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
458 RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address,
459 ELF::R_ARM_ABS32, Value.Addend);
460 if (Value.SymbolName)
461 addRelocationForSymbol(RE, Value.SymbolName);
463 addRelocationForSection(RE, Value.SectionID);
465 resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address +
466 Section.StubOffset, RelType, 0);
467 Section.StubOffset += getMaxStubSize();
469 } else if (Arch == Triple::mipsel && RelType == ELF::R_MIPS_26) {
470 // This is an Mips branch relocation, need to use a stub function.
471 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
472 SectionEntry &Section = Sections[Rel.SectionID];
473 uint8_t *Target = Section.Address + Rel.Offset;
474 uint32_t *TargetAddress = (uint32_t *)Target;
476 // Extract the addend from the instruction.
477 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
479 Value.Addend += Addend;
481 // Look up for existing stub.
482 StubMap::const_iterator i = Stubs.find(Value);
483 if (i != Stubs.end()) {
484 resolveRelocation(Target, (uint64_t)Target,
485 (uint64_t)Section.Address +
486 i->second, RelType, 0);
487 DEBUG(dbgs() << " Stub function found\n");
489 // Create a new stub function.
490 DEBUG(dbgs() << " Create a new stub function\n");
491 Stubs[Value] = Section.StubOffset;
492 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
495 // Creating Hi and Lo relocations for the filled stub instructions.
496 RelocationEntry REHi(Rel.SectionID,
497 StubTargetAddr - Section.Address,
498 ELF::R_MIPS_HI16, Value.Addend);
499 RelocationEntry RELo(Rel.SectionID,
500 StubTargetAddr - Section.Address + 4,
501 ELF::R_MIPS_LO16, Value.Addend);
503 if (Value.SymbolName) {
504 addRelocationForSymbol(REHi, Value.SymbolName);
505 addRelocationForSymbol(RELo, Value.SymbolName);
507 addRelocationForSection(REHi, Value.SectionID);
508 addRelocationForSection(RELo, Value.SectionID);
511 resolveRelocation(Target, (uint64_t)Target,
512 (uint64_t)Section.Address +
513 Section.StubOffset, RelType, 0);
514 Section.StubOffset += getMaxStubSize();
517 RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
518 if (Value.SymbolName)
519 addRelocationForSymbol(RE, Value.SymbolName);
521 addRelocationForSection(RE, Value.SectionID);
525 bool RuntimeDyldELF::isCompatibleFormat(const MemoryBuffer *InputBuffer) const {
526 StringRef Magic = InputBuffer->getBuffer().slice(0, ELF::EI_NIDENT);
527 return (memcmp(Magic.data(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;