[dsymutil] Add the DwarfStreamer class.
[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/DebugInfo/DWARF/DWARFContext.h"
15 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
16 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
17 #include "llvm/MC/MCAsmBackend.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCInstrInfo.h"
22 #include "llvm/MC/MCObjectFileInfo.h"
23 #include "llvm/MC/MCRegisterInfo.h"
24 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/Object/MachO.h"
26 #include "llvm/Support/Dwarf.h"
27 #include "llvm/Support/LEB128.h"
28 #include "llvm/Support/TargetRegistry.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include "llvm/Target/TargetOptions.h"
31 #include <string>
32
33 namespace llvm {
34 namespace dsymutil {
35
36 namespace {
37
38 void warn(const Twine &Warning, const Twine &Context) {
39   errs() << Twine("while processing ") + Context + ":\n";
40   errs() << Twine("warning: ") + Warning + "\n";
41 }
42
43 bool error(const Twine &Error, const Twine &Context) {
44   errs() << Twine("while processing ") + Context + ":\n";
45   errs() << Twine("error: ") + Error + "\n";
46   return false;
47 }
48
49 /// \brief Stores all information relating to a compile unit, be it in
50 /// its original instance in the object file to its brand new cloned
51 /// and linked DIE tree.
52 class CompileUnit {
53 public:
54   /// \brief Information gathered about a DIE in the object file.
55   struct DIEInfo {
56     uint64_t Address;   ///< Linked address of the described entity.
57     uint32_t ParentIdx; ///< The index of this DIE's parent.
58     bool Keep;          ///< Is the DIE part of the linked output?
59     bool InDebugMap;    ///< Was this DIE's entity found in the map?
60   };
61
62   CompileUnit(DWARFUnit &OrigUnit) : OrigUnit(OrigUnit) {
63     Info.resize(OrigUnit.getNumDIEs());
64   }
65
66   DWARFUnit &getOrigUnit() const { return OrigUnit; }
67
68   DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; }
69   const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; }
70
71 private:
72   DWARFUnit &OrigUnit;
73   std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index.
74 };
75
76 /// \brief The Dwarf streaming logic
77 ///
78 /// All interactions with the MC layer that is used to build the debug
79 /// information binary representation are handled in this class.
80 class DwarfStreamer {
81   /// \defgroup MCObjects MC layer objects constructed by the streamer
82   /// @{
83   std::unique_ptr<MCRegisterInfo> MRI;
84   std::unique_ptr<MCAsmInfo> MAI;
85   std::unique_ptr<MCObjectFileInfo> MOFI;
86   std::unique_ptr<MCContext> MC;
87   MCAsmBackend *MAB; // Owned by MCStreamer
88   std::unique_ptr<MCInstrInfo> MII;
89   std::unique_ptr<MCSubtargetInfo> MSTI;
90   MCCodeEmitter *MCE; // Owned by MCStreamer
91   MCStreamer *MS;     // Owned by AsmPrinter
92   std::unique_ptr<TargetMachine> TM;
93   std::unique_ptr<AsmPrinter> Asm;
94   /// @}
95
96   /// \brief the file we stream the linked Dwarf to.
97   std::unique_ptr<raw_fd_ostream> OutFile;
98
99 public:
100   /// \brief Actually create the streamer and the ouptut file.
101   ///
102   /// This could be done directly in the constructor, but it feels
103   /// more natural to handle errors through return value.
104   bool init(Triple TheTriple, StringRef OutputFilename);
105
106   ///\brief Dump the file to the disk.
107   bool finish();
108 };
109
110 bool DwarfStreamer::init(Triple TheTriple, StringRef OutputFilename) {
111   std::string ErrorStr;
112   std::string TripleName;
113   StringRef Context = "dwarf streamer init";
114
115   // Get the target.
116   const Target *TheTarget =
117       TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr);
118   if (!TheTarget)
119     return error(ErrorStr, Context);
120   TripleName = TheTriple.getTriple();
121
122   // Create all the MC Objects.
123   MRI.reset(TheTarget->createMCRegInfo(TripleName));
124   if (!MRI)
125     return error(Twine("no register info for target ") + TripleName, Context);
126
127   MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName));
128   if (!MAI)
129     return error("no asm info for target " + TripleName, Context);
130
131   MOFI.reset(new MCObjectFileInfo);
132   MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get()));
133   MOFI->InitMCObjectFileInfo(TripleName, Reloc::Default, CodeModel::Default,
134                              *MC);
135
136   MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
137   if (!MAB)
138     return error("no asm backend for target " + TripleName, Context);
139
140   MII.reset(TheTarget->createMCInstrInfo());
141   if (!MII)
142     return error("no instr info info for target " + TripleName, Context);
143
144   MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
145   if (!MSTI)
146     return error("no subtarget info for target " + TripleName, Context);
147
148   MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MSTI, *MC);
149   if (!MCE)
150     return error("no code emitter for target " + TripleName, Context);
151
152   // Create the output file.
153   std::error_code EC;
154   OutFile = make_unique<raw_fd_ostream>(OutputFilename, EC, sys::fs::F_None);
155   if (EC)
156     return error(Twine(OutputFilename) + ": " + EC.message(), Context);
157
158   MS = TheTarget->createMCObjectStreamer(TripleName, *MC, *MAB, *OutFile, MCE,
159                                          *MSTI, false);
160   if (!MS)
161     return error("no object streamer for target " + TripleName, Context);
162
163   // Finally create the AsmPrinter we'll use to emit the DIEs.
164   TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions()));
165   if (!TM)
166     return error("no target machine for target " + TripleName, Context);
167
168   Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS)));
169   if (!Asm)
170     return error("no asm printer for target " + TripleName, Context);
171
172   return true;
173 }
174
175 bool DwarfStreamer::finish() {
176   MS->Finish();
177   return true;
178 }
179
180 /// \brief The core of the Dwarf linking logic.
181 ///
182 /// The link of the dwarf information from the object files will be
183 /// driven by the selection of 'root DIEs', which are DIEs that
184 /// describe variables or functions that are present in the linked
185 /// binary (and thus have entries in the debug map). All the debug
186 /// information that will be linked (the DIEs, but also the line
187 /// tables, ranges, ...) is derived from that set of root DIEs.
188 ///
189 /// The root DIEs are identified because they contain relocations that
190 /// correspond to a debug map entry at specific places (the low_pc for
191 /// a function, the location for a variable). These relocations are
192 /// called ValidRelocs in the DwarfLinker and are gathered as a very
193 /// first step when we start processing a DebugMapObject.
194 class DwarfLinker {
195 public:
196   DwarfLinker(StringRef OutputFilename, const LinkOptions &Options)
197       : OutputFilename(OutputFilename), Options(Options),
198         BinHolder(Options.Verbose) {}
199
200   /// \brief Link the contents of the DebugMap.
201   bool link(const DebugMap &);
202
203 private:
204   /// \brief Called at the start of a debug object link.
205   void startDebugObject(DWARFContext &);
206
207   /// \brief Called at the end of a debug object link.
208   void endDebugObject();
209
210   /// \defgroup FindValidRelocations Translate debug map into a list
211   /// of relevant relocations
212   ///
213   /// @{
214   struct ValidReloc {
215     uint32_t Offset;
216     uint32_t Size;
217     uint64_t Addend;
218     const DebugMapObject::DebugMapEntry *Mapping;
219
220     ValidReloc(uint32_t Offset, uint32_t Size, uint64_t Addend,
221                const DebugMapObject::DebugMapEntry *Mapping)
222         : Offset(Offset), Size(Size), Addend(Addend), Mapping(Mapping) {}
223
224     bool operator<(const ValidReloc &RHS) const { return Offset < RHS.Offset; }
225   };
226
227   /// \brief The valid relocations for the current DebugMapObject.
228   /// This vector is sorted by relocation offset.
229   std::vector<ValidReloc> ValidRelocs;
230
231   /// \brief Index into ValidRelocs of the next relocation to
232   /// consider. As we walk the DIEs in acsending file offset and as
233   /// ValidRelocs is sorted by file offset, keeping this index
234   /// uptodate is all we have to do to have a cheap lookup during the
235   /// root DIE selection.
236   unsigned NextValidReloc;
237
238   bool findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
239                                   const DebugMapObject &DMO);
240
241   bool findValidRelocs(const object::SectionRef &Section,
242                        const object::ObjectFile &Obj,
243                        const DebugMapObject &DMO);
244
245   void findValidRelocsMachO(const object::SectionRef &Section,
246                             const object::MachOObjectFile &Obj,
247                             const DebugMapObject &DMO);
248   /// @}
249
250   /// \defgroup FindRootDIEs Find DIEs corresponding to debug map entries.
251   ///
252   /// @{
253   /// \brief Recursively walk the \p DIE tree and look for DIEs to
254   /// keep. Store that information in \p CU's DIEInfo.
255   void lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE,
256                          const DebugMapObject &DMO, CompileUnit &CU,
257                          unsigned Flags);
258
259   /// \brief Flags passed to DwarfLinker::lookForDIEsToKeep
260   enum TravesalFlags {
261     TF_Keep = 1 << 0,            ///< Mark the traversed DIEs as kept.
262     TF_InFunctionScope = 1 << 1, ///< Current scope is a fucntion scope.
263     TF_DependencyWalk = 1 << 2,  ///< Walking the dependencies of a kept DIE.
264     TF_ParentWalk = 1 << 3,      ///< Walking up the parents of a kept DIE.
265   };
266
267   /// \brief Mark the passed DIE as well as all the ones it depends on
268   /// as kept.
269   void keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE,
270                                CompileUnit::DIEInfo &MyInfo,
271                                const DebugMapObject &DMO, CompileUnit &CU,
272                                unsigned Flags);
273
274   unsigned shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE,
275                          CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
276                          unsigned Flags);
277
278   unsigned shouldKeepVariableDIE(const DWARFDebugInfoEntryMinimal &DIE,
279                                  CompileUnit &Unit,
280                                  CompileUnit::DIEInfo &MyInfo, unsigned Flags);
281
282   unsigned shouldKeepSubprogramDIE(const DWARFDebugInfoEntryMinimal &DIE,
283                                    CompileUnit &Unit,
284                                    CompileUnit::DIEInfo &MyInfo,
285                                    unsigned Flags);
286
287   bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
288                           CompileUnit::DIEInfo &Info);
289   /// @}
290
291   /// \defgroup Helpers Various helper methods.
292   ///
293   /// @{
294   const DWARFDebugInfoEntryMinimal *
295   resolveDIEReference(DWARFFormValue &RefValue, const DWARFUnit &Unit,
296                       const DWARFDebugInfoEntryMinimal &DIE,
297                       CompileUnit *&ReferencedCU);
298
299   CompileUnit *getUnitForOffset(unsigned Offset);
300
301   void reportWarning(const Twine &Warning, const DWARFUnit *Unit = nullptr,
302                      const DWARFDebugInfoEntryMinimal *DIE = nullptr);
303
304   bool createStreamer(Triple TheTriple, StringRef OutputFilename);
305   /// @}
306
307 private:
308   std::string OutputFilename;
309   LinkOptions Options;
310   BinaryHolder BinHolder;
311   std::unique_ptr<DwarfStreamer> Streamer;
312
313   /// The units of the current debug map object.
314   std::vector<CompileUnit> Units;
315
316   /// The debug map object curently under consideration.
317   DebugMapObject *CurrentDebugObject;
318 };
319
320 /// \brief Similar to DWARFUnitSection::getUnitForOffset(), but
321 /// returning our CompileUnit object instead.
322 CompileUnit *DwarfLinker::getUnitForOffset(unsigned Offset) {
323   auto CU =
324       std::upper_bound(Units.begin(), Units.end(), Offset,
325                        [](uint32_t LHS, const CompileUnit &RHS) {
326                          return LHS < RHS.getOrigUnit().getNextUnitOffset();
327                        });
328   return CU != Units.end() ? &*CU : nullptr;
329 }
330
331 /// \brief Resolve the DIE attribute reference that has been
332 /// extracted in \p RefValue. The resulting DIE migh be in another
333 /// CompileUnit which is stored into \p ReferencedCU.
334 /// \returns null if resolving fails for any reason.
335 const DWARFDebugInfoEntryMinimal *DwarfLinker::resolveDIEReference(
336     DWARFFormValue &RefValue, const DWARFUnit &Unit,
337     const DWARFDebugInfoEntryMinimal &DIE, CompileUnit *&RefCU) {
338   assert(RefValue.isFormClass(DWARFFormValue::FC_Reference));
339   uint64_t RefOffset = *RefValue.getAsReference(&Unit);
340
341   if ((RefCU = getUnitForOffset(RefOffset)))
342     if (const auto *RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset))
343       return RefDie;
344
345   reportWarning("could not find referenced DIE", &Unit, &DIE);
346   return nullptr;
347 }
348
349 /// \brief Report a warning to the user, optionaly including
350 /// information about a specific \p DIE related to the warning.
351 void DwarfLinker::reportWarning(const Twine &Warning, const DWARFUnit *Unit,
352                                 const DWARFDebugInfoEntryMinimal *DIE) {
353   StringRef Context = "<debug map>";
354   if (CurrentDebugObject)
355     Context = CurrentDebugObject->getObjectFilename();
356   warn(Warning, Context);
357
358   if (!Options.Verbose || !DIE)
359     return;
360
361   errs() << "    in DIE:\n";
362   DIE->dump(errs(), const_cast<DWARFUnit *>(Unit), 0 /* RecurseDepth */,
363             6 /* Indent */);
364 }
365
366 bool DwarfLinker::createStreamer(Triple TheTriple, StringRef OutputFilename) {
367   if (Options.NoOutput)
368     return true;
369
370   Streamer = make_unique<DwarfStreamer>();
371   return Streamer->init(TheTriple, OutputFilename);
372 }
373
374 /// \brief Recursive helper to gather the child->parent relationships in the
375 /// original compile unit.
376 static void gatherDIEParents(const DWARFDebugInfoEntryMinimal *DIE,
377                              unsigned ParentIdx, CompileUnit &CU) {
378   unsigned MyIdx = CU.getOrigUnit().getDIEIndex(DIE);
379   CU.getInfo(MyIdx).ParentIdx = ParentIdx;
380
381   if (DIE->hasChildren())
382     for (auto *Child = DIE->getFirstChild(); Child && !Child->isNULL();
383          Child = Child->getSibling())
384       gatherDIEParents(Child, MyIdx, CU);
385 }
386
387 static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) {
388   switch (Tag) {
389   default:
390     return false;
391   case dwarf::DW_TAG_subprogram:
392   case dwarf::DW_TAG_lexical_block:
393   case dwarf::DW_TAG_subroutine_type:
394   case dwarf::DW_TAG_structure_type:
395   case dwarf::DW_TAG_class_type:
396   case dwarf::DW_TAG_union_type:
397     return true;
398   }
399   llvm_unreachable("Invalid Tag");
400 }
401
402 void DwarfLinker::startDebugObject(DWARFContext &Dwarf) {
403   Units.reserve(Dwarf.getNumCompileUnits());
404   NextValidReloc = 0;
405 }
406
407 void DwarfLinker::endDebugObject() {
408   Units.clear();
409   ValidRelocs.clear();
410 }
411
412 /// \brief Iterate over the relocations of the given \p Section and
413 /// store the ones that correspond to debug map entries into the
414 /// ValidRelocs array.
415 void DwarfLinker::findValidRelocsMachO(const object::SectionRef &Section,
416                                        const object::MachOObjectFile &Obj,
417                                        const DebugMapObject &DMO) {
418   StringRef Contents;
419   Section.getContents(Contents);
420   DataExtractor Data(Contents, Obj.isLittleEndian(), 0);
421
422   for (const object::RelocationRef &Reloc : Section.relocations()) {
423     object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl();
424     MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef);
425     unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc);
426     uint64_t Offset64;
427     if ((RelocSize != 4 && RelocSize != 8) || Reloc.getOffset(Offset64)) {
428       reportWarning(" unsupported relocation in debug_info section.");
429       continue;
430     }
431     uint32_t Offset = Offset64;
432     // Mach-o uses REL relocations, the addend is at the relocation offset.
433     uint64_t Addend = Data.getUnsigned(&Offset, RelocSize);
434
435     auto Sym = Reloc.getSymbol();
436     if (Sym != Obj.symbol_end()) {
437       StringRef SymbolName;
438       if (Sym->getName(SymbolName)) {
439         reportWarning("error getting relocation symbol name.");
440         continue;
441       }
442       if (const auto *Mapping = DMO.lookupSymbol(SymbolName))
443         ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping);
444     } else if (const auto *Mapping = DMO.lookupObjectAddress(Addend)) {
445       // Do not store the addend. The addend was the address of the
446       // symbol in the object file, the address in the binary that is
447       // stored in the debug map doesn't need to be offseted.
448       ValidRelocs.emplace_back(Offset64, RelocSize, 0, Mapping);
449     }
450   }
451 }
452
453 /// \brief Dispatch the valid relocation finding logic to the
454 /// appropriate handler depending on the object file format.
455 bool DwarfLinker::findValidRelocs(const object::SectionRef &Section,
456                                   const object::ObjectFile &Obj,
457                                   const DebugMapObject &DMO) {
458   // Dispatch to the right handler depending on the file type.
459   if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj))
460     findValidRelocsMachO(Section, *MachOObj, DMO);
461   else
462     reportWarning(Twine("unsupported object file type: ") + Obj.getFileName());
463
464   if (ValidRelocs.empty())
465     return false;
466
467   // Sort the relocations by offset. We will walk the DIEs linearly in
468   // the file, this allows us to just keep an index in the relocation
469   // array that we advance during our walk, rather than resorting to
470   // some associative container. See DwarfLinker::NextValidReloc.
471   std::sort(ValidRelocs.begin(), ValidRelocs.end());
472   return true;
473 }
474
475 /// \brief Look for relocations in the debug_info section that match
476 /// entries in the debug map. These relocations will drive the Dwarf
477 /// link by indicating which DIEs refer to symbols present in the
478 /// linked binary.
479 /// \returns wether there are any valid relocations in the debug info.
480 bool DwarfLinker::findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
481                                              const DebugMapObject &DMO) {
482   // Find the debug_info section.
483   for (const object::SectionRef &Section : Obj.sections()) {
484     StringRef SectionName;
485     Section.getName(SectionName);
486     SectionName = SectionName.substr(SectionName.find_first_not_of("._"));
487     if (SectionName != "debug_info")
488       continue;
489     return findValidRelocs(Section, Obj, DMO);
490   }
491   return false;
492 }
493
494 /// \brief Checks that there is a relocation against an actual debug
495 /// map entry between \p StartOffset and \p NextOffset.
496 ///
497 /// This function must be called with offsets in strictly ascending
498 /// order because it never looks back at relocations it already 'went past'.
499 /// \returns true and sets Info.InDebugMap if it is the case.
500 bool DwarfLinker::hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
501                                      CompileUnit::DIEInfo &Info) {
502   assert(NextValidReloc == 0 ||
503          StartOffset > ValidRelocs[NextValidReloc - 1].Offset);
504   if (NextValidReloc >= ValidRelocs.size())
505     return false;
506
507   uint64_t RelocOffset = ValidRelocs[NextValidReloc].Offset;
508
509   // We might need to skip some relocs that we didn't consider. For
510   // example the high_pc of a discarded DIE might contain a reloc that
511   // is in the list because it actually corresponds to the start of a
512   // function that is in the debug map.
513   while (RelocOffset < StartOffset && NextValidReloc < ValidRelocs.size() - 1)
514     RelocOffset = ValidRelocs[++NextValidReloc].Offset;
515
516   if (RelocOffset < StartOffset || RelocOffset >= EndOffset)
517     return false;
518
519   const auto &ValidReloc = ValidRelocs[NextValidReloc++];
520   if (Options.Verbose)
521     outs() << "Found valid debug map entry: " << ValidReloc.Mapping->getKey()
522            << " " << format("\t%016" PRIx64 " => %016" PRIx64,
523                             ValidReloc.Mapping->getValue().ObjectAddress,
524                             ValidReloc.Mapping->getValue().BinaryAddress);
525
526   Info.Address =
527       ValidReloc.Mapping->getValue().BinaryAddress + ValidReloc.Addend;
528   Info.InDebugMap = true;
529   return true;
530 }
531
532 /// \brief Get the starting and ending (exclusive) offset for the
533 /// attribute with index \p Idx descibed by \p Abbrev. \p Offset is
534 /// supposed to point to the position of the first attribute described
535 /// by \p Abbrev.
536 /// \return [StartOffset, EndOffset) as a pair.
537 static std::pair<uint32_t, uint32_t>
538 getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx,
539                     unsigned Offset, const DWARFUnit &Unit) {
540   DataExtractor Data = Unit.getDebugInfoExtractor();
541
542   for (unsigned i = 0; i < Idx; ++i)
543     DWARFFormValue::skipValue(Abbrev->getFormByIndex(i), Data, &Offset, &Unit);
544
545   uint32_t End = Offset;
546   DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End, &Unit);
547
548   return std::make_pair(Offset, End);
549 }
550
551 /// \brief Check if a variable describing DIE should be kept.
552 /// \returns updated TraversalFlags.
553 unsigned DwarfLinker::shouldKeepVariableDIE(
554     const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit,
555     CompileUnit::DIEInfo &MyInfo, unsigned Flags) {
556   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
557
558   // Global variables with constant value can always be kept.
559   if (!(Flags & TF_InFunctionScope) &&
560       Abbrev->findAttributeIndex(dwarf::DW_AT_const_value) != -1U) {
561     MyInfo.InDebugMap = true;
562     return Flags | TF_Keep;
563   }
564
565   uint32_t LocationIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_location);
566   if (LocationIdx == -1U)
567     return Flags;
568
569   uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
570   const DWARFUnit &OrigUnit = Unit.getOrigUnit();
571   uint32_t LocationOffset, LocationEndOffset;
572   std::tie(LocationOffset, LocationEndOffset) =
573       getAttributeOffsets(Abbrev, LocationIdx, Offset, OrigUnit);
574
575   // See if there is a relocation to a valid debug map entry inside
576   // this variable's location. The order is important here. We want to
577   // always check in the variable has a valid relocation, so that the
578   // DIEInfo is filled. However, we don't want a static variable in a
579   // function to force us to keep the enclosing function.
580   if (!hasValidRelocation(LocationOffset, LocationEndOffset, MyInfo) ||
581       (Flags & TF_InFunctionScope))
582     return Flags;
583
584   if (Options.Verbose)
585     DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */);
586
587   return Flags | TF_Keep;
588 }
589
590 /// \brief Check if a function describing DIE should be kept.
591 /// \returns updated TraversalFlags.
592 unsigned DwarfLinker::shouldKeepSubprogramDIE(
593     const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit,
594     CompileUnit::DIEInfo &MyInfo, unsigned Flags) {
595   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
596
597   Flags |= TF_InFunctionScope;
598
599   uint32_t LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc);
600   if (LowPcIdx == -1U)
601     return Flags;
602
603   uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
604   const DWARFUnit &OrigUnit = Unit.getOrigUnit();
605   uint32_t LowPcOffset, LowPcEndOffset;
606   std::tie(LowPcOffset, LowPcEndOffset) =
607       getAttributeOffsets(Abbrev, LowPcIdx, Offset, OrigUnit);
608
609   uint64_t LowPc =
610       DIE.getAttributeValueAsAddress(&OrigUnit, dwarf::DW_AT_low_pc, -1ULL);
611   assert(LowPc != -1ULL && "low_pc attribute is not an address.");
612   if (LowPc == -1ULL ||
613       !hasValidRelocation(LowPcOffset, LowPcEndOffset, MyInfo))
614     return Flags;
615
616   if (Options.Verbose)
617     DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */);
618
619   return Flags | TF_Keep;
620 }
621
622 /// \brief Check if a DIE should be kept.
623 /// \returns updated TraversalFlags.
624 unsigned DwarfLinker::shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE,
625                                     CompileUnit &Unit,
626                                     CompileUnit::DIEInfo &MyInfo,
627                                     unsigned Flags) {
628   switch (DIE.getTag()) {
629   case dwarf::DW_TAG_constant:
630   case dwarf::DW_TAG_variable:
631     return shouldKeepVariableDIE(DIE, Unit, MyInfo, Flags);
632   case dwarf::DW_TAG_subprogram:
633     return shouldKeepSubprogramDIE(DIE, Unit, MyInfo, Flags);
634   case dwarf::DW_TAG_module:
635   case dwarf::DW_TAG_imported_module:
636   case dwarf::DW_TAG_imported_declaration:
637   case dwarf::DW_TAG_imported_unit:
638     // We always want to keep these.
639     return Flags | TF_Keep;
640   }
641
642   return Flags;
643 }
644
645 /// \brief Mark the passed DIE as well as all the ones it depends on
646 /// as kept.
647 ///
648 /// This function is called by lookForDIEsToKeep on DIEs that are
649 /// newly discovered to be needed in the link. It recursively calls
650 /// back to lookForDIEsToKeep while adding TF_DependencyWalk to the
651 /// TraversalFlags to inform it that it's not doing the primary DIE
652 /// tree walk.
653 void DwarfLinker::keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE,
654                                           CompileUnit::DIEInfo &MyInfo,
655                                           const DebugMapObject &DMO,
656                                           CompileUnit &CU, unsigned Flags) {
657   const DWARFUnit &Unit = CU.getOrigUnit();
658   MyInfo.Keep = true;
659
660   // First mark all the parent chain as kept.
661   unsigned AncestorIdx = MyInfo.ParentIdx;
662   while (!CU.getInfo(AncestorIdx).Keep) {
663     lookForDIEsToKeep(*Unit.getDIEAtIndex(AncestorIdx), DMO, CU,
664                       TF_ParentWalk | TF_Keep | TF_DependencyWalk);
665     AncestorIdx = CU.getInfo(AncestorIdx).ParentIdx;
666   }
667
668   // Then we need to mark all the DIEs referenced by this DIE's
669   // attributes as kept.
670   DataExtractor Data = Unit.getDebugInfoExtractor();
671   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
672   uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
673
674   // Mark all DIEs referenced through atttributes as kept.
675   for (const auto &AttrSpec : Abbrev->attributes()) {
676     DWARFFormValue Val(AttrSpec.Form);
677
678     if (!Val.isFormClass(DWARFFormValue::FC_Reference)) {
679       DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset, &Unit);
680       continue;
681     }
682
683     Val.extractValue(Data, &Offset, &Unit);
684     CompileUnit *ReferencedCU;
685     if (const auto *RefDIE = resolveDIEReference(Val, Unit, DIE, ReferencedCU))
686       lookForDIEsToKeep(*RefDIE, DMO, *ReferencedCU,
687                         TF_Keep | TF_DependencyWalk);
688   }
689 }
690
691 /// \brief Recursively walk the \p DIE tree and look for DIEs to
692 /// keep. Store that information in \p CU's DIEInfo.
693 ///
694 /// This function is the entry point of the DIE selection
695 /// algorithm. It is expected to walk the DIE tree in file order and
696 /// (though the mediation of its helper) call hasValidRelocation() on
697 /// each DIE that might be a 'root DIE' (See DwarfLinker class
698 /// comment).
699 /// While walking the dependencies of root DIEs, this function is
700 /// also called, but during these dependency walks the file order is
701 /// not respected. The TF_DependencyWalk flag tells us which kind of
702 /// traversal we are currently doing.
703 void DwarfLinker::lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE,
704                                     const DebugMapObject &DMO, CompileUnit &CU,
705                                     unsigned Flags) {
706   unsigned Idx = CU.getOrigUnit().getDIEIndex(&DIE);
707   CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
708   bool AlreadyKept = MyInfo.Keep;
709
710   // If the Keep flag is set, we are marking a required DIE's
711   // dependencies. If our target is already marked as kept, we're all
712   // set.
713   if ((Flags & TF_DependencyWalk) && AlreadyKept)
714     return;
715
716   // We must not call shouldKeepDIE while called from keepDIEAndDenpendencies,
717   // because it would screw up the relocation finding logic.
718   if (!(Flags & TF_DependencyWalk))
719     Flags = shouldKeepDIE(DIE, CU, MyInfo, Flags);
720
721   // If it is a newly kept DIE mark it as well as all its dependencies as kept.
722   if (!AlreadyKept && (Flags & TF_Keep))
723     keepDIEAndDenpendencies(DIE, MyInfo, DMO, CU, Flags);
724
725   // The TF_ParentWalk flag tells us that we are currently walking up
726   // the parent chain of a required DIE, and we don't want to mark all
727   // the children of the parents as kept (consider for example a
728   // DW_TAG_namespace node in the parent chain). There are however a
729   // set of DIE types for which we want to ignore that directive and still
730   // walk their children.
731   if (dieNeedsChildrenToBeMeaningful(DIE.getTag()))
732     Flags &= ~TF_ParentWalk;
733
734   if (!DIE.hasChildren() || (Flags & TF_ParentWalk))
735     return;
736
737   for (auto *Child = DIE.getFirstChild(); Child && !Child->isNULL();
738        Child = Child->getSibling())
739     lookForDIEsToKeep(*Child, DMO, CU, Flags);
740 }
741
742 bool DwarfLinker::link(const DebugMap &Map) {
743
744   if (Map.begin() == Map.end()) {
745     errs() << "Empty debug map.\n";
746     return false;
747   }
748
749   if (!createStreamer(Map.getTriple(), OutputFilename))
750     return false;
751
752   for (const auto &Obj : Map.objects()) {
753     CurrentDebugObject = Obj.get();
754
755     if (Options.Verbose)
756       outs() << "DEBUG MAP OBJECT: " << Obj->getObjectFilename() << "\n";
757     auto ErrOrObj = BinHolder.GetObjectFile(Obj->getObjectFilename());
758     if (std::error_code EC = ErrOrObj.getError()) {
759       reportWarning(Twine(Obj->getObjectFilename()) + ": " + EC.message());
760       continue;
761     }
762
763     // Look for relocations that correspond to debug map entries.
764     if (!findValidRelocsInDebugInfo(*ErrOrObj, *Obj)) {
765       if (Options.Verbose)
766         outs() << "No valid relocations found. Skipping.\n";
767       continue;
768     }
769
770     // Setup access to the debug info.
771     DWARFContextInMemory DwarfContext(*ErrOrObj);
772     startDebugObject(DwarfContext);
773
774     // In a first phase, just read in the debug info and store the DIE
775     // parent links that we will use during the next phase.
776     for (const auto &CU : DwarfContext.compile_units()) {
777       auto *CUDie = CU->getCompileUnitDIE(false);
778       if (Options.Verbose) {
779         outs() << "Input compilation unit:";
780         CUDie->dump(outs(), CU.get(), 0);
781       }
782       Units.emplace_back(*CU);
783       gatherDIEParents(CUDie, 0, Units.back());
784     }
785
786     // Then mark all the DIEs that need to be present in the linked
787     // output and collect some information about them. Note that this
788     // loop can not be merged with the previous one becaue cross-cu
789     // references require the ParentIdx to be setup for every CU in
790     // the object file before calling this.
791     for (auto &CurrentUnit : Units)
792       lookForDIEsToKeep(*CurrentUnit.getOrigUnit().getCompileUnitDIE(), *Obj,
793                         CurrentUnit, 0);
794
795     // Clean-up before starting working on the next object.
796     endDebugObject();
797   }
798
799   return Options.NoOutput ? true : Streamer->finish();
800 }
801 }
802
803 bool linkDwarf(StringRef OutputFilename, const DebugMap &DM,
804                const LinkOptions &Options) {
805   DwarfLinker Linker(OutputFilename, Options);
806   return Linker.link(DM);
807 }
808 }
809 }