xref: /freebsd-src/contrib/llvm-project/llvm/tools/llvm-dwp/llvm-dwp.cpp (revision 62cfcf62f627e5093fb37026a6d8c98e4d2ef04c)
1*62cfcf62SDimitry Andric //===-- llvm-dwp.cpp - Split DWARF merging tool for llvm ------------------===//
2*62cfcf62SDimitry Andric //
3*62cfcf62SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*62cfcf62SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*62cfcf62SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*62cfcf62SDimitry Andric //
7*62cfcf62SDimitry Andric //===----------------------------------------------------------------------===//
8*62cfcf62SDimitry Andric //
9*62cfcf62SDimitry Andric // A utility for merging DWARF 5 Split DWARF .dwo files into .dwp (DWARF
10*62cfcf62SDimitry Andric // package files).
11*62cfcf62SDimitry Andric //
12*62cfcf62SDimitry Andric //===----------------------------------------------------------------------===//
13*62cfcf62SDimitry Andric #include "DWPError.h"
14*62cfcf62SDimitry Andric #include "DWPStringPool.h"
15*62cfcf62SDimitry Andric #include "llvm/ADT/MapVector.h"
16*62cfcf62SDimitry Andric #include "llvm/ADT/Optional.h"
17*62cfcf62SDimitry Andric #include "llvm/ADT/STLExtras.h"
18*62cfcf62SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFContext.h"
19*62cfcf62SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
20*62cfcf62SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
21*62cfcf62SDimitry Andric #include "llvm/MC/MCAsmBackend.h"
22*62cfcf62SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
23*62cfcf62SDimitry Andric #include "llvm/MC/MCCodeEmitter.h"
24*62cfcf62SDimitry Andric #include "llvm/MC/MCContext.h"
25*62cfcf62SDimitry Andric #include "llvm/MC/MCInstrInfo.h"
26*62cfcf62SDimitry Andric #include "llvm/MC/MCObjectFileInfo.h"
27*62cfcf62SDimitry Andric #include "llvm/MC/MCObjectWriter.h"
28*62cfcf62SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
29*62cfcf62SDimitry Andric #include "llvm/MC/MCStreamer.h"
30*62cfcf62SDimitry Andric #include "llvm/MC/MCTargetOptionsCommandFlags.inc"
31*62cfcf62SDimitry Andric #include "llvm/Object/Decompressor.h"
32*62cfcf62SDimitry Andric #include "llvm/Object/ObjectFile.h"
33*62cfcf62SDimitry Andric #include "llvm/Support/DataExtractor.h"
34*62cfcf62SDimitry Andric #include "llvm/Support/Error.h"
35*62cfcf62SDimitry Andric #include "llvm/Support/FileSystem.h"
36*62cfcf62SDimitry Andric #include "llvm/Support/InitLLVM.h"
37*62cfcf62SDimitry Andric #include "llvm/Support/MathExtras.h"
38*62cfcf62SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
39*62cfcf62SDimitry Andric #include "llvm/Support/Path.h"
40*62cfcf62SDimitry Andric #include "llvm/Support/TargetRegistry.h"
41*62cfcf62SDimitry Andric #include "llvm/Support/TargetSelect.h"
42*62cfcf62SDimitry Andric #include "llvm/Support/ToolOutputFile.h"
43*62cfcf62SDimitry Andric #include "llvm/Support/WithColor.h"
44*62cfcf62SDimitry Andric #include "llvm/Support/raw_ostream.h"
45*62cfcf62SDimitry Andric 
46*62cfcf62SDimitry Andric using namespace llvm;
47*62cfcf62SDimitry Andric using namespace llvm::object;
48*62cfcf62SDimitry Andric 
49*62cfcf62SDimitry Andric cl::OptionCategory DwpCategory("Specific Options");
50*62cfcf62SDimitry Andric static cl::list<std::string> InputFiles(cl::Positional, cl::ZeroOrMore,
51*62cfcf62SDimitry Andric                                         cl::desc("<input files>"),
52*62cfcf62SDimitry Andric                                         cl::cat(DwpCategory));
53*62cfcf62SDimitry Andric 
54*62cfcf62SDimitry Andric static cl::list<std::string> ExecFilenames(
55*62cfcf62SDimitry Andric     "e", cl::ZeroOrMore,
56*62cfcf62SDimitry Andric     cl::desc("Specify the executable/library files to get the list of *.dwo from"),
57*62cfcf62SDimitry Andric     cl::value_desc("filename"), cl::cat(DwpCategory));
58*62cfcf62SDimitry Andric 
59*62cfcf62SDimitry Andric static cl::opt<std::string> OutputFilename(cl::Required, "o",
60*62cfcf62SDimitry Andric                                            cl::desc("Specify the output file."),
61*62cfcf62SDimitry Andric                                            cl::value_desc("filename"),
62*62cfcf62SDimitry Andric                                            cl::cat(DwpCategory));
63*62cfcf62SDimitry Andric 
64*62cfcf62SDimitry Andric static void writeStringsAndOffsets(MCStreamer &Out, DWPStringPool &Strings,
65*62cfcf62SDimitry Andric                                    MCSection *StrOffsetSection,
66*62cfcf62SDimitry Andric                                    StringRef CurStrSection,
67*62cfcf62SDimitry Andric                                    StringRef CurStrOffsetSection) {
68*62cfcf62SDimitry Andric   // Could possibly produce an error or warning if one of these was non-null but
69*62cfcf62SDimitry Andric   // the other was null.
70*62cfcf62SDimitry Andric   if (CurStrSection.empty() || CurStrOffsetSection.empty())
71*62cfcf62SDimitry Andric     return;
72*62cfcf62SDimitry Andric 
73*62cfcf62SDimitry Andric   DenseMap<uint64_t, uint32_t> OffsetRemapping;
74*62cfcf62SDimitry Andric 
75*62cfcf62SDimitry Andric   DataExtractor Data(CurStrSection, true, 0);
76*62cfcf62SDimitry Andric   uint64_t LocalOffset = 0;
77*62cfcf62SDimitry Andric   uint64_t PrevOffset = 0;
78*62cfcf62SDimitry Andric   while (const char *s = Data.getCStr(&LocalOffset)) {
79*62cfcf62SDimitry Andric     OffsetRemapping[PrevOffset] =
80*62cfcf62SDimitry Andric         Strings.getOffset(s, LocalOffset - PrevOffset);
81*62cfcf62SDimitry Andric     PrevOffset = LocalOffset;
82*62cfcf62SDimitry Andric   }
83*62cfcf62SDimitry Andric 
84*62cfcf62SDimitry Andric   Data = DataExtractor(CurStrOffsetSection, true, 0);
85*62cfcf62SDimitry Andric 
86*62cfcf62SDimitry Andric   Out.SwitchSection(StrOffsetSection);
87*62cfcf62SDimitry Andric 
88*62cfcf62SDimitry Andric   uint64_t Offset = 0;
89*62cfcf62SDimitry Andric   uint64_t Size = CurStrOffsetSection.size();
90*62cfcf62SDimitry Andric   while (Offset < Size) {
91*62cfcf62SDimitry Andric     auto OldOffset = Data.getU32(&Offset);
92*62cfcf62SDimitry Andric     auto NewOffset = OffsetRemapping[OldOffset];
93*62cfcf62SDimitry Andric     Out.EmitIntValue(NewOffset, 4);
94*62cfcf62SDimitry Andric   }
95*62cfcf62SDimitry Andric }
96*62cfcf62SDimitry Andric 
97*62cfcf62SDimitry Andric static uint64_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
98*62cfcf62SDimitry Andric   uint64_t CurCode;
99*62cfcf62SDimitry Andric   uint64_t Offset = 0;
100*62cfcf62SDimitry Andric   DataExtractor AbbrevData(Abbrev, true, 0);
101*62cfcf62SDimitry Andric   while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) {
102*62cfcf62SDimitry Andric     // Tag
103*62cfcf62SDimitry Andric     AbbrevData.getULEB128(&Offset);
104*62cfcf62SDimitry Andric     // DW_CHILDREN
105*62cfcf62SDimitry Andric     AbbrevData.getU8(&Offset);
106*62cfcf62SDimitry Andric     // Attributes
107*62cfcf62SDimitry Andric     while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset))
108*62cfcf62SDimitry Andric       ;
109*62cfcf62SDimitry Andric   }
110*62cfcf62SDimitry Andric   return Offset;
111*62cfcf62SDimitry Andric }
112*62cfcf62SDimitry Andric 
113*62cfcf62SDimitry Andric struct CompileUnitIdentifiers {
114*62cfcf62SDimitry Andric   uint64_t Signature = 0;
115*62cfcf62SDimitry Andric   const char *Name = "";
116*62cfcf62SDimitry Andric   const char *DWOName = "";
117*62cfcf62SDimitry Andric };
118*62cfcf62SDimitry Andric 
119*62cfcf62SDimitry Andric static Expected<const char *>
120*62cfcf62SDimitry Andric getIndexedString(dwarf::Form Form, DataExtractor InfoData,
121*62cfcf62SDimitry Andric                  uint64_t &InfoOffset, StringRef StrOffsets, StringRef Str) {
122*62cfcf62SDimitry Andric   if (Form == dwarf::DW_FORM_string)
123*62cfcf62SDimitry Andric     return InfoData.getCStr(&InfoOffset);
124*62cfcf62SDimitry Andric   if (Form != dwarf::DW_FORM_GNU_str_index)
125*62cfcf62SDimitry Andric     return make_error<DWPError>(
126*62cfcf62SDimitry Andric         "string field encoded without DW_FORM_string or DW_FORM_GNU_str_index");
127*62cfcf62SDimitry Andric   auto StrIndex = InfoData.getULEB128(&InfoOffset);
128*62cfcf62SDimitry Andric   DataExtractor StrOffsetsData(StrOffsets, true, 0);
129*62cfcf62SDimitry Andric   uint64_t StrOffsetsOffset = 4 * StrIndex;
130*62cfcf62SDimitry Andric   uint64_t StrOffset = StrOffsetsData.getU32(&StrOffsetsOffset);
131*62cfcf62SDimitry Andric   DataExtractor StrData(Str, true, 0);
132*62cfcf62SDimitry Andric   return StrData.getCStr(&StrOffset);
133*62cfcf62SDimitry Andric }
134*62cfcf62SDimitry Andric 
135*62cfcf62SDimitry Andric static Expected<CompileUnitIdentifiers> getCUIdentifiers(StringRef Abbrev,
136*62cfcf62SDimitry Andric                                                          StringRef Info,
137*62cfcf62SDimitry Andric                                                          StringRef StrOffsets,
138*62cfcf62SDimitry Andric                                                          StringRef Str) {
139*62cfcf62SDimitry Andric   uint64_t Offset = 0;
140*62cfcf62SDimitry Andric   DataExtractor InfoData(Info, true, 0);
141*62cfcf62SDimitry Andric   dwarf::DwarfFormat Format = dwarf::DwarfFormat::DWARF32;
142*62cfcf62SDimitry Andric   uint64_t Length = InfoData.getU32(&Offset);
143*62cfcf62SDimitry Andric   // If the length is 0xffffffff, then this indictes that this is a DWARF 64
144*62cfcf62SDimitry Andric   // stream and the length is actually encoded into a 64 bit value that follows.
145*62cfcf62SDimitry Andric   if (Length == 0xffffffffU) {
146*62cfcf62SDimitry Andric     Format = dwarf::DwarfFormat::DWARF64;
147*62cfcf62SDimitry Andric     Length = InfoData.getU64(&Offset);
148*62cfcf62SDimitry Andric   }
149*62cfcf62SDimitry Andric   uint16_t Version = InfoData.getU16(&Offset);
150*62cfcf62SDimitry Andric   InfoData.getU32(&Offset); // Abbrev offset (should be zero)
151*62cfcf62SDimitry Andric   uint8_t AddrSize = InfoData.getU8(&Offset);
152*62cfcf62SDimitry Andric 
153*62cfcf62SDimitry Andric   uint32_t AbbrCode = InfoData.getULEB128(&Offset);
154*62cfcf62SDimitry Andric 
155*62cfcf62SDimitry Andric   DataExtractor AbbrevData(Abbrev, true, 0);
156*62cfcf62SDimitry Andric   uint64_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
157*62cfcf62SDimitry Andric   auto Tag = static_cast<dwarf::Tag>(AbbrevData.getULEB128(&AbbrevOffset));
158*62cfcf62SDimitry Andric   if (Tag != dwarf::DW_TAG_compile_unit)
159*62cfcf62SDimitry Andric     return make_error<DWPError>("top level DIE is not a compile unit");
160*62cfcf62SDimitry Andric   // DW_CHILDREN
161*62cfcf62SDimitry Andric   AbbrevData.getU8(&AbbrevOffset);
162*62cfcf62SDimitry Andric   uint32_t Name;
163*62cfcf62SDimitry Andric   dwarf::Form Form;
164*62cfcf62SDimitry Andric   CompileUnitIdentifiers ID;
165*62cfcf62SDimitry Andric   Optional<uint64_t> Signature = None;
166*62cfcf62SDimitry Andric   while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |
167*62cfcf62SDimitry Andric          (Form = static_cast<dwarf::Form>(AbbrevData.getULEB128(&AbbrevOffset))) &&
168*62cfcf62SDimitry Andric          (Name != 0 || Form != 0)) {
169*62cfcf62SDimitry Andric     switch (Name) {
170*62cfcf62SDimitry Andric     case dwarf::DW_AT_name: {
171*62cfcf62SDimitry Andric       Expected<const char *> EName =
172*62cfcf62SDimitry Andric           getIndexedString(Form, InfoData, Offset, StrOffsets, Str);
173*62cfcf62SDimitry Andric       if (!EName)
174*62cfcf62SDimitry Andric         return EName.takeError();
175*62cfcf62SDimitry Andric       ID.Name = *EName;
176*62cfcf62SDimitry Andric       break;
177*62cfcf62SDimitry Andric     }
178*62cfcf62SDimitry Andric     case dwarf::DW_AT_GNU_dwo_name: {
179*62cfcf62SDimitry Andric       Expected<const char *> EName =
180*62cfcf62SDimitry Andric           getIndexedString(Form, InfoData, Offset, StrOffsets, Str);
181*62cfcf62SDimitry Andric       if (!EName)
182*62cfcf62SDimitry Andric         return EName.takeError();
183*62cfcf62SDimitry Andric       ID.DWOName = *EName;
184*62cfcf62SDimitry Andric       break;
185*62cfcf62SDimitry Andric     }
186*62cfcf62SDimitry Andric     case dwarf::DW_AT_GNU_dwo_id:
187*62cfcf62SDimitry Andric       Signature = InfoData.getU64(&Offset);
188*62cfcf62SDimitry Andric       break;
189*62cfcf62SDimitry Andric     default:
190*62cfcf62SDimitry Andric       DWARFFormValue::skipValue(Form, InfoData, &Offset,
191*62cfcf62SDimitry Andric                                 dwarf::FormParams({Version, AddrSize, Format}));
192*62cfcf62SDimitry Andric     }
193*62cfcf62SDimitry Andric   }
194*62cfcf62SDimitry Andric   if (!Signature)
195*62cfcf62SDimitry Andric     return make_error<DWPError>("compile unit missing dwo_id");
196*62cfcf62SDimitry Andric   ID.Signature = *Signature;
197*62cfcf62SDimitry Andric   return ID;
198*62cfcf62SDimitry Andric }
199*62cfcf62SDimitry Andric 
200*62cfcf62SDimitry Andric struct UnitIndexEntry {
201*62cfcf62SDimitry Andric   DWARFUnitIndex::Entry::SectionContribution Contributions[8];
202*62cfcf62SDimitry Andric   std::string Name;
203*62cfcf62SDimitry Andric   std::string DWOName;
204*62cfcf62SDimitry Andric   StringRef DWPName;
205*62cfcf62SDimitry Andric };
206*62cfcf62SDimitry Andric 
207*62cfcf62SDimitry Andric static StringRef getSubsection(StringRef Section,
208*62cfcf62SDimitry Andric                                const DWARFUnitIndex::Entry &Entry,
209*62cfcf62SDimitry Andric                                DWARFSectionKind Kind) {
210*62cfcf62SDimitry Andric   const auto *Off = Entry.getOffset(Kind);
211*62cfcf62SDimitry Andric   if (!Off)
212*62cfcf62SDimitry Andric     return StringRef();
213*62cfcf62SDimitry Andric   return Section.substr(Off->Offset, Off->Length);
214*62cfcf62SDimitry Andric }
215*62cfcf62SDimitry Andric 
216*62cfcf62SDimitry Andric static void addAllTypesFromDWP(
217*62cfcf62SDimitry Andric     MCStreamer &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
218*62cfcf62SDimitry Andric     const DWARFUnitIndex &TUIndex, MCSection *OutputTypes, StringRef Types,
219*62cfcf62SDimitry Andric     const UnitIndexEntry &TUEntry, uint32_t &TypesOffset) {
220*62cfcf62SDimitry Andric   Out.SwitchSection(OutputTypes);
221*62cfcf62SDimitry Andric   for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) {
222*62cfcf62SDimitry Andric     auto *I = E.getOffsets();
223*62cfcf62SDimitry Andric     if (!I)
224*62cfcf62SDimitry Andric       continue;
225*62cfcf62SDimitry Andric     auto P = TypeIndexEntries.insert(std::make_pair(E.getSignature(), TUEntry));
226*62cfcf62SDimitry Andric     if (!P.second)
227*62cfcf62SDimitry Andric       continue;
228*62cfcf62SDimitry Andric     auto &Entry = P.first->second;
229*62cfcf62SDimitry Andric     // Zero out the debug_info contribution
230*62cfcf62SDimitry Andric     Entry.Contributions[0] = {};
231*62cfcf62SDimitry Andric     for (auto Kind : TUIndex.getColumnKinds()) {
232*62cfcf62SDimitry Andric       auto &C = Entry.Contributions[Kind - DW_SECT_INFO];
233*62cfcf62SDimitry Andric       C.Offset += I->Offset;
234*62cfcf62SDimitry Andric       C.Length = I->Length;
235*62cfcf62SDimitry Andric       ++I;
236*62cfcf62SDimitry Andric     }
237*62cfcf62SDimitry Andric     auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
238*62cfcf62SDimitry Andric     Out.EmitBytes(Types.substr(
239*62cfcf62SDimitry Andric         C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset,
240*62cfcf62SDimitry Andric         C.Length));
241*62cfcf62SDimitry Andric     C.Offset = TypesOffset;
242*62cfcf62SDimitry Andric     TypesOffset += C.Length;
243*62cfcf62SDimitry Andric   }
244*62cfcf62SDimitry Andric }
245*62cfcf62SDimitry Andric 
246*62cfcf62SDimitry Andric static void addAllTypes(MCStreamer &Out,
247*62cfcf62SDimitry Andric                         MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
248*62cfcf62SDimitry Andric                         MCSection *OutputTypes,
249*62cfcf62SDimitry Andric                         const std::vector<StringRef> &TypesSections,
250*62cfcf62SDimitry Andric                         const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) {
251*62cfcf62SDimitry Andric   for (StringRef Types : TypesSections) {
252*62cfcf62SDimitry Andric     Out.SwitchSection(OutputTypes);
253*62cfcf62SDimitry Andric     uint64_t Offset = 0;
254*62cfcf62SDimitry Andric     DataExtractor Data(Types, true, 0);
255*62cfcf62SDimitry Andric     while (Data.isValidOffset(Offset)) {
256*62cfcf62SDimitry Andric       UnitIndexEntry Entry = CUEntry;
257*62cfcf62SDimitry Andric       // Zero out the debug_info contribution
258*62cfcf62SDimitry Andric       Entry.Contributions[0] = {};
259*62cfcf62SDimitry Andric       auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
260*62cfcf62SDimitry Andric       C.Offset = TypesOffset;
261*62cfcf62SDimitry Andric       auto PrevOffset = Offset;
262*62cfcf62SDimitry Andric       // Length of the unit, including the 4 byte length field.
263*62cfcf62SDimitry Andric       C.Length = Data.getU32(&Offset) + 4;
264*62cfcf62SDimitry Andric 
265*62cfcf62SDimitry Andric       Data.getU16(&Offset); // Version
266*62cfcf62SDimitry Andric       Data.getU32(&Offset); // Abbrev offset
267*62cfcf62SDimitry Andric       Data.getU8(&Offset);  // Address size
268*62cfcf62SDimitry Andric       auto Signature = Data.getU64(&Offset);
269*62cfcf62SDimitry Andric       Offset = PrevOffset + C.Length;
270*62cfcf62SDimitry Andric 
271*62cfcf62SDimitry Andric       auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry));
272*62cfcf62SDimitry Andric       if (!P.second)
273*62cfcf62SDimitry Andric         continue;
274*62cfcf62SDimitry Andric 
275*62cfcf62SDimitry Andric       Out.EmitBytes(Types.substr(PrevOffset, C.Length));
276*62cfcf62SDimitry Andric       TypesOffset += C.Length;
277*62cfcf62SDimitry Andric     }
278*62cfcf62SDimitry Andric   }
279*62cfcf62SDimitry Andric }
280*62cfcf62SDimitry Andric 
281*62cfcf62SDimitry Andric static void
282*62cfcf62SDimitry Andric writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,
283*62cfcf62SDimitry Andric                 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries,
284*62cfcf62SDimitry Andric                 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) {
285*62cfcf62SDimitry Andric   for (const auto &E : IndexEntries)
286*62cfcf62SDimitry Andric     for (size_t i = 0; i != array_lengthof(E.second.Contributions); ++i)
287*62cfcf62SDimitry Andric       if (ContributionOffsets[i])
288*62cfcf62SDimitry Andric         Out.EmitIntValue(E.second.Contributions[i].*Field, 4);
289*62cfcf62SDimitry Andric }
290*62cfcf62SDimitry Andric 
291*62cfcf62SDimitry Andric static void
292*62cfcf62SDimitry Andric writeIndex(MCStreamer &Out, MCSection *Section,
293*62cfcf62SDimitry Andric            ArrayRef<unsigned> ContributionOffsets,
294*62cfcf62SDimitry Andric            const MapVector<uint64_t, UnitIndexEntry> &IndexEntries) {
295*62cfcf62SDimitry Andric   if (IndexEntries.empty())
296*62cfcf62SDimitry Andric     return;
297*62cfcf62SDimitry Andric 
298*62cfcf62SDimitry Andric   unsigned Columns = 0;
299*62cfcf62SDimitry Andric   for (auto &C : ContributionOffsets)
300*62cfcf62SDimitry Andric     if (C)
301*62cfcf62SDimitry Andric       ++Columns;
302*62cfcf62SDimitry Andric 
303*62cfcf62SDimitry Andric   std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
304*62cfcf62SDimitry Andric   uint64_t Mask = Buckets.size() - 1;
305*62cfcf62SDimitry Andric   size_t i = 0;
306*62cfcf62SDimitry Andric   for (const auto &P : IndexEntries) {
307*62cfcf62SDimitry Andric     auto S = P.first;
308*62cfcf62SDimitry Andric     auto H = S & Mask;
309*62cfcf62SDimitry Andric     auto HP = ((S >> 32) & Mask) | 1;
310*62cfcf62SDimitry Andric     while (Buckets[H]) {
311*62cfcf62SDimitry Andric       assert(S != IndexEntries.begin()[Buckets[H] - 1].first &&
312*62cfcf62SDimitry Andric              "Duplicate unit");
313*62cfcf62SDimitry Andric       H = (H + HP) & Mask;
314*62cfcf62SDimitry Andric     }
315*62cfcf62SDimitry Andric     Buckets[H] = i + 1;
316*62cfcf62SDimitry Andric     ++i;
317*62cfcf62SDimitry Andric   }
318*62cfcf62SDimitry Andric 
319*62cfcf62SDimitry Andric   Out.SwitchSection(Section);
320*62cfcf62SDimitry Andric   Out.EmitIntValue(2, 4);                   // Version
321*62cfcf62SDimitry Andric   Out.EmitIntValue(Columns, 4);             // Columns
322*62cfcf62SDimitry Andric   Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
323*62cfcf62SDimitry Andric   Out.EmitIntValue(Buckets.size(), 4);      // Num Buckets
324*62cfcf62SDimitry Andric 
325*62cfcf62SDimitry Andric   // Write the signatures.
326*62cfcf62SDimitry Andric   for (const auto &I : Buckets)
327*62cfcf62SDimitry Andric     Out.EmitIntValue(I ? IndexEntries.begin()[I - 1].first : 0, 8);
328*62cfcf62SDimitry Andric 
329*62cfcf62SDimitry Andric   // Write the indexes.
330*62cfcf62SDimitry Andric   for (const auto &I : Buckets)
331*62cfcf62SDimitry Andric     Out.EmitIntValue(I, 4);
332*62cfcf62SDimitry Andric 
333*62cfcf62SDimitry Andric   // Write the column headers (which sections will appear in the table)
334*62cfcf62SDimitry Andric   for (size_t i = 0; i != ContributionOffsets.size(); ++i)
335*62cfcf62SDimitry Andric     if (ContributionOffsets[i])
336*62cfcf62SDimitry Andric       Out.EmitIntValue(i + DW_SECT_INFO, 4);
337*62cfcf62SDimitry Andric 
338*62cfcf62SDimitry Andric   // Write the offsets.
339*62cfcf62SDimitry Andric   writeIndexTable(Out, ContributionOffsets, IndexEntries,
340*62cfcf62SDimitry Andric                   &DWARFUnitIndex::Entry::SectionContribution::Offset);
341*62cfcf62SDimitry Andric 
342*62cfcf62SDimitry Andric   // Write the lengths.
343*62cfcf62SDimitry Andric   writeIndexTable(Out, ContributionOffsets, IndexEntries,
344*62cfcf62SDimitry Andric                   &DWARFUnitIndex::Entry::SectionContribution::Length);
345*62cfcf62SDimitry Andric }
346*62cfcf62SDimitry Andric 
347*62cfcf62SDimitry Andric std::string buildDWODescription(StringRef Name, StringRef DWPName, StringRef DWOName) {
348*62cfcf62SDimitry Andric   std::string Text = "\'";
349*62cfcf62SDimitry Andric   Text += Name;
350*62cfcf62SDimitry Andric   Text += '\'';
351*62cfcf62SDimitry Andric   if (!DWPName.empty()) {
352*62cfcf62SDimitry Andric     Text += " (from ";
353*62cfcf62SDimitry Andric     if (!DWOName.empty()) {
354*62cfcf62SDimitry Andric       Text += '\'';
355*62cfcf62SDimitry Andric       Text += DWOName;
356*62cfcf62SDimitry Andric       Text += "' in ";
357*62cfcf62SDimitry Andric     }
358*62cfcf62SDimitry Andric     Text += '\'';
359*62cfcf62SDimitry Andric     Text += DWPName;
360*62cfcf62SDimitry Andric     Text += "')";
361*62cfcf62SDimitry Andric   }
362*62cfcf62SDimitry Andric   return Text;
363*62cfcf62SDimitry Andric }
364*62cfcf62SDimitry Andric 
365*62cfcf62SDimitry Andric static Error createError(StringRef Name, Error E) {
366*62cfcf62SDimitry Andric   return make_error<DWPError>(
367*62cfcf62SDimitry Andric       ("failure while decompressing compressed section: '" + Name + "', " +
368*62cfcf62SDimitry Andric        llvm::toString(std::move(E)))
369*62cfcf62SDimitry Andric           .str());
370*62cfcf62SDimitry Andric }
371*62cfcf62SDimitry Andric 
372*62cfcf62SDimitry Andric static Error
373*62cfcf62SDimitry Andric handleCompressedSection(std::deque<SmallString<32>> &UncompressedSections,
374*62cfcf62SDimitry Andric                         StringRef &Name, StringRef &Contents) {
375*62cfcf62SDimitry Andric   if (!Decompressor::isGnuStyle(Name))
376*62cfcf62SDimitry Andric     return Error::success();
377*62cfcf62SDimitry Andric 
378*62cfcf62SDimitry Andric   Expected<Decompressor> Dec =
379*62cfcf62SDimitry Andric       Decompressor::create(Name, Contents, false /*IsLE*/, false /*Is64Bit*/);
380*62cfcf62SDimitry Andric   if (!Dec)
381*62cfcf62SDimitry Andric     return createError(Name, Dec.takeError());
382*62cfcf62SDimitry Andric 
383*62cfcf62SDimitry Andric   UncompressedSections.emplace_back();
384*62cfcf62SDimitry Andric   if (Error E = Dec->resizeAndDecompress(UncompressedSections.back()))
385*62cfcf62SDimitry Andric     return createError(Name, std::move(E));
386*62cfcf62SDimitry Andric 
387*62cfcf62SDimitry Andric   Name = Name.substr(2); // Drop ".z"
388*62cfcf62SDimitry Andric   Contents = UncompressedSections.back();
389*62cfcf62SDimitry Andric   return Error::success();
390*62cfcf62SDimitry Andric }
391*62cfcf62SDimitry Andric 
392*62cfcf62SDimitry Andric static Error handleSection(
393*62cfcf62SDimitry Andric     const StringMap<std::pair<MCSection *, DWARFSectionKind>> &KnownSections,
394*62cfcf62SDimitry Andric     const MCSection *StrSection, const MCSection *StrOffsetSection,
395*62cfcf62SDimitry Andric     const MCSection *TypesSection, const MCSection *CUIndexSection,
396*62cfcf62SDimitry Andric     const MCSection *TUIndexSection, const SectionRef &Section, MCStreamer &Out,
397*62cfcf62SDimitry Andric     std::deque<SmallString<32>> &UncompressedSections,
398*62cfcf62SDimitry Andric     uint32_t (&ContributionOffsets)[8], UnitIndexEntry &CurEntry,
399*62cfcf62SDimitry Andric     StringRef &CurStrSection, StringRef &CurStrOffsetSection,
400*62cfcf62SDimitry Andric     std::vector<StringRef> &CurTypesSection, StringRef &InfoSection,
401*62cfcf62SDimitry Andric     StringRef &AbbrevSection, StringRef &CurCUIndexSection,
402*62cfcf62SDimitry Andric     StringRef &CurTUIndexSection) {
403*62cfcf62SDimitry Andric   if (Section.isBSS())
404*62cfcf62SDimitry Andric     return Error::success();
405*62cfcf62SDimitry Andric 
406*62cfcf62SDimitry Andric   if (Section.isVirtual())
407*62cfcf62SDimitry Andric     return Error::success();
408*62cfcf62SDimitry Andric 
409*62cfcf62SDimitry Andric   Expected<StringRef> NameOrErr = Section.getName();
410*62cfcf62SDimitry Andric   if (!NameOrErr)
411*62cfcf62SDimitry Andric     return NameOrErr.takeError();
412*62cfcf62SDimitry Andric   StringRef Name = *NameOrErr;
413*62cfcf62SDimitry Andric 
414*62cfcf62SDimitry Andric   Expected<StringRef> ContentsOrErr = Section.getContents();
415*62cfcf62SDimitry Andric   if (!ContentsOrErr)
416*62cfcf62SDimitry Andric     return ContentsOrErr.takeError();
417*62cfcf62SDimitry Andric   StringRef Contents = *ContentsOrErr;
418*62cfcf62SDimitry Andric 
419*62cfcf62SDimitry Andric   if (auto Err = handleCompressedSection(UncompressedSections, Name, Contents))
420*62cfcf62SDimitry Andric     return Err;
421*62cfcf62SDimitry Andric 
422*62cfcf62SDimitry Andric   Name = Name.substr(Name.find_first_not_of("._"));
423*62cfcf62SDimitry Andric 
424*62cfcf62SDimitry Andric   auto SectionPair = KnownSections.find(Name);
425*62cfcf62SDimitry Andric   if (SectionPair == KnownSections.end())
426*62cfcf62SDimitry Andric     return Error::success();
427*62cfcf62SDimitry Andric 
428*62cfcf62SDimitry Andric   if (DWARFSectionKind Kind = SectionPair->second.second) {
429*62cfcf62SDimitry Andric     auto Index = Kind - DW_SECT_INFO;
430*62cfcf62SDimitry Andric     if (Kind != DW_SECT_TYPES) {
431*62cfcf62SDimitry Andric       CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
432*62cfcf62SDimitry Andric       ContributionOffsets[Index] +=
433*62cfcf62SDimitry Andric           (CurEntry.Contributions[Index].Length = Contents.size());
434*62cfcf62SDimitry Andric     }
435*62cfcf62SDimitry Andric 
436*62cfcf62SDimitry Andric     switch (Kind) {
437*62cfcf62SDimitry Andric     case DW_SECT_INFO:
438*62cfcf62SDimitry Andric       InfoSection = Contents;
439*62cfcf62SDimitry Andric       break;
440*62cfcf62SDimitry Andric     case DW_SECT_ABBREV:
441*62cfcf62SDimitry Andric       AbbrevSection = Contents;
442*62cfcf62SDimitry Andric       break;
443*62cfcf62SDimitry Andric     default:
444*62cfcf62SDimitry Andric       break;
445*62cfcf62SDimitry Andric     }
446*62cfcf62SDimitry Andric   }
447*62cfcf62SDimitry Andric 
448*62cfcf62SDimitry Andric   MCSection *OutSection = SectionPair->second.first;
449*62cfcf62SDimitry Andric   if (OutSection == StrOffsetSection)
450*62cfcf62SDimitry Andric     CurStrOffsetSection = Contents;
451*62cfcf62SDimitry Andric   else if (OutSection == StrSection)
452*62cfcf62SDimitry Andric     CurStrSection = Contents;
453*62cfcf62SDimitry Andric   else if (OutSection == TypesSection)
454*62cfcf62SDimitry Andric     CurTypesSection.push_back(Contents);
455*62cfcf62SDimitry Andric   else if (OutSection == CUIndexSection)
456*62cfcf62SDimitry Andric     CurCUIndexSection = Contents;
457*62cfcf62SDimitry Andric   else if (OutSection == TUIndexSection)
458*62cfcf62SDimitry Andric     CurTUIndexSection = Contents;
459*62cfcf62SDimitry Andric   else {
460*62cfcf62SDimitry Andric     Out.SwitchSection(OutSection);
461*62cfcf62SDimitry Andric     Out.EmitBytes(Contents);
462*62cfcf62SDimitry Andric   }
463*62cfcf62SDimitry Andric   return Error::success();
464*62cfcf62SDimitry Andric }
465*62cfcf62SDimitry Andric 
466*62cfcf62SDimitry Andric static Error
467*62cfcf62SDimitry Andric buildDuplicateError(const std::pair<uint64_t, UnitIndexEntry> &PrevE,
468*62cfcf62SDimitry Andric                     const CompileUnitIdentifiers &ID, StringRef DWPName) {
469*62cfcf62SDimitry Andric   return make_error<DWPError>(
470*62cfcf62SDimitry Andric       std::string("Duplicate DWO ID (") + utohexstr(PrevE.first) + ") in " +
471*62cfcf62SDimitry Andric       buildDWODescription(PrevE.second.Name, PrevE.second.DWPName,
472*62cfcf62SDimitry Andric                           PrevE.second.DWOName) +
473*62cfcf62SDimitry Andric       " and " + buildDWODescription(ID.Name, DWPName, ID.DWOName));
474*62cfcf62SDimitry Andric }
475*62cfcf62SDimitry Andric 
476*62cfcf62SDimitry Andric static Expected<SmallVector<std::string, 16>>
477*62cfcf62SDimitry Andric getDWOFilenames(StringRef ExecFilename) {
478*62cfcf62SDimitry Andric   auto ErrOrObj = object::ObjectFile::createObjectFile(ExecFilename);
479*62cfcf62SDimitry Andric   if (!ErrOrObj)
480*62cfcf62SDimitry Andric     return ErrOrObj.takeError();
481*62cfcf62SDimitry Andric 
482*62cfcf62SDimitry Andric   const ObjectFile &Obj = *ErrOrObj.get().getBinary();
483*62cfcf62SDimitry Andric   std::unique_ptr<DWARFContext> DWARFCtx = DWARFContext::create(Obj);
484*62cfcf62SDimitry Andric 
485*62cfcf62SDimitry Andric   SmallVector<std::string, 16> DWOPaths;
486*62cfcf62SDimitry Andric   for (const auto &CU : DWARFCtx->compile_units()) {
487*62cfcf62SDimitry Andric     const DWARFDie &Die = CU->getUnitDIE();
488*62cfcf62SDimitry Andric     std::string DWOName = dwarf::toString(
489*62cfcf62SDimitry Andric         Die.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
490*62cfcf62SDimitry Andric     if (DWOName.empty())
491*62cfcf62SDimitry Andric       continue;
492*62cfcf62SDimitry Andric     std::string DWOCompDir =
493*62cfcf62SDimitry Andric         dwarf::toString(Die.find(dwarf::DW_AT_comp_dir), "");
494*62cfcf62SDimitry Andric     if (!DWOCompDir.empty()) {
495*62cfcf62SDimitry Andric       SmallString<16> DWOPath;
496*62cfcf62SDimitry Andric       sys::path::append(DWOPath, DWOCompDir, DWOName);
497*62cfcf62SDimitry Andric       DWOPaths.emplace_back(DWOPath.data(), DWOPath.size());
498*62cfcf62SDimitry Andric     } else {
499*62cfcf62SDimitry Andric       DWOPaths.push_back(std::move(DWOName));
500*62cfcf62SDimitry Andric     }
501*62cfcf62SDimitry Andric   }
502*62cfcf62SDimitry Andric   return std::move(DWOPaths);
503*62cfcf62SDimitry Andric }
504*62cfcf62SDimitry Andric 
505*62cfcf62SDimitry Andric static Error write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
506*62cfcf62SDimitry Andric   const auto &MCOFI = *Out.getContext().getObjectFileInfo();
507*62cfcf62SDimitry Andric   MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
508*62cfcf62SDimitry Andric   MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
509*62cfcf62SDimitry Andric   MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
510*62cfcf62SDimitry Andric   MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection();
511*62cfcf62SDimitry Andric   MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection();
512*62cfcf62SDimitry Andric   const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
513*62cfcf62SDimitry Andric       {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
514*62cfcf62SDimitry Andric       {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
515*62cfcf62SDimitry Andric       {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
516*62cfcf62SDimitry Andric       {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
517*62cfcf62SDimitry Andric       {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
518*62cfcf62SDimitry Andric       {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
519*62cfcf62SDimitry Andric       {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
520*62cfcf62SDimitry Andric       {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}},
521*62cfcf62SDimitry Andric       {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}};
522*62cfcf62SDimitry Andric 
523*62cfcf62SDimitry Andric   MapVector<uint64_t, UnitIndexEntry> IndexEntries;
524*62cfcf62SDimitry Andric   MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries;
525*62cfcf62SDimitry Andric 
526*62cfcf62SDimitry Andric   uint32_t ContributionOffsets[8] = {};
527*62cfcf62SDimitry Andric 
528*62cfcf62SDimitry Andric   DWPStringPool Strings(Out, StrSection);
529*62cfcf62SDimitry Andric 
530*62cfcf62SDimitry Andric   SmallVector<OwningBinary<object::ObjectFile>, 128> Objects;
531*62cfcf62SDimitry Andric   Objects.reserve(Inputs.size());
532*62cfcf62SDimitry Andric 
533*62cfcf62SDimitry Andric   std::deque<SmallString<32>> UncompressedSections;
534*62cfcf62SDimitry Andric 
535*62cfcf62SDimitry Andric   for (const auto &Input : Inputs) {
536*62cfcf62SDimitry Andric     auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
537*62cfcf62SDimitry Andric     if (!ErrOrObj)
538*62cfcf62SDimitry Andric       return ErrOrObj.takeError();
539*62cfcf62SDimitry Andric 
540*62cfcf62SDimitry Andric     auto &Obj = *ErrOrObj->getBinary();
541*62cfcf62SDimitry Andric     Objects.push_back(std::move(*ErrOrObj));
542*62cfcf62SDimitry Andric 
543*62cfcf62SDimitry Andric     UnitIndexEntry CurEntry = {};
544*62cfcf62SDimitry Andric 
545*62cfcf62SDimitry Andric     StringRef CurStrSection;
546*62cfcf62SDimitry Andric     StringRef CurStrOffsetSection;
547*62cfcf62SDimitry Andric     std::vector<StringRef> CurTypesSection;
548*62cfcf62SDimitry Andric     StringRef InfoSection;
549*62cfcf62SDimitry Andric     StringRef AbbrevSection;
550*62cfcf62SDimitry Andric     StringRef CurCUIndexSection;
551*62cfcf62SDimitry Andric     StringRef CurTUIndexSection;
552*62cfcf62SDimitry Andric 
553*62cfcf62SDimitry Andric     for (const auto &Section : Obj.sections())
554*62cfcf62SDimitry Andric       if (auto Err = handleSection(
555*62cfcf62SDimitry Andric               KnownSections, StrSection, StrOffsetSection, TypesSection,
556*62cfcf62SDimitry Andric               CUIndexSection, TUIndexSection, Section, Out,
557*62cfcf62SDimitry Andric               UncompressedSections, ContributionOffsets, CurEntry,
558*62cfcf62SDimitry Andric               CurStrSection, CurStrOffsetSection, CurTypesSection, InfoSection,
559*62cfcf62SDimitry Andric               AbbrevSection, CurCUIndexSection, CurTUIndexSection))
560*62cfcf62SDimitry Andric         return Err;
561*62cfcf62SDimitry Andric 
562*62cfcf62SDimitry Andric     if (InfoSection.empty())
563*62cfcf62SDimitry Andric       continue;
564*62cfcf62SDimitry Andric 
565*62cfcf62SDimitry Andric     writeStringsAndOffsets(Out, Strings, StrOffsetSection, CurStrSection,
566*62cfcf62SDimitry Andric                            CurStrOffsetSection);
567*62cfcf62SDimitry Andric 
568*62cfcf62SDimitry Andric     if (CurCUIndexSection.empty()) {
569*62cfcf62SDimitry Andric       Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(
570*62cfcf62SDimitry Andric           AbbrevSection, InfoSection, CurStrOffsetSection, CurStrSection);
571*62cfcf62SDimitry Andric       if (!EID)
572*62cfcf62SDimitry Andric         return createFileError(Input, EID.takeError());
573*62cfcf62SDimitry Andric       const auto &ID = *EID;
574*62cfcf62SDimitry Andric       auto P = IndexEntries.insert(std::make_pair(ID.Signature, CurEntry));
575*62cfcf62SDimitry Andric       if (!P.second)
576*62cfcf62SDimitry Andric         return buildDuplicateError(*P.first, ID, "");
577*62cfcf62SDimitry Andric       P.first->second.Name = ID.Name;
578*62cfcf62SDimitry Andric       P.first->second.DWOName = ID.DWOName;
579*62cfcf62SDimitry Andric       addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection,
580*62cfcf62SDimitry Andric                   CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
581*62cfcf62SDimitry Andric       continue;
582*62cfcf62SDimitry Andric     }
583*62cfcf62SDimitry Andric 
584*62cfcf62SDimitry Andric     DWARFUnitIndex CUIndex(DW_SECT_INFO);
585*62cfcf62SDimitry Andric     DataExtractor CUIndexData(CurCUIndexSection, Obj.isLittleEndian(), 0);
586*62cfcf62SDimitry Andric     if (!CUIndex.parse(CUIndexData))
587*62cfcf62SDimitry Andric       return make_error<DWPError>("Failed to parse cu_index");
588*62cfcf62SDimitry Andric 
589*62cfcf62SDimitry Andric     for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
590*62cfcf62SDimitry Andric       auto *I = E.getOffsets();
591*62cfcf62SDimitry Andric       if (!I)
592*62cfcf62SDimitry Andric         continue;
593*62cfcf62SDimitry Andric       auto P = IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry));
594*62cfcf62SDimitry Andric       Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(
595*62cfcf62SDimitry Andric           getSubsection(AbbrevSection, E, DW_SECT_ABBREV),
596*62cfcf62SDimitry Andric           getSubsection(InfoSection, E, DW_SECT_INFO),
597*62cfcf62SDimitry Andric           getSubsection(CurStrOffsetSection, E, DW_SECT_STR_OFFSETS),
598*62cfcf62SDimitry Andric           CurStrSection);
599*62cfcf62SDimitry Andric       if (!EID)
600*62cfcf62SDimitry Andric         return createFileError(Input, EID.takeError());
601*62cfcf62SDimitry Andric       const auto &ID = *EID;
602*62cfcf62SDimitry Andric       if (!P.second)
603*62cfcf62SDimitry Andric         return buildDuplicateError(*P.first, ID, Input);
604*62cfcf62SDimitry Andric       auto &NewEntry = P.first->second;
605*62cfcf62SDimitry Andric       NewEntry.Name = ID.Name;
606*62cfcf62SDimitry Andric       NewEntry.DWOName = ID.DWOName;
607*62cfcf62SDimitry Andric       NewEntry.DWPName = Input;
608*62cfcf62SDimitry Andric       for (auto Kind : CUIndex.getColumnKinds()) {
609*62cfcf62SDimitry Andric         auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO];
610*62cfcf62SDimitry Andric         C.Offset += I->Offset;
611*62cfcf62SDimitry Andric         C.Length = I->Length;
612*62cfcf62SDimitry Andric         ++I;
613*62cfcf62SDimitry Andric       }
614*62cfcf62SDimitry Andric     }
615*62cfcf62SDimitry Andric 
616*62cfcf62SDimitry Andric     if (!CurTypesSection.empty()) {
617*62cfcf62SDimitry Andric       if (CurTypesSection.size() != 1)
618*62cfcf62SDimitry Andric         return make_error<DWPError>("multiple type unit sections in .dwp file");
619*62cfcf62SDimitry Andric       DWARFUnitIndex TUIndex(DW_SECT_TYPES);
620*62cfcf62SDimitry Andric       DataExtractor TUIndexData(CurTUIndexSection, Obj.isLittleEndian(), 0);
621*62cfcf62SDimitry Andric       if (!TUIndex.parse(TUIndexData))
622*62cfcf62SDimitry Andric         return make_error<DWPError>("Failed to parse tu_index");
623*62cfcf62SDimitry Andric       addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection,
624*62cfcf62SDimitry Andric                          CurTypesSection.front(), CurEntry,
625*62cfcf62SDimitry Andric                          ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
626*62cfcf62SDimitry Andric     }
627*62cfcf62SDimitry Andric   }
628*62cfcf62SDimitry Andric 
629*62cfcf62SDimitry Andric   // Lie about there being no info contributions so the TU index only includes
630*62cfcf62SDimitry Andric   // the type unit contribution
631*62cfcf62SDimitry Andric   ContributionOffsets[0] = 0;
632*62cfcf62SDimitry Andric   writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
633*62cfcf62SDimitry Andric              TypeIndexEntries);
634*62cfcf62SDimitry Andric 
635*62cfcf62SDimitry Andric   // Lie about the type contribution
636*62cfcf62SDimitry Andric   ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
637*62cfcf62SDimitry Andric   // Unlie about the info contribution
638*62cfcf62SDimitry Andric   ContributionOffsets[0] = 1;
639*62cfcf62SDimitry Andric 
640*62cfcf62SDimitry Andric   writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
641*62cfcf62SDimitry Andric              IndexEntries);
642*62cfcf62SDimitry Andric 
643*62cfcf62SDimitry Andric   return Error::success();
644*62cfcf62SDimitry Andric }
645*62cfcf62SDimitry Andric 
646*62cfcf62SDimitry Andric static int error(const Twine &Error, const Twine &Context) {
647*62cfcf62SDimitry Andric   errs() << Twine("while processing ") + Context + ":\n";
648*62cfcf62SDimitry Andric   errs() << Twine("error: ") + Error + "\n";
649*62cfcf62SDimitry Andric   return 1;
650*62cfcf62SDimitry Andric }
651*62cfcf62SDimitry Andric 
652*62cfcf62SDimitry Andric int main(int argc, char **argv) {
653*62cfcf62SDimitry Andric   InitLLVM X(argc, argv);
654*62cfcf62SDimitry Andric 
655*62cfcf62SDimitry Andric   cl::ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files\n");
656*62cfcf62SDimitry Andric 
657*62cfcf62SDimitry Andric   llvm::InitializeAllTargetInfos();
658*62cfcf62SDimitry Andric   llvm::InitializeAllTargetMCs();
659*62cfcf62SDimitry Andric   llvm::InitializeAllTargets();
660*62cfcf62SDimitry Andric   llvm::InitializeAllAsmPrinters();
661*62cfcf62SDimitry Andric 
662*62cfcf62SDimitry Andric   std::string ErrorStr;
663*62cfcf62SDimitry Andric   StringRef Context = "dwarf streamer init";
664*62cfcf62SDimitry Andric 
665*62cfcf62SDimitry Andric   Triple TheTriple("x86_64-linux-gnu");
666*62cfcf62SDimitry Andric 
667*62cfcf62SDimitry Andric   // Get the target.
668*62cfcf62SDimitry Andric   const Target *TheTarget =
669*62cfcf62SDimitry Andric       TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
670*62cfcf62SDimitry Andric   if (!TheTarget)
671*62cfcf62SDimitry Andric     return error(ErrorStr, Context);
672*62cfcf62SDimitry Andric   std::string TripleName = TheTriple.getTriple();
673*62cfcf62SDimitry Andric 
674*62cfcf62SDimitry Andric   // Create all the MC Objects.
675*62cfcf62SDimitry Andric   std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
676*62cfcf62SDimitry Andric   if (!MRI)
677*62cfcf62SDimitry Andric     return error(Twine("no register info for target ") + TripleName, Context);
678*62cfcf62SDimitry Andric 
679*62cfcf62SDimitry Andric   MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
680*62cfcf62SDimitry Andric   std::unique_ptr<MCAsmInfo> MAI(
681*62cfcf62SDimitry Andric       TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
682*62cfcf62SDimitry Andric   if (!MAI)
683*62cfcf62SDimitry Andric     return error("no asm info for target " + TripleName, Context);
684*62cfcf62SDimitry Andric 
685*62cfcf62SDimitry Andric   MCObjectFileInfo MOFI;
686*62cfcf62SDimitry Andric   MCContext MC(MAI.get(), MRI.get(), &MOFI);
687*62cfcf62SDimitry Andric   MOFI.InitMCObjectFileInfo(TheTriple, /*PIC*/ false, MC);
688*62cfcf62SDimitry Andric 
689*62cfcf62SDimitry Andric   std::unique_ptr<MCSubtargetInfo> MSTI(
690*62cfcf62SDimitry Andric       TheTarget->createMCSubtargetInfo(TripleName, "", ""));
691*62cfcf62SDimitry Andric   if (!MSTI)
692*62cfcf62SDimitry Andric     return error("no subtarget info for target " + TripleName, Context);
693*62cfcf62SDimitry Andric 
694*62cfcf62SDimitry Andric   MCTargetOptions Options;
695*62cfcf62SDimitry Andric   auto MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, Options);
696*62cfcf62SDimitry Andric   if (!MAB)
697*62cfcf62SDimitry Andric     return error("no asm backend for target " + TripleName, Context);
698*62cfcf62SDimitry Andric 
699*62cfcf62SDimitry Andric   std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
700*62cfcf62SDimitry Andric   if (!MII)
701*62cfcf62SDimitry Andric     return error("no instr info info for target " + TripleName, Context);
702*62cfcf62SDimitry Andric 
703*62cfcf62SDimitry Andric   MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
704*62cfcf62SDimitry Andric   if (!MCE)
705*62cfcf62SDimitry Andric     return error("no code emitter for target " + TripleName, Context);
706*62cfcf62SDimitry Andric 
707*62cfcf62SDimitry Andric   // Create the output file.
708*62cfcf62SDimitry Andric   std::error_code EC;
709*62cfcf62SDimitry Andric   ToolOutputFile OutFile(OutputFilename, EC, sys::fs::OF_None);
710*62cfcf62SDimitry Andric   Optional<buffer_ostream> BOS;
711*62cfcf62SDimitry Andric   raw_pwrite_stream *OS;
712*62cfcf62SDimitry Andric   if (EC)
713*62cfcf62SDimitry Andric     return error(Twine(OutputFilename) + ": " + EC.message(), Context);
714*62cfcf62SDimitry Andric   if (OutFile.os().supportsSeeking()) {
715*62cfcf62SDimitry Andric     OS = &OutFile.os();
716*62cfcf62SDimitry Andric   } else {
717*62cfcf62SDimitry Andric     BOS.emplace(OutFile.os());
718*62cfcf62SDimitry Andric     OS = BOS.getPointer();
719*62cfcf62SDimitry Andric   }
720*62cfcf62SDimitry Andric 
721*62cfcf62SDimitry Andric   std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
722*62cfcf62SDimitry Andric       TheTriple, MC, std::unique_ptr<MCAsmBackend>(MAB),
723*62cfcf62SDimitry Andric       MAB->createObjectWriter(*OS), std::unique_ptr<MCCodeEmitter>(MCE), *MSTI,
724*62cfcf62SDimitry Andric       MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible,
725*62cfcf62SDimitry Andric       /*DWARFMustBeAtTheEnd*/ false));
726*62cfcf62SDimitry Andric   if (!MS)
727*62cfcf62SDimitry Andric     return error("no object streamer for target " + TripleName, Context);
728*62cfcf62SDimitry Andric 
729*62cfcf62SDimitry Andric   std::vector<std::string> DWOFilenames = InputFiles;
730*62cfcf62SDimitry Andric   for (const auto &ExecFilename : ExecFilenames) {
731*62cfcf62SDimitry Andric     auto DWOs = getDWOFilenames(ExecFilename);
732*62cfcf62SDimitry Andric     if (!DWOs) {
733*62cfcf62SDimitry Andric       logAllUnhandledErrors(DWOs.takeError(), WithColor::error());
734*62cfcf62SDimitry Andric       return 1;
735*62cfcf62SDimitry Andric     }
736*62cfcf62SDimitry Andric     DWOFilenames.insert(DWOFilenames.end(),
737*62cfcf62SDimitry Andric                         std::make_move_iterator(DWOs->begin()),
738*62cfcf62SDimitry Andric                         std::make_move_iterator(DWOs->end()));
739*62cfcf62SDimitry Andric   }
740*62cfcf62SDimitry Andric 
741*62cfcf62SDimitry Andric   if (auto Err = write(*MS, DWOFilenames)) {
742*62cfcf62SDimitry Andric     logAllUnhandledErrors(std::move(Err), WithColor::error());
743*62cfcf62SDimitry Andric     return 1;
744*62cfcf62SDimitry Andric   }
745*62cfcf62SDimitry Andric 
746*62cfcf62SDimitry Andric   MS->Finish();
747*62cfcf62SDimitry Andric   OutFile.keep();
748*62cfcf62SDimitry Andric   return 0;
749*62cfcf62SDimitry Andric }
750