![](/img/trans.png)
[英]Compiling + linking a custom OS with Cygwin gcc: Unrecognized emulation mode: elf_i386
[英]Unrecognised emulation mode: elf_i386 on MinGW32
我正在嘗試創建一個內核,我無法將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
我正在使用:
為什么我會收到此錯誤,我該如何解決?
標准MinGW / 32 LD鏈接器不輸出ELF二進制文件。 您最好使用i686交叉編譯器,但如果不是,您可以使用下面的提示。
看來你正在使用Arjun的Let's Write a Kernel教程。 如果你正在跟蹤該教程你已經錯過了一步,使kernel.asm
使用GRUB引導加載程序和QEMU的兼容-kernel
選項。 在開始之前,您應該閱讀本教程的其余部分。 以下代碼將一個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映像。 您將希望將kernel.elf
與GRUB和/或QEMU一起使用 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.