LLVM  8.0.1
RuntimeDyldMachO.cpp
Go to the documentation of this file.
1 //===-- RuntimeDyldMachO.cpp - 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 // Implementation of the MC-JIT runtime dynamic linker.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "RuntimeDyldMachO.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/StringRef.h"
21 
22 using namespace llvm;
23 using namespace llvm::object;
24 
25 #define DEBUG_TYPE "dyld"
26 
27 namespace {
28 
29 class LoadedMachOObjectInfo final
30  : public LoadedObjectInfoHelper<LoadedMachOObjectInfo,
31  RuntimeDyld::LoadedObjectInfo> {
32 public:
33  LoadedMachOObjectInfo(RuntimeDyldImpl &RTDyld,
34  ObjSectionToIDMap ObjSecToIDMap)
35  : LoadedObjectInfoHelper(RTDyld, std::move(ObjSecToIDMap)) {}
36 
38  getObjectForDebug(const ObjectFile &Obj) const override {
39  return OwningBinary<ObjectFile>();
40  }
41 };
42 
43 }
44 
45 namespace llvm {
46 
48  unsigned NumBytes = 1 << RE.Size;
49  uint8_t *Src = Sections[RE.SectionID].getAddress() + RE.Offset;
50 
51  return static_cast<int64_t>(readBytesUnaligned(Src, NumBytes));
52 }
53 
56  unsigned SectionID, relocation_iterator RelI,
57  const ObjectFile &BaseObjT,
59  bool TargetIsLocalThumbFunc) {
60  const MachOObjectFile &Obj =
61  static_cast<const MachOObjectFile&>(BaseObjT);
63  Obj.getRelocation(RelI->getRawDataRefImpl());
64 
65  SectionEntry &Section = Sections[SectionID];
66  uint32_t RelocType = Obj.getAnyRelocationType(RE);
67  bool IsPCRel = Obj.getAnyRelocationPCRel(RE);
68  unsigned Size = Obj.getAnyRelocationLength(RE);
69  uint64_t Offset = RelI->getOffset();
70  uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
71  unsigned NumBytes = 1 << Size;
72  int64_t Addend = readBytesUnaligned(LocalAddress, NumBytes);
73 
74  unsigned SymbolBaseAddr = Obj.getScatteredRelocationValue(RE);
75  section_iterator TargetSI = getSectionByAddress(Obj, SymbolBaseAddr);
76  assert(TargetSI != Obj.section_end() && "Can't find section for symbol");
77  uint64_t SectionBaseAddr = TargetSI->getAddress();
78  SectionRef TargetSection = *TargetSI;
79  bool IsCode = TargetSection.isText();
80  uint32_t TargetSectionID = ~0U;
81  if (auto TargetSectionIDOrErr =
82  findOrEmitSection(Obj, TargetSection, IsCode, ObjSectionToID))
83  TargetSectionID = *TargetSectionIDOrErr;
84  else
85  return TargetSectionIDOrErr.takeError();
86 
87  Addend -= SectionBaseAddr;
88  RelocationEntry R(SectionID, Offset, RelocType, Addend, IsPCRel, Size);
89  R.IsTargetThumbFunc = TargetIsLocalThumbFunc;
90 
91  addRelocationForSection(R, TargetSectionID);
92 
93  return ++RelI;
94 }
95 
96 
99  const ObjectFile &BaseTObj, const relocation_iterator &RI,
100  const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID) {
101 
102  const MachOObjectFile &Obj =
103  static_cast<const MachOObjectFile &>(BaseTObj);
105  Obj.getRelocation(RI->getRawDataRefImpl());
107 
108  bool IsExternal = Obj.getPlainRelocationExternal(RelInfo);
109  if (IsExternal) {
110  symbol_iterator Symbol = RI->getSymbol();
111  StringRef TargetName;
112  if (auto TargetNameOrErr = Symbol->getName())
113  TargetName = *TargetNameOrErr;
114  else
115  return TargetNameOrErr.takeError();
117  GlobalSymbolTable.find(TargetName.data());
118  if (SI != GlobalSymbolTable.end()) {
119  const auto &SymInfo = SI->second;
120  Value.SectionID = SymInfo.getSectionID();
121  Value.Offset = SymInfo.getOffset() + RE.Addend;
122  } else {
123  Value.SymbolName = TargetName.data();
124  Value.Offset = RE.Addend;
125  }
126  } else {
127  SectionRef Sec = Obj.getAnyRelocationSection(RelInfo);
128  bool IsCode = Sec.isText();
129  if (auto SectionIDOrErr = findOrEmitSection(Obj, Sec, IsCode,
130  ObjSectionToID))
131  Value.SectionID = *SectionIDOrErr;
132  else
133  return SectionIDOrErr.takeError();
134  uint64_t Addr = Sec.getAddress();
135  Value.Offset = RE.Addend - Addr;
136  }
137 
138  return Value;
139 }
140 
142  const relocation_iterator &RI,
143  unsigned OffsetToNextPC) {
144  auto &O = *cast<MachOObjectFile>(RI->getObject());
145  section_iterator SecI = O.getRelocationRelocatedSection(RI);
146  Value.Offset += RI->getOffset() + OffsetToNextPC + SecI->getAddress();
147 }
148 
150  uint64_t Value) const {
151  const SectionEntry &Section = Sections[RE.SectionID];
152  uint8_t *LocalAddress = Section.getAddress() + RE.Offset;
153  uint64_t FinalAddress = Section.getLoadAddress() + RE.Offset;
154 
155  dbgs() << "resolveRelocation Section: " << RE.SectionID
156  << " LocalAddress: " << format("%p", LocalAddress)
157  << " FinalAddress: " << format("0x%016" PRIx64, FinalAddress)
158  << " Value: " << format("0x%016" PRIx64, Value) << " Addend: " << RE.Addend
159  << " isPCRel: " << RE.IsPCRel << " MachoType: " << RE.RelType
160  << " Size: " << (1 << RE.Size) << "\n";
161 }
162 
165  uint64_t Addr) {
167  section_iterator SE = Obj.section_end();
168 
169  for (; SI != SE; ++SI) {
170  uint64_t SAddr = SI->getAddress();
171  uint64_t SSize = SI->getSize();
172  if ((Addr >= SAddr) && (Addr < SAddr + SSize))
173  return SI;
174  }
175 
176  return SE;
177 }
178 
179 
180 // Populate __pointers section.
182  const MachOObjectFile &Obj,
183  const SectionRef &PTSection,
184  unsigned PTSectionID) {
185  assert(!Obj.is64Bit() &&
186  "Pointer table section not supported in 64-bit MachO.");
187 
189  MachO::section Sec32 = Obj.getSection(PTSection.getRawDataRefImpl());
190  uint32_t PTSectionSize = Sec32.size;
191  unsigned FirstIndirectSymbol = Sec32.reserved1;
192  const unsigned PTEntrySize = 4;
193  unsigned NumPTEntries = PTSectionSize / PTEntrySize;
194  unsigned PTEntryOffset = 0;
195 
196  assert((PTSectionSize % PTEntrySize) == 0 &&
197  "Pointers section does not contain a whole number of stubs?");
198 
199  LLVM_DEBUG(dbgs() << "Populating pointer table section "
200  << Sections[PTSectionID].getName() << ", Section ID "
201  << PTSectionID << ", " << NumPTEntries << " entries, "
202  << PTEntrySize << " bytes each:\n");
203 
204  for (unsigned i = 0; i < NumPTEntries; ++i) {
205  unsigned SymbolIndex =
206  Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
207  symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
208  StringRef IndirectSymbolName;
209  if (auto IndirectSymbolNameOrErr = SI->getName())
210  IndirectSymbolName = *IndirectSymbolNameOrErr;
211  else
212  return IndirectSymbolNameOrErr.takeError();
213  LLVM_DEBUG(dbgs() << " " << IndirectSymbolName << ": index " << SymbolIndex
214  << ", PT offset: " << PTEntryOffset << "\n");
215  RelocationEntry RE(PTSectionID, PTEntryOffset,
216  MachO::GENERIC_RELOC_VANILLA, 0, false, 2);
217  addRelocationForSymbol(RE, IndirectSymbolName);
218  PTEntryOffset += PTEntrySize;
219  }
220  return Error::success();
221 }
222 
224  return Obj.isMachO();
225 }
226 
227 template <typename Impl>
228 Error
230  ObjSectionToIDMap &SectionMap) {
231  unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
232  unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
233  unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
234 
235  for (const auto &Section : Obj.sections()) {
236  StringRef Name;
237  Section.getName(Name);
238 
239  // Force emission of the __text, __eh_frame, and __gcc_except_tab sections
240  // if they're present. Otherwise call down to the impl to handle other
241  // sections that have already been emitted.
242  if (Name == "__text") {
243  if (auto TextSIDOrErr = findOrEmitSection(Obj, Section, true, SectionMap))
244  TextSID = *TextSIDOrErr;
245  else
246  return TextSIDOrErr.takeError();
247  } else if (Name == "__eh_frame") {
248  if (auto EHFrameSIDOrErr = findOrEmitSection(Obj, Section, false,
249  SectionMap))
250  EHFrameSID = *EHFrameSIDOrErr;
251  else
252  return EHFrameSIDOrErr.takeError();
253  } else if (Name == "__gcc_except_tab") {
254  if (auto ExceptTabSIDOrErr = findOrEmitSection(Obj, Section, true,
255  SectionMap))
256  ExceptTabSID = *ExceptTabSIDOrErr;
257  else
258  return ExceptTabSIDOrErr.takeError();
259  } else {
260  auto I = SectionMap.find(Section);
261  if (I != SectionMap.end())
262  if (auto Err = impl().finalizeSection(Obj, I->second, Section))
263  return Err;
264  }
265  }
266  UnregisteredEHFrameSections.push_back(
267  EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID));
268 
269  return Error::success();
270 }
271 
272 template <typename Impl>
273 unsigned char *RuntimeDyldMachOCRTPBase<Impl>::processFDE(uint8_t *P,
274  int64_t DeltaForText,
275  int64_t DeltaForEH) {
276  typedef typename Impl::TargetPtrT TargetPtrT;
277 
278  LLVM_DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
279  << ", Delta for EH: " << DeltaForEH << "\n");
280  uint32_t Length = readBytesUnaligned(P, 4);
281  P += 4;
282  uint8_t *Ret = P + Length;
283  uint32_t Offset = readBytesUnaligned(P, 4);
284  if (Offset == 0) // is a CIE
285  return Ret;
286 
287  P += 4;
288  TargetPtrT FDELocation = readBytesUnaligned(P, sizeof(TargetPtrT));
289  TargetPtrT NewLocation = FDELocation - DeltaForText;
290  writeBytesUnaligned(NewLocation, P, sizeof(TargetPtrT));
291 
292  P += sizeof(TargetPtrT);
293 
294  // Skip the FDE address range
295  P += sizeof(TargetPtrT);
296 
297  uint8_t Augmentationsize = *P;
298  P += 1;
299  if (Augmentationsize != 0) {
300  TargetPtrT LSDA = readBytesUnaligned(P, sizeof(TargetPtrT));
301  TargetPtrT NewLSDA = LSDA - DeltaForEH;
302  writeBytesUnaligned(NewLSDA, P, sizeof(TargetPtrT));
303  }
304 
305  return Ret;
306 }
307 
308 static int64_t computeDelta(SectionEntry *A, SectionEntry *B) {
309  int64_t ObjDistance = static_cast<int64_t>(A->getObjAddress()) -
310  static_cast<int64_t>(B->getObjAddress());
311  int64_t MemDistance = A->getLoadAddress() - B->getLoadAddress();
312  return ObjDistance - MemDistance;
313 }
314 
315 template <typename Impl>
317 
318  for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
319  EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
320  if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
321  SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
322  continue;
323  SectionEntry *Text = &Sections[SectionInfo.TextSID];
324  SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
325  SectionEntry *ExceptTab = nullptr;
326  if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
327  ExceptTab = &Sections[SectionInfo.ExceptTabSID];
328 
329  int64_t DeltaForText = computeDelta(Text, EHFrame);
330  int64_t DeltaForEH = 0;
331  if (ExceptTab)
332  DeltaForEH = computeDelta(ExceptTab, EHFrame);
333 
334  uint8_t *P = EHFrame->getAddress();
335  uint8_t *End = P + EHFrame->getSize();
336  while (P != End) {
337  P = processFDE(P, DeltaForText, DeltaForEH);
338  }
339 
340  MemMgr.registerEHFrames(EHFrame->getAddress(), EHFrame->getLoadAddress(),
341  EHFrame->getSize());
342  }
343  UnregisteredEHFrameSections.clear();
344 }
345 
346 std::unique_ptr<RuntimeDyldMachO>
350  switch (Arch) {
351  default:
352  llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
353  break;
354  case Triple::arm:
355  return make_unique<RuntimeDyldMachOARM>(MemMgr, Resolver);
356  case Triple::aarch64:
357  return make_unique<RuntimeDyldMachOAArch64>(MemMgr, Resolver);
358  case Triple::x86:
359  return make_unique<RuntimeDyldMachOI386>(MemMgr, Resolver);
360  case Triple::x86_64:
361  return make_unique<RuntimeDyldMachOX86_64>(MemMgr, Resolver);
362  }
363 }
364 
365 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
367  if (auto ObjSectionToIDOrErr = loadObjectImpl(O))
368  return llvm::make_unique<LoadedMachOObjectInfo>(*this,
369  *ObjSectionToIDOrErr);
370  else {
371  HasError = true;
372  raw_string_ostream ErrStream(ErrorStr);
373  logAllUnhandledErrors(ObjSectionToIDOrErr.takeError(), ErrStream);
374  return nullptr;
375  }
376 }
377 
378 } // end namespace llvm
RelocationEntry - used to represent relocations internally in the dynamic linker. ...
Error finalizeLoad(const ObjectFile &Obj, ObjSectionToIDMap &SectionMap) override
This class represents lattice values for constants.
Definition: AllocatorList.h:24
Expected< StringRef > getName() const
Definition: ObjectFile.h:359
unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
uint32_t reserved1
Definition: MachO.h:564
bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const
Expected< SectionRef > getSection(unsigned SectionIndex) const
void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const
Dump information about the relocation entry (RE) and resolved value.
This class is the base class for all object file types.
Definition: ObjectFile.h:202
uint32_t size
Definition: MachO.h:558
uint8_t * getAddress() const
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
bool isCompatibleFile(const object::ObjectFile &Obj) const override
bool IsPCRel
True if this is a PCRel relocation (MachO specific).
unsigned SectionID
SectionID - the section this relocation points to.
amdgpu Simplify well known AMD library false Value Value const Twine & Name
uint64_t getAddress() const
Definition: ObjectFile.h:418
static StringRef getName(Value *V)
bool isText() const
Whether this section contains instructions.
Definition: ObjectFile.h:442
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
static std::unique_ptr< RuntimeDyldMachO > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
Create a RuntimeDyldMachO instance for the given target architecture.
place backedge safepoints impl
int64_t memcpyAddend(const RelocationEntry &RE) const
This convenience method uses memcpy to extract a contiguous addend (the addend size and offset are ta...
SectionRef getAnyRelocationSection(const MachO::any_relocation_info &RE) const
symbol_iterator getSymbolByIndex(unsigned Index) const
section_iterator_range sections() const
Definition: ObjectFile.h:292
unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const
RuntimeDyldMachOTarget - Templated base class for generic MachO linker algorithms and data structures...
#define P(N)
Expected< RelocationValueRef > getRelocationValueRef(const ObjectFile &BaseTObj, const relocation_iterator &RI, const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID)
Construct a RelocationValueRef representing the relocation target.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static int64_t computeDelta(SectionEntry *A, SectionEntry *B)
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
uintptr_t getObjAddress() const
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:486
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition: Error.cpp:62
static section_iterator getSectionByAddress(const MachOObjectFile &Obj, uint64_t Addr)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static ErrorSuccess success()
Create a success value.
Definition: Error.h:327
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
uint32_t RelType
RelType - relocation type.
Expected< relocation_iterator > processScatteredVANILLA(unsigned SectionID, relocation_iterator RelI, const ObjectFile &BaseObjT, RuntimeDyldMachO::ObjSectionToIDMap &ObjSectionToID, bool TargetIsLocalThumbFunc=false)
Process a scattered vanilla relocation.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
size_t getSize() const
#define RTDYLD_INVALID_SECTION_ID
unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const
uint64_t Offset
Offset - offset into the section.
std::map< SectionRef, unsigned > ObjSectionToIDMap
uint8_t * getAddressWithOffset(unsigned OffsetBytes) const
Return the address of this section with an offset.
#define I(x, y, z)
Definition: MD5.cpp:58
uint64_t getLoadAddress() const
uint32_t Size
Definition: Profile.cpp:47
section_iterator section_begin() const override
std::unique_ptr< RuntimeDyld::LoadedObjectInfo > loadObject(const object::ObjectFile &O) override
SymInfo contains information about symbol: it&#39;s address and section index which is -1LL for absolute ...
SectionEntry - represents a section emitted into memory by the dynamic linker.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:483
LLVM Value Representation.
Definition: Value.h:73
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
unsigned Size
The size of this relocation (MachO specific).
section_iterator section_end() const override
Error populateIndirectSymbolPointersSection(const MachOObjectFile &Obj, const SectionRef &PTSection, unsigned PTSectionID)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
bool isMachO() const
Definition: Binary.h:113
void makeValueAddendPCRel(RelocationValueRef &Value, const relocation_iterator &RI, unsigned OffsetToNextPC)
Make the RelocationValueRef addend PC-relative.
#define LLVM_DEBUG(X)
Definition: Debug.h:123
uint32_t getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC, unsigned Index) const
uint32_t getScatteredRelocationValue(const MachO::any_relocation_info &RE) const
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
MachO::dysymtab_command getDysymtabLoadCommand() const