1132718Skan/* Precompiled header implementation for the C languages. 2169689Skan Copyright (C) 2000, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. 3132718Skan 4132718SkanThis file is part of GCC. 5132718Skan 6132718SkanGCC is free software; you can redistribute it and/or modify 7132718Skanit under the terms of the GNU General Public License as published by 8132718Skanthe Free Software Foundation; either version 2, or (at your option) 9132718Skanany later version. 10132718Skan 11132718SkanGCC is distributed in the hope that it will be useful, 12132718Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of 13132718SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14132718SkanGNU General Public License for more details. 15132718Skan 16132718SkanYou should have received a copy of the GNU General Public License 17132718Skanalong with GCC; see the file COPYING. If not, write to 18169689Skanthe Free Software Foundation, 51 Franklin Street, Fifth Floor, 19169689SkanBoston, MA 02110-1301, USA. */ 20132718Skan 21132718Skan#include "config.h" 22132718Skan#include "system.h" 23132718Skan#include "coretypes.h" 24132718Skan#include "version.h" 25132718Skan#include "cpplib.h" 26132718Skan#include "tree.h" 27132718Skan#include "flags.h" 28132718Skan#include "c-common.h" 29132718Skan#include "output.h" 30132718Skan#include "toplev.h" 31132718Skan#include "debug.h" 32132718Skan#include "c-pragma.h" 33132718Skan#include "ggc.h" 34132718Skan#include "langhooks.h" 35132718Skan#include "hosthooks.h" 36132718Skan#include "target.h" 37132718Skan 38169689Skan/* This is a list of flag variables that must match exactly, and their 39169689Skan names for the error message. The possible values for *flag_var must 40169689Skan fit in a 'signed char'. */ 41132718Skan 42169689Skanstatic const struct c_pch_matching 43169689Skan{ 44169689Skan int *flag_var; 45169689Skan const char *flag_name; 46169689Skan} pch_matching[] = { 47169689Skan { &flag_exceptions, "-fexceptions" }, 48169689Skan { &flag_unit_at_a_time, "-funit-at-a-time" } 49169689Skan}; 50132718Skan 51169689Skanenum { 52169689Skan MATCH_SIZE = ARRAY_SIZE (pch_matching) 53169689Skan}; 54132718Skan 55169689Skan/* The value of the checksum in the dummy compiler that is actually 56169689Skan checksummed. That compiler should never be run. */ 57169689Skanstatic const char no_checksum[16] = { 0 }; 58132718Skan 59169689Skan/* Information about flags and suchlike that affect PCH validity. 60169689Skan 61169689Skan Before this structure is read, both an initial 8-character identification 62169689Skan string, and a 16-byte checksum, have been read and validated. */ 63169689Skan 64132718Skanstruct c_pch_validity 65132718Skan{ 66132718Skan unsigned char debug_info_type; 67169689Skan signed char match[MATCH_SIZE]; 68132718Skan void (*pch_init) (void); 69132718Skan size_t target_data_length; 70132718Skan}; 71132718Skan 72169689Skanstruct c_pch_header 73132718Skan{ 74132718Skan unsigned long asm_size; 75132718Skan}; 76132718Skan 77132718Skan#define IDENT_LENGTH 8 78132718Skan 79132718Skan/* The file we'll be writing the PCH to. */ 80132718Skanstatic FILE *pch_outfile; 81132718Skan 82132718Skan/* The position in the assembler output file when pch_init was called. */ 83132718Skanstatic long asm_file_startpos; 84132718Skan 85132718Skanstatic const char *get_ident (void); 86132718Skan 87132718Skan/* Compute an appropriate 8-byte magic number for the PCH file, so that 88132718Skan utilities like file(1) can identify it, and so that GCC can quickly 89132718Skan ignore non-PCH files and PCH files that are of a completely different 90132718Skan format. */ 91132718Skan 92132718Skanstatic const char * 93169689Skanget_ident (void) 94132718Skan{ 95132718Skan static char result[IDENT_LENGTH]; 96169689Skan static const char template[IDENT_LENGTH] = "gpch.013"; 97132718Skan static const char c_language_chars[] = "Co+O"; 98169689Skan 99132718Skan memcpy (result, template, IDENT_LENGTH); 100132718Skan result[4] = c_language_chars[c_language]; 101132718Skan 102132718Skan return result; 103132718Skan} 104132718Skan 105169689Skan/* Prepare to write a PCH file, if one is being written. This is 106169689Skan called at the start of compilation. 107132718Skan 108169689Skan Also, print out the executable checksum if -fverbose-asm is in effect. */ 109169689Skan 110132718Skanvoid 111132718Skanpch_init (void) 112132718Skan{ 113132718Skan FILE *f; 114132718Skan struct c_pch_validity v; 115132718Skan void *target_validity; 116132718Skan static const char partial_pch[IDENT_LENGTH] = "gpcWrite"; 117169689Skan 118169689Skan#ifdef ASM_COMMENT_START 119169689Skan if (flag_verbose_asm) 120169689Skan { 121169689Skan fprintf (asm_out_file, "%s ", ASM_COMMENT_START); 122169689Skan c_common_print_pch_checksum (asm_out_file); 123169689Skan fputc ('\n', asm_out_file); 124169689Skan } 125169689Skan#endif 126169689Skan 127169689Skan if (!pch_file) 128132718Skan return; 129132718Skan 130132718Skan f = fopen (pch_file, "w+b"); 131132718Skan if (f == NULL) 132169689Skan fatal_error ("can%'t create precompiled header %s: %m", pch_file); 133132718Skan pch_outfile = f; 134169689Skan 135169689Skan gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0); 136169689Skan 137132718Skan v.debug_info_type = write_symbols; 138169689Skan { 139169689Skan size_t i; 140169689Skan for (i = 0; i < MATCH_SIZE; i++) 141169689Skan { 142169689Skan v.match[i] = *pch_matching[i].flag_var; 143169689Skan gcc_assert (v.match[i] == *pch_matching[i].flag_var); 144169689Skan } 145169689Skan } 146132718Skan v.pch_init = &pch_init; 147132718Skan target_validity = targetm.get_pch_validity (&v.target_data_length); 148169689Skan 149132718Skan if (fwrite (partial_pch, IDENT_LENGTH, 1, f) != 1 150169689Skan || fwrite (executable_checksum, 16, 1, f) != 1 151132718Skan || fwrite (&v, sizeof (v), 1, f) != 1 152132718Skan || fwrite (target_validity, v.target_data_length, 1, f) != 1) 153169689Skan fatal_error ("can%'t write to %s: %m", pch_file); 154132718Skan 155132718Skan /* We need to be able to re-read the output. */ 156132718Skan /* The driver always provides a valid -o option. */ 157132718Skan if (asm_file_name == NULL 158132718Skan || strcmp (asm_file_name, "-") == 0) 159169689Skan fatal_error ("%qs is not a valid output file", asm_file_name); 160169689Skan 161132718Skan asm_file_startpos = ftell (asm_out_file); 162169689Skan 163132718Skan /* Let the debugging format deal with the PCHness. */ 164132718Skan (*debug_hooks->handle_pch) (0); 165169689Skan 166132718Skan cpp_save_state (parse_in, f); 167132718Skan} 168132718Skan 169132718Skan/* Write the PCH file. This is called at the end of a compilation which 170132718Skan will produce a PCH file. */ 171132718Skan 172132718Skanvoid 173132718Skanc_common_write_pch (void) 174132718Skan{ 175132718Skan char *buf; 176132718Skan long asm_file_end; 177132718Skan long written; 178132718Skan struct c_pch_header h; 179132718Skan 180132718Skan (*debug_hooks->handle_pch) (1); 181132718Skan 182132718Skan cpp_write_pch_deps (parse_in, pch_outfile); 183132718Skan 184132718Skan asm_file_end = ftell (asm_out_file); 185132718Skan h.asm_size = asm_file_end - asm_file_startpos; 186169689Skan 187132718Skan if (fwrite (&h, sizeof (h), 1, pch_outfile) != 1) 188169689Skan fatal_error ("can%'t write %s: %m", pch_file); 189132718Skan 190169689Skan buf = XNEWVEC (char, 16384); 191169689Skan 192132718Skan if (fseek (asm_out_file, asm_file_startpos, SEEK_SET) != 0) 193169689Skan fatal_error ("can%'t seek in %s: %m", asm_file_name); 194132718Skan 195132718Skan for (written = asm_file_startpos; written < asm_file_end; ) 196132718Skan { 197132718Skan long size = asm_file_end - written; 198132718Skan if (size > 16384) 199132718Skan size = 16384; 200132718Skan if (fread (buf, size, 1, asm_out_file) != 1) 201169689Skan fatal_error ("can%'t read %s: %m", asm_file_name); 202132718Skan if (fwrite (buf, size, 1, pch_outfile) != 1) 203169689Skan fatal_error ("can%'t write %s: %m", pch_file); 204132718Skan written += size; 205132718Skan } 206132718Skan free (buf); 207169689Skan /* asm_out_file can be written afterwards, so fseek to clear 208169689Skan _IOREAD flag. */ 209169689Skan if (fseek (asm_out_file, 0, SEEK_END) != 0) 210169689Skan fatal_error ("can%'t seek in %s: %m", asm_file_name); 211132718Skan 212132718Skan gt_pch_save (pch_outfile); 213132718Skan cpp_write_pch_state (parse_in, pch_outfile); 214132718Skan 215132718Skan if (fseek (pch_outfile, 0, SEEK_SET) != 0 216132718Skan || fwrite (get_ident (), IDENT_LENGTH, 1, pch_outfile) != 1) 217169689Skan fatal_error ("can%'t write %s: %m", pch_file); 218132718Skan 219132718Skan fclose (pch_outfile); 220132718Skan} 221132718Skan 222132718Skan/* Check the PCH file called NAME, open on FD, to see if it can be 223132718Skan used in this compilation. Return 1 if valid, 0 if the file can't 224132718Skan be used now but might be if it's seen later in the compilation, and 225132718Skan 2 if this file could never be used in the compilation. */ 226132718Skan 227132718Skanint 228132718Skanc_common_valid_pch (cpp_reader *pfile, const char *name, int fd) 229132718Skan{ 230132718Skan int sizeread; 231132718Skan int result; 232169689Skan char ident[IDENT_LENGTH + 16]; 233132718Skan const char *pch_ident; 234132718Skan struct c_pch_validity v; 235132718Skan 236132718Skan /* Perform a quick test of whether this is a valid 237169689Skan precompiled header for the current language. */ 238132718Skan 239169689Skan gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0); 240169689Skan 241169689Skan sizeread = read (fd, ident, IDENT_LENGTH + 16); 242132718Skan if (sizeread == -1) 243169689Skan fatal_error ("can%'t read %s: %m", name); 244169689Skan else if (sizeread != IDENT_LENGTH + 16) 245169689Skan { 246169689Skan cpp_error (pfile, CPP_DL_WARNING, "%s: too short to be a PCH file", 247169689Skan name); 248169689Skan return 2; 249169689Skan } 250169689Skan 251132718Skan pch_ident = get_ident(); 252132718Skan if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0) 253132718Skan { 254132718Skan if (cpp_get_options (pfile)->warn_invalid_pch) 255132718Skan { 256132718Skan if (memcmp (ident, pch_ident, 5) == 0) 257132718Skan /* It's a PCH, for the right language, but has the wrong version. 258132718Skan */ 259169689Skan cpp_error (pfile, CPP_DL_WARNING, 260132718Skan "%s: not compatible with this GCC version", name); 261132718Skan else if (memcmp (ident, pch_ident, 4) == 0) 262132718Skan /* It's a PCH for the wrong language. */ 263132718Skan cpp_error (pfile, CPP_DL_WARNING, "%s: not for %s", name, 264132718Skan lang_hooks.name); 265169689Skan else 266132718Skan /* Not any kind of PCH. */ 267132718Skan cpp_error (pfile, CPP_DL_WARNING, "%s: not a PCH file", name); 268132718Skan } 269132718Skan return 2; 270132718Skan } 271169689Skan if (memcmp (ident + IDENT_LENGTH, executable_checksum, 16) != 0) 272132718Skan { 273132718Skan if (cpp_get_options (pfile)->warn_invalid_pch) 274132718Skan cpp_error (pfile, CPP_DL_WARNING, 275169689Skan "%s: created by a different GCC executable", name); 276132718Skan return 2; 277132718Skan } 278132718Skan 279169689Skan /* At this point, we know it's a PCH file created by this 280169689Skan executable, so it ought to be long enough that we can read a 281169689Skan c_pch_validity structure. */ 282169689Skan if (read (fd, &v, sizeof (v)) != sizeof (v)) 283169689Skan fatal_error ("can%'t read %s: %m", name); 284169689Skan 285132718Skan /* The allowable debug info combinations are that either the PCH file 286132718Skan was built with the same as is being used now, or the PCH file was 287132718Skan built for some kind of debug info but now none is in use. */ 288132718Skan if (v.debug_info_type != write_symbols 289132718Skan && write_symbols != NO_DEBUG) 290132718Skan { 291132718Skan if (cpp_get_options (pfile)->warn_invalid_pch) 292169689Skan cpp_error (pfile, CPP_DL_WARNING, 293132718Skan "%s: created with -g%s, but used with -g%s", name, 294132718Skan debug_type_names[v.debug_info_type], 295132718Skan debug_type_names[write_symbols]); 296132718Skan return 2; 297132718Skan } 298132718Skan 299169689Skan /* Check flags that must match exactly. */ 300169689Skan { 301169689Skan size_t i; 302169689Skan for (i = 0; i < MATCH_SIZE; i++) 303169689Skan if (*pch_matching[i].flag_var != v.match[i]) 304169689Skan { 305169689Skan if (cpp_get_options (pfile)->warn_invalid_pch) 306169689Skan cpp_error (pfile, CPP_DL_WARNING, 307169689Skan "%s: settings for %s do not match", name, 308169689Skan pch_matching[i].flag_name); 309169689Skan return 2; 310169689Skan } 311169689Skan } 312169689Skan 313132718Skan /* If the text segment was not loaded at the same address as it was 314132718Skan when the PCH file was created, function pointers loaded from the 315132718Skan PCH will not be valid. We could in theory remap all the function 316169689Skan pointers, but no support for that exists at present. 317169689Skan Since we have the same executable, it should only be necessary to 318169689Skan check one function. */ 319132718Skan if (v.pch_init != &pch_init) 320132718Skan { 321132718Skan if (cpp_get_options (pfile)->warn_invalid_pch) 322169689Skan cpp_error (pfile, CPP_DL_WARNING, 323132718Skan "%s: had text segment at different address", name); 324132718Skan return 2; 325132718Skan } 326132718Skan 327132718Skan /* Check the target-specific validity data. */ 328132718Skan { 329132718Skan void *this_file_data = xmalloc (v.target_data_length); 330132718Skan const char *msg; 331169689Skan 332132718Skan if ((size_t) read (fd, this_file_data, v.target_data_length) 333132718Skan != v.target_data_length) 334169689Skan fatal_error ("can%'t read %s: %m", name); 335132718Skan msg = targetm.pch_valid_p (this_file_data, v.target_data_length); 336132718Skan free (this_file_data); 337132718Skan if (msg != NULL) 338132718Skan { 339132718Skan if (cpp_get_options (pfile)->warn_invalid_pch) 340132718Skan cpp_error (pfile, CPP_DL_WARNING, "%s: %s", name, msg); 341132718Skan return 2; 342132718Skan } 343132718Skan } 344132718Skan 345132718Skan /* Check the preprocessor macros are the same as when the PCH was 346132718Skan generated. */ 347169689Skan 348132718Skan result = cpp_valid_state (pfile, name, fd); 349132718Skan if (result == -1) 350132718Skan return 2; 351132718Skan else 352132718Skan return result == 0; 353132718Skan} 354132718Skan 355169689Skan/* If non-NULL, this function is called after a precompile header file 356169689Skan is loaded. */ 357169689Skanvoid (*lang_post_pch_load) (void); 358169689Skan 359132718Skan/* Load in the PCH file NAME, open on FD. It was originally searched for 360132718Skan by ORIG_NAME. */ 361132718Skan 362132718Skanvoid 363132718Skanc_common_read_pch (cpp_reader *pfile, const char *name, 364132718Skan int fd, const char *orig_name ATTRIBUTE_UNUSED) 365132718Skan{ 366132718Skan FILE *f; 367132718Skan struct c_pch_header h; 368132718Skan struct save_macro_data *smd; 369169689Skan 370132718Skan f = fdopen (fd, "rb"); 371132718Skan if (f == NULL) 372132718Skan { 373132718Skan cpp_errno (pfile, CPP_DL_ERROR, "calling fdopen"); 374132718Skan return; 375132718Skan } 376132718Skan 377132718Skan cpp_get_callbacks (parse_in)->valid_pch = NULL; 378132718Skan 379132718Skan if (fread (&h, sizeof (h), 1, f) != 1) 380132718Skan { 381132718Skan cpp_errno (pfile, CPP_DL_ERROR, "reading"); 382132718Skan return; 383132718Skan } 384132718Skan 385169689Skan if (!flag_preprocess_only) 386132718Skan { 387169689Skan unsigned long written; 388169689Skan char * buf = XNEWVEC (char, 16384); 389169689Skan 390169689Skan for (written = 0; written < h.asm_size; ) 391169689Skan { 392169689Skan long size = h.asm_size - written; 393169689Skan if (size > 16384) 394169689Skan size = 16384; 395169689Skan if (fread (buf, size, 1, f) != 1 396169689Skan || fwrite (buf, size, 1, asm_out_file) != 1) 397169689Skan cpp_errno (pfile, CPP_DL_ERROR, "reading"); 398169689Skan written += size; 399169689Skan } 400169689Skan free (buf); 401132718Skan } 402169689Skan else 403169689Skan { 404169689Skan /* If we're preprocessing, don't write to a NULL 405169689Skan asm_out_file. */ 406169689Skan if (fseek (f, h.asm_size, SEEK_CUR) != 0) 407169689Skan cpp_errno (pfile, CPP_DL_ERROR, "seeking"); 408169689Skan } 409132718Skan 410132718Skan cpp_prepare_state (pfile, &smd); 411132718Skan 412132718Skan gt_pch_restore (f); 413132718Skan 414132718Skan if (cpp_read_state (pfile, name, f, smd) != 0) 415132718Skan return; 416132718Skan 417132718Skan fclose (f); 418169689Skan 419169689Skan /* Give the front end a chance to take action after a PCH file has 420169689Skan been loaded. */ 421169689Skan if (lang_post_pch_load) 422169689Skan (*lang_post_pch_load) (); 423132718Skan} 424132718Skan 425132718Skan/* Indicate that no more PCH files should be read. */ 426132718Skan 427132718Skanvoid 428132718Skanc_common_no_more_pch (void) 429132718Skan{ 430132718Skan if (cpp_get_callbacks (parse_in)->valid_pch) 431132718Skan { 432132718Skan cpp_get_callbacks (parse_in)->valid_pch = NULL; 433161651Skan host_hooks.gt_pch_use_address (NULL, 0, -1, 0); 434132718Skan } 435132718Skan} 436169689Skan 437169689Skan/* Handle #pragma GCC pch_preprocess, to load in the PCH file. */ 438169689Skan 439169689Skan#ifndef O_BINARY 440169689Skan# define O_BINARY 0 441169689Skan#endif 442169689Skan 443169689Skanvoid 444169689Skanc_common_pch_pragma (cpp_reader *pfile, const char *name) 445169689Skan{ 446169689Skan int fd; 447169689Skan 448169689Skan if (!cpp_get_options (pfile)->preprocessed) 449169689Skan { 450169689Skan error ("pch_preprocess pragma should only be used with -fpreprocessed"); 451169689Skan inform ("use #include instead"); 452169689Skan return; 453169689Skan } 454169689Skan 455169689Skan fd = open (name, O_RDONLY | O_BINARY, 0666); 456169689Skan if (fd == -1) 457169689Skan fatal_error ("%s: couldn%'t open PCH file: %m", name); 458169689Skan 459169689Skan if (c_common_valid_pch (pfile, name, fd) != 1) 460169689Skan { 461169689Skan if (!cpp_get_options (pfile)->warn_invalid_pch) 462169689Skan inform ("use -Winvalid-pch for more information"); 463169689Skan fatal_error ("%s: PCH file was invalid", name); 464169689Skan } 465169689Skan 466169689Skan c_common_read_pch (pfile, name, fd, name); 467169689Skan 468169689Skan close (fd); 469169689Skan} 470169689Skan 471169689Skan/* Print out executable_checksum[]. */ 472169689Skan 473169689Skanvoid 474169689Skanc_common_print_pch_checksum (FILE *f) 475169689Skan{ 476169689Skan int i; 477169689Skan fputs ("Compiler executable checksum: ", f); 478169689Skan for (i = 0; i < 16; i++) 479169689Skan fprintf (f, "%02x", executable_checksum[i]); 480169689Skan putc ('\n', f); 481169689Skan} 482