我遇到了一个有趣的问题。我忘记了我使用的是 64 位机器和操作系统并编写了 32 位汇编代码。我不知道如何编写 64 位代码。
这是 Linux 上 Gnu Assembler(AT&T 语法)的 x86 32 位汇编代码。
//hello.S
#include <asm/unistd.h>
#include <syscall.h>
#define STDOUT 1
.data
hellostr:
.ascii "hello wolrd\n";
helloend:
.text
.globl _start
_start:
movl $(SYS_write) , %eax //ssize_t write(int fd, const void *buf, size_t count);
movl $(STDOUT) , %ebx
movl $hellostr , %ecx
movl $(helloend-hellostr) , %edx
int $0x80
movl $(SYS_exit), %eax //void _exit(int status);
xorl %ebx, %ebx
int $0x80
ret
现在,这段代码应该在 32 位处理器和 32 位操作系统上运行良好,对吧?众所周知,64 位处理器向后兼容 32 位处理器。所以,这也不是问题。问题的出现是因为 64 位操作系统和 32 位操作系统中的系统调用和调用机制不同。我不知道为什么,但他们在 32 位 linux 和 64 位 linux 之间更改了系统调用号。
asm/unistd_32.h 定义:
#define __NR_write 4
#define __NR_exit 1
asm/unistd_64.h 定义:
#define __NR_write 1
#define __NR_exit 60
无论如何,使用宏而不是直接数字是有回报的。它确保正确的系统调用号码。
当我组装&链接&运行程序时。
$cpp hello.S hello.s //pre-processor
$as hello.s -o hello.o //assemble
$ld hello.o // linker : converting relocatable to executable
它不打印helloworld
。
在 gdb 中显示:
- 程序以代码 01 退出。
我不知道如何在 gdb 中调试。使用教程我尝试调试它并在每一步通过指令检查寄存器执行指令。它总是向我显示“程序以 01 退出”。如果有人能告诉我如何调试它,那就太好了。
(gdb) break _start
Note: breakpoint -10 also set at pc 0x4000b0.
Breakpoint 8 at 0x4000b0
(gdb) start
Function "main" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Temporary breakpoint 9 (main) pending.
Starting program: /home/claws/helloworld
Program exited with code 01.
(gdb) info breakpoints
Num Type Disp Enb Address What
8 breakpoint keep y 0x00000000004000b0 <_start>
9 breakpoint del y <PENDING> main
我试着跑步strace
。这是它的输出:
execve("./helloworld", ["./helloworld"], [/* 39 vars */]) = 0
write(0, NULL, 12 <unfinished ... exit status 1>
- 解释一下
write(0, NULL, 12)
strace输出中系统调用的参数? - 到底发生了什么?我想知道为什么它以exitstatus = 1退出的原因?
- 有人可以告诉我如何使用 gdb 调试这个程序吗?
- 他们为什么要更改系统调用号码?
- 请适当更改此程序,以便它可以在这台机器上正确运行。
编辑:
阅读 Paul R 的回答后。我检查了我的文件
claws@claws-desktop:~$ file ./hello.o
./hello.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
claws@claws-desktop:~$ file ./hello
./hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
我同意他的观点,这些应该是 ELF 32 位可重定位和可执行的。但这并不能回答我的问题。我所有的问题仍然是问题。在这种情况下到底发生了什么?有人可以回答我的问题并提供此代码的 x86-64 版本吗?