1 //===-- llvm-ml.cpp - masm-compatible assembler -----------------*- 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 // A simple driver around MasmParser; based on llvm-mc.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/MC/MCAsmBackend.h"
15 #include "llvm/MC/MCAsmInfo.h"
16 #include "llvm/MC/MCCodeEmitter.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCInstPrinter.h"
19 #include "llvm/MC/MCInstrInfo.h"
20 #include "llvm/MC/MCObjectFileInfo.h"
21 #include "llvm/MC/MCObjectWriter.h"
22 #include "llvm/MC/MCParser/AsmLexer.h"
23 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
24 #include "llvm/MC/MCRegisterInfo.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/MC/MCSubtargetInfo.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/MC/MCTargetOptionsCommandFlags.h"
29 #include "llvm/MC/TargetRegistry.h"
30 #include "llvm/Option/Arg.h"
31 #include "llvm/Option/ArgList.h"
32 #include "llvm/Option/Option.h"
33 #include "llvm/Support/Compression.h"
34 #include "llvm/Support/FileUtilities.h"
35 #include "llvm/Support/FormatVariadic.h"
36 #include "llvm/Support/FormattedStream.h"
37 #include "llvm/Support/Host.h"
38 #include "llvm/Support/InitLLVM.h"
39 #include "llvm/Support/MemoryBuffer.h"
40 #include "llvm/Support/Path.h"
41 #include "llvm/Support/Process.h"
42 #include "llvm/Support/SourceMgr.h"
43 #include "llvm/Support/TargetSelect.h"
44 #include "llvm/Support/ToolOutputFile.h"
45 #include "llvm/Support/WithColor.h"
46 #include <ctime>
47 
48 using namespace llvm;
49 using namespace llvm::opt;
50 
51 namespace {
52 
53 enum ID {
54   OPT_INVALID = 0, // This is not an option ID.
55 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
56                HELPTEXT, METAVAR, VALUES)                                      \
57   OPT_##ID,
58 #include "Opts.inc"
59 #undef OPTION
60 };
61 
62 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
63 #include "Opts.inc"
64 #undef PREFIX
65 
66 const opt::OptTable::Info InfoTable[] = {
67 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
68                HELPTEXT, METAVAR, VALUES)                                      \
69   {                                                                            \
70       PREFIX,      NAME,      HELPTEXT,                                        \
71       METAVAR,     OPT_##ID,  opt::Option::KIND##Class,                        \
72       PARAM,       FLAGS,     OPT_##GROUP,                                     \
73       OPT_##ALIAS, ALIASARGS, VALUES},
74 #include "Opts.inc"
75 #undef OPTION
76 };
77 
78 class MLOptTable : public opt::OptTable {
79 public:
MLOptTable()80   MLOptTable() : OptTable(InfoTable, /*IgnoreCase=*/false) {}
81 };
82 } // namespace
83 
GetTriple(StringRef ProgName,opt::InputArgList & Args)84 static Triple GetTriple(StringRef ProgName, opt::InputArgList &Args) {
85   // Figure out the target triple.
86   StringRef DefaultBitness = "32";
87   SmallString<255> Program = ProgName;
88   sys::path::replace_extension(Program, "");
89   if (Program.endswith("ml64"))
90     DefaultBitness = "64";
91 
92   StringRef TripleName =
93       StringSwitch<StringRef>(Args.getLastArgValue(OPT_bitness, DefaultBitness))
94           .Case("32", "i386-pc-windows")
95           .Case("64", "x86_64-pc-windows")
96           .Default("");
97   return Triple(Triple::normalize(TripleName));
98 }
99 
GetOutputStream(StringRef Path)100 static std::unique_ptr<ToolOutputFile> GetOutputStream(StringRef Path) {
101   std::error_code EC;
102   auto Out = std::make_unique<ToolOutputFile>(Path, EC, sys::fs::OF_None);
103   if (EC) {
104     WithColor::error() << EC.message() << '\n';
105     return nullptr;
106   }
107 
108   return Out;
109 }
110 
AsLexInput(SourceMgr & SrcMgr,MCAsmInfo & MAI,raw_ostream & OS)111 static int AsLexInput(SourceMgr &SrcMgr, MCAsmInfo &MAI, raw_ostream &OS) {
112   AsmLexer Lexer(MAI);
113   Lexer.setBuffer(SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer());
114   Lexer.setLexMasmIntegers(true);
115   Lexer.useMasmDefaultRadix(true);
116   Lexer.setLexMasmHexFloats(true);
117   Lexer.setLexMasmStrings(true);
118 
119   bool Error = false;
120   while (Lexer.Lex().isNot(AsmToken::Eof)) {
121     Lexer.getTok().dump(OS);
122     OS << "\n";
123     if (Lexer.getTok().getKind() == AsmToken::Error)
124       Error = true;
125   }
126 
127   return Error;
128 }
129 
AssembleInput(StringRef ProgName,const Target * TheTarget,SourceMgr & SrcMgr,MCContext & Ctx,MCStreamer & Str,MCAsmInfo & MAI,MCSubtargetInfo & STI,MCInstrInfo & MCII,MCTargetOptions & MCOptions,const opt::ArgList & InputArgs)130 static int AssembleInput(StringRef ProgName, const Target *TheTarget,
131                          SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str,
132                          MCAsmInfo &MAI, MCSubtargetInfo &STI,
133                          MCInstrInfo &MCII, MCTargetOptions &MCOptions,
134                          const opt::ArgList &InputArgs) {
135   struct tm TM;
136   time_t Timestamp;
137   if (InputArgs.hasArg(OPT_timestamp)) {
138     StringRef TimestampStr = InputArgs.getLastArgValue(OPT_timestamp);
139     int64_t IntTimestamp;
140     if (TimestampStr.getAsInteger(10, IntTimestamp)) {
141       WithColor::error(errs(), ProgName)
142           << "invalid timestamp '" << TimestampStr
143           << "'; must be expressed in seconds since the UNIX epoch.\n";
144       return 1;
145     }
146     Timestamp = IntTimestamp;
147   } else {
148     Timestamp = time(nullptr);
149   }
150   if (InputArgs.hasArg(OPT_utc)) {
151     // Not thread-safe.
152     TM = *gmtime(&Timestamp);
153   } else {
154     // Not thread-safe.
155     TM = *localtime(&Timestamp);
156   }
157 
158   std::unique_ptr<MCAsmParser> Parser(
159       createMCMasmParser(SrcMgr, Ctx, Str, MAI, TM, 0));
160   std::unique_ptr<MCTargetAsmParser> TAP(
161       TheTarget->createMCAsmParser(STI, *Parser, MCII, MCOptions));
162 
163   if (!TAP) {
164     WithColor::error(errs(), ProgName)
165         << "this target does not support assembly parsing.\n";
166     return 1;
167   }
168 
169   Parser->setShowParsedOperands(InputArgs.hasArg(OPT_show_inst_operands));
170   Parser->setTargetParser(*TAP);
171   Parser->getLexer().setLexMasmIntegers(true);
172   Parser->getLexer().useMasmDefaultRadix(true);
173   Parser->getLexer().setLexMasmHexFloats(true);
174   Parser->getLexer().setLexMasmStrings(true);
175 
176   auto Defines = InputArgs.getAllArgValues(OPT_define);
177   for (StringRef Define : Defines) {
178     const auto NameValue = Define.split('=');
179     StringRef Name = NameValue.first, Value = NameValue.second;
180     if (Parser->defineMacro(Name, Value)) {
181       WithColor::error(errs(), ProgName)
182           << "can't define macro '" << Name << "' = '" << Value << "'\n";
183       return 1;
184     }
185   }
186 
187   int Res = Parser->Run(/*NoInitialTextSection=*/true);
188 
189   return Res;
190 }
191 
main(int Argc,char ** Argv)192 int main(int Argc, char **Argv) {
193   InitLLVM X(Argc, Argv);
194   StringRef ProgName = sys::path::filename(Argv[0]);
195 
196   // Initialize targets and assembly printers/parsers.
197   llvm::InitializeAllTargetInfos();
198   llvm::InitializeAllTargetMCs();
199   llvm::InitializeAllAsmParsers();
200   llvm::InitializeAllDisassemblers();
201 
202   MLOptTable T;
203   unsigned MissingArgIndex, MissingArgCount;
204   ArrayRef<const char *> ArgsArr = makeArrayRef(Argv + 1, Argc - 1);
205   opt::InputArgList InputArgs =
206       T.ParseArgs(ArgsArr, MissingArgIndex, MissingArgCount);
207 
208   std::string InputFilename;
209   for (auto *Arg : InputArgs.filtered(OPT_INPUT)) {
210     std::string ArgString = Arg->getAsString(InputArgs);
211     bool IsFile = false;
212     std::error_code IsFileEC =
213         llvm::sys::fs::is_regular_file(ArgString, IsFile);
214     if (ArgString == "-" || IsFile) {
215       if (!InputFilename.empty()) {
216         WithColor::warning(errs(), ProgName)
217             << "does not support multiple assembly files in one command; "
218             << "ignoring '" << InputFilename << "'\n";
219       }
220       InputFilename = ArgString;
221     } else {
222       std::string Diag;
223       raw_string_ostream OS(Diag);
224       OS << ArgString << ": " << IsFileEC.message();
225 
226       std::string Nearest;
227       if (T.findNearest(ArgString, Nearest) < 2)
228         OS << ", did you mean '" << Nearest << "'?";
229 
230       WithColor::error(errs(), ProgName) << OS.str() << '\n';
231       exit(1);
232     }
233   }
234   for (auto *Arg : InputArgs.filtered(OPT_assembly_file)) {
235     if (!InputFilename.empty()) {
236       WithColor::warning(errs(), ProgName)
237           << "does not support multiple assembly files in one command; "
238           << "ignoring '" << InputFilename << "'\n";
239     }
240     InputFilename = Arg->getValue();
241   }
242 
243   for (auto *Arg : InputArgs.filtered(OPT_unsupported_Group)) {
244     WithColor::warning(errs(), ProgName)
245         << "ignoring unsupported '" << Arg->getOption().getName()
246         << "' option\n";
247   }
248 
249   if (InputArgs.hasArg(OPT_debug)) {
250     DebugFlag = true;
251   }
252   for (auto *Arg : InputArgs.filtered(OPT_debug_only)) {
253     setCurrentDebugTypes(Arg->getValues().data(), Arg->getNumValues());
254   }
255 
256   if (InputArgs.hasArg(OPT_help)) {
257     std::string Usage = llvm::formatv("{0} [ /options ] file", ProgName).str();
258     T.printHelp(outs(), Usage.c_str(), "LLVM MASM Assembler",
259                 /*ShowHidden=*/false);
260     return 0;
261   } else if (InputFilename.empty()) {
262     outs() << "USAGE: " << ProgName << " [ /options ] file\n"
263            << "Run \"" << ProgName << " /?\" or \"" << ProgName
264            << " /help\" for more info.\n";
265     return 0;
266   }
267 
268   MCTargetOptions MCOptions;
269   MCOptions.AssemblyLanguage = "masm";
270   MCOptions.MCFatalWarnings = InputArgs.hasArg(OPT_fatal_warnings);
271 
272   Triple TheTriple = GetTriple(ProgName, InputArgs);
273   std::string Error;
274   const Target *TheTarget = TargetRegistry::lookupTarget("", TheTriple, Error);
275   if (!TheTarget) {
276     WithColor::error(errs(), ProgName) << Error;
277     return 1;
278   }
279   const std::string &TripleName = TheTriple.getTriple();
280 
281   bool SafeSEH = InputArgs.hasArg(OPT_safeseh);
282   if (SafeSEH && !(TheTriple.isArch32Bit() && TheTriple.isX86())) {
283     WithColor::warning()
284         << "/safeseh applies only to 32-bit X86 platforms; ignoring.\n";
285     SafeSEH = false;
286   }
287 
288   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
289       MemoryBuffer::getFileOrSTDIN(InputFilename);
290   if (std::error_code EC = BufferPtr.getError()) {
291     WithColor::error(errs(), ProgName)
292         << InputFilename << ": " << EC.message() << '\n';
293     return 1;
294   }
295 
296   SourceMgr SrcMgr;
297 
298   // Tell SrcMgr about this buffer, which is what the parser will pick up.
299   SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc());
300 
301   // Record the location of the include directories so that the lexer can find
302   // included files later.
303   std::vector<std::string> IncludeDirs =
304       InputArgs.getAllArgValues(OPT_include_path);
305   if (!InputArgs.hasArg(OPT_ignore_include_envvar)) {
306     if (llvm::Optional<std::string> IncludeEnvVar =
307             llvm::sys::Process::GetEnv("INCLUDE")) {
308       SmallVector<StringRef, 8> Dirs;
309       StringRef(*IncludeEnvVar)
310           .split(Dirs, ";", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
311       IncludeDirs.reserve(IncludeDirs.size() + Dirs.size());
312       for (StringRef Dir : Dirs)
313         IncludeDirs.push_back(Dir.str());
314     }
315   }
316   SrcMgr.setIncludeDirs(IncludeDirs);
317 
318   std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
319   assert(MRI && "Unable to create target register info!");
320 
321   std::unique_ptr<MCAsmInfo> MAI(
322       TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
323   assert(MAI && "Unable to create target asm info!");
324 
325   MAI->setPreserveAsmComments(InputArgs.hasArg(OPT_preserve_comments));
326 
327   std::unique_ptr<MCSubtargetInfo> STI(TheTarget->createMCSubtargetInfo(
328       TripleName, /*CPU=*/"", /*Features=*/""));
329   assert(STI && "Unable to create subtarget info!");
330 
331   // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
332   // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
333   MCContext Ctx(TheTriple, MAI.get(), MRI.get(), STI.get(), &SrcMgr);
334   std::unique_ptr<MCObjectFileInfo> MOFI(TheTarget->createMCObjectFileInfo(
335       Ctx, /*PIC=*/false, /*LargeCodeModel=*/true));
336   Ctx.setObjectFileInfo(MOFI.get());
337 
338   if (InputArgs.hasArg(OPT_save_temp_labels))
339     Ctx.setAllowTemporaryLabels(false);
340 
341   // Set compilation information.
342   SmallString<128> CWD;
343   if (!sys::fs::current_path(CWD))
344     Ctx.setCompilationDir(CWD);
345   Ctx.setMainFileName(InputFilename);
346 
347   StringRef FileType = InputArgs.getLastArgValue(OPT_filetype, "obj");
348   SmallString<255> DefaultOutputFilename;
349   if (InputArgs.hasArg(OPT_as_lex)) {
350     DefaultOutputFilename = "-";
351   } else {
352     DefaultOutputFilename = InputFilename;
353     sys::path::replace_extension(DefaultOutputFilename, FileType);
354   }
355   const StringRef OutputFilename =
356       InputArgs.getLastArgValue(OPT_output_file, DefaultOutputFilename);
357   std::unique_ptr<ToolOutputFile> Out = GetOutputStream(OutputFilename);
358   if (!Out)
359     return 1;
360 
361   std::unique_ptr<buffer_ostream> BOS;
362   raw_pwrite_stream *OS = &Out->os();
363   std::unique_ptr<MCStreamer> Str;
364 
365   std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
366   assert(MCII && "Unable to create instruction info!");
367 
368   MCInstPrinter *IP = nullptr;
369   if (FileType == "s") {
370     const bool OutputATTAsm = InputArgs.hasArg(OPT_output_att_asm);
371     const unsigned OutputAsmVariant = OutputATTAsm ? 0U   // ATT dialect
372                                                    : 1U;  // Intel dialect
373     IP = TheTarget->createMCInstPrinter(TheTriple, OutputAsmVariant, *MAI,
374                                         *MCII, *MRI);
375 
376     if (!IP) {
377       WithColor::error()
378           << "unable to create instruction printer for target triple '"
379           << TheTriple.normalize() << "' with "
380           << (OutputATTAsm ? "ATT" : "Intel") << " assembly variant.\n";
381       return 1;
382     }
383 
384     // Set the display preference for hex vs. decimal immediates.
385     IP->setPrintImmHex(InputArgs.hasArg(OPT_print_imm_hex));
386 
387     // Set up the AsmStreamer.
388     std::unique_ptr<MCCodeEmitter> CE;
389     if (InputArgs.hasArg(OPT_show_encoding))
390       CE.reset(TheTarget->createMCCodeEmitter(*MCII, Ctx));
391 
392     std::unique_ptr<MCAsmBackend> MAB(
393         TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions));
394     auto FOut = std::make_unique<formatted_raw_ostream>(*OS);
395     Str.reset(TheTarget->createAsmStreamer(
396         Ctx, std::move(FOut), /*asmverbose*/ true,
397         /*useDwarfDirectory*/ true, IP, std::move(CE), std::move(MAB),
398         InputArgs.hasArg(OPT_show_inst)));
399 
400   } else if (FileType == "null") {
401     Str.reset(TheTarget->createNullStreamer(Ctx));
402   } else if (FileType == "obj") {
403     if (!Out->os().supportsSeeking()) {
404       BOS = std::make_unique<buffer_ostream>(Out->os());
405       OS = BOS.get();
406     }
407 
408     MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, Ctx);
409     MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions);
410     Str.reset(TheTarget->createMCObjectStreamer(
411         TheTriple, Ctx, std::unique_ptr<MCAsmBackend>(MAB),
412         MAB->createObjectWriter(*OS), std::unique_ptr<MCCodeEmitter>(CE), *STI,
413         MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible,
414         /*DWARFMustBeAtTheEnd*/ false));
415   } else {
416     llvm_unreachable("Invalid file type!");
417   }
418 
419   if (TheTriple.isOSBinFormatCOFF()) {
420     // Emit an absolute @feat.00 symbol. This is a features bitfield read by
421     // link.exe.
422     int64_t Feat00Flags = 0x2;
423     if (SafeSEH) {
424       // According to the PE-COFF spec, the LSB of this value marks the object
425       // for "registered SEH".  This means that all SEH handler entry points
426       // must be registered in .sxdata.  Use of any unregistered handlers will
427       // cause the process to terminate immediately.
428       Feat00Flags |= 0x1;
429     }
430     MCSymbol *Feat00Sym = Ctx.getOrCreateSymbol("@feat.00");
431     Feat00Sym->setRedefinable(true);
432     Str->emitSymbolAttribute(Feat00Sym, MCSA_Global);
433     Str->emitAssignment(Feat00Sym, MCConstantExpr::create(Feat00Flags, Ctx));
434   }
435 
436   // Use Assembler information for parsing.
437   Str->setUseAssemblerInfoForParsing(true);
438 
439   int Res = 1;
440   if (InputArgs.hasArg(OPT_as_lex)) {
441     // -as-lex; Lex only, and output a stream of tokens
442     Res = AsLexInput(SrcMgr, *MAI, Out->os());
443   } else {
444     Res = AssembleInput(ProgName, TheTarget, SrcMgr, Ctx, *Str, *MAI, *STI,
445                         *MCII, MCOptions, InputArgs);
446   }
447 
448   // Keep output if no errors.
449   if (Res == 0)
450     Out->keep();
451   return Res;
452 }
453