简体   繁体   English

nasm,64,Linux,分段故障核心已转储

[英]nasm , 64 ,linux, segmentation fault core dumped

this is foo.asm 这是foo.asm

extern choose;
[section .data]
num1st dq 3
num2nd dq 4
[section .text]
global main
global myprint
main:
  push qword [num2nd]
  push qword [num1st]
  call choose
  add esp,8
  mov ebx,0
  mov eax,1
  int 0x80
  ;  pop qword [num1st]
  ;  pop qword [num2nd]
myprint:
  mov edx,[esp+8]
  mov ecx,[esp+4]
  mov ebx,1
  mov eax,4
  int 0x80
  ;  pop qword [num1st]
  ;  pop qword [num2nd]
  ret

it is a C-asm-program 这是一个C-asm程序

this is bar.c 这是bar.c

void myprint(char * msg ,int len);
int choose(int a,int b) 
{ 
  if (a>=b){
    myprint("the 1st one\n",13);}
  else {
    myprint("the 2nd one\n",13);}
  return 0;
}

nasm -f elf64 foo.asm nasm -f elf64 foo.asm

gcc -c bar.c gcc -c bar.c

gcc -s -o foobar bar.o foo.o gcc -s -o foobar bar.o foo.o

./foobar ,it says segmentation fault core dumped ./foobar,它表示分段故障核心已转储

I use gdb to debug ,but it says missing debuginfo-install, I am also trying to install it. 我使用gdb进行调试,但是它说缺少debuginfo-install,我也在尝试安装它。

maybe the problem has sth to do with the 86_64 arch... 也许问题与86_64拱门有关...

Segmentation fault when pushing on stack (NASM) after watched this link,I add some 'pop' into it but it doesn't work 观看此链接后在推入堆栈(NASM)时出现分段错误 ,我向其中添加了一些“弹出”按钮,但它不起作用

Arguments are not passed on the stack in 64-bit mode , unless you have more than 6 of them. 除非您有超过6个参数,否则不会以64位模式在堆栈上传递参数。 The first two arguments will be in RDI and RSI . 前两个参数将在RDIRSI

There's also a difference in how you should use system calls in 64-bit mode. 在64位模式下使用系统调用的方式也有所不同。 The syscall number and arguments should be placed in the following registers ( source ): 系统调用编号和参数应放在以下寄存器( )中:

syscall nr  rax
arg 1       rdi
arg 2       rsi
arg 3       rdx
arg 4       r10
arg 5       r9
arg 6       r8

And the sys_write syscall number in 64-bit mode is 1, not 4 . 在64位模式下, sys_write syscall编号为1,而不是4 Also, instead of int 0x80 you should use syscall . 另外, 应该使用syscall而不是int 0x80 Performing syscalls with int 0x80 might work in 64-bit mode depending on how your kernel has been configured, but you still need to consider how function arguments are passed. 使用int 0x80执行系统调用可能会在64位模式下工作,具体取决于内核的配置方式,但是您仍然需要考虑如何传递函数参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM