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

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

commit c8b71df6149703081245a6ab321ca55a66aa1833
Author: anjiahao <[email protected]>
AuthorDate: Fri Mar 7 17:15:30 2025 +0800

    libelf:support find symbol by symbol name
    
    Add libelf_findsymbol() to locate a symbol in the ELF symbol table by
    name, reusing the existing libelf_findsymtab/libelf_readsym/libelf_symname
    helpers, and expose its prototype in include/nuttx/lib/elf.h.
    
    Signed-off-by: anjiahao <[email protected]>
---
 include/nuttx/lib/elf.h     | 20 ++++++++++++
 libs/libc/elf/elf_symbols.c | 75 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 95 insertions(+)

diff --git a/include/nuttx/lib/elf.h b/include/nuttx/lib/elf.h
index 601609fe259..784210d38a9 100644
--- a/include/nuttx/lib/elf.h
+++ b/include/nuttx/lib/elf.h
@@ -780,4 +780,24 @@ FAR void *libelf_gethandle(FAR const char *name);
 #  define libelf_gethandle(n) NULL
 #endif
 
+/****************************************************************************
+ * Name: libelf_findsymbol
+ *
+ * Description:
+ *   Locate a symbol in the ELF symbol table by its name.
+ *
+ * Input Parameters:
+ *   loadinfo - Load state information containing the symbol table
+ *   name     - The name of the symbol to search for
+ *   sym      - Pointer to store the located symbol's table entry
+ *
+ * Returned Value:
+ *   0 (OK) is returned on success and a negated errno is returned on
+ *   failure.
+ *
+ ****************************************************************************/
+
+int libelf_findsymbol(FAR struct mod_loadinfo_s *loadinfo,
+                      FAR const char *name, FAR Elf_Sym *sym);
+
 #endif /* __INCLUDE_NUTTX_LIB_LIBC_ELF_H */
diff --git a/libs/libc/elf/elf_symbols.c b/libs/libc/elf/elf_symbols.c
index 9d9462114ab..39ad66f0858 100644
--- a/libs/libc/elf/elf_symbols.c
+++ b/libs/libc/elf/elf_symbols.c
@@ -640,3 +640,78 @@ void libelf_freesymtab(FAR struct module_s *modp)
       lib_free((FAR void *)symbol);
     }
 }
+
+/****************************************************************************
+ * Name: libelf_findsymbol
+ *
+ * Description:
+ *   Locate a symbol in the ELF symbol table by its name.
+ *
+ * Input Parameters:
+ *   loadinfo - Load state information containing the symbol table
+ *   name     - The name of the symbol to search for
+ *   sym      - Pointer to store the located symbol's table entry
+ *
+ * Returned Value:
+ *   0 (OK) is returned on success and a negated errno is returned on
+ *   failure.
+ *
+ ****************************************************************************/
+
+int libelf_findsymbol(FAR struct mod_loadinfo_s *loadinfo,
+                      FAR const char *name, FAR Elf_Sym *sym)
+{
+  size_t num;
+  int ret;
+  int i;
+
+  if (loadinfo == NULL || name == NULL)
+    {
+      return -EINVAL;
+    }
+
+  if (loadinfo->symtabidx == 0)
+    {
+      ret = libelf_findsymtab(loadinfo);
+      if (ret < 0)
+        {
+          berr("ERROR: Failed to find symbol table\n");
+          return ret;
+        }
+    }
+
+  /* Loop through all symbols in the symbol table */
+
+  num = loadinfo->shdr[loadinfo->symtabidx].sh_size / sizeof(Elf_Sym);
+  for (i = 0; i < num; i++)
+    {
+      /* Read the symbol entry */
+
+      ret = libelf_readsym(loadinfo, i, sym,
+                           &loadinfo->shdr[loadinfo->symtabidx]);
+      if (ret)
+        {
+          berr("ERROR: Failed to read symbol entry\n");
+          return ret;
+        }
+
+      /* Get the symbol name */
+
+      ret = libelf_symname(loadinfo, sym,
+                           loadinfo->shdr[loadinfo->strtabidx].sh_offset);
+      if (ret < 0 && ret != -ESRCH)
+        {
+          berr("ERROR: Failed to get symbol name\n");
+          return ret;
+        }
+
+      /* Compare the symbol name with the given name */
+
+      if (strcmp((FAR const char *)loadinfo->iobuffer, name) == 0)
+        {
+          return ret;
+        }
+    }
+
+  return -ENOENT;
+}

Reply via email to