我正在尝试遵循“关于操作系统开发的小书”并做一个我正在启动的小型操作系统。
但是我遇到了一个我无法解决或解释的问题。简而言之,当传递一个 char 字符串(在 C 中)时,内核启动正常并且在执行时工作;
char str[] = "a string";
call_some_function(str);
但是当我尝试直接调用函数时,使用函数调用中定义的 char 字符串,GRUB 不会启动我的内核;
call_some_function("a string");
然后我得到
Booting 'os'
kernel /boot/kernel.elf
Error 13: Invalid or unsupported executable format
编译和链接似乎很顺利,没有任何警告或错误。
我错过了什么?或者我做错了什么?
主程序
#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");
}
加载器.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
链接.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
生成文件
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