Skip to content

Commit 23c9838

Browse files
committed
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 <[email protected]>
1 parent b11d6d8 commit 23c9838

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

include/zephyr/llext/llext.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,22 @@ int arch_elf_relocate(elf_rela_t *rel, uintptr_t loc,
351351
*/
352352
ssize_t llext_find_section(struct llext_loader *loader, const char *search_name);
353353

354+
/**
355+
* @brief Extract ELF section header by name.
356+
*
357+
* Searches for a section by name in the ELF file and retrieves its full header.
358+
*
359+
* @param[in] loader Extension loader data and context
360+
* @param[in] ext Extension to be searched
361+
* @param[in] search_name Section name to search for
362+
* @param[out] shdr Buffer for the section header
363+
* @retval 0 Success
364+
* @retval -ENOTSUP "peek" method not supported
365+
* @retval -ENOENT section not found
366+
*/
367+
int llext_get_section_header(struct llext_loader *loader, struct llext *ext,
368+
const char *search_name, elf_shdr_t *shdr);
369+
354370
/**
355371
* @brief Architecture specific function for local binding relocations
356372
*

subsys/llext/llext.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,32 @@ static sys_slist_t _llext_list = SYS_SLIST_STATIC_INIT(&_llext_list);
2323

2424
static struct k_mutex llext_lock = Z_MUTEX_INITIALIZER(llext_lock);
2525

26+
int llext_get_section_header(struct llext_loader *ldr, struct llext *ext, const char *search_name,
27+
elf_shdr_t *shdr)
28+
{
29+
const elf_shdr_t *tmp;
30+
unsigned int i;
31+
32+
for (i = 0, tmp = ext->sect_hdrs;
33+
i < ext->sect_cnt;
34+
i++, tmp++) {
35+
const char *name = llext_peek(ldr,
36+
ldr->sects[LLEXT_MEM_SHSTRTAB].sh_offset +
37+
tmp->sh_name);
38+
39+
if (!name) {
40+
return -ENOTSUP;
41+
}
42+
43+
if (!strcmp(name, search_name)) {
44+
*shdr = *tmp;
45+
return 0;
46+
}
47+
}
48+
49+
return -ENOENT;
50+
}
51+
2652
ssize_t llext_find_section(struct llext_loader *ldr, const char *search_name)
2753
{
2854
elf_shdr_t *shdr;

tests/subsys/llext/simple/src/test_llext_simple.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#if defined(CONFIG_FILE_SYSTEM_LITTLEFS)
1212
#include <zephyr/fs/littlefs.h>
1313
#endif
14+
#include <zephyr/llext/elf.h>
1415
#include <zephyr/llext/llext.h>
1516
#include <zephyr/llext/symbol.h>
1617
#include <zephyr/llext/buf_loader.h>
@@ -411,13 +412,19 @@ ZTEST(llext, test_find_section)
411412
struct llext_loader *loader = &buf_loader.loader;
412413
struct llext_load_param ldr_parm = LLEXT_LOAD_PARAM_DEFAULT;
413414
struct llext *ext = NULL;
415+
elf_shdr_t shdr;
414416

415417
res = llext_load(loader, "find_section", &ext, &ldr_parm);
416418
zassert_ok(res, "load should succeed");
417419

418420
section_ofs = llext_find_section(loader, ".data");
419421
zassert_true(section_ofs > 0, "find_section returned %zd", section_ofs);
420422

423+
res = llext_get_section_header(loader, ext, ".data", &shdr);
424+
zassert_ok(res, "get_section_header() should succeed");
425+
zassert_equal(shdr.sh_offset, section_ofs,
426+
"different section offset %zd from get_section_header", shdr.sh_offset);
427+
421428
uintptr_t symbol_ptr = (uintptr_t)llext_find_sym(&ext->exp_tab, "number");
422429
uintptr_t section_ptr = (uintptr_t)find_section_ext + section_ofs;
423430

0 commit comments

Comments
 (0)