blob: 40bea8eabc604b1a49d7e48eb948b1859f5fa999 [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 Blaikie60fbd3b2016-04-05 20:16:38 +0000129static const char *getIndexedString(uint32_t Form, DataExtractor InfoData,
130 uint32_t &InfoOffset, StringRef StrOffsets,
David Blaikie4dd03f02016-03-26 20:32:14 +0000131 StringRef Str) {
David Blaikie60fbd3b2016-04-05 20:16:38 +0000132 if (Form == dwarf::DW_FORM_string)
133 return InfoData.getCStr(&InfoOffset);
David Blaikieee210802016-04-13 18:38:33 +0000134 assert(Form == dwarf::DW_FORM_GNU_str_index && "Only string and str_index "
135 "forms are supported for DWP "
136 "string attributes");
David Blaikie60fbd3b2016-04-05 20:16:38 +0000137 auto StrIndex = InfoData.getULEB128(&InfoOffset);
David Blaikie4dd03f02016-03-26 20:32:14 +0000138 DataExtractor StrOffsetsData(StrOffsets, true, 0);
139 uint32_t StrOffsetsOffset = 4 * StrIndex;
140 uint32_t StrOffset = StrOffsetsData.getU32(&StrOffsetsOffset);
141 DataExtractor StrData(Str, true, 0);
142 return StrData.getCStr(&StrOffset);
143}
144
David Blaikiece7c6cf2016-03-24 22:17:08 +0000145static CompileUnitIdentifiers getCUIdentifiers(StringRef Abbrev, StringRef Info,
146 StringRef StrOffsets,
147 StringRef Str) {
David Blaikief1958da2016-02-26 07:30:15 +0000148 uint32_t Offset = 0;
149 DataExtractor InfoData(Info, true, 0);
150 InfoData.getU32(&Offset); // Length
151 uint16_t Version = InfoData.getU16(&Offset);
152 InfoData.getU32(&Offset); // Abbrev offset (should be zero)
153 uint8_t AddrSize = InfoData.getU8(&Offset);
154
155 uint32_t AbbrCode = InfoData.getULEB128(&Offset);
156
157 DataExtractor AbbrevData(Abbrev, true, 0);
158 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
159 uint64_t Tag = AbbrevData.getULEB128(&AbbrevOffset);
160 (void)Tag;
161 // FIXME: Real error handling
162 assert(Tag == dwarf::DW_TAG_compile_unit);
163 // DW_CHILDREN
164 AbbrevData.getU8(&AbbrevOffset);
165 uint32_t Name;
166 uint32_t Form;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000167 CompileUnitIdentifiers ID;
David Blaikief1958da2016-02-26 07:30:15 +0000168 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |
169 (Form = AbbrevData.getULEB128(&AbbrevOffset)) &&
David Blaikiece7c6cf2016-03-24 22:17:08 +0000170 (Name != 0 || Form != 0)) {
171 switch (Name) {
172 case dwarf::DW_AT_name: {
David Blaikie60fbd3b2016-04-05 20:16:38 +0000173 ID.Name = getIndexedString(Form, InfoData, Offset, StrOffsets, Str);
David Blaikie4dd03f02016-03-26 20:32:14 +0000174 break;
175 }
176 case dwarf::DW_AT_GNU_dwo_name: {
David Blaikie60fbd3b2016-04-05 20:16:38 +0000177 ID.DWOName = getIndexedString(Form, InfoData, Offset, StrOffsets, Str);
David Blaikiece7c6cf2016-03-24 22:17:08 +0000178 break;
179 }
180 case dwarf::DW_AT_GNU_dwo_id:
181 ID.Signature = InfoData.getU64(&Offset);
182 break;
183 default:
184 DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize);
185 }
David Blaikief1958da2016-02-26 07:30:15 +0000186 }
David Blaikiece7c6cf2016-03-24 22:17:08 +0000187 return ID;
David Blaikiead07b5d2015-12-04 17:20:04 +0000188}
189
David Blaikie24c8ac92015-12-05 03:05:45 +0000190struct UnitIndexEntry {
David Blaikie24c8ac92015-12-05 03:05:45 +0000191 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
David Blaikief1958da2016-02-26 07:30:15 +0000192 std::string Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000193 std::string DWOName;
David Blaikief1958da2016-02-26 07:30:15 +0000194 StringRef DWPName;
David Blaikie24c8ac92015-12-05 03:05:45 +0000195};
196
David Blaikief1958da2016-02-26 07:30:15 +0000197StringRef getSubsection(StringRef Section, const DWARFUnitIndex::Entry &Entry, DWARFSectionKind Kind) {
198 const auto *Off = Entry.getOffset(Kind);
199 if (!Off)
200 return StringRef();
201 return Section.substr(Off->Offset, Off->Length);
202}
203
David Blaikie852c02b2016-02-19 21:09:26 +0000204static void addAllTypesFromDWP(
205 MCStreamer &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
206 const DWARFUnitIndex &TUIndex, MCSection *OutputTypes, StringRef Types,
207 const UnitIndexEntry &TUEntry, uint32_t &TypesOffset) {
David Blaikie8bce5a02016-02-17 07:00:24 +0000208 Out.SwitchSection(OutputTypes);
209 for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) {
210 auto *I = E.getOffsets();
211 if (!I)
212 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000213 auto P = TypeIndexEntries.insert(std::make_pair(E.getSignature(), TUEntry));
214 if (!P.second)
David Blaikie8bce5a02016-02-17 07:00:24 +0000215 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000216 auto &Entry = P.first->second;
David Blaikie8bce5a02016-02-17 07:00:24 +0000217 // Zero out the debug_info contribution
218 Entry.Contributions[0] = {};
219 for (auto Kind : TUIndex.getColumnKinds()) {
220 auto &C = Entry.Contributions[Kind - DW_SECT_INFO];
221 C.Offset += I->Offset;
222 C.Length = I->Length;
223 ++I;
224 }
225 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
226 Out.EmitBytes(Types.substr(
227 C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset,
228 C.Length));
229 C.Offset = TypesOffset;
230 TypesOffset += C.Length;
David Blaikie8bce5a02016-02-17 07:00:24 +0000231 }
232}
233
David Blaikiec3826da2015-12-09 21:02:33 +0000234static void addAllTypes(MCStreamer &Out,
David Blaikie852c02b2016-02-19 21:09:26 +0000235 MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
David Blaikie62be5ae2016-04-05 20:26:50 +0000236 MCSection *OutputTypes,
237 const std::vector<StringRef> &TypesSections,
David Blaikiec3826da2015-12-09 21:02:33 +0000238 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) {
David Blaikie62be5ae2016-04-05 20:26:50 +0000239 for (StringRef Types : TypesSections) {
240 Out.SwitchSection(OutputTypes);
241 uint32_t Offset = 0;
242 DataExtractor Data(Types, true, 0);
243 while (Data.isValidOffset(Offset)) {
244 UnitIndexEntry Entry = CUEntry;
245 // Zero out the debug_info contribution
246 Entry.Contributions[0] = {};
247 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
248 C.Offset = TypesOffset;
249 auto PrevOffset = Offset;
250 // Length of the unit, including the 4 byte length field.
251 C.Length = Data.getU32(&Offset) + 4;
David Blaikiec3826da2015-12-09 21:02:33 +0000252
David Blaikie62be5ae2016-04-05 20:26:50 +0000253 Data.getU16(&Offset); // Version
254 Data.getU32(&Offset); // Abbrev offset
255 Data.getU8(&Offset); // Address size
256 auto Signature = Data.getU64(&Offset);
257 Offset = PrevOffset + C.Length;
David Blaikie24c8ac92015-12-05 03:05:45 +0000258
David Blaikie62be5ae2016-04-05 20:26:50 +0000259 auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry));
260 if (!P.second)
261 continue;
David Blaikief5cb6272015-12-14 07:42:00 +0000262
David Blaikie62be5ae2016-04-05 20:26:50 +0000263 Out.EmitBytes(Types.substr(PrevOffset, C.Length));
264 TypesOffset += C.Length;
265 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000266 }
267}
268
269static void
270writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,
David Blaikie852c02b2016-02-19 21:09:26 +0000271 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries,
David Blaikie24c8ac92015-12-05 03:05:45 +0000272 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) {
273 for (const auto &E : IndexEntries)
David Blaikie852c02b2016-02-19 21:09:26 +0000274 for (size_t i = 0; i != array_lengthof(E.second.Contributions); ++i)
David Blaikie24c8ac92015-12-05 03:05:45 +0000275 if (ContributionOffsets[i])
David Blaikie852c02b2016-02-19 21:09:26 +0000276 Out.EmitIntValue(E.second.Contributions[i].*Field, 4);
David Blaikie24c8ac92015-12-05 03:05:45 +0000277}
278
David Blaikie852c02b2016-02-19 21:09:26 +0000279static void
280writeIndex(MCStreamer &Out, MCSection *Section,
281 ArrayRef<unsigned> ContributionOffsets,
282 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries) {
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000283 if (IndexEntries.empty())
284 return;
285
David Blaikie24c8ac92015-12-05 03:05:45 +0000286 unsigned Columns = 0;
287 for (auto &C : ContributionOffsets)
288 if (C)
289 ++Columns;
290
291 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
292 uint64_t Mask = Buckets.size() - 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000293 size_t i = 0;
294 for (const auto &P : IndexEntries) {
295 auto S = P.first;
David Blaikie24c8ac92015-12-05 03:05:45 +0000296 auto H = S & Mask;
David Blaikie9b492562016-04-05 17:51:40 +0000297 auto HP = ((S >> 32) & Mask) | 1;
David Blaikiec3826da2015-12-09 21:02:33 +0000298 while (Buckets[H]) {
David Blaikie852c02b2016-02-19 21:09:26 +0000299 assert(S != IndexEntries.begin()[Buckets[H] - 1].first &&
David Blaikie74f5b282016-02-19 01:51:44 +0000300 "Duplicate unit");
David Blaikie9b492562016-04-05 17:51:40 +0000301 H = (H + HP) & Mask;
David Blaikiec3826da2015-12-09 21:02:33 +0000302 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000303 Buckets[H] = i + 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000304 ++i;
David Blaikie24c8ac92015-12-05 03:05:45 +0000305 }
306
307 Out.SwitchSection(Section);
308 Out.EmitIntValue(2, 4); // Version
309 Out.EmitIntValue(Columns, 4); // Columns
310 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
David Blaikie2ed678c2015-12-05 03:06:30 +0000311 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets
David Blaikie24c8ac92015-12-05 03:05:45 +0000312
313 // Write the signatures.
314 for (const auto &I : Buckets)
David Blaikie852c02b2016-02-19 21:09:26 +0000315 Out.EmitIntValue(I ? IndexEntries.begin()[I - 1].first : 0, 8);
David Blaikie24c8ac92015-12-05 03:05:45 +0000316
317 // Write the indexes.
318 for (const auto &I : Buckets)
319 Out.EmitIntValue(I, 4);
320
321 // Write the column headers (which sections will appear in the table)
322 for (size_t i = 0; i != ContributionOffsets.size(); ++i)
323 if (ContributionOffsets[i])
324 Out.EmitIntValue(i + DW_SECT_INFO, 4);
325
326 // Write the offsets.
327 writeIndexTable(Out, ContributionOffsets, IndexEntries,
328 &DWARFUnitIndex::Entry::SectionContribution::Offset);
329
330 // Write the lengths.
331 writeIndexTable(Out, ContributionOffsets, IndexEntries,
332 &DWARFUnitIndex::Entry::SectionContribution::Length);
333}
David Blaikie74f5b282016-02-19 01:51:44 +0000334static bool consumeCompressedDebugSectionHeader(StringRef &data,
335 uint64_t &OriginalSize) {
336 // Consume "ZLIB" prefix.
337 if (!data.startswith("ZLIB"))
338 return false;
339 data = data.substr(4);
340 // Consume uncompressed section size (big-endian 8 bytes).
341 DataExtractor extractor(data, false, 8);
342 uint32_t Offset = 0;
343 OriginalSize = extractor.getU64(&Offset);
344 if (Offset == 0)
345 return false;
346 data = data.substr(Offset);
347 return true;
348}
349
David Blaikie4dd03f02016-03-26 20:32:14 +0000350void printDuplicateError(const std::pair<uint64_t, UnitIndexEntry> &PrevE,
351 const CompileUnitIdentifiers &ID, StringRef DWPName) {
352 errs() << "Duplicate DWO ID (" << PrevE.first << ") in '" << PrevE.second.Name
353 << '\'';
354 if (!PrevE.second.DWPName.empty()) {
355 errs() << " (from ";
356 if (!PrevE.second.DWOName.empty())
357 errs() << '\'' << PrevE.second.DWOName << "' in ";
358 errs() << "'" << PrevE.second.DWPName.str() << "')";
359 }
360 errs() << " and '" << ID.Name << '\'';
361 if (!DWPName.empty()) {
362 errs() << " (from ";
363 if (*ID.DWOName)
364 errs() << '\'' << ID.DWOName << "\' in ";
365 errs() << '\'' << DWPName << "')";
366 }
367 errs() << '\n';
368}
David Blaikie242b9482015-12-01 00:48:39 +0000369static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000370 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
371 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
372 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikiec3826da2015-12-09 21:02:33 +0000373 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
David Blaikie23919372016-02-06 01:15:26 +0000374 MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection();
David Blaikie8bce5a02016-02-17 07:00:24 +0000375 MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000376 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
377 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
378 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
379 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
380 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
381 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
David Blaikieb7020252015-12-04 21:16:42 +0000382 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikie23919372016-02-06 01:15:26 +0000383 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
David Blaikie8bce5a02016-02-17 07:00:24 +0000384 {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}},
385 {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}};
David Blaikieb073cb92015-12-02 06:21:34 +0000386
David Blaikie852c02b2016-02-19 21:09:26 +0000387 MapVector<uint64_t, UnitIndexEntry> IndexEntries;
388 MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000389
390 StringMap<uint32_t> Strings;
391 uint32_t StringOffset = 0;
392
David Blaikieb073cb92015-12-02 06:21:34 +0000393 uint32_t ContributionOffsets[8] = {};
394
David Blaikie242b9482015-12-01 00:48:39 +0000395 for (const auto &Input : Inputs) {
396 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
397 if (!ErrOrObj)
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000398 return errorToErrorCode(ErrOrObj.takeError());
David Blaikieb073cb92015-12-02 06:21:34 +0000399
David Blaikie23919372016-02-06 01:15:26 +0000400 UnitIndexEntry CurEntry = {};
David Blaikieb073cb92015-12-02 06:21:34 +0000401
David Blaikie98ad82a2015-12-01 18:07:07 +0000402 StringRef CurStrSection;
403 StringRef CurStrOffsetSection;
David Blaikie62be5ae2016-04-05 20:26:50 +0000404 std::vector<StringRef> CurTypesSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000405 StringRef InfoSection;
406 StringRef AbbrevSection;
David Blaikie23919372016-02-06 01:15:26 +0000407 StringRef CurCUIndexSection;
David Blaikie8bce5a02016-02-17 07:00:24 +0000408 StringRef CurTUIndexSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000409
David Blaikie74f5b282016-02-19 01:51:44 +0000410 SmallVector<SmallString<32>, 4> UncompressedSections;
411
David Blaikieddb27362016-03-01 21:24:04 +0000412 for (const auto &Section : ErrOrObj->getBinary()->sections()) {
David Blaikie74f5b282016-02-19 01:51:44 +0000413 if (Section.isBSS())
414 continue;
415 if (Section.isVirtual())
416 continue;
417
David Blaikie242b9482015-12-01 00:48:39 +0000418 StringRef Name;
419 if (std::error_code Err = Section.getName(Name))
420 return Err;
David Blaikieb073cb92015-12-02 06:21:34 +0000421
David Blaikie74f5b282016-02-19 01:51:44 +0000422 Name = Name.substr(Name.find_first_not_of("._"));
David Blaikieb073cb92015-12-02 06:21:34 +0000423
424 StringRef Contents;
425 if (auto Err = Section.getContents(Contents))
426 return Err;
427
David Blaikie74f5b282016-02-19 01:51:44 +0000428 if (Name.startswith("zdebug_")) {
429 uint64_t OriginalSize;
430 if (!zlib::isAvailable() ||
431 !consumeCompressedDebugSectionHeader(Contents, OriginalSize))
David Blaikie9a5d3642016-02-19 02:03:45 +0000432 return make_error_code(std::errc::invalid_argument);
David Blaikie74f5b282016-02-19 01:51:44 +0000433 UncompressedSections.resize(UncompressedSections.size() + 1);
434 if (zlib::uncompress(Contents, UncompressedSections.back(), OriginalSize) !=
435 zlib::StatusOK) {
436 UncompressedSections.pop_back();
437 continue;
438 }
439 Name = Name.substr(1);
440 Contents = UncompressedSections.back();
441 }
442
443 auto SectionPair = KnownSections.find(Name);
444 if (SectionPair == KnownSections.end())
445 continue;
446
David Blaikieb073cb92015-12-02 06:21:34 +0000447 if (DWARFSectionKind Kind = SectionPair->second.second) {
448 auto Index = Kind - DW_SECT_INFO;
David Blaikiec3826da2015-12-09 21:02:33 +0000449 if (Kind != DW_SECT_TYPES) {
450 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
451 ContributionOffsets[Index] +=
452 (CurEntry.Contributions[Index].Length = Contents.size());
453 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000454
David Blaikie24c8ac92015-12-05 03:05:45 +0000455 switch (Kind) {
456 case DW_SECT_INFO:
David Blaikiead07b5d2015-12-04 17:20:04 +0000457 InfoSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000458 break;
459 case DW_SECT_ABBREV:
David Blaikiead07b5d2015-12-04 17:20:04 +0000460 AbbrevSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000461 break;
David Blaikie24c8ac92015-12-05 03:05:45 +0000462 default:
463 break;
David Blaikiead07b5d2015-12-04 17:20:04 +0000464 }
David Blaikieb073cb92015-12-02 06:21:34 +0000465 }
466
467 MCSection *OutSection = SectionPair->second.first;
468 if (OutSection == StrOffsetSection)
469 CurStrOffsetSection = Contents;
470 else if (OutSection == StrSection)
471 CurStrSection = Contents;
David Blaikiec3826da2015-12-09 21:02:33 +0000472 else if (OutSection == TypesSection)
David Blaikie62be5ae2016-04-05 20:26:50 +0000473 CurTypesSection.push_back(Contents);
David Blaikie23919372016-02-06 01:15:26 +0000474 else if (OutSection == CUIndexSection)
475 CurCUIndexSection = Contents;
David Blaikie8bce5a02016-02-17 07:00:24 +0000476 else if (OutSection == TUIndexSection)
477 CurTUIndexSection = Contents;
David Blaikieb073cb92015-12-02 06:21:34 +0000478 else {
479 Out.SwitchSection(OutSection);
480 Out.EmitBytes(Contents);
David Blaikie98ad82a2015-12-01 18:07:07 +0000481 }
David Blaikie242b9482015-12-01 00:48:39 +0000482 }
David Blaikieb073cb92015-12-02 06:21:34 +0000483
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000484 if (InfoSection.empty())
485 continue;
486
David Blaikie23919372016-02-06 01:15:26 +0000487 if (!CurCUIndexSection.empty()) {
488 DWARFUnitIndex CUIndex(DW_SECT_INFO);
David Blaikieddb27362016-03-01 21:24:04 +0000489 DataExtractor CUIndexData(CurCUIndexSection,
490 ErrOrObj->getBinary()->isLittleEndian(), 0);
David Blaikie23919372016-02-06 01:15:26 +0000491 if (!CUIndex.parse(CUIndexData))
492 return make_error_code(std::errc::invalid_argument);
493
494 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
David Blaikie23919372016-02-06 01:15:26 +0000495 auto *I = E.getOffsets();
496 if (!I)
497 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000498 auto P =
499 IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry));
David Blaikiece7c6cf2016-03-24 22:17:08 +0000500 CompileUnitIdentifiers ID = getCUIdentifiers(
David Blaikief1958da2016-02-26 07:30:15 +0000501 getSubsection(AbbrevSection, E, DW_SECT_ABBREV),
502 getSubsection(InfoSection, E, DW_SECT_INFO),
503 getSubsection(CurStrOffsetSection, E, DW_SECT_STR_OFFSETS),
504 CurStrSection);
505 if (!P.second) {
David Blaikie4dd03f02016-03-26 20:32:14 +0000506 printDuplicateError(*P.first, ID, Input);
David Blaikief1958da2016-02-26 07:30:15 +0000507 return make_error_code(std::errc::invalid_argument);
508 }
David Blaikie852c02b2016-02-19 21:09:26 +0000509 auto &NewEntry = P.first->second;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000510 NewEntry.Name = ID.Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000511 NewEntry.DWOName = ID.DWOName;
David Blaikief1958da2016-02-26 07:30:15 +0000512 NewEntry.DWPName = Input;
David Blaikie23919372016-02-06 01:15:26 +0000513 for (auto Kind : CUIndex.getColumnKinds()) {
514 auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO];
515 C.Offset += I->Offset;
516 C.Length = I->Length;
517 ++I;
518 }
David Blaikie23919372016-02-06 01:15:26 +0000519 }
David Blaikie8bce5a02016-02-17 07:00:24 +0000520
521 if (!CurTypesSection.empty()) {
David Blaikie62be5ae2016-04-05 20:26:50 +0000522 assert(CurTypesSection.size() == 1);
David Blaikie8bce5a02016-02-17 07:00:24 +0000523 if (CurTUIndexSection.empty())
524 return make_error_code(std::errc::invalid_argument);
525 DWARFUnitIndex TUIndex(DW_SECT_TYPES);
David Blaikieddb27362016-03-01 21:24:04 +0000526 DataExtractor TUIndexData(CurTUIndexSection,
527 ErrOrObj->getBinary()->isLittleEndian(), 0);
David Blaikie8bce5a02016-02-17 07:00:24 +0000528 if (!TUIndex.parse(TUIndexData))
529 return make_error_code(std::errc::invalid_argument);
530 addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection,
David Blaikie62be5ae2016-04-05 20:26:50 +0000531 CurTypesSection.front(), CurEntry,
David Blaikie8bce5a02016-02-17 07:00:24 +0000532 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
533 }
David Blaikie23919372016-02-06 01:15:26 +0000534 } else {
David Blaikiece7c6cf2016-03-24 22:17:08 +0000535 CompileUnitIdentifiers ID = getCUIdentifiers(
536 AbbrevSection, InfoSection, CurStrOffsetSection, CurStrSection);
537 auto P = IndexEntries.insert(std::make_pair(ID.Signature, CurEntry));
David Blaikief1958da2016-02-26 07:30:15 +0000538 if (!P.second) {
David Blaikie4dd03f02016-03-26 20:32:14 +0000539 printDuplicateError(*P.first, ID, "");
David Blaikief1958da2016-02-26 07:30:15 +0000540 return make_error_code(std::errc::invalid_argument);
541 }
David Blaikiece7c6cf2016-03-24 22:17:08 +0000542 P.first->second.Name = ID.Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000543 P.first->second.DWOName = ID.DWOName;
David Blaikie23919372016-02-06 01:15:26 +0000544 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection,
545 CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
David Blaikie23919372016-02-06 01:15:26 +0000546 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000547
David Blaikiebb94e442015-12-01 19:17:58 +0000548 if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset,
549 StrSection, StrOffsetSection,
550 CurStrSection, CurStrOffsetSection))
David Blaikie98ad82a2015-12-01 18:07:07 +0000551 return Err;
David Blaikie242b9482015-12-01 00:48:39 +0000552 }
David Blaikieb073cb92015-12-02 06:21:34 +0000553
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000554 // Lie about there being no info contributions so the TU index only includes
555 // the type unit contribution
556 ContributionOffsets[0] = 0;
557 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
558 TypeIndexEntries);
David Blaikieb3757c02015-12-02 22:01:56 +0000559
David Blaikie24c8ac92015-12-05 03:05:45 +0000560 // Lie about the type contribution
561 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
562 // Unlie about the info contribution
563 ContributionOffsets[0] = 1;
David Blaikie7c4ffe02015-12-04 21:30:23 +0000564
David Blaikie24c8ac92015-12-05 03:05:45 +0000565 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
566 IndexEntries);
David Blaikieddb27362016-03-01 21:24:04 +0000567
David Blaikie242b9482015-12-01 00:48:39 +0000568 return std::error_code();
569}
570
David Blaikie2ed678c2015-12-05 03:06:30 +0000571int main(int argc, char **argv) {
David Blaikie242b9482015-12-01 00:48:39 +0000572
573 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
574
575 llvm::InitializeAllTargetInfos();
576 llvm::InitializeAllTargetMCs();
577 llvm::InitializeAllTargets();
578 llvm::InitializeAllAsmPrinters();
579
580 std::string ErrorStr;
581 StringRef Context = "dwarf streamer init";
582
583 Triple TheTriple("x86_64-linux-gnu");
584
585 // Get the target.
586 const Target *TheTarget =
587 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
588 if (!TheTarget)
589 return error(ErrorStr, Context);
590 std::string TripleName = TheTriple.getTriple();
591
592 // Create all the MC Objects.
593 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
594 if (!MRI)
595 return error(Twine("no register info for target ") + TripleName, Context);
596
597 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
598 if (!MAI)
599 return error("no asm info for target " + TripleName, Context);
600
601 MCObjectFileInfo MOFI;
602 MCContext MC(MAI.get(), MRI.get(), &MOFI);
David Blaikie2ed678c2015-12-05 03:06:30 +0000603 MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default, MC);
David Blaikie242b9482015-12-01 00:48:39 +0000604
605 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
606 if (!MAB)
607 return error("no asm backend for target " + TripleName, Context);
608
609 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
610 if (!MII)
611 return error("no instr info info for target " + TripleName, Context);
612
613 std::unique_ptr<MCSubtargetInfo> MSTI(
614 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
615 if (!MSTI)
616 return error("no subtarget info for target " + TripleName, Context);
617
618 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
619 if (!MCE)
620 return error("no code emitter for target " + TripleName, Context);
621
622 // Create the output file.
623 std::error_code EC;
624 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
625 if (EC)
626 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
627
David Majnemer03e2cc32015-12-21 22:09:27 +0000628 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
David Blaikie242b9482015-12-01 00:48:39 +0000629 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
David Majnemer03e2cc32015-12-21 22:09:27 +0000630 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, MCOptions.MCRelaxAll,
631 MCOptions.MCIncrementalLinkerCompatible,
David Blaikie242b9482015-12-01 00:48:39 +0000632 /*DWARFMustBeAtTheEnd*/ false));
633 if (!MS)
634 return error("no object streamer for target " + TripleName, Context);
635
636 if (auto Err = write(*MS, InputFiles))
637 return error(Err.message(), "Writing DWP file");
David Blaikieddb27362016-03-01 21:24:04 +0000638
639 MS->Finish();
David Blaikiedf055252015-12-01 00:48:34 +0000640}