blob: 60d07207ed415a9edaf1acfbc629eab09d29d0d0 [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 Blaikie4940f872016-05-17 00:07:10 +0000128static Expected<const char *>
129getIndexedString(uint32_t Form, DataExtractor InfoData, uint32_t &InfoOffset,
130 StringRef StrOffsets, StringRef Str) {
David Blaikie60fbd3b2016-04-05 20:16:38 +0000131 if (Form == dwarf::DW_FORM_string)
132 return InfoData.getCStr(&InfoOffset);
David Blaikie4940f872016-05-17 00:07:10 +0000133 if (Form != dwarf::DW_FORM_GNU_str_index)
134 return make_error<DWPError>(
135 "string field encoded without DW_FORM_string or DW_FORM_GNU_str_index");
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 Blaikie7bb62ef2016-05-16 23:26:29 +0000144static Expected<CompileUnitIdentifiers> getCUIdentifiers(StringRef Abbrev,
145 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);
David Blaikie7bb62ef2016-05-16 23:26:29 +0000160 if (Tag != dwarf::DW_TAG_compile_unit)
161 return make_error<DWPError>("top level DIE is not a compile unit");
David Blaikief1958da2016-02-26 07:30:15 +0000162 // 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 Blaikie4940f872016-05-17 00:07:10 +0000172 Expected<const char *> EName =
173 getIndexedString(Form, InfoData, Offset, StrOffsets, Str);
174 if (!EName)
175 return EName.takeError();
176 ID.Name = *EName;
David Blaikie4dd03f02016-03-26 20:32:14 +0000177 break;
178 }
179 case dwarf::DW_AT_GNU_dwo_name: {
David Blaikie4940f872016-05-17 00:07:10 +0000180 Expected<const char *> EName =
181 getIndexedString(Form, InfoData, Offset, StrOffsets, Str);
182 if (!EName)
183 return EName.takeError();
184 ID.DWOName = *EName;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000185 break;
186 }
187 case dwarf::DW_AT_GNU_dwo_id:
188 ID.Signature = InfoData.getU64(&Offset);
189 break;
190 default:
191 DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize);
192 }
David Blaikief1958da2016-02-26 07:30:15 +0000193 }
David Blaikiece7c6cf2016-03-24 22:17:08 +0000194 return ID;
David Blaikiead07b5d2015-12-04 17:20:04 +0000195}
196
David Blaikie24c8ac92015-12-05 03:05:45 +0000197struct UnitIndexEntry {
David Blaikie24c8ac92015-12-05 03:05:45 +0000198 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
David Blaikief1958da2016-02-26 07:30:15 +0000199 std::string Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000200 std::string DWOName;
David Blaikief1958da2016-02-26 07:30:15 +0000201 StringRef DWPName;
David Blaikie24c8ac92015-12-05 03:05:45 +0000202};
203
David Blaikief1958da2016-02-26 07:30:15 +0000204StringRef getSubsection(StringRef Section, const DWARFUnitIndex::Entry &Entry, DWARFSectionKind Kind) {
205 const auto *Off = Entry.getOffset(Kind);
206 if (!Off)
207 return StringRef();
208 return Section.substr(Off->Offset, Off->Length);
209}
210
David Blaikie852c02b2016-02-19 21:09:26 +0000211static void addAllTypesFromDWP(
212 MCStreamer &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
213 const DWARFUnitIndex &TUIndex, MCSection *OutputTypes, StringRef Types,
214 const UnitIndexEntry &TUEntry, uint32_t &TypesOffset) {
David Blaikie8bce5a02016-02-17 07:00:24 +0000215 Out.SwitchSection(OutputTypes);
216 for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) {
217 auto *I = E.getOffsets();
218 if (!I)
219 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000220 auto P = TypeIndexEntries.insert(std::make_pair(E.getSignature(), TUEntry));
221 if (!P.second)
David Blaikie8bce5a02016-02-17 07:00:24 +0000222 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000223 auto &Entry = P.first->second;
David Blaikie8bce5a02016-02-17 07:00:24 +0000224 // Zero out the debug_info contribution
225 Entry.Contributions[0] = {};
226 for (auto Kind : TUIndex.getColumnKinds()) {
227 auto &C = Entry.Contributions[Kind - DW_SECT_INFO];
228 C.Offset += I->Offset;
229 C.Length = I->Length;
230 ++I;
231 }
232 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
233 Out.EmitBytes(Types.substr(
234 C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset,
235 C.Length));
236 C.Offset = TypesOffset;
237 TypesOffset += C.Length;
David Blaikie8bce5a02016-02-17 07:00:24 +0000238 }
239}
240
David Blaikiec3826da2015-12-09 21:02:33 +0000241static void addAllTypes(MCStreamer &Out,
David Blaikie852c02b2016-02-19 21:09:26 +0000242 MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
David Blaikie62be5ae2016-04-05 20:26:50 +0000243 MCSection *OutputTypes,
244 const std::vector<StringRef> &TypesSections,
David Blaikiec3826da2015-12-09 21:02:33 +0000245 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) {
David Blaikie62be5ae2016-04-05 20:26:50 +0000246 for (StringRef Types : TypesSections) {
247 Out.SwitchSection(OutputTypes);
248 uint32_t Offset = 0;
249 DataExtractor Data(Types, true, 0);
250 while (Data.isValidOffset(Offset)) {
251 UnitIndexEntry Entry = CUEntry;
252 // Zero out the debug_info contribution
253 Entry.Contributions[0] = {};
254 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
255 C.Offset = TypesOffset;
256 auto PrevOffset = Offset;
257 // Length of the unit, including the 4 byte length field.
258 C.Length = Data.getU32(&Offset) + 4;
David Blaikiec3826da2015-12-09 21:02:33 +0000259
David Blaikie62be5ae2016-04-05 20:26:50 +0000260 Data.getU16(&Offset); // Version
261 Data.getU32(&Offset); // Abbrev offset
262 Data.getU8(&Offset); // Address size
263 auto Signature = Data.getU64(&Offset);
264 Offset = PrevOffset + C.Length;
David Blaikie24c8ac92015-12-05 03:05:45 +0000265
David Blaikie62be5ae2016-04-05 20:26:50 +0000266 auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry));
267 if (!P.second)
268 continue;
David Blaikief5cb6272015-12-14 07:42:00 +0000269
David Blaikie62be5ae2016-04-05 20:26:50 +0000270 Out.EmitBytes(Types.substr(PrevOffset, C.Length));
271 TypesOffset += C.Length;
272 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000273 }
274}
275
276static void
277writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,
David Blaikie852c02b2016-02-19 21:09:26 +0000278 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries,
David Blaikie24c8ac92015-12-05 03:05:45 +0000279 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) {
280 for (const auto &E : IndexEntries)
David Blaikie852c02b2016-02-19 21:09:26 +0000281 for (size_t i = 0; i != array_lengthof(E.second.Contributions); ++i)
David Blaikie24c8ac92015-12-05 03:05:45 +0000282 if (ContributionOffsets[i])
David Blaikie852c02b2016-02-19 21:09:26 +0000283 Out.EmitIntValue(E.second.Contributions[i].*Field, 4);
David Blaikie24c8ac92015-12-05 03:05:45 +0000284}
285
David Blaikie852c02b2016-02-19 21:09:26 +0000286static void
287writeIndex(MCStreamer &Out, MCSection *Section,
288 ArrayRef<unsigned> ContributionOffsets,
289 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries) {
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000290 if (IndexEntries.empty())
291 return;
292
David Blaikie24c8ac92015-12-05 03:05:45 +0000293 unsigned Columns = 0;
294 for (auto &C : ContributionOffsets)
295 if (C)
296 ++Columns;
297
298 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
299 uint64_t Mask = Buckets.size() - 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000300 size_t i = 0;
301 for (const auto &P : IndexEntries) {
302 auto S = P.first;
David Blaikie24c8ac92015-12-05 03:05:45 +0000303 auto H = S & Mask;
David Blaikie9b492562016-04-05 17:51:40 +0000304 auto HP = ((S >> 32) & Mask) | 1;
David Blaikiec3826da2015-12-09 21:02:33 +0000305 while (Buckets[H]) {
David Blaikie852c02b2016-02-19 21:09:26 +0000306 assert(S != IndexEntries.begin()[Buckets[H] - 1].first &&
David Blaikie74f5b282016-02-19 01:51:44 +0000307 "Duplicate unit");
David Blaikie9b492562016-04-05 17:51:40 +0000308 H = (H + HP) & Mask;
David Blaikiec3826da2015-12-09 21:02:33 +0000309 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000310 Buckets[H] = i + 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000311 ++i;
David Blaikie24c8ac92015-12-05 03:05:45 +0000312 }
313
314 Out.SwitchSection(Section);
315 Out.EmitIntValue(2, 4); // Version
316 Out.EmitIntValue(Columns, 4); // Columns
317 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
David Blaikie2ed678c2015-12-05 03:06:30 +0000318 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets
David Blaikie24c8ac92015-12-05 03:05:45 +0000319
320 // Write the signatures.
321 for (const auto &I : Buckets)
David Blaikie852c02b2016-02-19 21:09:26 +0000322 Out.EmitIntValue(I ? IndexEntries.begin()[I - 1].first : 0, 8);
David Blaikie24c8ac92015-12-05 03:05:45 +0000323
324 // Write the indexes.
325 for (const auto &I : Buckets)
326 Out.EmitIntValue(I, 4);
327
328 // Write the column headers (which sections will appear in the table)
329 for (size_t i = 0; i != ContributionOffsets.size(); ++i)
330 if (ContributionOffsets[i])
331 Out.EmitIntValue(i + DW_SECT_INFO, 4);
332
333 // Write the offsets.
334 writeIndexTable(Out, ContributionOffsets, IndexEntries,
335 &DWARFUnitIndex::Entry::SectionContribution::Offset);
336
337 // Write the lengths.
338 writeIndexTable(Out, ContributionOffsets, IndexEntries,
339 &DWARFUnitIndex::Entry::SectionContribution::Length);
340}
David Blaikie74f5b282016-02-19 01:51:44 +0000341static bool consumeCompressedDebugSectionHeader(StringRef &data,
342 uint64_t &OriginalSize) {
343 // Consume "ZLIB" prefix.
344 if (!data.startswith("ZLIB"))
345 return false;
346 data = data.substr(4);
347 // Consume uncompressed section size (big-endian 8 bytes).
348 DataExtractor extractor(data, false, 8);
349 uint32_t Offset = 0;
350 OriginalSize = extractor.getU64(&Offset);
351 if (Offset == 0)
352 return false;
353 data = data.substr(Offset);
354 return true;
355}
356
David Blaikied1f7ab32016-05-16 20:42:27 +0000357std::string buildDWODescription(StringRef Name, StringRef DWPName, StringRef DWOName) {
358 std::string Text = "\'";
359 Text += Name;
360 Text += '\'';
David Blaikie4dd03f02016-03-26 20:32:14 +0000361 if (!DWPName.empty()) {
David Blaikied1f7ab32016-05-16 20:42:27 +0000362 Text += " (from ";
363 if (!DWOName.empty()) {
364 Text += '\'';
365 Text += DWOName;
366 Text += "' in ";
367 }
368 Text += '\'';
369 Text += DWPName;
370 Text += "')";
David Blaikie4dd03f02016-03-26 20:32:14 +0000371 }
David Blaikied1f7ab32016-05-16 20:42:27 +0000372 return Text;
373}
374
David Blaikie11825c72016-05-17 19:40:28 +0000375Error buildDuplicateError(const std::pair<uint64_t, UnitIndexEntry> &PrevE,
376 const CompileUnitIdentifiers &ID, StringRef DWPName) {
377 return make_error<DWPError>(
378 std::string("Duplicate DWO ID (") + utohexstr(PrevE.first) + ") in " +
379 buildDWODescription(PrevE.second.Name, PrevE.second.DWPName,
380 PrevE.second.DWOName) +
381 " and " + buildDWODescription(ID.Name, DWPName, ID.DWOName));
David Blaikie4dd03f02016-03-26 20:32:14 +0000382}
David Blaikiebc8397c2016-05-12 19:59:54 +0000383static Error write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000384 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
385 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
386 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikiec3826da2015-12-09 21:02:33 +0000387 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
David Blaikie23919372016-02-06 01:15:26 +0000388 MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection();
David Blaikie8bce5a02016-02-17 07:00:24 +0000389 MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000390 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
391 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
392 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
393 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
394 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
395 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
David Blaikieb7020252015-12-04 21:16:42 +0000396 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikie23919372016-02-06 01:15:26 +0000397 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
David Blaikie8bce5a02016-02-17 07:00:24 +0000398 {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}},
399 {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}};
David Blaikieb073cb92015-12-02 06:21:34 +0000400
David Blaikie852c02b2016-02-19 21:09:26 +0000401 MapVector<uint64_t, UnitIndexEntry> IndexEntries;
402 MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000403
404 StringMap<uint32_t> Strings;
405 uint32_t StringOffset = 0;
406
David Blaikieb073cb92015-12-02 06:21:34 +0000407 uint32_t ContributionOffsets[8] = {};
408
David Blaikie242b9482015-12-01 00:48:39 +0000409 for (const auto &Input : Inputs) {
410 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
411 if (!ErrOrObj)
David Blaikiebc8397c2016-05-12 19:59:54 +0000412 return ErrOrObj.takeError();
David Blaikieb073cb92015-12-02 06:21:34 +0000413
David Blaikie23919372016-02-06 01:15:26 +0000414 UnitIndexEntry CurEntry = {};
David Blaikieb073cb92015-12-02 06:21:34 +0000415
David Blaikie98ad82a2015-12-01 18:07:07 +0000416 StringRef CurStrSection;
417 StringRef CurStrOffsetSection;
David Blaikie62be5ae2016-04-05 20:26:50 +0000418 std::vector<StringRef> CurTypesSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000419 StringRef InfoSection;
420 StringRef AbbrevSection;
David Blaikie23919372016-02-06 01:15:26 +0000421 StringRef CurCUIndexSection;
David Blaikie8bce5a02016-02-17 07:00:24 +0000422 StringRef CurTUIndexSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000423
David Blaikie74f5b282016-02-19 01:51:44 +0000424 SmallVector<SmallString<32>, 4> UncompressedSections;
425
David Blaikieddb27362016-03-01 21:24:04 +0000426 for (const auto &Section : ErrOrObj->getBinary()->sections()) {
David Blaikie74f5b282016-02-19 01:51:44 +0000427 if (Section.isBSS())
428 continue;
429 if (Section.isVirtual())
430 continue;
431
David Blaikie242b9482015-12-01 00:48:39 +0000432 StringRef Name;
433 if (std::error_code Err = Section.getName(Name))
David Blaikiebc8397c2016-05-12 19:59:54 +0000434 return errorCodeToError(Err);
David Blaikieb073cb92015-12-02 06:21:34 +0000435
David Blaikie74f5b282016-02-19 01:51:44 +0000436 Name = Name.substr(Name.find_first_not_of("._"));
David Blaikieb073cb92015-12-02 06:21:34 +0000437
438 StringRef Contents;
439 if (auto Err = Section.getContents(Contents))
David Blaikiebc8397c2016-05-12 19:59:54 +0000440 return errorCodeToError(Err);
David Blaikieb073cb92015-12-02 06:21:34 +0000441
David Blaikie74f5b282016-02-19 01:51:44 +0000442 if (Name.startswith("zdebug_")) {
443 uint64_t OriginalSize;
David Blaikiebc8397c2016-05-12 19:59:54 +0000444 if (!zlib::isAvailable())
445 return make_error<DWPError>("zlib not available");
446 if (!consumeCompressedDebugSectionHeader(Contents, OriginalSize))
447 return make_error<DWPError>(
448 ("failure while decompressing compressed section: '" + Name +
449 "\'").str());
David Blaikie74f5b282016-02-19 01:51:44 +0000450 UncompressedSections.resize(UncompressedSections.size() + 1);
451 if (zlib::uncompress(Contents, UncompressedSections.back(), OriginalSize) !=
452 zlib::StatusOK) {
453 UncompressedSections.pop_back();
454 continue;
455 }
456 Name = Name.substr(1);
457 Contents = UncompressedSections.back();
458 }
459
460 auto SectionPair = KnownSections.find(Name);
461 if (SectionPair == KnownSections.end())
462 continue;
463
David Blaikieb073cb92015-12-02 06:21:34 +0000464 if (DWARFSectionKind Kind = SectionPair->second.second) {
465 auto Index = Kind - DW_SECT_INFO;
David Blaikiec3826da2015-12-09 21:02:33 +0000466 if (Kind != DW_SECT_TYPES) {
467 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
468 ContributionOffsets[Index] +=
469 (CurEntry.Contributions[Index].Length = Contents.size());
470 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000471
David Blaikie24c8ac92015-12-05 03:05:45 +0000472 switch (Kind) {
473 case DW_SECT_INFO:
David Blaikiead07b5d2015-12-04 17:20:04 +0000474 InfoSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000475 break;
476 case DW_SECT_ABBREV:
David Blaikiead07b5d2015-12-04 17:20:04 +0000477 AbbrevSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000478 break;
David Blaikie24c8ac92015-12-05 03:05:45 +0000479 default:
480 break;
David Blaikiead07b5d2015-12-04 17:20:04 +0000481 }
David Blaikieb073cb92015-12-02 06:21:34 +0000482 }
483
484 MCSection *OutSection = SectionPair->second.first;
485 if (OutSection == StrOffsetSection)
486 CurStrOffsetSection = Contents;
487 else if (OutSection == StrSection)
488 CurStrSection = Contents;
David Blaikiec3826da2015-12-09 21:02:33 +0000489 else if (OutSection == TypesSection)
David Blaikie62be5ae2016-04-05 20:26:50 +0000490 CurTypesSection.push_back(Contents);
David Blaikie23919372016-02-06 01:15:26 +0000491 else if (OutSection == CUIndexSection)
492 CurCUIndexSection = Contents;
David Blaikie8bce5a02016-02-17 07:00:24 +0000493 else if (OutSection == TUIndexSection)
494 CurTUIndexSection = Contents;
David Blaikieb073cb92015-12-02 06:21:34 +0000495 else {
496 Out.SwitchSection(OutSection);
497 Out.EmitBytes(Contents);
David Blaikie98ad82a2015-12-01 18:07:07 +0000498 }
David Blaikie242b9482015-12-01 00:48:39 +0000499 }
David Blaikieb073cb92015-12-02 06:21:34 +0000500
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000501 if (InfoSection.empty())
502 continue;
503
David Blaikie23919372016-02-06 01:15:26 +0000504 if (!CurCUIndexSection.empty()) {
505 DWARFUnitIndex CUIndex(DW_SECT_INFO);
David Blaikieddb27362016-03-01 21:24:04 +0000506 DataExtractor CUIndexData(CurCUIndexSection,
507 ErrOrObj->getBinary()->isLittleEndian(), 0);
David Blaikie23919372016-02-06 01:15:26 +0000508 if (!CUIndex.parse(CUIndexData))
David Blaikiebc8397c2016-05-12 19:59:54 +0000509 return make_error<DWPError>("Failed to parse cu_index");
David Blaikie23919372016-02-06 01:15:26 +0000510
511 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
David Blaikie23919372016-02-06 01:15:26 +0000512 auto *I = E.getOffsets();
513 if (!I)
514 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000515 auto P =
516 IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry));
David Blaikie7bb62ef2016-05-16 23:26:29 +0000517 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(
David Blaikief1958da2016-02-26 07:30:15 +0000518 getSubsection(AbbrevSection, E, DW_SECT_ABBREV),
519 getSubsection(InfoSection, E, DW_SECT_INFO),
520 getSubsection(CurStrOffsetSection, E, DW_SECT_STR_OFFSETS),
521 CurStrSection);
David Blaikie7bb62ef2016-05-16 23:26:29 +0000522 if (!EID)
523 return EID.takeError();
524 const auto &ID = *EID;
David Blaikied1f7ab32016-05-16 20:42:27 +0000525 if (!P.second)
David Blaikie11825c72016-05-17 19:40:28 +0000526 return buildDuplicateError(*P.first, ID, Input);
David Blaikie852c02b2016-02-19 21:09:26 +0000527 auto &NewEntry = P.first->second;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000528 NewEntry.Name = ID.Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000529 NewEntry.DWOName = ID.DWOName;
David Blaikief1958da2016-02-26 07:30:15 +0000530 NewEntry.DWPName = Input;
David Blaikie23919372016-02-06 01:15:26 +0000531 for (auto Kind : CUIndex.getColumnKinds()) {
532 auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO];
533 C.Offset += I->Offset;
534 C.Length = I->Length;
535 ++I;
536 }
David Blaikie23919372016-02-06 01:15:26 +0000537 }
David Blaikie8bce5a02016-02-17 07:00:24 +0000538
539 if (!CurTypesSection.empty()) {
David Blaikie62be5ae2016-04-05 20:26:50 +0000540 assert(CurTypesSection.size() == 1);
David Blaikie8bce5a02016-02-17 07:00:24 +0000541 DWARFUnitIndex TUIndex(DW_SECT_TYPES);
David Blaikieddb27362016-03-01 21:24:04 +0000542 DataExtractor TUIndexData(CurTUIndexSection,
543 ErrOrObj->getBinary()->isLittleEndian(), 0);
David Blaikie8bce5a02016-02-17 07:00:24 +0000544 if (!TUIndex.parse(TUIndexData))
David Blaikiebc8397c2016-05-12 19:59:54 +0000545 return make_error<DWPError>("Failed to parse tu_index");
David Blaikie8bce5a02016-02-17 07:00:24 +0000546 addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection,
David Blaikie62be5ae2016-04-05 20:26:50 +0000547 CurTypesSection.front(), CurEntry,
David Blaikie8bce5a02016-02-17 07:00:24 +0000548 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
549 }
David Blaikie23919372016-02-06 01:15:26 +0000550 } else {
David Blaikie7bb62ef2016-05-16 23:26:29 +0000551 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(
David Blaikiece7c6cf2016-03-24 22:17:08 +0000552 AbbrevSection, InfoSection, CurStrOffsetSection, CurStrSection);
David Blaikie7bb62ef2016-05-16 23:26:29 +0000553 if (!EID)
554 return EID.takeError();
555 const auto &ID = *EID;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000556 auto P = IndexEntries.insert(std::make_pair(ID.Signature, CurEntry));
David Blaikied1f7ab32016-05-16 20:42:27 +0000557 if (!P.second)
David Blaikie11825c72016-05-17 19:40:28 +0000558 return buildDuplicateError(*P.first, ID, "");
David Blaikiece7c6cf2016-03-24 22:17:08 +0000559 P.first->second.Name = ID.Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000560 P.first->second.DWOName = ID.DWOName;
David Blaikie23919372016-02-06 01:15:26 +0000561 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection,
562 CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
David Blaikie23919372016-02-06 01:15:26 +0000563 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000564
David Blaikiebb94e442015-12-01 19:17:58 +0000565 if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset,
566 StrSection, StrOffsetSection,
567 CurStrSection, CurStrOffsetSection))
David Blaikie98ad82a2015-12-01 18:07:07 +0000568 return Err;
David Blaikie242b9482015-12-01 00:48:39 +0000569 }
David Blaikieb073cb92015-12-02 06:21:34 +0000570
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000571 // Lie about there being no info contributions so the TU index only includes
572 // the type unit contribution
573 ContributionOffsets[0] = 0;
574 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
575 TypeIndexEntries);
David Blaikieb3757c02015-12-02 22:01:56 +0000576
David Blaikie24c8ac92015-12-05 03:05:45 +0000577 // Lie about the type contribution
578 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
579 // Unlie about the info contribution
580 ContributionOffsets[0] = 1;
David Blaikie7c4ffe02015-12-04 21:30:23 +0000581
David Blaikie24c8ac92015-12-05 03:05:45 +0000582 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
583 IndexEntries);
David Blaikieddb27362016-03-01 21:24:04 +0000584
David Blaikiebc8397c2016-05-12 19:59:54 +0000585 return Error();
David Blaikie242b9482015-12-01 00:48:39 +0000586}
587
David Blaikie2ed678c2015-12-05 03:06:30 +0000588int main(int argc, char **argv) {
David Blaikie242b9482015-12-01 00:48:39 +0000589
590 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
591
592 llvm::InitializeAllTargetInfos();
593 llvm::InitializeAllTargetMCs();
594 llvm::InitializeAllTargets();
595 llvm::InitializeAllAsmPrinters();
596
597 std::string ErrorStr;
598 StringRef Context = "dwarf streamer init";
599
600 Triple TheTriple("x86_64-linux-gnu");
601
602 // Get the target.
603 const Target *TheTarget =
604 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
605 if (!TheTarget)
606 return error(ErrorStr, Context);
607 std::string TripleName = TheTriple.getTriple();
608
609 // Create all the MC Objects.
610 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
611 if (!MRI)
612 return error(Twine("no register info for target ") + TripleName, Context);
613
614 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
615 if (!MAI)
616 return error("no asm info for target " + TripleName, Context);
617
618 MCObjectFileInfo MOFI;
619 MCContext MC(MAI.get(), MRI.get(), &MOFI);
David Blaikie2ed678c2015-12-05 03:06:30 +0000620 MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default, MC);
David Blaikie242b9482015-12-01 00:48:39 +0000621
622 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
623 if (!MAB)
624 return error("no asm backend for target " + TripleName, Context);
625
626 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
627 if (!MII)
628 return error("no instr info info for target " + TripleName, Context);
629
630 std::unique_ptr<MCSubtargetInfo> MSTI(
631 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
632 if (!MSTI)
633 return error("no subtarget info for target " + TripleName, Context);
634
635 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
636 if (!MCE)
637 return error("no code emitter for target " + TripleName, Context);
638
639 // Create the output file.
640 std::error_code EC;
641 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
642 if (EC)
643 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
644
David Majnemer03e2cc32015-12-21 22:09:27 +0000645 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
David Blaikie242b9482015-12-01 00:48:39 +0000646 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
David Majnemer03e2cc32015-12-21 22:09:27 +0000647 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, MCOptions.MCRelaxAll,
648 MCOptions.MCIncrementalLinkerCompatible,
David Blaikie242b9482015-12-01 00:48:39 +0000649 /*DWARFMustBeAtTheEnd*/ false));
650 if (!MS)
651 return error("no object streamer for target " + TripleName, Context);
652
David Blaikiebc8397c2016-05-12 19:59:54 +0000653 if (auto Err = write(*MS, InputFiles)) {
654 logAllUnhandledErrors(std::move(Err), errs(), "error: ");
655 return 1;
656 }
David Blaikieddb27362016-03-01 21:24:04 +0000657
658 MS->Finish();
David Blaikiedf055252015-12-01 00:48:34 +0000659}