Skip to content

Commit 0f4f53f

Browse files
leisijixiaoxiang781216
authored andcommitted
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>
1 parent 2822701 commit 0f4f53f

2 files changed

Lines changed: 26 additions & 15 deletions

File tree

boards/arm/qemu/qemu-armv7a/scripts/dramboot.ld

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,6 @@ MEMORY
3434
SECTIONS
3535
{
3636

37-
/* where the global variable out-of-bounds detection information located */
38-
39-
#ifdef CONFIG_MM_KASAN_GLOBAL
40-
.kasan.unused : {
41-
*(.data..LASANLOC*)
42-
}
43-
.kasan.global : {
44-
KEEP (*(.data..LASAN0))
45-
KEEP (*(.data.rel.local..LASAN0))
46-
}
47-
.kasan.shadows : {
48-
*(.kasan.shadows)
49-
}
50-
#endif
51-
5237
.text : {
5338
_stext = .; /* Text section */
5439
__text_start = .;
@@ -93,6 +78,19 @@ SECTIONS
9378
_erodata = .;
9479
} > ROM
9580

81+
#ifdef CONFIG_MM_KASAN_GLOBAL
82+
.kasan.shadows : {
83+
KEEP(*(.kasan.shadows))
84+
} > ROM
85+
.kasan.unused : {
86+
*(.data..LASANLOC*)
87+
} > RAM
88+
.kasan.global : {
89+
KEEP (*(.data..LASAN0))
90+
KEEP (*(.data.rel.local..LASAN0))
91+
} > RAM
92+
#endif
93+
9694
_eronly = LOADADDR(.data);
9795
.data : { /* Data */
9896
_sdata = .;

cmake/nuttx_multiple_link.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,19 @@ define_multiple_link_target(final_nuttx second_link final)
134134

135135
# fixing timing dependencies
136136
add_dependencies(nuttx_post final_nuttx)
137+
138+
# Strip .kasan.unused and .kasan.global sections from the final binary. These
139+
# sections are only needed during the intermediate link passes for
140+
# kasan_global.py to extract global variable descriptors. At runtime they sit at
141+
# the start of RAM (0x40000000) and conflict with QEMU's DTB placement.
142+
if(CONFIG_MM_KASAN_GLOBAL)
143+
add_custom_command(
144+
TARGET final_nuttx
145+
POST_BUILD
146+
COMMAND ${CMAKE_OBJCOPY} -R .kasan.unused -R .kasan.global final_nuttx
147+
COMMENT "Stripping .kasan.unused and .kasan.global sections")
148+
endif()
149+
137150
# finally use final_nuttx to overwrite the already generated nuttx
138151
add_custom_command(
139152
TARGET final_nuttx

0 commit comments

Comments
 (0)