OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 25, 2024 10:01 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Beginner Question: Error 13
PostPosted: Sat Mar 02, 2019 6:00 pm 
Offline

Joined: Sat Mar 02, 2019 5:51 pm
Posts: 4
Hello. I started with trying osdev, just for fun.

I've managed to boot the kernel and print stuff with the framebuffer, so I implemented a few printing functions and now it doesn't work anymore for no good reason.

When I add an additional empty method in my framebuffer.c, the error appears and when I remove that empty method, it works again.

So I researched, and it seems the multiboot header is probably not located at the beginning anymore, when the size of the kernel increases.

How can I guarantee that the multiboot header is at the beginning?

My loader.s:

Code:
global loader
extern kmain

MAGIC_NUMBER      equ 0x1BADB002
FLAGS         equ 0x0
CHECKSUM      equ -MAGIC_NUMBER

KERNEL_STACK_SIZE   equ 4096

section .bss
align 4
kernel_stack:
   resb KERNEL_STACK_SIZE

section .text:
align 4
   dd MAGIC_NUMBER
   dd FLAGS
   dd CHECKSUM

loader:
   mov esp, kernel_stack + KERNEL_STACK_SIZE
   mov eax, 0xCAFEBABE
   call kmain
.loop:
   jmp .loop


My link.ld:

Code:
ENTRY(loader)

SECTIONS {
   . = 0x00100000;Hel

   .text ALIGN (0x1000) :
   {
      *(.text)
      *(.rodata)
   }

   .rodata ALIGN (0x1000) :
   {
      *(.rodata*)
   }

   .data ALIGN (0x1000) :
   {
      *(.data)
   }

   .bss ALIGN (0x1000) :
   {
      *(COMMON)
      *(.bss)
   }
}


My Makefile:

Code:
OBJECTS = loader.o kmain.o devices/framebuffer.o
CC = gcc
CFLAGS = -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -Wall -Wextra -Werror -c
LDFLAGS = -T link.ld -melf_i386
AS = nasm
ASFLAGS = -f elf

all: kernel.elf

kernel.elf: $(OBJECTS)
   ld $(LDFLAGS) $(OBJECTS) -o kernel.elf

os.iso: kernel.elf
   cp kernel.elf iso/boot/kernel.elf
   genisoimage -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -A os -input-charset utf8 -quiet -boot-info-table -o os.iso iso

run: os.iso
   bochs -f bochsrc.txt -q

%.o: %.c
   $(CC) $(CFLAGS)  $< -o $@

%.o: %.s
   $(AS) $(ASFLAGS) $< -o $@

clean:
   rm -rf *.o kernel.elf os.iso


Top
 Profile  
 
 Post subject: Re: Beginner Question: Error 13
PostPosted: Sat Mar 02, 2019 6:27 pm 
Offline
Member
Member

Joined: Wed Dec 12, 2018 12:16 pm
Posts: 119
OdinX wrote:
Hello. I started with trying osdev, just for fun.

I've managed to boot the kernel and print stuff with the framebuffer, so I implemented a few printing functions and now it doesn't work anymore for no good reason.

When I add an additional empty method in my framebuffer.c, the error appears and when I remove that empty method, it works again.

So I researched, and it seems the multiboot header is probably not located at the beginning anymore, when the size of the kernel increases.

How can I guarantee that the multiboot header is at the beginning?

My loader.s:

Code:
global loader
extern kmain

MAGIC_NUMBER      equ 0x1BADB002
FLAGS         equ 0x0
CHECKSUM      equ -MAGIC_NUMBER

KERNEL_STACK_SIZE   equ 4096

section .bss
align 4
kernel_stack:
   resb KERNEL_STACK_SIZE

section .text:
align 4
   dd MAGIC_NUMBER
   dd FLAGS
   dd CHECKSUM

loader:
   mov esp, kernel_stack + KERNEL_STACK_SIZE
   mov eax, 0xCAFEBABE
   call kmain
.loop:
   jmp .loop


My link.ld:

Code:
ENTRY(loader)

SECTIONS {
   . = 0x00100000;Hel

   .text ALIGN (0x1000) :
   {
      *(.text)
      *(.rodata)
   }

   .rodata ALIGN (0x1000) :
   {
      *(.rodata*)
   }

   .data ALIGN (0x1000) :
   {
      *(.data)
   }

   .bss ALIGN (0x1000) :
   {
      *(COMMON)
      *(.bss)
   }
}


My Makefile:

Code:
OBJECTS = loader.o kmain.o devices/framebuffer.o
CC = gcc
CFLAGS = -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -Wall -Wextra -Werror -c
LDFLAGS = -T link.ld -melf_i386
AS = nasm
ASFLAGS = -f elf

all: kernel.elf

kernel.elf: $(OBJECTS)
   ld $(LDFLAGS) $(OBJECTS) -o kernel.elf

os.iso: kernel.elf
   cp kernel.elf iso/boot/kernel.elf
   genisoimage -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -A os -input-charset utf8 -quiet -boot-info-table -o os.iso iso

run: os.iso
   bochs -f bochsrc.txt -q

%.o: %.c
   $(CC) $(CFLAGS)  $< -o $@

%.o: %.s
   $(AS) $(ASFLAGS) $< -o $@

clean:
   rm -rf *.o kernel.elf os.iso

Aaaaaaand where is framebuffer.c?


Top
 Profile  
 
 Post subject: Re: Beginner Question: Error 13
PostPosted: Sun Mar 03, 2019 12:29 am 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1605
Put the multiboot header into its own section and link that before anything else. The way you have it right now, I'm not sure, but I think you're wasting close to 4kB with the align command at the start, since the header of whatever output format you're using is misaligning the .text section. But honestly, the early boot doesn't need that alignment, typically. Anyway, no matter if that happens or not, your multiboot header will end up somewhere else. It is possible the linker is sorting by filename, so loader.o(.text) comes after framebuffer.o(.text). So the empty method you added might push the multiboot header beyond the 8kB line, and then it will not be found by GRUB.

_________________
Carpe diem!


Top
 Profile  
 
 Post subject: Re: Beginner Question: Error 13
PostPosted: Sun Mar 03, 2019 1:24 am 
Offline

Joined: Sat Mar 02, 2019 5:51 pm
Posts: 4
@hextakatt: I thought that files content is irrelevant for the problem, because only its size seems to be relevant for my kernel booting or not, that's why I didn't post it. But who knows, I may end up being wrong.

@nullplan: Like this?

loader.s
Code:
global loader
extern kmain

MAGIC_NUMBER      equ 0x1BADB002
FLAGS         equ 0x0
CHECKSUM      equ -MAGIC_NUMBER

KERNEL_STACK_SIZE   equ 4096

section .bss
align 4
kernel_stack:
   resb KERNEL_STACK_SIZE

section .mb:
align 4
   dd MAGIC_NUMBER
   dd FLAGS
   dd CHECKSUM

section .text
loader:
   mov esp, kernel_stack + KERNEL_STACK_SIZE
   mov eax, 0xCAFEBABE
   call kmain
.loop:
   jmp .loop



link.ld
Code:
ENTRY(loader)

SECTIONS {
   . = 0x00100000;

   .mb ALIGN (0x1000) :
   {
      *(.mb)
   }

   .text ALIGN (0x1000) :
   {
      *(.text)
      *(.rodata)
   }

   .rodata ALIGN (0x1000) :
   {
      *(.rodata*)
   }

   .data ALIGN (0x1000) :
   {
      *(.data)
   }

   .bss ALIGN (0x1000) :
   {
      *(COMMON)
      *(.bss)
   }
}


Seems that doesn't work, it's still running if I delete an empty method from framebuffer.c and producing the error if I don't.


Top
 Profile  
 
 Post subject: Re: Beginner Question: Error 13
PostPosted: Sun Mar 03, 2019 1:49 am 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1605
Yes, that is sort of what I had in mind.

Look at the file produced this way: Where is the multiboot header? Use a hexeditor, if need be. I can never remember the specifics but it has to be near the start of the file, either within the first 4k or the first 8k. But I do think for multiboot 1 it was 4k.

Again with the alignment. Can you try dropping the ALIGN directives, at least for .mb and .text? Or maybe turn it down to something reasonable, like 4?

_________________
Carpe diem!


Top
 Profile  
 
 Post subject: Re: Beginner Question: Error 13
PostPosted: Sun Mar 03, 2019 4:16 am 
Offline

Joined: Sat Mar 02, 2019 5:51 pm
Posts: 4
I inspected the ELF with a hex editor and I found the multiboot header 02B0AD1B at two places, byte 8116 and byte 12552.

Then I removed the align in the link.ld

Code:
ENTRY(loader)

SECTIONS {
   . = 0x00100000;

   .mb : /*ALIGN (0x1000) :*/
   {
      *(.mb)
   }

   .text : /*ALIGN (0x1000) :*/
   {
      *(.text)
      *(.rodata)
   }

   .rodata : /*ALIGN (0x1000) :*/
   {
      *(.rodata*)
   }

   .data : /*ALIGN (0x1000) :*/
   {
      *(.data)
   }

   .bss : /*ALIGN (0x1000) :*/
   {
      *(COMMON)
      *(.bss)
   }
}


But it did not really help.

I inspected the elf-file again and found the multiboot header again at two places, bytes 8432 and 9540, so not better.

I guess my multiboot magic string implies multiboot version 1
btw, I'm following the tutorial on https://littleosbook.github.io/, and I'm not sure why they (and other tutorials) align at 4k.


Top
 Profile  
 
 Post subject: Re: Beginner Question: Error 13
PostPosted: Sun Mar 03, 2019 10:42 am 
Online
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 693
I can't recall which tutorial this came from (I've seen the bug before), but this line:
Code:
section .text:
has an extra colon on the end. It should be
Code:
section .text


Top
 Profile  
 
 Post subject: Re: Beginner Question: Error 13
PostPosted: Mon Mar 04, 2019 12:43 am 
Offline

Joined: Sat Mar 02, 2019 5:51 pm
Posts: 4
@MichaelPetch Perfect, that's the solution. Thank you very much!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: MichaelPetch and 238 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group