blob: 5e83d7c9cacc73e44ca155cf46615aa8adfe23c7 [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 Blaikied1f7ab32016-05-16 20:42:27 +0000349std::string buildDWODescription(StringRef Name, StringRef DWPName, StringRef DWOName) {
350 std::string Text = "\'";
351 Text += Name;
352 Text += '\'';
David Blaikie4dd03f02016-03-26 20:32:14 +0000353 if (!DWPName.empty()) {
David Blaikied1f7ab32016-05-16 20:42:27 +0000354 Text += " (from ";
355 if (!DWOName.empty()) {
356 Text += '\'';
357 Text += DWOName;
358 Text += "' in ";
359 }
360 Text += '\'';
361 Text += DWPName;
362 Text += "')";
David Blaikie4dd03f02016-03-26 20:32:14 +0000363 }
David Blaikied1f7ab32016-05-16 20:42:27 +0000364 return Text;
365}
366
367std::string
368buildDuplicateError(const std::pair<uint64_t, UnitIndexEntry> &PrevE,
369 const CompileUnitIdentifiers &ID, StringRef DWPName) {
370 return std::string("Duplicate DWO ID (") + utohexstr(PrevE.first) + ") in " +
371 buildDWODescription(PrevE.second.Name, PrevE.second.DWPName,
372 PrevE.second.DWOName) +
373 " and " + buildDWODescription(ID.Name, DWPName, ID.DWOName);
David Blaikie4dd03f02016-03-26 20:32:14 +0000374}
David Blaikiebc8397c2016-05-12 19:59:54 +0000375static Error write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000376 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
377 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
378 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikiec3826da2015-12-09 21:02:33 +0000379 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
David Blaikie23919372016-02-06 01:15:26 +0000380 MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection();
David Blaikie8bce5a02016-02-17 07:00:24 +0000381 MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000382 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
383 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
384 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
385 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
386 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
387 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
David Blaikieb7020252015-12-04 21:16:42 +0000388 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikie23919372016-02-06 01:15:26 +0000389 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
David Blaikie8bce5a02016-02-17 07:00:24 +0000390 {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}},
391 {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}};
David Blaikieb073cb92015-12-02 06:21:34 +0000392
David Blaikie852c02b2016-02-19 21:09:26 +0000393 MapVector<uint64_t, UnitIndexEntry> IndexEntries;
394 MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000395
396 StringMap<uint32_t> Strings;
397 uint32_t StringOffset = 0;
398
David Blaikieb073cb92015-12-02 06:21:34 +0000399 uint32_t ContributionOffsets[8] = {};
400
David Blaikie242b9482015-12-01 00:48:39 +0000401 for (const auto &Input : Inputs) {
402 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
403 if (!ErrOrObj)
David Blaikiebc8397c2016-05-12 19:59:54 +0000404 return ErrOrObj.takeError();
David Blaikieb073cb92015-12-02 06:21:34 +0000405
David Blaikie23919372016-02-06 01:15:26 +0000406 UnitIndexEntry CurEntry = {};
David Blaikieb073cb92015-12-02 06:21:34 +0000407
David Blaikie98ad82a2015-12-01 18:07:07 +0000408 StringRef CurStrSection;
409 StringRef CurStrOffsetSection;
David Blaikie62be5ae2016-04-05 20:26:50 +0000410 std::vector<StringRef> CurTypesSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000411 StringRef InfoSection;
412 StringRef AbbrevSection;
David Blaikie23919372016-02-06 01:15:26 +0000413 StringRef CurCUIndexSection;
David Blaikie8bce5a02016-02-17 07:00:24 +0000414 StringRef CurTUIndexSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000415
David Blaikie74f5b282016-02-19 01:51:44 +0000416 SmallVector<SmallString<32>, 4> UncompressedSections;
417
David Blaikieddb27362016-03-01 21:24:04 +0000418 for (const auto &Section : ErrOrObj->getBinary()->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 +
441 "\'").str());
David Blaikie74f5b282016-02-19 01:51:44 +0000442 UncompressedSections.resize(UncompressedSections.size() + 1);
443 if (zlib::uncompress(Contents, UncompressedSections.back(), OriginalSize) !=
444 zlib::StatusOK) {
445 UncompressedSections.pop_back();
446 continue;
447 }
448 Name = Name.substr(1);
449 Contents = UncompressedSections.back();
450 }
451
452 auto SectionPair = KnownSections.find(Name);
453 if (SectionPair == KnownSections.end())
454 continue;
455
David Blaikieb073cb92015-12-02 06:21:34 +0000456 if (DWARFSectionKind Kind = SectionPair->second.second) {
457 auto Index = Kind - DW_SECT_INFO;
David Blaikiec3826da2015-12-09 21:02:33 +0000458 if (Kind != DW_SECT_TYPES) {
459 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
460 ContributionOffsets[Index] +=
461 (CurEntry.Contributions[Index].Length = Contents.size());
462 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000463
David Blaikie24c8ac92015-12-05 03:05:45 +0000464 switch (Kind) {
465 case DW_SECT_INFO:
David Blaikiead07b5d2015-12-04 17:20:04 +0000466 InfoSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000467 break;
468 case DW_SECT_ABBREV:
David Blaikiead07b5d2015-12-04 17:20:04 +0000469 AbbrevSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000470 break;
David Blaikie24c8ac92015-12-05 03:05:45 +0000471 default:
472 break;
David Blaikiead07b5d2015-12-04 17:20:04 +0000473 }
David Blaikieb073cb92015-12-02 06:21:34 +0000474 }
475
476 MCSection *OutSection = SectionPair->second.first;
477 if (OutSection == StrOffsetSection)
478 CurStrOffsetSection = Contents;
479 else if (OutSection == StrSection)
480 CurStrSection = Contents;
David Blaikiec3826da2015-12-09 21:02:33 +0000481 else if (OutSection == TypesSection)
David Blaikie62be5ae2016-04-05 20:26:50 +0000482 CurTypesSection.push_back(Contents);
David Blaikie23919372016-02-06 01:15:26 +0000483 else if (OutSection == CUIndexSection)
484 CurCUIndexSection = Contents;
David Blaikie8bce5a02016-02-17 07:00:24 +0000485 else if (OutSection == TUIndexSection)
486 CurTUIndexSection = Contents;
David Blaikieb073cb92015-12-02 06:21:34 +0000487 else {
488 Out.SwitchSection(OutSection);
489 Out.EmitBytes(Contents);
David Blaikie98ad82a2015-12-01 18:07:07 +0000490 }
David Blaikie242b9482015-12-01 00:48:39 +0000491 }
David Blaikieb073cb92015-12-02 06:21:34 +0000492
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000493 if (InfoSection.empty())
494 continue;
495
David Blaikie23919372016-02-06 01:15:26 +0000496 if (!CurCUIndexSection.empty()) {
497 DWARFUnitIndex CUIndex(DW_SECT_INFO);
David Blaikieddb27362016-03-01 21:24:04 +0000498 DataExtractor CUIndexData(CurCUIndexSection,
499 ErrOrObj->getBinary()->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 Blaikiece7c6cf2016-03-24 22:17:08 +0000509 CompileUnitIdentifiers ID = 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 Blaikied1f7ab32016-05-16 20:42:27 +0000514 if (!P.second)
515 return make_error<DWPError>(buildDuplicateError(*P.first, ID, Input));
David Blaikie852c02b2016-02-19 21:09:26 +0000516 auto &NewEntry = P.first->second;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000517 NewEntry.Name = ID.Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000518 NewEntry.DWOName = ID.DWOName;
David Blaikief1958da2016-02-26 07:30:15 +0000519 NewEntry.DWPName = Input;
David Blaikie23919372016-02-06 01:15:26 +0000520 for (auto Kind : CUIndex.getColumnKinds()) {
521 auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO];
522 C.Offset += I->Offset;
523 C.Length = I->Length;
524 ++I;
525 }
David Blaikie23919372016-02-06 01:15:26 +0000526 }
David Blaikie8bce5a02016-02-17 07:00:24 +0000527
528 if (!CurTypesSection.empty()) {
David Blaikie62be5ae2016-04-05 20:26:50 +0000529 assert(CurTypesSection.size() == 1);
David Blaikie8bce5a02016-02-17 07:00:24 +0000530 DWARFUnitIndex TUIndex(DW_SECT_TYPES);
David Blaikieddb27362016-03-01 21:24:04 +0000531 DataExtractor TUIndexData(CurTUIndexSection,
532 ErrOrObj->getBinary()->isLittleEndian(), 0);
David Blaikie8bce5a02016-02-17 07:00:24 +0000533 if (!TUIndex.parse(TUIndexData))
David Blaikiebc8397c2016-05-12 19:59:54 +0000534 return make_error<DWPError>("Failed to parse tu_index");
David Blaikie8bce5a02016-02-17 07:00:24 +0000535 addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection,
David Blaikie62be5ae2016-04-05 20:26:50 +0000536 CurTypesSection.front(), CurEntry,
David Blaikie8bce5a02016-02-17 07:00:24 +0000537 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
538 }
David Blaikie23919372016-02-06 01:15:26 +0000539 } else {
David Blaikiece7c6cf2016-03-24 22:17:08 +0000540 CompileUnitIdentifiers ID = getCUIdentifiers(
541 AbbrevSection, InfoSection, CurStrOffsetSection, CurStrSection);
542 auto P = IndexEntries.insert(std::make_pair(ID.Signature, CurEntry));
David Blaikied1f7ab32016-05-16 20:42:27 +0000543 if (!P.second)
544 return make_error<DWPError>(buildDuplicateError(*P.first, ID, ""));
David Blaikiece7c6cf2016-03-24 22:17:08 +0000545 P.first->second.Name = ID.Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000546 P.first->second.DWOName = ID.DWOName;
David Blaikie23919372016-02-06 01:15:26 +0000547 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection,
548 CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
David Blaikie23919372016-02-06 01:15:26 +0000549 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000550
David Blaikiebb94e442015-12-01 19:17:58 +0000551 if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset,
552 StrSection, StrOffsetSection,
553 CurStrSection, CurStrOffsetSection))
David Blaikie98ad82a2015-12-01 18:07:07 +0000554 return Err;
David Blaikie242b9482015-12-01 00:48:39 +0000555 }
David Blaikieb073cb92015-12-02 06:21:34 +0000556
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000557 // Lie about there being no info contributions so the TU index only includes
558 // the type unit contribution
559 ContributionOffsets[0] = 0;
560 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
561 TypeIndexEntries);
David Blaikieb3757c02015-12-02 22:01:56 +0000562
David Blaikie24c8ac92015-12-05 03:05:45 +0000563 // Lie about the type contribution
564 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
565 // Unlie about the info contribution
566 ContributionOffsets[0] = 1;
David Blaikie7c4ffe02015-12-04 21:30:23 +0000567
David Blaikie24c8ac92015-12-05 03:05:45 +0000568 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
569 IndexEntries);
David Blaikieddb27362016-03-01 21:24:04 +0000570
David Blaikiebc8397c2016-05-12 19:59:54 +0000571 return Error();
David Blaikie242b9482015-12-01 00:48:39 +0000572}
573
David Blaikie2ed678c2015-12-05 03:06:30 +0000574int main(int argc, char **argv) {
David Blaikie242b9482015-12-01 00:48:39 +0000575
576 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
577
578 llvm::InitializeAllTargetInfos();
579 llvm::InitializeAllTargetMCs();
580 llvm::InitializeAllTargets();
581 llvm::InitializeAllAsmPrinters();
582
583 std::string ErrorStr;
584 StringRef Context = "dwarf streamer init";
585
586 Triple TheTriple("x86_64-linux-gnu");
587
588 // Get the target.
589 const Target *TheTarget =
590 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
591 if (!TheTarget)
592 return error(ErrorStr, Context);
593 std::string TripleName = TheTriple.getTriple();
594
595 // Create all the MC Objects.
596 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
597 if (!MRI)
598 return error(Twine("no register info for target ") + TripleName, Context);
599
600 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
601 if (!MAI)
602 return error("no asm info for target " + TripleName, Context);
603
604 MCObjectFileInfo MOFI;
605 MCContext MC(MAI.get(), MRI.get(), &MOFI);
David Blaikie2ed678c2015-12-05 03:06:30 +0000606 MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default, MC);
David Blaikie242b9482015-12-01 00:48:39 +0000607
608 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
609 if (!MAB)
610 return error("no asm backend for target " + TripleName, Context);
611
612 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
613 if (!MII)
614 return error("no instr info info for target " + TripleName, Context);
615
616 std::unique_ptr<MCSubtargetInfo> MSTI(
617 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
618 if (!MSTI)
619 return error("no subtarget info for target " + TripleName, Context);
620
621 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
622 if (!MCE)
623 return error("no code emitter for target " + TripleName, Context);
624
625 // Create the output file.
626 std::error_code EC;
627 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
628 if (EC)
629 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
630
David Majnemer03e2cc32015-12-21 22:09:27 +0000631 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
David Blaikie242b9482015-12-01 00:48:39 +0000632 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
David Majnemer03e2cc32015-12-21 22:09:27 +0000633 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, MCOptions.MCRelaxAll,
634 MCOptions.MCIncrementalLinkerCompatible,
David Blaikie242b9482015-12-01 00:48:39 +0000635 /*DWARFMustBeAtTheEnd*/ false));
636 if (!MS)
637 return error("no object streamer for target " + TripleName, Context);
638
David Blaikiebc8397c2016-05-12 19:59:54 +0000639 if (auto Err = write(*MS, InputFiles)) {
640 logAllUnhandledErrors(std::move(Err), errs(), "error: ");
641 return 1;
642 }
David Blaikieddb27362016-03-01 21:24:04 +0000643
644 MS->Finish();
David Blaikiedf055252015-12-01 00:48:34 +0000645}