blob: ceadfb1b9ee6176c3da8adf3897d49f33e3be4d3 [file] [log] [blame]
David Blaikie852c02b2016-02-19 21:09:26 +00001#include "llvm/ADT/MapVector.h"
David Blaikie242b9482015-12-01 00:48:39 +00002#include "llvm/ADT/STLExtras.h"
3#include "llvm/ADT/StringSet.h"
4#include "llvm/CodeGen/AsmPrinter.h"
David Blaikie2ed678c2015-12-05 03:06:30 +00005#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
6#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
David Blaikie242b9482015-12-01 00:48:39 +00007#include "llvm/MC/MCAsmInfo.h"
8#include "llvm/MC/MCContext.h"
9#include "llvm/MC/MCInstrInfo.h"
10#include "llvm/MC/MCObjectFileInfo.h"
11#include "llvm/MC/MCRegisterInfo.h"
12#include "llvm/MC/MCSectionELF.h"
13#include "llvm/MC/MCStreamer.h"
David Majnemer03e2cc32015-12-21 22:09:27 +000014#include "llvm/MC/MCTargetOptionsCommandFlags.h"
David Blaikie242b9482015-12-01 00:48:39 +000015#include "llvm/Object/ObjectFile.h"
David Blaikie74f5b282016-02-19 01:51:44 +000016#include "llvm/Support/Compression.h"
David Blaikie98ad82a2015-12-01 18:07:07 +000017#include "llvm/Support/DataExtractor.h"
David Blaikie242b9482015-12-01 00:48:39 +000018#include "llvm/Support/FileSystem.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000019#include "llvm/Support/MathExtras.h"
David Blaikie242b9482015-12-01 00:48:39 +000020#include "llvm/Support/MemoryBuffer.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000021#include "llvm/Support/Options.h"
David Blaikie242b9482015-12-01 00:48:39 +000022#include "llvm/Support/TargetRegistry.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000023#include "llvm/Support/TargetSelect.h"
David Blaikie242b9482015-12-01 00:48:39 +000024#include "llvm/Support/raw_ostream.h"
25#include "llvm/Target/TargetMachine.h"
David Blaikie242b9482015-12-01 00:48:39 +000026#include <list>
David Blaikief1958da2016-02-26 07:30:15 +000027#include <iostream>
David Blaikie2ed678c2015-12-05 03:06:30 +000028#include <memory>
David Blaikie23919372016-02-06 01:15:26 +000029#include <system_error>
David Blaikie852c02b2016-02-19 21:09:26 +000030#include <unordered_set>
David Blaikie242b9482015-12-01 00:48:39 +000031
32using namespace llvm;
David Blaikie98ad82a2015-12-01 18:07:07 +000033using namespace llvm::object;
David Blaikie242b9482015-12-01 00:48:39 +000034using namespace cl;
35
36OptionCategory DwpCategory("Specific Options");
37static list<std::string> InputFiles(Positional, OneOrMore,
38 desc("<input files>"), cat(DwpCategory));
39
David Blaikie2ed678c2015-12-05 03:06:30 +000040static opt<std::string> OutputFilename(Required, "o",
41 desc("Specify the output file."),
42 value_desc("filename"),
43 cat(DwpCategory));
David Blaikie242b9482015-12-01 00:48:39 +000044
45static int error(const Twine &Error, const Twine &Context) {
46 errs() << Twine("while processing ") + Context + ":\n";
47 errs() << Twine("error: ") + Error + "\n";
48 return 1;
49}
50
David Blaikie98ad82a2015-12-01 18:07:07 +000051static std::error_code
52writeStringsAndOffsets(MCStreamer &Out, StringMap<uint32_t> &Strings,
David Blaikiebb94e442015-12-01 19:17:58 +000053 uint32_t &StringOffset, MCSection *StrSection,
54 MCSection *StrOffsetSection, StringRef CurStrSection,
55 StringRef CurStrOffsetSection) {
David Blaikie98ad82a2015-12-01 18:07:07 +000056 // Could possibly produce an error or warning if one of these was non-null but
57 // the other was null.
58 if (CurStrSection.empty() || CurStrOffsetSection.empty())
59 return std::error_code();
60
61 DenseMap<uint32_t, uint32_t> OffsetRemapping;
62
63 DataExtractor Data(CurStrSection, true, 0);
64 uint32_t LocalOffset = 0;
65 uint32_t PrevOffset = 0;
66 while (const char *s = Data.getCStr(&LocalOffset)) {
67 StringRef Str(s, LocalOffset - PrevOffset - 1);
David Blaikiebb94e442015-12-01 19:17:58 +000068 auto Pair = Strings.insert(std::make_pair(Str, StringOffset));
69 if (Pair.second) {
70 Out.SwitchSection(StrSection);
71 Out.EmitBytes(
72 StringRef(Pair.first->getKeyData(), Pair.first->getKeyLength() + 1));
73 StringOffset += Str.size() + 1;
74 }
75 OffsetRemapping[PrevOffset] = Pair.first->second;
David Blaikie98ad82a2015-12-01 18:07:07 +000076 PrevOffset = LocalOffset;
77 }
78
79 Data = DataExtractor(CurStrOffsetSection, true, 0);
80
81 Out.SwitchSection(StrOffsetSection);
82
83 uint32_t Offset = 0;
84 uint64_t Size = CurStrOffsetSection.size();
85 while (Offset < Size) {
86 auto OldOffset = Data.getU32(&Offset);
87 auto NewOffset = OffsetRemapping[OldOffset];
88 Out.EmitIntValue(NewOffset, 4);
89 }
90
David Blaikie242b9482015-12-01 00:48:39 +000091 return std::error_code();
92}
93
David Blaikiead07b5d2015-12-04 17:20:04 +000094static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
95 uint64_t CurCode;
96 uint32_t Offset = 0;
97 DataExtractor AbbrevData(Abbrev, true, 0);
98 while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) {
99 // Tag
100 AbbrevData.getULEB128(&Offset);
101 // DW_CHILDREN
102 AbbrevData.getU8(&Offset);
103 // Attributes
104 while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset))
105 ;
106 }
107 return Offset;
108}
109
David Blaikief1958da2016-02-26 07:30:15 +0000110static const char *getCUName(StringRef Abbrev, StringRef Info,
111 StringRef StrOffsets, StringRef Str) {
112 uint32_t Offset = 0;
113 DataExtractor InfoData(Info, true, 0);
114 InfoData.getU32(&Offset); // Length
115 uint16_t Version = InfoData.getU16(&Offset);
116 InfoData.getU32(&Offset); // Abbrev offset (should be zero)
117 uint8_t AddrSize = InfoData.getU8(&Offset);
118
119 uint32_t AbbrCode = InfoData.getULEB128(&Offset);
120
121 DataExtractor AbbrevData(Abbrev, true, 0);
122 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
123 uint64_t Tag = AbbrevData.getULEB128(&AbbrevOffset);
124 (void)Tag;
125 // FIXME: Real error handling
126 assert(Tag == dwarf::DW_TAG_compile_unit);
127 // DW_CHILDREN
128 AbbrevData.getU8(&AbbrevOffset);
129 uint32_t Name;
130 uint32_t Form;
131 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |
132 (Form = AbbrevData.getULEB128(&AbbrevOffset)) &&
133 Name != dwarf::DW_AT_name) {
134 DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize);
135 }
136 // FIXME: Real error handling
137 assert(Name == dwarf::DW_AT_name);
138 auto StrIndex = InfoData.getULEB128(&Offset);
139
140 DataExtractor StrOffsetsData(StrOffsets, true, 0);
141 uint32_t StrOffsetsOffset = 4 * StrIndex;
142 uint32_t StrOffset = StrOffsetsData.getU32(&StrOffsetsOffset);
143 DataExtractor StrData(Str, true, 0);
144 return StrData.getCStr(&StrOffset);
145}
146
David Blaikiead07b5d2015-12-04 17:20:04 +0000147static uint64_t getCUSignature(StringRef Abbrev, StringRef Info) {
148 uint32_t Offset = 0;
149 DataExtractor InfoData(Info, true, 0);
150 InfoData.getU32(&Offset); // Length
151 uint16_t Version = InfoData.getU16(&Offset);
152 InfoData.getU32(&Offset); // Abbrev offset (should be zero)
153 uint8_t AddrSize = InfoData.getU8(&Offset);
154
155 uint32_t AbbrCode = InfoData.getULEB128(&Offset);
156
157 DataExtractor AbbrevData(Abbrev, true, 0);
158 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
159 uint64_t Tag = AbbrevData.getULEB128(&AbbrevOffset);
160 (void)Tag;
161 // FIXME: Real error handling
162 assert(Tag == dwarf::DW_TAG_compile_unit);
163 // DW_CHILDREN
164 AbbrevData.getU8(&AbbrevOffset);
165 uint32_t Name;
166 uint32_t Form;
167 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |
168 (Form = AbbrevData.getULEB128(&AbbrevOffset)) &&
169 Name != dwarf::DW_AT_GNU_dwo_id) {
170 DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize);
171 }
172 // FIXME: Real error handling
173 assert(Name == dwarf::DW_AT_GNU_dwo_id);
174 return InfoData.getU64(&Offset);
175}
176
David Blaikie24c8ac92015-12-05 03:05:45 +0000177struct UnitIndexEntry {
David Blaikie24c8ac92015-12-05 03:05:45 +0000178 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
David Blaikief1958da2016-02-26 07:30:15 +0000179 std::string Name;
180 StringRef DWPName;
David Blaikie24c8ac92015-12-05 03:05:45 +0000181};
182
David Blaikief1958da2016-02-26 07:30:15 +0000183StringRef getSubsection(StringRef Section, const DWARFUnitIndex::Entry &Entry, DWARFSectionKind Kind) {
184 const auto *Off = Entry.getOffset(Kind);
185 if (!Off)
186 return StringRef();
187 return Section.substr(Off->Offset, Off->Length);
188}
189
David Blaikie852c02b2016-02-19 21:09:26 +0000190static void addAllTypesFromDWP(
191 MCStreamer &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
192 const DWARFUnitIndex &TUIndex, MCSection *OutputTypes, StringRef Types,
193 const UnitIndexEntry &TUEntry, uint32_t &TypesOffset) {
David Blaikie8bce5a02016-02-17 07:00:24 +0000194 Out.SwitchSection(OutputTypes);
195 for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) {
196 auto *I = E.getOffsets();
197 if (!I)
198 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000199 auto P = TypeIndexEntries.insert(std::make_pair(E.getSignature(), TUEntry));
200 if (!P.second)
David Blaikie8bce5a02016-02-17 07:00:24 +0000201 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000202 auto &Entry = P.first->second;
David Blaikie8bce5a02016-02-17 07:00:24 +0000203 // Zero out the debug_info contribution
204 Entry.Contributions[0] = {};
205 for (auto Kind : TUIndex.getColumnKinds()) {
206 auto &C = Entry.Contributions[Kind - DW_SECT_INFO];
207 C.Offset += I->Offset;
208 C.Length = I->Length;
209 ++I;
210 }
211 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
212 Out.EmitBytes(Types.substr(
213 C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset,
214 C.Length));
215 C.Offset = TypesOffset;
216 TypesOffset += C.Length;
David Blaikie8bce5a02016-02-17 07:00:24 +0000217 }
218}
219
David Blaikiec3826da2015-12-09 21:02:33 +0000220static void addAllTypes(MCStreamer &Out,
David Blaikie852c02b2016-02-19 21:09:26 +0000221 MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
David Blaikiec3826da2015-12-09 21:02:33 +0000222 MCSection *OutputTypes, StringRef Types,
223 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) {
224 if (Types.empty())
225 return;
226
227 Out.SwitchSection(OutputTypes);
David Blaikie24c8ac92015-12-05 03:05:45 +0000228 uint32_t Offset = 0;
229 DataExtractor Data(Types, true, 0);
230 while (Data.isValidOffset(Offset)) {
David Blaikief5cb6272015-12-14 07:42:00 +0000231 UnitIndexEntry Entry = CUEntry;
David Blaikie24c8ac92015-12-05 03:05:45 +0000232 // Zero out the debug_info contribution
233 Entry.Contributions[0] = {};
234 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
David Blaikief5cb6272015-12-14 07:42:00 +0000235 C.Offset = TypesOffset;
David Blaikie24c8ac92015-12-05 03:05:45 +0000236 auto PrevOffset = Offset;
237 // Length of the unit, including the 4 byte length field.
238 C.Length = Data.getU32(&Offset) + 4;
239
240 Data.getU16(&Offset); // Version
241 Data.getU32(&Offset); // Abbrev offset
242 Data.getU8(&Offset); // Address size
David Blaikie852c02b2016-02-19 21:09:26 +0000243 auto Signature = Data.getU64(&Offset);
David Blaikie24c8ac92015-12-05 03:05:45 +0000244 Offset = PrevOffset + C.Length;
David Blaikief5cb6272015-12-14 07:42:00 +0000245
David Blaikie852c02b2016-02-19 21:09:26 +0000246 auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry));
247 if (!P.second)
David Blaikief5cb6272015-12-14 07:42:00 +0000248 continue;
249
250 Out.EmitBytes(Types.substr(PrevOffset, C.Length));
251 TypesOffset += C.Length;
David Blaikie24c8ac92015-12-05 03:05:45 +0000252 }
253}
254
255static void
256writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,
David Blaikie852c02b2016-02-19 21:09:26 +0000257 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries,
David Blaikie24c8ac92015-12-05 03:05:45 +0000258 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) {
259 for (const auto &E : IndexEntries)
David Blaikie852c02b2016-02-19 21:09:26 +0000260 for (size_t i = 0; i != array_lengthof(E.second.Contributions); ++i)
David Blaikie24c8ac92015-12-05 03:05:45 +0000261 if (ContributionOffsets[i])
David Blaikie852c02b2016-02-19 21:09:26 +0000262 Out.EmitIntValue(E.second.Contributions[i].*Field, 4);
David Blaikie24c8ac92015-12-05 03:05:45 +0000263}
264
David Blaikie852c02b2016-02-19 21:09:26 +0000265static void
266writeIndex(MCStreamer &Out, MCSection *Section,
267 ArrayRef<unsigned> ContributionOffsets,
268 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries) {
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000269 if (IndexEntries.empty())
270 return;
271
David Blaikie24c8ac92015-12-05 03:05:45 +0000272 unsigned Columns = 0;
273 for (auto &C : ContributionOffsets)
274 if (C)
275 ++Columns;
276
277 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
278 uint64_t Mask = Buckets.size() - 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000279 size_t i = 0;
280 for (const auto &P : IndexEntries) {
281 auto S = P.first;
David Blaikie24c8ac92015-12-05 03:05:45 +0000282 auto H = S & Mask;
David Blaikiec3826da2015-12-09 21:02:33 +0000283 while (Buckets[H]) {
David Blaikie852c02b2016-02-19 21:09:26 +0000284 assert(S != IndexEntries.begin()[Buckets[H] - 1].first &&
David Blaikie74f5b282016-02-19 01:51:44 +0000285 "Duplicate unit");
Benjamin Kramere5930942016-02-18 13:23:17 +0000286 H = (H + (((S >> 32) & Mask) | 1)) % Buckets.size();
David Blaikiec3826da2015-12-09 21:02:33 +0000287 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000288 Buckets[H] = i + 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000289 ++i;
David Blaikie24c8ac92015-12-05 03:05:45 +0000290 }
291
292 Out.SwitchSection(Section);
293 Out.EmitIntValue(2, 4); // Version
294 Out.EmitIntValue(Columns, 4); // Columns
295 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
David Blaikie2ed678c2015-12-05 03:06:30 +0000296 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets
David Blaikie24c8ac92015-12-05 03:05:45 +0000297
298 // Write the signatures.
299 for (const auto &I : Buckets)
David Blaikie852c02b2016-02-19 21:09:26 +0000300 Out.EmitIntValue(I ? IndexEntries.begin()[I - 1].first : 0, 8);
David Blaikie24c8ac92015-12-05 03:05:45 +0000301
302 // Write the indexes.
303 for (const auto &I : Buckets)
304 Out.EmitIntValue(I, 4);
305
306 // Write the column headers (which sections will appear in the table)
307 for (size_t i = 0; i != ContributionOffsets.size(); ++i)
308 if (ContributionOffsets[i])
309 Out.EmitIntValue(i + DW_SECT_INFO, 4);
310
311 // Write the offsets.
312 writeIndexTable(Out, ContributionOffsets, IndexEntries,
313 &DWARFUnitIndex::Entry::SectionContribution::Offset);
314
315 // Write the lengths.
316 writeIndexTable(Out, ContributionOffsets, IndexEntries,
317 &DWARFUnitIndex::Entry::SectionContribution::Length);
318}
David Blaikie74f5b282016-02-19 01:51:44 +0000319static bool consumeCompressedDebugSectionHeader(StringRef &data,
320 uint64_t &OriginalSize) {
321 // Consume "ZLIB" prefix.
322 if (!data.startswith("ZLIB"))
323 return false;
324 data = data.substr(4);
325 // Consume uncompressed section size (big-endian 8 bytes).
326 DataExtractor extractor(data, false, 8);
327 uint32_t Offset = 0;
328 OriginalSize = extractor.getU64(&Offset);
329 if (Offset == 0)
330 return false;
331 data = data.substr(Offset);
332 return true;
333}
334
David Blaikie242b9482015-12-01 00:48:39 +0000335static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000336 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
337 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
338 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikiec3826da2015-12-09 21:02:33 +0000339 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
David Blaikie23919372016-02-06 01:15:26 +0000340 MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection();
David Blaikie8bce5a02016-02-17 07:00:24 +0000341 MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000342 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
343 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
344 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
345 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
346 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
347 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
David Blaikieb7020252015-12-04 21:16:42 +0000348 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikie23919372016-02-06 01:15:26 +0000349 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
David Blaikie8bce5a02016-02-17 07:00:24 +0000350 {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}},
351 {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}};
David Blaikieb073cb92015-12-02 06:21:34 +0000352
David Blaikie852c02b2016-02-19 21:09:26 +0000353 MapVector<uint64_t, UnitIndexEntry> IndexEntries;
354 MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000355
356 StringMap<uint32_t> Strings;
357 uint32_t StringOffset = 0;
358
David Blaikieb073cb92015-12-02 06:21:34 +0000359 uint32_t ContributionOffsets[8] = {};
360
David Blaikie242b9482015-12-01 00:48:39 +0000361 for (const auto &Input : Inputs) {
362 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
363 if (!ErrOrObj)
364 return ErrOrObj.getError();
David Blaikieb073cb92015-12-02 06:21:34 +0000365
David Blaikie23919372016-02-06 01:15:26 +0000366 UnitIndexEntry CurEntry = {};
David Blaikieb073cb92015-12-02 06:21:34 +0000367
David Blaikie98ad82a2015-12-01 18:07:07 +0000368 StringRef CurStrSection;
369 StringRef CurStrOffsetSection;
David Blaikiec3826da2015-12-09 21:02:33 +0000370 StringRef CurTypesSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000371 StringRef InfoSection;
372 StringRef AbbrevSection;
David Blaikie23919372016-02-06 01:15:26 +0000373 StringRef CurCUIndexSection;
David Blaikie8bce5a02016-02-17 07:00:24 +0000374 StringRef CurTUIndexSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000375
David Blaikie74f5b282016-02-19 01:51:44 +0000376 SmallVector<SmallString<32>, 4> UncompressedSections;
377
David Blaikieddb27362016-03-01 21:24:04 +0000378 for (const auto &Section : ErrOrObj->getBinary()->sections()) {
David Blaikie74f5b282016-02-19 01:51:44 +0000379 if (Section.isBSS())
380 continue;
381 if (Section.isVirtual())
382 continue;
383
David Blaikie242b9482015-12-01 00:48:39 +0000384 StringRef Name;
385 if (std::error_code Err = Section.getName(Name))
386 return Err;
David Blaikieb073cb92015-12-02 06:21:34 +0000387
David Blaikie74f5b282016-02-19 01:51:44 +0000388 Name = Name.substr(Name.find_first_not_of("._"));
David Blaikieb073cb92015-12-02 06:21:34 +0000389
390 StringRef Contents;
391 if (auto Err = Section.getContents(Contents))
392 return Err;
393
David Blaikie74f5b282016-02-19 01:51:44 +0000394 if (Name.startswith("zdebug_")) {
395 uint64_t OriginalSize;
396 if (!zlib::isAvailable() ||
397 !consumeCompressedDebugSectionHeader(Contents, OriginalSize))
David Blaikie9a5d3642016-02-19 02:03:45 +0000398 return make_error_code(std::errc::invalid_argument);
David Blaikie74f5b282016-02-19 01:51:44 +0000399 UncompressedSections.resize(UncompressedSections.size() + 1);
400 if (zlib::uncompress(Contents, UncompressedSections.back(), OriginalSize) !=
401 zlib::StatusOK) {
402 UncompressedSections.pop_back();
403 continue;
404 }
405 Name = Name.substr(1);
406 Contents = UncompressedSections.back();
407 }
408
409 auto SectionPair = KnownSections.find(Name);
410 if (SectionPair == KnownSections.end())
411 continue;
412
David Blaikieb073cb92015-12-02 06:21:34 +0000413 if (DWARFSectionKind Kind = SectionPair->second.second) {
414 auto Index = Kind - DW_SECT_INFO;
David Blaikiec3826da2015-12-09 21:02:33 +0000415 if (Kind != DW_SECT_TYPES) {
416 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
417 ContributionOffsets[Index] +=
418 (CurEntry.Contributions[Index].Length = Contents.size());
419 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000420
David Blaikie24c8ac92015-12-05 03:05:45 +0000421 switch (Kind) {
422 case DW_SECT_INFO:
David Blaikiead07b5d2015-12-04 17:20:04 +0000423 InfoSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000424 break;
425 case DW_SECT_ABBREV:
David Blaikiead07b5d2015-12-04 17:20:04 +0000426 AbbrevSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000427 break;
David Blaikie24c8ac92015-12-05 03:05:45 +0000428 default:
429 break;
David Blaikiead07b5d2015-12-04 17:20:04 +0000430 }
David Blaikieb073cb92015-12-02 06:21:34 +0000431 }
432
433 MCSection *OutSection = SectionPair->second.first;
434 if (OutSection == StrOffsetSection)
435 CurStrOffsetSection = Contents;
436 else if (OutSection == StrSection)
437 CurStrSection = Contents;
David Blaikiec3826da2015-12-09 21:02:33 +0000438 else if (OutSection == TypesSection)
439 CurTypesSection = Contents;
David Blaikie23919372016-02-06 01:15:26 +0000440 else if (OutSection == CUIndexSection)
441 CurCUIndexSection = Contents;
David Blaikie8bce5a02016-02-17 07:00:24 +0000442 else if (OutSection == TUIndexSection)
443 CurTUIndexSection = Contents;
David Blaikieb073cb92015-12-02 06:21:34 +0000444 else {
445 Out.SwitchSection(OutSection);
446 Out.EmitBytes(Contents);
David Blaikie98ad82a2015-12-01 18:07:07 +0000447 }
David Blaikie242b9482015-12-01 00:48:39 +0000448 }
David Blaikieb073cb92015-12-02 06:21:34 +0000449
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000450 if (InfoSection.empty())
451 continue;
452
David Blaikie23919372016-02-06 01:15:26 +0000453 if (!CurCUIndexSection.empty()) {
454 DWARFUnitIndex CUIndex(DW_SECT_INFO);
David Blaikieddb27362016-03-01 21:24:04 +0000455 DataExtractor CUIndexData(CurCUIndexSection,
456 ErrOrObj->getBinary()->isLittleEndian(), 0);
David Blaikie23919372016-02-06 01:15:26 +0000457 if (!CUIndex.parse(CUIndexData))
458 return make_error_code(std::errc::invalid_argument);
459
460 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
David Blaikie23919372016-02-06 01:15:26 +0000461 auto *I = E.getOffsets();
462 if (!I)
463 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000464 auto P =
465 IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry));
David Blaikief1958da2016-02-26 07:30:15 +0000466 const char* Name = getCUName(
467 getSubsection(AbbrevSection, E, DW_SECT_ABBREV),
468 getSubsection(InfoSection, E, DW_SECT_INFO),
469 getSubsection(CurStrOffsetSection, E, DW_SECT_STR_OFFSETS),
470 CurStrSection);
471 if (!P.second) {
472 auto &PrevE = *P.first;
473 std::cerr << "Duplicate DWO ID (" << PrevE.first << ") in '" << PrevE.second.Name << "' ";
474 if (!PrevE.second.DWPName.empty())
475 std::cerr << "(from '" << PrevE.second.DWPName.str() << "') ";
476 std::cerr << "and '" << Name << "' (from '" << Input << "')\n";
477 return make_error_code(std::errc::invalid_argument);
478 }
David Blaikie852c02b2016-02-19 21:09:26 +0000479 auto &NewEntry = P.first->second;
David Blaikief1958da2016-02-26 07:30:15 +0000480 NewEntry.Name = Name;
481 NewEntry.DWPName = Input;
David Blaikie23919372016-02-06 01:15:26 +0000482 for (auto Kind : CUIndex.getColumnKinds()) {
483 auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO];
484 C.Offset += I->Offset;
485 C.Length = I->Length;
486 ++I;
487 }
David Blaikie23919372016-02-06 01:15:26 +0000488 }
David Blaikie8bce5a02016-02-17 07:00:24 +0000489
490 if (!CurTypesSection.empty()) {
491 if (CurTUIndexSection.empty())
492 return make_error_code(std::errc::invalid_argument);
493 DWARFUnitIndex TUIndex(DW_SECT_TYPES);
David Blaikieddb27362016-03-01 21:24:04 +0000494 DataExtractor TUIndexData(CurTUIndexSection,
495 ErrOrObj->getBinary()->isLittleEndian(), 0);
David Blaikie8bce5a02016-02-17 07:00:24 +0000496 if (!TUIndex.parse(TUIndexData))
497 return make_error_code(std::errc::invalid_argument);
498 addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection,
499 CurTypesSection, CurEntry,
500 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
501 }
David Blaikie23919372016-02-06 01:15:26 +0000502 } else {
David Blaikief1958da2016-02-26 07:30:15 +0000503 auto P = IndexEntries.insert(
David Blaikie852c02b2016-02-19 21:09:26 +0000504 std::make_pair(getCUSignature(AbbrevSection, InfoSection), CurEntry));
David Blaikief1958da2016-02-26 07:30:15 +0000505 const char *Name = getCUName(AbbrevSection, InfoSection,
506 CurStrOffsetSection, CurStrSection);
507 if (!P.second) {
508 auto &E = *P.first;
509 std::cerr << "Duplicate DWO ID (" << E.first << ") in '" << Name
510 << "' ";
511 if (!E.second.DWPName.empty())
512 std::cerr << "(from '" << E.second.DWPName.str() << "') ";
513 std::cerr << "and '" << E.second.Name << "'\n";
514 return make_error_code(std::errc::invalid_argument);
515 }
516 P.first->second.Name = Name;
David Blaikie23919372016-02-06 01:15:26 +0000517 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection,
518 CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
David Blaikie23919372016-02-06 01:15:26 +0000519 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000520
David Blaikiebb94e442015-12-01 19:17:58 +0000521 if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset,
522 StrSection, StrOffsetSection,
523 CurStrSection, CurStrOffsetSection))
David Blaikie98ad82a2015-12-01 18:07:07 +0000524 return Err;
David Blaikie242b9482015-12-01 00:48:39 +0000525 }
David Blaikieb073cb92015-12-02 06:21:34 +0000526
David Blaikie5d6d4dc2016-02-26 07:04:58 +0000527 // Lie about there being no info contributions so the TU index only includes
528 // the type unit contribution
529 ContributionOffsets[0] = 0;
530 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
531 TypeIndexEntries);
David Blaikieb3757c02015-12-02 22:01:56 +0000532
David Blaikie24c8ac92015-12-05 03:05:45 +0000533 // Lie about the type contribution
534 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
535 // Unlie about the info contribution
536 ContributionOffsets[0] = 1;
David Blaikie7c4ffe02015-12-04 21:30:23 +0000537
David Blaikie24c8ac92015-12-05 03:05:45 +0000538 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
539 IndexEntries);
David Blaikieddb27362016-03-01 21:24:04 +0000540
David Blaikie242b9482015-12-01 00:48:39 +0000541 return std::error_code();
542}
543
David Blaikie2ed678c2015-12-05 03:06:30 +0000544int main(int argc, char **argv) {
David Blaikie242b9482015-12-01 00:48:39 +0000545
546 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
547
548 llvm::InitializeAllTargetInfos();
549 llvm::InitializeAllTargetMCs();
550 llvm::InitializeAllTargets();
551 llvm::InitializeAllAsmPrinters();
552
553 std::string ErrorStr;
554 StringRef Context = "dwarf streamer init";
555
556 Triple TheTriple("x86_64-linux-gnu");
557
558 // Get the target.
559 const Target *TheTarget =
560 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
561 if (!TheTarget)
562 return error(ErrorStr, Context);
563 std::string TripleName = TheTriple.getTriple();
564
565 // Create all the MC Objects.
566 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
567 if (!MRI)
568 return error(Twine("no register info for target ") + TripleName, Context);
569
570 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
571 if (!MAI)
572 return error("no asm info for target " + TripleName, Context);
573
574 MCObjectFileInfo MOFI;
575 MCContext MC(MAI.get(), MRI.get(), &MOFI);
David Blaikie2ed678c2015-12-05 03:06:30 +0000576 MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default, MC);
David Blaikie242b9482015-12-01 00:48:39 +0000577
578 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
579 if (!MAB)
580 return error("no asm backend for target " + TripleName, Context);
581
582 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
583 if (!MII)
584 return error("no instr info info for target " + TripleName, Context);
585
586 std::unique_ptr<MCSubtargetInfo> MSTI(
587 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
588 if (!MSTI)
589 return error("no subtarget info for target " + TripleName, Context);
590
591 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
592 if (!MCE)
593 return error("no code emitter for target " + TripleName, Context);
594
595 // Create the output file.
596 std::error_code EC;
597 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
598 if (EC)
599 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
600
David Majnemer03e2cc32015-12-21 22:09:27 +0000601 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
David Blaikie242b9482015-12-01 00:48:39 +0000602 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
David Majnemer03e2cc32015-12-21 22:09:27 +0000603 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, MCOptions.MCRelaxAll,
604 MCOptions.MCIncrementalLinkerCompatible,
David Blaikie242b9482015-12-01 00:48:39 +0000605 /*DWARFMustBeAtTheEnd*/ false));
606 if (!MS)
607 return error("no object streamer for target " + TripleName, Context);
608
609 if (auto Err = write(*MS, InputFiles))
610 return error(Err.message(), "Writing DWP file");
David Blaikieddb27362016-03-01 21:24:04 +0000611
612 MS->Finish();
David Blaikiedf055252015-12-01 00:48:34 +0000613}