且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在.text节中定义只读数据的原因是什么?

更新时间:2023-02-09 16:30:34

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

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.