Lose some COFF/PE static vars, and peicode.h constify

Alan Modra amodra@gmail.com
Wed Dec 16 04:43:31 GMT 2020


This patch tidies some COFF and PE code that unnecessarily used static
variables to communicate between functions.

	* coffcode.h (pelength, peheader): Delete static variables.
	(coff_apply_checksum): Instead, define them as auto vars, and pass..
	(coff_read_word, coff_compute_checksum): ..to here.  Delete
	unnecessary forward declarations.
	* pei-x86_64.c (pdata_count): Delete static variable.
	(struct pex64_paps): New.
	(pex64_print_all_pdata_sections, pex64_bfd_print_pdata): Pass
	a pex64_paps for counting.
	* peicode.h (jtab): Constify.

diff --git a/bfd/coffcode.h b/bfd/coffcode.h
index 4b934b9b2b..2a0eeedcd7 100644
--- a/bfd/coffcode.h
+++ b/bfd/coffcode.h
@@ -445,14 +445,6 @@ static void * coff_mkobject_hook
 static flagword handle_COMDAT
   (bfd *, flagword, void *, const char *, asection *);
 #endif
-#ifdef COFF_IMAGE_WITH_PE
-static bfd_boolean coff_read_word
-  (bfd *, unsigned int *);
-static unsigned int coff_compute_checksum
-  (bfd *);
-static bfd_boolean coff_apply_checksum
-  (bfd *);
-#endif
 #ifdef TICOFF
 static bfd_boolean ticoff0_bad_format_hook
   (bfd *, void * );
@@ -3274,11 +3266,8 @@ coff_compute_section_file_positions (bfd * abfd)
 
 #ifdef COFF_IMAGE_WITH_PE
 
-static unsigned int pelength;
-static unsigned int peheader;
-
 static bfd_boolean
-coff_read_word (bfd *abfd, unsigned int *value)
+coff_read_word (bfd *abfd, unsigned int *value, unsigned int *pelength)
 {
   unsigned char b[2];
   int status;
@@ -3295,13 +3284,13 @@ coff_read_word (bfd *abfd, unsigned int *value)
   else
     *value = (unsigned int) (b[0] + (b[1] << 8));
 
-  pelength += (unsigned int) status;
+  *pelength += status;
 
   return TRUE;
 }
 
 static unsigned int
-coff_compute_checksum (bfd *abfd)
+coff_compute_checksum (bfd *abfd, unsigned int *pelength)
 {
   bfd_boolean more_data;
   file_ptr filepos;
@@ -3309,7 +3298,7 @@ coff_compute_checksum (bfd *abfd)
   unsigned int total;
 
   total = 0;
-  pelength = 0;
+  *pelength = 0;
   filepos = (file_ptr) 0;
 
   do
@@ -3317,7 +3306,7 @@ coff_compute_checksum (bfd *abfd)
       if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
 	return 0;
 
-      more_data = coff_read_word (abfd, &value);
+      more_data = coff_read_word (abfd, &value, pelength);
       total += value;
       total = 0xffff & (total + (total >> 0x10));
       filepos += 2;
@@ -3332,11 +3321,13 @@ coff_apply_checksum (bfd *abfd)
 {
   unsigned int computed;
   unsigned int checksum = 0;
+  unsigned int peheader;
+  unsigned int pelength;
 
   if (bfd_seek (abfd, 0x3c, SEEK_SET) != 0)
     return FALSE;
 
-  if (!coff_read_word (abfd, &peheader))
+  if (!coff_read_word (abfd, &peheader, &pelength))
     return FALSE;
 
   if (bfd_seek (abfd, peheader + 0x58, SEEK_SET) != 0)
@@ -3348,7 +3339,7 @@ coff_apply_checksum (bfd *abfd)
   if (bfd_seek (abfd, peheader, SEEK_SET) != 0)
     return FALSE;
 
-  computed = coff_compute_checksum (abfd);
+  computed = coff_compute_checksum (abfd, &pelength);
 
   checksum = computed + pelength;
 
diff --git a/bfd/pei-x86_64.c b/bfd/pei-x86_64.c
index 7af0d49e3a..b1259a0d9e 100644
--- a/bfd/pei-x86_64.c
+++ b/bfd/pei-x86_64.c
@@ -820,20 +820,25 @@ pex64_bfd_print_pdata_section (bfd *abfd, void *vfile, asection *pdata_section)
   return TRUE;
 }
 
-/* Static counter of number of found pdata sections.  */
-static bfd_boolean pdata_count;
+struct pex64_paps
+{
+  void *obj;
+  /* Number of found pdata sections.  */
+  unsigned int pdata_count;
+};
 
 /* Functionn prototype.  */
 bfd_boolean pex64_bfd_print_pdata (bfd *, void *);
 
 /* Helper function for bfd_map_over_section.  */
 static void
-pex64_print_all_pdata_sections (bfd *abfd, asection *pdata, void *obj)
+pex64_print_all_pdata_sections (bfd *abfd, asection *pdata, void *arg)
 {
+  struct pex64_paps *paps = arg;
   if (CONST_STRNEQ (pdata->name, ".pdata"))
     {
-      if (pex64_bfd_print_pdata_section (abfd, obj, pdata))
-	pdata_count++;
+      if (pex64_bfd_print_pdata_section (abfd, paps->obj, pdata))
+	paps->pdata_count++;
     }
 }
 
@@ -841,13 +846,15 @@ bfd_boolean
 pex64_bfd_print_pdata (bfd *abfd, void *vfile)
 {
   asection *pdata_section = bfd_get_section_by_name (abfd, ".pdata");
+  struct pex64_paps paps;
 
   if (pdata_section)
     return pex64_bfd_print_pdata_section (abfd, vfile, pdata_section);
 
-  pdata_count = 0;
-  bfd_map_over_sections (abfd, pex64_print_all_pdata_sections, vfile);
-  return (pdata_count > 0);
+  paps.obj = vfile;
+  paps.pdata_count = 0;
+  bfd_map_over_sections (abfd, pex64_print_all_pdata_sections, &paps);
+  return paps.pdata_count != 0;
 }
 
 #define bfd_pe_print_pdata   pex64_bfd_print_pdata
diff --git a/bfd/peicode.h b/bfd/peicode.h
index f7d2b5f5f5..27a156fbdf 100644
--- a/bfd/peicode.h
+++ b/bfd/peicode.h
@@ -699,7 +699,7 @@ typedef struct
 }
 jump_table;
 
-static jump_table jtab[] =
+static const jump_table jtab[] =
 {
 #ifdef I386MAGIC
   { I386MAGIC,

-- 
Alan Modra
Australia Development Lab, IBM


More information about the Binutils mailing list