Grub 2 does not detect multiboot header in kernel

I have a problem with Grub 2 (and QEMU- -kernel ) that does not detect the Multiboot v1 header in my kernel. I have a title in a separate section before .text .

linker.ld :

 SECTIONS { . = 1M; .multiboot ALIGN(4K) : { *(.multiboot) } .text ALIGN(4K) : { *(.text) } [snip] 

boot.s (GNU as syntax):

 .set MAGIC, 0x1badb002 .set FLAGS, (1<<0 | 1<<1) # align, provide mem map .set CHECKSUM, -(MAGIC + FLAGS) .section .multiboot .long MAGIC .long FLAGS .long CHECKSUM .section .text [snip] 

I checked that the header section is added as indicated by the magic number:

 kernel.bin: file format elf32-i386 Contents of section .multiboot: 101000 02b0ad1b 03000000 fb4f52e4 .........OR. Contents of section .text: [snip] 

However, Grub 2 says that the kernel does not have a valid Multiboot header, and using the QEMU -kernel calls:

 qemu: fatal: Trying to execute code outside RAM or ROM at 0x000a000 

which, apparently, is the address in the area with the BIOS displayed, and not where Multiboot should be.

I have compared with regular code in Bran and OSDev (plus the previous kernel), but I can’t understand what I'm doing wrong.

+4
source share
1 answer

I came across the same error with my multitasking kernel. I got the same error as the .text section size exceeded about 4k. The reason for my problem was that when linking, I first specified kernel.o and loader.o second in the ld arguments (I wrote a Makefile to make my OSDev Wiki Bare Bones project more convenient for development). It is assumed that Multiboot looks for the title in the first 4k, and as my code grew, it pushed the title out of this area (since it was located before the bootloader in the .text section) of the kernel. You used a separate section for the multiboot header, which may or may not be a good idea, I don't know. What I will try:

  • delete the .multiboot section and put its contents at the beginning of the bootloader and make sure loader.o is the first linker argument and kernel.o is after.
  • use readelf -a kernel to make sure that the multitask header is indeed in the first 4k (that is, if the start is at 0x00100000 , its offset is below 0x00101000 .
+3
source

Source: https://habr.com/ru/post/1490411/


All Articles