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