Skip to content

Commit

Permalink
[Darwin] Introduce a new flag, -fapple-link-rtlib that forces linking…
Browse files Browse the repository at this point in the history
… of the builtins library.

This driver flag is useful when users want to link against the compiler's
builtins, but nothing else, and so use flags like -nostdlib.

Darwin can't use -nolibc & nostdlib++ like other platforms on because we
disable all runtime lib linking with -static, which we still want to have
an option to link with the builtins.

Differential Revision: https://reviews.llvm.org/D58320

llvm-svn: 360483
  • Loading branch information
aemerson authored and MrSidims committed May 24, 2019
1 parent ba3d49d commit c758e37
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
2 changes: 2 additions & 0 deletions clang/include/clang/Driver/Options.td
Expand Up @@ -1253,6 +1253,8 @@ def fno_fine_grained_bitfield_accesses : Flag<["-"],
def flat__namespace : Flag<["-"], "flat_namespace">;
def flax_vector_conversions : Flag<["-"], "flax-vector-conversions">, Group<f_Group>;
def flimited_precision_EQ : Joined<["-"], "flimited-precision=">, Group<f_Group>;
def fapple_link_rtlib : Flag<["-"], "fapple-link-rtlib">, Group<f_Group>,
HelpText<"Force linking the clang builtins runtime library">;
def flto_EQ : Joined<["-"], "flto=">, Flags<[CoreOption, CC1Option]>, Group<f_Group>,
HelpText<"Set LTO mode to either 'full' or 'thin'">, Values<"thin,full">;
def flto : Flag<["-"], "flto">, Flags<[CoreOption, CC1Option]>, Group<f_Group>,
Expand Down
34 changes: 25 additions & 9 deletions clang/lib/Driver/ToolChains/Darwin.cpp
Expand Up @@ -593,15 +593,26 @@ void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,

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_fapple_link_rtlib);
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 -fapple-link-rtlib 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)) {
Expand Down Expand Up @@ -1128,16 +1139,20 @@ ToolChain::RuntimeLibType DarwinClang::GetRuntimeLibType(
}

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);

// Darwin doesn't support real static executables, don't link any runtime
// 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
Expand Down Expand Up @@ -2106,7 +2121,8 @@ DerivedArgList *MachO::TranslateArgs(const DerivedArgList &Args,
}

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 }
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/Driver/ToolChains/Darwin.h
Expand Up @@ -157,7 +157,8 @@ class LLVM_LIBRARY_VISIBILITY MachO : public ToolChain {
/// 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 {
Expand Down Expand Up @@ -495,7 +496,8 @@ class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin {
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,
Expand Down
6 changes: 6 additions & 0 deletions clang/test/Driver/darwin-fapple-link-rtlib.c
@@ -0,0 +1,6 @@
// RUN: %clang -target arm64-apple-ios12.0 %s -nostdlib -fapple-link-rtlib -resource-dir=%S/Inputs/resource_dir -### 2>&1 | FileCheck %s
// RUN: %clang -target arm64-apple-ios12.0 %s -static -fapple-link-rtlib -resource-dir=%S/Inputs/resource_dir -### 2>&1 | FileCheck %s
// RUN: %clang -target arm64-apple-ios12.0 %s -fapple-link-rtlib -resource-dir=%S/Inputs/resource_dir -### 2>&1 | FileCheck %s --check-prefix=DEFAULT
// CHECK-NOT: "-lSystem"
// DEFAULT: "-lSystem"
// CHECK: libclang_rt.ios.a

0 comments on commit c758e37

Please sign in to comment.