【发布时间】:2020-11-02 15:20:23
【问题描述】:
在使用 C 语言为 RV32IM 目标 (RISC-V) 开发裸机固件时,我在启用 LTO 时遇到了链接错误:
/home/duranda/riscv/lib/gcc/riscv64-unknown-elf/10.2.0/../../../../riscv64-unknown-elf/bin/ld: /tmp/firmware.elf.5cZNyC.ltrans0.ltrans.o: in function `.L0 ':
/home/duranda/whatever/firmware.c:493: undefined reference to `memset'
但是在我的固件中没有调用memset。 memset 由 GCC 在优化 as described here 期间插入。该构建使用 GCC -Os 和 -flto -fuse-linker-plugin 标志针对大小进行了优化。此外,-fno-builtin-memset -nostdinc -fno-tree-loop-distribute-patterns -nostdlib -ffreestanding 标志用于防止在优化期间使用memset,并且不包含标准库。
如何防止在 LTO 期间插入memset?请注意,固件不应与 libc 链接。我还尝试提供memset 的自定义实现,但链接器不想将它用于优化期间插入的memset(仍然抛出未定义引用)。
【问题讨论】:
标签: gcc linker compiler-optimization lto freestanding