[LLVMdev] Compiling a raw binary with llvm/clang

Török Edwin edwintorok at gmail.com
Thu Dec 17 12:26:44 PST 2009


On 2009-12-17 22:08, LiteHacker wrote:
> Hey,
>
> The following code creates a raw binary (no ELF or PE.. just raw):
>
> gcc -nostartfiles -c -o hello.o hello.c
> ld -o hello1 hello.o --oformat binary
>
> You can try the following code out with it:
>
> void _start()
> {
>   while(1);
> }
>
> The resulting raw binary is 5 bytes.
>   

It is 2.1M here (on x86_64), and 4.3k with -m32 when using gcc 4.4.2.
Apparently it includes the dynamic linker inside it.
> Now how do you do this in clang/llvm?
> Apparently llvm-ld doesn't have a "--oformat binary" option.
> Anybody know how to do this with llvm?
>   

Simple, use ld on the .o files, as you would with gcc.

An example with clang:
$ clang -nostartfiles -c x.c -o x.o
$ ld -o x x.o --oformat binary

Or to get a 32-bit one:
$ /llvm-git/obj/Release/bin/clang -nostartfiles -c x.c -o x.o -m32
$ ld -o x1 x.o --oformat binary -melf_i386

This one is indeed 5 bytes long.

Best regards,
--Edwin




More information about the llvm-dev mailing list