Page 1 of 1

Questions about using ld script to generate multiboot header

Posted: Sun Jun 01, 2014 9:21 am
by songziming
I want to make a flat binary kernel, booting it with GRUB, and I tried write Multiboot header using ld script like this.

Code: Select all

OUTPUT_FORMAT(binary)
SECTIONS {
    . = 1M;
    .boot : ALIGN(4) {
        multiboot_magic = 0x1badb002;
        multiboot_flags = 1<<0 | 1<<1 | 1<<16;
        multiboot_check = - (multiboot_magic + multiboot_flags);

        LONG(multiboot_magic - 1M);
        LONG(multiboot_flags - 1M);
        LONG(multiboot_check - 1M);

        LONG(1M);   /* multiboot header_addr */
        LONG(1M);   /* multiboot load_addr */
        LONG(seg_data_end_addr);    /* multiboot load_end_addr */
        LONG(seg_bss_end_addr); /* multiboot bss_end_addr */
        LONG(multiboot_entry);  /* multiboot entry_addr */
        
        *(.bootstrap)   /* Assembly code for invoking C */
    }
    ... text rodata data and bss ...
}
the code works well, But I want to know why I have to use

Code: Select all

LONG(multiboot_magic - 1M);
other than

Code: Select all

LONG(multiboot_magic);
. In the latter case, if I use xxd to examine the hexidemical value I'll discover 0x1badb002 becomes 0x1bbdb002.

Re: Questions about using ld script to generate multiboot he

Posted: Sun Jun 01, 2014 12:43 pm
by xenos
I guess that's because you defined / assigned multiboot_magic, multiboot_flags and multiboot_check within the .boot section. If you define them at the beginning of your linker script, before starting the .boot section, it should work without the 1M shift.

http://sourceforge.net/p/xenos/code/HEA ... 2/ldscript