gpt4 book ai didi

assembly - 汇编程序给出错误 :/a.:无法执行二进制文件:Exec 格式错误

转载 作者:行者123 更新时间:2023-12-02 01:28:40 24 4
gpt4 key购买 nike

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

当我运行时:

as file.s

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

执行时出现此错误:

bash: ./a.out: cannot execute binary file: Exec format error



当我运行时:
file ./a.out 

我得到:

./a.out: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped



我使用的汇编代码是:
.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

最佳答案

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

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

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

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

然后你可以运行它:
./file

关于assembly - 汇编程序给出错误 :/a.:无法执行二进制文件:Exec 格式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35383131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com