x86 程序集中的 .data 部分 [英] .data section in x86 assembly

查看:22
本文介绍了x86 程序集中的 .data 部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习汇编语言,但无法理解通过 .data 部分声明全局变量的概念.当您在 .data 部分声明变量时,汇编程序/链接器会将其映射到内存位置.但是它如何知道在编译源代码时可用的空闲内存.如果内存分配是在运行时完成的,那么程序如何知道在哪里分配内存,因为我们没有在代码中这样做.

I am trying to learn assembly language and couldn't understand the concept of declaring global variables through .data section .When you declare a variable in .data section , the assembler/linker would map it to a memory location. But how does it know about the free memory available while compiling the source code. If the memory allocation is done during runtime ,then how does the program know where to allocate memory, since we are not doing that in the code.

推荐答案

但是它如何知道编译源代码时可用的空闲内存.

But how does it know about the free memory available while compiling the source code.

编译时;它使用一个非常简单的分配器为段中的代码和数据段创建空间,分配器可以像offset = section.size;"一样简单.section.size += object.size".稍后(链接时),当节的最终大小已知(并且每个节的开始地址已知)时,它返回并将这些偏移量转换为地址,方法是添加节开始的地址".到每个部分中的偏移量".

When compiling; it uses a very simple allocator to create space for pieces of code and data in sections, where that allocator could be as simple as "offset = section.size; section.size += object.size". Later (when linking), when the final sizes of sections are known (and the address for the start of each section is known), it goes back and converts these offsets into addresses by adding "address for the start of the section" to each "offset in section".

然后将节的详细信息(它们在内存中的地址、大小、属性、它们在可执行文件中的位置)存储在可执行文件的标题中.

Then the details for sections (their address in memory, size, attributes, where they are in the executable file) are stored in the executable file's header.

当加载可执行文件时,操作系统使用可执行文件的头来确定文件的哪些部分被加载/映射到哪里.通常操作系统(或编程语言的库)也会跟踪虚拟地址空间的哪些区域用于什么;这样,如果/当程序分配更多的虚拟地址空间(使用 Windows 上的 VirtualAlloc() 或 Unix 克隆上的 mmap() 等函数),它可以分配虚拟空间'尚未使用(按节等).

When the executable file is loaded, the OS uses the executable file's header to figure out which parts of the file get loaded/mapped where. Typically the OS (or the programming language's library) will also keep track of which areas of the virtual address space are used for what; so that if/when the program allocates more virtual address space (using functions like VirtualAlloc() on Windows or mmap() on Unix clones) it can allocate virtual space that isn't already used (by sections, etc).

最后;当程序开始运行时(可能在链接器为您提供的默认启动代码"中)它可能会为程序的堆分配虚拟空间,然后使用该虚拟空间来设置动态内存管理(例如 malloc()new 或任何对语言及其库有意义的东西.

Finally; when the program starts running (possibly in "default startup code" that the linker included for you) it will probably allocate virtual space for the program's heap, and then use that virtual space to set up dynamic memory management (e.g. malloc() or new or whatever makes sense for the language and its libraries).

注意 1:编译和链接并不是那么简单(例如,用于在节中创建空间的分配器也必须担心对齐之类的事情).

注意 2:可执行加载器并没有那么简单(例如,它们还加载/映射共享库并进行动态链接).

注 3:大多数现代系统使用地址空间布局随机化";(为了提高安全性),因此最终地址(由代码、数据使用)可能由可执行加载程序(作为动态链接的一部分)确定(根据段中的偏移量),而不是完全由链接器本身确定.

这篇关于x86 程序集中的 .data 部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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