blob: 5d95a751f71523588d0f59dc776b44e608b5c339 [file] [log] [blame]
David Blaikie242b9482015-12-01 00:48:39 +00001#include "llvm/ADT/STLExtras.h"
2#include "llvm/ADT/StringSet.h"
3#include "llvm/CodeGen/AsmPrinter.h"
David Blaikie2ed678c2015-12-05 03:06:30 +00004#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
5#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
David Blaikie242b9482015-12-01 00:48:39 +00006#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/Object/ObjectFile.h"
David Blaikie98ad82a2015-12-01 18:07:07 +000014#include "llvm/Support/DataExtractor.h"
David Blaikie242b9482015-12-01 00:48:39 +000015#include "llvm/Support/FileSystem.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000016#include "llvm/Support/MathExtras.h"
David Blaikie242b9482015-12-01 00:48:39 +000017#include "llvm/Support/MemoryBuffer.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000018#include "llvm/Support/Options.h"
David Blaikie242b9482015-12-01 00:48:39 +000019#include "llvm/Support/TargetRegistry.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000020#include "llvm/Support/TargetSelect.h"
David Blaikie242b9482015-12-01 00:48:39 +000021#include "llvm/Support/raw_ostream.h"
22#include "llvm/Target/TargetMachine.h"
David Blaikie242b9482015-12-01 00:48:39 +000023#include <list>
David Blaikie2ed678c2015-12-05 03:06:30 +000024#include <memory>
David Blaikie242b9482015-12-01 00:48:39 +000025#include <unordered_set>
26
27using namespace llvm;
David Blaikie98ad82a2015-12-01 18:07:07 +000028using namespace llvm::object;
David Blaikie242b9482015-12-01 00:48:39 +000029using namespace cl;
30
31OptionCategory DwpCategory("Specific Options");
32static list<std::string> InputFiles(Positional, OneOrMore,
33 desc("<input files>"), cat(DwpCategory));
34
David Blaikie2ed678c2015-12-05 03:06:30 +000035static opt<std::string> OutputFilename(Required, "o",
36 desc("Specify the output file."),
37 value_desc("filename"),
38 cat(DwpCategory));
David Blaikie242b9482015-12-01 00:48:39 +000039
40static int error(const Twine &Error, const Twine &Context) {
41 errs() << Twine("while processing ") + Context + ":\n";
42 errs() << Twine("error: ") + Error + "\n";
43 return 1;
44}
45
David Blaikie98ad82a2015-12-01 18:07:07 +000046static std::error_code
47writeStringsAndOffsets(MCStreamer &Out, StringMap<uint32_t> &Strings,
David Blaikiebb94e442015-12-01 19:17:58 +000048 uint32_t &StringOffset, MCSection *StrSection,
49 MCSection *StrOffsetSection, StringRef CurStrSection,
50 StringRef CurStrOffsetSection) {
David Blaikie98ad82a2015-12-01 18:07:07 +000051 // Could possibly produce an error or warning if one of these was non-null but
52 // the other was null.
53 if (CurStrSection.empty() || CurStrOffsetSection.empty())
54 return std::error_code();
55
56 DenseMap<uint32_t, uint32_t> OffsetRemapping;
57
58 DataExtractor Data(CurStrSection, true, 0);
59 uint32_t LocalOffset = 0;
60 uint32_t PrevOffset = 0;
61 while (const char *s = Data.getCStr(&LocalOffset)) {
62 StringRef Str(s, LocalOffset - PrevOffset - 1);
David Blaikiebb94e442015-12-01 19:17:58 +000063 auto Pair = Strings.insert(std::make_pair(Str, StringOffset));
64 if (Pair.second) {
65 Out.SwitchSection(StrSection);
66 Out.EmitBytes(
67 StringRef(Pair.first->getKeyData(), Pair.first->getKeyLength() + 1));
68 StringOffset += Str.size() + 1;
69 }
70 OffsetRemapping[PrevOffset] = Pair.first->second;
David Blaikie98ad82a2015-12-01 18:07:07 +000071 PrevOffset = LocalOffset;
72 }
73
74 Data = DataExtractor(CurStrOffsetSection, true, 0);
75
76 Out.SwitchSection(StrOffsetSection);
77
78 uint32_t Offset = 0;
79 uint64_t Size = CurStrOffsetSection.size();
80 while (Offset < Size) {
81 auto OldOffset = Data.getU32(&Offset);
82 auto NewOffset = OffsetRemapping[OldOffset];
83 Out.EmitIntValue(NewOffset, 4);
84 }
85
David Blaikie242b9482015-12-01 00:48:39 +000086 return std::error_code();
87}
88
David Blaikiead07b5d2015-12-04 17:20:04 +000089static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
90 uint64_t CurCode;
91 uint32_t Offset = 0;
92 DataExtractor AbbrevData(Abbrev, true, 0);
93 while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) {
94 // Tag
95 AbbrevData.getULEB128(&Offset);
96 // DW_CHILDREN
97 AbbrevData.getU8(&Offset);
98 // Attributes
99 while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset))
100 ;
101 }
102 return Offset;
103}
104
105static uint64_t getCUSignature(StringRef Abbrev, StringRef Info) {
106 uint32_t Offset = 0;
107 DataExtractor InfoData(Info, true, 0);
108 InfoData.getU32(&Offset); // Length
109 uint16_t Version = InfoData.getU16(&Offset);
110 InfoData.getU32(&Offset); // Abbrev offset (should be zero)
111 uint8_t AddrSize = InfoData.getU8(&Offset);
112
113 uint32_t AbbrCode = InfoData.getULEB128(&Offset);
114
115 DataExtractor AbbrevData(Abbrev, true, 0);
116 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
117 uint64_t Tag = AbbrevData.getULEB128(&AbbrevOffset);
118 (void)Tag;
119 // FIXME: Real error handling
120 assert(Tag == dwarf::DW_TAG_compile_unit);
121 // DW_CHILDREN
122 AbbrevData.getU8(&AbbrevOffset);
123 uint32_t Name;
124 uint32_t Form;
125 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |
126 (Form = AbbrevData.getULEB128(&AbbrevOffset)) &&
127 Name != dwarf::DW_AT_GNU_dwo_id) {
128 DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize);
129 }
130 // FIXME: Real error handling
131 assert(Name == dwarf::DW_AT_GNU_dwo_id);
132 return InfoData.getU64(&Offset);
133}
134
David Blaikie24c8ac92015-12-05 03:05:45 +0000135struct UnitIndexEntry {
136 uint64_t Signature;
137 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
138};
139
David Blaikiec3826da2015-12-09 21:02:33 +0000140static void addAllTypes(MCStreamer &Out,
141 std::vector<UnitIndexEntry> &TypeIndexEntries,
142 MCSection *OutputTypes, StringRef Types,
143 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) {
144 if (Types.empty())
145 return;
146
147 Out.SwitchSection(OutputTypes);
David Blaikie24c8ac92015-12-05 03:05:45 +0000148 uint32_t Offset = 0;
149 DataExtractor Data(Types, true, 0);
150 while (Data.isValidOffset(Offset)) {
151 TypeIndexEntries.push_back(CUEntry);
152 auto &Entry = TypeIndexEntries.back();
153 // Zero out the debug_info contribution
154 Entry.Contributions[0] = {};
155 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
David Blaikiec3826da2015-12-09 21:02:33 +0000156 C.Offset = TypesOffset + Offset;
David Blaikie24c8ac92015-12-05 03:05:45 +0000157 auto PrevOffset = Offset;
158 // Length of the unit, including the 4 byte length field.
159 C.Length = Data.getU32(&Offset) + 4;
160
David Blaikiec3826da2015-12-09 21:02:33 +0000161 Out.EmitBytes(Types.substr(Offset - 4, C.Length));
162 TypesOffset += C.Length;
163
David Blaikie24c8ac92015-12-05 03:05:45 +0000164 Data.getU16(&Offset); // Version
165 Data.getU32(&Offset); // Abbrev offset
166 Data.getU8(&Offset); // Address size
167 Entry.Signature = Data.getU64(&Offset);
168 Offset = PrevOffset + C.Length;
169 }
170}
171
172static void
173writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,
174 ArrayRef<UnitIndexEntry> IndexEntries,
175 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) {
176 for (const auto &E : IndexEntries)
177 for (size_t i = 0; i != array_lengthof(E.Contributions); ++i)
178 if (ContributionOffsets[i])
179 Out.EmitIntValue(E.Contributions[i].*Field, 4);
180}
181
182static void writeIndex(MCStreamer &Out, MCSection *Section,
183 ArrayRef<unsigned> ContributionOffsets,
184 ArrayRef<UnitIndexEntry> IndexEntries) {
185 unsigned Columns = 0;
186 for (auto &C : ContributionOffsets)
187 if (C)
188 ++Columns;
189
190 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
191 uint64_t Mask = Buckets.size() - 1;
192 for (size_t i = 0; i != IndexEntries.size(); ++i) {
193 auto S = IndexEntries[i].Signature;
194 auto H = S & Mask;
David Blaikiec3826da2015-12-09 21:02:33 +0000195 while (Buckets[H]) {
196 assert(S != IndexEntries[Buckets[H] - 1].Signature &&
197 "Duplicate type unit");
David Blaikie24c8ac92015-12-05 03:05:45 +0000198 H += ((S >> 32) & Mask) | 1;
David Blaikiec3826da2015-12-09 21:02:33 +0000199 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000200 Buckets[H] = i + 1;
201 }
202
203 Out.SwitchSection(Section);
204 Out.EmitIntValue(2, 4); // Version
205 Out.EmitIntValue(Columns, 4); // Columns
206 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
David Blaikie2ed678c2015-12-05 03:06:30 +0000207 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets
David Blaikie24c8ac92015-12-05 03:05:45 +0000208
209 // Write the signatures.
210 for (const auto &I : Buckets)
211 Out.EmitIntValue(I ? IndexEntries[I - 1].Signature : 0, 8);
212
213 // Write the indexes.
214 for (const auto &I : Buckets)
215 Out.EmitIntValue(I, 4);
216
217 // Write the column headers (which sections will appear in the table)
218 for (size_t i = 0; i != ContributionOffsets.size(); ++i)
219 if (ContributionOffsets[i])
220 Out.EmitIntValue(i + DW_SECT_INFO, 4);
221
222 // Write the offsets.
223 writeIndexTable(Out, ContributionOffsets, IndexEntries,
224 &DWARFUnitIndex::Entry::SectionContribution::Offset);
225
226 // Write the lengths.
227 writeIndexTable(Out, ContributionOffsets, IndexEntries,
228 &DWARFUnitIndex::Entry::SectionContribution::Length);
229}
David Blaikie242b9482015-12-01 00:48:39 +0000230static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000231 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
232 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
233 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikiec3826da2015-12-09 21:02:33 +0000234 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000235 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
236 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
237 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
238 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
239 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
240 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
David Blaikieb7020252015-12-04 21:16:42 +0000241 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikieb073cb92015-12-02 06:21:34 +0000242 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}}};
243
David Blaikieb073cb92015-12-02 06:21:34 +0000244 std::vector<UnitIndexEntry> IndexEntries;
David Blaikie24c8ac92015-12-05 03:05:45 +0000245 std::vector<UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000246
247 StringMap<uint32_t> Strings;
248 uint32_t StringOffset = 0;
249
David Blaikieb073cb92015-12-02 06:21:34 +0000250 uint32_t ContributionOffsets[8] = {};
251
David Blaikie242b9482015-12-01 00:48:39 +0000252 for (const auto &Input : Inputs) {
253 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
254 if (!ErrOrObj)
255 return ErrOrObj.getError();
David Blaikieb073cb92015-12-02 06:21:34 +0000256
257 IndexEntries.emplace_back();
258 UnitIndexEntry &CurEntry = IndexEntries.back();
David Blaikieb073cb92015-12-02 06:21:34 +0000259
David Blaikie98ad82a2015-12-01 18:07:07 +0000260 StringRef CurStrSection;
261 StringRef CurStrOffsetSection;
David Blaikiec3826da2015-12-09 21:02:33 +0000262 StringRef CurTypesSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000263 StringRef InfoSection;
264 StringRef AbbrevSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000265
266 for (const auto &Section : ErrOrObj->getBinary()->sections()) {
David Blaikie242b9482015-12-01 00:48:39 +0000267 StringRef Name;
268 if (std::error_code Err = Section.getName(Name))
269 return Err;
David Blaikieb073cb92015-12-02 06:21:34 +0000270
271 auto SectionPair =
272 KnownSections.find(Name.substr(Name.find_first_not_of("._")));
273 if (SectionPair == KnownSections.end())
274 continue;
275
276 StringRef Contents;
277 if (auto Err = Section.getContents(Contents))
278 return Err;
279
280 if (DWARFSectionKind Kind = SectionPair->second.second) {
281 auto Index = Kind - DW_SECT_INFO;
David Blaikiec3826da2015-12-09 21:02:33 +0000282 if (Kind != DW_SECT_TYPES) {
283 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
284 ContributionOffsets[Index] +=
285 (CurEntry.Contributions[Index].Length = Contents.size());
286 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000287
David Blaikie24c8ac92015-12-05 03:05:45 +0000288 switch (Kind) {
289 case DW_SECT_INFO:
David Blaikiead07b5d2015-12-04 17:20:04 +0000290 InfoSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000291 break;
292 case DW_SECT_ABBREV:
David Blaikiead07b5d2015-12-04 17:20:04 +0000293 AbbrevSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000294 break;
David Blaikie24c8ac92015-12-05 03:05:45 +0000295 default:
296 break;
David Blaikiead07b5d2015-12-04 17:20:04 +0000297 }
David Blaikieb073cb92015-12-02 06:21:34 +0000298 }
299
300 MCSection *OutSection = SectionPair->second.first;
301 if (OutSection == StrOffsetSection)
302 CurStrOffsetSection = Contents;
303 else if (OutSection == StrSection)
304 CurStrSection = Contents;
David Blaikiec3826da2015-12-09 21:02:33 +0000305 else if (OutSection == TypesSection)
306 CurTypesSection = Contents;
David Blaikieb073cb92015-12-02 06:21:34 +0000307 else {
308 Out.SwitchSection(OutSection);
309 Out.EmitBytes(Contents);
David Blaikie98ad82a2015-12-01 18:07:07 +0000310 }
David Blaikie242b9482015-12-01 00:48:39 +0000311 }
David Blaikieb073cb92015-12-02 06:21:34 +0000312
David Blaikiead07b5d2015-12-04 17:20:04 +0000313 assert(!AbbrevSection.empty());
314 assert(!InfoSection.empty());
315 CurEntry.Signature = getCUSignature(AbbrevSection, InfoSection);
David Blaikiec3826da2015-12-09 21:02:33 +0000316 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection, CurEntry,
317 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
David Blaikiead07b5d2015-12-04 17:20:04 +0000318
David Blaikiebb94e442015-12-01 19:17:58 +0000319 if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset,
320 StrSection, StrOffsetSection,
321 CurStrSection, CurStrOffsetSection))
David Blaikie98ad82a2015-12-01 18:07:07 +0000322 return Err;
David Blaikie242b9482015-12-01 00:48:39 +0000323 }
David Blaikieb073cb92015-12-02 06:21:34 +0000324
David Blaikie9e51c842015-12-05 03:41:53 +0000325 if (!TypeIndexEntries.empty()) {
326 // Lie about there being no info contributions so the TU index only includes
327 // the type unit contribution
328 ContributionOffsets[0] = 0;
329 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
330 TypeIndexEntries);
331 }
David Blaikieb3757c02015-12-02 22:01:56 +0000332
David Blaikie24c8ac92015-12-05 03:05:45 +0000333 // Lie about the type contribution
334 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
335 // Unlie about the info contribution
336 ContributionOffsets[0] = 1;
David Blaikie7c4ffe02015-12-04 21:30:23 +0000337
David Blaikie24c8ac92015-12-05 03:05:45 +0000338 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
339 IndexEntries);
David Blaikieb073cb92015-12-02 06:21:34 +0000340
David Blaikie242b9482015-12-01 00:48:39 +0000341 return std::error_code();
342}
343
David Blaikie2ed678c2015-12-05 03:06:30 +0000344int main(int argc, char **argv) {
David Blaikie242b9482015-12-01 00:48:39 +0000345
346 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
347
348 llvm::InitializeAllTargetInfos();
349 llvm::InitializeAllTargetMCs();
350 llvm::InitializeAllTargets();
351 llvm::InitializeAllAsmPrinters();
352
353 std::string ErrorStr;
354 StringRef Context = "dwarf streamer init";
355
356 Triple TheTriple("x86_64-linux-gnu");
357
358 // Get the target.
359 const Target *TheTarget =
360 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
361 if (!TheTarget)
362 return error(ErrorStr, Context);
363 std::string TripleName = TheTriple.getTriple();
364
365 // Create all the MC Objects.
366 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
367 if (!MRI)
368 return error(Twine("no register info for target ") + TripleName, Context);
369
370 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
371 if (!MAI)
372 return error("no asm info for target " + TripleName, Context);
373
374 MCObjectFileInfo MOFI;
375 MCContext MC(MAI.get(), MRI.get(), &MOFI);
David Blaikie2ed678c2015-12-05 03:06:30 +0000376 MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default, MC);
David Blaikie242b9482015-12-01 00:48:39 +0000377
378 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
379 if (!MAB)
380 return error("no asm backend for target " + TripleName, Context);
381
382 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
383 if (!MII)
384 return error("no instr info info for target " + TripleName, Context);
385
386 std::unique_ptr<MCSubtargetInfo> MSTI(
387 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
388 if (!MSTI)
389 return error("no subtarget info for target " + TripleName, Context);
390
391 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
392 if (!MCE)
393 return error("no code emitter for target " + TripleName, Context);
394
395 // Create the output file.
396 std::error_code EC;
397 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
398 if (EC)
399 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
400
401 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
402 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, false,
403 /*DWARFMustBeAtTheEnd*/ false));
404 if (!MS)
405 return error("no object streamer for target " + TripleName, Context);
406
407 if (auto Err = write(*MS, InputFiles))
408 return error(Err.message(), "Writing DWP file");
409
410 MS->Finish();
David Blaikiedf055252015-12-01 00:48:34 +0000411}