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