Toolchain: am I crazy if I use the Apple-supplied clang?
Posted: Sun Oct 07, 2018 11:02 am
After a quick test, it turned out that the copy of clang compiler supplied by Apple can produce elf files as well as Mach-O when targeting any platform Apple uses in their products (i686 and x86_64 for Macs, armv6, armv7a and armv8a for iPhones, plus armv6-m, armv7-m, armv7e-m and armv8-m for th Mx coprocessors in recent iPhones) and with some additional arguments it will even call linkers for you properly, as if you are using the prefix-gcc commands. I decided to poke around this:
You need to install binutils from source separately to get x86_64-elf-ld and aarch64-linux-gnu-ld. As of 32-bit ARM I used the linker from GNU ARM Embedded project, which is maintained directly by ARM themselves.
I was trying to use "-target x86_64-gnu" but that resulted in clang generating arguments targeting Apple's ld, freaking GNU ld out. Similar situation happened for aarch64. So you do need to lie a bit so clang would emit the correct command line for the linker, and the linker would accept it.
Code: Select all
clang -target x86_64-pc-linux-gnu -fuse-ld=/usr/local/bin/x86_64-elf-ld # for x86_64
clang -target i686-linux-gnu -fuse-ld=/usr/local/bin/x86_64-elf-ld # for i686, you can reuse x86_64-ld
clang -target arm-none-eabihf -fuse-ld=/usr/local/bin/arm-none-eabi-ld -mcpu=arm1176jzf-s -mfloat-abi=hard # for armv6f, for example arm1176jzf-s Raspberry Pi 1
clang -target arm-none-eabihf -fuse-ld=/usr/local/bin/arm-none-eabi-ld -mcpu=cortex-a7 -mfloat-abi=hard # for armv7a, for example cortex-a7 in Raspberry Pi 2
clang -target aarch64-linux-gnu -fuse-ld=/usr/local/bin/aarch64-linux-gnu-ld -mcpu=cortex-a53 # for aarch64, for example cortex-a53 in Raspberry Pi 3
# Some other ARM cores you might run across
clang -target arm-none-eabi -fuse-ld=/usr/local/bin/arm-none-eabi-ld -mcpu=arm7tdmi
clang -target arm-none-eabi -fuse-ld=/usr/local/bin/arm-none-eabi-ld -mcpu=arm926ej-s
clang -target arm-none-eabi -fuse-ld=/usr/local/bin/arm-none-eabi-ld -mcpu=cortex-m0
clang -target arm-none-eabi -fuse-ld=/usr/local/bin/arm-none-eabi-ld -mcpu=cortex-m3
clang -target arm-none-eabihf -fuse-ld=/usr/local/bin/arm-none-eabi-ld -mcpu=cortex-m4 -mfloat-abi=hard
clang -target arm-none-eabihf -fuse-ld=/usr/local/bin/arm-none-eabi-ld -mcpu=cortex-m7 -mfloat-abi=hard
clang -target arm-none-eabi -fuse-ld=/usr/local/bin/arm-none-eabi-ld -mcpu=cortex-m23
clang -target arm-none-eabihf -fuse-ld=/usr/local/bin/arm-none-eabi-ld -mcpu=cortex-m33 -mfloat-abi=hard
I was trying to use "-target x86_64-gnu" but that resulted in clang generating arguments targeting Apple's ld, freaking GNU ld out. Similar situation happened for aarch64. So you do need to lie a bit so clang would emit the correct command line for the linker, and the linker would accept it.