#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "version.h"
#include "cpplib.h"
#include "tree.h"
#include "flags.h"
#include "c-common.h"
#include "output.h"
#include "toplev.h"
#include "debug.h"
#include "c-pragma.h"
#include "ggc.h"
#include "langhooks.h"
#include "hosthooks.h"
#include "target.h"
#ifdef ENABLE_LLVM
#include "llvm.h"
#endif
static const struct c_pch_matching
{
int *flag_var;
const char *flag_name;
} pch_matching[] = {
{ &flag_exceptions, "-fexceptions" },
{ &flag_faltivec, "-faltivec" },
{ &flag_unit_at_a_time, "-funit-at-a-time" }
};
enum {
MATCH_SIZE = ARRAY_SIZE (pch_matching)
};
static const char no_checksum[16] = { 0 };
struct c_pch_validity
{
unsigned char debug_info_type;
signed char match[MATCH_SIZE];
void (*pch_init) (void);
size_t target_data_length;
};
struct c_pch_header
{
unsigned long asm_size;
};
#define IDENT_LENGTH 8
static FILE *pch_outfile;
static long asm_file_startpos;
static const char *get_ident (void);
static const char *
get_ident (void)
{
static char result[IDENT_LENGTH];
static const char template[IDENT_LENGTH] = "gpch.013";
static const char c_language_chars[] = "Co+O";
memcpy (result, template, IDENT_LENGTH);
result[4] = c_language_chars[c_language];
return result;
}
void
pch_init (void)
{
FILE *f;
struct c_pch_validity v;
void *target_validity;
static const char partial_pch[IDENT_LENGTH] = "gpcWrite";
#ifdef ASM_COMMENT_START
if (flag_verbose_asm)
{
fprintf (asm_out_file, "%s ", ASM_COMMENT_START);
c_common_print_pch_checksum (asm_out_file);
fputc ('\n', asm_out_file);
}
#endif
if (!pch_file)
return;
f = fopen (pch_file, "w+b");
if (f == NULL)
fatal_error ("can%'t create precompiled header %s: %m", pch_file);
pch_outfile = f;
gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
v.debug_info_type = write_symbols;
{
size_t i;
for (i = 0; i < MATCH_SIZE; i++)
{
v.match[i] = *pch_matching[i].flag_var;
gcc_assert (v.match[i] == *pch_matching[i].flag_var);
}
}
v.pch_init = &pch_init;
target_validity = targetm.get_pch_validity (&v.target_data_length);
if (fwrite (partial_pch, IDENT_LENGTH, 1, f) != 1
|| fwrite (executable_checksum, 16, 1, f) != 1
|| fwrite (&v, sizeof (v), 1, f) != 1
|| fwrite (target_validity, v.target_data_length, 1, f) != 1)
fatal_error ("can%'t write to %s: %m", pch_file);
if (asm_file_name == NULL
|| strcmp (asm_file_name, "-") == 0)
fatal_error ("%qs is not a valid output file", asm_file_name);
asm_file_startpos = ftell (asm_out_file);
(*debug_hooks->handle_pch) (0);
cpp_save_state (parse_in, f);
}
void
c_common_write_pch (void)
{
char *buf;
long asm_file_end;
long written;
struct c_pch_header h;
#ifdef ENABLE_LLVM
llvm_asm_file_end();
#endif
(*debug_hooks->handle_pch) (1);
cpp_write_pch_deps (parse_in, pch_outfile);
asm_file_end = ftell (asm_out_file);
h.asm_size = asm_file_end - asm_file_startpos;
if (fwrite (&h, sizeof (h), 1, pch_outfile) != 1)
fatal_error ("can%'t write %s: %m", pch_file);
buf = XNEWVEC (char, 16384);
if (fseek (asm_out_file, asm_file_startpos, SEEK_SET) != 0)
fatal_error ("can%'t seek in %s: %m", asm_file_name);
for (written = asm_file_startpos; written < asm_file_end; )
{
long size = asm_file_end - written;
if (size > 16384)
size = 16384;
if (fread (buf, size, 1, asm_out_file) != 1)
fatal_error ("can%'t read %s: %m", asm_file_name);
if (fwrite (buf, size, 1, pch_outfile) != 1)
fatal_error ("can%'t write %s: %m", pch_file);
written += size;
}
free (buf);
if (fseek (asm_out_file, 0, SEEK_END) != 0)
fatal_error ("can%'t seek in %s: %m", asm_file_name);
gt_pch_save (pch_outfile);
cpp_write_pch_state (parse_in, pch_outfile);
if (fseek (pch_outfile, 0, SEEK_SET) != 0
|| fwrite (get_ident (), IDENT_LENGTH, 1, pch_outfile) != 1)
fatal_error ("can%'t write %s: %m", pch_file);
fclose (pch_outfile);
}
int
c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
{
int sizeread;
int result;
char ident[IDENT_LENGTH + 16];
const char *pch_ident;
struct c_pch_validity v;
gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
sizeread = read (fd, ident, IDENT_LENGTH + 16);
if (sizeread == -1)
fatal_error ("can%'t read %s: %m", name);
else if (sizeread != IDENT_LENGTH + 16)
{
cpp_error (pfile, CPP_DL_WARNING, "%s: too short to be a PCH file",
name);
return 2;
}
pch_ident = get_ident();
if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0)
{
if (cpp_get_options (pfile)->warn_invalid_pch)
{
if (memcmp (ident, pch_ident, 5) == 0)
cpp_error (pfile, CPP_DL_WARNING,
"%s: not compatible with this GCC version", name);
else if (memcmp (ident, pch_ident, 4) == 0)
cpp_error (pfile, CPP_DL_WARNING, "%s: not for %s", name,
lang_hooks.name);
else
cpp_error (pfile, CPP_DL_WARNING, "%s: not a PCH file", name);
}
return 2;
}
if (memcmp (ident + IDENT_LENGTH, executable_checksum, 16) != 0)
{
if (cpp_get_options (pfile)->warn_invalid_pch)
cpp_error (pfile, CPP_DL_WARNING,
"%s: created by a different GCC executable", name);
return 2;
}
if (read (fd, &v, sizeof (v)) != sizeof (v))
fatal_error ("can%'t read %s: %m", name);
if (v.debug_info_type != write_symbols
&& write_symbols != NO_DEBUG)
{
if (cpp_get_options (pfile)->warn_invalid_pch)
cpp_error (pfile, CPP_DL_WARNING,
"%s: created with -g%s, but used with -g%s", name,
debug_type_names[v.debug_info_type],
debug_type_names[write_symbols]);
return 2;
}
{
size_t i;
for (i = 0; i < MATCH_SIZE; i++)
if (*pch_matching[i].flag_var != v.match[i])
{
if (cpp_get_options (pfile)->warn_invalid_pch)
cpp_error (pfile, CPP_DL_WARNING,
"%s: settings for %s do not match", name,
pch_matching[i].flag_name);
return 2;
}
}
if (v.pch_init != &pch_init)
{
if (cpp_get_options (pfile)->warn_invalid_pch)
cpp_error (pfile, CPP_DL_WARNING,
"%s: had text segment at different address", name);
return 2;
}
{
void *this_file_data = xmalloc (v.target_data_length);
const char *msg;
if ((size_t) read (fd, this_file_data, v.target_data_length)
!= v.target_data_length)
fatal_error ("can%'t read %s: %m", name);
msg = targetm.pch_valid_p (this_file_data, v.target_data_length);
free (this_file_data);
if (msg != NULL)
{
if (cpp_get_options (pfile)->warn_invalid_pch)
cpp_error (pfile, CPP_DL_WARNING, "%s: %s", name, msg);
return 2;
}
}
result = cpp_valid_state (pfile, name, fd);
if (result == -1)
return 2;
else
return result == 0;
}
void (*lang_post_pch_load) (void);
void
c_common_read_pch (cpp_reader *pfile, const char *name,
int fd, const char *orig_name ATTRIBUTE_UNUSED)
{
FILE *f;
struct c_pch_header h;
struct save_macro_data *smd;
f = fdopen (fd, "rb");
if (f == NULL)
{
cpp_errno (pfile, CPP_DL_ERROR, "calling fdopen");
return;
}
if (cpp_get_callbacks (parse_in)->valid_pch == 0)
fatal_error ("translation unit malformed for PCH");
cpp_get_callbacks (parse_in)->valid_pch = NULL;
if (fread (&h, sizeof (h), 1, f) != 1)
{
cpp_errno (pfile, CPP_DL_ERROR, "reading");
return;
}
if (!flag_preprocess_only)
{
#ifdef ENABLE_LLVM
unsigned char *buf = xmalloc (h.asm_size);
if (fread (buf, h.asm_size, 1, f) != 1)
cpp_errno (pfile, CPP_DL_ERROR, "reading");
llvm_pch_read(buf, h.asm_size);
#else
unsigned long written;
char * buf = XNEWVEC (char, 16384);
for (written = 0; written < h.asm_size; )
{
long size = h.asm_size - written;
if (size > 16384)
size = 16384;
if (fread (buf, size, 1, f) != 1
|| fwrite (buf, size, 1, asm_out_file) != 1)
cpp_errno (pfile, CPP_DL_ERROR, "reading");
written += size;
}
#endif
free (buf);
}
else
{
if (fseek (f, h.asm_size, SEEK_CUR) != 0)
cpp_errno (pfile, CPP_DL_ERROR, "seeking");
}
cpp_prepare_state (pfile, &smd);
gt_pch_restore (f);
if (cpp_read_state (pfile, name, f, smd) != 0)
return;
fclose (f);
if (lang_post_pch_load)
(*lang_post_pch_load) ();
#ifdef ENABLE_LLVM
llvm_post_pch_read();
#endif
}
void
c_common_no_more_pch (void)
{
if (cpp_get_callbacks (parse_in)->valid_pch)
{
cpp_get_callbacks (parse_in)->valid_pch = NULL;
host_hooks.gt_pch_use_address (NULL, 0, -1, 0);
}
}
const char *indirect_file PARAMS ((const char *, int));
#ifndef O_BINARY
# define O_BINARY 0
#endif
void
c_common_pch_pragma (cpp_reader *pfile, const char *name)
{
int fd;
if (!cpp_get_options (pfile)->preprocessed)
{
error ("pch_preprocess pragma should only be used with -fpreprocessed");
inform ("use #include instead");
return;
}
name = indirect_file (name, 0);
fd = open (name, O_RDONLY | O_BINARY, 0666);
if (fd == -1)
fatal_error ("%s: couldn%'t open PCH file: %m", name);
if (c_common_valid_pch (pfile, name, fd) != 1)
{
if (!cpp_get_options (pfile)->warn_invalid_pch)
inform ("use -Winvalid-pch for more information");
fatal_error ("%s: PCH file was invalid", name);
}
c_common_read_pch (pfile, name, fd, name);
close (fd);
}
void
c_common_print_pch_checksum (FILE *f)
{
int i;
fputs ("Compiler executable checksum: ", f);
for (i = 0; i < 16; i++)
fprintf (f, "%02x", executable_checksum[i]);
putc ('\n', f);
}