【问题标题】:Simple asm program with yasm in MacOS Mountain LionMacOS Mountain Lion 中带有 yasm 的简单 asm 程序
【发布时间】:2012-10-12 14:25:44
【问题描述】:

我想编译并执行一个非常简单的 64 位程序。

section .text 
global _start 
_start: 
   mov     rdx,len 
   mov     rcx,msg 
   mov     rbx,1 
   mov     rax,4 
   int     0x80 
   mov    rbx,0 
   mov     rax,1 
   int     0x80 
section .data 
msg     db      "Hello, world!",0xa 
len     equ     $ - msg 

编译它的行:

yasm -f elf64 -g dwarf2 example.asm

$ yasm --version
yasm 1.2.0

还尝试了另一种格式macho[|32|64], elf[|32] bin,但都没有成功。

链接它的行:

gcc -o example example.o

ld: warning: ignoring file example.o, file was built for unsupported file format ( 0x7f 0x45 0x4c 0x46 0x 2 0x 1 0x 1 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 ) which is not the architecture being linked (x86_64): example.o
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

$ gcc --version
i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)

还尝试了gcc 的一些选项,例如-m64 -arch x86_64

【问题讨论】:

  • MacOS 上的 gcc 不能使用 Mach 格式吗?
  • 您是否尝试过使用 NASM/YASM/FASM 的不同 -f 选项?我的意思是,那里的马赫格式。
  • 所有的-f macho, elf

标签: macos gcc assembly yasm


【解决方案1】:

我一直在使用yasm 为 OS X 开发汇编语言。组装线是:

yasm -f macho64 -l file.lst file.asm

然后链接ld:

ld -o file file.o

您应该在 OS X 上使用 start 而不是 _start

由于使用gdb有问题。似乎没有任何可用于 OS X 的调试格式。

您可能有兴趣尝试我的名为 ebe 的 IDE。我已经设法让断点和调试工作得很好。目前我在调试浮点代码时遇到了一些问题。 gdb 似乎不知道 ymm 寄存器,并且 gdb 的默认版本将 xmm 寄存器的部分向后。

你可能喜欢 ebe。使用

从 sourceforge 下载它
git clone git://git.code.sf.net/p/ebe-ide/code ebe

它需要 xterm、python、tkinter 和 pmw(一个 python 库)。

【讨论】:

    【解决方案2】:

    这里有几个问题:

    1) OS X 不支持 ELF,仅支持 Mach-O。

    2) 您的链接器正在寻找 x86_64 架构的 Mach-O,但没有找到。

    ld: warning: ignoring file example.o, file was built for unsupported file format ( 0x7f 0x45 0x4c 0x46 0x 2 0x 1 0x 1 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 ) which is not the architecture being linked (x86_64): example.o

    确保您正在为 x86_64 进行组装,否则尝试将 -m32 传递给 GCC 以使用 x86。

    3) 您的链接器正在尝试包含 C 运行时库 crt1.10.6.o,这看起来不像您想要的,因为您没有定义 _main 函数。也许您应该直接调用链接器而不是通过 GCC 调用它,或者找到正确的标志组合传递给链接器以避免这种情况。

    如果我是你,我会从以下步骤开始:

    a) 用 C 语言编写 hello world,让 clang 输出一个汇编文件 (-s),然后查看是否可以仅使用 clang 进行汇编和链接。这应该很简单。

    b) 一旦成功,使用fileotool 确定您的目标文件应该是什么样子,并尝试使用 yasm 生成它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-23
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多