9#ifndef LLVM_DWARFLINKER_DWARFLINKER_H
10#define LLVM_DWARFLINKER_DWARFLINKER_H
28struct MCDwarfLineTableParams;
29template <
typename T>
class SmallVectorImpl;
69 bool IsLittleEndian) = 0;
94 emitAbbrevs(
const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs,
95 unsigned DwarfVersion) = 0;
158 unsigned MinInstLength,
159 std::vector<DWARFDebugLine::Row> &Rows,
160 unsigned AdddressSize) = 0;
181 unsigned DwarfVersion) = 0;
227 const std::vector<std::string> &
Warnings)
247typedef std::function<ErrorOr<DWARFFile &>(
StringRef ContainerName,
273 : TheDwarfEmitter(
Emitter), DwarfLinkerClientID(ClientID) {}
304 void setNoODR(
bool NoODR) { Options.NoODR = NoODR; }
307 void setUpdate(
bool Update) { Options.Update = Update; }
311 Options.KeepFunctionForStatic = KeepFunctionForStatic;
319 assert(std::find(Options.AccelTables.begin(), Options.AccelTables.end(),
320 Kind) == Options.AccelTables.end());
321 Options.AccelTables.emplace_back(Kind);
330 this->StringsTranslator = StringsTranslator;
335 ObjectContexts.reserve(ObjFilesNum);
340 Options.WarningHandler = Handler;
345 Options.ErrorHandler = Handler;
350 Options.ParseableSwiftInterfaces = Map;
355 Options.ObjectPrefixMap = Map;
360 if (TargetDWARFVersion < 1 || TargetDWARFVersion > 5)
362 "unsupported DWARF version: %d",
365 Options.TargetDWARFVersion = TargetDWARFVersion;
371 enum TraversalFlags {
373 TF_InFunctionScope = 1 << 1,
374 TF_DependencyWalk = 1 << 2,
375 TF_ParentWalk = 1 << 3,
381 enum class WorklistItemType {
385 LookForChildDIEsToKeep,
387 LookForRefDIEsToKeep,
389 LookForParentDIEsToKeep,
392 UpdateChildIncompleteness,
395 UpdateRefIncompleteness,
403 struct WorklistItem {
405 WorklistItemType
Type;
409 const unsigned AncestorIdx;
410 CompileUnit::DIEInfo *OtherInfo;
413 WorklistItem(DWARFDie Die, CompileUnit &
CU,
unsigned Flags,
414 WorklistItemType
T = WorklistItemType::LookForDIEsToKeep)
417 WorklistItem(DWARFDie Die, CompileUnit &
CU, WorklistItemType
T,
418 CompileUnit::DIEInfo *OtherInfo =
nullptr)
421 WorklistItem(
unsigned AncestorIdx, CompileUnit &
CU,
unsigned Flags)
423 AncestorIdx(AncestorIdx) {}
427 bool verify(
const DWARFFile &File);
430 bool needToTranslateStrings() {
return StringsTranslator !=
nullptr; }
432 void reportWarning(
const Twine &
Warning,
const DWARFFile &File,
433 const DWARFDie *DIE =
nullptr)
const {
434 if (Options.WarningHandler !=
nullptr)
435 Options.WarningHandler(
Warning,
File.FileName, DIE);
438 void reportError(
const Twine &
Warning,
const DWARFFile &File,
439 const DWARFDie *DIE =
nullptr)
const {
440 if (Options.ErrorHandler !=
nullptr)
445 bool emitPaperTrailWarnings(
const DWARFFile &File,
448 void copyInvariantDebugSection(DWARFContext &
Dwarf);
452 struct RefModuleUnit {
453 RefModuleUnit(DWARFFile &File, std::unique_ptr<CompileUnit> Unit)
455 RefModuleUnit(RefModuleUnit &&
Other)
457 RefModuleUnit(
const RefModuleUnit &) =
delete;
460 std::unique_ptr<CompileUnit> Unit;
462 using ModuleUnitListTy = std::vector<RefModuleUnit>;
468 ModuleUnitListTy ModuleUnits;
471 LinkContext(DWARFFile &File) :
File(
File) {}
476 CompileUnits.clear();
477 File.Addresses->clear();
482 void cleanupAuxiliarryData(LinkContext &Context);
486 void lookForParentDIEsToKeep(
unsigned AncestorIdx, CompileUnit &
CU,
488 SmallVectorImpl<WorklistItem> &Worklist);
492 void lookForChildDIEsToKeep(
const DWARFDie &Die, CompileUnit &
CU,
494 SmallVectorImpl<WorklistItem> &Worklist);
498 void lookForRefDIEsToKeep(
const DWARFDie &Die, CompileUnit &
CU,
500 const DWARFFile &File,
501 SmallVectorImpl<WorklistItem> &Worklist);
505 void markODRCanonicalDie(
const DWARFDie &Die, CompileUnit &
CU);
514 void lookForDIEsToKeep(AddressesMap &RelocMgr,
RangesTy &Ranges,
516 const DWARFFile &File, CompileUnit &
CU,
523 std::pair<bool, bool> isClangModuleRef(
const DWARFDie &CUDie,
524 std::string &PCMFile,
525 LinkContext &Context,
unsigned Indent,
534 bool registerModuleReference(
const DWARFDie &CUDie, LinkContext &Context,
537 unsigned Indent = 0);
543 const std::string &PCMFile, LinkContext &Context,
547 Error cloneModuleUnit(LinkContext &Context, RefModuleUnit &Unit,
548 DeclContextTree &ODRContexts,
550 unsigned Indent = 0);
553 void keepDIEAndDependencies(AddressesMap &RelocMgr,
RangesTy &Ranges,
555 CompileUnit::DIEInfo &MyInfo,
556 const DWARFFile &File, CompileUnit &
CU,
559 unsigned shouldKeepDIE(AddressesMap &RelocMgr,
RangesTy &Ranges,
560 const DWARFDie &DIE,
const DWARFFile &File,
561 CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
566 unsigned shouldKeepVariableDIE(AddressesMap &RelocMgr,
const DWARFDie &DIE,
567 CompileUnit::DIEInfo &MyInfo,
unsigned Flags);
569 unsigned shouldKeepSubprogramDIE(AddressesMap &RelocMgr,
RangesTy &Ranges,
570 const DWARFDie &DIE,
const DWARFFile &File,
572 CompileUnit::DIEInfo &MyInfo,
579 DWARFDie resolveDIEReference(
const DWARFFile &File,
const UnitListTy &Units,
580 const DWARFFormValue &RefValue,
581 const DWARFDie &DIE, CompileUnit *&RefCU);
589 struct DWARFLinkerOptions;
599 std::vector<std::unique_ptr<CompileUnit>> &CompileUnits;
608 DIECloner(DWARFLinker &Linker, DwarfEmitter *Emitter, DWARFFile &ObjFile,
610 std::vector<std::unique_ptr<CompileUnit>> &CompileUnits,
613 DIEAlloc(DIEAlloc), CompileUnits(CompileUnits), Update(Update) {}
626 DIE *cloneDIE(
const DWARFDie &InputDIE,
const DWARFFile &File,
629 bool IsLittleEndian, DIE *Die =
nullptr);
634 uint64_t cloneAllCompileUnits(DWARFContext &DwarfContext,
635 const DWARFFile &File,
637 bool IsLittleEndian);
640 using AttributeSpec = DWARFAbbreviationDeclaration::AttributeSpec;
644 struct AttributesInfo {
646 DwarfStringPoolEntryRef
Name, MangledName, NameWithoutTemplate;
653 int64_t PCOffset = 0;
656 bool HasLowPc =
false;
659 bool HasRanges =
false;
662 bool IsDeclaration =
false;
664 AttributesInfo() =
default;
668 unsigned cloneAttribute(DIE &Die,
const DWARFDie &InputDIE,
669 const DWARFFile &File, CompileUnit &U,
671 const DWARFFormValue &Val,
672 const AttributeSpec AttrSpec,
unsigned AttrSize,
673 AttributesInfo &AttrInfo,
bool IsLittleEndian);
678 unsigned cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec,
679 const DWARFFormValue &Val,
const DWARFUnit &U,
681 AttributesInfo &
Info);
686 unsigned cloneDieReferenceAttribute(DIE &Die,
const DWARFDie &InputDIE,
687 AttributeSpec AttrSpec,
689 const DWARFFormValue &Val,
690 const DWARFFile &File,
694 void cloneExpression(DataExtractor &
Data, DWARFExpression Expression,
695 const DWARFFile &File, CompileUnit &Unit,
701 unsigned cloneBlockAttribute(DIE &Die,
const DWARFFile &File,
702 CompileUnit &Unit, AttributeSpec AttrSpec,
703 const DWARFFormValue &Val,
unsigned AttrSize,
704 bool IsLittleEndian);
709 unsigned cloneAddressAttribute(DIE &Die,
const DWARFDie &InputDIE,
710 AttributeSpec AttrSpec,
unsigned AttrSize,
711 const DWARFFormValue &Val,
712 const CompileUnit &Unit,
713 AttributesInfo &
Info);
717 unsigned cloneScalarAttribute(DIE &Die,
const DWARFDie &InputDIE,
718 const DWARFFile &File, CompileUnit &U,
719 AttributeSpec AttrSpec,
720 const DWARFFormValue &Val,
unsigned AttrSize,
721 AttributesInfo &
Info);
727 bool getDIENames(
const DWARFDie &Die, AttributesInfo &
Info,
730 uint32_t hashFullyQualifiedName(DWARFDie DIE, CompileUnit &U,
731 const DWARFFile &File,
732 int RecurseDepth = 0);
735 void addObjCAccelerator(CompileUnit &Unit,
const DIE *Die,
736 DwarfStringPoolEntryRef
Name,
739 void rememberUnitForMacroOffset(CompileUnit &Unit);
743 void assignAbbrev(DIEAbbrev &Abbrev);
747 void generateUnitRanges(CompileUnit &Unit,
const DWARFFile &File)
const;
749 using ExpressionHandlerRef = function_ref<void(SmallVectorImpl<uint8_t> &,
750 SmallVectorImpl<uint8_t> &)>;
754 void generateUnitLocations(CompileUnit &Unit,
const DWARFFile &File,
755 ExpressionHandlerRef ExprHandler)
const;
760 void patchLineTableForUnit(CompileUnit &Unit, DWARFContext &OrigDwarf,
761 const DWARFFile &File);
764 void emitAcceleratorEntriesForUnit(CompileUnit &Unit);
767 void patchFrameInfoForObject(
const DWARFFile &,
RangesTy &Ranges,
768 DWARFContext &,
unsigned AddressSize);
771 FoldingSet<DIEAbbrev> AbbreviationsSet;
776 std::vector<std::unique_ptr<DIEAbbrev>> Abbreviations;
779 std::vector<DIELoc *> DIELocs;
782 std::vector<DIEBlock *> DIEBlocks;
788 DwarfEmitter *TheDwarfEmitter;
789 std::vector<LinkContext> ObjectContexts;
794 StringMap<uint32_t> EmittedCIEs;
801 AccelTable<DWARF5AccelTableStaticData> DebugNames;
802 AccelTable<AppleAccelTableStaticOffsetData> AppleNames;
803 AccelTable<AppleAccelTableStaticOffsetData> AppleNamespaces;
804 AccelTable<AppleAccelTableStaticOffsetData> AppleObjc;
805 AccelTable<AppleAccelTableStaticTypeData> AppleTypes;
808 StringMap<uint64_t> ClangModules;
812 std::function<StringRef(StringRef)> StringsTranslator =
nullptr;
815 unsigned UniqueUnitID = 0;
818 struct DWARFLinkerOptions {
826 bool Statistics =
false;
829 bool VerifyInputDWARF =
false;
832 bool NoOutput =
false;
842 bool KeepFunctionForStatic =
false;
845 unsigned Threads = 1;
848 SmallVector<DwarfLinkerAccelTableKind, 1> AccelTables;
851 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...
AddressRangesMap class maps values to the address ranges.
The AddressRanges class helps normalize address range collections.
AddressesMap represents information about valid addresses used by debug information.
virtual bool isLiveVariable(const DWARFDie &DIE, CompileUnit::DIEInfo &Info)=0
Checks that the specified variable DIE references live code section.
virtual bool isLiveSubprogram(const DWARFDie &DIE, CompileUnit::DIEInfo &Info)=0
Checks that the specified subprogram DIE references live code section.
virtual bool applyValidRelocs(MutableArrayRef< char > Data, uint64_t BaseOffset, bool IsLittleEndian)=0
Apply the valid relocations to the buffer Data, taking into account that Data is at BaseOffset in the...
virtual void clear()=0
Erases all data.
virtual bool hasValidRelocs()=0
Checks that there are valid relocations against a .debug_info section.
virtual RangesTy & getValidAddressRanges()=0
Returns all valid functions address ranges(i.e., those ranges which points to sections with code).
Stores all information relating to a compile unit, be it in its original instance in the object file ...
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.
This class represents DWARF information for source file and its address map.
DWARFContext * Dwarf
The source DWARF information.
DWARFFile(StringRef Name, DWARFContext *Dwarf, AddressesMap *Addresses, const std::vector< std::string > &Warnings)
AddressesMap * Addresses
Helpful address information(list of valid address ranges, relocations).
StringRef FileName
The object file name.
const std::vector< std::string > & Warnings
Warnings for this object file.
The core of the Dwarf linking logic.
void setVerbosity(bool Verbose)
A number of methods setting various linking options:
void setStatistics(bool Statistics)
Print statistics to standard output.
DWARFLinker(DwarfEmitter *Emitter, DwarfLinkerClient ClientID=DwarfLinkerClient::General)
void setWarningHandler(messageHandler Handler)
Set warning handler which would be used to report warnings.
void setNumThreads(unsigned NumThreads)
Use specified number of threads for parallel files linking.
void setUpdate(bool Update)
update existing DWARF info(for the linked binary).
void addAccelTableKind(DwarfLinkerAccelTableKind Kind)
Add kind of accelerator tables to be generated.
void setKeepFunctionForStatic(bool KeepFunctionForStatic)
Set whether to keep the enclosing function for a static variable.
Error link()
Link debug info for added objFiles.
void setObjectPrefixMap(objectPrefixMap *Map)
Set prefix map for objects.
void setStringsTranslator(std::function< StringRef(StringRef)> StringsTranslator)
Set translator which would be used for strings.
void setPrependPath(const std::string &Ppath)
Set prepend path for clang modules.
void setNoOutput(bool NoOut)
Do not emit linked dwarf info.
void setErrorHandler(messageHandler Handler)
Set error handler which would be used to report errors.
void setEstimatedObjfilesAmount(unsigned ObjFilesNum)
Set estimated objects files amount, for preliminary data allocation.
void setSwiftInterfacesMap(swiftInterfacesMap *Map)
Set map for Swift interfaces.
void addObjectFile(DWARFFile &File, objFileLoader Loader=nullptr, CompileUnitHandler OnCUDieLoaded=[](const DWARFUnit &) {})
Add object file to be linked.
Error setTargetDWARFVersion(uint16_t TargetDWARFVersion)
Set target DWARF version.
void setNoODR(bool NoODR)
Do not unique types according to ODR.
void setVerifyInputDWARF(bool Verify)
Verify the input DWARF.
DwarfEmitter presents interface to generate all debug info tables.
virtual void emitPaperTrailWarningsDie(DIE &Die)=0
Emit DIE containing warnings.
virtual void emitCIE(StringRef CIEBytes)=0
Emit a CIE.
virtual void emitPubNamesForUnit(const CompileUnit &Unit)=0
Emit the .debug_pubnames contribution for Unit.
virtual void emitDwarfDebugArangesTable(const CompileUnit &Unit, const AddressRanges &LinkedRanges)=0
Emit .debug_aranges entries for Unit.
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 emitAppleNames(AccelTable< AppleAccelTableStaticOffsetData > &Table)=0
Emit Apple names accelerator table.
virtual void emitCompileUnitHeader(CompileUnit &Unit, unsigned DwarfVersion)=0
Emit the compilation unit header for Unit in the .debug_info section.
virtual void emitDebugNames(AccelTable< DWARF5AccelTableStaticData > &Table)=0
Emit DWARF debug names.
virtual void translateLineTable(DataExtractor LineData, uint64_t Offset)=0
Copy the .debug_line over to the updated binary while unobfuscating the file names and directories.
virtual uint64_t getRngListsSectionSize() const =0
Returns size of generated .debug_rnglists section.
virtual void emitStrings(const NonRelocatableStringpool &Pool)=0
Emit the string table described by Pool.
virtual void emitPubTypesForUnit(const CompileUnit &Unit)=0
Emit the .debug_pubtypes contribution for Unit.
virtual MCSymbol * emitDwarfDebugLocListHeader(const CompileUnit &Unit)=0
Emit debug locations (.debug_loc, .debug_loclists) header.
virtual void emitAppleNamespaces(AccelTable< AppleAccelTableStaticOffsetData > &Table)=0
Emit Apple namespaces accelerator table.
virtual void emitMacroTables(DWARFContext *Context, const Offset2UnitMap &UnitMacroMap, OffsetsStringPool &StringPool)=0
Emit all available macro tables(DWARFv4 and DWARFv5).
virtual uint64_t getLocListsSectionSize() const =0
Returns size of generated .debug_loclists section.
virtual void emitFDE(uint32_t CIEOffset, uint32_t AddreSize, uint64_t Address, StringRef Bytes)=0
Emit an FDE with data Bytes.
virtual uint64_t getRangesSectionSize() const =0
Returns size of generated .debug_ranges section.
virtual void emitLineTableForUnit(MCDwarfLineTableParams Params, StringRef PrologueBytes, unsigned MinInstLength, std::vector< DWARFDebugLine::Row > &Rows, unsigned AdddressSize)=0
Emit the line table described in Rows into the .debug_line section.
virtual uint64_t getDebugInfoSectionSize() const =0
Returns size of generated .debug_info section.
virtual void emitDwarfDebugLocListFragment(const CompileUnit &Unit, const DWARFLocationExpressionsVector &LinkedLocationExpression, PatchLocation Patch)=0
Emit debug locations (.debug_loc, .debug_loclists) fragment.
virtual uint64_t getDebugMacInfoSectionSize() const =0
Returns size of generated .debug_macinfo section.
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 void emitDIE(DIE &Die)=0
Recursively emit the DIE tree rooted at Die.
virtual void emitAppleTypes(AccelTable< AppleAccelTableStaticTypeData > &Table)=0
Emit Apple type accelerator table.
virtual void emitDwarfDebugLocListFooter(const CompileUnit &Unit, MCSymbol *EndLabel)=0
Emit debug locations (.debug_loc, .debug_loclists) footer.
virtual void emitDwarfDebugRangeListFooter(const CompileUnit &Unit, MCSymbol *EndLabel)=0
Emit debug ranges (.debug_ranges, .debug_rnglists) footer.
virtual uint64_t getDebugMacroSectionSize() const =0
Returns size of generated .debug_macro section.
virtual void emitSectionContents(StringRef SecData, StringRef SecName)=0
Emit section named SecName with data SecData.
virtual void emitDwarfDebugRangeListFragment(const CompileUnit &Unit, const AddressRanges &LinkedRanges, PatchLocation Patch)=0
Emit debug ranges (.debug_ranges, .debug_rnglists) fragment.
virtual uint64_t getFrameSectionSize() const =0
Returns size of generated .debug_frame section.
virtual void emitAppleObjc(AccelTable< AppleAccelTableStaticOffsetData > &Table)=0
Emit Apple Objective-C accelerator table.
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 ...
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
A string table that doesn't need relocations.
StringRef - Represent a constant reference to a string, i.e.
Helper for making strong types.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
An efficient, type-erasing, non-owning reference to a callable.
DWARFAbbreviationDeclaration::AttributeSpec AttributeSpec
Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
llvm::function_ref< void(const Twine &Msg)> ErrorHandler
This is an optimization pass for GlobalISel generic memory operations.
std::vector< std::unique_ptr< CompileUnit > > UnitListTy
function_ref< void(const DWARFUnit &Unit)> CompileUnitHandler
std::function< void(const Twine &Warning, StringRef Context, const DWARFDie *DIE)> messageHandler
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.
std::function< ErrorOr< DWARFFile & >(StringRef ContainerName, StringRef Path)> objFileLoader
@ Dwarf
DWARF v5 .debug_names.
StrongType< NonRelocatableStringpool, OffsetsTag > OffsetsStringPool
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
std::map< std::string, std::string > swiftInterfacesMap
DenseMap< uint64_t, CompileUnit * > Offset2UnitMap
DwarfLinkerAccelTableKind
The kind of accelerator tables we should emit.
@ DebugNames
.debug_names.
@ Apple
.apple_names, .apple_namespaces, .apple_types, .apple_objc.
@ Pub
.debug_pubnames, .debug_pubtypes
std::map< std::string, std::string > objectPrefixMap
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
AddressRangesMap RangesTy
Mapped value in the address map is the offset to apply to the linked address.
Information gathered about a DIE in the object file.