为什么 elf 可执行文件的函数要加载到 x86 Linux 中对齐的地址?

发布于 2024-09-02 05:36:14 字数 185 浏览 2 评论 0原文

我一直在研究 x86 上的 Linux elf 可执行文件,主要使用 IDA,但是 还有gdb。我注意到的一件事是函数总是被加载 在字对齐地址?有人知道其中的原因吗? 我不知道 x86 指令启动有任何要求 在对齐的地址处。并且不可能是由于页面对齐的原因 页面边界仍然可以位于函数内的任何位置。

我将不胜感激任何见解。

谢谢。

I've been looking at Linux elf executables on x86, mostly using IDA but
also gdb. One thing I've noticed is functions are always loaded
at word aligned addresses? Anybody knows the reason of that?
I am not aware of any requirement of x86 instructions to start
at aligned addresses. And it cannot be due to page alignment cause
the page boundary can still be anywhere within the function.

I would appreciate any insight at all.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻泪 2024-09-09 05:36:14

你是对的,指令不需要对齐。在 x86 处理器上,汇编指令使用 1 到至少 15 个字节的可变长度代码进行编码。

但是指令是从通常按 64 字节对齐的缓存中读取的,并且当代码正确对齐时,执行管道的某些部分运行得更快:解码、循环、分支预测等。

有关这方面的最佳信息来源是 Agner Fog 的文档:< a href="http://www.agner.org/optimize/" rel="noreferrer">http://www.agner.org/optimize/

You are right, instructions do not need to be aligned. On x86 processors, assembly instructions are encoded using variable length codes from 1 to at least 15 bytes.

But instructions are read from a cache usually aligned on 64 bytes, and some parts of the execution pipeline operate faster when code is correctly aligned: decoding, loops, branch prediction, etc.

The best source of info on this are Agner Fog's documents: http://www.agner.org/optimize/

酒绊 2024-09-09 05:36:14

对于某些体系结构,数据的对齐方式决定了每次操作可以复制的数据量。例如,尝试从地址 0x4000 复制 32 位可能需要 1 个 32 位移动操作,而从 0x4001 复制 32 位可能需要 4 个 8 位移动操作。此外,在未对齐的地址上使用 32 位移动指令可能会触发硬件异常。硬件异常是通过一次复制 8 位来处理的,但比从对齐地址复制要慢。

编辑:

这适用于所有数据,而不仅仅是将执行的数据。因此,函数入口点与开关目标、字符串常量、全局变量和其他数据对齐。

For some architectures, the alignment of the data dictates the amount of data that can be copied per operation. For example, trying to copy 32 bits from address 0x4000 might take one 32 bit move operations where copying 32 bits from 0x4001 might take 4 8 bit move operations. Furthermore, using the 32 bit move instruction on the misaligned address might trigger a hardware exception. The hardware exception is handled by copying 8 bits at a time, but is slower than copying from an aligned address.

Edit:

This applies to all data, not just data that will be executed. So function entry points are aligned along with switch targets, string constants, globals, and other data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文