<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/140683>140683</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang] When cross compiling to bare matel on macos, clang always use xcode clang as link driver.
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
neko-para
</td>
</tr>
</table>
<pre>
When targetting i686-elf (or i686-none-elf, more specifically), clang still using `/usr/bin/gcc` as the linking driver, which incorrectly introduce macos-only options.
```shell
$ clang -v
clang version 20.1.5 (https://github.com/llvm/llvm-project 7b09d7b446383b71b63d429b21ee45ba389c5134)
Target: i686-unknown-unknown-elf
Thread model: posix
InstalledDir: /opt/llvm-20/bin
```
* if use system linker
```shell
$ clang -target i686-none-none-elf build/src/libtest_lib.a -o kernel -ffreestanding -nostdlib -v
clang version 20.1.5 (https://github.com/llvm/llvm-project 7b09d7b446383b71b63d429b21ee45ba389c5134)
Target: i686-none-none-elf
Thread model: posix
InstalledDir: /opt/llvm-20/bin
"/usr/bin/gcc" -nostdlib -m32 -o kernel build/src/libtest_lib.a
ld: warning: The i386 architecture is deprecated for macOS (remove from the Xcode build setting: ARCHS)
ld: warning: ignoring file build/src/libtest_lib.a, building for macOS-i386 but attempting to link with file built for unknown-unsupported file format ( 0x21 0x3C 0x61 0x72 0x63 0x68 0x3E 0x0A 0x2F 0x20 0x20 0x20 0x20 0x20 0x20 0x20 )
Undefined symbols for architecture i386:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
clang: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)
```
* if use lld beside
```
$ clang -target i686-none-none-elf build/src/libtest_lib.a -o kernel -ffreestanding -nostdlib -fuse-ld=/opt/llvm-20/bin/ld.lld -v
clang version 20.1.5 (https://github.com/llvm/llvm-project 7b09d7b446383b71b63d429b21ee45ba389c5134)
Target: i686-none-none-elf
Thread model: posix
InstalledDir: /opt/llvm-20/bin
"/usr/bin/gcc" -nostdlib -fuse-ld=/opt/llvm-20/bin/ld.lld -m32 -o kernel build/src/libtest_lib.a
ld.lld: error: unknown argument '-dynamic', did you mean '-Bdynamic'
ld.lld: error: unknown argument '-arch'
ld.lld: error: unknown argument '-platform_version'
ld.lld: error: unknown argument '-syslibroot'
ld.lld: error: unable to find library -lto_library
ld.lld: error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib: unknown file type
ld.lld: error: cannot open i386: No such file or directory
ld.lld: error: cannot open macos: No such file or directory
ld.lld: error: cannot open 14.0.0: No such file or directory
ld.lld: error: cannot open 14.5: No such file or directory
ld.lld: error: cannot open /Library/Developer/CommandLineTools/SDKs/MacOSX14.5.sdk: Is a directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
clang: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)
```
* Use ld.lld directly, which works currently
```shell
$ ld.lld build/src/libtest_lib.a -o kernel
$ file kernel
kernel: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped
```
* Tried to specific `i686-linux-elf`, which works for linux
```shell
$ clang -target i686-none-linux-elf build/src/libtest_lib.a -o kernel -ffreestanding -nostdlib -fuse-ld=/opt/llvm-20/bin/ld.lld -v
clang version 20.1.5 (https://github.com/llvm/llvm-project 7b09d7b446383b71b63d429b21ee45ba389c5134)
Target: i686-none-linux-elf
Thread model: posix
InstalledDir: /opt/llvm-20/bin
"/opt/llvm-20/bin/ld.lld" --hash-style=gnu --eh-frame-hdr -m elf_i386 -pie -dynamic-linker /lib/ld-linux.so.2 -o kernel -L/usr/lib build/src/libtest_lib.a
...
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzkWN1v47gR_2vol4EEmvLngx-88RldNO0BTa69PgWUOLLYUKRAUo793xdDyYnTbnY3zeGw6BmGP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