在64位Linux和64位处理器上运行32位汇编代码:解释异常

cla*_*aws 14 linux x86 assembly gdb x86-64

我遇到了一个有趣的问题.我忘了我正在使用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
Run Code Online (Sandbox Code Playgroud)

现在,这个代码应该在32位处理器和32位操作系统上正常运行吗?我们知道64位处理器向后兼容32位处理器.所以,这也不是问题.问题出现是因为64位操作系统和32位操作系统中的系统调用和调用机制不同.我不知道为什么但是他们改变了32位linux和64位linux之间的系统调用号码.

asm/unistd_32.h定义:

#define __NR_write        4
#define __NR_exit         1
Run Code Online (Sandbox Code Playgroud)

asm/unistd_64.h定义:

#define __NR_write              1
#define __NR_exit               60
Run Code Online (Sandbox Code Playgroud)

无论如何使用宏而不是直接数字都可以获得回报.它确保正确的系统呼叫号码.

当我组装和链接并运行程序时.

$cpp hello.S hello.s //pre-processor
$as hello.s -o hello.o //assemble
$ld hello.o // linker : converting relocatable to executable
Run Code Online (Sandbox Code Playgroud)

它不打印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
Run Code Online (Sandbox Code Playgroud)

我试过跑步strace.这是它的输出:

execve("./helloworld", ["./helloworld"], [/* 39 vars */]) = 0
write(0, NULL, 12 <unfinished ... exit status 1>
Run Code Online (Sandbox Code Playgroud)
  1. write(0, NULL, 12)在strace的输出中解释系统调用的参数?
  2. 什么究竟是怎么回事?我想知道为什么的原因正是它与退出状态= 1退出?
  3. 有人可以告诉我如何使用gdb调试这个程序吗?
  4. 他们为什么要更改系统呼叫号码?
  5. 请妥善更改此程序,以便它可以在此计算机上正常运行.

编辑:

在阅读了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
Run Code Online (Sandbox Code Playgroud)

我同意他的说法,这些应该是ELF 32位可重定位和可执行文件.但这并不能回答我的问题.我的所有问题仍然存在问题.在这种情况下究竟发生了什么?有人可以回答我的问题并提供此代码的x86-64版本吗?

Pau*_*l R 8

请记住,默认情况下,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
Run Code Online (Sandbox Code Playgroud)

并像这样构建它:

$ gcc -Wall test.S -m32 -o test
Run Code Online (Sandbox Code Playgroud)

验证我们有一个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
Run Code Online (Sandbox Code Playgroud)

它似乎运行正常:

$ ./test
hello wolrd
Run Code Online (Sandbox Code Playgroud)

  • @claws:看起来你的系统上没有安装32位gcc或libs.您需要为系统安装32位开发包才能使用-m32 (2认同)

Chr*_*odd 6

正如Paul所说,如果你想在64位系统上构建32位二进制文​​件,你需要使用-m32标志,默认情况下你的安装可能不可用(某些64位Linux发行版没有默认包括32位编译器/链接器/ lib支持).

另一方面,您可以将代码构建为64位,在这种情况下,您需要使用64位调用约定.在这种情况下,系统调用号在%rax中,参数在%rdi,%rsi和%rdx中

编辑

我找到的最好的地方是www.x86-64.org,特别是abi.pdf