RuntimeDyldELF.cpp revision 239462
190075Sobrien//===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===//
250397Sobrien//
3169689Skan//                     The LLVM Compiler Infrastructure
450397Sobrien//
590075Sobrien// This file is distributed under the University of Illinois Open Source
650397Sobrien// License. See LICENSE.TXT for details.
790075Sobrien//
890075Sobrien//===----------------------------------------------------------------------===//
990075Sobrien//
1090075Sobrien// Implementation of ELF support for the MC-JIT runtime dynamic linker.
1150397Sobrien//
1290075Sobrien//===----------------------------------------------------------------------===//
1390075Sobrien
1490075Sobrien#define DEBUG_TYPE "dyld"
1590075Sobrien#include "llvm/ADT/OwningPtr.h"
1650397Sobrien#include "llvm/ADT/StringRef.h"
1750397Sobrien#include "llvm/ADT/STLExtras.h"
1890075Sobrien#include "llvm/ADT/IntervalMap.h"
19169689Skan#include "RuntimeDyldELF.h"
20169689Skan#include "llvm/Object/ObjectFile.h"
2150397Sobrien#include "llvm/Support/ELF.h"
2250397Sobrien#include "llvm/ADT/Triple.h"
2350397Sobrien#include "llvm/Object/ELF.h"
2450397Sobrien#include "JITRegistrar.h"
2550397Sobrienusing namespace llvm;
2650397Sobrienusing namespace llvm::object;
2750397Sobrien
2850397Sobriennamespace {
2990075Sobrien
3090075Sobrientemplate<support::endianness target_endianness, bool is64Bits>
3150397Sobrienclass DyldELFObject : public ELFObjectFile<target_endianness, is64Bits> {
3290075Sobrien  LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
3350397Sobrien
3450397Sobrien  typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
35169689Skan  typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
3650397Sobrien  typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel;
3750397Sobrien  typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela;
3850397Sobrien
3990075Sobrien  typedef typename ELFObjectFile<target_endianness, is64Bits>::
4090075Sobrien    Elf_Ehdr Elf_Ehdr;
4190075Sobrien
4290075Sobrien  typedef typename ELFDataTypeTypedefHelper<
4390075Sobrien          target_endianness, is64Bits>::value_type addr_type;
4490075Sobrien
4590075Sobrienprotected:
4690075Sobrien  // This duplicates the 'Data' member in the 'Binary' base class
4790075Sobrien  // but it is necessary to workaround a bug in gcc 4.2
4890075Sobrien  MemoryBuffer *InputData;
4990075Sobrien
5090075Sobrienpublic:
5190075Sobrien  DyldELFObject(MemoryBuffer *Object, error_code &ec);
5290075Sobrien
5350397Sobrien  void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
54117395Skan  void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr);
5550397Sobrien
5690075Sobrien  const MemoryBuffer& getBuffer() const { return *InputData; }
5790075Sobrien
5890075Sobrien  // Methods for type inquiry through isa, cast and dyn_cast
5990075Sobrien  static inline bool classof(const Binary *v) {
6090075Sobrien    return (isa<ELFObjectFile<target_endianness, is64Bits> >(v)
6190075Sobrien            && classof(cast<ELFObjectFile<target_endianness, is64Bits> >(v)));
62117395Skan  }
6390075Sobrien  static inline bool classof(
6490075Sobrien      const ELFObjectFile<target_endianness, is64Bits> *v) {
6590075Sobrien    return v->isDyldType();
6690075Sobrien  }
6790075Sobrien  static inline bool classof(const DyldELFObject *v) {
6890075Sobrien    return true;
6990075Sobrien  }
7090075Sobrien};
7190075Sobrien
72117395Skantemplate<support::endianness target_endianness, bool is64Bits>
7390075Sobrienclass ELFObjectImage : public ObjectImage {
7490075Sobrien  protected:
7590075Sobrien    DyldELFObject<target_endianness, is64Bits> *DyldObj;
7690075Sobrien    bool Registered;
7790075Sobrien
7890075Sobrien  public:
7990075Sobrien    ELFObjectImage(DyldELFObject<target_endianness, is64Bits> *Obj)
80117395Skan    : ObjectImage(Obj),
8190075Sobrien      DyldObj(Obj),
8290075Sobrien      Registered(false) {}
8390075Sobrien
8490075Sobrien    virtual ~ELFObjectImage() {
8590075Sobrien      if (Registered)
8690075Sobrien        deregisterWithDebugger();
8790075Sobrien    }
88117395Skan
8990075Sobrien    // Subclasses can override these methods to update the image with loaded
9090075Sobrien    // addresses for sections and common symbols
9190075Sobrien    virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr)
9290075Sobrien    {
9390075Sobrien      DyldObj->updateSectionAddress(Sec, Addr);
9490075Sobrien    }
95117395Skan
9690075Sobrien    virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr)
9790075Sobrien    {
9890075Sobrien      DyldObj->updateSymbolAddress(Sym, Addr);
9990075Sobrien    }
10090075Sobrien
10190075Sobrien    virtual void registerWithDebugger()
102117395Skan    {
10390075Sobrien      JITRegistrar::getGDBRegistrar().registerObject(DyldObj->getBuffer());
10490075Sobrien      Registered = true;
10590075Sobrien    }
106117395Skan    virtual void deregisterWithDebugger()
10790075Sobrien    {
10890075Sobrien      JITRegistrar::getGDBRegistrar().deregisterObject(DyldObj->getBuffer());
10990075Sobrien    }
11090075Sobrien};
11190075Sobrien
112117395Skantemplate<support::endianness target_endianness, bool is64Bits>
11390075SobrienDyldELFObject<target_endianness, is64Bits>::DyldELFObject(MemoryBuffer *Object,
11490075Sobrien                                                          error_code &ec)
115117395Skan  : ELFObjectFile<target_endianness, is64Bits>(Object, ec),
11690075Sobrien    InputData(Object) {
11790075Sobrien  this->isDyldELFObject = true;
11890075Sobrien}
11990075Sobrien
120117395Skantemplate<support::endianness target_endianness, bool is64Bits>
12190075Sobrienvoid DyldELFObject<target_endianness, is64Bits>::updateSectionAddress(
12290075Sobrien                                                       const SectionRef &Sec,
12350397Sobrien                                                       uint64_t Addr) {
12450397Sobrien  DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
12550397Sobrien  Elf_Shdr *shdr = const_cast<Elf_Shdr*>(
12690075Sobrien                          reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
12790075Sobrien
128117395Skan  // This assumes the address passed in matches the target address bitness
12990075Sobrien  // The template-based type cast handles everything else.
13090075Sobrien  shdr->sh_addr = static_cast<addr_type>(Addr);
13190075Sobrien}
13290075Sobrien
13390075Sobrientemplate<support::endianness target_endianness, bool is64Bits>
13490075Sobrienvoid DyldELFObject<target_endianness, is64Bits>::updateSymbolAddress(
13590075Sobrien                                                       const SymbolRef &SymRef,
13650397Sobrien                                                       uint64_t Addr) {
137117395Skan
13850397Sobrien  Elf_Sym *sym = const_cast<Elf_Sym*>(
13950397Sobrien                                 ELFObjectFile<target_endianness, is64Bits>::
14050397Sobrien                                   getSymbol(SymRef.getRawDataRefImpl()));
14150397Sobrien
14290075Sobrien  // This assumes the address passed in matches the target address bitness
14350397Sobrien  // The template-based type cast handles everything else.
144117395Skan  sym->st_value = static_cast<addr_type>(Addr);
14550397Sobrien}
14650397Sobrien
14750397Sobrien} // namespace
14850397Sobrien
14990075Sobrien
15050397Sobriennamespace llvm {
151117395Skan
15250397SobrienObjectImage *RuntimeDyldELF::createObjectImage(
15390075Sobrien                                         const MemoryBuffer *ConstInputBuffer) {
15450397Sobrien  MemoryBuffer *InputBuffer = const_cast<MemoryBuffer*>(ConstInputBuffer);
15550397Sobrien  std::pair<unsigned char, unsigned char> Ident = getElfArchType(InputBuffer);
15650397Sobrien  error_code ec;
15790075Sobrien
15890075Sobrien  if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
159117395Skan    DyldELFObject<support::little, false> *Obj =
16090075Sobrien           new DyldELFObject<support::little, false>(InputBuffer, ec);
16190075Sobrien    return new ELFObjectImage<support::little, false>(Obj);
16290075Sobrien  }
16390075Sobrien  else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
16490075Sobrien    DyldELFObject<support::big, false> *Obj =
16590075Sobrien           new DyldELFObject<support::big, false>(InputBuffer, ec);
16690075Sobrien    return new ELFObjectImage<support::big, false>(Obj);
167117395Skan  }
16890075Sobrien  else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
16990075Sobrien    DyldELFObject<support::big, true> *Obj =
17090075Sobrien           new DyldELFObject<support::big, true>(InputBuffer, ec);
17190075Sobrien    return new ELFObjectImage<support::big, true>(Obj);
17290075Sobrien  }
17390075Sobrien  else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
17490075Sobrien    DyldELFObject<support::little, true> *Obj =
17590075Sobrien           new DyldELFObject<support::little, true>(InputBuffer, ec);
176117395Skan    return new ELFObjectImage<support::little, true>(Obj);
17790075Sobrien  }
17890075Sobrien  else
17990075Sobrien    llvm_unreachable("Unexpected ELF format");
18090075Sobrien}
18190075Sobrien
18290075Sobrienvoid RuntimeDyldELF::handleObjectLoaded(ObjectImage *Obj)
183117395Skan{
18490075Sobrien  Obj->registerWithDebugger();
18590075Sobrien  // Save the loaded object.  It will deregister itself when deleted
18690075Sobrien  LoadedObject = Obj;
18790075Sobrien}
18890075Sobrien
18990075SobrienRuntimeDyldELF::~RuntimeDyldELF() {
190117395Skan  if (LoadedObject)
191117395Skan    delete LoadedObject;
19290075Sobrien}
19390075Sobrien
19490075Sobrienvoid RuntimeDyldELF::resolveX86_64Relocation(uint8_t *LocalAddress,
19590075Sobrien                                             uint64_t FinalAddress,
19690075Sobrien                                             uint64_t Value,
19790075Sobrien                                             uint32_t Type,
198117395Skan                                             int64_t Addend) {
19990075Sobrien  switch (Type) {
20090075Sobrien  default:
20190075Sobrien    llvm_unreachable("Relocation type not implemented yet!");
20290075Sobrien  break;
20390075Sobrien  case ELF::R_X86_64_64: {
20490075Sobrien    uint64_t *Target = (uint64_t*)(LocalAddress);
205117395Skan    *Target = Value + Addend;
20690075Sobrien    break;
20790075Sobrien  }
20890075Sobrien  case ELF::R_X86_64_32:
20990075Sobrien  case ELF::R_X86_64_32S: {
21090075Sobrien    Value += Addend;
21190075Sobrien    assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
21290075Sobrien           (Type == ELF::R_X86_64_32S &&
21390075Sobrien             ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
21490075Sobrien    uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
21590075Sobrien    uint32_t *Target = reinterpret_cast<uint32_t*>(LocalAddress);
21690075Sobrien    *Target = TruncatedAddr;
21790075Sobrien    break;
21890075Sobrien  }
21990075Sobrien  case ELF::R_X86_64_PC32: {
22090075Sobrien    uint32_t *Placeholder = reinterpret_cast<uint32_t*>(LocalAddress);
22190075Sobrien    int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
22290075Sobrien    assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
22390075Sobrien    int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
22490075Sobrien    *Placeholder = TruncOffset;
22590075Sobrien    break;
22690075Sobrien  }
22790075Sobrien  }
22890075Sobrien}
22990075Sobrien
23090075Sobrienvoid RuntimeDyldELF::resolveX86Relocation(uint8_t *LocalAddress,
23190075Sobrien                                          uint32_t FinalAddress,
23290075Sobrien                                          uint32_t Value,
23390075Sobrien                                          uint32_t Type,
23490075Sobrien                                          int32_t Addend) {
23590075Sobrien  switch (Type) {
236169689Skan  case ELF::R_386_32: {
237169689Skan    uint32_t *Target = (uint32_t*)(LocalAddress);
238169689Skan    uint32_t Placeholder = *Target;
239169689Skan    *Target = Placeholder + Value + Addend;
240169689Skan    break;
241169689Skan  }
242169689Skan  case ELF::R_386_PC32: {
243169689Skan    uint32_t *Placeholder = reinterpret_cast<uint32_t*>(LocalAddress);
244169689Skan    uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
245169689Skan    *Placeholder = RealOffset;
246169689Skan    break;
247169689Skan    }
248169689Skan    default:
249169689Skan      // There are other relocation types, but it appears these are the
250169689Skan      // only ones currently used by the LLVM ELF object writer
251169689Skan      llvm_unreachable("Relocation type not implemented yet!");
252169689Skan      break;
253169689Skan  }
25490075Sobrien}
25590075Sobrien
25690075Sobrienvoid RuntimeDyldELF::resolveARMRelocation(uint8_t *LocalAddress,
25790075Sobrien                                          uint32_t FinalAddress,
25890075Sobrien                                          uint32_t Value,
259                                          uint32_t Type,
260                                          int32_t Addend) {
261  // TODO: Add Thumb relocations.
262  uint32_t* TargetPtr = (uint32_t*)LocalAddress;
263  Value += Addend;
264
265  DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: " << LocalAddress
266               << " FinalAddress: " << format("%p",FinalAddress)
267               << " Value: " << format("%x",Value)
268               << " Type: " << format("%x",Type)
269               << " Addend: " << format("%x",Addend)
270               << "\n");
271
272  switch(Type) {
273  default:
274    llvm_unreachable("Not implemented relocation type!");
275
276  // Just write 32bit value to relocation address
277  case ELF::R_ARM_ABS32 :
278    *TargetPtr = Value;
279    break;
280
281  // Write first 16 bit of 32 bit value to the mov instruction.
282  // Last 4 bit should be shifted.
283  case ELF::R_ARM_MOVW_ABS_NC :
284    Value = Value & 0xFFFF;
285    *TargetPtr |= Value & 0xFFF;
286    *TargetPtr |= ((Value >> 12) & 0xF) << 16;
287    break;
288
289  // Write last 16 bit of 32 bit value to the mov instruction.
290  // Last 4 bit should be shifted.
291  case ELF::R_ARM_MOVT_ABS :
292    Value = (Value >> 16) & 0xFFFF;
293    *TargetPtr |= Value & 0xFFF;
294    *TargetPtr |= ((Value >> 12) & 0xF) << 16;
295    break;
296
297  // Write 24 bit relative value to the branch instruction.
298  case ELF::R_ARM_PC24 :    // Fall through.
299  case ELF::R_ARM_CALL :    // Fall through.
300  case ELF::R_ARM_JUMP24 :
301    int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
302    RelValue = (RelValue & 0x03FFFFFC) >> 2;
303    *TargetPtr &= 0xFF000000;
304    *TargetPtr |= RelValue;
305    break;
306  }
307}
308
309void RuntimeDyldELF::resolveRelocation(uint8_t *LocalAddress,
310                                       uint64_t FinalAddress,
311                                       uint64_t Value,
312                                       uint32_t Type,
313                                       int64_t Addend) {
314  switch (Arch) {
315  case Triple::x86_64:
316    resolveX86_64Relocation(LocalAddress, FinalAddress, Value, Type, Addend);
317    break;
318  case Triple::x86:
319    resolveX86Relocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL),
320                         (uint32_t)(Value & 0xffffffffL), Type,
321                         (uint32_t)(Addend & 0xffffffffL));
322    break;
323  case Triple::arm:    // Fall through.
324  case Triple::thumb:
325    resolveARMRelocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL),
326                         (uint32_t)(Value & 0xffffffffL), Type,
327                         (uint32_t)(Addend & 0xffffffffL));
328    break;
329  default: llvm_unreachable("Unsupported CPU type!");
330  }
331}
332
333void RuntimeDyldELF::processRelocationRef(const ObjRelocationInfo &Rel,
334                                          ObjectImage &Obj,
335                                          ObjSectionToIDMap &ObjSectionToID,
336                                          const SymbolTableMap &Symbols,
337                                          StubMap &Stubs) {
338
339  uint32_t RelType = (uint32_t)(Rel.Type & 0xffffffffL);
340  intptr_t Addend = (intptr_t)Rel.AdditionalInfo;
341  const SymbolRef &Symbol = Rel.Symbol;
342
343  // Obtain the symbol name which is referenced in the relocation
344  StringRef TargetName;
345  Symbol.getName(TargetName);
346  DEBUG(dbgs() << "\t\tRelType: " << RelType
347               << " Addend: " << Addend
348               << " TargetName: " << TargetName
349               << "\n");
350  RelocationValueRef Value;
351  // First search for the symbol in the local symbol table
352  SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
353  if (lsi != Symbols.end()) {
354    Value.SectionID = lsi->second.first;
355    Value.Addend = lsi->second.second;
356  } else {
357    // Search for the symbol in the global symbol table
358    SymbolTableMap::const_iterator gsi =
359        GlobalSymbolTable.find(TargetName.data());
360    if (gsi != GlobalSymbolTable.end()) {
361      Value.SectionID = gsi->second.first;
362      Value.Addend = gsi->second.second;
363    } else {
364      SymbolRef::Type SymType;
365      Symbol.getType(SymType);
366      switch (SymType) {
367        case SymbolRef::ST_Debug: {
368          // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
369          // and can be changed by another developers. Maybe best way is add
370          // a new symbol type ST_Section to SymbolRef and use it.
371          section_iterator si(Obj.end_sections());
372          Symbol.getSection(si);
373          if (si == Obj.end_sections())
374            llvm_unreachable("Symbol section not found, bad object file format!");
375          DEBUG(dbgs() << "\t\tThis is section symbol\n");
376          Value.SectionID = findOrEmitSection(Obj, (*si), true, ObjSectionToID);
377          Value.Addend = Addend;
378          break;
379        }
380        case SymbolRef::ST_Unknown: {
381          Value.SymbolName = TargetName.data();
382          Value.Addend = Addend;
383          break;
384        }
385        default:
386          llvm_unreachable("Unresolved symbol type!");
387          break;
388      }
389    }
390  }
391  DEBUG(dbgs() << "\t\tRel.SectionID: " << Rel.SectionID
392               << " Rel.Offset: " << Rel.Offset
393               << "\n");
394  if (Arch == Triple::arm &&
395      (RelType == ELF::R_ARM_PC24 ||
396       RelType == ELF::R_ARM_CALL ||
397       RelType == ELF::R_ARM_JUMP24)) {
398    // This is an ARM branch relocation, need to use a stub function.
399    DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
400    SectionEntry &Section = Sections[Rel.SectionID];
401    uint8_t *Target = Section.Address + Rel.Offset;
402
403    //  Look up for existing stub.
404    StubMap::const_iterator i = Stubs.find(Value);
405    if (i != Stubs.end()) {
406      resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address +
407                        i->second, RelType, 0);
408      DEBUG(dbgs() << " Stub function found\n");
409    } else {
410      // Create a new stub function.
411      DEBUG(dbgs() << " Create a new stub function\n");
412      Stubs[Value] = Section.StubOffset;
413      uint8_t *StubTargetAddr = createStubFunction(Section.Address +
414                                                   Section.StubOffset);
415      RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address,
416                         ELF::R_ARM_ABS32, Value.Addend);
417      if (Value.SymbolName)
418        addRelocationForSymbol(RE, Value.SymbolName);
419      else
420        addRelocationForSection(RE, Value.SectionID);
421
422      resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address +
423                        Section.StubOffset, RelType, 0);
424      Section.StubOffset += getMaxStubSize();
425    }
426  } else {
427    RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
428    if (Value.SymbolName)
429      addRelocationForSymbol(RE, Value.SymbolName);
430    else
431      addRelocationForSection(RE, Value.SectionID);
432  }
433}
434
435bool RuntimeDyldELF::isCompatibleFormat(const MemoryBuffer *InputBuffer) const {
436  StringRef Magic = InputBuffer->getBuffer().slice(0, ELF::EI_NIDENT);
437  return (memcmp(Magic.data(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
438}
439} // namespace llvm
440