LLVM  14.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 
44 
45  for (auto &I : Info) {
46  // Mark everything that wasn't explicit marked for pruning.
47  I.Keep = !I.Prune;
48  auto DIE = OrigUnit.getDIEAtIndex(Idx++);
49 
50  // Try to guess which DIEs must go to the accelerator tables. We do that
51  // just for variables, because functions will be handled depending on
52  // whether they carry a DW_AT_low_pc attribute or not.
53  if (DIE.getTag() != dwarf::DW_TAG_variable &&
54  DIE.getTag() != dwarf::DW_TAG_constant)
55  continue;
56 
58  if (!(Value = DIE.find(dwarf::DW_AT_location))) {
59  if ((Value = DIE.find(dwarf::DW_AT_const_value)) &&
60  !inFunctionScope(*this, I.ParentIdx))
61  I.InDebugMap = true;
62  continue;
63  }
64  if (auto Block = Value->getAsBlock()) {
65  if (Block->size() > OrigUnit.getAddressByteSize() &&
66  (*Block)[0] == dwarf::DW_OP_addr)
67  I.InDebugMap = true;
68  }
69  }
70 }
71 
73  NextUnitOffset = StartOffset;
74  if (NewUnit) {
75  NextUnitOffset += (DwarfVersion >= 5) ? 12 : 11; // Header size
76  NextUnitOffset += NewUnit->getUnitDie().getSize();
77  }
78  return NextUnitOffset;
79 }
80 
81 /// Keep track of a forward cross-cu reference from this unit
82 /// to \p Die that lives in \p RefUnit.
84  DeclContext *Ctxt, PatchLocation Attr) {
85  ForwardDIEReferences.emplace_back(Die, RefUnit, Ctxt, Attr);
86 }
87 
89  for (const auto &Ref : ForwardDIEReferences) {
90  DIE *RefDie;
91  const CompileUnit *RefUnit;
92  PatchLocation Attr;
93  DeclContext *Ctxt;
94  std::tie(RefDie, RefUnit, Ctxt, Attr) = Ref;
95  if (Ctxt && Ctxt->getCanonicalDIEOffset())
96  Attr.set(Ctxt->getCanonicalDIEOffset());
97  else
98  Attr.set(RefDie->getOffset() + RefUnit->getStartOffset());
99  }
100 }
101 
102 void CompileUnit::addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset) {
103  Labels.insert({LabelLowPc, PcOffset});
104 }
105 
107  int64_t PcOffset) {
108  // Don't add empty ranges to the interval map. They are a problem because
109  // the interval map expects half open intervals. This is safe because they
110  // are empty anyway.
111  if (FuncHighPc != FuncLowPc)
112  Ranges.insert(FuncLowPc, FuncHighPc, PcOffset);
113  this->LowPc = std::min(LowPc, FuncLowPc + PcOffset);
114  this->HighPc = std::max(HighPc, FuncHighPc + PcOffset);
115 }
116 
118  if (Die.getTag() != dwarf::DW_TAG_compile_unit)
119  RangeAttributes.push_back(Attr);
120  else
121  UnitRangeAttribute = Attr;
122 }
123 
124 void CompileUnit::noteLocationAttribute(PatchLocation Attr, int64_t PcOffset) {
125  LocationAttributes.emplace_back(Attr, PcOffset);
126 }
127 
130  Namespaces.emplace_back(Name, Die);
131 }
132 
135  bool SkipPubSection) {
136  ObjC.emplace_back(Name, Die, SkipPubSection);
137 }
138 
141  bool SkipPubSection) {
142  Pubnames.emplace_back(Name, Die, SkipPubSection);
143 }
144 
147  bool ObjcClassImplementation,
148  uint32_t QualifiedNameHash) {
149  Pubtypes.emplace_back(Name, Die, QualifiedNameHash, ObjcClassImplementation);
150 }
151 
152 } // namespace llvm
llvm::CompileUnit::getOrigUnit
DWARFUnit & getOrigUnit() const
Definition: DWARFLinkerCompileUnit.h:100
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
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:1083
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:145
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:117
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:133
llvm::DWARFUnit::getUnitDIE
DWARFDie getUnitDIE(bool ExtractUnitDIEOnly=true)
Definition: DWARFUnit.h:390
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:186
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:102
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:59
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:464
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:106
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:139
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:124
llvm::CompileUnit::addNamespaceAccelerator
void addNamespaceAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name)
Add a name accelerator entry for Die with Name.
Definition: DWARFLinkerCompileUnit.cpp:128
llvm::CompileUnit::computeNextUnitOffset
uint64_t computeNextUnitOffset(uint16_t DwarfVersion)
Compute the end offset for this unit.
Definition: DWARFLinkerCompileUnit.cpp:72
llvm::DenseMapBase::insert
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:207
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:286
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
uint16_t
llvm::CompileUnit::fixupForwardReferences
void fixupForwardReferences()
Apply all fixups recorded by noteForwardReference().
Definition: DWARFLinkerCompileUnit.cpp:88
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:210
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:83
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:501
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::CompileUnit::markEverythingAsKept
void markEverythingAsKept()
Mark every DIE in this unit as kept.
Definition: DWARFLinkerCompileUnit.cpp:40
llvm::CompileUnit::setHasInterestingContent
void setHasInterestingContent()
Definition: DWARFLinkerCompileUnit.h:151