]>
Commit | Line | Data |
---|---|---|
d7f09764 | 1 | /* Wrapper to call lto. Used by collect2 and the linker plugin. |
8d9254fc | 2 | Copyright (C) 2009-2020 Free Software Foundation, Inc. |
d7f09764 DN |
3 | |
4 | Factored out of collect2 by Rafael Espindola <espindola@google.com> | |
5 | ||
6 | This file is part of GCC. | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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/>. */ | |
21 | ||
22 | ||
23 | /* This program is passed a gcc, a list of gcc arguments and a list of | |
24 | object files containing IL. It scans the argument list to check if | |
25 | we are in whopr mode or not modifies the arguments and needed and | |
26 | prints a list of output files on stdout. | |
27 | ||
28 | Example: | |
29 | ||
30 | $ lto-wrapper gcc/xgcc -B gcc a.o b.o -o test -flto | |
31 | ||
32 | The above will print something like | |
33 | /tmp/ccwbQ8B2.lto.o | |
34 | ||
014d92e1 | 35 | If WHOPR is used instead, more than one file might be produced |
d7f09764 DN |
36 | ./ccXj2DTk.lto.ltrans.o |
37 | ./ccCJuXGv.lto.ltrans.o | |
38 | */ | |
39 | ||
40 | #include "config.h" | |
41 | #include "system.h" | |
10692477 | 42 | #include "coretypes.h" |
d7f09764 | 43 | #include "intl.h" |
2691e6d7 | 44 | #include "diagnostic.h" |
48cf395b | 45 | #include "obstack.h" |
f31c0018 RG |
46 | #include "opts.h" |
47 | #include "options.h" | |
52a35ef7 | 48 | #include "simple-object.h" |
4000360e | 49 | #include "lto-section-names.h" |
a185856a | 50 | #include "collect-utils.h" |
d7f09764 | 51 | |
fc8b3540 IV |
52 | /* Environment variable, used for passing the names of offload targets from GCC |
53 | driver to lto-wrapper. */ | |
54 | #define OFFLOAD_TARGET_NAMES_ENV "OFFLOAD_TARGET_NAMES" | |
55 | ||
1dedc12d AO |
56 | /* By default there is no special suffix for target executables. */ |
57 | #ifdef TARGET_EXECUTABLE_SUFFIX | |
58 | #define HAVE_TARGET_EXECUTABLE_SUFFIX | |
59 | #else | |
60 | #define TARGET_EXECUTABLE_SUFFIX "" | |
61 | #endif | |
62 | ||
d7f09764 | 63 | enum lto_mode_d { |
cf96bae7 RG |
64 | LTO_MODE_NONE, /* Not doing LTO. */ |
65 | LTO_MODE_LTO, /* Normal LTO. */ | |
66 | LTO_MODE_WHOPR /* WHOPR. */ | |
d7f09764 DN |
67 | }; |
68 | ||
69 | /* Current LTO mode. */ | |
70 | static enum lto_mode_d lto_mode = LTO_MODE_NONE; | |
71 | ||
b1b07c92 RG |
72 | static char *ltrans_output_file; |
73 | static char *flto_out; | |
50ee30d5 | 74 | static unsigned int nr; |
b6e33d73 | 75 | static int *ltrans_priorities; |
50ee30d5 RG |
76 | static char **input_names; |
77 | static char **output_names; | |
fc8b3540 | 78 | static char **offload_names; |
e6861a99 | 79 | static char *offload_objects_file_name; |
50ee30d5 | 80 | static char *makefile; |
b5327e50 IS |
81 | static unsigned int num_deb_objs; |
82 | static const char **early_debug_object_names; | |
f1a681a1 | 83 | static bool xassembler_options_error = false; |
b1b07c92 | 84 | |
a185856a | 85 | const char tool_name[] = "lto-wrapper"; |
b1b07c92 | 86 | |
a185856a | 87 | /* Delete tempfiles. Called from utils_cleanup. */ |
b1b07c92 | 88 | |
a185856a | 89 | void |
5f0ad6a5 | 90 | tool_cleanup (bool) |
b1b07c92 | 91 | { |
396717c9 RG |
92 | unsigned int i; |
93 | ||
396717c9 | 94 | if (ltrans_output_file) |
a185856a | 95 | maybe_unlink (ltrans_output_file); |
396717c9 | 96 | if (flto_out) |
a185856a | 97 | maybe_unlink (flto_out); |
e6861a99 IV |
98 | if (offload_objects_file_name) |
99 | maybe_unlink (offload_objects_file_name); | |
396717c9 | 100 | if (makefile) |
a185856a | 101 | maybe_unlink (makefile); |
b5327e50 IS |
102 | if (early_debug_object_names) |
103 | for (i = 0; i < num_deb_objs; ++i) | |
104 | if (early_debug_object_names[i]) | |
105 | maybe_unlink (early_debug_object_names[i]); | |
396717c9 | 106 | for (i = 0; i < nr; ++i) |
8aea79e6 | 107 | { |
a185856a | 108 | maybe_unlink (input_names[i]); |
396717c9 | 109 | if (output_names[i]) |
a185856a | 110 | maybe_unlink (output_names[i]); |
8aea79e6 | 111 | } |
396717c9 RG |
112 | } |
113 | ||
114 | static void | |
a185856a | 115 | lto_wrapper_cleanup (void) |
d7f09764 | 116 | { |
5f0ad6a5 | 117 | utils_cleanup (false); |
d7f09764 DN |
118 | } |
119 | ||
d7f09764 DN |
120 | /* Unlink a temporary LTRANS file unless requested otherwise. */ |
121 | ||
a185856a BS |
122 | void |
123 | maybe_unlink (const char *file) | |
d7f09764 | 124 | { |
608508a6 | 125 | if (!save_temps) |
d7f09764 | 126 | { |
62116e60 RG |
127 | if (unlink_if_ordinary (file) |
128 | && errno != ENOENT) | |
40fecdd6 | 129 | fatal_error (input_location, "deleting LTRANS file %s: %m", file); |
d7f09764 | 130 | } |
eba14fca | 131 | else if (verbose) |
d7f09764 DN |
132 | fprintf (stderr, "[Leaving LTRANS %s]\n", file); |
133 | } | |
134 | ||
48cf395b | 135 | /* Template of LTRANS dumpbase suffix. */ |
1dedc12d | 136 | #define DUMPBASE_SUFFIX "ltrans18446744073709551615" |
d7f09764 | 137 | |
f31c0018 | 138 | /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS |
8508ae1d | 139 | environment. */ |
f31c0018 RG |
140 | |
141 | static void | |
142 | get_options_from_collect_gcc_options (const char *collect_gcc, | |
143 | const char *collect_gcc_options, | |
f31c0018 RG |
144 | struct cl_decoded_option **decoded_options, |
145 | unsigned int *decoded_options_count) | |
146 | { | |
969d578c | 147 | struct obstack argv_obstack; |
f31c0018 | 148 | const char **argv; |
f1a681a1 | 149 | int argc; |
969d578c | 150 | |
969d578c RG |
151 | obstack_init (&argv_obstack); |
152 | obstack_ptr_grow (&argv_obstack, collect_gcc); | |
153 | ||
f1a681a1 PK |
154 | parse_options_from_collect_gcc_options (collect_gcc_options, |
155 | &argv_obstack, &argc); | |
969d578c | 156 | argv = XOBFINISH (&argv_obstack, const char **); |
f31c0018 | 157 | |
8508ae1d | 158 | decode_cmdline_options_to_array (argc, (const char **)argv, CL_DRIVER, |
f31c0018 | 159 | decoded_options, decoded_options_count); |
969d578c | 160 | obstack_free (&argv_obstack, NULL); |
f31c0018 RG |
161 | } |
162 | ||
52a35ef7 RG |
163 | /* Append OPTION to the options array DECODED_OPTIONS with size |
164 | DECODED_OPTIONS_COUNT. */ | |
165 | ||
166 | static void | |
167 | append_option (struct cl_decoded_option **decoded_options, | |
168 | unsigned int *decoded_options_count, | |
169 | struct cl_decoded_option *option) | |
170 | { | |
171 | ++*decoded_options_count; | |
172 | *decoded_options | |
173 | = (struct cl_decoded_option *) | |
174 | xrealloc (*decoded_options, | |
175 | (*decoded_options_count | |
176 | * sizeof (struct cl_decoded_option))); | |
177 | memcpy (&(*decoded_options)[*decoded_options_count - 1], option, | |
178 | sizeof (struct cl_decoded_option)); | |
179 | } | |
180 | ||
472a2536 JH |
181 | /* Remove option number INDEX from DECODED_OPTIONS, update |
182 | DECODED_OPTIONS_COUNT. */ | |
183 | ||
184 | static void | |
185 | remove_option (struct cl_decoded_option **decoded_options, | |
186 | int index, unsigned int *decoded_options_count) | |
187 | { | |
188 | --*decoded_options_count; | |
189 | memmove (&(*decoded_options)[index + 1], | |
190 | &(*decoded_options)[index], | |
191 | sizeof (struct cl_decoded_option) | |
192 | * (*decoded_options_count - index)); | |
193 | } | |
194 | ||
52a35ef7 RG |
195 | /* Try to merge and complain about options FDECODED_OPTIONS when applied |
196 | ontop of DECODED_OPTIONS. */ | |
197 | ||
198 | static void | |
199 | merge_and_complain (struct cl_decoded_option **decoded_options, | |
200 | unsigned int *decoded_options_count, | |
201 | struct cl_decoded_option *fdecoded_options, | |
202 | unsigned int fdecoded_options_count) | |
203 | { | |
204 | unsigned int i, j; | |
472a2536 JH |
205 | struct cl_decoded_option *pic_option = NULL; |
206 | struct cl_decoded_option *pie_option = NULL; | |
52a35ef7 RG |
207 | |
208 | /* ??? Merge options from files. Most cases can be | |
209 | handled by either unioning or intersecting | |
210 | (for example -fwrapv is a case for unioning, | |
211 | -ffast-math is for intersection). Most complaints | |
212 | about real conflicts between different options can | |
213 | be deferred to the compiler proper. Options that | |
214 | we can neither safely handle by intersection nor | |
215 | unioning would need to be complained about here. | |
216 | Ideally we'd have a flag in the opt files that | |
217 | tells whether to union or intersect or reject. | |
218 | In absence of that it's unclear what a good default is. | |
219 | It's also difficult to get positional handling correct. */ | |
220 | ||
221 | /* The following does what the old LTO option code did, | |
222 | union all target and a selected set of common options. */ | |
223 | for (i = 0; i < fdecoded_options_count; ++i) | |
224 | { | |
225 | struct cl_decoded_option *foption = &fdecoded_options[i]; | |
226 | switch (foption->opt_index) | |
227 | { | |
169d8507 L |
228 | case OPT_SPECIAL_unknown: |
229 | case OPT_SPECIAL_ignore: | |
68a57628 | 230 | case OPT_SPECIAL_warn_removed: |
169d8507 L |
231 | case OPT_SPECIAL_program_name: |
232 | case OPT_SPECIAL_input_file: | |
233 | break; | |
234 | ||
52a35ef7 RG |
235 | default: |
236 | if (!(cl_options[foption->opt_index].flags & CL_TARGET)) | |
237 | break; | |
238 | ||
239 | /* Fallthru. */ | |
eb472171 | 240 | case OPT_fdiagnostics_show_caret: |
96e6ae57 | 241 | case OPT_fdiagnostics_show_labels: |
56b61d7f | 242 | case OPT_fdiagnostics_show_line_numbers: |
eb472171 CLT |
243 | case OPT_fdiagnostics_show_option: |
244 | case OPT_fdiagnostics_show_location_: | |
245 | case OPT_fshow_column: | |
52a35ef7 | 246 | case OPT_fcommon: |
aad038ca | 247 | case OPT_fgnu_tm: |
5a307ee5 | 248 | case OPT_g: |
52a35ef7 RG |
249 | /* Do what the old LTO code did - collect exactly one option |
250 | setting per OPT code, we pick the first we encounter. | |
251 | ??? This doesn't make too much sense, but when it doesn't | |
252 | then we should complain. */ | |
253 | for (j = 0; j < *decoded_options_count; ++j) | |
254 | if ((*decoded_options)[j].opt_index == foption->opt_index) | |
255 | break; | |
256 | if (j == *decoded_options_count) | |
257 | append_option (decoded_options, decoded_options_count, foption); | |
258 | break; | |
8bb50e5c | 259 | |
472a2536 JH |
260 | /* Figure out what PIC/PIE level wins and merge the results. */ |
261 | case OPT_fPIC: | |
262 | case OPT_fpic: | |
263 | pic_option = foption; | |
264 | break; | |
265 | case OPT_fPIE: | |
266 | case OPT_fpie: | |
267 | pie_option = foption; | |
268 | break; | |
269 | ||
1506ae0e | 270 | case OPT_fopenmp: |
a0c88d06 | 271 | case OPT_fopenacc: |
4094757e RB |
272 | /* For selected options we can merge conservatively. */ |
273 | for (j = 0; j < *decoded_options_count; ++j) | |
274 | if ((*decoded_options)[j].opt_index == foption->opt_index) | |
275 | break; | |
276 | if (j == *decoded_options_count) | |
277 | append_option (decoded_options, decoded_options_count, foption); | |
2fff1c81 | 278 | /* -fopenmp > -fno-openmp, |
c0c12356 | 279 | -fopenacc > -fno-openacc */ |
4094757e RB |
280 | else if (foption->value > (*decoded_options)[j].value) |
281 | (*decoded_options)[j] = *foption; | |
282 | break; | |
283 | ||
b6adbb9f NS |
284 | case OPT_fopenacc_dim_: |
285 | /* Append or check identical. */ | |
286 | for (j = 0; j < *decoded_options_count; ++j) | |
287 | if ((*decoded_options)[j].opt_index == foption->opt_index) | |
288 | break; | |
289 | if (j == *decoded_options_count) | |
290 | append_option (decoded_options, decoded_options_count, foption); | |
291 | else if (strcmp ((*decoded_options)[j].arg, foption->arg)) | |
292 | fatal_error (input_location, | |
a9c697b8 | 293 | "option %s with different values", |
b6adbb9f NS |
294 | foption->orig_option_with_args_text); |
295 | break; | |
296 | ||
f3ba16d0 RB |
297 | case OPT_O: |
298 | case OPT_Ofast: | |
299 | case OPT_Og: | |
300 | case OPT_Os: | |
301 | for (j = 0; j < *decoded_options_count; ++j) | |
302 | if ((*decoded_options)[j].opt_index == OPT_O | |
303 | || (*decoded_options)[j].opt_index == OPT_Ofast | |
304 | || (*decoded_options)[j].opt_index == OPT_Og | |
305 | || (*decoded_options)[j].opt_index == OPT_Os) | |
306 | break; | |
307 | if (j == *decoded_options_count) | |
308 | append_option (decoded_options, decoded_options_count, foption); | |
309 | else if ((*decoded_options)[j].opt_index == foption->opt_index | |
310 | && foption->opt_index != OPT_O) | |
311 | /* Exact same options get merged. */ | |
312 | ; | |
313 | else | |
314 | { | |
315 | /* For mismatched option kinds preserve the optimization | |
316 | level only, thus merge it as -On. This also handles | |
317 | merging of same optimization level -On. */ | |
318 | int level = 0; | |
319 | switch (foption->opt_index) | |
320 | { | |
321 | case OPT_O: | |
322 | if (foption->arg[0] == '\0') | |
323 | level = MAX (level, 1); | |
324 | else | |
325 | level = MAX (level, atoi (foption->arg)); | |
326 | break; | |
327 | case OPT_Ofast: | |
328 | level = MAX (level, 3); | |
329 | break; | |
330 | case OPT_Og: | |
331 | level = MAX (level, 1); | |
332 | break; | |
333 | case OPT_Os: | |
334 | level = MAX (level, 2); | |
335 | break; | |
336 | default: | |
337 | gcc_unreachable (); | |
338 | } | |
339 | switch ((*decoded_options)[j].opt_index) | |
340 | { | |
341 | case OPT_O: | |
342 | if ((*decoded_options)[j].arg[0] == '\0') | |
343 | level = MAX (level, 1); | |
344 | else | |
345 | level = MAX (level, atoi ((*decoded_options)[j].arg)); | |
346 | break; | |
347 | case OPT_Ofast: | |
348 | level = MAX (level, 3); | |
349 | break; | |
350 | case OPT_Og: | |
351 | level = MAX (level, 1); | |
352 | break; | |
353 | case OPT_Os: | |
354 | level = MAX (level, 2); | |
355 | break; | |
356 | default: | |
357 | gcc_unreachable (); | |
358 | } | |
359 | (*decoded_options)[j].opt_index = OPT_O; | |
360 | char *tem; | |
582f770b | 361 | tem = xasprintf ("-O%d", level); |
f3ba16d0 RB |
362 | (*decoded_options)[j].arg = &tem[2]; |
363 | (*decoded_options)[j].canonical_option[0] = tem; | |
364 | (*decoded_options)[j].value = 1; | |
365 | } | |
366 | break; | |
472a2536 JH |
367 | |
368 | ||
369 | case OPT_foffload_abi_: | |
370 | for (j = 0; j < *decoded_options_count; ++j) | |
371 | if ((*decoded_options)[j].opt_index == foption->opt_index) | |
372 | break; | |
373 | if (j == *decoded_options_count) | |
374 | append_option (decoded_options, decoded_options_count, foption); | |
375 | else if (foption->value != (*decoded_options)[j].value) | |
376 | fatal_error (input_location, | |
a9c697b8 | 377 | "option %s not used consistently in all LTO input" |
472a2536 JH |
378 | " files", foption->orig_option_with_args_text); |
379 | break; | |
380 | ||
c713ddc0 BS |
381 | |
382 | case OPT_foffload_: | |
383 | append_option (decoded_options, decoded_options_count, foption); | |
384 | break; | |
52a35ef7 RG |
385 | } |
386 | } | |
472a2536 JH |
387 | |
388 | /* Merge PIC options: | |
389 | -fPIC + -fpic = -fpic | |
390 | -fPIC + -fno-pic = -fno-pic | |
5afa32b8 | 391 | -fpic/-fPIC + nothing = nothing. |
472a2536 JH |
392 | It is a common mistake to mix few -fPIC compiled objects into otherwise |
393 | non-PIC code. We do not want to build everything with PIC then. | |
394 | ||
4e6a9380 JH |
395 | Similarly we merge PIE options, however in addition we keep |
396 | -fPIC + -fPIE = -fPIE | |
397 | -fpic + -fPIE = -fpie | |
398 | -fPIC/-fpic + -fpie = -fpie | |
399 | ||
472a2536 JH |
400 | It would be good to warn on mismatches, but it is bit hard to do as |
401 | we do not know what nothing translates to. */ | |
402 | ||
403 | for (unsigned int j = 0; j < *decoded_options_count;) | |
404 | if ((*decoded_options)[j].opt_index == OPT_fPIC | |
405 | || (*decoded_options)[j].opt_index == OPT_fpic) | |
406 | { | |
4e6a9380 JH |
407 | /* -fno-pic in one unit implies -fno-pic everywhere. */ |
408 | if ((*decoded_options)[j].value == 0) | |
409 | j++; | |
410 | /* If we have no pic option or merge in -fno-pic, we still may turn | |
411 | existing pic/PIC mode into pie/PIE if -fpie/-fPIE is present. */ | |
412 | else if ((pic_option && pic_option->value == 0) | |
413 | || !pic_option) | |
414 | { | |
415 | if (pie_option) | |
416 | { | |
417 | bool big = (*decoded_options)[j].opt_index == OPT_fPIC | |
418 | && pie_option->opt_index == OPT_fPIE; | |
419 | (*decoded_options)[j].opt_index = big ? OPT_fPIE : OPT_fpie; | |
420 | if (pie_option->value) | |
5afa32b8 JJ |
421 | (*decoded_options)[j].canonical_option[0] |
422 | = big ? "-fPIE" : "-fpie"; | |
4e6a9380 | 423 | else |
5afa32b8 | 424 | (*decoded_options)[j].canonical_option[0] = "-fno-pie"; |
4e6a9380 JH |
425 | (*decoded_options)[j].value = pie_option->value; |
426 | j++; | |
427 | } | |
428 | else if (pic_option) | |
429 | { | |
430 | (*decoded_options)[j] = *pic_option; | |
431 | j++; | |
432 | } | |
433 | /* We do not know if target defaults to pic or not, so just remove | |
434 | option if it is missing in one unit but enabled in other. */ | |
435 | else | |
436 | remove_option (decoded_options, j, decoded_options_count); | |
437 | } | |
438 | else if (pic_option->opt_index == OPT_fpic | |
439 | && (*decoded_options)[j].opt_index == OPT_fPIC) | |
472a2536 JH |
440 | { |
441 | (*decoded_options)[j] = *pic_option; | |
442 | j++; | |
443 | } | |
444 | else | |
445 | j++; | |
446 | } | |
447 | else if ((*decoded_options)[j].opt_index == OPT_fPIE | |
448 | || (*decoded_options)[j].opt_index == OPT_fpie) | |
449 | { | |
4e6a9380 JH |
450 | /* -fno-pie in one unit implies -fno-pie everywhere. */ |
451 | if ((*decoded_options)[j].value == 0) | |
452 | j++; | |
453 | /* If we have no pie option or merge in -fno-pie, we still preserve | |
454 | PIE/pie if pic/PIC is present. */ | |
455 | else if ((pie_option && pie_option->value == 0) | |
456 | || !pie_option) | |
457 | { | |
458 | /* If -fPIC/-fpic is given, merge it with -fPIE/-fpie. */ | |
459 | if (pic_option) | |
460 | { | |
461 | if (pic_option->opt_index == OPT_fpic | |
462 | && (*decoded_options)[j].opt_index == OPT_fPIE) | |
463 | { | |
464 | (*decoded_options)[j].opt_index = OPT_fpie; | |
465 | (*decoded_options)[j].canonical_option[0] | |
5afa32b8 | 466 | = pic_option->value ? "-fpie" : "-fno-pie"; |
4e6a9380 JH |
467 | } |
468 | else if (!pic_option->value) | |
469 | (*decoded_options)[j].canonical_option[0] = "-fno-pie"; | |
470 | (*decoded_options)[j].value = pic_option->value; | |
471 | j++; | |
472 | } | |
473 | else if (pie_option) | |
474 | { | |
475 | (*decoded_options)[j] = *pie_option; | |
476 | j++; | |
477 | } | |
478 | /* Because we always append pic/PIE options this code path should | |
479 | not happen unless the LTO object was built by old lto1 which | |
480 | did not contain that logic yet. */ | |
481 | else | |
482 | remove_option (decoded_options, j, decoded_options_count); | |
483 | } | |
484 | else if (pie_option->opt_index == OPT_fpie | |
485 | && (*decoded_options)[j].opt_index == OPT_fPIE) | |
472a2536 JH |
486 | { |
487 | (*decoded_options)[j] = *pie_option; | |
488 | j++; | |
489 | } | |
490 | else | |
491 | j++; | |
492 | } | |
493 | else | |
494 | j++; | |
f1a681a1 PK |
495 | |
496 | if (!xassembler_options_error) | |
497 | for (i = j = 0; ; i++, j++) | |
498 | { | |
499 | for (; i < *decoded_options_count; i++) | |
500 | if ((*decoded_options)[i].opt_index == OPT_Xassembler) | |
501 | break; | |
502 | ||
503 | for (; j < fdecoded_options_count; j++) | |
504 | if (fdecoded_options[j].opt_index == OPT_Xassembler) | |
505 | break; | |
506 | ||
507 | if (i == *decoded_options_count && j == fdecoded_options_count) | |
508 | break; | |
509 | else if (i < *decoded_options_count && j == fdecoded_options_count) | |
510 | { | |
511 | warning (0, "Extra option to -Xassembler: %s," | |
512 | " dropping all -Xassembler and -Wa options.", | |
513 | (*decoded_options)[i].arg); | |
514 | xassembler_options_error = true; | |
515 | break; | |
516 | } | |
517 | else if (i == *decoded_options_count && j < fdecoded_options_count) | |
518 | { | |
519 | warning (0, "Extra option to -Xassembler: %s," | |
520 | " dropping all -Xassembler and -Wa options.", | |
521 | fdecoded_options[j].arg); | |
522 | xassembler_options_error = true; | |
523 | break; | |
524 | } | |
525 | else if (strcmp ((*decoded_options)[i].arg, fdecoded_options[j].arg)) | |
526 | { | |
527 | warning (0, "Options to Xassembler do not match: %s, %s," | |
528 | " dropping all -Xassembler and -Wa options.", | |
529 | (*decoded_options)[i].arg, fdecoded_options[j].arg); | |
530 | xassembler_options_error = true; | |
531 | break; | |
532 | } | |
533 | } | |
52a35ef7 | 534 | } |
f31c0018 | 535 | |
fc8b3540 IV |
536 | /* Auxiliary function that frees elements of PTR and PTR itself. |
537 | N is number of elements to be freed. If PTR is NULL, nothing is freed. | |
538 | If an element is NULL, subsequent elements are not freed. */ | |
539 | ||
540 | static void ** | |
541 | free_array_of_ptrs (void **ptr, unsigned n) | |
542 | { | |
543 | if (!ptr) | |
544 | return NULL; | |
545 | for (unsigned i = 0; i < n; i++) | |
546 | { | |
547 | if (!ptr[i]) | |
548 | break; | |
549 | free (ptr[i]); | |
550 | } | |
551 | free (ptr); | |
552 | return NULL; | |
553 | } | |
554 | ||
555 | /* Parse STR, saving found tokens into PVALUES and return their number. | |
556 | Tokens are assumed to be delimited by ':'. If APPEND is non-null, | |
557 | append it to every token we find. */ | |
558 | ||
559 | static unsigned | |
560 | parse_env_var (const char *str, char ***pvalues, const char *append) | |
561 | { | |
562 | const char *curval, *nextval; | |
563 | char **values; | |
564 | unsigned num = 1, i; | |
565 | ||
566 | curval = strchr (str, ':'); | |
567 | while (curval) | |
568 | { | |
569 | num++; | |
570 | curval = strchr (curval + 1, ':'); | |
571 | } | |
572 | ||
573 | values = (char**) xmalloc (num * sizeof (char*)); | |
574 | curval = str; | |
b08dec2f DH |
575 | nextval = strchr (curval, ':'); |
576 | if (nextval == NULL) | |
577 | nextval = strchr (curval, '\0'); | |
fc8b3540 IV |
578 | |
579 | int append_len = append ? strlen (append) : 0; | |
580 | for (i = 0; i < num; i++) | |
581 | { | |
582 | int l = nextval - curval; | |
583 | values[i] = (char*) xmalloc (l + 1 + append_len); | |
584 | memcpy (values[i], curval, l); | |
585 | values[i][l] = 0; | |
586 | if (append) | |
587 | strcat (values[i], append); | |
588 | curval = nextval + 1; | |
b08dec2f DH |
589 | nextval = strchr (curval, ':'); |
590 | if (nextval == NULL) | |
591 | nextval = strchr (curval, '\0'); | |
fc8b3540 IV |
592 | } |
593 | *pvalues = values; | |
594 | return num; | |
595 | } | |
596 | ||
c713ddc0 BS |
597 | /* Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK. */ |
598 | ||
599 | static void | |
600 | append_compiler_options (obstack *argv_obstack, struct cl_decoded_option *opts, | |
601 | unsigned int count) | |
602 | { | |
603 | /* Append compiler driver arguments as far as they were merged. */ | |
604 | for (unsigned int j = 1; j < count; ++j) | |
605 | { | |
606 | struct cl_decoded_option *option = &opts[j]; | |
607 | ||
608 | /* File options have been properly filtered by lto-opts.c. */ | |
609 | switch (option->opt_index) | |
610 | { | |
611 | /* Drop arguments that we want to take from the link line. */ | |
612 | case OPT_flto_: | |
613 | case OPT_flto: | |
614 | case OPT_flto_partition_: | |
615 | continue; | |
616 | ||
617 | default: | |
618 | break; | |
619 | } | |
620 | ||
621 | /* For now do what the original LTO option code was doing - pass | |
622 | on any CL_TARGET flag and a few selected others. */ | |
623 | switch (option->opt_index) | |
624 | { | |
eb472171 | 625 | case OPT_fdiagnostics_show_caret: |
96e6ae57 | 626 | case OPT_fdiagnostics_show_labels: |
56b61d7f | 627 | case OPT_fdiagnostics_show_line_numbers: |
eb472171 CLT |
628 | case OPT_fdiagnostics_show_option: |
629 | case OPT_fdiagnostics_show_location_: | |
630 | case OPT_fshow_column: | |
c713ddc0 BS |
631 | case OPT_fPIC: |
632 | case OPT_fpic: | |
633 | case OPT_fPIE: | |
634 | case OPT_fpie: | |
635 | case OPT_fcommon: | |
c713ddc0 | 636 | case OPT_fgnu_tm: |
1506ae0e | 637 | case OPT_fopenmp: |
a0c88d06 | 638 | case OPT_fopenacc: |
b6adbb9f | 639 | case OPT_fopenacc_dim_: |
c713ddc0 | 640 | case OPT_foffload_abi_: |
5a307ee5 | 641 | case OPT_g: |
c713ddc0 BS |
642 | case OPT_O: |
643 | case OPT_Ofast: | |
644 | case OPT_Og: | |
645 | case OPT_Os: | |
646 | break; | |
647 | ||
f1a681a1 PK |
648 | case OPT_Xassembler: |
649 | /* When we detected a mismatch in assembler options between | |
650 | the input TU's fall back to previous behavior of ignoring them. */ | |
651 | if (xassembler_options_error) | |
652 | continue; | |
653 | break; | |
654 | ||
c713ddc0 BS |
655 | default: |
656 | if (!(cl_options[option->opt_index].flags & CL_TARGET)) | |
657 | continue; | |
658 | } | |
659 | ||
660 | /* Pass the option on. */ | |
661 | for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i) | |
662 | obstack_ptr_grow (argv_obstack, option->canonical_option[i]); | |
663 | } | |
664 | } | |
665 | ||
34df756c TV |
666 | /* Append diag options in OPTS with length COUNT to ARGV_OBSTACK. */ |
667 | ||
668 | static void | |
669 | append_diag_options (obstack *argv_obstack, struct cl_decoded_option *opts, | |
670 | unsigned int count) | |
671 | { | |
672 | /* Append compiler driver arguments as far as they were merged. */ | |
673 | for (unsigned int j = 1; j < count; ++j) | |
674 | { | |
675 | struct cl_decoded_option *option = &opts[j]; | |
676 | ||
677 | switch (option->opt_index) | |
678 | { | |
679 | case OPT_fdiagnostics_color_: | |
478dd60d | 680 | case OPT_fdiagnostics_format_: |
34df756c | 681 | case OPT_fdiagnostics_show_caret: |
96e6ae57 | 682 | case OPT_fdiagnostics_show_labels: |
56b61d7f | 683 | case OPT_fdiagnostics_show_line_numbers: |
34df756c TV |
684 | case OPT_fdiagnostics_show_option: |
685 | case OPT_fdiagnostics_show_location_: | |
686 | case OPT_fshow_column: | |
687 | break; | |
688 | default: | |
689 | continue; | |
690 | } | |
691 | ||
692 | /* Pass the option on. */ | |
693 | for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i) | |
694 | obstack_ptr_grow (argv_obstack, option->canonical_option[i]); | |
695 | } | |
696 | } | |
697 | ||
698 | ||
c713ddc0 BS |
699 | /* Append linker options OPTS to ARGV_OBSTACK. */ |
700 | ||
701 | static void | |
702 | append_linker_options (obstack *argv_obstack, struct cl_decoded_option *opts, | |
703 | unsigned int count) | |
704 | { | |
705 | /* Append linker driver arguments. Compiler options from the linker | |
706 | driver arguments will override / merge with those from the compiler. */ | |
707 | for (unsigned int j = 1; j < count; ++j) | |
708 | { | |
709 | struct cl_decoded_option *option = &opts[j]; | |
710 | ||
711 | /* Do not pass on frontend specific flags not suitable for lto. */ | |
712 | if (!(cl_options[option->opt_index].flags | |
713 | & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO))) | |
714 | continue; | |
715 | ||
716 | switch (option->opt_index) | |
717 | { | |
718 | case OPT_o: | |
719 | case OPT_flto_: | |
720 | case OPT_flto: | |
721 | /* We've handled these LTO options, do not pass them on. */ | |
722 | continue; | |
723 | ||
b0b35f86 JJ |
724 | case OPT_fopenmp: |
725 | case OPT_fopenacc: | |
b0b35f86 JJ |
726 | /* Ignore -fno-XXX form of these options, as otherwise |
727 | corresponding builtins will not be enabled. */ | |
728 | if (option->value == 0) | |
729 | continue; | |
730 | break; | |
731 | ||
c713ddc0 BS |
732 | default: |
733 | break; | |
734 | } | |
735 | ||
736 | /* Pass the option on. */ | |
737 | for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i) | |
738 | obstack_ptr_grow (argv_obstack, option->canonical_option[i]); | |
739 | } | |
740 | } | |
741 | ||
742 | /* Extract options for TARGET offload compiler from OPTIONS and append | |
743 | them to ARGV_OBSTACK. */ | |
744 | ||
745 | static void | |
746 | append_offload_options (obstack *argv_obstack, const char *target, | |
747 | struct cl_decoded_option *options, | |
748 | unsigned int options_count) | |
749 | { | |
750 | for (unsigned i = 0; i < options_count; i++) | |
751 | { | |
752 | const char *cur, *next, *opts; | |
753 | char **argv; | |
754 | unsigned argc; | |
755 | struct cl_decoded_option *option = &options[i]; | |
756 | ||
757 | if (option->opt_index != OPT_foffload_) | |
758 | continue; | |
759 | ||
760 | /* If option argument starts with '-' then no target is specified. That | |
761 | means offload options are specified for all targets, so we need to | |
762 | append them. */ | |
763 | if (option->arg[0] == '-') | |
764 | opts = option->arg; | |
765 | else | |
766 | { | |
767 | opts = strchr (option->arg, '='); | |
64186aad TS |
768 | /* If there are offload targets specified, but no actual options, |
769 | there is nothing to do here. */ | |
c713ddc0 BS |
770 | if (!opts) |
771 | continue; | |
772 | ||
773 | cur = option->arg; | |
774 | ||
775 | while (cur < opts) | |
776 | { | |
b08dec2f DH |
777 | next = strchr (cur, ','); |
778 | if (next == NULL) | |
191ec640 | 779 | next = opts; |
c713ddc0 BS |
780 | next = (next > opts) ? opts : next; |
781 | ||
64186aad | 782 | /* Are we looking for this offload target? */ |
c713ddc0 BS |
783 | if (strlen (target) == (size_t) (next - cur) |
784 | && strncmp (target, cur, next - cur) == 0) | |
785 | break; | |
786 | ||
64186aad | 787 | /* Skip the comma or equal sign. */ |
c713ddc0 BS |
788 | cur = next + 1; |
789 | } | |
790 | ||
791 | if (cur >= opts) | |
792 | continue; | |
793 | ||
794 | opts++; | |
795 | } | |
796 | ||
797 | argv = buildargv (opts); | |
798 | for (argc = 0; argv[argc]; argc++) | |
799 | obstack_ptr_grow (argv_obstack, argv[argc]); | |
800 | } | |
801 | } | |
802 | ||
fc8b3540 IV |
803 | /* Check whether NAME can be accessed in MODE. This is like access, |
804 | except that it never considers directories to be executable. */ | |
805 | ||
806 | static int | |
807 | access_check (const char *name, int mode) | |
808 | { | |
809 | if (mode == X_OK) | |
810 | { | |
811 | struct stat st; | |
812 | ||
813 | if (stat (name, &st) < 0 | |
814 | || S_ISDIR (st.st_mode)) | |
815 | return -1; | |
816 | } | |
817 | ||
818 | return access (name, mode); | |
819 | } | |
820 | ||
821 | /* Prepare a target image for offload TARGET, using mkoffload tool from | |
822 | COMPILER_PATH. Return the name of the resultant object file. */ | |
823 | ||
824 | static char * | |
825 | compile_offload_image (const char *target, const char *compiler_path, | |
c713ddc0 BS |
826 | unsigned in_argc, char *in_argv[], |
827 | struct cl_decoded_option *compiler_opts, | |
828 | unsigned int compiler_opt_count, | |
829 | struct cl_decoded_option *linker_opts, | |
830 | unsigned int linker_opt_count) | |
fc8b3540 IV |
831 | { |
832 | char *filename = NULL; | |
833 | char **argv; | |
834 | char *suffix | |
835 | = XALLOCAVEC (char, sizeof ("/accel//mkoffload") + strlen (target)); | |
836 | strcpy (suffix, "/accel/"); | |
837 | strcat (suffix, target); | |
838 | strcat (suffix, "/mkoffload"); | |
839 | ||
840 | char **paths = NULL; | |
841 | unsigned n_paths = parse_env_var (compiler_path, &paths, suffix); | |
842 | ||
843 | const char *compiler = NULL; | |
844 | for (unsigned i = 0; i < n_paths; i++) | |
845 | if (access_check (paths[i], X_OK) == 0) | |
846 | { | |
847 | compiler = paths[i]; | |
848 | break; | |
849 | } | |
850 | ||
c2e1580c TV |
851 | if (!compiler) |
852 | fatal_error (input_location, | |
a9c697b8 | 853 | "could not find %s in %s (consider using %<-B%>)", |
904f3daa | 854 | suffix + 1, compiler_path); |
c2e1580c TV |
855 | |
856 | /* Generate temporary output file name. */ | |
857 | filename = make_temp_file (".target.o"); | |
858 | ||
859 | struct obstack argv_obstack; | |
860 | obstack_init (&argv_obstack); | |
861 | obstack_ptr_grow (&argv_obstack, compiler); | |
862 | if (save_temps) | |
863 | obstack_ptr_grow (&argv_obstack, "-save-temps"); | |
864 | if (verbose) | |
865 | obstack_ptr_grow (&argv_obstack, "-v"); | |
866 | obstack_ptr_grow (&argv_obstack, "-o"); | |
867 | obstack_ptr_grow (&argv_obstack, filename); | |
868 | ||
869 | /* Append names of input object files. */ | |
870 | for (unsigned i = 0; i < in_argc; i++) | |
871 | obstack_ptr_grow (&argv_obstack, in_argv[i]); | |
872 | ||
873 | /* Append options from offload_lto sections. */ | |
874 | append_compiler_options (&argv_obstack, compiler_opts, | |
875 | compiler_opt_count); | |
876 | append_diag_options (&argv_obstack, linker_opts, linker_opt_count); | |
877 | ||
878 | /* Append options specified by -foffload last. In case of conflicting | |
879 | options we expect offload compiler to choose the latest. */ | |
880 | append_offload_options (&argv_obstack, target, compiler_opts, | |
881 | compiler_opt_count); | |
882 | append_offload_options (&argv_obstack, target, linker_opts, | |
883 | linker_opt_count); | |
884 | ||
885 | obstack_ptr_grow (&argv_obstack, NULL); | |
886 | argv = XOBFINISH (&argv_obstack, char **); | |
887 | fork_execute (argv[0], argv, true); | |
888 | obstack_free (&argv_obstack, NULL); | |
fc8b3540 IV |
889 | |
890 | free_array_of_ptrs ((void **) paths, n_paths); | |
891 | return filename; | |
892 | } | |
893 | ||
894 | ||
895 | /* The main routine dealing with offloading. | |
896 | The routine builds a target image for each offload target. IN_ARGC and | |
897 | IN_ARGV specify options and input object files. As all of them could contain | |
898 | target sections, we pass them all to target compilers. */ | |
899 | ||
900 | static void | |
c713ddc0 BS |
901 | compile_images_for_offload_targets (unsigned in_argc, char *in_argv[], |
902 | struct cl_decoded_option *compiler_opts, | |
903 | unsigned int compiler_opt_count, | |
904 | struct cl_decoded_option *linker_opts, | |
905 | unsigned int linker_opt_count) | |
fc8b3540 IV |
906 | { |
907 | char **names = NULL; | |
908 | const char *target_names = getenv (OFFLOAD_TARGET_NAMES_ENV); | |
909 | if (!target_names) | |
910 | return; | |
911 | unsigned num_targets = parse_env_var (target_names, &names, NULL); | |
912 | ||
b2b40051 | 913 | int next_name_entry = 0; |
fc8b3540 IV |
914 | const char *compiler_path = getenv ("COMPILER_PATH"); |
915 | if (!compiler_path) | |
916 | goto out; | |
917 | ||
918 | /* Prepare an image for each target and save the name of the resultant object | |
919 | file to the OFFLOAD_NAMES array. It is terminated by a NULL entry. */ | |
920 | offload_names = XCNEWVEC (char *, num_targets + 1); | |
921 | for (unsigned i = 0; i < num_targets; i++) | |
922 | { | |
b2b40051 MJ |
923 | /* HSA does not use LTO-like streaming and a different compiler, skip |
924 | it. */ | |
925 | if (strcmp (names[i], "hsa") == 0) | |
926 | continue; | |
927 | ||
928 | offload_names[next_name_entry] | |
c713ddc0 BS |
929 | = compile_offload_image (names[i], compiler_path, in_argc, in_argv, |
930 | compiler_opts, compiler_opt_count, | |
931 | linker_opts, linker_opt_count); | |
b2b40051 | 932 | if (!offload_names[next_name_entry]) |
40fecdd6 | 933 | fatal_error (input_location, |
a9c697b8 | 934 | "problem with building target image for %s", names[i]); |
b2b40051 | 935 | next_name_entry++; |
fc8b3540 IV |
936 | } |
937 | ||
938 | out: | |
939 | free_array_of_ptrs ((void **) names, num_targets); | |
940 | } | |
941 | ||
942 | /* Copy a file from SRC to DEST. */ | |
943 | ||
944 | static void | |
945 | copy_file (const char *dest, const char *src) | |
946 | { | |
947 | FILE *d = fopen (dest, "wb"); | |
948 | FILE *s = fopen (src, "rb"); | |
949 | char buffer[512]; | |
950 | while (!feof (s)) | |
951 | { | |
952 | size_t len = fread (buffer, 1, 512, s); | |
953 | if (ferror (s) != 0) | |
40fecdd6 | 954 | fatal_error (input_location, "reading input file"); |
fc8b3540 IV |
955 | if (len > 0) |
956 | { | |
957 | fwrite (buffer, 1, len, d); | |
958 | if (ferror (d) != 0) | |
40fecdd6 | 959 | fatal_error (input_location, "writing output file"); |
fc8b3540 IV |
960 | } |
961 | } | |
367e91e1 SL |
962 | fclose (d); |
963 | fclose (s); | |
fc8b3540 IV |
964 | } |
965 | ||
e6861a99 IV |
966 | /* Find the crtoffloadtable.o file in LIBRARY_PATH, make copy and pass name of |
967 | the copy to the linker. */ | |
fc8b3540 IV |
968 | |
969 | static void | |
e6861a99 | 970 | find_crtoffloadtable (void) |
fc8b3540 IV |
971 | { |
972 | char **paths = NULL; | |
973 | const char *library_path = getenv ("LIBRARY_PATH"); | |
974 | if (!library_path) | |
975 | return; | |
e6861a99 | 976 | unsigned n_paths = parse_env_var (library_path, &paths, "/crtoffloadtable.o"); |
fc8b3540 IV |
977 | |
978 | unsigned i; | |
979 | for (i = 0; i < n_paths; i++) | |
980 | if (access_check (paths[i], R_OK) == 0) | |
981 | { | |
e6861a99 IV |
982 | /* The linker will delete the filename we give it, so make a copy. */ |
983 | char *crtoffloadtable = make_temp_file (".crtoffloadtable.o"); | |
984 | copy_file (crtoffloadtable, paths[i]); | |
985 | printf ("%s\n", crtoffloadtable); | |
986 | XDELETEVEC (crtoffloadtable); | |
fc8b3540 IV |
987 | break; |
988 | } | |
989 | if (i == n_paths) | |
40fecdd6 | 990 | fatal_error (input_location, |
a9c697b8 | 991 | "installation error, cannot find %<crtoffloadtable.o%>"); |
fc8b3540 IV |
992 | |
993 | free_array_of_ptrs ((void **) paths, n_paths); | |
994 | } | |
995 | ||
c713ddc0 BS |
996 | /* A subroutine of run_gcc. Examine the open file FD for lto sections with |
997 | name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS | |
998 | and OPT_COUNT. Return true if we found a matchingn section, false | |
999 | otherwise. COLLECT_GCC holds the value of the environment variable with | |
1000 | the same name. */ | |
1001 | ||
1002 | static bool | |
1003 | find_and_merge_options (int fd, off_t file_offset, const char *prefix, | |
1004 | struct cl_decoded_option **opts, | |
1005 | unsigned int *opt_count, const char *collect_gcc) | |
1006 | { | |
1007 | off_t offset, length; | |
1008 | char *data; | |
1009 | char *fopts; | |
1010 | const char *errmsg; | |
1011 | int err; | |
1012 | struct cl_decoded_option *fdecoded_options = *opts; | |
1013 | unsigned int fdecoded_options_count = *opt_count; | |
1014 | ||
1015 | simple_object_read *sobj; | |
1016 | sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO", | |
1017 | &errmsg, &err); | |
1018 | if (!sobj) | |
1019 | return false; | |
1020 | ||
1021 | char *secname = XALLOCAVEC (char, strlen (prefix) + sizeof (".opts")); | |
1022 | strcpy (secname, prefix); | |
1023 | strcat (secname, ".opts"); | |
1024 | if (!simple_object_find_section (sobj, secname, &offset, &length, | |
1025 | &errmsg, &err)) | |
1026 | { | |
1027 | simple_object_release_read (sobj); | |
1028 | return false; | |
1029 | } | |
1030 | ||
1031 | lseek (fd, file_offset + offset, SEEK_SET); | |
1032 | data = (char *)xmalloc (length); | |
1033 | read (fd, data, length); | |
1034 | fopts = data; | |
1035 | do | |
1036 | { | |
1037 | struct cl_decoded_option *f2decoded_options; | |
1038 | unsigned int f2decoded_options_count; | |
8508ae1d | 1039 | get_options_from_collect_gcc_options (collect_gcc, fopts, |
c713ddc0 BS |
1040 | &f2decoded_options, |
1041 | &f2decoded_options_count); | |
1042 | if (!fdecoded_options) | |
1043 | { | |
1044 | fdecoded_options = f2decoded_options; | |
1045 | fdecoded_options_count = f2decoded_options_count; | |
1046 | } | |
1047 | else | |
1048 | merge_and_complain (&fdecoded_options, | |
1049 | &fdecoded_options_count, | |
1050 | f2decoded_options, f2decoded_options_count); | |
1051 | ||
1052 | fopts += strlen (fopts) + 1; | |
1053 | } | |
1054 | while (fopts - data < length); | |
1055 | ||
1056 | free (data); | |
1057 | simple_object_release_read (sobj); | |
1058 | *opts = fdecoded_options; | |
1059 | *opt_count = fdecoded_options_count; | |
1060 | return true; | |
1061 | } | |
1062 | ||
1ea85365 RB |
1063 | /* Copy early debug info sections from INFILE to a new file whose name |
1064 | is returned. Return NULL on error. */ | |
1065 | ||
1066 | const char * | |
0df8dc6e | 1067 | debug_objcopy (const char *infile, bool rename) |
1ea85365 | 1068 | { |
b5327e50 | 1069 | char *outfile; |
1ea85365 RB |
1070 | const char *errmsg; |
1071 | int err; | |
1072 | ||
1073 | const char *p; | |
fe267711 | 1074 | const char *orig_infile = infile; |
1ea85365 RB |
1075 | off_t inoff = 0; |
1076 | long loffset; | |
1077 | int consumed; | |
1078 | if ((p = strrchr (infile, '@')) | |
1079 | && p != infile | |
1080 | && sscanf (p, "@%li%n", &loffset, &consumed) >= 1 | |
1081 | && strlen (p) == (unsigned int) consumed) | |
1082 | { | |
1083 | char *fname = xstrdup (infile); | |
1084 | fname[p - infile] = '\0'; | |
1085 | infile = fname; | |
1086 | inoff = (off_t) loffset; | |
1087 | } | |
dc0e0c6b | 1088 | int infd = open (infile, O_RDONLY | O_BINARY); |
1ea85365 RB |
1089 | if (infd == -1) |
1090 | return NULL; | |
1091 | simple_object_read *inobj = simple_object_start_read (infd, inoff, | |
1092 | "__GNU_LTO", | |
1093 | &errmsg, &err); | |
1094 | if (!inobj) | |
1095 | return NULL; | |
1096 | ||
1097 | off_t off, len; | |
1098 | if (simple_object_find_section (inobj, ".gnu.debuglto_.debug_info", | |
1099 | &off, &len, &errmsg, &err) != 1) | |
1100 | { | |
1101 | if (errmsg) | |
a9c697b8 | 1102 | fatal_error (0, "%s: %s", errmsg, xstrerror (err)); |
1ea85365 RB |
1103 | |
1104 | simple_object_release_read (inobj); | |
1105 | close (infd); | |
1106 | return NULL; | |
1107 | } | |
1108 | ||
b5327e50 | 1109 | if (save_temps) |
1dedc12d | 1110 | outfile = concat (orig_infile, ".debug.temp.o", NULL); |
b5327e50 IS |
1111 | else |
1112 | outfile = make_temp_file (".debug.temp.o"); | |
0df8dc6e | 1113 | errmsg = simple_object_copy_lto_debug_sections (inobj, outfile, &err, rename); |
1ea85365 RB |
1114 | if (errmsg) |
1115 | { | |
1116 | unlink_if_ordinary (outfile); | |
a9c697b8 | 1117 | fatal_error (0, "%s: %s", errmsg, xstrerror (err)); |
1ea85365 RB |
1118 | } |
1119 | ||
1120 | simple_object_release_read (inobj); | |
1121 | close (infd); | |
1122 | ||
1123 | return outfile; | |
1124 | } | |
1125 | ||
b6e33d73 JH |
1126 | /* Helper for qsort: compare priorities for parallel compilation. */ |
1127 | ||
1128 | int | |
1129 | cmp_priority (const void *a, const void *b) | |
1130 | { | |
1131 | return *((const int *)b)-*((const int *)a); | |
1132 | } | |
1ea85365 | 1133 | |
200b0e7e ML |
1134 | /* Number of CPUs that can be used for parallel LTRANS phase. */ |
1135 | ||
1136 | static unsigned long nthreads_var = 0; | |
1137 | ||
1138 | #ifdef HAVE_PTHREAD_AFFINITY_NP | |
1139 | unsigned long cpuset_size; | |
1140 | static unsigned long get_cpuset_size; | |
1141 | cpu_set_t *cpusetp; | |
1142 | ||
1143 | unsigned long | |
1144 | static cpuset_popcount (unsigned long cpusetsize, cpu_set_t *cpusetp) | |
1145 | { | |
1146 | #ifdef CPU_COUNT_S | |
1147 | /* glibc 2.7 and above provide a macro for this. */ | |
1148 | return CPU_COUNT_S (cpusetsize, cpusetp); | |
1149 | #else | |
1150 | #ifdef CPU_COUNT | |
1151 | if (cpusetsize == sizeof (cpu_set_t)) | |
1152 | /* glibc 2.6 and above provide a macro for this. */ | |
1153 | return CPU_COUNT (cpusetp); | |
1154 | #endif | |
1155 | size_t i; | |
1156 | unsigned long ret = 0; | |
1157 | STATIC_ASSERT (sizeof (cpusetp->__bits[0]) == sizeof (unsigned long int)); | |
1158 | for (i = 0; i < cpusetsize / sizeof (cpusetp->__bits[0]); i++) | |
1159 | { | |
1160 | unsigned long int mask = cpusetp->__bits[i]; | |
1161 | if (mask == 0) | |
1162 | continue; | |
1163 | ret += __builtin_popcountl (mask); | |
1164 | } | |
1165 | return ret; | |
1166 | #endif | |
1167 | } | |
1168 | #endif | |
1169 | ||
1170 | /* At startup, determine the default number of threads. It would seem | |
1171 | this should be related to the number of cpus online. */ | |
1172 | ||
1173 | static void | |
1174 | init_num_threads (void) | |
1175 | { | |
1176 | #ifdef HAVE_PTHREAD_AFFINITY_NP | |
1177 | #if defined (_SC_NPROCESSORS_CONF) && defined (CPU_ALLOC_SIZE) | |
1178 | cpuset_size = sysconf (_SC_NPROCESSORS_CONF); | |
1179 | cpuset_size = CPU_ALLOC_SIZE (cpuset_size); | |
1180 | #else | |
1181 | cpuset_size = sizeof (cpu_set_t); | |
1182 | #endif | |
1183 | ||
1184 | cpusetp = (cpu_set_t *) xmalloc (gomp_cpuset_size); | |
1185 | do | |
1186 | { | |
1187 | int ret = pthread_getaffinity_np (pthread_self (), gomp_cpuset_size, | |
1188 | cpusetp); | |
1189 | if (ret == 0) | |
1190 | { | |
1191 | /* Count only the CPUs this process can use. */ | |
1192 | nthreads_var = cpuset_popcount (cpuset_size, cpusetp); | |
1193 | if (nthreads_var == 0) | |
1194 | break; | |
1195 | get_cpuset_size = cpuset_size; | |
1196 | #ifdef CPU_ALLOC_SIZE | |
1197 | unsigned long i; | |
1198 | for (i = cpuset_size * 8; i; i--) | |
1199 | if (CPU_ISSET_S (i - 1, cpuset_size, cpusetp)) | |
1200 | break; | |
1201 | cpuset_size = CPU_ALLOC_SIZE (i); | |
1202 | #endif | |
1203 | return; | |
1204 | } | |
1205 | if (ret != EINVAL) | |
1206 | break; | |
1207 | #ifdef CPU_ALLOC_SIZE | |
1208 | if (cpuset_size < sizeof (cpu_set_t)) | |
1209 | cpuset_size = sizeof (cpu_set_t); | |
1210 | else | |
1211 | cpuset_size = cpuset_size * 2; | |
1212 | if (cpuset_size < 8 * sizeof (cpu_set_t)) | |
1213 | cpusetp | |
1214 | = (cpu_set_t *) realloc (cpusetp, cpuset_size); | |
1215 | else | |
1216 | { | |
1217 | /* Avoid fatal if too large memory allocation would be | |
1218 | requested, e.g. kernel returning EINVAL all the time. */ | |
1219 | void *p = realloc (cpusetp, cpuset_size); | |
1220 | if (p == NULL) | |
1221 | break; | |
1222 | cpusetp = (cpu_set_t *) p; | |
1223 | } | |
1224 | #else | |
1225 | break; | |
1226 | #endif | |
1227 | } | |
1228 | while (1); | |
1229 | cpuset_size = 0; | |
1230 | nthreads_var = 1; | |
1231 | free (cpusetp); | |
1232 | cpusetp = NULL; | |
1233 | #endif | |
1234 | #ifdef _SC_NPROCESSORS_ONLN | |
1235 | nthreads_var = sysconf (_SC_NPROCESSORS_ONLN); | |
1236 | #endif | |
1237 | } | |
1238 | ||
917e56a9 | 1239 | /* FIXME: once using -std=c++11, we can use std::thread::hardware_concurrency. */ |
200b0e7e | 1240 | |
0f62caf5 | 1241 | /* Test and return reason why a jobserver cannot be detected. */ |
200b0e7e | 1242 | |
0f62caf5 | 1243 | static const char * |
200b0e7e ML |
1244 | jobserver_active_p (void) |
1245 | { | |
0f62caf5 ML |
1246 | #define JS_PREFIX "jobserver is not available: " |
1247 | #define JS_NEEDLE "--jobserver-auth=" | |
1248 | ||
200b0e7e ML |
1249 | const char *makeflags = getenv ("MAKEFLAGS"); |
1250 | if (makeflags == NULL) | |
0f62caf5 | 1251 | return JS_PREFIX "%<MAKEFLAGS%> environment variable is unset"; |
200b0e7e | 1252 | |
0f62caf5 | 1253 | const char *n = strstr (makeflags, JS_NEEDLE); |
200b0e7e | 1254 | if (n == NULL) |
0f62caf5 | 1255 | return JS_PREFIX "%<" JS_NEEDLE "%> is not present in %<MAKEFLAGS%>"; |
200b0e7e ML |
1256 | |
1257 | int rfd = -1; | |
1258 | int wfd = -1; | |
1259 | ||
0f62caf5 ML |
1260 | if (sscanf (n + strlen (JS_NEEDLE), "%d,%d", &rfd, &wfd) == 2 |
1261 | && rfd > 0 | |
1262 | && wfd > 0 | |
1263 | && is_valid_fd (rfd) | |
1264 | && is_valid_fd (wfd)) | |
1265 | return NULL; | |
1266 | else | |
1267 | return JS_PREFIX "cannot access %<" JS_NEEDLE "%> file descriptors"; | |
200b0e7e | 1268 | } |
1ea85365 | 1269 | |
d7f09764 DN |
1270 | /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */ |
1271 | ||
1272 | static void | |
1273 | run_gcc (unsigned argc, char *argv[]) | |
1274 | { | |
cf96bae7 | 1275 | unsigned i, j; |
d7f09764 DN |
1276 | const char **new_argv; |
1277 | const char **argv_ptr; | |
d7f09764 | 1278 | char *list_option_full = NULL; |
cf96bae7 | 1279 | const char *linker_output = NULL; |
f1a681a1 PK |
1280 | const char *collect_gcc; |
1281 | char *collect_gcc_options; | |
279dc7a3 | 1282 | int parallel = 0; |
a478ffff | 1283 | int jobserver = 0; |
200b0e7e | 1284 | int auto_parallel = 0; |
014d92e1 | 1285 | bool no_partition = false; |
52a35ef7 | 1286 | struct cl_decoded_option *fdecoded_options = NULL; |
c713ddc0 | 1287 | struct cl_decoded_option *offload_fdecoded_options = NULL; |
52a35ef7 | 1288 | unsigned int fdecoded_options_count = 0; |
c713ddc0 | 1289 | unsigned int offload_fdecoded_options_count = 0; |
f31c0018 RG |
1290 | struct cl_decoded_option *decoded_options; |
1291 | unsigned int decoded_options_count; | |
ef6f874e RG |
1292 | struct obstack argv_obstack; |
1293 | int new_head_argc; | |
fc8b3540 IV |
1294 | bool have_lto = false; |
1295 | bool have_offload = false; | |
1ea85365 RB |
1296 | unsigned lto_argc = 0, ltoobj_argc = 0; |
1297 | char **lto_argv, **ltoobj_argv; | |
0df8dc6e | 1298 | bool linker_output_rel = false; |
1ea85365 RB |
1299 | bool skip_debug = false; |
1300 | unsigned n_debugobj; | |
1dedc12d AO |
1301 | const char *dumppfx = NULL, *incoming_dumppfx = NULL; |
1302 | static char current_dir[] = { '.', DIR_SEPARATOR, '\0' }; | |
cf96bae7 RG |
1303 | |
1304 | /* Get the driver and options. */ | |
1305 | collect_gcc = getenv ("COLLECT_GCC"); | |
1306 | if (!collect_gcc) | |
40fecdd6 | 1307 | fatal_error (input_location, |
a9c697b8 | 1308 | "environment variable %<COLLECT_GCC%> must be set"); |
cf96bae7 RG |
1309 | collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS"); |
1310 | if (!collect_gcc_options) | |
40fecdd6 | 1311 | fatal_error (input_location, |
a9c697b8 | 1312 | "environment variable %<COLLECT_GCC_OPTIONS%> must be set"); |
f1a681a1 PK |
1313 | |
1314 | char *collect_as_options = getenv ("COLLECT_AS_OPTIONS"); | |
1315 | ||
1316 | /* Prepend -Xassembler to each option, and append the string | |
1317 | to collect_gcc_options. */ | |
1318 | if (collect_as_options) | |
1319 | { | |
1320 | obstack temporary_obstack; | |
1321 | obstack_init (&temporary_obstack); | |
1322 | ||
1323 | prepend_xassembler_to_collect_as_options (collect_as_options, | |
1324 | &temporary_obstack); | |
1325 | obstack_1grow (&temporary_obstack, '\0'); | |
1326 | ||
1327 | char *xassembler_opts_string | |
1328 | = XOBFINISH (&temporary_obstack, char *); | |
98ff89d1 ML |
1329 | collect_gcc_options = concat (collect_gcc_options, xassembler_opts_string, |
1330 | NULL); | |
f1a681a1 PK |
1331 | } |
1332 | ||
f31c0018 | 1333 | get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options, |
f31c0018 RG |
1334 | &decoded_options, |
1335 | &decoded_options_count); | |
cf96bae7 | 1336 | |
e6861a99 | 1337 | /* Allocate array for input object files with LTO IL, |
443743fd IV |
1338 | and for possible preceding arguments. */ |
1339 | lto_argv = XNEWVEC (char *, argc); | |
1ea85365 | 1340 | ltoobj_argv = XNEWVEC (char *, argc); |
443743fd | 1341 | |
52a35ef7 RG |
1342 | /* Look at saved options in the IL files. */ |
1343 | for (i = 1; i < argc; ++i) | |
1344 | { | |
c713ddc0 | 1345 | char *p; |
52a35ef7 | 1346 | int fd; |
c713ddc0 | 1347 | off_t file_offset = 0; |
52a35ef7 | 1348 | long loffset; |
52a35ef7 | 1349 | int consumed; |
52a35ef7 | 1350 | char *filename = argv[i]; |
c713ddc0 | 1351 | |
e6861a99 IV |
1352 | if (strncmp (argv[i], "-foffload-objects=", |
1353 | sizeof ("-foffload-objects=") - 1) == 0) | |
1354 | { | |
1355 | have_offload = true; | |
1356 | offload_objects_file_name | |
1357 | = argv[i] + sizeof ("-foffload-objects=") - 1; | |
1358 | continue; | |
1359 | } | |
1360 | ||
52a35ef7 RG |
1361 | if ((p = strrchr (argv[i], '@')) |
1362 | && p != argv[i] | |
1363 | && sscanf (p, "@%li%n", &loffset, &consumed) >= 1 | |
1364 | && strlen (p) == (unsigned int) consumed) | |
1365 | { | |
1366 | filename = XNEWVEC (char, p - argv[i] + 1); | |
1367 | memcpy (filename, argv[i], p - argv[i]); | |
1368 | filename[p - argv[i]] = '\0'; | |
1369 | file_offset = (off_t) loffset; | |
1370 | } | |
1473ab9a | 1371 | fd = open (filename, O_RDONLY | O_BINARY); |
0df8dc6e JH |
1372 | /* Linker plugin passes -fresolution and -flinker-output options. |
1373 | -flinker-output is passed only when user did not specify one and thus | |
1374 | we do not need to worry about duplicities with the option handling | |
1375 | below. */ | |
52a35ef7 | 1376 | if (fd == -1) |
443743fd IV |
1377 | { |
1378 | lto_argv[lto_argc++] = argv[i]; | |
0df8dc6e JH |
1379 | if (strcmp (argv[i], "-flinker-output=rel") == 0) |
1380 | linker_output_rel = true; | |
443743fd IV |
1381 | continue; |
1382 | } | |
1383 | ||
1384 | if (find_and_merge_options (fd, file_offset, LTO_SECTION_NAME_PREFIX, | |
1385 | &fdecoded_options, &fdecoded_options_count, | |
1386 | collect_gcc)) | |
1387 | { | |
1388 | have_lto = true; | |
1ea85365 | 1389 | ltoobj_argv[ltoobj_argc++] = argv[i]; |
443743fd | 1390 | } |
52a35ef7 RG |
1391 | close (fd); |
1392 | } | |
1393 | ||
cf96bae7 | 1394 | /* Initalize the common arguments for the driver. */ |
ef6f874e RG |
1395 | obstack_init (&argv_obstack); |
1396 | obstack_ptr_grow (&argv_obstack, collect_gcc); | |
1397 | obstack_ptr_grow (&argv_obstack, "-xlto"); | |
1398 | obstack_ptr_grow (&argv_obstack, "-c"); | |
52a35ef7 | 1399 | |
c713ddc0 BS |
1400 | append_compiler_options (&argv_obstack, fdecoded_options, |
1401 | fdecoded_options_count); | |
1402 | append_linker_options (&argv_obstack, decoded_options, decoded_options_count); | |
52a35ef7 | 1403 | |
c713ddc0 | 1404 | /* Scan linker driver arguments for things that are of relevance to us. */ |
f31c0018 RG |
1405 | for (j = 1; j < decoded_options_count; ++j) |
1406 | { | |
1407 | struct cl_decoded_option *option = &decoded_options[j]; | |
f31c0018 RG |
1408 | switch (option->opt_index) |
1409 | { | |
1410 | case OPT_o: | |
1411 | linker_output = option->arg; | |
c713ddc0 | 1412 | break; |
f31c0018 | 1413 | |
1dedc12d AO |
1414 | /* We don't have to distinguish between -save-temps=* and |
1415 | -save-temps, -dumpdir already carries that | |
1416 | information. */ | |
1417 | case OPT_save_temps_: | |
f31c0018 | 1418 | case OPT_save_temps: |
608508a6 | 1419 | save_temps = 1; |
f31c0018 RG |
1420 | break; |
1421 | ||
1422 | case OPT_v: | |
cf96bae7 | 1423 | verbose = 1; |
f31c0018 | 1424 | break; |
cf96bae7 | 1425 | |
783dab6b RB |
1426 | case OPT_flto_partition_: |
1427 | if (strcmp (option->arg, "none") == 0) | |
1428 | no_partition = true; | |
f31c0018 RG |
1429 | break; |
1430 | ||
1431 | case OPT_flto_: | |
1432 | if (strcmp (option->arg, "jobserver") == 0) | |
19566bdd ML |
1433 | { |
1434 | parallel = 1; | |
1435 | jobserver = 1; | |
1436 | } | |
279dc7a3 ML |
1437 | else if (strcmp (option->arg, "auto") == 0) |
1438 | { | |
1439 | parallel = 1; | |
1440 | auto_parallel = 1; | |
1441 | } | |
f31c0018 RG |
1442 | else |
1443 | { | |
1444 | parallel = atoi (option->arg); | |
1445 | if (parallel <= 1) | |
1446 | parallel = 0; | |
1447 | } | |
1448 | /* Fallthru. */ | |
1449 | ||
1450 | case OPT_flto: | |
1451 | lto_mode = LTO_MODE_WHOPR; | |
c713ddc0 | 1452 | break; |
8bb50e5c | 1453 | |
0df8dc6e JH |
1454 | case OPT_flinker_output_: |
1455 | linker_output_rel = !strcmp (option->arg, "rel"); | |
1456 | break; | |
1457 | ||
5a307ee5 RB |
1458 | case OPT_g: |
1459 | /* Recognize -g0. */ | |
1460 | skip_debug = option->arg && !strcmp (option->arg, "0"); | |
1461 | break; | |
0df8dc6e | 1462 | |
1dedc12d AO |
1463 | case OPT_dumpdir: |
1464 | incoming_dumppfx = dumppfx = option->arg; | |
1465 | break; | |
1466 | ||
f31c0018 RG |
1467 | default: |
1468 | break; | |
1469 | } | |
f31c0018 RG |
1470 | } |
1471 | ||
1ea85365 RB |
1472 | /* Output lto-wrapper invocation command. */ |
1473 | if (verbose) | |
1474 | { | |
1475 | for (i = 0; i < argc; ++i) | |
1476 | { | |
1477 | fputs (argv[i], stderr); | |
1478 | fputc (' ', stderr); | |
1479 | } | |
1480 | fputc ('\n', stderr); | |
1481 | } | |
1482 | ||
0df8dc6e JH |
1483 | if (linker_output_rel) |
1484 | no_partition = true; | |
1485 | ||
014d92e1 JH |
1486 | if (no_partition) |
1487 | { | |
1488 | lto_mode = LTO_MODE_LTO; | |
1489 | jobserver = 0; | |
200b0e7e | 1490 | auto_parallel = 0; |
014d92e1 JH |
1491 | parallel = 0; |
1492 | } | |
0f62caf5 | 1493 | else |
19566bdd | 1494 | { |
0f62caf5 ML |
1495 | const char *jobserver_error = jobserver_active_p (); |
1496 | if (jobserver && jobserver_error != NULL) | |
1497 | warning (0, jobserver_error); | |
1498 | else if (!jobserver && jobserver_error == NULL) | |
1499 | { | |
1500 | parallel = 1; | |
1501 | jobserver = 1; | |
1502 | } | |
19566bdd | 1503 | } |
cf96bae7 | 1504 | |
1dedc12d | 1505 | if (!dumppfx) |
cf96bae7 | 1506 | { |
1dedc12d AO |
1507 | if (!linker_output |
1508 | || strcmp (linker_output, HOST_BIT_BUCKET) == 0) | |
1509 | dumppfx = "a."; | |
1510 | else | |
dd3b31fb | 1511 | { |
1dedc12d AO |
1512 | const char *obase = lbasename (linker_output), *temp; |
1513 | ||
1514 | /* Strip the executable extension. */ | |
1515 | size_t blen = strlen (obase), xlen; | |
1516 | if ((temp = strrchr (obase + 1, '.')) | |
1517 | && (xlen = strlen (temp)) | |
1518 | && (strcmp (temp, ".exe") == 0 | |
1519 | #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX) | |
1520 | || strcmp (temp, TARGET_EXECUTABLE_SUFFIX) == 0 | |
1521 | #endif | |
1522 | || strcmp (obase, "a.out") == 0)) | |
1523 | dumppfx = xstrndup (linker_output, | |
1524 | obase - linker_output + blen - xlen + 1); | |
1525 | else | |
1526 | dumppfx = concat (linker_output, ".", NULL); | |
dd3b31fb | 1527 | } |
1dedc12d | 1528 | } |
cf96bae7 | 1529 | |
1dedc12d AO |
1530 | /* If there's no directory component in the dumppfx, add one, so |
1531 | that, when it is used as -dumpbase, it overrides any occurrence | |
1532 | of -dumpdir that might have been passed in. */ | |
1533 | if (!dumppfx || lbasename (dumppfx) == dumppfx) | |
1534 | dumppfx = concat (current_dir, dumppfx, NULL); | |
1535 | ||
1536 | /* Make sure some -dumpdir is passed, so as to get predictable | |
1537 | -dumpbase overriding semantics. If we got an incoming -dumpdir | |
1538 | argument, we'll pass it on, so don't bother with another one | |
1539 | then. */ | |
1540 | if (!incoming_dumppfx) | |
1541 | { | |
1542 | obstack_ptr_grow (&argv_obstack, "-dumpdir"); | |
1543 | obstack_ptr_grow (&argv_obstack, ""); | |
cf96bae7 | 1544 | } |
1dedc12d | 1545 | obstack_ptr_grow (&argv_obstack, "-dumpbase"); |
ef6f874e RG |
1546 | |
1547 | /* Remember at which point we can scrub args to re-use the commons. */ | |
1548 | new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *); | |
d7f09764 | 1549 | |
fc8b3540 IV |
1550 | if (have_offload) |
1551 | { | |
e6861a99 IV |
1552 | unsigned i, num_offload_files; |
1553 | char **offload_argv; | |
1554 | FILE *f; | |
1555 | ||
1556 | f = fopen (offload_objects_file_name, "r"); | |
1557 | if (f == NULL) | |
1558 | fatal_error (input_location, "cannot open %s: %m", | |
1559 | offload_objects_file_name); | |
1560 | if (fscanf (f, "%u ", &num_offload_files) != 1) | |
1561 | fatal_error (input_location, "cannot read %s: %m", | |
1562 | offload_objects_file_name); | |
1563 | offload_argv = XCNEWVEC (char *, num_offload_files); | |
1564 | ||
1565 | /* Read names of object files with offload. */ | |
1566 | for (i = 0; i < num_offload_files; i++) | |
1567 | { | |
1568 | const unsigned piece = 32; | |
1569 | char *buf, *filename = XNEWVEC (char, piece); | |
1570 | size_t len; | |
1571 | ||
1572 | buf = filename; | |
1573 | cont1: | |
1574 | if (!fgets (buf, piece, f)) | |
1575 | break; | |
1576 | len = strlen (filename); | |
1577 | if (filename[len - 1] != '\n') | |
1578 | { | |
1579 | filename = XRESIZEVEC (char, filename, len + piece); | |
1580 | buf = filename + len; | |
1581 | goto cont1; | |
1582 | } | |
1583 | filename[len - 1] = '\0'; | |
1584 | offload_argv[i] = filename; | |
1585 | } | |
1586 | fclose (f); | |
1587 | if (offload_argv[num_offload_files - 1] == NULL) | |
1588 | fatal_error (input_location, "invalid format of %s", | |
1589 | offload_objects_file_name); | |
1590 | maybe_unlink (offload_objects_file_name); | |
1591 | offload_objects_file_name = NULL; | |
1592 | ||
1593 | /* Look at saved offload options in files. */ | |
1594 | for (i = 0; i < num_offload_files; i++) | |
1595 | { | |
1596 | char *p; | |
1597 | long loffset; | |
1598 | int fd, consumed; | |
1599 | off_t file_offset = 0; | |
1600 | char *filename = offload_argv[i]; | |
1601 | ||
1602 | if ((p = strrchr (offload_argv[i], '@')) | |
1603 | && p != offload_argv[i] | |
1604 | && sscanf (p, "@%li%n", &loffset, &consumed) >= 1 | |
1605 | && strlen (p) == (unsigned int) consumed) | |
1606 | { | |
1607 | filename = XNEWVEC (char, p - offload_argv[i] + 1); | |
1608 | memcpy (filename, offload_argv[i], p - offload_argv[i]); | |
1609 | filename[p - offload_argv[i]] = '\0'; | |
1610 | file_offset = (off_t) loffset; | |
1611 | } | |
1612 | fd = open (filename, O_RDONLY | O_BINARY); | |
1613 | if (fd == -1) | |
1614 | fatal_error (input_location, "cannot open %s: %m", filename); | |
1615 | if (!find_and_merge_options (fd, file_offset, | |
1616 | OFFLOAD_SECTION_NAME_PREFIX, | |
1617 | &offload_fdecoded_options, | |
1618 | &offload_fdecoded_options_count, | |
1619 | collect_gcc)) | |
1620 | fatal_error (input_location, "cannot read %s: %m", filename); | |
1621 | close (fd); | |
1622 | if (filename != offload_argv[i]) | |
1623 | XDELETEVEC (filename); | |
1624 | } | |
1625 | ||
1626 | compile_images_for_offload_targets (num_offload_files, offload_argv, | |
443743fd | 1627 | offload_fdecoded_options, |
c713ddc0 BS |
1628 | offload_fdecoded_options_count, |
1629 | decoded_options, | |
1630 | decoded_options_count); | |
e6861a99 IV |
1631 | |
1632 | free_array_of_ptrs ((void **) offload_argv, num_offload_files); | |
1633 | ||
fc8b3540 IV |
1634 | if (offload_names) |
1635 | { | |
e6861a99 | 1636 | find_crtoffloadtable (); |
fc8b3540 IV |
1637 | for (i = 0; offload_names[i]; i++) |
1638 | printf ("%s\n", offload_names[i]); | |
1639 | free_array_of_ptrs ((void **) offload_names, i); | |
1640 | } | |
1641 | } | |
1642 | ||
fc8b3540 IV |
1643 | /* If object files contain offload sections, but do not contain LTO sections, |
1644 | then there is no need to perform a link-time recompilation, i.e. | |
1645 | lto-wrapper is used only for a compilation of offload images. */ | |
1646 | if (have_offload && !have_lto) | |
e6861a99 | 1647 | goto finish; |
fc8b3540 | 1648 | |
d7f09764 DN |
1649 | if (lto_mode == LTO_MODE_LTO) |
1650 | { | |
1dedc12d AO |
1651 | /* -dumpbase argument for LTO. */ |
1652 | flto_out = concat (dumppfx, "lto.o", NULL); | |
1653 | obstack_ptr_grow (&argv_obstack, flto_out); | |
1654 | ||
1655 | if (!save_temps) | |
b5327e50 | 1656 | flto_out = make_temp_file (".lto.o"); |
ef6f874e RG |
1657 | obstack_ptr_grow (&argv_obstack, "-o"); |
1658 | obstack_ptr_grow (&argv_obstack, flto_out); | |
d7f09764 | 1659 | } |
be286227 | 1660 | else |
d7f09764 DN |
1661 | { |
1662 | const char *list_option = "-fltrans-output-list="; | |
d7f09764 | 1663 | |
1dedc12d AO |
1664 | /* -dumpbase argument for WPA. */ |
1665 | char *dumpbase = concat (dumppfx, "wpa", NULL); | |
1666 | obstack_ptr_grow (&argv_obstack, dumpbase); | |
cf96bae7 | 1667 | |
1dedc12d AO |
1668 | if (save_temps) |
1669 | ltrans_output_file = concat (dumppfx, "ltrans.out", NULL); | |
b5611987 | 1670 | else |
1dedc12d AO |
1671 | ltrans_output_file = make_temp_file (".ltrans.out"); |
1672 | list_option_full = concat (list_option, ltrans_output_file, NULL); | |
1673 | obstack_ptr_grow (&argv_obstack, list_option_full); | |
d7f09764 | 1674 | |
f300e7b8 | 1675 | if (jobserver) |
200b0e7e ML |
1676 | { |
1677 | if (verbose) | |
1678 | fprintf (stderr, "Using make jobserver\n"); | |
1679 | obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver")); | |
1680 | } | |
1681 | else if (auto_parallel) | |
1682 | { | |
1683 | char buf[256]; | |
1684 | init_num_threads (); | |
1685 | if (verbose) | |
1686 | fprintf (stderr, "LTO parallelism level set to %ld\n", | |
1687 | nthreads_var); | |
1688 | sprintf (buf, "-fwpa=%ld", nthreads_var); | |
1689 | obstack_ptr_grow (&argv_obstack, xstrdup (buf)); | |
1690 | } | |
f300e7b8 JH |
1691 | else if (parallel > 1) |
1692 | { | |
1693 | char buf[256]; | |
1694 | sprintf (buf, "-fwpa=%i", parallel); | |
1695 | obstack_ptr_grow (&argv_obstack, xstrdup (buf)); | |
1696 | } | |
1697 | else | |
1698 | obstack_ptr_grow (&argv_obstack, "-fwpa"); | |
d7f09764 | 1699 | } |
d7f09764 | 1700 | |
1ea85365 | 1701 | /* Append input arguments. */ |
443743fd IV |
1702 | for (i = 0; i < lto_argc; ++i) |
1703 | obstack_ptr_grow (&argv_obstack, lto_argv[i]); | |
1ea85365 RB |
1704 | /* Append the input objects. */ |
1705 | for (i = 0; i < ltoobj_argc; ++i) | |
1706 | obstack_ptr_grow (&argv_obstack, ltoobj_argv[i]); | |
ef6f874e | 1707 | obstack_ptr_grow (&argv_obstack, NULL); |
d7f09764 | 1708 | |
ef6f874e RG |
1709 | new_argv = XOBFINISH (&argv_obstack, const char **); |
1710 | argv_ptr = &new_argv[new_head_argc]; | |
5f0ad6a5 | 1711 | fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true); |
48cf395b | 1712 | |
1ea85365 RB |
1713 | /* Copy the early generated debug info from the objects to temporary |
1714 | files and append those to the partial link commandline. */ | |
1715 | n_debugobj = 0; | |
b5327e50 | 1716 | early_debug_object_names = NULL; |
1ea85365 | 1717 | if (! skip_debug) |
1ea85365 | 1718 | { |
b5327e50 IS |
1719 | early_debug_object_names = XCNEWVEC (const char *, ltoobj_argc+ 1); |
1720 | num_deb_objs = ltoobj_argc; | |
1721 | for (i = 0; i < ltoobj_argc; ++i) | |
1ea85365 | 1722 | { |
b5327e50 IS |
1723 | const char *tem; |
1724 | if ((tem = debug_objcopy (ltoobj_argv[i], !linker_output_rel))) | |
1725 | { | |
1726 | early_debug_object_names[i] = tem; | |
1727 | n_debugobj++; | |
1728 | } | |
1ea85365 RB |
1729 | } |
1730 | } | |
1ea85365 | 1731 | |
d7f09764 DN |
1732 | if (lto_mode == LTO_MODE_LTO) |
1733 | { | |
c3284718 | 1734 | printf ("%s\n", flto_out); |
1ea85365 RB |
1735 | if (!skip_debug) |
1736 | { | |
b5327e50 IS |
1737 | for (i = 0; i < ltoobj_argc; ++i) |
1738 | if (early_debug_object_names[i] != NULL) | |
1739 | printf ("%s\n", early_debug_object_names[i]); | |
1ea85365 | 1740 | } |
b5327e50 | 1741 | /* These now belong to collect2. */ |
d7f09764 DN |
1742 | free (flto_out); |
1743 | flto_out = NULL; | |
b5327e50 IS |
1744 | free (early_debug_object_names); |
1745 | early_debug_object_names = NULL; | |
d7f09764 | 1746 | } |
be286227 | 1747 | else |
d7f09764 DN |
1748 | { |
1749 | FILE *stream = fopen (ltrans_output_file, "r"); | |
c04b6b38 | 1750 | FILE *mstream = NULL; |
4559db79 | 1751 | struct obstack env_obstack; |
b6e33d73 | 1752 | int priority; |
d7f09764 DN |
1753 | |
1754 | if (!stream) | |
a9c697b8 | 1755 | fatal_error (input_location, "%<fopen%>: %s: %m", ltrans_output_file); |
d7f09764 | 1756 | |
50ee30d5 | 1757 | /* Parse the list of LTRANS inputs from the WPA stage. */ |
4559db79 | 1758 | obstack_init (&env_obstack); |
50ee30d5 | 1759 | nr = 0; |
48cf395b RB |
1760 | for (;;) |
1761 | { | |
1762 | const unsigned piece = 32; | |
50ee30d5 | 1763 | char *output_name = NULL; |
48cf395b RB |
1764 | char *buf, *input_name = (char *)xmalloc (piece); |
1765 | size_t len; | |
1766 | ||
1767 | buf = input_name; | |
b6e33d73 JH |
1768 | if (fscanf (stream, "%i\n", &priority) != 1) |
1769 | { | |
1770 | if (!feof (stream)) | |
1771 | fatal_error (input_location, | |
a9c697b8 | 1772 | "corrupted ltrans output file %s", |
b6e33d73 JH |
1773 | ltrans_output_file); |
1774 | break; | |
1775 | } | |
48cf395b RB |
1776 | cont: |
1777 | if (!fgets (buf, piece, stream)) | |
1778 | break; | |
1779 | len = strlen (input_name); | |
1780 | if (input_name[len - 1] != '\n') | |
1781 | { | |
1782 | input_name = (char *)xrealloc (input_name, len + piece); | |
1783 | buf = input_name + len; | |
1784 | goto cont; | |
1785 | } | |
1786 | input_name[len - 1] = '\0'; | |
1787 | ||
1788 | if (input_name[0] == '*') | |
c04b6b38 | 1789 | output_name = &input_name[1]; |
48cf395b | 1790 | |
c04b6b38 | 1791 | nr++; |
b6e33d73 JH |
1792 | ltrans_priorities |
1793 | = (int *)xrealloc (ltrans_priorities, nr * sizeof (int) * 2); | |
c04b6b38 RG |
1794 | input_names = (char **)xrealloc (input_names, nr * sizeof (char *)); |
1795 | output_names = (char **)xrealloc (output_names, nr * sizeof (char *)); | |
b6e33d73 JH |
1796 | ltrans_priorities[(nr-1)*2] = priority; |
1797 | ltrans_priorities[(nr-1)*2+1] = nr-1; | |
c04b6b38 RG |
1798 | input_names[nr-1] = input_name; |
1799 | output_names[nr-1] = output_name; | |
1800 | } | |
50ee30d5 | 1801 | fclose (stream); |
a185856a | 1802 | maybe_unlink (ltrans_output_file); |
50ee30d5 RG |
1803 | ltrans_output_file = NULL; |
1804 | ||
1805 | if (parallel) | |
1806 | { | |
1807 | makefile = make_temp_file (".mk"); | |
1808 | mstream = fopen (makefile, "w"); | |
b6e33d73 | 1809 | qsort (ltrans_priorities, nr, sizeof (int) * 2, cmp_priority); |
50ee30d5 RG |
1810 | } |
1811 | ||
1812 | /* Execute the LTRANS stage for each input file (or prepare a | |
1813 | makefile to invoke this in parallel). */ | |
1814 | for (i = 0; i < nr; ++i) | |
1815 | { | |
1816 | char *output_name; | |
1817 | char *input_name = input_names[i]; | |
1818 | /* If it's a pass-through file do nothing. */ | |
1819 | if (output_names[i]) | |
1820 | continue; | |
1821 | ||
1822 | /* Replace the .o suffix with a .ltrans.o suffix and write | |
1823 | the resulting name to the LTRANS output list. */ | |
50ee30d5 RG |
1824 | obstack_grow (&env_obstack, input_name, strlen (input_name) - 2); |
1825 | obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o")); | |
1826 | output_name = XOBFINISH (&env_obstack, char *); | |
1827 | ||
1828 | /* Adjust the dumpbase if the linker output file was seen. */ | |
1dedc12d AO |
1829 | int dumpbase_len = (strlen (dumppfx) + sizeof (DUMPBASE_SUFFIX)); |
1830 | char *dumpbase = (char *) xmalloc (dumpbase_len + 1); | |
1831 | snprintf (dumpbase, dumpbase_len, "%sltrans%u.ltrans", dumppfx, i); | |
1832 | argv_ptr[0] = dumpbase; | |
50ee30d5 RG |
1833 | |
1834 | argv_ptr[1] = "-fltrans"; | |
1835 | argv_ptr[2] = "-o"; | |
1836 | argv_ptr[3] = output_name; | |
1837 | argv_ptr[4] = input_name; | |
1838 | argv_ptr[5] = NULL; | |
1839 | if (parallel) | |
1840 | { | |
1841 | fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]); | |
1842 | for (j = 1; new_argv[j] != NULL; ++j) | |
1843 | fprintf (mstream, " '%s'", new_argv[j]); | |
1844 | fprintf (mstream, "\n"); | |
9d69847d RG |
1845 | /* If we are not preserving the ltrans input files then |
1846 | truncate them as soon as we have processed it. This | |
1847 | reduces temporary disk-space usage. */ | |
608508a6 | 1848 | if (! save_temps) |
9d69847d RG |
1849 | fprintf (mstream, "\t@-touch -r %s %s.tem > /dev/null 2>&1 " |
1850 | "&& mv %s.tem %s\n", | |
1851 | input_name, input_name, input_name, input_name); | |
50ee30d5 RG |
1852 | } |
1853 | else | |
9d69847d | 1854 | { |
5f0ad6a5 BS |
1855 | fork_execute (new_argv[0], CONST_CAST (char **, new_argv), |
1856 | true); | |
a185856a | 1857 | maybe_unlink (input_name); |
9d69847d | 1858 | } |
50ee30d5 RG |
1859 | |
1860 | output_names[i] = output_name; | |
1861 | } | |
c04b6b38 RG |
1862 | if (parallel) |
1863 | { | |
1864 | struct pex_obj *pex; | |
1865 | char jobs[32]; | |
a478ffff | 1866 | |
bb52a82a RB |
1867 | fprintf (mstream, |
1868 | ".PHONY: all\n" | |
1869 | "all:"); | |
c04b6b38 | 1870 | for (i = 0; i < nr; ++i) |
b6e33d73 JH |
1871 | { |
1872 | int j = ltrans_priorities[i*2 + 1]; | |
1873 | fprintf (mstream, " \\\n\t%s", output_names[j]); | |
1874 | } | |
c04b6b38 RG |
1875 | fprintf (mstream, "\n"); |
1876 | fclose (mstream); | |
a478ffff AK |
1877 | if (!jobserver) |
1878 | { | |
1879 | /* Avoid passing --jobserver-fd= and similar flags | |
1880 | unless jobserver mode is explicitly enabled. */ | |
1881 | putenv (xstrdup ("MAKEFLAGS=")); | |
1882 | putenv (xstrdup ("MFLAGS=")); | |
1883 | } | |
b24fc8a6 ML |
1884 | |
1885 | char **make_argv = buildargv (getenv ("MAKE")); | |
1886 | if (make_argv) | |
1887 | { | |
1888 | for (unsigned argc = 0; make_argv[argc]; argc++) | |
1889 | obstack_ptr_grow (&argv_obstack, make_argv[argc]); | |
1890 | } | |
1891 | else | |
1892 | obstack_ptr_grow (&argv_obstack, "make"); | |
1893 | ||
1894 | obstack_ptr_grow (&argv_obstack, "-f"); | |
1895 | obstack_ptr_grow (&argv_obstack, makefile); | |
a478ffff AK |
1896 | if (!jobserver) |
1897 | { | |
200b0e7e ML |
1898 | snprintf (jobs, 31, "-j%ld", |
1899 | auto_parallel ? nthreads_var : parallel); | |
b24fc8a6 | 1900 | obstack_ptr_grow (&argv_obstack, jobs); |
a478ffff | 1901 | } |
b24fc8a6 ML |
1902 | obstack_ptr_grow (&argv_obstack, "all"); |
1903 | obstack_ptr_grow (&argv_obstack, NULL); | |
1904 | new_argv = XOBFINISH (&argv_obstack, const char **); | |
1905 | ||
5f0ad6a5 BS |
1906 | pex = collect_execute (new_argv[0], CONST_CAST (char **, new_argv), |
1907 | NULL, NULL, PEX_SEARCH, false); | |
1908 | do_wait (new_argv[0], pex); | |
b24fc8a6 | 1909 | freeargv (make_argv); |
a185856a | 1910 | maybe_unlink (makefile); |
50ee30d5 | 1911 | makefile = NULL; |
9d69847d | 1912 | for (i = 0; i < nr; ++i) |
a185856a | 1913 | maybe_unlink (input_names[i]); |
c04b6b38 RG |
1914 | } |
1915 | for (i = 0; i < nr; ++i) | |
1916 | { | |
1917 | fputs (output_names[i], stdout); | |
48cf395b | 1918 | putc ('\n', stdout); |
c04b6b38 | 1919 | free (input_names[i]); |
48cf395b | 1920 | } |
b5327e50 IS |
1921 | if (!skip_debug) |
1922 | { | |
1923 | for (i = 0; i < ltoobj_argc; ++i) | |
1924 | if (early_debug_object_names[i] != NULL) | |
1925 | printf ("%s\n", early_debug_object_names[i]); | |
1926 | } | |
50ee30d5 | 1927 | nr = 0; |
b6e33d73 | 1928 | free (ltrans_priorities); |
c04b6b38 | 1929 | free (output_names); |
b5327e50 IS |
1930 | output_names = NULL; |
1931 | free (early_debug_object_names); | |
1932 | early_debug_object_names = NULL; | |
c04b6b38 | 1933 | free (input_names); |
d7f09764 | 1934 | free (list_option_full); |
4559db79 | 1935 | obstack_free (&env_obstack, NULL); |
d7f09764 | 1936 | } |
ef6f874e | 1937 | |
fc8b3540 | 1938 | finish: |
443743fd | 1939 | XDELETE (lto_argv); |
ef6f874e | 1940 | obstack_free (&argv_obstack, NULL); |
d7f09764 DN |
1941 | } |
1942 | ||
1943 | ||
1944 | /* Entry point. */ | |
1945 | ||
1946 | int | |
1947 | main (int argc, char *argv[]) | |
1948 | { | |
2691e6d7 JM |
1949 | const char *p; |
1950 | ||
de5672fc | 1951 | init_opts_obstack (); |
dc357798 | 1952 | |
2691e6d7 JM |
1953 | p = argv[0] + strlen (argv[0]); |
1954 | while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1])) | |
1955 | --p; | |
1956 | progname = p; | |
1957 | ||
1958 | xmalloc_set_program_name (progname); | |
1959 | ||
d7f09764 DN |
1960 | gcc_init_libintl (); |
1961 | ||
2691e6d7 JM |
1962 | diagnostic_initialize (global_dc, 0); |
1963 | ||
877088b7 | 1964 | if (atexit (lto_wrapper_cleanup) != 0) |
a9c697b8 | 1965 | fatal_error (input_location, "%<atexit%> failed"); |
877088b7 | 1966 | |
396717c9 RG |
1967 | if (signal (SIGINT, SIG_IGN) != SIG_IGN) |
1968 | signal (SIGINT, fatal_signal); | |
1969 | #ifdef SIGHUP | |
1970 | if (signal (SIGHUP, SIG_IGN) != SIG_IGN) | |
1971 | signal (SIGHUP, fatal_signal); | |
1972 | #endif | |
1973 | if (signal (SIGTERM, SIG_IGN) != SIG_IGN) | |
1974 | signal (SIGTERM, fatal_signal); | |
1975 | #ifdef SIGPIPE | |
1976 | if (signal (SIGPIPE, SIG_IGN) != SIG_IGN) | |
1977 | signal (SIGPIPE, fatal_signal); | |
1978 | #endif | |
1979 | #ifdef SIGCHLD | |
1980 | /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will | |
1981 | receive the signal. A different setting is inheritable */ | |
1982 | signal (SIGCHLD, SIG_DFL); | |
1983 | #endif | |
1984 | ||
d7f09764 DN |
1985 | /* We may be called with all the arguments stored in some file and |
1986 | passed with @file. Expand them into argv before processing. */ | |
1987 | expandargv (&argc, &argv); | |
f31c0018 | 1988 | |
cf96bae7 | 1989 | run_gcc (argc, argv); |
d7f09764 DN |
1990 | |
1991 | return 0; | |
1992 | } |