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