为什么汇编程序有单独的段?

发布于 2024-12-10 12:24:59 字数 158 浏览 0 评论 0原文

为什么汇编程序将段(.data/.bss.text)加载到单独的内存块中,而不是同时加载数据和代码分段成单个内存块?

我的猜测是,操作系统可以移动这些段,或者以某种方式针对存储的数据类型优化内存。想法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人海汹涌 2024-12-17 12:24:59

这不仅限于汇编程序,它是操作系统的可执行格式的布局方式,并且大多数操作系统已决定采用相当广泛的可执行文件格式,将程序的各个部分分成部分(“段”)。

将可执行文件分成各个部分有几个优点,例如您提到的那些:

.bss:存储有关在程序启动时需要清零的内存的信息。需要清零的内存很常见,操作系统通常具有用于分配清零内存的特殊服务,如果您碰巧分配了 1Mb 的全局数组,则不需要在可执行文件中嵌入 1Mb 的 0 - 您可以只需在 .bss 部分中编码该信息,操作系统将在程序启动时分配该 1Mb。

.data:这是在程序启动时初始化为非零的所有数据。

.text:这是实际的代码

,还可以有更多的部分,例如包含需要运行以初始化程序但可以在运行后丢弃的引导代码的特殊部分,或包含调试信息的部分(不需要除非您在调试器中运行该程序,否则将被加载到内存中)。另一个常见的部分是只读数据部分:

.rodata:包含不可写数据,例如程序中的所有字符串或常量数据。

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

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.

﹏半生如梦愿梦如真 2024-12-17 12:24:59

通常,您可以逐段设置属性。例如,只读段允许您指定“只读”一次,然后将只读数据放入该段中,而不是逐个变量地指定只读。

You can normally set attributes on a segment-by-segment basis. For example, a read-only segment lets you specify "read-only" once, and just put read-only data into that segment, rather than specifying read-only on a variable-by-variable basis.

心是晴朗的。 2024-12-17 12:24:59

不,我认为您理解错误,分段概念不仅用在汇编中,每种机器特定的高级语言(如 C、C++、Basic、Pascal 等)在内部都使用段。

因为每个 C 和 C++ 程序都由 GNU 编译器编译为汇编语言(在 Ubuntu 的情况下),编译器会将适当的数据放入适当的段,并且 GAS 汇编器会将此汇编转换为目标代码字节。

No, I think you understood wrong, segmentation concept in not only used in assembly, every machine specific high level languages (like C, C++, Basic, Pascal, etc.) use segments internally.

Because every C and C++ programs are compiled to assembly language by GNU compiler (in the case of Ubuntu), the compiler will put appropriate datas to appropriate segments and the GAS assembler will convert this assembly to object code bytes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文