blob: 59975f323bfd331ffd4ee63a3a913dc3b75a2589 [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"
Lang Hames2241ffa2017-10-11 23:34:47 +000025#include "llvm/MC/MCCodeEmitter.h"
David Blaikie242b9482015-12-01 00:48:39 +000026#include "llvm/MC/MCContext.h"
27#include "llvm/MC/MCInstrInfo.h"
28#include "llvm/MC/MCObjectFileInfo.h"
29#include "llvm/MC/MCRegisterInfo.h"
30#include "llvm/MC/MCSectionELF.h"
31#include "llvm/MC/MCStreamer.h"
David Majnemer03e2cc32015-12-21 22:09:27 +000032#include "llvm/MC/MCTargetOptionsCommandFlags.h"
George Rimar8f5976e2017-01-13 15:58:55 +000033#include "llvm/Object/Decompressor.h"
David Blaikie242b9482015-12-01 00:48:39 +000034#include "llvm/Object/ObjectFile.h"
David Blaikie74f5b282016-02-19 01:51:44 +000035#include "llvm/Support/Compression.h"
David Blaikie98ad82a2015-12-01 18:07:07 +000036#include "llvm/Support/DataExtractor.h"
David Blaikiefd800922016-05-23 16:32:11 +000037#include "llvm/Support/Error.h"
David Blaikie242b9482015-12-01 00:48:39 +000038#include "llvm/Support/FileSystem.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000039#include "llvm/Support/MathExtras.h"
David Blaikie242b9482015-12-01 00:48:39 +000040#include "llvm/Support/MemoryBuffer.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000041#include "llvm/Support/Options.h"
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +000042#include "llvm/Support/Path.h"
David Blaikie242b9482015-12-01 00:48:39 +000043#include "llvm/Support/TargetRegistry.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000044#include "llvm/Support/TargetSelect.h"
David Blaikie242b9482015-12-01 00:48:39 +000045#include "llvm/Support/raw_ostream.h"
46#include "llvm/Target/TargetMachine.h"
David Blaikie1fc3e6b2016-05-25 23:37:06 +000047#include <deque>
David Blaikief1958da2016-02-26 07:30:15 +000048#include <iostream>
David Blaikie2ed678c2015-12-05 03:06:30 +000049#include <memory>
David Blaikie242b9482015-12-01 00:48:39 +000050
51using namespace llvm;
David Blaikie98ad82a2015-12-01 18:07:07 +000052using namespace llvm::object;
David Blaikie242b9482015-12-01 00:48:39 +000053using namespace cl;
54
55OptionCategory DwpCategory("Specific Options");
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +000056static list<std::string> InputFiles(Positional, ZeroOrMore,
David Blaikie242b9482015-12-01 00:48:39 +000057 desc("<input files>"), cat(DwpCategory));
58
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +000059static list<std::string> ExecFilenames(
60 "e", ZeroOrMore,
61 desc("Specify the executable/library files to get the list of *.dwo from"),
62 value_desc("filename"), cat(DwpCategory));
63
David Blaikie2ed678c2015-12-05 03:06:30 +000064static opt<std::string> OutputFilename(Required, "o",
65 desc("Specify the output file."),
66 value_desc("filename"),
67 cat(DwpCategory));
David Blaikie242b9482015-12-01 00:48:39 +000068
David Blaikiefd800922016-05-23 16:32:11 +000069static void writeStringsAndOffsets(MCStreamer &Out, DWPStringPool &Strings,
70 MCSection *StrOffsetSection,
71 StringRef CurStrSection,
72 StringRef CurStrOffsetSection) {
David Blaikie98ad82a2015-12-01 18:07:07 +000073 // Could possibly produce an error or warning if one of these was non-null but
74 // the other was null.
75 if (CurStrSection.empty() || CurStrOffsetSection.empty())
David Blaikiebc619cd2016-05-17 23:44:13 +000076 return;
David Blaikie98ad82a2015-12-01 18:07:07 +000077
78 DenseMap<uint32_t, uint32_t> OffsetRemapping;
79
80 DataExtractor Data(CurStrSection, true, 0);
81 uint32_t LocalOffset = 0;
82 uint32_t PrevOffset = 0;
83 while (const char *s = Data.getCStr(&LocalOffset)) {
David Blaikiefd800922016-05-23 16:32:11 +000084 OffsetRemapping[PrevOffset] =
85 Strings.getOffset(s, LocalOffset - PrevOffset);
David Blaikie98ad82a2015-12-01 18:07:07 +000086 PrevOffset = LocalOffset;
87 }
88
89 Data = DataExtractor(CurStrOffsetSection, true, 0);
90
91 Out.SwitchSection(StrOffsetSection);
92
93 uint32_t Offset = 0;
94 uint64_t Size = CurStrOffsetSection.size();
95 while (Offset < Size) {
96 auto OldOffset = Data.getU32(&Offset);
97 auto NewOffset = OffsetRemapping[OldOffset];
98 Out.EmitIntValue(NewOffset, 4);
99 }
David Blaikie242b9482015-12-01 00:48:39 +0000100}
101
David Blaikiead07b5d2015-12-04 17:20:04 +0000102static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
103 uint64_t CurCode;
104 uint32_t Offset = 0;
105 DataExtractor AbbrevData(Abbrev, true, 0);
106 while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) {
107 // Tag
108 AbbrevData.getULEB128(&Offset);
109 // DW_CHILDREN
110 AbbrevData.getU8(&Offset);
111 // Attributes
112 while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset))
113 ;
114 }
115 return Offset;
116}
117
David Blaikiece7c6cf2016-03-24 22:17:08 +0000118struct CompileUnitIdentifiers {
119 uint64_t Signature = 0;
David Blaikie4dd03f02016-03-26 20:32:14 +0000120 const char *Name = "";
121 const char *DWOName = "";
David Blaikiece7c6cf2016-03-24 22:17:08 +0000122};
123
David Blaikie4940f872016-05-17 00:07:10 +0000124static Expected<const char *>
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +0000125getIndexedString(dwarf::Form Form, DataExtractor InfoData,
Greg Clayton6c273762016-10-27 16:32:04 +0000126 uint32_t &InfoOffset, StringRef StrOffsets, StringRef Str) {
David Blaikie60fbd3b2016-04-05 20:16:38 +0000127 if (Form == dwarf::DW_FORM_string)
128 return InfoData.getCStr(&InfoOffset);
David Blaikie4940f872016-05-17 00:07:10 +0000129 if (Form != dwarf::DW_FORM_GNU_str_index)
130 return make_error<DWPError>(
131 "string field encoded without DW_FORM_string or DW_FORM_GNU_str_index");
David Blaikie60fbd3b2016-04-05 20:16:38 +0000132 auto StrIndex = InfoData.getULEB128(&InfoOffset);
David Blaikie4dd03f02016-03-26 20:32:14 +0000133 DataExtractor StrOffsetsData(StrOffsets, true, 0);
134 uint32_t StrOffsetsOffset = 4 * StrIndex;
135 uint32_t StrOffset = StrOffsetsData.getU32(&StrOffsetsOffset);
136 DataExtractor StrData(Str, true, 0);
137 return StrData.getCStr(&StrOffset);
138}
139
David Blaikie7bb62ef2016-05-16 23:26:29 +0000140static Expected<CompileUnitIdentifiers> getCUIdentifiers(StringRef Abbrev,
141 StringRef Info,
142 StringRef StrOffsets,
143 StringRef Str) {
David Blaikief1958da2016-02-26 07:30:15 +0000144 uint32_t Offset = 0;
145 DataExtractor InfoData(Info, true, 0);
Greg Clayton82f12b12016-11-11 16:21:37 +0000146 dwarf::DwarfFormat Format = dwarf::DwarfFormat::DWARF32;
147 uint64_t Length = InfoData.getU32(&Offset);
148 // If the length is 0xffffffff, then this indictes that this is a DWARF 64
149 // stream and the length is actually encoded into a 64 bit value that follows.
150 if (Length == 0xffffffffU) {
151 Format = dwarf::DwarfFormat::DWARF64;
152 Length = InfoData.getU64(&Offset);
153 }
David Blaikief1958da2016-02-26 07:30:15 +0000154 uint16_t Version = InfoData.getU16(&Offset);
155 InfoData.getU32(&Offset); // Abbrev offset (should be zero)
156 uint8_t AddrSize = InfoData.getU8(&Offset);
157
158 uint32_t AbbrCode = InfoData.getULEB128(&Offset);
159
160 DataExtractor AbbrevData(Abbrev, true, 0);
161 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
Greg Clayton6c273762016-10-27 16:32:04 +0000162 auto Tag = static_cast<dwarf::Tag>(AbbrevData.getULEB128(&AbbrevOffset));
David Blaikie7bb62ef2016-05-16 23:26:29 +0000163 if (Tag != dwarf::DW_TAG_compile_unit)
164 return make_error<DWPError>("top level DIE is not a compile unit");
David Blaikief1958da2016-02-26 07:30:15 +0000165 // DW_CHILDREN
166 AbbrevData.getU8(&AbbrevOffset);
167 uint32_t Name;
Greg Clayton6c273762016-10-27 16:32:04 +0000168 dwarf::Form Form;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000169 CompileUnitIdentifiers ID;
David Blaikief1958da2016-02-26 07:30:15 +0000170 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |
Greg Clayton6c273762016-10-27 16:32:04 +0000171 (Form = static_cast<dwarf::Form>(AbbrevData.getULEB128(&AbbrevOffset))) &&
David Blaikiece7c6cf2016-03-24 22:17:08 +0000172 (Name != 0 || Form != 0)) {
173 switch (Name) {
174 case dwarf::DW_AT_name: {
David Blaikie4940f872016-05-17 00:07:10 +0000175 Expected<const char *> EName =
176 getIndexedString(Form, InfoData, Offset, StrOffsets, Str);
177 if (!EName)
178 return EName.takeError();
179 ID.Name = *EName;
David Blaikie4dd03f02016-03-26 20:32:14 +0000180 break;
181 }
182 case dwarf::DW_AT_GNU_dwo_name: {
David Blaikie4940f872016-05-17 00:07:10 +0000183 Expected<const char *> EName =
184 getIndexedString(Form, InfoData, Offset, StrOffsets, Str);
185 if (!EName)
186 return EName.takeError();
187 ID.DWOName = *EName;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000188 break;
189 }
190 case dwarf::DW_AT_GNU_dwo_id:
191 ID.Signature = InfoData.getU64(&Offset);
192 break;
193 default:
Paul Robinson75c068c2017-06-26 18:43:01 +0000194 DWARFFormValue::skipValue(Form, InfoData, &Offset,
195 DWARFFormParams({Version, AddrSize, Format}));
David Blaikiece7c6cf2016-03-24 22:17:08 +0000196 }
David Blaikief1958da2016-02-26 07:30:15 +0000197 }
David Blaikiece7c6cf2016-03-24 22:17:08 +0000198 return ID;
David Blaikiead07b5d2015-12-04 17:20:04 +0000199}
200
David Blaikie24c8ac92015-12-05 03:05:45 +0000201struct UnitIndexEntry {
David Blaikie24c8ac92015-12-05 03:05:45 +0000202 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
David Blaikief1958da2016-02-26 07:30:15 +0000203 std::string Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000204 std::string DWOName;
David Blaikief1958da2016-02-26 07:30:15 +0000205 StringRef DWPName;
David Blaikie24c8ac92015-12-05 03:05:45 +0000206};
207
David Blaikiefd800922016-05-23 16:32:11 +0000208static StringRef getSubsection(StringRef Section,
209 const DWARFUnitIndex::Entry &Entry,
210 DWARFSectionKind Kind) {
David Blaikief1958da2016-02-26 07:30:15 +0000211 const auto *Off = Entry.getOffset(Kind);
212 if (!Off)
213 return StringRef();
214 return Section.substr(Off->Offset, Off->Length);
215}
216
David Blaikie852c02b2016-02-19 21:09:26 +0000217static void addAllTypesFromDWP(
218 MCStreamer &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
219 const DWARFUnitIndex &TUIndex, MCSection *OutputTypes, StringRef Types,
220 const UnitIndexEntry &TUEntry, uint32_t &TypesOffset) {
David Blaikie8bce5a02016-02-17 07:00:24 +0000221 Out.SwitchSection(OutputTypes);
222 for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) {
223 auto *I = E.getOffsets();
224 if (!I)
225 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000226 auto P = TypeIndexEntries.insert(std::make_pair(E.getSignature(), TUEntry));
227 if (!P.second)
David Blaikie8bce5a02016-02-17 07:00:24 +0000228 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000229 auto &Entry = P.first->second;
David Blaikie8bce5a02016-02-17 07:00:24 +0000230 // Zero out the debug_info contribution
231 Entry.Contributions[0] = {};
232 for (auto Kind : TUIndex.getColumnKinds()) {
233 auto &C = Entry.Contributions[Kind - DW_SECT_INFO];
234 C.Offset += I->Offset;
235 C.Length = I->Length;
236 ++I;
237 }
238 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
239 Out.EmitBytes(Types.substr(
240 C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset,
241 C.Length));
242 C.Offset = TypesOffset;
243 TypesOffset += C.Length;
David Blaikie8bce5a02016-02-17 07:00:24 +0000244 }
245}
246
David Blaikiec3826da2015-12-09 21:02:33 +0000247static void addAllTypes(MCStreamer &Out,
David Blaikie852c02b2016-02-19 21:09:26 +0000248 MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
David Blaikie62be5ae2016-04-05 20:26:50 +0000249 MCSection *OutputTypes,
250 const std::vector<StringRef> &TypesSections,
David Blaikiec3826da2015-12-09 21:02:33 +0000251 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) {
David Blaikie62be5ae2016-04-05 20:26:50 +0000252 for (StringRef Types : TypesSections) {
253 Out.SwitchSection(OutputTypes);
254 uint32_t Offset = 0;
255 DataExtractor Data(Types, true, 0);
256 while (Data.isValidOffset(Offset)) {
257 UnitIndexEntry Entry = CUEntry;
258 // Zero out the debug_info contribution
259 Entry.Contributions[0] = {};
260 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
261 C.Offset = TypesOffset;
262 auto PrevOffset = Offset;
263 // Length of the unit, including the 4 byte length field.
264 C.Length = Data.getU32(&Offset) + 4;
David Blaikiec3826da2015-12-09 21:02:33 +0000265
David Blaikie62be5ae2016-04-05 20:26:50 +0000266 Data.getU16(&Offset); // Version
267 Data.getU32(&Offset); // Abbrev offset
268 Data.getU8(&Offset); // Address size
269 auto Signature = Data.getU64(&Offset);
270 Offset = PrevOffset + C.Length;
David Blaikie24c8ac92015-12-05 03:05:45 +0000271
David Blaikie62be5ae2016-04-05 20:26:50 +0000272 auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry));
273 if (!P.second)
274 continue;
David Blaikief5cb6272015-12-14 07:42:00 +0000275
David Blaikie62be5ae2016-04-05 20:26:50 +0000276 Out.EmitBytes(Types.substr(PrevOffset, C.Length));
277 TypesOffset += C.Length;
278 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000279 }
280}
281
282static void
283writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,
David Blaikie852c02b2016-02-19 21:09:26 +0000284 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries,
David Blaikie24c8ac92015-12-05 03:05:45 +0000285 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) {
286 for (const auto &E : IndexEntries)
David Blaikie852c02b2016-02-19 21:09:26 +0000287 for (size_t i = 0; i != array_lengthof(E.second.Contributions); ++i)
David Blaikie24c8ac92015-12-05 03:05:45 +0000288 if (ContributionOffsets[i])
David Blaikie852c02b2016-02-19 21:09:26 +0000289 Out.EmitIntValue(E.second.Contributions[i].*Field, 4);
David Blaikie24c8ac92015-12-05 03:05:45 +0000290}
291
David Blaikie852c02b2016-02-19 21:09:26 +0000292static void
293writeIndex(MCStreamer &Out, MCSection *Section,
294 ArrayRef<unsigned> ContributionOffsets,
295 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries) {
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000296 if (IndexEntries.empty())
297 return;
298
David Blaikie24c8ac92015-12-05 03:05:45 +0000299 unsigned Columns = 0;
300 for (auto &C : ContributionOffsets)
301 if (C)
302 ++Columns;
303
304 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
305 uint64_t Mask = Buckets.size() - 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000306 size_t i = 0;
307 for (const auto &P : IndexEntries) {
308 auto S = P.first;
David Blaikie24c8ac92015-12-05 03:05:45 +0000309 auto H = S & Mask;
David Blaikie9b492562016-04-05 17:51:40 +0000310 auto HP = ((S >> 32) & Mask) | 1;
David Blaikiec3826da2015-12-09 21:02:33 +0000311 while (Buckets[H]) {
David Blaikie852c02b2016-02-19 21:09:26 +0000312 assert(S != IndexEntries.begin()[Buckets[H] - 1].first &&
David Blaikie74f5b282016-02-19 01:51:44 +0000313 "Duplicate unit");
David Blaikie9b492562016-04-05 17:51:40 +0000314 H = (H + HP) & Mask;
David Blaikiec3826da2015-12-09 21:02:33 +0000315 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000316 Buckets[H] = i + 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000317 ++i;
David Blaikie24c8ac92015-12-05 03:05:45 +0000318 }
319
320 Out.SwitchSection(Section);
321 Out.EmitIntValue(2, 4); // Version
322 Out.EmitIntValue(Columns, 4); // Columns
323 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
David Blaikie2ed678c2015-12-05 03:06:30 +0000324 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets
David Blaikie24c8ac92015-12-05 03:05:45 +0000325
326 // Write the signatures.
327 for (const auto &I : Buckets)
David Blaikie852c02b2016-02-19 21:09:26 +0000328 Out.EmitIntValue(I ? IndexEntries.begin()[I - 1].first : 0, 8);
David Blaikie24c8ac92015-12-05 03:05:45 +0000329
330 // Write the indexes.
331 for (const auto &I : Buckets)
332 Out.EmitIntValue(I, 4);
333
334 // Write the column headers (which sections will appear in the table)
335 for (size_t i = 0; i != ContributionOffsets.size(); ++i)
336 if (ContributionOffsets[i])
337 Out.EmitIntValue(i + DW_SECT_INFO, 4);
338
339 // Write the offsets.
340 writeIndexTable(Out, ContributionOffsets, IndexEntries,
341 &DWARFUnitIndex::Entry::SectionContribution::Offset);
342
343 // Write the lengths.
344 writeIndexTable(Out, ContributionOffsets, IndexEntries,
345 &DWARFUnitIndex::Entry::SectionContribution::Length);
346}
David Blaikie74f5b282016-02-19 01:51:44 +0000347
David Blaikied1f7ab32016-05-16 20:42:27 +0000348std::string buildDWODescription(StringRef Name, StringRef DWPName, StringRef DWOName) {
349 std::string Text = "\'";
350 Text += Name;
351 Text += '\'';
David Blaikie4dd03f02016-03-26 20:32:14 +0000352 if (!DWPName.empty()) {
David Blaikied1f7ab32016-05-16 20:42:27 +0000353 Text += " (from ";
354 if (!DWOName.empty()) {
355 Text += '\'';
356 Text += DWOName;
357 Text += "' in ";
358 }
359 Text += '\'';
360 Text += DWPName;
361 Text += "')";
David Blaikie4dd03f02016-03-26 20:32:14 +0000362 }
David Blaikied1f7ab32016-05-16 20:42:27 +0000363 return Text;
364}
365
George Rimar8f5976e2017-01-13 15:58:55 +0000366static Error createError(StringRef Name, Error E) {
367 return make_error<DWPError>(
368 ("failure while decompressing compressed section: '" + Name + "', " +
369 llvm::toString(std::move(E)))
370 .str());
371}
372
373static Error
374handleCompressedSection(std::deque<SmallString<32>> &UncompressedSections,
375 StringRef &Name, StringRef &Contents) {
376 if (!Decompressor::isGnuStyle(Name))
Mehdi Amini41af4302016-11-11 04:28:40 +0000377 return Error::success();
George Rimar8f5976e2017-01-13 15:58:55 +0000378
379 Expected<Decompressor> Dec =
380 Decompressor::create(Name, Contents, false /*IsLE*/, false /*Is64Bit*/);
381 if (!Dec)
382 return createError(Name, Dec.takeError());
383
David Blaikie05e0d2b2016-05-23 21:58:58 +0000384 UncompressedSections.emplace_back();
George Rimarf98b9ac2017-05-18 08:00:01 +0000385 if (Error E = Dec->resizeAndDecompress(UncompressedSections.back()))
George Rimar8f5976e2017-01-13 15:58:55 +0000386 return createError(Name, std::move(E));
387
388 Name = Name.substr(2); // Drop ".z"
David Blaikie05e0d2b2016-05-23 21:58:58 +0000389 Contents = UncompressedSections.back();
Mehdi Amini41af4302016-11-11 04:28:40 +0000390 return Error::success();
David Blaikie05e0d2b2016-05-23 21:58:58 +0000391}
David Blaikied9517cb2016-05-23 22:21:10 +0000392
393static Error handleSection(
394 const StringMap<std::pair<MCSection *, DWARFSectionKind>> &KnownSections,
395 const MCSection *StrSection, const MCSection *StrOffsetSection,
396 const MCSection *TypesSection, const MCSection *CUIndexSection,
397 const MCSection *TUIndexSection, const SectionRef &Section, MCStreamer &Out,
David Blaikie1fc3e6b2016-05-25 23:37:06 +0000398 std::deque<SmallString<32>> &UncompressedSections,
David Blaikied9517cb2016-05-23 22:21:10 +0000399 uint32_t (&ContributionOffsets)[8], UnitIndexEntry &CurEntry,
400 StringRef &CurStrSection, StringRef &CurStrOffsetSection,
401 std::vector<StringRef> &CurTypesSection, StringRef &InfoSection,
402 StringRef &AbbrevSection, StringRef &CurCUIndexSection,
403 StringRef &CurTUIndexSection) {
404 if (Section.isBSS())
Mehdi Amini41af4302016-11-11 04:28:40 +0000405 return Error::success();
David Blaikied9517cb2016-05-23 22:21:10 +0000406
407 if (Section.isVirtual())
Mehdi Amini41af4302016-11-11 04:28:40 +0000408 return Error::success();
David Blaikied9517cb2016-05-23 22:21:10 +0000409
410 StringRef Name;
411 if (std::error_code Err = Section.getName(Name))
412 return errorCodeToError(Err);
413
David Blaikied9517cb2016-05-23 22:21:10 +0000414 StringRef Contents;
415 if (auto Err = Section.getContents(Contents))
416 return errorCodeToError(Err);
417
418 if (auto Err = handleCompressedSection(UncompressedSections, Name, Contents))
419 return Err;
420
George Rimar8f5976e2017-01-13 15:58:55 +0000421 Name = Name.substr(Name.find_first_not_of("._"));
422
David Blaikied9517cb2016-05-23 22:21:10 +0000423 auto SectionPair = KnownSections.find(Name);
424 if (SectionPair == KnownSections.end())
Mehdi Amini41af4302016-11-11 04:28:40 +0000425 return Error::success();
David Blaikied9517cb2016-05-23 22:21:10 +0000426
427 if (DWARFSectionKind Kind = SectionPair->second.second) {
428 auto Index = Kind - DW_SECT_INFO;
429 if (Kind != DW_SECT_TYPES) {
430 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
431 ContributionOffsets[Index] +=
432 (CurEntry.Contributions[Index].Length = Contents.size());
433 }
434
435 switch (Kind) {
436 case DW_SECT_INFO:
437 InfoSection = Contents;
438 break;
439 case DW_SECT_ABBREV:
440 AbbrevSection = Contents;
441 break;
442 default:
443 break;
444 }
445 }
446
447 MCSection *OutSection = SectionPair->second.first;
448 if (OutSection == StrOffsetSection)
449 CurStrOffsetSection = Contents;
450 else if (OutSection == StrSection)
451 CurStrSection = Contents;
452 else if (OutSection == TypesSection)
453 CurTypesSection.push_back(Contents);
454 else if (OutSection == CUIndexSection)
455 CurCUIndexSection = Contents;
456 else if (OutSection == TUIndexSection)
457 CurTUIndexSection = Contents;
458 else {
459 Out.SwitchSection(OutSection);
460 Out.EmitBytes(Contents);
461 }
Mehdi Amini41af4302016-11-11 04:28:40 +0000462 return Error::success();
David Blaikied9517cb2016-05-23 22:21:10 +0000463}
464
465static Error
466buildDuplicateError(const std::pair<uint64_t, UnitIndexEntry> &PrevE,
467 const CompileUnitIdentifiers &ID, StringRef DWPName) {
David Blaikie11825c72016-05-17 19:40:28 +0000468 return make_error<DWPError>(
469 std::string("Duplicate DWO ID (") + utohexstr(PrevE.first) + ") in " +
470 buildDWODescription(PrevE.second.Name, PrevE.second.DWPName,
471 PrevE.second.DWOName) +
472 " and " + buildDWODescription(ID.Name, DWPName, ID.DWOName));
David Blaikie4dd03f02016-03-26 20:32:14 +0000473}
David Blaikied9517cb2016-05-23 22:21:10 +0000474
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +0000475static Expected<SmallVector<std::string, 16>>
476getDWOFilenames(StringRef ExecFilename) {
477 auto ErrOrObj = object::ObjectFile::createObjectFile(ExecFilename);
478 if (!ErrOrObj)
479 return ErrOrObj.takeError();
480
481 const ObjectFile &Obj = *ErrOrObj.get().getBinary();
482 std::unique_ptr<DWARFContext> DWARFCtx = DWARFContext::create(Obj);
483
484 SmallVector<std::string, 16> DWOPaths;
485 for (const auto &CU : DWARFCtx->compile_units()) {
486 const DWARFDie &Die = CU->getUnitDIE();
487 std::string DWOName = dwarf::toString(
488 Die.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
489 if (DWOName.empty())
490 continue;
491 std::string DWOCompDir =
492 dwarf::toString(Die.find(dwarf::DW_AT_comp_dir), "");
493 if (!DWOCompDir.empty()) {
494 SmallString<16> DWOPath;
495 sys::path::append(DWOPath, DWOCompDir, DWOName);
496 DWOPaths.emplace_back(DWOPath.data(), DWOPath.size());
497 } else {
498 DWOPaths.push_back(std::move(DWOName));
499 }
500 }
501 return std::move(DWOPaths);
502}
503
David Blaikiebc8397c2016-05-12 19:59:54 +0000504static Error write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000505 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
506 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
507 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikiec3826da2015-12-09 21:02:33 +0000508 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
David Blaikie23919372016-02-06 01:15:26 +0000509 MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection();
David Blaikie8bce5a02016-02-17 07:00:24 +0000510 MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000511 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
512 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
513 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
514 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
515 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
516 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
David Blaikieb7020252015-12-04 21:16:42 +0000517 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikie23919372016-02-06 01:15:26 +0000518 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
David Blaikie8bce5a02016-02-17 07:00:24 +0000519 {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}},
520 {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}};
David Blaikieb073cb92015-12-02 06:21:34 +0000521
David Blaikie852c02b2016-02-19 21:09:26 +0000522 MapVector<uint64_t, UnitIndexEntry> IndexEntries;
523 MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000524
David Blaikieb073cb92015-12-02 06:21:34 +0000525 uint32_t ContributionOffsets[8] = {};
526
David Blaikiefd800922016-05-23 16:32:11 +0000527 DWPStringPool Strings(Out, StrSection);
528
529 SmallVector<OwningBinary<object::ObjectFile>, 128> Objects;
530 Objects.reserve(Inputs.size());
531
David Blaikie1fc3e6b2016-05-25 23:37:06 +0000532 std::deque<SmallString<32>> UncompressedSections;
David Blaikie2e9bd892016-05-23 17:35:51 +0000533
David Blaikie242b9482015-12-01 00:48:39 +0000534 for (const auto &Input : Inputs) {
535 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
536 if (!ErrOrObj)
David Blaikiebc8397c2016-05-12 19:59:54 +0000537 return ErrOrObj.takeError();
David Blaikieb073cb92015-12-02 06:21:34 +0000538
David Blaikiefd800922016-05-23 16:32:11 +0000539 auto &Obj = *ErrOrObj->getBinary();
540 Objects.push_back(std::move(*ErrOrObj));
541
David Blaikie23919372016-02-06 01:15:26 +0000542 UnitIndexEntry CurEntry = {};
David Blaikieb073cb92015-12-02 06:21:34 +0000543
David Blaikie98ad82a2015-12-01 18:07:07 +0000544 StringRef CurStrSection;
545 StringRef CurStrOffsetSection;
David Blaikie62be5ae2016-04-05 20:26:50 +0000546 std::vector<StringRef> CurTypesSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000547 StringRef InfoSection;
548 StringRef AbbrevSection;
David Blaikie23919372016-02-06 01:15:26 +0000549 StringRef CurCUIndexSection;
David Blaikie8bce5a02016-02-17 07:00:24 +0000550 StringRef CurTUIndexSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000551
David Blaikied9517cb2016-05-23 22:21:10 +0000552 for (const auto &Section : Obj.sections())
553 if (auto Err = handleSection(
554 KnownSections, StrSection, StrOffsetSection, TypesSection,
555 CUIndexSection, TUIndexSection, Section, Out,
556 UncompressedSections, ContributionOffsets, CurEntry,
557 CurStrSection, CurStrOffsetSection, CurTypesSection, InfoSection,
558 AbbrevSection, CurCUIndexSection, CurTUIndexSection))
David Blaikie05e0d2b2016-05-23 21:58:58 +0000559 return Err;
David Blaikie74f5b282016-02-19 01:51:44 +0000560
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000561 if (InfoSection.empty())
562 continue;
563
David Blaikie478c1a22016-05-23 22:38:06 +0000564 writeStringsAndOffsets(Out, Strings, StrOffsetSection, CurStrSection,
565 CurStrOffsetSection);
David Blaikie23919372016-02-06 01:15:26 +0000566
David Blaikie478c1a22016-05-23 22:38:06 +0000567 if (CurCUIndexSection.empty()) {
David Blaikie7bb62ef2016-05-16 23:26:29 +0000568 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(
David Blaikiece7c6cf2016-03-24 22:17:08 +0000569 AbbrevSection, InfoSection, CurStrOffsetSection, CurStrSection);
David Blaikie7bb62ef2016-05-16 23:26:29 +0000570 if (!EID)
571 return EID.takeError();
572 const auto &ID = *EID;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000573 auto P = IndexEntries.insert(std::make_pair(ID.Signature, CurEntry));
David Blaikied1f7ab32016-05-16 20:42:27 +0000574 if (!P.second)
David Blaikie11825c72016-05-17 19:40:28 +0000575 return buildDuplicateError(*P.first, ID, "");
David Blaikiece7c6cf2016-03-24 22:17:08 +0000576 P.first->second.Name = ID.Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000577 P.first->second.DWOName = ID.DWOName;
David Blaikie23919372016-02-06 01:15:26 +0000578 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection,
579 CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
David Blaikie478c1a22016-05-23 22:38:06 +0000580 continue;
David Blaikie23919372016-02-06 01:15:26 +0000581 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000582
David Blaikie478c1a22016-05-23 22:38:06 +0000583 DWARFUnitIndex CUIndex(DW_SECT_INFO);
584 DataExtractor CUIndexData(CurCUIndexSection, Obj.isLittleEndian(), 0);
585 if (!CUIndex.parse(CUIndexData))
586 return make_error<DWPError>("Failed to parse cu_index");
587
588 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
589 auto *I = E.getOffsets();
590 if (!I)
591 continue;
592 auto P = IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry));
593 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(
594 getSubsection(AbbrevSection, E, DW_SECT_ABBREV),
595 getSubsection(InfoSection, E, DW_SECT_INFO),
596 getSubsection(CurStrOffsetSection, E, DW_SECT_STR_OFFSETS),
597 CurStrSection);
598 if (!EID)
599 return EID.takeError();
600 const auto &ID = *EID;
601 if (!P.second)
602 return buildDuplicateError(*P.first, ID, Input);
603 auto &NewEntry = P.first->second;
604 NewEntry.Name = ID.Name;
605 NewEntry.DWOName = ID.DWOName;
606 NewEntry.DWPName = Input;
607 for (auto Kind : CUIndex.getColumnKinds()) {
608 auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO];
609 C.Offset += I->Offset;
610 C.Length = I->Length;
611 ++I;
612 }
613 }
614
615 if (!CurTypesSection.empty()) {
616 if (CurTypesSection.size() != 1)
617 return make_error<DWPError>("multiple type unit sections in .dwp file");
618 DWARFUnitIndex TUIndex(DW_SECT_TYPES);
619 DataExtractor TUIndexData(CurTUIndexSection, Obj.isLittleEndian(), 0);
620 if (!TUIndex.parse(TUIndexData))
621 return make_error<DWPError>("Failed to parse tu_index");
622 addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection,
623 CurTypesSection.front(), CurEntry,
624 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
625 }
David Blaikie242b9482015-12-01 00:48:39 +0000626 }
David Blaikieb073cb92015-12-02 06:21:34 +0000627
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000628 // Lie about there being no info contributions so the TU index only includes
629 // the type unit contribution
630 ContributionOffsets[0] = 0;
631 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
632 TypeIndexEntries);
David Blaikieb3757c02015-12-02 22:01:56 +0000633
David Blaikie24c8ac92015-12-05 03:05:45 +0000634 // Lie about the type contribution
635 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
636 // Unlie about the info contribution
637 ContributionOffsets[0] = 1;
David Blaikie7c4ffe02015-12-04 21:30:23 +0000638
David Blaikie24c8ac92015-12-05 03:05:45 +0000639 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
640 IndexEntries);
David Blaikieddb27362016-03-01 21:24:04 +0000641
Mehdi Amini41af4302016-11-11 04:28:40 +0000642 return Error::success();
David Blaikie242b9482015-12-01 00:48:39 +0000643}
644
David Blaikie17ab78e2016-05-17 23:37:44 +0000645static int error(const Twine &Error, const Twine &Context) {
646 errs() << Twine("while processing ") + Context + ":\n";
647 errs() << Twine("error: ") + Error + "\n";
648 return 1;
649}
650
David Blaikie2ed678c2015-12-05 03:06:30 +0000651int main(int argc, char **argv) {
David Blaikie242b9482015-12-01 00:48:39 +0000652
653 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
654
655 llvm::InitializeAllTargetInfos();
656 llvm::InitializeAllTargetMCs();
657 llvm::InitializeAllTargets();
658 llvm::InitializeAllAsmPrinters();
659
660 std::string ErrorStr;
661 StringRef Context = "dwarf streamer init";
662
663 Triple TheTriple("x86_64-linux-gnu");
664
665 // Get the target.
666 const Target *TheTarget =
667 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
668 if (!TheTarget)
669 return error(ErrorStr, Context);
670 std::string TripleName = TheTriple.getTriple();
671
672 // Create all the MC Objects.
673 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
674 if (!MRI)
675 return error(Twine("no register info for target ") + TripleName, Context);
676
677 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
678 if (!MAI)
679 return error("no asm info for target " + TripleName, Context);
680
681 MCObjectFileInfo MOFI;
682 MCContext MC(MAI.get(), MRI.get(), &MOFI);
Rafael Espindola9f929952017-08-02 20:32:26 +0000683 MOFI.InitMCObjectFileInfo(TheTriple, /*PIC*/ false, MC);
David Blaikie242b9482015-12-01 00:48:39 +0000684
Joel Jones373d7d32016-07-25 17:18:28 +0000685 MCTargetOptions Options;
686 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "", Options);
David Blaikie242b9482015-12-01 00:48:39 +0000687 if (!MAB)
688 return error("no asm backend for target " + TripleName, Context);
689
690 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
691 if (!MII)
692 return error("no instr info info for target " + TripleName, Context);
693
694 std::unique_ptr<MCSubtargetInfo> MSTI(
695 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
696 if (!MSTI)
697 return error("no subtarget info for target " + TripleName, Context);
698
699 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
700 if (!MCE)
701 return error("no code emitter for target " + TripleName, Context);
702
703 // Create the output file.
704 std::error_code EC;
705 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
706 if (EC)
707 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
708
David Majnemer03e2cc32015-12-21 22:09:27 +0000709 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
David Blaikie242b9482015-12-01 00:48:39 +0000710 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
Lang Hames2241ffa2017-10-11 23:34:47 +0000711 TheTriple, MC, std::unique_ptr<MCAsmBackend>(MAB), OutFile,
712 std::unique_ptr<MCCodeEmitter>(MCE), *MSTI, MCOptions.MCRelaxAll,
713 MCOptions.MCIncrementalLinkerCompatible,
David Blaikie242b9482015-12-01 00:48:39 +0000714 /*DWARFMustBeAtTheEnd*/ false));
715 if (!MS)
716 return error("no object streamer for target " + TripleName, Context);
717
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +0000718 std::vector<std::string> DWOFilenames = InputFiles;
719 for (const auto &ExecFilename : ExecFilenames) {
720 auto DWOs = getDWOFilenames(ExecFilename);
721 if (!DWOs) {
722 logAllUnhandledErrors(DWOs.takeError(), errs(), "error: ");
723 return 1;
724 }
725 DWOFilenames.insert(DWOFilenames.end(),
726 std::make_move_iterator(DWOs->begin()),
727 std::make_move_iterator(DWOs->end()));
728 }
729
730 if (auto Err = write(*MS, DWOFilenames)) {
David Blaikiebc8397c2016-05-12 19:59:54 +0000731 logAllUnhandledErrors(std::move(Err), errs(), "error: ");
732 return 1;
733 }
David Blaikieddb27362016-03-01 21:24:04 +0000734
735 MS->Finish();
David Blaikiedf055252015-12-01 00:48:34 +0000736}