blob: 70bad7565be4cba28ef18e7976eaedda4de626f0 [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;
125 const char *Name = nullptr;
126 const char *DWOName = nullptr;
127};
128
129static CompileUnitIdentifiers getCUIdentifiers(StringRef Abbrev, StringRef Info,
130 StringRef StrOffsets,
131 StringRef Str) {
David Blaikief1958da2016-02-26 07:30:15 +0000132 uint32_t Offset = 0;
133 DataExtractor InfoData(Info, true, 0);
134 InfoData.getU32(&Offset); // Length
135 uint16_t Version = InfoData.getU16(&Offset);
136 InfoData.getU32(&Offset); // Abbrev offset (should be zero)
137 uint8_t AddrSize = InfoData.getU8(&Offset);
138
139 uint32_t AbbrCode = InfoData.getULEB128(&Offset);
140
141 DataExtractor AbbrevData(Abbrev, true, 0);
142 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
143 uint64_t Tag = AbbrevData.getULEB128(&AbbrevOffset);
144 (void)Tag;
145 // FIXME: Real error handling
146 assert(Tag == dwarf::DW_TAG_compile_unit);
147 // DW_CHILDREN
148 AbbrevData.getU8(&AbbrevOffset);
149 uint32_t Name;
150 uint32_t Form;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000151 CompileUnitIdentifiers ID;
David Blaikief1958da2016-02-26 07:30:15 +0000152 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |
153 (Form = AbbrevData.getULEB128(&AbbrevOffset)) &&
David Blaikiece7c6cf2016-03-24 22:17:08 +0000154 (Name != 0 || Form != 0)) {
155 switch (Name) {
156 case dwarf::DW_AT_name: {
157 auto StrIndex = InfoData.getULEB128(&Offset);
158
159 DataExtractor StrOffsetsData(StrOffsets, true, 0);
160 uint32_t StrOffsetsOffset = 4 * StrIndex;
161 uint32_t StrOffset = StrOffsetsData.getU32(&StrOffsetsOffset);
162 DataExtractor StrData(Str, true, 0);
163 ID.Name = StrData.getCStr(&StrOffset);
164 break;
165 }
166 case dwarf::DW_AT_GNU_dwo_id:
167 ID.Signature = InfoData.getU64(&Offset);
168 break;
169 default:
170 DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize);
171 }
David Blaikief1958da2016-02-26 07:30:15 +0000172 }
David Blaikiece7c6cf2016-03-24 22:17:08 +0000173 return ID;
David Blaikiead07b5d2015-12-04 17:20:04 +0000174}
175
David Blaikie24c8ac92015-12-05 03:05:45 +0000176struct UnitIndexEntry {
David Blaikie24c8ac92015-12-05 03:05:45 +0000177 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
David Blaikief1958da2016-02-26 07:30:15 +0000178 std::string Name;
179 StringRef DWPName;
David Blaikie24c8ac92015-12-05 03:05:45 +0000180};
181
David Blaikief1958da2016-02-26 07:30:15 +0000182StringRef getSubsection(StringRef Section, const DWARFUnitIndex::Entry &Entry, DWARFSectionKind Kind) {
183 const auto *Off = Entry.getOffset(Kind);
184 if (!Off)
185 return StringRef();
186 return Section.substr(Off->Offset, Off->Length);
187}
188
David Blaikie852c02b2016-02-19 21:09:26 +0000189static void addAllTypesFromDWP(
190 MCStreamer &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
191 const DWARFUnitIndex &TUIndex, MCSection *OutputTypes, StringRef Types,
192 const UnitIndexEntry &TUEntry, uint32_t &TypesOffset) {
David Blaikie8bce5a02016-02-17 07:00:24 +0000193 Out.SwitchSection(OutputTypes);
194 for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) {
195 auto *I = E.getOffsets();
196 if (!I)
197 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000198 auto P = TypeIndexEntries.insert(std::make_pair(E.getSignature(), TUEntry));
199 if (!P.second)
David Blaikie8bce5a02016-02-17 07:00:24 +0000200 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000201 auto &Entry = P.first->second;
David Blaikie8bce5a02016-02-17 07:00:24 +0000202 // Zero out the debug_info contribution
203 Entry.Contributions[0] = {};
204 for (auto Kind : TUIndex.getColumnKinds()) {
205 auto &C = Entry.Contributions[Kind - DW_SECT_INFO];
206 C.Offset += I->Offset;
207 C.Length = I->Length;
208 ++I;
209 }
210 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
211 Out.EmitBytes(Types.substr(
212 C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset,
213 C.Length));
214 C.Offset = TypesOffset;
215 TypesOffset += C.Length;
David Blaikie8bce5a02016-02-17 07:00:24 +0000216 }
217}
218
David Blaikiec3826da2015-12-09 21:02:33 +0000219static void addAllTypes(MCStreamer &Out,
David Blaikie852c02b2016-02-19 21:09:26 +0000220 MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
David Blaikiec3826da2015-12-09 21:02:33 +0000221 MCSection *OutputTypes, StringRef Types,
222 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) {
223 if (Types.empty())
224 return;
225
226 Out.SwitchSection(OutputTypes);
David Blaikie24c8ac92015-12-05 03:05:45 +0000227 uint32_t Offset = 0;
228 DataExtractor Data(Types, true, 0);
229 while (Data.isValidOffset(Offset)) {
David Blaikief5cb6272015-12-14 07:42:00 +0000230 UnitIndexEntry Entry = CUEntry;
David Blaikie24c8ac92015-12-05 03:05:45 +0000231 // Zero out the debug_info contribution
232 Entry.Contributions[0] = {};
233 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
David Blaikief5cb6272015-12-14 07:42:00 +0000234 C.Offset = TypesOffset;
David Blaikie24c8ac92015-12-05 03:05:45 +0000235 auto PrevOffset = Offset;
236 // Length of the unit, including the 4 byte length field.
237 C.Length = Data.getU32(&Offset) + 4;
238
239 Data.getU16(&Offset); // Version
240 Data.getU32(&Offset); // Abbrev offset
241 Data.getU8(&Offset); // Address size
David Blaikie852c02b2016-02-19 21:09:26 +0000242 auto Signature = Data.getU64(&Offset);
David Blaikie24c8ac92015-12-05 03:05:45 +0000243 Offset = PrevOffset + C.Length;
David Blaikief5cb6272015-12-14 07:42:00 +0000244
David Blaikie852c02b2016-02-19 21:09:26 +0000245 auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry));
246 if (!P.second)
David Blaikief5cb6272015-12-14 07:42:00 +0000247 continue;
248
249 Out.EmitBytes(Types.substr(PrevOffset, C.Length));
250 TypesOffset += C.Length;
David Blaikie24c8ac92015-12-05 03:05:45 +0000251 }
252}
253
254static void
255writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,
David Blaikie852c02b2016-02-19 21:09:26 +0000256 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries,
David Blaikie24c8ac92015-12-05 03:05:45 +0000257 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) {
258 for (const auto &E : IndexEntries)
David Blaikie852c02b2016-02-19 21:09:26 +0000259 for (size_t i = 0; i != array_lengthof(E.second.Contributions); ++i)
David Blaikie24c8ac92015-12-05 03:05:45 +0000260 if (ContributionOffsets[i])
David Blaikie852c02b2016-02-19 21:09:26 +0000261 Out.EmitIntValue(E.second.Contributions[i].*Field, 4);
David Blaikie24c8ac92015-12-05 03:05:45 +0000262}
263
David Blaikie852c02b2016-02-19 21:09:26 +0000264static void
265writeIndex(MCStreamer &Out, MCSection *Section,
266 ArrayRef<unsigned> ContributionOffsets,
267 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries) {
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000268 if (IndexEntries.empty())
269 return;
270
David Blaikie24c8ac92015-12-05 03:05:45 +0000271 unsigned Columns = 0;
272 for (auto &C : ContributionOffsets)
273 if (C)
274 ++Columns;
275
276 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
277 uint64_t Mask = Buckets.size() - 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000278 size_t i = 0;
279 for (const auto &P : IndexEntries) {
280 auto S = P.first;
David Blaikie24c8ac92015-12-05 03:05:45 +0000281 auto H = S & Mask;
David Blaikiec3826da2015-12-09 21:02:33 +0000282 while (Buckets[H]) {
David Blaikie852c02b2016-02-19 21:09:26 +0000283 assert(S != IndexEntries.begin()[Buckets[H] - 1].first &&
David Blaikie74f5b282016-02-19 01:51:44 +0000284 "Duplicate unit");
Benjamin Kramere5930942016-02-18 13:23:17 +0000285 H = (H + (((S >> 32) & Mask) | 1)) % Buckets.size();
David Blaikiec3826da2015-12-09 21:02:33 +0000286 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000287 Buckets[H] = i + 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000288 ++i;
David Blaikie24c8ac92015-12-05 03:05:45 +0000289 }
290
291 Out.SwitchSection(Section);
292 Out.EmitIntValue(2, 4); // Version
293 Out.EmitIntValue(Columns, 4); // Columns
294 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
David Blaikie2ed678c2015-12-05 03:06:30 +0000295 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets
David Blaikie24c8ac92015-12-05 03:05:45 +0000296
297 // Write the signatures.
298 for (const auto &I : Buckets)
David Blaikie852c02b2016-02-19 21:09:26 +0000299 Out.EmitIntValue(I ? IndexEntries.begin()[I - 1].first : 0, 8);
David Blaikie24c8ac92015-12-05 03:05:45 +0000300
301 // Write the indexes.
302 for (const auto &I : Buckets)
303 Out.EmitIntValue(I, 4);
304
305 // Write the column headers (which sections will appear in the table)
306 for (size_t i = 0; i != ContributionOffsets.size(); ++i)
307 if (ContributionOffsets[i])
308 Out.EmitIntValue(i + DW_SECT_INFO, 4);
309
310 // Write the offsets.
311 writeIndexTable(Out, ContributionOffsets, IndexEntries,
312 &DWARFUnitIndex::Entry::SectionContribution::Offset);
313
314 // Write the lengths.
315 writeIndexTable(Out, ContributionOffsets, IndexEntries,
316 &DWARFUnitIndex::Entry::SectionContribution::Length);
317}
David Blaikie74f5b282016-02-19 01:51:44 +0000318static bool consumeCompressedDebugSectionHeader(StringRef &data,
319 uint64_t &OriginalSize) {
320 // Consume "ZLIB" prefix.
321 if (!data.startswith("ZLIB"))
322 return false;
323 data = data.substr(4);
324 // Consume uncompressed section size (big-endian 8 bytes).
325 DataExtractor extractor(data, false, 8);
326 uint32_t Offset = 0;
327 OriginalSize = extractor.getU64(&Offset);
328 if (Offset == 0)
329 return false;
330 data = data.substr(Offset);
331 return true;
332}
333
David Blaikie242b9482015-12-01 00:48:39 +0000334static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000335 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
336 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
337 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikiec3826da2015-12-09 21:02:33 +0000338 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
David Blaikie23919372016-02-06 01:15:26 +0000339 MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection();
David Blaikie8bce5a02016-02-17 07:00:24 +0000340 MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000341 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
342 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
343 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
344 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
345 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
346 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
David Blaikieb7020252015-12-04 21:16:42 +0000347 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikie23919372016-02-06 01:15:26 +0000348 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
David Blaikie8bce5a02016-02-17 07:00:24 +0000349 {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}},
350 {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}};
David Blaikieb073cb92015-12-02 06:21:34 +0000351
David Blaikie852c02b2016-02-19 21:09:26 +0000352 MapVector<uint64_t, UnitIndexEntry> IndexEntries;
353 MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000354
355 StringMap<uint32_t> Strings;
356 uint32_t StringOffset = 0;
357
David Blaikieb073cb92015-12-02 06:21:34 +0000358 uint32_t ContributionOffsets[8] = {};
359
David Blaikie242b9482015-12-01 00:48:39 +0000360 for (const auto &Input : Inputs) {
361 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
362 if (!ErrOrObj)
363 return ErrOrObj.getError();
David Blaikieb073cb92015-12-02 06:21:34 +0000364
David Blaikie23919372016-02-06 01:15:26 +0000365 UnitIndexEntry CurEntry = {};
David Blaikieb073cb92015-12-02 06:21:34 +0000366
David Blaikie98ad82a2015-12-01 18:07:07 +0000367 StringRef CurStrSection;
368 StringRef CurStrOffsetSection;
David Blaikiec3826da2015-12-09 21:02:33 +0000369 StringRef CurTypesSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000370 StringRef InfoSection;
371 StringRef AbbrevSection;
David Blaikie23919372016-02-06 01:15:26 +0000372 StringRef CurCUIndexSection;
David Blaikie8bce5a02016-02-17 07:00:24 +0000373 StringRef CurTUIndexSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000374
David Blaikie74f5b282016-02-19 01:51:44 +0000375 SmallVector<SmallString<32>, 4> UncompressedSections;
376
David Blaikieddb27362016-03-01 21:24:04 +0000377 for (const auto &Section : ErrOrObj->getBinary()->sections()) {
David Blaikie74f5b282016-02-19 01:51:44 +0000378 if (Section.isBSS())
379 continue;
380 if (Section.isVirtual())
381 continue;
382
David Blaikie242b9482015-12-01 00:48:39 +0000383 StringRef Name;
384 if (std::error_code Err = Section.getName(Name))
385 return Err;
David Blaikieb073cb92015-12-02 06:21:34 +0000386
David Blaikie74f5b282016-02-19 01:51:44 +0000387 Name = Name.substr(Name.find_first_not_of("._"));
David Blaikieb073cb92015-12-02 06:21:34 +0000388
389 StringRef Contents;
390 if (auto Err = Section.getContents(Contents))
391 return Err;
392
David Blaikie74f5b282016-02-19 01:51:44 +0000393 if (Name.startswith("zdebug_")) {
394 uint64_t OriginalSize;
395 if (!zlib::isAvailable() ||
396 !consumeCompressedDebugSectionHeader(Contents, OriginalSize))
David Blaikie9a5d3642016-02-19 02:03:45 +0000397 return make_error_code(std::errc::invalid_argument);
David Blaikie74f5b282016-02-19 01:51:44 +0000398 UncompressedSections.resize(UncompressedSections.size() + 1);
399 if (zlib::uncompress(Contents, UncompressedSections.back(), OriginalSize) !=
400 zlib::StatusOK) {
401 UncompressedSections.pop_back();
402 continue;
403 }
404 Name = Name.substr(1);
405 Contents = UncompressedSections.back();
406 }
407
408 auto SectionPair = KnownSections.find(Name);
409 if (SectionPair == KnownSections.end())
410 continue;
411
David Blaikieb073cb92015-12-02 06:21:34 +0000412 if (DWARFSectionKind Kind = SectionPair->second.second) {
413 auto Index = Kind - DW_SECT_INFO;
David Blaikiec3826da2015-12-09 21:02:33 +0000414 if (Kind != DW_SECT_TYPES) {
415 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
416 ContributionOffsets[Index] +=
417 (CurEntry.Contributions[Index].Length = Contents.size());
418 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000419
David Blaikie24c8ac92015-12-05 03:05:45 +0000420 switch (Kind) {
421 case DW_SECT_INFO:
David Blaikiead07b5d2015-12-04 17:20:04 +0000422 InfoSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000423 break;
424 case DW_SECT_ABBREV:
David Blaikiead07b5d2015-12-04 17:20:04 +0000425 AbbrevSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000426 break;
David Blaikie24c8ac92015-12-05 03:05:45 +0000427 default:
428 break;
David Blaikiead07b5d2015-12-04 17:20:04 +0000429 }
David Blaikieb073cb92015-12-02 06:21:34 +0000430 }
431
432 MCSection *OutSection = SectionPair->second.first;
433 if (OutSection == StrOffsetSection)
434 CurStrOffsetSection = Contents;
435 else if (OutSection == StrSection)
436 CurStrSection = Contents;
David Blaikiec3826da2015-12-09 21:02:33 +0000437 else if (OutSection == TypesSection)
438 CurTypesSection = Contents;
David Blaikie23919372016-02-06 01:15:26 +0000439 else if (OutSection == CUIndexSection)
440 CurCUIndexSection = Contents;
David Blaikie8bce5a02016-02-17 07:00:24 +0000441 else if (OutSection == TUIndexSection)
442 CurTUIndexSection = Contents;
David Blaikieb073cb92015-12-02 06:21:34 +0000443 else {
444 Out.SwitchSection(OutSection);
445 Out.EmitBytes(Contents);
David Blaikie98ad82a2015-12-01 18:07:07 +0000446 }
David Blaikie242b9482015-12-01 00:48:39 +0000447 }
David Blaikieb073cb92015-12-02 06:21:34 +0000448
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000449 if (InfoSection.empty())
450 continue;
451
David Blaikie23919372016-02-06 01:15:26 +0000452 if (!CurCUIndexSection.empty()) {
453 DWARFUnitIndex CUIndex(DW_SECT_INFO);
David Blaikieddb27362016-03-01 21:24:04 +0000454 DataExtractor CUIndexData(CurCUIndexSection,
455 ErrOrObj->getBinary()->isLittleEndian(), 0);
David Blaikie23919372016-02-06 01:15:26 +0000456 if (!CUIndex.parse(CUIndexData))
457 return make_error_code(std::errc::invalid_argument);
458
459 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
David Blaikie23919372016-02-06 01:15:26 +0000460 auto *I = E.getOffsets();
461 if (!I)
462 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000463 auto P =
464 IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry));
David Blaikiece7c6cf2016-03-24 22:17:08 +0000465 CompileUnitIdentifiers ID = getCUIdentifiers(
David Blaikief1958da2016-02-26 07:30:15 +0000466 getSubsection(AbbrevSection, E, DW_SECT_ABBREV),
467 getSubsection(InfoSection, E, DW_SECT_INFO),
468 getSubsection(CurStrOffsetSection, E, DW_SECT_STR_OFFSETS),
469 CurStrSection);
470 if (!P.second) {
471 auto &PrevE = *P.first;
472 std::cerr << "Duplicate DWO ID (" << PrevE.first << ") in '" << PrevE.second.Name << "' ";
473 if (!PrevE.second.DWPName.empty())
474 std::cerr << "(from '" << PrevE.second.DWPName.str() << "') ";
David Blaikiece7c6cf2016-03-24 22:17:08 +0000475 std::cerr << "and '" << ID.Name << "' (from '" << Input << "')\n";
David Blaikief1958da2016-02-26 07:30:15 +0000476 return make_error_code(std::errc::invalid_argument);
477 }
David Blaikie852c02b2016-02-19 21:09:26 +0000478 auto &NewEntry = P.first->second;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000479 NewEntry.Name = ID.Name;
David Blaikief1958da2016-02-26 07:30:15 +0000480 NewEntry.DWPName = Input;
David Blaikie23919372016-02-06 01:15:26 +0000481 for (auto Kind : CUIndex.getColumnKinds()) {
482 auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO];
483 C.Offset += I->Offset;
484 C.Length = I->Length;
485 ++I;
486 }
David Blaikie23919372016-02-06 01:15:26 +0000487 }
David Blaikie8bce5a02016-02-17 07:00:24 +0000488
489 if (!CurTypesSection.empty()) {
490 if (CurTUIndexSection.empty())
491 return make_error_code(std::errc::invalid_argument);
492 DWARFUnitIndex TUIndex(DW_SECT_TYPES);
David Blaikieddb27362016-03-01 21:24:04 +0000493 DataExtractor TUIndexData(CurTUIndexSection,
494 ErrOrObj->getBinary()->isLittleEndian(), 0);
David Blaikie8bce5a02016-02-17 07:00:24 +0000495 if (!TUIndex.parse(TUIndexData))
496 return make_error_code(std::errc::invalid_argument);
497 addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection,
498 CurTypesSection, CurEntry,
499 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
500 }
David Blaikie23919372016-02-06 01:15:26 +0000501 } else {
David Blaikiece7c6cf2016-03-24 22:17:08 +0000502 CompileUnitIdentifiers ID = getCUIdentifiers(
503 AbbrevSection, InfoSection, CurStrOffsetSection, CurStrSection);
504 auto P = IndexEntries.insert(std::make_pair(ID.Signature, CurEntry));
David Blaikief1958da2016-02-26 07:30:15 +0000505 if (!P.second) {
506 auto &E = *P.first;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000507 std::cerr << "Duplicate DWO ID (" << E.first << ") in '" << ID.Name
David Blaikief1958da2016-02-26 07:30:15 +0000508 << "' ";
509 if (!E.second.DWPName.empty())
510 std::cerr << "(from '" << E.second.DWPName.str() << "') ";
511 std::cerr << "and '" << E.second.Name << "'\n";
512 return make_error_code(std::errc::invalid_argument);
513 }
David Blaikiece7c6cf2016-03-24 22:17:08 +0000514 P.first->second.Name = ID.Name;
David Blaikie23919372016-02-06 01:15:26 +0000515 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection,
516 CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
David Blaikie23919372016-02-06 01:15:26 +0000517 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000518
David Blaikiebb94e442015-12-01 19:17:58 +0000519 if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset,
520 StrSection, StrOffsetSection,
521 CurStrSection, CurStrOffsetSection))
David Blaikie98ad82a2015-12-01 18:07:07 +0000522 return Err;
David Blaikie242b9482015-12-01 00:48:39 +0000523 }
David Blaikieb073cb92015-12-02 06:21:34 +0000524
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000525 // Lie about there being no info contributions so the TU index only includes
526 // the type unit contribution
527 ContributionOffsets[0] = 0;
528 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
529 TypeIndexEntries);
David Blaikieb3757c02015-12-02 22:01:56 +0000530
David Blaikie24c8ac92015-12-05 03:05:45 +0000531 // Lie about the type contribution
532 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
533 // Unlie about the info contribution
534 ContributionOffsets[0] = 1;
David Blaikie7c4ffe02015-12-04 21:30:23 +0000535
David Blaikie24c8ac92015-12-05 03:05:45 +0000536 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
537 IndexEntries);
David Blaikieddb27362016-03-01 21:24:04 +0000538
David Blaikie242b9482015-12-01 00:48:39 +0000539 return std::error_code();
540}
541
David Blaikie2ed678c2015-12-05 03:06:30 +0000542int main(int argc, char **argv) {
David Blaikie242b9482015-12-01 00:48:39 +0000543
544 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
545
546 llvm::InitializeAllTargetInfos();
547 llvm::InitializeAllTargetMCs();
548 llvm::InitializeAllTargets();
549 llvm::InitializeAllAsmPrinters();
550
551 std::string ErrorStr;
552 StringRef Context = "dwarf streamer init";
553
554 Triple TheTriple("x86_64-linux-gnu");
555
556 // Get the target.
557 const Target *TheTarget =
558 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
559 if (!TheTarget)
560 return error(ErrorStr, Context);
561 std::string TripleName = TheTriple.getTriple();
562
563 // Create all the MC Objects.
564 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
565 if (!MRI)
566 return error(Twine("no register info for target ") + TripleName, Context);
567
568 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
569 if (!MAI)
570 return error("no asm info for target " + TripleName, Context);
571
572 MCObjectFileInfo MOFI;
573 MCContext MC(MAI.get(), MRI.get(), &MOFI);
David Blaikie2ed678c2015-12-05 03:06:30 +0000574 MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default, MC);
David Blaikie242b9482015-12-01 00:48:39 +0000575
576 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
577 if (!MAB)
578 return error("no asm backend for target " + TripleName, Context);
579
580 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
581 if (!MII)
582 return error("no instr info info for target " + TripleName, Context);
583
584 std::unique_ptr<MCSubtargetInfo> MSTI(
585 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
586 if (!MSTI)
587 return error("no subtarget info for target " + TripleName, Context);
588
589 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
590 if (!MCE)
591 return error("no code emitter for target " + TripleName, Context);
592
593 // Create the output file.
594 std::error_code EC;
595 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
596 if (EC)
597 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
598
David Majnemer03e2cc32015-12-21 22:09:27 +0000599 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
David Blaikie242b9482015-12-01 00:48:39 +0000600 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
David Majnemer03e2cc32015-12-21 22:09:27 +0000601 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, MCOptions.MCRelaxAll,
602 MCOptions.MCIncrementalLinkerCompatible,
David Blaikie242b9482015-12-01 00:48:39 +0000603 /*DWARFMustBeAtTheEnd*/ false));
604 if (!MS)
605 return error("no object streamer for target " + TripleName, Context);
606
607 if (auto Err = write(*MS, InputFiles))
608 return error(Err.message(), "Writing DWP file");
David Blaikieddb27362016-03-01 21:24:04 +0000609
610 MS->Finish();
David Blaikiedf055252015-12-01 00:48:34 +0000611}