blob: a30c2d85eacc52b40af89d2afaa671b32766ace3 [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"
Frederic Rissc99ea202015-02-28 00:29:11 +000013#include "llvm/CodeGen/AsmPrinter.h"
Zachary Turner82af9432015-01-30 18:07:45 +000014#include "llvm/DebugInfo/DWARF/DWARFContext.h"
15#include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
Frederic Riss1b9da422015-02-13 23:18:29 +000016#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Frederic Rissc99ea202015-02-28 00:29:11 +000017#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"
Frederic Riss1036e642015-02-13 23:18:22 +000025#include "llvm/Object/MachO.h"
Frederic Riss84c09a52015-02-13 23:18:34 +000026#include "llvm/Support/Dwarf.h"
27#include "llvm/Support/LEB128.h"
Frederic Rissc99ea202015-02-28 00:29:11 +000028#include "llvm/Support/TargetRegistry.h"
29#include "llvm/Target/TargetMachine.h"
30#include "llvm/Target/TargetOptions.h"
Frederic Rissd3455182015-01-28 18:27:01 +000031#include <string>
Frederic Riss231f7142014-12-12 17:31:24 +000032
33namespace llvm {
34namespace dsymutil {
35
Frederic Rissd3455182015-01-28 18:27:01 +000036namespace {
37
Frederic Rissdef4fb72015-02-28 00:29:01 +000038void warn(const Twine &Warning, const Twine &Context) {
39 errs() << Twine("while processing ") + Context + ":\n";
40 errs() << Twine("warning: ") + Warning + "\n";
41}
42
Frederic Rissc99ea202015-02-28 00:29:11 +000043bool 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
Frederic Riss563cba62015-01-28 22:15:14 +000049/// \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.
52class CompileUnit {
53public:
54 /// \brief Information gathered about a DIE in the object file.
55 struct DIEInfo {
Frederic Riss84c09a52015-02-13 23:18:34 +000056 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?
Frederic Riss563cba62015-01-28 22:15:14 +000060 };
61
62 CompileUnit(DWARFUnit &OrigUnit) : OrigUnit(OrigUnit) {
63 Info.resize(OrigUnit.getNumDIEs());
64 }
65
Frederic Rissc3349d42015-02-13 23:18:27 +000066 DWARFUnit &getOrigUnit() const { return OrigUnit; }
Frederic Riss563cba62015-01-28 22:15:14 +000067
68 DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; }
69 const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; }
70
71private:
72 DWARFUnit &OrigUnit;
73 std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index.
74};
75
Frederic Rissc99ea202015-02-28 00:29:11 +000076/// \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.
80class 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
99public:
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
110bool 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;
Frederic Rissb52cf522015-02-28 00:42:37 +0000154 OutFile = llvm::make_unique<raw_fd_ostream>(OutputFilename, EC,
155 sys::fs::F_None);
Frederic Rissc99ea202015-02-28 00:29:11 +0000156 if (EC)
157 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
158
159 MS = TheTarget->createMCObjectStreamer(TripleName, *MC, *MAB, *OutFile, MCE,
160 *MSTI, false);
161 if (!MS)
162 return error("no object streamer for target " + TripleName, Context);
163
164 // Finally create the AsmPrinter we'll use to emit the DIEs.
165 TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions()));
166 if (!TM)
167 return error("no target machine for target " + TripleName, Context);
168
169 Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS)));
170 if (!Asm)
171 return error("no asm printer for target " + TripleName, Context);
172
173 return true;
174}
175
176bool DwarfStreamer::finish() {
177 MS->Finish();
178 return true;
179}
180
Frederic Rissd3455182015-01-28 18:27:01 +0000181/// \brief The core of the Dwarf linking logic.
Frederic Riss1036e642015-02-13 23:18:22 +0000182///
183/// The link of the dwarf information from the object files will be
184/// driven by the selection of 'root DIEs', which are DIEs that
185/// describe variables or functions that are present in the linked
186/// binary (and thus have entries in the debug map). All the debug
187/// information that will be linked (the DIEs, but also the line
188/// tables, ranges, ...) is derived from that set of root DIEs.
189///
190/// The root DIEs are identified because they contain relocations that
191/// correspond to a debug map entry at specific places (the low_pc for
192/// a function, the location for a variable). These relocations are
193/// called ValidRelocs in the DwarfLinker and are gathered as a very
194/// first step when we start processing a DebugMapObject.
Frederic Rissd3455182015-01-28 18:27:01 +0000195class DwarfLinker {
196public:
Frederic Rissb9818322015-02-28 00:29:07 +0000197 DwarfLinker(StringRef OutputFilename, const LinkOptions &Options)
198 : OutputFilename(OutputFilename), Options(Options),
199 BinHolder(Options.Verbose) {}
Frederic Rissd3455182015-01-28 18:27:01 +0000200
201 /// \brief Link the contents of the DebugMap.
202 bool link(const DebugMap &);
203
204private:
Frederic Riss563cba62015-01-28 22:15:14 +0000205 /// \brief Called at the start of a debug object link.
206 void startDebugObject(DWARFContext &);
207
208 /// \brief Called at the end of a debug object link.
209 void endDebugObject();
210
Frederic Riss1036e642015-02-13 23:18:22 +0000211 /// \defgroup FindValidRelocations Translate debug map into a list
212 /// of relevant relocations
213 ///
214 /// @{
215 struct ValidReloc {
216 uint32_t Offset;
217 uint32_t Size;
218 uint64_t Addend;
219 const DebugMapObject::DebugMapEntry *Mapping;
220
221 ValidReloc(uint32_t Offset, uint32_t Size, uint64_t Addend,
222 const DebugMapObject::DebugMapEntry *Mapping)
223 : Offset(Offset), Size(Size), Addend(Addend), Mapping(Mapping) {}
224
225 bool operator<(const ValidReloc &RHS) const { return Offset < RHS.Offset; }
226 };
227
228 /// \brief The valid relocations for the current DebugMapObject.
229 /// This vector is sorted by relocation offset.
230 std::vector<ValidReloc> ValidRelocs;
231
232 /// \brief Index into ValidRelocs of the next relocation to
233 /// consider. As we walk the DIEs in acsending file offset and as
234 /// ValidRelocs is sorted by file offset, keeping this index
235 /// uptodate is all we have to do to have a cheap lookup during the
236 /// root DIE selection.
237 unsigned NextValidReloc;
238
239 bool findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
240 const DebugMapObject &DMO);
241
242 bool findValidRelocs(const object::SectionRef &Section,
243 const object::ObjectFile &Obj,
244 const DebugMapObject &DMO);
245
246 void findValidRelocsMachO(const object::SectionRef &Section,
247 const object::MachOObjectFile &Obj,
248 const DebugMapObject &DMO);
249 /// @}
Frederic Riss1b9da422015-02-13 23:18:29 +0000250
Frederic Riss84c09a52015-02-13 23:18:34 +0000251 /// \defgroup FindRootDIEs Find DIEs corresponding to debug map entries.
252 ///
253 /// @{
254 /// \brief Recursively walk the \p DIE tree and look for DIEs to
255 /// keep. Store that information in \p CU's DIEInfo.
256 void lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE,
257 const DebugMapObject &DMO, CompileUnit &CU,
258 unsigned Flags);
259
260 /// \brief Flags passed to DwarfLinker::lookForDIEsToKeep
261 enum TravesalFlags {
262 TF_Keep = 1 << 0, ///< Mark the traversed DIEs as kept.
263 TF_InFunctionScope = 1 << 1, ///< Current scope is a fucntion scope.
264 TF_DependencyWalk = 1 << 2, ///< Walking the dependencies of a kept DIE.
265 TF_ParentWalk = 1 << 3, ///< Walking up the parents of a kept DIE.
266 };
267
268 /// \brief Mark the passed DIE as well as all the ones it depends on
269 /// as kept.
270 void keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE,
271 CompileUnit::DIEInfo &MyInfo,
272 const DebugMapObject &DMO, CompileUnit &CU,
273 unsigned Flags);
274
275 unsigned shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE,
276 CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
277 unsigned Flags);
278
279 unsigned shouldKeepVariableDIE(const DWARFDebugInfoEntryMinimal &DIE,
280 CompileUnit &Unit,
281 CompileUnit::DIEInfo &MyInfo, unsigned Flags);
282
283 unsigned shouldKeepSubprogramDIE(const DWARFDebugInfoEntryMinimal &DIE,
284 CompileUnit &Unit,
285 CompileUnit::DIEInfo &MyInfo,
286 unsigned Flags);
287
288 bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
289 CompileUnit::DIEInfo &Info);
290 /// @}
291
Frederic Riss1b9da422015-02-13 23:18:29 +0000292 /// \defgroup Helpers Various helper methods.
293 ///
294 /// @{
295 const DWARFDebugInfoEntryMinimal *
296 resolveDIEReference(DWARFFormValue &RefValue, const DWARFUnit &Unit,
297 const DWARFDebugInfoEntryMinimal &DIE,
298 CompileUnit *&ReferencedCU);
299
300 CompileUnit *getUnitForOffset(unsigned Offset);
301
302 void reportWarning(const Twine &Warning, const DWARFUnit *Unit = nullptr,
303 const DWARFDebugInfoEntryMinimal *DIE = nullptr);
Frederic Rissc99ea202015-02-28 00:29:11 +0000304
305 bool createStreamer(Triple TheTriple, StringRef OutputFilename);
Frederic Riss1b9da422015-02-13 23:18:29 +0000306 /// @}
307
Frederic Riss563cba62015-01-28 22:15:14 +0000308private:
Frederic Rissd3455182015-01-28 18:27:01 +0000309 std::string OutputFilename;
Frederic Rissb9818322015-02-28 00:29:07 +0000310 LinkOptions Options;
Frederic Rissd3455182015-01-28 18:27:01 +0000311 BinaryHolder BinHolder;
Frederic Rissc99ea202015-02-28 00:29:11 +0000312 std::unique_ptr<DwarfStreamer> Streamer;
Frederic Riss563cba62015-01-28 22:15:14 +0000313
314 /// The units of the current debug map object.
315 std::vector<CompileUnit> Units;
Frederic Riss1b9da422015-02-13 23:18:29 +0000316
317 /// The debug map object curently under consideration.
318 DebugMapObject *CurrentDebugObject;
Frederic Rissd3455182015-01-28 18:27:01 +0000319};
320
Frederic Riss1b9da422015-02-13 23:18:29 +0000321/// \brief Similar to DWARFUnitSection::getUnitForOffset(), but
322/// returning our CompileUnit object instead.
323CompileUnit *DwarfLinker::getUnitForOffset(unsigned Offset) {
324 auto CU =
325 std::upper_bound(Units.begin(), Units.end(), Offset,
326 [](uint32_t LHS, const CompileUnit &RHS) {
327 return LHS < RHS.getOrigUnit().getNextUnitOffset();
328 });
329 return CU != Units.end() ? &*CU : nullptr;
330}
331
332/// \brief Resolve the DIE attribute reference that has been
333/// extracted in \p RefValue. The resulting DIE migh be in another
334/// CompileUnit which is stored into \p ReferencedCU.
335/// \returns null if resolving fails for any reason.
336const DWARFDebugInfoEntryMinimal *DwarfLinker::resolveDIEReference(
337 DWARFFormValue &RefValue, const DWARFUnit &Unit,
338 const DWARFDebugInfoEntryMinimal &DIE, CompileUnit *&RefCU) {
339 assert(RefValue.isFormClass(DWARFFormValue::FC_Reference));
340 uint64_t RefOffset = *RefValue.getAsReference(&Unit);
341
342 if ((RefCU = getUnitForOffset(RefOffset)))
343 if (const auto *RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset))
344 return RefDie;
345
346 reportWarning("could not find referenced DIE", &Unit, &DIE);
347 return nullptr;
348}
349
350/// \brief Report a warning to the user, optionaly including
351/// information about a specific \p DIE related to the warning.
352void DwarfLinker::reportWarning(const Twine &Warning, const DWARFUnit *Unit,
353 const DWARFDebugInfoEntryMinimal *DIE) {
Frederic Rissdef4fb72015-02-28 00:29:01 +0000354 StringRef Context = "<debug map>";
Frederic Riss1b9da422015-02-13 23:18:29 +0000355 if (CurrentDebugObject)
Frederic Rissdef4fb72015-02-28 00:29:01 +0000356 Context = CurrentDebugObject->getObjectFilename();
357 warn(Warning, Context);
Frederic Riss1b9da422015-02-13 23:18:29 +0000358
Frederic Rissb9818322015-02-28 00:29:07 +0000359 if (!Options.Verbose || !DIE)
Frederic Riss1b9da422015-02-13 23:18:29 +0000360 return;
361
362 errs() << " in DIE:\n";
363 DIE->dump(errs(), const_cast<DWARFUnit *>(Unit), 0 /* RecurseDepth */,
364 6 /* Indent */);
365}
366
Frederic Rissc99ea202015-02-28 00:29:11 +0000367bool DwarfLinker::createStreamer(Triple TheTriple, StringRef OutputFilename) {
368 if (Options.NoOutput)
369 return true;
370
Frederic Rissb52cf522015-02-28 00:42:37 +0000371 Streamer = llvm::make_unique<DwarfStreamer>();
Frederic Rissc99ea202015-02-28 00:29:11 +0000372 return Streamer->init(TheTriple, OutputFilename);
373}
374
Frederic Riss563cba62015-01-28 22:15:14 +0000375/// \brief Recursive helper to gather the child->parent relationships in the
376/// original compile unit.
Frederic Riss9aa725b2015-02-13 23:18:31 +0000377static void gatherDIEParents(const DWARFDebugInfoEntryMinimal *DIE,
378 unsigned ParentIdx, CompileUnit &CU) {
Frederic Riss563cba62015-01-28 22:15:14 +0000379 unsigned MyIdx = CU.getOrigUnit().getDIEIndex(DIE);
380 CU.getInfo(MyIdx).ParentIdx = ParentIdx;
381
382 if (DIE->hasChildren())
383 for (auto *Child = DIE->getFirstChild(); Child && !Child->isNULL();
384 Child = Child->getSibling())
Frederic Riss9aa725b2015-02-13 23:18:31 +0000385 gatherDIEParents(Child, MyIdx, CU);
Frederic Riss563cba62015-01-28 22:15:14 +0000386}
387
Frederic Riss84c09a52015-02-13 23:18:34 +0000388static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) {
389 switch (Tag) {
390 default:
391 return false;
392 case dwarf::DW_TAG_subprogram:
393 case dwarf::DW_TAG_lexical_block:
394 case dwarf::DW_TAG_subroutine_type:
395 case dwarf::DW_TAG_structure_type:
396 case dwarf::DW_TAG_class_type:
397 case dwarf::DW_TAG_union_type:
398 return true;
399 }
400 llvm_unreachable("Invalid Tag");
401}
402
Frederic Riss563cba62015-01-28 22:15:14 +0000403void DwarfLinker::startDebugObject(DWARFContext &Dwarf) {
404 Units.reserve(Dwarf.getNumCompileUnits());
Frederic Riss1036e642015-02-13 23:18:22 +0000405 NextValidReloc = 0;
Frederic Riss563cba62015-01-28 22:15:14 +0000406}
407
Frederic Riss1036e642015-02-13 23:18:22 +0000408void DwarfLinker::endDebugObject() {
409 Units.clear();
410 ValidRelocs.clear();
411}
412
413/// \brief Iterate over the relocations of the given \p Section and
414/// store the ones that correspond to debug map entries into the
415/// ValidRelocs array.
416void DwarfLinker::findValidRelocsMachO(const object::SectionRef &Section,
417 const object::MachOObjectFile &Obj,
418 const DebugMapObject &DMO) {
419 StringRef Contents;
420 Section.getContents(Contents);
421 DataExtractor Data(Contents, Obj.isLittleEndian(), 0);
422
423 for (const object::RelocationRef &Reloc : Section.relocations()) {
424 object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl();
425 MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef);
426 unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc);
427 uint64_t Offset64;
428 if ((RelocSize != 4 && RelocSize != 8) || Reloc.getOffset(Offset64)) {
Frederic Riss1b9da422015-02-13 23:18:29 +0000429 reportWarning(" unsupported relocation in debug_info section.");
Frederic Riss1036e642015-02-13 23:18:22 +0000430 continue;
431 }
432 uint32_t Offset = Offset64;
433 // Mach-o uses REL relocations, the addend is at the relocation offset.
434 uint64_t Addend = Data.getUnsigned(&Offset, RelocSize);
435
436 auto Sym = Reloc.getSymbol();
437 if (Sym != Obj.symbol_end()) {
438 StringRef SymbolName;
439 if (Sym->getName(SymbolName)) {
Frederic Riss1b9da422015-02-13 23:18:29 +0000440 reportWarning("error getting relocation symbol name.");
Frederic Riss1036e642015-02-13 23:18:22 +0000441 continue;
442 }
443 if (const auto *Mapping = DMO.lookupSymbol(SymbolName))
444 ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping);
445 } else if (const auto *Mapping = DMO.lookupObjectAddress(Addend)) {
446 // Do not store the addend. The addend was the address of the
447 // symbol in the object file, the address in the binary that is
448 // stored in the debug map doesn't need to be offseted.
449 ValidRelocs.emplace_back(Offset64, RelocSize, 0, Mapping);
450 }
451 }
452}
453
454/// \brief Dispatch the valid relocation finding logic to the
455/// appropriate handler depending on the object file format.
456bool DwarfLinker::findValidRelocs(const object::SectionRef &Section,
457 const object::ObjectFile &Obj,
458 const DebugMapObject &DMO) {
459 // Dispatch to the right handler depending on the file type.
460 if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj))
461 findValidRelocsMachO(Section, *MachOObj, DMO);
462 else
Frederic Riss1b9da422015-02-13 23:18:29 +0000463 reportWarning(Twine("unsupported object file type: ") + Obj.getFileName());
Frederic Riss1036e642015-02-13 23:18:22 +0000464
465 if (ValidRelocs.empty())
466 return false;
467
468 // Sort the relocations by offset. We will walk the DIEs linearly in
469 // the file, this allows us to just keep an index in the relocation
470 // array that we advance during our walk, rather than resorting to
471 // some associative container. See DwarfLinker::NextValidReloc.
472 std::sort(ValidRelocs.begin(), ValidRelocs.end());
473 return true;
474}
475
476/// \brief Look for relocations in the debug_info section that match
477/// entries in the debug map. These relocations will drive the Dwarf
478/// link by indicating which DIEs refer to symbols present in the
479/// linked binary.
480/// \returns wether there are any valid relocations in the debug info.
481bool DwarfLinker::findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
482 const DebugMapObject &DMO) {
483 // Find the debug_info section.
484 for (const object::SectionRef &Section : Obj.sections()) {
485 StringRef SectionName;
486 Section.getName(SectionName);
487 SectionName = SectionName.substr(SectionName.find_first_not_of("._"));
488 if (SectionName != "debug_info")
489 continue;
490 return findValidRelocs(Section, Obj, DMO);
491 }
492 return false;
493}
Frederic Riss563cba62015-01-28 22:15:14 +0000494
Frederic Riss84c09a52015-02-13 23:18:34 +0000495/// \brief Checks that there is a relocation against an actual debug
496/// map entry between \p StartOffset and \p NextOffset.
497///
498/// This function must be called with offsets in strictly ascending
499/// order because it never looks back at relocations it already 'went past'.
500/// \returns true and sets Info.InDebugMap if it is the case.
501bool DwarfLinker::hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
502 CompileUnit::DIEInfo &Info) {
503 assert(NextValidReloc == 0 ||
504 StartOffset > ValidRelocs[NextValidReloc - 1].Offset);
505 if (NextValidReloc >= ValidRelocs.size())
506 return false;
507
508 uint64_t RelocOffset = ValidRelocs[NextValidReloc].Offset;
509
510 // We might need to skip some relocs that we didn't consider. For
511 // example the high_pc of a discarded DIE might contain a reloc that
512 // is in the list because it actually corresponds to the start of a
513 // function that is in the debug map.
514 while (RelocOffset < StartOffset && NextValidReloc < ValidRelocs.size() - 1)
515 RelocOffset = ValidRelocs[++NextValidReloc].Offset;
516
517 if (RelocOffset < StartOffset || RelocOffset >= EndOffset)
518 return false;
519
520 const auto &ValidReloc = ValidRelocs[NextValidReloc++];
Frederic Rissb9818322015-02-28 00:29:07 +0000521 if (Options.Verbose)
Frederic Riss84c09a52015-02-13 23:18:34 +0000522 outs() << "Found valid debug map entry: " << ValidReloc.Mapping->getKey()
523 << " " << format("\t%016" PRIx64 " => %016" PRIx64,
524 ValidReloc.Mapping->getValue().ObjectAddress,
525 ValidReloc.Mapping->getValue().BinaryAddress);
526
527 Info.Address =
528 ValidReloc.Mapping->getValue().BinaryAddress + ValidReloc.Addend;
529 Info.InDebugMap = true;
530 return true;
531}
532
533/// \brief Get the starting and ending (exclusive) offset for the
534/// attribute with index \p Idx descibed by \p Abbrev. \p Offset is
535/// supposed to point to the position of the first attribute described
536/// by \p Abbrev.
537/// \return [StartOffset, EndOffset) as a pair.
538static std::pair<uint32_t, uint32_t>
539getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx,
540 unsigned Offset, const DWARFUnit &Unit) {
541 DataExtractor Data = Unit.getDebugInfoExtractor();
542
543 for (unsigned i = 0; i < Idx; ++i)
544 DWARFFormValue::skipValue(Abbrev->getFormByIndex(i), Data, &Offset, &Unit);
545
546 uint32_t End = Offset;
547 DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End, &Unit);
548
549 return std::make_pair(Offset, End);
550}
551
552/// \brief Check if a variable describing DIE should be kept.
553/// \returns updated TraversalFlags.
554unsigned DwarfLinker::shouldKeepVariableDIE(
555 const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit,
556 CompileUnit::DIEInfo &MyInfo, unsigned Flags) {
557 const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
558
559 // Global variables with constant value can always be kept.
560 if (!(Flags & TF_InFunctionScope) &&
561 Abbrev->findAttributeIndex(dwarf::DW_AT_const_value) != -1U) {
562 MyInfo.InDebugMap = true;
563 return Flags | TF_Keep;
564 }
565
566 uint32_t LocationIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_location);
567 if (LocationIdx == -1U)
568 return Flags;
569
570 uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
571 const DWARFUnit &OrigUnit = Unit.getOrigUnit();
572 uint32_t LocationOffset, LocationEndOffset;
573 std::tie(LocationOffset, LocationEndOffset) =
574 getAttributeOffsets(Abbrev, LocationIdx, Offset, OrigUnit);
575
576 // See if there is a relocation to a valid debug map entry inside
577 // this variable's location. The order is important here. We want to
578 // always check in the variable has a valid relocation, so that the
579 // DIEInfo is filled. However, we don't want a static variable in a
580 // function to force us to keep the enclosing function.
581 if (!hasValidRelocation(LocationOffset, LocationEndOffset, MyInfo) ||
582 (Flags & TF_InFunctionScope))
583 return Flags;
584
Frederic Rissb9818322015-02-28 00:29:07 +0000585 if (Options.Verbose)
Frederic Riss84c09a52015-02-13 23:18:34 +0000586 DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */);
587
588 return Flags | TF_Keep;
589}
590
591/// \brief Check if a function describing DIE should be kept.
592/// \returns updated TraversalFlags.
593unsigned DwarfLinker::shouldKeepSubprogramDIE(
594 const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit,
595 CompileUnit::DIEInfo &MyInfo, unsigned Flags) {
596 const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
597
598 Flags |= TF_InFunctionScope;
599
600 uint32_t LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc);
601 if (LowPcIdx == -1U)
602 return Flags;
603
604 uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
605 const DWARFUnit &OrigUnit = Unit.getOrigUnit();
606 uint32_t LowPcOffset, LowPcEndOffset;
607 std::tie(LowPcOffset, LowPcEndOffset) =
608 getAttributeOffsets(Abbrev, LowPcIdx, Offset, OrigUnit);
609
610 uint64_t LowPc =
611 DIE.getAttributeValueAsAddress(&OrigUnit, dwarf::DW_AT_low_pc, -1ULL);
612 assert(LowPc != -1ULL && "low_pc attribute is not an address.");
613 if (LowPc == -1ULL ||
614 !hasValidRelocation(LowPcOffset, LowPcEndOffset, MyInfo))
615 return Flags;
616
Frederic Rissb9818322015-02-28 00:29:07 +0000617 if (Options.Verbose)
Frederic Riss84c09a52015-02-13 23:18:34 +0000618 DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */);
619
620 return Flags | TF_Keep;
621}
622
623/// \brief Check if a DIE should be kept.
624/// \returns updated TraversalFlags.
625unsigned DwarfLinker::shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE,
626 CompileUnit &Unit,
627 CompileUnit::DIEInfo &MyInfo,
628 unsigned Flags) {
629 switch (DIE.getTag()) {
630 case dwarf::DW_TAG_constant:
631 case dwarf::DW_TAG_variable:
632 return shouldKeepVariableDIE(DIE, Unit, MyInfo, Flags);
633 case dwarf::DW_TAG_subprogram:
634 return shouldKeepSubprogramDIE(DIE, Unit, MyInfo, Flags);
635 case dwarf::DW_TAG_module:
636 case dwarf::DW_TAG_imported_module:
637 case dwarf::DW_TAG_imported_declaration:
638 case dwarf::DW_TAG_imported_unit:
639 // We always want to keep these.
640 return Flags | TF_Keep;
641 }
642
643 return Flags;
644}
645
Frederic Riss84c09a52015-02-13 23:18:34 +0000646/// \brief Mark the passed DIE as well as all the ones it depends on
647/// as kept.
648///
649/// This function is called by lookForDIEsToKeep on DIEs that are
650/// newly discovered to be needed in the link. It recursively calls
651/// back to lookForDIEsToKeep while adding TF_DependencyWalk to the
652/// TraversalFlags to inform it that it's not doing the primary DIE
653/// tree walk.
654void DwarfLinker::keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE,
655 CompileUnit::DIEInfo &MyInfo,
656 const DebugMapObject &DMO,
657 CompileUnit &CU, unsigned Flags) {
658 const DWARFUnit &Unit = CU.getOrigUnit();
659 MyInfo.Keep = true;
660
661 // First mark all the parent chain as kept.
662 unsigned AncestorIdx = MyInfo.ParentIdx;
663 while (!CU.getInfo(AncestorIdx).Keep) {
664 lookForDIEsToKeep(*Unit.getDIEAtIndex(AncestorIdx), DMO, CU,
665 TF_ParentWalk | TF_Keep | TF_DependencyWalk);
666 AncestorIdx = CU.getInfo(AncestorIdx).ParentIdx;
667 }
668
669 // Then we need to mark all the DIEs referenced by this DIE's
670 // attributes as kept.
671 DataExtractor Data = Unit.getDebugInfoExtractor();
672 const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
673 uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
674
675 // Mark all DIEs referenced through atttributes as kept.
676 for (const auto &AttrSpec : Abbrev->attributes()) {
677 DWARFFormValue Val(AttrSpec.Form);
678
679 if (!Val.isFormClass(DWARFFormValue::FC_Reference)) {
680 DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset, &Unit);
681 continue;
682 }
683
684 Val.extractValue(Data, &Offset, &Unit);
685 CompileUnit *ReferencedCU;
686 if (const auto *RefDIE = resolveDIEReference(Val, Unit, DIE, ReferencedCU))
687 lookForDIEsToKeep(*RefDIE, DMO, *ReferencedCU,
688 TF_Keep | TF_DependencyWalk);
689 }
690}
691
692/// \brief Recursively walk the \p DIE tree and look for DIEs to
693/// keep. Store that information in \p CU's DIEInfo.
694///
695/// This function is the entry point of the DIE selection
696/// algorithm. It is expected to walk the DIE tree in file order and
697/// (though the mediation of its helper) call hasValidRelocation() on
698/// each DIE that might be a 'root DIE' (See DwarfLinker class
699/// comment).
700/// While walking the dependencies of root DIEs, this function is
701/// also called, but during these dependency walks the file order is
702/// not respected. The TF_DependencyWalk flag tells us which kind of
703/// traversal we are currently doing.
704void DwarfLinker::lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE,
705 const DebugMapObject &DMO, CompileUnit &CU,
706 unsigned Flags) {
707 unsigned Idx = CU.getOrigUnit().getDIEIndex(&DIE);
708 CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
709 bool AlreadyKept = MyInfo.Keep;
710
711 // If the Keep flag is set, we are marking a required DIE's
712 // dependencies. If our target is already marked as kept, we're all
713 // set.
714 if ((Flags & TF_DependencyWalk) && AlreadyKept)
715 return;
716
717 // We must not call shouldKeepDIE while called from keepDIEAndDenpendencies,
718 // because it would screw up the relocation finding logic.
719 if (!(Flags & TF_DependencyWalk))
720 Flags = shouldKeepDIE(DIE, CU, MyInfo, Flags);
721
722 // If it is a newly kept DIE mark it as well as all its dependencies as kept.
723 if (!AlreadyKept && (Flags & TF_Keep))
724 keepDIEAndDenpendencies(DIE, MyInfo, DMO, CU, Flags);
725
726 // The TF_ParentWalk flag tells us that we are currently walking up
727 // the parent chain of a required DIE, and we don't want to mark all
728 // the children of the parents as kept (consider for example a
729 // DW_TAG_namespace node in the parent chain). There are however a
730 // set of DIE types for which we want to ignore that directive and still
731 // walk their children.
732 if (dieNeedsChildrenToBeMeaningful(DIE.getTag()))
733 Flags &= ~TF_ParentWalk;
734
735 if (!DIE.hasChildren() || (Flags & TF_ParentWalk))
736 return;
737
738 for (auto *Child = DIE.getFirstChild(); Child && !Child->isNULL();
739 Child = Child->getSibling())
740 lookForDIEsToKeep(*Child, DMO, CU, Flags);
741}
742
Frederic Rissd3455182015-01-28 18:27:01 +0000743bool DwarfLinker::link(const DebugMap &Map) {
744
745 if (Map.begin() == Map.end()) {
746 errs() << "Empty debug map.\n";
747 return false;
748 }
749
Frederic Rissc99ea202015-02-28 00:29:11 +0000750 if (!createStreamer(Map.getTriple(), OutputFilename))
751 return false;
752
Frederic Rissd3455182015-01-28 18:27:01 +0000753 for (const auto &Obj : Map.objects()) {
Frederic Riss1b9da422015-02-13 23:18:29 +0000754 CurrentDebugObject = Obj.get();
755
Frederic Rissb9818322015-02-28 00:29:07 +0000756 if (Options.Verbose)
Frederic Rissd3455182015-01-28 18:27:01 +0000757 outs() << "DEBUG MAP OBJECT: " << Obj->getObjectFilename() << "\n";
758 auto ErrOrObj = BinHolder.GetObjectFile(Obj->getObjectFilename());
759 if (std::error_code EC = ErrOrObj.getError()) {
Frederic Riss1b9da422015-02-13 23:18:29 +0000760 reportWarning(Twine(Obj->getObjectFilename()) + ": " + EC.message());
Frederic Rissd3455182015-01-28 18:27:01 +0000761 continue;
762 }
763
Frederic Riss1036e642015-02-13 23:18:22 +0000764 // Look for relocations that correspond to debug map entries.
765 if (!findValidRelocsInDebugInfo(*ErrOrObj, *Obj)) {
Frederic Rissb9818322015-02-28 00:29:07 +0000766 if (Options.Verbose)
Frederic Riss1036e642015-02-13 23:18:22 +0000767 outs() << "No valid relocations found. Skipping.\n";
768 continue;
769 }
770
Frederic Riss563cba62015-01-28 22:15:14 +0000771 // Setup access to the debug info.
Frederic Rissd3455182015-01-28 18:27:01 +0000772 DWARFContextInMemory DwarfContext(*ErrOrObj);
Frederic Riss563cba62015-01-28 22:15:14 +0000773 startDebugObject(DwarfContext);
Frederic Rissd3455182015-01-28 18:27:01 +0000774
Frederic Riss563cba62015-01-28 22:15:14 +0000775 // In a first phase, just read in the debug info and store the DIE
776 // parent links that we will use during the next phase.
Frederic Rissd3455182015-01-28 18:27:01 +0000777 for (const auto &CU : DwarfContext.compile_units()) {
778 auto *CUDie = CU->getCompileUnitDIE(false);
Frederic Rissb9818322015-02-28 00:29:07 +0000779 if (Options.Verbose) {
Frederic Rissd3455182015-01-28 18:27:01 +0000780 outs() << "Input compilation unit:";
781 CUDie->dump(outs(), CU.get(), 0);
782 }
Frederic Riss563cba62015-01-28 22:15:14 +0000783 Units.emplace_back(*CU);
Frederic Riss9aa725b2015-02-13 23:18:31 +0000784 gatherDIEParents(CUDie, 0, Units.back());
Frederic Rissd3455182015-01-28 18:27:01 +0000785 }
Frederic Riss563cba62015-01-28 22:15:14 +0000786
Frederic Riss84c09a52015-02-13 23:18:34 +0000787 // Then mark all the DIEs that need to be present in the linked
788 // output and collect some information about them. Note that this
789 // loop can not be merged with the previous one becaue cross-cu
790 // references require the ParentIdx to be setup for every CU in
791 // the object file before calling this.
792 for (auto &CurrentUnit : Units)
793 lookForDIEsToKeep(*CurrentUnit.getOrigUnit().getCompileUnitDIE(), *Obj,
794 CurrentUnit, 0);
795
Frederic Riss563cba62015-01-28 22:15:14 +0000796 // Clean-up before starting working on the next object.
797 endDebugObject();
Frederic Rissd3455182015-01-28 18:27:01 +0000798 }
799
Frederic Rissc99ea202015-02-28 00:29:11 +0000800 return Options.NoOutput ? true : Streamer->finish();
Frederic Riss231f7142014-12-12 17:31:24 +0000801}
802}
Frederic Rissd3455182015-01-28 18:27:01 +0000803
Frederic Rissb9818322015-02-28 00:29:07 +0000804bool linkDwarf(StringRef OutputFilename, const DebugMap &DM,
805 const LinkOptions &Options) {
806 DwarfLinker Linker(OutputFilename, Options);
Frederic Rissd3455182015-01-28 18:27:01 +0000807 return Linker.link(DM);
808}
809}
Frederic Riss231f7142014-12-12 17:31:24 +0000810}