简体   繁体   English

.data 段如何加载到单独的 memory 区域而不是 x86 中的 .code 段?

[英]How is the .data segment loaded into a seperate memory area than the .code segment in x86?

So I've been dabbling with assembly and have always wondered, how are the different segments loaded into different memory areas when loading the kernel.所以我一直在涉足组装,一直想知道,在加载 kernel 时,不同的段是如何加载到不同的 memory 区域的。 I am aware for user code in an OS the linker and the OS can simply put the code and data in whichever area they want, but this question is more specific to kernels, which don't have an OS to put the.code and.data into different areas.我知道操作系统中的用户代码 linker 和操作系统可以简单地将代码和数据放在他们想要的任何区域,但这个问题更具体到内核,它没有操作系统来放置.code 和。数据到不同的区域。

Say I have a simple kernel:假设我有一个简单的 kernel:

.data
var dw 123

.code
mov eax ebx

How does the bootloader, or whatever handles this, actually load the Kernel code into a separate area to the data?引导加载程序或任何处理此问题的方法如何将 Kernel 代码实际加载到数据的单独区域中? And say I have 5 different assembly files, all with.data segments, are the variables all loaded one after the other into the same area?假设我有 5 个不同的程序集文件,都带有.data 段,变量是否都一个接一个地加载到同一个区域?

It's up to the bootloader to handle this so it ultimately depends on what the bootloader does, but generally bootloaders are loading standard executable images in standard formats like PECOFF or ELF.由引导加载程序来处理这个问题,因此它最终取决于引导加载程序的作用,但通常引导加载程序以标准格式(如 PECOFF 或 ELF)加载标准可执行映像。 They load the kernel much like operating systems load program executables.它们加载 kernel 就像操作系统加载程序可执行文件一样。

Sections exist so that the contents of sections with the same name will all be grouped together contiguously in the executable.存在节,因此具有相同名称的节的内容将在可执行文件中连续分组在一起。 The linker will take the contents of all the .text sections in all the input object files and combine them into one .text section in the output executable. linker 将获取所有输入 object 文件中所有.text部分的内容,并将它们组合成 output 可执行文件中的一个.text部分。 Similarly it will do the same for .data and other named sections.同样,它将对.data和其他命名部分执行相同的操作。

The linker takes all these combined sections and places them one after each other in the executable, creating one contiguous image that can be loaded into memory. linker 采用所有这些组合部分并将它们一个接一个地放置在可执行文件中,创建一个可以加载到 memory 的连续映像。 Under an operating system the executable would be loaded into memory in one single contiguous chunk.在操作系统下,可执行文件将在单个连续块中加载到 memory 中。 If necessary, relocations would be applied to account for the executable being loaded at a different address than where it was intended to be loaded and uninitialized data segments (.bss) would be initialized with zeroes.如有必要,将应用重定位以说明加载到与预期加载位置不同的地址的可执行文件,并且将用零初始化未初始化的数据段 (.bss)。 Finally the permissions of each page of the executable in memory would be adjusted according to the segments they belong to.最后memory中可执行文件的每一页的权限将根据它们所属的段进行调整。 For example, pages in .text sections would be marked read-only and executable, while in .data sections they would be marked read/write and not executable.例如, .text节中的页面将被标记为只读且可执行,而在.data节中,它们将被标记为读/写且不可执行。

(Note that this simplifies and glosses over many details how linkers and operating systems create and load executables. It's possible for sections to be merged, renamed, discarded, etc. Padding space may be inserted between segments so that they're page aligned. Under ELF named sections in object files are actually converted unnamed program segments in executables and it's these program segments that determine page permissions.) (请注意,这简化并掩盖了链接器和操作系统如何创建和加载可执行文件的许多细节。可以合并、重命名、丢弃等部分。可以在段之间插入填充空间,以便它们与页面对齐。在object 文件中的 ELF 命名节实际上是可执行文件中转换的未命名程序段,正是这些程序段确定了页面权限。)

A bootloader loads a kernel executable much like an operating system, but may not support relocations, and won't change page permissions because those need an operating system to work.引导加载程序加载 kernel 可执行文件,就像操作系统一样,但可能不支持重定位,并且不会更改页面权限,因为这些需要操作系统才能工作。 The kernel itself is responsible setting up its own page permissions. kernel 本身负责设置自己的页面权限。

So the kernel code and data gets loaded into one single contiguous area of memory, with that area of memory subdivided into separate areas for the code, data and any other sections the kernel uses. So the kernel code and data gets loaded into one single contiguous area of memory, with that area of memory subdivided into separate areas for the code, data and any other sections the kernel uses.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM