blob: 2297db6097e0f4d719ee18a235c178b6df438c91 [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"
George Rimar8f5976e2017-01-13 15:58:55 +000030#include "llvm/Object/Decompressor.h"
David Blaikie242b9482015-12-01 00:48:39 +000031#include "llvm/Object/ObjectFile.h"
David Blaikie74f5b282016-02-19 01:51:44 +000032#include "llvm/Support/Compression.h"
David Blaikie98ad82a2015-12-01 18:07:07 +000033#include "llvm/Support/DataExtractor.h"
David Blaikiefd800922016-05-23 16:32:11 +000034#include "llvm/Support/Error.h"
David Blaikie242b9482015-12-01 00:48:39 +000035#include "llvm/Support/FileSystem.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000036#include "llvm/Support/MathExtras.h"
David Blaikie242b9482015-12-01 00:48:39 +000037#include "llvm/Support/MemoryBuffer.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000038#include "llvm/Support/Options.h"
David Blaikie242b9482015-12-01 00:48:39 +000039#include "llvm/Support/TargetRegistry.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000040#include "llvm/Support/TargetSelect.h"
David Blaikie242b9482015-12-01 00:48:39 +000041#include "llvm/Support/raw_ostream.h"
42#include "llvm/Target/TargetMachine.h"
David Blaikie1fc3e6b2016-05-25 23:37:06 +000043#include <deque>
David Blaikief1958da2016-02-26 07:30:15 +000044#include <iostream>
David Blaikie2ed678c2015-12-05 03:06:30 +000045#include <memory>
David Blaikie242b9482015-12-01 00:48:39 +000046
47using namespace llvm;
David Blaikie98ad82a2015-12-01 18:07:07 +000048using namespace llvm::object;
David Blaikie242b9482015-12-01 00:48:39 +000049using namespace cl;
50
51OptionCategory DwpCategory("Specific Options");
52static list<std::string> InputFiles(Positional, OneOrMore,
53 desc("<input files>"), cat(DwpCategory));
54
David Blaikie2ed678c2015-12-05 03:06:30 +000055static opt<std::string> OutputFilename(Required, "o",
56 desc("Specify the output file."),
57 value_desc("filename"),
58 cat(DwpCategory));
David Blaikie242b9482015-12-01 00:48:39 +000059
David Blaikiefd800922016-05-23 16:32:11 +000060static void writeStringsAndOffsets(MCStreamer &Out, DWPStringPool &Strings,
61 MCSection *StrOffsetSection,
62 StringRef CurStrSection,
63 StringRef CurStrOffsetSection) {
David Blaikie98ad82a2015-12-01 18:07:07 +000064 // Could possibly produce an error or warning if one of these was non-null but
65 // the other was null.
66 if (CurStrSection.empty() || CurStrOffsetSection.empty())
David Blaikiebc619cd2016-05-17 23:44:13 +000067 return;
David Blaikie98ad82a2015-12-01 18:07:07 +000068
69 DenseMap<uint32_t, uint32_t> OffsetRemapping;
70
71 DataExtractor Data(CurStrSection, true, 0);
72 uint32_t LocalOffset = 0;
73 uint32_t PrevOffset = 0;
74 while (const char *s = Data.getCStr(&LocalOffset)) {
David Blaikiefd800922016-05-23 16:32:11 +000075 OffsetRemapping[PrevOffset] =
76 Strings.getOffset(s, LocalOffset - PrevOffset);
David Blaikie98ad82a2015-12-01 18:07:07 +000077 PrevOffset = LocalOffset;
78 }
79
80 Data = DataExtractor(CurStrOffsetSection, true, 0);
81
82 Out.SwitchSection(StrOffsetSection);
83
84 uint32_t Offset = 0;
85 uint64_t Size = CurStrOffsetSection.size();
86 while (Offset < Size) {
87 auto OldOffset = Data.getU32(&Offset);
88 auto NewOffset = OffsetRemapping[OldOffset];
89 Out.EmitIntValue(NewOffset, 4);
90 }
David Blaikie242b9482015-12-01 00:48:39 +000091}
92
David Blaikiead07b5d2015-12-04 17:20:04 +000093static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
94 uint64_t CurCode;
95 uint32_t Offset = 0;
96 DataExtractor AbbrevData(Abbrev, true, 0);
97 while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) {
98 // Tag
99 AbbrevData.getULEB128(&Offset);
100 // DW_CHILDREN
101 AbbrevData.getU8(&Offset);
102 // Attributes
103 while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset))
104 ;
105 }
106 return Offset;
107}
108
David Blaikiece7c6cf2016-03-24 22:17:08 +0000109struct CompileUnitIdentifiers {
110 uint64_t Signature = 0;
David Blaikie4dd03f02016-03-26 20:32:14 +0000111 const char *Name = "";
112 const char *DWOName = "";
David Blaikiece7c6cf2016-03-24 22:17:08 +0000113};
114
David Blaikie4940f872016-05-17 00:07:10 +0000115static Expected<const char *>
Greg Clayton6c273762016-10-27 16:32:04 +0000116getIndexedString(dwarf::Form Form, DataExtractor InfoData,
117 uint32_t &InfoOffset, StringRef StrOffsets, StringRef Str) {
David Blaikie60fbd3b2016-04-05 20:16:38 +0000118 if (Form == dwarf::DW_FORM_string)
119 return InfoData.getCStr(&InfoOffset);
David Blaikie4940f872016-05-17 00:07:10 +0000120 if (Form != dwarf::DW_FORM_GNU_str_index)
121 return make_error<DWPError>(
122 "string field encoded without DW_FORM_string or DW_FORM_GNU_str_index");
David Blaikie60fbd3b2016-04-05 20:16:38 +0000123 auto StrIndex = InfoData.getULEB128(&InfoOffset);
David Blaikie4dd03f02016-03-26 20:32:14 +0000124 DataExtractor StrOffsetsData(StrOffsets, true, 0);
125 uint32_t StrOffsetsOffset = 4 * StrIndex;
126 uint32_t StrOffset = StrOffsetsData.getU32(&StrOffsetsOffset);
127 DataExtractor StrData(Str, true, 0);
128 return StrData.getCStr(&StrOffset);
129}
130
David Blaikie7bb62ef2016-05-16 23:26:29 +0000131static Expected<CompileUnitIdentifiers> getCUIdentifiers(StringRef Abbrev,
132 StringRef Info,
133 StringRef StrOffsets,
134 StringRef Str) {
David Blaikief1958da2016-02-26 07:30:15 +0000135 uint32_t Offset = 0;
136 DataExtractor InfoData(Info, true, 0);
Greg Clayton82f12b12016-11-11 16:21:37 +0000137 dwarf::DwarfFormat Format = dwarf::DwarfFormat::DWARF32;
138 uint64_t Length = InfoData.getU32(&Offset);
139 // If the length is 0xffffffff, then this indictes that this is a DWARF 64
140 // stream and the length is actually encoded into a 64 bit value that follows.
141 if (Length == 0xffffffffU) {
142 Format = dwarf::DwarfFormat::DWARF64;
143 Length = InfoData.getU64(&Offset);
144 }
David Blaikief1958da2016-02-26 07:30:15 +0000145 uint16_t Version = InfoData.getU16(&Offset);
146 InfoData.getU32(&Offset); // Abbrev offset (should be zero)
147 uint8_t AddrSize = InfoData.getU8(&Offset);
148
149 uint32_t AbbrCode = InfoData.getULEB128(&Offset);
150
151 DataExtractor AbbrevData(Abbrev, true, 0);
152 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
Greg Clayton6c273762016-10-27 16:32:04 +0000153 auto Tag = static_cast<dwarf::Tag>(AbbrevData.getULEB128(&AbbrevOffset));
David Blaikie7bb62ef2016-05-16 23:26:29 +0000154 if (Tag != dwarf::DW_TAG_compile_unit)
155 return make_error<DWPError>("top level DIE is not a compile unit");
David Blaikief1958da2016-02-26 07:30:15 +0000156 // DW_CHILDREN
157 AbbrevData.getU8(&AbbrevOffset);
158 uint32_t Name;
Greg Clayton6c273762016-10-27 16:32:04 +0000159 dwarf::Form Form;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000160 CompileUnitIdentifiers ID;
David Blaikief1958da2016-02-26 07:30:15 +0000161 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |
Greg Clayton6c273762016-10-27 16:32:04 +0000162 (Form = static_cast<dwarf::Form>(AbbrevData.getULEB128(&AbbrevOffset))) &&
David Blaikiece7c6cf2016-03-24 22:17:08 +0000163 (Name != 0 || Form != 0)) {
164 switch (Name) {
165 case dwarf::DW_AT_name: {
David Blaikie4940f872016-05-17 00:07:10 +0000166 Expected<const char *> EName =
167 getIndexedString(Form, InfoData, Offset, StrOffsets, Str);
168 if (!EName)
169 return EName.takeError();
170 ID.Name = *EName;
David Blaikie4dd03f02016-03-26 20:32:14 +0000171 break;
172 }
173 case dwarf::DW_AT_GNU_dwo_name: {
David Blaikie4940f872016-05-17 00:07:10 +0000174 Expected<const char *> EName =
175 getIndexedString(Form, InfoData, Offset, StrOffsets, Str);
176 if (!EName)
177 return EName.takeError();
178 ID.DWOName = *EName;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000179 break;
180 }
181 case dwarf::DW_AT_GNU_dwo_id:
182 ID.Signature = InfoData.getU64(&Offset);
183 break;
184 default:
Paul Robinson75c068c2017-06-26 18:43:01 +0000185 DWARFFormValue::skipValue(Form, InfoData, &Offset,
186 DWARFFormParams({Version, AddrSize, Format}));
David Blaikiece7c6cf2016-03-24 22:17:08 +0000187 }
David Blaikief1958da2016-02-26 07:30:15 +0000188 }
David Blaikiece7c6cf2016-03-24 22:17:08 +0000189 return ID;
David Blaikiead07b5d2015-12-04 17:20:04 +0000190}
191
David Blaikie24c8ac92015-12-05 03:05:45 +0000192struct UnitIndexEntry {
David Blaikie24c8ac92015-12-05 03:05:45 +0000193 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
David Blaikief1958da2016-02-26 07:30:15 +0000194 std::string Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000195 std::string DWOName;
David Blaikief1958da2016-02-26 07:30:15 +0000196 StringRef DWPName;
David Blaikie24c8ac92015-12-05 03:05:45 +0000197};
198
David Blaikiefd800922016-05-23 16:32:11 +0000199static StringRef getSubsection(StringRef Section,
200 const DWARFUnitIndex::Entry &Entry,
201 DWARFSectionKind Kind) {
David Blaikief1958da2016-02-26 07:30:15 +0000202 const auto *Off = Entry.getOffset(Kind);
203 if (!Off)
204 return StringRef();
205 return Section.substr(Off->Offset, Off->Length);
206}
207
David Blaikie852c02b2016-02-19 21:09:26 +0000208static void addAllTypesFromDWP(
209 MCStreamer &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
210 const DWARFUnitIndex &TUIndex, MCSection *OutputTypes, StringRef Types,
211 const UnitIndexEntry &TUEntry, uint32_t &TypesOffset) {
David Blaikie8bce5a02016-02-17 07:00:24 +0000212 Out.SwitchSection(OutputTypes);
213 for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) {
214 auto *I = E.getOffsets();
215 if (!I)
216 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000217 auto P = TypeIndexEntries.insert(std::make_pair(E.getSignature(), TUEntry));
218 if (!P.second)
David Blaikie8bce5a02016-02-17 07:00:24 +0000219 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000220 auto &Entry = P.first->second;
David Blaikie8bce5a02016-02-17 07:00:24 +0000221 // Zero out the debug_info contribution
222 Entry.Contributions[0] = {};
223 for (auto Kind : TUIndex.getColumnKinds()) {
224 auto &C = Entry.Contributions[Kind - DW_SECT_INFO];
225 C.Offset += I->Offset;
226 C.Length = I->Length;
227 ++I;
228 }
229 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
230 Out.EmitBytes(Types.substr(
231 C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset,
232 C.Length));
233 C.Offset = TypesOffset;
234 TypesOffset += C.Length;
David Blaikie8bce5a02016-02-17 07:00:24 +0000235 }
236}
237
David Blaikiec3826da2015-12-09 21:02:33 +0000238static void addAllTypes(MCStreamer &Out,
David Blaikie852c02b2016-02-19 21:09:26 +0000239 MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
David Blaikie62be5ae2016-04-05 20:26:50 +0000240 MCSection *OutputTypes,
241 const std::vector<StringRef> &TypesSections,
David Blaikiec3826da2015-12-09 21:02:33 +0000242 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) {
David Blaikie62be5ae2016-04-05 20:26:50 +0000243 for (StringRef Types : TypesSections) {
244 Out.SwitchSection(OutputTypes);
245 uint32_t Offset = 0;
246 DataExtractor Data(Types, true, 0);
247 while (Data.isValidOffset(Offset)) {
248 UnitIndexEntry Entry = CUEntry;
249 // Zero out the debug_info contribution
250 Entry.Contributions[0] = {};
251 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
252 C.Offset = TypesOffset;
253 auto PrevOffset = Offset;
254 // Length of the unit, including the 4 byte length field.
255 C.Length = Data.getU32(&Offset) + 4;
David Blaikiec3826da2015-12-09 21:02:33 +0000256
David Blaikie62be5ae2016-04-05 20:26:50 +0000257 Data.getU16(&Offset); // Version
258 Data.getU32(&Offset); // Abbrev offset
259 Data.getU8(&Offset); // Address size
260 auto Signature = Data.getU64(&Offset);
261 Offset = PrevOffset + C.Length;
David Blaikie24c8ac92015-12-05 03:05:45 +0000262
David Blaikie62be5ae2016-04-05 20:26:50 +0000263 auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry));
264 if (!P.second)
265 continue;
David Blaikief5cb6272015-12-14 07:42:00 +0000266
David Blaikie62be5ae2016-04-05 20:26:50 +0000267 Out.EmitBytes(Types.substr(PrevOffset, C.Length));
268 TypesOffset += C.Length;
269 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000270 }
271}
272
273static void
274writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,
David Blaikie852c02b2016-02-19 21:09:26 +0000275 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries,
David Blaikie24c8ac92015-12-05 03:05:45 +0000276 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) {
277 for (const auto &E : IndexEntries)
David Blaikie852c02b2016-02-19 21:09:26 +0000278 for (size_t i = 0; i != array_lengthof(E.second.Contributions); ++i)
David Blaikie24c8ac92015-12-05 03:05:45 +0000279 if (ContributionOffsets[i])
David Blaikie852c02b2016-02-19 21:09:26 +0000280 Out.EmitIntValue(E.second.Contributions[i].*Field, 4);
David Blaikie24c8ac92015-12-05 03:05:45 +0000281}
282
David Blaikie852c02b2016-02-19 21:09:26 +0000283static void
284writeIndex(MCStreamer &Out, MCSection *Section,
285 ArrayRef<unsigned> ContributionOffsets,
286 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries) {
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000287 if (IndexEntries.empty())
288 return;
289
David Blaikie24c8ac92015-12-05 03:05:45 +0000290 unsigned Columns = 0;
291 for (auto &C : ContributionOffsets)
292 if (C)
293 ++Columns;
294
295 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
296 uint64_t Mask = Buckets.size() - 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000297 size_t i = 0;
298 for (const auto &P : IndexEntries) {
299 auto S = P.first;
David Blaikie24c8ac92015-12-05 03:05:45 +0000300 auto H = S & Mask;
David Blaikie9b492562016-04-05 17:51:40 +0000301 auto HP = ((S >> 32) & Mask) | 1;
David Blaikiec3826da2015-12-09 21:02:33 +0000302 while (Buckets[H]) {
David Blaikie852c02b2016-02-19 21:09:26 +0000303 assert(S != IndexEntries.begin()[Buckets[H] - 1].first &&
David Blaikie74f5b282016-02-19 01:51:44 +0000304 "Duplicate unit");
David Blaikie9b492562016-04-05 17:51:40 +0000305 H = (H + HP) & Mask;
David Blaikiec3826da2015-12-09 21:02:33 +0000306 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000307 Buckets[H] = i + 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000308 ++i;
David Blaikie24c8ac92015-12-05 03:05:45 +0000309 }
310
311 Out.SwitchSection(Section);
312 Out.EmitIntValue(2, 4); // Version
313 Out.EmitIntValue(Columns, 4); // Columns
314 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
David Blaikie2ed678c2015-12-05 03:06:30 +0000315 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets
David Blaikie24c8ac92015-12-05 03:05:45 +0000316
317 // Write the signatures.
318 for (const auto &I : Buckets)
David Blaikie852c02b2016-02-19 21:09:26 +0000319 Out.EmitIntValue(I ? IndexEntries.begin()[I - 1].first : 0, 8);
David Blaikie24c8ac92015-12-05 03:05:45 +0000320
321 // Write the indexes.
322 for (const auto &I : Buckets)
323 Out.EmitIntValue(I, 4);
324
325 // Write the column headers (which sections will appear in the table)
326 for (size_t i = 0; i != ContributionOffsets.size(); ++i)
327 if (ContributionOffsets[i])
328 Out.EmitIntValue(i + DW_SECT_INFO, 4);
329
330 // Write the offsets.
331 writeIndexTable(Out, ContributionOffsets, IndexEntries,
332 &DWARFUnitIndex::Entry::SectionContribution::Offset);
333
334 // Write the lengths.
335 writeIndexTable(Out, ContributionOffsets, IndexEntries,
336 &DWARFUnitIndex::Entry::SectionContribution::Length);
337}
David Blaikie74f5b282016-02-19 01:51:44 +0000338
David Blaikied1f7ab32016-05-16 20:42:27 +0000339std::string buildDWODescription(StringRef Name, StringRef DWPName, StringRef DWOName) {
340 std::string Text = "\'";
341 Text += Name;
342 Text += '\'';
David Blaikie4dd03f02016-03-26 20:32:14 +0000343 if (!DWPName.empty()) {
David Blaikied1f7ab32016-05-16 20:42:27 +0000344 Text += " (from ";
345 if (!DWOName.empty()) {
346 Text += '\'';
347 Text += DWOName;
348 Text += "' in ";
349 }
350 Text += '\'';
351 Text += DWPName;
352 Text += "')";
David Blaikie4dd03f02016-03-26 20:32:14 +0000353 }
David Blaikied1f7ab32016-05-16 20:42:27 +0000354 return Text;
355}
356
George Rimar8f5976e2017-01-13 15:58:55 +0000357static Error createError(StringRef Name, Error E) {
358 return make_error<DWPError>(
359 ("failure while decompressing compressed section: '" + Name + "', " +
360 llvm::toString(std::move(E)))
361 .str());
362}
363
364static Error
365handleCompressedSection(std::deque<SmallString<32>> &UncompressedSections,
366 StringRef &Name, StringRef &Contents) {
367 if (!Decompressor::isGnuStyle(Name))
Mehdi Amini41af4302016-11-11 04:28:40 +0000368 return Error::success();
George Rimar8f5976e2017-01-13 15:58:55 +0000369
370 Expected<Decompressor> Dec =
371 Decompressor::create(Name, Contents, false /*IsLE*/, false /*Is64Bit*/);
372 if (!Dec)
373 return createError(Name, Dec.takeError());
374
David Blaikie05e0d2b2016-05-23 21:58:58 +0000375 UncompressedSections.emplace_back();
George Rimarf98b9ac2017-05-18 08:00:01 +0000376 if (Error E = Dec->resizeAndDecompress(UncompressedSections.back()))
George Rimar8f5976e2017-01-13 15:58:55 +0000377 return createError(Name, std::move(E));
378
379 Name = Name.substr(2); // Drop ".z"
David Blaikie05e0d2b2016-05-23 21:58:58 +0000380 Contents = UncompressedSections.back();
Mehdi Amini41af4302016-11-11 04:28:40 +0000381 return Error::success();
David Blaikie05e0d2b2016-05-23 21:58:58 +0000382}
David Blaikied9517cb2016-05-23 22:21:10 +0000383
384static Error handleSection(
385 const StringMap<std::pair<MCSection *, DWARFSectionKind>> &KnownSections,
386 const MCSection *StrSection, const MCSection *StrOffsetSection,
387 const MCSection *TypesSection, const MCSection *CUIndexSection,
388 const MCSection *TUIndexSection, const SectionRef &Section, MCStreamer &Out,
David Blaikie1fc3e6b2016-05-25 23:37:06 +0000389 std::deque<SmallString<32>> &UncompressedSections,
David Blaikied9517cb2016-05-23 22:21:10 +0000390 uint32_t (&ContributionOffsets)[8], UnitIndexEntry &CurEntry,
391 StringRef &CurStrSection, StringRef &CurStrOffsetSection,
392 std::vector<StringRef> &CurTypesSection, StringRef &InfoSection,
393 StringRef &AbbrevSection, StringRef &CurCUIndexSection,
394 StringRef &CurTUIndexSection) {
395 if (Section.isBSS())
Mehdi Amini41af4302016-11-11 04:28:40 +0000396 return Error::success();
David Blaikied9517cb2016-05-23 22:21:10 +0000397
398 if (Section.isVirtual())
Mehdi Amini41af4302016-11-11 04:28:40 +0000399 return Error::success();
David Blaikied9517cb2016-05-23 22:21:10 +0000400
401 StringRef Name;
402 if (std::error_code Err = Section.getName(Name))
403 return errorCodeToError(Err);
404
David Blaikied9517cb2016-05-23 22:21:10 +0000405 StringRef Contents;
406 if (auto Err = Section.getContents(Contents))
407 return errorCodeToError(Err);
408
409 if (auto Err = handleCompressedSection(UncompressedSections, Name, Contents))
410 return Err;
411
George Rimar8f5976e2017-01-13 15:58:55 +0000412 Name = Name.substr(Name.find_first_not_of("._"));
413
David Blaikied9517cb2016-05-23 22:21:10 +0000414 auto SectionPair = KnownSections.find(Name);
415 if (SectionPair == KnownSections.end())
Mehdi Amini41af4302016-11-11 04:28:40 +0000416 return Error::success();
David Blaikied9517cb2016-05-23 22:21:10 +0000417
418 if (DWARFSectionKind Kind = SectionPair->second.second) {
419 auto Index = Kind - DW_SECT_INFO;
420 if (Kind != DW_SECT_TYPES) {
421 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
422 ContributionOffsets[Index] +=
423 (CurEntry.Contributions[Index].Length = Contents.size());
424 }
425
426 switch (Kind) {
427 case DW_SECT_INFO:
428 InfoSection = Contents;
429 break;
430 case DW_SECT_ABBREV:
431 AbbrevSection = Contents;
432 break;
433 default:
434 break;
435 }
436 }
437
438 MCSection *OutSection = SectionPair->second.first;
439 if (OutSection == StrOffsetSection)
440 CurStrOffsetSection = Contents;
441 else if (OutSection == StrSection)
442 CurStrSection = Contents;
443 else if (OutSection == TypesSection)
444 CurTypesSection.push_back(Contents);
445 else if (OutSection == CUIndexSection)
446 CurCUIndexSection = Contents;
447 else if (OutSection == TUIndexSection)
448 CurTUIndexSection = Contents;
449 else {
450 Out.SwitchSection(OutSection);
451 Out.EmitBytes(Contents);
452 }
Mehdi Amini41af4302016-11-11 04:28:40 +0000453 return Error::success();
David Blaikied9517cb2016-05-23 22:21:10 +0000454}
455
456static Error
457buildDuplicateError(const std::pair<uint64_t, UnitIndexEntry> &PrevE,
458 const CompileUnitIdentifiers &ID, StringRef DWPName) {
David Blaikie11825c72016-05-17 19:40:28 +0000459 return make_error<DWPError>(
460 std::string("Duplicate DWO ID (") + utohexstr(PrevE.first) + ") in " +
461 buildDWODescription(PrevE.second.Name, PrevE.second.DWPName,
462 PrevE.second.DWOName) +
463 " and " + buildDWODescription(ID.Name, DWPName, ID.DWOName));
David Blaikie4dd03f02016-03-26 20:32:14 +0000464}
David Blaikied9517cb2016-05-23 22:21:10 +0000465
David Blaikiebc8397c2016-05-12 19:59:54 +0000466static Error write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000467 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
468 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
469 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikiec3826da2015-12-09 21:02:33 +0000470 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
David Blaikie23919372016-02-06 01:15:26 +0000471 MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection();
David Blaikie8bce5a02016-02-17 07:00:24 +0000472 MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000473 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
474 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
475 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
476 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
477 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
478 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
David Blaikieb7020252015-12-04 21:16:42 +0000479 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikie23919372016-02-06 01:15:26 +0000480 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
David Blaikie8bce5a02016-02-17 07:00:24 +0000481 {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}},
482 {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}};
David Blaikieb073cb92015-12-02 06:21:34 +0000483
David Blaikie852c02b2016-02-19 21:09:26 +0000484 MapVector<uint64_t, UnitIndexEntry> IndexEntries;
485 MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000486
David Blaikieb073cb92015-12-02 06:21:34 +0000487 uint32_t ContributionOffsets[8] = {};
488
David Blaikiefd800922016-05-23 16:32:11 +0000489 DWPStringPool Strings(Out, StrSection);
490
491 SmallVector<OwningBinary<object::ObjectFile>, 128> Objects;
492 Objects.reserve(Inputs.size());
493
David Blaikie1fc3e6b2016-05-25 23:37:06 +0000494 std::deque<SmallString<32>> UncompressedSections;
David Blaikie2e9bd892016-05-23 17:35:51 +0000495
David Blaikie242b9482015-12-01 00:48:39 +0000496 for (const auto &Input : Inputs) {
497 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
498 if (!ErrOrObj)
David Blaikiebc8397c2016-05-12 19:59:54 +0000499 return ErrOrObj.takeError();
David Blaikieb073cb92015-12-02 06:21:34 +0000500
David Blaikiefd800922016-05-23 16:32:11 +0000501 auto &Obj = *ErrOrObj->getBinary();
502 Objects.push_back(std::move(*ErrOrObj));
503
David Blaikie23919372016-02-06 01:15:26 +0000504 UnitIndexEntry CurEntry = {};
David Blaikieb073cb92015-12-02 06:21:34 +0000505
David Blaikie98ad82a2015-12-01 18:07:07 +0000506 StringRef CurStrSection;
507 StringRef CurStrOffsetSection;
David Blaikie62be5ae2016-04-05 20:26:50 +0000508 std::vector<StringRef> CurTypesSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000509 StringRef InfoSection;
510 StringRef AbbrevSection;
David Blaikie23919372016-02-06 01:15:26 +0000511 StringRef CurCUIndexSection;
David Blaikie8bce5a02016-02-17 07:00:24 +0000512 StringRef CurTUIndexSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000513
David Blaikied9517cb2016-05-23 22:21:10 +0000514 for (const auto &Section : Obj.sections())
515 if (auto Err = handleSection(
516 KnownSections, StrSection, StrOffsetSection, TypesSection,
517 CUIndexSection, TUIndexSection, Section, Out,
518 UncompressedSections, ContributionOffsets, CurEntry,
519 CurStrSection, CurStrOffsetSection, CurTypesSection, InfoSection,
520 AbbrevSection, CurCUIndexSection, CurTUIndexSection))
David Blaikie05e0d2b2016-05-23 21:58:58 +0000521 return Err;
David Blaikie74f5b282016-02-19 01:51:44 +0000522
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000523 if (InfoSection.empty())
524 continue;
525
David Blaikie478c1a22016-05-23 22:38:06 +0000526 writeStringsAndOffsets(Out, Strings, StrOffsetSection, CurStrSection,
527 CurStrOffsetSection);
David Blaikie23919372016-02-06 01:15:26 +0000528
David Blaikie478c1a22016-05-23 22:38:06 +0000529 if (CurCUIndexSection.empty()) {
David Blaikie7bb62ef2016-05-16 23:26:29 +0000530 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(
David Blaikiece7c6cf2016-03-24 22:17:08 +0000531 AbbrevSection, InfoSection, CurStrOffsetSection, CurStrSection);
David Blaikie7bb62ef2016-05-16 23:26:29 +0000532 if (!EID)
533 return EID.takeError();
534 const auto &ID = *EID;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000535 auto P = IndexEntries.insert(std::make_pair(ID.Signature, CurEntry));
David Blaikied1f7ab32016-05-16 20:42:27 +0000536 if (!P.second)
David Blaikie11825c72016-05-17 19:40:28 +0000537 return buildDuplicateError(*P.first, ID, "");
David Blaikiece7c6cf2016-03-24 22:17:08 +0000538 P.first->second.Name = ID.Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000539 P.first->second.DWOName = ID.DWOName;
David Blaikie23919372016-02-06 01:15:26 +0000540 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection,
541 CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
David Blaikie478c1a22016-05-23 22:38:06 +0000542 continue;
David Blaikie23919372016-02-06 01:15:26 +0000543 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000544
David Blaikie478c1a22016-05-23 22:38:06 +0000545 DWARFUnitIndex CUIndex(DW_SECT_INFO);
546 DataExtractor CUIndexData(CurCUIndexSection, Obj.isLittleEndian(), 0);
547 if (!CUIndex.parse(CUIndexData))
548 return make_error<DWPError>("Failed to parse cu_index");
549
550 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
551 auto *I = E.getOffsets();
552 if (!I)
553 continue;
554 auto P = IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry));
555 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(
556 getSubsection(AbbrevSection, E, DW_SECT_ABBREV),
557 getSubsection(InfoSection, E, DW_SECT_INFO),
558 getSubsection(CurStrOffsetSection, E, DW_SECT_STR_OFFSETS),
559 CurStrSection);
560 if (!EID)
561 return EID.takeError();
562 const auto &ID = *EID;
563 if (!P.second)
564 return buildDuplicateError(*P.first, ID, Input);
565 auto &NewEntry = P.first->second;
566 NewEntry.Name = ID.Name;
567 NewEntry.DWOName = ID.DWOName;
568 NewEntry.DWPName = Input;
569 for (auto Kind : CUIndex.getColumnKinds()) {
570 auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO];
571 C.Offset += I->Offset;
572 C.Length = I->Length;
573 ++I;
574 }
575 }
576
577 if (!CurTypesSection.empty()) {
578 if (CurTypesSection.size() != 1)
579 return make_error<DWPError>("multiple type unit sections in .dwp file");
580 DWARFUnitIndex TUIndex(DW_SECT_TYPES);
581 DataExtractor TUIndexData(CurTUIndexSection, Obj.isLittleEndian(), 0);
582 if (!TUIndex.parse(TUIndexData))
583 return make_error<DWPError>("Failed to parse tu_index");
584 addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection,
585 CurTypesSection.front(), CurEntry,
586 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
587 }
David Blaikie242b9482015-12-01 00:48:39 +0000588 }
David Blaikieb073cb92015-12-02 06:21:34 +0000589
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000590 // Lie about there being no info contributions so the TU index only includes
591 // the type unit contribution
592 ContributionOffsets[0] = 0;
593 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
594 TypeIndexEntries);
David Blaikieb3757c02015-12-02 22:01:56 +0000595
David Blaikie24c8ac92015-12-05 03:05:45 +0000596 // Lie about the type contribution
597 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
598 // Unlie about the info contribution
599 ContributionOffsets[0] = 1;
David Blaikie7c4ffe02015-12-04 21:30:23 +0000600
David Blaikie24c8ac92015-12-05 03:05:45 +0000601 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
602 IndexEntries);
David Blaikieddb27362016-03-01 21:24:04 +0000603
Mehdi Amini41af4302016-11-11 04:28:40 +0000604 return Error::success();
David Blaikie242b9482015-12-01 00:48:39 +0000605}
606
David Blaikie17ab78e2016-05-17 23:37:44 +0000607static int error(const Twine &Error, const Twine &Context) {
608 errs() << Twine("while processing ") + Context + ":\n";
609 errs() << Twine("error: ") + Error + "\n";
610 return 1;
611}
612
David Blaikie2ed678c2015-12-05 03:06:30 +0000613int main(int argc, char **argv) {
David Blaikie242b9482015-12-01 00:48:39 +0000614
615 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
616
617 llvm::InitializeAllTargetInfos();
618 llvm::InitializeAllTargetMCs();
619 llvm::InitializeAllTargets();
620 llvm::InitializeAllAsmPrinters();
621
622 std::string ErrorStr;
623 StringRef Context = "dwarf streamer init";
624
625 Triple TheTriple("x86_64-linux-gnu");
626
627 // Get the target.
628 const Target *TheTarget =
629 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
630 if (!TheTarget)
631 return error(ErrorStr, Context);
632 std::string TripleName = TheTriple.getTriple();
633
634 // Create all the MC Objects.
635 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
636 if (!MRI)
637 return error(Twine("no register info for target ") + TripleName, Context);
638
639 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
640 if (!MAI)
641 return error("no asm info for target " + TripleName, Context);
642
643 MCObjectFileInfo MOFI;
644 MCContext MC(MAI.get(), MRI.get(), &MOFI);
Rafael Espindola9f929952017-08-02 20:32:26 +0000645 MOFI.InitMCObjectFileInfo(TheTriple, /*PIC*/ false, MC);
David Blaikie242b9482015-12-01 00:48:39 +0000646
Joel Jones373d7d32016-07-25 17:18:28 +0000647 MCTargetOptions Options;
648 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "", Options);
David Blaikie242b9482015-12-01 00:48:39 +0000649 if (!MAB)
650 return error("no asm backend for target " + TripleName, Context);
651
652 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
653 if (!MII)
654 return error("no instr info info for target " + TripleName, Context);
655
656 std::unique_ptr<MCSubtargetInfo> MSTI(
657 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
658 if (!MSTI)
659 return error("no subtarget info for target " + TripleName, Context);
660
661 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
662 if (!MCE)
663 return error("no code emitter for target " + TripleName, Context);
664
665 // Create the output file.
666 std::error_code EC;
667 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
668 if (EC)
669 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
670
David Majnemer03e2cc32015-12-21 22:09:27 +0000671 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
David Blaikie242b9482015-12-01 00:48:39 +0000672 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
David Majnemer03e2cc32015-12-21 22:09:27 +0000673 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, MCOptions.MCRelaxAll,
674 MCOptions.MCIncrementalLinkerCompatible,
David Blaikie242b9482015-12-01 00:48:39 +0000675 /*DWARFMustBeAtTheEnd*/ false));
676 if (!MS)
677 return error("no object streamer for target " + TripleName, Context);
678
David Blaikiebc8397c2016-05-12 19:59:54 +0000679 if (auto Err = write(*MS, InputFiles)) {
680 logAllUnhandledErrors(std::move(Err), errs(), "error: ");
681 return 1;
682 }
David Blaikieddb27362016-03-01 21:24:04 +0000683
684 MS->Finish();
David Blaikiedf055252015-12-01 00:48:34 +0000685}