Fel*_*elo 3 c x86 assembly linker osdev
我正在尝试创建一个内核,我无法将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) }
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
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:
Run Code Online (Sandbox Code Playgroud)
编译和链接我使用:
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
Run Code Online (Sandbox Code Playgroud)
我正在使用:
为什么我会收到此错误,我该如何解决?
标准MinGW/32 LD链接器不输出ELF二进制文件.您最好使用i686交叉编译器,但如果不是,您可以使用下面的提示.
看来你正在使用Arjun的Let's Write a Kernel教程.如果你正在跟踪该教程你已经错过了一步,使kernel.asm与兼容GRUB引导加载程序和QEMU的-kernel选项.在开始之前,您应该阅读本教程的其余部分.以下代码添加Multiboot标头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:
Run Code Online (Sandbox Code Playgroud)
除了添加标题之外,我还在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) }
}
Run Code Online (Sandbox Code Playgroud)
上面文件中的另一个重要变化是使用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
Run Code Online (Sandbox Code Playgroud)
该LD命令已被修改为不写出构建-ID到可执行以避免被移动的可执行文件的第一8K外多重引导头.该GCC选项已被使用选项修饰以产生独立的码(不标准库和包括)-ffreestanding -nostdlib -nostdinc.我们使用objcopy将PEI-I386文件(kernel)转换为调用的ELF32图像kernel.elf.您将希望kernel.elf与GRUB和/或QEMU一起使用.