Path: blob/main/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
35271 views
//===-- llvm/CodeGen/DwarfUnit.h - Dwarf Compile Unit ---*- C++ -*--===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains support for writing dwarf compile unit.9//10//===----------------------------------------------------------------------===//1112#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFUNIT_H13#define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFUNIT_H1415#include "DwarfDebug.h"16#include "llvm/ADT/DenseMap.h"17#include "llvm/CodeGen/AsmPrinter.h"18#include "llvm/CodeGen/DIE.h"19#include "llvm/Target/TargetMachine.h"20#include <optional>21#include <string>2223namespace llvm {2425class ConstantFP;26class ConstantInt;27class DwarfCompileUnit;28class MCDwarfDwoLineTable;29class MCSymbol;3031//===----------------------------------------------------------------------===//32/// This dwarf writer support class manages information associated with a33/// source file.34class DwarfUnit : public DIEUnit {35protected:36/// A numeric ID unique among all CUs in the module37unsigned UniqueID;38/// MDNode for the compile unit.39const DICompileUnit *CUNode;4041// All DIEValues are allocated through this allocator.42BumpPtrAllocator DIEValueAllocator;4344/// Target of Dwarf emission.45AsmPrinter *Asm;4647/// The start of the unit within its section.48MCSymbol *LabelBegin = nullptr;4950/// Emitted at the end of the CU and used to compute the CU Length field.51MCSymbol *EndLabel = nullptr;5253// Holders for some common dwarf information.54DwarfDebug *DD;55DwarfFile *DU;5657/// An anonymous type for index type. Owned by DIEUnit.58DIE *IndexTyDie = nullptr;5960/// Tracks the mapping of unit level debug information variables to debug61/// information entries.62DenseMap<const MDNode *, DIE *> MDNodeToDieMap;6364/// A list of all the DIEBlocks in use.65std::vector<DIEBlock *> DIEBlocks;6667/// A list of all the DIELocs in use.68std::vector<DIELoc *> DIELocs;6970/// This map is used to keep track of subprogram DIEs that need71/// DW_AT_containing_type attribute. This attribute points to a DIE that72/// corresponds to the MDNode mapped with the subprogram DIE.73DenseMap<DIE *, const DINode *> ContainingTypeMap;7475DwarfUnit(dwarf::Tag, const DICompileUnit *Node, AsmPrinter *A,76DwarfDebug *DW, DwarfFile *DWU, unsigned UniqueID = 0);7778bool applySubprogramDefinitionAttributes(const DISubprogram *SP, DIE &SPDie, bool Minimal);7980bool isShareableAcrossCUs(const DINode *D) const;8182template <typename T>83void addAttribute(DIEValueList &Die, dwarf::Attribute Attribute,84dwarf::Form Form, T &&Value) {85// For strict DWARF mode, only generate attributes available to current86// DWARF version.87// Attribute 0 is used when emitting form-encoded values in blocks, which88// don't have attributes (only forms) so we cannot detect their DWARF89// version compatibility here and assume they are compatible.90if (Attribute != 0 && Asm->TM.Options.DebugStrictDwarf &&91DD->getDwarfVersion() < dwarf::AttributeVersion(Attribute))92return;9394Die.addValue(DIEValueAllocator,95DIEValue(Attribute, Form, std::forward<T>(Value)));96}9798public:99/// Gets Unique ID for this unit.100unsigned getUniqueID() const { return UniqueID; }101// Accessors.102AsmPrinter* getAsmPrinter() const { return Asm; }103/// Get the the symbol for start of the section for this unit.104MCSymbol *getLabelBegin() const {105assert(LabelBegin && "LabelBegin is not initialized");106return LabelBegin;107}108MCSymbol *getEndLabel() const { return EndLabel; }109uint16_t getLanguage() const { return CUNode->getSourceLanguage(); }110const DICompileUnit *getCUNode() const { return CUNode; }111DwarfDebug &getDwarfDebug() const { return *DD; }112113/// Return true if this compile unit has something to write out.114bool hasContent() const { return getUnitDie().hasChildren(); }115116/// Get string containing language specific context for a global name.117///118/// Walks the metadata parent chain in a language specific manner (using the119/// compile unit language) and returns it as a string. This is done at the120/// metadata level because DIEs may not currently have been added to the121/// parent context and walking the DIEs looking for names is more expensive122/// than walking the metadata.123std::string getParentContextString(const DIScope *Context) const;124125/// Add a new global name to the compile unit.126virtual void addGlobalName(StringRef Name, const DIE &Die,127const DIScope *Context) = 0;128129/// Add a new global type to the compile unit.130virtual void addGlobalTypeImpl(const DIType *Ty, const DIE &Die,131const DIScope *Context) = 0;132133void addGlobalType(const DIType *Ty, const DIE &Die, const DIScope *Context);134135/// Returns the DIE map slot for the specified debug variable.136///137/// We delegate the request to DwarfDebug when the MDNode can be part of the138/// type system, since DIEs for the type system can be shared across CUs and139/// the mappings are kept in DwarfDebug.140DIE *getDIE(const DINode *D) const;141142/// Returns a fresh newly allocated DIELoc.143DIELoc *getDIELoc() { return new (DIEValueAllocator) DIELoc; }144145/// Insert DIE into the map.146///147/// We delegate the request to DwarfDebug when the MDNode can be part of the148/// type system, since DIEs for the type system can be shared across CUs and149/// the mappings are kept in DwarfDebug.150void insertDIE(const DINode *Desc, DIE *D);151152void insertDIE(DIE *D);153154/// Add a flag that is true to the DIE.155void addFlag(DIE &Die, dwarf::Attribute Attribute);156157/// Add an unsigned integer attribute data and value.158void addUInt(DIEValueList &Die, dwarf::Attribute Attribute,159std::optional<dwarf::Form> Form, uint64_t Integer);160161void addUInt(DIEValueList &Block, dwarf::Form Form, uint64_t Integer);162163/// Add an signed integer attribute data and value.164void addSInt(DIEValueList &Die, dwarf::Attribute Attribute,165std::optional<dwarf::Form> Form, int64_t Integer);166167void addSInt(DIELoc &Die, std::optional<dwarf::Form> Form, int64_t Integer);168169/// Add a string attribute data and value.170///171/// We always emit a reference to the string pool instead of immediate172/// strings so that DIEs have more predictable sizes. In the case of split173/// dwarf we emit an index into another table which gets us the static offset174/// into the string table.175void addString(DIE &Die, dwarf::Attribute Attribute, StringRef Str);176177/// Add a Dwarf label attribute data and value.178void addLabel(DIEValueList &Die, dwarf::Attribute Attribute, dwarf::Form Form,179const MCSymbol *Label);180181void addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label);182183/// Add an offset into a section attribute data and value.184void addSectionOffset(DIE &Die, dwarf::Attribute Attribute, uint64_t Integer);185186/// Add a dwarf op address data and value using the form given and an187/// op of either DW_FORM_addr or DW_FORM_GNU_addr_index.188void addOpAddress(DIELoc &Die, const MCSymbol *Sym);189void addPoolOpAddress(DIEValueList &Die, const MCSymbol *Label);190191/// Add a label delta attribute data and value.192void addLabelDelta(DIEValueList &Die, dwarf::Attribute Attribute,193const MCSymbol *Hi, const MCSymbol *Lo);194195/// Add a DIE attribute data and value.196void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry);197198/// Add a DIE attribute data and value.199void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIEEntry Entry);200201/// Add a type's DW_AT_signature and set the declaration flag.202void addDIETypeSignature(DIE &Die, uint64_t Signature);203204/// Add block data.205void addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc);206207/// Add block data.208void addBlock(DIE &Die, dwarf::Attribute Attribute, DIEBlock *Block);209void addBlock(DIE &Die, dwarf::Attribute Attribute, dwarf::Form Form,210DIEBlock *Block);211212/// Add location information to specified debug information entry.213void addSourceLine(DIE &Die, unsigned Line, const DIFile *File);214void addSourceLine(DIE &Die, const DILocalVariable *V);215void addSourceLine(DIE &Die, const DIGlobalVariable *G);216void addSourceLine(DIE &Die, const DISubprogram *SP);217void addSourceLine(DIE &Die, const DILabel *L);218void addSourceLine(DIE &Die, const DIType *Ty);219void addSourceLine(DIE &Die, const DIObjCProperty *Ty);220221/// Add constant value entry in variable DIE.222void addConstantValue(DIE &Die, const ConstantInt *CI, const DIType *Ty);223void addConstantValue(DIE &Die, const APInt &Val, const DIType *Ty);224void addConstantValue(DIE &Die, const APInt &Val, bool Unsigned);225void addConstantValue(DIE &Die, uint64_t Val, const DIType *Ty);226void addConstantValue(DIE &Die, bool Unsigned, uint64_t Val);227228/// Add constant value entry in variable DIE.229void addConstantFPValue(DIE &Die, const ConstantFP *CFP);230231/// Add a linkage name, if it isn't empty.232void addLinkageName(DIE &Die, StringRef LinkageName);233234/// Add template parameters in buffer.235void addTemplateParams(DIE &Buffer, DINodeArray TParams);236237/// Add thrown types.238void addThrownTypes(DIE &Die, DINodeArray ThrownTypes);239240/// Add the accessibility attribute.241void addAccess(DIE &Die, DINode::DIFlags Flags);242243/// Add a new type attribute to the specified entity.244///245/// This takes and attribute parameter because DW_AT_friend attributes are246/// also type references.247void addType(DIE &Entity, const DIType *Ty,248dwarf::Attribute Attribute = dwarf::DW_AT_type);249250DIE *getOrCreateNameSpace(const DINamespace *NS);251DIE *getOrCreateModule(const DIModule *M);252DIE *getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal = false);253254void applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie,255bool SkipSPAttributes = false);256257/// Creates type DIE with specific context.258DIE *createTypeDIE(const DIScope *Context, DIE &ContextDIE, const DIType *Ty);259260/// Find existing DIE or create new DIE for the given type.261virtual DIE *getOrCreateTypeDIE(const MDNode *TyNode);262263/// Get context owner's DIE.264virtual DIE *getOrCreateContextDIE(const DIScope *Context);265266/// Construct DIEs for types that contain vtables.267void constructContainingTypeDIEs();268269/// Construct function argument DIEs.270void constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args);271272/// Create a DIE with the given Tag, add the DIE to its parent, and273/// call insertDIE if MD is not null.274DIE &createAndAddDIE(dwarf::Tag Tag, DIE &Parent, const DINode *N = nullptr);275276bool useSegmentedStringOffsetsTable() const {277return DD->useSegmentedStringOffsetsTable();278}279280/// Compute the size of a header for this unit, not including the initial281/// length field.282virtual unsigned getHeaderSize() const {283return sizeof(int16_t) + // DWARF version number284Asm->getDwarfOffsetByteSize() + // Offset Into Abbrev. Section285sizeof(int8_t) + // Pointer Size (in bytes)286(DD->getDwarfVersion() >= 5 ? sizeof(int8_t)287: 0); // DWARF v5 unit type288}289290/// Emit the header for this unit, not including the initial length field.291virtual void emitHeader(bool UseOffsets) = 0;292293/// Add the DW_AT_str_offsets_base attribute to the unit DIE.294void addStringOffsetsStart();295296/// Add the DW_AT_rnglists_base attribute to the unit DIE.297void addRnglistsBase();298299virtual DwarfCompileUnit &getCU() = 0;300301void constructTypeDIE(DIE &Buffer, const DICompositeType *CTy);302303/// addSectionDelta - Add a label delta attribute data and value.304void addSectionDelta(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Hi,305const MCSymbol *Lo);306307/// Add a Dwarf section label attribute data and value.308void addSectionLabel(DIE &Die, dwarf::Attribute Attribute,309const MCSymbol *Label, const MCSymbol *Sec);310311/// Add DW_TAG_LLVM_annotation.312void addAnnotation(DIE &Buffer, DINodeArray Annotations);313314/// Get context owner's DIE.315DIE *createTypeDIE(const DICompositeType *Ty);316317protected:318~DwarfUnit();319320/// Create new static data member DIE.321DIE *getOrCreateStaticMemberDIE(const DIDerivedType *DT);322323/// Look up the source ID for the given file. If none currently exists,324/// create a new ID and insert it in the line table.325virtual unsigned getOrCreateSourceID(const DIFile *File) = 0;326327/// Emit the common part of the header for this unit.328void emitCommonHeader(bool UseOffsets, dwarf::UnitType UT);329330private:331void constructTypeDIE(DIE &Buffer, const DIBasicType *BTy);332void constructTypeDIE(DIE &Buffer, const DIStringType *BTy);333void constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy);334void constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy);335void constructSubrangeDIE(DIE &Buffer, const DISubrange *SR, DIE *IndexTy);336void constructGenericSubrangeDIE(DIE &Buffer, const DIGenericSubrange *SR,337DIE *IndexTy);338void constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy);339void constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy);340DIE &constructMemberDIE(DIE &Buffer, const DIDerivedType *DT);341void constructTemplateTypeParameterDIE(DIE &Buffer,342const DITemplateTypeParameter *TP);343void constructTemplateValueParameterDIE(DIE &Buffer,344const DITemplateValueParameter *TVP);345346/// Return the default lower bound for an array.347///348/// If the DWARF version doesn't handle the language, return -1.349int64_t getDefaultLowerBound() const;350351/// Get an anonymous type for index type.352DIE *getIndexTyDie();353354/// Set D as anonymous type for index which can be reused later.355void setIndexTyDie(DIE *D) { IndexTyDie = D; }356357virtual void finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) = 0;358359/// If this is a named finished type then include it in the list of types for360/// the accelerator tables.361void updateAcceleratorTables(const DIScope *Context, const DIType *Ty,362const DIE &TyDIE);363364virtual bool isDwoUnit() const = 0;365const MCSymbol *getCrossSectionRelativeBaseAddress() const override;366367/// Returns 'true' if the current DwarfVersion is compatible368/// with the specified \p Version.369bool isCompatibleWithVersion(uint16_t Version) const;370};371372class DwarfTypeUnit final : public DwarfUnit {373uint64_t TypeSignature;374const DIE *Ty;375DwarfCompileUnit &CU;376MCDwarfDwoLineTable *SplitLineTable;377bool UsedLineTable = false;378379unsigned getOrCreateSourceID(const DIFile *File) override;380void finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) override;381bool isDwoUnit() const override;382383public:384DwarfTypeUnit(DwarfCompileUnit &CU, AsmPrinter *A, DwarfDebug *DW,385DwarfFile *DWU, unsigned UniqueID,386MCDwarfDwoLineTable *SplitLineTable = nullptr);387388void setTypeSignature(uint64_t Signature) { TypeSignature = Signature; }389/// Returns Type Signature.390uint64_t getTypeSignature() const { return TypeSignature; }391void setType(const DIE *Ty) { this->Ty = Ty; }392393/// Emit the header for this unit, not including the initial length field.394void emitHeader(bool UseOffsets) override;395unsigned getHeaderSize() const override {396return DwarfUnit::getHeaderSize() + sizeof(uint64_t) + // Type Signature397Asm->getDwarfOffsetByteSize(); // Type DIE Offset398}399void addGlobalName(StringRef Name, const DIE &Die,400const DIScope *Context) override;401void addGlobalTypeImpl(const DIType *Ty, const DIE &Die,402const DIScope *Context) override;403DwarfCompileUnit &getCU() override { return CU; }404};405} // end llvm namespace406#endif407408409