diff --git a/src/zig_clang_cc1_main.cpp b/src/zig_clang_cc1_main.cpp index 1adb2170149736a3920084ead85d79453f2f93a6..89b0a340e6672ee56919f4fe073060919e43350f 100644 --- a/src/zig_clang_cc1_main.cpp +++ b/src/zig_clang_cc1_main.cpp @@ -144,13 +144,13 @@ static int PrintSupportedExtensions(std::string TargetStr) { std::unique_ptr TheTargetMachine( TheTarget->createTargetMachine(Triple, "", "", Options, std::nullopt)); const llvm::Triple &MachineTriple = TheTargetMachine->getTargetTriple(); - const llvm::MCSubtargetInfo *MCInfo = TheTargetMachine->getMCSubtargetInfo(); + const llvm::MCSubtargetInfo &MCInfo = TheTargetMachine->getMCSubtargetInfo(); const llvm::ArrayRef Features = - MCInfo->getAllProcessorFeatures(); + MCInfo.getAllProcessorFeatures(); llvm::StringMap DescMap; for (const llvm::SubtargetFeatureKV &feature : Features) - DescMap.insert({feature.Key, feature.Desc}); + DescMap.insert({feature.key(), feature.desc()}); if (MachineTriple.isRISCV()) llvm::RISCVISAInfo::printSupportedExtensions(DescMap); @@ -187,23 +187,23 @@ static int PrintEnabledExtensions(const TargetOptions& TargetOpts) { TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr, BackendOptions, std::nullopt)); const llvm::Triple &MachineTriple = TheTargetMachine->getTargetTriple(); - const llvm::MCSubtargetInfo *MCInfo = TheTargetMachine->getMCSubtargetInfo(); + const llvm::MCSubtargetInfo &MCInfo = TheTargetMachine->getMCSubtargetInfo(); // Extract the feature names that are enabled for the given target. // We do that by capturing the key from the set of SubtargetFeatureKV entries // provided by MCSubtargetInfo, which match the '-target-feature' values. - const std::vector Features = - MCInfo->getEnabledProcessorFeatures(); + const std::vector Features = + MCInfo.getEnabledProcessorFeatures(); std::set EnabledFeatureNames; - for (const llvm::SubtargetFeatureKV &feature : Features) - EnabledFeatureNames.insert(feature.Key); + for (const llvm::SubtargetFeatureKV *feature : Features) + EnabledFeatureNames.insert(feature->key()); if (MachineTriple.isAArch64()) llvm::AArch64::printEnabledExtensions(EnabledFeatureNames); else if (MachineTriple.isRISCV()) { llvm::StringMap DescMap; - for (const llvm::SubtargetFeatureKV &feature : Features) - DescMap.insert({feature.Key, feature.Desc}); + for (const llvm::SubtargetFeatureKV *feature : Features) + DescMap.insert({feature->key(), feature->desc()}); llvm::RISCVISAInfo::printEnabledExtensions(MachineTriple.isArch64Bit(), EnabledFeatureNames, DescMap); } else { @@ -289,20 +289,11 @@ int cc1_main(ArrayRef Argv, const char *Argv0, void *MainAddr) { static_cast(&Clang->getDiagnostics())); DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics()); - if (!Success) { - Clang->getDiagnosticClient().finish(); + if (!Success) return 1; - } // Execute the frontend actions. - { - llvm::TimeTraceScope TimeScope("ExecuteCompiler"); - bool TimePasses = Clang->getCodeGenOpts().TimePasses; - if (TimePasses) - Clang->createFrontendTimer(); - llvm::TimeRegion Timer(TimePasses ? &Clang->getFrontendTimer() : nullptr); - Success = ExecuteCompilerInvocation(Clang.get()); - } + Success = ExecuteCompilerInvocation(Clang.get()); // If any timers were active but haven't been destroyed yet, print their // results now. This happens in -disable-free mode. @@ -339,6 +330,8 @@ int cc1_main(ArrayRef Argv, const char *Argv0, void *MainAddr) { // When running with -disable-free, don't do any destruction or shutdown. if (Clang->getFrontendOpts().DisableFree) { + // DiagnosticConsumer must be always destroyed. + Clang->getDiagnosticClient().~DiagnosticConsumer(); llvm::BuryPointer(std::move(Clang)); return !Success; } diff --git a/src/zig_clang_cc1as_main.cpp b/src/zig_clang_cc1as_main.cpp index 339693e70996e45f802bf4423972f46dbcdce309..077cd69ce4e2caa93013e709933301f5fb654fd2 100644 --- a/src/zig_clang_cc1as_main.cpp +++ b/src/zig_clang_cc1as_main.cpp @@ -177,6 +177,8 @@ struct AssemblerInvocation { LLVM_PREFERRED_TYPE(bool) unsigned X86Sse2Avx : 1; + RelocSectionSymType RelocSectionSym = RelocSectionSymType::All; + /// The name of the relocation model to use. std::string RelocationModel; @@ -387,6 +389,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts, llvm::StringSwitch(A->getValue()) .Case("always", EmitDwarfUnwindType::Always) .Case("no-compact-unwind", EmitDwarfUnwindType::NoCompactUnwind) + .Case("dwarf-only", EmitDwarfUnwindType::DwarfOnly) .Case("default", EmitDwarfUnwindType::Default); } @@ -394,6 +397,12 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts, Args.hasArg(OPT_femit_compact_unwind_non_canonical); Opts.EmitSFrameUnwind = Args.hasArg(OPT_gsframe); Opts.Crel = Args.hasArg(OPT_crel); + Opts.RelocSectionSym = RelocSectionSymType::All; + if (auto *A = Args.getLastArg(OPT_reloc_section_sym)) + Opts.RelocSectionSym = StringSwitch(A->getValue()) + .Case("internal", RelocSectionSymType::Internal) + .Case("none", RelocSectionSymType::None) + .Default(RelocSectionSymType::All); Opts.ImplicitMapsyms = Args.hasArg(OPT_mmapsyms_implicit); Opts.X86RelaxRelocations = !Args.hasArg(OPT_mrelax_relocations_no); Opts.X86Sse2Avx = Args.hasArg(OPT_msse2avx); @@ -461,6 +470,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts, MCOptions.EmitSFrameUnwind = Opts.EmitSFrameUnwind; MCOptions.MCSaveTempLabels = Opts.SaveTemporaryLabels; MCOptions.Crel = Opts.Crel; + MCOptions.RelocSectionSym = Opts.RelocSectionSym; MCOptions.ImplicitMapSyms = Opts.ImplicitMapsyms; MCOptions.X86RelaxRelocations = Opts.X86RelaxRelocations; MCOptions.X86Sse2Avx = Opts.X86Sse2Avx; @@ -497,8 +507,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts, << Opts.CPU << FS.empty() << FS; } - MCContext Ctx(Triple(Opts.Triple), MAI.get(), MRI.get(), STI.get(), &SrcMgr, - &MCOptions); + MCContext Ctx(Triple(Opts.Triple), *MAI, *MRI, *STI, &SrcMgr); bool PIC = false; if (Opts.RelocationModel == "static") { @@ -617,9 +626,8 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts, std::unique_ptr Parser( createMCAsmParser(SrcMgr, Ctx, *Str, *MAI)); - // FIXME: init MCTargetOptions from sanitizer flags here. std::unique_ptr TAP( - TheTarget->createMCAsmParser(*STI, *Parser, *MCII, MCOptions)); + TheTarget->createMCAsmParser(*STI, *Parser, *MCII)); if (!TAP) Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple.str(); diff --git a/src/zig_clang_driver.cpp b/src/zig_clang_driver.cpp index 75d945d6ded8d6cd6a59c2d1d3e48abae41b2306..0020faa0b14bd15b0e1f3ada40c62d65a5593f7b 100644 --- a/src/zig_clang_driver.cpp +++ b/src/zig_clang_driver.cpp @@ -55,6 +55,10 @@ #include #include #include +// zig patch: don't rely on LLVM_ON_UNIX that comes from the build system +#if !defined(_WIN32) +#include +#endif using namespace clang; using namespace clang::driver; @@ -223,6 +227,7 @@ static int ExecuteCC1Tool(SmallVectorImpl &ArgV, return cc1_main(ArrayRef(ArgV).slice(1), ArgV[0], GetExecutablePathVP); if (Tool == "-cc1as") return cc1as_main(ArrayRef(ArgV).slice(2), ArgV[0], GetExecutablePathVP); + // zig patch: no -cc1gen-reproducer // Reject unknown tools. llvm::errs() << "error: unknown integrated tool '" << Tool << "'. " @@ -230,11 +235,13 @@ static int ExecuteCC1Tool(SmallVectorImpl &ArgV, return 1; } +// zig patch: use custom entry point static int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContext) { noteBottomOfStack(); llvm::setBugReportMsg("PLEASE submit a bug report to " BUG_REPORT_URL - " and include the crash backtrace, preprocessed " - "source, and associated run script.\n"); + " and include the crash backtrace and" + " dumped files.\n"); + // zig patch: fix argv offset size_t argv_offset = (strcmp(Argv[1], "-cc1") == 0 || strcmp(Argv[1], "-cc1as") == 0) ? 0 : 1; SmallVector Args(Argv + argv_offset, Argv + Argc); @@ -373,7 +380,8 @@ static int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContex if (!UseNewCC1Process) { TheDriver.CC1Main = ExecuteCC1WithContext; // Ensure the CC1Command actually catches cc1 crashes - llvm::CrashRecoveryContext::Enable(); + llvm::CrashRecoveryContext::Enable( + /*NeedsPOSIXUtilitySignalHandling=*/true); } std::unique_ptr C(TheDriver.BuildCompilation(Args)); @@ -402,6 +410,7 @@ static int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContex Driver::CommandStatus CommandStatus = Driver::CommandStatus::Ok; // Pretend the first command failed if ReproStatus is Always. const Command *FailingCommand = nullptr; + int CommandRes = 0; if (!C->getJobs().empty()) FailingCommand = &*C->getJobs().begin(); if (C && !C->containsError()) { @@ -409,7 +418,7 @@ static int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContex Res = TheDriver.ExecuteCompilation(*C, FailingCommands); for (const auto &P : FailingCommands) { - int CommandRes = P.first; + CommandRes = P.first; FailingCommand = P.second; if (!Res) Res = CommandRes; @@ -421,8 +430,8 @@ static int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContex IsCrash = CommandRes < 0 || CommandRes == 70; #ifdef _WIN32 IsCrash |= CommandRes == 3; -#endif -#if LLVM_ON_UNIX +// zig patch: don't rely on LLVM_ON_UNIX that comes from the build system +#else // When running in integrated-cc1 mode, the CrashRecoveryContext returns // the same codes as if the program crashed. See section "Exit Status for // Commands": @@ -445,8 +454,6 @@ static int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContex *C, *FailingCommand)) Res = 1; - Diags.getClient()->finish(); - if (!UseNewCC1Process && IsCrash) { // When crashing in -fintegrated-cc1 mode, bury the timer pointers, because // the internal linked list might point to already released stack frames. @@ -464,6 +471,28 @@ static int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContex // propagated. if (Res < 0) Res = 1; +// zig patch: don't rely on LLVM_ON_UNIX that comes from the build system +#else + // On Unix, signals are represented by return codes of 128 plus the signal + // number. If the return code indicates it was from a signal handler, raise + // the signal so that the exit code includes the signal number, as required + // by POSIX. Return code 255 is excluded because some tools, such as + // llvm-ifs, exit with code 255 (-1) on failure. + if (CommandRes > 128 && CommandRes != 255) { + llvm::sys::unregisterHandlers(); + // DiagnosticConsumer must be always destroyed. + Diags.getClient()->~DiagnosticConsumer(); + raise(CommandRes - 128); + } + // When cc1 runs out-of-process (CLANG_SPAWN_CC1), ExecuteAndWait returns -2 + // if the child was killed by a signal. The signal number is not preserved, + // so resignal with SIGABRT to ensure the driver exits via signal. + if (CommandRes == -2) { + llvm::sys::unregisterHandlers(); + // DiagnosticConsumer must be always destroyed. + Diags.getClient()->~DiagnosticConsumer(); + raise(SIGABRT); + } #endif // If we have multiple failing commands, we return the result of the first diff --git a/src/zig_llvm-ar.cpp b/src/zig_llvm-ar.cpp index fd00a7be497f2886a7eb483f7ce5b4804176abeb..37fe67af1fca0e424827d14c0830c6055913f8d4 100644 --- a/src/zig_llvm-ar.cpp +++ b/src/zig_llvm-ar.cpp @@ -83,6 +83,7 @@ static void printArHelp(StringRef ToolName) { =darwin - darwin =bsd - bsd =bigarchive - big archive (AIX OS) + =zos - zos archive (z/OS OS) =coff - coff --plugin= - ignored for compatibility -h --help - display this help and exit @@ -195,7 +196,16 @@ static SmallVector PositionalArgs; static bool MRI; namespace { -enum Format { Default, GNU, COFF, BSD, DARWIN, BIGARCHIVE, Unknown }; +enum Format { + Default, + GNU, + COFF, + BSD, + DARWIN, + BIGARCHIVE, + ZOSARCHIVE, + Unknown +}; } static Format FormatType = Default; @@ -713,8 +723,11 @@ static void performReadOperation(ArchiveOperation Operation, }); if (I == Members.end()) continue; - if (CountParam && ++MemberCount[Name] != CountParam) - continue; + if (CountParam) { + std::string CountKey = normalizePath(*I); + if (++MemberCount[CountKey] != CountParam) + continue; + } Members.erase(I); } @@ -854,14 +867,19 @@ static InsertAction computeInsertAction(ArchiveOperation Operation, if (Operation == QuickAppend || Members.empty()) return IA_AddOldMember; - auto MI = find_if(Members, [Name](StringRef Path) { + std::string CountKey; + auto MI = find_if(Members, [Name, &CountKey](StringRef Path) { + SmallString<128> MatchPath(Path); if (Thin && !sys::path::is_absolute(Path)) { Expected PathOrErr = computeArchiveRelativePath(ArchiveName, Path); - return comparePaths(Name, PathOrErr ? *PathOrErr : Path); - } else { - return comparePaths(Name, Path); + if (PathOrErr) + MatchPath = *PathOrErr; } + if (!comparePaths(Name, MatchPath)) + return false; + CountKey = normalizePath(MatchPath); + return true; }); if (MI == Members.end()) @@ -870,7 +888,7 @@ static InsertAction computeInsertAction(ArchiveOperation Operation, Pos = MI; if (Operation == Delete) { - if (CountParam && ++MemberCount[Name] != CountParam) + if (CountParam && ++MemberCount[CountKey] != CountParam) return IA_AddOldMember; return IA_Delete; } @@ -1071,6 +1089,11 @@ static void performWriteOperation(ArchiveOperation Operation, fail("only the gnu format has a thin mode"); Kind = object::Archive::K_AIXBIG; break; + case ZOSARCHIVE: + if (Thin) + fail("only the gnu format has a thin mode"); + Kind = object::Archive::K_ZOS; + break; case Unknown: llvm_unreachable(""); } @@ -1389,6 +1412,7 @@ static int ar_main(int argc, char **argv) { .Case("bsd", BSD) .Case("bigarchive", BIGARCHIVE) .Case("coff", COFF) + .Case("zos", ZOSARCHIVE) .Default(Unknown); if (FormatType == Unknown) fail(std::string("Invalid format ") + Match); @@ -1509,6 +1533,7 @@ static int ranlib_main(int argc, char **argv) { return 0; } +// zig patch: use custom entry point static int llvm_ar_main(int argc, char **argv, const llvm::ToolContext &) { ToolName = argv[0]; diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index 6b3ff4a5fbe626b37c0a49f7b734564c6563201e..44a5fd50eeea8aeee36a942cd85f50c079b8843c 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -373,7 +373,7 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi if (options->is_debug) opt_level = OptimizationLevel::O0; else if (options->is_small) - opt_level = OptimizationLevel::Oz; + opt_level = OptimizationLevel::O2; else opt_level = OptimizationLevel::O3;