From ed4f651bbdbc3dc38a11e9c3c4e6821400a68ced Mon Sep 17 00:00:00 2001 From: Neale Ferguson Date: Sun, 23 Jul 2023 19:45:16 -0300 Subject: [PATCH] Fix modlib to get binary loading working again This patch fixes the issue reported here: https://github.com/apache/nuttx/issues/9845 --- libs/libc/modlib/modlib_bind.c | 6 +++--- libs/libc/modlib/modlib_load.c | 13 ++++++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/libs/libc/modlib/modlib_bind.c b/libs/libc/modlib/modlib_bind.c index 0c195daa15..f730ccf10d 100644 --- a/libs/libc/modlib/modlib_bind.c +++ b/libs/libc/modlib/modlib_bind.c @@ -263,8 +263,8 @@ static int modlib_relocate(FAR struct module_s *modp, /* Read the symbol table entry into memory */ - ret = modlib_symvalue(modp, loadinfo, sym, - loadinfo->shdr[loadinfo->strtabidx].sh_offset); + ret = modlib_readsym(loadinfo, symidx, sym, + &loadinfo->shdr[loadinfo->symtabidx]); if (ret < 0) { berr("ERROR: Section %d reloc %d: " @@ -862,7 +862,7 @@ int modlib_bind(FAR struct module_s *modp, * sections that were not loaded into memory. */ - if ((loadinfo->shdr[i].sh_flags & SHF_ALLOC) == 0) + if ((loadinfo->shdr[infosec].sh_flags & SHF_ALLOC) == 0) { continue; } diff --git a/libs/libc/modlib/modlib_load.c b/libs/libc/modlib/modlib_load.c index d8c3316218..2c4c77b298 100644 --- a/libs/libc/modlib/modlib_load.c +++ b/libs/libc/modlib/modlib_load.c @@ -324,12 +324,10 @@ int modlib_load(FAR struct mod_loadinfo_s *loadinfo) loadinfo->textalloc = (uintptr_t) up_textheap_memalign(loadinfo->textalign, loadinfo->textsize + - loadinfo->datasize + loadinfo->segpad); #else loadinfo->textalloc = (uintptr_t)lib_memalign(loadinfo->textalign, loadinfo->textsize + - loadinfo->datasize + loadinfo->segpad); #endif if (!loadinfo->textalloc) @@ -342,9 +340,14 @@ int modlib_load(FAR struct mod_loadinfo_s *loadinfo) if (loadinfo->datasize > 0) { - loadinfo->datastart = loadinfo->textalloc + - loadinfo->textsize + - loadinfo->segpad; + loadinfo->datastart = (uintptr_t)lib_memalign(loadinfo->dataalign, + loadinfo->datasize); + if (!loadinfo->datastart) + { + berr("ERROR: Failed to allocate memory for the module data\n"); + ret = -ENOMEM; + goto errout_with_buffers; + } } /* Load ELF section data into memory */