diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -1239,6 +1239,8 @@ def flat__namespace : Flag<["-"], "flat_namespace">; def flax_vector_conversions : Flag<["-"], "flax-vector-conversions">, Group; def flimited_precision_EQ : Joined<["-"], "flimited-precision=">, Group; +def flink_builtins_rt : Flag<["-"], "flink-builtins-rt">, Group, + HelpText<"Force linking the clang builtins runtime library">; def flto_EQ : Joined<["-"], "flto=">, Flags<[CoreOption, CC1Option]>, Group, HelpText<"Set LTO mode to either 'full' or 'thin'">, Values<"thin,full">; def flto : Flag<["-"], "flto">, Flags<[CoreOption, CC1Option]>, Group, diff --git a/clang/lib/Driver/ToolChains/Darwin.h b/clang/lib/Driver/ToolChains/Darwin.h --- a/clang/lib/Driver/ToolChains/Darwin.h +++ b/clang/lib/Driver/ToolChains/Darwin.h @@ -157,7 +157,8 @@ /// FIXME: This API is intended for use with embedded libraries only, and is /// misleadingly named. virtual void AddLinkRuntimeLibArgs(const llvm::opt::ArgList &Args, - llvm::opt::ArgStringList &CmdArgs) const; + llvm::opt::ArgStringList &CmdArgs, + bool ForceLinkBuiltinRT = false) const; virtual void addStartObjectFileArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const { @@ -495,7 +496,8 @@ RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const override; void AddLinkRuntimeLibArgs(const llvm::opt::ArgList &Args, - llvm::opt::ArgStringList &CmdArgs) const override; + llvm::opt::ArgStringList &CmdArgs, + bool ForceLinkBuiltinRT = false) const override; void AddClangCXXStdlibIncludeArgs( const llvm::opt::ArgList &DriverArgs, diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp --- a/clang/lib/Driver/ToolChains/Darwin.cpp +++ b/clang/lib/Driver/ToolChains/Darwin.cpp @@ -568,15 +568,26 @@ if (getToolChain().ShouldLinkCXXStdlib(Args)) getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); - if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { + + bool NoStdOrDefaultLibs = + Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs); + bool ForceLinkBuiltins = Args.hasArg(options::OPT_flink_builtins_rt); + if (!NoStdOrDefaultLibs || ForceLinkBuiltins) { // link_ssp spec is empty. - // Let the tool chain choose which runtime library to link. - getMachOToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs); + // If we have both -nostdlib/nodefaultlibs and -flink-builtins-rt then + // we just want to link the builtins, not the other libs like libSystem. + if (NoStdOrDefaultLibs && ForceLinkBuiltins) { + getMachOToolChain().AddLinkRuntimeLib(Args, CmdArgs, "builtins"); + } else { + // Let the tool chain choose which runtime library to link. + getMachOToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs, + ForceLinkBuiltins); - // No need to do anything for pthreads. Claim argument to avoid warning. - Args.ClaimAllArgs(options::OPT_pthread); - Args.ClaimAllArgs(options::OPT_pthreads); + // No need to do anything for pthreads. Claim argument to avoid warning. + Args.ClaimAllArgs(options::OPT_pthread); + Args.ClaimAllArgs(options::OPT_pthreads); + } } if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) { @@ -1074,7 +1085,8 @@ } void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args, - ArgStringList &CmdArgs) const { + ArgStringList &CmdArgs, + bool ForceLinkBuiltinRT) const { // Call once to ensure diagnostic is printed if wrong value was specified GetRuntimeLibType(Args); @@ -1082,8 +1094,11 @@ // libraries with -static. if (Args.hasArg(options::OPT_static) || Args.hasArg(options::OPT_fapple_kext) || - Args.hasArg(options::OPT_mkernel)) + Args.hasArg(options::OPT_mkernel)) { + if (ForceLinkBuiltinRT) + AddLinkRuntimeLib(Args, CmdArgs, "builtins"); return; + } // Reject -static-libgcc for now, we can deal with this when and if someone // cares. This is useful in situations where someone wants to statically link @@ -2054,7 +2069,8 @@ } void MachO::AddLinkRuntimeLibArgs(const ArgList &Args, - ArgStringList &CmdArgs) const { + ArgStringList &CmdArgs, + bool ForceLinkBuiltinRT) const { // Embedded targets are simple at the moment, not supporting sanitizers and // with different libraries for each member of the product { static, PIC } x // { hard-float, soft-float } diff --git a/clang/test/Driver/darwin-flink-builtins-rt.c b/clang/test/Driver/darwin-flink-builtins-rt.c new file mode 100644 --- /dev/null +++ b/clang/test/Driver/darwin-flink-builtins-rt.c @@ -0,0 +1,6 @@ +// RUN: %clang -target arm64-apple-ios12.0 %s -nostdlib -flink-builtins-rt -### 2>&1 | FileCheck %s +// RUN: %clang -target arm64-apple-ios12.0 %s -static -flink-builtins-rt -### 2>&1 | FileCheck %s +// RUN: %clang -target arm64-apple-ios12.0 %s -flink-builtins-rt -### 2>&1 | FileCheck %s --check-prefix=DEFAULT +// CHECK-NOT: "-lSystem" +// DEFAULT: "-lSystem" +// CHECK: libclang_rt.ios.a