OSX(10.9)のclangでx86&ELFなオブジェクトを生成する

Xcode 4.0から標準のコンパイラーがLLVM(clang)になったが、OSX 10.9環境&xcode 5.xを普通に使うと"Mach-O 64-bit object x86_64"なバイナリを生成してしまう。
Linux/GNU界隈でメジャーなELF(Executable and Linkable Format)なオブジェクトの生成したくて、色々調べてたら成功したので、あんまり需要無さそうだけど忘れない様にメモ。

参考:

Clang can apparently be used as a cross compiler whatever way it was built, though the available documentation is a bit unclear on how exactly to do this. Come to think of it, the available documentation is a lot unclear on just about everything. Funny for a compiler which boasts expressive error messages as a feature…

Anyway with clang version 3.1 you can compile i386-elf object files through
> clang -ccc-host-triple i386-pc-linux -c source.c -o object.o

Thomas Lovén - New Environment

手順は"-target"オプションをつけてclangを実行する。
(Xcode 5.0のLLVMは(based on LLVM 3.3svn)なので"-ccc-host-triple"が"-target"に変更されている)

$ clang hello.c -target i686-pc-linux-gnu -c -ohello_i686_elf
$ file hello_i686_elf 
hello_i686_elf: ELF 32-bit LSB relocatable, Intel 80386, version 1 (GNU/Linux), not stripped

ちゃんとELFなオブジェクトが出来ている!