Page 1 of 1

I'm searching for a C compilator that can compile in binary

Posted: Sun Sep 13, 2009 4:00 pm
by electritwist
Does anyone know a C compiler that can compile out binary file?

Re: I'm searching for a C compilator that can compile in binary

Posted: Sun Sep 13, 2009 4:30 pm
by neon
There is none. Probably not possible either do to plain binary files having no readable symbolic names.

Re: I'm searching for a C compilator that can compile in binary

Posted: Sun Sep 13, 2009 4:34 pm
by Aurdal
You could link the object file(s) created by the compiler to a plain binary though. Though I don't know if that helps you.

Re: I'm searching for a C compilator that can compile in binary

Posted: Sun Sep 13, 2009 5:18 pm
by JohnnyTheDon
electritwist wrote:Does anyone know a C compiler that can compile out binary file?
GCC will do it, you just have to use a linker script or linker command line option to set the output format to binary.

Re: I'm searching for a C compilator that can compile in binary

Posted: Sun Sep 13, 2009 5:31 pm
by ~
What do you people think of it? I have this C file for example:

Code: Select all

main()
{
asm("mov $0x12345678,%eax");
asm("cli");
asm("hlt");

}
And then I have this command line for GCC:

Code: Select all

gcc -Wl,-nostdlib,--oformat,binary,-Ttext,0x100,-static c32.c -o c32.bin -nostartfiles -nodefaultlibs -ffreestanding

It produces the following machine code:

Code: Select all

push        ebp
mov         ebp,esp
sub         esp,008 ;""
and         esp,-010 ;"ð"
mov         eax,000000000
add         eax,00F ;""
add         eax,00F ;""
shr         eax,004 ;""
shl         eax,004 ;""
sub         esp,eax
mov         eax,012345678 ;"4Vx"
cli
hlt
leave
retn
nop
And the binary file is 768-bytes long. As you can see the actual custom code starts at the line #11 of the machine code, which in this case was offset 0x1C in the binary image. Could this be extended for more uses to actually use GCC to produce "raw code"?

Surely there will always have the need to use many compiler directives in the code to ensure such thing, but maybe inline assembly could be used sometimes instead.

Re: I'm searching for a C compilator that can compile in binary

Posted: Mon Sep 14, 2009 1:35 am
by Aurdal
~ wrote:And the binary file is 768-bytes long.
Note to self: Never try to make a bootloader with in-line assembly.

Re: I'm searching for a C compilator that can compile in binary

Posted: Mon Sep 14, 2009 2:45 am
by Solar
Note not only to self: A tool always does exactly what it was told. If results are not what you expected, double-check what you told the tool, because chances are excellent you didn't say what you wanted.

Code: Select all

$ gcc -nostartfiles -nodefaultlibs -Wl,--oformat=binary,-Ttext,0x100 c32.c -o c32.bin
$ ls c32.*
-rwxr-xr-x  1 solar solar   72 2009-09-14 10:52 c32.bin
-rw-r--r--  1 solar solar   80 2009-09-14 10:52 c32.c