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