armv8 汇编 绝对地址赋值,如何在arm asm上写PC相对地址?

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

...

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 :

3c: e59f0000 ldr r0, [pc] ; 44

40: e79f0000 ldr r0, [pc, r0]

00000044 :

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?

解决方案

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:

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值