blob: b88f0ef37d7c5b6a911833a077c49c6cf440e28c [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 Blaikiefd800922016-05-23 16:32:11 +000014#include "DWPError.h"
15#include "DWPStringPool.h"
David Blaikie852c02b2016-02-19 21:09:26 +000016#include "llvm/ADT/MapVector.h"
David Blaikie242b9482015-12-01 00:48:39 +000017#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/StringSet.h"
19#include "llvm/CodeGen/AsmPrinter.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000020#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
21#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
David Blaikie242b9482015-12-01 00:48:39 +000022#include "llvm/MC/MCAsmInfo.h"
23#include "llvm/MC/MCContext.h"
24#include "llvm/MC/MCInstrInfo.h"
25#include "llvm/MC/MCObjectFileInfo.h"
26#include "llvm/MC/MCRegisterInfo.h"
27#include "llvm/MC/MCSectionELF.h"
28#include "llvm/MC/MCStreamer.h"
David Majnemer03e2cc32015-12-21 22:09:27 +000029#include "llvm/MC/MCTargetOptionsCommandFlags.h"
David Blaikie242b9482015-12-01 00:48:39 +000030#include "llvm/Object/ObjectFile.h"
David Blaikie74f5b282016-02-19 01:51:44 +000031#include "llvm/Support/Compression.h"
David Blaikie98ad82a2015-12-01 18:07:07 +000032#include "llvm/Support/DataExtractor.h"
David Blaikiefd800922016-05-23 16:32:11 +000033#include "llvm/Support/Error.h"
David Blaikie242b9482015-12-01 00:48:39 +000034#include "llvm/Support/FileSystem.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000035#include "llvm/Support/MathExtras.h"
David Blaikie242b9482015-12-01 00:48:39 +000036#include "llvm/Support/MemoryBuffer.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000037#include "llvm/Support/Options.h"
David Blaikie242b9482015-12-01 00:48:39 +000038#include "llvm/Support/TargetRegistry.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000039#include "llvm/Support/TargetSelect.h"
David Blaikie242b9482015-12-01 00:48:39 +000040#include "llvm/Support/raw_ostream.h"
41#include "llvm/Target/TargetMachine.h"
David Blaikief1958da2016-02-26 07:30:15 +000042#include <iostream>
David Blaikie2ed678c2015-12-05 03:06:30 +000043#include <memory>
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
David Blaikiefd800922016-05-23 16:32:11 +000058static void writeStringsAndOffsets(MCStreamer &Out, DWPStringPool &Strings,
59 MCSection *StrOffsetSection,
60 StringRef CurStrSection,
61 StringRef CurStrOffsetSection) {
David Blaikie98ad82a2015-12-01 18:07:07 +000062 // Could possibly produce an error or warning if one of these was non-null but
63 // the other was null.
64 if (CurStrSection.empty() || CurStrOffsetSection.empty())
David Blaikiebc619cd2016-05-17 23:44:13 +000065 return;
David Blaikie98ad82a2015-12-01 18:07:07 +000066
67 DenseMap<uint32_t, uint32_t> OffsetRemapping;
68
69 DataExtractor Data(CurStrSection, true, 0);
70 uint32_t LocalOffset = 0;
71 uint32_t PrevOffset = 0;
72 while (const char *s = Data.getCStr(&LocalOffset)) {
David Blaikiefd800922016-05-23 16:32:11 +000073 OffsetRemapping[PrevOffset] =
74 Strings.getOffset(s, LocalOffset - PrevOffset);
David Blaikie98ad82a2015-12-01 18:07:07 +000075 PrevOffset = LocalOffset;
76 }
77
78 Data = DataExtractor(CurStrOffsetSection, true, 0);
79
80 Out.SwitchSection(StrOffsetSection);
81
82 uint32_t Offset = 0;
83 uint64_t Size = CurStrOffsetSection.size();
84 while (Offset < Size) {
85 auto OldOffset = Data.getU32(&Offset);
86 auto NewOffset = OffsetRemapping[OldOffset];
87 Out.EmitIntValue(NewOffset, 4);
88 }
David Blaikie242b9482015-12-01 00:48:39 +000089}
90
David Blaikiead07b5d2015-12-04 17:20:04 +000091static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
92 uint64_t CurCode;
93 uint32_t Offset = 0;
94 DataExtractor AbbrevData(Abbrev, true, 0);
95 while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) {
96 // Tag
97 AbbrevData.getULEB128(&Offset);
98 // DW_CHILDREN
99 AbbrevData.getU8(&Offset);
100 // Attributes
101 while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset))
102 ;
103 }
104 return Offset;
105}
106
David Blaikiece7c6cf2016-03-24 22:17:08 +0000107struct CompileUnitIdentifiers {
108 uint64_t Signature = 0;
David Blaikie4dd03f02016-03-26 20:32:14 +0000109 const char *Name = "";
110 const char *DWOName = "";
David Blaikiece7c6cf2016-03-24 22:17:08 +0000111};
112
David Blaikie4940f872016-05-17 00:07:10 +0000113static Expected<const char *>
114getIndexedString(uint32_t Form, DataExtractor InfoData, uint32_t &InfoOffset,
115 StringRef StrOffsets, StringRef Str) {
David Blaikie60fbd3b2016-04-05 20:16:38 +0000116 if (Form == dwarf::DW_FORM_string)
117 return InfoData.getCStr(&InfoOffset);
David Blaikie4940f872016-05-17 00:07:10 +0000118 if (Form != dwarf::DW_FORM_GNU_str_index)
119 return make_error<DWPError>(
120 "string field encoded without DW_FORM_string or DW_FORM_GNU_str_index");
David Blaikie60fbd3b2016-04-05 20:16:38 +0000121 auto StrIndex = InfoData.getULEB128(&InfoOffset);
David Blaikie4dd03f02016-03-26 20:32:14 +0000122 DataExtractor StrOffsetsData(StrOffsets, true, 0);
123 uint32_t StrOffsetsOffset = 4 * StrIndex;
124 uint32_t StrOffset = StrOffsetsData.getU32(&StrOffsetsOffset);
125 DataExtractor StrData(Str, true, 0);
126 return StrData.getCStr(&StrOffset);
127}
128
David Blaikie7bb62ef2016-05-16 23:26:29 +0000129static Expected<CompileUnitIdentifiers> getCUIdentifiers(StringRef Abbrev,
130 StringRef Info,
131 StringRef StrOffsets,
132 StringRef Str) {
David Blaikief1958da2016-02-26 07:30:15 +0000133 uint32_t Offset = 0;
134 DataExtractor InfoData(Info, true, 0);
135 InfoData.getU32(&Offset); // Length
136 uint16_t Version = InfoData.getU16(&Offset);
137 InfoData.getU32(&Offset); // Abbrev offset (should be zero)
138 uint8_t AddrSize = InfoData.getU8(&Offset);
139
140 uint32_t AbbrCode = InfoData.getULEB128(&Offset);
141
142 DataExtractor AbbrevData(Abbrev, true, 0);
143 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
144 uint64_t Tag = AbbrevData.getULEB128(&AbbrevOffset);
David Blaikie7bb62ef2016-05-16 23:26:29 +0000145 if (Tag != dwarf::DW_TAG_compile_unit)
146 return make_error<DWPError>("top level DIE is not a compile unit");
David Blaikief1958da2016-02-26 07:30:15 +0000147 // 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: {
David Blaikie4940f872016-05-17 00:07:10 +0000157 Expected<const char *> EName =
158 getIndexedString(Form, InfoData, Offset, StrOffsets, Str);
159 if (!EName)
160 return EName.takeError();
161 ID.Name = *EName;
David Blaikie4dd03f02016-03-26 20:32:14 +0000162 break;
163 }
164 case dwarf::DW_AT_GNU_dwo_name: {
David Blaikie4940f872016-05-17 00:07:10 +0000165 Expected<const char *> EName =
166 getIndexedString(Form, InfoData, Offset, StrOffsets, Str);
167 if (!EName)
168 return EName.takeError();
169 ID.DWOName = *EName;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000170 break;
171 }
172 case dwarf::DW_AT_GNU_dwo_id:
173 ID.Signature = InfoData.getU64(&Offset);
174 break;
175 default:
176 DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize);
177 }
David Blaikief1958da2016-02-26 07:30:15 +0000178 }
David Blaikiece7c6cf2016-03-24 22:17:08 +0000179 return ID;
David Blaikiead07b5d2015-12-04 17:20:04 +0000180}
181
David Blaikie24c8ac92015-12-05 03:05:45 +0000182struct UnitIndexEntry {
David Blaikie24c8ac92015-12-05 03:05:45 +0000183 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
David Blaikief1958da2016-02-26 07:30:15 +0000184 std::string Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000185 std::string DWOName;
David Blaikief1958da2016-02-26 07:30:15 +0000186 StringRef DWPName;
David Blaikie24c8ac92015-12-05 03:05:45 +0000187};
188
David Blaikiefd800922016-05-23 16:32:11 +0000189static StringRef getSubsection(StringRef Section,
190 const DWARFUnitIndex::Entry &Entry,
191 DWARFSectionKind Kind) {
David Blaikief1958da2016-02-26 07:30:15 +0000192 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 Blaikie62be5ae2016-04-05 20:26:50 +0000230 MCSection *OutputTypes,
231 const std::vector<StringRef> &TypesSections,
David Blaikiec3826da2015-12-09 21:02:33 +0000232 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) {
David Blaikie62be5ae2016-04-05 20:26:50 +0000233 for (StringRef Types : TypesSections) {
234 Out.SwitchSection(OutputTypes);
235 uint32_t Offset = 0;
236 DataExtractor Data(Types, true, 0);
237 while (Data.isValidOffset(Offset)) {
238 UnitIndexEntry Entry = CUEntry;
239 // Zero out the debug_info contribution
240 Entry.Contributions[0] = {};
241 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
242 C.Offset = TypesOffset;
243 auto PrevOffset = Offset;
244 // Length of the unit, including the 4 byte length field.
245 C.Length = Data.getU32(&Offset) + 4;
David Blaikiec3826da2015-12-09 21:02:33 +0000246
David Blaikie62be5ae2016-04-05 20:26:50 +0000247 Data.getU16(&Offset); // Version
248 Data.getU32(&Offset); // Abbrev offset
249 Data.getU8(&Offset); // Address size
250 auto Signature = Data.getU64(&Offset);
251 Offset = PrevOffset + C.Length;
David Blaikie24c8ac92015-12-05 03:05:45 +0000252
David Blaikie62be5ae2016-04-05 20:26:50 +0000253 auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry));
254 if (!P.second)
255 continue;
David Blaikief5cb6272015-12-14 07:42:00 +0000256
David Blaikie62be5ae2016-04-05 20:26:50 +0000257 Out.EmitBytes(Types.substr(PrevOffset, C.Length));
258 TypesOffset += C.Length;
259 }
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 Blaikied1f7ab32016-05-16 20:42:27 +0000344std::string buildDWODescription(StringRef Name, StringRef DWPName, StringRef DWOName) {
345 std::string Text = "\'";
346 Text += Name;
347 Text += '\'';
David Blaikie4dd03f02016-03-26 20:32:14 +0000348 if (!DWPName.empty()) {
David Blaikied1f7ab32016-05-16 20:42:27 +0000349 Text += " (from ";
350 if (!DWOName.empty()) {
351 Text += '\'';
352 Text += DWOName;
353 Text += "' in ";
354 }
355 Text += '\'';
356 Text += DWPName;
357 Text += "')";
David Blaikie4dd03f02016-03-26 20:32:14 +0000358 }
David Blaikied1f7ab32016-05-16 20:42:27 +0000359 return Text;
360}
361
David Blaikie11825c72016-05-17 19:40:28 +0000362Error buildDuplicateError(const std::pair<uint64_t, UnitIndexEntry> &PrevE,
363 const CompileUnitIdentifiers &ID, StringRef DWPName) {
364 return make_error<DWPError>(
365 std::string("Duplicate DWO ID (") + utohexstr(PrevE.first) + ") in " +
366 buildDWODescription(PrevE.second.Name, PrevE.second.DWPName,
367 PrevE.second.DWOName) +
368 " and " + buildDWODescription(ID.Name, DWPName, ID.DWOName));
David Blaikie4dd03f02016-03-26 20:32:14 +0000369}
David Blaikiebc8397c2016-05-12 19:59:54 +0000370static Error write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000371 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
372 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
373 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikiec3826da2015-12-09 21:02:33 +0000374 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
David Blaikie23919372016-02-06 01:15:26 +0000375 MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection();
David Blaikie8bce5a02016-02-17 07:00:24 +0000376 MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000377 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
378 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
379 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
380 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
381 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
382 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
David Blaikieb7020252015-12-04 21:16:42 +0000383 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikie23919372016-02-06 01:15:26 +0000384 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
David Blaikie8bce5a02016-02-17 07:00:24 +0000385 {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}},
386 {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}};
David Blaikieb073cb92015-12-02 06:21:34 +0000387
David Blaikie852c02b2016-02-19 21:09:26 +0000388 MapVector<uint64_t, UnitIndexEntry> IndexEntries;
389 MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000390
David Blaikieb073cb92015-12-02 06:21:34 +0000391 uint32_t ContributionOffsets[8] = {};
392
David Blaikiefd800922016-05-23 16:32:11 +0000393 DWPStringPool Strings(Out, StrSection);
394
395 SmallVector<OwningBinary<object::ObjectFile>, 128> Objects;
396 Objects.reserve(Inputs.size());
397
David Blaikie2e9bd892016-05-23 17:35:51 +0000398 SmallVector<SmallString<32>, 4> UncompressedSections;
399
David Blaikie242b9482015-12-01 00:48:39 +0000400 for (const auto &Input : Inputs) {
401 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
402 if (!ErrOrObj)
David Blaikiebc8397c2016-05-12 19:59:54 +0000403 return ErrOrObj.takeError();
David Blaikieb073cb92015-12-02 06:21:34 +0000404
David Blaikiefd800922016-05-23 16:32:11 +0000405 auto &Obj = *ErrOrObj->getBinary();
406 Objects.push_back(std::move(*ErrOrObj));
407
David Blaikie23919372016-02-06 01:15:26 +0000408 UnitIndexEntry CurEntry = {};
David Blaikieb073cb92015-12-02 06:21:34 +0000409
David Blaikie98ad82a2015-12-01 18:07:07 +0000410 StringRef CurStrSection;
411 StringRef CurStrOffsetSection;
David Blaikie62be5ae2016-04-05 20:26:50 +0000412 std::vector<StringRef> CurTypesSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000413 StringRef InfoSection;
414 StringRef AbbrevSection;
David Blaikie23919372016-02-06 01:15:26 +0000415 StringRef CurCUIndexSection;
David Blaikie8bce5a02016-02-17 07:00:24 +0000416 StringRef CurTUIndexSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000417
David Blaikiefd800922016-05-23 16:32:11 +0000418 for (const auto &Section : Obj.sections()) {
David Blaikie74f5b282016-02-19 01:51:44 +0000419 if (Section.isBSS())
420 continue;
421 if (Section.isVirtual())
422 continue;
423
David Blaikie242b9482015-12-01 00:48:39 +0000424 StringRef Name;
425 if (std::error_code Err = Section.getName(Name))
David Blaikiebc8397c2016-05-12 19:59:54 +0000426 return errorCodeToError(Err);
David Blaikieb073cb92015-12-02 06:21:34 +0000427
David Blaikie74f5b282016-02-19 01:51:44 +0000428 Name = Name.substr(Name.find_first_not_of("._"));
David Blaikieb073cb92015-12-02 06:21:34 +0000429
430 StringRef Contents;
431 if (auto Err = Section.getContents(Contents))
David Blaikiebc8397c2016-05-12 19:59:54 +0000432 return errorCodeToError(Err);
David Blaikieb073cb92015-12-02 06:21:34 +0000433
David Blaikie74f5b282016-02-19 01:51:44 +0000434 if (Name.startswith("zdebug_")) {
435 uint64_t OriginalSize;
David Blaikiebc8397c2016-05-12 19:59:54 +0000436 if (!zlib::isAvailable())
437 return make_error<DWPError>("zlib not available");
438 if (!consumeCompressedDebugSectionHeader(Contents, OriginalSize))
439 return make_error<DWPError>(
440 ("failure while decompressing compressed section: '" + Name +
David Blaikiefd800922016-05-23 16:32:11 +0000441 "\'")
442 .str());
David Blaikie74f5b282016-02-19 01:51:44 +0000443 UncompressedSections.resize(UncompressedSections.size() + 1);
David Blaikiefd800922016-05-23 16:32:11 +0000444 if (zlib::uncompress(Contents, UncompressedSections.back(),
445 OriginalSize) != zlib::StatusOK) {
David Blaikie74f5b282016-02-19 01:51:44 +0000446 UncompressedSections.pop_back();
447 continue;
448 }
449 Name = Name.substr(1);
450 Contents = UncompressedSections.back();
451 }
452
453 auto SectionPair = KnownSections.find(Name);
454 if (SectionPair == KnownSections.end())
455 continue;
456
David Blaikieb073cb92015-12-02 06:21:34 +0000457 if (DWARFSectionKind Kind = SectionPair->second.second) {
458 auto Index = Kind - DW_SECT_INFO;
David Blaikiec3826da2015-12-09 21:02:33 +0000459 if (Kind != DW_SECT_TYPES) {
460 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
461 ContributionOffsets[Index] +=
462 (CurEntry.Contributions[Index].Length = Contents.size());
463 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000464
David Blaikie24c8ac92015-12-05 03:05:45 +0000465 switch (Kind) {
466 case DW_SECT_INFO:
David Blaikiead07b5d2015-12-04 17:20:04 +0000467 InfoSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000468 break;
469 case DW_SECT_ABBREV:
David Blaikiead07b5d2015-12-04 17:20:04 +0000470 AbbrevSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000471 break;
David Blaikie24c8ac92015-12-05 03:05:45 +0000472 default:
473 break;
David Blaikiead07b5d2015-12-04 17:20:04 +0000474 }
David Blaikieb073cb92015-12-02 06:21:34 +0000475 }
476
477 MCSection *OutSection = SectionPair->second.first;
478 if (OutSection == StrOffsetSection)
479 CurStrOffsetSection = Contents;
480 else if (OutSection == StrSection)
481 CurStrSection = Contents;
David Blaikiec3826da2015-12-09 21:02:33 +0000482 else if (OutSection == TypesSection)
David Blaikie62be5ae2016-04-05 20:26:50 +0000483 CurTypesSection.push_back(Contents);
David Blaikie23919372016-02-06 01:15:26 +0000484 else if (OutSection == CUIndexSection)
485 CurCUIndexSection = Contents;
David Blaikie8bce5a02016-02-17 07:00:24 +0000486 else if (OutSection == TUIndexSection)
487 CurTUIndexSection = Contents;
David Blaikieb073cb92015-12-02 06:21:34 +0000488 else {
489 Out.SwitchSection(OutSection);
490 Out.EmitBytes(Contents);
David Blaikie98ad82a2015-12-01 18:07:07 +0000491 }
David Blaikie242b9482015-12-01 00:48:39 +0000492 }
David Blaikieb073cb92015-12-02 06:21:34 +0000493
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000494 if (InfoSection.empty())
495 continue;
496
David Blaikie23919372016-02-06 01:15:26 +0000497 if (!CurCUIndexSection.empty()) {
498 DWARFUnitIndex CUIndex(DW_SECT_INFO);
David Blaikiefd800922016-05-23 16:32:11 +0000499 DataExtractor CUIndexData(CurCUIndexSection, Obj.isLittleEndian(), 0);
David Blaikie23919372016-02-06 01:15:26 +0000500 if (!CUIndex.parse(CUIndexData))
David Blaikiebc8397c2016-05-12 19:59:54 +0000501 return make_error<DWPError>("Failed to parse cu_index");
David Blaikie23919372016-02-06 01:15:26 +0000502
503 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
David Blaikie23919372016-02-06 01:15:26 +0000504 auto *I = E.getOffsets();
505 if (!I)
506 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000507 auto P =
508 IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry));
David Blaikie7bb62ef2016-05-16 23:26:29 +0000509 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(
David Blaikief1958da2016-02-26 07:30:15 +0000510 getSubsection(AbbrevSection, E, DW_SECT_ABBREV),
511 getSubsection(InfoSection, E, DW_SECT_INFO),
512 getSubsection(CurStrOffsetSection, E, DW_SECT_STR_OFFSETS),
513 CurStrSection);
David Blaikie7bb62ef2016-05-16 23:26:29 +0000514 if (!EID)
515 return EID.takeError();
516 const auto &ID = *EID;
David Blaikied1f7ab32016-05-16 20:42:27 +0000517 if (!P.second)
David Blaikie11825c72016-05-17 19:40:28 +0000518 return buildDuplicateError(*P.first, ID, Input);
David Blaikie852c02b2016-02-19 21:09:26 +0000519 auto &NewEntry = P.first->second;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000520 NewEntry.Name = ID.Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000521 NewEntry.DWOName = ID.DWOName;
David Blaikief1958da2016-02-26 07:30:15 +0000522 NewEntry.DWPName = Input;
David Blaikie23919372016-02-06 01:15:26 +0000523 for (auto Kind : CUIndex.getColumnKinds()) {
524 auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO];
525 C.Offset += I->Offset;
526 C.Length = I->Length;
527 ++I;
528 }
David Blaikie23919372016-02-06 01:15:26 +0000529 }
David Blaikie8bce5a02016-02-17 07:00:24 +0000530
531 if (!CurTypesSection.empty()) {
David Blaikie8bef4122016-05-17 22:00:57 +0000532 if (CurTypesSection.size() != 1)
533 return make_error<DWPError>(
534 "multiple type unit sections in .dwp file");
David Blaikie8bce5a02016-02-17 07:00:24 +0000535 DWARFUnitIndex TUIndex(DW_SECT_TYPES);
David Blaikiefd800922016-05-23 16:32:11 +0000536 DataExtractor TUIndexData(CurTUIndexSection, Obj.isLittleEndian(), 0);
David Blaikie8bce5a02016-02-17 07:00:24 +0000537 if (!TUIndex.parse(TUIndexData))
David Blaikiebc8397c2016-05-12 19:59:54 +0000538 return make_error<DWPError>("Failed to parse tu_index");
David Blaikie8bce5a02016-02-17 07:00:24 +0000539 addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection,
David Blaikie62be5ae2016-04-05 20:26:50 +0000540 CurTypesSection.front(), CurEntry,
David Blaikie8bce5a02016-02-17 07:00:24 +0000541 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
542 }
David Blaikie23919372016-02-06 01:15:26 +0000543 } else {
David Blaikie7bb62ef2016-05-16 23:26:29 +0000544 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(
David Blaikiece7c6cf2016-03-24 22:17:08 +0000545 AbbrevSection, InfoSection, CurStrOffsetSection, CurStrSection);
David Blaikie7bb62ef2016-05-16 23:26:29 +0000546 if (!EID)
547 return EID.takeError();
548 const auto &ID = *EID;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000549 auto P = IndexEntries.insert(std::make_pair(ID.Signature, CurEntry));
David Blaikied1f7ab32016-05-16 20:42:27 +0000550 if (!P.second)
David Blaikie11825c72016-05-17 19:40:28 +0000551 return buildDuplicateError(*P.first, ID, "");
David Blaikiece7c6cf2016-03-24 22:17:08 +0000552 P.first->second.Name = ID.Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000553 P.first->second.DWOName = ID.DWOName;
David Blaikie23919372016-02-06 01:15:26 +0000554 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection,
555 CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
David Blaikie23919372016-02-06 01:15:26 +0000556 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000557
David Blaikiefd800922016-05-23 16:32:11 +0000558 writeStringsAndOffsets(Out, Strings, StrOffsetSection, CurStrSection,
David Blaikiebc619cd2016-05-17 23:44:13 +0000559 CurStrOffsetSection);
David Blaikie242b9482015-12-01 00:48:39 +0000560 }
David Blaikieb073cb92015-12-02 06:21:34 +0000561
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000562 // Lie about there being no info contributions so the TU index only includes
563 // the type unit contribution
564 ContributionOffsets[0] = 0;
565 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
566 TypeIndexEntries);
David Blaikieb3757c02015-12-02 22:01:56 +0000567
David Blaikie24c8ac92015-12-05 03:05:45 +0000568 // Lie about the type contribution
569 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
570 // Unlie about the info contribution
571 ContributionOffsets[0] = 1;
David Blaikie7c4ffe02015-12-04 21:30:23 +0000572
David Blaikie24c8ac92015-12-05 03:05:45 +0000573 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
574 IndexEntries);
David Blaikieddb27362016-03-01 21:24:04 +0000575
David Blaikiebc8397c2016-05-12 19:59:54 +0000576 return Error();
David Blaikie242b9482015-12-01 00:48:39 +0000577}
578
David Blaikie17ab78e2016-05-17 23:37:44 +0000579static int error(const Twine &Error, const Twine &Context) {
580 errs() << Twine("while processing ") + Context + ":\n";
581 errs() << Twine("error: ") + Error + "\n";
582 return 1;
583}
584
David Blaikie2ed678c2015-12-05 03:06:30 +0000585int main(int argc, char **argv) {
David Blaikie242b9482015-12-01 00:48:39 +0000586
587 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
588
589 llvm::InitializeAllTargetInfos();
590 llvm::InitializeAllTargetMCs();
591 llvm::InitializeAllTargets();
592 llvm::InitializeAllAsmPrinters();
593
594 std::string ErrorStr;
595 StringRef Context = "dwarf streamer init";
596
597 Triple TheTriple("x86_64-linux-gnu");
598
599 // Get the target.
600 const Target *TheTarget =
601 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
602 if (!TheTarget)
603 return error(ErrorStr, Context);
604 std::string TripleName = TheTriple.getTriple();
605
606 // Create all the MC Objects.
607 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
608 if (!MRI)
609 return error(Twine("no register info for target ") + TripleName, Context);
610
611 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
612 if (!MAI)
613 return error("no asm info for target " + TripleName, Context);
614
615 MCObjectFileInfo MOFI;
616 MCContext MC(MAI.get(), MRI.get(), &MOFI);
Rafael Espindola699281c2016-05-18 11:58:50 +0000617 MOFI.InitMCObjectFileInfo(TheTriple, /*PIC*/ false, CodeModel::Default, MC);
David Blaikie242b9482015-12-01 00:48:39 +0000618
619 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
620 if (!MAB)
621 return error("no asm backend for target " + TripleName, Context);
622
623 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
624 if (!MII)
625 return error("no instr info info for target " + TripleName, Context);
626
627 std::unique_ptr<MCSubtargetInfo> MSTI(
628 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
629 if (!MSTI)
630 return error("no subtarget info for target " + TripleName, Context);
631
632 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
633 if (!MCE)
634 return error("no code emitter for target " + TripleName, Context);
635
636 // Create the output file.
637 std::error_code EC;
638 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
639 if (EC)
640 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
641
David Majnemer03e2cc32015-12-21 22:09:27 +0000642 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
David Blaikie242b9482015-12-01 00:48:39 +0000643 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
David Majnemer03e2cc32015-12-21 22:09:27 +0000644 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, MCOptions.MCRelaxAll,
645 MCOptions.MCIncrementalLinkerCompatible,
David Blaikie242b9482015-12-01 00:48:39 +0000646 /*DWARFMustBeAtTheEnd*/ false));
647 if (!MS)
648 return error("no object streamer for target " + TripleName, Context);
649
David Blaikiebc8397c2016-05-12 19:59:54 +0000650 if (auto Err = write(*MS, InputFiles)) {
651 logAllUnhandledErrors(std::move(Err), errs(), "error: ");
652 return 1;
653 }
David Blaikieddb27362016-03-01 21:24:04 +0000654
655 MS->Finish();
David Blaikiedf055252015-12-01 00:48:34 +0000656}