Shvets04 wrote:
Problem is that the code generated by compiler doesn't try pass any a pointer to function.
Code:
b: c7 45 f4 00 00 00 00 movl $0x0,-0xc(%ebp)
e: R_386_32 .rodata
12: 8b 45 f4 mov -0xc(%ebp),%eax
15: 89 04 24 mov %eax,(%esp)
18: e8 fc ff ff ff call 19 <start+0x19>
19: R_386_PC32 print_s
Yes it does. It just so happens that the data is the first data in the rodata section, so it has an offset of 0x00000000.
Code:
b: c7 45 f4 00 00 00 00 movl $0x0,-0xc(%ebp)
Code:
Contents of section .rodata:
0000 48656c6c 6f20776f 726c6400 Hello world.
Here is the code:
Code:
// char* str = "Hello world";
b: c7 45 f4 00 00 00 00 movl $0x0,-0xc(%ebp)
The compiler reserved a pointer-sized variable at [ebp-12] (which is on the stack) and placed the offset from the .rodata section to the string. It then retrieves this value, pushing it onto the stack and calling the print_s routine:
Code:
12: 8b 45 f4 mov -0xc(%ebp),%eax
15: 89 04 24 mov %eax,(%esp)
18: e8 fc ff ff ff call 19 <start+0x19>
The above could be see as (in Intel syntax), though the above code eliminates the need for the stack clean up:
Code:
12: mov eax,[ebp-12]
15: push eax
16: call print_s
You will need to find out where your rodata section is, related to the text section and make an adjustment. For example, if your rodata section is 0x100 bytes from the "start of the file", then you will need.
Code:
12: mov eax,[ebp-12]
15: add eax,rodata_section_adjustment
xx: push eax
xx: call print_s
However, this isn't really the correct way to do it either.
My suggestion, to make it all must easier, is to only have one section. However, this gets difficult the larger the code base.
I don't use GCC so someone else is going to have to tell you how to get it to create only one section, or tell you how to output the correct binary file.
I use a standard Windows PE file with, what would be considered, a single section, so that SS == DS == ES, etc.
Ben
-
http://www.fysnet.net/osdesign_book_series.htm