gpt4 book ai didi

c - Bochs (2.4.6)/GRUB (0.97) "Error 13 Invalid or unsupported executable format"在 C 函数调用中传递 char 字符串时

转载 作者:太空宇宙 更新时间:2023-11-04 00:04:56 28 4
gpt4 key购买 nike

我正在尝试关注 "The little book about OS development"并做一个我在 bochs 中启动的小型操作系统。

但是我遇到了一个我无法解决或解释的问题。简而言之,当传递一个 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

编译和链接似乎很顺利,没有任何警告或错误。

我错过了什么?或者我做错了什么?


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

生成文件

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

最佳答案

我遇到了同样的问题。作为mhl在评论中指出,链接器不会创建 .rodata 部分。

您必须删除 .rodata 部分并将 *(.rodata) 放入 .text 部分,紧接在 *( .text) 以便能够启动您的操作系统。

祝你好运!

关于c - Bochs (2.4.6)/GRUB (0.97) "Error 13 Invalid or unsupported executable format"在 C 函数调用中传递 char 字符串时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27939316/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com