PR sanitizer/85029
[official-gcc.git] / gcc / lto-opts.c
blobea336ad99bb81254be698b5b0681f975e1ecc71d
1 /* LTO IL options.
3 Copyright (C) 2009-2018 Free Software Foundation, Inc.
4 Contributed by Simon Baldwin <simonb@google.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "target.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "cgraph.h"
30 #include "lto-streamer.h"
31 #include "opts.h"
32 #include "toplev.h"
34 /* Append the option piece OPT to the COLLECT_GCC_OPTIONS string
35 set up by OB, appropriately quoted and separated by spaces
36 (if !*FIRST_P). */
38 static void
39 append_to_collect_gcc_options (struct obstack *ob,
40 bool *first_p, const char *opt)
42 const char *p, *q = opt;
43 if (!*first_p)
44 obstack_grow (ob, " ", 1);
45 obstack_grow (ob, "'", 1);
46 while ((p = strchr (q, '\'')))
48 obstack_grow (ob, q, p - q);
49 obstack_grow (ob, "'\\''", 4);
50 q = ++p;
52 obstack_grow (ob, q, strlen (q));
53 obstack_grow (ob, "'", 1);
54 *first_p = false;
57 /* Write currently held options to an LTO IL section. */
59 void
60 lto_write_options (void)
62 char *section_name;
63 struct obstack temporary_obstack;
64 unsigned int i, j;
65 char *args;
66 bool first_p = true;
68 section_name = lto_get_section_name (LTO_section_opts, NULL, NULL);
69 lto_begin_section (section_name, false);
71 obstack_init (&temporary_obstack);
73 if (!global_options_set.x_flag_openmp
74 && !global_options.x_flag_openmp)
75 append_to_collect_gcc_options (&temporary_obstack, &first_p,
76 "-fno-openmp");
77 if (!global_options_set.x_flag_openacc
78 && !global_options.x_flag_openacc)
79 append_to_collect_gcc_options (&temporary_obstack, &first_p,
80 "-fno-openacc");
82 /* Append options from target hook and store them to offload_lto section. */
83 if (lto_stream_offload_p)
85 char *offload_opts = targetm.offload_options ();
86 char *offload_ptr = offload_opts;
87 while (offload_ptr)
89 char *next = strchr (offload_ptr, ' ');
90 if (next)
91 *next++ = '\0';
92 append_to_collect_gcc_options (&temporary_obstack, &first_p,
93 offload_ptr);
94 offload_ptr = next;
96 free (offload_opts);
99 /* Output explicitly passed options. */
100 for (i = 1; i < save_decoded_options_count; ++i)
102 struct cl_decoded_option *option = &save_decoded_options[i];
104 /* Skip explicitly some common options that we do not need. */
105 switch (option->opt_index)
107 case OPT_dumpbase:
108 case OPT_SPECIAL_unknown:
109 case OPT_SPECIAL_ignore:
110 case OPT_SPECIAL_program_name:
111 case OPT_SPECIAL_input_file:
112 continue;
114 default:
115 break;
118 /* Skip frontend and driver specific options here. */
119 if (!(cl_options[option->opt_index].flags & (CL_COMMON|CL_TARGET|CL_LTO)))
120 continue;
122 /* Do not store target-specific options in offload_lto section. */
123 if ((cl_options[option->opt_index].flags & CL_TARGET)
124 && lto_stream_offload_p)
125 continue;
127 /* Drop options created from the gcc driver that will be rejected
128 when passed on to the driver again. */
129 if (cl_options[option->opt_index].cl_reject_driver)
130 continue;
132 /* Also drop all options that are handled by the driver as well,
133 which includes things like -o and -v or -fhelp for example.
134 We do not need those. The only exception is -foffload option, if we
135 write it in offload_lto section. Also drop all diagnostic options. */
136 if ((cl_options[option->opt_index].flags & (CL_DRIVER|CL_WARNING))
137 && (!lto_stream_offload_p || option->opt_index != OPT_foffload_))
138 continue;
140 for (j = 0; j < option->canonical_option_num_elements; ++j)
141 append_to_collect_gcc_options (&temporary_obstack, &first_p,
142 option->canonical_option[j]);
144 obstack_grow (&temporary_obstack, "\0", 1);
145 args = XOBFINISH (&temporary_obstack, char *);
146 lto_write_data (args, strlen (args) + 1);
147 lto_end_section ();
149 obstack_free (&temporary_obstack, NULL);
150 free (section_name);