首页 > 解决方案 > 尝试调试简单的 nasm 汇编程序时如何修复“sigsegv”

问题描述

我在win10 64位家庭主机上运行一个虚拟机。在虚拟机上我安装了 ubuntu 18.04 64 位。

我从与我从“Assembly language step-by-step”学习的书相关的站点下载了一个示例汇编程序。

这是程序:

; vim: ft=nasm
;
; Build using these commands (works on 32-bit Linux)
;    nasm -f elf -g -F stabs eatsyscall.asm
;    ld -o eatsyscall eatsyscall.o
;
; Build on 64-bit Linux: (Linux 3.13.7-1-ARCH #1 x86_64 GNU/Linux)
;    nasm -f elf64 -g -F stabs eatsyscall.asm
;    ld -o eatsyscall eatsyscall.o
;
; Build on OSX (although the instructions are not valid for its architecture)
;    nasm -f macho eatsyscall.asm 
;    ld -arch i386 -macosx_version_min 10.5 -no_pie -e _start -o eatsyscall eatsyscall.o
;

section .data                 ; contains initialized data

EatMsg: db "Eat at Joe's!",10
EatLen  equ $ - EatMsg

section .bss                  ; contains uninitialized data

section .text                 ; contains code

global _start                 ; entry point found by linker (default is _start)

_start:
  nop                         ; needed to allow debugging with gdb - lldb not properly working ATM
  mov eax,4                   ; Specify sys_write syscall
  mov ebx,1                   ; specify file descriptor: stdout
  mov ecx,EatMsg              ; pass message offset
  mov edx,EatLen              ; pass message length
  int 80H                     ; make syscall to output text to stdout

  mov eax,1                   ; specify exit syscall
  mov ebx,0                   ; return code of zero
  int 80H                     ; make syscall to terminate program

当我尝试使用 kdbg 或直接使用 gdb 调试它时,会发生同样的 2 件奇怪的事情(对我来说很奇怪,但可能只是我完全没有经验):

  1. 当我尝试在第 29 行“mov eax, 4”处设置断点时 - 调试器不会停在那里。只有当我从上述行开始一个接一个地放置 3 个断点时,它才会停止。

  2. 当调试器停止时,如果我尝试执行单步执行命令,调试器会告诉我程序以“SIGSEGV”信号终止 - 分段错误。调试程序不会终止,但我正在尝试调试的程序。

我尝试在网上搜索此内容,但找不到与我遇到的问题相关的任何内容。

所有帮助将不胜感激。

标签: linuxassemblyx86-64nasm

解决方案


解决了!

显然问题是使用了错误的调试信息类型。解决这个问题的命令是:

nasm -f elf64 -g -F dwarf eatsyscall.asm -o eatsyscall.o

代替:

nasm -f elf64 -g -F stabs eatsyscall.asm -o eatsyscall.o

这个问题的答案给了我线索: 使用gdb调试时的消息:单步直到从函数_start退出


推荐阅读