LLVM  10.0.0
RuntimeDyldCOFFAArch64.h
Go to the documentation of this file.
1 //===-- RuntimeDyldCOFFAArch64.h --- COFF/AArch64 specific code ---*- C++
2 //-*-===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // COFF AArch64 support for MC-JIT runtime dynamic linker.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFFAARCH64_H
15 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFFAARCH64_H
16 
17 #include "../RuntimeDyldCOFF.h"
18 #include "llvm/BinaryFormat/COFF.h"
19 #include "llvm/Object/COFF.h"
20 #include "llvm/Support/Endian.h"
21 
22 #define DEBUG_TYPE "dyld"
23 
24 using namespace llvm::support::endian;
25 
26 namespace llvm {
27 
28 // This relocation type is used for handling long branch instruction
29 // throught the Stub.
30 enum InternalRelocationType : unsigned {
32 };
33 
34 static void add16(uint8_t *p, int16_t v) { write16le(p, read16le(p) + v); }
35 static void or32le(void *P, int32_t V) { write32le(P, read32le(P) | V); }
36 
37 static void write32AArch64Imm(uint8_t *T, uint64_t imm, uint32_t rangeLimit) {
38  uint32_t orig = read32le(T);
39  orig &= ~(0xFFF << 10);
40  write32le(T, orig | ((imm & (0xFFF >> rangeLimit)) << 10));
41 }
42 
43 static void write32AArch64Ldr(uint8_t *T, uint64_t imm) {
44  uint32_t orig = read32le(T);
45  uint32_t size = orig >> 30;
46  // 0x04000000 indicates SIMD/FP registers
47  // 0x00800000 indicates 128 bit
48  if ((orig & 0x04800000) == 0x04800000)
49  size += 4;
50  if ((imm & ((1 << size) - 1)) != 0)
51  assert(0 && "misaligned ldr/str offset");
52  write32AArch64Imm(T, imm >> size, size);
53 }
54 
55 static void write32AArch64Addr(void *T, uint64_t s, uint64_t p, int shift) {
56  uint64_t Imm = (s >> shift) - (p >> shift);
57  uint32_t ImmLo = (Imm & 0x3) << 29;
58  uint32_t ImmHi = (Imm & 0x1FFFFC) << 3;
59  uint64_t Mask = (0x3 << 29) | (0x1FFFFC << 3);
60  write32le(T, (read32le(T) & ~Mask) | ImmLo | ImmHi);
61 }
62 
64 
65 private:
66  // When a module is loaded we save the SectionID of the unwind
67  // sections in a table until we receive a request to register all
68  // unregisteredEH frame sections with the memory manager.
69  SmallVector<SID, 2> UnregisteredEHFrameSections;
70  SmallVector<SID, 2> RegisteredEHFrameSections;
71  uint64_t ImageBase;
72 
73  // Fake an __ImageBase pointer by returning the section with the lowest adress
74  uint64_t getImageBase() {
75  if (!ImageBase) {
77  for (const SectionEntry &Section : Sections)
78  // The Sections list may contain sections that weren't loaded for
79  // whatever reason: they may be debug sections, and ProcessAllSections
80  // is false, or they may be sections that contain 0 bytes. If the
81  // section isn't loaded, the load address will be 0, and it should not
82  // be included in the ImageBase calculation.
83  if (Section.getLoadAddress() != 0)
84  ImageBase = std::min(ImageBase, Section.getLoadAddress());
85  }
86  return ImageBase;
87  }
88 
89 public:
92  : RuntimeDyldCOFF(MM, Resolver), ImageBase(0) {}
93 
94  unsigned getStubAlignment() override { return 8; }
95 
96  unsigned getMaxStubSize() const override { return 20; }
97 
98  std::tuple<uint64_t, uint64_t, uint64_t>
99  generateRelocationStub(unsigned SectionID, StringRef TargetName,
100  uint64_t Offset, uint64_t RelType, uint64_t Addend,
101  StubMap &Stubs) {
102  uintptr_t StubOffset;
103  SectionEntry &Section = Sections[SectionID];
104 
105  RelocationValueRef OriginalRelValueRef;
106  OriginalRelValueRef.SectionID = SectionID;
107  OriginalRelValueRef.Offset = Offset;
108  OriginalRelValueRef.Addend = Addend;
109  OriginalRelValueRef.SymbolName = TargetName.data();
110 
111  auto Stub = Stubs.find(OriginalRelValueRef);
112  if (Stub == Stubs.end()) {
113  LLVM_DEBUG(dbgs() << " Create a new stub function for "
114  << TargetName.data() << "\n");
115 
116  StubOffset = Section.getStubOffset();
117  Stubs[OriginalRelValueRef] = StubOffset;
118  createStubFunction(Section.getAddressWithOffset(StubOffset));
119  Section.advanceStubOffset(getMaxStubSize());
120  } else {
121  LLVM_DEBUG(dbgs() << " Stub function found for " << TargetName.data()
122  << "\n");
123  StubOffset = Stub->second;
124  }
125 
126  // Resolve original relocation to stub function.
127  const RelocationEntry RE(SectionID, Offset, RelType, Addend);
128  resolveRelocation(RE, Section.getLoadAddressWithOffset(StubOffset));
129 
130  // adjust relocation info so resolution writes to the stub function
131  // Here an internal relocation type is used for resolving long branch via
132  // stub instruction.
133  Addend = 0;
134  Offset = StubOffset;
136 
137  return std::make_tuple(Offset, RelType, Addend);
138  }
139 
142  const object::ObjectFile &Obj,
143  ObjSectionToIDMap &ObjSectionToID,
144  StubMap &Stubs) override {
145 
146  auto Symbol = RelI->getSymbol();
147  if (Symbol == Obj.symbol_end())
148  report_fatal_error("Unknown symbol in relocation");
149 
150  Expected<StringRef> TargetNameOrErr = Symbol->getName();
151  if (!TargetNameOrErr)
152  return TargetNameOrErr.takeError();
153  StringRef TargetName = *TargetNameOrErr;
154 
155  auto SectionOrErr = Symbol->getSection();
156  if (!SectionOrErr)
157  return SectionOrErr.takeError();
158  auto Section = *SectionOrErr;
159 
160  uint64_t RelType = RelI->getType();
161  uint64_t Offset = RelI->getOffset();
162 
163  // If there is no section, this must be an external reference.
164  const bool IsExtern = Section == Obj.section_end();
165 
166  // Determine the Addend used to adjust the relocation value.
167  uint64_t Addend = 0;
168  SectionEntry &AddendSection = Sections[SectionID];
169  uintptr_t ObjTarget = AddendSection.getObjAddress() + Offset;
170  uint8_t *Displacement = (uint8_t *)ObjTarget;
171 
172  switch (RelType) {
177  Addend = read32le(Displacement);
178  break;
180  uint32_t orig = read32le(Displacement);
181  Addend = (orig & 0x03FFFFFF) << 2;
182 
183  if (IsExtern)
184  std::tie(Offset, RelType, Addend) = generateRelocationStub(
185  SectionID, TargetName, Offset, RelType, Addend, Stubs);
186  break;
187  }
189  uint32_t orig = read32le(Displacement);
190  Addend = (orig & 0x00FFFFE0) >> 3;
191  break;
192  }
194  uint32_t orig = read32le(Displacement);
195  Addend = (orig & 0x000FFFE0) >> 3;
196  break;
197  }
200  uint32_t orig = read32le(Displacement);
201  Addend = ((orig >> 29) & 0x3) | ((orig >> 3) & 0x1FFFFC);
202  break;
203  }
206  uint32_t orig = read32le(Displacement);
207  Addend = ((orig >> 10) & 0xFFF);
208  break;
209  }
211  Addend = read64le(Displacement);
212  break;
213  }
214  default:
215  break;
216  }
217 
218 #if !defined(NDEBUG)
219  SmallString<32> RelTypeName;
220  RelI->getTypeName(RelTypeName);
221 
222  LLVM_DEBUG(dbgs() << "\t\tIn Section " << SectionID << " Offset " << Offset
223  << " RelType: " << RelTypeName << " TargetName: "
224  << TargetName << " Addend " << Addend << "\n");
225 #endif
226 
227  unsigned TargetSectionID = -1;
228  if (IsExtern) {
229  RelocationEntry RE(SectionID, Offset, RelType, Addend);
230  addRelocationForSymbol(RE, TargetName);
231  } else {
232  if (auto TargetSectionIDOrErr = findOrEmitSection(
233  Obj, *Section, Section->isText(), ObjSectionToID)) {
234  TargetSectionID = *TargetSectionIDOrErr;
235  } else
236  return TargetSectionIDOrErr.takeError();
237 
238  uint64_t TargetOffset = getSymbolOffset(*Symbol);
239  RelocationEntry RE(SectionID, Offset, RelType, TargetOffset + Addend);
240  addRelocationForSection(RE, TargetSectionID);
241  }
242  return ++RelI;
243  }
244 
245  void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
246  const auto Section = Sections[RE.SectionID];
247  uint8_t *Target = Section.getAddressWithOffset(RE.Offset);
248  uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
249 
250  switch (RE.RelType) {
251  default:
252  llvm_unreachable("unsupported relocation type");
254  // This relocation is ignored.
255  break;
256  }
258  // The page base of the target, for ADRP instruction.
259  Value += RE.Addend;
260  write32AArch64Addr(Target, Value, FinalAddress, 12);
261  break;
262  }
264  // The 12-bit relative displacement to the target, for instruction ADR
265  Value += RE.Addend;
266  write32AArch64Addr(Target, Value, FinalAddress, 0);
267  break;
268  }
270  // The 12-bit page offset of the target,
271  // for instructions ADD/ADDS (immediate) with zero shift.
272  Value += RE.Addend;
273  write32AArch64Imm(Target, Value & 0xFFF, 0);
274  break;
275  }
277  // The 12-bit page offset of the target,
278  // for instruction LDR (indexed, unsigned immediate).
279  Value += RE.Addend;
280  write32AArch64Ldr(Target, Value & 0xFFF);
281  break;
282  }
284  // The 32-bit VA of the target.
285  uint32_t VA = Value + RE.Addend;
286  write32le(Target, VA);
287  break;
288  }
290  // The target's 32-bit RVA.
291  uint64_t RVA = Value + RE.Addend - getImageBase();
292  write32le(Target, RVA);
293  break;
294  }
296  // Encode the immadiate value for generated Stub instruction (MOVZ)
297  or32le(Target + 12, ((Value + RE.Addend) & 0xFFFF) << 5);
298  or32le(Target + 8, ((Value + RE.Addend) & 0xFFFF0000) >> 11);
299  or32le(Target + 4, ((Value + RE.Addend) & 0xFFFF00000000) >> 27);
300  or32le(Target + 0, ((Value + RE.Addend) & 0xFFFF000000000000) >> 43);
301  break;
302  }
304  // The 26-bit relative displacement to the target, for B and BL
305  // instructions.
306  uint64_t PCRelVal = Value + RE.Addend - FinalAddress;
307  assert(isInt<28>(PCRelVal) && "Branch target is out of range.");
308  write32le(Target, (read32le(Target) & ~(0x03FFFFFF)) |
309  (PCRelVal & 0x0FFFFFFC) >> 2);
310  break;
311  }
313  // The 19-bit offset to the relocation target,
314  // for conditional B instruction.
315  uint64_t PCRelVal = Value + RE.Addend - FinalAddress;
316  assert(isInt<21>(PCRelVal) && "Branch target is out of range.");
317  write32le(Target, (read32le(Target) & ~(0x00FFFFE0)) |
318  (PCRelVal & 0x001FFFFC) << 3);
319  break;
320  }
322  // The 14-bit offset to the relocation target,
323  // for instructions TBZ and TBNZ.
324  uint64_t PCRelVal = Value + RE.Addend - FinalAddress;
325  assert(isInt<16>(PCRelVal) && "Branch target is out of range.");
326  write32le(Target, (read32le(Target) & ~(0x000FFFE0)) |
327  (PCRelVal & 0x0000FFFC) << 3);
328  break;
329  }
331  // The 64-bit VA of the relocation target.
332  write64le(Target, Value + RE.Addend);
333  break;
334  }
336  // 16-bit section index of the section that contains the target.
337  assert(static_cast<uint32_t>(RE.SectionID) <= UINT16_MAX &&
338  "relocation overflow");
339  add16(Target, RE.SectionID);
340  break;
341  }
343  // 32-bit offset of the target from the beginning of its section.
344  assert(static_cast<int64_t>(RE.Addend) <= INT32_MAX &&
345  "Relocation overflow");
346  assert(static_cast<int64_t>(RE.Addend) >= INT32_MIN &&
347  "Relocation underflow");
348  write32le(Target, RE.Addend);
349  break;
350  }
352  // The 32-bit relative address from the byte following the relocation.
353  uint64_t Result = Value - FinalAddress - 4;
354  write32le(Target, Result + RE.Addend);
355  break;
356  }
357  }
358  }
359 
360  void registerEHFrames() override {}
361 };
362 
363 } // End namespace llvm
364 
365 #endif
RelocationEntry - used to represent relocations internally in the dynamic linker. ...
static void add16(uint8_t *p, int16_t v)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
This class represents lattice values for constants.
Definition: AllocatorList.h:23
uint64_t getLoadAddressWithOffset(unsigned OffsetBytes) const
Return the load address of this section with an offset.
This class is the base class for all object file types.
Definition: ObjectFile.h:223
void write32le(void *P, uint32_t V)
Definition: Endian.h:418
constexpr bool isInt< 16 >(int64_t x)
Definition: MathExtras.h:342
uint16_t read16le(const void *P)
Definition: Endian.h:382
Error takeError()
Take ownership of the stored error.
Definition: Error.h:552
unsigned SectionID
SectionID - the section this relocation points to.
void write16le(void *P, uint16_t V)
Definition: Endian.h:417
std::map< RelocationValueRef, uintptr_t > StubMap
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
static void or32le(void *P, int32_t V)
#define P(N)
std::tuple< uint64_t, uint64_t, uint64_t > generateRelocationStub(unsigned SectionID, StringRef TargetName, uint64_t Offset, uint64_t RelType, uint64_t Addend, StubMap &Stubs)
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:1870
Symbol resolution interface.
Definition: JITSymbol.h:343
virtual basic_symbol_iterator symbol_end() const =0
uintptr_t getObjAddress() const
static void write32AArch64Imm(uint8_t *T, uint64_t imm, uint32_t rangeLimit)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1163
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:390
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
uint32_t RelType
RelType - relocation type.
Expected< object::relocation_iterator > processRelocationRef(unsigned SectionID, object::relocation_iterator RelI, const object::ObjectFile &Obj, ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) override
Parses one or more object file relocations (some object files use relocation pairs) and stores it to ...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Target - Wrapper for Target specific information.
uintptr_t getStubOffset() const
static void write32AArch64Ldr(uint8_t *T, uint64_t imm)
uint64_t Offset
Offset - offset into the section.
uint64_t read64le(const void *P)
Definition: Endian.h:384
void write64le(void *P, uint64_t V)
Definition: Endian.h:419
virtual section_iterator section_end() const =0
std::map< SectionRef, unsigned > ObjSectionToIDMap
uint8_t * getAddressWithOffset(unsigned OffsetBytes) const
Return the address of this section with an offset.
static void write32AArch64Addr(void *T, uint64_t s, uint64_t p, int shift)
uint32_t read32le(const void *P)
Definition: Endian.h:383
SectionEntry - represents a section emitted into memory by the dynamic linker.
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:143
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override
A object file specific relocation resolver.
RuntimeDyldCOFFAArch64(RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver)
LLVM Value Representation.
Definition: Value.h:74
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
void advanceStubOffset(unsigned StubSize)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
#define LLVM_DEBUG(X)
Definition: Debug.h:122
unsigned getMaxStubSize() const override