Skip to content

Commit be8e462

Browse files
author
Andreu Carminati
authored
[Clang][RISCV] Forward --no-relax option to linker for RISC-V (#76432)
In the case of -mno-relax option. Otherwise, we cannot prevent relaxation if we split compilation and linking using Clang driver. One can consider the following use case: clang [...] -c -o myobject.o (just compile) clang [...] myobject.o -o myobject.elf -mno-relax (linking) In this case, myobject.elf will be relaxed, the -mno-relax will be silently ignored.
1 parent 45f883e commit be8e462

File tree

6 files changed

+87
-1
lines changed

6 files changed

+87
-1
lines changed

clang/lib/Driver/ToolChains/BareMetal.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,9 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
442442

443443
CmdArgs.push_back("-Bstatic");
444444

445+
if (TC.getTriple().isRISCV() && Args.hasArg(options::OPT_mno_relax))
446+
CmdArgs.push_back("--no-relax");
447+
445448
if (Triple.isARM() || Triple.isThumb()) {
446449
bool IsBigEndian = arm::isARMBigEndian(Triple, Args);
447450
if (IsBigEndian)

clang/lib/Driver/ToolChains/Gnu.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,12 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
423423
D.Diag(diag::err_target_unknown_triple) << Triple.str();
424424
return;
425425
}
426-
if (Triple.isRISCV())
426+
427+
if (Triple.isRISCV()) {
427428
CmdArgs.push_back("-X");
429+
if (Args.hasArg(options::OPT_mno_relax))
430+
CmdArgs.push_back("--no-relax");
431+
}
428432

429433
const bool IsShared = Args.hasArg(options::OPT_shared);
430434
if (IsShared)

clang/lib/Driver/ToolChains/RISCVToolchain.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ void RISCV::Linker::ConstructJob(Compilation &C, const JobAction &JA,
156156
if (!D.SysRoot.empty())
157157
CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
158158

159+
if (Args.hasArg(options::OPT_mno_relax))
160+
CmdArgs.push_back("--no-relax");
161+
159162
bool IsRV64 = ToolChain.getArch() == llvm::Triple::riscv64;
160163
CmdArgs.push_back("-m");
161164
if (IsRV64) {

clang/test/Driver/baremetal.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -460,3 +460,15 @@
460460
// RUN: | FileCheck --check-prefix=CHECK-CLANGRT-ARCH %s
461461
// CHECK-CLANGRT-ARCH: "-lclang_rt.builtins-armv6m"
462462
// CHECK-CLANGRT-ARCH-NOT: "-lclang_rt.builtins"
463+
464+
// Check that "--no-relax" is forwarded to the linker for RISC-V.
465+
// RUN: %clang %s -### 2>&1 --target=riscv64-unknown-elf -nostdinc -mno-relax \
466+
// RUN: --sysroot=%S/Inputs/basic_riscv64_tree/riscv64-unknown-elf \
467+
// RUN: | FileCheck --check-prefix=CHECK-RV64-NORELAX %s
468+
// CHECK-RV64-NORELAX: "--no-relax"
469+
470+
// Check that "--no-relax" is not forwarded to the linker for RISC-V.
471+
// RUN: %clang %s -### 2>&1 --target=riscv64-unknown-elf -nostdinc \
472+
// RUN: --sysroot=%S/Inputs/basic_riscv64_tree/riscv64-unknown-elf \
473+
// RUN: | FileCheck --check-prefix=CHECK-RV64-RELAX %s
474+
// CHECK-RV64-RELAX-NOT: "--no-relax"

clang/test/Driver/riscv32-toolchain.c

+32
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,38 @@
215215

216216
// RUN: %clang --target=riscv32 %s -emit-llvm -S -o - | FileCheck %s
217217

218+
// Check that "--no-relax" is forwarded to the linker for RISC-V (RISCVToolchain.cpp).
219+
// RUN: env "PATH=" %clang %s -### 2>&1 -mno-relax \
220+
// RUN: --target=riscv32-unknown-elf --rtlib=platform --unwindlib=platform --sysroot= \
221+
// RUN: -march=rv32imac -mabi=lp32\
222+
// RUN: --gcc-toolchain=%S/Inputs/multilib_riscv_elf_sdk 2>&1 \
223+
// RUN: | FileCheck --check-prefix=CHECK-RV32-NORELAX %s
224+
// CHECK-RV32-NORELAX: "--no-relax"
225+
226+
// Check that "--no-relax" is not forwarded to the linker for RISC-V (RISCVToolchain.cpp).
227+
// RUN:env "PATH=" %clang %s -### 2>&1 \
228+
// RUN: --target=riscv32-unknown-elf --rtlib=platform --unwindlib=platform --sysroot= \
229+
// RUN: -march=rv32imac -mabi=lp32\
230+
// RUN: --gcc-toolchain=%S/Inputs/multilib_riscv_elf_sdk 2>&1 \
231+
// RUN: | FileCheck --check-prefix=CHECK-RV32-RELAX %s
232+
// CHECK-RV32-RELAX-NOT: "--no-relax"
233+
234+
// Check that "--no-relax" is forwarded to the linker for RISC-V (Gnu.cpp).
235+
// RUN: env "PATH=" %clang -### %s -fuse-ld=ld -no-pie -mno-relax \
236+
// RUN: --target=riscv32-unknown-linux-gnu --rtlib=platform --unwindlib=platform -mabi=ilp32 \
237+
// RUN: --gcc-toolchain=%S/Inputs/multilib_riscv_linux_sdk \
238+
// RUN: --sysroot=%S/Inputs/multilib_riscv_linux_sdk/sysroot 2>&1 \
239+
// RUN: | FileCheck -check-prefix=CHECK-RV32-GNU-NORELAX %s
240+
// CHECK-RV32-GNU-NORELAX: "--no-relax"
241+
242+
// Check that "--no-relax" is not forwarded to the linker for RISC-V (Gnu.cpp).
243+
// RUN: env "PATH=" %clang -### %s -fuse-ld=ld -no-pie \
244+
// RUN: --target=riscv32-unknown-linux-gnu --rtlib=platform --unwindlib=platform -mabi=ilp32 \
245+
// RUN: --gcc-toolchain=%S/Inputs/multilib_riscv_linux_sdk \
246+
// RUN: --sysroot=%S/Inputs/multilib_riscv_linux_sdk/sysroot 2>&1 \
247+
// RUN: | FileCheck -check-prefix=CHECK-RV32-GNU-RELAX %s
248+
// CHECK-RV32-GNU-RELAX-NOT: "--no-relax"
249+
218250
typedef __builtin_va_list va_list;
219251
typedef __SIZE_TYPE__ size_t;
220252
typedef __PTRDIFF_TYPE__ ptrdiff_t;

clang/test/Driver/riscv64-toolchain.c

+32
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,38 @@
171171

172172
// RUN: %clang --target=riscv64 %s -emit-llvm -S -o - | FileCheck %s
173173

174+
// Check that "--no-relax" is forwarded to the linker for RISC-V (RISCVToolchain.cpp).
175+
// RUN: env "PATH=" %clang %s -### 2>&1 -mno-relax \
176+
// RUN: --target=riscv64-unknown-elf --rtlib=platform --unwindlib=platform --sysroot= \
177+
// RUN: -march=rv64imac -mabi=lp64\
178+
// RUN: --gcc-toolchain=%S/Inputs/multilib_riscv_elf_sdk 2>&1 \
179+
// RUN: | FileCheck --check-prefix=CHECK-RV64-NORELAX %s
180+
// CHECK-RV64-NORELAX: "--no-relax"
181+
182+
// Check that "--no-relax" is not forwarded to the linker for RISC-V (RISCVToolchain.cpp).
183+
// RUN:env "PATH=" %clang %s -### 2>&1 \
184+
// RUN: --target=riscv64-unknown-elf --rtlib=platform --unwindlib=platform --sysroot= \
185+
// RUN: -march=rv64imac -mabi=lp64\
186+
// RUN: --gcc-toolchain=%S/Inputs/multilib_riscv_elf_sdk 2>&1 \
187+
// RUN: | FileCheck --check-prefix=CHECK-RV64-RELAX %s
188+
// CHECK-RV64-RELAX-NOT: "--no-relax"
189+
190+
// Check that "--no-relax" is forwarded to the linker for RISC-V (Gnu.cpp).
191+
// RUN: env "PATH=" %clang -### %s -fuse-ld=ld -no-pie -mno-relax \
192+
// RUN: --target=riscv64-unknown-linux-gnu --rtlib=platform --unwindlib=platform -mabi=lp64 \
193+
// RUN: --gcc-toolchain=%S/Inputs/multilib_riscv_linux_sdk \
194+
// RUN: --sysroot=%S/Inputs/multilib_riscv_linux_sdk/sysroot 2>&1 \
195+
// RUN: | FileCheck -check-prefix=CHECK-RV64-GNU-NORELAX %s
196+
// CHECK-RV64-GNU-NORELAX: "--no-relax"
197+
198+
// Check that "--no-relax" is not forwarded to the linker for RISC-V (Gnu.cpp).
199+
// RUN: env "PATH=" %clang -### %s -fuse-ld=ld -no-pie \
200+
// RUN: --target=riscv64-unknown-linux-gnu --rtlib=platform --unwindlib=platform -mabi=lp64 \
201+
// RUN: --gcc-toolchain=%S/Inputs/multilib_riscv_linux_sdk \
202+
// RUN: --sysroot=%S/Inputs/multilib_riscv_linux_sdk/sysroot 2>&1 \
203+
// RUN: | FileCheck -check-prefix=CHECK-RV64-GNU-RELAX %s
204+
// CHECK-RV64-GNU-RELAX-NOT: "--no-relax"
205+
174206
typedef __builtin_va_list va_list;
175207
typedef __SIZE_TYPE__ size_t;
176208
typedef __PTRDIFF_TYPE__ ptrdiff_t;

0 commit comments

Comments
 (0)