00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "config.h"
00022 #include "system.h"
00023 #include "coretypes.h"
00024 #include "version.h"
00025 #include "cpplib.h"
00026 #include "tree.h"
00027 #include "flags.h"
00028 #include "c-common.h"
00029 #include "output.h"
00030 #include "toplev.h"
00031 #include "debug.h"
00032 #include "c-pragma.h"
00033 #include "ggc.h"
00034 #include "langhooks.h"
00035 #include "hosthooks.h"
00036 #include "target.h"
00037
00038
00039
00040
00041
00042 static const struct c_pch_matching
00043 {
00044 int *flag_var;
00045 const char *flag_name;
00046 } pch_matching[] = {
00047 { &flag_exceptions, "-fexceptions" },
00048 { &flag_unit_at_a_time, "-funit-at-a-time" }
00049 };
00050
00051 enum {
00052 MATCH_SIZE = ARRAY_SIZE (pch_matching)
00053 };
00054
00055
00056
00057 static const char no_checksum[16] = { 0 };
00058
00059
00060
00061
00062
00063
00064 struct c_pch_validity
00065 {
00066 unsigned char debug_info_type;
00067 signed char match[MATCH_SIZE];
00068 void (*pch_init) (void);
00069 size_t target_data_length;
00070 };
00071
00072 struct c_pch_header
00073 {
00074 unsigned long asm_size;
00075 };
00076
00077 #define IDENT_LENGTH 8
00078
00079
00080 static FILE *pch_outfile;
00081
00082
00083 static long asm_file_startpos;
00084
00085 static const char *get_ident (void);
00086
00087
00088
00089
00090
00091
00092 static const char *
00093 get_ident (void)
00094 {
00095 static char result[IDENT_LENGTH];
00096 static const char template[IDENT_LENGTH] = "gpch.013";
00097 static const char c_language_chars[] = "Co+O";
00098
00099 memcpy (result, template, IDENT_LENGTH);
00100 result[4] = c_language_chars[c_language];
00101
00102 return result;
00103 }
00104
00105
00106
00107
00108
00109
00110 void
00111 pch_init (void)
00112 {
00113 FILE *f;
00114 struct c_pch_validity v;
00115 void *target_validity;
00116 static const char partial_pch[IDENT_LENGTH] = "gpcWrite";
00117
00118 #ifdef ASM_COMMENT_START
00119 if (flag_verbose_asm)
00120 {
00121 fprintf (asm_out_file, "%s ", ASM_COMMENT_START);
00122 c_common_print_pch_checksum (asm_out_file);
00123 fputc ('\n', asm_out_file);
00124 }
00125 #endif
00126
00127 if (!pch_file)
00128 return;
00129
00130 f = fopen (pch_file, "w+b");
00131 if (f == NULL)
00132 fatal_error ("can%'t create precompiled header %s: %m", pch_file);
00133 pch_outfile = f;
00134
00135 gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
00136
00137 v.debug_info_type = write_symbols;
00138 {
00139 size_t i;
00140 for (i = 0; i < MATCH_SIZE; i++)
00141 {
00142 v.match[i] = *pch_matching[i].flag_var;
00143 gcc_assert (v.match[i] == *pch_matching[i].flag_var);
00144 }
00145 }
00146 v.pch_init = &pch_init;
00147 target_validity = targetm.get_pch_validity (&v.target_data_length);
00148
00149 if (fwrite (partial_pch, IDENT_LENGTH, 1, f) != 1
00150 || fwrite (executable_checksum, 16, 1, f) != 1
00151 || fwrite (&v, sizeof (v), 1, f) != 1
00152 || fwrite (target_validity, v.target_data_length, 1, f) != 1)
00153 fatal_error ("can%'t write to %s: %m", pch_file);
00154
00155
00156
00157 if (asm_file_name == NULL
00158 || strcmp (asm_file_name, "-") == 0)
00159 fatal_error ("%qs is not a valid output file", asm_file_name);
00160
00161 asm_file_startpos = ftell (asm_out_file);
00162
00163
00164 (*debug_hooks->handle_pch) (0);
00165
00166 cpp_save_state (parse_in, f);
00167 }
00168
00169
00170
00171
00172 void
00173 c_common_write_pch (void)
00174 {
00175 char *buf;
00176 long asm_file_end;
00177 long written;
00178 struct c_pch_header h;
00179
00180 (*debug_hooks->handle_pch) (1);
00181
00182 cpp_write_pch_deps (parse_in, pch_outfile);
00183
00184 asm_file_end = ftell (asm_out_file);
00185 h.asm_size = asm_file_end - asm_file_startpos;
00186
00187 if (fwrite (&h, sizeof (h), 1, pch_outfile) != 1)
00188 fatal_error ("can%'t write %s: %m", pch_file);
00189
00190 buf = XNEWVEC (char, 16384);
00191
00192 if (fseek (asm_out_file, asm_file_startpos, SEEK_SET) != 0)
00193 fatal_error ("can%'t seek in %s: %m", asm_file_name);
00194
00195 for (written = asm_file_startpos; written < asm_file_end; )
00196 {
00197 long size = asm_file_end - written;
00198 if (size > 16384)
00199 size = 16384;
00200 if (fread (buf, size, 1, asm_out_file) != 1)
00201 fatal_error ("can%'t read %s: %m", asm_file_name);
00202 if (fwrite (buf, size, 1, pch_outfile) != 1)
00203 fatal_error ("can%'t write %s: %m", pch_file);
00204 written += size;
00205 }
00206 free (buf);
00207
00208
00209 if (fseek (asm_out_file, 0, SEEK_END) != 0)
00210 fatal_error ("can%'t seek in %s: %m", asm_file_name);
00211
00212 gt_pch_save (pch_outfile);
00213 cpp_write_pch_state (parse_in, pch_outfile);
00214
00215 if (fseek (pch_outfile, 0, SEEK_SET) != 0
00216 || fwrite (get_ident (), IDENT_LENGTH, 1, pch_outfile) != 1)
00217 fatal_error ("can%'t write %s: %m", pch_file);
00218
00219 fclose (pch_outfile);
00220 }
00221
00222
00223
00224
00225
00226
00227 int
00228 c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
00229 {
00230 int sizeread;
00231 int result;
00232 char ident[IDENT_LENGTH + 16];
00233 const char *pch_ident;
00234 struct c_pch_validity v;
00235
00236
00237
00238
00239 gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
00240
00241 sizeread = read (fd, ident, IDENT_LENGTH + 16);
00242 if (sizeread == -1)
00243 fatal_error ("can%'t read %s: %m", name);
00244 else if (sizeread != IDENT_LENGTH + 16)
00245 {
00246 cpp_error (pfile, CPP_DL_WARNING, "%s: too short to be a PCH file",
00247 name);
00248 return 2;
00249 }
00250
00251 pch_ident = get_ident();
00252 if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0)
00253 {
00254 if (cpp_get_options (pfile)->warn_invalid_pch)
00255 {
00256 if (memcmp (ident, pch_ident, 5) == 0)
00257
00258
00259 cpp_error (pfile, CPP_DL_WARNING,
00260 "%s: not compatible with this GCC version", name);
00261 else if (memcmp (ident, pch_ident, 4) == 0)
00262
00263 cpp_error (pfile, CPP_DL_WARNING, "%s: not for %s", name,
00264 lang_hooks.name);
00265 else
00266
00267 cpp_error (pfile, CPP_DL_WARNING, "%s: not a PCH file", name);
00268 }
00269 return 2;
00270 }
00271 if (memcmp (ident + IDENT_LENGTH, executable_checksum, 16) != 0)
00272 {
00273 if (cpp_get_options (pfile)->warn_invalid_pch)
00274 cpp_error (pfile, CPP_DL_WARNING,
00275 "%s: created by a different GCC executable", name);
00276 return 2;
00277 }
00278
00279
00280
00281
00282 if (read (fd, &v, sizeof (v)) != sizeof (v))
00283 fatal_error ("can%'t read %s: %m", name);
00284
00285
00286
00287
00288 if (v.debug_info_type != write_symbols
00289 && write_symbols != NO_DEBUG)
00290 {
00291 if (cpp_get_options (pfile)->warn_invalid_pch)
00292 cpp_error (pfile, CPP_DL_WARNING,
00293 "%s: created with -g%s, but used with -g%s", name,
00294 debug_type_names[v.debug_info_type],
00295 debug_type_names[write_symbols]);
00296 return 2;
00297 }
00298
00299
00300 {
00301 size_t i;
00302 for (i = 0; i < MATCH_SIZE; i++)
00303 if (*pch_matching[i].flag_var != v.match[i])
00304 {
00305 if (cpp_get_options (pfile)->warn_invalid_pch)
00306 cpp_error (pfile, CPP_DL_WARNING,
00307 "%s: settings for %s do not match", name,
00308 pch_matching[i].flag_name);
00309 return 2;
00310 }
00311 }
00312
00313
00314
00315
00316
00317
00318
00319 if (v.pch_init != &pch_init)
00320 {
00321 if (cpp_get_options (pfile)->warn_invalid_pch)
00322 cpp_error (pfile, CPP_DL_WARNING,
00323 "%s: had text segment at different address", name);
00324 return 2;
00325 }
00326
00327
00328 {
00329 void *this_file_data = xmalloc (v.target_data_length);
00330 const char *msg;
00331
00332 if ((size_t) read (fd, this_file_data, v.target_data_length)
00333 != v.target_data_length)
00334 fatal_error ("can%'t read %s: %m", name);
00335 msg = targetm.pch_valid_p (this_file_data, v.target_data_length);
00336 free (this_file_data);
00337 if (msg != NULL)
00338 {
00339 if (cpp_get_options (pfile)->warn_invalid_pch)
00340 cpp_error (pfile, CPP_DL_WARNING, "%s: %s", name, msg);
00341 return 2;
00342 }
00343 }
00344
00345
00346
00347
00348 result = cpp_valid_state (pfile, name, fd);
00349 if (result == -1)
00350 return 2;
00351 else
00352 return result == 0;
00353 }
00354
00355
00356
00357 void (*lang_post_pch_load) (void);
00358
00359
00360
00361
00362 void
00363 c_common_read_pch (cpp_reader *pfile, const char *name,
00364 int fd, const char *orig_name ATTRIBUTE_UNUSED)
00365 {
00366 FILE *f;
00367 struct c_pch_header h;
00368 struct save_macro_data *smd;
00369
00370 f = fdopen (fd, "rb");
00371 if (f == NULL)
00372 {
00373 cpp_errno (pfile, CPP_DL_ERROR, "calling fdopen");
00374 return;
00375 }
00376
00377 cpp_get_callbacks (parse_in)->valid_pch = NULL;
00378
00379 if (fread (&h, sizeof (h), 1, f) != 1)
00380 {
00381 cpp_errno (pfile, CPP_DL_ERROR, "reading");
00382 return;
00383 }
00384
00385 if (!flag_preprocess_only)
00386 {
00387 unsigned long written;
00388 char * buf = XNEWVEC (char, 16384);
00389
00390 for (written = 0; written < h.asm_size; )
00391 {
00392 long size = h.asm_size - written;
00393 if (size > 16384)
00394 size = 16384;
00395 if (fread (buf, size, 1, f) != 1
00396 || fwrite (buf, size, 1, asm_out_file) != 1)
00397 cpp_errno (pfile, CPP_DL_ERROR, "reading");
00398 written += size;
00399 }
00400 free (buf);
00401 }
00402 else
00403 {
00404
00405
00406 if (fseek (f, h.asm_size, SEEK_CUR) != 0)
00407 cpp_errno (pfile, CPP_DL_ERROR, "seeking");
00408 }
00409
00410 cpp_prepare_state (pfile, &smd);
00411
00412 gt_pch_restore (f);
00413
00414 if (cpp_read_state (pfile, name, f, smd) != 0)
00415 return;
00416
00417 fclose (f);
00418
00419
00420
00421 if (lang_post_pch_load)
00422 (*lang_post_pch_load) ();
00423 }
00424
00425
00426
00427 void
00428 c_common_no_more_pch (void)
00429 {
00430 if (cpp_get_callbacks (parse_in)->valid_pch)
00431 {
00432 cpp_get_callbacks (parse_in)->valid_pch = NULL;
00433 host_hooks.gt_pch_use_address (NULL, 0, -1, 0);
00434 }
00435 }
00436
00437
00438
00439 #ifndef O_BINARY
00440 # define O_BINARY 0
00441 #endif
00442
00443 void
00444 c_common_pch_pragma (cpp_reader *pfile, const char *name)
00445 {
00446 int fd;
00447
00448 if (!cpp_get_options (pfile)->preprocessed)
00449 {
00450 error ("pch_preprocess pragma should only be used with -fpreprocessed");
00451 inform ("use #include instead");
00452 return;
00453 }
00454
00455 fd = open (name, O_RDONLY | O_BINARY, 0666);
00456 if (fd == -1)
00457 fatal_error ("%s: couldn%'t open PCH file: %m", name);
00458
00459 if (c_common_valid_pch (pfile, name, fd) != 1)
00460 {
00461 if (!cpp_get_options (pfile)->warn_invalid_pch)
00462 inform ("use -Winvalid-pch for more information");
00463 fatal_error ("%s: PCH file was invalid", name);
00464 }
00465
00466 c_common_read_pch (pfile, name, fd, name);
00467
00468 close (fd);
00469 }
00470
00471
00472
00473 void
00474 c_common_print_pch_checksum (FILE *f)
00475 {
00476 int i;
00477 fputs ("Compiler executable checksum: ", f);
00478 for (i = 0; i < 16; i++)
00479 fprintf (f, "%02x", executable_checksum[i]);
00480 putc ('\n', f);
00481 }