From 1c56b9de5b848d4d76cacdfc05f35c5044e1646b Mon Sep 17 00:00:00 2001 From: Guennadi Liakhovetski Date: Thu, 23 Nov 2023 16:50:20 +0100 Subject: [PATCH] llext: fix read-only extension image When using the LLEXT buffer loader we now avoid copying extensions from storage to allocated memory by pointing directly into the stored image. We then also perform linking and relocation in that memory, which modifies its contents. However, this is impossible if that storage is read-only. Add a Kconfig flag to distinguish between writable and read-only storage types. Also use that flag to decide, whether the extension image in test_llext_simple.c should be defined as const or not. Signed-off-by: Guennadi Liakhovetski --- subsys/llext/Kconfig | 6 ++++++ subsys/llext/llext.c | 3 ++- tests/subsys/llext/src/test_llext_simple.c | 5 ++++- tests/subsys/llext/testcase.yaml | 2 ++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/subsys/llext/Kconfig b/subsys/llext/Kconfig index 9fec16cb230635..b1210b8f2e8f80 100644 --- a/subsys/llext/Kconfig +++ b/subsys/llext/Kconfig @@ -27,6 +27,12 @@ config LLEXT_SHELL_MAX_SIZE help When loading llext with shell it is stored in a temporary buffer of this size +config LLEXT_STORAGE_WRITABLE + bool "llext storage is writable" + help + Select if LLEXT storage is writable, i.e. if extensions are stored in + RAM and can be modified in place + module = LLEXT module-str = llext source "subsys/logging/Kconfig.template.log_config" diff --git a/subsys/llext/llext.c b/subsys/llext/llext.c index 1bc56f4d23be79..77a741f0636524 100644 --- a/subsys/llext/llext.c +++ b/subsys/llext/llext.c @@ -303,7 +303,8 @@ static int llext_copy_section(struct llext_loader *ldr, struct llext *ext, return 0; } - if (ldr->sects[sect_idx].sh_type != SHT_NOBITS) { + if (ldr->sects[sect_idx].sh_type != SHT_NOBITS && + IS_ENABLED(CONFIG_LLEXT_STORAGE_WRITABLE)) { ext->mem[mem_idx] = llext_peek(ldr, ldr->sects[sect_idx].sh_offset); if (ext->mem[mem_idx]) { ext->mem_on_heap[mem_idx] = false; diff --git a/tests/subsys/llext/src/test_llext_simple.c b/tests/subsys/llext/src/test_llext_simple.c index 28a51a092b25d8..b78f2809009528 100644 --- a/tests/subsys/llext/src/test_llext_simple.c +++ b/tests/subsys/llext/src/test_llext_simple.c @@ -10,7 +10,10 @@ #include #if defined(CONFIG_ARM) /* ARMV7 */ || defined(CONFIG_XTENSA) -const static uint8_t hello_world_elf[] __aligned(4) = { +#ifndef CONFIG_LLEXT_STORAGE_WRITABLE +const +#endif +static uint8_t hello_world_elf[] __aligned(4) = { #include "hello_world.inc" }; #endif diff --git a/tests/subsys/llext/testcase.yaml b/tests/subsys/llext/testcase.yaml index 0bbccd5e1ca9c7..2747edf8d0c848 100644 --- a/tests/subsys/llext/testcase.yaml +++ b/tests/subsys/llext/testcase.yaml @@ -11,6 +11,8 @@ tests: - nuvoton_pfm_m487 # See #63167 llext.simple.xtensa: arch_allow: xtensa + extra_configs: + - CONFIG_LLEXT_STORAGE_WRITABLE=y # Broken platforms platform_exclude: - qemu_xtensa_mmu # ELF sections are read-only, and without peek() .text copy isn't executable