12#include "llvm/Config/config.h"
16#define DEBUG_TYPE "jitlink"
25 : EHFrameSectionName(EHFrameSectionName), PointerSize(PointerSize),
26 Pointer32(Pointer32), Pointer64(Pointer64), Delta32(Delta32),
27 Delta64(Delta64), NegDelta32(NegDelta32) {}
30 auto *EHFrame =
G.findSectionByName(EHFrameSectionName);
34 dbgs() <<
"EHFrameEdgeFixer: No " << EHFrameSectionName
35 <<
" section in \"" <<
G.getName() <<
"\". Nothing to do.\n";
41 if (
G.getPointerSize() != 4 &&
G.getPointerSize() != 8)
42 return make_error<JITLinkError>(
43 "EHFrameEdgeFixer only supports 32 and 64 bit targets");
46 dbgs() <<
"EHFrameEdgeFixer: Processing " << EHFrameSectionName <<
" in \""
47 <<
G.getName() <<
"\"...\n";
54 for (
auto &Sec :
G.sections()) {
57 for (
auto *
Sym : Sec.symbols()) {
58 auto &CurSym = PC.AddrToSym[
Sym->getAddress()];
59 if (!CurSym || (std::make_tuple(
Sym->getLinkage(),
Sym->getScope(),
60 !
Sym->hasName(),
Sym->getName()) <
61 std::make_tuple(CurSym->getLinkage(), CurSym->getScope(),
62 !CurSym->hasName(), CurSym->getName())))
65 if (
auto Err = PC.AddrToBlock.addBlocks(Sec.blocks(),
72 std::vector<Block *> EHFrameBlocks;
73 for (
auto *
B : EHFrame->blocks())
74 EHFrameBlocks.push_back(
B);
76 return LHS->getAddress() <
RHS->getAddress();
80 for (
auto *
B : EHFrameBlocks)
81 if (
auto Err = processBlock(PC, *
B))
90 if (
auto Err = R.readInteger(
Length))
91 return std::move(Err);
99 if (
auto Err = R.readInteger(ExtendedLength))
100 return std::move(Err);
102 if (ExtendedLength > std::numeric_limits<size_t>::max())
103 return make_error<JITLinkError>(
104 "In CFI record at " +
105 formatv(
"{0:x}",
B.getAddress() + R.getOffset() - 12) +
106 ", extended length of " +
formatv(
"{0:x}", ExtendedLength) +
107 " exceeds address-range max (" +
108 formatv(
"{0:x}", std::numeric_limits<size_t>::max()));
110 return ExtendedLength;
113Error EHFrameEdgeFixer::processBlock(ParseContext &PC, Block &
B) {
115 LLVM_DEBUG(
dbgs() <<
" Processing block at " <<
B.getAddress() <<
"\n");
119 return make_error<JITLinkError>(
"Unexpected zero-fill block in " +
120 EHFrameSectionName +
" section");
122 if (
B.getSize() == 0) {
128 BlockEdgesInfo BlockEdges;
129 for (
auto &
E :
B.edges())
130 if (
E.isRelocation()) {
132 if (BlockEdges.Multiple.contains(
E.getOffset()))
138 auto [It, Inserted] = BlockEdges.TargetMap.try_emplace(
E.getOffset(),
E);
140 BlockEdges.TargetMap.erase(It);
141 BlockEdges.Multiple.insert(
E.getOffset());
145 BinaryStreamReader BlockReader(
146 StringRef(
B.getContent().data(),
B.getContent().size()),
147 PC.G.getEndianness());
151 if (!RecordRemaining)
152 return RecordRemaining.takeError();
156 if (BlockReader.bytesRemaining() != *RecordRemaining)
157 return make_error<JITLinkError>(
"Incomplete CFI record at " +
158 formatv(
"{0:x16}",
B.getAddress()));
161 uint64_t CIEDeltaFieldOffset = BlockReader.getOffset();
163 if (
auto Err = BlockReader.readInteger(CIEDelta))
167 if (
auto Err = processCIE(PC,
B, CIEDeltaFieldOffset, BlockEdges))
170 if (
auto Err = processFDE(PC,
B, CIEDeltaFieldOffset, CIEDelta, BlockEdges))
177Error EHFrameEdgeFixer::processCIE(ParseContext &PC, Block &
B,
178 size_t CIEDeltaFieldOffset,
179 const BlockEdgesInfo &BlockEdges) {
183 BinaryStreamReader RecordReader(
184 StringRef(
B.getContent().data(),
B.getContent().size()),
185 PC.G.getEndianness());
188 RecordReader.setOffset(CIEDeltaFieldOffset + 4);
190 auto &CIESymbol = PC.G.addAnonymousSymbol(
B, 0,
B.getSize(),
false,
false);
191 CIEInformation CIEInfo(CIESymbol);
194 if (
auto Err = RecordReader.readInteger(
Version))
198 return make_error<JITLinkError>(
"Bad CIE version " + Twine(
Version) +
199 " (should be 0x01) in eh-frame");
201 auto AugInfo = parseAugmentationString(RecordReader);
203 return AugInfo.takeError();
206 if (AugInfo->EHDataFieldPresent)
207 if (
auto Err = RecordReader.skip(PC.G.getPointerSize()))
213 if (
auto Err = RecordReader.readULEB128(CodeAlignmentFactor))
219 int64_t DataAlignmentFactor = 0;
220 if (
auto Err = RecordReader.readSLEB128(DataAlignmentFactor))
225 if (
auto Err = RecordReader.skip(1))
228 if (AugInfo->AugmentationDataPresent) {
230 CIEInfo.AugmentationDataPresent =
true;
232 uint64_t AugmentationDataLength = 0;
233 if (
auto Err = RecordReader.readULEB128(AugmentationDataLength))
236 uint32_t AugmentationDataStartOffset = RecordReader.getOffset();
238 uint8_t *NextField = &AugInfo->Fields[0];
242 CIEInfo.LSDAPresent =
true;
243 if (
auto PE = readPointerEncoding(RecordReader,
B,
"LSDA"))
244 CIEInfo.LSDAEncoding = *PE;
246 return PE.takeError();
249 auto PersonalityPointerEncoding =
250 readPointerEncoding(RecordReader,
B,
"personality");
251 if (!PersonalityPointerEncoding)
252 return PersonalityPointerEncoding.takeError();
254 getOrCreateEncodedPointerEdge(
255 PC, BlockEdges, *PersonalityPointerEncoding, RecordReader,
256 B, RecordReader.getOffset(),
"personality")
262 if (
auto PE = readPointerEncoding(RecordReader,
B,
"address")) {
263 CIEInfo.AddressEncoding = *PE;
265 return make_error<JITLinkError>(
266 "Invalid address encoding DW_EH_PE_omit in CIE at " +
267 formatv(
"{0:x}",
B.getAddress().getValue()));
269 return PE.takeError();
276 if (RecordReader.getOffset() - AugmentationDataStartOffset >
277 AugmentationDataLength)
278 return make_error<JITLinkError>(
"Read past the end of the augmentation "
279 "data while parsing fields");
282 assert(!PC.CIEInfos.count(CIESymbol.getAddress()) &&
283 "Multiple CIEs recorded at the same address?");
284 PC.CIEInfos[CIESymbol.getAddress()] = std::move(CIEInfo);
289Error EHFrameEdgeFixer::processFDE(ParseContext &PC, Block &
B,
290 size_t CIEDeltaFieldOffset,
292 const BlockEdgesInfo &BlockEdges) {
295 orc::ExecutorAddr RecordAddress =
B.getAddress();
297 BinaryStreamReader RecordReader(
298 StringRef(
B.getContent().data(),
B.getContent().size()),
299 PC.G.getEndianness());
302 RecordReader.setOffset(CIEDeltaFieldOffset + 4);
304 auto &FDESymbol = PC.G.addAnonymousSymbol(
B, 0,
B.getSize(),
false,
false);
306 CIEInformation *CIEInfo =
nullptr;
310 if (BlockEdges.Multiple.contains(CIEDeltaFieldOffset))
311 return make_error<JITLinkError>(
312 "CIE pointer field already has multiple edges at " +
313 formatv(
"{0:x16}", RecordAddress + CIEDeltaFieldOffset));
315 auto CIEEdgeItr = BlockEdges.TargetMap.find(CIEDeltaFieldOffset);
317 orc::ExecutorAddr CIEAddress =
320 if (CIEEdgeItr == BlockEdges.TargetMap.end()) {
322 dbgs() <<
" Adding edge at "
323 << (RecordAddress + CIEDeltaFieldOffset)
324 <<
" to CIE at: " << CIEAddress <<
"\n";
326 if (
auto CIEInfoOrErr = PC.findCIEInfo(CIEAddress))
327 CIEInfo = *CIEInfoOrErr;
329 return CIEInfoOrErr.takeError();
330 assert(CIEInfo->CIESymbol &&
"CIEInfo has no CIE symbol set");
331 B.addEdge(NegDelta32, CIEDeltaFieldOffset, *CIEInfo->CIESymbol, 0);
334 dbgs() <<
" Already has edge at "
335 << (RecordAddress + CIEDeltaFieldOffset) <<
" to CIE at "
336 << CIEAddress <<
"\n";
338 auto &EI = CIEEdgeItr->second;
340 return make_error<JITLinkError>(
342 formatv(
"{0:x16}", RecordAddress + CIEDeltaFieldOffset) +
343 " has non-zero addend");
344 if (
auto CIEInfoOrErr = PC.findCIEInfo(EI.Target->getAddress()))
345 CIEInfo = *CIEInfoOrErr;
347 return CIEInfoOrErr.takeError();
353 dbgs() <<
" Processing PC-begin at "
354 << (RecordAddress + RecordReader.getOffset()) <<
"\n";
356 if (
auto PCBegin = getOrCreateEncodedPointerEdge(
357 PC, BlockEdges, CIEInfo->AddressEncoding, RecordReader,
B,
358 RecordReader.getOffset(),
"PC begin")) {
359 assert(*PCBegin &&
"PC-begin symbol not set");
360 if ((*PCBegin)->isDefined()) {
364 dbgs() <<
" Adding keep-alive edge from target at "
365 << (*PCBegin)->getBlock().getAddress() <<
" to FDE at "
366 << RecordAddress <<
"\n";
371 dbgs() <<
" WARNING: Not adding keep-alive edge to FDE at "
372 << RecordAddress <<
", which points to "
373 << ((*PCBegin)->isExternal() ?
"external" :
"absolute")
374 <<
" symbol \"" << (*PCBegin)->getName()
375 <<
"\" -- FDE must be kept alive manually or it will be "
376 <<
"dead stripped.\n";
380 return PCBegin.takeError();
383 if (
auto Err = skipEncodedPointer(CIEInfo->AddressEncoding, RecordReader))
386 if (CIEInfo->AugmentationDataPresent) {
388 if (
auto Err = RecordReader.readULEB128(AugmentationDataSize))
391 if (CIEInfo->LSDAPresent)
392 if (
auto Err = getOrCreateEncodedPointerEdge(
393 PC, BlockEdges, CIEInfo->LSDAEncoding, RecordReader,
B,
394 RecordReader.getOffset(),
"LSDA")
404Expected<EHFrameEdgeFixer::AugmentationInfo>
405EHFrameEdgeFixer::parseAugmentationString(BinaryStreamReader &RecordReader) {
406 AugmentationInfo AugInfo;
408 uint8_t *NextField = &AugInfo.Fields[0];
410 if (
auto Err = RecordReader.readInteger(NextChar))
411 return std::move(Err);
413 while (NextChar != 0) {
416 AugInfo.AugmentationDataPresent =
true;
419 if (
auto Err = RecordReader.readInteger(NextChar))
420 return std::move(Err);
422 return make_error<JITLinkError>(
"Unrecognized substring e" +
424 " in augmentation string");
425 AugInfo.EHDataFieldPresent =
true;
430 *NextField++ = NextChar;
433 return make_error<JITLinkError>(
"Unrecognized character " +
435 " in augmentation string");
438 if (
auto Err = RecordReader.readInteger(NextChar))
439 return std::move(Err);
442 return std::move(AugInfo);
445Expected<uint8_t> EHFrameEdgeFixer::readPointerEncoding(BinaryStreamReader &R,
447 const char *FieldName) {
448 using namespace dwarf;
451 if (
auto Err =
R.readInteger(PointerEncoding))
452 return std::move(Err);
454 bool Supported =
true;
455 switch (PointerEncoding & 0xf) {
464 switch (PointerEncoding & 0x70) {
475 return PointerEncoding;
477 return make_error<JITLinkError>(
"Unsupported pointer encoding " +
478 formatv(
"{0:x2}", PointerEncoding) +
" for " +
479 FieldName +
"in CFI record at " +
483Error EHFrameEdgeFixer::skipEncodedPointer(
uint8_t PointerEncoding,
484 BinaryStreamReader &RecordReader) {
485 using namespace dwarf;
488 if ((PointerEncoding & 0xf) == DW_EH_PE_absptr)
489 PointerEncoding |= (PointerSize == 8) ? DW_EH_PE_udata8 : DW_EH_PE_udata4;
491 switch (PointerEncoding & 0xf) {
494 if (
auto Err = RecordReader.skip(4))
499 if (
auto Err = RecordReader.skip(8))
508Expected<Symbol *> EHFrameEdgeFixer::getOrCreateEncodedPointerEdge(
509 ParseContext &PC,
const BlockEdgesInfo &BlockEdges,
uint8_t PointerEncoding,
510 BinaryStreamReader &RecordReader, Block &BlockToFix,
511 size_t PointerFieldOffset,
const char *FieldName) {
512 using namespace dwarf;
514 if (PointerEncoding == DW_EH_PE_omit)
520 auto EdgeI = BlockEdges.TargetMap.find(PointerFieldOffset);
521 if (EdgeI != BlockEdges.TargetMap.end()) {
523 dbgs() <<
" Existing edge at "
524 << (BlockToFix.getAddress() + PointerFieldOffset) <<
" to "
525 << FieldName <<
" at " << EdgeI->second.Target->getAddress();
526 if (EdgeI->second.Target->hasName())
527 dbgs() <<
" (" << EdgeI->second.Target->getName() <<
")";
530 if (
auto Err = skipEncodedPointer(PointerEncoding, RecordReader))
531 return std::move(Err);
532 return EdgeI->second.Target;
535 if (BlockEdges.Multiple.contains(PointerFieldOffset))
536 return make_error<JITLinkError>(
"Multiple relocations at offset " +
537 formatv(
"{0:x16}", PointerFieldOffset));
546 bool Is64Bit =
false;
547 switch (PointerEncoding & 0xf) {
550 if (
auto Err = RecordReader.readInteger(Val))
551 return std::move(Err);
557 if (
auto Err = RecordReader.readInteger(Val))
558 return std::move(Err);
565 if (
auto Err = RecordReader.readInteger(FieldValue))
566 return std::move(Err);
575 if ((PointerEncoding & 0x70) == DW_EH_PE_pcrel) {
576 Target = BlockToFix.getAddress() + PointerFieldOffset;
577 PtrEdgeKind = Is64Bit ? Delta64 : Delta32;
579 PtrEdgeKind = Is64Bit ? Pointer64 : Pointer32;
583 auto TargetSym = getOrCreateSymbol(PC, Target);
585 return TargetSym.takeError();
586 BlockToFix.addEdge(PtrEdgeKind, PointerFieldOffset, *TargetSym, 0);
589 dbgs() <<
" Adding edge at "
590 << (BlockToFix.getAddress() + PointerFieldOffset) <<
" to "
591 << FieldName <<
" at " << TargetSym->getAddress();
592 if (TargetSym->hasName())
593 dbgs() <<
" (" << TargetSym->getName() <<
")";
600Expected<Symbol &> EHFrameEdgeFixer::getOrCreateSymbol(ParseContext &PC,
601 orc::ExecutorAddr
Addr) {
603 auto CanonicalSymI = PC.AddrToSym.find(
Addr);
604 if (CanonicalSymI != PC.AddrToSym.end())
605 return *CanonicalSymI->second;
608 auto *
B = PC.AddrToBlock.getBlockCovering(
Addr);
610 return make_error<JITLinkError>(
"No symbol or block covering address " +
614 PC.G.addAnonymousSymbol(*
B,
Addr -
B->getAddress(), 0,
false,
false);
615 PC.AddrToSym[S.getAddress()] = &S;
619char EHFrameNullTerminator::NullTerminatorBlockContent[4] = {0, 0, 0, 0};
622 : EHFrameSectionName(EHFrameSectionName) {}
625 auto *EHFrame =
G.findSectionByName(EHFrameSectionName);
631 dbgs() <<
"EHFrameNullTerminator adding null terminator to "
632 << EHFrameSectionName <<
"\n";
635 auto &NullTerminatorBlock =
636 G.createContentBlock(*EHFrame, NullTerminatorBlockContent,
638 G.addAnonymousSymbol(NullTerminatorBlock, 0, 4,
false,
true);
647 EHFrameSection.
size());
653 EHFrameSection.
size());
659 if (
B.edges_size() == 1)
662 for (
auto &
E :
B.edges())
664 assert(Es.
size() >= 2 && Es.
size() <= 3 &&
"Unexpected number of edges");
666 return LHS->getOffset() <
RHS->getOffset();
669 Es.
size() == 3 ? Es[2] :
nullptr);
673EHFrameCFIBlockInspector::EHFrameCFIBlockInspector(
Edge *PersonalityEdge)
674 : PersonalityEdge(PersonalityEdge) {}
676EHFrameCFIBlockInspector::EHFrameCFIBlockInspector(Edge &CIEEdge,
679 : CIEEdge(&CIEEdge), PCBeginEdge(&PCBeginEdge), LSDAEdge(LSDAEdge) {}
684 const char *EHFrameSectionName =
nullptr;
686 EHFrameSectionName =
"__TEXT,__eh_frame";
688 EHFrameSectionName =
".eh_frame";
692 StoreFrameRange = std::move(StoreRangeAddress)](
LinkGraph &
G) ->
Error {
697 if (
auto *S =
G.findSectionByName(EHFrameSectionName)) {
703 return make_error<JITLinkError>(
705 " section can not have zero address with non-zero size");
710 return RecordEHFrame;
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains constants used for implementing Dwarf debug support.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool InBlock(const Value *V, const BasicBlock *BB)
Provides read only access to a subclass of BinaryStream.
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.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Triple - Helper class for working with autoconf configuration names.
static bool includeNonNull(const Block &B)
A block predicate that always includes blocks with non-null addresses.
An Addressable with content and edges.
Inspect an eh-frame CFI record.
static EHFrameCFIBlockInspector FromEdgeScan(Block &B)
Identify CFI record type and edges based on number and order of edges in the given block only.
Error operator()(LinkGraph &G)
EHFrameEdgeFixer(StringRef EHFrameSectionName, unsigned PointerSize, Edge::Kind Pointer32, Edge::Kind Pointer64, Edge::Kind Delta32, Edge::Kind Delta64, Edge::Kind NegDelta32)
Create an eh-frame edge fixer.
virtual ~EHFrameRegistrar()
Represents fixups and constraints in the LinkGraph.
Error registerEHFrames(orc::ExecutorAddrRange EHFrameSection) override
Error deregisterEHFrames(orc::ExecutorAddrRange EHFrameSection) override
Represents a section address range via a pair of Block pointers to the first and last Blocks in the s...
Represents an address in the executor process.
std::enable_if_t< std::is_pointer< T >::value, T > toPtr(WrapFn &&Wrap=WrapFn()) const
Cast this ExecutorAddr to a pointer of the given type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::function< void(orc::ExecutorAddr EHFrameSectionAddr, size_t EHFrameSectionSize)> StoreFrameRangeFunction
static Expected< size_t > readCFIRecordLength(const Block &B, BinaryStreamReader &R)
LinkGraphPassFunction createEHFrameRecorderPass(const Triple &TT, StoreFrameRangeFunction StoreFrameRange)
Creates a pass that records the address and size of the EH frame section.
unique_function< Error(LinkGraph &)> LinkGraphPassFunction
A function for mutating LinkGraphs.
Error deregisterEHFrameSection(const void *EHFrameSectionAddr, size_t EHFrameSectionSize)
Unregister frames in the given eh-frame section with libunwind.
Error registerEHFrameSection(const void *EHFrameSectionAddr, size_t EHFrameSectionSize)
Register frames in the given eh-frame section with libunwind.
uint64_t ExecutorAddrDiff
This is an optimization pass for GlobalISel generic memory operations.
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
void sort(IteratorTy Start, IteratorTy End)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Represents an address range in the exceutor process.
ExecutorAddrDiff size() const