【问题标题】:Assembled program gives error: /a.out: cannot execute binary file: Exec format error汇编程序报错:/a.out: cannot execute binary file: Exec format error
【发布时间】:2016-02-13 17:40:41
【问题描述】:

我是 64 位 x86 编码的新手,但我运行的是 Ubuntu 14.04(值得信赖),并且我有一段非常简单的 64 位代码,我使用 as 组装。我得到的输出有奇怪的权限和文件类型。

当我跑步时:

as file.s

我得到一个文件a.out 具有 770 权限。

执行时出现此错误:

bash: ./a.out: 无法执行二进制文件:执行格式错误

当我跑步时:

file ./a.out 

我明白了:

./a.out:ELF 64 位 LSB 可重定位,x86-64,版本 1 (SYSV),未剥离

我使用的汇编代码是:

.section    .data
.LC0:
    .string "/bin/sh"

.LC1:
    .string "/bin/sh"

.LC3:
    .quad .LC1, 0

    .text
    .globl  _start
_start:
.LFB0:

    pushq   %rbp
    movq    %rsp, %rbp

    movq    $59,%rax     # System Call to execve
    movq    $.LC0, %rdi  # Pass program to execute
    movq    $.LC3, %rsi  # Pass command line arguments
    syscall

    movl    $0, %eax
    popq    %rbp
    ret

【问题讨论】:

    标签: assembly x86 ubuntu-14.04 x86-64 gnu-assembler


    【解决方案1】:

    您想先用 GNU Assembler (AS) 汇编程序,然后使用链接器生成最终的可执行文件。为此,您应该能够使用以下内容:

    as file.s -o file.o
    ld file.o -o file
    

    第一个命令告诉汇编器使用-o 选项输出一个名为file.oELF64 对象。第二个命令使用-o 选项将file.o 链接到一个名为file 的64 位ELF 可执行文件。

    64 位系统上的典型默认行为是汇编器和链接器生成 64 位对象和可执行文件。

    然后你可以运行它:

    ./file
    

    【讨论】:

    • 谢谢,我假设它生成了一个它试图链接的 a.out 文件,因为当你没有提供特定名称时,gas 会生成一个 .o 文件。我曾尝试在没有选项的情况下在 a.out 上运行 ld,但同样没有用(我猜是因为它试图读取和写入同一个文件)。无论如何,谢谢你的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    • 2017-01-13
    相关资源
    最近更新 更多