中预留一定范围的内存

中预留一定范围的内存

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

问题描述

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



我正在使用ARM GNU toolchain进行编译。

解决方案

这个问题有几种解决方案。使用链接器

  • 注释变量

  • $ b
  • 全局范围

  • 易变(可能)


  • 链接器脚本



    显然,您可以使用链接器文件来执行此操作。这是该工作的适当工具。传递链接器 - verbose 参数以查看默认脚本的内容。您可以修改它以精确地保留内存。



    变量属性



    使用更新版本的 gcc ,也将做你想要的。 版本都会支持这个。它也比链接器脚本更简单;但只有链接脚本以可靠的方式精确控制的位置。



    全局范围



    你也可以给你的数组全局作用域,编译器不应该消除它。如果您使用,则这可能并非如此。理论上,编译器可以消除静态的 volatile >数组。当你有涉及数组的代码时, volatile 会起作用。它修改访问行为,以便编译器永远不会缓存对该范围的访问。 至少我的行为还不清楚,我不会推荐这种方法。它可以与编译器的一些版本(和优化级别)一起工作,而不是其他的。 限制

    另外,链接器选项可以消除由全局范围和易失性方法保留的空间,因为符号可能不以任何方式以对象格式注释;请参阅链接描述文件(KEEP)



    只有链接器脚本覆盖写入。您需要在保留区域之前放置栈顶。通常情况下,增长,增长减少。所以这两个相互碰撞。这对您的环境/ C库尤为重要(例如,是典型的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. 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)中预留一定范围的内存,并防止使用该内存的同一应用程序的堆/堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-23 15:19