You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/09/27 12:43:12 UTC

[GitHub] [incubator-nuttx] nealef opened a new pull request, #7202: Add support for the loading of ET_DYN objects

nealef opened a new pull request, #7202:
URL: https://github.com/apache/incubator-nuttx/pull/7202

   **This is not a real PR**. It follows on from my [presentation](https://www.youtube.com/watch?v=qSvYHhM7hz0&t=4711s) at NuttX 2022 Workshop. It is my 1st attempt to take my changes to our backlevel NuttX repo and apply it to the current master. It is for information only as people expressed a desire to see what was done so I thought this was the best way to do it. 
   
   #### Caveats
   * Created specifically for the Meadow project which uses an arm7m - not a general solution
   * Built using a custom configuration
     - Important bits:
   ```
       CONFIG_BUILD_PROTECTED=y
       CONFIG_BUILD_2PASS=y
       CONFIG_PASS1_TARGET="all"
       CONFIG_PASS1_BUILDIR="boards/arm/stm32/olimex-stm32-p407/kernel"
       CONFIG_PASS1_OBJECT=""
       CONFIG_NUTTX_USERSPACE=0x08020000
       :
       CONFIG_LIBC_DLFCN=y 
       CONFIG_LIBC_MODLIB=y
       :
       #
       # Module library configuration
       #
       CONFIG_MODLIB_MAXDEPEND=2
       CONFIG_MODLIB_ALIGN_LOG2=2
       CONFIG_MODLIB_BUFFERSIZE=32
       CONFIG_MODLIB_BUFFERINCR=32
       CONFIG_MODLIB_RELOCATION_BUFFERCOUNT=256
       CONFIG_MODLIB_SYMBOL_CACHECOUNT=256
   ```
   * Not tried in other modes other than PROTECTED
   * Does not handle `.so` objects having dependencies on other `.so` objects
   * Only armv7-m has its `arch_elf.c` updated with the additional relocation types
   * It builds cleanly but I have not run to verify functionality
   
   #### Change Descriptions
   
   * build-globals.sh
     - Build the modlib_globals.S file used to resolve symbols when dynamically loading
     -  I have not looked at `mksymtab.c` to see if this will generate an architecture-independent of what I am attempting to create with this script
   
   * include/elf.h
     - Add definitions that may be found in shared objects
   
   * include/nuttx/lib/modlib.h
     - Add fields to mod_loadifno_s:
       - Program headers
       - Exported symbols
       - Data section address
       - Padding requirement
       - Section index for dynamic symbol table
       - Number of symbols exported
     - Add prottotype for modlib_freesymtab
   
   * libs/libc/dlfcn/lib_dlclose.c
     - Free the symbol table when the dll is closed
   
   * libs/libc/dlfcn/lib_dlopen.c
     - Add dump of program headers to debug routine
     - Differentiate between ET_REL and ET_DYN objects
   
   * libs/libc/machine/arm/armv7-m/arch_elf.c
     - Add handling of R_ARM_RELATIVE and R_ARM_JUMP slot relocation types
   
   * libs/libc/modlib/Make.defs
     - Include new file containing library APIs used when loading shared objects
   
   * libs/libc/modlib/modlib.h
     - Add parameter to modlib_readsym prototype
     - Add prototypes for:
       - modlib_insertsymtab
       - modlib_findglobal
   
   * libs/libc/modlib/modlib_loadshdrs.c
     - Rename modlib_loadshdrs.c to modlib_loadhdrs.c
     - Rename modlib_loadshdrs to modlib_loadhdrs
     - Add code to load program headers
   
   * libs/libc/modlib/modlib_bind.c
     - Call modlib_readsym with pointer to symbol table
     - Add modlib_relocatedyn to manage relocation of symbols with shared object (ET_DYN)
     - Differentiate between ET_REL and ET_DYN objects
   
   * libs/libc/modlib/modlib_load.c
     - Calculate sizes and text/data addresses based on program headers rather than section headers
   
   * libs/libc/modlib/modlib_symbols.c
     - Define entry point structure
     - Add offset parameter to modlib_symname() and use to find symbol names
     - Add symtab section header parameter to modlib_readsym()
     - Add offset parameter to modlib_symvalue() to locate symbol names
     - Add modlib_insertsyntab() to create a symbol table for exporting and resolution
     - Add findEP() to resolve a symbol in the modlib_global table
     - Add modlib_findglobal() to find symbol in the modlib_global table
     - Add modlib_freesymtab() to free the symbol table
   
   * libs/libc/modlib/modlib_uninit.c
     - Free header and sections from a module_loadinfo_s control block
   
   * libs/libc/modlib/modlib_verify.c
     - Handle ET_DYN shared objects
   
   * libs/libc/modlib/modlib_globals.S
     - Define library APIs that may be resolved when loading a shared object


-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [incubator-nuttx] nealef commented on a diff in pull request #7202: Add support for the loading of ET_DYN objects

Posted by GitBox <gi...@apache.org>.
nealef commented on code in PR #7202:
URL: https://github.com/apache/incubator-nuttx/pull/7202#discussion_r981863847


##########
libs/libc/modlib/modlib_bind.c:
##########
@@ -518,6 +518,228 @@ static int modlib_relocateadd(FAR struct module_s *modp,
   return ret;
 }
 
+/****************************************************************************
+ * Name: modlib_relocatedyn
+ *
+ * Description:
+ *   Perform all relocations associated with a dynamic section.
+ *
+ * Returned Value:
+ *   0 (OK) is returned on success and a negated errno is returned on
+ *   failure.
+ *
+ ****************************************************************************/
+
+static int modlib_relocatedyn(FAR struct module_s *modp,
+                              FAR struct mod_loadinfo_s *loadinfo, int relidx)
+
+{
+  FAR Elf32_Shdr *shdr = &loadinfo->shdr[relidx];
+  FAR Elf32_Shdr *symhdr;
+  FAR Elf32_Dyn  *dyn = NULL;
+  FAR Elf32_Rel  *rels = NULL;
+  FAR Elf32_Rel  *rel;
+  FAR Elf32_Sym  *sym = NULL;
+  uintptr_t       addr;
+  int             ret;
+  int             i, iRel, iSym;
+  struct {
+	int	strOff;		/* Offset to string table */
+	int	symOff;		/* Offset to symbol table */
+	int	lSymTab;	/* Size of symbol table */
+	int	relEntSz;	/* Size of relocation entry */
+  	int	relOff[2];	/* Offset to the relocation section */
+	int	relSz[2];	/* Size of relocation table */
+#define I_REL	0
+#define I_PLT	1
+#define N_RELS	2
+  } relData;

Review Comment:
   Done. However, I kept the define inside the struct and added comments as it provides context for those PP values.



-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [incubator-nuttx] acassis commented on a diff in pull request #7202: Add support for the loading of ET_DYN objects

Posted by GitBox <gi...@apache.org>.
acassis commented on code in PR #7202:
URL: https://github.com/apache/incubator-nuttx/pull/7202#discussion_r981647569


##########
include/elf.h:
##########
@@ -214,6 +214,15 @@
 #define PT_NOTE            4
 #define PT_SHLIB           5
 #define PT_PHDR            6
+
+/* Processor specific values for the Phdr p_type field.  */
+#define PT_ARM_EXIDX            (PT_LOPROC + 1) /* ARM unwind segment.  */
+
+/* GCC specific */
+#define PT_GNU_EH_FRAME 0x6474e550      /* GCC exception handler frame */

Review Comment:
   ditto



##########
include/elf.h:
##########
@@ -214,6 +214,15 @@
 #define PT_NOTE            4
 #define PT_SHLIB           5
 #define PT_PHDR            6
+
+/* Processor specific values for the Phdr p_type field.  */
+#define PT_ARM_EXIDX            (PT_LOPROC + 1) /* ARM unwind segment.  */

Review Comment:
   Add space between comments and code/definition lines



##########
libs/libc/modlib/modlib_bind.c:
##########
@@ -518,6 +518,228 @@ static int modlib_relocateadd(FAR struct module_s *modp,
   return ret;
 }
 
+/****************************************************************************
+ * Name: modlib_relocatedyn
+ *
+ * Description:
+ *   Perform all relocations associated with a dynamic section.
+ *
+ * Returned Value:
+ *   0 (OK) is returned on success and a negated errno is returned on
+ *   failure.
+ *
+ ****************************************************************************/
+
+static int modlib_relocatedyn(FAR struct module_s *modp,
+                              FAR struct mod_loadinfo_s *loadinfo, int relidx)
+
+{
+  FAR Elf32_Shdr *shdr = &loadinfo->shdr[relidx];
+  FAR Elf32_Shdr *symhdr;
+  FAR Elf32_Dyn  *dyn = NULL;
+  FAR Elf32_Rel  *rels = NULL;
+  FAR Elf32_Rel  *rel;
+  FAR Elf32_Sym  *sym = NULL;
+  uintptr_t       addr;
+  int             ret;
+  int             i, iRel, iSym;
+  struct {
+	int	strOff;		/* Offset to string table */
+	int	symOff;		/* Offset to symbol table */
+	int	lSymTab;	/* Size of symbol table */
+	int	relEntSz;	/* Size of relocation entry */
+  	int	relOff[2];	/* Offset to the relocation section */
+	int	relSz[2];	/* Size of relocation table */
+#define I_REL	0
+#define I_PLT	1
+#define N_RELS	2
+  } relData;
+
+  dyn = lib_malloc(shdr->sh_size);
+  ret = modlib_read(loadinfo, (FAR uint8_t *) dyn, shdr->sh_size, shdr->sh_offset);
+  if (ret < 0) 
+    {
+      berr("Failed to read dynamic section header");
+      return ret;
+    }
+
+  rels = lib_malloc(CONFIG_MODLIB_RELOCATION_BUFFERCOUNT * sizeof(Elf32_Rel));
+  if (!rels)
+    {
+      berr("Failed to allocate memory for elf relocation rels\n");
+      lib_free(dyn);
+      return -ENOMEM;
+    }
+
+  memset((void *) &relData, 0, sizeof(relData));
+
+  for (i = 0; dyn[i].d_tag != DT_NULL; i++) 
+    {
+      switch(dyn[i].d_tag) 
+	{
+          case DT_REL :
+              relData.relOff[I_REL] = dyn[i].d_un.d_val;
+              break;
+          case DT_RELSZ :
+              relData.relSz[I_REL] = dyn[i].d_un.d_val;
+              break;
+          case DT_RELENT :
+              relData.relEntSz = dyn[i].d_un.d_val;
+              break;
+	  case DT_SYMTAB :
+	      relData.symOff = dyn[i].d_un.d_val;
+ 	      break;
+	  case DT_STRTAB :
+	      relData.strOff = dyn[i].d_un.d_val;
+ 	      break;
+	  case DT_JMPREL :
+	      relData.relOff[I_PLT] = dyn[i].d_un.d_val;
+ 	      break;
+	  case DT_PLTRELSZ :
+	      relData.relSz[I_PLT] = dyn[i].d_un.d_val;
+ 	      break;
+        }
+    }
+
+  symhdr = &loadinfo->shdr[loadinfo->dsymtabidx];
+  sym = lib_malloc(symhdr->sh_size);
+  if (!sym)
+    {
+      berr("Error obtaining storage for dynamic symbol table");
+      lib_free(rels);
+      lib_free(dyn);
+      return -ENOMEM;
+    }
+
+  ret = modlib_read(loadinfo, (uint8_t *) sym, symhdr->sh_size, symhdr->sh_offset);
+  if (ret < 0) 
+    {
+      berr("Error reading dynamic symbol table - %d", ret);
+      lib_free(sym);
+      lib_free(rels);
+      lib_free(dyn);
+      return ret;
+    }
+
+  relData.lSymTab = relData.strOff - relData.symOff;
+
+  for (iRel = 0; iRel < N_RELS; iRel++)
+    {
+      if (relData.relOff[iRel] == 0)
+          continue;
+
+      /* Examine each relocation in the .rel.* section.
+       */

Review Comment:
   Close comment in the same line, since it is lesser than 80 chars



##########
libs/libc/modlib/modlib_bind.c:
##########
@@ -518,6 +518,228 @@ static int modlib_relocateadd(FAR struct module_s *modp,
   return ret;
 }
 
+/****************************************************************************
+ * Name: modlib_relocatedyn
+ *
+ * Description:
+ *   Perform all relocations associated with a dynamic section.
+ *
+ * Returned Value:
+ *   0 (OK) is returned on success and a negated errno is returned on
+ *   failure.
+ *
+ ****************************************************************************/
+
+static int modlib_relocatedyn(FAR struct module_s *modp,
+                              FAR struct mod_loadinfo_s *loadinfo, int relidx)
+
+{
+  FAR Elf32_Shdr *shdr = &loadinfo->shdr[relidx];
+  FAR Elf32_Shdr *symhdr;
+  FAR Elf32_Dyn  *dyn = NULL;
+  FAR Elf32_Rel  *rels = NULL;
+  FAR Elf32_Rel  *rel;
+  FAR Elf32_Sym  *sym = NULL;
+  uintptr_t       addr;
+  int             ret;
+  int             i, iRel, iSym;
+  struct {
+	int	strOff;		/* Offset to string table */
+	int	symOff;		/* Offset to symbol table */
+	int	lSymTab;	/* Size of symbol table */
+	int	relEntSz;	/* Size of relocation entry */
+  	int	relOff[2];	/* Offset to the relocation section */
+	int	relSz[2];	/* Size of relocation table */
+#define I_REL	0
+#define I_PLT	1
+#define N_RELS	2
+  } relData;
+
+  dyn = lib_malloc(shdr->sh_size);
+  ret = modlib_read(loadinfo, (FAR uint8_t *) dyn, shdr->sh_size, shdr->sh_offset);
+  if (ret < 0) 
+    {
+      berr("Failed to read dynamic section header");
+      return ret;
+    }
+
+  rels = lib_malloc(CONFIG_MODLIB_RELOCATION_BUFFERCOUNT * sizeof(Elf32_Rel));
+  if (!rels)
+    {
+      berr("Failed to allocate memory for elf relocation rels\n");
+      lib_free(dyn);
+      return -ENOMEM;
+    }
+
+  memset((void *) &relData, 0, sizeof(relData));
+
+  for (i = 0; dyn[i].d_tag != DT_NULL; i++) 
+    {
+      switch(dyn[i].d_tag) 
+	{
+          case DT_REL :
+              relData.relOff[I_REL] = dyn[i].d_un.d_val;
+              break;
+          case DT_RELSZ :
+              relData.relSz[I_REL] = dyn[i].d_un.d_val;
+              break;
+          case DT_RELENT :
+              relData.relEntSz = dyn[i].d_un.d_val;
+              break;
+	  case DT_SYMTAB :
+	      relData.symOff = dyn[i].d_un.d_val;
+ 	      break;
+	  case DT_STRTAB :
+	      relData.strOff = dyn[i].d_un.d_val;
+ 	      break;
+	  case DT_JMPREL :
+	      relData.relOff[I_PLT] = dyn[i].d_un.d_val;
+ 	      break;
+	  case DT_PLTRELSZ :
+	      relData.relSz[I_PLT] = dyn[i].d_un.d_val;
+ 	      break;
+        }
+    }
+
+  symhdr = &loadinfo->shdr[loadinfo->dsymtabidx];
+  sym = lib_malloc(symhdr->sh_size);
+  if (!sym)
+    {
+      berr("Error obtaining storage for dynamic symbol table");
+      lib_free(rels);
+      lib_free(dyn);
+      return -ENOMEM;
+    }
+
+  ret = modlib_read(loadinfo, (uint8_t *) sym, symhdr->sh_size, symhdr->sh_offset);
+  if (ret < 0) 
+    {
+      berr("Error reading dynamic symbol table - %d", ret);
+      lib_free(sym);
+      lib_free(rels);
+      lib_free(dyn);
+      return ret;
+    }
+
+  relData.lSymTab = relData.strOff - relData.symOff;
+
+  for (iRel = 0; iRel < N_RELS; iRel++)
+    {
+      if (relData.relOff[iRel] == 0)
+          continue;

Review Comment:
   Please include braces of "if ()" even with a single line, required bu nxstyle to avoid mistakes in the future



##########
libs/libc/modlib/modlib_bind.c:
##########
@@ -518,6 +518,228 @@ static int modlib_relocateadd(FAR struct module_s *modp,
   return ret;
 }
 
+/****************************************************************************
+ * Name: modlib_relocatedyn
+ *
+ * Description:
+ *   Perform all relocations associated with a dynamic section.
+ *
+ * Returned Value:
+ *   0 (OK) is returned on success and a negated errno is returned on
+ *   failure.
+ *
+ ****************************************************************************/
+
+static int modlib_relocatedyn(FAR struct module_s *modp,
+                              FAR struct mod_loadinfo_s *loadinfo, int relidx)
+
+{
+  FAR Elf32_Shdr *shdr = &loadinfo->shdr[relidx];
+  FAR Elf32_Shdr *symhdr;
+  FAR Elf32_Dyn  *dyn = NULL;
+  FAR Elf32_Rel  *rels = NULL;
+  FAR Elf32_Rel  *rel;
+  FAR Elf32_Sym  *sym = NULL;
+  uintptr_t       addr;
+  int             ret;
+  int             i, iRel, iSym;
+  struct {
+	int	strOff;		/* Offset to string table */
+	int	symOff;		/* Offset to symbol table */
+	int	lSymTab;	/* Size of symbol table */
+	int	relEntSz;	/* Size of relocation entry */
+  	int	relOff[2];	/* Offset to the relocation section */
+	int	relSz[2];	/* Size of relocation table */
+#define I_REL	0
+#define I_PLT	1
+#define N_RELS	2
+  } relData;
+
+  dyn = lib_malloc(shdr->sh_size);
+  ret = modlib_read(loadinfo, (FAR uint8_t *) dyn, shdr->sh_size, shdr->sh_offset);
+  if (ret < 0) 
+    {
+      berr("Failed to read dynamic section header");
+      return ret;
+    }
+
+  rels = lib_malloc(CONFIG_MODLIB_RELOCATION_BUFFERCOUNT * sizeof(Elf32_Rel));
+  if (!rels)
+    {
+      berr("Failed to allocate memory for elf relocation rels\n");
+      lib_free(dyn);
+      return -ENOMEM;
+    }
+
+  memset((void *) &relData, 0, sizeof(relData));
+
+  for (i = 0; dyn[i].d_tag != DT_NULL; i++) 
+    {
+      switch(dyn[i].d_tag) 
+	{
+          case DT_REL :
+              relData.relOff[I_REL] = dyn[i].d_un.d_val;
+              break;
+          case DT_RELSZ :
+              relData.relSz[I_REL] = dyn[i].d_un.d_val;
+              break;
+          case DT_RELENT :
+              relData.relEntSz = dyn[i].d_un.d_val;
+              break;
+	  case DT_SYMTAB :
+	      relData.symOff = dyn[i].d_un.d_val;
+ 	      break;
+	  case DT_STRTAB :
+	      relData.strOff = dyn[i].d_un.d_val;
+ 	      break;
+	  case DT_JMPREL :
+	      relData.relOff[I_PLT] = dyn[i].d_un.d_val;
+ 	      break;
+	  case DT_PLTRELSZ :
+	      relData.relSz[I_PLT] = dyn[i].d_un.d_val;
+ 	      break;
+        }
+    }
+
+  symhdr = &loadinfo->shdr[loadinfo->dsymtabidx];
+  sym = lib_malloc(symhdr->sh_size);
+  if (!sym)
+    {
+      berr("Error obtaining storage for dynamic symbol table");
+      lib_free(rels);
+      lib_free(dyn);
+      return -ENOMEM;
+    }
+
+  ret = modlib_read(loadinfo, (uint8_t *) sym, symhdr->sh_size, symhdr->sh_offset);
+  if (ret < 0) 
+    {
+      berr("Error reading dynamic symbol table - %d", ret);
+      lib_free(sym);
+      lib_free(rels);
+      lib_free(dyn);
+      return ret;
+    }
+
+  relData.lSymTab = relData.strOff - relData.symOff;
+
+  for (iRel = 0; iRel < N_RELS; iRel++)
+    {
+      if (relData.relOff[iRel] == 0)
+          continue;
+
+      /* Examine each relocation in the .rel.* section.
+       */
+
+      ret = OK;
+
+      for (i = 0; i < relData.relSz[iRel] / relData.relEntSz; i++)
+        {
+          /* Process each relocation entry */
+
+          rel = &rels[i % CONFIG_MODLIB_RELOCATION_BUFFERCOUNT];
+
+          if (!(i % CONFIG_MODLIB_RELOCATION_BUFFERCOUNT))
+            {
+              ret = modlib_read(loadinfo, (FAR uint8_t *) rels, 
+                                sizeof(Elf32_Rel) * CONFIG_MODLIB_RELOCATION_BUFFERCOUNT,
+                                relData.relOff[iRel] + i * sizeof(Elf32_Rel));
+              if (ret < 0)
+                {
+                  berr("ERROR: Section %d reloc %d: Failed to read relocation entry: %d\n",
+                       relidx, i, ret);
+                  break;
+                }
+            }
+
+          /* Calculate the relocation address. */
+
+          if (rel->r_offset < 0)
+            {
+              berr("ERROR: Section %d reloc %d: Relocation address out of range, offset %d\n",
+                   relidx, i, rel->r_offset);
+              ret = -EINVAL;
+              lib_free(sym);
+              lib_free(rels);
+              lib_free(dyn);
+              return ret;
+            }
+
+          /* Now perform the architecture-specific relocation */
+
+          if ((iSym = ELF32_R_SYM(rel->r_info)) != 0) 
+            {
+              if (sym[iSym].st_shndx == SHN_UNDEF)	/* We have an external reference */
+                {
+                    void *ep;
+
+                    ep = modlib_findglobal(modp, loadinfo, symhdr, &sym[iSym]);
+                    if (ep == NULL) 
+                      {
+                        berr("ERROR: Unable to resolve address of external reference %s\n",
+                             loadinfo->iobuffer);
+                        ret = -EINVAL;
+                        lib_free(sym);
+                        lib_free(rels);
+                        lib_free(dyn);
+                        return ret;
+                      }
+
+                    addr = rel->r_offset + loadinfo->textalloc;
+		    *(uintptr_t *)addr = (uintptr_t)ep;
+                }
+            }
+          else
+            {
+              Elf32_Sym dynSym;
+
+              addr = rel->r_offset - loadinfo->datasec + loadinfo->datastart;
+
+              if ((*(uint32_t *) addr) < loadinfo->datasec)
+                  dynSym.st_value = *(uint32_t *) addr + loadinfo->textalloc;
+              else
+                  dynSym.st_value = *(uint32_t *) addr - loadinfo->datasec + loadinfo->datastart;
+              ret = up_relocate(rel, &dynSym, addr);
+            }
+
+          if (ret < 0)
+            {
+              berr("ERROR: Section %d reloc %d: Relocation failed: %d\n", relidx, i, ret);
+              lib_free(sym);
+              lib_free(rels);
+              lib_free(dyn);
+              return ret;
+            }
+        }
+    }    
+
+  /* Iterate through the dynamic symbol table looking for global symbols to put in 
+   * our own symbol table for use with dlgetsym()
+   */
+
+  /* Relocate the entries in the table */
+  for (i = 0; i < (symhdr->sh_size / sizeof(Elf32_Sym)); i++)

Review Comment:
   Add space between comment line and code line



##########
libs/libc/modlib/modlib_bind.c:
##########
@@ -518,6 +518,228 @@ static int modlib_relocateadd(FAR struct module_s *modp,
   return ret;
 }
 
+/****************************************************************************
+ * Name: modlib_relocatedyn
+ *
+ * Description:
+ *   Perform all relocations associated with a dynamic section.
+ *
+ * Returned Value:
+ *   0 (OK) is returned on success and a negated errno is returned on
+ *   failure.
+ *
+ ****************************************************************************/
+
+static int modlib_relocatedyn(FAR struct module_s *modp,
+                              FAR struct mod_loadinfo_s *loadinfo, int relidx)
+
+{
+  FAR Elf32_Shdr *shdr = &loadinfo->shdr[relidx];
+  FAR Elf32_Shdr *symhdr;
+  FAR Elf32_Dyn  *dyn = NULL;
+  FAR Elf32_Rel  *rels = NULL;
+  FAR Elf32_Rel  *rel;
+  FAR Elf32_Sym  *sym = NULL;
+  uintptr_t       addr;
+  int             ret;
+  int             i, iRel, iSym;
+  struct {
+	int	strOff;		/* Offset to string table */
+	int	symOff;		/* Offset to symbol table */
+	int	lSymTab;	/* Size of symbol table */
+	int	relEntSz;	/* Size of relocation entry */
+  	int	relOff[2];	/* Offset to the relocation section */
+	int	relSz[2];	/* Size of relocation table */
+#define I_REL	0
+#define I_PLT	1
+#define N_RELS	2
+  } relData;

Review Comment:
   It is better to create a struct definition outsite and only create an instance here, also these #defines could be moved to outside of the struct as well.



##########
libs/libc/modlib/modlib_bind.c:
##########
@@ -577,24 +799,38 @@ int modlib_bind(FAR struct module_s *modp,
           continue;
         }
 
-      /* Make sure that the section is allocated.  We can't relocate
-       * sections that were not loaded into memory.
-       */
-
-      if ((loadinfo->shdr[infosec].sh_flags & SHF_ALLOC) == 0)
+      if (loadinfo->ehdr.e_type == ET_DYN) 
         {
-          continue;
-        }
+          switch (loadinfo->shdr[i].sh_type) 
+            {
+              case SHT_DYNAMIC :
+                  ret = modlib_relocatedyn(modp, loadinfo, i);
+                  break;
+              case SHT_DYNSYM :
+                  loadinfo->dsymtabidx = i;
+                  break;
+            }
+        } 
+      else
+        {
+          /* Make sure that the section is allocated.  We can't relocate
+           * sections that were not loaded into memory.
+           */
 
-      /* Process the relocations by type */
+          if ((loadinfo->shdr[i].sh_flags & SHF_ALLOC) == 0)
+                continue;

Review Comment:
   Ditto



##########
libs/libc/modlib/modlib_load.c:
##########
@@ -148,75 +134,33 @@ static inline int modlib_loadfile(FAR struct mod_loadinfo_s *loadinfo)
   int ret;
   int i;
 
-  /* Read each section into memory that is marked SHF_ALLOC + SHT_NOBITS */
+  /* Read each PT_LOAD area into memory */
 
-  binfo("Loaded sections:\n");
+  binfo("Loading sections - text: %p.%x data: %p.%x\n",
+        loadinfo->textalloc,loadinfo->textsize,loadinfo->datastart,loadinfo->datasize);
   text = (FAR uint8_t *)loadinfo->textalloc;
   data = (FAR uint8_t *)loadinfo->datastart;
 
-  for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
+  for (i = 0; i < loadinfo->ehdr.e_phnum; i++)
     {
-      FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
-
-      /* SHF_ALLOC indicates that the section requires memory during
-       * execution
-       */
-
-      if ((shdr->sh_flags & SHF_ALLOC) == 0)
-        {
-          continue;
-        }
-
-      /* SHF_WRITE indicates that the section address space is write-
-       * able
-       */
-
-      if ((shdr->sh_flags & SHF_WRITE) != 0)
-        {
-          pptr = &data;
-        }
-      else
-        {
-          pptr = &text;
-        }
-
-      *pptr = (FAR uint8_t *)_ALIGN_UP((uintptr_t)*pptr, shdr->sh_addralign);
+      FAR Elf32_Phdr *phdr = &loadinfo->phdr[i];
 
-      /* SHT_NOBITS indicates that there is no data in the file for the
-       * section.
-       */
-
-      if (shdr->sh_type != SHT_NOBITS)
+      if (phdr->p_type == PT_LOAD)
         {
-          /* Read the section data from sh_offset to the memory region */
-
-          ret = modlib_read(loadinfo, *pptr, shdr->sh_size, shdr->sh_offset);
+          if (phdr->p_flags & PF_X)
+              ret = modlib_read(loadinfo, text, phdr->p_filesz, phdr->p_offset);

Review Comment:
   Ditto



##########
libs/libc/modlib/modlib_symbols.c:
##########
@@ -55,6 +56,11 @@ struct mod_exportinfo_s
   FAR const struct symtab_s *symbol; /* Symbol info returned (if found) */
 };
 
+typedef struct {
+	uint8_t	*epName;	/* Name of global symbol */
+	void	*epAddr;	/* Address of global symbol */
+} epTable_t;
+

Review Comment:
   Better avoid typedefing structures: "The use of typedef’ed structures is acceptable but discouraged."
   https://nuttx.apache.org/docs/latest/contributing/coding_style.html



##########
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:
   Ditto, include braces in both: if and else



##########
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:
   Ditto



##########
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;
+}
+
+/****************************************************************************
+ * Name: modlib_freesymtab
+ *
+ * Description:
+ *   Free a symbol table
+ *
+ * Input Parameters:
+ *   modp     - Module state information
+ *
+ ****************************************************************************/
+
+void modlib_freesymtab(FAR struct module_s *modp)
+{
+  FAR const struct symtab_s *symbol;
+
+  if ((symbol = modp->modinfo.exports))
+    {
+      for (int i = 0; i < modp->modinfo.nexports; i++)
+          lib_free((FAR void *) symbol[i].sym_name);

Review Comment:
   Please include braces here too



-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [incubator-nuttx] nealef commented on a diff in pull request #7202: Add support for the loading of ET_DYN objects

Posted by GitBox <gi...@apache.org>.
nealef commented on code in PR #7202:
URL: https://github.com/apache/incubator-nuttx/pull/7202#discussion_r981863910


##########
libs/libc/modlib/modlib_bind.c:
##########
@@ -518,6 +518,228 @@ static int modlib_relocateadd(FAR struct module_s *modp,
   return ret;
 }
 
+/****************************************************************************
+ * Name: modlib_relocatedyn
+ *
+ * Description:
+ *   Perform all relocations associated with a dynamic section.
+ *
+ * Returned Value:
+ *   0 (OK) is returned on success and a negated errno is returned on
+ *   failure.
+ *
+ ****************************************************************************/
+
+static int modlib_relocatedyn(FAR struct module_s *modp,
+                              FAR struct mod_loadinfo_s *loadinfo, int relidx)
+
+{
+  FAR Elf32_Shdr *shdr = &loadinfo->shdr[relidx];
+  FAR Elf32_Shdr *symhdr;
+  FAR Elf32_Dyn  *dyn = NULL;
+  FAR Elf32_Rel  *rels = NULL;
+  FAR Elf32_Rel  *rel;
+  FAR Elf32_Sym  *sym = NULL;
+  uintptr_t       addr;
+  int             ret;
+  int             i, iRel, iSym;
+  struct {
+	int	strOff;		/* Offset to string table */
+	int	symOff;		/* Offset to symbol table */
+	int	lSymTab;	/* Size of symbol table */
+	int	relEntSz;	/* Size of relocation entry */
+  	int	relOff[2];	/* Offset to the relocation section */
+	int	relSz[2];	/* Size of relocation table */
+#define I_REL	0
+#define I_PLT	1
+#define N_RELS	2
+  } relData;
+
+  dyn = lib_malloc(shdr->sh_size);
+  ret = modlib_read(loadinfo, (FAR uint8_t *) dyn, shdr->sh_size, shdr->sh_offset);
+  if (ret < 0) 
+    {
+      berr("Failed to read dynamic section header");
+      return ret;
+    }
+
+  rels = lib_malloc(CONFIG_MODLIB_RELOCATION_BUFFERCOUNT * sizeof(Elf32_Rel));
+  if (!rels)
+    {
+      berr("Failed to allocate memory for elf relocation rels\n");
+      lib_free(dyn);
+      return -ENOMEM;
+    }
+
+  memset((void *) &relData, 0, sizeof(relData));
+
+  for (i = 0; dyn[i].d_tag != DT_NULL; i++) 
+    {
+      switch(dyn[i].d_tag) 
+	{
+          case DT_REL :
+              relData.relOff[I_REL] = dyn[i].d_un.d_val;
+              break;
+          case DT_RELSZ :
+              relData.relSz[I_REL] = dyn[i].d_un.d_val;
+              break;
+          case DT_RELENT :
+              relData.relEntSz = dyn[i].d_un.d_val;
+              break;
+	  case DT_SYMTAB :
+	      relData.symOff = dyn[i].d_un.d_val;
+ 	      break;
+	  case DT_STRTAB :
+	      relData.strOff = dyn[i].d_un.d_val;
+ 	      break;
+	  case DT_JMPREL :
+	      relData.relOff[I_PLT] = dyn[i].d_un.d_val;
+ 	      break;
+	  case DT_PLTRELSZ :
+	      relData.relSz[I_PLT] = dyn[i].d_un.d_val;
+ 	      break;
+        }
+    }
+
+  symhdr = &loadinfo->shdr[loadinfo->dsymtabidx];
+  sym = lib_malloc(symhdr->sh_size);
+  if (!sym)
+    {
+      berr("Error obtaining storage for dynamic symbol table");
+      lib_free(rels);
+      lib_free(dyn);
+      return -ENOMEM;
+    }
+
+  ret = modlib_read(loadinfo, (uint8_t *) sym, symhdr->sh_size, symhdr->sh_offset);
+  if (ret < 0) 
+    {
+      berr("Error reading dynamic symbol table - %d", ret);
+      lib_free(sym);
+      lib_free(rels);
+      lib_free(dyn);
+      return ret;
+    }
+
+  relData.lSymTab = relData.strOff - relData.symOff;
+
+  for (iRel = 0; iRel < N_RELS; iRel++)
+    {
+      if (relData.relOff[iRel] == 0)
+          continue;
+
+      /* Examine each relocation in the .rel.* section.
+       */

Review Comment:
   Fixed



##########
libs/libc/modlib/modlib_bind.c:
##########
@@ -518,6 +518,228 @@ static int modlib_relocateadd(FAR struct module_s *modp,
   return ret;
 }
 
+/****************************************************************************
+ * Name: modlib_relocatedyn
+ *
+ * Description:
+ *   Perform all relocations associated with a dynamic section.
+ *
+ * Returned Value:
+ *   0 (OK) is returned on success and a negated errno is returned on
+ *   failure.
+ *
+ ****************************************************************************/
+
+static int modlib_relocatedyn(FAR struct module_s *modp,
+                              FAR struct mod_loadinfo_s *loadinfo, int relidx)
+
+{
+  FAR Elf32_Shdr *shdr = &loadinfo->shdr[relidx];
+  FAR Elf32_Shdr *symhdr;
+  FAR Elf32_Dyn  *dyn = NULL;
+  FAR Elf32_Rel  *rels = NULL;
+  FAR Elf32_Rel  *rel;
+  FAR Elf32_Sym  *sym = NULL;
+  uintptr_t       addr;
+  int             ret;
+  int             i, iRel, iSym;
+  struct {
+	int	strOff;		/* Offset to string table */
+	int	symOff;		/* Offset to symbol table */
+	int	lSymTab;	/* Size of symbol table */
+	int	relEntSz;	/* Size of relocation entry */
+  	int	relOff[2];	/* Offset to the relocation section */
+	int	relSz[2];	/* Size of relocation table */
+#define I_REL	0
+#define I_PLT	1
+#define N_RELS	2
+  } relData;
+
+  dyn = lib_malloc(shdr->sh_size);
+  ret = modlib_read(loadinfo, (FAR uint8_t *) dyn, shdr->sh_size, shdr->sh_offset);
+  if (ret < 0) 
+    {
+      berr("Failed to read dynamic section header");
+      return ret;
+    }
+
+  rels = lib_malloc(CONFIG_MODLIB_RELOCATION_BUFFERCOUNT * sizeof(Elf32_Rel));
+  if (!rels)
+    {
+      berr("Failed to allocate memory for elf relocation rels\n");
+      lib_free(dyn);
+      return -ENOMEM;
+    }
+
+  memset((void *) &relData, 0, sizeof(relData));
+
+  for (i = 0; dyn[i].d_tag != DT_NULL; i++) 
+    {
+      switch(dyn[i].d_tag) 
+	{
+          case DT_REL :
+              relData.relOff[I_REL] = dyn[i].d_un.d_val;
+              break;
+          case DT_RELSZ :
+              relData.relSz[I_REL] = dyn[i].d_un.d_val;
+              break;
+          case DT_RELENT :
+              relData.relEntSz = dyn[i].d_un.d_val;
+              break;
+	  case DT_SYMTAB :
+	      relData.symOff = dyn[i].d_un.d_val;
+ 	      break;
+	  case DT_STRTAB :
+	      relData.strOff = dyn[i].d_un.d_val;
+ 	      break;
+	  case DT_JMPREL :
+	      relData.relOff[I_PLT] = dyn[i].d_un.d_val;
+ 	      break;
+	  case DT_PLTRELSZ :
+	      relData.relSz[I_PLT] = dyn[i].d_un.d_val;
+ 	      break;
+        }
+    }
+
+  symhdr = &loadinfo->shdr[loadinfo->dsymtabidx];
+  sym = lib_malloc(symhdr->sh_size);
+  if (!sym)
+    {
+      berr("Error obtaining storage for dynamic symbol table");
+      lib_free(rels);
+      lib_free(dyn);
+      return -ENOMEM;
+    }
+
+  ret = modlib_read(loadinfo, (uint8_t *) sym, symhdr->sh_size, symhdr->sh_offset);
+  if (ret < 0) 
+    {
+      berr("Error reading dynamic symbol table - %d", ret);
+      lib_free(sym);
+      lib_free(rels);
+      lib_free(dyn);
+      return ret;
+    }
+
+  relData.lSymTab = relData.strOff - relData.symOff;
+
+  for (iRel = 0; iRel < N_RELS; iRel++)
+    {
+      if (relData.relOff[iRel] == 0)
+          continue;

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-unsubscribe@nuttx.apache.org

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


[GitHub] [incubator-nuttx] nealef commented on a diff in pull request #7202: Add support for the loading of ET_DYN objects

Posted by GitBox <gi...@apache.org>.
nealef commented on code in PR #7202:
URL: https://github.com/apache/incubator-nuttx/pull/7202#discussion_r981865225


##########
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;
+}
+
+/****************************************************************************
+ * Name: modlib_freesymtab
+ *
+ * Description:
+ *   Free a symbol table
+ *
+ * Input Parameters:
+ *   modp     - Module state information
+ *
+ ****************************************************************************/
+
+void modlib_freesymtab(FAR struct module_s *modp)
+{
+  FAR const struct symtab_s *symbol;
+
+  if ((symbol = modp->modinfo.exports))
+    {
+      for (int i = 0; i < modp->modinfo.nexports; i++)
+          lib_free((FAR void *) symbol[i].sym_name);

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-unsubscribe@nuttx.apache.org

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


[GitHub] [nuttx] acassis commented on a diff in pull request #7202: Add support for the loading of ET_DYN objects

Posted by "acassis (via GitHub)" <gi...@apache.org>.
acassis commented on code in PR #7202:
URL: https://github.com/apache/nuttx/pull/7202#discussion_r1233066939


##########
libs/libc/machine/arm/armv7-m/arch_elf.c:
##########
@@ -123,7 +123,8 @@ int up_relocate(const Elf32_Rel *rel, const Elf32_Sym *sym, uintptr_t addr)
    */

Review Comment:
   Please update the comment to include R_ARM_RELATIVE and R_ARM_JUMP_SLOT



-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [nuttx] acassis merged pull request #7202: Add support for the loading of ET_DYN objects

Posted by "acassis (via GitHub)" <gi...@apache.org>.
acassis merged PR #7202:
URL: https://github.com/apache/nuttx/pull/7202


-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [incubator-nuttx] nealef commented on a diff in pull request #7202: Add support for the loading of ET_DYN objects

Posted by GitBox <gi...@apache.org>.
nealef commented on code in PR #7202:
URL: https://github.com/apache/incubator-nuttx/pull/7202#discussion_r981864061


##########
libs/libc/modlib/modlib_bind.c:
##########
@@ -518,6 +518,228 @@ static int modlib_relocateadd(FAR struct module_s *modp,
   return ret;
 }
 
+/****************************************************************************
+ * Name: modlib_relocatedyn
+ *
+ * Description:
+ *   Perform all relocations associated with a dynamic section.
+ *
+ * Returned Value:
+ *   0 (OK) is returned on success and a negated errno is returned on
+ *   failure.
+ *
+ ****************************************************************************/
+
+static int modlib_relocatedyn(FAR struct module_s *modp,
+                              FAR struct mod_loadinfo_s *loadinfo, int relidx)
+
+{
+  FAR Elf32_Shdr *shdr = &loadinfo->shdr[relidx];
+  FAR Elf32_Shdr *symhdr;
+  FAR Elf32_Dyn  *dyn = NULL;
+  FAR Elf32_Rel  *rels = NULL;
+  FAR Elf32_Rel  *rel;
+  FAR Elf32_Sym  *sym = NULL;
+  uintptr_t       addr;
+  int             ret;
+  int             i, iRel, iSym;
+  struct {
+	int	strOff;		/* Offset to string table */
+	int	symOff;		/* Offset to symbol table */
+	int	lSymTab;	/* Size of symbol table */
+	int	relEntSz;	/* Size of relocation entry */
+  	int	relOff[2];	/* Offset to the relocation section */
+	int	relSz[2];	/* Size of relocation table */
+#define I_REL	0
+#define I_PLT	1
+#define N_RELS	2
+  } relData;
+
+  dyn = lib_malloc(shdr->sh_size);
+  ret = modlib_read(loadinfo, (FAR uint8_t *) dyn, shdr->sh_size, shdr->sh_offset);
+  if (ret < 0) 
+    {
+      berr("Failed to read dynamic section header");
+      return ret;
+    }
+
+  rels = lib_malloc(CONFIG_MODLIB_RELOCATION_BUFFERCOUNT * sizeof(Elf32_Rel));
+  if (!rels)
+    {
+      berr("Failed to allocate memory for elf relocation rels\n");
+      lib_free(dyn);
+      return -ENOMEM;
+    }
+
+  memset((void *) &relData, 0, sizeof(relData));
+
+  for (i = 0; dyn[i].d_tag != DT_NULL; i++) 
+    {
+      switch(dyn[i].d_tag) 
+	{
+          case DT_REL :
+              relData.relOff[I_REL] = dyn[i].d_un.d_val;
+              break;
+          case DT_RELSZ :
+              relData.relSz[I_REL] = dyn[i].d_un.d_val;
+              break;
+          case DT_RELENT :
+              relData.relEntSz = dyn[i].d_un.d_val;
+              break;
+	  case DT_SYMTAB :
+	      relData.symOff = dyn[i].d_un.d_val;
+ 	      break;
+	  case DT_STRTAB :
+	      relData.strOff = dyn[i].d_un.d_val;
+ 	      break;
+	  case DT_JMPREL :
+	      relData.relOff[I_PLT] = dyn[i].d_un.d_val;
+ 	      break;
+	  case DT_PLTRELSZ :
+	      relData.relSz[I_PLT] = dyn[i].d_un.d_val;
+ 	      break;
+        }
+    }
+
+  symhdr = &loadinfo->shdr[loadinfo->dsymtabidx];
+  sym = lib_malloc(symhdr->sh_size);
+  if (!sym)
+    {
+      berr("Error obtaining storage for dynamic symbol table");
+      lib_free(rels);
+      lib_free(dyn);
+      return -ENOMEM;
+    }
+
+  ret = modlib_read(loadinfo, (uint8_t *) sym, symhdr->sh_size, symhdr->sh_offset);
+  if (ret < 0) 
+    {
+      berr("Error reading dynamic symbol table - %d", ret);
+      lib_free(sym);
+      lib_free(rels);
+      lib_free(dyn);
+      return ret;
+    }
+
+  relData.lSymTab = relData.strOff - relData.symOff;
+
+  for (iRel = 0; iRel < N_RELS; iRel++)
+    {
+      if (relData.relOff[iRel] == 0)
+          continue;
+
+      /* Examine each relocation in the .rel.* section.
+       */
+
+      ret = OK;
+
+      for (i = 0; i < relData.relSz[iRel] / relData.relEntSz; i++)
+        {
+          /* Process each relocation entry */
+
+          rel = &rels[i % CONFIG_MODLIB_RELOCATION_BUFFERCOUNT];
+
+          if (!(i % CONFIG_MODLIB_RELOCATION_BUFFERCOUNT))
+            {
+              ret = modlib_read(loadinfo, (FAR uint8_t *) rels, 
+                                sizeof(Elf32_Rel) * CONFIG_MODLIB_RELOCATION_BUFFERCOUNT,
+                                relData.relOff[iRel] + i * sizeof(Elf32_Rel));
+              if (ret < 0)
+                {
+                  berr("ERROR: Section %d reloc %d: Failed to read relocation entry: %d\n",
+                       relidx, i, ret);
+                  break;
+                }
+            }
+
+          /* Calculate the relocation address. */
+
+          if (rel->r_offset < 0)
+            {
+              berr("ERROR: Section %d reloc %d: Relocation address out of range, offset %d\n",
+                   relidx, i, rel->r_offset);
+              ret = -EINVAL;
+              lib_free(sym);
+              lib_free(rels);
+              lib_free(dyn);
+              return ret;
+            }
+
+          /* Now perform the architecture-specific relocation */
+
+          if ((iSym = ELF32_R_SYM(rel->r_info)) != 0) 
+            {
+              if (sym[iSym].st_shndx == SHN_UNDEF)	/* We have an external reference */
+                {
+                    void *ep;
+
+                    ep = modlib_findglobal(modp, loadinfo, symhdr, &sym[iSym]);
+                    if (ep == NULL) 
+                      {
+                        berr("ERROR: Unable to resolve address of external reference %s\n",
+                             loadinfo->iobuffer);
+                        ret = -EINVAL;
+                        lib_free(sym);
+                        lib_free(rels);
+                        lib_free(dyn);
+                        return ret;
+                      }
+
+                    addr = rel->r_offset + loadinfo->textalloc;
+		    *(uintptr_t *)addr = (uintptr_t)ep;
+                }
+            }
+          else
+            {
+              Elf32_Sym dynSym;
+
+              addr = rel->r_offset - loadinfo->datasec + loadinfo->datastart;
+
+              if ((*(uint32_t *) addr) < loadinfo->datasec)
+                  dynSym.st_value = *(uint32_t *) addr + loadinfo->textalloc;
+              else
+                  dynSym.st_value = *(uint32_t *) addr - loadinfo->datasec + loadinfo->datastart;
+              ret = up_relocate(rel, &dynSym, addr);
+            }
+
+          if (ret < 0)
+            {
+              berr("ERROR: Section %d reloc %d: Relocation failed: %d\n", relidx, i, ret);
+              lib_free(sym);
+              lib_free(rels);
+              lib_free(dyn);
+              return ret;
+            }
+        }
+    }    
+
+  /* Iterate through the dynamic symbol table looking for global symbols to put in 
+   * our own symbol table for use with dlgetsym()
+   */
+
+  /* Relocate the entries in the table */
+  for (i = 0; i < (symhdr->sh_size / sizeof(Elf32_Sym)); i++)

Review Comment:
   Fixed



##########
libs/libc/modlib/modlib_load.c:
##########
@@ -148,75 +134,33 @@ static inline int modlib_loadfile(FAR struct mod_loadinfo_s *loadinfo)
   int ret;
   int i;
 
-  /* Read each section into memory that is marked SHF_ALLOC + SHT_NOBITS */
+  /* Read each PT_LOAD area into memory */
 
-  binfo("Loaded sections:\n");
+  binfo("Loading sections - text: %p.%x data: %p.%x\n",
+        loadinfo->textalloc,loadinfo->textsize,loadinfo->datastart,loadinfo->datasize);
   text = (FAR uint8_t *)loadinfo->textalloc;
   data = (FAR uint8_t *)loadinfo->datastart;
 
-  for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
+  for (i = 0; i < loadinfo->ehdr.e_phnum; i++)
     {
-      FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
-
-      /* SHF_ALLOC indicates that the section requires memory during
-       * execution
-       */
-
-      if ((shdr->sh_flags & SHF_ALLOC) == 0)
-        {
-          continue;
-        }
-
-      /* SHF_WRITE indicates that the section address space is write-
-       * able
-       */
-
-      if ((shdr->sh_flags & SHF_WRITE) != 0)
-        {
-          pptr = &data;
-        }
-      else
-        {
-          pptr = &text;
-        }
-
-      *pptr = (FAR uint8_t *)_ALIGN_UP((uintptr_t)*pptr, shdr->sh_addralign);
+      FAR Elf32_Phdr *phdr = &loadinfo->phdr[i];
 
-      /* SHT_NOBITS indicates that there is no data in the file for the
-       * section.
-       */
-
-      if (shdr->sh_type != SHT_NOBITS)
+      if (phdr->p_type == PT_LOAD)
         {
-          /* Read the section data from sh_offset to the memory region */
-
-          ret = modlib_read(loadinfo, *pptr, shdr->sh_size, shdr->sh_offset);
+          if (phdr->p_flags & PF_X)
+              ret = modlib_read(loadinfo, text, phdr->p_filesz, phdr->p_offset);

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-unsubscribe@nuttx.apache.org

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


[GitHub] [nuttx] nealef commented on pull request #7202: Add support for the loading of ET_DYN objects

Posted by "nealef (via GitHub)" <gi...@apache.org>.
nealef commented on PR #7202:
URL: https://github.com/apache/nuttx/pull/7202#issuecomment-1593640243

   Yes, I forgot to add the .S file (did a git commit -a rather than a git add . for these new files). It has built again but we now fail with a relocation error.
   
   
   


-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [incubator-nuttx] nealef commented on a diff in pull request #7202: Add support for the loading of ET_DYN objects

Posted by GitBox <gi...@apache.org>.
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-unsubscribe@nuttx.apache.org

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


[GitHub] [nuttx] acassis commented on pull request #7202: Add support for the loading of ET_DYN objects

Posted by "acassis (via GitHub)" <gi...@apache.org>.
acassis commented on PR #7202:
URL: https://github.com/apache/nuttx/pull/7202#issuecomment-1589472859

   @xiaoxiang781216 :
   ```
   ====================================================================================
   Configuration/Tool: c5471evm/nsh,CONFIG_ARM_TOOLCHAIN_GNU_EABI
   ------------------------------------------------------------------------------------
     Cleaning...
     Configuring...
   /usr/bin/bash: arm-nuttx-eabi-gcc: command not found
   /usr/bin/bash: arm-nuttx-eabi-gcc: command not found
   /usr/bin/bash: arm-nuttx-eabi-gcc: command not found
   /usr/bin/bash: arm-nuttx-eabi-gcc: command not found
   /usr/bin/bash: arm-nuttx-eabi-gcc: command not found
   /usr/bin/bash: arm-nuttx-eabi-gcc: command not found
   /usr/bin/bash: arm-nuttx-eabi-gcc: command not found
   /usr/bin/bash: arm-nuttx-eabi-gcc: command not found
   ```
   
   Why is it failing to find arm-nuttx-eabi-gcc ?


-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [nuttx] pkarashchenko commented on pull request #7202: Add support for the loading of ET_DYN objects

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on PR #7202:
URL: https://github.com/apache/nuttx/pull/7202#issuecomment-1623597904

   Also style check fails.
   ```
   2023-07-06T04:46:48.5913429Z ##[error]/home/runner/work/nuttx/nuttx/nuttx/libs/libc/dlfcn/lib_dlopen.c:251:38: error: Dangling whitespace at the end of line
   2023-07-06T04:46:48.5994412Z ##[error]/home/runner/work/nuttx/nuttx/nuttx/libs/libc/machine/arm/armv7-m/arch_elf.c:499:79: error: Long line found
   ```
   Are from changes in this PR


-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [nuttx] patacongo commented on pull request #7202: Add support for the loading of ET_DYN objects

Posted by "patacongo (via GitHub)" <gi...@apache.org>.
patacongo commented on PR #7202:
URL: https://github.com/apache/nuttx/pull/7202#issuecomment-1589488380

   > Why is it failing to find arm-nuttx-eabi-gcc ?
   
   arm-nuttx-eabi-gcc is the "buildroot" toolchain (https://bitbucket.org/nuttx/buildroot/src/master/) which you probably do not have installed.
   


-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [nuttx] acassis commented on pull request #7202: Add support for the loading of ET_DYN objects

Posted by "acassis (via GitHub)" <gi...@apache.org>.
acassis commented on PR #7202:
URL: https://github.com/apache/nuttx/pull/7202#issuecomment-1593635447

   @nealef see https://github.com/apache/nuttx/actions/runs/5281758940/jobs/9555847442?pr=7202
   
   It is failing with same error I reported above! Any idea?


-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [nuttx] acassis commented on pull request #7202: Add support for the loading of ET_DYN objects

Posted by "acassis (via GitHub)" <gi...@apache.org>.
acassis commented on PR #7202:
URL: https://github.com/apache/nuttx/pull/7202#issuecomment-1623575170

   Thank you @nealef now it worked here!


-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [incubator-nuttx] nealef commented on a diff in pull request #7202: Add support for the loading of ET_DYN objects

Posted by GitBox <gi...@apache.org>.
nealef commented on code in PR #7202:
URL: https://github.com/apache/incubator-nuttx/pull/7202#discussion_r981864312


##########
libs/libc/modlib/modlib_symbols.c:
##########
@@ -55,6 +56,11 @@ struct mod_exportinfo_s
   FAR const struct symtab_s *symbol; /* Symbol info returned (if found) */
 };
 
+typedef struct {
+	uint8_t	*epName;	/* Name of global symbol */
+	void	*epAddr;	/* Address of global symbol */
+} epTable_t;
+

Review Comment:
   Done, but what's the rationale?



-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [incubator-nuttx] acassis commented on a diff in pull request #7202: Add support for the loading of ET_DYN objects

Posted by GitBox <gi...@apache.org>.
acassis commented on code in PR #7202:
URL: https://github.com/apache/incubator-nuttx/pull/7202#discussion_r982376578


##########
libs/libc/modlib/modlib_symbols.c:
##########
@@ -55,6 +56,11 @@ struct mod_exportinfo_s
   FAR const struct symtab_s *symbol; /* Symbol info returned (if found) */
 };
 
+typedef struct {
+	uint8_t	*epName;	/* Name of global symbol */
+	void	*epAddr;	/* Address of global symbol */
+} epTable_t;
+

Review Comment:
   I think the issue is because the typedef makes it more difficult to know what is that type (more opaque) and Hungarian notation (like used by FreeRTOS) is not good visually. I could be wrong here, maybe others can confirm. @xiaoxiang781216 @gregory-nutt @jerpelea ?



-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [nuttx] acassis commented on pull request #7202: Add support for the loading of ET_DYN objects

Posted by "acassis (via GitHub)" <gi...@apache.org>.
acassis commented on PR #7202:
URL: https://github.com/apache/nuttx/pull/7202#issuecomment-1593225599

   @xiaoxiang781216 I'm getting this error when compiling for sim:ostest:
   ```
   LD:  nuttx
   /usr/bin/ld: nuttx.rel: relocation R_X86_64_16 against `.data' can not be used when making a PIE object; recompile with -fPIE
   /usr/bin/ld: failed to set dynamic section sizes: bad value
   collect2: error: ld returned 1 exit status
   make[1]: *** [Makefile:368: nuttx] Error 1
   make: *** [tools/Unix.mk:527: nuttx] Error 2
   ```
   
   Please find attached the modifications I did to fix the "undefined reference to `nGlobals' and `globalTable' "
   [0001-Move-build-globals.sh-to-tools-and-add-modlib_x64_gl.zip](https://github.com/apache/nuttx/files/11759691/0001-Move-build-globals.sh-to-tools-and-add-modlib_x64_gl.zip)
   


-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [incubator-nuttx] acassis commented on pull request #7202: Add support for the loading of ET_DYN objects

Posted by GitBox <gi...@apache.org>.
acassis commented on PR #7202:
URL: https://github.com/apache/incubator-nuttx/pull/7202#issuecomment-1259875283

   Hi @nealef thank you very much for submitting it! Now we can use it as base to polish it to integrate on NuttX! This is an amazing contribution! Thank you and WildernessLab for doing it!


-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [incubator-nuttx] nealef commented on a diff in pull request #7202: Add support for the loading of ET_DYN objects

Posted by GitBox <gi...@apache.org>.
nealef commented on code in PR #7202:
URL: https://github.com/apache/incubator-nuttx/pull/7202#discussion_r981864164


##########
libs/libc/modlib/modlib_bind.c:
##########
@@ -577,24 +799,38 @@ int modlib_bind(FAR struct module_s *modp,
           continue;
         }
 
-      /* Make sure that the section is allocated.  We can't relocate
-       * sections that were not loaded into memory.
-       */
-
-      if ((loadinfo->shdr[infosec].sh_flags & SHF_ALLOC) == 0)
+      if (loadinfo->ehdr.e_type == ET_DYN) 
         {
-          continue;
-        }
+          switch (loadinfo->shdr[i].sh_type) 
+            {
+              case SHT_DYNAMIC :
+                  ret = modlib_relocatedyn(modp, loadinfo, i);
+                  break;
+              case SHT_DYNSYM :
+                  loadinfo->dsymtabidx = i;
+                  break;
+            }
+        } 
+      else
+        {
+          /* Make sure that the section is allocated.  We can't relocate
+           * sections that were not loaded into memory.
+           */
 
-      /* Process the relocations by type */
+          if ((loadinfo->shdr[i].sh_flags & SHF_ALLOC) == 0)
+                continue;

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-unsubscribe@nuttx.apache.org

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


[GitHub] [nuttx] pkarashchenko commented on pull request #7202: Add support for the loading of ET_DYN objects

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on PR #7202:
URL: https://github.com/apache/nuttx/pull/7202#issuecomment-1623595268

   @acassis I see that PR CI fails. Are we sure that changes are safe to merge?


-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [nuttx] xiaoxiang781216 commented on pull request #7202: Add support for the loading of ET_DYN objects

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on PR #7202:
URL: https://github.com/apache/nuttx/pull/7202#issuecomment-1589507928

   @acassis please ignore the warning which doesn't block the ci.


-- 
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-unsubscribe@nuttx.apache.org

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