在Linux问题上构建内核:未识别的仿真模式:ELF_I386

发布于 2025-02-08 08:42:23 字数 2260 浏览 1 评论 0 原文

我正在关注。我使用虚拟化Ubuntu在MacOS 12.4上构建它。我无法让内核编译并接收未识别的仿真模式:ELF_I386 错误。 GCC命令不适用于 -M32 选项,而是在我删除后运行。 我使用的命令位于本文的底部。 感谢您的帮助。

kernel.asm

;;kernel.asm
bits 32     ;nasm directive
section .text
    ;multiboot spec
    align 4
    dd 0x1BADB002           ;magic
    dd 0x00             ;flags
    dd - (0x1BADB002 + 0x00)    ;checksum. m+f+c should be zero

global start
extern kmain    ;kmain is defined in the c file

start:
    cli ;block interrupts
    call kmain
    hlt ;halt the CPU

kernel.c

#define WHITE_TXT 0x07 /* light gray on black text */

void k_clear_screen();
unsigned int k_printf(char *message, unsigned int line);

/* simple kernel written in C */
void k_main() 
{
    k_clear_screen();
    k_printf("Hello, world! Welcome to my kernel.", 0);
};

/* k_clear_screen : to clear the entire text screen */
void k_clear_screen()
{
    char *vidmem = (char *) 0xb8000;
    unsigned int i=0;
    while(i < (80*25*2))
    {
        vidmem[i]=' ';
        i++;
        vidmem[i]=WHITE_TXT;
        i++;
    };
};

/* k_printf : the message and the line # */
unsigned int k_printf(char *message, unsigned int line)
{
    char *vidmem = (char *) 0xb8000;
    unsigned int i=0;

    i=(line*80*2);

    while(*message!=0)
    {
        if(*message=='\n') // check for a new line
        {
            line++;
            i=(line*80*2);
            *message++;
        } else {
            vidmem[i]=*message;
            *message++;
            i++;
            vidmem[i]=WHITE_TXT;
            i++;
        };
    };

    return(1);
}

linker.ld

OUTPUT_FORMAT(elf32-i386)
ENTRY(start)
SECTIONS
{
    . = 0x100000;
    .text : {*(.text)}
    .data : {*(.data)}
    .bss  : {*(.bss)}
}

我用来构建项目的命令。

nasm -f elf32 kernel.asm -o kasm.o => WORKS
gcc -m32 -c kernel.c -o kc.o
=> unrecognized command line option -m32 (works when I remove it)

ld -m elf_i386 -T link.ld -o kernel kasm.o kc.o 
=> ERROR HERE: ld: unrecognised emulation mode: elf_i386

qemu-system-i386 -kernel kernel

I am following the tutorial from https://github.com/chipsetx/Simple-Kernel-in-C-and-Assembly. I am building it on macOS 12.4 using virtualised Ubuntu. I cannot get the kernel to compile and receive the unrecognised emulation mode: elf_i386 error.
GCC command does not work with -m32 option, but runs after I remove it.
The commands that I used are at the bottom of this post.
Thanks for help.

kernel.asm

;;kernel.asm
bits 32     ;nasm directive
section .text
    ;multiboot spec
    align 4
    dd 0x1BADB002           ;magic
    dd 0x00             ;flags
    dd - (0x1BADB002 + 0x00)    ;checksum. m+f+c should be zero

global start
extern kmain    ;kmain is defined in the c file

start:
    cli ;block interrupts
    call kmain
    hlt ;halt the CPU

kernel.c

#define WHITE_TXT 0x07 /* light gray on black text */

void k_clear_screen();
unsigned int k_printf(char *message, unsigned int line);

/* simple kernel written in C */
void k_main() 
{
    k_clear_screen();
    k_printf("Hello, world! Welcome to my kernel.", 0);
};

/* k_clear_screen : to clear the entire text screen */
void k_clear_screen()
{
    char *vidmem = (char *) 0xb8000;
    unsigned int i=0;
    while(i < (80*25*2))
    {
        vidmem[i]=' ';
        i++;
        vidmem[i]=WHITE_TXT;
        i++;
    };
};

/* k_printf : the message and the line # */
unsigned int k_printf(char *message, unsigned int line)
{
    char *vidmem = (char *) 0xb8000;
    unsigned int i=0;

    i=(line*80*2);

    while(*message!=0)
    {
        if(*message=='\n') // check for a new line
        {
            line++;
            i=(line*80*2);
            *message++;
        } else {
            vidmem[i]=*message;
            *message++;
            i++;
            vidmem[i]=WHITE_TXT;
            i++;
        };
    };

    return(1);
}

linker.ld

OUTPUT_FORMAT(elf32-i386)
ENTRY(start)
SECTIONS
{
    . = 0x100000;
    .text : {*(.text)}
    .data : {*(.data)}
    .bss  : {*(.bss)}
}

The commands I have used to build the project.

nasm -f elf32 kernel.asm -o kasm.o => WORKS
gcc -m32 -c kernel.c -o kc.o
=> unrecognized command line option -m32 (works when I remove it)

ld -m elf_i386 -T link.ld -o kernel kasm.o kc.o 
=> ERROR HERE: ld: unrecognised emulation mode: elf_i386

qemu-system-i386 -kernel kernel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文