1//===-- llvm-mc.cpp - Machine Code Hacking Driver ---------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This utility is a simple driver that allows command line hacking on machine
10// code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Disassembler.h"
15#include "llvm/ADT/ScopeExit.h"
16#include "llvm/DWARFCFIChecker/DWARFCFIFunctionFrameAnalyzer.h"
17#include "llvm/DWARFCFIChecker/DWARFCFIFunctionFrameStreamer.h"
18#include "llvm/MC/MCAsmBackend.h"
19#include "llvm/MC/MCAsmInfo.h"
20#include "llvm/MC/MCCodeEmitter.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCInstPrinter.h"
23#include "llvm/MC/MCInstrInfo.h"
24#include "llvm/MC/MCLFI.h"
25#include "llvm/MC/MCObjectFileInfo.h"
26#include "llvm/MC/MCObjectWriter.h"
27#include "llvm/MC/MCParser/AsmLexer.h"
28#include "llvm/MC/MCParser/MCTargetAsmParser.h"
29#include "llvm/MC/MCRegisterInfo.h"
30#include "llvm/MC/MCStreamer.h"
31#include "llvm/MC/MCSubtargetInfo.h"
32#include "llvm/MC/MCTargetOptionsCommandFlags.h"
33#include "llvm/MC/TargetRegistry.h"
34#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/Compression.h"
36#include "llvm/Support/FileUtilities.h"
37#include "llvm/Support/FormattedStream.h"
38#include "llvm/Support/InitLLVM.h"
39#include "llvm/Support/MemoryBuffer.h"
40#include "llvm/Support/SourceMgr.h"
41#include "llvm/Support/TargetSelect.h"
42#include "llvm/Support/TimeProfiler.h"
43#include "llvm/Support/ToolOutputFile.h"
44#include "llvm/Support/VirtualFileSystem.h"
45#include "llvm/Support/WithColor.h"
46#include "llvm/TargetParser/Host.h"
47#include <memory>
48
49using namespace llvm;
50
51static mc::RegisterMCTargetOptionsFlags MOF;
52
53static cl::OptionCategory MCCategory("MC Options");
54
55static cl::opt<std::string> InputFilename(cl::Positional,
56 cl::desc("<input file>"),
57 cl::init(Val: "-"), cl::cat(MCCategory));
58
59static cl::list<std::string> InstPrinterOptions("M",
60 cl::desc("InstPrinter options"),
61 cl::cat(MCCategory));
62
63static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
64 cl::value_desc("filename"),
65 cl::init(Val: "-"), cl::cat(MCCategory));
66
67static cl::opt<std::string> SplitDwarfFile("split-dwarf-file",
68 cl::desc("DWO output filename"),
69 cl::value_desc("filename"),
70 cl::cat(MCCategory));
71
72static cl::opt<bool> ShowEncoding("show-encoding",
73 cl::desc("Show instruction encodings"),
74 cl::cat(MCCategory));
75
76static cl::opt<DebugCompressionType> CompressDebugSections(
77 "compress-debug-sections", cl::ValueOptional,
78 cl::init(Val: DebugCompressionType::None),
79 cl::desc("Choose DWARF debug sections compression:"),
80 cl::values(clEnumValN(DebugCompressionType::None, "none", "No compression"),
81 clEnumValN(DebugCompressionType::Zlib, "zlib", "Use zlib"),
82 clEnumValN(DebugCompressionType::Zstd, "zstd", "Use zstd")),
83 cl::cat(MCCategory));
84
85static cl::opt<bool>
86 ShowInst("show-inst", cl::desc("Show internal instruction representation"),
87 cl::cat(MCCategory));
88
89static cl::opt<bool>
90 ShowInstOperands("show-inst-operands",
91 cl::desc("Show instructions operands as parsed"),
92 cl::cat(MCCategory));
93
94static cl::opt<unsigned>
95 OutputAsmVariant("output-asm-variant",
96 cl::desc("Syntax variant to use for output printing"),
97 cl::cat(MCCategory));
98
99static cl::opt<bool>
100 PrintImmHex("print-imm-hex", cl::init(Val: false),
101 cl::desc("Prefer hex format for immediate values"),
102 cl::cat(MCCategory));
103
104static cl::opt<bool>
105 HexBytes("hex",
106 cl::desc("Take raw hexadecimal bytes as input for disassembly. "
107 "Whitespace is ignored"),
108 cl::cat(MCCategory));
109
110static cl::list<std::string>
111 DefineSymbol("defsym",
112 cl::desc("Defines a symbol to be an integer constant"),
113 cl::cat(MCCategory));
114
115static cl::opt<bool>
116 PreserveComments("preserve-comments",
117 cl::desc("Preserve Comments in outputted assembly"),
118 cl::cat(MCCategory));
119
120static cl::opt<unsigned> CommentColumn("comment-column",
121 cl::desc("Asm comments indentation"),
122 cl::init(Val: 40));
123
124enum OutputFileType {
125 OFT_Null,
126 OFT_AssemblyFile,
127 OFT_ObjectFile
128};
129static cl::opt<OutputFileType>
130 FileType("filetype", cl::init(Val: OFT_AssemblyFile),
131 cl::desc("Choose an output file type:"),
132 cl::values(clEnumValN(OFT_AssemblyFile, "asm",
133 "Emit an assembly ('.s') file"),
134 clEnumValN(OFT_Null, "null",
135 "Don't emit anything (for timing purposes)"),
136 clEnumValN(OFT_ObjectFile, "obj",
137 "Emit a native object ('.o') file")),
138 cl::cat(MCCategory));
139
140static cl::list<std::string> IncludeDirs("I",
141 cl::desc("Directory of include files"),
142 cl::value_desc("directory"),
143 cl::Prefix, cl::cat(MCCategory));
144
145static cl::opt<std::string>
146 ArchName("arch",
147 cl::desc("Target arch to assemble for, "
148 "see -version for available targets"),
149 cl::cat(MCCategory));
150
151static cl::opt<std::string>
152 TripleName("triple",
153 cl::desc("Target triple to assemble for, "
154 "see -version for available targets"),
155 cl::cat(MCCategory));
156
157static cl::opt<std::string>
158 MCPU("mcpu",
159 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
160 cl::value_desc("cpu-name"), cl::init(Val: ""), cl::cat(MCCategory));
161
162static cl::list<std::string>
163 MAttrs("mattr", cl::CommaSeparated,
164 cl::desc("Target specific attributes (-mattr=help for details)"),
165 cl::value_desc("a1,+a2,-a3,..."), cl::cat(MCCategory));
166
167static cl::opt<bool> PIC("position-independent",
168 cl::desc("Position independent"), cl::init(Val: false),
169 cl::cat(MCCategory));
170
171static cl::opt<bool>
172 LargeCodeModel("large-code-model",
173 cl::desc("Create cfi directives that assume the code might "
174 "be more than 2gb away"),
175 cl::cat(MCCategory));
176
177static cl::opt<bool>
178 NoInitialTextSection("n",
179 cl::desc("Don't assume assembly file starts "
180 "in the text section"),
181 cl::cat(MCCategory));
182
183static cl::opt<bool>
184 GenDwarfForAssembly("g",
185 cl::desc("Generate dwarf debugging info for assembly "
186 "source files"),
187 cl::cat(MCCategory));
188
189static cl::opt<std::string>
190 DebugCompilationDir("fdebug-compilation-dir",
191 cl::desc("Specifies the debug info's compilation dir"),
192 cl::cat(MCCategory));
193
194static cl::list<std::string> DebugPrefixMap(
195 "fdebug-prefix-map", cl::desc("Map file source paths in debug info"),
196 cl::value_desc("= separated key-value pairs"), cl::cat(MCCategory));
197
198static cl::opt<std::string> MainFileName(
199 "main-file-name",
200 cl::desc("Specifies the name we should consider the input file"),
201 cl::cat(MCCategory));
202
203static cl::opt<bool> LexMasmIntegers(
204 "masm-integers",
205 cl::desc("Enable binary and hex masm integers (0b110 and 0ABCh)"),
206 cl::cat(MCCategory));
207
208static cl::opt<bool> LexMasmHexFloats(
209 "masm-hexfloats",
210 cl::desc("Enable MASM-style hex float initializers (3F800000r)"),
211 cl::cat(MCCategory));
212
213static cl::opt<bool> LexMotorolaIntegers(
214 "motorola-integers",
215 cl::desc("Enable binary and hex Motorola integers (%110 and $ABC)"),
216 cl::cat(MCCategory));
217
218static cl::opt<bool> NoExecStack("no-exec-stack",
219 cl::desc("File doesn't need an exec stack"),
220 cl::cat(MCCategory));
221
222static cl::opt<bool> ValidateCFI("validate-cfi",
223 cl::desc("Validate the CFI directives"),
224 cl::cat(MCCategory));
225
226enum ActionType {
227 AC_AsLex,
228 AC_Assemble,
229 AC_Disassemble,
230 AC_MDisassemble,
231 AC_CDisassemble,
232};
233
234static cl::opt<ActionType> Action(
235 cl::desc("Action to perform:"), cl::init(Val: AC_Assemble),
236 cl::values(clEnumValN(AC_AsLex, "as-lex", "Lex tokens from a .s file"),
237 clEnumValN(AC_Assemble, "assemble",
238 "Assemble a .s file (default)"),
239 clEnumValN(AC_Disassemble, "disassemble",
240 "Disassemble strings of hex bytes"),
241 clEnumValN(AC_MDisassemble, "mdis",
242 "Marked up disassembly of strings of hex bytes"),
243 clEnumValN(AC_CDisassemble, "cdis",
244 "Colored disassembly of strings of hex bytes")),
245 cl::cat(MCCategory));
246
247static cl::opt<unsigned>
248 NumBenchmarkRuns("runs", cl::desc("Number of runs for benchmarking"),
249 cl::cat(MCCategory));
250
251static cl::opt<bool> TimeTrace("time-trace", cl::desc("Record time trace"));
252
253static cl::opt<unsigned> TimeTraceGranularity(
254 "time-trace-granularity",
255 cl::desc(
256 "Minimum time granularity (in microseconds) traced by time profiler"),
257 cl::init(Val: 500), cl::Hidden);
258
259static cl::opt<std::string>
260 TimeTraceFile("time-trace-file",
261 cl::desc("Specify time trace file destination"),
262 cl::value_desc("filename"));
263
264static const Target *GetTarget(const char *ProgName) {
265 // Figure out the target triple.
266 if (TripleName.empty())
267 TripleName = sys::getDefaultTargetTriple();
268 Triple TheTriple(Triple::normalize(Str: TripleName));
269
270 // Get the target specific parser.
271 std::string Error;
272 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
273 Error);
274 if (!TheTarget) {
275 WithColor::error(OS&: errs(), Prefix: ProgName) << Error;
276 return nullptr;
277 }
278
279 // Update the triple name and return the found target.
280 TripleName = TheTriple.getTriple();
281 return TheTarget;
282}
283
284static std::unique_ptr<ToolOutputFile> GetOutputStream(StringRef Path,
285 sys::fs::OpenFlags Flags) {
286 std::error_code EC;
287 auto Out = std::make_unique<ToolOutputFile>(args&: Path, args&: EC, args&: Flags);
288 if (EC) {
289 WithColor::error() << EC.message() << '\n';
290 return nullptr;
291 }
292
293 return Out;
294}
295
296static std::string DwarfDebugFlags;
297static void setDwarfDebugFlags(int argc, char **argv) {
298 if (!getenv(name: "RC_DEBUG_OPTIONS"))
299 return;
300 for (int i = 0; i < argc; i++) {
301 DwarfDebugFlags += argv[i];
302 if (i + 1 < argc)
303 DwarfDebugFlags += " ";
304 }
305}
306
307static std::string DwarfDebugProducer;
308static void setDwarfDebugProducer() {
309 if(!getenv(name: "DEBUG_PRODUCER"))
310 return;
311 DwarfDebugProducer += getenv(name: "DEBUG_PRODUCER");
312}
313
314static int AsLexInput(SourceMgr &SrcMgr, MCAsmInfo &MAI,
315 raw_ostream &OS) {
316
317 AsmLexer Lexer(MAI);
318 Lexer.setBuffer(Buf: SrcMgr.getMemoryBuffer(i: SrcMgr.getMainFileID())->getBuffer());
319
320 bool Error = false;
321 while (Lexer.Lex().isNot(K: AsmToken::Eof)) {
322 Lexer.getTok().dump(OS);
323 OS << "\n";
324 if (Lexer.getTok().getKind() == AsmToken::Error)
325 Error = true;
326 }
327
328 return Error;
329}
330
331static int fillCommandLineSymbols(MCAsmParser &Parser) {
332 for (auto &I: DefineSymbol) {
333 auto Pair = StringRef(I).split(Separator: '=');
334 auto Sym = Pair.first;
335 auto Val = Pair.second;
336
337 if (Sym.empty() || Val.empty()) {
338 WithColor::error() << "defsym must be of the form: sym=value: " << I
339 << "\n";
340 return 1;
341 }
342 int64_t Value;
343 if (Val.getAsInteger(Radix: 0, Result&: Value)) {
344 WithColor::error() << "value is not an integer: " << Val << "\n";
345 return 1;
346 }
347 Parser.getContext().setSymbolValue(Streamer&: Parser.getStreamer(), Sym, Val: Value);
348 }
349 return 0;
350}
351
352static int AssembleInput(const char *ProgName, const Target *TheTarget,
353 SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str,
354 MCAsmInfo &MAI, MCSubtargetInfo &STI,
355 MCInstrInfo &MCII, MCTargetOptions const &MCOptions) {
356 std::unique_ptr<MCAsmParser> Parser(
357 createMCAsmParser(SrcMgr, Ctx, Str, MAI));
358 std::unique_ptr<MCTargetAsmParser> TAP(
359 TheTarget->createMCAsmParser(STI, Parser&: *Parser, MII: MCII, Options: MCOptions));
360
361 if (!TAP) {
362 WithColor::error(OS&: errs(), Prefix: ProgName)
363 << "this target does not support assembly parsing.\n";
364 return 1;
365 }
366
367 int SymbolResult = fillCommandLineSymbols(Parser&: *Parser);
368 if(SymbolResult)
369 return SymbolResult;
370 Parser->setShowParsedOperands(ShowInstOperands);
371 Parser->setTargetParser(*TAP);
372 Parser->getLexer().setLexMasmIntegers(LexMasmIntegers);
373 Parser->getLexer().setLexMasmHexFloats(LexMasmHexFloats);
374 Parser->getLexer().setLexMotorolaIntegers(LexMotorolaIntegers);
375
376 int Res = Parser->Run(NoInitialTextSection);
377
378 return Res;
379}
380
381int main(int argc, char **argv) {
382 InitLLVM X(argc, argv);
383
384 // Initialize targets and assembly printers/parsers.
385 llvm::InitializeAllTargetInfos();
386 llvm::InitializeAllTargetMCs();
387 llvm::InitializeAllAsmParsers();
388 llvm::InitializeAllDisassemblers();
389
390 // Register the target printer for --version.
391 cl::AddExtraVersionPrinter(func: TargetRegistry::printRegisteredTargetsForVersion);
392
393 cl::HideUnrelatedOptions(Categories: {&MCCategory, &getColorCategory()});
394 cl::ParseCommandLineOptions(argc, argv, Overview: "llvm machine code playground\n");
395
396 if (TimeTrace)
397 timeTraceProfilerInitialize(TimeTraceGranularity, ProcName: argv[0]);
398
399 llvm::scope_exit TimeTraceScopeExit([]() {
400 if (!TimeTrace)
401 return;
402 if (auto E = timeTraceProfilerWrite(PreferredFileName: TimeTraceFile, FallbackFileName: OutputFilename)) {
403 logAllUnhandledErrors(E: std::move(E), OS&: errs());
404 return;
405 }
406 timeTraceProfilerCleanup();
407 });
408
409 MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags();
410 MCOptions.CompressDebugSections = CompressDebugSections.getValue();
411 MCOptions.ShowMCInst = ShowInst;
412 MCOptions.AsmVerbose = true;
413 MCOptions.MCUseDwarfDirectory = MCTargetOptions::EnableDwarfDirectory;
414 MCOptions.InstPrinterOptions = InstPrinterOptions;
415
416 setDwarfDebugFlags(argc, argv);
417 setDwarfDebugProducer();
418
419 const char *ProgName = argv[0];
420 const Target *TheTarget = GetTarget(ProgName);
421 if (!TheTarget)
422 return 1;
423 // Now that GetTarget() has (potentially) replaced TripleName, it's safe to
424 // construct the Triple object.
425 Triple TheTriple(TripleName);
426
427 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
428 MemoryBuffer::getFileOrSTDIN(Filename: InputFilename, /*IsText=*/true);
429 if (std::error_code EC = BufferPtr.getError()) {
430 WithColor::error(OS&: errs(), Prefix: ProgName)
431 << InputFilename << ": " << EC.message() << '\n';
432 return 1;
433 }
434 MemoryBuffer *Buffer = BufferPtr->get();
435
436 SourceMgr SrcMgr;
437
438 // Tell SrcMgr about this buffer, which is what the parser will pick up.
439 SrcMgr.AddNewSourceBuffer(F: std::move(*BufferPtr), IncludeLoc: SMLoc());
440
441 // Record the location of the include directories so that the lexer can find
442 // it later.
443 SrcMgr.setIncludeDirs(IncludeDirs);
444 SrcMgr.setVirtualFileSystem(vfs::getRealFileSystem());
445
446 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT: TheTriple));
447 assert(MRI && "Unable to create target register info!");
448
449 std::unique_ptr<MCAsmInfo> MAI(
450 TheTarget->createMCAsmInfo(MRI: *MRI, TheTriple, Options: MCOptions));
451 assert(MAI && "Unable to create target asm info!");
452
453 if (CompressDebugSections != DebugCompressionType::None) {
454 if (const char *Reason = compression::getReasonIfUnsupported(
455 F: compression::formatFor(Type: CompressDebugSections))) {
456 WithColor::error(OS&: errs(), Prefix: ProgName)
457 << "--compress-debug-sections: " << Reason;
458 return 1;
459 }
460 }
461 MAI->setPreserveAsmComments(PreserveComments);
462 MAI->setCommentColumn(CommentColumn);
463
464 // Package up features to be passed to target/subtarget
465 SubtargetFeatures Features;
466 std::string FeaturesStr;
467
468 // Replace -mcpu=native with Host CPU and features.
469 if (MCPU == "native") {
470 MCPU = std::string(llvm::sys::getHostCPUName());
471
472 llvm::StringMap<bool> TargetFeatures = llvm::sys::getHostCPUFeatures();
473 for (auto const &[FeatureName, IsSupported] : TargetFeatures)
474 Features.AddFeature(String: FeatureName, Enable: IsSupported);
475 }
476
477 // Handle features passed to target/subtarget.
478 for (unsigned i = 0; i != MAttrs.size(); ++i)
479 Features.AddFeature(String: MAttrs[i]);
480 FeaturesStr = Features.getString();
481
482 std::unique_ptr<MCSubtargetInfo> STI(
483 TheTarget->createMCSubtargetInfo(TheTriple, CPU: MCPU, Features: FeaturesStr));
484 if (!STI) {
485 WithColor::error(OS&: errs(), Prefix: ProgName) << "unable to create subtarget info\n";
486 return 1;
487 }
488
489 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
490 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
491 MCContext Ctx(TheTriple, MAI.get(), MRI.get(), STI.get(), &SrcMgr,
492 &MCOptions);
493 std::unique_ptr<MCObjectFileInfo> MOFI(
494 TheTarget->createMCObjectFileInfo(Ctx, PIC, LargeCodeModel));
495 Ctx.setObjectFileInfo(MOFI.get());
496
497 Ctx.setGenDwarfForAssembly(GenDwarfForAssembly);
498 // Default to 4 for dwarf version.
499 unsigned DwarfVersion = MCOptions.DwarfVersion ? MCOptions.DwarfVersion : 4;
500 if (DwarfVersion < 2 || DwarfVersion > 5) {
501 errs() << ProgName << ": Dwarf version " << DwarfVersion
502 << " is not supported." << '\n';
503 return 1;
504 }
505 Ctx.setDwarfVersion(DwarfVersion);
506 if (MCOptions.Dwarf64) {
507 // The 64-bit DWARF format was introduced in DWARFv3.
508 if (DwarfVersion < 3) {
509 errs() << ProgName
510 << ": the 64-bit DWARF format is not supported for DWARF versions "
511 "prior to 3\n";
512 return 1;
513 }
514 // 32-bit targets don't support DWARF64, which requires 64-bit relocations.
515 if (MAI->getCodePointerSize() < 8) {
516 errs() << ProgName
517 << ": the 64-bit DWARF format is only supported for 64-bit "
518 "targets\n";
519 return 1;
520 }
521 // If needsDwarfSectionOffsetDirective is true, we would eventually call
522 // MCStreamer::emitSymbolValue() with IsSectionRelative = true, but that
523 // is supported only for 4-byte long references.
524 if (MAI->needsDwarfSectionOffsetDirective()) {
525 errs() << ProgName << ": the 64-bit DWARF format is not supported for "
526 << TheTriple.normalize() << "\n";
527 return 1;
528 }
529 Ctx.setDwarfFormat(dwarf::DWARF64);
530 }
531 if (!DwarfDebugFlags.empty())
532 Ctx.setDwarfDebugFlags(StringRef(DwarfDebugFlags));
533 if (!DwarfDebugProducer.empty())
534 Ctx.setDwarfDebugProducer(StringRef(DwarfDebugProducer));
535 if (!DebugCompilationDir.empty())
536 Ctx.setCompilationDir(DebugCompilationDir);
537 else {
538 // If no compilation dir is set, try to use the current directory.
539 SmallString<128> CWD;
540 if (!sys::fs::current_path(result&: CWD))
541 Ctx.setCompilationDir(CWD);
542 }
543 for (const auto &Arg : DebugPrefixMap) {
544 const auto &KV = StringRef(Arg).split(Separator: '=');
545 Ctx.addDebugPrefixMapEntry(From: std::string(KV.first), To: std::string(KV.second));
546 }
547 if (!MainFileName.empty())
548 Ctx.setMainFileName(MainFileName);
549 if (GenDwarfForAssembly)
550 Ctx.setGenDwarfRootFile(FileName: InputFilename, Buffer: Buffer->getBuffer());
551
552 sys::fs::OpenFlags Flags = (FileType == OFT_AssemblyFile)
553 ? sys::fs::OF_TextWithCRLF
554 : sys::fs::OF_None;
555 std::unique_ptr<ToolOutputFile> Out = GetOutputStream(Path: OutputFilename, Flags);
556 if (!Out)
557 return 1;
558
559 std::unique_ptr<ToolOutputFile> DwoOut;
560 if (!SplitDwarfFile.empty()) {
561 if (FileType != OFT_ObjectFile) {
562 WithColor::error() << "dwo output only supported with object files\n";
563 return 1;
564 }
565 DwoOut = GetOutputStream(Path: SplitDwarfFile, Flags: sys::fs::OF_None);
566 if (!DwoOut)
567 return 1;
568 }
569
570 std::unique_ptr<buffer_ostream> BOS;
571 raw_pwrite_stream *OS = &Out->os();
572 std::unique_ptr<MCStreamer> Str;
573
574 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
575 assert(MCII && "Unable to create instruction info!");
576
577 std::unique_ptr<MCInstPrinter> IP;
578 if (ValidateCFI) {
579 // TODO: The DWARF CFI checker support for emitting anything other than
580 // errors and warnings has not been implemented yet. Because of this, it is
581 // assert-checked that the filetype output is null.
582 assert(FileType == OFT_Null);
583 auto FFA = std::make_unique<CFIFunctionFrameAnalyzer>(args&: Ctx, args&: *MCII);
584 auto FFS = std::make_unique<CFIFunctionFrameStreamer>(args&: Ctx, args: std::move(FFA));
585 TheTarget->createNullTargetStreamer(S&: *FFS);
586 Str = std::move(FFS);
587 } else if (FileType == OFT_AssemblyFile) {
588 IP.reset(p: TheTarget->createMCInstPrinter(
589 T: Triple(TripleName), SyntaxVariant: OutputAsmVariant, MAI: *MAI, MII: *MCII, MRI: *MRI));
590
591 if (!IP) {
592 WithColor::error()
593 << "unable to create instruction printer for target triple '"
594 << TheTriple.normalize() << "' with assembly variant "
595 << OutputAsmVariant << ".\n";
596 return 1;
597 }
598
599 for (StringRef Opt : InstPrinterOptions)
600 if (!IP->applyTargetSpecificCLOption(Opt)) {
601 WithColor::error() << "invalid InstPrinter option '" << Opt << "'\n";
602 return 1;
603 }
604
605 // Set the display preference for hex vs. decimal immediates.
606 IP->setPrintImmHex(PrintImmHex);
607
608 switch (Action) {
609 case AC_MDisassemble:
610 IP->setUseMarkup(true);
611 break;
612 case AC_CDisassemble:
613 IP->setUseColor(true);
614 break;
615 default:
616 break;
617 }
618
619 // Set up the AsmStreamer.
620 std::unique_ptr<MCCodeEmitter> CE;
621 if (ShowEncoding)
622 CE.reset(p: TheTarget->createMCCodeEmitter(II: *MCII, Ctx));
623
624 std::unique_ptr<MCAsmBackend> MAB(
625 TheTarget->createMCAsmBackend(STI: *STI, MRI: *MRI, Options: MCOptions));
626 auto FOut = std::make_unique<formatted_raw_ostream>(args&: *OS);
627 Str.reset(p: TheTarget->createAsmStreamer(Ctx, OS: std::move(FOut), IP: std::move(IP),
628 CE: std::move(CE), TAB: std::move(MAB)));
629
630 Triple T(TripleName);
631 if (T.isLFI()) {
632 Str->initSections(NoExecStack, STI: *STI);
633 initializeLFIMCStreamer(Streamer&: *Str.get(), Ctx, TheTriple: T);
634 }
635 } else if (FileType == OFT_Null) {
636 Str.reset(p: TheTarget->createNullStreamer(Ctx));
637 } else {
638 assert(FileType == OFT_ObjectFile && "Invalid file type!");
639
640 if (!Out->os().supportsSeeking()) {
641 BOS = std::make_unique<buffer_ostream>(args&: Out->os());
642 OS = BOS.get();
643 }
644
645 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(II: *MCII, Ctx);
646 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(STI: *STI, MRI: *MRI, Options: MCOptions);
647 Str.reset(p: TheTarget->createMCObjectStreamer(
648 T: TheTriple, Ctx, TAB: std::unique_ptr<MCAsmBackend>(MAB),
649 OW: DwoOut ? MAB->createDwoObjectWriter(OS&: *OS, DwoOS&: DwoOut->os())
650 : MAB->createObjectWriter(OS&: *OS),
651 Emitter: std::unique_ptr<MCCodeEmitter>(CE), STI: *STI));
652 if (NoExecStack)
653 Str->switchSection(
654 Section: Ctx.getAsmInfo()->getStackSection(Ctx, /*Exec=*/false));
655 Str->emitVersionForTarget(Target: TheTriple, SDKVersion: VersionTuple(), DarwinTargetVariantTriple: nullptr,
656 DarwinTargetVariantSDKVersion: VersionTuple());
657 }
658
659 int Res = 1;
660 bool disassemble = false;
661 switch (Action) {
662 case AC_AsLex:
663 Res = AsLexInput(SrcMgr, MAI&: *MAI, OS&: Out->os());
664 break;
665 case AC_Assemble:
666 Res = AssembleInput(ProgName, TheTarget, SrcMgr, Ctx, Str&: *Str, MAI&: *MAI, STI&: *STI,
667 MCII&: *MCII, MCOptions);
668 break;
669 case AC_MDisassemble:
670 case AC_CDisassemble:
671 case AC_Disassemble:
672 disassemble = true;
673 break;
674 }
675 if (disassemble)
676 Res = Disassembler::disassemble(T: *TheTarget, STI&: *STI, Streamer&: *Str, Buffer&: *Buffer, SM&: SrcMgr,
677 Ctx, MCOptions, HexBytes, NumBenchmarkRuns);
678
679 // Keep output if no errors.
680 if (Res == 0) {
681 Out->keep();
682 if (DwoOut)
683 DwoOut->keep();
684 }
685
686 return Res;
687}
688