Skip to content

[clang] When cross compiling to bare matel on macos, clang always use xcode clang as link driver. #140683

@neko-para

Description

@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.

$ 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
$ 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
$ 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
$ 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
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:driver'clang' and 'clang++' user-facing binaries. Not 'clang-cl'platform:macos

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions