如何在 ARM 架构上从 RAM 运行代码 [英] How to run code from RAM on ARM architecture

查看:135
本文介绍了如何在 ARM 架构上从 RAM 运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对 ARM Cortex-R4 进行编程,我有一些二进制文件,我想从 TCRAM 执行它们,只是为了看看性能的提高是否足够好.

I am programming an ARM Cortex-R4 and I have a few binary files that I'd like to execute them from TCRAM, just to see if the increase in performance is good enough.

我知道我必须编写一个函数来将二进制文件复制到 RAM(这可以通过链接器脚本完成,并且知道二进制文件的大小).但是他们将如何运行?

I know I'd have to write a function to copy the binaries to the RAM (which can be accomplished with the linker script, and knowing the size of the binaries). But how would they run?

想象一下:第一个二进制文件有 func1()、func2()、func3() 和 func4().我将整个模块复制到 TCRAM,我将如何在那里调用函数?我必须使用指向该特定函数的函数指针吗?如果 func4() 调用 func2() 和 func3() 呢?如果我没记错的话,他们会指向闪存中的一段代码.这是否意味着我必须重新编写这些函数?完全使用函数指针?有人告诉我,仅链接器脚本就足以完成所有这些工作,我无需担心任何事情,但我仍然不明白它是如何工作的.

Imagine this: The first binary has func1(), func2(), func3() and func4(). I'd copy the entire module to TCRAM and how would I call a function there? I'd have to use a function pointer to that specific function? And what if func4(), calls func2() and func3()? If I'm not mistaken they'd point to the piece of code located in the flash. Does that mean I'd have to re write those funcs? Use entirely function pointers? I've been told that just the linker script is enough to do all of this and I needn't worry about anything, but I still don't understand how it works.

推荐答案

您有两个选择.

  1. 按照你的建议复制它们,用 pc relative 编译.
  2. 使用具有不同加载/运行地址的链接器文件.

一个简单的副本只有在例程不使用任何绝对地址时才有效.如果他们确实使用绝对地址可能没问题,因为我猜您会在标准 RAM 中保留副本.但是,这可能无法获得 TCM 的全部好处.

A simple copy will only work if the routines do not use any absolute addresses. It maybe fine if they do use the absolute address as I guess you are going to leave a copy in standard RAM. However, this may not get the full benefit of the TCM.

使用链接描述文件,您可以指定不同的 LOADRUN 位置.

With a linker script, you can specify a different LOAD and RUN locations.

sections {
 .text { *(.text); } >FLASH
 .tcm {
       *(.tcm);
  } >TCM_MEM AT>FLASH
  .data { *(.data); } > RAM
  .bss : NOLOAD { *(.bss); } > RAM
}

特别注意AT>FLASH.

另见:gnu 链接器映射文件... 以及 stackoverflow 上的更多内容.Gnu Ld 手册有关于LMA 部分(LOAD 地址).您的 LMA 将是 flash,但 VMA(RUN 地址)将是 TCM.上面的手动链接还显示了如何复制.RAMFLASHTCM_MEM 定义为 ld MEMORY 信息,取决于您的电路板的地址.所有这些都将记录在 MAP 文件中.请务必生成 MAP 文件并检查地址以仔细检查您的 ld 脚本.

See also: gnu linker map file... and many more on stackoverflow. The Gnu Ld manual has information on LMA sections (LOAD address). Your LMA would be flash, but the VMA (RUN address) would be TCM. The manual link above also shows how to copy. The RAM, FLASH, and TCM_MEM are defined with ld MEMORY information, depending on the addresses are for your board. All of this will be documented in a MAP file. Be sure to generate a MAP file and examine the addresses to double check your ld script.

第二种情况也需要一个副本(在启动时或至少在第一个 TCM 功能使用之前).但是,编译器可以使用绝对地址,并且它们将在TCM 内存中.并且主DRAM内的任何函数都可以直接调用TCM函数.对于第一种情况,您必须使用函数指针来调用TCM 代码.如果你希望将全局变量放在这个内存中,你可以使用属性将它们放在不同的部分,并使用gnu ld来适当地放置它们.我认为有 ITCMDTCM 吗?所以也许这不适用于您,或者您需要两个部分.

The 2nd case also requires a copy (at start-up or at least before the first TCM function use). However, the compiler can use absolute addresses and they will be in the TCM memory. Also any function within the main DRAM can call the TCM function directly. With the first case, you must use function pointers to call the TCM code. If you wish global variables to be placed in this memory, you can use attributes to put them in different sections and use gnu ld to place them appropriately. I think there is ITCM and DTCM? So maybe this doesn't apply for you, or you need two sections.

链接描述文件更通用,如果您将复杂的功能放在TCM 中,它将发挥最佳效果.仅使用 -fpic 等和复制可能会使事情快速运行,尤其是当您只有一个 pure 函数时.

The linker script is more generic and will work best if you put complicated functionality in the TCM. Just using -fpic, etc and copying may get things working quickly, especially if you only have a single pure function.

这篇关于如何在 ARM 架构上从 RAM 运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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