在OS X Yosemite上编译ELF之类的程序集

时间:2015-01-06 14:23:24

标签: gcc assembly compilation osx-yosemite

在编译器构建项目的上下文中,我曾经能够在此之前编译“ia32”代码:

$ gcc -m32 -ofoo foo.s

与foo.s看起来像:

        .file   "tiger-runtime.c"
        .text
        .globl  tc_init_array
        .type   tc_init_array, @function
tc_init_array:
.LFB0:
        .cfi_startproc
        pushl   %ebp
        .cfi_def_cfa_offset 8
        .cfi_offset 5, -8
        movl    %esp, %ebp
        .cfi_def_cfa_register 5
        subl    $40, %esp
        movl    8(%ebp), %eax
        sall    $2, %eax
        movl    %eax, (%esp)
        call    malloc
        movl    %eax, -16(%ebp)
        movl    $0, -12(%ebp)
        jmp     .L2

现在我搬到Yosemite和最新版本的Xcode,我得到了:

$ gcc-mp-5 -m32 -oadd-ia32 add-ia32.s   
tiger-runtime.c:4:Unknown pseudo-op: .type
tiger-runtime.c:4:Rest of line ignored. 1st junk character valued 116 (t).
tiger-runtime.c:7:Unknown pseudo-op: .cfi_startproc
tiger-runtime.c:9:Unknown pseudo-op: .cfi_def_cfa_offset
tiger-runtime.c:9:Rest of line ignored. 1st junk character valued 56 (8).
tiger-runtime.c:10:Unknown pseudo-op: .cfi_offset
tiger-runtime.c:10:Rest of line ignored. 1st junk character valued 53 (5).
tiger-runtime.c:12:Unknown pseudo-op: .cfi_def_cfa_register

还有更多这样的错误。这个问题已被多次提出,但到目前为止我所看到的答案都没有在优胜美地上发挥作用(例如,mac OSX Unknown pseudo-op: .typeI'm learning x86 assembly on OS X 10.6, how do I compile?)。特别是,即使我使用GCC(通过MacPorts作为GCC 5)作为编译器驱动程序,它似乎使用/usr/bin/as,而不是它自己的汇编程序。

我已经安装了几个版本的GCC / Clang / Binutils(通过MacPorts)来尝试找到合适的装配,但没有成功。充其量,我得到:

$ /opt/local/i386-elf/bin/as add-ia32.s -o add-ia32.o
$ /opt/local/i386-elf/bin/ld add-ia32.o -o add-ia32
/opt/local/i386-elf/bin/ld: warning: cannot find entry symbol _start; defaulting to 00000000
add-ia32.o: In function `tc_init_array':
tiger-runtime.c:(.text+0x10): undefined reference to `malloc'
add-ia32.o: In function `tc_malloc':
tiger-runtime.c:(.text+0x54): undefined reference to `malloc'
tiger-runtime.c:(.text+0x68): undefined reference to `stderr'
tiger-runtime.c:(.text+0x88): undefined reference to `fwrite'
tiger-runtime.c:(.text+0x94): undefined reference to `exit'

我想我离完成还不太远,但是main函数的约定似乎有所不同,并且没有找到libc(-lc没有用)。

此处提供了以前正常工作的程序集文件的完整版本:http://pastebin.com/Xq8DTmf6。提前谢谢。

0 个答案:

没有答案