LLVM  10.0.0svn
RuntimeDyldMachOI386.h
Go to the documentation of this file.
1 //===---- RuntimeDyldMachOI386.h ---- MachO/I386 specific code. ---*- 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 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOI386_H
10 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOI386_H
11 
12 #include "../RuntimeDyldMachO.h"
13 #include <string>
14 
15 #define DEBUG_TYPE "dyld"
16 
17 namespace llvm {
18 
20  : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOI386> {
21 public:
22 
24 
27  : RuntimeDyldMachOCRTPBase(MM, Resolver) {}
28 
29  unsigned getMaxStubSize() const 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 processScatteredVANILLA(SectionID, RelI, Obj, ObjSectionToID);
51  return make_error<RuntimeDyldError>(("Unhandled I386 scattered relocation "
52  "type: " + Twine(RelType)).str());
53  }
54 
55  switch (RelType) {
59  default:
60  if (RelType > MachO::GENERIC_RELOC_TLV)
61  return make_error<RuntimeDyldError>(("MachO I386 relocation type " +
62  Twine(RelType) +
63  " is out of range").str());
64  break;
65  }
66 
67  RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));
68  RE.Addend = memcpyAddend(RE);
70  if (auto ValueOrErr = getRelocationValueRef(Obj, RelI, RE, ObjSectionToID))
71  Value = *ValueOrErr;
72  else
73  return ValueOrErr.takeError();
74 
75  // Addends for external, PC-rel relocations on i386 point back to the zero
76  // offset. Calculate the final offset from the relocation target instead.
77  // This allows us to use the same logic for both external and internal
78  // relocations in resolveI386RelocationRef.
79  // bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
80  // if (IsExtern && RE.IsPCRel) {
81  // uint64_t RelocAddr = 0;
82  // RelI->getAddress(RelocAddr);
83  // Value.Addend += RelocAddr + 4;
84  // }
85  if (RE.IsPCRel)
86  makeValueAddendPCRel(Value, RelI, 1 << RE.Size);
87 
88  RE.Addend = Value.Offset;
89 
90  if (Value.SymbolName)
92  else
94 
95  return ++RelI;
96  }
97 
98  void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
100 
101  const SectionEntry &Section = Sections[RE.SectionID];
102  uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
103 
104  if (RE.IsPCRel) {
105  uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
106  Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
107  }
108 
109  switch (RE.RelType) {
111  writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
112  break;
115  uint64_t SectionABase = Sections[RE.Sections.SectionA].getLoadAddress();
116  uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress();
117  assert((Value == SectionABase || Value == SectionBBase) &&
118  "Unexpected SECTDIFF relocation value.");
119  Value = SectionABase - SectionBBase + RE.Addend;
120  writeBytesUnaligned(Value, LocalAddress, 1 << RE.Size);
121  break;
122  }
123  default:
124  llvm_unreachable("Invalid relocation type!");
125  }
126  }
127 
128  Error finalizeSection(const ObjectFile &Obj, unsigned SectionID,
129  const SectionRef &Section) {
130  StringRef Name;
131  if (Expected<StringRef> NameOrErr = Section.getName())
132  Name = *NameOrErr;
133  else
134  consumeError(NameOrErr.takeError());
135 
136  if (Name == "__jump_table")
137  return populateJumpTable(cast<MachOObjectFile>(Obj), Section, SectionID);
138  else if (Name == "__pointers")
139  return populateIndirectSymbolPointersSection(cast<MachOObjectFile>(Obj),
140  Section, SectionID);
141  return Error::success();
142  }
143 
144 private:
146  processSECTDIFFRelocation(unsigned SectionID, relocation_iterator RelI,
147  const ObjectFile &BaseObjT,
148  ObjSectionToIDMap &ObjSectionToID) {
149  const MachOObjectFile &Obj =
150  static_cast<const MachOObjectFile&>(BaseObjT);
152  Obj.getRelocation(RelI->getRawDataRefImpl());
153 
154  SectionEntry &Section = Sections[SectionID];
155  uint32_t RelocType = Obj.getAnyRelocationType(RE);
156  bool IsPCRel = Obj.getAnyRelocationPCRel(RE);
157  unsigned Size = Obj.getAnyRelocationLength(RE);
158  uint64_t Offset = RelI->getOffset();
159  uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
160  unsigned NumBytes = 1 << Size;
161  uint64_t Addend = readBytesUnaligned(LocalAddress, NumBytes);
162 
163  ++RelI;
165  Obj.getRelocation(RelI->getRawDataRefImpl());
166 
167  uint32_t AddrA = Obj.getScatteredRelocationValue(RE);
168  section_iterator SAI = getSectionByAddress(Obj, AddrA);
169  assert(SAI != Obj.section_end() && "Can't find section for address A");
170  uint64_t SectionABase = SAI->getAddress();
171  uint64_t SectionAOffset = AddrA - SectionABase;
172  SectionRef SectionA = *SAI;
173  bool IsCode = SectionA.isText();
174  uint32_t SectionAID = ~0U;
175  if (auto SectionAIDOrErr =
176  findOrEmitSection(Obj, SectionA, IsCode, ObjSectionToID))
177  SectionAID = *SectionAIDOrErr;
178  else
179  return SectionAIDOrErr.takeError();
180 
181  uint32_t AddrB = Obj.getScatteredRelocationValue(RE2);
182  section_iterator SBI = getSectionByAddress(Obj, AddrB);
183  assert(SBI != Obj.section_end() && "Can't find section for address B");
184  uint64_t SectionBBase = SBI->getAddress();
185  uint64_t SectionBOffset = AddrB - SectionBBase;
186  SectionRef SectionB = *SBI;
187  uint32_t SectionBID = ~0U;
188  if (auto SectionBIDOrErr =
189  findOrEmitSection(Obj, SectionB, IsCode, ObjSectionToID))
190  SectionBID = *SectionBIDOrErr;
191  else
192  return SectionBIDOrErr.takeError();
193 
194  // Compute the addend 'C' from the original expression 'A - B + C'.
195  Addend -= AddrA - AddrB;
196 
197  LLVM_DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA
198  << ", AddrB: " << AddrB << ", Addend: " << Addend
199  << ", SectionA ID: " << SectionAID << ", SectionAOffset: "
200  << SectionAOffset << ", SectionB ID: " << SectionBID
201  << ", SectionBOffset: " << SectionBOffset << "\n");
202  RelocationEntry R(SectionID, Offset, RelocType, Addend, SectionAID,
203  SectionAOffset, SectionBID, SectionBOffset,
204  IsPCRel, Size);
205 
206  addRelocationForSection(R, SectionAID);
207 
208  return ++RelI;
209  }
210 
211  // Populate stubs in __jump_table section.
212  Error populateJumpTable(const MachOObjectFile &Obj,
213  const SectionRef &JTSection,
214  unsigned JTSectionID) {
216  MachO::section Sec32 = Obj.getSection(JTSection.getRawDataRefImpl());
217  uint32_t JTSectionSize = Sec32.size;
218  unsigned FirstIndirectSymbol = Sec32.reserved1;
219  unsigned JTEntrySize = Sec32.reserved2;
220  unsigned NumJTEntries = JTSectionSize / JTEntrySize;
221  uint8_t *JTSectionAddr = getSectionAddress(JTSectionID);
222  unsigned JTEntryOffset = 0;
223 
224  if (JTSectionSize % JTEntrySize != 0)
225  return make_error<RuntimeDyldError>("Jump-table section does not contain "
226  "a whole number of stubs?");
227 
228  for (unsigned i = 0; i < NumJTEntries; ++i) {
229  unsigned SymbolIndex =
230  Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
231  symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
232  Expected<StringRef> IndirectSymbolName = SI->getName();
233  if (!IndirectSymbolName)
234  return IndirectSymbolName.takeError();
235  uint8_t *JTEntryAddr = JTSectionAddr + JTEntryOffset;
236  createStubFunction(JTEntryAddr);
237  RelocationEntry RE(JTSectionID, JTEntryOffset + 1,
238  MachO::GENERIC_RELOC_VANILLA, 0, true, 2);
239  addRelocationForSymbol(RE, *IndirectSymbolName);
240  JTEntryOffset += JTEntrySize;
241  }
242 
243  return Error::success();
244  }
245 
246 };
247 }
248 
249 #undef DEBUG_TYPE
250 
251 #endif
RelocationEntry - used to represent relocations internally in the dynamic linker. ...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Expected< StringRef > getName() const
Definition: ObjectFile.h:375
unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const
Endian-aware read Read the least significant Size bytes from Src.
uint64_t getLoadAddressWithOffset(unsigned OffsetBytes) const
Return the load address of this section with an offset.
void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override
A object file specific relocation resolver.
uint32_t reserved1
Definition: MachO.h:565
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:221
unsigned getMaxStubSize() const override
uint32_t size
Definition: MachO.h:559
Error takeError()
Take ownership of the stored error.
Definition: Error.h:552
void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const
Endian-aware write.
bool IsPCRel
True if this is a PCRel relocation (MachO specific).
unsigned SectionID
SectionID - the section this relocation points to.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
bool isText() const
Whether this section contains instructions.
Definition: ObjectFile.h:464
std::map< RelocationValueRef, uintptr_t > StubMap
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
int64_t memcpyAddend(const RelocationEntry &RE) const
This convenience method uses memcpy to extract a contiguous addend (the addend size and offset are ta...
symbol_iterator getSymbolByIndex(unsigned Index) const
unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const
RuntimeDyldMachOTarget - Templated base class for generic MachO linker algorithms and data structures...
Expected< RelocationValueRef > getRelocationValueRef(const ObjectFile &BaseTObj, const relocation_iterator &RI, const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID)
Construct a RelocationValueRef representing the relocation target.
void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName)
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...
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)
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:1863
Symbol resolution interface.
Definition: JITSymbol.h:325
Expected< unsigned > findOrEmitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode, ObjSectionToIDMap &LocalSections)
Find Section in LocalSections.
uint8_t * getSectionAddress(unsigned SectionID) const
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:508
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:981
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.
bool isRelocationScattered(const MachO::any_relocation_info &RE) const
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
RuntimeDyldMachOI386(RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver)
Expected< 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 ...
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.
uint8_t * createStubFunction(uint8_t *Addr, unsigned AbiVariant=0)
Emits long jump instruction to Addr.
Expected< StringRef > getName() const
Definition: ObjectFile.h:432
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
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.
unsigned getStubAlignment() override
uint32_t Size
Definition: Profile.cpp:46
SectionEntry - represents a section emitted into memory by the dynamic linker.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
unsigned Size
The size of this relocation (MachO specific).
section_iterator section_end() const override
#define UNIMPLEMENTED_RELOC(RelType)
Error populateIndirectSymbolPointersSection(const MachOObjectFile &Obj, const SectionRef &PTSection, unsigned PTSectionID)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
uint32_t reserved2
Definition: MachO.h:566
void makeValueAddendPCRel(RelocationValueRef &Value, const relocation_iterator &RI, unsigned OffsetToNextPC)
Make the RelocationValueRef addend PC-relative.
#define LLVM_DEBUG(X)
Definition: Debug.h:122
uint32_t getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC, unsigned Index) const
uint32_t getScatteredRelocationValue(const MachO::any_relocation_info &RE) const
Error finalizeSection(const ObjectFile &Obj, unsigned SectionID, const SectionRef &Section)
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