1//=== AcceleratorRecordsSaver.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 "AcceleratorRecordsSaver.h"
10#include "llvm/DWARFLinker/Utils.h"
11#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
12#include "llvm/Support/DJB.h"
13
14using namespace llvm;
15using namespace dwarf_linker;
16using namespace dwarf_linker::parallel;
17
18void AcceleratorRecordsSaver::save(const DWARFDebugInfoEntry *InputDieEntry,
19 DIE *OutDIE, AttributesInfo &AttrInfo,
20 TypeEntry *TypeEntry) {
21 if (GlobalData.getOptions().AccelTables.empty())
22 return;
23
24 DWARFDie InputDIE = InUnit.getDIE(Die: InputDieEntry);
25
26 // Look for short name recursively if short name is not known yet.
27 if (AttrInfo.Name == nullptr)
28 if (const char *ShortName = InputDIE.getShortName())
29 AttrInfo.Name = GlobalData.getStringPool().insert(NewValue: ShortName).first;
30
31 switch (InputDieEntry->getTag()) {
32 case dwarf::DW_TAG_array_type:
33 case dwarf::DW_TAG_class_type:
34 case dwarf::DW_TAG_enumeration_type:
35 case dwarf::DW_TAG_pointer_type:
36 case dwarf::DW_TAG_reference_type:
37 case dwarf::DW_TAG_string_type:
38 case dwarf::DW_TAG_structure_type:
39 case dwarf::DW_TAG_subroutine_type:
40 case dwarf::DW_TAG_template_alias:
41 case dwarf::DW_TAG_typedef:
42 case dwarf::DW_TAG_union_type:
43 case dwarf::DW_TAG_ptr_to_member_type:
44 case dwarf::DW_TAG_set_type:
45 case dwarf::DW_TAG_subrange_type:
46 case dwarf::DW_TAG_base_type:
47 case dwarf::DW_TAG_const_type:
48 case dwarf::DW_TAG_constant:
49 case dwarf::DW_TAG_file_type:
50 case dwarf::DW_TAG_namelist:
51 case dwarf::DW_TAG_packed_type:
52 case dwarf::DW_TAG_volatile_type:
53 case dwarf::DW_TAG_restrict_type:
54 case dwarf::DW_TAG_atomic_type:
55 case dwarf::DW_TAG_interface_type:
56 case dwarf::DW_TAG_unspecified_type:
57 case dwarf::DW_TAG_shared_type:
58 case dwarf::DW_TAG_immutable_type:
59 case dwarf::DW_TAG_rvalue_reference_type: {
60 if (AttrInfo.IsDeclaration)
61 break;
62
63 // The linkage name may be attached to the declaration rather than to the
64 // definition.
65 if (!AttrInfo.MangledName)
66 if (const char *LinkageName = InputDIE.getLinkageName())
67 AttrInfo.MangledName =
68 GlobalData.getStringPool().insert(NewValue: LinkageName).first;
69
70 uint64_t RuntimeLang =
71 dwarf::toUnsigned(V: InputDIE.find(Attr: dwarf::DW_AT_APPLE_runtime_class))
72 .value_or(u: 0);
73
74 bool ObjCClassIsImplementation =
75 (RuntimeLang == dwarf::DW_LANG_ObjC ||
76 RuntimeLang == dwarf::DW_LANG_ObjC_plus_plus) &&
77 dwarf::toUnsigned(V: InputDIE.find(Attr: dwarf::DW_AT_APPLE_objc_complete_type))
78 .value_or(u: 0);
79
80 if (AttrInfo.Name != nullptr && !AttrInfo.Name->getKey().empty()) {
81 // A reference is relative to the unit of the DIE that holds it, which
82 // is the unit of the last DIE resolved.
83 CompileUnit *CU = &InUnit;
84 uint32_t Hash = hashFullyQualifiedName(
85 Die: InputDIE, ResolveRef: [&](const DWARFDie &Referrer, const DWARFFormValue &Ref) {
86 assert(Referrer.getDwarfUnit() == &CU->getOrigUnit());
87 std::optional<UnitEntryPairTy> RefDie = CU->resolveDIEReference(
88 RefValue: Ref, CanResolveInterCUReferences: ResolveInterCUReferencesMode::Resolve);
89 if (!RefDie || !RefDie->DieEntry)
90 return DWARFDie();
91 CU = RefDie->CU;
92 return CU->getDIE(Die: RefDie->DieEntry);
93 });
94 saveTypeRecord(InputDieEntry, Name: AttrInfo.Name, OutDIE,
95 Tag: InputDieEntry->getTag(), QualifiedNameHash: Hash, ObjcClassImplementation: ObjCClassIsImplementation,
96 TypeEntry);
97 }
98
99 // Swift carries a type's mangled name in DW_AT_linkage_name, which a
100 // consumer may look up on its own.
101 if (AttrInfo.MangledName != nullptr &&
102 RuntimeLang == dwarf::DW_LANG_Swift &&
103 !AttrInfo.MangledName->getKey().empty() &&
104 AttrInfo.MangledName != AttrInfo.Name)
105 saveTypeRecord(InputDieEntry, Name: AttrInfo.MangledName, OutDIE,
106 Tag: InputDieEntry->getTag(),
107 QualifiedNameHash: djbHash(Buffer: AttrInfo.MangledName->getKey()),
108 ObjcClassImplementation: ObjCClassIsImplementation, TypeEntry);
109 } break;
110 case dwarf::DW_TAG_namespace: {
111 if (AttrInfo.Name == nullptr)
112 AttrInfo.Name =
113 GlobalData.getStringPool().insert(NewValue: "(anonymous namespace)").first;
114
115 saveNamespaceRecord(InputDieEntry, Name: AttrInfo.Name, OutDIE,
116 Tag: InputDieEntry->getTag(), TypeEntry);
117 } break;
118 case dwarf::DW_TAG_imported_declaration: {
119 if (AttrInfo.Name != nullptr)
120 saveNamespaceRecord(InputDieEntry, Name: AttrInfo.Name, OutDIE,
121 Tag: InputDieEntry->getTag(), TypeEntry);
122 } break;
123 case dwarf::DW_TAG_compile_unit:
124 case dwarf::DW_TAG_lexical_block: {
125 // Nothing to do.
126 } break;
127 default:
128 // HasLiveAddress / HasRanges below decides whether a DIE carries enough
129 // information of its own to warrant a name record; the output unit is
130 // incidental and routed by the helpers.
131
132 if ((AttrInfo.HasLiveAddress || AttrInfo.HasRanges) &&
133 !(InputDieEntry->getTag() == dwarf::DW_TAG_variable &&
134 hasImplicitAddressLocation(Die: InputDIE))) {
135 if (AttrInfo.Name)
136 saveNameRecord(
137 InputDieEntry, Name: AttrInfo.Name, OutDIE, Tag: InputDieEntry->getTag(),
138 AvoidForPubSections: InputDieEntry->getTag() == dwarf::DW_TAG_inlined_subroutine,
139 TypeEntry);
140
141 // Look for mangled name recursively if mangled name is not known yet.
142 if (!AttrInfo.MangledName)
143 if (const char *LinkageName = InputDIE.getLinkageName())
144 AttrInfo.MangledName =
145 GlobalData.getStringPool().insert(NewValue: LinkageName).first;
146
147 if (AttrInfo.MangledName && AttrInfo.MangledName != AttrInfo.Name)
148 saveNameRecord(InputDieEntry, Name: AttrInfo.MangledName, OutDIE,
149 Tag: InputDieEntry->getTag(),
150 AvoidForPubSections: InputDieEntry->getTag() ==
151 dwarf::DW_TAG_inlined_subroutine,
152 TypeEntry);
153
154 // Strip template parameters from the short name.
155 if (AttrInfo.Name && AttrInfo.MangledName != AttrInfo.Name &&
156 (InputDieEntry->getTag() != dwarf::DW_TAG_inlined_subroutine)) {
157 if (std::optional<StringRef> Name =
158 StripTemplateParameters(Name: AttrInfo.Name->getKey())) {
159 StringEntry *NameWithoutTemplateParams =
160 GlobalData.getStringPool().insert(NewValue: *Name).first;
161
162 saveNameRecord(InputDieEntry, Name: NameWithoutTemplateParams, OutDIE,
163 Tag: InputDieEntry->getTag(), AvoidForPubSections: true, TypeEntry);
164 }
165 }
166
167 if (AttrInfo.Name)
168 saveObjC(InputDieEntry, OutDIE, AttrInfo, TypeEntry);
169 }
170 break;
171 }
172}
173
174void AcceleratorRecordsSaver::saveObjC(const DWARFDebugInfoEntry *InputDieEntry,
175 DIE *OutDIE, AttributesInfo &AttrInfo,
176 TypeEntry *TypeEntry) {
177 std::optional<ObjCSelectorNames> Names =
178 getObjCNamesIfSelector(Name: AttrInfo.Name->getKey());
179 if (!Names)
180 return;
181
182 StringEntry *Selector =
183 GlobalData.getStringPool().insert(NewValue: Names->Selector).first;
184 saveNameRecord(InputDieEntry, Name: Selector, OutDIE, Tag: InputDieEntry->getTag(), AvoidForPubSections: true,
185 TypeEntry);
186 StringEntry *ClassName =
187 GlobalData.getStringPool().insert(NewValue: Names->ClassName).first;
188 saveObjCNameRecord(InputDieEntry, Name: ClassName, OutDIE, Tag: InputDieEntry->getTag(),
189 TypeEntry);
190 if (Names->ClassNameNoCategory) {
191 StringEntry *ClassNameNoCategory =
192 GlobalData.getStringPool().insert(NewValue: *Names->ClassNameNoCategory).first;
193 saveObjCNameRecord(InputDieEntry, Name: ClassNameNoCategory, OutDIE,
194 Tag: InputDieEntry->getTag(), TypeEntry);
195 }
196 if (Names->MethodNameNoCategory) {
197 StringEntry *MethodNameNoCategory =
198 GlobalData.getStringPool().insert(NewValue: *Names->MethodNameNoCategory).first;
199 saveNameRecord(InputDieEntry, Name: MethodNameNoCategory, OutDIE,
200 Tag: InputDieEntry->getTag(), AvoidForPubSections: true, TypeEntry);
201 }
202}
203
204std::optional<uint64_t> AcceleratorRecordsSaver::getDefiningParentOutOffset(
205 const DWARFDebugInfoEntry *InputDieEntry) {
206 // getDieOutOffset returns this for input DIEs that were not cloned into
207 // this CU's plain DWARF (e.g. routed only into the artificial type unit).
208 // OutDieOffsetArray is zero-initialized and a real DIE never lives at
209 // offset 0 (the CU header occupies the first bytes of the unit), so 0 is
210 // an unambiguous "no plain-DWARF copy" sentinel.
211 constexpr uint64_t NotClonedInPlainDWARF = 0;
212
213 std::optional<uint32_t> ParentIdx = InputDieEntry->getParentIdx();
214 if (!ParentIdx)
215 return std::nullopt;
216 // Skip parents marked as declarations; the name table should only reference
217 // definitions.
218 if (dwarf::toUnsigned(V: InUnit.find(DieIdx: *ParentIdx, Attrs: dwarf::DW_AT_declaration), Default: 0))
219 return std::nullopt;
220 uint64_t ParentOutOffset = InUnit.getDieOutOffset(Idx: *ParentIdx);
221 if (ParentOutOffset == NotClonedInPlainDWARF)
222 return std::nullopt;
223 return ParentOutOffset;
224}
225
226void AcceleratorRecordsSaver::saveNameRecord(
227 const DWARFDebugInfoEntry *InputDieEntry, StringEntry *Name, DIE *OutDIE,
228 dwarf::Tag Tag, bool AvoidForPubSections, TypeEntry *TypeEntry) {
229 if (OutUnit.isCompileUnit()) {
230 assert(TypeEntry == nullptr);
231 DwarfUnit::AccelInfo Info;
232
233 Info.Type = DwarfUnit::AccelType::Name;
234 Info.String = Name;
235 Info.OutOffset = OutDIE->getOffset();
236 Info.ParentOffset = getDefiningParentOutOffset(InputDieEntry);
237 Info.Tag = Tag;
238 Info.AvoidForPubSections = AvoidForPubSections;
239
240 OutUnit.getAsCompileUnit()->saveAcceleratorInfo(Info);
241 return;
242 }
243
244 // TODO: compute DW_IDX_parent for entries emitted into the artificial type
245 // unit (see saveNamespaceRecord).
246
247 assert(TypeEntry != nullptr);
248 TypeUnit::TypeUnitAccelInfo Info;
249 Info.Type = DwarfUnit::AccelType::Name;
250 Info.String = Name;
251 Info.OutOffset = 0xbaddef;
252 Info.Tag = Tag;
253 Info.AvoidForPubSections = AvoidForPubSections;
254 Info.OutDIE = OutDIE;
255 Info.TypeEntryBodyPtr = TypeEntry->getValue().load();
256
257 OutUnit.getAsTypeUnit()->saveAcceleratorInfo(Info);
258}
259void AcceleratorRecordsSaver::saveNamespaceRecord(
260 const DWARFDebugInfoEntry *InputDieEntry, StringEntry *Name, DIE *OutDIE,
261 dwarf::Tag Tag, TypeEntry *TypeEntry) {
262 if (OutUnit.isCompileUnit()) {
263 assert(TypeEntry == nullptr);
264 DwarfUnit::AccelInfo Info;
265
266 Info.Type = DwarfUnit::AccelType::Namespace;
267 Info.String = Name;
268 Info.OutOffset = OutDIE->getOffset();
269 Info.ParentOffset = getDefiningParentOutOffset(InputDieEntry);
270 Info.Tag = Tag;
271
272 OutUnit.getAsCompileUnit()->saveAcceleratorInfo(Info);
273 return;
274 }
275
276 // TODO: compute DW_IDX_parent for entries emitted into the artificial type
277 // unit. The parent lookup via the input-side DIE tree is only valid for
278 // DIEs cloned into this CU's plain DWARF.
279
280 assert(TypeEntry != nullptr);
281 TypeUnit::TypeUnitAccelInfo Info;
282 Info.Type = DwarfUnit::AccelType::Namespace;
283 Info.String = Name;
284 Info.OutOffset = 0xbaddef;
285 Info.Tag = Tag;
286 Info.OutDIE = OutDIE;
287 Info.TypeEntryBodyPtr = TypeEntry->getValue().load();
288
289 OutUnit.getAsTypeUnit()->saveAcceleratorInfo(Info);
290}
291
292void AcceleratorRecordsSaver::saveObjCNameRecord(
293 const DWARFDebugInfoEntry *InputDieEntry, StringEntry *Name, DIE *OutDIE,
294 dwarf::Tag Tag, TypeEntry *TypeEntry) {
295 if (OutUnit.isCompileUnit()) {
296 assert(TypeEntry == nullptr);
297 DwarfUnit::AccelInfo Info;
298
299 Info.Type = DwarfUnit::AccelType::ObjC;
300 Info.String = Name;
301 Info.OutOffset = OutDIE->getOffset();
302 Info.ParentOffset = getDefiningParentOutOffset(InputDieEntry);
303 Info.Tag = Tag;
304 Info.AvoidForPubSections = true;
305
306 OutUnit.getAsCompileUnit()->saveAcceleratorInfo(Info);
307 return;
308 }
309
310 // TODO: compute DW_IDX_parent for entries emitted into the artificial type
311 // unit (see saveNamespaceRecord).
312
313 assert(TypeEntry != nullptr);
314 TypeUnit::TypeUnitAccelInfo Info;
315 Info.Type = DwarfUnit::AccelType::ObjC;
316 Info.String = Name;
317 Info.OutOffset = 0xbaddef;
318 Info.Tag = Tag;
319 Info.AvoidForPubSections = true;
320 Info.OutDIE = OutDIE;
321 Info.TypeEntryBodyPtr = TypeEntry->getValue().load();
322
323 OutUnit.getAsTypeUnit()->saveAcceleratorInfo(Info);
324}
325
326void AcceleratorRecordsSaver::saveTypeRecord(
327 const DWARFDebugInfoEntry *InputDieEntry, StringEntry *Name, DIE *OutDIE,
328 dwarf::Tag Tag, uint32_t QualifiedNameHash, bool ObjcClassImplementation,
329 TypeEntry *TypeEntry) {
330 if (OutUnit.isCompileUnit()) {
331 assert(TypeEntry == nullptr);
332 DwarfUnit::AccelInfo Info;
333
334 Info.Type = DwarfUnit::AccelType::Type;
335 Info.String = Name;
336 Info.OutOffset = OutDIE->getOffset();
337 Info.ParentOffset = getDefiningParentOutOffset(InputDieEntry);
338 Info.Tag = Tag;
339 Info.QualifiedNameHash = QualifiedNameHash;
340 Info.ObjcClassImplementation = ObjcClassImplementation;
341
342 OutUnit.getAsCompileUnit()->saveAcceleratorInfo(Info);
343 return;
344 }
345
346 // TODO: compute DW_IDX_parent for entries emitted into the artificial type
347 // unit (see saveNamespaceRecord).
348
349 assert(TypeEntry != nullptr);
350 TypeUnit::TypeUnitAccelInfo Info;
351
352 Info.Type = DwarfUnit::AccelType::Type;
353 Info.String = Name;
354 Info.OutOffset = 0xbaddef;
355 Info.Tag = Tag;
356 Info.QualifiedNameHash = QualifiedNameHash;
357 Info.ObjcClassImplementation = ObjcClassImplementation;
358 Info.OutDIE = OutDIE;
359 Info.TypeEntryBodyPtr = TypeEntry->getValue().load();
360 OutUnit.getAsTypeUnit()->saveAcceleratorInfo(Info);
361}
362