LLVM  14.0.0git
RuntimeDyldMachOX86_64.h
Go to the documentation of this file.
1 //===-- RuntimeDyldMachOX86_64.h ---- MachO/X86_64 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_RUNTIMEDYLDMACHOX86_64_H
10 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOX86_64_H
11 
12 #include "../RuntimeDyldMachO.h"
13 
14 #define DEBUG_TYPE "dyld"
15 
16 namespace llvm {
17 
19  : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOX86_64> {
20 public:
21 
23 
27 
28  unsigned getMaxStubSize() const override { return 8; }
29 
30  unsigned getStubAlignment() override { return 8; }
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 (RelType == MachO::X86_64_RELOC_SUBTRACTOR)
44  return processSubtractRelocation(SectionID, RelI, Obj, ObjSectionToID);
45 
46  assert(!Obj.isRelocationScattered(RelInfo) &&
47  "Scattered relocations not supported on X86_64");
48 
49  RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));
50  RE.Addend = memcpyAddend(RE);
52  if (auto ValueOrErr = getRelocationValueRef(Obj, RelI, RE, ObjSectionToID))
53  Value = *ValueOrErr;
54  else
55  return ValueOrErr.takeError();
56 
57  bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
58  if (!IsExtern && RE.IsPCRel)
59  makeValueAddendPCRel(Value, RelI, 1 << RE.Size);
60 
61  switch (RelType) {
63  default:
64  if (RelType > MachO::X86_64_RELOC_TLV)
65  return make_error<RuntimeDyldError>(("MachO X86_64 relocation type " +
66  Twine(RelType) +
67  " is out of range").str());
68  break;
69  }
70 
71  if (RE.RelType == MachO::X86_64_RELOC_GOT ||
73  processGOTRelocation(RE, Value, Stubs);
74  else {
75  RE.Addend = Value.Offset;
76  if (Value.SymbolName)
77  addRelocationForSymbol(RE, Value.SymbolName);
78  else
79  addRelocationForSection(RE, Value.SectionID);
80  }
81 
82  return ++RelI;
83  }
84 
85  void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
88  uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
89 
90  // If the relocation is PC-relative, the value to be encoded is the
91  // pointer difference.
92  if (RE.IsPCRel) {
93  // FIXME: It seems this value needs to be adjusted by 4 for an effective
94  // PC address. Is that expected? Only for branches, perhaps?
95  uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
96  Value -= FinalAddress + 4;
97  }
98 
99  switch (RE.RelType) {
100  default:
101  llvm_unreachable("Invalid relocation type!");
108  writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
109  break;
111  uint64_t SectionABase = Sections[RE.Sections.SectionA].getLoadAddress();
112  uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress();
113  assert((Value == SectionABase || Value == SectionBBase) &&
114  "Unexpected SUBTRACTOR relocation value.");
115  Value = SectionABase - SectionBBase + RE.Addend;
116  writeBytesUnaligned(Value, LocalAddress, 1 << RE.Size);
117  break;
118  }
119  }
120  }
121 
122  Error finalizeSection(const ObjectFile &Obj, unsigned SectionID,
123  const SectionRef &Section) {
124  return Error::success();
125  }
126 
127 private:
128  void processGOTRelocation(const RelocationEntry &RE,
129  RelocationValueRef &Value, StubMap &Stubs) {
131  assert(RE.IsPCRel);
132  assert(RE.Size == 2);
133  Value.Offset -= RE.Addend;
134  RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
135  uint8_t *Addr;
136  if (i != Stubs.end()) {
137  Addr = Section.getAddressWithOffset(i->second);
138  } else {
139  Stubs[Value] = Section.getStubOffset();
140  uint8_t *GOTEntry = Section.getAddressWithOffset(Section.getStubOffset());
141  RelocationEntry GOTRE(RE.SectionID, Section.getStubOffset(),
142  MachO::X86_64_RELOC_UNSIGNED, Value.Offset, false,
143  3);
144  if (Value.SymbolName)
145  addRelocationForSymbol(GOTRE, Value.SymbolName);
146  else
147  addRelocationForSection(GOTRE, Value.SectionID);
148  Section.advanceStubOffset(8);
149  Addr = GOTEntry;
150  }
151  RelocationEntry TargetRE(RE.SectionID, RE.Offset,
152  MachO::X86_64_RELOC_UNSIGNED, RE.Addend, true, 2);
153  resolveRelocation(TargetRE, (uint64_t)Addr);
154  }
155 
157  processSubtractRelocation(unsigned SectionID, relocation_iterator RelI,
158  const MachOObjectFile &BaseObj,
159  ObjSectionToIDMap &ObjSectionToID) {
160  const MachOObjectFile &Obj =
161  static_cast<const MachOObjectFile&>(BaseObj);
163  Obj.getRelocation(RelI->getRawDataRefImpl());
164 
165  unsigned Size = Obj.getAnyRelocationLength(RE);
166  uint64_t Offset = RelI->getOffset();
167  uint8_t *LocalAddress = Sections[SectionID].getAddressWithOffset(Offset);
168  unsigned NumBytes = 1 << Size;
169  int64_t Addend =
170  SignExtend64(readBytesUnaligned(LocalAddress, NumBytes), NumBytes * 8);
171 
172  unsigned SectionBID = ~0U;
173  uint64_t SectionBOffset = 0;
174 
176  Obj.getRelocation(RelI->getRawDataRefImpl());
177 
178  bool AIsExternal = BaseObj.getPlainRelocationExternal(RelInfo);
179 
180  if (AIsExternal) {
181  Expected<StringRef> SubtrahendNameOrErr = RelI->getSymbol()->getName();
182  if (!SubtrahendNameOrErr)
183  return SubtrahendNameOrErr.takeError();
184  auto SubtrahendI = GlobalSymbolTable.find(*SubtrahendNameOrErr);
185  SectionBID = SubtrahendI->second.getSectionID();
186  SectionBOffset = SubtrahendI->second.getOffset();
187  } else {
188  SectionRef SecB = Obj.getAnyRelocationSection(RelInfo);
189  bool IsCode = SecB.isText();
190  Expected<unsigned> SectionBIDOrErr =
191  findOrEmitSection(Obj, SecB, IsCode, ObjSectionToID);
192  if (!SectionBIDOrErr)
193  return SectionBIDOrErr.takeError();
194  SectionBID = *SectionBIDOrErr;
195  Addend += SecB.getAddress();
196  }
197 
198  ++RelI;
199 
200  unsigned SectionAID = ~0U;
201  uint64_t SectionAOffset = 0;
202 
203  RelInfo = Obj.getRelocation(RelI->getRawDataRefImpl());
204 
205  bool BIsExternal = BaseObj.getPlainRelocationExternal(RelInfo);
206  if (BIsExternal) {
207  Expected<StringRef> MinuendNameOrErr = RelI->getSymbol()->getName();
208  if (!MinuendNameOrErr)
209  return MinuendNameOrErr.takeError();
210  auto MinuendI = GlobalSymbolTable.find(*MinuendNameOrErr);
211  SectionAID = MinuendI->second.getSectionID();
212  SectionAOffset = MinuendI->second.getOffset();
213  } else {
214  SectionRef SecA = Obj.getAnyRelocationSection(RelInfo);
215  bool IsCode = SecA.isText();
216  Expected<unsigned> SectionAIDOrErr =
217  findOrEmitSection(Obj, SecA, IsCode, ObjSectionToID);
218  if (!SectionAIDOrErr)
219  return SectionAIDOrErr.takeError();
220  SectionAID = *SectionAIDOrErr;
221  Addend -= SecA.getAddress();
222  }
223 
225  SectionAID, SectionAOffset, SectionBID, SectionBOffset,
226  false, Size);
227 
228  addRelocationForSection(R, SectionAID);
229 
230  return ++RelI;
231  }
232 
233 };
234 }
235 
236 #undef DEBUG_TYPE
237 
238 #endif
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
i
i
Definition: README.txt:29
llvm::object::MachOObjectFile::getPlainRelocationExternal
bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const
Definition: MachOObjectFile.cpp:4248
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: AllocatorList.h:22
llvm::MachO::any_relocation_info
Definition: MachO.h:976
llvm::RuntimeDyldMachOX86_64::TargetPtrT
uint64_t TargetPtrT
Definition: RuntimeDyldMachOX86_64.h:22
llvm::RelocationEntry::SectionPair::SectionA
uint32_t SectionA
Definition: RuntimeDyldImpl.h:133
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::MachO::X86_64_RELOC_SIGNED_4
@ X86_64_RELOC_SIGNED_4
Definition: MachO.h:482
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
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:80
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::StringMap::find
iterator find(StringRef Key)
Definition: StringMap.h:217
llvm::MachO::X86_64_RELOC_SIGNED_2
@ X86_64_RELOC_SIGNED_2
Definition: MachO.h:481
llvm::RuntimeDyldImpl::addRelocationForSymbol
void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName)
Definition: RuntimeDyld.cpp:957
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:208
llvm::RelocationEntry::SectionID
unsigned SectionID
SectionID - the section this relocation points to.
Definition: RuntimeDyldImpl.h:120
llvm::MachO::X86_64_RELOC_BRANCH
@ X86_64_RELOC_BRANCH
Definition: MachO.h:476
llvm::ARMBuildAttrs::Section
@ Section
Legacy Tags.
Definition: ARMBuildAttributes.h:82
llvm::Resolver
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2012
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::RuntimeDyldMachOX86_64::getMaxStubSize
unsigned getMaxStubSize() const override
Definition: RuntimeDyldMachOX86_64.h:28
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:4467
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:79
llvm::RelocationEntry
RelocationEntry - used to represent relocations internally in the dynamic linker.
Definition: RuntimeDyldImpl.h:117
llvm::object::MachOObjectFile
Definition: MachO.h:262
llvm::MachO::X86_64_RELOC_UNSIGNED
@ X86_64_RELOC_UNSIGNED
Definition: MachO.h:474
llvm::RuntimeDyldMachOX86_64
Definition: RuntimeDyldMachOX86_64.h:18
uint64_t
llvm::RelocationEntry::Size
unsigned Size
The size of this relocation (MachO specific).
Definition: RuntimeDyldImpl.h:148
Addr
uint64_t Addr
Definition: ELFObjHandler.cpp:79
llvm::MachO::X86_64_RELOC_GOT
@ X86_64_RELOC_GOT
Definition: MachO.h:478
llvm::RuntimeDyldMachOX86_64::RuntimeDyldMachOX86_64
RuntimeDyldMachOX86_64(RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver)
Definition: RuntimeDyldMachOX86_64.h:24
llvm::MachO::X86_64_RELOC_GOT_LOAD
@ X86_64_RELOC_GOT_LOAD
Definition: MachO.h:477
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::content_iterator
Definition: SymbolicFile.h:67
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::X86_64_RELOC_SUBTRACTOR
@ X86_64_RELOC_SUBTRACTOR
Definition: MachO.h:479
llvm::RuntimeDyldMachOX86_64::getStubAlignment
unsigned getStubAlignment() override
Definition: RuntimeDyldMachOX86_64.h:30
llvm::object::MachOObjectFile::getAnyRelocationSection
SectionRef getAnyRelocationSection(const MachO::any_relocation_info &RE) const
Definition: MachOObjectFile.cpp:4300
llvm::object::ObjectFile
This class is the base class for all object file types.
Definition: ObjectFile.h:227
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
uint32_t
llvm::object::SectionRef::getAddress
uint64_t getAddress() const
Definition: ObjectFile.h:449
llvm::SignExtend64
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition: MathExtras.h:777
llvm::object::MachOObjectFile::isRelocationScattered
bool isRelocationScattered(const MachO::any_relocation_info &RE) const
Definition: MachOObjectFile.cpp:4234
llvm::MachO::X86_64_RELOC_SIGNED
@ X86_64_RELOC_SIGNED
Definition: MachO.h:475
llvm::object::MachOObjectFile::getAnyRelocationType
unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const
Definition: MachOObjectFile.cpp:4292
llvm::object::SectionRef::isText
bool isText() const
Whether this section contains instructions.
Definition: ObjectFile.h:477
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::MachO::X86_64_RELOC_TLV
@ X86_64_RELOC_TLV
Definition: MachO.h:483
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::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::RuntimeDyldMachOX86_64::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: RuntimeDyldMachOX86_64.h:33
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
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::object::MachOObjectFile::getAnyRelocationLength
unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const
Definition: MachOObjectFile.cpp:4284
llvm::RuntimeDyldImpl::StubMap
std::map< RelocationValueRef, uintptr_t > StubMap
Definition: RuntimeDyldImpl.h:286
llvm::RuntimeDyldMachOX86_64::finalizeSection
Error finalizeSection(const ObjectFile &Obj, unsigned SectionID, const SectionRef &Section)
Definition: RuntimeDyldMachOX86_64.h:122
llvm::RelocationValueRef
Definition: RuntimeDyldImpl.h:190
llvm::MachO::X86_64_RELOC_SIGNED_1
@ X86_64_RELOC_SIGNED_1
Definition: MachO.h:480
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::RelocationEntry::RelType
uint32_t RelType
RelType - relocation type.
Definition: RuntimeDyldImpl.h:126
llvm::RuntimeDyldImpl::GlobalSymbolTable
RTDyldSymbolTable GlobalSymbolTable
Definition: RuntimeDyldImpl.h:264
llvm::RuntimeDyldImpl::ObjSectionToIDMap
std::map< SectionRef, unsigned > ObjSectionToIDMap
Definition: RuntimeDyldImpl.h:261
llvm::RuntimeDyld::MemoryManager
Memory Management.
Definition: RuntimeDyld.h:92
llvm::RuntimeDyldMachOX86_64::resolveRelocation
void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override
A object file specific relocation resolver.
Definition: RuntimeDyldMachOX86_64.h:85
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