Relative References in ARM64 Disassembly

POV: You are a compiler targeting arm641, and you want some code to reference this global variable from the same library. The classic way to do this is to emit an instruction that loads “the address of X”, which will be determined at run time by the dynamic loader. But that’s not super efficient! For one thing, addresses are 64 bits long, and instructions are only 32 bits, so you can either break it up into multiple instructions, or load the address from some other location. But more importantly, the global variable is in the same library. The dynamic loader isn’t going to break it up from this code2, and if we knew how far away it was we could reference it that way.

That’s what the adrp instruction’s for.


Just wanted to share a quick hint for GDB users. When you want to debug a program that takes command-line arguments, the “traditional” way looks something like this:

% gdb a.out
GNU gdb 6.3.50-20050815...

(gdb) set args x y z
(gdb) run

But you can do even better. GDB itself has the command-line flag --args, which means that the rest of the arguments go to the program you’re debugging!

% gdb --args a.out x y z

Possibly Related Tags