在x64环境中与GCC和x86 ASM链接

时间:2014-08-22 04:01:15

标签: c gcc assembly linker

我正在运行Windows Ultimate x64(Core i5),并在Cygwin上使用x64 GCC编译器。我想通过链接GCC C库(通过Cygwin安装)创建一个简单的“Hello,world”程序。我正在使用NASM并尝试使用以下代码的ELF32和Win32格式:

;;
;; hello.asm - use the C library to output hello world
;;

; In case I wanted to use the code for Windows later
%if 0
%define main    _main
%define printf  _printf
%endif

global main
extern printf

; we set up our string here
section .data
    formatstr:  db 'hello, world!', 10, 0

section .text

    main:

    sub     esp, 4

    lea     eax, [formatstr]
    mov     [esp], eax
    call    printf

    add     esp, 4
    mov     eax, 0

    ret

使用nasm -f elf32 hello.asmnasm -f win32 hello.asm以及gcc hello.o -o hello时,GCC会告诉我:

/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: i386 architecture of input file `hello.o' is incompatible with i386:x86-64 output
collect2: error: ld returned 1 exit status

我的谷歌搜索引导我为Cygwin安装cygwin32软件包,所以我已经做到了。

当我查看如何在32位模式下运行GCC时,我添加了m32开关,但这导致ELF和Win32的错误输出更多:

/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3//libgcc_s.dll.a when searching for -lgcc_s
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3/libgcc_s.dll.a when searching for -lgcc_s
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3//libgcc_s.dll.a when searching for -lgcc_s
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3/libgcc_s.dll.a when searching for -lgcc_s
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lgcc_s
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3//libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3//libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3/libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3/libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3//libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3/libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3/../../../libcygwin.a when searching for -lcygwin
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3/../../../libcygwin.a when searching for -lcygwin
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3/../../../libcygwin.a when searching for -lcygwin
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lcygwin
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: cannot find -ladvapi32
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lshell32
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: cannot find -luser32
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lkernel32
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3//libgcc_s.dll.a when searching for -lgcc_s
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3/libgcc_s.dll.a when searching for -lgcc_s
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3//libgcc_s.dll.a when searching for -lgcc_s
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3/libgcc_s.dll.a when searching for -lgcc_s
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lgcc_s
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3//libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3//libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3/libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3/libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3//libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/            lib/gcc/x86_64-pc-cygwin/4.8.3/libgcc.a when searching for -lgcc
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lgcc
collect2: error: ld returned 1 exit status

我对装配和消化ABI还是一个新手,所以我不太清楚如何继续这里。我觉得我错过了一些非常简单的东西,因为我已经成功链接了我过去为C项目编写的汇编代码。

1 个答案:

答案 0 :(得分:0)

感谢Adam Rosenfield指出Cygwin邮件列表帖子,我继续尝试用x64支持重写我的ASM。这确实允许我输出一个带GCC的二进制文件。我应该注意到我的程序会导致分段错误,但这是一个我不太了解x64汇编程序或相应的调用约定的问题。