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 "llvm/ADT/IntervalMap.h"
14#include "llvm/ADT/StringMap.h"
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/CodeGen/AsmPrinter.h"
17#include "llvm/CodeGen/DIE.h"
18#include "llvm/DebugInfo/DWARF/DWARFContext.h"
19#include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
20#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
21#include "llvm/MC/MCAsmBackend.h"
22#include "llvm/MC/MCAsmInfo.h"
23#include "llvm/MC/MCContext.h"
24#include "llvm/MC/MCCodeEmitter.h"
25#include "llvm/MC/MCDwarf.h"
26#include "llvm/MC/MCInstrInfo.h"
27#include "llvm/MC/MCObjectFileInfo.h"
28#include "llvm/MC/MCRegisterInfo.h"
29#include "llvm/MC/MCStreamer.h"
30#include "llvm/Object/MachO.h"
31#include "llvm/Support/Dwarf.h"
32#include "llvm/Support/LEB128.h"
33#include "llvm/Support/TargetRegistry.h"
34#include "llvm/Target/TargetMachine.h"
35#include "llvm/Target/TargetOptions.h"
36#include <string>
37#include <tuple>
38
39namespace llvm {
40namespace dsymutil {
41
42namespace {
43
44void warn(const Twine &Warning, const Twine &Context) {
45  errs() << Twine("while processing ") + Context + ":\n";
46  errs() << Twine("warning: ") + Warning + "\n";
47}
48
49bool error(const Twine &Error, const Twine &Context) {
50  errs() << Twine("while processing ") + Context + ":\n";
51  errs() << Twine("error: ") + Error + "\n";
52  return false;
53}
54
55template <typename KeyT, typename ValT>
56using HalfOpenIntervalMap =
57    IntervalMap<KeyT, ValT, IntervalMapImpl::NodeSizer<KeyT, ValT>::LeafSize,
58                IntervalMapHalfOpenInfo<KeyT>>;
59
60typedef HalfOpenIntervalMap<uint64_t, int64_t> FunctionIntervals;
61
62/// \brief Stores all information relating to a compile unit, be it in
63/// its original instance in the object file to its brand new cloned
64/// and linked DIE tree.
65class CompileUnit {
66public:
67  /// \brief Information gathered about a DIE in the object file.
68  struct DIEInfo {
69    int64_t AddrAdjust; ///< Address offset to apply to the described entity.
70    DIE *Clone;         ///< Cloned version of that DIE.
71    uint32_t ParentIdx; ///< The index of this DIE's parent.
72    bool Keep;          ///< Is the DIE part of the linked output?
73    bool InDebugMap;    ///< Was this DIE's entity found in the map?
74  };
75
76  CompileUnit(DWARFUnit &OrigUnit, unsigned ID)
77      : OrigUnit(OrigUnit), ID(ID), LowPc(UINT64_MAX), HighPc(0), RangeAlloc(),
78        Ranges(RangeAlloc), UnitRangeAttribute(nullptr) {
79    Info.resize(OrigUnit.getNumDIEs());
80  }
81
82  CompileUnit(CompileUnit &&RHS)
83      : OrigUnit(RHS.OrigUnit), Info(std::move(RHS.Info)),
84        CUDie(std::move(RHS.CUDie)), StartOffset(RHS.StartOffset),
85        NextUnitOffset(RHS.NextUnitOffset), RangeAlloc(), Ranges(RangeAlloc) {
86    // The CompileUnit container has been 'reserve()'d with the right
87    // size. We cannot move the IntervalMap anyway.
88    llvm_unreachable("CompileUnits should not be moved.");
89  }
90
91  DWARFUnit &getOrigUnit() const { return OrigUnit; }
92
93  unsigned getUniqueID() const { return ID; }
94
95  DIE *getOutputUnitDIE() const { return CUDie.get(); }
96  void setOutputUnitDIE(DIE *Die) { CUDie.reset(Die); }
97
98  DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; }
99  const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; }
100
101  uint64_t getStartOffset() const { return StartOffset; }
102  uint64_t getNextUnitOffset() const { return NextUnitOffset; }
103  void setStartOffset(uint64_t DebugInfoSize) { StartOffset = DebugInfoSize; }
104
105  uint64_t getLowPc() const { return LowPc; }
106  uint64_t getHighPc() const { return HighPc; }
107
108  DIEInteger *getUnitRangesAttribute() const { return UnitRangeAttribute; }
109  const FunctionIntervals &getFunctionRanges() const { return Ranges; }
110  const std::vector<DIEInteger *> &getRangesAttributes() const {
111    return RangeAttributes;
112  }
113
114  const std::vector<std::pair<DIEInteger *, int64_t>> &
115  getLocationAttributes() const {
116    return LocationAttributes;
117  }
118
119  /// \brief Compute the end offset for this unit. Must be
120  /// called after the CU's DIEs have been cloned.
121  /// \returns the next unit offset (which is also the current
122  /// debug_info section size).
123  uint64_t computeNextUnitOffset();
124
125  /// \brief Keep track of a forward reference to DIE \p Die in \p
126  /// RefUnit by \p Attr. The attribute should be fixed up later to
127  /// point to the absolute offset of \p Die in the debug_info section.
128  void noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
129                            DIEInteger *Attr);
130
131  /// \brief Apply all fixups recored by noteForwardReference().
132  void fixupForwardReferences();
133
134  /// \brief Add a function range [\p LowPC, \p HighPC) that is
135  /// relocatad by applying offset \p PCOffset.
136  void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset);
137
138  /// \brief Keep track of a DW_AT_range attribute that we will need to
139  /// patch up later.
140  void noteRangeAttribute(const DIE &Die, DIEInteger *Attr);
141
142  /// \brief Keep track of a location attribute pointing to a location
143  /// list in the debug_loc section.
144  void noteLocationAttribute(DIEInteger *Attr, int64_t PcOffset);
145
146  /// \brief Add a name accelerator entry for \p Die with \p Name
147  /// which is stored in the string table at \p Offset.
148  void addNameAccelerator(const DIE *Die, const char *Name, uint32_t Offset,
149                          bool SkipPubnamesSection = false);
150
151  /// \brief Add a type accelerator entry for \p Die with \p Name
152  /// which is stored in the string table at \p Offset.
153  void addTypeAccelerator(const DIE *Die, const char *Name, uint32_t Offset);
154
155  struct AccelInfo {
156    StringRef Name; ///< Name of the entry.
157    const DIE *Die; ///< DIE this entry describes.
158    uint32_t NameOffset; ///< Offset of Name in the string pool.
159    bool SkipPubSection; ///< Emit this entry only in the apple_* sections.
160
161    AccelInfo(StringRef Name, const DIE *Die, uint32_t NameOffset,
162              bool SkipPubSection = false)
163        : Name(Name), Die(Die), NameOffset(NameOffset),
164          SkipPubSection(SkipPubSection) {}
165  };
166
167  const std::vector<AccelInfo> &getPubnames() const { return Pubnames; }
168  const std::vector<AccelInfo> &getPubtypes() const { return Pubtypes; }
169
170private:
171  DWARFUnit &OrigUnit;
172  unsigned ID;
173  std::vector<DIEInfo> Info;  ///< DIE info indexed by DIE index.
174  std::unique_ptr<DIE> CUDie; ///< Root of the linked DIE tree.
175
176  uint64_t StartOffset;
177  uint64_t NextUnitOffset;
178
179  uint64_t LowPc;
180  uint64_t HighPc;
181
182  /// \brief A list of attributes to fixup with the absolute offset of
183  /// a DIE in the debug_info section.
184  ///
185  /// The offsets for the attributes in this array couldn't be set while
186  /// cloning because for cross-cu forward refences the target DIE's
187  /// offset isn't known you emit the reference attribute.
188  std::vector<std::tuple<DIE *, const CompileUnit *, DIEInteger *>>
189      ForwardDIEReferences;
190
191  FunctionIntervals::Allocator RangeAlloc;
192  /// \brief The ranges in that interval map are the PC ranges for
193  /// functions in this unit, associated with the PC offset to apply
194  /// to the addresses to get the linked address.
195  FunctionIntervals Ranges;
196
197  /// \brief DW_AT_ranges attributes to patch after we have gathered
198  /// all the unit's function addresses.
199  /// @{
200  std::vector<DIEInteger *> RangeAttributes;
201  DIEInteger *UnitRangeAttribute;
202  /// @}
203
204  /// \brief Location attributes that need to be transfered from th
205  /// original debug_loc section to the liked one. They are stored
206  /// along with the PC offset that is to be applied to their
207  /// function's address.
208  std::vector<std::pair<DIEInteger *, int64_t>> LocationAttributes;
209
210  /// \brief Accelerator entries for the unit, both for the pub*
211  /// sections and the apple* ones.
212  /// @{
213  std::vector<AccelInfo> Pubnames;
214  std::vector<AccelInfo> Pubtypes;
215  /// @}
216};
217
218uint64_t CompileUnit::computeNextUnitOffset() {
219  NextUnitOffset = StartOffset + 11 /* Header size */;
220  // The root DIE might be null, meaning that the Unit had nothing to
221  // contribute to the linked output. In that case, we will emit the
222  // unit header without any actual DIE.
223  if (CUDie)
224    NextUnitOffset += CUDie->getSize();
225  return NextUnitOffset;
226}
227
228/// \brief Keep track of a forward cross-cu reference from this unit
229/// to \p Die that lives in \p RefUnit.
230void CompileUnit::noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
231                                       DIEInteger *Attr) {
232  ForwardDIEReferences.emplace_back(Die, RefUnit, Attr);
233}
234
235/// \brief Apply all fixups recorded by noteForwardReference().
236void CompileUnit::fixupForwardReferences() {
237  for (const auto &Ref : ForwardDIEReferences) {
238    DIE *RefDie;
239    const CompileUnit *RefUnit;
240    DIEInteger *Attr;
241    std::tie(RefDie, RefUnit, Attr) = Ref;
242    Attr->setValue(RefDie->getOffset() + RefUnit->getStartOffset());
243  }
244}
245
246void CompileUnit::addFunctionRange(uint64_t FuncLowPc, uint64_t FuncHighPc,
247                                   int64_t PcOffset) {
248  Ranges.insert(FuncLowPc, FuncHighPc, PcOffset);
249  this->LowPc = std::min(LowPc, FuncLowPc + PcOffset);
250  this->HighPc = std::max(HighPc, FuncHighPc + PcOffset);
251}
252
253void CompileUnit::noteRangeAttribute(const DIE &Die, DIEInteger *Attr) {
254  if (Die.getTag() != dwarf::DW_TAG_compile_unit)
255    RangeAttributes.push_back(Attr);
256  else
257    UnitRangeAttribute = Attr;
258}
259
260void CompileUnit::noteLocationAttribute(DIEInteger *Attr, int64_t PcOffset) {
261  LocationAttributes.emplace_back(Attr, PcOffset);
262}
263
264/// \brief Add a name accelerator entry for \p Die with \p Name
265/// which is stored in the string table at \p Offset.
266void CompileUnit::addNameAccelerator(const DIE *Die, const char *Name,
267                                     uint32_t Offset, bool SkipPubSection) {
268  Pubnames.emplace_back(Name, Die, Offset, SkipPubSection);
269}
270
271/// \brief Add a type accelerator entry for \p Die with \p Name
272/// which is stored in the string table at \p Offset.
273void CompileUnit::addTypeAccelerator(const DIE *Die, const char *Name,
274                                     uint32_t Offset) {
275  Pubtypes.emplace_back(Name, Die, Offset, false);
276}
277
278/// \brief A string table that doesn't need relocations.
279///
280/// We are doing a final link, no need for a string table that
281/// has relocation entries for every reference to it. This class
282/// provides this ablitity by just associating offsets with
283/// strings.
284class NonRelocatableStringpool {
285public:
286  /// \brief Entries are stored into the StringMap and simply linked
287  /// together through the second element of this pair in order to
288  /// keep track of insertion order.
289  typedef StringMap<std::pair<uint32_t, StringMapEntryBase *>, BumpPtrAllocator>
290      MapTy;
291
292  NonRelocatableStringpool()
293      : CurrentEndOffset(0), Sentinel(0), Last(&Sentinel) {
294    // Legacy dsymutil puts an empty string at the start of the line
295    // table.
296    getStringOffset("");
297  }
298
299  /// \brief Get the offset of string \p S in the string table. This
300  /// can insert a new element or return the offset of a preexisitng
301  /// one.
302  uint32_t getStringOffset(StringRef S);
303
304  /// \brief Get permanent storage for \p S (but do not necessarily
305  /// emit \p S in the output section).
306  /// \returns The StringRef that points to permanent storage to use
307  /// in place of \p S.
308  StringRef internString(StringRef S);
309
310  // \brief Return the first entry of the string table.
311  const MapTy::MapEntryTy *getFirstEntry() const {
312    return getNextEntry(&Sentinel);
313  }
314
315  // \brief Get the entry following \p E in the string table or null
316  // if \p E was the last entry.
317  const MapTy::MapEntryTy *getNextEntry(const MapTy::MapEntryTy *E) const {
318    return static_cast<const MapTy::MapEntryTy *>(E->getValue().second);
319  }
320
321  uint64_t getSize() { return CurrentEndOffset; }
322
323private:
324  MapTy Strings;
325  uint32_t CurrentEndOffset;
326  MapTy::MapEntryTy Sentinel, *Last;
327};
328
329/// \brief Get the offset of string \p S in the string table. This
330/// can insert a new element or return the offset of a preexisitng
331/// one.
332uint32_t NonRelocatableStringpool::getStringOffset(StringRef S) {
333  if (S.empty() && !Strings.empty())
334    return 0;
335
336  std::pair<uint32_t, StringMapEntryBase *> Entry(0, nullptr);
337  MapTy::iterator It;
338  bool Inserted;
339
340  // A non-empty string can't be at offset 0, so if we have an entry
341  // with a 0 offset, it must be a previously interned string.
342  std::tie(It, Inserted) = Strings.insert(std::make_pair(S, Entry));
343  if (Inserted || It->getValue().first == 0) {
344    // Set offset and chain at the end of the entries list.
345    It->getValue().first = CurrentEndOffset;
346    CurrentEndOffset += S.size() + 1; // +1 for the '\0'.
347    Last->getValue().second = &*It;
348    Last = &*It;
349  }
350  return It->getValue().first;
351}
352
353/// \brief Put \p S into the StringMap so that it gets permanent
354/// storage, but do not actually link it in the chain of elements
355/// that go into the output section. A latter call to
356/// getStringOffset() with the same string will chain it though.
357StringRef NonRelocatableStringpool::internString(StringRef S) {
358  std::pair<uint32_t, StringMapEntryBase *> Entry(0, nullptr);
359  auto InsertResult = Strings.insert(std::make_pair(S, Entry));
360  return InsertResult.first->getKey();
361}
362
363/// \brief The Dwarf streaming logic
364///
365/// All interactions with the MC layer that is used to build the debug
366/// information binary representation are handled in this class.
367class DwarfStreamer {
368  /// \defgroup MCObjects MC layer objects constructed by the streamer
369  /// @{
370  std::unique_ptr<MCRegisterInfo> MRI;
371  std::unique_ptr<MCAsmInfo> MAI;
372  std::unique_ptr<MCObjectFileInfo> MOFI;
373  std::unique_ptr<MCContext> MC;
374  MCAsmBackend *MAB; // Owned by MCStreamer
375  std::unique_ptr<MCInstrInfo> MII;
376  std::unique_ptr<MCSubtargetInfo> MSTI;
377  MCCodeEmitter *MCE; // Owned by MCStreamer
378  MCStreamer *MS;     // Owned by AsmPrinter
379  std::unique_ptr<TargetMachine> TM;
380  std::unique_ptr<AsmPrinter> Asm;
381  /// @}
382
383  /// \brief the file we stream the linked Dwarf to.
384  std::unique_ptr<raw_fd_ostream> OutFile;
385
386  uint32_t RangesSectionSize;
387  uint32_t LocSectionSize;
388  uint32_t LineSectionSize;
389
390  /// \brief Emit the pubnames or pubtypes section contribution for \p
391  /// Unit into \p Sec. The data is provided in \p Names.
392  void emitPubSectionForUnit(const MCSection *Sec, StringRef Name,
393                             const CompileUnit &Unit,
394                             const std::vector<CompileUnit::AccelInfo> &Names);
395
396public:
397  /// \brief Actually create the streamer and the ouptut file.
398  ///
399  /// This could be done directly in the constructor, but it feels
400  /// more natural to handle errors through return value.
401  bool init(Triple TheTriple, StringRef OutputFilename);
402
403  /// \brief Dump the file to the disk.
404  bool finish();
405
406  AsmPrinter &getAsmPrinter() const { return *Asm; }
407
408  /// \brief Set the current output section to debug_info and change
409  /// the MC Dwarf version to \p DwarfVersion.
410  void switchToDebugInfoSection(unsigned DwarfVersion);
411
412  /// \brief Emit the compilation unit header for \p Unit in the
413  /// debug_info section.
414  ///
415  /// As a side effect, this also switches the current Dwarf version
416  /// of the MC layer to the one of U.getOrigUnit().
417  void emitCompileUnitHeader(CompileUnit &Unit);
418
419  /// \brief Recursively emit the DIE tree rooted at \p Die.
420  void emitDIE(DIE &Die);
421
422  /// \brief Emit the abbreviation table \p Abbrevs to the
423  /// debug_abbrev section.
424  void emitAbbrevs(const std::vector<DIEAbbrev *> &Abbrevs);
425
426  /// \brief Emit the string table described by \p Pool.
427  void emitStrings(const NonRelocatableStringpool &Pool);
428
429  /// \brief Emit debug_ranges for \p FuncRange by translating the
430  /// original \p Entries.
431  void emitRangesEntries(
432      int64_t UnitPcOffset, uint64_t OrigLowPc,
433      FunctionIntervals::const_iterator FuncRange,
434      const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
435      unsigned AddressSize);
436
437  /// \brief Emit debug_aranges entries for \p Unit and if \p
438  /// DoRangesSection is true, also emit the debug_ranges entries for
439  /// the DW_TAG_compile_unit's DW_AT_ranges attribute.
440  void emitUnitRangesEntries(CompileUnit &Unit, bool DoRangesSection);
441
442  uint32_t getRangesSectionSize() const { return RangesSectionSize; }
443
444  /// \brief Emit the debug_loc contribution for \p Unit by copying
445  /// the entries from \p Dwarf and offseting them. Update the
446  /// location attributes to point to the new entries.
447  void emitLocationsForUnit(const CompileUnit &Unit, DWARFContext &Dwarf);
448
449  /// \brief Emit the line table described in \p Rows into the
450  /// debug_line section.
451  void emitLineTableForUnit(StringRef PrologueBytes, unsigned MinInstLength,
452                            std::vector<DWARFDebugLine::Row> &Rows,
453                            unsigned AdddressSize);
454
455  uint32_t getLineSectionSize() const { return LineSectionSize; }
456
457  /// \brief Emit the .debug_pubnames contribution for \p Unit.
458  void emitPubNamesForUnit(const CompileUnit &Unit);
459
460  /// \brief Emit the .debug_pubtypes contribution for \p Unit.
461  void emitPubTypesForUnit(const CompileUnit &Unit);
462};
463
464bool DwarfStreamer::init(Triple TheTriple, StringRef OutputFilename) {
465  std::string ErrorStr;
466  std::string TripleName;
467  StringRef Context = "dwarf streamer init";
468
469  // Get the target.
470  const Target *TheTarget =
471      TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr);
472  if (!TheTarget)
473    return error(ErrorStr, Context);
474  TripleName = TheTriple.getTriple();
475
476  // Create all the MC Objects.
477  MRI.reset(TheTarget->createMCRegInfo(TripleName));
478  if (!MRI)
479    return error(Twine("no register info for target ") + TripleName, Context);
480
481  MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName));
482  if (!MAI)
483    return error("no asm info for target " + TripleName, Context);
484
485  MOFI.reset(new MCObjectFileInfo);
486  MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get()));
487  MOFI->InitMCObjectFileInfo(TripleName, Reloc::Default, CodeModel::Default,
488                             *MC);
489
490  MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
491  if (!MAB)
492    return error("no asm backend for target " + TripleName, Context);
493
494  MII.reset(TheTarget->createMCInstrInfo());
495  if (!MII)
496    return error("no instr info info for target " + TripleName, Context);
497
498  MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
499  if (!MSTI)
500    return error("no subtarget info for target " + TripleName, Context);
501
502  MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MC);
503  if (!MCE)
504    return error("no code emitter for target " + TripleName, Context);
505
506  // Create the output file.
507  std::error_code EC;
508  OutFile =
509      llvm::make_unique<raw_fd_ostream>(OutputFilename, EC, sys::fs::F_None);
510  if (EC)
511    return error(Twine(OutputFilename) + ": " + EC.message(), Context);
512
513  MS = TheTarget->createMCObjectStreamer(TheTriple, *MC, *MAB, *OutFile, MCE,
514                                         *MSTI, false,
515                                         /*DWARFMustBeAtTheEnd*/ false);
516  if (!MS)
517    return error("no object streamer for target " + TripleName, Context);
518
519  // Finally create the AsmPrinter we'll use to emit the DIEs.
520  TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions()));
521  if (!TM)
522    return error("no target machine for target " + TripleName, Context);
523
524  Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS)));
525  if (!Asm)
526    return error("no asm printer for target " + TripleName, Context);
527
528  RangesSectionSize = 0;
529  LocSectionSize = 0;
530  LineSectionSize = 0;
531
532  return true;
533}
534
535bool DwarfStreamer::finish() {
536  MS->Finish();
537  return true;
538}
539
540/// \brief Set the current output section to debug_info and change
541/// the MC Dwarf version to \p DwarfVersion.
542void DwarfStreamer::switchToDebugInfoSection(unsigned DwarfVersion) {
543  MS->SwitchSection(MOFI->getDwarfInfoSection());
544  MC->setDwarfVersion(DwarfVersion);
545}
546
547/// \brief Emit the compilation unit header for \p Unit in the
548/// debug_info section.
549///
550/// A Dwarf scetion header is encoded as:
551///  uint32_t   Unit length (omiting this field)
552///  uint16_t   Version
553///  uint32_t   Abbreviation table offset
554///  uint8_t    Address size
555///
556/// Leading to a total of 11 bytes.
557void DwarfStreamer::emitCompileUnitHeader(CompileUnit &Unit) {
558  unsigned Version = Unit.getOrigUnit().getVersion();
559  switchToDebugInfoSection(Version);
560
561  // Emit size of content not including length itself. The size has
562  // already been computed in CompileUnit::computeOffsets(). Substract
563  // 4 to that size to account for the length field.
564  Asm->EmitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset() - 4);
565  Asm->EmitInt16(Version);
566  // We share one abbreviations table across all units so it's always at the
567  // start of the section.
568  Asm->EmitInt32(0);
569  Asm->EmitInt8(Unit.getOrigUnit().getAddressByteSize());
570}
571
572/// \brief Emit the \p Abbrevs array as the shared abbreviation table
573/// for the linked Dwarf file.
574void DwarfStreamer::emitAbbrevs(const std::vector<DIEAbbrev *> &Abbrevs) {
575  MS->SwitchSection(MOFI->getDwarfAbbrevSection());
576  Asm->emitDwarfAbbrevs(Abbrevs);
577}
578
579/// \brief Recursively emit the DIE tree rooted at \p Die.
580void DwarfStreamer::emitDIE(DIE &Die) {
581  MS->SwitchSection(MOFI->getDwarfInfoSection());
582  Asm->emitDwarfDIE(Die);
583}
584
585/// \brief Emit the debug_str section stored in \p Pool.
586void DwarfStreamer::emitStrings(const NonRelocatableStringpool &Pool) {
587  Asm->OutStreamer.SwitchSection(MOFI->getDwarfStrSection());
588  for (auto *Entry = Pool.getFirstEntry(); Entry;
589       Entry = Pool.getNextEntry(Entry))
590    Asm->OutStreamer.EmitBytes(
591        StringRef(Entry->getKey().data(), Entry->getKey().size() + 1));
592}
593
594/// \brief Emit the debug_range section contents for \p FuncRange by
595/// translating the original \p Entries. The debug_range section
596/// format is totally trivial, consisting just of pairs of address
597/// sized addresses describing the ranges.
598void DwarfStreamer::emitRangesEntries(
599    int64_t UnitPcOffset, uint64_t OrigLowPc,
600    FunctionIntervals::const_iterator FuncRange,
601    const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
602    unsigned AddressSize) {
603  MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
604
605  // Offset each range by the right amount.
606  int64_t PcOffset = FuncRange.value() + UnitPcOffset;
607  for (const auto &Range : Entries) {
608    if (Range.isBaseAddressSelectionEntry(AddressSize)) {
609      warn("unsupported base address selection operation",
610           "emitting debug_ranges");
611      break;
612    }
613    // Do not emit empty ranges.
614    if (Range.StartAddress == Range.EndAddress)
615      continue;
616
617    // All range entries should lie in the function range.
618    if (!(Range.StartAddress + OrigLowPc >= FuncRange.start() &&
619          Range.EndAddress + OrigLowPc <= FuncRange.stop()))
620      warn("inconsistent range data.", "emitting debug_ranges");
621    MS->EmitIntValue(Range.StartAddress + PcOffset, AddressSize);
622    MS->EmitIntValue(Range.EndAddress + PcOffset, AddressSize);
623    RangesSectionSize += 2 * AddressSize;
624  }
625
626  // Add the terminator entry.
627  MS->EmitIntValue(0, AddressSize);
628  MS->EmitIntValue(0, AddressSize);
629  RangesSectionSize += 2 * AddressSize;
630}
631
632/// \brief Emit the debug_aranges contribution of a unit and
633/// if \p DoDebugRanges is true the debug_range contents for a
634/// compile_unit level DW_AT_ranges attribute (Which are basically the
635/// same thing with a different base address).
636/// Just aggregate all the ranges gathered inside that unit.
637void DwarfStreamer::emitUnitRangesEntries(CompileUnit &Unit,
638                                          bool DoDebugRanges) {
639  unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
640  // Gather the ranges in a vector, so that we can simplify them. The
641  // IntervalMap will have coalesced the non-linked ranges, but here
642  // we want to coalesce the linked addresses.
643  std::vector<std::pair<uint64_t, uint64_t>> Ranges;
644  const auto &FunctionRanges = Unit.getFunctionRanges();
645  for (auto Range = FunctionRanges.begin(), End = FunctionRanges.end();
646       Range != End; ++Range)
647    Ranges.push_back(std::make_pair(Range.start() + Range.value(),
648                                    Range.stop() + Range.value()));
649
650  // The object addresses where sorted, but again, the linked
651  // addresses might end up in a different order.
652  std::sort(Ranges.begin(), Ranges.end());
653
654  if (!Ranges.empty()) {
655    MS->SwitchSection(MC->getObjectFileInfo()->getDwarfARangesSection());
656
657    MCSymbol *BeginLabel = Asm->createTempSymbol("Barange");
658    MCSymbol *EndLabel = Asm->createTempSymbol("Earange");
659
660    unsigned HeaderSize =
661        sizeof(int32_t) + // Size of contents (w/o this field
662        sizeof(int16_t) + // DWARF ARange version number
663        sizeof(int32_t) + // Offset of CU in the .debug_info section
664        sizeof(int8_t) +  // Pointer Size (in bytes)
665        sizeof(int8_t);   // Segment Size (in bytes)
666
667    unsigned TupleSize = AddressSize * 2;
668    unsigned Padding = OffsetToAlignment(HeaderSize, TupleSize);
669
670    Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); // Arange length
671    Asm->OutStreamer.EmitLabel(BeginLabel);
672    Asm->EmitInt16(dwarf::DW_ARANGES_VERSION); // Version number
673    Asm->EmitInt32(Unit.getStartOffset());     // Corresponding unit's offset
674    Asm->EmitInt8(AddressSize);                // Address size
675    Asm->EmitInt8(0);                          // Segment size
676
677    Asm->OutStreamer.EmitFill(Padding, 0x0);
678
679    for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End;
680         ++Range) {
681      uint64_t RangeStart = Range->first;
682      MS->EmitIntValue(RangeStart, AddressSize);
683      while ((Range + 1) != End && Range->second == (Range + 1)->first)
684        ++Range;
685      MS->EmitIntValue(Range->second - RangeStart, AddressSize);
686    }
687
688    // Emit terminator
689    Asm->OutStreamer.EmitIntValue(0, AddressSize);
690    Asm->OutStreamer.EmitIntValue(0, AddressSize);
691    Asm->OutStreamer.EmitLabel(EndLabel);
692  }
693
694  if (!DoDebugRanges)
695    return;
696
697  MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
698  // Offset each range by the right amount.
699  int64_t PcOffset = -Unit.getLowPc();
700  // Emit coalesced ranges.
701  for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End; ++Range) {
702    MS->EmitIntValue(Range->first + PcOffset, AddressSize);
703    while (Range + 1 != End && Range->second == (Range + 1)->first)
704      ++Range;
705    MS->EmitIntValue(Range->second + PcOffset, AddressSize);
706    RangesSectionSize += 2 * AddressSize;
707  }
708
709  // Add the terminator entry.
710  MS->EmitIntValue(0, AddressSize);
711  MS->EmitIntValue(0, AddressSize);
712  RangesSectionSize += 2 * AddressSize;
713}
714
715/// \brief Emit location lists for \p Unit and update attribtues to
716/// point to the new entries.
717void DwarfStreamer::emitLocationsForUnit(const CompileUnit &Unit,
718                                         DWARFContext &Dwarf) {
719  const std::vector<std::pair<DIEInteger *, int64_t>> &Attributes =
720      Unit.getLocationAttributes();
721
722  if (Attributes.empty())
723    return;
724
725  MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLocSection());
726
727  unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
728  const DWARFSection &InputSec = Dwarf.getLocSection();
729  DataExtractor Data(InputSec.Data, Dwarf.isLittleEndian(), AddressSize);
730  DWARFUnit &OrigUnit = Unit.getOrigUnit();
731  const auto *OrigUnitDie = OrigUnit.getCompileUnitDIE(false);
732  int64_t UnitPcOffset = 0;
733  uint64_t OrigLowPc = OrigUnitDie->getAttributeValueAsAddress(
734      &OrigUnit, dwarf::DW_AT_low_pc, -1ULL);
735  if (OrigLowPc != -1ULL)
736    UnitPcOffset = int64_t(OrigLowPc) - Unit.getLowPc();
737
738  for (const auto &Attr : Attributes) {
739    uint32_t Offset = Attr.first->getValue();
740    Attr.first->setValue(LocSectionSize);
741    // This is the quantity to add to the old location address to get
742    // the correct address for the new one.
743    int64_t LocPcOffset = Attr.second + UnitPcOffset;
744    while (Data.isValidOffset(Offset)) {
745      uint64_t Low = Data.getUnsigned(&Offset, AddressSize);
746      uint64_t High = Data.getUnsigned(&Offset, AddressSize);
747      LocSectionSize += 2 * AddressSize;
748      if (Low == 0 && High == 0) {
749        Asm->OutStreamer.EmitIntValue(0, AddressSize);
750        Asm->OutStreamer.EmitIntValue(0, AddressSize);
751        break;
752      }
753      Asm->OutStreamer.EmitIntValue(Low + LocPcOffset, AddressSize);
754      Asm->OutStreamer.EmitIntValue(High + LocPcOffset, AddressSize);
755      uint64_t Length = Data.getU16(&Offset);
756      Asm->OutStreamer.EmitIntValue(Length, 2);
757      // Just copy the bytes over.
758      Asm->OutStreamer.EmitBytes(
759          StringRef(InputSec.Data.substr(Offset, Length)));
760      Offset += Length;
761      LocSectionSize += Length + 2;
762    }
763  }
764}
765
766void DwarfStreamer::emitLineTableForUnit(StringRef PrologueBytes,
767                                         unsigned MinInstLength,
768                                         std::vector<DWARFDebugLine::Row> &Rows,
769                                         unsigned PointerSize) {
770  // Switch to the section where the table will be emitted into.
771  MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLineSection());
772  MCSymbol *LineStartSym = MC->CreateTempSymbol();
773  MCSymbol *LineEndSym = MC->CreateTempSymbol();
774
775  // The first 4 bytes is the total length of the information for this
776  // compilation unit (not including these 4 bytes for the length).
777  Asm->EmitLabelDifference(LineEndSym, LineStartSym, 4);
778  Asm->OutStreamer.EmitLabel(LineStartSym);
779  // Copy Prologue.
780  MS->EmitBytes(PrologueBytes);
781  LineSectionSize += PrologueBytes.size() + 4;
782
783  SmallString<128> EncodingBuffer;
784  raw_svector_ostream EncodingOS(EncodingBuffer);
785
786  if (Rows.empty()) {
787    // We only have the dummy entry, dsymutil emits an entry with a 0
788    // address in that case.
789    MCDwarfLineAddr::Encode(*MC, INT64_MAX, 0, EncodingOS);
790    MS->EmitBytes(EncodingOS.str());
791    LineSectionSize += EncodingBuffer.size();
792    MS->EmitLabel(LineEndSym);
793    return;
794  }
795
796  // Line table state machine fields
797  unsigned FileNum = 1;
798  unsigned LastLine = 1;
799  unsigned Column = 0;
800  unsigned IsStatement = 1;
801  unsigned Isa = 0;
802  uint64_t Address = -1ULL;
803
804  unsigned RowsSinceLastSequence = 0;
805
806  for (unsigned Idx = 0; Idx < Rows.size(); ++Idx) {
807    auto &Row = Rows[Idx];
808
809    int64_t AddressDelta;
810    if (Address == -1ULL) {
811      MS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
812      MS->EmitULEB128IntValue(PointerSize + 1);
813      MS->EmitIntValue(dwarf::DW_LNE_set_address, 1);
814      MS->EmitIntValue(Row.Address, PointerSize);
815      LineSectionSize += 2 + PointerSize + getULEB128Size(PointerSize + 1);
816      AddressDelta = 0;
817    } else {
818      AddressDelta = (Row.Address - Address) / MinInstLength;
819    }
820
821    // FIXME: code copied and transfromed from
822    // MCDwarf.cpp::EmitDwarfLineTable. We should find a way to share
823    // this code, but the current compatibility requirement with
824    // classic dsymutil makes it hard. Revisit that once this
825    // requirement is dropped.
826
827    if (FileNum != Row.File) {
828      FileNum = Row.File;
829      MS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
830      MS->EmitULEB128IntValue(FileNum);
831      LineSectionSize += 1 + getULEB128Size(FileNum);
832    }
833    if (Column != Row.Column) {
834      Column = Row.Column;
835      MS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
836      MS->EmitULEB128IntValue(Column);
837      LineSectionSize += 1 + getULEB128Size(Column);
838    }
839
840    // FIXME: We should handle the discriminator here, but dsymutil
841    // doesn' consider it, thus ignore it for now.
842
843    if (Isa != Row.Isa) {
844      Isa = Row.Isa;
845      MS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
846      MS->EmitULEB128IntValue(Isa);
847      LineSectionSize += 1 + getULEB128Size(Isa);
848    }
849    if (IsStatement != Row.IsStmt) {
850      IsStatement = Row.IsStmt;
851      MS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
852      LineSectionSize += 1;
853    }
854    if (Row.BasicBlock) {
855      MS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
856      LineSectionSize += 1;
857    }
858
859    if (Row.PrologueEnd) {
860      MS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
861      LineSectionSize += 1;
862    }
863
864    if (Row.EpilogueBegin) {
865      MS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
866      LineSectionSize += 1;
867    }
868
869    int64_t LineDelta = int64_t(Row.Line) - LastLine;
870    if (!Row.EndSequence) {
871      MCDwarfLineAddr::Encode(*MC, LineDelta, AddressDelta, EncodingOS);
872      MS->EmitBytes(EncodingOS.str());
873      LineSectionSize += EncodingBuffer.size();
874      EncodingBuffer.resize(0);
875      EncodingOS.resync();
876      Address = Row.Address;
877      LastLine = Row.Line;
878      RowsSinceLastSequence++;
879    } else {
880      if (LineDelta) {
881        MS->EmitIntValue(dwarf::DW_LNS_advance_line, 1);
882        MS->EmitSLEB128IntValue(LineDelta);
883        LineSectionSize += 1 + getSLEB128Size(LineDelta);
884      }
885      if (AddressDelta) {
886        MS->EmitIntValue(dwarf::DW_LNS_advance_pc, 1);
887        MS->EmitULEB128IntValue(AddressDelta);
888        LineSectionSize += 1 + getULEB128Size(AddressDelta);
889      }
890      MCDwarfLineAddr::Encode(*MC, INT64_MAX, 0, EncodingOS);
891      MS->EmitBytes(EncodingOS.str());
892      LineSectionSize += EncodingBuffer.size();
893      EncodingBuffer.resize(0);
894      EncodingOS.resync();
895      Address = -1ULL;
896      LastLine = FileNum = IsStatement = 1;
897      RowsSinceLastSequence = Column = Isa = 0;
898    }
899  }
900
901  if (RowsSinceLastSequence) {
902    MCDwarfLineAddr::Encode(*MC, INT64_MAX, 0, EncodingOS);
903    MS->EmitBytes(EncodingOS.str());
904    LineSectionSize += EncodingBuffer.size();
905    EncodingBuffer.resize(0);
906    EncodingOS.resync();
907  }
908
909  MS->EmitLabel(LineEndSym);
910}
911
912/// \brief Emit the pubnames or pubtypes section contribution for \p
913/// Unit into \p Sec. The data is provided in \p Names.
914void DwarfStreamer::emitPubSectionForUnit(
915    const MCSection *Sec, StringRef SecName, const CompileUnit &Unit,
916    const std::vector<CompileUnit::AccelInfo> &Names) {
917  if (Names.empty())
918    return;
919
920  // Start the dwarf pubnames section.
921  Asm->OutStreamer.SwitchSection(Sec);
922  MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + SecName + "_begin");
923  MCSymbol *EndLabel = Asm->createTempSymbol("pub" + SecName + "_end");
924
925  bool HeaderEmitted = false;
926  // Emit the pubnames for this compilation unit.
927  for (const auto &Name : Names) {
928    if (Name.SkipPubSection)
929      continue;
930
931    if (!HeaderEmitted) {
932      // Emit the header.
933      Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); // Length
934      Asm->OutStreamer.EmitLabel(BeginLabel);
935      Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION); // Version
936      Asm->EmitInt32(Unit.getStartOffset()); // Unit offset
937      Asm->EmitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset()); // Size
938      HeaderEmitted = true;
939    }
940    Asm->EmitInt32(Name.Die->getOffset());
941    Asm->OutStreamer.EmitBytes(
942        StringRef(Name.Name.data(), Name.Name.size() + 1));
943  }
944
945  if (!HeaderEmitted)
946    return;
947  Asm->EmitInt32(0); // End marker.
948  Asm->OutStreamer.EmitLabel(EndLabel);
949}
950
951/// \brief Emit .debug_pubnames for \p Unit.
952void DwarfStreamer::emitPubNamesForUnit(const CompileUnit &Unit) {
953  emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubNamesSection(),
954                        "names", Unit, Unit.getPubnames());
955}
956
957/// \brief Emit .debug_pubtypes for \p Unit.
958void DwarfStreamer::emitPubTypesForUnit(const CompileUnit &Unit) {
959  emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubTypesSection(),
960                        "types", Unit, Unit.getPubtypes());
961}
962
963/// \brief The core of the Dwarf linking logic.
964///
965/// The link of the dwarf information from the object files will be
966/// driven by the selection of 'root DIEs', which are DIEs that
967/// describe variables or functions that are present in the linked
968/// binary (and thus have entries in the debug map). All the debug
969/// information that will be linked (the DIEs, but also the line
970/// tables, ranges, ...) is derived from that set of root DIEs.
971///
972/// The root DIEs are identified because they contain relocations that
973/// correspond to a debug map entry at specific places (the low_pc for
974/// a function, the location for a variable). These relocations are
975/// called ValidRelocs in the DwarfLinker and are gathered as a very
976/// first step when we start processing a DebugMapObject.
977class DwarfLinker {
978public:
979  DwarfLinker(StringRef OutputFilename, const LinkOptions &Options)
980      : OutputFilename(OutputFilename), Options(Options),
981        BinHolder(Options.Verbose) {}
982
983  ~DwarfLinker() {
984    for (auto *Abbrev : Abbreviations)
985      delete Abbrev;
986  }
987
988  /// \brief Link the contents of the DebugMap.
989  bool link(const DebugMap &);
990
991private:
992  /// \brief Called at the start of a debug object link.
993  void startDebugObject(DWARFContext &, DebugMapObject &);
994
995  /// \brief Called at the end of a debug object link.
996  void endDebugObject();
997
998  /// \defgroup FindValidRelocations Translate debug map into a list
999  /// of relevant relocations
1000  ///
1001  /// @{
1002  struct ValidReloc {
1003    uint32_t Offset;
1004    uint32_t Size;
1005    uint64_t Addend;
1006    const DebugMapObject::DebugMapEntry *Mapping;
1007
1008    ValidReloc(uint32_t Offset, uint32_t Size, uint64_t Addend,
1009               const DebugMapObject::DebugMapEntry *Mapping)
1010        : Offset(Offset), Size(Size), Addend(Addend), Mapping(Mapping) {}
1011
1012    bool operator<(const ValidReloc &RHS) const { return Offset < RHS.Offset; }
1013  };
1014
1015  /// \brief The valid relocations for the current DebugMapObject.
1016  /// This vector is sorted by relocation offset.
1017  std::vector<ValidReloc> ValidRelocs;
1018
1019  /// \brief Index into ValidRelocs of the next relocation to
1020  /// consider. As we walk the DIEs in acsending file offset and as
1021  /// ValidRelocs is sorted by file offset, keeping this index
1022  /// uptodate is all we have to do to have a cheap lookup during the
1023  /// root DIE selection and during DIE cloning.
1024  unsigned NextValidReloc;
1025
1026  bool findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
1027                                  const DebugMapObject &DMO);
1028
1029  bool findValidRelocs(const object::SectionRef &Section,
1030                       const object::ObjectFile &Obj,
1031                       const DebugMapObject &DMO);
1032
1033  void findValidRelocsMachO(const object::SectionRef &Section,
1034                            const object::MachOObjectFile &Obj,
1035                            const DebugMapObject &DMO);
1036  /// @}
1037
1038  /// \defgroup FindRootDIEs Find DIEs corresponding to debug map entries.
1039  ///
1040  /// @{
1041  /// \brief Recursively walk the \p DIE tree and look for DIEs to
1042  /// keep. Store that information in \p CU's DIEInfo.
1043  void lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE,
1044                         const DebugMapObject &DMO, CompileUnit &CU,
1045                         unsigned Flags);
1046
1047  /// \brief Flags passed to DwarfLinker::lookForDIEsToKeep
1048  enum TravesalFlags {
1049    TF_Keep = 1 << 0,            ///< Mark the traversed DIEs as kept.
1050    TF_InFunctionScope = 1 << 1, ///< Current scope is a fucntion scope.
1051    TF_DependencyWalk = 1 << 2,  ///< Walking the dependencies of a kept DIE.
1052    TF_ParentWalk = 1 << 3,      ///< Walking up the parents of a kept DIE.
1053  };
1054
1055  /// \brief Mark the passed DIE as well as all the ones it depends on
1056  /// as kept.
1057  void keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE,
1058                               CompileUnit::DIEInfo &MyInfo,
1059                               const DebugMapObject &DMO, CompileUnit &CU,
1060                               unsigned Flags);
1061
1062  unsigned shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE,
1063                         CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
1064                         unsigned Flags);
1065
1066  unsigned shouldKeepVariableDIE(const DWARFDebugInfoEntryMinimal &DIE,
1067                                 CompileUnit &Unit,
1068                                 CompileUnit::DIEInfo &MyInfo, unsigned Flags);
1069
1070  unsigned shouldKeepSubprogramDIE(const DWARFDebugInfoEntryMinimal &DIE,
1071                                   CompileUnit &Unit,
1072                                   CompileUnit::DIEInfo &MyInfo,
1073                                   unsigned Flags);
1074
1075  bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
1076                          CompileUnit::DIEInfo &Info);
1077  /// @}
1078
1079  /// \defgroup Linking Methods used to link the debug information
1080  ///
1081  /// @{
1082  /// \brief Recursively clone \p InputDIE into an tree of DIE objects
1083  /// where useless (as decided by lookForDIEsToKeep()) bits have been
1084  /// stripped out and addresses have been rewritten according to the
1085  /// debug map.
1086  ///
1087  /// \param OutOffset is the offset the cloned DIE in the output
1088  /// compile unit.
1089  /// \param PCOffset (while cloning a function scope) is the offset
1090  /// applied to the entry point of the function to get the linked address.
1091  ///
1092  /// \returns the root of the cloned tree.
1093  DIE *cloneDIE(const DWARFDebugInfoEntryMinimal &InputDIE, CompileUnit &U,
1094                int64_t PCOffset, uint32_t OutOffset);
1095
1096  typedef DWARFAbbreviationDeclaration::AttributeSpec AttributeSpec;
1097
1098  /// \brief Information gathered and exchanged between the various
1099  /// clone*Attributes helpers about the attributes of a particular DIE.
1100  struct AttributesInfo {
1101    const char *Name, *MangledName;         ///< Names.
1102    uint32_t NameOffset, MangledNameOffset; ///< Offsets in the string pool.
1103
1104    uint64_t OrigHighPc; ///< Value of AT_high_pc in the input DIE
1105    int64_t PCOffset;    ///< Offset to apply to PC addresses inside a function.
1106
1107    bool HasLowPc;      ///< Does the DIE have a low_pc attribute?
1108    bool IsDeclaration; ///< Is this DIE only a declaration?
1109
1110    AttributesInfo()
1111        : Name(nullptr), MangledName(nullptr), NameOffset(0),
1112          MangledNameOffset(0), OrigHighPc(0), PCOffset(0), HasLowPc(false),
1113          IsDeclaration(false) {}
1114  };
1115
1116  /// \brief Helper for cloneDIE.
1117  unsigned cloneAttribute(DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE,
1118                          CompileUnit &U, const DWARFFormValue &Val,
1119                          const AttributeSpec AttrSpec, unsigned AttrSize,
1120                          AttributesInfo &AttrInfo);
1121
1122  /// \brief Helper for cloneDIE.
1123  unsigned cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec,
1124                                const DWARFFormValue &Val, const DWARFUnit &U);
1125
1126  /// \brief Helper for cloneDIE.
1127  unsigned
1128  cloneDieReferenceAttribute(DIE &Die,
1129                             const DWARFDebugInfoEntryMinimal &InputDIE,
1130                             AttributeSpec AttrSpec, unsigned AttrSize,
1131                             const DWARFFormValue &Val, CompileUnit &Unit);
1132
1133  /// \brief Helper for cloneDIE.
1134  unsigned cloneBlockAttribute(DIE &Die, AttributeSpec AttrSpec,
1135                               const DWARFFormValue &Val, unsigned AttrSize);
1136
1137  /// \brief Helper for cloneDIE.
1138  unsigned cloneAddressAttribute(DIE &Die, AttributeSpec AttrSpec,
1139                                 const DWARFFormValue &Val,
1140                                 const CompileUnit &Unit, AttributesInfo &Info);
1141
1142  /// \brief Helper for cloneDIE.
1143  unsigned cloneScalarAttribute(DIE &Die,
1144                                const DWARFDebugInfoEntryMinimal &InputDIE,
1145                                CompileUnit &U, AttributeSpec AttrSpec,
1146                                const DWARFFormValue &Val, unsigned AttrSize,
1147                                AttributesInfo &Info);
1148
1149  /// \brief Helper for cloneDIE.
1150  bool applyValidRelocs(MutableArrayRef<char> Data, uint32_t BaseOffset,
1151                        bool isLittleEndian);
1152
1153  /// \brief Assign an abbreviation number to \p Abbrev
1154  void AssignAbbrev(DIEAbbrev &Abbrev);
1155
1156  /// \brief FoldingSet that uniques the abbreviations.
1157  FoldingSet<DIEAbbrev> AbbreviationsSet;
1158  /// \brief Storage for the unique Abbreviations.
1159  /// This is passed to AsmPrinter::emitDwarfAbbrevs(), thus it cannot
1160  /// be changed to a vecot of unique_ptrs.
1161  std::vector<DIEAbbrev *> Abbreviations;
1162
1163  /// \brief Compute and emit debug_ranges section for \p Unit, and
1164  /// patch the attributes referencing it.
1165  void patchRangesForUnit(const CompileUnit &Unit, DWARFContext &Dwarf) const;
1166
1167  /// \brief Generate and emit the DW_AT_ranges attribute for a
1168  /// compile_unit if it had one.
1169  void generateUnitRanges(CompileUnit &Unit) const;
1170
1171  /// \brief Extract the line tables fromt he original dwarf, extract
1172  /// the relevant parts according to the linked function ranges and
1173  /// emit the result in the debug_line section.
1174  void patchLineTableForUnit(CompileUnit &Unit, DWARFContext &OrigDwarf);
1175
1176  /// \brief Emit the accelerator entries for \p Unit.
1177  void emitAcceleratorEntriesForUnit(CompileUnit &Unit);
1178
1179  /// \brief DIELoc objects that need to be destructed (but not freed!).
1180  std::vector<DIELoc *> DIELocs;
1181  /// \brief DIEBlock objects that need to be destructed (but not freed!).
1182  std::vector<DIEBlock *> DIEBlocks;
1183  /// \brief Allocator used for all the DIEValue objects.
1184  BumpPtrAllocator DIEAlloc;
1185  /// @}
1186
1187  /// \defgroup Helpers Various helper methods.
1188  ///
1189  /// @{
1190  const DWARFDebugInfoEntryMinimal *
1191  resolveDIEReference(DWARFFormValue &RefValue, const DWARFUnit &Unit,
1192                      const DWARFDebugInfoEntryMinimal &DIE,
1193                      CompileUnit *&ReferencedCU);
1194
1195  CompileUnit *getUnitForOffset(unsigned Offset);
1196
1197  bool getDIENames(const DWARFDebugInfoEntryMinimal &Die, DWARFUnit &U,
1198                   AttributesInfo &Info);
1199
1200  void reportWarning(const Twine &Warning, const DWARFUnit *Unit = nullptr,
1201                     const DWARFDebugInfoEntryMinimal *DIE = nullptr) const;
1202
1203  bool createStreamer(Triple TheTriple, StringRef OutputFilename);
1204  /// @}
1205
1206private:
1207  std::string OutputFilename;
1208  LinkOptions Options;
1209  BinaryHolder BinHolder;
1210  std::unique_ptr<DwarfStreamer> Streamer;
1211
1212  /// The units of the current debug map object.
1213  std::vector<CompileUnit> Units;
1214
1215  /// The debug map object curently under consideration.
1216  DebugMapObject *CurrentDebugObject;
1217
1218  /// \brief The Dwarf string pool
1219  NonRelocatableStringpool StringPool;
1220
1221  /// \brief This map is keyed by the entry PC of functions in that
1222  /// debug object and the associated value is a pair storing the
1223  /// corresponding end PC and the offset to apply to get the linked
1224  /// address.
1225  ///
1226  /// See startDebugObject() for a more complete description of its use.
1227  std::map<uint64_t, std::pair<uint64_t, int64_t>> Ranges;
1228};
1229
1230/// \brief Similar to DWARFUnitSection::getUnitForOffset(), but
1231/// returning our CompileUnit object instead.
1232CompileUnit *DwarfLinker::getUnitForOffset(unsigned Offset) {
1233  auto CU =
1234      std::upper_bound(Units.begin(), Units.end(), Offset,
1235                       [](uint32_t LHS, const CompileUnit &RHS) {
1236                         return LHS < RHS.getOrigUnit().getNextUnitOffset();
1237                       });
1238  return CU != Units.end() ? &*CU : nullptr;
1239}
1240
1241/// \brief Resolve the DIE attribute reference that has been
1242/// extracted in \p RefValue. The resulting DIE migh be in another
1243/// CompileUnit which is stored into \p ReferencedCU.
1244/// \returns null if resolving fails for any reason.
1245const DWARFDebugInfoEntryMinimal *DwarfLinker::resolveDIEReference(
1246    DWARFFormValue &RefValue, const DWARFUnit &Unit,
1247    const DWARFDebugInfoEntryMinimal &DIE, CompileUnit *&RefCU) {
1248  assert(RefValue.isFormClass(DWARFFormValue::FC_Reference));
1249  uint64_t RefOffset = *RefValue.getAsReference(&Unit);
1250
1251  if ((RefCU = getUnitForOffset(RefOffset)))
1252    if (const auto *RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset))
1253      return RefDie;
1254
1255  reportWarning("could not find referenced DIE", &Unit, &DIE);
1256  return nullptr;
1257}
1258
1259/// \brief Get the potential name and mangled name for the entity
1260/// described by \p Die and store them in \Info if they are not
1261/// already there.
1262/// \returns is a name was found.
1263bool DwarfLinker::getDIENames(const DWARFDebugInfoEntryMinimal &Die,
1264                              DWARFUnit &U, AttributesInfo &Info) {
1265  // FIXME: a bit wastefull as the first getName might return the
1266  // short name.
1267  if (!Info.MangledName &&
1268      (Info.MangledName = Die.getName(&U, DINameKind::LinkageName)))
1269    Info.MangledNameOffset = StringPool.getStringOffset(Info.MangledName);
1270
1271  if (!Info.Name && (Info.Name = Die.getName(&U, DINameKind::ShortName)))
1272    Info.NameOffset = StringPool.getStringOffset(Info.Name);
1273
1274  return Info.Name || Info.MangledName;
1275}
1276
1277/// \brief Report a warning to the user, optionaly including
1278/// information about a specific \p DIE related to the warning.
1279void DwarfLinker::reportWarning(const Twine &Warning, const DWARFUnit *Unit,
1280                                const DWARFDebugInfoEntryMinimal *DIE) const {
1281  StringRef Context = "<debug map>";
1282  if (CurrentDebugObject)
1283    Context = CurrentDebugObject->getObjectFilename();
1284  warn(Warning, Context);
1285
1286  if (!Options.Verbose || !DIE)
1287    return;
1288
1289  errs() << "    in DIE:\n";
1290  DIE->dump(errs(), const_cast<DWARFUnit *>(Unit), 0 /* RecurseDepth */,
1291            6 /* Indent */);
1292}
1293
1294bool DwarfLinker::createStreamer(Triple TheTriple, StringRef OutputFilename) {
1295  if (Options.NoOutput)
1296    return true;
1297
1298  Streamer = llvm::make_unique<DwarfStreamer>();
1299  return Streamer->init(TheTriple, OutputFilename);
1300}
1301
1302/// \brief Recursive helper to gather the child->parent relationships in the
1303/// original compile unit.
1304static void gatherDIEParents(const DWARFDebugInfoEntryMinimal *DIE,
1305                             unsigned ParentIdx, CompileUnit &CU) {
1306  unsigned MyIdx = CU.getOrigUnit().getDIEIndex(DIE);
1307  CU.getInfo(MyIdx).ParentIdx = ParentIdx;
1308
1309  if (DIE->hasChildren())
1310    for (auto *Child = DIE->getFirstChild(); Child && !Child->isNULL();
1311         Child = Child->getSibling())
1312      gatherDIEParents(Child, MyIdx, CU);
1313}
1314
1315static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) {
1316  switch (Tag) {
1317  default:
1318    return false;
1319  case dwarf::DW_TAG_subprogram:
1320  case dwarf::DW_TAG_lexical_block:
1321  case dwarf::DW_TAG_subroutine_type:
1322  case dwarf::DW_TAG_structure_type:
1323  case dwarf::DW_TAG_class_type:
1324  case dwarf::DW_TAG_union_type:
1325    return true;
1326  }
1327  llvm_unreachable("Invalid Tag");
1328}
1329
1330void DwarfLinker::startDebugObject(DWARFContext &Dwarf, DebugMapObject &Obj) {
1331  Units.reserve(Dwarf.getNumCompileUnits());
1332  NextValidReloc = 0;
1333  // Iterate over the debug map entries and put all the ones that are
1334  // functions (because they have a size) into the Ranges map. This
1335  // map is very similar to the FunctionRanges that are stored in each
1336  // unit, with 2 notable differences:
1337  //  - obviously this one is global, while the other ones are per-unit.
1338  //  - this one contains not only the functions described in the DIE
1339  // tree, but also the ones that are only in the debug map.
1340  // The latter information is required to reproduce dsymutil's logic
1341  // while linking line tables. The cases where this information
1342  // matters look like bugs that need to be investigated, but for now
1343  // we need to reproduce dsymutil's behavior.
1344  // FIXME: Once we understood exactly if that information is needed,
1345  // maybe totally remove this (or try to use it to do a real
1346  // -gline-tables-only on Darwin.
1347  for (const auto &Entry : Obj.symbols()) {
1348    const auto &Mapping = Entry.getValue();
1349    if (Mapping.Size)
1350      Ranges[Mapping.ObjectAddress] = std::make_pair(
1351          Mapping.ObjectAddress + Mapping.Size,
1352          int64_t(Mapping.BinaryAddress) - Mapping.ObjectAddress);
1353  }
1354}
1355
1356void DwarfLinker::endDebugObject() {
1357  Units.clear();
1358  ValidRelocs.clear();
1359  Ranges.clear();
1360
1361  for (auto *Block : DIEBlocks)
1362    Block->~DIEBlock();
1363  for (auto *Loc : DIELocs)
1364    Loc->~DIELoc();
1365
1366  DIEBlocks.clear();
1367  DIELocs.clear();
1368  DIEAlloc.Reset();
1369}
1370
1371/// \brief Iterate over the relocations of the given \p Section and
1372/// store the ones that correspond to debug map entries into the
1373/// ValidRelocs array.
1374void DwarfLinker::findValidRelocsMachO(const object::SectionRef &Section,
1375                                       const object::MachOObjectFile &Obj,
1376                                       const DebugMapObject &DMO) {
1377  StringRef Contents;
1378  Section.getContents(Contents);
1379  DataExtractor Data(Contents, Obj.isLittleEndian(), 0);
1380
1381  for (const object::RelocationRef &Reloc : Section.relocations()) {
1382    object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl();
1383    MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef);
1384    unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc);
1385    uint64_t Offset64;
1386    if ((RelocSize != 4 && RelocSize != 8) || Reloc.getOffset(Offset64)) {
1387      reportWarning(" unsupported relocation in debug_info section.");
1388      continue;
1389    }
1390    uint32_t Offset = Offset64;
1391    // Mach-o uses REL relocations, the addend is at the relocation offset.
1392    uint64_t Addend = Data.getUnsigned(&Offset, RelocSize);
1393
1394    auto Sym = Reloc.getSymbol();
1395    if (Sym != Obj.symbol_end()) {
1396      StringRef SymbolName;
1397      if (Sym->getName(SymbolName)) {
1398        reportWarning("error getting relocation symbol name.");
1399        continue;
1400      }
1401      if (const auto *Mapping = DMO.lookupSymbol(SymbolName))
1402        ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping);
1403    } else if (const auto *Mapping = DMO.lookupObjectAddress(Addend)) {
1404      // Do not store the addend. The addend was the address of the
1405      // symbol in the object file, the address in the binary that is
1406      // stored in the debug map doesn't need to be offseted.
1407      ValidRelocs.emplace_back(Offset64, RelocSize, 0, Mapping);
1408    }
1409  }
1410}
1411
1412/// \brief Dispatch the valid relocation finding logic to the
1413/// appropriate handler depending on the object file format.
1414bool DwarfLinker::findValidRelocs(const object::SectionRef &Section,
1415                                  const object::ObjectFile &Obj,
1416                                  const DebugMapObject &DMO) {
1417  // Dispatch to the right handler depending on the file type.
1418  if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj))
1419    findValidRelocsMachO(Section, *MachOObj, DMO);
1420  else
1421    reportWarning(Twine("unsupported object file type: ") + Obj.getFileName());
1422
1423  if (ValidRelocs.empty())
1424    return false;
1425
1426  // Sort the relocations by offset. We will walk the DIEs linearly in
1427  // the file, this allows us to just keep an index in the relocation
1428  // array that we advance during our walk, rather than resorting to
1429  // some associative container. See DwarfLinker::NextValidReloc.
1430  std::sort(ValidRelocs.begin(), ValidRelocs.end());
1431  return true;
1432}
1433
1434/// \brief Look for relocations in the debug_info section that match
1435/// entries in the debug map. These relocations will drive the Dwarf
1436/// link by indicating which DIEs refer to symbols present in the
1437/// linked binary.
1438/// \returns wether there are any valid relocations in the debug info.
1439bool DwarfLinker::findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
1440                                             const DebugMapObject &DMO) {
1441  // Find the debug_info section.
1442  for (const object::SectionRef &Section : Obj.sections()) {
1443    StringRef SectionName;
1444    Section.getName(SectionName);
1445    SectionName = SectionName.substr(SectionName.find_first_not_of("._"));
1446    if (SectionName != "debug_info")
1447      continue;
1448    return findValidRelocs(Section, Obj, DMO);
1449  }
1450  return false;
1451}
1452
1453/// \brief Checks that there is a relocation against an actual debug
1454/// map entry between \p StartOffset and \p NextOffset.
1455///
1456/// This function must be called with offsets in strictly ascending
1457/// order because it never looks back at relocations it already 'went past'.
1458/// \returns true and sets Info.InDebugMap if it is the case.
1459bool DwarfLinker::hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
1460                                     CompileUnit::DIEInfo &Info) {
1461  assert(NextValidReloc == 0 ||
1462         StartOffset > ValidRelocs[NextValidReloc - 1].Offset);
1463  if (NextValidReloc >= ValidRelocs.size())
1464    return false;
1465
1466  uint64_t RelocOffset = ValidRelocs[NextValidReloc].Offset;
1467
1468  // We might need to skip some relocs that we didn't consider. For
1469  // example the high_pc of a discarded DIE might contain a reloc that
1470  // is in the list because it actually corresponds to the start of a
1471  // function that is in the debug map.
1472  while (RelocOffset < StartOffset && NextValidReloc < ValidRelocs.size() - 1)
1473    RelocOffset = ValidRelocs[++NextValidReloc].Offset;
1474
1475  if (RelocOffset < StartOffset || RelocOffset >= EndOffset)
1476    return false;
1477
1478  const auto &ValidReloc = ValidRelocs[NextValidReloc++];
1479  if (Options.Verbose)
1480    outs() << "Found valid debug map entry: " << ValidReloc.Mapping->getKey()
1481           << " " << format("\t%016" PRIx64 " => %016" PRIx64,
1482                            ValidReloc.Mapping->getValue().ObjectAddress,
1483                            ValidReloc.Mapping->getValue().BinaryAddress);
1484
1485  Info.AddrAdjust = int64_t(ValidReloc.Mapping->getValue().BinaryAddress) +
1486                    ValidReloc.Addend -
1487                    ValidReloc.Mapping->getValue().ObjectAddress;
1488  Info.InDebugMap = true;
1489  return true;
1490}
1491
1492/// \brief Get the starting and ending (exclusive) offset for the
1493/// attribute with index \p Idx descibed by \p Abbrev. \p Offset is
1494/// supposed to point to the position of the first attribute described
1495/// by \p Abbrev.
1496/// \return [StartOffset, EndOffset) as a pair.
1497static std::pair<uint32_t, uint32_t>
1498getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx,
1499                    unsigned Offset, const DWARFUnit &Unit) {
1500  DataExtractor Data = Unit.getDebugInfoExtractor();
1501
1502  for (unsigned i = 0; i < Idx; ++i)
1503    DWARFFormValue::skipValue(Abbrev->getFormByIndex(i), Data, &Offset, &Unit);
1504
1505  uint32_t End = Offset;
1506  DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End, &Unit);
1507
1508  return std::make_pair(Offset, End);
1509}
1510
1511/// \brief Check if a variable describing DIE should be kept.
1512/// \returns updated TraversalFlags.
1513unsigned DwarfLinker::shouldKeepVariableDIE(
1514    const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit,
1515    CompileUnit::DIEInfo &MyInfo, unsigned Flags) {
1516  const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
1517
1518  // Global variables with constant value can always be kept.
1519  if (!(Flags & TF_InFunctionScope) &&
1520      Abbrev->findAttributeIndex(dwarf::DW_AT_const_value) != -1U) {
1521    MyInfo.InDebugMap = true;
1522    return Flags | TF_Keep;
1523  }
1524
1525  uint32_t LocationIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_location);
1526  if (LocationIdx == -1U)
1527    return Flags;
1528
1529  uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
1530  const DWARFUnit &OrigUnit = Unit.getOrigUnit();
1531  uint32_t LocationOffset, LocationEndOffset;
1532  std::tie(LocationOffset, LocationEndOffset) =
1533      getAttributeOffsets(Abbrev, LocationIdx, Offset, OrigUnit);
1534
1535  // See if there is a relocation to a valid debug map entry inside
1536  // this variable's location. The order is important here. We want to
1537  // always check in the variable has a valid relocation, so that the
1538  // DIEInfo is filled. However, we don't want a static variable in a
1539  // function to force us to keep the enclosing function.
1540  if (!hasValidRelocation(LocationOffset, LocationEndOffset, MyInfo) ||
1541      (Flags & TF_InFunctionScope))
1542    return Flags;
1543
1544  if (Options.Verbose)
1545    DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */);
1546
1547  return Flags | TF_Keep;
1548}
1549
1550/// \brief Check if a function describing DIE should be kept.
1551/// \returns updated TraversalFlags.
1552unsigned DwarfLinker::shouldKeepSubprogramDIE(
1553    const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit,
1554    CompileUnit::DIEInfo &MyInfo, unsigned Flags) {
1555  const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
1556
1557  Flags |= TF_InFunctionScope;
1558
1559  uint32_t LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc);
1560  if (LowPcIdx == -1U)
1561    return Flags;
1562
1563  uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
1564  const DWARFUnit &OrigUnit = Unit.getOrigUnit();
1565  uint32_t LowPcOffset, LowPcEndOffset;
1566  std::tie(LowPcOffset, LowPcEndOffset) =
1567      getAttributeOffsets(Abbrev, LowPcIdx, Offset, OrigUnit);
1568
1569  uint64_t LowPc =
1570      DIE.getAttributeValueAsAddress(&OrigUnit, dwarf::DW_AT_low_pc, -1ULL);
1571  assert(LowPc != -1ULL && "low_pc attribute is not an address.");
1572  if (LowPc == -1ULL ||
1573      !hasValidRelocation(LowPcOffset, LowPcEndOffset, MyInfo))
1574    return Flags;
1575
1576  if (Options.Verbose)
1577    DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */);
1578
1579  Flags |= TF_Keep;
1580
1581  DWARFFormValue HighPcValue;
1582  if (!DIE.getAttributeValue(&OrigUnit, dwarf::DW_AT_high_pc, HighPcValue)) {
1583    reportWarning("Function without high_pc. Range will be discarded.\n",
1584                  &OrigUnit, &DIE);
1585    return Flags;
1586  }
1587
1588  uint64_t HighPc;
1589  if (HighPcValue.isFormClass(DWARFFormValue::FC_Address)) {
1590    HighPc = *HighPcValue.getAsAddress(&OrigUnit);
1591  } else {
1592    assert(HighPcValue.isFormClass(DWARFFormValue::FC_Constant));
1593    HighPc = LowPc + *HighPcValue.getAsUnsignedConstant();
1594  }
1595
1596  // Replace the debug map range with a more accurate one.
1597  Ranges[LowPc] = std::make_pair(HighPc, MyInfo.AddrAdjust);
1598  Unit.addFunctionRange(LowPc, HighPc, MyInfo.AddrAdjust);
1599  return Flags;
1600}
1601
1602/// \brief Check if a DIE should be kept.
1603/// \returns updated TraversalFlags.
1604unsigned DwarfLinker::shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE,
1605                                    CompileUnit &Unit,
1606                                    CompileUnit::DIEInfo &MyInfo,
1607                                    unsigned Flags) {
1608  switch (DIE.getTag()) {
1609  case dwarf::DW_TAG_constant:
1610  case dwarf::DW_TAG_variable:
1611    return shouldKeepVariableDIE(DIE, Unit, MyInfo, Flags);
1612  case dwarf::DW_TAG_subprogram:
1613    return shouldKeepSubprogramDIE(DIE, Unit, MyInfo, Flags);
1614  case dwarf::DW_TAG_module:
1615  case dwarf::DW_TAG_imported_module:
1616  case dwarf::DW_TAG_imported_declaration:
1617  case dwarf::DW_TAG_imported_unit:
1618    // We always want to keep these.
1619    return Flags | TF_Keep;
1620  }
1621
1622  return Flags;
1623}
1624
1625/// \brief Mark the passed DIE as well as all the ones it depends on
1626/// as kept.
1627///
1628/// This function is called by lookForDIEsToKeep on DIEs that are
1629/// newly discovered to be needed in the link. It recursively calls
1630/// back to lookForDIEsToKeep while adding TF_DependencyWalk to the
1631/// TraversalFlags to inform it that it's not doing the primary DIE
1632/// tree walk.
1633void DwarfLinker::keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE,
1634                                          CompileUnit::DIEInfo &MyInfo,
1635                                          const DebugMapObject &DMO,
1636                                          CompileUnit &CU, unsigned Flags) {
1637  const DWARFUnit &Unit = CU.getOrigUnit();
1638  MyInfo.Keep = true;
1639
1640  // First mark all the parent chain as kept.
1641  unsigned AncestorIdx = MyInfo.ParentIdx;
1642  while (!CU.getInfo(AncestorIdx).Keep) {
1643    lookForDIEsToKeep(*Unit.getDIEAtIndex(AncestorIdx), DMO, CU,
1644                      TF_ParentWalk | TF_Keep | TF_DependencyWalk);
1645    AncestorIdx = CU.getInfo(AncestorIdx).ParentIdx;
1646  }
1647
1648  // Then we need to mark all the DIEs referenced by this DIE's
1649  // attributes as kept.
1650  DataExtractor Data = Unit.getDebugInfoExtractor();
1651  const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
1652  uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
1653
1654  // Mark all DIEs referenced through atttributes as kept.
1655  for (const auto &AttrSpec : Abbrev->attributes()) {
1656    DWARFFormValue Val(AttrSpec.Form);
1657
1658    if (!Val.isFormClass(DWARFFormValue::FC_Reference)) {
1659      DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset, &Unit);
1660      continue;
1661    }
1662
1663    Val.extractValue(Data, &Offset, &Unit);
1664    CompileUnit *ReferencedCU;
1665    if (const auto *RefDIE = resolveDIEReference(Val, Unit, DIE, ReferencedCU))
1666      lookForDIEsToKeep(*RefDIE, DMO, *ReferencedCU,
1667                        TF_Keep | TF_DependencyWalk);
1668  }
1669}
1670
1671/// \brief Recursively walk the \p DIE tree and look for DIEs to
1672/// keep. Store that information in \p CU's DIEInfo.
1673///
1674/// This function is the entry point of the DIE selection
1675/// algorithm. It is expected to walk the DIE tree in file order and
1676/// (though the mediation of its helper) call hasValidRelocation() on
1677/// each DIE that might be a 'root DIE' (See DwarfLinker class
1678/// comment).
1679/// While walking the dependencies of root DIEs, this function is
1680/// also called, but during these dependency walks the file order is
1681/// not respected. The TF_DependencyWalk flag tells us which kind of
1682/// traversal we are currently doing.
1683void DwarfLinker::lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE,
1684                                    const DebugMapObject &DMO, CompileUnit &CU,
1685                                    unsigned Flags) {
1686  unsigned Idx = CU.getOrigUnit().getDIEIndex(&DIE);
1687  CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
1688  bool AlreadyKept = MyInfo.Keep;
1689
1690  // If the Keep flag is set, we are marking a required DIE's
1691  // dependencies. If our target is already marked as kept, we're all
1692  // set.
1693  if ((Flags & TF_DependencyWalk) && AlreadyKept)
1694    return;
1695
1696  // We must not call shouldKeepDIE while called from keepDIEAndDenpendencies,
1697  // because it would screw up the relocation finding logic.
1698  if (!(Flags & TF_DependencyWalk))
1699    Flags = shouldKeepDIE(DIE, CU, MyInfo, Flags);
1700
1701  // If it is a newly kept DIE mark it as well as all its dependencies as kept.
1702  if (!AlreadyKept && (Flags & TF_Keep))
1703    keepDIEAndDenpendencies(DIE, MyInfo, DMO, CU, Flags);
1704
1705  // The TF_ParentWalk flag tells us that we are currently walking up
1706  // the parent chain of a required DIE, and we don't want to mark all
1707  // the children of the parents as kept (consider for example a
1708  // DW_TAG_namespace node in the parent chain). There are however a
1709  // set of DIE types for which we want to ignore that directive and still
1710  // walk their children.
1711  if (dieNeedsChildrenToBeMeaningful(DIE.getTag()))
1712    Flags &= ~TF_ParentWalk;
1713
1714  if (!DIE.hasChildren() || (Flags & TF_ParentWalk))
1715    return;
1716
1717  for (auto *Child = DIE.getFirstChild(); Child && !Child->isNULL();
1718       Child = Child->getSibling())
1719    lookForDIEsToKeep(*Child, DMO, CU, Flags);
1720}
1721
1722/// \brief Assign an abbreviation numer to \p Abbrev.
1723///
1724/// Our DIEs get freed after every DebugMapObject has been processed,
1725/// thus the FoldingSet we use to unique DIEAbbrevs cannot refer to
1726/// the instances hold by the DIEs. When we encounter an abbreviation
1727/// that we don't know, we create a permanent copy of it.
1728void DwarfLinker::AssignAbbrev(DIEAbbrev &Abbrev) {
1729  // Check the set for priors.
1730  FoldingSetNodeID ID;
1731  Abbrev.Profile(ID);
1732  void *InsertToken;
1733  DIEAbbrev *InSet = AbbreviationsSet.FindNodeOrInsertPos(ID, InsertToken);
1734
1735  // If it's newly added.
1736  if (InSet) {
1737    // Assign existing abbreviation number.
1738    Abbrev.setNumber(InSet->getNumber());
1739  } else {
1740    // Add to abbreviation list.
1741    Abbreviations.push_back(
1742        new DIEAbbrev(Abbrev.getTag(), Abbrev.hasChildren()));
1743    for (const auto &Attr : Abbrev.getData())
1744      Abbreviations.back()->AddAttribute(Attr.getAttribute(), Attr.getForm());
1745    AbbreviationsSet.InsertNode(Abbreviations.back(), InsertToken);
1746    // Assign the unique abbreviation number.
1747    Abbrev.setNumber(Abbreviations.size());
1748    Abbreviations.back()->setNumber(Abbreviations.size());
1749  }
1750}
1751
1752/// \brief Clone a string attribute described by \p AttrSpec and add
1753/// it to \p Die.
1754/// \returns the size of the new attribute.
1755unsigned DwarfLinker::cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec,
1756                                           const DWARFFormValue &Val,
1757                                           const DWARFUnit &U) {
1758  // Switch everything to out of line strings.
1759  const char *String = *Val.getAsCString(&U);
1760  unsigned Offset = StringPool.getStringOffset(String);
1761  Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::DW_FORM_strp,
1762               new (DIEAlloc) DIEInteger(Offset));
1763  return 4;
1764}
1765
1766/// \brief Clone an attribute referencing another DIE and add
1767/// it to \p Die.
1768/// \returns the size of the new attribute.
1769unsigned DwarfLinker::cloneDieReferenceAttribute(
1770    DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE,
1771    AttributeSpec AttrSpec, unsigned AttrSize, const DWARFFormValue &Val,
1772    CompileUnit &Unit) {
1773  uint32_t Ref = *Val.getAsReference(&Unit.getOrigUnit());
1774  DIE *NewRefDie = nullptr;
1775  CompileUnit *RefUnit = nullptr;
1776  const DWARFDebugInfoEntryMinimal *RefDie = nullptr;
1777
1778  if (!(RefUnit = getUnitForOffset(Ref)) ||
1779      !(RefDie = RefUnit->getOrigUnit().getDIEForOffset(Ref))) {
1780    const char *AttributeString = dwarf::AttributeString(AttrSpec.Attr);
1781    if (!AttributeString)
1782      AttributeString = "DW_AT_???";
1783    reportWarning(Twine("Missing DIE for ref in attribute ") + AttributeString +
1784                      ". Dropping.",
1785                  &Unit.getOrigUnit(), &InputDIE);
1786    return 0;
1787  }
1788
1789  unsigned Idx = RefUnit->getOrigUnit().getDIEIndex(RefDie);
1790  CompileUnit::DIEInfo &RefInfo = RefUnit->getInfo(Idx);
1791  if (!RefInfo.Clone) {
1792    assert(Ref > InputDIE.getOffset());
1793    // We haven't cloned this DIE yet. Just create an empty one and
1794    // store it. It'll get really cloned when we process it.
1795    RefInfo.Clone = new DIE(dwarf::Tag(RefDie->getTag()));
1796  }
1797  NewRefDie = RefInfo.Clone;
1798
1799  if (AttrSpec.Form == dwarf::DW_FORM_ref_addr) {
1800    // We cannot currently rely on a DIEEntry to emit ref_addr
1801    // references, because the implementation calls back to DwarfDebug
1802    // to find the unit offset. (We don't have a DwarfDebug)
1803    // FIXME: we should be able to design DIEEntry reliance on
1804    // DwarfDebug away.
1805    DIEInteger *Attr;
1806    if (Ref < InputDIE.getOffset()) {
1807      // We must have already cloned that DIE.
1808      uint32_t NewRefOffset =
1809          RefUnit->getStartOffset() + NewRefDie->getOffset();
1810      Attr = new (DIEAlloc) DIEInteger(NewRefOffset);
1811    } else {
1812      // A forward reference. Note and fixup later.
1813      Attr = new (DIEAlloc) DIEInteger(0xBADDEF);
1814      Unit.noteForwardReference(NewRefDie, RefUnit, Attr);
1815    }
1816    Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::DW_FORM_ref_addr,
1817                 Attr);
1818    return AttrSize;
1819  }
1820
1821  Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::Form(AttrSpec.Form),
1822               new (DIEAlloc) DIEEntry(*NewRefDie));
1823  return AttrSize;
1824}
1825
1826/// \brief Clone an attribute of block form (locations, constants) and add
1827/// it to \p Die.
1828/// \returns the size of the new attribute.
1829unsigned DwarfLinker::cloneBlockAttribute(DIE &Die, AttributeSpec AttrSpec,
1830                                          const DWARFFormValue &Val,
1831                                          unsigned AttrSize) {
1832  DIE *Attr;
1833  DIEValue *Value;
1834  DIELoc *Loc = nullptr;
1835  DIEBlock *Block = nullptr;
1836  // Just copy the block data over.
1837  if (AttrSpec.Form == dwarf::DW_FORM_exprloc) {
1838    Loc = new (DIEAlloc) DIELoc();
1839    DIELocs.push_back(Loc);
1840  } else {
1841    Block = new (DIEAlloc) DIEBlock();
1842    DIEBlocks.push_back(Block);
1843  }
1844  Attr = Loc ? static_cast<DIE *>(Loc) : static_cast<DIE *>(Block);
1845  Value = Loc ? static_cast<DIEValue *>(Loc) : static_cast<DIEValue *>(Block);
1846  ArrayRef<uint8_t> Bytes = *Val.getAsBlock();
1847  for (auto Byte : Bytes)
1848    Attr->addValue(static_cast<dwarf::Attribute>(0), dwarf::DW_FORM_data1,
1849                   new (DIEAlloc) DIEInteger(Byte));
1850  // FIXME: If DIEBlock and DIELoc just reuses the Size field of
1851  // the DIE class, this if could be replaced by
1852  // Attr->setSize(Bytes.size()).
1853  if (Streamer) {
1854    if (Loc)
1855      Loc->ComputeSize(&Streamer->getAsmPrinter());
1856    else
1857      Block->ComputeSize(&Streamer->getAsmPrinter());
1858  }
1859  Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::Form(AttrSpec.Form),
1860               Value);
1861  return AttrSize;
1862}
1863
1864/// \brief Clone an address attribute and add it to \p Die.
1865/// \returns the size of the new attribute.
1866unsigned DwarfLinker::cloneAddressAttribute(DIE &Die, AttributeSpec AttrSpec,
1867                                            const DWARFFormValue &Val,
1868                                            const CompileUnit &Unit,
1869                                            AttributesInfo &Info) {
1870  uint64_t Addr = *Val.getAsAddress(&Unit.getOrigUnit());
1871  if (AttrSpec.Attr == dwarf::DW_AT_low_pc) {
1872    if (Die.getTag() == dwarf::DW_TAG_inlined_subroutine ||
1873        Die.getTag() == dwarf::DW_TAG_lexical_block)
1874      Addr += Info.PCOffset;
1875    else if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
1876      Addr = Unit.getLowPc();
1877      if (Addr == UINT64_MAX)
1878        return 0;
1879    }
1880    Info.HasLowPc = true;
1881  } else if (AttrSpec.Attr == dwarf::DW_AT_high_pc) {
1882    if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
1883      if (uint64_t HighPc = Unit.getHighPc())
1884        Addr = HighPc;
1885      else
1886        return 0;
1887    } else
1888      // If we have a high_pc recorded for the input DIE, use
1889      // it. Otherwise (when no relocations where applied) just use the
1890      // one we just decoded.
1891      Addr = (Info.OrigHighPc ? Info.OrigHighPc : Addr) + Info.PCOffset;
1892  }
1893
1894  Die.addValue(static_cast<dwarf::Attribute>(AttrSpec.Attr),
1895               static_cast<dwarf::Form>(AttrSpec.Form),
1896               new (DIEAlloc) DIEInteger(Addr));
1897  return Unit.getOrigUnit().getAddressByteSize();
1898}
1899
1900/// \brief Clone a scalar attribute  and add it to \p Die.
1901/// \returns the size of the new attribute.
1902unsigned DwarfLinker::cloneScalarAttribute(
1903    DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE, CompileUnit &Unit,
1904    AttributeSpec AttrSpec, const DWARFFormValue &Val, unsigned AttrSize,
1905    AttributesInfo &Info) {
1906  uint64_t Value;
1907  if (AttrSpec.Attr == dwarf::DW_AT_high_pc &&
1908      Die.getTag() == dwarf::DW_TAG_compile_unit) {
1909    if (Unit.getLowPc() == -1ULL)
1910      return 0;
1911    // Dwarf >= 4 high_pc is an size, not an address.
1912    Value = Unit.getHighPc() - Unit.getLowPc();
1913  } else if (AttrSpec.Form == dwarf::DW_FORM_sec_offset)
1914    Value = *Val.getAsSectionOffset();
1915  else if (AttrSpec.Form == dwarf::DW_FORM_sdata)
1916    Value = *Val.getAsSignedConstant();
1917  else if (auto OptionalValue = Val.getAsUnsignedConstant())
1918    Value = *OptionalValue;
1919  else {
1920    reportWarning("Unsupported scalar attribute form. Dropping attribute.",
1921                  &Unit.getOrigUnit(), &InputDIE);
1922    return 0;
1923  }
1924  DIEInteger *Attr = new (DIEAlloc) DIEInteger(Value);
1925  if (AttrSpec.Attr == dwarf::DW_AT_ranges)
1926    Unit.noteRangeAttribute(Die, Attr);
1927  // A more generic way to check for location attributes would be
1928  // nice, but it's very unlikely that any other attribute needs a
1929  // location list.
1930  else if (AttrSpec.Attr == dwarf::DW_AT_location ||
1931           AttrSpec.Attr == dwarf::DW_AT_frame_base)
1932    Unit.noteLocationAttribute(Attr, Info.PCOffset);
1933  else if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
1934    Info.IsDeclaration = true;
1935
1936  Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::Form(AttrSpec.Form),
1937               Attr);
1938  return AttrSize;
1939}
1940
1941/// \brief Clone \p InputDIE's attribute described by \p AttrSpec with
1942/// value \p Val, and add it to \p Die.
1943/// \returns the size of the cloned attribute.
1944unsigned DwarfLinker::cloneAttribute(DIE &Die,
1945                                     const DWARFDebugInfoEntryMinimal &InputDIE,
1946                                     CompileUnit &Unit,
1947                                     const DWARFFormValue &Val,
1948                                     const AttributeSpec AttrSpec,
1949                                     unsigned AttrSize, AttributesInfo &Info) {
1950  const DWARFUnit &U = Unit.getOrigUnit();
1951
1952  switch (AttrSpec.Form) {
1953  case dwarf::DW_FORM_strp:
1954  case dwarf::DW_FORM_string:
1955    return cloneStringAttribute(Die, AttrSpec, Val, U);
1956  case dwarf::DW_FORM_ref_addr:
1957  case dwarf::DW_FORM_ref1:
1958  case dwarf::DW_FORM_ref2:
1959  case dwarf::DW_FORM_ref4:
1960  case dwarf::DW_FORM_ref8:
1961    return cloneDieReferenceAttribute(Die, InputDIE, AttrSpec, AttrSize, Val,
1962                                      Unit);
1963  case dwarf::DW_FORM_block:
1964  case dwarf::DW_FORM_block1:
1965  case dwarf::DW_FORM_block2:
1966  case dwarf::DW_FORM_block4:
1967  case dwarf::DW_FORM_exprloc:
1968    return cloneBlockAttribute(Die, AttrSpec, Val, AttrSize);
1969  case dwarf::DW_FORM_addr:
1970    return cloneAddressAttribute(Die, AttrSpec, Val, Unit, Info);
1971  case dwarf::DW_FORM_data1:
1972  case dwarf::DW_FORM_data2:
1973  case dwarf::DW_FORM_data4:
1974  case dwarf::DW_FORM_data8:
1975  case dwarf::DW_FORM_udata:
1976  case dwarf::DW_FORM_sdata:
1977  case dwarf::DW_FORM_sec_offset:
1978  case dwarf::DW_FORM_flag:
1979  case dwarf::DW_FORM_flag_present:
1980    return cloneScalarAttribute(Die, InputDIE, Unit, AttrSpec, Val, AttrSize,
1981                                Info);
1982  default:
1983    reportWarning("Unsupported attribute form in cloneAttribute. Dropping.", &U,
1984                  &InputDIE);
1985  }
1986
1987  return 0;
1988}
1989
1990/// \brief Apply the valid relocations found by findValidRelocs() to
1991/// the buffer \p Data, taking into account that Data is at \p BaseOffset
1992/// in the debug_info section.
1993///
1994/// Like for findValidRelocs(), this function must be called with
1995/// monotonic \p BaseOffset values.
1996///
1997/// \returns wether any reloc has been applied.
1998bool DwarfLinker::applyValidRelocs(MutableArrayRef<char> Data,
1999                                   uint32_t BaseOffset, bool isLittleEndian) {
2000  assert((NextValidReloc == 0 ||
2001          BaseOffset > ValidRelocs[NextValidReloc - 1].Offset) &&
2002         "BaseOffset should only be increasing.");
2003  if (NextValidReloc >= ValidRelocs.size())
2004    return false;
2005
2006  // Skip relocs that haven't been applied.
2007  while (NextValidReloc < ValidRelocs.size() &&
2008         ValidRelocs[NextValidReloc].Offset < BaseOffset)
2009    ++NextValidReloc;
2010
2011  bool Applied = false;
2012  uint64_t EndOffset = BaseOffset + Data.size();
2013  while (NextValidReloc < ValidRelocs.size() &&
2014         ValidRelocs[NextValidReloc].Offset >= BaseOffset &&
2015         ValidRelocs[NextValidReloc].Offset < EndOffset) {
2016    const auto &ValidReloc = ValidRelocs[NextValidReloc++];
2017    assert(ValidReloc.Offset - BaseOffset < Data.size());
2018    assert(ValidReloc.Offset - BaseOffset + ValidReloc.Size <= Data.size());
2019    char Buf[8];
2020    uint64_t Value = ValidReloc.Mapping->getValue().BinaryAddress;
2021    Value += ValidReloc.Addend;
2022    for (unsigned i = 0; i != ValidReloc.Size; ++i) {
2023      unsigned Index = isLittleEndian ? i : (ValidReloc.Size - i - 1);
2024      Buf[i] = uint8_t(Value >> (Index * 8));
2025    }
2026    assert(ValidReloc.Size <= sizeof(Buf));
2027    memcpy(&Data[ValidReloc.Offset - BaseOffset], Buf, ValidReloc.Size);
2028    Applied = true;
2029  }
2030
2031  return Applied;
2032}
2033
2034static bool isTypeTag(uint16_t Tag) {
2035  switch (Tag) {
2036  case dwarf::DW_TAG_array_type:
2037  case dwarf::DW_TAG_class_type:
2038  case dwarf::DW_TAG_enumeration_type:
2039  case dwarf::DW_TAG_pointer_type:
2040  case dwarf::DW_TAG_reference_type:
2041  case dwarf::DW_TAG_string_type:
2042  case dwarf::DW_TAG_structure_type:
2043  case dwarf::DW_TAG_subroutine_type:
2044  case dwarf::DW_TAG_typedef:
2045  case dwarf::DW_TAG_union_type:
2046  case dwarf::DW_TAG_ptr_to_member_type:
2047  case dwarf::DW_TAG_set_type:
2048  case dwarf::DW_TAG_subrange_type:
2049  case dwarf::DW_TAG_base_type:
2050  case dwarf::DW_TAG_const_type:
2051  case dwarf::DW_TAG_constant:
2052  case dwarf::DW_TAG_file_type:
2053  case dwarf::DW_TAG_namelist:
2054  case dwarf::DW_TAG_packed_type:
2055  case dwarf::DW_TAG_volatile_type:
2056  case dwarf::DW_TAG_restrict_type:
2057  case dwarf::DW_TAG_interface_type:
2058  case dwarf::DW_TAG_unspecified_type:
2059  case dwarf::DW_TAG_shared_type:
2060    return true;
2061  default:
2062    break;
2063  }
2064  return false;
2065}
2066
2067/// \brief Recursively clone \p InputDIE's subtrees that have been
2068/// selected to appear in the linked output.
2069///
2070/// \param OutOffset is the Offset where the newly created DIE will
2071/// lie in the linked compile unit.
2072///
2073/// \returns the cloned DIE object or null if nothing was selected.
2074DIE *DwarfLinker::cloneDIE(const DWARFDebugInfoEntryMinimal &InputDIE,
2075                           CompileUnit &Unit, int64_t PCOffset,
2076                           uint32_t OutOffset) {
2077  DWARFUnit &U = Unit.getOrigUnit();
2078  unsigned Idx = U.getDIEIndex(&InputDIE);
2079  CompileUnit::DIEInfo &Info = Unit.getInfo(Idx);
2080
2081  // Should the DIE appear in the output?
2082  if (!Unit.getInfo(Idx).Keep)
2083    return nullptr;
2084
2085  uint32_t Offset = InputDIE.getOffset();
2086  // The DIE might have been already created by a forward reference
2087  // (see cloneDieReferenceAttribute()).
2088  DIE *Die = Info.Clone;
2089  if (!Die)
2090    Die = Info.Clone = new DIE(dwarf::Tag(InputDIE.getTag()));
2091  assert(Die->getTag() == InputDIE.getTag());
2092  Die->setOffset(OutOffset);
2093
2094  // Extract and clone every attribute.
2095  DataExtractor Data = U.getDebugInfoExtractor();
2096  uint32_t NextOffset = U.getDIEAtIndex(Idx + 1)->getOffset();
2097  AttributesInfo AttrInfo;
2098
2099  // We could copy the data only if we need to aply a relocation to
2100  // it. After testing, it seems there is no performance downside to
2101  // doing the copy unconditionally, and it makes the code simpler.
2102  SmallString<40> DIECopy(Data.getData().substr(Offset, NextOffset - Offset));
2103  Data = DataExtractor(DIECopy, Data.isLittleEndian(), Data.getAddressSize());
2104  // Modify the copy with relocated addresses.
2105  if (applyValidRelocs(DIECopy, Offset, Data.isLittleEndian())) {
2106    // If we applied relocations, we store the value of high_pc that was
2107    // potentially stored in the input DIE. If high_pc is an address
2108    // (Dwarf version == 2), then it might have been relocated to a
2109    // totally unrelated value (because the end address in the object
2110    // file might be start address of another function which got moved
2111    // independantly by the linker). The computation of the actual
2112    // high_pc value is done in cloneAddressAttribute().
2113    AttrInfo.OrigHighPc =
2114        InputDIE.getAttributeValueAsAddress(&U, dwarf::DW_AT_high_pc, 0);
2115  }
2116
2117  // Reset the Offset to 0 as we will be working on the local copy of
2118  // the data.
2119  Offset = 0;
2120
2121  const auto *Abbrev = InputDIE.getAbbreviationDeclarationPtr();
2122  Offset += getULEB128Size(Abbrev->getCode());
2123
2124  // We are entering a subprogram. Get and propagate the PCOffset.
2125  if (Die->getTag() == dwarf::DW_TAG_subprogram)
2126    PCOffset = Info.AddrAdjust;
2127  AttrInfo.PCOffset = PCOffset;
2128
2129  for (const auto &AttrSpec : Abbrev->attributes()) {
2130    DWARFFormValue Val(AttrSpec.Form);
2131    uint32_t AttrSize = Offset;
2132    Val.extractValue(Data, &Offset, &U);
2133    AttrSize = Offset - AttrSize;
2134
2135    OutOffset +=
2136        cloneAttribute(*Die, InputDIE, Unit, Val, AttrSpec, AttrSize, AttrInfo);
2137  }
2138
2139  // Look for accelerator entries.
2140  uint16_t Tag = InputDIE.getTag();
2141  // FIXME: This is slightly wrong. An inline_subroutine without a
2142  // low_pc, but with AT_ranges might be interesting to get into the
2143  // accelerator tables too. For now stick with dsymutil's behavior.
2144  if ((Info.InDebugMap || AttrInfo.HasLowPc) &&
2145      Tag != dwarf::DW_TAG_compile_unit &&
2146      getDIENames(InputDIE, Unit.getOrigUnit(), AttrInfo)) {
2147    if (AttrInfo.MangledName && AttrInfo.MangledName != AttrInfo.Name)
2148      Unit.addNameAccelerator(Die, AttrInfo.MangledName,
2149                              AttrInfo.MangledNameOffset,
2150                              Tag == dwarf::DW_TAG_inlined_subroutine);
2151    if (AttrInfo.Name)
2152      Unit.addNameAccelerator(Die, AttrInfo.Name, AttrInfo.NameOffset,
2153                              Tag == dwarf::DW_TAG_inlined_subroutine);
2154  } else if (isTypeTag(Tag) && !AttrInfo.IsDeclaration &&
2155             getDIENames(InputDIE, Unit.getOrigUnit(), AttrInfo)) {
2156    Unit.addTypeAccelerator(Die, AttrInfo.Name, AttrInfo.NameOffset);
2157  }
2158
2159  DIEAbbrev &NewAbbrev = Die->getAbbrev();
2160  // If a scope DIE is kept, we must have kept at least one child. If
2161  // it's not the case, we'll just be emitting one wasteful end of
2162  // children marker, but things won't break.
2163  if (InputDIE.hasChildren())
2164    NewAbbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
2165  // Assign a permanent abbrev number
2166  AssignAbbrev(Die->getAbbrev());
2167
2168  // Add the size of the abbreviation number to the output offset.
2169  OutOffset += getULEB128Size(Die->getAbbrevNumber());
2170
2171  if (!Abbrev->hasChildren()) {
2172    // Update our size.
2173    Die->setSize(OutOffset - Die->getOffset());
2174    return Die;
2175  }
2176
2177  // Recursively clone children.
2178  for (auto *Child = InputDIE.getFirstChild(); Child && !Child->isNULL();
2179       Child = Child->getSibling()) {
2180    if (DIE *Clone = cloneDIE(*Child, Unit, PCOffset, OutOffset)) {
2181      Die->addChild(std::unique_ptr<DIE>(Clone));
2182      OutOffset = Clone->getOffset() + Clone->getSize();
2183    }
2184  }
2185
2186  // Account for the end of children marker.
2187  OutOffset += sizeof(int8_t);
2188  // Update our size.
2189  Die->setSize(OutOffset - Die->getOffset());
2190  return Die;
2191}
2192
2193/// \brief Patch the input object file relevant debug_ranges entries
2194/// and emit them in the output file. Update the relevant attributes
2195/// to point at the new entries.
2196void DwarfLinker::patchRangesForUnit(const CompileUnit &Unit,
2197                                     DWARFContext &OrigDwarf) const {
2198  DWARFDebugRangeList RangeList;
2199  const auto &FunctionRanges = Unit.getFunctionRanges();
2200  unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
2201  DataExtractor RangeExtractor(OrigDwarf.getRangeSection(),
2202                               OrigDwarf.isLittleEndian(), AddressSize);
2203  auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
2204  DWARFUnit &OrigUnit = Unit.getOrigUnit();
2205  const auto *OrigUnitDie = OrigUnit.getCompileUnitDIE(false);
2206  uint64_t OrigLowPc = OrigUnitDie->getAttributeValueAsAddress(
2207      &OrigUnit, dwarf::DW_AT_low_pc, -1ULL);
2208  // Ranges addresses are based on the unit's low_pc. Compute the
2209  // offset we need to apply to adapt to the the new unit's low_pc.
2210  int64_t UnitPcOffset = 0;
2211  if (OrigLowPc != -1ULL)
2212    UnitPcOffset = int64_t(OrigLowPc) - Unit.getLowPc();
2213
2214  for (const auto &RangeAttribute : Unit.getRangesAttributes()) {
2215    uint32_t Offset = RangeAttribute->getValue();
2216    RangeAttribute->setValue(Streamer->getRangesSectionSize());
2217    RangeList.extract(RangeExtractor, &Offset);
2218    const auto &Entries = RangeList.getEntries();
2219    const DWARFDebugRangeList::RangeListEntry &First = Entries.front();
2220
2221    if (CurrRange == InvalidRange || First.StartAddress < CurrRange.start() ||
2222        First.StartAddress >= CurrRange.stop()) {
2223      CurrRange = FunctionRanges.find(First.StartAddress + OrigLowPc);
2224      if (CurrRange == InvalidRange ||
2225          CurrRange.start() > First.StartAddress + OrigLowPc) {
2226        reportWarning("no mapping for range.");
2227        continue;
2228      }
2229    }
2230
2231    Streamer->emitRangesEntries(UnitPcOffset, OrigLowPc, CurrRange, Entries,
2232                                AddressSize);
2233  }
2234}
2235
2236/// \brief Generate the debug_aranges entries for \p Unit and if the
2237/// unit has a DW_AT_ranges attribute, also emit the debug_ranges
2238/// contribution for this attribute.
2239/// FIXME: this could actually be done right in patchRangesForUnit,
2240/// but for the sake of initial bit-for-bit compatibility with legacy
2241/// dsymutil, we have to do it in a delayed pass.
2242void DwarfLinker::generateUnitRanges(CompileUnit &Unit) const {
2243  DIEInteger *Attr = Unit.getUnitRangesAttribute();
2244  if (Attr)
2245    Attr->setValue(Streamer->getRangesSectionSize());
2246  Streamer->emitUnitRangesEntries(Unit, Attr != nullptr);
2247}
2248
2249/// \brief Insert the new line info sequence \p Seq into the current
2250/// set of already linked line info \p Rows.
2251static void insertLineSequence(std::vector<DWARFDebugLine::Row> &Seq,
2252                               std::vector<DWARFDebugLine::Row> &Rows) {
2253  if (Seq.empty())
2254    return;
2255
2256  if (!Rows.empty() && Rows.back().Address < Seq.front().Address) {
2257    Rows.insert(Rows.end(), Seq.begin(), Seq.end());
2258    Seq.clear();
2259    return;
2260  }
2261
2262  auto InsertPoint = std::lower_bound(
2263      Rows.begin(), Rows.end(), Seq.front(),
2264      [](const DWARFDebugLine::Row &LHS, const DWARFDebugLine::Row &RHS) {
2265        return LHS.Address < RHS.Address;
2266      });
2267
2268  // FIXME: this only removes the unneeded end_sequence if the
2269  // sequences have been inserted in order. using a global sort like
2270  // described in patchLineTableForUnit() and delaying the end_sequene
2271  // elimination to emitLineTableForUnit() we can get rid of all of them.
2272  if (InsertPoint != Rows.end() &&
2273      InsertPoint->Address == Seq.front().Address && InsertPoint->EndSequence) {
2274    *InsertPoint = Seq.front();
2275    Rows.insert(InsertPoint + 1, Seq.begin() + 1, Seq.end());
2276  } else {
2277    Rows.insert(InsertPoint, Seq.begin(), Seq.end());
2278  }
2279
2280  Seq.clear();
2281}
2282
2283/// \brief Extract the line table for \p Unit from \p OrigDwarf, and
2284/// recreate a relocated version of these for the address ranges that
2285/// are present in the binary.
2286void DwarfLinker::patchLineTableForUnit(CompileUnit &Unit,
2287                                        DWARFContext &OrigDwarf) {
2288  const DWARFDebugInfoEntryMinimal *CUDie =
2289      Unit.getOrigUnit().getCompileUnitDIE();
2290  uint64_t StmtList = CUDie->getAttributeValueAsSectionOffset(
2291      &Unit.getOrigUnit(), dwarf::DW_AT_stmt_list, -1ULL);
2292  if (StmtList == -1ULL)
2293    return;
2294
2295  // Update the cloned DW_AT_stmt_list with the correct debug_line offset.
2296  if (auto *OutputDIE = Unit.getOutputUnitDIE()) {
2297    const auto &Abbrev = OutputDIE->getAbbrev().getData();
2298    auto Stmt = std::find_if(
2299        Abbrev.begin(), Abbrev.end(), [](const DIEAbbrevData &AbbrevData) {
2300          return AbbrevData.getAttribute() == dwarf::DW_AT_stmt_list;
2301        });
2302    assert(Stmt < Abbrev.end() && "Didn't find DW_AT_stmt_list in cloned DIE!");
2303    DIEInteger *StmtAttr =
2304        cast<DIEInteger>(OutputDIE->getValues()[Stmt - Abbrev.begin()]);
2305    StmtAttr->setValue(Streamer->getLineSectionSize());
2306  }
2307
2308  // Parse the original line info for the unit.
2309  DWARFDebugLine::LineTable LineTable;
2310  uint32_t StmtOffset = StmtList;
2311  StringRef LineData = OrigDwarf.getLineSection().Data;
2312  DataExtractor LineExtractor(LineData, OrigDwarf.isLittleEndian(),
2313                              Unit.getOrigUnit().getAddressByteSize());
2314  LineTable.parse(LineExtractor, &OrigDwarf.getLineSection().Relocs,
2315                  &StmtOffset);
2316
2317  // This vector is the output line table.
2318  std::vector<DWARFDebugLine::Row> NewRows;
2319  NewRows.reserve(LineTable.Rows.size());
2320
2321  // Current sequence of rows being extracted, before being inserted
2322  // in NewRows.
2323  std::vector<DWARFDebugLine::Row> Seq;
2324  const auto &FunctionRanges = Unit.getFunctionRanges();
2325  auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
2326
2327  // FIXME: This logic is meant to generate exactly the same output as
2328  // Darwin's classic dsynutil. There is a nicer way to implement this
2329  // by simply putting all the relocated line info in NewRows and simply
2330  // sorting NewRows before passing it to emitLineTableForUnit. This
2331  // should be correct as sequences for a function should stay
2332  // together in the sorted output. There are a few corner cases that
2333  // look suspicious though, and that required to implement the logic
2334  // this way. Revisit that once initial validation is finished.
2335
2336  // Iterate over the object file line info and extract the sequences
2337  // that correspond to linked functions.
2338  for (auto &Row : LineTable.Rows) {
2339    // Check wether we stepped out of the range. The range is
2340    // half-open, but consider accept the end address of the range if
2341    // it is marked as end_sequence in the input (because in that
2342    // case, the relocation offset is accurate and that entry won't
2343    // serve as the start of another function).
2344    if (CurrRange == InvalidRange || Row.Address < CurrRange.start() ||
2345        Row.Address > CurrRange.stop() ||
2346        (Row.Address == CurrRange.stop() && !Row.EndSequence)) {
2347      // We just stepped out of a known range. Insert a end_sequence
2348      // corresponding to the end of the range.
2349      uint64_t StopAddress = CurrRange != InvalidRange
2350                                 ? CurrRange.stop() + CurrRange.value()
2351                                 : -1ULL;
2352      CurrRange = FunctionRanges.find(Row.Address);
2353      bool CurrRangeValid =
2354          CurrRange != InvalidRange && CurrRange.start() <= Row.Address;
2355      if (!CurrRangeValid) {
2356        CurrRange = InvalidRange;
2357        if (StopAddress != -1ULL) {
2358          // Try harder by looking in the DebugMapObject function
2359          // ranges map. There are corner cases where this finds a
2360          // valid entry. It's unclear if this is right or wrong, but
2361          // for now do as dsymutil.
2362          // FIXME: Understand exactly what cases this addresses and
2363          // potentially remove it along with the Ranges map.
2364          auto Range = Ranges.lower_bound(Row.Address);
2365          if (Range != Ranges.begin() && Range != Ranges.end())
2366            --Range;
2367
2368          if (Range != Ranges.end() && Range->first <= Row.Address &&
2369              Range->second.first >= Row.Address) {
2370            StopAddress = Row.Address + Range->second.second;
2371          }
2372        }
2373      }
2374      if (StopAddress != -1ULL && !Seq.empty()) {
2375        // Insert end sequence row with the computed end address, but
2376        // the same line as the previous one.
2377        Seq.emplace_back(Seq.back());
2378        Seq.back().Address = StopAddress;
2379        Seq.back().EndSequence = 1;
2380        Seq.back().PrologueEnd = 0;
2381        Seq.back().BasicBlock = 0;
2382        Seq.back().EpilogueBegin = 0;
2383        insertLineSequence(Seq, NewRows);
2384      }
2385
2386      if (!CurrRangeValid)
2387        continue;
2388    }
2389
2390    // Ignore empty sequences.
2391    if (Row.EndSequence && Seq.empty())
2392      continue;
2393
2394    // Relocate row address and add it to the current sequence.
2395    Row.Address += CurrRange.value();
2396    Seq.emplace_back(Row);
2397
2398    if (Row.EndSequence)
2399      insertLineSequence(Seq, NewRows);
2400  }
2401
2402  // Finished extracting, now emit the line tables.
2403  uint32_t PrologueEnd = StmtList + 10 + LineTable.Prologue.PrologueLength;
2404  // FIXME: LLVM hardcodes it's prologue values. We just copy the
2405  // prologue over and that works because we act as both producer and
2406  // consumer. It would be nicer to have a real configurable line
2407  // table emitter.
2408  if (LineTable.Prologue.Version != 2 ||
2409      LineTable.Prologue.DefaultIsStmt != DWARF2_LINE_DEFAULT_IS_STMT ||
2410      LineTable.Prologue.LineBase != -5 || LineTable.Prologue.LineRange != 14 ||
2411      LineTable.Prologue.OpcodeBase != 13)
2412    reportWarning("line table paramters mismatch. Cannot emit.");
2413  else
2414    Streamer->emitLineTableForUnit(LineData.slice(StmtList + 4, PrologueEnd),
2415                                   LineTable.Prologue.MinInstLength, NewRows,
2416                                   Unit.getOrigUnit().getAddressByteSize());
2417}
2418
2419void DwarfLinker::emitAcceleratorEntriesForUnit(CompileUnit &Unit) {
2420  Streamer->emitPubNamesForUnit(Unit);
2421  Streamer->emitPubTypesForUnit(Unit);
2422}
2423
2424bool DwarfLinker::link(const DebugMap &Map) {
2425
2426  if (Map.begin() == Map.end()) {
2427    errs() << "Empty debug map.\n";
2428    return false;
2429  }
2430
2431  if (!createStreamer(Map.getTriple(), OutputFilename))
2432    return false;
2433
2434  // Size of the DIEs (and headers) generated for the linked output.
2435  uint64_t OutputDebugInfoSize = 0;
2436  // A unique ID that identifies each compile unit.
2437  unsigned UnitID = 0;
2438  for (const auto &Obj : Map.objects()) {
2439    CurrentDebugObject = Obj.get();
2440
2441    if (Options.Verbose)
2442      outs() << "DEBUG MAP OBJECT: " << Obj->getObjectFilename() << "\n";
2443    auto ErrOrObj = BinHolder.GetObjectFile(Obj->getObjectFilename());
2444    if (std::error_code EC = ErrOrObj.getError()) {
2445      reportWarning(Twine(Obj->getObjectFilename()) + ": " + EC.message());
2446      continue;
2447    }
2448
2449    // Look for relocations that correspond to debug map entries.
2450    if (!findValidRelocsInDebugInfo(*ErrOrObj, *Obj)) {
2451      if (Options.Verbose)
2452        outs() << "No valid relocations found. Skipping.\n";
2453      continue;
2454    }
2455
2456    // Setup access to the debug info.
2457    DWARFContextInMemory DwarfContext(*ErrOrObj);
2458    startDebugObject(DwarfContext, *Obj);
2459
2460    // In a first phase, just read in the debug info and store the DIE
2461    // parent links that we will use during the next phase.
2462    for (const auto &CU : DwarfContext.compile_units()) {
2463      auto *CUDie = CU->getCompileUnitDIE(false);
2464      if (Options.Verbose) {
2465        outs() << "Input compilation unit:";
2466        CUDie->dump(outs(), CU.get(), 0);
2467      }
2468      Units.emplace_back(*CU, UnitID++);
2469      gatherDIEParents(CUDie, 0, Units.back());
2470    }
2471
2472    // Then mark all the DIEs that need to be present in the linked
2473    // output and collect some information about them. Note that this
2474    // loop can not be merged with the previous one becaue cross-cu
2475    // references require the ParentIdx to be setup for every CU in
2476    // the object file before calling this.
2477    for (auto &CurrentUnit : Units)
2478      lookForDIEsToKeep(*CurrentUnit.getOrigUnit().getCompileUnitDIE(), *Obj,
2479                        CurrentUnit, 0);
2480
2481    // The calls to applyValidRelocs inside cloneDIE will walk the
2482    // reloc array again (in the same way findValidRelocsInDebugInfo()
2483    // did). We need to reset the NextValidReloc index to the beginning.
2484    NextValidReloc = 0;
2485
2486    // Construct the output DIE tree by cloning the DIEs we chose to
2487    // keep above. If there are no valid relocs, then there's nothing
2488    // to clone/emit.
2489    if (!ValidRelocs.empty())
2490      for (auto &CurrentUnit : Units) {
2491        const auto *InputDIE = CurrentUnit.getOrigUnit().getCompileUnitDIE();
2492        CurrentUnit.setStartOffset(OutputDebugInfoSize);
2493        DIE *OutputDIE = cloneDIE(*InputDIE, CurrentUnit, 0 /* PCOffset */,
2494                                  11 /* Unit Header size */);
2495        CurrentUnit.setOutputUnitDIE(OutputDIE);
2496        OutputDebugInfoSize = CurrentUnit.computeNextUnitOffset();
2497        if (Options.NoOutput)
2498          continue;
2499        // FIXME: for compatibility with the classic dsymutil, we emit
2500        // an empty line table for the unit, even if the unit doesn't
2501        // actually exist in the DIE tree.
2502        patchLineTableForUnit(CurrentUnit, DwarfContext);
2503        if (!OutputDIE)
2504          continue;
2505        patchRangesForUnit(CurrentUnit, DwarfContext);
2506        Streamer->emitLocationsForUnit(CurrentUnit, DwarfContext);
2507        emitAcceleratorEntriesForUnit(CurrentUnit);
2508      }
2509
2510    // Emit all the compile unit's debug information.
2511    if (!ValidRelocs.empty() && !Options.NoOutput)
2512      for (auto &CurrentUnit : Units) {
2513        generateUnitRanges(CurrentUnit);
2514        CurrentUnit.fixupForwardReferences();
2515        Streamer->emitCompileUnitHeader(CurrentUnit);
2516        if (!CurrentUnit.getOutputUnitDIE())
2517          continue;
2518        Streamer->emitDIE(*CurrentUnit.getOutputUnitDIE());
2519      }
2520
2521    // Clean-up before starting working on the next object.
2522    endDebugObject();
2523  }
2524
2525  // Emit everything that's global.
2526  if (!Options.NoOutput) {
2527    Streamer->emitAbbrevs(Abbreviations);
2528    Streamer->emitStrings(StringPool);
2529  }
2530
2531  return Options.NoOutput ? true : Streamer->finish();
2532}
2533}
2534
2535bool linkDwarf(StringRef OutputFilename, const DebugMap &DM,
2536               const LinkOptions &Options) {
2537  DwarfLinker Linker(OutputFilename, Options);
2538  return Linker.link(DM);
2539}
2540}
2541}
2542