在 C 函数调用中传递 char 字符串时,Bochs (2.4.6)/GRUB (0.97) “错误 13 无效或不受支持的可执行格式”

Posted

技术标签:

【中文标题】在 C 函数调用中传递 char 字符串时,Bochs (2.4.6)/GRUB (0.97) “错误 13 无效或不受支持的可执行格式”【英文标题】:Bochs (2.4.6)/GRUB (0.97) "Error 13 Invalid or unsupported executable format" when passing char string in C function call 【发布时间】:2015-03-12 10:08:39 【问题描述】:

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

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

【问题讨论】:

您是否使用特定于您的操作系统的交叉编译器?使用系统编译器时可能会出现一些问题。 我在 debian 上使用默认的 gcc (4.7.2 / Debian 4.7.2-5),这与我启动 bochs 的主机相同。不幸的是,我对编译器不是很流利,但我应该使用其他编译器吗? 是的。为您的目标设置和交叉编译器并不难,并且可以将您与主机依赖项分开。看看这篇文章:wiki.osdev.org/GCC_Cross-Compiler 谢谢。我将阅读并尝试一下。 链接器似乎没有创建 .rodata 部分。您可以删除 .rodata 部分并将 *(.rodata) 放在 *(.text) 之后的 .text 部分吗? 【参考方案1】:

我认为问题出在您的 kmain.c 文件上。在您的 loader.s 文件中,您将 3 个参数推送到了您的 kmain 函数没有预料到的堆栈。再读一遍第 3 章,你就会明白了。

我建议您将更改回滚到内核启动功能的阶段并从那里开始修改。

【讨论】:

【参考方案2】:

.rodata 部分未创建的原因是因为您在section .text 后面有一个冒号,而该冒号不应该有。

这个冒号创建了一个名为 .text: 的附加部分,它会破坏你的记忆内容。

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

【讨论】:

【参考方案3】:

我遇到了同样的问题。正如评论中的mhl 所述,链接器不会创建.rodata 部分。

您必须删除.rodata 部分并将*(.rodata) 放入.text 部分,紧跟在*(.text) 之后才能启动您的操作系统。

祝你好运!

【讨论】:

以上是关于在 C 函数调用中传递 char 字符串时,Bochs (2.4.6)/GRUB (0.97) “错误 13 无效或不受支持的可执行格式”的主要内容,如果未能解决你的问题,请参考以下文章

QML和C++混合编程中,在qml中向C++的char* 函数传递一个char*的字符串参数,qml不能识别char*的参数类型

c ++将带引号的字符串作为char数组传递给函数

C ++将字符串传递给lua错误

请问这个C语言中有参数的函数是怎么传递值的,调用的时候没有参数啊。

C#调用C++的dll库怎么传递结构体中不定长度的char数组

c语言字符串在函数间传递