如何在gas assembler中分配超过2GB的内存? [英] How can I allocate more than 2GB of memory in gas assembler?

查看:25
本文介绍了如何在gas assembler中分配超过2GB的内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 64 位操作系统,我正在构建一个使用 rip 相对寻址的 64 位程序.我的问题是我不能使用 .lcomm 指令分配超过 2GB 的数据.有没有办法分配超过 2GB 的 gas 或者我必须使用不同的指令?

I have a 64bit OS and I am building a 64bit program that uses rip-relative addressing. My problem is that I cannot allocate using .lcomm directive more than 2GB of data. Is there a way to allocate more than 2GB in gas or do I have to use a different directive?

.lcomm array,2*1024*1024*1024 
#>=2GB doesn't work
#error:additional relocation overflows omitted from the output

推荐答案

RIP 相对位移是有符号的 32 位.默认代码模型是小"的,其中所有静态代码/数据都在虚拟地址空间的低 2GB 中,因此 RIP 相对寻址模式或 rel32 分支/调用可以从任何地方到达任何符号.(或者对于小型 PIC,所有内容最多不超过 2GB,但您可以让 ASLR 将其放在内存中的任何位置.)

RIP-relative displacements are signed 32-bit. The default code model is "small", where all static code/data is in the low 2GB of virtual address space, so a RIP-relative addressing mode or a rel32 branch / call can reach any symbol from anywhere. (Or for small PIC, everything is within 2GB at most but you let ASLR put it anywhere in memory.)

检查 x86-64 System V ABI 以了解如何使用默认小"以外的代码模型.x86-64 System V ABI 在哪里记录?.

Check the x86-64 System V ABI for how to use code models other than the default "small". Where is the x86-64 System V ABI documented?.

如果只是这个一个巨大的数组,你可能应该使用某种链接器脚本或其他东西来安排成为唯一扩展到低2G之外的东西,所以其他一切都可以仍然假设小代码模型.即把它放在 BSS 的末尾,高于一切,所以 array 符号是 RIP 相对的,可以从低 2G 中的任何代码以及所有其他标签寻址.

If it's only just this one giant array, you should probably use some kind of linker script or something to arrange for it to be the only thing that extends outside the low 2G, so everything else can still assume the small code model. i.e. put it at the end of the BSS above everything else, so the array symbol is RIP-relative addressable from code anywhere in the low 2G, along with all other labels.

但是数组的末尾可能无法到达而不会引起问题,因为您将基地址放入寄存器并对其进行索引.如果您在 C 中编写类似 array[1ULL<<32] = 1; 的内容,并且编译器使用的是小代码模型,因此它会发出类似 movb $1, array+1<<32 (%rip) 这当然行不通.

But the end of the array can be out of reach without causing a problem, because you get the base address into a register and index it. You'll only get a linker error if you write something like array[1ULL<<32] = 1; in C and the compiler is using the small code model so it will emit asm like movb $1, array+1<<32 (%rip) which of course won't work.

这篇关于如何在gas assembler中分配超过2GB的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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