blob: 3079b3fe1b543f167b7c735fa5c97e10079c2807 [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"
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +000018#include "llvm/DebugInfo/DWARF/DWARFContext.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000019#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
20#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
Lang Hames02d33052017-10-11 01:57:21 +000021#include "llvm/MC/MCAsmBackend.h"
David Blaikie242b9482015-12-01 00:48:39 +000022#include "llvm/MC/MCAsmInfo.h"
Lang Hames2241ffa2017-10-11 23:34:47 +000023#include "llvm/MC/MCCodeEmitter.h"
David Blaikie242b9482015-12-01 00:48:39 +000024#include "llvm/MC/MCContext.h"
25#include "llvm/MC/MCInstrInfo.h"
26#include "llvm/MC/MCObjectFileInfo.h"
27#include "llvm/MC/MCRegisterInfo.h"
David Blaikie242b9482015-12-01 00:48:39 +000028#include "llvm/MC/MCStreamer.h"
David Blaikie4333f972018-04-11 18:49:37 +000029#include "llvm/MC/MCTargetOptionsCommandFlags.inc"
George Rimar8f5976e2017-01-13 15:58:55 +000030#include "llvm/Object/Decompressor.h"
David Blaikie242b9482015-12-01 00:48:39 +000031#include "llvm/Object/ObjectFile.h"
David Blaikie98ad82a2015-12-01 18:07:07 +000032#include "llvm/Support/DataExtractor.h"
David Blaikiefd800922016-05-23 16:32:11 +000033#include "llvm/Support/Error.h"
David Blaikie242b9482015-12-01 00:48:39 +000034#include "llvm/Support/FileSystem.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000035#include "llvm/Support/MathExtras.h"
David Blaikie242b9482015-12-01 00:48:39 +000036#include "llvm/Support/MemoryBuffer.h"
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +000037#include "llvm/Support/Path.h"
David Blaikie242b9482015-12-01 00:48:39 +000038#include "llvm/Support/TargetRegistry.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000039#include "llvm/Support/TargetSelect.h"
David Blaikie242b9482015-12-01 00:48:39 +000040#include "llvm/Support/raw_ostream.h"
David Blaikie242b9482015-12-01 00:48:39 +000041
42using namespace llvm;
David Blaikie98ad82a2015-12-01 18:07:07 +000043using namespace llvm::object;
David Blaikie242b9482015-12-01 00:48:39 +000044using namespace cl;
45
46OptionCategory DwpCategory("Specific Options");
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +000047static list<std::string> InputFiles(Positional, ZeroOrMore,
David Blaikie242b9482015-12-01 00:48:39 +000048 desc("<input files>"), cat(DwpCategory));
49
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +000050static list<std::string> ExecFilenames(
51 "e", ZeroOrMore,
52 desc("Specify the executable/library files to get the list of *.dwo from"),
53 value_desc("filename"), cat(DwpCategory));
54
David Blaikie2ed678c2015-12-05 03:06:30 +000055static opt<std::string> OutputFilename(Required, "o",
56 desc("Specify the output file."),
57 value_desc("filename"),
58 cat(DwpCategory));
David Blaikie242b9482015-12-01 00:48:39 +000059
David Blaikiefd800922016-05-23 16:32:11 +000060static void writeStringsAndOffsets(MCStreamer &Out, DWPStringPool &Strings,
61 MCSection *StrOffsetSection,
62 StringRef CurStrSection,
63 StringRef CurStrOffsetSection) {
David Blaikie98ad82a2015-12-01 18:07:07 +000064 // Could possibly produce an error or warning if one of these was non-null but
65 // the other was null.
66 if (CurStrSection.empty() || CurStrOffsetSection.empty())
David Blaikiebc619cd2016-05-17 23:44:13 +000067 return;
David Blaikie98ad82a2015-12-01 18:07:07 +000068
69 DenseMap<uint32_t, uint32_t> OffsetRemapping;
70
71 DataExtractor Data(CurStrSection, true, 0);
72 uint32_t LocalOffset = 0;
73 uint32_t PrevOffset = 0;
74 while (const char *s = Data.getCStr(&LocalOffset)) {
David Blaikiefd800922016-05-23 16:32:11 +000075 OffsetRemapping[PrevOffset] =
76 Strings.getOffset(s, LocalOffset - PrevOffset);
David Blaikie98ad82a2015-12-01 18:07:07 +000077 PrevOffset = LocalOffset;
78 }
79
80 Data = DataExtractor(CurStrOffsetSection, true, 0);
81
82 Out.SwitchSection(StrOffsetSection);
83
84 uint32_t Offset = 0;
85 uint64_t Size = CurStrOffsetSection.size();
86 while (Offset < Size) {
87 auto OldOffset = Data.getU32(&Offset);
88 auto NewOffset = OffsetRemapping[OldOffset];
89 Out.EmitIntValue(NewOffset, 4);
90 }
David Blaikie242b9482015-12-01 00:48:39 +000091}
92
David Blaikiead07b5d2015-12-04 17:20:04 +000093static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
94 uint64_t CurCode;
95 uint32_t Offset = 0;
96 DataExtractor AbbrevData(Abbrev, true, 0);
97 while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) {
98 // Tag
99 AbbrevData.getULEB128(&Offset);
100 // DW_CHILDREN
101 AbbrevData.getU8(&Offset);
102 // Attributes
103 while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset))
104 ;
105 }
106 return Offset;
107}
108
David Blaikiece7c6cf2016-03-24 22:17:08 +0000109struct CompileUnitIdentifiers {
110 uint64_t Signature = 0;
David Blaikie4dd03f02016-03-26 20:32:14 +0000111 const char *Name = "";
112 const char *DWOName = "";
David Blaikiece7c6cf2016-03-24 22:17:08 +0000113};
114
David Blaikie4940f872016-05-17 00:07:10 +0000115static Expected<const char *>
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +0000116getIndexedString(dwarf::Form Form, DataExtractor InfoData,
Greg Clayton6c273762016-10-27 16:32:04 +0000117 uint32_t &InfoOffset, StringRef StrOffsets, StringRef Str) {
David Blaikie60fbd3b2016-04-05 20:16:38 +0000118 if (Form == dwarf::DW_FORM_string)
119 return InfoData.getCStr(&InfoOffset);
David Blaikie4940f872016-05-17 00:07:10 +0000120 if (Form != dwarf::DW_FORM_GNU_str_index)
121 return make_error<DWPError>(
122 "string field encoded without DW_FORM_string or DW_FORM_GNU_str_index");
David Blaikie60fbd3b2016-04-05 20:16:38 +0000123 auto StrIndex = InfoData.getULEB128(&InfoOffset);
David Blaikie4dd03f02016-03-26 20:32:14 +0000124 DataExtractor StrOffsetsData(StrOffsets, true, 0);
125 uint32_t StrOffsetsOffset = 4 * StrIndex;
126 uint32_t StrOffset = StrOffsetsData.getU32(&StrOffsetsOffset);
127 DataExtractor StrData(Str, true, 0);
128 return StrData.getCStr(&StrOffset);
129}
130
David Blaikie7bb62ef2016-05-16 23:26:29 +0000131static Expected<CompileUnitIdentifiers> getCUIdentifiers(StringRef Abbrev,
132 StringRef Info,
133 StringRef StrOffsets,
134 StringRef Str) {
David Blaikief1958da2016-02-26 07:30:15 +0000135 uint32_t Offset = 0;
136 DataExtractor InfoData(Info, true, 0);
Greg Clayton82f12b12016-11-11 16:21:37 +0000137 dwarf::DwarfFormat Format = dwarf::DwarfFormat::DWARF32;
138 uint64_t Length = InfoData.getU32(&Offset);
139 // If the length is 0xffffffff, then this indictes that this is a DWARF 64
140 // stream and the length is actually encoded into a 64 bit value that follows.
141 if (Length == 0xffffffffU) {
142 Format = dwarf::DwarfFormat::DWARF64;
143 Length = InfoData.getU64(&Offset);
144 }
David Blaikief1958da2016-02-26 07:30:15 +0000145 uint16_t Version = InfoData.getU16(&Offset);
146 InfoData.getU32(&Offset); // Abbrev offset (should be zero)
147 uint8_t AddrSize = InfoData.getU8(&Offset);
148
149 uint32_t AbbrCode = InfoData.getULEB128(&Offset);
150
151 DataExtractor AbbrevData(Abbrev, true, 0);
152 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
Greg Clayton6c273762016-10-27 16:32:04 +0000153 auto Tag = static_cast<dwarf::Tag>(AbbrevData.getULEB128(&AbbrevOffset));
David Blaikie7bb62ef2016-05-16 23:26:29 +0000154 if (Tag != dwarf::DW_TAG_compile_unit)
155 return make_error<DWPError>("top level DIE is not a compile unit");
David Blaikief1958da2016-02-26 07:30:15 +0000156 // DW_CHILDREN
157 AbbrevData.getU8(&AbbrevOffset);
158 uint32_t Name;
Greg Clayton6c273762016-10-27 16:32:04 +0000159 dwarf::Form Form;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000160 CompileUnitIdentifiers ID;
David Blaikief1958da2016-02-26 07:30:15 +0000161 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |
Greg Clayton6c273762016-10-27 16:32:04 +0000162 (Form = static_cast<dwarf::Form>(AbbrevData.getULEB128(&AbbrevOffset))) &&
David Blaikiece7c6cf2016-03-24 22:17:08 +0000163 (Name != 0 || Form != 0)) {
164 switch (Name) {
165 case dwarf::DW_AT_name: {
David Blaikie4940f872016-05-17 00:07:10 +0000166 Expected<const char *> EName =
167 getIndexedString(Form, InfoData, Offset, StrOffsets, Str);
168 if (!EName)
169 return EName.takeError();
170 ID.Name = *EName;
David Blaikie4dd03f02016-03-26 20:32:14 +0000171 break;
172 }
173 case dwarf::DW_AT_GNU_dwo_name: {
David Blaikie4940f872016-05-17 00:07:10 +0000174 Expected<const char *> EName =
175 getIndexedString(Form, InfoData, Offset, StrOffsets, Str);
176 if (!EName)
177 return EName.takeError();
178 ID.DWOName = *EName;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000179 break;
180 }
181 case dwarf::DW_AT_GNU_dwo_id:
182 ID.Signature = InfoData.getU64(&Offset);
183 break;
184 default:
Paul Robinson75c068c2017-06-26 18:43:01 +0000185 DWARFFormValue::skipValue(Form, InfoData, &Offset,
Pavel Labath322711f2018-03-14 09:39:54 +0000186 dwarf::FormParams({Version, AddrSize, Format}));
David Blaikiece7c6cf2016-03-24 22:17:08 +0000187 }
David Blaikief1958da2016-02-26 07:30:15 +0000188 }
David Blaikiece7c6cf2016-03-24 22:17:08 +0000189 return ID;
David Blaikiead07b5d2015-12-04 17:20:04 +0000190}
191
David Blaikie24c8ac92015-12-05 03:05:45 +0000192struct UnitIndexEntry {
David Blaikie24c8ac92015-12-05 03:05:45 +0000193 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
David Blaikief1958da2016-02-26 07:30:15 +0000194 std::string Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000195 std::string DWOName;
David Blaikief1958da2016-02-26 07:30:15 +0000196 StringRef DWPName;
David Blaikie24c8ac92015-12-05 03:05:45 +0000197};
198
David Blaikiefd800922016-05-23 16:32:11 +0000199static StringRef getSubsection(StringRef Section,
200 const DWARFUnitIndex::Entry &Entry,
201 DWARFSectionKind Kind) {
David Blaikief1958da2016-02-26 07:30:15 +0000202 const auto *Off = Entry.getOffset(Kind);
203 if (!Off)
204 return StringRef();
205 return Section.substr(Off->Offset, Off->Length);
206}
207
David Blaikie852c02b2016-02-19 21:09:26 +0000208static void addAllTypesFromDWP(
209 MCStreamer &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
210 const DWARFUnitIndex &TUIndex, MCSection *OutputTypes, StringRef Types,
211 const UnitIndexEntry &TUEntry, uint32_t &TypesOffset) {
David Blaikie8bce5a02016-02-17 07:00:24 +0000212 Out.SwitchSection(OutputTypes);
213 for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) {
214 auto *I = E.getOffsets();
215 if (!I)
216 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000217 auto P = TypeIndexEntries.insert(std::make_pair(E.getSignature(), TUEntry));
218 if (!P.second)
David Blaikie8bce5a02016-02-17 07:00:24 +0000219 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000220 auto &Entry = P.first->second;
David Blaikie8bce5a02016-02-17 07:00:24 +0000221 // Zero out the debug_info contribution
222 Entry.Contributions[0] = {};
223 for (auto Kind : TUIndex.getColumnKinds()) {
224 auto &C = Entry.Contributions[Kind - DW_SECT_INFO];
225 C.Offset += I->Offset;
226 C.Length = I->Length;
227 ++I;
228 }
229 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
230 Out.EmitBytes(Types.substr(
231 C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset,
232 C.Length));
233 C.Offset = TypesOffset;
234 TypesOffset += C.Length;
David Blaikie8bce5a02016-02-17 07:00:24 +0000235 }
236}
237
David Blaikiec3826da2015-12-09 21:02:33 +0000238static void addAllTypes(MCStreamer &Out,
David Blaikie852c02b2016-02-19 21:09:26 +0000239 MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
David Blaikie62be5ae2016-04-05 20:26:50 +0000240 MCSection *OutputTypes,
241 const std::vector<StringRef> &TypesSections,
David Blaikiec3826da2015-12-09 21:02:33 +0000242 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) {
David Blaikie62be5ae2016-04-05 20:26:50 +0000243 for (StringRef Types : TypesSections) {
244 Out.SwitchSection(OutputTypes);
245 uint32_t Offset = 0;
246 DataExtractor Data(Types, true, 0);
247 while (Data.isValidOffset(Offset)) {
248 UnitIndexEntry Entry = CUEntry;
249 // Zero out the debug_info contribution
250 Entry.Contributions[0] = {};
251 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
252 C.Offset = TypesOffset;
253 auto PrevOffset = Offset;
254 // Length of the unit, including the 4 byte length field.
255 C.Length = Data.getU32(&Offset) + 4;
David Blaikiec3826da2015-12-09 21:02:33 +0000256
David Blaikie62be5ae2016-04-05 20:26:50 +0000257 Data.getU16(&Offset); // Version
258 Data.getU32(&Offset); // Abbrev offset
259 Data.getU8(&Offset); // Address size
260 auto Signature = Data.getU64(&Offset);
261 Offset = PrevOffset + C.Length;
David Blaikie24c8ac92015-12-05 03:05:45 +0000262
David Blaikie62be5ae2016-04-05 20:26:50 +0000263 auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry));
264 if (!P.second)
265 continue;
David Blaikief5cb6272015-12-14 07:42:00 +0000266
David Blaikie62be5ae2016-04-05 20:26:50 +0000267 Out.EmitBytes(Types.substr(PrevOffset, C.Length));
268 TypesOffset += C.Length;
269 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000270 }
271}
272
273static void
274writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,
David Blaikie852c02b2016-02-19 21:09:26 +0000275 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries,
David Blaikie24c8ac92015-12-05 03:05:45 +0000276 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) {
277 for (const auto &E : IndexEntries)
David Blaikie852c02b2016-02-19 21:09:26 +0000278 for (size_t i = 0; i != array_lengthof(E.second.Contributions); ++i)
David Blaikie24c8ac92015-12-05 03:05:45 +0000279 if (ContributionOffsets[i])
David Blaikie852c02b2016-02-19 21:09:26 +0000280 Out.EmitIntValue(E.second.Contributions[i].*Field, 4);
David Blaikie24c8ac92015-12-05 03:05:45 +0000281}
282
David Blaikie852c02b2016-02-19 21:09:26 +0000283static void
284writeIndex(MCStreamer &Out, MCSection *Section,
285 ArrayRef<unsigned> ContributionOffsets,
286 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries) {
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000287 if (IndexEntries.empty())
288 return;
289
David Blaikie24c8ac92015-12-05 03:05:45 +0000290 unsigned Columns = 0;
291 for (auto &C : ContributionOffsets)
292 if (C)
293 ++Columns;
294
295 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
296 uint64_t Mask = Buckets.size() - 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000297 size_t i = 0;
298 for (const auto &P : IndexEntries) {
299 auto S = P.first;
David Blaikie24c8ac92015-12-05 03:05:45 +0000300 auto H = S & Mask;
David Blaikie9b492562016-04-05 17:51:40 +0000301 auto HP = ((S >> 32) & Mask) | 1;
David Blaikiec3826da2015-12-09 21:02:33 +0000302 while (Buckets[H]) {
David Blaikie852c02b2016-02-19 21:09:26 +0000303 assert(S != IndexEntries.begin()[Buckets[H] - 1].first &&
David Blaikie74f5b282016-02-19 01:51:44 +0000304 "Duplicate unit");
David Blaikie9b492562016-04-05 17:51:40 +0000305 H = (H + HP) & Mask;
David Blaikiec3826da2015-12-09 21:02:33 +0000306 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000307 Buckets[H] = i + 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000308 ++i;
David Blaikie24c8ac92015-12-05 03:05:45 +0000309 }
310
311 Out.SwitchSection(Section);
312 Out.EmitIntValue(2, 4); // Version
313 Out.EmitIntValue(Columns, 4); // Columns
314 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
David Blaikie2ed678c2015-12-05 03:06:30 +0000315 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets
David Blaikie24c8ac92015-12-05 03:05:45 +0000316
317 // Write the signatures.
318 for (const auto &I : Buckets)
David Blaikie852c02b2016-02-19 21:09:26 +0000319 Out.EmitIntValue(I ? IndexEntries.begin()[I - 1].first : 0, 8);
David Blaikie24c8ac92015-12-05 03:05:45 +0000320
321 // Write the indexes.
322 for (const auto &I : Buckets)
323 Out.EmitIntValue(I, 4);
324
325 // Write the column headers (which sections will appear in the table)
326 for (size_t i = 0; i != ContributionOffsets.size(); ++i)
327 if (ContributionOffsets[i])
328 Out.EmitIntValue(i + DW_SECT_INFO, 4);
329
330 // Write the offsets.
331 writeIndexTable(Out, ContributionOffsets, IndexEntries,
332 &DWARFUnitIndex::Entry::SectionContribution::Offset);
333
334 // Write the lengths.
335 writeIndexTable(Out, ContributionOffsets, IndexEntries,
336 &DWARFUnitIndex::Entry::SectionContribution::Length);
337}
David Blaikie74f5b282016-02-19 01:51:44 +0000338
David Blaikied1f7ab32016-05-16 20:42:27 +0000339std::string buildDWODescription(StringRef Name, StringRef DWPName, StringRef DWOName) {
340 std::string Text = "\'";
341 Text += Name;
342 Text += '\'';
David Blaikie4dd03f02016-03-26 20:32:14 +0000343 if (!DWPName.empty()) {
David Blaikied1f7ab32016-05-16 20:42:27 +0000344 Text += " (from ";
345 if (!DWOName.empty()) {
346 Text += '\'';
347 Text += DWOName;
348 Text += "' in ";
349 }
350 Text += '\'';
351 Text += DWPName;
352 Text += "')";
David Blaikie4dd03f02016-03-26 20:32:14 +0000353 }
David Blaikied1f7ab32016-05-16 20:42:27 +0000354 return Text;
355}
356
George Rimar8f5976e2017-01-13 15:58:55 +0000357static Error createError(StringRef Name, Error E) {
358 return make_error<DWPError>(
359 ("failure while decompressing compressed section: '" + Name + "', " +
360 llvm::toString(std::move(E)))
361 .str());
362}
363
364static Error
365handleCompressedSection(std::deque<SmallString<32>> &UncompressedSections,
366 StringRef &Name, StringRef &Contents) {
367 if (!Decompressor::isGnuStyle(Name))
Mehdi Amini41af4302016-11-11 04:28:40 +0000368 return Error::success();
George Rimar8f5976e2017-01-13 15:58:55 +0000369
370 Expected<Decompressor> Dec =
371 Decompressor::create(Name, Contents, false /*IsLE*/, false /*Is64Bit*/);
372 if (!Dec)
373 return createError(Name, Dec.takeError());
374
David Blaikie05e0d2b2016-05-23 21:58:58 +0000375 UncompressedSections.emplace_back();
George Rimarf98b9ac2017-05-18 08:00:01 +0000376 if (Error E = Dec->resizeAndDecompress(UncompressedSections.back()))
George Rimar8f5976e2017-01-13 15:58:55 +0000377 return createError(Name, std::move(E));
378
379 Name = Name.substr(2); // Drop ".z"
David Blaikie05e0d2b2016-05-23 21:58:58 +0000380 Contents = UncompressedSections.back();
Mehdi Amini41af4302016-11-11 04:28:40 +0000381 return Error::success();
David Blaikie05e0d2b2016-05-23 21:58:58 +0000382}
David Blaikied9517cb2016-05-23 22:21:10 +0000383
384static Error handleSection(
385 const StringMap<std::pair<MCSection *, DWARFSectionKind>> &KnownSections,
386 const MCSection *StrSection, const MCSection *StrOffsetSection,
387 const MCSection *TypesSection, const MCSection *CUIndexSection,
388 const MCSection *TUIndexSection, const SectionRef &Section, MCStreamer &Out,
David Blaikie1fc3e6b2016-05-25 23:37:06 +0000389 std::deque<SmallString<32>> &UncompressedSections,
David Blaikied9517cb2016-05-23 22:21:10 +0000390 uint32_t (&ContributionOffsets)[8], UnitIndexEntry &CurEntry,
391 StringRef &CurStrSection, StringRef &CurStrOffsetSection,
392 std::vector<StringRef> &CurTypesSection, StringRef &InfoSection,
393 StringRef &AbbrevSection, StringRef &CurCUIndexSection,
394 StringRef &CurTUIndexSection) {
395 if (Section.isBSS())
Mehdi Amini41af4302016-11-11 04:28:40 +0000396 return Error::success();
David Blaikied9517cb2016-05-23 22:21:10 +0000397
398 if (Section.isVirtual())
Mehdi Amini41af4302016-11-11 04:28:40 +0000399 return Error::success();
David Blaikied9517cb2016-05-23 22:21:10 +0000400
401 StringRef Name;
402 if (std::error_code Err = Section.getName(Name))
403 return errorCodeToError(Err);
404
David Blaikied9517cb2016-05-23 22:21:10 +0000405 StringRef Contents;
406 if (auto Err = Section.getContents(Contents))
407 return errorCodeToError(Err);
408
409 if (auto Err = handleCompressedSection(UncompressedSections, Name, Contents))
410 return Err;
411
George Rimar8f5976e2017-01-13 15:58:55 +0000412 Name = Name.substr(Name.find_first_not_of("._"));
413
David Blaikied9517cb2016-05-23 22:21:10 +0000414 auto SectionPair = KnownSections.find(Name);
415 if (SectionPair == KnownSections.end())
Mehdi Amini41af4302016-11-11 04:28:40 +0000416 return Error::success();
David Blaikied9517cb2016-05-23 22:21:10 +0000417
418 if (DWARFSectionKind Kind = SectionPair->second.second) {
419 auto Index = Kind - DW_SECT_INFO;
420 if (Kind != DW_SECT_TYPES) {
421 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
422 ContributionOffsets[Index] +=
423 (CurEntry.Contributions[Index].Length = Contents.size());
424 }
425
426 switch (Kind) {
427 case DW_SECT_INFO:
428 InfoSection = Contents;
429 break;
430 case DW_SECT_ABBREV:
431 AbbrevSection = Contents;
432 break;
433 default:
434 break;
435 }
436 }
437
438 MCSection *OutSection = SectionPair->second.first;
439 if (OutSection == StrOffsetSection)
440 CurStrOffsetSection = Contents;
441 else if (OutSection == StrSection)
442 CurStrSection = Contents;
443 else if (OutSection == TypesSection)
444 CurTypesSection.push_back(Contents);
445 else if (OutSection == CUIndexSection)
446 CurCUIndexSection = Contents;
447 else if (OutSection == TUIndexSection)
448 CurTUIndexSection = Contents;
449 else {
450 Out.SwitchSection(OutSection);
451 Out.EmitBytes(Contents);
452 }
Mehdi Amini41af4302016-11-11 04:28:40 +0000453 return Error::success();
David Blaikied9517cb2016-05-23 22:21:10 +0000454}
455
456static Error
457buildDuplicateError(const std::pair<uint64_t, UnitIndexEntry> &PrevE,
458 const CompileUnitIdentifiers &ID, StringRef DWPName) {
David Blaikie11825c72016-05-17 19:40:28 +0000459 return make_error<DWPError>(
460 std::string("Duplicate DWO ID (") + utohexstr(PrevE.first) + ") in " +
461 buildDWODescription(PrevE.second.Name, PrevE.second.DWPName,
462 PrevE.second.DWOName) +
463 " and " + buildDWODescription(ID.Name, DWPName, ID.DWOName));
David Blaikie4dd03f02016-03-26 20:32:14 +0000464}
David Blaikied9517cb2016-05-23 22:21:10 +0000465
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +0000466static Expected<SmallVector<std::string, 16>>
467getDWOFilenames(StringRef ExecFilename) {
468 auto ErrOrObj = object::ObjectFile::createObjectFile(ExecFilename);
469 if (!ErrOrObj)
470 return ErrOrObj.takeError();
471
472 const ObjectFile &Obj = *ErrOrObj.get().getBinary();
473 std::unique_ptr<DWARFContext> DWARFCtx = DWARFContext::create(Obj);
474
475 SmallVector<std::string, 16> DWOPaths;
476 for (const auto &CU : DWARFCtx->compile_units()) {
477 const DWARFDie &Die = CU->getUnitDIE();
478 std::string DWOName = dwarf::toString(
479 Die.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
480 if (DWOName.empty())
481 continue;
482 std::string DWOCompDir =
483 dwarf::toString(Die.find(dwarf::DW_AT_comp_dir), "");
484 if (!DWOCompDir.empty()) {
485 SmallString<16> DWOPath;
486 sys::path::append(DWOPath, DWOCompDir, DWOName);
487 DWOPaths.emplace_back(DWOPath.data(), DWOPath.size());
488 } else {
489 DWOPaths.push_back(std::move(DWOName));
490 }
491 }
492 return std::move(DWOPaths);
493}
494
David Blaikiebc8397c2016-05-12 19:59:54 +0000495static Error write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000496 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
497 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
498 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikiec3826da2015-12-09 21:02:33 +0000499 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
David Blaikie23919372016-02-06 01:15:26 +0000500 MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection();
David Blaikie8bce5a02016-02-17 07:00:24 +0000501 MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000502 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
503 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
504 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
505 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
506 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
507 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
David Blaikieb7020252015-12-04 21:16:42 +0000508 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikie23919372016-02-06 01:15:26 +0000509 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
David Blaikie8bce5a02016-02-17 07:00:24 +0000510 {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}},
511 {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}};
David Blaikieb073cb92015-12-02 06:21:34 +0000512
David Blaikie852c02b2016-02-19 21:09:26 +0000513 MapVector<uint64_t, UnitIndexEntry> IndexEntries;
514 MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000515
David Blaikieb073cb92015-12-02 06:21:34 +0000516 uint32_t ContributionOffsets[8] = {};
517
David Blaikiefd800922016-05-23 16:32:11 +0000518 DWPStringPool Strings(Out, StrSection);
519
520 SmallVector<OwningBinary<object::ObjectFile>, 128> Objects;
521 Objects.reserve(Inputs.size());
522
David Blaikie1fc3e6b2016-05-25 23:37:06 +0000523 std::deque<SmallString<32>> UncompressedSections;
David Blaikie2e9bd892016-05-23 17:35:51 +0000524
David Blaikie242b9482015-12-01 00:48:39 +0000525 for (const auto &Input : Inputs) {
526 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
527 if (!ErrOrObj)
David Blaikiebc8397c2016-05-12 19:59:54 +0000528 return ErrOrObj.takeError();
David Blaikieb073cb92015-12-02 06:21:34 +0000529
David Blaikiefd800922016-05-23 16:32:11 +0000530 auto &Obj = *ErrOrObj->getBinary();
531 Objects.push_back(std::move(*ErrOrObj));
532
David Blaikie23919372016-02-06 01:15:26 +0000533 UnitIndexEntry CurEntry = {};
David Blaikieb073cb92015-12-02 06:21:34 +0000534
David Blaikie98ad82a2015-12-01 18:07:07 +0000535 StringRef CurStrSection;
536 StringRef CurStrOffsetSection;
David Blaikie62be5ae2016-04-05 20:26:50 +0000537 std::vector<StringRef> CurTypesSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000538 StringRef InfoSection;
539 StringRef AbbrevSection;
David Blaikie23919372016-02-06 01:15:26 +0000540 StringRef CurCUIndexSection;
David Blaikie8bce5a02016-02-17 07:00:24 +0000541 StringRef CurTUIndexSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000542
David Blaikied9517cb2016-05-23 22:21:10 +0000543 for (const auto &Section : Obj.sections())
544 if (auto Err = handleSection(
545 KnownSections, StrSection, StrOffsetSection, TypesSection,
546 CUIndexSection, TUIndexSection, Section, Out,
547 UncompressedSections, ContributionOffsets, CurEntry,
548 CurStrSection, CurStrOffsetSection, CurTypesSection, InfoSection,
549 AbbrevSection, CurCUIndexSection, CurTUIndexSection))
David Blaikie05e0d2b2016-05-23 21:58:58 +0000550 return Err;
David Blaikie74f5b282016-02-19 01:51:44 +0000551
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000552 if (InfoSection.empty())
553 continue;
554
David Blaikie478c1a22016-05-23 22:38:06 +0000555 writeStringsAndOffsets(Out, Strings, StrOffsetSection, CurStrSection,
556 CurStrOffsetSection);
David Blaikie23919372016-02-06 01:15:26 +0000557
David Blaikie478c1a22016-05-23 22:38:06 +0000558 if (CurCUIndexSection.empty()) {
David Blaikie7bb62ef2016-05-16 23:26:29 +0000559 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(
David Blaikiece7c6cf2016-03-24 22:17:08 +0000560 AbbrevSection, InfoSection, CurStrOffsetSection, CurStrSection);
David Blaikie7bb62ef2016-05-16 23:26:29 +0000561 if (!EID)
562 return EID.takeError();
563 const auto &ID = *EID;
David Blaikiece7c6cf2016-03-24 22:17:08 +0000564 auto P = IndexEntries.insert(std::make_pair(ID.Signature, CurEntry));
David Blaikied1f7ab32016-05-16 20:42:27 +0000565 if (!P.second)
David Blaikie11825c72016-05-17 19:40:28 +0000566 return buildDuplicateError(*P.first, ID, "");
David Blaikiece7c6cf2016-03-24 22:17:08 +0000567 P.first->second.Name = ID.Name;
David Blaikie4dd03f02016-03-26 20:32:14 +0000568 P.first->second.DWOName = ID.DWOName;
David Blaikie23919372016-02-06 01:15:26 +0000569 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection,
570 CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
David Blaikie478c1a22016-05-23 22:38:06 +0000571 continue;
David Blaikie23919372016-02-06 01:15:26 +0000572 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000573
David Blaikie478c1a22016-05-23 22:38:06 +0000574 DWARFUnitIndex CUIndex(DW_SECT_INFO);
575 DataExtractor CUIndexData(CurCUIndexSection, Obj.isLittleEndian(), 0);
576 if (!CUIndex.parse(CUIndexData))
577 return make_error<DWPError>("Failed to parse cu_index");
578
579 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
580 auto *I = E.getOffsets();
581 if (!I)
582 continue;
583 auto P = IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry));
584 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(
585 getSubsection(AbbrevSection, E, DW_SECT_ABBREV),
586 getSubsection(InfoSection, E, DW_SECT_INFO),
587 getSubsection(CurStrOffsetSection, E, DW_SECT_STR_OFFSETS),
588 CurStrSection);
589 if (!EID)
590 return EID.takeError();
591 const auto &ID = *EID;
592 if (!P.second)
593 return buildDuplicateError(*P.first, ID, Input);
594 auto &NewEntry = P.first->second;
595 NewEntry.Name = ID.Name;
596 NewEntry.DWOName = ID.DWOName;
597 NewEntry.DWPName = Input;
598 for (auto Kind : CUIndex.getColumnKinds()) {
599 auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO];
600 C.Offset += I->Offset;
601 C.Length = I->Length;
602 ++I;
603 }
604 }
605
606 if (!CurTypesSection.empty()) {
607 if (CurTypesSection.size() != 1)
608 return make_error<DWPError>("multiple type unit sections in .dwp file");
609 DWARFUnitIndex TUIndex(DW_SECT_TYPES);
610 DataExtractor TUIndexData(CurTUIndexSection, Obj.isLittleEndian(), 0);
611 if (!TUIndex.parse(TUIndexData))
612 return make_error<DWPError>("Failed to parse tu_index");
613 addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection,
614 CurTypesSection.front(), CurEntry,
615 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
616 }
David Blaikie242b9482015-12-01 00:48:39 +0000617 }
David Blaikieb073cb92015-12-02 06:21:34 +0000618
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000619 // Lie about there being no info contributions so the TU index only includes
620 // the type unit contribution
621 ContributionOffsets[0] = 0;
622 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
623 TypeIndexEntries);
David Blaikieb3757c02015-12-02 22:01:56 +0000624
David Blaikie24c8ac92015-12-05 03:05:45 +0000625 // Lie about the type contribution
626 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
627 // Unlie about the info contribution
628 ContributionOffsets[0] = 1;
David Blaikie7c4ffe02015-12-04 21:30:23 +0000629
David Blaikie24c8ac92015-12-05 03:05:45 +0000630 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
631 IndexEntries);
David Blaikieddb27362016-03-01 21:24:04 +0000632
Mehdi Amini41af4302016-11-11 04:28:40 +0000633 return Error::success();
David Blaikie242b9482015-12-01 00:48:39 +0000634}
635
David Blaikie17ab78e2016-05-17 23:37:44 +0000636static int error(const Twine &Error, const Twine &Context) {
637 errs() << Twine("while processing ") + Context + ":\n";
638 errs() << Twine("error: ") + Error + "\n";
639 return 1;
640}
641
David Blaikie2ed678c2015-12-05 03:06:30 +0000642int main(int argc, char **argv) {
David Blaikie242b9482015-12-01 00:48:39 +0000643
644 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
645
646 llvm::InitializeAllTargetInfos();
647 llvm::InitializeAllTargetMCs();
648 llvm::InitializeAllTargets();
649 llvm::InitializeAllAsmPrinters();
650
651 std::string ErrorStr;
652 StringRef Context = "dwarf streamer init";
653
654 Triple TheTriple("x86_64-linux-gnu");
655
656 // Get the target.
657 const Target *TheTarget =
658 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
659 if (!TheTarget)
660 return error(ErrorStr, Context);
661 std::string TripleName = TheTriple.getTriple();
662
663 // Create all the MC Objects.
664 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
665 if (!MRI)
666 return error(Twine("no register info for target ") + TripleName, Context);
667
668 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
669 if (!MAI)
670 return error("no asm info for target " + TripleName, Context);
671
672 MCObjectFileInfo MOFI;
673 MCContext MC(MAI.get(), MRI.get(), &MOFI);
Rafael Espindola9f929952017-08-02 20:32:26 +0000674 MOFI.InitMCObjectFileInfo(TheTriple, /*PIC*/ false, MC);
David Blaikie242b9482015-12-01 00:48:39 +0000675
Alex Bradburyb22f7512018-01-03 08:53:05 +0000676 std::unique_ptr<MCSubtargetInfo> MSTI(
677 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
678 if (!MSTI)
679 return error("no subtarget info for target " + TripleName, Context);
680
Joel Jones373d7d32016-07-25 17:18:28 +0000681 MCTargetOptions Options;
Alex Bradburyb22f7512018-01-03 08:53:05 +0000682 auto MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, Options);
David Blaikie242b9482015-12-01 00:48:39 +0000683 if (!MAB)
684 return error("no asm backend for target " + TripleName, Context);
685
686 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
687 if (!MII)
688 return error("no instr info info for target " + TripleName, Context);
689
David Blaikie242b9482015-12-01 00:48:39 +0000690 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
691 if (!MCE)
692 return error("no code emitter for target " + TripleName, Context);
693
694 // Create the output file.
695 std::error_code EC;
696 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
697 if (EC)
698 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
699
David Majnemer03e2cc32015-12-21 22:09:27 +0000700 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
David Blaikie242b9482015-12-01 00:48:39 +0000701 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
Lang Hames2241ffa2017-10-11 23:34:47 +0000702 TheTriple, MC, std::unique_ptr<MCAsmBackend>(MAB), OutFile,
703 std::unique_ptr<MCCodeEmitter>(MCE), *MSTI, MCOptions.MCRelaxAll,
704 MCOptions.MCIncrementalLinkerCompatible,
David Blaikie242b9482015-12-01 00:48:39 +0000705 /*DWARFMustBeAtTheEnd*/ false));
706 if (!MS)
707 return error("no object streamer for target " + TripleName, Context);
708
Alexander Shaposhnikovf1f9c342017-09-02 08:19:01 +0000709 std::vector<std::string> DWOFilenames = InputFiles;
710 for (const auto &ExecFilename : ExecFilenames) {
711 auto DWOs = getDWOFilenames(ExecFilename);
712 if (!DWOs) {
713 logAllUnhandledErrors(DWOs.takeError(), errs(), "error: ");
714 return 1;
715 }
716 DWOFilenames.insert(DWOFilenames.end(),
717 std::make_move_iterator(DWOs->begin()),
718 std::make_move_iterator(DWOs->end()));
719 }
720
721 if (auto Err = write(*MS, DWOFilenames)) {
David Blaikiebc8397c2016-05-12 19:59:54 +0000722 logAllUnhandledErrors(std::move(Err), errs(), "error: ");
723 return 1;
724 }
David Blaikieddb27362016-03-01 21:24:04 +0000725
726 MS->Finish();
David Blaikiedf055252015-12-01 00:48:34 +0000727}