为什么汇编程序有不同的细分? [英] Why do assembly programs have separate segments?

查看:123
本文介绍了为什么汇编程序有不同的细分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么汇编程序负载段(。数据 / 的.bss 的.text )到单独的存储器块,而不是两者的数据和code个段加载到单个存储器块

我的猜测是,操作系统就可以左右移动的部分或以某种方式存储数据的类型优化内存。思考?


解决方案

这不限于汇编程序,这对您的操作系统的可执行文件格式是如何布局,大多数操作系统的决定对可执行文件的相当广泛的格式,分离程序的不同部分段(段)。

在各部分分离的可执行具有若干优点,例如那些你提及:

.bss中:关于内存需要在程序启动时被清零存储信息。内存需要归零是常见的,操作系统通常有发放清零的内存特殊服务的,如果你碰巧分配一个1MB的全局数组,你并不需要嵌入0的可执行1MB的 - 你可以只是带code在.bss段的信息,并且OS将分配了1Mb的在程序启动。

。数据:这是公司在程序启动时初始化为零以外的东西所有的数据

的.text:这是实际code

有可能更多的部分,例如含引导code需要运行初始化程序,但一旦它被运行,或包含调试信息的部分(即不需要,除非你运行调试程序被加载到内存中)可以被丢弃特殊路段。另一种常见的部分是只读数据部分:

.RODATA:包含非可写数据,例如在你的程序中的所有字符串或const数据。

此外,CPU可以应用保护存储器,诸如可读/可写/可执行存储器。拥有独立的部分允​​许轻松地应用这些内存保护。例如。在code必须是可执行文件,但其数据是可执行的可能是一个坏主意。
只读部分也可以更容易地其他进程​​之间共享,则code和只读存储器段可以在程序的多个实例之间共享。如果需要文本部分的部分被换出,它们可以只是被丢弃,因为它们已经驻留在可执行文件本身,而数据/ BSS部分可以没有,他们必须被换出到一个特殊的交换区域。

Why do assembly programs load segments (.data/.bss and .text) into separate memory blocks instead of loading both the data and the code segments into a single memory block?

My guess is that the OS can then move the segments around or somehow optimize memory for the type of data stored. Thoughts?

解决方案

This is not limited to assembly programs, it's how the executable format for your OS is laid out, and most OS's have decided to have a rather extensive format for executables, separating various parts of a program into sections("segments").

Separating an executable in various sections have several advantages, e.g. the ones you mention:

.bss: Stores information about memory that needs to be zeroed at program startup. Memory that needs to be zeroed is common, and an OS typically have special services for handing out zeroed memory, and if you happen to allocate a global array of 1Mb, you don't need to embed 1Mb of 0's in the executable - you can just encode that information in the .bss section, and the OS will allocate that 1Mb at program startup.

.data: This is all your data that's initialized to something other than zero at program startup.

.text: this is the actual code

There's can be many more sections, e.g. special sections containing bootstrap code that needs to run to initialize the program but can be discarded once it's been run, or sections containing debug information(that doesn't need to be loaded into memory unless you run the program in a debugger). Another common section is a readonly data section:

.rodata: contains non-writable data, e.g. all the strings or const data in your program.

Moreover, CPUs can apply protection to memory, such as readable/writable/executable memory. Having separate sections allows for easily applying these memory protection. E.g. the code needs to be executable, but having the data be executable might be a bad idea. Read only sections can also more easily be shared among other processes, the code and readonly memory sections can be shared between multiple instances of the program. If parts of the text section needed to be swapped out, they can just be discarded, as they already reside in the executable itself, whereas the data/bss sections cannot, they have to be swapped out to a special swap area.

这篇关于为什么汇编程序有不同的细分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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