This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 4156eca0c129e80da94f2a74a8141acd4302d728
Author: anjiahao <anjia...@xiaomi.com>
AuthorDate: Sun Oct 13 17:25:10 2024 +0800

    modlib:so need export symbol, exec elf not need
    
    Signed-off-by: anjiahao <anjia...@xiaomi.com>
---
 libs/libc/modlib/modlib_bind.c   | 33 ---------------------
 libs/libc/modlib/modlib_insert.c | 62 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+), 33 deletions(-)

diff --git a/libs/libc/modlib/modlib_bind.c b/libs/libc/modlib/modlib_bind.c
index 5b8cce26c2..c136ea3f53 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 92c58ce5d4..c9faac0cda 100644
--- a/libs/libc/modlib/modlib_insert.c
+++ b/libs/libc/modlib/modlib_insert.c
@@ -31,6 +31,8 @@
 #include <nuttx/lib/lib.h>
 #include <nuttx/lib/modlib.h>
 
+#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;

Reply via email to