OSDN Git Service

16318e204f9f59f3d36490e35c4f485425d019b3
[android-x86/external-llvm.git] / tools / dsymutil / DwarfLinker.cpp
1 //===- tools/dsymutil/DwarfLinker.cpp - Dwarf debug info linker -----------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "BinaryHolder.h"
11 #include "DebugMap.h"
12 #include "ErrorReporting.h"
13 #include "MachOUtils.h"
14 #include "NonRelocatableStringpool.h"
15 #include "dsymutil.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/BitVector.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/DenseMapInfo.h"
20 #include "llvm/ADT/DenseSet.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/ADT/Hashing.h"
23 #include "llvm/ADT/IntervalMap.h"
24 #include "llvm/ADT/None.h"
25 #include "llvm/ADT/Optional.h"
26 #include "llvm/ADT/PointerIntPair.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/ADT/SmallString.h"
29 #include "llvm/ADT/StringMap.h"
30 #include "llvm/ADT/StringRef.h"
31 #include "llvm/ADT/Triple.h"
32 #include "llvm/ADT/Twine.h"
33 #include "llvm/BinaryFormat/Dwarf.h"
34 #include "llvm/BinaryFormat/MachO.h"
35 #include "llvm/CodeGen/AccelTable.h"
36 #include "llvm/CodeGen/AsmPrinter.h"
37 #include "llvm/CodeGen/DIE.h"
38 #include "llvm/Config/config.h"
39 #include "llvm/DebugInfo/DIContext.h"
40 #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
41 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
42 #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
43 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
44 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
45 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
46 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
47 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
48 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
49 #include "llvm/MC/MCAsmBackend.h"
50 #include "llvm/MC/MCAsmInfo.h"
51 #include "llvm/MC/MCCodeEmitter.h"
52 #include "llvm/MC/MCContext.h"
53 #include "llvm/MC/MCDwarf.h"
54 #include "llvm/MC/MCInstrInfo.h"
55 #include "llvm/MC/MCObjectFileInfo.h"
56 #include "llvm/MC/MCRegisterInfo.h"
57 #include "llvm/MC/MCSection.h"
58 #include "llvm/MC/MCStreamer.h"
59 #include "llvm/MC/MCSubtargetInfo.h"
60 #include "llvm/MC/MCTargetOptions.h"
61 #include "llvm/MC/MCTargetOptionsCommandFlags.inc"
62 #include "llvm/Object/MachO.h"
63 #include "llvm/Object/ObjectFile.h"
64 #include "llvm/Object/SymbolicFile.h"
65 #include "llvm/Support/Allocator.h"
66 #include "llvm/Support/Casting.h"
67 #include "llvm/Support/Compiler.h"
68 #include "llvm/Support/DJB.h"
69 #include "llvm/Support/DataExtractor.h"
70 #include "llvm/Support/Error.h"
71 #include "llvm/Support/ErrorHandling.h"
72 #include "llvm/Support/ErrorOr.h"
73 #include "llvm/Support/FileSystem.h"
74 #include "llvm/Support/Format.h"
75 #include "llvm/Support/LEB128.h"
76 #include "llvm/Support/MathExtras.h"
77 #include "llvm/Support/MemoryBuffer.h"
78 #include "llvm/Support/Path.h"
79 #include "llvm/Support/TargetRegistry.h"
80 #include "llvm/Support/ThreadPool.h"
81 #include "llvm/Support/ToolOutputFile.h"
82 #include "llvm/Support/raw_ostream.h"
83 #include "llvm/Target/TargetMachine.h"
84 #include "llvm/Target/TargetOptions.h"
85 #include <algorithm>
86 #include <cassert>
87 #include <cinttypes>
88 #include <climits>
89 #include <cstdint>
90 #include <cstdlib>
91 #include <cstring>
92 #include <limits>
93 #include <map>
94 #include <memory>
95 #include <string>
96 #include <system_error>
97 #include <tuple>
98 #include <utility>
99 #include <vector>
100
101 namespace llvm {
102 namespace dsymutil {
103
104 namespace {
105
106 /// Helper for making strong types.
107 template <typename T, typename S> class StrongType : public T {
108 public:
109   template <typename... Args>
110   explicit StrongType(Args... A) : T(std::forward<Args>(A)...) {}
111 };
112
113 /// It's very easy to introduce bugs by passing the wrong string pool. By using
114 /// strong types the interface enforces that the right kind of pool is used.
115 struct UniqueTag {};
116 struct OffsetsTag {};
117 using UniquingStringPool = StrongType<NonRelocatableStringpool, UniqueTag>;
118 using OffsetsStringPool = StrongType<NonRelocatableStringpool, OffsetsTag>;
119
120 /// Small helper that resolves and caches file paths. This helps reduce the
121 /// number of calls to realpath which is expensive. We assume the input are
122 /// files, and cache the realpath of their parent. This way we can quickly
123 /// resolve different files under the same path.
124 class CachedPathResolver {
125 public:
126   /// Resolve a path by calling realpath and cache its result. The returned
127   /// StringRef is interned in the given \p StringPool.
128   StringRef resolve(std::string Path, NonRelocatableStringpool &StringPool) {
129     StringRef FileName = sys::path::filename(Path);
130     SmallString<256> ParentPath = sys::path::parent_path(Path);
131
132     // If the ParentPath has not yet been resolved, resolve and cache it for
133     // future look-ups.
134     if (!ResolvedPaths.count(ParentPath)) {
135       SmallString<256> RealPath;
136       sys::fs::real_path(ParentPath, RealPath);
137       ResolvedPaths.insert({ParentPath, StringRef(RealPath).str()});
138     }
139
140     // Join the file name again with the resolved path.
141     SmallString<256> ResolvedPath(ResolvedPaths[ParentPath]);
142     sys::path::append(ResolvedPath, FileName);
143     return StringPool.internString(ResolvedPath);
144   }
145
146 private:
147   StringMap<std::string> ResolvedPaths;
148 };
149
150 /// Retrieve the section named \a SecName in \a Obj.
151 ///
152 /// To accommodate for platform discrepancies, the name passed should be
153 /// (for example) 'debug_info' to match either '__debug_info' or '.debug_info'.
154 /// This function will strip the initial platform-specific characters.
155 static Optional<object::SectionRef>
156 getSectionByName(const object::ObjectFile &Obj, StringRef SecName) {
157   for (const object::SectionRef &Section : Obj.sections()) {
158     StringRef SectionName;
159     Section.getName(SectionName);
160     SectionName = SectionName.substr(SectionName.find_first_not_of("._"));
161     if (SectionName != SecName)
162       continue;
163     return Section;
164   }
165   return None;
166 }
167
168 template <typename KeyT, typename ValT>
169 using HalfOpenIntervalMap =
170     IntervalMap<KeyT, ValT, IntervalMapImpl::NodeSizer<KeyT, ValT>::LeafSize,
171                 IntervalMapHalfOpenInfo<KeyT>>;
172
173 using FunctionIntervals = HalfOpenIntervalMap<uint64_t, int64_t>;
174
175 // FIXME: Delete this structure.
176 struct PatchLocation {
177   DIE::value_iterator I;
178
179   PatchLocation() = default;
180   PatchLocation(DIE::value_iterator I) : I(I) {}
181
182   void set(uint64_t New) const {
183     assert(I);
184     const auto &Old = *I;
185     assert(Old.getType() == DIEValue::isInteger);
186     *I = DIEValue(Old.getAttribute(), Old.getForm(), DIEInteger(New));
187   }
188
189   uint64_t get() const {
190     assert(I);
191     return I->getDIEInteger().getValue();
192   }
193 };
194
195 class CompileUnit;
196 struct DeclMapInfo;
197
198 /// A DeclContext is a named program scope that is used for ODR
199 /// uniquing of types.
200 /// The set of DeclContext for the ODR-subject parts of a Dwarf link
201 /// is expanded (and uniqued) with each new object file processed. We
202 /// need to determine the context of each DIE in an linked object file
203 /// to see if the corresponding type has already been emitted.
204 ///
205 /// The contexts are conceptually organized as a tree (eg. a function
206 /// scope is contained in a namespace scope that contains other
207 /// scopes), but storing/accessing them in an actual tree is too
208 /// inefficient: we need to be able to very quickly query a context
209 /// for a given child context by name. Storing a StringMap in each
210 /// DeclContext would be too space inefficient.
211 /// The solution here is to give each DeclContext a link to its parent
212 /// (this allows to walk up the tree), but to query the existence of a
213 /// specific DeclContext using a separate DenseMap keyed on the hash
214 /// of the fully qualified name of the context.
215 class DeclContext {
216   friend DeclMapInfo;
217
218   unsigned QualifiedNameHash = 0;
219   uint32_t Line = 0;
220   uint32_t ByteSize = 0;
221   uint16_t Tag = dwarf::DW_TAG_compile_unit;
222   unsigned DefinedInClangModule : 1;
223   StringRef Name;
224   StringRef File;
225   const DeclContext &Parent;
226   DWARFDie LastSeenDIE;
227   uint32_t LastSeenCompileUnitID = 0;
228   uint32_t CanonicalDIEOffset = 0;
229
230 public:
231   using Map = DenseSet<DeclContext *, DeclMapInfo>;
232
233   DeclContext() : DefinedInClangModule(0), Parent(*this) {}
234
235   DeclContext(unsigned Hash, uint32_t Line, uint32_t ByteSize, uint16_t Tag,
236               StringRef Name, StringRef File, const DeclContext &Parent,
237               DWARFDie LastSeenDIE = DWARFDie(), unsigned CUId = 0)
238       : QualifiedNameHash(Hash), Line(Line), ByteSize(ByteSize), Tag(Tag),
239         DefinedInClangModule(0), Name(Name), File(File), Parent(Parent),
240         LastSeenDIE(LastSeenDIE), LastSeenCompileUnitID(CUId) {}
241
242   uint32_t getQualifiedNameHash() const { return QualifiedNameHash; }
243
244   bool setLastSeenDIE(CompileUnit &U, const DWARFDie &Die);
245
246   uint32_t getCanonicalDIEOffset() const { return CanonicalDIEOffset; }
247   void setCanonicalDIEOffset(uint32_t Offset) { CanonicalDIEOffset = Offset; }
248
249   bool isDefinedInClangModule() const { return DefinedInClangModule; }
250   void setDefinedInClangModule(bool Val) { DefinedInClangModule = Val; }
251
252   uint16_t getTag() const { return Tag; }
253   StringRef getName() const { return Name; }
254 };
255
256 /// Info type for the DenseMap storing the DeclContext pointers.
257 struct DeclMapInfo : private DenseMapInfo<DeclContext *> {
258   using DenseMapInfo<DeclContext *>::getEmptyKey;
259   using DenseMapInfo<DeclContext *>::getTombstoneKey;
260
261   static unsigned getHashValue(const DeclContext *Ctxt) {
262     return Ctxt->QualifiedNameHash;
263   }
264
265   static bool isEqual(const DeclContext *LHS, const DeclContext *RHS) {
266     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
267       return RHS == LHS;
268     return LHS->QualifiedNameHash == RHS->QualifiedNameHash &&
269            LHS->Line == RHS->Line && LHS->ByteSize == RHS->ByteSize &&
270            LHS->Name.data() == RHS->Name.data() &&
271            LHS->File.data() == RHS->File.data() &&
272            LHS->Parent.QualifiedNameHash == RHS->Parent.QualifiedNameHash;
273   }
274 };
275
276 /// This class gives a tree-like API to the DenseMap that stores the
277 /// DeclContext objects. It also holds the BumpPtrAllocator where
278 /// these objects will be allocated.
279 class DeclContextTree {
280   BumpPtrAllocator Allocator;
281   DeclContext Root;
282   DeclContext::Map Contexts;
283
284   /// Cache resolved paths from the line table.
285   CachedPathResolver PathResolver;
286
287 public:
288   /// Get the child of \a Context described by \a DIE in \a Unit. The
289   /// required strings will be interned in \a StringPool.
290   /// \returns The child DeclContext along with one bit that is set if
291   /// this context is invalid.
292   ///
293   /// An invalid context means it shouldn't be considered for uniquing, but its
294   /// not returning null, because some children of that context might be
295   /// uniquing candidates.
296   ///
297   /// FIXME: The invalid bit along the return value is to emulate some
298   /// dsymutil-classic functionality.
299   PointerIntPair<DeclContext *, 1>
300   getChildDeclContext(DeclContext &Context, const DWARFDie &DIE,
301                       CompileUnit &Unit, UniquingStringPool &StringPool,
302                       bool InClangModule);
303
304   DeclContext &getRoot() { return Root; }
305 };
306
307 /// Stores all information relating to a compile unit, be it in its original
308 /// instance in the object file to its brand new cloned and linked DIE tree.
309 class CompileUnit {
310 public:
311   /// Information gathered about a DIE in the object file.
312   struct DIEInfo {
313     /// Address offset to apply to the described entity.
314     int64_t AddrAdjust;
315
316     /// ODR Declaration context.
317     DeclContext *Ctxt;
318
319     /// Cloned version of that DIE.
320     DIE *Clone;
321
322     /// The index of this DIE's parent.
323     uint32_t ParentIdx;
324
325     /// Is the DIE part of the linked output?
326     bool Keep : 1;
327
328     /// Was this DIE's entity found in the map?
329     bool InDebugMap : 1;
330
331     /// Is this a pure forward declaration we can strip?
332     bool Prune : 1;
333
334     /// Does DIE transitively refer an incomplete decl?
335     bool Incomplete : 1;
336   };
337
338   CompileUnit(DWARFUnit &OrigUnit, unsigned ID, bool CanUseODR,
339               StringRef ClangModuleName)
340       : OrigUnit(OrigUnit), ID(ID), Ranges(RangeAlloc),
341         ClangModuleName(ClangModuleName) {
342     Info.resize(OrigUnit.getNumDIEs());
343
344     auto CUDie = OrigUnit.getUnitDIE(false);
345     if (!CUDie) {
346       HasODR = false;
347       return;
348     }
349     if (auto Lang = dwarf::toUnsigned(CUDie.find(dwarf::DW_AT_language)))
350       HasODR = CanUseODR && (*Lang == dwarf::DW_LANG_C_plus_plus ||
351                              *Lang == dwarf::DW_LANG_C_plus_plus_03 ||
352                              *Lang == dwarf::DW_LANG_C_plus_plus_11 ||
353                              *Lang == dwarf::DW_LANG_C_plus_plus_14 ||
354                              *Lang == dwarf::DW_LANG_ObjC_plus_plus);
355     else
356       HasODR = false;
357   }
358
359   DWARFUnit &getOrigUnit() const { return OrigUnit; }
360
361   unsigned getUniqueID() const { return ID; }
362
363   void createOutputDIE() {
364     NewUnit.emplace(OrigUnit.getVersion(), OrigUnit.getAddressByteSize(),
365                     OrigUnit.getUnitDIE().getTag());
366   }
367
368   DIE *getOutputUnitDIE() const {
369     if (NewUnit)
370       return &const_cast<BasicDIEUnit &>(*NewUnit).getUnitDie();
371     return nullptr;
372   }
373
374   bool hasODR() const { return HasODR; }
375   bool isClangModule() const { return !ClangModuleName.empty(); }
376   const std::string &getClangModuleName() const { return ClangModuleName; }
377
378   DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; }
379   const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; }
380
381   uint64_t getStartOffset() const { return StartOffset; }
382   uint64_t getNextUnitOffset() const { return NextUnitOffset; }
383   void setStartOffset(uint64_t DebugInfoSize) { StartOffset = DebugInfoSize; }
384
385   uint64_t getLowPc() const { return LowPc; }
386   uint64_t getHighPc() const { return HighPc; }
387   bool hasLabelAt(uint64_t Addr) const { return Labels.count(Addr); }
388
389   Optional<PatchLocation> getUnitRangesAttribute() const {
390     return UnitRangeAttribute;
391   }
392
393   const FunctionIntervals &getFunctionRanges() const { return Ranges; }
394
395   const std::vector<PatchLocation> &getRangesAttributes() const {
396     return RangeAttributes;
397   }
398
399   const std::vector<std::pair<PatchLocation, int64_t>> &
400   getLocationAttributes() const {
401     return LocationAttributes;
402   }
403
404   void setHasInterestingContent() { HasInterestingContent = true; }
405   bool hasInterestingContent() { return HasInterestingContent; }
406
407   /// Mark every DIE in this unit as kept. This function also
408   /// marks variables as InDebugMap so that they appear in the
409   /// reconstructed accelerator tables.
410   void markEverythingAsKept();
411
412   /// Compute the end offset for this unit. Must be called after the CU's DIEs
413   /// have been cloned.  \returns the next unit offset (which is also the
414   /// current debug_info section size).
415   uint64_t computeNextUnitOffset();
416
417   /// Keep track of a forward reference to DIE \p Die in \p RefUnit by \p
418   /// Attr. The attribute should be fixed up later to point to the absolute
419   /// offset of \p Die in the debug_info section or to the canonical offset of
420   /// \p Ctxt if it is non-null.
421   void noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
422                             DeclContext *Ctxt, PatchLocation Attr);
423
424   /// Apply all fixups recorded by noteForwardReference().
425   void fixupForwardReferences();
426
427   /// Add the low_pc of a label that is relocated by applying
428   /// offset \p PCOffset.
429   void addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset);
430
431   /// Add a function range [\p LowPC, \p HighPC) that is relocated by applying
432   /// offset \p PCOffset.
433   void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset);
434
435   /// Keep track of a DW_AT_range attribute that we will need to patch up later.
436   void noteRangeAttribute(const DIE &Die, PatchLocation Attr);
437
438   /// Keep track of a location attribute pointing to a location list in the
439   /// debug_loc section.
440   void noteLocationAttribute(PatchLocation Attr, int64_t PcOffset);
441
442   /// Add a name accelerator entry for \a Die with \a Name.
443   void addNamespaceAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name);
444
445   /// Add a name accelerator entry for \a Die with \a Name.
446   void addNameAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
447                           bool SkipPubnamesSection = false);
448
449   /// Add various accelerator entries for \p Die with \p Name which is stored
450   /// in the string table at \p Offset. \p Name must be an Objective-C
451   /// selector.
452   void addObjCAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
453                           bool SkipPubnamesSection = false);
454
455   /// Add a type accelerator entry for \p Die with \p Name which is stored in
456   /// the string table at \p Offset.
457   void addTypeAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
458                           bool ObjcClassImplementation,
459                           uint32_t QualifiedNameHash);
460
461   struct AccelInfo {
462     /// Name of the entry.
463     DwarfStringPoolEntryRef Name;
464
465     /// DIE this entry describes.
466     const DIE *Die;
467
468     /// Hash of the fully qualified name.
469     uint32_t QualifiedNameHash;
470
471     /// Emit this entry only in the apple_* sections.
472     bool SkipPubSection;
473
474     /// Is this an ObjC class implementation?
475     bool ObjcClassImplementation;
476
477     AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die,
478               bool SkipPubSection = false)
479         : Name(Name), Die(Die), SkipPubSection(SkipPubSection) {}
480
481     AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die,
482               uint32_t QualifiedNameHash, bool ObjCClassIsImplementation)
483         : Name(Name), Die(Die), QualifiedNameHash(QualifiedNameHash),
484           SkipPubSection(false),
485           ObjcClassImplementation(ObjCClassIsImplementation) {}
486   };
487
488   const std::vector<AccelInfo> &getPubnames() const { return Pubnames; }
489   const std::vector<AccelInfo> &getPubtypes() const { return Pubtypes; }
490   const std::vector<AccelInfo> &getNamespaces() const { return Namespaces; }
491   const std::vector<AccelInfo> &getObjC() const { return ObjC; }
492
493   /// Get the full path for file \a FileNum in the line table
494   StringRef getResolvedPath(unsigned FileNum) {
495     if (FileNum >= ResolvedPaths.size())
496       return StringRef();
497     return ResolvedPaths[FileNum];
498   }
499
500   /// Set the fully resolved path for the line-table's file \a FileNum
501   /// to \a Path.
502   void setResolvedPath(unsigned FileNum, StringRef Path) {
503     if (ResolvedPaths.size() <= FileNum)
504       ResolvedPaths.resize(FileNum + 1);
505     ResolvedPaths[FileNum] = Path;
506   }
507
508 private:
509   DWARFUnit &OrigUnit;
510   unsigned ID;
511   std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index.
512   Optional<BasicDIEUnit> NewUnit;
513
514   uint64_t StartOffset;
515   uint64_t NextUnitOffset;
516
517   uint64_t LowPc = std::numeric_limits<uint64_t>::max();
518   uint64_t HighPc = 0;
519
520   /// A list of attributes to fixup with the absolute offset of
521   /// a DIE in the debug_info section.
522   ///
523   /// The offsets for the attributes in this array couldn't be set while
524   /// cloning because for cross-cu forward references the target DIE's offset
525   /// isn't known you emit the reference attribute.
526   std::vector<
527       std::tuple<DIE *, const CompileUnit *, DeclContext *, PatchLocation>>
528       ForwardDIEReferences;
529
530   FunctionIntervals::Allocator RangeAlloc;
531
532   /// The ranges in that interval map are the PC ranges for
533   /// functions in this unit, associated with the PC offset to apply
534   /// to the addresses to get the linked address.
535   FunctionIntervals Ranges;
536
537   /// The DW_AT_low_pc of each DW_TAG_label.
538   SmallDenseMap<uint64_t, uint64_t, 1> Labels;
539
540   /// DW_AT_ranges attributes to patch after we have gathered
541   /// all the unit's function addresses.
542   /// @{
543   std::vector<PatchLocation> RangeAttributes;
544   Optional<PatchLocation> UnitRangeAttribute;
545   /// @}
546
547   /// Location attributes that need to be transferred from the
548   /// original debug_loc section to the liked one. They are stored
549   /// along with the PC offset that is to be applied to their
550   /// function's address.
551   std::vector<std::pair<PatchLocation, int64_t>> LocationAttributes;
552
553   /// Accelerator entries for the unit, both for the pub*
554   /// sections and the apple* ones.
555   /// @{
556   std::vector<AccelInfo> Pubnames;
557   std::vector<AccelInfo> Pubtypes;
558   std::vector<AccelInfo> Namespaces;
559   std::vector<AccelInfo> ObjC;
560   /// @}
561
562   /// Cached resolved paths from the line table.
563   /// Note, the StringRefs here point in to the intern (uniquing) string pool.
564   /// This means that a StringRef returned here doesn't need to then be uniqued
565   /// for the purposes of getting a unique address for each string.
566   std::vector<StringRef> ResolvedPaths;
567
568   /// Is this unit subject to the ODR rule?
569   bool HasODR;
570
571   /// Did a DIE actually contain a valid reloc?
572   bool HasInterestingContent;
573
574   /// If this is a Clang module, this holds the module's name.
575   std::string ClangModuleName;
576 };
577
578 /// Check if the DIE at \p Idx is in the scope of a function.
579 static bool inFunctionScope(CompileUnit &U, unsigned Idx) {
580   while (Idx) {
581     if (U.getOrigUnit().getDIEAtIndex(Idx).getTag() == dwarf::DW_TAG_subprogram)
582       return true;
583     Idx = U.getInfo(Idx).ParentIdx;
584   }
585   return false;
586 }
587 } // namespace
588
589 void warn(Twine Warning, Twine Context) {
590   warn_ostream() << Warning + "\n";
591   if (!Context.isTriviallyEmpty())
592     note_ostream() << Twine("while processing ") + Context + "\n";
593 }
594
595 bool error(Twine Error, Twine Context) {
596   error_ostream() << Error + "\n";
597   if (!Context.isTriviallyEmpty())
598     note_ostream() << Twine("while processing ") + Context + "\n";
599   return false;
600 }
601
602 void CompileUnit::markEverythingAsKept() {
603   unsigned Idx = 0;
604
605   setHasInterestingContent();
606
607   for (auto &I : Info) {
608     // Mark everything that wasn't explicit marked for pruning.
609     I.Keep = !I.Prune;
610     auto DIE = OrigUnit.getDIEAtIndex(Idx++);
611
612     // Try to guess which DIEs must go to the accelerator tables. We do that
613     // just for variables, because functions will be handled depending on
614     // whether they carry a DW_AT_low_pc attribute or not.
615     if (DIE.getTag() != dwarf::DW_TAG_variable &&
616         DIE.getTag() != dwarf::DW_TAG_constant)
617       continue;
618
619     Optional<DWARFFormValue> Value;
620     if (!(Value = DIE.find(dwarf::DW_AT_location))) {
621       if ((Value = DIE.find(dwarf::DW_AT_const_value)) &&
622           !inFunctionScope(*this, I.ParentIdx))
623         I.InDebugMap = true;
624       continue;
625     }
626     if (auto Block = Value->getAsBlock()) {
627       if (Block->size() > OrigUnit.getAddressByteSize() &&
628           (*Block)[0] == dwarf::DW_OP_addr)
629         I.InDebugMap = true;
630     }
631   }
632 }
633
634 uint64_t CompileUnit::computeNextUnitOffset() {
635   NextUnitOffset = StartOffset + 11 /* Header size */;
636   // The root DIE might be null, meaning that the Unit had nothing to
637   // contribute to the linked output. In that case, we will emit the
638   // unit header without any actual DIE.
639   if (NewUnit)
640     NextUnitOffset += NewUnit->getUnitDie().getSize();
641   return NextUnitOffset;
642 }
643
644 /// Keep track of a forward cross-cu reference from this unit
645 /// to \p Die that lives in \p RefUnit.
646 void CompileUnit::noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
647                                        DeclContext *Ctxt, PatchLocation Attr) {
648   ForwardDIEReferences.emplace_back(Die, RefUnit, Ctxt, Attr);
649 }
650
651 void CompileUnit::fixupForwardReferences() {
652   for (const auto &Ref : ForwardDIEReferences) {
653     DIE *RefDie;
654     const CompileUnit *RefUnit;
655     PatchLocation Attr;
656     DeclContext *Ctxt;
657     std::tie(RefDie, RefUnit, Ctxt, Attr) = Ref;
658     if (Ctxt && Ctxt->getCanonicalDIEOffset())
659       Attr.set(Ctxt->getCanonicalDIEOffset());
660     else
661       Attr.set(RefDie->getOffset() + RefUnit->getStartOffset());
662   }
663 }
664
665 void CompileUnit::addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset) {
666   Labels.insert({LabelLowPc, PcOffset});
667 }
668
669 void CompileUnit::addFunctionRange(uint64_t FuncLowPc, uint64_t FuncHighPc,
670                                    int64_t PcOffset) {
671   Ranges.insert(FuncLowPc, FuncHighPc, PcOffset);
672   this->LowPc = std::min(LowPc, FuncLowPc + PcOffset);
673   this->HighPc = std::max(HighPc, FuncHighPc + PcOffset);
674 }
675
676 void CompileUnit::noteRangeAttribute(const DIE &Die, PatchLocation Attr) {
677   if (Die.getTag() != dwarf::DW_TAG_compile_unit)
678     RangeAttributes.push_back(Attr);
679   else
680     UnitRangeAttribute = Attr;
681 }
682
683 void CompileUnit::noteLocationAttribute(PatchLocation Attr, int64_t PcOffset) {
684   LocationAttributes.emplace_back(Attr, PcOffset);
685 }
686
687 void CompileUnit::addNamespaceAccelerator(const DIE *Die,
688                                           DwarfStringPoolEntryRef Name) {
689   Namespaces.emplace_back(Name, Die);
690 }
691
692 void CompileUnit::addObjCAccelerator(const DIE *Die,
693                                      DwarfStringPoolEntryRef Name,
694                                      bool SkipPubSection) {
695   ObjC.emplace_back(Name, Die, SkipPubSection);
696 }
697
698 void CompileUnit::addNameAccelerator(const DIE *Die,
699                                      DwarfStringPoolEntryRef Name,
700                                      bool SkipPubSection) {
701   Pubnames.emplace_back(Name, Die, SkipPubSection);
702 }
703
704 void CompileUnit::addTypeAccelerator(const DIE *Die,
705                                      DwarfStringPoolEntryRef Name,
706                                      bool ObjcClassImplementation,
707                                      uint32_t QualifiedNameHash) {
708   Pubtypes.emplace_back(Name, Die, QualifiedNameHash, ObjcClassImplementation);
709 }
710
711 namespace {
712
713 /// The Dwarf streaming logic
714 ///
715 /// All interactions with the MC layer that is used to build the debug
716 /// information binary representation are handled in this class.
717 class DwarfStreamer {
718   /// \defgroup MCObjects MC layer objects constructed by the streamer
719   /// @{
720   std::unique_ptr<MCRegisterInfo> MRI;
721   std::unique_ptr<MCAsmInfo> MAI;
722   std::unique_ptr<MCObjectFileInfo> MOFI;
723   std::unique_ptr<MCContext> MC;
724   MCAsmBackend *MAB; // Owned by MCStreamer
725   std::unique_ptr<MCInstrInfo> MII;
726   std::unique_ptr<MCSubtargetInfo> MSTI;
727   MCCodeEmitter *MCE; // Owned by MCStreamer
728   MCStreamer *MS;     // Owned by AsmPrinter
729   std::unique_ptr<TargetMachine> TM;
730   std::unique_ptr<AsmPrinter> Asm;
731   /// @}
732
733   /// The file we stream the linked Dwarf to.
734   raw_fd_ostream &OutFile;
735
736   uint32_t RangesSectionSize;
737   uint32_t LocSectionSize;
738   uint32_t LineSectionSize;
739   uint32_t FrameSectionSize;
740
741   /// Emit the pubnames or pubtypes section contribution for \p
742   /// Unit into \p Sec. The data is provided in \p Names.
743   void emitPubSectionForUnit(MCSection *Sec, StringRef Name,
744                              const CompileUnit &Unit,
745                              const std::vector<CompileUnit::AccelInfo> &Names);
746
747 public:
748   DwarfStreamer(raw_fd_ostream &OutFile) : OutFile(OutFile) {}
749   bool init(Triple TheTriple);
750
751   /// Dump the file to the disk.
752   bool finish(const DebugMap &);
753
754   AsmPrinter &getAsmPrinter() const { return *Asm; }
755
756   /// Set the current output section to debug_info and change
757   /// the MC Dwarf version to \p DwarfVersion.
758   void switchToDebugInfoSection(unsigned DwarfVersion);
759
760   /// Emit the compilation unit header for \p Unit in the
761   /// debug_info section.
762   ///
763   /// As a side effect, this also switches the current Dwarf version
764   /// of the MC layer to the one of U.getOrigUnit().
765   void emitCompileUnitHeader(CompileUnit &Unit);
766
767   /// Recursively emit the DIE tree rooted at \p Die.
768   void emitDIE(DIE &Die);
769
770   /// Emit the abbreviation table \p Abbrevs to the debug_abbrev section.
771   void emitAbbrevs(const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs,
772                    unsigned DwarfVersion);
773
774   /// Emit the string table described by \p Pool.
775   void emitStrings(const NonRelocatableStringpool &Pool);
776
777   /// Emit the swift_ast section stored in \p Buffer.
778   void emitSwiftAST(StringRef Buffer);
779
780   /// Emit debug_ranges for \p FuncRange by translating the
781   /// original \p Entries.
782   void emitRangesEntries(
783       int64_t UnitPcOffset, uint64_t OrigLowPc,
784       const FunctionIntervals::const_iterator &FuncRange,
785       const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
786       unsigned AddressSize);
787
788   /// Emit debug_aranges entries for \p Unit and if \p DoRangesSection is true,
789   /// also emit the debug_ranges entries for the DW_TAG_compile_unit's
790   /// DW_AT_ranges attribute.
791   void emitUnitRangesEntries(CompileUnit &Unit, bool DoRangesSection);
792
793   uint32_t getRangesSectionSize() const { return RangesSectionSize; }
794
795   /// Emit the debug_loc contribution for \p Unit by copying the entries from
796   /// \p Dwarf and offsetting them. Update the location attributes to point to
797   /// the new entries.
798   void emitLocationsForUnit(const CompileUnit &Unit, DWARFContext &Dwarf);
799
800   /// Emit the line table described in \p Rows into the debug_line section.
801   void emitLineTableForUnit(MCDwarfLineTableParams Params,
802                             StringRef PrologueBytes, unsigned MinInstLength,
803                             std::vector<DWARFDebugLine::Row> &Rows,
804                             unsigned AdddressSize);
805
806   /// Copy over the debug sections that are not modified when updating.
807   void copyInvariantDebugSection(const object::ObjectFile &Obj, LinkOptions &);
808
809   uint32_t getLineSectionSize() const { return LineSectionSize; }
810
811   /// Emit the .debug_pubnames contribution for \p Unit.
812   void emitPubNamesForUnit(const CompileUnit &Unit);
813
814   /// Emit the .debug_pubtypes contribution for \p Unit.
815   void emitPubTypesForUnit(const CompileUnit &Unit);
816
817   /// Emit a CIE.
818   void emitCIE(StringRef CIEBytes);
819
820   /// Emit an FDE with data \p Bytes.
821   void emitFDE(uint32_t CIEOffset, uint32_t AddreSize, uint32_t Address,
822                StringRef Bytes);
823
824   /// Emit Apple namespaces accelerator table.
825   void emitAppleNamespaces(AccelTable<AppleAccelTableStaticOffsetData> &Table);
826
827   /// Emit Apple names accelerator table.
828   void emitAppleNames(AccelTable<AppleAccelTableStaticOffsetData> &Table);
829
830   /// Emit Apple Objective-C accelerator table.
831   void emitAppleObjc(AccelTable<AppleAccelTableStaticOffsetData> &Table);
832
833   /// Emit Apple type accelerator table.
834   void emitAppleTypes(AccelTable<AppleAccelTableStaticTypeData> &Table);
835
836   uint32_t getFrameSectionSize() const { return FrameSectionSize; }
837 };
838
839 } // end anonymous namespace
840
841 bool DwarfStreamer::init(Triple TheTriple) {
842   std::string ErrorStr;
843   std::string TripleName;
844   StringRef Context = "dwarf streamer init";
845
846   // Get the target.
847   const Target *TheTarget =
848       TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr);
849   if (!TheTarget)
850     return error(ErrorStr, Context);
851   TripleName = TheTriple.getTriple();
852
853   // Create all the MC Objects.
854   MRI.reset(TheTarget->createMCRegInfo(TripleName));
855   if (!MRI)
856     return error(Twine("no register info for target ") + TripleName, Context);
857
858   MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName));
859   if (!MAI)
860     return error("no asm info for target " + TripleName, Context);
861
862   MOFI.reset(new MCObjectFileInfo);
863   MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get()));
864   MOFI->InitMCObjectFileInfo(TheTriple, /*PIC*/ false, *MC);
865
866   MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
867   if (!MSTI)
868     return error("no subtarget info for target " + TripleName, Context);
869
870   MCTargetOptions Options;
871   MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, Options);
872   if (!MAB)
873     return error("no asm backend for target " + TripleName, Context);
874
875   MII.reset(TheTarget->createMCInstrInfo());
876   if (!MII)
877     return error("no instr info info for target " + TripleName, Context);
878
879   MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MC);
880   if (!MCE)
881     return error("no code emitter for target " + TripleName, Context);
882
883   MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
884   MS = TheTarget->createMCObjectStreamer(
885       TheTriple, *MC, std::unique_ptr<MCAsmBackend>(MAB), OutFile,
886       std::unique_ptr<MCCodeEmitter>(MCE), *MSTI, MCOptions.MCRelaxAll,
887       MCOptions.MCIncrementalLinkerCompatible,
888       /*DWARFMustBeAtTheEnd*/ false);
889   if (!MS)
890     return error("no object streamer for target " + TripleName, Context);
891
892   // Finally create the AsmPrinter we'll use to emit the DIEs.
893   TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions(),
894                                           None));
895   if (!TM)
896     return error("no target machine for target " + TripleName, Context);
897
898   Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS)));
899   if (!Asm)
900     return error("no asm printer for target " + TripleName, Context);
901
902   RangesSectionSize = 0;
903   LocSectionSize = 0;
904   LineSectionSize = 0;
905   FrameSectionSize = 0;
906
907   return true;
908 }
909
910 bool DwarfStreamer::finish(const DebugMap &DM) {
911   bool Result = true;
912   if (DM.getTriple().isOSDarwin() && !DM.getBinaryPath().empty())
913     Result = MachOUtils::generateDsymCompanion(DM, *MS, OutFile);
914   else
915     MS->Finish();
916   return Result;
917 }
918
919 void DwarfStreamer::switchToDebugInfoSection(unsigned DwarfVersion) {
920   MS->SwitchSection(MOFI->getDwarfInfoSection());
921   MC->setDwarfVersion(DwarfVersion);
922 }
923
924 /// Emit the compilation unit header for \p Unit in the debug_info section.
925 ///
926 /// A Dwarf section header is encoded as:
927 ///  uint32_t   Unit length (omitting this field)
928 ///  uint16_t   Version
929 ///  uint32_t   Abbreviation table offset
930 ///  uint8_t    Address size
931 ///
932 /// Leading to a total of 11 bytes.
933 void DwarfStreamer::emitCompileUnitHeader(CompileUnit &Unit) {
934   unsigned Version = Unit.getOrigUnit().getVersion();
935   switchToDebugInfoSection(Version);
936
937   // Emit size of content not including length itself. The size has already
938   // been computed in CompileUnit::computeOffsets(). Subtract 4 to that size to
939   // account for the length field.
940   Asm->emitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset() - 4);
941   Asm->emitInt16(Version);
942   // We share one abbreviations table across all units so it's always at the
943   // start of the section.
944   Asm->emitInt32(0);
945   Asm->emitInt8(Unit.getOrigUnit().getAddressByteSize());
946 }
947
948 /// Emit the \p Abbrevs array as the shared abbreviation table
949 /// for the linked Dwarf file.
950 void DwarfStreamer::emitAbbrevs(
951     const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs,
952     unsigned DwarfVersion) {
953   MS->SwitchSection(MOFI->getDwarfAbbrevSection());
954   MC->setDwarfVersion(DwarfVersion);
955   Asm->emitDwarfAbbrevs(Abbrevs);
956 }
957
958 /// Recursively emit the DIE tree rooted at \p Die.
959 void DwarfStreamer::emitDIE(DIE &Die) {
960   MS->SwitchSection(MOFI->getDwarfInfoSection());
961   Asm->emitDwarfDIE(Die);
962 }
963
964 /// Emit the debug_str section stored in \p Pool.
965 void DwarfStreamer::emitStrings(const NonRelocatableStringpool &Pool) {
966   Asm->OutStreamer->SwitchSection(MOFI->getDwarfStrSection());
967   std::vector<DwarfStringPoolEntryRef> Entries = Pool.getEntries();
968   for (auto Entry : Entries) {
969     if (Entry.getIndex() == -1U)
970       break;
971     // Emit the string itself.
972     Asm->OutStreamer->EmitBytes(Entry.getString());
973     // Emit a null terminator.
974     Asm->emitInt8(0);
975   }
976 }
977
978 void DwarfStreamer::emitAppleNamespaces(
979     AccelTable<AppleAccelTableStaticOffsetData> &Table) {
980   Asm->OutStreamer->SwitchSection(MOFI->getDwarfAccelNamespaceSection());
981   auto *SectionBegin = Asm->createTempSymbol("namespac_begin");
982   Asm->OutStreamer->EmitLabel(SectionBegin);
983   emitAppleAccelTable(Asm.get(), Table, "namespac", SectionBegin);
984 }
985
986 void DwarfStreamer::emitAppleNames(
987     AccelTable<AppleAccelTableStaticOffsetData> &Table) {
988   Asm->OutStreamer->SwitchSection(MOFI->getDwarfAccelNamesSection());
989   auto *SectionBegin = Asm->createTempSymbol("names_begin");
990   Asm->OutStreamer->EmitLabel(SectionBegin);
991   emitAppleAccelTable(Asm.get(), Table, "names", SectionBegin);
992 }
993
994 void DwarfStreamer::emitAppleObjc(
995     AccelTable<AppleAccelTableStaticOffsetData> &Table) {
996   Asm->OutStreamer->SwitchSection(MOFI->getDwarfAccelObjCSection());
997   auto *SectionBegin = Asm->createTempSymbol("objc_begin");
998   Asm->OutStreamer->EmitLabel(SectionBegin);
999   emitAppleAccelTable(Asm.get(), Table, "objc", SectionBegin);
1000 }
1001
1002 void DwarfStreamer::emitAppleTypes(
1003     AccelTable<AppleAccelTableStaticTypeData> &Table) {
1004   Asm->OutStreamer->SwitchSection(MOFI->getDwarfAccelTypesSection());
1005   auto *SectionBegin = Asm->createTempSymbol("types_begin");
1006   Asm->OutStreamer->EmitLabel(SectionBegin);
1007   emitAppleAccelTable(Asm.get(), Table, "types", SectionBegin);
1008 }
1009
1010 /// Emit the swift_ast section stored in \p Buffers.
1011 void DwarfStreamer::emitSwiftAST(StringRef Buffer) {
1012   MCSection *SwiftASTSection = MOFI->getDwarfSwiftASTSection();
1013   SwiftASTSection->setAlignment(1 << 5);
1014   MS->SwitchSection(SwiftASTSection);
1015   MS->EmitBytes(Buffer);
1016 }
1017
1018 /// Emit the debug_range section contents for \p FuncRange by
1019 /// translating the original \p Entries. The debug_range section
1020 /// format is totally trivial, consisting just of pairs of address
1021 /// sized addresses describing the ranges.
1022 void DwarfStreamer::emitRangesEntries(
1023     int64_t UnitPcOffset, uint64_t OrigLowPc,
1024     const FunctionIntervals::const_iterator &FuncRange,
1025     const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
1026     unsigned AddressSize) {
1027   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
1028
1029   // Offset each range by the right amount.
1030   int64_t PcOffset = Entries.empty() ? 0 : FuncRange.value() + UnitPcOffset;
1031   for (const auto &Range : Entries) {
1032     if (Range.isBaseAddressSelectionEntry(AddressSize)) {
1033       warn("unsupported base address selection operation",
1034            "emitting debug_ranges");
1035       break;
1036     }
1037     // Do not emit empty ranges.
1038     if (Range.StartAddress == Range.EndAddress)
1039       continue;
1040
1041     // All range entries should lie in the function range.
1042     if (!(Range.StartAddress + OrigLowPc >= FuncRange.start() &&
1043           Range.EndAddress + OrigLowPc <= FuncRange.stop()))
1044       warn("inconsistent range data.", "emitting debug_ranges");
1045     MS->EmitIntValue(Range.StartAddress + PcOffset, AddressSize);
1046     MS->EmitIntValue(Range.EndAddress + PcOffset, AddressSize);
1047     RangesSectionSize += 2 * AddressSize;
1048   }
1049
1050   // Add the terminator entry.
1051   MS->EmitIntValue(0, AddressSize);
1052   MS->EmitIntValue(0, AddressSize);
1053   RangesSectionSize += 2 * AddressSize;
1054 }
1055
1056 /// Emit the debug_aranges contribution of a unit and
1057 /// if \p DoDebugRanges is true the debug_range contents for a
1058 /// compile_unit level DW_AT_ranges attribute (Which are basically the
1059 /// same thing with a different base address).
1060 /// Just aggregate all the ranges gathered inside that unit.
1061 void DwarfStreamer::emitUnitRangesEntries(CompileUnit &Unit,
1062                                           bool DoDebugRanges) {
1063   unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
1064   // Gather the ranges in a vector, so that we can simplify them. The
1065   // IntervalMap will have coalesced the non-linked ranges, but here
1066   // we want to coalesce the linked addresses.
1067   std::vector<std::pair<uint64_t, uint64_t>> Ranges;
1068   const auto &FunctionRanges = Unit.getFunctionRanges();
1069   for (auto Range = FunctionRanges.begin(), End = FunctionRanges.end();
1070        Range != End; ++Range)
1071     Ranges.push_back(std::make_pair(Range.start() + Range.value(),
1072                                     Range.stop() + Range.value()));
1073
1074   // The object addresses where sorted, but again, the linked
1075   // addresses might end up in a different order.
1076   llvm::sort(Ranges.begin(), Ranges.end());
1077
1078   if (!Ranges.empty()) {
1079     MS->SwitchSection(MC->getObjectFileInfo()->getDwarfARangesSection());
1080
1081     MCSymbol *BeginLabel = Asm->createTempSymbol("Barange");
1082     MCSymbol *EndLabel = Asm->createTempSymbol("Earange");
1083
1084     unsigned HeaderSize =
1085         sizeof(int32_t) + // Size of contents (w/o this field
1086         sizeof(int16_t) + // DWARF ARange version number
1087         sizeof(int32_t) + // Offset of CU in the .debug_info section
1088         sizeof(int8_t) +  // Pointer Size (in bytes)
1089         sizeof(int8_t);   // Segment Size (in bytes)
1090
1091     unsigned TupleSize = AddressSize * 2;
1092     unsigned Padding = OffsetToAlignment(HeaderSize, TupleSize);
1093
1094     Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); // Arange length
1095     Asm->OutStreamer->EmitLabel(BeginLabel);
1096     Asm->emitInt16(dwarf::DW_ARANGES_VERSION); // Version number
1097     Asm->emitInt32(Unit.getStartOffset());     // Corresponding unit's offset
1098     Asm->emitInt8(AddressSize);                // Address size
1099     Asm->emitInt8(0);                          // Segment size
1100
1101     Asm->OutStreamer->emitFill(Padding, 0x0);
1102
1103     for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End;
1104          ++Range) {
1105       uint64_t RangeStart = Range->first;
1106       MS->EmitIntValue(RangeStart, AddressSize);
1107       while ((Range + 1) != End && Range->second == (Range + 1)->first)
1108         ++Range;
1109       MS->EmitIntValue(Range->second - RangeStart, AddressSize);
1110     }
1111
1112     // Emit terminator
1113     Asm->OutStreamer->EmitIntValue(0, AddressSize);
1114     Asm->OutStreamer->EmitIntValue(0, AddressSize);
1115     Asm->OutStreamer->EmitLabel(EndLabel);
1116   }
1117
1118   if (!DoDebugRanges)
1119     return;
1120
1121   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
1122   // Offset each range by the right amount.
1123   int64_t PcOffset = -Unit.getLowPc();
1124   // Emit coalesced ranges.
1125   for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End; ++Range) {
1126     MS->EmitIntValue(Range->first + PcOffset, AddressSize);
1127     while (Range + 1 != End && Range->second == (Range + 1)->first)
1128       ++Range;
1129     MS->EmitIntValue(Range->second + PcOffset, AddressSize);
1130     RangesSectionSize += 2 * AddressSize;
1131   }
1132
1133   // Add the terminator entry.
1134   MS->EmitIntValue(0, AddressSize);
1135   MS->EmitIntValue(0, AddressSize);
1136   RangesSectionSize += 2 * AddressSize;
1137 }
1138
1139 /// Emit location lists for \p Unit and update attributes to point to the new
1140 /// entries.
1141 void DwarfStreamer::emitLocationsForUnit(const CompileUnit &Unit,
1142                                          DWARFContext &Dwarf) {
1143   const auto &Attributes = Unit.getLocationAttributes();
1144
1145   if (Attributes.empty())
1146     return;
1147
1148   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLocSection());
1149
1150   unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
1151   const DWARFSection &InputSec = Dwarf.getDWARFObj().getLocSection();
1152   DataExtractor Data(InputSec.Data, Dwarf.isLittleEndian(), AddressSize);
1153   DWARFUnit &OrigUnit = Unit.getOrigUnit();
1154   auto OrigUnitDie = OrigUnit.getUnitDIE(false);
1155   int64_t UnitPcOffset = 0;
1156   if (auto OrigLowPc = dwarf::toAddress(OrigUnitDie.find(dwarf::DW_AT_low_pc)))
1157     UnitPcOffset = int64_t(*OrigLowPc) - Unit.getLowPc();
1158
1159   for (const auto &Attr : Attributes) {
1160     uint32_t Offset = Attr.first.get();
1161     Attr.first.set(LocSectionSize);
1162     // This is the quantity to add to the old location address to get
1163     // the correct address for the new one.
1164     int64_t LocPcOffset = Attr.second + UnitPcOffset;
1165     while (Data.isValidOffset(Offset)) {
1166       uint64_t Low = Data.getUnsigned(&Offset, AddressSize);
1167       uint64_t High = Data.getUnsigned(&Offset, AddressSize);
1168       LocSectionSize += 2 * AddressSize;
1169       if (Low == 0 && High == 0) {
1170         Asm->OutStreamer->EmitIntValue(0, AddressSize);
1171         Asm->OutStreamer->EmitIntValue(0, AddressSize);
1172         break;
1173       }
1174       Asm->OutStreamer->EmitIntValue(Low + LocPcOffset, AddressSize);
1175       Asm->OutStreamer->EmitIntValue(High + LocPcOffset, AddressSize);
1176       uint64_t Length = Data.getU16(&Offset);
1177       Asm->OutStreamer->EmitIntValue(Length, 2);
1178       // Just copy the bytes over.
1179       Asm->OutStreamer->EmitBytes(
1180           StringRef(InputSec.Data.substr(Offset, Length)));
1181       Offset += Length;
1182       LocSectionSize += Length + 2;
1183     }
1184   }
1185 }
1186
1187 void DwarfStreamer::emitLineTableForUnit(MCDwarfLineTableParams Params,
1188                                          StringRef PrologueBytes,
1189                                          unsigned MinInstLength,
1190                                          std::vector<DWARFDebugLine::Row> &Rows,
1191                                          unsigned PointerSize) {
1192   // Switch to the section where the table will be emitted into.
1193   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLineSection());
1194   MCSymbol *LineStartSym = MC->createTempSymbol();
1195   MCSymbol *LineEndSym = MC->createTempSymbol();
1196
1197   // The first 4 bytes is the total length of the information for this
1198   // compilation unit (not including these 4 bytes for the length).
1199   Asm->EmitLabelDifference(LineEndSym, LineStartSym, 4);
1200   Asm->OutStreamer->EmitLabel(LineStartSym);
1201   // Copy Prologue.
1202   MS->EmitBytes(PrologueBytes);
1203   LineSectionSize += PrologueBytes.size() + 4;
1204
1205   SmallString<128> EncodingBuffer;
1206   raw_svector_ostream EncodingOS(EncodingBuffer);
1207
1208   if (Rows.empty()) {
1209     // We only have the dummy entry, dsymutil emits an entry with a 0
1210     // address in that case.
1211     MCDwarfLineAddr::Encode(*MC, Params, std::numeric_limits<int64_t>::max(), 0,
1212                             EncodingOS);
1213     MS->EmitBytes(EncodingOS.str());
1214     LineSectionSize += EncodingBuffer.size();
1215     MS->EmitLabel(LineEndSym);
1216     return;
1217   }
1218
1219   // Line table state machine fields
1220   unsigned FileNum = 1;
1221   unsigned LastLine = 1;
1222   unsigned Column = 0;
1223   unsigned IsStatement = 1;
1224   unsigned Isa = 0;
1225   uint64_t Address = -1ULL;
1226
1227   unsigned RowsSinceLastSequence = 0;
1228
1229   for (unsigned Idx = 0; Idx < Rows.size(); ++Idx) {
1230     auto &Row = Rows[Idx];
1231
1232     int64_t AddressDelta;
1233     if (Address == -1ULL) {
1234       MS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
1235       MS->EmitULEB128IntValue(PointerSize + 1);
1236       MS->EmitIntValue(dwarf::DW_LNE_set_address, 1);
1237       MS->EmitIntValue(Row.Address, PointerSize);
1238       LineSectionSize += 2 + PointerSize + getULEB128Size(PointerSize + 1);
1239       AddressDelta = 0;
1240     } else {
1241       AddressDelta = (Row.Address - Address) / MinInstLength;
1242     }
1243
1244     // FIXME: code copied and transformed from MCDwarf.cpp::EmitDwarfLineTable.
1245     // We should find a way to share this code, but the current compatibility
1246     // requirement with classic dsymutil makes it hard. Revisit that once this
1247     // requirement is dropped.
1248
1249     if (FileNum != Row.File) {
1250       FileNum = Row.File;
1251       MS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
1252       MS->EmitULEB128IntValue(FileNum);
1253       LineSectionSize += 1 + getULEB128Size(FileNum);
1254     }
1255     if (Column != Row.Column) {
1256       Column = Row.Column;
1257       MS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
1258       MS->EmitULEB128IntValue(Column);
1259       LineSectionSize += 1 + getULEB128Size(Column);
1260     }
1261
1262     // FIXME: We should handle the discriminator here, but dsymutil doesn't
1263     // consider it, thus ignore it for now.
1264
1265     if (Isa != Row.Isa) {
1266       Isa = Row.Isa;
1267       MS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
1268       MS->EmitULEB128IntValue(Isa);
1269       LineSectionSize += 1 + getULEB128Size(Isa);
1270     }
1271     if (IsStatement != Row.IsStmt) {
1272       IsStatement = Row.IsStmt;
1273       MS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
1274       LineSectionSize += 1;
1275     }
1276     if (Row.BasicBlock) {
1277       MS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
1278       LineSectionSize += 1;
1279     }
1280
1281     if (Row.PrologueEnd) {
1282       MS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
1283       LineSectionSize += 1;
1284     }
1285
1286     if (Row.EpilogueBegin) {
1287       MS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
1288       LineSectionSize += 1;
1289     }
1290
1291     int64_t LineDelta = int64_t(Row.Line) - LastLine;
1292     if (!Row.EndSequence) {
1293       MCDwarfLineAddr::Encode(*MC, Params, LineDelta, AddressDelta, EncodingOS);
1294       MS->EmitBytes(EncodingOS.str());
1295       LineSectionSize += EncodingBuffer.size();
1296       EncodingBuffer.resize(0);
1297       Address = Row.Address;
1298       LastLine = Row.Line;
1299       RowsSinceLastSequence++;
1300     } else {
1301       if (LineDelta) {
1302         MS->EmitIntValue(dwarf::DW_LNS_advance_line, 1);
1303         MS->EmitSLEB128IntValue(LineDelta);
1304         LineSectionSize += 1 + getSLEB128Size(LineDelta);
1305       }
1306       if (AddressDelta) {
1307         MS->EmitIntValue(dwarf::DW_LNS_advance_pc, 1);
1308         MS->EmitULEB128IntValue(AddressDelta);
1309         LineSectionSize += 1 + getULEB128Size(AddressDelta);
1310       }
1311       MCDwarfLineAddr::Encode(*MC, Params, std::numeric_limits<int64_t>::max(),
1312                               0, EncodingOS);
1313       MS->EmitBytes(EncodingOS.str());
1314       LineSectionSize += EncodingBuffer.size();
1315       EncodingBuffer.resize(0);
1316       Address = -1ULL;
1317       LastLine = FileNum = IsStatement = 1;
1318       RowsSinceLastSequence = Column = Isa = 0;
1319     }
1320   }
1321
1322   if (RowsSinceLastSequence) {
1323     MCDwarfLineAddr::Encode(*MC, Params, std::numeric_limits<int64_t>::max(), 0,
1324                             EncodingOS);
1325     MS->EmitBytes(EncodingOS.str());
1326     LineSectionSize += EncodingBuffer.size();
1327     EncodingBuffer.resize(0);
1328   }
1329
1330   MS->EmitLabel(LineEndSym);
1331 }
1332
1333 static void emitSectionContents(const object::ObjectFile &Obj,
1334                                 StringRef SecName, MCStreamer *MS) {
1335   StringRef Contents;
1336   if (auto Sec = getSectionByName(Obj, SecName))
1337     if (!Sec->getContents(Contents))
1338       MS->EmitBytes(Contents);
1339 }
1340
1341 void DwarfStreamer::copyInvariantDebugSection(const object::ObjectFile &Obj,
1342                                               LinkOptions &Options) {
1343   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLineSection());
1344   emitSectionContents(Obj, "debug_line", MS);
1345
1346   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLocSection());
1347   emitSectionContents(Obj, "debug_loc", MS);
1348
1349   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
1350   emitSectionContents(Obj, "debug_ranges", MS);
1351
1352   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
1353   emitSectionContents(Obj, "debug_frame", MS);
1354
1355   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfARangesSection());
1356   emitSectionContents(Obj, "debug_aranges", MS);
1357 }
1358
1359 /// Emit the pubnames or pubtypes section contribution for \p
1360 /// Unit into \p Sec. The data is provided in \p Names.
1361 void DwarfStreamer::emitPubSectionForUnit(
1362     MCSection *Sec, StringRef SecName, const CompileUnit &Unit,
1363     const std::vector<CompileUnit::AccelInfo> &Names) {
1364   if (Names.empty())
1365     return;
1366
1367   // Start the dwarf pubnames section.
1368   Asm->OutStreamer->SwitchSection(Sec);
1369   MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + SecName + "_begin");
1370   MCSymbol *EndLabel = Asm->createTempSymbol("pub" + SecName + "_end");
1371
1372   bool HeaderEmitted = false;
1373   // Emit the pubnames for this compilation unit.
1374   for (const auto &Name : Names) {
1375     if (Name.SkipPubSection)
1376       continue;
1377
1378     if (!HeaderEmitted) {
1379       // Emit the header.
1380       Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); // Length
1381       Asm->OutStreamer->EmitLabel(BeginLabel);
1382       Asm->emitInt16(dwarf::DW_PUBNAMES_VERSION); // Version
1383       Asm->emitInt32(Unit.getStartOffset());      // Unit offset
1384       Asm->emitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset()); // Size
1385       HeaderEmitted = true;
1386     }
1387     Asm->emitInt32(Name.Die->getOffset());
1388
1389     // Emit the string itself.
1390     Asm->OutStreamer->EmitBytes(Name.Name.getString());
1391     // Emit a null terminator.
1392     Asm->emitInt8(0);
1393   }
1394
1395   if (!HeaderEmitted)
1396     return;
1397   Asm->emitInt32(0); // End marker.
1398   Asm->OutStreamer->EmitLabel(EndLabel);
1399 }
1400
1401 /// Emit .debug_pubnames for \p Unit.
1402 void DwarfStreamer::emitPubNamesForUnit(const CompileUnit &Unit) {
1403   emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubNamesSection(),
1404                         "names", Unit, Unit.getPubnames());
1405 }
1406
1407 /// Emit .debug_pubtypes for \p Unit.
1408 void DwarfStreamer::emitPubTypesForUnit(const CompileUnit &Unit) {
1409   emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubTypesSection(),
1410                         "types", Unit, Unit.getPubtypes());
1411 }
1412
1413 /// Emit a CIE into the debug_frame section.
1414 void DwarfStreamer::emitCIE(StringRef CIEBytes) {
1415   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
1416
1417   MS->EmitBytes(CIEBytes);
1418   FrameSectionSize += CIEBytes.size();
1419 }
1420
1421 /// Emit a FDE into the debug_frame section. \p FDEBytes
1422 /// contains the FDE data without the length, CIE offset and address
1423 /// which will be replaced with the parameter values.
1424 void DwarfStreamer::emitFDE(uint32_t CIEOffset, uint32_t AddrSize,
1425                             uint32_t Address, StringRef FDEBytes) {
1426   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
1427
1428   MS->EmitIntValue(FDEBytes.size() + 4 + AddrSize, 4);
1429   MS->EmitIntValue(CIEOffset, 4);
1430   MS->EmitIntValue(Address, AddrSize);
1431   MS->EmitBytes(FDEBytes);
1432   FrameSectionSize += FDEBytes.size() + 8 + AddrSize;
1433 }
1434
1435 namespace {
1436
1437 /// Partial address range for debug map objects. Besides an offset, only the
1438 /// HighPC is stored. The structure is stored in a map where the LowPC is the
1439 /// key.
1440 struct DebugMapObjectRange {
1441   /// Function HighPC.
1442   uint64_t HighPC;
1443   /// Offset to apply to the linked address.
1444   int64_t Offset;
1445
1446   DebugMapObjectRange(uint64_t EndPC, int64_t Offset)
1447       : HighPC(EndPC), Offset(Offset) {}
1448
1449   DebugMapObjectRange() : HighPC(0), Offset(0) {}
1450 };
1451
1452 /// Map LowPC to DebugMapObjectRange.
1453 using RangesTy = std::map<uint64_t, DebugMapObjectRange>;
1454 using UnitListTy = std::vector<std::unique_ptr<CompileUnit>>;
1455
1456 /// The core of the Dwarf linking logic.
1457 ///
1458 /// The link of the dwarf information from the object files will be
1459 /// driven by the selection of 'root DIEs', which are DIEs that
1460 /// describe variables or functions that are present in the linked
1461 /// binary (and thus have entries in the debug map). All the debug
1462 /// information that will be linked (the DIEs, but also the line
1463 /// tables, ranges, ...) is derived from that set of root DIEs.
1464 ///
1465 /// The root DIEs are identified because they contain relocations that
1466 /// correspond to a debug map entry at specific places (the low_pc for
1467 /// a function, the location for a variable). These relocations are
1468 /// called ValidRelocs in the DwarfLinker and are gathered as a very
1469 /// first step when we start processing a DebugMapObject.
1470 class DwarfLinker {
1471 public:
1472   DwarfLinker(raw_fd_ostream &OutFile, const LinkOptions &Options)
1473       : OutFile(OutFile), Options(Options) {}
1474
1475   /// Link the contents of the DebugMap.
1476   bool link(const DebugMap &);
1477
1478   void reportWarning(const Twine &Warning, const DebugMapObject &DMO,
1479                      const DWARFDie *DIE = nullptr) const;
1480
1481 private:
1482   /// Remembers the newest DWARF version we've seen in a unit.
1483   void maybeUpdateMaxDwarfVersion(unsigned Version) {
1484     if (MaxDwarfVersion < Version)
1485       MaxDwarfVersion = Version;
1486   }
1487
1488   /// Emit warnings as Dwarf compile units to leave a trail after linking.
1489   bool emitPaperTrailWarnings(const DebugMapObject &DMO, const DebugMap &Map,
1490                               OffsetsStringPool &StringPool);
1491
1492   /// Keeps track of relocations.
1493   class RelocationManager {
1494     struct ValidReloc {
1495       uint32_t Offset;
1496       uint32_t Size;
1497       uint64_t Addend;
1498       const DebugMapObject::DebugMapEntry *Mapping;
1499
1500       ValidReloc(uint32_t Offset, uint32_t Size, uint64_t Addend,
1501                  const DebugMapObject::DebugMapEntry *Mapping)
1502           : Offset(Offset), Size(Size), Addend(Addend), Mapping(Mapping) {}
1503
1504       bool operator<(const ValidReloc &RHS) const {
1505         return Offset < RHS.Offset;
1506       }
1507     };
1508
1509     const DwarfLinker &Linker;
1510
1511     /// The valid relocations for the current DebugMapObject.
1512     /// This vector is sorted by relocation offset.
1513     std::vector<ValidReloc> ValidRelocs;
1514
1515     /// Index into ValidRelocs of the next relocation to consider. As we walk
1516     /// the DIEs in acsending file offset and as ValidRelocs is sorted by file
1517     /// offset, keeping this index up to date is all we have to do to have a
1518     /// cheap lookup during the root DIE selection and during DIE cloning.
1519     unsigned NextValidReloc = 0;
1520
1521   public:
1522     RelocationManager(DwarfLinker &Linker) : Linker(Linker) {}
1523
1524     bool hasValidRelocs() const { return !ValidRelocs.empty(); }
1525
1526     /// Reset the NextValidReloc counter.
1527     void resetValidRelocs() { NextValidReloc = 0; }
1528
1529     /// \defgroup FindValidRelocations Translate debug map into a list
1530     /// of relevant relocations
1531     ///
1532     /// @{
1533     bool findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
1534                                     const DebugMapObject &DMO);
1535
1536     bool findValidRelocs(const object::SectionRef &Section,
1537                          const object::ObjectFile &Obj,
1538                          const DebugMapObject &DMO);
1539
1540     void findValidRelocsMachO(const object::SectionRef &Section,
1541                               const object::MachOObjectFile &Obj,
1542                               const DebugMapObject &DMO);
1543     /// @}
1544
1545     bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
1546                             CompileUnit::DIEInfo &Info);
1547
1548     bool applyValidRelocs(MutableArrayRef<char> Data, uint32_t BaseOffset,
1549                           bool isLittleEndian);
1550   };
1551
1552   /// Keeps track of data associated with one object during linking.
1553   struct LinkContext {
1554     DebugMapObject &DMO;
1555     BinaryHolder BinHolder;
1556     const object::ObjectFile *ObjectFile;
1557     RelocationManager RelocMgr;
1558     std::unique_ptr<DWARFContext> DwarfContext;
1559     RangesTy Ranges;
1560     UnitListTy CompileUnits;
1561
1562     LinkContext(const DebugMap &Map, DwarfLinker &Linker, DebugMapObject &DMO,
1563                 bool Verbose = false)
1564         : DMO(DMO), BinHolder(Verbose), RelocMgr(Linker) {
1565       // Swift ASTs are not object files.
1566       if (DMO.getType() == MachO::N_AST) {
1567         ObjectFile = nullptr;
1568         return;
1569       }
1570       auto ErrOrObj = Linker.loadObject(BinHolder, DMO, Map);
1571       ObjectFile = ErrOrObj ? &*ErrOrObj : nullptr;
1572       DwarfContext = ObjectFile ? DWARFContext::create(*ObjectFile) : nullptr;
1573     }
1574
1575     /// Clear compile units and ranges.
1576     void Clear() {
1577       CompileUnits.clear();
1578       Ranges.clear();
1579     }
1580   };
1581
1582   /// Called at the start of a debug object link.
1583   void startDebugObject(LinkContext &Context);
1584
1585   /// Called at the end of a debug object link.
1586   void endDebugObject(LinkContext &Context);
1587
1588   /// \defgroup FindRootDIEs Find DIEs corresponding to debug map entries.
1589   ///
1590   /// @{
1591   /// Recursively walk the \p DIE tree and look for DIEs to
1592   /// keep. Store that information in \p CU's DIEInfo.
1593   ///
1594   /// The return value indicates whether the DIE is incomplete.
1595   bool lookForDIEsToKeep(RelocationManager &RelocMgr, RangesTy &Ranges,
1596                          UnitListTy &Units, const DWARFDie &DIE,
1597                          const DebugMapObject &DMO, CompileUnit &CU,
1598                          unsigned Flags);
1599
1600   /// If this compile unit is really a skeleton CU that points to a
1601   /// clang module, register it in ClangModules and return true.
1602   ///
1603   /// A skeleton CU is a CU without children, a DW_AT_gnu_dwo_name
1604   /// pointing to the module, and a DW_AT_gnu_dwo_id with the module
1605   /// hash.
1606   bool registerModuleReference(const DWARFDie &CUDie, const DWARFUnit &Unit,
1607                                DebugMap &ModuleMap, const DebugMapObject &DMO,
1608                                RangesTy &Ranges,
1609                                OffsetsStringPool &OffsetsStringPool,
1610                                UniquingStringPool &UniquingStringPoolStringPool,
1611                                DeclContextTree &ODRContexts, unsigned &UnitID,
1612                                unsigned Indent = 0);
1613
1614   /// Recursively add the debug info in this clang module .pcm
1615   /// file (and all the modules imported by it in a bottom-up fashion)
1616   /// to Units.
1617   Error loadClangModule(StringRef Filename, StringRef ModulePath,
1618                         StringRef ModuleName, uint64_t DwoId,
1619                         DebugMap &ModuleMap, const DebugMapObject &DMO,
1620                         RangesTy &Ranges, OffsetsStringPool &OffsetsStringPool,
1621                         UniquingStringPool &UniquingStringPool,
1622                         DeclContextTree &ODRContexts, unsigned &UnitID,
1623                         unsigned Indent = 0);
1624
1625   /// Flags passed to DwarfLinker::lookForDIEsToKeep
1626   enum TraversalFlags {
1627     TF_Keep = 1 << 0,            ///< Mark the traversed DIEs as kept.
1628     TF_InFunctionScope = 1 << 1, ///< Current scope is a function scope.
1629     TF_DependencyWalk = 1 << 2,  ///< Walking the dependencies of a kept DIE.
1630     TF_ParentWalk = 1 << 3,      ///< Walking up the parents of a kept DIE.
1631     TF_ODR = 1 << 4,             ///< Use the ODR while keeping dependents.
1632     TF_SkipPC = 1 << 5,          ///< Skip all location attributes.
1633   };
1634
1635   /// Mark the passed DIE as well as all the ones it depends on as kept.
1636   void keepDIEAndDependencies(RelocationManager &RelocMgr, RangesTy &Ranges,
1637                               UnitListTy &Units, const DWARFDie &DIE,
1638                               CompileUnit::DIEInfo &MyInfo,
1639                               const DebugMapObject &DMO, CompileUnit &CU,
1640                               bool UseODR);
1641
1642   unsigned shouldKeepDIE(RelocationManager &RelocMgr, RangesTy &Ranges,
1643                          const DWARFDie &DIE, const DebugMapObject &DMO,
1644                          CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
1645                          unsigned Flags);
1646
1647   unsigned shouldKeepVariableDIE(RelocationManager &RelocMgr,
1648                                  const DWARFDie &DIE, CompileUnit &Unit,
1649                                  CompileUnit::DIEInfo &MyInfo, unsigned Flags);
1650
1651   unsigned shouldKeepSubprogramDIE(RelocationManager &RelocMgr,
1652                                    RangesTy &Ranges, const DWARFDie &DIE,
1653                                    const DebugMapObject &DMO, CompileUnit &Unit,
1654                                    CompileUnit::DIEInfo &MyInfo,
1655                                    unsigned Flags);
1656
1657   bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
1658                           CompileUnit::DIEInfo &Info);
1659   /// @}
1660
1661   /// \defgroup Linking Methods used to link the debug information
1662   ///
1663   /// @{
1664
1665   class DIECloner {
1666     DwarfLinker &Linker;
1667     RelocationManager &RelocMgr;
1668
1669     /// Allocator used for all the DIEValue objects.
1670     BumpPtrAllocator &DIEAlloc;
1671
1672     std::vector<std::unique_ptr<CompileUnit>> &CompileUnits;
1673     LinkOptions Options;
1674
1675   public:
1676     DIECloner(DwarfLinker &Linker, RelocationManager &RelocMgr,
1677               BumpPtrAllocator &DIEAlloc,
1678               std::vector<std::unique_ptr<CompileUnit>> &CompileUnits,
1679               LinkOptions &Options)
1680         : Linker(Linker), RelocMgr(RelocMgr), DIEAlloc(DIEAlloc),
1681           CompileUnits(CompileUnits), Options(Options) {}
1682
1683     /// Recursively clone \p InputDIE into an tree of DIE objects
1684     /// where useless (as decided by lookForDIEsToKeep()) bits have been
1685     /// stripped out and addresses have been rewritten according to the
1686     /// debug map.
1687     ///
1688     /// \param OutOffset is the offset the cloned DIE in the output
1689     /// compile unit.
1690     /// \param PCOffset (while cloning a function scope) is the offset
1691     /// applied to the entry point of the function to get the linked address.
1692     /// \param Die the output DIE to use, pass NULL to create one.
1693     /// \returns the root of the cloned tree or null if nothing was selected.
1694     DIE *cloneDIE(const DWARFDie &InputDIE, const DebugMapObject &DMO,
1695                   CompileUnit &U, OffsetsStringPool &StringPool,
1696                   int64_t PCOffset, uint32_t OutOffset, unsigned Flags,
1697                   DIE *Die = nullptr);
1698
1699     /// Construct the output DIE tree by cloning the DIEs we
1700     /// chose to keep above. If there are no valid relocs, then there's
1701     /// nothing to clone/emit.
1702     void cloneAllCompileUnits(DWARFContext &DwarfContext,
1703                               const DebugMapObject &DMO, RangesTy &Ranges,
1704                               OffsetsStringPool &StringPool);
1705
1706   private:
1707     using AttributeSpec = DWARFAbbreviationDeclaration::AttributeSpec;
1708
1709     /// Information gathered and exchanged between the various
1710     /// clone*Attributes helpers about the attributes of a particular DIE.
1711     struct AttributesInfo {
1712       /// Names.
1713       DwarfStringPoolEntryRef Name, MangledName, NameWithoutTemplate;
1714
1715       /// Offsets in the string pool.
1716       uint32_t NameOffset = 0;
1717       uint32_t MangledNameOffset = 0;
1718
1719       /// Value of AT_low_pc in the input DIE
1720       uint64_t OrigLowPc = std::numeric_limits<uint64_t>::max();
1721
1722       /// Value of AT_high_pc in the input DIE
1723       uint64_t OrigHighPc = 0;
1724
1725       /// Offset to apply to PC addresses inside a function.
1726       int64_t PCOffset = 0;
1727
1728       /// Does the DIE have a low_pc attribute?
1729       bool HasLowPc = false;
1730
1731       /// Does the DIE have a ranges attribute?
1732       bool HasRanges = false;
1733
1734       /// Is this DIE only a declaration?
1735       bool IsDeclaration = false;
1736
1737       AttributesInfo() = default;
1738     };
1739
1740     /// Helper for cloneDIE.
1741     unsigned cloneAttribute(DIE &Die, const DWARFDie &InputDIE,
1742                             const DebugMapObject &DMO, CompileUnit &U,
1743                             OffsetsStringPool &StringPool,
1744                             const DWARFFormValue &Val,
1745                             const AttributeSpec AttrSpec, unsigned AttrSize,
1746                             AttributesInfo &AttrInfo);
1747
1748     /// Clone a string attribute described by \p AttrSpec and add
1749     /// it to \p Die.
1750     /// \returns the size of the new attribute.
1751     unsigned cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec,
1752                                   const DWARFFormValue &Val, const DWARFUnit &U,
1753                                   OffsetsStringPool &StringPool,
1754                                   AttributesInfo &Info);
1755
1756     /// Clone an attribute referencing another DIE and add
1757     /// it to \p Die.
1758     /// \returns the size of the new attribute.
1759     unsigned cloneDieReferenceAttribute(DIE &Die, const DWARFDie &InputDIE,
1760                                         AttributeSpec AttrSpec,
1761                                         unsigned AttrSize,
1762                                         const DWARFFormValue &Val,
1763                                         const DebugMapObject &DMO,
1764                                         CompileUnit &Unit);
1765
1766     /// Clone an attribute referencing another DIE and add
1767     /// it to \p Die.
1768     /// \returns the size of the new attribute.
1769     unsigned cloneBlockAttribute(DIE &Die, AttributeSpec AttrSpec,
1770                                  const DWARFFormValue &Val, unsigned AttrSize);
1771
1772     /// Clone an attribute referencing another DIE and add
1773     /// it to \p Die.
1774     /// \returns the size of the new attribute.
1775     unsigned cloneAddressAttribute(DIE &Die, AttributeSpec AttrSpec,
1776                                    const DWARFFormValue &Val,
1777                                    const CompileUnit &Unit,
1778                                    AttributesInfo &Info);
1779
1780     /// Clone a scalar attribute  and add it to \p Die.
1781     /// \returns the size of the new attribute.
1782     unsigned cloneScalarAttribute(DIE &Die, const DWARFDie &InputDIE,
1783                                   const DebugMapObject &DMO, CompileUnit &U,
1784                                   AttributeSpec AttrSpec,
1785                                   const DWARFFormValue &Val, unsigned AttrSize,
1786                                   AttributesInfo &Info);
1787
1788     /// Get the potential name and mangled name for the entity
1789     /// described by \p Die and store them in \Info if they are not
1790     /// already there.
1791     /// \returns is a name was found.
1792     bool getDIENames(const DWARFDie &Die, AttributesInfo &Info,
1793                      OffsetsStringPool &StringPool, bool StripTemplate = false);
1794
1795     /// Create a copy of abbreviation Abbrev.
1796     void copyAbbrev(const DWARFAbbreviationDeclaration &Abbrev, bool hasODR);
1797
1798     uint32_t hashFullyQualifiedName(DWARFDie DIE, CompileUnit &U,
1799                                     const DebugMapObject &DMO,
1800                                     int RecurseDepth = 0);
1801
1802     /// Helper for cloneDIE.
1803     void addObjCAccelerator(CompileUnit &Unit, const DIE *Die,
1804                             DwarfStringPoolEntryRef Name,
1805                             OffsetsStringPool &StringPool, bool SkipPubSection);
1806   };
1807
1808   /// Assign an abbreviation number to \p Abbrev
1809   void AssignAbbrev(DIEAbbrev &Abbrev);
1810
1811   /// Compute and emit debug_ranges section for \p Unit, and
1812   /// patch the attributes referencing it.
1813   void patchRangesForUnit(const CompileUnit &Unit, DWARFContext &Dwarf,
1814                           const DebugMapObject &DMO) const;
1815
1816   /// Generate and emit the DW_AT_ranges attribute for a compile_unit if it had
1817   /// one.
1818   void generateUnitRanges(CompileUnit &Unit) const;
1819
1820   /// Extract the line tables from the original dwarf, extract the relevant
1821   /// parts according to the linked function ranges and emit the result in the
1822   /// debug_line section.
1823   void patchLineTableForUnit(CompileUnit &Unit, DWARFContext &OrigDwarf,
1824                              RangesTy &Ranges, const DebugMapObject &DMO);
1825
1826   /// Emit the accelerator entries for \p Unit.
1827   void emitAcceleratorEntriesForUnit(CompileUnit &Unit);
1828
1829   /// Patch the frame info for an object file and emit it.
1830   void patchFrameInfoForObject(const DebugMapObject &, RangesTy &Ranges,
1831                                DWARFContext &, unsigned AddressSize);
1832
1833   /// FoldingSet that uniques the abbreviations.
1834   FoldingSet<DIEAbbrev> AbbreviationsSet;
1835
1836   /// Storage for the unique Abbreviations.
1837   /// This is passed to AsmPrinter::emitDwarfAbbrevs(), thus it cannot be
1838   /// changed to a vector of unique_ptrs.
1839   std::vector<std::unique_ptr<DIEAbbrev>> Abbreviations;
1840
1841   /// DIELoc objects that need to be destructed (but not freed!).
1842   std::vector<DIELoc *> DIELocs;
1843
1844   /// DIEBlock objects that need to be destructed (but not freed!).
1845   std::vector<DIEBlock *> DIEBlocks;
1846
1847   /// Allocator used for all the DIEValue objects.
1848   BumpPtrAllocator DIEAlloc;
1849   /// @}
1850
1851   /// \defgroup Helpers Various helper methods.
1852   ///
1853   /// @{
1854   bool createStreamer(const Triple &TheTriple, raw_fd_ostream &OutFile);
1855
1856   /// Attempt to load a debug object from disk.
1857   ErrorOr<const object::ObjectFile &> loadObject(BinaryHolder &BinaryHolder,
1858                                                  const DebugMapObject &Obj,
1859                                                  const DebugMap &Map);
1860   /// @}
1861
1862   raw_fd_ostream &OutFile;
1863   LinkOptions Options;
1864   std::unique_ptr<DwarfStreamer> Streamer;
1865   uint64_t OutputDebugInfoSize;
1866   unsigned MaxDwarfVersion = 0;
1867
1868   /// The CIEs that have been emitted in the output section. The actual CIE
1869   /// data serves a the key to this StringMap, this takes care of comparing the
1870   /// semantics of CIEs defined in different object files.
1871   StringMap<uint32_t> EmittedCIEs;
1872
1873   /// Offset of the last CIE that has been emitted in the output
1874   /// debug_frame section.
1875   uint32_t LastCIEOffset = 0;
1876
1877   /// Apple accelerator tables.
1878   AccelTable<AppleAccelTableStaticOffsetData> AppleNames;
1879   AccelTable<AppleAccelTableStaticOffsetData> AppleNamespaces;
1880   AccelTable<AppleAccelTableStaticOffsetData> AppleObjc;
1881   AccelTable<AppleAccelTableStaticTypeData> AppleTypes;
1882
1883   /// Mapping the PCM filename to the DwoId.
1884   StringMap<uint64_t> ClangModules;
1885
1886   bool ModuleCacheHintDisplayed = false;
1887   bool ArchiveHintDisplayed = false;
1888 };
1889
1890 } // end anonymous namespace
1891
1892 /// Similar to DWARFUnitSection::getUnitForOffset(), but returning our
1893 /// CompileUnit object instead.
1894 static CompileUnit *
1895 getUnitForOffset(std::vector<std::unique_ptr<CompileUnit>> &Units,
1896                  unsigned Offset) {
1897   auto CU = std::upper_bound(
1898       Units.begin(), Units.end(), Offset,
1899       [](uint32_t LHS, const std::unique_ptr<CompileUnit> &RHS) {
1900         return LHS < RHS->getOrigUnit().getNextUnitOffset();
1901       });
1902   return CU != Units.end() ? CU->get() : nullptr;
1903 }
1904
1905 /// Resolve the DIE attribute reference that has been extracted in \p RefValue.
1906 /// The resulting DIE might be in another CompileUnit which is stored into \p
1907 /// ReferencedCU. \returns null if resolving fails for any reason.
1908 static DWARFDie
1909 resolveDIEReference(const DwarfLinker &Linker, const DebugMapObject &DMO,
1910                     std::vector<std::unique_ptr<CompileUnit>> &Units,
1911                     const DWARFFormValue &RefValue, const DWARFUnit &Unit,
1912                     const DWARFDie &DIE, CompileUnit *&RefCU) {
1913   assert(RefValue.isFormClass(DWARFFormValue::FC_Reference));
1914   uint64_t RefOffset = *RefValue.getAsReference();
1915
1916   if ((RefCU = getUnitForOffset(Units, RefOffset)))
1917     if (const auto RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset)) {
1918       // In a file with broken references, an attribute might point to a NULL
1919       // DIE.
1920       if (!RefDie.isNULL())
1921         return RefDie;
1922     }
1923
1924   Linker.reportWarning("could not find referenced DIE", DMO, &DIE);
1925   return DWARFDie();
1926 }
1927
1928 /// \returns whether the passed \a Attr type might contain a DIE reference
1929 /// suitable for ODR uniquing.
1930 static bool isODRAttribute(uint16_t Attr) {
1931   switch (Attr) {
1932   default:
1933     return false;
1934   case dwarf::DW_AT_type:
1935   case dwarf::DW_AT_containing_type:
1936   case dwarf::DW_AT_specification:
1937   case dwarf::DW_AT_abstract_origin:
1938   case dwarf::DW_AT_import:
1939     return true;
1940   }
1941   llvm_unreachable("Improper attribute.");
1942 }
1943
1944 /// Set the last DIE/CU a context was seen in and, possibly invalidate the
1945 /// context if it is ambiguous.
1946 ///
1947 /// In the current implementation, we don't handle overloaded functions well,
1948 /// because the argument types are not taken into account when computing the
1949 /// DeclContext tree.
1950 ///
1951 /// Some of this is mitigated byt using mangled names that do contain the
1952 /// arguments types, but sometimes (e.g. with function templates) we don't have
1953 /// that. In that case, just do not unique anything that refers to the contexts
1954 /// we are not able to distinguish.
1955 ///
1956 /// If a context that is not a namespace appears twice in the same CU, we know
1957 /// it is ambiguous. Make it invalid.
1958 bool DeclContext::setLastSeenDIE(CompileUnit &U, const DWARFDie &Die) {
1959   if (LastSeenCompileUnitID == U.getUniqueID()) {
1960     DWARFUnit &OrigUnit = U.getOrigUnit();
1961     uint32_t FirstIdx = OrigUnit.getDIEIndex(LastSeenDIE);
1962     U.getInfo(FirstIdx).Ctxt = nullptr;
1963     return false;
1964   }
1965
1966   LastSeenCompileUnitID = U.getUniqueID();
1967   LastSeenDIE = Die;
1968   return true;
1969 }
1970
1971 PointerIntPair<DeclContext *, 1> DeclContextTree::getChildDeclContext(
1972     DeclContext &Context, const DWARFDie &DIE, CompileUnit &U,
1973     UniquingStringPool &StringPool, bool InClangModule) {
1974   unsigned Tag = DIE.getTag();
1975
1976   // FIXME: dsymutil-classic compat: We should bail out here if we
1977   // have a specification or an abstract_origin. We will get the
1978   // parent context wrong here.
1979
1980   switch (Tag) {
1981   default:
1982     // By default stop gathering child contexts.
1983     return PointerIntPair<DeclContext *, 1>(nullptr);
1984   case dwarf::DW_TAG_module:
1985     break;
1986   case dwarf::DW_TAG_compile_unit:
1987     return PointerIntPair<DeclContext *, 1>(&Context);
1988   case dwarf::DW_TAG_subprogram:
1989     // Do not unique anything inside CU local functions.
1990     if ((Context.getTag() == dwarf::DW_TAG_namespace ||
1991          Context.getTag() == dwarf::DW_TAG_compile_unit) &&
1992         !dwarf::toUnsigned(DIE.find(dwarf::DW_AT_external), 0))
1993       return PointerIntPair<DeclContext *, 1>(nullptr);
1994     LLVM_FALLTHROUGH;
1995   case dwarf::DW_TAG_member:
1996   case dwarf::DW_TAG_namespace:
1997   case dwarf::DW_TAG_structure_type:
1998   case dwarf::DW_TAG_class_type:
1999   case dwarf::DW_TAG_union_type:
2000   case dwarf::DW_TAG_enumeration_type:
2001   case dwarf::DW_TAG_typedef:
2002     // Artificial things might be ambiguous, because they might be created on
2003     // demand. For example implicitly defined constructors are ambiguous
2004     // because of the way we identify contexts, and they won't be generated
2005     // every time everywhere.
2006     if (dwarf::toUnsigned(DIE.find(dwarf::DW_AT_artificial), 0))
2007       return PointerIntPair<DeclContext *, 1>(nullptr);
2008     break;
2009   }
2010
2011   const char *Name = DIE.getName(DINameKind::LinkageName);
2012   const char *ShortName = DIE.getName(DINameKind::ShortName);
2013   StringRef NameRef;
2014   StringRef ShortNameRef;
2015   StringRef FileRef;
2016
2017   if (Name)
2018     NameRef = StringPool.internString(Name);
2019   else if (Tag == dwarf::DW_TAG_namespace)
2020     // FIXME: For dsymutil-classic compatibility. I think uniquing within
2021     // anonymous namespaces is wrong. There is no ODR guarantee there.
2022     NameRef = StringPool.internString("(anonymous namespace)");
2023
2024   if (ShortName && ShortName != Name)
2025     ShortNameRef = StringPool.internString(ShortName);
2026   else
2027     ShortNameRef = NameRef;
2028
2029   if (Tag != dwarf::DW_TAG_class_type && Tag != dwarf::DW_TAG_structure_type &&
2030       Tag != dwarf::DW_TAG_union_type &&
2031       Tag != dwarf::DW_TAG_enumeration_type && NameRef.empty())
2032     return PointerIntPair<DeclContext *, 1>(nullptr);
2033
2034   unsigned Line = 0;
2035   unsigned ByteSize = std::numeric_limits<uint32_t>::max();
2036
2037   if (!InClangModule) {
2038     // Gather some discriminating data about the DeclContext we will be
2039     // creating: File, line number and byte size. This shouldn't be necessary,
2040     // because the ODR is just about names, but given that we do some
2041     // approximations with overloaded functions and anonymous namespaces, use
2042     // these additional data points to make the process safer.
2043     //
2044     // This is disabled for clang modules, because forward declarations of
2045     // module-defined types do not have a file and line.
2046     ByteSize = dwarf::toUnsigned(DIE.find(dwarf::DW_AT_byte_size),
2047                                  std::numeric_limits<uint64_t>::max());
2048     if (Tag != dwarf::DW_TAG_namespace || !Name) {
2049       if (unsigned FileNum =
2050               dwarf::toUnsigned(DIE.find(dwarf::DW_AT_decl_file), 0)) {
2051         if (const auto *LT = U.getOrigUnit().getContext().getLineTableForUnit(
2052                 &U.getOrigUnit())) {
2053           // FIXME: dsymutil-classic compatibility. I'd rather not
2054           // unique anything in anonymous namespaces, but if we do, then
2055           // verify that the file and line correspond.
2056           if (!Name && Tag == dwarf::DW_TAG_namespace)
2057             FileNum = 1;
2058
2059           if (LT->hasFileAtIndex(FileNum)) {
2060             Line = dwarf::toUnsigned(DIE.find(dwarf::DW_AT_decl_line), 0);
2061             // Cache the resolved paths based on the index in the line table,
2062             // because calling realpath is expansive.
2063             StringRef ResolvedPath = U.getResolvedPath(FileNum);
2064             if (!ResolvedPath.empty()) {
2065               FileRef = ResolvedPath;
2066             } else {
2067               std::string File;
2068               bool FoundFileName = LT->getFileNameByIndex(
2069                   FileNum, U.getOrigUnit().getCompilationDir(),
2070                   DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
2071                   File);
2072               (void)FoundFileName;
2073               assert(FoundFileName && "Must get file name from line table");
2074               // Second level of caching, this time based on the file's parent
2075               // path.
2076               FileRef = PathResolver.resolve(File, StringPool);
2077               U.setResolvedPath(FileNum, FileRef);
2078             }
2079           }
2080         }
2081       }
2082     }
2083   }
2084
2085   if (!Line && NameRef.empty())
2086     return PointerIntPair<DeclContext *, 1>(nullptr);
2087
2088   // We hash NameRef, which is the mangled name, in order to get most
2089   // overloaded functions resolve correctly.
2090   //
2091   // Strictly speaking, hashing the Tag is only necessary for a
2092   // DW_TAG_module, to prevent uniquing of a module and a namespace
2093   // with the same name.
2094   //
2095   // FIXME: dsymutil-classic won't unique the same type presented
2096   // once as a struct and once as a class. Using the Tag in the fully
2097   // qualified name hash to get the same effect.
2098   unsigned Hash = hash_combine(Context.getQualifiedNameHash(), Tag, NameRef);
2099
2100   // FIXME: dsymutil-classic compatibility: when we don't have a name,
2101   // use the filename.
2102   if (Tag == dwarf::DW_TAG_namespace && NameRef == "(anonymous namespace)")
2103     Hash = hash_combine(Hash, FileRef);
2104
2105   // Now look if this context already exists.
2106   DeclContext Key(Hash, Line, ByteSize, Tag, NameRef, FileRef, Context);
2107   auto ContextIter = Contexts.find(&Key);
2108
2109   if (ContextIter == Contexts.end()) {
2110     // The context wasn't found.
2111     bool Inserted;
2112     DeclContext *NewContext =
2113         new (Allocator) DeclContext(Hash, Line, ByteSize, Tag, NameRef, FileRef,
2114                                     Context, DIE, U.getUniqueID());
2115     std::tie(ContextIter, Inserted) = Contexts.insert(NewContext);
2116     assert(Inserted && "Failed to insert DeclContext");
2117     (void)Inserted;
2118   } else if (Tag != dwarf::DW_TAG_namespace &&
2119              !(*ContextIter)->setLastSeenDIE(U, DIE)) {
2120     // The context was found, but it is ambiguous with another context
2121     // in the same file. Mark it invalid.
2122     return PointerIntPair<DeclContext *, 1>(*ContextIter, /* Invalid= */ 1);
2123   }
2124
2125   assert(ContextIter != Contexts.end());
2126   // FIXME: dsymutil-classic compatibility. Union types aren't
2127   // uniques, but their children might be.
2128   if ((Tag == dwarf::DW_TAG_subprogram &&
2129        Context.getTag() != dwarf::DW_TAG_structure_type &&
2130        Context.getTag() != dwarf::DW_TAG_class_type) ||
2131       (Tag == dwarf::DW_TAG_union_type))
2132     return PointerIntPair<DeclContext *, 1>(*ContextIter, /* Invalid= */ 1);
2133
2134   return PointerIntPair<DeclContext *, 1>(*ContextIter);
2135 }
2136
2137 bool DwarfLinker::DIECloner::getDIENames(const DWARFDie &Die,
2138                                          AttributesInfo &Info,
2139                                          OffsetsStringPool &StringPool,
2140                                          bool StripTemplate) {
2141   // This function will be called on DIEs having low_pcs and
2142   // ranges. As getting the name might be more expansive, filter out
2143   // blocks directly.
2144   if (Die.getTag() == dwarf::DW_TAG_lexical_block)
2145     return false;
2146
2147   // FIXME: a bit wasteful as the first getName might return the
2148   // short name.
2149   if (!Info.MangledName)
2150     if (const char *MangledName = Die.getName(DINameKind::LinkageName))
2151       Info.MangledName = StringPool.getEntry(MangledName);
2152
2153   if (!Info.Name)
2154     if (const char *Name = Die.getName(DINameKind::ShortName))
2155       Info.Name = StringPool.getEntry(Name);
2156
2157   if (StripTemplate && Info.Name && Info.MangledName != Info.Name) {
2158     // FIXME: dsymutil compatibility. This is wrong for operator<
2159     auto Split = Info.Name.getString().split('<');
2160     if (!Split.second.empty())
2161       Info.NameWithoutTemplate = StringPool.getEntry(Split.first);
2162   }
2163
2164   return Info.Name || Info.MangledName;
2165 }
2166
2167 /// Report a warning to the user, optionally including information about a
2168 /// specific \p DIE related to the warning.
2169 void DwarfLinker::reportWarning(const Twine &Warning, const DebugMapObject &DMO,
2170                                 const DWARFDie *DIE) const {
2171   StringRef Context = DMO.getObjectFilename();
2172   warn(Warning, Context);
2173
2174   if (!Options.Verbose || !DIE)
2175     return;
2176
2177   DIDumpOptions DumpOpts;
2178   DumpOpts.RecurseDepth = 0;
2179   DumpOpts.Verbose = Options.Verbose;
2180
2181   note_ostream() << "    in DIE:\n";
2182   DIE->dump(errs(), 6 /* Indent */, DumpOpts);
2183 }
2184
2185 bool DwarfLinker::createStreamer(const Triple &TheTriple,
2186                                  raw_fd_ostream &OutFile) {
2187   if (Options.NoOutput)
2188     return true;
2189
2190   Streamer = llvm::make_unique<DwarfStreamer>(OutFile);
2191   return Streamer->init(TheTriple);
2192 }
2193
2194 /// Recursive helper to build the global DeclContext information and
2195 /// gather the child->parent relationships in the original compile unit.
2196 ///
2197 /// \return true when this DIE and all of its children are only
2198 /// forward declarations to types defined in external clang modules
2199 /// (i.e., forward declarations that are children of a DW_TAG_module).
2200 static bool analyzeContextInfo(const DWARFDie &DIE, unsigned ParentIdx,
2201                                CompileUnit &CU, DeclContext *CurrentDeclContext,
2202                                UniquingStringPool &StringPool,
2203                                DeclContextTree &Contexts,
2204                                bool InImportedModule = false) {
2205   unsigned MyIdx = CU.getOrigUnit().getDIEIndex(DIE);
2206   CompileUnit::DIEInfo &Info = CU.getInfo(MyIdx);
2207
2208   // Clang imposes an ODR on modules(!) regardless of the language:
2209   //  "The module-id should consist of only a single identifier,
2210   //   which provides the name of the module being defined. Each
2211   //   module shall have a single definition."
2212   //
2213   // This does not extend to the types inside the modules:
2214   //  "[I]n C, this implies that if two structs are defined in
2215   //   different submodules with the same name, those two types are
2216   //   distinct types (but may be compatible types if their
2217   //   definitions match)."
2218   //
2219   // We treat non-C++ modules like namespaces for this reason.
2220   if (DIE.getTag() == dwarf::DW_TAG_module && ParentIdx == 0 &&
2221       dwarf::toString(DIE.find(dwarf::DW_AT_name), "") !=
2222           CU.getClangModuleName()) {
2223     InImportedModule = true;
2224   }
2225
2226   Info.ParentIdx = ParentIdx;
2227   bool InClangModule = CU.isClangModule() || InImportedModule;
2228   if (CU.hasODR() || InClangModule) {
2229     if (CurrentDeclContext) {
2230       auto PtrInvalidPair = Contexts.getChildDeclContext(
2231           *CurrentDeclContext, DIE, CU, StringPool, InClangModule);
2232       CurrentDeclContext = PtrInvalidPair.getPointer();
2233       Info.Ctxt =
2234           PtrInvalidPair.getInt() ? nullptr : PtrInvalidPair.getPointer();
2235       if (Info.Ctxt)
2236         Info.Ctxt->setDefinedInClangModule(InClangModule);
2237     } else
2238       Info.Ctxt = CurrentDeclContext = nullptr;
2239   }
2240
2241   Info.Prune = InImportedModule;
2242   if (DIE.hasChildren())
2243     for (auto Child : DIE.children())
2244       Info.Prune &= analyzeContextInfo(Child, MyIdx, CU, CurrentDeclContext,
2245                                        StringPool, Contexts, InImportedModule);
2246
2247   // Prune this DIE if it is either a forward declaration inside a
2248   // DW_TAG_module or a DW_TAG_module that contains nothing but
2249   // forward declarations.
2250   Info.Prune &= (DIE.getTag() == dwarf::DW_TAG_module) ||
2251                 dwarf::toUnsigned(DIE.find(dwarf::DW_AT_declaration), 0);
2252
2253   // Don't prune it if there is no definition for the DIE.
2254   Info.Prune &= Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset();
2255
2256   return Info.Prune;
2257 }
2258
2259 static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) {
2260   switch (Tag) {
2261   default:
2262     return false;
2263   case dwarf::DW_TAG_subprogram:
2264   case dwarf::DW_TAG_lexical_block:
2265   case dwarf::DW_TAG_subroutine_type:
2266   case dwarf::DW_TAG_structure_type:
2267   case dwarf::DW_TAG_class_type:
2268   case dwarf::DW_TAG_union_type:
2269     return true;
2270   }
2271   llvm_unreachable("Invalid Tag");
2272 }
2273
2274 void DwarfLinker::startDebugObject(LinkContext &Context) {
2275   // Iterate over the debug map entries and put all the ones that are
2276   // functions (because they have a size) into the Ranges map. This map is
2277   // very similar to the FunctionRanges that are stored in each unit, with 2
2278   // notable differences:
2279   //
2280   //  1. Obviously this one is global, while the other ones are per-unit.
2281   //
2282   //  2. This one contains not only the functions described in the DIE
2283   //     tree, but also the ones that are only in the debug map.
2284   //
2285   // The latter information is required to reproduce dsymutil's logic while
2286   // linking line tables. The cases where this information matters look like
2287   // bugs that need to be investigated, but for now we need to reproduce
2288   // dsymutil's behavior.
2289   // FIXME: Once we understood exactly if that information is needed,
2290   // maybe totally remove this (or try to use it to do a real
2291   // -gline-tables-only on Darwin.
2292   for (const auto &Entry : Context.DMO.symbols()) {
2293     const auto &Mapping = Entry.getValue();
2294     if (Mapping.Size && Mapping.ObjectAddress)
2295       Context.Ranges[*Mapping.ObjectAddress] = DebugMapObjectRange(
2296           *Mapping.ObjectAddress + Mapping.Size,
2297           int64_t(Mapping.BinaryAddress) - *Mapping.ObjectAddress);
2298   }
2299 }
2300
2301 void DwarfLinker::endDebugObject(LinkContext &Context) {
2302   Context.Clear();
2303   for (auto I = DIEBlocks.begin(), E = DIEBlocks.end(); I != E; ++I)
2304     (*I)->~DIEBlock();
2305   for (auto I = DIELocs.begin(), E = DIELocs.end(); I != E; ++I)
2306     (*I)->~DIELoc();
2307
2308   DIEBlocks.clear();
2309   DIELocs.clear();
2310   DIEAlloc.Reset();
2311 }
2312
2313 static bool isMachOPairedReloc(uint64_t RelocType, uint64_t Arch) {
2314   switch (Arch) {
2315   case Triple::x86:
2316     return RelocType == MachO::GENERIC_RELOC_SECTDIFF ||
2317            RelocType == MachO::GENERIC_RELOC_LOCAL_SECTDIFF;
2318   case Triple::x86_64:
2319     return RelocType == MachO::X86_64_RELOC_SUBTRACTOR;
2320   case Triple::arm:
2321   case Triple::thumb:
2322     return RelocType == MachO::ARM_RELOC_SECTDIFF ||
2323            RelocType == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
2324            RelocType == MachO::ARM_RELOC_HALF ||
2325            RelocType == MachO::ARM_RELOC_HALF_SECTDIFF;
2326   case Triple::aarch64:
2327     return RelocType == MachO::ARM64_RELOC_SUBTRACTOR;
2328   default:
2329     return false;
2330   }
2331 }
2332
2333 /// Iterate over the relocations of the given \p Section and
2334 /// store the ones that correspond to debug map entries into the
2335 /// ValidRelocs array.
2336 void DwarfLinker::RelocationManager::findValidRelocsMachO(
2337     const object::SectionRef &Section, const object::MachOObjectFile &Obj,
2338     const DebugMapObject &DMO) {
2339   StringRef Contents;
2340   Section.getContents(Contents);
2341   DataExtractor Data(Contents, Obj.isLittleEndian(), 0);
2342   bool SkipNext = false;
2343
2344   for (const object::RelocationRef &Reloc : Section.relocations()) {
2345     if (SkipNext) {
2346       SkipNext = false;
2347       continue;
2348     }
2349
2350     object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl();
2351     MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef);
2352
2353     if (isMachOPairedReloc(Obj.getAnyRelocationType(MachOReloc),
2354                            Obj.getArch())) {
2355       SkipNext = true;
2356       Linker.reportWarning("unsupported relocation in debug_info section.",
2357                            DMO);
2358       continue;
2359     }
2360
2361     unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc);
2362     uint64_t Offset64 = Reloc.getOffset();
2363     if ((RelocSize != 4 && RelocSize != 8)) {
2364       Linker.reportWarning("unsupported relocation in debug_info section.",
2365                            DMO);
2366       continue;
2367     }
2368     uint32_t Offset = Offset64;
2369     // Mach-o uses REL relocations, the addend is at the relocation offset.
2370     uint64_t Addend = Data.getUnsigned(&Offset, RelocSize);
2371     uint64_t SymAddress;
2372     int64_t SymOffset;
2373
2374     if (Obj.isRelocationScattered(MachOReloc)) {
2375       // The address of the base symbol for scattered relocations is
2376       // stored in the reloc itself. The actual addend will store the
2377       // base address plus the offset.
2378       SymAddress = Obj.getScatteredRelocationValue(MachOReloc);
2379       SymOffset = int64_t(Addend) - SymAddress;
2380     } else {
2381       SymAddress = Addend;
2382       SymOffset = 0;
2383     }
2384
2385     auto Sym = Reloc.getSymbol();
2386     if (Sym != Obj.symbol_end()) {
2387       Expected<StringRef> SymbolName = Sym->getName();
2388       if (!SymbolName) {
2389         consumeError(SymbolName.takeError());
2390         Linker.reportWarning("error getting relocation symbol name.", DMO);
2391         continue;
2392       }
2393       if (const auto *Mapping = DMO.lookupSymbol(*SymbolName))
2394         ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping);
2395     } else if (const auto *Mapping = DMO.lookupObjectAddress(SymAddress)) {
2396       // Do not store the addend. The addend was the address of the symbol in
2397       // the object file, the address in the binary that is stored in the debug
2398       // map doesn't need to be offset.
2399       ValidRelocs.emplace_back(Offset64, RelocSize, SymOffset, Mapping);
2400     }
2401   }
2402 }
2403
2404 /// Dispatch the valid relocation finding logic to the
2405 /// appropriate handler depending on the object file format.
2406 bool DwarfLinker::RelocationManager::findValidRelocs(
2407     const object::SectionRef &Section, const object::ObjectFile &Obj,
2408     const DebugMapObject &DMO) {
2409   // Dispatch to the right handler depending on the file type.
2410   if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj))
2411     findValidRelocsMachO(Section, *MachOObj, DMO);
2412   else
2413     Linker.reportWarning(
2414         Twine("unsupported object file type: ") + Obj.getFileName(), DMO);
2415
2416   if (ValidRelocs.empty())
2417     return false;
2418
2419   // Sort the relocations by offset. We will walk the DIEs linearly in
2420   // the file, this allows us to just keep an index in the relocation
2421   // array that we advance during our walk, rather than resorting to
2422   // some associative container. See DwarfLinker::NextValidReloc.
2423   llvm::sort(ValidRelocs.begin(), ValidRelocs.end());
2424   return true;
2425 }
2426
2427 /// Look for relocations in the debug_info section that match
2428 /// entries in the debug map. These relocations will drive the Dwarf
2429 /// link by indicating which DIEs refer to symbols present in the
2430 /// linked binary.
2431 /// \returns whether there are any valid relocations in the debug info.
2432 bool DwarfLinker::RelocationManager::findValidRelocsInDebugInfo(
2433     const object::ObjectFile &Obj, const DebugMapObject &DMO) {
2434   // Find the debug_info section.
2435   for (const object::SectionRef &Section : Obj.sections()) {
2436     StringRef SectionName;
2437     Section.getName(SectionName);
2438     SectionName = SectionName.substr(SectionName.find_first_not_of("._"));
2439     if (SectionName != "debug_info")
2440       continue;
2441     return findValidRelocs(Section, Obj, DMO);
2442   }
2443   return false;
2444 }
2445
2446 /// Checks that there is a relocation against an actual debug
2447 /// map entry between \p StartOffset and \p NextOffset.
2448 ///
2449 /// This function must be called with offsets in strictly ascending
2450 /// order because it never looks back at relocations it already 'went past'.
2451 /// \returns true and sets Info.InDebugMap if it is the case.
2452 bool DwarfLinker::RelocationManager::hasValidRelocation(
2453     uint32_t StartOffset, uint32_t EndOffset, CompileUnit::DIEInfo &Info) {
2454   assert(NextValidReloc == 0 ||
2455          StartOffset > ValidRelocs[NextValidReloc - 1].Offset);
2456   if (NextValidReloc >= ValidRelocs.size())
2457     return false;
2458
2459   uint64_t RelocOffset = ValidRelocs[NextValidReloc].Offset;
2460
2461   // We might need to skip some relocs that we didn't consider. For
2462   // example the high_pc of a discarded DIE might contain a reloc that
2463   // is in the list because it actually corresponds to the start of a
2464   // function that is in the debug map.
2465   while (RelocOffset < StartOffset && NextValidReloc < ValidRelocs.size() - 1)
2466     RelocOffset = ValidRelocs[++NextValidReloc].Offset;
2467
2468   if (RelocOffset < StartOffset || RelocOffset >= EndOffset)
2469     return false;
2470
2471   const auto &ValidReloc = ValidRelocs[NextValidReloc++];
2472   const auto &Mapping = ValidReloc.Mapping->getValue();
2473   uint64_t ObjectAddress = Mapping.ObjectAddress
2474                                ? uint64_t(*Mapping.ObjectAddress)
2475                                : std::numeric_limits<uint64_t>::max();
2476   if (Linker.Options.Verbose)
2477     outs() << "Found valid debug map entry: " << ValidReloc.Mapping->getKey()
2478            << " "
2479            << format("\t%016" PRIx64 " => %016" PRIx64, ObjectAddress,
2480                      uint64_t(Mapping.BinaryAddress));
2481
2482   Info.AddrAdjust = int64_t(Mapping.BinaryAddress) + ValidReloc.Addend;
2483   if (Mapping.ObjectAddress)
2484     Info.AddrAdjust -= ObjectAddress;
2485   Info.InDebugMap = true;
2486   return true;
2487 }
2488
2489 /// Get the starting and ending (exclusive) offset for the
2490 /// attribute with index \p Idx descibed by \p Abbrev. \p Offset is
2491 /// supposed to point to the position of the first attribute described
2492 /// by \p Abbrev.
2493 /// \return [StartOffset, EndOffset) as a pair.
2494 static std::pair<uint32_t, uint32_t>
2495 getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx,
2496                     unsigned Offset, const DWARFUnit &Unit) {
2497   DataExtractor Data = Unit.getDebugInfoExtractor();
2498
2499   for (unsigned i = 0; i < Idx; ++i)
2500     DWARFFormValue::skipValue(Abbrev->getFormByIndex(i), Data, &Offset,
2501                               Unit.getFormParams());
2502
2503   uint32_t End = Offset;
2504   DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End,
2505                             Unit.getFormParams());
2506
2507   return std::make_pair(Offset, End);
2508 }
2509
2510 /// Check if a variable describing DIE should be kept.
2511 /// \returns updated TraversalFlags.
2512 unsigned DwarfLinker::shouldKeepVariableDIE(RelocationManager &RelocMgr,
2513                                             const DWARFDie &DIE,
2514                                             CompileUnit &Unit,
2515                                             CompileUnit::DIEInfo &MyInfo,
2516                                             unsigned Flags) {
2517   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
2518
2519   // Global variables with constant value can always be kept.
2520   if (!(Flags & TF_InFunctionScope) &&
2521       Abbrev->findAttributeIndex(dwarf::DW_AT_const_value)) {
2522     MyInfo.InDebugMap = true;
2523     return Flags | TF_Keep;
2524   }
2525
2526   Optional<uint32_t> LocationIdx =
2527       Abbrev->findAttributeIndex(dwarf::DW_AT_location);
2528   if (!LocationIdx)
2529     return Flags;
2530
2531   uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
2532   const DWARFUnit &OrigUnit = Unit.getOrigUnit();
2533   uint32_t LocationOffset, LocationEndOffset;
2534   std::tie(LocationOffset, LocationEndOffset) =
2535       getAttributeOffsets(Abbrev, *LocationIdx, Offset, OrigUnit);
2536
2537   // See if there is a relocation to a valid debug map entry inside
2538   // this variable's location. The order is important here. We want to
2539   // always check in the variable has a valid relocation, so that the
2540   // DIEInfo is filled. However, we don't want a static variable in a
2541   // function to force us to keep the enclosing function.
2542   if (!RelocMgr.hasValidRelocation(LocationOffset, LocationEndOffset, MyInfo) ||
2543       (Flags & TF_InFunctionScope))
2544     return Flags;
2545
2546   if (Options.Verbose) {
2547     DIDumpOptions DumpOpts;
2548     DumpOpts.RecurseDepth = 0;
2549     DumpOpts.Verbose = Options.Verbose;
2550     DIE.dump(outs(), 8 /* Indent */, DumpOpts);
2551   }
2552
2553   return Flags | TF_Keep;
2554 }
2555
2556 /// Check if a function describing DIE should be kept.
2557 /// \returns updated TraversalFlags.
2558 unsigned DwarfLinker::shouldKeepSubprogramDIE(
2559     RelocationManager &RelocMgr, RangesTy &Ranges, const DWARFDie &DIE,
2560     const DebugMapObject &DMO, CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
2561     unsigned Flags) {
2562   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
2563
2564   Flags |= TF_InFunctionScope;
2565
2566   Optional<uint32_t> LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc);
2567   if (!LowPcIdx)
2568     return Flags;
2569
2570   uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
2571   DWARFUnit &OrigUnit = Unit.getOrigUnit();
2572   uint32_t LowPcOffset, LowPcEndOffset;
2573   std::tie(LowPcOffset, LowPcEndOffset) =
2574       getAttributeOffsets(Abbrev, *LowPcIdx, Offset, OrigUnit);
2575
2576   auto LowPc = dwarf::toAddress(DIE.find(dwarf::DW_AT_low_pc));
2577   assert(LowPc.hasValue() && "low_pc attribute is not an address.");
2578   if (!LowPc ||
2579       !RelocMgr.hasValidRelocation(LowPcOffset, LowPcEndOffset, MyInfo))
2580     return Flags;
2581
2582   if (Options.Verbose) {
2583     DIDumpOptions DumpOpts;
2584     DumpOpts.RecurseDepth = 0;
2585     DumpOpts.Verbose = Options.Verbose;
2586     DIE.dump(outs(), 8 /* Indent */, DumpOpts);
2587   }
2588
2589   if (DIE.getTag() == dwarf::DW_TAG_label) {
2590     if (Unit.hasLabelAt(*LowPc))
2591       return Flags;
2592     // FIXME: dsymutil-classic compat. dsymutil-classic doesn't consider labels
2593     // that don't fall into the CU's aranges. This is wrong IMO. Debug info
2594     // generation bugs aside, this is really wrong in the case of labels, where
2595     // a label marking the end of a function will have a PC == CU's high_pc.
2596     if (dwarf::toAddress(OrigUnit.getUnitDIE().find(dwarf::DW_AT_high_pc))
2597             .getValueOr(UINT64_MAX) <= LowPc)
2598       return Flags;
2599     Unit.addLabelLowPc(*LowPc, MyInfo.AddrAdjust);
2600     return Flags | TF_Keep;
2601   }
2602
2603   Flags |= TF_Keep;
2604
2605   Optional<uint64_t> HighPc = DIE.getHighPC(*LowPc);
2606   if (!HighPc) {
2607     reportWarning("Function without high_pc. Range will be discarded.\n", DMO,
2608                   &DIE);
2609     return Flags;
2610   }
2611
2612   // Replace the debug map range with a more accurate one.
2613   Ranges[*LowPc] = DebugMapObjectRange(*HighPc, MyInfo.AddrAdjust);
2614   Unit.addFunctionRange(*LowPc, *HighPc, MyInfo.AddrAdjust);
2615   return Flags;
2616 }
2617
2618 /// Check if a DIE should be kept.
2619 /// \returns updated TraversalFlags.
2620 unsigned DwarfLinker::shouldKeepDIE(RelocationManager &RelocMgr,
2621                                     RangesTy &Ranges, const DWARFDie &DIE,
2622                                     const DebugMapObject &DMO,
2623                                     CompileUnit &Unit,
2624                                     CompileUnit::DIEInfo &MyInfo,
2625                                     unsigned Flags) {
2626   switch (DIE.getTag()) {
2627   case dwarf::DW_TAG_constant:
2628   case dwarf::DW_TAG_variable:
2629     return shouldKeepVariableDIE(RelocMgr, DIE, Unit, MyInfo, Flags);
2630   case dwarf::DW_TAG_subprogram:
2631   case dwarf::DW_TAG_label:
2632     return shouldKeepSubprogramDIE(RelocMgr, Ranges, DIE, DMO, Unit, MyInfo,
2633                                    Flags);
2634   case dwarf::DW_TAG_imported_module:
2635   case dwarf::DW_TAG_imported_declaration:
2636   case dwarf::DW_TAG_imported_unit:
2637     // We always want to keep these.
2638     return Flags | TF_Keep;
2639   default:
2640     break;
2641   }
2642
2643   return Flags;
2644 }
2645
2646 /// Mark the passed DIE as well as all the ones it depends on
2647 /// as kept.
2648 ///
2649 /// This function is called by lookForDIEsToKeep on DIEs that are
2650 /// newly discovered to be needed in the link. It recursively calls
2651 /// back to lookForDIEsToKeep while adding TF_DependencyWalk to the
2652 /// TraversalFlags to inform it that it's not doing the primary DIE
2653 /// tree walk.
2654 void DwarfLinker::keepDIEAndDependencies(RelocationManager &RelocMgr,
2655                                          RangesTy &Ranges, UnitListTy &Units,
2656                                          const DWARFDie &Die,
2657                                          CompileUnit::DIEInfo &MyInfo,
2658                                          const DebugMapObject &DMO,
2659                                          CompileUnit &CU, bool UseODR) {
2660   DWARFUnit &Unit = CU.getOrigUnit();
2661   MyInfo.Keep = true;
2662
2663   // We're looking for incomplete types.
2664   MyInfo.Incomplete = Die.getTag() != dwarf::DW_TAG_subprogram &&
2665                       Die.getTag() != dwarf::DW_TAG_member &&
2666                       dwarf::toUnsigned(Die.find(dwarf::DW_AT_declaration), 0);
2667
2668   // First mark all the parent chain as kept.
2669   unsigned AncestorIdx = MyInfo.ParentIdx;
2670   while (!CU.getInfo(AncestorIdx).Keep) {
2671     unsigned ODRFlag = UseODR ? TF_ODR : 0;
2672     lookForDIEsToKeep(RelocMgr, Ranges, Units, Unit.getDIEAtIndex(AncestorIdx),
2673                       DMO, CU,
2674                       TF_ParentWalk | TF_Keep | TF_DependencyWalk | ODRFlag);
2675     AncestorIdx = CU.getInfo(AncestorIdx).ParentIdx;
2676   }
2677
2678   // Then we need to mark all the DIEs referenced by this DIE's
2679   // attributes as kept.
2680   DWARFDataExtractor Data = Unit.getDebugInfoExtractor();
2681   const auto *Abbrev = Die.getAbbreviationDeclarationPtr();
2682   uint32_t Offset = Die.getOffset() + getULEB128Size(Abbrev->getCode());
2683
2684   // Mark all DIEs referenced through attributes as kept.
2685   for (const auto &AttrSpec : Abbrev->attributes()) {
2686     DWARFFormValue Val(AttrSpec.Form);
2687
2688     if (!Val.isFormClass(DWARFFormValue::FC_Reference) ||
2689         AttrSpec.Attr == dwarf::DW_AT_sibling) {
2690       DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset,
2691                                 Unit.getFormParams());
2692       continue;
2693     }
2694
2695     Val.extractValue(Data, &Offset, Unit.getFormParams(), &Unit);
2696     CompileUnit *ReferencedCU;
2697     if (auto RefDie = resolveDIEReference(*this, DMO, Units, Val, Unit, Die,
2698                                           ReferencedCU)) {
2699       uint32_t RefIdx = ReferencedCU->getOrigUnit().getDIEIndex(RefDie);
2700       CompileUnit::DIEInfo &Info = ReferencedCU->getInfo(RefIdx);
2701       bool IsModuleRef = Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset() &&
2702                          Info.Ctxt->isDefinedInClangModule();
2703       // If the referenced DIE has a DeclContext that has already been
2704       // emitted, then do not keep the one in this CU. We'll link to
2705       // the canonical DIE in cloneDieReferenceAttribute.
2706       // FIXME: compatibility with dsymutil-classic. UseODR shouldn't
2707       // be necessary and could be advantageously replaced by
2708       // ReferencedCU->hasODR() && CU.hasODR().
2709       // FIXME: compatibility with dsymutil-classic. There is no
2710       // reason not to unique ref_addr references.
2711       if (AttrSpec.Form != dwarf::DW_FORM_ref_addr && (UseODR || IsModuleRef) &&
2712           Info.Ctxt &&
2713           Info.Ctxt != ReferencedCU->getInfo(Info.ParentIdx).Ctxt &&
2714           Info.Ctxt->getCanonicalDIEOffset() && isODRAttribute(AttrSpec.Attr))
2715         continue;
2716
2717       // Keep a module forward declaration if there is no definition.
2718       if (!(isODRAttribute(AttrSpec.Attr) && Info.Ctxt &&
2719             Info.Ctxt->getCanonicalDIEOffset()))
2720         Info.Prune = false;
2721
2722       unsigned ODRFlag = UseODR ? TF_ODR : 0;
2723       lookForDIEsToKeep(RelocMgr, Ranges, Units, RefDie, DMO, *ReferencedCU,
2724                         TF_Keep | TF_DependencyWalk | ODRFlag);
2725
2726       // The incomplete property is propagated if the current DIE is complete
2727       // but references an incomplete DIE.
2728       if (Info.Incomplete && !MyInfo.Incomplete &&
2729           (Die.getTag() == dwarf::DW_TAG_typedef ||
2730            Die.getTag() == dwarf::DW_TAG_member ||
2731            Die.getTag() == dwarf::DW_TAG_reference_type ||
2732            Die.getTag() == dwarf::DW_TAG_ptr_to_member_type ||
2733            Die.getTag() == dwarf::DW_TAG_pointer_type))
2734         MyInfo.Incomplete = true;
2735     }
2736   }
2737 }
2738
2739 /// Recursively walk the \p DIE tree and look for DIEs to
2740 /// keep. Store that information in \p CU's DIEInfo.
2741 ///
2742 /// This function is the entry point of the DIE selection
2743 /// algorithm. It is expected to walk the DIE tree in file order and
2744 /// (though the mediation of its helper) call hasValidRelocation() on
2745 /// each DIE that might be a 'root DIE' (See DwarfLinker class
2746 /// comment).
2747 /// While walking the dependencies of root DIEs, this function is
2748 /// also called, but during these dependency walks the file order is
2749 /// not respected. The TF_DependencyWalk flag tells us which kind of
2750 /// traversal we are currently doing.
2751 ///
2752 /// The return value indicates whether the DIE is incomplete.
2753 bool DwarfLinker::lookForDIEsToKeep(RelocationManager &RelocMgr,
2754                                     RangesTy &Ranges, UnitListTy &Units,
2755                                     const DWARFDie &Die,
2756                                     const DebugMapObject &DMO, CompileUnit &CU,
2757                                     unsigned Flags) {
2758   unsigned Idx = CU.getOrigUnit().getDIEIndex(Die);
2759   CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
2760   bool AlreadyKept = MyInfo.Keep;
2761   if (MyInfo.Prune)
2762     return true;
2763
2764   // If the Keep flag is set, we are marking a required DIE's
2765   // dependencies. If our target is already marked as kept, we're all
2766   // set.
2767   if ((Flags & TF_DependencyWalk) && AlreadyKept)
2768     return MyInfo.Incomplete;
2769
2770   // We must not call shouldKeepDIE while called from keepDIEAndDependencies,
2771   // because it would screw up the relocation finding logic.
2772   if (!(Flags & TF_DependencyWalk))
2773     Flags = shouldKeepDIE(RelocMgr, Ranges, Die, DMO, CU, MyInfo, Flags);
2774
2775   // If it is a newly kept DIE mark it as well as all its dependencies as kept.
2776   if (!AlreadyKept && (Flags & TF_Keep)) {
2777     bool UseOdr = (Flags & TF_DependencyWalk) ? (Flags & TF_ODR) : CU.hasODR();
2778     keepDIEAndDependencies(RelocMgr, Ranges, Units, Die, MyInfo, DMO, CU,
2779                            UseOdr);
2780   }
2781   // The TF_ParentWalk flag tells us that we are currently walking up
2782   // the parent chain of a required DIE, and we don't want to mark all
2783   // the children of the parents as kept (consider for example a
2784   // DW_TAG_namespace node in the parent chain). There are however a
2785   // set of DIE types for which we want to ignore that directive and still
2786   // walk their children.
2787   if (dieNeedsChildrenToBeMeaningful(Die.getTag()))
2788     Flags &= ~TF_ParentWalk;
2789
2790   if (!Die.hasChildren() || (Flags & TF_ParentWalk))
2791     return MyInfo.Incomplete;
2792
2793   bool Incomplete = false;
2794   for (auto Child : Die.children()) {
2795     Incomplete |=
2796         lookForDIEsToKeep(RelocMgr, Ranges, Units, Child, DMO, CU, Flags);
2797
2798     // If any of the members are incomplete we propagate the incompleteness.
2799     if (!MyInfo.Incomplete && Incomplete &&
2800         (Die.getTag() == dwarf::DW_TAG_structure_type ||
2801          Die.getTag() == dwarf::DW_TAG_class_type))
2802       MyInfo.Incomplete = true;
2803   }
2804   return MyInfo.Incomplete;
2805 }
2806
2807 /// Assign an abbreviation number to \p Abbrev.
2808 ///
2809 /// Our DIEs get freed after every DebugMapObject has been processed,
2810 /// thus the FoldingSet we use to unique DIEAbbrevs cannot refer to
2811 /// the instances hold by the DIEs. When we encounter an abbreviation
2812 /// that we don't know, we create a permanent copy of it.
2813 void DwarfLinker::AssignAbbrev(DIEAbbrev &Abbrev) {
2814   // Check the set for priors.
2815   FoldingSetNodeID ID;
2816   Abbrev.Profile(ID);
2817   void *InsertToken;
2818   DIEAbbrev *InSet = AbbreviationsSet.FindNodeOrInsertPos(ID, InsertToken);
2819
2820   // If it's newly added.
2821   if (InSet) {
2822     // Assign existing abbreviation number.
2823     Abbrev.setNumber(InSet->getNumber());
2824   } else {
2825     // Add to abbreviation list.
2826     Abbreviations.push_back(
2827         llvm::make_unique<DIEAbbrev>(Abbrev.getTag(), Abbrev.hasChildren()));
2828     for (const auto &Attr : Abbrev.getData())
2829       Abbreviations.back()->AddAttribute(Attr.getAttribute(), Attr.getForm());
2830     AbbreviationsSet.InsertNode(Abbreviations.back().get(), InsertToken);
2831     // Assign the unique abbreviation number.
2832     Abbrev.setNumber(Abbreviations.size());
2833     Abbreviations.back()->setNumber(Abbreviations.size());
2834   }
2835 }
2836
2837 unsigned DwarfLinker::DIECloner::cloneStringAttribute(
2838     DIE &Die, AttributeSpec AttrSpec, const DWARFFormValue &Val,
2839     const DWARFUnit &U, OffsetsStringPool &StringPool, AttributesInfo &Info) {
2840   // Switch everything to out of line strings.
2841   const char *String = *Val.getAsCString();
2842   auto StringEntry = StringPool.getEntry(String);
2843
2844   // Update attributes info.
2845   if (AttrSpec.Attr == dwarf::DW_AT_name)
2846     Info.Name = StringEntry;
2847   else if (AttrSpec.Attr == dwarf::DW_AT_MIPS_linkage_name ||
2848            AttrSpec.Attr == dwarf::DW_AT_linkage_name)
2849     Info.MangledName = StringEntry;
2850
2851   Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), dwarf::DW_FORM_strp,
2852                DIEInteger(StringEntry.getOffset()));
2853
2854   return 4;
2855 }
2856
2857 unsigned DwarfLinker::DIECloner::cloneDieReferenceAttribute(
2858     DIE &Die, const DWARFDie &InputDIE, AttributeSpec AttrSpec,
2859     unsigned AttrSize, const DWARFFormValue &Val, const DebugMapObject &DMO,
2860     CompileUnit &Unit) {
2861   const DWARFUnit &U = Unit.getOrigUnit();
2862   uint32_t Ref = *Val.getAsReference();
2863   DIE *NewRefDie = nullptr;
2864   CompileUnit *RefUnit = nullptr;
2865   DeclContext *Ctxt = nullptr;
2866
2867   DWARFDie RefDie =
2868       resolveDIEReference(Linker, DMO, CompileUnits, Val, U, InputDIE, RefUnit);
2869
2870   // If the referenced DIE is not found,  drop the attribute.
2871   if (!RefDie || AttrSpec.Attr == dwarf::DW_AT_sibling)
2872     return 0;
2873
2874   unsigned Idx = RefUnit->getOrigUnit().getDIEIndex(RefDie);
2875   CompileUnit::DIEInfo &RefInfo = RefUnit->getInfo(Idx);
2876
2877   // If we already have emitted an equivalent DeclContext, just point
2878   // at it.
2879   if (isODRAttribute(AttrSpec.Attr)) {
2880     Ctxt = RefInfo.Ctxt;
2881     if (Ctxt && Ctxt->getCanonicalDIEOffset()) {
2882       DIEInteger Attr(Ctxt->getCanonicalDIEOffset());
2883       Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
2884                    dwarf::DW_FORM_ref_addr, Attr);
2885       return U.getRefAddrByteSize();
2886     }
2887   }
2888
2889   if (!RefInfo.Clone) {
2890     assert(Ref > InputDIE.getOffset());
2891     // We haven't cloned this DIE yet. Just create an empty one and
2892     // store it. It'll get really cloned when we process it.
2893     RefInfo.Clone = DIE::get(DIEAlloc, dwarf::Tag(RefDie.getTag()));
2894   }
2895   NewRefDie = RefInfo.Clone;
2896
2897   if (AttrSpec.Form == dwarf::DW_FORM_ref_addr ||
2898       (Unit.hasODR() && isODRAttribute(AttrSpec.Attr))) {
2899     // We cannot currently rely on a DIEEntry to emit ref_addr
2900     // references, because the implementation calls back to DwarfDebug
2901     // to find the unit offset. (We don't have a DwarfDebug)
2902     // FIXME: we should be able to design DIEEntry reliance on
2903     // DwarfDebug away.
2904     uint64_t Attr;
2905     if (Ref < InputDIE.getOffset()) {
2906       // We must have already cloned that DIE.
2907       uint32_t NewRefOffset =
2908           RefUnit->getStartOffset() + NewRefDie->getOffset();
2909       Attr = NewRefOffset;
2910       Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
2911                    dwarf::DW_FORM_ref_addr, DIEInteger(Attr));
2912     } else {
2913       // A forward reference. Note and fixup later.
2914       Attr = 0xBADDEF;
2915       Unit.noteForwardReference(
2916           NewRefDie, RefUnit, Ctxt,
2917           Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
2918                        dwarf::DW_FORM_ref_addr, DIEInteger(Attr)));
2919     }
2920     return U.getRefAddrByteSize();
2921   }
2922
2923   Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
2924                dwarf::Form(AttrSpec.Form), DIEEntry(*NewRefDie));
2925   return AttrSize;
2926 }
2927
2928 unsigned DwarfLinker::DIECloner::cloneBlockAttribute(DIE &Die,
2929                                                      AttributeSpec AttrSpec,
2930                                                      const DWARFFormValue &Val,
2931                                                      unsigned AttrSize) {
2932   DIEValueList *Attr;
2933   DIEValue Value;
2934   DIELoc *Loc = nullptr;
2935   DIEBlock *Block = nullptr;
2936   // Just copy the block data over.
2937   if (AttrSpec.Form == dwarf::DW_FORM_exprloc) {
2938     Loc = new (DIEAlloc) DIELoc;
2939     Linker.DIELocs.push_back(Loc);
2940   } else {
2941     Block = new (DIEAlloc) DIEBlock;
2942     Linker.DIEBlocks.push_back(Block);
2943   }
2944   Attr = Loc ? static_cast<DIEValueList *>(Loc)
2945              : static_cast<DIEValueList *>(Block);
2946
2947   if (Loc)
2948     Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
2949                      dwarf::Form(AttrSpec.Form), Loc);
2950   else
2951     Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
2952                      dwarf::Form(AttrSpec.Form), Block);
2953   ArrayRef<uint8_t> Bytes = *Val.getAsBlock();
2954   for (auto Byte : Bytes)
2955     Attr->addValue(DIEAlloc, static_cast<dwarf::Attribute>(0),
2956                    dwarf::DW_FORM_data1, DIEInteger(Byte));
2957   // FIXME: If DIEBlock and DIELoc just reuses the Size field of
2958   // the DIE class, this if could be replaced by
2959   // Attr->setSize(Bytes.size()).
2960   if (Linker.Streamer) {
2961     auto *AsmPrinter = &Linker.Streamer->getAsmPrinter();
2962     if (Loc)
2963       Loc->ComputeSize(AsmPrinter);
2964     else
2965       Block->ComputeSize(AsmPrinter);
2966   }
2967   Die.addValue(DIEAlloc, Value);
2968   return AttrSize;
2969 }
2970
2971 unsigned DwarfLinker::DIECloner::cloneAddressAttribute(
2972     DIE &Die, AttributeSpec AttrSpec, const DWARFFormValue &Val,
2973     const CompileUnit &Unit, AttributesInfo &Info) {
2974   uint64_t Addr = *Val.getAsAddress();
2975
2976   if (LLVM_UNLIKELY(Linker.Options.Update)) {
2977     if (AttrSpec.Attr == dwarf::DW_AT_low_pc)
2978       Info.HasLowPc = true;
2979     Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
2980                  dwarf::Form(AttrSpec.Form), DIEInteger(Addr));
2981     return Unit.getOrigUnit().getAddressByteSize();
2982   }
2983
2984   if (AttrSpec.Attr == dwarf::DW_AT_low_pc) {
2985     if (Die.getTag() == dwarf::DW_TAG_inlined_subroutine ||
2986         Die.getTag() == dwarf::DW_TAG_lexical_block)
2987       // The low_pc of a block or inline subroutine might get
2988       // relocated because it happens to match the low_pc of the
2989       // enclosing subprogram. To prevent issues with that, always use
2990       // the low_pc from the input DIE if relocations have been applied.
2991       Addr = (Info.OrigLowPc != std::numeric_limits<uint64_t>::max()
2992                   ? Info.OrigLowPc
2993                   : Addr) +
2994              Info.PCOffset;
2995     else if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
2996       Addr = Unit.getLowPc();
2997       if (Addr == std::numeric_limits<uint64_t>::max())
2998         return 0;
2999     }
3000     Info.HasLowPc = true;
3001   } else if (AttrSpec.Attr == dwarf::DW_AT_high_pc) {
3002     if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
3003       if (uint64_t HighPc = Unit.getHighPc())
3004         Addr = HighPc;
3005       else
3006         return 0;
3007     } else
3008       // If we have a high_pc recorded for the input DIE, use
3009       // it. Otherwise (when no relocations where applied) just use the
3010       // one we just decoded.
3011       Addr = (Info.OrigHighPc ? Info.OrigHighPc : Addr) + Info.PCOffset;
3012   }
3013
3014   Die.addValue(DIEAlloc, static_cast<dwarf::Attribute>(AttrSpec.Attr),
3015                static_cast<dwarf::Form>(AttrSpec.Form), DIEInteger(Addr));
3016   return Unit.getOrigUnit().getAddressByteSize();
3017 }
3018
3019 unsigned DwarfLinker::DIECloner::cloneScalarAttribute(
3020     DIE &Die, const DWARFDie &InputDIE, const DebugMapObject &DMO,
3021     CompileUnit &Unit, AttributeSpec AttrSpec, const DWARFFormValue &Val,
3022     unsigned AttrSize, AttributesInfo &Info) {
3023   uint64_t Value;
3024
3025   if (LLVM_UNLIKELY(Linker.Options.Update)) {
3026     if (auto OptionalValue = Val.getAsUnsignedConstant())
3027       Value = *OptionalValue;
3028     else if (auto OptionalValue = Val.getAsSignedConstant())
3029       Value = *OptionalValue;
3030     else if (auto OptionalValue = Val.getAsSectionOffset())
3031       Value = *OptionalValue;
3032     else {
3033       Linker.reportWarning(
3034           "Unsupported scalar attribute form. Dropping attribute.", DMO,
3035           &InputDIE);
3036       return 0;
3037     }
3038     if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
3039       Info.IsDeclaration = true;
3040     Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
3041                  dwarf::Form(AttrSpec.Form), DIEInteger(Value));
3042     return AttrSize;
3043   }
3044
3045   if (AttrSpec.Attr == dwarf::DW_AT_high_pc &&
3046       Die.getTag() == dwarf::DW_TAG_compile_unit) {
3047     if (Unit.getLowPc() == -1ULL)
3048       return 0;
3049     // Dwarf >= 4 high_pc is an size, not an address.
3050     Value = Unit.getHighPc() - Unit.getLowPc();
3051   } else if (AttrSpec.Form == dwarf::DW_FORM_sec_offset)
3052     Value = *Val.getAsSectionOffset();
3053   else if (AttrSpec.Form == dwarf::DW_FORM_sdata)
3054     Value = *Val.getAsSignedConstant();
3055   else if (auto OptionalValue = Val.getAsUnsignedConstant())
3056     Value = *OptionalValue;
3057   else {
3058     Linker.reportWarning(
3059         "Unsupported scalar attribute form. Dropping attribute.", DMO,
3060         &InputDIE);
3061     return 0;
3062   }
3063   PatchLocation Patch =
3064       Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
3065                    dwarf::Form(AttrSpec.Form), DIEInteger(Value));
3066   if (AttrSpec.Attr == dwarf::DW_AT_ranges) {
3067     Unit.noteRangeAttribute(Die, Patch);
3068     Info.HasRanges = true;
3069   }
3070
3071   // A more generic way to check for location attributes would be
3072   // nice, but it's very unlikely that any other attribute needs a
3073   // location list.
3074   else if (AttrSpec.Attr == dwarf::DW_AT_location ||
3075            AttrSpec.Attr == dwarf::DW_AT_frame_base)
3076     Unit.noteLocationAttribute(Patch, Info.PCOffset);
3077   else if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
3078     Info.IsDeclaration = true;
3079
3080   return AttrSize;
3081 }
3082
3083 /// Clone \p InputDIE's attribute described by \p AttrSpec with
3084 /// value \p Val, and add it to \p Die.
3085 /// \returns the size of the cloned attribute.
3086 unsigned DwarfLinker::DIECloner::cloneAttribute(
3087     DIE &Die, const DWARFDie &InputDIE, const DebugMapObject &DMO,
3088     CompileUnit &Unit, OffsetsStringPool &StringPool, const DWARFFormValue &Val,
3089     const AttributeSpec AttrSpec, unsigned AttrSize, AttributesInfo &Info) {
3090   const DWARFUnit &U = Unit.getOrigUnit();
3091
3092   switch (AttrSpec.Form) {
3093   case dwarf::DW_FORM_strp:
3094   case dwarf::DW_FORM_string:
3095     return cloneStringAttribute(Die, AttrSpec, Val, U, StringPool, Info);
3096   case dwarf::DW_FORM_ref_addr:
3097   case dwarf::DW_FORM_ref1:
3098   case dwarf::DW_FORM_ref2:
3099   case dwarf::DW_FORM_ref4:
3100   case dwarf::DW_FORM_ref8:
3101     return cloneDieReferenceAttribute(Die, InputDIE, AttrSpec, AttrSize, Val,
3102                                       DMO, Unit);
3103   case dwarf::DW_FORM_block:
3104   case dwarf::DW_FORM_block1:
3105   case dwarf::DW_FORM_block2:
3106   case dwarf::DW_FORM_block4:
3107   case dwarf::DW_FORM_exprloc:
3108     return cloneBlockAttribute(Die, AttrSpec, Val, AttrSize);
3109   case dwarf::DW_FORM_addr:
3110     return cloneAddressAttribute(Die, AttrSpec, Val, Unit, Info);
3111   case dwarf::DW_FORM_data1:
3112   case dwarf::DW_FORM_data2:
3113   case dwarf::DW_FORM_data4:
3114   case dwarf::DW_FORM_data8:
3115   case dwarf::DW_FORM_udata:
3116   case dwarf::DW_FORM_sdata:
3117   case dwarf::DW_FORM_sec_offset:
3118   case dwarf::DW_FORM_flag:
3119   case dwarf::DW_FORM_flag_present:
3120     return cloneScalarAttribute(Die, InputDIE, DMO, Unit, AttrSpec, Val,
3121                                 AttrSize, Info);
3122   default:
3123     Linker.reportWarning(
3124         "Unsupported attribute form in cloneAttribute. Dropping.", DMO,
3125         &InputDIE);
3126   }
3127
3128   return 0;
3129 }
3130
3131 /// Apply the valid relocations found by findValidRelocs() to
3132 /// the buffer \p Data, taking into account that Data is at \p BaseOffset
3133 /// in the debug_info section.
3134 ///
3135 /// Like for findValidRelocs(), this function must be called with
3136 /// monotonic \p BaseOffset values.
3137 ///
3138 /// \returns whether any reloc has been applied.
3139 bool DwarfLinker::RelocationManager::applyValidRelocs(
3140     MutableArrayRef<char> Data, uint32_t BaseOffset, bool isLittleEndian) {
3141   assert((NextValidReloc == 0 ||
3142           BaseOffset > ValidRelocs[NextValidReloc - 1].Offset) &&
3143          "BaseOffset should only be increasing.");
3144   if (NextValidReloc >= ValidRelocs.size())
3145     return false;
3146
3147   // Skip relocs that haven't been applied.
3148   while (NextValidReloc < ValidRelocs.size() &&
3149          ValidRelocs[NextValidReloc].Offset < BaseOffset)
3150     ++NextValidReloc;
3151
3152   bool Applied = false;
3153   uint64_t EndOffset = BaseOffset + Data.size();
3154   while (NextValidReloc < ValidRelocs.size() &&
3155          ValidRelocs[NextValidReloc].Offset >= BaseOffset &&
3156          ValidRelocs[NextValidReloc].Offset < EndOffset) {
3157     const auto &ValidReloc = ValidRelocs[NextValidReloc++];
3158     assert(ValidReloc.Offset - BaseOffset < Data.size());
3159     assert(ValidReloc.Offset - BaseOffset + ValidReloc.Size <= Data.size());
3160     char Buf[8];
3161     uint64_t Value = ValidReloc.Mapping->getValue().BinaryAddress;
3162     Value += ValidReloc.Addend;
3163     for (unsigned i = 0; i != ValidReloc.Size; ++i) {
3164       unsigned Index = isLittleEndian ? i : (ValidReloc.Size - i - 1);
3165       Buf[i] = uint8_t(Value >> (Index * 8));
3166     }
3167     assert(ValidReloc.Size <= sizeof(Buf));
3168     memcpy(&Data[ValidReloc.Offset - BaseOffset], Buf, ValidReloc.Size);
3169     Applied = true;
3170   }
3171
3172   return Applied;
3173 }
3174
3175 static bool isTypeTag(uint16_t Tag) {
3176   switch (Tag) {
3177   case dwarf::DW_TAG_array_type:
3178   case dwarf::DW_TAG_class_type:
3179   case dwarf::DW_TAG_enumeration_type:
3180   case dwarf::DW_TAG_pointer_type:
3181   case dwarf::DW_TAG_reference_type:
3182   case dwarf::DW_TAG_string_type:
3183   case dwarf::DW_TAG_structure_type:
3184   case dwarf::DW_TAG_subroutine_type:
3185   case dwarf::DW_TAG_typedef:
3186   case dwarf::DW_TAG_union_type:
3187   case dwarf::DW_TAG_ptr_to_member_type:
3188   case dwarf::DW_TAG_set_type:
3189   case dwarf::DW_TAG_subrange_type:
3190   case dwarf::DW_TAG_base_type:
3191   case dwarf::DW_TAG_const_type:
3192   case dwarf::DW_TAG_constant:
3193   case dwarf::DW_TAG_file_type:
3194   case dwarf::DW_TAG_namelist:
3195   case dwarf::DW_TAG_packed_type:
3196   case dwarf::DW_TAG_volatile_type:
3197   case dwarf::DW_TAG_restrict_type:
3198   case dwarf::DW_TAG_atomic_type:
3199   case dwarf::DW_TAG_interface_type:
3200   case dwarf::DW_TAG_unspecified_type:
3201   case dwarf::DW_TAG_shared_type:
3202     return true;
3203   default:
3204     break;
3205   }
3206   return false;
3207 }
3208
3209 static bool isObjCSelector(StringRef Name) {
3210   return Name.size() > 2 && (Name[0] == '-' || Name[0] == '+') &&
3211          (Name[1] == '[');
3212 }
3213
3214 void DwarfLinker::DIECloner::addObjCAccelerator(CompileUnit &Unit,
3215                                                 const DIE *Die,
3216                                                 DwarfStringPoolEntryRef Name,
3217                                                 OffsetsStringPool &StringPool,
3218                                                 bool SkipPubSection) {
3219   assert(isObjCSelector(Name.getString()) && "not an objc selector");
3220   // Objective C method or class function.
3221   // "- [Class(Category) selector :withArg ...]"
3222   StringRef ClassNameStart(Name.getString().drop_front(2));
3223   size_t FirstSpace = ClassNameStart.find(' ');
3224   if (FirstSpace == StringRef::npos)
3225     return;
3226
3227   StringRef SelectorStart(ClassNameStart.data() + FirstSpace + 1);
3228   if (!SelectorStart.size())
3229     return;
3230
3231   StringRef Selector(SelectorStart.data(), SelectorStart.size() - 1);
3232   Unit.addNameAccelerator(Die, StringPool.getEntry(Selector), SkipPubSection);
3233
3234   // Add an entry for the class name that points to this
3235   // method/class function.
3236   StringRef ClassName(ClassNameStart.data(), FirstSpace);
3237   Unit.addObjCAccelerator(Die, StringPool.getEntry(ClassName), SkipPubSection);
3238
3239   if (ClassName[ClassName.size() - 1] == ')') {
3240     size_t OpenParens = ClassName.find('(');
3241     if (OpenParens != StringRef::npos) {
3242       StringRef ClassNameNoCategory(ClassName.data(), OpenParens);
3243       Unit.addObjCAccelerator(Die, StringPool.getEntry(ClassNameNoCategory),
3244                               SkipPubSection);
3245
3246       std::string MethodNameNoCategory(Name.getString().data(), OpenParens + 2);
3247       // FIXME: The missing space here may be a bug, but
3248       //        dsymutil-classic also does it this way.
3249       MethodNameNoCategory.append(SelectorStart);
3250       Unit.addNameAccelerator(Die, StringPool.getEntry(MethodNameNoCategory),
3251                               SkipPubSection);
3252     }
3253   }
3254 }
3255
3256 static bool
3257 shouldSkipAttribute(DWARFAbbreviationDeclaration::AttributeSpec AttrSpec,
3258                     uint16_t Tag, bool InDebugMap, bool SkipPC,
3259                     bool InFunctionScope) {
3260   switch (AttrSpec.Attr) {
3261   default:
3262     return false;
3263   case dwarf::DW_AT_low_pc:
3264   case dwarf::DW_AT_high_pc:
3265   case dwarf::DW_AT_ranges:
3266     return SkipPC;
3267   case dwarf::DW_AT_location:
3268   case dwarf::DW_AT_frame_base:
3269     // FIXME: for some reason dsymutil-classic keeps the location attributes
3270     // when they are of block type (i.e. not location lists). This is totally
3271     // wrong for globals where we will keep a wrong address. It is mostly
3272     // harmless for locals, but there is no point in keeping these anyway when
3273     // the function wasn't linked.
3274     return (SkipPC || (!InFunctionScope && Tag == dwarf::DW_TAG_variable &&
3275                        !InDebugMap)) &&
3276            !DWARFFormValue(AttrSpec.Form).isFormClass(DWARFFormValue::FC_Block);
3277   }
3278 }
3279
3280 DIE *DwarfLinker::DIECloner::cloneDIE(const DWARFDie &InputDIE,
3281                                       const DebugMapObject &DMO,
3282                                       CompileUnit &Unit,
3283                                       OffsetsStringPool &StringPool,
3284                                       int64_t PCOffset, uint32_t OutOffset,
3285                                       unsigned Flags, DIE *Die) {
3286   DWARFUnit &U = Unit.getOrigUnit();
3287   unsigned Idx = U.getDIEIndex(InputDIE);
3288   CompileUnit::DIEInfo &Info = Unit.getInfo(Idx);
3289
3290   // Should the DIE appear in the output?
3291   if (!Unit.getInfo(Idx).Keep)
3292     return nullptr;
3293
3294   uint32_t Offset = InputDIE.getOffset();
3295   assert(!(Die && Info.Clone) && "Can't supply a DIE and a cloned DIE");
3296   if (!Die) {
3297     // The DIE might have been already created by a forward reference
3298     // (see cloneDieReferenceAttribute()).
3299     if (!Info.Clone)
3300       Info.Clone = DIE::get(DIEAlloc, dwarf::Tag(InputDIE.getTag()));
3301     Die = Info.Clone;
3302   }
3303
3304   assert(Die->getTag() == InputDIE.getTag());
3305   Die->setOffset(OutOffset);
3306   if ((Unit.hasODR() || Unit.isClangModule()) && !Info.Incomplete &&
3307       Die->getTag() != dwarf::DW_TAG_namespace && Info.Ctxt &&
3308       Info.Ctxt != Unit.getInfo(Info.ParentIdx).Ctxt &&
3309       !Info.Ctxt->getCanonicalDIEOffset()) {
3310     // We are about to emit a DIE that is the root of its own valid
3311     // DeclContext tree. Make the current offset the canonical offset
3312     // for this context.
3313     Info.Ctxt->setCanonicalDIEOffset(OutOffset + Unit.getStartOffset());
3314   }
3315
3316   // Extract and clone every attribute.
3317   DWARFDataExtractor Data = U.getDebugInfoExtractor();
3318   // Point to the next DIE (generally there is always at least a NULL
3319   // entry after the current one). If this is a lone
3320   // DW_TAG_compile_unit without any children, point to the next unit.
3321   uint32_t NextOffset = (Idx + 1 < U.getNumDIEs())
3322                             ? U.getDIEAtIndex(Idx + 1).getOffset()
3323                             : U.getNextUnitOffset();
3324   AttributesInfo AttrInfo;
3325
3326   // We could copy the data only if we need to apply a relocation to it. After
3327   // testing, it seems there is no performance downside to doing the copy
3328   // unconditionally, and it makes the code simpler.
3329   SmallString<40> DIECopy(Data.getData().substr(Offset, NextOffset - Offset));
3330   Data =
3331       DWARFDataExtractor(DIECopy, Data.isLittleEndian(), Data.getAddressSize());
3332   // Modify the copy with relocated addresses.
3333   if (RelocMgr.applyValidRelocs(DIECopy, Offset, Data.isLittleEndian())) {
3334     // If we applied relocations, we store the value of high_pc that was
3335     // potentially stored in the input DIE. If high_pc is an address
3336     // (Dwarf version == 2), then it might have been relocated to a
3337     // totally unrelated value (because the end address in the object
3338     // file might be start address of another function which got moved
3339     // independently by the linker). The computation of the actual
3340     // high_pc value is done in cloneAddressAttribute().
3341     AttrInfo.OrigHighPc =
3342         dwarf::toAddress(InputDIE.find(dwarf::DW_AT_high_pc), 0);
3343     // Also store the low_pc. It might get relocated in an
3344     // inline_subprogram that happens at the beginning of its
3345     // inlining function.
3346     AttrInfo.OrigLowPc = dwarf::toAddress(InputDIE.find(dwarf::DW_AT_low_pc),
3347                                           std::numeric_limits<uint64_t>::max());
3348   }
3349
3350   // Reset the Offset to 0 as we will be working on the local copy of
3351   // the data.
3352   Offset = 0;
3353
3354   const auto *Abbrev = InputDIE.getAbbreviationDeclarationPtr();
3355   Offset += getULEB128Size(Abbrev->getCode());
3356
3357   // We are entering a subprogram. Get and propagate the PCOffset.
3358   if (Die->getTag() == dwarf::DW_TAG_subprogram)
3359     PCOffset = Info.AddrAdjust;
3360   AttrInfo.PCOffset = PCOffset;
3361
3362   if (Abbrev->getTag() == dwarf::DW_TAG_subprogram) {
3363     Flags |= TF_InFunctionScope;
3364     if (!Info.InDebugMap && LLVM_LIKELY(!Options.Update))
3365       Flags |= TF_SkipPC;
3366   }
3367
3368   bool Copied = false;
3369   for (const auto &AttrSpec : Abbrev->attributes()) {
3370     if (LLVM_LIKELY(!Options.Update) &&
3371         shouldSkipAttribute(AttrSpec, Die->getTag(), Info.InDebugMap,
3372                             Flags & TF_SkipPC, Flags & TF_InFunctionScope)) {
3373       DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset,
3374                                 U.getFormParams());
3375       // FIXME: dsymutil-classic keeps the old abbreviation around
3376       // even if it's not used. We can remove this (and the copyAbbrev
3377       // helper) as soon as bit-for-bit compatibility is not a goal anymore.
3378       if (!Copied) {
3379         copyAbbrev(*InputDIE.getAbbreviationDeclarationPtr(), Unit.hasODR());
3380         Copied = true;
3381       }
3382       continue;
3383     }
3384
3385     DWARFFormValue Val(AttrSpec.Form);
3386     uint32_t AttrSize = Offset;
3387     Val.extractValue(Data, &Offset, U.getFormParams(), &U);
3388     AttrSize = Offset - AttrSize;
3389
3390     OutOffset += cloneAttribute(*Die, InputDIE, DMO, Unit, StringPool, Val,
3391                                 AttrSpec, AttrSize, AttrInfo);
3392   }
3393
3394   // Look for accelerator entries.
3395   uint16_t Tag = InputDIE.getTag();
3396   // FIXME: This is slightly wrong. An inline_subroutine without a
3397   // low_pc, but with AT_ranges might be interesting to get into the
3398   // accelerator tables too. For now stick with dsymutil's behavior.
3399   if ((Info.InDebugMap || AttrInfo.HasLowPc || AttrInfo.HasRanges) &&
3400       Tag != dwarf::DW_TAG_compile_unit &&
3401       getDIENames(InputDIE, AttrInfo, StringPool,
3402                   Tag != dwarf::DW_TAG_inlined_subroutine)) {
3403     if (AttrInfo.MangledName && AttrInfo.MangledName != AttrInfo.Name)
3404       Unit.addNameAccelerator(Die, AttrInfo.MangledName,
3405                               Tag == dwarf::DW_TAG_inlined_subroutine);
3406     if (AttrInfo.Name) {
3407       if (AttrInfo.NameWithoutTemplate)
3408         Unit.addNameAccelerator(Die, AttrInfo.NameWithoutTemplate,
3409                                 /* SkipPubSection */ true);
3410       Unit.addNameAccelerator(Die, AttrInfo.Name,
3411                               Tag == dwarf::DW_TAG_inlined_subroutine);
3412     }
3413     if (AttrInfo.Name && isObjCSelector(AttrInfo.Name.getString()))
3414       addObjCAccelerator(Unit, Die, AttrInfo.Name, StringPool,
3415                          /* SkipPubSection =*/true);
3416
3417   } else if (Tag == dwarf::DW_TAG_namespace) {
3418     if (!AttrInfo.Name)
3419       AttrInfo.Name = StringPool.getEntry("(anonymous namespace)");
3420     Unit.addNamespaceAccelerator(Die, AttrInfo.Name);
3421   } else if (isTypeTag(Tag) && !AttrInfo.IsDeclaration &&
3422              getDIENames(InputDIE, AttrInfo, StringPool) && AttrInfo.Name &&
3423              AttrInfo.Name.getString()[0]) {
3424     uint32_t Hash = hashFullyQualifiedName(InputDIE, Unit, DMO);
3425     uint64_t RuntimeLang =
3426         dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_runtime_class))
3427             .getValueOr(0);
3428     bool ObjCClassIsImplementation =
3429         (RuntimeLang == dwarf::DW_LANG_ObjC ||
3430          RuntimeLang == dwarf::DW_LANG_ObjC_plus_plus) &&
3431         dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_objc_complete_type))
3432             .getValueOr(0);
3433     Unit.addTypeAccelerator(Die, AttrInfo.Name, ObjCClassIsImplementation,
3434                             Hash);
3435   }
3436
3437   // Determine whether there are any children that we want to keep.
3438   bool HasChildren = false;
3439   for (auto Child : InputDIE.children()) {
3440     unsigned Idx = U.getDIEIndex(Child);
3441     if (Unit.getInfo(Idx).Keep) {
3442       HasChildren = true;
3443       break;
3444     }
3445   }
3446
3447   DIEAbbrev NewAbbrev = Die->generateAbbrev();
3448   if (HasChildren)
3449     NewAbbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
3450   // Assign a permanent abbrev number
3451   Linker.AssignAbbrev(NewAbbrev);
3452   Die->setAbbrevNumber(NewAbbrev.getNumber());
3453
3454   // Add the size of the abbreviation number to the output offset.
3455   OutOffset += getULEB128Size(Die->getAbbrevNumber());
3456
3457   if (!HasChildren) {
3458     // Update our size.
3459     Die->setSize(OutOffset - Die->getOffset());
3460     return Die;
3461   }
3462
3463   // Recursively clone children.
3464   for (auto Child : InputDIE.children()) {
3465     if (DIE *Clone = cloneDIE(Child, DMO, Unit, StringPool, PCOffset, OutOffset,
3466                               Flags)) {
3467       Die->addChild(Clone);
3468       OutOffset = Clone->getOffset() + Clone->getSize();
3469     }
3470   }
3471
3472   // Account for the end of children marker.
3473   OutOffset += sizeof(int8_t);
3474   // Update our size.
3475   Die->setSize(OutOffset - Die->getOffset());
3476   return Die;
3477 }
3478
3479 /// Patch the input object file relevant debug_ranges entries
3480 /// and emit them in the output file. Update the relevant attributes
3481 /// to point at the new entries.
3482 void DwarfLinker::patchRangesForUnit(const CompileUnit &Unit,
3483                                      DWARFContext &OrigDwarf,
3484                                      const DebugMapObject &DMO) const {
3485   DWARFDebugRangeList RangeList;
3486   const auto &FunctionRanges = Unit.getFunctionRanges();
3487   unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
3488   DWARFDataExtractor RangeExtractor(OrigDwarf.getDWARFObj(),
3489                                     OrigDwarf.getDWARFObj().getRangeSection(),
3490                                     OrigDwarf.isLittleEndian(), AddressSize);
3491   auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
3492   DWARFUnit &OrigUnit = Unit.getOrigUnit();
3493   auto OrigUnitDie = OrigUnit.getUnitDIE(false);
3494   uint64_t OrigLowPc =
3495       dwarf::toAddress(OrigUnitDie.find(dwarf::DW_AT_low_pc), -1ULL);
3496   // Ranges addresses are based on the unit's low_pc. Compute the
3497   // offset we need to apply to adapt to the new unit's low_pc.
3498   int64_t UnitPcOffset = 0;
3499   if (OrigLowPc != -1ULL)
3500     UnitPcOffset = int64_t(OrigLowPc) - Unit.getLowPc();
3501
3502   for (const auto &RangeAttribute : Unit.getRangesAttributes()) {
3503     uint32_t Offset = RangeAttribute.get();
3504     RangeAttribute.set(Streamer->getRangesSectionSize());
3505     RangeList.extract(RangeExtractor, &Offset);
3506     const auto &Entries = RangeList.getEntries();
3507     if (!Entries.empty()) {
3508       const DWARFDebugRangeList::RangeListEntry &First = Entries.front();
3509
3510       if (CurrRange == InvalidRange ||
3511           First.StartAddress + OrigLowPc < CurrRange.start() ||
3512           First.StartAddress + OrigLowPc >= CurrRange.stop()) {
3513         CurrRange = FunctionRanges.find(First.StartAddress + OrigLowPc);
3514         if (CurrRange == InvalidRange ||
3515             CurrRange.start() > First.StartAddress + OrigLowPc) {
3516           reportWarning("no mapping for range.", DMO);
3517           continue;
3518         }
3519       }
3520     }
3521
3522     Streamer->emitRangesEntries(UnitPcOffset, OrigLowPc, CurrRange, Entries,
3523                                 AddressSize);
3524   }
3525 }
3526
3527 /// Generate the debug_aranges entries for \p Unit and if the
3528 /// unit has a DW_AT_ranges attribute, also emit the debug_ranges
3529 /// contribution for this attribute.
3530 /// FIXME: this could actually be done right in patchRangesForUnit,
3531 /// but for the sake of initial bit-for-bit compatibility with legacy
3532 /// dsymutil, we have to do it in a delayed pass.
3533 void DwarfLinker::generateUnitRanges(CompileUnit &Unit) const {
3534   auto Attr = Unit.getUnitRangesAttribute();
3535   if (Attr)
3536     Attr->set(Streamer->getRangesSectionSize());
3537   Streamer->emitUnitRangesEntries(Unit, static_cast<bool>(Attr));
3538 }
3539
3540 /// Insert the new line info sequence \p Seq into the current
3541 /// set of already linked line info \p Rows.
3542 static void insertLineSequence(std::vector<DWARFDebugLine::Row> &Seq,
3543                                std::vector<DWARFDebugLine::Row> &Rows) {
3544   if (Seq.empty())
3545     return;
3546
3547   if (!Rows.empty() && Rows.back().Address < Seq.front().Address) {
3548     Rows.insert(Rows.end(), Seq.begin(), Seq.end());
3549     Seq.clear();
3550     return;
3551   }
3552
3553   auto InsertPoint = std::lower_bound(
3554       Rows.begin(), Rows.end(), Seq.front(),
3555       [](const DWARFDebugLine::Row &LHS, const DWARFDebugLine::Row &RHS) {
3556         return LHS.Address < RHS.Address;
3557       });
3558
3559   // FIXME: this only removes the unneeded end_sequence if the
3560   // sequences have been inserted in order. Using a global sort like
3561   // described in patchLineTableForUnit() and delaying the end_sequene
3562   // elimination to emitLineTableForUnit() we can get rid of all of them.
3563   if (InsertPoint != Rows.end() &&
3564       InsertPoint->Address == Seq.front().Address && InsertPoint->EndSequence) {
3565     *InsertPoint = Seq.front();
3566     Rows.insert(InsertPoint + 1, Seq.begin() + 1, Seq.end());
3567   } else {
3568     Rows.insert(InsertPoint, Seq.begin(), Seq.end());
3569   }
3570
3571   Seq.clear();
3572 }
3573
3574 static void patchStmtList(DIE &Die, DIEInteger Offset) {
3575   for (auto &V : Die.values())
3576     if (V.getAttribute() == dwarf::DW_AT_stmt_list) {
3577       V = DIEValue(V.getAttribute(), V.getForm(), Offset);
3578       return;
3579     }
3580
3581   llvm_unreachable("Didn't find DW_AT_stmt_list in cloned DIE!");
3582 }
3583
3584 /// Extract the line table for \p Unit from \p OrigDwarf, and
3585 /// recreate a relocated version of these for the address ranges that
3586 /// are present in the binary.
3587 void DwarfLinker::patchLineTableForUnit(CompileUnit &Unit,
3588                                         DWARFContext &OrigDwarf,
3589                                         RangesTy &Ranges,
3590                                         const DebugMapObject &DMO) {
3591   DWARFDie CUDie = Unit.getOrigUnit().getUnitDIE();
3592   auto StmtList = dwarf::toSectionOffset(CUDie.find(dwarf::DW_AT_stmt_list));
3593   if (!StmtList)
3594     return;
3595
3596   // Update the cloned DW_AT_stmt_list with the correct debug_line offset.
3597   if (auto *OutputDIE = Unit.getOutputUnitDIE())
3598     patchStmtList(*OutputDIE, DIEInteger(Streamer->getLineSectionSize()));
3599
3600   // Parse the original line info for the unit.
3601   DWARFDebugLine::LineTable LineTable;
3602   uint32_t StmtOffset = *StmtList;
3603   DWARFDataExtractor LineExtractor(
3604       OrigDwarf.getDWARFObj(), OrigDwarf.getDWARFObj().getLineSection(),
3605       OrigDwarf.isLittleEndian(), Unit.getOrigUnit().getAddressByteSize());
3606   LineTable.parse(LineExtractor, &StmtOffset, OrigDwarf, &Unit.getOrigUnit());
3607
3608   // This vector is the output line table.
3609   std::vector<DWARFDebugLine::Row> NewRows;
3610   NewRows.reserve(LineTable.Rows.size());
3611
3612   // Current sequence of rows being extracted, before being inserted
3613   // in NewRows.
3614   std::vector<DWARFDebugLine::Row> Seq;
3615   const auto &FunctionRanges = Unit.getFunctionRanges();
3616   auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
3617
3618   // FIXME: This logic is meant to generate exactly the same output as
3619   // Darwin's classic dsymutil. There is a nicer way to implement this
3620   // by simply putting all the relocated line info in NewRows and simply
3621   // sorting NewRows before passing it to emitLineTableForUnit. This
3622   // should be correct as sequences for a function should stay
3623   // together in the sorted output. There are a few corner cases that
3624   // look suspicious though, and that required to implement the logic
3625   // this way. Revisit that once initial validation is finished.
3626
3627   // Iterate over the object file line info and extract the sequences
3628   // that correspond to linked functions.
3629   for (auto &Row : LineTable.Rows) {
3630     // Check whether we stepped out of the range. The range is
3631     // half-open, but consider accept the end address of the range if
3632     // it is marked as end_sequence in the input (because in that
3633     // case, the relocation offset is accurate and that entry won't
3634     // serve as the start of another function).
3635     if (CurrRange == InvalidRange || Row.Address < CurrRange.start() ||
3636         Row.Address > CurrRange.stop() ||
3637         (Row.Address == CurrRange.stop() && !Row.EndSequence)) {
3638       // We just stepped out of a known range. Insert a end_sequence
3639       // corresponding to the end of the range.
3640       uint64_t StopAddress = CurrRange != InvalidRange
3641                                  ? CurrRange.stop() + CurrRange.value()
3642                                  : -1ULL;
3643       CurrRange = FunctionRanges.find(Row.Address);
3644       bool CurrRangeValid =
3645           CurrRange != InvalidRange && CurrRange.start() <= Row.Address;
3646       if (!CurrRangeValid) {
3647         CurrRange = InvalidRange;
3648         if (StopAddress != -1ULL) {
3649           // Try harder by looking in the DebugMapObject function
3650           // ranges map. There are corner cases where this finds a
3651           // valid entry. It's unclear if this is right or wrong, but
3652           // for now do as dsymutil.
3653           // FIXME: Understand exactly what cases this addresses and
3654           // potentially remove it along with the Ranges map.
3655           auto Range = Ranges.lower_bound(Row.Address);
3656           if (Range != Ranges.begin() && Range != Ranges.end())
3657             --Range;
3658
3659           if (Range != Ranges.end() && Range->first <= Row.Address &&
3660               Range->second.HighPC >= Row.Address) {
3661             StopAddress = Row.Address + Range->second.Offset;
3662           }
3663         }
3664       }
3665       if (StopAddress != -1ULL && !Seq.empty()) {
3666         // Insert end sequence row with the computed end address, but
3667         // the same line as the previous one.
3668         auto NextLine = Seq.back();
3669         NextLine.Address = StopAddress;
3670         NextLine.EndSequence = 1;
3671         NextLine.PrologueEnd = 0;
3672         NextLine.BasicBlock = 0;
3673         NextLine.EpilogueBegin = 0;
3674         Seq.push_back(NextLine);
3675         insertLineSequence(Seq, NewRows);
3676       }
3677
3678       if (!CurrRangeValid)
3679         continue;
3680     }
3681
3682     // Ignore empty sequences.
3683     if (Row.EndSequence && Seq.empty())
3684       continue;
3685
3686     // Relocate row address and add it to the current sequence.
3687     Row.Address += CurrRange.value();
3688     Seq.emplace_back(Row);
3689
3690     if (Row.EndSequence)
3691       insertLineSequence(Seq, NewRows);
3692   }
3693
3694   // Finished extracting, now emit the line tables.
3695   // FIXME: LLVM hard-codes its prologue values. We just copy the
3696   // prologue over and that works because we act as both producer and
3697   // consumer. It would be nicer to have a real configurable line
3698   // table emitter.
3699   if (LineTable.Prologue.getVersion() < 2 ||
3700       LineTable.Prologue.getVersion() > 5 ||
3701       LineTable.Prologue.DefaultIsStmt != DWARF2_LINE_DEFAULT_IS_STMT ||
3702       LineTable.Prologue.OpcodeBase > 13)
3703     reportWarning("line table parameters mismatch. Cannot emit.", DMO);
3704   else {
3705     uint32_t PrologueEnd = *StmtList + 10 + LineTable.Prologue.PrologueLength;
3706     // DWARF v5 has an extra 2 bytes of information before the header_length
3707     // field.
3708     if (LineTable.Prologue.getVersion() == 5)
3709       PrologueEnd += 2;
3710     StringRef LineData = OrigDwarf.getDWARFObj().getLineSection().Data;
3711     MCDwarfLineTableParams Params;
3712     Params.DWARF2LineOpcodeBase = LineTable.Prologue.OpcodeBase;
3713     Params.DWARF2LineBase = LineTable.Prologue.LineBase;
3714     Params.DWARF2LineRange = LineTable.Prologue.LineRange;
3715     Streamer->emitLineTableForUnit(Params,
3716                                    LineData.slice(*StmtList + 4, PrologueEnd),
3717                                    LineTable.Prologue.MinInstLength, NewRows,
3718                                    Unit.getOrigUnit().getAddressByteSize());
3719   }
3720 }
3721
3722 void DwarfLinker::emitAcceleratorEntriesForUnit(CompileUnit &Unit) {
3723   // Add namespaces.
3724   for (const auto &Namespace : Unit.getNamespaces())
3725     AppleNamespaces.addName(Namespace.Name,
3726                             Namespace.Die->getOffset() + Unit.getStartOffset());
3727
3728   /// Add names.
3729   if (!Options.Minimize)
3730     Streamer->emitPubNamesForUnit(Unit);
3731   for (const auto &Pubname : Unit.getPubnames())
3732     AppleNames.addName(Pubname.Name,
3733                        Pubname.Die->getOffset() + Unit.getStartOffset());
3734
3735   /// Add types.
3736   if (!Options.Minimize)
3737     Streamer->emitPubTypesForUnit(Unit);
3738   for (const auto &Pubtype : Unit.getPubtypes())
3739     AppleTypes.addName(
3740         Pubtype.Name, Pubtype.Die->getOffset() + Unit.getStartOffset(),
3741         Pubtype.Die->getTag(),
3742         Pubtype.ObjcClassImplementation ? dwarf::DW_FLAG_type_implementation
3743                                         : 0,
3744         Pubtype.QualifiedNameHash);
3745
3746   /// Add ObjC names.
3747   for (const auto &ObjC : Unit.getObjC())
3748     AppleObjc.addName(ObjC.Name, ObjC.Die->getOffset() + Unit.getStartOffset());
3749 }
3750
3751 /// Read the frame info stored in the object, and emit the
3752 /// patched frame descriptions for the linked binary.
3753 ///
3754 /// This is actually pretty easy as the data of the CIEs and FDEs can
3755 /// be considered as black boxes and moved as is. The only thing to do
3756 /// is to patch the addresses in the headers.
3757 void DwarfLinker::patchFrameInfoForObject(const DebugMapObject &DMO,
3758                                           RangesTy &Ranges,
3759                                           DWARFContext &OrigDwarf,
3760                                           unsigned AddrSize) {
3761   StringRef FrameData = OrigDwarf.getDWARFObj().getDebugFrameSection();
3762   if (FrameData.empty())
3763     return;
3764
3765   DataExtractor Data(FrameData, OrigDwarf.isLittleEndian(), 0);
3766   uint32_t InputOffset = 0;
3767
3768   // Store the data of the CIEs defined in this object, keyed by their
3769   // offsets.
3770   DenseMap<uint32_t, StringRef> LocalCIES;
3771
3772   while (Data.isValidOffset(InputOffset)) {
3773     uint32_t EntryOffset = InputOffset;
3774     uint32_t InitialLength = Data.getU32(&InputOffset);
3775     if (InitialLength == 0xFFFFFFFF)
3776       return reportWarning("Dwarf64 bits no supported", DMO);
3777
3778     uint32_t CIEId = Data.getU32(&InputOffset);
3779     if (CIEId == 0xFFFFFFFF) {
3780       // This is a CIE, store it.
3781       StringRef CIEData = FrameData.substr(EntryOffset, InitialLength + 4);
3782       LocalCIES[EntryOffset] = CIEData;
3783       // The -4 is to account for the CIEId we just read.
3784       InputOffset += InitialLength - 4;
3785       continue;
3786     }
3787
3788     uint32_t Loc = Data.getUnsigned(&InputOffset, AddrSize);
3789
3790     // Some compilers seem to emit frame info that doesn't start at
3791     // the function entry point, thus we can't just lookup the address
3792     // in the debug map. Use the linker's range map to see if the FDE
3793     // describes something that we can relocate.
3794     auto Range = Ranges.upper_bound(Loc);
3795     if (Range != Ranges.begin())
3796       --Range;
3797     if (Range == Ranges.end() || Range->first > Loc ||
3798         Range->second.HighPC <= Loc) {
3799       // The +4 is to account for the size of the InitialLength field itself.
3800       InputOffset = EntryOffset + InitialLength + 4;
3801       continue;
3802     }
3803
3804     // This is an FDE, and we have a mapping.
3805     // Have we already emitted a corresponding CIE?
3806     StringRef CIEData = LocalCIES[CIEId];
3807     if (CIEData.empty())
3808       return reportWarning("Inconsistent debug_frame content. Dropping.", DMO);
3809
3810     // Look if we already emitted a CIE that corresponds to the
3811     // referenced one (the CIE data is the key of that lookup).
3812     auto IteratorInserted = EmittedCIEs.insert(
3813         std::make_pair(CIEData, Streamer->getFrameSectionSize()));
3814     // If there is no CIE yet for this ID, emit it.
3815     if (IteratorInserted.second ||
3816         // FIXME: dsymutil-classic only caches the last used CIE for
3817         // reuse. Mimic that behavior for now. Just removing that
3818         // second half of the condition and the LastCIEOffset variable
3819         // makes the code DTRT.
3820         LastCIEOffset != IteratorInserted.first->getValue()) {
3821       LastCIEOffset = Streamer->getFrameSectionSize();
3822       IteratorInserted.first->getValue() = LastCIEOffset;
3823       Streamer->emitCIE(CIEData);
3824     }
3825
3826     // Emit the FDE with updated address and CIE pointer.
3827     // (4 + AddrSize) is the size of the CIEId + initial_location
3828     // fields that will get reconstructed by emitFDE().
3829     unsigned FDERemainingBytes = InitialLength - (4 + AddrSize);
3830     Streamer->emitFDE(IteratorInserted.first->getValue(), AddrSize,
3831                       Loc + Range->second.Offset,
3832                       FrameData.substr(InputOffset, FDERemainingBytes));
3833     InputOffset += FDERemainingBytes;
3834   }
3835 }
3836
3837 void DwarfLinker::DIECloner::copyAbbrev(
3838     const DWARFAbbreviationDeclaration &Abbrev, bool hasODR) {
3839   DIEAbbrev Copy(dwarf::Tag(Abbrev.getTag()),
3840                  dwarf::Form(Abbrev.hasChildren()));
3841
3842   for (const auto &Attr : Abbrev.attributes()) {
3843     uint16_t Form = Attr.Form;
3844     if (hasODR && isODRAttribute(Attr.Attr))
3845       Form = dwarf::DW_FORM_ref_addr;
3846     Copy.AddAttribute(dwarf::Attribute(Attr.Attr), dwarf::Form(Form));
3847   }
3848
3849   Linker.AssignAbbrev(Copy);
3850 }
3851
3852 uint32_t DwarfLinker::DIECloner::hashFullyQualifiedName(
3853     DWARFDie DIE, CompileUnit &U, const DebugMapObject &DMO, int RecurseDepth) {
3854   const char *Name = nullptr;
3855   DWARFUnit *OrigUnit = &U.getOrigUnit();
3856   CompileUnit *CU = &U;
3857   Optional<DWARFFormValue> Ref;
3858
3859   while (1) {
3860     if (const char *CurrentName = DIE.getName(DINameKind::ShortName))
3861       Name = CurrentName;
3862
3863     if (!(Ref = DIE.find(dwarf::DW_AT_specification)) &&
3864         !(Ref = DIE.find(dwarf::DW_AT_abstract_origin)))
3865       break;
3866
3867     if (!Ref->isFormClass(DWARFFormValue::FC_Reference))
3868       break;
3869
3870     CompileUnit *RefCU;
3871     if (auto RefDIE = resolveDIEReference(Linker, DMO, CompileUnits, *Ref,
3872                                           U.getOrigUnit(), DIE, RefCU)) {
3873       CU = RefCU;
3874       OrigUnit = &RefCU->getOrigUnit();
3875       DIE = RefDIE;
3876     }
3877   }
3878
3879   unsigned Idx = OrigUnit->getDIEIndex(DIE);
3880   if (!Name && DIE.getTag() == dwarf::DW_TAG_namespace)
3881     Name = "(anonymous namespace)";
3882
3883   if (CU->getInfo(Idx).ParentIdx == 0 ||
3884       // FIXME: dsymutil-classic compatibility. Ignore modules.
3885       CU->getOrigUnit().getDIEAtIndex(CU->getInfo(Idx).ParentIdx).getTag() ==
3886           dwarf::DW_TAG_module)
3887     return djbHash(Name ? Name : "", djbHash(RecurseDepth ? "" : "::"));
3888
3889   DWARFDie Die = OrigUnit->getDIEAtIndex(CU->getInfo(Idx).ParentIdx);
3890   return djbHash(
3891       (Name ? Name : ""),
3892       djbHash((Name ? "::" : ""),
3893               hashFullyQualifiedName(Die, *CU, DMO, ++RecurseDepth)));
3894 }
3895
3896 static uint64_t getDwoId(const DWARFDie &CUDie, const DWARFUnit &Unit) {
3897   auto DwoId = dwarf::toUnsigned(
3898       CUDie.find({dwarf::DW_AT_dwo_id, dwarf::DW_AT_GNU_dwo_id}));
3899   if (DwoId)
3900     return *DwoId;
3901   return 0;
3902 }
3903
3904 bool DwarfLinker::registerModuleReference(
3905     const DWARFDie &CUDie, const DWARFUnit &Unit, DebugMap &ModuleMap,
3906     const DebugMapObject &DMO, RangesTy &Ranges, OffsetsStringPool &StringPool,
3907     UniquingStringPool &UniquingStringPool, DeclContextTree &ODRContexts,
3908     unsigned &UnitID, unsigned Indent) {
3909   std::string PCMfile = dwarf::toString(
3910       CUDie.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
3911   if (PCMfile.empty())
3912     return false;
3913
3914   // Clang module DWARF skeleton CUs abuse this for the path to the module.
3915   std::string PCMpath = dwarf::toString(CUDie.find(dwarf::DW_AT_comp_dir), "");
3916   uint64_t DwoId = getDwoId(CUDie, Unit);
3917
3918   std::string Name = dwarf::toString(CUDie.find(dwarf::DW_AT_name), "");
3919   if (Name.empty()) {
3920     reportWarning("Anonymous module skeleton CU for " + PCMfile, DMO);
3921     return true;
3922   }
3923
3924   if (Options.Verbose) {
3925     outs().indent(Indent);
3926     outs() << "Found clang module reference " << PCMfile;
3927   }
3928
3929   auto Cached = ClangModules.find(PCMfile);
3930   if (Cached != ClangModules.end()) {
3931     // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
3932     // fixed in clang, only warn about DWO_id mismatches in verbose mode.
3933     // ASTFileSignatures will change randomly when a module is rebuilt.
3934     if (Options.Verbose && (Cached->second != DwoId))
3935       reportWarning(Twine("hash mismatch: this object file was built against a "
3936                           "different version of the module ") +
3937                         PCMfile,
3938                     DMO);
3939     if (Options.Verbose)
3940       outs() << " [cached].\n";
3941     return true;
3942   }
3943   if (Options.Verbose)
3944     outs() << " ...\n";
3945
3946   // Cyclic dependencies are disallowed by Clang, but we still
3947   // shouldn't run into an infinite loop, so mark it as processed now.
3948   ClangModules.insert({PCMfile, DwoId});
3949   if (Error E = loadClangModule(PCMfile, PCMpath, Name, DwoId, ModuleMap, DMO,
3950                                 Ranges, StringPool, UniquingStringPool,
3951                                 ODRContexts, UnitID, Indent + 2)) {
3952     consumeError(std::move(E));
3953     return false;
3954   }
3955   return true;
3956 }
3957
3958 ErrorOr<const object::ObjectFile &>
3959 DwarfLinker::loadObject(BinaryHolder &BinaryHolder, const DebugMapObject &Obj,
3960                         const DebugMap &Map) {
3961   auto ErrOrObjs =
3962       BinaryHolder.GetObjectFiles(Obj.getObjectFilename(), Obj.getTimestamp());
3963   if (std::error_code EC = ErrOrObjs.getError()) {
3964     reportWarning(Twine(Obj.getObjectFilename()) + ": " + EC.message(), Obj);
3965     return EC;
3966   }
3967   auto ErrOrObj = BinaryHolder.Get(Map.getTriple());
3968   if (std::error_code EC = ErrOrObj.getError())
3969     reportWarning(Twine(Obj.getObjectFilename()) + ": " + EC.message(), Obj);
3970   return ErrOrObj;
3971 }
3972
3973 Error DwarfLinker::loadClangModule(StringRef Filename, StringRef ModulePath,
3974                                    StringRef ModuleName, uint64_t DwoId,
3975                                    DebugMap &ModuleMap,
3976                                    const DebugMapObject &DMO, RangesTy &Ranges,
3977                                    OffsetsStringPool &StringPool,
3978                                    UniquingStringPool &UniquingStringPool,
3979                                    DeclContextTree &ODRContexts,
3980                                    unsigned &UnitID, unsigned Indent) {
3981   SmallString<80> Path(Options.PrependPath);
3982   if (sys::path::is_relative(Filename))
3983     sys::path::append(Path, ModulePath, Filename);
3984   else
3985     sys::path::append(Path, Filename);
3986   BinaryHolder ObjHolder(Options.Verbose);
3987   auto &Obj = ModuleMap.addDebugMapObject(
3988       Path, sys::TimePoint<std::chrono::seconds>(), MachO::N_OSO);
3989   auto ErrOrObj = loadObject(ObjHolder, Obj, ModuleMap);
3990   if (!ErrOrObj) {
3991     // Try and emit more helpful warnings by applying some heuristics.
3992     StringRef ObjFile = DMO.getObjectFilename();
3993     bool isClangModule = sys::path::extension(Filename).equals(".pcm");
3994     bool isArchive = ObjFile.endswith(")");
3995     if (isClangModule) {
3996       StringRef ModuleCacheDir = sys::path::parent_path(Path);
3997       if (sys::fs::exists(ModuleCacheDir)) {
3998         // If the module's parent directory exists, we assume that the module
3999         // cache has expired and was pruned by clang.  A more adventurous
4000         // dsymutil would invoke clang to rebuild the module now.
4001         if (!ModuleCacheHintDisplayed) {
4002           note_ostream() << "The clang module cache may have expired since "
4003                             "this object file was built. Rebuilding the "
4004                             "object file will rebuild the module cache.\n";
4005           ModuleCacheHintDisplayed = true;
4006         }
4007       } else if (isArchive) {
4008         // If the module cache directory doesn't exist at all and the object
4009         // file is inside a static library, we assume that the static library
4010         // was built on a different machine. We don't want to discourage module
4011         // debugging for convenience libraries within a project though.
4012         if (!ArchiveHintDisplayed) {
4013           note_ostream() << "Linking a static library that was built with "
4014                             "-gmodules, but the module cache was not found.  "
4015                             "Redistributable static libraries should never be "
4016                             "built with module debugging enabled.  The debug "
4017                             "experience will be degraded due to incomplete "
4018                             "debug information.\n";
4019           ArchiveHintDisplayed = true;
4020         }
4021       }
4022     }
4023     return Error::success();
4024   }
4025
4026   std::unique_ptr<CompileUnit> Unit;
4027
4028   // Setup access to the debug info.
4029   auto DwarfContext = DWARFContext::create(*ErrOrObj);
4030   RelocationManager RelocMgr(*this);
4031   for (const auto &CU : DwarfContext->compile_units()) {
4032     maybeUpdateMaxDwarfVersion(CU->getVersion());
4033
4034     // Recursively get all modules imported by this one.
4035     auto CUDie = CU->getUnitDIE(false);
4036     if (!CUDie)
4037       continue;
4038     if (!registerModuleReference(CUDie, *CU, ModuleMap, DMO, Ranges, StringPool,
4039                                  UniquingStringPool, ODRContexts, UnitID,
4040                                  Indent)) {
4041       if (Unit) {
4042         std::string Err =
4043             (Filename +
4044              ": Clang modules are expected to have exactly 1 compile unit.\n")
4045                 .str();
4046         error(Err);
4047         return make_error<StringError>(Err, inconvertibleErrorCode());
4048       }
4049       // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
4050       // fixed in clang, only warn about DWO_id mismatches in verbose mode.
4051       // ASTFileSignatures will change randomly when a module is rebuilt.
4052       uint64_t PCMDwoId = getDwoId(CUDie, *CU);
4053       if (PCMDwoId != DwoId) {
4054         if (Options.Verbose)
4055           reportWarning(
4056               Twine("hash mismatch: this object file was built against a "
4057                     "different version of the module ") +
4058                   Filename,
4059               DMO);
4060         // Update the cache entry with the DwoId of the module loaded from disk.
4061         ClangModules[Filename] = PCMDwoId;
4062       }
4063
4064       // Add this module.
4065       Unit = llvm::make_unique<CompileUnit>(*CU, UnitID++, !Options.NoODR,
4066                                             ModuleName);
4067       Unit->setHasInterestingContent();
4068       analyzeContextInfo(CUDie, 0, *Unit, &ODRContexts.getRoot(),
4069                          UniquingStringPool, ODRContexts);
4070       // Keep everything.
4071       Unit->markEverythingAsKept();
4072     }
4073   }
4074   if (!Unit->getOrigUnit().getUnitDIE().hasChildren())
4075     return Error::success();
4076   if (Options.Verbose) {
4077     outs().indent(Indent);
4078     outs() << "cloning .debug_info from " << Filename << "\n";
4079   }
4080
4081   std::vector<std::unique_ptr<CompileUnit>> CompileUnits;
4082   CompileUnits.push_back(std::move(Unit));
4083   DIECloner(*this, RelocMgr, DIEAlloc, CompileUnits, Options)
4084       .cloneAllCompileUnits(*DwarfContext, DMO, Ranges, StringPool);
4085   return Error::success();
4086 }
4087
4088 void DwarfLinker::DIECloner::cloneAllCompileUnits(
4089     DWARFContext &DwarfContext, const DebugMapObject &DMO, RangesTy &Ranges,
4090     OffsetsStringPool &StringPool) {
4091   if (!Linker.Streamer)
4092     return;
4093
4094   for (auto &CurrentUnit : CompileUnits) {
4095     auto InputDIE = CurrentUnit->getOrigUnit().getUnitDIE();
4096     CurrentUnit->setStartOffset(Linker.OutputDebugInfoSize);
4097     if (!InputDIE) {
4098       Linker.OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset();
4099       continue;
4100     }
4101     if (CurrentUnit->getInfo(0).Keep) {
4102       // Clone the InputDIE into your Unit DIE in our compile unit since it
4103       // already has a DIE inside of it.
4104       CurrentUnit->createOutputDIE();
4105       cloneDIE(InputDIE, DMO, *CurrentUnit, StringPool, 0 /* PC offset */,
4106                11 /* Unit Header size */, 0, CurrentUnit->getOutputUnitDIE());
4107     }
4108     Linker.OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset();
4109     if (Linker.Options.NoOutput)
4110       continue;
4111
4112     if (LLVM_LIKELY(!Linker.Options.Update)) {
4113       // FIXME: for compatibility with the classic dsymutil, we emit an empty
4114       // line table for the unit, even if the unit doesn't actually exist in
4115       // the DIE tree.
4116       Linker.patchLineTableForUnit(*CurrentUnit, DwarfContext, Ranges, DMO);
4117       Linker.emitAcceleratorEntriesForUnit(*CurrentUnit);
4118       Linker.patchRangesForUnit(*CurrentUnit, DwarfContext, DMO);
4119       Linker.Streamer->emitLocationsForUnit(*CurrentUnit, DwarfContext);
4120     } else {
4121       Linker.emitAcceleratorEntriesForUnit(*CurrentUnit);
4122     }
4123   }
4124
4125   if (Linker.Options.NoOutput)
4126     return;
4127
4128   // Emit all the compile unit's debug information.
4129   for (auto &CurrentUnit : CompileUnits) {
4130     if (LLVM_LIKELY(!Linker.Options.Update))
4131       Linker.generateUnitRanges(*CurrentUnit);
4132     CurrentUnit->fixupForwardReferences();
4133     Linker.Streamer->emitCompileUnitHeader(*CurrentUnit);
4134     if (!CurrentUnit->getOutputUnitDIE())
4135       continue;
4136     Linker.Streamer->emitDIE(*CurrentUnit->getOutputUnitDIE());
4137   }
4138 }
4139
4140 bool DwarfLinker::emitPaperTrailWarnings(const DebugMapObject &DMO,
4141                                          const DebugMap &Map,
4142                                          OffsetsStringPool &StringPool) {
4143   if (DMO.getWarnings().empty() || !DMO.empty())
4144     return false;
4145
4146   Streamer->switchToDebugInfoSection(/* Version */ 2);
4147   DIE *CUDie = DIE::get(DIEAlloc, dwarf::DW_TAG_compile_unit);
4148   CUDie->setOffset(11);
4149   StringRef Producer = StringPool.internString("dsymutil");
4150   StringRef File = StringPool.internString(DMO.getObjectFilename());
4151   CUDie->addValue(DIEAlloc, dwarf::DW_AT_producer, dwarf::DW_FORM_strp,
4152                   DIEInteger(StringPool.getStringOffset(Producer)));
4153   DIEBlock *String = new (DIEAlloc) DIEBlock();
4154   DIEBlocks.push_back(String);
4155   for (auto &C : File)
4156     String->addValue(DIEAlloc, dwarf::Attribute(0), dwarf::DW_FORM_data1,
4157                      DIEInteger(C));
4158   String->addValue(DIEAlloc, dwarf::Attribute(0), dwarf::DW_FORM_data1,
4159                    DIEInteger(0));
4160
4161   CUDie->addValue(DIEAlloc, dwarf::DW_AT_name, dwarf::DW_FORM_string, String);
4162   for (const auto &Warning : DMO.getWarnings()) {
4163     DIE &ConstDie = CUDie->addChild(DIE::get(DIEAlloc, dwarf::DW_TAG_constant));
4164     ConstDie.addValue(
4165         DIEAlloc, dwarf::DW_AT_name, dwarf::DW_FORM_strp,
4166         DIEInteger(StringPool.getStringOffset("dsymutil_warning")));
4167     ConstDie.addValue(DIEAlloc, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag,
4168                       DIEInteger(1));
4169     ConstDie.addValue(DIEAlloc, dwarf::DW_AT_const_value, dwarf::DW_FORM_strp,
4170                       DIEInteger(StringPool.getStringOffset(Warning)));
4171   }
4172   unsigned Size = 4 /* FORM_strp */ + File.size() + 1 +
4173                   DMO.getWarnings().size() * (4 + 1 + 4) +
4174                   1 /* End of children */;
4175   DIEAbbrev Abbrev = CUDie->generateAbbrev();
4176   AssignAbbrev(Abbrev);
4177   CUDie->setAbbrevNumber(Abbrev.getNumber());
4178   Size += getULEB128Size(Abbrev.getNumber());
4179   // Abbreviation ordering needed for classic compatibility.
4180   for (auto &Child : CUDie->children()) {
4181     Abbrev = Child.generateAbbrev();
4182     AssignAbbrev(Abbrev);
4183     Child.setAbbrevNumber(Abbrev.getNumber());
4184     Size += getULEB128Size(Abbrev.getNumber());
4185   }
4186   CUDie->setSize(Size);
4187   auto &Asm = Streamer->getAsmPrinter();
4188   Asm.emitInt32(11 + CUDie->getSize() - 4);
4189   Asm.emitInt16(2);
4190   Asm.emitInt32(0);
4191   Asm.emitInt8(Map.getTriple().isArch64Bit() ? 8 : 4);
4192   Streamer->emitDIE(*CUDie);
4193   OutputDebugInfoSize += 11 /* Header */ + Size;
4194
4195   return true;
4196 }
4197
4198 bool DwarfLinker::link(const DebugMap &Map) {
4199   if (!createStreamer(Map.getTriple(), OutFile))
4200     return false;
4201
4202   // Size of the DIEs (and headers) generated for the linked output.
4203   OutputDebugInfoSize = 0;
4204   // A unique ID that identifies each compile unit.
4205   unsigned UnitID = 0;
4206   DebugMap ModuleMap(Map.getTriple(), Map.getBinaryPath());
4207
4208   // First populate the data structure we need for each iteration of the
4209   // parallel loop.
4210   unsigned NumObjects = Map.getNumberOfObjects();
4211   std::vector<LinkContext> ObjectContexts;
4212   ObjectContexts.reserve(NumObjects);
4213   for (const auto &Obj : Map.objects())
4214     ObjectContexts.emplace_back(Map, *this, *Obj.get(), Options.Verbose);
4215
4216   // This Dwarf string pool which is only used for uniquing. This one should
4217   // never be used for offsets as its not thread-safe or predictable.
4218   UniquingStringPool UniquingStringPool;
4219
4220   // This Dwarf string pool which is used for emission. It must be used
4221   // serially as the order of calling getStringOffset matters for
4222   // reproducibility.
4223   OffsetsStringPool OffsetsStringPool;
4224
4225   // ODR Contexts for the link.
4226   DeclContextTree ODRContexts;
4227
4228   for (LinkContext &LinkContext : ObjectContexts) {
4229     if (Options.Verbose)
4230       outs() << "DEBUG MAP OBJECT: " << LinkContext.DMO.getObjectFilename()
4231              << "\n";
4232
4233     // N_AST objects (swiftmodule files) should get dumped directly into the
4234     // appropriate DWARF section.
4235     if (LinkContext.DMO.getType() == MachO::N_AST) {
4236       StringRef File = LinkContext.DMO.getObjectFilename();
4237       auto ErrorOrMem = MemoryBuffer::getFile(File);
4238       if (!ErrorOrMem) {
4239         warn("Could not open '" + File + "'\n");
4240         continue;
4241       }
4242       sys::fs::file_status Stat;
4243       if (auto Err = sys::fs::status(File, Stat)) {
4244         warn(Err.message());
4245         continue;
4246       }
4247       if (!Options.NoTimestamp &&
4248           Stat.getLastModificationTime() !=
4249               sys::TimePoint<>(LinkContext.DMO.getTimestamp())) {
4250         // Not using the helper here as we can easily stream TimePoint<>.
4251         warn_ostream() << "Timestamp mismatch for " << File << ": "
4252                        << Stat.getLastModificationTime() << " and "
4253                        << sys::TimePoint<>(LinkContext.DMO.getTimestamp())
4254                        << "\n";
4255         continue;
4256       }
4257
4258       // Copy the module into the .swift_ast section.
4259       if (!Options.NoOutput)
4260         Streamer->emitSwiftAST((*ErrorOrMem)->getBuffer());
4261       continue;
4262     }
4263
4264     if (emitPaperTrailWarnings(LinkContext.DMO, Map, OffsetsStringPool))
4265       continue;
4266
4267     if (!LinkContext.ObjectFile)
4268       continue;
4269
4270     // Look for relocations that correspond to debug map entries.
4271
4272     if (LLVM_LIKELY(!Options.Update) &&
4273         !LinkContext.RelocMgr.findValidRelocsInDebugInfo(
4274             *LinkContext.ObjectFile, LinkContext.DMO)) {
4275       if (Options.Verbose)
4276         outs() << "No valid relocations found. Skipping.\n";
4277
4278       // Clear this ObjFile entry as a signal to other loops that we should not
4279       // process this iteration.
4280       LinkContext.ObjectFile = nullptr;
4281       continue;
4282     }
4283
4284     // Setup access to the debug info.
4285     if (!LinkContext.DwarfContext)
4286       continue;
4287
4288     startDebugObject(LinkContext);
4289
4290     // In a first phase, just read in the debug info and load all clang modules.
4291     LinkContext.CompileUnits.reserve(
4292         LinkContext.DwarfContext->getNumCompileUnits());
4293     for (const auto &CU : LinkContext.DwarfContext->compile_units()) {
4294       auto CUDie = CU->getUnitDIE(false);
4295       if (Options.Verbose) {
4296         outs() << "Input compilation unit:";
4297         DIDumpOptions DumpOpts;
4298         DumpOpts.RecurseDepth = 0;
4299         DumpOpts.Verbose = Options.Verbose;
4300         CUDie.dump(outs(), 0, DumpOpts);
4301       }
4302
4303       if (!CUDie || LLVM_UNLIKELY(Options.Update) ||
4304           !registerModuleReference(CUDie, *CU, ModuleMap, LinkContext.DMO,
4305                                    LinkContext.Ranges, OffsetsStringPool,
4306                                    UniquingStringPool, ODRContexts, UnitID)) {
4307         LinkContext.CompileUnits.push_back(llvm::make_unique<CompileUnit>(
4308             *CU, UnitID++, !Options.NoODR && !Options.Update, ""));
4309         maybeUpdateMaxDwarfVersion(CU->getVersion());
4310       }
4311     }
4312   }
4313
4314   // If we haven't seen any CUs, pick an arbitrary valid Dwarf version anyway,
4315   // to be able to emit papertrail warnings.
4316   if (MaxDwarfVersion == 0)
4317     MaxDwarfVersion = 3;
4318
4319   // These variables manage the list of processed object files.
4320   // The mutex and condition variable are to ensure that this is thread safe.
4321   std::mutex ProcessedFilesMutex;
4322   std::condition_variable ProcessedFilesConditionVariable;
4323   BitVector ProcessedFiles(NumObjects, false);
4324
4325   // Now do analyzeContextInfo in parallel as it is particularly expensive.
4326   auto AnalyzeLambda = [&]() {
4327     for (unsigned i = 0, e = NumObjects; i != e; ++i) {
4328       auto &LinkContext = ObjectContexts[i];
4329
4330       if (!LinkContext.ObjectFile) {
4331         std::unique_lock<std::mutex> LockGuard(ProcessedFilesMutex);
4332         ProcessedFiles.set(i);
4333         ProcessedFilesConditionVariable.notify_one();
4334         continue;
4335       }
4336
4337       // Now build the DIE parent links that we will use during the next phase.
4338       for (auto &CurrentUnit : LinkContext.CompileUnits) {
4339         auto CUDie = CurrentUnit->getOrigUnit().getUnitDIE();
4340         if (!CUDie)
4341           continue;
4342         analyzeContextInfo(CurrentUnit->getOrigUnit().getUnitDIE(), 0,
4343                            *CurrentUnit, &ODRContexts.getRoot(),
4344                            UniquingStringPool, ODRContexts);
4345       }
4346
4347       std::unique_lock<std::mutex> LockGuard(ProcessedFilesMutex);
4348       ProcessedFiles.set(i);
4349       ProcessedFilesConditionVariable.notify_one();
4350     }
4351   };
4352
4353   // And then the remaining work in serial again.
4354   // Note, although this loop runs in serial, it can run in parallel with
4355   // the analyzeContextInfo loop so long as we process files with indices >=
4356   // than those processed by analyzeContextInfo.
4357   auto CloneLambda = [&]() {
4358     for (unsigned i = 0, e = NumObjects; i != e; ++i) {
4359       {
4360         std::unique_lock<std::mutex> LockGuard(ProcessedFilesMutex);
4361         if (!ProcessedFiles[i]) {
4362           ProcessedFilesConditionVariable.wait(
4363               LockGuard, [&]() { return ProcessedFiles[i]; });
4364         }
4365       }
4366
4367       auto &LinkContext = ObjectContexts[i];
4368       if (!LinkContext.ObjectFile)
4369         continue;
4370
4371       // Then mark all the DIEs that need to be present in the linked output
4372       // and collect some information about them.
4373       // Note that this loop can not be merged with the previous one because
4374       // cross-cu references require the ParentIdx to be setup for every CU in
4375       // the object file before calling this.
4376       if (LLVM_UNLIKELY(Options.Update)) {
4377         for (auto &CurrentUnit : LinkContext.CompileUnits)
4378           CurrentUnit->markEverythingAsKept();
4379         Streamer->copyInvariantDebugSection(*LinkContext.ObjectFile, Options);
4380       } else {
4381         for (auto &CurrentUnit : LinkContext.CompileUnits)
4382           lookForDIEsToKeep(LinkContext.RelocMgr, LinkContext.Ranges,
4383                             LinkContext.CompileUnits,
4384                             CurrentUnit->getOrigUnit().getUnitDIE(),
4385                             LinkContext.DMO, *CurrentUnit, 0);
4386       }
4387
4388       // The calls to applyValidRelocs inside cloneDIE will walk the reloc
4389       // array again (in the same way findValidRelocsInDebugInfo() did). We
4390       // need to reset the NextValidReloc index to the beginning.
4391       LinkContext.RelocMgr.resetValidRelocs();
4392       if (LinkContext.RelocMgr.hasValidRelocs() ||
4393           LLVM_UNLIKELY(Options.Update))
4394         DIECloner(*this, LinkContext.RelocMgr, DIEAlloc,
4395                   LinkContext.CompileUnits, Options)
4396             .cloneAllCompileUnits(*LinkContext.DwarfContext, LinkContext.DMO,
4397                                   LinkContext.Ranges, OffsetsStringPool);
4398       if (!Options.NoOutput && !LinkContext.CompileUnits.empty() &&
4399           LLVM_LIKELY(!Options.Update))
4400         patchFrameInfoForObject(
4401             LinkContext.DMO, LinkContext.Ranges, *LinkContext.DwarfContext,
4402             LinkContext.CompileUnits[0]->getOrigUnit().getAddressByteSize());
4403
4404       // Clean-up before starting working on the next object.
4405       endDebugObject(LinkContext);
4406     }
4407
4408     // Emit everything that's global.
4409     if (!Options.NoOutput) {
4410       Streamer->emitAbbrevs(Abbreviations, MaxDwarfVersion);
4411       Streamer->emitStrings(OffsetsStringPool);
4412       Streamer->emitAppleNames(AppleNames);
4413       Streamer->emitAppleNamespaces(AppleNamespaces);
4414       Streamer->emitAppleTypes(AppleTypes);
4415       Streamer->emitAppleObjc(AppleObjc);
4416     }
4417   };
4418
4419   // FIXME: The DwarfLinker can have some very deep recursion that can max
4420   // out the (significantly smaller) stack when using threads. We don't
4421   // want this limitation when we only have a single thread.
4422   if (Options.Threads == 1) {
4423     AnalyzeLambda();
4424     CloneLambda();
4425   } else {
4426     ThreadPool pool(2);
4427     pool.async(AnalyzeLambda);
4428     pool.async(CloneLambda);
4429     pool.wait();
4430   }
4431
4432   return Options.NoOutput ? true : Streamer->finish(Map);
4433 }
4434
4435 bool linkDwarf(raw_fd_ostream &OutFile, const DebugMap &DM,
4436                const LinkOptions &Options) {
4437   DwarfLinker Linker(OutFile, Options);
4438   return Linker.link(DM);
4439 }
4440
4441 } // namespace dsymutil
4442 } // namespace llvm