在Linux的64位程序的命令行 [英] Process command line in Linux 64 bit

查看:189
本文介绍了在Linux的64位程序的命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在访问来自Linux的64位汇编程序过程中的命令行的问题。要重现此以最小的code,我做它打印的程序名的前5个字符这32位程序:

I have problems accessing the process command line from Linux 64 bit Assembly program. To reproduce this with minimal code, I made this 32-bit program which prints first 5 characters of the program name:


.section .text

.globl _start
_start:
 movl  %esp, %ebp

 movl $4, %eax        # write
 movl $1, %ebx        # stdout
 movl 4(%ebp), %ecx   # program name address (argv[0])
 movl $5, %edx        # hard-coded length
 int  $0x80

 movl $1, %eax
 movl $0, %ebx
 int  $0x80

此程序工作。当我把它翻译为64位和Linux的64运行,它不打印任何东西:

This program is working. When I translate it to 64 bit and run in Linux 64, it doesn't print anything:


.section .text

.globl _start
_start:
 movq  %rsp, %rbp

 movq $4, %rax
 movq $1, %rbx
 movq 8(%rbp), %rcx       # program name address ?
 movq $5, %rdx
 int  $0x80

 movq $1, %rax
 movq $0, %rbx
 int  $0x80

在哪里是我的错?

Where is my mistake?

推荐答案

您加载正确的地址为%RCX

INT 0x80的然后调用32位的系统调用接口。截断的地址为32位,这使得它不正确。 (如果你使用调试器,只是之后的第一个 INT 0x80的,你会看到,它在-14 返回%EAX ,这是 -EFAULT

int 0x80 then invokes the 32-bit syscall interface. That truncates the address to 32 bits, which makes it incorrect. (If you use a debugger and set a breakpoint just after the first int 0x80, you will see that it returns with -14 in %eax, which is -EFAULT.)

第二个系统调用,退出,工程确定,因为截断到32位没有做任何伤害在这种情况下。

The second syscall, exit, works OK because the truncation to 32 bits doesn't do any harm in that case.

如果你想传递一个64位的地址,系统调用,你将不得不使用64位系统调用接口:

If you want to pass a 64-bit address to a system call, you will have to use the 64-bit syscall interface:

  • 使用系统调用,不是 INT 0x80的;
  • 在不同的寄存器用于:看这里;
  • 系统调用号不同,以及:请参见这里
  • use syscall, not int 0x80;
  • different registers are used: see here;
  • the system call numbers are different as well: see here.

下面是你的code的工作版本:

Here is a working version of your code:

.section .text

.globl _start
_start:
 movq  %rsp, %rbp

 movq $1, %rax
 movq $1, %rdi
 movq 8(%rbp), %rsi       # program name address ?
 movq $5, %rdx
 syscall

 movq $60, %rax
 movq $0, %rdi
 syscall

这篇关于在Linux的64位程序的命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆