1 /* Precompiled header implementation for the C languages.
2 Copyright (C) 2000-2021 Free Software Foundation, Inc.
4 This file is part of GCC.
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)
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.
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/>. */
22 #include "coretypes.h"
29 #include "langhooks.h"
30 #include "hosthooks.h"
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'. */
36 static const struct c_pch_matching
39 const char *flag_name
;
41 { &flag_exceptions
, "-fexceptions" },
45 MATCH_SIZE
= ARRAY_SIZE (pch_matching
)
48 /* Information about flags and suchlike that affect PCH validity.
50 Before this structure is read, both an initial 8-character identification
51 string, and a 16-byte checksum, have been read and validated. */
55 uint32_t pch_write_symbols
;
56 signed char match
[MATCH_SIZE
];
57 void (*pch_init
) (void);
58 size_t target_data_length
;
61 #define IDENT_LENGTH 8
63 /* The file we'll be writing the PCH to. */
64 static FILE *pch_outfile
;
66 static const char *get_ident (void);
68 /* Compute an appropriate 8-byte magic number for the PCH file, so that
69 utilities like file(1) can identify it, and so that GCC can quickly
70 ignore non-PCH files and PCH files that are of a completely different
76 static char result
[IDENT_LENGTH
];
77 static const char templ
[] = "gpch.014";
78 static const char c_language_chars
[] = "Co+O";
80 memcpy (result
, templ
, IDENT_LENGTH
);
81 result
[4] = c_language_chars
[c_language
];
86 /* Whether preprocessor state should be saved by pch_init. */
88 static bool pch_ready_to_save_cpp_state
= false;
90 /* Prepare to write a PCH file, if one is being written. This is
91 called at the start of compilation. */
97 struct c_pch_validity v
;
98 void *target_validity
;
99 static const char partial_pch
[] = "gpcWrite";
104 f
= fopen (pch_file
, "w+b");
106 fatal_error (input_location
, "cannot create precompiled header %s: %m",
110 memset (&v
, '\0', sizeof (v
));
111 v
.pch_write_symbols
= write_symbols
;
114 for (i
= 0; i
< MATCH_SIZE
; i
++)
116 v
.match
[i
] = *pch_matching
[i
].flag_var
;
117 gcc_assert (v
.match
[i
] == *pch_matching
[i
].flag_var
);
120 v
.pch_init
= &pch_init
;
121 target_validity
= targetm
.get_pch_validity (&v
.target_data_length
);
123 if (fwrite (partial_pch
, IDENT_LENGTH
, 1, f
) != 1
124 || fwrite (executable_checksum
, 16, 1, f
) != 1
125 || fwrite (&v
, sizeof (v
), 1, f
) != 1
126 || fwrite (target_validity
, v
.target_data_length
, 1, f
) != 1)
127 fatal_error (input_location
, "cannot write to %s: %m", pch_file
);
129 /* Let the debugging format deal with the PCHness. */
130 (*debug_hooks
->handle_pch
) (0);
132 if (pch_ready_to_save_cpp_state
)
133 pch_cpp_save_state ();
135 XDELETE (target_validity
);
138 /* Whether preprocessor state has been saved in a PCH file. */
140 static bool pch_cpp_state_saved
= false;
142 /* Save preprocessor state in a PCH file, after implicitly included
143 headers have been read. If the PCH file has not yet been opened,
144 record that state should be saved when it is opened. */
147 pch_cpp_save_state (void)
149 if (!pch_cpp_state_saved
)
153 cpp_save_state (parse_in
, pch_outfile
);
154 pch_cpp_state_saved
= true;
157 pch_ready_to_save_cpp_state
= true;
161 /* Write the PCH file. This is called at the end of a compilation which
162 will produce a PCH file. */
165 c_common_write_pch (void)
167 timevar_push (TV_PCH_SAVE
);
169 targetm
.prepare_pch_save ();
171 (*debug_hooks
->handle_pch
) (1);
173 prepare_target_option_nodes_for_pch ();
175 cpp_write_pch_deps (parse_in
, pch_outfile
);
177 gt_pch_save (pch_outfile
);
179 timevar_push (TV_PCH_CPP_SAVE
);
180 cpp_write_pch_state (parse_in
, pch_outfile
);
181 timevar_pop (TV_PCH_CPP_SAVE
);
183 if (fseek (pch_outfile
, 0, SEEK_SET
) != 0
184 || fwrite (get_ident (), IDENT_LENGTH
, 1, pch_outfile
) != 1)
185 fatal_error (input_location
, "cannot write %s: %m", pch_file
);
187 fclose (pch_outfile
);
189 timevar_pop (TV_PCH_SAVE
);
192 /* Check the PCH file called NAME, open on FD, to see if it can be
193 used in this compilation. Return 1 if valid, 0 if the file can't
194 be used now but might be if it's seen later in the compilation, and
195 2 if this file could never be used in the compilation. */
198 c_common_valid_pch (cpp_reader
*pfile
, const char *name
, int fd
)
202 char ident
[IDENT_LENGTH
+ 16];
203 const char *pch_ident
;
204 struct c_pch_validity v
;
206 /* Perform a quick test of whether this is a valid
207 precompiled header for the current language. */
209 /* C++ modules and PCH don't play together. */
213 sizeread
= read (fd
, ident
, IDENT_LENGTH
+ 16);
215 fatal_error (input_location
, "cannot read %s: %m", name
);
216 else if (sizeread
!= IDENT_LENGTH
+ 16)
218 cpp_warning (pfile
, CPP_W_INVALID_PCH
, "%s: too short to be a PCH file",
223 pch_ident
= get_ident();
224 if (memcmp (ident
, pch_ident
, IDENT_LENGTH
) != 0)
226 if (memcmp (ident
, pch_ident
, 5) == 0)
227 /* It's a PCH, for the right language, but has the wrong version. */
228 cpp_warning (pfile
, CPP_W_INVALID_PCH
,
229 "%s: not compatible with this GCC version", name
);
230 else if (memcmp (ident
, pch_ident
, 4) == 0)
231 /* It's a PCH for the wrong language. */
232 cpp_warning (pfile
, CPP_W_INVALID_PCH
, "%s: not for %s", name
,
235 /* Not any kind of PCH. */
236 cpp_warning (pfile
, CPP_W_INVALID_PCH
, "%s: not a PCH file", name
);
239 if (memcmp (ident
+ IDENT_LENGTH
, executable_checksum
, 16) != 0)
241 cpp_warning (pfile
, CPP_W_INVALID_PCH
,
242 "%s: created by a different GCC executable", name
);
246 /* At this point, we know it's a PCH file created by this
247 executable, so it ought to be long enough that we can read a
248 c_pch_validity structure. */
249 if (read (fd
, &v
, sizeof (v
)) != sizeof (v
))
250 fatal_error (input_location
, "cannot read %s: %m", name
);
252 /* The allowable debug info combinations are that either the PCH file
253 was built with the same as is being used now, or the PCH file was
254 built for some kind of debug info but now none is in use. */
255 if (v
.pch_write_symbols
!= write_symbols
256 && write_symbols
!= NO_DEBUG
)
258 char *created_str
= xstrdup (debug_set_names (v
.pch_write_symbols
));
259 char *used_str
= xstrdup (debug_set_names (write_symbols
));
260 cpp_warning (pfile
, CPP_W_INVALID_PCH
,
261 "%s: created with '%s' debug info, but used with '%s'", name
,
262 created_str
, used_str
);
268 /* Check flags that must match exactly. */
271 for (i
= 0; i
< MATCH_SIZE
; i
++)
272 if (*pch_matching
[i
].flag_var
!= v
.match
[i
])
274 cpp_warning (pfile
, CPP_W_INVALID_PCH
,
275 "%s: settings for %s do not match", name
,
276 pch_matching
[i
].flag_name
);
281 /* If the text segment was not loaded at the same address as it was
282 when the PCH file was created, function pointers loaded from the
283 PCH will not be valid. We could in theory remap all the function
284 pointers, but no support for that exists at present.
285 Since we have the same executable, it should only be necessary to
286 check one function. */
287 if (v
.pch_init
!= &pch_init
)
289 cpp_warning (pfile
, CPP_W_INVALID_PCH
,
290 "%s: had text segment at different address", name
);
294 /* Check the target-specific validity data. */
296 void *this_file_data
= xmalloc (v
.target_data_length
);
299 if ((size_t) read (fd
, this_file_data
, v
.target_data_length
)
300 != v
.target_data_length
)
301 fatal_error (input_location
, "cannot read %s: %m", name
);
302 msg
= targetm
.pch_valid_p (this_file_data
, v
.target_data_length
);
303 free (this_file_data
);
306 cpp_warning (pfile
, CPP_W_INVALID_PCH
, "%s: %s", name
, msg
);
311 /* Check the preprocessor macros are the same as when the PCH was
314 result
= cpp_valid_state (pfile
, name
, fd
);
321 /* If non-NULL, this function is called after a precompile header file
323 void (*lang_post_pch_load
) (void);
325 /* Load in the PCH file NAME, open on FD. It was originally searched for
329 c_common_read_pch (cpp_reader
*pfile
, const char *name
,
330 int fd
, const char *orig_name ATTRIBUTE_UNUSED
)
333 struct save_macro_data
*smd
;
334 expanded_location saved_loc
;
335 bool saved_trace_includes
;
337 timevar_push (TV_PCH_RESTORE
);
339 f
= fdopen (fd
, "rb");
342 cpp_errno (pfile
, CPP_DL_ERROR
, "calling fdopen");
347 cpp_get_callbacks (parse_in
)->valid_pch
= NULL
;
349 /* Save the location and then restore it after reading the PCH. */
350 saved_loc
= expand_location (line_table
->highest_line
);
351 saved_trace_includes
= line_table
->trace_includes
;
353 timevar_push (TV_PCH_CPP_RESTORE
);
354 cpp_prepare_state (pfile
, &smd
);
355 timevar_pop (TV_PCH_CPP_RESTORE
);
358 cpp_set_line_map (pfile
, line_table
);
359 rebuild_location_adhoc_htab (line_table
);
361 timevar_push (TV_PCH_CPP_RESTORE
);
362 if (cpp_read_state (pfile
, name
, f
, smd
) != 0)
365 timevar_pop (TV_PCH_CPP_RESTORE
);
368 timevar_pop (TV_PCH_CPP_RESTORE
);
373 line_table
->trace_includes
= saved_trace_includes
;
374 linemap_add (line_table
, LC_ENTER
, 0, saved_loc
.file
, saved_loc
.line
);
376 /* Give the front end a chance to take action after a PCH file has
378 if (lang_post_pch_load
)
379 (*lang_post_pch_load
) ();
382 timevar_pop (TV_PCH_RESTORE
);
385 /* Indicate that no more PCH files should be read. */
388 c_common_no_more_pch (void)
390 if (cpp_get_callbacks (parse_in
)->valid_pch
)
392 cpp_get_callbacks (parse_in
)->valid_pch
= NULL
;
393 host_hooks
.gt_pch_use_address (NULL
, 0, -1, 0);
397 /* Handle #pragma GCC pch_preprocess, to load in the PCH file. */
400 c_common_pch_pragma (cpp_reader
*pfile
, const char *name
)
404 if (!cpp_get_options (pfile
)->preprocessed
)
406 error ("%<pch_preprocess%> pragma should only be used "
407 "with %<-fpreprocessed%>");
408 inform (input_location
, "use %<#include%> instead");
412 fd
= open (name
, O_RDONLY
| O_BINARY
, 0666);
414 fatal_error (input_location
, "%s: couldn%'t open PCH file: %m", name
);
416 if (c_common_valid_pch (pfile
, name
, fd
) != 1)
418 if (!cpp_get_options (pfile
)->warn_invalid_pch
)
419 inform (input_location
, "use %<-Winvalid-pch%> for more information");
420 fatal_error (input_location
, "%s: PCH file was invalid", name
);
423 c_common_read_pch (pfile
, name
, fd
, name
);