【发布时间】:2021-05-16 16:30:12
【问题描述】:
我正在尝试学习 ARM64。我正在 Apple M1 上组装。
我正在尝试分配我可以写入的内存。我不断收到以下错误:
ld: Absolute addressing not allowed in arm64 code but used in '_main' referencing 'foo'
我的程序很简单:
// foo.s
.global _main
.align 2
_main:
ldr x0, =foo
.data
foo: .zero 8
我正在使用这个脚本来编译它:
#!/bin/bash
as foo.s -o foo.o && \
\
ld foo.o -o foo \
-arch arm64 \
-syslibroot `xcrun -sdk macosx --show-sdk-path` \
-lSystem
经过一番谷歌搜索后,我尝试为 ld 设置 -no_pie,但结果如下:
ld: warning: -no_pie ignored for arm64
我不太确定发生了什么。
谢谢
更新:有一个 SO 问题 here 可以解释问题。
我应用了该修复程序并编写了这个快速程序来测试它是否有效:
.global _main
.align 2
_main:
// Set x0 to the memory address of foo.
adrp x0, foo@PAGE
add x0, x0, foo@PAGEOFF
// Store 123 in foo.
mov x1, 123
str x1, [x0]
// Load the contents of foo into x2.
ldr x2, [x0]
// Exit with a status code set to foo.
mov x0, x2
mov x16, 1
svc 0
.data
foo: .zero 8
如我所料,它以 123 的退出状态返回。
【问题讨论】:
-
在开始定义
_main之前不要忘记.text。它可能是默认设置,但如果您决定将数据移动到文件顶部,它将使您免于损坏。 -
谢谢,其他 SO 问题的答案已经成功了!我一定会按照您的建议将
.text添加到我的程序中!