llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

LLD is unable to link musl for x32

Open alexrp opened this issue 1 year ago • 8 comments

Repro:

$ git clone https://git.musl-libc.org/git/musl
$ cd musl
$ ./configure --prefix=$PWD/build/x32 --target=x86_64-linux-muslx32 AR="llvm-ar" CC="clang" RANLIB="llvm-ranlib" CFLAGS="--target=x86_64-linux-muslx32 -fuse-ld=lld"
$ make -j$(nproc)

You should see link errors like this:

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/exit/atexit.lo
>>> referenced by atexit.c
>>>               obj/src/exit/atexit.lo:(__atexit_lockptr)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/linux/membarrier.lo
>>> referenced by membarrier.c
>>>               obj/src/linux/membarrier.lo:(.data.rel.ro..L__const.__membarrier.sa+0x0)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/c_locale.lo
>>> referenced by c_locale.c
>>>               obj/src/locale/c_locale.lo:(__c_dot_utf8)

ld.lld: error: relocation R_X86_64_32 cannot be used against symbol '__c_dot_utf8'; recompile with -fPIC
>>> defined in obj/src/locale/c_locale.lo
>>> referenced by c_locale.c
>>>               obj/src/locale/c_locale.lo:(__c_dot_utf8_locale)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/dcngettext.lo
>>> referenced by dcngettext.c
>>>               obj/src/locale/dcngettext.lo:(__gettext_lockptr)

ld.lld: error: relocation R_X86_64_32 cannot be used against symbol '__locale_lock'; recompile with -fPIC
>>> defined in obj/src/locale/locale_map.lo
>>> referenced by locale_map.c
>>>               obj/src/locale/locale_map.lo:(__locale_lockptr)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/localeconv.lo
>>> referenced by localeconv.c
>>>               obj/src/locale/localeconv.lo:(posix_lconv)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/localeconv.lo
>>> referenced by localeconv.c
>>>               obj/src/locale/localeconv.lo:(posix_lconv)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/localeconv.lo
>>> referenced by localeconv.c
>>>               obj/src/locale/localeconv.lo:(posix_lconv)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/malloc/lite_malloc.lo
>>> referenced by lite_malloc.c
>>>               obj/src/malloc/lite_malloc.lo:(__bump_lockptr)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/localeconv.lo
>>> referenced by localeconv.c
>>>               obj/src/locale/localeconv.lo:(posix_lconv)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/localeconv.lo
>>> referenced by localeconv.c
>>>               obj/src/locale/localeconv.lo:(posix_lconv)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/localeconv.lo
>>> referenced by localeconv.c
>>>               obj/src/locale/localeconv.lo:(posix_lconv)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/localeconv.lo
>>> referenced by localeconv.c
>>>               obj/src/locale/localeconv.lo:(posix_lconv)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/localeconv.lo
>>> referenced by localeconv.c
>>>               obj/src/locale/localeconv.lo:(posix_lconv)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/localeconv.lo
>>> referenced by localeconv.c
>>>               obj/src/locale/localeconv.lo:(posix_lconv)

In case it's helpful:

❯ llvm-readelf -sr obj/src/locale/localeconv.lo

Relocation section '.rela.text.localeconv' at offset 0x10c contains 1 entries:
 Offset     Info    Type                Sym. Value  Symbol's Name + Addend
00000002  00000402 R_X86_64_PC32          00000000   .data.rel.ro.posix_lconv - 4

Relocation section '.rela.data.rel.ro.posix_lconv' at offset 0x118 contains 10 entries:
 Offset     Info    Type                Sym. Value  Symbol's Name + Addend
00000000  0000030a R_X86_64_32            00000000   .rodata.str1.1 + 0
00000004  0000030a R_X86_64_32            00000000   .rodata.str1.1 + 2
00000008  0000030a R_X86_64_32            00000000   .rodata.str1.1 + 2
0000000c  0000030a R_X86_64_32            00000000   .rodata.str1.1 + 2
00000010  0000030a R_X86_64_32            00000000   .rodata.str1.1 + 2
00000014  0000030a R_X86_64_32            00000000   .rodata.str1.1 + 2
00000018  0000030a R_X86_64_32            00000000   .rodata.str1.1 + 2
0000001c  0000030a R_X86_64_32            00000000   .rodata.str1.1 + 2
00000020  0000030a R_X86_64_32            00000000   .rodata.str1.1 + 2
00000024  0000030a R_X86_64_32            00000000   .rodata.str1.1 + 2

Symbol table '.symtab' contains 6 entries:
   Num:    Value  Size Type    Bind   Vis       Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT   UND
     1: 00000000     0 FILE    LOCAL  DEFAULT   ABS localeconv.c
     2: 00000000    56 OBJECT  LOCAL  DEFAULT     6 posix_lconv
     3: 00000000     0 SECTION LOCAL  DEFAULT     5 .rodata.str1.1
     4: 00000000     0 SECTION LOCAL  DEFAULT     6 .data.rel.ro.posix_lconv
     5: 00000000     7 FUNC    GLOBAL DEFAULT     3 localeconv

I'm labeling this as an LLD bug, but for all I know it could be an LLVM backend bug too; please relabel if needed.

alexrp avatar May 18 '25 18:05 alexrp

@llvm/issue-subscribers-lld-elf

Author: Alex Rønne Petersen (alexrp)

Repro:
$ git clone https://git.musl-libc.org/git/musl
$ cd musl
$ ./configure --prefix=$PWD/build/x32 --target=x86_64-linux-muslx32 AR="llvm-ar" CC="clang" RANLIB="llvm-ranlib" CFLAGS="--target=x86_64-linux-muslx32 -fuse-ld=lld"
$ make -j$(nproc)

You should see link errors like this:

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/exit/atexit.lo
>>> referenced by atexit.c
>>>               obj/src/exit/atexit.lo:(__atexit_lockptr)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/linux/membarrier.lo
>>> referenced by membarrier.c
>>>               obj/src/linux/membarrier.lo:(.data.rel.ro..L__const.__membarrier.sa+0x0)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/c_locale.lo
>>> referenced by c_locale.c
>>>               obj/src/locale/c_locale.lo:(__c_dot_utf8)

ld.lld: error: relocation R_X86_64_32 cannot be used against symbol '__c_dot_utf8'; recompile with -fPIC
>>> defined in obj/src/locale/c_locale.lo
>>> referenced by c_locale.c
>>>               obj/src/locale/c_locale.lo:(__c_dot_utf8_locale)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/dcngettext.lo
>>> referenced by dcngettext.c
>>>               obj/src/locale/dcngettext.lo:(__gettext_lockptr)

ld.lld: error: relocation R_X86_64_32 cannot be used against symbol '__locale_lock'; recompile with -fPIC
>>> defined in obj/src/locale/locale_map.lo
>>> referenced by locale_map.c
>>>               obj/src/locale/locale_map.lo:(__locale_lockptr)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/localeconv.lo
>>> referenced by localeconv.c
>>>               obj/src/locale/localeconv.lo:(posix_lconv)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/localeconv.lo
>>> referenced by localeconv.c
>>>               obj/src/locale/localeconv.lo:(posix_lconv)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/localeconv.lo
>>> referenced by localeconv.c
>>>               obj/src/locale/localeconv.lo:(posix_lconv)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/malloc/lite_malloc.lo
>>> referenced by lite_malloc.c
>>>               obj/src/malloc/lite_malloc.lo:(__bump_lockptr)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/localeconv.lo
>>> referenced by localeconv.c
>>>               obj/src/locale/localeconv.lo:(posix_lconv)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/localeconv.lo
>>> referenced by localeconv.c
>>>               obj/src/locale/localeconv.lo:(posix_lconv)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/localeconv.lo
>>> referenced by localeconv.c
>>>               obj/src/locale/localeconv.lo:(posix_lconv)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/localeconv.lo
>>> referenced by localeconv.c
>>>               obj/src/locale/localeconv.lo:(posix_lconv)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/localeconv.lo
>>> referenced by localeconv.c
>>>               obj/src/locale/localeconv.lo:(posix_lconv)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in obj/src/locale/localeconv.lo
>>> referenced by localeconv.c
>>>               obj/src/locale/localeconv.lo:(posix_lconv)

In case it's helpful:

❯ llvm-readelf -sr obj/src/locale/localeconv.lo

Relocation section '.rela.text.localeconv' at offset 0x10c contains 1 entries:
 Offset     Info    Type                Sym. Value  Symbol's Name + Addend
00000002  00000402 R_X86_64_PC32          00000000   .data.rel.ro.posix_lconv - 4

Relocation section '.rela.data.rel.ro.posix_lconv' at offset 0x118 contains 10 entries:
 Offset     Info    Type                Sym. Value  Symbol's Name + Addend
00000000  0000030a R_X86_64_32            00000000   .rodata.str1.1 + 0
00000004  0000030a R_X86_64_32            00000000   .rodata.str1.1 + 2
00000008  0000030a R_X86_64_32            00000000   .rodata.str1.1 + 2
0000000c  0000030a R_X86_64_32            00000000   .rodata.str1.1 + 2
00000010  0000030a R_X86_64_32            00000000   .rodata.str1.1 + 2
00000014  0000030a R_X86_64_32            00000000   .rodata.str1.1 + 2
00000018  0000030a R_X86_64_32            00000000   .rodata.str1.1 + 2
0000001c  0000030a R_X86_64_32            00000000   .rodata.str1.1 + 2
00000020  0000030a R_X86_64_32            00000000   .rodata.str1.1 + 2
00000024  0000030a R_X86_64_32            00000000   .rodata.str1.1 + 2

Symbol table '.symtab' contains 6 entries:
   Num:    Value  Size Type    Bind   Vis       Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT   UND
     1: 00000000     0 FILE    LOCAL  DEFAULT   ABS localeconv.c
     2: 00000000    56 OBJECT  LOCAL  DEFAULT     6 posix_lconv
     3: 00000000     0 SECTION LOCAL  DEFAULT     5 .rodata.str1.1
     4: 00000000     0 SECTION LOCAL  DEFAULT     6 .data.rel.ro.posix_lconv
     5: 00000000     7 FUNC    GLOBAL DEFAULT     3 localeconv

I'm labeling this as an LLD bug, but for all I know it could be an LLVM backend bug too; please relabel if needed.

llvmbot avatar May 18 '25 18:05 llvmbot

I faced a similar issue. Compiling an example C++ program for x32 ABI using clang and lld results into same error. With default ld compilation works fine, so it's likely lld-specific issue.

Even compiling LLVM library itself for x32 ABI using ld.lld as linker results into a lot of linking errors.

Panzerschrek avatar Aug 06 '25 13:08 Panzerschrek

No a lot of love shown to x32. Can you see if this fixes the issue with musl needed some math functions from gcc?

diff --git a/lld/ELF/Arch/X86_64.cpp b/lld/ELF/Arch/X86_64.cpp
index 4ee6ea1cb618..613ccefec543 100644
--- a/lld/ELF/Arch/X86_64.cpp
+++ b/lld/ELF/Arch/X86_64.cpp
@@ -438,7 +438,7 @@ void X86_64::writePlt(uint8_t *buf, const Symbol &sym,
 
 RelType X86_64::getDynRel(RelType type) const {
   if (type == R_X86_64_64 || type == R_X86_64_PC64 || type == R_X86_64_SIZE32 ||
-      type == R_X86_64_SIZE64)
+      type == R_X86_64_SIZE64 || type == R_X86_64_32)
     return type;
   return R_X86_64_NONE;
 }

daunabomba avatar Feb 20 '26 01:02 daunabomba

It's much more complex than adding R_X86_64_32 to getDynRel. We need to figure out the glibc and GNU ld behavior https://sourceware.org/pipermail/binutils/2011-August/073890.html We probably also need R_X86_64_RELATIVE64.

MaskRay avatar Feb 23 '26 08:02 MaskRay

/cherry-pick f16f97419d2fa97888512ad9429e21fa883cd22b

alexrp avatar Mar 07 '26 04:03 alexrp

Failed to cherry-pick: f16f97419d2fa97888512ad9429e21fa883cd22b

https://github.com/llvm/llvm-project/actions/runs/22792030367

Please manually backport the fix and push it to your github fork. Once this is done, please create a pull request

llvmbot avatar Mar 07 '26 04:03 llvmbot

/cherry-pick 13838efa3c8f8861ae98de9f0deec2c6c13d0728

alexrp avatar Mar 07 '26 04:03 alexrp

Failed to cherry-pick: 13838efa3c8f8861ae98de9f0deec2c6c13d0728

https://github.com/llvm/llvm-project/actions/runs/22792174739

Please manually backport the fix and push it to your github fork. Once this is done, please create a pull request

llvmbot avatar Mar 07 '26 04:03 llvmbot