gpt4 book ai didi

linux - 使用 GNU 汇编程序进行系统调用时出现段错误

转载 作者:太空宇宙 更新时间:2023-11-04 09:16:25 24 4
gpt4 key购买 nike

我试图理解为什么在汇编级别执行系统调用时 as 的行为与 nasm 不同。因为我是惩罚的贪吃者,所以我使用的是 Intel 语法。这是我的程序:

    .intel_syntax noprefix
.section .rodata
.LC0:
.string "Hello world!\n"
.text
.globl _start
.type _start, @function
_start:
mov edx, 13
mov ecx, OFFSET FLAT:.LC0
mov eax, 4
int 0x80
ret

我使用 as -o prog.o prog.s 进行汇编,并使用 ld -s -o prog prog.o 进行链接。

但是当我运行它时,我得到:

$ ./prog 
Hello world!
Segmentation fault (core dumped)

GDB 在这里不是特别有用。当我在 retstepi 时,它说 Cannot access memory at address 0x1。这令人费解,因为 ESP 的值是:

(gdb) info registers esp
info registers esp
esp 0xbffff660 0xbffff660

为什么这个程序会出现段错误?

最佳答案

因为它永远不会正常退出。 _start 没有父堆栈帧,因此从它返回会导致崩溃。

您可以从 main 返回,让标准库的 _start 实现为您调用 exit,但是如果您正在编写自己的 _start,你需要自己调用exit,因为没有父栈帧可以返回。

关于linux - 使用 GNU 汇编程序进行系统调用时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47169310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com