Index: clang/include/clang/Driver/XRayArgs.h =================================================================== --- clang/include/clang/Driver/XRayArgs.h +++ clang/include/clang/Driver/XRayArgs.h @@ -31,6 +31,8 @@ XRayArgs(const ToolChain &TC, const llvm::opt::ArgList &Args); void addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const; + + bool needsXRayRt() const { return XRayInstrument; } }; } // namespace driver Index: clang/lib/Driver/ToolChains/CommonArgs.h =================================================================== --- clang/lib/Driver/ToolChains/CommonArgs.h +++ clang/lib/Driver/ToolChains/CommonArgs.h @@ -35,6 +35,12 @@ void linkSanitizerRuntimeDeps(const ToolChain &TC, llvm::opt::ArgStringList &CmdArgs); +bool addXRayRuntime(const ToolChain &TC, const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &CmdArgs); + +void linkXRayRuntimeDeps(const ToolChain &TC, + llvm::opt::ArgStringList &CmdArgs); + void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args); Index: clang/lib/Driver/ToolChains/CommonArgs.cpp =================================================================== --- clang/lib/Driver/ToolChains/CommonArgs.cpp +++ clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -31,6 +31,7 @@ #include "clang/Driver/SanitizerArgs.h" #include "clang/Driver/ToolChain.h" #include "clang/Driver/Util.h" +#include "clang/Driver/XRayArgs.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" @@ -705,6 +706,33 @@ return !StaticRuntimes.empty() || !NonWholeStaticRuntimes.empty(); } +bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringList &CmdArgs) { + if (Args.hasArg(options::OPT_shared)) + return false; + + if (TC.getXRayArgs().needsXRayRt()) { + CmdArgs.push_back("-whole-archive"); + CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray", false)); + CmdArgs.push_back("-no-whole-archive"); + return true; + } + + return false; +} + +void tools::linkXRayRuntimeDeps(const ToolChain &TC, ArgStringList &CmdArgs) { + CmdArgs.push_back("--no-as-needed"); + CmdArgs.push_back("-lpthread"); + if (TC.getTriple().getOS() != llvm::Triple::OpenBSD) + CmdArgs.push_back("-lrt"); + CmdArgs.push_back("-lm"); + + if (TC.getTriple().getOS() != llvm::Triple::FreeBSD && + TC.getTriple().getOS() != llvm::Triple::NetBSD && + TC.getTriple().getOS() != llvm::Triple::OpenBSD) + CmdArgs.push_back("-ldl"); +} + bool tools::areOptimizationsEnabled(const ArgList &Args) { // Find the last -O arg and see if it is non-zero. if (Arg *A = Args.getLastArg(options::OPT_O_Group)) Index: clang/lib/Driver/ToolChains/FreeBSD.cpp =================================================================== --- clang/lib/Driver/ToolChains/FreeBSD.cpp +++ clang/lib/Driver/ToolChains/FreeBSD.cpp @@ -117,30 +117,6 @@ C.addCommand(llvm::make_unique(JA, *this, Exec, CmdArgs, Inputs)); } -static bool addXRayRuntime(const ToolChain &TC, const ArgList &Args, - ArgStringList &CmdArgs) { - if (Args.hasArg(options::OPT_shared)) - return false; - - if (Args.hasFlag(options::OPT_fxray_instrument, - options::OPT_fnoxray_instrument, false)) { - CmdArgs.push_back("-whole-archive"); - CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray", false)); - CmdArgs.push_back("-no-whole-archive"); - return true; - } - - return false; -} - -static void linkXRayRuntimeDeps(const ToolChain &TC, const ArgList &Args, - ArgStringList &CmdArgs) { - CmdArgs.push_back("--no-as-needed"); - CmdArgs.push_back("-lrt"); - CmdArgs.push_back("-lm"); - CmdArgs.push_back("-lpthread"); -} - void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, @@ -275,7 +251,7 @@ if (NeedsSanitizerDeps) linkSanitizerRuntimeDeps(ToolChain, CmdArgs); if (NeedsXRayDeps) - linkXRayRuntimeDeps(ToolChain, Args, CmdArgs); + linkXRayRuntimeDeps(ToolChain, CmdArgs); // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding // the default system libraries. Just mimic this for now. if (Args.hasArg(options::OPT_pg)) Index: clang/lib/Driver/ToolChains/Gnu.cpp =================================================================== --- clang/lib/Driver/ToolChains/Gnu.cpp +++ clang/lib/Driver/ToolChains/Gnu.cpp @@ -221,37 +221,6 @@ // The types are (hopefully) good enough. } -static bool addXRayRuntime(const ToolChain &TC, const ArgList &Args, - ArgStringList &CmdArgs) { - // Do not add the XRay runtime to shared libraries. - if (Args.hasArg(options::OPT_shared)) - return false; - - if (Args.hasFlag(options::OPT_fxray_instrument, - options::OPT_fnoxray_instrument, false)) { - CmdArgs.push_back("-whole-archive"); - CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray", false)); - CmdArgs.push_back("-no-whole-archive"); - return true; - } - - return false; -} - -static void linkXRayRuntimeDeps(const ToolChain &TC, const ArgList &Args, - ArgStringList &CmdArgs) { - CmdArgs.push_back("--no-as-needed"); - CmdArgs.push_back("-lpthread"); - if (TC.getTriple().getOS() != llvm::Triple::OpenBSD) - CmdArgs.push_back("-lrt"); - CmdArgs.push_back("-lm"); - - if (TC.getTriple().getOS() != llvm::Triple::FreeBSD && - TC.getTriple().getOS() != llvm::Triple::NetBSD && - TC.getTriple().getOS() != llvm::Triple::OpenBSD) - CmdArgs.push_back("-ldl"); -} - static const char *getLDMOption(const llvm::Triple &T, const ArgList &Args) { switch (T.getArch()) { case llvm::Triple::x86: @@ -496,7 +465,7 @@ linkSanitizerRuntimeDeps(ToolChain, CmdArgs); if (NeedsXRayDeps) - linkXRayRuntimeDeps(ToolChain, Args, CmdArgs); + linkXRayRuntimeDeps(ToolChain, CmdArgs); bool WantPthread = Args.hasArg(options::OPT_pthread) || Args.hasArg(options::OPT_pthreads);