I am trying to build raw executable binary to load and execute at arbitrary memory address at boot.
For elf binary, i can easily generate 32-bit binary:
nasm -felf32 -F dwarf boota.asm
gcc -m32 bootc.c boota.o -o boot.elf.
However i dont need elf file along its headers. I also see when main() is declared (as program entry) compiler puts additional (possibly O/S dependent code) prior to executing main() so I dont need those too.
I sort of did hacked the resulting elf binary such that bootloader code will directly jump into main() but when main() calls one of the assembler function, it interprets call dest. address 2 bytes less.
How do i make it work?
in brief:
opcode on vm sees: e8 48 00 (call label at 6a) (using virtualbox vdebug capability to step-through)
opcode on objdump sees: (call label at 6c) e8 48 00 00 00
more code (code starts executing at 800:00 address after bios boot):
from virtual box debugger:
VBoxDbg> u 800:0
0800:0 8d 4c 24 lea cx, [si+024h]
0800:03 04 83 add AL, 083h
0800:05 e4 f0 in AL, 0f0h
0800:07 ff 71 fc push word [bx+di-004h]
0800:0a 55 push bp
0800:0b 89 e5 mov bp, sp
0800:0d 53 push bx
0800:0e 51 push cx
0800:0f e8 ef fe call 0ff01h
0800:00000012 ff ff Illegal opcode
0800:00000014 81 c3 db 1a add bx, 01adbh
0800:00000018 00 00 add byte [bx+si], al
0800:0000001a 83 ec 0c sub sp, byte 0000ch
0800:0000001d 6a 5b push byte 0005bh
0800:0000001f e8 48 00 call 0006ah <--------------------------------- ( here it is calling the assembler defined function or label, but as you can see below, function entry is at 6c)
0800:00000022 00 00 add byte [bx+si], al
0800:00000024 83 c4 10 add sp, byte 00010h
0800:00000027 83 ec 04 sub sp, byte 00004h
0800:0000002a 6a f9 push byte 0fff9h
0800:0000002c 6a fc push byte 0fffch
0800:00000065 66 39 d0 cmp eax, edx
0800:00000068 66 0f 4c c2 cmovl eax, edx
0800:0000006c b4 0e mov AH, 00eh <---------- (but function is at 6c)
0800:0000006e b0 25 mov AL, 025h
0800:00000070 cd 10
disassemly looks OK:
OBJDUMP OUTPUT:
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