LLVM  3.7.0
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
30  : public RuntimeDyld::LoadedObjectInfoHelper<LoadedMachOObjectInfo> {
31 public:
32  LoadedMachOObjectInfo(RuntimeDyldImpl &RTDyld, unsigned BeginIdx,
33  unsigned EndIdx)
34  : LoadedObjectInfoHelper(RTDyld, BeginIdx, EndIdx) {}
35 
37  getObjectForDebug(const ObjectFile &Obj) const override {
38  return OwningBinary<ObjectFile>();
39  }
40 };
41 
42 }
43 
44 namespace llvm {
45 
47  unsigned NumBytes = 1 << RE.Size;
48  uint8_t *Src = Sections[RE.SectionID].Address + RE.Offset;
49 
50  return static_cast<int64_t>(readBytesUnaligned(Src, NumBytes));
51 }
52 
54  const ObjectFile &BaseTObj, const relocation_iterator &RI,
55  const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID) {
56 
57  const MachOObjectFile &Obj =
58  static_cast<const MachOObjectFile &>(BaseTObj);
60  Obj.getRelocation(RI->getRawDataRefImpl());
62 
63  bool IsExternal = Obj.getPlainRelocationExternal(RelInfo);
64  if (IsExternal) {
65  symbol_iterator Symbol = RI->getSymbol();
66  ErrorOr<StringRef> TargetNameOrErr = Symbol->getName();
67  if (std::error_code EC = TargetNameOrErr.getError())
68  report_fatal_error(EC.message());
69  StringRef TargetName = *TargetNameOrErr;
71  GlobalSymbolTable.find(TargetName.data());
72  if (SI != GlobalSymbolTable.end()) {
73  const auto &SymInfo = SI->second;
74  Value.SectionID = SymInfo.getSectionID();
75  Value.Offset = SymInfo.getOffset() + RE.Addend;
76  } else {
77  Value.SymbolName = TargetName.data();
78  Value.Offset = RE.Addend;
79  }
80  } else {
81  SectionRef Sec = Obj.getAnyRelocationSection(RelInfo);
82  bool IsCode = Sec.isText();
83  Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID);
84  uint64_t Addr = Sec.getAddress();
85  Value.Offset = RE.Addend - Addr;
86  }
87 
88  return Value;
89 }
90 
92  const relocation_iterator &RI,
93  unsigned OffsetToNextPC) {
94  auto &O = *cast<MachOObjectFile>(RI->getObject());
95  section_iterator SecI = O.getRelocationRelocatedSection(RI);
96  Value.Offset += RI->getOffset() + OffsetToNextPC + SecI->getAddress();
97 }
98 
100  uint64_t Value) const {
101  const SectionEntry &Section = Sections[RE.SectionID];
102  uint8_t *LocalAddress = Section.Address + RE.Offset;
103  uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
104 
105  dbgs() << "resolveRelocation Section: " << RE.SectionID
106  << " LocalAddress: " << format("%p", LocalAddress)
107  << " FinalAddress: " << format("0x%016" PRIx64, FinalAddress)
108  << " Value: " << format("0x%016" PRIx64, Value) << " Addend: " << RE.Addend
109  << " isPCRel: " << RE.IsPCRel << " MachoType: " << RE.RelType
110  << " Size: " << (1 << RE.Size) << "\n";
111 }
112 
115  uint64_t Addr) {
117  section_iterator SE = Obj.section_end();
118 
119  for (; SI != SE; ++SI) {
120  uint64_t SAddr = SI->getAddress();
121  uint64_t SSize = SI->getSize();
122  if ((Addr >= SAddr) && (Addr < SAddr + SSize))
123  return SI;
124  }
125 
126  return SE;
127 }
128 
129 
130 // Populate __pointers section.
132  const MachOObjectFile &Obj,
133  const SectionRef &PTSection,
134  unsigned PTSectionID) {
135  assert(!Obj.is64Bit() &&
136  "Pointer table section not supported in 64-bit MachO.");
137 
139  MachO::section Sec32 = Obj.getSection(PTSection.getRawDataRefImpl());
140  uint32_t PTSectionSize = Sec32.size;
141  unsigned FirstIndirectSymbol = Sec32.reserved1;
142  const unsigned PTEntrySize = 4;
143  unsigned NumPTEntries = PTSectionSize / PTEntrySize;
144  unsigned PTEntryOffset = 0;
145 
146  assert((PTSectionSize % PTEntrySize) == 0 &&
147  "Pointers section does not contain a whole number of stubs?");
148 
149  DEBUG(dbgs() << "Populating pointer table section "
150  << Sections[PTSectionID].Name
151  << ", Section ID " << PTSectionID << ", "
152  << NumPTEntries << " entries, " << PTEntrySize
153  << " bytes each:\n");
154 
155  for (unsigned i = 0; i < NumPTEntries; ++i) {
156  unsigned SymbolIndex =
157  Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
158  symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
159  ErrorOr<StringRef> IndirectSymbolNameOrErr = SI->getName();
160  if (std::error_code EC = IndirectSymbolNameOrErr.getError())
161  report_fatal_error(EC.message());
162  StringRef IndirectSymbolName = *IndirectSymbolNameOrErr;
163  DEBUG(dbgs() << " " << IndirectSymbolName << ": index " << SymbolIndex
164  << ", PT offset: " << PTEntryOffset << "\n");
165  RelocationEntry RE(PTSectionID, PTEntryOffset,
166  MachO::GENERIC_RELOC_VANILLA, 0, false, 2);
167  addRelocationForSymbol(RE, IndirectSymbolName);
168  PTEntryOffset += PTEntrySize;
169  }
170 }
171 
173  return Obj.isMachO();
174 }
175 
176 template <typename Impl>
178  ObjSectionToIDMap &SectionMap) {
179  unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
180  unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
181  unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
182 
183  for (const auto &Section : Obj.sections()) {
184  StringRef Name;
185  Section.getName(Name);
186 
187  // Force emission of the __text, __eh_frame, and __gcc_except_tab sections
188  // if they're present. Otherwise call down to the impl to handle other
189  // sections that have already been emitted.
190  if (Name == "__text")
191  TextSID = findOrEmitSection(Obj, Section, true, SectionMap);
192  else if (Name == "__eh_frame")
193  EHFrameSID = findOrEmitSection(Obj, Section, false, SectionMap);
194  else if (Name == "__gcc_except_tab")
195  ExceptTabSID = findOrEmitSection(Obj, Section, true, SectionMap);
196  else {
197  auto I = SectionMap.find(Section);
198  if (I != SectionMap.end())
199  impl().finalizeSection(Obj, I->second, Section);
200  }
201  }
202  UnregisteredEHFrameSections.push_back(
203  EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID));
204 }
205 
206 template <typename Impl>
207 unsigned char *RuntimeDyldMachOCRTPBase<Impl>::processFDE(unsigned char *P,
208  int64_t DeltaForText,
209  int64_t DeltaForEH) {
210  typedef typename Impl::TargetPtrT TargetPtrT;
211 
212  DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
213  << ", Delta for EH: " << DeltaForEH << "\n");
214  uint32_t Length = readBytesUnaligned(P, 4);
215  P += 4;
216  unsigned char *Ret = P + Length;
217  uint32_t Offset = readBytesUnaligned(P, 4);
218  if (Offset == 0) // is a CIE
219  return Ret;
220 
221  P += 4;
222  TargetPtrT FDELocation = readBytesUnaligned(P, sizeof(TargetPtrT));
223  TargetPtrT NewLocation = FDELocation - DeltaForText;
224  writeBytesUnaligned(NewLocation, P, sizeof(TargetPtrT));
225 
226  P += sizeof(TargetPtrT);
227 
228  // Skip the FDE address range
229  P += sizeof(TargetPtrT);
230 
231  uint8_t Augmentationsize = *P;
232  P += 1;
233  if (Augmentationsize != 0) {
234  TargetPtrT LSDA = readBytesUnaligned(P, sizeof(TargetPtrT));
235  TargetPtrT NewLSDA = LSDA - DeltaForEH;
236  writeBytesUnaligned(NewLSDA, P, sizeof(TargetPtrT));
237  }
238 
239  return Ret;
240 }
241 
242 static int64_t computeDelta(SectionEntry *A, SectionEntry *B) {
243  int64_t ObjDistance =
244  static_cast<int64_t>(A->ObjAddress) - static_cast<int64_t>(B->ObjAddress);
245  int64_t MemDistance = A->LoadAddress - B->LoadAddress;
246  return ObjDistance - MemDistance;
247 }
248 
249 template <typename Impl>
251 
252  for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
253  EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
254  if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
255  SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
256  continue;
257  SectionEntry *Text = &Sections[SectionInfo.TextSID];
258  SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
259  SectionEntry *ExceptTab = nullptr;
260  if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
261  ExceptTab = &Sections[SectionInfo.ExceptTabSID];
262 
263  int64_t DeltaForText = computeDelta(Text, EHFrame);
264  int64_t DeltaForEH = 0;
265  if (ExceptTab)
266  DeltaForEH = computeDelta(ExceptTab, EHFrame);
267 
268  unsigned char *P = EHFrame->Address;
269  unsigned char *End = P + EHFrame->Size;
270  do {
271  P = processFDE(P, DeltaForText, DeltaForEH);
272  } while (P != End);
273 
274  MemMgr.registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
275  EHFrame->Size);
276  }
277  UnregisteredEHFrameSections.clear();
278 }
279 
280 std::unique_ptr<RuntimeDyldMachO>
283  RuntimeDyld::SymbolResolver &Resolver) {
284  switch (Arch) {
285  default:
286  llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
287  break;
288  case Triple::arm:
289  return make_unique<RuntimeDyldMachOARM>(MemMgr, Resolver);
290  case Triple::aarch64:
291  return make_unique<RuntimeDyldMachOAArch64>(MemMgr, Resolver);
292  case Triple::x86:
293  return make_unique<RuntimeDyldMachOI386>(MemMgr, Resolver);
294  case Triple::x86_64:
295  return make_unique<RuntimeDyldMachOX86_64>(MemMgr, Resolver);
296  }
297 }
298 
299 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
301  unsigned SectionStartIdx, SectionEndIdx;
302  std::tie(SectionStartIdx, SectionEndIdx) = loadObjectImpl(O);
303  return llvm::make_unique<LoadedMachOObjectInfo>(*this, SectionStartIdx,
304  SectionEndIdx);
305 }
306 
307 } // end namespace llvm
RelocationEntry - used to represent relocations internally in the dynamic linker. ...
std::error_code getError() const
Definition: ErrorOr.h:178
size_t Size
Size - section size. Doesn't include the stubs.
bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const
Represents either an error or a value T.
Definition: ErrorOr.h:82
This class is the base class for all object file types.
Definition: ObjectFile.h:176
uint8_t * Address
Address - address in the linker's memory where the section resides.
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:410
void populateIndirectSymbolPointersSection(const MachOObjectFile &Obj, const SectionRef &PTSection, unsigned PTSectionID)
uint32_t getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC, unsigned Index) const
bool isCompatibleFile(const object::ObjectFile &Obj) const override
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
bool IsPCRel
True if this is a PCRel relocation (MachO specific).
unsigned SectionID
SectionID - the section this relocation points to.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
int64_t memcpyAddend(const RelocationEntry &RE) const
This convenience method uses memcpy to extract a contiguous addend (the addend size and offset are ta...
place backedge safepoints impl
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:107
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:111
void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const
Dump information about the relocation entry (RE) and resolved value.
RuntimeDyldMachOTarget - Templated base class for generic MachO linker algorithms and data structures...
#define P(N)
MachO::any_relocation_info getRelocation(DataRefImpl Rel) const
MachO::section getSection(DataRefImpl DRI) const
static int64_t computeDelta(SectionEntry *A, SectionEntry *B)
bool isText() const
Definition: ObjectFile.h:382
static std::unique_ptr< RuntimeDyldMachO > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, RuntimeDyld::SymbolResolver &Resolver)
Create a RuntimeDyldMachO instance for the given target architecture.
section_iterator_range sections() const
Definition: ObjectFile.h:253
uint64_t getAddress() const
Definition: ObjectFile.h:366
static section_iterator getSectionByAddress(const MachOObjectFile &Obj, uint64_t Addr)
bool isMachO() const
Definition: Binary.h:108
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
uint32_t RelType
RelType - relocation type.
uintptr_t ObjAddress
ObjAddress - address of the section in the in-memory object file.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
SectionRef getAnyRelocationSection(const MachO::any_relocation_info &RE) const
ErrorOr< StringRef > getName() const
Definition: ObjectFile.h:306
#define RTDYLD_INVALID_SECTION_ID
uint64_t Offset
Offset - offset into the section.
std::map< SectionRef, unsigned > ObjSectionToIDMap
RelocationValueRef getRelocationValueRef(const ObjectFile &BaseTObj, const relocation_iterator &RI, const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID)
Construct a RelocationValueRef representing the relocation target.
#define I(x, y, z)
Definition: MD5.cpp:54
uint64_t LoadAddress
LoadAddress - the address of the section in the target process's memory.
basic_symbol_iterator getSymbolByIndex(unsigned Index) const
section_iterator section_begin() const override
std::unique_ptr< RuntimeDyld::LoadedObjectInfo > loadObject(const object::ObjectFile &O) override
MachO::dysymtab_command getDysymtabLoadCommand() const
SectionEntry - represents a section emitted into memory by the dynamic linker.
LLVM Value Representation.
Definition: Value.h:69
unsigned Size
The size of this relocation (MachO specific).
section_iterator section_end() const override
#define DEBUG(X)
Definition: Debug.h:92
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
void makeValueAddendPCRel(RelocationValueRef &Value, const relocation_iterator &RI, unsigned OffsetToNextPC)
Make the RelocationValueRef addend PC-relative.
void finalizeLoad(const ObjectFile &Obj, ObjSectionToIDMap &SectionMap) override
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:69