I’m trying to print a string through a system call, use macros to refer to string length. When I assemble this code with clang, it complains that the macros ‘LEN’ must be a 32-bit immediate. The compilation command used is as follows:
clang --target=riscv32 -march=rv32imafcv -c -mno-relax hello.s -o hello.o
The assembler source code is shown below:
.option norelax
.section .rodata
.align 2
msg: .ascii "hello world!\n"
.equ LEN, .-msg
.type msg, object
.size msg, LEN
.text
.globl _start
.align 4
_start:
li a0, 1
la a1, msg
li a2, LEN
li a7, 64 /* write */
ecall
_exit:
li a0, 0
li a7, 93
ecall
It seems to me that the ‘LEN’ should be an immediate with the value 13, but clang doesn’t seem to think so:
hello.s:14:21: error: immediate must be an integer in the range [-2147483648, 4294967295]
li a2, LEN
^
Furthermore, If I change this line as follows,
li a2, 13
the program works:
$ld.lld --no-relax hello.o -o hello
$qemu-riscv32 hello
hello world!
I don’t know why the macro ‘LEN’ can’t be identified correctly by clang, and the clang version is 14.0.6. Any suggestions would be helpful, thanks!