LLVM 23.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. Sort data here to produce deterministic output.
136
138
139 TG.spawn([&]() {
140 // Sort types to have a deterministic output.
141 Types.sortTypes();
142 });
143
144 TG.spawn([&]() {
145 // Sort decl type patches to have a deterministic output.
146 std::function<bool(const DebugTypeDeclFilePatch &LHS,
147 const DebugTypeDeclFilePatch &RHS)>
148 PatchesComparator = [&](const DebugTypeDeclFilePatch &LHS,
149 const DebugTypeDeclFilePatch &RHS) {
150 return LHS.Directory->first() < RHS.Directory->first() ||
151 (!(RHS.Directory->first() < LHS.Directory->first()) &&
152 LHS.FilePath->first() < RHS.FilePath->first());
153 };
154 DebugInfoSection.ListDebugTypeDeclFilePatch.sort(PatchesComparator);
155
156 // Update DW_AT_decl_file attribute
157 dwarf::Form DeclFileForm =
158 getScalarFormForValue(
159 DebugInfoSection.ListDebugTypeDeclFilePatch.size())
160 .first;
161
162 DebugInfoSection.ListDebugTypeDeclFilePatch.forEach(
163 [&](DebugTypeDeclFilePatch &Patch) {
164 TypeEntryBody *TypeEntry = Patch.TypeName->getValue().load();
166 formatv("No data for type {0}", Patch.TypeName->getKey())
167 .str()
168 .c_str());
169 if (&TypeEntry->getFinalDie() != Patch.Die)
170 return;
171
172 uint32_t FileIdx =
173 addFileNameIntoLinetable(Patch.Directory, Patch.FilePath);
174
175 unsigned DIESize = Patch.Die->getSize();
176 DIEGenerator DIEGen(Patch.Die, Types.getThreadLocalAllocator(),
177 *this);
178
179 DIESize += DIEGen
180 .addScalarAttribute(dwarf::DW_AT_decl_file,
181 DeclFileForm, FileIdx)
182 .second;
183 Patch.Die->setSize(DIESize);
184 });
185 });
186
187 // Sort patches to have a deterministic output.
188 TG.spawn([&]() {
189 forEach([&](SectionDescriptor &OutSection) {
190 std::function<bool(const DebugStrPatch &LHS, const DebugStrPatch &RHS)>
191 StrPatchesComparator =
192 [&](const DebugStrPatch &LHS, const DebugStrPatch &RHS) {
193 return LHS.String->getKey() < RHS.String->getKey();
194 };
195 OutSection.ListDebugStrPatch.sort(StrPatchesComparator);
196
197 std::function<bool(const DebugTypeStrPatch &LHS,
198 const DebugTypeStrPatch &RHS)>
199 TypeStrPatchesComparator =
200 [&](const DebugTypeStrPatch &LHS, const DebugTypeStrPatch &RHS) {
201 return LHS.String->getKey() < RHS.String->getKey();
202 };
203 OutSection.ListDebugTypeStrPatch.sort(TypeStrPatchesComparator);
204 });
205 });
206
207 // Sort patches to have a deterministic output.
208 TG.spawn([&]() {
209 forEach([&](SectionDescriptor &OutSection) {
210 std::function<bool(const DebugLineStrPatch &LHS,
211 const DebugLineStrPatch &RHS)>
212 LineStrPatchesComparator =
213 [&](const DebugLineStrPatch &LHS, const DebugLineStrPatch &RHS) {
214 return LHS.String->getKey() < RHS.String->getKey();
215 };
216 OutSection.ListDebugLineStrPatch.sort(LineStrPatchesComparator);
217
218 std::function<bool(const DebugTypeLineStrPatch &LHS,
219 const DebugTypeLineStrPatch &RHS)>
220 TypeLineStrPatchesComparator = [&](const DebugTypeLineStrPatch &LHS,
221 const DebugTypeLineStrPatch &RHS) {
222 return LHS.String->getKey() < RHS.String->getKey();
223 };
224 OutSection.ListDebugTypeLineStrPatch.sort(TypeLineStrPatchesComparator);
225 });
226 });
227}
228
229uint64_t TypeUnit::finalizeTypeEntryRec(uint64_t OutOffset, DIE *OutDIE,
230 TypeEntry *Entry) {
231 bool HasChildren = !Entry->getValue().load()->Children.empty();
232 DIEGenerator DIEGen(OutDIE, Types.getThreadLocalAllocator(), *this);
233 OutOffset += DIEGen.finalizeAbbreviations(HasChildren, nullptr);
234 OutOffset += OutDIE->getSize() - 1;
235
236 if (HasChildren) {
237 Entry->getValue().load()->Children.forEach([&](TypeEntry *ChildEntry) {
238 DIE *ChildDIE = &ChildEntry->getValue().load()->getFinalDie();
239 DIEGen.addChild(ChildDIE);
240
241 ChildDIE->setOffset(OutOffset);
242
243 OutOffset = finalizeTypeEntryRec(OutOffset, ChildDIE, ChildEntry);
244 });
245
246 // End of children marker.
247 OutOffset += sizeof(int8_t);
248 }
249
250 OutDIE->setSize(OutOffset - OutDIE->getOffset());
251 return OutOffset;
252}
253
254uint32_t TypeUnit::addFileNameIntoLinetable(StringEntry *Dir,
255 StringEntry *FileName) {
256 uint32_t DirIdx = 0;
257
258 if (Dir->first() == "") {
259 DirIdx = 0;
260 } else {
261 DirectoriesMapTy::iterator DirEntry = DirectoriesMap.find(Dir);
262 if (DirEntry == DirectoriesMap.end()) {
263 // We currently do not support more than UINT32_MAX directories.
264 assert(LineTable.Prologue.IncludeDirectories.size() < UINT32_MAX);
265 DirIdx = LineTable.Prologue.IncludeDirectories.size();
266 DirectoriesMap.insert({Dir, DirIdx});
267 LineTable.Prologue.IncludeDirectories.push_back(
268 DWARFFormValue::createFromPValue(dwarf::DW_FORM_string,
269 Dir->getKeyData()));
270 } else {
271 DirIdx = DirEntry->second;
272 }
273
274 if (getVersion() < 5)
275 DirIdx++;
276 }
277
278 auto [FileEntry, Inserted] = FileNamesMap.try_emplace(
279 {FileName, DirIdx}, LineTable.Prologue.FileNames.size());
280 if (Inserted) {
281 // We currently do not support more than UINT32_MAX files.
282 assert(LineTable.Prologue.FileNames.size() < UINT32_MAX);
283 LineTable.Prologue.FileNames.push_back(DWARFDebugLine::FileNameEntry());
284 LineTable.Prologue.FileNames.back().Name = DWARFFormValue::createFromPValue(
285 dwarf::DW_FORM_string, FileName->getKeyData());
286 LineTable.Prologue.FileNames.back().DirIdx = DirIdx;
287 }
288
289 uint32_t FileIdx = FileEntry->second;
290 return getVersion() < 5 ? FileIdx + 1 : FileIdx;
291}
292
293std::pair<dwarf::Form, uint8_t>
294TypeUnit::getScalarFormForValue(uint64_t Value) const {
295 if (Value > 0xFFFFFFFF)
296 return std::make_pair(dwarf::DW_FORM_data8, 8);
297
298 if (Value > 0xFFFF)
299 return std::make_pair(dwarf::DW_FORM_data4, 4);
300
301 if (Value > 0xFF)
302 return std::make_pair(dwarf::DW_FORM_data2, 2);
303
304 return std::make_pair(dwarf::DW_FORM_data1, 1);
305}
306
307uint8_t TypeUnit::getSizeByAttrForm(dwarf::Form Form) const {
308 if (Form == dwarf::DW_FORM_data1)
309 return 1;
310
311 if (Form == dwarf::DW_FORM_data2)
312 return 2;
313
314 if (Form == dwarf::DW_FORM_data4)
315 return 4;
316
317 if (Form == dwarf::DW_FORM_data8)
318 return 8;
319
320 if (Form == dwarf::DW_FORM_data16)
321 return 16;
322
323 llvm_unreachable("Unsupported Attr Form");
324}
325
327 BumpPtrAllocator Allocator;
328 createDIETree(Allocator);
329
330 if (getOutUnitDIE() == nullptr)
331 return Error::success();
332
333 // Create sections ahead so that they should not be created asynchronously
334 // later.
339 if (llvm::is_contained(GlobalData.getOptions().AccelTables,
343 }
344
345 SmallVector<std::function<Error(void)>> Tasks;
346
347 // Add task for emitting .debug_line section.
348 if (!LineTable.Prologue.FileNames.empty()) {
349 Tasks.push_back(
350 [&]() -> Error { return emitDebugLine(TargetTriple, LineTable); });
351 }
352
353 // Add task for emitting .debug_info section.
354 Tasks.push_back([&]() -> Error { return emitDebugInfo(TargetTriple); });
355
356 // Add task for emitting Pub accelerator sections.
357 if (llvm::is_contained(GlobalData.getOptions().AccelTables,
359 Tasks.push_back([&]() -> Error {
361 return Error::success();
362 });
363 }
364
365 // Add task for emitting .debug_str_offsets section.
366 Tasks.push_back([&]() -> Error { return emitDebugStringOffsetSection(); });
367
368 // Add task for emitting .debug_abbr section.
369 Tasks.push_back([&]() -> Error { return emitAbbreviations(); });
370
371 if (auto Err = parallelForEachError(
372 Tasks, [&](std::function<Error(void)> F) { return F(); }))
373 return Err;
374
375 return Error::success();
376}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define F(x, y, z)
Definition MD5.cpp:54
Value * RHS
Value * LHS
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.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
const ValueTy & getValue() const
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
This class is a helper to create output DIE tree.
std::pair< DIEValue &, size_t > addStringPlaceholderAttribute(dwarf::Attribute Attr, dwarf::Form AttrForm)
Adds string attribute with dummy offset to the current DIE.
DIE * createDIE(dwarf::Tag DieTag, uint32_t OutOffset)
Creates a DIE of specified tag DieTag and OutOffset.
std::pair< DIEValue &, size_t > addScalarAttribute(dwarf::Attribute Attr, dwarf::Form AttrForm, uint64_t Value)
Adds specified scalar attribute to the current DIE.
std::string UnitName
The name of this unit.
IndexedValuesMap< const StringEntry * > DebugStringIndexMap
Maps a string into the index inside .debug_str_offsets section.
unsigned ID
Unique ID for the unit.
StringRef getUnitName() const
Returns this unit name.
void setOutUnitDIE(DIE *UnitDie)
Set output unit DIE.
DwarfUnit(LinkingGlobalData &GlobalData, unsigned ID, StringRef ClangModuleName)
DIE * getOutUnitDIE()
Returns output unit DIE.
This class keeps data and services common for the whole linking process.
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.
void forEach(function_ref< void(SectionDescriptor &)> Handler)
Enumerate all sections and call Handler for each.
SectionDescriptor & getOrCreateSectionDescriptor(DebugSectionKind SectionKind)
Returns descriptor for the specified section of SectionKind.
Keeps cloned data for the type DIE.
Definition TypePool.h:31
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:257
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
SmallVector< uint64_t * > OffsetsPtrVector
Type for list of pointers to patches offsets.
StringMapEntry< std::atomic< TypeEntryBody * > > TypeEntry
Definition TypePool.h:28
StringMapEntry< EmptyStringSetTag > StringEntry
StringEntry keeps data of the string: the length, external offset and a string body which is placed r...
Definition StringPool.h:23
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
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:1946
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
endianness
Definition bit.h:71
Error parallelForEachError(RangeTy &&R, FuncTy Fn)
Definition Parallel.h:272
A helper struct providing information about the byte size of DW_FORM values that vary in size dependi...
Definition Dwarf.h:1114
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...