xref: /freebsd/contrib/llvm-project/llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
1 //=== DWARFLinker.cpp -----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/DWARFLinker/Classic/DWARFLinker.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/BitVector.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/CodeGen/NonRelocatableStringpool.h"
15 #include "llvm/DWARFLinker/Classic/DWARFLinkerDeclContext.h"
16 #include "llvm/DWARFLinker/Classic/DWARFStreamer.h"
17 #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
18 #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
19 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
20 #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
21 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
22 #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
23 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
24 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
25 #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
26 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
27 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
28 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
29 #include "llvm/MC/MCDwarf.h"
30 #include "llvm/Support/DataExtractor.h"
31 #include "llvm/Support/Error.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/ErrorOr.h"
34 #include "llvm/Support/FormatVariadic.h"
35 #include "llvm/Support/LEB128.h"
36 #include "llvm/Support/Path.h"
37 #include "llvm/Support/ThreadPool.h"
38 #include <vector>
39 
40 namespace llvm {
41 
42 using namespace dwarf_linker;
43 using namespace dwarf_linker::classic;
44 
45 /// Hold the input and output of the debug info size in bytes.
46 struct DebugInfoSize {
47   uint64_t Input;
48   uint64_t Output;
49 };
50 
51 /// Compute the total size of the debug info.
52 static uint64_t getDebugInfoSize(DWARFContext &Dwarf) {
53   uint64_t Size = 0;
54   for (auto &Unit : Dwarf.compile_units()) {
55     Size += Unit->getLength();
56   }
57   return Size;
58 }
59 
60 /// Similar to DWARFUnitSection::getUnitForOffset(), but returning our
61 /// CompileUnit object instead.
62 static CompileUnit *getUnitForOffset(const UnitListTy &Units, uint64_t Offset) {
63   auto CU = llvm::upper_bound(
64       Units, Offset, [](uint64_t LHS, const std::unique_ptr<CompileUnit> &RHS) {
65         return LHS < RHS->getOrigUnit().getNextUnitOffset();
66       });
67   return CU != Units.end() ? CU->get() : nullptr;
68 }
69 
70 /// Resolve the DIE attribute reference that has been extracted in \p RefValue.
71 /// The resulting DIE might be in another CompileUnit which is stored into \p
72 /// ReferencedCU. \returns null if resolving fails for any reason.
73 DWARFDie DWARFLinker::resolveDIEReference(const DWARFFile &File,
74                                           const UnitListTy &Units,
75                                           const DWARFFormValue &RefValue,
76                                           const DWARFDie &DIE,
77                                           CompileUnit *&RefCU) {
78   assert(RefValue.isFormClass(DWARFFormValue::FC_Reference));
79   uint64_t RefOffset = *RefValue.getAsReference();
80   if ((RefCU = getUnitForOffset(Units, RefOffset)))
81     if (const auto RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset)) {
82       // In a file with broken references, an attribute might point to a NULL
83       // DIE.
84       if (!RefDie.isNULL())
85         return RefDie;
86     }
87 
88   reportWarning("could not find referenced DIE", File, &DIE);
89   return DWARFDie();
90 }
91 
92 /// \returns whether the passed \a Attr type might contain a DIE reference
93 /// suitable for ODR uniquing.
94 static bool isODRAttribute(uint16_t Attr) {
95   switch (Attr) {
96   default:
97     return false;
98   case dwarf::DW_AT_type:
99   case dwarf::DW_AT_containing_type:
100   case dwarf::DW_AT_specification:
101   case dwarf::DW_AT_abstract_origin:
102   case dwarf::DW_AT_import:
103     return true;
104   }
105   llvm_unreachable("Improper attribute.");
106 }
107 
108 static bool isTypeTag(uint16_t Tag) {
109   switch (Tag) {
110   case dwarf::DW_TAG_array_type:
111   case dwarf::DW_TAG_class_type:
112   case dwarf::DW_TAG_enumeration_type:
113   case dwarf::DW_TAG_pointer_type:
114   case dwarf::DW_TAG_reference_type:
115   case dwarf::DW_TAG_string_type:
116   case dwarf::DW_TAG_structure_type:
117   case dwarf::DW_TAG_subroutine_type:
118   case dwarf::DW_TAG_typedef:
119   case dwarf::DW_TAG_union_type:
120   case dwarf::DW_TAG_ptr_to_member_type:
121   case dwarf::DW_TAG_set_type:
122   case dwarf::DW_TAG_subrange_type:
123   case dwarf::DW_TAG_base_type:
124   case dwarf::DW_TAG_const_type:
125   case dwarf::DW_TAG_constant:
126   case dwarf::DW_TAG_file_type:
127   case dwarf::DW_TAG_namelist:
128   case dwarf::DW_TAG_packed_type:
129   case dwarf::DW_TAG_volatile_type:
130   case dwarf::DW_TAG_restrict_type:
131   case dwarf::DW_TAG_atomic_type:
132   case dwarf::DW_TAG_interface_type:
133   case dwarf::DW_TAG_unspecified_type:
134   case dwarf::DW_TAG_shared_type:
135   case dwarf::DW_TAG_immutable_type:
136     return true;
137   default:
138     break;
139   }
140   return false;
141 }
142 
143 bool DWARFLinker::DIECloner::getDIENames(const DWARFDie &Die,
144                                          AttributesInfo &Info,
145                                          OffsetsStringPool &StringPool,
146                                          bool StripTemplate) {
147   // This function will be called on DIEs having low_pcs and
148   // ranges. As getting the name might be more expansive, filter out
149   // blocks directly.
150   if (Die.getTag() == dwarf::DW_TAG_lexical_block)
151     return false;
152 
153   if (!Info.MangledName)
154     if (const char *MangledName = Die.getLinkageName())
155       Info.MangledName = StringPool.getEntry(MangledName);
156 
157   if (!Info.Name)
158     if (const char *Name = Die.getShortName())
159       Info.Name = StringPool.getEntry(Name);
160 
161   if (!Info.MangledName)
162     Info.MangledName = Info.Name;
163 
164   if (StripTemplate && Info.Name && Info.MangledName != Info.Name) {
165     StringRef Name = Info.Name.getString();
166     if (std::optional<StringRef> StrippedName = StripTemplateParameters(Name))
167       Info.NameWithoutTemplate = StringPool.getEntry(*StrippedName);
168   }
169 
170   return Info.Name || Info.MangledName;
171 }
172 
173 /// Resolve the relative path to a build artifact referenced by DWARF by
174 /// applying DW_AT_comp_dir.
175 static void resolveRelativeObjectPath(SmallVectorImpl<char> &Buf, DWARFDie CU) {
176   sys::path::append(Buf, dwarf::toString(CU.find(dwarf::DW_AT_comp_dir), ""));
177 }
178 
179 /// Make a best effort to guess the
180 /// Xcode.app/Contents/Developer/Toolchains/ path from an SDK path.
181 static SmallString<128> guessToolchainBaseDir(StringRef SysRoot) {
182   SmallString<128> Result;
183   // Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
184   StringRef Base = sys::path::parent_path(SysRoot);
185   if (sys::path::filename(Base) != "SDKs")
186     return Result;
187   Base = sys::path::parent_path(Base);
188   Result = Base;
189   Result += "/Toolchains";
190   return Result;
191 }
192 
193 /// Collect references to parseable Swift interfaces in imported
194 /// DW_TAG_module blocks.
195 static void analyzeImportedModule(
196     const DWARFDie &DIE, CompileUnit &CU,
197     DWARFLinkerBase::SwiftInterfacesMapTy *ParseableSwiftInterfaces,
198     std::function<void(const Twine &, const DWARFDie &)> ReportWarning) {
199   if (CU.getLanguage() != dwarf::DW_LANG_Swift)
200     return;
201 
202   if (!ParseableSwiftInterfaces)
203     return;
204 
205   StringRef Path = dwarf::toStringRef(DIE.find(dwarf::DW_AT_LLVM_include_path));
206   if (!Path.ends_with(".swiftinterface"))
207     return;
208   // Don't track interfaces that are part of the SDK.
209   StringRef SysRoot = dwarf::toStringRef(DIE.find(dwarf::DW_AT_LLVM_sysroot));
210   if (SysRoot.empty())
211     SysRoot = CU.getSysRoot();
212   if (!SysRoot.empty() && Path.starts_with(SysRoot))
213     return;
214   // Don't track interfaces that are part of the toolchain.
215   // For example: Swift, _Concurrency, ...
216   SmallString<128> Toolchain = guessToolchainBaseDir(SysRoot);
217   if (!Toolchain.empty() && Path.starts_with(Toolchain))
218     return;
219   std::optional<const char *> Name =
220       dwarf::toString(DIE.find(dwarf::DW_AT_name));
221   if (!Name)
222     return;
223   auto &Entry = (*ParseableSwiftInterfaces)[*Name];
224   // The prepend path is applied later when copying.
225   DWARFDie CUDie = CU.getOrigUnit().getUnitDIE();
226   SmallString<128> ResolvedPath;
227   if (sys::path::is_relative(Path))
228     resolveRelativeObjectPath(ResolvedPath, CUDie);
229   sys::path::append(ResolvedPath, Path);
230   if (!Entry.empty() && Entry != ResolvedPath)
231     ReportWarning(Twine("Conflicting parseable interfaces for Swift Module ") +
232                       *Name + ": " + Entry + " and " + Path,
233                   DIE);
234   Entry = std::string(ResolvedPath.str());
235 }
236 
237 /// The distinct types of work performed by the work loop in
238 /// analyzeContextInfo.
239 enum class ContextWorklistItemType : uint8_t {
240   AnalyzeContextInfo,
241   UpdateChildPruning,
242   UpdatePruning,
243 };
244 
245 /// This class represents an item in the work list. The type defines what kind
246 /// of work needs to be performed when processing the current item. Everything
247 /// but the Type and Die fields are optional based on the type.
248 struct ContextWorklistItem {
249   DWARFDie Die;
250   unsigned ParentIdx;
251   union {
252     CompileUnit::DIEInfo *OtherInfo;
253     DeclContext *Context;
254   };
255   ContextWorklistItemType Type;
256   bool InImportedModule;
257 
258   ContextWorklistItem(DWARFDie Die, ContextWorklistItemType T,
259                       CompileUnit::DIEInfo *OtherInfo = nullptr)
260       : Die(Die), ParentIdx(0), OtherInfo(OtherInfo), Type(T),
261         InImportedModule(false) {}
262 
263   ContextWorklistItem(DWARFDie Die, DeclContext *Context, unsigned ParentIdx,
264                       bool InImportedModule)
265       : Die(Die), ParentIdx(ParentIdx), Context(Context),
266         Type(ContextWorklistItemType::AnalyzeContextInfo),
267         InImportedModule(InImportedModule) {}
268 };
269 
270 static bool updatePruning(const DWARFDie &Die, CompileUnit &CU,
271                           uint64_t ModulesEndOffset) {
272   CompileUnit::DIEInfo &Info = CU.getInfo(Die);
273 
274   // Prune this DIE if it is either a forward declaration inside a
275   // DW_TAG_module or a DW_TAG_module that contains nothing but
276   // forward declarations.
277   Info.Prune &= (Die.getTag() == dwarf::DW_TAG_module) ||
278                 (isTypeTag(Die.getTag()) &&
279                  dwarf::toUnsigned(Die.find(dwarf::DW_AT_declaration), 0));
280 
281   // Only prune forward declarations inside a DW_TAG_module for which a
282   // definition exists elsewhere.
283   if (ModulesEndOffset == 0)
284     Info.Prune &= Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset();
285   else
286     Info.Prune &= Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset() > 0 &&
287                   Info.Ctxt->getCanonicalDIEOffset() <= ModulesEndOffset;
288 
289   return Info.Prune;
290 }
291 
292 static void updateChildPruning(const DWARFDie &Die, CompileUnit &CU,
293                                CompileUnit::DIEInfo &ChildInfo) {
294   CompileUnit::DIEInfo &Info = CU.getInfo(Die);
295   Info.Prune &= ChildInfo.Prune;
296 }
297 
298 /// Recursive helper to build the global DeclContext information and
299 /// gather the child->parent relationships in the original compile unit.
300 ///
301 /// This function uses the same work list approach as lookForDIEsToKeep.
302 ///
303 /// \return true when this DIE and all of its children are only
304 /// forward declarations to types defined in external clang modules
305 /// (i.e., forward declarations that are children of a DW_TAG_module).
306 static void analyzeContextInfo(
307     const DWARFDie &DIE, unsigned ParentIdx, CompileUnit &CU,
308     DeclContext *CurrentDeclContext, DeclContextTree &Contexts,
309     uint64_t ModulesEndOffset,
310     DWARFLinkerBase::SwiftInterfacesMapTy *ParseableSwiftInterfaces,
311     std::function<void(const Twine &, const DWARFDie &)> ReportWarning) {
312   // LIFO work list.
313   std::vector<ContextWorklistItem> Worklist;
314   Worklist.emplace_back(DIE, CurrentDeclContext, ParentIdx, false);
315 
316   while (!Worklist.empty()) {
317     ContextWorklistItem Current = Worklist.back();
318     Worklist.pop_back();
319 
320     switch (Current.Type) {
321     case ContextWorklistItemType::UpdatePruning:
322       updatePruning(Current.Die, CU, ModulesEndOffset);
323       continue;
324     case ContextWorklistItemType::UpdateChildPruning:
325       updateChildPruning(Current.Die, CU, *Current.OtherInfo);
326       continue;
327     case ContextWorklistItemType::AnalyzeContextInfo:
328       break;
329     }
330 
331     unsigned Idx = CU.getOrigUnit().getDIEIndex(Current.Die);
332     CompileUnit::DIEInfo &Info = CU.getInfo(Idx);
333 
334     // Clang imposes an ODR on modules(!) regardless of the language:
335     //  "The module-id should consist of only a single identifier,
336     //   which provides the name of the module being defined. Each
337     //   module shall have a single definition."
338     //
339     // This does not extend to the types inside the modules:
340     //  "[I]n C, this implies that if two structs are defined in
341     //   different submodules with the same name, those two types are
342     //   distinct types (but may be compatible types if their
343     //   definitions match)."
344     //
345     // We treat non-C++ modules like namespaces for this reason.
346     if (Current.Die.getTag() == dwarf::DW_TAG_module &&
347         Current.ParentIdx == 0 &&
348         dwarf::toString(Current.Die.find(dwarf::DW_AT_name), "") !=
349             CU.getClangModuleName()) {
350       Current.InImportedModule = true;
351       analyzeImportedModule(Current.Die, CU, ParseableSwiftInterfaces,
352                             ReportWarning);
353     }
354 
355     Info.ParentIdx = Current.ParentIdx;
356     Info.InModuleScope = CU.isClangModule() || Current.InImportedModule;
357     if (CU.hasODR() || Info.InModuleScope) {
358       if (Current.Context) {
359         auto PtrInvalidPair = Contexts.getChildDeclContext(
360             *Current.Context, Current.Die, CU, Info.InModuleScope);
361         Current.Context = PtrInvalidPair.getPointer();
362         Info.Ctxt =
363             PtrInvalidPair.getInt() ? nullptr : PtrInvalidPair.getPointer();
364         if (Info.Ctxt)
365           Info.Ctxt->setDefinedInClangModule(Info.InModuleScope);
366       } else
367         Info.Ctxt = Current.Context = nullptr;
368     }
369 
370     Info.Prune = Current.InImportedModule;
371     // Add children in reverse order to the worklist to effectively process
372     // them in order.
373     Worklist.emplace_back(Current.Die, ContextWorklistItemType::UpdatePruning);
374     for (auto Child : reverse(Current.Die.children())) {
375       CompileUnit::DIEInfo &ChildInfo = CU.getInfo(Child);
376       Worklist.emplace_back(
377           Current.Die, ContextWorklistItemType::UpdateChildPruning, &ChildInfo);
378       Worklist.emplace_back(Child, Current.Context, Idx,
379                             Current.InImportedModule);
380     }
381   }
382 }
383 
384 static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) {
385   switch (Tag) {
386   default:
387     return false;
388   case dwarf::DW_TAG_class_type:
389   case dwarf::DW_TAG_common_block:
390   case dwarf::DW_TAG_lexical_block:
391   case dwarf::DW_TAG_structure_type:
392   case dwarf::DW_TAG_subprogram:
393   case dwarf::DW_TAG_subroutine_type:
394   case dwarf::DW_TAG_union_type:
395     return true;
396   }
397   llvm_unreachable("Invalid Tag");
398 }
399 
400 void DWARFLinker::cleanupAuxiliarryData(LinkContext &Context) {
401   Context.clear();
402 
403   for (DIEBlock *I : DIEBlocks)
404     I->~DIEBlock();
405   for (DIELoc *I : DIELocs)
406     I->~DIELoc();
407 
408   DIEBlocks.clear();
409   DIELocs.clear();
410   DIEAlloc.Reset();
411 }
412 
413 static bool isTlsAddressCode(uint8_t DW_OP_Code) {
414   return DW_OP_Code == dwarf::DW_OP_form_tls_address ||
415          DW_OP_Code == dwarf::DW_OP_GNU_push_tls_address;
416 }
417 
418 std::pair<bool, std::optional<int64_t>>
419 DWARFLinker::getVariableRelocAdjustment(AddressesMap &RelocMgr,
420                                         const DWARFDie &DIE) {
421   assert((DIE.getTag() == dwarf::DW_TAG_variable ||
422           DIE.getTag() == dwarf::DW_TAG_constant) &&
423          "Wrong type of input die");
424 
425   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
426 
427   // Check if DIE has DW_AT_location attribute.
428   DWARFUnit *U = DIE.getDwarfUnit();
429   std::optional<uint32_t> LocationIdx =
430       Abbrev->findAttributeIndex(dwarf::DW_AT_location);
431   if (!LocationIdx)
432     return std::make_pair(false, std::nullopt);
433 
434   // Get offset to the DW_AT_location attribute.
435   uint64_t AttrOffset =
436       Abbrev->getAttributeOffsetFromIndex(*LocationIdx, DIE.getOffset(), *U);
437 
438   // Get value of the DW_AT_location attribute.
439   std::optional<DWARFFormValue> LocationValue =
440       Abbrev->getAttributeValueFromOffset(*LocationIdx, AttrOffset, *U);
441   if (!LocationValue)
442     return std::make_pair(false, std::nullopt);
443 
444   // Check that DW_AT_location attribute is of 'exprloc' class.
445   // Handling value of location expressions for attributes of 'loclist'
446   // class is not implemented yet.
447   std::optional<ArrayRef<uint8_t>> Expr = LocationValue->getAsBlock();
448   if (!Expr)
449     return std::make_pair(false, std::nullopt);
450 
451   // Parse 'exprloc' expression.
452   DataExtractor Data(toStringRef(*Expr), U->getContext().isLittleEndian(),
453                      U->getAddressByteSize());
454   DWARFExpression Expression(Data, U->getAddressByteSize(),
455                              U->getFormParams().Format);
456 
457   bool HasLocationAddress = false;
458   uint64_t CurExprOffset = 0;
459   for (DWARFExpression::iterator It = Expression.begin();
460        It != Expression.end(); ++It) {
461     DWARFExpression::iterator NextIt = It;
462     ++NextIt;
463 
464     const DWARFExpression::Operation &Op = *It;
465     switch (Op.getCode()) {
466     case dwarf::DW_OP_const2u:
467     case dwarf::DW_OP_const4u:
468     case dwarf::DW_OP_const8u:
469     case dwarf::DW_OP_const2s:
470     case dwarf::DW_OP_const4s:
471     case dwarf::DW_OP_const8s:
472       if (NextIt == Expression.end() || !isTlsAddressCode(NextIt->getCode()))
473         break;
474       [[fallthrough]];
475     case dwarf::DW_OP_addr: {
476       HasLocationAddress = true;
477       // Check relocation for the address.
478       if (std::optional<int64_t> RelocAdjustment =
479               RelocMgr.getExprOpAddressRelocAdjustment(
480                   *U, Op, AttrOffset + CurExprOffset,
481                   AttrOffset + Op.getEndOffset()))
482         return std::make_pair(HasLocationAddress, *RelocAdjustment);
483     } break;
484     case dwarf::DW_OP_constx:
485     case dwarf::DW_OP_addrx: {
486       HasLocationAddress = true;
487       if (std::optional<uint64_t> AddressOffset =
488               DIE.getDwarfUnit()->getIndexedAddressOffset(
489                   Op.getRawOperand(0))) {
490         // Check relocation for the address.
491         if (std::optional<int64_t> RelocAdjustment =
492                 RelocMgr.getExprOpAddressRelocAdjustment(
493                     *U, Op, *AddressOffset,
494                     *AddressOffset + DIE.getDwarfUnit()->getAddressByteSize()))
495           return std::make_pair(HasLocationAddress, *RelocAdjustment);
496       }
497     } break;
498     default: {
499       // Nothing to do.
500     } break;
501     }
502     CurExprOffset = Op.getEndOffset();
503   }
504 
505   return std::make_pair(HasLocationAddress, std::nullopt);
506 }
507 
508 /// Check if a variable describing DIE should be kept.
509 /// \returns updated TraversalFlags.
510 unsigned DWARFLinker::shouldKeepVariableDIE(AddressesMap &RelocMgr,
511                                             const DWARFDie &DIE,
512                                             CompileUnit::DIEInfo &MyInfo,
513                                             unsigned Flags) {
514   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
515 
516   // Global variables with constant value can always be kept.
517   if (!(Flags & TF_InFunctionScope) &&
518       Abbrev->findAttributeIndex(dwarf::DW_AT_const_value)) {
519     MyInfo.InDebugMap = true;
520     return Flags | TF_Keep;
521   }
522 
523   // See if there is a relocation to a valid debug map entry inside this
524   // variable's location. The order is important here. We want to always check
525   // if the variable has a valid relocation, so that the DIEInfo is filled.
526   // However, we don't want a static variable in a function to force us to keep
527   // the enclosing function, unless requested explicitly.
528   std::pair<bool, std::optional<int64_t>> LocExprAddrAndRelocAdjustment =
529       getVariableRelocAdjustment(RelocMgr, DIE);
530 
531   if (LocExprAddrAndRelocAdjustment.first)
532     MyInfo.HasLocationExpressionAddr = true;
533 
534   if (!LocExprAddrAndRelocAdjustment.second)
535     return Flags;
536 
537   MyInfo.AddrAdjust = *LocExprAddrAndRelocAdjustment.second;
538   MyInfo.InDebugMap = true;
539 
540   if (((Flags & TF_InFunctionScope) &&
541        !LLVM_UNLIKELY(Options.KeepFunctionForStatic)))
542     return Flags;
543 
544   if (Options.Verbose) {
545     outs() << "Keeping variable DIE:";
546     DIDumpOptions DumpOpts;
547     DumpOpts.ChildRecurseDepth = 0;
548     DumpOpts.Verbose = Options.Verbose;
549     DIE.dump(outs(), 8 /* Indent */, DumpOpts);
550   }
551 
552   return Flags | TF_Keep;
553 }
554 
555 /// Check if a function describing DIE should be kept.
556 /// \returns updated TraversalFlags.
557 unsigned DWARFLinker::shouldKeepSubprogramDIE(
558     AddressesMap &RelocMgr, const DWARFDie &DIE, const DWARFFile &File,
559     CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo, unsigned Flags) {
560   Flags |= TF_InFunctionScope;
561 
562   auto LowPc = dwarf::toAddress(DIE.find(dwarf::DW_AT_low_pc));
563   if (!LowPc)
564     return Flags;
565 
566   assert(LowPc && "low_pc attribute is not an address.");
567   std::optional<int64_t> RelocAdjustment =
568       RelocMgr.getSubprogramRelocAdjustment(DIE);
569   if (!RelocAdjustment)
570     return Flags;
571 
572   MyInfo.AddrAdjust = *RelocAdjustment;
573   MyInfo.InDebugMap = true;
574 
575   if (Options.Verbose) {
576     outs() << "Keeping subprogram DIE:";
577     DIDumpOptions DumpOpts;
578     DumpOpts.ChildRecurseDepth = 0;
579     DumpOpts.Verbose = Options.Verbose;
580     DIE.dump(outs(), 8 /* Indent */, DumpOpts);
581   }
582 
583   if (DIE.getTag() == dwarf::DW_TAG_label) {
584     if (Unit.hasLabelAt(*LowPc))
585       return Flags;
586 
587     DWARFUnit &OrigUnit = Unit.getOrigUnit();
588     // FIXME: dsymutil-classic compat. dsymutil-classic doesn't consider labels
589     // that don't fall into the CU's aranges. This is wrong IMO. Debug info
590     // generation bugs aside, this is really wrong in the case of labels, where
591     // a label marking the end of a function will have a PC == CU's high_pc.
592     if (dwarf::toAddress(OrigUnit.getUnitDIE().find(dwarf::DW_AT_high_pc))
593             .value_or(UINT64_MAX) <= LowPc)
594       return Flags;
595     Unit.addLabelLowPc(*LowPc, MyInfo.AddrAdjust);
596     return Flags | TF_Keep;
597   }
598 
599   Flags |= TF_Keep;
600 
601   std::optional<uint64_t> HighPc = DIE.getHighPC(*LowPc);
602   if (!HighPc) {
603     reportWarning("Function without high_pc. Range will be discarded.\n", File,
604                   &DIE);
605     return Flags;
606   }
607   if (*LowPc > *HighPc) {
608     reportWarning("low_pc greater than high_pc. Range will be discarded.\n",
609                   File, &DIE);
610     return Flags;
611   }
612 
613   // Replace the debug map range with a more accurate one.
614   Unit.addFunctionRange(*LowPc, *HighPc, MyInfo.AddrAdjust);
615   return Flags;
616 }
617 
618 /// Check if a DIE should be kept.
619 /// \returns updated TraversalFlags.
620 unsigned DWARFLinker::shouldKeepDIE(AddressesMap &RelocMgr, const DWARFDie &DIE,
621                                     const DWARFFile &File, CompileUnit &Unit,
622                                     CompileUnit::DIEInfo &MyInfo,
623                                     unsigned Flags) {
624   switch (DIE.getTag()) {
625   case dwarf::DW_TAG_constant:
626   case dwarf::DW_TAG_variable:
627     return shouldKeepVariableDIE(RelocMgr, DIE, MyInfo, Flags);
628   case dwarf::DW_TAG_subprogram:
629   case dwarf::DW_TAG_label:
630     return shouldKeepSubprogramDIE(RelocMgr, DIE, File, Unit, MyInfo, Flags);
631   case dwarf::DW_TAG_base_type:
632     // DWARF Expressions may reference basic types, but scanning them
633     // is expensive. Basic types are tiny, so just keep all of them.
634   case dwarf::DW_TAG_imported_module:
635   case dwarf::DW_TAG_imported_declaration:
636   case dwarf::DW_TAG_imported_unit:
637     // We always want to keep these.
638     return Flags | TF_Keep;
639   default:
640     break;
641   }
642 
643   return Flags;
644 }
645 
646 /// Helper that updates the completeness of the current DIE based on the
647 /// completeness of one of its children. It depends on the incompleteness of
648 /// the children already being computed.
649 static void updateChildIncompleteness(const DWARFDie &Die, CompileUnit &CU,
650                                       CompileUnit::DIEInfo &ChildInfo) {
651   switch (Die.getTag()) {
652   case dwarf::DW_TAG_structure_type:
653   case dwarf::DW_TAG_class_type:
654   case dwarf::DW_TAG_union_type:
655     break;
656   default:
657     return;
658   }
659 
660   CompileUnit::DIEInfo &MyInfo = CU.getInfo(Die);
661 
662   if (ChildInfo.Incomplete || ChildInfo.Prune)
663     MyInfo.Incomplete = true;
664 }
665 
666 /// Helper that updates the completeness of the current DIE based on the
667 /// completeness of the DIEs it references. It depends on the incompleteness of
668 /// the referenced DIE already being computed.
669 static void updateRefIncompleteness(const DWARFDie &Die, CompileUnit &CU,
670                                     CompileUnit::DIEInfo &RefInfo) {
671   switch (Die.getTag()) {
672   case dwarf::DW_TAG_typedef:
673   case dwarf::DW_TAG_member:
674   case dwarf::DW_TAG_reference_type:
675   case dwarf::DW_TAG_ptr_to_member_type:
676   case dwarf::DW_TAG_pointer_type:
677     break;
678   default:
679     return;
680   }
681 
682   CompileUnit::DIEInfo &MyInfo = CU.getInfo(Die);
683 
684   if (MyInfo.Incomplete)
685     return;
686 
687   if (RefInfo.Incomplete)
688     MyInfo.Incomplete = true;
689 }
690 
691 /// Look at the children of the given DIE and decide whether they should be
692 /// kept.
693 void DWARFLinker::lookForChildDIEsToKeep(
694     const DWARFDie &Die, CompileUnit &CU, unsigned Flags,
695     SmallVectorImpl<WorklistItem> &Worklist) {
696   // The TF_ParentWalk flag tells us that we are currently walking up the
697   // parent chain of a required DIE, and we don't want to mark all the children
698   // of the parents as kept (consider for example a DW_TAG_namespace node in
699   // the parent chain). There are however a set of DIE types for which we want
700   // to ignore that directive and still walk their children.
701   if (dieNeedsChildrenToBeMeaningful(Die.getTag()))
702     Flags &= ~DWARFLinker::TF_ParentWalk;
703 
704   // We're finished if this DIE has no children or we're walking the parent
705   // chain.
706   if (!Die.hasChildren() || (Flags & DWARFLinker::TF_ParentWalk))
707     return;
708 
709   // Add children in reverse order to the worklist to effectively process them
710   // in order.
711   for (auto Child : reverse(Die.children())) {
712     // Add a worklist item before every child to calculate incompleteness right
713     // after the current child is processed.
714     CompileUnit::DIEInfo &ChildInfo = CU.getInfo(Child);
715     Worklist.emplace_back(Die, CU, WorklistItemType::UpdateChildIncompleteness,
716                           &ChildInfo);
717     Worklist.emplace_back(Child, CU, Flags);
718   }
719 }
720 
721 static bool isODRCanonicalCandidate(const DWARFDie &Die, CompileUnit &CU) {
722   CompileUnit::DIEInfo &Info = CU.getInfo(Die);
723 
724   if (!Info.Ctxt || (Die.getTag() == dwarf::DW_TAG_namespace))
725     return false;
726 
727   if (!CU.hasODR() && !Info.InModuleScope)
728     return false;
729 
730   return !Info.Incomplete && Info.Ctxt != CU.getInfo(Info.ParentIdx).Ctxt;
731 }
732 
733 void DWARFLinker::markODRCanonicalDie(const DWARFDie &Die, CompileUnit &CU) {
734   CompileUnit::DIEInfo &Info = CU.getInfo(Die);
735 
736   Info.ODRMarkingDone = true;
737   if (Info.Keep && isODRCanonicalCandidate(Die, CU) &&
738       !Info.Ctxt->hasCanonicalDIE())
739     Info.Ctxt->setHasCanonicalDIE();
740 }
741 
742 /// Look at DIEs referenced by the given DIE and decide whether they should be
743 /// kept. All DIEs referenced though attributes should be kept.
744 void DWARFLinker::lookForRefDIEsToKeep(
745     const DWARFDie &Die, CompileUnit &CU, unsigned Flags,
746     const UnitListTy &Units, const DWARFFile &File,
747     SmallVectorImpl<WorklistItem> &Worklist) {
748   bool UseOdr = (Flags & DWARFLinker::TF_DependencyWalk)
749                     ? (Flags & DWARFLinker::TF_ODR)
750                     : CU.hasODR();
751   DWARFUnit &Unit = CU.getOrigUnit();
752   DWARFDataExtractor Data = Unit.getDebugInfoExtractor();
753   const auto *Abbrev = Die.getAbbreviationDeclarationPtr();
754   uint64_t Offset = Die.getOffset() + getULEB128Size(Abbrev->getCode());
755 
756   SmallVector<std::pair<DWARFDie, CompileUnit &>, 4> ReferencedDIEs;
757   for (const auto &AttrSpec : Abbrev->attributes()) {
758     DWARFFormValue Val(AttrSpec.Form);
759     if (!Val.isFormClass(DWARFFormValue::FC_Reference) ||
760         AttrSpec.Attr == dwarf::DW_AT_sibling) {
761       DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset,
762                                 Unit.getFormParams());
763       continue;
764     }
765 
766     Val.extractValue(Data, &Offset, Unit.getFormParams(), &Unit);
767     CompileUnit *ReferencedCU;
768     if (auto RefDie =
769             resolveDIEReference(File, Units, Val, Die, ReferencedCU)) {
770       CompileUnit::DIEInfo &Info = ReferencedCU->getInfo(RefDie);
771       // If the referenced DIE has a DeclContext that has already been
772       // emitted, then do not keep the one in this CU. We'll link to
773       // the canonical DIE in cloneDieReferenceAttribute.
774       //
775       // FIXME: compatibility with dsymutil-classic. UseODR shouldn't
776       // be necessary and could be advantageously replaced by
777       // ReferencedCU->hasODR() && CU.hasODR().
778       //
779       // FIXME: compatibility with dsymutil-classic. There is no
780       // reason not to unique ref_addr references.
781       if (AttrSpec.Form != dwarf::DW_FORM_ref_addr &&
782           isODRAttribute(AttrSpec.Attr) && Info.Ctxt &&
783           Info.Ctxt->hasCanonicalDIE())
784         continue;
785 
786       // Keep a module forward declaration if there is no definition.
787       if (!(isODRAttribute(AttrSpec.Attr) && Info.Ctxt &&
788             Info.Ctxt->hasCanonicalDIE()))
789         Info.Prune = false;
790       ReferencedDIEs.emplace_back(RefDie, *ReferencedCU);
791     }
792   }
793 
794   unsigned ODRFlag = UseOdr ? DWARFLinker::TF_ODR : 0;
795 
796   // Add referenced DIEs in reverse order to the worklist to effectively
797   // process them in order.
798   for (auto &P : reverse(ReferencedDIEs)) {
799     // Add a worklist item before every child to calculate incompleteness right
800     // after the current child is processed.
801     CompileUnit::DIEInfo &Info = P.second.getInfo(P.first);
802     Worklist.emplace_back(Die, CU, WorklistItemType::UpdateRefIncompleteness,
803                           &Info);
804     Worklist.emplace_back(P.first, P.second,
805                           DWARFLinker::TF_Keep |
806                               DWARFLinker::TF_DependencyWalk | ODRFlag);
807   }
808 }
809 
810 /// Look at the parent of the given DIE and decide whether they should be kept.
811 void DWARFLinker::lookForParentDIEsToKeep(
812     unsigned AncestorIdx, CompileUnit &CU, unsigned Flags,
813     SmallVectorImpl<WorklistItem> &Worklist) {
814   // Stop if we encounter an ancestor that's already marked as kept.
815   if (CU.getInfo(AncestorIdx).Keep)
816     return;
817 
818   DWARFUnit &Unit = CU.getOrigUnit();
819   DWARFDie ParentDIE = Unit.getDIEAtIndex(AncestorIdx);
820   Worklist.emplace_back(CU.getInfo(AncestorIdx).ParentIdx, CU, Flags);
821   Worklist.emplace_back(ParentDIE, CU, Flags);
822 }
823 
824 /// Recursively walk the \p DIE tree and look for DIEs to keep. Store that
825 /// information in \p CU's DIEInfo.
826 ///
827 /// This function is the entry point of the DIE selection algorithm. It is
828 /// expected to walk the DIE tree in file order and (though the mediation of
829 /// its helper) call hasValidRelocation() on each DIE that might be a 'root
830 /// DIE' (See DwarfLinker class comment).
831 ///
832 /// While walking the dependencies of root DIEs, this function is also called,
833 /// but during these dependency walks the file order is not respected. The
834 /// TF_DependencyWalk flag tells us which kind of traversal we are currently
835 /// doing.
836 ///
837 /// The recursive algorithm is implemented iteratively as a work list because
838 /// very deep recursion could exhaust the stack for large projects. The work
839 /// list acts as a scheduler for different types of work that need to be
840 /// performed.
841 ///
842 /// The recursive nature of the algorithm is simulated by running the "main"
843 /// algorithm (LookForDIEsToKeep) followed by either looking at more DIEs
844 /// (LookForChildDIEsToKeep, LookForRefDIEsToKeep, LookForParentDIEsToKeep) or
845 /// fixing up a computed property (UpdateChildIncompleteness,
846 /// UpdateRefIncompleteness).
847 ///
848 /// The return value indicates whether the DIE is incomplete.
849 void DWARFLinker::lookForDIEsToKeep(AddressesMap &AddressesMap,
850                                     const UnitListTy &Units,
851                                     const DWARFDie &Die, const DWARFFile &File,
852                                     CompileUnit &Cu, unsigned Flags) {
853   // LIFO work list.
854   SmallVector<WorklistItem, 4> Worklist;
855   Worklist.emplace_back(Die, Cu, Flags);
856 
857   while (!Worklist.empty()) {
858     WorklistItem Current = Worklist.pop_back_val();
859 
860     // Look at the worklist type to decide what kind of work to perform.
861     switch (Current.Type) {
862     case WorklistItemType::UpdateChildIncompleteness:
863       updateChildIncompleteness(Current.Die, Current.CU, *Current.OtherInfo);
864       continue;
865     case WorklistItemType::UpdateRefIncompleteness:
866       updateRefIncompleteness(Current.Die, Current.CU, *Current.OtherInfo);
867       continue;
868     case WorklistItemType::LookForChildDIEsToKeep:
869       lookForChildDIEsToKeep(Current.Die, Current.CU, Current.Flags, Worklist);
870       continue;
871     case WorklistItemType::LookForRefDIEsToKeep:
872       lookForRefDIEsToKeep(Current.Die, Current.CU, Current.Flags, Units, File,
873                            Worklist);
874       continue;
875     case WorklistItemType::LookForParentDIEsToKeep:
876       lookForParentDIEsToKeep(Current.AncestorIdx, Current.CU, Current.Flags,
877                               Worklist);
878       continue;
879     case WorklistItemType::MarkODRCanonicalDie:
880       markODRCanonicalDie(Current.Die, Current.CU);
881       continue;
882     case WorklistItemType::LookForDIEsToKeep:
883       break;
884     }
885 
886     unsigned Idx = Current.CU.getOrigUnit().getDIEIndex(Current.Die);
887     CompileUnit::DIEInfo &MyInfo = Current.CU.getInfo(Idx);
888 
889     if (MyInfo.Prune) {
890       // We're walking the dependencies of a module forward declaration that was
891       // kept because there is no definition.
892       if (Current.Flags & TF_DependencyWalk)
893         MyInfo.Prune = false;
894       else
895         continue;
896     }
897 
898     // If the Keep flag is set, we are marking a required DIE's dependencies.
899     // If our target is already marked as kept, we're all set.
900     bool AlreadyKept = MyInfo.Keep;
901     if ((Current.Flags & TF_DependencyWalk) && AlreadyKept)
902       continue;
903 
904     if (!(Current.Flags & TF_DependencyWalk))
905       Current.Flags = shouldKeepDIE(AddressesMap, Current.Die, File, Current.CU,
906                                     MyInfo, Current.Flags);
907 
908     // We need to mark context for the canonical die in the end of normal
909     // traversing(not TF_DependencyWalk) or after normal traversing if die
910     // was not marked as kept.
911     if (!(Current.Flags & TF_DependencyWalk) ||
912         (MyInfo.ODRMarkingDone && !MyInfo.Keep)) {
913       if (Current.CU.hasODR() || MyInfo.InModuleScope)
914         Worklist.emplace_back(Current.Die, Current.CU,
915                               WorklistItemType::MarkODRCanonicalDie);
916     }
917 
918     // Finish by looking for child DIEs. Because of the LIFO worklist we need
919     // to schedule that work before any subsequent items are added to the
920     // worklist.
921     Worklist.emplace_back(Current.Die, Current.CU, Current.Flags,
922                           WorklistItemType::LookForChildDIEsToKeep);
923 
924     if (AlreadyKept || !(Current.Flags & TF_Keep))
925       continue;
926 
927     // If it is a newly kept DIE mark it as well as all its dependencies as
928     // kept.
929     MyInfo.Keep = true;
930 
931     // We're looking for incomplete types.
932     MyInfo.Incomplete =
933         Current.Die.getTag() != dwarf::DW_TAG_subprogram &&
934         Current.Die.getTag() != dwarf::DW_TAG_member &&
935         dwarf::toUnsigned(Current.Die.find(dwarf::DW_AT_declaration), 0);
936 
937     // After looking at the parent chain, look for referenced DIEs. Because of
938     // the LIFO worklist we need to schedule that work before any subsequent
939     // items are added to the worklist.
940     Worklist.emplace_back(Current.Die, Current.CU, Current.Flags,
941                           WorklistItemType::LookForRefDIEsToKeep);
942 
943     bool UseOdr = (Current.Flags & TF_DependencyWalk) ? (Current.Flags & TF_ODR)
944                                                       : Current.CU.hasODR();
945     unsigned ODRFlag = UseOdr ? TF_ODR : 0;
946     unsigned ParFlags = TF_ParentWalk | TF_Keep | TF_DependencyWalk | ODRFlag;
947 
948     // Now schedule the parent walk.
949     Worklist.emplace_back(MyInfo.ParentIdx, Current.CU, ParFlags);
950   }
951 }
952 
953 #ifndef NDEBUG
954 /// A broken link in the keep chain. By recording both the parent and the child
955 /// we can show only broken links for DIEs with multiple children.
956 struct BrokenLink {
957   BrokenLink(DWARFDie Parent, DWARFDie Child) : Parent(Parent), Child(Child) {}
958   DWARFDie Parent;
959   DWARFDie Child;
960 };
961 
962 /// Verify the keep chain by looking for DIEs that are kept but who's parent
963 /// isn't.
964 static void verifyKeepChain(CompileUnit &CU) {
965   std::vector<DWARFDie> Worklist;
966   Worklist.push_back(CU.getOrigUnit().getUnitDIE());
967 
968   // List of broken links.
969   std::vector<BrokenLink> BrokenLinks;
970 
971   while (!Worklist.empty()) {
972     const DWARFDie Current = Worklist.back();
973     Worklist.pop_back();
974 
975     const bool CurrentDieIsKept = CU.getInfo(Current).Keep;
976 
977     for (DWARFDie Child : reverse(Current.children())) {
978       Worklist.push_back(Child);
979 
980       const bool ChildDieIsKept = CU.getInfo(Child).Keep;
981       if (!CurrentDieIsKept && ChildDieIsKept)
982         BrokenLinks.emplace_back(Current, Child);
983     }
984   }
985 
986   if (!BrokenLinks.empty()) {
987     for (BrokenLink Link : BrokenLinks) {
988       WithColor::error() << formatv(
989           "Found invalid link in keep chain between {0:x} and {1:x}\n",
990           Link.Parent.getOffset(), Link.Child.getOffset());
991 
992       errs() << "Parent:";
993       Link.Parent.dump(errs(), 0, {});
994       CU.getInfo(Link.Parent).dump();
995 
996       errs() << "Child:";
997       Link.Child.dump(errs(), 2, {});
998       CU.getInfo(Link.Child).dump();
999     }
1000     report_fatal_error("invalid keep chain");
1001   }
1002 }
1003 #endif
1004 
1005 /// Assign an abbreviation number to \p Abbrev.
1006 ///
1007 /// Our DIEs get freed after every DebugMapObject has been processed,
1008 /// thus the FoldingSet we use to unique DIEAbbrevs cannot refer to
1009 /// the instances hold by the DIEs. When we encounter an abbreviation
1010 /// that we don't know, we create a permanent copy of it.
1011 void DWARFLinker::assignAbbrev(DIEAbbrev &Abbrev) {
1012   // Check the set for priors.
1013   FoldingSetNodeID ID;
1014   Abbrev.Profile(ID);
1015   void *InsertToken;
1016   DIEAbbrev *InSet = AbbreviationsSet.FindNodeOrInsertPos(ID, InsertToken);
1017 
1018   // If it's newly added.
1019   if (InSet) {
1020     // Assign existing abbreviation number.
1021     Abbrev.setNumber(InSet->getNumber());
1022   } else {
1023     // Add to abbreviation list.
1024     Abbreviations.push_back(
1025         std::make_unique<DIEAbbrev>(Abbrev.getTag(), Abbrev.hasChildren()));
1026     for (const auto &Attr : Abbrev.getData())
1027       Abbreviations.back()->AddAttribute(Attr);
1028     AbbreviationsSet.InsertNode(Abbreviations.back().get(), InsertToken);
1029     // Assign the unique abbreviation number.
1030     Abbrev.setNumber(Abbreviations.size());
1031     Abbreviations.back()->setNumber(Abbreviations.size());
1032   }
1033 }
1034 
1035 unsigned DWARFLinker::DIECloner::cloneStringAttribute(DIE &Die,
1036                                                       AttributeSpec AttrSpec,
1037                                                       const DWARFFormValue &Val,
1038                                                       const DWARFUnit &U,
1039                                                       AttributesInfo &Info) {
1040   std::optional<const char *> String = dwarf::toString(Val);
1041   if (!String)
1042     return 0;
1043   DwarfStringPoolEntryRef StringEntry;
1044   if (AttrSpec.Form == dwarf::DW_FORM_line_strp) {
1045     StringEntry = DebugLineStrPool.getEntry(*String);
1046   } else {
1047     StringEntry = DebugStrPool.getEntry(*String);
1048 
1049     if (AttrSpec.Attr == dwarf::DW_AT_APPLE_origin) {
1050       Info.HasAppleOrigin = true;
1051       if (std::optional<StringRef> FileName =
1052               ObjFile.Addresses->getLibraryInstallName()) {
1053         StringEntry = DebugStrPool.getEntry(*FileName);
1054       }
1055     }
1056 
1057     // Update attributes info.
1058     if (AttrSpec.Attr == dwarf::DW_AT_name)
1059       Info.Name = StringEntry;
1060     else if (AttrSpec.Attr == dwarf::DW_AT_MIPS_linkage_name ||
1061              AttrSpec.Attr == dwarf::DW_AT_linkage_name)
1062       Info.MangledName = StringEntry;
1063     if (U.getVersion() >= 5) {
1064       // Switch everything to DW_FORM_strx strings.
1065       auto StringOffsetIndex =
1066           StringOffsetPool.getValueIndex(StringEntry.getOffset());
1067       return Die
1068           .addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1069                     dwarf::DW_FORM_strx, DIEInteger(StringOffsetIndex))
1070           ->sizeOf(U.getFormParams());
1071     }
1072     // Switch everything to out of line strings.
1073     AttrSpec.Form = dwarf::DW_FORM_strp;
1074   }
1075   Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), AttrSpec.Form,
1076                DIEInteger(StringEntry.getOffset()));
1077   return 4;
1078 }
1079 
1080 unsigned DWARFLinker::DIECloner::cloneDieReferenceAttribute(
1081     DIE &Die, const DWARFDie &InputDIE, AttributeSpec AttrSpec,
1082     unsigned AttrSize, const DWARFFormValue &Val, const DWARFFile &File,
1083     CompileUnit &Unit) {
1084   const DWARFUnit &U = Unit.getOrigUnit();
1085   uint64_t Ref = *Val.getAsReference();
1086 
1087   DIE *NewRefDie = nullptr;
1088   CompileUnit *RefUnit = nullptr;
1089 
1090   DWARFDie RefDie =
1091       Linker.resolveDIEReference(File, CompileUnits, Val, InputDIE, RefUnit);
1092 
1093   // If the referenced DIE is not found,  drop the attribute.
1094   if (!RefDie || AttrSpec.Attr == dwarf::DW_AT_sibling)
1095     return 0;
1096 
1097   CompileUnit::DIEInfo &RefInfo = RefUnit->getInfo(RefDie);
1098 
1099   // If we already have emitted an equivalent DeclContext, just point
1100   // at it.
1101   if (isODRAttribute(AttrSpec.Attr) && RefInfo.Ctxt &&
1102       RefInfo.Ctxt->getCanonicalDIEOffset()) {
1103     assert(RefInfo.Ctxt->hasCanonicalDIE() &&
1104            "Offset to canonical die is set, but context is not marked");
1105     DIEInteger Attr(RefInfo.Ctxt->getCanonicalDIEOffset());
1106     Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1107                  dwarf::DW_FORM_ref_addr, Attr);
1108     return U.getRefAddrByteSize();
1109   }
1110 
1111   if (!RefInfo.Clone) {
1112     // We haven't cloned this DIE yet. Just create an empty one and
1113     // store it. It'll get really cloned when we process it.
1114     RefInfo.UnclonedReference = true;
1115     RefInfo.Clone = DIE::get(DIEAlloc, dwarf::Tag(RefDie.getTag()));
1116   }
1117   NewRefDie = RefInfo.Clone;
1118 
1119   if (AttrSpec.Form == dwarf::DW_FORM_ref_addr ||
1120       (Unit.hasODR() && isODRAttribute(AttrSpec.Attr))) {
1121     // We cannot currently rely on a DIEEntry to emit ref_addr
1122     // references, because the implementation calls back to DwarfDebug
1123     // to find the unit offset. (We don't have a DwarfDebug)
1124     // FIXME: we should be able to design DIEEntry reliance on
1125     // DwarfDebug away.
1126     uint64_t Attr;
1127     if (Ref < InputDIE.getOffset() && !RefInfo.UnclonedReference) {
1128       // We have already cloned that DIE.
1129       uint32_t NewRefOffset =
1130           RefUnit->getStartOffset() + NewRefDie->getOffset();
1131       Attr = NewRefOffset;
1132       Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1133                    dwarf::DW_FORM_ref_addr, DIEInteger(Attr));
1134     } else {
1135       // A forward reference. Note and fixup later.
1136       Attr = 0xBADDEF;
1137       Unit.noteForwardReference(
1138           NewRefDie, RefUnit, RefInfo.Ctxt,
1139           Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1140                        dwarf::DW_FORM_ref_addr, DIEInteger(Attr)));
1141     }
1142     return U.getRefAddrByteSize();
1143   }
1144 
1145   Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1146                dwarf::Form(AttrSpec.Form), DIEEntry(*NewRefDie));
1147 
1148   return AttrSize;
1149 }
1150 
1151 void DWARFLinker::DIECloner::cloneExpression(
1152     DataExtractor &Data, DWARFExpression Expression, const DWARFFile &File,
1153     CompileUnit &Unit, SmallVectorImpl<uint8_t> &OutputBuffer,
1154     int64_t AddrRelocAdjustment, bool IsLittleEndian) {
1155   using Encoding = DWARFExpression::Operation::Encoding;
1156 
1157   uint8_t OrigAddressByteSize = Unit.getOrigUnit().getAddressByteSize();
1158 
1159   uint64_t OpOffset = 0;
1160   for (auto &Op : Expression) {
1161     auto Desc = Op.getDescription();
1162     // DW_OP_const_type is variable-length and has 3
1163     // operands. Thus far we only support 2.
1164     if ((Desc.Op.size() == 2 && Desc.Op[0] == Encoding::BaseTypeRef) ||
1165         (Desc.Op.size() == 2 && Desc.Op[1] == Encoding::BaseTypeRef &&
1166          Desc.Op[0] != Encoding::Size1))
1167       Linker.reportWarning("Unsupported DW_OP encoding.", File);
1168 
1169     if ((Desc.Op.size() == 1 && Desc.Op[0] == Encoding::BaseTypeRef) ||
1170         (Desc.Op.size() == 2 && Desc.Op[1] == Encoding::BaseTypeRef &&
1171          Desc.Op[0] == Encoding::Size1)) {
1172       // This code assumes that the other non-typeref operand fits into 1 byte.
1173       assert(OpOffset < Op.getEndOffset());
1174       uint32_t ULEBsize = Op.getEndOffset() - OpOffset - 1;
1175       assert(ULEBsize <= 16);
1176 
1177       // Copy over the operation.
1178       assert(!Op.getSubCode() && "SubOps not yet supported");
1179       OutputBuffer.push_back(Op.getCode());
1180       uint64_t RefOffset;
1181       if (Desc.Op.size() == 1) {
1182         RefOffset = Op.getRawOperand(0);
1183       } else {
1184         OutputBuffer.push_back(Op.getRawOperand(0));
1185         RefOffset = Op.getRawOperand(1);
1186       }
1187       uint32_t Offset = 0;
1188       // Look up the base type. For DW_OP_convert, the operand may be 0 to
1189       // instead indicate the generic type. The same holds for
1190       // DW_OP_reinterpret, which is currently not supported.
1191       if (RefOffset > 0 || Op.getCode() != dwarf::DW_OP_convert) {
1192         RefOffset += Unit.getOrigUnit().getOffset();
1193         auto RefDie = Unit.getOrigUnit().getDIEForOffset(RefOffset);
1194         CompileUnit::DIEInfo &Info = Unit.getInfo(RefDie);
1195         if (DIE *Clone = Info.Clone)
1196           Offset = Clone->getOffset();
1197         else
1198           Linker.reportWarning(
1199               "base type ref doesn't point to DW_TAG_base_type.", File);
1200       }
1201       uint8_t ULEB[16];
1202       unsigned RealSize = encodeULEB128(Offset, ULEB, ULEBsize);
1203       if (RealSize > ULEBsize) {
1204         // Emit the generic type as a fallback.
1205         RealSize = encodeULEB128(0, ULEB, ULEBsize);
1206         Linker.reportWarning("base type ref doesn't fit.", File);
1207       }
1208       assert(RealSize == ULEBsize && "padding failed");
1209       ArrayRef<uint8_t> ULEBbytes(ULEB, ULEBsize);
1210       OutputBuffer.append(ULEBbytes.begin(), ULEBbytes.end());
1211     } else if (!Linker.Options.Update && Op.getCode() == dwarf::DW_OP_addrx) {
1212       if (std::optional<object::SectionedAddress> SA =
1213               Unit.getOrigUnit().getAddrOffsetSectionItem(
1214                   Op.getRawOperand(0))) {
1215         // DWARFLinker does not use addrx forms since it generates relocated
1216         // addresses. Replace DW_OP_addrx with DW_OP_addr here.
1217         // Argument of DW_OP_addrx should be relocated here as it is not
1218         // processed by applyValidRelocs.
1219         OutputBuffer.push_back(dwarf::DW_OP_addr);
1220         uint64_t LinkedAddress = SA->Address + AddrRelocAdjustment;
1221         if (IsLittleEndian != sys::IsLittleEndianHost)
1222           sys::swapByteOrder(LinkedAddress);
1223         ArrayRef<uint8_t> AddressBytes(
1224             reinterpret_cast<const uint8_t *>(&LinkedAddress),
1225             OrigAddressByteSize);
1226         OutputBuffer.append(AddressBytes.begin(), AddressBytes.end());
1227       } else
1228         Linker.reportWarning("cannot read DW_OP_addrx operand.", File);
1229     } else if (!Linker.Options.Update && Op.getCode() == dwarf::DW_OP_constx) {
1230       if (std::optional<object::SectionedAddress> SA =
1231               Unit.getOrigUnit().getAddrOffsetSectionItem(
1232                   Op.getRawOperand(0))) {
1233         // DWARFLinker does not use constx forms since it generates relocated
1234         // addresses. Replace DW_OP_constx with DW_OP_const[*]u here.
1235         // Argument of DW_OP_constx should be relocated here as it is not
1236         // processed by applyValidRelocs.
1237         std::optional<uint8_t> OutOperandKind;
1238         switch (OrigAddressByteSize) {
1239         case 4:
1240           OutOperandKind = dwarf::DW_OP_const4u;
1241           break;
1242         case 8:
1243           OutOperandKind = dwarf::DW_OP_const8u;
1244           break;
1245         default:
1246           Linker.reportWarning(
1247               formatv(("unsupported address size: {0}."), OrigAddressByteSize),
1248               File);
1249           break;
1250         }
1251 
1252         if (OutOperandKind) {
1253           OutputBuffer.push_back(*OutOperandKind);
1254           uint64_t LinkedAddress = SA->Address + AddrRelocAdjustment;
1255           if (IsLittleEndian != sys::IsLittleEndianHost)
1256             sys::swapByteOrder(LinkedAddress);
1257           ArrayRef<uint8_t> AddressBytes(
1258               reinterpret_cast<const uint8_t *>(&LinkedAddress),
1259               OrigAddressByteSize);
1260           OutputBuffer.append(AddressBytes.begin(), AddressBytes.end());
1261         }
1262       } else
1263         Linker.reportWarning("cannot read DW_OP_constx operand.", File);
1264     } else {
1265       // Copy over everything else unmodified.
1266       StringRef Bytes = Data.getData().slice(OpOffset, Op.getEndOffset());
1267       OutputBuffer.append(Bytes.begin(), Bytes.end());
1268     }
1269     OpOffset = Op.getEndOffset();
1270   }
1271 }
1272 
1273 unsigned DWARFLinker::DIECloner::cloneBlockAttribute(
1274     DIE &Die, const DWARFDie &InputDIE, const DWARFFile &File,
1275     CompileUnit &Unit, AttributeSpec AttrSpec, const DWARFFormValue &Val,
1276     bool IsLittleEndian) {
1277   DIEValueList *Attr;
1278   DIEValue Value;
1279   DIELoc *Loc = nullptr;
1280   DIEBlock *Block = nullptr;
1281   if (AttrSpec.Form == dwarf::DW_FORM_exprloc) {
1282     Loc = new (DIEAlloc) DIELoc;
1283     Linker.DIELocs.push_back(Loc);
1284   } else {
1285     Block = new (DIEAlloc) DIEBlock;
1286     Linker.DIEBlocks.push_back(Block);
1287   }
1288   Attr = Loc ? static_cast<DIEValueList *>(Loc)
1289              : static_cast<DIEValueList *>(Block);
1290 
1291   DWARFUnit &OrigUnit = Unit.getOrigUnit();
1292   // If the block is a DWARF Expression, clone it into the temporary
1293   // buffer using cloneExpression(), otherwise copy the data directly.
1294   SmallVector<uint8_t, 32> Buffer;
1295   ArrayRef<uint8_t> Bytes = *Val.getAsBlock();
1296   if (DWARFAttribute::mayHaveLocationExpr(AttrSpec.Attr) &&
1297       (Val.isFormClass(DWARFFormValue::FC_Block) ||
1298        Val.isFormClass(DWARFFormValue::FC_Exprloc))) {
1299     DataExtractor Data(StringRef((const char *)Bytes.data(), Bytes.size()),
1300                        IsLittleEndian, OrigUnit.getAddressByteSize());
1301     DWARFExpression Expr(Data, OrigUnit.getAddressByteSize(),
1302                          OrigUnit.getFormParams().Format);
1303     cloneExpression(Data, Expr, File, Unit, Buffer,
1304                     Unit.getInfo(InputDIE).AddrAdjust, IsLittleEndian);
1305     Bytes = Buffer;
1306   }
1307   for (auto Byte : Bytes)
1308     Attr->addValue(DIEAlloc, static_cast<dwarf::Attribute>(0),
1309                    dwarf::DW_FORM_data1, DIEInteger(Byte));
1310 
1311   // FIXME: If DIEBlock and DIELoc just reuses the Size field of
1312   // the DIE class, this "if" could be replaced by
1313   // Attr->setSize(Bytes.size()).
1314   if (Loc)
1315     Loc->setSize(Bytes.size());
1316   else
1317     Block->setSize(Bytes.size());
1318 
1319   if (Loc)
1320     Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
1321                      dwarf::Form(AttrSpec.Form), Loc);
1322   else {
1323     // The expression location data might be updated and exceed the original
1324     // size. Check whether the new data fits into the original form.
1325     if ((AttrSpec.Form == dwarf::DW_FORM_block1 &&
1326          (Bytes.size() > UINT8_MAX)) ||
1327         (AttrSpec.Form == dwarf::DW_FORM_block2 &&
1328          (Bytes.size() > UINT16_MAX)) ||
1329         (AttrSpec.Form == dwarf::DW_FORM_block4 && (Bytes.size() > UINT32_MAX)))
1330       AttrSpec.Form = dwarf::DW_FORM_block;
1331 
1332     Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
1333                      dwarf::Form(AttrSpec.Form), Block);
1334   }
1335 
1336   return Die.addValue(DIEAlloc, Value)->sizeOf(OrigUnit.getFormParams());
1337 }
1338 
1339 unsigned DWARFLinker::DIECloner::cloneAddressAttribute(
1340     DIE &Die, const DWARFDie &InputDIE, AttributeSpec AttrSpec,
1341     unsigned AttrSize, const DWARFFormValue &Val, const CompileUnit &Unit,
1342     AttributesInfo &Info) {
1343   if (AttrSpec.Attr == dwarf::DW_AT_low_pc)
1344     Info.HasLowPc = true;
1345 
1346   if (LLVM_UNLIKELY(Linker.Options.Update)) {
1347     Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1348                  dwarf::Form(AttrSpec.Form), DIEInteger(Val.getRawUValue()));
1349     return AttrSize;
1350   }
1351 
1352   // Cloned Die may have address attributes relocated to a
1353   // totally unrelated value. This can happen:
1354   //   - If high_pc is an address (Dwarf version == 2), then it might have been
1355   //     relocated to a totally unrelated value (because the end address in the
1356   //     object file might be start address of another function which got moved
1357   //     independently by the linker).
1358   //   - If address relocated in an inline_subprogram that happens at the
1359   //     beginning of its inlining function.
1360   //  To avoid above cases and to not apply relocation twice (in
1361   //  applyValidRelocs and here), read address attribute from InputDIE and apply
1362   //  Info.PCOffset here.
1363 
1364   std::optional<DWARFFormValue> AddrAttribute = InputDIE.find(AttrSpec.Attr);
1365   if (!AddrAttribute)
1366     llvm_unreachable("Cann't find attribute.");
1367 
1368   std::optional<uint64_t> Addr = AddrAttribute->getAsAddress();
1369   if (!Addr) {
1370     Linker.reportWarning("Cann't read address attribute value.", ObjFile);
1371     return 0;
1372   }
1373 
1374   if (InputDIE.getTag() == dwarf::DW_TAG_compile_unit &&
1375       AttrSpec.Attr == dwarf::DW_AT_low_pc) {
1376     if (std::optional<uint64_t> LowPC = Unit.getLowPc())
1377       Addr = *LowPC;
1378     else
1379       return 0;
1380   } else if (InputDIE.getTag() == dwarf::DW_TAG_compile_unit &&
1381              AttrSpec.Attr == dwarf::DW_AT_high_pc) {
1382     if (uint64_t HighPc = Unit.getHighPc())
1383       Addr = HighPc;
1384     else
1385       return 0;
1386   } else {
1387     *Addr += Info.PCOffset;
1388   }
1389 
1390   if (AttrSpec.Form == dwarf::DW_FORM_addr) {
1391     Die.addValue(DIEAlloc, static_cast<dwarf::Attribute>(AttrSpec.Attr),
1392                  AttrSpec.Form, DIEInteger(*Addr));
1393     return Unit.getOrigUnit().getAddressByteSize();
1394   }
1395 
1396   auto AddrIndex = AddrPool.getValueIndex(*Addr);
1397 
1398   return Die
1399       .addValue(DIEAlloc, static_cast<dwarf::Attribute>(AttrSpec.Attr),
1400                 dwarf::Form::DW_FORM_addrx, DIEInteger(AddrIndex))
1401       ->sizeOf(Unit.getOrigUnit().getFormParams());
1402 }
1403 
1404 unsigned DWARFLinker::DIECloner::cloneScalarAttribute(
1405     DIE &Die, const DWARFDie &InputDIE, const DWARFFile &File,
1406     CompileUnit &Unit, AttributeSpec AttrSpec, const DWARFFormValue &Val,
1407     unsigned AttrSize, AttributesInfo &Info) {
1408   uint64_t Value;
1409 
1410   // Check for the offset to the macro table. If offset is incorrect then we
1411   // need to remove the attribute.
1412   if (AttrSpec.Attr == dwarf::DW_AT_macro_info) {
1413     if (std::optional<uint64_t> Offset = Val.getAsSectionOffset()) {
1414       const llvm::DWARFDebugMacro *Macro = File.Dwarf->getDebugMacinfo();
1415       if (Macro == nullptr || !Macro->hasEntryForOffset(*Offset))
1416         return 0;
1417     }
1418   }
1419 
1420   if (AttrSpec.Attr == dwarf::DW_AT_macros) {
1421     if (std::optional<uint64_t> Offset = Val.getAsSectionOffset()) {
1422       const llvm::DWARFDebugMacro *Macro = File.Dwarf->getDebugMacro();
1423       if (Macro == nullptr || !Macro->hasEntryForOffset(*Offset))
1424         return 0;
1425     }
1426   }
1427 
1428   if (AttrSpec.Attr == dwarf::DW_AT_str_offsets_base) {
1429     // DWARFLinker generates common .debug_str_offsets table used for all
1430     // compile units. The offset to the common .debug_str_offsets table is 8 on
1431     // DWARF32.
1432     Info.AttrStrOffsetBaseSeen = true;
1433     return Die
1434         .addValue(DIEAlloc, dwarf::DW_AT_str_offsets_base,
1435                   dwarf::DW_FORM_sec_offset, DIEInteger(8))
1436         ->sizeOf(Unit.getOrigUnit().getFormParams());
1437   }
1438 
1439   if (LLVM_UNLIKELY(Linker.Options.Update)) {
1440     if (auto OptionalValue = Val.getAsUnsignedConstant())
1441       Value = *OptionalValue;
1442     else if (auto OptionalValue = Val.getAsSignedConstant())
1443       Value = *OptionalValue;
1444     else if (auto OptionalValue = Val.getAsSectionOffset())
1445       Value = *OptionalValue;
1446     else {
1447       Linker.reportWarning(
1448           "Unsupported scalar attribute form. Dropping attribute.", File,
1449           &InputDIE);
1450       return 0;
1451     }
1452     if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
1453       Info.IsDeclaration = true;
1454 
1455     if (AttrSpec.Form == dwarf::DW_FORM_loclistx)
1456       Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1457                    dwarf::Form(AttrSpec.Form), DIELocList(Value));
1458     else
1459       Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1460                    dwarf::Form(AttrSpec.Form), DIEInteger(Value));
1461     return AttrSize;
1462   }
1463 
1464   [[maybe_unused]] dwarf::Form OriginalForm = AttrSpec.Form;
1465   if (AttrSpec.Form == dwarf::DW_FORM_rnglistx) {
1466     // DWARFLinker does not generate .debug_addr table. Thus we need to change
1467     // all "addrx" related forms to "addr" version. Change DW_FORM_rnglistx
1468     // to DW_FORM_sec_offset here.
1469     std::optional<uint64_t> Index = Val.getAsSectionOffset();
1470     if (!Index) {
1471       Linker.reportWarning("Cannot read the attribute. Dropping.", File,
1472                            &InputDIE);
1473       return 0;
1474     }
1475     std::optional<uint64_t> Offset =
1476         Unit.getOrigUnit().getRnglistOffset(*Index);
1477     if (!Offset) {
1478       Linker.reportWarning("Cannot read the attribute. Dropping.", File,
1479                            &InputDIE);
1480       return 0;
1481     }
1482 
1483     Value = *Offset;
1484     AttrSpec.Form = dwarf::DW_FORM_sec_offset;
1485     AttrSize = Unit.getOrigUnit().getFormParams().getDwarfOffsetByteSize();
1486   } else if (AttrSpec.Form == dwarf::DW_FORM_loclistx) {
1487     // DWARFLinker does not generate .debug_addr table. Thus we need to change
1488     // all "addrx" related forms to "addr" version. Change DW_FORM_loclistx
1489     // to DW_FORM_sec_offset here.
1490     std::optional<uint64_t> Index = Val.getAsSectionOffset();
1491     if (!Index) {
1492       Linker.reportWarning("Cannot read the attribute. Dropping.", File,
1493                            &InputDIE);
1494       return 0;
1495     }
1496     std::optional<uint64_t> Offset =
1497         Unit.getOrigUnit().getLoclistOffset(*Index);
1498     if (!Offset) {
1499       Linker.reportWarning("Cannot read the attribute. Dropping.", File,
1500                            &InputDIE);
1501       return 0;
1502     }
1503 
1504     Value = *Offset;
1505     AttrSpec.Form = dwarf::DW_FORM_sec_offset;
1506     AttrSize = Unit.getOrigUnit().getFormParams().getDwarfOffsetByteSize();
1507   } else if (AttrSpec.Attr == dwarf::DW_AT_high_pc &&
1508              Die.getTag() == dwarf::DW_TAG_compile_unit) {
1509     std::optional<uint64_t> LowPC = Unit.getLowPc();
1510     if (!LowPC)
1511       return 0;
1512     // Dwarf >= 4 high_pc is an size, not an address.
1513     Value = Unit.getHighPc() - *LowPC;
1514   } else if (AttrSpec.Form == dwarf::DW_FORM_sec_offset)
1515     Value = *Val.getAsSectionOffset();
1516   else if (AttrSpec.Form == dwarf::DW_FORM_sdata)
1517     Value = *Val.getAsSignedConstant();
1518   else if (auto OptionalValue = Val.getAsUnsignedConstant())
1519     Value = *OptionalValue;
1520   else {
1521     Linker.reportWarning(
1522         "Unsupported scalar attribute form. Dropping attribute.", File,
1523         &InputDIE);
1524     return 0;
1525   }
1526 
1527   DIE::value_iterator Patch =
1528       Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1529                    dwarf::Form(AttrSpec.Form), DIEInteger(Value));
1530   if (AttrSpec.Attr == dwarf::DW_AT_ranges ||
1531       AttrSpec.Attr == dwarf::DW_AT_start_scope) {
1532     Unit.noteRangeAttribute(Die, Patch);
1533     Info.HasRanges = true;
1534   } else if (DWARFAttribute::mayHaveLocationList(AttrSpec.Attr) &&
1535              dwarf::doesFormBelongToClass(AttrSpec.Form,
1536                                           DWARFFormValue::FC_SectionOffset,
1537                                           Unit.getOrigUnit().getVersion())) {
1538 
1539     CompileUnit::DIEInfo &LocationDieInfo = Unit.getInfo(InputDIE);
1540     Unit.noteLocationAttribute({Patch, LocationDieInfo.InDebugMap
1541                                            ? LocationDieInfo.AddrAdjust
1542                                            : Info.PCOffset});
1543   } else if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
1544     Info.IsDeclaration = true;
1545 
1546   // check that all dwarf::DW_FORM_rnglistx are handled previously.
1547   assert((Info.HasRanges || (OriginalForm != dwarf::DW_FORM_rnglistx)) &&
1548          "Unhandled DW_FORM_rnglistx attribute");
1549 
1550   return AttrSize;
1551 }
1552 
1553 /// Clone \p InputDIE's attribute described by \p AttrSpec with
1554 /// value \p Val, and add it to \p Die.
1555 /// \returns the size of the cloned attribute.
1556 unsigned DWARFLinker::DIECloner::cloneAttribute(
1557     DIE &Die, const DWARFDie &InputDIE, const DWARFFile &File,
1558     CompileUnit &Unit, const DWARFFormValue &Val, const AttributeSpec AttrSpec,
1559     unsigned AttrSize, AttributesInfo &Info, bool IsLittleEndian) {
1560   const DWARFUnit &U = Unit.getOrigUnit();
1561 
1562   switch (AttrSpec.Form) {
1563   case dwarf::DW_FORM_strp:
1564   case dwarf::DW_FORM_line_strp:
1565   case dwarf::DW_FORM_string:
1566   case dwarf::DW_FORM_strx:
1567   case dwarf::DW_FORM_strx1:
1568   case dwarf::DW_FORM_strx2:
1569   case dwarf::DW_FORM_strx3:
1570   case dwarf::DW_FORM_strx4:
1571     return cloneStringAttribute(Die, AttrSpec, Val, U, Info);
1572   case dwarf::DW_FORM_ref_addr:
1573   case dwarf::DW_FORM_ref1:
1574   case dwarf::DW_FORM_ref2:
1575   case dwarf::DW_FORM_ref4:
1576   case dwarf::DW_FORM_ref8:
1577     return cloneDieReferenceAttribute(Die, InputDIE, AttrSpec, AttrSize, Val,
1578                                       File, Unit);
1579   case dwarf::DW_FORM_block:
1580   case dwarf::DW_FORM_block1:
1581   case dwarf::DW_FORM_block2:
1582   case dwarf::DW_FORM_block4:
1583   case dwarf::DW_FORM_exprloc:
1584     return cloneBlockAttribute(Die, InputDIE, File, Unit, AttrSpec, Val,
1585                                IsLittleEndian);
1586   case dwarf::DW_FORM_addr:
1587   case dwarf::DW_FORM_addrx:
1588   case dwarf::DW_FORM_addrx1:
1589   case dwarf::DW_FORM_addrx2:
1590   case dwarf::DW_FORM_addrx3:
1591   case dwarf::DW_FORM_addrx4:
1592     return cloneAddressAttribute(Die, InputDIE, AttrSpec, AttrSize, Val, Unit,
1593                                  Info);
1594   case dwarf::DW_FORM_data1:
1595   case dwarf::DW_FORM_data2:
1596   case dwarf::DW_FORM_data4:
1597   case dwarf::DW_FORM_data8:
1598   case dwarf::DW_FORM_udata:
1599   case dwarf::DW_FORM_sdata:
1600   case dwarf::DW_FORM_sec_offset:
1601   case dwarf::DW_FORM_flag:
1602   case dwarf::DW_FORM_flag_present:
1603   case dwarf::DW_FORM_rnglistx:
1604   case dwarf::DW_FORM_loclistx:
1605   case dwarf::DW_FORM_implicit_const:
1606     return cloneScalarAttribute(Die, InputDIE, File, Unit, AttrSpec, Val,
1607                                 AttrSize, Info);
1608   default:
1609     Linker.reportWarning("Unsupported attribute form " +
1610                              dwarf::FormEncodingString(AttrSpec.Form) +
1611                              " in cloneAttribute. Dropping.",
1612                          File, &InputDIE);
1613   }
1614 
1615   return 0;
1616 }
1617 
1618 void DWARFLinker::DIECloner::addObjCAccelerator(CompileUnit &Unit,
1619                                                 const DIE *Die,
1620                                                 DwarfStringPoolEntryRef Name,
1621                                                 OffsetsStringPool &StringPool,
1622                                                 bool SkipPubSection) {
1623   std::optional<ObjCSelectorNames> Names =
1624       getObjCNamesIfSelector(Name.getString());
1625   if (!Names)
1626     return;
1627   Unit.addNameAccelerator(Die, StringPool.getEntry(Names->Selector),
1628                           SkipPubSection);
1629   Unit.addObjCAccelerator(Die, StringPool.getEntry(Names->ClassName),
1630                           SkipPubSection);
1631   if (Names->ClassNameNoCategory)
1632     Unit.addObjCAccelerator(
1633         Die, StringPool.getEntry(*Names->ClassNameNoCategory), SkipPubSection);
1634   if (Names->MethodNameNoCategory)
1635     Unit.addNameAccelerator(
1636         Die, StringPool.getEntry(*Names->MethodNameNoCategory), SkipPubSection);
1637 }
1638 
1639 static bool
1640 shouldSkipAttribute(bool Update,
1641                     DWARFAbbreviationDeclaration::AttributeSpec AttrSpec,
1642                     bool SkipPC) {
1643   switch (AttrSpec.Attr) {
1644   default:
1645     return false;
1646   case dwarf::DW_AT_low_pc:
1647   case dwarf::DW_AT_high_pc:
1648   case dwarf::DW_AT_ranges:
1649     return !Update && SkipPC;
1650   case dwarf::DW_AT_rnglists_base:
1651     // In case !Update the .debug_addr table is not generated/preserved.
1652     // Thus instead of DW_FORM_rnglistx the DW_FORM_sec_offset is used.
1653     // Since DW_AT_rnglists_base is used for only DW_FORM_rnglistx the
1654     // DW_AT_rnglists_base is removed.
1655     return !Update;
1656   case dwarf::DW_AT_loclists_base:
1657     // In case !Update the .debug_addr table is not generated/preserved.
1658     // Thus instead of DW_FORM_loclistx the DW_FORM_sec_offset is used.
1659     // Since DW_AT_loclists_base is used for only DW_FORM_loclistx the
1660     // DW_AT_loclists_base is removed.
1661     return !Update;
1662   case dwarf::DW_AT_location:
1663   case dwarf::DW_AT_frame_base:
1664     return !Update && SkipPC;
1665   }
1666 }
1667 
1668 struct AttributeLinkedOffsetFixup {
1669   int64_t LinkedOffsetFixupVal;
1670   uint64_t InputAttrStartOffset;
1671   uint64_t InputAttrEndOffset;
1672 };
1673 
1674 DIE *DWARFLinker::DIECloner::cloneDIE(const DWARFDie &InputDIE,
1675                                       const DWARFFile &File, CompileUnit &Unit,
1676                                       int64_t PCOffset, uint32_t OutOffset,
1677                                       unsigned Flags, bool IsLittleEndian,
1678                                       DIE *Die) {
1679   DWARFUnit &U = Unit.getOrigUnit();
1680   unsigned Idx = U.getDIEIndex(InputDIE);
1681   CompileUnit::DIEInfo &Info = Unit.getInfo(Idx);
1682 
1683   // Should the DIE appear in the output?
1684   if (!Unit.getInfo(Idx).Keep)
1685     return nullptr;
1686 
1687   uint64_t Offset = InputDIE.getOffset();
1688   assert(!(Die && Info.Clone) && "Can't supply a DIE and a cloned DIE");
1689   if (!Die) {
1690     // The DIE might have been already created by a forward reference
1691     // (see cloneDieReferenceAttribute()).
1692     if (!Info.Clone)
1693       Info.Clone = DIE::get(DIEAlloc, dwarf::Tag(InputDIE.getTag()));
1694     Die = Info.Clone;
1695   }
1696 
1697   assert(Die->getTag() == InputDIE.getTag());
1698   Die->setOffset(OutOffset);
1699   if (isODRCanonicalCandidate(InputDIE, Unit) && Info.Ctxt &&
1700       (Info.Ctxt->getCanonicalDIEOffset() == 0)) {
1701     if (!Info.Ctxt->hasCanonicalDIE())
1702       Info.Ctxt->setHasCanonicalDIE();
1703     // We are about to emit a DIE that is the root of its own valid
1704     // DeclContext tree. Make the current offset the canonical offset
1705     // for this context.
1706     Info.Ctxt->setCanonicalDIEOffset(OutOffset + Unit.getStartOffset());
1707   }
1708 
1709   // Extract and clone every attribute.
1710   DWARFDataExtractor Data = U.getDebugInfoExtractor();
1711   // Point to the next DIE (generally there is always at least a NULL
1712   // entry after the current one). If this is a lone
1713   // DW_TAG_compile_unit without any children, point to the next unit.
1714   uint64_t NextOffset = (Idx + 1 < U.getNumDIEs())
1715                             ? U.getDIEAtIndex(Idx + 1).getOffset()
1716                             : U.getNextUnitOffset();
1717   AttributesInfo AttrInfo;
1718 
1719   // We could copy the data only if we need to apply a relocation to it. After
1720   // testing, it seems there is no performance downside to doing the copy
1721   // unconditionally, and it makes the code simpler.
1722   SmallString<40> DIECopy(Data.getData().substr(Offset, NextOffset - Offset));
1723   Data =
1724       DWARFDataExtractor(DIECopy, Data.isLittleEndian(), Data.getAddressSize());
1725 
1726   // Modify the copy with relocated addresses.
1727   ObjFile.Addresses->applyValidRelocs(DIECopy, Offset, Data.isLittleEndian());
1728 
1729   // Reset the Offset to 0 as we will be working on the local copy of
1730   // the data.
1731   Offset = 0;
1732 
1733   const auto *Abbrev = InputDIE.getAbbreviationDeclarationPtr();
1734   Offset += getULEB128Size(Abbrev->getCode());
1735 
1736   // We are entering a subprogram. Get and propagate the PCOffset.
1737   if (Die->getTag() == dwarf::DW_TAG_subprogram)
1738     PCOffset = Info.AddrAdjust;
1739   AttrInfo.PCOffset = PCOffset;
1740 
1741   if (Abbrev->getTag() == dwarf::DW_TAG_subprogram) {
1742     Flags |= TF_InFunctionScope;
1743     if (!Info.InDebugMap && LLVM_LIKELY(!Update))
1744       Flags |= TF_SkipPC;
1745   } else if (Abbrev->getTag() == dwarf::DW_TAG_variable) {
1746     // Function-local globals could be in the debug map even when the function
1747     // is not, e.g., inlined functions.
1748     if ((Flags & TF_InFunctionScope) && Info.InDebugMap)
1749       Flags &= ~TF_SkipPC;
1750     // Location expressions referencing an address which is not in debug map
1751     // should be deleted.
1752     else if (!Info.InDebugMap && Info.HasLocationExpressionAddr &&
1753              LLVM_LIKELY(!Update))
1754       Flags |= TF_SkipPC;
1755   }
1756 
1757   std::optional<StringRef> LibraryInstallName =
1758       ObjFile.Addresses->getLibraryInstallName();
1759   SmallVector<AttributeLinkedOffsetFixup> AttributesFixups;
1760   for (const auto &AttrSpec : Abbrev->attributes()) {
1761     if (shouldSkipAttribute(Update, AttrSpec, Flags & TF_SkipPC)) {
1762       DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset,
1763                                 U.getFormParams());
1764       continue;
1765     }
1766 
1767     AttributeLinkedOffsetFixup CurAttrFixup;
1768     CurAttrFixup.InputAttrStartOffset = InputDIE.getOffset() + Offset;
1769     CurAttrFixup.LinkedOffsetFixupVal =
1770         Unit.getStartOffset() + OutOffset - CurAttrFixup.InputAttrStartOffset;
1771 
1772     DWARFFormValue Val = AttrSpec.getFormValue();
1773     uint64_t AttrSize = Offset;
1774     Val.extractValue(Data, &Offset, U.getFormParams(), &U);
1775     CurAttrFixup.InputAttrEndOffset = InputDIE.getOffset() + Offset;
1776     AttrSize = Offset - AttrSize;
1777 
1778     uint64_t FinalAttrSize =
1779         cloneAttribute(*Die, InputDIE, File, Unit, Val, AttrSpec, AttrSize,
1780                        AttrInfo, IsLittleEndian);
1781     if (FinalAttrSize != 0 && ObjFile.Addresses->needToSaveValidRelocs())
1782       AttributesFixups.push_back(CurAttrFixup);
1783 
1784     OutOffset += FinalAttrSize;
1785   }
1786 
1787   uint16_t Tag = InputDIE.getTag();
1788   // Add the DW_AT_APPLE_origin attribute to Compile Unit die if we have
1789   // an install name and the DWARF doesn't have the attribute yet.
1790   const bool NeedsAppleOrigin = (Tag == dwarf::DW_TAG_compile_unit) &&
1791                                 LibraryInstallName.has_value() &&
1792                                 !AttrInfo.HasAppleOrigin;
1793   if (NeedsAppleOrigin) {
1794     auto StringEntry = DebugStrPool.getEntry(LibraryInstallName.value());
1795     Die->addValue(DIEAlloc, dwarf::Attribute(dwarf::DW_AT_APPLE_origin),
1796                   dwarf::DW_FORM_strp, DIEInteger(StringEntry.getOffset()));
1797     AttrInfo.Name = StringEntry;
1798     OutOffset += 4;
1799   }
1800 
1801   // Look for accelerator entries.
1802   // FIXME: This is slightly wrong. An inline_subroutine without a
1803   // low_pc, but with AT_ranges might be interesting to get into the
1804   // accelerator tables too. For now stick with dsymutil's behavior.
1805   if ((Info.InDebugMap || AttrInfo.HasLowPc || AttrInfo.HasRanges) &&
1806       Tag != dwarf::DW_TAG_compile_unit &&
1807       getDIENames(InputDIE, AttrInfo, DebugStrPool,
1808                   Tag != dwarf::DW_TAG_inlined_subroutine)) {
1809     if (AttrInfo.MangledName && AttrInfo.MangledName != AttrInfo.Name)
1810       Unit.addNameAccelerator(Die, AttrInfo.MangledName,
1811                               Tag == dwarf::DW_TAG_inlined_subroutine);
1812     if (AttrInfo.Name) {
1813       if (AttrInfo.NameWithoutTemplate)
1814         Unit.addNameAccelerator(Die, AttrInfo.NameWithoutTemplate,
1815                                 /* SkipPubSection */ true);
1816       Unit.addNameAccelerator(Die, AttrInfo.Name,
1817                               Tag == dwarf::DW_TAG_inlined_subroutine);
1818     }
1819     if (AttrInfo.Name)
1820       addObjCAccelerator(Unit, Die, AttrInfo.Name, DebugStrPool,
1821                          /* SkipPubSection =*/true);
1822 
1823   } else if (Tag == dwarf::DW_TAG_namespace) {
1824     if (!AttrInfo.Name)
1825       AttrInfo.Name = DebugStrPool.getEntry("(anonymous namespace)");
1826     Unit.addNamespaceAccelerator(Die, AttrInfo.Name);
1827   } else if (Tag == dwarf::DW_TAG_imported_declaration && AttrInfo.Name) {
1828     Unit.addNamespaceAccelerator(Die, AttrInfo.Name);
1829   } else if (isTypeTag(Tag) && !AttrInfo.IsDeclaration &&
1830              getDIENames(InputDIE, AttrInfo, DebugStrPool) && AttrInfo.Name &&
1831              AttrInfo.Name.getString()[0]) {
1832     uint32_t Hash = hashFullyQualifiedName(InputDIE, Unit, File);
1833     uint64_t RuntimeLang =
1834         dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_runtime_class))
1835             .value_or(0);
1836     bool ObjCClassIsImplementation =
1837         (RuntimeLang == dwarf::DW_LANG_ObjC ||
1838          RuntimeLang == dwarf::DW_LANG_ObjC_plus_plus) &&
1839         dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_objc_complete_type))
1840             .value_or(0);
1841     Unit.addTypeAccelerator(Die, AttrInfo.Name, ObjCClassIsImplementation,
1842                             Hash);
1843   }
1844 
1845   // Determine whether there are any children that we want to keep.
1846   bool HasChildren = false;
1847   for (auto Child : InputDIE.children()) {
1848     unsigned Idx = U.getDIEIndex(Child);
1849     if (Unit.getInfo(Idx).Keep) {
1850       HasChildren = true;
1851       break;
1852     }
1853   }
1854 
1855   if (Unit.getOrigUnit().getVersion() >= 5 && !AttrInfo.AttrStrOffsetBaseSeen &&
1856       Die->getTag() == dwarf::DW_TAG_compile_unit) {
1857     // No DW_AT_str_offsets_base seen, add it to the DIE.
1858     Die->addValue(DIEAlloc, dwarf::DW_AT_str_offsets_base,
1859                   dwarf::DW_FORM_sec_offset, DIEInteger(8));
1860     OutOffset += 4;
1861   }
1862 
1863   DIEAbbrev NewAbbrev = Die->generateAbbrev();
1864   if (HasChildren)
1865     NewAbbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
1866   // Assign a permanent abbrev number
1867   Linker.assignAbbrev(NewAbbrev);
1868   Die->setAbbrevNumber(NewAbbrev.getNumber());
1869 
1870   uint64_t AbbrevNumberSize = getULEB128Size(Die->getAbbrevNumber());
1871 
1872   // Add the size of the abbreviation number to the output offset.
1873   OutOffset += AbbrevNumberSize;
1874 
1875   // Update fixups with the size of the abbreviation number
1876   for (AttributeLinkedOffsetFixup &F : AttributesFixups)
1877     F.LinkedOffsetFixupVal += AbbrevNumberSize;
1878 
1879   for (AttributeLinkedOffsetFixup &F : AttributesFixups)
1880     ObjFile.Addresses->updateAndSaveValidRelocs(
1881         Unit.getOrigUnit().getVersion() >= 5, Unit.getOrigUnit().getOffset(),
1882         F.LinkedOffsetFixupVal, F.InputAttrStartOffset, F.InputAttrEndOffset);
1883 
1884   if (!HasChildren) {
1885     // Update our size.
1886     Die->setSize(OutOffset - Die->getOffset());
1887     return Die;
1888   }
1889 
1890   // Recursively clone children.
1891   for (auto Child : InputDIE.children()) {
1892     if (DIE *Clone = cloneDIE(Child, File, Unit, PCOffset, OutOffset, Flags,
1893                               IsLittleEndian)) {
1894       Die->addChild(Clone);
1895       OutOffset = Clone->getOffset() + Clone->getSize();
1896     }
1897   }
1898 
1899   // Account for the end of children marker.
1900   OutOffset += sizeof(int8_t);
1901   // Update our size.
1902   Die->setSize(OutOffset - Die->getOffset());
1903   return Die;
1904 }
1905 
1906 /// Patch the input object file relevant debug_ranges or debug_rnglists
1907 /// entries and emit them in the output file. Update the relevant attributes
1908 /// to point at the new entries.
1909 void DWARFLinker::generateUnitRanges(CompileUnit &Unit, const DWARFFile &File,
1910                                      DebugDieValuePool &AddrPool) const {
1911   if (LLVM_UNLIKELY(Options.Update))
1912     return;
1913 
1914   const auto &FunctionRanges = Unit.getFunctionRanges();
1915 
1916   // Build set of linked address ranges for unit function ranges.
1917   AddressRanges LinkedFunctionRanges;
1918   for (const AddressRangeValuePair &Range : FunctionRanges)
1919     LinkedFunctionRanges.insert(
1920         {Range.Range.start() + Range.Value, Range.Range.end() + Range.Value});
1921 
1922   // Emit LinkedFunctionRanges into .debug_aranges
1923   if (!LinkedFunctionRanges.empty())
1924     TheDwarfEmitter->emitDwarfDebugArangesTable(Unit, LinkedFunctionRanges);
1925 
1926   RngListAttributesTy AllRngListAttributes = Unit.getRangesAttributes();
1927   std::optional<PatchLocation> UnitRngListAttribute =
1928       Unit.getUnitRangesAttribute();
1929 
1930   if (!AllRngListAttributes.empty() || UnitRngListAttribute) {
1931     std::optional<AddressRangeValuePair> CachedRange;
1932     MCSymbol *EndLabel = TheDwarfEmitter->emitDwarfDebugRangeListHeader(Unit);
1933 
1934     // Read original address ranges, apply relocation value, emit linked address
1935     // ranges.
1936     for (PatchLocation &AttributePatch : AllRngListAttributes) {
1937       // Get ranges from the source DWARF corresponding to the current
1938       // attribute.
1939       AddressRanges LinkedRanges;
1940       if (Expected<DWARFAddressRangesVector> OriginalRanges =
1941               Unit.getOrigUnit().findRnglistFromOffset(AttributePatch.get())) {
1942         // Apply relocation adjustment.
1943         for (const auto &Range : *OriginalRanges) {
1944           if (!CachedRange || !CachedRange->Range.contains(Range.LowPC))
1945             CachedRange = FunctionRanges.getRangeThatContains(Range.LowPC);
1946 
1947           // All range entries should lie in the function range.
1948           if (!CachedRange) {
1949             reportWarning("inconsistent range data.", File);
1950             continue;
1951           }
1952 
1953           // Store range for emiting.
1954           LinkedRanges.insert({Range.LowPC + CachedRange->Value,
1955                                Range.HighPC + CachedRange->Value});
1956         }
1957       } else {
1958         llvm::consumeError(OriginalRanges.takeError());
1959         reportWarning("invalid range list ignored.", File);
1960       }
1961 
1962       // Emit linked ranges.
1963       TheDwarfEmitter->emitDwarfDebugRangeListFragment(
1964           Unit, LinkedRanges, AttributePatch, AddrPool);
1965     }
1966 
1967     // Emit ranges for Unit AT_ranges attribute.
1968     if (UnitRngListAttribute.has_value())
1969       TheDwarfEmitter->emitDwarfDebugRangeListFragment(
1970           Unit, LinkedFunctionRanges, *UnitRngListAttribute, AddrPool);
1971 
1972     // Emit ranges footer.
1973     TheDwarfEmitter->emitDwarfDebugRangeListFooter(Unit, EndLabel);
1974   }
1975 }
1976 
1977 void DWARFLinker::DIECloner::generateUnitLocations(
1978     CompileUnit &Unit, const DWARFFile &File,
1979     ExpressionHandlerRef ExprHandler) {
1980   if (LLVM_UNLIKELY(Linker.Options.Update))
1981     return;
1982 
1983   const LocListAttributesTy &AllLocListAttributes =
1984       Unit.getLocationAttributes();
1985 
1986   if (AllLocListAttributes.empty())
1987     return;
1988 
1989   // Emit locations list table header.
1990   MCSymbol *EndLabel = Emitter->emitDwarfDebugLocListHeader(Unit);
1991 
1992   for (auto &CurLocAttr : AllLocListAttributes) {
1993     // Get location expressions vector corresponding to the current attribute
1994     // from the source DWARF.
1995     Expected<DWARFLocationExpressionsVector> OriginalLocations =
1996         Unit.getOrigUnit().findLoclistFromOffset(CurLocAttr.get());
1997 
1998     if (!OriginalLocations) {
1999       llvm::consumeError(OriginalLocations.takeError());
2000       Linker.reportWarning("Invalid location attribute ignored.", File);
2001       continue;
2002     }
2003 
2004     DWARFLocationExpressionsVector LinkedLocationExpressions;
2005     for (DWARFLocationExpression &CurExpression : *OriginalLocations) {
2006       DWARFLocationExpression LinkedExpression;
2007 
2008       if (CurExpression.Range) {
2009         // Relocate address range.
2010         LinkedExpression.Range = {
2011             CurExpression.Range->LowPC + CurLocAttr.RelocAdjustment,
2012             CurExpression.Range->HighPC + CurLocAttr.RelocAdjustment};
2013       }
2014 
2015       // Clone expression.
2016       LinkedExpression.Expr.reserve(CurExpression.Expr.size());
2017       ExprHandler(CurExpression.Expr, LinkedExpression.Expr,
2018                   CurLocAttr.RelocAdjustment);
2019 
2020       LinkedLocationExpressions.push_back(LinkedExpression);
2021     }
2022 
2023     // Emit locations list table fragment corresponding to the CurLocAttr.
2024     Emitter->emitDwarfDebugLocListFragment(Unit, LinkedLocationExpressions,
2025                                            CurLocAttr, AddrPool);
2026   }
2027 
2028   // Emit locations list table footer.
2029   Emitter->emitDwarfDebugLocListFooter(Unit, EndLabel);
2030 }
2031 
2032 static void patchAddrBase(DIE &Die, DIEInteger Offset) {
2033   for (auto &V : Die.values())
2034     if (V.getAttribute() == dwarf::DW_AT_addr_base) {
2035       V = DIEValue(V.getAttribute(), V.getForm(), Offset);
2036       return;
2037     }
2038 
2039   llvm_unreachable("Didn't find a DW_AT_addr_base in cloned DIE!");
2040 }
2041 
2042 void DWARFLinker::DIECloner::emitDebugAddrSection(
2043     CompileUnit &Unit, const uint16_t DwarfVersion) const {
2044 
2045   if (LLVM_UNLIKELY(Linker.Options.Update))
2046     return;
2047 
2048   if (DwarfVersion < 5)
2049     return;
2050 
2051   if (AddrPool.DieValues.empty())
2052     return;
2053 
2054   MCSymbol *EndLabel = Emitter->emitDwarfDebugAddrsHeader(Unit);
2055   patchAddrBase(*Unit.getOutputUnitDIE(),
2056                 DIEInteger(Emitter->getDebugAddrSectionSize()));
2057   Emitter->emitDwarfDebugAddrs(AddrPool.DieValues,
2058                                Unit.getOrigUnit().getAddressByteSize());
2059   Emitter->emitDwarfDebugAddrsFooter(Unit, EndLabel);
2060 }
2061 
2062 /// Insert the new line info sequence \p Seq into the current
2063 /// set of already linked line info \p Rows.
2064 static void insertLineSequence(std::vector<DWARFDebugLine::Row> &Seq,
2065                                std::vector<DWARFDebugLine::Row> &Rows) {
2066   if (Seq.empty())
2067     return;
2068 
2069   if (!Rows.empty() && Rows.back().Address < Seq.front().Address) {
2070     llvm::append_range(Rows, Seq);
2071     Seq.clear();
2072     return;
2073   }
2074 
2075   object::SectionedAddress Front = Seq.front().Address;
2076   auto InsertPoint = partition_point(
2077       Rows, [=](const DWARFDebugLine::Row &O) { return O.Address < Front; });
2078 
2079   // FIXME: this only removes the unneeded end_sequence if the
2080   // sequences have been inserted in order. Using a global sort like
2081   // described in generateLineTableForUnit() and delaying the end_sequene
2082   // elimination to emitLineTableForUnit() we can get rid of all of them.
2083   if (InsertPoint != Rows.end() && InsertPoint->Address == Front &&
2084       InsertPoint->EndSequence) {
2085     *InsertPoint = Seq.front();
2086     Rows.insert(InsertPoint + 1, Seq.begin() + 1, Seq.end());
2087   } else {
2088     Rows.insert(InsertPoint, Seq.begin(), Seq.end());
2089   }
2090 
2091   Seq.clear();
2092 }
2093 
2094 static void patchStmtList(DIE &Die, DIEInteger Offset) {
2095   for (auto &V : Die.values())
2096     if (V.getAttribute() == dwarf::DW_AT_stmt_list) {
2097       V = DIEValue(V.getAttribute(), V.getForm(), Offset);
2098       return;
2099     }
2100 
2101   llvm_unreachable("Didn't find DW_AT_stmt_list in cloned DIE!");
2102 }
2103 
2104 void DWARFLinker::DIECloner::rememberUnitForMacroOffset(CompileUnit &Unit) {
2105   DWARFUnit &OrigUnit = Unit.getOrigUnit();
2106   DWARFDie OrigUnitDie = OrigUnit.getUnitDIE();
2107 
2108   if (std::optional<uint64_t> MacroAttr =
2109           dwarf::toSectionOffset(OrigUnitDie.find(dwarf::DW_AT_macros))) {
2110     UnitMacroMap.insert(std::make_pair(*MacroAttr, &Unit));
2111     return;
2112   }
2113 
2114   if (std::optional<uint64_t> MacroAttr =
2115           dwarf::toSectionOffset(OrigUnitDie.find(dwarf::DW_AT_macro_info))) {
2116     UnitMacroMap.insert(std::make_pair(*MacroAttr, &Unit));
2117     return;
2118   }
2119 }
2120 
2121 void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) {
2122   if (LLVM_UNLIKELY(Emitter == nullptr))
2123     return;
2124 
2125   // Check whether DW_AT_stmt_list attribute is presented.
2126   DWARFDie CUDie = Unit.getOrigUnit().getUnitDIE();
2127   auto StmtList = dwarf::toSectionOffset(CUDie.find(dwarf::DW_AT_stmt_list));
2128   if (!StmtList)
2129     return;
2130 
2131   // Update the cloned DW_AT_stmt_list with the correct debug_line offset.
2132   if (auto *OutputDIE = Unit.getOutputUnitDIE())
2133     patchStmtList(*OutputDIE, DIEInteger(Emitter->getLineSectionSize()));
2134 
2135   if (const DWARFDebugLine::LineTable *LT =
2136           ObjFile.Dwarf->getLineTableForUnit(&Unit.getOrigUnit())) {
2137 
2138     DWARFDebugLine::LineTable LineTable;
2139 
2140     // Set Line Table header.
2141     LineTable.Prologue = LT->Prologue;
2142 
2143     // Set Line Table Rows.
2144     if (Linker.Options.Update) {
2145       LineTable.Rows = LT->Rows;
2146       // If all the line table contains is a DW_LNE_end_sequence, clear the line
2147       // table rows, it will be inserted again in the DWARFStreamer.
2148       if (LineTable.Rows.size() == 1 && LineTable.Rows[0].EndSequence)
2149         LineTable.Rows.clear();
2150 
2151       LineTable.Sequences = LT->Sequences;
2152     } else {
2153       // This vector is the output line table.
2154       std::vector<DWARFDebugLine::Row> NewRows;
2155       NewRows.reserve(LT->Rows.size());
2156 
2157       // Current sequence of rows being extracted, before being inserted
2158       // in NewRows.
2159       std::vector<DWARFDebugLine::Row> Seq;
2160 
2161       const auto &FunctionRanges = Unit.getFunctionRanges();
2162       std::optional<AddressRangeValuePair> CurrRange;
2163 
2164       // FIXME: This logic is meant to generate exactly the same output as
2165       // Darwin's classic dsymutil. There is a nicer way to implement this
2166       // by simply putting all the relocated line info in NewRows and simply
2167       // sorting NewRows before passing it to emitLineTableForUnit. This
2168       // should be correct as sequences for a function should stay
2169       // together in the sorted output. There are a few corner cases that
2170       // look suspicious though, and that required to implement the logic
2171       // this way. Revisit that once initial validation is finished.
2172 
2173       // Iterate over the object file line info and extract the sequences
2174       // that correspond to linked functions.
2175       for (DWARFDebugLine::Row Row : LT->Rows) {
2176         // Check whether we stepped out of the range. The range is
2177         // half-open, but consider accept the end address of the range if
2178         // it is marked as end_sequence in the input (because in that
2179         // case, the relocation offset is accurate and that entry won't
2180         // serve as the start of another function).
2181         if (!CurrRange || !CurrRange->Range.contains(Row.Address.Address)) {
2182           // We just stepped out of a known range. Insert a end_sequence
2183           // corresponding to the end of the range.
2184           uint64_t StopAddress =
2185               CurrRange ? CurrRange->Range.end() + CurrRange->Value : -1ULL;
2186           CurrRange = FunctionRanges.getRangeThatContains(Row.Address.Address);
2187           if (StopAddress != -1ULL && !Seq.empty()) {
2188             // Insert end sequence row with the computed end address, but
2189             // the same line as the previous one.
2190             auto NextLine = Seq.back();
2191             NextLine.Address.Address = StopAddress;
2192             NextLine.EndSequence = 1;
2193             NextLine.PrologueEnd = 0;
2194             NextLine.BasicBlock = 0;
2195             NextLine.EpilogueBegin = 0;
2196             Seq.push_back(NextLine);
2197             insertLineSequence(Seq, NewRows);
2198           }
2199 
2200           if (!CurrRange)
2201             continue;
2202         }
2203 
2204         // Ignore empty sequences.
2205         if (Row.EndSequence && Seq.empty())
2206           continue;
2207 
2208         // Relocate row address and add it to the current sequence.
2209         Row.Address.Address += CurrRange->Value;
2210         Seq.emplace_back(Row);
2211 
2212         if (Row.EndSequence)
2213           insertLineSequence(Seq, NewRows);
2214       }
2215 
2216       LineTable.Rows = std::move(NewRows);
2217     }
2218 
2219     Emitter->emitLineTableForUnit(LineTable, Unit, DebugStrPool,
2220                                   DebugLineStrPool);
2221   } else
2222     Linker.reportWarning("Cann't load line table.", ObjFile);
2223 }
2224 
2225 void DWARFLinker::emitAcceleratorEntriesForUnit(CompileUnit &Unit) {
2226   for (AccelTableKind AccelTableKind : Options.AccelTables) {
2227     switch (AccelTableKind) {
2228     case AccelTableKind::Apple: {
2229       // Add namespaces.
2230       for (const auto &Namespace : Unit.getNamespaces())
2231         AppleNamespaces.addName(Namespace.Name, Namespace.Die->getOffset() +
2232                                                     Unit.getStartOffset());
2233       // Add names.
2234       for (const auto &Pubname : Unit.getPubnames())
2235         AppleNames.addName(Pubname.Name,
2236                            Pubname.Die->getOffset() + Unit.getStartOffset());
2237       // Add types.
2238       for (const auto &Pubtype : Unit.getPubtypes())
2239         AppleTypes.addName(
2240             Pubtype.Name, Pubtype.Die->getOffset() + Unit.getStartOffset(),
2241             Pubtype.Die->getTag(),
2242             Pubtype.ObjcClassImplementation ? dwarf::DW_FLAG_type_implementation
2243                                             : 0,
2244             Pubtype.QualifiedNameHash);
2245       // Add ObjC names.
2246       for (const auto &ObjC : Unit.getObjC())
2247         AppleObjc.addName(ObjC.Name,
2248                           ObjC.Die->getOffset() + Unit.getStartOffset());
2249     } break;
2250     case AccelTableKind::Pub: {
2251       TheDwarfEmitter->emitPubNamesForUnit(Unit);
2252       TheDwarfEmitter->emitPubTypesForUnit(Unit);
2253     } break;
2254     case AccelTableKind::DebugNames: {
2255       for (const auto &Namespace : Unit.getNamespaces())
2256         DebugNames.addName(Namespace.Name, Namespace.Die->getOffset(),
2257                            Namespace.Die->getTag(), Unit.getUniqueID());
2258       for (const auto &Pubname : Unit.getPubnames())
2259         DebugNames.addName(Pubname.Name, Pubname.Die->getOffset(),
2260                            Pubname.Die->getTag(), Unit.getUniqueID());
2261       for (const auto &Pubtype : Unit.getPubtypes())
2262         DebugNames.addName(Pubtype.Name, Pubtype.Die->getOffset(),
2263                            Pubtype.Die->getTag(), Unit.getUniqueID());
2264     } break;
2265     }
2266   }
2267 }
2268 
2269 /// Read the frame info stored in the object, and emit the
2270 /// patched frame descriptions for the resulting file.
2271 ///
2272 /// This is actually pretty easy as the data of the CIEs and FDEs can
2273 /// be considered as black boxes and moved as is. The only thing to do
2274 /// is to patch the addresses in the headers.
2275 void DWARFLinker::patchFrameInfoForObject(LinkContext &Context) {
2276   DWARFContext &OrigDwarf = *Context.File.Dwarf;
2277   unsigned SrcAddrSize = OrigDwarf.getDWARFObj().getAddressSize();
2278 
2279   StringRef FrameData = OrigDwarf.getDWARFObj().getFrameSection().Data;
2280   if (FrameData.empty())
2281     return;
2282 
2283   RangesTy AllUnitsRanges;
2284   for (std::unique_ptr<CompileUnit> &Unit : Context.CompileUnits) {
2285     for (auto CurRange : Unit->getFunctionRanges())
2286       AllUnitsRanges.insert(CurRange.Range, CurRange.Value);
2287   }
2288 
2289   DataExtractor Data(FrameData, OrigDwarf.isLittleEndian(), 0);
2290   uint64_t InputOffset = 0;
2291 
2292   // Store the data of the CIEs defined in this object, keyed by their
2293   // offsets.
2294   DenseMap<uint64_t, StringRef> LocalCIES;
2295 
2296   while (Data.isValidOffset(InputOffset)) {
2297     uint64_t EntryOffset = InputOffset;
2298     uint32_t InitialLength = Data.getU32(&InputOffset);
2299     if (InitialLength == 0xFFFFFFFF)
2300       return reportWarning("Dwarf64 bits no supported", Context.File);
2301 
2302     uint32_t CIEId = Data.getU32(&InputOffset);
2303     if (CIEId == 0xFFFFFFFF) {
2304       // This is a CIE, store it.
2305       StringRef CIEData = FrameData.substr(EntryOffset, InitialLength + 4);
2306       LocalCIES[EntryOffset] = CIEData;
2307       // The -4 is to account for the CIEId we just read.
2308       InputOffset += InitialLength - 4;
2309       continue;
2310     }
2311 
2312     uint64_t Loc = Data.getUnsigned(&InputOffset, SrcAddrSize);
2313 
2314     // Some compilers seem to emit frame info that doesn't start at
2315     // the function entry point, thus we can't just lookup the address
2316     // in the debug map. Use the AddressInfo's range map to see if the FDE
2317     // describes something that we can relocate.
2318     std::optional<AddressRangeValuePair> Range =
2319         AllUnitsRanges.getRangeThatContains(Loc);
2320     if (!Range) {
2321       // The +4 is to account for the size of the InitialLength field itself.
2322       InputOffset = EntryOffset + InitialLength + 4;
2323       continue;
2324     }
2325 
2326     // This is an FDE, and we have a mapping.
2327     // Have we already emitted a corresponding CIE?
2328     StringRef CIEData = LocalCIES[CIEId];
2329     if (CIEData.empty())
2330       return reportWarning("Inconsistent debug_frame content. Dropping.",
2331                            Context.File);
2332 
2333     // Look if we already emitted a CIE that corresponds to the
2334     // referenced one (the CIE data is the key of that lookup).
2335     auto IteratorInserted = EmittedCIEs.insert(
2336         std::make_pair(CIEData, TheDwarfEmitter->getFrameSectionSize()));
2337     // If there is no CIE yet for this ID, emit it.
2338     if (IteratorInserted.second) {
2339       LastCIEOffset = TheDwarfEmitter->getFrameSectionSize();
2340       IteratorInserted.first->getValue() = LastCIEOffset;
2341       TheDwarfEmitter->emitCIE(CIEData);
2342     }
2343 
2344     // Emit the FDE with updated address and CIE pointer.
2345     // (4 + AddrSize) is the size of the CIEId + initial_location
2346     // fields that will get reconstructed by emitFDE().
2347     unsigned FDERemainingBytes = InitialLength - (4 + SrcAddrSize);
2348     TheDwarfEmitter->emitFDE(IteratorInserted.first->getValue(), SrcAddrSize,
2349                              Loc + Range->Value,
2350                              FrameData.substr(InputOffset, FDERemainingBytes));
2351     InputOffset += FDERemainingBytes;
2352   }
2353 }
2354 
2355 uint32_t DWARFLinker::DIECloner::hashFullyQualifiedName(DWARFDie DIE,
2356                                                         CompileUnit &U,
2357                                                         const DWARFFile &File,
2358                                                         int ChildRecurseDepth) {
2359   const char *Name = nullptr;
2360   DWARFUnit *OrigUnit = &U.getOrigUnit();
2361   CompileUnit *CU = &U;
2362   std::optional<DWARFFormValue> Ref;
2363 
2364   while (true) {
2365     if (const char *CurrentName = DIE.getName(DINameKind::ShortName))
2366       Name = CurrentName;
2367 
2368     if (!(Ref = DIE.find(dwarf::DW_AT_specification)) &&
2369         !(Ref = DIE.find(dwarf::DW_AT_abstract_origin)))
2370       break;
2371 
2372     if (!Ref->isFormClass(DWARFFormValue::FC_Reference))
2373       break;
2374 
2375     CompileUnit *RefCU;
2376     if (auto RefDIE =
2377             Linker.resolveDIEReference(File, CompileUnits, *Ref, DIE, RefCU)) {
2378       CU = RefCU;
2379       OrigUnit = &RefCU->getOrigUnit();
2380       DIE = RefDIE;
2381     }
2382   }
2383 
2384   unsigned Idx = OrigUnit->getDIEIndex(DIE);
2385   if (!Name && DIE.getTag() == dwarf::DW_TAG_namespace)
2386     Name = "(anonymous namespace)";
2387 
2388   if (CU->getInfo(Idx).ParentIdx == 0 ||
2389       // FIXME: dsymutil-classic compatibility. Ignore modules.
2390       CU->getOrigUnit().getDIEAtIndex(CU->getInfo(Idx).ParentIdx).getTag() ==
2391           dwarf::DW_TAG_module)
2392     return djbHash(Name ? Name : "", djbHash(ChildRecurseDepth ? "" : "::"));
2393 
2394   DWARFDie Die = OrigUnit->getDIEAtIndex(CU->getInfo(Idx).ParentIdx);
2395   return djbHash(
2396       (Name ? Name : ""),
2397       djbHash((Name ? "::" : ""),
2398               hashFullyQualifiedName(Die, *CU, File, ++ChildRecurseDepth)));
2399 }
2400 
2401 static uint64_t getDwoId(const DWARFDie &CUDie) {
2402   auto DwoId = dwarf::toUnsigned(
2403       CUDie.find({dwarf::DW_AT_dwo_id, dwarf::DW_AT_GNU_dwo_id}));
2404   if (DwoId)
2405     return *DwoId;
2406   return 0;
2407 }
2408 
2409 static std::string
2410 remapPath(StringRef Path,
2411           const DWARFLinkerBase::ObjectPrefixMapTy &ObjectPrefixMap) {
2412   if (ObjectPrefixMap.empty())
2413     return Path.str();
2414 
2415   SmallString<256> p = Path;
2416   for (const auto &Entry : ObjectPrefixMap)
2417     if (llvm::sys::path::replace_path_prefix(p, Entry.first, Entry.second))
2418       break;
2419   return p.str().str();
2420 }
2421 
2422 static std::string
2423 getPCMFile(const DWARFDie &CUDie,
2424            const DWARFLinkerBase::ObjectPrefixMapTy *ObjectPrefixMap) {
2425   std::string PCMFile = dwarf::toString(
2426       CUDie.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
2427 
2428   if (PCMFile.empty())
2429     return PCMFile;
2430 
2431   if (ObjectPrefixMap)
2432     PCMFile = remapPath(PCMFile, *ObjectPrefixMap);
2433 
2434   return PCMFile;
2435 }
2436 
2437 std::pair<bool, bool> DWARFLinker::isClangModuleRef(const DWARFDie &CUDie,
2438                                                     std::string &PCMFile,
2439                                                     LinkContext &Context,
2440                                                     unsigned Indent,
2441                                                     bool Quiet) {
2442   if (PCMFile.empty())
2443     return std::make_pair(false, false);
2444 
2445   // Clang module DWARF skeleton CUs abuse this for the path to the module.
2446   uint64_t DwoId = getDwoId(CUDie);
2447 
2448   std::string Name = dwarf::toString(CUDie.find(dwarf::DW_AT_name), "");
2449   if (Name.empty()) {
2450     if (!Quiet)
2451       reportWarning("Anonymous module skeleton CU for " + PCMFile,
2452                     Context.File);
2453     return std::make_pair(true, true);
2454   }
2455 
2456   if (!Quiet && Options.Verbose) {
2457     outs().indent(Indent);
2458     outs() << "Found clang module reference " << PCMFile;
2459   }
2460 
2461   auto Cached = ClangModules.find(PCMFile);
2462   if (Cached != ClangModules.end()) {
2463     // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
2464     // fixed in clang, only warn about DWO_id mismatches in verbose mode.
2465     // ASTFileSignatures will change randomly when a module is rebuilt.
2466     if (!Quiet && Options.Verbose && (Cached->second != DwoId))
2467       reportWarning(Twine("hash mismatch: this object file was built against a "
2468                           "different version of the module ") +
2469                         PCMFile,
2470                     Context.File);
2471     if (!Quiet && Options.Verbose)
2472       outs() << " [cached].\n";
2473     return std::make_pair(true, true);
2474   }
2475 
2476   return std::make_pair(true, false);
2477 }
2478 
2479 bool DWARFLinker::registerModuleReference(const DWARFDie &CUDie,
2480                                           LinkContext &Context,
2481                                           ObjFileLoaderTy Loader,
2482                                           CompileUnitHandlerTy OnCUDieLoaded,
2483                                           unsigned Indent) {
2484   std::string PCMFile = getPCMFile(CUDie, Options.ObjectPrefixMap);
2485   std::pair<bool, bool> IsClangModuleRef =
2486       isClangModuleRef(CUDie, PCMFile, Context, Indent, false);
2487 
2488   if (!IsClangModuleRef.first)
2489     return false;
2490 
2491   if (IsClangModuleRef.second)
2492     return true;
2493 
2494   if (Options.Verbose)
2495     outs() << " ...\n";
2496 
2497   // Cyclic dependencies are disallowed by Clang, but we still
2498   // shouldn't run into an infinite loop, so mark it as processed now.
2499   ClangModules.insert({PCMFile, getDwoId(CUDie)});
2500 
2501   if (Error E = loadClangModule(Loader, CUDie, PCMFile, Context, OnCUDieLoaded,
2502                                 Indent + 2)) {
2503     consumeError(std::move(E));
2504     return false;
2505   }
2506   return true;
2507 }
2508 
2509 Error DWARFLinker::loadClangModule(
2510     ObjFileLoaderTy Loader, const DWARFDie &CUDie, const std::string &PCMFile,
2511     LinkContext &Context, CompileUnitHandlerTy OnCUDieLoaded, unsigned Indent) {
2512 
2513   uint64_t DwoId = getDwoId(CUDie);
2514   std::string ModuleName = dwarf::toString(CUDie.find(dwarf::DW_AT_name), "");
2515 
2516   /// Using a SmallString<0> because loadClangModule() is recursive.
2517   SmallString<0> Path(Options.PrependPath);
2518   if (sys::path::is_relative(PCMFile))
2519     resolveRelativeObjectPath(Path, CUDie);
2520   sys::path::append(Path, PCMFile);
2521   // Don't use the cached binary holder because we have no thread-safety
2522   // guarantee and the lifetime is limited.
2523 
2524   if (Loader == nullptr) {
2525     reportError("Could not load clang module: loader is not specified.\n",
2526                 Context.File);
2527     return Error::success();
2528   }
2529 
2530   auto ErrOrObj = Loader(Context.File.FileName, Path);
2531   if (!ErrOrObj)
2532     return Error::success();
2533 
2534   std::unique_ptr<CompileUnit> Unit;
2535   for (const auto &CU : ErrOrObj->Dwarf->compile_units()) {
2536     OnCUDieLoaded(*CU);
2537     // Recursively get all modules imported by this one.
2538     auto ChildCUDie = CU->getUnitDIE();
2539     if (!ChildCUDie)
2540       continue;
2541     if (!registerModuleReference(ChildCUDie, Context, Loader, OnCUDieLoaded,
2542                                  Indent)) {
2543       if (Unit) {
2544         std::string Err =
2545             (PCMFile +
2546              ": Clang modules are expected to have exactly 1 compile unit.\n");
2547         reportError(Err, Context.File);
2548         return make_error<StringError>(Err, inconvertibleErrorCode());
2549       }
2550       // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
2551       // fixed in clang, only warn about DWO_id mismatches in verbose mode.
2552       // ASTFileSignatures will change randomly when a module is rebuilt.
2553       uint64_t PCMDwoId = getDwoId(ChildCUDie);
2554       if (PCMDwoId != DwoId) {
2555         if (Options.Verbose)
2556           reportWarning(
2557               Twine("hash mismatch: this object file was built against a "
2558                     "different version of the module ") +
2559                   PCMFile,
2560               Context.File);
2561         // Update the cache entry with the DwoId of the module loaded from disk.
2562         ClangModules[PCMFile] = PCMDwoId;
2563       }
2564 
2565       // Add this module.
2566       Unit = std::make_unique<CompileUnit>(*CU, UniqueUnitID++, !Options.NoODR,
2567                                            ModuleName);
2568     }
2569   }
2570 
2571   if (Unit)
2572     Context.ModuleUnits.emplace_back(RefModuleUnit{*ErrOrObj, std::move(Unit)});
2573 
2574   return Error::success();
2575 }
2576 
2577 uint64_t DWARFLinker::DIECloner::cloneAllCompileUnits(
2578     DWARFContext &DwarfContext, const DWARFFile &File, bool IsLittleEndian) {
2579   uint64_t OutputDebugInfoSize =
2580       (Emitter == nullptr) ? 0 : Emitter->getDebugInfoSectionSize();
2581   const uint64_t StartOutputDebugInfoSize = OutputDebugInfoSize;
2582 
2583   for (auto &CurrentUnit : CompileUnits) {
2584     const uint16_t DwarfVersion = CurrentUnit->getOrigUnit().getVersion();
2585     const uint32_t UnitHeaderSize = DwarfVersion >= 5 ? 12 : 11;
2586     auto InputDIE = CurrentUnit->getOrigUnit().getUnitDIE();
2587     CurrentUnit->setStartOffset(OutputDebugInfoSize);
2588     if (!InputDIE) {
2589       OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset(DwarfVersion);
2590       continue;
2591     }
2592     if (CurrentUnit->getInfo(0).Keep) {
2593       // Clone the InputDIE into your Unit DIE in our compile unit since it
2594       // already has a DIE inside of it.
2595       CurrentUnit->createOutputDIE();
2596       rememberUnitForMacroOffset(*CurrentUnit);
2597       cloneDIE(InputDIE, File, *CurrentUnit, 0 /* PC offset */, UnitHeaderSize,
2598                0, IsLittleEndian, CurrentUnit->getOutputUnitDIE());
2599     }
2600 
2601     OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset(DwarfVersion);
2602 
2603     if (Emitter != nullptr) {
2604 
2605       generateLineTableForUnit(*CurrentUnit);
2606 
2607       Linker.emitAcceleratorEntriesForUnit(*CurrentUnit);
2608 
2609       if (LLVM_UNLIKELY(Linker.Options.Update))
2610         continue;
2611 
2612       Linker.generateUnitRanges(*CurrentUnit, File, AddrPool);
2613 
2614       auto ProcessExpr = [&](SmallVectorImpl<uint8_t> &SrcBytes,
2615                              SmallVectorImpl<uint8_t> &OutBytes,
2616                              int64_t RelocAdjustment) {
2617         DWARFUnit &OrigUnit = CurrentUnit->getOrigUnit();
2618         DataExtractor Data(SrcBytes, IsLittleEndian,
2619                            OrigUnit.getAddressByteSize());
2620         cloneExpression(Data,
2621                         DWARFExpression(Data, OrigUnit.getAddressByteSize(),
2622                                         OrigUnit.getFormParams().Format),
2623                         File, *CurrentUnit, OutBytes, RelocAdjustment,
2624                         IsLittleEndian);
2625       };
2626       generateUnitLocations(*CurrentUnit, File, ProcessExpr);
2627       emitDebugAddrSection(*CurrentUnit, DwarfVersion);
2628     }
2629     AddrPool.clear();
2630   }
2631 
2632   if (Emitter != nullptr) {
2633     assert(Emitter);
2634     // Emit macro tables.
2635     Emitter->emitMacroTables(File.Dwarf.get(), UnitMacroMap, DebugStrPool);
2636 
2637     // Emit all the compile unit's debug information.
2638     for (auto &CurrentUnit : CompileUnits) {
2639       CurrentUnit->fixupForwardReferences();
2640 
2641       if (!CurrentUnit->getOutputUnitDIE())
2642         continue;
2643 
2644       unsigned DwarfVersion = CurrentUnit->getOrigUnit().getVersion();
2645 
2646       assert(Emitter->getDebugInfoSectionSize() ==
2647              CurrentUnit->getStartOffset());
2648       Emitter->emitCompileUnitHeader(*CurrentUnit, DwarfVersion);
2649       Emitter->emitDIE(*CurrentUnit->getOutputUnitDIE());
2650       assert(Emitter->getDebugInfoSectionSize() ==
2651              CurrentUnit->computeNextUnitOffset(DwarfVersion));
2652     }
2653   }
2654 
2655   return OutputDebugInfoSize - StartOutputDebugInfoSize;
2656 }
2657 
2658 void DWARFLinker::copyInvariantDebugSection(DWARFContext &Dwarf) {
2659   TheDwarfEmitter->emitSectionContents(Dwarf.getDWARFObj().getLocSection().Data,
2660                                        "debug_loc");
2661   TheDwarfEmitter->emitSectionContents(
2662       Dwarf.getDWARFObj().getRangesSection().Data, "debug_ranges");
2663   TheDwarfEmitter->emitSectionContents(
2664       Dwarf.getDWARFObj().getFrameSection().Data, "debug_frame");
2665   TheDwarfEmitter->emitSectionContents(Dwarf.getDWARFObj().getArangesSection(),
2666                                        "debug_aranges");
2667   TheDwarfEmitter->emitSectionContents(
2668       Dwarf.getDWARFObj().getAddrSection().Data, "debug_addr");
2669   TheDwarfEmitter->emitSectionContents(
2670       Dwarf.getDWARFObj().getRnglistsSection().Data, "debug_rnglists");
2671   TheDwarfEmitter->emitSectionContents(
2672       Dwarf.getDWARFObj().getLoclistsSection().Data, "debug_loclists");
2673 }
2674 
2675 void DWARFLinker::addObjectFile(DWARFFile &File, ObjFileLoaderTy Loader,
2676                                 CompileUnitHandlerTy OnCUDieLoaded) {
2677   ObjectContexts.emplace_back(LinkContext(File));
2678 
2679   if (ObjectContexts.back().File.Dwarf) {
2680     for (const std::unique_ptr<DWARFUnit> &CU :
2681          ObjectContexts.back().File.Dwarf->compile_units()) {
2682       DWARFDie CUDie = CU->getUnitDIE();
2683 
2684       if (!CUDie)
2685         continue;
2686 
2687       OnCUDieLoaded(*CU);
2688 
2689       if (!LLVM_UNLIKELY(Options.Update))
2690         registerModuleReference(CUDie, ObjectContexts.back(), Loader,
2691                                 OnCUDieLoaded);
2692     }
2693   }
2694 }
2695 
2696 Error DWARFLinker::link() {
2697   assert((Options.TargetDWARFVersion != 0) &&
2698          "TargetDWARFVersion should be set");
2699 
2700   // First populate the data structure we need for each iteration of the
2701   // parallel loop.
2702   unsigned NumObjects = ObjectContexts.size();
2703 
2704   // This Dwarf string pool which is used for emission. It must be used
2705   // serially as the order of calling getStringOffset matters for
2706   // reproducibility.
2707   OffsetsStringPool DebugStrPool(StringsTranslator, true);
2708   OffsetsStringPool DebugLineStrPool(StringsTranslator, false);
2709   DebugDieValuePool StringOffsetPool;
2710 
2711   // ODR Contexts for the optimize.
2712   DeclContextTree ODRContexts;
2713 
2714   for (LinkContext &OptContext : ObjectContexts) {
2715     if (Options.Verbose)
2716       outs() << "DEBUG MAP OBJECT: " << OptContext.File.FileName << "\n";
2717 
2718     if (!OptContext.File.Dwarf)
2719       continue;
2720 
2721     if (Options.VerifyInputDWARF)
2722       verifyInput(OptContext.File);
2723 
2724     // Look for relocations that correspond to address map entries.
2725 
2726     // there was findvalidrelocations previously ... probably we need to gather
2727     // info here
2728     if (LLVM_LIKELY(!Options.Update) &&
2729         !OptContext.File.Addresses->hasValidRelocs()) {
2730       if (Options.Verbose)
2731         outs() << "No valid relocations found. Skipping.\n";
2732 
2733       // Set "Skip" flag as a signal to other loops that we should not
2734       // process this iteration.
2735       OptContext.Skip = true;
2736       continue;
2737     }
2738 
2739     // Setup access to the debug info.
2740     if (!OptContext.File.Dwarf)
2741       continue;
2742 
2743     // Check whether type units are presented.
2744     if (!OptContext.File.Dwarf->types_section_units().empty()) {
2745       reportWarning("type units are not currently supported: file will "
2746                     "be skipped",
2747                     OptContext.File);
2748       OptContext.Skip = true;
2749       continue;
2750     }
2751 
2752     // Clone all the clang modules with requires extracting the DIE units. We
2753     // don't need the full debug info until the Analyze phase.
2754     OptContext.CompileUnits.reserve(
2755         OptContext.File.Dwarf->getNumCompileUnits());
2756     for (const auto &CU : OptContext.File.Dwarf->compile_units()) {
2757       auto CUDie = CU->getUnitDIE(/*ExtractUnitDIEOnly=*/true);
2758       if (Options.Verbose) {
2759         outs() << "Input compilation unit:";
2760         DIDumpOptions DumpOpts;
2761         DumpOpts.ChildRecurseDepth = 0;
2762         DumpOpts.Verbose = Options.Verbose;
2763         CUDie.dump(outs(), 0, DumpOpts);
2764       }
2765     }
2766 
2767     for (auto &CU : OptContext.ModuleUnits) {
2768       if (Error Err = cloneModuleUnit(OptContext, CU, ODRContexts, DebugStrPool,
2769                                       DebugLineStrPool, StringOffsetPool))
2770         reportWarning(toString(std::move(Err)), CU.File);
2771     }
2772   }
2773 
2774   // At this point we know how much data we have emitted. We use this value to
2775   // compare canonical DIE offsets in analyzeContextInfo to see if a definition
2776   // is already emitted, without being affected by canonical die offsets set
2777   // later. This prevents undeterminism when analyze and clone execute
2778   // concurrently, as clone set the canonical DIE offset and analyze reads it.
2779   const uint64_t ModulesEndOffset =
2780       (TheDwarfEmitter == nullptr) ? 0
2781                                    : TheDwarfEmitter->getDebugInfoSectionSize();
2782 
2783   // These variables manage the list of processed object files.
2784   // The mutex and condition variable are to ensure that this is thread safe.
2785   std::mutex ProcessedFilesMutex;
2786   std::condition_variable ProcessedFilesConditionVariable;
2787   BitVector ProcessedFiles(NumObjects, false);
2788 
2789   //  Analyzing the context info is particularly expensive so it is executed in
2790   //  parallel with emitting the previous compile unit.
2791   auto AnalyzeLambda = [&](size_t I) {
2792     auto &Context = ObjectContexts[I];
2793 
2794     if (Context.Skip || !Context.File.Dwarf)
2795       return;
2796 
2797     for (const auto &CU : Context.File.Dwarf->compile_units()) {
2798       // Previously we only extracted the unit DIEs. We need the full debug info
2799       // now.
2800       auto CUDie = CU->getUnitDIE(/*ExtractUnitDIEOnly=*/false);
2801       std::string PCMFile = getPCMFile(CUDie, Options.ObjectPrefixMap);
2802 
2803       if (!CUDie || LLVM_UNLIKELY(Options.Update) ||
2804           !isClangModuleRef(CUDie, PCMFile, Context, 0, true).first) {
2805         Context.CompileUnits.push_back(std::make_unique<CompileUnit>(
2806             *CU, UniqueUnitID++, !Options.NoODR && !Options.Update, ""));
2807       }
2808     }
2809 
2810     // Now build the DIE parent links that we will use during the next phase.
2811     for (auto &CurrentUnit : Context.CompileUnits) {
2812       auto CUDie = CurrentUnit->getOrigUnit().getUnitDIE();
2813       if (!CUDie)
2814         continue;
2815       analyzeContextInfo(CurrentUnit->getOrigUnit().getUnitDIE(), 0,
2816                          *CurrentUnit, &ODRContexts.getRoot(), ODRContexts,
2817                          ModulesEndOffset, Options.ParseableSwiftInterfaces,
2818                          [&](const Twine &Warning, const DWARFDie &DIE) {
2819                            reportWarning(Warning, Context.File, &DIE);
2820                          });
2821     }
2822   };
2823 
2824   // For each object file map how many bytes were emitted.
2825   StringMap<DebugInfoSize> SizeByObject;
2826 
2827   // And then the remaining work in serial again.
2828   // Note, although this loop runs in serial, it can run in parallel with
2829   // the analyzeContextInfo loop so long as we process files with indices >=
2830   // than those processed by analyzeContextInfo.
2831   auto CloneLambda = [&](size_t I) {
2832     auto &OptContext = ObjectContexts[I];
2833     if (OptContext.Skip || !OptContext.File.Dwarf)
2834       return;
2835 
2836     // Then mark all the DIEs that need to be present in the generated output
2837     // and collect some information about them.
2838     // Note that this loop can not be merged with the previous one because
2839     // cross-cu references require the ParentIdx to be setup for every CU in
2840     // the object file before calling this.
2841     if (LLVM_UNLIKELY(Options.Update)) {
2842       for (auto &CurrentUnit : OptContext.CompileUnits)
2843         CurrentUnit->markEverythingAsKept();
2844       copyInvariantDebugSection(*OptContext.File.Dwarf);
2845     } else {
2846       for (auto &CurrentUnit : OptContext.CompileUnits) {
2847         lookForDIEsToKeep(*OptContext.File.Addresses, OptContext.CompileUnits,
2848                           CurrentUnit->getOrigUnit().getUnitDIE(),
2849                           OptContext.File, *CurrentUnit, 0);
2850 #ifndef NDEBUG
2851         verifyKeepChain(*CurrentUnit);
2852 #endif
2853       }
2854     }
2855 
2856     // The calls to applyValidRelocs inside cloneDIE will walk the reloc
2857     // array again (in the same way findValidRelocsInDebugInfo() did). We
2858     // need to reset the NextValidReloc index to the beginning.
2859     if (OptContext.File.Addresses->hasValidRelocs() ||
2860         LLVM_UNLIKELY(Options.Update)) {
2861       SizeByObject[OptContext.File.FileName].Input =
2862           getDebugInfoSize(*OptContext.File.Dwarf);
2863       SizeByObject[OptContext.File.FileName].Output =
2864           DIECloner(*this, TheDwarfEmitter.get(), OptContext.File, DIEAlloc,
2865                     OptContext.CompileUnits, Options.Update, DebugStrPool,
2866                     DebugLineStrPool, StringOffsetPool)
2867               .cloneAllCompileUnits(*OptContext.File.Dwarf, OptContext.File,
2868                                     OptContext.File.Dwarf->isLittleEndian());
2869     }
2870     if ((TheDwarfEmitter != nullptr) && !OptContext.CompileUnits.empty() &&
2871         LLVM_LIKELY(!Options.Update))
2872       patchFrameInfoForObject(OptContext);
2873 
2874     // Clean-up before starting working on the next object.
2875     cleanupAuxiliarryData(OptContext);
2876   };
2877 
2878   auto EmitLambda = [&]() {
2879     // Emit everything that's global.
2880     if (TheDwarfEmitter != nullptr) {
2881       TheDwarfEmitter->emitAbbrevs(Abbreviations, Options.TargetDWARFVersion);
2882       TheDwarfEmitter->emitStrings(DebugStrPool);
2883       TheDwarfEmitter->emitStringOffsets(StringOffsetPool.DieValues,
2884                                          Options.TargetDWARFVersion);
2885       TheDwarfEmitter->emitLineStrings(DebugLineStrPool);
2886       for (AccelTableKind TableKind : Options.AccelTables) {
2887         switch (TableKind) {
2888         case AccelTableKind::Apple:
2889           TheDwarfEmitter->emitAppleNamespaces(AppleNamespaces);
2890           TheDwarfEmitter->emitAppleNames(AppleNames);
2891           TheDwarfEmitter->emitAppleTypes(AppleTypes);
2892           TheDwarfEmitter->emitAppleObjc(AppleObjc);
2893           break;
2894         case AccelTableKind::Pub:
2895           // Already emitted by emitAcceleratorEntriesForUnit.
2896           // Already emitted by emitAcceleratorEntriesForUnit.
2897           break;
2898         case AccelTableKind::DebugNames:
2899           TheDwarfEmitter->emitDebugNames(DebugNames);
2900           break;
2901         }
2902       }
2903     }
2904   };
2905 
2906   auto AnalyzeAll = [&]() {
2907     for (unsigned I = 0, E = NumObjects; I != E; ++I) {
2908       AnalyzeLambda(I);
2909 
2910       std::unique_lock<std::mutex> LockGuard(ProcessedFilesMutex);
2911       ProcessedFiles.set(I);
2912       ProcessedFilesConditionVariable.notify_one();
2913     }
2914   };
2915 
2916   auto CloneAll = [&]() {
2917     for (unsigned I = 0, E = NumObjects; I != E; ++I) {
2918       {
2919         std::unique_lock<std::mutex> LockGuard(ProcessedFilesMutex);
2920         if (!ProcessedFiles[I]) {
2921           ProcessedFilesConditionVariable.wait(
2922               LockGuard, [&]() { return ProcessedFiles[I]; });
2923         }
2924       }
2925 
2926       CloneLambda(I);
2927     }
2928     EmitLambda();
2929   };
2930 
2931   // To limit memory usage in the single threaded case, analyze and clone are
2932   // run sequentially so the OptContext is freed after processing each object
2933   // in endDebugObject.
2934   if (Options.Threads == 1) {
2935     for (unsigned I = 0, E = NumObjects; I != E; ++I) {
2936       AnalyzeLambda(I);
2937       CloneLambda(I);
2938     }
2939     EmitLambda();
2940   } else {
2941     ThreadPool Pool(hardware_concurrency(2));
2942     Pool.async(AnalyzeAll);
2943     Pool.async(CloneAll);
2944     Pool.wait();
2945   }
2946 
2947   if (Options.Statistics) {
2948     // Create a vector sorted in descending order by output size.
2949     std::vector<std::pair<StringRef, DebugInfoSize>> Sorted;
2950     for (auto &E : SizeByObject)
2951       Sorted.emplace_back(E.first(), E.second);
2952     llvm::sort(Sorted, [](auto &LHS, auto &RHS) {
2953       return LHS.second.Output > RHS.second.Output;
2954     });
2955 
2956     auto ComputePercentange = [](int64_t Input, int64_t Output) -> float {
2957       const float Difference = Output - Input;
2958       const float Sum = Input + Output;
2959       if (Sum == 0)
2960         return 0;
2961       return (Difference / (Sum / 2));
2962     };
2963 
2964     int64_t InputTotal = 0;
2965     int64_t OutputTotal = 0;
2966     const char *FormatStr = "{0,-45} {1,10}b  {2,10}b {3,8:P}\n";
2967 
2968     // Print header.
2969     outs() << ".debug_info section size (in bytes)\n";
2970     outs() << "----------------------------------------------------------------"
2971               "---------------\n";
2972     outs() << "Filename                                           Object       "
2973               "  dSYM   Change\n";
2974     outs() << "----------------------------------------------------------------"
2975               "---------------\n";
2976 
2977     // Print body.
2978     for (auto &E : Sorted) {
2979       InputTotal += E.second.Input;
2980       OutputTotal += E.second.Output;
2981       llvm::outs() << formatv(
2982           FormatStr, sys::path::filename(E.first).take_back(45), E.second.Input,
2983           E.second.Output, ComputePercentange(E.second.Input, E.second.Output));
2984     }
2985     // Print total and footer.
2986     outs() << "----------------------------------------------------------------"
2987               "---------------\n";
2988     llvm::outs() << formatv(FormatStr, "Total", InputTotal, OutputTotal,
2989                             ComputePercentange(InputTotal, OutputTotal));
2990     outs() << "----------------------------------------------------------------"
2991               "---------------\n\n";
2992   }
2993 
2994   return Error::success();
2995 }
2996 
2997 Error DWARFLinker::cloneModuleUnit(LinkContext &Context, RefModuleUnit &Unit,
2998                                    DeclContextTree &ODRContexts,
2999                                    OffsetsStringPool &DebugStrPool,
3000                                    OffsetsStringPool &DebugLineStrPool,
3001                                    DebugDieValuePool &StringOffsetPool,
3002                                    unsigned Indent) {
3003   assert(Unit.Unit.get() != nullptr);
3004 
3005   if (!Unit.Unit->getOrigUnit().getUnitDIE().hasChildren())
3006     return Error::success();
3007 
3008   if (Options.Verbose) {
3009     outs().indent(Indent);
3010     outs() << "cloning .debug_info from " << Unit.File.FileName << "\n";
3011   }
3012 
3013   // Analyze context for the module.
3014   analyzeContextInfo(Unit.Unit->getOrigUnit().getUnitDIE(), 0, *(Unit.Unit),
3015                      &ODRContexts.getRoot(), ODRContexts, 0,
3016                      Options.ParseableSwiftInterfaces,
3017                      [&](const Twine &Warning, const DWARFDie &DIE) {
3018                        reportWarning(Warning, Context.File, &DIE);
3019                      });
3020   // Keep everything.
3021   Unit.Unit->markEverythingAsKept();
3022 
3023   // Clone unit.
3024   UnitListTy CompileUnits;
3025   CompileUnits.emplace_back(std::move(Unit.Unit));
3026   assert(TheDwarfEmitter);
3027   DIECloner(*this, TheDwarfEmitter.get(), Unit.File, DIEAlloc, CompileUnits,
3028             Options.Update, DebugStrPool, DebugLineStrPool, StringOffsetPool)
3029       .cloneAllCompileUnits(*Unit.File.Dwarf, Unit.File,
3030                             Unit.File.Dwarf->isLittleEndian());
3031   return Error::success();
3032 }
3033 
3034 void DWARFLinker::verifyInput(const DWARFFile &File) {
3035   assert(File.Dwarf);
3036 
3037   std::string Buffer;
3038   raw_string_ostream OS(Buffer);
3039   DIDumpOptions DumpOpts;
3040   if (!File.Dwarf->verify(OS, DumpOpts.noImplicitRecursion())) {
3041     if (Options.InputVerificationHandler)
3042       Options.InputVerificationHandler(File, OS.str());
3043   }
3044 }
3045 
3046 Error DWARFLinker::createEmitter(const Triple &TheTriple,
3047                                  OutputFileType FileType,
3048                                  raw_pwrite_stream &OutFile) {
3049 
3050   TheDwarfEmitter = std::make_unique<DwarfStreamer>(
3051       FileType, OutFile, StringsTranslator, WarningHandler);
3052 
3053   return TheDwarfEmitter->init(TheTriple, "__DWARF");
3054 }
3055 
3056 DwarfEmitter *DWARFLinker::getEmitter() { return TheDwarfEmitter.get(); }
3057 
3058 } // namespace llvm
3059