You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by pk...@apache.org on 2023/07/16 11:17:13 UTC

[nuttx] 01/02: binfmt: Move elf_allocbuffer to elf_sectname and elf_symname

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

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

commit 879dab08a6310b309689e7b6b668a323bc2564df
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sat Jul 15 22:11:06 2023 +0800

    binfmt: Move elf_allocbuffer to elf_sectname and elf_symname
    
    it's better to allocate the buffer just before really use it.
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 binfmt/libelf/libelf_bind.c     | 11 -----------
 binfmt/libelf/libelf_ctors.c    | 11 -----------
 binfmt/libelf/libelf_dtors.c    | 11 -----------
 binfmt/libelf/libelf_sections.c | 11 +++++++++++
 binfmt/libelf/libelf_symbols.c  | 11 +++++++++++
 5 files changed, 22 insertions(+), 33 deletions(-)

diff --git a/binfmt/libelf/libelf_bind.c b/binfmt/libelf/libelf_bind.c
index f09eb00b17..564e53f0e1 100644
--- a/binfmt/libelf/libelf_bind.c
+++ b/binfmt/libelf/libelf_bind.c
@@ -572,17 +572,6 @@ int elf_bind(FAR struct elf_loadinfo_s *loadinfo,
       return ret;
     }
 
-  /* Allocate an I/O buffer.  This buffer is used by elf_symname() to
-   * accumulate the variable length symbol name.
-   */
-
-  ret = elf_allocbuffer(loadinfo);
-  if (ret < 0)
-    {
-      berr("elf_allocbuffer failed: %d\n", ret);
-      return ret;
-    }
-
 #ifdef CONFIG_ARCH_ADDRENV
   /* If CONFIG_ARCH_ADDRENV=y, then the loaded ELF lies in a virtual address
    * space that may not be in place now.  elf_addrenv_select() will
diff --git a/binfmt/libelf/libelf_ctors.c b/binfmt/libelf/libelf_ctors.c
index a9c772749e..8722df4893 100644
--- a/binfmt/libelf/libelf_ctors.c
+++ b/binfmt/libelf/libelf_ctors.c
@@ -82,17 +82,6 @@ int elf_loadctors(FAR struct elf_loadinfo_s *loadinfo)
 
   DEBUGASSERT(loadinfo->ctors == NULL);
 
-  /* Allocate an I/O buffer if necessary.  This buffer is used by
-   * elf_sectname() to accumulate the variable length symbol name.
-   */
-
-  ret = elf_allocbuffer(loadinfo);
-  if (ret < 0)
-    {
-      berr("elf_allocbuffer failed: %d\n", ret);
-      return -ENOMEM;
-    }
-
   /* Find the index to the section named ".ctors."  NOTE:  On old ABI system,
    * .ctors is the name of the section containing the list of constructors;
    * On newer systems, the similar section is called .init_array.  It is
diff --git a/binfmt/libelf/libelf_dtors.c b/binfmt/libelf/libelf_dtors.c
index 9f6ce0a243..2f50274099 100644
--- a/binfmt/libelf/libelf_dtors.c
+++ b/binfmt/libelf/libelf_dtors.c
@@ -83,17 +83,6 @@ int elf_loaddtors(FAR struct elf_loadinfo_s *loadinfo)
 
   DEBUGASSERT(loadinfo->dtors == NULL);
 
-  /* Allocate an I/O buffer if necessary.  This buffer is used by
-   * elf_sectname() to accumulate the variable length symbol name.
-   */
-
-  ret = elf_allocbuffer(loadinfo);
-  if (ret < 0)
-    {
-      berr("elf_allocbuffer failed: %d\n", ret);
-      return -ENOMEM;
-    }
-
   /* Find the index to the section named ".dtors."  NOTE:  On old ABI system,
    * .dtors is the name of the section containing the list of destructors;
    * On newer systems, the similar section is called .fini_array.  It is
diff --git a/binfmt/libelf/libelf_sections.c b/binfmt/libelf/libelf_sections.c
index 94d503c51a..50bb79c0b2 100644
--- a/binfmt/libelf/libelf_sections.c
+++ b/binfmt/libelf/libelf_sections.c
@@ -82,6 +82,17 @@ static inline int elf_sectname(FAR struct elf_loadinfo_s *loadinfo,
       return -EINVAL;
     }
 
+  /* Allocate an I/O buffer if necessary.  This buffer is used by
+   * elf_sectname() to accumulate the variable length symbol name.
+   */
+
+  ret = elf_allocbuffer(loadinfo);
+  if (ret < 0)
+    {
+      berr("elf_allocbuffer failed: %d\n", ret);
+      return ret;
+    }
+
   /* Get the section name string table section header */
 
   shstr = &loadinfo->shdr[shstrndx];
diff --git a/binfmt/libelf/libelf_symbols.c b/binfmt/libelf/libelf_symbols.c
index 70b20b911f..8c3557ab9d 100644
--- a/binfmt/libelf/libelf_symbols.c
+++ b/binfmt/libelf/libelf_symbols.c
@@ -82,6 +82,17 @@ static int elf_symname(FAR struct elf_loadinfo_s *loadinfo,
       return -ESRCH;
     }
 
+  /* Allocate an I/O buffer.  This buffer is used by elf_symname() to
+   * accumulate the variable length symbol name.
+   */
+
+  ret = elf_allocbuffer(loadinfo);
+  if (ret < 0)
+    {
+      berr("elf_allocbuffer failed: %d\n", ret);
+      return ret;
+    }
+
   offset = loadinfo->shdr[loadinfo->strtabidx].sh_offset + sym->st_name;
 
   /* Loop until we get the entire symbol name into memory */