LLVM: lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOARM_H

10#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOARM_H

11

13

14#define DEBUG_TYPE "dyld"

15

16namespace llvm {

17

20private:

22

23public:

24

26

30

32

34

37 if (!Flags)

38 return Flags.takeError();

40 return Flags;

41 }

42

46 Addr |= 0x1;

47 return Addr;

48 }

49

51 auto TargetObjAddr = Sections[SectionID].getObjAddress() + Offset;

53 auto &Entry = KV.second;

54 auto SymbolObjAddr =

55 Sections[Entry.getSectionID()].getObjAddress() + Entry.getOffset();

56 if (TargetObjAddr == SymbolObjAddr)

58 }

59 return false;

60 }

61

64 uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);

65

67 default:

71 Temp &= 0x00ffffff;

72

74 }

75

77

78

79

80

82 if ((HighInsn & 0xf800) != 0xf000)

84 "(BR22 high bits)",

86

88 if ((LowInsn & 0xf800) != 0xf800)

90 "(BR22 low bits)",

92

94 ((LowInsn & 0x7ff) << 1));

95 }

96 }

97 }

98

103 StubMap &Stubs) override {

108 uint32_t RelType = Obj.getAnyRelocationType(RelInfo);

109

110

111

112 bool TargetIsLocalThumbFunc = false;

113 if (Obj.getPlainRelocationExternal(RelInfo)) {

116 if (auto TargetNameOrErr = Symbol->getName())

117 TargetName = *TargetNameOrErr;

118 else

119 return TargetNameOrErr.takeError();

120

121

122

123

126 TargetIsLocalThumbFunc =

127 EntryItr->second.getFlags().getTargetFlags() &

129 }

130 }

131

132 if (Obj.isRelocationScattered(RelInfo)) {

134 return processHALFSECTDIFFRelocation(SectionID, RelI, Obj,

135 ObjSectionToID);

138 TargetIsLocalThumbFunc);

139 else

140 return ++RelI;

141 }

142

143

144 switch (RelType) {

151 default:

155 " is out of range").str());

156 break;

157 }

158

161 RE.Addend = *AddendOrErr;

162 else

163 return AddendOrErr.takeError();

165

168 Value = *ValueOrErr;

169 else

170 return ValueOrErr.takeError();

171

172

173

174

176 Value.IsStubThumb = true;

177

181

182

183

187

190 processBranchRelocation(RE, Value, Stubs);

191 else {

193 if (Value.SymbolName)

195 else

197 }

198

199 return ++RelI;

200 }

201

205 uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);

206

207

208

210 uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);

211 Value -= FinalAddress;

212

213

215 }

216

221 assert((HighInsn & 0xf800) == 0xf000 &&

222 "Unrecognized thumb branch encoding (BR22 high bits)");

223 HighInsn = (HighInsn & 0xf800) | ((Value >> 12) & 0x7ff);

224

226 assert((LowInsn & 0xf800) == 0xf800 &&

227 "Unrecognized thumb branch encoding (BR22 low bits)");

228 LowInsn = (LowInsn & 0xf800) | ((Value >> 1) & 0x7ff);

229

232 break;

233 }

234

239 break;

241

242

244

246

248

249

250

251

252

255

256 break;

257 }

262 "Unexpected HALFSECTDIFF relocation value.");

263 Value = SectionABase - SectionBBase + RE.Addend;

264 if (RE.Size & 0x1)

266

267 bool IsThumb = RE.Size & 0x2;

268

270

272

273 if (IsThumb)

274 Insn = (Insn & 0x8f00fbf0) | ((Value & 0xf000) >> 12) |

275 ((Value & 0x0800) >> 1) | ((Value & 0x0700) << 20) |

276 ((Value & 0x00ff) << 16);

277 else

278 Insn = (Insn & 0xfff0f000) | ((Value & 0xf000) << 4) | (Value & 0x0fff);

280 break;

281 }

282

283 default:

285 }

286 }

287

292 Name = *NameOrErr;

293 else

295

296 if (Name == "__nl_symbol_ptr")

298 Section, SectionID);

300 }

301

302private:

303

307

308

310 RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);

312 if (i != Stubs.end()) {

313 Addr = Section.getAddressWithOffset(i->second);

314 } else {

315

316 assert(Section.getStubOffset() % 4 == 0 && "Misaligned stub");

317 Stubs[Value] = Section.getStubOffset();

320 StubOpcode = 0xe51ff004;

322 StubOpcode = 0xf000f8df;

323 else

325 Addr = Section.getAddressWithOffset(Section.getStubOffset());

327 uint8_t *StubTargetAddr = Addr + 4;

329 RE.SectionID, StubTargetAddr - Section.getAddress(),

332 if (Value.SymbolName)

334 else

337 }

341 }

342

343 Expected<relocation_iterator>

344 processHALFSECTDIFFRelocation(unsigned SectionID, relocation_iterator RelI,

347 const MachOObjectFile &MachO =

348 static_cast<const MachOObjectFile&>(BaseTObj);

349 MachO::any_relocation_info RE =

351

352

353

354

355

357 bool IsThumb = HalfDiffKindBits & 0x2;

358

363 uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);

364 int64_t Immediate = readBytesUnaligned(LocalAddress, 4);

365

366 if (IsThumb)

367 Immediate = ((Immediate & 0x0000000f) << 12) |

368 ((Immediate & 0x00000400) << 1) |

369 ((Immediate & 0x70000000) >> 20) |

370 ((Immediate & 0x00ff0000) >> 16);

371 else

372 Immediate = ((Immediate >> 4) & 0xf000) | (Immediate & 0xfff);

373

374 ++RelI;

375 MachO::any_relocation_info RE2 =

379 assert(SAI != MachO.section_end() && "Can't find section for address A");

380 uint64_t SectionABase = SAI->getAddress();

381 uint64_t SectionAOffset = AddrA - SectionABase;

382 SectionRef SectionA = *SAI;

383 bool IsCode = SectionA.isText();

384 uint32_t SectionAID = ~0U;

385 if (auto SectionAIDOrErr =

387 SectionAID = *SectionAIDOrErr;

388 else

389 return SectionAIDOrErr.takeError();

390

393 assert(SBI != MachO.section_end() && "Can't find section for address B");

394 uint64_t SectionBBase = SBI->getAddress();

395 uint64_t SectionBOffset = AddrB - SectionBBase;

396 SectionRef SectionB = *SBI;

397 uint32_t SectionBID = ~0U;

398 if (auto SectionBIDOrErr =

400 SectionBID = *SectionBIDOrErr;

401 else

402 return SectionBIDOrErr.takeError();

403

405 unsigned Shift = (HalfDiffKindBits & 0x1) ? 16 : 0;

406 uint32_t FullImmVal = (Immediate << Shift) | (OtherHalf << (16 - Shift));

407 int64_t Addend = FullImmVal - (AddrA - AddrB);

408

409

410

411

412 LLVM_DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA

413 << ", AddrB: " << AddrB << ", Addend: " << Addend

414 << ", SectionA ID: " << SectionAID << ", SectionAOffset: "

415 << SectionAOffset << ", SectionB ID: " << SectionBID

416 << ", SectionBOffset: " << SectionBOffset << "\n");

417 RelocationEntry R(SectionID, Offset, RelocType, Addend, SectionAID,

418 SectionAOffset, SectionBID, SectionBOffset, IsPCRel,

419 HalfDiffKindBits);

420

422

423 return ++RelI;

424 }

425

426};

427}

428

429#undef DEBUG_TYPE

430

431#endif

assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")

#define UNIMPLEMENTED_RELOC(RelType)

static LLVM_ABI ARMJITSymbolFlags fromObjectSymbol(const object::SymbolRef &Symbol)

Lightweight error class with error context and mandatory checking.

static ErrorSuccess success()

Create a success value.

Tagged union holding either a T or a Error.

Flags for symbols in the JIT.

Symbol resolution interface.

RelocationEntry - used to represent relocations internally in the dynamic linker.

unsigned Size

The size of this relocation (MachO specific).

uint32_t RelType

RelType - relocation type.

uint64_t Offset

Offset - offset into the section.

bool IsPCRel

True if this is a PCRel relocation (MachO specific).

int64_t Addend

Addend - the relocation addend encoded in the instruction itself.

unsigned SectionID

SectionID - the section this relocation points to.

Interface for looking up the initializer for a variable name, used by Init::resolveReferences.

std::map< SectionRef, unsigned > ObjSectionToIDMap

std::map< RelocationValueRef, uintptr_t > StubMap

void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName)

void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)

Expected< unsigned > findOrEmitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode, ObjSectionToIDMap &LocalSections)

Find Section in LocalSections.

void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const

Endian-aware write.

uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const

Endian-aware read Read the least significant Size bytes from Src.

RTDyldSymbolTable GlobalSymbolTable

virtual Expected< JITSymbolFlags > getJITSymbolFlags(const SymbolRef &Sym)

Generate JITSymbolFlags from a libObject symbol.

uint64_t modifyAddressBasedOnFlags(uint64_t Addr, JITSymbolFlags Flags) const override

Modify the given target address based on the given symbol flags.

Definition RuntimeDyldMachOARM.h:43

Error finalizeSection(const ObjectFile &Obj, unsigned SectionID, const SectionRef &Section)

Definition RuntimeDyldMachOARM.h:288

RuntimeDyldMachOARM(RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver)

Definition RuntimeDyldMachOARM.h:27

void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override

A object file specific relocation resolver.

Definition RuntimeDyldMachOARM.h:202

Expected< int64_t > decodeAddend(const RelocationEntry &RE) const

Definition RuntimeDyldMachOARM.h:62

Expected< JITSymbolFlags > getJITSymbolFlags(const SymbolRef &SR) override

Generate JITSymbolFlags from a libObject symbol.

Definition RuntimeDyldMachOARM.h:35

bool isAddrTargetThumb(unsigned SectionID, uint64_t Offset)

Definition RuntimeDyldMachOARM.h:50

uint32_t TargetPtrT

Definition RuntimeDyldMachOARM.h:25

Align getStubAlignment() override

Definition RuntimeDyldMachOARM.h:33

unsigned getMaxStubSize() const override

Definition RuntimeDyldMachOARM.h:31

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 RuntimeDyldMachOARM.h:100

RuntimeDyldMachOCRTPBase(RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)

static section_iterator getSectionByAddress(const MachOObjectFile &Obj, uint64_t Addr)

int64_t memcpyAddend(const RelocationEntry &RE) const

This convenience method uses memcpy to extract a contiguous addend (the addend size and offset are ta...

Error populateIndirectSymbolPointersSection(const MachOObjectFile &Obj, const SectionRef &PTSection, unsigned PTSectionID)

void makeValueAddendPCRel(RelocationValueRef &Value, const relocation_iterator &RI, unsigned OffsetToNextPC)

Make the RelocationValueRef addend PC-relative.

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...

Expected< RelocationValueRef > getRelocationValueRef(const ObjectFile &BaseTObj, const relocation_iterator &RI, const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID)

Construct a RelocationValueRef representing the relocation target.

Expected< relocation_iterator > processScatteredVANILLA(unsigned SectionID, relocation_iterator RelI, const ObjectFile &BaseObjT, RuntimeDyldMachO::ObjSectionToIDMap &ObjSectionToID, bool TargetIsLocalThumbFunc=false)

Process a scattered vanilla relocation.

void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const

Dump information about the relocation entry (RE) and resolved value.

SectionEntry - represents a section emitted into memory by the dynamic linker.

StringRef - Represent a constant reference to a string, i.e.

Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...

LLVM Value Representation.

section_iterator section_end() const override

uint32_t getScatteredRelocationValue(const MachO::any_relocation_info &RE) const

unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const

unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const

unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const

MachO::any_relocation_info getRelocation(DataRefImpl Rel) const

unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const

This class is the base class for all object file types.

uint64_t getOffset() const

symbol_iterator getSymbol() const

DataRefImpl getRawDataRefImpl() const

This is a value type class that represents a single section in the list of sections in the object fil...

uint64_t getAddress() const

bool isText() const

Whether this section contains instructions.

This is a value type class that represents a single symbol in the list of symbols in the object file.

#define llvm_unreachable(msg)

Marks that the current location is not supposed to be reachable.

@ ARM_RELOC_LOCAL_SECTDIFF

@ ARM_RELOC_HALF_SECTDIFF

content_iterator< SectionRef > section_iterator

content_iterator< RelocationRef > relocation_iterator

This is an optimization pass for GlobalISel generic memory operations.

LLVM_ABI std::error_code inconvertibleErrorCode()

The value returned by this function can be returned from convertToErrorCode for Error values where no...

LLVM_ABI raw_ostream & dbgs()

dbgs() - This returns a reference to a raw_ostream for debugging messages.

Error make_error(ArgTs &&... Args)

Make a Error instance representing failure using the given error info type.

constexpr int32_t SignExtend32(uint32_t X)

Sign-extend the number in the bottom B bits of X to a 32-bit integer.

decltype(auto) cast(const From &Val)

cast - Return the argument parameter cast to the specified type.

constexpr int64_t SignExtend64(uint64_t x)

Sign-extend the number in the bottom B bits of X to a 64-bit integer.

void consumeError(Error Err)

Consume a Error without doing anything.

This struct is a compact representation of a valid (non-zero power of two) alignment.