LLVM  15.0.0git
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 
14 #define DEBUG_TYPE "dyld"
15 
16 namespace llvm {
17 
19  : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOI386> {
20 public:
21 
23 
27 
28  unsigned getMaxStubSize() const override { return 0; }
29 
30  unsigned getStubAlignment() override { return 1; }
31 
33  processRelocationRef(unsigned SectionID, relocation_iterator RelI,
34  const ObjectFile &BaseObjT,
35  ObjSectionToIDMap &ObjSectionToID,
36  StubMap &Stubs) override {
37  const MachOObjectFile &Obj =
38  static_cast<const MachOObjectFile &>(BaseObjT);
40  Obj.getRelocation(RelI->getRawDataRefImpl());
41  uint32_t RelType = Obj.getAnyRelocationType(RelInfo);
42 
43  if (Obj.isRelocationScattered(RelInfo)) {
44  if (RelType == MachO::GENERIC_RELOC_SECTDIFF ||
46  return processSECTDIFFRelocation(SectionID, RelI, Obj,
47  ObjSectionToID);
48  else if (RelType == MachO::GENERIC_RELOC_VANILLA)
49  return processScatteredVANILLA(SectionID, RelI, Obj, ObjSectionToID);
50  return make_error<RuntimeDyldError>(("Unhandled I386 scattered relocation "
51  "type: " + Twine(RelType)).str());
52  }
53 
54  switch (RelType) {
58  default:
59  if (RelType > MachO::GENERIC_RELOC_TLV)
60  return make_error<RuntimeDyldError>(("MachO I386 relocation type " +
61  Twine(RelType) +
62  " is out of range").str());
63  break;
64  }
65 
66  RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));
67  RE.Addend = memcpyAddend(RE);
69  if (auto ValueOrErr = getRelocationValueRef(Obj, RelI, RE, ObjSectionToID))
70  Value = *ValueOrErr;
71  else
72  return ValueOrErr.takeError();
73 
74  // Addends for external, PC-rel relocations on i386 point back to the zero
75  // offset. Calculate the final offset from the relocation target instead.
76  // This allows us to use the same logic for both external and internal
77  // relocations in resolveI386RelocationRef.
78  // bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
79  // if (IsExtern && RE.IsPCRel) {
80  // uint64_t RelocAddr = 0;
81  // RelI->getAddress(RelocAddr);
82  // Value.Addend += RelocAddr + 4;
83  // }
84  if (RE.IsPCRel)
85  makeValueAddendPCRel(Value, RelI, 1 << RE.Size);
86 
87  RE.Addend = Value.Offset;
88 
89  if (Value.SymbolName)
90  addRelocationForSymbol(RE, Value.SymbolName);
91  else
92  addRelocationForSection(RE, Value.SectionID);
93 
94  return ++RelI;
95  }
96 
97  void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
99 
100  const SectionEntry &Section = Sections[RE.SectionID];
101  uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
102 
103  if (RE.IsPCRel) {
104  uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
105  Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
106  }
107 
108  switch (RE.RelType) {
110  writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
111  break;
114  uint64_t SectionABase = Sections[RE.Sections.SectionA].getLoadAddress();
115  uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress();
116  assert((Value == SectionABase || Value == SectionBBase) &&
117  "Unexpected SECTDIFF relocation value.");
118  Value = SectionABase - SectionBBase + RE.Addend;
119  writeBytesUnaligned(Value, LocalAddress, 1 << RE.Size);
120  break;
121  }
122  default:
123  llvm_unreachable("Invalid relocation type!");
124  }
125  }
126 
127  Error finalizeSection(const ObjectFile &Obj, unsigned SectionID,
128  const SectionRef &Section) {
129  StringRef Name;
130  if (Expected<StringRef> NameOrErr = Section.getName())
131  Name = *NameOrErr;
132  else
133  consumeError(NameOrErr.takeError());
134 
135  if (Name == "__jump_table")
136  return populateJumpTable(cast<MachOObjectFile>(Obj), Section, SectionID);
137  else if (Name == "__pointers")
138  return populateIndirectSymbolPointersSection(cast<MachOObjectFile>(Obj),
139  Section, SectionID);
140  return Error::success();
141  }
142 
143 private:
145  processSECTDIFFRelocation(unsigned SectionID, relocation_iterator RelI,
146  const ObjectFile &BaseObjT,
147  ObjSectionToIDMap &ObjSectionToID) {
148  const MachOObjectFile &Obj =
149  static_cast<const MachOObjectFile&>(BaseObjT);
151  Obj.getRelocation(RelI->getRawDataRefImpl());
152 
153  SectionEntry &Section = Sections[SectionID];
154  uint32_t RelocType = Obj.getAnyRelocationType(RE);
155  bool IsPCRel = Obj.getAnyRelocationPCRel(RE);
156  unsigned Size = Obj.getAnyRelocationLength(RE);
157  uint64_t Offset = RelI->getOffset();
158  uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
159  unsigned NumBytes = 1 << Size;
160  uint64_t Addend = readBytesUnaligned(LocalAddress, NumBytes);
161 
162  ++RelI;
164  Obj.getRelocation(RelI->getRawDataRefImpl());
165 
166  uint32_t AddrA = Obj.getScatteredRelocationValue(RE);
167  section_iterator SAI = getSectionByAddress(Obj, AddrA);
168  assert(SAI != Obj.section_end() && "Can't find section for address A");
169  uint64_t SectionABase = SAI->getAddress();
170  uint64_t SectionAOffset = AddrA - SectionABase;
171  SectionRef SectionA = *SAI;
172  bool IsCode = SectionA.isText();
173  uint32_t SectionAID = ~0U;
174  if (auto SectionAIDOrErr =
175  findOrEmitSection(Obj, SectionA, IsCode, ObjSectionToID))
176  SectionAID = *SectionAIDOrErr;
177  else
178  return SectionAIDOrErr.takeError();
179 
180  uint32_t AddrB = Obj.getScatteredRelocationValue(RE2);
181  section_iterator SBI = getSectionByAddress(Obj, AddrB);
182  assert(SBI != Obj.section_end() && "Can't find section for address B");
183  uint64_t SectionBBase = SBI->getAddress();
184  uint64_t SectionBOffset = AddrB - SectionBBase;
185  SectionRef SectionB = *SBI;
186  uint32_t SectionBID = ~0U;
187  if (auto SectionBIDOrErr =
188  findOrEmitSection(Obj, SectionB, IsCode, ObjSectionToID))
189  SectionBID = *SectionBIDOrErr;
190  else
191  return SectionBIDOrErr.takeError();
192 
193  // Compute the addend 'C' from the original expression 'A - B + C'.
194  Addend -= AddrA - AddrB;
195 
196  LLVM_DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA
197  << ", AddrB: " << AddrB << ", Addend: " << Addend
198  << ", SectionA ID: " << SectionAID << ", SectionAOffset: "
199  << SectionAOffset << ", SectionB ID: " << SectionBID
200  << ", SectionBOffset: " << SectionBOffset << "\n");
201  RelocationEntry R(SectionID, Offset, RelocType, Addend, SectionAID,
202  SectionAOffset, SectionBID, SectionBOffset,
203  IsPCRel, Size);
204 
205  addRelocationForSection(R, SectionAID);
206 
207  return ++RelI;
208  }
209 
210  // Populate stubs in __jump_table section.
211  Error populateJumpTable(const MachOObjectFile &Obj,
212  const SectionRef &JTSection,
213  unsigned JTSectionID) {
215  MachO::section Sec32 = Obj.getSection(JTSection.getRawDataRefImpl());
216  uint32_t JTSectionSize = Sec32.size;
217  unsigned FirstIndirectSymbol = Sec32.reserved1;
218  unsigned JTEntrySize = Sec32.reserved2;
219  unsigned NumJTEntries = JTSectionSize / JTEntrySize;
220  uint8_t *JTSectionAddr = getSectionAddress(JTSectionID);
221  unsigned JTEntryOffset = 0;
222 
223  if (JTSectionSize % JTEntrySize != 0)
224  return make_error<RuntimeDyldError>("Jump-table section does not contain "
225  "a whole number of stubs?");
226 
227  for (unsigned i = 0; i < NumJTEntries; ++i) {
228  unsigned SymbolIndex =
229  Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
230  symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
231  Expected<StringRef> IndirectSymbolName = SI->getName();
232  if (!IndirectSymbolName)
233  return IndirectSymbolName.takeError();
234  uint8_t *JTEntryAddr = JTSectionAddr + JTEntryOffset;
235  createStubFunction(JTEntryAddr);
236  RelocationEntry RE(JTSectionID, JTEntryOffset + 1,
237  MachO::GENERIC_RELOC_VANILLA, 0, true, 2);
238  addRelocationForSymbol(RE, *IndirectSymbolName);
239  JTEntryOffset += JTEntrySize;
240  }
241 
242  return Error::success();
243  }
244 
245 };
246 }
247 
248 #undef DEBUG_TYPE
249 
250 #endif
i
i
Definition: README.txt:29
llvm::MachO::section
Definition: MachO.h:565
llvm::RuntimeDyldMachO::dumpRelocationToResolve
void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const
Dump information about the relocation entry (RE) and resolved value.
Definition: RuntimeDyldMachO.cpp:148
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::MachO::any_relocation_info
Definition: MachO.h:977
llvm::RelocationEntry::SectionPair::SectionA
uint32_t SectionA
Definition: RuntimeDyldImpl.h:133
llvm::MachO::GENERIC_RELOC_SECTDIFF
@ GENERIC_RELOC_SECTDIFF
Definition: MachO.h:406
llvm::RuntimeDyldImpl::Sections
SectionList Sections
Definition: RuntimeDyldImpl.h:254
llvm::RelocationEntry::IsPCRel
bool IsPCRel
True if this is a PCRel relocation (MachO specific).
Definition: RuntimeDyldImpl.h:145
llvm::RuntimeDyldMachOI386::getMaxStubSize
unsigned getMaxStubSize() const override
Definition: RuntimeDyldMachOI386.h:28
llvm::object::SectionRef::getRawDataRefImpl
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:536
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
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::RuntimeDyldImpl::addRelocationForSection
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)
Definition: RuntimeDyld.cpp:952
llvm::object::MachOObjectFile::getAnyRelocationPCRel
unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const
Definition: MachOObjectFile.cpp:4394
llvm::MachO::section::size
uint32_t size
Definition: MachO.h:569
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::RuntimeDyldImpl::addRelocationForSymbol
void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName)
Definition: RuntimeDyld.cpp:957
llvm::object::MachOObjectFile::getSymbolByIndex
symbol_iterator getSymbolByIndex(unsigned Index) const
Definition: MachOObjectFile.cpp:2570
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1043
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
llvm::object::MachOObjectFile::getSection
Expected< SectionRef > getSection(unsigned SectionIndex) const
Definition: MachOObjectFile.cpp:2000
llvm::RelocationEntry::SectionID
unsigned SectionID
SectionID - the section this relocation points to.
Definition: RuntimeDyldImpl.h:120
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::object::MachOObjectFile::getIndirectSymbolTableEntry
uint32_t getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC, unsigned Index) const
Definition: MachOObjectFile.cpp:4625
llvm::Resolver
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2087
llvm::RuntimeDyldMachO::makeValueAddendPCRel
void makeValueAddendPCRel(RelocationValueRef &Value, const relocation_iterator &RI, unsigned OffsetToNextPC)
Make the RelocationValueRef addend PC-relative.
Definition: RuntimeDyldMachO.cpp:140
llvm::RelocationEntry::Offset
uint64_t Offset
Offset - offset into the section.
Definition: RuntimeDyldImpl.h:123
llvm::RuntimeDyldMachOI386::processRelocationRef
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 ...
Definition: RuntimeDyldMachOI386.h:33
llvm::object::MachOObjectFile::getDysymtabLoadCommand
MachO::dysymtab_command getDysymtabLoadCommand() const
Definition: MachOObjectFile.cpp:4654
llvm::MachO::GENERIC_RELOC_PAIR
@ GENERIC_RELOC_PAIR
Definition: MachO.h:405
llvm::RuntimeDyldMachOCRTPBase
RuntimeDyldMachOTarget - Templated base class for generic MachO linker algorithms and data structures...
Definition: RuntimeDyldMachO.h:145
UNIMPLEMENTED_RELOC
#define UNIMPLEMENTED_RELOC(RelType)
Definition: RuntimeDyldImpl.h:39
llvm::object::MachOObjectFile::getRelocation
MachO::any_relocation_info getRelocation(DataRefImpl Rel) const
Definition: MachOObjectFile.cpp:4584
llvm::RuntimeDyldImpl::getSectionAddress
uint8_t * getSectionAddress(unsigned SectionID) const
Definition: RuntimeDyldImpl.h:471
llvm::MachO::GENERIC_RELOC_VANILLA
@ GENERIC_RELOC_VANILLA
Definition: MachO.h:404
llvm::RuntimeDyldMachO::populateIndirectSymbolPointersSection
Error populateIndirectSymbolPointersSection(const MachOObjectFile &Obj, const SectionRef &PTSection, unsigned PTSectionID)
Definition: RuntimeDyldMachO.cpp:180
llvm::RuntimeDyldImpl::writeBytesUnaligned
void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const
Endian-aware write.
Definition: RuntimeDyld.cpp:725
llvm::RelocationEntry::Addend
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
Definition: RuntimeDyldImpl.h:130
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:381
uint64_t
llvm::object::symbol_iterator
Definition: ObjectFile.h:207
llvm::RelocationEntry::Size
unsigned Size
The size of this relocation (MachO specific).
Definition: RuntimeDyldImpl.h:148
llvm::MachO::section::reserved1
uint32_t reserved1
Definition: MachO.h:575
llvm::SectionEntry
SectionEntry - represents a section emitted into memory by the dynamic linker.
Definition: RuntimeDyldImpl.h:45
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::object::MachOObjectFile::section_end
section_iterator section_end() const override
Definition: MachOObjectFile.cpp:2599
SI
StandardInstrumentations SI(Debug, VerifyEach)
llvm::object::content_iterator
Definition: SymbolicFile.h:69
llvm::RuntimeDyldMachO::getRelocationValueRef
Expected< RelocationValueRef > getRelocationValueRef(const ObjectFile &BaseTObj, const relocation_iterator &RI, const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID)
Construct a RelocationValueRef representing the relocation target.
Definition: RuntimeDyldMachO.cpp:97
llvm::RelocationEntry::SectionPair::SectionB
uint32_t SectionB
Definition: RuntimeDyldImpl.h:134
llvm::MachO::GENERIC_RELOC_TLV
@ GENERIC_RELOC_TLV
Definition: MachO.h:409
llvm::RuntimeDyldMachOI386
Definition: RuntimeDyldMachOI386.h:18
llvm::RuntimeDyldMachOI386::getStubAlignment
unsigned getStubAlignment() override
Definition: RuntimeDyldMachOI386.h:30
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::object::ObjectFile
This class is the base class for all object file types.
Definition: ObjectFile.h:228
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
uint32_t
llvm::RuntimeDyldMachO::processScatteredVANILLA
Expected< relocation_iterator > processScatteredVANILLA(unsigned SectionID, relocation_iterator RelI, const ObjectFile &BaseObjT, RuntimeDyldMachO::ObjSectionToIDMap &ObjSectionToID, bool TargetIsLocalThumbFunc=false)
Process a scattered vanilla relocation.
Definition: RuntimeDyldMachO.cpp:54
llvm::MachO::GENERIC_RELOC_LOCAL_SECTDIFF
@ GENERIC_RELOC_LOCAL_SECTDIFF
Definition: MachO.h:408
llvm::object::MachOObjectFile::isRelocationScattered
bool isRelocationScattered(const MachO::any_relocation_info &RE) const
Definition: MachOObjectFile.cpp:4351
llvm::RuntimeDyldImpl::createStubFunction
uint8_t * createStubFunction(uint8_t *Addr, unsigned AbiVariant=0)
Emits long jump instruction to Addr.
Definition: RuntimeDyld.cpp:976
llvm::MachO::dysymtab_command
Definition: MachO.h:704
llvm::object::MachOObjectFile::getAnyRelocationType
unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const
Definition: MachOObjectFile.cpp:4409
llvm::object::SectionRef::isText
bool isText() const
Whether this section contains instructions.
Definition: ObjectFile.h:488
llvm::JITSymbolResolver
Symbol resolution interface.
Definition: JITSymbol.h:371
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::RuntimeDyldImpl::findOrEmitSection
Expected< unsigned > findOrEmitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode, ObjSectionToIDMap &LocalSections)
Find Section in LocalSections.
Definition: RuntimeDyld.cpp:933
llvm::RuntimeDyldMachOI386::TargetPtrT
uint32_t TargetPtrT
Definition: RuntimeDyldMachOI386.h:22
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::MachO::GENERIC_RELOC_PB_LA_PTR
@ GENERIC_RELOC_PB_LA_PTR
Definition: MachO.h:407
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
llvm::object::MachOObjectFile::getScatteredRelocationValue
uint32_t getScatteredRelocationValue(const MachO::any_relocation_info &RE) const
Definition: MachOObjectFile.cpp:4377
llvm::RelocationEntry::Sections
SectionPair Sections
Definition: RuntimeDyldImpl.h:141
llvm::RuntimeDyldImpl::readBytesUnaligned
uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const
Endian-aware read Read the least significant Size bytes from Src.
Definition: RuntimeDyld.cpp:711
llvm::MachO::section::reserved2
uint32_t reserved2
Definition: MachO.h:576
llvm::RuntimeDyldMachOI386::RuntimeDyldMachOI386
RuntimeDyldMachOI386(RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver)
Definition: RuntimeDyldMachOI386.h:24
llvm::object::MachOObjectFile::getAnyRelocationLength
unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const
Definition: MachOObjectFile.cpp:4401
llvm::RuntimeDyldImpl::StubMap
std::map< RelocationValueRef, uintptr_t > StubMap
Definition: RuntimeDyldImpl.h:286
llvm::RuntimeDyldMachO::getSectionByAddress
static section_iterator getSectionByAddress(const MachOObjectFile &Obj, uint64_t Addr)
Definition: RuntimeDyldMachO.cpp:163
llvm::RelocationValueRef
Definition: RuntimeDyldImpl.h:190
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::RuntimeDyldMachOI386::finalizeSection
Error finalizeSection(const ObjectFile &Obj, unsigned SectionID, const SectionRef &Section)
Definition: RuntimeDyldMachOI386.h:127
llvm::RelocationEntry::RelType
uint32_t RelType
RelType - relocation type.
Definition: RuntimeDyldImpl.h:126
llvm::RuntimeDyldMachOI386::resolveRelocation
void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override
A object file specific relocation resolver.
Definition: RuntimeDyldMachOI386.h:97
llvm::RuntimeDyldImpl::ObjSectionToIDMap
std::map< SectionRef, unsigned > ObjSectionToIDMap
Definition: RuntimeDyldImpl.h:261
llvm::RuntimeDyld::MemoryManager
Memory Management.
Definition: RuntimeDyld.h:92
llvm::RuntimeDyldMachO::memcpyAddend
int64_t memcpyAddend(const RelocationEntry &RE) const
This convenience method uses memcpy to extract a contiguous addend (the addend size and offset are ta...
Definition: RuntimeDyldMachO.cpp:46