gpt4 book ai didi

linux - 在 64 位 Linux 和 64 位处理器上运行 32 位汇编代码 : Explain the anomaly

转载 作者:IT王子 更新时间:2023-10-29 00:25:33 24 4
gpt4 key购买 nike

我遇到了一个有趣的问题。我忘记了我正在使用 64 位机器和操作系统并编写了 32 位汇编代码。我不知道如何编写 64 位代码。

这是 Linux 上 Gnu 汇编程序(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>
  1. 解释一下strace输出中write(0, NULL, 12)系统调用的参数?
  2. 到底发生了什么?我想知道它以 exitstatus=1 退出的确切的原因?
  3. 有人可以告诉我如何使用 gdb 调试这个程序吗?
  4. 他们为什么要更改系统调用号码?
  5. 请适当修改此程序,使其能在 native 上正常运行。

编辑:

阅读 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 版本吗?

最佳答案

请记住,默认情况下,64 位操作系统上的所有内容都倾向于采用 64 位。您需要确保您 (a) 在适当的地方使用 #includes 的 32 位版本 (b) 与 32 位库链接,以及 (c) 构建 32 位可执行文件。如果您显示了您的 makefile 的内容(如果您有的话),或者显示您用于构建此示例的命令,这可能会有所帮助。

FWIW 我稍微更改了您的代码(_start -> main):

#include <asm/unistd.h>
#include <syscall.h>
#define STDOUT 1

.data
hellostr:
.ascii "hello wolrd\n" ;
helloend:

.text
.globl main

main:
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

并像这样构建它:

$ gcc -Wall test.S -m32 -o test

确认我们有一个 32 位可执行文件:

$ file test
test: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.4, dynamically linked (uses shared libs), not stripped

它似乎运行正常:

$ ./test
hello wolrd

关于linux - 在 64 位 Linux 和 64 位处理器上运行 32 位汇编代码 : Explain the anomaly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2500362/

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