Comment 4 for bug 1955320

Revision history for this message
David Brown (davidbrown) wrote :

The reason this is undefined behaviour is that the three symbols __text_end__, __data_start__ and __data_end__ will be declared as extern objects. Even if they are declared as arrays, if you start with a pointer that points to one variable, and increment it, you can never end up with a pointer that points at another variable. This can lead to unexpected optimisation behaviour in some cases.

However, I don't see how the compiler could end up removing the assignments at the start of the function. I would want to see the declarations of the external symbols here, and know the command line flags used.

A very useful tool for this is the online compiler at <https://godbolt.org>. You can select "ARM gcc 10.2.1 (none)" as your compiler here and add the flags such as "-mcpu=cortex-m33 -O2 -Wall -Wextra" and see the assembly results. You can also use "share" to get a link, and put that in a comment here, so that others can see exactly the effect you are facing.

In general, I disagree with Liviu here - C and C++ are perfectly good languages for writing startup code and there is no need to resort to assembly. But you do need to understand the limitations for using C and C++ before main(), and you need to be careful with linker symbols that might not be quite right for C.