Skip to content

Commit 35cd96b

Browse files
committed
[Driver][RISCV] Forward --no-relax option to linker for RISC-V on *BSD, Fuchsia and Haiku
Based on llvm#76432
1 parent d5f77e1 commit 35cd96b

File tree

10 files changed

+76
-10
lines changed

10 files changed

+76
-10
lines changed

clang/lib/Driver/ToolChains/FreeBSD.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
133133
const char *LinkingOutput) const {
134134
const auto &ToolChain = static_cast<const FreeBSD &>(getToolChain());
135135
const Driver &D = ToolChain.getDriver();
136+
const llvm::Triple &Triple = ToolChain.getTriple();
136137
const llvm::Triple::ArchType Arch = ToolChain.getArch();
137138
const bool IsPIE =
138139
!Args.hasArg(options::OPT_shared) &&
@@ -165,8 +166,7 @@ void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
165166
CmdArgs.push_back("-dynamic-linker");
166167
CmdArgs.push_back("/libexec/ld-elf.so.1");
167168
}
168-
const llvm::Triple &T = ToolChain.getTriple();
169-
if (Arch == llvm::Triple::arm || T.isX86())
169+
if (Arch == llvm::Triple::arm || Triple.isX86())
170170
CmdArgs.push_back("--hash-style=both");
171171
CmdArgs.push_back("--enable-new-dtags");
172172
}
@@ -212,12 +212,17 @@ void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
212212
case llvm::Triple::riscv64:
213213
CmdArgs.push_back("-m");
214214
CmdArgs.push_back("elf64lriscv");
215-
CmdArgs.push_back("-X");
216215
break;
217216
default:
218217
break;
219218
}
220219

220+
if (Triple.isRISCV64()) {
221+
CmdArgs.push_back("-X");
222+
if (Args.hasArg(options::OPT_mno_relax))
223+
CmdArgs.push_back("--no-relax");
224+
}
225+
221226
if (Arg *A = Args.getLastArg(options::OPT_G)) {
222227
if (ToolChain.getTriple().isMIPS()) {
223228
StringRef v = A->getValue();

clang/lib/Driver/ToolChains/Fuchsia.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,11 @@ void fuchsia::Linker::ConstructJob(Compilation &C, const JobAction &JA,
119119
CmdArgs.push_back(Args.MakeArgString(Dyld));
120120
}
121121

122-
if (ToolChain.getArch() == llvm::Triple::riscv64)
122+
if (Triple.isRISCV64()) {
123123
CmdArgs.push_back("-X");
124+
if (Args.hasArg(options::OPT_mno_relax))
125+
CmdArgs.push_back("--no-relax");
126+
}
124127

125128
CmdArgs.push_back("-o");
126129
CmdArgs.push_back(Output.getFilename());

clang/lib/Driver/ToolChains/Haiku.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void haiku::Linker::ConstructJob(Compilation &C, const JobAction &JA,
2525
const char *LinkingOutput) const {
2626
const auto &ToolChain = static_cast<const Haiku &>(getToolChain());
2727
const Driver &D = ToolChain.getDriver();
28-
const llvm::Triple::ArchType Arch = ToolChain.getArch();
28+
const llvm::Triple &Triple = ToolChain.getTriple();
2929
const bool Static = Args.hasArg(options::OPT_static);
3030
const bool Shared = Args.hasArg(options::OPT_shared);
3131
ArgStringList CmdArgs;
@@ -61,8 +61,11 @@ void haiku::Linker::ConstructJob(Compilation &C, const JobAction &JA,
6161
if (!Shared)
6262
CmdArgs.push_back("--no-undefined");
6363

64-
if (Arch == llvm::Triple::riscv64)
64+
if (Triple.isRISCV64()) {
6565
CmdArgs.push_back("-X");
66+
if (Args.hasArg(options::OPT_mno_relax))
67+
CmdArgs.push_back("--no-relax");
68+
}
6669

6770
assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
6871
if (Output.isFilename()) {

clang/lib/Driver/ToolChains/NetBSD.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,11 @@ void netbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
240240
break;
241241
}
242242

243-
if (Triple.isRISCV())
243+
if (Triple.isRISCV()) {
244244
CmdArgs.push_back("-X");
245+
if (Args.hasArg(options::OPT_mno_relax))
246+
CmdArgs.push_back("--no-relax");
247+
}
245248

246249
assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
247250
if (Output.isFilename()) {

clang/lib/Driver/ToolChains/OpenBSD.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
111111
const char *LinkingOutput) const {
112112
const auto &ToolChain = static_cast<const OpenBSD &>(getToolChain());
113113
const Driver &D = ToolChain.getDriver();
114+
const llvm::Triple &Triple = ToolChain.getTriple();
114115
const llvm::Triple::ArchType Arch = ToolChain.getArch();
115116
const bool Static = Args.hasArg(options::OPT_static);
116117
const bool Shared = Args.hasArg(options::OPT_shared);
@@ -160,8 +161,11 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
160161
if (Nopie || Profiling)
161162
CmdArgs.push_back("-nopie");
162163

163-
if (Arch == llvm::Triple::riscv64)
164+
if (Triple.isRISCV64()) {
164165
CmdArgs.push_back("-X");
166+
if (Args.hasArg(options::OPT_mno_relax))
167+
CmdArgs.push_back("--no-relax");
168+
}
165169

166170
assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
167171
if (Output.isFilename()) {

clang/test/Driver/freebsd.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,12 @@
203203
// RELOCATABLE-NOT: "-l
204204
// RELOCATABLE-NOT: crt{{[^./\\]+}}.o
205205

206+
// Check that the -X flag is passed to the linker on riscv64
207+
// RUN: %clang --target=riscv64-unknown-freebsd -### %s 2>&1 \
208+
// RUN: | FileCheck -check-prefix=CHECK-RISCV64-FLAG-X %s
209+
// CHECK-RISCV64-FLAG-X: "-X"
210+
211+
// Check that the --no-relax flag is passed to the linker on riscv64
212+
// RUN: %clang --target=riscv64-unknown-freebsd -mno-relax -### %s 2>&1 \
213+
// RUN: | FileCheck -check-prefix=CHECK-RISCV64-FLAG-NO-RELAX %s
214+
// CHECK-RISCV64-FLAG-NO-RELAX: "--no-relax"

clang/test/Driver/fuchsia.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,13 @@
292292
// RUN: | FileCheck %s -check-prefix=CHECK-PROFRT-X86_64
293293
// CHECK-PROFRT-X86_64: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]"
294294
// CHECK-PROFRT-X86_64: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}x86_64-unknown-fuchsia{{/|\\\\}}libclang_rt.profile.a"
295+
296+
// Check that the -X flag is passed to the linker on riscv64
297+
// RUN: %clang --target=riscv64-unknown-fuchsia -### %s 2>&1 \
298+
// RUN: | FileCheck -check-prefix=CHECK-RISCV64-FLAG-X %s
299+
// CHECK-RISCV64-FLAG-X: "-X"
300+
301+
// Check that the --no-relax flag is passed to the linker on riscv64
302+
// RUN: %clang --target=riscv64-unknown-fuchsia -mno-relax -### %s 2>&1 \
303+
// RUN: | FileCheck -check-prefix=CHECK-RISCV64-FLAG-NO-RELAX %s
304+
// CHECK-RISCV64-FLAG-NO-RELAX: "--no-relax"

clang/test/Driver/haiku.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@
7676
// RUN: | FileCheck --check-prefix=CHECK-ARM-CPU %s
7777
// CHECK-ARM-CPU: "-target-cpu" "arm1176jzf-s"
7878

79+
// Check that the -X flag is passed to the linker on riscv64
80+
// RUN: %clang --target=riscv64-unknown-haiku -### %s 2>&1 \
81+
// RUN: | FileCheck -check-prefix=CHECK-RISCV64-FLAG-X %s
82+
// CHECK-RISCV64-FLAG-X: "-X"
83+
84+
// Check that the --no-relax flag is passed to the linker on riscv64
85+
// RUN: %clang --target=riscv64-unknown-haiku -mno-relax -### %s 2>&1 \
86+
// RUN: | FileCheck -check-prefix=CHECK-RISCV64-FLAG-NO-RELAX %s
87+
// CHECK-RISCV64-FLAG-NO-RELAX: "--no-relax"
88+
7989
// Check passing LTO flags to the linker
8090
// RUN: %clang --target=x86_64-unknown-haiku -flto -### %s 2>&1 \
8191
// RUN: | FileCheck -check-prefix=CHECK-LTO-FLAGS %s

clang/test/Driver/netbsd.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,17 @@
342342
// DRIVER-PASS-INCLUDES: "-cc1" {{.*}}"-resource-dir" "[[RESOURCE:[^"]+]]"
343343
// DRIVER-PASS-INCLUDES-SAME: "-internal-isystem" "[[RESOURCE]]{{/|\\\\}}include"
344344
// DRIVER-PASS-INCLUDES-SAME: {{^}} "-internal-externc-isystem" "{{.*}}/usr/include"
345+
346+
// Check that the -X flag is passed to the linker on riscv
347+
// RUN: %clang --target=riscv32-unknown-netbsd -### %s 2>&1 \
348+
// RUN: | FileCheck -check-prefix=CHECK-RISCV-FLAG-X %s
349+
// RUN: %clang --target=riscv64-unknown-netbsd -### %s 2>&1 \
350+
// RUN: | FileCheck -check-prefix=CHECK-RISCV-FLAG-X %s
351+
// CHECK-RISCV-FLAG-X: "-X"
352+
353+
// Check that the --no-relax flag is passed to the linker on riscv
354+
// RUN: %clang --target=riscv32-unknown-netbsd -mno-relax -### %s 2>&1 \
355+
// RUN: | FileCheck -check-prefix=CHECK-RISCV-FLAG-NO-RELAX %s
356+
// RUN: %clang --target=riscv64-unknown-netbsd -mno-relax -### %s 2>&1 \
357+
// RUN: | FileCheck -check-prefix=CHECK-RISCV-FLAG-NO-RELAX %s
358+
// CHECK-RISCV-FLAG-NO-RELAX: "--no-relax"

clang/test/Driver/openbsd.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,13 @@
129129

130130
// Check that the -X flag is passed to the linker on riscv64
131131
// RUN: %clang --target=riscv64-unknown-openbsd -### %s 2>&1 \
132-
// RUN: | FileCheck -check-prefix=CHECK-RISCV64-FLAGS %s
133-
// CHECK-RISCV64-FLAGS: "-X"
132+
// RUN: | FileCheck -check-prefix=CHECK-RISCV64-FLAG-X %s
133+
// CHECK-RISCV64-FLAG-X: "-X"
134+
135+
// Check that the --no-relax flag is passed to the linker on riscv64
136+
// RUN: %clang --target=riscv64-unknown-openbsd -mno-relax -### %s 2>&1 \
137+
// RUN: | FileCheck -check-prefix=CHECK-RISCV64-FLAG-NO-RELAX %s
138+
// CHECK-RISCV64-FLAG-NO-RELAX: "--no-relax"
134139

135140
// Check passing LTO flags to the linker
136141
// RUN: %clang --target=amd64-unknown-openbsd -flto -### %s 2>&1 \

0 commit comments

Comments
 (0)