blob: bf0d29f4c2d079b2ba63df400af77f42bd64b846 [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 Blaikiec3826da2015-12-09 21:02:33 +0000142static void addAllTypes(MCStreamer &Out,
143 std::vector<UnitIndexEntry> &TypeIndexEntries,
144 MCSection *OutputTypes, StringRef Types,
145 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) {
146 if (Types.empty())
147 return;
148
149 Out.SwitchSection(OutputTypes);
David Blaikie24c8ac92015-12-05 03:05:45 +0000150 uint32_t Offset = 0;
151 DataExtractor Data(Types, true, 0);
152 while (Data.isValidOffset(Offset)) {
David Blaikief5cb6272015-12-14 07:42:00 +0000153 UnitIndexEntry Entry = CUEntry;
David Blaikie24c8ac92015-12-05 03:05:45 +0000154 // Zero out the debug_info contribution
155 Entry.Contributions[0] = {};
156 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
David Blaikief5cb6272015-12-14 07:42:00 +0000157 C.Offset = TypesOffset;
David Blaikie24c8ac92015-12-05 03:05:45 +0000158 auto PrevOffset = Offset;
159 // Length of the unit, including the 4 byte length field.
160 C.Length = Data.getU32(&Offset) + 4;
161
162 Data.getU16(&Offset); // Version
163 Data.getU32(&Offset); // Abbrev offset
164 Data.getU8(&Offset); // Address size
165 Entry.Signature = Data.getU64(&Offset);
166 Offset = PrevOffset + C.Length;
David Blaikief5cb6272015-12-14 07:42:00 +0000167
168 if (any_of(TypeIndexEntries, [&](const UnitIndexEntry &E) {
169 return E.Signature == Entry.Signature;
170 }))
171 continue;
172
173 Out.EmitBytes(Types.substr(PrevOffset, C.Length));
174 TypesOffset += C.Length;
175
176 TypeIndexEntries.push_back(Entry);
David Blaikie24c8ac92015-12-05 03:05:45 +0000177 }
178}
179
180static void
181writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,
182 ArrayRef<UnitIndexEntry> IndexEntries,
183 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) {
184 for (const auto &E : IndexEntries)
185 for (size_t i = 0; i != array_lengthof(E.Contributions); ++i)
186 if (ContributionOffsets[i])
187 Out.EmitIntValue(E.Contributions[i].*Field, 4);
188}
189
190static void writeIndex(MCStreamer &Out, MCSection *Section,
191 ArrayRef<unsigned> ContributionOffsets,
192 ArrayRef<UnitIndexEntry> IndexEntries) {
193 unsigned Columns = 0;
194 for (auto &C : ContributionOffsets)
195 if (C)
196 ++Columns;
197
198 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
199 uint64_t Mask = Buckets.size() - 1;
200 for (size_t i = 0; i != IndexEntries.size(); ++i) {
201 auto S = IndexEntries[i].Signature;
202 auto H = S & Mask;
David Blaikiec3826da2015-12-09 21:02:33 +0000203 while (Buckets[H]) {
204 assert(S != IndexEntries[Buckets[H] - 1].Signature &&
205 "Duplicate type unit");
David Blaikie24c8ac92015-12-05 03:05:45 +0000206 H += ((S >> 32) & Mask) | 1;
David Blaikiec3826da2015-12-09 21:02:33 +0000207 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000208 Buckets[H] = i + 1;
209 }
210
211 Out.SwitchSection(Section);
212 Out.EmitIntValue(2, 4); // Version
213 Out.EmitIntValue(Columns, 4); // Columns
214 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
David Blaikie2ed678c2015-12-05 03:06:30 +0000215 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets
David Blaikie24c8ac92015-12-05 03:05:45 +0000216
217 // Write the signatures.
218 for (const auto &I : Buckets)
219 Out.EmitIntValue(I ? IndexEntries[I - 1].Signature : 0, 8);
220
221 // Write the indexes.
222 for (const auto &I : Buckets)
223 Out.EmitIntValue(I, 4);
224
225 // Write the column headers (which sections will appear in the table)
226 for (size_t i = 0; i != ContributionOffsets.size(); ++i)
227 if (ContributionOffsets[i])
228 Out.EmitIntValue(i + DW_SECT_INFO, 4);
229
230 // Write the offsets.
231 writeIndexTable(Out, ContributionOffsets, IndexEntries,
232 &DWARFUnitIndex::Entry::SectionContribution::Offset);
233
234 // Write the lengths.
235 writeIndexTable(Out, ContributionOffsets, IndexEntries,
236 &DWARFUnitIndex::Entry::SectionContribution::Length);
237}
David Blaikie242b9482015-12-01 00:48:39 +0000238static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000239 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
240 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
241 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikiec3826da2015-12-09 21:02:33 +0000242 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
David Blaikie23919372016-02-06 01:15:26 +0000243 MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000244 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
245 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
246 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
247 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
248 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
249 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
David Blaikieb7020252015-12-04 21:16:42 +0000250 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikie23919372016-02-06 01:15:26 +0000251 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
252 {"debug_cu_index",
253 {MCOFI.getDwarfCUIndexSection(), static_cast<DWARFSectionKind>(0)}}};
David Blaikieb073cb92015-12-02 06:21:34 +0000254
David Blaikieb073cb92015-12-02 06:21:34 +0000255 std::vector<UnitIndexEntry> IndexEntries;
David Blaikie24c8ac92015-12-05 03:05:45 +0000256 std::vector<UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000257
258 StringMap<uint32_t> Strings;
259 uint32_t StringOffset = 0;
260
David Blaikieb073cb92015-12-02 06:21:34 +0000261 uint32_t ContributionOffsets[8] = {};
262
David Blaikie242b9482015-12-01 00:48:39 +0000263 for (const auto &Input : Inputs) {
264 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
265 if (!ErrOrObj)
266 return ErrOrObj.getError();
David Blaikieb073cb92015-12-02 06:21:34 +0000267
David Blaikie23919372016-02-06 01:15:26 +0000268 UnitIndexEntry CurEntry = {};
David Blaikieb073cb92015-12-02 06:21:34 +0000269
David Blaikie98ad82a2015-12-01 18:07:07 +0000270 StringRef CurStrSection;
271 StringRef CurStrOffsetSection;
David Blaikiec3826da2015-12-09 21:02:33 +0000272 StringRef CurTypesSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000273 StringRef InfoSection;
274 StringRef AbbrevSection;
David Blaikie23919372016-02-06 01:15:26 +0000275 StringRef CurCUIndexSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000276
277 for (const auto &Section : ErrOrObj->getBinary()->sections()) {
David Blaikie242b9482015-12-01 00:48:39 +0000278 StringRef Name;
279 if (std::error_code Err = Section.getName(Name))
280 return Err;
David Blaikieb073cb92015-12-02 06:21:34 +0000281
282 auto SectionPair =
283 KnownSections.find(Name.substr(Name.find_first_not_of("._")));
284 if (SectionPair == KnownSections.end())
285 continue;
286
287 StringRef Contents;
288 if (auto Err = Section.getContents(Contents))
289 return Err;
290
291 if (DWARFSectionKind Kind = SectionPair->second.second) {
292 auto Index = Kind - DW_SECT_INFO;
David Blaikiec3826da2015-12-09 21:02:33 +0000293 if (Kind != DW_SECT_TYPES) {
294 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
295 ContributionOffsets[Index] +=
296 (CurEntry.Contributions[Index].Length = Contents.size());
297 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000298
David Blaikie24c8ac92015-12-05 03:05:45 +0000299 switch (Kind) {
300 case DW_SECT_INFO:
David Blaikiead07b5d2015-12-04 17:20:04 +0000301 InfoSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000302 break;
303 case DW_SECT_ABBREV:
David Blaikiead07b5d2015-12-04 17:20:04 +0000304 AbbrevSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000305 break;
David Blaikie24c8ac92015-12-05 03:05:45 +0000306 default:
307 break;
David Blaikiead07b5d2015-12-04 17:20:04 +0000308 }
David Blaikieb073cb92015-12-02 06:21:34 +0000309 }
310
311 MCSection *OutSection = SectionPair->second.first;
312 if (OutSection == StrOffsetSection)
313 CurStrOffsetSection = Contents;
314 else if (OutSection == StrSection)
315 CurStrSection = Contents;
David Blaikiec3826da2015-12-09 21:02:33 +0000316 else if (OutSection == TypesSection)
317 CurTypesSection = Contents;
David Blaikie23919372016-02-06 01:15:26 +0000318 else if (OutSection == CUIndexSection)
319 CurCUIndexSection = Contents;
David Blaikieb073cb92015-12-02 06:21:34 +0000320 else {
321 Out.SwitchSection(OutSection);
322 Out.EmitBytes(Contents);
David Blaikie98ad82a2015-12-01 18:07:07 +0000323 }
David Blaikie242b9482015-12-01 00:48:39 +0000324 }
David Blaikieb073cb92015-12-02 06:21:34 +0000325
David Blaikiead07b5d2015-12-04 17:20:04 +0000326 assert(!AbbrevSection.empty());
327 assert(!InfoSection.empty());
David Blaikie23919372016-02-06 01:15:26 +0000328 if (!CurCUIndexSection.empty()) {
329 DWARFUnitIndex CUIndex(DW_SECT_INFO);
330 DataExtractor CUIndexData(CurCUIndexSection,
331 ErrOrObj->getBinary()->isLittleEndian(), 0);
332 if (!CUIndex.parse(CUIndexData))
333 return make_error_code(std::errc::invalid_argument);
334
335 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
336 auto NewEntry = CurEntry;
337 auto *I = E.getOffsets();
338 if (!I)
339 continue;
340 NewEntry.Signature = E.getSignature();
341 for (auto Kind : CUIndex.getColumnKinds()) {
342 auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO];
343 C.Offset += I->Offset;
344 C.Length = I->Length;
345 ++I;
346 }
347 IndexEntries.push_back(NewEntry);
348 }
349 } else {
350 CurEntry.Signature = getCUSignature(AbbrevSection, InfoSection);
351 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection,
352 CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
353
354 IndexEntries.push_back(CurEntry);
355 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000356
David Blaikiebb94e442015-12-01 19:17:58 +0000357 if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset,
358 StrSection, StrOffsetSection,
359 CurStrSection, CurStrOffsetSection))
David Blaikie98ad82a2015-12-01 18:07:07 +0000360 return Err;
David Blaikie242b9482015-12-01 00:48:39 +0000361 }
David Blaikieb073cb92015-12-02 06:21:34 +0000362
David Blaikie9e51c842015-12-05 03:41:53 +0000363 if (!TypeIndexEntries.empty()) {
364 // Lie about there being no info contributions so the TU index only includes
365 // the type unit contribution
366 ContributionOffsets[0] = 0;
367 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
368 TypeIndexEntries);
369 }
David Blaikieb3757c02015-12-02 22:01:56 +0000370
David Blaikie24c8ac92015-12-05 03:05:45 +0000371 // Lie about the type contribution
372 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
373 // Unlie about the info contribution
374 ContributionOffsets[0] = 1;
David Blaikie7c4ffe02015-12-04 21:30:23 +0000375
David Blaikie24c8ac92015-12-05 03:05:45 +0000376 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
377 IndexEntries);
David Blaikieb073cb92015-12-02 06:21:34 +0000378
David Blaikie242b9482015-12-01 00:48:39 +0000379 return std::error_code();
380}
381
David Blaikie2ed678c2015-12-05 03:06:30 +0000382int main(int argc, char **argv) {
David Blaikie242b9482015-12-01 00:48:39 +0000383
384 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
385
386 llvm::InitializeAllTargetInfos();
387 llvm::InitializeAllTargetMCs();
388 llvm::InitializeAllTargets();
389 llvm::InitializeAllAsmPrinters();
390
391 std::string ErrorStr;
392 StringRef Context = "dwarf streamer init";
393
394 Triple TheTriple("x86_64-linux-gnu");
395
396 // Get the target.
397 const Target *TheTarget =
398 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
399 if (!TheTarget)
400 return error(ErrorStr, Context);
401 std::string TripleName = TheTriple.getTriple();
402
403 // Create all the MC Objects.
404 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
405 if (!MRI)
406 return error(Twine("no register info for target ") + TripleName, Context);
407
408 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
409 if (!MAI)
410 return error("no asm info for target " + TripleName, Context);
411
412 MCObjectFileInfo MOFI;
413 MCContext MC(MAI.get(), MRI.get(), &MOFI);
David Blaikie2ed678c2015-12-05 03:06:30 +0000414 MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default, MC);
David Blaikie242b9482015-12-01 00:48:39 +0000415
416 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
417 if (!MAB)
418 return error("no asm backend for target " + TripleName, Context);
419
420 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
421 if (!MII)
422 return error("no instr info info for target " + TripleName, Context);
423
424 std::unique_ptr<MCSubtargetInfo> MSTI(
425 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
426 if (!MSTI)
427 return error("no subtarget info for target " + TripleName, Context);
428
429 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
430 if (!MCE)
431 return error("no code emitter for target " + TripleName, Context);
432
433 // Create the output file.
434 std::error_code EC;
435 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
436 if (EC)
437 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
438
David Majnemer03e2cc32015-12-21 22:09:27 +0000439 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
David Blaikie242b9482015-12-01 00:48:39 +0000440 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
David Majnemer03e2cc32015-12-21 22:09:27 +0000441 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, MCOptions.MCRelaxAll,
442 MCOptions.MCIncrementalLinkerCompatible,
David Blaikie242b9482015-12-01 00:48:39 +0000443 /*DWARFMustBeAtTheEnd*/ false));
444 if (!MS)
445 return error("no object streamer for target " + TripleName, Context);
446
447 if (auto Err = write(*MS, InputFiles))
448 return error(Err.message(), "Writing DWP file");
449
450 MS->Finish();
David Blaikiedf055252015-12-01 00:48:34 +0000451}