LLVM  10.0.0
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  unsigned Idx = 0;
34 
36 
37  for (auto &I : Info) {
38  // Mark everything that wasn't explicit marked for pruning.
39  I.Keep = !I.Prune;
40  auto DIE = OrigUnit.getDIEAtIndex(Idx++);
41 
42  // Try to guess which DIEs must go to the accelerator tables. We do that
43  // just for variables, because functions will be handled depending on
44  // whether they carry a DW_AT_low_pc attribute or not.
45  if (DIE.getTag() != dwarf::DW_TAG_variable &&
46  DIE.getTag() != dwarf::DW_TAG_constant)
47  continue;
48 
50  if (!(Value = DIE.find(dwarf::DW_AT_location))) {
51  if ((Value = DIE.find(dwarf::DW_AT_const_value)) &&
52  !inFunctionScope(*this, I.ParentIdx))
53  I.InDebugMap = true;
54  continue;
55  }
56  if (auto Block = Value->getAsBlock()) {
57  if (Block->size() > OrigUnit.getAddressByteSize() &&
58  (*Block)[0] == dwarf::DW_OP_addr)
59  I.InDebugMap = true;
60  }
61  }
62 }
63 
65  NextUnitOffset = StartOffset;
66  if (NewUnit) {
67  NextUnitOffset += 11 /* Header size */;
68  NextUnitOffset += NewUnit->getUnitDie().getSize();
69  }
70  return NextUnitOffset;
71 }
72 
73 /// Keep track of a forward cross-cu reference from this unit
74 /// to \p Die that lives in \p RefUnit.
76  DeclContext *Ctxt, PatchLocation Attr) {
77  ForwardDIEReferences.emplace_back(Die, RefUnit, Ctxt, Attr);
78 }
79 
81  for (const auto &Ref : ForwardDIEReferences) {
82  DIE *RefDie;
83  const CompileUnit *RefUnit;
84  PatchLocation Attr;
85  DeclContext *Ctxt;
86  std::tie(RefDie, RefUnit, Ctxt, Attr) = Ref;
87  if (Ctxt && Ctxt->getCanonicalDIEOffset())
88  Attr.set(Ctxt->getCanonicalDIEOffset());
89  else
90  Attr.set(RefDie->getOffset() + RefUnit->getStartOffset());
91  }
92 }
93 
94 void CompileUnit::addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset) {
95  Labels.insert({LabelLowPc, PcOffset});
96 }
97 
98 void CompileUnit::addFunctionRange(uint64_t FuncLowPc, uint64_t FuncHighPc,
99  int64_t PcOffset) {
100  // Don't add empty ranges to the interval map. They are a problem because
101  // the interval map expects half open intervals. This is safe because they
102  // are empty anyway.
103  if (FuncHighPc != FuncLowPc)
104  Ranges.insert(FuncLowPc, FuncHighPc, PcOffset);
105  this->LowPc = std::min(LowPc, FuncLowPc + PcOffset);
106  this->HighPc = std::max(HighPc, FuncHighPc + PcOffset);
107 }
108 
110  if (Die.getTag() != dwarf::DW_TAG_compile_unit)
111  RangeAttributes.push_back(Attr);
112  else
113  UnitRangeAttribute = Attr;
114 }
115 
116 void CompileUnit::noteLocationAttribute(PatchLocation Attr, int64_t PcOffset) {
117  LocationAttributes.emplace_back(Attr, PcOffset);
118 }
119 
122  Namespaces.emplace_back(Name, Die);
123 }
124 
127  bool SkipPubSection) {
128  ObjC.emplace_back(Name, Die, SkipPubSection);
129 }
130 
133  bool SkipPubSection) {
134  Pubnames.emplace_back(Name, Die, SkipPubSection);
135 }
136 
139  bool ObjcClassImplementation,
140  uint32_t QualifiedNameHash) {
141  Pubtypes.emplace_back(Name, Die, QualifiedNameHash, ObjcClassImplementation);
142 }
143 
144 } // namespace llvm
A DeclContext is a named program scope that is used for ODR uniquing of types.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
uint32_t getCanonicalDIEOffset() const
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
Stores all information relating to a compile unit, be it in its original instance in the object file ...
void markEverythingAsKept()
Mark every DIE in this unit as kept.
void addNamespaceAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name)
Add a name accelerator entry for Die with Name.
void set(uint64_t New) const
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:195
uint32_t ParentIdx
The index of this DIE&#39;s parent.
The access may reference the value stored in memory.
uint8_t getAddressByteSize() const
Definition: DWARFUnit.h:284
void noteRangeAttribute(const DIE &Die, PatchLocation Attr)
Keep track of a DW_AT_range attribute that we will need to patch up later.
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...
void addNameAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name, bool SkipPubnamesSection=false)
Add a name accelerator entry for Die with Name.
static bool inFunctionScope(CompileUnit &U, unsigned Idx)
Check if the DIE at Idx is in the scope of a function.
Utility class that carries the DWARF compile/type unit and the debug info entry in an object...
Definition: DWARFDie.h:43
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...
A structured debug information entry.
Definition: DIE.h:712
uint64_t computeNextUnitOffset()
Compute the end offset for this unit.
uint64_t getStartOffset() const
void insert(KeyT a, KeyT b, ValT y)
insert - Add a mapping of [a;b] to y, coalesce with adjacent intervals.
Definition: IntervalMap.h:1082
DWARFDie getUnitDIE(bool ExtractUnitDIEOnly=true)
Definition: DWARFUnit.h:396
void noteLocationAttribute(PatchLocation Attr, int64_t PcOffset)
Keep track of a location attribute pointing to a location list in the debug_loc section.
DIEInfo & getInfo(unsigned Idx)
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:390
void addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset)
Add the low_pc of a label that is relocated by applying offset PCOffset.
void fixupForwardReferences()
Apply all fixups recorded by 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.
dwarf::Tag getTag() const
Definition: DIE.h:748
String pool entry reference.
DWARFDie getDIEAtIndex(unsigned Index)
Return the DIE object at the given index.
Definition: DWARFUnit.h:481
dwarf::Tag getTag() const
Definition: DWARFDie.h:72
#define I(x, y, z)
Definition: MD5.cpp:58
Optional< DWARFFormValue > find(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE.
Definition: DWARFDie.cpp:334
unsigned getOffset() const
Get the compile/type unit relative offset of this DIE.
Definition: DIE.h:750
Optional< uint64_t > toUnsigned(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an unsigned constant.
void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset)
Add a function range [LowPC, HighPC) that is relocated by applying offset PCOffset.
DWARFUnit & getOrigUnit() const