Index: clang/lib/Driver/ToolChains/RISCVToolchain.h =================================================================== --- clang/lib/Driver/ToolChains/RISCVToolchain.h +++ clang/lib/Driver/ToolChains/RISCVToolchain.h @@ -26,6 +26,7 @@ llvm::opt::ArgStringList &CC1Args, Action::OffloadKind) const override; bool HasNativeLLVMSupport() const override { return true; } + RuntimeLibType GetDefaultRuntimeLibType() const override; void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override; Index: clang/lib/Driver/ToolChains/RISCVToolchain.cpp =================================================================== --- clang/lib/Driver/ToolChains/RISCVToolchain.cpp +++ clang/lib/Driver/ToolChains/RISCVToolchain.cpp @@ -42,6 +42,13 @@ return new tools::RISCV::Linker(*this); } +ToolChain::RuntimeLibType RISCVToolChain::GetDefaultRuntimeLibType() const { + if (GCCInstallation.isValid()) + return ToolChain::RLT_Libgcc; + else + return ToolChain::RLT_CompilerRT; +} + void RISCVToolChain::addClangTargetOptions( const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, @@ -99,7 +106,7 @@ const InputInfoList &Inputs, const ArgList &Args, const char *LinkingOutput) const { - const ToolChain &ToolChain = getToolChain(); + const auto &ToolChain = getToolChain(); const Driver &D = ToolChain.getDriver(); ArgStringList CmdArgs; @@ -117,9 +124,22 @@ bool WantCRTs = !Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles); + const char *crtbegin, *crtend; + auto RuntimeLib = ToolChain.GetRuntimeLibType(Args); + if (RuntimeLib == ToolChain::RLT_Libgcc) { + crtbegin = "crtbegin.o"; + crtend = "crtend.o"; + } else { + assert (RuntimeLib == ToolChain::RLT_CompilerRT); + crtbegin = ToolChain.getCompilerRTArgString(Args, "crtbegin", + ToolChain::FT_Object); + crtend = ToolChain.getCompilerRTArgString(Args, "crtend", + ToolChain::FT_Object); + } + if (WantCRTs) { CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o"))); - CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o"))); + CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin))); } Args.AddAllArgs(CmdArgs, options::OPT_L); @@ -140,11 +160,11 @@ CmdArgs.push_back("-lc"); CmdArgs.push_back("-lgloss"); CmdArgs.push_back("--end-group"); - CmdArgs.push_back("-lgcc"); + AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args); } if (WantCRTs) - CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o"))); + CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend))); CmdArgs.push_back("-o"); CmdArgs.push_back(Output.getFilename()); Index: clang/test/Driver/riscv32-toolchain-extra.c =================================================================== --- clang/test/Driver/riscv32-toolchain-extra.c +++ clang/test/Driver/riscv32-toolchain-extra.c @@ -22,12 +22,12 @@ // RUN: -target riscv32-unknown-elf 2>&1 \ // RUN: | FileCheck -check-prefix=C-RV32-BAREMETAL-ILP32-NOGCC %s -// C-RV32-BAREMETAL-ILP32-NOGCC: InstalledDir: [[DRIVERDIR:.*]] // C-RV32-BAREMETAL-ILP32-NOGCC: "-fuse-init-array" -// C-RV32-BAREMETAL-ILP32-NOGCC: "-internal-isystem" "[[DRIVERDIR]]/../riscv32-unknown-elf/include" -// C-RV32-BAREMETAL-ILP32-NOGCC: "[[DRIVERDIR]]/riscv32-unknown-elf-ld" -// C-RV32-BAREMETAL-ILP32-NOGCC: "[[DRIVERDIR]]/../riscv32-unknown-elf/lib/crt0.o" -// C-RV32-BAREMETAL-ILP32-NOGCC: "[[DRIVERDIR]]/../riscv32-unknown-elf/lib/crtbegin.o" -// C-RV32-BAREMETAL-ILP32-NOGCC: "-L[[DRIVERDIR]]/../riscv32-unknown-elf/lib" -// C-RV32-BAREMETAL-ILP32-NOGCC: "--start-group" "-lc" "-lgloss" "--end-group" "-lgcc" -// C-RV32-BAREMETAL-ILP32-NOGCC: "[[DRIVERDIR]]/../riscv32-unknown-elf/lib/crtend.o" +// C-RV32-BAREMETAL-ILP32-NOGCC: "-internal-isystem" "{{.*}}Output/testroot-riscv32-baremetal-nogcc/bin/../riscv32-unknown-elf/include" +// C-RV32-BAREMETAL-ILP32-NOGCC: "{{.*}}Output/testroot-riscv32-baremetal-nogcc/bin/riscv32-unknown-elf-ld" +// C-RV32-BAREMETAL-ILP32-NOGCC: "{{.*}}Output/testroot-riscv32-baremetal-nogcc/bin/../riscv32-unknown-elf/lib/crt0.o" +// C-RV32-BAREMETAL-ILP32-NOGCC: "{{.*}}Output/testroot-riscv32-baremetal-nogcc/lib/clang/{{[0-9.]*}}/lib/clang_rt.crtbegin-riscv32.o" +// C-RV32-BAREMETAL-ILP32-NOGCC: "{{.*}}Output/testroot-riscv32-baremetal-nogcc/bin/../riscv32-unknown-elf/lib" +// C-RV32-BAREMETAL-ILP32-NOGCC: "--start-group" "-lc" "-lgloss" "--end-group" +// C-RV32-BAREMETAL-ILP32-NOGCC: "{{.*}}Output/testroot-riscv32-baremetal-nogcc/lib/clang/{{[0-9.]*}}/lib/libclang_rt.builtins-riscv32.a" +// C-RV32-BAREMETAL-ILP32-NOGCC: "{{.*}}Output/testroot-riscv32-baremetal-nogcc/lib/clang/{{[0-9.]*}}/lib/clang_rt.crtend-riscv32.o" Index: clang/test/Driver/riscv64-toolchain-extra.c =================================================================== --- clang/test/Driver/riscv64-toolchain-extra.c +++ clang/test/Driver/riscv64-toolchain-extra.c @@ -22,12 +22,12 @@ // RUN: -target riscv64-unknown-elf 2>&1 \ // RUN: | FileCheck -check-prefix=C-RV64-BAREMETAL-LP64-NOGCC %s -// C-RV64-BAREMETAL-LP64-NOGCC: InstalledDir: [[DRIVERDIR:.*]] // C-RV64-BAREMETAL-LP64-NOGCC: "-fuse-init-array" -// C-RV64-BAREMETAL-LP64-NOGCC: "-internal-isystem" "[[DRIVERDIR]]/../riscv64-unknown-elf/include" -// C-RV64-BAREMETAL-LP64-NOGCC: "[[DRIVERDIR]]/riscv64-unknown-elf-ld" -// C-RV64-BAREMETAL-LP64-NOGCC: "[[DRIVERDIR]]/../riscv64-unknown-elf/lib/crt0.o" -// C-RV64-BAREMETAL-LP64-NOGCC: "[[DRIVERDIR]]/../riscv64-unknown-elf/lib/crtbegin.o" -// C-RV64-BAREMETAL-LP64-NOGCC: "-L[[DRIVERDIR]]/../riscv64-unknown-elf/lib" -// C-RV64-BAREMETAL-LP64-NOGCC: "--start-group" "-lc" "-lgloss" "--end-group" "-lgcc" -// C-RV64-BAREMETAL-LP64-NOGCC: "[[DRIVERDIR]]/../riscv64-unknown-elf/lib/crtend.o" +// C-RV64-BAREMETAL-LP64-NOGCC: "-internal-isystem" "{{.*}}Output/testroot-riscv64-baremetal-nogcc/bin/../riscv64-unknown-elf/include" +// C-RV64-BAREMETAL-LP64-NOGCC: "{{.*}}Output/testroot-riscv64-baremetal-nogcc/bin/riscv64-unknown-elf-ld" +// C-RV64-BAREMETAL-LP64-NOGCC: "{{.*}}Output/testroot-riscv64-baremetal-nogcc/bin/../riscv64-unknown-elf/lib/crt0.o" +// C-RV64-BAREMETAL-LP64-NOGCC: "{{.*}}Output/testroot-riscv64-baremetal-nogcc/lib/clang/{{[0-9.]*}}/lib/clang_rt.crtbegin-riscv64.o" +// C-RV64-BAREMETAL-LP64-NOGCC: "{{.*}}Output/testroot-riscv64-baremetal-nogcc/bin/../riscv64-unknown-elf/lib" +// C-RV64-BAREMETAL-LP64-NOGCC: "--start-group" "-lc" "-lgloss" "--end-group" +// C-RV64-BAREMETAL-LP64-NOGCC: "{{.*}}Output/testroot-riscv64-baremetal-nogcc/lib/clang/{{[0-9.]*}}/lib/libclang_rt.builtins-riscv64.a" +// C-RV64-BAREMETAL-LP64-NOGCC: "{{.*}}Output/testroot-riscv64-baremetal-nogcc/lib/clang/{{[0-9.]*}}/lib/clang_rt.crtend-riscv64.o"