| Issue |
110186
|
| Summary |
`ld.lld` fails to link librararies/executables with a name longer than 8 characters targeting `aarch64-w64-mingw32`
|
| Labels |
lld
|
| Assignees |
|
| Reporter |
Martchus
|
The title of this issue sounds strange, but I somehow managed to get myself into this problem:
```
$ /usr/bin/clang main.c -v -fuse-ld=lld -target aarch64-w64-mingw32 -Xclang -triple -Xclang aarch64-w64-mingw32 -rtlib=compiler-rt -shared -o 123456789
clang version 18.1.8
Target: aarch64-w64-windows-gnu
Thread model: posix
InstalledDir: /usr/bin
"/usr/bin/clang-18" -cc1 -triple aarch64-w64-windows-gnu -emit-obj -mrelax-all -dumpdir 123456789- -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.c -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -mms-bitfields -funwind-tables=2 -fno-use-init-array -target-cpu generic -target-feature +v8a -target-feature +fp-armv8 -target-feature +neon -target-abi aapcs -debugger-tuning=gdb -fdebug-compilation-dir=/run/media/devel/projects/c++/misc -v -fcoverage-compilation-dir=/run/media/devel/projects/c++/misc -resource-dir /usr/lib/clang/18 -internal-isystem /usr/lib/clang/18/include -internal-isystem /usr/aarch64-w64-mingw32/include -internal-isystem /usr/aarch64-w64-mingw32/usr/include -ferror-limit 19 -fno-use-cxa-atexit -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -exception-model=seh -fcolor-diagnostics -triple aarch64-w64-mingw32 -faddrsig -o /tmp/main-9ea098.o -x c main.c
clang -cc1 version 18.1.8 based upon LLVM 18.1.8 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/usr/aarch64-w64-mingw32/usr/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/clang/18/include
/usr/aarch64-w64-mingw32/include
End of search list.
"/usr/bin/ld.lld" -m arm64pe --shared -Bdynamic -e DllMainCRTStartup --enable-auto-image-base -o 123456789.exe /usr/aarch64-w64-mingw32/lib/dllcrt2.o /usr/aarch64-w64-mingw32/lib/crtbegin.o -L/usr/aarch64-w64-mingw32/lib -L/usr/aarch64-w64-mingw32/mingw/lib -L/usr/lib/clang/18/lib/windows /tmp/main-9ea098.o -lmingw32 /usr/lib/clang/18/lib/windows/libclang_rt.builtins-aarch64.a -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 /usr/lib/clang/18/lib/windows/libclang_rt.builtins-aarch64.a -lmoldname -lmingwex -lmsvcrt -lkernel32 /usr/aarch64-w64-mingw32/lib/crtend.o
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
clang: error: linker command failed with exit code 1 (use -v to see invocation)
```
With `main.c`:
```
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello world!");
return 0;
}
```
When removing the `9` (so the library name becomes just `12345678`) the program compiles and links. Not sure whether it runs as I am not sure how to test this (on my x86_64 GNU/Linux system).
---
This is likely a problem caused by me doing something stupid when setting up the toolchain. I am currently trying to use [LLVM](https://gitlab.archlinux.org/archlinux/packaging/packages/llvm/-/blob/main/PKGBUILD?ref_type=heads)/[clang](https://gitlab.archlinux.org/archlinux/packaging/packages/clang/-/blob/main/PKGBUILD?ref_type=heads)/[lld](https://gitlab.archlinux.org/archlinux/packaging/packages/lld/-/blob/main/PKGBUILD?ref_type=heads) as provided by Arch Linux to cross compile for `aarch64-w64-mingw32`. For this I built [binutils](https://github.com/Martchus/PKGBUILDs/blob/master/binutils/mingw-w64-clang-aarch64/PKGBUILD), mingw-w64 [headers](https://github.com/Martchus/PKGBUILDs/blob/master/headers/mingw-w64-clang-aarch64/PKGBUILD) and [crt](https://github.com/Martchus/PKGBUILDs/blob/master/crt/mingw-w64-clang-aarch64/PKGBUILD) and the [compiler runtime for the aarch64-w64-mingw32 target](https://github.com/Martchus/PKGBUILDs/blob/master/compiler-rt/mingw-w64-clang-aarch64/PKGBUILD). You can click the links to see what versions I built and how I invoked the builds exactly. Then I tried to build [libc++](https://github.com/Martchus/PKGBUILDs/blob/master/libc++/mingw-w64-clang-aarch64/PKGBUILD). It compiled until the linking of `libunwind.dll` - which has a name longer than 8 characters:
```
FAILED: lib/libunwind.dll lib/libunwind.dll.a
: && /usr/lib/ccache/bin/clang -rtlib=compiler-rt -fuse-ld=lld -mguard=cf -target aarch64-w64-mingw32 -Xclang -triple -Xclang aarch64-w64-mingw32 -D_FORTIFY_SOURCE=3 -D_GLIBCXX_ASSERTIONS -O2 -pipe -fno-plt -fexceptions --param=ssp-buffer-size=4 -Wformat -Werror=format-security -rtlib=compiler-rt -fuse-ld=lld -mguard=cf -target aarch64-w64-mingw32 -Xclang -triple -Xclang aarch64-w64-mingw32 -D_FORTIFY_SOURCE=3 -D_GLIBCXX_ASSERTIONS -O2 -pipe -fno-plt -fexceptions --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -ffunction-sections -fdata-sections -O3 -DNDEBUG -Wl,-O1,--sort-common,--as-needed -fstack-protector -Wl,-O1,--sort-common,--as-needed -fstack-protector -rtlib=compiler-rt -nostdlib++ --unwindlib=none -shared -o lib/libunwind.dll -Wl,--out-implib,lib/libunwind.dll.a -Wl,--major-image-version,1,--minor-image-version,0 libunwind/src/CMakeFiles/unwind_shared_objects.dir/libunwind.cpp.obj libunwind/src/CMakeFiles/unwind_shared_objects.dir/Unwind-EHABI.cpp.obj libunwind/src/CMakeFiles/unwind_shared_objects.dir/Unwind-seh.cpp.obj libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindLevel1.c.obj libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindLevel1-gcc-ext.c.obj libunwind/src/CMakeFiles/unwind_shared_objects.dir/Unwind-sjlj.c.obj libunwind/src/CMakeFiles/unwind_shared_objects.dir/Unwind-wasm.c.obj libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindRegistersRestore.S.obj libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindRegistersSave.S.obj -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && :
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: misaligned ldr/str offset
ld.lld: error: too many errors emitted, stopping now (use --error-limit=0 to see all errors)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
```
I tried to invoke the command manually (outside of CMake) and could reproduce the problem. It also didn't work without `ccache`. I tried to strip down the command a lot. Removing none of the many options helped. Eventually I noticed that all I need to do to get passed this error is to make the library name shorter. The following command worked:
```
/usr/bin/clang -rtlib=compiler-rt -fuse-ld=lld -mguard=cf -target aarch64-w64-mingw32 -Xclang -triple -Xclang aarch64-w64-mingw32 -D_FORTIFY_SOURCE=3 -D_GLIBCXX_ASSERTIONS -O2 -pipe -fno-plt -fexceptions --param=ssp-buffer-size=4 -Wformat -Werror=format-security -rtlib=compiler-rt -fuse-ld=lld -mguard=cf -target aarch64-w64-mingw32 -Xclang -triple -Xclang aarch64-w64-mingw32 -D_FORTIFY_SOURCE=3 -D_GLIBCXX_ASSERTIONS -O2 -pipe -fno-plt -fexceptions --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -ffunction-sections -fdata-sections -O3 -DNDEBUG -Wl,-O1,--sort-common,--as-needed -fstack-protector -Wl,-O1,--sort-common,--as-needed -fstack-protector -rtlib=compiler-rt -nostdlib++ --unwindlib=none -shared -o lib/libfoo.dll -Wl,--out-implib,lib/libfoo.dll.a -Wl,--major-image-version,1,--minor-image-version,0 libunwind/src/CMakeFiles/unwind_shared_objects.dir/libunwind.cpp.obj libunwind/src/CMakeFiles/unwind_shared_objects.dir/Unwind-EHABI.cpp.obj libunwind/src/CMakeFiles/unwind_shared_objects.dir/Unwind-seh.cpp.obj libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindLevel1.c.obj libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindLevel1-gcc-ext.c.obj libunwind/src/CMakeFiles/unwind_shared_objects.dir/Unwind-sjlj.c.obj libunwind/src/CMakeFiles/unwind_shared_objects.dir/Unwind-wasm.c.obj libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindRegistersRestore.S.obj libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindRegistersSave.S.obj -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32
```
I find that very strange. I am not very familiar with linker internals but probably the section where the library name is stored cannot be aligned correctly if it exceeds a certain size. Am I doing something wrong in the way I am trying to setup the toolchain?
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs