https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120393
Bug ID: 120393
Summary: Link-time optimization vs. Os/O2; baremetal memcpy
elimination
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: jas...@chili-chips.com
Target Milestone: ---
Use of `-flto' option generates an error for initialized char string variables.
Here is an example:
char test[] = "Hello world!\r\n";
for (i = 0; i < 14; i++) {
uart_send_char(csr, test[i]);
}
/usr/lib/gcc/riscv64-unknown-elf/13.2.0/../../../riscv64-unknown-elf/bin/ld:
/tmp/ccerPl2Y.ltrans0.ltrans.o: in function `main':
:(.text.startup+0x314): undefined reference to `memcpy'
The error comes from premature optimization, where linker concludes that the
`memcpy' function is not used in the source code, and eliminates it. The
workaround is to accompany the `memcpy' declaration with __attribute__((used)),
such as:
__attribute__((used)) void *memcpy(void *dest, const void *src, size_t
len);
Hence the questions:
Q1) why does `-flto' option make the compile flow optimize out `memcpy'
function, which was actually inserted by the flow, to copy the initialized
strings?
Q2) can other functions outside of `main.cpp', including calls from boot CRT
Assembly code, be similarly eliminated by `-flto'?
This issue is uncovered in Wireguard-FPGA project, where tracked under:
https://github.com/chili-chips-ba/wireguard-fpga/issues/17
It seems related to:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119424