╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ zig_clang_cc1as_main.cpp ─────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
498 } 499 500 MCContext Ctx(Triple(Opts.Triple), MAI.get(), MRI.get(), STI.get(), &SrcMgr, 501 &MCOptions); 502 503 bool PIC = false; 504 if (Opts.RelocationModel == "static") { 505 PIC = false; 506 } else if (Opts.RelocationModel == "pic") { 507 PIC = true; 508 } else { 509 assert(Opts.RelocationModel == "dynamic-no-pic" && 510 "Invalid PIC model!"); 511 PIC = false; 512 } 513 514 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and 515 // MCObjectFileInfo needs a MCContext reference in order to initialize itself. 516 std::unique_ptr<MCObjectFileInfo> MOFI( 517 TheTarget->createMCObjectFileInfo(Ctx, PIC)); 518 Ctx.setObjectFileInfo(MOFI.get()); 519 520 if (Opts.GenDwarfForAssembly) 521 Ctx.setGenDwarfForAssembly(true); 522 if (!Opts.DwarfDebugFlags.empty()) 523 Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags)); 524 if (!Opts.DwarfDebugProducer.empty()) 525 Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer)); 526 if (!Opts.DebugCompilationDir.empty()) 527 Ctx.setCompilationDir(Opts.DebugCompilationDir); 528 else { 529 // If no compilation dir is set, try to use the current directory. 530 if (auto CWD = VFS->getCurrentWorkingDirectory()) 531 Ctx.setCompilationDir(*CWD); 532 } 533 if (!Opts.DebugPrefixMap.empty()) 534 for (const auto &KV : Opts.DebugPrefixMap) 535 Ctx.addDebugPrefixMapEntry(KV.first, KV.second); 536 if (!Opts.MainFileName.empty()) 537 Ctx.setMainFileName(StringRef(Opts.MainFileName)); 538 Ctx.setDwarfFormat(Opts.Dwarf64 ? dwarf::DWARF64 : dwarf::DWARF32); 539 Ctx.setDwarfVersion(Opts.DwarfVersion); 540 if (Opts.GenDwarfForAssembly) 541 Ctx.setGenDwarfRootFile(Opts.InputFile, 542 SrcMgr.getMemoryBuffer(BufferIndex)->getBuffer()); 543 544 std::unique_ptr<MCStreamer> Str; 545 546 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo()); 547 assert(MCII && "Unable to create instruction info!"); 548 549 raw_pwrite_stream *Out = FDOS.get(); 550 std::unique_ptr<buffer_ostream> BOS; 551 552 MCOptions.MCNoWarn = Opts.NoWarn; 553 MCOptions.MCFatalWarnings = Opts.FatalWarnings; 554 MCOptions.MCNoTypeCheck = Opts.NoTypeCheck; 555 MCOptions.ShowMCInst = Opts.ShowInst; 556 MCOptions.AsmVerbose = true; 557 MCOptions.MCUseDwarfDirectory = MCTargetOptions::EnableDwarfDirectory; 558 MCOptions.ABIName = Opts.TargetABI; 559 560 // FIXME: There is a bit of code duplication with addPassesToEmitFile. 561 if (Opts.OutputType == AssemblerInvocation::FT_Asm) { 562 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( 563 llvm::Triple(Opts.Triple), Opts.OutputAsmVariant, *MAI, *MCII, *MRI)); 564 565 std::unique_ptr<MCCodeEmitter> CE; 566 if (Opts.ShowEncoding) 567 CE.reset(TheTarget->createMCCodeEmitter(*MCII, Ctx)); 568 std::unique_ptr<MCAsmBackend> MAB( 569 TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions)); 570 571 auto FOut = std::make_unique<formatted_raw_ostream>(*Out); 572 Str.reset(TheTarget->createAsmStreamer(Ctx, std::move(FOut), std::move(IP), 573 std::move(CE), std::move(MAB))); 574 } else if (Opts.OutputType == AssemblerInvocation::FT_Null) { 575 Str.reset(createNullStreamer(Ctx)); 576 } else { 577 assert(Opts.OutputType == AssemblerInvocation::FT_Obj && 578 "Invalid file type!"); 579 if (!FDOS->supportsSeeking()) { 580 BOS = std::make_unique<buffer_ostream>(*FDOS); 581 Out = BOS.get(); 582 } 583 584 std::unique_ptr<MCCodeEmitter> CE( 585 TheTarget->createMCCodeEmitter(*MCII, Ctx)); 586 std::unique_ptr<MCAsmBackend> MAB( 587 TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions)); 588 assert(MAB && "Unable to create asm backend!"); 589 590 std::unique_ptr<MCObjectWriter> OW = 591 DwoOS ? MAB->createDwoObjectWriter(*Out, *DwoOS) 592 : MAB->createObjectWriter(*Out); 593 594 Triple T(Opts.Triple); 595 Str.reset(TheTarget->createMCObjectStreamer( 596 T, Ctx, std::move(MAB), std::move(OW), std::move(CE), *STI)); 597 if (T.isOSBinFormatMachO() && T.isOSDarwin()) { 598 Triple *TVT = Opts.DarwinTargetVariantTriple 599 ? &*Opts.DarwinTargetVariantTriple 600 : nullptr; 601 Str->emitVersionForTarget(T, VersionTuple(), TVT, 602 Opts.DarwinTargetVariantSDKVersion); 603 } 604 } 605 606 // When -fembed-bitcode is passed to clang_as, a 1-byte marker 607 // is emitted in __LLVM,__asm section if the object file is MachO format. 608 if (Opts.EmbedBitcode && Ctx.getObjectFileType() == MCContext::IsMachO) { 609 MCSection *AsmLabel = Ctx.getMachOSection( 610 "__LLVM", "__asm", MachO::S_REGULAR, 4, SectionKind::getReadOnly()); 611 Str->switchSection(AsmLabel); 612 Str->emitZeros(1); 613 } 614 615 bool Failed = false; 616 617 std::unique_ptr<MCAsmParser> Parser( 618 createMCAsmParser(SrcMgr, Ctx, *Str, *MAI)); 619 620 // FIXME: init MCTargetOptions from sanitizer flags here. 621 std::unique_ptr<MCTargetAsmParser> TAP( 622 TheTarget->createMCAsmParser(*STI, *Parser, *MCII, MCOptions)); 623 if (!TAP) 624 Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple.str(); 625 626 // Set values for symbols, if any. 627 for (auto &S : Opts.SymbolDefs) { 628 auto Pair = StringRef(S).split('='); 629 auto Sym = Pair.first; 630 auto Val = Pair.second; 631 int64_t Value; 632 // We have already error checked this in the driver. 633 Val.getAsInteger(0, Value); 634 Ctx.setSymbolValue(Parser->getStreamer(), Sym, Value); 635 } 636 637 if (!Failed) { 638 Parser->setTargetParser(*TAP); 639 Failed = Parser->Run(Opts.NoInitialTextSection); 640 } 641 642 return Failed; 643 } 644 645 static bool ExecuteAssembler(AssemblerInvocation &Opts, 646 DiagnosticsEngine &Diags, 647 IntrusiveRefCntPtr<vfs::FileSystem> VFS) { 648 bool Failed = ExecuteAssemblerImpl(Opts, Diags, VFS); 649 650 // Delete output file if there were errors. 651 if (Failed) { 652 if (Opts.OutputPath != "-") 653 sys::fs::remove(Opts.OutputPath); 654 if (!Opts.SplitDwarfOutput.empty() && Opts.SplitDwarfOutput != "-") 655 sys::fs::remove(Opts.SplitDwarfOutput); 656 } 657 658 return Failed; 659 } 660 661 static void LLVMErrorHandler(void *UserData, const char *Message, 662 bool GenCrashDiag) { 663 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData); 664 665 Diags.Report(diag::err_fe_error_backend) << Message; 666 667 // We cannot recover from llvm errors. 668 sys::Process::Exit(1); 669 } 670 671 int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) { 672 // Initialize targets and assembly printers/parsers. 673 InitializeAllTargetInfos(); 674 InitializeAllTargetMCs(); 675 InitializeAllAsmParsers(); 676 677 // Construct our diagnostic client. 678 DiagnosticOptions DiagOpts; 679 TextDiagnosticPrinter *DiagClient = 680 new TextDiagnosticPrinter(errs(), DiagOpts); 681 DiagClient->setPrefix("clang -cc1as"); 682 DiagnosticsEngine Diags(DiagnosticIDs::create(), DiagOpts, DiagClient); 683 684 auto VFS = [] { 685 auto BypassSandbox = sys::sandbox::scopedDisable(); 686 return vfs::getRealFileSystem(); 687 }(); 688 689 // Set an error handler, so that any LLVM backend diagnostics go through our 690 // error handler. 691 ScopedFatalErrorHandler FatalErrorHandler 692 (LLVMErrorHandler, static_cast<void*>(&Diags)); 693 694 // Parse the arguments. 695 AssemblerInvocation Asm; 696 if (!AssemblerInvocation::CreateFromArgs(Asm, Argv, Diags)) 697 return 1; 698 699 if (Asm.ShowHelp) { 700 getDriverOptTable().printHelp( 701 llvm::outs(), "clang -cc1as [options] file...", 702 "Clang Integrated Assembler", /*ShowHidden=*/false, 703 /*ShowAllAliases=*/false, llvm::opt::Visibility(options::CC1AsOption)); 704 705 return 0; 706 } 707 708 // Honor -version. 709 // 710 // FIXME: Use a better -version message? 711 if (Asm.ShowVersion) { 712 llvm::cl::PrintVersionMessage(); 713 return 0; 714 } 715 716 // Honor -mllvm. 717 // 718 // FIXME: Remove this, one day. 719 if (!Asm.LLVMArgs.empty()) { 720 unsigned NumArgs = Asm.LLVMArgs.size(); 721 auto Args = std::make_unique<const char*[]>(NumArgs + 2); 722 Args[0] = "clang (LLVM option parsing)"; 723 for (unsigned i = 0; i != NumArgs; ++i) 724 Args[i + 1] = Asm.LLVMArgs[i].c_str(); 725 Args[NumArgs + 1] = nullptr; 726 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get(), /*Overview=*/"", 727 /*Errs=*/nullptr, /*VFS=*/VFS.get()); 728 } 729 730 // Execute the invocation, unless there were parsing errors. 731 bool Failed = Diags.hasErrorOccurred() || ExecuteAssembler(Asm, Diags, VFS); 732 733 // If any timers were active but haven't been destroyed yet, print their 734 // results now. 735 TimerGroup::printAll(errs()); 736 TimerGroup::clearAll(); 737 738 return !!Failed; 739 } 740