无法将 .data 中的变量移动到 Mac x86 程序集的注册 [英] Unable to move variables in .data to registers with Mac x86 Assembly

查看:16
本文介绍了无法将 .data 中的变量移动到 Mac x86 程序集的注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 AT&T 语法编写了一小段程序集,目前在 .data 部分声明了三个变量.但是,当我尝试将这些变量中的任何一个移动到寄存器(例如 %eax)时,会引发来自 gcc 的错误.代码和错误信息如下:

.datax:.int 14y:.int 4str: .string "一些字符串
";.globl _main_主要的:pushq %rbpmovq %rsp, %rbpsubq 16 美元,%rspmovl x, %eax;#试图将 x 的值移动到 %eax;离开退

引发的错误是:

<块引用>

call_function.s:14:3: 错误:64 位模式不支持 32 位绝对寻址

movl x, %eax;

^

我还尝试通过首先在 x 前面添加 $ 字符来移动值,但是,引发了 clang 错误:

<块引用>

clang: 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)

有谁知道存储在x中的值如何成功移动到%eax?我在 Mac OSX 上使用 x86 程序集并使用 gcc 编译.

解决方案

RIP 相对寻址模式是 MacOS 上静态数据寻址的唯一好选择;图像基地址高于 2^32,因此 32 位绝对地址即使在依赖位置的代码中也不可用(与 x86-64 Linux 不同).静态数据的 RIP 相对寻址与位置无关,因此它甚至可以在位置无关的可执行文件 (ASLR) 和库中使用.

movl x(%rip), %eax 是 RIP 相关的 AT&T 语法.

mov eax, dword ptr [rip+x] in GAS .intel_syntax noprefix.

或者,将符号的地址存入寄存器,lea x(%rip), %rdi

<小时>

NASM 语法:mov eax, [rel x],或使用 default rel 所以 [x] 是 RIP 相关的.>

Mach-O 64 位格式不支持 32 位绝对地址.NASM 访问数组,了解更多关于可以在 OS X 上做什么的背景,例如movabs x, %eax 是可能的,因为目标寄存器是 AL/AX/EAX/RAX.(64 位绝对地址,但不要这样做,因为它比 RIP 相对加载更大且速度不快.)

另见 http://felixcloutier.com/x86/MOV.html.

I have written a small piece of assembly with AT&T syntax and have currently declared three variables in the .data section. However, when I attempt to move any of those variables to a register, such as %eax, an error from gcc is raised. The code and error message is below:

.data
  x:.int 14
  y:.int 4
  str: .string "some string
"

.globl _main

_main:
  pushq %rbp
  movq %rsp, %rbp
  subq $16, %rsp
  movl x, %eax; #attempting to move the value of x to %eax;
  leave
  ret

The error raised is:

call_function.s:14:3: error: 32-bit absolute addressing is not supported in 64-bit mode

movl x, %eax;

^

I have also tried moving the value by first adding the $ character in front of x, however, a clang error is raised:

clang: error: linker command failed with exit code 1 (use -v to see invocation)

Does anyone know how the value stored in x can be successfully moved to %eax? I am using x86 assembly on Mac OSX and compiling with gcc.

解决方案

A RIP-relative addressing mode is the only good option for addressing static data on MacOS; the image base address is above 2^32 so 32-bit absolute addresses aren't usable even in position-dependent code (unlike x86-64 Linux). RIP-relative addressing of static data is position-independent, so it works even in position-independent executables (ASLR) and libraries.

movl x(%rip), %eax is the AT&T syntax for RIP-relative.

mov eax, dword ptr [rip+x] in GAS .intel_syntax noprefix.

Or, to get the address of a symbol into a register, lea x(%rip), %rdi


NASM syntax: mov eax, [rel x], or use default rel so [x] is RIP-relative.

See Mach-O 64-bit format does not support 32-bit absolute addresses. NASM Accessing Array for more background on what you can do on OS X, e.g. movabs x, %eax would be possible because the destination register is AL/AX/EAX/RAX. (64-bit absolute address, but don't do that because it's larger and not faster than a RIP-relative load.)

See also http://felixcloutier.com/x86/MOV.html.

这篇关于无法将 .data 中的变量移动到 Mac x86 程序集的注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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