Compiling a multiboot kernel on Haiku

Hello,

I’m trying to compile and link a multiboot kernel using Haiku, but when I boot into my kernel on another machine I get a ‘No Multiboot Header found’

I started out defining a .multiboot section in my kernel code, and when that didn’t work I tried doing it in the linker script, but either way when I link using:

ld -o kernel.bin -T linker.ld --oformat binary obj/kernel.o obj/print.o -m elf_i386_haiku -e _start

it shoves everything in the .data section, and I suspect this is why the bootloader is not finding the header.

Linker Script

ENTRY(_start)

SECTIONS
{
    . = 0x10000;

    .multiboot : {
        KEEP(*(.multiboot))
    }

    .text : {
        *(.text)
    }

    .rodata : {
        *(.rodata*)
    }

    .data : {
        *(.data)
    }

    .bss : {
        *(.bss)
    }
}

And my original Multiboot definition in the source:

...

#define MULTIBOOT_MAGIC 0x1BADB002
#define MULTIBOOT_FLAGS	0x00000003															

extern "C" void print(const char* str);

struct MultibootHeader {
	uint32_t magic;
	uint32_t flags;
	uint32_t checksum;	
};

// Align the Multiboot header to 8 bytes
__attribute__((section(".multiboot"),aligned(8)))
static MultibootHeader header = {
	.magic = MULTIBOOT_MAGIC,
	.flags = MULTIBOOT_FLAGS,
	.checksum = static_cast<uint32_t>(-(MULTIBOOT_MAGIC + MULTIBOOT_FLAGS))
};
...

Regardless, something ain’t right. Is there some optimization or default flag being set in Haiku’s implementation of the linker which would be causing this? Or is there something blindingly obvious I should be seeing in my script/code?

I’m fairly new to kernel code, so I’m sort of in unfamiliar territory here.

Any help would be gratefully received!

EDIT: When running objdump -f kernel.bin -b binary I get:

kernel.bin               file format binary
architecture: UNKNOWN!, flags 0x00000000

start address 0x00000000

You need to add the “used” attribute to your static variable, otherwise gcc will think that nothing uses it, and will remove it when compiling.

It may b, difficult to get the kernel to work in the multiboot setup, as it expects the bootloader to do a lot of setup work. it may be easier to do it in the stage 2 bootloader, and have that load the kernel, depending on what you’re trying to do.

1 Like

Thanks for the heads up! I was wondering if maybe the linker emulation was a factor; I’m linking to x86_64_haiku, and the machine I’m booting my kernel on is different from the one I’m compiling on. I mean, the architecture is the same (x86_64) but maybe I need to be cross-compiling? Didn’t think that would be required, but maybe…