title: Howto write PC relative adressing on arm asm?
body: <p>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</p>
  
  <pre><code>adr r0, symbol
  </code></pre>
  
  <p>but that only works for symbols defined in the same section in the assembly file. The other method to load a symbol is</p>
  
  <pre><code>ldr r0, =symbol
  </code></pre>
  
  <p>which stores the absolute address of the symbol in a constant and loads it pc relative from there. So you get code like this:</p>
  
  <pre><code>  48:   e59f0008        ldr     r0, [pc, #8]    ; 58 &lt;text+0xc&gt;
    ...
    58:   00000008        .word   0x00200018
                          58: R_ARM_ABS32 symbol
  </code></pre>
  
  <p>But what I need is a <strong>R_ARM_REL32</strong> linker reference.</p>
  
  <p>What I have come up with is this code:</p>
  
  <pre><code>tmp1:    ldr    r0, [pc, #tmp2 - tmp1 - 8]
           ldr    r0, [pc, r0]
  tmp2:    .word  symbol - tmp1
  </code></pre>
  
  <p>This produces:</p>
  
  <pre><code>0000003c &lt;tmp1&gt;:
    3c:   e59f0000        ldr     r0, [pc]        ; 44 &lt;tmp2&gt;
    40:   e79f0000        ldr     r0, [pc, r0]
  
  00000044 &lt;tmp2&gt;:
    44:   00000008        .word   0x00000008
                          44: R_ARM_REL32 symbol
  </code></pre>
  
  <p>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.</p>
  
  <p>But isn't there already some existing syntax or macro for that in GNU AS?</p>