LLVM  15.0.0git
DWARFLinkerCompileUnit.cpp
Go to the documentation of this file.
1 //===- DWARFLinkerCompileUnit.cpp -----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
11 
12 namespace llvm {
13 
14 /// Check if the DIE at \p Idx is in the scope of a function.
15 static bool inFunctionScope(CompileUnit &U, unsigned Idx) {
16  while (Idx) {
17  if (U.getOrigUnit().getDIEAtIndex(Idx).getTag() == dwarf::DW_TAG_subprogram)
18  return true;
19  Idx = U.getInfo(Idx).ParentIdx;
20  }
21  return false;
22 }
23 
25  if (!Language) {
27  Language = dwarf::toUnsigned(CU.find(dwarf::DW_AT_language), 0);
28  }
29  return Language;
30 }
31 
33  if (SysRoot.empty()) {
35  SysRoot = dwarf::toStringRef(CU.find(dwarf::DW_AT_LLVM_sysroot)).str();
36  }
37  return SysRoot;
38 }
39 
41  unsigned Idx = 0;
42 
43  for (auto &I : Info) {
44  // Mark everything that wasn't explicit marked for pruning.
45  I.Keep = !I.Prune;
46  auto DIE = OrigUnit.getDIEAtIndex(Idx++);
47 
48  // Try to guess which DIEs must go to the accelerator tables. We do that
49  // just for variables, because functions will be handled depending on
50  // whether they carry a DW_AT_low_pc attribute or not.
51  if (DIE.getTag() != dwarf::DW_TAG_variable &&
52  DIE.getTag() != dwarf::DW_TAG_constant)
53  continue;
54 
56  if (!(Value = DIE.find(dwarf::DW_AT_location))) {
57  if ((Value = DIE.find(dwarf::DW_AT_const_value)) &&
58  !inFunctionScope(*this, I.ParentIdx))
59  I.InDebugMap = true;
60  continue;
61  }
62  if (auto Block = Value->getAsBlock()) {
63  if (Block->size() > OrigUnit.getAddressByteSize() &&
64  (*Block)[0] == dwarf::DW_OP_addr)
65  I.InDebugMap = true;
66  }
67  }
68 }
69 
71  NextUnitOffset = StartOffset;
72  if (NewUnit) {
73  NextUnitOffset += (DwarfVersion >= 5) ? 12 : 11; // Header size
74  NextUnitOffset += NewUnit->getUnitDie().getSize();
75  }
76  return NextUnitOffset;
77 }
78 
79 /// Keep track of a forward cross-cu reference from this unit
80 /// to \p Die that lives in \p RefUnit.
82  DeclContext *Ctxt, PatchLocation Attr) {
83  ForwardDIEReferences.emplace_back(Die, RefUnit, Ctxt, Attr);
84 }
85 
87  for (const auto &Ref : ForwardDIEReferences) {
88  DIE *RefDie;
89  const CompileUnit *RefUnit;
90  PatchLocation Attr;
91  DeclContext *Ctxt;
92  std::tie(RefDie, RefUnit, Ctxt, Attr) = Ref;
93  if (Ctxt && Ctxt->getCanonicalDIEOffset())
94  Attr.set(Ctxt->getCanonicalDIEOffset());
95  else
96  Attr.set(RefDie->getOffset() + RefUnit->getStartOffset());
97  }
98 }
99 
100 void CompileUnit::addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset) {
101  Labels.insert({LabelLowPc, PcOffset});
102 }
103 
105  int64_t PcOffset) {
106  // Don't add empty ranges to the interval map. They are a problem because
107  // the interval map expects half open intervals. This is safe because they
108  // are empty anyway.
109  if (FuncHighPc != FuncLowPc)
110  Ranges.insert(FuncLowPc, FuncHighPc, PcOffset);
111  this->LowPc = std::min(LowPc, FuncLowPc + PcOffset);
112  this->HighPc = std::max(HighPc, FuncHighPc + PcOffset);
113 }
114 
116  if (Die.getTag() != dwarf::DW_TAG_compile_unit)
117  RangeAttributes.push_back(Attr);
118  else
119  UnitRangeAttribute = Attr;
120 }
121 
122 void CompileUnit::noteLocationAttribute(PatchLocation Attr, int64_t PcOffset) {
123  LocationAttributes.emplace_back(Attr, PcOffset);
124 }
125 
128  Namespaces.emplace_back(Name, Die);
129 }
130 
133  bool SkipPubSection) {
134  ObjC.emplace_back(Name, Die, SkipPubSection);
135 }
136 
139  bool SkipPubSection) {
140  Pubnames.emplace_back(Name, Die, SkipPubSection);
141 }
142 
145  bool ObjcClassImplementation,
146  uint32_t QualifiedNameHash) {
147  Pubtypes.emplace_back(Name, Die, QualifiedNameHash, ObjcClassImplementation);
148 }
149 
150 } // namespace llvm
llvm::CompileUnit::getOrigUnit
DWARFUnit & getOrigUnit() const
Definition: DWARFLinkerCompileUnit.h:100
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::IntervalMap::insert
void insert(KeyT a, KeyT b, ValT y)
insert - Add a mapping of [a;b] to y, coalesce with adjacent intervals.
Definition: IntervalMap.h:1079
llvm::CompileUnit::getInfo
DIEInfo & getInfo(unsigned Idx)
Definition: DWARFLinkerCompileUnit.h:120
llvm::DeclContext
A DeclContext is a named program scope that is used for ODR uniquing of types.
Definition: DWARFLinkerDeclContext.h:77
llvm::inFunctionScope
static bool inFunctionScope(CompileUnit &U, unsigned Idx)
Check if the DIE at Idx is in the scope of a function.
Definition: DWARFLinkerCompileUnit.cpp:15
llvm::CompileUnit::DIEInfo::ParentIdx
uint32_t ParentIdx
The index of this DIE's parent.
Definition: DWARFLinkerCompileUnit.h:64
llvm::CompileUnit::addTypeAccelerator
void addTypeAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name, bool ObjcClassImplementation, uint32_t QualifiedNameHash)
Add a type accelerator entry for Die with Name which is stored in the string table at Offset.
Definition: DWARFLinkerCompileUnit.cpp:143
llvm::CompileUnit::getLanguage
uint16_t getLanguage()
Definition: DWARFLinkerCompileUnit.cpp:24
llvm::Optional
Definition: APInt.h:33
llvm::CompileUnit::noteRangeAttribute
void noteRangeAttribute(const DIE &Die, PatchLocation Attr)
Keep track of a DW_AT_range attribute that we will need to patch up later.
Definition: DWARFLinkerCompileUnit.cpp:115
llvm::DIE
A structured debug information entry.
Definition: DIE.h:739
llvm::DIE::getTag
dwarf::Tag getTag() const
Definition: DIE.h:775
DWARFLinkerCompileUnit.h
llvm::DwarfStringPoolEntryRef
String pool entry reference.
Definition: DwarfStringPoolEntry.h:31
llvm::CompileUnit::addObjCAccelerator
void addObjCAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name, bool SkipPubnamesSection=false)
Add various accelerator entries for Die with Name which is stored in the string table at Offset.
Definition: DWARFLinkerCompileUnit.cpp:131
llvm::DWARFUnit::getUnitDIE
DWARFDie getUnitDIE(bool ExtractUnitDIEOnly=true)
Definition: DWARFUnit.h:402
llvm::ModRefInfo::Ref
@ Ref
The access may reference the value stored in memory.
llvm::dwarf::toStringRef
StringRef toStringRef(const Optional< DWARFFormValue > &V, StringRef Default={})
Take an optional DWARFFormValue and try to extract a string value from it.
Definition: DWARFFormValue.h:193
llvm::StringRef::str
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:245
llvm::DeclContext::getCanonicalDIEOffset
uint32_t getCanonicalDIEOffset() const
Definition: DWARFLinkerDeclContext.h:94
uint64_t
llvm::CompileUnit::addLabelLowPc
void addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset)
Add the low_pc of a label that is relocated by applying offset PCOffset.
Definition: DWARFLinkerCompileUnit.cpp:100
llvm::CompileUnit
Stores all information relating to a compile unit, be it in its original instance in the object file ...
Definition: DWARFLinkerCompileUnit.h:50
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::CompileUnit::getStartOffset
uint64_t getStartOffset() const
Definition: DWARFLinkerCompileUnit.h:128
llvm::DWARFUnit::getDIEAtIndex
DWARFDie getDIEAtIndex(unsigned Index)
Return the DIE object at the given index.
Definition: DWARFUnit.h:480
llvm::CompileUnit::addFunctionRange
void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset)
Add a function range [LowPC, HighPC) that is relocated by applying offset PCOffset.
Definition: DWARFLinkerCompileUnit.cpp:104
llvm::min
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:357
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::CompileUnit::addNameAccelerator
void addNameAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name, bool SkipPubnamesSection=false)
Add a name accelerator entry for Die with Name.
Definition: DWARFLinkerCompileUnit.cpp:137
uint32_t
llvm::CompileUnit::noteLocationAttribute
void noteLocationAttribute(PatchLocation Attr, int64_t PcOffset)
Keep track of a location attribute pointing to a location list in the debug_loc section.
Definition: DWARFLinkerCompileUnit.cpp:122
llvm::CompileUnit::addNamespaceAccelerator
void addNamespaceAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name)
Add a name accelerator entry for Die with Name.
Definition: DWARFLinkerCompileUnit.cpp:126
llvm::CompileUnit::computeNextUnitOffset
uint64_t computeNextUnitOffset(uint16_t DwarfVersion)
Compute the end offset for this unit.
Definition: DWARFLinkerCompileUnit.cpp:70
llvm::DenseMapBase::insert
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:209
llvm::CompileUnit::getSysRoot
StringRef getSysRoot()
Return the DW_AT_LLVM_sysroot of the compile unit or an empty StringRef.
Definition: DWARFLinkerCompileUnit.cpp:32
llvm::DWARFUnit::getAddressByteSize
uint8_t getAddressByteSize() const
Definition: DWARFUnit.h:295
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
uint16_t
llvm::CompileUnit::fixupForwardReferences
void fixupForwardReferences()
Apply all fixups recorded by noteForwardReference().
Definition: DWARFLinkerCompileUnit.cpp:86
llvm::PatchLocation
Definition: DWARFLinkerCompileUnit.h:29
llvm::PatchLocation::set
void set(uint64_t New) const
Definition: DWARFLinkerCompileUnit.h:35
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::dwarf::toUnsigned
Optional< uint64_t > toUnsigned(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an unsigned constant.
Definition: DWARFFormValue.h:225
llvm::DWARFDie::getTag
dwarf::Tag getTag() const
Definition: DWARFDie.h:72
DWARFLinkerDeclContext.h
llvm::max
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:340
llvm::CompileUnit::noteForwardReference
void noteForwardReference(DIE *Die, const CompileUnit *RefUnit, DeclContext *Ctxt, PatchLocation Attr)
Keep track of a forward reference to DIE Die in RefUnit by Attr.
Definition: DWARFLinkerCompileUnit.cpp:81
llvm::DWARFDie
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Definition: DWARFDie.h:43
llvm::DIE::getOffset
unsigned getOffset() const
Get the compile/type unit relative offset of this DIE.
Definition: DIE.h:777
CU
Definition: AArch64AsmBackend.cpp:505
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::CompileUnit::markEverythingAsKept
void markEverythingAsKept()
Mark every DIE in this unit as kept.
Definition: DWARFLinkerCompileUnit.cpp:40