[binutils-gdb] ld: Use correct types for crc64 calculations

Nick Clifton nickc@sourceware.org
Wed Mar 8 13:11:46 GMT 2023


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=9a534b9f8e3d0f3cdb5a20f19ff165693fbb84d2

commit 9a534b9f8e3d0f3cdb5a20f19ff165693fbb84d2
Author: Nick Clifton <nickc@redhat.com>
Date:   Wed Mar 8 13:11:37 2023 +0000

    ld: Use correct types for crc64 calculations

Diff:
---
 ld/ld.texi                           |  4 +++
 ld/lddigest.c                        | 52 +++++++++++++++++++++++-------------
 ld/lddigest.h                        | 50 +++++++++++++++++++---------------
 ld/ldgram.y                          |  4 +--
 ld/testsuite/ld-scripts/crc64-poly.d |  2 +-
 ld/testsuite/ld-scripts/crc64-poly.t |  2 +-
 6 files changed, 70 insertions(+), 44 deletions(-)

diff --git a/ld/ld.texi b/ld/ld.texi
index c21d849fe24..207ac98126b 100644
--- a/ld/ld.texi
+++ b/ld/ld.texi
@@ -5581,6 +5581,10 @@ The parameters are explained in detail in
 
 Some of the predefined polynomes are the same, but differs in the other
 parameters.
+
+Note - the generation of 64-bit polynomes on 32-bit hosts for 32-bit
+targets is not supported.
+
 @page
 The 32-bit <polynome> command defines the following global symbols.
 
diff --git a/ld/lddigest.c b/ld/lddigest.c
index d0bb4db73ab..a2d7460e56a 100644
--- a/ld/lddigest.c
+++ b/ld/lddigest.c
@@ -115,7 +115,7 @@ lang_add_crc32_table (bool big_endian)
 	}
       local_table = true;
     }
-  for (bfd_vma i = 0; i < 256; i++)
+  for (int i = 0; i < 256; i++)
     {
       uint32_t elem = crc32_table[i];
       if (big_endian)
@@ -137,7 +137,10 @@ lang_add_crc64_syndrome (algorithm_desc_t * a)
   CRC_START = CRC64_START;
   CRC_END = CRC64_END;
   CRC_TABLE = CRC64_TABLE;
-  lang_add_data (QUAD, exp_intop (0));	/* Reserve room for the ECC value */
+
+  /* Reserve room for the ECC value.  */
+  lang_add_data (QUAD, exp_intop (0));
+
   a->crc_tab = init_crc64_tab (a);
   if (a->crc_tab == NULL)
     {
@@ -181,10 +184,13 @@ print_hash64_table (algorithm_desc_t * a)
 static void
 lang_add_crc64_table (bool big_endian)
 {
-  bfd_vma *crc64_table = algorithm.crc_tab;	/* Use a precomputed, if it exists */
+  /* Use a precomputed table, if one exists.  */
+  uint64_t *crc64_table = algorithm.crc_tab;
   bool local_table = false;
+
   if (crc64_table == NULL)
-    {				/* No luck, create a table */
+    {
+      /* No luck, create a table.  */
       crc64_table = init_crc64_tab (&algorithm);
       if (crc64_table == NULL)
 	{
@@ -193,21 +199,28 @@ lang_add_crc64_table (bool big_endian)
 	}
       local_table = true;
     }
+
   print_hash64_table (&algorithm);
-  for (bfd_vma i = 0; i < 256; i++)
+
+  for (int i = 0; i < 256; i++)
     {
-      bfd_vma elem = crc64_table[i];
+      uint64_t elem = crc64_table[i];
+
       if (big_endian)
-	{
-	  elem = __builtin_bswap64 (elem);
-	}
+	elem = __builtin_bswap64 (elem);
+
       if (link_info.big_endian)
+	elem = __builtin_bswap64 (elem);
+
+      if (sizeof (bfd_vma) >= QUAD_SIZE)
+	lang_add_data (QUAD, exp_intop (elem));
+      else
 	{
-	  elem = __builtin_bswap64 (elem);
+	  lang_add_data (LONG, exp_intop (elem >> 32));
+	  lang_add_data (LONG, exp_intop (elem));
 	}
-
-      lang_add_data (QUAD, exp_intop (elem));
     }
+
   if (local_table)
     free (crc64_table);
 }
@@ -219,7 +232,9 @@ lang_add_digest (bfd_vma size,
 		 bfd_vma poly,
 		 bfd_vma initial,
 		 bfd_vma xor_val,
-		 bfd_vma ireflect, bfd_vma oreflect, bfd_vma reciprocal)
+		 bfd_vma ireflect,
+		 bfd_vma oreflect,
+		 bfd_vma reciprocal)
 {
   if (algorithm.crc_algo != no_algo)	/* We only allow one CRC <polynom> */
     {
@@ -578,7 +593,7 @@ get_text_section_contents (void)
 				   text_section->output_section,
 				   (bfd_byte **) & text_contents))
     {
-      einfo (_("%X%P: '&s' section contents unavailable\n"
+      einfo (_("%X%P: '%s' section contents unavailable\n"
 	       "CRC generation aborted\n"), digest_section);
       return false;
     }
@@ -660,7 +675,7 @@ set_crc64_checksum (uint64_t crc, bfd_vma addr)
 }
 
 static bool
-set_crc_checksum (bfd_vma crc, bfd_vma addr, bfd_vma size)
+set_crc_checksum (uint64_t crc, bfd_vma addr, bfd_vma size)
 {
   bool status;
   if (size == 64)
@@ -700,7 +715,7 @@ symbol_lookup (char *name, bfd_vma * val)
  * Multiplexing function for calculating CRC with different algorithms
  * 'algorithm.crc_algo'
  */
-static bfd_vma
+static uint64_t
 calculate_crc (const unsigned char *input_str, size_t num_bytes)
 {
   if (algorithm.crc_algo == crc_algo_64)
@@ -723,7 +738,8 @@ calculate_crc (const unsigned char *input_str, size_t num_bytes)
 
 static bool
 invalid_crc_parameters (bfd_vma crc_addr,
-			bfd_vma crc_area_start, bfd_vma crc_area_end)
+			bfd_vma crc_area_start,
+			bfd_vma crc_area_end)
 {
   bool crc_in_section;
   bfd_vma crc_size = algorithm.crc_size / 8;
@@ -782,7 +798,7 @@ void
 lang_generate_crc (void)
 {
   bfd_vma crc_addr, crc_area_start, crc_area_end;
-  bfd_vma crc;
+  uint64_t crc;
   bool can_do_crc;
 
   /* Return immediately, if CRC is not requested */
diff --git a/ld/lddigest.h b/ld/lddigest.h
index 9f5e5f3fbda..a1eeb022d00 100755
--- a/ld/lddigest.h
+++ b/ld/lddigest.h
@@ -156,28 +156,34 @@ extern const char *digest_label;
 extern bool digest_big_endian;
 extern bool polynome_valid;
 
-/* CRC-32 */
-extern uint32_t *init_crc32_tab (algorithm_desc_t * dsc);
+/* In ldcrc32.c.  */
+extern uint32_t * init_crc32_tab
+  (algorithm_desc_t *);
 extern uint32_t calc_crc32
-  (algorithm_desc_t * dsc, const unsigned char *input_str, size_t num_bytes);
-extern void lang_add_crc32_syndrome (algorithm_desc_t * a);
-
-/* CR-64 */
-extern bfd_vma *init_crc64_tab (algorithm_desc_t * dsc);
-extern bfd_vma calc_crc64
-  (algorithm_desc_t * dsc, const unsigned char *input_str, size_t num_bytes);
-extern void lang_add_crc64_syndrome (algorithm_desc_t * a);
-
-extern void lang_add_digest (bfd_vma size,
-			     bfd_vma poly,
-			     bfd_vma initial,
-			     bfd_vma xor_val,
-			     bfd_vma ireflect,
-			     bfd_vma oreflect, bfd_vma reciprocal);
-extern bool lang_set_digest (char *digest);
-extern void lang_add_digest_table (bool big_endian);
-extern const char *lang_get_label (const char *label, bool *big_endian);
-extern void lang_generate_crc (void);
-extern void lang_generate_digest (void);
+  (algorithm_desc_t *, const unsigned char *, size_t);
+extern void lang_add_crc32_syndrome
+  (algorithm_desc_t *);
+
+/* In ldcrc64.c.  */
+extern uint64_t * init_crc64_tab
+  (algorithm_desc_t *);
+extern uint64_t calc_crc64
+  (algorithm_desc_t *, const unsigned char *, size_t);
+extern void lang_add_crc64_syndrome
+  (algorithm_desc_t * );
+
+/* In lddigest.c  */
+extern void lang_add_digest
+  (bfd_vma, bfd_vma, bfd_vma, bfd_vma, bfd_vma, bfd_vma, bfd_vma);
+extern bool lang_set_digest
+  (char *);
+extern void lang_add_digest_table
+  (bool);
+extern const char * lang_get_label
+  (const char *, bool *);
+extern void lang_generate_crc
+  (void);
+extern void lang_generate_digest
+  (void);
 
 #endif /* LDDIGEST_H */
diff --git a/ld/ldgram.y b/ld/ldgram.y
index ea0c569279a..54bf8e0f908 100644
--- a/ld/ldgram.y
+++ b/ld/ldgram.y
@@ -691,7 +691,7 @@ statement:
 		}
 	| DIGEST NAME
 		{ /* CRC_ADDRESS is set in <polynome>, but polynome reserves space, so we use a temporary */
-		  digest_label = lang_get_label($2, &digest_big_endian);
+		  digest_label = lang_get_label ($2, &digest_big_endian);
 		  lang_add_assignment (exp_assign (digest_label, exp_nameop (NAME, "."), false));
 		}
 		polynome '(' mustbe_exp ',' mustbe_exp ')'
@@ -740,7 +740,7 @@ statement:
 polynome:
 	NAME
 		{
-		  polynome_valid = lang_set_digest($1);
+		  polynome_valid = lang_set_digest ($1);
 		}
 	| POLY '(' mustbe_exp ','
 		   mustbe_exp ',' mustbe_exp ',' mustbe_exp ','
diff --git a/ld/testsuite/ld-scripts/crc64-poly.d b/ld/testsuite/ld-scripts/crc64-poly.d
index b54ac47e448..a1e6eb8f150 100644
--- a/ld/testsuite/ld-scripts/crc64-poly.d
+++ b/ld/testsuite/ld-scripts/crc64-poly.d
@@ -9,7 +9,7 @@
 Contents of section .text:
  1200 434f4445 deadbeef 00000000 00000000  CODE............
  1210 6c40df5f 0b497347 00000000 00000000  l@._.IsG........
- 1220 6c40df5f 0b497347 00000000 00000000  l@._.IsG........
+ 1220 ee5e1ecd 02f31206 00000000 00000000  ................
  1230 00000000 00000000 deadbeef 434f4445  ............CODE
  1240 31323334 35363738 3900ffff ffffffff  123456789.......
  1250 434f4445 00000000 00000000 00000000  CODE............
diff --git a/ld/testsuite/ld-scripts/crc64-poly.t b/ld/testsuite/ld-scripts/crc64-poly.t
index fb357caedf9..00a21557827 100644
--- a/ld/testsuite/ld-scripts/crc64-poly.t
+++ b/ld/testsuite/ld-scripts/crc64-poly.t
@@ -26,7 +26,7 @@ SECTIONS
       QUAD(0x0);
 
       crc64 = .;
-      DIGEST "_CRC64#BE" POLY(64,0x42F0E1EBA9EA3693,0,0,0,0,0)(ecc_start , ecc_end)
+      DIGEST "_CRC64#BE" POLY(64,0xA9EA3693,0,0,0,0,0)(ecc_start , ecc_end)
       QUAD(0x0);
 
       INCLUDE "end_tag.inc"


More information about the Binutils-cvs mailing list