Beginner Question: Error 13

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
OdinX
Posts: 4
Joined: Sat Mar 02, 2019 5:51 pm

Beginner Question: Error 13

Post by OdinX »

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: Select all

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: Select all

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: Select all

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
deleted8917
Member
Member
Posts: 119
Joined: Wed Dec 12, 2018 12:16 pm

Re: Beginner Question: Error 13

Post by deleted8917 »

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: Select all

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: Select all

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: Select all

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?
nullplan
Member
Member
Posts: 1801
Joined: Wed Aug 30, 2017 8:24 am

Re: Beginner Question: Error 13

Post by nullplan »

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!
OdinX
Posts: 4
Joined: Sat Mar 02, 2019 5:51 pm

Re: Beginner Question: Error 13

Post by OdinX »

@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: Select all

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: Select all

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.
nullplan
Member
Member
Posts: 1801
Joined: Wed Aug 30, 2017 8:24 am

Re: Beginner Question: Error 13

Post by nullplan »

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!
OdinX
Posts: 4
Joined: Sat Mar 02, 2019 5:51 pm

Re: Beginner Question: Error 13

Post by OdinX »

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: Select all

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.
MichaelPetch
Member
Member
Posts: 799
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Beginner Question: Error 13

Post by MichaelPetch »

I can't recall which tutorial this came from (I've seen the bug before), but this line:

Code: Select all

section .text:
has an extra colon on the end. It should be

Code: Select all

section .text
OdinX
Posts: 4
Joined: Sat Mar 02, 2019 5:51 pm

Re: Beginner Question: Error 13

Post by OdinX »

@MichaelPetch Perfect, that's the solution. Thank you very much!
Post Reply