如何从位置无关code(PIC)在ARM汇编访问数据吗? [英] How to access data from Position Independent Code (PIC) in ARM Assembly?

查看:758
本文介绍了如何从位置无关code(PIC)在ARM汇编访问数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的GCC选项 -mpic-数据是文本相对 -fpic 生成位置无关code。这对于我的C code正常工作。

I am using the GCC options -mpic-data-is-text-relative and -fpic to generate position independent code. This works fine for my C code.

不过,我也有装配code需要使用PIC。问题是我无法找到如何实现这样的一个例子。是否有一些ARM GCC汇编关键字,我需要建立自己GOT表还是有什么巧妙的技巧?

But I also have assembly code that needs to use PIC. The problem is I cannot find an example on how to achieve this. Are there some ARM GCC assembly keywords, do I need to set up my own GOT table or are there any neat tricks?

我试图使用PIC从我的code达到一个变量:

I'm trying to reach a variable from my code using PIC:

   .section .data @HWA_SWI_HND_DATA, "w"
hwa_ext_reset_hnd:
   .word 0x00000000

   .section .text @HWA_SWI_HND_CODE, "x"
   .code 32

   .list

   .global hwa_reset_cb_attach
hwa_reset_cb_attach:
   ldr r1, =hwa_ext_reset_hnd
   str r0, [r1]
   bx lr

正如 LDR R1,= hwa_ext_reset_hnd 拨打上面所看到的我想这个地址取是PIC。

As seen above in the ldr r1, =hwa_ext_reset_hnd call I want this address fetch to be PIC.

推荐答案

小精灵数据如下文字和偏移数据可以在链接时是已知的。需要向PC添加到一个已知的位置,并访问数据的数据之间的偏移。请参见 ARM ELF 并的链接程序和装载机由约翰·莱文 chp8。

The Elf DATA follows text and the offset to data can be known at link time. You need to add the PC to an offset between known location and data to access the data. See ARM ELF and Linkers and loader chp8 by John Levine.

显然,如果这是由OS和装载机托管,则需要使用该平台的约定。为的裸机的,你必须选择的选项系统或地方写入以下内容。

Obviously, if this is hosted by an OS and loader, you will need to use the conventions of the platform. The following is written for a bare metal system or places where you have the option to choose.

例如,

   .global hwa_reset_cb_attach
hwa_reset_cb_attach:
   adr r2, 1f                ; pc relative value of label '1'
   ldr r1, [r2]              ; offset between label and data to r1
   add r1, r1, r2            ; pc + offset
   str r0, [r1]              ; store to corrected address.
   bx lr
1: .word hwa_ext_reset_hnd-. ; offset from here to data fixed at link.

这适用于PIC code立即用以下数据。如果有许多事件,您可以创建一个宏来访问数据。如果有很多参考文献,它也许更容易保持装入的的.data 的部分的开始的寄存器。在静态基的使用编译器选项 -msingle-PIC基 -mpic注册= 的;的静态基的一般 R9 。所以数据的加载时间开始被放在 R9 一次且仅使用海峡RX [R9,#hwa_ext_reset-start_of_data] 。这是通过的的u-boot 的使用的战术,你甚至可以的移居的数据段(从IRAM移动到SDRAM等)。但是,它消耗额外的寄存器。

This works for PIC code with data following immediately. If you have many occurrences, you can create a macro to access the data. If there are a lot of references, it maybe easier to keep a register loaded with the beginning of the .data section. The static base with the compiler options -msingle-pic-base and -mpic-register=reg; static base is typically r9. So the load time start of data is put in r9 once and only use str rx,[r9, #hwa_ext_reset-start_of_data]. This is a tactic used by u-boot and you can even relocate the data sections (moving from iram to SDRAM, etc). However, it consumes an extra register.

这篇关于如何从位置无关code(PIC)在ARM汇编访问数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆