Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto write PC relative adressing on arm asm?

Tags:

assembly

arm

I'm using GNU AS as assembler and I'm trying to write a position independent binary and I have some problems writing the PC relative address of an external symbols. Normaly a relative address is loaded with

adr r0, symbol

but that only works for symbols defined in the same section in the assembly file. The other method to load a symbol is

ldr r0, =symbol

which stores the absolute address of the symbol in a constant and loads it pc relative from there. So you get code like this:

  48:   e59f0008        ldr     r0, [pc, #8]    ; 58 <text+0xc>
  ...
  58:   00000008        .word   0x00200018
                        58: R_ARM_ABS32 symbol

But what I need is a R_ARM_REL32 linker reference.

What I have come up with is this code:

tmp1:    ldr    r0, [pc, #tmp2 - tmp1 - 8]
         ldr    r0, [pc, r0]
tmp2:    .word  symbol - tmp1

This produces:

0000003c <tmp1>:
  3c:   e59f0000        ldr     r0, [pc]        ; 44 <tmp2>
  40:   e79f0000        ldr     r0, [pc, r0]

00000044 <tmp2>:
  44:   00000008        .word   0x00000008
                        44: R_ARM_REL32 symbol

I can nearly put that into a macro so it is reusesable for whatever symbol I need. Except I don't know how to tell the assembler that tmp2 needs to go to the constants block instead of ending up right there in the middle of the code.

But isn't there already some existing syntax or macro for that in GNU AS?

like image 753
Goswin von Brederlow Avatar asked Feb 20 '15 22:02

Goswin von Brederlow


1 Answers

ARMv8

Note that in ARMv8, the PC cannot be treated as a regular register anymore as was the case for ARMv7.

There is however a special PC-relative encoding for ldr called "LDR (literal)" with syntax:

    ldr x0, pc_relative_ldr
    b 1f
pc_relative_ldr:
    .quad 0x123456789ABCDEF0
1:

GitHub upstream.

Trying to do an old "LDR (register)" syntax as in:

ldr x0, [pc]

fails with:

64-bit integer or SP register expected at operand 2 -- `ldr x0,[pc]'

For some reason, there is not such PC-relative encoding for str. I think you just have to use adr + "STR (register)" as in:

    adr x1, pc_relative_str
    ldr x0, pc_relative_ldr
    str x0, [x1]
    ldr x0, pc_relative_str
.data
pc_relative_str:
    .quad 0x0000000000000000

GitHub upstream.

where adr loads a PC-relative address into a register.

You might also be interested in ADRP if you need longer jumps, see: What are the semantics of ADRP and ADRL instructions in ARM assembly?

like image 176
Ciro Santilli Avatar answered Nov 12 '22 10:11

Ciro Santilli