]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-pch.c
2015-06-17 Andrew MacLeod <amacleod@redhat.com>
[thirdparty/gcc.git] / gcc / c-family / c-pch.c
CommitLineData
573aba85 1/* Precompiled header implementation for the C languages.
d353bf18 2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
573aba85 3
5a8b6e6a 4This file is part of GCC.
573aba85 5
5a8b6e6a 6GCC is free software; you can redistribute it and/or modify
573aba85 7it under the terms of the GNU General Public License as published by
8c4c00c1 8the Free Software Foundation; either version 3, or (at your option)
573aba85 9any later version.
10
5a8b6e6a 11GCC is distributed in the hope that it will be useful,
573aba85 12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
8c4c00c1 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
573aba85 19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
451b3797 23#include "version.h"
573aba85 24#include "cpplib.h"
b20a8bb4 25#include "options.h"
b20a8bb4 26#include "alias.h"
27#include "symtab.h"
573aba85 28#include "tree.h"
cc4ca723 29#include "flags.h"
573aba85 30#include "c-common.h"
573aba85 31#include "debug.h"
32#include "c-pragma.h"
6090deb8 33#include "langhooks.h"
ddf4604f 34#include "hosthooks.h"
7988d6e2 35#include "target.h"
931b0a0f 36#include "opts.h"
dbb19e66 37#include "timevar.h"
573aba85 38
a08be054 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
a0c938f0 43static const struct c_pch_matching
a08be054 44{
45 int *flag_var;
46 const char *flag_name;
47} pch_matching[] = {
48 { &flag_exceptions, "-fexceptions" },
a08be054 49};
50
51enum {
52 MATCH_SIZE = ARRAY_SIZE (pch_matching)
53};
54
f1035767 55/* The value of the checksum in the dummy compiler that is actually
56 checksummed. That compiler should never be run. */
57static const char no_checksum[16] = { 0 };
451b3797 58
f1035767 59/* Information about flags and suchlike that affect PCH validity.
7988d6e2 60
f1035767 61 Before this structure is read, both an initial 8-character identification
62 string, and a 16-byte checksum, have been read and validated. */
451b3797 63
cc4ca723 64struct c_pch_validity
65{
66 unsigned char debug_info_type;
a08be054 67 signed char match[MATCH_SIZE];
16c8d9c8 68 void (*pch_init) (void);
7988d6e2 69 size_t target_data_length;
cc4ca723 70};
71
6090deb8 72#define IDENT_LENGTH 8
573aba85 73
451b3797 74/* The file we'll be writing the PCH to. */
573aba85 75static FILE *pch_outfile;
76
2b30d46c 77static const char *get_ident (void);
6090deb8 78
cc4ca723 79/* Compute an appropriate 8-byte magic number for the PCH file, so that
80 utilities like file(1) can identify it, and so that GCC can quickly
81 ignore non-PCH files and PCH files that are of a completely different
82 format. */
83
6090deb8 84static const char *
84166705 85get_ident (void)
6090deb8 86{
87 static char result[IDENT_LENGTH];
3169c57a 88 static const char templ[] = "gpch.014";
c0f19401 89 static const char c_language_chars[] = "Co+O";
a0c938f0 90
c32319fc 91 memcpy (result, templ, IDENT_LENGTH);
c0f19401 92 result[4] = c_language_chars[c_language];
93
6090deb8 94 return result;
95}
96
6adc88f8 97/* Whether preprocessor state should be saved by pch_init. */
98
99static bool pch_ready_to_save_cpp_state = false;
100
f1035767 101/* Prepare to write a PCH file, if one is being written. This is
c2a65b90 102 called at the start of compilation. */
cc4ca723 103
573aba85 104void
2b30d46c 105pch_init (void)
573aba85 106{
107 FILE *f;
cc4ca723 108 struct c_pch_validity v;
7988d6e2 109 void *target_validity;
019c559b 110 static const char partial_pch[] = "gpcWrite";
a0c938f0 111
84166705 112 if (!pch_file)
cc4ca723 113 return;
a0c938f0 114
cc4ca723 115 f = fopen (pch_file, "w+b");
116 if (f == NULL)
c05be867 117 fatal_error (input_location, "can%'t create precompiled header %s: %m",
118 pch_file);
cc4ca723 119 pch_outfile = f;
231bd014 120
f1035767 121 gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
a0c938f0 122
b2c1582f 123 memset (&v, '\0', sizeof (v));
cc4ca723 124 v.debug_info_type = write_symbols;
a08be054 125 {
126 size_t i;
127 for (i = 0; i < MATCH_SIZE; i++)
128 {
129 v.match[i] = *pch_matching[i].flag_var;
231bd014 130 gcc_assert (v.match[i] == *pch_matching[i].flag_var);
a08be054 131 }
132 }
16c8d9c8 133 v.pch_init = &pch_init;
7988d6e2 134 target_validity = targetm.get_pch_validity (&v.target_data_length);
a0c938f0 135
be5cb0d1 136 if (fwrite (partial_pch, IDENT_LENGTH, 1, f) != 1
f1035767 137 || fwrite (executable_checksum, 16, 1, f) != 1
451b3797 138 || fwrite (&v, sizeof (v), 1, f) != 1
7988d6e2 139 || fwrite (target_validity, v.target_data_length, 1, f) != 1)
c05be867 140 fatal_error (input_location, "can%'t write to %s: %m", pch_file);
cc4ca723 141
cc4ca723 142 /* Let the debugging format deal with the PCHness. */
143 (*debug_hooks->handle_pch) (0);
a0c938f0 144
6adc88f8 145 if (pch_ready_to_save_cpp_state)
146 pch_cpp_save_state ();
5ceebb21 147
148 XDELETE (target_validity);
6adc88f8 149}
150
151/* Whether preprocessor state has been saved in a PCH file. */
152
153static bool pch_cpp_state_saved = false;
154
155/* Save preprocessor state in a PCH file, after implicitly included
156 headers have been read. If the PCH file has not yet been opened,
157 record that state should be saved when it is opened. */
158
159void
160pch_cpp_save_state (void)
161{
162 if (!pch_cpp_state_saved)
163 {
164 if (pch_outfile)
165 {
166 cpp_save_state (parse_in, pch_outfile);
167 pch_cpp_state_saved = true;
168 }
169 else
170 pch_ready_to_save_cpp_state = true;
171 }
573aba85 172}
173
cc4ca723 174/* Write the PCH file. This is called at the end of a compilation which
175 will produce a PCH file. */
176
573aba85 177void
2b30d46c 178c_common_write_pch (void)
573aba85 179{
dbb19e66 180 timevar_push (TV_PCH_SAVE);
181
e5d92c9b 182 targetm.prepare_pch_save ();
183
cf8e41a4 184 (*debug_hooks->handle_pch) (1);
185
6e5c480b 186 prepare_target_option_nodes_for_pch ();
187
573aba85 188 cpp_write_pch_deps (parse_in, pch_outfile);
189
573aba85 190 gt_pch_save (pch_outfile);
dbb19e66 191
192 timevar_push (TV_PCH_CPP_SAVE);
573aba85 193 cpp_write_pch_state (parse_in, pch_outfile);
dbb19e66 194 timevar_pop (TV_PCH_CPP_SAVE);
573aba85 195
be5cb0d1 196 if (fseek (pch_outfile, 0, SEEK_SET) != 0
197 || fwrite (get_ident (), IDENT_LENGTH, 1, pch_outfile) != 1)
c05be867 198 fatal_error (input_location, "can%'t write %s: %m", pch_file);
be5cb0d1 199
573aba85 200 fclose (pch_outfile);
dbb19e66 201
202 timevar_pop (TV_PCH_SAVE);
573aba85 203}
204
7988d6e2 205/* Check the PCH file called NAME, open on FD, to see if it can be
206 used in this compilation. Return 1 if valid, 0 if the file can't
207 be used now but might be if it's seen later in the compilation, and
208 2 if this file could never be used in the compilation. */
cc4ca723 209
573aba85 210int
2b30d46c 211c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
573aba85 212{
213 int sizeread;
214 int result;
f1035767 215 char ident[IDENT_LENGTH + 16];
6090deb8 216 const char *pch_ident;
cc4ca723 217 struct c_pch_validity v;
573aba85 218
573aba85 219 /* Perform a quick test of whether this is a valid
cc4ca723 220 precompiled header for the current language. */
573aba85 221
f1035767 222 gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
223
224 sizeread = read (fd, ident, IDENT_LENGTH + 16);
573aba85 225 if (sizeread == -1)
c05be867 226 fatal_error (input_location, "can%'t read %s: %m", name);
f1035767 227 else if (sizeread != IDENT_LENGTH + 16)
228 {
c12ee0c6 229 if (cpp_get_options (pfile)->warn_invalid_pch)
230 cpp_error (pfile, CPP_DL_WARNING, "%s: too short to be a PCH file",
231 name);
f1035767 232 return 2;
233 }
a0c938f0 234
6090deb8 235 pch_ident = get_ident();
236 if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0)
573aba85 237 {
238 if (cpp_get_options (pfile)->warn_invalid_pch)
239 {
240 if (memcmp (ident, pch_ident, 5) == 0)
241 /* It's a PCH, for the right language, but has the wrong version.
242 */
a0c938f0 243 cpp_error (pfile, CPP_DL_WARNING,
573aba85 244 "%s: not compatible with this GCC version", name);
245 else if (memcmp (ident, pch_ident, 4) == 0)
246 /* It's a PCH for the wrong language. */
d80d2074 247 cpp_error (pfile, CPP_DL_WARNING, "%s: not for %s", name,
6090deb8 248 lang_hooks.name);
a0c938f0 249 else
573aba85 250 /* Not any kind of PCH. */
d80d2074 251 cpp_error (pfile, CPP_DL_WARNING, "%s: not a PCH file", name);
573aba85 252 }
253 return 2;
254 }
f1035767 255 if (memcmp (ident + IDENT_LENGTH, executable_checksum, 16) != 0)
451b3797 256 {
257 if (cpp_get_options (pfile)->warn_invalid_pch)
d80d2074 258 cpp_error (pfile, CPP_DL_WARNING,
f1035767 259 "%s: created by a different GCC executable", name);
451b3797 260 return 2;
261 }
262
f1035767 263 /* At this point, we know it's a PCH file created by this
264 executable, so it ought to be long enough that we can read a
265 c_pch_validity structure. */
266 if (read (fd, &v, sizeof (v)) != sizeof (v))
c05be867 267 fatal_error (input_location, "can%'t read %s: %m", name);
f1035767 268
cc4ca723 269 /* The allowable debug info combinations are that either the PCH file
270 was built with the same as is being used now, or the PCH file was
271 built for some kind of debug info but now none is in use. */
272 if (v.debug_info_type != write_symbols
273 && write_symbols != NO_DEBUG)
274 {
275 if (cpp_get_options (pfile)->warn_invalid_pch)
a0c938f0 276 cpp_error (pfile, CPP_DL_WARNING,
cc4ca723 277 "%s: created with -g%s, but used with -g%s", name,
278 debug_type_names[v.debug_info_type],
279 debug_type_names[write_symbols]);
280 return 2;
281 }
282
a08be054 283 /* Check flags that must match exactly. */
284 {
285 size_t i;
286 for (i = 0; i < MATCH_SIZE; i++)
287 if (*pch_matching[i].flag_var != v.match[i])
288 {
289 if (cpp_get_options (pfile)->warn_invalid_pch)
a0c938f0 290 cpp_error (pfile, CPP_DL_WARNING,
a08be054 291 "%s: settings for %s do not match", name,
292 pch_matching[i].flag_name);
293 return 2;
294 }
295 }
296
16c8d9c8 297 /* If the text segment was not loaded at the same address as it was
298 when the PCH file was created, function pointers loaded from the
299 PCH will not be valid. We could in theory remap all the function
a0c938f0 300 pointers, but no support for that exists at present.
f1035767 301 Since we have the same executable, it should only be necessary to
302 check one function. */
16c8d9c8 303 if (v.pch_init != &pch_init)
304 {
305 if (cpp_get_options (pfile)->warn_invalid_pch)
a0c938f0 306 cpp_error (pfile, CPP_DL_WARNING,
16c8d9c8 307 "%s: had text segment at different address", name);
308 return 2;
309 }
310
7988d6e2 311 /* Check the target-specific validity data. */
312 {
313 void *this_file_data = xmalloc (v.target_data_length);
314 const char *msg;
a0c938f0 315
7988d6e2 316 if ((size_t) read (fd, this_file_data, v.target_data_length)
317 != v.target_data_length)
c05be867 318 fatal_error (input_location, "can%'t read %s: %m", name);
7988d6e2 319 msg = targetm.pch_valid_p (this_file_data, v.target_data_length);
320 free (this_file_data);
321 if (msg != NULL)
322 {
323 if (cpp_get_options (pfile)->warn_invalid_pch)
d80d2074 324 cpp_error (pfile, CPP_DL_WARNING, "%s: %s", name, msg);
7988d6e2 325 return 2;
326 }
327 }
328
573aba85 329 /* Check the preprocessor macros are the same as when the PCH was
330 generated. */
a0c938f0 331
573aba85 332 result = cpp_valid_state (pfile, name, fd);
333 if (result == -1)
334 return 2;
335 else
336 return result == 0;
337}
338
caa6fdce 339/* If non-NULL, this function is called after a precompile header file
340 is loaded. */
341void (*lang_post_pch_load) (void);
342
cc4ca723 343/* Load in the PCH file NAME, open on FD. It was originally searched for
344 by ORIG_NAME. */
345
573aba85 346void
2b30d46c 347c_common_read_pch (cpp_reader *pfile, const char *name,
348 int fd, const char *orig_name ATTRIBUTE_UNUSED)
573aba85 349{
350 FILE *f;
573aba85 351 struct save_macro_data *smd;
931b0a0f 352 expanded_location saved_loc;
7f1c321c 353 bool saved_trace_includes;
a0c938f0 354
dbb19e66 355 timevar_push (TV_PCH_RESTORE);
356
573aba85 357 f = fdopen (fd, "rb");
358 if (f == NULL)
359 {
d80d2074 360 cpp_errno (pfile, CPP_DL_ERROR, "calling fdopen");
f2225b1a 361 close (fd);
dbb19e66 362 goto end;
573aba85 363 }
364
ddf4604f 365 cpp_get_callbacks (parse_in)->valid_pch = NULL;
573aba85 366
931b0a0f 367 /* Save the location and then restore it after reading the PCH. */
931b0a0f 368 saved_loc = expand_location (line_table->highest_line);
7f1c321c 369 saved_trace_includes = line_table->trace_includes;
931b0a0f 370
dbb19e66 371 timevar_push (TV_PCH_CPP_RESTORE);
573aba85 372 cpp_prepare_state (pfile, &smd);
dbb19e66 373 timevar_pop (TV_PCH_CPP_RESTORE);
573aba85 374
375 gt_pch_restore (f);
915e0b4d 376 cpp_set_line_map (pfile, line_table);
5c1946c8 377 rebuild_location_adhoc_htab (line_table);
573aba85 378
dbb19e66 379 timevar_push (TV_PCH_CPP_RESTORE);
573aba85 380 if (cpp_read_state (pfile, name, f, smd) != 0)
f2225b1a 381 {
382 fclose (f);
dbb19e66 383 timevar_pop (TV_PCH_CPP_RESTORE);
384 goto end;
f2225b1a 385 }
dbb19e66 386 timevar_pop (TV_PCH_CPP_RESTORE);
387
573aba85 388
389 fclose (f);
a0c938f0 390
7f1c321c 391 line_table->trace_includes = saved_trace_includes;
1dc92c59 392 linemap_add (line_table, LC_ENTER, 0, saved_loc.file, saved_loc.line);
931b0a0f 393
caa6fdce 394 /* Give the front end a chance to take action after a PCH file has
597ff315 395 been loaded. */
caa6fdce 396 if (lang_post_pch_load)
397 (*lang_post_pch_load) ();
dbb19e66 398
399end:
400 timevar_pop (TV_PCH_RESTORE);
573aba85 401}
ddf4604f 402
403/* Indicate that no more PCH files should be read. */
404
405void
406c_common_no_more_pch (void)
407{
408 if (cpp_get_callbacks (parse_in)->valid_pch)
409 {
410 cpp_get_callbacks (parse_in)->valid_pch = NULL;
53ee4dac 411 host_hooks.gt_pch_use_address (NULL, 0, -1, 0);
ddf4604f 412 }
413}
d718b525 414
415/* Handle #pragma GCC pch_preprocess, to load in the PCH file. */
416
d718b525 417void
b75b98aa 418c_common_pch_pragma (cpp_reader *pfile, const char *name)
d718b525 419{
d718b525 420 int fd;
421
84166705 422 if (!cpp_get_options (pfile)->preprocessed)
d718b525 423 {
424 error ("pch_preprocess pragma should only be used with -fpreprocessed");
5bcc316e 425 inform (input_location, "use #include instead");
d718b525 426 return;
427 }
428
d718b525 429 fd = open (name, O_RDONLY | O_BINARY, 0666);
430 if (fd == -1)
c05be867 431 fatal_error (input_location, "%s: couldn%'t open PCH file: %m", name);
a0c938f0 432
d718b525 433 if (c_common_valid_pch (pfile, name, fd) != 1)
434 {
435 if (!cpp_get_options (pfile)->warn_invalid_pch)
5bcc316e 436 inform (input_location, "use -Winvalid-pch for more information");
c05be867 437 fatal_error (input_location, "%s: PCH file was invalid", name);
d718b525 438 }
a0c938f0 439
d718b525 440 c_common_read_pch (pfile, name, fd, name);
a0c938f0 441
d718b525 442 close (fd);
443}
f1035767 444