LLEXT: add llext_section_offset() to replace llext_find_section()

Now that section header cache is persistent, it can be used for
finding sections. Add a new function, similar to llext_find_section()
to use it and deprecate llext_find_section().

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
This commit is contained in:
Guennadi Liakhovetski 2024-11-05 10:38:24 +01:00 committed by Anas Nashif
parent c714f0e148
commit 44d96a2668
3 changed files with 49 additions and 0 deletions

View File

@ -354,6 +354,22 @@ int arch_elf_relocate(elf_rela_t *rel, uintptr_t loc,
*/
ssize_t llext_find_section(struct llext_loader *loader, const char *search_name);
/**
* @brief Extract ELF section header by name.
*
* Searches for a section by name in the ELF file and retrieves its full header.
*
* @param[in] loader Extension loader data and context
* @param[in] ext Extension to be searched
* @param[in] search_name Section name to search for
* @param[out] shdr Buffer for the section header
* @retval 0 Success
* @retval -ENOTSUP "peek" method not supported
* @retval -ENOENT section not found
*/
int llext_get_section_header(struct llext_loader *loader, struct llext *ext,
const char *search_name, elf_shdr_t *shdr);
/**
* @brief Architecture specific function for local binding relocations
*

View File

@ -23,6 +23,32 @@ static sys_slist_t _llext_list = SYS_SLIST_STATIC_INIT(&_llext_list);
static struct k_mutex llext_lock = Z_MUTEX_INITIALIZER(llext_lock);
int llext_get_section_header(struct llext_loader *ldr, struct llext *ext, const char *search_name,
elf_shdr_t *shdr)
{
const elf_shdr_t *tmp;
unsigned int i;
for (i = 0, tmp = ext->sect_hdrs;
i < ext->sect_cnt;
i++, tmp++) {
const char *name = llext_peek(ldr,
ldr->sects[LLEXT_MEM_SHSTRTAB].sh_offset +
tmp->sh_name);
if (!name) {
return -ENOTSUP;
}
if (!strcmp(name, search_name)) {
*shdr = *tmp;
return 0;
}
}
return -ENOENT;
}
ssize_t llext_find_section(struct llext_loader *ldr, const char *search_name)
{
elf_shdr_t *shdr;

View File

@ -11,6 +11,7 @@
#if defined(CONFIG_FILE_SYSTEM_LITTLEFS)
#include <zephyr/fs/littlefs.h>
#endif
#include <zephyr/llext/elf.h>
#include <zephyr/llext/llext.h>
#include <zephyr/llext/symbol.h>
#include <zephyr/llext/buf_loader.h>
@ -411,6 +412,7 @@ ZTEST(llext, test_find_section)
struct llext_loader *loader = &buf_loader.loader;
struct llext_load_param ldr_parm = LLEXT_LOAD_PARAM_DEFAULT;
struct llext *ext = NULL;
elf_shdr_t shdr;
res = llext_load(loader, "find_section", &ext, &ldr_parm);
zassert_ok(res, "load should succeed");
@ -418,6 +420,11 @@ ZTEST(llext, test_find_section)
section_ofs = llext_find_section(loader, ".data");
zassert_true(section_ofs > 0, "find_section returned %zd", section_ofs);
res = llext_get_section_header(loader, ext, ".data", &shdr);
zassert_ok(res, "get_section_header() should succeed");
zassert_equal(shdr.sh_offset, section_ofs,
"different section offset %zd from get_section_header", shdr.sh_offset);
uintptr_t symbol_ptr = (uintptr_t)llext_find_sym(&ext->exp_tab, "number");
uintptr_t section_ptr = (uintptr_t)find_section_ext + section_ofs;