【问题标题】:unable to compile assembly: /usr/bin/ld: i386 architecture of input file `array1.o' is incompatible with i386:x86-64 output [duplicate]无法编译程序集:/usr/bin/ld:输入文件“array1.o”的 i386 体系结构与 i386:x86-64 输出不兼容 [重复]
【发布时间】:2015-09-30 22:09:39
【问题描述】:

我正在使用 Kali linux 64 位,我正在尝试执行 Paul Carter 博士网站上的以下程序。 gcc 命令给出错误。我应该在 gcc 命令中使用什么?

nasm -f elf32 array1.asm
root@kali:assembly# gcc -o array1 array1.o array1c.c
array1c.c:9:1: warning: ‘cdecl’ attribute ignored [-Wattributes]
array1c.c:10:1: warning: ‘cdecl’ attribute ignored [-Wattributes]
/usr/bin/ld: i386 architecture of input file `array1.o' is    incompatible    with i386:x86-64 output
collect2: error: ld returned 1 exit status

【问题讨论】:

  • 以 root 身份尝试汇编语言是一个糟糕的想法。 apple.stackexchange.com/a/192422/118588 很好地解释了对所有事情都使用 root 的陷阱,这适用于所有 Unix 操作系统,而不仅仅是 OS X。在尝试 ASM 时更糟,因为您可能不小心进行了意外的系统调用,从而重新启动了系统,或更糟。
  • 另请参阅stackoverflow.com/questions/36861903/…,了解使用或不使用 libc 从 asm 源构建 32 位或 64 位二进制文​​件的各种方法的更完整指南。

标签: gcc assembly x86 nasm


【解决方案1】:

您正在尝试将 32 位目标文件 i386 链接到 64 位可执行文件 (i386:x86-64)。将-m32 添加到gcc 编译行以创建32 位可执行文件。

【讨论】:

  • 添加 -m32 无效.. gcc -m32 array1.o -o array1 /usr/bin/ld: 找不到 crt1.o: 没有这样的文件或目录 /usr/bin/ld:找不到 crti.o:没有这样的文件或目录 /usr/bin/ld:在搜索 -lgcc /usr/bin/ld 时跳过不兼容的 /usr/lib/gcc/x86_64-linux-gnu/4.7/libgcc.a:找不到-lgcc /usr/bin/ld:在搜索-lgcc_s /usr/bin/ld时跳过不兼容的/usr/lib/gcc/x86_64-linux-gnu/4.7/libgcc_s.so:找不到-lgcc_s /usr/ bin/ld: 找不到 -lc .....
  • 你需要安装32位glibc开发文件供gcc链接。
【解决方案2】:
nasm -f elf64 array1.asm

然后

ld -s -o array1 array1.o

【讨论】:

    【解决方案3】:

    首先安装这个:

    sudo apt-get install gcc-multilib g++-multilib
    

    然后以这种方式组装和链接:

    nasm -f elf array1.asm -o array1.o
    

    最后,

    gcc -m32 array1.o -o array1.out
    

    然后运行,

    ./array1.out
    

    这应该工作......

    【讨论】:

      【解决方案4】:

      (哎呀,我只是略过了这个问题,并认为您正在制作一个仅使用 ld 的独立可执行文件。请参阅 cad 对 gcc -m32 的回答,因为当您确实想与 libc 以及所有这些链接时,而不仅仅是独立尝试一些小实验。)

      您必须告诉ld 您希望输出用于哪台机器。默认为原生类型。

      nasm -f elf32 array1.asm  # or yasm
      ld -m elf_i386 array1.o -o 32bit-array1
      

      不幸的是,很多 asm 指南/资源仍然有 32 位 x86 代码的示例。

      【讨论】:

      • 嗨,谢谢,但我遇到了一大堆新问题
      • ld:警告:找不到条目符号_start;默认为0000000008048080 array1.o:在函数init_loop': array1.asm:(.text+0x1d): undefined reference to puts'array1.o:在函数Prompt_loop': array1.asm:(.text+0x37): undefined reference to printf'array1.asm:(.text+0x46):未定义引用scanf' array1.asm:(.text+0x53): undefined reference to dump_line'array1.o:在函数中InputOK': array1.asm:(.text+0x6a): undefined reference to printf' array1.asm:(.text+0x77): 未定义引用puts' array1.o: In function print_loop':
      • 您的代码定义了main,而不是_start,并且使用了库函数,所以您只需使用gcc。我的回答只有在您编写一个直接进行系统调用的独立程序时才有用。
      猜你喜欢
      • 2020-06-18
      • 2011-07-04
      • 2013-10-12
      • 2019-01-18
      • 2011-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多