blob: 9bc327605910e4e07f800c1e6fb8dd682cb8e498 [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 Blaikie98ad82a2015-12-01 18:07:07 +000015#include "llvm/Support/DataExtractor.h"
David Blaikie242b9482015-12-01 00:48:39 +000016#include "llvm/Support/FileSystem.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000017#include "llvm/Support/MathExtras.h"
David Blaikie242b9482015-12-01 00:48:39 +000018#include "llvm/Support/MemoryBuffer.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000019#include "llvm/Support/Options.h"
David Blaikie242b9482015-12-01 00:48:39 +000020#include "llvm/Support/TargetRegistry.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000021#include "llvm/Support/TargetSelect.h"
David Blaikie242b9482015-12-01 00:48:39 +000022#include "llvm/Support/raw_ostream.h"
23#include "llvm/Target/TargetMachine.h"
David Blaikie242b9482015-12-01 00:48:39 +000024#include <list>
David Blaikie2ed678c2015-12-05 03:06:30 +000025#include <memory>
David Blaikie242b9482015-12-01 00:48:39 +000026#include <unordered_set>
David Blaikie23919372016-02-06 01:15:26 +000027#include <system_error>
David Blaikie242b9482015-12-01 00:48:39 +000028
29using namespace llvm;
David Blaikie98ad82a2015-12-01 18:07:07 +000030using namespace llvm::object;
David Blaikie242b9482015-12-01 00:48:39 +000031using namespace cl;
32
33OptionCategory DwpCategory("Specific Options");
34static list<std::string> InputFiles(Positional, OneOrMore,
35 desc("<input files>"), cat(DwpCategory));
36
David Blaikie2ed678c2015-12-05 03:06:30 +000037static opt<std::string> OutputFilename(Required, "o",
38 desc("Specify the output file."),
39 value_desc("filename"),
40 cat(DwpCategory));
David Blaikie242b9482015-12-01 00:48:39 +000041
42static int error(const Twine &Error, const Twine &Context) {
43 errs() << Twine("while processing ") + Context + ":\n";
44 errs() << Twine("error: ") + Error + "\n";
45 return 1;
46}
47
David Blaikie98ad82a2015-12-01 18:07:07 +000048static std::error_code
49writeStringsAndOffsets(MCStreamer &Out, StringMap<uint32_t> &Strings,
David Blaikiebb94e442015-12-01 19:17:58 +000050 uint32_t &StringOffset, MCSection *StrSection,
51 MCSection *StrOffsetSection, StringRef CurStrSection,
52 StringRef CurStrOffsetSection) {
David Blaikie98ad82a2015-12-01 18:07:07 +000053 // Could possibly produce an error or warning if one of these was non-null but
54 // the other was null.
55 if (CurStrSection.empty() || CurStrOffsetSection.empty())
56 return std::error_code();
57
58 DenseMap<uint32_t, uint32_t> OffsetRemapping;
59
60 DataExtractor Data(CurStrSection, true, 0);
61 uint32_t LocalOffset = 0;
62 uint32_t PrevOffset = 0;
63 while (const char *s = Data.getCStr(&LocalOffset)) {
64 StringRef Str(s, LocalOffset - PrevOffset - 1);
David Blaikiebb94e442015-12-01 19:17:58 +000065 auto Pair = Strings.insert(std::make_pair(Str, StringOffset));
66 if (Pair.second) {
67 Out.SwitchSection(StrSection);
68 Out.EmitBytes(
69 StringRef(Pair.first->getKeyData(), Pair.first->getKeyLength() + 1));
70 StringOffset += Str.size() + 1;
71 }
72 OffsetRemapping[PrevOffset] = Pair.first->second;
David Blaikie98ad82a2015-12-01 18:07:07 +000073 PrevOffset = LocalOffset;
74 }
75
76 Data = DataExtractor(CurStrOffsetSection, true, 0);
77
78 Out.SwitchSection(StrOffsetSection);
79
80 uint32_t Offset = 0;
81 uint64_t Size = CurStrOffsetSection.size();
82 while (Offset < Size) {
83 auto OldOffset = Data.getU32(&Offset);
84 auto NewOffset = OffsetRemapping[OldOffset];
85 Out.EmitIntValue(NewOffset, 4);
86 }
87
David Blaikie242b9482015-12-01 00:48:39 +000088 return std::error_code();
89}
90
David Blaikiead07b5d2015-12-04 17:20:04 +000091static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
92 uint64_t CurCode;
93 uint32_t Offset = 0;
94 DataExtractor AbbrevData(Abbrev, true, 0);
95 while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) {
96 // Tag
97 AbbrevData.getULEB128(&Offset);
98 // DW_CHILDREN
99 AbbrevData.getU8(&Offset);
100 // Attributes
101 while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset))
102 ;
103 }
104 return Offset;
105}
106
107static uint64_t getCUSignature(StringRef Abbrev, StringRef Info) {
108 uint32_t Offset = 0;
109 DataExtractor InfoData(Info, true, 0);
110 InfoData.getU32(&Offset); // Length
111 uint16_t Version = InfoData.getU16(&Offset);
112 InfoData.getU32(&Offset); // Abbrev offset (should be zero)
113 uint8_t AddrSize = InfoData.getU8(&Offset);
114
115 uint32_t AbbrCode = InfoData.getULEB128(&Offset);
116
117 DataExtractor AbbrevData(Abbrev, true, 0);
118 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
119 uint64_t Tag = AbbrevData.getULEB128(&AbbrevOffset);
120 (void)Tag;
121 // FIXME: Real error handling
122 assert(Tag == dwarf::DW_TAG_compile_unit);
123 // DW_CHILDREN
124 AbbrevData.getU8(&AbbrevOffset);
125 uint32_t Name;
126 uint32_t Form;
127 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |
128 (Form = AbbrevData.getULEB128(&AbbrevOffset)) &&
129 Name != dwarf::DW_AT_GNU_dwo_id) {
130 DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize);
131 }
132 // FIXME: Real error handling
133 assert(Name == dwarf::DW_AT_GNU_dwo_id);
134 return InfoData.getU64(&Offset);
135}
136
David Blaikie24c8ac92015-12-05 03:05:45 +0000137struct UnitIndexEntry {
138 uint64_t Signature;
139 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
140};
141
David Blaikie8bce5a02016-02-17 07:00:24 +0000142static void addAllTypesFromDWP(MCStreamer &Out,
143 std::vector<UnitIndexEntry> &TypeIndexEntries,
144 const DWARFUnitIndex &TUIndex,
145 MCSection *OutputTypes, StringRef Types,
146 const UnitIndexEntry &TUEntry,
147 uint32_t &TypesOffset) {
148 Out.SwitchSection(OutputTypes);
149 for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) {
150 auto *I = E.getOffsets();
151 if (!I)
152 continue;
153 if (any_of(TypeIndexEntries, [&](const UnitIndexEntry &OldEntry) {
154 return OldEntry.Signature == E.getSignature();
155 }))
156 continue;
157 auto Entry = TUEntry;
158 Entry.Signature = E.getSignature();
159 // Zero out the debug_info contribution
160 Entry.Contributions[0] = {};
161 for (auto Kind : TUIndex.getColumnKinds()) {
162 auto &C = Entry.Contributions[Kind - DW_SECT_INFO];
163 C.Offset += I->Offset;
164 C.Length = I->Length;
165 ++I;
166 }
167 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
168 Out.EmitBytes(Types.substr(
169 C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset,
170 C.Length));
171 C.Offset = TypesOffset;
172 TypesOffset += C.Length;
173 TypeIndexEntries.push_back(Entry);
174 }
175}
176
David Blaikiec3826da2015-12-09 21:02:33 +0000177static void addAllTypes(MCStreamer &Out,
178 std::vector<UnitIndexEntry> &TypeIndexEntries,
179 MCSection *OutputTypes, StringRef Types,
180 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) {
181 if (Types.empty())
182 return;
183
184 Out.SwitchSection(OutputTypes);
David Blaikie24c8ac92015-12-05 03:05:45 +0000185 uint32_t Offset = 0;
186 DataExtractor Data(Types, true, 0);
187 while (Data.isValidOffset(Offset)) {
David Blaikief5cb6272015-12-14 07:42:00 +0000188 UnitIndexEntry Entry = CUEntry;
David Blaikie24c8ac92015-12-05 03:05:45 +0000189 // Zero out the debug_info contribution
190 Entry.Contributions[0] = {};
191 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
David Blaikief5cb6272015-12-14 07:42:00 +0000192 C.Offset = TypesOffset;
David Blaikie24c8ac92015-12-05 03:05:45 +0000193 auto PrevOffset = Offset;
194 // Length of the unit, including the 4 byte length field.
195 C.Length = Data.getU32(&Offset) + 4;
196
197 Data.getU16(&Offset); // Version
198 Data.getU32(&Offset); // Abbrev offset
199 Data.getU8(&Offset); // Address size
200 Entry.Signature = Data.getU64(&Offset);
201 Offset = PrevOffset + C.Length;
David Blaikief5cb6272015-12-14 07:42:00 +0000202
203 if (any_of(TypeIndexEntries, [&](const UnitIndexEntry &E) {
204 return E.Signature == Entry.Signature;
205 }))
206 continue;
207
208 Out.EmitBytes(Types.substr(PrevOffset, C.Length));
209 TypesOffset += C.Length;
210
211 TypeIndexEntries.push_back(Entry);
David Blaikie24c8ac92015-12-05 03:05:45 +0000212 }
213}
214
215static void
216writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,
217 ArrayRef<UnitIndexEntry> IndexEntries,
218 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) {
219 for (const auto &E : IndexEntries)
220 for (size_t i = 0; i != array_lengthof(E.Contributions); ++i)
221 if (ContributionOffsets[i])
222 Out.EmitIntValue(E.Contributions[i].*Field, 4);
223}
224
225static void writeIndex(MCStreamer &Out, MCSection *Section,
226 ArrayRef<unsigned> ContributionOffsets,
227 ArrayRef<UnitIndexEntry> IndexEntries) {
228 unsigned Columns = 0;
229 for (auto &C : ContributionOffsets)
230 if (C)
231 ++Columns;
232
233 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
234 uint64_t Mask = Buckets.size() - 1;
235 for (size_t i = 0; i != IndexEntries.size(); ++i) {
236 auto S = IndexEntries[i].Signature;
237 auto H = S & Mask;
David Blaikiec3826da2015-12-09 21:02:33 +0000238 while (Buckets[H]) {
239 assert(S != IndexEntries[Buckets[H] - 1].Signature &&
240 "Duplicate type unit");
Benjamin Kramere5930942016-02-18 13:23:17 +0000241 H = (H + (((S >> 32) & Mask) | 1)) % Buckets.size();
David Blaikiec3826da2015-12-09 21:02:33 +0000242 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000243 Buckets[H] = i + 1;
244 }
245
246 Out.SwitchSection(Section);
247 Out.EmitIntValue(2, 4); // Version
248 Out.EmitIntValue(Columns, 4); // Columns
249 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
David Blaikie2ed678c2015-12-05 03:06:30 +0000250 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets
David Blaikie24c8ac92015-12-05 03:05:45 +0000251
252 // Write the signatures.
253 for (const auto &I : Buckets)
254 Out.EmitIntValue(I ? IndexEntries[I - 1].Signature : 0, 8);
255
256 // Write the indexes.
257 for (const auto &I : Buckets)
258 Out.EmitIntValue(I, 4);
259
260 // Write the column headers (which sections will appear in the table)
261 for (size_t i = 0; i != ContributionOffsets.size(); ++i)
262 if (ContributionOffsets[i])
263 Out.EmitIntValue(i + DW_SECT_INFO, 4);
264
265 // Write the offsets.
266 writeIndexTable(Out, ContributionOffsets, IndexEntries,
267 &DWARFUnitIndex::Entry::SectionContribution::Offset);
268
269 // Write the lengths.
270 writeIndexTable(Out, ContributionOffsets, IndexEntries,
271 &DWARFUnitIndex::Entry::SectionContribution::Length);
272}
David Blaikie242b9482015-12-01 00:48:39 +0000273static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000274 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
275 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
276 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikiec3826da2015-12-09 21:02:33 +0000277 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
David Blaikie23919372016-02-06 01:15:26 +0000278 MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection();
David Blaikie8bce5a02016-02-17 07:00:24 +0000279 MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000280 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
281 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
282 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
283 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
284 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
285 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
David Blaikieb7020252015-12-04 21:16:42 +0000286 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikie23919372016-02-06 01:15:26 +0000287 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
David Blaikie8bce5a02016-02-17 07:00:24 +0000288 {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}},
289 {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}};
David Blaikieb073cb92015-12-02 06:21:34 +0000290
David Blaikieb073cb92015-12-02 06:21:34 +0000291 std::vector<UnitIndexEntry> IndexEntries;
David Blaikie24c8ac92015-12-05 03:05:45 +0000292 std::vector<UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000293
294 StringMap<uint32_t> Strings;
295 uint32_t StringOffset = 0;
296
David Blaikieb073cb92015-12-02 06:21:34 +0000297 uint32_t ContributionOffsets[8] = {};
298
David Blaikie242b9482015-12-01 00:48:39 +0000299 for (const auto &Input : Inputs) {
300 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
301 if (!ErrOrObj)
302 return ErrOrObj.getError();
David Blaikieb073cb92015-12-02 06:21:34 +0000303
David Blaikie23919372016-02-06 01:15:26 +0000304 UnitIndexEntry CurEntry = {};
David Blaikieb073cb92015-12-02 06:21:34 +0000305
David Blaikie98ad82a2015-12-01 18:07:07 +0000306 StringRef CurStrSection;
307 StringRef CurStrOffsetSection;
David Blaikiec3826da2015-12-09 21:02:33 +0000308 StringRef CurTypesSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000309 StringRef InfoSection;
310 StringRef AbbrevSection;
David Blaikie23919372016-02-06 01:15:26 +0000311 StringRef CurCUIndexSection;
David Blaikie8bce5a02016-02-17 07:00:24 +0000312 StringRef CurTUIndexSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000313
314 for (const auto &Section : ErrOrObj->getBinary()->sections()) {
David Blaikie242b9482015-12-01 00:48:39 +0000315 StringRef Name;
316 if (std::error_code Err = Section.getName(Name))
317 return Err;
David Blaikieb073cb92015-12-02 06:21:34 +0000318
319 auto SectionPair =
320 KnownSections.find(Name.substr(Name.find_first_not_of("._")));
321 if (SectionPair == KnownSections.end())
322 continue;
323
324 StringRef Contents;
325 if (auto Err = Section.getContents(Contents))
326 return Err;
327
328 if (DWARFSectionKind Kind = SectionPair->second.second) {
329 auto Index = Kind - DW_SECT_INFO;
David Blaikiec3826da2015-12-09 21:02:33 +0000330 if (Kind != DW_SECT_TYPES) {
331 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
332 ContributionOffsets[Index] +=
333 (CurEntry.Contributions[Index].Length = Contents.size());
334 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000335
David Blaikie24c8ac92015-12-05 03:05:45 +0000336 switch (Kind) {
337 case DW_SECT_INFO:
David Blaikiead07b5d2015-12-04 17:20:04 +0000338 InfoSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000339 break;
340 case DW_SECT_ABBREV:
David Blaikiead07b5d2015-12-04 17:20:04 +0000341 AbbrevSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000342 break;
David Blaikie24c8ac92015-12-05 03:05:45 +0000343 default:
344 break;
David Blaikiead07b5d2015-12-04 17:20:04 +0000345 }
David Blaikieb073cb92015-12-02 06:21:34 +0000346 }
347
348 MCSection *OutSection = SectionPair->second.first;
349 if (OutSection == StrOffsetSection)
350 CurStrOffsetSection = Contents;
351 else if (OutSection == StrSection)
352 CurStrSection = Contents;
David Blaikiec3826da2015-12-09 21:02:33 +0000353 else if (OutSection == TypesSection)
354 CurTypesSection = Contents;
David Blaikie23919372016-02-06 01:15:26 +0000355 else if (OutSection == CUIndexSection)
356 CurCUIndexSection = Contents;
David Blaikie8bce5a02016-02-17 07:00:24 +0000357 else if (OutSection == TUIndexSection)
358 CurTUIndexSection = Contents;
David Blaikieb073cb92015-12-02 06:21:34 +0000359 else {
360 Out.SwitchSection(OutSection);
361 Out.EmitBytes(Contents);
David Blaikie98ad82a2015-12-01 18:07:07 +0000362 }
David Blaikie242b9482015-12-01 00:48:39 +0000363 }
David Blaikieb073cb92015-12-02 06:21:34 +0000364
David Blaikiead07b5d2015-12-04 17:20:04 +0000365 assert(!AbbrevSection.empty());
366 assert(!InfoSection.empty());
David Blaikie23919372016-02-06 01:15:26 +0000367 if (!CurCUIndexSection.empty()) {
368 DWARFUnitIndex CUIndex(DW_SECT_INFO);
369 DataExtractor CUIndexData(CurCUIndexSection,
370 ErrOrObj->getBinary()->isLittleEndian(), 0);
371 if (!CUIndex.parse(CUIndexData))
372 return make_error_code(std::errc::invalid_argument);
373
374 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
375 auto NewEntry = CurEntry;
376 auto *I = E.getOffsets();
377 if (!I)
378 continue;
379 NewEntry.Signature = E.getSignature();
380 for (auto Kind : CUIndex.getColumnKinds()) {
381 auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO];
382 C.Offset += I->Offset;
383 C.Length = I->Length;
384 ++I;
385 }
386 IndexEntries.push_back(NewEntry);
387 }
David Blaikie8bce5a02016-02-17 07:00:24 +0000388
389 if (!CurTypesSection.empty()) {
390 if (CurTUIndexSection.empty())
391 return make_error_code(std::errc::invalid_argument);
392 DWARFUnitIndex TUIndex(DW_SECT_TYPES);
393 DataExtractor TUIndexData(CurTUIndexSection,
394 ErrOrObj->getBinary()->isLittleEndian(), 0);
395 if (!TUIndex.parse(TUIndexData))
396 return make_error_code(std::errc::invalid_argument);
397 addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection,
398 CurTypesSection, CurEntry,
399 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
400 }
David Blaikie23919372016-02-06 01:15:26 +0000401 } else {
402 CurEntry.Signature = getCUSignature(AbbrevSection, InfoSection);
403 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection,
404 CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
405
406 IndexEntries.push_back(CurEntry);
407 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000408
David Blaikiebb94e442015-12-01 19:17:58 +0000409 if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset,
410 StrSection, StrOffsetSection,
411 CurStrSection, CurStrOffsetSection))
David Blaikie98ad82a2015-12-01 18:07:07 +0000412 return Err;
David Blaikie242b9482015-12-01 00:48:39 +0000413 }
David Blaikieb073cb92015-12-02 06:21:34 +0000414
David Blaikie9e51c842015-12-05 03:41:53 +0000415 if (!TypeIndexEntries.empty()) {
416 // Lie about there being no info contributions so the TU index only includes
417 // the type unit contribution
418 ContributionOffsets[0] = 0;
419 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
420 TypeIndexEntries);
421 }
David Blaikieb3757c02015-12-02 22:01:56 +0000422
David Blaikie24c8ac92015-12-05 03:05:45 +0000423 // Lie about the type contribution
424 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
425 // Unlie about the info contribution
426 ContributionOffsets[0] = 1;
David Blaikie7c4ffe02015-12-04 21:30:23 +0000427
David Blaikie24c8ac92015-12-05 03:05:45 +0000428 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
429 IndexEntries);
David Blaikieb073cb92015-12-02 06:21:34 +0000430
David Blaikie242b9482015-12-01 00:48:39 +0000431 return std::error_code();
432}
433
David Blaikie2ed678c2015-12-05 03:06:30 +0000434int main(int argc, char **argv) {
David Blaikie242b9482015-12-01 00:48:39 +0000435
436 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
437
438 llvm::InitializeAllTargetInfos();
439 llvm::InitializeAllTargetMCs();
440 llvm::InitializeAllTargets();
441 llvm::InitializeAllAsmPrinters();
442
443 std::string ErrorStr;
444 StringRef Context = "dwarf streamer init";
445
446 Triple TheTriple("x86_64-linux-gnu");
447
448 // Get the target.
449 const Target *TheTarget =
450 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
451 if (!TheTarget)
452 return error(ErrorStr, Context);
453 std::string TripleName = TheTriple.getTriple();
454
455 // Create all the MC Objects.
456 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
457 if (!MRI)
458 return error(Twine("no register info for target ") + TripleName, Context);
459
460 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
461 if (!MAI)
462 return error("no asm info for target " + TripleName, Context);
463
464 MCObjectFileInfo MOFI;
465 MCContext MC(MAI.get(), MRI.get(), &MOFI);
David Blaikie2ed678c2015-12-05 03:06:30 +0000466 MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default, MC);
David Blaikie242b9482015-12-01 00:48:39 +0000467
468 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
469 if (!MAB)
470 return error("no asm backend for target " + TripleName, Context);
471
472 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
473 if (!MII)
474 return error("no instr info info for target " + TripleName, Context);
475
476 std::unique_ptr<MCSubtargetInfo> MSTI(
477 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
478 if (!MSTI)
479 return error("no subtarget info for target " + TripleName, Context);
480
481 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
482 if (!MCE)
483 return error("no code emitter for target " + TripleName, Context);
484
485 // Create the output file.
486 std::error_code EC;
487 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
488 if (EC)
489 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
490
David Majnemer03e2cc32015-12-21 22:09:27 +0000491 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
David Blaikie242b9482015-12-01 00:48:39 +0000492 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
David Majnemer03e2cc32015-12-21 22:09:27 +0000493 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, MCOptions.MCRelaxAll,
494 MCOptions.MCIncrementalLinkerCompatible,
David Blaikie242b9482015-12-01 00:48:39 +0000495 /*DWARFMustBeAtTheEnd*/ false));
496 if (!MS)
497 return error("no object streamer for target " + TripleName, Context);
498
499 if (auto Err = write(*MS, InputFiles))
500 return error(Err.message(), "Writing DWP file");
501
502 MS->Finish();
David Blaikiedf055252015-12-01 00:48:34 +0000503}