1 //===- tools/dsymutil/DwarfLinker.cpp - Dwarf debug info linker -----------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "BinaryHolder.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"
38 void warn(const Twine &Warning, const Twine &Context) {
39 errs() << Twine("while processing ") + Context + ":\n";
40 errs() << Twine("warning: ") + Warning + "\n";
43 bool error(const Twine &Error, const Twine &Context) {
44 errs() << Twine("while processing ") + Context + ":\n";
45 errs() << Twine("error: ") + Error + "\n";
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.
54 /// \brief Information gathered about a DIE in the object file.
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?
62 CompileUnit(DWARFUnit &OrigUnit) : OrigUnit(OrigUnit) {
63 Info.resize(OrigUnit.getNumDIEs());
66 DWARFUnit &getOrigUnit() const { return OrigUnit; }
68 DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; }
69 const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; }
73 std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index.
76 /// \brief The Dwarf streaming logic
78 /// All interactions with the MC layer that is used to build the debug
79 /// information binary representation are handled in this class.
81 /// \defgroup MCObjects MC layer objects constructed by the streamer
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;
96 /// \brief the file we stream the linked Dwarf to.
97 std::unique_ptr<raw_fd_ostream> OutFile;
100 /// \brief Actually create the streamer and the ouptut file.
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);
106 ///\brief Dump the file to the disk.
110 bool DwarfStreamer::init(Triple TheTriple, StringRef OutputFilename) {
111 std::string ErrorStr;
112 std::string TripleName;
113 StringRef Context = "dwarf streamer init";
116 const Target *TheTarget =
117 TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr);
119 return error(ErrorStr, Context);
120 TripleName = TheTriple.getTriple();
122 // Create all the MC Objects.
123 MRI.reset(TheTarget->createMCRegInfo(TripleName));
125 return error(Twine("no register info for target ") + TripleName, Context);
127 MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName));
129 return error("no asm info for target " + TripleName, Context);
131 MOFI.reset(new MCObjectFileInfo);
132 MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get()));
133 MOFI->InitMCObjectFileInfo(TripleName, Reloc::Default, CodeModel::Default,
136 MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
138 return error("no asm backend for target " + TripleName, Context);
140 MII.reset(TheTarget->createMCInstrInfo());
142 return error("no instr info info for target " + TripleName, Context);
144 MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
146 return error("no subtarget info for target " + TripleName, Context);
148 MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MSTI, *MC);
150 return error("no code emitter for target " + TripleName, Context);
152 // Create the output file.
154 OutFile = make_unique<raw_fd_ostream>(OutputFilename, EC, sys::fs::F_None);
156 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
158 MS = TheTarget->createMCObjectStreamer(TripleName, *MC, *MAB, *OutFile, MCE,
161 return error("no object streamer for target " + TripleName, Context);
163 // Finally create the AsmPrinter we'll use to emit the DIEs.
164 TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions()));
166 return error("no target machine for target " + TripleName, Context);
168 Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS)));
170 return error("no asm printer for target " + TripleName, Context);
175 bool DwarfStreamer::finish() {
180 /// \brief The core of the Dwarf linking logic.
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.
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.
196 DwarfLinker(StringRef OutputFilename, const LinkOptions &Options)
197 : OutputFilename(OutputFilename), Options(Options),
198 BinHolder(Options.Verbose) {}
200 /// \brief Link the contents of the DebugMap.
201 bool link(const DebugMap &);
204 /// \brief Called at the start of a debug object link.
205 void startDebugObject(DWARFContext &);
207 /// \brief Called at the end of a debug object link.
208 void endDebugObject();
210 /// \defgroup FindValidRelocations Translate debug map into a list
211 /// of relevant relocations
218 const DebugMapObject::DebugMapEntry *Mapping;
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) {}
224 bool operator<(const ValidReloc &RHS) const { return Offset < RHS.Offset; }
227 /// \brief The valid relocations for the current DebugMapObject.
228 /// This vector is sorted by relocation offset.
229 std::vector<ValidReloc> ValidRelocs;
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;
238 bool findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
239 const DebugMapObject &DMO);
241 bool findValidRelocs(const object::SectionRef &Section,
242 const object::ObjectFile &Obj,
243 const DebugMapObject &DMO);
245 void findValidRelocsMachO(const object::SectionRef &Section,
246 const object::MachOObjectFile &Obj,
247 const DebugMapObject &DMO);
250 /// \defgroup FindRootDIEs Find DIEs corresponding to debug map entries.
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,
259 /// \brief Flags passed to DwarfLinker::lookForDIEsToKeep
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.
267 /// \brief Mark the passed DIE as well as all the ones it depends on
269 void keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE,
270 CompileUnit::DIEInfo &MyInfo,
271 const DebugMapObject &DMO, CompileUnit &CU,
274 unsigned shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE,
275 CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
278 unsigned shouldKeepVariableDIE(const DWARFDebugInfoEntryMinimal &DIE,
280 CompileUnit::DIEInfo &MyInfo, unsigned Flags);
282 unsigned shouldKeepSubprogramDIE(const DWARFDebugInfoEntryMinimal &DIE,
284 CompileUnit::DIEInfo &MyInfo,
287 bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
288 CompileUnit::DIEInfo &Info);
291 /// \defgroup Helpers Various helper methods.
294 const DWARFDebugInfoEntryMinimal *
295 resolveDIEReference(DWARFFormValue &RefValue, const DWARFUnit &Unit,
296 const DWARFDebugInfoEntryMinimal &DIE,
297 CompileUnit *&ReferencedCU);
299 CompileUnit *getUnitForOffset(unsigned Offset);
301 void reportWarning(const Twine &Warning, const DWARFUnit *Unit = nullptr,
302 const DWARFDebugInfoEntryMinimal *DIE = nullptr);
304 bool createStreamer(Triple TheTriple, StringRef OutputFilename);
308 std::string OutputFilename;
310 BinaryHolder BinHolder;
311 std::unique_ptr<DwarfStreamer> Streamer;
313 /// The units of the current debug map object.
314 std::vector<CompileUnit> Units;
316 /// The debug map object curently under consideration.
317 DebugMapObject *CurrentDebugObject;
320 /// \brief Similar to DWARFUnitSection::getUnitForOffset(), but
321 /// returning our CompileUnit object instead.
322 CompileUnit *DwarfLinker::getUnitForOffset(unsigned Offset) {
324 std::upper_bound(Units.begin(), Units.end(), Offset,
325 [](uint32_t LHS, const CompileUnit &RHS) {
326 return LHS < RHS.getOrigUnit().getNextUnitOffset();
328 return CU != Units.end() ? &*CU : nullptr;
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);
341 if ((RefCU = getUnitForOffset(RefOffset)))
342 if (const auto *RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset))
345 reportWarning("could not find referenced DIE", &Unit, &DIE);
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);
358 if (!Options.Verbose || !DIE)
361 errs() << " in DIE:\n";
362 DIE->dump(errs(), const_cast<DWARFUnit *>(Unit), 0 /* RecurseDepth */,
366 bool DwarfLinker::createStreamer(Triple TheTriple, StringRef OutputFilename) {
367 if (Options.NoOutput)
370 Streamer = make_unique<DwarfStreamer>();
371 return Streamer->init(TheTriple, OutputFilename);
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;
381 if (DIE->hasChildren())
382 for (auto *Child = DIE->getFirstChild(); Child && !Child->isNULL();
383 Child = Child->getSibling())
384 gatherDIEParents(Child, MyIdx, CU);
387 static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) {
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:
399 llvm_unreachable("Invalid Tag");
402 void DwarfLinker::startDebugObject(DWARFContext &Dwarf) {
403 Units.reserve(Dwarf.getNumCompileUnits());
407 void DwarfLinker::endDebugObject() {
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) {
419 Section.getContents(Contents);
420 DataExtractor Data(Contents, Obj.isLittleEndian(), 0);
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);
427 if ((RelocSize != 4 && RelocSize != 8) || Reloc.getOffset(Offset64)) {
428 reportWarning(" unsupported relocation in debug_info section.");
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);
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.");
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);
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);
462 reportWarning(Twine("unsupported object file type: ") + Obj.getFileName());
464 if (ValidRelocs.empty())
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());
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
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")
489 return findValidRelocs(Section, Obj, DMO);
494 /// \brief Checks that there is a relocation against an actual debug
495 /// map entry between \p StartOffset and \p NextOffset.
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())
507 uint64_t RelocOffset = ValidRelocs[NextValidReloc].Offset;
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;
516 if (RelocOffset < StartOffset || RelocOffset >= EndOffset)
519 const auto &ValidReloc = ValidRelocs[NextValidReloc++];
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);
527 ValidReloc.Mapping->getValue().BinaryAddress + ValidReloc.Addend;
528 Info.InDebugMap = true;
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
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();
542 for (unsigned i = 0; i < Idx; ++i)
543 DWARFFormValue::skipValue(Abbrev->getFormByIndex(i), Data, &Offset, &Unit);
545 uint32_t End = Offset;
546 DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End, &Unit);
548 return std::make_pair(Offset, End);
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();
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;
565 uint32_t LocationIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_location);
566 if (LocationIdx == -1U)
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);
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))
585 DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */);
587 return Flags | TF_Keep;
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();
597 Flags |= TF_InFunctionScope;
599 uint32_t LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc);
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);
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))
617 DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */);
619 return Flags | TF_Keep;
622 /// \brief Check if a DIE should be kept.
623 /// \returns updated TraversalFlags.
624 unsigned DwarfLinker::shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE,
626 CompileUnit::DIEInfo &MyInfo,
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;
645 /// \brief Mark the passed DIE as well as all the ones it depends on
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
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();
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;
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());
674 // Mark all DIEs referenced through atttributes as kept.
675 for (const auto &AttrSpec : Abbrev->attributes()) {
676 DWARFFormValue Val(AttrSpec.Form);
678 if (!Val.isFormClass(DWARFFormValue::FC_Reference)) {
679 DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset, &Unit);
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);
691 /// \brief Recursively walk the \p DIE tree and look for DIEs to
692 /// keep. Store that information in \p CU's DIEInfo.
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
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,
706 unsigned Idx = CU.getOrigUnit().getDIEIndex(&DIE);
707 CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
708 bool AlreadyKept = MyInfo.Keep;
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
713 if ((Flags & TF_DependencyWalk) && AlreadyKept)
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);
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);
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;
734 if (!DIE.hasChildren() || (Flags & TF_ParentWalk))
737 for (auto *Child = DIE.getFirstChild(); Child && !Child->isNULL();
738 Child = Child->getSibling())
739 lookForDIEsToKeep(*Child, DMO, CU, Flags);
742 bool DwarfLinker::link(const DebugMap &Map) {
744 if (Map.begin() == Map.end()) {
745 errs() << "Empty debug map.\n";
749 if (!createStreamer(Map.getTriple(), OutputFilename))
752 for (const auto &Obj : Map.objects()) {
753 CurrentDebugObject = Obj.get();
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());
763 // Look for relocations that correspond to debug map entries.
764 if (!findValidRelocsInDebugInfo(*ErrOrObj, *Obj)) {
766 outs() << "No valid relocations found. Skipping.\n";
770 // Setup access to the debug info.
771 DWARFContextInMemory DwarfContext(*ErrOrObj);
772 startDebugObject(DwarfContext);
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);
782 Units.emplace_back(*CU);
783 gatherDIEParents(CUDie, 0, Units.back());
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,
795 // Clean-up before starting working on the next object.
799 return Options.NoOutput ? true : Streamer->finish();
803 bool linkDwarf(StringRef OutputFilename, const DebugMap &DM,
804 const LinkOptions &Options) {
805 DwarfLinker Linker(OutputFilename, Options);
806 return Linker.link(DM);