【问题标题】:nasm/gcc issue on 64-bit Mac OS X Lion64 位 Mac OS X Lion 上的 nasm/gcc 问题
【发布时间】:2012-03-07 16:55:18
【问题描述】:

我正在阅读this 的文章, 在某一时刻,它给了我这个 nasm 程序:

; tiny.asm
BITS 32
GLOBAL main
SECTION .text
main:
              mov     eax, 42
              ret

并告诉我运行以下命令:

$ nasm -f elf tiny.asm
$ gcc -Wall -s tiny.o

我收到以下错误:

ld: warning: option -s is obsolete and being ignored
ld: warning: ignoring file tiny.o, file was built for unsupported file format which is not the architecture being linked (x86_64)
Undefined symbols for architecture x86_64:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

我大胆猜测可能是什么问题,并将 BITS 行更改为:

 BITS 64

但是当我运行 nasm -f elf tiny.asm 时,我得到:

tiny.asm:2: error: `64' is not a valid segment size; must be 16 or 32

如何修改代码以在我的机器上工作?

编辑:

我听取了 cmets 的 Alex 的建议并下载了更新的版本。然而,

./nasm-2.09.10/nasm -f elf tiny.asm

抱怨

tiny.asm:2: error: elf32 output format does not support 64-bit code

另一方面,

./nasm-2.09.10/nasm -f elf64 tiny.asm
gcc -Wall -s tiny.o

抱怨

ld: warning: ignoring file tiny.o, file was built for unsupported file format which is not the architecture being linked (x86_64)
Undefined symbols for architecture x86_64:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

【问题讨论】:

  • @Alex elf64 给了我“无法识别的输出格式”。至于版本, nasm -v 给了我“2011 年 11 月 3 日编译的 NASM 版本 0.98.40(Apple Computer, Inc. build 11)” 0.98 看起来可能是旧版本,但它是在 2011 年编译的,所以它可以'不会那么老吧?苹果为什么要发布过时的软件?无法在 64 位平台上汇编 64 位代码的默认汇编器?
  • 你的已经过时了。我的打印“2011 年 7 月 15 日编译的 NASM 版本 2.09.10”。较新的版本是here

标签: macos gcc nasm


【解决方案1】:

您似乎仍在使用 32 位版本。如果你nasm -hf 它应该列出macho64。如果没有,您将需要再次更新。

您可以在控制台中尝试 brew update 。如果这会执行更新,那么brew search nasm 应该会在其中显示nasm。然后只需brew install nasm。这应该将 nasm 安装到您的计算机上。一定要注意它的安装位置。我的安装在 /usr/local/cellar/nasm/2.11.02/bin/ 。然后输入nasm -hf,它应该会显示一个可用格式列表,您应该看到macho64 可用。

【讨论】:

    【解决方案2】:

    您必须进行特定于 OS X 的调整才能使您的示例正常工作: OS X 链接器在 main 方法前面加上一个 _:

    ; tiny.asm
    BITS 32
    GLOBAL _main
    SECTION .text
    _main:
        mov     eax, 42
        ret
    

    第二个是你必须使用mach文件格式:

    nasm -f macho tiny.asm
    

    现在你可以链接它了(使用 -m32 表示一个 32 位的目标文件):

    gcc -m32 tiny.o
    

    【讨论】:

    • 对于 64 位代码,请使用 -fmacho64。此外,您不需要bits 32,我建议您避免使用它。如果您尝试将 32 位源代码组装成 64 位目标文件,您想要一条错误消息,反之亦然。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 2012-08-29
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    相关资源
    最近更新 更多