OSDev.org

The Place to Start for Operating System Developers
It is currently Sat Mar 06, 2021 9:02 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: guest VM (vbox) not interpreting the call address right
PostPosted: Mon Dec 28, 2020 1:53 pm 
Offline

Joined: Thu Dec 24, 2020 2:56 pm
Posts: 1
I got rather complicate task of building C and asm code together and from the mbr jump to C code directly. Both built with nasm and gcc with 32-bit switch to get it right. But when it calls asm function from C (main) it interprets the called function's address exactly 2 bytes less. Closer look reveals VM guest is interpreting the opcode differently for call:

opcode on vm shows:
Code:
0800:0000001f e8 48 00                call 0006ah  <-
0800:00000022 00 00                   add byte [bx+si], al

Code:
opcode on objdump shows: e8 48 00 00 00
1f: e8 48 00 00 00                call   0x6c


What do I make of this? I know this is bit of a hacky way because I generated the elf binary, truncated the header + plus all the code till the beginning of main() and just mbr code directly jumped into the main() (below address 0-1f). I see there are some codes functions being called before calling function at 6c, so not sure it has anything to do with above address interpretation:

Code:
Disassembly of section .data:

0 <.data>:
       0:       8d 4c 24 04             lea    0x4(%esp),%ecx
       4:       83 e4 f0                and    $0xf0,%esp
       7:       ff 71 fc                pushl  -0x4(%ecx)
       a:       55                      push   %ebp
       b:       89 e5                   mov    %esp,%ebp
       d:       53                      push   %ebx
       e:       51                      push   %ecx
       f:       e8 ef fe ff ff          call   0xffffff03
      14:       81 c3 db 1a 00 00       add    $0x1adb,%ebx
      1a:       83 ec 0c                sub    $0xc,%esp
      1d:       6a 5b                   push   $0x5b
      1f:       e8 48 00 00 00          call   0x6c
      24:       83 c4 10                add    $0x10,%esp
      27:       83 ec 04                sub    $0x4,%esp
      2a:       6a f9                   push   $0xf9



Top
 Profile  
 
 Post subject: Re: guest VM (vbox) not interpreting the call address right
PostPosted: Mon Dec 28, 2020 6:35 pm 
Offline
Member
Member
User avatar

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 639
Are you saying that you are trying to use C code in the MBR? If so, that is not how you should do it. You should write the whole thing in asm. Also, you can tell ld to make a pure binary file by using the OUTPUT_FORMAT(binary) command in your ld script. Anyway, it looks a little strange how it shows it in the .data section. Is that intentional? Plus, notice how in the objdump output, the call 0x6c is not call $0x6c, which means it is reading what is at the address 0x6c and using that for the call instruction.
Hope this helps,
nexos

_________________
Currently working on the Nexware project, an attempt to make a less bloated version of GNU. All repos for it can be found at https://github.com/Nexware-Project.


Top
 Profile  
 
 Post subject: Re: guest VM (vbox) not interpreting the call address right
PostPosted: Mon Dec 28, 2020 8:44 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 2822
juryduty000 wrote:
opcode on vm shows:

It shows the code disassembled as 16-bit, but your objdump output shows 32-bit code. Are you trying to execute 32-bit code in 16-bit mode?

juryduty000 wrote:
I know this is bit of a hacky way because I generated the elf binary, truncated the header + plus all the code till the beginning of main() and just mbr code directly jumped into the main() (below address 0-1f).

You can make it less of a hack by using objdump to convert the ELF binary into a flat binary. (A bootloader that handles ELF binaries would be even better.)

Is there any particular reason you have to write a bootloader? Writing a bootloader that works well across a wide variety of hardware can be surprisingly difficult.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group