如何像堆,栈,文字的不同环节涉及到的物理内存? [英] How are the different segments like heap, stack, text related to the physical memory?

查看:140
本文介绍了如何像堆,栈,文字的不同环节涉及到的物理内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 在一个C程序进行编译和创建目标文件(ELF)。目标文件中包含不同的部分,如BSS,数据,文字等段。我理解的是,ELF的这些部分是虚拟内存地址空间的一部分。我对吗?请纠正我,如果我错了。

  1. When a C program is compiled and the object file(ELF) is created. the object file contains different sections such as bss, data, text and other segments. I understood that these sections of the ELF are part of virtual memory address space. Am I right? Please correct me if I am wrong.

此外,将有与编译程序相关联的虚拟存储器和页表。页表加载程序时,在ELF虚拟内存地址present关联到真实的物理内存地址。是我的理解是否正确?

Also, there will be a virtual memory and page table associated with the compiled program. Page table associates the virtual memory address present in ELF to the real physical memory address when loading the program. Is my understanding correct?

我看了,在创建ELF文件,BSS部分只是保持未初始化的全局变量的引用。在这里,未初始化的全局变量的手段,该声明中没有intialised变量?

I read that in the created ELF file, bss sections just keeps the reference of the uninitialised global variables. Here uninitialised global variable means, the variables that are not intialised during declaration?

另外,我读的局部变量会在运行时分配空间(即在堆栈)。然后,他们将如何在目标文件中引用?

Also, I read that the local variables will be allocated space at run time (i.e., in stack). Then how they will be referenced in the object file?

如果在节目中,没有可动态分配内存code的特定部分。如何将这些变量将在目标文件中引用?

If in the program, there is particular section of code available to allocate memory dynamically. How these variables will be referenced in object file?

我很困惑该目标文件的这些不同的部分(如文本,rodata,数据,BSS,栈和堆)是物理内存(RAM),所有的程序执行的一部分。
但我觉得,我的理解是错误的。如何是与物理内存的这些不同的细分当一个进程或进入执行程序?

I am confused that these different segments of object file (like text, rodata, data, bss, stack and heap) are part of the physical memory (RAM), where all the programs are executed. But I feel that my understanding is wrong. How are these different segments related to the physical memory when a process or a program is in execution?

推荐答案

1 正确,ELF文件勾画出一个进程的虚拟地址空间的绝对或相对位置的操作系统应该ELF文件的内容复制到。 (BSS的只是一个位置和大小,因为其应该是全零,就没有必要对实际上在ELF文件中的零)。注意,位置可以是绝对位置(如虚拟地址的0x100000或类似的文本的结尾后4096字节相对位置。)

1. Correct, the ELF file lays out the absolute or relative locations in the virtual address space of a process that the operating system should copy the ELF file contents into. (The bss is just a location and a size, since its supposed to be all zeros, there is no need to actually have the zeros in the ELF file). Note that locations can be absolute locations (like virtual address 0x100000 or relative locations like 4096 bytes after the end of text.)

<强> 2 的虚拟存储器的定义(其被保持在页表和虚拟地址映射到物理地址)不与已编译的程序相关联,但与过程(或任务或任何你的操作系统调用它),稀土presents运行的实例的该计划。例如,单个ELF文件可以加载到两种不同的过程,在不同的虚拟地址(如果ELF文件是可重定位)。

2. The virtual memory definition (which is kept in page tables and maps virtual addresses to physical addresses) is not associated with a compiled program, but with a "process" (or "task" or whatever your OS calls it) that represents a running instance of that program. For example, a single ELF file can be loaded into two different processes, at different virtual addresses (if the ELF file is relocatable).

3 您正在使用的未初始化的状态进入了BSS定义,以及编程语言被明确地初始化。请注意,BSS做的的含有引用,以这些变量,它的的存储支持这些变量。

3. The programming language you're using defines which uninitialized state goes in the bss, and which gets explicitly initialized. Note that the bss does not contain "references" to these variables, it is the storage backing those variables.

4。堆栈变量是隐式从生成code引用。没有什么明确的关于他们(甚至堆栈)ELF文件。

4. Stack variables are referenced implicitly from the generated code. There is nothing explicit about them (or even the stack) in the ELF file.

5。如同栈引用,堆引用是ELF文件生成的code含蓄。 (他们都存储在通过调用更改虚拟地址空间中创建的内存 SBRK 或同等学历)。

5. Like stack references, heap references are implicit in the generated code in the ELF file. (They're all stored in memory created by changing the virtual address space via a call to sbrk or its equivalent.)

ELF文件解释对操作系统如何设置一个虚拟地址空间为程序的一个实例。不同的部分描述了不同的需求。例如,.RODATA说:我想存储只读数据(而不是可执行文件code)。该的.text部分是指执行code。的BSS是用于存储应当由OS归零状态的区域。虚拟地址空间是指程序可以(可选)依靠的事情都是在启动时它期望。 (例如,如果它要求的.bss是在地址0x4000的,那么无论是操作系统将拒绝启动它,或者它会在那里。)

The ELF file explains to an OS how to setup a virtual address space for an instance of a program. The different sections describe different needs. For example ".rodata" says I'd like to store read-only data (as opposed to executable code). The ".text" section means executable code. The "bss" is a region used to store state that should be zeroed by the OS. The virtual address space means the program can (optionally) rely on things being where it expects when it starts up. (For example, if it asks for the .bss to be at address 0x4000, then either the OS will refuse to start it, or it will be there.)

请注意,这些虚拟地址由由OS管理的页表映射到物理地址。 ELF文件的实例并不需要知道任何所涉及的细节,其中使用物理页。

Note that these virtual addresses are mapped to physical addresses by the page tables managed by the OS. The instance of the ELF file doesn't need to know any of the details involved in which physical pages are used.

这篇关于如何像堆,栈,文字的不同环节涉及到的物理内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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