The latest clang and lld defaults to PIE binaries, but it doesn’t work for x32 currently (x86_64-linux-gnux32). The error is relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
, but ld.bfd works file, and also does passing in -no-pie
:
$ clang hello.c
ld.lld: error: cannot preempt symbol: __dso_handle
>>> defined in /usr/lib/gcc/x86_64-pc-linux-gnux32/11.3.0/crtbeginS.o
>>> referenced by /usr/lib/gcc/x86_64-pc-linux-gnux32/11.3.0/crtbeginS.o:(.data.rel.local+0x0)
ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in /usr/lib/gcc/x86_64-pc-linux-gnux32/11.3.0/crtbeginS.o
>>> referenced by /usr/lib/gcc/x86_64-pc-linux-gnux32/11.3.0/crtbeginS.o:(.fini_array+0x0)
ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in /usr/lib/gcc/x86_64-pc-linux-gnux32/11.3.0/crtbeginS.o
>>> referenced by /usr/lib/gcc/x86_64-pc-linux-gnux32/11.3.0/crtbeginS.o:(.init_array+0x0)
clang-15: error: linker command failed with exit code 1 (use -v to see invocation)
$ clang -fuse-ld=bfd hello.c
$ clang -no-pie hello.c
I did my testing inside gentoo stage3 container with image imported from here: Downloads – Gentoo Linux