[PATCH 2/3] link_map: Pull read_addrs() in file scope

tbaeder@redhat.com tbaeder@redhat.com
Tue Dec 1 08:38:53 GMT 2020


From: Timm Bäder <tbaeder@redhat.com>

Get rid of another nested function this way
---
 libdwfl/link_map.c | 114 ++++++++++++++++++++++++++-------------------
 1 file changed, 66 insertions(+), 48 deletions(-)

diff --git a/libdwfl/link_map.c b/libdwfl/link_map.c
index 5c39c631..64baaec4 100644
--- a/libdwfl/link_map.c
+++ b/libdwfl/link_map.c
@@ -237,6 +237,64 @@ release_buffer (Dwfl *dwfl,
   return result;
 }
 
+static inline bool
+read_addrs (Dwfl *dwfl,
+            Dwfl_Memory_Callback *memory_callback,
+            void *memory_callback_arg,
+            void **buffer,
+            size_t *buffer_available,
+            GElf_Addr vaddr, GElf_Addr *read_vaddr,
+            size_t n,
+            uint_fast8_t elfclass,
+            uint_fast8_t elfdata,
+            GElf_Addr *addrs /* [4] */)
+{
+  size_t nb = n * addrsize (elfclass); /* Address words -> bytes to read.  */
+
+  /* Read a new buffer if the old one doesn't cover these words.  */
+  if (buffer == NULL
+      || vaddr < *read_vaddr
+      || vaddr - (*read_vaddr) + nb > *buffer_available)
+    {
+      release_buffer (dwfl, memory_callback, memory_callback_arg,
+                      buffer, buffer_available, 0);
+
+      *read_vaddr = vaddr;
+      int segndx = INTUSE(dwfl_addrsegment) (dwfl, vaddr, NULL);
+      if (unlikely (segndx < 0)
+          || unlikely (! (*memory_callback) (dwfl, segndx,
+                                             buffer, buffer_available,
+                                             vaddr, nb, memory_callback_arg)))
+        return true;
+    }
+
+  Elf32_Addr (*a32)[n] = vaddr - (*read_vaddr) + *buffer;
+  Elf64_Addr (*a64)[n] = (void *) a32;
+
+  if (elfclass == ELFCLASS32)
+    {
+      if (elfdata == ELFDATA2MSB)
+        for (size_t i = 0; i < n; ++i)
+          addrs[i] = BE32 (read_4ubyte_unaligned_noncvt (&(*a32)[i]));
+      else
+        for (size_t i = 0; i < n; ++i)
+          addrs[i] = LE32 (read_4ubyte_unaligned_noncvt (&(*a32)[i]));
+    }
+  else
+    {
+      if (elfdata == ELFDATA2MSB)
+        for (size_t i = 0; i < n; ++i)
+          addrs[i] = BE64 (read_8ubyte_unaligned_noncvt (&(*a64)[i]));
+      else
+        for (size_t i = 0; i < n; ++i)
+          addrs[i] = LE64 (read_8ubyte_unaligned_noncvt (&(*a64)[i]));
+    }
+
+  return false;
+}
+
+
+
 /* Report a module for each struct link_map in the linked list at r_map
    in the struct r_debug at R_DEBUG_VADDR.  For r_debug_info description
    see dwfl_link_map_report in libdwflP.h.  If R_DEBUG_INFO is not NULL then no
@@ -262,54 +320,12 @@ report_r_debug (uint_fast8_t elfclass, uint_fast8_t elfdata,
   void *buffer = NULL;
   size_t buffer_available = 0;
 
-  GElf_Addr addrs[4];
-  inline bool read_addrs (GElf_Addr vaddr, size_t n)
-  {
-    size_t nb = n * addrsize (elfclass); /* Address words -> bytes to read.  */
-
-    /* Read a new buffer if the old one doesn't cover these words.  */
-    if (buffer == NULL
-	|| vaddr < read_vaddr
-	|| vaddr - read_vaddr + nb > buffer_available)
-      {
-	release_buffer (dwfl, memory_callback, memory_callback_arg,
-                        &buffer, &buffer_available, 0);
-
-	read_vaddr = vaddr;
-	int segndx = INTUSE(dwfl_addrsegment) (dwfl, vaddr, NULL);
-	if (unlikely (segndx < 0)
-	    || unlikely (! (*memory_callback) (dwfl, segndx,
-					       &buffer, &buffer_available,
-					       vaddr, nb, memory_callback_arg)))
-	  return true;
-      }
-
-    Elf32_Addr (*a32)[n] = vaddr - read_vaddr + buffer;
-    Elf64_Addr (*a64)[n] = (void *) a32;
-
-    if (elfclass == ELFCLASS32)
-      {
-	if (elfdata == ELFDATA2MSB)
-	  for (size_t i = 0; i < n; ++i)
-	    addrs[i] = BE32 (read_4ubyte_unaligned_noncvt (&(*a32)[i]));
-	else
-	  for (size_t i = 0; i < n; ++i)
-	    addrs[i] = LE32 (read_4ubyte_unaligned_noncvt (&(*a32)[i]));
-      }
-    else
-      {
-	if (elfdata == ELFDATA2MSB)
-	  for (size_t i = 0; i < n; ++i)
-	    addrs[i] = BE64 (read_8ubyte_unaligned_noncvt (&(*a64)[i]));
-	else
-	  for (size_t i = 0; i < n; ++i)
-	    addrs[i] = LE64 (read_8ubyte_unaligned_noncvt (&(*a64)[i]));
-      }
-
-    return false;
-  }
+  GElf_Addr addrs[4] = { 0, 0, 0, 0 };
 
-  if (unlikely (read_addrs (read_vaddr, 1)))
+  if (unlikely (read_addrs (dwfl, memory_callback, memory_callback_arg,
+                            &buffer, &buffer_available,
+                            read_vaddr, &read_vaddr, 1,
+                            elfclass, elfdata, addrs)))
     release_buffer (dwfl, memory_callback, memory_callback_arg,
                     &buffer, &buffer_available, -1);
 
@@ -326,7 +342,9 @@ report_r_debug (uint_fast8_t elfclass, uint_fast8_t elfdata,
   size_t iterations = 0;
   while (next != 0 && ++iterations < dwfl->lookup_elts)
     {
-      if (read_addrs (next, 4))
+      if (read_addrs (dwfl, memory_callback, memory_callback_arg,
+                      &buffer, &buffer_available,
+                      next, &read_vaddr, 4, elfclass, elfdata, addrs))
         return release_buffer (dwfl, memory_callback, memory_callback_arg,
                                &buffer, &buffer_available, -1);
 
-- 
2.26.2



More information about the Elfutils-devel mailing list