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/AddressRanges.h"
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/CodeGen/DIE.h"
15 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
16 #include <optional>
17 
18 namespace llvm {
19 
20 class DeclContext;
21 
22 /// Mapped value in the address map is the offset to apply to the
23 /// linked address.
24 using RangesTy = AddressRangesMap;
25 
26 // This structure keeps patch for the attribute and, optionally,
27 // the value of relocation which should be applied. Currently,
28 // only location attribute needs to have relocation: either to the
29 // function ranges if location attribute is of type 'loclist',
30 // either to the operand of DW_OP_addr/DW_OP_addrx if location attribute
31 // is of type 'exprloc'.
32 // ASSUMPTION: Location attributes of 'loclist' type containing 'exprloc'
33 //             with address expression operands are not supported yet.
34 struct PatchLocation {
35   DIE::value_iterator I;
36   int64_t RelocAdjustment = 0;
37 
38   PatchLocation() = default;
PatchLocationPatchLocation39   PatchLocation(DIE::value_iterator I) : I(I) {}
PatchLocationPatchLocation40   PatchLocation(DIE::value_iterator I, int64_t Reloc)
41       : I(I), RelocAdjustment(Reloc) {}
42 
setPatchLocation43   void set(uint64_t New) const {
44     assert(I);
45     const auto &Old = *I;
46     assert(Old.getType() == DIEValue::isInteger);
47     *I = DIEValue(Old.getAttribute(), Old.getForm(), DIEInteger(New));
48   }
49 
getPatchLocation50   uint64_t get() const {
51     assert(I);
52     return I->getDIEInteger().getValue();
53   }
54 };
55 
56 using RngListAttributesTy = SmallVector<PatchLocation>;
57 using LocListAttributesTy = SmallVector<PatchLocation>;
58 
59 /// Stores all information relating to a compile unit, be it in its original
60 /// instance in the object file to its brand new cloned and generated DIE tree.
61 class CompileUnit {
62 public:
63   /// Information gathered about a DIE in the object file.
64   struct DIEInfo {
65     /// Address offset to apply to the described entity.
66     int64_t AddrAdjust;
67 
68     /// ODR Declaration context.
69     DeclContext *Ctxt;
70 
71     /// Cloned version of that DIE.
72     DIE *Clone;
73 
74     /// The index of this DIE's parent.
75     uint32_t ParentIdx;
76 
77     /// Is the DIE part of the linked output?
78     bool Keep : 1;
79 
80     /// Was this DIE's entity found in the map?
81     bool InDebugMap : 1;
82 
83     /// Is this a pure forward declaration we can strip?
84     bool Prune : 1;
85 
86     /// Does DIE transitively refer an incomplete decl?
87     bool Incomplete : 1;
88 
89     /// Is DIE in the clang module scope?
90     bool InModuleScope : 1;
91 
92     /// Is ODR marking done?
93     bool ODRMarkingDone : 1;
94 
95     /// Is this a reference to a DIE that hasn't been cloned yet?
96     bool UnclonedReference : 1;
97 
98     /// Is this a variable with a location attribute referencing address?
99     bool HasLocationExpressionAddr : 1;
100 
101 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
102     LLVM_DUMP_METHOD void dump();
103 #endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
104   };
105 
CompileUnit(DWARFUnit & OrigUnit,unsigned ID,bool CanUseODR,StringRef ClangModuleName)106   CompileUnit(DWARFUnit &OrigUnit, unsigned ID, bool CanUseODR,
107               StringRef ClangModuleName)
108       : OrigUnit(OrigUnit), ID(ID), ClangModuleName(ClangModuleName) {
109     Info.resize(OrigUnit.getNumDIEs());
110 
111     auto CUDie = OrigUnit.getUnitDIE(false);
112     if (!CUDie) {
113       HasODR = false;
114       return;
115     }
116     if (auto Lang = dwarf::toUnsigned(CUDie.find(dwarf::DW_AT_language)))
117       HasODR = CanUseODR && (*Lang == dwarf::DW_LANG_C_plus_plus ||
118                              *Lang == dwarf::DW_LANG_C_plus_plus_03 ||
119                              *Lang == dwarf::DW_LANG_C_plus_plus_11 ||
120                              *Lang == dwarf::DW_LANG_C_plus_plus_14 ||
121                              *Lang == dwarf::DW_LANG_ObjC_plus_plus);
122     else
123       HasODR = false;
124   }
125 
getOrigUnit()126   DWARFUnit &getOrigUnit() const { return OrigUnit; }
127 
getUniqueID()128   unsigned getUniqueID() const { return ID; }
129 
createOutputDIE()130   void createOutputDIE() { NewUnit.emplace(OrigUnit.getUnitDIE().getTag()); }
131 
getOutputUnitDIE()132   DIE *getOutputUnitDIE() const {
133     if (NewUnit)
134       return &const_cast<BasicDIEUnit &>(*NewUnit).getUnitDie();
135     return nullptr;
136   }
137 
hasODR()138   bool hasODR() const { return HasODR; }
isClangModule()139   bool isClangModule() const { return !ClangModuleName.empty(); }
140   uint16_t getLanguage();
141   /// Return the DW_AT_LLVM_sysroot of the compile unit or an empty StringRef.
142   StringRef getSysRoot();
143 
getClangModuleName()144   const std::string &getClangModuleName() const { return ClangModuleName; }
145 
getInfo(unsigned Idx)146   DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; }
getInfo(unsigned Idx)147   const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; }
148 
getInfo(const DWARFDie & Die)149   DIEInfo &getInfo(const DWARFDie &Die) {
150     unsigned Idx = getOrigUnit().getDIEIndex(Die);
151     return Info[Idx];
152   }
153 
getStartOffset()154   uint64_t getStartOffset() const { return StartOffset; }
getNextUnitOffset()155   uint64_t getNextUnitOffset() const { return NextUnitOffset; }
setStartOffset(uint64_t DebugInfoSize)156   void setStartOffset(uint64_t DebugInfoSize) { StartOffset = DebugInfoSize; }
157 
getLowPc()158   std::optional<uint64_t> getLowPc() const { return LowPc; }
getHighPc()159   uint64_t getHighPc() const { return HighPc; }
hasLabelAt(uint64_t Addr)160   bool hasLabelAt(uint64_t Addr) const { return Labels.count(Addr); }
161 
getFunctionRanges()162   const RangesTy &getFunctionRanges() const { return Ranges; }
163 
getRangesAttributes()164   const RngListAttributesTy &getRangesAttributes() { return RangeAttributes; }
165 
getUnitRangesAttribute()166   std::optional<PatchLocation> getUnitRangesAttribute() const {
167     return UnitRangeAttribute;
168   }
169 
getLocationAttributes()170   const LocListAttributesTy &getLocationAttributes() const {
171     return LocationAttributes;
172   }
173 
174   /// Mark every DIE in this unit as kept. This function also
175   /// marks variables as InDebugMap so that they appear in the
176   /// reconstructed accelerator tables.
177   void markEverythingAsKept();
178 
179   /// Compute the end offset for this unit. Must be called after the CU's DIEs
180   /// have been cloned.  \returns the next unit offset (which is also the
181   /// current debug_info section size).
182   uint64_t computeNextUnitOffset(uint16_t DwarfVersion);
183 
184   /// Keep track of a forward reference to DIE \p Die in \p RefUnit by \p
185   /// Attr. The attribute should be fixed up later to point to the absolute
186   /// offset of \p Die in the debug_info section or to the canonical offset of
187   /// \p Ctxt if it is non-null.
188   void noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
189                             DeclContext *Ctxt, PatchLocation Attr);
190 
191   /// Apply all fixups recorded by noteForwardReference().
192   void fixupForwardReferences();
193 
194   /// Add the low_pc of a label that is relocated by applying
195   /// offset \p PCOffset.
196   void addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset);
197 
198   /// Add a function range [\p LowPC, \p HighPC) that is relocated by applying
199   /// offset \p PCOffset.
200   void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset);
201 
202   /// Keep track of a DW_AT_range attribute that we will need to patch up later.
203   void noteRangeAttribute(const DIE &Die, PatchLocation Attr);
204 
205   /// Keep track of a location attribute pointing to a location list in the
206   /// debug_loc section.
207   void noteLocationAttribute(PatchLocation Attr);
208 
209   /// Add a name accelerator entry for \a Die with \a Name.
210   void addNamespaceAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name);
211 
212   /// Add a name accelerator entry for \a Die with \a Name.
213   void addNameAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
214                           bool SkipPubnamesSection = false);
215 
216   /// Add various accelerator entries for \p Die with \p Name which is stored
217   /// in the string table at \p Offset. \p Name must be an Objective-C
218   /// selector.
219   void addObjCAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
220                           bool SkipPubnamesSection = false);
221 
222   /// Add a type accelerator entry for \p Die with \p Name which is stored in
223   /// the string table at \p Offset.
224   void addTypeAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
225                           bool ObjcClassImplementation,
226                           uint32_t QualifiedNameHash);
227 
228   struct AccelInfo {
229     /// Name of the entry.
230     DwarfStringPoolEntryRef Name;
231 
232     /// DIE this entry describes.
233     const DIE *Die;
234 
235     /// Hash of the fully qualified name.
236     uint32_t QualifiedNameHash;
237 
238     /// Emit this entry only in the apple_* sections.
239     bool SkipPubSection;
240 
241     /// Is this an ObjC class implementation?
242     bool ObjcClassImplementation;
243 
244     AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die,
245               bool SkipPubSection = false)
NameAccelInfo246         : Name(Name), Die(Die), SkipPubSection(SkipPubSection) {}
247 
AccelInfoAccelInfo248     AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die,
249               uint32_t QualifiedNameHash, bool ObjCClassIsImplementation)
250         : Name(Name), Die(Die), QualifiedNameHash(QualifiedNameHash),
251           SkipPubSection(false),
252           ObjcClassImplementation(ObjCClassIsImplementation) {}
253   };
254 
getPubnames()255   const std::vector<AccelInfo> &getPubnames() const { return Pubnames; }
getPubtypes()256   const std::vector<AccelInfo> &getPubtypes() const { return Pubtypes; }
getNamespaces()257   const std::vector<AccelInfo> &getNamespaces() const { return Namespaces; }
getObjC()258   const std::vector<AccelInfo> &getObjC() const { return ObjC; }
259 
getLabelBegin()260   MCSymbol *getLabelBegin() { return LabelBegin; }
setLabelBegin(MCSymbol * S)261   void setLabelBegin(MCSymbol *S) { LabelBegin = S; }
262 
263 private:
264   DWARFUnit &OrigUnit;
265   unsigned ID;
266   std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index.
267   std::optional<BasicDIEUnit> NewUnit;
268   MCSymbol *LabelBegin = nullptr;
269 
270   uint64_t StartOffset;
271   uint64_t NextUnitOffset;
272 
273   std::optional<uint64_t> LowPc;
274   uint64_t HighPc = 0;
275 
276   /// A list of attributes to fixup with the absolute offset of
277   /// a DIE in the debug_info section.
278   ///
279   /// The offsets for the attributes in this array couldn't be set while
280   /// cloning because for cross-cu forward references the target DIE's offset
281   /// isn't known you emit the reference attribute.
282   std::vector<
283       std::tuple<DIE *, const CompileUnit *, DeclContext *, PatchLocation>>
284       ForwardDIEReferences;
285 
286   /// The ranges in that map are the PC ranges for functions in this unit,
287   /// associated with the PC offset to apply to the addresses to get
288   /// the linked address.
289   RangesTy Ranges;
290 
291   /// The DW_AT_low_pc of each DW_TAG_label.
292   SmallDenseMap<uint64_t, uint64_t, 1> Labels;
293 
294   /// 'rnglist'(DW_AT_ranges, DW_AT_start_scope) attributes to patch after
295   /// we have gathered all the unit's function addresses.
296   /// @{
297   RngListAttributesTy RangeAttributes;
298   std::optional<PatchLocation> UnitRangeAttribute;
299   /// @}
300 
301   /// Location attributes that need to be transferred from the
302   /// original debug_loc section to the linked one. They are stored
303   /// along with the PC offset that is to be applied to their
304   /// function's address or to be applied to address operands of
305   /// location expression.
306   LocListAttributesTy LocationAttributes;
307 
308   /// Accelerator entries for the unit, both for the pub*
309   /// sections and the apple* ones.
310   /// @{
311   std::vector<AccelInfo> Pubnames;
312   std::vector<AccelInfo> Pubtypes;
313   std::vector<AccelInfo> Namespaces;
314   std::vector<AccelInfo> ObjC;
315   /// @}
316 
317   /// Is this unit subject to the ODR rule?
318   bool HasODR;
319 
320   /// The DW_AT_language of this unit.
321   uint16_t Language = 0;
322 
323   /// The DW_AT_LLVM_sysroot of this unit.
324   std::string SysRoot;
325 
326   /// If this is a Clang module, this holds the module's name.
327   std::string ClangModuleName;
328 };
329 
330 } // end namespace llvm
331 
332 #endif // LLVM_DWARFLINKER_DWARFLINKERCOMPILEUNIT_H
333