tests: subsys: llext: Add test for LLEXT_RODATA_NO_RELOC.

Test extension for CONFIG_LLEXT_RODATA_NO_RELOC feature.

Signed-off-by: Ibrahim Abdalkader <i.abdalkader@gmail.com>
This commit is contained in:
Ibrahim Abdalkader
2025-12-09 20:29:53 +01:00
committed by Fabio Baltieri
parent dca4136f5e
commit d320921cd1
4 changed files with 68 additions and 0 deletions

View File

@@ -47,6 +47,10 @@ if(NOT CONFIG_LLEXT_TYPE_ELF_SHAREDLIB)
list(APPEND ext_names init_fini)
endif()
if(CONFIG_LLEXT_RODATA_NO_RELOC)
list(APPEND ext_names rodata_no_reloc)
endif()
# generate extension targets for each extension in 'ext_names'
foreach(ext_name ${ext_names})
set(ext_src ${PROJECT_SOURCE_DIR}/src/${ext_name}_ext.c)

View File

@@ -0,0 +1,51 @@
/*
* Copyright (c) 2025 Arduino SA
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* Test extension for CONFIG_LLEXT_RODATA_NO_RELOC feature.
*/
#include <stdint.h>
#include <zephyr/llext/llext.h>
#include <zephyr/llext/symbol.h>
#include <zephyr/sys/printk.h>
#include <zephyr/ztest_assert.h>
/* Exported by the main test to provide ELF buffer location */
extern const void *rodata_no_reloc_ext_ptr;
extern const size_t rodata_no_reloc_ext_size;
/* rodata with a relocation - forces .rodata to be relocated */
Z_GENERIC_SECTION(.rodata)
static const void *relocated_data = &printk;
/* rodata with no relocation - should stay in place */
LLEXT_RODATA_NO_RELOC
static const uint32_t noreloc_data = 0x12345678;
void test_entry(void)
{
uintptr_t elf_start = (uintptr_t)rodata_no_reloc_ext_ptr;
uintptr_t elf_end = elf_start + rodata_no_reloc_ext_size;
uintptr_t noreloc_addr = (uintptr_t)&noreloc_data;
uintptr_t relocated_addr = (uintptr_t)&relocated_data;
printk("noreloc at %p, relocated at %p, ELF: %p - %p\n",
(void *)noreloc_addr, (void *)relocated_addr,
(void *)elf_start, (void *)elf_end);
/* Verify data is correct */
zassert_equal(noreloc_data, 0x12345678, "noreloc_data value mismatch");
/* noreloc_data should stay in original ELF buffer */
zassert_true(noreloc_addr >= elf_start && noreloc_addr < elf_end,
"noreloc_data should remain in ELF buffer");
/* relocated_data should be outside ELF buffer */
zassert_true(relocated_addr < elf_start || relocated_addr >= elf_end,
"relocated_data should be outside ELF buffer");
}
EXPORT_SYMBOL(test_entry);

View File

@@ -332,6 +332,17 @@ static LLEXT_CONST uint8_t inspect_ext[] LLEXT_SECT ELF_ALIGN = {
#include "inspect.inc"
};
#if defined(CONFIG_LLEXT_RODATA_NO_RELOC)
static LLEXT_CONST uint8_t rodata_no_reloc_ext[] ELF_ALIGN = {
#include "rodata_no_reloc.inc"
};
const void *rodata_no_reloc_ext_ptr = rodata_no_reloc_ext;
EXPORT_SYMBOL(rodata_no_reloc_ext_ptr);
const size_t rodata_no_reloc_ext_size = sizeof(rodata_no_reloc_ext);
EXPORT_SYMBOL(rodata_no_reloc_ext_size);
LLEXT_LOAD_UNLOAD(rodata_no_reloc)
#endif
void do_inspect_checks(struct llext_loader *ldr, struct llext *ext, enum llext_mem reg_idx,
const char *sect_name, const char *sym_name)
{

View File

@@ -44,6 +44,7 @@ tests:
extra_conf_files: ['no_mem_protection.conf']
extra_configs:
- CONFIG_LLEXT_STORAGE_WRITABLE=n
- CONFIG_LLEXT_RODATA_NO_RELOC=y
llext.readonly_mpu:
arch_allow:
- arm # Xtensa needs writable storage, currently not supported on RISC-V
@@ -64,6 +65,7 @@ tests:
extra_configs:
- CONFIG_LLEXT_HEAP_SIZE=128 # qemu_cortex_a9 requires larger heap
- CONFIG_LLEXT_STORAGE_WRITABLE=n
- CONFIG_LLEXT_RODATA_NO_RELOC=y
llext.writable:
arch_allow:
- arm