LLVM  12.0.0git
DWARFLinkerCompileUnit.h
Go to the documentation of this file.
1 //===- DWARFLinkerCompileUnit.h ---------------------------------*- C++ -*-===//
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 
9 #ifndef LLVM_DWARFLINKER_DWARFLINKERCOMPILEUNIT_H
10 #define LLVM_DWARFLINKER_DWARFLINKERCOMPILEUNIT_H
11 
12 #include "llvm/ADT/IntervalMap.h"
13 #include "llvm/CodeGen/DIE.h"
16 
17 namespace llvm {
18 
19 class DeclContext;
20 
21 template <typename KeyT, typename ValT>
22 using HalfOpenIntervalMap =
23  IntervalMap<KeyT, ValT, IntervalMapImpl::NodeSizer<KeyT, ValT>::LeafSize,
25 
27 
28 // FIXME: Delete this structure.
29 struct PatchLocation {
31 
32  PatchLocation() = default;
34 
35  void set(uint64_t New) const {
36  assert(I);
37  const auto &Old = *I;
38  assert(Old.getType() == DIEValue::isInteger);
39  *I = DIEValue(Old.getAttribute(), Old.getForm(), DIEInteger(New));
40  }
41 
42  uint64_t get() const {
43  assert(I);
44  return I->getDIEInteger().getValue();
45  }
46 };
47 
48 /// Stores all information relating to a compile unit, be it in its original
49 /// instance in the object file to its brand new cloned and generated DIE tree.
50 class CompileUnit {
51 public:
52  /// Information gathered about a DIE in the object file.
53  struct DIEInfo {
54  /// Address offset to apply to the described entity.
55  int64_t AddrAdjust;
56 
57  /// ODR Declaration context.
59 
60  /// Cloned version of that DIE.
62 
63  /// The index of this DIE's parent.
65 
66  /// Is the DIE part of the linked output?
67  bool Keep : 1;
68 
69  /// Was this DIE's entity found in the map?
70  bool InDebugMap : 1;
71 
72  /// Is this a pure forward declaration we can strip?
73  bool Prune : 1;
74 
75  /// Does DIE transitively refer an incomplete decl?
76  bool Incomplete : 1;
77  };
78 
79  CompileUnit(DWARFUnit &OrigUnit, unsigned ID, bool CanUseODR,
80  StringRef ClangModuleName)
81  : OrigUnit(OrigUnit), ID(ID), Ranges(RangeAlloc),
82  ClangModuleName(ClangModuleName) {
83  Info.resize(OrigUnit.getNumDIEs());
84 
85  auto CUDie = OrigUnit.getUnitDIE(false);
86  if (!CUDie) {
87  HasODR = false;
88  return;
89  }
90  if (auto Lang = dwarf::toUnsigned(CUDie.find(dwarf::DW_AT_language)))
91  HasODR = CanUseODR && (*Lang == dwarf::DW_LANG_C_plus_plus ||
92  *Lang == dwarf::DW_LANG_C_plus_plus_03 ||
93  *Lang == dwarf::DW_LANG_C_plus_plus_11 ||
94  *Lang == dwarf::DW_LANG_C_plus_plus_14 ||
95  *Lang == dwarf::DW_LANG_ObjC_plus_plus);
96  else
97  HasODR = false;
98  }
99 
100  DWARFUnit &getOrigUnit() const { return OrigUnit; }
101 
102  unsigned getUniqueID() const { return ID; }
103 
105  NewUnit.emplace(OrigUnit.getVersion(), OrigUnit.getAddressByteSize(),
106  OrigUnit.getUnitDIE().getTag());
107  }
108 
110  if (NewUnit)
111  return &const_cast<BasicDIEUnit &>(*NewUnit).getUnitDie();
112  return nullptr;
113  }
114 
115  bool hasODR() const { return HasODR; }
116  bool isClangModule() const { return !ClangModuleName.empty(); }
118  /// Return the DW_AT_LLVM_sysroot of the compile unit or an empty StringRef.
119  StringRef getSysRoot();
120 
121  const std::string &getClangModuleName() const { return ClangModuleName; }
122 
123  DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; }
124  const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; }
125 
126  uint64_t getStartOffset() const { return StartOffset; }
127  uint64_t getNextUnitOffset() const { return NextUnitOffset; }
128  void setStartOffset(uint64_t DebugInfoSize) { StartOffset = DebugInfoSize; }
129 
130  uint64_t getLowPc() const { return LowPc; }
131  uint64_t getHighPc() const { return HighPc; }
132  bool hasLabelAt(uint64_t Addr) const { return Labels.count(Addr); }
133 
135  return UnitRangeAttribute;
136  }
137 
138  const FunctionIntervals &getFunctionRanges() const { return Ranges; }
139 
140  const std::vector<PatchLocation> &getRangesAttributes() const {
141  return RangeAttributes;
142  }
143 
144  const std::vector<std::pair<PatchLocation, int64_t>> &
146  return LocationAttributes;
147  }
148 
149  void setHasInterestingContent() { HasInterestingContent = true; }
150  bool hasInterestingContent() { return HasInterestingContent; }
151 
152  /// Mark every DIE in this unit as kept. This function also
153  /// marks variables as InDebugMap so that they appear in the
154  /// reconstructed accelerator tables.
155  void markEverythingAsKept();
156 
157  /// Compute the end offset for this unit. Must be called after the CU's DIEs
158  /// have been cloned. \returns the next unit offset (which is also the
159  /// current debug_info section size).
160  uint64_t computeNextUnitOffset();
161 
162  /// Keep track of a forward reference to DIE \p Die in \p RefUnit by \p
163  /// Attr. The attribute should be fixed up later to point to the absolute
164  /// offset of \p Die in the debug_info section or to the canonical offset of
165  /// \p Ctxt if it is non-null.
166  void noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
167  DeclContext *Ctxt, PatchLocation Attr);
168 
169  /// Apply all fixups recorded by noteForwardReference().
170  void fixupForwardReferences();
171 
172  /// Add the low_pc of a label that is relocated by applying
173  /// offset \p PCOffset.
174  void addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset);
175 
176  /// Add a function range [\p LowPC, \p HighPC) that is relocated by applying
177  /// offset \p PCOffset.
178  void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset);
179 
180  /// Keep track of a DW_AT_range attribute that we will need to patch up later.
181  void noteRangeAttribute(const DIE &Die, PatchLocation Attr);
182 
183  /// Keep track of a location attribute pointing to a location list in the
184  /// debug_loc section.
185  void noteLocationAttribute(PatchLocation Attr, int64_t PcOffset);
186 
187  /// Add a name accelerator entry for \a Die with \a Name.
188  void addNamespaceAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name);
189 
190  /// Add a name accelerator entry for \a Die with \a Name.
191  void addNameAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
192  bool SkipPubnamesSection = false);
193 
194  /// Add various accelerator entries for \p Die with \p Name which is stored
195  /// in the string table at \p Offset. \p Name must be an Objective-C
196  /// selector.
197  void addObjCAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
198  bool SkipPubnamesSection = false);
199 
200  /// Add a type accelerator entry for \p Die with \p Name which is stored in
201  /// the string table at \p Offset.
202  void addTypeAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
203  bool ObjcClassImplementation,
204  uint32_t QualifiedNameHash);
205 
206  struct AccelInfo {
207  /// Name of the entry.
209 
210  /// DIE this entry describes.
211  const DIE *Die;
212 
213  /// Hash of the fully qualified name.
215 
216  /// Emit this entry only in the apple_* sections.
218 
219  /// Is this an ObjC class implementation?
221 
223  bool SkipPubSection = false)
224  : Name(Name), Die(Die), SkipPubSection(SkipPubSection) {}
225 
227  uint32_t QualifiedNameHash, bool ObjCClassIsImplementation)
228  : Name(Name), Die(Die), QualifiedNameHash(QualifiedNameHash),
229  SkipPubSection(false),
230  ObjcClassImplementation(ObjCClassIsImplementation) {}
231  };
232 
233  const std::vector<AccelInfo> &getPubnames() const { return Pubnames; }
234  const std::vector<AccelInfo> &getPubtypes() const { return Pubtypes; }
235  const std::vector<AccelInfo> &getNamespaces() const { return Namespaces; }
236  const std::vector<AccelInfo> &getObjC() const { return ObjC; }
237 
238  /// Get the full path for file \a FileNum in the line table
239  StringRef getResolvedPath(unsigned FileNum) {
240  if (FileNum >= ResolvedPaths.size())
241  return StringRef();
242  return ResolvedPaths[FileNum];
243  }
244 
245  /// Set the fully resolved path for the line-table's file \a FileNum
246  /// to \a Path.
247  void setResolvedPath(unsigned FileNum, StringRef Path) {
248  if (ResolvedPaths.size() <= FileNum)
249  ResolvedPaths.resize(FileNum + 1);
250  ResolvedPaths[FileNum] = Path;
251  }
252 
253  MCSymbol *getLabelBegin() { return LabelBegin; }
254  void setLabelBegin(MCSymbol *S) { LabelBegin = S; }
255 
256 private:
257  DWARFUnit &OrigUnit;
258  unsigned ID;
259  std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index.
260  Optional<BasicDIEUnit> NewUnit;
261  MCSymbol *LabelBegin = nullptr;
262 
263  uint64_t StartOffset;
264  uint64_t NextUnitOffset;
265 
266  uint64_t LowPc = std::numeric_limits<uint64_t>::max();
267  uint64_t HighPc = 0;
268 
269  /// A list of attributes to fixup with the absolute offset of
270  /// a DIE in the debug_info section.
271  ///
272  /// The offsets for the attributes in this array couldn't be set while
273  /// cloning because for cross-cu forward references the target DIE's offset
274  /// isn't known you emit the reference attribute.
275  std::vector<
276  std::tuple<DIE *, const CompileUnit *, DeclContext *, PatchLocation>>
277  ForwardDIEReferences;
278 
279  FunctionIntervals::Allocator RangeAlloc;
280 
281  /// The ranges in that interval map are the PC ranges for
282  /// functions in this unit, associated with the PC offset to apply
283  /// to the addresses to get the linked address.
284  FunctionIntervals Ranges;
285 
286  /// The DW_AT_low_pc of each DW_TAG_label.
288 
289  /// DW_AT_ranges attributes to patch after we have gathered
290  /// all the unit's function addresses.
291  /// @{
292  std::vector<PatchLocation> RangeAttributes;
293  Optional<PatchLocation> UnitRangeAttribute;
294  /// @}
295 
296  /// Location attributes that need to be transferred from the
297  /// original debug_loc section to the liked one. They are stored
298  /// along with the PC offset that is to be applied to their
299  /// function's address.
300  std::vector<std::pair<PatchLocation, int64_t>> LocationAttributes;
301 
302  /// Accelerator entries for the unit, both for the pub*
303  /// sections and the apple* ones.
304  /// @{
305  std::vector<AccelInfo> Pubnames;
306  std::vector<AccelInfo> Pubtypes;
307  std::vector<AccelInfo> Namespaces;
308  std::vector<AccelInfo> ObjC;
309  /// @}
310 
311  /// Cached resolved paths from the line table.
312  /// Note, the StringRefs here point in to the intern (uniquing) string pool.
313  /// This means that a StringRef returned here doesn't need to then be uniqued
314  /// for the purposes of getting a unique address for each string.
315  std::vector<StringRef> ResolvedPaths;
316 
317  /// Is this unit subject to the ODR rule?
318  bool HasODR;
319 
320  /// Did a DIE actually contain a valid reloc?
321  bool HasInterestingContent;
322 
323  /// The DW_AT_language of this unit.
324  uint16_t Language = 0;
325 
326  /// The DW_AT_LLVM_sysroot of this unit.
327  std::string SysRoot;
328 
329  /// If this is a Clang module, this holds the module's name.
330  std::string ClangModuleName;
331 };
332 
333 } // end namespace llvm
334 
335 #endif // LLVM_DWARFLINKER_DWARFLINKERCOMPILEUNIT_H
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
DEBUG_TYPE to vector
void setLabelBegin(MCSymbol *S)
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
uint64_t getLowPc() const
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die, bool SkipPubSection=false)
bool hasLabelAt(uint64_t Addr) const
Stores all information relating to a compile unit, be it in its original instance in the object file ...
bool SkipPubSection
Emit this entry only in the apple_* sections.
uint64_t getNextUnitOffset() const
bool InDebugMap
Was this DIE&#39;s entity found in the map?
constexpr char Language[]
Key for Kernel::Metadata::mLanguage.
int64_t AddrAdjust
Address offset to apply to the described entity.
const std::vector< PatchLocation > & getRangesAttributes() const
uint32_t ParentIdx
The index of this DIE&#39;s parent.
PatchLocation()=default
CompileUnit(DWARFUnit &OrigUnit, unsigned ID, bool CanUseODR, StringRef ClangModuleName)
Hold the input and output of the debug info size in bytes.
Definition: DWARFLinker.cpp:38
unsigned getNumDIEs()
Returns the number of DIEs in the unit.
Definition: DWARFUnit.h:450
const std::vector< std::pair< PatchLocation, int64_t > > & getLocationAttributes() const
Analysis containing CSE Info
Definition: CSEInfo.cpp:25
void setResolvedPath(unsigned FileNum, StringRef Path)
Set the fully resolved path for the line-table&#39;s file FileNum to Path.
unsigned getUniqueID() const
StringRef getResolvedPath(unsigned FileNum)
Get the full path for file FileNum in the line table.
typename Sizer::Allocator Allocator
Definition: IntervalMap.h:959
A structured debug information entry.
Definition: DIE.h:723
DIE & getUnitDie()
Definition: DIE.h:902
bool Keep
Is the DIE part of the linked output?
DIE * Clone
Cloned version of that DIE.
uint64_t getStartOffset() const
bool Prune
Is this a pure forward declaration we can strip?
unsigned getLanguage(StringRef LanguageString)
Definition: Dwarf.cpp:332
Optional< PatchLocation > getUnitRangesAttribute() const
PatchLocation(DIE::value_iterator I)
uint32_t QualifiedNameHash
Hash of the fully qualified name.
DeclContext * Ctxt
ODR Declaration context.
DWARFDie getUnitDIE(bool ExtractUnitDIEOnly=true)
Definition: DWARFUnit.h:381
DIE * getOutputUnitDIE() const
DIEInfo & getInfo(unsigned Idx)
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:350
const std::string & getClangModuleName() const
const std::vector< AccelInfo > & getPubnames() const
const std::vector< AccelInfo > & getNamespaces() const
const std::vector< AccelInfo > & getObjC() const
DwarfStringPoolEntryRef Name
Name of the entry.
String pool entry reference.
void setStartOffset(uint64_t DebugInfoSize)
An integer value DIE.
Definition: DIE.h:163
const DIE * Die
DIE this entry describes.
const DIEInfo & getInfo(unsigned Idx) const
IntervalMap< KeyT, ValT, IntervalMapImpl::NodeSizer< KeyT, ValT >::LeafSize, IntervalMapHalfOpenInfo< KeyT > > HalfOpenIntervalMap
const std::vector< AccelInfo > & getPubtypes() const
AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die, uint32_t QualifiedNameHash, bool ObjCClassIsImplementation)
const FunctionIntervals & getFunctionRanges() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
DIE::value_iterator I
bool ObjcClassImplementation
Is this an ObjC class implementation?
bool Incomplete
Does DIE transitively refer an incomplete decl?
uint64_t getHighPc() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
Information gathered about a DIE in the object file.
Optional< uint64_t > toUnsigned(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an unsigned constant.
DWARFUnit & getOrigUnit() const