1 #include "llvm/ADT/STLExtras.h"
2 #include "llvm/ADT/StringSet.h"
3 #include "llvm/CodeGen/AsmPrinter.h"
4 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
5 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
6 #include "llvm/MC/MCAsmInfo.h"
7 #include "llvm/MC/MCContext.h"
8 #include "llvm/MC/MCInstrInfo.h"
9 #include "llvm/MC/MCObjectFileInfo.h"
10 #include "llvm/MC/MCRegisterInfo.h"
11 #include "llvm/MC/MCSectionELF.h"
12 #include "llvm/MC/MCStreamer.h"
13 #include "llvm/MC/MCTargetOptionsCommandFlags.h"
14 #include "llvm/Object/ObjectFile.h"
15 #include "llvm/Support/DataExtractor.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/Support/MathExtras.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/Options.h"
20 #include "llvm/Support/TargetRegistry.h"
21 #include "llvm/Support/TargetSelect.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Target/TargetMachine.h"
26 #include <unordered_set>
29 using namespace llvm::object;
32 OptionCategory DwpCategory("Specific Options");
33 static list<std::string> InputFiles(Positional, OneOrMore,
34 desc("<input files>"), cat(DwpCategory));
36 static opt<std::string> OutputFilename(Required, "o",
37 desc("Specify the output file."),
38 value_desc("filename"),
41 static int error(const Twine &Error, const Twine &Context) {
42 errs() << Twine("while processing ") + Context + ":\n";
43 errs() << Twine("error: ") + Error + "\n";
47 static std::error_code
48 writeStringsAndOffsets(MCStreamer &Out, StringMap<uint32_t> &Strings,
49 uint32_t &StringOffset, MCSection *StrSection,
50 MCSection *StrOffsetSection, StringRef CurStrSection,
51 StringRef CurStrOffsetSection) {
52 // Could possibly produce an error or warning if one of these was non-null but
53 // the other was null.
54 if (CurStrSection.empty() || CurStrOffsetSection.empty())
55 return std::error_code();
57 DenseMap<uint32_t, uint32_t> OffsetRemapping;
59 DataExtractor Data(CurStrSection, true, 0);
60 uint32_t LocalOffset = 0;
61 uint32_t PrevOffset = 0;
62 while (const char *s = Data.getCStr(&LocalOffset)) {
63 StringRef Str(s, LocalOffset - PrevOffset - 1);
64 auto Pair = Strings.insert(std::make_pair(Str, StringOffset));
66 Out.SwitchSection(StrSection);
68 StringRef(Pair.first->getKeyData(), Pair.first->getKeyLength() + 1));
69 StringOffset += Str.size() + 1;
71 OffsetRemapping[PrevOffset] = Pair.first->second;
72 PrevOffset = LocalOffset;
75 Data = DataExtractor(CurStrOffsetSection, true, 0);
77 Out.SwitchSection(StrOffsetSection);
80 uint64_t Size = CurStrOffsetSection.size();
81 while (Offset < Size) {
82 auto OldOffset = Data.getU32(&Offset);
83 auto NewOffset = OffsetRemapping[OldOffset];
84 Out.EmitIntValue(NewOffset, 4);
87 return std::error_code();
90 static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
93 DataExtractor AbbrevData(Abbrev, true, 0);
94 while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) {
96 AbbrevData.getULEB128(&Offset);
98 AbbrevData.getU8(&Offset);
100 while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset))
106 static uint64_t getCUSignature(StringRef Abbrev, StringRef Info) {
108 DataExtractor InfoData(Info, true, 0);
109 InfoData.getU32(&Offset); // Length
110 uint16_t Version = InfoData.getU16(&Offset);
111 InfoData.getU32(&Offset); // Abbrev offset (should be zero)
112 uint8_t AddrSize = InfoData.getU8(&Offset);
114 uint32_t AbbrCode = InfoData.getULEB128(&Offset);
116 DataExtractor AbbrevData(Abbrev, true, 0);
117 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
118 uint64_t Tag = AbbrevData.getULEB128(&AbbrevOffset);
120 // FIXME: Real error handling
121 assert(Tag == dwarf::DW_TAG_compile_unit);
123 AbbrevData.getU8(&AbbrevOffset);
126 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |
127 (Form = AbbrevData.getULEB128(&AbbrevOffset)) &&
128 Name != dwarf::DW_AT_GNU_dwo_id) {
129 DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize);
131 // FIXME: Real error handling
132 assert(Name == dwarf::DW_AT_GNU_dwo_id);
133 return InfoData.getU64(&Offset);
136 struct UnitIndexEntry {
138 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
141 static void addAllTypes(MCStreamer &Out,
142 std::vector<UnitIndexEntry> &TypeIndexEntries,
143 MCSection *OutputTypes, StringRef Types,
144 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) {
148 Out.SwitchSection(OutputTypes);
150 DataExtractor Data(Types, true, 0);
151 while (Data.isValidOffset(Offset)) {
152 UnitIndexEntry Entry = CUEntry;
153 // Zero out the debug_info contribution
154 Entry.Contributions[0] = {};
155 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
156 C.Offset = TypesOffset;
157 auto PrevOffset = Offset;
158 // Length of the unit, including the 4 byte length field.
159 C.Length = Data.getU32(&Offset) + 4;
161 Data.getU16(&Offset); // Version
162 Data.getU32(&Offset); // Abbrev offset
163 Data.getU8(&Offset); // Address size
164 Entry.Signature = Data.getU64(&Offset);
165 Offset = PrevOffset + C.Length;
167 if (any_of(TypeIndexEntries, [&](const UnitIndexEntry &E) {
168 return E.Signature == Entry.Signature;
172 Out.EmitBytes(Types.substr(PrevOffset, C.Length));
173 TypesOffset += C.Length;
175 TypeIndexEntries.push_back(Entry);
180 writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,
181 ArrayRef<UnitIndexEntry> IndexEntries,
182 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) {
183 for (const auto &E : IndexEntries)
184 for (size_t i = 0; i != array_lengthof(E.Contributions); ++i)
185 if (ContributionOffsets[i])
186 Out.EmitIntValue(E.Contributions[i].*Field, 4);
189 static void writeIndex(MCStreamer &Out, MCSection *Section,
190 ArrayRef<unsigned> ContributionOffsets,
191 ArrayRef<UnitIndexEntry> IndexEntries) {
192 unsigned Columns = 0;
193 for (auto &C : ContributionOffsets)
197 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
198 uint64_t Mask = Buckets.size() - 1;
199 for (size_t i = 0; i != IndexEntries.size(); ++i) {
200 auto S = IndexEntries[i].Signature;
203 assert(S != IndexEntries[Buckets[H] - 1].Signature &&
204 "Duplicate type unit");
205 H += ((S >> 32) & Mask) | 1;
210 Out.SwitchSection(Section);
211 Out.EmitIntValue(2, 4); // Version
212 Out.EmitIntValue(Columns, 4); // Columns
213 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
214 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets
216 // Write the signatures.
217 for (const auto &I : Buckets)
218 Out.EmitIntValue(I ? IndexEntries[I - 1].Signature : 0, 8);
220 // Write the indexes.
221 for (const auto &I : Buckets)
222 Out.EmitIntValue(I, 4);
224 // Write the column headers (which sections will appear in the table)
225 for (size_t i = 0; i != ContributionOffsets.size(); ++i)
226 if (ContributionOffsets[i])
227 Out.EmitIntValue(i + DW_SECT_INFO, 4);
229 // Write the offsets.
230 writeIndexTable(Out, ContributionOffsets, IndexEntries,
231 &DWARFUnitIndex::Entry::SectionContribution::Offset);
233 // Write the lengths.
234 writeIndexTable(Out, ContributionOffsets, IndexEntries,
235 &DWARFUnitIndex::Entry::SectionContribution::Length);
237 static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
238 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
239 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
240 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
241 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
242 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
243 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
244 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
245 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
246 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
247 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
248 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
249 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}}};
251 std::vector<UnitIndexEntry> IndexEntries;
252 std::vector<UnitIndexEntry> TypeIndexEntries;
254 StringMap<uint32_t> Strings;
255 uint32_t StringOffset = 0;
257 uint32_t ContributionOffsets[8] = {};
259 for (const auto &Input : Inputs) {
260 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
262 return ErrOrObj.getError();
264 IndexEntries.emplace_back();
265 UnitIndexEntry &CurEntry = IndexEntries.back();
267 StringRef CurStrSection;
268 StringRef CurStrOffsetSection;
269 StringRef CurTypesSection;
270 StringRef InfoSection;
271 StringRef AbbrevSection;
273 for (const auto &Section : ErrOrObj->getBinary()->sections()) {
275 if (std::error_code Err = Section.getName(Name))
279 KnownSections.find(Name.substr(Name.find_first_not_of("._")));
280 if (SectionPair == KnownSections.end())
284 if (auto Err = Section.getContents(Contents))
287 if (DWARFSectionKind Kind = SectionPair->second.second) {
288 auto Index = Kind - DW_SECT_INFO;
289 if (Kind != DW_SECT_TYPES) {
290 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
291 ContributionOffsets[Index] +=
292 (CurEntry.Contributions[Index].Length = Contents.size());
297 InfoSection = Contents;
300 AbbrevSection = Contents;
307 MCSection *OutSection = SectionPair->second.first;
308 if (OutSection == StrOffsetSection)
309 CurStrOffsetSection = Contents;
310 else if (OutSection == StrSection)
311 CurStrSection = Contents;
312 else if (OutSection == TypesSection)
313 CurTypesSection = Contents;
315 Out.SwitchSection(OutSection);
316 Out.EmitBytes(Contents);
320 assert(!AbbrevSection.empty());
321 assert(!InfoSection.empty());
322 CurEntry.Signature = getCUSignature(AbbrevSection, InfoSection);
323 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection, CurEntry,
324 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
326 if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset,
327 StrSection, StrOffsetSection,
328 CurStrSection, CurStrOffsetSection))
332 if (!TypeIndexEntries.empty()) {
333 // Lie about there being no info contributions so the TU index only includes
334 // the type unit contribution
335 ContributionOffsets[0] = 0;
336 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
340 // Lie about the type contribution
341 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
342 // Unlie about the info contribution
343 ContributionOffsets[0] = 1;
345 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
348 return std::error_code();
351 int main(int argc, char **argv) {
353 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
355 llvm::InitializeAllTargetInfos();
356 llvm::InitializeAllTargetMCs();
357 llvm::InitializeAllTargets();
358 llvm::InitializeAllAsmPrinters();
360 std::string ErrorStr;
361 StringRef Context = "dwarf streamer init";
363 Triple TheTriple("x86_64-linux-gnu");
366 const Target *TheTarget =
367 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
369 return error(ErrorStr, Context);
370 std::string TripleName = TheTriple.getTriple();
372 // Create all the MC Objects.
373 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
375 return error(Twine("no register info for target ") + TripleName, Context);
377 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
379 return error("no asm info for target " + TripleName, Context);
381 MCObjectFileInfo MOFI;
382 MCContext MC(MAI.get(), MRI.get(), &MOFI);
383 MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default, MC);
385 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
387 return error("no asm backend for target " + TripleName, Context);
389 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
391 return error("no instr info info for target " + TripleName, Context);
393 std::unique_ptr<MCSubtargetInfo> MSTI(
394 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
396 return error("no subtarget info for target " + TripleName, Context);
398 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
400 return error("no code emitter for target " + TripleName, Context);
402 // Create the output file.
404 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
406 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
408 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
409 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
410 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, MCOptions.MCRelaxAll,
411 MCOptions.MCIncrementalLinkerCompatible,
412 /*DWARFMustBeAtTheEnd*/ false));
414 return error("no object streamer for target " + TripleName, Context);
416 if (auto Err = write(*MS, InputFiles))
417 return error(Err.message(), "Writing DWP file");