arm-none-eabi-gcc编译stm32,去掉C++异常处理,精简flash占用大小

如果arm-none-eabi-gcc编译stm32代码,出来的固件里包含类似下面这一堆包含unwind和throw的函数:

<L23__gxx_exception_cleanup19_Unwind_Reason_CodeP21_Unwind_Control_Block>:
<L21base_of_encoded_valuehP15_Unwind_Context>:
<L17parse_lsda_headerP15_Unwind_ContextPKhP16lsda_header_info>:
<_gnu_unwind_get_pr_addr>:
<_gnu_unwind_24bit.isra.1>:
<_gnu_Unwind_RaiseException>:
<_gnu_Unwind_ForcedUnwind>:
<_gnu_Unwind_Resume>:
<_gnu_Unwind_Resume_or_Rethrow>:
<_gnu_Unwind_Backtrace>:
<_gnu_unwind_pr_common>:
<_aeabi_unwind_cpp_pr0>:
<_aeabi_unwind_cpp_pr1>:
<_aeabi_unwind_cpp_pr2>:
<_gnu_Unwind_Restore_VFP>:
<_gnu_Unwind_Save_VFP>:
<_gnu_Unwind_Restore_VFP_D>:
<_gnu_Unwind_Save_VFP_D>:
<_gnu_Unwind_Restore_VFP_D_16_to_31>:
<_gnu_Unwind_Save_VFP_D_16_to_31>:
<_gnu_Unwind_Restore_WMMXD>:
<_gnu_Unwind_Save_WMMXD>:
<_gnu_Unwind_Restore_WMMXC>:
<_gnu_Unwind_Save_WMMXC>:
<Unwind_ForcedUnwind>:
<nextunwind_byte>:
<_gnu_unwind_execute>:
<_gnu_unwind_frame>:
<_cxa_throw>:
<_cxa_rethrow>:
<_gnu_Unwind_Resume_or_Rethrow>:
<Unwind_Resume_or_Rethrow>:

这说明固件里包含了C++异常处理的函数,嵌入式软件一般不会用到C++的异常处理功能,可以在makefile中CXXFLAGS增加-fno-unwind-tables -fno-exceptions -fno-asynchronous-unwind-tables选项,去掉这些函数。

如果代码的makefile里增加这些选项后,编译出来的固件里任然有那些函数,则很有可能是引用的一些外部静态库包含进来的,最常见的是标准的libc库。在arm-none-eabi-gcc编译器5.4版本的readme.txt中有下面一段:

* C Libraries usage *

This toolchain is released with two prebuilt C libraries based on newlib:
one is the standard newlib and the other is newlib-nano for code size.
To distinguish them, we rename the size optimized libraries as:

  libc.a --> libc_nano.a
  libg.a --> libg_nano.a

To use newlib-nano, users should provide additional gcc compile and link time
option:
 --specs=nano.specs

At compile time, a 'newlib.h' header file especially configured for newlib-nano
will be used if --specs=nano.specs is passed to the compiler.

Nano.specs also handles two additional gcc libraries: libstdc++_nano.a and
libsupc++_nano.a, which are optimized for code size.

For example:
$ arm-none-eabi-gcc src.c --specs=nano.specs $(OTHER_OPTIONS)

意思是编译器包含2种基于newlib的C库,一种是标准板的newlib,另一种是优化代码大小的newlib-nano,增加编译选项–specs=nano.specs即可使用newlib-nano。

实测增加这些编译链接选项后,能够删除固件中那些异常处理相关的函数,缩减20多KB的flash占用空间。

0 0 投票数
文章评分
订阅评论
提醒

0 评论
内联反馈
查看所有评论