文本段如何设置为只读? [英] How is text segment made read-only?

查看:60
本文介绍了文本段如何设置为只读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道该文本段是只读段,尝试对其进行写入会导致总线错误".我对如何将此段设置为只读感到好奇.

I know that text segment is a read-only segment and trying to write to it results in "Bus error". I am curious about how this segment is made read-only.

由于物理内存不是只读的,因此必须在分页期间完成.

Since the physical memory is not readonly, this must be done during paging.

为文本段设置的只读页面的内存的每一页是否都有位?

Is there a bit for each page of memory for read-only pages which is set for text segment?

推荐答案

通常,发生这种情况的一种方法是程序加载器为程序文本分配内存,从可执行文件中读取程序文本,并要求操作系统执行以下操作:使该内存为只读.操作系统将其记录在其数据库中,并将信息传达给硬件,以告知其不允许对该内存进行写操作.

Generally, one way this happens is the program loader allocates memory for the program text, reads program text into it from the executable file, and asks the operating system to make that memory read-only. The operating system records this in its databases and conveys the information to the hardware to tell it not to allow writes to that memory.

另一种可能性是,加载程序实际上并未将程序文本读入内存,而只是向操作系统发出请求,以标记可执行文件的适当部分已映射"到内存中,为只读.当进程实际上尝试执行内存页中的代码时,操作系统会将其读入内存(具有自身的写访问权限,因此可以执行此操作),并将其标记为只读(针对进程).

Another possibility is that the loader does not actually read the program text into memory but merely issues a request to the operating system to mark that the appropriate portion of the executable file is "mapped" into memory, as read-only. When the process actually tries to execute the code in a memory page, the operating system will read it into memory (with write access for itself, so it can do this) and mark it read-only (for the process).

后一种情况是一种优化,因为它允许您在不先加载所有程序的情况下启动程序.而且其中从未使用过的部分,例如处理罕见错误的代码,可能永远也不会加载.

This latter case is an optimization because it allows your program to start without loading all of it first. And parts of it that are never used, such as code to handle rare errors, might never be loaded.

这篇关于文本段如何设置为只读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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