Skip to content

Commit

Permalink
LLEXT: add llext_section_offset() to replace llext_find_section()
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
lyakh authored and nashif committed Nov 16, 2024
1 parent c714f0e commit 44d96a2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/zephyr/llext/llext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
26 changes: 26 additions & 0 deletions subsys/llext/llext.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions tests/subsys/llext/simple/src/test_llext_simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -411,13 +412,19 @@ 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");

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;

Expand Down

0 comments on commit 44d96a2

Please sign in to comment.