diff --git a/libs/libc/modlib/modlib_bind.c b/libs/libc/modlib/modlib_bind.c index 5b8cce26c2d0a..c136ea3f53a32 100644 --- a/libs/libc/modlib/modlib_bind.c +++ b/libs/libc/modlib/modlib_bind.c @@ -856,8 +856,6 @@ int modlib_bind(FAR struct module_s *modp, FAR struct mod_loadinfo_s *loadinfo, FAR const struct symtab_s *exports, int nexports) { - FAR Elf_Shdr *symhdr; - FAR Elf_Sym *sym; int ret; int i; @@ -991,37 +989,6 @@ int modlib_bind(FAR struct module_s *modp, } } - symhdr = &loadinfo->shdr[loadinfo->symtabidx]; - sym = lib_malloc(symhdr->sh_size); - - ret = modlib_read(loadinfo, (FAR uint8_t *)sym, symhdr->sh_size, - symhdr->sh_offset); - - if (ret < 0) - { - berr("Failed to read symbol table\n"); - lib_free(sym); - return ret; - } - - for (i = 0; i < symhdr->sh_size / sizeof(Elf_Sym); i++) - { - FAR Elf_Shdr *s = &loadinfo->shdr[sym[i].st_shndx]; - - if (sym[i].st_shndx != SHN_UNDEF) - { - sym[i].st_value = sym[i].st_value + s->sh_addr; - } - } - - ret = modlib_insertsymtab(modp, loadinfo, symhdr, sym); - lib_free(sym); - if (ret != 0) - { - binfo("Failed to export symbols program binary: %d\n", ret); - return ret; - } - /* Ensure that the I and D caches are coherent before starting the newly * loaded module by cleaning the D cache (i.e., flushing the D cache * contents to memory and invalidating the I cache). diff --git a/libs/libc/modlib/modlib_insert.c b/libs/libc/modlib/modlib_insert.c index 92c58ce5d43af..c9faac0cdad06 100644 --- a/libs/libc/modlib/modlib_insert.c +++ b/libs/libc/modlib/modlib_insert.c @@ -31,6 +31,8 @@ #include #include +#include "modlib.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -203,6 +205,59 @@ void modlib_dumpentrypt(FAR struct mod_loadinfo_s *loadinfo) } #endif +/**************************************************************************** + * Name: modlib_loadsymtab + * + * Description: + * Load the symbol table into memory. + * + ****************************************************************************/ + +static int modlib_loadsymtab(FAR struct module_s *modp, + FAR struct mod_loadinfo_s *loadinfo) +{ + FAR Elf_Shdr *symhdr = &loadinfo->shdr[loadinfo->symtabidx]; + FAR Elf_Sym *sym = lib_malloc(symhdr->sh_size); + int ret; + int i; + + if (sym == NULL) + { + return -ENOMEM; + } + + ret = modlib_read(loadinfo, (FAR uint8_t *)sym, symhdr->sh_size, + symhdr->sh_offset); + + if (ret < 0) + { + berr("Failed to read symbol table\n"); + lib_free(sym); + return ret; + } + + for (i = 0; i < symhdr->sh_size / sizeof(Elf_Sym); i++) + { + if (sym[i].st_shndx != SHN_UNDEF && + sym[i].st_shndx < loadinfo->ehdr.e_shnum) + { + FAR Elf_Shdr *s = &loadinfo->shdr[sym[i].st_shndx]; + + sym[i].st_value = sym[i].st_value + s->sh_addr; + } + } + + ret = modlib_insertsymtab(modp, loadinfo, symhdr, sym); + lib_free(sym); + if (ret != 0) + { + binfo("Failed to export symbols program binary: %d\n", ret); + return ret; + } + + return ret; +} + /**************************************************************************** * Name: modlib_insert * @@ -306,6 +361,13 @@ FAR void *modlib_insert(FAR const char *filename, FAR const char *modname) goto errout_with_load; } + ret = modlib_loadsymtab(modp, &loadinfo); + if (ret != 0) + { + binfo("Failed to load symbol table: %d\n", ret); + goto errout_with_load; + } + /* Save the load information */ modp->textalloc = (FAR void *)loadinfo.textalloc;