在.text节中定义只读数据的原因是什么? [英] What is the reason for having read-only data defined in .text section?

查看:136
本文介绍了在.text节中定义只读数据的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习汇编和低级编程本身,并正在阅读有关此书的.据说在那里我们可以将任何数据放入 elf 文件的 .text 部分中,但是由于页面/段的权限不同,我们当然不能对其进行突变.但这没有被告知,原因是在 .text 部分中包含了数据.许多C ++程序员也告诉我g ++编译器可以放置

I am learning assembly and low-level programming itself and reading a book about it. It is said there that we can put any data inside the .text section of an elf file but of course we can't mutate it because of different permissions of pages/segments. But it was not told there, what was the reason for it, for having data inside .text section. I was also told by many C++ programmers that g++ compiler puts

static const char DATA[] = "SOME DATA";

.text 部分中也

.我想知道,为什么不将这些数据放在 .rodata 部分中,目的是什么?如果使用 .text ,那么在 .rodata 中存储什么?

inside the .text section too. I wonder, why not to put this data inside .rodata section, what is the purpose? And if .text is used, what to store in the .rodata then?

主要问题是有关长时间模式下的行为.

The main question is about this behaviour in long mode.

推荐答案

传统上,出于以下两个原因,将只读数据放置在文本部分中:

Traditionally, read-only data was placed in the text section for two reasons:

  • 该文本部分不可写,因此内存保护可以捕获对只读数据的意外写入,从而使程序崩溃
  • 使用内存管理单元(MMU),同一进程的多个实例可以共享文本节的一个副本(因为它保证在程序的所有实例中都是相同的),从而节省了内存

在ELF目标上,对该方案进行了一些修改.现在,只读数据被放置在新的.rodata部分中,该部分与.text部分类似,但是它也无法执行,从而阻止了某些攻击向量.优点仍然存在.

On ELF targets, this scheme was modified a bit. Read-only data is now placed in the new .rodata section which is like the .text section except it also cannot be executed, preventing certain attack vectors. The advantages remain.

这篇关于在.text节中定义只读数据的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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