From fdc7eb8531f40320d3aef5e5f91310cb724005ea Mon Sep 17 00:00:00 2001 From: Dean Michael Berris Date: Wed, 4 Apr 2018 12:47:49 +0000 Subject: [PATCH] [XRay][clang] Allow clang to build XRay instrumented binaries in OpenBSD Summary: This patch was originally reviewed in D45126. It enables clang to add the XRay runtime and the link-time dependencies for XRay instrumentation in OpenBSD. Landing for devnexen. Reviewers: brad, dberris Subscribers: dberris, krytarowski, cfe-commits Author: devnexen Differential Revision: https://reviews.llvm.org/D45126 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@329183 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Driver/ToolChains/OpenBSD.cpp | 28 ++++++++++++++++++++++++++++ lib/Driver/XRayArgs.cpp | 3 ++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/Driver/ToolChains/OpenBSD.cpp b/lib/Driver/ToolChains/OpenBSD.cpp index c0f8eff641..3a33432f3d 100644 --- a/lib/Driver/ToolChains/OpenBSD.cpp +++ b/lib/Driver/ToolChains/OpenBSD.cpp @@ -22,6 +22,29 @@ using namespace clang::driver::toolchains; using namespace clang; using namespace llvm::opt; +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("-lm"); + CmdArgs.push_back("-lpthread"); +} + void openbsd::Assembler::ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, @@ -180,6 +203,7 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA, options::OPT_Z_Flag, options::OPT_r}); bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs); + bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs); AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA); if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { @@ -195,6 +219,10 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins", false)); linkSanitizerRuntimeDeps(ToolChain, CmdArgs); } + if (NeedsXRayDeps) { + CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins", false)); + linkXRayRuntimeDeps(ToolChain, Args, CmdArgs); + } // FIXME: For some reason GCC passes -lgcc before adding // the default system libraries. Just mimic this for now. CmdArgs.push_back("-lgcc"); diff --git a/lib/Driver/XRayArgs.cpp b/lib/Driver/XRayArgs.cpp index c85d89cfd9..19bf9b7d22 100644 --- a/lib/Driver/XRayArgs.cpp +++ b/lib/Driver/XRayArgs.cpp @@ -49,7 +49,8 @@ XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) { D.Diag(diag::err_drv_clang_unsupported) << (std::string(XRayInstrumentOption) + " on " + Triple.str()); } - } else if (Triple.getOS() == llvm::Triple::FreeBSD) { + } else if (Triple.getOS() == llvm::Triple::FreeBSD || + Triple.getOS() == llvm::Triple::OpenBSD) { if (Triple.getArch() != llvm::Triple::x86_64) { D.Diag(diag::err_drv_clang_unsupported) << (std::string(XRayInstrumentOption) + " on " + Triple.str()); -- 2.40.0