1121982Sjhb/* Precompiled header implementation for the C languages.
2121982Sjhb   Copyright (C) 2000, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3121982Sjhb
4121982SjhbThis file is part of GCC.
5121982Sjhb
6121982SjhbGCC is free software; you can redistribute it and/or modify
7121982Sjhbit under the terms of the GNU General Public License as published by
8121982Sjhbthe Free Software Foundation; either version 2, or (at your option)
9121982Sjhbany later version.
10121982Sjhb
11121982SjhbGCC is distributed in the hope that it will be useful,
12121982Sjhbbut WITHOUT ANY WARRANTY; without even the implied warranty of
13121982SjhbMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14121982SjhbGNU General Public License for more details.
15121982Sjhb
16121982SjhbYou should have received a copy of the GNU General Public License
17121982Sjhbalong with GCC; see the file COPYING.  If not, write to
18121982Sjhbthe Free Software Foundation, 51 Franklin Street, Fifth Floor,
19121982SjhbBoston, MA 02110-1301, USA.  */
20121982Sjhb
21121982Sjhb#include "config.h"
22121982Sjhb#include "system.h"
23121982Sjhb#include "coretypes.h"
24121982Sjhb#include "version.h"
25121982Sjhb#include "cpplib.h"
26121982Sjhb#include "tree.h"
27121982Sjhb#include "flags.h"
28121982Sjhb#include "c-common.h"
29121982Sjhb#include "output.h"
30121982Sjhb#include "toplev.h"
31121982Sjhb#include "debug.h"
32121982Sjhb#include "c-pragma.h"
33121982Sjhb#include "ggc.h"
34151979Sjhb#include "langhooks.h"
35151979Sjhb#include "hosthooks.h"
36151979Sjhb#include "target.h"
37151979Sjhb
38151979Sjhb/* This is a list of flag variables that must match exactly, and their
39151979Sjhb   names for the error message.  The possible values for *flag_var must
40151979Sjhb   fit in a 'signed char'.  */
41151979Sjhb
42151979Sjhbstatic const struct c_pch_matching
43151979Sjhb{
44151979Sjhb  int *flag_var;
45151979Sjhb  const char *flag_name;
46164265Sjhb} pch_matching[] = {
47164265Sjhb  { &flag_exceptions, "-fexceptions" },
48164265Sjhb  { &flag_unit_at_a_time, "-funit-at-a-time" }
49151979Sjhb};
50187880Sjeff
51164265Sjhbenum {
52164265Sjhb  MATCH_SIZE = ARRAY_SIZE (pch_matching)
53121982Sjhb};
54151979Sjhb
55164265Sjhb/* The value of the checksum in the dummy compiler that is actually
56164265Sjhb   checksummed.  That compiler should never be run.  */
57164265Sjhbstatic const char no_checksum[16] = { 0 };
58164265Sjhb
59164265Sjhb/* Information about flags and suchlike that affect PCH validity.
60151979Sjhb
61151979Sjhb   Before this structure is read, both an initial 8-character identification
62151979Sjhb   string, and a 16-byte checksum, have been read and validated.  */
63234207Savg
64151979Sjhbstruct c_pch_validity
65151979Sjhb{
66234207Savg  unsigned char debug_info_type;
67163212Sjhb  signed char match[MATCH_SIZE];
68151979Sjhb  void (*pch_init) (void);
69151979Sjhb  size_t target_data_length;
70151979Sjhb};
71121982Sjhb
72121982Sjhbstruct c_pch_header
73121982Sjhb{
74121982Sjhb  unsigned long asm_size;
75121982Sjhb};
76121982Sjhb
77121982Sjhb#define IDENT_LENGTH 8
78121982Sjhb
79121982Sjhb/* The file we'll be writing the PCH to.  */
80121982Sjhbstatic FILE *pch_outfile;
81121982Sjhb
82121982Sjhb/* The position in the assembler output file when pch_init was called.  */
83121982Sjhbstatic long asm_file_startpos;
84121982Sjhb
85121982Sjhbstatic const char *get_ident (void);
86133907Speter
87121982Sjhb/* Compute an appropriate 8-byte magic number for the PCH file, so that
88121982Sjhb   utilities like file(1) can identify it, and so that GCC can quickly
89169391Sjhb   ignore non-PCH files and PCH files that are of a completely different
90121982Sjhb   format.  */
91121982Sjhb
92163219Sjhbstatic const char *
93163219Sjhbget_ident (void)
94129284Speter{
95129284Speter  static char result[IDENT_LENGTH];
96195249Sjhb  static const char template[IDENT_LENGTH] = "gpch.013";
97246247Savg  static const char c_language_chars[] = "Co+O";
98121982Sjhb
99121982Sjhb  memcpy (result, template, IDENT_LENGTH);
100133907Speter  result[4] = c_language_chars[c_language];
101133907Speter
102133907Speter  return result;
103133907Speter}
104133907Speter
105133907Speter/* Prepare to write a PCH file, if one is being written.  This is
106121982Sjhb   called at the start of compilation.
107121982Sjhb
108121982Sjhb   Also, print out the executable checksum if -fverbose-asm is in effect.  */
109121982Sjhb
110121982Sjhbvoid
111121982Sjhbpch_init (void)
112121982Sjhb{
113121982Sjhb  FILE *f;
114151658Sjhb  struct c_pch_validity v;
115121982Sjhb  void *target_validity;
116121982Sjhb  static const char partial_pch[IDENT_LENGTH] = "gpcWrite";
117121982Sjhb
118169391Sjhb#ifdef ASM_COMMENT_START
119121982Sjhb  if (flag_verbose_asm)
120121982Sjhb    {
121153241Sjhb      fprintf (asm_out_file, "%s ", ASM_COMMENT_START);
122121982Sjhb      c_common_print_pch_checksum (asm_out_file);
123188065Sjkoshy      fputc ('\n', asm_out_file);
124188065Sjkoshy    }
125188065Sjkoshy#endif
126188065Sjkoshy
127188065Sjkoshy  if (!pch_file)
128188065Sjkoshy    return;
129188065Sjkoshy
130188065Sjkoshy  f = fopen (pch_file, "w+b");
131188065Sjkoshy  if (f == NULL)
132121982Sjhb    fatal_error ("can%'t create precompiled header %s: %m", pch_file);
133140555Speter  pch_outfile = f;
134121982Sjhb
135163219Sjhb  gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
136163219Sjhb
137163219Sjhb  v.debug_info_type = write_symbols;
138129284Speter  {
139129284Speter    size_t i;
140129284Speter    for (i = 0; i < MATCH_SIZE; i++)
141129284Speter      {
142129284Speter	v.match[i] = *pch_matching[i].flag_var;
143241371Sattilio	gcc_assert (v.match[i] == *pch_matching[i].flag_var);
144167273Sjhb      }
145241371Sattilio  }
146166901Spiso  v.pch_init = &pch_init;
147166901Spiso  target_validity = targetm.get_pch_validity (&v.target_data_length);
148166901Spiso
149234989Sattilio  if (fwrite (partial_pch, IDENT_LENGTH, 1, f) != 1
150177181Sjhb      || fwrite (executable_checksum, 16, 1, f) != 1
151234989Sattilio      || fwrite (&v, sizeof (v), 1, f) != 1
152129284Speter      || fwrite (target_validity, v.target_data_length, 1, f) != 1)
153129284Speter    fatal_error ("can%'t write to %s: %m", pch_file);
154198134Sjhb
155153241Sjhb  /* We need to be able to re-read the output.  */
156194985Sjhb  /* The driver always provides a valid -o option.  */
157121982Sjhb  if (asm_file_name == NULL
158163219Sjhb      || strcmp (asm_file_name, "-") == 0)
159121982Sjhb    fatal_error ("%qs is not a valid output file", asm_file_name);
160121982Sjhb
161121982Sjhb  asm_file_startpos = ftell (asm_out_file);
162121982Sjhb
163140555Speter  /* Let the debugging format deal with the PCHness.  */
164169391Sjhb  (*debug_hooks->handle_pch) (0);
165169391Sjhb
166165127Sjhb  cpp_save_state (parse_in, f);
167169221Sjhb}
168164265Sjhb
169169391Sjhb/* Write the PCH file.  This is called at the end of a compilation which
170164265Sjhb   will produce a PCH file.  */
171121982Sjhb
172121982Sjhbvoid
173121982Sjhbc_common_write_pch (void)
174121982Sjhb{
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
227int
228c_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      cpp_error (pfile, CPP_DL_WARNING, "%s: too short to be a PCH file",
247		 name);
248      return 2;
249    }
250
251  pch_ident = get_ident();
252  if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0)
253    {
254      if (cpp_get_options (pfile)->warn_invalid_pch)
255	{
256	  if (memcmp (ident, pch_ident, 5) == 0)
257	    /* It's a PCH, for the right language, but has the wrong version.
258	     */
259	    cpp_error (pfile, CPP_DL_WARNING,
260		       "%s: not compatible with this GCC version", name);
261	  else if (memcmp (ident, pch_ident, 4) == 0)
262	    /* It's a PCH for the wrong language.  */
263	    cpp_error (pfile, CPP_DL_WARNING, "%s: not for %s", name,
264		       lang_hooks.name);
265	  else
266	    /* Not any kind of PCH.  */
267	    cpp_error (pfile, CPP_DL_WARNING, "%s: not a PCH file", name);
268	}
269      return 2;
270    }
271  if (memcmp (ident + IDENT_LENGTH, executable_checksum, 16) != 0)
272    {
273      if (cpp_get_options (pfile)->warn_invalid_pch)
274	cpp_error (pfile, CPP_DL_WARNING,
275		   "%s: created by a different GCC executable", name);
276      return 2;
277    }
278
279  /* At this point, we know it's a PCH file created by this
280     executable, so it ought to be long enough that we can read a
281     c_pch_validity structure.  */
282  if (read (fd, &v, sizeof (v)) != sizeof (v))
283    fatal_error ("can%'t read %s: %m", name);
284
285  /* The allowable debug info combinations are that either the PCH file
286     was built with the same as is being used now, or the PCH file was
287     built for some kind of debug info but now none is in use.  */
288  if (v.debug_info_type != write_symbols
289      && write_symbols != NO_DEBUG)
290    {
291      if (cpp_get_options (pfile)->warn_invalid_pch)
292	cpp_error (pfile, CPP_DL_WARNING,
293		   "%s: created with -g%s, but used with -g%s", name,
294		   debug_type_names[v.debug_info_type],
295		   debug_type_names[write_symbols]);
296      return 2;
297    }
298
299  /* Check flags that must match exactly.  */
300  {
301    size_t i;
302    for (i = 0; i < MATCH_SIZE; i++)
303      if (*pch_matching[i].flag_var != v.match[i])
304	{
305	  if (cpp_get_options (pfile)->warn_invalid_pch)
306	    cpp_error (pfile, CPP_DL_WARNING,
307		       "%s: settings for %s do not match", name,
308		       pch_matching[i].flag_name);
309	  return 2;
310	}
311  }
312
313  /* If the text segment was not loaded at the same address as it was
314     when the PCH file was created, function pointers loaded from the
315     PCH will not be valid.  We could in theory remap all the function
316     pointers, but no support for that exists at present.
317     Since we have the same executable, it should only be necessary to
318     check one function.  */
319  if (v.pch_init != &pch_init)
320    {
321      if (cpp_get_options (pfile)->warn_invalid_pch)
322	cpp_error (pfile, CPP_DL_WARNING,
323		   "%s: had text segment at different address", name);
324      return 2;
325    }
326
327  /* Check the target-specific validity data.  */
328  {
329    void *this_file_data = xmalloc (v.target_data_length);
330    const char *msg;
331
332    if ((size_t) read (fd, this_file_data, v.target_data_length)
333	!= v.target_data_length)
334      fatal_error ("can%'t read %s: %m", name);
335    msg = targetm.pch_valid_p (this_file_data, v.target_data_length);
336    free (this_file_data);
337    if (msg != NULL)
338      {
339	if (cpp_get_options (pfile)->warn_invalid_pch)
340	  cpp_error (pfile, CPP_DL_WARNING, "%s: %s", name, msg);
341	return 2;
342      }
343  }
344
345  /* Check the preprocessor macros are the same as when the PCH was
346     generated.  */
347
348  result = cpp_valid_state (pfile, name, fd);
349  if (result == -1)
350    return 2;
351  else
352    return result == 0;
353}
354
355/* If non-NULL, this function is called after a precompile header file
356   is loaded.  */
357void (*lang_post_pch_load) (void);
358
359/* Load in the PCH file NAME, open on FD.  It was originally searched for
360   by ORIG_NAME.  */
361
362void
363c_common_read_pch (cpp_reader *pfile, const char *name,
364		   int fd, const char *orig_name ATTRIBUTE_UNUSED)
365{
366  FILE *f;
367  struct c_pch_header h;
368  struct save_macro_data *smd;
369
370  f = fdopen (fd, "rb");
371  if (f == NULL)
372    {
373      cpp_errno (pfile, CPP_DL_ERROR, "calling fdopen");
374      return;
375    }
376
377  cpp_get_callbacks (parse_in)->valid_pch = NULL;
378
379  if (fread (&h, sizeof (h), 1, f) != 1)
380    {
381      cpp_errno (pfile, CPP_DL_ERROR, "reading");
382      return;
383    }
384
385  if (!flag_preprocess_only)
386    {
387      unsigned long written;
388      char * buf = XNEWVEC (char, 16384);
389
390      for (written = 0; written < h.asm_size; )
391	{
392	  long size = h.asm_size - written;
393	  if (size > 16384)
394	    size = 16384;
395	  if (fread (buf, size, 1, f) != 1
396	      || fwrite (buf, size, 1, asm_out_file) != 1)
397	    cpp_errno (pfile, CPP_DL_ERROR, "reading");
398	  written += size;
399	}
400      free (buf);
401    }
402  else
403    {
404      /* If we're preprocessing, don't write to a NULL
405	 asm_out_file.  */
406      if (fseek (f, h.asm_size, SEEK_CUR) != 0)
407	cpp_errno (pfile, CPP_DL_ERROR, "seeking");
408    }
409
410  cpp_prepare_state (pfile, &smd);
411
412  gt_pch_restore (f);
413
414  if (cpp_read_state (pfile, name, f, smd) != 0)
415    return;
416
417  fclose (f);
418
419  /* Give the front end a chance to take action after a PCH file has
420     been loaded.  */
421  if (lang_post_pch_load)
422    (*lang_post_pch_load) ();
423}
424
425/* Indicate that no more PCH files should be read.  */
426
427void
428c_common_no_more_pch (void)
429{
430  if (cpp_get_callbacks (parse_in)->valid_pch)
431    {
432      cpp_get_callbacks (parse_in)->valid_pch = NULL;
433      host_hooks.gt_pch_use_address (NULL, 0, -1, 0);
434    }
435}
436
437/* Handle #pragma GCC pch_preprocess, to load in the PCH file.  */
438
439#ifndef O_BINARY
440# define O_BINARY 0
441#endif
442
443void
444c_common_pch_pragma (cpp_reader *pfile, const char *name)
445{
446  int fd;
447
448  if (!cpp_get_options (pfile)->preprocessed)
449    {
450      error ("pch_preprocess pragma should only be used with -fpreprocessed");
451      inform ("use #include instead");
452      return;
453    }
454
455  fd = open (name, O_RDONLY | O_BINARY, 0666);
456  if (fd == -1)
457    fatal_error ("%s: couldn%'t open PCH file: %m", name);
458
459  if (c_common_valid_pch (pfile, name, fd) != 1)
460    {
461      if (!cpp_get_options (pfile)->warn_invalid_pch)
462	inform ("use -Winvalid-pch for more information");
463      fatal_error ("%s: PCH file was invalid", name);
464    }
465
466  c_common_read_pch (pfile, name, fd, name);
467
468  close (fd);
469}
470
471/* Print out executable_checksum[].  */
472
473void
474c_common_print_pch_checksum (FILE *f)
475{
476  int i;
477  fputs ("Compiler executable checksum: ", f);
478  for (i = 0; i < 16; i++)
479    fprintf (f, "%02x", executable_checksum[i]);
480  putc ('\n', f);
481}
482