Skip to content

Commit bdd0b63

Browse files
committed
Zeroing of section .bss is now conditional on .bss start & end being defined rather than .data start & end being defined (this was likely a copy-pasta error when re-purposing code from the .data section above it to make the .bss piece). Also, the pointer jumps forward 4 bytes at a time now rather than one byte, given that pSrc and pDest are pointers to a uint32_t, meaning memory is written 4 bytes at a time.
A bunch of processor architectures (especially the simple ones) can only write int32 values to a memory address aligned at a 4-byte boundary, and int16 values aligned at a 2-byte boundary. If one writes C/C++ code not taking this into account, the compiler is forced to generate all sorts of shifts and tricks to make that code work, which is very suboptimal code by every measure. (In this case suboptimal means having every byte but the first and last three written 4 or more times over and some extra space in flash memory for the tricks.) If one were to do have written the init of .data and .bss in assembly (meaning the compiler not saves your ass by inserting shifts & tricks to your doing it per-byte) this would have been a HardFault on most Cortex CPUs due to the alignment requirement.
1 parent 84c09b3 commit bdd0b63

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

cores/arduino/cortex_handlers.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ void Reset_Handler(void)
146146
pDest = &__data_start__;
147147

148148
if ((&__data_start__ != &__data_end__) && (pSrc != pDest)) {
149-
for (; pDest < &__data_end__; pDest++, pSrc++)
149+
for (; pDest < &__data_end__; pDest += sizeof(uint32_t), pSrc += sizeof(uint32_t))
150150
*pDest = *pSrc;
151151
}
152152

153153
/* Clear the zero section */
154-
if ((&__data_start__ != &__data_end__) && (pSrc != pDest)) {
155-
for (pDest = &__bss_start__; pDest < &__bss_end__; pDest++)
154+
if ((&__bss_start__ != &__bss_end__)) {
155+
for (pDest = &__bss_start__; pDest < &__bss_end__; pDest += sizeof(uint32_t))
156156
*pDest = 0;
157157
}
158158

0 commit comments

Comments
 (0)