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