PIC in ARM assembly

Hello,

I’m trying to create position-independent ARM assembly code. For armcc I do the following:

IMPORT foo

L_PIC ldr r0, L_FOO

add r0, pc, r0

L_FOO DCD foo - (L_PIC + 8)

which for gcc looks similar:

.extern foo

L_PIC: ldr r0, L_FOO

add r0, pc, r0

L_FOO: .word foo - (L_PIC + 8)

This works fine when using the GNU assembler. But when using Clang I get the following weird error: “symbol ‘_foo’ can not be undefined in a subtraction expression”. Looking at assembly code produced from C by the Clang compiler, it does things differently, using the directive “.indirect_symbol”, which gcc and armcc don’t support:

movw r0, :lower16:(L_FOO-(L_PIC+8))

movt r0, :upper16:(L_FOO-(L_PIC+8))

L_PIC: ldr r0, [pc, r0]

L_FOO: .indirect_symbol _foo

This would be fine except that I want to have one code base that can be built on multiple architectures with different tools. In the short term, I’m looking for an approach that will work with both gcc and Clang. In the longer term, it would be nice if this could be “fixed” in Clang. (I put “fixed” in quotes since I don’t know if Clang has compatibility with armcc/gcc as a goal.) Thanks in advance for any suggestions.

Thanks,

Jeff Schenck