Page 1 of 1

basic problem to start with an OS project

Posted: Sat Oct 23, 2004 11:00 pm
by prajwal
Hi,

My name is Prajwal....

I'v a problem in generating flat binary file in Linux....
Please try to solve my problem with Patience...
It'll be easy for me to explain this with an example....

1 ***************************

ASM CODE: start.asm

[BITS 32]
[EXTERN main1]
[GLOBAL _start]

_start:
call main1

2 ****************************

C CODE: test.c

extern void main1();

void display()
{
}

void main1()
{
display();
}

3 ******************************

nasm -f aout start.asm -o start.o // runs successfully
gcc -c test.c -o test.o // runs successfully

Now I try to link these two using ld - linker

ld -o final.bin --oformat binary start.o test.o // runs successfully

I get "final.bin"....

But when I disassemble it using "ndisasm"... I observe that
function calls are not properly resovled....

what I mean is... the disassembled code would look like this:

01 call 0xb
04 nop // why these nops....
05 nop
06 nop
07 push ... // display function
09 ...
0a leave
0b ret
0c push ... // main1 function
0d sub...
0e call 0x5
10 leave
11 ret

The addresses used may not be correct but all function calls to
some address which is always some 2 bytes or 1 byte less than
what it should have been...

Also the call to display which is part of c program is also improper...

I got some Kernel code in C from websites... which had similar kind
of scenario when compiling and linking...

I followed same procedure to compile them as specified in their readme..
but even there the bin code generated had this problem....

Please help me out of this problem....

Thanks for reading with patience,
-Prajwal

In the above case call is to 0b but should have been to 0c

Re: basic problem to start with an OS project

Posted: Sat Oct 23, 2004 11:00 pm
by chase
I think you just left off the option for ndisasm to force 32-bit opcode disassembly. Try using:

Code: Select all

ndisasm -b 32 final.bin
The result of which is:

Code: Select all

00000000  E808000000        call 0xd
00000005  90                nop
00000006  90                nop
00000007  90                nop
00000008  55                push ebp
00000009  89E5              mov ebp,esp
0000000B  5D                pop ebp
0000000C  C3                ret
0000000D  55                pushn ebp
0000000E  89E5              mov ebp,esp
00000010  83EC08            sub esp,byte +0x8
00000013  E8F0FFFFFF        call 0x8
00000018  C9                leave
00000019  C3                ret
Additionally the extra nop calls are probably put in by gcc, try looking up all the options to turn off the byte padding to align instructions and branch prediction.

Re: basic problem to start with an OS project

Posted: Sat Oct 23, 2004 11:00 pm
by prajwal
Thanks Chase.... Thanks very much.... that problem is solved...

Problems before solution is like Himalayan Mountains....
After Solution seems to be like an ant.....

Thanks for clarifying my doubt....