Skip to content

Commit c4b4924

Browse files
committedFeb 13, 2014
Add EXPERIMENTAL --rtlib=compiler-rt to GNU Clang
This commit is not strictly correct nor accounts for all uses (shared objects, for example), but it allows one to test the compiler-rt library on GNU targets. Using this patch to run the test-suite has already shown me problems on ARM. Since this is a Darwin-only flag, nobody is using it, so it shouldn't be a problem. I will need extension to deal with the shared cases, but since we're not compiling libclang_rt.so, that's not yet applicable. Many other problems will have to be fixed first in compiler-rt (such as removing the 'arch' name from it and making it trully multi-arch, moving it to the default lib directory, make both .a and .so variants, etc). llvm-svn: 201307
1 parent f6cb35a commit c4b4924

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed
 

‎clang/lib/Driver/Tools.cpp

+37-2
Original file line numberDiff line numberDiff line change
@@ -1737,6 +1737,24 @@ static StringRef getArchNameForCompilerRTLib(const ToolChain &TC) {
17371737
return TC.getArchName();
17381738
}
17391739

1740+
// This adds the static libclang_rt.arch.a directly to the command line
1741+
// FIXME: Make sure we can also emit shared objects if they're requested
1742+
// and available, check for possible errors, etc.
1743+
static void addClangRTLinux(
1744+
const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs) {
1745+
// The runtime is located in the Linux library directory and has name
1746+
// "libclang_rt.<ArchName>.a".
1747+
SmallString<128> LibProfile(TC.getDriver().ResourceDir);
1748+
llvm::sys::path::append(
1749+
LibProfile, "lib", "linux",
1750+
Twine("libclang_rt.") + getArchNameForCompilerRTLib(TC) + ".a");
1751+
1752+
CmdArgs.push_back(Args.MakeArgString(LibProfile));
1753+
CmdArgs.push_back("-lgcc_s");
1754+
if (TC.getDriver().CCCIsCXX())
1755+
CmdArgs.push_back("-lgcc_eh");
1756+
}
1757+
17401758
static void addProfileRTLinux(
17411759
const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs) {
17421760
if (!(Args.hasArg(options::OPT_fprofile_arcs) ||
@@ -6534,6 +6552,23 @@ static StringRef getLinuxDynamicLinker(const ArgList &Args,
65346552
return "/lib64/ld-linux-x86-64.so.2";
65356553
}
65366554

6555+
static void AddRunTimeLibs(const ToolChain &TC, const Driver &D,
6556+
ArgStringList &CmdArgs, const ArgList &Args) {
6557+
// Make use of compiler-rt if --rtlib option is used
6558+
ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(Args);
6559+
6560+
switch(RLT) {
6561+
case ToolChain::RLT_CompilerRT:
6562+
addClangRTLinux(TC, Args, CmdArgs);
6563+
break;
6564+
case ToolChain::RLT_Libgcc:
6565+
AddLibgcc(TC.getTriple(), D, CmdArgs, Args);
6566+
break;
6567+
default:
6568+
llvm_unreachable("Unknown RT-Lib type");
6569+
}
6570+
}
6571+
65376572
void gnutools::Link::ConstructJob(Compilation &C, const JobAction &JA,
65386573
const InputInfo &Output,
65396574
const InputInfoList &Inputs,
@@ -6737,7 +6772,7 @@ void gnutools::Link::ConstructJob(Compilation &C, const JobAction &JA,
67376772
CmdArgs.push_back("-lrt");
67386773
}
67396774

6740-
AddLibgcc(ToolChain.getTriple(), D, CmdArgs, Args);
6775+
AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
67416776

67426777
if (Args.hasArg(options::OPT_pthread) ||
67436778
Args.hasArg(options::OPT_pthreads) || OpenMP)
@@ -6748,7 +6783,7 @@ void gnutools::Link::ConstructJob(Compilation &C, const JobAction &JA,
67486783
if (Args.hasArg(options::OPT_static))
67496784
CmdArgs.push_back("--end-group");
67506785
else
6751-
AddLibgcc(ToolChain.getTriple(), D, CmdArgs, Args);
6786+
AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
67526787
}
67536788

67546789
if (!Args.hasArg(options::OPT_nostartfiles)) {

‎clang/test/Driver/linux-ld.c

+40
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,46 @@
3535
//
3636
// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
3737
// RUN: --target=x86_64-unknown-linux \
38+
// RUN: --sysroot=%S/Inputs/basic_linux_tree \
39+
// RUN: --rtlib=compiler-rt \
40+
// RUN: | FileCheck --check-prefix=CHECK-LD-RT %s
41+
// CHECK-LD-RT-NOT: warning:
42+
// CHECK-LD-RT: "{{.*}}ld{{(.exe)?}}" "--sysroot=[[SYSROOT:[^"]+]]"
43+
// CHECK-LD-RT: "--eh-frame-hdr"
44+
// CHECK-LD-RT: "-m" "elf_x86_64"
45+
// CHECK-LD-RT: "-dynamic-linker"
46+
// CHECK-LD-RT: "{{.*}}/usr/lib/gcc/x86_64-unknown-linux/4.6.0{{/|\\\\}}crtbegin.o"
47+
// CHECK-LD-RT: "-L[[SYSROOT]]/usr/lib/gcc/x86_64-unknown-linux/4.6.0"
48+
// CHECK-LD-RT: "-L[[SYSROOT]]/usr/lib/gcc/x86_64-unknown-linux/4.6.0/../../../../x86_64-unknown-linux/lib"
49+
// CHECK-LD-RT: "-L[[SYSROOT]]/usr/lib/gcc/x86_64-unknown-linux/4.6.0/../../.."
50+
// CHECK-LD-RT: "-L[[SYSROOT]]/lib"
51+
// CHECK-LD-RT: "-L[[SYSROOT]]/usr/lib"
52+
// CHECK-LD-RT: libclang_rt.x86_64.a" "-lgcc_s"
53+
// CHECK-LD-RT: "-lc"
54+
// CHECK-LD-RT: libclang_rt.x86_64.a" "-lgcc_s"
55+
//
56+
// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
57+
// RUN: --target=x86_64-unknown-linux \
58+
// RUN: --sysroot=%S/Inputs/basic_linux_tree \
59+
// RUN: --rtlib=libgcc \
60+
// RUN: | FileCheck --check-prefix=CHECK-LD-GCC %s
61+
// CHECK-LD-GCC-NOT: warning:
62+
// CHECK-LD-GCC: "{{.*}}ld{{(.exe)?}}" "--sysroot=[[SYSROOT:[^"]+]]"
63+
// CHECK-LD-GCC: "--eh-frame-hdr"
64+
// CHECK-LD-GCC: "-m" "elf_x86_64"
65+
// CHECK-LD-GCC: "-dynamic-linker"
66+
// CHECK-LD-GCC: "{{.*}}/usr/lib/gcc/x86_64-unknown-linux/4.6.0{{/|\\\\}}crtbegin.o"
67+
// CHECK-LD-GCC: "-L[[SYSROOT]]/usr/lib/gcc/x86_64-unknown-linux/4.6.0"
68+
// CHECK-LD-GCC: "-L[[SYSROOT]]/usr/lib/gcc/x86_64-unknown-linux/4.6.0/../../../../x86_64-unknown-linux/lib"
69+
// CHECK-LD-GCC: "-L[[SYSROOT]]/usr/lib/gcc/x86_64-unknown-linux/4.6.0/../../.."
70+
// CHECK-LD-GCC: "-L[[SYSROOT]]/lib"
71+
// CHECK-LD-GCC: "-L[[SYSROOT]]/usr/lib"
72+
// CHECK-LD-GCC "-lgcc" "--as-needed" "-lgcc_s" "--no-as-needed"
73+
// CHECK-LD-GCC: "-lc"
74+
// CHECK-LD-GCC: "-lgcc" "--as-needed" "-lgcc_s" "--no-as-needed"
75+
//
76+
// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
77+
// RUN: --target=x86_64-unknown-linux \
3878
// RUN: -static-libgcc \
3979
// RUN: --sysroot=%S/Inputs/basic_linux_tree \
4080
// RUN: | FileCheck --check-prefix=CHECK-LD-64-STATIC-LIBGCC %s

0 commit comments

Comments
 (0)
Please sign in to comment.