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/DebugInfo/DWARF/DWARFContext.h"
14 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
15 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
16 #include "llvm/Object/MachO.h"
17 #include "llvm/Support/Dwarf.h"
18 #include "llvm/Support/LEB128.h"
26 /// \brief Stores all information relating to a compile unit, be it in
27 /// its original instance in the object file to its brand new cloned
28 /// and linked DIE tree.
31 /// \brief Information gathered about a DIE in the object file.
33 uint64_t Address; ///< Linked address of the described entity.
34 uint32_t ParentIdx; ///< The index of this DIE's parent.
35 bool Keep; ///< Is the DIE part of the linked output?
36 bool InDebugMap; ///< Was this DIE's entity found in the map?
39 CompileUnit(DWARFUnit &OrigUnit) : OrigUnit(OrigUnit) {
40 Info.resize(OrigUnit.getNumDIEs());
43 DWARFUnit &getOrigUnit() const { return OrigUnit; }
45 DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; }
46 const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; }
50 std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index.
53 /// \brief The core of the Dwarf linking logic.
55 /// The link of the dwarf information from the object files will be
56 /// driven by the selection of 'root DIEs', which are DIEs that
57 /// describe variables or functions that are present in the linked
58 /// binary (and thus have entries in the debug map). All the debug
59 /// information that will be linked (the DIEs, but also the line
60 /// tables, ranges, ...) is derived from that set of root DIEs.
62 /// The root DIEs are identified because they contain relocations that
63 /// correspond to a debug map entry at specific places (the low_pc for
64 /// a function, the location for a variable). These relocations are
65 /// called ValidRelocs in the DwarfLinker and are gathered as a very
66 /// first step when we start processing a DebugMapObject.
69 DwarfLinker(StringRef OutputFilename, bool Verbose)
70 : OutputFilename(OutputFilename), Verbose(Verbose), BinHolder(Verbose) {}
72 /// \brief Link the contents of the DebugMap.
73 bool link(const DebugMap &);
76 /// \brief Called at the start of a debug object link.
77 void startDebugObject(DWARFContext &);
79 /// \brief Called at the end of a debug object link.
80 void endDebugObject();
82 /// \defgroup FindValidRelocations Translate debug map into a list
83 /// of relevant relocations
90 const DebugMapObject::DebugMapEntry *Mapping;
92 ValidReloc(uint32_t Offset, uint32_t Size, uint64_t Addend,
93 const DebugMapObject::DebugMapEntry *Mapping)
94 : Offset(Offset), Size(Size), Addend(Addend), Mapping(Mapping) {}
96 bool operator<(const ValidReloc &RHS) const { return Offset < RHS.Offset; }
99 /// \brief The valid relocations for the current DebugMapObject.
100 /// This vector is sorted by relocation offset.
101 std::vector<ValidReloc> ValidRelocs;
103 /// \brief Index into ValidRelocs of the next relocation to
104 /// consider. As we walk the DIEs in acsending file offset and as
105 /// ValidRelocs is sorted by file offset, keeping this index
106 /// uptodate is all we have to do to have a cheap lookup during the
107 /// root DIE selection.
108 unsigned NextValidReloc;
110 bool findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
111 const DebugMapObject &DMO);
113 bool findValidRelocs(const object::SectionRef &Section,
114 const object::ObjectFile &Obj,
115 const DebugMapObject &DMO);
117 void findValidRelocsMachO(const object::SectionRef &Section,
118 const object::MachOObjectFile &Obj,
119 const DebugMapObject &DMO);
122 /// \defgroup FindRootDIEs Find DIEs corresponding to debug map entries.
125 /// \brief Recursively walk the \p DIE tree and look for DIEs to
126 /// keep. Store that information in \p CU's DIEInfo.
127 void lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE,
128 const DebugMapObject &DMO, CompileUnit &CU,
131 /// \brief Flags passed to DwarfLinker::lookForDIEsToKeep
133 TF_Keep = 1 << 0, ///< Mark the traversed DIEs as kept.
134 TF_InFunctionScope = 1 << 1, ///< Current scope is a fucntion scope.
135 TF_DependencyWalk = 1 << 2, ///< Walking the dependencies of a kept DIE.
136 TF_ParentWalk = 1 << 3, ///< Walking up the parents of a kept DIE.
139 /// \brief Mark the passed DIE as well as all the ones it depends on
141 void keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE,
142 CompileUnit::DIEInfo &MyInfo,
143 const DebugMapObject &DMO, CompileUnit &CU,
146 unsigned shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE,
147 CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
150 unsigned shouldKeepVariableDIE(const DWARFDebugInfoEntryMinimal &DIE,
152 CompileUnit::DIEInfo &MyInfo, unsigned Flags);
154 unsigned shouldKeepSubprogramDIE(const DWARFDebugInfoEntryMinimal &DIE,
156 CompileUnit::DIEInfo &MyInfo,
159 bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
160 CompileUnit::DIEInfo &Info);
163 /// \defgroup Helpers Various helper methods.
166 const DWARFDebugInfoEntryMinimal *
167 resolveDIEReference(DWARFFormValue &RefValue, const DWARFUnit &Unit,
168 const DWARFDebugInfoEntryMinimal &DIE,
169 CompileUnit *&ReferencedCU);
171 CompileUnit *getUnitForOffset(unsigned Offset);
173 void reportWarning(const Twine &Warning, const DWARFUnit *Unit = nullptr,
174 const DWARFDebugInfoEntryMinimal *DIE = nullptr);
178 std::string OutputFilename;
180 BinaryHolder BinHolder;
182 /// The units of the current debug map object.
183 std::vector<CompileUnit> Units;
185 /// The debug map object curently under consideration.
186 DebugMapObject *CurrentDebugObject;
189 /// \brief Similar to DWARFUnitSection::getUnitForOffset(), but
190 /// returning our CompileUnit object instead.
191 CompileUnit *DwarfLinker::getUnitForOffset(unsigned Offset) {
193 std::upper_bound(Units.begin(), Units.end(), Offset,
194 [](uint32_t LHS, const CompileUnit &RHS) {
195 return LHS < RHS.getOrigUnit().getNextUnitOffset();
197 return CU != Units.end() ? &*CU : nullptr;
200 /// \brief Resolve the DIE attribute reference that has been
201 /// extracted in \p RefValue. The resulting DIE migh be in another
202 /// CompileUnit which is stored into \p ReferencedCU.
203 /// \returns null if resolving fails for any reason.
204 const DWARFDebugInfoEntryMinimal *DwarfLinker::resolveDIEReference(
205 DWARFFormValue &RefValue, const DWARFUnit &Unit,
206 const DWARFDebugInfoEntryMinimal &DIE, CompileUnit *&RefCU) {
207 assert(RefValue.isFormClass(DWARFFormValue::FC_Reference));
208 uint64_t RefOffset = *RefValue.getAsReference(&Unit);
210 if ((RefCU = getUnitForOffset(RefOffset)))
211 if (const auto *RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset))
214 reportWarning("could not find referenced DIE", &Unit, &DIE);
218 /// \brief Report a warning to the user, optionaly including
219 /// information about a specific \p DIE related to the warning.
220 void DwarfLinker::reportWarning(const Twine &Warning, const DWARFUnit *Unit,
221 const DWARFDebugInfoEntryMinimal *DIE) {
222 if (CurrentDebugObject)
223 errs() << Twine("while processing ") +
224 CurrentDebugObject->getObjectFilename() + ":\n";
225 errs() << Twine("warning: ") + Warning + "\n";
227 if (!Verbose || !DIE)
230 errs() << " in DIE:\n";
231 DIE->dump(errs(), const_cast<DWARFUnit *>(Unit), 0 /* RecurseDepth */,
235 /// \brief Recursive helper to gather the child->parent relationships in the
236 /// original compile unit.
237 static void gatherDIEParents(const DWARFDebugInfoEntryMinimal *DIE,
238 unsigned ParentIdx, CompileUnit &CU) {
239 unsigned MyIdx = CU.getOrigUnit().getDIEIndex(DIE);
240 CU.getInfo(MyIdx).ParentIdx = ParentIdx;
242 if (DIE->hasChildren())
243 for (auto *Child = DIE->getFirstChild(); Child && !Child->isNULL();
244 Child = Child->getSibling())
245 gatherDIEParents(Child, MyIdx, CU);
248 static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) {
252 case dwarf::DW_TAG_subprogram:
253 case dwarf::DW_TAG_lexical_block:
254 case dwarf::DW_TAG_subroutine_type:
255 case dwarf::DW_TAG_structure_type:
256 case dwarf::DW_TAG_class_type:
257 case dwarf::DW_TAG_union_type:
260 llvm_unreachable("Invalid Tag");
263 void DwarfLinker::startDebugObject(DWARFContext &Dwarf) {
264 Units.reserve(Dwarf.getNumCompileUnits());
268 void DwarfLinker::endDebugObject() {
273 /// \brief Iterate over the relocations of the given \p Section and
274 /// store the ones that correspond to debug map entries into the
275 /// ValidRelocs array.
276 void DwarfLinker::findValidRelocsMachO(const object::SectionRef &Section,
277 const object::MachOObjectFile &Obj,
278 const DebugMapObject &DMO) {
280 Section.getContents(Contents);
281 DataExtractor Data(Contents, Obj.isLittleEndian(), 0);
283 for (const object::RelocationRef &Reloc : Section.relocations()) {
284 object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl();
285 MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef);
286 unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc);
288 if ((RelocSize != 4 && RelocSize != 8) || Reloc.getOffset(Offset64)) {
289 reportWarning(" unsupported relocation in debug_info section.");
292 uint32_t Offset = Offset64;
293 // Mach-o uses REL relocations, the addend is at the relocation offset.
294 uint64_t Addend = Data.getUnsigned(&Offset, RelocSize);
296 auto Sym = Reloc.getSymbol();
297 if (Sym != Obj.symbol_end()) {
298 StringRef SymbolName;
299 if (Sym->getName(SymbolName)) {
300 reportWarning("error getting relocation symbol name.");
303 if (const auto *Mapping = DMO.lookupSymbol(SymbolName))
304 ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping);
305 } else if (const auto *Mapping = DMO.lookupObjectAddress(Addend)) {
306 // Do not store the addend. The addend was the address of the
307 // symbol in the object file, the address in the binary that is
308 // stored in the debug map doesn't need to be offseted.
309 ValidRelocs.emplace_back(Offset64, RelocSize, 0, Mapping);
314 /// \brief Dispatch the valid relocation finding logic to the
315 /// appropriate handler depending on the object file format.
316 bool DwarfLinker::findValidRelocs(const object::SectionRef &Section,
317 const object::ObjectFile &Obj,
318 const DebugMapObject &DMO) {
319 // Dispatch to the right handler depending on the file type.
320 if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj))
321 findValidRelocsMachO(Section, *MachOObj, DMO);
323 reportWarning(Twine("unsupported object file type: ") + Obj.getFileName());
325 if (ValidRelocs.empty())
328 // Sort the relocations by offset. We will walk the DIEs linearly in
329 // the file, this allows us to just keep an index in the relocation
330 // array that we advance during our walk, rather than resorting to
331 // some associative container. See DwarfLinker::NextValidReloc.
332 std::sort(ValidRelocs.begin(), ValidRelocs.end());
336 /// \brief Look for relocations in the debug_info section that match
337 /// entries in the debug map. These relocations will drive the Dwarf
338 /// link by indicating which DIEs refer to symbols present in the
340 /// \returns wether there are any valid relocations in the debug info.
341 bool DwarfLinker::findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
342 const DebugMapObject &DMO) {
343 // Find the debug_info section.
344 for (const object::SectionRef &Section : Obj.sections()) {
345 StringRef SectionName;
346 Section.getName(SectionName);
347 SectionName = SectionName.substr(SectionName.find_first_not_of("._"));
348 if (SectionName != "debug_info")
350 return findValidRelocs(Section, Obj, DMO);
355 /// \brief Checks that there is a relocation against an actual debug
356 /// map entry between \p StartOffset and \p NextOffset.
358 /// This function must be called with offsets in strictly ascending
359 /// order because it never looks back at relocations it already 'went past'.
360 /// \returns true and sets Info.InDebugMap if it is the case.
361 bool DwarfLinker::hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
362 CompileUnit::DIEInfo &Info) {
363 assert(NextValidReloc == 0 ||
364 StartOffset > ValidRelocs[NextValidReloc - 1].Offset);
365 if (NextValidReloc >= ValidRelocs.size())
368 uint64_t RelocOffset = ValidRelocs[NextValidReloc].Offset;
370 // We might need to skip some relocs that we didn't consider. For
371 // example the high_pc of a discarded DIE might contain a reloc that
372 // is in the list because it actually corresponds to the start of a
373 // function that is in the debug map.
374 while (RelocOffset < StartOffset && NextValidReloc < ValidRelocs.size() - 1)
375 RelocOffset = ValidRelocs[++NextValidReloc].Offset;
377 if (RelocOffset < StartOffset || RelocOffset >= EndOffset)
380 const auto &ValidReloc = ValidRelocs[NextValidReloc++];
382 outs() << "Found valid debug map entry: " << ValidReloc.Mapping->getKey()
383 << " " << format("\t%016" PRIx64 " => %016" PRIx64,
384 ValidReloc.Mapping->getValue().ObjectAddress,
385 ValidReloc.Mapping->getValue().BinaryAddress);
388 ValidReloc.Mapping->getValue().BinaryAddress + ValidReloc.Addend;
389 Info.InDebugMap = true;
393 /// \brief Get the starting and ending (exclusive) offset for the
394 /// attribute with index \p Idx descibed by \p Abbrev. \p Offset is
395 /// supposed to point to the position of the first attribute described
397 /// \return [StartOffset, EndOffset) as a pair.
398 static std::pair<uint32_t, uint32_t>
399 getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx,
400 unsigned Offset, const DWARFUnit &Unit) {
401 DataExtractor Data = Unit.getDebugInfoExtractor();
403 for (unsigned i = 0; i < Idx; ++i)
404 DWARFFormValue::skipValue(Abbrev->getFormByIndex(i), Data, &Offset, &Unit);
406 uint32_t End = Offset;
407 DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End, &Unit);
409 return std::make_pair(Offset, End);
412 /// \brief Check if a variable describing DIE should be kept.
413 /// \returns updated TraversalFlags.
414 unsigned DwarfLinker::shouldKeepVariableDIE(
415 const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit,
416 CompileUnit::DIEInfo &MyInfo, unsigned Flags) {
417 const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
419 // Global variables with constant value can always be kept.
420 if (!(Flags & TF_InFunctionScope) &&
421 Abbrev->findAttributeIndex(dwarf::DW_AT_const_value) != -1U) {
422 MyInfo.InDebugMap = true;
423 return Flags | TF_Keep;
426 uint32_t LocationIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_location);
427 if (LocationIdx == -1U)
430 uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
431 const DWARFUnit &OrigUnit = Unit.getOrigUnit();
432 uint32_t LocationOffset, LocationEndOffset;
433 std::tie(LocationOffset, LocationEndOffset) =
434 getAttributeOffsets(Abbrev, LocationIdx, Offset, OrigUnit);
436 // See if there is a relocation to a valid debug map entry inside
437 // this variable's location. The order is important here. We want to
438 // always check in the variable has a valid relocation, so that the
439 // DIEInfo is filled. However, we don't want a static variable in a
440 // function to force us to keep the enclosing function.
441 if (!hasValidRelocation(LocationOffset, LocationEndOffset, MyInfo) ||
442 (Flags & TF_InFunctionScope))
446 DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */);
448 return Flags | TF_Keep;
451 /// \brief Check if a function describing DIE should be kept.
452 /// \returns updated TraversalFlags.
453 unsigned DwarfLinker::shouldKeepSubprogramDIE(
454 const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit,
455 CompileUnit::DIEInfo &MyInfo, unsigned Flags) {
456 const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
458 Flags |= TF_InFunctionScope;
460 uint32_t LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc);
464 uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
465 const DWARFUnit &OrigUnit = Unit.getOrigUnit();
466 uint32_t LowPcOffset, LowPcEndOffset;
467 std::tie(LowPcOffset, LowPcEndOffset) =
468 getAttributeOffsets(Abbrev, LowPcIdx, Offset, OrigUnit);
471 DIE.getAttributeValueAsAddress(&OrigUnit, dwarf::DW_AT_low_pc, -1ULL);
472 assert(LowPc != -1ULL && "low_pc attribute is not an address.");
473 if (LowPc == -1ULL ||
474 !hasValidRelocation(LowPcOffset, LowPcEndOffset, MyInfo))
478 DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */);
480 return Flags | TF_Keep;
483 /// \brief Check if a DIE should be kept.
484 /// \returns updated TraversalFlags.
485 unsigned DwarfLinker::shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE,
487 CompileUnit::DIEInfo &MyInfo,
489 switch (DIE.getTag()) {
490 case dwarf::DW_TAG_constant:
491 case dwarf::DW_TAG_variable:
492 return shouldKeepVariableDIE(DIE, Unit, MyInfo, Flags);
493 case dwarf::DW_TAG_subprogram:
494 return shouldKeepSubprogramDIE(DIE, Unit, MyInfo, Flags);
495 case dwarf::DW_TAG_module:
496 case dwarf::DW_TAG_imported_module:
497 case dwarf::DW_TAG_imported_declaration:
498 case dwarf::DW_TAG_imported_unit:
499 // We always want to keep these.
500 return Flags | TF_Keep;
507 /// \brief Mark the passed DIE as well as all the ones it depends on
510 /// This function is called by lookForDIEsToKeep on DIEs that are
511 /// newly discovered to be needed in the link. It recursively calls
512 /// back to lookForDIEsToKeep while adding TF_DependencyWalk to the
513 /// TraversalFlags to inform it that it's not doing the primary DIE
515 void DwarfLinker::keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE,
516 CompileUnit::DIEInfo &MyInfo,
517 const DebugMapObject &DMO,
518 CompileUnit &CU, unsigned Flags) {
519 const DWARFUnit &Unit = CU.getOrigUnit();
522 // First mark all the parent chain as kept.
523 unsigned AncestorIdx = MyInfo.ParentIdx;
524 while (!CU.getInfo(AncestorIdx).Keep) {
525 lookForDIEsToKeep(*Unit.getDIEAtIndex(AncestorIdx), DMO, CU,
526 TF_ParentWalk | TF_Keep | TF_DependencyWalk);
527 AncestorIdx = CU.getInfo(AncestorIdx).ParentIdx;
530 // Then we need to mark all the DIEs referenced by this DIE's
531 // attributes as kept.
532 DataExtractor Data = Unit.getDebugInfoExtractor();
533 const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
534 uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
536 // Mark all DIEs referenced through atttributes as kept.
537 for (const auto &AttrSpec : Abbrev->attributes()) {
538 DWARFFormValue Val(AttrSpec.Form);
540 if (!Val.isFormClass(DWARFFormValue::FC_Reference)) {
541 DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset, &Unit);
545 Val.extractValue(Data, &Offset, &Unit);
546 CompileUnit *ReferencedCU;
547 if (const auto *RefDIE = resolveDIEReference(Val, Unit, DIE, ReferencedCU))
548 lookForDIEsToKeep(*RefDIE, DMO, *ReferencedCU,
549 TF_Keep | TF_DependencyWalk);
553 /// \brief Recursively walk the \p DIE tree and look for DIEs to
554 /// keep. Store that information in \p CU's DIEInfo.
556 /// This function is the entry point of the DIE selection
557 /// algorithm. It is expected to walk the DIE tree in file order and
558 /// (though the mediation of its helper) call hasValidRelocation() on
559 /// each DIE that might be a 'root DIE' (See DwarfLinker class
561 /// While walking the dependencies of root DIEs, this function is
562 /// also called, but during these dependency walks the file order is
563 /// not respected. The TF_DependencyWalk flag tells us which kind of
564 /// traversal we are currently doing.
565 void DwarfLinker::lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE,
566 const DebugMapObject &DMO, CompileUnit &CU,
568 unsigned Idx = CU.getOrigUnit().getDIEIndex(&DIE);
569 CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
570 bool AlreadyKept = MyInfo.Keep;
572 // If the Keep flag is set, we are marking a required DIE's
573 // dependencies. If our target is already marked as kept, we're all
575 if ((Flags & TF_DependencyWalk) && AlreadyKept)
578 // We must not call shouldKeepDIE while called from keepDIEAndDenpendencies,
579 // because it would screw up the relocation finding logic.
580 if (!(Flags & TF_DependencyWalk))
581 Flags = shouldKeepDIE(DIE, CU, MyInfo, Flags);
583 // If it is a newly kept DIE mark it as well as all its dependencies as kept.
584 if (!AlreadyKept && (Flags & TF_Keep))
585 keepDIEAndDenpendencies(DIE, MyInfo, DMO, CU, Flags);
587 // The TF_ParentWalk flag tells us that we are currently walking up
588 // the parent chain of a required DIE, and we don't want to mark all
589 // the children of the parents as kept (consider for example a
590 // DW_TAG_namespace node in the parent chain). There are however a
591 // set of DIE types for which we want to ignore that directive and still
592 // walk their children.
593 if (dieNeedsChildrenToBeMeaningful(DIE.getTag()))
594 Flags &= ~TF_ParentWalk;
596 if (!DIE.hasChildren() || (Flags & TF_ParentWalk))
599 for (auto *Child = DIE.getFirstChild(); Child && !Child->isNULL();
600 Child = Child->getSibling())
601 lookForDIEsToKeep(*Child, DMO, CU, Flags);
604 bool DwarfLinker::link(const DebugMap &Map) {
606 if (Map.begin() == Map.end()) {
607 errs() << "Empty debug map.\n";
611 for (const auto &Obj : Map.objects()) {
612 CurrentDebugObject = Obj.get();
615 outs() << "DEBUG MAP OBJECT: " << Obj->getObjectFilename() << "\n";
616 auto ErrOrObj = BinHolder.GetObjectFile(Obj->getObjectFilename());
617 if (std::error_code EC = ErrOrObj.getError()) {
618 reportWarning(Twine(Obj->getObjectFilename()) + ": " + EC.message());
622 // Look for relocations that correspond to debug map entries.
623 if (!findValidRelocsInDebugInfo(*ErrOrObj, *Obj)) {
625 outs() << "No valid relocations found. Skipping.\n";
629 // Setup access to the debug info.
630 DWARFContextInMemory DwarfContext(*ErrOrObj);
631 startDebugObject(DwarfContext);
633 // In a first phase, just read in the debug info and store the DIE
634 // parent links that we will use during the next phase.
635 for (const auto &CU : DwarfContext.compile_units()) {
636 auto *CUDie = CU->getCompileUnitDIE(false);
638 outs() << "Input compilation unit:";
639 CUDie->dump(outs(), CU.get(), 0);
641 Units.emplace_back(*CU);
642 gatherDIEParents(CUDie, 0, Units.back());
645 // Then mark all the DIEs that need to be present in the linked
646 // output and collect some information about them. Note that this
647 // loop can not be merged with the previous one becaue cross-cu
648 // references require the ParentIdx to be setup for every CU in
649 // the object file before calling this.
650 for (auto &CurrentUnit : Units)
651 lookForDIEsToKeep(*CurrentUnit.getOrigUnit().getCompileUnitDIE(), *Obj,
654 // Clean-up before starting working on the next object.
662 bool linkDwarf(StringRef OutputFilename, const DebugMap &DM, bool Verbose) {
663 DwarfLinker Linker(OutputFilename, Verbose);
664 return Linker.link(DM);