plain binaries with gcc

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
sancho1980
Member
Member
Posts: 199
Joined: Fri Jul 13, 2007 6:37 am
Location: Stuttgart/Germany
Contact:

plain binaries with gcc

Post by sancho1980 »

hi

i have found this pdf online

http://www.cs.york.ac.uk/rtslab/demos/r ... mpiler.pdf

it describes how to compile plain binaries with gcc...i have followed the examples in there and compiled them the way im told in there and the problem is: i dont get the same compiled results as described in the pdf! i have disassembled the compiled output and i get different output..it seems there have been too many changes to the gcc compiler since this was written...can anyone tell me where else i can find information on how to make binary files with c???
i also gave it a try by simply jumping to the compiled binaries from my assembly code, which didnt work :-( can u help me?

thanks

martin
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Do you use a linker script for ld? If so, just use the line:

Code: Select all

OUTPUT_FORMAT(binary);
If not, there is a command line switch for this too (use --help).

Cheers,
Adam
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

I attend that university (York UK). RTS is realtime systems, and is all done in ADA95, for embedded systems. I wouldn't be surprised if the docs there are out of date. The document also isn't written by anyone at York uni, so I'm not sure of it's quality. What exactly is the difference between your results and theirs?

JamesM <[email protected]>
sancho1980
Member
Member
Posts: 199
Joined: Fri Jul 13, 2007 6:37 am
Location: Stuttgart/Germany
Contact:

Post by sancho1980 »

AJ wrote:Do you use a linker script for ld? If so, just use the line:

Code: Select all

OUTPUT_FORMAT(binary);
If not, there is a command line switch for this too (use --help).

Cheers,
Adam
i dont use a linker script..just using a simple makefile

i compile with "--oformat binary"...should that be the same?
sancho1980
Member
Member
Posts: 199
Joined: Fri Jul 13, 2007 6:37 am
Location: Stuttgart/Germany
Contact:

Post by sancho1980 »

JamesM wrote:I attend that university (York UK). RTS is realtime systems, and is all done in ADA95, for embedded systems. I wouldn't be surprised if the docs there are out of date. The document also isn't written by anyone at York uni, so I'm not sure of it's quality. What exactly is the difference between your results and theirs?

JamesM <[email protected]>
there are a couple extra instructions in my output plus, im not sure, when i compile a file with several functions where main() calls one of the other functions and i specify main() as the entry functions, i get the impression that main() is not really the code that is place right at the top of the output, not so sure though..looks all a bit weird to me..ill post it when i come home from work...
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

sancho1980 wrote: i compile with "--oformat binary"...should that be the same?
Yes - that's the one.
sancho1980 wrote: ill post it when i come home from work...
Will be interesting to see. I'll also look through that pdf - I haven't really compared my code to a dissasembly before...

Adam
sancho1980
Member
Member
Posts: 199
Joined: Fri Jul 13, 2007 6:37 am
Location: Stuttgart/Germany
Contact:

Post by sancho1980 »

Okay, the simplest program ever, as discussed at the very start of the text:

Code: Select all

//begin test.c

int main()
{
}

//end test.c
The text recommends to compile:

Code: Select all

gcc -c test.c
ld test.o -o test.bin -Ttext 0x0 -e main -oformat binary
And then says

Code: Select all

ndisasm -b 32 test.bin
will give me:

Code: Select all

00000000 55 push ebp
00000001 89E5 mov ebp,esp
00000003 C9 leave
00000004 C3 ret
Now, here's what I get:

Code: Select all

sancho@Kiste:~$ gcc -c test.c
sancho@Kiste:~$ ld test.o -o test.bin -Ttext 0x0 -e main --oformat binary
sancho@Kiste:~$ ndisasm -b 32 test.bin 
00000000  8D4C2404          lea ecx,[esp+0x4]
00000004  83E4F0            and esp,byte -0x10
00000007  FF71FC            push dword [ecx-0x4]
0000000A  55                push ebp
0000000B  89E5              mov ebp,esp
0000000D  51                push ecx
0000000E  59                pop ecx
0000000F  5D                pop ebp
00000010  8D61FC            lea esp,[ecx-0x4]
00000013  C3                ret
What are all these extra instructions all about? Am I really supposed to be able to execute this in a no-strings-attached environment, i.e., without any operating system? Also, is there no way to specify in the compiler options that main() neednt really be compiled as a function, since I'm not technically calling it but only jmp'ing there...
Thanks for any help

martin
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

All of that extra code seems to be there in order to align the stack to a 16 byte boundary. That code will run just fine without an operating system, it doesn't call any other functions, it doesn't do any privileged operations it just aligns the stack. If you don't want main to return put a for( ;; ) before the end bracket and it can't possibly return.
sancho1980
Member
Member
Posts: 199
Joined: Fri Jul 13, 2007 6:37 am
Location: Stuttgart/Germany
Contact:

Post by sancho1980 »

Yes it came to me too a little later...so it seems I'm really able to jump from my assembly boot code to my C-code *happy*
Post Reply