blob: 52dee3add4807c1f05a64d3774dbcf15f87f0206 [file] [log] [blame]
David Blaikie242b9482015-12-01 00:48:39 +00001#include "llvm/ADT/STLExtras.h"
2#include "llvm/ADT/StringSet.h"
3#include "llvm/CodeGen/AsmPrinter.h"
David Blaikie2ed678c2015-12-05 03:06:30 +00004#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
5#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
David Blaikie242b9482015-12-01 00:48:39 +00006#include "llvm/MC/MCAsmInfo.h"
7#include "llvm/MC/MCContext.h"
8#include "llvm/MC/MCInstrInfo.h"
9#include "llvm/MC/MCObjectFileInfo.h"
10#include "llvm/MC/MCRegisterInfo.h"
11#include "llvm/MC/MCSectionELF.h"
12#include "llvm/MC/MCStreamer.h"
David Majnemer03e2cc32015-12-21 22:09:27 +000013#include "llvm/MC/MCTargetOptionsCommandFlags.h"
David Blaikie242b9482015-12-01 00:48:39 +000014#include "llvm/Object/ObjectFile.h"
David Blaikie74f5b282016-02-19 01:51:44 +000015#include "llvm/Support/Compression.h"
David Blaikie98ad82a2015-12-01 18:07:07 +000016#include "llvm/Support/DataExtractor.h"
David Blaikie242b9482015-12-01 00:48:39 +000017#include "llvm/Support/FileSystem.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000018#include "llvm/Support/MathExtras.h"
David Blaikie242b9482015-12-01 00:48:39 +000019#include "llvm/Support/MemoryBuffer.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000020#include "llvm/Support/Options.h"
David Blaikie242b9482015-12-01 00:48:39 +000021#include "llvm/Support/TargetRegistry.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000022#include "llvm/Support/TargetSelect.h"
David Blaikie242b9482015-12-01 00:48:39 +000023#include "llvm/Support/raw_ostream.h"
24#include "llvm/Target/TargetMachine.h"
David Blaikie242b9482015-12-01 00:48:39 +000025#include <list>
David Blaikie2ed678c2015-12-05 03:06:30 +000026#include <memory>
David Blaikie242b9482015-12-01 00:48:39 +000027#include <unordered_set>
David Blaikie23919372016-02-06 01:15:26 +000028#include <system_error>
David Blaikie242b9482015-12-01 00:48:39 +000029
30using namespace llvm;
David Blaikie98ad82a2015-12-01 18:07:07 +000031using namespace llvm::object;
David Blaikie242b9482015-12-01 00:48:39 +000032using namespace cl;
33
34OptionCategory DwpCategory("Specific Options");
35static list<std::string> InputFiles(Positional, OneOrMore,
36 desc("<input files>"), cat(DwpCategory));
37
David Blaikie2ed678c2015-12-05 03:06:30 +000038static opt<std::string> OutputFilename(Required, "o",
39 desc("Specify the output file."),
40 value_desc("filename"),
41 cat(DwpCategory));
David Blaikie242b9482015-12-01 00:48:39 +000042
43static int error(const Twine &Error, const Twine &Context) {
44 errs() << Twine("while processing ") + Context + ":\n";
45 errs() << Twine("error: ") + Error + "\n";
46 return 1;
47}
48
David Blaikie98ad82a2015-12-01 18:07:07 +000049static std::error_code
50writeStringsAndOffsets(MCStreamer &Out, StringMap<uint32_t> &Strings,
David Blaikiebb94e442015-12-01 19:17:58 +000051 uint32_t &StringOffset, MCSection *StrSection,
52 MCSection *StrOffsetSection, StringRef CurStrSection,
53 StringRef CurStrOffsetSection) {
David Blaikie98ad82a2015-12-01 18:07:07 +000054 // Could possibly produce an error or warning if one of these was non-null but
55 // the other was null.
56 if (CurStrSection.empty() || CurStrOffsetSection.empty())
57 return std::error_code();
58
59 DenseMap<uint32_t, uint32_t> OffsetRemapping;
60
61 DataExtractor Data(CurStrSection, true, 0);
62 uint32_t LocalOffset = 0;
63 uint32_t PrevOffset = 0;
64 while (const char *s = Data.getCStr(&LocalOffset)) {
65 StringRef Str(s, LocalOffset - PrevOffset - 1);
David Blaikiebb94e442015-12-01 19:17:58 +000066 auto Pair = Strings.insert(std::make_pair(Str, StringOffset));
67 if (Pair.second) {
68 Out.SwitchSection(StrSection);
69 Out.EmitBytes(
70 StringRef(Pair.first->getKeyData(), Pair.first->getKeyLength() + 1));
71 StringOffset += Str.size() + 1;
72 }
73 OffsetRemapping[PrevOffset] = Pair.first->second;
David Blaikie98ad82a2015-12-01 18:07:07 +000074 PrevOffset = LocalOffset;
75 }
76
77 Data = DataExtractor(CurStrOffsetSection, true, 0);
78
79 Out.SwitchSection(StrOffsetSection);
80
81 uint32_t Offset = 0;
82 uint64_t Size = CurStrOffsetSection.size();
83 while (Offset < Size) {
84 auto OldOffset = Data.getU32(&Offset);
85 auto NewOffset = OffsetRemapping[OldOffset];
86 Out.EmitIntValue(NewOffset, 4);
87 }
88
David Blaikie242b9482015-12-01 00:48:39 +000089 return std::error_code();
90}
91
David Blaikiead07b5d2015-12-04 17:20:04 +000092static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
93 uint64_t CurCode;
94 uint32_t Offset = 0;
95 DataExtractor AbbrevData(Abbrev, true, 0);
96 while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) {
97 // Tag
98 AbbrevData.getULEB128(&Offset);
99 // DW_CHILDREN
100 AbbrevData.getU8(&Offset);
101 // Attributes
102 while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset))
103 ;
104 }
105 return Offset;
106}
107
108static uint64_t getCUSignature(StringRef Abbrev, StringRef Info) {
109 uint32_t Offset = 0;
110 DataExtractor InfoData(Info, true, 0);
111 InfoData.getU32(&Offset); // Length
112 uint16_t Version = InfoData.getU16(&Offset);
113 InfoData.getU32(&Offset); // Abbrev offset (should be zero)
114 uint8_t AddrSize = InfoData.getU8(&Offset);
115
116 uint32_t AbbrCode = InfoData.getULEB128(&Offset);
117
118 DataExtractor AbbrevData(Abbrev, true, 0);
119 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
120 uint64_t Tag = AbbrevData.getULEB128(&AbbrevOffset);
121 (void)Tag;
122 // FIXME: Real error handling
123 assert(Tag == dwarf::DW_TAG_compile_unit);
124 // DW_CHILDREN
125 AbbrevData.getU8(&AbbrevOffset);
126 uint32_t Name;
127 uint32_t Form;
128 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |
129 (Form = AbbrevData.getULEB128(&AbbrevOffset)) &&
130 Name != dwarf::DW_AT_GNU_dwo_id) {
131 DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize);
132 }
133 // FIXME: Real error handling
134 assert(Name == dwarf::DW_AT_GNU_dwo_id);
135 return InfoData.getU64(&Offset);
136}
137
David Blaikie24c8ac92015-12-05 03:05:45 +0000138struct UnitIndexEntry {
139 uint64_t Signature;
140 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
141};
142
David Blaikie8bce5a02016-02-17 07:00:24 +0000143static void addAllTypesFromDWP(MCStreamer &Out,
144 std::vector<UnitIndexEntry> &TypeIndexEntries,
145 const DWARFUnitIndex &TUIndex,
146 MCSection *OutputTypes, StringRef Types,
147 const UnitIndexEntry &TUEntry,
148 uint32_t &TypesOffset) {
149 Out.SwitchSection(OutputTypes);
150 for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) {
151 auto *I = E.getOffsets();
152 if (!I)
153 continue;
154 if (any_of(TypeIndexEntries, [&](const UnitIndexEntry &OldEntry) {
155 return OldEntry.Signature == E.getSignature();
156 }))
157 continue;
158 auto Entry = TUEntry;
159 Entry.Signature = E.getSignature();
160 // Zero out the debug_info contribution
161 Entry.Contributions[0] = {};
162 for (auto Kind : TUIndex.getColumnKinds()) {
163 auto &C = Entry.Contributions[Kind - DW_SECT_INFO];
164 C.Offset += I->Offset;
165 C.Length = I->Length;
166 ++I;
167 }
168 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
169 Out.EmitBytes(Types.substr(
170 C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset,
171 C.Length));
172 C.Offset = TypesOffset;
173 TypesOffset += C.Length;
174 TypeIndexEntries.push_back(Entry);
175 }
176}
177
David Blaikiec3826da2015-12-09 21:02:33 +0000178static void addAllTypes(MCStreamer &Out,
179 std::vector<UnitIndexEntry> &TypeIndexEntries,
180 MCSection *OutputTypes, StringRef Types,
181 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) {
182 if (Types.empty())
183 return;
184
185 Out.SwitchSection(OutputTypes);
David Blaikie24c8ac92015-12-05 03:05:45 +0000186 uint32_t Offset = 0;
187 DataExtractor Data(Types, true, 0);
188 while (Data.isValidOffset(Offset)) {
David Blaikief5cb6272015-12-14 07:42:00 +0000189 UnitIndexEntry Entry = CUEntry;
David Blaikie24c8ac92015-12-05 03:05:45 +0000190 // Zero out the debug_info contribution
191 Entry.Contributions[0] = {};
192 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
David Blaikief5cb6272015-12-14 07:42:00 +0000193 C.Offset = TypesOffset;
David Blaikie24c8ac92015-12-05 03:05:45 +0000194 auto PrevOffset = Offset;
195 // Length of the unit, including the 4 byte length field.
196 C.Length = Data.getU32(&Offset) + 4;
197
198 Data.getU16(&Offset); // Version
199 Data.getU32(&Offset); // Abbrev offset
200 Data.getU8(&Offset); // Address size
201 Entry.Signature = Data.getU64(&Offset);
202 Offset = PrevOffset + C.Length;
David Blaikief5cb6272015-12-14 07:42:00 +0000203
204 if (any_of(TypeIndexEntries, [&](const UnitIndexEntry &E) {
205 return E.Signature == Entry.Signature;
206 }))
207 continue;
208
209 Out.EmitBytes(Types.substr(PrevOffset, C.Length));
210 TypesOffset += C.Length;
211
212 TypeIndexEntries.push_back(Entry);
David Blaikie24c8ac92015-12-05 03:05:45 +0000213 }
214}
215
216static void
217writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,
218 ArrayRef<UnitIndexEntry> IndexEntries,
219 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) {
220 for (const auto &E : IndexEntries)
221 for (size_t i = 0; i != array_lengthof(E.Contributions); ++i)
222 if (ContributionOffsets[i])
223 Out.EmitIntValue(E.Contributions[i].*Field, 4);
224}
225
226static void writeIndex(MCStreamer &Out, MCSection *Section,
227 ArrayRef<unsigned> ContributionOffsets,
228 ArrayRef<UnitIndexEntry> IndexEntries) {
229 unsigned Columns = 0;
230 for (auto &C : ContributionOffsets)
231 if (C)
232 ++Columns;
233
234 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
235 uint64_t Mask = Buckets.size() - 1;
236 for (size_t i = 0; i != IndexEntries.size(); ++i) {
237 auto S = IndexEntries[i].Signature;
238 auto H = S & Mask;
David Blaikiec3826da2015-12-09 21:02:33 +0000239 while (Buckets[H]) {
240 assert(S != IndexEntries[Buckets[H] - 1].Signature &&
David Blaikie74f5b282016-02-19 01:51:44 +0000241 "Duplicate unit");
Benjamin Kramere5930942016-02-18 13:23:17 +0000242 H = (H + (((S >> 32) & Mask) | 1)) % Buckets.size();
David Blaikiec3826da2015-12-09 21:02:33 +0000243 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000244 Buckets[H] = i + 1;
245 }
246
247 Out.SwitchSection(Section);
248 Out.EmitIntValue(2, 4); // Version
249 Out.EmitIntValue(Columns, 4); // Columns
250 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
David Blaikie2ed678c2015-12-05 03:06:30 +0000251 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets
David Blaikie24c8ac92015-12-05 03:05:45 +0000252
253 // Write the signatures.
254 for (const auto &I : Buckets)
255 Out.EmitIntValue(I ? IndexEntries[I - 1].Signature : 0, 8);
256
257 // Write the indexes.
258 for (const auto &I : Buckets)
259 Out.EmitIntValue(I, 4);
260
261 // Write the column headers (which sections will appear in the table)
262 for (size_t i = 0; i != ContributionOffsets.size(); ++i)
263 if (ContributionOffsets[i])
264 Out.EmitIntValue(i + DW_SECT_INFO, 4);
265
266 // Write the offsets.
267 writeIndexTable(Out, ContributionOffsets, IndexEntries,
268 &DWARFUnitIndex::Entry::SectionContribution::Offset);
269
270 // Write the lengths.
271 writeIndexTable(Out, ContributionOffsets, IndexEntries,
272 &DWARFUnitIndex::Entry::SectionContribution::Length);
273}
David Blaikie74f5b282016-02-19 01:51:44 +0000274static bool consumeCompressedDebugSectionHeader(StringRef &data,
275 uint64_t &OriginalSize) {
276 // Consume "ZLIB" prefix.
277 if (!data.startswith("ZLIB"))
278 return false;
279 data = data.substr(4);
280 // Consume uncompressed section size (big-endian 8 bytes).
281 DataExtractor extractor(data, false, 8);
282 uint32_t Offset = 0;
283 OriginalSize = extractor.getU64(&Offset);
284 if (Offset == 0)
285 return false;
286 data = data.substr(Offset);
287 return true;
288}
289
David Blaikie242b9482015-12-01 00:48:39 +0000290static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000291 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
292 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
293 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikiec3826da2015-12-09 21:02:33 +0000294 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
David Blaikie23919372016-02-06 01:15:26 +0000295 MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection();
David Blaikie8bce5a02016-02-17 07:00:24 +0000296 MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000297 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
298 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
299 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
300 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
301 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
302 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
David Blaikieb7020252015-12-04 21:16:42 +0000303 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikie23919372016-02-06 01:15:26 +0000304 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
David Blaikie8bce5a02016-02-17 07:00:24 +0000305 {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}},
306 {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}};
David Blaikieb073cb92015-12-02 06:21:34 +0000307
David Blaikieb073cb92015-12-02 06:21:34 +0000308 std::vector<UnitIndexEntry> IndexEntries;
David Blaikie24c8ac92015-12-05 03:05:45 +0000309 std::vector<UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000310
311 StringMap<uint32_t> Strings;
312 uint32_t StringOffset = 0;
313
David Blaikieb073cb92015-12-02 06:21:34 +0000314 uint32_t ContributionOffsets[8] = {};
315
David Blaikie242b9482015-12-01 00:48:39 +0000316 for (const auto &Input : Inputs) {
317 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
318 if (!ErrOrObj)
319 return ErrOrObj.getError();
David Blaikieb073cb92015-12-02 06:21:34 +0000320
David Blaikie23919372016-02-06 01:15:26 +0000321 UnitIndexEntry CurEntry = {};
David Blaikieb073cb92015-12-02 06:21:34 +0000322
David Blaikie98ad82a2015-12-01 18:07:07 +0000323 StringRef CurStrSection;
324 StringRef CurStrOffsetSection;
David Blaikiec3826da2015-12-09 21:02:33 +0000325 StringRef CurTypesSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000326 StringRef InfoSection;
327 StringRef AbbrevSection;
David Blaikie23919372016-02-06 01:15:26 +0000328 StringRef CurCUIndexSection;
David Blaikie8bce5a02016-02-17 07:00:24 +0000329 StringRef CurTUIndexSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000330
David Blaikie74f5b282016-02-19 01:51:44 +0000331 SmallVector<SmallString<32>, 4> UncompressedSections;
332
David Blaikieb073cb92015-12-02 06:21:34 +0000333 for (const auto &Section : ErrOrObj->getBinary()->sections()) {
David Blaikie74f5b282016-02-19 01:51:44 +0000334 if (Section.isBSS())
335 continue;
336 if (Section.isVirtual())
337 continue;
338
David Blaikie242b9482015-12-01 00:48:39 +0000339 StringRef Name;
340 if (std::error_code Err = Section.getName(Name))
341 return Err;
David Blaikieb073cb92015-12-02 06:21:34 +0000342
David Blaikie74f5b282016-02-19 01:51:44 +0000343 Name = Name.substr(Name.find_first_not_of("._"));
David Blaikieb073cb92015-12-02 06:21:34 +0000344
345 StringRef Contents;
346 if (auto Err = Section.getContents(Contents))
347 return Err;
348
David Blaikie74f5b282016-02-19 01:51:44 +0000349 if (Name.startswith("zdebug_")) {
350 uint64_t OriginalSize;
351 if (!zlib::isAvailable() ||
352 !consumeCompressedDebugSectionHeader(Contents, OriginalSize))
353 continue;
354 UncompressedSections.resize(UncompressedSections.size() + 1);
355 if (zlib::uncompress(Contents, UncompressedSections.back(), OriginalSize) !=
356 zlib::StatusOK) {
357 UncompressedSections.pop_back();
358 continue;
359 }
360 Name = Name.substr(1);
361 Contents = UncompressedSections.back();
362 }
363
364 auto SectionPair = KnownSections.find(Name);
365 if (SectionPair == KnownSections.end())
366 continue;
367
David Blaikieb073cb92015-12-02 06:21:34 +0000368 if (DWARFSectionKind Kind = SectionPair->second.second) {
369 auto Index = Kind - DW_SECT_INFO;
David Blaikiec3826da2015-12-09 21:02:33 +0000370 if (Kind != DW_SECT_TYPES) {
371 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
372 ContributionOffsets[Index] +=
373 (CurEntry.Contributions[Index].Length = Contents.size());
374 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000375
David Blaikie24c8ac92015-12-05 03:05:45 +0000376 switch (Kind) {
377 case DW_SECT_INFO:
David Blaikiead07b5d2015-12-04 17:20:04 +0000378 InfoSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000379 break;
380 case DW_SECT_ABBREV:
David Blaikiead07b5d2015-12-04 17:20:04 +0000381 AbbrevSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000382 break;
David Blaikie24c8ac92015-12-05 03:05:45 +0000383 default:
384 break;
David Blaikiead07b5d2015-12-04 17:20:04 +0000385 }
David Blaikieb073cb92015-12-02 06:21:34 +0000386 }
387
388 MCSection *OutSection = SectionPair->second.first;
389 if (OutSection == StrOffsetSection)
390 CurStrOffsetSection = Contents;
391 else if (OutSection == StrSection)
392 CurStrSection = Contents;
David Blaikiec3826da2015-12-09 21:02:33 +0000393 else if (OutSection == TypesSection)
394 CurTypesSection = Contents;
David Blaikie23919372016-02-06 01:15:26 +0000395 else if (OutSection == CUIndexSection)
396 CurCUIndexSection = Contents;
David Blaikie8bce5a02016-02-17 07:00:24 +0000397 else if (OutSection == TUIndexSection)
398 CurTUIndexSection = Contents;
David Blaikieb073cb92015-12-02 06:21:34 +0000399 else {
400 Out.SwitchSection(OutSection);
401 Out.EmitBytes(Contents);
David Blaikie98ad82a2015-12-01 18:07:07 +0000402 }
David Blaikie242b9482015-12-01 00:48:39 +0000403 }
David Blaikieb073cb92015-12-02 06:21:34 +0000404
David Blaikiead07b5d2015-12-04 17:20:04 +0000405 assert(!AbbrevSection.empty());
406 assert(!InfoSection.empty());
David Blaikie23919372016-02-06 01:15:26 +0000407 if (!CurCUIndexSection.empty()) {
408 DWARFUnitIndex CUIndex(DW_SECT_INFO);
409 DataExtractor CUIndexData(CurCUIndexSection,
410 ErrOrObj->getBinary()->isLittleEndian(), 0);
411 if (!CUIndex.parse(CUIndexData))
412 return make_error_code(std::errc::invalid_argument);
413
414 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
415 auto NewEntry = CurEntry;
416 auto *I = E.getOffsets();
417 if (!I)
418 continue;
419 NewEntry.Signature = E.getSignature();
420 for (auto Kind : CUIndex.getColumnKinds()) {
421 auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO];
422 C.Offset += I->Offset;
423 C.Length = I->Length;
424 ++I;
425 }
426 IndexEntries.push_back(NewEntry);
427 }
David Blaikie8bce5a02016-02-17 07:00:24 +0000428
429 if (!CurTypesSection.empty()) {
430 if (CurTUIndexSection.empty())
431 return make_error_code(std::errc::invalid_argument);
432 DWARFUnitIndex TUIndex(DW_SECT_TYPES);
433 DataExtractor TUIndexData(CurTUIndexSection,
434 ErrOrObj->getBinary()->isLittleEndian(), 0);
435 if (!TUIndex.parse(TUIndexData))
436 return make_error_code(std::errc::invalid_argument);
437 addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection,
438 CurTypesSection, CurEntry,
439 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
440 }
David Blaikie23919372016-02-06 01:15:26 +0000441 } else {
442 CurEntry.Signature = getCUSignature(AbbrevSection, InfoSection);
443 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection,
444 CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
445
446 IndexEntries.push_back(CurEntry);
447 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000448
David Blaikiebb94e442015-12-01 19:17:58 +0000449 if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset,
450 StrSection, StrOffsetSection,
451 CurStrSection, CurStrOffsetSection))
David Blaikie98ad82a2015-12-01 18:07:07 +0000452 return Err;
David Blaikie242b9482015-12-01 00:48:39 +0000453 }
David Blaikieb073cb92015-12-02 06:21:34 +0000454
David Blaikie9e51c842015-12-05 03:41:53 +0000455 if (!TypeIndexEntries.empty()) {
456 // Lie about there being no info contributions so the TU index only includes
457 // the type unit contribution
458 ContributionOffsets[0] = 0;
459 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
460 TypeIndexEntries);
461 }
David Blaikieb3757c02015-12-02 22:01:56 +0000462
David Blaikie24c8ac92015-12-05 03:05:45 +0000463 // Lie about the type contribution
464 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
465 // Unlie about the info contribution
466 ContributionOffsets[0] = 1;
David Blaikie7c4ffe02015-12-04 21:30:23 +0000467
David Blaikie24c8ac92015-12-05 03:05:45 +0000468 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
469 IndexEntries);
David Blaikieb073cb92015-12-02 06:21:34 +0000470
David Blaikie242b9482015-12-01 00:48:39 +0000471 return std::error_code();
472}
473
David Blaikie2ed678c2015-12-05 03:06:30 +0000474int main(int argc, char **argv) {
David Blaikie242b9482015-12-01 00:48:39 +0000475
476 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
477
478 llvm::InitializeAllTargetInfos();
479 llvm::InitializeAllTargetMCs();
480 llvm::InitializeAllTargets();
481 llvm::InitializeAllAsmPrinters();
482
483 std::string ErrorStr;
484 StringRef Context = "dwarf streamer init";
485
486 Triple TheTriple("x86_64-linux-gnu");
487
488 // Get the target.
489 const Target *TheTarget =
490 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
491 if (!TheTarget)
492 return error(ErrorStr, Context);
493 std::string TripleName = TheTriple.getTriple();
494
495 // Create all the MC Objects.
496 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
497 if (!MRI)
498 return error(Twine("no register info for target ") + TripleName, Context);
499
500 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
501 if (!MAI)
502 return error("no asm info for target " + TripleName, Context);
503
504 MCObjectFileInfo MOFI;
505 MCContext MC(MAI.get(), MRI.get(), &MOFI);
David Blaikie2ed678c2015-12-05 03:06:30 +0000506 MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default, MC);
David Blaikie242b9482015-12-01 00:48:39 +0000507
508 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
509 if (!MAB)
510 return error("no asm backend for target " + TripleName, Context);
511
512 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
513 if (!MII)
514 return error("no instr info info for target " + TripleName, Context);
515
516 std::unique_ptr<MCSubtargetInfo> MSTI(
517 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
518 if (!MSTI)
519 return error("no subtarget info for target " + TripleName, Context);
520
521 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
522 if (!MCE)
523 return error("no code emitter for target " + TripleName, Context);
524
525 // Create the output file.
526 std::error_code EC;
527 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
528 if (EC)
529 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
530
David Majnemer03e2cc32015-12-21 22:09:27 +0000531 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
David Blaikie242b9482015-12-01 00:48:39 +0000532 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
David Majnemer03e2cc32015-12-21 22:09:27 +0000533 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, MCOptions.MCRelaxAll,
534 MCOptions.MCIncrementalLinkerCompatible,
David Blaikie242b9482015-12-01 00:48:39 +0000535 /*DWARFMustBeAtTheEnd*/ false));
536 if (!MS)
537 return error("no object streamer for target " + TripleName, Context);
538
539 if (auto Err = write(*MS, InputFiles))
540 return error(Err.message(), "Writing DWP file");
541
542 MS->Finish();
David Blaikiedf055252015-12-01 00:48:34 +0000543}