LLVM  14.0.0git
RuntimeDyldMachO.h
Go to the documentation of this file.
1 //===-- RuntimeDyldMachO.h - Run-time dynamic linker for MC-JIT ---*- C++ -*-=//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // MachO support for MC-JIT runtime dynamic linker.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H
14 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H
15 
16 #include "RuntimeDyldImpl.h"
17 #include "llvm/Object/MachO.h"
18 #include "llvm/Support/Format.h"
19 
20 #define DEBUG_TYPE "dyld"
21 
22 using namespace llvm;
23 using namespace llvm::object;
24 
25 namespace llvm {
27 protected:
29  unsigned SectionID;
31  };
32 
35  : EHFrameSID(RTDYLD_INVALID_SECTION_ID),
37  ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {}
38 
40  : EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {}
44  };
45 
46  // When a module is loaded we save the SectionID of the EH frame section
47  // in a table until we receive a request to register all unregistered
48  // EH frame sections with the memory manager.
50 
53  : RuntimeDyldImpl(MemMgr, Resolver) {}
54 
55  /// This convenience method uses memcpy to extract a contiguous addend (the
56  /// addend size and offset are taken from the corresponding fields of the RE).
57  int64_t memcpyAddend(const RelocationEntry &RE) const;
58 
59  /// Given a relocation_iterator for a non-scattered relocation, construct a
60  /// RelocationEntry and fill in the common fields. The 'Addend' field is *not*
61  /// filled in, since immediate encodings are highly target/opcode specific.
62  /// For targets/opcodes with simple, contiguous immediates (e.g. X86) the
63  /// memcpyAddend method can be used to read the immediate.
64  RelocationEntry getRelocationEntry(unsigned SectionID,
65  const ObjectFile &BaseTObj,
66  const relocation_iterator &RI) const {
67  const MachOObjectFile &Obj =
68  static_cast<const MachOObjectFile &>(BaseTObj);
70  Obj.getRelocation(RI->getRawDataRefImpl());
71 
72  bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
73  unsigned Size = Obj.getAnyRelocationLength(RelInfo);
74  uint64_t Offset = RI->getOffset();
76  static_cast<MachO::RelocationInfoType>(Obj.getAnyRelocationType(RelInfo));
77 
78  return RelocationEntry(SectionID, Offset, RelType, 0, IsPCRel, Size);
79  }
80 
81  /// Process a scattered vanilla relocation.
83  processScatteredVANILLA(unsigned SectionID, relocation_iterator RelI,
84  const ObjectFile &BaseObjT,
86  bool TargetIsLocalThumbFunc = false);
87 
88  /// Construct a RelocationValueRef representing the relocation target.
89  /// For Symbols in known sections, this will return a RelocationValueRef
90  /// representing a (SectionID, Offset) pair.
91  /// For Symbols whose section is not known, this will return a
92  /// (SymbolName, Offset) pair, where the Offset is taken from the instruction
93  /// immediate (held in RE.Addend).
94  /// In both cases the Addend field is *NOT* fixed up to be PC-relative. That
95  /// should be done by the caller where appropriate by calling makePCRel on
96  /// the RelocationValueRef.
98  getRelocationValueRef(const ObjectFile &BaseTObj,
99  const relocation_iterator &RI,
100  const RelocationEntry &RE,
101  ObjSectionToIDMap &ObjSectionToID);
102 
103  /// Make the RelocationValueRef addend PC-relative.
104  void makeValueAddendPCRel(RelocationValueRef &Value,
105  const relocation_iterator &RI,
106  unsigned OffsetToNextPC);
107 
108  /// Dump information about the relocation entry (RE) and resolved value.
109  void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const;
110 
111  // Return a section iterator for the section containing the given address.
112  static section_iterator getSectionByAddress(const MachOObjectFile &Obj,
113  uint64_t Addr);
114 
115 
116  // Populate __pointers section.
117  Error populateIndirectSymbolPointersSection(const MachOObjectFile &Obj,
118  const SectionRef &PTSection,
119  unsigned PTSectionID);
120 
121 public:
122 
123  /// Create a RuntimeDyldMachO instance for the given target architecture.
124  static std::unique_ptr<RuntimeDyldMachO>
125  create(Triple::ArchType Arch,
128 
129  std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
130  loadObject(const object::ObjectFile &O) override;
131 
132  SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; }
133 
134  bool isCompatibleFile(const object::ObjectFile &Obj) const override;
135 };
136 
137 /// RuntimeDyldMachOTarget - Templated base class for generic MachO linker
138 /// algorithms and data structures.
139 ///
140 /// Concrete, target specific sub-classes can be accessed via the impl()
141 /// methods. (i.e. the RuntimeDyldMachO hierarchy uses the Curiously
142 /// Recurring Template Idiom). Concrete subclasses for each target
143 /// can be found in ./Targets.
144 template <typename Impl>
146 private:
147  Impl &impl() { return static_cast<Impl &>(*this); }
148  const Impl &impl() const { return static_cast<const Impl &>(*this); }
149 
150  unsigned char *processFDE(uint8_t *P, int64_t DeltaForText,
151  int64_t DeltaForEH);
152 
153 public:
156  : RuntimeDyldMachO(MemMgr, Resolver) {}
157 
158  Error finalizeLoad(const ObjectFile &Obj,
159  ObjSectionToIDMap &SectionMap) override;
160  void registerEHFrames() override;
161 };
162 
163 } // end namespace llvm
164 
165 #undef DEBUG_TYPE
166 
167 #endif
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
RuntimeDyldImpl.h
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::MachO::any_relocation_info
Definition: MachO.h:975
llvm::RuntimeDyldMachO::EHFrameRelatedSections::EHFrameRelatedSections
EHFrameRelatedSections(SID EH, SID T, SID Ex)
Definition: RuntimeDyldMachO.h:39
T
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::RuntimeDyldMachO::EHFrameRelatedSections::EHFrameSID
SID EHFrameSID
Definition: RuntimeDyldMachO.h:41
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::RuntimeDyldMachO::getRelocationEntry
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...
Definition: RuntimeDyldMachO.h:64
llvm::object::MachOObjectFile::getAnyRelocationPCRel
unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const
Definition: MachOObjectFile.cpp:4234
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
impl
place backedge safepoints impl
Definition: PlaceSafepoints.cpp:612
llvm::RuntimeDyldMachO::UnregisteredEHFrameSections
SmallVector< EHFrameRelatedSections, 2 > UnregisteredEHFrameSections
Definition: RuntimeDyldMachO.h:49
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::RuntimeDyldImpl
Definition: RuntimeDyldImpl.h:238
llvm::RuntimeDyldMachO::EHFrameRelatedSections::TextSID
SID TextSID
Definition: RuntimeDyldMachO.h:42
Format.h
MachO.h
llvm::Resolver
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2000
llvm::RuntimeDyldMachO::RuntimeDyldMachO
RuntimeDyldMachO(RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
Definition: RuntimeDyldMachO.h:51
llvm::object
Definition: ObjectFileTransformer.h:18
llvm::Triple::ArchType
ArchType
Definition: Triple.h:47
llvm::RuntimeDyldImpl::SID
unsigned SID
Definition: RuntimeDyldImpl.h:256
llvm::RuntimeDyldMachOCRTPBase
RuntimeDyldMachOTarget - Templated base class for generic MachO linker algorithms and data structures...
Definition: RuntimeDyldMachO.h:145
llvm::object::MachOObjectFile::getRelocation
MachO::any_relocation_info getRelocation(DataRefImpl Rel) const
Definition: MachOObjectFile.cpp:4424
llvm::RuntimeDyldMachO::EHFrameRelatedSections::EHFrameRelatedSections
EHFrameRelatedSections()
Definition: RuntimeDyldMachO.h:34
llvm::RuntimeDyldMachO
Definition: RuntimeDyldMachO.h:26
llvm::object::SectionRef
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:80
llvm::RelocationEntry
RelocationEntry - used to represent relocations internally in the dynamic linker.
Definition: RuntimeDyldImpl.h:117
llvm::object::MachOObjectFile
Definition: MachO.h:262
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:197
uint64_t
Addr
uint64_t Addr
Definition: ELFObjHandler.cpp:80
llvm::RuntimeDyldMachO::EHFrameRelatedSections
Definition: RuntimeDyldMachO.h:33
llvm::SectionEntry
SectionEntry - represents a section emitted into memory by the dynamic linker.
Definition: RuntimeDyldImpl.h:45
llvm::RuntimeDyldMachOCRTPBase::RuntimeDyldMachOCRTPBase
RuntimeDyldMachOCRTPBase(RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
Definition: RuntimeDyldMachO.h:154
llvm::object::content_iterator
Definition: SymbolicFile.h:67
llvm::object::ObjectFile
This class is the base class for all object file types.
Definition: ObjectFile.h:228
llvm::RuntimeDyldMachO::SectionOffsetPair::SectionID
unsigned SectionID
Definition: RuntimeDyldMachO.h:29
llvm::MachO::RelocationInfoType
RelocationInfoType
Definition: MachO.h:398
llvm::RuntimeDyldMachO::EHFrameRelatedSections::ExceptTabSID
SID ExceptTabSID
Definition: RuntimeDyldMachO.h:43
llvm::RuntimeDyldMachO::SectionOffsetPair::Offset
uint64_t Offset
Definition: RuntimeDyldMachO.h:30
llvm::object::MachOObjectFile::getAnyRelocationType
unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const
Definition: MachOObjectFile.cpp:4249
llvm::JITSymbolResolver
Symbol resolution interface.
Definition: JITSymbol.h:371
RTDYLD_INVALID_SECTION_ID
#define RTDYLD_INVALID_SECTION_ID
Definition: RuntimeDyldImpl.h:257
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::RuntimeDyldMachO::SectionOffsetPair
Definition: RuntimeDyldMachO.h:28
llvm::object::MachOObjectFile::getAnyRelocationLength
unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const
Definition: MachOObjectFile.cpp:4241
llvm::RelocationValueRef
Definition: RuntimeDyldImpl.h:190
llvm::RuntimeDyldMachO::getSection
SectionEntry & getSection(unsigned SectionID)
Definition: RuntimeDyldMachO.h:132
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::RuntimeDyldImpl::ObjSectionToIDMap
std::map< SectionRef, unsigned > ObjSectionToIDMap
Definition: RuntimeDyldImpl.h:261
llvm::RuntimeDyld::MemoryManager
Memory Management.
Definition: RuntimeDyld.h:92