[llvm-dwp] Initial partial prototype
[oota-llvm.git] / tools / llvm-dwp / llvm-dwp.cpp
1 #include "llvm/ADT/STLExtras.h"
2 #include "llvm/ADT/StringSet.h"
3 #include "llvm/CodeGen/AsmPrinter.h"
4 #include "llvm/MC/MCAsmInfo.h"
5 #include "llvm/MC/MCContext.h"
6 #include "llvm/MC/MCInstrInfo.h"
7 #include "llvm/MC/MCObjectFileInfo.h"
8 #include "llvm/MC/MCRegisterInfo.h"
9 #include "llvm/MC/MCSectionELF.h"
10 #include "llvm/MC/MCStreamer.h"
11 #include "llvm/Object/ObjectFile.h"
12 #include "llvm/Support/Options.h"
13 #include "llvm/Support/FileSystem.h"
14 #include "llvm/Support/MemoryBuffer.h"
15 #include "llvm/Support/TargetRegistry.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include "llvm/Target/TargetMachine.h"
18 #include "llvm/Support/TargetSelect.h"
19 #include <memory>
20 #include <list>
21 #include <unordered_set>
22
23 using namespace llvm;
24 using namespace cl;
25
26 OptionCategory DwpCategory("Specific Options");
27 static list<std::string> InputFiles(Positional, OneOrMore,
28                                     desc("<input files>"), cat(DwpCategory));
29
30 static opt<std::string> OutputFilename(Required, "o", desc("Specify the output file."),
31                                       value_desc("filename"), cat(DwpCategory));
32
33 static int error(const Twine &Error, const Twine &Context) {
34   errs() << Twine("while processing ") + Context + ":\n";
35   errs() << Twine("error: ") + Error + "\n";
36   return 1;
37 }
38
39 static std::error_code writeSection(MCStreamer &Out, MCSection *OutSection,
40                                     const object::SectionRef &Sym) {
41   StringRef Contents;
42   if (auto Err = Sym.getContents(Contents))
43     return Err;
44   Out.SwitchSection(OutSection);
45   Out.EmitBytes(Contents);
46   return std::error_code();
47 }
48
49 static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
50   for (const auto &Input : Inputs) {
51     auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
52     if (!ErrOrObj)
53       return ErrOrObj.getError();
54     const auto *Obj = ErrOrObj->getBinary();
55     for (const auto &Section : Obj->sections()) {
56       const auto &MCOFI = *Out.getContext().getObjectFileInfo();
57       static const StringMap<MCSection *> KnownSections = {
58           {"debug_info.dwo", MCOFI.getDwarfInfoDWOSection()},
59           {"debug_types.dwo", MCOFI.getDwarfTypesDWOSection()},
60           {"debug_str_offsets.dwo", MCOFI.getDwarfStrOffDWOSection()},
61           {"debug_str.dwo", MCOFI.getDwarfStrDWOSection()},
62           {"debug_loc.dwo", MCOFI.getDwarfLocDWOSection()},
63           {"debug_abbrev.dwo", MCOFI.getDwarfAbbrevDWOSection()}};
64       StringRef Name;
65       if (std::error_code Err = Section.getName(Name))
66         return Err;
67       if (MCSection *OutSection =
68               KnownSections.lookup(Name.substr(Name.find_first_not_of("._"))))
69         if (auto Err = writeSection(Out, OutSection, Section))
70           return Err;
71     }
72   }
73   return std::error_code();
74 }
75
76 int main(int argc, char** argv) {
77
78   ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
79
80   llvm::InitializeAllTargetInfos();
81   llvm::InitializeAllTargetMCs();
82   llvm::InitializeAllTargets();
83   llvm::InitializeAllAsmPrinters();
84
85   std::string ErrorStr;
86   StringRef Context = "dwarf streamer init";
87
88   Triple TheTriple("x86_64-linux-gnu");
89
90   // Get the target.
91   const Target *TheTarget =
92       TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
93   if (!TheTarget)
94     return error(ErrorStr, Context);
95   std::string TripleName = TheTriple.getTriple();
96
97   // Create all the MC Objects.
98   std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
99   if (!MRI)
100     return error(Twine("no register info for target ") + TripleName, Context);
101
102   std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
103   if (!MAI)
104     return error("no asm info for target " + TripleName, Context);
105
106   MCObjectFileInfo MOFI;
107   MCContext MC(MAI.get(), MRI.get(), &MOFI);
108   MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default,
109                              MC);
110
111   auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
112   if (!MAB)
113     return error("no asm backend for target " + TripleName, Context);
114
115   std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
116   if (!MII)
117     return error("no instr info info for target " + TripleName, Context);
118
119   std::unique_ptr<MCSubtargetInfo> MSTI(
120       TheTarget->createMCSubtargetInfo(TripleName, "", ""));
121   if (!MSTI)
122     return error("no subtarget info for target " + TripleName, Context);
123
124   MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
125   if (!MCE)
126     return error("no code emitter for target " + TripleName, Context);
127
128   // Create the output file.
129   std::error_code EC;
130   raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
131   if (EC)
132     return error(Twine(OutputFilename) + ": " + EC.message(), Context);
133
134   std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
135       TheTriple, MC, *MAB, OutFile, MCE, *MSTI, false,
136       /*DWARFMustBeAtTheEnd*/ false));
137   if (!MS)
138     return error("no object streamer for target " + TripleName, Context);
139
140   if (auto Err = write(*MS, InputFiles))
141     return error(Err.message(), "Writing DWP file");
142
143   MS->Finish();
144 }