[dsymutil] Add DwarfLinker class.
[oota-llvm.git] / tools / dsymutil / DwarfLinker.cpp
1 //===- tools/dsymutil/DwarfLinker.cpp - Dwarf debug info linker -----------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #include "DebugMap.h"
10
11 #include "BinaryHolder.h"
12 #include "DebugMap.h"
13 #include "dsymutil.h"
14 #include "llvm/DebugInfo/DWARFContext.h"
15 #include "llvm/DebugInfo/DWARFDebugInfoEntry.h"
16 #include <string>
17
18 namespace llvm {
19 namespace dsymutil {
20
21 namespace {
22
23 /// \brief The core of the Dwarf linking logic.
24 class DwarfLinker {
25 public:
26   DwarfLinker(StringRef OutputFilename, bool Verbose)
27       : OutputFilename(OutputFilename), Verbose(Verbose), BinHolder(Verbose) {}
28
29   /// \brief Link the contents of the DebugMap.
30   bool link(const DebugMap &);
31
32 private:
33   std::string OutputFilename;
34   bool Verbose;
35   BinaryHolder BinHolder;
36 };
37
38 bool DwarfLinker::link(const DebugMap &Map) {
39
40   if (Map.begin() == Map.end()) {
41     errs() << "Empty debug map.\n";
42     return false;
43   }
44
45   for (const auto &Obj : Map.objects()) {
46     if (Verbose)
47       outs() << "DEBUG MAP OBJECT: " << Obj->getObjectFilename() << "\n";
48     auto ErrOrObj = BinHolder.GetObjectFile(Obj->getObjectFilename());
49     if (std::error_code EC = ErrOrObj.getError()) {
50       errs() << Obj->getObjectFilename() << ": " << EC.message() << "\n";
51       continue;
52     }
53
54     DWARFContextInMemory DwarfContext(*ErrOrObj);
55
56     for (const auto &CU : DwarfContext.compile_units()) {
57       auto *CUDie = CU->getCompileUnitDIE(false);
58       if (Verbose) {
59         outs() << "Input compilation unit:";
60         CUDie->dump(outs(), CU.get(), 0);
61       }
62     }
63   }
64
65   return true;
66 }
67 }
68
69 bool linkDwarf(StringRef OutputFilename, const DebugMap &DM, bool Verbose) {
70   DwarfLinker Linker(OutputFilename, Verbose);
71   return Linker.link(DM);
72 }
73 }
74 }