I'm trying to learn ARM64. I'm assembling on the Apple M1.
I'm trying to allocate memory I can write to. I keep receiving the following error:
ld: Absolute addressing not allowed in arm64 code but used in '_main' referencing 'foo'
My program is very simple:
// foo.s
.global _main
.align 2
_main:
ldr x0, =foo
.data
foo: .zero 8
I'm using this script to compile it:
#!/bin/bash
as foo.s -o foo.o && \
\
ld foo.o -o foo \
-arch arm64 \
-syslibroot `xcrun -sdk macosx --show-sdk-path` \
-lSystem
After some googling, I tried setting -no_pie
for ld but that results in:
ld: warning: -no_pie ignored for arm64
I'm not really sure what's going on.
Thanks
Update: There's a SO question here that explains the problem.
I applied that fix and wrote this quick program to test that it works:
.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
It returns with an exit status of 123 as I'd expect.