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

查看:31
本文介绍了.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 部分.

It seems the label main is in the .data section.

它会导致不允许在 .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 部分.(读取 + 执行)

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

令人惊讶的是,在 GNU/Linux 系统上,手写 asm 通常会生成可执行的 .data,除非您小心地避免这种情况,因此这通常不是真正的问题:参见 为什么数据和堆栈段是可执行的? 但是将代码放在 中.文本它所属的地方可以让一些调试工具更好地工作.

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天全站免登陆