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
1437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDIMPL_H
1537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDIMPL_H
16cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
17cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev#include "llvm/ADT/SmallVector.h"
186d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky#include "llvm/ADT/StringMap.h"
196d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky#include "llvm/ADT/Triple.h"
200c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
21a1514e24cc24b050f53a12650e047799358833a1Chandler Carruth#include "llvm/ExecutionEngine/RuntimeDyld.h"
22c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines#include "llvm/ExecutionEngine/RuntimeDyldChecker.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 <map>
31f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#include <unordered_map>
32c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines#include <system_error>
33cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
34cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshevusing namespace llvm;
350e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshevusing namespace llvm::object;
36cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
37cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshevnamespace llvm {
380e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
396d15e8717767af9a6a20c6ea456119c15c77dd00Eli Benderskyclass Twine;
406d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
41de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar#define UNIMPLEMENTED_RELOC(RelType) \
42de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  case RelType: \
43de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    return make_error<RuntimeDyldError>("Unimplemented relocation: " #RelType)
44de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
456d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky/// SectionEntry - represents a section emitted into memory by the dynamic
466d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky/// linker.
470e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshevclass SectionEntry {
486e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella  /// Name - section name.
490c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  std::string Name;
506e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella
516d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// Address - address in the linker's memory where the section resides.
526d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  uint8_t *Address;
536d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
54a2e40fbd624916c187a95ed76939ca7f02ed3e53Rafael Espindola  /// Size - section size. Doesn't include the stubs.
556d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  size_t Size;
566d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
576d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// LoadAddress - the address of the section in the target process's memory.
586d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// Used for situations in which JIT-ed code is being executed in the address
596d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// space of a separate process.  If the code executes in the same address
606d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// space where it was JIT-ed, this just equals Address.
616d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  uint64_t LoadAddress;
626d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
636d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// StubOffset - used for architectures with stub functions for far
646d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// relocations (like ARM).
656d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  uintptr_t StubOffset;
666d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
67f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// The total amount of space allocated for this section.  This includes the
68f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// section size and the maximum amount of space that the stubs can occupy.
69f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  size_t AllocationSize;
70f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
716d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// ObjAddress - address of the section in the in-memory object file.  Used
726d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// for calculating relocations in some object formats (like MachO).
736d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  uintptr_t ObjAddress;
746d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
75f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainarpublic:
766e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella  SectionEntry(StringRef name, uint8_t *address, size_t size,
77f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar               size_t allocationSize, uintptr_t objAddress)
7836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      : Name(name), Address(address), Size(size),
7937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        LoadAddress(reinterpret_cast<uintptr_t>(address)), StubOffset(size),
80f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar        AllocationSize(allocationSize), ObjAddress(objAddress) {
81f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    // AllocationSize is used only in asserts, prevent an "unused private field"
82f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    // warning:
83f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    (void)AllocationSize;
84f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  }
85f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
86f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  StringRef getName() const { return Name; }
87f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
88f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t *getAddress() const { return Address; }
89f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
90f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// \brief Return the address of this section with an offset.
91f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t *getAddressWithOffset(unsigned OffsetBytes) const {
92f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    assert(OffsetBytes <= AllocationSize && "Offset out of bounds!");
93f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    return Address + OffsetBytes;
94f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  }
95f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
96f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  size_t getSize() const { return Size; }
97f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
98f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint64_t getLoadAddress() const { return LoadAddress; }
99f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  void setLoadAddress(uint64_t LA) { LoadAddress = LA; }
100f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
101f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// \brief Return the load address of this section with an offset.
102f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint64_t getLoadAddressWithOffset(unsigned OffsetBytes) const {
103f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    assert(OffsetBytes <= AllocationSize && "Offset out of bounds!");
104f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    return LoadAddress + OffsetBytes;
105f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  }
106f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
107f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uintptr_t getStubOffset() const { return StubOffset; }
108f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
109f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  void advanceStubOffset(unsigned StubSize) {
110f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    StubOffset += StubSize;
111f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    assert(StubOffset <= AllocationSize && "Not enough space allocated!");
112f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  }
113f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
114f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uintptr_t getObjAddress() const { return ObjAddress; }
1150e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev};
1160e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
1176d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky/// RelocationEntry - used to represent relocations internally in the dynamic
1186d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky/// linker.
1190e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshevclass RelocationEntry {
1200e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshevpublic:
1216d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// SectionID - the section this relocation points to.
1226d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  unsigned SectionID;
1236d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
1246d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// Offset - offset into the section.
125ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor  uint64_t Offset;
1266d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
1276d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// RelType - relocation type.
1286d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  uint32_t RelType;
1296d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
1306d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// Addend - the relocation addend encoded in the instruction itself.  Also
1316d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  /// used to make a relocation section relative instead of symbol relative.
132ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor  int64_t Addend;
133ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor
134dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  struct SectionPair {
135dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      uint32_t SectionA;
136dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      uint32_t SectionB;
137dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  };
138dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
139ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor  /// SymOffset - Section offset of the relocation entry's symbol (used for GOT
140ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor  /// lookup).
141dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  union {
142dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    uint64_t SymOffset;
143dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    SectionPair Sections;
144dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  };
1456d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky
14687b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola  /// True if this is a PCRel relocation (MachO specific).
14787b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola  bool IsPCRel;
14887b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola
14987b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola  /// The size of this relocation (MachO specific).
15087b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola  unsigned Size;
15187b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola
1526d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend)
15336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      : SectionID(id), Offset(offset), RelType(type), Addend(addend),
15436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        SymOffset(0), IsPCRel(false), Size(0) {}
155ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor
156ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor  RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend,
157ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor                  uint64_t symoffset)
15836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      : SectionID(id), Offset(offset), RelType(type), Addend(addend),
15936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        SymOffset(symoffset), IsPCRel(false), Size(0) {}
16087b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola
16187b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola  RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend,
16287b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola                  bool IsPCRel, unsigned Size)
16336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      : SectionID(id), Offset(offset), RelType(type), Addend(addend),
16436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        SymOffset(0), IsPCRel(IsPCRel), Size(Size) {}
165dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
166dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend,
167dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                  unsigned SectionA, uint64_t SectionAOffset, unsigned SectionB,
168dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                  uint64_t SectionBOffset, bool IsPCRel, unsigned Size)
169dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      : SectionID(id), Offset(offset), RelType(type),
170dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        Addend(SectionAOffset - SectionBOffset + addend), IsPCRel(IsPCRel),
171dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        Size(Size) {
172dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    Sections.SectionA = SectionA;
173dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    Sections.SectionB = SectionB;
174dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
1750e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev};
1760e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
1770e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshevclass RelocationValueRef {
1780e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshevpublic:
17936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  unsigned SectionID;
18036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  uint64_t Offset;
18136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  int64_t Addend;
1820e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  const char *SymbolName;
183dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  RelocationValueRef() : SectionID(0), Offset(0), Addend(0),
184dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                         SymbolName(nullptr) {}
1850e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
1860e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  inline bool operator==(const RelocationValueRef &Other) const {
18774e81aae7c07b0619a77a5a0a56fdb954ce4b8fdBenjamin Kramer    return SectionID == Other.SectionID && Offset == Other.Offset &&
18874e81aae7c07b0619a77a5a0a56fdb954ce4b8fdBenjamin Kramer           Addend == Other.Addend && SymbolName == Other.SymbolName;
1890e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  }
19036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  inline bool operator<(const RelocationValueRef &Other) const {
19174e81aae7c07b0619a77a5a0a56fdb954ce4b8fdBenjamin Kramer    if (SectionID != Other.SectionID)
19274e81aae7c07b0619a77a5a0a56fdb954ce4b8fdBenjamin Kramer      return SectionID < Other.SectionID;
19374e81aae7c07b0619a77a5a0a56fdb954ce4b8fdBenjamin Kramer    if (Offset != Other.Offset)
19474e81aae7c07b0619a77a5a0a56fdb954ce4b8fdBenjamin Kramer      return Offset < Other.Offset;
19574e81aae7c07b0619a77a5a0a56fdb954ce4b8fdBenjamin Kramer    if (Addend != Other.Addend)
19674e81aae7c07b0619a77a5a0a56fdb954ce4b8fdBenjamin Kramer      return Addend < Other.Addend;
19774e81aae7c07b0619a77a5a0a56fdb954ce4b8fdBenjamin Kramer    return SymbolName < Other.SymbolName;
1980e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  }
1990e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev};
2000e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
2014c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar/// @brief Symbol info for RuntimeDyld.
2024c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainarclass SymbolTableEntry : public JITSymbolBase {
203ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinespublic:
2044c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  SymbolTableEntry()
2054c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    : JITSymbolBase(JITSymbolFlags::None), Offset(0), SectionID(0) {}
206ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
2074c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  SymbolTableEntry(unsigned SectionID, uint64_t Offset, JITSymbolFlags Flags)
2084c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    : JITSymbolBase(Flags), Offset(Offset), SectionID(SectionID) {}
209ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
210ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  unsigned getSectionID() const { return SectionID; }
211ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  uint64_t getOffset() const { return Offset; }
212ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
213c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hinesprivate:
214ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  uint64_t Offset;
2154c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  unsigned SectionID;
216ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines};
217c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
2184c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainartypedef StringMap<SymbolTableEntry> RTDyldSymbolTable;
219c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
220ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesclass RuntimeDyldImpl {
221ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  friend class RuntimeDyld::LoadedObjectInfo;
222ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  friend class RuntimeDyldCheckerImpl;
223cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshevprotected:
224f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  static const unsigned AbsoluteSymbolSection = ~0U;
225f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
226cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  // The MemoryManager to load objects into.
2270c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  RuntimeDyld::MemoryManager &MemMgr;
2280c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar
2290c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  // The symbol resolver to use for external symbols.
2300c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  RuntimeDyld::SymbolResolver &Resolver;
231cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
23237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // Attached RuntimeDyldChecker instance. Null if no instance attached.
23337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  RuntimeDyldCheckerImpl *Checker;
23437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
2356d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  // A list of all sections emitted by the dynamic linker.  These sections are
2366d15e8717767af9a6a20c6ea456119c15c77dd00Eli Bendersky  // referenced in the code by means of their index in this list - SectionID.
2370e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  typedef SmallVector<SectionEntry, 64> SectionList;
2380e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  SectionList Sections;
239cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
240528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor  typedef unsigned SID; // Type for SectionIDs
2416948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#define RTDYLD_INVALID_SECTION_ID ((RuntimeDyldImpl::SID)(-1))
242528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor
2430e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // Keep a map of sections from object file to the SectionID which
2440e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // references it.
2450e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  typedef std::map<SectionRef, unsigned> ObjSectionToIDMap;
246020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach
247ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  // A global symbol table for symbols from all loaded modules.
248ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  RTDyldSymbolTable GlobalSymbolTable;
2490e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
250f00677d74f1be5b4c7c73e1681a64c9062f4c7eeTim Northover  // Keep a map of common symbols to their info pairs
251ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  typedef std::vector<SymbolRef> CommonSymbolList;
252c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd
2530e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // For each symbol, keep a list of relocations based on it. Anytime
2540e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // its address is reassigned (the JIT re-compiled the function, e.g.),
2550e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // the relocations get re-resolved.
2560e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // The symbol (or section) the relocation is sourced from is the Key
2570e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // in the relocation list where it's stored.
2580e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  typedef SmallVector<RelocationEntry, 64> RelocationList;
2590e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // Relocations to sections already loaded. Indexed by SectionID which is the
260d9b0b025612992a0b724eeca8bdf10b1d7a5c355Benjamin Kramer  // source of the address. The target where the address will be written is
2610e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // SectionID/Offset in the relocation itself.
262f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  std::unordered_map<unsigned, RelocationList> Relocations;
26337bc5a200092f1b41da799c75da70258015e43a4Eli Bendersky
26437bc5a200092f1b41da799c75da70258015e43a4Eli Bendersky  // Relocations to external symbols that are not yet resolved.  Symbols are
26537bc5a200092f1b41da799c75da70258015e43a4Eli Bendersky  // external when they aren't found in the global symbol table of all loaded
26637bc5a200092f1b41da799c75da70258015e43a4Eli Bendersky  // modules.  This map is indexed by symbol name.
26737bc5a200092f1b41da799c75da70258015e43a4Eli Bendersky  StringMap<RelocationList> ExternalSymbolRelocations;
2680e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
26937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
2700e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  typedef std::map<RelocationValueRef, uintptr_t> StubMap;
2710e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
2720e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  Triple::ArchType Arch;
273ab950f5f334d30a7b5bfb1e009846dfb0b47f61cAndrew Kaylor  bool IsTargetLittleEndian;
2746948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  bool IsMipsO32ABI;
2756948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  bool IsMipsN64ABI;
2760e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
27736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // True if all sections should be passed to the memory manager, false if only
27836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // sections containing relocations should be. Defaults to 'false'.
27936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  bool ProcessAllSections;
28036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
2816169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  // This mutex prevents simultaneously loading objects from two different
2826169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  // threads.  This keeps us from having to protect individual data structures
2836169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  // and guarantees that section allocation requests to the memory manager
2846169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  // won't be interleaved between modules.  It is also used in mapSectionAddress
2856169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  // and resolveRelocations to protect write access to internal data structures.
2866169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  //
2876169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  // loadObject may be called on the same thread during the handling of of
2886169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  // processRelocations, and that's OK.  The handling of the relocation lists
2896169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  // is written in such a way as to work correctly if new elements are added to
2906169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  // the end of the list while the list is being processed.
2916169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  sys::Mutex lock;
2926169453ba37ac353655f2475f336e66f31276752Andrew Kaylor
29372be32c6332ff9dd38b989d5a0dd80f40996dd10Andrew Kaylor  virtual unsigned getMaxStubSize() = 0;
29472be32c6332ff9dd38b989d5a0dd80f40996dd10Andrew Kaylor  virtual unsigned getStubAlignment() = 0;
2956fc2ad62e23673ef829c9c4bbf62743d30928a5bRichard Sandiford
296cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  bool HasError;
297cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  std::string ErrorStr;
298cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
29937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  uint64_t getSectionLoadAddress(unsigned SectionID) const {
300f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    return Sections[SectionID].getLoadAddress();
30135ed842773da41779d57d3ed23f440202d0be198Jim Grosbach  }
30235ed842773da41779d57d3ed23f440202d0be198Jim Grosbach
30337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  uint8_t *getSectionAddress(unsigned SectionID) const {
304f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    return Sections[SectionID].getAddress();
30561425c0a7f4e3608a85f7bbf254cd052a15b7446Jim Grosbach  }
306cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
3076e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella  void writeInt16BE(uint8_t *Addr, uint16_t Value) {
308ab950f5f334d30a7b5bfb1e009846dfb0b47f61cAndrew Kaylor    if (IsTargetLittleEndian)
309c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines      sys::swapByteOrder(Value);
31036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    *Addr       = (Value >> 8) & 0xFF;
31136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    *(Addr + 1) = Value & 0xFF;
3126e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella  }
3136e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella
3146e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella  void writeInt32BE(uint8_t *Addr, uint32_t Value) {
315ab950f5f334d30a7b5bfb1e009846dfb0b47f61cAndrew Kaylor    if (IsTargetLittleEndian)
316c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines      sys::swapByteOrder(Value);
31736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    *Addr       = (Value >> 24) & 0xFF;
31836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    *(Addr + 1) = (Value >> 16) & 0xFF;
31936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    *(Addr + 2) = (Value >> 8) & 0xFF;
32036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    *(Addr + 3) = Value & 0xFF;
3216e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella  }
3226e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella
3236e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella  void writeInt64BE(uint8_t *Addr, uint64_t Value) {
324ab950f5f334d30a7b5bfb1e009846dfb0b47f61cAndrew Kaylor    if (IsTargetLittleEndian)
325c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines      sys::swapByteOrder(Value);
32636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    *Addr       = (Value >> 56) & 0xFF;
32736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    *(Addr + 1) = (Value >> 48) & 0xFF;
32836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    *(Addr + 2) = (Value >> 40) & 0xFF;
32936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    *(Addr + 3) = (Value >> 32) & 0xFF;
33036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    *(Addr + 4) = (Value >> 24) & 0xFF;
33136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    *(Addr + 5) = (Value >> 16) & 0xFF;
33236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    *(Addr + 6) = (Value >> 8) & 0xFF;
33336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    *(Addr + 7) = Value & 0xFF;
3346e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella  }
3356e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella
3366948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  virtual void setMipsABI(const ObjectFile &Obj) {
3376948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    IsMipsO32ABI = false;
3386948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    IsMipsN64ABI = false;
3396948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  }
3406948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
34137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// Endian-aware read Read the least significant Size bytes from Src.
34237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const;
34337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
34437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// Endian-aware write. Write the least significant Size bytes from Value to
34537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// Dst.
34637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const;
34737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
348c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  /// \brief Given the common symbols discovered in the object file, emit a
349c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  /// new section for them and update the symbol mappings in the object and
350c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  /// symbol table.
351de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  Error emitCommonSymbols(const ObjectFile &Obj,
352de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                          CommonSymbolList &CommonSymbols);
353c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd
3540e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \brief Emits section data from the object file to the MemoryManager.
3550e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \param IsCode if it's true then allocateCodeSection() will be
3565fe019835c269ccbfe185276269bc53b3f9a7a86Eli Bendersky  ///        used for emits, else allocateDataSection() will be used.
3570e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \return SectionID.
358de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  Expected<unsigned> emitSection(const ObjectFile &Obj,
359de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                                 const SectionRef &Section,
360de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                                 bool IsCode);
3610e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
3620e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \brief Find Section in LocalSections. If the secton is not found - emit
3630e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  ///        it and store in LocalSections.
3640e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \param IsCode if it's true then allocateCodeSection() will be
3650e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  ///        used for emmits, else allocateDataSection() will be used.
3660e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \return SectionID.
367de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  Expected<unsigned> findOrEmitSection(const ObjectFile &Obj,
368de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                                       const SectionRef &Section, bool IsCode,
369de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                                       ObjSectionToIDMap &LocalSections);
3700e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
371c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  // \brief Add a relocation entry that uses the given section.
372c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID);
373c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky
374c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  // \brief Add a relocation entry that uses the given symbol.  This symbol may
375c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  // be found in the global symbol table, or it may be external.
376c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName);
3770e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
3780e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \brief Emits long jump instruction to Addr.
3790e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \return Pointer to the memory area for emitting target address.
38037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  uint8_t *createStubFunction(uint8_t *Addr, unsigned AbiVariant = 0);
3810e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
3820e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \brief Resolves relocations from Relocs list with address from Value.
3830e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  void resolveRelocationList(const RelocationList &Relocs, uint64_t Value);
3840e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
3850e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \brief A object file specific relocation resolver
3861fbeecb0394a2f203fd48b699718c4f90a8870b7Rafael Espindola  /// \param RE The relocation to be resolved
3870e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  /// \param Value Target symbol address to apply the relocation action
38887b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola  virtual void resolveRelocation(const RelocationEntry &RE, uint64_t Value) = 0;
3890e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
39036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// \brief Parses one or more object file relocations (some object files use
39136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ///        relocation pairs) and stores it to Relocations or SymbolRelocations
39236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ///        (this depends on the object file type).
39336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// \return Iterator to the next relocation that needs to be parsed.
394de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  virtual Expected<relocation_iterator>
39536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  processRelocationRef(unsigned SectionID, relocation_iterator RelI,
396ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                       const ObjectFile &Obj, ObjSectionToIDMap &ObjSectionToID,
397ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                       StubMap &Stubs) = 0;
3980e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
39937bc5a200092f1b41da799c75da70258015e43a4Eli Bendersky  /// \brief Resolve relocations to external symbols.
40037bc5a200092f1b41da799c75da70258015e43a4Eli Bendersky  void resolveExternalSymbols();
401ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor
40236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // \brief Compute an upper bound of the memory that is required to load all
40336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // sections
404de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  Error computeTotalAllocSize(const ObjectFile &Obj,
405de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                              uint64_t &CodeSize, uint32_t &CodeAlign,
406de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                              uint64_t &RODataSize, uint32_t &RODataAlign,
407de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                              uint64_t &RWDataSize, uint32_t &RWDataAlign);
40836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
40936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // \brief Compute the stub buffer size required for a section
410ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  unsigned computeSectionStubBufSize(const ObjectFile &Obj,
41136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                     const SectionRef &Section);
41236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
413ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  // \brief Implementation of the generic part of the loadObject algorithm.
414de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  Expected<ObjSectionToIDMap> loadObjectImpl(const object::ObjectFile &Obj);
415f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
416f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  // \brief Return true if the relocation R may require allocating a stub.
417f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  virtual bool relocationNeedsStub(const RelocationRef &R) const {
418f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    return true;    // Conservative answer
419f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  }
420ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
421cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshevpublic:
4220c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  RuntimeDyldImpl(RuntimeDyld::MemoryManager &MemMgr,
4230c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar                  RuntimeDyld::SymbolResolver &Resolver)
4240c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    : MemMgr(MemMgr), Resolver(Resolver), Checker(nullptr),
4250c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar      ProcessAllSections(false), HasError(false) {
426c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  }
427cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
428cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  virtual ~RuntimeDyldImpl();
429cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
43036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void setProcessAllSections(bool ProcessAllSections) {
43136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    this->ProcessAllSections = ProcessAllSections;
43236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
43336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
43437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  void setRuntimeDyldChecker(RuntimeDyldCheckerImpl *Checker) {
43537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    this->Checker = Checker;
43637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
43737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
438ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  virtual std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
439ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  loadObject(const object::ObjectFile &Obj) = 0;
440cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
4414c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  uint8_t* getSymbolLocalAddress(StringRef Name) const {
442cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev    // FIXME: Just look up as a function for now. Overly simple of course.
443cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev    // Work in progress.
444ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    RTDyldSymbolTable::const_iterator pos = GlobalSymbolTable.find(Name);
4454805bf59b9e41a95336c066ec58194ff6801694aYaron Keren    if (pos == GlobalSymbolTable.end())
446dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      return nullptr;
447ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    const auto &SymInfo = pos->second;
448f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    // Absolute symbols do not have a local address.
449f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (SymInfo.getSectionID() == AbsoluteSymbolSection)
450f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar      return nullptr;
451ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return getSectionAddress(SymInfo.getSectionID()) + SymInfo.getOffset();
452cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  }
453cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
4544c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  RuntimeDyld::SymbolInfo getSymbol(StringRef Name) const {
45535ed842773da41779d57d3ed23f440202d0be198Jim Grosbach    // FIXME: Just look up as a function for now. Overly simple of course.
45635ed842773da41779d57d3ed23f440202d0be198Jim Grosbach    // Work in progress.
457ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    RTDyldSymbolTable::const_iterator pos = GlobalSymbolTable.find(Name);
4584805bf59b9e41a95336c066ec58194ff6801694aYaron Keren    if (pos == GlobalSymbolTable.end())
4594c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar      return nullptr;
4604c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    const auto &SymEntry = pos->second;
461f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    uint64_t SectionAddr = 0;
462f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (SymEntry.getSectionID() != AbsoluteSymbolSection)
463f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar      SectionAddr = getSectionLoadAddress(SymEntry.getSectionID());
464f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    uint64_t TargetAddr = SectionAddr + SymEntry.getOffset();
4654c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    return RuntimeDyld::SymbolInfo(TargetAddr, SymEntry.getFlags());
46635ed842773da41779d57d3ed23f440202d0be198Jim Grosbach  }
46735ed842773da41779d57d3ed23f440202d0be198Jim Grosbach
4680e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  void resolveRelocations();
469cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
4700e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
471cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
472e940c1bb6c976539f07d6f440aeaacf7c25e1ddcJim Grosbach  void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress);
473020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach
474cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  // Is the linker in an error state?
475cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  bool hasError() { return HasError; }
476cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
477cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  // Mark the error condition as handled and continue.
478cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  void clearError() { HasError = false; }
479cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
480cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  // Get the error message.
481cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  StringRef getErrorString() { return ErrorStr; }
482cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
483ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  virtual bool isCompatibleFile(const ObjectFile &Obj) const = 0;
484a2e40fbd624916c187a95ed76939ca7f02ed3e53Rafael Espindola
485528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor  virtual void registerEHFrames();
486ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor
48743507d026bef31100cb0c35614bcf419029a265bAndrew Kaylor  virtual void deregisterEHFrames();
48843507d026bef31100cb0c35614bcf419029a265bAndrew Kaylor
489de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  virtual Error finalizeLoad(const ObjectFile &ObjImg,
490de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                             ObjSectionToIDMap &SectionMap) {
491de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    return Error::success();
492de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  }
493cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev};
494cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
495cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev} // end namespace llvm
496cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
497cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev#endif
498