1//===- DWARFLinkerUnit.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_LIB_DWARFLINKER_PARALLEL_DWARFLINKERUNIT_H
10#define LLVM_LIB_DWARFLINKER_PARALLEL_DWARFLINKERUNIT_H
11
12#include "DWARFLinkerGlobalData.h"
13#include "OutputSections.h"
14#include "llvm/CodeGen/DIE.h"
15#include "llvm/DWARFLinker/IndexedValuesMap.h"
16#include "llvm/DWARFLinker/Parallel/DWARFLinker.h"
17#include "llvm/DWARFLinker/StringPool.h"
18#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
19#include "llvm/Support/LEB128.h"
20#include <memory>
21#include <mutex>
22
23namespace llvm {
24namespace dwarf_linker {
25namespace parallel {
26
27class DwarfUnit;
28using MacroOffset2UnitMapTy = DenseMap<uint64_t, DwarfUnit *>;
29
30/// Base class for all Dwarf units(Compile unit/Type table unit).
31class DwarfUnit : public OutputSections {
32public:
33 virtual ~DwarfUnit() = default;
34 DwarfUnit(LinkingGlobalData &GlobalData, unsigned ID,
35 StringRef ClangModuleName)
36 : OutputSections(GlobalData), ID(ID), ClangModuleName(ClangModuleName),
37 OutUnitDIE(nullptr) {}
38
39 /// Unique id of the unit.
40 unsigned getUniqueID() const { return ID; }
41
42 /// Returns size of this(newly generated) compile unit.
43 uint64_t getUnitSize() const { return UnitSize; }
44
45 /// Returns this unit name.
46 StringRef getUnitName() const { return UnitName; }
47
48 /// Return the DW_AT_LLVM_sysroot of the compile unit or an empty StringRef.
49 StringRef getSysRoot() { return SysRoot; }
50
51 /// Return true if this compile unit is from Clang module.
52 bool isClangModule() const { return !ClangModuleName.empty(); }
53
54 /// Return Clang module name;
55 const std::string &getClangModuleName() const { return ClangModuleName; }
56
57 /// Return global data.
58 LinkingGlobalData &getGlobalData() { return GlobalData; }
59
60 /// Returns true if unit is inter-connected(it references/referenced by other
61 /// unit).
62 bool isInterconnectedCU() const { return IsInterconnectedCU; }
63
64 /// Mark this unit as inter-connected(it references/referenced by other unit).
65 void setInterconnectedCU() { IsInterconnectedCU = true; }
66
67 /// Adds \p Abbrev into unit`s abbreviation table.
68 void assignAbbrev(DIEAbbrev &Abbrev);
69
70 /// Returns abbreviations for this compile unit.
71 const std::vector<std::unique_ptr<DIEAbbrev>> &getAbbreviations() const {
72 return Abbreviations;
73 }
74
75 /// Returns output unit DIE.
76 DIE *getOutUnitDIE() { return OutUnitDIE; }
77
78 /// Set output unit DIE.
79 void setOutUnitDIE(DIE *UnitDie) {
80 OutUnitDIE = UnitDie;
81
82 if (OutUnitDIE != nullptr) {
83 UnitSize = getDebugInfoHeaderSize() + OutUnitDIE->getSize();
84 UnitTag = OutUnitDIE->getTag();
85 }
86 }
87
88 /// Returns unit DWARF tag.
89 dwarf::Tag getTag() const { return UnitTag; }
90
91 /// \defgroup Methods used to emit unit's debug info:
92 ///
93 /// @{
94 /// Emit unit's abbreviations.
95 Error emitAbbreviations();
96
97 /// Emit .debug_info section for unit DIEs.
98 Error emitDebugInfo(const Triple &TargetTriple);
99
100 /// Emit .debug_line section. When \p OrigRowIndices is non-empty it
101 /// must be the same length as \p OutLineTable.Rows and carry the input
102 /// row index each output row originated from (or an invalid-row
103 /// sentinel for manufactured end-of-range rows); if
104 /// \p RowIndexToSeqStartOffset is non-null, the emitter populates it
105 /// with an entry for each real row mapping input row index to the
106 /// byte offset of the DW_LNE_set_address that opens the output
107 /// sequence containing the row.
108 Error emitDebugLine(
109 const Triple &TargetTriple, const DWARFDebugLine::LineTable &OutLineTable,
110 ArrayRef<uint64_t> OrigRowIndices = {},
111 DenseMap<uint64_t, uint64_t> *RowIndexToSeqStartOffset = nullptr);
112
113 /// Emit the .debug_str_offsets section for current unit.
114 Error emitDebugStringOffsetSection();
115 /// @}
116
117 /// \defgroup Methods used for reporting warnings and errors:
118 ///
119 /// @{
120 void warn(const Twine &Warning) { GlobalData.warn(Warning, Context: getUnitName()); }
121
122 void error(const Twine &Err) { GlobalData.warn(Warning: Err, Context: getUnitName()); }
123 /// @}
124
125 /// \defgroup Methods and data members used for building accelerator tables:
126 ///
127 /// @{
128
129 enum class AccelType : uint8_t { None, Name, Namespace, ObjC, Type };
130
131 /// This structure keeps fields which would be used for creating accelerator
132 /// table.
133 struct AccelInfo {
134 AccelInfo() {
135 AvoidForPubSections = false;
136 ObjcClassImplementation = false;
137 }
138
139 /// Name of the entry.
140 StringEntry *String = nullptr;
141
142 /// Output offset of the DIE this entry describes.
143 uint64_t OutOffset;
144
145 /// Output offset of the enclosing non-declaration DIE, used for the
146 /// DW_IDX_parent field of DWARF 5 name index entries.
147 std::optional<uint64_t> ParentOffset;
148
149 /// Hash of the fully qualified name.
150 uint32_t QualifiedNameHash = 0;
151
152 /// Tag of the DIE this entry describes.
153 dwarf::Tag Tag = dwarf::DW_TAG_null;
154
155 /// Type of this accelerator record.
156 AccelType Type = AccelType::None;
157
158 /// Avoid emitting this entry for pub sections.
159 bool AvoidForPubSections : 1;
160
161 /// Is this an ObjC class implementation?
162 bool ObjcClassImplementation : 1;
163 };
164
165 /// Emit .debug_pubnames and .debug_pubtypes for \p Unit.
166 void emitPubAccelerators();
167
168 /// Enumerates accelerator data.
169 virtual void
170 forEachAcceleratorRecord(function_ref<void(AccelInfo &)> Handler) = 0;
171
172 /// @}
173
174 /// Returns index(inside .debug_str_offsets) of specified string.
175 virtual uint64_t getDebugStrIndex(const StringEntry *String) {
176 return DebugStringIndexMap.getValueIndex(Value: String);
177 }
178
179protected:
180 /// Emit single abbreviation entry.
181 void emitDwarfAbbrevEntry(const DIEAbbrev &Abbrev,
182 SectionDescriptor &AbbrevSection);
183
184 /// Emit single pubnames/pubtypes accelerator entry.
185 std::optional<uint64_t>
186 emitPubAcceleratorEntry(SectionDescriptor &OutSection, const AccelInfo &Info,
187 std::optional<uint64_t> LengthOffset);
188
189 /// Unique ID for the unit.
190 unsigned ID = 0;
191
192 /// The name of this unit.
193 std::string UnitName;
194
195 /// The DW_AT_LLVM_sysroot of this unit.
196 std::string SysRoot;
197
198 /// If this is a Clang module, this holds the module's name.
199 std::string ClangModuleName;
200
201 uint64_t UnitSize = 0;
202
203 /// DWARF unit tag.
204 dwarf::Tag UnitTag = dwarf::DW_TAG_null;
205
206 /// true if current unit references_to/is_referenced by other unit.
207 std::atomic<bool> IsInterconnectedCU = {false};
208
209 /// FoldingSet that uniques the abbreviations.
210 FoldingSet<DIEAbbrev> AbbreviationsSet;
211
212 /// Storage for the unique Abbreviations.
213 std::vector<std::unique_ptr<DIEAbbrev>> Abbreviations;
214
215 /// Output unit DIE.
216 DIE *OutUnitDIE = nullptr;
217
218 /// Cache for file names for this unit. Entries are heap-allocated so the
219 /// StringRefs handed out by getDirAndFilenameFromLineTable keep pointing at
220 /// valid storage when a later insertion rehashes the map.
221 using FileNamesCache =
222 DenseMap<uint64_t, std::unique_ptr<std::pair<std::string, std::string>>>;
223 FileNamesCache FileNames;
224
225 /// Guards FileNames. During the parallel type-name assignment phase a unit's
226 /// cache is filled both by its own worker and, through cross-unit type-name
227 /// references, by other units' workers, so access must be serialized.
228 std::mutex FileNamesMutex;
229
230 /// Maps a string into the index inside .debug_str_offsets section.
231 IndexedValuesMap<const StringEntry *> DebugStringIndexMap;
232};
233
234inline bool isODRLanguage(uint16_t Language) {
235 switch (Language) {
236 case dwarf::DW_LANG_C_plus_plus:
237 case dwarf::DW_LANG_C_plus_plus_03:
238 case dwarf::DW_LANG_C_plus_plus_11:
239 case dwarf::DW_LANG_C_plus_plus_14:
240 case dwarf::DW_LANG_ObjC_plus_plus:
241 return true;
242 default:
243 return false;
244 };
245
246 return false;
247}
248
249} // end of namespace parallel
250} // end of namespace dwarf_linker
251} // end of namespace llvm
252
253#endif // LLVM_LIB_DWARFLINKER_PARALLEL_DWARFLINKERUNIT_H
254