[PATCH] make coff_compute_checksum faster

Oleg Tolmatcev oleg.tolmatcev@gmail.com
Thu Apr 27 18:24:48 GMT 2023


This makes linking faster by reading 8 MB of data at once instead of
reading 2 bytes at once. This drastically reduces the number of system
calls.

---
 bfd/coffcode.h | 52 ++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 44 insertions(+), 8 deletions(-)

diff --git a/bfd/coffcode.h b/bfd/coffcode.h
index 19a5ce18..8118ff47 100644
--- a/bfd/coffcode.h
+++ b/bfd/coffcode.h
@@ -3361,30 +3361,66 @@ coff_read_word (bfd *abfd, unsigned int
*value, unsigned int *pelength)
   return true;
 }

+static bool
+coff_read_word_from_buffer (unsigned char *b, int buf_size,
+                            unsigned int *value, unsigned int *pelength)
+{
+
+  if (buf_size < 1)
+    {
+      *value = 0;
+      return false;
+    }
+
+  if (buf_size == 1)
+    *value = (unsigned int)b[0];
+  else
+    *value = (unsigned int)(b[0] + (b[1] << 8));
+
+  *pelength += buf_size == 1 ? 1 : 2;
+
+  return true;
+}
+
 static unsigned int
 coff_compute_checksum (bfd *abfd, unsigned int *pelength)
 {
-  bool more_data;
   file_ptr filepos;
   unsigned int value;
   unsigned int total;
+  unsigned char *buf;
+  unsigned char *cur_buf;
+  int buf_size;
+  int cur_buf_size;

   total = 0;
   *pelength = 0;
-  filepos = (file_ptr) 0;
+  filepos = (file_ptr)0;
+  buf = (unsigned char *)bfd_malloc (0x800000);
+  if (buf == NULL)
+    return 0;

   do
     {
       if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
-    return 0;
+        return 0;

-      more_data = coff_read_word (abfd, &value, pelength);
-      total += value;
-      total = 0xffff & (total + (total >> 0x10));
-      filepos += 2;
+      buf_size = bfd_bread (buf, 0x800000, abfd);
+      cur_buf_size = buf_size;
+      cur_buf = buf;
+      while (cur_buf_size > 0)
+        {
+          coff_read_word_from_buffer (cur_buf, cur_buf_size, &value, pelength);
+          cur_buf += 2;
+          cur_buf_size -= 2;
+          total += value;
+          total = 0xffff & (total + (total >> 0x10));
+        }
+      filepos += buf_size;
     }
-  while (more_data);
+  while (buf_size > 0);

+  free (buf);
   return (0xffff & (total + (total >> 0x10)));
 }

-- 
2.40.0.windows.1


More information about the Binutils mailing list