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/DWARFLinker.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/BitVector.h"
12 #include "llvm/ADT/Triple.h"
13 #include "llvm/CodeGen/NonRelocatableStringpool.h"
14 #include "llvm/DWARFLinker/DWARFLinkerDeclContext.h"
15 #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
16 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
17 #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
18 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
19 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
20 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
21 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
22 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
23 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
24 #include "llvm/Support/DataExtractor.h"
25 #include "llvm/Support/Error.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/ErrorOr.h"
28 #include "llvm/Support/LEB128.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/ThreadPool.h"
31 #include <vector>
32 
33 namespace llvm {
34 
35 /// Similar to DWARFUnitSection::getUnitForOffset(), but returning our
36 /// CompileUnit object instead.
37 static CompileUnit *getUnitForOffset(const UnitListTy &Units, uint64_t Offset) {
38   auto CU = std::upper_bound(
39       Units.begin(), Units.end(), Offset,
40       [](uint64_t LHS, const std::unique_ptr<CompileUnit> &RHS) {
41         return LHS < RHS->getOrigUnit().getNextUnitOffset();
42       });
43   return CU != Units.end() ? CU->get() : nullptr;
44 }
45 
46 /// Resolve the DIE attribute reference that has been extracted in \p RefValue.
47 /// The resulting DIE might be in another CompileUnit which is stored into \p
48 /// ReferencedCU. \returns null if resolving fails for any reason.
49 DWARFDie DWARFLinker::resolveDIEReference(const DwarfFile &File,
50                                           const UnitListTy &Units,
51                                           const DWARFFormValue &RefValue,
52                                           const DWARFDie &DIE,
53                                           CompileUnit *&RefCU) {
54   assert(RefValue.isFormClass(DWARFFormValue::FC_Reference));
55   uint64_t RefOffset = *RefValue.getAsReference();
56   if ((RefCU = getUnitForOffset(Units, RefOffset)))
57     if (const auto RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset)) {
58       // In a file with broken references, an attribute might point to a NULL
59       // DIE.
60       if (!RefDie.isNULL())
61         return RefDie;
62     }
63 
64   reportWarning("could not find referenced DIE", File, &DIE);
65   return DWARFDie();
66 }
67 
68 /// \returns whether the passed \a Attr type might contain a DIE reference
69 /// suitable for ODR uniquing.
70 static bool isODRAttribute(uint16_t Attr) {
71   switch (Attr) {
72   default:
73     return false;
74   case dwarf::DW_AT_type:
75   case dwarf::DW_AT_containing_type:
76   case dwarf::DW_AT_specification:
77   case dwarf::DW_AT_abstract_origin:
78   case dwarf::DW_AT_import:
79     return true;
80   }
81   llvm_unreachable("Improper attribute.");
82 }
83 
84 static bool isTypeTag(uint16_t Tag) {
85   switch (Tag) {
86   case dwarf::DW_TAG_array_type:
87   case dwarf::DW_TAG_class_type:
88   case dwarf::DW_TAG_enumeration_type:
89   case dwarf::DW_TAG_pointer_type:
90   case dwarf::DW_TAG_reference_type:
91   case dwarf::DW_TAG_string_type:
92   case dwarf::DW_TAG_structure_type:
93   case dwarf::DW_TAG_subroutine_type:
94   case dwarf::DW_TAG_typedef:
95   case dwarf::DW_TAG_union_type:
96   case dwarf::DW_TAG_ptr_to_member_type:
97   case dwarf::DW_TAG_set_type:
98   case dwarf::DW_TAG_subrange_type:
99   case dwarf::DW_TAG_base_type:
100   case dwarf::DW_TAG_const_type:
101   case dwarf::DW_TAG_constant:
102   case dwarf::DW_TAG_file_type:
103   case dwarf::DW_TAG_namelist:
104   case dwarf::DW_TAG_packed_type:
105   case dwarf::DW_TAG_volatile_type:
106   case dwarf::DW_TAG_restrict_type:
107   case dwarf::DW_TAG_atomic_type:
108   case dwarf::DW_TAG_interface_type:
109   case dwarf::DW_TAG_unspecified_type:
110   case dwarf::DW_TAG_shared_type:
111     return true;
112   default:
113     break;
114   }
115   return false;
116 }
117 
118 AddressesMap::~AddressesMap() {}
119 
120 DwarfEmitter::~DwarfEmitter() {}
121 
122 static Optional<StringRef> StripTemplateParameters(StringRef Name) {
123   // We are looking for template parameters to strip from Name. e.g.
124   //
125   //  operator<<B>
126   //
127   // We look for > at the end but if it does not contain any < then we
128   // have something like operator>>. We check for the operator<=> case.
129   if (!Name.endswith(">") || Name.count("<") == 0 || Name.endswith("<=>"))
130     return {};
131 
132   // How many < until we have the start of the template parameters.
133   size_t NumLeftAnglesToSkip = 1;
134 
135   // If we have operator<=> then we need to skip its < as well.
136   NumLeftAnglesToSkip += Name.count("<=>");
137 
138   size_t RightAngleCount = Name.count('>');
139   size_t LeftAngleCount = Name.count('<');
140 
141   // If we have more < than > we have operator< or operator<<
142   // we to account for their < as well.
143   if (LeftAngleCount > RightAngleCount)
144     NumLeftAnglesToSkip += LeftAngleCount - RightAngleCount;
145 
146   size_t StartOfTemplate = 0;
147   while (NumLeftAnglesToSkip--)
148     StartOfTemplate = Name.find('<', StartOfTemplate) + 1;
149 
150   return Name.substr(0, StartOfTemplate - 1);
151 }
152 
153 bool DWARFLinker::DIECloner::getDIENames(const DWARFDie &Die,
154                                          AttributesInfo &Info,
155                                          OffsetsStringPool &StringPool,
156                                          bool StripTemplate) {
157   // This function will be called on DIEs having low_pcs and
158   // ranges. As getting the name might be more expansive, filter out
159   // blocks directly.
160   if (Die.getTag() == dwarf::DW_TAG_lexical_block)
161     return false;
162 
163   if (!Info.MangledName)
164     if (const char *MangledName = Die.getLinkageName())
165       Info.MangledName = StringPool.getEntry(MangledName);
166 
167   if (!Info.Name)
168     if (const char *Name = Die.getShortName())
169       Info.Name = StringPool.getEntry(Name);
170 
171   if (!Info.MangledName)
172     Info.MangledName = Info.Name;
173 
174   if (StripTemplate && Info.Name && Info.MangledName != Info.Name) {
175     StringRef Name = Info.Name.getString();
176     if (Optional<StringRef> StrippedName = StripTemplateParameters(Name))
177       Info.NameWithoutTemplate = StringPool.getEntry(*StrippedName);
178   }
179 
180   return Info.Name || Info.MangledName;
181 }
182 
183 /// Resolve the relative path to a build artifact referenced by DWARF by
184 /// applying DW_AT_comp_dir.
185 static void resolveRelativeObjectPath(SmallVectorImpl<char> &Buf, DWARFDie CU) {
186   sys::path::append(Buf, dwarf::toString(CU.find(dwarf::DW_AT_comp_dir), ""));
187 }
188 
189 /// Collect references to parseable Swift interfaces in imported
190 /// DW_TAG_module blocks.
191 static void analyzeImportedModule(
192     const DWARFDie &DIE, CompileUnit &CU,
193     swiftInterfacesMap *ParseableSwiftInterfaces,
194     std::function<void(const Twine &, const DWARFDie &)> ReportWarning) {
195   if (CU.getLanguage() != dwarf::DW_LANG_Swift)
196     return;
197 
198   if (!ParseableSwiftInterfaces)
199     return;
200 
201   StringRef Path = dwarf::toStringRef(DIE.find(dwarf::DW_AT_LLVM_include_path));
202   if (!Path.endswith(".swiftinterface"))
203     return;
204   // Don't track interfaces that are part of the SDK.
205   StringRef SysRoot = dwarf::toStringRef(DIE.find(dwarf::DW_AT_LLVM_sysroot));
206   if (SysRoot.empty())
207     SysRoot = CU.getSysRoot();
208   if (!SysRoot.empty() && Path.startswith(SysRoot))
209     return;
210   if (Optional<DWARFFormValue> Val = DIE.find(dwarf::DW_AT_name))
211     if (Optional<const char *> Name = Val->getAsCString()) {
212       auto &Entry = (*ParseableSwiftInterfaces)[*Name];
213       // The prepend path is applied later when copying.
214       DWARFDie CUDie = CU.getOrigUnit().getUnitDIE();
215       SmallString<128> ResolvedPath;
216       if (sys::path::is_relative(Path))
217         resolveRelativeObjectPath(ResolvedPath, CUDie);
218       sys::path::append(ResolvedPath, Path);
219       if (!Entry.empty() && Entry != ResolvedPath)
220         ReportWarning(
221             Twine("Conflicting parseable interfaces for Swift Module ") +
222                 *Name + ": " + Entry + " and " + Path,
223             DIE);
224       Entry = std::string(ResolvedPath.str());
225     }
226 }
227 
228 /// Recursive helper to build the global DeclContext information and
229 /// gather the child->parent relationships in the original compile unit.
230 ///
231 /// \return true when this DIE and all of its children are only
232 /// forward declarations to types defined in external clang modules
233 /// (i.e., forward declarations that are children of a DW_TAG_module).
234 static bool analyzeContextInfo(
235     const DWARFDie &DIE, unsigned ParentIdx, CompileUnit &CU,
236     DeclContext *CurrentDeclContext, UniquingStringPool &StringPool,
237     DeclContextTree &Contexts, uint64_t ModulesEndOffset,
238     swiftInterfacesMap *ParseableSwiftInterfaces,
239     std::function<void(const Twine &, const DWARFDie &)> ReportWarning,
240     bool InImportedModule = false) {
241   unsigned MyIdx = CU.getOrigUnit().getDIEIndex(DIE);
242   CompileUnit::DIEInfo &Info = CU.getInfo(MyIdx);
243 
244   // Clang imposes an ODR on modules(!) regardless of the language:
245   //  "The module-id should consist of only a single identifier,
246   //   which provides the name of the module being defined. Each
247   //   module shall have a single definition."
248   //
249   // This does not extend to the types inside the modules:
250   //  "[I]n C, this implies that if two structs are defined in
251   //   different submodules with the same name, those two types are
252   //   distinct types (but may be compatible types if their
253   //   definitions match)."
254   //
255   // We treat non-C++ modules like namespaces for this reason.
256   if (DIE.getTag() == dwarf::DW_TAG_module && ParentIdx == 0 &&
257       dwarf::toString(DIE.find(dwarf::DW_AT_name), "") !=
258           CU.getClangModuleName()) {
259     InImportedModule = true;
260     analyzeImportedModule(DIE, CU, ParseableSwiftInterfaces, ReportWarning);
261   }
262 
263   Info.ParentIdx = ParentIdx;
264   bool InClangModule = CU.isClangModule() || InImportedModule;
265   if (CU.hasODR() || InClangModule) {
266     if (CurrentDeclContext) {
267       auto PtrInvalidPair = Contexts.getChildDeclContext(
268           *CurrentDeclContext, DIE, CU, StringPool, InClangModule);
269       CurrentDeclContext = PtrInvalidPair.getPointer();
270       Info.Ctxt =
271           PtrInvalidPair.getInt() ? nullptr : PtrInvalidPair.getPointer();
272       if (Info.Ctxt)
273         Info.Ctxt->setDefinedInClangModule(InClangModule);
274     } else
275       Info.Ctxt = CurrentDeclContext = nullptr;
276   }
277 
278   Info.Prune = InImportedModule;
279   if (DIE.hasChildren())
280     for (auto Child : DIE.children())
281       Info.Prune &= analyzeContextInfo(Child, MyIdx, CU, CurrentDeclContext,
282                                        StringPool, Contexts, ModulesEndOffset,
283                                        ParseableSwiftInterfaces, ReportWarning,
284                                        InImportedModule);
285 
286   // Prune this DIE if it is either a forward declaration inside a
287   // DW_TAG_module or a DW_TAG_module that contains nothing but
288   // forward declarations.
289   Info.Prune &= (DIE.getTag() == dwarf::DW_TAG_module) ||
290                 (isTypeTag(DIE.getTag()) &&
291                  dwarf::toUnsigned(DIE.find(dwarf::DW_AT_declaration), 0));
292 
293   // Only prune forward declarations inside a DW_TAG_module for which a
294   // definition exists elsewhere.
295   if (ModulesEndOffset == 0)
296     Info.Prune &= Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset();
297   else
298     Info.Prune &= Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset() > 0 &&
299                   Info.Ctxt->getCanonicalDIEOffset() <= ModulesEndOffset;
300 
301   return Info.Prune;
302 }
303 
304 static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) {
305   switch (Tag) {
306   default:
307     return false;
308   case dwarf::DW_TAG_class_type:
309   case dwarf::DW_TAG_common_block:
310   case dwarf::DW_TAG_lexical_block:
311   case dwarf::DW_TAG_structure_type:
312   case dwarf::DW_TAG_subprogram:
313   case dwarf::DW_TAG_subroutine_type:
314   case dwarf::DW_TAG_union_type:
315     return true;
316   }
317   llvm_unreachable("Invalid Tag");
318 }
319 
320 void DWARFLinker::cleanupAuxiliarryData(LinkContext &Context) {
321   Context.clear();
322 
323   for (auto I = DIEBlocks.begin(), E = DIEBlocks.end(); I != E; ++I)
324     (*I)->~DIEBlock();
325   for (auto I = DIELocs.begin(), E = DIELocs.end(); I != E; ++I)
326     (*I)->~DIELoc();
327 
328   DIEBlocks.clear();
329   DIELocs.clear();
330   DIEAlloc.Reset();
331 }
332 
333 /// Get the starting and ending (exclusive) offset for the
334 /// attribute with index \p Idx descibed by \p Abbrev. \p Offset is
335 /// supposed to point to the position of the first attribute described
336 /// by \p Abbrev.
337 /// \return [StartOffset, EndOffset) as a pair.
338 static std::pair<uint64_t, uint64_t>
339 getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx,
340                     uint64_t Offset, const DWARFUnit &Unit) {
341   DataExtractor Data = Unit.getDebugInfoExtractor();
342 
343   for (unsigned I = 0; I < Idx; ++I)
344     DWARFFormValue::skipValue(Abbrev->getFormByIndex(I), Data, &Offset,
345                               Unit.getFormParams());
346 
347   uint64_t End = Offset;
348   DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End,
349                             Unit.getFormParams());
350 
351   return std::make_pair(Offset, End);
352 }
353 
354 /// Check if a variable describing DIE should be kept.
355 /// \returns updated TraversalFlags.
356 unsigned DWARFLinker::shouldKeepVariableDIE(AddressesMap &RelocMgr,
357                                             const DWARFDie &DIE,
358                                             CompileUnit &Unit,
359                                             CompileUnit::DIEInfo &MyInfo,
360                                             unsigned Flags) {
361   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
362 
363   // Global variables with constant value can always be kept.
364   if (!(Flags & TF_InFunctionScope) &&
365       Abbrev->findAttributeIndex(dwarf::DW_AT_const_value)) {
366     MyInfo.InDebugMap = true;
367     return Flags | TF_Keep;
368   }
369 
370   Optional<uint32_t> LocationIdx =
371       Abbrev->findAttributeIndex(dwarf::DW_AT_location);
372   if (!LocationIdx)
373     return Flags;
374 
375   uint64_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
376   const DWARFUnit &OrigUnit = Unit.getOrigUnit();
377   uint64_t LocationOffset, LocationEndOffset;
378   std::tie(LocationOffset, LocationEndOffset) =
379       getAttributeOffsets(Abbrev, *LocationIdx, Offset, OrigUnit);
380 
381   // See if there is a relocation to a valid debug map entry inside
382   // this variable's location. The order is important here. We want to
383   // always check if the variable has a valid relocation, so that the
384   // DIEInfo is filled. However, we don't want a static variable in a
385   // function to force us to keep the enclosing function.
386   if (!RelocMgr.hasValidRelocationAt(LocationOffset, LocationEndOffset,
387                                      MyInfo) ||
388       (Flags & TF_InFunctionScope))
389     return Flags;
390 
391   if (Options.Verbose) {
392     outs() << "Keeping variable DIE:";
393     DIDumpOptions DumpOpts;
394     DumpOpts.ChildRecurseDepth = 0;
395     DumpOpts.Verbose = Options.Verbose;
396     DIE.dump(outs(), 8 /* Indent */, DumpOpts);
397   }
398 
399   return Flags | TF_Keep;
400 }
401 
402 /// Check if a function describing DIE should be kept.
403 /// \returns updated TraversalFlags.
404 unsigned DWARFLinker::shouldKeepSubprogramDIE(
405     AddressesMap &RelocMgr, RangesTy &Ranges, const DWARFDie &DIE,
406     const DwarfFile &File, CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
407     unsigned Flags) {
408   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
409 
410   Flags |= TF_InFunctionScope;
411 
412   Optional<uint32_t> LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc);
413   if (!LowPcIdx)
414     return Flags;
415 
416   uint64_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
417   DWARFUnit &OrigUnit = Unit.getOrigUnit();
418   uint64_t LowPcOffset, LowPcEndOffset;
419   std::tie(LowPcOffset, LowPcEndOffset) =
420       getAttributeOffsets(Abbrev, *LowPcIdx, Offset, OrigUnit);
421 
422   auto LowPc = dwarf::toAddress(DIE.find(dwarf::DW_AT_low_pc));
423   assert(LowPc.hasValue() && "low_pc attribute is not an address.");
424   if (!LowPc ||
425       !RelocMgr.hasValidRelocationAt(LowPcOffset, LowPcEndOffset, MyInfo))
426     return Flags;
427 
428   if (Options.Verbose) {
429     outs() << "Keeping subprogram DIE:";
430     DIDumpOptions DumpOpts;
431     DumpOpts.ChildRecurseDepth = 0;
432     DumpOpts.Verbose = Options.Verbose;
433     DIE.dump(outs(), 8 /* Indent */, DumpOpts);
434   }
435 
436   if (DIE.getTag() == dwarf::DW_TAG_label) {
437     if (Unit.hasLabelAt(*LowPc))
438       return Flags;
439     // FIXME: dsymutil-classic compat. dsymutil-classic doesn't consider labels
440     // that don't fall into the CU's aranges. This is wrong IMO. Debug info
441     // generation bugs aside, this is really wrong in the case of labels, where
442     // a label marking the end of a function will have a PC == CU's high_pc.
443     if (dwarf::toAddress(OrigUnit.getUnitDIE().find(dwarf::DW_AT_high_pc))
444             .getValueOr(UINT64_MAX) <= LowPc)
445       return Flags;
446     Unit.addLabelLowPc(*LowPc, MyInfo.AddrAdjust);
447     return Flags | TF_Keep;
448   }
449 
450   Flags |= TF_Keep;
451 
452   Optional<uint64_t> HighPc = DIE.getHighPC(*LowPc);
453   if (!HighPc) {
454     reportWarning("Function without high_pc. Range will be discarded.\n", File,
455                   &DIE);
456     return Flags;
457   }
458 
459   // Replace the debug map range with a more accurate one.
460   Ranges[*LowPc] = ObjFileAddressRange(*HighPc, MyInfo.AddrAdjust);
461   Unit.addFunctionRange(*LowPc, *HighPc, MyInfo.AddrAdjust);
462   return Flags;
463 }
464 
465 /// Check if a DIE should be kept.
466 /// \returns updated TraversalFlags.
467 unsigned DWARFLinker::shouldKeepDIE(AddressesMap &RelocMgr, RangesTy &Ranges,
468                                     const DWARFDie &DIE, const DwarfFile &File,
469                                     CompileUnit &Unit,
470                                     CompileUnit::DIEInfo &MyInfo,
471                                     unsigned Flags) {
472   switch (DIE.getTag()) {
473   case dwarf::DW_TAG_constant:
474   case dwarf::DW_TAG_variable:
475     return shouldKeepVariableDIE(RelocMgr, DIE, Unit, MyInfo, Flags);
476   case dwarf::DW_TAG_subprogram:
477   case dwarf::DW_TAG_label:
478     return shouldKeepSubprogramDIE(RelocMgr, Ranges, DIE, File, Unit, MyInfo,
479                                    Flags);
480   case dwarf::DW_TAG_base_type:
481     // DWARF Expressions may reference basic types, but scanning them
482     // is expensive. Basic types are tiny, so just keep all of them.
483   case dwarf::DW_TAG_imported_module:
484   case dwarf::DW_TAG_imported_declaration:
485   case dwarf::DW_TAG_imported_unit:
486     // We always want to keep these.
487     return Flags | TF_Keep;
488   default:
489     break;
490   }
491 
492   return Flags;
493 }
494 
495 /// Helper that updates the completeness of the current DIE based on the
496 /// completeness of one of its children. It depends on the incompleteness of
497 /// the children already being computed.
498 static void updateChildIncompleteness(const DWARFDie &Die, CompileUnit &CU,
499                                       CompileUnit::DIEInfo &ChildInfo) {
500   switch (Die.getTag()) {
501   case dwarf::DW_TAG_structure_type:
502   case dwarf::DW_TAG_class_type:
503     break;
504   default:
505     return;
506   }
507 
508   unsigned Idx = CU.getOrigUnit().getDIEIndex(Die);
509   CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
510 
511   if (ChildInfo.Incomplete || ChildInfo.Prune)
512     MyInfo.Incomplete = true;
513 }
514 
515 /// Helper that updates the completeness of the current DIE based on the
516 /// completeness of the DIEs it references. It depends on the incompleteness of
517 /// the referenced DIE already being computed.
518 static void updateRefIncompleteness(const DWARFDie &Die, CompileUnit &CU,
519                                     CompileUnit::DIEInfo &RefInfo) {
520   switch (Die.getTag()) {
521   case dwarf::DW_TAG_typedef:
522   case dwarf::DW_TAG_member:
523   case dwarf::DW_TAG_reference_type:
524   case dwarf::DW_TAG_ptr_to_member_type:
525   case dwarf::DW_TAG_pointer_type:
526     break;
527   default:
528     return;
529   }
530 
531   unsigned Idx = CU.getOrigUnit().getDIEIndex(Die);
532   CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
533 
534   if (MyInfo.Incomplete)
535     return;
536 
537   if (RefInfo.Incomplete)
538     MyInfo.Incomplete = true;
539 }
540 
541 /// Look at the children of the given DIE and decide whether they should be
542 /// kept.
543 void DWARFLinker::lookForChildDIEsToKeep(
544     const DWARFDie &Die, CompileUnit &CU, unsigned Flags,
545     SmallVectorImpl<WorklistItem> &Worklist) {
546   // The TF_ParentWalk flag tells us that we are currently walking up the
547   // parent chain of a required DIE, and we don't want to mark all the children
548   // of the parents as kept (consider for example a DW_TAG_namespace node in
549   // the parent chain). There are however a set of DIE types for which we want
550   // to ignore that directive and still walk their children.
551   if (dieNeedsChildrenToBeMeaningful(Die.getTag()))
552     Flags &= ~DWARFLinker::TF_ParentWalk;
553 
554   // We're finished if this DIE has no children or we're walking the parent
555   // chain.
556   if (!Die.hasChildren() || (Flags & DWARFLinker::TF_ParentWalk))
557     return;
558 
559   // Add children in reverse order to the worklist to effectively process them
560   // in order.
561   for (auto Child : reverse(Die.children())) {
562     // Add a worklist item before every child to calculate incompleteness right
563     // after the current child is processed.
564     unsigned Idx = CU.getOrigUnit().getDIEIndex(Child);
565     CompileUnit::DIEInfo &ChildInfo = CU.getInfo(Idx);
566     Worklist.emplace_back(Die, CU, WorklistItemType::UpdateChildIncompleteness,
567                           &ChildInfo);
568     Worklist.emplace_back(Child, CU, Flags);
569   }
570 }
571 
572 /// Look at DIEs referenced by the given DIE and decide whether they should be
573 /// kept. All DIEs referenced though attributes should be kept.
574 void DWARFLinker::lookForRefDIEsToKeep(
575     const DWARFDie &Die, CompileUnit &CU, unsigned Flags,
576     const UnitListTy &Units, const DwarfFile &File,
577     SmallVectorImpl<WorklistItem> &Worklist) {
578   bool UseOdr = (Flags & DWARFLinker::TF_DependencyWalk)
579                     ? (Flags & DWARFLinker::TF_ODR)
580                     : CU.hasODR();
581   DWARFUnit &Unit = CU.getOrigUnit();
582   DWARFDataExtractor Data = Unit.getDebugInfoExtractor();
583   const auto *Abbrev = Die.getAbbreviationDeclarationPtr();
584   uint64_t Offset = Die.getOffset() + getULEB128Size(Abbrev->getCode());
585 
586   SmallVector<std::pair<DWARFDie, CompileUnit &>, 4> ReferencedDIEs;
587   for (const auto &AttrSpec : Abbrev->attributes()) {
588     DWARFFormValue Val(AttrSpec.Form);
589     if (!Val.isFormClass(DWARFFormValue::FC_Reference) ||
590         AttrSpec.Attr == dwarf::DW_AT_sibling) {
591       DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset,
592                                 Unit.getFormParams());
593       continue;
594     }
595 
596     Val.extractValue(Data, &Offset, Unit.getFormParams(), &Unit);
597     CompileUnit *ReferencedCU;
598     if (auto RefDie =
599             resolveDIEReference(File, Units, Val, Die, ReferencedCU)) {
600       uint32_t RefIdx = ReferencedCU->getOrigUnit().getDIEIndex(RefDie);
601       CompileUnit::DIEInfo &Info = ReferencedCU->getInfo(RefIdx);
602       bool IsModuleRef = Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset() &&
603                          Info.Ctxt->isDefinedInClangModule();
604       // If the referenced DIE has a DeclContext that has already been
605       // emitted, then do not keep the one in this CU. We'll link to
606       // the canonical DIE in cloneDieReferenceAttribute.
607       //
608       // FIXME: compatibility with dsymutil-classic. UseODR shouldn't
609       // be necessary and could be advantageously replaced by
610       // ReferencedCU->hasODR() && CU.hasODR().
611       //
612       // FIXME: compatibility with dsymutil-classic. There is no
613       // reason not to unique ref_addr references.
614       if (AttrSpec.Form != dwarf::DW_FORM_ref_addr && (UseOdr || IsModuleRef) &&
615           Info.Ctxt &&
616           Info.Ctxt != ReferencedCU->getInfo(Info.ParentIdx).Ctxt &&
617           Info.Ctxt->getCanonicalDIEOffset() && isODRAttribute(AttrSpec.Attr))
618         continue;
619 
620       // Keep a module forward declaration if there is no definition.
621       if (!(isODRAttribute(AttrSpec.Attr) && Info.Ctxt &&
622             Info.Ctxt->getCanonicalDIEOffset()))
623         Info.Prune = false;
624       ReferencedDIEs.emplace_back(RefDie, *ReferencedCU);
625     }
626   }
627 
628   unsigned ODRFlag = UseOdr ? DWARFLinker::TF_ODR : 0;
629 
630   // Add referenced DIEs in reverse order to the worklist to effectively
631   // process them in order.
632   for (auto &P : reverse(ReferencedDIEs)) {
633     // Add a worklist item before every child to calculate incompleteness right
634     // after the current child is processed.
635     uint32_t RefIdx = P.second.getOrigUnit().getDIEIndex(P.first);
636     CompileUnit::DIEInfo &Info = P.second.getInfo(RefIdx);
637     Worklist.emplace_back(Die, CU, WorklistItemType::UpdateRefIncompleteness,
638                           &Info);
639     Worklist.emplace_back(P.first, P.second,
640                           DWARFLinker::TF_Keep |
641                               DWARFLinker::TF_DependencyWalk | ODRFlag);
642   }
643 }
644 
645 /// Look at the parent of the given DIE and decide whether they should be kept.
646 void DWARFLinker::lookForParentDIEsToKeep(
647     unsigned AncestorIdx, CompileUnit &CU, unsigned Flags,
648     SmallVectorImpl<WorklistItem> &Worklist) {
649   // Stop if we encounter an ancestor that's already marked as kept.
650   if (CU.getInfo(AncestorIdx).Keep)
651     return;
652 
653   DWARFUnit &Unit = CU.getOrigUnit();
654   DWARFDie ParentDIE = Unit.getDIEAtIndex(AncestorIdx);
655   Worklist.emplace_back(CU.getInfo(AncestorIdx).ParentIdx, CU, Flags);
656   Worklist.emplace_back(ParentDIE, CU, Flags);
657 }
658 
659 /// Recursively walk the \p DIE tree and look for DIEs to keep. Store that
660 /// information in \p CU's DIEInfo.
661 ///
662 /// This function is the entry point of the DIE selection algorithm. It is
663 /// expected to walk the DIE tree in file order and (though the mediation of
664 /// its helper) call hasValidRelocation() on each DIE that might be a 'root
665 /// DIE' (See DwarfLinker class comment).
666 ///
667 /// While walking the dependencies of root DIEs, this function is also called,
668 /// but during these dependency walks the file order is not respected. The
669 /// TF_DependencyWalk flag tells us which kind of traversal we are currently
670 /// doing.
671 ///
672 /// The recursive algorithm is implemented iteratively as a work list because
673 /// very deep recursion could exhaust the stack for large projects. The work
674 /// list acts as a scheduler for different types of work that need to be
675 /// performed.
676 ///
677 /// The recursive nature of the algorithm is simulated by running the "main"
678 /// algorithm (LookForDIEsToKeep) followed by either looking at more DIEs
679 /// (LookForChildDIEsToKeep, LookForRefDIEsToKeep, LookForParentDIEsToKeep) or
680 /// fixing up a computed property (UpdateChildIncompleteness,
681 /// UpdateRefIncompleteness).
682 ///
683 /// The return value indicates whether the DIE is incomplete.
684 void DWARFLinker::lookForDIEsToKeep(AddressesMap &AddressesMap,
685                                     RangesTy &Ranges, const UnitListTy &Units,
686                                     const DWARFDie &Die, const DwarfFile &File,
687                                     CompileUnit &Cu, unsigned Flags) {
688   // LIFO work list.
689   SmallVector<WorklistItem, 4> Worklist;
690   Worklist.emplace_back(Die, Cu, Flags);
691 
692   while (!Worklist.empty()) {
693     WorklistItem Current = Worklist.back();
694     Worklist.pop_back();
695 
696     // Look at the worklist type to decide what kind of work to perform.
697     switch (Current.Type) {
698     case WorklistItemType::UpdateChildIncompleteness:
699       updateChildIncompleteness(Current.Die, Current.CU, *Current.OtherInfo);
700       continue;
701     case WorklistItemType::UpdateRefIncompleteness:
702       updateRefIncompleteness(Current.Die, Current.CU, *Current.OtherInfo);
703       continue;
704     case WorklistItemType::LookForChildDIEsToKeep:
705       lookForChildDIEsToKeep(Current.Die, Current.CU, Current.Flags, Worklist);
706       continue;
707     case WorklistItemType::LookForRefDIEsToKeep:
708       lookForRefDIEsToKeep(Current.Die, Current.CU, Current.Flags, Units, File,
709                            Worklist);
710       continue;
711     case WorklistItemType::LookForParentDIEsToKeep:
712       lookForParentDIEsToKeep(Current.AncestorIdx, Current.CU, Current.Flags,
713                               Worklist);
714       continue;
715     case WorklistItemType::LookForDIEsToKeep:
716       break;
717     }
718 
719     unsigned Idx = Current.CU.getOrigUnit().getDIEIndex(Current.Die);
720     CompileUnit::DIEInfo &MyInfo = Current.CU.getInfo(Idx);
721 
722     if (MyInfo.Prune)
723       continue;
724 
725     // If the Keep flag is set, we are marking a required DIE's dependencies.
726     // If our target is already marked as kept, we're all set.
727     bool AlreadyKept = MyInfo.Keep;
728     if ((Current.Flags & TF_DependencyWalk) && AlreadyKept)
729       continue;
730 
731     // We must not call shouldKeepDIE while called from keepDIEAndDependencies,
732     // because it would screw up the relocation finding logic.
733     if (!(Current.Flags & TF_DependencyWalk))
734       Current.Flags = shouldKeepDIE(AddressesMap, Ranges, Current.Die, File,
735                                     Current.CU, MyInfo, Current.Flags);
736 
737     // Finish by looking for child DIEs. Because of the LIFO worklist we need
738     // to schedule that work before any subsequent items are added to the
739     // worklist.
740     Worklist.emplace_back(Current.Die, Current.CU, Current.Flags,
741                           WorklistItemType::LookForChildDIEsToKeep);
742 
743     if (AlreadyKept || !(Current.Flags & TF_Keep))
744       continue;
745 
746     // If it is a newly kept DIE mark it as well as all its dependencies as
747     // kept.
748     MyInfo.Keep = true;
749 
750     // We're looking for incomplete types.
751     MyInfo.Incomplete =
752         Current.Die.getTag() != dwarf::DW_TAG_subprogram &&
753         Current.Die.getTag() != dwarf::DW_TAG_member &&
754         dwarf::toUnsigned(Current.Die.find(dwarf::DW_AT_declaration), 0);
755 
756     // After looking at the parent chain, look for referenced DIEs. Because of
757     // the LIFO worklist we need to schedule that work before any subsequent
758     // items are added to the worklist.
759     Worklist.emplace_back(Current.Die, Current.CU, Current.Flags,
760                           WorklistItemType::LookForRefDIEsToKeep);
761 
762     bool UseOdr = (Current.Flags & TF_DependencyWalk) ? (Current.Flags & TF_ODR)
763                                                       : Current.CU.hasODR();
764     unsigned ODRFlag = UseOdr ? TF_ODR : 0;
765     unsigned ParFlags = TF_ParentWalk | TF_Keep | TF_DependencyWalk | ODRFlag;
766 
767     // Now schedule the parent walk.
768     Worklist.emplace_back(MyInfo.ParentIdx, Current.CU, ParFlags);
769   }
770 }
771 
772 /// Assign an abbreviation number to \p Abbrev.
773 ///
774 /// Our DIEs get freed after every DebugMapObject has been processed,
775 /// thus the FoldingSet we use to unique DIEAbbrevs cannot refer to
776 /// the instances hold by the DIEs. When we encounter an abbreviation
777 /// that we don't know, we create a permanent copy of it.
778 void DWARFLinker::assignAbbrev(DIEAbbrev &Abbrev) {
779   // Check the set for priors.
780   FoldingSetNodeID ID;
781   Abbrev.Profile(ID);
782   void *InsertToken;
783   DIEAbbrev *InSet = AbbreviationsSet.FindNodeOrInsertPos(ID, InsertToken);
784 
785   // If it's newly added.
786   if (InSet) {
787     // Assign existing abbreviation number.
788     Abbrev.setNumber(InSet->getNumber());
789   } else {
790     // Add to abbreviation list.
791     Abbreviations.push_back(
792         std::make_unique<DIEAbbrev>(Abbrev.getTag(), Abbrev.hasChildren()));
793     for (const auto &Attr : Abbrev.getData())
794       Abbreviations.back()->AddAttribute(Attr.getAttribute(), Attr.getForm());
795     AbbreviationsSet.InsertNode(Abbreviations.back().get(), InsertToken);
796     // Assign the unique abbreviation number.
797     Abbrev.setNumber(Abbreviations.size());
798     Abbreviations.back()->setNumber(Abbreviations.size());
799   }
800 }
801 
802 unsigned DWARFLinker::DIECloner::cloneStringAttribute(
803     DIE &Die, AttributeSpec AttrSpec, const DWARFFormValue &Val,
804     const DWARFUnit &U, OffsetsStringPool &StringPool, AttributesInfo &Info) {
805   // Switch everything to out of line strings.
806   const char *String = *Val.getAsCString();
807   auto StringEntry = StringPool.getEntry(String);
808 
809   // Update attributes info.
810   if (AttrSpec.Attr == dwarf::DW_AT_name)
811     Info.Name = StringEntry;
812   else if (AttrSpec.Attr == dwarf::DW_AT_MIPS_linkage_name ||
813            AttrSpec.Attr == dwarf::DW_AT_linkage_name)
814     Info.MangledName = StringEntry;
815 
816   Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), dwarf::DW_FORM_strp,
817                DIEInteger(StringEntry.getOffset()));
818 
819   return 4;
820 }
821 
822 unsigned DWARFLinker::DIECloner::cloneDieReferenceAttribute(
823     DIE &Die, const DWARFDie &InputDIE, AttributeSpec AttrSpec,
824     unsigned AttrSize, const DWARFFormValue &Val, const DwarfFile &File,
825     CompileUnit &Unit) {
826   const DWARFUnit &U = Unit.getOrigUnit();
827   uint64_t Ref = *Val.getAsReference();
828 
829   DIE *NewRefDie = nullptr;
830   CompileUnit *RefUnit = nullptr;
831   DeclContext *Ctxt = nullptr;
832 
833   DWARFDie RefDie =
834       Linker.resolveDIEReference(File, CompileUnits, Val, InputDIE, RefUnit);
835 
836   // If the referenced DIE is not found,  drop the attribute.
837   if (!RefDie || AttrSpec.Attr == dwarf::DW_AT_sibling)
838     return 0;
839 
840   unsigned Idx = RefUnit->getOrigUnit().getDIEIndex(RefDie);
841   CompileUnit::DIEInfo &RefInfo = RefUnit->getInfo(Idx);
842 
843   // If we already have emitted an equivalent DeclContext, just point
844   // at it.
845   if (isODRAttribute(AttrSpec.Attr)) {
846     Ctxt = RefInfo.Ctxt;
847     if (Ctxt && Ctxt->getCanonicalDIEOffset()) {
848       DIEInteger Attr(Ctxt->getCanonicalDIEOffset());
849       Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
850                    dwarf::DW_FORM_ref_addr, Attr);
851       return U.getRefAddrByteSize();
852     }
853   }
854 
855   if (!RefInfo.Clone) {
856     assert(Ref > InputDIE.getOffset());
857     // We haven't cloned this DIE yet. Just create an empty one and
858     // store it. It'll get really cloned when we process it.
859     RefInfo.Clone = DIE::get(DIEAlloc, dwarf::Tag(RefDie.getTag()));
860   }
861   NewRefDie = RefInfo.Clone;
862 
863   if (AttrSpec.Form == dwarf::DW_FORM_ref_addr ||
864       (Unit.hasODR() && isODRAttribute(AttrSpec.Attr))) {
865     // We cannot currently rely on a DIEEntry to emit ref_addr
866     // references, because the implementation calls back to DwarfDebug
867     // to find the unit offset. (We don't have a DwarfDebug)
868     // FIXME: we should be able to design DIEEntry reliance on
869     // DwarfDebug away.
870     uint64_t Attr;
871     if (Ref < InputDIE.getOffset()) {
872       // We must have already cloned that DIE.
873       uint32_t NewRefOffset =
874           RefUnit->getStartOffset() + NewRefDie->getOffset();
875       Attr = NewRefOffset;
876       Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
877                    dwarf::DW_FORM_ref_addr, DIEInteger(Attr));
878     } else {
879       // A forward reference. Note and fixup later.
880       Attr = 0xBADDEF;
881       Unit.noteForwardReference(
882           NewRefDie, RefUnit, Ctxt,
883           Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
884                        dwarf::DW_FORM_ref_addr, DIEInteger(Attr)));
885     }
886     return U.getRefAddrByteSize();
887   }
888 
889   Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
890                dwarf::Form(AttrSpec.Form), DIEEntry(*NewRefDie));
891 
892   return AttrSize;
893 }
894 
895 void DWARFLinker::DIECloner::cloneExpression(
896     DataExtractor &Data, DWARFExpression Expression, const DwarfFile &File,
897     CompileUnit &Unit, SmallVectorImpl<uint8_t> &OutputBuffer) {
898   using Encoding = DWARFExpression::Operation::Encoding;
899 
900   uint64_t OpOffset = 0;
901   for (auto &Op : Expression) {
902     auto Description = Op.getDescription();
903     // DW_OP_const_type is variable-length and has 3
904     // operands. DWARFExpression thus far only supports 2.
905     auto Op0 = Description.Op[0];
906     auto Op1 = Description.Op[1];
907     if ((Op0 == Encoding::BaseTypeRef && Op1 != Encoding::SizeNA) ||
908         (Op1 == Encoding::BaseTypeRef && Op0 != Encoding::Size1))
909       Linker.reportWarning("Unsupported DW_OP encoding.", File);
910 
911     if ((Op0 == Encoding::BaseTypeRef && Op1 == Encoding::SizeNA) ||
912         (Op1 == Encoding::BaseTypeRef && Op0 == Encoding::Size1)) {
913       // This code assumes that the other non-typeref operand fits into 1 byte.
914       assert(OpOffset < Op.getEndOffset());
915       uint32_t ULEBsize = Op.getEndOffset() - OpOffset - 1;
916       assert(ULEBsize <= 16);
917 
918       // Copy over the operation.
919       OutputBuffer.push_back(Op.getCode());
920       uint64_t RefOffset;
921       if (Op1 == Encoding::SizeNA) {
922         RefOffset = Op.getRawOperand(0);
923       } else {
924         OutputBuffer.push_back(Op.getRawOperand(0));
925         RefOffset = Op.getRawOperand(1);
926       }
927       uint32_t Offset = 0;
928       // Look up the base type. For DW_OP_convert, the operand may be 0 to
929       // instead indicate the generic type. The same holds for
930       // DW_OP_reinterpret, which is currently not supported.
931       if (RefOffset > 0 || Op.getCode() != dwarf::DW_OP_convert) {
932         auto RefDie = Unit.getOrigUnit().getDIEForOffset(RefOffset);
933         uint32_t RefIdx = Unit.getOrigUnit().getDIEIndex(RefDie);
934         CompileUnit::DIEInfo &Info = Unit.getInfo(RefIdx);
935         if (DIE *Clone = Info.Clone)
936           Offset = Clone->getOffset();
937         else
938           Linker.reportWarning(
939               "base type ref doesn't point to DW_TAG_base_type.", File);
940       }
941       uint8_t ULEB[16];
942       unsigned RealSize = encodeULEB128(Offset, ULEB, ULEBsize);
943       if (RealSize > ULEBsize) {
944         // Emit the generic type as a fallback.
945         RealSize = encodeULEB128(0, ULEB, ULEBsize);
946         Linker.reportWarning("base type ref doesn't fit.", File);
947       }
948       assert(RealSize == ULEBsize && "padding failed");
949       ArrayRef<uint8_t> ULEBbytes(ULEB, ULEBsize);
950       OutputBuffer.append(ULEBbytes.begin(), ULEBbytes.end());
951     } else {
952       // Copy over everything else unmodified.
953       StringRef Bytes = Data.getData().slice(OpOffset, Op.getEndOffset());
954       OutputBuffer.append(Bytes.begin(), Bytes.end());
955     }
956     OpOffset = Op.getEndOffset();
957   }
958 }
959 
960 unsigned DWARFLinker::DIECloner::cloneBlockAttribute(
961     DIE &Die, const DwarfFile &File, CompileUnit &Unit, AttributeSpec AttrSpec,
962     const DWARFFormValue &Val, unsigned AttrSize, bool IsLittleEndian) {
963   DIEValueList *Attr;
964   DIEValue Value;
965   DIELoc *Loc = nullptr;
966   DIEBlock *Block = nullptr;
967   if (AttrSpec.Form == dwarf::DW_FORM_exprloc) {
968     Loc = new (DIEAlloc) DIELoc;
969     Linker.DIELocs.push_back(Loc);
970   } else {
971     Block = new (DIEAlloc) DIEBlock;
972     Linker.DIEBlocks.push_back(Block);
973   }
974   Attr = Loc ? static_cast<DIEValueList *>(Loc)
975              : static_cast<DIEValueList *>(Block);
976 
977   if (Loc)
978     Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
979                      dwarf::Form(AttrSpec.Form), Loc);
980   else
981     Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
982                      dwarf::Form(AttrSpec.Form), Block);
983 
984   // If the block is a DWARF Expression, clone it into the temporary
985   // buffer using cloneExpression(), otherwise copy the data directly.
986   SmallVector<uint8_t, 32> Buffer;
987   ArrayRef<uint8_t> Bytes = *Val.getAsBlock();
988   if (DWARFAttribute::mayHaveLocationDescription(AttrSpec.Attr) &&
989       (Val.isFormClass(DWARFFormValue::FC_Block) ||
990        Val.isFormClass(DWARFFormValue::FC_Exprloc))) {
991     DWARFUnit &OrigUnit = Unit.getOrigUnit();
992     DataExtractor Data(StringRef((const char *)Bytes.data(), Bytes.size()),
993                        IsLittleEndian, OrigUnit.getAddressByteSize());
994     DWARFExpression Expr(Data, OrigUnit.getAddressByteSize());
995     cloneExpression(Data, Expr, File, Unit, Buffer);
996     Bytes = Buffer;
997   }
998   for (auto Byte : Bytes)
999     Attr->addValue(DIEAlloc, static_cast<dwarf::Attribute>(0),
1000                    dwarf::DW_FORM_data1, DIEInteger(Byte));
1001 
1002   // FIXME: If DIEBlock and DIELoc just reuses the Size field of
1003   // the DIE class, this "if" could be replaced by
1004   // Attr->setSize(Bytes.size()).
1005   if (Loc)
1006     Loc->setSize(Bytes.size());
1007   else
1008     Block->setSize(Bytes.size());
1009 
1010   Die.addValue(DIEAlloc, Value);
1011   return AttrSize;
1012 }
1013 
1014 unsigned DWARFLinker::DIECloner::cloneAddressAttribute(
1015     DIE &Die, AttributeSpec AttrSpec, const DWARFFormValue &Val,
1016     const CompileUnit &Unit, AttributesInfo &Info) {
1017   uint64_t Addr = *Val.getAsAddress();
1018 
1019   if (LLVM_UNLIKELY(Linker.Options.Update)) {
1020     if (AttrSpec.Attr == dwarf::DW_AT_low_pc)
1021       Info.HasLowPc = true;
1022     Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1023                  dwarf::Form(AttrSpec.Form), DIEInteger(Addr));
1024     return Unit.getOrigUnit().getAddressByteSize();
1025   }
1026 
1027   if (AttrSpec.Attr == dwarf::DW_AT_low_pc) {
1028     if (Die.getTag() == dwarf::DW_TAG_inlined_subroutine ||
1029         Die.getTag() == dwarf::DW_TAG_lexical_block)
1030       // The low_pc of a block or inline subroutine might get
1031       // relocated because it happens to match the low_pc of the
1032       // enclosing subprogram. To prevent issues with that, always use
1033       // the low_pc from the input DIE if relocations have been applied.
1034       Addr = (Info.OrigLowPc != std::numeric_limits<uint64_t>::max()
1035                   ? Info.OrigLowPc
1036                   : Addr) +
1037              Info.PCOffset;
1038     else if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
1039       Addr = Unit.getLowPc();
1040       if (Addr == std::numeric_limits<uint64_t>::max())
1041         return 0;
1042     }
1043     Info.HasLowPc = true;
1044   } else if (AttrSpec.Attr == dwarf::DW_AT_high_pc) {
1045     if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
1046       if (uint64_t HighPc = Unit.getHighPc())
1047         Addr = HighPc;
1048       else
1049         return 0;
1050     } else
1051       // If we have a high_pc recorded for the input DIE, use
1052       // it. Otherwise (when no relocations where applied) just use the
1053       // one we just decoded.
1054       Addr = (Info.OrigHighPc ? Info.OrigHighPc : Addr) + Info.PCOffset;
1055   } else if (AttrSpec.Attr == dwarf::DW_AT_call_return_pc) {
1056     // Relocate a return PC address within a call site entry.
1057     if (Die.getTag() == dwarf::DW_TAG_call_site)
1058       Addr = (Info.OrigCallReturnPc ? Info.OrigCallReturnPc : Addr) +
1059              Info.PCOffset;
1060   } else if (AttrSpec.Attr == dwarf::DW_AT_call_pc) {
1061     // Relocate the address of a branch instruction within a call site entry.
1062     if (Die.getTag() == dwarf::DW_TAG_call_site)
1063       Addr = (Info.OrigCallPc ? Info.OrigCallPc : Addr) + Info.PCOffset;
1064   }
1065 
1066   Die.addValue(DIEAlloc, static_cast<dwarf::Attribute>(AttrSpec.Attr),
1067                static_cast<dwarf::Form>(AttrSpec.Form), DIEInteger(Addr));
1068   return Unit.getOrigUnit().getAddressByteSize();
1069 }
1070 
1071 unsigned DWARFLinker::DIECloner::cloneScalarAttribute(
1072     DIE &Die, const DWARFDie &InputDIE, const DwarfFile &File,
1073     CompileUnit &Unit, AttributeSpec AttrSpec, const DWARFFormValue &Val,
1074     unsigned AttrSize, AttributesInfo &Info) {
1075   uint64_t Value;
1076 
1077   if (LLVM_UNLIKELY(Linker.Options.Update)) {
1078     if (auto OptionalValue = Val.getAsUnsignedConstant())
1079       Value = *OptionalValue;
1080     else if (auto OptionalValue = Val.getAsSignedConstant())
1081       Value = *OptionalValue;
1082     else if (auto OptionalValue = Val.getAsSectionOffset())
1083       Value = *OptionalValue;
1084     else {
1085       Linker.reportWarning(
1086           "Unsupported scalar attribute form. Dropping attribute.", File,
1087           &InputDIE);
1088       return 0;
1089     }
1090     if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
1091       Info.IsDeclaration = true;
1092     Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1093                  dwarf::Form(AttrSpec.Form), DIEInteger(Value));
1094     return AttrSize;
1095   }
1096 
1097   if (AttrSpec.Attr == dwarf::DW_AT_high_pc &&
1098       Die.getTag() == dwarf::DW_TAG_compile_unit) {
1099     if (Unit.getLowPc() == -1ULL)
1100       return 0;
1101     // Dwarf >= 4 high_pc is an size, not an address.
1102     Value = Unit.getHighPc() - Unit.getLowPc();
1103   } else if (AttrSpec.Form == dwarf::DW_FORM_sec_offset)
1104     Value = *Val.getAsSectionOffset();
1105   else if (AttrSpec.Form == dwarf::DW_FORM_sdata)
1106     Value = *Val.getAsSignedConstant();
1107   else if (auto OptionalValue = Val.getAsUnsignedConstant())
1108     Value = *OptionalValue;
1109   else {
1110     Linker.reportWarning(
1111         "Unsupported scalar attribute form. Dropping attribute.", File,
1112         &InputDIE);
1113     return 0;
1114   }
1115   PatchLocation Patch =
1116       Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
1117                    dwarf::Form(AttrSpec.Form), DIEInteger(Value));
1118   if (AttrSpec.Attr == dwarf::DW_AT_ranges) {
1119     Unit.noteRangeAttribute(Die, Patch);
1120     Info.HasRanges = true;
1121   }
1122 
1123   // A more generic way to check for location attributes would be
1124   // nice, but it's very unlikely that any other attribute needs a
1125   // location list.
1126   // FIXME: use DWARFAttribute::mayHaveLocationDescription().
1127   else if (AttrSpec.Attr == dwarf::DW_AT_location ||
1128            AttrSpec.Attr == dwarf::DW_AT_frame_base) {
1129     Unit.noteLocationAttribute(Patch, Info.PCOffset);
1130   } else if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
1131     Info.IsDeclaration = true;
1132 
1133   return AttrSize;
1134 }
1135 
1136 /// Clone \p InputDIE's attribute described by \p AttrSpec with
1137 /// value \p Val, and add it to \p Die.
1138 /// \returns the size of the cloned attribute.
1139 unsigned DWARFLinker::DIECloner::cloneAttribute(
1140     DIE &Die, const DWARFDie &InputDIE, const DwarfFile &File,
1141     CompileUnit &Unit, OffsetsStringPool &StringPool, const DWARFFormValue &Val,
1142     const AttributeSpec AttrSpec, unsigned AttrSize, AttributesInfo &Info,
1143     bool IsLittleEndian) {
1144   const DWARFUnit &U = Unit.getOrigUnit();
1145 
1146   switch (AttrSpec.Form) {
1147   case dwarf::DW_FORM_strp:
1148   case dwarf::DW_FORM_string:
1149     return cloneStringAttribute(Die, AttrSpec, Val, U, StringPool, Info);
1150   case dwarf::DW_FORM_ref_addr:
1151   case dwarf::DW_FORM_ref1:
1152   case dwarf::DW_FORM_ref2:
1153   case dwarf::DW_FORM_ref4:
1154   case dwarf::DW_FORM_ref8:
1155     return cloneDieReferenceAttribute(Die, InputDIE, AttrSpec, AttrSize, Val,
1156                                       File, Unit);
1157   case dwarf::DW_FORM_block:
1158   case dwarf::DW_FORM_block1:
1159   case dwarf::DW_FORM_block2:
1160   case dwarf::DW_FORM_block4:
1161   case dwarf::DW_FORM_exprloc:
1162     return cloneBlockAttribute(Die, File, Unit, AttrSpec, Val, AttrSize,
1163                                IsLittleEndian);
1164   case dwarf::DW_FORM_addr:
1165     return cloneAddressAttribute(Die, AttrSpec, Val, Unit, Info);
1166   case dwarf::DW_FORM_data1:
1167   case dwarf::DW_FORM_data2:
1168   case dwarf::DW_FORM_data4:
1169   case dwarf::DW_FORM_data8:
1170   case dwarf::DW_FORM_udata:
1171   case dwarf::DW_FORM_sdata:
1172   case dwarf::DW_FORM_sec_offset:
1173   case dwarf::DW_FORM_flag:
1174   case dwarf::DW_FORM_flag_present:
1175     return cloneScalarAttribute(Die, InputDIE, File, Unit, AttrSpec, Val,
1176                                 AttrSize, Info);
1177   default:
1178     Linker.reportWarning(
1179         "Unsupported attribute form in cloneAttribute. Dropping.", File,
1180         &InputDIE);
1181   }
1182 
1183   return 0;
1184 }
1185 
1186 static bool isObjCSelector(StringRef Name) {
1187   return Name.size() > 2 && (Name[0] == '-' || Name[0] == '+') &&
1188          (Name[1] == '[');
1189 }
1190 
1191 void DWARFLinker::DIECloner::addObjCAccelerator(CompileUnit &Unit,
1192                                                 const DIE *Die,
1193                                                 DwarfStringPoolEntryRef Name,
1194                                                 OffsetsStringPool &StringPool,
1195                                                 bool SkipPubSection) {
1196   assert(isObjCSelector(Name.getString()) && "not an objc selector");
1197   // Objective C method or class function.
1198   // "- [Class(Category) selector :withArg ...]"
1199   StringRef ClassNameStart(Name.getString().drop_front(2));
1200   size_t FirstSpace = ClassNameStart.find(' ');
1201   if (FirstSpace == StringRef::npos)
1202     return;
1203 
1204   StringRef SelectorStart(ClassNameStart.data() + FirstSpace + 1);
1205   if (!SelectorStart.size())
1206     return;
1207 
1208   StringRef Selector(SelectorStart.data(), SelectorStart.size() - 1);
1209   Unit.addNameAccelerator(Die, StringPool.getEntry(Selector), SkipPubSection);
1210 
1211   // Add an entry for the class name that points to this
1212   // method/class function.
1213   StringRef ClassName(ClassNameStart.data(), FirstSpace);
1214   Unit.addObjCAccelerator(Die, StringPool.getEntry(ClassName), SkipPubSection);
1215 
1216   if (ClassName[ClassName.size() - 1] == ')') {
1217     size_t OpenParens = ClassName.find('(');
1218     if (OpenParens != StringRef::npos) {
1219       StringRef ClassNameNoCategory(ClassName.data(), OpenParens);
1220       Unit.addObjCAccelerator(Die, StringPool.getEntry(ClassNameNoCategory),
1221                               SkipPubSection);
1222 
1223       std::string MethodNameNoCategory(Name.getString().data(), OpenParens + 2);
1224       // FIXME: The missing space here may be a bug, but
1225       //        dsymutil-classic also does it this way.
1226       MethodNameNoCategory.append(std::string(SelectorStart));
1227       Unit.addNameAccelerator(Die, StringPool.getEntry(MethodNameNoCategory),
1228                               SkipPubSection);
1229     }
1230   }
1231 }
1232 
1233 static bool
1234 shouldSkipAttribute(DWARFAbbreviationDeclaration::AttributeSpec AttrSpec,
1235                     uint16_t Tag, bool InDebugMap, bool SkipPC,
1236                     bool InFunctionScope) {
1237   switch (AttrSpec.Attr) {
1238   default:
1239     return false;
1240   case dwarf::DW_AT_low_pc:
1241   case dwarf::DW_AT_high_pc:
1242   case dwarf::DW_AT_ranges:
1243     return SkipPC;
1244   case dwarf::DW_AT_location:
1245   case dwarf::DW_AT_frame_base:
1246     // FIXME: for some reason dsymutil-classic keeps the location attributes
1247     // when they are of block type (i.e. not location lists). This is totally
1248     // wrong for globals where we will keep a wrong address. It is mostly
1249     // harmless for locals, but there is no point in keeping these anyway when
1250     // the function wasn't linked.
1251     return (SkipPC || (!InFunctionScope && Tag == dwarf::DW_TAG_variable &&
1252                        !InDebugMap)) &&
1253            !DWARFFormValue(AttrSpec.Form).isFormClass(DWARFFormValue::FC_Block);
1254   }
1255 }
1256 
1257 DIE *DWARFLinker::DIECloner::cloneDIE(const DWARFDie &InputDIE,
1258                                       const DwarfFile &File, CompileUnit &Unit,
1259                                       OffsetsStringPool &StringPool,
1260                                       int64_t PCOffset, uint32_t OutOffset,
1261                                       unsigned Flags, bool IsLittleEndian,
1262                                       DIE *Die) {
1263   DWARFUnit &U = Unit.getOrigUnit();
1264   unsigned Idx = U.getDIEIndex(InputDIE);
1265   CompileUnit::DIEInfo &Info = Unit.getInfo(Idx);
1266 
1267   // Should the DIE appear in the output?
1268   if (!Unit.getInfo(Idx).Keep)
1269     return nullptr;
1270 
1271   uint64_t Offset = InputDIE.getOffset();
1272   assert(!(Die && Info.Clone) && "Can't supply a DIE and a cloned DIE");
1273   if (!Die) {
1274     // The DIE might have been already created by a forward reference
1275     // (see cloneDieReferenceAttribute()).
1276     if (!Info.Clone)
1277       Info.Clone = DIE::get(DIEAlloc, dwarf::Tag(InputDIE.getTag()));
1278     Die = Info.Clone;
1279   }
1280 
1281   assert(Die->getTag() == InputDIE.getTag());
1282   Die->setOffset(OutOffset);
1283   if ((Unit.hasODR() || Unit.isClangModule()) && !Info.Incomplete &&
1284       Die->getTag() != dwarf::DW_TAG_namespace && Info.Ctxt &&
1285       Info.Ctxt != Unit.getInfo(Info.ParentIdx).Ctxt &&
1286       !Info.Ctxt->getCanonicalDIEOffset()) {
1287     // We are about to emit a DIE that is the root of its own valid
1288     // DeclContext tree. Make the current offset the canonical offset
1289     // for this context.
1290     Info.Ctxt->setCanonicalDIEOffset(OutOffset + Unit.getStartOffset());
1291   }
1292 
1293   // Extract and clone every attribute.
1294   DWARFDataExtractor Data = U.getDebugInfoExtractor();
1295   // Point to the next DIE (generally there is always at least a NULL
1296   // entry after the current one). If this is a lone
1297   // DW_TAG_compile_unit without any children, point to the next unit.
1298   uint64_t NextOffset = (Idx + 1 < U.getNumDIEs())
1299                             ? U.getDIEAtIndex(Idx + 1).getOffset()
1300                             : U.getNextUnitOffset();
1301   AttributesInfo AttrInfo;
1302 
1303   // We could copy the data only if we need to apply a relocation to it. After
1304   // testing, it seems there is no performance downside to doing the copy
1305   // unconditionally, and it makes the code simpler.
1306   SmallString<40> DIECopy(Data.getData().substr(Offset, NextOffset - Offset));
1307   Data =
1308       DWARFDataExtractor(DIECopy, Data.isLittleEndian(), Data.getAddressSize());
1309 
1310   // Modify the copy with relocated addresses.
1311   if (ObjFile.Addresses->areRelocationsResolved() &&
1312       ObjFile.Addresses->applyValidRelocs(DIECopy, Offset,
1313                                           Data.isLittleEndian())) {
1314     // If we applied relocations, we store the value of high_pc that was
1315     // potentially stored in the input DIE. If high_pc is an address
1316     // (Dwarf version == 2), then it might have been relocated to a
1317     // totally unrelated value (because the end address in the object
1318     // file might be start address of another function which got moved
1319     // independently by the linker). The computation of the actual
1320     // high_pc value is done in cloneAddressAttribute().
1321     AttrInfo.OrigHighPc =
1322         dwarf::toAddress(InputDIE.find(dwarf::DW_AT_high_pc), 0);
1323     // Also store the low_pc. It might get relocated in an
1324     // inline_subprogram that happens at the beginning of its
1325     // inlining function.
1326     AttrInfo.OrigLowPc = dwarf::toAddress(InputDIE.find(dwarf::DW_AT_low_pc),
1327                                           std::numeric_limits<uint64_t>::max());
1328     AttrInfo.OrigCallReturnPc =
1329         dwarf::toAddress(InputDIE.find(dwarf::DW_AT_call_return_pc), 0);
1330   }
1331 
1332   // Reset the Offset to 0 as we will be working on the local copy of
1333   // the data.
1334   Offset = 0;
1335 
1336   const auto *Abbrev = InputDIE.getAbbreviationDeclarationPtr();
1337   Offset += getULEB128Size(Abbrev->getCode());
1338 
1339   // We are entering a subprogram. Get and propagate the PCOffset.
1340   if (Die->getTag() == dwarf::DW_TAG_subprogram)
1341     PCOffset = Info.AddrAdjust;
1342   AttrInfo.PCOffset = PCOffset;
1343 
1344   if (Abbrev->getTag() == dwarf::DW_TAG_subprogram) {
1345     Flags |= TF_InFunctionScope;
1346     if (!Info.InDebugMap && LLVM_LIKELY(!Update))
1347       Flags |= TF_SkipPC;
1348   }
1349 
1350   bool Copied = false;
1351   for (const auto &AttrSpec : Abbrev->attributes()) {
1352     if (LLVM_LIKELY(!Update) &&
1353         shouldSkipAttribute(AttrSpec, Die->getTag(), Info.InDebugMap,
1354                             Flags & TF_SkipPC, Flags & TF_InFunctionScope)) {
1355       DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset,
1356                                 U.getFormParams());
1357       // FIXME: dsymutil-classic keeps the old abbreviation around
1358       // even if it's not used. We can remove this (and the copyAbbrev
1359       // helper) as soon as bit-for-bit compatibility is not a goal anymore.
1360       if (!Copied) {
1361         copyAbbrev(*InputDIE.getAbbreviationDeclarationPtr(), Unit.hasODR());
1362         Copied = true;
1363       }
1364       continue;
1365     }
1366 
1367     DWARFFormValue Val(AttrSpec.Form);
1368     uint64_t AttrSize = Offset;
1369     Val.extractValue(Data, &Offset, U.getFormParams(), &U);
1370     AttrSize = Offset - AttrSize;
1371 
1372     OutOffset += cloneAttribute(*Die, InputDIE, File, Unit, StringPool, Val,
1373                                 AttrSpec, AttrSize, AttrInfo, IsLittleEndian);
1374   }
1375 
1376   // Look for accelerator entries.
1377   uint16_t Tag = InputDIE.getTag();
1378   // FIXME: This is slightly wrong. An inline_subroutine without a
1379   // low_pc, but with AT_ranges might be interesting to get into the
1380   // accelerator tables too. For now stick with dsymutil's behavior.
1381   if ((Info.InDebugMap || AttrInfo.HasLowPc || AttrInfo.HasRanges) &&
1382       Tag != dwarf::DW_TAG_compile_unit &&
1383       getDIENames(InputDIE, AttrInfo, StringPool,
1384                   Tag != dwarf::DW_TAG_inlined_subroutine)) {
1385     if (AttrInfo.MangledName && AttrInfo.MangledName != AttrInfo.Name)
1386       Unit.addNameAccelerator(Die, AttrInfo.MangledName,
1387                               Tag == dwarf::DW_TAG_inlined_subroutine);
1388     if (AttrInfo.Name) {
1389       if (AttrInfo.NameWithoutTemplate)
1390         Unit.addNameAccelerator(Die, AttrInfo.NameWithoutTemplate,
1391                                 /* SkipPubSection */ true);
1392       Unit.addNameAccelerator(Die, AttrInfo.Name,
1393                               Tag == dwarf::DW_TAG_inlined_subroutine);
1394     }
1395     if (AttrInfo.Name && isObjCSelector(AttrInfo.Name.getString()))
1396       addObjCAccelerator(Unit, Die, AttrInfo.Name, StringPool,
1397                          /* SkipPubSection =*/true);
1398 
1399   } else if (Tag == dwarf::DW_TAG_namespace) {
1400     if (!AttrInfo.Name)
1401       AttrInfo.Name = StringPool.getEntry("(anonymous namespace)");
1402     Unit.addNamespaceAccelerator(Die, AttrInfo.Name);
1403   } else if (isTypeTag(Tag) && !AttrInfo.IsDeclaration &&
1404              getDIENames(InputDIE, AttrInfo, StringPool) && AttrInfo.Name &&
1405              AttrInfo.Name.getString()[0]) {
1406     uint32_t Hash = hashFullyQualifiedName(InputDIE, Unit, File);
1407     uint64_t RuntimeLang =
1408         dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_runtime_class))
1409             .getValueOr(0);
1410     bool ObjCClassIsImplementation =
1411         (RuntimeLang == dwarf::DW_LANG_ObjC ||
1412          RuntimeLang == dwarf::DW_LANG_ObjC_plus_plus) &&
1413         dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_objc_complete_type))
1414             .getValueOr(0);
1415     Unit.addTypeAccelerator(Die, AttrInfo.Name, ObjCClassIsImplementation,
1416                             Hash);
1417   }
1418 
1419   // Determine whether there are any children that we want to keep.
1420   bool HasChildren = false;
1421   for (auto Child : InputDIE.children()) {
1422     unsigned Idx = U.getDIEIndex(Child);
1423     if (Unit.getInfo(Idx).Keep) {
1424       HasChildren = true;
1425       break;
1426     }
1427   }
1428 
1429   DIEAbbrev NewAbbrev = Die->generateAbbrev();
1430   if (HasChildren)
1431     NewAbbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
1432   // Assign a permanent abbrev number
1433   Linker.assignAbbrev(NewAbbrev);
1434   Die->setAbbrevNumber(NewAbbrev.getNumber());
1435 
1436   // Add the size of the abbreviation number to the output offset.
1437   OutOffset += getULEB128Size(Die->getAbbrevNumber());
1438 
1439   if (!HasChildren) {
1440     // Update our size.
1441     Die->setSize(OutOffset - Die->getOffset());
1442     return Die;
1443   }
1444 
1445   // Recursively clone children.
1446   for (auto Child : InputDIE.children()) {
1447     if (DIE *Clone = cloneDIE(Child, File, Unit, StringPool, PCOffset,
1448                               OutOffset, Flags, IsLittleEndian)) {
1449       Die->addChild(Clone);
1450       OutOffset = Clone->getOffset() + Clone->getSize();
1451     }
1452   }
1453 
1454   // Account for the end of children marker.
1455   OutOffset += sizeof(int8_t);
1456   // Update our size.
1457   Die->setSize(OutOffset - Die->getOffset());
1458   return Die;
1459 }
1460 
1461 /// Patch the input object file relevant debug_ranges entries
1462 /// and emit them in the output file. Update the relevant attributes
1463 /// to point at the new entries.
1464 void DWARFLinker::patchRangesForUnit(const CompileUnit &Unit,
1465                                      DWARFContext &OrigDwarf,
1466                                      const DwarfFile &File) const {
1467   DWARFDebugRangeList RangeList;
1468   const auto &FunctionRanges = Unit.getFunctionRanges();
1469   unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
1470   DWARFDataExtractor RangeExtractor(OrigDwarf.getDWARFObj(),
1471                                     OrigDwarf.getDWARFObj().getRangesSection(),
1472                                     OrigDwarf.isLittleEndian(), AddressSize);
1473   auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
1474   DWARFUnit &OrigUnit = Unit.getOrigUnit();
1475   auto OrigUnitDie = OrigUnit.getUnitDIE(false);
1476   uint64_t OrigLowPc =
1477       dwarf::toAddress(OrigUnitDie.find(dwarf::DW_AT_low_pc), -1ULL);
1478   // Ranges addresses are based on the unit's low_pc. Compute the
1479   // offset we need to apply to adapt to the new unit's low_pc.
1480   int64_t UnitPcOffset = 0;
1481   if (OrigLowPc != -1ULL)
1482     UnitPcOffset = int64_t(OrigLowPc) - Unit.getLowPc();
1483 
1484   for (const auto &RangeAttribute : Unit.getRangesAttributes()) {
1485     uint64_t Offset = RangeAttribute.get();
1486     RangeAttribute.set(TheDwarfEmitter->getRangesSectionSize());
1487     if (Error E = RangeList.extract(RangeExtractor, &Offset)) {
1488       llvm::consumeError(std::move(E));
1489       reportWarning("invalid range list ignored.", File);
1490       RangeList.clear();
1491     }
1492     const auto &Entries = RangeList.getEntries();
1493     if (!Entries.empty()) {
1494       const DWARFDebugRangeList::RangeListEntry &First = Entries.front();
1495 
1496       if (CurrRange == InvalidRange ||
1497           First.StartAddress + OrigLowPc < CurrRange.start() ||
1498           First.StartAddress + OrigLowPc >= CurrRange.stop()) {
1499         CurrRange = FunctionRanges.find(First.StartAddress + OrigLowPc);
1500         if (CurrRange == InvalidRange ||
1501             CurrRange.start() > First.StartAddress + OrigLowPc) {
1502           reportWarning("no mapping for range.", File);
1503           continue;
1504         }
1505       }
1506     }
1507 
1508     TheDwarfEmitter->emitRangesEntries(UnitPcOffset, OrigLowPc, CurrRange,
1509                                        Entries, AddressSize);
1510   }
1511 }
1512 
1513 /// Generate the debug_aranges entries for \p Unit and if the
1514 /// unit has a DW_AT_ranges attribute, also emit the debug_ranges
1515 /// contribution for this attribute.
1516 /// FIXME: this could actually be done right in patchRangesForUnit,
1517 /// but for the sake of initial bit-for-bit compatibility with legacy
1518 /// dsymutil, we have to do it in a delayed pass.
1519 void DWARFLinker::generateUnitRanges(CompileUnit &Unit) const {
1520   auto Attr = Unit.getUnitRangesAttribute();
1521   if (Attr)
1522     Attr->set(TheDwarfEmitter->getRangesSectionSize());
1523   TheDwarfEmitter->emitUnitRangesEntries(Unit, static_cast<bool>(Attr));
1524 }
1525 
1526 /// Insert the new line info sequence \p Seq into the current
1527 /// set of already linked line info \p Rows.
1528 static void insertLineSequence(std::vector<DWARFDebugLine::Row> &Seq,
1529                                std::vector<DWARFDebugLine::Row> &Rows) {
1530   if (Seq.empty())
1531     return;
1532 
1533   if (!Rows.empty() && Rows.back().Address < Seq.front().Address) {
1534     Rows.insert(Rows.end(), Seq.begin(), Seq.end());
1535     Seq.clear();
1536     return;
1537   }
1538 
1539   object::SectionedAddress Front = Seq.front().Address;
1540   auto InsertPoint = partition_point(
1541       Rows, [=](const DWARFDebugLine::Row &O) { return O.Address < Front; });
1542 
1543   // FIXME: this only removes the unneeded end_sequence if the
1544   // sequences have been inserted in order. Using a global sort like
1545   // described in patchLineTableForUnit() and delaying the end_sequene
1546   // elimination to emitLineTableForUnit() we can get rid of all of them.
1547   if (InsertPoint != Rows.end() && InsertPoint->Address == Front &&
1548       InsertPoint->EndSequence) {
1549     *InsertPoint = Seq.front();
1550     Rows.insert(InsertPoint + 1, Seq.begin() + 1, Seq.end());
1551   } else {
1552     Rows.insert(InsertPoint, Seq.begin(), Seq.end());
1553   }
1554 
1555   Seq.clear();
1556 }
1557 
1558 static void patchStmtList(DIE &Die, DIEInteger Offset) {
1559   for (auto &V : Die.values())
1560     if (V.getAttribute() == dwarf::DW_AT_stmt_list) {
1561       V = DIEValue(V.getAttribute(), V.getForm(), Offset);
1562       return;
1563     }
1564 
1565   llvm_unreachable("Didn't find DW_AT_stmt_list in cloned DIE!");
1566 }
1567 
1568 /// Extract the line table for \p Unit from \p OrigDwarf, and
1569 /// recreate a relocated version of these for the address ranges that
1570 /// are present in the binary.
1571 void DWARFLinker::patchLineTableForUnit(CompileUnit &Unit,
1572                                         DWARFContext &OrigDwarf,
1573                                         const DwarfFile &File) {
1574   DWARFDie CUDie = Unit.getOrigUnit().getUnitDIE();
1575   auto StmtList = dwarf::toSectionOffset(CUDie.find(dwarf::DW_AT_stmt_list));
1576   if (!StmtList)
1577     return;
1578 
1579   // Update the cloned DW_AT_stmt_list with the correct debug_line offset.
1580   if (auto *OutputDIE = Unit.getOutputUnitDIE())
1581     patchStmtList(*OutputDIE,
1582                   DIEInteger(TheDwarfEmitter->getLineSectionSize()));
1583 
1584   RangesTy &Ranges = File.Addresses->getValidAddressRanges();
1585 
1586   // Parse the original line info for the unit.
1587   DWARFDebugLine::LineTable LineTable;
1588   uint64_t StmtOffset = *StmtList;
1589   DWARFDataExtractor LineExtractor(
1590       OrigDwarf.getDWARFObj(), OrigDwarf.getDWARFObj().getLineSection(),
1591       OrigDwarf.isLittleEndian(), Unit.getOrigUnit().getAddressByteSize());
1592   if (needToTranslateStrings())
1593     return TheDwarfEmitter->translateLineTable(LineExtractor, StmtOffset);
1594 
1595   if (Error Err =
1596           LineTable.parse(LineExtractor, &StmtOffset, OrigDwarf,
1597                           &Unit.getOrigUnit(), OrigDwarf.getWarningHandler()))
1598     OrigDwarf.getWarningHandler()(std::move(Err));
1599 
1600   // This vector is the output line table.
1601   std::vector<DWARFDebugLine::Row> NewRows;
1602   NewRows.reserve(LineTable.Rows.size());
1603 
1604   // Current sequence of rows being extracted, before being inserted
1605   // in NewRows.
1606   std::vector<DWARFDebugLine::Row> Seq;
1607   const auto &FunctionRanges = Unit.getFunctionRanges();
1608   auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
1609 
1610   // FIXME: This logic is meant to generate exactly the same output as
1611   // Darwin's classic dsymutil. There is a nicer way to implement this
1612   // by simply putting all the relocated line info in NewRows and simply
1613   // sorting NewRows before passing it to emitLineTableForUnit. This
1614   // should be correct as sequences for a function should stay
1615   // together in the sorted output. There are a few corner cases that
1616   // look suspicious though, and that required to implement the logic
1617   // this way. Revisit that once initial validation is finished.
1618 
1619   // Iterate over the object file line info and extract the sequences
1620   // that correspond to linked functions.
1621   for (auto &Row : LineTable.Rows) {
1622     // Check whether we stepped out of the range. The range is
1623     // half-open, but consider accept the end address of the range if
1624     // it is marked as end_sequence in the input (because in that
1625     // case, the relocation offset is accurate and that entry won't
1626     // serve as the start of another function).
1627     if (CurrRange == InvalidRange || Row.Address.Address < CurrRange.start() ||
1628         Row.Address.Address > CurrRange.stop() ||
1629         (Row.Address.Address == CurrRange.stop() && !Row.EndSequence)) {
1630       // We just stepped out of a known range. Insert a end_sequence
1631       // corresponding to the end of the range.
1632       uint64_t StopAddress = CurrRange != InvalidRange
1633                                  ? CurrRange.stop() + CurrRange.value()
1634                                  : -1ULL;
1635       CurrRange = FunctionRanges.find(Row.Address.Address);
1636       bool CurrRangeValid =
1637           CurrRange != InvalidRange && CurrRange.start() <= Row.Address.Address;
1638       if (!CurrRangeValid) {
1639         CurrRange = InvalidRange;
1640         if (StopAddress != -1ULL) {
1641           // Try harder by looking in the Address ranges map.
1642           // There are corner cases where this finds a
1643           // valid entry. It's unclear if this is right or wrong, but
1644           // for now do as dsymutil.
1645           // FIXME: Understand exactly what cases this addresses and
1646           // potentially remove it along with the Ranges map.
1647           auto Range = Ranges.lower_bound(Row.Address.Address);
1648           if (Range != Ranges.begin() && Range != Ranges.end())
1649             --Range;
1650 
1651           if (Range != Ranges.end() && Range->first <= Row.Address.Address &&
1652               Range->second.HighPC >= Row.Address.Address) {
1653             StopAddress = Row.Address.Address + Range->second.Offset;
1654           }
1655         }
1656       }
1657       if (StopAddress != -1ULL && !Seq.empty()) {
1658         // Insert end sequence row with the computed end address, but
1659         // the same line as the previous one.
1660         auto NextLine = Seq.back();
1661         NextLine.Address.Address = StopAddress;
1662         NextLine.EndSequence = 1;
1663         NextLine.PrologueEnd = 0;
1664         NextLine.BasicBlock = 0;
1665         NextLine.EpilogueBegin = 0;
1666         Seq.push_back(NextLine);
1667         insertLineSequence(Seq, NewRows);
1668       }
1669 
1670       if (!CurrRangeValid)
1671         continue;
1672     }
1673 
1674     // Ignore empty sequences.
1675     if (Row.EndSequence && Seq.empty())
1676       continue;
1677 
1678     // Relocate row address and add it to the current sequence.
1679     Row.Address.Address += CurrRange.value();
1680     Seq.emplace_back(Row);
1681 
1682     if (Row.EndSequence)
1683       insertLineSequence(Seq, NewRows);
1684   }
1685 
1686   // Finished extracting, now emit the line tables.
1687   // FIXME: LLVM hard-codes its prologue values. We just copy the
1688   // prologue over and that works because we act as both producer and
1689   // consumer. It would be nicer to have a real configurable line
1690   // table emitter.
1691   if (LineTable.Prologue.getVersion() < 2 ||
1692       LineTable.Prologue.getVersion() > 5 ||
1693       LineTable.Prologue.DefaultIsStmt != DWARF2_LINE_DEFAULT_IS_STMT ||
1694       LineTable.Prologue.OpcodeBase > 13)
1695     reportWarning("line table parameters mismatch. Cannot emit.", File);
1696   else {
1697     uint32_t PrologueEnd = *StmtList + 10 + LineTable.Prologue.PrologueLength;
1698     // DWARF v5 has an extra 2 bytes of information before the header_length
1699     // field.
1700     if (LineTable.Prologue.getVersion() == 5)
1701       PrologueEnd += 2;
1702     StringRef LineData = OrigDwarf.getDWARFObj().getLineSection().Data;
1703     MCDwarfLineTableParams Params;
1704     Params.DWARF2LineOpcodeBase = LineTable.Prologue.OpcodeBase;
1705     Params.DWARF2LineBase = LineTable.Prologue.LineBase;
1706     Params.DWARF2LineRange = LineTable.Prologue.LineRange;
1707     TheDwarfEmitter->emitLineTableForUnit(
1708         Params, LineData.slice(*StmtList + 4, PrologueEnd),
1709         LineTable.Prologue.MinInstLength, NewRows,
1710         Unit.getOrigUnit().getAddressByteSize());
1711   }
1712 }
1713 
1714 void DWARFLinker::emitAcceleratorEntriesForUnit(CompileUnit &Unit) {
1715   switch (Options.TheAccelTableKind) {
1716   case AccelTableKind::Apple:
1717     emitAppleAcceleratorEntriesForUnit(Unit);
1718     break;
1719   case AccelTableKind::Dwarf:
1720     emitDwarfAcceleratorEntriesForUnit(Unit);
1721     break;
1722   case AccelTableKind::Default:
1723     llvm_unreachable("The default must be updated to a concrete value.");
1724     break;
1725   }
1726 }
1727 
1728 void DWARFLinker::emitAppleAcceleratorEntriesForUnit(CompileUnit &Unit) {
1729   // Add namespaces.
1730   for (const auto &Namespace : Unit.getNamespaces())
1731     AppleNamespaces.addName(Namespace.Name,
1732                             Namespace.Die->getOffset() + Unit.getStartOffset());
1733 
1734   /// Add names.
1735   TheDwarfEmitter->emitPubNamesForUnit(Unit);
1736   for (const auto &Pubname : Unit.getPubnames())
1737     AppleNames.addName(Pubname.Name,
1738                        Pubname.Die->getOffset() + Unit.getStartOffset());
1739 
1740   /// Add types.
1741   TheDwarfEmitter->emitPubTypesForUnit(Unit);
1742   for (const auto &Pubtype : Unit.getPubtypes())
1743     AppleTypes.addName(
1744         Pubtype.Name, Pubtype.Die->getOffset() + Unit.getStartOffset(),
1745         Pubtype.Die->getTag(),
1746         Pubtype.ObjcClassImplementation ? dwarf::DW_FLAG_type_implementation
1747                                         : 0,
1748         Pubtype.QualifiedNameHash);
1749 
1750   /// Add ObjC names.
1751   for (const auto &ObjC : Unit.getObjC())
1752     AppleObjc.addName(ObjC.Name, ObjC.Die->getOffset() + Unit.getStartOffset());
1753 }
1754 
1755 void DWARFLinker::emitDwarfAcceleratorEntriesForUnit(CompileUnit &Unit) {
1756   for (const auto &Namespace : Unit.getNamespaces())
1757     DebugNames.addName(Namespace.Name, Namespace.Die->getOffset(),
1758                        Namespace.Die->getTag(), Unit.getUniqueID());
1759   for (const auto &Pubname : Unit.getPubnames())
1760     DebugNames.addName(Pubname.Name, Pubname.Die->getOffset(),
1761                        Pubname.Die->getTag(), Unit.getUniqueID());
1762   for (const auto &Pubtype : Unit.getPubtypes())
1763     DebugNames.addName(Pubtype.Name, Pubtype.Die->getOffset(),
1764                        Pubtype.Die->getTag(), Unit.getUniqueID());
1765 }
1766 
1767 /// Read the frame info stored in the object, and emit the
1768 /// patched frame descriptions for the resulting file.
1769 ///
1770 /// This is actually pretty easy as the data of the CIEs and FDEs can
1771 /// be considered as black boxes and moved as is. The only thing to do
1772 /// is to patch the addresses in the headers.
1773 void DWARFLinker::patchFrameInfoForObject(const DwarfFile &File,
1774                                           RangesTy &Ranges,
1775                                           DWARFContext &OrigDwarf,
1776                                           unsigned AddrSize) {
1777   StringRef FrameData = OrigDwarf.getDWARFObj().getFrameSection().Data;
1778   if (FrameData.empty())
1779     return;
1780 
1781   DataExtractor Data(FrameData, OrigDwarf.isLittleEndian(), 0);
1782   uint64_t InputOffset = 0;
1783 
1784   // Store the data of the CIEs defined in this object, keyed by their
1785   // offsets.
1786   DenseMap<uint64_t, StringRef> LocalCIES;
1787 
1788   while (Data.isValidOffset(InputOffset)) {
1789     uint64_t EntryOffset = InputOffset;
1790     uint32_t InitialLength = Data.getU32(&InputOffset);
1791     if (InitialLength == 0xFFFFFFFF)
1792       return reportWarning("Dwarf64 bits no supported", File);
1793 
1794     uint32_t CIEId = Data.getU32(&InputOffset);
1795     if (CIEId == 0xFFFFFFFF) {
1796       // This is a CIE, store it.
1797       StringRef CIEData = FrameData.substr(EntryOffset, InitialLength + 4);
1798       LocalCIES[EntryOffset] = CIEData;
1799       // The -4 is to account for the CIEId we just read.
1800       InputOffset += InitialLength - 4;
1801       continue;
1802     }
1803 
1804     uint32_t Loc = Data.getUnsigned(&InputOffset, AddrSize);
1805 
1806     // Some compilers seem to emit frame info that doesn't start at
1807     // the function entry point, thus we can't just lookup the address
1808     // in the debug map. Use the AddressInfo's range map to see if the FDE
1809     // describes something that we can relocate.
1810     auto Range = Ranges.upper_bound(Loc);
1811     if (Range != Ranges.begin())
1812       --Range;
1813     if (Range == Ranges.end() || Range->first > Loc ||
1814         Range->second.HighPC <= Loc) {
1815       // The +4 is to account for the size of the InitialLength field itself.
1816       InputOffset = EntryOffset + InitialLength + 4;
1817       continue;
1818     }
1819 
1820     // This is an FDE, and we have a mapping.
1821     // Have we already emitted a corresponding CIE?
1822     StringRef CIEData = LocalCIES[CIEId];
1823     if (CIEData.empty())
1824       return reportWarning("Inconsistent debug_frame content. Dropping.", File);
1825 
1826     // Look if we already emitted a CIE that corresponds to the
1827     // referenced one (the CIE data is the key of that lookup).
1828     auto IteratorInserted = EmittedCIEs.insert(
1829         std::make_pair(CIEData, TheDwarfEmitter->getFrameSectionSize()));
1830     // If there is no CIE yet for this ID, emit it.
1831     if (IteratorInserted.second ||
1832         // FIXME: dsymutil-classic only caches the last used CIE for
1833         // reuse. Mimic that behavior for now. Just removing that
1834         // second half of the condition and the LastCIEOffset variable
1835         // makes the code DTRT.
1836         LastCIEOffset != IteratorInserted.first->getValue()) {
1837       LastCIEOffset = TheDwarfEmitter->getFrameSectionSize();
1838       IteratorInserted.first->getValue() = LastCIEOffset;
1839       TheDwarfEmitter->emitCIE(CIEData);
1840     }
1841 
1842     // Emit the FDE with updated address and CIE pointer.
1843     // (4 + AddrSize) is the size of the CIEId + initial_location
1844     // fields that will get reconstructed by emitFDE().
1845     unsigned FDERemainingBytes = InitialLength - (4 + AddrSize);
1846     TheDwarfEmitter->emitFDE(IteratorInserted.first->getValue(), AddrSize,
1847                              Loc + Range->second.Offset,
1848                              FrameData.substr(InputOffset, FDERemainingBytes));
1849     InputOffset += FDERemainingBytes;
1850   }
1851 }
1852 
1853 void DWARFLinker::DIECloner::copyAbbrev(
1854     const DWARFAbbreviationDeclaration &Abbrev, bool HasODR) {
1855   DIEAbbrev Copy(dwarf::Tag(Abbrev.getTag()),
1856                  dwarf::Form(Abbrev.hasChildren()));
1857 
1858   for (const auto &Attr : Abbrev.attributes()) {
1859     uint16_t Form = Attr.Form;
1860     if (HasODR && isODRAttribute(Attr.Attr))
1861       Form = dwarf::DW_FORM_ref_addr;
1862     Copy.AddAttribute(dwarf::Attribute(Attr.Attr), dwarf::Form(Form));
1863   }
1864 
1865   Linker.assignAbbrev(Copy);
1866 }
1867 
1868 uint32_t DWARFLinker::DIECloner::hashFullyQualifiedName(DWARFDie DIE,
1869                                                         CompileUnit &U,
1870                                                         const DwarfFile &File,
1871                                                         int ChildRecurseDepth) {
1872   const char *Name = nullptr;
1873   DWARFUnit *OrigUnit = &U.getOrigUnit();
1874   CompileUnit *CU = &U;
1875   Optional<DWARFFormValue> Ref;
1876 
1877   while (1) {
1878     if (const char *CurrentName = DIE.getName(DINameKind::ShortName))
1879       Name = CurrentName;
1880 
1881     if (!(Ref = DIE.find(dwarf::DW_AT_specification)) &&
1882         !(Ref = DIE.find(dwarf::DW_AT_abstract_origin)))
1883       break;
1884 
1885     if (!Ref->isFormClass(DWARFFormValue::FC_Reference))
1886       break;
1887 
1888     CompileUnit *RefCU;
1889     if (auto RefDIE =
1890             Linker.resolveDIEReference(File, CompileUnits, *Ref, DIE, RefCU)) {
1891       CU = RefCU;
1892       OrigUnit = &RefCU->getOrigUnit();
1893       DIE = RefDIE;
1894     }
1895   }
1896 
1897   unsigned Idx = OrigUnit->getDIEIndex(DIE);
1898   if (!Name && DIE.getTag() == dwarf::DW_TAG_namespace)
1899     Name = "(anonymous namespace)";
1900 
1901   if (CU->getInfo(Idx).ParentIdx == 0 ||
1902       // FIXME: dsymutil-classic compatibility. Ignore modules.
1903       CU->getOrigUnit().getDIEAtIndex(CU->getInfo(Idx).ParentIdx).getTag() ==
1904           dwarf::DW_TAG_module)
1905     return djbHash(Name ? Name : "", djbHash(ChildRecurseDepth ? "" : "::"));
1906 
1907   DWARFDie Die = OrigUnit->getDIEAtIndex(CU->getInfo(Idx).ParentIdx);
1908   return djbHash(
1909       (Name ? Name : ""),
1910       djbHash((Name ? "::" : ""),
1911               hashFullyQualifiedName(Die, *CU, File, ++ChildRecurseDepth)));
1912 }
1913 
1914 static uint64_t getDwoId(const DWARFDie &CUDie, const DWARFUnit &Unit) {
1915   auto DwoId = dwarf::toUnsigned(
1916       CUDie.find({dwarf::DW_AT_dwo_id, dwarf::DW_AT_GNU_dwo_id}));
1917   if (DwoId)
1918     return *DwoId;
1919   return 0;
1920 }
1921 
1922 static std::string remapPath(StringRef Path,
1923                              const objectPrefixMap &ObjectPrefixMap) {
1924   for (const auto &Entry : ObjectPrefixMap)
1925     if (Path.startswith(Entry.first))
1926       return (Twine(Entry.second) + Path.substr(Entry.first.size())).str();
1927   return Path.str();
1928 }
1929 
1930 bool DWARFLinker::registerModuleReference(
1931     DWARFDie CUDie, const DWARFUnit &Unit, const DwarfFile &File,
1932     OffsetsStringPool &StringPool, UniquingStringPool &UniquingStringPool,
1933     DeclContextTree &ODRContexts, uint64_t ModulesEndOffset, unsigned &UnitID,
1934     bool IsLittleEndian, unsigned Indent, bool Quiet) {
1935   std::string PCMfile = dwarf::toString(
1936       CUDie.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
1937   if (PCMfile.empty())
1938     return false;
1939   if (Options.ObjectPrefixMap)
1940     PCMfile = remapPath(PCMfile, *Options.ObjectPrefixMap);
1941 
1942   // Clang module DWARF skeleton CUs abuse this for the path to the module.
1943   uint64_t DwoId = getDwoId(CUDie, Unit);
1944 
1945   std::string Name = dwarf::toString(CUDie.find(dwarf::DW_AT_name), "");
1946   if (Name.empty()) {
1947     if (!Quiet)
1948       reportWarning("Anonymous module skeleton CU for " + PCMfile, File);
1949     return true;
1950   }
1951 
1952   if (!Quiet && Options.Verbose) {
1953     outs().indent(Indent);
1954     outs() << "Found clang module reference " << PCMfile;
1955   }
1956 
1957   auto Cached = ClangModules.find(PCMfile);
1958   if (Cached != ClangModules.end()) {
1959     // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
1960     // fixed in clang, only warn about DWO_id mismatches in verbose mode.
1961     // ASTFileSignatures will change randomly when a module is rebuilt.
1962     if (!Quiet && Options.Verbose && (Cached->second != DwoId))
1963       reportWarning(Twine("hash mismatch: this object file was built against a "
1964                           "different version of the module ") +
1965                         PCMfile,
1966                     File);
1967     if (!Quiet && Options.Verbose)
1968       outs() << " [cached].\n";
1969     return true;
1970   }
1971   if (!Quiet && Options.Verbose)
1972     outs() << " ...\n";
1973 
1974   // Cyclic dependencies are disallowed by Clang, but we still
1975   // shouldn't run into an infinite loop, so mark it as processed now.
1976   ClangModules.insert({PCMfile, DwoId});
1977 
1978   if (Error E =
1979           loadClangModule(CUDie, PCMfile, Name, DwoId, File, StringPool,
1980                           UniquingStringPool, ODRContexts, ModulesEndOffset,
1981                           UnitID, IsLittleEndian, Indent + 2, Quiet)) {
1982     consumeError(std::move(E));
1983     return false;
1984   }
1985   return true;
1986 }
1987 
1988 Error DWARFLinker::loadClangModule(
1989     DWARFDie CUDie, StringRef Filename, StringRef ModuleName, uint64_t DwoId,
1990     const DwarfFile &File, OffsetsStringPool &StringPool,
1991     UniquingStringPool &UniquingStringPool, DeclContextTree &ODRContexts,
1992     uint64_t ModulesEndOffset, unsigned &UnitID, bool IsLittleEndian,
1993     unsigned Indent, bool Quiet) {
1994   /// Using a SmallString<0> because loadClangModule() is recursive.
1995   SmallString<0> Path(Options.PrependPath);
1996   if (sys::path::is_relative(Filename))
1997     resolveRelativeObjectPath(Path, CUDie);
1998   sys::path::append(Path, Filename);
1999   // Don't use the cached binary holder because we have no thread-safety
2000   // guarantee and the lifetime is limited.
2001 
2002   if (Options.ObjFileLoader == nullptr)
2003     return Error::success();
2004 
2005   auto ErrOrObj = Options.ObjFileLoader(File.FileName, Path);
2006   if (!ErrOrObj)
2007     return Error::success();
2008 
2009   std::unique_ptr<CompileUnit> Unit;
2010 
2011   for (const auto &CU : ErrOrObj->Dwarf->compile_units()) {
2012     updateDwarfVersion(CU->getVersion());
2013     // Recursively get all modules imported by this one.
2014     auto CUDie = CU->getUnitDIE(false);
2015     if (!CUDie)
2016       continue;
2017     if (!registerModuleReference(
2018             CUDie, *CU, File, StringPool, UniquingStringPool, ODRContexts,
2019             ModulesEndOffset, UnitID, IsLittleEndian, Indent, Quiet)) {
2020       if (Unit) {
2021         std::string Err =
2022             (Filename +
2023              ": Clang modules are expected to have exactly 1 compile unit.\n")
2024                 .str();
2025         reportError(Err, File);
2026         return make_error<StringError>(Err, inconvertibleErrorCode());
2027       }
2028       // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
2029       // fixed in clang, only warn about DWO_id mismatches in verbose mode.
2030       // ASTFileSignatures will change randomly when a module is rebuilt.
2031       uint64_t PCMDwoId = getDwoId(CUDie, *CU);
2032       if (PCMDwoId != DwoId) {
2033         if (!Quiet && Options.Verbose)
2034           reportWarning(
2035               Twine("hash mismatch: this object file was built against a "
2036                     "different version of the module ") +
2037                   Filename,
2038               File);
2039         // Update the cache entry with the DwoId of the module loaded from disk.
2040         ClangModules[Filename] = PCMDwoId;
2041       }
2042 
2043       // Add this module.
2044       Unit = std::make_unique<CompileUnit>(*CU, UnitID++, !Options.NoODR,
2045                                            ModuleName);
2046       Unit->setHasInterestingContent();
2047       analyzeContextInfo(CUDie, 0, *Unit, &ODRContexts.getRoot(),
2048                          UniquingStringPool, ODRContexts, ModulesEndOffset,
2049                          Options.ParseableSwiftInterfaces,
2050                          [&](const Twine &Warning, const DWARFDie &DIE) {
2051                            reportWarning(Warning, File, &DIE);
2052                          });
2053       // Keep everything.
2054       Unit->markEverythingAsKept();
2055     }
2056   }
2057   if (!Unit->getOrigUnit().getUnitDIE().hasChildren())
2058     return Error::success();
2059   if (!Quiet && Options.Verbose) {
2060     outs().indent(Indent);
2061     outs() << "cloning .debug_info from " << Filename << "\n";
2062   }
2063 
2064   UnitListTy CompileUnits;
2065   CompileUnits.push_back(std::move(Unit));
2066   assert(TheDwarfEmitter);
2067   DIECloner(*this, TheDwarfEmitter, *ErrOrObj, DIEAlloc, CompileUnits,
2068             Options.Update)
2069       .cloneAllCompileUnits(*(ErrOrObj->Dwarf), File, StringPool,
2070                             IsLittleEndian);
2071   return Error::success();
2072 }
2073 
2074 void DWARFLinker::DIECloner::cloneAllCompileUnits(DWARFContext &DwarfContext,
2075                                                   const DwarfFile &File,
2076                                                   OffsetsStringPool &StringPool,
2077                                                   bool IsLittleEndian) {
2078   uint64_t OutputDebugInfoSize =
2079       Linker.Options.NoOutput ? 0 : Emitter->getDebugInfoSectionSize();
2080   for (auto &CurrentUnit : CompileUnits) {
2081     auto InputDIE = CurrentUnit->getOrigUnit().getUnitDIE();
2082     CurrentUnit->setStartOffset(OutputDebugInfoSize);
2083     if (!InputDIE) {
2084       OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset();
2085       continue;
2086     }
2087     if (CurrentUnit->getInfo(0).Keep) {
2088       // Clone the InputDIE into your Unit DIE in our compile unit since it
2089       // already has a DIE inside of it.
2090       CurrentUnit->createOutputDIE();
2091       cloneDIE(InputDIE, File, *CurrentUnit, StringPool, 0 /* PC offset */,
2092                11 /* Unit Header size */, 0, IsLittleEndian,
2093                CurrentUnit->getOutputUnitDIE());
2094     }
2095 
2096     OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset();
2097 
2098     if (!Linker.Options.NoOutput) {
2099       assert(Emitter);
2100 
2101       if (LLVM_LIKELY(!Linker.Options.Update) ||
2102           Linker.needToTranslateStrings())
2103         Linker.patchLineTableForUnit(*CurrentUnit, DwarfContext, File);
2104 
2105       Linker.emitAcceleratorEntriesForUnit(*CurrentUnit);
2106 
2107       if (LLVM_UNLIKELY(Linker.Options.Update))
2108         continue;
2109 
2110       Linker.patchRangesForUnit(*CurrentUnit, DwarfContext, File);
2111       auto ProcessExpr = [&](StringRef Bytes,
2112                              SmallVectorImpl<uint8_t> &Buffer) {
2113         DWARFUnit &OrigUnit = CurrentUnit->getOrigUnit();
2114         DataExtractor Data(Bytes, IsLittleEndian,
2115                            OrigUnit.getAddressByteSize());
2116         cloneExpression(Data,
2117                         DWARFExpression(Data, OrigUnit.getAddressByteSize()),
2118                         File, *CurrentUnit, Buffer);
2119       };
2120       Emitter->emitLocationsForUnit(*CurrentUnit, DwarfContext, ProcessExpr);
2121     }
2122   }
2123 
2124   if (!Linker.Options.NoOutput) {
2125     assert(Emitter);
2126     // Emit all the compile unit's debug information.
2127     for (auto &CurrentUnit : CompileUnits) {
2128       if (LLVM_LIKELY(!Linker.Options.Update))
2129         Linker.generateUnitRanges(*CurrentUnit);
2130 
2131       CurrentUnit->fixupForwardReferences();
2132 
2133       if (!CurrentUnit->getOutputUnitDIE())
2134         continue;
2135 
2136       assert(Emitter->getDebugInfoSectionSize() ==
2137              CurrentUnit->getStartOffset());
2138       Emitter->emitCompileUnitHeader(*CurrentUnit);
2139       Emitter->emitDIE(*CurrentUnit->getOutputUnitDIE());
2140       assert(Emitter->getDebugInfoSectionSize() ==
2141              CurrentUnit->computeNextUnitOffset());
2142     }
2143   }
2144 }
2145 
2146 void DWARFLinker::updateAccelKind(DWARFContext &Dwarf) {
2147   if (Options.TheAccelTableKind != AccelTableKind::Default)
2148     return;
2149 
2150   auto &DwarfObj = Dwarf.getDWARFObj();
2151 
2152   if (!AtLeastOneDwarfAccelTable &&
2153       (!DwarfObj.getAppleNamesSection().Data.empty() ||
2154        !DwarfObj.getAppleTypesSection().Data.empty() ||
2155        !DwarfObj.getAppleNamespacesSection().Data.empty() ||
2156        !DwarfObj.getAppleObjCSection().Data.empty())) {
2157     AtLeastOneAppleAccelTable = true;
2158   }
2159 
2160   if (!AtLeastOneDwarfAccelTable && !DwarfObj.getNamesSection().Data.empty()) {
2161     AtLeastOneDwarfAccelTable = true;
2162   }
2163 }
2164 
2165 bool DWARFLinker::emitPaperTrailWarnings(const DwarfFile &File,
2166                                          OffsetsStringPool &StringPool) {
2167 
2168   if (File.Warnings.empty())
2169     return false;
2170 
2171   DIE *CUDie = DIE::get(DIEAlloc, dwarf::DW_TAG_compile_unit);
2172   CUDie->setOffset(11);
2173   StringRef Producer;
2174   StringRef WarningHeader;
2175 
2176   switch (DwarfLinkerClientID) {
2177   case DwarfLinkerClient::Dsymutil:
2178     Producer = StringPool.internString("dsymutil");
2179     WarningHeader = "dsymutil_warning";
2180     break;
2181 
2182   default:
2183     Producer = StringPool.internString("dwarfopt");
2184     WarningHeader = "dwarfopt_warning";
2185     break;
2186   }
2187 
2188   StringRef FileName = StringPool.internString(File.FileName);
2189   CUDie->addValue(DIEAlloc, dwarf::DW_AT_producer, dwarf::DW_FORM_strp,
2190                   DIEInteger(StringPool.getStringOffset(Producer)));
2191   DIEBlock *String = new (DIEAlloc) DIEBlock();
2192   DIEBlocks.push_back(String);
2193   for (auto &C : FileName)
2194     String->addValue(DIEAlloc, dwarf::Attribute(0), dwarf::DW_FORM_data1,
2195                      DIEInteger(C));
2196   String->addValue(DIEAlloc, dwarf::Attribute(0), dwarf::DW_FORM_data1,
2197                    DIEInteger(0));
2198 
2199   CUDie->addValue(DIEAlloc, dwarf::DW_AT_name, dwarf::DW_FORM_string, String);
2200   for (const auto &Warning : File.Warnings) {
2201     DIE &ConstDie = CUDie->addChild(DIE::get(DIEAlloc, dwarf::DW_TAG_constant));
2202     ConstDie.addValue(DIEAlloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp,
2203                       DIEInteger(StringPool.getStringOffset(WarningHeader)));
2204     ConstDie.addValue(DIEAlloc, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag,
2205                       DIEInteger(1));
2206     ConstDie.addValue(DIEAlloc, dwarf::DW_AT_const_value, dwarf::DW_FORM_strp,
2207                       DIEInteger(StringPool.getStringOffset(Warning)));
2208   }
2209   unsigned Size = 4 /* FORM_strp */ + FileName.size() + 1 +
2210                   File.Warnings.size() * (4 + 1 + 4) + 1 /* End of children */;
2211   DIEAbbrev Abbrev = CUDie->generateAbbrev();
2212   assignAbbrev(Abbrev);
2213   CUDie->setAbbrevNumber(Abbrev.getNumber());
2214   Size += getULEB128Size(Abbrev.getNumber());
2215   // Abbreviation ordering needed for classic compatibility.
2216   for (auto &Child : CUDie->children()) {
2217     Abbrev = Child.generateAbbrev();
2218     assignAbbrev(Abbrev);
2219     Child.setAbbrevNumber(Abbrev.getNumber());
2220     Size += getULEB128Size(Abbrev.getNumber());
2221   }
2222   CUDie->setSize(Size);
2223   TheDwarfEmitter->emitPaperTrailWarningsDie(*CUDie);
2224 
2225   return true;
2226 }
2227 
2228 void DWARFLinker::copyInvariantDebugSection(DWARFContext &Dwarf) {
2229   if (!needToTranslateStrings())
2230     TheDwarfEmitter->emitSectionContents(
2231         Dwarf.getDWARFObj().getLineSection().Data, "debug_line");
2232   TheDwarfEmitter->emitSectionContents(Dwarf.getDWARFObj().getLocSection().Data,
2233                                        "debug_loc");
2234   TheDwarfEmitter->emitSectionContents(
2235       Dwarf.getDWARFObj().getRangesSection().Data, "debug_ranges");
2236   TheDwarfEmitter->emitSectionContents(
2237       Dwarf.getDWARFObj().getFrameSection().Data, "debug_frame");
2238   TheDwarfEmitter->emitSectionContents(Dwarf.getDWARFObj().getArangesSection(),
2239                                        "debug_aranges");
2240 }
2241 
2242 void DWARFLinker::addObjectFile(DwarfFile &File) {
2243   ObjectContexts.emplace_back(LinkContext(File));
2244 
2245   if (ObjectContexts.back().File.Dwarf)
2246     updateAccelKind(*ObjectContexts.back().File.Dwarf);
2247 }
2248 
2249 bool DWARFLinker::link() {
2250   assert(Options.NoOutput || TheDwarfEmitter);
2251 
2252   // A unique ID that identifies each compile unit.
2253   unsigned UnitID = 0;
2254 
2255   // First populate the data structure we need for each iteration of the
2256   // parallel loop.
2257   unsigned NumObjects = ObjectContexts.size();
2258 
2259   // This Dwarf string pool which is only used for uniquing. This one should
2260   // never be used for offsets as its not thread-safe or predictable.
2261   UniquingStringPool UniquingStringPool(nullptr, true);
2262 
2263   // This Dwarf string pool which is used for emission. It must be used
2264   // serially as the order of calling getStringOffset matters for
2265   // reproducibility.
2266   OffsetsStringPool OffsetsStringPool(StringsTranslator, true);
2267 
2268   // ODR Contexts for the optimize.
2269   DeclContextTree ODRContexts;
2270 
2271   // If we haven't decided on an accelerator table kind yet, we base ourselves
2272   // on the DWARF we have seen so far. At this point we haven't pulled in debug
2273   // information from modules yet, so it is technically possible that they
2274   // would affect the decision. However, as they're built with the same
2275   // compiler and flags, it is safe to assume that they will follow the
2276   // decision made here.
2277   if (Options.TheAccelTableKind == AccelTableKind::Default) {
2278     if (AtLeastOneDwarfAccelTable && !AtLeastOneAppleAccelTable)
2279       Options.TheAccelTableKind = AccelTableKind::Dwarf;
2280     else
2281       Options.TheAccelTableKind = AccelTableKind::Apple;
2282   }
2283 
2284   for (LinkContext &OptContext : ObjectContexts) {
2285     if (Options.Verbose) {
2286       if (DwarfLinkerClientID == DwarfLinkerClient::Dsymutil)
2287         outs() << "DEBUG MAP OBJECT: " << OptContext.File.FileName << "\n";
2288       else
2289         outs() << "OBJECT FILE: " << OptContext.File.FileName << "\n";
2290     }
2291 
2292     if (emitPaperTrailWarnings(OptContext.File, OffsetsStringPool))
2293       continue;
2294 
2295     if (!OptContext.File.Dwarf)
2296       continue;
2297     // Look for relocations that correspond to address map entries.
2298 
2299     // there was findvalidrelocations previously ... probably we need to gather
2300     // info here
2301     if (LLVM_LIKELY(!Options.Update) &&
2302         !OptContext.File.Addresses->hasValidRelocs()) {
2303       if (Options.Verbose)
2304         outs() << "No valid relocations found. Skipping.\n";
2305 
2306       // Set "Skip" flag as a signal to other loops that we should not
2307       // process this iteration.
2308       OptContext.Skip = true;
2309       continue;
2310     }
2311 
2312     // Setup access to the debug info.
2313     if (!OptContext.File.Dwarf)
2314       continue;
2315 
2316     // In a first phase, just read in the debug info and load all clang modules.
2317     OptContext.CompileUnits.reserve(
2318         OptContext.File.Dwarf->getNumCompileUnits());
2319 
2320     for (const auto &CU : OptContext.File.Dwarf->compile_units()) {
2321       updateDwarfVersion(CU->getVersion());
2322       auto CUDie = CU->getUnitDIE(false);
2323       if (Options.Verbose) {
2324         outs() << "Input compilation unit:";
2325         DIDumpOptions DumpOpts;
2326         DumpOpts.ChildRecurseDepth = 0;
2327         DumpOpts.Verbose = Options.Verbose;
2328         CUDie.dump(outs(), 0, DumpOpts);
2329       }
2330       if (CUDie && !LLVM_UNLIKELY(Options.Update))
2331         registerModuleReference(CUDie, *CU, OptContext.File, OffsetsStringPool,
2332                                 UniquingStringPool, ODRContexts, 0, UnitID,
2333                                 OptContext.File.Dwarf->isLittleEndian());
2334     }
2335   }
2336 
2337   // If we haven't seen any CUs, pick an arbitrary valid Dwarf version anyway.
2338   if (MaxDwarfVersion == 0)
2339     MaxDwarfVersion = 3;
2340 
2341   // At this point we know how much data we have emitted. We use this value to
2342   // compare canonical DIE offsets in analyzeContextInfo to see if a definition
2343   // is already emitted, without being affected by canonical die offsets set
2344   // later. This prevents undeterminism when analyze and clone execute
2345   // concurrently, as clone set the canonical DIE offset and analyze reads it.
2346   const uint64_t ModulesEndOffset =
2347       Options.NoOutput ? 0 : TheDwarfEmitter->getDebugInfoSectionSize();
2348 
2349   // These variables manage the list of processed object files.
2350   // The mutex and condition variable are to ensure that this is thread safe.
2351   std::mutex ProcessedFilesMutex;
2352   std::condition_variable ProcessedFilesConditionVariable;
2353   BitVector ProcessedFiles(NumObjects, false);
2354 
2355   //  Analyzing the context info is particularly expensive so it is executed in
2356   //  parallel with emitting the previous compile unit.
2357   auto AnalyzeLambda = [&](size_t I) {
2358     auto &Context = ObjectContexts[I];
2359 
2360     if (Context.Skip || !Context.File.Dwarf)
2361       return;
2362 
2363     for (const auto &CU : Context.File.Dwarf->compile_units()) {
2364       updateDwarfVersion(CU->getVersion());
2365       // The !registerModuleReference() condition effectively skips
2366       // over fully resolved skeleton units. This second pass of
2367       // registerModuleReferences doesn't do any new work, but it
2368       // will collect top-level errors, which are suppressed. Module
2369       // warnings were already displayed in the first iteration.
2370       bool Quiet = true;
2371       auto CUDie = CU->getUnitDIE(false);
2372       if (!CUDie || LLVM_UNLIKELY(Options.Update) ||
2373           !registerModuleReference(CUDie, *CU, Context.File, OffsetsStringPool,
2374                                    UniquingStringPool, ODRContexts,
2375                                    ModulesEndOffset, UnitID, Quiet)) {
2376         Context.CompileUnits.push_back(std::make_unique<CompileUnit>(
2377             *CU, UnitID++, !Options.NoODR && !Options.Update, ""));
2378       }
2379     }
2380 
2381     // Now build the DIE parent links that we will use during the next phase.
2382     for (auto &CurrentUnit : Context.CompileUnits) {
2383       auto CUDie = CurrentUnit->getOrigUnit().getUnitDIE();
2384       if (!CUDie)
2385         continue;
2386       analyzeContextInfo(CurrentUnit->getOrigUnit().getUnitDIE(), 0,
2387                          *CurrentUnit, &ODRContexts.getRoot(),
2388                          UniquingStringPool, ODRContexts, ModulesEndOffset,
2389                          Options.ParseableSwiftInterfaces,
2390                          [&](const Twine &Warning, const DWARFDie &DIE) {
2391                            reportWarning(Warning, Context.File, &DIE);
2392                          });
2393     }
2394   };
2395 
2396   // And then the remaining work in serial again.
2397   // Note, although this loop runs in serial, it can run in parallel with
2398   // the analyzeContextInfo loop so long as we process files with indices >=
2399   // than those processed by analyzeContextInfo.
2400   auto CloneLambda = [&](size_t I) {
2401     auto &OptContext = ObjectContexts[I];
2402     if (OptContext.Skip || !OptContext.File.Dwarf)
2403       return;
2404 
2405     // Then mark all the DIEs that need to be present in the generated output
2406     // and collect some information about them.
2407     // Note that this loop can not be merged with the previous one because
2408     // cross-cu references require the ParentIdx to be setup for every CU in
2409     // the object file before calling this.
2410     if (LLVM_UNLIKELY(Options.Update)) {
2411       for (auto &CurrentUnit : OptContext.CompileUnits)
2412         CurrentUnit->markEverythingAsKept();
2413       copyInvariantDebugSection(*OptContext.File.Dwarf);
2414     } else {
2415       for (auto &CurrentUnit : OptContext.CompileUnits)
2416         lookForDIEsToKeep(*OptContext.File.Addresses,
2417                           OptContext.File.Addresses->getValidAddressRanges(),
2418                           OptContext.CompileUnits,
2419                           CurrentUnit->getOrigUnit().getUnitDIE(),
2420                           OptContext.File, *CurrentUnit, 0);
2421     }
2422 
2423     // The calls to applyValidRelocs inside cloneDIE will walk the reloc
2424     // array again (in the same way findValidRelocsInDebugInfo() did). We
2425     // need to reset the NextValidReloc index to the beginning.
2426     if (OptContext.File.Addresses->hasValidRelocs() ||
2427         LLVM_UNLIKELY(Options.Update)) {
2428       DIECloner(*this, TheDwarfEmitter, OptContext.File, DIEAlloc,
2429                 OptContext.CompileUnits, Options.Update)
2430           .cloneAllCompileUnits(*OptContext.File.Dwarf, OptContext.File,
2431                                 OffsetsStringPool,
2432                                 OptContext.File.Dwarf->isLittleEndian());
2433     }
2434     if (!Options.NoOutput && !OptContext.CompileUnits.empty() &&
2435         LLVM_LIKELY(!Options.Update))
2436       patchFrameInfoForObject(
2437           OptContext.File, OptContext.File.Addresses->getValidAddressRanges(),
2438           *OptContext.File.Dwarf,
2439           OptContext.CompileUnits[0]->getOrigUnit().getAddressByteSize());
2440 
2441     // Clean-up before starting working on the next object.
2442     cleanupAuxiliarryData(OptContext);
2443   };
2444 
2445   auto EmitLambda = [&]() {
2446     // Emit everything that's global.
2447     if (!Options.NoOutput) {
2448       TheDwarfEmitter->emitAbbrevs(Abbreviations, MaxDwarfVersion);
2449       TheDwarfEmitter->emitStrings(OffsetsStringPool);
2450       switch (Options.TheAccelTableKind) {
2451       case AccelTableKind::Apple:
2452         TheDwarfEmitter->emitAppleNames(AppleNames);
2453         TheDwarfEmitter->emitAppleNamespaces(AppleNamespaces);
2454         TheDwarfEmitter->emitAppleTypes(AppleTypes);
2455         TheDwarfEmitter->emitAppleObjc(AppleObjc);
2456         break;
2457       case AccelTableKind::Dwarf:
2458         TheDwarfEmitter->emitDebugNames(DebugNames);
2459         break;
2460       case AccelTableKind::Default:
2461         llvm_unreachable("Default should have already been resolved.");
2462         break;
2463       }
2464     }
2465   };
2466 
2467   auto AnalyzeAll = [&]() {
2468     for (unsigned I = 0, E = NumObjects; I != E; ++I) {
2469       AnalyzeLambda(I);
2470 
2471       std::unique_lock<std::mutex> LockGuard(ProcessedFilesMutex);
2472       ProcessedFiles.set(I);
2473       ProcessedFilesConditionVariable.notify_one();
2474     }
2475   };
2476 
2477   auto CloneAll = [&]() {
2478     for (unsigned I = 0, E = NumObjects; I != E; ++I) {
2479       {
2480         std::unique_lock<std::mutex> LockGuard(ProcessedFilesMutex);
2481         if (!ProcessedFiles[I]) {
2482           ProcessedFilesConditionVariable.wait(
2483               LockGuard, [&]() { return ProcessedFiles[I]; });
2484         }
2485       }
2486 
2487       CloneLambda(I);
2488     }
2489     EmitLambda();
2490   };
2491 
2492   // To limit memory usage in the single threaded case, analyze and clone are
2493   // run sequentially so the OptContext is freed after processing each object
2494   // in endDebugObject.
2495   if (Options.Threads == 1) {
2496     for (unsigned I = 0, E = NumObjects; I != E; ++I) {
2497       AnalyzeLambda(I);
2498       CloneLambda(I);
2499     }
2500     EmitLambda();
2501   } else {
2502     ThreadPool Pool(hardware_concurrency(2));
2503     Pool.async(AnalyzeAll);
2504     Pool.async(CloneAll);
2505     Pool.wait();
2506   }
2507 
2508   return true;
2509 }
2510 
2511 } // namespace llvm
2512