LLVM 24.0.0git
DWARFLinkerCompileUnit.cpp
Go to the documentation of this file.
1//=== DWARFLinkerCompileUnit.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
11#include "DIEAttributeCloner.h"
12#include "DIEGenerator.h"
13#include "DependencyTracker.h"
20#include "llvm/Support/Path.h"
21#include <utility>
22
23using namespace llvm;
24using namespace dwarf_linker;
25using namespace dwarf_linker::parallel;
26
29 OffsetToUnitTy UnitFromOffset,
31 : DwarfUnit(GlobalData, ID, ClangModuleName), File(File),
32 getUnitFromOffset(UnitFromOffset), Stage(Stage::CreatedNotLoaded),
33 AcceleratorRecords(&GlobalData.getAllocator()) {
34 UnitName = File.FileName;
35 setOutputFormat(Format, Endianess);
37}
38
41 DWARFFile &File, OffsetToUnitTy UnitFromOffset,
43 : DwarfUnit(GlobalData, ID, ClangModuleName), File(File),
44 OrigUnit(&OrigUnit), getUnitFromOffset(UnitFromOffset),
46 AcceleratorRecords(&GlobalData.getAllocator()) {
47 setOutputFormat(Format, Endianess);
49
50 DWARFDie CUDie = OrigUnit.getUnitDIE();
51 if (!CUDie)
52 return;
53
54 Language = CUDie.getLanguage();
55
56 if (!GlobalData.getOptions().NoODR && Language.has_value() &&
57 isODRLanguage(*Language))
58 NoODR = false;
59
60 if (const char *CUName = CUDie.getName(DINameKind::ShortName))
61 UnitName = CUName;
62 else
63 UnitName = File.FileName;
64 SysRoot = dwarf::toStringRef(CUDie.find(dwarf::DW_AT_LLVM_sysroot)).str();
65}
66
68 LineTablePtr = File.Dwarf->getLineTableForUnit(&getOrigUnit());
69}
70
72 // Nothing to reset if stage is less than "Loaded".
73 if (getStage() < Stage::Loaded)
74 return;
75
76 // Note: We need to do erasing for "Loaded" stage because
77 // if live analysys failed then we will have "Loaded" stage
78 // with marking from "LivenessAnalysisDone" stage partially
79 // done. That marking should be cleared.
80
81 for (DIEInfo &Info : DieInfoArray)
82 Info.unsetFlagsWhichSetDuringLiveAnalysis();
83
84 LowPc = std::nullopt;
85 HighPc = 0;
86 Labels.clear();
87 Ranges.clear();
88 Dependencies.reset(nullptr);
89
90 if (getStage() < Stage::Cloned) {
92 return;
93 }
94
95 AcceleratorRecords.erase();
96 AbbreviationsSet.clear();
97 Abbreviations.clear();
98 OutUnitDIE = nullptr;
99 DebugAddrIndexMap.clear();
100 StmtSeqListAttributes.clear();
101
102 llvm::fill(OutDieOffsetArray, 0);
103 llvm::fill(TypeEntries, nullptr);
105
107}
108
110 DWARFDie InputUnitDIE = getUnitDIE(false);
111 if (!InputUnitDIE)
112 return false;
113
114 // load input dies, resize Info structures array.
115 DieInfoArray.resize(getOrigUnit().getNumDIEs());
116 OutDieOffsetArray.resize(getOrigUnit().getNumDIEs(), 0);
117 if (!NoODR)
118 TypeEntries.resize(getOrigUnit().getNumDIEs());
119 return true;
120}
121
122void CompileUnit::analyzeDWARFStructureRec(const DWARFDebugInfoEntry *DieEntry,
123 bool IsODRUnavailableFunctionScope) {
124 CompileUnit::DIEInfo &DieInfo = getDIEInfo(DieEntry);
125
126 for (const DWARFDebugInfoEntry *CurChild = getFirstChildEntry(DieEntry);
127 CurChild && CurChild->getAbbreviationDeclarationPtr();
128 CurChild = getSiblingEntry(CurChild)) {
129 CompileUnit::DIEInfo &ChildInfo = getDIEInfo(CurChild);
130 bool ChildIsODRUnavailableFunctionScope = IsODRUnavailableFunctionScope;
131
132 if (DieInfo.getIsInMouduleScope())
133 ChildInfo.setIsInMouduleScope();
134
135 if (DieInfo.getIsInFunctionScope())
136 ChildInfo.setIsInFunctionScope();
137
138 if (DieInfo.getIsInAnonNamespaceScope())
139 ChildInfo.setIsInAnonNamespaceScope();
140
141 switch (CurChild->getTag()) {
142 case dwarf::DW_TAG_module:
143 ChildInfo.setIsInMouduleScope();
144 if (DieEntry->getTag() == dwarf::DW_TAG_compile_unit &&
145 dwarf::toString(find(CurChild, dwarf::DW_AT_name), "") !=
147 analyzeImportedModule(CurChild);
148 break;
149 case dwarf::DW_TAG_subprogram:
150 ChildInfo.setIsInFunctionScope();
151 if (!ChildIsODRUnavailableFunctionScope &&
152 !ChildInfo.getIsInMouduleScope()) {
153 if (find(CurChild,
154 {dwarf::DW_AT_abstract_origin, dwarf::DW_AT_specification}))
155 ChildIsODRUnavailableFunctionScope = true;
156 }
157 break;
158 case dwarf::DW_TAG_namespace: {
159 UnitEntryPairTy NamespaceEntry = {this, CurChild};
160
161 if (find(CurChild, dwarf::DW_AT_extension))
162 NamespaceEntry = NamespaceEntry.getNamespaceOrigin();
163
164 if (!NamespaceEntry.CU->find(NamespaceEntry.DieEntry, dwarf::DW_AT_name))
165 ChildInfo.setIsInAnonNamespaceScope();
166 } break;
167 default:
168 break;
169 }
170
171 if (!isClangModule() && !getGlobalData().getOptions().UpdateIndexTablesOnly)
172 ChildInfo.setTrackLiveness();
173
174 if ((!ChildInfo.getIsInAnonNamespaceScope() &&
175 !ChildIsODRUnavailableFunctionScope && !NoODR))
176 ChildInfo.setODRAvailable();
177
178 if (CurChild->hasChildren())
179 analyzeDWARFStructureRec(CurChild, ChildIsODRUnavailableFunctionScope);
180 }
181}
182
184 StringPool &GlobalStrings) {
185 if (LineTablePtr) {
186 if (LineTablePtr->hasFileAtIndex(FileIdx)) {
187 // Cache the resolved paths based on the index in the line table,
188 // because calling realpath is expensive.
189 ResolvedPathsMap::const_iterator It = ResolvedFullPaths.find(FileIdx);
190 if (It == ResolvedFullPaths.end()) {
191 std::string OrigFileName;
192 bool FoundFileName = LineTablePtr->getFileNameByIndex(
193 FileIdx, getOrigUnit().getCompilationDir(),
195 OrigFileName);
196 (void)FoundFileName;
197 assert(FoundFileName && "Must get file name from line table");
198
199 // Second level of caching, this time based on the file's parent
200 // path.
201 StringRef FileName = sys::path::filename(OrigFileName);
202 StringRef ParentPath = sys::path::parent_path(OrigFileName);
203
204 // If the ParentPath has not yet been resolved, resolve and cache it for
205 // future look-ups.
207 ResolvedParentPaths.find(ParentPath);
208 if (ParentIt == ResolvedParentPaths.end()) {
209 SmallString<256> RealPath;
210 sys::fs::real_path(ParentPath, RealPath);
211 ParentIt =
212 ResolvedParentPaths
213 .insert({ParentPath, GlobalStrings.insert(RealPath).first})
214 .first;
215 }
216
217 // Join the file name again with the resolved path.
218 SmallString<256> ResolvedPath(ParentIt->second->first());
219 sys::path::append(ResolvedPath, FileName);
220
221 It = ResolvedFullPaths
222 .insert(std::make_pair(
223 FileIdx, GlobalStrings.insert(ResolvedPath).first))
224 .first;
225 }
226
227 return It->second;
228 }
229 }
230
231 return nullptr;
232}
233
234llvm::Error CompileUnit::setPriority(uint64_t ObjFileIdx, uint64_t LocalIdx) {
235 if (ObjFileIdx > std::numeric_limits<uint32_t>::max())
236 return llvm::createStringError("cannot compute priority when number of "
237 "object files exceeds UINT32_MAX");
238 if (LocalIdx > std::numeric_limits<uint32_t>::max())
239 return llvm::createStringError("cannot compute priority when number of "
240 "local index exceeds UINT32_MAX");
241
242 Priority = (ObjFileIdx << 32) | LocalIdx;
243 return llvm::Error::success();
244}
245
247 AbbreviationsSet.clear();
248 ResolvedFullPaths.shrink_and_clear();
249 ResolvedParentPaths.clear();
250 FileNames.shrink_and_clear();
251 DieInfoArray = SmallVector<DIEInfo>();
252 OutDieOffsetArray = SmallVector<uint64_t>();
253 TypeEntries = SmallVector<TypeEntry *>();
254 Dependencies.reset(nullptr);
255 StmtSeqListAttributes.clear();
256 getOrigUnit().clear();
257}
258
260 SmallVectorImpl<char> &Path) {
261 assert(DieEntry->getTag() == dwarf::DW_TAG_module);
262
264 for (const DWARFDebugInfoEntry *CurEntry = DieEntry;
265 CurEntry && CurEntry->getTag() == dwarf::DW_TAG_module;
266 CurEntry = getParent(CurEntry).getDebugInfoEntry()) {
267 StringRef Name = dwarf::toStringRef(find(CurEntry, dwarf::DW_AT_name));
268 if (Name.empty())
269 return false;
270 Names.push_back(Name);
271 }
272
273 for (StringRef Name : reverse(Names)) {
274 Path.append(Name.begin(), Name.end());
275 Path.push_back('\0');
276 }
277
278 return true;
279}
280
282 const DWARFDebugInfoEntry *UnitEntry = getUnitDIE().getDebugInfoEntry();
283 assert(UnitEntry && "unit reached cloning without loaded DIEs");
284
285 // Find the root, ignoring the skeletons for the imported module.
286 const DWARFDebugInfoEntry *Root = nullptr;
287 for (const DWARFDebugInfoEntry *CurChild = getFirstChildEntry(UnitEntry);
288 CurChild && CurChild->getAbbreviationDeclarationPtr();
289 CurChild = getSiblingEntry(CurChild)) {
290 if (CurChild->getTag() == dwarf::DW_TAG_module &&
291 dwarf::toStringRef(find(CurChild, dwarf::DW_AT_name)) ==
293 Root = CurChild;
294 break;
295 }
296 }
297 if (!Root)
298 return;
299
300 SmallString<128> Path;
301
302 // Submodules nest inside the module which declares them, so following the
303 // DW_TAG_module chain down from the root visits every module this unit
304 // describes, without walking the types they contain.
306 while (!WorkList.empty()) {
307 const DWARFDebugInfoEntry *DieEntry = WorkList.pop_back_val();
308
309 for (const DWARFDebugInfoEntry *CurChild = getFirstChildEntry(DieEntry);
310 CurChild && CurChild->getAbbreviationDeclarationPtr();
311 CurChild = getSiblingEntry(CurChild))
312 if (CurChild->getTag() == dwarf::DW_TAG_module)
313 WorkList.push_back(CurChild);
314
315 Path.clear();
316 if (!getModulePath(DieEntry, Path))
317 continue;
318
319 ModuleAnchor Anchor;
320 Anchor.Priority = getPriority();
321 if (getDIEInfo(DieEntry).needToPlaceInTypeTable())
322 Anchor.TypeName = getDieTypeEntry(DieEntry);
323 if (!Anchor.TypeName) {
324 // Don't create a dangling reference to a DIE without a type entry or
325 // output offset.
326 uint64_t OutOffset = getDieOutOffset(DieEntry);
327 if (!OutOffset)
328 continue;
330 Anchor.LocalOffset = OutOffset;
331 }
332
333 getGlobalData().getModulePool().set(Path, Anchor);
334 }
335}
336
337/// Collect references to parseable Swift interfaces in imported
338/// DW_TAG_module blocks.
340 if (!Language || Language != dwarf::DW_LANG_Swift)
341 return;
342
343 if (!GlobalData.getOptions().ParseableSwiftInterfaces)
344 return;
345
346 StringRef Path =
347 dwarf::toStringRef(find(DieEntry, dwarf::DW_AT_LLVM_include_path));
348 if (!Path.ends_with(".swiftinterface"))
349 return;
350 // Don't track interfaces that are part of the SDK.
352 dwarf::toStringRef(find(DieEntry, dwarf::DW_AT_LLVM_sysroot));
353 if (SysRoot.empty())
355 if (!SysRoot.empty() && Path.starts_with(SysRoot))
356 return;
357 // Don't track interfaces that are part of the toolchain.
358 // For example: Swift, _Concurrency, ...
359 StringRef DeveloperDir = guessDeveloperDir(SysRoot);
360 if (!DeveloperDir.empty() && Path.starts_with(DeveloperDir))
361 return;
362 if (isInToolchainDir(Path))
363 return;
364 if (std::optional<DWARFFormValue> Val = find(DieEntry, dwarf::DW_AT_name)) {
365 Expected<const char *> Name = Val->getAsCString();
366 if (!Name) {
367 warn(Name.takeError());
368 return;
369 }
370
371 // The prepend path is applied later when copying.
372 SmallString<128> ResolvedPath;
373 if (sys::path::is_relative(Path))
375 ResolvedPath,
376 dwarf::toString(getUnitDIE().find(dwarf::DW_AT_comp_dir), ""));
377 sys::path::append(ResolvedPath, Path);
378
379 // Stage the entry. It will be merged into the shared
380 // ParseableSwiftInterfaces map after the parallel analysis phase so that
381 // the final contents and any conflict warnings are deterministic.
382 PendingSwiftInterfaces.emplace_back(*Name, ResolvedPath);
383 }
384}
385
388 for (auto &Pending : PendingSwiftInterfaces) {
389 auto &Entry = Map[Pending.ModuleName];
390 if (!Entry.empty() && Entry != Pending.ResolvedPath)
391 warn(Twine("conflicting parseable interfaces for Swift Module ") +
392 Pending.ModuleName + ": " + Entry + " and " + Pending.ResolvedPath +
393 ".");
394 Entry = Pending.ResolvedPath;
395 }
396 PendingSwiftInterfaces.clear();
397}
398
400 if (!getUnitDIE().isValid())
401 return Error::success();
402
403 SyntheticTypeNameBuilder NameBuilder(TypePoolRef);
404 return assignTypeNamesRec(getDebugInfoEntry(0), NameBuilder);
405}
406
407Error CompileUnit::assignTypeNamesRec(const DWARFDebugInfoEntry *DieEntry,
408 SyntheticTypeNameBuilder &NameBuilder) {
409 OrderedChildrenIndexAssigner ChildrenIndexAssigner(*this, DieEntry);
410 for (const DWARFDebugInfoEntry *CurChild = getFirstChildEntry(DieEntry);
411 CurChild && CurChild->getAbbreviationDeclarationPtr();
412 CurChild = getSiblingEntry(CurChild)) {
413 CompileUnit::DIEInfo &ChildInfo = getDIEInfo(CurChild);
414 if (!ChildInfo.needToPlaceInTypeTable())
415 continue;
416
417 assert(ChildInfo.getODRAvailable());
418 if (Error Err = NameBuilder.assignName(
419 {this, CurChild},
420 ChildrenIndexAssigner.getChildIndex(*this, CurChild)))
421 return Err;
422
423 if (Error Err = assignTypeNamesRec(CurChild, NameBuilder))
424 return Err;
425 }
426
427 return Error::success();
428}
429
431 if (std::optional<SectionDescriptor *> DebugInfoSection =
433
434 (*DebugInfoSection)
435 ->ListDebugDieRefPatch.forEach([&](DebugDieRefPatch &Patch) {
436 /// Replace stored DIE indexes with DIE output offsets.
438 Patch.RefCU.getPointer()->getDieOutOffset(
440 });
441
442 (*DebugInfoSection)
443 ->ListDebugULEB128DieRefPatch.forEach(
444 [&](DebugULEB128DieRefPatch &Patch) {
445 /// Replace stored DIE indexes with DIE output offsets.
447 Patch.RefCU.getPointer()->getDieOutOffset(
449 });
450
451 (*DebugInfoSection)
452 ->ListDebugDieModuleRefPatch.forEach(
453 [&](DebugDieModuleRefPatch &Patch) {
454 /// Replace stored DIE indexes with DIE output offsets.
456 Patch.RefCU.getPointer()->getDieOutOffset(
458 });
459 }
460
461 if (std::optional<SectionDescriptor *> DebugLocSection =
463 (*DebugLocSection)
464 ->ListDebugULEB128DieRefPatch.forEach(
465 [](DebugULEB128DieRefPatch &Patch) {
466 /// Replace stored DIE indexes with DIE output offsets.
468 Patch.RefCU.getPointer()->getDieOutOffset(
470 });
471 }
472
473 if (std::optional<SectionDescriptor *> DebugLocListsSection =
475 (*DebugLocListsSection)
476 ->ListDebugULEB128DieRefPatch.forEach(
477 [](DebugULEB128DieRefPatch &Patch) {
478 /// Replace stored DIE indexes with DIE output offsets.
480 Patch.RefCU.getPointer()->getDieOutOffset(
482 });
483 }
484}
485
486std::optional<UnitEntryPairTy> CompileUnit::resolveDIEReference(
487 const DWARFFormValue &RefValue,
488 ResolveInterCUReferencesMode CanResolveInterCUReferences) {
489 CompileUnit *RefCU;
490 uint64_t RefDIEOffset;
491 if (std::optional<uint64_t> Offset = RefValue.getAsRelativeReference()) {
492 RefCU = this;
493 RefDIEOffset = RefValue.getUnit()->getOffset() + *Offset;
494 } else if (Offset = RefValue.getAsDebugInfoReference(); Offset) {
495 RefCU = getUnitFromOffset(*Offset);
496 RefDIEOffset = *Offset;
497 } else {
498 return std::nullopt;
499 }
500
501 if (RefCU == this) {
502 // Referenced DIE is in current compile unit.
503 if (std::optional<uint32_t> RefDieIdx =
504 getDIEIndexForOffset(RefDIEOffset)) {
505 const DWARFDebugInfoEntry *RefEntry = getDebugInfoEntry(*RefDieIdx);
506 // In a file with broken references, an attribute might point to a
507 // NULL DIE. Treat that as a resolution failure so callers can warn.
508 if (RefEntry && RefEntry->getAbbreviationDeclarationPtr())
509 return UnitEntryPairTy{this, RefEntry};
510 }
511 } else if (RefCU && CanResolveInterCUReferences) {
512 // Referenced DIE is in other compile unit.
513
514 // Check whether DIEs are loaded for that compile unit.
515 enum Stage ReferredCUStage = RefCU->getStage();
516 if (ReferredCUStage < Stage::Loaded || ReferredCUStage > Stage::Cloned)
517 return UnitEntryPairTy{RefCU, nullptr};
518
519 if (std::optional<uint32_t> RefDieIdx =
520 RefCU->getDIEIndexForOffset(RefDIEOffset)) {
521 const DWARFDebugInfoEntry *RefEntry =
522 RefCU->getDebugInfoEntry(*RefDieIdx);
523 if (RefEntry && RefEntry->getAbbreviationDeclarationPtr())
524 return UnitEntryPairTy{RefCU, RefEntry};
525 }
526 } else {
527 return UnitEntryPairTy{RefCU, nullptr};
528 }
529 return std::nullopt;
530}
531
532std::optional<UnitEntryPairTy> CompileUnit::resolveDIEReference(
533 const DWARFDebugInfoEntry *DieEntry, dwarf::Attribute Attr,
534 ResolveInterCUReferencesMode CanResolveInterCUReferences) {
535 if (std::optional<DWARFFormValue> AttrVal = find(DieEntry, Attr))
536 return resolveDIEReference(*AttrVal, CanResolveInterCUReferences);
537
538 return std::nullopt;
539}
540
541void CompileUnit::addFunctionRange(uint64_t FuncLowPc, uint64_t FuncHighPc,
542 int64_t PcOffset) {
543 std::lock_guard<std::mutex> Guard(RangesMutex);
544
545 Ranges.insert({FuncLowPc, FuncHighPc}, PcOffset);
546 if (LowPc)
547 LowPc = std::min(*LowPc, FuncLowPc + PcOffset);
548 else
549 LowPc = FuncLowPc + PcOffset;
550 this->HighPc = std::max(HighPc, FuncHighPc + PcOffset);
551}
552
553void CompileUnit::addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset) {
554 std::lock_guard<std::mutex> Guard(LabelsMutex);
555 Labels.insert({LabelLowPc, PcOffset});
556}
557
559 if (getGlobalData().getOptions().UpdateIndexTablesOnly)
560 return Error::success();
561
562 if (getOrigUnit().getVersion() < 5) {
563 emitLocations(DebugSectionKind::DebugLoc);
564 return Error::success();
565 }
566
567 emitLocations(DebugSectionKind::DebugLocLists);
568 return Error::success();
569}
570
571void CompileUnit::emitLocations(DebugSectionKind LocationSectionKind) {
572 SectionDescriptor &DebugInfoSection =
574
575 if (!DebugInfoSection.ListDebugLocPatch.empty()) {
576 SectionDescriptor &OutLocationSection =
577 getOrCreateSectionDescriptor(LocationSectionKind);
578 DWARFUnit &OrigUnit = getOrigUnit();
579
580 uint64_t OffsetAfterUnitLength = emitLocListHeader(OutLocationSection);
581
582 DebugInfoSection.ListDebugLocPatch.forEach([&](DebugLocPatch &Patch) {
583 // Get location expressions vector corresponding to the current
584 // attribute from the source DWARF.
585 uint64_t InputDebugLocSectionOffset = DebugInfoSection.getIntVal(
586 Patch.PatchOffset,
587 DebugInfoSection.getFormParams().getDwarfOffsetByteSize());
589 OrigUnit.findLoclistFromOffset(InputDebugLocSectionOffset);
590
591 if (!OriginalLocations) {
592 warn(OriginalLocations.takeError());
593 return;
594 }
595
596 LinkedLocationExpressionsVector LinkedLocationExpressions;
597 for (DWARFLocationExpression &CurExpression : *OriginalLocations) {
598 LinkedLocationExpressionsWithOffsetPatches LinkedExpression;
599
600 if (CurExpression.Range) {
601 // Relocate address range.
602 LinkedExpression.Expression.Range = {
603 CurExpression.Range->LowPC + Patch.AddrAdjustmentValue,
604 CurExpression.Range->HighPC + Patch.AddrAdjustmentValue};
605 }
606
607 DataExtractor Data(CurExpression.Expr, OrigUnit.isLittleEndian());
608
609 DWARFExpression InputExpression(Data, OrigUnit.getAddressByteSize(),
610 OrigUnit.getFormParams().Format);
611 cloneDieAttrExpression(InputExpression,
612 LinkedExpression.Expression.Expr,
613 OutLocationSection, Patch.AddrAdjustmentValue,
614 LinkedExpression.Patches);
615
616 LinkedLocationExpressions.push_back({LinkedExpression});
617 }
618
619 // Emit locations list table fragment corresponding to the CurLocAttr.
620 DebugInfoSection.apply(Patch.PatchOffset, dwarf::DW_FORM_sec_offset,
621 OutLocationSection.OS.tell());
622 emitLocListFragment(LinkedLocationExpressions, OutLocationSection);
623 });
624
625 if (OffsetAfterUnitLength > 0) {
626 assert(OffsetAfterUnitLength -
627 OutLocationSection.getFormParams().getDwarfOffsetByteSize() <
628 OffsetAfterUnitLength);
629 OutLocationSection.apply(
630 OffsetAfterUnitLength -
631 OutLocationSection.getFormParams().getDwarfOffsetByteSize(),
632 dwarf::DW_FORM_sec_offset,
633 OutLocationSection.OS.tell() - OffsetAfterUnitLength);
634 }
635 }
636}
637
638/// Emit debug locations(.debug_loc, .debug_loclists) header.
639uint64_t CompileUnit::emitLocListHeader(SectionDescriptor &OutLocationSection) {
640 if (getOrigUnit().getVersion() < 5)
641 return 0;
642
643 // unit_length.
644 OutLocationSection.emitUnitLength(0xBADDEF);
645 uint64_t OffsetAfterUnitLength = OutLocationSection.OS.tell();
646
647 // Version.
648 OutLocationSection.emitIntVal(5, 2);
649
650 // Address size.
651 OutLocationSection.emitIntVal(OutLocationSection.getFormParams().AddrSize, 1);
652
653 // Seg_size
654 OutLocationSection.emitIntVal(0, 1);
655
656 // Offset entry count
657 OutLocationSection.emitIntVal(0, 4);
658
659 return OffsetAfterUnitLength;
660}
661
662/// Emit debug locations(.debug_loc, .debug_loclists) fragment.
663uint64_t CompileUnit::emitLocListFragment(
664 const LinkedLocationExpressionsVector &LinkedLocationExpression,
665 SectionDescriptor &OutLocationSection) {
666 uint64_t OffsetBeforeLocationExpression = 0;
667
668 if (getOrigUnit().getVersion() < 5) {
669 uint64_t BaseAddress = 0;
670 if (std::optional<uint64_t> LowPC = getLowPc())
671 BaseAddress = *LowPC;
672
673 for (const LinkedLocationExpressionsWithOffsetPatches &LocExpression :
674 LinkedLocationExpression) {
675 if (LocExpression.Expression.Range) {
676 OutLocationSection.emitIntVal(
677 LocExpression.Expression.Range->LowPC - BaseAddress,
678 OutLocationSection.getFormParams().AddrSize);
679 OutLocationSection.emitIntVal(
680 LocExpression.Expression.Range->HighPC - BaseAddress,
681 OutLocationSection.getFormParams().AddrSize);
682 }
683
684 OutLocationSection.emitIntVal(LocExpression.Expression.Expr.size(), 2);
685 OffsetBeforeLocationExpression = OutLocationSection.OS.tell();
686 for (uint64_t *OffsetPtr : LocExpression.Patches)
687 *OffsetPtr += OffsetBeforeLocationExpression;
688
689 OutLocationSection.OS
690 << StringRef((const char *)LocExpression.Expression.Expr.data(),
691 LocExpression.Expression.Expr.size());
692 }
693
694 // Emit the terminator entry.
695 OutLocationSection.emitIntVal(0,
696 OutLocationSection.getFormParams().AddrSize);
697 OutLocationSection.emitIntVal(0,
698 OutLocationSection.getFormParams().AddrSize);
699 return OffsetBeforeLocationExpression;
700 }
701
702 std::optional<uint64_t> BaseAddress;
703 for (const LinkedLocationExpressionsWithOffsetPatches &LocExpression :
704 LinkedLocationExpression) {
705 if (LocExpression.Expression.Range) {
706 // Check whether base address is set. If it is not set yet
707 // then set current base address and emit base address selection entry.
708 if (!BaseAddress) {
709 BaseAddress = LocExpression.Expression.Range->LowPC;
710
711 // Emit base address.
712 OutLocationSection.emitIntVal(dwarf::DW_LLE_base_addressx, 1);
713 encodeULEB128(DebugAddrIndexMap.getValueIndex(*BaseAddress),
714 OutLocationSection.OS);
715 }
716
717 // Emit type of entry.
718 OutLocationSection.emitIntVal(dwarf::DW_LLE_offset_pair, 1);
719
720 // Emit start offset relative to base address.
721 encodeULEB128(LocExpression.Expression.Range->LowPC - *BaseAddress,
722 OutLocationSection.OS);
723
724 // Emit end offset relative to base address.
725 encodeULEB128(LocExpression.Expression.Range->HighPC - *BaseAddress,
726 OutLocationSection.OS);
727 } else
728 // Emit type of entry.
729 OutLocationSection.emitIntVal(dwarf::DW_LLE_default_location, 1);
730
731 encodeULEB128(LocExpression.Expression.Expr.size(), OutLocationSection.OS);
732 OffsetBeforeLocationExpression = OutLocationSection.OS.tell();
733 for (uint64_t *OffsetPtr : LocExpression.Patches)
734 *OffsetPtr += OffsetBeforeLocationExpression;
735
736 OutLocationSection.OS << StringRef(
737 (const char *)LocExpression.Expression.Expr.data(),
738 LocExpression.Expression.Expr.size());
739 }
740
741 // Emit the terminator entry.
742 OutLocationSection.emitIntVal(dwarf::DW_LLE_end_of_list, 1);
743 return OffsetBeforeLocationExpression;
744}
745
746Error CompileUnit::emitDebugAddrSection() {
747 if (GlobalData.getOptions().UpdateIndexTablesOnly)
748 return Error::success();
749
750 if (getVersion() < 5)
751 return Error::success();
752
753 // Every cloned unit root has DW_AT_addr_base, so emit the table even when it
754 // is empty.
755 SectionDescriptor &OutAddrSection =
757
758 // Emit section header.
759
760 // Emit length.
761 OutAddrSection.emitUnitLength(0xBADDEF);
762 uint64_t OffsetAfterSectionLength = OutAddrSection.OS.tell();
763
764 // Emit version.
765 OutAddrSection.emitIntVal(5, 2);
766
767 // Emit address size.
768 OutAddrSection.emitIntVal(getFormParams().AddrSize, 1);
769
770 // Emit segment size.
771 OutAddrSection.emitIntVal(0, 1);
772
773 // Emit addresses.
774 for (uint64_t AddrValue : DebugAddrIndexMap.getValues())
775 OutAddrSection.emitIntVal(AddrValue, getFormParams().AddrSize);
776
777 // Patch section length.
778 OutAddrSection.apply(
779 OffsetAfterSectionLength -
780 OutAddrSection.getFormParams().getDwarfOffsetByteSize(),
781 dwarf::DW_FORM_sec_offset,
782 OutAddrSection.OS.tell() - OffsetAfterSectionLength);
783
784 return Error::success();
785}
786
788 if (getGlobalData().getOptions().UpdateIndexTablesOnly)
789 return Error::success();
790
791 // Build set of linked address ranges for unit function ranges.
792 AddressRanges LinkedFunctionRanges;
794 LinkedFunctionRanges.insert(
795 {Range.Range.start() + Range.Value, Range.Range.end() + Range.Value});
796
797 emitAranges(LinkedFunctionRanges);
798
799 if (getOrigUnit().getVersion() < 5) {
800 cloneAndEmitRangeList(DebugSectionKind::DebugRange, LinkedFunctionRanges);
801 return Error::success();
802 }
803
804 cloneAndEmitRangeList(DebugSectionKind::DebugRngLists, LinkedFunctionRanges);
805 return Error::success();
806}
807
808void CompileUnit::cloneAndEmitRangeList(DebugSectionKind RngSectionKind,
809 AddressRanges &LinkedFunctionRanges) {
810 SectionDescriptor &DebugInfoSection =
812 SectionDescriptor &OutRangeSection =
813 getOrCreateSectionDescriptor(RngSectionKind);
814
815 if (!DebugInfoSection.ListDebugRangePatch.empty()) {
816 std::optional<AddressRangeValuePair> CachedRange;
817 uint64_t OffsetAfterUnitLength = emitRangeListHeader(OutRangeSection);
818
819 DebugRangePatch *CompileUnitRangePtr = nullptr;
820 DebugInfoSection.ListDebugRangePatch.forEach([&](DebugRangePatch &Patch) {
821 if (Patch.IsCompileUnitRanges) {
822 CompileUnitRangePtr = &Patch;
823 } else {
824 // Get ranges from the source DWARF corresponding to the current
825 // attribute.
826 AddressRanges LinkedRanges;
827 uint64_t InputDebugRangesSectionOffset = DebugInfoSection.getIntVal(
828 Patch.PatchOffset,
829 DebugInfoSection.getFormParams().getDwarfOffsetByteSize());
830 if (Expected<DWARFAddressRangesVector> InputRanges =
831 getOrigUnit().findRnglistFromOffset(
832 InputDebugRangesSectionOffset)) {
833 // Apply relocation adjustment.
834 for (const auto &Range : *InputRanges) {
835 if (!CachedRange || !CachedRange->Range.contains(Range.LowPC))
836 CachedRange =
838
839 // All range entries should lie in the function range.
840 if (!CachedRange) {
841 warn("inconsistent range data.");
842 continue;
843 }
844
845 // Store range for emiting.
846 LinkedRanges.insert({Range.LowPC + CachedRange->Value,
847 Range.HighPC + CachedRange->Value});
848 }
849 } else {
850 llvm::consumeError(InputRanges.takeError());
851 warn("invalid range list ignored.");
852 }
853
854 // Emit linked ranges.
855 DebugInfoSection.apply(Patch.PatchOffset, dwarf::DW_FORM_sec_offset,
856 OutRangeSection.OS.tell());
857 emitRangeListFragment(LinkedRanges, OutRangeSection);
858 }
859 });
860
861 if (CompileUnitRangePtr != nullptr) {
862 // Emit compile unit ranges last to be binary compatible with classic
863 // dsymutil.
864 DebugInfoSection.apply(CompileUnitRangePtr->PatchOffset,
865 dwarf::DW_FORM_sec_offset,
866 OutRangeSection.OS.tell());
867 emitRangeListFragment(LinkedFunctionRanges, OutRangeSection);
868 }
869
870 if (OffsetAfterUnitLength > 0) {
871 assert(OffsetAfterUnitLength -
872 OutRangeSection.getFormParams().getDwarfOffsetByteSize() <
873 OffsetAfterUnitLength);
874 OutRangeSection.apply(
875 OffsetAfterUnitLength -
876 OutRangeSection.getFormParams().getDwarfOffsetByteSize(),
877 dwarf::DW_FORM_sec_offset,
878 OutRangeSection.OS.tell() - OffsetAfterUnitLength);
879 }
880 }
881}
882
883uint64_t CompileUnit::emitRangeListHeader(SectionDescriptor &OutRangeSection) {
884 if (OutRangeSection.getFormParams().Version < 5)
885 return 0;
886
887 // unit_length.
888 OutRangeSection.emitUnitLength(0xBADDEF);
889 uint64_t OffsetAfterUnitLength = OutRangeSection.OS.tell();
890
891 // Version.
892 OutRangeSection.emitIntVal(5, 2);
893
894 // Address size.
895 OutRangeSection.emitIntVal(OutRangeSection.getFormParams().AddrSize, 1);
896
897 // Seg_size
898 OutRangeSection.emitIntVal(0, 1);
899
900 // Offset entry count
901 OutRangeSection.emitIntVal(0, 4);
902
903 return OffsetAfterUnitLength;
904}
905
906void CompileUnit::emitRangeListFragment(const AddressRanges &LinkedRanges,
907 SectionDescriptor &OutRangeSection) {
908 if (OutRangeSection.getFormParams().Version < 5) {
909 // Emit ranges.
910 uint64_t BaseAddress = 0;
911 if (std::optional<uint64_t> LowPC = getLowPc())
912 BaseAddress = *LowPC;
913
914 for (const AddressRange &Range : LinkedRanges) {
915 OutRangeSection.emitIntVal(Range.start() - BaseAddress,
916 OutRangeSection.getFormParams().AddrSize);
917 OutRangeSection.emitIntVal(Range.end() - BaseAddress,
918 OutRangeSection.getFormParams().AddrSize);
919 }
920
921 // Add the terminator entry.
922 OutRangeSection.emitIntVal(0, OutRangeSection.getFormParams().AddrSize);
923 OutRangeSection.emitIntVal(0, OutRangeSection.getFormParams().AddrSize);
924 return;
925 }
926
927 std::optional<uint64_t> BaseAddress;
928 for (const AddressRange &Range : LinkedRanges) {
929 if (!BaseAddress) {
930 BaseAddress = Range.start();
931
932 // Emit base address.
933 OutRangeSection.emitIntVal(dwarf::DW_RLE_base_addressx, 1);
934 encodeULEB128(getDebugAddrIndex(*BaseAddress), OutRangeSection.OS);
935 }
936
937 // Emit type of entry.
938 OutRangeSection.emitIntVal(dwarf::DW_RLE_offset_pair, 1);
939
940 // Emit start offset relative to base address.
941 encodeULEB128(Range.start() - *BaseAddress, OutRangeSection.OS);
942
943 // Emit end offset relative to base address.
944 encodeULEB128(Range.end() - *BaseAddress, OutRangeSection.OS);
945 }
946
947 // Emit the terminator entry.
948 OutRangeSection.emitIntVal(dwarf::DW_RLE_end_of_list, 1);
949}
950
951void CompileUnit::emitAranges(AddressRanges &LinkedFunctionRanges) {
952 if (LinkedFunctionRanges.empty())
953 return;
954
955 SectionDescriptor &DebugInfoSection =
957 SectionDescriptor &OutArangesSection =
959
960 // Emit Header.
961 unsigned HeaderSize =
962 sizeof(int32_t) + // Size of contents (w/o this field
963 sizeof(int16_t) + // DWARF ARange version number
964 sizeof(int32_t) + // Offset of CU in the .debug_info section
965 sizeof(int8_t) + // Pointer Size (in bytes)
966 sizeof(int8_t); // Segment Size (in bytes)
967
968 unsigned TupleSize = OutArangesSection.getFormParams().AddrSize * 2;
969 unsigned Padding = offsetToAlignment(HeaderSize, Align(TupleSize));
970
971 OutArangesSection.emitOffset(0xBADDEF); // Aranges length
972 uint64_t OffsetAfterArangesLengthField = OutArangesSection.OS.tell();
973
974 OutArangesSection.emitIntVal(dwarf::DW_ARANGES_VERSION, 2); // Version number
975 OutArangesSection.notePatch(
976 DebugOffsetPatch{OutArangesSection.OS.tell(), &DebugInfoSection});
977 OutArangesSection.emitOffset(0xBADDEF); // Corresponding unit's offset
978 OutArangesSection.emitIntVal(OutArangesSection.getFormParams().AddrSize,
979 1); // Address size
980 OutArangesSection.emitIntVal(0, 1); // Segment size
981
982 for (size_t Idx = 0; Idx < Padding; Idx++)
983 OutArangesSection.emitIntVal(0, 1); // Padding
984
985 // Emit linked ranges.
986 for (const AddressRange &Range : LinkedFunctionRanges) {
987 OutArangesSection.emitIntVal(Range.start(),
988 OutArangesSection.getFormParams().AddrSize);
989 OutArangesSection.emitIntVal(Range.end() - Range.start(),
990 OutArangesSection.getFormParams().AddrSize);
991 }
992
993 // Emit terminator.
994 OutArangesSection.emitIntVal(0, OutArangesSection.getFormParams().AddrSize);
995 OutArangesSection.emitIntVal(0, OutArangesSection.getFormParams().AddrSize);
996
997 uint64_t OffsetAfterArangesEnd = OutArangesSection.OS.tell();
998
999 // Update Aranges lentgh.
1000 OutArangesSection.apply(
1001 OffsetAfterArangesLengthField -
1002 OutArangesSection.getFormParams().getDwarfOffsetByteSize(),
1003 dwarf::DW_FORM_sec_offset,
1004 OffsetAfterArangesEnd - OffsetAfterArangesLengthField);
1005}
1006
1008 if (getOutUnitDIE() == nullptr)
1009 return Error::success();
1010
1011 DWARFUnit &OrigUnit = getOrigUnit();
1012 DWARFDie OrigUnitDie = OrigUnit.getUnitDIE();
1013
1014 // Check for .debug_macro table.
1015 if (std::optional<uint64_t> MacroAttr =
1016 dwarf::toSectionOffset(OrigUnitDie.find(dwarf::DW_AT_macros))) {
1017 if (const DWARFDebugMacro *Table =
1018 getContainingFile().Dwarf->getDebugMacro()) {
1019 emitMacroTableImpl(Table, *MacroAttr, true);
1020 }
1021 }
1022
1023 // Check for .debug_macinfo table.
1024 if (std::optional<uint64_t> MacroAttr =
1025 dwarf::toSectionOffset(OrigUnitDie.find(dwarf::DW_AT_macro_info))) {
1026 if (const DWARFDebugMacro *Table =
1027 getContainingFile().Dwarf->getDebugMacinfo()) {
1028 emitMacroTableImpl(Table, *MacroAttr, false);
1029 }
1030 }
1031
1032 return Error::success();
1033}
1034
1035void CompileUnit::emitMacroTableImpl(const DWARFDebugMacro *MacroTable,
1036 uint64_t OffsetToMacroTable,
1037 bool hasDWARFv5Header) {
1038 SectionDescriptor &OutSection =
1039 hasDWARFv5Header
1042
1043 bool DefAttributeIsReported = false;
1044 bool UndefAttributeIsReported = false;
1045 bool ImportAttributeIsReported = false;
1046
1047 for (const DWARFDebugMacro::MacroList &List : MacroTable->MacroLists) {
1048 if (OffsetToMacroTable == List.Offset) {
1049 // Write DWARFv5 header.
1050 if (hasDWARFv5Header) {
1051 // Write header version.
1052 OutSection.emitIntVal(List.Header.Version, sizeof(List.Header.Version));
1053
1054 uint8_t Flags = List.Header.Flags;
1055
1056 // Check for OPCODE_OPERANDS_TABLE.
1057 if (Flags &
1058 DWARFDebugMacro::HeaderFlagMask::MACRO_OPCODE_OPERANDS_TABLE) {
1059 Flags &=
1060 ~DWARFDebugMacro::HeaderFlagMask::MACRO_OPCODE_OPERANDS_TABLE;
1061 warn("opcode_operands_table is not supported yet.");
1062 }
1063
1064 // Check for DEBUG_LINE_OFFSET.
1065 std::optional<uint64_t> StmtListOffset;
1066 if (Flags & DWARFDebugMacro::HeaderFlagMask::MACRO_DEBUG_LINE_OFFSET) {
1067 // Get offset to the line table from the cloned compile unit.
1068 for (auto &V : getOutUnitDIE()->values()) {
1069 if (V.getAttribute() == dwarf::DW_AT_stmt_list) {
1070 StmtListOffset = V.getDIEInteger().getValue();
1071 break;
1072 }
1073 }
1074
1075 if (!StmtListOffset) {
1076 Flags &= ~DWARFDebugMacro::HeaderFlagMask::MACRO_DEBUG_LINE_OFFSET;
1077 warn("couldn`t find line table for macro table.");
1078 }
1079 }
1080
1081 // Write flags.
1082 OutSection.emitIntVal(Flags, sizeof(Flags));
1083
1084 // Write offset to line table.
1085 if (StmtListOffset) {
1086 OutSection.notePatch(DebugOffsetPatch{
1087 OutSection.OS.tell(),
1088 &getOrCreateSectionDescriptor(DebugSectionKind::DebugLine)});
1089 // TODO: check that List.Header.getOffsetByteSize() and
1090 // DebugOffsetPatch agree on size.
1091 OutSection.emitIntVal(0xBADDEF, List.Header.getOffsetByteSize());
1092 }
1093 }
1094
1095 // Write macro entries.
1096 for (const DWARFDebugMacro::Entry &MacroEntry : List.Macros) {
1097 if (MacroEntry.Type == 0) {
1098 encodeULEB128(MacroEntry.Type, OutSection.OS);
1099 continue;
1100 }
1101
1102 uint8_t MacroType = MacroEntry.Type;
1103 switch (MacroType) {
1104 default: {
1105 bool HasVendorSpecificExtension =
1106 (!hasDWARFv5Header &&
1107 MacroType == dwarf::DW_MACINFO_vendor_ext) ||
1108 (hasDWARFv5Header && (MacroType >= dwarf::DW_MACRO_lo_user &&
1109 MacroType <= dwarf::DW_MACRO_hi_user));
1110
1111 if (HasVendorSpecificExtension) {
1112 // Write macinfo type.
1113 OutSection.emitIntVal(MacroType, 1);
1114
1115 // Write vendor extension constant.
1116 encodeULEB128(MacroEntry.ExtConstant, OutSection.OS);
1117
1118 // Write vendor extension string.
1119 OutSection.emitString(dwarf::DW_FORM_string, MacroEntry.ExtStr);
1120 } else
1121 warn("unknown macro type. skip.");
1122 } break;
1123 // debug_macro and debug_macinfo share some common encodings.
1124 // DW_MACRO_define == DW_MACINFO_define
1125 // DW_MACRO_undef == DW_MACINFO_undef
1126 // DW_MACRO_start_file == DW_MACINFO_start_file
1127 // DW_MACRO_end_file == DW_MACINFO_end_file
1128 // For readibility/uniformity we are using DW_MACRO_*.
1129 case dwarf::DW_MACRO_define:
1130 case dwarf::DW_MACRO_undef: {
1131 // Write macinfo type.
1132 OutSection.emitIntVal(MacroType, 1);
1133
1134 // Write source line.
1135 encodeULEB128(MacroEntry.Line, OutSection.OS);
1136
1137 // Write macro string.
1138 OutSection.emitString(dwarf::DW_FORM_string, MacroEntry.MacroStr);
1139 } break;
1140 case dwarf::DW_MACRO_define_strp:
1141 case dwarf::DW_MACRO_undef_strp:
1142 case dwarf::DW_MACRO_define_strx:
1143 case dwarf::DW_MACRO_undef_strx: {
1144 // DW_MACRO_*_strx forms are not supported currently.
1145 // Convert to *_strp.
1146 switch (MacroType) {
1147 case dwarf::DW_MACRO_define_strx: {
1148 MacroType = dwarf::DW_MACRO_define_strp;
1149 if (!DefAttributeIsReported) {
1150 warn("DW_MACRO_define_strx unsupported yet. Convert to "
1151 "DW_MACRO_define_strp.");
1152 DefAttributeIsReported = true;
1153 }
1154 } break;
1155 case dwarf::DW_MACRO_undef_strx: {
1156 MacroType = dwarf::DW_MACRO_undef_strp;
1157 if (!UndefAttributeIsReported) {
1158 warn("DW_MACRO_undef_strx unsupported yet. Convert to "
1159 "DW_MACRO_undef_strp.");
1160 UndefAttributeIsReported = true;
1161 }
1162 } break;
1163 default:
1164 // Nothing to do.
1165 break;
1166 }
1167
1168 // Write macinfo type.
1169 OutSection.emitIntVal(MacroType, 1);
1170
1171 // Write source line.
1172 encodeULEB128(MacroEntry.Line, OutSection.OS);
1173
1174 // Write macro string.
1175 OutSection.emitString(dwarf::DW_FORM_strp, MacroEntry.MacroStr);
1176 break;
1177 }
1178 case dwarf::DW_MACRO_start_file: {
1179 // Write macinfo type.
1180 OutSection.emitIntVal(MacroType, 1);
1181 // Write source line.
1182 encodeULEB128(MacroEntry.Line, OutSection.OS);
1183 // Write source file id.
1184 encodeULEB128(MacroEntry.File, OutSection.OS);
1185 } break;
1186 case dwarf::DW_MACRO_end_file: {
1187 // Write macinfo type.
1188 OutSection.emitIntVal(MacroType, 1);
1189 } break;
1190 case dwarf::DW_MACRO_import:
1191 case dwarf::DW_MACRO_import_sup: {
1192 if (!ImportAttributeIsReported) {
1193 warn("DW_MACRO_import and DW_MACRO_import_sup are unsupported "
1194 "yet. remove.");
1195 ImportAttributeIsReported = true;
1196 }
1197 } break;
1198 }
1199 }
1200
1201 return;
1202 }
1203 }
1204}
1205
1207 const DWARFExpression &InputExpression,
1208 SmallVectorImpl<uint8_t> &OutputExpression, SectionDescriptor &Section,
1209 std::optional<int64_t> VarAddressAdjustment,
1210 OffsetsPtrVector &PatchesOffsets) {
1211 using Encoding = DWARFExpression::Operation::Encoding;
1212
1213 DWARFUnit &OrigUnit = getOrigUnit();
1214 uint8_t OrigAddressByteSize = OrigUnit.getAddressByteSize();
1215
1216 uint64_t OpOffset = 0;
1217 for (auto &Op : InputExpression) {
1218 if (Op.isError()) {
1219 // The operation could not be decoded, so neither it nor anything after
1220 // it can be located. Its end offset is the offset it started at, so the
1221 // slice copied below would be empty and the rest of the expression
1222 // would be silently dropped. Preserve the remaining bytes instead.
1223 warn("cannot decode a DW_OP, copying the rest of the expression "
1224 "unmodified.");
1225 StringRef Bytes = InputExpression.getData().substr(OpOffset);
1226 OutputExpression.append(Bytes.begin(), Bytes.end());
1227 return;
1228 }
1229 auto Desc = Op.getDescription();
1230 // DW_OP_const_type is variable-length and has 3
1231 // operands. Thus far we only support 2.
1232 if ((Desc.Op.size() == 2 && Desc.Op[0] == Encoding::BaseTypeRef) ||
1233 (Desc.Op.size() == 2 && Desc.Op[1] == Encoding::BaseTypeRef &&
1234 Desc.Op[0] != Encoding::Size1))
1235 warn("unsupported DW_OP encoding.");
1236
1237 if ((Desc.Op.size() == 1 && Desc.Op[0] == Encoding::BaseTypeRef) ||
1238 (Desc.Op.size() == 2 && Desc.Op[1] == Encoding::BaseTypeRef &&
1239 Desc.Op[0] == Encoding::Size1)) {
1240 // This code assumes that the other non-typeref operand fits into 1 byte.
1241 assert(OpOffset < Op.getEndOffset());
1242 uint32_t ULEBsize = Op.getEndOffset() - OpOffset - 1;
1243 assert(ULEBsize <= 16);
1244
1245 // Copy over the operation.
1246 assert(!Op.getSubCode() && "SubOps not yet supported");
1247 OutputExpression.push_back(Op.getCode());
1248 uint64_t RefOffset;
1249 if (Desc.Op.size() == 1) {
1250 RefOffset = Op.getRawOperand(0);
1251 } else {
1252 OutputExpression.push_back(Op.getRawOperand(0));
1253 RefOffset = Op.getRawOperand(1);
1254 }
1255 uint8_t ULEB[16];
1256 uint32_t Offset = 0;
1257 unsigned RealSize = 0;
1258 // Look up the base type. For DW_OP_convert, the operand may be 0 to
1259 // instead indicate the generic type. The same holds for
1260 // DW_OP_reinterpret, which is currently not supported.
1261 if (RefOffset > 0 || Op.getCode() != dwarf::DW_OP_convert) {
1262 RefOffset += OrigUnit.getOffset();
1263 uint32_t RefDieIdx = 0;
1264 if (std::optional<uint32_t> Idx =
1265 OrigUnit.getDIEIndexForOffset(RefOffset))
1266 RefDieIdx = *Idx;
1267
1268 // Use fixed size for ULEB128 data, since we need to update that size
1269 // later with the proper offsets. Use 5 for DWARF32, 9 for DWARF64.
1270 ULEBsize = getFormParams().getDwarfOffsetByteSize() + 1;
1271
1272 RealSize = encodeULEB128(0xBADDEF, ULEB, ULEBsize);
1273
1274 Section.notePatchWithOffsetUpdate(
1275 DebugULEB128DieRefPatch(OutputExpression.size(), this, this,
1276 RefDieIdx),
1277 PatchesOffsets);
1278 } else
1279 RealSize = encodeULEB128(Offset, ULEB, ULEBsize);
1280
1281 if (RealSize > ULEBsize) {
1282 // Emit the generic type as a fallback.
1283 RealSize = encodeULEB128(0, ULEB, ULEBsize);
1284 warn("base type ref doesn't fit.");
1285 }
1286 assert(RealSize == ULEBsize && "padding failed");
1287 ArrayRef<uint8_t> ULEBbytes(ULEB, ULEBsize);
1288 OutputExpression.append(ULEBbytes.begin(), ULEBbytes.end());
1289 } else if (!getGlobalData().getOptions().UpdateIndexTablesOnly &&
1290 Op.getCode() == dwarf::DW_OP_addrx) {
1291 if (std::optional<object::SectionedAddress> SA =
1292 OrigUnit.getAddrOffsetSectionItem(Op.getRawOperand(0))) {
1293 // DWARFLinker does not use addrx forms since it generates relocated
1294 // addresses. Replace DW_OP_addrx with DW_OP_addr here.
1295 // Argument of DW_OP_addrx should be relocated here as it is not
1296 // processed by applyValidRelocs.
1297 OutputExpression.push_back(dwarf::DW_OP_addr);
1298 uint64_t LinkedAddress =
1299 SA->Address +
1301 .Addresses->getAddrIndexRelocAdjustment(OrigUnit, Op, false)
1302 .value_or(VarAddressAdjustment.value_or(0));
1304 sys::swapByteOrder(LinkedAddress);
1305 ArrayRef<uint8_t> AddressBytes(
1306 reinterpret_cast<const uint8_t *>(&LinkedAddress),
1307 OrigAddressByteSize);
1308 OutputExpression.append(AddressBytes.begin(), AddressBytes.end());
1309 } else
1310 warn("cann't read DW_OP_addrx operand.");
1311 } else if (!getGlobalData().getOptions().UpdateIndexTablesOnly &&
1312 Op.getCode() == dwarf::DW_OP_constx) {
1313 if (std::optional<object::SectionedAddress> SA =
1314 OrigUnit.getAddrOffsetSectionItem(Op.getRawOperand(0))) {
1315 // DWARFLinker does not use constx forms since it generates relocated
1316 // addresses. Replace DW_OP_constx with DW_OP_const[*]u here.
1317 // Argument of DW_OP_constx should be relocated here as it is not
1318 // processed by applyValidRelocs.
1319 std::optional<uint8_t> OutOperandKind;
1320 switch (OrigAddressByteSize) {
1321 case 2:
1322 OutOperandKind = dwarf::DW_OP_const2u;
1323 break;
1324 case 4:
1325 OutOperandKind = dwarf::DW_OP_const4u;
1326 break;
1327 case 8:
1328 OutOperandKind = dwarf::DW_OP_const8u;
1329 break;
1330 default:
1331 warn(
1332 formatv(("unsupported address size: {0}."), OrigAddressByteSize));
1333 break;
1334 }
1335
1336 if (OutOperandKind) {
1337 OutputExpression.push_back(*OutOperandKind);
1338 uint64_t LinkedAddress =
1339 SA->Address +
1341 .Addresses->getAddrIndexRelocAdjustment(OrigUnit, Op, false)
1342 .value_or(VarAddressAdjustment.value_or(0));
1344 sys::swapByteOrder(LinkedAddress);
1345 ArrayRef<uint8_t> AddressBytes(
1346 reinterpret_cast<const uint8_t *>(&LinkedAddress),
1347 OrigAddressByteSize);
1348 OutputExpression.append(AddressBytes.begin(), AddressBytes.end());
1349 }
1350 } else
1351 warn("cann't read DW_OP_constx operand.");
1352 } else {
1353 // Copy over everything else unmodified.
1354 StringRef Bytes =
1355 InputExpression.getData().slice(OpOffset, Op.getEndOffset());
1356 OutputExpression.append(Bytes.begin(), Bytes.end());
1357 }
1358 OpOffset = Op.getEndOffset();
1359 }
1360}
1361
1363 std::optional<std::reference_wrapper<const Triple>> TargetTriple,
1364 TypeUnit *ArtificialTypeUnit) {
1365 BumpPtrAllocator Allocator;
1366
1367 DWARFDie OrigUnitDIE = getOrigUnit().getUnitDIE();
1368 if (!OrigUnitDIE.isValid())
1369 return Error::success();
1370
1371 TypeEntry *RootEntry = nullptr;
1372 if (ArtificialTypeUnit)
1373 RootEntry = ArtificialTypeUnit->getTypePool().getRoot();
1374
1375 // Clone input DIE entry recursively.
1376 std::pair<DIE *, TypeEntry *> OutCUDie = cloneDIE(
1377 OrigUnitDIE.getDebugInfoEntry(), RootEntry, getDebugInfoHeaderSize(),
1378 std::nullopt, std::nullopt, Allocator, ArtificialTypeUnit);
1379 setOutUnitDIE(OutCUDie.first);
1380
1381 if (!TargetTriple.has_value() || (OutCUDie.first == nullptr))
1382 return Error::success();
1383
1384 if (Error Err = cloneAndEmitLineTable((*TargetTriple).get()))
1385 return Err;
1386
1387 if (Error Err = cloneAndEmitDebugMacro())
1388 return Err;
1389
1391 if (Error Err = emitDebugInfo((*TargetTriple).get()))
1392 return Err;
1393
1394 // ASSUMPTION: .debug_info section should already be emitted at this point.
1395 // cloneAndEmitRanges & cloneAndEmitDebugLocations use .debug_info section
1396 // data.
1397
1398 if (Error Err = cloneAndEmitRanges())
1399 return Err;
1400
1402 return Err;
1403
1404 if (Error Err = emitDebugAddrSection())
1405 return Err;
1406
1407 // Generate Pub accelerator tables.
1408 if (llvm::is_contained(GlobalData.getOptions().AccelTables,
1411
1413 return Err;
1414
1415 return emitAbbreviations();
1416}
1417
1418std::pair<DIE *, TypeEntry *> CompileUnit::cloneDIE(
1419 const DWARFDebugInfoEntry *InputDieEntry, TypeEntry *ClonedParentTypeDIE,
1420 uint64_t OutOffset, std::optional<int64_t> FuncAddressAdjustment,
1421 std::optional<int64_t> VarAddressAdjustment, BumpPtrAllocator &Allocator,
1422 TypeUnit *ArtificialTypeUnit, uint32_t SiblingOrdinal) {
1423 uint32_t InputDieIdx = getDIEIndex(InputDieEntry);
1424 CompileUnit::DIEInfo &Info = getDIEInfo(InputDieIdx);
1425
1426 bool NeedToClonePlainDIE = Info.needToKeepInPlainDwarf();
1427 bool NeedToCloneTypeDIE =
1428 (InputDieEntry->getTag() != dwarf::DW_TAG_compile_unit) &&
1429 Info.needToPlaceInTypeTable();
1430 std::pair<DIE *, TypeEntry *> ClonedDIE;
1431
1432 DIEGenerator PlainDIEGenerator(Allocator, *this);
1433
1434 if (NeedToClonePlainDIE)
1435 // Create a cloned DIE which would be placed into the cloned version
1436 // of input compile unit.
1437 ClonedDIE.first = createPlainDIEandCloneAttributes(
1438 InputDieEntry, PlainDIEGenerator, OutOffset, FuncAddressAdjustment,
1439 VarAddressAdjustment);
1440 if (NeedToCloneTypeDIE) {
1441 // Create a cloned DIE which would be placed into the artificial type
1442 // unit.
1443 assert(ArtificialTypeUnit != nullptr);
1444 DIEGenerator TypeDIEGenerator(
1445 ArtificialTypeUnit->getTypePool().getThreadLocalAllocator(), *this);
1446
1447 ClonedDIE.second = createTypeDIEandCloneAttributes(
1448 InputDieEntry, TypeDIEGenerator, ClonedParentTypeDIE,
1449 ArtificialTypeUnit, SiblingOrdinal);
1450 }
1451 TypeEntry *TypeParentForChild =
1452 ClonedDIE.second ? ClonedDIE.second : ClonedParentTypeDIE;
1453
1454 bool HasPlainChildrenToClone =
1455 (ClonedDIE.first && Info.getKeepPlainChildren());
1456
1457 bool HasTypeChildrenToClone =
1458 ((ClonedDIE.second ||
1459 InputDieEntry->getTag() == dwarf::DW_TAG_compile_unit) &&
1460 Info.getKeepTypeChildren());
1461
1462 // Recursively clone children.
1463 if (HasPlainChildrenToClone || HasTypeChildrenToClone) {
1464 uint32_t ChildOrdinal = 0;
1465 for (const DWARFDebugInfoEntry *CurChild =
1466 getFirstChildEntry(InputDieEntry);
1467 CurChild && CurChild->getAbbreviationDeclarationPtr();
1468 CurChild = getSiblingEntry(CurChild), ++ChildOrdinal) {
1469 std::pair<DIE *, TypeEntry *> ClonedChild = cloneDIE(
1470 CurChild, TypeParentForChild, OutOffset, FuncAddressAdjustment,
1471 VarAddressAdjustment, Allocator, ArtificialTypeUnit, ChildOrdinal);
1472
1473 if (ClonedChild.first) {
1474 OutOffset =
1475 ClonedChild.first->getOffset() + ClonedChild.first->getSize();
1476 PlainDIEGenerator.addChild(ClonedChild.first);
1477 }
1478 }
1479 assert(ClonedDIE.first == nullptr ||
1480 HasPlainChildrenToClone == ClonedDIE.first->hasChildren());
1481
1482 // Account for the end of children marker.
1483 if (HasPlainChildrenToClone)
1484 OutOffset += sizeof(int8_t);
1485 }
1486
1487 // Update our size.
1488 if (ClonedDIE.first != nullptr)
1489 ClonedDIE.first->setSize(OutOffset - ClonedDIE.first->getOffset());
1490
1491 return ClonedDIE;
1492}
1493
1494DIE *CompileUnit::createPlainDIEandCloneAttributes(
1495 const DWARFDebugInfoEntry *InputDieEntry, DIEGenerator &PlainDIEGenerator,
1496 uint64_t &OutOffset, std::optional<int64_t> &FuncAddressAdjustment,
1497 std::optional<int64_t> &VarAddressAdjustment) {
1498 uint32_t InputDieIdx = getDIEIndex(InputDieEntry);
1499 CompileUnit::DIEInfo &Info = getDIEInfo(InputDieIdx);
1500 DIE *ClonedDIE = nullptr;
1501 bool HasLocationExpressionAddress = false;
1502 if (InputDieEntry->getTag() == dwarf::DW_TAG_subprogram) {
1503 // Get relocation adjustment value for the current function.
1504 FuncAddressAdjustment =
1505 getContainingFile().Addresses->getSubprogramRelocAdjustment(
1506 getDIE(InputDieEntry), false);
1507 } else if (InputDieEntry->getTag() == dwarf::DW_TAG_label) {
1508 // Get relocation adjustment value for the current label.
1509 std::optional<uint64_t> lowPC =
1510 dwarf::toAddress(find(InputDieEntry, dwarf::DW_AT_low_pc));
1511 if (lowPC) {
1512 LabelMapTy::iterator It = Labels.find(*lowPC);
1513 if (It != Labels.end())
1514 FuncAddressAdjustment = It->second;
1515 }
1516 } else if (InputDieEntry->getTag() == dwarf::DW_TAG_variable) {
1517 // Get relocation adjustment value for the current variable.
1518 std::pair<bool, std::optional<int64_t>> LocExprAddrAndRelocAdjustment =
1519 getContainingFile().Addresses->getVariableRelocAdjustment(
1520 getDIE(InputDieEntry), false);
1521
1522 HasLocationExpressionAddress = LocExprAddrAndRelocAdjustment.first;
1523 if (LocExprAddrAndRelocAdjustment.first &&
1524 LocExprAddrAndRelocAdjustment.second)
1525 VarAddressAdjustment = *LocExprAddrAndRelocAdjustment.second;
1526 }
1527
1528 ClonedDIE = PlainDIEGenerator.createDIE(InputDieEntry->getTag(), OutOffset);
1529
1530 // Offset to the DIE would be used after output DIE tree is deleted.
1531 // Thus we need to remember DIE offset separately.
1532 rememberDieOutOffset(InputDieIdx, OutOffset);
1533
1534 // Clone Attributes.
1535 DIEAttributeCloner AttributesCloner(ClonedDIE, *this, this, InputDieEntry,
1536 PlainDIEGenerator, FuncAddressAdjustment,
1537 VarAddressAdjustment,
1538 HasLocationExpressionAddress);
1539 AttributesCloner.clone();
1540
1541 // Remember accelerator info.
1542 AcceleratorRecordsSaver AccelRecordsSaver(getGlobalData(), *this, this);
1543 AccelRecordsSaver.save(InputDieEntry, ClonedDIE, AttributesCloner.AttrInfo,
1544 nullptr);
1545
1546 OutOffset =
1547 AttributesCloner.finalizeAbbreviations(Info.getKeepPlainChildren());
1548
1549 return ClonedDIE;
1550}
1551
1552/// Allocates output DIE for the specified \p TypeDescriptor.
1553DIE *CompileUnit::allocateTypeDie(TypeEntryBody *TypeDescriptor,
1554 DIEGenerator &TypeDIEGenerator,
1555 dwarf::Tag DieTag, bool IsDeclaration,
1556 bool IsParentDeclaration) {
1557 uint64_t Priority = getPriority();
1558
1559 // Lock-free pre-checks: skip the lock (and downstream cloning) when this CU
1560 // has no chance of winning the type slot.
1561 if (!IsDeclaration && !IsParentDeclaration) {
1562 // DiePriority only ever decreases, so a relaxed read that is <= our
1563 // priority means we definitely cannot win.
1564 if (Priority >= TypeDescriptor->DiePriority.load(std::memory_order_relaxed))
1565 return nullptr;
1566 } else {
1567 // Once a definition exists the declaration slot is dead.
1568 if (TypeDescriptor->Die.load(std::memory_order_relaxed))
1569 return nullptr;
1570 }
1571
1572 while (TypeDescriptor->Lock.test_and_set(std::memory_order_acquire))
1573 ; // spin
1574
1575 DIE *Result = nullptr;
1576
1577 if (!IsDeclaration && !IsParentDeclaration) {
1578 // Definition: lowest priority wins.
1579 if (Priority <
1580 TypeDescriptor->DiePriority.load(std::memory_order_relaxed)) {
1581 TypeDescriptor->DiePriority.store(Priority, std::memory_order_relaxed);
1582 Result = TypeDIEGenerator.createDIE(DieTag, 0);
1583 TypeDescriptor->Die.store(Result, std::memory_order_relaxed);
1584 }
1585 } else if (!TypeDescriptor->Die.load(std::memory_order_relaxed)) {
1586 // Declaration (no definition exists yet).
1587 // Prefer declarations whose parent is a definition (better context);
1588 // break ties by CU priority (lower wins).
1589 bool WorseParent =
1590 IsParentDeclaration && !TypeDescriptor->DeclarationParentIsDeclaration;
1591 bool BetterParent =
1592 !IsParentDeclaration && TypeDescriptor->DeclarationParentIsDeclaration;
1593 if (!WorseParent &&
1594 (BetterParent || Priority < TypeDescriptor->DeclarationDiePriority)) {
1595 TypeDescriptor->DeclarationDiePriority = Priority;
1596 TypeDescriptor->DeclarationParentIsDeclaration = IsParentDeclaration;
1597 Result = TypeDIEGenerator.createDIE(DieTag, 0);
1598 TypeDescriptor->DeclarationDie.store(Result, std::memory_order_relaxed);
1599 }
1600 }
1601
1602 TypeDescriptor->Lock.clear(std::memory_order_release);
1603 return Result;
1604}
1605
1606TypeEntry *CompileUnit::createTypeDIEandCloneAttributes(
1607 const DWARFDebugInfoEntry *InputDieEntry, DIEGenerator &TypeDIEGenerator,
1608 TypeEntry *ClonedParentTypeDIE, TypeUnit *ArtificialTypeUnit,
1609 uint32_t SiblingOrdinal) {
1610 assert(ArtificialTypeUnit != nullptr);
1611 uint32_t InputDieIdx = getDIEIndex(InputDieEntry);
1612
1613 TypeEntry *Entry = getDieTypeEntry(InputDieIdx);
1614 assert(Entry != nullptr);
1615 assert(ClonedParentTypeDIE != nullptr);
1616 TypeEntryBody *EntryBody =
1617 ArtificialTypeUnit->getTypePool().getOrCreateTypeEntryBody(
1618 Entry, ClonedParentTypeDIE);
1619 assert(EntryBody);
1620
1621 // Min-merge this child's ordinal in its parent's child list so children of
1622 // record-like types (class/struct/union/interface) sort in source order.
1623 // Min across CUs because Clang appends template instantiations lazily, so
1624 // positions vary between CUs.
1625 if (std::optional<uint32_t> ParentIdx = InputDieEntry->getParentIdx()) {
1626 dwarf::Tag ParentTag = getDebugInfoEntry(*ParentIdx)->getTag();
1627 if (ParentTag == dwarf::DW_TAG_structure_type ||
1628 ParentTag == dwarf::DW_TAG_class_type ||
1629 ParentTag == dwarf::DW_TAG_union_type ||
1630 ParentTag == dwarf::DW_TAG_interface_type) {
1631 uint32_t Prev = EntryBody->SortKey.load(std::memory_order_relaxed);
1632 while (SiblingOrdinal < Prev &&
1633 !EntryBody->SortKey.compare_exchange_weak(
1634 Prev, SiblingOrdinal, std::memory_order_relaxed,
1635 std::memory_order_relaxed))
1636 ;
1637 }
1638 }
1639
1640 bool IsDeclaration =
1641 dwarf::toUnsigned(find(InputDieEntry, dwarf::DW_AT_declaration), 0);
1642
1643 bool ParentIsDeclaration = false;
1644 if (std::optional<uint32_t> ParentIdx = InputDieEntry->getParentIdx())
1645 ParentIsDeclaration =
1646 dwarf::toUnsigned(find(*ParentIdx, dwarf::DW_AT_declaration), 0);
1647
1648 DIE *OutDIE =
1649 allocateTypeDie(EntryBody, TypeDIEGenerator, InputDieEntry->getTag(),
1650 IsDeclaration, ParentIsDeclaration);
1651
1652 if (OutDIE != nullptr) {
1653 assert(ArtificialTypeUnit != nullptr);
1655
1656 DIEAttributeCloner AttributesCloner(OutDIE, *this, ArtificialTypeUnit,
1657 InputDieEntry, TypeDIEGenerator,
1658 std::nullopt, std::nullopt, false);
1659 AttributesCloner.clone();
1660
1661 // Remember accelerator info.
1662 AcceleratorRecordsSaver AccelRecordsSaver(getGlobalData(), *this,
1663 ArtificialTypeUnit);
1664 AccelRecordsSaver.save(InputDieEntry, OutDIE, AttributesCloner.AttrInfo,
1665 Entry);
1666
1667 // if AttributesCloner.getOutOffset() == 0 then we need to add
1668 // 1 to avoid assertion for zero size. We will subtract it back later.
1669 OutDIE->setSize(AttributesCloner.getOutOffset() + 1);
1670 }
1671
1672 return Entry;
1673}
1674
1676 const DWARFDebugLine::LineTable *InputLineTable =
1677 getContainingFile().Dwarf->getLineTableForUnit(&getOrigUnit());
1678 if (InputLineTable == nullptr) {
1679 if (getOrigUnit().getUnitDIE().find(dwarf::DW_AT_stmt_list))
1680 warn("cann't load line table.");
1681 return Error::success();
1682 }
1683
1684 DWARFDebugLine::LineTable OutLineTable;
1685
1686 // Set Line Table header.
1687 OutLineTable.Prologue = InputLineTable->Prologue;
1689
1690 // Set Line Table Rows.
1691 if (getGlobalData().getOptions().UpdateIndexTablesOnly) {
1692 OutLineTable.Rows = InputLineTable->Rows;
1693 // If all the line table contains is a DW_LNE_end_sequence, clear the line
1694 // table rows, it will be inserted again in the DWARFStreamer.
1695 if (OutLineTable.Rows.size() == 1 && OutLineTable.Rows[0].EndSequence)
1696 OutLineTable.Rows.clear();
1697
1698 OutLineTable.Sequences = InputLineTable->Sequences;
1699 return emitDebugLine(TargetTriple, OutLineTable);
1700 }
1701
1702 SmallVector<uint64_t> OrigRowIndices;
1703 filterLineTableRows(*InputLineTable, OutLineTable.Rows, OrigRowIndices);
1704
1705 if (StmtSeqListAttributes.empty())
1706 return emitDebugLine(TargetTriple, OutLineTable);
1707
1708 // When DW_AT_LLVM_stmt_sequence attributes on this CU need their values
1709 // rewritten to point at the correct output sequence, have the emitter
1710 // record, for every row that originated from an input row, the byte
1711 // offset of the DW_LNE_set_address that opens the sequence containing
1712 // that row. Keying the map on the input row index (rather than on an
1713 // output address) avoids collisions when two input sequences would
1714 // relocate to the same output address — e.g. ICF folding two functions
1715 // from the same CU to a single output range.
1716 //
1717 // The patching below MUST run before emitDebugInfo() serializes the
1718 // DIE bytes and before OutputSections::applyPatches() runs for this
1719 // unit's .debug_info — it writes a local offset into the DIEValue that
1720 // the serializer then emits, and a DebugOffsetPatch (registered at DIE
1721 // cloning time) later adds the CU's .debug_line start offset to reach
1722 // the final absolute value.
1723 DenseMap<uint64_t, uint64_t> RowIndexToSeqStartOffset;
1724 if (Error Err = emitDebugLine(TargetTriple, OutLineTable, OrigRowIndices,
1725 &RowIndexToSeqStartOffset))
1726 return Err;
1727
1728 DenseMap<uint64_t, uint64_t> SeqOffsetToFirstRowIndex =
1729 buildStmtSeqOffsetToFirstRowIndex(*InputLineTable);
1730 patchStmtSeqAttributes(SeqOffsetToFirstRowIndex, RowIndexToSeqStartOffset);
1731 return Error::success();
1732}
1733
1734void CompileUnit::filterLineTableRows(
1735 const DWARFDebugLine::LineTable &InputLineTable,
1736 std::vector<DWARFDebugLine::Row> &NewRows,
1737 SmallVectorImpl<uint64_t> &NewRowIndices) {
1738 NewRows.reserve(InputLineTable.Rows.size());
1739 NewRowIndices.reserve(InputLineTable.Rows.size());
1740
1741 // Current sequence of rows being extracted, before being inserted
1742 // in NewRows. Kept in lockstep with SeqIndices, which stores the
1743 // originating input row index (or InvalidRowIndex for manufactured
1744 // end-of-range rows).
1745 std::vector<DWARFDebugLine::Row> Seq;
1746 SmallVector<uint64_t> SeqIndices;
1747 constexpr uint64_t InvalidRowIndex = std::numeric_limits<uint64_t>::max();
1748
1749 const auto &FunctionRanges = getFunctionRanges();
1750 std::optional<AddressRangeValuePair> CurrRange;
1751
1752 // FIXME: This logic is meant to generate exactly the same output as
1753 // Darwin's classic dsymutil. There is a nicer way to implement this
1754 // by simply putting all the relocated line info in NewRows and simply
1755 // sorting NewRows before passing it to emitLineTableForUnit. This
1756 // should be correct as sequences for a function should stay
1757 // together in the sorted output. There are a few corner cases that
1758 // look suspicious though, and that required to implement the logic
1759 // this way. Revisit that once initial validation is finished.
1760
1761 // Iterate over the object file line info and extract the sequences
1762 // that correspond to linked functions.
1763 for (auto [InputRowIdx, InputRow] : llvm::enumerate(InputLineTable.Rows)) {
1764 DWARFDebugLine::Row Row = InputRow;
1765 // Check whether we stepped out of the range. The range is
1766 // half-open, but consider accept the end address of the range if
1767 // it is marked as end_sequence in the input (because in that
1768 // case, the relocation offset is accurate and that entry won't
1769 // serve as the start of another function).
1770 if (!CurrRange || !CurrRange->Range.contains(Row.Address.Address)) {
1771 // We just stepped out of a known range. Insert a end_sequence
1772 // corresponding to the end of the range.
1773 uint64_t StopAddress =
1774 CurrRange ? CurrRange->Range.end() + CurrRange->Value : -1ULL;
1775 CurrRange = FunctionRanges.getRangeThatContains(Row.Address.Address);
1776 if (StopAddress != -1ULL && !Seq.empty()) {
1777 // Insert end sequence row with the computed end address, but
1778 // the same line as the previous one. This row is synthesised
1779 // and has no input counterpart, so tag it with
1780 // InvalidRowIndex.
1781 auto NextLine = Seq.back();
1782 NextLine.Address.Address = StopAddress;
1783 NextLine.EndSequence = 1;
1784 NextLine.PrologueEnd = 0;
1785 NextLine.BasicBlock = 0;
1786 NextLine.EpilogueBegin = 0;
1787 Seq.push_back(NextLine);
1788 SeqIndices.push_back(InvalidRowIndex);
1789 insertLineSequence(Seq, SeqIndices, NewRows, NewRowIndices);
1790 }
1791
1792 if (!CurrRange)
1793 continue;
1794 }
1795
1796 // Ignore empty sequences.
1797 if (Row.EndSequence && Seq.empty())
1798 continue;
1799
1800 // Relocate row address and add it to the current sequence.
1801 Row.Address.Address += CurrRange->Value;
1802 Seq.emplace_back(Row);
1803 SeqIndices.push_back(InputRowIdx);
1804
1805 if (Row.EndSequence)
1806 insertLineSequence(Seq, SeqIndices, NewRows, NewRowIndices);
1807 }
1808}
1809
1810void CompileUnit::patchStmtSeqAttributes(
1811 const DenseMap<uint64_t, uint64_t> &SeqOffsetToFirstRowIndex,
1812 const DenseMap<uint64_t, uint64_t> &RowIndexToSeqStartOffset) {
1813 const uint64_t InvalidOffset = getFormParams().getDwarfMaxOffset();
1814
1815 for (const CompileUnit::StmtSeqPatch &Patch : StmtSeqListAttributes) {
1816 uint64_t NewStmtSeq = InvalidOffset;
1817 auto RowIt = SeqOffsetToFirstRowIndex.find(Patch.InputStmtSeqOffset);
1818 if (RowIt != SeqOffsetToFirstRowIndex.end()) {
1819 auto OffIt = RowIndexToSeqStartOffset.find(RowIt->second);
1820 if (OffIt != RowIndexToSeqStartOffset.end())
1821 NewStmtSeq = OffIt->second;
1822 }
1823 // When resolution fails, the InvalidOffset sentinel must survive the
1824 // combination-time section-offset fixup. The patch applier preserves
1825 // InvalidOffset as-is so consumers see a clean invalid marker rather
1826 // than StartOffset - 1.
1827 *Patch.Value = DIEValue(Patch.Value->getAttribute(), Patch.Value->getForm(),
1828 DIEInteger(NewStmtSeq));
1829 }
1830}
1831
1832DenseMap<uint64_t, uint64_t> CompileUnit::buildStmtSeqOffsetToFirstRowIndex(
1833 const DWARFDebugLine::LineTable &InputLineTable) const {
1834 // Collect this CU's stmt-sequence attribute values (input offsets),
1835 // sorted ascending and deduplicated.
1836 SmallVector<uint64_t> StmtAttrs;
1837 StmtAttrs.reserve(StmtSeqListAttributes.size());
1838 for (const StmtSeqPatch &Patch : StmtSeqListAttributes)
1839 StmtAttrs.push_back(Patch.InputStmtSeqOffset);
1840 llvm::sort(StmtAttrs);
1841 StmtAttrs.erase(llvm::unique(StmtAttrs), StmtAttrs.end());
1842
1843 DenseMap<uint64_t, uint64_t> Result;
1844 dwarf_linker::buildStmtSeqOffsetToFirstRowIndex(InputLineTable, StmtAttrs,
1845 Result);
1846 return Result;
1847}
1848
1849void CompileUnit::insertLineSequence(std::vector<DWARFDebugLine::Row> &Seq,
1850 SmallVectorImpl<uint64_t> &SeqIndices,
1851 std::vector<DWARFDebugLine::Row> &Rows,
1852 SmallVectorImpl<uint64_t> &RowIndices) {
1853 assert(Seq.size() == SeqIndices.size() &&
1854 "Seq and SeqIndices must be kept in lockstep");
1855 assert(Rows.size() == RowIndices.size() &&
1856 "Rows and RowIndices must be kept in lockstep");
1857 if (Seq.empty())
1858 return;
1859
1860 auto ClearSeq = [&] {
1861 Seq.clear();
1862 SeqIndices.clear();
1863 };
1864
1865 if (!Rows.empty() && Rows.back().Address < Seq.front().Address) {
1866 llvm::append_range(Rows, Seq);
1867 llvm::append_range(RowIndices, SeqIndices);
1868 ClearSeq();
1869 return;
1870 }
1871
1872 object::SectionedAddress Front = Seq.front().Address;
1873 auto InsertPoint = partition_point(
1874 Rows, [=](const DWARFDebugLine::Row &O) { return O.Address < Front; });
1875 size_t InsertIdx = std::distance(Rows.begin(), InsertPoint);
1876
1877 // FIXME: this only removes the unneeded end_sequence if the
1878 // sequences have been inserted in order. Using a global sort like
1879 // described in cloneAndEmitLineTable() and delaying the end_sequene
1880 // elimination to DebugLineEmitter::emit() we can get rid of all of them.
1881 if (InsertPoint != Rows.end() && InsertPoint->Address == Front &&
1882 InsertPoint->EndSequence) {
1883 *InsertPoint = Seq.front();
1884 RowIndices[InsertIdx] = SeqIndices.front();
1885 Rows.insert(InsertPoint + 1, Seq.begin() + 1, Seq.end());
1886 RowIndices.insert(RowIndices.begin() + InsertIdx + 1,
1887 SeqIndices.begin() + 1, SeqIndices.end());
1888 } else {
1889 Rows.insert(InsertPoint, Seq.begin(), Seq.end());
1890 RowIndices.insert(RowIndices.begin() + InsertIdx, SeqIndices.begin(),
1891 SeqIndices.end());
1892 }
1893
1894 ClearSeq();
1895}
1896
1897#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1899 llvm::errs() << "{";
1900 llvm::errs() << " Placement: ";
1901 switch (getPlacement()) {
1902 case NotSet:
1903 llvm::errs() << "NotSet";
1904 break;
1905 case TypeTable:
1906 llvm::errs() << "TypeTable";
1907 break;
1908 case PlainDwarf:
1909 llvm::errs() << "PlainDwarf";
1910 break;
1911 case Both:
1912 llvm::errs() << "Both";
1913 break;
1914 }
1915
1916 llvm::errs() << " Keep: " << getKeep();
1917 llvm::errs() << " KeepPlainChildren: " << getKeepPlainChildren();
1918 llvm::errs() << " KeepTypeChildren: " << getKeepTypeChildren();
1919 llvm::errs() << " IsInMouduleScope: " << getIsInMouduleScope();
1920 llvm::errs() << " IsInFunctionScope: " << getIsInFunctionScope();
1921 llvm::errs() << " IsInAnonNamespaceScope: " << getIsInAnonNamespaceScope();
1922 llvm::errs() << " ODRAvailable: " << getODRAvailable();
1923 llvm::errs() << " TrackLiveness: " << getTrackLiveness();
1924 llvm::errs() << "}\n";
1925}
1926#endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1927
1928std::optional<std::pair<StringRef, StringRef>>
1930 const DWARFFormValue &FileIdxValue) {
1931 uint64_t FileIdx;
1932 if (std::optional<uint64_t> Val = FileIdxValue.getAsUnsignedConstant())
1933 FileIdx = *Val;
1934 else if (std::optional<int64_t> Val = FileIdxValue.getAsSignedConstant())
1935 FileIdx = *Val;
1936 else if (std::optional<uint64_t> Val = FileIdxValue.getAsSectionOffset())
1937 FileIdx = *Val;
1938 else
1939 return std::nullopt;
1940
1941 return getDirAndFilenameFromLineTable(FileIdx);
1942}
1943
1944std::optional<std::pair<StringRef, StringRef>>
1946 std::lock_guard<std::mutex> Guard(FileNamesMutex);
1947 FileNamesCache::iterator FileData = FileNames.find(FileIdx);
1948 if (FileData != FileNames.end())
1949 return {{StringRef(FileData->second->first),
1950 StringRef(FileData->second->second)}};
1951
1952 if (const DWARFDebugLine::LineTable *LineTable =
1953 getOrigUnit().getContext().getLineTableForUnit(&getOrigUnit())) {
1954 if (LineTable->hasFileAtIndex(FileIdx)) {
1955
1957 LineTable->Prologue.getFileNameEntry(FileIdx);
1958
1959 Expected<const char *> Name = Entry.Name.getAsCString();
1960 if (!Name) {
1961 warn(Name.takeError());
1962 return std::nullopt;
1963 }
1964
1965 std::string FileName = *Name;
1966 if (isPathAbsoluteOnWindowsOrPosix(FileName)) {
1967 FileNamesCache::iterator FileData =
1968 FileNames
1969 .insert({FileIdx,
1970 std::make_unique<std::pair<std::string, std::string>>(
1971 std::string(""), std::move(FileName))})
1972 .first;
1973 return {{StringRef(FileData->second->first),
1974 StringRef(FileData->second->second)}};
1975 }
1976
1977 SmallString<256> FilePath;
1978 StringRef IncludeDir;
1979 // Be defensive about the contents of Entry.
1980 if (getVersion() >= 5) {
1981 // DirIdx 0 is the compilation directory, so don't include it for
1982 // relative names.
1983 if ((Entry.DirIdx != 0) &&
1984 Entry.DirIdx < LineTable->Prologue.IncludeDirectories.size()) {
1985 Expected<const char *> DirName =
1986 LineTable->Prologue.IncludeDirectories[Entry.DirIdx]
1987 .getAsCString();
1988 if (DirName)
1989 IncludeDir = *DirName;
1990 else {
1991 warn(DirName.takeError());
1992 return std::nullopt;
1993 }
1994 }
1995 } else {
1996 if (0 < Entry.DirIdx &&
1997 Entry.DirIdx <= LineTable->Prologue.IncludeDirectories.size()) {
1998 Expected<const char *> DirName =
1999 LineTable->Prologue.IncludeDirectories[Entry.DirIdx - 1]
2000 .getAsCString();
2001 if (DirName)
2002 IncludeDir = *DirName;
2003 else {
2004 warn(DirName.takeError());
2005 return std::nullopt;
2006 }
2007 }
2008 }
2009
2011
2012 if (!CompDir.empty() && !isPathAbsoluteOnWindowsOrPosix(IncludeDir)) {
2013 sys::path::append(FilePath, sys::path::Style::native, CompDir);
2014 }
2015
2016 sys::path::append(FilePath, sys::path::Style::native, IncludeDir);
2017
2018 FileNamesCache::iterator FileData =
2019 FileNames
2020 .insert({FileIdx,
2021 std::make_unique<std::pair<std::string, std::string>>(
2022 std::string(FilePath), std::move(FileName))})
2023 .first;
2024 return {{StringRef(FileData->second->first),
2025 StringRef(FileData->second->second)}};
2026 }
2027 }
2028
2029 return std::nullopt;
2030}
2031
2032#define MAX_REFERENCIES_DEPTH 1000
2034 UnitEntryPairTy CUDiePair(*this);
2035 std::optional<UnitEntryPairTy> RefDiePair;
2036 int refDepth = 0;
2037 do {
2038 RefDiePair = CUDiePair.CU->resolveDIEReference(
2039 CUDiePair.DieEntry, dwarf::DW_AT_extension,
2041 if (!RefDiePair || !RefDiePair->DieEntry)
2042 return CUDiePair;
2043
2044 CUDiePair = *RefDiePair;
2045 } while (refDepth++ < MAX_REFERENCIES_DEPTH);
2046
2047 return CUDiePair;
2048}
2049
2050std::optional<UnitEntryPairTy> UnitEntryPairTy::getParent() {
2051 if (std::optional<uint32_t> ParentIdx = DieEntry->getParentIdx())
2052 return UnitEntryPairTy{CU, CU->getDebugInfoEntry(*ParentIdx)};
2053
2054 return std::nullopt;
2055}
2056
2061
2065
2067 if (isCompileUnit())
2068 return getAsCompileUnit();
2069 else
2070 return getAsTypeUnit();
2071}
2072
2076
2080
2084
2088
2090 bool InterCUProcessingStarted, std::atomic<bool> &HasNewInterconnectedCUs) {
2091 if (!Dependencies)
2092 Dependencies.reset(new DependencyTracker(*this));
2093
2094 return Dependencies->resolveDependenciesAndMarkLiveness(
2095 InterCUProcessingStarted, HasNewInterconnectedCUs);
2096}
2097
2099 assert(Dependencies.get());
2100
2101 return Dependencies->updateDependenciesCompleteness();
2102}
2103
2105 assert(Dependencies.get());
2106
2107 Dependencies->verifyKeepChain();
2108}
2109
2111 static dwarf::Attribute ODRAttributes[] = {
2112 dwarf::DW_AT_type, dwarf::DW_AT_specification,
2113 dwarf::DW_AT_abstract_origin, dwarf::DW_AT_import,
2114 dwarf::DW_AT_LLVM_alloc_type};
2115
2116 return ODRAttributes;
2117}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
static const IROptions & getOptions(const Module *M)
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:682
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define MAX_REFERENCIES_DEPTH
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
std::optional< T > getRangeThatContains(uint64_t Addr) const
void insert(AddressRange Range, int64_t Value)
The AddressRanges class helps normalize address range collections.
Collection::const_iterator insert(AddressRange Range)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:130
iterator begin() const
Definition ArrayRef.h:129
std::pair< KeyDataTy *, bool > insert(const KeyTy &NewValue)
Insert new value NewValue or return already existing entry.
A structured debug information entry.
Definition DIE.h:835
void setSize(unsigned S)
Definition DIE.h:942
DWARFDebugInfoEntry - A DIE with only the minimum required data.
std::optional< uint32_t > getParentIdx() const
Returns index of the parent die.
const DWARFAbbreviationDeclaration * getAbbreviationDeclarationPtr() const
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Definition DWARFDie.h:43
LLVM_ABI std::optional< DWARFFormValue > find(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE.
Definition DWARFDie.cpp:335
const DWARFDebugInfoEntry * getDebugInfoEntry() const
Definition DWARFDie.h:54
LLVM_ABI const char * getName(DINameKind Kind) const
Return the DIE name resolving DW_AT_specification or DW_AT_abstract_origin references if necessary.
Definition DWARFDie.cpp:560
LLVM_ABI std::optional< uint64_t > getLanguage() const
Returns the DW_LANG_ code for this DIE's DWARF unit, if it exists.
Definition DWARFDie.cpp:506
bool isValid() const
Definition DWARFDie.h:52
Encoding
Size and signedness of expression operations' operands.
StringRef getData() const
LLVM_ABI std::optional< uint64_t > getAsSectionOffset() const
LLVM_ABI std::optional< int64_t > getAsSignedConstant() const
LLVM_ABI std::optional< uint64_t > getAsRelativeReference() const
getAsFoo functions below return the extracted value as Foo if only DWARFFormValue has form class is s...
LLVM_ABI std::optional< uint64_t > getAsDebugInfoReference() const
LLVM_ABI std::optional< uint64_t > getAsUnsignedConstant() const
const DWARFUnit * getUnit() const
const dwarf::FormParams & getFormParams() const
Definition DWARFUnit.h:329
DWARFDie getUnitDIE(bool ExtractUnitDIEOnly=true)
Definition DWARFUnit.h:450
uint8_t getAddressByteSize() const
Definition DWARFUnit.h:333
const char * getCompilationDir()
Expected< DWARFLocationExpressionsVector > findLoclistFromOffset(uint64_t Offset)
bool isLittleEndian() const
Definition DWARFUnit.h:324
uint64_t getOffset() const
Definition DWARFUnit.h:328
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:766
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:679
iterator end()
Definition DenseMap.h:686
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:678
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:827
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
iterator erase(const_iterator CI)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
iterator insert(iterator I, T &&Elt)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMapIterBase< ValueTy, false > iterator
Definition StringMap.h:209
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:597
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
iterator begin() const
Definition StringRef.h:114
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition StringRef.h:720
iterator end() const
Definition StringRef.h:116
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
This class represents DWARF information for source file and it's address map.
Definition DWARFFile.h:25
std::unique_ptr< AddressesMap > Addresses
Helpful address information(list of valid address ranges, relocations).
Definition DWARFFile.h:42
std::unique_ptr< DWARFContext > Dwarf
Source DWARF information.
Definition DWARFFile.h:39
std::map< std::string, std::string > SwiftInterfacesMapTy
CompileUnit(DWARFUnit &OrigUnit, unsigned ID, bool CanUseODR, StringRef ClangModuleName)
CompileUnit * getAsCompileUnit()
Returns CompileUnit if applicable.
void addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset)
Add the low_pc of a label that is relocated by applying offset PCOffset.
Error cloneAndEmitDebugLocations()
Clone and emit debug locations(.debug_loc/.debug_loclists).
void cloneDieAttrExpression(const DWARFExpression &InputExpression, SmallVectorImpl< uint8_t > &OutputExpression, SectionDescriptor &Section, std::optional< int64_t > VarAddressAdjustment, OffsetsPtrVector &PatchesOffsets)
Clone attribute location axpression.
void maybeResetToLoadedStage()
Reset compile units data(results of liveness analysis, clonning) if current stage greater than Stage:...
void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset)
Add a function range [LowPC, HighPC) that is relocated by applying offset PCOffset.
void analyzeImportedModule(const DWARFDebugInfoEntry *DieEntry)
Collect references to parseable Swift interfaces in imported DW_TAG_module blocks.
std::pair< DIE *, TypeEntry * > cloneDIE(const DWARFDebugInfoEntry *InputDieEntry, TypeEntry *ClonedParentTypeDIE, uint64_t OutOffset, std::optional< int64_t > FuncAddressAdjustment, std::optional< int64_t > VarAddressAdjustment, BumpPtrAllocator &Allocator, TypeUnit *ArtificialTypeUnit, uint32_t SiblingOrdinal=std::numeric_limits< uint32_t >::max())
void cleanupDataAfterClonning()
Cleanup unneeded resources after compile unit is cloned.
Error assignTypeNames(TypePool &TypePoolRef)
Search for type entries and assign names.
llvm::Error setPriority(uint64_t ObjFileIdx, uint64_t LocalIdx)
Set deterministic priority for type DIE allocation ordering.
void noteModuleAnchors()
Must run while the output offsets are still available, and once they are final.
@ TypeTable
Corresponding DIE goes to the type table only.
@ PlainDwarf
Corresponding DIE goes to the plain dwarf only.
Error cloneAndEmitLineTable(const Triple &TargetTriple)
const DWARFFile & getContainingFile() const
Returns DWARFFile containing this compile unit.
void mergeSwiftInterfaces(DWARFLinkerBase::SwiftInterfacesMapTy &Map)
Merge the Swift interface entries collected by analyzeImportedModule into Map, emitting a warning for...
void updateDieRefPatchesWithClonedOffsets()
After cloning stage the output DIEs offsets are deallocated.
uint64_t getDebugAddrIndex(uint64_t Addr)
Returns index(inside .debug_addr) of an address.
bool resolveDependenciesAndMarkLiveness(bool InterCUProcessingStarted, std::atomic< bool > &HasNewInterconnectedCUs)
Search for subprograms and variables referencing live code and discover dependend DIEs.
bool updateDependenciesCompleteness()
Check dependend DIEs for incompatible placement.
bool loadInputDIEs()
Load DIEs of input compilation unit.
const RangesTy & getFunctionRanges() const
Returns function ranges of this unit.
Error cloneAndEmitDebugMacro()
Clone and emit debug macros(.debug_macinfo/.debug_macro).
Error cloneAndEmit(std::optional< std::reference_wrapper< const Triple > > TargetTriple, TypeUnit *ArtificialTypeUnit)
Clone and emit this compilation unit.
void setStage(Stage Stage)
Set stage of overall processing.
Stage getStage() const
Returns stage of overall processing.
CompileUnit(LinkingGlobalData &GlobalData, unsigned ID, StringRef ClangModuleName, DWARFFile &File, OffsetToUnitTy UnitFromOffset, dwarf::FormParams Format, llvm::endianness Endianess)
void verifyDependencies()
Check DIEs to have a consistent marking(keep marking, placement marking).
Stage
The stages of new compile unit processing.
@ CreatedNotLoaded
Created, linked with input DWARF file.
std::optional< uint64_t > getLowPc() const
Returns value of DW_AT_low_pc attribute.
std::optional< std::pair< StringRef, StringRef > > getDirAndFilenameFromLineTable(const DWARFFormValue &FileIdxValue)
Returns directory and file from the line table by index.
std::optional< UnitEntryPairTy > resolveDIEReference(const DWARFFormValue &RefValue, ResolveInterCUReferencesMode CanResolveInterCUReferences)
Resolve the DIE attribute reference that has been extracted in RefValue.
bool getModulePath(const DWARFDebugInfoEntry *DieEntry, SmallVectorImpl< char > &Path)
Appends the names of the DW_TAG_module enclosing DieEntry, outermost first.
StringEntry * getFileName(unsigned FileIdx, StringPool &GlobalStrings)
Returns name of the file for the FileIdx from the unit`s line table.
This class is a helper to create output DIE tree.
void addChild(DIE *Child)
Adds a specified Child to the current DIE.
DIE * createDIE(dwarf::Tag DieTag, uint32_t OutOffset)
Creates a DIE of specified tag DieTag and OutOffset.
This class discovers DIEs dependencies: marks "live" DIEs, marks DIE locations (whether DIE should be...
std::string UnitName
The name of this unit.
LinkingGlobalData & getGlobalData()
Return global data.
std::vector< std::unique_ptr< DIEAbbrev > > Abbreviations
Storage for the unique Abbreviations.
std::string SysRoot
The DW_AT_LLVM_sysroot of this unit.
bool isClangModule() const
Return true if this compile unit is from Clang module.
std::mutex FileNamesMutex
Guards FileNames.
unsigned ID
Unique ID for the unit.
const std::string & getClangModuleName() const
Return Clang module name;.
void setOutUnitDIE(DIE *UnitDie)
Set output unit DIE.
DwarfUnit(LinkingGlobalData &GlobalData, unsigned ID, StringRef ClangModuleName)
std::string ClangModuleName
If this is a Clang module, this holds the module's name.
FoldingSet< DIEAbbrev > AbbreviationsSet
FoldingSet that uniques the abbreviations.
StringRef getSysRoot()
Return the DW_AT_LLVM_sysroot of the compile unit or an empty StringRef.
DIE * getOutUnitDIE()
Returns output unit DIE.
This class keeps data and services common for the whole linking process.
void set(StringRef Path, const ModuleAnchor &Location)
Definition ModulePool.h:51
This class helps to assign indexes for DIE children.
dwarf::FormParams Format
Format for sections.
const dwarf::FormParams & getFormParams() const
Return size of address.
void eraseSections()
Erases data of all sections.
std::optional< const SectionDescriptor * > tryGetSectionDescriptor(DebugSectionKind SectionKind) const
Returns descriptor for the specified section of SectionKind.
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.
llvm::endianness getEndianness() const
Endiannes for the sections.
SectionDescriptor & getOrCreateSectionDescriptor(DebugSectionKind SectionKind)
Returns descriptor for the specified section of SectionKind.
const SectionDescriptor & getSectionDescriptor(DebugSectionKind SectionKind) const
Returns descriptor for the specified section of SectionKind.
The helper class to build type name based on DIE properties.
Error assignName(UnitEntryPairTy InputUnitEntryPair, std::optional< std::pair< size_t, size_t > > ChildIndex)
Create synthetic name for the specified DIE InputUnitEntryPair and assign created name to the DIE typ...
Keeps cloned data for the type DIE.
Definition TypePool.h:31
std::atomic< DIE * > Die
TypeEntryBody keeps partially cloned DIEs corresponding to this type.
Definition TypePool.h:60
std::atomic< uint64_t > DiePriority
Definition TypePool.h:71
TypePool keeps type descriptors which contain partially cloned DIE correspinding to each type.
Definition TypePool.h:129
BumpPtrAllocator & getThreadLocalAllocator()
Return thread local allocator used by pool.
Definition TypePool.h:182
TypeEntryBody * getOrCreateTypeEntryBody(TypeEntry *Entry, TypeEntry *ParentEntry)
Create or return existing type entry body for the specified Entry.
Definition TypePool.h:152
TypeEntry * getRoot() const
Return root for all type entries.
Definition TypePool.h:179
Type Unit is used to represent an artificial compilation unit which keeps all type information.
TypePool & getTypePool()
Returns global type pool.
uint64_t tell() const
tell - Return the current offset with the file.
void rememberDieOutOffset(uint32_t Idx, uint64_t Offset)
Idx index of the DIE.
TypeEntry * getDieTypeEntry(uint32_t Idx)
Idx index of the DIE.
DIEInfo & getDIEInfo(unsigned Idx)
Idx index of the DIE.
uint64_t getDieOutOffset(uint32_t Idx)
Idx index of the DIE.
const DWARFDebugInfoEntry * getSiblingEntry(const DWARFDebugInfoEntry *Die) const
const DWARFDebugInfoEntry * getFirstChildEntry(const DWARFDebugInfoEntry *Die) const
std::optional< uint32_t > getDIEIndexForOffset(uint64_t Offset)
DWARFDie getDIE(const DWARFDebugInfoEntry *Die)
const DWARFDebugInfoEntry * getDebugInfoEntry(unsigned Index) const
DWARFUnit & getOrigUnit() const
Returns paired compile unit from input DWARF.
DWARFDie getUnitDIE(bool ExtractUnitDIEOnly=true)
DWARFDie getParent(const DWARFDebugInfoEntry *Die)
uint32_t getDIEIndex(const DWARFDebugInfoEntry *Die) const
std::optional< DWARFFormValue > find(uint32_t DieIdx, ArrayRef< dwarf::Attribute > Attrs) const
Error emitDebugInfo(const Triple &TargetTriple)
Emit .debug_info section for unit DIEs.
Error emitDebugLine(const Triple &TargetTriple, const DWARFDebugLine::LineTable &OutLineTable, ArrayRef< uint64_t > OrigRowIndices={}, DenseMap< uint64_t, uint64_t > *RowIndexToSeqStartOffset=nullptr)
Emit .debug_line section.
Error emitDebugStringOffsetSection()
Emit the .debug_str_offsets section for current unit.
void emitPubAccelerators()
Emit .debug_pubnames and .debug_pubtypes for Unit.
void warn(const Twine &Warning, const DWARFDie *DIE=nullptr)
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ HeaderSize
Definition BTF.h:61
@ Entry
Definition COFF.h:862
bool isODRLanguage(uint16_t Language)
function_ref< CompileUnit *(uint64_t Offset)> OffsetToUnitTy
SmallVector< uint64_t * > OffsetsPtrVector
Type for list of pointers to patches offsets.
StringMapEntry< std::atomic< TypeEntryBody * > > TypeEntry
Definition TypePool.h:28
ArrayRef< dwarf::Attribute > getODRAttributes()
StringRef guessDeveloperDir(StringRef SysRoot)
Make a best effort to guess the Xcode.app/Contents/Developer path from an SDK path.
Definition Utils.h:80
DebugSectionKind
List of tracked debug tables.
LLVM_ABI void buildStmtSeqOffsetToFirstRowIndex(const DWARFDebugLine::LineTable &LT, ArrayRef< uint64_t > SortedStmtSeqOffsets, DenseMap< uint64_t, uint64_t > &SeqOffToFirstRow)
Build a map from an input DW_AT_LLVM_stmt_sequence byte offset to the first-row index (in LT....
Definition Utils.cpp:82
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
bool isInToolchainDir(StringRef Path)
Make a best effort to determine whether Path is inside a toolchain.
Definition Utils.h:116
bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path)
Definition Utils.h:137
std::optional< uint64_t > toAddress(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an address.
Attribute
Attributes.
Definition Dwarf.h:125
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
std::optional< uint64_t > toSectionOffset(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an section offset.
StringRef toStringRef(const std::optional< DWARFFormValue > &V, StringRef Default={})
Take an optional DWARFFormValue and try to extract a string value from it.
std::optional< uint64_t > toUnsigned(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an unsigned constant.
@ DW_ARANGES_VERSION
Section version number for .debug_aranges.
Definition Dwarf.h:66
LLVM_ABI std::error_code real_path(const Twine &path, SmallVectorImpl< char > &output, bool expand_tilde=false)
Collapse all .
LLVM_ABI StringRef parent_path(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get parent path.
Definition Path.cpp:478
LLVM_ABI bool is_relative(const Twine &path, Style style=Style::native)
Is path relative?
Definition Path.cpp:716
LLVM_ABI StringRef filename(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get filename.
Definition Path.cpp:594
LLVM_ABI void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition Path.cpp:467
void swapByteOrder(T &Value)
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1781
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1775
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2570
auto partition_point(R &&Range, Predicate P)
Binary search for the first iterator in a range where a predicate is false.
Definition STLExtras.h:2145
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2224
auto unique(Range &&R, Predicate P)
Definition STLExtras.h:2150
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
Op::Description Desc
static void insertLineSequence(std::vector< TrackedRow > &Seq, std::vector< TrackedRow > &Rows)
Insert the new line info sequence Seq into the current set of already linked line info Rows.
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1652
@ Dwarf
DWARF v5 .debug_names.
Definition DwarfDebug.h:348
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
Definition Alignment.h:191
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition LEB128.h:79
bool isCompileUnit(const std::unique_ptr< DWARFUnit > &U)
Definition DWARFUnit.h:612
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:391
endianness
Definition bit.h:71
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1106
dwarf::FormParams FormParams
Version, address size (starting in v5), and DWARF32/64 format; these parameters affect interpretation...
Standard .debug_line state machine structure.
object::SectionedAddress Address
The program-counter value corresponding to a machine instruction generated by the compiler and sectio...
Represents a single DWARF expression, whose value is location-dependent.
A helper struct providing information about the byte size of DW_FORM values that vary in size dependi...
Definition Dwarf.h:1216
DwarfFormat Format
Definition Dwarf.h:1219
uint64_t getDwarfMaxOffset() const
Definition Dwarf.h:1237
uint8_t getDwarfOffsetByteSize() const
The size of a reference is determined by the DWARF 32/64-bit format.
Definition Dwarf.h:1234
This structure is used to update a DW_AT_import reference to a DW_TAG_module.
This structure is used to update reference to the DIE.
PointerIntPair< CompileUnit *, 1 > RefCU
This structure is used to update location list offset into .debug_loc/.debug_loclists.
This structure is used to update range list offset into .debug_ranges/.debug_rnglists.
bool IsCompileUnitRanges
Indicates patch which points to immediate compile unit's attribute.
This structure is used to update reference to the DIE of ULEB128 form.
Where the DW_TAG_module DIE describing a clang module ended up in the output.
Definition ModulePool.h:30
uint64_t Priority
Priority of the unit which recorded this anchor.
Definition ModulePool.h:38
dwarf::FormParams getFormParams() const
Returns FormParams used by section.
This structure is used to keep data of the concrete section.
raw_svector_ostream OS
Stream which stores data to the Contents.
void emitUnitLength(uint64_t Length)
Emit unit length into the current section contents.
void emitOffset(uint64_t Val)
Emit specified offset value into the current section contents.
void emitIntVal(uint64_t Val, unsigned Size)
Emit specified integer value into the current section contents.
void apply(uint64_t PatchOffset, dwarf::Form AttrForm, uint64_t Val)
Write specified Value of AttrForm to the PatchOffset.
uint64_t getIntVal(uint64_t PatchOffset, unsigned Size)
Returns integer value of Size located by specified PatchOffset.
This is a helper structure which keeps a debug info entry with it's containing compilation unit.