9#ifndef LLVM_DWARFLINKER_CLASSIC_DWARFLINKER_H
10#define LLVM_DWARFLINKER_CLASSIC_DWARFLINKER_H
30template <
typename T>
class SmallVectorImpl;
32namespace dwarf_linker {
50 emitAbbrevs(
const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs,
51 unsigned DwarfVersion) = 0;
113 uint8_t AddrSize) = 0;
149 unsigned DwarfVersion) = 0;
215 : StringsTranslator(StringsTranslator), ErrorHandler(ErrorHandler),
216 WarningHandler(WarningHandler) {}
221 return std::make_unique<DWARFLinker>(ErrorHandler, WarningHandler,
250 Options.Statistics = Statistics;
255 Options.VerifyInputDWARF =
Verify;
259 void setNoODR(
bool NoODR)
override { Options.NoODR = NoODR; }
263 Options.Update = Update;
272 Options.KeepFunctionForStatic = KeepFunctionForStatic;
277 Options.Threads = NumThreads;
283 Options.AccelTables.emplace_back(Kind);
291 ObjectContexts.reserve(ObjFilesNum);
298 Options.InputVerificationHandler = Handler;
303 Options.ParseableSwiftInterfaces = Map;
308 Options.ObjectPrefixMap = Map;
313 if ((TargetDWARFVersion < 1) || (TargetDWARFVersion > 5))
315 "unsupported DWARF version: %d",
318 Options.TargetDWARFVersion = TargetDWARFVersion;
324 enum TraversalFlags {
326 TF_InFunctionScope = 1 << 1,
327 TF_DependencyWalk = 1 << 2,
328 TF_ParentWalk = 1 << 3,
334 enum class WorklistItemType {
338 LookForChildDIEsToKeep,
340 LookForRefDIEsToKeep,
342 LookForParentDIEsToKeep,
345 UpdateChildIncompleteness,
348 UpdateRefIncompleteness,
356 struct WorklistItem {
358 WorklistItemType
Type;
362 const unsigned AncestorIdx;
366 WorklistItem(DWARFDie Die, CompileUnit &
CU,
unsigned Flags,
367 WorklistItemType
T = WorklistItemType::LookForDIEsToKeep)
368 : Die(Die), Type(
T),
CU(
CU), Flags(Flags), AncestorIdx(0) {}
370 WorklistItem(DWARFDie Die, CompileUnit &
CU, WorklistItemType
T,
371 CompileUnit::DIEInfo *OtherInfo =
nullptr)
374 WorklistItem(
unsigned AncestorIdx, CompileUnit &
CU,
unsigned Flags)
376 AncestorIdx(AncestorIdx) {}
380 void verifyInput(
const DWARFFile &File);
383 bool needToTranslateStrings() {
return StringsTranslator !=
nullptr; }
385 void reportWarning(
const Twine &
Warning,
const DWARFFile &File,
386 const DWARFDie *DIE =
nullptr)
const {
387 if (WarningHandler !=
nullptr)
391 void reportError(
const Twine &
Warning,
const DWARFFile &File,
392 const DWARFDie *DIE =
nullptr)
const {
393 if (ErrorHandler !=
nullptr)
397 void copyInvariantDebugSection(DWARFContext &
Dwarf);
401 struct RefModuleUnit {
402 RefModuleUnit(DWARFFile &File, std::unique_ptr<CompileUnit> Unit)
404 RefModuleUnit(RefModuleUnit &&
Other)
406 RefModuleUnit(
const RefModuleUnit &) =
delete;
409 std::unique_ptr<CompileUnit> Unit;
411 using ModuleUnitListTy = std::vector<RefModuleUnit>;
417 ModuleUnitListTy ModuleUnits;
420 LinkContext(DWARFFile &File) :
File(
File) {}
425 CompileUnits.clear();
432 void cleanupAuxiliarryData(LinkContext &Context);
436 void lookForParentDIEsToKeep(
unsigned AncestorIdx, CompileUnit &
CU,
438 SmallVectorImpl<WorklistItem> &Worklist);
442 void lookForChildDIEsToKeep(
const DWARFDie &Die, CompileUnit &
CU,
444 SmallVectorImpl<WorklistItem> &Worklist);
448 void lookForRefDIEsToKeep(
const DWARFDie &Die, CompileUnit &
CU,
450 const DWARFFile &File,
451 SmallVectorImpl<WorklistItem> &Worklist);
455 void markODRCanonicalDie(
const DWARFDie &Die, CompileUnit &
CU);
464 void lookForDIEsToKeep(AddressesMap &RelocMgr,
const UnitListTy &Units,
465 const DWARFDie &DIE,
const DWARFFile &File,
466 CompileUnit &
CU,
unsigned Flags);
472 std::pair<bool, bool> isClangModuleRef(
const DWARFDie &CUDie,
473 std::string &PCMFile,
474 LinkContext &Context,
unsigned Indent,
483 bool registerModuleReference(
const DWARFDie &CUDie, LinkContext &Context,
486 unsigned Indent = 0);
492 const std::string &PCMFile, LinkContext &Context,
494 unsigned Indent = 0);
497 Error cloneModuleUnit(LinkContext &Context, RefModuleUnit &Unit,
498 DeclContextTree &ODRContexts,
502 unsigned Indent = 0);
504 unsigned shouldKeepDIE(AddressesMap &RelocMgr,
const DWARFDie &DIE,
505 const DWARFFile &File, CompileUnit &Unit,
506 CompileUnit::DIEInfo &MyInfo,
unsigned Flags);
514 std::pair<bool, std::optional<int64_t>>
515 getVariableRelocAdjustment(AddressesMap &RelocMgr,
const DWARFDie &DIE);
519 unsigned shouldKeepVariableDIE(AddressesMap &RelocMgr,
const DWARFDie &DIE,
520 CompileUnit::DIEInfo &MyInfo,
unsigned Flags);
522 unsigned shouldKeepSubprogramDIE(AddressesMap &RelocMgr,
const DWARFDie &DIE,
523 const DWARFFile &File, CompileUnit &Unit,
524 CompileUnit::DIEInfo &MyInfo,
531 DWARFDie resolveDIEReference(
const DWARFFile &File,
const UnitListTy &Units,
532 const DWARFFormValue &RefValue,
533 const DWARFDie &DIE, CompileUnit *&RefCU);
541 struct DWARFLinkerOptions;
555 std::vector<std::unique_ptr<CompileUnit>> &CompileUnits;
564 DIECloner(DWARFLinker &Linker, DwarfEmitter *Emitter, DWARFFile &ObjFile,
566 std::vector<std::unique_ptr<CompileUnit>> &CompileUnits,
571 DebugStrPool(DebugStrPool), DebugLineStrPool(DebugLineStrPool),
572 StringOffsetPool(StringOffsetPool), DIEAlloc(DIEAlloc),
573 CompileUnits(CompileUnits), Update(Update) {}
586 DIE *cloneDIE(
const DWARFDie &InputDIE,
const DWARFFile &File,
587 CompileUnit &U, int64_t PCOffset,
uint32_t OutOffset,
588 unsigned Flags,
bool IsLittleEndian, DIE *Die =
nullptr);
593 uint64_t cloneAllCompileUnits(DWARFContext &DwarfContext,
594 const DWARFFile &File,
bool IsLittleEndian);
597 void emitDebugAddrSection(CompileUnit &Unit,
600 using ExpressionHandlerRef = function_ref<void(
601 SmallVectorImpl<uint8_t> &, SmallVectorImpl<uint8_t> &,
602 int64_t AddrRelocAdjustment)>;
606 void generateUnitLocations(CompileUnit &Unit,
const DWARFFile &File,
607 ExpressionHandlerRef ExprHandler);
610 using AttributeSpec = DWARFAbbreviationDeclaration::AttributeSpec;
614 struct AttributesInfo {
616 DwarfStringPoolEntryRef
Name, MangledName, NameWithoutTemplate;
623 int64_t PCOffset = 0;
626 bool HasLowPc =
false;
629 bool HasRanges =
false;
632 bool IsDeclaration =
false;
635 bool AttrStrOffsetBaseSeen =
false;
638 bool HasAppleOrigin =
false;
640 AttributesInfo() =
default;
644 unsigned cloneAttribute(DIE &Die,
const DWARFDie &InputDIE,
645 const DWARFFile &File, CompileUnit &U,
646 const DWARFFormValue &Val,
647 const AttributeSpec AttrSpec,
unsigned AttrSize,
648 AttributesInfo &AttrInfo,
bool IsLittleEndian);
653 unsigned cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec,
654 const DWARFFormValue &Val,
const DWARFUnit &U,
655 AttributesInfo &
Info);
660 unsigned cloneDieReferenceAttribute(DIE &Die,
const DWARFDie &InputDIE,
661 AttributeSpec AttrSpec,
663 const DWARFFormValue &Val,
664 const DWARFFile &File,
668 void cloneExpression(DataExtractor &
Data, DWARFExpression Expression,
669 const DWARFFile &File, CompileUnit &Unit,
671 int64_t AddrRelocAdjustment,
bool IsLittleEndian);
676 unsigned cloneBlockAttribute(DIE &Die,
const DWARFDie &InputDIE,
677 const DWARFFile &File, CompileUnit &Unit,
678 AttributeSpec AttrSpec,
679 const DWARFFormValue &Val,
680 bool IsLittleEndian);
685 unsigned cloneAddressAttribute(DIE &Die,
const DWARFDie &InputDIE,
686 AttributeSpec AttrSpec,
unsigned AttrSize,
687 const DWARFFormValue &Val,
688 const CompileUnit &Unit,
689 AttributesInfo &
Info);
693 unsigned cloneScalarAttribute(DIE &Die,
const DWARFDie &InputDIE,
694 const DWARFFile &File, CompileUnit &U,
695 AttributeSpec AttrSpec,
696 const DWARFFormValue &Val,
unsigned AttrSize,
697 AttributesInfo &
Info);
703 bool getDIENames(
const DWARFDie &Die, AttributesInfo &
Info,
706 uint32_t hashFullyQualifiedName(DWARFDie DIE, CompileUnit &U,
707 const DWARFFile &File,
708 int RecurseDepth = 0);
711 void addObjCAccelerator(CompileUnit &Unit,
const DIE *Die,
712 DwarfStringPoolEntryRef
Name,
715 void rememberUnitForMacroOffset(CompileUnit &Unit);
720 void generateLineTableForUnit(CompileUnit &Unit);
724 void assignAbbrev(DIEAbbrev &Abbrev);
728 void generateUnitRanges(CompileUnit &Unit,
const DWARFFile &File,
732 void emitAcceleratorEntriesForUnit(CompileUnit &Unit);
735 void patchFrameInfoForObject(LinkContext &Context);
738 FoldingSet<DIEAbbrev> AbbreviationsSet;
743 std::vector<std::unique_ptr<DIEAbbrev>> Abbreviations;
746 std::vector<DIELoc *> DIELocs;
749 std::vector<DIEBlock *> DIEBlocks;
755 DwarfEmitter *TheDwarfEmitter =
nullptr;
756 std::vector<LinkContext> ObjectContexts;
761 StringMap<uint32_t> EmittedCIEs;
768 DWARF5AccelTable DebugNames;
769 AccelTable<AppleAccelTableStaticOffsetData> AppleNames;
770 AccelTable<AppleAccelTableStaticOffsetData> AppleNamespaces;
771 AccelTable<AppleAccelTableStaticOffsetData> AppleObjc;
772 AccelTable<AppleAccelTableStaticTypeData> AppleTypes;
775 StringMap<uint64_t> ClangModules;
777 std::function<StringRef(StringRef)> StringsTranslator =
nullptr;
780 unsigned UniqueUnitID = 0;
789 struct DWARFLinkerOptions {
797 bool Statistics =
false;
800 bool VerifyInputDWARF =
false;
810 bool KeepFunctionForStatic =
false;
813 unsigned Threads = 1;
816 SmallVector<AccelTableKind, 1> AccelTables;
819 std::string PrependPath;
This file contains support for writing accelerator tables.
Analysis containing CSE Info
dxil DXContainer Global Emitter
This file defines the DenseMap class.
ppc ctr loops PowerPC CTR Loops Verify
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class holds an abstract representation of an Accelerator Table, consisting of a sequence of buck...
The AddressRanges class helps normalize address range collections.
A structured debug information entry.
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
A string table that doesn't need relocations.
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.
Helper for making strong types.
The instances of the Type class are immutable: once they are created, they are never changed.
This class represents DWARF information for source file and it's address map.
The base interface for DWARFLinker implementations.
std::map< std::string, std::string > ObjectPrefixMapTy
function_ref< void(const DWARFUnit &Unit)> CompileUnitHandlerTy
AccelTableKind
The kind of accelerator tables to be emitted.
std::function< void(const Twine &Warning, StringRef Context, const DWARFDie *DIE)> MessageHandlerTy
std::function< void(const DWARFFile &File, llvm::StringRef Output)> InputVerificationHandlerTy
std::map< std::string, std::string > SwiftInterfacesMapTy
std::function< ErrorOr< DWARFFile & >(StringRef ContainerName, StringRef Path)> ObjFileLoaderTy
Stores all information relating to a compile unit, be it in its original instance in the object file ...
The core of the Dwarf linking logic.
void setStatistics(bool Statistics) override
Print statistics to standard output.
void setInputVerificationHandler(InputVerificationHandlerTy Handler) override
Set verification handler which would be used to report verification errors.
void setSwiftInterfacesMap(SwiftInterfacesMapTy *Map) override
Set map for Swift interfaces.
void setNumThreads(unsigned NumThreads) override
Use specified number of threads for parallel files linking.
void setAllowNonDeterministicOutput(bool) override
Allow generating valid, but non-deterministic output.
void setObjectPrefixMap(ObjectPrefixMapTy *Map) override
Set prefix map for objects.
void setKeepFunctionForStatic(bool KeepFunctionForStatic) override
Set whether to keep the enclosing function for a static variable.
DWARFLinker(MessageHandlerTy ErrorHandler, MessageHandlerTy WarningHandler, std::function< StringRef(StringRef)> StringsTranslator)
void setVerifyInputDWARF(bool Verify) override
Verify the input DWARF.
void addAccelTableKind(AccelTableKind Kind) override
Add kind of accelerator tables to be generated.
static std::unique_ptr< DWARFLinker > createLinker(MessageHandlerTy ErrorHandler, MessageHandlerTy WarningHandler, std::function< StringRef(StringRef)> StringsTranslator=nullptr)
void setNoODR(bool NoODR) override
Do not unique types according to ODR.
void setPrependPath(StringRef Ppath) override
Set prepend path for clang modules.
Error setTargetDWARFVersion(uint16_t TargetDWARFVersion) override
Set target DWARF version.
void setOutputDWARFEmitter(DwarfEmitter *Emitter)
Set output DWARF emitter.
void setUpdateIndexTablesOnly(bool Update) override
Update index tables only(do not modify rest of DWARF).
void setEstimatedObjfilesAmount(unsigned ObjFilesNum) override
Set estimated objects files amount, for preliminary data allocation.
void addObjectFile(DWARFFile &File, ObjFileLoaderTy Loader=nullptr, CompileUnitHandlerTy OnCUDieLoaded=[](const DWARFUnit &) {}) override
Add object file to be linked.
void setVerbosity(bool Verbose) override
A number of methods setting various linking options:
Error link() override
Link debug info for added objFiles. Object files are linked all together.
DwarfEmitter presents interface to generate all debug info tables.
virtual void emitDwarfDebugAddrs(const SmallVector< uint64_t > &Addrs, uint8_t AddrSize)=0
Emit the addresses described by Addrs into the .debug_addr section.
virtual uint64_t getDebugAddrSectionSize() const =0
Returns size of generated .debug_addr section.
virtual void emitPubTypesForUnit(const CompileUnit &Unit)=0
Emit the .debug_pubtypes contribution for Unit.
virtual void emitSectionContents(StringRef SecData, DebugSectionKind SecKind)=0
Emit section named SecName with data SecData.
virtual uint64_t getDebugMacroSectionSize() const =0
Returns size of generated .debug_macro section.
virtual void emitDwarfDebugRangeListFragment(const CompileUnit &Unit, const AddressRanges &LinkedRanges, PatchLocation Patch, DebugDieValuePool &AddrPool)=0
Emit debug ranges (.debug_ranges, .debug_rnglists) fragment.
virtual void emitDwarfDebugArangesTable(const CompileUnit &Unit, const AddressRanges &LinkedRanges)=0
Emit .debug_aranges entries for Unit.
virtual uint64_t getDebugInfoSectionSize() const =0
Returns size of generated .debug_info section.
virtual ~DwarfEmitter()=default
virtual void emitCompileUnitHeader(CompileUnit &Unit, unsigned DwarfVersion)=0
Emit the compilation unit header for Unit in the .debug_info section.
virtual void emitCIE(StringRef CIEBytes)=0
Emit a CIE.
virtual uint64_t getFrameSectionSize() const =0
Returns size of generated .debug_frame section.
virtual void emitFDE(uint32_t CIEOffset, uint32_t AddreSize, uint64_t Address, StringRef Bytes)=0
Emit an FDE with data Bytes.
virtual void emitAppleNamespaces(AccelTable< AppleAccelTableStaticOffsetData > &Table)=0
Emit Apple namespaces accelerator table.
virtual uint64_t getRngListsSectionSize() const =0
Returns size of generated .debug_rnglists section.
virtual void emitAppleObjc(AccelTable< AppleAccelTableStaticOffsetData > &Table)=0
Emit Apple Objective-C accelerator table.
virtual void emitMacroTables(DWARFContext *Context, const Offset2UnitMap &UnitMacroMap, OffsetsStringPool &StringPool)=0
Emit all available macro tables(DWARFv4 and DWARFv5).
virtual void emitDwarfDebugLocListFragment(const CompileUnit &Unit, const DWARFLocationExpressionsVector &LinkedLocationExpression, PatchLocation Patch, DebugDieValuePool &AddrPool)=0
Emit debug locations (.debug_loc, .debug_loclists) fragment.
virtual void emitDebugNames(DWARF5AccelTable &Table)=0
Emit DWARF debug names.
virtual void emitAppleTypes(AccelTable< AppleAccelTableStaticTypeData > &Table)=0
Emit Apple type accelerator table.
virtual MCSymbol * emitDwarfDebugAddrsHeader(const CompileUnit &Unit)=0
Emit .debug_addr header.
virtual MCSymbol * emitDwarfDebugLocListHeader(const CompileUnit &Unit)=0
Emit debug locations (.debug_loc, .debug_loclists) header.
virtual void emitDIE(DIE &Die)=0
Recursively emit the DIE tree rooted at Die.
virtual uint64_t getDebugMacInfoSectionSize() const =0
Returns size of generated .debug_macinfo section.
virtual void emitPubNamesForUnit(const CompileUnit &Unit)=0
Emit the .debug_pubnames contribution for Unit.
virtual void emitAppleNames(AccelTable< AppleAccelTableStaticOffsetData > &Table)=0
Emit Apple names accelerator table.
virtual void emitAbbrevs(const std::vector< std::unique_ptr< DIEAbbrev > > &Abbrevs, unsigned DwarfVersion)=0
Emit the abbreviation table Abbrevs to the .debug_abbrev section.
virtual uint64_t getLocListsSectionSize() const =0
Returns size of generated .debug_loclists section.
virtual void emitLineTableForUnit(const DWARFDebugLine::LineTable &LineTable, const CompileUnit &Unit, OffsetsStringPool &DebugStrPool, OffsetsStringPool &DebugLineStrPool)=0
Emit specified LineTable into .debug_line table.
virtual uint64_t getLineSectionSize() const =0
Returns size of generated .debug_line section.
virtual MCSymbol * emitDwarfDebugRangeListHeader(const CompileUnit &Unit)=0
Emit debug ranges (.debug_ranges, .debug_rnglists) header.
virtual void emitStrings(const NonRelocatableStringpool &Pool)=0
Emit the string table described by Pool into .debug_str table.
virtual void finish()=0
Dump the file to the disk.
virtual void emitDwarfDebugAddrsFooter(const CompileUnit &Unit, MCSymbol *EndLabel)=0
Emit .debug_addr footer.
virtual uint64_t getRangesSectionSize() const =0
Returns size of generated .debug_ranges section.
virtual void emitLineStrings(const NonRelocatableStringpool &Pool)=0
Emit the string table described by Pool into .debug_line_str table.
virtual void emitDwarfDebugLocListFooter(const CompileUnit &Unit, MCSymbol *EndLabel)=0
Emit debug locations (.debug_loc, .debug_loclists) footer.
virtual void emitStringOffsets(const SmallVector< uint64_t > &StringOffsets, uint16_t TargetDWARFVersion)=0
Emit the debug string offset table described by StringOffsets into the .debug_str_offsets table.
virtual void emitDwarfDebugRangeListFooter(const CompileUnit &Unit, MCSymbol *EndLabel)=0
Emit debug ranges (.debug_ranges, .debug_rnglists) footer.
User of DwarfStreamer should call initialization code for AsmPrinter:
DenseMap< uint64_t, CompileUnit * > Offset2UnitMap
IndexedValuesMap< uint64_t > DebugDieValuePool
std::vector< std::unique_ptr< CompileUnit > > UnitListTy
DebugSectionKind
List of tracked debug tables.
DWARFAbbreviationDeclaration::AttributeSpec AttributeSpec
Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
function_ref< void(const ErrorInfoBase &, StringRef)> ErrorHandler
This is an optimization pass for GlobalISel generic memory operations.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
std::vector< DWARFLocationExpression > DWARFLocationExpressionsVector
Represents a set of absolute location expressions.
@ Dwarf
DWARF v5 .debug_names.
StrongType< NonRelocatableStringpool, OffsetsTag > OffsetsStringPool
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Implement std::hash so that hash_code can be used in STL containers.
Information gathered about a DIE in the object file.