Indeed we shouldn't be trying to use --gc-sections on Darwin. The only time we should use this flag is when linking an executable with GNU ld, which is clearly not the case here. It would indeed be useful to have the output from ghc --info and ld --version.
I have tried to reproduce this using the upstream 9.2.2 bindist on an M1 mac but failed.
@rae, is it possible that you have multiple linkers installed (e.g. perhaps binutils is installed through brew)? Perhaps configure found one but GHC is using the other?
There was a mention of configure, but I did not invoke configure at all: this was installed via ghcup (which might internally invoke configure -- I don't know).
Indeed it sounds like you have ld.lld installed via Homebrew, which confuses configure. There appears to be two bugs here:
Somehow configure detects ld.lld yet clang is still using the system's linker. Something must have gone very wrong to get into this state.
I suspect we should probably not use configure's linker-override logic on Darwin since we really never want to use anything but the system's toolchain on that platform.
The cause of this regression in %9.2.2 was 0299de81. Prior to that change things worked but only accidentally. Specifically, the m4/find_ld.m4 logic would attempt to probe lld but would fail to recognize the version string as lld and consequently give up. However, now since that commit we conclude that lld is usable.
Further, ld.lld's --version string contains (compatible with GNU linkers), which then causes the logic in m4/fp_prog_ld_is_gnu.m4 to concluded that the linker is a GNU bfd linker. GHC then in turn uses that knowledge to conclude that the linker supports --gc-sections. However, this is fine as ld.lld does indeed support --gc-sections, just as ld.bfd would.
GHC consequently passes -fuse-ld=lld -Wl,--gc-sections to gcc (which is Apple's clang). However, gcc then decides to use ld64.lld (notld.lld as configure found), which does not support --gc-sections. Incidentally, ld64.lld's version string does not contain the string (compatible with GNU linkers) (which indeed it is not).
Some output @bgamari asked for in a very helpful live debugging session (thank you!):
rae:16:42:36 ~/temp> which gcc/usr/bin/gccrae:16:44:09 ~/temp> gcc --versionApple clang version 13.1.6 (clang-1316.0.21.2.5)Target: arm64-apple-darwin21.4.0Thread model: posixInstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/binrae:16:44:11 ~/temp> which ld/usr/bin/ldrae:16:44:13 ~/temp> ld -v@(#)PROGRAM:ld PROJECT:ld64-764BUILD 11:29:07 May 17 2022configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7emLTO support using: LLVM version 13.1.6, (clang-1316.0.21.2.5) (static support for 28, runtime is 28)TAPI support using: Apple TAPI version 13.1.6 (tapi-1316.0.7.3)rae:16:44:15 ~/temp> which ld64.lld/opt/homebrew/opt/llvm/bin/ld64.lldrae:16:44:18 ~/temp> ld64.lld -vld64.lld: error: must specify -archrae:16:44:26 ~/temp> ld64.lld --versionHomebrew LLD 13.0.1rae:16:44:31 ~/temp> which ld.lld/opt/homebrew/opt/llvm/bin/ld.lldrae:16:44:45 ~/temp> ld.lld -vHomebrew LLD 13.0.1 (compatible with GNU linkers)rae:16:44:49 ~/temp> ld.lld --versionHomebrew LLD 13.0.1 (compatible with GNU linkers)
As a temporary workaround, one can pass --disable-ld-override to the binary distribution configure script. This will disable the linker discovery logic and use the system's default linker.
i think i hit this very same issue. its nasty and somehow even doing that one step isn't quite complete in fixing the generated settings file
clang: error: invalid linker name in argument '-fuse-ld=lld'ghc-9.4.3: `gcc' failed in phase `Linker'. (Exit code: 1)Error: cabal: Failed to build exe:happy from happy-1.20.0. See the build logabove for details.Failed to build exe:alex from alex-3.2.7.1. See the build log above fordetails.
i get past that, and i get errors instead about gc-sections
ld: unknown option: --gc-sectionsclang: error: linker command failed with exit code 1 (use -v to see invocation)ghc-9.4.3: `clang' failed in phase `Linker'. (Exit code: 1)Error: cabal: Failed to build exe:happy from happy-1.20.0. See the build logabove for details.Failed to build exe:alex from alex-3.2.7.1. See the build log above fordetails.