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"
21 #include <unordered_set>
26 OptionCategory DwpCategory("Specific Options");
27 static list<std::string> InputFiles(Positional, OneOrMore,
28 desc("<input files>"), cat(DwpCategory));
30 static opt<std::string> OutputFilename(Required, "o", desc("Specify the output file."),
31 value_desc("filename"), cat(DwpCategory));
33 static int error(const Twine &Error, const Twine &Context) {
34 errs() << Twine("while processing ") + Context + ":\n";
35 errs() << Twine("error: ") + Error + "\n";
39 static std::error_code writeSection(MCStreamer &Out, MCSection *OutSection,
40 const object::SectionRef &Sym) {
42 if (auto Err = Sym.getContents(Contents))
44 Out.SwitchSection(OutSection);
45 Out.EmitBytes(Contents);
46 return std::error_code();
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);
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()}};
65 if (std::error_code Err = Section.getName(Name))
67 if (MCSection *OutSection =
68 KnownSections.lookup(Name.substr(Name.find_first_not_of("._"))))
69 if (auto Err = writeSection(Out, OutSection, Section))
73 return std::error_code();
76 int main(int argc, char** argv) {
78 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
80 llvm::InitializeAllTargetInfos();
81 llvm::InitializeAllTargetMCs();
82 llvm::InitializeAllTargets();
83 llvm::InitializeAllAsmPrinters();
86 StringRef Context = "dwarf streamer init";
88 Triple TheTriple("x86_64-linux-gnu");
91 const Target *TheTarget =
92 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
94 return error(ErrorStr, Context);
95 std::string TripleName = TheTriple.getTriple();
97 // Create all the MC Objects.
98 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
100 return error(Twine("no register info for target ") + TripleName, Context);
102 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
104 return error("no asm info for target " + TripleName, Context);
106 MCObjectFileInfo MOFI;
107 MCContext MC(MAI.get(), MRI.get(), &MOFI);
108 MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default,
111 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
113 return error("no asm backend for target " + TripleName, Context);
115 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
117 return error("no instr info info for target " + TripleName, Context);
119 std::unique_ptr<MCSubtargetInfo> MSTI(
120 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
122 return error("no subtarget info for target " + TripleName, Context);
124 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
126 return error("no code emitter for target " + TripleName, Context);
128 // Create the output file.
130 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
132 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
134 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
135 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, false,
136 /*DWARFMustBeAtTheEnd*/ false));
138 return error("no object streamer for target " + TripleName, Context);
140 if (auto Err = write(*MS, InputFiles))
141 return error(Err.message(), "Writing DWP file");