as

Table of Contents

Next: , Up: (dir)   [Contents]

as

This manual is for program, version version.


Previous: , Up: Top   [Contents]

1 as

https://sourceware.org/binutils/docs/as/    // as document
---
aarch relocations

Relocations for `MOVZ' and `MOVK' instructions can be generated by prefixing the label with `#:abs_g2:' etc. For example to load the 48-bit absolute address of foo into x0:
             movz x0, #:abs_g2:foo		// bits 32-47, overflow check
             movk x0, #:abs_g1_nc:foo	// bits 16-31, no overflow check
             movk x0, #:abs_g0_nc:foo	// bits  0-15, no overflow check
Relocations for `ADRP', and `ADD', `LDR' or `STR' instructions can be generated by prefixing the label with `:pg_hi21:' and `#:lo12:' respectively.
For example to use 33-bit (+/-4GB) pc-relative addressing to load the address of foo into x0:
             adrp x0, :pg_hi21:foo
             add  x0, x0, #:lo12:foo
Or to load the value of foo into x0:
             adrp x0, :pg_hi21:foo
             ldr  x0, [x0, #:lo12:foo]
Note that `:pg_hi21:' is optional.
             adrp x0, foo
is equivalent to
             adrp x0, :pg_hi21:foo

code example
// CFLAGS=-O3 -fPIE -pie -g3 --sysroot=${SYSROOT}
.text //code section
.globl main
main:
	mov x0, 0     // stdout has file descriptor 0
	adrp x1, msg  // buffer to write
	add x1, x1, :lo12:msg
	mov x2, len   // size of buffer
	mov x8, 64    // sys_write() is at index 64 in kernel functions table
	svc #0        // generate kernel call sys_write(stdout, msg, len);

	mov x0, 123 // exit code
	mov x8, 93  // sys_exit() is at index 93 in kernel functions table
	svc #0      // generate kernel call sys_exit(123);

.data //data section
msg:
    .ascii      "Hello, ARM!\n"
len = . - msg