blob: f25c87c241a834a9043f106eb1faa9e9ef1b78b5 [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"
David Blaikiebc8397c2016-05-12 19:59:54 +000038#include "llvm/Support/Error.h"
David Blaikie242b9482015-12-01 00:48:39 +000039#include "llvm/Target/TargetMachine.h"
David Blaikiebc8397c2016-05-12 19:59:54 +000040#include "DWPError.h"
David Blaikief1958da2016-02-26 07:30:15 +000041#include <iostream>
David Blaikie2ed678c2015-12-05 03:06:30 +000042#include <memory>
David Blaikie242b9482015-12-01 00:48:39 +000043
44using namespace llvm;
David Blaikie98ad82a2015-12-01 18:07:07 +000045using namespace llvm::object;
David Blaikie242b9482015-12-01 00:48:39 +000046using namespace cl;
47
48OptionCategory DwpCategory("Specific Options");
49static list<std::string> InputFiles(Positional, OneOrMore,
50 desc("<input files>"), cat(DwpCategory));
51
David Blaikie2ed678c2015-12-05 03:06:30 +000052static opt<std::string> OutputFilename(Required, "o",
53 desc("Specify the output file."),
54 value_desc("filename"),
55 cat(DwpCategory));
David Blaikie242b9482015-12-01 00:48:39 +000056
57static int error(const Twine &Error, const Twine &Context) {
58 errs() << Twine("while processing ") + Context + ":\n";
59 errs() << Twine("error: ") + Error + "\n";
60 return 1;
61}
62
David Blaikiebc8397c2016-05-12 19:59:54 +000063static Error
David Blaikie98ad82a2015-12-01 18:07:07 +000064writeStringsAndOffsets(MCStreamer &Out, StringMap<uint32_t> &Strings,
David Blaikiebb94e442015-12-01 19:17:58 +000065 uint32_t &StringOffset, MCSection *StrSection,
66 MCSection *StrOffsetSection, StringRef CurStrSection,
67 StringRef CurStrOffsetSection) {
David Blaikie98ad82a2015-12-01 18:07:07 +000068 // Could possibly produce an error or warning if one of these was non-null but
69 // the other was null.
70 if (CurStrSection.empty() || CurStrOffsetSection.empty())
David Blaikiebc8397c2016-05-12 19:59:54 +000071 return Error();
David Blaikie98ad82a2015-12-01 18:07:07 +000072
73 DenseMap<uint32_t, uint32_t> OffsetRemapping;
74
75 DataExtractor Data(CurStrSection, true, 0);
76 uint32_t LocalOffset = 0;
77 uint32_t PrevOffset = 0;
78 while (const char *s = Data.getCStr(&LocalOffset)) {
79 StringRef Str(s, LocalOffset - PrevOffset - 1);
David Blaikiebb94e442015-12-01 19:17:58 +000080 auto Pair = Strings.insert(std::make_pair(Str, StringOffset));
81 if (Pair.second) {
82 Out.SwitchSection(StrSection);
83 Out.EmitBytes(
84 StringRef(Pair.first->getKeyData(), Pair.first->getKeyLength() + 1));
85 StringOffset += Str.size() + 1;
86 }
87 OffsetRemapping[PrevOffset] = Pair.first->second;
David Blaikie98ad82a2015-12-01 18:07:07 +000088 PrevOffset = LocalOffset;
89 }
90
91 Data = DataExtractor(CurStrOffsetSection, true, 0);
92
93 Out.SwitchSection(StrOffsetSection);
94
95 uint32_t Offset = 0;
96 uint64_t Size = CurStrOffsetSection.size();
97 while (Offset < Size) {
98 auto OldOffset = Data.getU32(&Offset);
99 auto NewOffset = OffsetRemapping[OldOffset];
100 Out.EmitIntValue(NewOffset, 4);
101 }
102
David Blaikiebc8397c2016-05-12 19:59:54 +0000103 return Error();
David Blaikie242b9482015-12-01 00:48:39 +0000104}
105
David Blaikiead07b5d2015-12-04 17:20:04 +0000106static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
107 uint64_t CurCode;
108 uint32_t Offset = 0;
109 DataExtractor AbbrevData(Abbrev, true, 0);
110 while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) {
111 // Tag
112 AbbrevData.getULEB128(&Offset);
113 // DW_CHILDREN
114 AbbrevData.getU8(&Offset);
115 // Attributes
116 while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset))
117 ;
118 }
119 return Offset;
120}
121
David Blaikiece7c6cf2016-03-24 22:17:08 +0000122struct CompileUnitIdentifiers {
123 uint64_t Signature = 0;
David Blaikie4dd03f02016-03-26 20:32:14 +0000124 const char *Name = "";
125 const char *DWOName = "";
David Blaikiece7c6cf2016-03-24 22:17:08 +0000126};
127
David Blaikie60fbd3b2016-04-05 20:16:38 +0000128static const char *getIndexedString(uint32_t Form, DataExtractor InfoData,
129 uint32_t &InfoOffset, StringRef StrOffsets,
David Blaikie4dd03f02016-03-26 20:32:14 +0000130 StringRef Str) {
David Blaikie60fbd3b2016-04-05 20:16:38 +0000131 if (Form == dwarf::DW_FORM_string)
132 return InfoData.getCStr(&InfoOffset);
David Blaikieee210802016-04-13 18:38:33 +0000133 assert(Form == dwarf::DW_FORM_GNU_str_index && "Only string and str_index "
134 "forms are supported for DWP "
135 "string attributes");
David Blaikie60fbd3b2016-04-05 20:16:38 +0000136 auto StrIndex = InfoData.getULEB128(&InfoOffset);
David Blaikie4dd03f02016-03-26 20:32:14 +0000137 DataExtractor StrOffsetsData(StrOffsets, true, 0);
138 uint32_t StrOffsetsOffset = 4 * StrIndex;
139 uint32_t StrOffset = StrOffsetsData.getU32(&StrOffsetsOffset);
140 DataExtractor StrData(Str, true, 0);
141 return StrData.getCStr(&StrOffset);
142}
143
David Blaikiece7c6cf2016-03-24 22:17:08 +0000144static CompileUnitIdentifiers getCUIdentifiers(StringRef Abbrev, StringRef Info,
145 StringRef StrOffsets,
146 StringRef Str) {
David Blaikief1958da2016-02-26 07:30:15 +0000147 uint32_t Offset = 0;
148 DataExtractor InfoData(Info, true, 0);
149 InfoData.getU32(&Offset); // Length
150 uint16_t Version = InfoData.getU16(&Offset);
151 InfoData.getU32(&Offset); // Abbrev offset (should be zero)
152 uint8_t AddrSize = InfoData.getU8(&Offset);
153
154 uint32_t AbbrCode = InfoData.getULEB128(&Offset);
155
156 DataExtractor AbbrevData(Abbrev, true, 0);
157 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
158 uint64_t Tag = AbbrevData.getULEB128(&AbbrevOffset);
159 (void)Tag;
160 // FIXME: Real error handling
161 assert(Tag == dwarf::DW_TAG_compile_unit);
162 // DW_CHILDREN
163 AbbrevData.getU8(&AbbrevOffset);
164 uint32_t Name;
165 uint32_t Form;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000166 CompileUnitIdentifiers ID;
David Blaikief1958da2016-02-26 07:30:15 +0000167 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |
168 (Form = AbbrevData.getULEB128(&AbbrevOffset)) &&
David Blaikiece7c6cf2016-03-24 22:17:08 +0000169 (Name != 0 || Form != 0)) {
170 switch (Name) {
171 case dwarf::DW_AT_name: {
David Blaikie60fbd3b2016-04-05 20:16:38 +0000172 ID.Name = getIndexedString(Form, InfoData, Offset, StrOffsets, Str);
David Blaikie4dd03f02016-03-26 20:32:14 +0000173 break;
174 }
175 case dwarf::DW_AT_GNU_dwo_name: {
David Blaikie60fbd3b2016-04-05 20:16:38 +0000176 ID.DWOName = getIndexedString(Form, InfoData, Offset, StrOffsets, Str);
David Blaikiece7c6cf2016-03-24 22:17:08 +0000177 break;
178 }
179 case dwarf::DW_AT_GNU_dwo_id:
180 ID.Signature = InfoData.getU64(&Offset);
181 break;
182 default:
183 DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize);
184 }
David Blaikief1958da2016-02-26 07:30:15 +0000185 }
David Blaikiece7c6cf2016-03-24 22:17:08 +0000186 return ID;
David Blaikiead07b5d2015-12-04 17:20:04 +0000187}
188
David Blaikie24c8ac92015-12-05 03:05:45 +0000189struct UnitIndexEntry {
David Blaikie24c8ac92015-12-05 03:05:45 +0000190 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
David Blaikief1958da2016-02-26 07:30:15 +0000191 std::string Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000192 std::string DWOName;
David Blaikief1958da2016-02-26 07:30:15 +0000193 StringRef DWPName;
David Blaikie24c8ac92015-12-05 03:05:45 +0000194};
195
David Blaikief1958da2016-02-26 07:30:15 +0000196StringRef getSubsection(StringRef Section, const DWARFUnitIndex::Entry &Entry, DWARFSectionKind Kind) {
197 const auto *Off = Entry.getOffset(Kind);
198 if (!Off)
199 return StringRef();
200 return Section.substr(Off->Offset, Off->Length);
201}
202
David Blaikie852c02b2016-02-19 21:09:26 +0000203static void addAllTypesFromDWP(
204 MCStreamer &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
205 const DWARFUnitIndex &TUIndex, MCSection *OutputTypes, StringRef Types,
206 const UnitIndexEntry &TUEntry, uint32_t &TypesOffset) {
David Blaikie8bce5a02016-02-17 07:00:24 +0000207 Out.SwitchSection(OutputTypes);
208 for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) {
209 auto *I = E.getOffsets();
210 if (!I)
211 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000212 auto P = TypeIndexEntries.insert(std::make_pair(E.getSignature(), TUEntry));
213 if (!P.second)
David Blaikie8bce5a02016-02-17 07:00:24 +0000214 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000215 auto &Entry = P.first->second;
David Blaikie8bce5a02016-02-17 07:00:24 +0000216 // Zero out the debug_info contribution
217 Entry.Contributions[0] = {};
218 for (auto Kind : TUIndex.getColumnKinds()) {
219 auto &C = Entry.Contributions[Kind - DW_SECT_INFO];
220 C.Offset += I->Offset;
221 C.Length = I->Length;
222 ++I;
223 }
224 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
225 Out.EmitBytes(Types.substr(
226 C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset,
227 C.Length));
228 C.Offset = TypesOffset;
229 TypesOffset += C.Length;
David Blaikie8bce5a02016-02-17 07:00:24 +0000230 }
231}
232
David Blaikiec3826da2015-12-09 21:02:33 +0000233static void addAllTypes(MCStreamer &Out,
David Blaikie852c02b2016-02-19 21:09:26 +0000234 MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
David Blaikie62be5ae2016-04-05 20:26:50 +0000235 MCSection *OutputTypes,
236 const std::vector<StringRef> &TypesSections,
David Blaikiec3826da2015-12-09 21:02:33 +0000237 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) {
David Blaikie62be5ae2016-04-05 20:26:50 +0000238 for (StringRef Types : TypesSections) {
239 Out.SwitchSection(OutputTypes);
240 uint32_t Offset = 0;
241 DataExtractor Data(Types, true, 0);
242 while (Data.isValidOffset(Offset)) {
243 UnitIndexEntry Entry = CUEntry;
244 // Zero out the debug_info contribution
245 Entry.Contributions[0] = {};
246 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
247 C.Offset = TypesOffset;
248 auto PrevOffset = Offset;
249 // Length of the unit, including the 4 byte length field.
250 C.Length = Data.getU32(&Offset) + 4;
David Blaikiec3826da2015-12-09 21:02:33 +0000251
David Blaikie62be5ae2016-04-05 20:26:50 +0000252 Data.getU16(&Offset); // Version
253 Data.getU32(&Offset); // Abbrev offset
254 Data.getU8(&Offset); // Address size
255 auto Signature = Data.getU64(&Offset);
256 Offset = PrevOffset + C.Length;
David Blaikie24c8ac92015-12-05 03:05:45 +0000257
David Blaikie62be5ae2016-04-05 20:26:50 +0000258 auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry));
259 if (!P.second)
260 continue;
David Blaikief5cb6272015-12-14 07:42:00 +0000261
David Blaikie62be5ae2016-04-05 20:26:50 +0000262 Out.EmitBytes(Types.substr(PrevOffset, C.Length));
263 TypesOffset += C.Length;
264 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000265 }
266}
267
268static void
269writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,
David Blaikie852c02b2016-02-19 21:09:26 +0000270 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries,
David Blaikie24c8ac92015-12-05 03:05:45 +0000271 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) {
272 for (const auto &E : IndexEntries)
David Blaikie852c02b2016-02-19 21:09:26 +0000273 for (size_t i = 0; i != array_lengthof(E.second.Contributions); ++i)
David Blaikie24c8ac92015-12-05 03:05:45 +0000274 if (ContributionOffsets[i])
David Blaikie852c02b2016-02-19 21:09:26 +0000275 Out.EmitIntValue(E.second.Contributions[i].*Field, 4);
David Blaikie24c8ac92015-12-05 03:05:45 +0000276}
277
David Blaikie852c02b2016-02-19 21:09:26 +0000278static void
279writeIndex(MCStreamer &Out, MCSection *Section,
280 ArrayRef<unsigned> ContributionOffsets,
281 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries) {
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000282 if (IndexEntries.empty())
283 return;
284
David Blaikie24c8ac92015-12-05 03:05:45 +0000285 unsigned Columns = 0;
286 for (auto &C : ContributionOffsets)
287 if (C)
288 ++Columns;
289
290 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
291 uint64_t Mask = Buckets.size() - 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000292 size_t i = 0;
293 for (const auto &P : IndexEntries) {
294 auto S = P.first;
David Blaikie24c8ac92015-12-05 03:05:45 +0000295 auto H = S & Mask;
David Blaikie9b492562016-04-05 17:51:40 +0000296 auto HP = ((S >> 32) & Mask) | 1;
David Blaikiec3826da2015-12-09 21:02:33 +0000297 while (Buckets[H]) {
David Blaikie852c02b2016-02-19 21:09:26 +0000298 assert(S != IndexEntries.begin()[Buckets[H] - 1].first &&
David Blaikie74f5b282016-02-19 01:51:44 +0000299 "Duplicate unit");
David Blaikie9b492562016-04-05 17:51:40 +0000300 H = (H + HP) & Mask;
David Blaikiec3826da2015-12-09 21:02:33 +0000301 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000302 Buckets[H] = i + 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000303 ++i;
David Blaikie24c8ac92015-12-05 03:05:45 +0000304 }
305
306 Out.SwitchSection(Section);
307 Out.EmitIntValue(2, 4); // Version
308 Out.EmitIntValue(Columns, 4); // Columns
309 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
David Blaikie2ed678c2015-12-05 03:06:30 +0000310 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets
David Blaikie24c8ac92015-12-05 03:05:45 +0000311
312 // Write the signatures.
313 for (const auto &I : Buckets)
David Blaikie852c02b2016-02-19 21:09:26 +0000314 Out.EmitIntValue(I ? IndexEntries.begin()[I - 1].first : 0, 8);
David Blaikie24c8ac92015-12-05 03:05:45 +0000315
316 // Write the indexes.
317 for (const auto &I : Buckets)
318 Out.EmitIntValue(I, 4);
319
320 // Write the column headers (which sections will appear in the table)
321 for (size_t i = 0; i != ContributionOffsets.size(); ++i)
322 if (ContributionOffsets[i])
323 Out.EmitIntValue(i + DW_SECT_INFO, 4);
324
325 // Write the offsets.
326 writeIndexTable(Out, ContributionOffsets, IndexEntries,
327 &DWARFUnitIndex::Entry::SectionContribution::Offset);
328
329 // Write the lengths.
330 writeIndexTable(Out, ContributionOffsets, IndexEntries,
331 &DWARFUnitIndex::Entry::SectionContribution::Length);
332}
David Blaikie74f5b282016-02-19 01:51:44 +0000333static bool consumeCompressedDebugSectionHeader(StringRef &data,
334 uint64_t &OriginalSize) {
335 // Consume "ZLIB" prefix.
336 if (!data.startswith("ZLIB"))
337 return false;
338 data = data.substr(4);
339 // Consume uncompressed section size (big-endian 8 bytes).
340 DataExtractor extractor(data, false, 8);
341 uint32_t Offset = 0;
342 OriginalSize = extractor.getU64(&Offset);
343 if (Offset == 0)
344 return false;
345 data = data.substr(Offset);
346 return true;
347}
348
David Blaikie4dd03f02016-03-26 20:32:14 +0000349void printDuplicateError(const std::pair<uint64_t, UnitIndexEntry> &PrevE,
350 const CompileUnitIdentifiers &ID, StringRef DWPName) {
351 errs() << "Duplicate DWO ID (" << PrevE.first << ") in '" << PrevE.second.Name
352 << '\'';
353 if (!PrevE.second.DWPName.empty()) {
354 errs() << " (from ";
355 if (!PrevE.second.DWOName.empty())
356 errs() << '\'' << PrevE.second.DWOName << "' in ";
357 errs() << "'" << PrevE.second.DWPName.str() << "')";
358 }
359 errs() << " and '" << ID.Name << '\'';
360 if (!DWPName.empty()) {
361 errs() << " (from ";
362 if (*ID.DWOName)
363 errs() << '\'' << ID.DWOName << "\' in ";
364 errs() << '\'' << DWPName << "')";
365 }
366 errs() << '\n';
367}
David Blaikiebc8397c2016-05-12 19:59:54 +0000368static Error write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000369 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
370 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
371 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikiec3826da2015-12-09 21:02:33 +0000372 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
David Blaikie23919372016-02-06 01:15:26 +0000373 MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection();
David Blaikie8bce5a02016-02-17 07:00:24 +0000374 MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000375 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
376 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
377 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
378 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
379 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
380 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
David Blaikieb7020252015-12-04 21:16:42 +0000381 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikie23919372016-02-06 01:15:26 +0000382 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
David Blaikie8bce5a02016-02-17 07:00:24 +0000383 {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}},
384 {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}};
David Blaikieb073cb92015-12-02 06:21:34 +0000385
David Blaikie852c02b2016-02-19 21:09:26 +0000386 MapVector<uint64_t, UnitIndexEntry> IndexEntries;
387 MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000388
389 StringMap<uint32_t> Strings;
390 uint32_t StringOffset = 0;
391
David Blaikieb073cb92015-12-02 06:21:34 +0000392 uint32_t ContributionOffsets[8] = {};
393
David Blaikie242b9482015-12-01 00:48:39 +0000394 for (const auto &Input : Inputs) {
395 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
396 if (!ErrOrObj)
David Blaikiebc8397c2016-05-12 19:59:54 +0000397 return ErrOrObj.takeError();
David Blaikieb073cb92015-12-02 06:21:34 +0000398
David Blaikie23919372016-02-06 01:15:26 +0000399 UnitIndexEntry CurEntry = {};
David Blaikieb073cb92015-12-02 06:21:34 +0000400
David Blaikie98ad82a2015-12-01 18:07:07 +0000401 StringRef CurStrSection;
402 StringRef CurStrOffsetSection;
David Blaikie62be5ae2016-04-05 20:26:50 +0000403 std::vector<StringRef> CurTypesSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000404 StringRef InfoSection;
405 StringRef AbbrevSection;
David Blaikie23919372016-02-06 01:15:26 +0000406 StringRef CurCUIndexSection;
David Blaikie8bce5a02016-02-17 07:00:24 +0000407 StringRef CurTUIndexSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000408
David Blaikie74f5b282016-02-19 01:51:44 +0000409 SmallVector<SmallString<32>, 4> UncompressedSections;
410
David Blaikieddb27362016-03-01 21:24:04 +0000411 for (const auto &Section : ErrOrObj->getBinary()->sections()) {
David Blaikie74f5b282016-02-19 01:51:44 +0000412 if (Section.isBSS())
413 continue;
414 if (Section.isVirtual())
415 continue;
416
David Blaikie242b9482015-12-01 00:48:39 +0000417 StringRef Name;
418 if (std::error_code Err = Section.getName(Name))
David Blaikiebc8397c2016-05-12 19:59:54 +0000419 return errorCodeToError(Err);
David Blaikieb073cb92015-12-02 06:21:34 +0000420
David Blaikie74f5b282016-02-19 01:51:44 +0000421 Name = Name.substr(Name.find_first_not_of("._"));
David Blaikieb073cb92015-12-02 06:21:34 +0000422
423 StringRef Contents;
424 if (auto Err = Section.getContents(Contents))
David Blaikiebc8397c2016-05-12 19:59:54 +0000425 return errorCodeToError(Err);
David Blaikieb073cb92015-12-02 06:21:34 +0000426
David Blaikie74f5b282016-02-19 01:51:44 +0000427 if (Name.startswith("zdebug_")) {
428 uint64_t OriginalSize;
David Blaikiebc8397c2016-05-12 19:59:54 +0000429 if (!zlib::isAvailable())
430 return make_error<DWPError>("zlib not available");
431 if (!consumeCompressedDebugSectionHeader(Contents, OriginalSize))
432 return make_error<DWPError>(
433 ("failure while decompressing compressed section: '" + Name +
434 "\'").str());
David Blaikie74f5b282016-02-19 01:51:44 +0000435 UncompressedSections.resize(UncompressedSections.size() + 1);
436 if (zlib::uncompress(Contents, UncompressedSections.back(), OriginalSize) !=
437 zlib::StatusOK) {
438 UncompressedSections.pop_back();
439 continue;
440 }
441 Name = Name.substr(1);
442 Contents = UncompressedSections.back();
443 }
444
445 auto SectionPair = KnownSections.find(Name);
446 if (SectionPair == KnownSections.end())
447 continue;
448
David Blaikieb073cb92015-12-02 06:21:34 +0000449 if (DWARFSectionKind Kind = SectionPair->second.second) {
450 auto Index = Kind - DW_SECT_INFO;
David Blaikiec3826da2015-12-09 21:02:33 +0000451 if (Kind != DW_SECT_TYPES) {
452 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
453 ContributionOffsets[Index] +=
454 (CurEntry.Contributions[Index].Length = Contents.size());
455 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000456
David Blaikie24c8ac92015-12-05 03:05:45 +0000457 switch (Kind) {
458 case DW_SECT_INFO:
David Blaikiead07b5d2015-12-04 17:20:04 +0000459 InfoSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000460 break;
461 case DW_SECT_ABBREV:
David Blaikiead07b5d2015-12-04 17:20:04 +0000462 AbbrevSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000463 break;
David Blaikie24c8ac92015-12-05 03:05:45 +0000464 default:
465 break;
David Blaikiead07b5d2015-12-04 17:20:04 +0000466 }
David Blaikieb073cb92015-12-02 06:21:34 +0000467 }
468
469 MCSection *OutSection = SectionPair->second.first;
470 if (OutSection == StrOffsetSection)
471 CurStrOffsetSection = Contents;
472 else if (OutSection == StrSection)
473 CurStrSection = Contents;
David Blaikiec3826da2015-12-09 21:02:33 +0000474 else if (OutSection == TypesSection)
David Blaikie62be5ae2016-04-05 20:26:50 +0000475 CurTypesSection.push_back(Contents);
David Blaikie23919372016-02-06 01:15:26 +0000476 else if (OutSection == CUIndexSection)
477 CurCUIndexSection = Contents;
David Blaikie8bce5a02016-02-17 07:00:24 +0000478 else if (OutSection == TUIndexSection)
479 CurTUIndexSection = Contents;
David Blaikieb073cb92015-12-02 06:21:34 +0000480 else {
481 Out.SwitchSection(OutSection);
482 Out.EmitBytes(Contents);
David Blaikie98ad82a2015-12-01 18:07:07 +0000483 }
David Blaikie242b9482015-12-01 00:48:39 +0000484 }
David Blaikieb073cb92015-12-02 06:21:34 +0000485
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000486 if (InfoSection.empty())
487 continue;
488
David Blaikie23919372016-02-06 01:15:26 +0000489 if (!CurCUIndexSection.empty()) {
490 DWARFUnitIndex CUIndex(DW_SECT_INFO);
David Blaikieddb27362016-03-01 21:24:04 +0000491 DataExtractor CUIndexData(CurCUIndexSection,
492 ErrOrObj->getBinary()->isLittleEndian(), 0);
David Blaikie23919372016-02-06 01:15:26 +0000493 if (!CUIndex.parse(CUIndexData))
David Blaikiebc8397c2016-05-12 19:59:54 +0000494 return make_error<DWPError>("Failed to parse cu_index");
David Blaikie23919372016-02-06 01:15:26 +0000495
496 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
David Blaikie23919372016-02-06 01:15:26 +0000497 auto *I = E.getOffsets();
498 if (!I)
499 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000500 auto P =
501 IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry));
David Blaikiece7c6cf2016-03-24 22:17:08 +0000502 CompileUnitIdentifiers ID = getCUIdentifiers(
David Blaikief1958da2016-02-26 07:30:15 +0000503 getSubsection(AbbrevSection, E, DW_SECT_ABBREV),
504 getSubsection(InfoSection, E, DW_SECT_INFO),
505 getSubsection(CurStrOffsetSection, E, DW_SECT_STR_OFFSETS),
506 CurStrSection);
507 if (!P.second) {
David Blaikie4dd03f02016-03-26 20:32:14 +0000508 printDuplicateError(*P.first, ID, Input);
David Blaikiebc8397c2016-05-12 19:59:54 +0000509 return make_error<DWPError>("Duplicate DWO ID");
David Blaikief1958da2016-02-26 07:30:15 +0000510 }
David Blaikie852c02b2016-02-19 21:09:26 +0000511 auto &NewEntry = P.first->second;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000512 NewEntry.Name = ID.Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000513 NewEntry.DWOName = ID.DWOName;
David Blaikief1958da2016-02-26 07:30:15 +0000514 NewEntry.DWPName = Input;
David Blaikie23919372016-02-06 01:15:26 +0000515 for (auto Kind : CUIndex.getColumnKinds()) {
516 auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO];
517 C.Offset += I->Offset;
518 C.Length = I->Length;
519 ++I;
520 }
David Blaikie23919372016-02-06 01:15:26 +0000521 }
David Blaikie8bce5a02016-02-17 07:00:24 +0000522
523 if (!CurTypesSection.empty()) {
David Blaikie62be5ae2016-04-05 20:26:50 +0000524 assert(CurTypesSection.size() == 1);
David Blaikie8bce5a02016-02-17 07:00:24 +0000525 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))
David Blaikiebc8397c2016-05-12 19:59:54 +0000529 return make_error<DWPError>("Failed to parse tu_index");
David Blaikie8bce5a02016-02-17 07:00:24 +0000530 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 Blaikiebc8397c2016-05-12 19:59:54 +0000540 return make_error<DWPError>("Duplicate DWO ID");
David Blaikief1958da2016-02-26 07:30:15 +0000541 }
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 Blaikiebc8397c2016-05-12 19:59:54 +0000568 return Error();
David Blaikie242b9482015-12-01 00:48:39 +0000569}
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
David Blaikiebc8397c2016-05-12 19:59:54 +0000636 if (auto Err = write(*MS, InputFiles)) {
637 logAllUnhandledErrors(std::move(Err), errs(), "error: ");
638 return 1;
639 }
David Blaikieddb27362016-03-01 21:24:04 +0000640
641 MS->Finish();
David Blaikiedf055252015-12-01 00:48:34 +0000642}