plain binaries with gcc
-
- Member
- Posts: 199
- Joined: Fri Jul 13, 2007 6:37 am
- Location: Stuttgart/Germany
- Contact:
plain binaries with gcc
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
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
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
Code: Select all
OUTPUT_FORMAT(binary);
Cheers,
Adam
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]>
JamesM <[email protected]>
-
- Member
- Posts: 199
- Joined: Fri Jul 13, 2007 6:37 am
- Location: Stuttgart/Germany
- Contact:
i dont use a linker script..just using a simple makefileAJ 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).Code: Select all
OUTPUT_FORMAT(binary);
Cheers,
Adam
i compile with "--oformat binary"...should that be the same?
-
- Member
- Posts: 199
- Joined: Fri Jul 13, 2007 6:37 am
- Location: Stuttgart/Germany
- Contact:
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...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]>
-
- Member
- Posts: 199
- Joined: Fri Jul 13, 2007 6:37 am
- Location: Stuttgart/Germany
- Contact:
Okay, the simplest program ever, as discussed at the very start of the text:
The text recommends to compile:
And then says will give me:
Now, here's what I get:
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
Code: Select all
//begin test.c
int main()
{
}
//end test.c
Code: Select all
gcc -c test.c
ld test.o -o test.bin -Ttext 0x0 -e main -oformat binary
Code: Select all
ndisasm -b 32 test.bin
Code: Select all
00000000 55 push ebp
00000001 89E5 mov ebp,esp
00000003 C9 leave
00000004 C3 ret
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
Thanks for any help
martin
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.
-
- Member
- Posts: 199
- Joined: Fri Jul 13, 2007 6:37 am
- Location: Stuttgart/Germany
- Contact: