This is an automated email from the ASF dual-hosted git repository.

raiden00pl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 85337c0efc216a5304b35d2880438d86d443bdf6
Author: leisiji <[email protected]>
AuthorDate: Sun Jun 14 05:47:30 2026 +0800

    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 <[email protected]>
---
 boards/arm/qemu/qemu-armv7a/scripts/dramboot.ld | 28 ++++++++++++-------------
 cmake/nuttx_multiple_link.cmake                 | 13 ++++++++++++
 2 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/boards/arm/qemu/qemu-armv7a/scripts/dramboot.ld 
b/boards/arm/qemu/qemu-armv7a/scripts/dramboot.ld
index 19e481ecd6d..b0e9b276042 100644
--- a/boards/arm/qemu/qemu-armv7a/scripts/dramboot.ld
+++ b/boards/arm/qemu/qemu-armv7a/scripts/dramboot.ld
@@ -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 = .;
diff --git a/cmake/nuttx_multiple_link.cmake b/cmake/nuttx_multiple_link.cmake
index fef695f52dc..141da198a72 100644
--- a/cmake/nuttx_multiple_link.cmake
+++ b/cmake/nuttx_multiple_link.cmake
@@ -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

Reply via email to