编译器如何在内存中布局代码 [英] How does compiler lay out code in memory

查看:35
本文介绍了编译器如何在内存中布局代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有一个菜鸟学生的问题.

Ok I have a bit of a noob student question.

所以我很熟悉堆栈包含子程序调用,堆包含可变长度数据结构,全局静态变量被分配到永久内存位置的事实.

So I'm familiar with the fact that stacks contain subroutine calls, and heaps contain variable length data structures, and global static variables are assigned to permanant memory locations.

但在较不理论的层面上,这一切是如何运作的?

But how does it all work on a less theoretical level?

编译器是否只是假设它有一个完整的内存区域,从地址 0 到地址无穷大?然后就开始分配东西?

Does the compiler just assume it's got an entire memory region to itself from address 0 to address infinity? And then just start assigning stuff?

它在哪里布局指令、堆栈和堆?在内存区域的顶部,内存区域的末尾?

And where does it layout the instructions, stack, and heap? At the top of the memory region, end of memory region?

这如何与虚拟内存一起工作?虚拟内存对程序透明?

And how does this then work with virtual memory? The virtual memory is transparent to the program?

抱歉问了一大堆问题,但我正在学习编程语言结构,它一直指的是这些区域,我想在更实用的层面上理解它们.

Sorry for a bajilion questions but I'm taking programming language structures and it keeps referring to these regions and I want to understand them on a more practical level.

提前致谢!

推荐答案

全面的解释可能超出了本论坛的范围.整篇文章都致力于这个主题.然而,在一个简单的层面上,您可以这样看待它.

A comprehensive explanation is probably beyond the scope of this forum. Entire texts are devoted to the subject. However, at a simplistic level you can look at it this way.

编译器不会在内存中布置代码.它确实假设它拥有自己的整个内存区域.编译器生成目标文件,其中目标文件中的符号通常从偏移量 0 开始.

The compiler does not lay out the code in memory. It does assume it has the entire memory region to itself. The compiler generates object files where the symbols in the object files typically begin at offset 0.

链接器负责将目标文件拉到一起,将符号链接到链接对象内的新偏移位置并生成可执行文件格式.

The linker is responsible for pulling the object files together, linking symbols to their new offset location within the linked object and generating the executable file format.

链接器也不在内存中布置代码.它将代码和数据打包成部分,通常标记为 .text 用于可执行代码指令,.data 用于全局变量和字符串常量等.(并且还有其他部分用于不同的目的)链接器可能会向操作系统加载程序提供一个提示,提示将符号重定位到何处,但加载程序不必强制执行.

The linker doesn't lay out code in memory either. It packages code and data into sections typically labeled .text for the executable code instructions and .data for things like global variables and string constants. (and there are other sections as well for different purposes) The linker may provide a hint to the operating system loader where to relocate symbols but the loader doesn't have to oblige.

操作系统加载器解析可执行文件并决定代码和数据在内存中的布局.其中的位置完全取决于操作系统.通常堆栈位于比程序指令和数据更高的内存区域并向下增长.

It is the operating system loader that parses the executable file and decides where code and data are layed out in memory. The location of which depends entirely on the operating system. Typically the stack is located in a higher memory region than the program instructions and data and grows downward.

每个程序在编译/链接时都假设它拥有自己的整个地址空间.这就是虚拟内存的用武之地.它对程序完全透明,完全由操作系统管理.

Each program is compiled/linked with the assumption it has the entire address space to itself. This is where virtual memory comes in. It is completely transparent to the program and managed entirely by the operating system.

虚拟内存的范围通常从地址 0 到平台支持的最大地址(不是无穷大).这个虚拟地址空间被操作系统划分为内核可寻址空间和用户可寻址空间.假设在一个假设的 32 位操作系统上,0x80000000 上面的地址是为操作系统保留的,下面的地址供程序使用.如果程序试图访问此分区上的内存,它将被中止.

Virtual memory typically ranges from address 0 and up to the max address supported by the platform (not infinity). This virtual address space is partitioned off by the operating system into kernel addressable space and user addressable space. Say on a hypothetical 32-bit OS, the addresses above 0x80000000 are reserved for the operating system and the addresses below are for use by the program. If the program tries to access memory above this partition it will be aborted.

操作系统可能会决定堆栈从最高的可寻址用户内存开始,然后随着位于低得多的地址的程序代码向下增长.

The operating system may decide the stack starts at the highest addressable user memory and grows down with the program code located at a much lower address.

堆的位置通常由您构建程序的运行时库管理.它可以从您的程序代码和数据之后的下一个可用地址开始.

The location of the heap is typically managed by the run-time library against which you've built your program. It could live beginning with the next available address after your program code and data.

这篇关于编译器如何在内存中布局代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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