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

前端之家收集整理的这篇文章主要介绍了在64位Linux和64位处理器上运行32位汇编代码:解释异常前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在一个有趣的问题.我忘了我使用64位机器&操作系统并写了一个32位汇编代码.我不知道如何写64位代码.

这是Linux上Gnu Assembler(AT& T语法)的x86 32位汇编代码.

  1. //hello.S
  2. #include <asm/unistd.h>
  3. #include <syscall.h>
  4. #define STDOUT 1
  5.  
  6. .data
  7. hellostr:
  8. .ascii "hello wolrd\n";
  9. helloend:
  10.  
  11. .text
  12. .globl _start
  13.  
  14. _start:
  15. movl $(SYS_write),%eax //ssize_t write(int fd,const void *buf,size_t count);
  16. movl $(STDOUT),%ebx
  17. movl $hellostr,%ecx
  18. movl $(helloend-hellostr),%edx
  19. int $0x80
  20.  
  21. movl $(SYS_exit),%eax //void _exit(int status);
  22. xorl %ebx,%ebx
  23. int $0x80
  24.  
  25. ret

现在,这个代码应该在32位处理器上运行良好32位操作系统?我们知道64位处理器向后兼容32位处理器.所以,这也不会是一个问题.出现这个问题是因为系统调用和呼叫机制在64位操作系统32位操作系统.我不知道为什么,但是他们改变了32位linux& 64位linux.

asm / unistd_32.h定义:

  1. #define __NR_write 4
  2. #define __NR_exit 1

asm / unistd_64.h定义:

  1. #define __NR_write 1
  2. #define __NR_exit 60

无论如何,使用宏而不是直接数字是有偿的.确保正确的系统呼叫号码.

当我组装&链接&运行程序.

  1. $cpp hello.S hello.s //pre-processor
  2. $as hello.s -o hello.o //assemble
  3. $ld hello.o // linker : converting relocatable to executable

它不打印helloworld.

在gdb中显示

>程序退出代码01.

我不知道如何在gdb中进行调试.使用教程我试图调试它,并在每个步骤执行指令检查寄存器的指令.它总是显示“程序退出01”.如果有的话可以告诉我如何调试这将是很棒的.

  1. (gdb) break _start
  2. Note: breakpoint -10 also set at pc 0x4000b0.
  3. Breakpoint 8 at 0x4000b0
  4. (gdb) start
  5. Function "main" not defined.
  6. Make breakpoint pending on future shared library load? (y or [n]) y
  7. Temporary breakpoint 9 (main) pending.
  8. Starting program: /home/claws/helloworld
  9.  
  10. Program exited with code 01.
  11. (gdb) info breakpoints
  12. Num Type Disp Enb Address What
  13. 8 breakpoint keep y 0x00000000004000b0 <_start>
  14. 9 breakpoint del y <PENDING> main

我试过运行strace.这是它的输出

  1. execve("./helloworld",["./helloworld"],[/* 39 vars */]) = 0
  2. write(0,NULL,12 <unfinished ... exit status 1>

>在strace的输出中解释write(0,12)系统调用的参数?
>究竟发生了什么?我想知道为什么完全退出exitstatus = 1的原因?
有人可以告诉我如何使用gdb来调试这个程序?
>为什么要更改系统调用号码?
>请妥善更改此程序,以便在本机上正常运行.

编辑:

阅读保罗·R的答案后.我检查了我的文件

  1. claws@claws-desktop:~$file ./hello.o
  2. ./hello.o: ELF 64-bit LSB relocatable,x86-64,version 1 (SYSV),not stripped
  3.  
  4. claws@claws-desktop:~$file ./hello
  5. ./hello: ELF 64-bit LSB executable,statically linked,not stripped

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

解决方法

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

FWIW我改变了你的代码(_start – > main):

  1. #include <asm/unistd.h>
  2. #include <syscall.h>
  3. #define STDOUT 1
  4.  
  5. .data
  6. hellostr:
  7. .ascii "hello wolrd\n" ;
  8. helloend:
  9.  
  10. .text
  11. .globl main
  12.  
  13. main:
  14. movl $(SYS_write),%ebx
  15. int $0x80
  16.  
  17. ret

并建立如下:

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

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

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

它似乎运行正常:

  1. $./test
  2. hello wolrd

猜你在找的Linux相关文章