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

macOS 14.5下无-no_pie编译NASM代码报错问题求助

解决macOS 14.5下NASM编译警告与总线错误问题

问题根源

  1. 对齐不足:NASM默认生成的_main符号仅1字节对齐,macOS现代链接器要求函数入口满足至少16字节对齐(PIE模式下更严格),否则会触发对齐警告,甚至总线错误。
  2. PIE兼容性:移除-no_pie后启用了位置无关可执行(PIE)模式,但原代码直接用绝对地址加载message(mov rsi, message),在PIE环境下会导致地址解析错误,触发总线错误。同时-no_pie在新macOS版本已被弃用,必须适配PIE。

修改后的代码

global    _main

section   .text
align     16  ; 显式设置16字节对齐,满足macOS要求
_main:    mov       rax, 0x02000004         ; system call for write
          lea       rsi, [rel message]      ; 使用相对寻址适配PIE模式
          mov       rdi, 1                  ; file handle 1 is stdout
          mov       rdx, 13                 ; number of bytes
          syscall                           ; invoke operating system to do the write
          mov       rax, 0x02000001         ; system call for exit
          xor       rdi, rdi                ; exit code 0
          syscall                           ; invoke operating system to exit

section   .data
message:  db        "Hello, World", 10      ; note the newline at the end

正确编译命令

nasm -fmacho64 hello.asm && gcc hello.o -o hello && ./hello

关键修改说明

  • align 16:在_main前添加该指令,强制函数入口按16字节对齐,彻底消除对齐警告,避免总线错误的触发条件。
  • lea rsi, [rel message]:替换原有的mov rsi, message,使用相对寻址方式获取message的地址,适配PIE模式的位置无关要求,确保程序在PIE环境下能正确解析数据地址。
  • 移除-Wl,-no_pie:新编译命令不再依赖该弃用参数,完全适配macOS 14.5的现代链接器要求。

内容的提问来源于stack exchange,提问作者Christopher Rybicki

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 23:22:09