GCC - 如何阻止malloc被链接? [英] GCC - How to stop malloc being linked?

查看:619
本文介绍了GCC - 如何阻止malloc被链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力将我的代码缩小到最小的裸露尺寸!我使用的STM32F0只有32k的闪存,需要很大一部分闪存用于数据存储。我的代码已经在大约20k的闪存大小!其中一些原因是由于使用了STM32的HAL功能,我可以考虑并在以后进行优化。如果需要的话。

然而,我最大的flash使用者是隐含的库例程。我似乎无法删除这些功能。他们不会在我的代码或任何HAL代码中被调用。诸如_malloc_r(1.3k字节)和__vfiprintf_r(3kB)等许多函数正在使用我的大部分Flash。我认为这些都是libc的功能。



有人知道如何去除这些吗?



我已经尝试了不同的优化级别和链接器选项,但目前为止还没有运气。我已经尝试过-nostdlib和--specs = nosys.specs,但没有任何更改。如果我使用_exit等函数的定义删除我的文件,则会出现一个链接器错误,提示库仍然包含在内,并且需要这些。此外,链接器映射表确认存在大量不需要的函数!



有什么建议吗?

解决方案解决方案...我的一些代码包含并称为assert。我删除断言的那一刻调用了我的代码大小减半!我使用了STM32 HAL的assert_param宏,它是一个轻量级的断言,只是重定向到用户定义的函数。



如果有人能向我解释gcc决定在调用assert时包含库函数?我看到assert.h声明了一个外部函数__assert_func。链接器如何知道从库中引用它,而不是只说对__asert_func的未定义引用?


I am struggling to get my code down to minimal bare bones size! I am using a STM32F0 with only 32k flash and need a good part of the flash for data storage. My code is already at approx 20k flash size! Some of this is due to use of the STM32 HAL functions which I can account for and optimise later if needed.

However, my biggest consumer of flash is all the implicitly included library routines. I can not seem to remove these functions. They are not called anywhere in my code or any HAL code. Functions such as _malloc_r (1.3k Bytes), and __vfiprintf_r (3kB) and many others are using a large part of my flash. I think these are all the libc functions. I do not use these and would like them gone!

Does anybody know how to remove these?

I have tried different optimisation levels and linker options but no luck so far. I have tried -nostdlib and --specs=nosys.specs with no change. If I remove my file with definitions for functions such as _exit I get a linker error suggesting that the library is still included and needs these. Also linker map confirms presence of a lot of unwanted functions!

Any suggestions?

解决方案

Solved... Some of my code included and called assert. The moment I removed assert calls my code size more than halved! I instead used the STM32 HALs assert_param macro that is a light weight assert that just redirects to a user defined function.

It would be helpful if someone could explain to me how gcc decides to include library functions when assert is called? I see that assert.h declares an external function __assert_func. How does the linker know to reference it from a library rather than just say "undefined reference to __asert_func"?

这篇关于GCC - 如何阻止malloc被链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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