Issue 140683
Summary [clang] When cross compiling to bare matel on macos, clang always use xcode clang as link driver.
Labels clang
Assignees
Reporter neko-para
    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
...
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to