Skip to content

Commit 13648ea

Browse files
author
Andreu Carminati
committed
[Clang][RISCV] Forward --no-relax option to linker for RISC-V
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 ae0b263 commit 13648ea

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

clang/lib/Driver/ToolChains/BareMetal.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,9 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
443443

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

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

clang/lib/Driver/ToolChains/RISCVToolchain.cpp

Lines changed: 3 additions & 0 deletions
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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,22 @@
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+
218234
typedef __builtin_va_list va_list;
219235
typedef __SIZE_TYPE__ size_t;
220236
typedef __PTRDIFF_TYPE__ ptrdiff_t;

clang/test/Driver/riscv64-toolchain.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,22 @@
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+
174190
typedef __builtin_va_list va_list;
175191
typedef __SIZE_TYPE__ size_t;
176192
typedef __PTRDIFF_TYPE__ ptrdiff_t;

0 commit comments

Comments
 (0)