我正在使用GAS程序集并在不使用 gcc 的情况下进行链接。相反,我使用作为汇编程序并使用 ld 进行链接。
我的代码:
.section .text
.globl _start
_start:
xorq %rax, %rax
movb $60, %al
movb $1, %dil
int $0x80
我用来汇编和链接的命令:
as -g -D -o test.o test.S && ld --format elf64-x86-64 -o test test.o
这是 gdb 调试之前/之后:
(gdb) disas _start
Dump of assembler code for function _start:
0x0000000000400078 <+0>: xor %rax,%rax
0x000000000040007b <+3>: mov $0x3c,%al
0x000000000040007d <+5>: mov $0x1,%dil
0x0000000000400080 <+8>: int $0x80
End of assembler dump.
(gdb) b *_start+8
Breakpoint 1 at 0x400080: file test.S, line 7.
(gdb) r
Starting program: /tmp/test
Breakpoint 1, _start () at test.S:7
7 int $0x80
(gdb) i r
rax 0x3c 60
rbx 0x0 0
rcx 0x0 0
rdx 0x0 0
rsi 0x0 0
rdi 0x1 1
rbp 0x0 0x0
rsp 0x7fffffffebc0 0x7fffffffebc0
r8 0x0 0
r9 0x0 0
r10 0x0 0
r11 0x0 0
r12 0x0 0
r13 0x0 0
r14 0x0 0
r15 0x0 0
rip 0x400080 0x400080 <_start+8>
eflags 0x246 [ PF ZF IF ]
cs 0x33 51
ss 0x2b 43
ds 0x0 0
es 0x0 0
fs 0x0 0
gs 0x0 0
(gdb) c
Continuing.
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400082 in ?? ()
(gdb) i r
rax 0x12 18
rbx 0x0 0
rcx 0x0 0
rdx 0x0 0
rsi 0x0 0
rdi 0x1 1
rbp 0x0 0x0
rsp 0x7fffffffebc0 0x7fffffffebc0
r8 0x0 0
r9 0x0 0
r10 0x0 0
r11 0x0 0
r12 0x0 0
r13 0x0 0
r14 0x0 0
r15 0x0 0
rip 0x400082 0x400082
eflags 0x10246 [ PF ZF IF RF ]
cs 0x33 51
ss 0x2b 43
ds 0x0 0
es 0x0 0
fs 0x0 0
gs 0x0 0
(gdb) x/x $rip
0x400082: 0x00000000
(gdb) x/x $rip-2
0x400080 <_start+8>: 0x000080cd
%rip 正在继续他的代码行程到0x40082,其中不存在代码。知道在执行exit()syscall后如何避免SEGFAULT?
添加 支持该计划:
$ strace ./test
execve("./test", ["./test"], [/* 18 vars */]) = 0
exit(1) = 18
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0x12} ---
+++ killed by SIGSEGV (core dumped) +++
Segmentation fault (core dumped)