Build improperly does a partial discard of 'const' defined variables
I have run across a build problem that appears to impact Arm platforms. The problem involves improper discarding of ‘const’ defined variables that are only referenced within a single file getting partially optimized out by the final linker stage. The map file shows the symbol in the Discard section but there is code still referencing that variable so the result is that the variable is located at address 0… all const variables that have this problem are located at address zero because the final linker stage didn’t fixup the address with a proper location.
This problem can be seen in the ‘hello world’ sample by looking at what the linker does with _main_thread defined in init.c. The map file shows it as a discarded symbol but it is still in the elf file and you can inspect it with gdb and see that it is at address 0.
The final link stage links some of the libraries using the –whole-archive flag and some of the others using the –no-whole-archive flag so as an experiment I linked all libraries using just the whole-archive flag and it appeared to properly place the const variable.
Also note that this doesn’t seem to be a problem when building for x86 targets.
I added a bug report on this: https://github.com/zephyrproject-rtos/zephyr/issues/12905
** PROPRIETARY & COMPANY-CONFIDENTIAL **