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