我可以在 x86-64 中使用 gcc 为不同变量选择 RIP 相对或绝对寻址吗
我编写了自己的链接脚本,将不同的变量放入两个不同的数据部分(A 和 B)。
A链接到零地址; B 链接到代码附近,位于高地址空间(高于 4G,这对于 x86-64 中的正常绝对寻址是不可用的)。
A可以通过绝对寻址来访问,但不能通过RIP相对寻址来访问; B可以通过RIP相对寻址来访问,但不是绝对的;
我的问题:有没有办法为 gcc 中的不同变量选择 RIP 相对或绝对寻址?也许带有一些注释,例如 #pragma
?
I write my own link script to put different variables in two different data sections (A & B).
A is linked to zero address;
B is linked near to code, and in high address space (higher than 4G, which is not available for normal absolute addressing in x86-64).
A can be accessed through absolute addressing, but not RIP-relative;
B can be accessed through RIP-relative addressing, but not absolute;
My question: Is there any way to choose RIP-relative or absolute addressing for different variables in gcc? Perhaps with some annotation like #pragma
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不破解 GCC 源代码,您将无法让它发出32 位绝对寻址,但在某些情况下 gcc 将使用 64 位绝对地址。
-mcmodel=medium
将大对象放入单独的部分,对大数据部分使用 64 位绝对地址。 (所有对象都必须同意大小阈值,由-mlarge-data-threshold=
设置)。但对于所有其他变量仍使用 RIP 相对值。有关不同内存模型的更多信息,请参阅 x86-64 System V ABI 文档。和/或
-mcmodel=
和-mlarge-data-threshold=
的 GCC 文档:https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html默认值为
-mcmodel=small
:所有内容都在 2GiB 范围内,因此 RIP 相对有效。对于非 PIE 可执行文件,这是虚拟地址空间的低 2GiB,因此静态地址可以是 32 位绝对符号或零扩展立即数或寻址模式中的 disp32。ASM 输出 (Godbolt):
用于加载到 AL/AX/EAX/RAX、GCC 以外的寄存器将使用
movabs r64, imm64
与地址,然后使用mov reg, [reg]
。您不会让 gcc 对 A 部分使用 32 位绝对寻址。它将始终使用 64 位绝对寻址,从不
[array + rdx* 4]
或[abs foo]
(NASM 语法)。并且永远不要使用mov edi, msg
(imm32) 将地址放入寄存器,而总是mov rdi, qword msg
(imm64)。GCC 将
b
放入.lbss
部分,并将a
放入常规.bss
中。 下使用 __attribute__((section("name")))想必您可以在不起作用的情况
https://gcc.gnu.org/onlinedocs/gcc/Variable -Attributes.html 不记录任何与内存模型或大小相关的 x86 或常见变量属性。唯一特定于 x86 的变量属性是 ms vs gcc 结构布局。
函数和类型有特定于 x86 的属性,但这些没有帮助。
可能的黑客:
将所有 A 部分变量放入一个大结构中,比任何 B 部分全局/静态对象都大。可能在末尾用虚拟数组填充它以使其更大:您的链接器脚本可能可以避免实际为该虚拟数组分配额外的空间。
然后使用 -mcmodel=medium mlarge-data-threshold=该大小进行编译。
Without hacking the GCC source code, you're not going to get it to emit 32-bit absolute addressing, but there are cases where gcc will use 64-bit absolute addresses.
-mcmodel=medium
puts large objects into a separate section, using 64-bit absolute addresses for the large-data section. (With a size threshold that all objects have to agree on, set by-mlarge-data-threshold=
). But still uses RIP-relative for all other variables.See the x86-64 System V ABI doc for more about the different memory models. And/or GCC docs for
-mcmodel=
and-mlarge-data-threshold=
: https://gcc.gnu.org/onlinedocs/gcc/x86-Options.htmlThe default is
-mcmodel=small
: everything is within 2GiB of everything else, so RIP-relative works. And for non-PIE executables, that's the low 2GiB of virtual address space so static addresses can be 32-bit absolute sign- or zero-extended immediates or disp32 in addressing modes.ASM output (Godbolt):
For loading into a register other than AL/AX/EAX/RAX, GCC would use
movabs r64, imm64
with the address and then usemov reg, [reg]
.You won't get gcc to use 32-bit absolute addressing for section A. It will always be using 64-bit absolute, never
[array + rdx*4]
or[abs foo]
(NASM syntax). And nevermov edi, msg
(imm32) for putting an address in a register, alwaysmov rdi, qword msg
(imm64).GCC puts
b
in the.lbss
section anda
in the regular.bss
. Presumably you can use__attribute__((section("name")))
onThings that don't work:
__attribute__((optimize("mcmodel=large")))
on a per-function basis. Doesn't actually work, and is per-function not per-variable anyway.https://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html doesn't document any x86 or common variable attributes related to memory-model or size. The only x86-specific variable attribute is ms vs gcc struct layout.
There are x86-specific attributes for functions and types, but those don't help.
Possible hacks:
Put all your section-A variables in a large struct, larger than any section-B global/static objects. Possibly pad it at the end with a dummy array to make it larger: your linker script can probably avoid actually allocating extra space for that dummy array.
Then compile with
-mcmodel=medium mlarge-data-threshold=
that size.