LLVM 20.0.0git
DWARFLinkerTypeUnit.cpp
Go to the documentation of this file.
1//===- DWARFLinkerTypeUnit.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
10#include "DIEGenerator.h"
11#include "DWARFEmitterImpl.h"
12#include "llvm/Support/LEB128.h"
13
14using namespace llvm;
15using namespace dwarf_linker;
16using namespace dwarf_linker::parallel;
17
19 std::optional<uint16_t> Language, dwarf::FormParams Format,
20 endianness Endianess)
21 : DwarfUnit(GlobalData, ID, ""), Language(Language),
22 AcceleratorRecords(&GlobalData.getAllocator()) {
23
24 UnitName = "__artificial_type_unit";
25
26 setOutputFormat(Format, Endianess);
27
28 // Create line table prologue.
29 LineTable.Prologue.FormParams = getFormParams();
30 LineTable.Prologue.MinInstLength = 1;
31 LineTable.Prologue.MaxOpsPerInst = 1;
32 LineTable.Prologue.DefaultIsStmt = 1;
33 LineTable.Prologue.LineBase = -5;
34 LineTable.Prologue.LineRange = 14;
35 LineTable.Prologue.OpcodeBase = 13;
36 LineTable.Prologue.StandardOpcodeLengths = {0, 1, 1, 1, 1, 0,
37 0, 0, 1, 0, 0, 1};
38
40}
41
43 prepareDataForTreeCreation();
44
45 // TaskGroup is created here as internal code has calls to
46 // PerThreadBumpPtrAllocator which should be called from the task group task.
48 TG.spawn([&]() {
49 SectionDescriptor &DebugInfoSection =
51 SectionDescriptor &DebugLineSection =
53
54 DIEGenerator DIETreeGenerator(Allocator, *this);
55 OffsetsPtrVector PatchesOffsets;
56
57 // Create a Die for artificial compilation unit for types.
58 DIE *UnitDIE = DIETreeGenerator.createDIE(dwarf::DW_TAG_compile_unit, 0);
59 uint64_t OutOffset = getDebugInfoHeaderSize();
60 UnitDIE->setOffset(OutOffset);
61
62 SmallString<200> ProducerString;
63 ProducerString += "llvm DWARFLinkerParallel library version ";
64 DebugInfoSection.notePatchWithOffsetUpdate(
66 {OutOffset},
67 GlobalData.getStringPool().insert(ProducerString.str()).first},
68 PatchesOffsets);
69 OutOffset += DIETreeGenerator
70 .addStringPlaceholderAttribute(dwarf::DW_AT_producer,
71 dwarf::DW_FORM_strp)
72 .second;
73
74 if (Language) {
75 OutOffset += DIETreeGenerator
76 .addScalarAttribute(dwarf::DW_AT_language,
77 dwarf::DW_FORM_data2, *Language)
78 .second;
79 }
80
81 DebugInfoSection.notePatchWithOffsetUpdate(
82 DebugStrPatch{{OutOffset},
83 GlobalData.getStringPool().insert(getUnitName()).first},
84 PatchesOffsets);
85 OutOffset += DIETreeGenerator
86 .addStringPlaceholderAttribute(dwarf::DW_AT_name,
87 dwarf::DW_FORM_strp)
88 .second;
89
90 if (!LineTable.Prologue.FileNames.empty()) {
91 DebugInfoSection.notePatchWithOffsetUpdate(
92 DebugOffsetPatch{OutOffset, &DebugLineSection}, PatchesOffsets);
93
94 OutOffset += DIETreeGenerator
95 .addScalarAttribute(dwarf::DW_AT_stmt_list,
96 dwarf::DW_FORM_sec_offset, 0xbaddef)
97 .second;
98 }
99
100 DebugInfoSection.notePatchWithOffsetUpdate(
101 DebugStrPatch{{OutOffset}, GlobalData.getStringPool().insert("").first},
102 PatchesOffsets);
103 OutOffset += DIETreeGenerator
104 .addStringPlaceholderAttribute(dwarf::DW_AT_comp_dir,
105 dwarf::DW_FORM_strp)
106 .second;
107
108 if (!DebugStringIndexMap.empty()) {
109 // Type unit is assumed to be emitted first. Thus we can use direct value
110 // for DW_AT_str_offsets_base attribute(No need to fix it up with unit
111 // offset value).
112 OutOffset += DIETreeGenerator
113 .addScalarAttribute(dwarf::DW_AT_str_offsets_base,
114 dwarf::DW_FORM_sec_offset,
116 .second;
117 }
118
119 UnitDIE->setSize(OutOffset - UnitDIE->getOffset() + 1);
120 OutOffset =
121 finalizeTypeEntryRec(UnitDIE->getOffset(), UnitDIE, Types.getRoot());
122
123 // Update patch offsets.
124 for (uint64_t *OffsetPtr : PatchesOffsets)
125 *OffsetPtr += getULEB128Size(UnitDIE->getAbbrevNumber());
126
127 setOutUnitDIE(UnitDIE);
128 });
129}
130
131void TypeUnit::prepareDataForTreeCreation() {
132 SectionDescriptor &DebugInfoSection =
134
135 // Type unit data created parallelly. So the order of data is not
136 // deterministic. Order data here if we need deterministic output.
137
139
141 TG.spawn([&]() {
142 // Sort types to have a deterministic output.
143 Types.sortTypes();
144 });
145 }
146
147 TG.spawn([&]() {
149 // Sort decl type patches to have a deterministic output.
150 std::function<bool(const DebugTypeDeclFilePatch &LHS,
151 const DebugTypeDeclFilePatch &RHS)>
152 PatchesComparator = [&](const DebugTypeDeclFilePatch &LHS,
153 const DebugTypeDeclFilePatch &RHS) {
154 return LHS.Directory->first() < RHS.Directory->first() ||
155 (!(RHS.Directory->first() < LHS.Directory->first()) &&
156 LHS.FilePath->first() < RHS.FilePath->first());
157 };
158 // Sort patches to have a deterministic output.
159 DebugInfoSection.ListDebugTypeDeclFilePatch.sort(PatchesComparator);
160 }
161
162 // Update DW_AT_decl_file attribute
163 dwarf::Form DeclFileForm =
164 getScalarFormForValue(
165 DebugInfoSection.ListDebugTypeDeclFilePatch.size())
166 .first;
167
168 DebugInfoSection.ListDebugTypeDeclFilePatch.forEach(
169 [&](DebugTypeDeclFilePatch &Patch) {
172 formatv("No data for type {0}", Patch.TypeName->getKey())
173 .str()
174 .c_str());
175 if (&TypeEntry->getFinalDie() != Patch.Die)
176 return;
177
178 uint32_t FileIdx =
179 addFileNameIntoLinetable(Patch.Directory, Patch.FilePath);
180
181 unsigned DIESize = Patch.Die->getSize();
182 DIEGenerator DIEGen(Patch.Die, Types.getThreadLocalAllocator(),
183 *this);
184
185 DIESize += DIEGen
186 .addScalarAttribute(dwarf::DW_AT_decl_file,
187 DeclFileForm, FileIdx)
188 .second;
189 Patch.Die->setSize(DIESize);
190 });
191 });
192
193 if (!GlobalData.getOptions().AllowNonDeterministicOutput) {
194 // Sort patches to have a deterministic output.
195 TG.spawn([&]() {
196 forEach([&](SectionDescriptor &OutSection) {
197 std::function<bool(const DebugStrPatch &LHS, const DebugStrPatch &RHS)>
198 StrPatchesComparator =
199 [&](const DebugStrPatch &LHS, const DebugStrPatch &RHS) {
200 return LHS.String->getKey() < RHS.String->getKey();
201 };
202 OutSection.ListDebugStrPatch.sort(StrPatchesComparator);
203
204 std::function<bool(const DebugTypeStrPatch &LHS,
205 const DebugTypeStrPatch &RHS)>
206 TypeStrPatchesComparator = [&](const DebugTypeStrPatch &LHS,
207 const DebugTypeStrPatch &RHS) {
208 return LHS.String->getKey() < RHS.String->getKey();
209 };
210 OutSection.ListDebugTypeStrPatch.sort(TypeStrPatchesComparator);
211 });
212 });
213 }
214
215 if (!GlobalData.getOptions().AllowNonDeterministicOutput) {
216 // Sort patches to have a deterministic output.
217 TG.spawn([&]() {
218 forEach([&](SectionDescriptor &OutSection) {
219 std::function<bool(const DebugLineStrPatch &LHS,
220 const DebugLineStrPatch &RHS)>
221 LineStrPatchesComparator = [&](const DebugLineStrPatch &LHS,
222 const DebugLineStrPatch &RHS) {
223 return LHS.String->getKey() < RHS.String->getKey();
224 };
225 OutSection.ListDebugLineStrPatch.sort(LineStrPatchesComparator);
226
227 std::function<bool(const DebugTypeLineStrPatch &LHS,
228 const DebugTypeLineStrPatch &RHS)>
229 TypeLineStrPatchesComparator =
230 [&](const DebugTypeLineStrPatch &LHS,
231 const DebugTypeLineStrPatch &RHS) {
232 return LHS.String->getKey() < RHS.String->getKey();
233 };
234 OutSection.ListDebugTypeLineStrPatch.sort(TypeLineStrPatchesComparator);
235 });
236 });
237 }
238}
239
240uint64_t TypeUnit::finalizeTypeEntryRec(uint64_t OutOffset, DIE *OutDIE,
241 TypeEntry *Entry) {
242 bool HasChildren = !Entry->getValue().load()->Children.empty();
243 DIEGenerator DIEGen(OutDIE, Types.getThreadLocalAllocator(), *this);
244 OutOffset += DIEGen.finalizeAbbreviations(HasChildren, nullptr);
245 OutOffset += OutDIE->getSize() - 1;
246
247 if (HasChildren) {
248 Entry->getValue().load()->Children.forEach([&](TypeEntry *ChildEntry) {
249 DIE *ChildDIE = &ChildEntry->getValue().load()->getFinalDie();
250 DIEGen.addChild(ChildDIE);
251
252 ChildDIE->setOffset(OutOffset);
253
254 OutOffset = finalizeTypeEntryRec(OutOffset, ChildDIE, ChildEntry);
255 });
256
257 // End of children marker.
258 OutOffset += sizeof(int8_t);
259 }
260
261 OutDIE->setSize(OutOffset - OutDIE->getOffset());
262 return OutOffset;
263}
264
265uint32_t TypeUnit::addFileNameIntoLinetable(StringEntry *Dir,
266 StringEntry *FileName) {
267 uint32_t DirIdx = 0;
268
269 if (Dir->first() == "") {
270 DirIdx = 0;
271 } else {
272 DirectoriesMapTy::iterator DirEntry = DirectoriesMap.find(Dir);
273 if (DirEntry == DirectoriesMap.end()) {
274 // We currently do not support more than UINT32_MAX directories.
275 assert(LineTable.Prologue.IncludeDirectories.size() < UINT32_MAX);
276 DirIdx = LineTable.Prologue.IncludeDirectories.size();
277 DirectoriesMap.insert({Dir, DirIdx});
278 LineTable.Prologue.IncludeDirectories.push_back(
279 DWARFFormValue::createFromPValue(dwarf::DW_FORM_string,
280 Dir->getKeyData()));
281 } else {
282 DirIdx = DirEntry->second;
283 }
284
285 if (getVersion() < 5)
286 DirIdx++;
287 }
288
289 auto [FileEntry, Inserted] = FileNamesMap.try_emplace(
290 {FileName, DirIdx}, LineTable.Prologue.FileNames.size());
291 if (Inserted) {
292 // We currently do not support more than UINT32_MAX files.
293 assert(LineTable.Prologue.FileNames.size() < UINT32_MAX);
294 LineTable.Prologue.FileNames.push_back(DWARFDebugLine::FileNameEntry());
296 dwarf::DW_FORM_string, FileName->getKeyData());
297 LineTable.Prologue.FileNames.back().DirIdx = DirIdx;
298 }
299
300 uint32_t FileIdx = FileEntry->second;
301 return getVersion() < 5 ? FileIdx + 1 : FileIdx;
302}
303
304std::pair<dwarf::Form, uint8_t>
305TypeUnit::getScalarFormForValue(uint64_t Value) const {
306 if (Value > 0xFFFFFFFF)
307 return std::make_pair(dwarf::DW_FORM_data8, 8);
308
309 if (Value > 0xFFFF)
310 return std::make_pair(dwarf::DW_FORM_data4, 4);
311
312 if (Value > 0xFF)
313 return std::make_pair(dwarf::DW_FORM_data2, 2);
314
315 return std::make_pair(dwarf::DW_FORM_data1, 1);
316}
317
318uint8_t TypeUnit::getSizeByAttrForm(dwarf::Form Form) const {
319 if (Form == dwarf::DW_FORM_data1)
320 return 1;
321
322 if (Form == dwarf::DW_FORM_data2)
323 return 2;
324
325 if (Form == dwarf::DW_FORM_data4)
326 return 4;
327
328 if (Form == dwarf::DW_FORM_data8)
329 return 8;
330
331 if (Form == dwarf::DW_FORM_data16)
332 return 16;
333
334 llvm_unreachable("Unsupported Attr Form");
335}
336
340
341 if (getOutUnitDIE() == nullptr)
342 return Error::success();
343
344 // Create sections ahead so that they should not be created asynchronously
345 // later.
354 }
355
356 SmallVector<std::function<Error(void)>> Tasks;
357
358 // Add task for emitting .debug_line section.
359 if (!LineTable.Prologue.FileNames.empty()) {
360 Tasks.push_back(
361 [&]() -> Error { return emitDebugLine(TargetTriple, LineTable); });
362 }
363
364 // Add task for emitting .debug_info section.
365 Tasks.push_back([&]() -> Error { return emitDebugInfo(TargetTriple); });
366
367 // Add task for emitting Pub accelerator sections.
370 Tasks.push_back([&]() -> Error {
372 return Error::success();
373 });
374 }
375
376 // Add task for emitting .debug_str_offsets section.
377 Tasks.push_back([&]() -> Error { return emitDebugStringOffsetSection(); });
378
379 // Add task for emitting .debug_abbr section.
380 Tasks.push_back([&]() -> Error { return emitAbbreviations(); });
381
382 if (auto Err = parallelForEachError(
383 Tasks, [&](std::function<Error(void)> F) { return F(); }))
384 return Err;
385
386 return Error::success();
387}
AMDGPU Mark last scratch load
#define F(x, y, z)
Definition: MD5.cpp:55
Basic Register Allocator
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Value * RHS
Value * LHS
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
A structured debug information entry.
Definition: DIE.h:819
unsigned getAbbrevNumber() const
Definition: DIE.h:854
unsigned getSize() const
Definition: DIE.h:862
void setSize(unsigned S)
Definition: DIE.h:931
unsigned getOffset() const
Get the compile/type unit relative offset of this DIE.
Definition: DIE.h:857
void setOffset(unsigned O)
Definition: DIE.h:930
static DWARFFormValue createFromPValue(dwarf::Form F, const char *V)
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:254
const ValueTy & getValue() const
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
StringRef getKey() const
StringRef first() const
const char * getKeyData() const
getKeyData - Return the start of the string data that is the key for this value.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
LLVM Value Representation.
Definition: Value.h:74
This class is a helper to create output DIE tree.
Definition: DIEGenerator.h:22
std::pair< DIEValue &, size_t > addStringPlaceholderAttribute(dwarf::Attribute Attr, dwarf::Form AttrForm)
Adds string attribute with dummy offset to the current DIE.
Definition: DIEGenerator.h:109
DIE * createDIE(dwarf::Tag DieTag, uint32_t OutOffset)
Creates a DIE of specified tag DieTag and OutOffset.
Definition: DIEGenerator.h:31
std::pair< DIEValue &, size_t > addScalarAttribute(dwarf::Attribute Attr, dwarf::Form AttrForm, uint64_t Value)
Adds specified scalar attribute to the current DIE.
Definition: DIEGenerator.h:50
Base class for all Dwarf units(Compile unit/Type table unit).
std::string UnitName
The name of this unit.
IndexedValuesMap< const StringEntry * > DebugStringIndexMap
Maps a string into the index inside .debug_str_offsets section.
StringRef getUnitName() const
Returns this unit name.
void setOutUnitDIE(DIE *UnitDie)
Set output unit DIE.
DIE * getOutUnitDIE()
Returns output unit DIE.
This class keeps data and services common for the whole linking process.
const DWARFLinkerOptions & getOptions() const
Returns linking options.
StringPool & getStringPool()
Returns global string pool.
uint16_t getDebugStrOffsetsHeaderSize() const
Return size of header of debug_str_offsets table.
dwarf::FormParams Format
Format for sections.
const dwarf::FormParams & getFormParams() const
Return size of address.
void setOutputFormat(dwarf::FormParams Format, llvm::endianness Endianness)
Sets output format for all keeping sections.
uint16_t getVersion() const
Return DWARF version.
uint16_t getDebugInfoHeaderSize() const
Return size of header of debug_info table.
SectionDescriptor & getOrCreateSectionDescriptor(DebugSectionKind SectionKind)
Returns descriptor for the specified section of SectionKind.
Keeps cloned data for the type DIE.
Definition: TypePool.h:30
BumpPtrAllocator & getThreadLocalAllocator()
Return thread local allocator used by pool.
Definition: TypePool.h:161
TypeEntry * getRoot() const
Return root for all type entries.
Definition: TypePool.h:158
void sortTypes()
Sort children for each kept type entry.
Definition: TypePool.h:147
Error finishCloningAndEmit(const Triple &TargetTriple)
Emits resulting dwarf based on information from DIE tree.
void createDIETree(BumpPtrAllocator &Allocator)
Generates DIE tree based on information from TypesMap.
TypeUnit(LinkingGlobalData &GlobalData, unsigned ID, std::optional< uint16_t > Language, dwarf::FormParams Format, llvm::endianness Endianess)
void spawn(std::function< void()> f)
Definition: Parallel.cpp:194
Error emitDebugInfo(const Triple &TargetTriple)
Emit .debug_info section for unit DIEs.
Error emitDebugStringOffsetSection()
Emit the .debug_str_offsets section for current unit.
void emitPubAccelerators()
Emit .debug_pubnames and .debug_pubtypes for Unit.
Error emitDebugLine(const Triple &TargetTriple, const DWARFDebugLine::LineTable &OutLineTable)
Emit .debug_line section.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Entry
Definition: COFF.h:844
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
unsigned getULEB128Size(uint64_t Value)
Utility function to get the size of the ULEB128-encoded value.
Definition: LEB128.cpp:19
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1903
endianness
Definition: bit.h:70
Error parallelForEachError(RangeTy &&R, FuncTy Fn)
Definition: Parallel.h:274
uint8_t MaxOpsPerInst
The maximum number of individual operations that may be encoded in an instruction.
uint8_t MinInstLength
The size in bytes of the smallest target machine instruction.
int8_t LineBase
This parameter affects the meaning of the special opcodes. See below.
uint8_t LineRange
This parameter affects the meaning of the special opcodes. See below.
std::vector< DWARFFormValue > IncludeDirectories
uint8_t OpcodeBase
The number assigned to the first special opcode.
std::vector< uint8_t > StandardOpcodeLengths
uint8_t DefaultIsStmt
The initial value of theis_stmtregister.
dwarf::FormParams FormParams
Version, address size (starting in v5), and DWARF32/64 format; these parameters affect interpretation...
std::vector< FileNameEntry > FileNames
A helper struct providing information about the byte size of DW_FORM values that vary in size dependi...
Definition: Dwarf.h:1084
SmallVector< DWARFLinkerBase::AccelTableKind, 1 > AccelTables
The accelerator table kinds.
bool AllowNonDeterministicOutput
Allow to generate valid, but non deterministic output.
This structure is used to update strings offsets into .debug_line_str.
This structure is used to update strings offsets into .debug_str.
This structure is used to keep data of the concrete section.
void notePatchWithOffsetUpdate(const T &Patch, OffsetsPtrVector &PatchesOffsetsList)
While creating patches, offsets to attributes may be partially unknown(because size of abbreviation n...