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
9#include "DWARFLinkerTypeUnit.h"
10#include "DIEGenerator.h"
11#include "llvm/Support/LEB128.h"
12
13using namespace llvm;
14using namespace dwarf_linker;
15using namespace dwarf_linker::parallel;
16
17TypeUnit::TypeUnit(LinkingGlobalData &GlobalData, unsigned ID,
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, Endianness: 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
38 getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugInfo);
39}
40
41void TypeUnit::createDIETree(BumpPtrAllocator &Allocator) {
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.
46 llvm::parallel::TaskGroup TG;
47 TG.spawn(f: [&]() {
48 SectionDescriptor &DebugInfoSection =
49 getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugInfo);
50 SectionDescriptor &DebugLineSection =
51 getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugLine);
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(DieTag: dwarf::DW_TAG_compile_unit, OutOffset: 0);
58 uint64_t OutOffset = getDebugInfoHeaderSize();
59 UnitDIE->setOffset(OutOffset);
60
61 SmallString<200> ProducerString;
62 ProducerString += "llvm DWARFLinkerParallel library version ";
63 DebugInfoSection.notePatchWithOffsetUpdate(
64 Patch: DebugStrPatch{
65 {.PatchOffset: OutOffset},
66 .String: GlobalData.getStringPool().insert(NewValue: ProducerString.str()).first},
67 PatchesOffsetsList&: PatchesOffsets);
68 OutOffset += DIETreeGenerator
69 .addStringPlaceholderAttribute(Attr: dwarf::DW_AT_producer,
70 AttrForm: dwarf::DW_FORM_strp)
71 .second;
72
73 if (Language) {
74 OutOffset += DIETreeGenerator
75 .addScalarAttribute(Attr: dwarf::DW_AT_language,
76 AttrForm: dwarf::DW_FORM_data2, Value: *Language)
77 .second;
78 }
79
80 DebugInfoSection.notePatchWithOffsetUpdate(
81 Patch: DebugStrPatch{{.PatchOffset: OutOffset},
82 .String: GlobalData.getStringPool().insert(NewValue: getUnitName()).first},
83 PatchesOffsetsList&: PatchesOffsets);
84 OutOffset += DIETreeGenerator
85 .addStringPlaceholderAttribute(Attr: dwarf::DW_AT_name,
86 AttrForm: dwarf::DW_FORM_strp)
87 .second;
88
89 if (!LineTable.Prologue.FileNames.empty()) {
90 DebugInfoSection.notePatchWithOffsetUpdate(
91 Patch: DebugOffsetPatch{OutOffset, &DebugLineSection}, PatchesOffsetsList&: PatchesOffsets);
92
93 OutOffset += DIETreeGenerator
94 .addScalarAttribute(Attr: dwarf::DW_AT_stmt_list,
95 AttrForm: dwarf::DW_FORM_sec_offset, Value: 0xbaddef)
96 .second;
97 }
98
99 DebugInfoSection.notePatchWithOffsetUpdate(
100 Patch: DebugStrPatch{{.PatchOffset: OutOffset}, .String: GlobalData.getStringPool().insert(NewValue: "").first},
101 PatchesOffsetsList&: PatchesOffsets);
102 OutOffset += DIETreeGenerator
103 .addStringPlaceholderAttribute(Attr: dwarf::DW_AT_comp_dir,
104 AttrForm: 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(Attr: dwarf::DW_AT_str_offsets_base,
113 AttrForm: dwarf::DW_FORM_sec_offset,
114 Value: getDebugStrOffsetsHeaderSize())
115 .second;
116 }
117
118 UnitDIE->setSize(OutOffset - UnitDIE->getOffset() + 1);
119 OutOffset =
120 finalizeTypeEntryRec(OutOffset: UnitDIE->getOffset(), OutDIE: UnitDIE, Entry: Types.getRoot());
121
122 // Update patch offsets.
123 for (uint64_t *OffsetPtr : PatchesOffsets)
124 *OffsetPtr += getULEB128Size(Value: UnitDIE->getAbbrevNumber());
125
126 setOutUnitDIE(UnitDIE);
127 });
128}
129
130void TypeUnit::prepareDataForTreeCreation() {
131 SectionDescriptor &DebugInfoSection =
132 getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugInfo);
133
134 // Type unit data created parallelly. So the order of data is not
135 // deterministic. Sort data here to produce deterministic output.
136
137 llvm::parallel::TaskGroup TG;
138
139 TG.spawn(f: [&]() {
140 // Sort types to have a deterministic output.
141 Types.sortTypes();
142 });
143
144 TG.spawn(f: [&]() {
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(Comparator: PatchesComparator);
155
156 // Update DW_AT_decl_file attribute
157 dwarf::Form DeclFileForm =
158 getScalarFormForValue(
159 Value: DebugInfoSection.ListDebugTypeDeclFilePatch.size())
160 .first;
161
162 DebugInfoSection.ListDebugTypeDeclFilePatch.forEach(
163 Handler: [&](DebugTypeDeclFilePatch &Patch) {
164 TypeEntryBody *TypeEntry = Patch.TypeName->getValue().load();
165 assert(TypeEntry &&
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(Dir: Patch.Directory, FileName: Patch.FilePath);
174
175 unsigned DIESize = Patch.Die->getSize();
176 DIEGenerator DIEGen(Patch.Die, Types.getThreadLocalAllocator(),
177 *this);
178
179 DIESize += DIEGen
180 .addScalarAttribute(Attr: dwarf::DW_AT_decl_file,
181 AttrForm: DeclFileForm, Value: FileIdx)
182 .second;
183 Patch.Die->setSize(DIESize);
184 });
185 });
186
187 // Sort patches to have a deterministic output.
188 TG.spawn(f: [&]() {
189 forEach(Handler: [&](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(Comparator: 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(Comparator: TypeStrPatchesComparator);
204 });
205 });
206
207 // Sort patches to have a deterministic output.
208 TG.spawn(f: [&]() {
209 forEach(Handler: [&](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(Comparator: 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(Comparator: 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(CHILDREN_yes: HasChildren, OffsetsList: nullptr);
234 OutOffset += OutDIE->getSize() - 1;
235
236 if (HasChildren) {
237 Entry->getValue().load()->Children.forEach(Handler: [&](TypeEntry *ChildEntry) {
238 DIE *ChildDIE = &ChildEntry->getValue().load()->getFinalDie();
239 DIEGen.addChild(Child: ChildDIE);
240
241 ChildDIE->setOffset(OutOffset);
242
243 OutOffset = finalizeTypeEntryRec(OutOffset, OutDIE: ChildDIE, Entry: 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(x: 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(x: {Dir, DirIdx});
267 LineTable.Prologue.IncludeDirectories.push_back(
268 x: DWARFFormValue::createFromPValue(F: dwarf::DW_FORM_string,
269 V: 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 k: {FileName, DirIdx}, args: 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(x: DWARFDebugLine::FileNameEntry());
284 LineTable.Prologue.FileNames.back().Name = DWARFFormValue::createFromPValue(
285 F: dwarf::DW_FORM_string, V: 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(x: dwarf::DW_FORM_data8, y: 8);
297
298 if (Value > 0xFFFF)
299 return std::make_pair(x: dwarf::DW_FORM_data4, y: 4);
300
301 if (Value > 0xFF)
302 return std::make_pair(x: dwarf::DW_FORM_data2, y: 2);
303
304 return std::make_pair(x: dwarf::DW_FORM_data1, y: 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
326Error TypeUnit::finishCloningAndEmit(const Triple &TargetTriple) {
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.
335 getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugInfo);
336 getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugLine);
337 getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugStrOffsets);
338 getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugAbbrev);
339 if (llvm::is_contained(Range: GlobalData.getOptions().AccelTables,
340 Element: DWARFLinker::AccelTableKind::Pub)) {
341 getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugPubNames);
342 getOrCreateSectionDescriptor(SectionKind: DebugSectionKind::DebugPubTypes);
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 Elt: [&]() -> Error { return emitDebugLine(TargetTriple, OutLineTable: LineTable); });
351 }
352
353 // Add task for emitting .debug_info section.
354 Tasks.push_back(Elt: [&]() -> Error { return emitDebugInfo(TargetTriple); });
355
356 // Add task for emitting Pub accelerator sections.
357 if (llvm::is_contained(Range: GlobalData.getOptions().AccelTables,
358 Element: DWARFLinker::AccelTableKind::Pub)) {
359 Tasks.push_back(Elt: [&]() -> Error {
360 emitPubAccelerators();
361 return Error::success();
362 });
363 }
364
365 // Add task for emitting .debug_str_offsets section.
366 Tasks.push_back(Elt: [&]() -> Error { return emitDebugStringOffsetSection(); });
367
368 // Add task for emitting .debug_abbr section.
369 Tasks.push_back(Elt: [&]() -> Error { return emitAbbreviations(); });
370
371 if (auto Err = parallelForEachError(
372 R&: Tasks, Fn: [&](std::function<Error(void)> F) { return F(); }))
373 return Err;
374
375 return Error::success();
376}
377