LLVM  8.0.1
RuntimeDyldMachO.h
Go to the documentation of this file.
1 //===-- RuntimeDyldMachO.h - Run-time dynamic linker for MC-JIT ---*- C++ -*-=//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // MachO support for MC-JIT runtime dynamic linker.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H
15 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H
16 
17 #include "RuntimeDyldImpl.h"
18 #include "llvm/Object/MachO.h"
19 #include "llvm/Support/Format.h"
20 
21 #define DEBUG_TYPE "dyld"
22 
23 using namespace llvm;
24 using namespace llvm::object;
25 
26 namespace llvm {
28 protected:
30  unsigned SectionID;
31  uint64_t Offset;
32  };
33 
36  : EHFrameSID(RTDYLD_INVALID_SECTION_ID),
38  ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {}
39 
40  EHFrameRelatedSections(SID EH, SID T, SID Ex)
41  : EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {}
43  SID TextSID;
45  };
46 
47  // When a module is loaded we save the SectionID of the EH frame section
48  // in a table until we receive a request to register all unregistered
49  // EH frame sections with the memory manager.
51 
54  : RuntimeDyldImpl(MemMgr, Resolver) {}
55 
56  /// This convenience method uses memcpy to extract a contiguous addend (the
57  /// addend size and offset are taken from the corresponding fields of the RE).
58  int64_t memcpyAddend(const RelocationEntry &RE) const;
59 
60  /// Given a relocation_iterator for a non-scattered relocation, construct a
61  /// RelocationEntry and fill in the common fields. The 'Addend' field is *not*
62  /// filled in, since immediate encodings are highly target/opcode specific.
63  /// For targets/opcodes with simple, contiguous immediates (e.g. X86) the
64  /// memcpyAddend method can be used to read the immediate.
65  RelocationEntry getRelocationEntry(unsigned SectionID,
66  const ObjectFile &BaseTObj,
67  const relocation_iterator &RI) const {
68  const MachOObjectFile &Obj =
69  static_cast<const MachOObjectFile &>(BaseTObj);
71  Obj.getRelocation(RI->getRawDataRefImpl());
72 
73  bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
74  unsigned Size = Obj.getAnyRelocationLength(RelInfo);
75  uint64_t Offset = RI->getOffset();
77  static_cast<MachO::RelocationInfoType>(Obj.getAnyRelocationType(RelInfo));
78 
79  return RelocationEntry(SectionID, Offset, RelType, 0, IsPCRel, Size);
80  }
81 
82  /// Process a scattered vanilla relocation.
84  processScatteredVANILLA(unsigned SectionID, relocation_iterator RelI,
85  const ObjectFile &BaseObjT,
87  bool TargetIsLocalThumbFunc = false);
88 
89  /// Construct a RelocationValueRef representing the relocation target.
90  /// For Symbols in known sections, this will return a RelocationValueRef
91  /// representing a (SectionID, Offset) pair.
92  /// For Symbols whose section is not known, this will return a
93  /// (SymbolName, Offset) pair, where the Offset is taken from the instruction
94  /// immediate (held in RE.Addend).
95  /// In both cases the Addend field is *NOT* fixed up to be PC-relative. That
96  /// should be done by the caller where appropriate by calling makePCRel on
97  /// the RelocationValueRef.
99  getRelocationValueRef(const ObjectFile &BaseTObj,
100  const relocation_iterator &RI,
101  const RelocationEntry &RE,
102  ObjSectionToIDMap &ObjSectionToID);
103 
104  /// Make the RelocationValueRef addend PC-relative.
105  void makeValueAddendPCRel(RelocationValueRef &Value,
106  const relocation_iterator &RI,
107  unsigned OffsetToNextPC);
108 
109  /// Dump information about the relocation entry (RE) and resolved value.
110  void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const;
111 
112  // Return a section iterator for the section containing the given address.
113  static section_iterator getSectionByAddress(const MachOObjectFile &Obj,
114  uint64_t Addr);
115 
116 
117  // Populate __pointers section.
118  Error populateIndirectSymbolPointersSection(const MachOObjectFile &Obj,
119  const SectionRef &PTSection,
120  unsigned PTSectionID);
121 
122 public:
123 
124  /// Create a RuntimeDyldMachO instance for the given target architecture.
125  static std::unique_ptr<RuntimeDyldMachO>
126  create(Triple::ArchType Arch,
129 
130  std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
131  loadObject(const object::ObjectFile &O) override;
132 
133  SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; }
134 
135  bool isCompatibleFile(const object::ObjectFile &Obj) const override;
136 };
137 
138 /// RuntimeDyldMachOTarget - Templated base class for generic MachO linker
139 /// algorithms and data structures.
140 ///
141 /// Concrete, target specific sub-classes can be accessed via the impl()
142 /// methods. (i.e. the RuntimeDyldMachO hierarchy uses the Curiously
143 /// Recurring Template Idiom). Concrete subclasses for each target
144 /// can be found in ./Targets.
145 template <typename Impl>
147 private:
148  Impl &impl() { return static_cast<Impl &>(*this); }
149  const Impl &impl() const { return static_cast<const Impl &>(*this); }
150 
151  unsigned char *processFDE(uint8_t *P, int64_t DeltaForText,
152  int64_t DeltaForEH);
153 
154 public:
157  : RuntimeDyldMachO(MemMgr, Resolver) {}
158 
159  Error finalizeLoad(const ObjectFile &Obj,
160  ObjSectionToIDMap &SectionMap) override;
161  void registerEHFrames() override;
162 };
163 
164 } // end namespace llvm
165 
166 #undef DEBUG_TYPE
167 
168 #endif
RelocationEntry - used to represent relocations internally in the dynamic linker. ...
This class represents lattice values for constants.
Definition: AllocatorList.h:24
unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const
This class is the base class for all object file types.
Definition: ObjectFile.h:202
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
place backedge safepoints impl
SectionEntry & getSection(unsigned SectionID)
unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const
RuntimeDyldMachOTarget - Templated base class for generic MachO linker algorithms and data structures...
#define P(N)
RuntimeDyldMachO(RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
RelocationEntry getRelocationEntry(unsigned SectionID, const ObjectFile &BaseTObj, const relocation_iterator &RI) const
Given a relocation_iterator for a non-scattered relocation, construct a RelocationEntry and fill in t...
MachO::any_relocation_info getRelocation(DataRefImpl Rel) const
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:1774
Symbol resolution interface.
Definition: JITSymbol.h:344
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
#define RTDYLD_INVALID_SECTION_ID
unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const
std::map< SectionRef, unsigned > ObjSectionToIDMap
SmallVector< EHFrameRelatedSections, 2 > UnregisteredEHFrameSections
uint32_t Size
Definition: Profile.cpp:47
RuntimeDyldMachOCRTPBase(RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
SectionEntry - represents a section emitted into memory by the dynamic linker.
RelocationInfoType
Definition: MachO.h:392
LLVM Value Representation.
Definition: Value.h:73
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81