Skip to content

Commit c6bf743

Browse files
lyakhhenrikbrixandersen
authored andcommitted
LLEXT: add support for detached sections
Some LLEXT objects can include sections, that should not be merged with other sections of the same type. E.g. when such sections should be placed into locations, other than the default. Add support for such sections. Signed-off-by: Guennadi Liakhovetski <[email protected]>
1 parent 0aa6b1c commit c6bf743

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

include/zephyr/llext/llext.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ struct llext_load_param {
135135
* the memory buffer, when calculating relocation targets.
136136
*/
137137
bool pre_located;
138+
/**
139+
* Extensions can implement custom ELF sections to be loaded in specific
140+
* memory regions, detached from other sections of compatible types.
141+
* This optional callback checks whether a section should be detached.
142+
*/
143+
bool (*section_detached)(const elf_shdr_t *shdr);
138144
};
139145

140146
/** Default initializer for @ref llext_load_param */

subsys/llext/llext_load.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ static int llext_find_tables(struct llext_loader *ldr)
210210
* Maps the ELF sections into regions according to their usage flags,
211211
* calculating ldr->sects and ldr->sect_map.
212212
*/
213-
static int llext_map_sections(struct llext_loader *ldr, struct llext *ext)
213+
static int llext_map_sections(struct llext_loader *ldr, struct llext *ext,
214+
const struct llext_load_param *ldr_parm)
214215
{
215216
int i, j;
216217
const char *name;
@@ -286,6 +287,15 @@ static int llext_map_sections(struct llext_loader *ldr, struct llext *ext)
286287
ldr->sect_map[i].mem_idx = mem_idx;
287288
elf_shdr_t *region = ldr->sects + mem_idx;
288289

290+
/*
291+
* ELF objects can have sections for memory regions, detached from
292+
* other sections of the same type. E.g. executable sections that will be
293+
* placed in slower memory. Don't merge such sections into main regions
294+
*/
295+
if (ldr_parm->section_detached && ldr_parm->section_detached(shdr)) {
296+
continue;
297+
}
298+
289299
if (region->sh_type == SHT_NULL) {
290300
/* First section of this type, copy all info to the
291301
* region descriptor.
@@ -586,7 +596,8 @@ static int llext_copy_symbols(struct llext_loader *ldr, struct llext *ext,
586596
}
587597
}
588598

589-
if (ldr_parm->pre_located) {
599+
if (ldr_parm->pre_located &&
600+
(!ldr_parm->section_detached || !ldr_parm->section_detached(shdr))) {
590601
sym_tab->syms[j].addr = (uint8_t *)sym.st_value +
591602
(ldr->hdr.e_type == ET_REL ? section_addr : 0);
592603
} else {
@@ -658,7 +669,7 @@ int do_llext_load(struct llext_loader *ldr, struct llext *ext,
658669
}
659670

660671
LOG_DBG("Mapping ELF sections...");
661-
ret = llext_map_sections(ldr, ext);
672+
ret = llext_map_sections(ldr, ext, ldr_parm);
662673
if (ret != 0) {
663674
LOG_ERR("Failed to map ELF sections, ret %d", ret);
664675
goto out;

0 commit comments

Comments
 (0)