I'm searching for a C compilator that can compile in binary
-
- Posts: 4
- Joined: Mon Sep 07, 2009 3:46 am
I'm searching for a C compilator that can compile in binary
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
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();}
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Re: I'm searching for a C compilator that can compile in binary
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.
-
- 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
GCC will do it, you just have to use a linker script or linker command line option to set the output format to binary.electritwist wrote: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
What do you people think of it? I have this C file for example:
And then I have this command line for GCC:
It produces the following machine code:
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.
Code: Select all
main()
{
asm("mov $0x12345678,%eax");
asm("cli");
asm("hlt");
}
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
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
http://youtube.com/@AltComp126
My x86 emulator/kernel project and software tools/documentation:
http://master.dl.sourceforge.net/projec ... 7z?viasf=1
Re: I'm searching for a C compilator that can compile in binary
Note to self: Never try to make a bootloader with in-line assembly.~ wrote:And the binary file is 768-bytes long.
Caution: Enjoy above remarks responsibly. Overuse may be hazardous to your mental health.
Re: I'm searching for a C compilator that can compile in binary
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.