Skip to content

Linker flag detection for lld fails when using LDFLAGS="-fuse-ld=lld" #14582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mhthies opened this issue May 9, 2025 · 4 comments
Open

Linker flag detection for lld fails when using LDFLAGS="-fuse-ld=lld" #14582

mhthies opened this issue May 9, 2025 · 4 comments

Comments

@mhthies
Copy link

mhthies commented May 9, 2025

Describe the bug
When using the lld linker via Clang by setting the environment variables LD="clang" and LDFLAGS="-fuse-ld=lld", Meson fails to detect the supported lld linker flags and applies invalid flags to the linker invocation, e.g. -Wl,--allow-shlib-undefined, which is unsupported for Windows targets.

To Reproduce
Setup any Meson project on Windows for building with MinGW x86_64 CLang compiler, using the following environment variables:

CC=C:/msys64/mingw64/bin/clang.exe
LD=C:/msys64/mingw64/bin/clang.exe
CHOST=x86_64-w64-mingw64
LDFLAGS=-fuse-ld=lld
CFLAGS=-m64

A good example is the pkgconf project.

Detailed instructions for reproducing:
On a Windows machine,

  • get MSYS2 install from https://www.msys2.org/ and install to C:\msys64
  • in MSYS2 shell, install CLang for x86_64: pacman -S mingw-w64-x86_64-clang
  • install Ninja
  • clone pkgconf code from https://github.com/pkgconf/pkgconf
  • apply the environment variables listed above
  • run meson setup build in pkgconf repository
  • run meson compile -C build in pkgconf repository

Actual behavior
meson setup succeeds, but compilation fails in linking stage with the following error:

lld: error: unknown argument: --allow-shlib-undefined
clang: error: linker command failed with exit code 1 (use -v to see invocation)

This is caused by Meson, automatically applying the linker argument -Wl,--allow-shlib-undefined, which is not supported by clang/lld on Windows.

Expected behavior
Meson should detect that the lld linker does not support the --allow-shlib-undefined flag. The build should succeed.

system parameters

  • plain native build, but MinGW clang on Windows
  • Windows 11
  • Python 3.12
  • meson 1.8.99 (latest master: 36de1c6)
  • ninja 1.12.1
@bonzini
Copy link
Collaborator

bonzini commented May 9, 2025

Put both -fuse-ld=lld and -m64 in the CC environment variable (or if using a machine file, put -fuse-ld=lld in c_ld and -m64 in both c and c_ld). This is documented (but could be better) at https://mesonbuild.com/Machine-files.html#binaries.

@mhthies
Copy link
Author

mhthies commented May 9, 2025

The issue is caused by the LLVMDynamicLinker.supports_flag() not using the flags from the LDFLAGS environment variable: The command invoked for checking the support for the --allow-shlib-undefined flag is: ['C:/msys64/mingw64/bin/clang.exe', '-Wl,--allow-shlib-undefined']. It returns the following output on stderr:

C:/msys64/mingw64/bin\ld: C:/msys64/mingw64\x86_64-w64-mingw32\lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): in function `main':
D:/mingwbuild/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Obviously, the wrong linker has been used and the output does not include the "unknown argument: '--allow-shlib-undefined'" string, Meson is looking for, so the incompatibility is not detected.

Instead, the following command line should be used for the check: ['C:/msys64/mingw64/bin/clang.exe', '-fuse-ld=lld', '-Wl,--allow-shlib-undefined'], which then would report

lld: error: unknown argument: --allow-shlib-undefined
clang: error: linker command failed with exit code 1 (use -v to see invocation)

which would be processed correctly by Meson.

@mhthies
Copy link
Author

mhthies commented May 9, 2025

@bonzini: I've found #10654, which explicitly adds the LDFLAGS (via env.coredata.get_external_link_args()) to the command line arguments for detecting the linker. Thus, I assume that it is an oversight that these flags are not used when checking the supported linker flags.

@eli-schwartz
Copy link
Member

Thus, I assume that it is an oversight that these flags are not used when checking the supported linker flags.

That is my feeling as well. Thanks for digging into this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants