OSDN Git Service

2011-02-07 Iain Sandoe <iains@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / c-family / c-pch.c
1 /* Precompiled header implementation for the C languages.
2    Copyright (C) 2000, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "version.h"
25 #include "cpplib.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "c-common.h"
29 #include "output.h"
30 #include "debug.h"
31 #include "c-pragma.h"
32 #include "ggc.h"
33 #include "langhooks.h"
34 #include "hosthooks.h"
35 #include "target.h"
36 #include "opts.h"
37 #include "timevar.h"
38
39 /* This is a list of flag variables that must match exactly, and their
40    names for the error message.  The possible values for *flag_var must
41    fit in a 'signed char'.  */
42
43 static const struct c_pch_matching
44 {
45   int *flag_var;
46   const char *flag_name;
47 } pch_matching[] = {
48   { &flag_exceptions, "-fexceptions" },
49 };
50
51 enum {
52   MATCH_SIZE = ARRAY_SIZE (pch_matching)
53 };
54
55 /* The value of the checksum in the dummy compiler that is actually
56    checksummed.  That compiler should never be run.  */
57 static const char no_checksum[16] = { 0 };
58
59 /* Information about flags and suchlike that affect PCH validity.
60
61    Before this structure is read, both an initial 8-character identification
62    string, and a 16-byte checksum, have been read and validated.  */
63
64 struct c_pch_validity
65 {
66   unsigned char debug_info_type;
67   signed char match[MATCH_SIZE];
68   void (*pch_init) (void);
69   size_t target_data_length;
70 };
71
72 struct c_pch_header
73 {
74   unsigned long asm_size;
75 };
76
77 #define IDENT_LENGTH 8
78
79 /* The file we'll be writing the PCH to.  */
80 static FILE *pch_outfile;
81
82 /* The position in the assembler output file when pch_init was called.  */
83 static long asm_file_startpos;
84
85 static const char *get_ident (void);
86
87 /* Compute an appropriate 8-byte magic number for the PCH file, so that
88    utilities like file(1) can identify it, and so that GCC can quickly
89    ignore non-PCH files and PCH files that are of a completely different
90    format.  */
91
92 static const char *
93 get_ident (void)
94 {
95   static char result[IDENT_LENGTH];
96   static const char templ[] = "gpch.013";
97   static const char c_language_chars[] = "Co+O";
98
99   memcpy (result, templ, IDENT_LENGTH);
100   result[4] = c_language_chars[c_language];
101
102   return result;
103 }
104
105 /* Prepare to write a PCH file, if one is being written.  This is
106    called at the start of compilation.
107
108    Also, print out the executable checksum if -fverbose-asm is in effect.  */
109
110 void
111 pch_init (void)
112 {
113   FILE *f;
114   struct c_pch_validity v;
115   void *target_validity;
116   static const char partial_pch[] = "gpcWrite";
117
118 #ifdef ASM_COMMENT_START
119   if (flag_verbose_asm)
120     {
121       fprintf (asm_out_file, "%s ", ASM_COMMENT_START);
122       c_common_print_pch_checksum (asm_out_file);
123       fputc ('\n', asm_out_file);
124     }
125 #endif
126
127   if (!pch_file)
128     return;
129
130   f = fopen (pch_file, "w+b");
131   if (f == NULL)
132     fatal_error ("can%'t create precompiled header %s: %m", pch_file);
133   pch_outfile = f;
134
135   gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
136
137   memset (&v, '\0', sizeof (v));
138   v.debug_info_type = write_symbols;
139   {
140     size_t i;
141     for (i = 0; i < MATCH_SIZE; i++)
142       {
143         v.match[i] = *pch_matching[i].flag_var;
144         gcc_assert (v.match[i] == *pch_matching[i].flag_var);
145       }
146   }
147   v.pch_init = &pch_init;
148   target_validity = targetm.get_pch_validity (&v.target_data_length);
149
150   if (fwrite (partial_pch, IDENT_LENGTH, 1, f) != 1
151       || fwrite (executable_checksum, 16, 1, f) != 1
152       || fwrite (&v, sizeof (v), 1, f) != 1
153       || fwrite (target_validity, v.target_data_length, 1, f) != 1)
154     fatal_error ("can%'t write to %s: %m", pch_file);
155
156   /* We need to be able to re-read the output.  */
157   /* The driver always provides a valid -o option.  */
158   if (asm_file_name == NULL
159       || strcmp (asm_file_name, "-") == 0)
160     fatal_error ("%qs is not a valid output file", asm_file_name);
161
162   asm_file_startpos = ftell (asm_out_file);
163
164   /* Let the debugging format deal with the PCHness.  */
165   (*debug_hooks->handle_pch) (0);
166
167   cpp_save_state (parse_in, f);
168 }
169
170 /* Write the PCH file.  This is called at the end of a compilation which
171    will produce a PCH file.  */
172
173 void
174 c_common_write_pch (void)
175 {
176   char *buf;
177   long asm_file_end;
178   long written;
179   struct c_pch_header h;
180
181   timevar_push (TV_PCH_SAVE);
182
183   (*debug_hooks->handle_pch) (1);
184
185   cpp_write_pch_deps (parse_in, pch_outfile);
186
187   asm_file_end = ftell (asm_out_file);
188   h.asm_size = asm_file_end - asm_file_startpos;
189
190   if (fwrite (&h, sizeof (h), 1, pch_outfile) != 1)
191     fatal_error ("can%'t write %s: %m", pch_file);
192
193   buf = XNEWVEC (char, 16384);
194
195   if (fseek (asm_out_file, asm_file_startpos, SEEK_SET) != 0)
196     fatal_error ("can%'t seek in %s: %m", asm_file_name);
197
198   for (written = asm_file_startpos; written < asm_file_end; )
199     {
200       long size = asm_file_end - written;
201       if (size > 16384)
202         size = 16384;
203       if (fread (buf, size, 1, asm_out_file) != 1)
204         fatal_error ("can%'t read %s: %m", asm_file_name);
205       if (fwrite (buf, size, 1, pch_outfile) != 1)
206         fatal_error ("can%'t write %s: %m", pch_file);
207       written += size;
208     }
209   free (buf);
210   /* asm_out_file can be written afterwards, so fseek to clear
211      _IOREAD flag.  */
212   if (fseek (asm_out_file, 0, SEEK_END) != 0)
213     fatal_error ("can%'t seek in %s: %m", asm_file_name);
214
215   gt_pch_save (pch_outfile);
216
217   timevar_push (TV_PCH_CPP_SAVE);
218   cpp_write_pch_state (parse_in, pch_outfile);
219   timevar_pop (TV_PCH_CPP_SAVE);
220
221   if (fseek (pch_outfile, 0, SEEK_SET) != 0
222       || fwrite (get_ident (), IDENT_LENGTH, 1, pch_outfile) != 1)
223     fatal_error ("can%'t write %s: %m", pch_file);
224
225   fclose (pch_outfile);
226
227   timevar_pop (TV_PCH_SAVE);
228 }
229
230 /* Check the PCH file called NAME, open on FD, to see if it can be
231    used in this compilation.  Return 1 if valid, 0 if the file can't
232    be used now but might be if it's seen later in the compilation, and
233    2 if this file could never be used in the compilation.  */
234
235 int
236 c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
237 {
238   int sizeread;
239   int result;
240   char ident[IDENT_LENGTH + 16];
241   const char *pch_ident;
242   struct c_pch_validity v;
243
244   /* Perform a quick test of whether this is a valid
245      precompiled header for the current language.  */
246
247   gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
248
249   sizeread = read (fd, ident, IDENT_LENGTH + 16);
250   if (sizeread == -1)
251     fatal_error ("can%'t read %s: %m", name);
252   else if (sizeread != IDENT_LENGTH + 16)
253     {
254       if (cpp_get_options (pfile)->warn_invalid_pch)
255         cpp_error (pfile, CPP_DL_WARNING, "%s: too short to be a PCH file",
256                    name);
257       return 2;
258     }
259
260   pch_ident = get_ident();
261   if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0)
262     {
263       if (cpp_get_options (pfile)->warn_invalid_pch)
264         {
265           if (memcmp (ident, pch_ident, 5) == 0)
266             /* It's a PCH, for the right language, but has the wrong version.
267              */
268             cpp_error (pfile, CPP_DL_WARNING,
269                        "%s: not compatible with this GCC version", name);
270           else if (memcmp (ident, pch_ident, 4) == 0)
271             /* It's a PCH for the wrong language.  */
272             cpp_error (pfile, CPP_DL_WARNING, "%s: not for %s", name,
273                        lang_hooks.name);
274           else
275             /* Not any kind of PCH.  */
276             cpp_error (pfile, CPP_DL_WARNING, "%s: not a PCH file", name);
277         }
278       return 2;
279     }
280   if (memcmp (ident + IDENT_LENGTH, executable_checksum, 16) != 0)
281     {
282       if (cpp_get_options (pfile)->warn_invalid_pch)
283         cpp_error (pfile, CPP_DL_WARNING,
284                    "%s: created by a different GCC executable", name);
285       return 2;
286     }
287
288   /* At this point, we know it's a PCH file created by this
289      executable, so it ought to be long enough that we can read a
290      c_pch_validity structure.  */
291   if (read (fd, &v, sizeof (v)) != sizeof (v))
292     fatal_error ("can%'t read %s: %m", name);
293
294   /* The allowable debug info combinations are that either the PCH file
295      was built with the same as is being used now, or the PCH file was
296      built for some kind of debug info but now none is in use.  */
297   if (v.debug_info_type != write_symbols
298       && write_symbols != NO_DEBUG)
299     {
300       if (cpp_get_options (pfile)->warn_invalid_pch)
301         cpp_error (pfile, CPP_DL_WARNING,
302                    "%s: created with -g%s, but used with -g%s", name,
303                    debug_type_names[v.debug_info_type],
304                    debug_type_names[write_symbols]);
305       return 2;
306     }
307
308   /* Check flags that must match exactly.  */
309   {
310     size_t i;
311     for (i = 0; i < MATCH_SIZE; i++)
312       if (*pch_matching[i].flag_var != v.match[i])
313         {
314           if (cpp_get_options (pfile)->warn_invalid_pch)
315             cpp_error (pfile, CPP_DL_WARNING,
316                        "%s: settings for %s do not match", name,
317                        pch_matching[i].flag_name);
318           return 2;
319         }
320   }
321
322   /* If the text segment was not loaded at the same address as it was
323      when the PCH file was created, function pointers loaded from the
324      PCH will not be valid.  We could in theory remap all the function
325      pointers, but no support for that exists at present.
326      Since we have the same executable, it should only be necessary to
327      check one function.  */
328   if (v.pch_init != &pch_init)
329     {
330       if (cpp_get_options (pfile)->warn_invalid_pch)
331         cpp_error (pfile, CPP_DL_WARNING,
332                    "%s: had text segment at different address", name);
333       return 2;
334     }
335
336   /* Check the target-specific validity data.  */
337   {
338     void *this_file_data = xmalloc (v.target_data_length);
339     const char *msg;
340
341     if ((size_t) read (fd, this_file_data, v.target_data_length)
342         != v.target_data_length)
343       fatal_error ("can%'t read %s: %m", name);
344     msg = targetm.pch_valid_p (this_file_data, v.target_data_length);
345     free (this_file_data);
346     if (msg != NULL)
347       {
348         if (cpp_get_options (pfile)->warn_invalid_pch)
349           cpp_error (pfile, CPP_DL_WARNING, "%s: %s", name, msg);
350         return 2;
351       }
352   }
353
354   /* Check the preprocessor macros are the same as when the PCH was
355      generated.  */
356
357   result = cpp_valid_state (pfile, name, fd);
358   if (result == -1)
359     return 2;
360   else
361     return result == 0;
362 }
363
364 /* If non-NULL, this function is called after a precompile header file
365    is loaded.  */
366 void (*lang_post_pch_load) (void);
367
368 /* Load in the PCH file NAME, open on FD.  It was originally searched for
369    by ORIG_NAME.  */
370
371 void
372 c_common_read_pch (cpp_reader *pfile, const char *name,
373                    int fd, const char *orig_name ATTRIBUTE_UNUSED)
374 {
375   FILE *f;
376   struct c_pch_header h;
377   struct save_macro_data *smd;
378   expanded_location saved_loc;
379   bool saved_trace_includes;
380
381   timevar_push (TV_PCH_RESTORE);
382
383   f = fdopen (fd, "rb");
384   if (f == NULL)
385     {
386       cpp_errno (pfile, CPP_DL_ERROR, "calling fdopen");
387       close (fd);
388       goto end;
389     }
390
391   cpp_get_callbacks (parse_in)->valid_pch = NULL;
392
393   if (fread (&h, sizeof (h), 1, f) != 1)
394     {
395       cpp_errno (pfile, CPP_DL_ERROR, "reading");
396       fclose (f);
397       goto end;
398     }
399
400   if (!flag_preprocess_only)
401     {
402       unsigned long written;
403       char * buf = XNEWVEC (char, 16384);
404
405       for (written = 0; written < h.asm_size; )
406         {
407           long size = h.asm_size - written;
408           if (size > 16384)
409             size = 16384;
410           if (fread (buf, size, 1, f) != 1
411               || fwrite (buf, size, 1, asm_out_file) != 1)
412             cpp_errno (pfile, CPP_DL_ERROR, "reading");
413           written += size;
414         }
415       free (buf);
416     }
417   else
418     {
419       /* If we're preprocessing, don't write to a NULL
420          asm_out_file.  */
421       if (fseek (f, h.asm_size, SEEK_CUR) != 0)
422         cpp_errno (pfile, CPP_DL_ERROR, "seeking");
423     }
424
425   /* Save the location and then restore it after reading the PCH.  */
426   saved_loc = expand_location (line_table->highest_line);
427   saved_trace_includes = line_table->trace_includes;
428
429   timevar_push (TV_PCH_CPP_RESTORE);
430   cpp_prepare_state (pfile, &smd);
431   timevar_pop (TV_PCH_CPP_RESTORE);
432
433   gt_pch_restore (f);
434
435   timevar_push (TV_PCH_CPP_RESTORE);
436   if (cpp_read_state (pfile, name, f, smd) != 0)
437     {
438       fclose (f);
439       timevar_pop (TV_PCH_CPP_RESTORE);
440       goto end;
441     }
442   timevar_pop (TV_PCH_CPP_RESTORE);
443
444
445   fclose (f);
446
447   line_table->trace_includes = saved_trace_includes;
448   cpp_set_line_map (pfile, line_table);
449   linemap_add (line_table, LC_RENAME, 0, saved_loc.file, saved_loc.line);
450
451   /* Give the front end a chance to take action after a PCH file has
452      been loaded.  */
453   if (lang_post_pch_load)
454     (*lang_post_pch_load) ();
455
456 end:
457   timevar_pop (TV_PCH_RESTORE);
458 }
459
460 /* Indicate that no more PCH files should be read.  */
461
462 void
463 c_common_no_more_pch (void)
464 {
465   if (cpp_get_callbacks (parse_in)->valid_pch)
466     {
467       cpp_get_callbacks (parse_in)->valid_pch = NULL;
468       host_hooks.gt_pch_use_address (NULL, 0, -1, 0);
469     }
470 }
471
472 /* Handle #pragma GCC pch_preprocess, to load in the PCH file.  */
473
474 void
475 c_common_pch_pragma (cpp_reader *pfile, const char *name)
476 {
477   int fd;
478
479   if (!cpp_get_options (pfile)->preprocessed)
480     {
481       error ("pch_preprocess pragma should only be used with -fpreprocessed");
482       inform (input_location, "use #include instead");
483       return;
484     }
485
486   fd = open (name, O_RDONLY | O_BINARY, 0666);
487   if (fd == -1)
488     fatal_error ("%s: couldn%'t open PCH file: %m", name);
489
490   if (c_common_valid_pch (pfile, name, fd) != 1)
491     {
492       if (!cpp_get_options (pfile)->warn_invalid_pch)
493         inform (input_location, "use -Winvalid-pch for more information");
494       fatal_error ("%s: PCH file was invalid", name);
495     }
496
497   c_common_read_pch (pfile, name, fd, name);
498
499   close (fd);
500 }
501
502 /* Print out executable_checksum[].  */
503
504 void
505 c_common_print_pch_checksum (FILE *f)
506 {
507   int i;
508   fputs ("Compiler executable checksum: ", f);
509   for (i = 0; i < 16; i++)
510     fprintf (f, "%02x", executable_checksum[i]);
511   putc ('\n', f);
512 }