LLVM  3.7.0
RuntimeDyldMachOI386.h
Go to the documentation of this file.
1 //===---- RuntimeDyldMachOI386.h ---- MachO/I386 specific code. ---*- 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 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOI386_H
11 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOI386_H
12 
13 #include "../RuntimeDyldMachO.h"
14 
15 #define DEBUG_TYPE "dyld"
16 
17 namespace llvm {
18 
20  : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOI386> {
21 public:
22 
23  typedef uint32_t TargetPtrT;
24 
27  : RuntimeDyldMachOCRTPBase(MM, Resolver) {}
28 
29  unsigned getMaxStubSize() override { return 0; }
30 
31  unsigned getStubAlignment() override { return 1; }
32 
34  processRelocationRef(unsigned SectionID, relocation_iterator RelI,
35  const ObjectFile &BaseObjT,
36  ObjSectionToIDMap &ObjSectionToID,
37  StubMap &Stubs) override {
38  const MachOObjectFile &Obj =
39  static_cast<const MachOObjectFile &>(BaseObjT);
41  Obj.getRelocation(RelI->getRawDataRefImpl());
42  uint32_t RelType = Obj.getAnyRelocationType(RelInfo);
43 
44  if (Obj.isRelocationScattered(RelInfo)) {
45  if (RelType == MachO::GENERIC_RELOC_SECTDIFF ||
47  return processSECTDIFFRelocation(SectionID, RelI, Obj,
48  ObjSectionToID);
49  else if (RelType == MachO::GENERIC_RELOC_VANILLA)
50  return processI386ScatteredVANILLA(SectionID, RelI, Obj,
51  ObjSectionToID);
52  llvm_unreachable("Unhandled scattered relocation.");
53  }
54 
55  RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));
56  RE.Addend = memcpyAddend(RE);
58  getRelocationValueRef(Obj, RelI, RE, ObjSectionToID));
59 
60  // Addends for external, PC-rel relocations on i386 point back to the zero
61  // offset. Calculate the final offset from the relocation target instead.
62  // This allows us to use the same logic for both external and internal
63  // relocations in resolveI386RelocationRef.
64  // bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
65  // if (IsExtern && RE.IsPCRel) {
66  // uint64_t RelocAddr = 0;
67  // RelI->getAddress(RelocAddr);
68  // Value.Addend += RelocAddr + 4;
69  // }
70  if (RE.IsPCRel)
71  makeValueAddendPCRel(Value, RelI, 1 << RE.Size);
72 
73  RE.Addend = Value.Offset;
74 
75  if (Value.SymbolName)
77  else
79 
80  return ++RelI;
81  }
82 
83  void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
84  DEBUG(dumpRelocationToResolve(RE, Value));
85 
87  uint8_t *LocalAddress = Section.Address + RE.Offset;
88 
89  if (RE.IsPCRel) {
90  uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
91  Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
92  }
93 
94  switch (RE.RelType) {
95  default:
96  llvm_unreachable("Invalid relocation type!");
98  writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
99  break;
102  uint64_t SectionABase = Sections[RE.Sections.SectionA].LoadAddress;
103  uint64_t SectionBBase = Sections[RE.Sections.SectionB].LoadAddress;
104  assert((Value == SectionABase || Value == SectionBBase) &&
105  "Unexpected SECTDIFF relocation value.");
106  Value = SectionABase - SectionBBase + RE.Addend;
107  writeBytesUnaligned(Value, LocalAddress, 1 << RE.Size);
108  break;
109  }
111  Error("Relocation type not implemented yet!");
112  }
113  }
114 
115  void finalizeSection(const ObjectFile &Obj, unsigned SectionID,
116  const SectionRef &Section) {
117  StringRef Name;
118  Section.getName(Name);
119 
120  if (Name == "__jump_table")
121  populateJumpTable(cast<MachOObjectFile>(Obj), Section, SectionID);
122  else if (Name == "__pointers")
123  populateIndirectSymbolPointersSection(cast<MachOObjectFile>(Obj),
124  Section, SectionID);
125  }
126 
127 private:
129  processSECTDIFFRelocation(unsigned SectionID, relocation_iterator RelI,
130  const ObjectFile &BaseObjT,
131  ObjSectionToIDMap &ObjSectionToID) {
132  const MachOObjectFile &Obj =
133  static_cast<const MachOObjectFile&>(BaseObjT);
135  Obj.getRelocation(RelI->getRawDataRefImpl());
136 
137  SectionEntry &Section = Sections[SectionID];
138  uint32_t RelocType = Obj.getAnyRelocationType(RE);
139  bool IsPCRel = Obj.getAnyRelocationPCRel(RE);
140  unsigned Size = Obj.getAnyRelocationLength(RE);
141  uint64_t Offset = RelI->getOffset();
142  uint8_t *LocalAddress = Section.Address + Offset;
143  unsigned NumBytes = 1 << Size;
144  uint64_t Addend = readBytesUnaligned(LocalAddress, NumBytes);
145 
146  ++RelI;
148  Obj.getRelocation(RelI->getRawDataRefImpl());
149 
150  uint32_t AddrA = Obj.getScatteredRelocationValue(RE);
151  section_iterator SAI = getSectionByAddress(Obj, AddrA);
152  assert(SAI != Obj.section_end() && "Can't find section for address A");
153  uint64_t SectionABase = SAI->getAddress();
154  uint64_t SectionAOffset = AddrA - SectionABase;
155  SectionRef SectionA = *SAI;
156  bool IsCode = SectionA.isText();
157  uint32_t SectionAID =
158  findOrEmitSection(Obj, SectionA, IsCode, ObjSectionToID);
159 
160  uint32_t AddrB = Obj.getScatteredRelocationValue(RE2);
161  section_iterator SBI = getSectionByAddress(Obj, AddrB);
162  assert(SBI != Obj.section_end() && "Can't find section for address B");
163  uint64_t SectionBBase = SBI->getAddress();
164  uint64_t SectionBOffset = AddrB - SectionBBase;
165  SectionRef SectionB = *SBI;
166  uint32_t SectionBID =
167  findOrEmitSection(Obj, SectionB, IsCode, ObjSectionToID);
168 
169  // Compute the addend 'C' from the original expression 'A - B + C'.
170  Addend -= AddrA - AddrB;
171 
172  DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA << ", AddrB: " << AddrB
173  << ", Addend: " << Addend << ", SectionA ID: " << SectionAID
174  << ", SectionAOffset: " << SectionAOffset
175  << ", SectionB ID: " << SectionBID
176  << ", SectionBOffset: " << SectionBOffset << "\n");
177  RelocationEntry R(SectionID, Offset, RelocType, Addend, SectionAID,
178  SectionAOffset, SectionBID, SectionBOffset,
179  IsPCRel, Size);
180 
181  addRelocationForSection(R, SectionAID);
182 
183  return ++RelI;
184  }
185 
186  relocation_iterator processI386ScatteredVANILLA(
187  unsigned SectionID, relocation_iterator RelI,
188  const ObjectFile &BaseObjT,
189  RuntimeDyldMachO::ObjSectionToIDMap &ObjSectionToID) {
190  const MachOObjectFile &Obj =
191  static_cast<const MachOObjectFile&>(BaseObjT);
193  Obj.getRelocation(RelI->getRawDataRefImpl());
194 
195  SectionEntry &Section = Sections[SectionID];
196  uint32_t RelocType = Obj.getAnyRelocationType(RE);
197  bool IsPCRel = Obj.getAnyRelocationPCRel(RE);
198  unsigned Size = Obj.getAnyRelocationLength(RE);
199  uint64_t Offset = RelI->getOffset();
200  uint8_t *LocalAddress = Section.Address + Offset;
201  unsigned NumBytes = 1 << Size;
202  int64_t Addend = readBytesUnaligned(LocalAddress, NumBytes);
203 
204  unsigned SymbolBaseAddr = Obj.getScatteredRelocationValue(RE);
205  section_iterator TargetSI = getSectionByAddress(Obj, SymbolBaseAddr);
206  assert(TargetSI != Obj.section_end() && "Can't find section for symbol");
207  uint64_t SectionBaseAddr = TargetSI->getAddress();
208  SectionRef TargetSection = *TargetSI;
209  bool IsCode = TargetSection.isText();
210  uint32_t TargetSectionID =
211  findOrEmitSection(Obj, TargetSection, IsCode, ObjSectionToID);
212 
213  Addend -= SectionBaseAddr;
214  RelocationEntry R(SectionID, Offset, RelocType, Addend, IsPCRel, Size);
215 
216  addRelocationForSection(R, TargetSectionID);
217 
218  return ++RelI;
219  }
220 
221  // Populate stubs in __jump_table section.
222  void populateJumpTable(const MachOObjectFile &Obj, const SectionRef &JTSection,
223  unsigned JTSectionID) {
224  assert(!Obj.is64Bit() &&
225  "__jump_table section not supported in 64-bit MachO.");
226 
228  MachO::section Sec32 = Obj.getSection(JTSection.getRawDataRefImpl());
229  uint32_t JTSectionSize = Sec32.size;
230  unsigned FirstIndirectSymbol = Sec32.reserved1;
231  unsigned JTEntrySize = Sec32.reserved2;
232  unsigned NumJTEntries = JTSectionSize / JTEntrySize;
233  uint8_t *JTSectionAddr = getSectionAddress(JTSectionID);
234  unsigned JTEntryOffset = 0;
235 
236  assert((JTSectionSize % JTEntrySize) == 0 &&
237  "Jump-table section does not contain a whole number of stubs?");
238 
239  for (unsigned i = 0; i < NumJTEntries; ++i) {
240  unsigned SymbolIndex =
241  Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
242  symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
243  ErrorOr<StringRef> IndirectSymbolName = SI->getName();
244  if (std::error_code EC = IndirectSymbolName.getError())
245  report_fatal_error(EC.message());
246  uint8_t *JTEntryAddr = JTSectionAddr + JTEntryOffset;
247  createStubFunction(JTEntryAddr);
248  RelocationEntry RE(JTSectionID, JTEntryOffset + 1,
249  MachO::GENERIC_RELOC_VANILLA, 0, true, 2);
250  addRelocationForSymbol(RE, *IndirectSymbolName);
251  JTEntryOffset += JTEntrySize;
252  }
253  }
254 
255 };
256 }
257 
258 #undef DEBUG_TYPE
259 
260 #endif
RelocationEntry - used to represent relocations internally in the dynamic linker. ...
std::error_code getError() const
Definition: ErrorOr.h:178
void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const
Endian-aware write.
Represents either an error or a value T.
Definition: ErrorOr.h:82
void finalizeSection(const ObjectFile &Obj, unsigned SectionID, const SectionRef &Section)
unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const
void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override
A object file specific relocation resolver.
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.
unsigned findOrEmitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode, ObjSectionToIDMap &LocalSections)
Find Section in LocalSections.
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
unsigned getMaxStubSize() 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.
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...
#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...
std::map< RelocationValueRef, uintptr_t > StubMap
uint32_t getScatteredRelocationValue(const MachO::any_relocation_info &RE) const
relocation_iterator processRelocationRef(unsigned SectionID, relocation_iterator RelI, const ObjectFile &BaseObjT, ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) override
Parses one or more object file relocations (some object files use relocation pairs) and stores it to ...
std::error_code getName(StringRef &Result) const
Definition: ObjectFile.h:362
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...
bool Error(const Twine &Msg)
void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName)
MachO::any_relocation_info getRelocation(DataRefImpl Rel) const
RuntimeDyld::SymbolResolver & Resolver
MachO::section getSection(DataRefImpl DRI) const
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)
uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const
Endian-aware read Read the least significant Size bytes from Src.
bool isText() const
Definition: ObjectFile.h:382
unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const
uint8_t * getSectionAddress(unsigned SectionID) const
static section_iterator getSectionByAddress(const MachOObjectFile &Obj, uint64_t Addr)
unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
uint32_t RelType
RelType - relocation type.
RuntimeDyldMachOI386(RuntimeDyld::MemoryManager &MM, RuntimeDyld::SymbolResolver &Resolver)
uint8_t * createStubFunction(uint8_t *Addr, unsigned AbiVariant=0)
Emits long jump instruction to Addr.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
ErrorOr< StringRef > getName() const
Definition: ObjectFile.h:306
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.
uint64_t LoadAddress
LoadAddress - the address of the section in the target process's memory.
basic_symbol_iterator getSymbolByIndex(unsigned Index) const
unsigned getStubAlignment() 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.
bool isRelocationScattered(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:69