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