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

Programming, for all ages and all languages.
Post Reply
electritwist
Posts: 4
Joined: Mon Sep 07, 2009 3:46 am

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

Post by electritwist »

Does anyone know a C compiler that can compile out binary file?
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

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

Post by neon »

There is none. Probably not possible either do to plain binary files having no readable symbolic names.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Aurdal
Posts: 11
Joined: Fri Sep 11, 2009 6:08 pm
Location: Norway

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

Post 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.
Caution: Enjoy above remarks responsibly. Overuse may be hazardous to your mental health.
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

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

Post 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.
User avatar
~
Member
Member
Posts: 1226
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

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

Post 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.
YouTube:
http://youtube.com/@AltComp126

My x86 emulator/kernel project and software tools/documentation:
http://master.dl.sourceforge.net/projec ... 7z?viasf=1
Aurdal
Posts: 11
Joined: Fri Sep 11, 2009 6:08 pm
Location: Norway

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

Post by Aurdal »

~ wrote:And the binary file is 768-bytes long.
Note to self: Never try to make a bootloader with in-line assembly.
Caution: Enjoy above remarks responsibly. Overuse may be hazardous to your mental health.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

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

Post 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
Every good solution is obvious once you've found it.
Post Reply