How to resolve the issue with the 'R_RISCV_ALIGN' relocation in clang14.0.6?

I’m compiling tests using clang and glibc, but the linker is reporting an error with ‘R_RISCV_ALIGN’ relocation in ‘crt1.o’ . The source code of the program “test.cpp” is as follows:

int main(int argc, char *argv[]){
  return 0;

The compilation parameters are as follows:

clang --target=riscv64-unknown-linux-gnu -march=rv64imafcv \
--sysroot=${path}/glibc64_host/sysroot \
--gcc-toolchain=${path}/glibc64_host \
-g -fuse-ld=lld -O2 test.cpp -o test 

The ld.lld output is as follows:

ld.lld: error: ${path}/glibc64_host/sysroot/usr/lib/crt1.o:(.text+0x0): \
relocation R_RISCV_ALIGN requires unimplemented linker relaxation; \
recompile with -mno-relax

I understand that this is related to linker relaxation optimization, so I tried to disable linker relaxation optimization using ‘-mno-relax’. But even though I compiled with ‘-mno-relax’ parameter, the linker still gave me the same error. I also tried using ‘-Wl’ option to pass ‘–no-relax’ directly to the linker, but it didn’t work well.

clang --target=riscv64-unknown-linux-gnu -march=rv64imafcv \
--sysroot=${path}/glibc64_host/sysroot \
--gcc-toolchain=${path}/glibc64_host \
-g -fuse-ld=lld -mno-relax -O2 test.cpp -o test

After looking up some information on the Internet, I realized that this might be due to the clang version as well, so I compiled the program with clang-15, and surprisingly it worked!

Do I have to update the clang version? What should I do if I want to compile with clang-14.0.6? Any suggestions would be helpful, thanks!

The problem isn’t Clang, it’s LLD (note the -fuse-ld=lld). Prior to LLD 15, linker relaxations were not supported, and so code using them (specifically R_RISCV_ALIGN needs support if used by the compiler; the others are just optional optimisations) could not be linked. This applied to all code, not just the code you compiled, and thus included the various system libc+crt bits; as you can see here it’s complaining about the system crt1.o, which would need to be recompiled with -mno-relax in order to work. Since LLD 15, linker relaxations are supported, and so there are no issues using the standard system libraries.

1 Like

Thanks, I’ll try to recompiler glibc with ‘-mno-relax’.

Why can’t you just use LLVM 15 (or, once we’re past the bumpier first release or two, 16)?

You’re right, I’m just worried if switching to a new version will introduce some incompatible features like intrinsics that I added earlier. Maybe I should use the new version of clang to fix the problem once and for all.