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