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/DataExtractor.h"
13 #include "llvm/Support/Options.h"
14 #include "llvm/Support/FileSystem.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include "llvm/Support/TargetRegistry.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/Target/TargetMachine.h"
19 #include "llvm/Support/TargetSelect.h"
22 #include <unordered_set>
25 using namespace llvm::object;
28 OptionCategory DwpCategory("Specific Options");
29 static list<std::string> InputFiles(Positional, OneOrMore,
30 desc("<input files>"), cat(DwpCategory));
32 static opt<std::string> OutputFilename(Required, "o", desc("Specify the output file."),
33 value_desc("filename"), cat(DwpCategory));
35 static int error(const Twine &Error, const Twine &Context) {
36 errs() << Twine("while processing ") + Context + ":\n";
37 errs() << Twine("error: ") + Error + "\n";
41 static std::error_code
42 writeStringsAndOffsets(MCStreamer &Out, StringMap<uint32_t> &Strings,
43 uint32_t &StringOffset, MCSection *StrSection,
44 MCSection *StrOffsetSection, StringRef CurStrSection,
45 StringRef CurStrOffsetSection) {
46 // Could possibly produce an error or warning if one of these was non-null but
47 // the other was null.
48 if (CurStrSection.empty() || CurStrOffsetSection.empty())
49 return std::error_code();
51 DenseMap<uint32_t, uint32_t> OffsetRemapping;
53 DataExtractor Data(CurStrSection, true, 0);
54 uint32_t LocalOffset = 0;
55 uint32_t PrevOffset = 0;
56 while (const char *s = Data.getCStr(&LocalOffset)) {
57 StringRef Str(s, LocalOffset - PrevOffset - 1);
58 auto Pair = Strings.insert(std::make_pair(Str, StringOffset));
60 Out.SwitchSection(StrSection);
62 StringRef(Pair.first->getKeyData(), Pair.first->getKeyLength() + 1));
63 StringOffset += Str.size() + 1;
65 OffsetRemapping[PrevOffset] = Pair.first->second;
66 PrevOffset = LocalOffset;
69 Data = DataExtractor(CurStrOffsetSection, true, 0);
71 Out.SwitchSection(StrOffsetSection);
74 uint64_t Size = CurStrOffsetSection.size();
75 while (Offset < Size) {
76 auto OldOffset = Data.getU32(&Offset);
77 auto NewOffset = OffsetRemapping[OldOffset];
78 Out.EmitIntValue(NewOffset, 4);
81 return std::error_code();
84 static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
85 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
86 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
87 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
88 const StringMap<MCSection *> KnownSections = {
89 {"debug_info.dwo", MCOFI.getDwarfInfoDWOSection()},
90 {"debug_types.dwo", MCOFI.getDwarfTypesDWOSection()},
91 {"debug_str_offsets.dwo", StrOffsetSection},
92 {"debug_str.dwo", StrSection},
93 {"debug_loc.dwo", MCOFI.getDwarfLocDWOSection()},
94 {"debug_abbrev.dwo", MCOFI.getDwarfAbbrevDWOSection()}};
96 StringMap<uint32_t> Strings;
97 uint32_t StringOffset = 0;
99 for (const auto &Input : Inputs) {
100 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
102 return ErrOrObj.getError();
103 const auto *Obj = ErrOrObj->getBinary();
104 StringRef CurStrSection;
105 StringRef CurStrOffsetSection;
106 for (const auto &Section : Obj->sections()) {
108 if (std::error_code Err = Section.getName(Name))
110 if (MCSection *OutSection =
111 KnownSections.lookup(Name.substr(Name.find_first_not_of("._")))) {
113 if (auto Err = Section.getContents(Contents))
115 if (OutSection == StrOffsetSection)
116 CurStrOffsetSection = Contents;
117 else if (OutSection == StrSection)
118 CurStrSection = Contents;
120 Out.SwitchSection(OutSection);
121 Out.EmitBytes(Contents);
125 if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset,
126 StrSection, StrOffsetSection,
127 CurStrSection, CurStrOffsetSection))
130 return std::error_code();
133 int main(int argc, char** argv) {
135 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
137 llvm::InitializeAllTargetInfos();
138 llvm::InitializeAllTargetMCs();
139 llvm::InitializeAllTargets();
140 llvm::InitializeAllAsmPrinters();
142 std::string ErrorStr;
143 StringRef Context = "dwarf streamer init";
145 Triple TheTriple("x86_64-linux-gnu");
148 const Target *TheTarget =
149 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
151 return error(ErrorStr, Context);
152 std::string TripleName = TheTriple.getTriple();
154 // Create all the MC Objects.
155 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
157 return error(Twine("no register info for target ") + TripleName, Context);
159 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
161 return error("no asm info for target " + TripleName, Context);
163 MCObjectFileInfo MOFI;
164 MCContext MC(MAI.get(), MRI.get(), &MOFI);
165 MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default,
168 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
170 return error("no asm backend for target " + TripleName, Context);
172 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
174 return error("no instr info info for target " + TripleName, Context);
176 std::unique_ptr<MCSubtargetInfo> MSTI(
177 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
179 return error("no subtarget info for target " + TripleName, Context);
181 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
183 return error("no code emitter for target " + TripleName, Context);
185 // Create the output file.
187 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
189 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
191 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
192 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, false,
193 /*DWARFMustBeAtTheEnd*/ false));
195 return error("no object streamer for target " + TripleName, Context);
197 if (auto Err = write(*MS, InputFiles))
198 return error(Err.message(), "Writing DWP file");