You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "g2gps (via GitHub)" <gi...@apache.org> on 2023/05/26 05:55:48 UTC

[GitHub] [nuttx] g2gps opened a new pull request, #9395: binfmt/elf: Support loading fully linked executables.

g2gps opened a new pull request, #9395:
URL: https://github.com/apache/nuttx/pull/9395

   ## Summary
   
   The following changes make it possible for Nuttx to load binaries in ELF format which are fully linked.
   
   The change does not include the necessary modifications to produce such binaries. In order to build an applicable binary:
    - The userspace applications linker script (`gnu-elf.ld`) needs to modified so the data and text section origin's match those setup by the address environment.
    - The makefile used, in `apps/import/Make.defs` needs to remove the `-r` LDELFFLAG.
   
   ## Impact
   
    - No measurable impact on existing application which load relocatable ELFs.
    - Massive improvement in launch time when loading fully linked ELFs, as there's no longer any need to process relocations. 
    
   ## Testing
   
   Using existing configurations and relocatable ELFs:
    - sim:elf
    - rv-virt:knsh32
   
   Using fully linked ELFs requires modifications as outlined above. For testing on rv-virt:knsh32:
   
   Changes to Nuttx:
   ```diff
   diff --git a/binfmt/libelf/gnu-elf.ld b/binfmt/libelf/gnu-elf.ld
   index 81928663ee..9b03039e3f 100644
   --- a/binfmt/libelf/gnu-elf.ld
   +++ b/binfmt/libelf/gnu-elf.ld
   @@ -20,7 +20,8 @@
    
    SECTIONS
    {
   -  .text 0x00000000 :
   +  . = 0xC0000000;
   +  .text :
        {
          _stext = . ;
          *(.text)
   @@ -51,6 +52,7 @@ SECTIONS
          _erodata = . ;
        }
    
   +  . = 0xC0401000;
      .data :
        {
          _sdata = . ;
   diff --git a/boards/risc-v/qemu-rv/rv-virt/scripts/Make.defs b/boards/risc-v/qemu-rv/rv-virt/scripts/Make.defs
   index ab13f19b9c..4511054841 100644
   --- a/boards/risc-v/qemu-rv/rv-virt/scripts/Make.defs
   +++ b/boards/risc-v/qemu-rv/rv-virt/scripts/Make.defs
   @@ -57,5 +57,5 @@ else
      LDELFFLAGS = --oformat elf64-littleriscv
    endif
    
   -LDELFFLAGS += -r -e main
   +LDELFFLAGS += -e main
    LDELFFLAGS += -T $(call CONVERT_PATH,$(TOPDIR)/binfmt/libelf/gnu-elf.ld)
   
   ```
   
   Changes to apps:
   ```diff
   diff --git a/import/Make.defs b/import/Make.defs
   index b8377f79a..b97b260a2 100644
   --- a/import/Make.defs
   +++ b/import/Make.defs
   @@ -90,5 +90,5 @@ endif
    
    # ELF module definitions
    
   -LDELFFLAGS += -r -e _start -Bstatic
   +LDELFFLAGS += -s -e _start -Bstatic
    LDELFFLAGS += $(addprefix -T,$(call CONVERT_PATH,$(TOPDIR)/scripts/gnu-elf.ld))
   ```
   
   As can be seen, the required changes are board, and configuration specific. It may be possible to provide the infrastructure in NuttX (and apps) to build fully-linked applications, picking up the necessary configuration values from the board's directory. However, this would be quite a large change, and may need to be addressed separately.
   
   For our current application I'm building NuttX native applications as relocatables and our custom applications  fully linked against Nuttx export with a custom Makefile and linker script. Loading both ELF types simultaneously works without issues.
   
   


-- 
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 a diff in pull request #9395: binfmt/elf: Support loading fully linked executables.

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


##########
binfmt/elf.c:
##########
@@ -247,16 +247,40 @@ static int elf_loadbinary(FAR struct binary_s *binp,
 
   /* Bind the program to the exported symbol table */
 
-  ret = elf_bind(&loadinfo, exports, nexports);
-  if (ret != 0)
+  if (loadinfo.ehdr.e_type == ET_REL)
+    {
+      ret = elf_bind(&loadinfo, exports, nexports);
+      if (ret != 0)
+        {
+          berr("Failed to bind symbols program binary: %d\n", ret);
+          goto errout_with_load;
+        }
+
+      binp->entrypt = (main_t)(loadinfo.textalloc + loadinfo.ehdr.e_entry);
+    }
+  else if (loadinfo.ehdr.e_type == ET_EXEC)
+    {
+      if (nexports > 0)
+        {
+          berr("Cannot bind exported symbols to a "\
+                                    "fully linked executable\n");
+          ret = -ENOEXEC;
+          goto errout_with_load;
+        }
+
+      /* The entrypoint for a fully linked executable can be found directly */
+
+      binp->entrypt = (main_t)(loadinfo.ehdr.e_entry);
+    }
+
+  else
     {
-      berr("Failed to bind symbols program binary: %d\n", ret);
-      goto errout_with_load;
+        berr("Unexpected elf type %d\n", loadinfo.ehdr.e_type);
+        ret = -ENOEXEC;

Review Comment:
   This style change is wrong



##########
binfmt/libelf/libelf_verify.c:
##########
@@ -89,9 +89,10 @@ int elf_verifyheader(FAR const Elf_Ehdr *ehdr)
 
   /* Verify that this is a relocatable file */
 
-  if (ehdr->e_type != ET_REL)
+  if ((ehdr->e_type != ET_REL) && (ehdr->e_type != ET_EXEC))
     {
-      berr("Not a relocatable file: e_type=%d\n", ehdr->e_type);
+      berr("Not a relocatable or executable file: e_type=%d\n",
+                                        ehdr->e_type);

Review Comment:
   Need to align `ehdr->e_type` with upper`("`



##########
binfmt/elf.c:
##########
@@ -247,16 +247,40 @@ static int elf_loadbinary(FAR struct binary_s *binp,
 
   /* Bind the program to the exported symbol table */
 
-  ret = elf_bind(&loadinfo, exports, nexports);
-  if (ret != 0)
+  if (loadinfo.ehdr.e_type == ET_REL)
+    {
+      ret = elf_bind(&loadinfo, exports, nexports);
+      if (ret != 0)
+        {
+          berr("Failed to bind symbols program binary: %d\n", ret);
+          goto errout_with_load;
+        }
+
+      binp->entrypt = (main_t)(loadinfo.textalloc + loadinfo.ehdr.e_entry);
+    }
+  else if (loadinfo.ehdr.e_type == ET_EXEC)
+    {
+      if (nexports > 0)
+        {
+          berr("Cannot bind exported symbols to a "\
+                                    "fully linked executable\n");

Review Comment:
   ```
   berr("Cannot bind exported symbols to a "
        "fully linked executable\n");
   ```



-- 
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 #9395: binfmt/elf: Support loading fully linked executables.

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


-- 
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