Skip to content

Commit d69e872

Browse files
committed
it8xxx2: ensure kernel in ram code section
This change was made to make sure kernel still can run from ram code section under LTO enabled. Signed-off-by: Tim Lin <tim2.lin@ite.corp-partner.google.com>
1 parent feae716 commit d69e872

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

soc/ite/ec/it8xxx2/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
if(CONFIG_LTO AND CONFIG_SOC_IT8XXX2_KERNEL_IN_RAM)
2+
include(${CMAKE_CURRENT_SOURCE_DIR}/kernel_no_lto.cmake)
3+
endif()
4+
15
zephyr_sources(soc.c check_regs.c vector.S)
26
zephyr_include_directories(.)
37

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright (c) 2025 ITE Corporation. All Rights Reserved.
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
import re
8+
import sys
9+
from pathlib import Path
10+
11+
MARKER = "# AUTO_INJECTED_NO_LTO"
12+
kernel_cmake = Path(sys.argv[1])
13+
14+
if not kernel_cmake.exists():
15+
sys.exit(0)
16+
17+
content = kernel_cmake.read_text()
18+
19+
# Remove injected block
20+
cleaned = re.sub(rf"{re.escape(MARKER)}[^\n]*\n.*$", "", content, flags=re.S)
21+
cleaned = cleaned.rstrip() + "\n"
22+
23+
if content != cleaned:
24+
kernel_cmake.write_text(cleaned)
25+
print(f"[no-LTO] Cleaned up injection from {kernel_cmake}")
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
set(KERNEL_CMAKE "${ZEPHYR_BASE}/kernel/CMakeLists.txt")
2+
set(MARKER "# AUTO_INJECTED_NO_LTO")
3+
4+
execute_process(
5+
COMMAND ${CMAKE_COMMAND} -E env python3
6+
${CMAKE_CURRENT_LIST_DIR}/cleanup_injected_code.py
7+
${KERNEL_CMAKE}
8+
RESULT_VARIABLE CLEAN_RESULT
9+
OUTPUT_QUIET
10+
ERROR_QUIET
11+
)
12+
13+
if(NOT CLEAN_RESULT EQUAL 0)
14+
message(WARNING "[no-LTO] Warning: cleanup script encountered an issue, continuing anyway.")
15+
endif()
16+
17+
# Prepare the code snippet to inject
18+
message(STATUS "[no-LTO] Injecting no-LTO configuration into kernel/CMakeLists.txt...")
19+
set(INJECT_CODE "${MARKER}
20+
set_source_files_properties(busy_wait.c device.c kheap.c idle.c mutex.c sem.c work.c
21+
thread.c sched.c timeout.c time.c poll.c events.c banner.c
22+
system_work_q.c
23+
PROPERTIES COMPILE_FLAGS \"-fno-lto -g\")")
24+
25+
# Append the injected code to the end of the file
26+
file(APPEND ${KERNEL_CMAKE} "${INJECT_CODE}")
27+
message(STATUS "[no-LTO] Injection complete.")
28+
29+
# Define a cleanup target to remove the injected code after the build
30+
add_custom_target(cleanup_kernel_injection ALL
31+
COMMAND ${CMAKE_COMMAND} -E env python3
32+
${CMAKE_CURRENT_LIST_DIR}/cleanup_injected_code.py
33+
${KERNEL_CMAKE}
34+
COMMENT "Cleaning up kernel injection"
35+
)
36+
37+
# Ensure the cleanup target runs after the final build stage
38+
add_dependencies(cleanup_kernel_injection zephyr_final)

0 commit comments

Comments
 (0)