blob: 737e396ed74c37928009da5ee0e117ad623f0678 [file] [log] [blame]
David Blaikief72dbc12016-03-01 22:29:00 +00001//===-- llvm-dwp.cpp - Split DWARF merging tool for llvm ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// A utility for merging DWARF 5 Split DWARF .dwo files into .dwp (DWARF
11// package files).
12//
13//===----------------------------------------------------------------------===//
David Blaikie852c02b2016-02-19 21:09:26 +000014#include "llvm/ADT/MapVector.h"
David Blaikie242b9482015-12-01 00:48:39 +000015#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/StringSet.h"
17#include "llvm/CodeGen/AsmPrinter.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000018#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
19#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
David Blaikie242b9482015-12-01 00:48:39 +000020#include "llvm/MC/MCAsmInfo.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCInstrInfo.h"
23#include "llvm/MC/MCObjectFileInfo.h"
24#include "llvm/MC/MCRegisterInfo.h"
25#include "llvm/MC/MCSectionELF.h"
26#include "llvm/MC/MCStreamer.h"
David Majnemer03e2cc32015-12-21 22:09:27 +000027#include "llvm/MC/MCTargetOptionsCommandFlags.h"
David Blaikie242b9482015-12-01 00:48:39 +000028#include "llvm/Object/ObjectFile.h"
David Blaikie74f5b282016-02-19 01:51:44 +000029#include "llvm/Support/Compression.h"
David Blaikie98ad82a2015-12-01 18:07:07 +000030#include "llvm/Support/DataExtractor.h"
David Blaikie242b9482015-12-01 00:48:39 +000031#include "llvm/Support/FileSystem.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000032#include "llvm/Support/MathExtras.h"
David Blaikie242b9482015-12-01 00:48:39 +000033#include "llvm/Support/MemoryBuffer.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000034#include "llvm/Support/Options.h"
David Blaikie242b9482015-12-01 00:48:39 +000035#include "llvm/Support/TargetRegistry.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000036#include "llvm/Support/TargetSelect.h"
David Blaikie242b9482015-12-01 00:48:39 +000037#include "llvm/Support/raw_ostream.h"
38#include "llvm/Target/TargetMachine.h"
David Blaikie242b9482015-12-01 00:48:39 +000039#include <list>
David Blaikief1958da2016-02-26 07:30:15 +000040#include <iostream>
David Blaikie2ed678c2015-12-05 03:06:30 +000041#include <memory>
David Blaikie23919372016-02-06 01:15:26 +000042#include <system_error>
David Blaikie852c02b2016-02-19 21:09:26 +000043#include <unordered_set>
David Blaikie242b9482015-12-01 00:48:39 +000044
45using namespace llvm;
David Blaikie98ad82a2015-12-01 18:07:07 +000046using namespace llvm::object;
David Blaikie242b9482015-12-01 00:48:39 +000047using namespace cl;
48
49OptionCategory DwpCategory("Specific Options");
50static list<std::string> InputFiles(Positional, OneOrMore,
51 desc("<input files>"), cat(DwpCategory));
52
David Blaikie2ed678c2015-12-05 03:06:30 +000053static opt<std::string> OutputFilename(Required, "o",
54 desc("Specify the output file."),
55 value_desc("filename"),
56 cat(DwpCategory));
David Blaikie242b9482015-12-01 00:48:39 +000057
58static int error(const Twine &Error, const Twine &Context) {
59 errs() << Twine("while processing ") + Context + ":\n";
60 errs() << Twine("error: ") + Error + "\n";
61 return 1;
62}
63
David Blaikie98ad82a2015-12-01 18:07:07 +000064static std::error_code
65writeStringsAndOffsets(MCStreamer &Out, StringMap<uint32_t> &Strings,
David Blaikiebb94e442015-12-01 19:17:58 +000066 uint32_t &StringOffset, MCSection *StrSection,
67 MCSection *StrOffsetSection, StringRef CurStrSection,
68 StringRef CurStrOffsetSection) {
David Blaikie98ad82a2015-12-01 18:07:07 +000069 // Could possibly produce an error or warning if one of these was non-null but
70 // the other was null.
71 if (CurStrSection.empty() || CurStrOffsetSection.empty())
72 return std::error_code();
73
74 DenseMap<uint32_t, uint32_t> OffsetRemapping;
75
76 DataExtractor Data(CurStrSection, true, 0);
77 uint32_t LocalOffset = 0;
78 uint32_t PrevOffset = 0;
79 while (const char *s = Data.getCStr(&LocalOffset)) {
80 StringRef Str(s, LocalOffset - PrevOffset - 1);
David Blaikiebb94e442015-12-01 19:17:58 +000081 auto Pair = Strings.insert(std::make_pair(Str, StringOffset));
82 if (Pair.second) {
83 Out.SwitchSection(StrSection);
84 Out.EmitBytes(
85 StringRef(Pair.first->getKeyData(), Pair.first->getKeyLength() + 1));
86 StringOffset += Str.size() + 1;
87 }
88 OffsetRemapping[PrevOffset] = Pair.first->second;
David Blaikie98ad82a2015-12-01 18:07:07 +000089 PrevOffset = LocalOffset;
90 }
91
92 Data = DataExtractor(CurStrOffsetSection, true, 0);
93
94 Out.SwitchSection(StrOffsetSection);
95
96 uint32_t Offset = 0;
97 uint64_t Size = CurStrOffsetSection.size();
98 while (Offset < Size) {
99 auto OldOffset = Data.getU32(&Offset);
100 auto NewOffset = OffsetRemapping[OldOffset];
101 Out.EmitIntValue(NewOffset, 4);
102 }
103
David Blaikie242b9482015-12-01 00:48:39 +0000104 return std::error_code();
105}
106
David Blaikiead07b5d2015-12-04 17:20:04 +0000107static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
108 uint64_t CurCode;
109 uint32_t Offset = 0;
110 DataExtractor AbbrevData(Abbrev, true, 0);
111 while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) {
112 // Tag
113 AbbrevData.getULEB128(&Offset);
114 // DW_CHILDREN
115 AbbrevData.getU8(&Offset);
116 // Attributes
117 while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset))
118 ;
119 }
120 return Offset;
121}
122
David Blaikiece7c6cf2016-03-24 22:17:08 +0000123struct CompileUnitIdentifiers {
124 uint64_t Signature = 0;
David Blaikie4dd03f02016-03-26 20:32:14 +0000125 const char *Name = "";
126 const char *DWOName = "";
David Blaikiece7c6cf2016-03-24 22:17:08 +0000127};
128
David Blaikie4dd03f02016-03-26 20:32:14 +0000129static const char *getIndexedString(uint32_t StrIndex, StringRef StrOffsets,
130 StringRef Str) {
131 DataExtractor StrOffsetsData(StrOffsets, true, 0);
132 uint32_t StrOffsetsOffset = 4 * StrIndex;
133 uint32_t StrOffset = StrOffsetsData.getU32(&StrOffsetsOffset);
134 DataExtractor StrData(Str, true, 0);
135 return StrData.getCStr(&StrOffset);
136}
137
David Blaikiece7c6cf2016-03-24 22:17:08 +0000138static CompileUnitIdentifiers getCUIdentifiers(StringRef Abbrev, StringRef Info,
139 StringRef StrOffsets,
140 StringRef Str) {
David Blaikief1958da2016-02-26 07:30:15 +0000141 uint32_t Offset = 0;
142 DataExtractor InfoData(Info, true, 0);
143 InfoData.getU32(&Offset); // Length
144 uint16_t Version = InfoData.getU16(&Offset);
145 InfoData.getU32(&Offset); // Abbrev offset (should be zero)
146 uint8_t AddrSize = InfoData.getU8(&Offset);
147
148 uint32_t AbbrCode = InfoData.getULEB128(&Offset);
149
150 DataExtractor AbbrevData(Abbrev, true, 0);
151 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
152 uint64_t Tag = AbbrevData.getULEB128(&AbbrevOffset);
153 (void)Tag;
154 // FIXME: Real error handling
155 assert(Tag == dwarf::DW_TAG_compile_unit);
156 // DW_CHILDREN
157 AbbrevData.getU8(&AbbrevOffset);
158 uint32_t Name;
159 uint32_t Form;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000160 CompileUnitIdentifiers ID;
David Blaikief1958da2016-02-26 07:30:15 +0000161 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |
162 (Form = AbbrevData.getULEB128(&AbbrevOffset)) &&
David Blaikiece7c6cf2016-03-24 22:17:08 +0000163 (Name != 0 || Form != 0)) {
164 switch (Name) {
165 case dwarf::DW_AT_name: {
David Blaikie4dd03f02016-03-26 20:32:14 +0000166 ID.Name = getIndexedString(InfoData.getULEB128(&Offset), StrOffsets, Str);
167 break;
168 }
169 case dwarf::DW_AT_GNU_dwo_name: {
170 ID.DWOName =
171 getIndexedString(InfoData.getULEB128(&Offset), StrOffsets, Str);
David Blaikiece7c6cf2016-03-24 22:17:08 +0000172 break;
173 }
174 case dwarf::DW_AT_GNU_dwo_id:
175 ID.Signature = InfoData.getU64(&Offset);
176 break;
177 default:
178 DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize);
179 }
David Blaikief1958da2016-02-26 07:30:15 +0000180 }
David Blaikiece7c6cf2016-03-24 22:17:08 +0000181 return ID;
David Blaikiead07b5d2015-12-04 17:20:04 +0000182}
183
David Blaikie24c8ac92015-12-05 03:05:45 +0000184struct UnitIndexEntry {
David Blaikie24c8ac92015-12-05 03:05:45 +0000185 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
David Blaikief1958da2016-02-26 07:30:15 +0000186 std::string Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000187 std::string DWOName;
David Blaikief1958da2016-02-26 07:30:15 +0000188 StringRef DWPName;
David Blaikie24c8ac92015-12-05 03:05:45 +0000189};
190
David Blaikief1958da2016-02-26 07:30:15 +0000191StringRef getSubsection(StringRef Section, const DWARFUnitIndex::Entry &Entry, DWARFSectionKind Kind) {
192 const auto *Off = Entry.getOffset(Kind);
193 if (!Off)
194 return StringRef();
195 return Section.substr(Off->Offset, Off->Length);
196}
197
David Blaikie852c02b2016-02-19 21:09:26 +0000198static void addAllTypesFromDWP(
199 MCStreamer &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
200 const DWARFUnitIndex &TUIndex, MCSection *OutputTypes, StringRef Types,
201 const UnitIndexEntry &TUEntry, uint32_t &TypesOffset) {
David Blaikie8bce5a02016-02-17 07:00:24 +0000202 Out.SwitchSection(OutputTypes);
203 for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) {
204 auto *I = E.getOffsets();
205 if (!I)
206 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000207 auto P = TypeIndexEntries.insert(std::make_pair(E.getSignature(), TUEntry));
208 if (!P.second)
David Blaikie8bce5a02016-02-17 07:00:24 +0000209 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000210 auto &Entry = P.first->second;
David Blaikie8bce5a02016-02-17 07:00:24 +0000211 // Zero out the debug_info contribution
212 Entry.Contributions[0] = {};
213 for (auto Kind : TUIndex.getColumnKinds()) {
214 auto &C = Entry.Contributions[Kind - DW_SECT_INFO];
215 C.Offset += I->Offset;
216 C.Length = I->Length;
217 ++I;
218 }
219 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
220 Out.EmitBytes(Types.substr(
221 C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset,
222 C.Length));
223 C.Offset = TypesOffset;
224 TypesOffset += C.Length;
David Blaikie8bce5a02016-02-17 07:00:24 +0000225 }
226}
227
David Blaikiec3826da2015-12-09 21:02:33 +0000228static void addAllTypes(MCStreamer &Out,
David Blaikie852c02b2016-02-19 21:09:26 +0000229 MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
David Blaikiec3826da2015-12-09 21:02:33 +0000230 MCSection *OutputTypes, StringRef Types,
231 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) {
232 if (Types.empty())
233 return;
234
235 Out.SwitchSection(OutputTypes);
David Blaikie24c8ac92015-12-05 03:05:45 +0000236 uint32_t Offset = 0;
237 DataExtractor Data(Types, true, 0);
238 while (Data.isValidOffset(Offset)) {
David Blaikief5cb6272015-12-14 07:42:00 +0000239 UnitIndexEntry Entry = CUEntry;
David Blaikie24c8ac92015-12-05 03:05:45 +0000240 // Zero out the debug_info contribution
241 Entry.Contributions[0] = {};
242 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
David Blaikief5cb6272015-12-14 07:42:00 +0000243 C.Offset = TypesOffset;
David Blaikie24c8ac92015-12-05 03:05:45 +0000244 auto PrevOffset = Offset;
245 // Length of the unit, including the 4 byte length field.
246 C.Length = Data.getU32(&Offset) + 4;
247
248 Data.getU16(&Offset); // Version
249 Data.getU32(&Offset); // Abbrev offset
250 Data.getU8(&Offset); // Address size
David Blaikie852c02b2016-02-19 21:09:26 +0000251 auto Signature = Data.getU64(&Offset);
David Blaikie24c8ac92015-12-05 03:05:45 +0000252 Offset = PrevOffset + C.Length;
David Blaikief5cb6272015-12-14 07:42:00 +0000253
David Blaikie852c02b2016-02-19 21:09:26 +0000254 auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry));
255 if (!P.second)
David Blaikief5cb6272015-12-14 07:42:00 +0000256 continue;
257
258 Out.EmitBytes(Types.substr(PrevOffset, C.Length));
259 TypesOffset += C.Length;
David Blaikie24c8ac92015-12-05 03:05:45 +0000260 }
261}
262
263static void
264writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,
David Blaikie852c02b2016-02-19 21:09:26 +0000265 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries,
David Blaikie24c8ac92015-12-05 03:05:45 +0000266 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) {
267 for (const auto &E : IndexEntries)
David Blaikie852c02b2016-02-19 21:09:26 +0000268 for (size_t i = 0; i != array_lengthof(E.second.Contributions); ++i)
David Blaikie24c8ac92015-12-05 03:05:45 +0000269 if (ContributionOffsets[i])
David Blaikie852c02b2016-02-19 21:09:26 +0000270 Out.EmitIntValue(E.second.Contributions[i].*Field, 4);
David Blaikie24c8ac92015-12-05 03:05:45 +0000271}
272
David Blaikie852c02b2016-02-19 21:09:26 +0000273static void
274writeIndex(MCStreamer &Out, MCSection *Section,
275 ArrayRef<unsigned> ContributionOffsets,
276 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries) {
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000277 if (IndexEntries.empty())
278 return;
279
David Blaikie24c8ac92015-12-05 03:05:45 +0000280 unsigned Columns = 0;
281 for (auto &C : ContributionOffsets)
282 if (C)
283 ++Columns;
284
285 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
286 uint64_t Mask = Buckets.size() - 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000287 size_t i = 0;
288 for (const auto &P : IndexEntries) {
289 auto S = P.first;
David Blaikie24c8ac92015-12-05 03:05:45 +0000290 auto H = S & Mask;
David Blaikie9b492562016-04-05 17:51:40 +0000291 auto HP = ((S >> 32) & Mask) | 1;
David Blaikiec3826da2015-12-09 21:02:33 +0000292 while (Buckets[H]) {
David Blaikie852c02b2016-02-19 21:09:26 +0000293 assert(S != IndexEntries.begin()[Buckets[H] - 1].first &&
David Blaikie74f5b282016-02-19 01:51:44 +0000294 "Duplicate unit");
David Blaikie9b492562016-04-05 17:51:40 +0000295 H = (H + HP) & Mask;
David Blaikiec3826da2015-12-09 21:02:33 +0000296 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000297 Buckets[H] = i + 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000298 ++i;
David Blaikie24c8ac92015-12-05 03:05:45 +0000299 }
300
301 Out.SwitchSection(Section);
302 Out.EmitIntValue(2, 4); // Version
303 Out.EmitIntValue(Columns, 4); // Columns
304 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
David Blaikie2ed678c2015-12-05 03:06:30 +0000305 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets
David Blaikie24c8ac92015-12-05 03:05:45 +0000306
307 // Write the signatures.
308 for (const auto &I : Buckets)
David Blaikie852c02b2016-02-19 21:09:26 +0000309 Out.EmitIntValue(I ? IndexEntries.begin()[I - 1].first : 0, 8);
David Blaikie24c8ac92015-12-05 03:05:45 +0000310
311 // Write the indexes.
312 for (const auto &I : Buckets)
313 Out.EmitIntValue(I, 4);
314
315 // Write the column headers (which sections will appear in the table)
316 for (size_t i = 0; i != ContributionOffsets.size(); ++i)
317 if (ContributionOffsets[i])
318 Out.EmitIntValue(i + DW_SECT_INFO, 4);
319
320 // Write the offsets.
321 writeIndexTable(Out, ContributionOffsets, IndexEntries,
322 &DWARFUnitIndex::Entry::SectionContribution::Offset);
323
324 // Write the lengths.
325 writeIndexTable(Out, ContributionOffsets, IndexEntries,
326 &DWARFUnitIndex::Entry::SectionContribution::Length);
327}
David Blaikie74f5b282016-02-19 01:51:44 +0000328static bool consumeCompressedDebugSectionHeader(StringRef &data,
329 uint64_t &OriginalSize) {
330 // Consume "ZLIB" prefix.
331 if (!data.startswith("ZLIB"))
332 return false;
333 data = data.substr(4);
334 // Consume uncompressed section size (big-endian 8 bytes).
335 DataExtractor extractor(data, false, 8);
336 uint32_t Offset = 0;
337 OriginalSize = extractor.getU64(&Offset);
338 if (Offset == 0)
339 return false;
340 data = data.substr(Offset);
341 return true;
342}
343
David Blaikie4dd03f02016-03-26 20:32:14 +0000344void printDuplicateError(const std::pair<uint64_t, UnitIndexEntry> &PrevE,
345 const CompileUnitIdentifiers &ID, StringRef DWPName) {
346 errs() << "Duplicate DWO ID (" << PrevE.first << ") in '" << PrevE.second.Name
347 << '\'';
348 if (!PrevE.second.DWPName.empty()) {
349 errs() << " (from ";
350 if (!PrevE.second.DWOName.empty())
351 errs() << '\'' << PrevE.second.DWOName << "' in ";
352 errs() << "'" << PrevE.second.DWPName.str() << "')";
353 }
354 errs() << " and '" << ID.Name << '\'';
355 if (!DWPName.empty()) {
356 errs() << " (from ";
357 if (*ID.DWOName)
358 errs() << '\'' << ID.DWOName << "\' in ";
359 errs() << '\'' << DWPName << "')";
360 }
361 errs() << '\n';
362}
David Blaikie242b9482015-12-01 00:48:39 +0000363static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000364 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
365 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
366 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikiec3826da2015-12-09 21:02:33 +0000367 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
David Blaikie23919372016-02-06 01:15:26 +0000368 MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection();
David Blaikie8bce5a02016-02-17 07:00:24 +0000369 MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000370 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
371 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
372 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
373 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
374 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
375 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
David Blaikieb7020252015-12-04 21:16:42 +0000376 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikie23919372016-02-06 01:15:26 +0000377 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
David Blaikie8bce5a02016-02-17 07:00:24 +0000378 {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}},
379 {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}};
David Blaikieb073cb92015-12-02 06:21:34 +0000380
David Blaikie852c02b2016-02-19 21:09:26 +0000381 MapVector<uint64_t, UnitIndexEntry> IndexEntries;
382 MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000383
384 StringMap<uint32_t> Strings;
385 uint32_t StringOffset = 0;
386
David Blaikieb073cb92015-12-02 06:21:34 +0000387 uint32_t ContributionOffsets[8] = {};
388
David Blaikie242b9482015-12-01 00:48:39 +0000389 for (const auto &Input : Inputs) {
390 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
391 if (!ErrOrObj)
392 return ErrOrObj.getError();
David Blaikieb073cb92015-12-02 06:21:34 +0000393
David Blaikie23919372016-02-06 01:15:26 +0000394 UnitIndexEntry CurEntry = {};
David Blaikieb073cb92015-12-02 06:21:34 +0000395
David Blaikie98ad82a2015-12-01 18:07:07 +0000396 StringRef CurStrSection;
397 StringRef CurStrOffsetSection;
David Blaikiec3826da2015-12-09 21:02:33 +0000398 StringRef CurTypesSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000399 StringRef InfoSection;
400 StringRef AbbrevSection;
David Blaikie23919372016-02-06 01:15:26 +0000401 StringRef CurCUIndexSection;
David Blaikie8bce5a02016-02-17 07:00:24 +0000402 StringRef CurTUIndexSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000403
David Blaikie74f5b282016-02-19 01:51:44 +0000404 SmallVector<SmallString<32>, 4> UncompressedSections;
405
David Blaikieddb27362016-03-01 21:24:04 +0000406 for (const auto &Section : ErrOrObj->getBinary()->sections()) {
David Blaikie74f5b282016-02-19 01:51:44 +0000407 if (Section.isBSS())
408 continue;
409 if (Section.isVirtual())
410 continue;
411
David Blaikie242b9482015-12-01 00:48:39 +0000412 StringRef Name;
413 if (std::error_code Err = Section.getName(Name))
414 return Err;
David Blaikieb073cb92015-12-02 06:21:34 +0000415
David Blaikie74f5b282016-02-19 01:51:44 +0000416 Name = Name.substr(Name.find_first_not_of("._"));
David Blaikieb073cb92015-12-02 06:21:34 +0000417
418 StringRef Contents;
419 if (auto Err = Section.getContents(Contents))
420 return Err;
421
David Blaikie74f5b282016-02-19 01:51:44 +0000422 if (Name.startswith("zdebug_")) {
423 uint64_t OriginalSize;
424 if (!zlib::isAvailable() ||
425 !consumeCompressedDebugSectionHeader(Contents, OriginalSize))
David Blaikie9a5d3642016-02-19 02:03:45 +0000426 return make_error_code(std::errc::invalid_argument);
David Blaikie74f5b282016-02-19 01:51:44 +0000427 UncompressedSections.resize(UncompressedSections.size() + 1);
428 if (zlib::uncompress(Contents, UncompressedSections.back(), OriginalSize) !=
429 zlib::StatusOK) {
430 UncompressedSections.pop_back();
431 continue;
432 }
433 Name = Name.substr(1);
434 Contents = UncompressedSections.back();
435 }
436
437 auto SectionPair = KnownSections.find(Name);
438 if (SectionPair == KnownSections.end())
439 continue;
440
David Blaikieb073cb92015-12-02 06:21:34 +0000441 if (DWARFSectionKind Kind = SectionPair->second.second) {
442 auto Index = Kind - DW_SECT_INFO;
David Blaikiec3826da2015-12-09 21:02:33 +0000443 if (Kind != DW_SECT_TYPES) {
444 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
445 ContributionOffsets[Index] +=
446 (CurEntry.Contributions[Index].Length = Contents.size());
447 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000448
David Blaikie24c8ac92015-12-05 03:05:45 +0000449 switch (Kind) {
450 case DW_SECT_INFO:
David Blaikiead07b5d2015-12-04 17:20:04 +0000451 InfoSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000452 break;
453 case DW_SECT_ABBREV:
David Blaikiead07b5d2015-12-04 17:20:04 +0000454 AbbrevSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000455 break;
David Blaikie24c8ac92015-12-05 03:05:45 +0000456 default:
457 break;
David Blaikiead07b5d2015-12-04 17:20:04 +0000458 }
David Blaikieb073cb92015-12-02 06:21:34 +0000459 }
460
461 MCSection *OutSection = SectionPair->second.first;
462 if (OutSection == StrOffsetSection)
463 CurStrOffsetSection = Contents;
464 else if (OutSection == StrSection)
465 CurStrSection = Contents;
David Blaikiec3826da2015-12-09 21:02:33 +0000466 else if (OutSection == TypesSection)
467 CurTypesSection = Contents;
David Blaikie23919372016-02-06 01:15:26 +0000468 else if (OutSection == CUIndexSection)
469 CurCUIndexSection = Contents;
David Blaikie8bce5a02016-02-17 07:00:24 +0000470 else if (OutSection == TUIndexSection)
471 CurTUIndexSection = Contents;
David Blaikieb073cb92015-12-02 06:21:34 +0000472 else {
473 Out.SwitchSection(OutSection);
474 Out.EmitBytes(Contents);
David Blaikie98ad82a2015-12-01 18:07:07 +0000475 }
David Blaikie242b9482015-12-01 00:48:39 +0000476 }
David Blaikieb073cb92015-12-02 06:21:34 +0000477
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000478 if (InfoSection.empty())
479 continue;
480
David Blaikie23919372016-02-06 01:15:26 +0000481 if (!CurCUIndexSection.empty()) {
482 DWARFUnitIndex CUIndex(DW_SECT_INFO);
David Blaikieddb27362016-03-01 21:24:04 +0000483 DataExtractor CUIndexData(CurCUIndexSection,
484 ErrOrObj->getBinary()->isLittleEndian(), 0);
David Blaikie23919372016-02-06 01:15:26 +0000485 if (!CUIndex.parse(CUIndexData))
486 return make_error_code(std::errc::invalid_argument);
487
488 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
David Blaikie23919372016-02-06 01:15:26 +0000489 auto *I = E.getOffsets();
490 if (!I)
491 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000492 auto P =
493 IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry));
David Blaikiece7c6cf2016-03-24 22:17:08 +0000494 CompileUnitIdentifiers ID = getCUIdentifiers(
David Blaikief1958da2016-02-26 07:30:15 +0000495 getSubsection(AbbrevSection, E, DW_SECT_ABBREV),
496 getSubsection(InfoSection, E, DW_SECT_INFO),
497 getSubsection(CurStrOffsetSection, E, DW_SECT_STR_OFFSETS),
498 CurStrSection);
499 if (!P.second) {
David Blaikie4dd03f02016-03-26 20:32:14 +0000500 printDuplicateError(*P.first, ID, Input);
David Blaikief1958da2016-02-26 07:30:15 +0000501 return make_error_code(std::errc::invalid_argument);
502 }
David Blaikie852c02b2016-02-19 21:09:26 +0000503 auto &NewEntry = P.first->second;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000504 NewEntry.Name = ID.Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000505 NewEntry.DWOName = ID.DWOName;
David Blaikief1958da2016-02-26 07:30:15 +0000506 NewEntry.DWPName = Input;
David Blaikie23919372016-02-06 01:15:26 +0000507 for (auto Kind : CUIndex.getColumnKinds()) {
508 auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO];
509 C.Offset += I->Offset;
510 C.Length = I->Length;
511 ++I;
512 }
David Blaikie23919372016-02-06 01:15:26 +0000513 }
David Blaikie8bce5a02016-02-17 07:00:24 +0000514
515 if (!CurTypesSection.empty()) {
516 if (CurTUIndexSection.empty())
517 return make_error_code(std::errc::invalid_argument);
518 DWARFUnitIndex TUIndex(DW_SECT_TYPES);
David Blaikieddb27362016-03-01 21:24:04 +0000519 DataExtractor TUIndexData(CurTUIndexSection,
520 ErrOrObj->getBinary()->isLittleEndian(), 0);
David Blaikie8bce5a02016-02-17 07:00:24 +0000521 if (!TUIndex.parse(TUIndexData))
522 return make_error_code(std::errc::invalid_argument);
523 addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection,
524 CurTypesSection, CurEntry,
525 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
526 }
David Blaikie23919372016-02-06 01:15:26 +0000527 } else {
David Blaikiece7c6cf2016-03-24 22:17:08 +0000528 CompileUnitIdentifiers ID = getCUIdentifiers(
529 AbbrevSection, InfoSection, CurStrOffsetSection, CurStrSection);
530 auto P = IndexEntries.insert(std::make_pair(ID.Signature, CurEntry));
David Blaikief1958da2016-02-26 07:30:15 +0000531 if (!P.second) {
David Blaikie4dd03f02016-03-26 20:32:14 +0000532 printDuplicateError(*P.first, ID, "");
David Blaikief1958da2016-02-26 07:30:15 +0000533 return make_error_code(std::errc::invalid_argument);
534 }
David Blaikiece7c6cf2016-03-24 22:17:08 +0000535 P.first->second.Name = ID.Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000536 P.first->second.DWOName = ID.DWOName;
David Blaikie23919372016-02-06 01:15:26 +0000537 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection,
538 CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
David Blaikie23919372016-02-06 01:15:26 +0000539 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000540
David Blaikiebb94e442015-12-01 19:17:58 +0000541 if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset,
542 StrSection, StrOffsetSection,
543 CurStrSection, CurStrOffsetSection))
David Blaikie98ad82a2015-12-01 18:07:07 +0000544 return Err;
David Blaikie242b9482015-12-01 00:48:39 +0000545 }
David Blaikieb073cb92015-12-02 06:21:34 +0000546
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000547 // Lie about there being no info contributions so the TU index only includes
548 // the type unit contribution
549 ContributionOffsets[0] = 0;
550 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
551 TypeIndexEntries);
David Blaikieb3757c02015-12-02 22:01:56 +0000552
David Blaikie24c8ac92015-12-05 03:05:45 +0000553 // Lie about the type contribution
554 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
555 // Unlie about the info contribution
556 ContributionOffsets[0] = 1;
David Blaikie7c4ffe02015-12-04 21:30:23 +0000557
David Blaikie24c8ac92015-12-05 03:05:45 +0000558 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
559 IndexEntries);
David Blaikieddb27362016-03-01 21:24:04 +0000560
David Blaikie242b9482015-12-01 00:48:39 +0000561 return std::error_code();
562}
563
David Blaikie2ed678c2015-12-05 03:06:30 +0000564int main(int argc, char **argv) {
David Blaikie242b9482015-12-01 00:48:39 +0000565
566 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
567
568 llvm::InitializeAllTargetInfos();
569 llvm::InitializeAllTargetMCs();
570 llvm::InitializeAllTargets();
571 llvm::InitializeAllAsmPrinters();
572
573 std::string ErrorStr;
574 StringRef Context = "dwarf streamer init";
575
576 Triple TheTriple("x86_64-linux-gnu");
577
578 // Get the target.
579 const Target *TheTarget =
580 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
581 if (!TheTarget)
582 return error(ErrorStr, Context);
583 std::string TripleName = TheTriple.getTriple();
584
585 // Create all the MC Objects.
586 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
587 if (!MRI)
588 return error(Twine("no register info for target ") + TripleName, Context);
589
590 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
591 if (!MAI)
592 return error("no asm info for target " + TripleName, Context);
593
594 MCObjectFileInfo MOFI;
595 MCContext MC(MAI.get(), MRI.get(), &MOFI);
David Blaikie2ed678c2015-12-05 03:06:30 +0000596 MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default, MC);
David Blaikie242b9482015-12-01 00:48:39 +0000597
598 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
599 if (!MAB)
600 return error("no asm backend for target " + TripleName, Context);
601
602 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
603 if (!MII)
604 return error("no instr info info for target " + TripleName, Context);
605
606 std::unique_ptr<MCSubtargetInfo> MSTI(
607 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
608 if (!MSTI)
609 return error("no subtarget info for target " + TripleName, Context);
610
611 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
612 if (!MCE)
613 return error("no code emitter for target " + TripleName, Context);
614
615 // Create the output file.
616 std::error_code EC;
617 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
618 if (EC)
619 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
620
David Majnemer03e2cc32015-12-21 22:09:27 +0000621 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
David Blaikie242b9482015-12-01 00:48:39 +0000622 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
David Majnemer03e2cc32015-12-21 22:09:27 +0000623 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, MCOptions.MCRelaxAll,
624 MCOptions.MCIncrementalLinkerCompatible,
David Blaikie242b9482015-12-01 00:48:39 +0000625 /*DWARFMustBeAtTheEnd*/ false));
626 if (!MS)
627 return error("no object streamer for target " + TripleName, Context);
628
629 if (auto Err = write(*MS, InputFiles))
630 return error(Err.message(), "Writing DWP file");
David Blaikieddb27362016-03-01 21:24:04 +0000631
632 MS->Finish();
David Blaikiedf055252015-12-01 00:48:34 +0000633}