RuntimeDyldImpl.h revision 6169453ba37ac353655f2475f336e66f31276752
1e0934bee3a4f40731169bc42b15a39ce39978175Jim Grosbach//===-- RuntimeDyldImpl.h - Run-time dynamic linker for MC-JIT --*- C++ -*-===//
2cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev//
3cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev//                     The LLVM Compiler Infrastructure
4cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev//
5cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev// This file is distributed under the University of Illinois Open Source
6cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev// License. See LICENSE.TXT for details.
7cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev//
8cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev//===----------------------------------------------------------------------===//
9cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev//
10cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev// Interface for the implementations of runtime dynamic linker facilities.
11cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev//
12cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev//===----------------------------------------------------------------------===//
13cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
14cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev#ifndef LLVM_RUNTIME_DYLD_IMPL_H
15cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev#define LLVM_RUNTIME_DYLD_IMPL_H
16cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
17020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach#include "llvm/ADT/DenseMap.h"
18cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev#include "llvm/ADT/SmallVector.h"
196d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky#include "llvm/ADT/StringMap.h"
206d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky#include "llvm/ADT/Triple.h"
21a1514e24cc24b050f53a12650e047799358833a1Chandler Carruth#include "llvm/ExecutionEngine/ObjectImage.h"
22a1514e24cc24b050f53a12650e047799358833a1Chandler Carruth#include "llvm/ExecutionEngine/RuntimeDyld.h"
236d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky#include "llvm/Object/ObjectFile.h"
24cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev#include "llvm/Support/Debug.h"
25cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev#include "llvm/Support/ErrorHandling.h"
260e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev#include "llvm/Support/Format.h"
276e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella#include "llvm/Support/Host.h"
286169453ba37ac353655f2475f336e66f31276752Andrew Kaylor#include "llvm/Support/Mutex.h"
296e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella#include "llvm/Support/SwapByteOrder.h"
306d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky#include "llvm/Support/raw_ostream.h"
316d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky#include "llvm/Support/system_error.h"
326d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky#include <map>
33cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
34cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshevusing namespace llvm;
350e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshevusing namespace llvm::object;
36cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
37cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshevnamespace llvm {
380e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
393f23cef24fc9200def464bd4bce820678b5715deAndrew Kaylorclass ObjectBuffer;
406d15e8717767af9a6a20c6ea456119c15c77dd00Eli Benderskyclass Twine;
416d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
426d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
436d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky/// SectionEntry - represents a section emitted into memory by the dynamic
446d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky/// linker.
450e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshevclass SectionEntry {
460e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshevpublic:
476e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella  /// Name - section name.
486e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella  StringRef Name;
496e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella
506d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// Address - address in the linker's memory where the section resides.
516d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  uint8_t *Address;
526d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
53a2e40fbd624916c187a95ed76939ca7f02ed3e53Rafael Espindola  /// Size - section size. Doesn't include the stubs.
546d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  size_t Size;
556d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
566d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// LoadAddress - the address of the section in the target process's memory.
576d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// Used for situations in which JIT-ed code is being executed in the address
586d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// space of a separate process.  If the code executes in the same address
596d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// space where it was JIT-ed, this just equals Address.
606d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  uint64_t LoadAddress;
616d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
626d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// StubOffset - used for architectures with stub functions for far
636d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// relocations (like ARM).
646d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  uintptr_t StubOffset;
656d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
666d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// ObjAddress - address of the section in the in-memory object file.  Used
676d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// for calculating relocations in some object formats (like MachO).
686d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  uintptr_t ObjAddress;
696d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
706e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella  SectionEntry(StringRef name, uint8_t *address, size_t size,
71a2e40fbd624916c187a95ed76939ca7f02ed3e53Rafael Espindola               uintptr_t objAddress)
726e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    : Name(name), Address(address), Size(size), LoadAddress((uintptr_t)address),
73a2e40fbd624916c187a95ed76939ca7f02ed3e53Rafael Espindola      StubOffset(size), ObjAddress(objAddress) {}
740e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev};
750e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
766d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky/// RelocationEntry - used to represent relocations internally in the dynamic
776d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky/// linker.
780e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshevclass RelocationEntry {
790e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshevpublic:
806d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// SectionID - the section this relocation points to.
816d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  unsigned SectionID;
826d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
836d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// Offset - offset into the section.
84ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor  uint64_t Offset;
856d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
866d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// RelType - relocation type.
876d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  uint32_t RelType;
886d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
896d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// Addend - the relocation addend encoded in the instruction itself.  Also
906d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// used to make a relocation section relative instead of symbol relative.
91ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor  int64_t Addend;
92ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor
93ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor  /// SymOffset - Section offset of the relocation entry's symbol (used for GOT
94ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor  /// lookup).
95ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor  uint64_t SymOffset;
966d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
9787b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola  /// True if this is a PCRel relocation (MachO specific).
9887b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola  bool IsPCRel;
9987b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola
10087b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola  /// The size of this relocation (MachO specific).
10187b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola  unsigned Size;
10287b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola
1036d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend)
10487b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola    : SectionID(id), Offset(offset), RelType(type), Addend(addend),
105ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor      SymOffset(0), IsPCRel(false), Size(0) {}
106ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor
107ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor  RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend,
108ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor                  uint64_t symoffset)
109ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor    : SectionID(id), Offset(offset), RelType(type), Addend(addend),
110ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor      SymOffset(symoffset), IsPCRel(false), Size(0) {}
11187b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola
11287b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola  RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend,
11387b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola                  bool IsPCRel, unsigned Size)
11487b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola    : SectionID(id), Offset(offset), RelType(type), Addend(addend),
115ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor      SymOffset(0), IsPCRel(IsPCRel), Size(Size) {}
1160e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev};
1170e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
1180e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshevclass RelocationValueRef {
1190e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshevpublic:
1200e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  unsigned  SectionID;
121ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor  uint64_t  Offset;
122ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor  int64_t   Addend;
1230e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  const char *SymbolName;
124ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor  RelocationValueRef(): SectionID(0), Offset(0), Addend(0), SymbolName(0) {}
1250e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
1260e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  inline bool operator==(const RelocationValueRef &Other) const {
12774e81aae7c07b0619a77a5a0a56fdb954ce4b8fdBenjamin Kramer    return SectionID == Other.SectionID && Offset == Other.Offset &&
12874e81aae7c07b0619a77a5a0a56fdb954ce4b8fdBenjamin Kramer           Addend == Other.Addend && SymbolName == Other.SymbolName;
1290e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  }
1300e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  inline bool operator <(const RelocationValueRef &Other) const {
13174e81aae7c07b0619a77a5a0a56fdb954ce4b8fdBenjamin Kramer    if (SectionID != Other.SectionID)
13274e81aae7c07b0619a77a5a0a56fdb954ce4b8fdBenjamin Kramer      return SectionID < Other.SectionID;
13374e81aae7c07b0619a77a5a0a56fdb954ce4b8fdBenjamin Kramer    if (Offset != Other.Offset)
13474e81aae7c07b0619a77a5a0a56fdb954ce4b8fdBenjamin Kramer      return Offset < Other.Offset;
13574e81aae7c07b0619a77a5a0a56fdb954ce4b8fdBenjamin Kramer    if (Addend != Other.Addend)
13674e81aae7c07b0619a77a5a0a56fdb954ce4b8fdBenjamin Kramer      return Addend < Other.Addend;
13774e81aae7c07b0619a77a5a0a56fdb954ce4b8fdBenjamin Kramer    return SymbolName < Other.SymbolName;
1380e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  }
1390e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev};
1400e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
141cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshevclass RuntimeDyldImpl {
142cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshevprotected:
143cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  // The MemoryManager to load objects into.
144cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  RTDyldMemoryManager *MemMgr;
145cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
1466d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  // A list of all sections emitted by the dynamic linker.  These sections are
1476d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  // referenced in the code by means of their index in this list - SectionID.
1480e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  typedef SmallVector<SectionEntry, 64> SectionList;
1490e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  SectionList Sections;
150cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
151528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor  typedef unsigned SID; // Type for SectionIDs
152528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor  #define RTDYLD_INVALID_SECTION_ID ((SID)(-1))
153528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor
1540e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // Keep a map of sections from object file to the SectionID which
1550e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // references it.
1560e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  typedef std::map<SectionRef, unsigned> ObjSectionToIDMap;
157020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach
158d98c9e918c9750d965c1efe9efcc9e13feacbe13Eli Bendersky  // A global symbol table for symbols from all loaded modules.  Maps the
159d98c9e918c9750d965c1efe9efcc9e13feacbe13Eli Bendersky  // symbol name to a (SectionID, offset in section) pair.
1600e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  typedef std::pair<unsigned, uintptr_t> SymbolLoc;
161d98c9e918c9750d965c1efe9efcc9e13feacbe13Eli Bendersky  typedef StringMap<SymbolLoc> SymbolTableMap;
162d98c9e918c9750d965c1efe9efcc9e13feacbe13Eli Bendersky  SymbolTableMap GlobalSymbolTable;
1630e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
164f00677d74f1be5b4c7c73e1681a64c9062f4c7eeTim Northover  // Pair representing the size and alignment requirement for a common symbol.
165f00677d74f1be5b4c7c73e1681a64c9062f4c7eeTim Northover  typedef std::pair<unsigned, unsigned> CommonSymbolInfo;
166f00677d74f1be5b4c7c73e1681a64c9062f4c7eeTim Northover  // Keep a map of common symbols to their info pairs
167f00677d74f1be5b4c7c73e1681a64c9062f4c7eeTim Northover  typedef std::map<SymbolRef, CommonSymbolInfo> CommonSymbolMap;
168c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd
1690e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // For each symbol, keep a list of relocations based on it. Anytime
1700e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // its address is reassigned (the JIT re-compiled the function, e.g.),
1710e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // the relocations get re-resolved.
1720e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // The symbol (or section) the relocation is sourced from is the Key
1730e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // in the relocation list where it's stored.
1740e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  typedef SmallVector<RelocationEntry, 64> RelocationList;
1750e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // Relocations to sections already loaded. Indexed by SectionID which is the
176d9b0b025612992a0b724eeca8bdf10b1d7a5c355Benjamin Kramer  // source of the address. The target where the address will be written is
1770e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // SectionID/Offset in the relocation itself.
1780e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  DenseMap<unsigned, RelocationList> Relocations;
17937bc5a200092f1b41da799c75da70258015e43a4Eli Bendersky
18037bc5a200092f1b41da799c75da70258015e43a4Eli Bendersky  // Relocations to external symbols that are not yet resolved.  Symbols are
18137bc5a200092f1b41da799c75da70258015e43a4Eli Bendersky  // external when they aren't found in the global symbol table of all loaded
18237bc5a200092f1b41da799c75da70258015e43a4Eli Bendersky  // modules.  This map is indexed by symbol name.
18337bc5a200092f1b41da799c75da70258015e43a4Eli Bendersky  StringMap<RelocationList> ExternalSymbolRelocations;
1840e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
1850e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  typedef std::map<RelocationValueRef, uintptr_t> StubMap;
1860e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
1870e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  Triple::ArchType Arch;
188ab950f5f334d30a7b5bfb1e009846dfb0b47f61cAndrew Kaylor  bool IsTargetLittleEndian;
1890e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
1906169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  // This mutex prevents simultaneously loading objects from two different
1916169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  // threads.  This keeps us from having to protect individual data structures
1926169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  // and guarantees that section allocation requests to the memory manager
1936169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  // won't be interleaved between modules.  It is also used in mapSectionAddress
1946169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  // and resolveRelocations to protect write access to internal data structures.
1956169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  //
1966169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  // loadObject may be called on the same thread during the handling of of
1976169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  // processRelocations, and that's OK.  The handling of the relocation lists
1986169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  // is written in such a way as to work correctly if new elements are added to
1996169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  // the end of the list while the list is being processed.
2006169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  sys::Mutex lock;
2016169453ba37ac353655f2475f336e66f31276752Andrew Kaylor
20272be32c6332ff9dd38b989d5a0dd80f40996dd10Andrew Kaylor  virtual unsigned getMaxStubSize() = 0;
20372be32c6332ff9dd38b989d5a0dd80f40996dd10Andrew Kaylor  virtual unsigned getStubAlignment() = 0;
2046fc2ad62e23673ef829c9c4bbf62743d30928a5bRichard Sandiford
205cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  bool HasError;
206cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  std::string ErrorStr;
207cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
208cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  // Set the error state and record an error string.
209cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  bool Error(const Twine &Msg) {
210cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev    ErrorStr = Msg.str();
211cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev    HasError = true;
212cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev    return true;
213cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  }
214cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
21535ed842773da41779d57d3ed23f440202d0be198Jim Grosbach  uint64_t getSectionLoadAddress(unsigned SectionID) {
21635ed842773da41779d57d3ed23f440202d0be198Jim Grosbach    return Sections[SectionID].LoadAddress;
21735ed842773da41779d57d3ed23f440202d0be198Jim Grosbach  }
21835ed842773da41779d57d3ed23f440202d0be198Jim Grosbach
21961425c0a7f4e3608a85f7bbf254cd052a15b7446Jim Grosbach  uint8_t *getSectionAddress(unsigned SectionID) {
2200e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev    return (uint8_t*)Sections[SectionID].Address;
22161425c0a7f4e3608a85f7bbf254cd052a15b7446Jim Grosbach  }
222cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
2236e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella  void writeInt16BE(uint8_t *Addr, uint16_t Value) {
224ab950f5f334d30a7b5bfb1e009846dfb0b47f61cAndrew Kaylor    if (IsTargetLittleEndian)
2256e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella      Value = sys::SwapByteOrder(Value);
2266e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    *Addr     = (Value >> 8) & 0xFF;
2276e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    *(Addr+1) = Value & 0xFF;
2286e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella  }
2296e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella
2306e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella  void writeInt32BE(uint8_t *Addr, uint32_t Value) {
231ab950f5f334d30a7b5bfb1e009846dfb0b47f61cAndrew Kaylor    if (IsTargetLittleEndian)
2326e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella      Value = sys::SwapByteOrder(Value);
2336e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    *Addr     = (Value >> 24) & 0xFF;
2346e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    *(Addr+1) = (Value >> 16) & 0xFF;
2356e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    *(Addr+2) = (Value >> 8) & 0xFF;
2366e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    *(Addr+3) = Value & 0xFF;
2376e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella  }
2386e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella
2396e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella  void writeInt64BE(uint8_t *Addr, uint64_t Value) {
240ab950f5f334d30a7b5bfb1e009846dfb0b47f61cAndrew Kaylor    if (IsTargetLittleEndian)
2416e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella      Value = sys::SwapByteOrder(Value);
2426e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    *Addr     = (Value >> 56) & 0xFF;
2436e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    *(Addr+1) = (Value >> 48) & 0xFF;
2446e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    *(Addr+2) = (Value >> 40) & 0xFF;
2456e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    *(Addr+3) = (Value >> 32) & 0xFF;
2466e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    *(Addr+4) = (Value >> 24) & 0xFF;
2476e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    *(Addr+5) = (Value >> 16) & 0xFF;
2486e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    *(Addr+6) = (Value >> 8) & 0xFF;
2496e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    *(Addr+7) = Value & 0xFF;
2506e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella  }
2516e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella
252c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  /// \brief Given the common symbols discovered in the object file, emit a
253c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  /// new section for them and update the symbol mappings in the object and
254c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  /// symbol table.
255c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  void emitCommonSymbols(ObjectImage &Obj,
256c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky                         const CommonSymbolMap &CommonSymbols,
257c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky                         uint64_t TotalSize,
258c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky                         SymbolTableMap &SymbolTable);
259c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd
2600e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \brief Emits section data from the object file to the MemoryManager.
2610e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \param IsCode if it's true then allocateCodeSection() will be
2625fe019835c269ccbfe185276269bc53b3f9a7a86Eli Bendersky  ///        used for emits, else allocateDataSection() will be used.
2630e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \return SectionID.
264689ff9c00f4f3dcf3491778bcdbda79e19e2285dPreston Gurd  unsigned emitSection(ObjectImage &Obj,
265689ff9c00f4f3dcf3491778bcdbda79e19e2285dPreston Gurd                       const SectionRef &Section,
266689ff9c00f4f3dcf3491778bcdbda79e19e2285dPreston Gurd                       bool IsCode);
2670e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
2680e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \brief Find Section in LocalSections. If the secton is not found - emit
2690e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  ///        it and store in LocalSections.
2700e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \param IsCode if it's true then allocateCodeSection() will be
2710e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  ///        used for emmits, else allocateDataSection() will be used.
2720e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \return SectionID.
273689ff9c00f4f3dcf3491778bcdbda79e19e2285dPreston Gurd  unsigned findOrEmitSection(ObjectImage &Obj,
274689ff9c00f4f3dcf3491778bcdbda79e19e2285dPreston Gurd                             const SectionRef &Section,
275689ff9c00f4f3dcf3491778bcdbda79e19e2285dPreston Gurd                             bool IsCode,
2760e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev                             ObjSectionToIDMap &LocalSections);
2770e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
278c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  // \brief Add a relocation entry that uses the given section.
279c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID);
280c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky
281c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  // \brief Add a relocation entry that uses the given symbol.  This symbol may
282c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  // be found in the global symbol table, or it may be external.
283c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName);
2840e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
2850e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \brief Emits long jump instruction to Addr.
2860e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \return Pointer to the memory area for emitting target address.
2870e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  uint8_t* createStubFunction(uint8_t *Addr);
2880e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
2890e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \brief Resolves relocations from Relocs list with address from Value.
2900e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  void resolveRelocationList(const RelocationList &Relocs, uint64_t Value);
2910e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
2920e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \brief A object file specific relocation resolver
2931fbeecb0394a2f203fd48b699718c4f90a8870b7Rafael Espindola  /// \param RE The relocation to be resolved
2940e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \param Value Target symbol address to apply the relocation action
29587b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola  virtual void resolveRelocation(const RelocationEntry &RE, uint64_t Value) = 0;
2960e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
2975fe019835c269ccbfe185276269bc53b3f9a7a86Eli Bendersky  /// \brief Parses the object file relocation and stores it to Relocations
2985fe019835c269ccbfe185276269bc53b3f9a7a86Eli Bendersky  ///        or SymbolRelocations (this depends on the object file type).
299efa91f6475f6e96552986104ab4857db46185a2aRafael Espindola  virtual void processRelocationRef(unsigned SectionID,
300ca0e73610056110e9a175c14dd82d6d616fd830fRafael Espindola                                    RelocationRef RelI,
301689ff9c00f4f3dcf3491778bcdbda79e19e2285dPreston Gurd                                    ObjectImage &Obj,
3020e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev                                    ObjSectionToIDMap &ObjSectionToID,
303d98c9e918c9750d965c1efe9efcc9e13feacbe13Eli Bendersky                                    const SymbolTableMap &Symbols,
3045fe019835c269ccbfe185276269bc53b3f9a7a86Eli Bendersky                                    StubMap &Stubs) = 0;
3050e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
30637bc5a200092f1b41da799c75da70258015e43a4Eli Bendersky  /// \brief Resolve relocations to external symbols.
30737bc5a200092f1b41da799c75da70258015e43a4Eli Bendersky  void resolveExternalSymbols();
308ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor
309ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor  /// \brief Update GOT entries for external symbols.
310ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor  // The base class does nothing.  ELF overrides this.
311ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor  virtual void updateGOTEntries(StringRef Name, uint64_t Addr) {}
312ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor
3133f23cef24fc9200def464bd4bce820678b5715deAndrew Kaylor  virtual ObjectImage *createObjectImage(ObjectBuffer *InputBuffer);
314cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshevpublic:
315cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {}
316cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
317cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  virtual ~RuntimeDyldImpl();
318cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
3193f23cef24fc9200def464bd4bce820678b5715deAndrew Kaylor  ObjectImage *loadObject(ObjectBuffer *InputBuffer);
320cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
321cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  void *getSymbolAddress(StringRef Name) {
322cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev    // FIXME: Just look up as a function for now. Overly simple of course.
323cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev    // Work in progress.
3244805bf59b9e41a95336c066ec58194ff6801694aYaron Keren    SymbolTableMap::const_iterator pos = GlobalSymbolTable.find(Name);
3254805bf59b9e41a95336c066ec58194ff6801694aYaron Keren    if (pos == GlobalSymbolTable.end())
32661425c0a7f4e3608a85f7bbf254cd052a15b7446Jim Grosbach      return 0;
3274805bf59b9e41a95336c066ec58194ff6801694aYaron Keren    SymbolLoc Loc = pos->second;
32861425c0a7f4e3608a85f7bbf254cd052a15b7446Jim Grosbach    return getSectionAddress(Loc.first) + Loc.second;
329cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  }
330cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
33135ed842773da41779d57d3ed23f440202d0be198Jim Grosbach  uint64_t getSymbolLoadAddress(StringRef Name) {
33235ed842773da41779d57d3ed23f440202d0be198Jim Grosbach    // FIXME: Just look up as a function for now. Overly simple of course.
33335ed842773da41779d57d3ed23f440202d0be198Jim Grosbach    // Work in progress.
3344805bf59b9e41a95336c066ec58194ff6801694aYaron Keren    SymbolTableMap::const_iterator pos = GlobalSymbolTable.find(Name);
3354805bf59b9e41a95336c066ec58194ff6801694aYaron Keren    if (pos == GlobalSymbolTable.end())
33635ed842773da41779d57d3ed23f440202d0be198Jim Grosbach      return 0;
3374805bf59b9e41a95336c066ec58194ff6801694aYaron Keren    SymbolLoc Loc = pos->second;
33835ed842773da41779d57d3ed23f440202d0be198Jim Grosbach    return getSectionLoadAddress(Loc.first) + Loc.second;
33935ed842773da41779d57d3ed23f440202d0be198Jim Grosbach  }
34035ed842773da41779d57d3ed23f440202d0be198Jim Grosbach
3410e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  void resolveRelocations();
342cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
3430e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
344cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
345e940c1bb6c976539f07d6f440aeaacf7c25e1ddcJim Grosbach  void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress);
346020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach
347cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  // Is the linker in an error state?
348cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  bool hasError() { return HasError; }
349cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
350cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  // Mark the error condition as handled and continue.
351cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  void clearError() { HasError = false; }
352cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
353cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  // Get the error message.
354cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  StringRef getErrorString() { return ErrorStr; }
355cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
3563f23cef24fc9200def464bd4bce820678b5715deAndrew Kaylor  virtual bool isCompatibleFormat(const ObjectBuffer *Buffer) const = 0;
357a2e40fbd624916c187a95ed76939ca7f02ed3e53Rafael Espindola
358528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor  virtual void registerEHFrames();
359ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor
36043507d026bef31100cb0c35614bcf419029a265bAndrew Kaylor  virtual void deregisterEHFrames();
36143507d026bef31100cb0c35614bcf419029a265bAndrew Kaylor
362528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor  virtual void finalizeLoad(ObjSectionToIDMap &SectionMap) {}
363cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev};
364cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
365cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev} // end namespace llvm
366cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
367cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
368cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev#endif
369