OSDN Git Service

2009-08-14 Paolo Bonzini <bonzini@gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / c-pch.c
1 /* Precompiled header implementation for the C languages.
2    Copyright (C) 2000, 2002, 2003, 2004, 2005, 2007, 2008, 2009
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 "toplev.h"
31 #include "debug.h"
32 #include "c-pragma.h"
33 #include "ggc.h"
34 #include "langhooks.h"
35 #include "hosthooks.h"
36 #include "target.h"
37 #include "opts.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   v.debug_info_type = write_symbols;
138   {
139     size_t i;
140     for (i = 0; i < MATCH_SIZE; i++)
141       {
142         v.match[i] = *pch_matching[i].flag_var;
143         gcc_assert (v.match[i] == *pch_matching[i].flag_var);
144       }
145   }
146   v.pch_init = &pch_init;
147   target_validity = targetm.get_pch_validity (&v.target_data_length);
148
149   if (fwrite (partial_pch, IDENT_LENGTH, 1, f) != 1
150       || fwrite (executable_checksum, 16, 1, f) != 1
151       || fwrite (&v, sizeof (v), 1, f) != 1
152       || fwrite (target_validity, v.target_data_length, 1, f) != 1)
153     fatal_error ("can%'t write to %s: %m", pch_file);
154
155   /* We need to be able to re-read the output.  */
156   /* The driver always provides a valid -o option.  */
157   if (asm_file_name == NULL
158       || strcmp (asm_file_name, "-") == 0)
159     fatal_error ("%qs is not a valid output file", asm_file_name);
160
161   asm_file_startpos = ftell (asm_out_file);
162
163   /* Let the debugging format deal with the PCHness.  */
164   (*debug_hooks->handle_pch) (0);
165
166   cpp_save_state (parse_in, f);
167 }
168
169 /* Write the PCH file.  This is called at the end of a compilation which
170    will produce a PCH file.  */
171
172 void
173 c_common_write_pch (void)
174 {
175   char *buf;
176   long asm_file_end;
177   long written;
178   struct c_pch_header h;
179
180   (*debug_hooks->handle_pch) (1);
181
182   cpp_write_pch_deps (parse_in, pch_outfile);
183
184   asm_file_end = ftell (asm_out_file);
185   h.asm_size = asm_file_end - asm_file_startpos;
186
187   if (fwrite (&h, sizeof (h), 1, pch_outfile) != 1)
188     fatal_error ("can%'t write %s: %m", pch_file);
189
190   buf = XNEWVEC (char, 16384);
191
192   if (fseek (asm_out_file, asm_file_startpos, SEEK_SET) != 0)
193     fatal_error ("can%'t seek in %s: %m", asm_file_name);
194
195   for (written = asm_file_startpos; written < asm_file_end; )
196     {
197       long size = asm_file_end - written;
198       if (size > 16384)
199         size = 16384;
200       if (fread (buf, size, 1, asm_out_file) != 1)
201         fatal_error ("can%'t read %s: %m", asm_file_name);
202       if (fwrite (buf, size, 1, pch_outfile) != 1)
203         fatal_error ("can%'t write %s: %m", pch_file);
204       written += size;
205     }
206   free (buf);
207   /* asm_out_file can be written afterwards, so fseek to clear
208      _IOREAD flag.  */
209   if (fseek (asm_out_file, 0, SEEK_END) != 0)
210     fatal_error ("can%'t seek in %s: %m", asm_file_name);
211
212   gt_pch_save (pch_outfile);
213   cpp_write_pch_state (parse_in, pch_outfile);
214
215   if (fseek (pch_outfile, 0, SEEK_SET) != 0
216       || fwrite (get_ident (), IDENT_LENGTH, 1, pch_outfile) != 1)
217     fatal_error ("can%'t write %s: %m", pch_file);
218
219   fclose (pch_outfile);
220 }
221
222 /* Check the PCH file called NAME, open on FD, to see if it can be
223    used in this compilation.  Return 1 if valid, 0 if the file can't
224    be used now but might be if it's seen later in the compilation, and
225    2 if this file could never be used in the compilation.  */
226
227 int
228 c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
229 {
230   int sizeread;
231   int result;
232   char ident[IDENT_LENGTH + 16];
233   const char *pch_ident;
234   struct c_pch_validity v;
235
236   /* Perform a quick test of whether this is a valid
237      precompiled header for the current language.  */
238
239   gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
240
241   sizeread = read (fd, ident, IDENT_LENGTH + 16);
242   if (sizeread == -1)
243     fatal_error ("can%'t read %s: %m", name);
244   else if (sizeread != IDENT_LENGTH + 16)
245     {
246       if (cpp_get_options (pfile)->warn_invalid_pch)
247         cpp_error (pfile, CPP_DL_WARNING, "%s: too short to be a PCH file",
248                    name);
249       return 2;
250     }
251
252   pch_ident = get_ident();
253   if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0)
254     {
255       if (cpp_get_options (pfile)->warn_invalid_pch)
256         {
257           if (memcmp (ident, pch_ident, 5) == 0)
258             /* It's a PCH, for the right language, but has the wrong version.
259              */
260             cpp_error (pfile, CPP_DL_WARNING,
261                        "%s: not compatible with this GCC version", name);
262           else if (memcmp (ident, pch_ident, 4) == 0)
263             /* It's a PCH for the wrong language.  */
264             cpp_error (pfile, CPP_DL_WARNING, "%s: not for %s", name,
265                        lang_hooks.name);
266           else
267             /* Not any kind of PCH.  */
268             cpp_error (pfile, CPP_DL_WARNING, "%s: not a PCH file", name);
269         }
270       return 2;
271     }
272   if (memcmp (ident + IDENT_LENGTH, executable_checksum, 16) != 0)
273     {
274       if (cpp_get_options (pfile)->warn_invalid_pch)
275         cpp_error (pfile, CPP_DL_WARNING,
276                    "%s: created by a different GCC executable", name);
277       return 2;
278     }
279
280   /* At this point, we know it's a PCH file created by this
281      executable, so it ought to be long enough that we can read a
282      c_pch_validity structure.  */
283   if (read (fd, &v, sizeof (v)) != sizeof (v))
284     fatal_error ("can%'t read %s: %m", name);
285
286   /* The allowable debug info combinations are that either the PCH file
287      was built with the same as is being used now, or the PCH file was
288      built for some kind of debug info but now none is in use.  */
289   if (v.debug_info_type != write_symbols
290       && write_symbols != NO_DEBUG)
291     {
292       if (cpp_get_options (pfile)->warn_invalid_pch)
293         cpp_error (pfile, CPP_DL_WARNING,
294                    "%s: created with -g%s, but used with -g%s", name,
295                    debug_type_names[v.debug_info_type],
296                    debug_type_names[write_symbols]);
297       return 2;
298     }
299
300   /* Check flags that must match exactly.  */
301   {
302     size_t i;
303     for (i = 0; i < MATCH_SIZE; i++)
304       if (*pch_matching[i].flag_var != v.match[i])
305         {
306           if (cpp_get_options (pfile)->warn_invalid_pch)
307             cpp_error (pfile, CPP_DL_WARNING,
308                        "%s: settings for %s do not match", name,
309                        pch_matching[i].flag_name);
310           return 2;
311         }
312   }
313
314   /* If the text segment was not loaded at the same address as it was
315      when the PCH file was created, function pointers loaded from the
316      PCH will not be valid.  We could in theory remap all the function
317      pointers, but no support for that exists at present.
318      Since we have the same executable, it should only be necessary to
319      check one function.  */
320   if (v.pch_init != &pch_init)
321     {
322       if (cpp_get_options (pfile)->warn_invalid_pch)
323         cpp_error (pfile, CPP_DL_WARNING,
324                    "%s: had text segment at different address", name);
325       return 2;
326     }
327
328   /* Check the target-specific validity data.  */
329   {
330     void *this_file_data = xmalloc (v.target_data_length);
331     const char *msg;
332
333     if ((size_t) read (fd, this_file_data, v.target_data_length)
334         != v.target_data_length)
335       fatal_error ("can%'t read %s: %m", name);
336     msg = targetm.pch_valid_p (this_file_data, v.target_data_length);
337     free (this_file_data);
338     if (msg != NULL)
339       {
340         if (cpp_get_options (pfile)->warn_invalid_pch)
341           cpp_error (pfile, CPP_DL_WARNING, "%s: %s", name, msg);
342         return 2;
343       }
344   }
345
346   /* Check the preprocessor macros are the same as when the PCH was
347      generated.  */
348
349   result = cpp_valid_state (pfile, name, fd);
350   if (result == -1)
351     return 2;
352   else
353     return result == 0;
354 }
355
356 /* If non-NULL, this function is called after a precompile header file
357    is loaded.  */
358 void (*lang_post_pch_load) (void);
359
360 /* Load in the PCH file NAME, open on FD.  It was originally searched for
361    by ORIG_NAME.  */
362
363 void
364 c_common_read_pch (cpp_reader *pfile, const char *name,
365                    int fd, const char *orig_name ATTRIBUTE_UNUSED)
366 {
367   FILE *f;
368   struct c_pch_header h;
369   struct save_macro_data *smd;
370   expanded_location saved_loc;
371   bool saved_trace_includes;
372
373   f = fdopen (fd, "rb");
374   if (f == NULL)
375     {
376       cpp_errno (pfile, CPP_DL_ERROR, "calling fdopen");
377       close (fd);
378       return;
379     }
380
381   cpp_get_callbacks (parse_in)->valid_pch = NULL;
382
383   if (fread (&h, sizeof (h), 1, f) != 1)
384     {
385       cpp_errno (pfile, CPP_DL_ERROR, "reading");
386       fclose (f);
387       return;
388     }
389
390   if (!flag_preprocess_only)
391     {
392       unsigned long written;
393       char * buf = XNEWVEC (char, 16384);
394
395       for (written = 0; written < h.asm_size; )
396         {
397           long size = h.asm_size - written;
398           if (size > 16384)
399             size = 16384;
400           if (fread (buf, size, 1, f) != 1
401               || fwrite (buf, size, 1, asm_out_file) != 1)
402             cpp_errno (pfile, CPP_DL_ERROR, "reading");
403           written += size;
404         }
405       free (buf);
406     }
407   else
408     {
409       /* If we're preprocessing, don't write to a NULL
410          asm_out_file.  */
411       if (fseek (f, h.asm_size, SEEK_CUR) != 0)
412         cpp_errno (pfile, CPP_DL_ERROR, "seeking");
413     }
414
415   /* Save the location and then restore it after reading the PCH.  */
416   saved_loc = expand_location (line_table->highest_line);
417   saved_trace_includes = line_table->trace_includes;
418
419   cpp_prepare_state (pfile, &smd);
420
421   gt_pch_restore (f);
422
423   if (cpp_read_state (pfile, name, f, smd) != 0)
424     {
425       fclose (f);
426       return;
427     }
428
429   fclose (f);
430
431   line_table->trace_includes = saved_trace_includes;
432   cpp_set_line_map (pfile, line_table);
433   linemap_add (line_table, LC_RENAME, 0, saved_loc.file, saved_loc.line);
434
435   /* Give the front end a chance to take action after a PCH file has
436      been loaded.  */
437   if (lang_post_pch_load)
438     (*lang_post_pch_load) ();
439 }
440
441 /* Indicate that no more PCH files should be read.  */
442
443 void
444 c_common_no_more_pch (void)
445 {
446   if (cpp_get_callbacks (parse_in)->valid_pch)
447     {
448       cpp_get_callbacks (parse_in)->valid_pch = NULL;
449       host_hooks.gt_pch_use_address (NULL, 0, -1, 0);
450     }
451 }
452
453 /* Handle #pragma GCC pch_preprocess, to load in the PCH file.  */
454
455 #ifndef O_BINARY
456 # define O_BINARY 0
457 #endif
458
459 void
460 c_common_pch_pragma (cpp_reader *pfile, const char *name)
461 {
462   int fd;
463
464   if (!cpp_get_options (pfile)->preprocessed)
465     {
466       error ("pch_preprocess pragma should only be used with -fpreprocessed");
467       inform (input_location, "use #include instead");
468       return;
469     }
470
471   fd = open (name, O_RDONLY | O_BINARY, 0666);
472   if (fd == -1)
473     fatal_error ("%s: couldn%'t open PCH file: %m", name);
474
475   if (c_common_valid_pch (pfile, name, fd) != 1)
476     {
477       if (!cpp_get_options (pfile)->warn_invalid_pch)
478         inform (input_location, "use -Winvalid-pch for more information");
479       fatal_error ("%s: PCH file was invalid", name);
480     }
481
482   c_common_read_pch (pfile, name, fd, name);
483
484   close (fd);
485 }
486
487 /* Print out executable_checksum[].  */
488
489 void
490 c_common_print_pch_checksum (FILE *f)
491 {
492   int i;
493   fputs ("Compiler executable checksum: ", f);
494   for (i = 0; i < 16; i++)
495     fprintf (f, "%02x", executable_checksum[i]);
496   putc ('\n', f);
497 }