nealef commented on code in PR #7202:
URL: https://github.com/apache/incubator-nuttx/pull/7202#discussion_r981865110


##########
libs/libc/modlib/modlib_symbols.c:
##########
@@ -417,3 +426,157 @@ int modlib_symvalue(FAR struct module_s *modp,
 
   return OK;
 }
+
+/****************************************************************************
+ * Name: modlib_insertsymtab
+ *
+ * Description:
+ *   Insert a symbol into the modules exportinfo array.
+ *
+ * Input Parameters:
+ *   modp     - Module state information
+ *   loadinfo - Load state information
+ *   shdr     - Symbol table section header
+ *   sym      - Symbol table entry
+ *
+ * Returned Value:
+ *   0 (OK) is returned on success and a negated errno is returned on
+ *   failure.
+ *
+ *   EINVAL - There is something inconsistent in the symbol table (should only
+ *            happen if the file is corrupted).
+ *
+ ****************************************************************************/
+
+int modlib_insertsymtab(FAR struct module_s *modp, 
+                       struct mod_loadinfo_s *loadinfo, 
+                       FAR Elf32_Shdr *shdr, FAR Elf32_Sym *sym)
+{
+  FAR struct symtab_s *symbol;
+  FAR Elf32_Shdr *strTab = &loadinfo->shdr[shdr->sh_link];
+  int ret = 0, i, j;
+  int nSym, symCount;
+
+  if (modp->modinfo.exports != NULL)
+    {
+      bwarn("Module export information already present - replacing");
+      modlib_freesymtab((FAR void *) modp);
+    }
+  
+
+  /* Count the "live" symbols */
+  nSym = shdr->sh_size / sizeof(Elf32_Sym);

Review Comment:
   Fixed



##########
libs/libc/modlib/modlib_symbols.c:
##########
@@ -417,3 +426,157 @@ int modlib_symvalue(FAR struct module_s *modp,
 
   return OK;
 }
+
+/****************************************************************************
+ * Name: modlib_insertsymtab
+ *
+ * Description:
+ *   Insert a symbol into the modules exportinfo array.
+ *
+ * Input Parameters:
+ *   modp     - Module state information
+ *   loadinfo - Load state information
+ *   shdr     - Symbol table section header
+ *   sym      - Symbol table entry
+ *
+ * Returned Value:
+ *   0 (OK) is returned on success and a negated errno is returned on
+ *   failure.
+ *
+ *   EINVAL - There is something inconsistent in the symbol table (should only
+ *            happen if the file is corrupted).
+ *
+ ****************************************************************************/
+
+int modlib_insertsymtab(FAR struct module_s *modp, 
+                       struct mod_loadinfo_s *loadinfo, 
+                       FAR Elf32_Shdr *shdr, FAR Elf32_Sym *sym)
+{
+  FAR struct symtab_s *symbol;
+  FAR Elf32_Shdr *strTab = &loadinfo->shdr[shdr->sh_link];
+  int ret = 0, i, j;
+  int nSym, symCount;
+
+  if (modp->modinfo.exports != NULL)
+    {
+      bwarn("Module export information already present - replacing");
+      modlib_freesymtab((FAR void *) modp);
+    }
+  
+
+  /* Count the "live" symbols */
+  nSym = shdr->sh_size / sizeof(Elf32_Sym);
+  for (i = 0, symCount = 0; i < nSym; i++)
+    {
+      if (sym[i].st_name != 0)
+          symCount++;
+    }
+
+  if (symCount > 0)
+    {
+      modp->modinfo.exports = symbol = loadinfo->exported = 
lib_malloc(sizeof(*symbol) * symCount);
+      if (modp->modinfo.exports)
+        { 
+          /* Build out module's symbol table */
+          modp->modinfo.nexports = symCount;
+          for (i = 0, j = 0; i < nSym; i++)
+            {
+             if (sym[i].st_name != 0)
+                {
+                  ret = modlib_symname(loadinfo, &sym[i], strTab->sh_offset);
+                  if (ret < 0) 
+                    {
+                      lib_free((FAR void *) modp->modinfo.exports);
+                      modp->modinfo.exports = NULL;
+                      return ret;
+                    }
+
+                  symbol[j].sym_name = strdup((char *) loadinfo->iobuffer);
+                  symbol[j].sym_value = (FAR const void *) sym[i].st_value;
+                  j++;
+                }
+            }
+        }
+      else
+        {
+          berr("Unable to get memory for exported symbols table");
+          ret = -ENOMEM;  
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: findEP
+ *
+ * Description:
+ *   Binary search comparison function
+ *
+ * Input Parameters:
+ *   c1 - Comparand 1
+ *   c2 - Comparand 2
+ *
+ ****************************************************************************/
+static int findEP(const void *c1, const void *c2)
+{
+  const epTable_t *m1 = (epTable_t *) c1;
+  const epTable_t *m2 = (epTable_t *) c2;
+  return strcmp((FAR const char *)m1->epName, (FAR const char *)m2->epName);
+}
+
+/****************************************************************************
+ * Name: modlib_findglobal
+ *
+ * Description:
+ *   Find a symbol in our library entry point table
+ *
+ * Input Parameters:
+ *   modp     - Module state information
+ *
+ ****************************************************************************/
+
+void *modlib_findglobal(FAR struct module_s *modp,
+                        struct mod_loadinfo_s *loadinfo, 
+                        FAR Elf32_Shdr *shdr, FAR Elf32_Sym *sym)
+{
+  FAR Elf32_Shdr *strTab = &loadinfo->shdr[shdr->sh_link];
+  int ret;
+  epTable_t key, *res;
+  extern epTable_t globalTable[];
+  extern int nGlobals;
+
+  ret = modlib_symname(loadinfo, sym, strTab->sh_offset);
+  if (ret < 0)
+      return NULL;
+
+  key.epName = loadinfo->iobuffer;
+  res = bsearch(&key, globalTable, nGlobals, sizeof(epTable_t), findEP);
+  if (res != NULL)
+     return res->epAddr;
+  else
+     return NULL;

Review Comment:
   Fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to