You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

苹果M1芯片Ubuntu环境下OS内核链接报错:无法识别elf_i386仿真模式

解决aarch64 Ubuntu下链接i386内核时ld: unrecognised emulation mode: elf_i386错误

问题场景

在M1芯片的Mac上通过UTM运行aarch64架构的Ubuntu,编写操作系统内核时,执行链接命令:

ld -T link.ld -melf_i386 loader.o -o kernel.elf

出现报错:

ld: unrecognised emulation mode: elf_i386 Supported emulations: aarch64linux aarch64elf aarch64elf32 aarch64elf32b aarch64elfb armelf armelfb aarch64linuxb aarch64linux32 aarch64linux32b armelfb_linux_eabi armelf_linux_eabi

核心原因

当前Ubuntu是ARM64(aarch64)架构,系统默认的ld链接器仅支持ARM系列的目标文件格式,无法处理x86(i386)架构的elf文件,需要安装x86交叉编译工具链来生成和链接i386架构的目标文件。

解决方案

  1. 安装i386交叉编译工具链
    执行以下命令安装支持i386的链接器和编译器:

    sudo apt update && sudo apt install binutils-i686-linux-gnu gcc-i686-linux-gnu
    
  2. 重新编译loader.s为i386目标文件
    使用交叉汇编器编译你的loader.s,确保生成的是i386架构的目标文件:

    i686-linux-gnu-as loader.s -o loader.o
    
  3. 用交叉链接器执行链接操作
    替换系统默认的ld为i386专用的交叉链接器:

    i686-linux-gnu-ld -T link.ld -melf_i386 loader.o -o kernel.elf
    

附:你的源码文件

loader.s

global loader                   ; the entry symbol for ELF

MAGIC_NUMBER equ 0x1BADB002     ; define the magic number constant
FLAGS        equ 0x0            ; multiboot flags
CHECKSUM     equ -MAGIC_NUMBER  ; calculate the checksum
                                ; (magic number + checksum + flags should equal 0)

section .text                  ; start of the text (code) section
align 4                         ; the code must be 4 byte aligned
    dd MAGIC_NUMBER             ; write the magic number to the machine code,
    dd FLAGS                    ; the flags,
    dd CHECKSUM                 ; and the checksum

loader:                         ; the loader label (defined as entry point in linker script)
    mov eax, 0xCAFEBABE         ; place the number 0xCAFEBABE in the register eax
.loop:
    jmp .loop                   ; loop forever

link.ld

ENTRY(loader)                /* the name of the entry label */

SECTIONS {
. = 0x00100000;          /* the code should be loaded at 1 MB */

.text ALIGN (0x1000) :   /* align at 4 KB */
{
    *(.text)             /* all text sections from all files */
}

.rodata ALIGN (0x1000) : /* align at 4 KB */
{
    *(.rodata*)          /* all read-only data sections from all files */
}

.data ALIGN (0x1000) :   /* align at 4 KB */
{
    *(.data)             /* all data sections from all files */
}

.bss ALIGN (0x1000) :    /* align at 4 KB */
{
    *(COMMON)            /* all COMMON sections from all files */
    *(.bss)              /* all bss sections from all files */
}
}

内容的提问来源于stack exchange,提问作者Hiran Sanjeewa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 11:40:38