Description
Describe the bug
Concerning Zephyr v3.2.0, present since release v3.0.0, introduced by commit
70c403c - arch: arm: core: aarch32: introduce basic ARMv7 MMU support
The linker script for aarch32 cortex A and R places z_mapped_start after the exception vector section, just before the code text section.
This causes the Newlib malloc_prepare to allocate a heap at the same physical address as the exception vector, because it's page frame is marked by mmu as available rather than pinned.
The effect is that when allocated heap memory is written to, this overwrites the exception vector, causing undefined operations.
This does not affect aarch32 cortex M as it does not have MMU.
This does not affect minimal libc.
This does not affect aarch64 as that linker script places z_mapped_start before the exception vector.
This does not affect XIP targets.
I believe this only affects CPU_CORTEX_A9 with Newlib and no XIP.
To Reproduce
This is what we did to encounter the problem:
- create a soc using CPU_CORTEX_A9 (we used another new ARMv7-A CPU_AARCH32_CORTEX_A which is ISA_THUMB2 but that should be irrelevant)
- create a board using that soc, and include in the _defconfig file CONFIG_HEAP_MEM_POOL_SIZE=4096 and CONFIG_NEWLIB_LIBC=y
- build using the hello_world sample or any sample for that matter
- execute and observe
Expected behavior
I think the linker script for aarch32 cortex A should define z_mapped_start before the exception vector
Impact
We have currently implemented a workaround by modifying as per above.
Logs and console output
None.
Environment (please complete the following information):
Debian, Zephyr SDK 0.15 toolchains, tag v3.1.0 2ddd73f
Proposed patch
diff --git a/include/zephyr/arch/arm/aarch32/cortex_a_r/scripts/linker.ld b/include/zephyr/arch/arm/aarch32/cortex_a_r/scripts/linker.ld
index 6be9667943..21863a8bd2 100644
--- a/include/zephyr/arch/arm/aarch32/cortex_a_r/scripts/linker.ld
+++ b/include/zephyr/arch/arm/aarch32/cortex_a_r/scripts/linker.ld
@@ -120,6 +120,11 @@ SECTIONS
* zephyr_linker_sources(ROM_START ...). This typically contains the vector
* table and debug information.
*/
+#ifndef CONFIG_XIP
+ z_mapped_start = .;
+#endif
#include <snippets-rom-start.ld>
} GROUP_LINK_IN(ROMABLE_REGION)
@@ -133,10 +138,6 @@ SECTIONS
SECTION_PROLOGUE(_TEXT_SECTION_NAME,,)
{
. = ALIGN(_region_min_align);
__text_region_start = .;
-#ifndef CONFIG_XIP
- z_mapped_start = .;
-#endif
#include <zephyr/linker/kobject-text.ld>
If you agree with my assessment of this bug, I can open a merge request.