blob: aab3f88d257bc1bf6ddaf916ee43abdea80b913b [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"
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +000020#include "llvm/DebugInfo/DWARF/DWARFContext.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000021#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
22#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
David Blaikie242b9482015-12-01 00:48:39 +000023#include "llvm/MC/MCAsmInfo.h"
24#include "llvm/MC/MCContext.h"
25#include "llvm/MC/MCInstrInfo.h"
26#include "llvm/MC/MCObjectFileInfo.h"
27#include "llvm/MC/MCRegisterInfo.h"
28#include "llvm/MC/MCSectionELF.h"
29#include "llvm/MC/MCStreamer.h"
David Majnemer03e2cc32015-12-21 22:09:27 +000030#include "llvm/MC/MCTargetOptionsCommandFlags.h"
George Rimar8f5976e2017-01-13 15:58:55 +000031#include "llvm/Object/Decompressor.h"
David Blaikie242b9482015-12-01 00:48:39 +000032#include "llvm/Object/ObjectFile.h"
David Blaikie74f5b282016-02-19 01:51:44 +000033#include "llvm/Support/Compression.h"
David Blaikie98ad82a2015-12-01 18:07:07 +000034#include "llvm/Support/DataExtractor.h"
David Blaikiefd800922016-05-23 16:32:11 +000035#include "llvm/Support/Error.h"
David Blaikie242b9482015-12-01 00:48:39 +000036#include "llvm/Support/FileSystem.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000037#include "llvm/Support/MathExtras.h"
David Blaikie242b9482015-12-01 00:48:39 +000038#include "llvm/Support/MemoryBuffer.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000039#include "llvm/Support/Options.h"
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +000040#include "llvm/Support/Path.h"
David Blaikie242b9482015-12-01 00:48:39 +000041#include "llvm/Support/TargetRegistry.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000042#include "llvm/Support/TargetSelect.h"
David Blaikie242b9482015-12-01 00:48:39 +000043#include "llvm/Support/raw_ostream.h"
44#include "llvm/Target/TargetMachine.h"
David Blaikie1fc3e6b2016-05-25 23:37:06 +000045#include <deque>
David Blaikief1958da2016-02-26 07:30:15 +000046#include <iostream>
David Blaikie2ed678c2015-12-05 03:06:30 +000047#include <memory>
David Blaikie242b9482015-12-01 00:48:39 +000048
49using namespace llvm;
David Blaikie98ad82a2015-12-01 18:07:07 +000050using namespace llvm::object;
David Blaikie242b9482015-12-01 00:48:39 +000051using namespace cl;
52
53OptionCategory DwpCategory("Specific Options");
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +000054static list<std::string> InputFiles(Positional, ZeroOrMore,
David Blaikie242b9482015-12-01 00:48:39 +000055 desc("<input files>"), cat(DwpCategory));
56
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +000057static list<std::string> ExecFilenames(
58 "e", ZeroOrMore,
59 desc("Specify the executable/library files to get the list of *.dwo from"),
60 value_desc("filename"), cat(DwpCategory));
61
David Blaikie2ed678c2015-12-05 03:06:30 +000062static opt<std::string> OutputFilename(Required, "o",
63 desc("Specify the output file."),
64 value_desc("filename"),
65 cat(DwpCategory));
David Blaikie242b9482015-12-01 00:48:39 +000066
David Blaikiefd800922016-05-23 16:32:11 +000067static void writeStringsAndOffsets(MCStreamer &Out, DWPStringPool &Strings,
68 MCSection *StrOffsetSection,
69 StringRef CurStrSection,
70 StringRef CurStrOffsetSection) {
David Blaikie98ad82a2015-12-01 18:07:07 +000071 // Could possibly produce an error or warning if one of these was non-null but
72 // the other was null.
73 if (CurStrSection.empty() || CurStrOffsetSection.empty())
David Blaikiebc619cd2016-05-17 23:44:13 +000074 return;
David Blaikie98ad82a2015-12-01 18:07:07 +000075
76 DenseMap<uint32_t, uint32_t> OffsetRemapping;
77
78 DataExtractor Data(CurStrSection, true, 0);
79 uint32_t LocalOffset = 0;
80 uint32_t PrevOffset = 0;
81 while (const char *s = Data.getCStr(&LocalOffset)) {
David Blaikiefd800922016-05-23 16:32:11 +000082 OffsetRemapping[PrevOffset] =
83 Strings.getOffset(s, LocalOffset - PrevOffset);
David Blaikie98ad82a2015-12-01 18:07:07 +000084 PrevOffset = LocalOffset;
85 }
86
87 Data = DataExtractor(CurStrOffsetSection, true, 0);
88
89 Out.SwitchSection(StrOffsetSection);
90
91 uint32_t Offset = 0;
92 uint64_t Size = CurStrOffsetSection.size();
93 while (Offset < Size) {
94 auto OldOffset = Data.getU32(&Offset);
95 auto NewOffset = OffsetRemapping[OldOffset];
96 Out.EmitIntValue(NewOffset, 4);
97 }
David Blaikie242b9482015-12-01 00:48:39 +000098}
99
David Blaikiead07b5d2015-12-04 17:20:04 +0000100static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
101 uint64_t CurCode;
102 uint32_t Offset = 0;
103 DataExtractor AbbrevData(Abbrev, true, 0);
104 while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) {
105 // Tag
106 AbbrevData.getULEB128(&Offset);
107 // DW_CHILDREN
108 AbbrevData.getU8(&Offset);
109 // Attributes
110 while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset))
111 ;
112 }
113 return Offset;
114}
115
David Blaikiece7c6cf2016-03-24 22:17:08 +0000116struct CompileUnitIdentifiers {
117 uint64_t Signature = 0;
David Blaikie4dd03f02016-03-26 20:32:14 +0000118 const char *Name = "";
119 const char *DWOName = "";
David Blaikiece7c6cf2016-03-24 22:17:08 +0000120};
121
David Blaikie4940f872016-05-17 00:07:10 +0000122static Expected<const char *>
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +0000123getIndexedString(dwarf::Form Form, DataExtractor InfoData,
Greg Clayton6c273762016-10-27 16:32:04 +0000124 uint32_t &InfoOffset, StringRef StrOffsets, StringRef Str) {
David Blaikie60fbd3b2016-04-05 20:16:38 +0000125 if (Form == dwarf::DW_FORM_string)
126 return InfoData.getCStr(&InfoOffset);
David Blaikie4940f872016-05-17 00:07:10 +0000127 if (Form != dwarf::DW_FORM_GNU_str_index)
128 return make_error<DWPError>(
129 "string field encoded without DW_FORM_string or DW_FORM_GNU_str_index");
David Blaikie60fbd3b2016-04-05 20:16:38 +0000130 auto StrIndex = InfoData.getULEB128(&InfoOffset);
David Blaikie4dd03f02016-03-26 20:32:14 +0000131 DataExtractor StrOffsetsData(StrOffsets, true, 0);
132 uint32_t StrOffsetsOffset = 4 * StrIndex;
133 uint32_t StrOffset = StrOffsetsData.getU32(&StrOffsetsOffset);
134 DataExtractor StrData(Str, true, 0);
135 return StrData.getCStr(&StrOffset);
136}
137
David Blaikie7bb62ef2016-05-16 23:26:29 +0000138static Expected<CompileUnitIdentifiers> getCUIdentifiers(StringRef Abbrev,
139 StringRef Info,
140 StringRef StrOffsets,
141 StringRef Str) {
David Blaikief1958da2016-02-26 07:30:15 +0000142 uint32_t Offset = 0;
143 DataExtractor InfoData(Info, true, 0);
Greg Clayton82f12b12016-11-11 16:21:37 +0000144 dwarf::DwarfFormat Format = dwarf::DwarfFormat::DWARF32;
145 uint64_t Length = InfoData.getU32(&Offset);
146 // If the length is 0xffffffff, then this indictes that this is a DWARF 64
147 // stream and the length is actually encoded into a 64 bit value that follows.
148 if (Length == 0xffffffffU) {
149 Format = dwarf::DwarfFormat::DWARF64;
150 Length = InfoData.getU64(&Offset);
151 }
David Blaikief1958da2016-02-26 07:30:15 +0000152 uint16_t Version = InfoData.getU16(&Offset);
153 InfoData.getU32(&Offset); // Abbrev offset (should be zero)
154 uint8_t AddrSize = InfoData.getU8(&Offset);
155
156 uint32_t AbbrCode = InfoData.getULEB128(&Offset);
157
158 DataExtractor AbbrevData(Abbrev, true, 0);
159 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
Greg Clayton6c273762016-10-27 16:32:04 +0000160 auto Tag = static_cast<dwarf::Tag>(AbbrevData.getULEB128(&AbbrevOffset));
David Blaikie7bb62ef2016-05-16 23:26:29 +0000161 if (Tag != dwarf::DW_TAG_compile_unit)
162 return make_error<DWPError>("top level DIE is not a compile unit");
David Blaikief1958da2016-02-26 07:30:15 +0000163 // DW_CHILDREN
164 AbbrevData.getU8(&AbbrevOffset);
165 uint32_t Name;
Greg Clayton6c273762016-10-27 16:32:04 +0000166 dwarf::Form Form;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000167 CompileUnitIdentifiers ID;
David Blaikief1958da2016-02-26 07:30:15 +0000168 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |
Greg Clayton6c273762016-10-27 16:32:04 +0000169 (Form = static_cast<dwarf::Form>(AbbrevData.getULEB128(&AbbrevOffset))) &&
David Blaikiece7c6cf2016-03-24 22:17:08 +0000170 (Name != 0 || Form != 0)) {
171 switch (Name) {
172 case dwarf::DW_AT_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.Name = *EName;
David Blaikie4dd03f02016-03-26 20:32:14 +0000178 break;
179 }
180 case dwarf::DW_AT_GNU_dwo_name: {
David Blaikie4940f872016-05-17 00:07:10 +0000181 Expected<const char *> EName =
182 getIndexedString(Form, InfoData, Offset, StrOffsets, Str);
183 if (!EName)
184 return EName.takeError();
185 ID.DWOName = *EName;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000186 break;
187 }
188 case dwarf::DW_AT_GNU_dwo_id:
189 ID.Signature = InfoData.getU64(&Offset);
190 break;
191 default:
Paul Robinson75c068c2017-06-26 18:43:01 +0000192 DWARFFormValue::skipValue(Form, InfoData, &Offset,
193 DWARFFormParams({Version, AddrSize, Format}));
David Blaikiece7c6cf2016-03-24 22:17:08 +0000194 }
David Blaikief1958da2016-02-26 07:30:15 +0000195 }
David Blaikiece7c6cf2016-03-24 22:17:08 +0000196 return ID;
David Blaikiead07b5d2015-12-04 17:20:04 +0000197}
198
David Blaikie24c8ac92015-12-05 03:05:45 +0000199struct UnitIndexEntry {
David Blaikie24c8ac92015-12-05 03:05:45 +0000200 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
David Blaikief1958da2016-02-26 07:30:15 +0000201 std::string Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000202 std::string DWOName;
David Blaikief1958da2016-02-26 07:30:15 +0000203 StringRef DWPName;
David Blaikie24c8ac92015-12-05 03:05:45 +0000204};
205
David Blaikiefd800922016-05-23 16:32:11 +0000206static StringRef getSubsection(StringRef Section,
207 const DWARFUnitIndex::Entry &Entry,
208 DWARFSectionKind Kind) {
David Blaikief1958da2016-02-26 07:30:15 +0000209 const auto *Off = Entry.getOffset(Kind);
210 if (!Off)
211 return StringRef();
212 return Section.substr(Off->Offset, Off->Length);
213}
214
David Blaikie852c02b2016-02-19 21:09:26 +0000215static void addAllTypesFromDWP(
216 MCStreamer &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
217 const DWARFUnitIndex &TUIndex, MCSection *OutputTypes, StringRef Types,
218 const UnitIndexEntry &TUEntry, uint32_t &TypesOffset) {
David Blaikie8bce5a02016-02-17 07:00:24 +0000219 Out.SwitchSection(OutputTypes);
220 for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) {
221 auto *I = E.getOffsets();
222 if (!I)
223 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000224 auto P = TypeIndexEntries.insert(std::make_pair(E.getSignature(), TUEntry));
225 if (!P.second)
David Blaikie8bce5a02016-02-17 07:00:24 +0000226 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000227 auto &Entry = P.first->second;
David Blaikie8bce5a02016-02-17 07:00:24 +0000228 // Zero out the debug_info contribution
229 Entry.Contributions[0] = {};
230 for (auto Kind : TUIndex.getColumnKinds()) {
231 auto &C = Entry.Contributions[Kind - DW_SECT_INFO];
232 C.Offset += I->Offset;
233 C.Length = I->Length;
234 ++I;
235 }
236 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
237 Out.EmitBytes(Types.substr(
238 C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset,
239 C.Length));
240 C.Offset = TypesOffset;
241 TypesOffset += C.Length;
David Blaikie8bce5a02016-02-17 07:00:24 +0000242 }
243}
244
David Blaikiec3826da2015-12-09 21:02:33 +0000245static void addAllTypes(MCStreamer &Out,
David Blaikie852c02b2016-02-19 21:09:26 +0000246 MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
David Blaikie62be5ae2016-04-05 20:26:50 +0000247 MCSection *OutputTypes,
248 const std::vector<StringRef> &TypesSections,
David Blaikiec3826da2015-12-09 21:02:33 +0000249 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) {
David Blaikie62be5ae2016-04-05 20:26:50 +0000250 for (StringRef Types : TypesSections) {
251 Out.SwitchSection(OutputTypes);
252 uint32_t Offset = 0;
253 DataExtractor Data(Types, true, 0);
254 while (Data.isValidOffset(Offset)) {
255 UnitIndexEntry Entry = CUEntry;
256 // Zero out the debug_info contribution
257 Entry.Contributions[0] = {};
258 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
259 C.Offset = TypesOffset;
260 auto PrevOffset = Offset;
261 // Length of the unit, including the 4 byte length field.
262 C.Length = Data.getU32(&Offset) + 4;
David Blaikiec3826da2015-12-09 21:02:33 +0000263
David Blaikie62be5ae2016-04-05 20:26:50 +0000264 Data.getU16(&Offset); // Version
265 Data.getU32(&Offset); // Abbrev offset
266 Data.getU8(&Offset); // Address size
267 auto Signature = Data.getU64(&Offset);
268 Offset = PrevOffset + C.Length;
David Blaikie24c8ac92015-12-05 03:05:45 +0000269
David Blaikie62be5ae2016-04-05 20:26:50 +0000270 auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry));
271 if (!P.second)
272 continue;
David Blaikief5cb6272015-12-14 07:42:00 +0000273
David Blaikie62be5ae2016-04-05 20:26:50 +0000274 Out.EmitBytes(Types.substr(PrevOffset, C.Length));
275 TypesOffset += C.Length;
276 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000277 }
278}
279
280static void
281writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,
David Blaikie852c02b2016-02-19 21:09:26 +0000282 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries,
David Blaikie24c8ac92015-12-05 03:05:45 +0000283 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) {
284 for (const auto &E : IndexEntries)
David Blaikie852c02b2016-02-19 21:09:26 +0000285 for (size_t i = 0; i != array_lengthof(E.second.Contributions); ++i)
David Blaikie24c8ac92015-12-05 03:05:45 +0000286 if (ContributionOffsets[i])
David Blaikie852c02b2016-02-19 21:09:26 +0000287 Out.EmitIntValue(E.second.Contributions[i].*Field, 4);
David Blaikie24c8ac92015-12-05 03:05:45 +0000288}
289
David Blaikie852c02b2016-02-19 21:09:26 +0000290static void
291writeIndex(MCStreamer &Out, MCSection *Section,
292 ArrayRef<unsigned> ContributionOffsets,
293 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries) {
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000294 if (IndexEntries.empty())
295 return;
296
David Blaikie24c8ac92015-12-05 03:05:45 +0000297 unsigned Columns = 0;
298 for (auto &C : ContributionOffsets)
299 if (C)
300 ++Columns;
301
302 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
303 uint64_t Mask = Buckets.size() - 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000304 size_t i = 0;
305 for (const auto &P : IndexEntries) {
306 auto S = P.first;
David Blaikie24c8ac92015-12-05 03:05:45 +0000307 auto H = S & Mask;
David Blaikie9b492562016-04-05 17:51:40 +0000308 auto HP = ((S >> 32) & Mask) | 1;
David Blaikiec3826da2015-12-09 21:02:33 +0000309 while (Buckets[H]) {
David Blaikie852c02b2016-02-19 21:09:26 +0000310 assert(S != IndexEntries.begin()[Buckets[H] - 1].first &&
David Blaikie74f5b282016-02-19 01:51:44 +0000311 "Duplicate unit");
David Blaikie9b492562016-04-05 17:51:40 +0000312 H = (H + HP) & Mask;
David Blaikiec3826da2015-12-09 21:02:33 +0000313 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000314 Buckets[H] = i + 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000315 ++i;
David Blaikie24c8ac92015-12-05 03:05:45 +0000316 }
317
318 Out.SwitchSection(Section);
319 Out.EmitIntValue(2, 4); // Version
320 Out.EmitIntValue(Columns, 4); // Columns
321 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
David Blaikie2ed678c2015-12-05 03:06:30 +0000322 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets
David Blaikie24c8ac92015-12-05 03:05:45 +0000323
324 // Write the signatures.
325 for (const auto &I : Buckets)
David Blaikie852c02b2016-02-19 21:09:26 +0000326 Out.EmitIntValue(I ? IndexEntries.begin()[I - 1].first : 0, 8);
David Blaikie24c8ac92015-12-05 03:05:45 +0000327
328 // Write the indexes.
329 for (const auto &I : Buckets)
330 Out.EmitIntValue(I, 4);
331
332 // Write the column headers (which sections will appear in the table)
333 for (size_t i = 0; i != ContributionOffsets.size(); ++i)
334 if (ContributionOffsets[i])
335 Out.EmitIntValue(i + DW_SECT_INFO, 4);
336
337 // Write the offsets.
338 writeIndexTable(Out, ContributionOffsets, IndexEntries,
339 &DWARFUnitIndex::Entry::SectionContribution::Offset);
340
341 // Write the lengths.
342 writeIndexTable(Out, ContributionOffsets, IndexEntries,
343 &DWARFUnitIndex::Entry::SectionContribution::Length);
344}
David Blaikie74f5b282016-02-19 01:51:44 +0000345
David Blaikied1f7ab32016-05-16 20:42:27 +0000346std::string buildDWODescription(StringRef Name, StringRef DWPName, StringRef DWOName) {
347 std::string Text = "\'";
348 Text += Name;
349 Text += '\'';
David Blaikie4dd03f02016-03-26 20:32:14 +0000350 if (!DWPName.empty()) {
David Blaikied1f7ab32016-05-16 20:42:27 +0000351 Text += " (from ";
352 if (!DWOName.empty()) {
353 Text += '\'';
354 Text += DWOName;
355 Text += "' in ";
356 }
357 Text += '\'';
358 Text += DWPName;
359 Text += "')";
David Blaikie4dd03f02016-03-26 20:32:14 +0000360 }
David Blaikied1f7ab32016-05-16 20:42:27 +0000361 return Text;
362}
363
George Rimar8f5976e2017-01-13 15:58:55 +0000364static Error createError(StringRef Name, Error E) {
365 return make_error<DWPError>(
366 ("failure while decompressing compressed section: '" + Name + "', " +
367 llvm::toString(std::move(E)))
368 .str());
369}
370
371static Error
372handleCompressedSection(std::deque<SmallString<32>> &UncompressedSections,
373 StringRef &Name, StringRef &Contents) {
374 if (!Decompressor::isGnuStyle(Name))
Mehdi Amini41af4302016-11-11 04:28:40 +0000375 return Error::success();
George Rimar8f5976e2017-01-13 15:58:55 +0000376
377 Expected<Decompressor> Dec =
378 Decompressor::create(Name, Contents, false /*IsLE*/, false /*Is64Bit*/);
379 if (!Dec)
380 return createError(Name, Dec.takeError());
381
David Blaikie05e0d2b2016-05-23 21:58:58 +0000382 UncompressedSections.emplace_back();
George Rimarf98b9ac2017-05-18 08:00:01 +0000383 if (Error E = Dec->resizeAndDecompress(UncompressedSections.back()))
George Rimar8f5976e2017-01-13 15:58:55 +0000384 return createError(Name, std::move(E));
385
386 Name = Name.substr(2); // Drop ".z"
David Blaikie05e0d2b2016-05-23 21:58:58 +0000387 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
David Blaikied9517cb2016-05-23 22:21:10 +0000412 StringRef Contents;
413 if (auto Err = Section.getContents(Contents))
414 return errorCodeToError(Err);
415
416 if (auto Err = handleCompressedSection(UncompressedSections, Name, Contents))
417 return Err;
418
George Rimar8f5976e2017-01-13 15:58:55 +0000419 Name = Name.substr(Name.find_first_not_of("._"));
420
David Blaikied9517cb2016-05-23 22:21:10 +0000421 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
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +0000473static Expected<SmallVector<std::string, 16>>
474getDWOFilenames(StringRef ExecFilename) {
475 auto ErrOrObj = object::ObjectFile::createObjectFile(ExecFilename);
476 if (!ErrOrObj)
477 return ErrOrObj.takeError();
478
479 const ObjectFile &Obj = *ErrOrObj.get().getBinary();
480 std::unique_ptr<DWARFContext> DWARFCtx = DWARFContext::create(Obj);
481
482 SmallVector<std::string, 16> DWOPaths;
483 for (const auto &CU : DWARFCtx->compile_units()) {
484 const DWARFDie &Die = CU->getUnitDIE();
485 std::string DWOName = dwarf::toString(
486 Die.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
487 if (DWOName.empty())
488 continue;
489 std::string DWOCompDir =
490 dwarf::toString(Die.find(dwarf::DW_AT_comp_dir), "");
491 if (!DWOCompDir.empty()) {
492 SmallString<16> DWOPath;
493 sys::path::append(DWOPath, DWOCompDir, DWOName);
494 DWOPaths.emplace_back(DWOPath.data(), DWOPath.size());
495 } else {
496 DWOPaths.push_back(std::move(DWOName));
497 }
498 }
499 return std::move(DWOPaths);
500}
501
David Blaikiebc8397c2016-05-12 19:59:54 +0000502static Error write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000503 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
504 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
505 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikiec3826da2015-12-09 21:02:33 +0000506 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
David Blaikie23919372016-02-06 01:15:26 +0000507 MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection();
David Blaikie8bce5a02016-02-17 07:00:24 +0000508 MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000509 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
510 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
511 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
512 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
513 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
514 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
David Blaikieb7020252015-12-04 21:16:42 +0000515 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikie23919372016-02-06 01:15:26 +0000516 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
David Blaikie8bce5a02016-02-17 07:00:24 +0000517 {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}},
518 {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}};
David Blaikieb073cb92015-12-02 06:21:34 +0000519
David Blaikie852c02b2016-02-19 21:09:26 +0000520 MapVector<uint64_t, UnitIndexEntry> IndexEntries;
521 MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000522
David Blaikieb073cb92015-12-02 06:21:34 +0000523 uint32_t ContributionOffsets[8] = {};
524
David Blaikiefd800922016-05-23 16:32:11 +0000525 DWPStringPool Strings(Out, StrSection);
526
527 SmallVector<OwningBinary<object::ObjectFile>, 128> Objects;
528 Objects.reserve(Inputs.size());
529
David Blaikie1fc3e6b2016-05-25 23:37:06 +0000530 std::deque<SmallString<32>> UncompressedSections;
David Blaikie2e9bd892016-05-23 17:35:51 +0000531
David Blaikie242b9482015-12-01 00:48:39 +0000532 for (const auto &Input : Inputs) {
533 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
534 if (!ErrOrObj)
David Blaikiebc8397c2016-05-12 19:59:54 +0000535 return ErrOrObj.takeError();
David Blaikieb073cb92015-12-02 06:21:34 +0000536
David Blaikiefd800922016-05-23 16:32:11 +0000537 auto &Obj = *ErrOrObj->getBinary();
538 Objects.push_back(std::move(*ErrOrObj));
539
David Blaikie23919372016-02-06 01:15:26 +0000540 UnitIndexEntry CurEntry = {};
David Blaikieb073cb92015-12-02 06:21:34 +0000541
David Blaikie98ad82a2015-12-01 18:07:07 +0000542 StringRef CurStrSection;
543 StringRef CurStrOffsetSection;
David Blaikie62be5ae2016-04-05 20:26:50 +0000544 std::vector<StringRef> CurTypesSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000545 StringRef InfoSection;
546 StringRef AbbrevSection;
David Blaikie23919372016-02-06 01:15:26 +0000547 StringRef CurCUIndexSection;
David Blaikie8bce5a02016-02-17 07:00:24 +0000548 StringRef CurTUIndexSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000549
David Blaikied9517cb2016-05-23 22:21:10 +0000550 for (const auto &Section : Obj.sections())
551 if (auto Err = handleSection(
552 KnownSections, StrSection, StrOffsetSection, TypesSection,
553 CUIndexSection, TUIndexSection, Section, Out,
554 UncompressedSections, ContributionOffsets, CurEntry,
555 CurStrSection, CurStrOffsetSection, CurTypesSection, InfoSection,
556 AbbrevSection, CurCUIndexSection, CurTUIndexSection))
David Blaikie05e0d2b2016-05-23 21:58:58 +0000557 return Err;
David Blaikie74f5b282016-02-19 01:51:44 +0000558
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000559 if (InfoSection.empty())
560 continue;
561
David Blaikie478c1a22016-05-23 22:38:06 +0000562 writeStringsAndOffsets(Out, Strings, StrOffsetSection, CurStrSection,
563 CurStrOffsetSection);
David Blaikie23919372016-02-06 01:15:26 +0000564
David Blaikie478c1a22016-05-23 22:38:06 +0000565 if (CurCUIndexSection.empty()) {
David Blaikie7bb62ef2016-05-16 23:26:29 +0000566 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(
David Blaikiece7c6cf2016-03-24 22:17:08 +0000567 AbbrevSection, InfoSection, CurStrOffsetSection, CurStrSection);
David Blaikie7bb62ef2016-05-16 23:26:29 +0000568 if (!EID)
569 return EID.takeError();
570 const auto &ID = *EID;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000571 auto P = IndexEntries.insert(std::make_pair(ID.Signature, CurEntry));
David Blaikied1f7ab32016-05-16 20:42:27 +0000572 if (!P.second)
David Blaikie11825c72016-05-17 19:40:28 +0000573 return buildDuplicateError(*P.first, ID, "");
David Blaikiece7c6cf2016-03-24 22:17:08 +0000574 P.first->second.Name = ID.Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000575 P.first->second.DWOName = ID.DWOName;
David Blaikie23919372016-02-06 01:15:26 +0000576 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection,
577 CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
David Blaikie478c1a22016-05-23 22:38:06 +0000578 continue;
David Blaikie23919372016-02-06 01:15:26 +0000579 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000580
David Blaikie478c1a22016-05-23 22:38:06 +0000581 DWARFUnitIndex CUIndex(DW_SECT_INFO);
582 DataExtractor CUIndexData(CurCUIndexSection, Obj.isLittleEndian(), 0);
583 if (!CUIndex.parse(CUIndexData))
584 return make_error<DWPError>("Failed to parse cu_index");
585
586 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
587 auto *I = E.getOffsets();
588 if (!I)
589 continue;
590 auto P = IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry));
591 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(
592 getSubsection(AbbrevSection, E, DW_SECT_ABBREV),
593 getSubsection(InfoSection, E, DW_SECT_INFO),
594 getSubsection(CurStrOffsetSection, E, DW_SECT_STR_OFFSETS),
595 CurStrSection);
596 if (!EID)
597 return EID.takeError();
598 const auto &ID = *EID;
599 if (!P.second)
600 return buildDuplicateError(*P.first, ID, Input);
601 auto &NewEntry = P.first->second;
602 NewEntry.Name = ID.Name;
603 NewEntry.DWOName = ID.DWOName;
604 NewEntry.DWPName = Input;
605 for (auto Kind : CUIndex.getColumnKinds()) {
606 auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO];
607 C.Offset += I->Offset;
608 C.Length = I->Length;
609 ++I;
610 }
611 }
612
613 if (!CurTypesSection.empty()) {
614 if (CurTypesSection.size() != 1)
615 return make_error<DWPError>("multiple type unit sections in .dwp file");
616 DWARFUnitIndex TUIndex(DW_SECT_TYPES);
617 DataExtractor TUIndexData(CurTUIndexSection, Obj.isLittleEndian(), 0);
618 if (!TUIndex.parse(TUIndexData))
619 return make_error<DWPError>("Failed to parse tu_index");
620 addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection,
621 CurTypesSection.front(), CurEntry,
622 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
623 }
David Blaikie242b9482015-12-01 00:48:39 +0000624 }
David Blaikieb073cb92015-12-02 06:21:34 +0000625
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000626 // Lie about there being no info contributions so the TU index only includes
627 // the type unit contribution
628 ContributionOffsets[0] = 0;
629 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
630 TypeIndexEntries);
David Blaikieb3757c02015-12-02 22:01:56 +0000631
David Blaikie24c8ac92015-12-05 03:05:45 +0000632 // Lie about the type contribution
633 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
634 // Unlie about the info contribution
635 ContributionOffsets[0] = 1;
David Blaikie7c4ffe02015-12-04 21:30:23 +0000636
David Blaikie24c8ac92015-12-05 03:05:45 +0000637 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
638 IndexEntries);
David Blaikieddb27362016-03-01 21:24:04 +0000639
Mehdi Amini41af4302016-11-11 04:28:40 +0000640 return Error::success();
David Blaikie242b9482015-12-01 00:48:39 +0000641}
642
David Blaikie17ab78e2016-05-17 23:37:44 +0000643static int error(const Twine &Error, const Twine &Context) {
644 errs() << Twine("while processing ") + Context + ":\n";
645 errs() << Twine("error: ") + Error + "\n";
646 return 1;
647}
648
David Blaikie2ed678c2015-12-05 03:06:30 +0000649int main(int argc, char **argv) {
David Blaikie242b9482015-12-01 00:48:39 +0000650
651 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
652
653 llvm::InitializeAllTargetInfos();
654 llvm::InitializeAllTargetMCs();
655 llvm::InitializeAllTargets();
656 llvm::InitializeAllAsmPrinters();
657
658 std::string ErrorStr;
659 StringRef Context = "dwarf streamer init";
660
661 Triple TheTriple("x86_64-linux-gnu");
662
663 // Get the target.
664 const Target *TheTarget =
665 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
666 if (!TheTarget)
667 return error(ErrorStr, Context);
668 std::string TripleName = TheTriple.getTriple();
669
670 // Create all the MC Objects.
671 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
672 if (!MRI)
673 return error(Twine("no register info for target ") + TripleName, Context);
674
675 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
676 if (!MAI)
677 return error("no asm info for target " + TripleName, Context);
678
679 MCObjectFileInfo MOFI;
680 MCContext MC(MAI.get(), MRI.get(), &MOFI);
Rafael Espindola9f929952017-08-02 20:32:26 +0000681 MOFI.InitMCObjectFileInfo(TheTriple, /*PIC*/ false, MC);
David Blaikie242b9482015-12-01 00:48:39 +0000682
Joel Jones373d7d32016-07-25 17:18:28 +0000683 MCTargetOptions Options;
684 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "", Options);
David Blaikie242b9482015-12-01 00:48:39 +0000685 if (!MAB)
686 return error("no asm backend for target " + TripleName, Context);
687
688 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
689 if (!MII)
690 return error("no instr info info for target " + TripleName, Context);
691
692 std::unique_ptr<MCSubtargetInfo> MSTI(
693 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
694 if (!MSTI)
695 return error("no subtarget info for target " + TripleName, Context);
696
697 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
698 if (!MCE)
699 return error("no code emitter for target " + TripleName, Context);
700
701 // Create the output file.
702 std::error_code EC;
703 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
704 if (EC)
705 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
706
David Majnemer03e2cc32015-12-21 22:09:27 +0000707 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
David Blaikie242b9482015-12-01 00:48:39 +0000708 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
David Majnemer03e2cc32015-12-21 22:09:27 +0000709 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, MCOptions.MCRelaxAll,
710 MCOptions.MCIncrementalLinkerCompatible,
David Blaikie242b9482015-12-01 00:48:39 +0000711 /*DWARFMustBeAtTheEnd*/ false));
712 if (!MS)
713 return error("no object streamer for target " + TripleName, Context);
714
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +0000715 std::vector<std::string> DWOFilenames = InputFiles;
716 for (const auto &ExecFilename : ExecFilenames) {
717 auto DWOs = getDWOFilenames(ExecFilename);
718 if (!DWOs) {
719 logAllUnhandledErrors(DWOs.takeError(), errs(), "error: ");
720 return 1;
721 }
722 DWOFilenames.insert(DWOFilenames.end(),
723 std::make_move_iterator(DWOs->begin()),
724 std::make_move_iterator(DWOs->end()));
725 }
726
727 if (auto Err = write(*MS, DWOFilenames)) {
David Blaikiebc8397c2016-05-12 19:59:54 +0000728 logAllUnhandledErrors(std::move(Err), errs(), "error: ");
729 return 1;
730 }
David Blaikieddb27362016-03-01 21:24:04 +0000731
732 MS->Finish();
David Blaikiedf055252015-12-01 00:48:34 +0000733}