History

genksyms: use uint32_t instead of unsigned long for calculating CRC

Currently, 'unsigned long' is used for intermediate variables when calculating CRCs. The size of 'long' differs depending on the architecture: it is 32 bits on 32-bit architectures and 64 bits on 64-bit architectures. The CRC values generated by genksyms represent the compatibility of exported symbols. Therefore, reproducibility is important. In other words, we need to ensure that the output is the same when the kernel source is identical, regardless of whether genksyms is running on a 32-bit or 64-bit build machine. Fortunately, the output from genksyms is not affected by the build machine's architecture because only the lower 32 bits of the 'unsigned long' variables are used. To make it even clearer that the CRC calculation is independent of the build machine's architecture, this commit explicitly uses the fixed-width type, uint32_t.

Masahiro Yamada committed 1 year ago

Commit a56fece1 parent 2759bd91 file · +8 −7Browse files

Showing the changes to scripts (1 of 1 file). Show all

Unified · Split

scripts/genksyms/genksyms.c+8−7
@@ -12,6 +12,7 @@ · 11 unmodified lines
12 12
13#include <stdio.h>13#include <stdio.h>
14#include <string.h>14#include <string.h>
15#include <stdint.h>
15#include <stdlib.h>16#include <stdlib.h>
16#include <unistd.h>17#include <unistd.h>
17#include <assert.h>18#include <assert.h>
@@ -60,7 +61,7 @@ static void print_type_name(enum symbol_type type, const char *name); · 42 unmodified lines
60 61
61/*----------------------------------------------------------------------*/62/*----------------------------------------------------------------------*/
62 63
63static const unsigned int crctab32[] = {64static const uint32_t crctab32[] = {
64 0x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U,65 0x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U,
65 0x706af48fU, 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U,66 0x706af48fU, 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U,
66 0xe0d5e91eU, 0x97d2d988U, 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U,67 0xe0d5e91eU, 0x97d2d988U, 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U,
@@ -115,19 +116,19 @@ static const unsigned int crctab32[] = { · 48 unmodified lines
115 0x2d02ef8dU116 0x2d02ef8dU
116};117};
117 118
118static unsigned long partial_crc32_one(unsigned char c, unsigned long crc)119static uint32_t partial_crc32_one(uint8_t c, uint32_t crc)
119{120{
120 return crctab32[(crc ^ c) & 0xff] ^ (crc >> 8);121 return crctab32[(crc ^ c) & 0xff] ^ (crc >> 8);
121}122}
122 123
123static unsigned long partial_crc32(const char *s, unsigned long crc)124static uint32_t partial_crc32(const char *s, uint32_t crc)
124{125{
125 while (*s)126 while (*s)
126 crc = partial_crc32_one(*s++, crc);127 crc = partial_crc32_one(*s++, crc);
127 return crc;128 return crc;
128}129}
129 130
130static unsigned long crc32(const char *s)131static uint32_t crc32(const char *s)
131{132{
132 return partial_crc32(s, 0xffffffff) ^ 0xffffffff;133 return partial_crc32(s, 0xffffffff) ^ 0xffffffff;
133}134}
@@ -517,7 +518,7 @@ static void print_list(FILE * f, struct string_list *list) · 383 unmodified lines
517 }518 }
518}519}
519 520
520static unsigned long expand_and_crc_sym(struct symbol *sym, unsigned long crc)521static uint32_t expand_and_crc_sym(struct symbol *sym, uint32_t crc)
521{522{
522 struct string_list *list = sym->defn;523 struct string_list *list = sym->defn;
523 struct string_list **e, **b;524 struct string_list **e, **b;
@@ -624,7 +625,7 @@ static unsigned long expand_and_crc_sym(struct symbol *sym, unsigned long crc) · 100 unmodified lines
624void export_symbol(const char *name)625void export_symbol(const char *name)
625{626{
626 struct symbol *sym;627 struct symbol *sym;
627 unsigned long crc;628 uint32_t crc;
628 int has_changed = 0;629 int has_changed = 0;
629 630
630 sym = find_symbol(name, SYM_NORMAL, 0);631 sym = find_symbol(name, SYM_NORMAL, 0);
@@ -672,7 +673,7 @@ void export_symbol(const char *name) · 41 unmodified lines
672 if (flag_dump_defs)673 if (flag_dump_defs)
673 fputs(">\n", debugfile);674 fputs(">\n", debugfile);
674 675
675 printf("#SYMVER %s 0x%08lx\n", name, crc);676 printf("#SYMVER %s 0x%08lx\n", name, (unsigned long)crc);
676}677}
677 678
678/*----------------------------------------------------------------------*/679/*----------------------------------------------------------------------*/