From 0571348d95216059895e899325effcf09e42bcaf Mon Sep 17 00:00:00 2001 From: Guennadi Liakhovetski Date: Wed, 12 Mar 2025 14:21:48 +0100 Subject: [PATCH] llext: fix alignment of cold / detached sections Cold sections within LLEXT modules should be page-size aligned to allow setting permission flags. They're never copied after being initially placed in DRAM, so alignment should happen already in ELF files. Signed-off-by: Guennadi Liakhovetski --- scripts/llext_link_helper.py | 23 +++++++++++++++++++++++ zephyr/CMakeLists.txt | 6 +++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/scripts/llext_link_helper.py b/scripts/llext_link_helper.py index 194068321878..f10777c9918f 100755 --- a/scripts/llext_link_helper.py +++ b/scripts/llext_link_helper.py @@ -26,6 +26,10 @@ def parse_args(): parser.add_argument('params', nargs='+', help='Additional linker parameters') parser.add_argument("-f", "--file", required=True, type=str, help='Object file name') + parser.add_argument("-c", "--copy", required=True, type=str, + help='Objcopy command') + parser.add_argument("-o", "--output", required=True, type=str, + help='Output file name') parser.add_argument("-t", "--text-addr", required=True, type=str, help='.text section address') parser.add_argument("-s", "--size-file", required=True, type=str, @@ -148,11 +152,16 @@ def main(): # we accept only the .coldrodata name for now. dram_addr = 0 + first_dram_text = None + first_dram_rodata = None for section in executable: s_alignment = section.header['sh_addralign'] s_name = section.name + if not first_dram_text: + first_dram_text = s_name + dram_addr = align_up(dram_addr, s_alignment) command.append(f'-Wl,--section-start={s_name}=0x{dram_addr:x}') @@ -163,6 +172,9 @@ def main(): s_alignment = section.header['sh_addralign'] s_name = section.name + if not first_dram_rodata: + first_dram_rodata = s_name + dram_addr = align_up(dram_addr, s_alignment) command.append(f'-Wl,--section-start={s_name}=0x{dram_addr:x}') @@ -196,9 +208,20 @@ def main(): start_addr += section.header['sh_size'] + command.extend(['-o', f'{args.file}.tmp']) command.extend(args.params) subprocess.run(command) + copy_command = [args.copy] + + if first_dram_text: + copy_command.extend(['--set-section-alignment', f'{first_dram_text}=4096']) + if first_dram_rodata: + copy_command.extend(['--set-section-alignment', f'{first_dram_rodata}=4096']) + + copy_command.extend([f'{args.file}.tmp', f'{args.output}']) + subprocess.run(copy_command) + if __name__ == "__main__": main() diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index 194d567819de..d0ba04d4a879 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -107,9 +107,9 @@ function(sof_llext_build module) POST_BUILD COMMAND ${PYTHON_EXECUTABLE} ${SOF_BASE}scripts/llext_link_helper.py -s ${size_file} --text-addr=${CONFIG_LIBRARY_BASE_ADDRESS} - -f ${proc_in_file} ${CMAKE_C_COMPILER} -- - -o ${proc_out_file} ${EXTRA_LINKER_PARAMS} - $ + -c $ + -f ${proc_in_file} -o ${proc_out_file} ${CMAKE_C_COMPILER} -- + ${EXTRA_LINKER_PARAMS} $ ) add_llext_command(TARGET ${module}