1 //===-- cc1as_main.cpp - Clang Assembler ---------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This is the entry point to the clang -cc1as functionality, which implements 11 // the direct interface to the LLVM MC based assembler. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "clang/Basic/Diagnostic.h" 16 #include "clang/Basic/DiagnosticOptions.h" 17 #include "clang/Driver/DriverDiagnostic.h" 18 #include "clang/Driver/Options.h" 19 #include "clang/Frontend/FrontendDiagnostic.h" 20 #include "clang/Frontend/TextDiagnosticPrinter.h" 21 #include "clang/Frontend/Utils.h" 22 #include "llvm/ADT/StringSwitch.h" 23 #include "llvm/ADT/Triple.h" 24 #include "llvm/IR/DataLayout.h" 25 #include "llvm/MC/MCAsmBackend.h" 26 #include "llvm/MC/MCAsmInfo.h" 27 #include "llvm/MC/MCCodeEmitter.h" 28 #include "llvm/MC/MCContext.h" 29 #include "llvm/MC/MCInstrInfo.h" 30 #include "llvm/MC/MCObjectFileInfo.h" 31 #include "llvm/MC/MCParser/MCAsmParser.h" 32 #include "llvm/MC/MCRegisterInfo.h" 33 #include "llvm/MC/MCStreamer.h" 34 #include "llvm/MC/MCSubtargetInfo.h" 35 #include "llvm/MC/MCTargetAsmParser.h" 36 #include "llvm/MC/MCTargetOptions.h" 37 #include "llvm/Option/Arg.h" 38 #include "llvm/Option/ArgList.h" 39 #include "llvm/Option/OptTable.h" 40 #include "llvm/Support/CommandLine.h" 41 #include "llvm/Support/ErrorHandling.h" 42 #include "llvm/Support/FileSystem.h" 43 #include "llvm/Support/FormattedStream.h" 44 #include "llvm/Support/Host.h" 45 #include "llvm/Support/ManagedStatic.h" 46 #include "llvm/Support/MemoryBuffer.h" 47 #include "llvm/Support/Path.h" 48 #include "llvm/Support/PrettyStackTrace.h" 49 #include "llvm/Support/Signals.h" 50 #include "llvm/Support/SourceMgr.h" 51 #include "llvm/Support/TargetRegistry.h" 52 #include "llvm/Support/TargetSelect.h" 53 #include "llvm/Support/Timer.h" 54 #include "llvm/Support/raw_ostream.h" 55 #include <memory> 56 #include <system_error> 57 using namespace clang; 58 using namespace clang::driver; 59 using namespace clang::driver::options; 60 using namespace llvm; 61 using namespace llvm::opt; 62 63 namespace { 64 65 /// \brief Helper class for representing a single invocation of the assembler. 66 struct AssemblerInvocation { 67 /// @name Target Options 68 /// @{ 69 70 /// The name of the target triple to assemble for. 71 std::string Triple; 72 73 /// If given, the name of the target CPU to determine which instructions 74 /// are legal. 75 std::string CPU; 76 77 /// The list of target specific features to enable or disable -- this should 78 /// be a list of strings starting with '+' or '-'. 79 std::vector<std::string> Features; 80 81 /// @} 82 /// @name Language Options 83 /// @{ 84 85 std::vector<std::string> IncludePaths; 86 unsigned NoInitialTextSection : 1; 87 unsigned SaveTemporaryLabels : 1; 88 unsigned GenDwarfForAssembly : 1; 89 unsigned CompressDebugSections : 1; 90 unsigned DwarfVersion; 91 std::string DwarfDebugFlags; 92 std::string DwarfDebugProducer; 93 std::string DebugCompilationDir; 94 std::string MainFileName; 95 96 /// @} 97 /// @name Frontend Options 98 /// @{ 99 100 std::string InputFile; 101 std::vector<std::string> LLVMArgs; 102 std::string OutputPath; 103 enum FileType { 104 FT_Asm, ///< Assembly (.s) output, transliterate mode. 105 FT_Null, ///< No output, for timing purposes. 106 FT_Obj ///< Object file output. 107 }; 108 FileType OutputType; 109 unsigned ShowHelp : 1; 110 unsigned ShowVersion : 1; 111 112 /// @} 113 /// @name Transliterate Options 114 /// @{ 115 116 unsigned OutputAsmVariant; 117 unsigned ShowEncoding : 1; 118 unsigned ShowInst : 1; 119 120 /// @} 121 /// @name Assembler Options 122 /// @{ 123 124 unsigned RelaxAll : 1; 125 unsigned NoExecStack : 1; 126 unsigned FatalWarnings : 1; 127 128 /// @} 129 130 public: 131 AssemblerInvocation() { 132 Triple = ""; 133 NoInitialTextSection = 0; 134 InputFile = "-"; 135 OutputPath = "-"; 136 OutputType = FT_Asm; 137 OutputAsmVariant = 0; 138 ShowInst = 0; 139 ShowEncoding = 0; 140 RelaxAll = 0; 141 NoExecStack = 0; 142 FatalWarnings = 0; 143 DwarfVersion = 3; 144 } 145 146 static bool CreateFromArgs(AssemblerInvocation &Res, 147 ArrayRef<const char *> Argv, 148 DiagnosticsEngine &Diags); 149 }; 150 151 } 152 153 bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts, 154 ArrayRef<const char *> Argv, 155 DiagnosticsEngine &Diags) { 156 bool Success = true; 157 158 // Parse the arguments. 159 std::unique_ptr<OptTable> OptTbl(createDriverOptTable()); 160 161 const unsigned IncludedFlagsBitmask = options::CC1AsOption; 162 unsigned MissingArgIndex, MissingArgCount; 163 std::unique_ptr<InputArgList> Args( 164 OptTbl->ParseArgs(Argv.begin(), Argv.end(), MissingArgIndex, MissingArgCount, 165 IncludedFlagsBitmask)); 166 167 // Check for missing argument error. 168 if (MissingArgCount) { 169 Diags.Report(diag::err_drv_missing_argument) 170 << Args->getArgString(MissingArgIndex) << MissingArgCount; 171 Success = false; 172 } 173 174 // Issue errors on unknown arguments. 175 for (arg_iterator it = Args->filtered_begin(OPT_UNKNOWN), 176 ie = Args->filtered_end(); 177 it != ie; ++it) { 178 Diags.Report(diag::err_drv_unknown_argument) << (*it)->getAsString(*Args); 179 Success = false; 180 } 181 182 // Construct the invocation. 183 184 // Target Options 185 Opts.Triple = llvm::Triple::normalize(Args->getLastArgValue(OPT_triple)); 186 Opts.CPU = Args->getLastArgValue(OPT_target_cpu); 187 Opts.Features = Args->getAllArgValues(OPT_target_feature); 188 189 // Use the default target triple if unspecified. 190 if (Opts.Triple.empty()) 191 Opts.Triple = llvm::sys::getDefaultTargetTriple(); 192 193 // Language Options 194 Opts.IncludePaths = Args->getAllArgValues(OPT_I); 195 Opts.NoInitialTextSection = Args->hasArg(OPT_n); 196 Opts.SaveTemporaryLabels = Args->hasArg(OPT_msave_temp_labels); 197 Opts.GenDwarfForAssembly = Args->hasArg(OPT_g_Flag); 198 Opts.CompressDebugSections = Args->hasArg(OPT_compress_debug_sections); 199 if (Args->hasArg(OPT_gdwarf_2)) 200 Opts.DwarfVersion = 2; 201 if (Args->hasArg(OPT_gdwarf_3)) 202 Opts.DwarfVersion = 3; 203 if (Args->hasArg(OPT_gdwarf_4)) 204 Opts.DwarfVersion = 4; 205 Opts.DwarfDebugFlags = Args->getLastArgValue(OPT_dwarf_debug_flags); 206 Opts.DwarfDebugProducer = Args->getLastArgValue(OPT_dwarf_debug_producer); 207 Opts.DebugCompilationDir = Args->getLastArgValue(OPT_fdebug_compilation_dir); 208 Opts.MainFileName = Args->getLastArgValue(OPT_main_file_name); 209 210 // Frontend Options 211 if (Args->hasArg(OPT_INPUT)) { 212 bool First = true; 213 for (arg_iterator it = Args->filtered_begin(OPT_INPUT), 214 ie = Args->filtered_end(); it != ie; ++it, First=false) { 215 const Arg *A = it; 216 if (First) 217 Opts.InputFile = A->getValue(); 218 else { 219 Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(*Args); 220 Success = false; 221 } 222 } 223 } 224 Opts.LLVMArgs = Args->getAllArgValues(OPT_mllvm); 225 Opts.OutputPath = Args->getLastArgValue(OPT_o); 226 if (Arg *A = Args->getLastArg(OPT_filetype)) { 227 StringRef Name = A->getValue(); 228 unsigned OutputType = StringSwitch<unsigned>(Name) 229 .Case("asm", FT_Asm) 230 .Case("null", FT_Null) 231 .Case("obj", FT_Obj) 232 .Default(~0U); 233 if (OutputType == ~0U) { 234 Diags.Report(diag::err_drv_invalid_value) 235 << A->getAsString(*Args) << Name; 236 Success = false; 237 } else 238 Opts.OutputType = FileType(OutputType); 239 } 240 Opts.ShowHelp = Args->hasArg(OPT_help); 241 Opts.ShowVersion = Args->hasArg(OPT_version); 242 243 // Transliterate Options 244 Opts.OutputAsmVariant = 245 getLastArgIntValue(*Args.get(), OPT_output_asm_variant, 0, Diags); 246 Opts.ShowEncoding = Args->hasArg(OPT_show_encoding); 247 Opts.ShowInst = Args->hasArg(OPT_show_inst); 248 249 // Assemble Options 250 Opts.RelaxAll = Args->hasArg(OPT_mrelax_all); 251 Opts.NoExecStack = Args->hasArg(OPT_mno_exec_stack); 252 Opts.FatalWarnings = Args->hasArg(OPT_massembler_fatal_warnings); 253 254 return Success; 255 } 256 257 static formatted_raw_ostream *GetOutputStream(AssemblerInvocation &Opts, 258 DiagnosticsEngine &Diags, 259 bool Binary) { 260 if (Opts.OutputPath.empty()) 261 Opts.OutputPath = "-"; 262 263 // Make sure that the Out file gets unlinked from the disk if we get a 264 // SIGINT. 265 if (Opts.OutputPath != "-") 266 sys::RemoveFileOnSignal(Opts.OutputPath); 267 268 std::error_code EC; 269 raw_fd_ostream *Out = new raw_fd_ostream( 270 Opts.OutputPath, EC, (Binary ? sys::fs::F_None : sys::fs::F_Text)); 271 if (EC) { 272 Diags.Report(diag::err_fe_unable_to_open_output) << Opts.OutputPath 273 << EC.message(); 274 delete Out; 275 return nullptr; 276 } 277 278 return new formatted_raw_ostream(*Out, formatted_raw_ostream::DELETE_STREAM); 279 } 280 281 static bool ExecuteAssembler(AssemblerInvocation &Opts, 282 DiagnosticsEngine &Diags) { 283 // Get the target specific parser. 284 std::string Error; 285 const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error); 286 if (!TheTarget) 287 return Diags.Report(diag::err_target_unknown_triple) << Opts.Triple; 288 289 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = 290 MemoryBuffer::getFileOrSTDIN(Opts.InputFile); 291 292 if (std::error_code EC = Buffer.getError()) { 293 Error = EC.message(); 294 return Diags.Report(diag::err_fe_error_reading) << Opts.InputFile; 295 } 296 297 SourceMgr SrcMgr; 298 299 // Tell SrcMgr about this buffer, which is what the parser will pick up. 300 SrcMgr.AddNewSourceBuffer(std::move(*Buffer), SMLoc()); 301 302 // Record the location of the include directories so that the lexer can find 303 // it later. 304 SrcMgr.setIncludeDirs(Opts.IncludePaths); 305 306 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple)); 307 assert(MRI && "Unable to create target register info!"); 308 309 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, Opts.Triple)); 310 assert(MAI && "Unable to create target asm info!"); 311 312 // Ensure MCAsmInfo initialization occurs before any use, otherwise sections 313 // may be created with a combination of default and explicit settings. 314 if (Opts.CompressDebugSections) 315 MAI->setCompressDebugSections(true); 316 317 bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj; 318 std::unique_ptr<formatted_raw_ostream> Out( 319 GetOutputStream(Opts, Diags, IsBinary)); 320 if (!Out) 321 return true; 322 323 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and 324 // MCObjectFileInfo needs a MCContext reference in order to initialize itself. 325 std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo()); 326 327 MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr); 328 // FIXME: Assembler behavior can change with -static. 329 MOFI->InitMCObjectFileInfo(Opts.Triple, 330 Reloc::Default, CodeModel::Default, Ctx); 331 if (Opts.SaveTemporaryLabels) 332 Ctx.setAllowTemporaryLabels(false); 333 if (Opts.GenDwarfForAssembly) 334 Ctx.setGenDwarfForAssembly(true); 335 if (!Opts.DwarfDebugFlags.empty()) 336 Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags)); 337 if (!Opts.DwarfDebugProducer.empty()) 338 Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer)); 339 if (!Opts.DebugCompilationDir.empty()) 340 Ctx.setCompilationDir(Opts.DebugCompilationDir); 341 if (!Opts.MainFileName.empty()) 342 Ctx.setMainFileName(StringRef(Opts.MainFileName)); 343 Ctx.setDwarfVersion(Opts.DwarfVersion); 344 345 // Build up the feature string from the target feature list. 346 std::string FS; 347 if (!Opts.Features.empty()) { 348 FS = Opts.Features[0]; 349 for (unsigned i = 1, e = Opts.Features.size(); i != e; ++i) 350 FS += "," + Opts.Features[i]; 351 } 352 353 std::unique_ptr<MCStreamer> Str; 354 355 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo()); 356 std::unique_ptr<MCSubtargetInfo> STI( 357 TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS)); 358 359 // FIXME: There is a bit of code duplication with addPassesToEmitFile. 360 if (Opts.OutputType == AssemblerInvocation::FT_Asm) { 361 MCInstPrinter *IP = 362 TheTarget->createMCInstPrinter(Opts.OutputAsmVariant, *MAI, *MCII, *MRI, 363 *STI); 364 MCCodeEmitter *CE = nullptr; 365 MCAsmBackend *MAB = nullptr; 366 if (Opts.ShowEncoding) { 367 CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx); 368 MAB = TheTarget->createMCAsmBackend(*MRI, Opts.Triple, Opts.CPU); 369 } 370 Str.reset(TheTarget->createAsmStreamer(Ctx, *Out, /*asmverbose*/true, 371 /*useDwarfDirectory*/ true, 372 IP, CE, MAB, 373 Opts.ShowInst)); 374 } else if (Opts.OutputType == AssemblerInvocation::FT_Null) { 375 Str.reset(createNullStreamer(Ctx)); 376 } else { 377 assert(Opts.OutputType == AssemblerInvocation::FT_Obj && 378 "Invalid file type!"); 379 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx); 380 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*MRI, Opts.Triple, 381 Opts.CPU); 382 Str.reset(TheTarget->createMCObjectStreamer(Opts.Triple, Ctx, *MAB, *Out, 383 CE, *STI, Opts.RelaxAll)); 384 Str.get()->InitSections(Opts.NoExecStack); 385 } 386 387 bool Failed = false; 388 389 std::unique_ptr<MCAsmParser> Parser( 390 createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI)); 391 392 // FIXME: init MCTargetOptions from sanitizer flags here. 393 MCTargetOptions Options; 394 std::unique_ptr<MCTargetAsmParser> TAP( 395 TheTarget->createMCAsmParser(*STI, *Parser, *MCII, Options)); 396 if (!TAP) 397 Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple; 398 399 if (!Failed) { 400 Parser->setTargetParser(*TAP.get()); 401 Failed = Parser->Run(Opts.NoInitialTextSection); 402 } 403 404 // Close the output stream early. 405 Out.reset(); 406 407 // Delete output file if there were errors. 408 if (Failed && Opts.OutputPath != "-") 409 sys::fs::remove(Opts.OutputPath); 410 411 return Failed; 412 } 413 414 static void LLVMErrorHandler(void *UserData, const std::string &Message, 415 bool GenCrashDiag) { 416 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData); 417 418 Diags.Report(diag::err_fe_error_backend) << Message; 419 420 // We cannot recover from llvm errors. 421 exit(1); 422 } 423 424 int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) { 425 // Print a stack trace if we signal out. 426 sys::PrintStackTraceOnErrorSignal(); 427 PrettyStackTraceProgram X(Argv.size(), Argv.data()); 428 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 429 430 // Initialize targets and assembly printers/parsers. 431 InitializeAllTargetInfos(); 432 InitializeAllTargetMCs(); 433 InitializeAllAsmParsers(); 434 435 // Construct our diagnostic client. 436 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); 437 TextDiagnosticPrinter *DiagClient 438 = new TextDiagnosticPrinter(errs(), &*DiagOpts); 439 DiagClient->setPrefix("clang -cc1as"); 440 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 441 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient); 442 443 // Set an error handler, so that any LLVM backend diagnostics go through our 444 // error handler. 445 ScopedFatalErrorHandler FatalErrorHandler 446 (LLVMErrorHandler, static_cast<void*>(&Diags)); 447 448 // Parse the arguments. 449 AssemblerInvocation Asm; 450 if (!AssemblerInvocation::CreateFromArgs(Asm, Argv, Diags)) 451 return 1; 452 453 if (Asm.ShowHelp) { 454 std::unique_ptr<OptTable> Opts(driver::createDriverOptTable()); 455 Opts->PrintHelp(llvm::outs(), "clang -cc1as", "Clang Integrated Assembler", 456 /*Include=*/driver::options::CC1AsOption, /*Exclude=*/0); 457 return 0; 458 } 459 460 // Honor -version. 461 // 462 // FIXME: Use a better -version message? 463 if (Asm.ShowVersion) { 464 llvm::cl::PrintVersionMessage(); 465 return 0; 466 } 467 468 // Honor -mllvm. 469 // 470 // FIXME: Remove this, one day. 471 if (!Asm.LLVMArgs.empty()) { 472 unsigned NumArgs = Asm.LLVMArgs.size(); 473 const char **Args = new const char*[NumArgs + 2]; 474 Args[0] = "clang (LLVM option parsing)"; 475 for (unsigned i = 0; i != NumArgs; ++i) 476 Args[i + 1] = Asm.LLVMArgs[i].c_str(); 477 Args[NumArgs + 1] = nullptr; 478 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args); 479 } 480 481 // Execute the invocation, unless there were parsing errors. 482 bool Failed = Diags.hasErrorOccurred() || ExecuteAssembler(Asm, Diags); 483 484 // If any timers were active but haven't been destroyed yet, print their 485 // results now. 486 TimerGroup::printAll(errs()); 487 488 return !!Failed; 489 } 490 491