llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.03k stars 11.97k forks source link

[lld] relocation R_AARCH64_ABS64 cannot be used against local symbol #99297

Open eastB233 opened 3 months ago

eastB233 commented 3 months ago

My test case is

.text

.global gf_vect_mad_neon
.type gf_vect_mad_neon, %function

x_const         .req    x0
q_tmp           .req    q1

gf_vect_mad_neon:
        ldr     x_const, =const_tbl
        ldr     q_tmp, [x_const]
        ret

.section .data
.balign 8
const_tbl:
        .dword 0x0000000000000000, 0x0000000000000000
        .dword 0xffffffffffffffff, 0xffffffffffffffff

It is okay when I use ld but fails with lld (I use LLVM 17)

$ clang test.S -c
$ clang test.o -fPIC -shared -o libtest.so
# It is okay

$ clang test.o -fPIC -shared -o libtest.so -fuse-ld=lld
ld.lld: error: relocation R_AARCH64_ABS64 cannot be used against local symbol; recompile with -fPIC
>>> defined in test.o
>>> referenced by test.o:(.text+0x10)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
eastB233 commented 3 months ago
$ clang test.o -fPIC -shared -o libtest.so
$ objdump -DR libtest.so

...
0000000000000518 <gf_vect_mad_neon>:
 518:   58000080        ldr     x0, 528 <gf_vect_mad_neon+0x10>
 51c:   3dc00001        ldr     q1, [x0]
 520:   d65f03c0        ret
 524:   00000000        udf     #0
 528:   00020018        .inst   0x00020018 ; undefined
                        528: R_AARCH64_RELATIVE *ABS*+0x20018
 52c:   00000000        udf     #0
...

It seems ld will transform R_AARCH64_ABS64 to R_AARCH64_RELATIVE in some way.

And I find https://reviews.llvm.org/D17265 may do the same thing.