boards/arm/qemu: Fix KASAN global sections placement in linker script

1. The .kasan.unused and .kasan.global sections contain compiler-generated
data with the WRITE flag (.data..LASAN*), but the linker script placed
them before .text without specifying a memory region. The linker could
not put writable sections into the read-only ROM (rx) region, so it
silently placed them at 0x40000000 in RAM, creating an extra LOAD
segment that conflicts with QEMU virt's RAM layout and causes boot
failure.
2. The .kasan.global should be placed before .data because .data
patten (*(.data*)) includes .kasan.global pattern (*(.data..LASAN0)),
and it cause kasan_global.py cannot find .kasan.global section to
generate the g_global_region array.
3. Move .kasan.shadows from before .text to after .rodata. Placing it
before .text causes .text to shift when .kasan.shadows transitions
from empty (pass 1) to populated (pass 2+), preventing the multi-pass
link addresses from converging. After .rodata it does not affect any
upstream section addresses.
4. Add CMake post-build step to strip .kasan.unused and .kasan.global
sections from the final binary, matching the Makefile build behavior.

Signed-off-by: leisiji <2265215145@qq.com>
This commit is contained in:
leisiji 2026-06-14 05:47:30 +08:00 committed by Mateusz Szafoni
parent dd86c92405
commit 85337c0efc
2 changed files with 26 additions and 15 deletions

View file

@ -34,21 +34,6 @@ MEMORY
SECTIONS
{
/* where the global variable out-of-bounds detection information located */
#ifdef CONFIG_MM_KASAN_GLOBAL
.kasan.unused : {
*(.data..LASANLOC*)
}
.kasan.global : {
KEEP (*(.data..LASAN0))
KEEP (*(.data.rel.local..LASAN0))
}
.kasan.shadows : {
*(.kasan.shadows)
}
#endif
.text : {
_stext = .; /* Text section */
__text_start = .;
@ -93,6 +78,19 @@ SECTIONS
_erodata = .;
} > ROM
#ifdef CONFIG_MM_KASAN_GLOBAL
.kasan.shadows : {
KEEP(*(.kasan.shadows))
} > ROM
.kasan.unused : {
*(.data..LASANLOC*)
} > RAM
.kasan.global : {
KEEP (*(.data..LASAN0))
KEEP (*(.data.rel.local..LASAN0))
} > RAM
#endif
_eronly = LOADADDR(.data);
.data : { /* Data */
_sdata = .;

View file

@ -134,6 +134,19 @@ define_multiple_link_target(final_nuttx second_link final)
# fixing timing dependencies
add_dependencies(nuttx_post final_nuttx)
# Strip .kasan.unused and .kasan.global sections from the final binary. These
# sections are only needed during the intermediate link passes for
# kasan_global.py to extract global variable descriptors. At runtime they sit at
# the start of RAM (0x40000000) and conflict with QEMU's DTB placement.
if(CONFIG_MM_KASAN_GLOBAL)
add_custom_command(
TARGET final_nuttx
POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -R .kasan.unused -R .kasan.global final_nuttx
COMMENT "Stripping .kasan.unused and .kasan.global sections")
endif()
# finally use final_nuttx to overwrite the already generated nuttx
add_custom_command(
TARGET final_nuttx