llvm.org GIT mirror llvm / fc1fc8b
This change removes the dependency on DwarfDebug that was used for DW_FORM_ref_addr by making a new DIEUnit class in DIE.cpp. The DIEUnit class represents a compile or type unit and it owns the unit DIE as an instance variable. This allows anyone with a DIE, to get the unit DIE, and then get back to its DIEUnit without adding any new ivars to the DIE class. Why was this needed? The DIE class has an Offset that is always the CU relative DIE offset, not the "offset in debug info section" as was commented in the header file (the comment has been corrected). This is great for performance because most DIE references are compile unit relative and this means most code that accessed the DIE's offset didn't need to make it into a compile unit relative offset because it already was. When we needed to emit a DW_FORM_ref_addr though, we needed to find the absolute offset of the DIE by finding the DIE's compile/type unit. This class did have the absolute debug info/type offset and could be added to the CU relative offset to compute the absolute offset. With this change we can easily get back to a DIE's DIEUnit which will have this needed offset. Prior to this is required having a DwarfDebug and required calling: DwarfCompileUnit *DwarfDebug::lookupUnit(const DIE *CU) const; Now we can use the DIEUnit class to do so without needing DwarfDebug. All clients now use DIEUnit objects (the DwarfDebug stack and the DwarfLinker). A follow on patch for the DWARF generator will also take advantage of this. Differential Revision: https://reviews.llvm.org/D27170 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@288399 91177308-0d34-0410-b5e6-96231b3b80d8 Greg Clayton 3 years ago
12 changed file(s) with 224 addition(s) and 160 deletion(s). Raw diff Collapse all Expand all
1717 #include "llvm/ADT/iterator.h"
1818 #include "llvm/ADT/iterator_range.h"
1919 #include "llvm/ADT/PointerIntPair.h"
20 #include "llvm/ADT/PointerUnion.h"
2021 #include "llvm/ADT/SmallVector.h"
2122 #include "llvm/ADT/StringRef.h"
2223 #include "llvm/CodeGen/DwarfStringPoolEntry.h"
3334 namespace llvm {
3435
3536 class AsmPrinter;
37 class DIE;
38 class DIEUnit;
3639 class MCExpr;
40 class MCSection;
3741 class MCSymbol;
3842 class raw_ostream;
3943
584588 };
585589
586590 //===--------------------------------------------------------------------===//
587 /// DIE - A structured debug information entry. Has an abbreviation which
591 /// A structured debug information entry. Has an abbreviation which
588592 /// describes its organization.
589593 class DIE : IntrusiveBackListNode, public DIEValueList {
590594 friend class IntrusiveBackList;
591
592 /// Offset - Offset in debug info section.
595 friend class DIEUnit;
596
597 /// Offset - Dwarf unit relative offset.
593598 ///
594599 unsigned Offset;
595600
606611 /// Children DIEs.
607612 IntrusiveBackList Children;
608613
609 DIE *Parent = nullptr;
614 /// The owner is either the parent DIE for children of other DIEs, or a
615 /// DIEUnit which contains this DIE as its unit DIE.
616 PointerUnion Owner;
610617
611618 DIE() = delete;
612619 explicit DIE(dwarf::Tag Tag) : Offset(0), Size(0), Tag(Tag) {}
613620
621 protected:
622 void setUnit(DIEUnit *U) {
623 Owner = U;
624 }
614625 public:
615626 static DIE *get(BumpPtrAllocator &Alloc, dwarf::Tag Tag) {
616627 return new (Alloc) DIE(Tag);
617628 }
629
630 DIE(const DIE &RHS) = delete;
631 DIE(DIE &&RHS) = delete;
632 void operator=(const DIE &RHS) = delete;
633 void operator=(const DIE &&RHS) = delete;
618634
619635 // Accessors.
620636 unsigned getAbbrevNumber() const { return AbbrevNumber; }
621637 dwarf::Tag getTag() const { return Tag; }
638 /// Get the compile/type unit relative offset of this DIE.
622639 unsigned getOffset() const { return Offset; }
623640 unsigned getSize() const { return Size; }
624641 bool hasChildren() const { return !Children.empty(); }
635652 return make_range(Children.begin(), Children.end());
636653 }
637654
638 DIE *getParent() const { return Parent; }
655 DIE *getParent() const;
639656
640657 /// Generate the abbreviation for this DIE.
641658 ///
646663 /// Set the abbreviation number for this DIE.
647664 void setAbbrevNumber(unsigned I) { AbbrevNumber = I; }
648665
649 /// Climb up the parent chain to get the compile or type unit DIE this DIE
650 /// belongs to.
651 const DIE *getUnit() const;
652 /// Similar to getUnit, returns null when DIE is not added to an
653 /// owner yet.
654 const DIE *getUnitOrNull() const;
666 /// Get the absolute offset within the .debug_info or .debug_types section
667 /// for this DIE.
668 unsigned getDebugSectionOffset() const;
669
670 /// Climb up the parent chain to get the compile unit or type unit DIE that
671 /// this DIE belongs to.
672 ///
673 /// \returns the compile or type unit DIE that owns this DIE, or NULL if
674 /// this DIE hasn't been added to a unit DIE.
675 const DIE *getUnitDie() const;
676
677 /// Climb up the parent chain to get the compile unit or type unit that this
678 /// DIE belongs to.
679 ///
680 /// \returns the DIEUnit that represents the compile or type unit that owns
681 /// this DIE, or NULL if this DIE hasn't been added to a unit DIE.
682 const DIEUnit *getUnit() const;
683
655684 void setOffset(unsigned O) { Offset = O; }
656685 void setSize(unsigned S) { Size = S; }
657686
658687 /// Add a child to the DIE.
659688 DIE &addChild(DIE *Child) {
660689 assert(!Child->getParent() && "Child should be orphaned");
661 Child->Parent = this;
690 Child->Owner = this;
662691 Children.push_back(*Child);
663692 return Children.back();
664693 }
673702 void dump();
674703 };
675704
705 //===--------------------------------------------------------------------===//
706 /// Represents a compile or type unit.
707 class DIEUnit {
708 protected:
709 /// The compile unit or type unit DIE. This variable must be an instance of
710 /// DIE so that we can calculate the DIEUnit from any DIE by traversing the
711 /// parent backchain and getting the Unit DIE, and then casting itself to a
712 /// DIEUnit. This allows us to be able to find the DIEUnit for any DIE without
713 /// having to store a pointer to the DIEUnit in each DIE instance.
714 DIE Die;
715 /// The section this unit will be emitted in. This may or may not be set to
716 /// a valid section depending on the client that is emitting DWARF.
717 MCSection *Section;
718 uint64_t Offset; /// .debug_info or .debug_types absolute section offset.
719 uint32_t Length; /// The length in bytes of all of the DIEs in this unit.
720 const uint16_t Version; /// The Dwarf version number for this unit.
721 const uint8_t AddrSize; /// The size in bytes of an address for this unit.
722 public:
723 DIEUnit(uint16_t Version, uint8_t AddrSize, dwarf::Tag UnitTag);
724 DIEUnit(const DIEUnit &RHS) = delete;
725 DIEUnit(DIEUnit &&RHS) = delete;
726 void operator=(const DIEUnit &RHS) = delete;
727 void operator=(const DIEUnit &&RHS) = delete;
728 /// Set the section that this DIEUnit will be emitted into.
729 ///
730 /// This function is used by some clients to set the section. Not all clients
731 /// that emit DWARF use this section variable.
732 void setSection(MCSection *Section) {
733 assert(!this->Section);
734 this->Section = Section;
735 }
736
737 /// Return the section that this DIEUnit will be emitted into.
738 ///
739 /// \returns Section pointer which can be NULL.
740 MCSection *getSection() const { return Section; }
741 void setDebugSectionOffset(unsigned O) { Offset = O; }
742 unsigned getDebugSectionOffset() const { return Offset; }
743 void setLength(uint64_t L) { Length = L; }
744 uint64_t getLength() const { return Length; }
745 uint16_t getDwarfVersion() const { return Version; }
746 uint16_t getAddressSize() const { return AddrSize; }
747 DIE &getUnitDie() { return Die; }
748 const DIE &getUnitDie() const { return Die; }
749 };
750
751
676752 //===--------------------------------------------------------------------===//
677753 /// DIELoc - Represents an expression location.
678754 //
1414 #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
1515 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
1616 #include "llvm/Support/DataTypes.h"
17 #include "llvm/Support/Dwarf.h"
1718
1819 namespace llvm {
1920
5152 bool extractFast(const DWARFUnit &U, uint32_t *OffsetPtr,
5253 const DataExtractor &DebugInfoData, uint32_t UEndOffset);
5354
54 uint32_t getTag() const { return AbbrevDecl ? AbbrevDecl->getTag() : 0; }
55 dwarf::Tag getTag() const {
56 return AbbrevDecl ? AbbrevDecl->getTag() : dwarf::DW_TAG_null;
57 }
58
5559 bool isNULL() const { return AbbrevDecl == nullptr; }
5660
5761 /// Returns true if DIE represents a subprogram (not inlined).
107107 LLVM_DUMP_METHOD
108108 void DIEAbbrev::dump() { print(dbgs()); }
109109
110 DIE *DIE::getParent() const {
111 return Owner.dyn_cast();
112 }
113
110114 DIEAbbrev DIE::generateAbbrev() const {
111115 DIEAbbrev Abbrev(Tag, hasChildren());
112116 for (const DIEValue &V : values())
114118 return Abbrev;
115119 }
116120
117 /// Climb up the parent chain to get the unit DIE to which this DIE
118 /// belongs.
119 const DIE *DIE::getUnit() const {
120 const DIE *Cu = getUnitOrNull();
121 assert(Cu && "We should not have orphaned DIEs.");
122 return Cu;
123 }
124
125 /// Climb up the parent chain to get the unit DIE this DIE belongs
126 /// to. Return NULL if DIE is not added to an owner yet.
127 const DIE *DIE::getUnitOrNull() const {
121 unsigned DIE::getDebugSectionOffset() const {
122 const DIEUnit *Unit = getUnit();
123 assert(Unit && "DIE must be owned by a DIEUnit to get its absolute offset");
124 return getUnit()->getDebugSectionOffset() + getOffset();
125 }
126
127 const DIE *DIE::getUnitDie() const {
128128 const DIE *p = this;
129129 while (p) {
130130 if (p->getTag() == dwarf::DW_TAG_compile_unit ||
132132 return p;
133133 p = p->getParent();
134134 }
135 return nullptr;
136 }
137
138 const DIEUnit *DIE::getUnit() const {
139 const DIE *UnitDie = getUnitDie();
140 if (UnitDie)
141 return UnitDie->Owner.dyn_cast();
135142 return nullptr;
136143 }
137144
188195 LLVM_DUMP_METHOD
189196 void DIE::dump() {
190197 print(dbgs());
198 }
199
200 DIEUnit::DIEUnit(uint16_t V, uint8_t A, dwarf::Tag UnitTag)
201 : Die(UnitTag), Section(nullptr), Offset(0), Length(0), Version(V),
202 AddrSize(A)
203 {
204 Die.setUnit(this);
205 assert((UnitTag == dwarf::DW_TAG_compile_unit ||
206 UnitTag == dwarf::DW_TAG_type_unit ||
207 UnitTag == dwarf::DW_TAG_partial_unit) && "expected a unit TAG");
191208 }
192209
193210 void DIEValue::EmitValue(const AsmPrinter *AP) const {
442459 void DIEEntry::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
443460
444461 if (Form == dwarf::DW_FORM_ref_addr) {
445 const DwarfDebug *DD = AP->getDwarfDebug();
446 unsigned Addr = Entry->getOffset();
447 assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations.");
448 // For DW_FORM_ref_addr, output the offset from beginning of debug info
449 // section. Entry->getOffset() returns the offset from start of the
450 // compile unit.
451 DwarfCompileUnit *CU = DD->lookupUnit(Entry->getUnit());
452 assert(CU && "CUDie should belong to a CU.");
453 Addr += CU->getDebugInfoOffset();
454 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
455 AP->EmitLabelPlusOffset(CU->getSectionSym(), Addr,
456 DIEEntry::getRefAddrSize(AP));
457 else
462 // Get the absolute offset for this DIE within the debug info/types section.
463 unsigned Addr = Entry->getDebugSectionOffset();
464 if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) {
465 const DwarfDebug *DD = AP->getDwarfDebug();
466 if (DD)
467 assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations.");
468 const DIEUnit *Unit = Entry->getUnit();
469 assert(Unit && "CUDie should belong to a CU.");
470 MCSection *Section = Unit->getSection();
471 assert(Section && "Must have a section if we are doing relocations");
472 const MCSymbol *SectionSym = Section->getBeginSymbol();
473 AP->EmitLabelPlusOffset(SectionSym, Addr, DIEEntry::getRefAddrSize(AP));
474 } else
458475 AP->OutStreamer->EmitIntValue(Addr, DIEEntry::getRefAddrSize(AP));
459476 } else
460477 AP->EmitInt32(Entry->getOffset());
220220 Asm->EmitInt32((*HI)->Data.Values.size());
221221 for (HashDataContents *HD : (*HI)->Data.Values) {
222222 // Emit the DIE offset
223 DwarfCompileUnit *CU = D->lookupUnit(HD->Die->getUnit());
224 assert(CU && "Accelerated DIE should belong to a CU.");
225 Asm->EmitInt32(HD->Die->getOffset() + CU->getDebugInfoOffset());
223 Asm->EmitInt32(HD->Die->getDebugSectionOffset());
226224 // If we have multiple Atoms emit that info too.
227225 // FIXME: A bit of a hack, we either emit only one atom or all info.
228226 if (HeaderData.Atoms.size() > 1) {
241241 // is not okay to use line_table_start here.
242242 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
243243 StmtListValue =
244 addSectionLabel(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym,
244 addSectionLabel(getUnitDie(), dwarf::DW_AT_stmt_list, LineTableStartSym,
245245 TLOF.getDwarfLineSection()->getBeginSymbol());
246246 }
247247
3131 /// A numeric ID unique among all CUs in the module
3232 unsigned UniqueID;
3333
34 /// Offset of the UnitDie from beginning of debug info section.
35 unsigned DebugInfoOffset = 0;
36
3734 /// The attribute index of DW_AT_stmt_list in the compile unit DIE, avoiding
3835 /// the need to search for it in applyStmtList.
3936 DIE::value_iterator StmtListValue;
8380 DwarfDebug *DW, DwarfFile *DWU);
8481
8582 unsigned getUniqueID() const { return UniqueID; }
86 unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
87 void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
8883
8984 DwarfCompileUnit *getSkeleton() const {
9085 return Skeleton;
190185 /// Set the skeleton unit associated with this unit.
191186 void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
192187
193 const MCSymbol *getSectionSym() const {
194 assert(Section);
195 return Section->getBeginSymbol();
196 }
197
198188 unsigned getLength() {
199189 return sizeof(uint32_t) + // Length field
200 getHeaderSize() + UnitDie.getSize();
190 getHeaderSize() + getUnitDie().getSize();
201191 }
202192
203193 void emitHeader(bool UseOffsets) override;
436436 }
437437
438438 if (useSplitDwarf())
439 NewCU.initSection(Asm->getObjFileLowering().getDwarfInfoDWOSection());
439 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoDWOSection());
440440 else
441 NewCU.initSection(Asm->getObjFileLowering().getDwarfInfoSection());
441 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection());
442442
443443 if (DIUnit->getDWOId()) {
444444 // This CU is either a clang module DWO or a skeleton CU.
519519 // FIXME: Consider the time-space tradeoff of just storing the unit pointer
520520 // in the ConcreteVariables list, rather than looking it up again here.
521521 // DIE::getUnit isn't simple - it walks parent pointers, etc.
522 DwarfCompileUnit *Unit = lookupUnit(VariableDie->getUnit());
522 DwarfCompileUnit *Unit = CUDieMap.lookup(VariableDie->getUnitDie());
523523 assert(Unit);
524524 DbgVariable *AbsVar = getExistingAbstractVariable(
525525 InlinedVariable(Var->getVariable(), Var->getInlinedAt()));
18201820 auto OwnedUnit = make_unique(
18211821 CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder);
18221822 DwarfCompileUnit &NewCU = *OwnedUnit;
1823 NewCU.initSection(Asm->getObjFileLowering().getDwarfInfoSection());
1823 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection());
18241824
18251825 NewCU.initStmtList();
18261826
19121912 Ins.first->second = Signature;
19131913
19141914 if (useSplitDwarf())
1915 NewTU.initSection(Asm->getObjFileLowering().getDwarfTypesDWOSection());
1915 NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesDWOSection());
19161916 else {
19171917 CU.applyStmtList(UnitDie);
1918 NewTU.initSection(
1919 Asm->getObjFileLowering().getDwarfTypesSection(Signature));
1918 NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesSection(Signature));
19201919 }
19211920
19221921 NewTU.setType(NewTU.createTypeDIE(CTy));
532532 return Ref.resolve();
533533 }
534534
535 /// Find the DwarfCompileUnit for the given CU Die.
536 DwarfCompileUnit *lookupUnit(const DIE *CU) const {
537 return CUDieMap.lookup(CU);
538 }
539
540535 void addSubprogramNames(const DISubprogram *SP, DIE &Die);
541536
542537 AddressPool &getAddressPool() { return AddrPool; }
7979 // Iterate over each compile unit and set the size and offsets for each
8080 // DIE within each compile unit. All offsets are CU relative.
8181 for (const auto &TheU : CUs) {
82 TheU->setDebugInfoOffset(SecOffset);
82 TheU->setDebugSectionOffset(SecOffset);
8383 SecOffset += computeSizeAndOffsetsForUnit(TheU.get());
8484 }
8585 }
7272
7373 DwarfUnit::DwarfUnit(dwarf::Tag UnitTag, const DICompileUnit *Node,
7474 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU)
75 : CUNode(Node), UnitDie(*DIE::get(DIEValueAllocator, UnitTag)), Asm(A),
76 DD(DW), DU(DWU), IndexTyDie(nullptr), Section(nullptr) {
77 assert(UnitTag == dwarf::DW_TAG_compile_unit ||
78 UnitTag == dwarf::DW_TAG_type_unit);
75 : DIEUnit(A->getDwarfVersion(), A->getPointerSize(), UnitTag), CUNode(Node),
76 Asm(A), DD(DW), DU(DWU), IndexTyDie(nullptr) {
7977 }
8078
8179 DwarfTypeUnit::DwarfTypeUnit(DwarfCompileUnit &CU, AsmPrinter *A,
8482 : DwarfUnit(dwarf::DW_TAG_type_unit, CU.getCUNode(), A, DW, DWU), CU(CU),
8583 SplitLineTable(SplitLineTable) {
8684 if (SplitLineTable)
87 addSectionOffset(UnitDie, dwarf::DW_AT_stmt_list, 0);
85 addSectionOffset(getUnitDie(), dwarf::DW_AT_stmt_list, 0);
8886 }
8987
9088 DwarfUnit::~DwarfUnit() {
293291
294292 void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute,
295293 DIEEntry Entry) {
296 const DIE *DieCU = Die.getUnitOrNull();
297 const DIE *EntryCU = Entry.getEntry().getUnitOrNull();
298 if (!DieCU)
294 const DIEUnit *CU = Die.getUnit();
295 const DIEUnit *EntryCU = Entry.getEntry().getUnit();
296 if (!CU)
299297 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
300 DieCU = &getUnitDie();
298 CU = getUnitDie().getUnit();
301299 if (!EntryCU)
302 EntryCU = &getUnitDie();
300 EntryCU = getUnitDie().getUnit();
303301 Die.addValue(DIEValueAllocator, Attribute,
304 EntryCU == DieCU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
302 EntryCU == CU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
305303 Entry);
306304 }
307305
13121310 if (IndexTyDie)
13131311 return IndexTyDie;
13141312 // Construct an integer type to use for indexes.
1315 IndexTyDie = &createAndAddDIE(dwarf::DW_TAG_base_type, UnitDie);
1313 IndexTyDie = &createAndAddDIE(dwarf::DW_TAG_base_type, getUnitDie());
13161314 addString(*IndexTyDie, dwarf::DW_AT_name, "sizetype");
13171315 addUInt(*IndexTyDie, dwarf::DW_AT_byte_size, None, sizeof(int64_t));
13181316 addUInt(*IndexTyDie, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
15341532 void DwarfUnit::emitHeader(bool UseOffsets) {
15351533 // Emit size of content not including length itself
15361534 Asm->OutStreamer->AddComment("Length of Unit");
1537 Asm->EmitInt32(getHeaderSize() + UnitDie.getSize());
1535 Asm->EmitInt32(getHeaderSize() + getUnitDie().getSize());
15381536
15391537 Asm->OutStreamer->AddComment("DWARF version number");
15401538 Asm->EmitInt16(DD->getDwarfVersion());
15541552 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
15551553 }
15561554
1557 void DwarfUnit::initSection(MCSection *Section) {
1558 assert(!this->Section);
1559 this->Section = Section;
1560 }
1561
15621555 void DwarfTypeUnit::emitHeader(bool UseOffsets) {
15631556 DwarfUnit::emitHeader(UseOffsets);
15641557 Asm->OutStreamer->AddComment("Type Signature");
6464 //===----------------------------------------------------------------------===//
6565 /// This dwarf writer support class manages information associated with a
6666 /// source file.
67 class DwarfUnit {
67 class DwarfUnit : public DIEUnit {
6868 protected:
6969 /// MDNode for the compile unit.
7070 const DICompileUnit *CUNode;
7272 // All DIEValues are allocated through this allocator.
7373 BumpPtrAllocator DIEValueAllocator;
7474
75 /// Unit debug information entry.
76 DIE &UnitDie;
77
7875 /// Target of Dwarf emission.
7976 AsmPrinter *Asm;
8077
8279 DwarfDebug *DD;
8380 DwarfFile *DU;
8481
85 /// An anonymous type for index type. Owned by UnitDie.
82 /// An anonymous type for index type. Owned by DIEUnit.
8683 DIE *IndexTyDie;
8784
8885 /// Tracks the mapping of unit level debug information variables to debug
10097 /// corresponds to the MDNode mapped with the subprogram DIE.
10198 DenseMap ContainingTypeMap;
10299
103 /// The section this unit will be emitted in.
104 MCSection *Section;
105
106100 DwarfUnit(dwarf::Tag, const DICompileUnit *CU, AsmPrinter *A, DwarfDebug *DW,
107101 DwarfFile *DWU);
108102
110104
111105 public:
112106 virtual ~DwarfUnit();
113
114 void initSection(MCSection *Section);
115
116 MCSection *getSection() const {
117 assert(Section);
118 return Section;
119 }
120107
121108 // Accessors.
122109 AsmPrinter* getAsmPrinter() const { return Asm; }
123110 uint16_t getLanguage() const { return CUNode->getSourceLanguage(); }
124111 const DICompileUnit *getCUNode() const { return CUNode; }
125 DIE &getUnitDie() { return UnitDie; }
126112
127113 /// Return true if this compile unit has something to write out.
128 bool hasContent() const { return UnitDie.hasChildren(); }
114 bool hasContent() const { return Die.hasChildren(); }
129115
130116 /// Get string containing language specific context for a global name.
131117 ///
3737 #include "llvm/Support/TargetRegistry.h"
3838 #include "llvm/Target/TargetMachine.h"
3939 #include "llvm/Target/TargetOptions.h"
40 #include
4041 #include
4142 #include
4243
196197
197198 CompileUnit(DWARFUnit &OrigUnit, unsigned ID, bool CanUseODR,
198199 StringRef ClangModuleName)
199 : OrigUnit(OrigUnit), ID(ID), LowPc(UINT64_MAX), HighPc(0), RangeAlloc(),
200 Ranges(RangeAlloc), ClangModuleName(ClangModuleName) {
200 : OrigUnit(OrigUnit), ID(ID), NewUnit(OrigUnit.getVersion(),
201 OrigUnit.getAddressByteSize(),
202 OrigUnit.getUnitDIE()->getTag()),
203 LowPc(UINT64_MAX), HighPc(0), RangeAlloc(), Ranges(RangeAlloc),
204 ClangModuleName(ClangModuleName) {
201205 Info.resize(OrigUnit.getNumDIEs());
202206
203207 const auto *CUDie = OrigUnit.getUnitDIE(false);
210214 Lang == dwarf::DW_LANG_ObjC_plus_plus);
211215 }
212216
213 CompileUnit(CompileUnit &&RHS)
214 : OrigUnit(RHS.OrigUnit), Info(std::move(RHS.Info)),
215 CUDie(std::move(RHS.CUDie)), StartOffset(RHS.StartOffset),
216 NextUnitOffset(RHS.NextUnitOffset), RangeAlloc(), Ranges(RangeAlloc) {
217 // The CompileUnit container has been 'reserve()'d with the right
218 // size. We cannot move the IntervalMap anyway.
219 llvm_unreachable("CompileUnits should not be moved.");
220 }
221
222217 DWARFUnit &getOrigUnit() const { return OrigUnit; }
223218
224219 unsigned getUniqueID() const { return ID; }
225220
226 DIE *getOutputUnitDIE() const { return CUDie; }
227 void setOutputUnitDIE(DIE *Die) { CUDie = Die; }
221 DIE *getOutputUnitDIE() const {
222 return &const_cast(NewUnit).getUnitDie();
223 }
228224
229225 bool hasODR() const { return HasODR; }
230226 bool isClangModule() const { return !ClangModuleName.empty(); }
332328 DWARFUnit &OrigUnit;
333329 unsigned ID;
334330 std::vector Info; ///< DIE info indexed by DIE index.
335 DIE *CUDie; ///< Root of the linked DIE tree.
331 DIEUnit NewUnit;
336332
337333 uint64_t StartOffset;
338334 uint64_t NextUnitOffset;
400396 // The root DIE might be null, meaning that the Unit had nothing to
401397 // contribute to the linked output. In that case, we will emit the
402398 // unit header without any actual DIE.
403 if (CUDie)
404 NextUnitOffset += CUDie->getSize();
399 NextUnitOffset += NewUnit.getUnitDie().getSize();
405400 return NextUnitOffset;
406401 }
407402
12651260 RelocationManager &RelocMgr;
12661261 /// Allocator used for all the DIEValue objects.
12671262 BumpPtrAllocator &DIEAlloc;
1268 MutableArrayRef CompileUnits;
1263 std::vector> &CompileUnits;
12691264 LinkOptions Options;
12701265
12711266 public:
12721267 DIECloner(DwarfLinker &Linker, RelocationManager &RelocMgr,
12731268 BumpPtrAllocator &DIEAlloc,
1274 MutableArrayRef CompileUnits, LinkOptions &Options)
1269 std::vector> &CompileUnits,
1270 LinkOptions &Options)
12751271 : Linker(Linker), RelocMgr(RelocMgr), DIEAlloc(DIEAlloc),
12761272 CompileUnits(CompileUnits), Options(Options) {}
12771273
12841280 /// compile unit.
12851281 /// \param PCOffset (while cloning a function scope) is the offset
12861282 /// applied to the entry point of the function to get the linked address.
1287 ///
1283 /// \param Die the output DIE to use, pass NULL to create one.
12881284 /// \returns the root of the cloned tree or null if nothing was selected.
12891285 DIE *cloneDIE(const DWARFDebugInfoEntryMinimal &InputDIE, CompileUnit &U,
1290 int64_t PCOffset, uint32_t OutOffset, unsigned Flags);
1286 int64_t PCOffset, uint32_t OutOffset, unsigned Flags,
1287 DIE *Die = nullptr);
12911288
12921289 /// Construct the output DIE tree by cloning the DIEs we
12931290 /// chose to keep above. If there are no valid relocs, then there's
14321429 unsigned UnitID; ///< A unique ID that identifies each compile unit.
14331430
14341431 /// The units of the current debug map object.
1435 std::vector<CompileUnit> Units;
1432 std::vector<std::unique_ptr> Units;
1433
14361434
14371435 /// The debug map object currently under consideration.
14381436 DebugMapObject *CurrentDebugObject;
14671465
14681466 /// Similar to DWARFUnitSection::getUnitForOffset(), but returning our
14691467 /// CompileUnit object instead.
1470 static CompileUnit *getUnitForOffset(MutableArrayRef Units,
1471 unsigned Offset) {
1468 static CompileUnit *getUnitForOffset(
1469 std::vector> &Units, unsigned Offset) {
14721470 auto CU =
14731471 std::upper_bound(Units.begin(), Units.end(), Offset,
1474 [](uint32_t LHS, const CompileUnit &RHS) {
1475 return LHS < RHS.getOrigUnit().getNextUnitOffset();
1472 [](uint32_t LHS, const std::unique_ptr &RHS) {
1473 return LHS < RHS->getOrigUnit().getNextUnitOffset();
14761474 });
1477 return CU != Units.end() ? &*CU : nullptr;
1475 return CU != Units.end() ? CU->get() : nullptr;
14781476 }
14791477
14801478 /// Resolve the DIE attribute reference that has been
14821480 /// CompileUnit which is stored into \p ReferencedCU.
14831481 /// \returns null if resolving fails for any reason.
14841482 static const DWARFDebugInfoEntryMinimal *resolveDIEReference(
1485 const DwarfLinker &Linker, MutableArrayRef Units,
1483 const DwarfLinker &Linker, std::vector> &Units,
14861484 const DWARFFormValue &RefValue, const DWARFUnit &Unit,
14871485 const DWARFDebugInfoEntryMinimal &DIE, CompileUnit *&RefCU) {
14881486 assert(RefValue.isFormClass(DWARFFormValue::FC_Reference));
18381836 }
18391837
18401838 void DwarfLinker::startDebugObject(DWARFContext &Dwarf, DebugMapObject &Obj) {
1841 Units.reserve(Dwarf.getNumCompileUnits());
18421839 // Iterate over the debug map entries and put all the ones that are
18431840 // functions (because they have a size) into the Ranges map. This
18441841 // map is very similar to the FunctionRanges that are stored in each
21842181 case dwarf::DW_TAG_imported_unit:
21852182 // We always want to keep these.
21862183 return Flags | TF_Keep;
2184 default:
2185 break;
21872186 }
21882187
21892188 return Flags;
22322231 Val.extractValue(Data, &Offset, &Unit);
22332232 CompileUnit *ReferencedCU;
22342233 if (const auto *RefDIE =
2235 resolveDIEReference(*this, MutableArrayRef(Units), Val,
2236 Unit, Die, ReferencedCU)) {
2234 resolveDIEReference(*this, Units, Val, Unit, Die, ReferencedCU)) {
22372235 uint32_t RefIdx = ReferencedCU->getOrigUnit().getDIEIndex(RefDIE);
22382236 CompileUnit::DIEInfo &Info = ReferencedCU->getInfo(RefIdx);
22392237 // If the referenced DIE has a DeclContext that has already been
27032701
27042702 DIE *DwarfLinker::DIECloner::cloneDIE(
27052703 const DWARFDebugInfoEntryMinimal &InputDIE, CompileUnit &Unit,
2706 int64_t PCOffset, uint32_t OutOffset, unsigned Flags) {
2704 int64_t PCOffset, uint32_t OutOffset, unsigned Flags, DIE *Die) {
27072705 DWARFUnit &U = Unit.getOrigUnit();
27082706 unsigned Idx = U.getDIEIndex(&InputDIE);
27092707 CompileUnit::DIEInfo &Info = Unit.getInfo(Idx);
27132711 return nullptr;
27142712
27152713 uint32_t Offset = InputDIE.getOffset();
2716 // The DIE might have been already created by a forward reference
2717 // (see cloneDieReferenceAttribute()).
2718 DIE *Die = Info.Clone;
2719 if (!Die)
2720 Die = Info.Clone = DIE::get(DIEAlloc, dwarf::Tag(InputDIE.getTag()));
2714 assert(!(Die && Info.Clone) && "Can't supply a DIE and a cloned DIE");
2715 if (!Die) {
2716 // The DIE might have been already created by a forward reference
2717 // (see cloneDieReferenceAttribute()).
2718 if (Info.Clone)
2719 Die = Info.Clone;
2720 else
2721 Die = Info.Clone = DIE::get(DIEAlloc, dwarf::Tag(InputDIE.getTag()));
2722 }
2723
27212724 assert(Die->getTag() == InputDIE.getTag());
27222725 Die->setOffset(OutOffset);
27232726 if ((Unit.hasODR() || Unit.isClangModule()) &&
33733376 outs() << "cloning .debug_info from " << Filename << "\n";
33743377 }
33753378
3376 DIECloner(*this, RelocMgr, DIEAlloc, MutableArrayRef(*Unit),
3377 Options)
3379 std::vector> CompileUnits;
3380 CompileUnits.push_back(std::move(Unit));
3381 DIECloner(*this, RelocMgr, DIEAlloc, CompileUnits, Options)
33783382 .cloneAllCompileUnits(DwarfContext);
33793383 }
33803384
33843388 return;
33853389
33863390 for (auto &CurrentUnit : CompileUnits) {
3387 const auto *InputDIE = CurrentUnit.getOrigUnit().getUnitDIE();
3388 CurrentUnit.setStartOffset(Linker.OutputDebugInfoSize);
3389 DIE *OutputDIE = cloneDIE(*InputDIE, CurrentUnit, 0 /* PC offset */,
3390 11 /* Unit Header size */, 0);
3391 CurrentUnit.setOutputUnitDIE(OutputDIE);
3392 Linker.OutputDebugInfoSize = CurrentUnit.computeNextUnitOffset();
3391 const auto *InputDIE = CurrentUnit->getOrigUnit().getUnitDIE();
3392 CurrentUnit->setStartOffset(Linker.OutputDebugInfoSize);
3393 // Clonse the InputDIE into your Unit DIE in our compile unit since it
3394 // already has a DIE inside of it.
3395 if (!cloneDIE(*InputDIE, *CurrentUnit, 0 /* PC offset */,
3396 11 /* Unit Header size */, 0,
3397 CurrentUnit->getOutputUnitDIE()))
3398 continue;
3399 Linker.OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset();
33933400 if (Linker.Options.NoOutput)
33943401 continue;
33953402 // FIXME: for compatibility with the classic dsymutil, we emit
33963403 // an empty line table for the unit, even if the unit doesn't
33973404 // actually exist in the DIE tree.
3398 Linker.patchLineTableForUnit(CurrentUnit, DwarfContext);
3399 if (!OutputDIE)
3400 continue;
3401 Linker.patchRangesForUnit(CurrentUnit, DwarfContext);
3402 Linker.Streamer->emitLocationsForUnit(CurrentUnit, DwarfContext);
3403 Linker.emitAcceleratorEntriesForUnit(CurrentUnit);
3405 Linker.patchLineTableForUnit(*CurrentUnit, DwarfContext);
3406 Linker.patchRangesForUnit(*CurrentUnit, DwarfContext);
3407 Linker.Streamer->emitLocationsForUnit(*CurrentUnit, DwarfContext);
3408 Linker.emitAcceleratorEntriesForUnit(*CurrentUnit);
34043409 }
34053410
34063411 if (Linker.Options.NoOutput)
34083413
34093414 // Emit all the compile unit's debug information.
34103415 for (auto &CurrentUnit : CompileUnits) {
3411 Linker.generateUnitRanges(CurrentUnit);
3412 CurrentUnit.fixupForwardReferences();
3413 Linker.Streamer->emitCompileUnitHeader(CurrentUnit);
3414 if (!CurrentUnit.getOutputUnitDIE())
3416 Linker.generateUnitRanges(*CurrentUnit);
3417 CurrentUnit->fixupForwardReferences();
3418 Linker.Streamer->emitCompileUnitHeader(*CurrentUnit);
3419 if (!CurrentUnit->getOutputUnitDIE())
34153420 continue;
3416 Linker.Streamer->emitDIE(*CurrentUnit.getOutputUnitDIE());
3421 Linker.Streamer->emitDIE(*CurrentUnit->getOutputUnitDIE());
34173422 }
34183423 }
34193424
34583463 }
34593464
34603465 if (!registerModuleReference(*CUDie, *CU, ModuleMap))
3461 Units.emplace_back(*CU, UnitID++, !Options.NoODR, "");
3466 Units.push_back(llvm::make_unique(*CU, UnitID++,
3467 !Options.NoODR, ""));
34623468 }
34633469
34643470 // Now build the DIE parent links that we will use during the next phase.
34653471 for (auto &CurrentUnit : Units)
3466 analyzeContextInfo(CurrentUnit.getOrigUnit().getUnitDIE(), 0, CurrentUnit,
3472 analyzeContextInfo(CurrentUnit->getOrigUnit().getUnitDIE(), 0, *CurrentUnit,
34673473 &ODRContexts.getRoot(), StringPool, ODRContexts);
34683474
34693475 // Then mark all the DIEs that need to be present in the linked
34723478 // references require the ParentIdx to be setup for every CU in
34733479 // the object file before calling this.
34743480 for (auto &CurrentUnit : Units)
3475 lookForDIEsToKeep(RelocMgr, *CurrentUnit.getOrigUnit().getUnitDIE(), *Obj,
3476 CurrentUnit, 0);
3481 lookForDIEsToKeep(RelocMgr, *CurrentUnit->getOrigUnit().getUnitDIE(), *Obj,
3482 *CurrentUnit, 0);
34773483
34783484 // The calls to applyValidRelocs inside cloneDIE will walk the
34793485 // reloc array again (in the same way findValidRelocsInDebugInfo()
34843490 .cloneAllCompileUnits(DwarfContext);
34853491 if (!Options.NoOutput && !Units.empty())
34863492 patchFrameInfoForObject(*Obj, DwarfContext,
3487 Units[0].getOrigUnit().getAddressByteSize());
3493 Units[0]->getOrigUnit().getAddressByteSize());
34883494
34893495 // Clean-up before starting working on the next object.
34903496 endDebugObject();