220 : StringsTranslator(StringsTranslator), ErrorHandler(ErrorHandler),
221 WarningHandler(WarningHandler) {}
226 return std::make_unique<DWARFLinker>(ErrorHandler, WarningHandler,
242 DWARFFile &File, ObjFileLoaderTy Loader =
nullptr,
243 CompileUnitHandlerTy OnCUDieLoaded = [](
const DWARFUnit &) {})
override;
246 Error link()
override;
255 Options.Statistics = Statistics;
260 Options.VerifyInputDWARF =
Verify;
264 void setNoODR(
bool NoODR)
override { Options.NoODR = NoODR; }
268 Options.Update = Update;
273 Options.KeepFunctionForStatic = KeepFunctionForStatic;
278 Options.Threads = NumThreads;
287 Options.AccelTables.emplace_back(Kind);
295 ObjectContexts.reserve(ObjFilesNum);
302 Options.InputVerificationHandler = Handler;
307 Options.ParseableSwiftInterfaces = Map;
312 Options.ObjectPrefixMap = Map;
317 if ((TargetDWARFVersion < 1) || (TargetDWARFVersion > 5))
319 "unsupported DWARF version: %d",
322 Options.TargetDWARFVersion = TargetDWARFVersion;
328 enum TraversalFlags {
330 TF_InFunctionScope = 1 << 1,
331 TF_DependencyWalk = 1 << 2,
332 TF_ParentWalk = 1 << 3,
338 enum class WorklistItemType {
342 LookForChildDIEsToKeep,
344 LookForRefDIEsToKeep,
346 LookForParentDIEsToKeep,
349 UpdateChildIncompleteness,
352 UpdateRefIncompleteness,
360 struct WorklistItem {
362 WorklistItemType
Type;
366 const unsigned AncestorIdx;
370 WorklistItem(DWARFDie Die, CompileUnit &
CU,
unsigned Flags,
371 WorklistItemType
T = WorklistItemType::LookForDIEsToKeep)
372 : Die(Die), Type(
T),
CU(
CU), Flags(Flags), AncestorIdx(0) {}
374 WorklistItem(DWARFDie Die, CompileUnit &CU, WorklistItemType
T,
375 CompileUnit::DIEInfo *OtherInfo =
nullptr)
376 : Die(Die),
Type(
T), CU(CU),
Flags(0), OtherInfo(OtherInfo) {}
378 WorklistItem(
unsigned AncestorIdx, CompileUnit &CU,
unsigned Flags)
379 :
Type(WorklistItemType::LookForParentDIEsToKeep), CU(CU),
Flags(
Flags),
380 AncestorIdx(AncestorIdx) {}
384 void verifyInput(
const DWARFFile &File);
387 bool needToTranslateStrings() {
return StringsTranslator !=
nullptr; }
389 void reportWarning(
const Twine &Warning,
const DWARFFile &File,
390 const DWARFDie *DIE =
nullptr)
const {
391 if (WarningHandler !=
nullptr)
392 WarningHandler(Warning,
File.FileName, DIE);
395 void reportError(
const Twine &Warning,
const DWARFFile &File,
396 const DWARFDie *DIE =
nullptr)
const {
401 void copyInvariantDebugSection(DWARFContext &Dwarf);
405 struct RefModuleUnit {
406 RefModuleUnit(DWARFFile &File, std::unique_ptr<CompileUnit> Unit)
408 RefModuleUnit(RefModuleUnit &&Other)
410 RefModuleUnit(
const RefModuleUnit &) =
delete;
413 std::unique_ptr<CompileUnit>
Unit;
415 using ModuleUnitListTy = std::vector<RefModuleUnit>;
421 ModuleUnitListTy ModuleUnits;
424 LinkContext(DWARFFile &File) :
File(
File) {}
429 CompileUnits.clear();
436 void cleanupAuxiliarryData(LinkContext &
Context);
440 void lookForParentDIEsToKeep(
unsigned AncestorIdx, CompileUnit &CU,
442 SmallVectorImpl<WorklistItem> &Worklist);
446 void lookForChildDIEsToKeep(
const DWARFDie &Die, CompileUnit &CU,
448 SmallVectorImpl<WorklistItem> &Worklist);
452 void lookForRefDIEsToKeep(
const DWARFDie &Die, CompileUnit &CU,
453 unsigned Flags,
const UnitListTy &Units,
454 const DWARFFile &File,
455 SmallVectorImpl<WorklistItem> &Worklist);
459 void markODRCanonicalDie(
const DWARFDie &Die, CompileUnit &CU);
468 void lookForDIEsToKeep(AddressesMap &RelocMgr,
const UnitListTy &Units,
469 const DWARFDie &DIE,
const DWARFFile &File,
470 CompileUnit &CU,
unsigned Flags);
476 std::pair<bool, bool> isClangModuleRef(
const DWARFDie &CUDie,
477 std::string &PCMFile,
478 LinkContext &
Context,
unsigned Indent,
487 bool registerModuleReference(
const DWARFDie &CUDie, LinkContext &
Context,
488 ObjFileLoaderTy Loader,
489 CompileUnitHandlerTy OnCUDieLoaded,
490 unsigned Indent = 0);
495 Error loadClangModule(ObjFileLoaderTy Loader,
const DWARFDie &CUDie,
496 const std::string &PCMFile, LinkContext &
Context,
497 CompileUnitHandlerTy OnCUDieLoaded,
498 unsigned Indent = 0);
501 Error cloneModuleUnit(LinkContext &
Context, RefModuleUnit &Unit,
502 DeclContextTree &ODRContexts,
503 OffsetsStringPool &DebugStrPool,
504 OffsetsStringPool &DebugLineStrPool,
505 DebugDieValuePool &StringOffsetPool,
506 unsigned Indent = 0);
508 unsigned shouldKeepDIE(AddressesMap &RelocMgr,
const DWARFDie &DIE,
509 const DWARFFile &File, CompileUnit &Unit,
510 CompileUnit::DIEInfo &MyInfo,
unsigned Flags);
518 std::pair<bool, std::optional<int64_t>>
519 getVariableRelocAdjustment(AddressesMap &RelocMgr,
const DWARFDie &DIE);
523 unsigned shouldKeepVariableDIE(AddressesMap &RelocMgr,
const DWARFDie &DIE,
524 CompileUnit::DIEInfo &MyInfo,
unsigned Flags);
526 unsigned shouldKeepSubprogramDIE(AddressesMap &RelocMgr,
const DWARFDie &DIE,
527 const DWARFFile &File, CompileUnit &Unit,
528 CompileUnit::DIEInfo &MyInfo,
535 DWARFDie resolveDIEReference(
const DWARFFile &File,
const UnitListTy &Units,
536 const DWARFFormValue &RefValue,
537 const DWARFDie &DIE, CompileUnit *&RefCU);
545 struct DWARFLinkerOptions;
556 RelocationFixups RelocFixups;
561 std::vector<std::unique_ptr<CompileUnit>> &CompileUnits;
570 DIECloner(DWARFLinker &Linker, DwarfEmitter *
Emitter, DWARFFile &ObjFile,
571 BumpPtrAllocator &DIEAlloc,
572 std::vector<std::unique_ptr<CompileUnit>> &CompileUnits,
573 bool Update, OffsetsStringPool &DebugStrPool,
574 OffsetsStringPool &DebugLineStrPool,
575 DebugDieValuePool &StringOffsetPool)
577 DebugStrPool(DebugStrPool), DebugLineStrPool(DebugLineStrPool),
578 StringOffsetPool(StringOffsetPool), DIEAlloc(DIEAlloc),
579 CompileUnits(CompileUnits), Update(Update) {}
592 LLVM_ABI DIE *cloneDIE(
const DWARFDie &InputDIE,
const DWARFFile &File,
593 CompileUnit &U, int64_t PCOffset, uint32_t OutOffset,
594 unsigned Flags,
bool IsLittleEndian,
600 LLVM_ABI Expected<uint64_t> cloneAllCompileUnits(DWARFContext &DwarfContext,
601 const DWARFFile &File,
602 bool IsLittleEndian);
606 const uint16_t DwarfVersion);
608 using ExpressionHandlerRef = function_ref<void(
609 SmallVectorImpl<uint8_t> &, SmallVectorImpl<uint8_t> &,
610 int64_t AddrRelocAdjustment)>;
615 const DWARFFile &File,
616 ExpressionHandlerRef ExprHandler);
619 using AttributeSpec = DWARFAbbreviationDeclaration::AttributeSpec;
623 struct AttributesInfo {
625 DwarfStringPoolEntryRef
Name, MangledName, NameWithoutTemplate;
628 int64_t PCOffset = 0;
631 bool HasLowPc =
false;
634 bool HasRanges =
false;
637 bool IsDeclaration =
false;
640 bool AttrStrOffsetBaseSeen =
false;
643 bool HasAppleOrigin =
false;
645 RelocationFixups::AttributeSource
Source;
647 AttributesInfo() =
default;
651 unsigned cloneAttribute(DIE &Die,
const DWARFDie &InputDIE,
652 const DWARFFile &File, CompileUnit &U,
653 const DWARFFormValue &Val,
654 const AttributeSpec AttrSpec,
unsigned AttrSize,
655 AttributesInfo &AttrInfo,
bool IsLittleEndian);
660 unsigned cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec,
661 const DWARFFormValue &Val,
const DWARFUnit &U,
662 AttributesInfo &Info);
667 unsigned cloneDieReferenceAttribute(DIE &Die,
const DWARFDie &InputDIE,
668 AttributeSpec AttrSpec,
670 const DWARFFormValue &Val,
671 const DWARFFile &File,
678 void cloneExpression(DataExtractor &Data, DWARFExpression Expression,
679 const DWARFFile &File, CompileUnit &Unit,
680 SmallVectorImpl<uint8_t> &OutputBuffer,
681 int64_t AddrRelocAdjustment,
bool IsLittleEndian,
682 RelocationFixups::ClonedExpression *Cloned =
nullptr,
683 bool KeepAddrIndices =
false);
688 unsigned cloneBlockAttribute(DIE &Die,
const DWARFDie &InputDIE,
689 const DWARFFile &File, CompileUnit &Unit,
690 AttributeSpec AttrSpec,
691 const DWARFFormValue &Val,
692 AttributesInfo &Info,
bool IsLittleEndian);
697 unsigned cloneAddressAttribute(DIE &Die,
const DWARFDie &InputDIE,
698 AttributeSpec AttrSpec,
unsigned AttrSize,
699 const DWARFFormValue &Val,
700 const CompileUnit &Unit,
701 AttributesInfo &Info);
705 unsigned cloneScalarAttribute(DIE &Die,
const DWARFDie &InputDIE,
706 const DWARFFile &File, CompileUnit &U,
707 AttributeSpec AttrSpec,
708 const DWARFFormValue &Val,
unsigned AttrSize,
709 AttributesInfo &Info);
715 bool getDIENames(
const DWARFDie &Die, AttributesInfo &Info,
716 OffsetsStringPool &StringPool,
const DWARFFile &File,
717 CompileUnit &Unit,
bool StripTemplate =
false);
719 llvm::StringRef getCanonicalDIEName(DWARFDie Die,
const DWARFFile &File,
723 void addObjCAccelerator(CompileUnit &Unit,
const DIE *Die,
724 DwarfStringPoolEntryRef Name,
725 OffsetsStringPool &StringPool,
bool SkipPubSection);
727 void rememberUnitForMacroOffset(CompileUnit &Unit);
732 Error generateLineTableForUnit(CompileUnit &Unit);
736 void assignAbbrev(DIEAbbrev &Abbrev);
740 Error generateUnitRanges(CompileUnit &Unit,
const DWARFFile &File,
741 DebugDieValuePool &AddrPool)
const;
744 void emitAcceleratorEntriesForUnit(CompileUnit &Unit);
747 void patchFrameInfoForObject(LinkContext &
Context);
750 FoldingSet<DIEAbbrev> AbbreviationsSet;
755 std::vector<std::unique_ptr<DIEAbbrev>> Abbreviations;
758 std::vector<DIELoc *> DIELocs;
761 std::vector<DIEBlock *> DIEBlocks;
767 DwarfEmitter *TheDwarfEmitter =
nullptr;
768 std::vector<LinkContext> ObjectContexts;
773 StringMap<uint32_t> EmittedCIEs;
777 uint32_t LastCIEOffset = 0;
781 AccelTable<AppleAccelTableStaticOffsetData>
AppleNames;
783 AccelTable<AppleAccelTableStaticOffsetData> AppleObjc;
784 AccelTable<AppleAccelTableStaticTypeData>
AppleTypes;
787 StringMap<uint64_t> ClangModules;
789 std::function<StringRef(StringRef)> StringsTranslator =
nullptr;
792 unsigned UniqueUnitID = 0;
801 struct DWARFLinkerOptions {
803 uint16_t TargetDWARFVersion = 0;
809 bool Statistics =
false;
812 bool VerifyInputDWARF =
false;
822 bool KeepFunctionForStatic =
false;
825 unsigned Threads = 1;
831 std::string PrependPath;
834 InputVerificationHandlerTy InputVerificationHandler =
nullptr;
841 SwiftInterfacesMapTy *ParseableSwiftInterfaces =
nullptr;
844 ObjectPrefixMapTy *ObjectPrefixMap =
nullptr;