Page 1 of 1
plain binaries with gcc
Posted: Fri Jul 27, 2007 1:38 am
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
![Sad :-(](./images/smilies/icon_sad.gif)
can u help me?
thanks
martin
Posted: Fri Jul 27, 2007 2:11 am
by AJ
Do you use a linker script for ld? If so, just use the line:
If not, there is a command line switch for this too (use --help).
Cheers,
Adam
Posted: Fri Jul 27, 2007 2:13 am
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]>
Posted: Fri Jul 27, 2007 2:51 am
by sancho1980
AJ wrote:Do you use a linker script for ld? If so, just use the line:
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?
Posted: Fri Jul 27, 2007 3:09 am
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...
Posted: Fri Jul 27, 2007 3:16 am
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
Posted: Fri Jul 27, 2007 10:12 am
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
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
Posted: Fri Jul 27, 2007 7:01 pm
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.
Posted: Sat Jul 28, 2007 12:50 am
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*