将 LTO 与 arm-none-eabi 和 newlib-nano 结合使用

Using LTO with arm-none-eabi and newlib-nano

我正在为 STM32F103 开发一个裸机嵌入式项目,我正在使用 GNU ARM Embedded version 7-2017-q4-major 工具链。我目前正在通过 GNU ARM Eclipse 进行编译。

我正处于需要开始优化项目以提高速度的地步,作为第一件事,我当然尝试打开所有优化器标志。其他一切都很好,但是当我尝试使用 -flto 打开 Link 时间优化时,我在最后一步出现链接器错误:

Invoking: Cross ARM C++ Linker
arm-none-eabi-g++ -mcpu=cortex-m3 -mthumb -O3 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -flto -Wall -Wextra  -g3 -T mem.ld -T libs.ld -T sections.ld -nostartfiles -Xlinker --gc-sections -L"../ldscripts" -Wl,-Map,"Project.map" -Xlinker --cref --specs=nano.specs -o "Project.elf"  ./tiny-mt/tinymt/tinymt32.o  ... .o   
/Users/me/opt/gcc-arm-none-eabi-7-2017-q4-major/bin/../lib/gcc/arm-none-eabi/7.2.1/../../../../arm-none-eabi/lib/thumb/v7-m/libg_nano.a(lib_a-fstatr.o): In function `_fstat_r':
fstatr.c:(.text._fstat_r+0xe): undefined reference to `_fstat'
/Users/me/opt/gcc-arm-none-eabi-7-2017-q4-major/bin/../lib/gcc/arm-none-eabi/7.2.1/../../../../arm-none-eabi/lib/thumb/v7-m/libg_nano.a(lib_a-isattyr.o): In function `_isatty_r':
isattyr.c:(.text._isatty_r+0xc): undefined reference to `_isatty'
collect2: error: ld returned 1 exit status
make: *** [Project.elf] Error 1

这显然是因为 newlib-nano 没有使用 LTO 编译?

那么我该如何让它发挥作用呢?我想我可以尝试 compiling newlib-nano myself 并添加必要的标志(并更改工具以使用 -gcc-ar 等),但我 imagine/hope 有人已经这样做了吗?我的 google-fu 不足以找到有用的东西。

nosys.specs specifyes to link -lnosys with should provide stub implementations for _fstat 和 _isatty 以及其他 standard/posix 函数。
来自 gcc manual link options:

-llibrary
Search the library named library when linking. ...
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.

因此,如果您将 --specs=nano.specs 移动到 link 命令的末尾,您的来源将 link 与 -lnosys 并正确使用 _isatty_fstat 来自 libnosys 库的实现。像这样:

arm-none-eabi-g++ -mcpu=cortex-m3 -mthumb -O3 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -flto -Wall -Wextra  -g3 -T mem.ld -T libs.ld -T sections.ld -nostartfiles -Xlinker --gc-sections -L"../ldscripts" -Wl,-Map,"Project.map" -Xlinker --cref -o "Project.elf"  ./tiny-mt/tinymt/tinymt32.o  ... .o --specs=nano.specs

我可以猜测没有 LTO 编译的 newlib-nano 与它无关。我将多个项目与 LTO 和 newlib-nano 一起使用,它们工作得非常完美。通常 LTO 工作得很好,删除了抽象函数层,可预测,优化非常好,但我只有 2 年的使用经验。如果我真的需要速度(并且可以忍受非标准行为),我会使用 -Ofast -flto -fno-fat-lto-objects