[mips] Add initial release notes for MIPS32.
[oota-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 #include "DebugMap.h"
10 #include "BinaryHolder.h"
11 #include "DebugMap.h"
12 #include "dsymutil.h"
13 #include "MachOUtils.h"
14 #include "NonRelocatableStringpool.h"
15 #include "llvm/ADT/IntervalMap.h"
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/CodeGen/AsmPrinter.h"
19 #include "llvm/CodeGen/DIE.h"
20 #include "llvm/Config/config.h"
21 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
22 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
23 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
24 #include "llvm/MC/MCAsmBackend.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCContext.h"
27 #include "llvm/MC/MCCodeEmitter.h"
28 #include "llvm/MC/MCDwarf.h"
29 #include "llvm/MC/MCInstrInfo.h"
30 #include "llvm/MC/MCObjectFileInfo.h"
31 #include "llvm/MC/MCRegisterInfo.h"
32 #include "llvm/MC/MCStreamer.h"
33 #include "llvm/MC/MCSubtargetInfo.h"
34 #include "llvm/MC/MCTargetOptionsCommandFlags.h"
35 #include "llvm/Object/MachO.h"
36 #include "llvm/Support/Dwarf.h"
37 #include "llvm/Support/LEB128.h"
38 #include "llvm/Support/TargetRegistry.h"
39 #include "llvm/Target/TargetMachine.h"
40 #include "llvm/Target/TargetOptions.h"
41 #include <string>
42 #include <tuple>
43
44 namespace llvm {
45 namespace dsymutil {
46
47 namespace {
48
49 template <typename KeyT, typename ValT>
50 using HalfOpenIntervalMap =
51     IntervalMap<KeyT, ValT, IntervalMapImpl::NodeSizer<KeyT, ValT>::LeafSize,
52                 IntervalMapHalfOpenInfo<KeyT>>;
53
54 typedef HalfOpenIntervalMap<uint64_t, int64_t> FunctionIntervals;
55
56 // FIXME: Delete this structure.
57 struct PatchLocation {
58   DIE::value_iterator I;
59
60   PatchLocation() = default;
61   PatchLocation(DIE::value_iterator I) : I(I) {}
62
63   void set(uint64_t New) const {
64     assert(I);
65     const auto &Old = *I;
66     assert(Old.getType() == DIEValue::isInteger);
67     *I = DIEValue(Old.getAttribute(), Old.getForm(), DIEInteger(New));
68   }
69
70   uint64_t get() const {
71     assert(I);
72     return I->getDIEInteger().getValue();
73   }
74 };
75
76 class CompileUnit;
77 struct DeclMapInfo;
78
79 /// A DeclContext is a named program scope that is used for ODR
80 /// uniquing of types.
81 /// The set of DeclContext for the ODR-subject parts of a Dwarf link
82 /// is expanded (and uniqued) with each new object file processed. We
83 /// need to determine the context of each DIE in an linked object file
84 /// to see if the corresponding type has already been emitted.
85 ///
86 /// The contexts are conceptually organised as a tree (eg. a function
87 /// scope is contained in a namespace scope that contains other
88 /// scopes), but storing/accessing them in an actual tree is too
89 /// inefficient: we need to be able to very quickly query a context
90 /// for a given child context by name. Storing a StringMap in each
91 /// DeclContext would be too space inefficient.
92 /// The solution here is to give each DeclContext a link to its parent
93 /// (this allows to walk up the tree), but to query the existance of a
94 /// specific DeclContext using a separate DenseMap keyed on the hash
95 /// of the fully qualified name of the context.
96 class DeclContext {
97   unsigned QualifiedNameHash;
98   uint32_t Line;
99   uint32_t ByteSize;
100   uint16_t Tag;
101   StringRef Name;
102   StringRef File;
103   const DeclContext &Parent;
104   const DWARFDebugInfoEntryMinimal *LastSeenDIE;
105   uint32_t LastSeenCompileUnitID;
106   uint32_t CanonicalDIEOffset;
107
108   friend DeclMapInfo;
109
110 public:
111   typedef DenseSet<DeclContext *, DeclMapInfo> Map;
112
113   DeclContext()
114       : QualifiedNameHash(0), Line(0), ByteSize(0),
115         Tag(dwarf::DW_TAG_compile_unit), Name(), File(), Parent(*this),
116         LastSeenDIE(nullptr), LastSeenCompileUnitID(0), CanonicalDIEOffset(0) {}
117
118   DeclContext(unsigned Hash, uint32_t Line, uint32_t ByteSize, uint16_t Tag,
119               StringRef Name, StringRef File, const DeclContext &Parent,
120               const DWARFDebugInfoEntryMinimal *LastSeenDIE = nullptr,
121               unsigned CUId = 0)
122       : QualifiedNameHash(Hash), Line(Line), ByteSize(ByteSize), Tag(Tag),
123         Name(Name), File(File), Parent(Parent), LastSeenDIE(LastSeenDIE),
124         LastSeenCompileUnitID(CUId), CanonicalDIEOffset(0) {}
125
126   uint32_t getQualifiedNameHash() const { return QualifiedNameHash; }
127
128   bool setLastSeenDIE(CompileUnit &U, const DWARFDebugInfoEntryMinimal *Die);
129
130   uint32_t getCanonicalDIEOffset() const { return CanonicalDIEOffset; }
131   void setCanonicalDIEOffset(uint32_t Offset) { CanonicalDIEOffset = Offset; }
132
133   uint16_t getTag() const { return Tag; }
134   StringRef getName() const { return Name; }
135 };
136
137 /// Info type for the DenseMap storing the DeclContext pointers.
138 struct DeclMapInfo : private DenseMapInfo<DeclContext *> {
139   using DenseMapInfo<DeclContext *>::getEmptyKey;
140   using DenseMapInfo<DeclContext *>::getTombstoneKey;
141
142   static unsigned getHashValue(const DeclContext *Ctxt) {
143     return Ctxt->QualifiedNameHash;
144   }
145
146   static bool isEqual(const DeclContext *LHS, const DeclContext *RHS) {
147     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
148       return RHS == LHS;
149     return LHS->QualifiedNameHash == RHS->QualifiedNameHash &&
150            LHS->Line == RHS->Line && LHS->ByteSize == RHS->ByteSize &&
151            LHS->Name.data() == RHS->Name.data() &&
152            LHS->File.data() == RHS->File.data() &&
153            LHS->Parent.QualifiedNameHash == RHS->Parent.QualifiedNameHash;
154   }
155 };
156
157 /// This class gives a tree-like API to the DenseMap that stores the
158 /// DeclContext objects. It also holds the BumpPtrAllocator where
159 /// these objects will be allocated.
160 class DeclContextTree {
161   BumpPtrAllocator Allocator;
162   DeclContext Root;
163   DeclContext::Map Contexts;
164
165 public:
166   /// Get the child of \a Context described by \a DIE in \a Unit. The
167   /// required strings will be interned in \a StringPool.
168   /// \returns The child DeclContext along with one bit that is set if
169   /// this context is invalid.
170   /// An invalid context means it shouldn't be considered for uniquing, but its
171   /// not returning null, because some children of that context might be
172   /// uniquing candidates.  FIXME: The invalid bit along the return value is to
173   /// emulate some dsymutil-classic functionality.
174   PointerIntPair<DeclContext *, 1>
175   getChildDeclContext(DeclContext &Context,
176                       const DWARFDebugInfoEntryMinimal *DIE, CompileUnit &Unit,
177                       NonRelocatableStringpool &StringPool, bool InClangModule);
178
179   DeclContext &getRoot() { return Root; }
180 };
181
182 /// \brief Stores all information relating to a compile unit, be it in
183 /// its original instance in the object file to its brand new cloned
184 /// and linked DIE tree.
185 class CompileUnit {
186 public:
187   /// \brief Information gathered about a DIE in the object file.
188   struct DIEInfo {
189     int64_t AddrAdjust; ///< Address offset to apply to the described entity.
190     DeclContext *Ctxt;  ///< ODR Declaration context.
191     DIE *Clone;         ///< Cloned version of that DIE.
192     uint32_t ParentIdx; ///< The index of this DIE's parent.
193     bool Keep : 1;      ///< Is the DIE part of the linked output?
194     bool InDebugMap : 1;///< Was this DIE's entity found in the map?
195     bool Prune : 1;     ///< Is this a pure forward declaration we can strip?
196   };
197
198   CompileUnit(DWARFUnit &OrigUnit, unsigned ID, bool CanUseODR,
199               StringRef ClangModuleName)
200       : OrigUnit(OrigUnit), ID(ID), LowPc(UINT64_MAX), HighPc(0), RangeAlloc(),
201         Ranges(RangeAlloc), ClangModuleName(ClangModuleName) {
202     Info.resize(OrigUnit.getNumDIEs());
203
204     const auto *CUDie = OrigUnit.getUnitDIE(false);
205     unsigned Lang = CUDie->getAttributeValueAsUnsignedConstant(
206         &OrigUnit, dwarf::DW_AT_language, 0);
207     HasODR = CanUseODR && (Lang == dwarf::DW_LANG_C_plus_plus ||
208                            Lang == dwarf::DW_LANG_C_plus_plus_03 ||
209                            Lang == dwarf::DW_LANG_C_plus_plus_11 ||
210                            Lang == dwarf::DW_LANG_C_plus_plus_14 ||
211                            Lang == dwarf::DW_LANG_ObjC_plus_plus);
212   }
213
214   CompileUnit(CompileUnit &&RHS)
215       : OrigUnit(RHS.OrigUnit), Info(std::move(RHS.Info)),
216         CUDie(std::move(RHS.CUDie)), StartOffset(RHS.StartOffset),
217         NextUnitOffset(RHS.NextUnitOffset), RangeAlloc(), Ranges(RangeAlloc) {
218     // The CompileUnit container has been 'reserve()'d with the right
219     // size. We cannot move the IntervalMap anyway.
220     llvm_unreachable("CompileUnits should not be moved.");
221   }
222
223   DWARFUnit &getOrigUnit() const { return OrigUnit; }
224
225   unsigned getUniqueID() const { return ID; }
226
227   DIE *getOutputUnitDIE() const { return CUDie; }
228   void setOutputUnitDIE(DIE *Die) { CUDie = Die; }
229
230   bool hasODR() const { return HasODR; }
231   bool isClangModule() const { return !ClangModuleName.empty(); }
232   const std::string &getClangModuleName() const { return ClangModuleName; }
233
234   DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; }
235   const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; }
236
237   uint64_t getStartOffset() const { return StartOffset; }
238   uint64_t getNextUnitOffset() const { return NextUnitOffset; }
239   void setStartOffset(uint64_t DebugInfoSize) { StartOffset = DebugInfoSize; }
240
241   uint64_t getLowPc() const { return LowPc; }
242   uint64_t getHighPc() const { return HighPc; }
243
244   Optional<PatchLocation> getUnitRangesAttribute() const {
245     return UnitRangeAttribute;
246   }
247   const FunctionIntervals &getFunctionRanges() const { return Ranges; }
248   const std::vector<PatchLocation> &getRangesAttributes() const {
249     return RangeAttributes;
250   }
251
252   const std::vector<std::pair<PatchLocation, int64_t>> &
253   getLocationAttributes() const {
254     return LocationAttributes;
255   }
256
257   void setHasInterestingContent() { HasInterestingContent = true; }
258   bool hasInterestingContent() { return HasInterestingContent; }
259
260   /// Mark every DIE in this unit as kept. This function also
261   /// marks variables as InDebugMap so that they appear in the
262   /// reconstructed accelerator tables.
263   void markEverythingAsKept();
264
265   /// \brief Compute the end offset for this unit. Must be
266   /// called after the CU's DIEs have been cloned.
267   /// \returns the next unit offset (which is also the current
268   /// debug_info section size).
269   uint64_t computeNextUnitOffset();
270
271   /// \brief Keep track of a forward reference to DIE \p Die in \p
272   /// RefUnit by \p Attr. The attribute should be fixed up later to
273   /// point to the absolute offset of \p Die in the debug_info section
274   /// or to the canonical offset of \p Ctxt if it is non-null.
275   void noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
276                             DeclContext *Ctxt, PatchLocation Attr);
277
278   /// \brief Apply all fixups recored by noteForwardReference().
279   void fixupForwardReferences();
280
281   /// \brief Add a function range [\p LowPC, \p HighPC) that is
282   /// relocatad by applying offset \p PCOffset.
283   void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset);
284
285   /// \brief Keep track of a DW_AT_range attribute that we will need to
286   /// patch up later.
287   void noteRangeAttribute(const DIE &Die, PatchLocation Attr);
288
289   /// \brief Keep track of a location attribute pointing to a location
290   /// list in the debug_loc section.
291   void noteLocationAttribute(PatchLocation Attr, int64_t PcOffset);
292
293   /// \brief Add a name accelerator entry for \p Die with \p Name
294   /// which is stored in the string table at \p Offset.
295   void addNameAccelerator(const DIE *Die, const char *Name, uint32_t Offset,
296                           bool SkipPubnamesSection = false);
297
298   /// \brief Add a type accelerator entry for \p Die with \p Name
299   /// which is stored in the string table at \p Offset.
300   void addTypeAccelerator(const DIE *Die, const char *Name, uint32_t Offset);
301
302   struct AccelInfo {
303     StringRef Name;      ///< Name of the entry.
304     const DIE *Die;      ///< DIE this entry describes.
305     uint32_t NameOffset; ///< Offset of Name in the string pool.
306     bool SkipPubSection; ///< Emit this entry only in the apple_* sections.
307
308     AccelInfo(StringRef Name, const DIE *Die, uint32_t NameOffset,
309               bool SkipPubSection = false)
310         : Name(Name), Die(Die), NameOffset(NameOffset),
311           SkipPubSection(SkipPubSection) {}
312   };
313
314   const std::vector<AccelInfo> &getPubnames() const { return Pubnames; }
315   const std::vector<AccelInfo> &getPubtypes() const { return Pubtypes; }
316
317   /// Get the full path for file \a FileNum in the line table
318   const char *getResolvedPath(unsigned FileNum) {
319     if (FileNum >= ResolvedPaths.size())
320       return nullptr;
321     return ResolvedPaths[FileNum].size() ? ResolvedPaths[FileNum].c_str()
322                                          : nullptr;
323   }
324
325   /// Set the fully resolved path for the line-table's file \a FileNum
326   /// to \a Path.
327   void setResolvedPath(unsigned FileNum, const std::string &Path) {
328     if (ResolvedPaths.size() <= FileNum)
329       ResolvedPaths.resize(FileNum + 1);
330     ResolvedPaths[FileNum] = Path;
331   }
332
333 private:
334   DWARFUnit &OrigUnit;
335   unsigned ID;
336   std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index.
337   DIE *CUDie;                ///< Root of the linked DIE tree.
338
339   uint64_t StartOffset;
340   uint64_t NextUnitOffset;
341
342   uint64_t LowPc;
343   uint64_t HighPc;
344
345   /// \brief A list of attributes to fixup with the absolute offset of
346   /// a DIE in the debug_info section.
347   ///
348   /// The offsets for the attributes in this array couldn't be set while
349   /// cloning because for cross-cu forward refences the target DIE's
350   /// offset isn't known you emit the reference attribute.
351   std::vector<std::tuple<DIE *, const CompileUnit *, DeclContext *,
352                          PatchLocation>> ForwardDIEReferences;
353
354   FunctionIntervals::Allocator RangeAlloc;
355   /// \brief The ranges in that interval map are the PC ranges for
356   /// functions in this unit, associated with the PC offset to apply
357   /// to the addresses to get the linked address.
358   FunctionIntervals Ranges;
359
360   /// \brief DW_AT_ranges attributes to patch after we have gathered
361   /// all the unit's function addresses.
362   /// @{
363   std::vector<PatchLocation> RangeAttributes;
364   Optional<PatchLocation> UnitRangeAttribute;
365   /// @}
366
367   /// \brief Location attributes that need to be transfered from th
368   /// original debug_loc section to the liked one. They are stored
369   /// along with the PC offset that is to be applied to their
370   /// function's address.
371   std::vector<std::pair<PatchLocation, int64_t>> LocationAttributes;
372
373   /// \brief Accelerator entries for the unit, both for the pub*
374   /// sections and the apple* ones.
375   /// @{
376   std::vector<AccelInfo> Pubnames;
377   std::vector<AccelInfo> Pubtypes;
378   /// @}
379
380   /// Cached resolved paths from the line table.
381   std::vector<std::string> ResolvedPaths;
382
383   /// Is this unit subject to the ODR rule?
384   bool HasODR;
385   /// Did a DIE actually contain a valid reloc?
386   bool HasInterestingContent;
387   /// If this is a Clang module, this holds the module's name.
388   std::string ClangModuleName;
389 };
390
391 void CompileUnit::markEverythingAsKept() {
392   for (auto &I : Info)
393     // Mark everything that wasn't explicity marked for pruning.
394     I.Keep = !I.Prune;
395 }
396
397 uint64_t CompileUnit::computeNextUnitOffset() {
398   NextUnitOffset = StartOffset + 11 /* Header size */;
399   // The root DIE might be null, meaning that the Unit had nothing to
400   // contribute to the linked output. In that case, we will emit the
401   // unit header without any actual DIE.
402   if (CUDie)
403     NextUnitOffset += CUDie->getSize();
404   return NextUnitOffset;
405 }
406
407 /// \brief Keep track of a forward cross-cu reference from this unit
408 /// to \p Die that lives in \p RefUnit.
409 void CompileUnit::noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
410                                        DeclContext *Ctxt, PatchLocation Attr) {
411   ForwardDIEReferences.emplace_back(Die, RefUnit, Ctxt, Attr);
412 }
413
414 /// \brief Apply all fixups recorded by noteForwardReference().
415 void CompileUnit::fixupForwardReferences() {
416   for (const auto &Ref : ForwardDIEReferences) {
417     DIE *RefDie;
418     const CompileUnit *RefUnit;
419     PatchLocation Attr;
420     DeclContext *Ctxt;
421     std::tie(RefDie, RefUnit, Ctxt, Attr) = Ref;
422     if (Ctxt && Ctxt->getCanonicalDIEOffset())
423       Attr.set(Ctxt->getCanonicalDIEOffset());
424     else
425       Attr.set(RefDie->getOffset() + RefUnit->getStartOffset());
426   }
427 }
428
429 void CompileUnit::addFunctionRange(uint64_t FuncLowPc, uint64_t FuncHighPc,
430                                    int64_t PcOffset) {
431   Ranges.insert(FuncLowPc, FuncHighPc, PcOffset);
432   this->LowPc = std::min(LowPc, FuncLowPc + PcOffset);
433   this->HighPc = std::max(HighPc, FuncHighPc + PcOffset);
434 }
435
436 void CompileUnit::noteRangeAttribute(const DIE &Die, PatchLocation Attr) {
437   if (Die.getTag() != dwarf::DW_TAG_compile_unit)
438     RangeAttributes.push_back(Attr);
439   else
440     UnitRangeAttribute = Attr;
441 }
442
443 void CompileUnit::noteLocationAttribute(PatchLocation Attr, int64_t PcOffset) {
444   LocationAttributes.emplace_back(Attr, PcOffset);
445 }
446
447 /// \brief Add a name accelerator entry for \p Die with \p Name
448 /// which is stored in the string table at \p Offset.
449 void CompileUnit::addNameAccelerator(const DIE *Die, const char *Name,
450                                      uint32_t Offset, bool SkipPubSection) {
451   Pubnames.emplace_back(Name, Die, Offset, SkipPubSection);
452 }
453
454 /// \brief Add a type accelerator entry for \p Die with \p Name
455 /// which is stored in the string table at \p Offset.
456 void CompileUnit::addTypeAccelerator(const DIE *Die, const char *Name,
457                                      uint32_t Offset) {
458   Pubtypes.emplace_back(Name, Die, Offset, false);
459 }
460
461 /// \brief The Dwarf streaming logic
462 ///
463 /// All interactions with the MC layer that is used to build the debug
464 /// information binary representation are handled in this class.
465 class DwarfStreamer {
466   /// \defgroup MCObjects MC layer objects constructed by the streamer
467   /// @{
468   std::unique_ptr<MCRegisterInfo> MRI;
469   std::unique_ptr<MCAsmInfo> MAI;
470   std::unique_ptr<MCObjectFileInfo> MOFI;
471   std::unique_ptr<MCContext> MC;
472   MCAsmBackend *MAB; // Owned by MCStreamer
473   std::unique_ptr<MCInstrInfo> MII;
474   std::unique_ptr<MCSubtargetInfo> MSTI;
475   MCCodeEmitter *MCE; // Owned by MCStreamer
476   MCStreamer *MS;     // Owned by AsmPrinter
477   std::unique_ptr<TargetMachine> TM;
478   std::unique_ptr<AsmPrinter> Asm;
479   /// @}
480
481   /// \brief the file we stream the linked Dwarf to.
482   std::unique_ptr<raw_fd_ostream> OutFile;
483
484   uint32_t RangesSectionSize;
485   uint32_t LocSectionSize;
486   uint32_t LineSectionSize;
487   uint32_t FrameSectionSize;
488
489   /// \brief Emit the pubnames or pubtypes section contribution for \p
490   /// Unit into \p Sec. The data is provided in \p Names.
491   void emitPubSectionForUnit(MCSection *Sec, StringRef Name,
492                              const CompileUnit &Unit,
493                              const std::vector<CompileUnit::AccelInfo> &Names);
494
495 public:
496   /// \brief Actually create the streamer and the ouptut file.
497   ///
498   /// This could be done directly in the constructor, but it feels
499   /// more natural to handle errors through return value.
500   bool init(Triple TheTriple, StringRef OutputFilename);
501
502   /// \brief Dump the file to the disk.
503   bool finish(const DebugMap &);
504
505   AsmPrinter &getAsmPrinter() const { return *Asm; }
506
507   /// \brief Set the current output section to debug_info and change
508   /// the MC Dwarf version to \p DwarfVersion.
509   void switchToDebugInfoSection(unsigned DwarfVersion);
510
511   /// \brief Emit the compilation unit header for \p Unit in the
512   /// debug_info section.
513   ///
514   /// As a side effect, this also switches the current Dwarf version
515   /// of the MC layer to the one of U.getOrigUnit().
516   void emitCompileUnitHeader(CompileUnit &Unit);
517
518   /// \brief Recursively emit the DIE tree rooted at \p Die.
519   void emitDIE(DIE &Die);
520
521   /// \brief Emit the abbreviation table \p Abbrevs to the
522   /// debug_abbrev section.
523   void emitAbbrevs(const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs);
524
525   /// \brief Emit the string table described by \p Pool.
526   void emitStrings(const NonRelocatableStringpool &Pool);
527
528   /// \brief Emit debug_ranges for \p FuncRange by translating the
529   /// original \p Entries.
530   void emitRangesEntries(
531       int64_t UnitPcOffset, uint64_t OrigLowPc,
532       FunctionIntervals::const_iterator FuncRange,
533       const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
534       unsigned AddressSize);
535
536   /// \brief Emit debug_aranges entries for \p Unit and if \p
537   /// DoRangesSection is true, also emit the debug_ranges entries for
538   /// the DW_TAG_compile_unit's DW_AT_ranges attribute.
539   void emitUnitRangesEntries(CompileUnit &Unit, bool DoRangesSection);
540
541   uint32_t getRangesSectionSize() const { return RangesSectionSize; }
542
543   /// \brief Emit the debug_loc contribution for \p Unit by copying
544   /// the entries from \p Dwarf and offseting them. Update the
545   /// location attributes to point to the new entries.
546   void emitLocationsForUnit(const CompileUnit &Unit, DWARFContext &Dwarf);
547
548   /// \brief Emit the line table described in \p Rows into the
549   /// debug_line section.
550   void emitLineTableForUnit(MCDwarfLineTableParams Params,
551                             StringRef PrologueBytes, unsigned MinInstLength,
552                             std::vector<DWARFDebugLine::Row> &Rows,
553                             unsigned AdddressSize);
554
555   uint32_t getLineSectionSize() const { return LineSectionSize; }
556
557   /// \brief Emit the .debug_pubnames contribution for \p Unit.
558   void emitPubNamesForUnit(const CompileUnit &Unit);
559
560   /// \brief Emit the .debug_pubtypes contribution for \p Unit.
561   void emitPubTypesForUnit(const CompileUnit &Unit);
562
563   /// \brief Emit a CIE.
564   void emitCIE(StringRef CIEBytes);
565
566   /// \brief Emit an FDE with data \p Bytes.
567   void emitFDE(uint32_t CIEOffset, uint32_t AddreSize, uint32_t Address,
568                StringRef Bytes);
569
570   uint32_t getFrameSectionSize() const { return FrameSectionSize; }
571 };
572
573 bool DwarfStreamer::init(Triple TheTriple, StringRef OutputFilename) {
574   std::string ErrorStr;
575   std::string TripleName;
576   StringRef Context = "dwarf streamer init";
577
578   // Get the target.
579   const Target *TheTarget =
580       TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr);
581   if (!TheTarget)
582     return error(ErrorStr, Context);
583   TripleName = TheTriple.getTriple();
584
585   // Create all the MC Objects.
586   MRI.reset(TheTarget->createMCRegInfo(TripleName));
587   if (!MRI)
588     return error(Twine("no register info for target ") + TripleName, Context);
589
590   MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName));
591   if (!MAI)
592     return error("no asm info for target " + TripleName, Context);
593
594   MOFI.reset(new MCObjectFileInfo);
595   MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get()));
596   MOFI->InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default,
597                              *MC);
598
599   MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
600   if (!MAB)
601     return error("no asm backend for target " + TripleName, Context);
602
603   MII.reset(TheTarget->createMCInstrInfo());
604   if (!MII)
605     return error("no instr info info for target " + TripleName, Context);
606
607   MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
608   if (!MSTI)
609     return error("no subtarget info for target " + TripleName, Context);
610
611   MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MC);
612   if (!MCE)
613     return error("no code emitter for target " + TripleName, Context);
614
615   // Create the output file.
616   std::error_code EC;
617   OutFile =
618       llvm::make_unique<raw_fd_ostream>(OutputFilename, EC, sys::fs::F_None);
619   if (EC)
620     return error(Twine(OutputFilename) + ": " + EC.message(), Context);
621
622   MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
623   MS = TheTarget->createMCObjectStreamer(
624       TheTriple, *MC, *MAB, *OutFile, MCE, *MSTI, MCOptions.MCRelaxAll,
625       MCOptions.MCIncrementalLinkerCompatible,
626       /*DWARFMustBeAtTheEnd*/ false);
627   if (!MS)
628     return error("no object streamer for target " + TripleName, Context);
629
630   // Finally create the AsmPrinter we'll use to emit the DIEs.
631   TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions()));
632   if (!TM)
633     return error("no target machine for target " + TripleName, Context);
634
635   Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS)));
636   if (!Asm)
637     return error("no asm printer for target " + TripleName, Context);
638
639   RangesSectionSize = 0;
640   LocSectionSize = 0;
641   LineSectionSize = 0;
642   FrameSectionSize = 0;
643
644   return true;
645 }
646
647 bool DwarfStreamer::finish(const DebugMap &DM) {
648   if (DM.getTriple().isOSDarwin() && !DM.getBinaryPath().empty())
649     return MachOUtils::generateDsymCompanion(DM, *MS, *OutFile);
650
651   MS->Finish();
652   return true;
653 }
654
655 /// \brief Set the current output section to debug_info and change
656 /// the MC Dwarf version to \p DwarfVersion.
657 void DwarfStreamer::switchToDebugInfoSection(unsigned DwarfVersion) {
658   MS->SwitchSection(MOFI->getDwarfInfoSection());
659   MC->setDwarfVersion(DwarfVersion);
660 }
661
662 /// \brief Emit the compilation unit header for \p Unit in the
663 /// debug_info section.
664 ///
665 /// A Dwarf scetion header is encoded as:
666 ///  uint32_t   Unit length (omiting this field)
667 ///  uint16_t   Version
668 ///  uint32_t   Abbreviation table offset
669 ///  uint8_t    Address size
670 ///
671 /// Leading to a total of 11 bytes.
672 void DwarfStreamer::emitCompileUnitHeader(CompileUnit &Unit) {
673   unsigned Version = Unit.getOrigUnit().getVersion();
674   switchToDebugInfoSection(Version);
675
676   // Emit size of content not including length itself. The size has
677   // already been computed in CompileUnit::computeOffsets(). Substract
678   // 4 to that size to account for the length field.
679   Asm->EmitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset() - 4);
680   Asm->EmitInt16(Version);
681   // We share one abbreviations table across all units so it's always at the
682   // start of the section.
683   Asm->EmitInt32(0);
684   Asm->EmitInt8(Unit.getOrigUnit().getAddressByteSize());
685 }
686
687 /// \brief Emit the \p Abbrevs array as the shared abbreviation table
688 /// for the linked Dwarf file.
689 void DwarfStreamer::emitAbbrevs(
690     const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs) {
691   MS->SwitchSection(MOFI->getDwarfAbbrevSection());
692   Asm->emitDwarfAbbrevs(Abbrevs);
693 }
694
695 /// \brief Recursively emit the DIE tree rooted at \p Die.
696 void DwarfStreamer::emitDIE(DIE &Die) {
697   MS->SwitchSection(MOFI->getDwarfInfoSection());
698   Asm->emitDwarfDIE(Die);
699 }
700
701 /// \brief Emit the debug_str section stored in \p Pool.
702 void DwarfStreamer::emitStrings(const NonRelocatableStringpool &Pool) {
703   Asm->OutStreamer->SwitchSection(MOFI->getDwarfStrSection());
704   for (auto *Entry = Pool.getFirstEntry(); Entry;
705        Entry = Pool.getNextEntry(Entry))
706     Asm->OutStreamer->EmitBytes(
707         StringRef(Entry->getKey().data(), Entry->getKey().size() + 1));
708 }
709
710 /// \brief Emit the debug_range section contents for \p FuncRange by
711 /// translating the original \p Entries. The debug_range section
712 /// format is totally trivial, consisting just of pairs of address
713 /// sized addresses describing the ranges.
714 void DwarfStreamer::emitRangesEntries(
715     int64_t UnitPcOffset, uint64_t OrigLowPc,
716     FunctionIntervals::const_iterator FuncRange,
717     const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
718     unsigned AddressSize) {
719   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
720
721   // Offset each range by the right amount.
722   int64_t PcOffset = Entries.empty() ? 0 : FuncRange.value() + UnitPcOffset;
723   for (const auto &Range : Entries) {
724     if (Range.isBaseAddressSelectionEntry(AddressSize)) {
725       warn("unsupported base address selection operation",
726            "emitting debug_ranges");
727       break;
728     }
729     // Do not emit empty ranges.
730     if (Range.StartAddress == Range.EndAddress)
731       continue;
732
733     // All range entries should lie in the function range.
734     if (!(Range.StartAddress + OrigLowPc >= FuncRange.start() &&
735           Range.EndAddress + OrigLowPc <= FuncRange.stop()))
736       warn("inconsistent range data.", "emitting debug_ranges");
737     MS->EmitIntValue(Range.StartAddress + PcOffset, AddressSize);
738     MS->EmitIntValue(Range.EndAddress + PcOffset, AddressSize);
739     RangesSectionSize += 2 * AddressSize;
740   }
741
742   // Add the terminator entry.
743   MS->EmitIntValue(0, AddressSize);
744   MS->EmitIntValue(0, AddressSize);
745   RangesSectionSize += 2 * AddressSize;
746 }
747
748 /// \brief Emit the debug_aranges contribution of a unit and
749 /// if \p DoDebugRanges is true the debug_range contents for a
750 /// compile_unit level DW_AT_ranges attribute (Which are basically the
751 /// same thing with a different base address).
752 /// Just aggregate all the ranges gathered inside that unit.
753 void DwarfStreamer::emitUnitRangesEntries(CompileUnit &Unit,
754                                           bool DoDebugRanges) {
755   unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
756   // Gather the ranges in a vector, so that we can simplify them. The
757   // IntervalMap will have coalesced the non-linked ranges, but here
758   // we want to coalesce the linked addresses.
759   std::vector<std::pair<uint64_t, uint64_t>> Ranges;
760   const auto &FunctionRanges = Unit.getFunctionRanges();
761   for (auto Range = FunctionRanges.begin(), End = FunctionRanges.end();
762        Range != End; ++Range)
763     Ranges.push_back(std::make_pair(Range.start() + Range.value(),
764                                     Range.stop() + Range.value()));
765
766   // The object addresses where sorted, but again, the linked
767   // addresses might end up in a different order.
768   std::sort(Ranges.begin(), Ranges.end());
769
770   if (!Ranges.empty()) {
771     MS->SwitchSection(MC->getObjectFileInfo()->getDwarfARangesSection());
772
773     MCSymbol *BeginLabel = Asm->createTempSymbol("Barange");
774     MCSymbol *EndLabel = Asm->createTempSymbol("Earange");
775
776     unsigned HeaderSize =
777         sizeof(int32_t) + // Size of contents (w/o this field
778         sizeof(int16_t) + // DWARF ARange version number
779         sizeof(int32_t) + // Offset of CU in the .debug_info section
780         sizeof(int8_t) +  // Pointer Size (in bytes)
781         sizeof(int8_t);   // Segment Size (in bytes)
782
783     unsigned TupleSize = AddressSize * 2;
784     unsigned Padding = OffsetToAlignment(HeaderSize, TupleSize);
785
786     Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); // Arange length
787     Asm->OutStreamer->EmitLabel(BeginLabel);
788     Asm->EmitInt16(dwarf::DW_ARANGES_VERSION); // Version number
789     Asm->EmitInt32(Unit.getStartOffset());     // Corresponding unit's offset
790     Asm->EmitInt8(AddressSize);                // Address size
791     Asm->EmitInt8(0);                          // Segment size
792
793     Asm->OutStreamer->EmitFill(Padding, 0x0);
794
795     for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End;
796          ++Range) {
797       uint64_t RangeStart = Range->first;
798       MS->EmitIntValue(RangeStart, AddressSize);
799       while ((Range + 1) != End && Range->second == (Range + 1)->first)
800         ++Range;
801       MS->EmitIntValue(Range->second - RangeStart, AddressSize);
802     }
803
804     // Emit terminator
805     Asm->OutStreamer->EmitIntValue(0, AddressSize);
806     Asm->OutStreamer->EmitIntValue(0, AddressSize);
807     Asm->OutStreamer->EmitLabel(EndLabel);
808   }
809
810   if (!DoDebugRanges)
811     return;
812
813   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
814   // Offset each range by the right amount.
815   int64_t PcOffset = -Unit.getLowPc();
816   // Emit coalesced ranges.
817   for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End; ++Range) {
818     MS->EmitIntValue(Range->first + PcOffset, AddressSize);
819     while (Range + 1 != End && Range->second == (Range + 1)->first)
820       ++Range;
821     MS->EmitIntValue(Range->second + PcOffset, AddressSize);
822     RangesSectionSize += 2 * AddressSize;
823   }
824
825   // Add the terminator entry.
826   MS->EmitIntValue(0, AddressSize);
827   MS->EmitIntValue(0, AddressSize);
828   RangesSectionSize += 2 * AddressSize;
829 }
830
831 /// \brief Emit location lists for \p Unit and update attribtues to
832 /// point to the new entries.
833 void DwarfStreamer::emitLocationsForUnit(const CompileUnit &Unit,
834                                          DWARFContext &Dwarf) {
835   const auto &Attributes = Unit.getLocationAttributes();
836
837   if (Attributes.empty())
838     return;
839
840   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLocSection());
841
842   unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
843   const DWARFSection &InputSec = Dwarf.getLocSection();
844   DataExtractor Data(InputSec.Data, Dwarf.isLittleEndian(), AddressSize);
845   DWARFUnit &OrigUnit = Unit.getOrigUnit();
846   const auto *OrigUnitDie = OrigUnit.getUnitDIE(false);
847   int64_t UnitPcOffset = 0;
848   uint64_t OrigLowPc = OrigUnitDie->getAttributeValueAsAddress(
849       &OrigUnit, dwarf::DW_AT_low_pc, -1ULL);
850   if (OrigLowPc != -1ULL)
851     UnitPcOffset = int64_t(OrigLowPc) - Unit.getLowPc();
852
853   for (const auto &Attr : Attributes) {
854     uint32_t Offset = Attr.first.get();
855     Attr.first.set(LocSectionSize);
856     // This is the quantity to add to the old location address to get
857     // the correct address for the new one.
858     int64_t LocPcOffset = Attr.second + UnitPcOffset;
859     while (Data.isValidOffset(Offset)) {
860       uint64_t Low = Data.getUnsigned(&Offset, AddressSize);
861       uint64_t High = Data.getUnsigned(&Offset, AddressSize);
862       LocSectionSize += 2 * AddressSize;
863       if (Low == 0 && High == 0) {
864         Asm->OutStreamer->EmitIntValue(0, AddressSize);
865         Asm->OutStreamer->EmitIntValue(0, AddressSize);
866         break;
867       }
868       Asm->OutStreamer->EmitIntValue(Low + LocPcOffset, AddressSize);
869       Asm->OutStreamer->EmitIntValue(High + LocPcOffset, AddressSize);
870       uint64_t Length = Data.getU16(&Offset);
871       Asm->OutStreamer->EmitIntValue(Length, 2);
872       // Just copy the bytes over.
873       Asm->OutStreamer->EmitBytes(
874           StringRef(InputSec.Data.substr(Offset, Length)));
875       Offset += Length;
876       LocSectionSize += Length + 2;
877     }
878   }
879 }
880
881 void DwarfStreamer::emitLineTableForUnit(MCDwarfLineTableParams Params,
882                                          StringRef PrologueBytes,
883                                          unsigned MinInstLength,
884                                          std::vector<DWARFDebugLine::Row> &Rows,
885                                          unsigned PointerSize) {
886   // Switch to the section where the table will be emitted into.
887   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLineSection());
888   MCSymbol *LineStartSym = MC->createTempSymbol();
889   MCSymbol *LineEndSym = MC->createTempSymbol();
890
891   // The first 4 bytes is the total length of the information for this
892   // compilation unit (not including these 4 bytes for the length).
893   Asm->EmitLabelDifference(LineEndSym, LineStartSym, 4);
894   Asm->OutStreamer->EmitLabel(LineStartSym);
895   // Copy Prologue.
896   MS->EmitBytes(PrologueBytes);
897   LineSectionSize += PrologueBytes.size() + 4;
898
899   SmallString<128> EncodingBuffer;
900   raw_svector_ostream EncodingOS(EncodingBuffer);
901
902   if (Rows.empty()) {
903     // We only have the dummy entry, dsymutil emits an entry with a 0
904     // address in that case.
905     MCDwarfLineAddr::Encode(*MC, Params, INT64_MAX, 0, EncodingOS);
906     MS->EmitBytes(EncodingOS.str());
907     LineSectionSize += EncodingBuffer.size();
908     MS->EmitLabel(LineEndSym);
909     return;
910   }
911
912   // Line table state machine fields
913   unsigned FileNum = 1;
914   unsigned LastLine = 1;
915   unsigned Column = 0;
916   unsigned IsStatement = 1;
917   unsigned Isa = 0;
918   uint64_t Address = -1ULL;
919
920   unsigned RowsSinceLastSequence = 0;
921
922   for (unsigned Idx = 0; Idx < Rows.size(); ++Idx) {
923     auto &Row = Rows[Idx];
924
925     int64_t AddressDelta;
926     if (Address == -1ULL) {
927       MS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
928       MS->EmitULEB128IntValue(PointerSize + 1);
929       MS->EmitIntValue(dwarf::DW_LNE_set_address, 1);
930       MS->EmitIntValue(Row.Address, PointerSize);
931       LineSectionSize += 2 + PointerSize + getULEB128Size(PointerSize + 1);
932       AddressDelta = 0;
933     } else {
934       AddressDelta = (Row.Address - Address) / MinInstLength;
935     }
936
937     // FIXME: code copied and transfromed from
938     // MCDwarf.cpp::EmitDwarfLineTable. We should find a way to share
939     // this code, but the current compatibility requirement with
940     // classic dsymutil makes it hard. Revisit that once this
941     // requirement is dropped.
942
943     if (FileNum != Row.File) {
944       FileNum = Row.File;
945       MS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
946       MS->EmitULEB128IntValue(FileNum);
947       LineSectionSize += 1 + getULEB128Size(FileNum);
948     }
949     if (Column != Row.Column) {
950       Column = Row.Column;
951       MS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
952       MS->EmitULEB128IntValue(Column);
953       LineSectionSize += 1 + getULEB128Size(Column);
954     }
955
956     // FIXME: We should handle the discriminator here, but dsymutil
957     // doesn' consider it, thus ignore it for now.
958
959     if (Isa != Row.Isa) {
960       Isa = Row.Isa;
961       MS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
962       MS->EmitULEB128IntValue(Isa);
963       LineSectionSize += 1 + getULEB128Size(Isa);
964     }
965     if (IsStatement != Row.IsStmt) {
966       IsStatement = Row.IsStmt;
967       MS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
968       LineSectionSize += 1;
969     }
970     if (Row.BasicBlock) {
971       MS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
972       LineSectionSize += 1;
973     }
974
975     if (Row.PrologueEnd) {
976       MS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
977       LineSectionSize += 1;
978     }
979
980     if (Row.EpilogueBegin) {
981       MS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
982       LineSectionSize += 1;
983     }
984
985     int64_t LineDelta = int64_t(Row.Line) - LastLine;
986     if (!Row.EndSequence) {
987       MCDwarfLineAddr::Encode(*MC, Params, LineDelta, AddressDelta, EncodingOS);
988       MS->EmitBytes(EncodingOS.str());
989       LineSectionSize += EncodingBuffer.size();
990       EncodingBuffer.resize(0);
991       Address = Row.Address;
992       LastLine = Row.Line;
993       RowsSinceLastSequence++;
994     } else {
995       if (LineDelta) {
996         MS->EmitIntValue(dwarf::DW_LNS_advance_line, 1);
997         MS->EmitSLEB128IntValue(LineDelta);
998         LineSectionSize += 1 + getSLEB128Size(LineDelta);
999       }
1000       if (AddressDelta) {
1001         MS->EmitIntValue(dwarf::DW_LNS_advance_pc, 1);
1002         MS->EmitULEB128IntValue(AddressDelta);
1003         LineSectionSize += 1 + getULEB128Size(AddressDelta);
1004       }
1005       MCDwarfLineAddr::Encode(*MC, Params, INT64_MAX, 0, EncodingOS);
1006       MS->EmitBytes(EncodingOS.str());
1007       LineSectionSize += EncodingBuffer.size();
1008       EncodingBuffer.resize(0);
1009       Address = -1ULL;
1010       LastLine = FileNum = IsStatement = 1;
1011       RowsSinceLastSequence = Column = Isa = 0;
1012     }
1013   }
1014
1015   if (RowsSinceLastSequence) {
1016     MCDwarfLineAddr::Encode(*MC, Params, INT64_MAX, 0, EncodingOS);
1017     MS->EmitBytes(EncodingOS.str());
1018     LineSectionSize += EncodingBuffer.size();
1019     EncodingBuffer.resize(0);
1020   }
1021
1022   MS->EmitLabel(LineEndSym);
1023 }
1024
1025 /// \brief Emit the pubnames or pubtypes section contribution for \p
1026 /// Unit into \p Sec. The data is provided in \p Names.
1027 void DwarfStreamer::emitPubSectionForUnit(
1028     MCSection *Sec, StringRef SecName, const CompileUnit &Unit,
1029     const std::vector<CompileUnit::AccelInfo> &Names) {
1030   if (Names.empty())
1031     return;
1032
1033   // Start the dwarf pubnames section.
1034   Asm->OutStreamer->SwitchSection(Sec);
1035   MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + SecName + "_begin");
1036   MCSymbol *EndLabel = Asm->createTempSymbol("pub" + SecName + "_end");
1037
1038   bool HeaderEmitted = false;
1039   // Emit the pubnames for this compilation unit.
1040   for (const auto &Name : Names) {
1041     if (Name.SkipPubSection)
1042       continue;
1043
1044     if (!HeaderEmitted) {
1045       // Emit the header.
1046       Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); // Length
1047       Asm->OutStreamer->EmitLabel(BeginLabel);
1048       Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION); // Version
1049       Asm->EmitInt32(Unit.getStartOffset());      // Unit offset
1050       Asm->EmitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset()); // Size
1051       HeaderEmitted = true;
1052     }
1053     Asm->EmitInt32(Name.Die->getOffset());
1054     Asm->OutStreamer->EmitBytes(
1055         StringRef(Name.Name.data(), Name.Name.size() + 1));
1056   }
1057
1058   if (!HeaderEmitted)
1059     return;
1060   Asm->EmitInt32(0); // End marker.
1061   Asm->OutStreamer->EmitLabel(EndLabel);
1062 }
1063
1064 /// \brief Emit .debug_pubnames for \p Unit.
1065 void DwarfStreamer::emitPubNamesForUnit(const CompileUnit &Unit) {
1066   emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubNamesSection(),
1067                         "names", Unit, Unit.getPubnames());
1068 }
1069
1070 /// \brief Emit .debug_pubtypes for \p Unit.
1071 void DwarfStreamer::emitPubTypesForUnit(const CompileUnit &Unit) {
1072   emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubTypesSection(),
1073                         "types", Unit, Unit.getPubtypes());
1074 }
1075
1076 /// \brief Emit a CIE into the debug_frame section.
1077 void DwarfStreamer::emitCIE(StringRef CIEBytes) {
1078   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
1079
1080   MS->EmitBytes(CIEBytes);
1081   FrameSectionSize += CIEBytes.size();
1082 }
1083
1084 /// \brief Emit a FDE into the debug_frame section. \p FDEBytes
1085 /// contains the FDE data without the length, CIE offset and address
1086 /// which will be replaced with the paramter values.
1087 void DwarfStreamer::emitFDE(uint32_t CIEOffset, uint32_t AddrSize,
1088                             uint32_t Address, StringRef FDEBytes) {
1089   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
1090
1091   MS->EmitIntValue(FDEBytes.size() + 4 + AddrSize, 4);
1092   MS->EmitIntValue(CIEOffset, 4);
1093   MS->EmitIntValue(Address, AddrSize);
1094   MS->EmitBytes(FDEBytes);
1095   FrameSectionSize += FDEBytes.size() + 8 + AddrSize;
1096 }
1097
1098 /// \brief The core of the Dwarf linking logic.
1099 ///
1100 /// The link of the dwarf information from the object files will be
1101 /// driven by the selection of 'root DIEs', which are DIEs that
1102 /// describe variables or functions that are present in the linked
1103 /// binary (and thus have entries in the debug map). All the debug
1104 /// information that will be linked (the DIEs, but also the line
1105 /// tables, ranges, ...) is derived from that set of root DIEs.
1106 ///
1107 /// The root DIEs are identified because they contain relocations that
1108 /// correspond to a debug map entry at specific places (the low_pc for
1109 /// a function, the location for a variable). These relocations are
1110 /// called ValidRelocs in the DwarfLinker and are gathered as a very
1111 /// first step when we start processing a DebugMapObject.
1112 class DwarfLinker {
1113 public:
1114   DwarfLinker(StringRef OutputFilename, const LinkOptions &Options)
1115       : OutputFilename(OutputFilename), Options(Options),
1116         BinHolder(Options.Verbose), LastCIEOffset(0) {}
1117
1118   /// \brief Link the contents of the DebugMap.
1119   bool link(const DebugMap &);
1120
1121   void reportWarning(const Twine &Warning, const DWARFUnit *Unit = nullptr,
1122                      const DWARFDebugInfoEntryMinimal *DIE = nullptr) const;
1123
1124 private:
1125   /// \brief Called at the start of a debug object link.
1126   void startDebugObject(DWARFContext &, DebugMapObject &);
1127
1128   /// \brief Called at the end of a debug object link.
1129   void endDebugObject();
1130
1131   /// Keeps track of relocations.
1132   class RelocationManager {
1133     struct ValidReloc {
1134       uint32_t Offset;
1135       uint32_t Size;
1136       uint64_t Addend;
1137       const DebugMapObject::DebugMapEntry *Mapping;
1138
1139       ValidReloc(uint32_t Offset, uint32_t Size, uint64_t Addend,
1140                  const DebugMapObject::DebugMapEntry *Mapping)
1141           : Offset(Offset), Size(Size), Addend(Addend), Mapping(Mapping) {}
1142
1143       bool operator<(const ValidReloc &RHS) const {
1144         return Offset < RHS.Offset;
1145       }
1146     };
1147
1148     DwarfLinker &Linker;
1149
1150     /// \brief The valid relocations for the current DebugMapObject.
1151     /// This vector is sorted by relocation offset.
1152     std::vector<ValidReloc> ValidRelocs;
1153
1154     /// \brief Index into ValidRelocs of the next relocation to
1155     /// consider. As we walk the DIEs in acsending file offset and as
1156     /// ValidRelocs is sorted by file offset, keeping this index
1157     /// uptodate is all we have to do to have a cheap lookup during the
1158     /// root DIE selection and during DIE cloning.
1159     unsigned NextValidReloc;
1160
1161   public:
1162     RelocationManager(DwarfLinker &Linker)
1163         : Linker(Linker), NextValidReloc(0) {}
1164
1165     bool hasValidRelocs() const { return !ValidRelocs.empty(); }
1166     /// \brief Reset the NextValidReloc counter.
1167     void resetValidRelocs() { NextValidReloc = 0; }
1168
1169     /// \defgroup FindValidRelocations Translate debug map into a list
1170     /// of relevant relocations
1171     ///
1172     /// @{
1173     bool findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
1174                                     const DebugMapObject &DMO);
1175
1176     bool findValidRelocs(const object::SectionRef &Section,
1177                          const object::ObjectFile &Obj,
1178                          const DebugMapObject &DMO);
1179
1180     void findValidRelocsMachO(const object::SectionRef &Section,
1181                               const object::MachOObjectFile &Obj,
1182                               const DebugMapObject &DMO);
1183     /// @}
1184
1185     bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
1186                             CompileUnit::DIEInfo &Info);
1187
1188     bool applyValidRelocs(MutableArrayRef<char> Data, uint32_t BaseOffset,
1189                           bool isLittleEndian);
1190   };
1191
1192   /// \defgroup FindRootDIEs Find DIEs corresponding to debug map entries.
1193   ///
1194   /// @{
1195   /// \brief Recursively walk the \p DIE tree and look for DIEs to
1196   /// keep. Store that information in \p CU's DIEInfo.
1197   void lookForDIEsToKeep(RelocationManager &RelocMgr,
1198                          const DWARFDebugInfoEntryMinimal &DIE,
1199                          const DebugMapObject &DMO, CompileUnit &CU,
1200                          unsigned Flags);
1201
1202   /// If this compile unit is really a skeleton CU that points to a
1203   /// clang module, register it in ClangModules and return true.
1204   ///
1205   /// A skeleton CU is a CU without children, a DW_AT_gnu_dwo_name
1206   /// pointing to the module, and a DW_AT_gnu_dwo_id with the module
1207   /// hash.
1208   bool registerModuleReference(const DWARFDebugInfoEntryMinimal &CUDie,
1209                                const DWARFUnit &Unit, DebugMap &ModuleMap,
1210                                unsigned Indent = 0);
1211
1212   /// Recursively add the debug info in this clang module .pcm
1213   /// file (and all the modules imported by it in a bottom-up fashion)
1214   /// to Units.
1215   void loadClangModule(StringRef Filename, StringRef ModulePath,
1216                        StringRef ModuleName, uint64_t DwoId,
1217                        DebugMap &ModuleMap, unsigned Indent = 0);
1218
1219   /// \brief Flags passed to DwarfLinker::lookForDIEsToKeep
1220   enum TravesalFlags {
1221     TF_Keep = 1 << 0,            ///< Mark the traversed DIEs as kept.
1222     TF_InFunctionScope = 1 << 1, ///< Current scope is a fucntion scope.
1223     TF_DependencyWalk = 1 << 2,  ///< Walking the dependencies of a kept DIE.
1224     TF_ParentWalk = 1 << 3,      ///< Walking up the parents of a kept DIE.
1225     TF_ODR = 1 << 4,             ///< Use the ODR whhile keeping dependants.
1226     TF_SkipPC = 1 << 5,          ///< Skip all location attributes.
1227   };
1228
1229   /// \brief Mark the passed DIE as well as all the ones it depends on
1230   /// as kept.
1231   void keepDIEAndDependencies(RelocationManager &RelocMgr,
1232                                const DWARFDebugInfoEntryMinimal &DIE,
1233                                CompileUnit::DIEInfo &MyInfo,
1234                                const DebugMapObject &DMO, CompileUnit &CU,
1235                                bool UseODR);
1236
1237   unsigned shouldKeepDIE(RelocationManager &RelocMgr,
1238                          const DWARFDebugInfoEntryMinimal &DIE,
1239                          CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
1240                          unsigned Flags);
1241
1242   unsigned shouldKeepVariableDIE(RelocationManager &RelocMgr,
1243                                  const DWARFDebugInfoEntryMinimal &DIE,
1244                                  CompileUnit &Unit,
1245                                  CompileUnit::DIEInfo &MyInfo, unsigned Flags);
1246
1247   unsigned shouldKeepSubprogramDIE(RelocationManager &RelocMgr,
1248                                    const DWARFDebugInfoEntryMinimal &DIE,
1249                                    CompileUnit &Unit,
1250                                    CompileUnit::DIEInfo &MyInfo,
1251                                    unsigned Flags);
1252
1253   bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
1254                           CompileUnit::DIEInfo &Info);
1255   /// @}
1256
1257   /// \defgroup Linking Methods used to link the debug information
1258   ///
1259   /// @{
1260
1261   class DIECloner {
1262     DwarfLinker &Linker;
1263     RelocationManager &RelocMgr;
1264     /// Allocator used for all the DIEValue objects.
1265     BumpPtrAllocator &DIEAlloc;
1266     MutableArrayRef<CompileUnit> CompileUnits;
1267     LinkOptions Options;
1268
1269   public:
1270     DIECloner(DwarfLinker &Linker, RelocationManager &RelocMgr,
1271               BumpPtrAllocator &DIEAlloc,
1272               MutableArrayRef<CompileUnit> CompileUnits, LinkOptions &Options)
1273         : Linker(Linker), RelocMgr(RelocMgr), DIEAlloc(DIEAlloc),
1274           CompileUnits(CompileUnits), Options(Options) {}
1275
1276     /// Recursively clone \p InputDIE into an tree of DIE objects
1277     /// where useless (as decided by lookForDIEsToKeep()) bits have been
1278     /// stripped out and addresses have been rewritten according to the
1279     /// debug map.
1280     ///
1281     /// \param OutOffset is the offset the cloned DIE in the output
1282     /// compile unit.
1283     /// \param PCOffset (while cloning a function scope) is the offset
1284     /// applied to the entry point of the function to get the linked address.
1285     ///
1286     /// \returns the root of the cloned tree or null if nothing was selected.
1287     DIE *cloneDIE(const DWARFDebugInfoEntryMinimal &InputDIE, CompileUnit &U,
1288                   int64_t PCOffset, uint32_t OutOffset, unsigned Flags);
1289
1290     /// Construct the output DIE tree by cloning the DIEs we
1291     /// chose to keep above. If there are no valid relocs, then there's
1292     /// nothing to clone/emit.
1293     void cloneAllCompileUnits(DWARFContextInMemory &DwarfContext);
1294
1295   private:
1296     typedef DWARFAbbreviationDeclaration::AttributeSpec AttributeSpec;
1297
1298     /// Information gathered and exchanged between the various
1299     /// clone*Attributes helpers about the attributes of a particular DIE.
1300     struct AttributesInfo {
1301       const char *Name, *MangledName;         ///< Names.
1302       uint32_t NameOffset, MangledNameOffset; ///< Offsets in the string pool.
1303
1304       uint64_t OrigLowPc;  ///< Value of AT_low_pc in the input DIE
1305       uint64_t OrigHighPc; ///< Value of AT_high_pc in the input DIE
1306       int64_t PCOffset; ///< Offset to apply to PC addresses inside a function.
1307
1308       bool HasLowPc;      ///< Does the DIE have a low_pc attribute?
1309       bool IsDeclaration; ///< Is this DIE only a declaration?
1310
1311       AttributesInfo()
1312           : Name(nullptr), MangledName(nullptr), NameOffset(0),
1313             MangledNameOffset(0), OrigLowPc(UINT64_MAX), OrigHighPc(0),
1314             PCOffset(0), HasLowPc(false), IsDeclaration(false) {}
1315     };
1316
1317     /// Helper for cloneDIE.
1318     unsigned cloneAttribute(DIE &Die,
1319                             const DWARFDebugInfoEntryMinimal &InputDIE,
1320                             CompileUnit &U, const DWARFFormValue &Val,
1321                             const AttributeSpec AttrSpec, unsigned AttrSize,
1322                             AttributesInfo &AttrInfo);
1323
1324     /// Clone a string attribute described by \p AttrSpec and add
1325     /// it to \p Die.
1326     /// \returns the size of the new attribute.
1327     unsigned cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec,
1328                                   const DWARFFormValue &Val,
1329                                   const DWARFUnit &U);
1330
1331     /// Clone an attribute referencing another DIE and add
1332     /// it to \p Die.
1333     /// \returns the size of the new attribute.
1334     unsigned
1335     cloneDieReferenceAttribute(DIE &Die,
1336                                const DWARFDebugInfoEntryMinimal &InputDIE,
1337                                AttributeSpec AttrSpec, unsigned AttrSize,
1338                                const DWARFFormValue &Val, CompileUnit &Unit);
1339
1340     /// Clone an attribute referencing another DIE and add
1341     /// it to \p Die.
1342     /// \returns the size of the new attribute.
1343     unsigned cloneBlockAttribute(DIE &Die, AttributeSpec AttrSpec,
1344                                  const DWARFFormValue &Val, unsigned AttrSize);
1345
1346     /// Clone an attribute referencing another DIE and add
1347     /// it to \p Die.
1348     /// \returns the size of the new attribute.
1349     unsigned cloneAddressAttribute(DIE &Die, AttributeSpec AttrSpec,
1350                                    const DWARFFormValue &Val,
1351                                    const CompileUnit &Unit,
1352                                    AttributesInfo &Info);
1353
1354     /// Clone a scalar attribute  and add it to \p Die.
1355     /// \returns the size of the new attribute.
1356     unsigned cloneScalarAttribute(DIE &Die,
1357                                   const DWARFDebugInfoEntryMinimal &InputDIE,
1358                                   CompileUnit &U, AttributeSpec AttrSpec,
1359                                   const DWARFFormValue &Val, unsigned AttrSize,
1360                                   AttributesInfo &Info);
1361
1362     /// Get the potential name and mangled name for the entity
1363     /// described by \p Die and store them in \Info if they are not
1364     /// already there.
1365     /// \returns is a name was found.
1366     bool getDIENames(const DWARFDebugInfoEntryMinimal &Die, DWARFUnit &U,
1367                      AttributesInfo &Info);
1368
1369     /// Create a copy of abbreviation Abbrev.
1370     void copyAbbrev(const DWARFAbbreviationDeclaration &Abbrev, bool hasODR);
1371   };
1372
1373   /// \brief Assign an abbreviation number to \p Abbrev
1374   void AssignAbbrev(DIEAbbrev &Abbrev);
1375
1376   /// \brief FoldingSet that uniques the abbreviations.
1377   FoldingSet<DIEAbbrev> AbbreviationsSet;
1378   /// \brief Storage for the unique Abbreviations.
1379   /// This is passed to AsmPrinter::emitDwarfAbbrevs(), thus it cannot
1380   /// be changed to a vecot of unique_ptrs.
1381   std::vector<std::unique_ptr<DIEAbbrev>> Abbreviations;
1382
1383   /// \brief Compute and emit debug_ranges section for \p Unit, and
1384   /// patch the attributes referencing it.
1385   void patchRangesForUnit(const CompileUnit &Unit, DWARFContext &Dwarf) const;
1386
1387   /// \brief Generate and emit the DW_AT_ranges attribute for a
1388   /// compile_unit if it had one.
1389   void generateUnitRanges(CompileUnit &Unit) const;
1390
1391   /// \brief Extract the line tables fromt he original dwarf, extract
1392   /// the relevant parts according to the linked function ranges and
1393   /// emit the result in the debug_line section.
1394   void patchLineTableForUnit(CompileUnit &Unit, DWARFContext &OrigDwarf);
1395
1396   /// \brief Emit the accelerator entries for \p Unit.
1397   void emitAcceleratorEntriesForUnit(CompileUnit &Unit);
1398
1399   /// \brief Patch the frame info for an object file and emit it.
1400   void patchFrameInfoForObject(const DebugMapObject &, DWARFContext &,
1401                                unsigned AddressSize);
1402
1403   /// \brief DIELoc objects that need to be destructed (but not freed!).
1404   std::vector<DIELoc *> DIELocs;
1405   /// \brief DIEBlock objects that need to be destructed (but not freed!).
1406   std::vector<DIEBlock *> DIEBlocks;
1407   /// \brief Allocator used for all the DIEValue objects.
1408   BumpPtrAllocator DIEAlloc;
1409   /// @}
1410
1411   /// ODR Contexts for that link.
1412   DeclContextTree ODRContexts;
1413
1414   /// \defgroup Helpers Various helper methods.
1415   ///
1416   /// @{
1417   bool createStreamer(Triple TheTriple, StringRef OutputFilename);
1418
1419   /// \brief Attempt to load a debug object from disk.
1420   ErrorOr<const object::ObjectFile &> loadObject(BinaryHolder &BinaryHolder,
1421                                                  DebugMapObject &Obj,
1422                                                  const DebugMap &Map);
1423   /// @}
1424
1425   std::string OutputFilename;
1426   LinkOptions Options;
1427   BinaryHolder BinHolder;
1428   std::unique_ptr<DwarfStreamer> Streamer;
1429   uint64_t OutputDebugInfoSize;
1430   unsigned UnitID; ///< A unique ID that identifies each compile unit.
1431
1432   /// The units of the current debug map object.
1433   std::vector<CompileUnit> Units;
1434
1435   /// The debug map object currently under consideration.
1436   DebugMapObject *CurrentDebugObject;
1437
1438   /// \brief The Dwarf string pool
1439   NonRelocatableStringpool StringPool;
1440
1441   /// \brief This map is keyed by the entry PC of functions in that
1442   /// debug object and the associated value is a pair storing the
1443   /// corresponding end PC and the offset to apply to get the linked
1444   /// address.
1445   ///
1446   /// See startDebugObject() for a more complete description of its use.
1447   std::map<uint64_t, std::pair<uint64_t, int64_t>> Ranges;
1448
1449   /// \brief The CIEs that have been emitted in the output
1450   /// section. The actual CIE data serves a the key to this StringMap,
1451   /// this takes care of comparing the semantics of CIEs defined in
1452   /// different object files.
1453   StringMap<uint32_t> EmittedCIEs;
1454
1455   /// Offset of the last CIE that has been emitted in the output
1456   /// debug_frame section.
1457   uint32_t LastCIEOffset;
1458
1459   /// Mapping the PCM filename to the DwoId.
1460   StringMap<uint64_t> ClangModules;
1461 };
1462
1463 /// Similar to DWARFUnitSection::getUnitForOffset(), but returning our
1464 /// CompileUnit object instead.
1465 static CompileUnit *getUnitForOffset(MutableArrayRef<CompileUnit> Units,
1466                                      unsigned Offset) {
1467   auto CU =
1468       std::upper_bound(Units.begin(), Units.end(), Offset,
1469                        [](uint32_t LHS, const CompileUnit &RHS) {
1470                          return LHS < RHS.getOrigUnit().getNextUnitOffset();
1471                        });
1472   return CU != Units.end() ? &*CU : nullptr;
1473 }
1474
1475 /// Resolve the DIE attribute reference that has been
1476 /// extracted in \p RefValue. The resulting DIE migh be in another
1477 /// CompileUnit which is stored into \p ReferencedCU.
1478 /// \returns null if resolving fails for any reason.
1479 static const DWARFDebugInfoEntryMinimal *resolveDIEReference(
1480     const DwarfLinker &Linker, MutableArrayRef<CompileUnit> Units,
1481     const DWARFFormValue &RefValue, const DWARFUnit &Unit,
1482     const DWARFDebugInfoEntryMinimal &DIE, CompileUnit *&RefCU) {
1483   assert(RefValue.isFormClass(DWARFFormValue::FC_Reference));
1484   uint64_t RefOffset = *RefValue.getAsReference(&Unit);
1485
1486   if ((RefCU = getUnitForOffset(Units, RefOffset)))
1487     if (const auto *RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset))
1488       return RefDie;
1489
1490   Linker.reportWarning("could not find referenced DIE", &Unit, &DIE);
1491   return nullptr;
1492 }
1493
1494 /// \returns whether the passed \a Attr type might contain a DIE
1495 /// reference suitable for ODR uniquing.
1496 static bool isODRAttribute(uint16_t Attr) {
1497   switch (Attr) {
1498   default:
1499     return false;
1500   case dwarf::DW_AT_type:
1501   case dwarf::DW_AT_containing_type:
1502   case dwarf::DW_AT_specification:
1503   case dwarf::DW_AT_abstract_origin:
1504   case dwarf::DW_AT_import:
1505     return true;
1506   }
1507   llvm_unreachable("Improper attribute.");
1508 }
1509
1510 /// Set the last DIE/CU a context was seen in and, possibly invalidate
1511 /// the context if it is ambiguous.
1512 ///
1513 /// In the current implementation, we don't handle overloaded
1514 /// functions well, because the argument types are not taken into
1515 /// account when computing the DeclContext tree.
1516 ///
1517 /// Some of this is mitigated byt using mangled names that do contain
1518 /// the arguments types, but sometimes (eg. with function templates)
1519 /// we don't have that. In that case, just do not unique anything that
1520 /// refers to the contexts we are not able to distinguish.
1521 ///
1522 /// If a context that is not a namespace appears twice in the same CU,
1523 /// we know it is ambiguous. Make it invalid.
1524 bool DeclContext::setLastSeenDIE(CompileUnit &U,
1525                                  const DWARFDebugInfoEntryMinimal *Die) {
1526   if (LastSeenCompileUnitID == U.getUniqueID()) {
1527     DWARFUnit &OrigUnit = U.getOrigUnit();
1528     uint32_t FirstIdx = OrigUnit.getDIEIndex(LastSeenDIE);
1529     U.getInfo(FirstIdx).Ctxt = nullptr;
1530     return false;
1531   }
1532
1533   LastSeenCompileUnitID = U.getUniqueID();
1534   LastSeenDIE = Die;
1535   return true;
1536 }
1537
1538 PointerIntPair<DeclContext *, 1> DeclContextTree::getChildDeclContext(
1539     DeclContext &Context, const DWARFDebugInfoEntryMinimal *DIE, CompileUnit &U,
1540     NonRelocatableStringpool &StringPool, bool InClangModule) {
1541   unsigned Tag = DIE->getTag();
1542
1543   // FIXME: dsymutil-classic compat: We should bail out here if we
1544   // have a specification or an abstract_origin. We will get the
1545   // parent context wrong here.
1546
1547   switch (Tag) {
1548   default:
1549     // By default stop gathering child contexts.
1550     return PointerIntPair<DeclContext *, 1>(nullptr);
1551   case dwarf::DW_TAG_module:
1552     break;
1553   case dwarf::DW_TAG_compile_unit:
1554     return PointerIntPair<DeclContext *, 1>(&Context);
1555   case dwarf::DW_TAG_subprogram:
1556     // Do not unique anything inside CU local functions.
1557     if ((Context.getTag() == dwarf::DW_TAG_namespace ||
1558          Context.getTag() == dwarf::DW_TAG_compile_unit) &&
1559         !DIE->getAttributeValueAsUnsignedConstant(&U.getOrigUnit(),
1560                                                   dwarf::DW_AT_external, 0))
1561       return PointerIntPair<DeclContext *, 1>(nullptr);
1562   // Fallthrough
1563   case dwarf::DW_TAG_member:
1564   case dwarf::DW_TAG_namespace:
1565   case dwarf::DW_TAG_structure_type:
1566   case dwarf::DW_TAG_class_type:
1567   case dwarf::DW_TAG_union_type:
1568   case dwarf::DW_TAG_enumeration_type:
1569   case dwarf::DW_TAG_typedef:
1570     // Artificial things might be ambiguous, because they might be
1571     // created on demand. For example implicitely defined constructors
1572     // are ambiguous because of the way we identify contexts, and they
1573     // won't be generated everytime everywhere.
1574     if (DIE->getAttributeValueAsUnsignedConstant(&U.getOrigUnit(),
1575                                                  dwarf::DW_AT_artificial, 0))
1576       return PointerIntPair<DeclContext *, 1>(nullptr);
1577     break;
1578   }
1579
1580   const char *Name = DIE->getName(&U.getOrigUnit(), DINameKind::LinkageName);
1581   const char *ShortName = DIE->getName(&U.getOrigUnit(), DINameKind::ShortName);
1582   StringRef NameRef;
1583   StringRef ShortNameRef;
1584   StringRef FileRef;
1585
1586   if (Name)
1587     NameRef = StringPool.internString(Name);
1588   else if (Tag == dwarf::DW_TAG_namespace)
1589     // FIXME: For dsymutil-classic compatibility. I think uniquing
1590     // within anonymous namespaces is wrong. There is no ODR guarantee
1591     // there.
1592     NameRef = StringPool.internString("(anonymous namespace)");
1593
1594   if (ShortName && ShortName != Name)
1595     ShortNameRef = StringPool.internString(ShortName);
1596   else
1597     ShortNameRef = NameRef;
1598
1599   if (Tag != dwarf::DW_TAG_class_type && Tag != dwarf::DW_TAG_structure_type &&
1600       Tag != dwarf::DW_TAG_union_type &&
1601       Tag != dwarf::DW_TAG_enumeration_type && NameRef.empty())
1602     return PointerIntPair<DeclContext *, 1>(nullptr);
1603
1604   std::string File;
1605   unsigned Line = 0;
1606   unsigned ByteSize = UINT32_MAX;
1607
1608   if (!InClangModule) {
1609     // Gather some discriminating data about the DeclContext we will be
1610     // creating: File, line number and byte size. This shouldn't be
1611     // necessary, because the ODR is just about names, but given that we
1612     // do some approximations with overloaded functions and anonymous
1613     // namespaces, use these additional data points to make the process
1614     // safer.  This is disabled for clang modules, because forward
1615     // declarations of module-defined types do not have a file and line.
1616     ByteSize = DIE->getAttributeValueAsUnsignedConstant(
1617         &U.getOrigUnit(), dwarf::DW_AT_byte_size, UINT64_MAX);
1618     if (Tag != dwarf::DW_TAG_namespace || !Name) {
1619       if (unsigned FileNum = DIE->getAttributeValueAsUnsignedConstant(
1620               &U.getOrigUnit(), dwarf::DW_AT_decl_file, 0)) {
1621         if (const auto *LT = U.getOrigUnit().getContext().getLineTableForUnit(
1622                 &U.getOrigUnit())) {
1623           // FIXME: dsymutil-classic compatibility. I'd rather not
1624           // unique anything in anonymous namespaces, but if we do, then
1625           // verify that the file and line correspond.
1626           if (!Name && Tag == dwarf::DW_TAG_namespace)
1627             FileNum = 1;
1628
1629           // FIXME: Passing U.getOrigUnit().getCompilationDir()
1630           // instead of "" would allow more uniquing, but for now, do
1631           // it this way to match dsymutil-classic.
1632           if (LT->getFileNameByIndex(
1633                   FileNum, "",
1634                   DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
1635                   File)) {
1636             Line = DIE->getAttributeValueAsUnsignedConstant(
1637                 &U.getOrigUnit(), dwarf::DW_AT_decl_line, 0);
1638 #ifdef HAVE_REALPATH
1639             // Cache the resolved paths, because calling realpath is expansive.
1640             if (const char *ResolvedPath = U.getResolvedPath(FileNum)) {
1641               File = ResolvedPath;
1642             } else {
1643               char RealPath[PATH_MAX + 1];
1644               RealPath[PATH_MAX] = 0;
1645               if (::realpath(File.c_str(), RealPath))
1646                 File = RealPath;
1647               U.setResolvedPath(FileNum, File);
1648             }
1649 #endif
1650             FileRef = StringPool.internString(File);
1651           }
1652         }
1653       }
1654     }
1655   }
1656
1657   if (!Line && NameRef.empty())
1658     return PointerIntPair<DeclContext *, 1>(nullptr);
1659
1660   // We hash NameRef, which is the mangled name, in order to get most
1661   // overloaded functions resolve correctly.
1662   //
1663   // Strictly speaking, hashing the Tag is only necessary for a
1664   // DW_TAG_module, to prevent uniquing of a module and a namespace
1665   // with the same name.
1666   //
1667   // FIXME: dsymutil-classic won't unique the same type presented
1668   // once as a struct and once as a class. Using the Tag in the fully
1669   // qualified name hash to get the same effect.
1670   unsigned Hash = hash_combine(Context.getQualifiedNameHash(), Tag, NameRef);
1671
1672   // FIXME: dsymutil-classic compatibility: when we don't have a name,
1673   // use the filename.
1674   if (Tag == dwarf::DW_TAG_namespace && NameRef == "(anonymous namespace)")
1675     Hash = hash_combine(Hash, FileRef);
1676
1677   // Now look if this context already exists.
1678   DeclContext Key(Hash, Line, ByteSize, Tag, NameRef, FileRef, Context);
1679   auto ContextIter = Contexts.find(&Key);
1680
1681   if (ContextIter == Contexts.end()) {
1682     // The context wasn't found.
1683     bool Inserted;
1684     DeclContext *NewContext =
1685         new (Allocator) DeclContext(Hash, Line, ByteSize, Tag, NameRef, FileRef,
1686                                     Context, DIE, U.getUniqueID());
1687     std::tie(ContextIter, Inserted) = Contexts.insert(NewContext);
1688     assert(Inserted && "Failed to insert DeclContext");
1689     (void)Inserted;
1690   } else if (Tag != dwarf::DW_TAG_namespace &&
1691              !(*ContextIter)->setLastSeenDIE(U, DIE)) {
1692     // The context was found, but it is ambiguous with another context
1693     // in the same file. Mark it invalid.
1694     return PointerIntPair<DeclContext *, 1>(*ContextIter, /* Invalid= */ 1);
1695   }
1696
1697   assert(ContextIter != Contexts.end());
1698   // FIXME: dsymutil-classic compatibility. Union types aren't
1699   // uniques, but their children might be.
1700   if ((Tag == dwarf::DW_TAG_subprogram &&
1701        Context.getTag() != dwarf::DW_TAG_structure_type &&
1702        Context.getTag() != dwarf::DW_TAG_class_type) ||
1703       (Tag == dwarf::DW_TAG_union_type))
1704     return PointerIntPair<DeclContext *, 1>(*ContextIter, /* Invalid= */ 1);
1705
1706   return PointerIntPair<DeclContext *, 1>(*ContextIter);
1707 }
1708
1709 bool DwarfLinker::DIECloner::getDIENames(const DWARFDebugInfoEntryMinimal &Die,
1710                                          DWARFUnit &U, AttributesInfo &Info) {
1711   // FIXME: a bit wasteful as the first getName might return the
1712   // short name.
1713   if (!Info.MangledName &&
1714       (Info.MangledName = Die.getName(&U, DINameKind::LinkageName)))
1715     Info.MangledNameOffset =
1716         Linker.StringPool.getStringOffset(Info.MangledName);
1717
1718   if (!Info.Name && (Info.Name = Die.getName(&U, DINameKind::ShortName)))
1719     Info.NameOffset = Linker.StringPool.getStringOffset(Info.Name);
1720
1721   return Info.Name || Info.MangledName;
1722 }
1723
1724 /// \brief Report a warning to the user, optionaly including
1725 /// information about a specific \p DIE related to the warning.
1726 void DwarfLinker::reportWarning(const Twine &Warning, const DWARFUnit *Unit,
1727                                 const DWARFDebugInfoEntryMinimal *DIE) const {
1728   StringRef Context = "<debug map>";
1729   if (CurrentDebugObject)
1730     Context = CurrentDebugObject->getObjectFilename();
1731   warn(Warning, Context);
1732
1733   if (!Options.Verbose || !DIE)
1734     return;
1735
1736   errs() << "    in DIE:\n";
1737   DIE->dump(errs(), const_cast<DWARFUnit *>(Unit), 0 /* RecurseDepth */,
1738             6 /* Indent */);
1739 }
1740
1741 bool DwarfLinker::createStreamer(Triple TheTriple, StringRef OutputFilename) {
1742   if (Options.NoOutput)
1743     return true;
1744
1745   Streamer = llvm::make_unique<DwarfStreamer>();
1746   return Streamer->init(TheTriple, OutputFilename);
1747 }
1748
1749 /// Recursive helper to build the global DeclContext information and
1750 /// gather the child->parent relationships in the original compile unit.
1751 ///
1752 /// \return true when this DIE and all of its children are only
1753 /// forward declarations to types defined in external clang modules
1754 /// (i.e., forward declarations that are children of a DW_TAG_module).
1755 static bool analyzeContextInfo(const DWARFDebugInfoEntryMinimal *DIE,
1756                                unsigned ParentIdx, CompileUnit &CU,
1757                                DeclContext *CurrentDeclContext,
1758                                NonRelocatableStringpool &StringPool,
1759                                DeclContextTree &Contexts,
1760                                bool InImportedModule = false) {
1761   unsigned MyIdx = CU.getOrigUnit().getDIEIndex(DIE);
1762   CompileUnit::DIEInfo &Info = CU.getInfo(MyIdx);
1763
1764   // Clang imposes an ODR on modules(!) regardless of the language:
1765   //  "The module-id should consist of only a single identifier,
1766   //   which provides the name of the module being defined. Each
1767   //   module shall have a single definition."
1768   //
1769   // This does not extend to the types inside the modules:
1770   //  "[I]n C, this implies that if two structs are defined in
1771   //   different submodules with the same name, those two types are
1772   //   distinct types (but may be compatible types if their
1773   //   definitions match)."
1774   //
1775   // We treat non-C++ modules like namespaces for this reason.
1776   if (DIE->getTag() == dwarf::DW_TAG_module && ParentIdx == 0 &&
1777       DIE->getAttributeValueAsString(&CU.getOrigUnit(), dwarf::DW_AT_name,
1778                                      "") != CU.getClangModuleName()) {
1779     InImportedModule = true;
1780   }
1781
1782   Info.ParentIdx = ParentIdx;
1783   bool InClangModule = CU.isClangModule() || InImportedModule;
1784   if (CU.hasODR() || InClangModule) {
1785     if (CurrentDeclContext) {
1786       auto PtrInvalidPair = Contexts.getChildDeclContext(
1787           *CurrentDeclContext, DIE, CU, StringPool, InClangModule);
1788       CurrentDeclContext = PtrInvalidPair.getPointer();
1789       Info.Ctxt =
1790           PtrInvalidPair.getInt() ? nullptr : PtrInvalidPair.getPointer();
1791     } else
1792       Info.Ctxt = CurrentDeclContext = nullptr;
1793   }
1794
1795   Info.Prune = InImportedModule;
1796   if (DIE->hasChildren())
1797     for (auto *Child = DIE->getFirstChild(); Child && !Child->isNULL();
1798          Child = Child->getSibling())
1799       Info.Prune &= analyzeContextInfo(Child, MyIdx, CU, CurrentDeclContext,
1800                                        StringPool, Contexts, InImportedModule);
1801
1802   // Prune this DIE if it is either a forward declaration inside a
1803   // DW_TAG_module or a DW_TAG_module that contains nothing but
1804   // forward declarations.
1805   Info.Prune &= (DIE->getTag() == dwarf::DW_TAG_module) ||
1806                 DIE->getAttributeValueAsUnsignedConstant(
1807                     &CU.getOrigUnit(), dwarf::DW_AT_declaration, 0);
1808
1809   // Don't prune it if there is no definition for the DIE.
1810   Info.Prune &= Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset();
1811
1812   return Info.Prune;
1813 }
1814
1815 static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) {
1816   switch (Tag) {
1817   default:
1818     return false;
1819   case dwarf::DW_TAG_subprogram:
1820   case dwarf::DW_TAG_lexical_block:
1821   case dwarf::DW_TAG_subroutine_type:
1822   case dwarf::DW_TAG_structure_type:
1823   case dwarf::DW_TAG_class_type:
1824   case dwarf::DW_TAG_union_type:
1825     return true;
1826   }
1827   llvm_unreachable("Invalid Tag");
1828 }
1829
1830 static unsigned getRefAddrSize(const DWARFUnit &U) {
1831   if (U.getVersion() == 2)
1832     return U.getAddressByteSize();
1833   return 4;
1834 }
1835
1836 void DwarfLinker::startDebugObject(DWARFContext &Dwarf, DebugMapObject &Obj) {
1837   Units.reserve(Dwarf.getNumCompileUnits());
1838   // Iterate over the debug map entries and put all the ones that are
1839   // functions (because they have a size) into the Ranges map. This
1840   // map is very similar to the FunctionRanges that are stored in each
1841   // unit, with 2 notable differences:
1842   //  - obviously this one is global, while the other ones are per-unit.
1843   //  - this one contains not only the functions described in the DIE
1844   // tree, but also the ones that are only in the debug map.
1845   // The latter information is required to reproduce dsymutil's logic
1846   // while linking line tables. The cases where this information
1847   // matters look like bugs that need to be investigated, but for now
1848   // we need to reproduce dsymutil's behavior.
1849   // FIXME: Once we understood exactly if that information is needed,
1850   // maybe totally remove this (or try to use it to do a real
1851   // -gline-tables-only on Darwin.
1852   for (const auto &Entry : Obj.symbols()) {
1853     const auto &Mapping = Entry.getValue();
1854     if (Mapping.Size)
1855       Ranges[Mapping.ObjectAddress] = std::make_pair(
1856           Mapping.ObjectAddress + Mapping.Size,
1857           int64_t(Mapping.BinaryAddress) - Mapping.ObjectAddress);
1858   }
1859 }
1860
1861 void DwarfLinker::endDebugObject() {
1862   Units.clear();
1863   Ranges.clear();
1864
1865   for (auto I = DIEBlocks.begin(), E = DIEBlocks.end(); I != E; ++I)
1866     (*I)->~DIEBlock();
1867   for (auto I = DIELocs.begin(), E = DIELocs.end(); I != E; ++I)
1868     (*I)->~DIELoc();
1869
1870   DIEBlocks.clear();
1871   DIELocs.clear();
1872   DIEAlloc.Reset();
1873 }
1874
1875 /// \brief Iterate over the relocations of the given \p Section and
1876 /// store the ones that correspond to debug map entries into the
1877 /// ValidRelocs array.
1878 void DwarfLinker::RelocationManager::
1879 findValidRelocsMachO(const object::SectionRef &Section,
1880                      const object::MachOObjectFile &Obj,
1881                      const DebugMapObject &DMO) {
1882   StringRef Contents;
1883   Section.getContents(Contents);
1884   DataExtractor Data(Contents, Obj.isLittleEndian(), 0);
1885
1886   for (const object::RelocationRef &Reloc : Section.relocations()) {
1887     object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl();
1888     MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef);
1889     unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc);
1890     uint64_t Offset64 = Reloc.getOffset();
1891     if ((RelocSize != 4 && RelocSize != 8)) {
1892       Linker.reportWarning(" unsupported relocation in debug_info section.");
1893       continue;
1894     }
1895     uint32_t Offset = Offset64;
1896     // Mach-o uses REL relocations, the addend is at the relocation offset.
1897     uint64_t Addend = Data.getUnsigned(&Offset, RelocSize);
1898
1899     auto Sym = Reloc.getSymbol();
1900     if (Sym != Obj.symbol_end()) {
1901       ErrorOr<StringRef> SymbolName = Sym->getName();
1902       if (!SymbolName) {
1903         Linker.reportWarning("error getting relocation symbol name.");
1904         continue;
1905       }
1906       if (const auto *Mapping = DMO.lookupSymbol(*SymbolName))
1907         ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping);
1908     } else if (const auto *Mapping = DMO.lookupObjectAddress(Addend)) {
1909       // Do not store the addend. The addend was the address of the
1910       // symbol in the object file, the address in the binary that is
1911       // stored in the debug map doesn't need to be offseted.
1912       ValidRelocs.emplace_back(Offset64, RelocSize, 0, Mapping);
1913     }
1914   }
1915 }
1916
1917 /// \brief Dispatch the valid relocation finding logic to the
1918 /// appropriate handler depending on the object file format.
1919 bool DwarfLinker::RelocationManager::findValidRelocs(
1920     const object::SectionRef &Section, const object::ObjectFile &Obj,
1921     const DebugMapObject &DMO) {
1922   // Dispatch to the right handler depending on the file type.
1923   if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj))
1924     findValidRelocsMachO(Section, *MachOObj, DMO);
1925   else
1926     Linker.reportWarning(Twine("unsupported object file type: ") +
1927                          Obj.getFileName());
1928
1929   if (ValidRelocs.empty())
1930     return false;
1931
1932   // Sort the relocations by offset. We will walk the DIEs linearly in
1933   // the file, this allows us to just keep an index in the relocation
1934   // array that we advance during our walk, rather than resorting to
1935   // some associative container. See DwarfLinker::NextValidReloc.
1936   std::sort(ValidRelocs.begin(), ValidRelocs.end());
1937   return true;
1938 }
1939
1940 /// \brief Look for relocations in the debug_info section that match
1941 /// entries in the debug map. These relocations will drive the Dwarf
1942 /// link by indicating which DIEs refer to symbols present in the
1943 /// linked binary.
1944 /// \returns wether there are any valid relocations in the debug info.
1945 bool DwarfLinker::RelocationManager::
1946 findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
1947                            const DebugMapObject &DMO) {
1948   // Find the debug_info section.
1949   for (const object::SectionRef &Section : Obj.sections()) {
1950     StringRef SectionName;
1951     Section.getName(SectionName);
1952     SectionName = SectionName.substr(SectionName.find_first_not_of("._"));
1953     if (SectionName != "debug_info")
1954       continue;
1955     return findValidRelocs(Section, Obj, DMO);
1956   }
1957   return false;
1958 }
1959
1960 /// \brief Checks that there is a relocation against an actual debug
1961 /// map entry between \p StartOffset and \p NextOffset.
1962 ///
1963 /// This function must be called with offsets in strictly ascending
1964 /// order because it never looks back at relocations it already 'went past'.
1965 /// \returns true and sets Info.InDebugMap if it is the case.
1966 bool DwarfLinker::RelocationManager::
1967 hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
1968                    CompileUnit::DIEInfo &Info) {
1969   assert(NextValidReloc == 0 ||
1970          StartOffset > ValidRelocs[NextValidReloc - 1].Offset);
1971   if (NextValidReloc >= ValidRelocs.size())
1972     return false;
1973
1974   uint64_t RelocOffset = ValidRelocs[NextValidReloc].Offset;
1975
1976   // We might need to skip some relocs that we didn't consider. For
1977   // example the high_pc of a discarded DIE might contain a reloc that
1978   // is in the list because it actually corresponds to the start of a
1979   // function that is in the debug map.
1980   while (RelocOffset < StartOffset && NextValidReloc < ValidRelocs.size() - 1)
1981     RelocOffset = ValidRelocs[++NextValidReloc].Offset;
1982
1983   if (RelocOffset < StartOffset || RelocOffset >= EndOffset)
1984     return false;
1985
1986   const auto &ValidReloc = ValidRelocs[NextValidReloc++];
1987   const auto &Mapping = ValidReloc.Mapping->getValue();
1988   if (Linker.Options.Verbose)
1989     outs() << "Found valid debug map entry: " << ValidReloc.Mapping->getKey()
1990            << " " << format("\t%016" PRIx64 " => %016" PRIx64,
1991                             uint64_t(Mapping.ObjectAddress),
1992                             uint64_t(Mapping.BinaryAddress));
1993
1994   Info.AddrAdjust = int64_t(Mapping.BinaryAddress) + ValidReloc.Addend -
1995                     Mapping.ObjectAddress;
1996   Info.InDebugMap = true;
1997   return true;
1998 }
1999
2000 /// \brief Get the starting and ending (exclusive) offset for the
2001 /// attribute with index \p Idx descibed by \p Abbrev. \p Offset is
2002 /// supposed to point to the position of the first attribute described
2003 /// by \p Abbrev.
2004 /// \return [StartOffset, EndOffset) as a pair.
2005 static std::pair<uint32_t, uint32_t>
2006 getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx,
2007                     unsigned Offset, const DWARFUnit &Unit) {
2008   DataExtractor Data = Unit.getDebugInfoExtractor();
2009
2010   for (unsigned i = 0; i < Idx; ++i)
2011     DWARFFormValue::skipValue(Abbrev->getFormByIndex(i), Data, &Offset, &Unit);
2012
2013   uint32_t End = Offset;
2014   DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End, &Unit);
2015
2016   return std::make_pair(Offset, End);
2017 }
2018
2019 /// \brief Check if a variable describing DIE should be kept.
2020 /// \returns updated TraversalFlags.
2021 unsigned DwarfLinker::shouldKeepVariableDIE(RelocationManager &RelocMgr,
2022                                             const DWARFDebugInfoEntryMinimal &DIE,
2023                                             CompileUnit &Unit,
2024                                             CompileUnit::DIEInfo &MyInfo,
2025                                             unsigned Flags) {
2026   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
2027
2028   // Global variables with constant value can always be kept.
2029   if (!(Flags & TF_InFunctionScope) &&
2030       Abbrev->findAttributeIndex(dwarf::DW_AT_const_value) != -1U) {
2031     MyInfo.InDebugMap = true;
2032     return Flags | TF_Keep;
2033   }
2034
2035   uint32_t LocationIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_location);
2036   if (LocationIdx == -1U)
2037     return Flags;
2038
2039   uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
2040   const DWARFUnit &OrigUnit = Unit.getOrigUnit();
2041   uint32_t LocationOffset, LocationEndOffset;
2042   std::tie(LocationOffset, LocationEndOffset) =
2043       getAttributeOffsets(Abbrev, LocationIdx, Offset, OrigUnit);
2044
2045   // See if there is a relocation to a valid debug map entry inside
2046   // this variable's location. The order is important here. We want to
2047   // always check in the variable has a valid relocation, so that the
2048   // DIEInfo is filled. However, we don't want a static variable in a
2049   // function to force us to keep the enclosing function.
2050   if (!RelocMgr.hasValidRelocation(LocationOffset, LocationEndOffset, MyInfo) ||
2051       (Flags & TF_InFunctionScope))
2052     return Flags;
2053
2054   if (Options.Verbose)
2055     DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */);
2056
2057   return Flags | TF_Keep;
2058 }
2059
2060 /// \brief Check if a function describing DIE should be kept.
2061 /// \returns updated TraversalFlags.
2062 unsigned DwarfLinker::shouldKeepSubprogramDIE(
2063     RelocationManager &RelocMgr,
2064     const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit,
2065     CompileUnit::DIEInfo &MyInfo, unsigned Flags) {
2066   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
2067
2068   Flags |= TF_InFunctionScope;
2069
2070   uint32_t LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc);
2071   if (LowPcIdx == -1U)
2072     return Flags;
2073
2074   uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
2075   const DWARFUnit &OrigUnit = Unit.getOrigUnit();
2076   uint32_t LowPcOffset, LowPcEndOffset;
2077   std::tie(LowPcOffset, LowPcEndOffset) =
2078       getAttributeOffsets(Abbrev, LowPcIdx, Offset, OrigUnit);
2079
2080   uint64_t LowPc =
2081       DIE.getAttributeValueAsAddress(&OrigUnit, dwarf::DW_AT_low_pc, -1ULL);
2082   assert(LowPc != -1ULL && "low_pc attribute is not an address.");
2083   if (LowPc == -1ULL ||
2084       !RelocMgr.hasValidRelocation(LowPcOffset, LowPcEndOffset, MyInfo))
2085     return Flags;
2086
2087   if (Options.Verbose)
2088     DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */);
2089
2090   Flags |= TF_Keep;
2091
2092   DWARFFormValue HighPcValue;
2093   if (!DIE.getAttributeValue(&OrigUnit, dwarf::DW_AT_high_pc, HighPcValue)) {
2094     reportWarning("Function without high_pc. Range will be discarded.\n",
2095                   &OrigUnit, &DIE);
2096     return Flags;
2097   }
2098
2099   uint64_t HighPc;
2100   if (HighPcValue.isFormClass(DWARFFormValue::FC_Address)) {
2101     HighPc = *HighPcValue.getAsAddress(&OrigUnit);
2102   } else {
2103     assert(HighPcValue.isFormClass(DWARFFormValue::FC_Constant));
2104     HighPc = LowPc + *HighPcValue.getAsUnsignedConstant();
2105   }
2106
2107   // Replace the debug map range with a more accurate one.
2108   Ranges[LowPc] = std::make_pair(HighPc, MyInfo.AddrAdjust);
2109   Unit.addFunctionRange(LowPc, HighPc, MyInfo.AddrAdjust);
2110   return Flags;
2111 }
2112
2113 /// \brief Check if a DIE should be kept.
2114 /// \returns updated TraversalFlags.
2115 unsigned DwarfLinker::shouldKeepDIE(RelocationManager &RelocMgr,
2116                                     const DWARFDebugInfoEntryMinimal &DIE,
2117                                     CompileUnit &Unit,
2118                                     CompileUnit::DIEInfo &MyInfo,
2119                                     unsigned Flags) {
2120   switch (DIE.getTag()) {
2121   case dwarf::DW_TAG_constant:
2122   case dwarf::DW_TAG_variable:
2123     return shouldKeepVariableDIE(RelocMgr, DIE, Unit, MyInfo, Flags);
2124   case dwarf::DW_TAG_subprogram:
2125     return shouldKeepSubprogramDIE(RelocMgr, DIE, Unit, MyInfo, Flags);
2126   case dwarf::DW_TAG_module:
2127   case dwarf::DW_TAG_imported_module:
2128   case dwarf::DW_TAG_imported_declaration:
2129   case dwarf::DW_TAG_imported_unit:
2130     // We always want to keep these.
2131     return Flags | TF_Keep;
2132   }
2133
2134   return Flags;
2135 }
2136
2137 /// \brief Mark the passed DIE as well as all the ones it depends on
2138 /// as kept.
2139 ///
2140 /// This function is called by lookForDIEsToKeep on DIEs that are
2141 /// newly discovered to be needed in the link. It recursively calls
2142 /// back to lookForDIEsToKeep while adding TF_DependencyWalk to the
2143 /// TraversalFlags to inform it that it's not doing the primary DIE
2144 /// tree walk.
2145 void DwarfLinker::keepDIEAndDependencies(RelocationManager &RelocMgr,
2146                                           const DWARFDebugInfoEntryMinimal &Die,
2147                                           CompileUnit::DIEInfo &MyInfo,
2148                                           const DebugMapObject &DMO,
2149                                           CompileUnit &CU, bool UseODR) {
2150   const DWARFUnit &Unit = CU.getOrigUnit();
2151   MyInfo.Keep = true;
2152
2153   // First mark all the parent chain as kept.
2154   unsigned AncestorIdx = MyInfo.ParentIdx;
2155   while (!CU.getInfo(AncestorIdx).Keep) {
2156     unsigned ODRFlag = UseODR ? TF_ODR : 0;
2157     lookForDIEsToKeep(RelocMgr, *Unit.getDIEAtIndex(AncestorIdx), DMO, CU,
2158                       TF_ParentWalk | TF_Keep | TF_DependencyWalk | ODRFlag);
2159     AncestorIdx = CU.getInfo(AncestorIdx).ParentIdx;
2160   }
2161
2162   // Then we need to mark all the DIEs referenced by this DIE's
2163   // attributes as kept.
2164   DataExtractor Data = Unit.getDebugInfoExtractor();
2165   const auto *Abbrev = Die.getAbbreviationDeclarationPtr();
2166   uint32_t Offset = Die.getOffset() + getULEB128Size(Abbrev->getCode());
2167
2168   // Mark all DIEs referenced through atttributes as kept.
2169   for (const auto &AttrSpec : Abbrev->attributes()) {
2170     DWARFFormValue Val(AttrSpec.Form);
2171
2172     if (!Val.isFormClass(DWARFFormValue::FC_Reference)) {
2173       DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset, &Unit);
2174       continue;
2175     }
2176
2177     Val.extractValue(Data, &Offset, &Unit);
2178     CompileUnit *ReferencedCU;
2179     if (const auto *RefDIE =
2180             resolveDIEReference(*this, MutableArrayRef<CompileUnit>(Units), Val,
2181                                 Unit, Die, ReferencedCU)) {
2182       uint32_t RefIdx = ReferencedCU->getOrigUnit().getDIEIndex(RefDIE);
2183       CompileUnit::DIEInfo &Info = ReferencedCU->getInfo(RefIdx);
2184       // If the referenced DIE has a DeclContext that has already been
2185       // emitted, then do not keep the one in this CU. We'll link to
2186       // the canonical DIE in cloneDieReferenceAttribute.
2187       // FIXME: compatibility with dsymutil-classic. UseODR shouldn't
2188       // be necessary and could be advantageously replaced by
2189       // ReferencedCU->hasODR() && CU.hasODR().
2190       // FIXME: compatibility with dsymutil-classic. There is no
2191       // reason not to unique ref_addr references.
2192       if (AttrSpec.Form != dwarf::DW_FORM_ref_addr && UseODR && Info.Ctxt &&
2193           Info.Ctxt != ReferencedCU->getInfo(Info.ParentIdx).Ctxt &&
2194           Info.Ctxt->getCanonicalDIEOffset() && isODRAttribute(AttrSpec.Attr))
2195         continue;
2196
2197       // Keep a module forward declaration if there is no definition.
2198       if (!(isODRAttribute(AttrSpec.Attr) && Info.Ctxt &&
2199             Info.Ctxt->getCanonicalDIEOffset()))
2200         Info.Prune = false;
2201
2202       unsigned ODRFlag = UseODR ? TF_ODR : 0;
2203       lookForDIEsToKeep(RelocMgr, *RefDIE, DMO, *ReferencedCU,
2204                         TF_Keep | TF_DependencyWalk | ODRFlag);
2205     }
2206   }
2207 }
2208
2209 /// \brief Recursively walk the \p DIE tree and look for DIEs to
2210 /// keep. Store that information in \p CU's DIEInfo.
2211 ///
2212 /// This function is the entry point of the DIE selection
2213 /// algorithm. It is expected to walk the DIE tree in file order and
2214 /// (though the mediation of its helper) call hasValidRelocation() on
2215 /// each DIE that might be a 'root DIE' (See DwarfLinker class
2216 /// comment).
2217 /// While walking the dependencies of root DIEs, this function is
2218 /// also called, but during these dependency walks the file order is
2219 /// not respected. The TF_DependencyWalk flag tells us which kind of
2220 /// traversal we are currently doing.
2221 void DwarfLinker::lookForDIEsToKeep(RelocationManager &RelocMgr,
2222                                     const DWARFDebugInfoEntryMinimal &Die,
2223                                     const DebugMapObject &DMO, CompileUnit &CU,
2224                                     unsigned Flags) {
2225   unsigned Idx = CU.getOrigUnit().getDIEIndex(&Die);
2226   CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
2227   bool AlreadyKept = MyInfo.Keep;
2228   if (MyInfo.Prune)
2229     return;
2230
2231   // If the Keep flag is set, we are marking a required DIE's
2232   // dependencies. If our target is already marked as kept, we're all
2233   // set.
2234   if ((Flags & TF_DependencyWalk) && AlreadyKept)
2235     return;
2236
2237   // We must not call shouldKeepDIE while called from keepDIEAndDependencies,
2238   // because it would screw up the relocation finding logic.
2239   if (!(Flags & TF_DependencyWalk))
2240     Flags = shouldKeepDIE(RelocMgr, Die, CU, MyInfo, Flags);
2241
2242   // If it is a newly kept DIE mark it as well as all its dependencies as kept.
2243   if (!AlreadyKept && (Flags & TF_Keep)) {
2244     bool UseOdr = (Flags & TF_DependencyWalk) ? (Flags & TF_ODR) : CU.hasODR();
2245     keepDIEAndDependencies(RelocMgr, Die, MyInfo, DMO, CU, UseOdr);
2246   }
2247   // The TF_ParentWalk flag tells us that we are currently walking up
2248   // the parent chain of a required DIE, and we don't want to mark all
2249   // the children of the parents as kept (consider for example a
2250   // DW_TAG_namespace node in the parent chain). There are however a
2251   // set of DIE types for which we want to ignore that directive and still
2252   // walk their children.
2253   if (dieNeedsChildrenToBeMeaningful(Die.getTag()))
2254     Flags &= ~TF_ParentWalk;
2255
2256   if (!Die.hasChildren() || (Flags & TF_ParentWalk))
2257     return;
2258
2259   for (auto *Child = Die.getFirstChild(); Child && !Child->isNULL();
2260        Child = Child->getSibling())
2261     lookForDIEsToKeep(RelocMgr, *Child, DMO, CU, Flags);
2262 }
2263
2264 /// \brief Assign an abbreviation numer to \p Abbrev.
2265 ///
2266 /// Our DIEs get freed after every DebugMapObject has been processed,
2267 /// thus the FoldingSet we use to unique DIEAbbrevs cannot refer to
2268 /// the instances hold by the DIEs. When we encounter an abbreviation
2269 /// that we don't know, we create a permanent copy of it.
2270 void DwarfLinker::AssignAbbrev(DIEAbbrev &Abbrev) {
2271   // Check the set for priors.
2272   FoldingSetNodeID ID;
2273   Abbrev.Profile(ID);
2274   void *InsertToken;
2275   DIEAbbrev *InSet = AbbreviationsSet.FindNodeOrInsertPos(ID, InsertToken);
2276
2277   // If it's newly added.
2278   if (InSet) {
2279     // Assign existing abbreviation number.
2280     Abbrev.setNumber(InSet->getNumber());
2281   } else {
2282     // Add to abbreviation list.
2283     Abbreviations.push_back(
2284         llvm::make_unique<DIEAbbrev>(Abbrev.getTag(), Abbrev.hasChildren()));
2285     for (const auto &Attr : Abbrev.getData())
2286       Abbreviations.back()->AddAttribute(Attr.getAttribute(), Attr.getForm());
2287     AbbreviationsSet.InsertNode(Abbreviations.back().get(), InsertToken);
2288     // Assign the unique abbreviation number.
2289     Abbrev.setNumber(Abbreviations.size());
2290     Abbreviations.back()->setNumber(Abbreviations.size());
2291   }
2292 }
2293
2294 unsigned DwarfLinker::DIECloner::cloneStringAttribute(DIE &Die,
2295                                                       AttributeSpec AttrSpec,
2296                                                       const DWARFFormValue &Val,
2297                                                       const DWARFUnit &U) {
2298   // Switch everything to out of line strings.
2299   const char *String = *Val.getAsCString(&U);
2300   unsigned Offset = Linker.StringPool.getStringOffset(String);
2301   Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), dwarf::DW_FORM_strp,
2302                DIEInteger(Offset));
2303   return 4;
2304 }
2305
2306 unsigned DwarfLinker::DIECloner::cloneDieReferenceAttribute(
2307     DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE,
2308     AttributeSpec AttrSpec, unsigned AttrSize, const DWARFFormValue &Val,
2309     CompileUnit &Unit) {
2310   const DWARFUnit &U = Unit.getOrigUnit();
2311   uint32_t Ref = *Val.getAsReference(&U);
2312   DIE *NewRefDie = nullptr;
2313   CompileUnit *RefUnit = nullptr;
2314   DeclContext *Ctxt = nullptr;
2315
2316   const DWARFDebugInfoEntryMinimal *RefDie =
2317       resolveDIEReference(Linker, CompileUnits, Val, U, InputDIE, RefUnit);
2318
2319   // If the referenced DIE is not found,  drop the attribute.
2320   if (!RefDie)
2321     return 0;
2322
2323   unsigned Idx = RefUnit->getOrigUnit().getDIEIndex(RefDie);
2324   CompileUnit::DIEInfo &RefInfo = RefUnit->getInfo(Idx);
2325
2326   // If we already have emitted an equivalent DeclContext, just point
2327   // at it.
2328   if (isODRAttribute(AttrSpec.Attr)) {
2329     Ctxt = RefInfo.Ctxt;
2330     if (Ctxt && Ctxt->getCanonicalDIEOffset()) {
2331       DIEInteger Attr(Ctxt->getCanonicalDIEOffset());
2332       Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
2333                    dwarf::DW_FORM_ref_addr, Attr);
2334       return getRefAddrSize(U);
2335     }
2336   }
2337
2338   if (!RefInfo.Clone) {
2339     assert(Ref > InputDIE.getOffset());
2340     // We haven't cloned this DIE yet. Just create an empty one and
2341     // store it. It'll get really cloned when we process it.
2342     RefInfo.Clone = DIE::get(DIEAlloc, dwarf::Tag(RefDie->getTag()));
2343   }
2344   NewRefDie = RefInfo.Clone;
2345
2346   if (AttrSpec.Form == dwarf::DW_FORM_ref_addr ||
2347       (Unit.hasODR() && isODRAttribute(AttrSpec.Attr))) {
2348     // We cannot currently rely on a DIEEntry to emit ref_addr
2349     // references, because the implementation calls back to DwarfDebug
2350     // to find the unit offset. (We don't have a DwarfDebug)
2351     // FIXME: we should be able to design DIEEntry reliance on
2352     // DwarfDebug away.
2353     uint64_t Attr;
2354     if (Ref < InputDIE.getOffset()) {
2355       // We must have already cloned that DIE.
2356       uint32_t NewRefOffset =
2357           RefUnit->getStartOffset() + NewRefDie->getOffset();
2358       Attr = NewRefOffset;
2359       Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
2360                    dwarf::DW_FORM_ref_addr, DIEInteger(Attr));
2361     } else {
2362       // A forward reference. Note and fixup later.
2363       Attr = 0xBADDEF;
2364       Unit.noteForwardReference(
2365           NewRefDie, RefUnit, Ctxt,
2366           Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
2367                        dwarf::DW_FORM_ref_addr, DIEInteger(Attr)));
2368     }
2369     return getRefAddrSize(U);
2370   }
2371
2372   Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
2373                dwarf::Form(AttrSpec.Form), DIEEntry(*NewRefDie));
2374   return AttrSize;
2375 }
2376
2377 unsigned DwarfLinker::DIECloner::cloneBlockAttribute(DIE &Die,
2378                                                      AttributeSpec AttrSpec,
2379                                                      const DWARFFormValue &Val,
2380                                                      unsigned AttrSize) {
2381   DIEValueList *Attr;
2382   DIEValue Value;
2383   DIELoc *Loc = nullptr;
2384   DIEBlock *Block = nullptr;
2385   // Just copy the block data over.
2386   if (AttrSpec.Form == dwarf::DW_FORM_exprloc) {
2387     Loc = new (DIEAlloc) DIELoc;
2388     Linker.DIELocs.push_back(Loc);
2389   } else {
2390     Block = new (DIEAlloc) DIEBlock;
2391     Linker.DIEBlocks.push_back(Block);
2392   }
2393   Attr = Loc ? static_cast<DIEValueList *>(Loc)
2394              : static_cast<DIEValueList *>(Block);
2395
2396   if (Loc)
2397     Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
2398                      dwarf::Form(AttrSpec.Form), Loc);
2399   else
2400     Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
2401                      dwarf::Form(AttrSpec.Form), Block);
2402   ArrayRef<uint8_t> Bytes = *Val.getAsBlock();
2403   for (auto Byte : Bytes)
2404     Attr->addValue(DIEAlloc, static_cast<dwarf::Attribute>(0),
2405                    dwarf::DW_FORM_data1, DIEInteger(Byte));
2406   // FIXME: If DIEBlock and DIELoc just reuses the Size field of
2407   // the DIE class, this if could be replaced by
2408   // Attr->setSize(Bytes.size()).
2409   if (Linker.Streamer) {
2410     auto *AsmPrinter = &Linker.Streamer->getAsmPrinter();
2411     if (Loc)
2412       Loc->ComputeSize(AsmPrinter);
2413     else
2414       Block->ComputeSize(AsmPrinter);
2415   }
2416   Die.addValue(DIEAlloc, Value);
2417   return AttrSize;
2418 }
2419
2420 unsigned DwarfLinker::DIECloner::cloneAddressAttribute(
2421     DIE &Die, AttributeSpec AttrSpec, const DWARFFormValue &Val,
2422     const CompileUnit &Unit, AttributesInfo &Info) {
2423   uint64_t Addr = *Val.getAsAddress(&Unit.getOrigUnit());
2424   if (AttrSpec.Attr == dwarf::DW_AT_low_pc) {
2425     if (Die.getTag() == dwarf::DW_TAG_inlined_subroutine ||
2426         Die.getTag() == dwarf::DW_TAG_lexical_block)
2427       // The low_pc of a block or inline subroutine might get
2428       // relocated because it happens to match the low_pc of the
2429       // enclosing subprogram. To prevent issues with that, always use
2430       // the low_pc from the input DIE if relocations have been applied.
2431       Addr = (Info.OrigLowPc != UINT64_MAX ? Info.OrigLowPc : Addr) +
2432              Info.PCOffset;
2433     else if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
2434       Addr = Unit.getLowPc();
2435       if (Addr == UINT64_MAX)
2436         return 0;
2437     }
2438     Info.HasLowPc = true;
2439   } else if (AttrSpec.Attr == dwarf::DW_AT_high_pc) {
2440     if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
2441       if (uint64_t HighPc = Unit.getHighPc())
2442         Addr = HighPc;
2443       else
2444         return 0;
2445     } else
2446       // If we have a high_pc recorded for the input DIE, use
2447       // it. Otherwise (when no relocations where applied) just use the
2448       // one we just decoded.
2449       Addr = (Info.OrigHighPc ? Info.OrigHighPc : Addr) + Info.PCOffset;
2450   }
2451
2452   Die.addValue(DIEAlloc, static_cast<dwarf::Attribute>(AttrSpec.Attr),
2453                static_cast<dwarf::Form>(AttrSpec.Form), DIEInteger(Addr));
2454   return Unit.getOrigUnit().getAddressByteSize();
2455 }
2456
2457 unsigned DwarfLinker::DIECloner::cloneScalarAttribute(
2458     DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE, CompileUnit &Unit,
2459     AttributeSpec AttrSpec, const DWARFFormValue &Val, unsigned AttrSize,
2460     AttributesInfo &Info) {
2461   uint64_t Value;
2462   if (AttrSpec.Attr == dwarf::DW_AT_high_pc &&
2463       Die.getTag() == dwarf::DW_TAG_compile_unit) {
2464     if (Unit.getLowPc() == -1ULL)
2465       return 0;
2466     // Dwarf >= 4 high_pc is an size, not an address.
2467     Value = Unit.getHighPc() - Unit.getLowPc();
2468   } else if (AttrSpec.Form == dwarf::DW_FORM_sec_offset)
2469     Value = *Val.getAsSectionOffset();
2470   else if (AttrSpec.Form == dwarf::DW_FORM_sdata)
2471     Value = *Val.getAsSignedConstant();
2472   else if (auto OptionalValue = Val.getAsUnsignedConstant())
2473     Value = *OptionalValue;
2474   else {
2475     Linker.reportWarning(
2476         "Unsupported scalar attribute form. Dropping attribute.",
2477         &Unit.getOrigUnit(), &InputDIE);
2478     return 0;
2479   }
2480   PatchLocation Patch =
2481       Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
2482                    dwarf::Form(AttrSpec.Form), DIEInteger(Value));
2483   if (AttrSpec.Attr == dwarf::DW_AT_ranges)
2484     Unit.noteRangeAttribute(Die, Patch);
2485
2486   // A more generic way to check for location attributes would be
2487   // nice, but it's very unlikely that any other attribute needs a
2488   // location list.
2489   else if (AttrSpec.Attr == dwarf::DW_AT_location ||
2490            AttrSpec.Attr == dwarf::DW_AT_frame_base)
2491     Unit.noteLocationAttribute(Patch, Info.PCOffset);
2492   else if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
2493     Info.IsDeclaration = true;
2494
2495   return AttrSize;
2496 }
2497
2498 /// \brief Clone \p InputDIE's attribute described by \p AttrSpec with
2499 /// value \p Val, and add it to \p Die.
2500 /// \returns the size of the cloned attribute.
2501 unsigned DwarfLinker::DIECloner::cloneAttribute(
2502     DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE, CompileUnit &Unit,
2503     const DWARFFormValue &Val, const AttributeSpec AttrSpec, unsigned AttrSize,
2504     AttributesInfo &Info) {
2505   const DWARFUnit &U = Unit.getOrigUnit();
2506
2507   switch (AttrSpec.Form) {
2508   case dwarf::DW_FORM_strp:
2509   case dwarf::DW_FORM_string:
2510     return cloneStringAttribute(Die, AttrSpec, Val, U);
2511   case dwarf::DW_FORM_ref_addr:
2512   case dwarf::DW_FORM_ref1:
2513   case dwarf::DW_FORM_ref2:
2514   case dwarf::DW_FORM_ref4:
2515   case dwarf::DW_FORM_ref8:
2516     return cloneDieReferenceAttribute(Die, InputDIE, AttrSpec, AttrSize, Val,
2517                                       Unit);
2518   case dwarf::DW_FORM_block:
2519   case dwarf::DW_FORM_block1:
2520   case dwarf::DW_FORM_block2:
2521   case dwarf::DW_FORM_block4:
2522   case dwarf::DW_FORM_exprloc:
2523     return cloneBlockAttribute(Die, AttrSpec, Val, AttrSize);
2524   case dwarf::DW_FORM_addr:
2525     return cloneAddressAttribute(Die, AttrSpec, Val, Unit, Info);
2526   case dwarf::DW_FORM_data1:
2527   case dwarf::DW_FORM_data2:
2528   case dwarf::DW_FORM_data4:
2529   case dwarf::DW_FORM_data8:
2530   case dwarf::DW_FORM_udata:
2531   case dwarf::DW_FORM_sdata:
2532   case dwarf::DW_FORM_sec_offset:
2533   case dwarf::DW_FORM_flag:
2534   case dwarf::DW_FORM_flag_present:
2535     return cloneScalarAttribute(Die, InputDIE, Unit, AttrSpec, Val, AttrSize,
2536                                 Info);
2537   default:
2538     Linker.reportWarning(
2539         "Unsupported attribute form in cloneAttribute. Dropping.", &U,
2540         &InputDIE);
2541   }
2542
2543   return 0;
2544 }
2545
2546 /// \brief Apply the valid relocations found by findValidRelocs() to
2547 /// the buffer \p Data, taking into account that Data is at \p BaseOffset
2548 /// in the debug_info section.
2549 ///
2550 /// Like for findValidRelocs(), this function must be called with
2551 /// monotonic \p BaseOffset values.
2552 ///
2553 /// \returns wether any reloc has been applied.
2554 bool DwarfLinker::RelocationManager::
2555 applyValidRelocs(MutableArrayRef<char> Data, uint32_t BaseOffset,
2556                  bool isLittleEndian) {
2557   assert((NextValidReloc == 0 ||
2558           BaseOffset > ValidRelocs[NextValidReloc - 1].Offset) &&
2559          "BaseOffset should only be increasing.");
2560   if (NextValidReloc >= ValidRelocs.size())
2561     return false;
2562
2563   // Skip relocs that haven't been applied.
2564   while (NextValidReloc < ValidRelocs.size() &&
2565          ValidRelocs[NextValidReloc].Offset < BaseOffset)
2566     ++NextValidReloc;
2567
2568   bool Applied = false;
2569   uint64_t EndOffset = BaseOffset + Data.size();
2570   while (NextValidReloc < ValidRelocs.size() &&
2571          ValidRelocs[NextValidReloc].Offset >= BaseOffset &&
2572          ValidRelocs[NextValidReloc].Offset < EndOffset) {
2573     const auto &ValidReloc = ValidRelocs[NextValidReloc++];
2574     assert(ValidReloc.Offset - BaseOffset < Data.size());
2575     assert(ValidReloc.Offset - BaseOffset + ValidReloc.Size <= Data.size());
2576     char Buf[8];
2577     uint64_t Value = ValidReloc.Mapping->getValue().BinaryAddress;
2578     Value += ValidReloc.Addend;
2579     for (unsigned i = 0; i != ValidReloc.Size; ++i) {
2580       unsigned Index = isLittleEndian ? i : (ValidReloc.Size - i - 1);
2581       Buf[i] = uint8_t(Value >> (Index * 8));
2582     }
2583     assert(ValidReloc.Size <= sizeof(Buf));
2584     memcpy(&Data[ValidReloc.Offset - BaseOffset], Buf, ValidReloc.Size);
2585     Applied = true;
2586   }
2587
2588   return Applied;
2589 }
2590
2591 static bool isTypeTag(uint16_t Tag) {
2592   switch (Tag) {
2593   case dwarf::DW_TAG_array_type:
2594   case dwarf::DW_TAG_class_type:
2595   case dwarf::DW_TAG_enumeration_type:
2596   case dwarf::DW_TAG_pointer_type:
2597   case dwarf::DW_TAG_reference_type:
2598   case dwarf::DW_TAG_string_type:
2599   case dwarf::DW_TAG_structure_type:
2600   case dwarf::DW_TAG_subroutine_type:
2601   case dwarf::DW_TAG_typedef:
2602   case dwarf::DW_TAG_union_type:
2603   case dwarf::DW_TAG_ptr_to_member_type:
2604   case dwarf::DW_TAG_set_type:
2605   case dwarf::DW_TAG_subrange_type:
2606   case dwarf::DW_TAG_base_type:
2607   case dwarf::DW_TAG_const_type:
2608   case dwarf::DW_TAG_constant:
2609   case dwarf::DW_TAG_file_type:
2610   case dwarf::DW_TAG_namelist:
2611   case dwarf::DW_TAG_packed_type:
2612   case dwarf::DW_TAG_volatile_type:
2613   case dwarf::DW_TAG_restrict_type:
2614   case dwarf::DW_TAG_interface_type:
2615   case dwarf::DW_TAG_unspecified_type:
2616   case dwarf::DW_TAG_shared_type:
2617     return true;
2618   default:
2619     break;
2620   }
2621   return false;
2622 }
2623
2624 static bool
2625 shouldSkipAttribute(DWARFAbbreviationDeclaration::AttributeSpec AttrSpec,
2626                     uint16_t Tag, bool InDebugMap, bool SkipPC,
2627                     bool InFunctionScope) {
2628   switch (AttrSpec.Attr) {
2629   default:
2630     return false;
2631   case dwarf::DW_AT_low_pc:
2632   case dwarf::DW_AT_high_pc:
2633   case dwarf::DW_AT_ranges:
2634     return SkipPC;
2635   case dwarf::DW_AT_location:
2636   case dwarf::DW_AT_frame_base:
2637     // FIXME: for some reason dsymutil-classic keeps the location
2638     // attributes when they are of block type (ie. not location
2639     // lists). This is totally wrong for globals where we will keep a
2640     // wrong address. It is mostly harmless for locals, but there is
2641     // no point in keeping these anyway when the function wasn't linked.
2642     return (SkipPC || (!InFunctionScope && Tag == dwarf::DW_TAG_variable &&
2643                        !InDebugMap)) &&
2644            !DWARFFormValue(AttrSpec.Form).isFormClass(DWARFFormValue::FC_Block);
2645   }
2646 }
2647
2648 DIE *DwarfLinker::DIECloner::cloneDIE(
2649     const DWARFDebugInfoEntryMinimal &InputDIE, CompileUnit &Unit,
2650     int64_t PCOffset, uint32_t OutOffset, unsigned Flags) {
2651   DWARFUnit &U = Unit.getOrigUnit();
2652   unsigned Idx = U.getDIEIndex(&InputDIE);
2653   CompileUnit::DIEInfo &Info = Unit.getInfo(Idx);
2654
2655   // Should the DIE appear in the output?
2656   if (!Unit.getInfo(Idx).Keep)
2657     return nullptr;
2658
2659   uint32_t Offset = InputDIE.getOffset();
2660   // The DIE might have been already created by a forward reference
2661   // (see cloneDieReferenceAttribute()).
2662   DIE *Die = Info.Clone;
2663   if (!Die)
2664     Die = Info.Clone = DIE::get(DIEAlloc, dwarf::Tag(InputDIE.getTag()));
2665   assert(Die->getTag() == InputDIE.getTag());
2666   Die->setOffset(OutOffset);
2667   if ((Unit.hasODR() || Unit.isClangModule()) &&
2668       Die->getTag() != dwarf::DW_TAG_namespace && Info.Ctxt &&
2669       Info.Ctxt != Unit.getInfo(Info.ParentIdx).Ctxt &&
2670       !Info.Ctxt->getCanonicalDIEOffset()) {
2671     // We are about to emit a DIE that is the root of its own valid
2672     // DeclContext tree. Make the current offset the canonical offset
2673     // for this context.
2674     Info.Ctxt->setCanonicalDIEOffset(OutOffset + Unit.getStartOffset());
2675   }
2676
2677   // Extract and clone every attribute.
2678   DataExtractor Data = U.getDebugInfoExtractor();
2679   // Point to the next DIE (generally there is always at least a NULL
2680   // entry after the current one). If this is a lone
2681   // DW_TAG_compile_unit without any children, point to the next unit.
2682   uint32_t NextOffset =
2683     (Idx + 1 < U.getNumDIEs())
2684     ? U.getDIEAtIndex(Idx + 1)->getOffset()
2685     : U.getNextUnitOffset();
2686   AttributesInfo AttrInfo;
2687
2688   // We could copy the data only if we need to aply a relocation to
2689   // it. After testing, it seems there is no performance downside to
2690   // doing the copy unconditionally, and it makes the code simpler.
2691   SmallString<40> DIECopy(Data.getData().substr(Offset, NextOffset - Offset));
2692   Data = DataExtractor(DIECopy, Data.isLittleEndian(), Data.getAddressSize());
2693   // Modify the copy with relocated addresses.
2694   if (RelocMgr.applyValidRelocs(DIECopy, Offset, Data.isLittleEndian())) {
2695     // If we applied relocations, we store the value of high_pc that was
2696     // potentially stored in the input DIE. If high_pc is an address
2697     // (Dwarf version == 2), then it might have been relocated to a
2698     // totally unrelated value (because the end address in the object
2699     // file might be start address of another function which got moved
2700     // independantly by the linker). The computation of the actual
2701     // high_pc value is done in cloneAddressAttribute().
2702     AttrInfo.OrigHighPc =
2703         InputDIE.getAttributeValueAsAddress(&U, dwarf::DW_AT_high_pc, 0);
2704     // Also store the low_pc. It might get relocated in an
2705     // inline_subprogram that happens at the beginning of its
2706     // inlining function.
2707     AttrInfo.OrigLowPc =
2708         InputDIE.getAttributeValueAsAddress(&U, dwarf::DW_AT_low_pc, UINT64_MAX);
2709   }
2710
2711   // Reset the Offset to 0 as we will be working on the local copy of
2712   // the data.
2713   Offset = 0;
2714
2715   const auto *Abbrev = InputDIE.getAbbreviationDeclarationPtr();
2716   Offset += getULEB128Size(Abbrev->getCode());
2717
2718   // We are entering a subprogram. Get and propagate the PCOffset.
2719   if (Die->getTag() == dwarf::DW_TAG_subprogram)
2720     PCOffset = Info.AddrAdjust;
2721   AttrInfo.PCOffset = PCOffset;
2722
2723   if (Abbrev->getTag() == dwarf::DW_TAG_subprogram) {
2724     Flags |= TF_InFunctionScope;
2725     if (!Info.InDebugMap)
2726       Flags |= TF_SkipPC;
2727   }
2728
2729   bool Copied = false;
2730   for (const auto &AttrSpec : Abbrev->attributes()) {
2731     if (shouldSkipAttribute(AttrSpec, Die->getTag(), Info.InDebugMap,
2732                             Flags & TF_SkipPC, Flags & TF_InFunctionScope)) {
2733       DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset, &U);
2734       // FIXME: dsymutil-classic keeps the old abbreviation around
2735       // even if it's not used. We can remove this (and the copyAbbrev
2736       // helper) as soon as bit-for-bit compatibility is not a goal anymore.
2737       if (!Copied) {
2738         copyAbbrev(*InputDIE.getAbbreviationDeclarationPtr(), Unit.hasODR());
2739         Copied = true;
2740       }
2741       continue;
2742     }
2743
2744     DWARFFormValue Val(AttrSpec.Form);
2745     uint32_t AttrSize = Offset;
2746     Val.extractValue(Data, &Offset, &U);
2747     AttrSize = Offset - AttrSize;
2748
2749     OutOffset +=
2750         cloneAttribute(*Die, InputDIE, Unit, Val, AttrSpec, AttrSize, AttrInfo);
2751   }
2752
2753   // Look for accelerator entries.
2754   uint16_t Tag = InputDIE.getTag();
2755   // FIXME: This is slightly wrong. An inline_subroutine without a
2756   // low_pc, but with AT_ranges might be interesting to get into the
2757   // accelerator tables too. For now stick with dsymutil's behavior.
2758   if ((Info.InDebugMap || AttrInfo.HasLowPc) &&
2759       Tag != dwarf::DW_TAG_compile_unit &&
2760       getDIENames(InputDIE, Unit.getOrigUnit(), AttrInfo)) {
2761     if (AttrInfo.MangledName && AttrInfo.MangledName != AttrInfo.Name)
2762       Unit.addNameAccelerator(Die, AttrInfo.MangledName,
2763                               AttrInfo.MangledNameOffset,
2764                               Tag == dwarf::DW_TAG_inlined_subroutine);
2765     if (AttrInfo.Name)
2766       Unit.addNameAccelerator(Die, AttrInfo.Name, AttrInfo.NameOffset,
2767                               Tag == dwarf::DW_TAG_inlined_subroutine);
2768   } else if (isTypeTag(Tag) && !AttrInfo.IsDeclaration &&
2769              getDIENames(InputDIE, Unit.getOrigUnit(), AttrInfo)) {
2770     Unit.addTypeAccelerator(Die, AttrInfo.Name, AttrInfo.NameOffset);
2771   }
2772
2773   // Determine whether there are any children that we want to keep.
2774   bool HasChildren = false;
2775   for (auto *Child = InputDIE.getFirstChild(); Child && !Child->isNULL();
2776        Child = Child->getSibling()) {
2777     unsigned Idx = U.getDIEIndex(Child);
2778     if (Unit.getInfo(Idx).Keep) {
2779       HasChildren = true;
2780       break;
2781     }
2782   }
2783
2784   DIEAbbrev NewAbbrev = Die->generateAbbrev();
2785   if (HasChildren)
2786     NewAbbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
2787   // Assign a permanent abbrev number
2788   Linker.AssignAbbrev(NewAbbrev);
2789   Die->setAbbrevNumber(NewAbbrev.getNumber());
2790
2791   // Add the size of the abbreviation number to the output offset.
2792   OutOffset += getULEB128Size(Die->getAbbrevNumber());
2793
2794   if (!HasChildren) {
2795     // Update our size.
2796     Die->setSize(OutOffset - Die->getOffset());
2797     return Die;
2798   }
2799
2800   // Recursively clone children.
2801   for (auto *Child = InputDIE.getFirstChild(); Child && !Child->isNULL();
2802        Child = Child->getSibling()) {
2803     if (DIE *Clone = cloneDIE(*Child, Unit, PCOffset, OutOffset, Flags)) {
2804       Die->addChild(Clone);
2805       OutOffset = Clone->getOffset() + Clone->getSize();
2806     }
2807   }
2808
2809   // Account for the end of children marker.
2810   OutOffset += sizeof(int8_t);
2811   // Update our size.
2812   Die->setSize(OutOffset - Die->getOffset());
2813   return Die;
2814 }
2815
2816 /// \brief Patch the input object file relevant debug_ranges entries
2817 /// and emit them in the output file. Update the relevant attributes
2818 /// to point at the new entries.
2819 void DwarfLinker::patchRangesForUnit(const CompileUnit &Unit,
2820                                      DWARFContext &OrigDwarf) const {
2821   DWARFDebugRangeList RangeList;
2822   const auto &FunctionRanges = Unit.getFunctionRanges();
2823   unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
2824   DataExtractor RangeExtractor(OrigDwarf.getRangeSection(),
2825                                OrigDwarf.isLittleEndian(), AddressSize);
2826   auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
2827   DWARFUnit &OrigUnit = Unit.getOrigUnit();
2828   const auto *OrigUnitDie = OrigUnit.getUnitDIE(false);
2829   uint64_t OrigLowPc = OrigUnitDie->getAttributeValueAsAddress(
2830       &OrigUnit, dwarf::DW_AT_low_pc, -1ULL);
2831   // Ranges addresses are based on the unit's low_pc. Compute the
2832   // offset we need to apply to adapt to the new unit's low_pc.
2833   int64_t UnitPcOffset = 0;
2834   if (OrigLowPc != -1ULL)
2835     UnitPcOffset = int64_t(OrigLowPc) - Unit.getLowPc();
2836
2837   for (const auto &RangeAttribute : Unit.getRangesAttributes()) {
2838     uint32_t Offset = RangeAttribute.get();
2839     RangeAttribute.set(Streamer->getRangesSectionSize());
2840     RangeList.extract(RangeExtractor, &Offset);
2841     const auto &Entries = RangeList.getEntries();
2842     if (!Entries.empty()) {
2843       const DWARFDebugRangeList::RangeListEntry &First = Entries.front();
2844
2845       if (CurrRange == InvalidRange ||
2846           First.StartAddress + OrigLowPc < CurrRange.start() ||
2847           First.StartAddress + OrigLowPc >= CurrRange.stop()) {
2848         CurrRange = FunctionRanges.find(First.StartAddress + OrigLowPc);
2849         if (CurrRange == InvalidRange ||
2850             CurrRange.start() > First.StartAddress + OrigLowPc) {
2851           reportWarning("no mapping for range.");
2852           continue;
2853         }
2854       }
2855     }
2856
2857     Streamer->emitRangesEntries(UnitPcOffset, OrigLowPc, CurrRange, Entries,
2858                                 AddressSize);
2859   }
2860 }
2861
2862 /// \brief Generate the debug_aranges entries for \p Unit and if the
2863 /// unit has a DW_AT_ranges attribute, also emit the debug_ranges
2864 /// contribution for this attribute.
2865 /// FIXME: this could actually be done right in patchRangesForUnit,
2866 /// but for the sake of initial bit-for-bit compatibility with legacy
2867 /// dsymutil, we have to do it in a delayed pass.
2868 void DwarfLinker::generateUnitRanges(CompileUnit &Unit) const {
2869   auto Attr = Unit.getUnitRangesAttribute();
2870   if (Attr)
2871     Attr->set(Streamer->getRangesSectionSize());
2872   Streamer->emitUnitRangesEntries(Unit, static_cast<bool>(Attr));
2873 }
2874
2875 /// \brief Insert the new line info sequence \p Seq into the current
2876 /// set of already linked line info \p Rows.
2877 static void insertLineSequence(std::vector<DWARFDebugLine::Row> &Seq,
2878                                std::vector<DWARFDebugLine::Row> &Rows) {
2879   if (Seq.empty())
2880     return;
2881
2882   if (!Rows.empty() && Rows.back().Address < Seq.front().Address) {
2883     Rows.insert(Rows.end(), Seq.begin(), Seq.end());
2884     Seq.clear();
2885     return;
2886   }
2887
2888   auto InsertPoint = std::lower_bound(
2889       Rows.begin(), Rows.end(), Seq.front(),
2890       [](const DWARFDebugLine::Row &LHS, const DWARFDebugLine::Row &RHS) {
2891         return LHS.Address < RHS.Address;
2892       });
2893
2894   // FIXME: this only removes the unneeded end_sequence if the
2895   // sequences have been inserted in order. using a global sort like
2896   // described in patchLineTableForUnit() and delaying the end_sequene
2897   // elimination to emitLineTableForUnit() we can get rid of all of them.
2898   if (InsertPoint != Rows.end() &&
2899       InsertPoint->Address == Seq.front().Address && InsertPoint->EndSequence) {
2900     *InsertPoint = Seq.front();
2901     Rows.insert(InsertPoint + 1, Seq.begin() + 1, Seq.end());
2902   } else {
2903     Rows.insert(InsertPoint, Seq.begin(), Seq.end());
2904   }
2905
2906   Seq.clear();
2907 }
2908
2909 static void patchStmtList(DIE &Die, DIEInteger Offset) {
2910   for (auto &V : Die.values())
2911     if (V.getAttribute() == dwarf::DW_AT_stmt_list) {
2912       V = DIEValue(V.getAttribute(), V.getForm(), Offset);
2913       return;
2914     }
2915
2916   llvm_unreachable("Didn't find DW_AT_stmt_list in cloned DIE!");
2917 }
2918
2919 /// \brief Extract the line table for \p Unit from \p OrigDwarf, and
2920 /// recreate a relocated version of these for the address ranges that
2921 /// are present in the binary.
2922 void DwarfLinker::patchLineTableForUnit(CompileUnit &Unit,
2923                                         DWARFContext &OrigDwarf) {
2924   const DWARFDebugInfoEntryMinimal *CUDie = Unit.getOrigUnit().getUnitDIE();
2925   uint64_t StmtList = CUDie->getAttributeValueAsSectionOffset(
2926       &Unit.getOrigUnit(), dwarf::DW_AT_stmt_list, -1ULL);
2927   if (StmtList == -1ULL)
2928     return;
2929
2930   // Update the cloned DW_AT_stmt_list with the correct debug_line offset.
2931   if (auto *OutputDIE = Unit.getOutputUnitDIE())
2932     patchStmtList(*OutputDIE, DIEInteger(Streamer->getLineSectionSize()));
2933
2934   // Parse the original line info for the unit.
2935   DWARFDebugLine::LineTable LineTable;
2936   uint32_t StmtOffset = StmtList;
2937   StringRef LineData = OrigDwarf.getLineSection().Data;
2938   DataExtractor LineExtractor(LineData, OrigDwarf.isLittleEndian(),
2939                               Unit.getOrigUnit().getAddressByteSize());
2940   LineTable.parse(LineExtractor, &OrigDwarf.getLineSection().Relocs,
2941                   &StmtOffset);
2942
2943   // This vector is the output line table.
2944   std::vector<DWARFDebugLine::Row> NewRows;
2945   NewRows.reserve(LineTable.Rows.size());
2946
2947   // Current sequence of rows being extracted, before being inserted
2948   // in NewRows.
2949   std::vector<DWARFDebugLine::Row> Seq;
2950   const auto &FunctionRanges = Unit.getFunctionRanges();
2951   auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
2952
2953   // FIXME: This logic is meant to generate exactly the same output as
2954   // Darwin's classic dsynutil. There is a nicer way to implement this
2955   // by simply putting all the relocated line info in NewRows and simply
2956   // sorting NewRows before passing it to emitLineTableForUnit. This
2957   // should be correct as sequences for a function should stay
2958   // together in the sorted output. There are a few corner cases that
2959   // look suspicious though, and that required to implement the logic
2960   // this way. Revisit that once initial validation is finished.
2961
2962   // Iterate over the object file line info and extract the sequences
2963   // that correspond to linked functions.
2964   for (auto &Row : LineTable.Rows) {
2965     // Check wether we stepped out of the range. The range is
2966     // half-open, but consider accept the end address of the range if
2967     // it is marked as end_sequence in the input (because in that
2968     // case, the relocation offset is accurate and that entry won't
2969     // serve as the start of another function).
2970     if (CurrRange == InvalidRange || Row.Address < CurrRange.start() ||
2971         Row.Address > CurrRange.stop() ||
2972         (Row.Address == CurrRange.stop() && !Row.EndSequence)) {
2973       // We just stepped out of a known range. Insert a end_sequence
2974       // corresponding to the end of the range.
2975       uint64_t StopAddress = CurrRange != InvalidRange
2976                                  ? CurrRange.stop() + CurrRange.value()
2977                                  : -1ULL;
2978       CurrRange = FunctionRanges.find(Row.Address);
2979       bool CurrRangeValid =
2980           CurrRange != InvalidRange && CurrRange.start() <= Row.Address;
2981       if (!CurrRangeValid) {
2982         CurrRange = InvalidRange;
2983         if (StopAddress != -1ULL) {
2984           // Try harder by looking in the DebugMapObject function
2985           // ranges map. There are corner cases where this finds a
2986           // valid entry. It's unclear if this is right or wrong, but
2987           // for now do as dsymutil.
2988           // FIXME: Understand exactly what cases this addresses and
2989           // potentially remove it along with the Ranges map.
2990           auto Range = Ranges.lower_bound(Row.Address);
2991           if (Range != Ranges.begin() && Range != Ranges.end())
2992             --Range;
2993
2994           if (Range != Ranges.end() && Range->first <= Row.Address &&
2995               Range->second.first >= Row.Address) {
2996             StopAddress = Row.Address + Range->second.second;
2997           }
2998         }
2999       }
3000       if (StopAddress != -1ULL && !Seq.empty()) {
3001         // Insert end sequence row with the computed end address, but
3002         // the same line as the previous one.
3003         auto NextLine = Seq.back();
3004         NextLine.Address = StopAddress;
3005         NextLine.EndSequence = 1;
3006         NextLine.PrologueEnd = 0;
3007         NextLine.BasicBlock = 0;
3008         NextLine.EpilogueBegin = 0;
3009         Seq.push_back(NextLine);
3010         insertLineSequence(Seq, NewRows);
3011       }
3012
3013       if (!CurrRangeValid)
3014         continue;
3015     }
3016
3017     // Ignore empty sequences.
3018     if (Row.EndSequence && Seq.empty())
3019       continue;
3020
3021     // Relocate row address and add it to the current sequence.
3022     Row.Address += CurrRange.value();
3023     Seq.emplace_back(Row);
3024
3025     if (Row.EndSequence)
3026       insertLineSequence(Seq, NewRows);
3027   }
3028
3029   // Finished extracting, now emit the line tables.
3030   uint32_t PrologueEnd = StmtList + 10 + LineTable.Prologue.PrologueLength;
3031   // FIXME: LLVM hardcodes it's prologue values. We just copy the
3032   // prologue over and that works because we act as both producer and
3033   // consumer. It would be nicer to have a real configurable line
3034   // table emitter.
3035   if (LineTable.Prologue.Version != 2 ||
3036       LineTable.Prologue.DefaultIsStmt != DWARF2_LINE_DEFAULT_IS_STMT ||
3037       LineTable.Prologue.OpcodeBase > 13)
3038     reportWarning("line table paramters mismatch. Cannot emit.");
3039   else {
3040     MCDwarfLineTableParams Params;
3041     Params.DWARF2LineOpcodeBase = LineTable.Prologue.OpcodeBase;
3042     Params.DWARF2LineBase = LineTable.Prologue.LineBase;
3043     Params.DWARF2LineRange = LineTable.Prologue.LineRange;
3044     Streamer->emitLineTableForUnit(Params,
3045                                    LineData.slice(StmtList + 4, PrologueEnd),
3046                                    LineTable.Prologue.MinInstLength, NewRows,
3047                                    Unit.getOrigUnit().getAddressByteSize());
3048   }
3049 }
3050
3051 void DwarfLinker::emitAcceleratorEntriesForUnit(CompileUnit &Unit) {
3052   Streamer->emitPubNamesForUnit(Unit);
3053   Streamer->emitPubTypesForUnit(Unit);
3054 }
3055
3056 /// \brief Read the frame info stored in the object, and emit the
3057 /// patched frame descriptions for the linked binary.
3058 ///
3059 /// This is actually pretty easy as the data of the CIEs and FDEs can
3060 /// be considered as black boxes and moved as is. The only thing to do
3061 /// is to patch the addresses in the headers.
3062 void DwarfLinker::patchFrameInfoForObject(const DebugMapObject &DMO,
3063                                           DWARFContext &OrigDwarf,
3064                                           unsigned AddrSize) {
3065   StringRef FrameData = OrigDwarf.getDebugFrameSection();
3066   if (FrameData.empty())
3067     return;
3068
3069   DataExtractor Data(FrameData, OrigDwarf.isLittleEndian(), 0);
3070   uint32_t InputOffset = 0;
3071
3072   // Store the data of the CIEs defined in this object, keyed by their
3073   // offsets.
3074   DenseMap<uint32_t, StringRef> LocalCIES;
3075
3076   while (Data.isValidOffset(InputOffset)) {
3077     uint32_t EntryOffset = InputOffset;
3078     uint32_t InitialLength = Data.getU32(&InputOffset);
3079     if (InitialLength == 0xFFFFFFFF)
3080       return reportWarning("Dwarf64 bits no supported");
3081
3082     uint32_t CIEId = Data.getU32(&InputOffset);
3083     if (CIEId == 0xFFFFFFFF) {
3084       // This is a CIE, store it.
3085       StringRef CIEData = FrameData.substr(EntryOffset, InitialLength + 4);
3086       LocalCIES[EntryOffset] = CIEData;
3087       // The -4 is to account for the CIEId we just read.
3088       InputOffset += InitialLength - 4;
3089       continue;
3090     }
3091
3092     uint32_t Loc = Data.getUnsigned(&InputOffset, AddrSize);
3093
3094     // Some compilers seem to emit frame info that doesn't start at
3095     // the function entry point, thus we can't just lookup the address
3096     // in the debug map. Use the linker's range map to see if the FDE
3097     // describes something that we can relocate.
3098     auto Range = Ranges.upper_bound(Loc);
3099     if (Range != Ranges.begin())
3100       --Range;
3101     if (Range == Ranges.end() || Range->first > Loc ||
3102         Range->second.first <= Loc) {
3103       // The +4 is to account for the size of the InitialLength field itself.
3104       InputOffset = EntryOffset + InitialLength + 4;
3105       continue;
3106     }
3107
3108     // This is an FDE, and we have a mapping.
3109     // Have we already emitted a corresponding CIE?
3110     StringRef CIEData = LocalCIES[CIEId];
3111     if (CIEData.empty())
3112       return reportWarning("Inconsistent debug_frame content. Dropping.");
3113
3114     // Look if we already emitted a CIE that corresponds to the
3115     // referenced one (the CIE data is the key of that lookup).
3116     auto IteratorInserted = EmittedCIEs.insert(
3117         std::make_pair(CIEData, Streamer->getFrameSectionSize()));
3118     // If there is no CIE yet for this ID, emit it.
3119     if (IteratorInserted.second ||
3120         // FIXME: dsymutil-classic only caches the last used CIE for
3121         // reuse. Mimic that behavior for now. Just removing that
3122         // second half of the condition and the LastCIEOffset variable
3123         // makes the code DTRT.
3124         LastCIEOffset != IteratorInserted.first->getValue()) {
3125       LastCIEOffset = Streamer->getFrameSectionSize();
3126       IteratorInserted.first->getValue() = LastCIEOffset;
3127       Streamer->emitCIE(CIEData);
3128     }
3129
3130     // Emit the FDE with updated address and CIE pointer.
3131     // (4 + AddrSize) is the size of the CIEId + initial_location
3132     // fields that will get reconstructed by emitFDE().
3133     unsigned FDERemainingBytes = InitialLength - (4 + AddrSize);
3134     Streamer->emitFDE(IteratorInserted.first->getValue(), AddrSize,
3135                       Loc + Range->second.second,
3136                       FrameData.substr(InputOffset, FDERemainingBytes));
3137     InputOffset += FDERemainingBytes;
3138   }
3139 }
3140
3141 void DwarfLinker::DIECloner::copyAbbrev(
3142     const DWARFAbbreviationDeclaration &Abbrev, bool hasODR) {
3143   DIEAbbrev Copy(dwarf::Tag(Abbrev.getTag()),
3144                  dwarf::Form(Abbrev.hasChildren()));
3145
3146   for (const auto &Attr : Abbrev.attributes()) {
3147     uint16_t Form = Attr.Form;
3148     if (hasODR && isODRAttribute(Attr.Attr))
3149       Form = dwarf::DW_FORM_ref_addr;
3150     Copy.AddAttribute(dwarf::Attribute(Attr.Attr), dwarf::Form(Form));
3151   }
3152
3153   Linker.AssignAbbrev(Copy);
3154 }
3155
3156 static uint64_t getDwoId(const DWARFDebugInfoEntryMinimal &CUDie,
3157                          const DWARFUnit &Unit) {
3158   uint64_t DwoId =
3159       CUDie.getAttributeValueAsUnsignedConstant(&Unit, dwarf::DW_AT_dwo_id, 0);
3160   if (!DwoId)
3161     DwoId = CUDie.getAttributeValueAsUnsignedConstant(&Unit,
3162                                                       dwarf::DW_AT_GNU_dwo_id, 0);
3163   return DwoId;
3164 }
3165
3166 bool DwarfLinker::registerModuleReference(
3167     const DWARFDebugInfoEntryMinimal &CUDie, const DWARFUnit &Unit,
3168     DebugMap &ModuleMap, unsigned Indent) {
3169   std::string PCMfile =
3170       CUDie.getAttributeValueAsString(&Unit, dwarf::DW_AT_dwo_name, "");
3171   if (PCMfile.empty())
3172     PCMfile =
3173         CUDie.getAttributeValueAsString(&Unit, dwarf::DW_AT_GNU_dwo_name, "");
3174   if (PCMfile.empty())
3175     return false;
3176
3177   // Clang module DWARF skeleton CUs abuse this for the path to the module.
3178   std::string PCMpath =
3179       CUDie.getAttributeValueAsString(&Unit, dwarf::DW_AT_comp_dir, "");
3180   uint64_t DwoId = getDwoId(CUDie, Unit);
3181
3182   std::string Name =
3183       CUDie.getAttributeValueAsString(&Unit, dwarf::DW_AT_name, "");
3184   if (Name.empty()) {
3185     reportWarning("Anonymous module skeleton CU for " + PCMfile);
3186     return true;
3187   }
3188
3189   if (Options.Verbose) {
3190     outs().indent(Indent);
3191     outs() << "Found clang module reference " << PCMfile;
3192   }
3193
3194   auto Cached = ClangModules.find(PCMfile);
3195   if (Cached != ClangModules.end()) {
3196     if (Cached->second != DwoId)
3197       reportWarning(Twine("hash mismatch: this object file was built against a "
3198                           "different version of the module ") + PCMfile);
3199     if (Options.Verbose)
3200       outs() << " [cached].\n";
3201     return true;
3202   }
3203   if (Options.Verbose)
3204     outs() << " ...\n";
3205
3206   // Cyclic dependencies are disallowed by Clang, but we still
3207   // shouldn't run into an infinite loop, so mark it as processed now.
3208   ClangModules.insert({PCMfile, DwoId});
3209   loadClangModule(PCMfile, PCMpath, Name, DwoId, ModuleMap, Indent + 2);
3210   return true;
3211 }
3212
3213 ErrorOr<const object::ObjectFile &>
3214 DwarfLinker::loadObject(BinaryHolder &BinaryHolder, DebugMapObject &Obj,
3215                         const DebugMap &Map) {
3216   auto ErrOrObjs =
3217       BinaryHolder.GetObjectFiles(Obj.getObjectFilename(), Obj.getTimestamp());
3218   if (std::error_code EC = ErrOrObjs.getError()) {
3219     reportWarning(Twine(Obj.getObjectFilename()) + ": " + EC.message());
3220     return EC;
3221   }
3222   auto ErrOrObj = BinaryHolder.Get(Map.getTriple());
3223   if (std::error_code EC = ErrOrObj.getError())
3224     reportWarning(Twine(Obj.getObjectFilename()) + ": " + EC.message());
3225   return ErrOrObj;
3226 }
3227
3228 void DwarfLinker::loadClangModule(StringRef Filename, StringRef ModulePath,
3229                                   StringRef ModuleName, uint64_t DwoId,
3230                                   DebugMap &ModuleMap, unsigned Indent) {
3231   SmallString<80> Path(Options.PrependPath);
3232   if (sys::path::is_relative(Filename))
3233     sys::path::append(Path, ModulePath, Filename);
3234   else
3235     sys::path::append(Path, Filename);
3236   BinaryHolder ObjHolder(Options.Verbose);
3237   auto &Obj =
3238       ModuleMap.addDebugMapObject(Path, sys::TimeValue::PosixZeroTime());
3239   auto ErrOrObj = loadObject(ObjHolder, Obj, ModuleMap);
3240   if (!ErrOrObj) {
3241     ClangModules.erase(ClangModules.find(Filename));
3242     return;
3243   }
3244
3245   std::unique_ptr<CompileUnit> Unit;
3246
3247   // Setup access to the debug info.
3248   DWARFContextInMemory DwarfContext(*ErrOrObj);
3249   RelocationManager RelocMgr(*this);
3250   for (const auto &CU : DwarfContext.compile_units()) {
3251     auto *CUDie = CU->getUnitDIE(false);
3252     // Recursively get all modules imported by this one.
3253     if (!registerModuleReference(*CUDie, *CU, ModuleMap, Indent)) {
3254       if (Unit) {
3255         errs() << Filename << ": Clang modules are expected to have exactly"
3256                << " 1 compile unit.\n";
3257         exitDsymutil(1);
3258       }
3259       if (getDwoId(*CUDie, *CU) != DwoId)
3260         reportWarning(
3261             Twine("hash mismatch: this object file was built against a "
3262                   "different version of the module ") + Filename);
3263
3264       // Add this module.
3265       Unit = llvm::make_unique<CompileUnit>(*CU, UnitID++, !Options.NoODR,
3266                                             ModuleName);
3267       Unit->setHasInterestingContent();
3268       analyzeContextInfo(CUDie, 0, *Unit, &ODRContexts.getRoot(), StringPool,
3269                          ODRContexts);
3270       // Keep everything.
3271       Unit->markEverythingAsKept();
3272     }
3273   }
3274   if (Options.Verbose) {
3275     outs().indent(Indent);
3276     outs() << "cloning .debug_info from " << Filename << "\n";
3277   }
3278
3279   DIECloner(*this, RelocMgr, DIEAlloc, MutableArrayRef<CompileUnit>(*Unit),
3280             Options)
3281       .cloneAllCompileUnits(DwarfContext);
3282 }
3283
3284 void DwarfLinker::DIECloner::cloneAllCompileUnits(
3285     DWARFContextInMemory &DwarfContext) {
3286   if (!Linker.Streamer)
3287     return;
3288
3289   for (auto &CurrentUnit : CompileUnits) {
3290     const auto *InputDIE = CurrentUnit.getOrigUnit().getUnitDIE();
3291     CurrentUnit.setStartOffset(Linker.OutputDebugInfoSize);
3292     DIE *OutputDIE = cloneDIE(*InputDIE, CurrentUnit, 0 /* PC offset */,
3293                               11 /* Unit Header size */, 0);
3294     CurrentUnit.setOutputUnitDIE(OutputDIE);
3295     Linker.OutputDebugInfoSize = CurrentUnit.computeNextUnitOffset();
3296     if (Linker.Options.NoOutput)
3297       continue;
3298     // FIXME: for compatibility with the classic dsymutil, we emit
3299     // an empty line table for the unit, even if the unit doesn't
3300     // actually exist in the DIE tree.
3301     Linker.patchLineTableForUnit(CurrentUnit, DwarfContext);
3302     if (!OutputDIE)
3303       continue;
3304     Linker.patchRangesForUnit(CurrentUnit, DwarfContext);
3305     Linker.Streamer->emitLocationsForUnit(CurrentUnit, DwarfContext);
3306     Linker.emitAcceleratorEntriesForUnit(CurrentUnit);
3307   }
3308
3309   if (Linker.Options.NoOutput)
3310     return;
3311
3312   // Emit all the compile unit's debug information.
3313   for (auto &CurrentUnit : CompileUnits) {
3314     Linker.generateUnitRanges(CurrentUnit);
3315     CurrentUnit.fixupForwardReferences();
3316     Linker.Streamer->emitCompileUnitHeader(CurrentUnit);
3317     if (!CurrentUnit.getOutputUnitDIE())
3318       continue;
3319     Linker.Streamer->emitDIE(*CurrentUnit.getOutputUnitDIE());
3320   }
3321 }
3322
3323 bool DwarfLinker::link(const DebugMap &Map) {
3324
3325   if (!createStreamer(Map.getTriple(), OutputFilename))
3326     return false;
3327
3328   // Size of the DIEs (and headers) generated for the linked output.
3329   OutputDebugInfoSize = 0;
3330   // A unique ID that identifies each compile unit.
3331   UnitID = 0;
3332   DebugMap ModuleMap(Map.getTriple(), Map.getBinaryPath());
3333
3334   for (const auto &Obj : Map.objects()) {
3335     CurrentDebugObject = Obj.get();
3336
3337     if (Options.Verbose)
3338       outs() << "DEBUG MAP OBJECT: " << Obj->getObjectFilename() << "\n";
3339     auto ErrOrObj = loadObject(BinHolder, *Obj, Map);
3340     if (!ErrOrObj)
3341       continue;
3342
3343     // Look for relocations that correspond to debug map entries.
3344     RelocationManager RelocMgr(*this);
3345     if (!RelocMgr.findValidRelocsInDebugInfo(*ErrOrObj, *Obj)) {
3346       if (Options.Verbose)
3347         outs() << "No valid relocations found. Skipping.\n";
3348       continue;
3349     }
3350
3351     // Setup access to the debug info.
3352     DWARFContextInMemory DwarfContext(*ErrOrObj);
3353     startDebugObject(DwarfContext, *Obj);
3354
3355     // In a first phase, just read in the debug info and load all clang modules.
3356     for (const auto &CU : DwarfContext.compile_units()) {
3357       auto *CUDie = CU->getUnitDIE(false);
3358       if (Options.Verbose) {
3359         outs() << "Input compilation unit:";
3360         CUDie->dump(outs(), CU.get(), 0);
3361       }
3362
3363       if (!registerModuleReference(*CUDie, *CU, ModuleMap))
3364         Units.emplace_back(*CU, UnitID++, !Options.NoODR, "");
3365     }
3366
3367     // Now build the DIE parent links that we will use during the next phase.
3368     for (auto &CurrentUnit : Units)
3369       analyzeContextInfo(CurrentUnit.getOrigUnit().getUnitDIE(), 0, CurrentUnit,
3370                          &ODRContexts.getRoot(), StringPool, ODRContexts);
3371
3372     // Then mark all the DIEs that need to be present in the linked
3373     // output and collect some information about them. Note that this
3374     // loop can not be merged with the previous one becaue cross-cu
3375     // references require the ParentIdx to be setup for every CU in
3376     // the object file before calling this.
3377     for (auto &CurrentUnit : Units)
3378       lookForDIEsToKeep(RelocMgr, *CurrentUnit.getOrigUnit().getUnitDIE(), *Obj,
3379                         CurrentUnit, 0);
3380
3381     // The calls to applyValidRelocs inside cloneDIE will walk the
3382     // reloc array again (in the same way findValidRelocsInDebugInfo()
3383     // did). We need to reset the NextValidReloc index to the beginning.
3384     RelocMgr.resetValidRelocs();
3385     if (RelocMgr.hasValidRelocs())
3386       DIECloner(*this, RelocMgr, DIEAlloc, Units, Options)
3387           .cloneAllCompileUnits(DwarfContext);
3388     if (!Options.NoOutput && !Units.empty())
3389       patchFrameInfoForObject(*Obj, DwarfContext,
3390                               Units[0].getOrigUnit().getAddressByteSize());
3391
3392     // Clean-up before starting working on the next object.
3393     endDebugObject();
3394   }
3395
3396   // Emit everything that's global.
3397   if (!Options.NoOutput) {
3398     Streamer->emitAbbrevs(Abbreviations);
3399     Streamer->emitStrings(StringPool);
3400   }
3401
3402   return Options.NoOutput ? true : Streamer->finish(Map);
3403 }
3404 }
3405
3406 /// \brief Get the offset of string \p S in the string table. This
3407 /// can insert a new element or return the offset of a preexisitng
3408 /// one.
3409 uint32_t NonRelocatableStringpool::getStringOffset(StringRef S) {
3410   if (S.empty() && !Strings.empty())
3411     return 0;
3412
3413   std::pair<uint32_t, StringMapEntryBase *> Entry(0, nullptr);
3414   MapTy::iterator It;
3415   bool Inserted;
3416
3417   // A non-empty string can't be at offset 0, so if we have an entry
3418   // with a 0 offset, it must be a previously interned string.
3419   std::tie(It, Inserted) = Strings.insert(std::make_pair(S, Entry));
3420   if (Inserted || It->getValue().first == 0) {
3421     // Set offset and chain at the end of the entries list.
3422     It->getValue().first = CurrentEndOffset;
3423     CurrentEndOffset += S.size() + 1; // +1 for the '\0'.
3424     Last->getValue().second = &*It;
3425     Last = &*It;
3426   }
3427   return It->getValue().first;
3428 }
3429
3430 /// \brief Put \p S into the StringMap so that it gets permanent
3431 /// storage, but do not actually link it in the chain of elements
3432 /// that go into the output section. A latter call to
3433 /// getStringOffset() with the same string will chain it though.
3434 StringRef NonRelocatableStringpool::internString(StringRef S) {
3435   std::pair<uint32_t, StringMapEntryBase *> Entry(0, nullptr);
3436   auto InsertResult = Strings.insert(std::make_pair(S, Entry));
3437   return InsertResult.first->getKey();
3438 }
3439
3440 void warn(const Twine &Warning, const Twine &Context) {
3441   errs() << Twine("while processing ") + Context + ":\n";
3442   errs() << Twine("warning: ") + Warning + "\n";
3443 }
3444
3445 bool error(const Twine &Error, const Twine &Context) {
3446   errs() << Twine("while processing ") + Context + ":\n";
3447   errs() << Twine("error: ") + Error + "\n";
3448   return false;
3449 }
3450
3451 bool linkDwarf(StringRef OutputFilename, const DebugMap &DM,
3452                const LinkOptions &Options) {
3453   DwarfLinker Linker(OutputFilename, Options);
3454   return Linker.link(DM);
3455 }
3456 }
3457 }