如何在数据部分 (RAM) 中保留一定范围的内存并防止使用该内存的同一应用程序的堆/堆栈? [英] How to reserve a range of memory in data section (RAM) and the prevent heap/stack of same application using that memory?

查看:14
本文介绍了如何在数据部分 (RAM) 中保留一定范围的内存并防止使用该内存的同一应用程序的堆/堆栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 RAM 中保留/分配一定范围的内存,并且同一应用程序不应覆盖或使用该范围的内存进行堆/堆栈存储.如何在受堆栈/堆覆盖保护的 ram 中分配一定范围的内存?我想过向应用程序本身添加(或分配)一个数组并保留内存,但它被编译器优化了,因为它没有在应用程序中的任何地方被引用.

我使用 ARM GNU 工具链进行编译.

解决方案

这个问题有几种解决方案.按最好到最坏的顺序列出,

  1. 使用链接器
  2. 注释变量
  3. 全球范围
  4. 不稳定(可能)

链接脚本

您显然可以使用链接器文件来执行此操作.它是完成这项工作的合适工具.向链接器传递 --verbose 参数以查看默认脚本是什么.然后您可以修改它以精确保留内存.

变量属性

随着 gcc 的更新版本,attribute used 也会做你想做的.大多数现代 gcc 版本将支持这个.它也比链接脚本容易得多;但只有链接器脚本才能以可靠的方式精确控制的位置.

全局范围

你也可以给你的数组全局范围,编译器不应该消除它.如果您使用链接时间优化,这可能不是真的.>

易变

理论上,编译器可以消除静态 volatile 数组.当您有涉及数组的代码时,volatile 就会发挥作用.它修改访问行为,因此编译器永远不会缓存对该范围的访问.博士.Dobbs on volatile 至少我不清楚这种行为,我不推荐这种方法.它可能适用于编译器的某些版本(和优化级别),而不适用于其他版本.

限制

此外,链接器选项 -gc-sections, 可以消除全局作用域和 volatile 方法保留的空间,因为符号可能不会以任何方式在对象格式中进行注释;见链接脚本(KEEP).

只有链接脚本可以绝对限制堆栈的覆盖.您需要在保留区域之前放置堆栈顶部.通常,向上增长,向下增长.所以这两者相互碰撞.这是特定于您的环境/C 库(例如 newlib 是典型的 ARM 裸机库).查看链接器文件将提供最好的线索.

我的猜测是您想要一个休耕区在系统崩溃时为某种调试信息保留?对您的问题进行更明确的解释会有所帮助.你似乎不关心内存的位置,所以我猜这与硬件无关.

I want to reserve/allocate a range of memory in RAM and the same application should not overwrite or use that range of memory for heap/stack storage. How to allocate a range of memory in ram protected from stack/heap overwrite? I thought about adding(or allocating) an array to the application itself and reserve memory, But its optimized out by compiler as its not referenced anywhere in the application.

I am using ARM GNU toolchain for compiling.

解决方案

There are several solutions to this problem. Listing in best to worse order,

  1. Use the linker
  2. Annotate the variable
  3. Global scope
  4. Volatile (maybe)

Linker script

You can obviously use a linker file to do this. It is the proper tool for the job. Pass the linker the --verbose parameter to see what the default script is. You may then modify it to precisely reserve the memory.

Variable Attributes

With more recent versions of gcc, the attribute used will also do what you want. Most modern gcc versions will support this. It is also significantly easier than the linker script; but only the linker script gives precise control over the position of the hole in a reliable manner.

Global scope

You may also give your array global scope and the compiler should not eliminate it. This may not be true if you use link time optimization.

Volatile

Theoretically, a compiler may eliminate a static volatile array. The volatile comes into play when you have code involving the array. It modifies the access behavior so the compiler will never caches access to that range. Dr. Dobbs on volatile At least the behavior is unclear to me and I would not recommend this method. It may work with some versions (and optimization levels) of the compiler and not others.

Limitations

Also, the linker option -gc-sections, can eliminate space reserved with either the global scope and the volatile methods as the symbol may not be annotated in any way in object formats; see the linker script (KEEP).

Only the Linker script can definitely restrict over-writes by the stack. You need to position the top of the stack before your reserved area. Typically, the heap grows up and the stack grows down. So these two collide with each other. This is particular to your environment/C library (for instance newlib is the typical ARM bare metal library). Looking at the linker file will give the best clue to this.

My guess is you want a fallow area to reserve for some sort of debugging information in the event of a system crash? A more explicit explaination of you problem would be helpful. You don't seem to be concerned with the position of the memory, so I guess this is not hardware related.

这篇关于如何在数据部分 (RAM) 中保留一定范围的内存并防止使用该内存的同一应用程序的堆/堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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