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