.text .data和main的分段错误(主要在.data节中) [英] segmentation fault with .text .data and main (main in .data section)

查看:141
本文介绍了.text .data和main的分段错误(主要在.data节中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想将 myarray [0] 的值加载到 eax

I'm just trying to load the value of myarray[0] to eax:

        .text
        .data

        # define an array of 3 words
array_words:     .word 1, 2, 3

        .globl main

main:
        # assign array_words[0] to eax
        mov $0, %edi
        lea array_words(,%edi,4), %eax

但是当我运行此命令时,我不断出现段错误。
有人可以指出我在这里做错了什么吗?

But when I run this, I keep getting seg fault. Could someone please point out what I did wrong here?

推荐答案

似乎标签 main 位于 .data 部分。

这会导致在没有不允许执行 .data 部分中的代码。 (大多数现代系统将 .data 映射为具有读写权限,但没有exec权限。)

It leads to a segmentation fault on systems that doesn't allow to execute code in the .data section. (Most modern systems map .data with read + write but not exec permission.)

程序代码应位于 .text 部分。 (阅读+ exec)

Program code should be in the .text section. (Read + exec)

令人惊讶的是,在GNU / Linux系统上,手写asm通常会生成可执行文件 .data ,除非您请务必避免,因此这通常不是真正的问题:请参阅为什么数据和堆栈段是可执行的吗?但是将代码放在它所属的 .text 中可以使某些调试工具更好地工作。

Surprisingly, on GNU/Linux systems, hand-written asm often results in an executable .data unless you're careful to avoid that, so this is often not the real problem: See Why data and stack segments are executable? But putting code in .text where it belongs can make some debugging tools work better.

此外,您还需要从main ret 退出或致电 exit (或 _exit 系统调用),因此执行不会落在 main 的末尾而不会影响下一个字节。请参阅>如果存在汇编程序中没有退出系统调用?

Also you need to ret from main or call exit (or make an _exit system call) so execution doesn't fall off the end of main into whatever bytes come next. See What happens if there is no exit system call in an assembly program?

这篇关于.text .data和main的分段错误(主要在.data节中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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