1 /* Generate checksums of executables for PCH validation
2 Copyright (C) 2005-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
27 fputs ("Usage: genchecksums <filename> ...\n", stderr
);
30 /* Important: BLOCKSIZE must be a multiple of 64. */
31 #define BLOCKSIZE 4096
34 dosum (struct md5_ctx
*ctx
, const char *file
)
37 char buffer
[BLOCKSIZE
+ 72];
40 f
= fopen (file
, "rb");
43 fprintf (stderr
, "opening %s: %s\n", file
, xstrerror (errno
));
47 /* Some executable formats have timestamps in the first 16 bytes, yuck. */
48 if (fseek (f
, 16, SEEK_SET
) != 0)
50 fprintf (stderr
, "seeking in %s: %s\n", file
, xstrerror (errno
));
54 /* Iterate over full file contents. */
57 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
58 computation function processes the whole buffer so that with the
59 next round of the loop another block can be read. */
63 /* Read block. Take care for partial reads. */
66 n
= fread (buffer
+ sum
, 1, BLOCKSIZE
- sum
, f
);
70 while (sum
< BLOCKSIZE
&& n
!= 0);
71 if (n
== 0 && ferror (f
))
74 /* If end of file is reached, end the loop. */
78 /* Process buffer with BLOCKSIZE bytes. Note that
81 md5_process_block (buffer
, BLOCKSIZE
, ctx
);
84 /* Add the last bytes if necessary. */
86 md5_process_bytes (buffer
, sum
, ctx
);
90 fprintf (stderr
, "reading %s: %s\n", file
, xstrerror (errno
));
96 main (int argc
, char ** argv
)
99 unsigned char result
[16];
109 for (i
= 1; i
< argc
; i
++)
110 dosum (&ctx
, argv
[i
]);
111 md5_finish_ctx (&ctx
, result
);
113 puts ("#include \"config.h\"");
114 puts ("#include \"system.h\"");
115 fputs ("EXPORTED_CONST unsigned char executable_checksum[16] = { ", stdout
);
116 for (i
= 0; i
< 16; i
++)
117 printf ("0x%02x%s", result
[i
], i
== 15 ? " };\n" : ", ");