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