blob: 3c0bc0b85afac0917b22bbe7baf874ee54df9e74 [file] [log] [blame]
Frederic Riss231f7142014-12-12 17:31:24 +00001//===- 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"
Frederic Rissd3455182015-01-28 18:27:01 +000010#include "BinaryHolder.h"
11#include "DebugMap.h"
Frederic Riss231f7142014-12-12 17:31:24 +000012#include "dsymutil.h"
Zachary Turner82af9432015-01-30 18:07:45 +000013#include "llvm/DebugInfo/DWARF/DWARFContext.h"
14#include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
Frederic Riss1b9da422015-02-13 23:18:29 +000015#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Frederic Riss1036e642015-02-13 23:18:22 +000016#include "llvm/Object/MachO.h"
Frederic Riss84c09a52015-02-13 23:18:34 +000017#include "llvm/Support/Dwarf.h"
18#include "llvm/Support/LEB128.h"
Frederic Rissd3455182015-01-28 18:27:01 +000019#include <string>
Frederic Riss231f7142014-12-12 17:31:24 +000020
21namespace llvm {
22namespace dsymutil {
23
Frederic Rissd3455182015-01-28 18:27:01 +000024namespace {
25
Frederic Riss563cba62015-01-28 22:15:14 +000026/// \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.
29class CompileUnit {
30public:
31 /// \brief Information gathered about a DIE in the object file.
32 struct DIEInfo {
Frederic Riss84c09a52015-02-13 23:18:34 +000033 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?
Frederic Riss563cba62015-01-28 22:15:14 +000037 };
38
39 CompileUnit(DWARFUnit &OrigUnit) : OrigUnit(OrigUnit) {
40 Info.resize(OrigUnit.getNumDIEs());
41 }
42
Frederic Rissc3349d42015-02-13 23:18:27 +000043 DWARFUnit &getOrigUnit() const { return OrigUnit; }
Frederic Riss563cba62015-01-28 22:15:14 +000044
45 DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; }
46 const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; }
47
48private:
49 DWARFUnit &OrigUnit;
50 std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index.
51};
52
Frederic Rissd3455182015-01-28 18:27:01 +000053/// \brief The core of the Dwarf linking logic.
Frederic Riss1036e642015-02-13 23:18:22 +000054///
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.
61///
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.
Frederic Rissd3455182015-01-28 18:27:01 +000067class DwarfLinker {
68public:
69 DwarfLinker(StringRef OutputFilename, bool Verbose)
70 : OutputFilename(OutputFilename), Verbose(Verbose), BinHolder(Verbose) {}
71
72 /// \brief Link the contents of the DebugMap.
73 bool link(const DebugMap &);
74
75private:
Frederic Riss563cba62015-01-28 22:15:14 +000076 /// \brief Called at the start of a debug object link.
77 void startDebugObject(DWARFContext &);
78
79 /// \brief Called at the end of a debug object link.
80 void endDebugObject();
81
Frederic Riss1036e642015-02-13 23:18:22 +000082 /// \defgroup FindValidRelocations Translate debug map into a list
83 /// of relevant relocations
84 ///
85 /// @{
86 struct ValidReloc {
87 uint32_t Offset;
88 uint32_t Size;
89 uint64_t Addend;
90 const DebugMapObject::DebugMapEntry *Mapping;
91
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) {}
95
96 bool operator<(const ValidReloc &RHS) const { return Offset < RHS.Offset; }
97 };
98
99 /// \brief The valid relocations for the current DebugMapObject.
100 /// This vector is sorted by relocation offset.
101 std::vector<ValidReloc> ValidRelocs;
102
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;
109
110 bool findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
111 const DebugMapObject &DMO);
112
113 bool findValidRelocs(const object::SectionRef &Section,
114 const object::ObjectFile &Obj,
115 const DebugMapObject &DMO);
116
117 void findValidRelocsMachO(const object::SectionRef &Section,
118 const object::MachOObjectFile &Obj,
119 const DebugMapObject &DMO);
120 /// @}
Frederic Riss1b9da422015-02-13 23:18:29 +0000121
Frederic Riss84c09a52015-02-13 23:18:34 +0000122 /// \defgroup FindRootDIEs Find DIEs corresponding to debug map entries.
123 ///
124 /// @{
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,
129 unsigned Flags);
130
131 /// \brief Flags passed to DwarfLinker::lookForDIEsToKeep
132 enum TravesalFlags {
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.
137 };
138
139 /// \brief Mark the passed DIE as well as all the ones it depends on
140 /// as kept.
141 void keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE,
142 CompileUnit::DIEInfo &MyInfo,
143 const DebugMapObject &DMO, CompileUnit &CU,
144 unsigned Flags);
145
146 unsigned shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE,
147 CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
148 unsigned Flags);
149
150 unsigned shouldKeepVariableDIE(const DWARFDebugInfoEntryMinimal &DIE,
151 CompileUnit &Unit,
152 CompileUnit::DIEInfo &MyInfo, unsigned Flags);
153
154 unsigned shouldKeepSubprogramDIE(const DWARFDebugInfoEntryMinimal &DIE,
155 CompileUnit &Unit,
156 CompileUnit::DIEInfo &MyInfo,
157 unsigned Flags);
158
159 bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
160 CompileUnit::DIEInfo &Info);
161 /// @}
162
Frederic Riss1b9da422015-02-13 23:18:29 +0000163 /// \defgroup Helpers Various helper methods.
164 ///
165 /// @{
166 const DWARFDebugInfoEntryMinimal *
167 resolveDIEReference(DWARFFormValue &RefValue, const DWARFUnit &Unit,
168 const DWARFDebugInfoEntryMinimal &DIE,
169 CompileUnit *&ReferencedCU);
170
171 CompileUnit *getUnitForOffset(unsigned Offset);
172
173 void reportWarning(const Twine &Warning, const DWARFUnit *Unit = nullptr,
174 const DWARFDebugInfoEntryMinimal *DIE = nullptr);
175 /// @}
176
Frederic Riss563cba62015-01-28 22:15:14 +0000177private:
Frederic Rissd3455182015-01-28 18:27:01 +0000178 std::string OutputFilename;
179 bool Verbose;
180 BinaryHolder BinHolder;
Frederic Riss563cba62015-01-28 22:15:14 +0000181
182 /// The units of the current debug map object.
183 std::vector<CompileUnit> Units;
Frederic Riss1b9da422015-02-13 23:18:29 +0000184
185 /// The debug map object curently under consideration.
186 DebugMapObject *CurrentDebugObject;
Frederic Rissd3455182015-01-28 18:27:01 +0000187};
188
Frederic Riss1b9da422015-02-13 23:18:29 +0000189/// \brief Similar to DWARFUnitSection::getUnitForOffset(), but
190/// returning our CompileUnit object instead.
191CompileUnit *DwarfLinker::getUnitForOffset(unsigned Offset) {
192 auto CU =
193 std::upper_bound(Units.begin(), Units.end(), Offset,
194 [](uint32_t LHS, const CompileUnit &RHS) {
195 return LHS < RHS.getOrigUnit().getNextUnitOffset();
196 });
197 return CU != Units.end() ? &*CU : nullptr;
198}
199
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.
204const 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);
209
210 if ((RefCU = getUnitForOffset(RefOffset)))
211 if (const auto *RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset))
212 return RefDie;
213
214 reportWarning("could not find referenced DIE", &Unit, &DIE);
215 return nullptr;
216}
217
218/// \brief Report a warning to the user, optionaly including
219/// information about a specific \p DIE related to the warning.
220void 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";
226
227 if (!Verbose || !DIE)
228 return;
229
230 errs() << " in DIE:\n";
231 DIE->dump(errs(), const_cast<DWARFUnit *>(Unit), 0 /* RecurseDepth */,
232 6 /* Indent */);
233}
234
Frederic Riss563cba62015-01-28 22:15:14 +0000235/// \brief Recursive helper to gather the child->parent relationships in the
236/// original compile unit.
Frederic Riss9aa725b2015-02-13 23:18:31 +0000237static void gatherDIEParents(const DWARFDebugInfoEntryMinimal *DIE,
238 unsigned ParentIdx, CompileUnit &CU) {
Frederic Riss563cba62015-01-28 22:15:14 +0000239 unsigned MyIdx = CU.getOrigUnit().getDIEIndex(DIE);
240 CU.getInfo(MyIdx).ParentIdx = ParentIdx;
241
242 if (DIE->hasChildren())
243 for (auto *Child = DIE->getFirstChild(); Child && !Child->isNULL();
244 Child = Child->getSibling())
Frederic Riss9aa725b2015-02-13 23:18:31 +0000245 gatherDIEParents(Child, MyIdx, CU);
Frederic Riss563cba62015-01-28 22:15:14 +0000246}
247
Frederic Riss84c09a52015-02-13 23:18:34 +0000248static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) {
249 switch (Tag) {
250 default:
251 return false;
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:
258 return true;
259 }
260 llvm_unreachable("Invalid Tag");
261}
262
Frederic Riss563cba62015-01-28 22:15:14 +0000263void DwarfLinker::startDebugObject(DWARFContext &Dwarf) {
264 Units.reserve(Dwarf.getNumCompileUnits());
Frederic Riss1036e642015-02-13 23:18:22 +0000265 NextValidReloc = 0;
Frederic Riss563cba62015-01-28 22:15:14 +0000266}
267
Frederic Riss1036e642015-02-13 23:18:22 +0000268void DwarfLinker::endDebugObject() {
269 Units.clear();
270 ValidRelocs.clear();
271}
272
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.
276void DwarfLinker::findValidRelocsMachO(const object::SectionRef &Section,
277 const object::MachOObjectFile &Obj,
278 const DebugMapObject &DMO) {
279 StringRef Contents;
280 Section.getContents(Contents);
281 DataExtractor Data(Contents, Obj.isLittleEndian(), 0);
282
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);
287 uint64_t Offset64;
288 if ((RelocSize != 4 && RelocSize != 8) || Reloc.getOffset(Offset64)) {
Frederic Riss1b9da422015-02-13 23:18:29 +0000289 reportWarning(" unsupported relocation in debug_info section.");
Frederic Riss1036e642015-02-13 23:18:22 +0000290 continue;
291 }
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);
295
296 auto Sym = Reloc.getSymbol();
297 if (Sym != Obj.symbol_end()) {
298 StringRef SymbolName;
299 if (Sym->getName(SymbolName)) {
Frederic Riss1b9da422015-02-13 23:18:29 +0000300 reportWarning("error getting relocation symbol name.");
Frederic Riss1036e642015-02-13 23:18:22 +0000301 continue;
302 }
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);
310 }
311 }
312}
313
314/// \brief Dispatch the valid relocation finding logic to the
315/// appropriate handler depending on the object file format.
316bool 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);
322 else
Frederic Riss1b9da422015-02-13 23:18:29 +0000323 reportWarning(Twine("unsupported object file type: ") + Obj.getFileName());
Frederic Riss1036e642015-02-13 23:18:22 +0000324
325 if (ValidRelocs.empty())
326 return false;
327
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());
333 return true;
334}
335
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
339/// linked binary.
340/// \returns wether there are any valid relocations in the debug info.
341bool 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")
349 continue;
350 return findValidRelocs(Section, Obj, DMO);
351 }
352 return false;
353}
Frederic Riss563cba62015-01-28 22:15:14 +0000354
Frederic Riss84c09a52015-02-13 23:18:34 +0000355/// \brief Checks that there is a relocation against an actual debug
356/// map entry between \p StartOffset and \p NextOffset.
357///
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.
361bool 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())
366 return false;
367
368 uint64_t RelocOffset = ValidRelocs[NextValidReloc].Offset;
369
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;
376
377 if (RelocOffset < StartOffset || RelocOffset >= EndOffset)
378 return false;
379
380 const auto &ValidReloc = ValidRelocs[NextValidReloc++];
381 if (Verbose)
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);
386
387 Info.Address =
388 ValidReloc.Mapping->getValue().BinaryAddress + ValidReloc.Addend;
389 Info.InDebugMap = true;
390 return true;
391}
392
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
396/// by \p Abbrev.
397/// \return [StartOffset, EndOffset) as a pair.
398static std::pair<uint32_t, uint32_t>
399getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx,
400 unsigned Offset, const DWARFUnit &Unit) {
401 DataExtractor Data = Unit.getDebugInfoExtractor();
402
403 for (unsigned i = 0; i < Idx; ++i)
404 DWARFFormValue::skipValue(Abbrev->getFormByIndex(i), Data, &Offset, &Unit);
405
406 uint32_t End = Offset;
407 DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End, &Unit);
408
409 return std::make_pair(Offset, End);
410}
411
412/// \brief Check if a variable describing DIE should be kept.
413/// \returns updated TraversalFlags.
414unsigned DwarfLinker::shouldKeepVariableDIE(
415 const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit,
416 CompileUnit::DIEInfo &MyInfo, unsigned Flags) {
417 const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
418
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;
424 }
425
426 uint32_t LocationIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_location);
427 if (LocationIdx == -1U)
428 return Flags;
429
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);
435
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))
443 return Flags;
444
445 if (Verbose)
446 DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */);
447
448 return Flags | TF_Keep;
449}
450
451/// \brief Check if a function describing DIE should be kept.
452/// \returns updated TraversalFlags.
453unsigned DwarfLinker::shouldKeepSubprogramDIE(
454 const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit,
455 CompileUnit::DIEInfo &MyInfo, unsigned Flags) {
456 const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
457
458 Flags |= TF_InFunctionScope;
459
460 uint32_t LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc);
461 if (LowPcIdx == -1U)
462 return Flags;
463
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);
469
470 uint64_t LowPc =
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))
475 return Flags;
476
477 if (Verbose)
478 DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */);
479
480 return Flags | TF_Keep;
481}
482
483/// \brief Check if a DIE should be kept.
484/// \returns updated TraversalFlags.
485unsigned DwarfLinker::shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE,
486 CompileUnit &Unit,
487 CompileUnit::DIEInfo &MyInfo,
488 unsigned Flags) {
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;
501 }
502
503 return Flags;
504}
505
506
507/// \brief Mark the passed DIE as well as all the ones it depends on
508/// as kept.
509///
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
514/// tree walk.
515void DwarfLinker::keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE,
516 CompileUnit::DIEInfo &MyInfo,
517 const DebugMapObject &DMO,
518 CompileUnit &CU, unsigned Flags) {
519 const DWARFUnit &Unit = CU.getOrigUnit();
520 MyInfo.Keep = true;
521
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;
528 }
529
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());
535
536 // Mark all DIEs referenced through atttributes as kept.
537 for (const auto &AttrSpec : Abbrev->attributes()) {
538 DWARFFormValue Val(AttrSpec.Form);
539
540 if (!Val.isFormClass(DWARFFormValue::FC_Reference)) {
541 DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset, &Unit);
542 continue;
543 }
544
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);
550 }
551}
552
553/// \brief Recursively walk the \p DIE tree and look for DIEs to
554/// keep. Store that information in \p CU's DIEInfo.
555///
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
560/// comment).
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.
565void DwarfLinker::lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE,
566 const DebugMapObject &DMO, CompileUnit &CU,
567 unsigned Flags) {
568 unsigned Idx = CU.getOrigUnit().getDIEIndex(&DIE);
569 CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
570 bool AlreadyKept = MyInfo.Keep;
571
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
574 // set.
575 if ((Flags & TF_DependencyWalk) && AlreadyKept)
576 return;
577
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);
582
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);
586
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;
595
596 if (!DIE.hasChildren() || (Flags & TF_ParentWalk))
597 return;
598
599 for (auto *Child = DIE.getFirstChild(); Child && !Child->isNULL();
600 Child = Child->getSibling())
601 lookForDIEsToKeep(*Child, DMO, CU, Flags);
602}
603
Frederic Rissd3455182015-01-28 18:27:01 +0000604bool DwarfLinker::link(const DebugMap &Map) {
605
606 if (Map.begin() == Map.end()) {
607 errs() << "Empty debug map.\n";
608 return false;
609 }
610
611 for (const auto &Obj : Map.objects()) {
Frederic Riss1b9da422015-02-13 23:18:29 +0000612 CurrentDebugObject = Obj.get();
613
Frederic Rissd3455182015-01-28 18:27:01 +0000614 if (Verbose)
615 outs() << "DEBUG MAP OBJECT: " << Obj->getObjectFilename() << "\n";
616 auto ErrOrObj = BinHolder.GetObjectFile(Obj->getObjectFilename());
617 if (std::error_code EC = ErrOrObj.getError()) {
Frederic Riss1b9da422015-02-13 23:18:29 +0000618 reportWarning(Twine(Obj->getObjectFilename()) + ": " + EC.message());
Frederic Rissd3455182015-01-28 18:27:01 +0000619 continue;
620 }
621
Frederic Riss1036e642015-02-13 23:18:22 +0000622 // Look for relocations that correspond to debug map entries.
623 if (!findValidRelocsInDebugInfo(*ErrOrObj, *Obj)) {
624 if (Verbose)
625 outs() << "No valid relocations found. Skipping.\n";
626 continue;
627 }
628
Frederic Riss563cba62015-01-28 22:15:14 +0000629 // Setup access to the debug info.
Frederic Rissd3455182015-01-28 18:27:01 +0000630 DWARFContextInMemory DwarfContext(*ErrOrObj);
Frederic Riss563cba62015-01-28 22:15:14 +0000631 startDebugObject(DwarfContext);
Frederic Rissd3455182015-01-28 18:27:01 +0000632
Frederic Riss563cba62015-01-28 22:15:14 +0000633 // 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.
Frederic Rissd3455182015-01-28 18:27:01 +0000635 for (const auto &CU : DwarfContext.compile_units()) {
636 auto *CUDie = CU->getCompileUnitDIE(false);
637 if (Verbose) {
638 outs() << "Input compilation unit:";
639 CUDie->dump(outs(), CU.get(), 0);
640 }
Frederic Riss563cba62015-01-28 22:15:14 +0000641 Units.emplace_back(*CU);
Frederic Riss9aa725b2015-02-13 23:18:31 +0000642 gatherDIEParents(CUDie, 0, Units.back());
Frederic Rissd3455182015-01-28 18:27:01 +0000643 }
Frederic Riss563cba62015-01-28 22:15:14 +0000644
Frederic Riss84c09a52015-02-13 23:18:34 +0000645 // 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,
652 CurrentUnit, 0);
653
Frederic Riss563cba62015-01-28 22:15:14 +0000654 // Clean-up before starting working on the next object.
655 endDebugObject();
Frederic Rissd3455182015-01-28 18:27:01 +0000656 }
657
Frederic Riss231f7142014-12-12 17:31:24 +0000658 return true;
659}
660}
Frederic Rissd3455182015-01-28 18:27:01 +0000661
662bool linkDwarf(StringRef OutputFilename, const DebugMap &DM, bool Verbose) {
663 DwarfLinker Linker(OutputFilename, Verbose);
664 return Linker.link(DM);
665}
666}
Frederic Riss231f7142014-12-12 17:31:24 +0000667}