尝试在 cortex-m3 上加载与位置无关的代码 [英] Trying to load position independent code on cortex-m3

查看:25
本文介绍了尝试在 cortex-m3 上加载与位置无关的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌入式应用程序,它有一个引导加载程序,它将决定直接从内部闪存运行两个应用程序中的一个.我正在尝试使这些应用程序位置独立,以便它们都可以针对相同的基地址进行编译.没有操作系统,所以没有动态链接器可用.到目前为止,我已经尝试使用 -fpie 选项(使用 gcc)进行构建,但没有取得太大的成功.函数调用似乎是正确的,但全局数据没有正确的地址.本地定义的全局数据的地址偏移量似乎是应用程序与其原始基地址的偏移量.在其他文件中声明的全局数据的地址完全错误(如果我使用 -fpic 构建,那么本地声明的全局数据和其他文件中的全局数据都是完全错误的).我怀疑我需要在我的应用启动时对 GOT 部分进行一些操作,但我不确定.

I have an embedded application which will have a bootloader which will decide to run 1 of two applications directly from internal flash. I am trying to make these apps position independent so that they both can be compiled for the same base address. There is no operating system, so no dynamic linker is available. So far I have tried building with -fpie option (using gcc) with not too much success. The function calls appear to be correct but the global data does not have the correct address. The locally defined global data seems to have it's address offset by the amount that the app is offset from its original base address. The global data which is declared in other files has a completely wrong address(and if I build with -fpic then both the locally declared global data and global data in other files are completely wrong). I suspect I need to do some manipulation of the GOT section when my app starts but I am not sure.

推荐答案

我终于搞定了.看来我需要执行以下操作:所有代码都需要遵守-fpic(之前我在尝试-fpie)

I finally got it working. It looks like I need to do the following: All code needs to be complied with -fpic (previously I was trying -fpie)

我的链接器脚本也需要修改.我强迫 GOT 进入 sram 部分,它位于 flash 中的动态部分之后.如果 GOT 部分位于闪存中的动态部分之前,则看起来一切正常.不知道为什么这很重要,但它似乎解决了所有问题 - 在此之前,好像代码没有正确定位 GOT,因为 GOT 中存储了正确的值,但我所有变量的地址都不正确.

Also I had my linker script needed modification. I was forcing the GOT into the sram section and it was located after the dynamic section which was in flash. Looks like everything works properly if the GOT section is located prior to the dynamic section in the flash. Not sure why this matters but it seemed to fix everything - prior to this it was as if the code did not locate the GOT properly since the GOT had the correct values stored in it but the address of all my variables were incorrect.

这篇关于尝试在 cortex-m3 上加载与位置无关的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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