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

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

问题描述

我使用 GCC 选项 -mpic-data-is-text-relative-fpic 生成位置无关代码.这适用于我的 C 代码.

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.

但我也有需要使用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 从我的代码中获取变量:

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.

推荐答案

Elf DATA 跟随文本,并且在链接时可以知道数据的偏移量.您需要将 PC 添加到已知位置和数据之间的偏移量才能访问数据.请参阅 ARM ELFJohn Levine 的链接器和加载器 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.

显然,如果这是由操作系统和加载程序托管的,您将需要使用平台的约定.以下内容针对裸机系统或您可以选择的地方.

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 relative '1' label + offset there
   str r0, [r1]              ; store to corrected address.
   bx lr
1: .word hwa_ext_reset_hnd-. ; offset from here to data fixed at link.

这适用于数据紧随其后的 PIC 代码.如果出现多次,您可以创建一个宏来访问数据.如果有很多引用,保持一个寄存器加载在 .data 部分的开头可能更容易.带有编译器选项 -msingle-pic-base-mpic-register=regstatic basestatic base 通常是 r9.所以数据的加载时间开始放在r9中一次,并且只使用str 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.

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

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