LLVM 22.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 "llvm/Support/LEB128.h"
12
13using namespace llvm;
14using namespace dwarf_linker;
15using namespace dwarf_linker::parallel;
16
18 std::optional<uint16_t> Language, dwarf::FormParams Format,
19 endianness Endianess)
20 : DwarfUnit(GlobalData, ID, ""), Language(Language),
21 AcceleratorRecords(&GlobalData.getAllocator()) {
22
23 UnitName = "__artificial_type_unit";
24
25 setOutputFormat(Format, Endianess);
26
27 // Create line table prologue.
28 LineTable.Prologue.FormParams = getFormParams();
29 LineTable.Prologue.MinInstLength = 1;
30 LineTable.Prologue.MaxOpsPerInst = 1;
31 LineTable.Prologue.DefaultIsStmt = 1;
32 LineTable.Prologue.LineBase = -5;
33 LineTable.Prologue.LineRange = 14;
34 LineTable.Prologue.OpcodeBase = 13;
35 LineTable.Prologue.StandardOpcodeLengths = {0, 1, 1, 1, 1, 0,
36 0, 0, 1, 0, 0, 1};
37
39}
40
42 prepareDataForTreeCreation();
43
44 // TaskGroup is created here as internal code has calls to
45 // PerThreadBumpPtrAllocator which should be called from the task group task.
47 TG.spawn([&]() {
48 SectionDescriptor &DebugInfoSection =
50 SectionDescriptor &DebugLineSection =
52
53 DIEGenerator DIETreeGenerator(Allocator, *this);
54 OffsetsPtrVector PatchesOffsets;
55
56 // Create a Die for artificial compilation unit for types.
57 DIE *UnitDIE = DIETreeGenerator.createDIE(dwarf::DW_TAG_compile_unit, 0);
58 uint64_t OutOffset = getDebugInfoHeaderSize();
59 UnitDIE->setOffset(OutOffset);
60
61 SmallString<200> ProducerString;
62 ProducerString += "llvm DWARFLinkerParallel library version ";
63 DebugInfoSection.notePatchWithOffsetUpdate(
65 {OutOffset},
66 GlobalData.getStringPool().insert(ProducerString.str()).first},
67 PatchesOffsets);
68 OutOffset += DIETreeGenerator
69 .addStringPlaceholderAttribute(dwarf::DW_AT_producer,
70 dwarf::DW_FORM_strp)
71 .second;
72
73 if (Language) {
74 OutOffset += DIETreeGenerator
75 .addScalarAttribute(dwarf::DW_AT_language,
76 dwarf::DW_FORM_data2, *Language)
77 .second;
78 }
79
80 DebugInfoSection.notePatchWithOffsetUpdate(
81 DebugStrPatch{{OutOffset},
82 GlobalData.getStringPool().insert(getUnitName()).first},
83 PatchesOffsets);
84 OutOffset += DIETreeGenerator
85 .addStringPlaceholderAttribute(dwarf::DW_AT_name,
86 dwarf::DW_FORM_strp)
87 .second;
88
89 if (!LineTable.Prologue.FileNames.empty()) {
90 DebugInfoSection.notePatchWithOffsetUpdate(
91 DebugOffsetPatch{OutOffset, &DebugLineSection}, PatchesOffsets);
92
93 OutOffset += DIETreeGenerator
94 .addScalarAttribute(dwarf::DW_AT_stmt_list,
95 dwarf::DW_FORM_sec_offset, 0xbaddef)
96 .second;
97 }
98
99 DebugInfoSection.notePatchWithOffsetUpdate(
100 DebugStrPatch{{OutOffset}, GlobalData.getStringPool().insert("").first},
101 PatchesOffsets);
102 OutOffset += DIETreeGenerator
103 .addStringPlaceholderAttribute(dwarf::DW_AT_comp_dir,
104 dwarf::DW_FORM_strp)
105 .second;
106
107 if (!DebugStringIndexMap.empty()) {
108 // Type unit is assumed to be emitted first. Thus we can use direct value
109 // for DW_AT_str_offsets_base attribute(No need to fix it up with unit
110 // offset value).
111 OutOffset += DIETreeGenerator
112 .addScalarAttribute(dwarf::DW_AT_str_offsets_base,
113 dwarf::DW_FORM_sec_offset,
115 .second;
116 }
117
118 UnitDIE->setSize(OutOffset - UnitDIE->getOffset() + 1);
119 OutOffset =
120 finalizeTypeEntryRec(UnitDIE->getOffset(), UnitDIE, Types.getRoot());
121
122 // Update patch offsets.
123 for (uint64_t *OffsetPtr : PatchesOffsets)
124 *OffsetPtr += getULEB128Size(UnitDIE->getAbbrevNumber());
125
126 setOutUnitDIE(UnitDIE);
127 });
128}
129
130void TypeUnit::prepareDataForTreeCreation() {
131 SectionDescriptor &DebugInfoSection =
133
134 // Type unit data created parallelly. So the order of data is not
135 // deterministic. Order data here if we need deterministic output.
136
138
140 TG.spawn([&]() {
141 // Sort types to have a deterministic output.
142 Types.sortTypes();
143 });
144 }
145
146 TG.spawn([&]() {
148 // Sort decl type patches to have a deterministic output.
149 std::function<bool(const DebugTypeDeclFilePatch &LHS,
150 const DebugTypeDeclFilePatch &RHS)>
151 PatchesComparator = [&](const DebugTypeDeclFilePatch &LHS,
152 const DebugTypeDeclFilePatch &RHS) {
153 return LHS.Directory->first() < RHS.Directory->first() ||
154 (!(RHS.Directory->first() < LHS.Directory->first()) &&
155 LHS.FilePath->first() < RHS.FilePath->first());
156 };
157 // Sort patches to have a deterministic output.
158 DebugInfoSection.ListDebugTypeDeclFilePatch.sort(PatchesComparator);
159 }
160
161 // Update DW_AT_decl_file attribute
162 dwarf::Form DeclFileForm =
163 getScalarFormForValue(
164 DebugInfoSection.ListDebugTypeDeclFilePatch.size())
165 .first;
166
167 DebugInfoSection.ListDebugTypeDeclFilePatch.forEach(
168 [&](DebugTypeDeclFilePatch &Patch) {
171 formatv("No data for type {0}", Patch.TypeName->getKey())
172 .str()
173 .c_str());
174 if (&TypeEntry->getFinalDie() != Patch.Die)
175 return;
176
177 uint32_t FileIdx =
178 addFileNameIntoLinetable(Patch.Directory, Patch.FilePath);
179
180 unsigned DIESize = Patch.Die->getSize();
181 DIEGenerator DIEGen(Patch.Die, Types.getThreadLocalAllocator(),
182 *this);
183
184 DIESize += DIEGen
185 .addScalarAttribute(dwarf::DW_AT_decl_file,
186 DeclFileForm, FileIdx)
187 .second;
188 Patch.Die->setSize(DIESize);
189 });
190 });
191
192 if (!GlobalData.getOptions().AllowNonDeterministicOutput) {
193 // Sort patches to have a deterministic output.
194 TG.spawn([&]() {
195 forEach([&](SectionDescriptor &OutSection) {
196 std::function<bool(const DebugStrPatch &LHS, const DebugStrPatch &RHS)>
197 StrPatchesComparator =
198 [&](const DebugStrPatch &LHS, const DebugStrPatch &RHS) {
199 return LHS.String->getKey() < RHS.String->getKey();
200 };
201 OutSection.ListDebugStrPatch.sort(StrPatchesComparator);
202
203 std::function<bool(const DebugTypeStrPatch &LHS,
204 const DebugTypeStrPatch &RHS)>
205 TypeStrPatchesComparator = [&](const DebugTypeStrPatch &LHS,
206 const DebugTypeStrPatch &RHS) {
207 return LHS.String->getKey() < RHS.String->getKey();
208 };
209 OutSection.ListDebugTypeStrPatch.sort(TypeStrPatchesComparator);
210 });
211 });
212 }
213
214 if (!GlobalData.getOptions().AllowNonDeterministicOutput) {
215 // Sort patches to have a deterministic output.
216 TG.spawn([&]() {
217 forEach([&](SectionDescriptor &OutSection) {
218 std::function<bool(const DebugLineStrPatch &LHS,
219 const DebugLineStrPatch &RHS)>
220 LineStrPatchesComparator = [&](const DebugLineStrPatch &LHS,
221 const DebugLineStrPatch &RHS) {
222 return LHS.String->getKey() < RHS.String->getKey();
223 };
224 OutSection.ListDebugLineStrPatch.sort(LineStrPatchesComparator);
225
226 std::function<bool(const DebugTypeLineStrPatch &LHS,
227 const DebugTypeLineStrPatch &RHS)>
228 TypeLineStrPatchesComparator =
229 [&](const DebugTypeLineStrPatch &LHS,
230 const DebugTypeLineStrPatch &RHS) {
231 return LHS.String->getKey() < RHS.String->getKey();
232 };
233 OutSection.ListDebugTypeLineStrPatch.sort(TypeLineStrPatchesComparator);
234 });
235 });
236 }
237}
238
239uint64_t TypeUnit::finalizeTypeEntryRec(uint64_t OutOffset, DIE *OutDIE,
240 TypeEntry *Entry) {
241 bool HasChildren = !Entry->getValue().load()->Children.empty();
242 DIEGenerator DIEGen(OutDIE, Types.getThreadLocalAllocator(), *this);
243 OutOffset += DIEGen.finalizeAbbreviations(HasChildren, nullptr);
244 OutOffset += OutDIE->getSize() - 1;
245
246 if (HasChildren) {
247 Entry->getValue().load()->Children.forEach([&](TypeEntry *ChildEntry) {
248 DIE *ChildDIE = &ChildEntry->getValue().load()->getFinalDie();
249 DIEGen.addChild(ChildDIE);
250
251 ChildDIE->setOffset(OutOffset);
252
253 OutOffset = finalizeTypeEntryRec(OutOffset, ChildDIE, ChildEntry);
254 });
255
256 // End of children marker.
257 OutOffset += sizeof(int8_t);
258 }
259
260 OutDIE->setSize(OutOffset - OutDIE->getOffset());
261 return OutOffset;
262}
263
264uint32_t TypeUnit::addFileNameIntoLinetable(StringEntry *Dir,
265 StringEntry *FileName) {
266 uint32_t DirIdx = 0;
267
268 if (Dir->first() == "") {
269 DirIdx = 0;
270 } else {
271 DirectoriesMapTy::iterator DirEntry = DirectoriesMap.find(Dir);
272 if (DirEntry == DirectoriesMap.end()) {
273 // We currently do not support more than UINT32_MAX directories.
274 assert(LineTable.Prologue.IncludeDirectories.size() < UINT32_MAX);
275 DirIdx = LineTable.Prologue.IncludeDirectories.size();
276 DirectoriesMap.insert({Dir, DirIdx});
277 LineTable.Prologue.IncludeDirectories.push_back(
278 DWARFFormValue::createFromPValue(dwarf::DW_FORM_string,
279 Dir->getKeyData()));
280 } else {
281 DirIdx = DirEntry->second;
282 }
283
284 if (getVersion() < 5)
285 DirIdx++;
286 }
287
288 auto [FileEntry, Inserted] = FileNamesMap.try_emplace(
289 {FileName, DirIdx}, LineTable.Prologue.FileNames.size());
290 if (Inserted) {
291 // We currently do not support more than UINT32_MAX files.
292 assert(LineTable.Prologue.FileNames.size() < UINT32_MAX);
293 LineTable.Prologue.FileNames.push_back(DWARFDebugLine::FileNameEntry());
295 dwarf::DW_FORM_string, FileName->getKeyData());
296 LineTable.Prologue.FileNames.back().DirIdx = DirIdx;
297 }
298
299 uint32_t FileIdx = FileEntry->second;
300 return getVersion() < 5 ? FileIdx + 1 : FileIdx;
301}
302
303std::pair<dwarf::Form, uint8_t>
304TypeUnit::getScalarFormForValue(uint64_t Value) const {
305 if (Value > 0xFFFFFFFF)
306 return std::make_pair(dwarf::DW_FORM_data8, 8);
307
308 if (Value > 0xFFFF)
309 return std::make_pair(dwarf::DW_FORM_data4, 4);
310
311 if (Value > 0xFF)
312 return std::make_pair(dwarf::DW_FORM_data2, 2);
313
314 return std::make_pair(dwarf::DW_FORM_data1, 1);
315}
316
317uint8_t TypeUnit::getSizeByAttrForm(dwarf::Form Form) const {
318 if (Form == dwarf::DW_FORM_data1)
319 return 1;
320
321 if (Form == dwarf::DW_FORM_data2)
322 return 2;
323
324 if (Form == dwarf::DW_FORM_data4)
325 return 4;
326
327 if (Form == dwarf::DW_FORM_data8)
328 return 8;
329
330 if (Form == dwarf::DW_FORM_data16)
331 return 16;
332
333 llvm_unreachable("Unsupported Attr Form");
334}
335
339
340 if (getOutUnitDIE() == nullptr)
341 return Error::success();
342
343 // Create sections ahead so that they should not be created asynchronously
344 // later.
353 }
354
355 SmallVector<std::function<Error(void)>> Tasks;
356
357 // Add task for emitting .debug_line section.
358 if (!LineTable.Prologue.FileNames.empty()) {
359 Tasks.push_back(
360 [&]() -> Error { return emitDebugLine(TargetTriple, LineTable); });
361 }
362
363 // Add task for emitting .debug_info section.
364 Tasks.push_back([&]() -> Error { return emitDebugInfo(TargetTriple); });
365
366 // Add task for emitting Pub accelerator sections.
369 Tasks.push_back([&]() -> Error {
371 return Error::success();
372 });
373 }
374
375 // Add task for emitting .debug_str_offsets section.
376 Tasks.push_back([&]() -> Error { return emitDebugStringOffsetSection(); });
377
378 // Add task for emitting .debug_abbr section.
379 Tasks.push_back([&]() -> Error { return emitAbbreviations(); });
380
381 if (auto Err = parallelForEachError(
382 Tasks, [&](std::function<Error(void)> F) { return F(); }))
383 return Err;
384
385 return Error::success();
386}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Mark last scratch load
#define F(x, y, z)
Definition: MD5.cpp:55
Basic Register Allocator
Value * RHS
Value * LHS
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:67
A structured debug information entry.
Definition: DIE.h:828
unsigned getAbbrevNumber() const
Definition: DIE.h:863
unsigned getSize() const
Definition: DIE.h:871
void setSize(unsigned S)
Definition: DIE.h:941
unsigned getOffset() const
Get the compile/type unit relative offset of this DIE.
Definition: DIE.h:866
void setOffset(unsigned O)
Definition: DIE.h:940
static LLVM_ABI DWARFFormValue createFromPValue(dwarf::Form F, const char *V)
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
static ErrorSuccess success()
Create a success value.
Definition: Error.h:336
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:47
LLVM Value Representation.
Definition: Value.h:75
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)
LLVM_ABI 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:862
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
LLVM_ABI 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:1916
endianness
Definition: bit.h:71
Error parallelForEachError(RangeTy &&R, FuncTy Fn)
Definition: Parallel.h:276
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:1093
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...