Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bochs (2.4.6)/GRUB (0.97) "Error 13 Invalid or unsupported executable format" when passing char string in C function call

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
like image 918
max7 Avatar asked Sep 07 '25 20:09

max7


1 Answers

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 !

like image 177
Lukhio Avatar answered Sep 10 '25 13:09

Lukhio