堆、堆栈、文本等不同的段与物理内存有何关系? [英] How are the different segments like heap, stack, text related to the physical memory?

查看:34
本文介绍了堆、堆栈、文本等不同的段与物理内存有何关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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 中存在的虚拟内存地址与实际物理内存地址相关联.我的理解正确吗?

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 部分只保留未初始化的全局变量的引用.这里未初始化的全局变量是指,声明时未初始化的变量?

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?

如果在程序中,有特定的代码段可用于动态分配内存.这些变量将如何在目标文件中被引用?

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. 虚拟内存定义(保存在页表中并将虚拟地址映射到物理地址)与已编译的程序无关,而是与进程"(或任务"或任何你的操作系统调用它)代表该程序的运行实例.例如,一个 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. 堆栈变量从生成的代码中隐式引用.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 文件中生成的代码中.(它们都存储在通过调用 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"表示我想存储只读数据(而不是可执行代码)..text"部分表示可执行代码.bss"是用于存储状态的区域,应该由操作系统清零.虚拟地址空间意味着程序可以(可选地)依赖于它在启动时所期望的位置.(例如,如果它要求 .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.)

请注意,这些虚拟地址通过操作系统管理的页表映射到物理地址.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天全站免登陆