blob: fd6559f281bbcb6c0b9b23680d4f5392b77bcdf9 [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"
Lang Hames02d33052017-10-11 01:57:21 +000023#include "llvm/MC/MCAsmBackend.h"
David Blaikie242b9482015-12-01 00:48:39 +000024#include "llvm/MC/MCAsmInfo.h"
25#include "llvm/MC/MCContext.h"
26#include "llvm/MC/MCInstrInfo.h"
27#include "llvm/MC/MCObjectFileInfo.h"
28#include "llvm/MC/MCRegisterInfo.h"
29#include "llvm/MC/MCSectionELF.h"
30#include "llvm/MC/MCStreamer.h"
David Majnemer03e2cc32015-12-21 22:09:27 +000031#include "llvm/MC/MCTargetOptionsCommandFlags.h"
George Rimar8f5976e2017-01-13 15:58:55 +000032#include "llvm/Object/Decompressor.h"
David Blaikie242b9482015-12-01 00:48:39 +000033#include "llvm/Object/ObjectFile.h"
David Blaikie74f5b282016-02-19 01:51:44 +000034#include "llvm/Support/Compression.h"
David Blaikie98ad82a2015-12-01 18:07:07 +000035#include "llvm/Support/DataExtractor.h"
David Blaikiefd800922016-05-23 16:32:11 +000036#include "llvm/Support/Error.h"
David Blaikie242b9482015-12-01 00:48:39 +000037#include "llvm/Support/FileSystem.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000038#include "llvm/Support/MathExtras.h"
David Blaikie242b9482015-12-01 00:48:39 +000039#include "llvm/Support/MemoryBuffer.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000040#include "llvm/Support/Options.h"
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +000041#include "llvm/Support/Path.h"
David Blaikie242b9482015-12-01 00:48:39 +000042#include "llvm/Support/TargetRegistry.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000043#include "llvm/Support/TargetSelect.h"
David Blaikie242b9482015-12-01 00:48:39 +000044#include "llvm/Support/raw_ostream.h"
45#include "llvm/Target/TargetMachine.h"
David Blaikie1fc3e6b2016-05-25 23:37:06 +000046#include <deque>
David Blaikief1958da2016-02-26 07:30:15 +000047#include <iostream>
David Blaikie2ed678c2015-12-05 03:06:30 +000048#include <memory>
David Blaikie242b9482015-12-01 00:48:39 +000049
50using namespace llvm;
David Blaikie98ad82a2015-12-01 18:07:07 +000051using namespace llvm::object;
David Blaikie242b9482015-12-01 00:48:39 +000052using namespace cl;
53
54OptionCategory DwpCategory("Specific Options");
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +000055static list<std::string> InputFiles(Positional, ZeroOrMore,
David Blaikie242b9482015-12-01 00:48:39 +000056 desc("<input files>"), cat(DwpCategory));
57
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +000058static list<std::string> ExecFilenames(
59 "e", ZeroOrMore,
60 desc("Specify the executable/library files to get the list of *.dwo from"),
61 value_desc("filename"), cat(DwpCategory));
62
David Blaikie2ed678c2015-12-05 03:06:30 +000063static opt<std::string> OutputFilename(Required, "o",
64 desc("Specify the output file."),
65 value_desc("filename"),
66 cat(DwpCategory));
David Blaikie242b9482015-12-01 00:48:39 +000067
David Blaikiefd800922016-05-23 16:32:11 +000068static void writeStringsAndOffsets(MCStreamer &Out, DWPStringPool &Strings,
69 MCSection *StrOffsetSection,
70 StringRef CurStrSection,
71 StringRef CurStrOffsetSection) {
David Blaikie98ad82a2015-12-01 18:07:07 +000072 // Could possibly produce an error or warning if one of these was non-null but
73 // the other was null.
74 if (CurStrSection.empty() || CurStrOffsetSection.empty())
David Blaikiebc619cd2016-05-17 23:44:13 +000075 return;
David Blaikie98ad82a2015-12-01 18:07:07 +000076
77 DenseMap<uint32_t, uint32_t> OffsetRemapping;
78
79 DataExtractor Data(CurStrSection, true, 0);
80 uint32_t LocalOffset = 0;
81 uint32_t PrevOffset = 0;
82 while (const char *s = Data.getCStr(&LocalOffset)) {
David Blaikiefd800922016-05-23 16:32:11 +000083 OffsetRemapping[PrevOffset] =
84 Strings.getOffset(s, LocalOffset - PrevOffset);
David Blaikie98ad82a2015-12-01 18:07:07 +000085 PrevOffset = LocalOffset;
86 }
87
88 Data = DataExtractor(CurStrOffsetSection, true, 0);
89
90 Out.SwitchSection(StrOffsetSection);
91
92 uint32_t Offset = 0;
93 uint64_t Size = CurStrOffsetSection.size();
94 while (Offset < Size) {
95 auto OldOffset = Data.getU32(&Offset);
96 auto NewOffset = OffsetRemapping[OldOffset];
97 Out.EmitIntValue(NewOffset, 4);
98 }
David Blaikie242b9482015-12-01 00:48:39 +000099}
100
David Blaikiead07b5d2015-12-04 17:20:04 +0000101static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
102 uint64_t CurCode;
103 uint32_t Offset = 0;
104 DataExtractor AbbrevData(Abbrev, true, 0);
105 while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) {
106 // Tag
107 AbbrevData.getULEB128(&Offset);
108 // DW_CHILDREN
109 AbbrevData.getU8(&Offset);
110 // Attributes
111 while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset))
112 ;
113 }
114 return Offset;
115}
116
David Blaikiece7c6cf2016-03-24 22:17:08 +0000117struct CompileUnitIdentifiers {
118 uint64_t Signature = 0;
David Blaikie4dd03f02016-03-26 20:32:14 +0000119 const char *Name = "";
120 const char *DWOName = "";
David Blaikiece7c6cf2016-03-24 22:17:08 +0000121};
122
David Blaikie4940f872016-05-17 00:07:10 +0000123static Expected<const char *>
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +0000124getIndexedString(dwarf::Form Form, DataExtractor InfoData,
Greg Clayton6c273762016-10-27 16:32:04 +0000125 uint32_t &InfoOffset, StringRef StrOffsets, StringRef Str) {
David Blaikie60fbd3b2016-04-05 20:16:38 +0000126 if (Form == dwarf::DW_FORM_string)
127 return InfoData.getCStr(&InfoOffset);
David Blaikie4940f872016-05-17 00:07:10 +0000128 if (Form != dwarf::DW_FORM_GNU_str_index)
129 return make_error<DWPError>(
130 "string field encoded without DW_FORM_string or DW_FORM_GNU_str_index");
David Blaikie60fbd3b2016-04-05 20:16:38 +0000131 auto StrIndex = InfoData.getULEB128(&InfoOffset);
David Blaikie4dd03f02016-03-26 20:32:14 +0000132 DataExtractor StrOffsetsData(StrOffsets, true, 0);
133 uint32_t StrOffsetsOffset = 4 * StrIndex;
134 uint32_t StrOffset = StrOffsetsData.getU32(&StrOffsetsOffset);
135 DataExtractor StrData(Str, true, 0);
136 return StrData.getCStr(&StrOffset);
137}
138
David Blaikie7bb62ef2016-05-16 23:26:29 +0000139static Expected<CompileUnitIdentifiers> getCUIdentifiers(StringRef Abbrev,
140 StringRef Info,
141 StringRef StrOffsets,
142 StringRef Str) {
David Blaikief1958da2016-02-26 07:30:15 +0000143 uint32_t Offset = 0;
144 DataExtractor InfoData(Info, true, 0);
Greg Clayton82f12b12016-11-11 16:21:37 +0000145 dwarf::DwarfFormat Format = dwarf::DwarfFormat::DWARF32;
146 uint64_t Length = InfoData.getU32(&Offset);
147 // If the length is 0xffffffff, then this indictes that this is a DWARF 64
148 // stream and the length is actually encoded into a 64 bit value that follows.
149 if (Length == 0xffffffffU) {
150 Format = dwarf::DwarfFormat::DWARF64;
151 Length = InfoData.getU64(&Offset);
152 }
David Blaikief1958da2016-02-26 07:30:15 +0000153 uint16_t Version = InfoData.getU16(&Offset);
154 InfoData.getU32(&Offset); // Abbrev offset (should be zero)
155 uint8_t AddrSize = InfoData.getU8(&Offset);
156
157 uint32_t AbbrCode = InfoData.getULEB128(&Offset);
158
159 DataExtractor AbbrevData(Abbrev, true, 0);
160 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
Greg Clayton6c273762016-10-27 16:32:04 +0000161 auto Tag = static_cast<dwarf::Tag>(AbbrevData.getULEB128(&AbbrevOffset));
David Blaikie7bb62ef2016-05-16 23:26:29 +0000162 if (Tag != dwarf::DW_TAG_compile_unit)
163 return make_error<DWPError>("top level DIE is not a compile unit");
David Blaikief1958da2016-02-26 07:30:15 +0000164 // DW_CHILDREN
165 AbbrevData.getU8(&AbbrevOffset);
166 uint32_t Name;
Greg Clayton6c273762016-10-27 16:32:04 +0000167 dwarf::Form Form;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000168 CompileUnitIdentifiers ID;
David Blaikief1958da2016-02-26 07:30:15 +0000169 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |
Greg Clayton6c273762016-10-27 16:32:04 +0000170 (Form = static_cast<dwarf::Form>(AbbrevData.getULEB128(&AbbrevOffset))) &&
David Blaikiece7c6cf2016-03-24 22:17:08 +0000171 (Name != 0 || Form != 0)) {
172 switch (Name) {
173 case dwarf::DW_AT_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.Name = *EName;
David Blaikie4dd03f02016-03-26 20:32:14 +0000179 break;
180 }
181 case dwarf::DW_AT_GNU_dwo_name: {
David Blaikie4940f872016-05-17 00:07:10 +0000182 Expected<const char *> EName =
183 getIndexedString(Form, InfoData, Offset, StrOffsets, Str);
184 if (!EName)
185 return EName.takeError();
186 ID.DWOName = *EName;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000187 break;
188 }
189 case dwarf::DW_AT_GNU_dwo_id:
190 ID.Signature = InfoData.getU64(&Offset);
191 break;
192 default:
Paul Robinson75c068c2017-06-26 18:43:01 +0000193 DWARFFormValue::skipValue(Form, InfoData, &Offset,
194 DWARFFormParams({Version, AddrSize, Format}));
David Blaikiece7c6cf2016-03-24 22:17:08 +0000195 }
David Blaikief1958da2016-02-26 07:30:15 +0000196 }
David Blaikiece7c6cf2016-03-24 22:17:08 +0000197 return ID;
David Blaikiead07b5d2015-12-04 17:20:04 +0000198}
199
David Blaikie24c8ac92015-12-05 03:05:45 +0000200struct UnitIndexEntry {
David Blaikie24c8ac92015-12-05 03:05:45 +0000201 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
David Blaikief1958da2016-02-26 07:30:15 +0000202 std::string Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000203 std::string DWOName;
David Blaikief1958da2016-02-26 07:30:15 +0000204 StringRef DWPName;
David Blaikie24c8ac92015-12-05 03:05:45 +0000205};
206
David Blaikiefd800922016-05-23 16:32:11 +0000207static StringRef getSubsection(StringRef Section,
208 const DWARFUnitIndex::Entry &Entry,
209 DWARFSectionKind Kind) {
David Blaikief1958da2016-02-26 07:30:15 +0000210 const auto *Off = Entry.getOffset(Kind);
211 if (!Off)
212 return StringRef();
213 return Section.substr(Off->Offset, Off->Length);
214}
215
David Blaikie852c02b2016-02-19 21:09:26 +0000216static void addAllTypesFromDWP(
217 MCStreamer &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
218 const DWARFUnitIndex &TUIndex, MCSection *OutputTypes, StringRef Types,
219 const UnitIndexEntry &TUEntry, uint32_t &TypesOffset) {
David Blaikie8bce5a02016-02-17 07:00:24 +0000220 Out.SwitchSection(OutputTypes);
221 for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) {
222 auto *I = E.getOffsets();
223 if (!I)
224 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000225 auto P = TypeIndexEntries.insert(std::make_pair(E.getSignature(), TUEntry));
226 if (!P.second)
David Blaikie8bce5a02016-02-17 07:00:24 +0000227 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000228 auto &Entry = P.first->second;
David Blaikie8bce5a02016-02-17 07:00:24 +0000229 // Zero out the debug_info contribution
230 Entry.Contributions[0] = {};
231 for (auto Kind : TUIndex.getColumnKinds()) {
232 auto &C = Entry.Contributions[Kind - DW_SECT_INFO];
233 C.Offset += I->Offset;
234 C.Length = I->Length;
235 ++I;
236 }
237 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
238 Out.EmitBytes(Types.substr(
239 C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset,
240 C.Length));
241 C.Offset = TypesOffset;
242 TypesOffset += C.Length;
David Blaikie8bce5a02016-02-17 07:00:24 +0000243 }
244}
245
David Blaikiec3826da2015-12-09 21:02:33 +0000246static void addAllTypes(MCStreamer &Out,
David Blaikie852c02b2016-02-19 21:09:26 +0000247 MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
David Blaikie62be5ae2016-04-05 20:26:50 +0000248 MCSection *OutputTypes,
249 const std::vector<StringRef> &TypesSections,
David Blaikiec3826da2015-12-09 21:02:33 +0000250 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) {
David Blaikie62be5ae2016-04-05 20:26:50 +0000251 for (StringRef Types : TypesSections) {
252 Out.SwitchSection(OutputTypes);
253 uint32_t Offset = 0;
254 DataExtractor Data(Types, true, 0);
255 while (Data.isValidOffset(Offset)) {
256 UnitIndexEntry Entry = CUEntry;
257 // Zero out the debug_info contribution
258 Entry.Contributions[0] = {};
259 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
260 C.Offset = TypesOffset;
261 auto PrevOffset = Offset;
262 // Length of the unit, including the 4 byte length field.
263 C.Length = Data.getU32(&Offset) + 4;
David Blaikiec3826da2015-12-09 21:02:33 +0000264
David Blaikie62be5ae2016-04-05 20:26:50 +0000265 Data.getU16(&Offset); // Version
266 Data.getU32(&Offset); // Abbrev offset
267 Data.getU8(&Offset); // Address size
268 auto Signature = Data.getU64(&Offset);
269 Offset = PrevOffset + C.Length;
David Blaikie24c8ac92015-12-05 03:05:45 +0000270
David Blaikie62be5ae2016-04-05 20:26:50 +0000271 auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry));
272 if (!P.second)
273 continue;
David Blaikief5cb6272015-12-14 07:42:00 +0000274
David Blaikie62be5ae2016-04-05 20:26:50 +0000275 Out.EmitBytes(Types.substr(PrevOffset, C.Length));
276 TypesOffset += C.Length;
277 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000278 }
279}
280
281static void
282writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,
David Blaikie852c02b2016-02-19 21:09:26 +0000283 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries,
David Blaikie24c8ac92015-12-05 03:05:45 +0000284 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) {
285 for (const auto &E : IndexEntries)
David Blaikie852c02b2016-02-19 21:09:26 +0000286 for (size_t i = 0; i != array_lengthof(E.second.Contributions); ++i)
David Blaikie24c8ac92015-12-05 03:05:45 +0000287 if (ContributionOffsets[i])
David Blaikie852c02b2016-02-19 21:09:26 +0000288 Out.EmitIntValue(E.second.Contributions[i].*Field, 4);
David Blaikie24c8ac92015-12-05 03:05:45 +0000289}
290
David Blaikie852c02b2016-02-19 21:09:26 +0000291static void
292writeIndex(MCStreamer &Out, MCSection *Section,
293 ArrayRef<unsigned> ContributionOffsets,
294 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries) {
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000295 if (IndexEntries.empty())
296 return;
297
David Blaikie24c8ac92015-12-05 03:05:45 +0000298 unsigned Columns = 0;
299 for (auto &C : ContributionOffsets)
300 if (C)
301 ++Columns;
302
303 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
304 uint64_t Mask = Buckets.size() - 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000305 size_t i = 0;
306 for (const auto &P : IndexEntries) {
307 auto S = P.first;
David Blaikie24c8ac92015-12-05 03:05:45 +0000308 auto H = S & Mask;
David Blaikie9b492562016-04-05 17:51:40 +0000309 auto HP = ((S >> 32) & Mask) | 1;
David Blaikiec3826da2015-12-09 21:02:33 +0000310 while (Buckets[H]) {
David Blaikie852c02b2016-02-19 21:09:26 +0000311 assert(S != IndexEntries.begin()[Buckets[H] - 1].first &&
David Blaikie74f5b282016-02-19 01:51:44 +0000312 "Duplicate unit");
David Blaikie9b492562016-04-05 17:51:40 +0000313 H = (H + HP) & Mask;
David Blaikiec3826da2015-12-09 21:02:33 +0000314 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000315 Buckets[H] = i + 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000316 ++i;
David Blaikie24c8ac92015-12-05 03:05:45 +0000317 }
318
319 Out.SwitchSection(Section);
320 Out.EmitIntValue(2, 4); // Version
321 Out.EmitIntValue(Columns, 4); // Columns
322 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
David Blaikie2ed678c2015-12-05 03:06:30 +0000323 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets
David Blaikie24c8ac92015-12-05 03:05:45 +0000324
325 // Write the signatures.
326 for (const auto &I : Buckets)
David Blaikie852c02b2016-02-19 21:09:26 +0000327 Out.EmitIntValue(I ? IndexEntries.begin()[I - 1].first : 0, 8);
David Blaikie24c8ac92015-12-05 03:05:45 +0000328
329 // Write the indexes.
330 for (const auto &I : Buckets)
331 Out.EmitIntValue(I, 4);
332
333 // Write the column headers (which sections will appear in the table)
334 for (size_t i = 0; i != ContributionOffsets.size(); ++i)
335 if (ContributionOffsets[i])
336 Out.EmitIntValue(i + DW_SECT_INFO, 4);
337
338 // Write the offsets.
339 writeIndexTable(Out, ContributionOffsets, IndexEntries,
340 &DWARFUnitIndex::Entry::SectionContribution::Offset);
341
342 // Write the lengths.
343 writeIndexTable(Out, ContributionOffsets, IndexEntries,
344 &DWARFUnitIndex::Entry::SectionContribution::Length);
345}
David Blaikie74f5b282016-02-19 01:51:44 +0000346
David Blaikied1f7ab32016-05-16 20:42:27 +0000347std::string buildDWODescription(StringRef Name, StringRef DWPName, StringRef DWOName) {
348 std::string Text = "\'";
349 Text += Name;
350 Text += '\'';
David Blaikie4dd03f02016-03-26 20:32:14 +0000351 if (!DWPName.empty()) {
David Blaikied1f7ab32016-05-16 20:42:27 +0000352 Text += " (from ";
353 if (!DWOName.empty()) {
354 Text += '\'';
355 Text += DWOName;
356 Text += "' in ";
357 }
358 Text += '\'';
359 Text += DWPName;
360 Text += "')";
David Blaikie4dd03f02016-03-26 20:32:14 +0000361 }
David Blaikied1f7ab32016-05-16 20:42:27 +0000362 return Text;
363}
364
George Rimar8f5976e2017-01-13 15:58:55 +0000365static Error createError(StringRef Name, Error E) {
366 return make_error<DWPError>(
367 ("failure while decompressing compressed section: '" + Name + "', " +
368 llvm::toString(std::move(E)))
369 .str());
370}
371
372static Error
373handleCompressedSection(std::deque<SmallString<32>> &UncompressedSections,
374 StringRef &Name, StringRef &Contents) {
375 if (!Decompressor::isGnuStyle(Name))
Mehdi Amini41af4302016-11-11 04:28:40 +0000376 return Error::success();
George Rimar8f5976e2017-01-13 15:58:55 +0000377
378 Expected<Decompressor> Dec =
379 Decompressor::create(Name, Contents, false /*IsLE*/, false /*Is64Bit*/);
380 if (!Dec)
381 return createError(Name, Dec.takeError());
382
David Blaikie05e0d2b2016-05-23 21:58:58 +0000383 UncompressedSections.emplace_back();
George Rimarf98b9ac2017-05-18 08:00:01 +0000384 if (Error E = Dec->resizeAndDecompress(UncompressedSections.back()))
George Rimar8f5976e2017-01-13 15:58:55 +0000385 return createError(Name, std::move(E));
386
387 Name = Name.substr(2); // Drop ".z"
David Blaikie05e0d2b2016-05-23 21:58:58 +0000388 Contents = UncompressedSections.back();
Mehdi Amini41af4302016-11-11 04:28:40 +0000389 return Error::success();
David Blaikie05e0d2b2016-05-23 21:58:58 +0000390}
David Blaikied9517cb2016-05-23 22:21:10 +0000391
392static Error handleSection(
393 const StringMap<std::pair<MCSection *, DWARFSectionKind>> &KnownSections,
394 const MCSection *StrSection, const MCSection *StrOffsetSection,
395 const MCSection *TypesSection, const MCSection *CUIndexSection,
396 const MCSection *TUIndexSection, const SectionRef &Section, MCStreamer &Out,
David Blaikie1fc3e6b2016-05-25 23:37:06 +0000397 std::deque<SmallString<32>> &UncompressedSections,
David Blaikied9517cb2016-05-23 22:21:10 +0000398 uint32_t (&ContributionOffsets)[8], UnitIndexEntry &CurEntry,
399 StringRef &CurStrSection, StringRef &CurStrOffsetSection,
400 std::vector<StringRef> &CurTypesSection, StringRef &InfoSection,
401 StringRef &AbbrevSection, StringRef &CurCUIndexSection,
402 StringRef &CurTUIndexSection) {
403 if (Section.isBSS())
Mehdi Amini41af4302016-11-11 04:28:40 +0000404 return Error::success();
David Blaikied9517cb2016-05-23 22:21:10 +0000405
406 if (Section.isVirtual())
Mehdi Amini41af4302016-11-11 04:28:40 +0000407 return Error::success();
David Blaikied9517cb2016-05-23 22:21:10 +0000408
409 StringRef Name;
410 if (std::error_code Err = Section.getName(Name))
411 return errorCodeToError(Err);
412
David Blaikied9517cb2016-05-23 22:21:10 +0000413 StringRef Contents;
414 if (auto Err = Section.getContents(Contents))
415 return errorCodeToError(Err);
416
417 if (auto Err = handleCompressedSection(UncompressedSections, Name, Contents))
418 return Err;
419
George Rimar8f5976e2017-01-13 15:58:55 +0000420 Name = Name.substr(Name.find_first_not_of("._"));
421
David Blaikied9517cb2016-05-23 22:21:10 +0000422 auto SectionPair = KnownSections.find(Name);
423 if (SectionPair == KnownSections.end())
Mehdi Amini41af4302016-11-11 04:28:40 +0000424 return Error::success();
David Blaikied9517cb2016-05-23 22:21:10 +0000425
426 if (DWARFSectionKind Kind = SectionPair->second.second) {
427 auto Index = Kind - DW_SECT_INFO;
428 if (Kind != DW_SECT_TYPES) {
429 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
430 ContributionOffsets[Index] +=
431 (CurEntry.Contributions[Index].Length = Contents.size());
432 }
433
434 switch (Kind) {
435 case DW_SECT_INFO:
436 InfoSection = Contents;
437 break;
438 case DW_SECT_ABBREV:
439 AbbrevSection = Contents;
440 break;
441 default:
442 break;
443 }
444 }
445
446 MCSection *OutSection = SectionPair->second.first;
447 if (OutSection == StrOffsetSection)
448 CurStrOffsetSection = Contents;
449 else if (OutSection == StrSection)
450 CurStrSection = Contents;
451 else if (OutSection == TypesSection)
452 CurTypesSection.push_back(Contents);
453 else if (OutSection == CUIndexSection)
454 CurCUIndexSection = Contents;
455 else if (OutSection == TUIndexSection)
456 CurTUIndexSection = Contents;
457 else {
458 Out.SwitchSection(OutSection);
459 Out.EmitBytes(Contents);
460 }
Mehdi Amini41af4302016-11-11 04:28:40 +0000461 return Error::success();
David Blaikied9517cb2016-05-23 22:21:10 +0000462}
463
464static Error
465buildDuplicateError(const std::pair<uint64_t, UnitIndexEntry> &PrevE,
466 const CompileUnitIdentifiers &ID, StringRef DWPName) {
David Blaikie11825c72016-05-17 19:40:28 +0000467 return make_error<DWPError>(
468 std::string("Duplicate DWO ID (") + utohexstr(PrevE.first) + ") in " +
469 buildDWODescription(PrevE.second.Name, PrevE.second.DWPName,
470 PrevE.second.DWOName) +
471 " and " + buildDWODescription(ID.Name, DWPName, ID.DWOName));
David Blaikie4dd03f02016-03-26 20:32:14 +0000472}
David Blaikied9517cb2016-05-23 22:21:10 +0000473
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +0000474static Expected<SmallVector<std::string, 16>>
475getDWOFilenames(StringRef ExecFilename) {
476 auto ErrOrObj = object::ObjectFile::createObjectFile(ExecFilename);
477 if (!ErrOrObj)
478 return ErrOrObj.takeError();
479
480 const ObjectFile &Obj = *ErrOrObj.get().getBinary();
481 std::unique_ptr<DWARFContext> DWARFCtx = DWARFContext::create(Obj);
482
483 SmallVector<std::string, 16> DWOPaths;
484 for (const auto &CU : DWARFCtx->compile_units()) {
485 const DWARFDie &Die = CU->getUnitDIE();
486 std::string DWOName = dwarf::toString(
487 Die.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
488 if (DWOName.empty())
489 continue;
490 std::string DWOCompDir =
491 dwarf::toString(Die.find(dwarf::DW_AT_comp_dir), "");
492 if (!DWOCompDir.empty()) {
493 SmallString<16> DWOPath;
494 sys::path::append(DWOPath, DWOCompDir, DWOName);
495 DWOPaths.emplace_back(DWOPath.data(), DWOPath.size());
496 } else {
497 DWOPaths.push_back(std::move(DWOName));
498 }
499 }
500 return std::move(DWOPaths);
501}
502
David Blaikiebc8397c2016-05-12 19:59:54 +0000503static Error write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000504 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
505 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
506 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikiec3826da2015-12-09 21:02:33 +0000507 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
David Blaikie23919372016-02-06 01:15:26 +0000508 MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection();
David Blaikie8bce5a02016-02-17 07:00:24 +0000509 MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000510 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
511 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
512 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
513 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
514 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
515 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
David Blaikieb7020252015-12-04 21:16:42 +0000516 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikie23919372016-02-06 01:15:26 +0000517 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
David Blaikie8bce5a02016-02-17 07:00:24 +0000518 {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}},
519 {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}};
David Blaikieb073cb92015-12-02 06:21:34 +0000520
David Blaikie852c02b2016-02-19 21:09:26 +0000521 MapVector<uint64_t, UnitIndexEntry> IndexEntries;
522 MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000523
David Blaikieb073cb92015-12-02 06:21:34 +0000524 uint32_t ContributionOffsets[8] = {};
525
David Blaikiefd800922016-05-23 16:32:11 +0000526 DWPStringPool Strings(Out, StrSection);
527
528 SmallVector<OwningBinary<object::ObjectFile>, 128> Objects;
529 Objects.reserve(Inputs.size());
530
David Blaikie1fc3e6b2016-05-25 23:37:06 +0000531 std::deque<SmallString<32>> UncompressedSections;
David Blaikie2e9bd892016-05-23 17:35:51 +0000532
David Blaikie242b9482015-12-01 00:48:39 +0000533 for (const auto &Input : Inputs) {
534 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
535 if (!ErrOrObj)
David Blaikiebc8397c2016-05-12 19:59:54 +0000536 return ErrOrObj.takeError();
David Blaikieb073cb92015-12-02 06:21:34 +0000537
David Blaikiefd800922016-05-23 16:32:11 +0000538 auto &Obj = *ErrOrObj->getBinary();
539 Objects.push_back(std::move(*ErrOrObj));
540
David Blaikie23919372016-02-06 01:15:26 +0000541 UnitIndexEntry CurEntry = {};
David Blaikieb073cb92015-12-02 06:21:34 +0000542
David Blaikie98ad82a2015-12-01 18:07:07 +0000543 StringRef CurStrSection;
544 StringRef CurStrOffsetSection;
David Blaikie62be5ae2016-04-05 20:26:50 +0000545 std::vector<StringRef> CurTypesSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000546 StringRef InfoSection;
547 StringRef AbbrevSection;
David Blaikie23919372016-02-06 01:15:26 +0000548 StringRef CurCUIndexSection;
David Blaikie8bce5a02016-02-17 07:00:24 +0000549 StringRef CurTUIndexSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000550
David Blaikied9517cb2016-05-23 22:21:10 +0000551 for (const auto &Section : Obj.sections())
552 if (auto Err = handleSection(
553 KnownSections, StrSection, StrOffsetSection, TypesSection,
554 CUIndexSection, TUIndexSection, Section, Out,
555 UncompressedSections, ContributionOffsets, CurEntry,
556 CurStrSection, CurStrOffsetSection, CurTypesSection, InfoSection,
557 AbbrevSection, CurCUIndexSection, CurTUIndexSection))
David Blaikie05e0d2b2016-05-23 21:58:58 +0000558 return Err;
David Blaikie74f5b282016-02-19 01:51:44 +0000559
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000560 if (InfoSection.empty())
561 continue;
562
David Blaikie478c1a22016-05-23 22:38:06 +0000563 writeStringsAndOffsets(Out, Strings, StrOffsetSection, CurStrSection,
564 CurStrOffsetSection);
David Blaikie23919372016-02-06 01:15:26 +0000565
David Blaikie478c1a22016-05-23 22:38:06 +0000566 if (CurCUIndexSection.empty()) {
David Blaikie7bb62ef2016-05-16 23:26:29 +0000567 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(
David Blaikiece7c6cf2016-03-24 22:17:08 +0000568 AbbrevSection, InfoSection, CurStrOffsetSection, CurStrSection);
David Blaikie7bb62ef2016-05-16 23:26:29 +0000569 if (!EID)
570 return EID.takeError();
571 const auto &ID = *EID;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000572 auto P = IndexEntries.insert(std::make_pair(ID.Signature, CurEntry));
David Blaikied1f7ab32016-05-16 20:42:27 +0000573 if (!P.second)
David Blaikie11825c72016-05-17 19:40:28 +0000574 return buildDuplicateError(*P.first, ID, "");
David Blaikiece7c6cf2016-03-24 22:17:08 +0000575 P.first->second.Name = ID.Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000576 P.first->second.DWOName = ID.DWOName;
David Blaikie23919372016-02-06 01:15:26 +0000577 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection,
578 CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
David Blaikie478c1a22016-05-23 22:38:06 +0000579 continue;
David Blaikie23919372016-02-06 01:15:26 +0000580 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000581
David Blaikie478c1a22016-05-23 22:38:06 +0000582 DWARFUnitIndex CUIndex(DW_SECT_INFO);
583 DataExtractor CUIndexData(CurCUIndexSection, Obj.isLittleEndian(), 0);
584 if (!CUIndex.parse(CUIndexData))
585 return make_error<DWPError>("Failed to parse cu_index");
586
587 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
588 auto *I = E.getOffsets();
589 if (!I)
590 continue;
591 auto P = IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry));
592 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(
593 getSubsection(AbbrevSection, E, DW_SECT_ABBREV),
594 getSubsection(InfoSection, E, DW_SECT_INFO),
595 getSubsection(CurStrOffsetSection, E, DW_SECT_STR_OFFSETS),
596 CurStrSection);
597 if (!EID)
598 return EID.takeError();
599 const auto &ID = *EID;
600 if (!P.second)
601 return buildDuplicateError(*P.first, ID, Input);
602 auto &NewEntry = P.first->second;
603 NewEntry.Name = ID.Name;
604 NewEntry.DWOName = ID.DWOName;
605 NewEntry.DWPName = Input;
606 for (auto Kind : CUIndex.getColumnKinds()) {
607 auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO];
608 C.Offset += I->Offset;
609 C.Length = I->Length;
610 ++I;
611 }
612 }
613
614 if (!CurTypesSection.empty()) {
615 if (CurTypesSection.size() != 1)
616 return make_error<DWPError>("multiple type unit sections in .dwp file");
617 DWARFUnitIndex TUIndex(DW_SECT_TYPES);
618 DataExtractor TUIndexData(CurTUIndexSection, Obj.isLittleEndian(), 0);
619 if (!TUIndex.parse(TUIndexData))
620 return make_error<DWPError>("Failed to parse tu_index");
621 addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection,
622 CurTypesSection.front(), CurEntry,
623 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
624 }
David Blaikie242b9482015-12-01 00:48:39 +0000625 }
David Blaikieb073cb92015-12-02 06:21:34 +0000626
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000627 // Lie about there being no info contributions so the TU index only includes
628 // the type unit contribution
629 ContributionOffsets[0] = 0;
630 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
631 TypeIndexEntries);
David Blaikieb3757c02015-12-02 22:01:56 +0000632
David Blaikie24c8ac92015-12-05 03:05:45 +0000633 // Lie about the type contribution
634 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
635 // Unlie about the info contribution
636 ContributionOffsets[0] = 1;
David Blaikie7c4ffe02015-12-04 21:30:23 +0000637
David Blaikie24c8ac92015-12-05 03:05:45 +0000638 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
639 IndexEntries);
David Blaikieddb27362016-03-01 21:24:04 +0000640
Mehdi Amini41af4302016-11-11 04:28:40 +0000641 return Error::success();
David Blaikie242b9482015-12-01 00:48:39 +0000642}
643
David Blaikie17ab78e2016-05-17 23:37:44 +0000644static int error(const Twine &Error, const Twine &Context) {
645 errs() << Twine("while processing ") + Context + ":\n";
646 errs() << Twine("error: ") + Error + "\n";
647 return 1;
648}
649
David Blaikie2ed678c2015-12-05 03:06:30 +0000650int main(int argc, char **argv) {
David Blaikie242b9482015-12-01 00:48:39 +0000651
652 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
653
654 llvm::InitializeAllTargetInfos();
655 llvm::InitializeAllTargetMCs();
656 llvm::InitializeAllTargets();
657 llvm::InitializeAllAsmPrinters();
658
659 std::string ErrorStr;
660 StringRef Context = "dwarf streamer init";
661
662 Triple TheTriple("x86_64-linux-gnu");
663
664 // Get the target.
665 const Target *TheTarget =
666 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
667 if (!TheTarget)
668 return error(ErrorStr, Context);
669 std::string TripleName = TheTriple.getTriple();
670
671 // Create all the MC Objects.
672 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
673 if (!MRI)
674 return error(Twine("no register info for target ") + TripleName, Context);
675
676 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
677 if (!MAI)
678 return error("no asm info for target " + TripleName, Context);
679
680 MCObjectFileInfo MOFI;
681 MCContext MC(MAI.get(), MRI.get(), &MOFI);
Rafael Espindola9f929952017-08-02 20:32:26 +0000682 MOFI.InitMCObjectFileInfo(TheTriple, /*PIC*/ false, MC);
David Blaikie242b9482015-12-01 00:48:39 +0000683
Joel Jones373d7d32016-07-25 17:18:28 +0000684 MCTargetOptions Options;
685 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "", Options);
David Blaikie242b9482015-12-01 00:48:39 +0000686 if (!MAB)
687 return error("no asm backend for target " + TripleName, Context);
688
689 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
690 if (!MII)
691 return error("no instr info info for target " + TripleName, Context);
692
693 std::unique_ptr<MCSubtargetInfo> MSTI(
694 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
695 if (!MSTI)
696 return error("no subtarget info for target " + TripleName, Context);
697
698 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
699 if (!MCE)
700 return error("no code emitter for target " + TripleName, Context);
701
702 // Create the output file.
703 std::error_code EC;
704 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
705 if (EC)
706 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
707
David Majnemer03e2cc32015-12-21 22:09:27 +0000708 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
David Blaikie242b9482015-12-01 00:48:39 +0000709 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
Lang Hames02d33052017-10-11 01:57:21 +0000710 TheTriple, MC, std::unique_ptr<MCAsmBackend>(MAB), OutFile, MCE, *MSTI,
711 MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible,
David Blaikie242b9482015-12-01 00:48:39 +0000712 /*DWARFMustBeAtTheEnd*/ false));
713 if (!MS)
714 return error("no object streamer for target " + TripleName, Context);
715
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +0000716 std::vector<std::string> DWOFilenames = InputFiles;
717 for (const auto &ExecFilename : ExecFilenames) {
718 auto DWOs = getDWOFilenames(ExecFilename);
719 if (!DWOs) {
720 logAllUnhandledErrors(DWOs.takeError(), errs(), "error: ");
721 return 1;
722 }
723 DWOFilenames.insert(DWOFilenames.end(),
724 std::make_move_iterator(DWOs->begin()),
725 std::make_move_iterator(DWOs->end()));
726 }
727
728 if (auto Err = write(*MS, DWOFilenames)) {
David Blaikiebc8397c2016-05-12 19:59:54 +0000729 logAllUnhandledErrors(std::move(Err), errs(), "error: ");
730 return 1;
731 }
David Blaikieddb27362016-03-01 21:24:04 +0000732
733 MS->Finish();
David Blaikiedf055252015-12-01 00:48:34 +0000734}