GCC:如何在 MCU 上完全禁用堆使用? [英] GCC: How to disable heap usage entirely on an MCU?

查看:31
本文介绍了GCC:如何在 MCU 上完全禁用堆使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在基于 ARM Cortex-M 的 MCU 上运行的应用程序,它是用 C 和 C++ 编写的.我使用 gccg++ 来编译它,并希望完全禁用任何堆使用.

在 MCU 启动文件中,堆大小已经设置为 0.除此之外,我还想禁止在代码中意外使用堆.

换句话说,我希望链接器(和/或编译器)在 malloccallocfree 函数或 newnew[]deletedelete[] 运算符.>

到目前为止,我已经尝试了 -nostdlib,它给了我诸如 undefined reference to _start 之类的问题.我也尝试过 -nodefaultlibs 但当我尝试调用 malloc 时,它仍然没有抱怨.这样做的正确方法是什么?

注意事项:

  • 此应用在裸机"上运行,没有操作系统.
  • 我还想避免在 3rd 方代码(特定于供应商的库、标准库、printf 等)中使用任何 malloc.
  • 我完全同意不使用需要动态内存分配的 C/C++ 标准库部分.
  • 我更喜欢编译时而不是运行时解决方案.

解决方案

我不确定这是最好的方法,但是您可以使用 ld 的 --wrap 标志(它可以通过 gcc 使用 -Wl).

这个想法是 --wrap 允许您要求 ld 将真实"符号重定向到您的自定义符号;例如,如果您执行 --wrap=malloc,则 ld 将查找要调用的 __wrap_malloc 函数而不是原始的 `malloc.

现在,如果你执行 --wrap=malloc 而没有定义 __wrap_malloc 如果没有人使用它,你将逃脱它,但是如果任何引用 malloc 的人都会收到链接错误.

$ cat test-nomalloc.c#include int main() {#ifdef USE_MALLOCmalloc(10);#万一返回0;}$ gcc test-nomalloc.c -Wl,--wrap=malloc$ gcc test-nomalloc.c -DUSE_MALLOC -Wl,--wrap=malloc/tmp/ccIEUu9v.o:在函数main"中:test-nomalloc.c:(.text+0xa):对`__wrap_malloc'的未定义引用collect2:错误:ld 返回 1 个退出状态

对于 new,您可以使用变形名称 _Znwm (operator new(unsigned long)) 和 _Znam(operator new[](unsigned long)),这应该是每个 new 最终都应该得到的.

I have an application that runs on an ARM Cortex-M based MCU and is written in C and C++. I use gcc and g++ to compile it and would like to completely disable any heap usage.

In the MCU startup file the heap size is already set to 0. In addition to that, I would also like to disallow any accidental heap use in the code.

In other words, I would like the linker (and/or the compiler) to give me an error when the malloc, calloc, free functions or the new, new[], delete, delete[] operators are used.

So far I've tried -nostdlib which gives me issues like undefined reference to _start. I also tried -nodefaultlibs but that one still does not complain when I try to call malloc. What is the right way to do this?

Notes:

  • This app runs on "bare metal", there is no operating system.
  • I would also like to avoid any malloc usage in 3rd-party code (vendor-specific libraries, the standard library, printf etc.).
  • I'm fully okay with not using the parts of the C / C++ standard libraries that would require dynamic memory allocations.
  • I'd prefer a compile-time rather than a run-time solution.

解决方案

I'm not sure it's the best way to go, however you can use the --wrap flag of ld (which can pass through gcc using -Wl).

The idea is that --wrap allows you to ask to ld to redirect the "real" symbol to your custom one; for example, if you do --wrap=malloc, then ld will look for your __wrap_malloc function to be called instead of the original `malloc.

Now, if you do --wrap=malloc without defining __wrap_malloc you will get away with it if nobody uses it, but if anyone references malloc you'll get a linking error.

$ cat test-nomalloc.c 
#include <stdlib.h>

int main() {
#ifdef USE_MALLOC
    malloc(10);
#endif
    return 0;
}
$ gcc test-nomalloc.c -Wl,--wrap=malloc
$ gcc test-nomalloc.c -DUSE_MALLOC -Wl,--wrap=malloc
/tmp/ccIEUu9v.o: In function `main':
test-nomalloc.c:(.text+0xa): undefined reference to `__wrap_malloc'
collect2: error: ld returned 1 exit status

For new you can use the mangled names _Znwm (operator new(unsigned long)) and _Znam (operator new[](unsigned long)), which should be what every new should come down to in the end.

这篇关于GCC:如何在 MCU 上完全禁用堆使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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