diff --git a/include/zephyr/llext/llext.h b/include/zephyr/llext/llext.h index e0ceece0cf8..a4458a05468 100644 --- a/include/zephyr/llext/llext.h +++ b/include/zephyr/llext/llext.h @@ -33,6 +33,7 @@ enum llext_mem { LLEXT_MEM_DATA, LLEXT_MEM_RODATA, LLEXT_MEM_BSS, + LLEXT_MEM_SYMTAB, LLEXT_MEM_STRTAB, LLEXT_MEM_SHSTRTAB, diff --git a/include/zephyr/llext/loader.h b/include/zephyr/llext/loader.h index 5bcfb1e908b..3cc53da7d88 100644 --- a/include/zephyr/llext/loader.h +++ b/include/zephyr/llext/loader.h @@ -42,6 +42,8 @@ enum llext_section { LLEXT_SECT_COUNT, }; +enum llext_mem; + /** * @brief Linkable loadable extension loader context */ @@ -90,7 +92,7 @@ struct llext_loader { /** @cond ignore */ elf_ehdr_t hdr; elf_shdr_t sects[LLEXT_SECT_COUNT]; - uint32_t *sect_map; + enum llext_mem *sect_map; uint32_t sect_cnt; /** @endcond */ }; diff --git a/subsys/llext/llext.c b/subsys/llext/llext.c index 6c85a633072..84c38765d95 100644 --- a/subsys/llext/llext.c +++ b/subsys/llext/llext.c @@ -128,18 +128,18 @@ static int llext_find_tables(struct llext_loader *ldr) case SHT_DYNSYM: LOG_DBG("symtab at %d", i); ldr->sects[LLEXT_SECT_SYMTAB] = shdr; - ldr->sect_map[i] = LLEXT_SECT_SYMTAB; + ldr->sect_map[i] = LLEXT_MEM_SYMTAB; sect_cnt++; break; case SHT_STRTAB: if (ldr->hdr.e_shstrndx == i) { LOG_DBG("shstrtab at %d", i); ldr->sects[LLEXT_SECT_SHSTRTAB] = shdr; - ldr->sect_map[i] = LLEXT_SECT_SHSTRTAB; + ldr->sect_map[i] = LLEXT_MEM_SHSTRTAB; } else { LOG_DBG("strtab at %d", i); ldr->sects[LLEXT_SECT_STRTAB] = shdr; - ldr->sect_map[i] = LLEXT_SECT_STRTAB; + ldr->sect_map[i] = LLEXT_MEM_STRTAB; } sect_cnt++; break; @@ -192,22 +192,27 @@ static int llext_map_sections(struct llext_loader *ldr, struct llext *ext) LOG_DBG("section %d name %s", i, name); enum llext_section sect_idx; + enum llext_mem mem_idx; if (strcmp(name, ".text") == 0) { sect_idx = LLEXT_SECT_TEXT; + mem_idx = LLEXT_MEM_TEXT; } else if (strcmp(name, ".data") == 0) { sect_idx = LLEXT_SECT_DATA; + mem_idx = LLEXT_MEM_DATA; } else if (strcmp(name, ".rodata") == 0) { sect_idx = LLEXT_SECT_RODATA; + mem_idx = LLEXT_MEM_RODATA; } else if (strcmp(name, ".bss") == 0) { sect_idx = LLEXT_SECT_BSS; + mem_idx = LLEXT_MEM_BSS; } else { LOG_DBG("Not copied section %s", name); continue; } ldr->sects[sect_idx] = shdr; - ldr->sect_map[i] = sect_idx; + ldr->sect_map[i] = mem_idx; } return 0; @@ -230,6 +235,9 @@ static enum llext_section llext_sect_from_mem(enum llext_mem m) case LLEXT_MEM_TEXT: s = LLEXT_SECT_TEXT; break; + case LLEXT_MEM_SYMTAB: + s = LLEXT_SECT_SYMTAB; + break; case LLEXT_MEM_STRTAB: s = LLEXT_SECT_STRTAB; break; @@ -415,17 +423,18 @@ static int llext_copy_symbols(struct llext_loader *ldr, struct llext *ext) uint32_t stt = ELF_ST_TYPE(sym.st_info); uint32_t stb = ELF_ST_BIND(sym.st_info); - uint32_t sect = sym.st_shndx; + unsigned int sect = sym.st_shndx; if (stt == STT_FUNC && stb == STB_GLOBAL && sect != SHN_UNDEF) { + enum llext_mem mem = ldr->sect_map[sect]; + enum llext_section sect_idx = llext_sect_from_mem(mem); const char *name = llext_string(ldr, ext, LLEXT_MEM_STRTAB, sym.st_name); __ASSERT(j <= sym_tab->sym_cnt, "Miscalculated symbol number %u\n", j); sym_tab->syms[j].name = name; - sym_tab->syms[j].addr = - (void *)((uintptr_t)ext->mem[ldr->sect_map[sym.st_shndx]] - + sym.st_value); + sym_tab->syms[j].addr = (void *)((uintptr_t)ext->mem[mem] + + sym.st_value); LOG_DBG("function symbol %d name %s addr %p", j, name, sym_tab->syms[j].addr); j++;