Can't load flat binary file into kernel

397 views Asked by At

NOTE - I don't have paging set up yet and my kernel is multi-boot, ELF. I do have irqs and the isrs done.

So I have this GAS file here:

.section .text
.global _start

_start:
    mov $0xDEADBEEF, %eax

And GRUB2 setup to load the flat binary file:

menuentry "fOS-Terminal (25x80)" {
    multiboot /boot/fos.elf
    module /modules/program.bin
    set gfxmode=80x25
}

And here in my kernel.c, I can parse the multiboot header to get the module's address and I am calling it:

typedef void (*call_module_t)(void);
call_module_t start_program = (call_module_t)mbd->mods_addr;
start_program();

Right now I am trying to compile my GAS file into a flat binary with these commands:

i686-elf-as --32 ./iso/modules/program.s -o ./iso/modules/program.o

i686-elf-ld -fPIC -shared --oformat binary ./iso/modules/program.o -o ./iso/modules/program.bin

PROBLEM - GRUB2 is surely loading the kernel, multi-boot header is telling me it's at address - 0x100ac but when I go there, I get the exception: INVALID OPCODE.

This seems helpful but is not :(

https://littleosbook.github.io/book.pdf#page=49&zoom=auto,-100,472

EDIT - 1 So when I gdb'd to the calling function, this comes up: The pointer seems to point nowhere

1

There are 1 answers

0
amaneureka On

Here is the problem

typedef void (*call_module_t)(void);
call_module_t start_program = (call_module_t)mbd->mods_addr;
start_program();

mbd->mods_addr is the address of modules structure table. And not the address to module itself. so what is the solution?

unsigned int* modules = (unsigned int*)mbd->mods_addr;
if (mbd->mods_count > 0)
{
    unsigned int addr = modules[0];
    unsigned int size = modules[1];
    call_module_t start_program = (call_module_t)addr;
    start_program();
}
else
    painc("module wasn't loaded");