无法识别的仿真模式:MinGW32上的elf_i386
我正在尝试创建一个内核,我无法将C输出与程序集链接起来。 ld
。 我收到了错误:
无法识别的仿真模式:elf_i386
我正在使用Windows 10专业版与MinGW32和MSYS。 我正在使用的代码:
link.ld
/* * link.ld */ OUTPUT_FORMAT(elf32-i386) ENTRY(start) SECTIONS { . = 0x100000; .text : { *(.text) } .data : { *(.data) } .bss : { *(.bss) } }
kernel.c
/* * kernel.c */ void kmain(void) { const char *str = "my first kernel"; char *vidptr = (char*)0xb8000; //video mem begins here. unsigned int i = 0; unsigned int j = 0; /* this loops clears the screen * there are 25 lines each of 80 columns; each element takes 2 bytes */ while(j < 80 * 25 * 2) { /* blank character */ vidptr[j] = ' '; /* attribute-byte - light grey on black screen */ vidptr[j+1] = 0x07; j = j + 2; } j = 0; /* this loop writes the string to video memory */ while(str[j] != '\0') { /* the character's ascii */ vidptr[i] = str[j]; /* attribute-byte: give character black bg and light grey fg */ vidptr[i+1] = 0x07; ++j; i = i + 2; } return; }
kernel.asm
;;kernel.asm bits 32 ;nasm directive - 32 bit section .text global start extern kmain ;kmain is defined in the c file start: cli ;block interrupts mov esp, stack_space ;set stack pointer call kmain hlt ;halt the CPU section .bss resb 8192 ;8KB for stack stack_space:
要编译和链接我使用:
nasm -f elf32 kernel.asm -o kasm.o gcc -m32 -c kernel.c -o kc.o ld -m elf_i386 -T link.ld -o kernel kasm.o kc.o
我正在使用:
- Gcc 4.8.1
- Ld 2.25.1
- Nasm 2.11.09rc1
为什么我会收到此错误,我该如何解决?
标准MinGW / 32 LD链接器不输出ELF二进制文件。 您最好使用i686交叉编译器,但如果您不是,则可以使用以下提示。
看来你正在使用Arjun的Let’s Write a Kernel教程。 如果您正在学习该教程,那么您错过了使kernel.asm
与GRUB引导加载程序和QEMU的kernel.asm
兼容的步骤。 在开始之前,您应该阅读本教程的其余部分。 以下代码将一个kernel.asm
标头添加到kernel.asm
以使其与GRUB兼容:
;;kernel.asm bits 32 ;nasm directive - 32 bit global entry extern _kmain ;kmain is defined in the c file section .text entry: jmp start ;multiboot spec align 4 dd 0x1BADB002 ;magic dd 0x00 ;flags dd -(0x1BADB002 + 0x00) ;checksum. m+f+c should be zero start: cli ;block interrupts mov esp, stack_space ;set stack pointer call _kmain hlt ;halt the CPU section .bss resb 8192 ;8KB for stack stack_space:
除了添加标题之外,我还在文件中放入了一个entry
标签,并且jmp start
跳过Multiboot标头。 我已经这样做了,如果你开始调试,将来很容易在0x100000设置一个断点。
另一个变化是,在MinGW上, GCC默认为函数名添加下划线。 我已经将对C函数kmain
引用更改为_kmain
。 这与Linux惯例不同。
由于我们的代码的入口点现在是entry
而不是start
我已经修改了link.ld
:
/* * link.ld */ OUTPUT_FORMAT(pei-i386) ENTRY(entry) SECTIONS { . = 0x100000; .text : { *(.text) } .data : { *(.data) } .bss : { *(.bss) } }
上述文件中的另一个重要变化是使用OUTPUT_FORMAT(pei-i386)
。 这将输出可移植可执行映像(32位)而不是ELF (不支持)。
为了构建内核并从PEI-I386生成ELF映像,我们可以使用以下命令:
nasm -f elf32 kernel.asm -o kasm.o gcc -m32 -c kernel.c -o kc.o -ffreestanding -nostdlib -nostdinc ld -T link.ld -o kernel kasm.o kc.o -build-id=none objcopy -O elf32-i386 kernel kernel.elf
已修改LD命令,以便不将build-id写入可执行文件,以避免Multiboot标头移出可执行文件的前8k。 已修改GCC选项以使用-ffreestanding -nostdlib -nostdinc
选项生成独立代码(没有标准库和包含)。 我们使用objcopy
将PEI-I386文件( kernel
)转换为名为kernel.elf
的ELF32映像。 您将希望使用带有GRUB和/或QEMU的 kernel.elf
。