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

添加-g选项后aarch64-none-elf-as汇编ARM64向量文件出现未对齐错误

AArch64汇编添加-g选项触发“unaligned opcodes detected”错误的解决方法

问题背景

使用aarch64-none-elf-as编译ARM64异常向量表vector.s时,无-g选项可正常汇编,添加-g后抛出错误:

aarch64/shared/vectors.s: Error: unaligned opcodes detected in executable segment

所用汇编器版本:

GNU assembler (GNU Toolchain for the Arm Architecture 11.2-2022.02 (arm-11.14)) 2.37.20220122

向量表已按AArch64要求配置:整体0x800对齐,每个条目0x80对齐,代码位于vectors可执行段。因汇编失败无法生成二进制文件排查地址,且未找到汇编器内置的对齐信息查看选项。

根因分析

核心问题是可执行段中混入了非指令数据:你将.asciz定义的调试字符串直接放在了标记为"ax"(可执行、可分配)的vectors段内。

  • 无-g时,汇编器仅做基础汇编,对可执行段内的非指令内容校验宽松;
  • 添加-g后,汇编器需要生成精确的调试信息,会严格校验可执行段的合规性:AArch64要求可执行段内的指令必须4字节对齐,而字符串是字节流(长度不一定为4的倍数),不属于指令范畴,触发了汇编器的对齐检查错误。

解决方案

将所有字符串数据移至单独的只读数据段(.rodata),与可执行代码分离。修改后的vector.s示例如下:

.section vectors, "ax", %progbits
        .global  vector_table

        .balign 0x800
vector_table:
curr_el_sp0_sync:
        b       curr_el_sp0_sync_vector
        adr     x0, curr_el_sp0_sync_str
        bl      print
        b       terminate

        .balign 0x80
curr_el_sp0_irq:
        b       curr_el_sp0_irq_vector
        adr     x0, curr_el_sp0_irq_str
        bl      print
        b       terminate

        .balign 0x80
print:
        ret
terminate:
        ret

# 单独的只读数据段存放字符串常量
        .section .rodata, "a", %progbits
curr_el_sp0_sync_str:
        .asciz "Unexpected exception: curr_el_sp0_sync\n"
curr_el_sp0_irq_str:
        .asciz "Unexpected exception: curr_el_sp0_irq\n"
fail_str:
        .asciz "** TEST FAILED**\n\004"
        .end

验证

执行带-g的汇编命令:

aarch64-none-elf-as -march=armv8.4-a+crc aarch64/shared/vectors.s -g -o aarch64/shared/vectors.o

此时汇编可正常完成,同时保留调试信息。

内容的提问来源于stack exchange,提问作者Chan Kim

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 13:40:04