[dsymutil] Add debug_str construction support.
[oota-llvm.git] / tools / dsymutil / DwarfLinker.cpp
1 //===- tools/dsymutil/DwarfLinker.cpp - Dwarf debug info linker -----------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #include "DebugMap.h"
10 #include "BinaryHolder.h"
11 #include "DebugMap.h"
12 #include "dsymutil.h"
13 #include "llvm/CodeGen/AsmPrinter.h"
14 #include "llvm/CodeGen/DIE.h"
15 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
16 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
17 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
18 #include "llvm/MC/MCAsmBackend.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCCodeEmitter.h"
22 #include "llvm/MC/MCInstrInfo.h"
23 #include "llvm/MC/MCObjectFileInfo.h"
24 #include "llvm/MC/MCRegisterInfo.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/Object/MachO.h"
27 #include "llvm/Support/Dwarf.h"
28 #include "llvm/Support/LEB128.h"
29 #include "llvm/Support/TargetRegistry.h"
30 #include "llvm/Target/TargetMachine.h"
31 #include "llvm/Target/TargetOptions.h"
32 #include <string>
33
34 namespace llvm {
35 namespace dsymutil {
36
37 namespace {
38
39 void warn(const Twine &Warning, const Twine &Context) {
40   errs() << Twine("while processing ") + Context + ":\n";
41   errs() << Twine("warning: ") + Warning + "\n";
42 }
43
44 bool error(const Twine &Error, const Twine &Context) {
45   errs() << Twine("while processing ") + Context + ":\n";
46   errs() << Twine("error: ") + Error + "\n";
47   return false;
48 }
49
50 /// \brief Stores all information relating to a compile unit, be it in
51 /// its original instance in the object file to its brand new cloned
52 /// and linked DIE tree.
53 class CompileUnit {
54 public:
55   /// \brief Information gathered about a DIE in the object file.
56   struct DIEInfo {
57     uint64_t Address;   ///< Linked address of the described entity.
58     uint32_t ParentIdx; ///< The index of this DIE's parent.
59     bool Keep;          ///< Is the DIE part of the linked output?
60     bool InDebugMap;    ///< Was this DIE's entity found in the map?
61   };
62
63   CompileUnit(DWARFUnit &OrigUnit) : OrigUnit(OrigUnit) {
64     Info.resize(OrigUnit.getNumDIEs());
65   }
66
67   // Workaround MSVC not supporting implicit move ops
68   CompileUnit(CompileUnit &&RHS)
69       : OrigUnit(RHS.OrigUnit), Info(std::move(RHS.Info)),
70         CUDie(std::move(RHS.CUDie)), StartOffset(RHS.StartOffset),
71         NextUnitOffset(RHS.NextUnitOffset) {}
72
73   DWARFUnit &getOrigUnit() const { return OrigUnit; }
74
75   DIE *getOutputUnitDIE() const { return CUDie.get(); }
76   void setOutputUnitDIE(DIE *Die) { CUDie.reset(Die); }
77
78   DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; }
79   const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; }
80
81   uint64_t getStartOffset() const { return StartOffset; }
82   uint64_t getNextUnitOffset() const { return NextUnitOffset; }
83
84   /// \brief Set the start and end offsets for this unit. Must be
85   /// called after the CU's DIEs have been cloned. The unit start
86   /// offset will be set to \p DebugInfoSize.
87   /// \returns the next unit offset (which is also the current
88   /// debug_info section size).
89   uint64_t computeOffsets(uint64_t DebugInfoSize);
90
91 private:
92   DWARFUnit &OrigUnit;
93   std::vector<DIEInfo> Info;  ///< DIE info indexed by DIE index.
94   std::unique_ptr<DIE> CUDie; ///< Root of the linked DIE tree.
95
96   uint64_t StartOffset;
97   uint64_t NextUnitOffset;
98 };
99
100 uint64_t CompileUnit::computeOffsets(uint64_t DebugInfoSize) {
101   StartOffset = DebugInfoSize;
102   NextUnitOffset = StartOffset + 11 /* Header size */;
103   // The root DIE might be null, meaning that the Unit had nothing to
104   // contribute to the linked output. In that case, we will emit the
105   // unit header without any actual DIE.
106   if (CUDie)
107     NextUnitOffset += CUDie->getSize();
108   return NextUnitOffset;
109 }
110
111 /// \brief A string table that doesn't need relocations.
112 ///
113 /// We are doing a final link, no need for a string table that
114 /// has relocation entries for every reference to it. This class
115 /// provides this ablitity by just associating offsets with
116 /// strings.
117 class NonRelocatableStringpool {
118 public:
119   /// \brief Entries are stored into the StringMap and simply linked
120   /// together through the second element of this pair in order to
121   /// keep track of insertion order.
122   typedef StringMap<std::pair<uint32_t, StringMapEntryBase *>, BumpPtrAllocator>
123       MapTy;
124
125   NonRelocatableStringpool()
126       : CurrentEndOffset(0), Sentinel(0), Last(&Sentinel) {
127     // Legacy dsymutil puts an empty string at the start of the line
128     // table.
129     getStringOffset("");
130   }
131
132   /// \brief Get the offset of string \p S in the string table. This
133   /// can insert a new element or return the offset of a preexisitng
134   /// one.
135   uint32_t getStringOffset(StringRef S);
136
137   /// \brief Get permanent storage for \p S (but do not necessarily
138   /// emit \p S in the output section).
139   /// \returns The StringRef that points to permanent storage to use
140   /// in place of \p S.
141   StringRef internString(StringRef S);
142
143   // \brief Return the first entry of the string table.
144   const MapTy::MapEntryTy *getFirstEntry() const {
145     return getNextEntry(&Sentinel);
146   }
147
148   // \brief Get the entry following \p E in the string table or null
149   // if \p E was the last entry.
150   const MapTy::MapEntryTy *getNextEntry(const MapTy::MapEntryTy *E) const {
151     return static_cast<const MapTy::MapEntryTy *>(E->getValue().second);
152   }
153
154   uint64_t getSize() { return CurrentEndOffset; }
155
156 private:
157   MapTy Strings;
158   uint32_t CurrentEndOffset;
159   MapTy::MapEntryTy Sentinel, *Last;
160 };
161
162 /// \brief Get the offset of string \p S in the string table. This
163 /// can insert a new element or return the offset of a preexisitng
164 /// one.
165 uint32_t NonRelocatableStringpool::getStringOffset(StringRef S) {
166   if (S.empty() && !Strings.empty())
167     return 0;
168
169   std::pair<uint32_t, StringMapEntryBase *> Entry(0, nullptr);
170   MapTy::iterator It;
171   bool Inserted;
172
173   // A non-empty string can't be at offset 0, so if we have an entry
174   // with a 0 offset, it must be a previously interned string.
175   std::tie(It, Inserted) = Strings.insert(std::make_pair(S, Entry));
176   if (Inserted || It->getValue().first == 0) {
177     // Set offset and chain at the end of the entries list.
178     It->getValue().first = CurrentEndOffset;
179     CurrentEndOffset += S.size() + 1; // +1 for the '\0'.
180     Last->getValue().second = &*It;
181     Last = &*It;
182   }
183   return It->getValue().first;
184 };
185
186 /// \brief Put \p S into the StringMap so that it gets permanent
187 /// storage, but do not actually link it in the chain of elements
188 /// that go into the output section. A latter call to
189 /// getStringOffset() with the same string will chain it though.
190 StringRef NonRelocatableStringpool::internString(StringRef S) {
191   std::pair<uint32_t, StringMapEntryBase *> Entry(0, nullptr);
192   auto InsertResult = Strings.insert(std::make_pair(S, Entry));
193   return InsertResult.first->getKey();
194 };
195
196 /// \brief The Dwarf streaming logic
197 ///
198 /// All interactions with the MC layer that is used to build the debug
199 /// information binary representation are handled in this class.
200 class DwarfStreamer {
201   /// \defgroup MCObjects MC layer objects constructed by the streamer
202   /// @{
203   std::unique_ptr<MCRegisterInfo> MRI;
204   std::unique_ptr<MCAsmInfo> MAI;
205   std::unique_ptr<MCObjectFileInfo> MOFI;
206   std::unique_ptr<MCContext> MC;
207   MCAsmBackend *MAB; // Owned by MCStreamer
208   std::unique_ptr<MCInstrInfo> MII;
209   std::unique_ptr<MCSubtargetInfo> MSTI;
210   MCCodeEmitter *MCE; // Owned by MCStreamer
211   MCStreamer *MS;     // Owned by AsmPrinter
212   std::unique_ptr<TargetMachine> TM;
213   std::unique_ptr<AsmPrinter> Asm;
214   /// @}
215
216   /// \brief the file we stream the linked Dwarf to.
217   std::unique_ptr<raw_fd_ostream> OutFile;
218
219 public:
220   /// \brief Actually create the streamer and the ouptut file.
221   ///
222   /// This could be done directly in the constructor, but it feels
223   /// more natural to handle errors through return value.
224   bool init(Triple TheTriple, StringRef OutputFilename);
225
226   /// \brief Dump the file to the disk.
227   bool finish();
228
229   AsmPrinter &getAsmPrinter() const { return *Asm; }
230
231   /// \brief Set the current output section to debug_info and change
232   /// the MC Dwarf version to \p DwarfVersion.
233   void switchToDebugInfoSection(unsigned DwarfVersion);
234
235   /// \brief Emit the compilation unit header for \p Unit in the
236   /// debug_info section.
237   ///
238   /// As a side effect, this also switches the current Dwarf version
239   /// of the MC layer to the one of U.getOrigUnit().
240   void emitCompileUnitHeader(CompileUnit &Unit);
241
242   /// \brief Recursively emit the DIE tree rooted at \p Die.
243   void emitDIE(DIE &Die);
244
245   /// \brief Emit the abbreviation table \p Abbrevs to the
246   /// debug_abbrev section.
247   void emitAbbrevs(const std::vector<DIEAbbrev *> &Abbrevs);
248
249   /// \brief Emit the string table described by \p Pool.
250   void emitStrings(const NonRelocatableStringpool &Pool);
251 };
252
253 bool DwarfStreamer::init(Triple TheTriple, StringRef OutputFilename) {
254   std::string ErrorStr;
255   std::string TripleName;
256   StringRef Context = "dwarf streamer init";
257
258   // Get the target.
259   const Target *TheTarget =
260       TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr);
261   if (!TheTarget)
262     return error(ErrorStr, Context);
263   TripleName = TheTriple.getTriple();
264
265   // Create all the MC Objects.
266   MRI.reset(TheTarget->createMCRegInfo(TripleName));
267   if (!MRI)
268     return error(Twine("no register info for target ") + TripleName, Context);
269
270   MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName));
271   if (!MAI)
272     return error("no asm info for target " + TripleName, Context);
273
274   MOFI.reset(new MCObjectFileInfo);
275   MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get()));
276   MOFI->InitMCObjectFileInfo(TripleName, Reloc::Default, CodeModel::Default,
277                              *MC);
278
279   MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
280   if (!MAB)
281     return error("no asm backend for target " + TripleName, Context);
282
283   MII.reset(TheTarget->createMCInstrInfo());
284   if (!MII)
285     return error("no instr info info for target " + TripleName, Context);
286
287   MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
288   if (!MSTI)
289     return error("no subtarget info for target " + TripleName, Context);
290
291   MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MSTI, *MC);
292   if (!MCE)
293     return error("no code emitter for target " + TripleName, Context);
294
295   // Create the output file.
296   std::error_code EC;
297   OutFile =
298       llvm::make_unique<raw_fd_ostream>(OutputFilename, EC, sys::fs::F_None);
299   if (EC)
300     return error(Twine(OutputFilename) + ": " + EC.message(), Context);
301
302   MS = TheTarget->createMCObjectStreamer(TripleName, *MC, *MAB, *OutFile, MCE,
303                                          *MSTI, false);
304   if (!MS)
305     return error("no object streamer for target " + TripleName, Context);
306
307   // Finally create the AsmPrinter we'll use to emit the DIEs.
308   TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions()));
309   if (!TM)
310     return error("no target machine for target " + TripleName, Context);
311
312   Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS)));
313   if (!Asm)
314     return error("no asm printer for target " + TripleName, Context);
315
316   return true;
317 }
318
319 bool DwarfStreamer::finish() {
320   MS->Finish();
321   return true;
322 }
323
324 /// \brief Set the current output section to debug_info and change
325 /// the MC Dwarf version to \p DwarfVersion.
326 void DwarfStreamer::switchToDebugInfoSection(unsigned DwarfVersion) {
327   MS->SwitchSection(MOFI->getDwarfInfoSection());
328   MC->setDwarfVersion(DwarfVersion);
329 }
330
331 /// \brief Emit the compilation unit header for \p Unit in the
332 /// debug_info section.
333 ///
334 /// A Dwarf scetion header is encoded as:
335 ///  uint32_t   Unit length (omiting this field)
336 ///  uint16_t   Version
337 ///  uint32_t   Abbreviation table offset
338 ///  uint8_t    Address size
339 ///
340 /// Leading to a total of 11 bytes.
341 void DwarfStreamer::emitCompileUnitHeader(CompileUnit &Unit) {
342   unsigned Version = Unit.getOrigUnit().getVersion();
343   switchToDebugInfoSection(Version);
344
345   // Emit size of content not including length itself. The size has
346   // already been computed in CompileUnit::computeOffsets(). Substract
347   // 4 to that size to account for the length field.
348   Asm->EmitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset() - 4);
349   Asm->EmitInt16(Version);
350   // We share one abbreviations table across all units so it's always at the
351   // start of the section.
352   Asm->EmitInt32(0);
353   Asm->EmitInt8(Unit.getOrigUnit().getAddressByteSize());
354 }
355
356 /// \brief Emit the \p Abbrevs array as the shared abbreviation table
357 /// for the linked Dwarf file.
358 void DwarfStreamer::emitAbbrevs(const std::vector<DIEAbbrev *> &Abbrevs) {
359   MS->SwitchSection(MOFI->getDwarfAbbrevSection());
360   Asm->emitDwarfAbbrevs(Abbrevs);
361 }
362
363 /// \brief Recursively emit the DIE tree rooted at \p Die.
364 void DwarfStreamer::emitDIE(DIE &Die) {
365   MS->SwitchSection(MOFI->getDwarfInfoSection());
366   Asm->emitDwarfDIE(Die);
367 }
368
369 /// \brief Emit the debug_str section stored in \p Pool.
370 void DwarfStreamer::emitStrings(const NonRelocatableStringpool &Pool) {
371   Asm->OutStreamer.SwitchSection(MOFI->getDwarfStrSection());
372   for (auto *Entry = Pool.getFirstEntry(); Entry;
373        Entry = Pool.getNextEntry(Entry))
374     Asm->OutStreamer.EmitBytes(
375         StringRef(Entry->getKey().data(), Entry->getKey().size() + 1));
376 }
377
378 /// \brief The core of the Dwarf linking logic.
379 ///
380 /// The link of the dwarf information from the object files will be
381 /// driven by the selection of 'root DIEs', which are DIEs that
382 /// describe variables or functions that are present in the linked
383 /// binary (and thus have entries in the debug map). All the debug
384 /// information that will be linked (the DIEs, but also the line
385 /// tables, ranges, ...) is derived from that set of root DIEs.
386 ///
387 /// The root DIEs are identified because they contain relocations that
388 /// correspond to a debug map entry at specific places (the low_pc for
389 /// a function, the location for a variable). These relocations are
390 /// called ValidRelocs in the DwarfLinker and are gathered as a very
391 /// first step when we start processing a DebugMapObject.
392 class DwarfLinker {
393 public:
394   DwarfLinker(StringRef OutputFilename, const LinkOptions &Options)
395       : OutputFilename(OutputFilename), Options(Options),
396         BinHolder(Options.Verbose) {}
397
398   ~DwarfLinker() {
399     for (auto *Abbrev : Abbreviations)
400       delete Abbrev;
401   }
402
403   /// \brief Link the contents of the DebugMap.
404   bool link(const DebugMap &);
405
406 private:
407   /// \brief Called at the start of a debug object link.
408   void startDebugObject(DWARFContext &);
409
410   /// \brief Called at the end of a debug object link.
411   void endDebugObject();
412
413   /// \defgroup FindValidRelocations Translate debug map into a list
414   /// of relevant relocations
415   ///
416   /// @{
417   struct ValidReloc {
418     uint32_t Offset;
419     uint32_t Size;
420     uint64_t Addend;
421     const DebugMapObject::DebugMapEntry *Mapping;
422
423     ValidReloc(uint32_t Offset, uint32_t Size, uint64_t Addend,
424                const DebugMapObject::DebugMapEntry *Mapping)
425         : Offset(Offset), Size(Size), Addend(Addend), Mapping(Mapping) {}
426
427     bool operator<(const ValidReloc &RHS) const { return Offset < RHS.Offset; }
428   };
429
430   /// \brief The valid relocations for the current DebugMapObject.
431   /// This vector is sorted by relocation offset.
432   std::vector<ValidReloc> ValidRelocs;
433
434   /// \brief Index into ValidRelocs of the next relocation to
435   /// consider. As we walk the DIEs in acsending file offset and as
436   /// ValidRelocs is sorted by file offset, keeping this index
437   /// uptodate is all we have to do to have a cheap lookup during the
438   /// root DIE selection.
439   unsigned NextValidReloc;
440
441   bool findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
442                                   const DebugMapObject &DMO);
443
444   bool findValidRelocs(const object::SectionRef &Section,
445                        const object::ObjectFile &Obj,
446                        const DebugMapObject &DMO);
447
448   void findValidRelocsMachO(const object::SectionRef &Section,
449                             const object::MachOObjectFile &Obj,
450                             const DebugMapObject &DMO);
451   /// @}
452
453   /// \defgroup FindRootDIEs Find DIEs corresponding to debug map entries.
454   ///
455   /// @{
456   /// \brief Recursively walk the \p DIE tree and look for DIEs to
457   /// keep. Store that information in \p CU's DIEInfo.
458   void lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE,
459                          const DebugMapObject &DMO, CompileUnit &CU,
460                          unsigned Flags);
461
462   /// \brief Flags passed to DwarfLinker::lookForDIEsToKeep
463   enum TravesalFlags {
464     TF_Keep = 1 << 0,            ///< Mark the traversed DIEs as kept.
465     TF_InFunctionScope = 1 << 1, ///< Current scope is a fucntion scope.
466     TF_DependencyWalk = 1 << 2,  ///< Walking the dependencies of a kept DIE.
467     TF_ParentWalk = 1 << 3,      ///< Walking up the parents of a kept DIE.
468   };
469
470   /// \brief Mark the passed DIE as well as all the ones it depends on
471   /// as kept.
472   void keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE,
473                                CompileUnit::DIEInfo &MyInfo,
474                                const DebugMapObject &DMO, CompileUnit &CU,
475                                unsigned Flags);
476
477   unsigned shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE,
478                          CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
479                          unsigned Flags);
480
481   unsigned shouldKeepVariableDIE(const DWARFDebugInfoEntryMinimal &DIE,
482                                  CompileUnit &Unit,
483                                  CompileUnit::DIEInfo &MyInfo, unsigned Flags);
484
485   unsigned shouldKeepSubprogramDIE(const DWARFDebugInfoEntryMinimal &DIE,
486                                    CompileUnit &Unit,
487                                    CompileUnit::DIEInfo &MyInfo,
488                                    unsigned Flags);
489
490   bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
491                           CompileUnit::DIEInfo &Info);
492   /// @}
493
494   /// \defgroup Linking Methods used to link the debug information
495   ///
496   /// @{
497   /// \brief Recursively clone \p InputDIE into an tree of DIE objects
498   /// where useless (as decided by lookForDIEsToKeep()) bits have been
499   /// stripped out and addresses have been rewritten according to the
500   /// debug map.
501   ///
502   /// \param OutOffset is the offset the cloned DIE in the output
503   /// compile unit.
504   ///
505   /// \returns the root of the cloned tree.
506   DIE *cloneDIE(const DWARFDebugInfoEntryMinimal &InputDIE, CompileUnit &U,
507                 uint32_t OutOffset);
508
509   typedef DWARFAbbreviationDeclaration::AttributeSpec AttributeSpec;
510
511   /// \brief Helper for cloneDIE.
512   unsigned cloneAttribute(DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE,
513                           CompileUnit &U, const DWARFFormValue &Val,
514                           const AttributeSpec AttrSpec, unsigned AttrSize);
515
516   /// \brief Helper for cloneDIE.
517   unsigned cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec,
518                                 const DWARFFormValue &Val, const DWARFUnit &U);
519
520   /// \brief Helper for cloneDIE.
521   unsigned cloneDieReferenceAttribute(DIE &Die, AttributeSpec AttrSpec,
522                                       unsigned AttrSize);
523
524   /// \brief Helper for cloneDIE.
525   unsigned cloneBlockAttribute(DIE &Die, AttributeSpec AttrSpec,
526                                const DWARFFormValue &Val, unsigned AttrSize);
527
528   /// \brief Helper for cloneDIE.
529   unsigned cloneScalarAttribute(DIE &Die,
530                                 const DWARFDebugInfoEntryMinimal &InputDIE,
531                                 const DWARFUnit &U, AttributeSpec AttrSpec,
532                                 const DWARFFormValue &Val, unsigned AttrSize);
533
534   /// \brief Assign an abbreviation number to \p Abbrev
535   void AssignAbbrev(DIEAbbrev &Abbrev);
536
537   /// \brief FoldingSet that uniques the abbreviations.
538   FoldingSet<DIEAbbrev> AbbreviationsSet;
539   /// \brief Storage for the unique Abbreviations.
540   /// This is passed to AsmPrinter::emitDwarfAbbrevs(), thus it cannot
541   /// be changed to a vecot of unique_ptrs.
542   std::vector<DIEAbbrev *> Abbreviations;
543
544   /// \brief DIELoc objects that need to be destructed (but not freed!).
545   std::vector<DIELoc *> DIELocs;
546   /// \brief DIEBlock objects that need to be destructed (but not freed!).
547   std::vector<DIEBlock *> DIEBlocks;
548   /// \brief Allocator used for all the DIEValue objects.
549   BumpPtrAllocator DIEAlloc;
550   /// @}
551
552   /// \defgroup Helpers Various helper methods.
553   ///
554   /// @{
555   const DWARFDebugInfoEntryMinimal *
556   resolveDIEReference(DWARFFormValue &RefValue, const DWARFUnit &Unit,
557                       const DWARFDebugInfoEntryMinimal &DIE,
558                       CompileUnit *&ReferencedCU);
559
560   CompileUnit *getUnitForOffset(unsigned Offset);
561
562   void reportWarning(const Twine &Warning, const DWARFUnit *Unit = nullptr,
563                      const DWARFDebugInfoEntryMinimal *DIE = nullptr);
564
565   bool createStreamer(Triple TheTriple, StringRef OutputFilename);
566   /// @}
567
568 private:
569   std::string OutputFilename;
570   LinkOptions Options;
571   BinaryHolder BinHolder;
572   std::unique_ptr<DwarfStreamer> Streamer;
573
574   /// The units of the current debug map object.
575   std::vector<CompileUnit> Units;
576
577   /// The debug map object curently under consideration.
578   DebugMapObject *CurrentDebugObject;
579
580   /// \brief The Dwarf string pool
581   NonRelocatableStringpool StringPool;
582 };
583
584 /// \brief Similar to DWARFUnitSection::getUnitForOffset(), but
585 /// returning our CompileUnit object instead.
586 CompileUnit *DwarfLinker::getUnitForOffset(unsigned Offset) {
587   auto CU =
588       std::upper_bound(Units.begin(), Units.end(), Offset,
589                        [](uint32_t LHS, const CompileUnit &RHS) {
590                          return LHS < RHS.getOrigUnit().getNextUnitOffset();
591                        });
592   return CU != Units.end() ? &*CU : nullptr;
593 }
594
595 /// \brief Resolve the DIE attribute reference that has been
596 /// extracted in \p RefValue. The resulting DIE migh be in another
597 /// CompileUnit which is stored into \p ReferencedCU.
598 /// \returns null if resolving fails for any reason.
599 const DWARFDebugInfoEntryMinimal *DwarfLinker::resolveDIEReference(
600     DWARFFormValue &RefValue, const DWARFUnit &Unit,
601     const DWARFDebugInfoEntryMinimal &DIE, CompileUnit *&RefCU) {
602   assert(RefValue.isFormClass(DWARFFormValue::FC_Reference));
603   uint64_t RefOffset = *RefValue.getAsReference(&Unit);
604
605   if ((RefCU = getUnitForOffset(RefOffset)))
606     if (const auto *RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset))
607       return RefDie;
608
609   reportWarning("could not find referenced DIE", &Unit, &DIE);
610   return nullptr;
611 }
612
613 /// \brief Report a warning to the user, optionaly including
614 /// information about a specific \p DIE related to the warning.
615 void DwarfLinker::reportWarning(const Twine &Warning, const DWARFUnit *Unit,
616                                 const DWARFDebugInfoEntryMinimal *DIE) {
617   StringRef Context = "<debug map>";
618   if (CurrentDebugObject)
619     Context = CurrentDebugObject->getObjectFilename();
620   warn(Warning, Context);
621
622   if (!Options.Verbose || !DIE)
623     return;
624
625   errs() << "    in DIE:\n";
626   DIE->dump(errs(), const_cast<DWARFUnit *>(Unit), 0 /* RecurseDepth */,
627             6 /* Indent */);
628 }
629
630 bool DwarfLinker::createStreamer(Triple TheTriple, StringRef OutputFilename) {
631   if (Options.NoOutput)
632     return true;
633
634   Streamer = llvm::make_unique<DwarfStreamer>();
635   return Streamer->init(TheTriple, OutputFilename);
636 }
637
638 /// \brief Recursive helper to gather the child->parent relationships in the
639 /// original compile unit.
640 static void gatherDIEParents(const DWARFDebugInfoEntryMinimal *DIE,
641                              unsigned ParentIdx, CompileUnit &CU) {
642   unsigned MyIdx = CU.getOrigUnit().getDIEIndex(DIE);
643   CU.getInfo(MyIdx).ParentIdx = ParentIdx;
644
645   if (DIE->hasChildren())
646     for (auto *Child = DIE->getFirstChild(); Child && !Child->isNULL();
647          Child = Child->getSibling())
648       gatherDIEParents(Child, MyIdx, CU);
649 }
650
651 static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) {
652   switch (Tag) {
653   default:
654     return false;
655   case dwarf::DW_TAG_subprogram:
656   case dwarf::DW_TAG_lexical_block:
657   case dwarf::DW_TAG_subroutine_type:
658   case dwarf::DW_TAG_structure_type:
659   case dwarf::DW_TAG_class_type:
660   case dwarf::DW_TAG_union_type:
661     return true;
662   }
663   llvm_unreachable("Invalid Tag");
664 }
665
666 void DwarfLinker::startDebugObject(DWARFContext &Dwarf) {
667   Units.reserve(Dwarf.getNumCompileUnits());
668   NextValidReloc = 0;
669 }
670
671 void DwarfLinker::endDebugObject() {
672   Units.clear();
673   ValidRelocs.clear();
674
675   for (auto *Block : DIEBlocks)
676     Block->~DIEBlock();
677   for (auto *Loc : DIELocs)
678     Loc->~DIELoc();
679
680   DIEBlocks.clear();
681   DIELocs.clear();
682   DIEAlloc.Reset();
683 }
684
685 /// \brief Iterate over the relocations of the given \p Section and
686 /// store the ones that correspond to debug map entries into the
687 /// ValidRelocs array.
688 void DwarfLinker::findValidRelocsMachO(const object::SectionRef &Section,
689                                        const object::MachOObjectFile &Obj,
690                                        const DebugMapObject &DMO) {
691   StringRef Contents;
692   Section.getContents(Contents);
693   DataExtractor Data(Contents, Obj.isLittleEndian(), 0);
694
695   for (const object::RelocationRef &Reloc : Section.relocations()) {
696     object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl();
697     MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef);
698     unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc);
699     uint64_t Offset64;
700     if ((RelocSize != 4 && RelocSize != 8) || Reloc.getOffset(Offset64)) {
701       reportWarning(" unsupported relocation in debug_info section.");
702       continue;
703     }
704     uint32_t Offset = Offset64;
705     // Mach-o uses REL relocations, the addend is at the relocation offset.
706     uint64_t Addend = Data.getUnsigned(&Offset, RelocSize);
707
708     auto Sym = Reloc.getSymbol();
709     if (Sym != Obj.symbol_end()) {
710       StringRef SymbolName;
711       if (Sym->getName(SymbolName)) {
712         reportWarning("error getting relocation symbol name.");
713         continue;
714       }
715       if (const auto *Mapping = DMO.lookupSymbol(SymbolName))
716         ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping);
717     } else if (const auto *Mapping = DMO.lookupObjectAddress(Addend)) {
718       // Do not store the addend. The addend was the address of the
719       // symbol in the object file, the address in the binary that is
720       // stored in the debug map doesn't need to be offseted.
721       ValidRelocs.emplace_back(Offset64, RelocSize, 0, Mapping);
722     }
723   }
724 }
725
726 /// \brief Dispatch the valid relocation finding logic to the
727 /// appropriate handler depending on the object file format.
728 bool DwarfLinker::findValidRelocs(const object::SectionRef &Section,
729                                   const object::ObjectFile &Obj,
730                                   const DebugMapObject &DMO) {
731   // Dispatch to the right handler depending on the file type.
732   if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj))
733     findValidRelocsMachO(Section, *MachOObj, DMO);
734   else
735     reportWarning(Twine("unsupported object file type: ") + Obj.getFileName());
736
737   if (ValidRelocs.empty())
738     return false;
739
740   // Sort the relocations by offset. We will walk the DIEs linearly in
741   // the file, this allows us to just keep an index in the relocation
742   // array that we advance during our walk, rather than resorting to
743   // some associative container. See DwarfLinker::NextValidReloc.
744   std::sort(ValidRelocs.begin(), ValidRelocs.end());
745   return true;
746 }
747
748 /// \brief Look for relocations in the debug_info section that match
749 /// entries in the debug map. These relocations will drive the Dwarf
750 /// link by indicating which DIEs refer to symbols present in the
751 /// linked binary.
752 /// \returns wether there are any valid relocations in the debug info.
753 bool DwarfLinker::findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
754                                              const DebugMapObject &DMO) {
755   // Find the debug_info section.
756   for (const object::SectionRef &Section : Obj.sections()) {
757     StringRef SectionName;
758     Section.getName(SectionName);
759     SectionName = SectionName.substr(SectionName.find_first_not_of("._"));
760     if (SectionName != "debug_info")
761       continue;
762     return findValidRelocs(Section, Obj, DMO);
763   }
764   return false;
765 }
766
767 /// \brief Checks that there is a relocation against an actual debug
768 /// map entry between \p StartOffset and \p NextOffset.
769 ///
770 /// This function must be called with offsets in strictly ascending
771 /// order because it never looks back at relocations it already 'went past'.
772 /// \returns true and sets Info.InDebugMap if it is the case.
773 bool DwarfLinker::hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
774                                      CompileUnit::DIEInfo &Info) {
775   assert(NextValidReloc == 0 ||
776          StartOffset > ValidRelocs[NextValidReloc - 1].Offset);
777   if (NextValidReloc >= ValidRelocs.size())
778     return false;
779
780   uint64_t RelocOffset = ValidRelocs[NextValidReloc].Offset;
781
782   // We might need to skip some relocs that we didn't consider. For
783   // example the high_pc of a discarded DIE might contain a reloc that
784   // is in the list because it actually corresponds to the start of a
785   // function that is in the debug map.
786   while (RelocOffset < StartOffset && NextValidReloc < ValidRelocs.size() - 1)
787     RelocOffset = ValidRelocs[++NextValidReloc].Offset;
788
789   if (RelocOffset < StartOffset || RelocOffset >= EndOffset)
790     return false;
791
792   const auto &ValidReloc = ValidRelocs[NextValidReloc++];
793   if (Options.Verbose)
794     outs() << "Found valid debug map entry: " << ValidReloc.Mapping->getKey()
795            << " " << format("\t%016" PRIx64 " => %016" PRIx64,
796                             ValidReloc.Mapping->getValue().ObjectAddress,
797                             ValidReloc.Mapping->getValue().BinaryAddress);
798
799   Info.Address =
800       ValidReloc.Mapping->getValue().BinaryAddress + ValidReloc.Addend;
801   Info.InDebugMap = true;
802   return true;
803 }
804
805 /// \brief Get the starting and ending (exclusive) offset for the
806 /// attribute with index \p Idx descibed by \p Abbrev. \p Offset is
807 /// supposed to point to the position of the first attribute described
808 /// by \p Abbrev.
809 /// \return [StartOffset, EndOffset) as a pair.
810 static std::pair<uint32_t, uint32_t>
811 getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx,
812                     unsigned Offset, const DWARFUnit &Unit) {
813   DataExtractor Data = Unit.getDebugInfoExtractor();
814
815   for (unsigned i = 0; i < Idx; ++i)
816     DWARFFormValue::skipValue(Abbrev->getFormByIndex(i), Data, &Offset, &Unit);
817
818   uint32_t End = Offset;
819   DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End, &Unit);
820
821   return std::make_pair(Offset, End);
822 }
823
824 /// \brief Check if a variable describing DIE should be kept.
825 /// \returns updated TraversalFlags.
826 unsigned DwarfLinker::shouldKeepVariableDIE(
827     const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit,
828     CompileUnit::DIEInfo &MyInfo, unsigned Flags) {
829   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
830
831   // Global variables with constant value can always be kept.
832   if (!(Flags & TF_InFunctionScope) &&
833       Abbrev->findAttributeIndex(dwarf::DW_AT_const_value) != -1U) {
834     MyInfo.InDebugMap = true;
835     return Flags | TF_Keep;
836   }
837
838   uint32_t LocationIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_location);
839   if (LocationIdx == -1U)
840     return Flags;
841
842   uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
843   const DWARFUnit &OrigUnit = Unit.getOrigUnit();
844   uint32_t LocationOffset, LocationEndOffset;
845   std::tie(LocationOffset, LocationEndOffset) =
846       getAttributeOffsets(Abbrev, LocationIdx, Offset, OrigUnit);
847
848   // See if there is a relocation to a valid debug map entry inside
849   // this variable's location. The order is important here. We want to
850   // always check in the variable has a valid relocation, so that the
851   // DIEInfo is filled. However, we don't want a static variable in a
852   // function to force us to keep the enclosing function.
853   if (!hasValidRelocation(LocationOffset, LocationEndOffset, MyInfo) ||
854       (Flags & TF_InFunctionScope))
855     return Flags;
856
857   if (Options.Verbose)
858     DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */);
859
860   return Flags | TF_Keep;
861 }
862
863 /// \brief Check if a function describing DIE should be kept.
864 /// \returns updated TraversalFlags.
865 unsigned DwarfLinker::shouldKeepSubprogramDIE(
866     const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit,
867     CompileUnit::DIEInfo &MyInfo, unsigned Flags) {
868   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
869
870   Flags |= TF_InFunctionScope;
871
872   uint32_t LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc);
873   if (LowPcIdx == -1U)
874     return Flags;
875
876   uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
877   const DWARFUnit &OrigUnit = Unit.getOrigUnit();
878   uint32_t LowPcOffset, LowPcEndOffset;
879   std::tie(LowPcOffset, LowPcEndOffset) =
880       getAttributeOffsets(Abbrev, LowPcIdx, Offset, OrigUnit);
881
882   uint64_t LowPc =
883       DIE.getAttributeValueAsAddress(&OrigUnit, dwarf::DW_AT_low_pc, -1ULL);
884   assert(LowPc != -1ULL && "low_pc attribute is not an address.");
885   if (LowPc == -1ULL ||
886       !hasValidRelocation(LowPcOffset, LowPcEndOffset, MyInfo))
887     return Flags;
888
889   if (Options.Verbose)
890     DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */);
891
892   return Flags | TF_Keep;
893 }
894
895 /// \brief Check if a DIE should be kept.
896 /// \returns updated TraversalFlags.
897 unsigned DwarfLinker::shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE,
898                                     CompileUnit &Unit,
899                                     CompileUnit::DIEInfo &MyInfo,
900                                     unsigned Flags) {
901   switch (DIE.getTag()) {
902   case dwarf::DW_TAG_constant:
903   case dwarf::DW_TAG_variable:
904     return shouldKeepVariableDIE(DIE, Unit, MyInfo, Flags);
905   case dwarf::DW_TAG_subprogram:
906     return shouldKeepSubprogramDIE(DIE, Unit, MyInfo, Flags);
907   case dwarf::DW_TAG_module:
908   case dwarf::DW_TAG_imported_module:
909   case dwarf::DW_TAG_imported_declaration:
910   case dwarf::DW_TAG_imported_unit:
911     // We always want to keep these.
912     return Flags | TF_Keep;
913   }
914
915   return Flags;
916 }
917
918 /// \brief Mark the passed DIE as well as all the ones it depends on
919 /// as kept.
920 ///
921 /// This function is called by lookForDIEsToKeep on DIEs that are
922 /// newly discovered to be needed in the link. It recursively calls
923 /// back to lookForDIEsToKeep while adding TF_DependencyWalk to the
924 /// TraversalFlags to inform it that it's not doing the primary DIE
925 /// tree walk.
926 void DwarfLinker::keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE,
927                                           CompileUnit::DIEInfo &MyInfo,
928                                           const DebugMapObject &DMO,
929                                           CompileUnit &CU, unsigned Flags) {
930   const DWARFUnit &Unit = CU.getOrigUnit();
931   MyInfo.Keep = true;
932
933   // First mark all the parent chain as kept.
934   unsigned AncestorIdx = MyInfo.ParentIdx;
935   while (!CU.getInfo(AncestorIdx).Keep) {
936     lookForDIEsToKeep(*Unit.getDIEAtIndex(AncestorIdx), DMO, CU,
937                       TF_ParentWalk | TF_Keep | TF_DependencyWalk);
938     AncestorIdx = CU.getInfo(AncestorIdx).ParentIdx;
939   }
940
941   // Then we need to mark all the DIEs referenced by this DIE's
942   // attributes as kept.
943   DataExtractor Data = Unit.getDebugInfoExtractor();
944   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
945   uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
946
947   // Mark all DIEs referenced through atttributes as kept.
948   for (const auto &AttrSpec : Abbrev->attributes()) {
949     DWARFFormValue Val(AttrSpec.Form);
950
951     if (!Val.isFormClass(DWARFFormValue::FC_Reference)) {
952       DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset, &Unit);
953       continue;
954     }
955
956     Val.extractValue(Data, &Offset, &Unit);
957     CompileUnit *ReferencedCU;
958     if (const auto *RefDIE = resolveDIEReference(Val, Unit, DIE, ReferencedCU))
959       lookForDIEsToKeep(*RefDIE, DMO, *ReferencedCU,
960                         TF_Keep | TF_DependencyWalk);
961   }
962 }
963
964 /// \brief Recursively walk the \p DIE tree and look for DIEs to
965 /// keep. Store that information in \p CU's DIEInfo.
966 ///
967 /// This function is the entry point of the DIE selection
968 /// algorithm. It is expected to walk the DIE tree in file order and
969 /// (though the mediation of its helper) call hasValidRelocation() on
970 /// each DIE that might be a 'root DIE' (See DwarfLinker class
971 /// comment).
972 /// While walking the dependencies of root DIEs, this function is
973 /// also called, but during these dependency walks the file order is
974 /// not respected. The TF_DependencyWalk flag tells us which kind of
975 /// traversal we are currently doing.
976 void DwarfLinker::lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE,
977                                     const DebugMapObject &DMO, CompileUnit &CU,
978                                     unsigned Flags) {
979   unsigned Idx = CU.getOrigUnit().getDIEIndex(&DIE);
980   CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
981   bool AlreadyKept = MyInfo.Keep;
982
983   // If the Keep flag is set, we are marking a required DIE's
984   // dependencies. If our target is already marked as kept, we're all
985   // set.
986   if ((Flags & TF_DependencyWalk) && AlreadyKept)
987     return;
988
989   // We must not call shouldKeepDIE while called from keepDIEAndDenpendencies,
990   // because it would screw up the relocation finding logic.
991   if (!(Flags & TF_DependencyWalk))
992     Flags = shouldKeepDIE(DIE, CU, MyInfo, Flags);
993
994   // If it is a newly kept DIE mark it as well as all its dependencies as kept.
995   if (!AlreadyKept && (Flags & TF_Keep))
996     keepDIEAndDenpendencies(DIE, MyInfo, DMO, CU, Flags);
997
998   // The TF_ParentWalk flag tells us that we are currently walking up
999   // the parent chain of a required DIE, and we don't want to mark all
1000   // the children of the parents as kept (consider for example a
1001   // DW_TAG_namespace node in the parent chain). There are however a
1002   // set of DIE types for which we want to ignore that directive and still
1003   // walk their children.
1004   if (dieNeedsChildrenToBeMeaningful(DIE.getTag()))
1005     Flags &= ~TF_ParentWalk;
1006
1007   if (!DIE.hasChildren() || (Flags & TF_ParentWalk))
1008     return;
1009
1010   for (auto *Child = DIE.getFirstChild(); Child && !Child->isNULL();
1011        Child = Child->getSibling())
1012     lookForDIEsToKeep(*Child, DMO, CU, Flags);
1013 }
1014
1015 /// \brief Assign an abbreviation numer to \p Abbrev.
1016 ///
1017 /// Our DIEs get freed after every DebugMapObject has been processed,
1018 /// thus the FoldingSet we use to unique DIEAbbrevs cannot refer to
1019 /// the instances hold by the DIEs. When we encounter an abbreviation
1020 /// that we don't know, we create a permanent copy of it.
1021 void DwarfLinker::AssignAbbrev(DIEAbbrev &Abbrev) {
1022   // Check the set for priors.
1023   FoldingSetNodeID ID;
1024   Abbrev.Profile(ID);
1025   void *InsertToken;
1026   DIEAbbrev *InSet = AbbreviationsSet.FindNodeOrInsertPos(ID, InsertToken);
1027
1028   // If it's newly added.
1029   if (InSet) {
1030     // Assign existing abbreviation number.
1031     Abbrev.setNumber(InSet->getNumber());
1032   } else {
1033     // Add to abbreviation list.
1034     Abbreviations.push_back(
1035         new DIEAbbrev(Abbrev.getTag(), Abbrev.hasChildren()));
1036     for (const auto &Attr : Abbrev.getData())
1037       Abbreviations.back()->AddAttribute(Attr.getAttribute(), Attr.getForm());
1038     AbbreviationsSet.InsertNode(Abbreviations.back(), InsertToken);
1039     // Assign the unique abbreviation number.
1040     Abbrev.setNumber(Abbreviations.size());
1041     Abbreviations.back()->setNumber(Abbreviations.size());
1042   }
1043 }
1044
1045 /// \brief Clone a string attribute described by \p AttrSpec and add
1046 /// it to \p Die.
1047 /// \returns the size of the new attribute.
1048 unsigned DwarfLinker::cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec,
1049                                            const DWARFFormValue &Val,
1050                                            const DWARFUnit &U) {
1051   // Switch everything to out of line strings.
1052   const char *String = *Val.getAsCString(&U);
1053   unsigned Offset = StringPool.getStringOffset(String);
1054   Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::DW_FORM_strp,
1055                new (DIEAlloc) DIEInteger(Offset));
1056   return 4;
1057 }
1058
1059 /// \brief Clone an attribute referencing another DIE and add
1060 /// it to \p Die.
1061 /// \returns the size of the new attribute.
1062 unsigned DwarfLinker::cloneDieReferenceAttribute(DIE &Die,
1063                                                  AttributeSpec AttrSpec,
1064                                                  unsigned AttrSize) {
1065   // FIXME: Handle DIE references.
1066   Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::Form(AttrSpec.Form),
1067                new (DIEAlloc) DIEInteger(0));
1068   return AttrSize;
1069 }
1070
1071 /// \brief Clone an attribute of block form (locations, constants) and add
1072 /// it to \p Die.
1073 /// \returns the size of the new attribute.
1074 unsigned DwarfLinker::cloneBlockAttribute(DIE &Die, AttributeSpec AttrSpec,
1075                                           const DWARFFormValue &Val,
1076                                           unsigned AttrSize) {
1077   DIE *Attr;
1078   DIEValue *Value;
1079   DIELoc *Loc = nullptr;
1080   DIEBlock *Block = nullptr;
1081   // Just copy the block data over.
1082   if (AttrSpec.Attr == dwarf::DW_FORM_exprloc) {
1083     Loc = new (DIEAlloc) DIELoc();
1084     DIELocs.push_back(Loc);
1085   } else {
1086     Block = new (DIEAlloc) DIEBlock();
1087     DIEBlocks.push_back(Block);
1088   }
1089   Attr = Loc ? static_cast<DIE *>(Loc) : static_cast<DIE *>(Block);
1090   Value = Loc ? static_cast<DIEValue *>(Loc) : static_cast<DIEValue *>(Block);
1091   ArrayRef<uint8_t> Bytes = *Val.getAsBlock();
1092   for (auto Byte : Bytes)
1093     Attr->addValue(static_cast<dwarf::Attribute>(0), dwarf::DW_FORM_data1,
1094                    new (DIEAlloc) DIEInteger(Byte));
1095   // FIXME: If DIEBlock and DIELoc just reuses the Size field of
1096   // the DIE class, this if could be replaced by
1097   // Attr->setSize(Bytes.size()).
1098   if (Streamer) {
1099     if (Loc)
1100       Loc->ComputeSize(&Streamer->getAsmPrinter());
1101     else
1102       Block->ComputeSize(&Streamer->getAsmPrinter());
1103   }
1104   Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::Form(AttrSpec.Form),
1105                Value);
1106   return AttrSize;
1107 }
1108
1109 /// \brief Clone a scalar attribute  and add it to \p Die.
1110 /// \returns the size of the new attribute.
1111 unsigned DwarfLinker::cloneScalarAttribute(
1112     DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE, const DWARFUnit &U,
1113     AttributeSpec AttrSpec, const DWARFFormValue &Val, unsigned AttrSize) {
1114   uint64_t Value;
1115   if (AttrSpec.Form == dwarf::DW_FORM_sec_offset)
1116     Value = *Val.getAsSectionOffset();
1117   else if (AttrSpec.Form == dwarf::DW_FORM_sdata)
1118     Value = *Val.getAsSignedConstant();
1119   else if (AttrSpec.Form == dwarf::DW_FORM_addr)
1120     Value = *Val.getAsAddress(&U);
1121   else if (auto OptionalValue = Val.getAsUnsignedConstant())
1122     Value = *OptionalValue;
1123   else {
1124     reportWarning("Unsupported scalar attribute form. Dropping attribute.", &U,
1125                   &InputDIE);
1126     return 0;
1127   }
1128   Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::Form(AttrSpec.Form),
1129                new (DIEAlloc) DIEInteger(Value));
1130   return AttrSize;
1131 }
1132
1133 /// \brief Clone \p InputDIE's attribute described by \p AttrSpec with
1134 /// value \p Val, and add it to \p Die.
1135 /// \returns the size of the cloned attribute.
1136 unsigned DwarfLinker::cloneAttribute(DIE &Die,
1137                                      const DWARFDebugInfoEntryMinimal &InputDIE,
1138                                      CompileUnit &Unit,
1139                                      const DWARFFormValue &Val,
1140                                      const AttributeSpec AttrSpec,
1141                                      unsigned AttrSize) {
1142   const DWARFUnit &U = Unit.getOrigUnit();
1143
1144   switch (AttrSpec.Form) {
1145   case dwarf::DW_FORM_strp:
1146   case dwarf::DW_FORM_string:
1147     return cloneStringAttribute(Die, AttrSpec, Val, U);
1148   case dwarf::DW_FORM_ref_addr:
1149   case dwarf::DW_FORM_ref1:
1150   case dwarf::DW_FORM_ref2:
1151   case dwarf::DW_FORM_ref4:
1152   case dwarf::DW_FORM_ref8:
1153     return cloneDieReferenceAttribute(Die, AttrSpec, AttrSize);
1154   case dwarf::DW_FORM_block:
1155   case dwarf::DW_FORM_block1:
1156   case dwarf::DW_FORM_block2:
1157   case dwarf::DW_FORM_block4:
1158   case dwarf::DW_FORM_exprloc:
1159     return cloneBlockAttribute(Die, AttrSpec, Val, AttrSize);
1160   case dwarf::DW_FORM_addr:
1161   case dwarf::DW_FORM_data1:
1162   case dwarf::DW_FORM_data2:
1163   case dwarf::DW_FORM_data4:
1164   case dwarf::DW_FORM_data8:
1165   case dwarf::DW_FORM_udata:
1166   case dwarf::DW_FORM_sdata:
1167   case dwarf::DW_FORM_sec_offset:
1168   case dwarf::DW_FORM_flag:
1169   case dwarf::DW_FORM_flag_present:
1170     return cloneScalarAttribute(Die, InputDIE, U, AttrSpec, Val, AttrSize);
1171   default:
1172     reportWarning("Unsupported attribute form in cloneAttribute. Dropping.", &U,
1173                   &InputDIE);
1174   }
1175
1176   return 0;
1177 }
1178
1179 /// \brief Recursively clone \p InputDIE's subtrees that have been
1180 /// selected to appear in the linked output.
1181 ///
1182 /// \param OutOffset is the Offset where the newly created DIE will
1183 /// lie in the linked compile unit.
1184 ///
1185 /// \returns the cloned DIE object or null if nothing was selected.
1186 DIE *DwarfLinker::cloneDIE(const DWARFDebugInfoEntryMinimal &InputDIE,
1187                            CompileUnit &Unit, uint32_t OutOffset) {
1188   DWARFUnit &U = Unit.getOrigUnit();
1189   unsigned Idx = U.getDIEIndex(&InputDIE);
1190
1191   // Should the DIE appear in the output?
1192   if (!Unit.getInfo(Idx).Keep)
1193     return nullptr;
1194
1195   uint32_t Offset = InputDIE.getOffset();
1196
1197   DIE *Die = new DIE(static_cast<dwarf::Tag>(InputDIE.getTag()));
1198   Die->setOffset(OutOffset);
1199
1200   // Extract and clone every attribute.
1201   DataExtractor Data = U.getDebugInfoExtractor();
1202   const auto *Abbrev = InputDIE.getAbbreviationDeclarationPtr();
1203   Offset += getULEB128Size(Abbrev->getCode());
1204
1205   for (const auto &AttrSpec : Abbrev->attributes()) {
1206     DWARFFormValue Val(AttrSpec.Form);
1207     uint32_t AttrSize = Offset;
1208     Val.extractValue(Data, &Offset, &U);
1209     AttrSize = Offset - AttrSize;
1210
1211     OutOffset += cloneAttribute(*Die, InputDIE, Unit, Val, AttrSpec, AttrSize);
1212   }
1213
1214   DIEAbbrev &NewAbbrev = Die->getAbbrev();
1215   // If a scope DIE is kept, we must have kept at least one child. If
1216   // it's not the case, we'll just be emitting one wasteful end of
1217   // children marker, but things won't break.
1218   if (InputDIE.hasChildren())
1219     NewAbbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
1220   // Assign a permanent abbrev number
1221   AssignAbbrev(Die->getAbbrev());
1222
1223   // Add the size of the abbreviation number to the output offset.
1224   OutOffset += getULEB128Size(Die->getAbbrevNumber());
1225
1226   if (!Abbrev->hasChildren()) {
1227     // Update our size.
1228     Die->setSize(OutOffset - Die->getOffset());
1229     return Die;
1230   }
1231
1232   // Recursively clone children.
1233   for (auto *Child = InputDIE.getFirstChild(); Child && !Child->isNULL();
1234        Child = Child->getSibling()) {
1235     if (DIE *Clone = cloneDIE(*Child, Unit, OutOffset)) {
1236       Die->addChild(std::unique_ptr<DIE>(Clone));
1237       OutOffset = Clone->getOffset() + Clone->getSize();
1238     }
1239   }
1240
1241   // Account for the end of children marker.
1242   OutOffset += sizeof(int8_t);
1243   // Update our size.
1244   Die->setSize(OutOffset - Die->getOffset());
1245   return Die;
1246 }
1247
1248 bool DwarfLinker::link(const DebugMap &Map) {
1249
1250   if (Map.begin() == Map.end()) {
1251     errs() << "Empty debug map.\n";
1252     return false;
1253   }
1254
1255   if (!createStreamer(Map.getTriple(), OutputFilename))
1256     return false;
1257
1258   // Size of the DIEs (and headers) generated for the linked output.
1259   uint64_t OutputDebugInfoSize = 0;
1260
1261   for (const auto &Obj : Map.objects()) {
1262     CurrentDebugObject = Obj.get();
1263
1264     if (Options.Verbose)
1265       outs() << "DEBUG MAP OBJECT: " << Obj->getObjectFilename() << "\n";
1266     auto ErrOrObj = BinHolder.GetObjectFile(Obj->getObjectFilename());
1267     if (std::error_code EC = ErrOrObj.getError()) {
1268       reportWarning(Twine(Obj->getObjectFilename()) + ": " + EC.message());
1269       continue;
1270     }
1271
1272     // Look for relocations that correspond to debug map entries.
1273     if (!findValidRelocsInDebugInfo(*ErrOrObj, *Obj)) {
1274       if (Options.Verbose)
1275         outs() << "No valid relocations found. Skipping.\n";
1276       continue;
1277     }
1278
1279     // Setup access to the debug info.
1280     DWARFContextInMemory DwarfContext(*ErrOrObj);
1281     startDebugObject(DwarfContext);
1282
1283     // In a first phase, just read in the debug info and store the DIE
1284     // parent links that we will use during the next phase.
1285     for (const auto &CU : DwarfContext.compile_units()) {
1286       auto *CUDie = CU->getCompileUnitDIE(false);
1287       if (Options.Verbose) {
1288         outs() << "Input compilation unit:";
1289         CUDie->dump(outs(), CU.get(), 0);
1290       }
1291       Units.emplace_back(*CU);
1292       gatherDIEParents(CUDie, 0, Units.back());
1293     }
1294
1295     // Then mark all the DIEs that need to be present in the linked
1296     // output and collect some information about them. Note that this
1297     // loop can not be merged with the previous one becaue cross-cu
1298     // references require the ParentIdx to be setup for every CU in
1299     // the object file before calling this.
1300     for (auto &CurrentUnit : Units)
1301       lookForDIEsToKeep(*CurrentUnit.getOrigUnit().getCompileUnitDIE(), *Obj,
1302                         CurrentUnit, 0);
1303
1304     // Construct the output DIE tree by cloning the DIEs we chose to
1305     // keep above. If there are no valid relocs, then there's nothing
1306     // to clone/emit.
1307     if (!ValidRelocs.empty())
1308       for (auto &CurrentUnit : Units) {
1309         const auto *InputDIE = CurrentUnit.getOrigUnit().getCompileUnitDIE();
1310         DIE *OutputDIE =
1311             cloneDIE(*InputDIE, CurrentUnit, 11 /* Unit Header size */);
1312         CurrentUnit.setOutputUnitDIE(OutputDIE);
1313         OutputDebugInfoSize = CurrentUnit.computeOffsets(OutputDebugInfoSize);
1314       }
1315
1316     // Emit all the compile unit's debug information.
1317     if (!ValidRelocs.empty() && !Options.NoOutput)
1318       for (auto &CurrentUnit : Units) {
1319         Streamer->emitCompileUnitHeader(CurrentUnit);
1320         if (!CurrentUnit.getOutputUnitDIE())
1321           continue;
1322         Streamer->emitDIE(*CurrentUnit.getOutputUnitDIE());
1323       }
1324
1325     // Clean-up before starting working on the next object.
1326     endDebugObject();
1327   }
1328
1329   // Emit everything that's global.
1330   if (!Options.NoOutput) {
1331     Streamer->emitAbbrevs(Abbreviations);
1332     Streamer->emitStrings(StringPool);
1333   }
1334
1335   return Options.NoOutput ? true : Streamer->finish();
1336 }
1337 }
1338
1339 bool linkDwarf(StringRef OutputFilename, const DebugMap &DM,
1340                const LinkOptions &Options) {
1341   DwarfLinker Linker(OutputFilename, Options);
1342   return Linker.link(DM);
1343 }
1344 }
1345 }