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"
20 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
23 #include <unordered_set>
26 using namespace llvm::object;
29 OptionCategory DwpCategory("Specific Options");
30 static list<std::string> InputFiles(Positional, OneOrMore,
31 desc("<input files>"), cat(DwpCategory));
33 static opt<std::string> OutputFilename(Required, "o", desc("Specify the output file."),
34 value_desc("filename"), cat(DwpCategory));
36 static int error(const Twine &Error, const Twine &Context) {
37 errs() << Twine("while processing ") + Context + ":\n";
38 errs() << Twine("error: ") + Error + "\n";
42 static std::error_code
43 writeStringsAndOffsets(MCStreamer &Out, StringMap<uint32_t> &Strings,
44 uint32_t &StringOffset, MCSection *StrSection,
45 MCSection *StrOffsetSection, StringRef CurStrSection,
46 StringRef CurStrOffsetSection) {
47 // Could possibly produce an error or warning if one of these was non-null but
48 // the other was null.
49 if (CurStrSection.empty() || CurStrOffsetSection.empty())
50 return std::error_code();
52 DenseMap<uint32_t, uint32_t> OffsetRemapping;
54 DataExtractor Data(CurStrSection, true, 0);
55 uint32_t LocalOffset = 0;
56 uint32_t PrevOffset = 0;
57 while (const char *s = Data.getCStr(&LocalOffset)) {
58 StringRef Str(s, LocalOffset - PrevOffset - 1);
59 auto Pair = Strings.insert(std::make_pair(Str, StringOffset));
61 Out.SwitchSection(StrSection);
63 StringRef(Pair.first->getKeyData(), Pair.first->getKeyLength() + 1));
64 StringOffset += Str.size() + 1;
66 OffsetRemapping[PrevOffset] = Pair.first->second;
67 PrevOffset = LocalOffset;
70 Data = DataExtractor(CurStrOffsetSection, true, 0);
72 Out.SwitchSection(StrOffsetSection);
75 uint64_t Size = CurStrOffsetSection.size();
76 while (Offset < Size) {
77 auto OldOffset = Data.getU32(&Offset);
78 auto NewOffset = OffsetRemapping[OldOffset];
79 Out.EmitIntValue(NewOffset, 4);
82 return std::error_code();
85 static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
86 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
87 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
88 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
89 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
90 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
91 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
92 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
93 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
94 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
95 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}}};
97 struct UnitIndexEntry {
99 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
102 std::vector<UnitIndexEntry> IndexEntries;
104 StringMap<uint32_t> Strings;
105 uint32_t StringOffset = 0;
107 uint64_t UnitIndex = 0;
108 uint32_t ContributionOffsets[8] = {};
110 for (const auto &Input : Inputs) {
111 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
113 return ErrOrObj.getError();
115 IndexEntries.emplace_back();
116 UnitIndexEntry &CurEntry = IndexEntries.back();
117 CurEntry.Signature = UnitIndex++;
119 StringRef CurStrSection;
120 StringRef CurStrOffsetSection;
122 for (const auto &Section : ErrOrObj->getBinary()->sections()) {
124 if (std::error_code Err = Section.getName(Name))
128 KnownSections.find(Name.substr(Name.find_first_not_of("._")));
129 if (SectionPair == KnownSections.end())
133 if (auto Err = Section.getContents(Contents))
136 if (DWARFSectionKind Kind = SectionPair->second.second) {
137 auto Index = Kind - DW_SECT_INFO;
138 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
139 ContributionOffsets[Index] +=
140 (CurEntry.Contributions[Index].Length = Contents.size());
143 MCSection *OutSection = SectionPair->second.first;
144 if (OutSection == StrOffsetSection)
145 CurStrOffsetSection = Contents;
146 else if (OutSection == StrSection)
147 CurStrSection = Contents;
149 Out.SwitchSection(OutSection);
150 Out.EmitBytes(Contents);
154 if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset,
155 StrSection, StrOffsetSection,
156 CurStrSection, CurStrOffsetSection))
160 unsigned Columns = 0;
161 for (auto &C : ContributionOffsets)
165 Out.SwitchSection(MCOFI.getDwarfCUIndexSection());
166 Out.EmitIntValue(2, 4); // Version
167 Out.EmitIntValue(Columns, 4); // Columns
168 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
169 // FIXME: This is not the right number of buckets for a real hash.
170 Out.EmitIntValue(IndexEntries.size(), 4); // Num Buckets
172 // Write the signatures.
173 for (const auto &E : IndexEntries)
174 Out.EmitIntValue(E.Signature, 8);
176 // Write the indexes.
177 for (size_t i = 0; i != IndexEntries.size(); ++i)
178 Out.EmitIntValue(i + 1, 4);
180 // Write the column headers (which sections will appear in the table)
181 for (size_t i = 0; i != array_lengthof(ContributionOffsets); ++i)
182 if (ContributionOffsets[i])
183 Out.EmitIntValue(i + DW_SECT_INFO, 4);
185 // Write the offsets.
186 for (const auto &E : IndexEntries)
187 for (size_t i = 0; i != array_lengthof(E.Contributions); ++i)
188 if (ContributionOffsets[i])
189 Out.EmitIntValue(E.Contributions[i].Offset, 4);
191 // Write the lengths.
192 for (const auto &E : IndexEntries)
193 for (size_t i = 0; i != array_lengthof(E.Contributions); ++i)
194 if (ContributionOffsets[i])
195 Out.EmitIntValue(E.Contributions[i].Length, 4);
197 return std::error_code();
200 int main(int argc, char** argv) {
202 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
204 llvm::InitializeAllTargetInfos();
205 llvm::InitializeAllTargetMCs();
206 llvm::InitializeAllTargets();
207 llvm::InitializeAllAsmPrinters();
209 std::string ErrorStr;
210 StringRef Context = "dwarf streamer init";
212 Triple TheTriple("x86_64-linux-gnu");
215 const Target *TheTarget =
216 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
218 return error(ErrorStr, Context);
219 std::string TripleName = TheTriple.getTriple();
221 // Create all the MC Objects.
222 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
224 return error(Twine("no register info for target ") + TripleName, Context);
226 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
228 return error("no asm info for target " + TripleName, Context);
230 MCObjectFileInfo MOFI;
231 MCContext MC(MAI.get(), MRI.get(), &MOFI);
232 MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default,
235 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
237 return error("no asm backend for target " + TripleName, Context);
239 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
241 return error("no instr info info for target " + TripleName, Context);
243 std::unique_ptr<MCSubtargetInfo> MSTI(
244 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
246 return error("no subtarget info for target " + TripleName, Context);
248 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
250 return error("no code emitter for target " + TripleName, Context);
252 // Create the output file.
254 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
256 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
258 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
259 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, false,
260 /*DWARFMustBeAtTheEnd*/ false));
262 return error("no object streamer for target " + TripleName, Context);
264 if (auto Err = write(*MS, InputFiles))
265 return error(Err.message(), "Writing DWP file");