3

I'm trying to follow the "The little book about OS development" and doing a small OS that I'm booting in bochs.

But I've run into a problem that I can't solve or explain. In short when passing a char string (in C), the kernel boot ok and works when doing;

char str[] = "a string";
call_some_function(str);

But when I try to call the function directly, with the char string defined in the function call, GRUB won't boot my kernel;

call_some_function("a string");

Then I get

Booting 'os'

kernel /boot/kernel.elf

Error 13: Invalid or unsupported executable format

The compilation and linking seems to go fine without any warning or error.

What am I missing? Or what am I doing wrong?


kmain.c

#include "framebuffer.h"

void call_some_function(char* str)
{
  fb_write(str, 1); // just print the first char in str
}

int kmain(void)
{
  char str[] = "a string";
  call_some_function(str); // this works and runs correctly

  // if I add this call GRUB won't boot
  // call_some_function("another string");
}

loader.s

global loader                           ; entry symbol for ELF
global outb
extern kmain

KERN_STACK_SIZE equ 4096                ; kernel stack size
MAGIC_NUMBER    equ 0x1BADB002          ; os magic number
FLAGS           equ 0x0                 ; multiboot flags
CHECKSUM        equ -MAGIC_NUMBER       ; magic number + checksum should eq 0

section .bss
align 4
kernel_stack:
        resb KERN_STACK_SIZE            ; reserve stack space

section .text:                          ; start code section
align 4                                 ; 4 byte aligned code
        dd MAGIC_NUMBER                 ; write magic number
        dd FLAGS                        ; write multiboot flags
        dd CHECKSUM                     ; write checksum

loader:                                 ; loader label (used as entry point in linker script)
        mov esp, kernel_stack + KERN_STACK_SIZE ; init esp

        push dword 3
        push dword 2
        push dword 1
        call kmain
.loop:
        jmp .loop                       ; loop forever

outb:
; outb - send a byte to an IO port
; stack: [esp + 8] data byte
;        [esp + 4] cmd byte
;        [esp    ] return addr
        mov al, [esp + 8]       ; move data byte
        mov dx, [esp + 4]       ; move cmd byte
        out dx, al
        ret

link.ld

ENTRY(loader)                           /* entry label */

SECTIONS
{
        . = 0x00100000;                 /* code should be loader at 1MB */

        .text : ALIGN (0x1000)          /* align at 4KB */
        {
                *(.text)                /* all text sections from all files */
        }

        .rodata : ALIGN (0x1000)        /* align at 4KB */
        {
                *(.rodata*)             /* all read-only data sections from all files */
        }

        .data : ALIGN (0x1000)          /* align at 4KB */
        {
                *(.data)                /* all data sections from all files */
        }

        .bss : ALIGN (0x1000)           /* align at 4KB */
        {
                *(COMMON)               /* all COMMON sections from all files */
                *(.bss)                 /* all bss sections from all files */
        }
}

bochsrc.txt

megs:                   32
display_library:        sdl
romimage:               file=/usr/share/bochs/BIOS-bochs-latest
vgaromimage:            file=/usr/share/bochs/VGABIOS-lgpl-latest
ata0-master:            type=cdrom, path=os.iso, status=inserted
boot:                   cdrom
clock:                  sync=realtime, time0=local
cpu:                    count=1, ips=1000000

Makefile

OBJECTS = loader.o framebuffer.o print.o kmain.o
CC = gcc
CFLAGS = -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector \
         -nostartfiles -nodefaultlibs -Wall -Wextra -Werror -c
LDFALGS = -T link.ld -melf_i386
AS = nasm
ASFALGS = -f elf

all: kernel.elf

kernel.elf: $(OBJECTS)
        ld $(LDFALGS) $(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) $(ASFALGS) $< -o $@

clean:
        rm -rf *.o kernel.elf os.iso
max7
  • 43
  • 4
  • Are you using cross-compiler specific to your os? There can be some problems when using system compiler. – mhl Jan 14 '15 at 09:34
  • I'm using the default gcc (4.7.2 / Debian 4.7.2-5) on debian which is the same host I'm booting bochs on. I'm not that fluent in compilers unfortunatly, but should I use another compiler? – max7 Jan 14 '15 at 15:54
  • Yes. Setting up and cross-compiler for your target is not so hard, and can separate you from host dependencies. Check out this article: http://wiki.osdev.org/GCC_Cross-Compiler – mhl Jan 14 '15 at 16:12
  • Thanks. I'm going to read up on it and try it out. – max7 Jan 16 '15 at 07:57
  • It appears that linker doesn't create .rodata section. Can you remove .rodata section and put *(.rodata) after *(.text) in .text section? – mhl Jan 21 '15 at 14:31
  • `section text:` is a bug with the colon on the end, it should be `section text` – Michael Petch Mar 04 '19 at 17:24

3 Answers3

1

I had the same problem. As mhl stated in a comment, the linker does not create the .rodata section.

You must remove the .rodata section and put *(.rodata) into the .text section just after *(.text) to be able to boot your OS.

Good luck !

Community
  • 1
  • 1
Lukhio
  • 46
  • 1
  • 7
0

The reason that the .rodata section is not created is because you have a colon behind section .text where there should not be one.

This colon creates an additional section called .text: which screws up your memory contents.

Also see Error 13: Invalid or unsupported executable while booting simple kernel in grub with string literal

Community
  • 1
  • 1
ErwinM
  • 5,051
  • 2
  • 23
  • 35
0

I think the problem is on your kmain.c file. In your loader.s file, you pushed 3 parameters to the stack which your kmain function did not expect. Re-read Chapter 3 again and you will get the idea.

I suggested you rollback your changes to the stage where the kernel started function and start modifying from there.

David Yu
  • 1
  • 1