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