Skip to content

Commit 81617f8

Browse files
authored
[Driver][RISCV] Forward --no-relax option to linker for RISC-V on *BS… (#83216)
…D, Fuchsia and Haiku Based on #76432
1 parent 57592e9 commit 81617f8

File tree

10 files changed

+51
-12
lines changed

10 files changed

+51
-12
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,7 @@
203203
// RELOCATABLE-NOT: "-l
204204
// RELOCATABLE-NOT: crt{{[^./\\]+}}.o
205205

206+
// Check that the -X and --no-relax flags are passed to the linker on riscv64
207+
// RUN: %clang --target=riscv64-unknown-freebsd -mno-relax -### %s 2>&1 \
208+
// RUN: | FileCheck -check-prefix=RISCV64-FLAGS %s
209+
// RISCV64-FLAGS: "-X" "--no-relax"

clang/test/Driver/fuchsia.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,8 @@
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 and --no-relax flags are passed to the linker on riscv64
297+
// RUN: %clang --target=riscv64-unknown-fuchsia -mno-relax -### %s 2>&1 \
298+
// RUN: | FileCheck -check-prefix=RISCV64-FLAGS %s
299+
// RISCV64-FLAGS: "-X" "--no-relax"

clang/test/Driver/haiku.c

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

79+
// Check that the -X and --no-relax flags are passed to the linker on riscv64
80+
// RUN: %clang --target=riscv64-unknown-haiku -mno-relax -### %s 2>&1 \
81+
// RUN: | FileCheck -check-prefix=RISCV64-FLAGS %s
82+
// RISCV64-FLAGS: "-X" "--no-relax"
83+
7984
// Check passing LTO flags to the linker
8085
// RUN: %clang --target=x86_64-unknown-haiku -flto -### %s 2>&1 \
8186
// RUN: | FileCheck -check-prefix=CHECK-LTO-FLAGS %s

clang/test/Driver/netbsd.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,10 @@
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 and --no-relax flags are passed to the linker on riscv
347+
// RUN: %clang --target=riscv32-unknown-netbsd -mno-relax -### %s 2>&1 \
348+
// RUN: | FileCheck -check-prefix=RISCV-FLAGS %s
349+
// RUN: %clang --target=riscv64-unknown-netbsd -mno-relax -### %s 2>&1 \
350+
// RUN: | FileCheck -check-prefix=RISCV-FLAGS %s
351+
// RISCV-FLAGS: "-X" "--no-relax"

clang/test/Driver/openbsd.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@
127127
// UNWIND-TABLES: "-funwind-tables=2"
128128
// NO-UNWIND-TABLES-NOT: "-funwind-tables=2"
129129

130-
// Check that the -X flag is passed to the linker on riscv64
131-
// RUN: %clang --target=riscv64-unknown-openbsd -### %s 2>&1 \
132-
// RUN: | FileCheck -check-prefix=CHECK-RISCV64-FLAGS %s
133-
// CHECK-RISCV64-FLAGS: "-X"
130+
// Check that the -X and --no-relax flags are passed to the linker on riscv64
131+
// RUN: %clang --target=riscv64-unknown-openbsd -mno-relax -### %s 2>&1 \
132+
// RUN: | FileCheck -check-prefix=RISCV64-FLAGS %s
133+
// RISCV64-FLAGS: "-X" "--no-relax"
134134

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

0 commit comments

Comments
 (0)