blob: 7418c77bdeca2a775427d1ff84f9d728ba405295 [file] [log] [blame]
David Blaikief72dbc12016-03-01 22:29:00 +00001//===-- llvm-dwp.cpp - Split DWARF merging tool for llvm ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// A utility for merging DWARF 5 Split DWARF .dwo files into .dwp (DWARF
11// package files).
12//
13//===----------------------------------------------------------------------===//
David Blaikiefd800922016-05-23 16:32:11 +000014#include "DWPError.h"
15#include "DWPStringPool.h"
David Blaikie852c02b2016-02-19 21:09:26 +000016#include "llvm/ADT/MapVector.h"
David Blaikie242b9482015-12-01 00:48:39 +000017#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/StringSet.h"
19#include "llvm/CodeGen/AsmPrinter.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000020#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
21#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
David Blaikie242b9482015-12-01 00:48:39 +000022#include "llvm/MC/MCAsmInfo.h"
23#include "llvm/MC/MCContext.h"
24#include "llvm/MC/MCInstrInfo.h"
25#include "llvm/MC/MCObjectFileInfo.h"
26#include "llvm/MC/MCRegisterInfo.h"
27#include "llvm/MC/MCSectionELF.h"
28#include "llvm/MC/MCStreamer.h"
David Majnemer03e2cc32015-12-21 22:09:27 +000029#include "llvm/MC/MCTargetOptionsCommandFlags.h"
David Blaikie242b9482015-12-01 00:48:39 +000030#include "llvm/Object/ObjectFile.h"
David Blaikie74f5b282016-02-19 01:51:44 +000031#include "llvm/Support/Compression.h"
David Blaikie98ad82a2015-12-01 18:07:07 +000032#include "llvm/Support/DataExtractor.h"
David Blaikiefd800922016-05-23 16:32:11 +000033#include "llvm/Support/Error.h"
David Blaikie242b9482015-12-01 00:48:39 +000034#include "llvm/Support/FileSystem.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000035#include "llvm/Support/MathExtras.h"
David Blaikie242b9482015-12-01 00:48:39 +000036#include "llvm/Support/MemoryBuffer.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000037#include "llvm/Support/Options.h"
David Blaikie242b9482015-12-01 00:48:39 +000038#include "llvm/Support/TargetRegistry.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000039#include "llvm/Support/TargetSelect.h"
David Blaikie242b9482015-12-01 00:48:39 +000040#include "llvm/Support/raw_ostream.h"
41#include "llvm/Target/TargetMachine.h"
David Blaikie1fc3e6b2016-05-25 23:37:06 +000042#include <deque>
David Blaikief1958da2016-02-26 07:30:15 +000043#include <iostream>
David Blaikie2ed678c2015-12-05 03:06:30 +000044#include <memory>
David Blaikie242b9482015-12-01 00:48:39 +000045
46using namespace llvm;
David Blaikie98ad82a2015-12-01 18:07:07 +000047using namespace llvm::object;
David Blaikie242b9482015-12-01 00:48:39 +000048using namespace cl;
49
50OptionCategory DwpCategory("Specific Options");
51static list<std::string> InputFiles(Positional, OneOrMore,
52 desc("<input files>"), cat(DwpCategory));
53
David Blaikie2ed678c2015-12-05 03:06:30 +000054static opt<std::string> OutputFilename(Required, "o",
55 desc("Specify the output file."),
56 value_desc("filename"),
57 cat(DwpCategory));
David Blaikie242b9482015-12-01 00:48:39 +000058
David Blaikiefd800922016-05-23 16:32:11 +000059static void writeStringsAndOffsets(MCStreamer &Out, DWPStringPool &Strings,
60 MCSection *StrOffsetSection,
61 StringRef CurStrSection,
62 StringRef CurStrOffsetSection) {
David Blaikie98ad82a2015-12-01 18:07:07 +000063 // Could possibly produce an error or warning if one of these was non-null but
64 // the other was null.
65 if (CurStrSection.empty() || CurStrOffsetSection.empty())
David Blaikiebc619cd2016-05-17 23:44:13 +000066 return;
David Blaikie98ad82a2015-12-01 18:07:07 +000067
68 DenseMap<uint32_t, uint32_t> OffsetRemapping;
69
70 DataExtractor Data(CurStrSection, true, 0);
71 uint32_t LocalOffset = 0;
72 uint32_t PrevOffset = 0;
73 while (const char *s = Data.getCStr(&LocalOffset)) {
David Blaikiefd800922016-05-23 16:32:11 +000074 OffsetRemapping[PrevOffset] =
75 Strings.getOffset(s, LocalOffset - PrevOffset);
David Blaikie98ad82a2015-12-01 18:07:07 +000076 PrevOffset = LocalOffset;
77 }
78
79 Data = DataExtractor(CurStrOffsetSection, true, 0);
80
81 Out.SwitchSection(StrOffsetSection);
82
83 uint32_t Offset = 0;
84 uint64_t Size = CurStrOffsetSection.size();
85 while (Offset < Size) {
86 auto OldOffset = Data.getU32(&Offset);
87 auto NewOffset = OffsetRemapping[OldOffset];
88 Out.EmitIntValue(NewOffset, 4);
89 }
David Blaikie242b9482015-12-01 00:48:39 +000090}
91
David Blaikiead07b5d2015-12-04 17:20:04 +000092static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
93 uint64_t CurCode;
94 uint32_t Offset = 0;
95 DataExtractor AbbrevData(Abbrev, true, 0);
96 while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) {
97 // Tag
98 AbbrevData.getULEB128(&Offset);
99 // DW_CHILDREN
100 AbbrevData.getU8(&Offset);
101 // Attributes
102 while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset))
103 ;
104 }
105 return Offset;
106}
107
David Blaikiece7c6cf2016-03-24 22:17:08 +0000108struct CompileUnitIdentifiers {
109 uint64_t Signature = 0;
David Blaikie4dd03f02016-03-26 20:32:14 +0000110 const char *Name = "";
111 const char *DWOName = "";
David Blaikiece7c6cf2016-03-24 22:17:08 +0000112};
113
David Blaikie4940f872016-05-17 00:07:10 +0000114static Expected<const char *>
Greg Clayton6c273762016-10-27 16:32:04 +0000115getIndexedString(dwarf::Form Form, DataExtractor InfoData,
116 uint32_t &InfoOffset, StringRef StrOffsets, StringRef Str) {
David Blaikie60fbd3b2016-04-05 20:16:38 +0000117 if (Form == dwarf::DW_FORM_string)
118 return InfoData.getCStr(&InfoOffset);
David Blaikie4940f872016-05-17 00:07:10 +0000119 if (Form != dwarf::DW_FORM_GNU_str_index)
120 return make_error<DWPError>(
121 "string field encoded without DW_FORM_string or DW_FORM_GNU_str_index");
David Blaikie60fbd3b2016-04-05 20:16:38 +0000122 auto StrIndex = InfoData.getULEB128(&InfoOffset);
David Blaikie4dd03f02016-03-26 20:32:14 +0000123 DataExtractor StrOffsetsData(StrOffsets, true, 0);
124 uint32_t StrOffsetsOffset = 4 * StrIndex;
125 uint32_t StrOffset = StrOffsetsData.getU32(&StrOffsetsOffset);
126 DataExtractor StrData(Str, true, 0);
127 return StrData.getCStr(&StrOffset);
128}
129
David Blaikie7bb62ef2016-05-16 23:26:29 +0000130static Expected<CompileUnitIdentifiers> getCUIdentifiers(StringRef Abbrev,
131 StringRef Info,
132 StringRef StrOffsets,
133 StringRef Str) {
David Blaikief1958da2016-02-26 07:30:15 +0000134 uint32_t Offset = 0;
135 DataExtractor InfoData(Info, true, 0);
Greg Clayton82f12b12016-11-11 16:21:37 +0000136 dwarf::DwarfFormat Format = dwarf::DwarfFormat::DWARF32;
137 uint64_t Length = InfoData.getU32(&Offset);
138 // If the length is 0xffffffff, then this indictes that this is a DWARF 64
139 // stream and the length is actually encoded into a 64 bit value that follows.
140 if (Length == 0xffffffffU) {
141 Format = dwarf::DwarfFormat::DWARF64;
142 Length = InfoData.getU64(&Offset);
143 }
David Blaikief1958da2016-02-26 07:30:15 +0000144 uint16_t Version = InfoData.getU16(&Offset);
145 InfoData.getU32(&Offset); // Abbrev offset (should be zero)
146 uint8_t AddrSize = InfoData.getU8(&Offset);
147
148 uint32_t AbbrCode = InfoData.getULEB128(&Offset);
149
150 DataExtractor AbbrevData(Abbrev, true, 0);
151 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
Greg Clayton6c273762016-10-27 16:32:04 +0000152 auto Tag = static_cast<dwarf::Tag>(AbbrevData.getULEB128(&AbbrevOffset));
David Blaikie7bb62ef2016-05-16 23:26:29 +0000153 if (Tag != dwarf::DW_TAG_compile_unit)
154 return make_error<DWPError>("top level DIE is not a compile unit");
David Blaikief1958da2016-02-26 07:30:15 +0000155 // DW_CHILDREN
156 AbbrevData.getU8(&AbbrevOffset);
157 uint32_t Name;
Greg Clayton6c273762016-10-27 16:32:04 +0000158 dwarf::Form Form;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000159 CompileUnitIdentifiers ID;
David Blaikief1958da2016-02-26 07:30:15 +0000160 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |
Greg Clayton6c273762016-10-27 16:32:04 +0000161 (Form = static_cast<dwarf::Form>(AbbrevData.getULEB128(&AbbrevOffset))) &&
David Blaikiece7c6cf2016-03-24 22:17:08 +0000162 (Name != 0 || Form != 0)) {
163 switch (Name) {
164 case dwarf::DW_AT_name: {
David Blaikie4940f872016-05-17 00:07:10 +0000165 Expected<const char *> EName =
166 getIndexedString(Form, InfoData, Offset, StrOffsets, Str);
167 if (!EName)
168 return EName.takeError();
169 ID.Name = *EName;
David Blaikie4dd03f02016-03-26 20:32:14 +0000170 break;
171 }
172 case dwarf::DW_AT_GNU_dwo_name: {
David Blaikie4940f872016-05-17 00:07:10 +0000173 Expected<const char *> EName =
174 getIndexedString(Form, InfoData, Offset, StrOffsets, Str);
175 if (!EName)
176 return EName.takeError();
177 ID.DWOName = *EName;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000178 break;
179 }
180 case dwarf::DW_AT_GNU_dwo_id:
181 ID.Signature = InfoData.getU64(&Offset);
182 break;
183 default:
Greg Clayton82f12b12016-11-11 16:21:37 +0000184 DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize,
185 Format);
David Blaikiece7c6cf2016-03-24 22:17:08 +0000186 }
David Blaikief1958da2016-02-26 07:30:15 +0000187 }
David Blaikiece7c6cf2016-03-24 22:17:08 +0000188 return ID;
David Blaikiead07b5d2015-12-04 17:20:04 +0000189}
190
David Blaikie24c8ac92015-12-05 03:05:45 +0000191struct UnitIndexEntry {
David Blaikie24c8ac92015-12-05 03:05:45 +0000192 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
David Blaikief1958da2016-02-26 07:30:15 +0000193 std::string Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000194 std::string DWOName;
David Blaikief1958da2016-02-26 07:30:15 +0000195 StringRef DWPName;
David Blaikie24c8ac92015-12-05 03:05:45 +0000196};
197
David Blaikiefd800922016-05-23 16:32:11 +0000198static StringRef getSubsection(StringRef Section,
199 const DWARFUnitIndex::Entry &Entry,
200 DWARFSectionKind Kind) {
David Blaikief1958da2016-02-26 07:30:15 +0000201 const auto *Off = Entry.getOffset(Kind);
202 if (!Off)
203 return StringRef();
204 return Section.substr(Off->Offset, Off->Length);
205}
206
David Blaikie852c02b2016-02-19 21:09:26 +0000207static void addAllTypesFromDWP(
208 MCStreamer &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
209 const DWARFUnitIndex &TUIndex, MCSection *OutputTypes, StringRef Types,
210 const UnitIndexEntry &TUEntry, uint32_t &TypesOffset) {
David Blaikie8bce5a02016-02-17 07:00:24 +0000211 Out.SwitchSection(OutputTypes);
212 for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) {
213 auto *I = E.getOffsets();
214 if (!I)
215 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000216 auto P = TypeIndexEntries.insert(std::make_pair(E.getSignature(), TUEntry));
217 if (!P.second)
David Blaikie8bce5a02016-02-17 07:00:24 +0000218 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000219 auto &Entry = P.first->second;
David Blaikie8bce5a02016-02-17 07:00:24 +0000220 // Zero out the debug_info contribution
221 Entry.Contributions[0] = {};
222 for (auto Kind : TUIndex.getColumnKinds()) {
223 auto &C = Entry.Contributions[Kind - DW_SECT_INFO];
224 C.Offset += I->Offset;
225 C.Length = I->Length;
226 ++I;
227 }
228 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
229 Out.EmitBytes(Types.substr(
230 C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset,
231 C.Length));
232 C.Offset = TypesOffset;
233 TypesOffset += C.Length;
David Blaikie8bce5a02016-02-17 07:00:24 +0000234 }
235}
236
David Blaikiec3826da2015-12-09 21:02:33 +0000237static void addAllTypes(MCStreamer &Out,
David Blaikie852c02b2016-02-19 21:09:26 +0000238 MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
David Blaikie62be5ae2016-04-05 20:26:50 +0000239 MCSection *OutputTypes,
240 const std::vector<StringRef> &TypesSections,
David Blaikiec3826da2015-12-09 21:02:33 +0000241 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) {
David Blaikie62be5ae2016-04-05 20:26:50 +0000242 for (StringRef Types : TypesSections) {
243 Out.SwitchSection(OutputTypes);
244 uint32_t Offset = 0;
245 DataExtractor Data(Types, true, 0);
246 while (Data.isValidOffset(Offset)) {
247 UnitIndexEntry Entry = CUEntry;
248 // Zero out the debug_info contribution
249 Entry.Contributions[0] = {};
250 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
251 C.Offset = TypesOffset;
252 auto PrevOffset = Offset;
253 // Length of the unit, including the 4 byte length field.
254 C.Length = Data.getU32(&Offset) + 4;
David Blaikiec3826da2015-12-09 21:02:33 +0000255
David Blaikie62be5ae2016-04-05 20:26:50 +0000256 Data.getU16(&Offset); // Version
257 Data.getU32(&Offset); // Abbrev offset
258 Data.getU8(&Offset); // Address size
259 auto Signature = Data.getU64(&Offset);
260 Offset = PrevOffset + C.Length;
David Blaikie24c8ac92015-12-05 03:05:45 +0000261
David Blaikie62be5ae2016-04-05 20:26:50 +0000262 auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry));
263 if (!P.second)
264 continue;
David Blaikief5cb6272015-12-14 07:42:00 +0000265
David Blaikie62be5ae2016-04-05 20:26:50 +0000266 Out.EmitBytes(Types.substr(PrevOffset, C.Length));
267 TypesOffset += C.Length;
268 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000269 }
270}
271
272static void
273writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,
David Blaikie852c02b2016-02-19 21:09:26 +0000274 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries,
David Blaikie24c8ac92015-12-05 03:05:45 +0000275 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) {
276 for (const auto &E : IndexEntries)
David Blaikie852c02b2016-02-19 21:09:26 +0000277 for (size_t i = 0; i != array_lengthof(E.second.Contributions); ++i)
David Blaikie24c8ac92015-12-05 03:05:45 +0000278 if (ContributionOffsets[i])
David Blaikie852c02b2016-02-19 21:09:26 +0000279 Out.EmitIntValue(E.second.Contributions[i].*Field, 4);
David Blaikie24c8ac92015-12-05 03:05:45 +0000280}
281
David Blaikie852c02b2016-02-19 21:09:26 +0000282static void
283writeIndex(MCStreamer &Out, MCSection *Section,
284 ArrayRef<unsigned> ContributionOffsets,
285 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries) {
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000286 if (IndexEntries.empty())
287 return;
288
David Blaikie24c8ac92015-12-05 03:05:45 +0000289 unsigned Columns = 0;
290 for (auto &C : ContributionOffsets)
291 if (C)
292 ++Columns;
293
294 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
295 uint64_t Mask = Buckets.size() - 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000296 size_t i = 0;
297 for (const auto &P : IndexEntries) {
298 auto S = P.first;
David Blaikie24c8ac92015-12-05 03:05:45 +0000299 auto H = S & Mask;
David Blaikie9b492562016-04-05 17:51:40 +0000300 auto HP = ((S >> 32) & Mask) | 1;
David Blaikiec3826da2015-12-09 21:02:33 +0000301 while (Buckets[H]) {
David Blaikie852c02b2016-02-19 21:09:26 +0000302 assert(S != IndexEntries.begin()[Buckets[H] - 1].first &&
David Blaikie74f5b282016-02-19 01:51:44 +0000303 "Duplicate unit");
David Blaikie9b492562016-04-05 17:51:40 +0000304 H = (H + HP) & Mask;
David Blaikiec3826da2015-12-09 21:02:33 +0000305 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000306 Buckets[H] = i + 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000307 ++i;
David Blaikie24c8ac92015-12-05 03:05:45 +0000308 }
309
310 Out.SwitchSection(Section);
311 Out.EmitIntValue(2, 4); // Version
312 Out.EmitIntValue(Columns, 4); // Columns
313 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
David Blaikie2ed678c2015-12-05 03:06:30 +0000314 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets
David Blaikie24c8ac92015-12-05 03:05:45 +0000315
316 // Write the signatures.
317 for (const auto &I : Buckets)
David Blaikie852c02b2016-02-19 21:09:26 +0000318 Out.EmitIntValue(I ? IndexEntries.begin()[I - 1].first : 0, 8);
David Blaikie24c8ac92015-12-05 03:05:45 +0000319
320 // Write the indexes.
321 for (const auto &I : Buckets)
322 Out.EmitIntValue(I, 4);
323
324 // Write the column headers (which sections will appear in the table)
325 for (size_t i = 0; i != ContributionOffsets.size(); ++i)
326 if (ContributionOffsets[i])
327 Out.EmitIntValue(i + DW_SECT_INFO, 4);
328
329 // Write the offsets.
330 writeIndexTable(Out, ContributionOffsets, IndexEntries,
331 &DWARFUnitIndex::Entry::SectionContribution::Offset);
332
333 // Write the lengths.
334 writeIndexTable(Out, ContributionOffsets, IndexEntries,
335 &DWARFUnitIndex::Entry::SectionContribution::Length);
336}
David Blaikie74f5b282016-02-19 01:51:44 +0000337static bool consumeCompressedDebugSectionHeader(StringRef &data,
338 uint64_t &OriginalSize) {
339 // Consume "ZLIB" prefix.
340 if (!data.startswith("ZLIB"))
341 return false;
342 data = data.substr(4);
343 // Consume uncompressed section size (big-endian 8 bytes).
344 DataExtractor extractor(data, false, 8);
345 uint32_t Offset = 0;
346 OriginalSize = extractor.getU64(&Offset);
347 if (Offset == 0)
348 return false;
349 data = data.substr(Offset);
350 return true;
351}
352
David Blaikied1f7ab32016-05-16 20:42:27 +0000353std::string buildDWODescription(StringRef Name, StringRef DWPName, StringRef DWOName) {
354 std::string Text = "\'";
355 Text += Name;
356 Text += '\'';
David Blaikie4dd03f02016-03-26 20:32:14 +0000357 if (!DWPName.empty()) {
David Blaikied1f7ab32016-05-16 20:42:27 +0000358 Text += " (from ";
359 if (!DWOName.empty()) {
360 Text += '\'';
361 Text += DWOName;
362 Text += "' in ";
363 }
364 Text += '\'';
365 Text += DWPName;
366 Text += "')";
David Blaikie4dd03f02016-03-26 20:32:14 +0000367 }
David Blaikied1f7ab32016-05-16 20:42:27 +0000368 return Text;
369}
370
David Blaikie05e0d2b2016-05-23 21:58:58 +0000371static Error handleCompressedSection(
David Blaikie1fc3e6b2016-05-25 23:37:06 +0000372 std::deque<SmallString<32>> &UncompressedSections, StringRef &Name,
David Blaikie05e0d2b2016-05-23 21:58:58 +0000373 StringRef &Contents) {
374 if (!Name.startswith("zdebug_"))
Mehdi Amini41af4302016-11-11 04:28:40 +0000375 return Error::success();
David Blaikie05e0d2b2016-05-23 21:58:58 +0000376 UncompressedSections.emplace_back();
377 uint64_t OriginalSize;
378 if (!zlib::isAvailable())
379 return make_error<DWPError>("zlib not available");
380 if (!consumeCompressedDebugSectionHeader(Contents, OriginalSize) ||
381 zlib::uncompress(Contents, UncompressedSections.back(), OriginalSize) !=
382 zlib::StatusOK)
383 return make_error<DWPError>(
384 ("failure while decompressing compressed section: '" + Name + "\'")
385 .str());
386 Name = Name.substr(1);
387 Contents = UncompressedSections.back();
Mehdi Amini41af4302016-11-11 04:28:40 +0000388 return Error::success();
David Blaikie05e0d2b2016-05-23 21:58:58 +0000389}
David Blaikied9517cb2016-05-23 22:21:10 +0000390
391static Error handleSection(
392 const StringMap<std::pair<MCSection *, DWARFSectionKind>> &KnownSections,
393 const MCSection *StrSection, const MCSection *StrOffsetSection,
394 const MCSection *TypesSection, const MCSection *CUIndexSection,
395 const MCSection *TUIndexSection, const SectionRef &Section, MCStreamer &Out,
David Blaikie1fc3e6b2016-05-25 23:37:06 +0000396 std::deque<SmallString<32>> &UncompressedSections,
David Blaikied9517cb2016-05-23 22:21:10 +0000397 uint32_t (&ContributionOffsets)[8], UnitIndexEntry &CurEntry,
398 StringRef &CurStrSection, StringRef &CurStrOffsetSection,
399 std::vector<StringRef> &CurTypesSection, StringRef &InfoSection,
400 StringRef &AbbrevSection, StringRef &CurCUIndexSection,
401 StringRef &CurTUIndexSection) {
402 if (Section.isBSS())
Mehdi Amini41af4302016-11-11 04:28:40 +0000403 return Error::success();
David Blaikied9517cb2016-05-23 22:21:10 +0000404
405 if (Section.isVirtual())
Mehdi Amini41af4302016-11-11 04:28:40 +0000406 return Error::success();
David Blaikied9517cb2016-05-23 22:21:10 +0000407
408 StringRef Name;
409 if (std::error_code Err = Section.getName(Name))
410 return errorCodeToError(Err);
411
412 Name = Name.substr(Name.find_first_not_of("._"));
413
414 StringRef Contents;
415 if (auto Err = Section.getContents(Contents))
416 return errorCodeToError(Err);
417
418 if (auto Err = handleCompressedSection(UncompressedSections, Name, Contents))
419 return Err;
420
421 auto SectionPair = KnownSections.find(Name);
422 if (SectionPair == KnownSections.end())
Mehdi Amini41af4302016-11-11 04:28:40 +0000423 return Error::success();
David Blaikied9517cb2016-05-23 22:21:10 +0000424
425 if (DWARFSectionKind Kind = SectionPair->second.second) {
426 auto Index = Kind - DW_SECT_INFO;
427 if (Kind != DW_SECT_TYPES) {
428 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
429 ContributionOffsets[Index] +=
430 (CurEntry.Contributions[Index].Length = Contents.size());
431 }
432
433 switch (Kind) {
434 case DW_SECT_INFO:
435 InfoSection = Contents;
436 break;
437 case DW_SECT_ABBREV:
438 AbbrevSection = Contents;
439 break;
440 default:
441 break;
442 }
443 }
444
445 MCSection *OutSection = SectionPair->second.first;
446 if (OutSection == StrOffsetSection)
447 CurStrOffsetSection = Contents;
448 else if (OutSection == StrSection)
449 CurStrSection = Contents;
450 else if (OutSection == TypesSection)
451 CurTypesSection.push_back(Contents);
452 else if (OutSection == CUIndexSection)
453 CurCUIndexSection = Contents;
454 else if (OutSection == TUIndexSection)
455 CurTUIndexSection = Contents;
456 else {
457 Out.SwitchSection(OutSection);
458 Out.EmitBytes(Contents);
459 }
Mehdi Amini41af4302016-11-11 04:28:40 +0000460 return Error::success();
David Blaikied9517cb2016-05-23 22:21:10 +0000461}
462
463static Error
464buildDuplicateError(const std::pair<uint64_t, UnitIndexEntry> &PrevE,
465 const CompileUnitIdentifiers &ID, StringRef DWPName) {
David Blaikie11825c72016-05-17 19:40:28 +0000466 return make_error<DWPError>(
467 std::string("Duplicate DWO ID (") + utohexstr(PrevE.first) + ") in " +
468 buildDWODescription(PrevE.second.Name, PrevE.second.DWPName,
469 PrevE.second.DWOName) +
470 " and " + buildDWODescription(ID.Name, DWPName, ID.DWOName));
David Blaikie4dd03f02016-03-26 20:32:14 +0000471}
David Blaikied9517cb2016-05-23 22:21:10 +0000472
David Blaikiebc8397c2016-05-12 19:59:54 +0000473static Error write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000474 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
475 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
476 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikiec3826da2015-12-09 21:02:33 +0000477 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
David Blaikie23919372016-02-06 01:15:26 +0000478 MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection();
David Blaikie8bce5a02016-02-17 07:00:24 +0000479 MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000480 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
481 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
482 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
483 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
484 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
485 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
David Blaikieb7020252015-12-04 21:16:42 +0000486 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikie23919372016-02-06 01:15:26 +0000487 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
David Blaikie8bce5a02016-02-17 07:00:24 +0000488 {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}},
489 {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}};
David Blaikieb073cb92015-12-02 06:21:34 +0000490
David Blaikie852c02b2016-02-19 21:09:26 +0000491 MapVector<uint64_t, UnitIndexEntry> IndexEntries;
492 MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000493
David Blaikieb073cb92015-12-02 06:21:34 +0000494 uint32_t ContributionOffsets[8] = {};
495
David Blaikiefd800922016-05-23 16:32:11 +0000496 DWPStringPool Strings(Out, StrSection);
497
498 SmallVector<OwningBinary<object::ObjectFile>, 128> Objects;
499 Objects.reserve(Inputs.size());
500
David Blaikie1fc3e6b2016-05-25 23:37:06 +0000501 std::deque<SmallString<32>> UncompressedSections;
David Blaikie2e9bd892016-05-23 17:35:51 +0000502
David Blaikie242b9482015-12-01 00:48:39 +0000503 for (const auto &Input : Inputs) {
504 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
505 if (!ErrOrObj)
David Blaikiebc8397c2016-05-12 19:59:54 +0000506 return ErrOrObj.takeError();
David Blaikieb073cb92015-12-02 06:21:34 +0000507
David Blaikiefd800922016-05-23 16:32:11 +0000508 auto &Obj = *ErrOrObj->getBinary();
509 Objects.push_back(std::move(*ErrOrObj));
510
David Blaikie23919372016-02-06 01:15:26 +0000511 UnitIndexEntry CurEntry = {};
David Blaikieb073cb92015-12-02 06:21:34 +0000512
David Blaikie98ad82a2015-12-01 18:07:07 +0000513 StringRef CurStrSection;
514 StringRef CurStrOffsetSection;
David Blaikie62be5ae2016-04-05 20:26:50 +0000515 std::vector<StringRef> CurTypesSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000516 StringRef InfoSection;
517 StringRef AbbrevSection;
David Blaikie23919372016-02-06 01:15:26 +0000518 StringRef CurCUIndexSection;
David Blaikie8bce5a02016-02-17 07:00:24 +0000519 StringRef CurTUIndexSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000520
David Blaikied9517cb2016-05-23 22:21:10 +0000521 for (const auto &Section : Obj.sections())
522 if (auto Err = handleSection(
523 KnownSections, StrSection, StrOffsetSection, TypesSection,
524 CUIndexSection, TUIndexSection, Section, Out,
525 UncompressedSections, ContributionOffsets, CurEntry,
526 CurStrSection, CurStrOffsetSection, CurTypesSection, InfoSection,
527 AbbrevSection, CurCUIndexSection, CurTUIndexSection))
David Blaikie05e0d2b2016-05-23 21:58:58 +0000528 return Err;
David Blaikie74f5b282016-02-19 01:51:44 +0000529
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000530 if (InfoSection.empty())
531 continue;
532
David Blaikie478c1a22016-05-23 22:38:06 +0000533 writeStringsAndOffsets(Out, Strings, StrOffsetSection, CurStrSection,
534 CurStrOffsetSection);
David Blaikie23919372016-02-06 01:15:26 +0000535
David Blaikie478c1a22016-05-23 22:38:06 +0000536 if (CurCUIndexSection.empty()) {
David Blaikie7bb62ef2016-05-16 23:26:29 +0000537 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(
David Blaikiece7c6cf2016-03-24 22:17:08 +0000538 AbbrevSection, InfoSection, CurStrOffsetSection, CurStrSection);
David Blaikie7bb62ef2016-05-16 23:26:29 +0000539 if (!EID)
540 return EID.takeError();
541 const auto &ID = *EID;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000542 auto P = IndexEntries.insert(std::make_pair(ID.Signature, CurEntry));
David Blaikied1f7ab32016-05-16 20:42:27 +0000543 if (!P.second)
David Blaikie11825c72016-05-17 19:40:28 +0000544 return 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 Blaikie478c1a22016-05-23 22:38:06 +0000549 continue;
David Blaikie23919372016-02-06 01:15:26 +0000550 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000551
David Blaikie478c1a22016-05-23 22:38:06 +0000552 DWARFUnitIndex CUIndex(DW_SECT_INFO);
553 DataExtractor CUIndexData(CurCUIndexSection, Obj.isLittleEndian(), 0);
554 if (!CUIndex.parse(CUIndexData))
555 return make_error<DWPError>("Failed to parse cu_index");
556
557 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
558 auto *I = E.getOffsets();
559 if (!I)
560 continue;
561 auto P = IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry));
562 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(
563 getSubsection(AbbrevSection, E, DW_SECT_ABBREV),
564 getSubsection(InfoSection, E, DW_SECT_INFO),
565 getSubsection(CurStrOffsetSection, E, DW_SECT_STR_OFFSETS),
566 CurStrSection);
567 if (!EID)
568 return EID.takeError();
569 const auto &ID = *EID;
570 if (!P.second)
571 return buildDuplicateError(*P.first, ID, Input);
572 auto &NewEntry = P.first->second;
573 NewEntry.Name = ID.Name;
574 NewEntry.DWOName = ID.DWOName;
575 NewEntry.DWPName = Input;
576 for (auto Kind : CUIndex.getColumnKinds()) {
577 auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO];
578 C.Offset += I->Offset;
579 C.Length = I->Length;
580 ++I;
581 }
582 }
583
584 if (!CurTypesSection.empty()) {
585 if (CurTypesSection.size() != 1)
586 return make_error<DWPError>("multiple type unit sections in .dwp file");
587 DWARFUnitIndex TUIndex(DW_SECT_TYPES);
588 DataExtractor TUIndexData(CurTUIndexSection, Obj.isLittleEndian(), 0);
589 if (!TUIndex.parse(TUIndexData))
590 return make_error<DWPError>("Failed to parse tu_index");
591 addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection,
592 CurTypesSection.front(), CurEntry,
593 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
594 }
David Blaikie242b9482015-12-01 00:48:39 +0000595 }
David Blaikieb073cb92015-12-02 06:21:34 +0000596
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000597 // Lie about there being no info contributions so the TU index only includes
598 // the type unit contribution
599 ContributionOffsets[0] = 0;
600 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
601 TypeIndexEntries);
David Blaikieb3757c02015-12-02 22:01:56 +0000602
David Blaikie24c8ac92015-12-05 03:05:45 +0000603 // Lie about the type contribution
604 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
605 // Unlie about the info contribution
606 ContributionOffsets[0] = 1;
David Blaikie7c4ffe02015-12-04 21:30:23 +0000607
David Blaikie24c8ac92015-12-05 03:05:45 +0000608 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
609 IndexEntries);
David Blaikieddb27362016-03-01 21:24:04 +0000610
Mehdi Amini41af4302016-11-11 04:28:40 +0000611 return Error::success();
David Blaikie242b9482015-12-01 00:48:39 +0000612}
613
David Blaikie17ab78e2016-05-17 23:37:44 +0000614static int error(const Twine &Error, const Twine &Context) {
615 errs() << Twine("while processing ") + Context + ":\n";
616 errs() << Twine("error: ") + Error + "\n";
617 return 1;
618}
619
David Blaikie2ed678c2015-12-05 03:06:30 +0000620int main(int argc, char **argv) {
David Blaikie242b9482015-12-01 00:48:39 +0000621
622 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
623
624 llvm::InitializeAllTargetInfos();
625 llvm::InitializeAllTargetMCs();
626 llvm::InitializeAllTargets();
627 llvm::InitializeAllAsmPrinters();
628
629 std::string ErrorStr;
630 StringRef Context = "dwarf streamer init";
631
632 Triple TheTriple("x86_64-linux-gnu");
633
634 // Get the target.
635 const Target *TheTarget =
636 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
637 if (!TheTarget)
638 return error(ErrorStr, Context);
639 std::string TripleName = TheTriple.getTriple();
640
641 // Create all the MC Objects.
642 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
643 if (!MRI)
644 return error(Twine("no register info for target ") + TripleName, Context);
645
646 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
647 if (!MAI)
648 return error("no asm info for target " + TripleName, Context);
649
650 MCObjectFileInfo MOFI;
651 MCContext MC(MAI.get(), MRI.get(), &MOFI);
Rafael Espindola699281c2016-05-18 11:58:50 +0000652 MOFI.InitMCObjectFileInfo(TheTriple, /*PIC*/ false, CodeModel::Default, MC);
David Blaikie242b9482015-12-01 00:48:39 +0000653
Joel Jones373d7d32016-07-25 17:18:28 +0000654 MCTargetOptions Options;
655 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "", Options);
David Blaikie242b9482015-12-01 00:48:39 +0000656 if (!MAB)
657 return error("no asm backend for target " + TripleName, Context);
658
659 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
660 if (!MII)
661 return error("no instr info info for target " + TripleName, Context);
662
663 std::unique_ptr<MCSubtargetInfo> MSTI(
664 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
665 if (!MSTI)
666 return error("no subtarget info for target " + TripleName, Context);
667
668 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
669 if (!MCE)
670 return error("no code emitter for target " + TripleName, Context);
671
672 // Create the output file.
673 std::error_code EC;
674 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
675 if (EC)
676 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
677
David Majnemer03e2cc32015-12-21 22:09:27 +0000678 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
David Blaikie242b9482015-12-01 00:48:39 +0000679 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
David Majnemer03e2cc32015-12-21 22:09:27 +0000680 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, MCOptions.MCRelaxAll,
681 MCOptions.MCIncrementalLinkerCompatible,
David Blaikie242b9482015-12-01 00:48:39 +0000682 /*DWARFMustBeAtTheEnd*/ false));
683 if (!MS)
684 return error("no object streamer for target " + TripleName, Context);
685
David Blaikiebc8397c2016-05-12 19:59:54 +0000686 if (auto Err = write(*MS, InputFiles)) {
687 logAllUnhandledErrors(std::move(Err), errs(), "error: ");
688 return 1;
689 }
David Blaikieddb27362016-03-01 21:24:04 +0000690
691 MS->Finish();
David Blaikiedf055252015-12-01 00:48:34 +0000692}