Skip to content

Commit 6244037

Browse files
committedApr 6, 2018
[XRay][clang] Consolidate runtime and link-time flag processing (NFC)
Summary: This change fixes http://llvm.org/PR36985 to define a single place in CommonArgs.{h,cpp} where XRay runtime flags and link-time dependencies are processed for all toolchains that support XRay instrumentation. This is a refactoring of the same functionality spread across multiple toolchain definitions. Reviewers: echristo, devnexen, eizan Reviewed By: eizan Subscribers: emaste, cfe-commits Differential Revision: https://reviews.llvm.org/D45243 llvm-svn: 329372
1 parent 3bd98b1 commit 6244037

File tree

6 files changed

+39
-81
lines changed

6 files changed

+39
-81
lines changed
 

‎clang/include/clang/Driver/XRayArgs.h

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class XRayArgs {
3131
XRayArgs(const ToolChain &TC, const llvm::opt::ArgList &Args);
3232
void addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
3333
llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const;
34+
35+
bool needsXRayRt() const { return XRayInstrument; }
3436
};
3537

3638
} // namespace driver

‎clang/lib/Driver/ToolChains/CommonArgs.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "clang/Driver/SanitizerArgs.h"
3232
#include "clang/Driver/ToolChain.h"
3333
#include "clang/Driver/Util.h"
34+
#include "clang/Driver/XRayArgs.h"
3435
#include "llvm/ADT/STLExtras.h"
3536
#include "llvm/ADT/SmallString.h"
3637
#include "llvm/ADT/StringExtras.h"
@@ -705,6 +706,33 @@ bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
705706
return !StaticRuntimes.empty() || !NonWholeStaticRuntimes.empty();
706707
}
707708

709+
bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringList &CmdArgs) {
710+
if (Args.hasArg(options::OPT_shared))
711+
return false;
712+
713+
if (TC.getXRayArgs().needsXRayRt()) {
714+
CmdArgs.push_back("-whole-archive");
715+
CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray", false));
716+
CmdArgs.push_back("-no-whole-archive");
717+
return true;
718+
}
719+
720+
return false;
721+
}
722+
723+
void tools::linkXRayRuntimeDeps(const ToolChain &TC, ArgStringList &CmdArgs) {
724+
CmdArgs.push_back("--no-as-needed");
725+
CmdArgs.push_back("-lpthread");
726+
if (TC.getTriple().getOS() != llvm::Triple::OpenBSD)
727+
CmdArgs.push_back("-lrt");
728+
CmdArgs.push_back("-lm");
729+
730+
if (TC.getTriple().getOS() != llvm::Triple::FreeBSD &&
731+
TC.getTriple().getOS() != llvm::Triple::NetBSD &&
732+
TC.getTriple().getOS() != llvm::Triple::OpenBSD)
733+
CmdArgs.push_back("-ldl");
734+
}
735+
708736
bool tools::areOptimizationsEnabled(const ArgList &Args) {
709737
// Find the last -O arg and see if it is non-zero.
710738
if (Arg *A = Args.getLastArg(options::OPT_O_Group))

‎clang/lib/Driver/ToolChains/CommonArgs.h

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args,
3535
void linkSanitizerRuntimeDeps(const ToolChain &TC,
3636
llvm::opt::ArgStringList &CmdArgs);
3737

38+
bool addXRayRuntime(const ToolChain &TC, const llvm::opt::ArgList &Args,
39+
llvm::opt::ArgStringList &CmdArgs);
40+
41+
void linkXRayRuntimeDeps(const ToolChain &TC,
42+
llvm::opt::ArgStringList &CmdArgs);
43+
3844
void AddRunTimeLibs(const ToolChain &TC, const Driver &D,
3945
llvm::opt::ArgStringList &CmdArgs,
4046
const llvm::opt::ArgList &Args);

‎clang/lib/Driver/ToolChains/FreeBSD.cpp

+1-25
Original file line numberDiff line numberDiff line change
@@ -117,30 +117,6 @@ void freebsd::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
117117
C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
118118
}
119119

120-
static bool addXRayRuntime(const ToolChain &TC, const ArgList &Args,
121-
ArgStringList &CmdArgs) {
122-
if (Args.hasArg(options::OPT_shared))
123-
return false;
124-
125-
if (Args.hasFlag(options::OPT_fxray_instrument,
126-
options::OPT_fnoxray_instrument, false)) {
127-
CmdArgs.push_back("-whole-archive");
128-
CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray", false));
129-
CmdArgs.push_back("-no-whole-archive");
130-
return true;
131-
}
132-
133-
return false;
134-
}
135-
136-
static void linkXRayRuntimeDeps(const ToolChain &TC, const ArgList &Args,
137-
ArgStringList &CmdArgs) {
138-
CmdArgs.push_back("--no-as-needed");
139-
CmdArgs.push_back("-lrt");
140-
CmdArgs.push_back("-lm");
141-
CmdArgs.push_back("-lpthread");
142-
}
143-
144120
void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
145121
const InputInfo &Output,
146122
const InputInfoList &Inputs,
@@ -275,7 +251,7 @@ void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
275251
if (NeedsSanitizerDeps)
276252
linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
277253
if (NeedsXRayDeps)
278-
linkXRayRuntimeDeps(ToolChain, Args, CmdArgs);
254+
linkXRayRuntimeDeps(ToolChain, CmdArgs);
279255
// FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
280256
// the default system libraries. Just mimic this for now.
281257
if (Args.hasArg(options::OPT_pg))

‎clang/lib/Driver/ToolChains/Gnu.cpp

+1-32
Original file line numberDiff line numberDiff line change
@@ -221,37 +221,6 @@ void tools::gcc::Linker::RenderExtraToolArgs(const JobAction &JA,
221221
// The types are (hopefully) good enough.
222222
}
223223

224-
static bool addXRayRuntime(const ToolChain &TC, const ArgList &Args,
225-
ArgStringList &CmdArgs) {
226-
// Do not add the XRay runtime to shared libraries.
227-
if (Args.hasArg(options::OPT_shared))
228-
return false;
229-
230-
if (Args.hasFlag(options::OPT_fxray_instrument,
231-
options::OPT_fnoxray_instrument, false)) {
232-
CmdArgs.push_back("-whole-archive");
233-
CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray", false));
234-
CmdArgs.push_back("-no-whole-archive");
235-
return true;
236-
}
237-
238-
return false;
239-
}
240-
241-
static void linkXRayRuntimeDeps(const ToolChain &TC, const ArgList &Args,
242-
ArgStringList &CmdArgs) {
243-
CmdArgs.push_back("--no-as-needed");
244-
CmdArgs.push_back("-lpthread");
245-
if (TC.getTriple().getOS() != llvm::Triple::OpenBSD)
246-
CmdArgs.push_back("-lrt");
247-
CmdArgs.push_back("-lm");
248-
249-
if (TC.getTriple().getOS() != llvm::Triple::FreeBSD &&
250-
TC.getTriple().getOS() != llvm::Triple::NetBSD &&
251-
TC.getTriple().getOS() != llvm::Triple::OpenBSD)
252-
CmdArgs.push_back("-ldl");
253-
}
254-
255224
static const char *getLDMOption(const llvm::Triple &T, const ArgList &Args) {
256225
switch (T.getArch()) {
257226
case llvm::Triple::x86:
@@ -496,7 +465,7 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
496465
linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
497466

498467
if (NeedsXRayDeps)
499-
linkXRayRuntimeDeps(ToolChain, Args, CmdArgs);
468+
linkXRayRuntimeDeps(ToolChain, CmdArgs);
500469

501470
bool WantPthread = Args.hasArg(options::OPT_pthread) ||
502471
Args.hasArg(options::OPT_pthreads);

‎clang/lib/Driver/ToolChains/OpenBSD.cpp

+1-24
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,6 @@ using namespace clang::driver::toolchains;
2222
using namespace clang;
2323
using namespace llvm::opt;
2424

25-
static bool addXRayRuntime(const ToolChain &TC, const ArgList &Args,
26-
ArgStringList &CmdArgs) {
27-
if (Args.hasArg(options::OPT_shared))
28-
return false;
29-
30-
if (Args.hasFlag(options::OPT_fxray_instrument,
31-
options::OPT_fnoxray_instrument, false)) {
32-
CmdArgs.push_back("-whole-archive");
33-
CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray", false));
34-
CmdArgs.push_back("-no-whole-archive");
35-
return true;
36-
}
37-
38-
return false;
39-
}
40-
41-
static void linkXRayRuntimeDeps(const ToolChain &TC, const ArgList &Args,
42-
ArgStringList &CmdArgs) {
43-
CmdArgs.push_back("--no-as-needed");
44-
CmdArgs.push_back("-lm");
45-
CmdArgs.push_back("-lpthread");
46-
}
47-
4825
void openbsd::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
4926
const InputInfo &Output,
5027
const InputInfoList &Inputs,
@@ -221,7 +198,7 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
221198
}
222199
if (NeedsXRayDeps) {
223200
CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins", false));
224-
linkXRayRuntimeDeps(ToolChain, Args, CmdArgs);
201+
linkXRayRuntimeDeps(ToolChain, CmdArgs);
225202
}
226203
// FIXME: For some reason GCC passes -lgcc before adding
227204
// the default system libraries. Just mimic this for now.

0 commit comments

Comments
 (0)
Please sign in to comment.