]> git.ipfire.org Git - thirdparty/gcc.git/blob - lto-plugin/lto-plugin.c
API extension for binutils (type of symbols).
[thirdparty/gcc.git] / lto-plugin / lto-plugin.c
1 /* LTO plugin for gold and/or GNU ld.
2 Copyright (C) 2009-2020 Free Software Foundation, Inc.
3 Contributed by Rafael Avila de Espindola (espindola@google.com).
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING3. If not see
17 <http://www.gnu.org/licenses/>. */
18
19 /* The plugin has only one external function: onload. Gold passes it an array of
20 function that the plugin uses to communicate back to gold.
21
22 With the functions provided by gold, the plugin can be notified when
23 gold first analyzes a file and pass a symbol table back to gold. The plugin
24 is also notified when all symbols have been read and it is time to generate
25 machine code for the necessary symbols.
26
27 More information at http://gcc.gnu.org/wiki/whopr/driver.
28
29 This plugin should be passed the lto-wrapper options and will forward them.
30 It also has options at his own:
31 -debug: Print the command line used to run lto-wrapper.
32 -nop: Instead of running lto-wrapper, pass the original to the plugin. This
33 only works if the input files are hybrid.
34 -linker-output-known: Do not determine linker output
35 -sym-style={none,win32,underscore|uscore}
36 -pass-through */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41 #if HAVE_STDINT_H
42 #include <stdint.h>
43 #endif
44 #include <stdbool.h>
45 #include <assert.h>
46 #include <errno.h>
47 #include <string.h>
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <inttypes.h>
51 #include <sys/stat.h>
52 #include <unistd.h>
53 #include <fcntl.h>
54 #include <sys/types.h>
55 #ifdef HAVE_SYS_WAIT_H
56 #include <sys/wait.h>
57 #endif
58 #ifndef WIFEXITED
59 #define WIFEXITED(S) (((S) & 0xff) == 0)
60 #endif
61 #ifndef WEXITSTATUS
62 #define WEXITSTATUS(S) (((S) & 0xff00) >> 8)
63 #endif
64 #include <libiberty.h>
65 #include <hashtab.h>
66 #include "../gcc/lto/common.h"
67 #include "simple-object.h"
68 #include "plugin-api.h"
69
70 /* We need to use I64 instead of ll width-specifier on native Windows.
71 The reason for this is that older MS-runtimes don't support the ll. */
72 #ifdef __MINGW32__
73 #define PRI_LL "I64"
74 #else
75 #define PRI_LL "ll"
76 #endif
77
78 /* Handle opening elf files on hosts, such as Windows, that may use
79 text file handling that will break binary access. */
80 #ifndef O_BINARY
81 # define O_BINARY 0
82 #endif
83
84 /* Segment name for LTO sections. This is only used for Mach-O.
85 FIXME: This needs to be kept in sync with darwin.c. */
86
87 #define LTO_SEGMENT_NAME "__GNU_LTO"
88
89 /* LTO magic section name. */
90
91 #define LTO_SYMTAB_PREFIX ".gnu.lto_.symtab"
92 #define LTO_SYMTAB_PREFIX_LEN (sizeof (LTO_SYMTAB_PREFIX) - 1)
93 #define LTO_SYMTAB_EXT_PREFIX ".gnu.lto_.ext_symtab"
94 #define LTO_SYMTAB_EXT_PREFIX_LEN (sizeof (LTO_SYMTAB_EXT_PREFIX) - 1)
95 #define LTO_LTO_PREFIX ".gnu.lto_.lto"
96 #define LTO_LTO_PREFIX_LEN (sizeof (LTO_LTO_PREFIX) - 1)
97 #define OFFLOAD_SECTION ".gnu.offload_lto_.opts"
98 #define OFFLOAD_SECTION_LEN (sizeof (OFFLOAD_SECTION) - 1)
99
100 /* The part of the symbol table the plugin has to keep track of. Note that we
101 must keep SYMS until all_symbols_read is called to give the linker time to
102 copy the symbol information.
103 The id must be 64bit to minimze collisions. */
104
105 struct sym_aux
106 {
107 uint32_t slot;
108 unsigned long long id;
109 unsigned next_conflict;
110 };
111
112 struct plugin_symtab
113 {
114 int nsyms;
115 struct sym_aux *aux;
116 struct ld_plugin_symbol *syms;
117 unsigned long long id;
118 };
119
120 /* Encapsulates object file data during symbol scan. */
121 struct plugin_objfile
122 {
123 int found;
124 int offload;
125 simple_object_read *objfile;
126 struct plugin_symtab *out;
127 const struct ld_plugin_input_file *file;
128 };
129
130 /* All that we have to remember about a file. */
131
132 struct plugin_file_info
133 {
134 char *name;
135 void *handle;
136 struct plugin_symtab symtab;
137 struct plugin_symtab conflicts;
138 };
139
140 /* List item with name of the file with offloading. */
141
142 struct plugin_offload_file
143 {
144 char *name;
145 struct plugin_offload_file *next;
146 };
147
148 /* Until ASM_OUTPUT_LABELREF can be hookized and decoupled from
149 stdio file streams, we do simple label translation here. */
150
151 enum symbol_style
152 {
153 ss_none, /* No underscore prefix. */
154 ss_win32, /* Underscore prefix any symbol not beginning with '@'. */
155 ss_uscore, /* Underscore prefix all symbols. */
156 };
157
158 static char *arguments_file_name;
159 static ld_plugin_register_claim_file register_claim_file;
160 static ld_plugin_register_all_symbols_read register_all_symbols_read;
161 static ld_plugin_get_symbols get_symbols, get_symbols_v2;
162 static ld_plugin_register_cleanup register_cleanup;
163 static ld_plugin_add_input_file add_input_file;
164 static ld_plugin_add_input_library add_input_library;
165 static ld_plugin_message message;
166 static ld_plugin_add_symbols add_symbols, add_symbols_v2;
167
168 static struct plugin_file_info *claimed_files = NULL;
169 static unsigned int num_claimed_files = 0;
170 static unsigned int non_claimed_files = 0;
171
172 /* List of files with offloading. */
173 static struct plugin_offload_file *offload_files;
174 /* Last file in the list. */
175 static struct plugin_offload_file *offload_files_last;
176 /* Last non-archive file in the list. */
177 static struct plugin_offload_file *offload_files_last_obj;
178 /* Last LTO file in the list. */
179 static struct plugin_offload_file *offload_files_last_lto;
180 /* Total number of files with offloading. */
181 static unsigned num_offload_files;
182
183 static char **output_files = NULL;
184 static unsigned int num_output_files = 0;
185
186 static char **lto_wrapper_argv;
187 static int lto_wrapper_num_args;
188
189 static char **pass_through_items = NULL;
190 static unsigned int num_pass_through_items;
191
192 static bool debug;
193 static bool save_temps;
194 static bool verbose;
195 static char nop;
196 static char *resolution_file = NULL;
197 static enum ld_plugin_output_file_type linker_output;
198 static int linker_output_set;
199 static int linker_output_known;
200 static const char *link_output_name = NULL;
201
202 /* The version of gold being used, or -1 if not gold. The number is
203 MAJOR * 100 + MINOR. */
204 static int gold_version = -1;
205
206 /* Not used by default, but can be overridden at runtime
207 by using -plugin-opt=-sym-style={none,win32,underscore|uscore}
208 (in fact, only first letter of style arg is checked.) */
209 static enum symbol_style sym_style = ss_none;
210
211 static void
212 check_1 (int gate, enum ld_plugin_level level, const char *text)
213 {
214 if (gate)
215 return;
216
217 if (message)
218 message (level, text);
219 else
220 {
221 /* If there is no nicer way to inform the user, fallback to stderr. */
222 fprintf (stderr, "%s\n", text);
223 if (level == LDPL_FATAL)
224 abort ();
225 }
226 }
227
228 /* This little wrapper allows check to be called with a non-integer
229 first argument, such as a pointer that must be non-NULL. We can't
230 use c99 bool type to coerce it into range, so we explicitly test. */
231 #define check(GATE, LEVEL, TEXT) check_1 (((GATE) != 0), (LEVEL), (TEXT))
232
233 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
234 by P and the result is written in ENTRY. The slot number is stored in SLOT.
235 Returns the address of the next entry. */
236
237 static char *
238 parse_table_entry (char *p, struct ld_plugin_symbol *entry,
239 struct sym_aux *aux)
240 {
241 unsigned char t;
242 enum ld_plugin_symbol_kind translate_kind[] =
243 {
244 LDPK_DEF,
245 LDPK_WEAKDEF,
246 LDPK_UNDEF,
247 LDPK_WEAKUNDEF,
248 LDPK_COMMON
249 };
250
251 enum ld_plugin_symbol_visibility translate_visibility[] =
252 {
253 LDPV_DEFAULT,
254 LDPV_PROTECTED,
255 LDPV_INTERNAL,
256 LDPV_HIDDEN
257 };
258
259 switch (sym_style)
260 {
261 case ss_win32:
262 if (p[0] == '@')
263 {
264 /* cf. Duff's device. */
265 case ss_none:
266 entry->name = xstrdup (p);
267 break;
268 }
269 /* FALL-THROUGH. */
270 case ss_uscore:
271 entry->name = concat ("_", p, NULL);
272 break;
273 default:
274 check (0, LDPL_FATAL, "invalid symbol style requested");
275 break;
276 }
277 while (*p)
278 p++;
279 p++;
280
281 entry->version = NULL;
282
283 entry->comdat_key = p;
284 while (*p)
285 p++;
286 p++;
287
288 if (strlen (entry->comdat_key) == 0)
289 entry->comdat_key = NULL;
290 else
291 entry->comdat_key = xstrdup (entry->comdat_key);
292
293 entry->unused = entry->section_kind = entry->symbol_type = 0;
294
295 t = *p;
296 check (t <= 4, LDPL_FATAL, "invalid symbol kind found");
297 entry->def = translate_kind[t];
298 p++;
299
300 t = *p;
301 check (t <= 3, LDPL_FATAL, "invalid symbol visibility found");
302 entry->visibility = translate_visibility[t];
303 p++;
304
305 memcpy (&entry->size, p, sizeof (uint64_t));
306 p += 8;
307
308 memcpy (&aux->slot, p, sizeof (uint32_t));
309 p += 4;
310
311 entry->resolution = LDPR_UNKNOWN;
312
313 aux->next_conflict = -1;
314
315 return p;
316 }
317
318 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
319 by P and the result is written in ENTRY. The slot number is stored in SLOT.
320 Returns the address of the next entry. */
321
322 static char *
323 parse_table_entry_extension (char *p, struct ld_plugin_symbol *entry)
324 {
325 unsigned char t;
326 enum ld_plugin_symbol_type symbol_types[] =
327 {
328 LDST_UNKNOWN,
329 LDST_FUNCTION,
330 LDST_VARIABLE,
331 };
332
333 t = *p;
334 check (t <= 2, LDPL_FATAL, "invalid symbol type found");
335 entry->symbol_type = symbol_types[t];
336 p++;
337 entry->section_kind = *p;
338 p++;
339
340 return p;
341 }
342
343
344 /* Translate the IL symbol table located between DATA and END. Append the
345 slots and symbols to OUT. */
346
347 static void
348 translate (char *data, char *end, struct plugin_symtab *out)
349 {
350 struct sym_aux *aux;
351 struct ld_plugin_symbol *syms = NULL;
352 int n, len;
353
354 /* This overestimates the output buffer sizes, but at least
355 the algorithm is O(1) now. */
356
357 len = (end - data)/8 + out->nsyms + 1;
358 syms = xrealloc (out->syms, len * sizeof (struct ld_plugin_symbol));
359 aux = xrealloc (out->aux, len * sizeof (struct sym_aux));
360
361 for (n = out->nsyms; data < end; n++)
362 {
363 aux[n].id = out->id;
364 data = parse_table_entry (data, &syms[n], &aux[n]);
365 }
366
367 assert(n < len);
368
369 out->nsyms = n;
370 out->syms = syms;
371 out->aux = aux;
372 }
373
374 static void
375 parse_symtab_extension (char *data, char *end, struct plugin_symtab *out)
376 {
377 unsigned i;
378
379 unsigned char version = *data;
380 data++;
381
382 /* Version 1 contains the following data per entry:
383 - symbol_type
384 - section_kind
385 . */
386
387 if (version == 1)
388 for (i = 0; i < out->nsyms; i++)
389 data = parse_table_entry_extension (data, &out->syms[i]);
390 }
391
392 /* Free all memory that is no longer needed after writing the symbol
393 resolution. */
394
395 static void
396 free_1 (struct plugin_file_info *files, unsigned num_files)
397 {
398 unsigned int i;
399 for (i = 0; i < num_files; i++)
400 {
401 struct plugin_file_info *info = &files[i];
402 struct plugin_symtab *symtab = &info->symtab;
403 unsigned int j;
404 for (j = 0; j < symtab->nsyms; j++)
405 {
406 struct ld_plugin_symbol *s = &symtab->syms[j];
407 free (s->name);
408 free (s->comdat_key);
409 }
410 free (symtab->syms);
411 symtab->syms = NULL;
412 }
413 }
414
415 /* Free all remaining memory. */
416
417 static void
418 free_2 (void)
419 {
420 unsigned int i;
421 for (i = 0; i < num_claimed_files; i++)
422 {
423 struct plugin_file_info *info = &claimed_files[i];
424 struct plugin_symtab *symtab = &info->symtab;
425 free (symtab->aux);
426 free (info->name);
427 }
428
429 for (i = 0; i < num_output_files; i++)
430 free (output_files[i]);
431 free (output_files);
432
433 free (claimed_files);
434 claimed_files = NULL;
435 num_claimed_files = 0;
436
437 while (offload_files)
438 {
439 struct plugin_offload_file *ofld = offload_files;
440 offload_files = offload_files->next;
441 free (ofld);
442 }
443 num_offload_files = 0;
444
445 free (arguments_file_name);
446 arguments_file_name = NULL;
447 }
448
449 /* Dump SYMTAB to resolution file F. */
450
451 static void
452 dump_symtab (FILE *f, struct plugin_symtab *symtab)
453 {
454 unsigned j;
455
456 for (j = 0; j < symtab->nsyms; j++)
457 {
458 uint32_t slot = symtab->aux[j].slot;
459 unsigned int resolution = symtab->syms[j].resolution;
460
461 assert (resolution != LDPR_UNKNOWN);
462
463 fprintf (f, "%u %" PRI_LL "x %s %s\n",
464 (unsigned int) slot, symtab->aux[j].id,
465 lto_resolution_str[resolution],
466 symtab->syms[j].name);
467 }
468 }
469
470 /* Finish the conflicts' resolution information after the linker resolved
471 the original symbols */
472
473 static void
474 finish_conflict_resolution (struct plugin_symtab *symtab,
475 struct plugin_symtab *conflicts)
476 {
477 int i, j;
478
479 if (conflicts->nsyms == 0)
480 return;
481
482 for (i = 0; i < symtab->nsyms; i++)
483 {
484 char resolution = LDPR_UNKNOWN;
485
486 if (symtab->aux[i].next_conflict == -1)
487 continue;
488
489 switch (symtab->syms[i].def)
490 {
491 case LDPK_DEF:
492 case LDPK_COMMON: /* ??? */
493 resolution = LDPR_RESOLVED_IR;
494 break;
495 case LDPK_WEAKDEF:
496 resolution = LDPR_PREEMPTED_IR;
497 break;
498 case LDPK_UNDEF:
499 case LDPK_WEAKUNDEF:
500 resolution = symtab->syms[i].resolution;
501 break;
502 default:
503 assert (0);
504 }
505
506 assert (resolution != LDPR_UNKNOWN);
507
508 for (j = symtab->aux[i].next_conflict;
509 j != -1;
510 j = conflicts->aux[j].next_conflict)
511 conflicts->syms[j].resolution = resolution;
512 }
513 }
514
515 /* Free symbol table SYMTAB. */
516
517 static void
518 free_symtab (struct plugin_symtab *symtab)
519 {
520 free (symtab->syms);
521 symtab->syms = NULL;
522 free (symtab->aux);
523 symtab->aux = NULL;
524 }
525
526 /* Writes the relocations to disk. */
527
528 static void
529 write_resolution (void)
530 {
531 unsigned int i;
532 FILE *f;
533
534 check (resolution_file, LDPL_FATAL, "resolution file not specified");
535 f = fopen (resolution_file, "w");
536 check (f, LDPL_FATAL, "could not open file");
537
538 fprintf (f, "%d\n", num_claimed_files);
539
540 for (i = 0; i < num_claimed_files; i++)
541 {
542 struct plugin_file_info *info = &claimed_files[i];
543 struct plugin_symtab *symtab = &info->symtab;
544 struct ld_plugin_symbol *syms = symtab->syms;
545
546 /* Version 2 of API supports IRONLY_EXP resolution that is
547 accepted by GCC-4.7 and newer. */
548 if (get_symbols_v2)
549 get_symbols_v2 (info->handle, symtab->nsyms, syms);
550 else
551 get_symbols (info->handle, symtab->nsyms, syms);
552
553 finish_conflict_resolution (symtab, &info->conflicts);
554
555 fprintf (f, "%s %d\n", info->name, symtab->nsyms + info->conflicts.nsyms);
556 dump_symtab (f, symtab);
557 if (info->conflicts.nsyms)
558 {
559 dump_symtab (f, &info->conflicts);
560 free_symtab (&info->conflicts);
561 }
562 }
563 fclose (f);
564 }
565
566 /* Pass files generated by the lto-wrapper to the linker. FD is lto-wrapper's
567 stdout. */
568
569 static void
570 add_output_files (FILE *f)
571 {
572 for (;;)
573 {
574 const unsigned piece = 32;
575 char *buf, *s = xmalloc (piece);
576 size_t len;
577
578 buf = s;
579 cont:
580 if (!fgets (buf, piece, f))
581 {
582 free (s);
583 break;
584 }
585 len = strlen (s);
586 if (s[len - 1] != '\n')
587 {
588 s = xrealloc (s, len + piece);
589 buf = s + len;
590 goto cont;
591 }
592 s[len - 1] = '\0';
593
594 num_output_files++;
595 output_files
596 = xrealloc (output_files, num_output_files * sizeof (char *));
597 output_files[num_output_files - 1] = s;
598 add_input_file (output_files[num_output_files - 1]);
599 }
600 }
601
602 /* Execute the lto-wrapper. ARGV[0] is the binary. The rest of ARGV is the
603 argument list. */
604
605 static void
606 exec_lto_wrapper (char *argv[])
607 {
608 int t, i;
609 int status;
610 char *at_args;
611 FILE *args;
612 FILE *wrapper_output;
613 char *new_argv[3];
614 struct pex_obj *pex;
615 const char *errmsg;
616
617 /* Write argv to a file to avoid a command line that is too long
618 Save the file locally on save-temps. */
619 if (save_temps && link_output_name)
620 {
621 arguments_file_name = (char *) xmalloc (strlen (link_output_name)
622 + sizeof (".lto_wrapper_args") + 1);
623 strcpy (arguments_file_name, link_output_name);
624 strcat (arguments_file_name, ".lto_wrapper_args");
625 }
626 else
627 arguments_file_name = make_temp_file (".lto_wrapper_args");
628 check (arguments_file_name, LDPL_FATAL,
629 "Failed to generate a temorary file name");
630
631 args = fopen (arguments_file_name, "w");
632 check (args, LDPL_FATAL, "could not open arguments file");
633
634 t = writeargv (&argv[1], args);
635 check (t == 0, LDPL_FATAL, "could not write arguments");
636 t = fclose (args);
637 check (t == 0, LDPL_FATAL, "could not close arguments file");
638
639 at_args = concat ("@", arguments_file_name, NULL);
640 check (at_args, LDPL_FATAL, "could not allocate");
641
642 for (i = 1; argv[i]; i++)
643 {
644 char *a = argv[i];
645 /* Check the input argument list for a verbose marker too. */
646 if (a[0] == '-' && a[1] == 'v' && a[2] == '\0')
647 {
648 verbose = true;
649 break;
650 }
651 }
652
653 if (verbose)
654 {
655 for (i = 0; argv[i]; i++)
656 fprintf (stderr, "%s ", argv[i]);
657 fprintf (stderr, "\n");
658 }
659
660 new_argv[0] = argv[0];
661 new_argv[1] = at_args;
662 new_argv[2] = NULL;
663
664 if (debug)
665 {
666 for (i = 0; new_argv[i]; i++)
667 fprintf (stderr, "%s ", new_argv[i]);
668 fprintf (stderr, "\n");
669 }
670
671 pex = pex_init (PEX_USE_PIPES, "lto-wrapper", NULL);
672 check (pex != NULL, LDPL_FATAL, "could not pex_init lto-wrapper");
673
674 errmsg = pex_run (pex, 0, new_argv[0], new_argv, NULL, NULL, &t);
675 check (errmsg == NULL, LDPL_FATAL, "could not run lto-wrapper");
676 check (t == 0, LDPL_FATAL, "could not run lto-wrapper");
677
678 wrapper_output = pex_read_output (pex, 0);
679 check (wrapper_output, LDPL_FATAL, "could not read lto-wrapper output");
680
681 add_output_files (wrapper_output);
682
683 t = pex_get_status (pex, 1, &status);
684 check (t == 1, LDPL_FATAL, "could not get lto-wrapper exit status");
685 check (WIFEXITED (status) && WEXITSTATUS (status) == 0, LDPL_FATAL,
686 "lto-wrapper failed");
687
688 pex_free (pex);
689
690 free (at_args);
691 }
692
693 /* Pass the original files back to the linker. */
694
695 static void
696 use_original_files (void)
697 {
698 unsigned i;
699 for (i = 0; i < num_claimed_files; i++)
700 {
701 struct plugin_file_info *info = &claimed_files[i];
702 add_input_file (info->name);
703 }
704 }
705
706
707 /* Called by the linker once all symbols have been read. */
708
709 static enum ld_plugin_status
710 all_symbols_read_handler (void)
711 {
712 unsigned i;
713 unsigned num_lto_args = num_claimed_files + lto_wrapper_num_args + 2
714 + !linker_output_known;
715 char **lto_argv;
716 const char *linker_output_str = NULL;
717 const char **lto_arg_ptr;
718 if (num_claimed_files + num_offload_files == 0)
719 return LDPS_OK;
720
721 if (nop)
722 {
723 use_original_files ();
724 return LDPS_OK;
725 }
726
727 lto_argv = (char **) xcalloc (sizeof (char *), num_lto_args);
728 lto_arg_ptr = (const char **) lto_argv;
729 assert (lto_wrapper_argv);
730
731 write_resolution ();
732
733 free_1 (claimed_files, num_claimed_files);
734
735 for (i = 0; i < lto_wrapper_num_args; i++)
736 *lto_arg_ptr++ = lto_wrapper_argv[i];
737
738 if (!linker_output_known)
739 {
740 assert (linker_output_set);
741 switch (linker_output)
742 {
743 case LDPO_REL:
744 if (non_claimed_files)
745 {
746 message (LDPL_WARNING, "incremental linking of LTO and non-LTO "
747 "objects; using -flinker-output=nolto-rel which will "
748 "bypass whole program optimization");
749 linker_output_str = "-flinker-output=nolto-rel";
750 }
751 else
752 linker_output_str = "-flinker-output=rel";
753 break;
754 case LDPO_DYN:
755 linker_output_str = "-flinker-output=dyn";
756 break;
757 case LDPO_PIE:
758 linker_output_str = "-flinker-output=pie";
759 break;
760 case LDPO_EXEC:
761 linker_output_str = "-flinker-output=exec";
762 break;
763 default:
764 message (LDPL_FATAL, "unsupported linker output %i", linker_output);
765 break;
766 }
767 *lto_arg_ptr++ = xstrdup (linker_output_str);
768 }
769
770 if (num_offload_files > 0)
771 {
772 FILE *f;
773 char *arg;
774 char *offload_objects_file_name;
775 struct plugin_offload_file *ofld;
776
777 offload_objects_file_name = make_temp_file (".ofldlist");
778 check (offload_objects_file_name, LDPL_FATAL,
779 "Failed to generate a temporary file name");
780 f = fopen (offload_objects_file_name, "w");
781 check (f, LDPL_FATAL, "could not open file with offload objects");
782 fprintf (f, "%u\n", num_offload_files);
783
784 /* Skip the dummy item at the start of the list. */
785 ofld = offload_files->next;
786 while (ofld)
787 {
788 fprintf (f, "%s\n", ofld->name);
789 ofld = ofld->next;
790 }
791 fclose (f);
792
793 arg = concat ("-foffload-objects=", offload_objects_file_name, NULL);
794 check (arg, LDPL_FATAL, "could not allocate");
795 *lto_arg_ptr++ = arg;
796 }
797
798 for (i = 0; i < num_claimed_files; i++)
799 {
800 struct plugin_file_info *info = &claimed_files[i];
801
802 *lto_arg_ptr++ = info->name;
803 }
804
805 *lto_arg_ptr++ = NULL;
806 exec_lto_wrapper (lto_argv);
807
808 free (lto_argv);
809
810 /* --pass-through is not needed when using gold 1.11 or later. */
811 if (pass_through_items && gold_version < 111)
812 {
813 unsigned int i;
814 for (i = 0; i < num_pass_through_items; i++)
815 {
816 if (strncmp (pass_through_items[i], "-l", 2) == 0)
817 add_input_library (pass_through_items[i] + 2);
818 else
819 add_input_file (pass_through_items[i]);
820 free (pass_through_items[i]);
821 pass_through_items[i] = NULL;
822 }
823 free (pass_through_items);
824 pass_through_items = NULL;
825 }
826
827 return LDPS_OK;
828 }
829
830 /* Helper, as used in collect2. */
831 static int
832 file_exists (const char *name)
833 {
834 return access (name, R_OK) == 0;
835 }
836
837 /* Unlink FILE unless we have save-temps set.
838 Note that we're saving files if verbose output is set. */
839
840 static void
841 maybe_unlink (const char *file)
842 {
843 if (save_temps && file_exists (file))
844 {
845 if (verbose)
846 fprintf (stderr, "[Leaving %s]\n", file);
847 return;
848 }
849
850 unlink_if_ordinary (file);
851 }
852
853 /* Remove temporary files at the end of the link. */
854
855 static enum ld_plugin_status
856 cleanup_handler (void)
857 {
858 unsigned int i;
859
860 if (debug)
861 return LDPS_OK;
862
863 if (arguments_file_name)
864 maybe_unlink (arguments_file_name);
865
866 for (i = 0; i < num_output_files; i++)
867 maybe_unlink (output_files[i]);
868
869 free_2 ();
870 return LDPS_OK;
871 }
872
873 #define SWAP(type, a, b) \
874 do { type tmp_; tmp_ = (a); (a) = (b); (b) = tmp_; } while(0)
875
876 /* Compare two hash table entries */
877
878 static int eq_sym (const void *a, const void *b)
879 {
880 const struct ld_plugin_symbol *as = (const struct ld_plugin_symbol *)a;
881 const struct ld_plugin_symbol *bs = (const struct ld_plugin_symbol *)b;
882
883 return !strcmp (as->name, bs->name);
884 }
885
886 /* Hash a symbol */
887
888 static hashval_t hash_sym (const void *a)
889 {
890 const struct ld_plugin_symbol *as = (const struct ld_plugin_symbol *)a;
891
892 return htab_hash_string (as->name);
893 }
894
895 /* Determine how strong a symbol is */
896
897 static int symbol_strength (struct ld_plugin_symbol *s)
898 {
899 switch (s->def)
900 {
901 case LDPK_UNDEF:
902 case LDPK_WEAKUNDEF:
903 return 0;
904 case LDPK_WEAKDEF:
905 return 1;
906 default:
907 return 2;
908 }
909 }
910
911 /* In the ld -r case we can get dups in the LTO symbol tables, where
912 the same symbol can have different resolutions (e.g. undefined and defined).
913
914 We have to keep that in the LTO symbol tables, but the dups confuse
915 gold and then finally gcc by supplying incorrect resolutions.
916
917 Problem is that the main gold symbol table doesn't know about subids
918 and does not distingush the same symbols in different states.
919
920 So we drop duplicates from the linker visible symbol table
921 and keep them in a private table. Then later do own symbol
922 resolution for the duplicated based on the results for the
923 originals.
924
925 Then when writing out the resolution file readd the dropped symbols.
926
927 XXX how to handle common? */
928
929 static void
930 resolve_conflicts (struct plugin_symtab *t, struct plugin_symtab *conflicts)
931 {
932 htab_t symtab = htab_create (t->nsyms, hash_sym, eq_sym, NULL);
933 int i;
934 int out;
935 int outlen;
936
937 outlen = t->nsyms;
938 conflicts->syms = xmalloc (sizeof (struct ld_plugin_symbol) * outlen);
939 conflicts->aux = xmalloc (sizeof (struct sym_aux) * outlen);
940
941 /* Move all duplicate symbols into the auxiliary conflicts table. */
942 out = 0;
943 for (i = 0; i < t->nsyms; i++)
944 {
945 struct ld_plugin_symbol *s = &t->syms[i];
946 struct sym_aux *aux = &t->aux[i];
947 void **slot;
948
949 slot = htab_find_slot (symtab, s, INSERT);
950 if (*slot != NULL)
951 {
952 int cnf;
953 struct ld_plugin_symbol *orig = (struct ld_plugin_symbol *)*slot;
954 struct sym_aux *orig_aux = &t->aux[orig - t->syms];
955
956 /* Always let the linker resolve the strongest symbol */
957 if (symbol_strength (orig) < symbol_strength (s))
958 {
959 SWAP (struct ld_plugin_symbol, *orig, *s);
960 SWAP (uint32_t, orig_aux->slot, aux->slot);
961 SWAP (unsigned long long, orig_aux->id, aux->id);
962 /* Don't swap conflict chain pointer */
963 }
964
965 /* Move current symbol into the conflicts table */
966 cnf = conflicts->nsyms++;
967 conflicts->syms[cnf] = *s;
968 conflicts->aux[cnf] = *aux;
969 aux = &conflicts->aux[cnf];
970
971 /* Update conflicts chain of the original symbol */
972 aux->next_conflict = orig_aux->next_conflict;
973 orig_aux->next_conflict = cnf;
974
975 continue;
976 }
977
978 /* Remove previous duplicates in the main table */
979 if (out < i)
980 {
981 t->syms[out] = *s;
982 t->aux[out] = *aux;
983 }
984
985 /* Put original into the hash table */
986 *slot = &t->syms[out];
987 out++;
988 }
989
990 assert (conflicts->nsyms <= outlen);
991 assert (conflicts->nsyms + out == t->nsyms);
992
993 t->nsyms = out;
994 htab_delete (symtab);
995 }
996
997 /* Process one section of an object file. */
998
999 static int
1000 process_symtab (void *data, const char *name, off_t offset, off_t length)
1001 {
1002 struct plugin_objfile *obj = (struct plugin_objfile *)data;
1003 char *s;
1004 char *secdatastart, *secdata;
1005
1006 if (strncmp (name, LTO_SYMTAB_PREFIX, LTO_SYMTAB_PREFIX_LEN) != 0)
1007 return 1;
1008
1009 s = strrchr (name, '.');
1010 if (s)
1011 sscanf (s, ".%" PRI_LL "x", &obj->out->id);
1012 secdata = secdatastart = xmalloc (length);
1013 offset += obj->file->offset;
1014 if (offset != lseek (obj->file->fd, offset, SEEK_SET))
1015 goto err;
1016
1017 do
1018 {
1019 ssize_t got = read (obj->file->fd, secdata, length);
1020 if (got == 0)
1021 break;
1022 else if (got > 0)
1023 {
1024 secdata += got;
1025 length -= got;
1026 }
1027 else if (errno != EINTR)
1028 goto err;
1029 }
1030 while (length > 0);
1031 if (length > 0)
1032 goto err;
1033
1034 translate (secdatastart, secdata, obj->out);
1035 obj->found++;
1036 free (secdatastart);
1037 return 1;
1038
1039 err:
1040 if (message)
1041 message (LDPL_FATAL, "%s: corrupt object file", obj->file->name);
1042 /* Force claim_file_handler to abandon this file. */
1043 obj->found = 0;
1044 free (secdatastart);
1045 return 0;
1046 }
1047
1048 /* Process one section of an object file. */
1049
1050 static int
1051 process_symtab_extension (void *data, const char *name, off_t offset,
1052 off_t length)
1053 {
1054 struct plugin_objfile *obj = (struct plugin_objfile *)data;
1055 char *s;
1056 char *secdatastart, *secdata;
1057
1058 if (strncmp (name, LTO_SYMTAB_EXT_PREFIX, LTO_SYMTAB_EXT_PREFIX_LEN) != 0)
1059 return 1;
1060
1061 s = strrchr (name, '.');
1062 if (s)
1063 sscanf (s, ".%" PRI_LL "x", &obj->out->id);
1064 secdata = secdatastart = xmalloc (length);
1065 offset += obj->file->offset;
1066 if (offset != lseek (obj->file->fd, offset, SEEK_SET))
1067 goto err;
1068
1069 do
1070 {
1071 ssize_t got = read (obj->file->fd, secdata, length);
1072 if (got == 0)
1073 break;
1074 else if (got > 0)
1075 {
1076 secdata += got;
1077 length -= got;
1078 }
1079 else if (errno != EINTR)
1080 goto err;
1081 }
1082 while (length > 0);
1083 if (length > 0)
1084 goto err;
1085
1086 parse_symtab_extension (secdatastart, secdata, obj->out);
1087 obj->found++;
1088 free (secdatastart);
1089 return 1;
1090
1091 err:
1092 if (message)
1093 message (LDPL_FATAL, "%s: corrupt object file", obj->file->name);
1094 /* Force claim_file_handler to abandon this file. */
1095 obj->found = 0;
1096 free (secdatastart);
1097 return 0;
1098 }
1099
1100
1101 /* Find an offload section of an object file. */
1102
1103 static int
1104 process_offload_section (void *data, const char *name, off_t offset, off_t len)
1105 {
1106 if (!strncmp (name, OFFLOAD_SECTION, OFFLOAD_SECTION_LEN))
1107 {
1108 struct plugin_objfile *obj = (struct plugin_objfile *) data;
1109 obj->offload = 1;
1110 return 0;
1111 }
1112
1113 return 1;
1114 }
1115
1116 /* Callback used by gold to check if the plugin will claim FILE. Writes
1117 the result in CLAIMED. */
1118
1119 static enum ld_plugin_status
1120 claim_file_handler (const struct ld_plugin_input_file *file, int *claimed)
1121 {
1122 enum ld_plugin_status status;
1123 struct plugin_objfile obj;
1124 struct plugin_file_info lto_file;
1125 int err;
1126 const char *errmsg;
1127
1128 memset (&lto_file, 0, sizeof (struct plugin_file_info));
1129
1130 if (file->offset != 0)
1131 {
1132 /* We pass the offset of the actual file, not the archive header.
1133 Can't use PRIx64, because that's C99, so we have to print the
1134 64-bit hex int as two 32-bit ones. Use xasprintf instead of
1135 asprintf because asprintf doesn't work as expected on some older
1136 mingw32 hosts. */
1137 int lo, hi;
1138 lo = file->offset & 0xffffffff;
1139 hi = ((int64_t)file->offset >> 32) & 0xffffffff;
1140 lto_file.name = hi ? xasprintf ("%s@0x%x%08x", file->name, hi, lo)
1141 : xasprintf ("%s@0x%x", file->name, lo);
1142 }
1143 else
1144 {
1145 lto_file.name = xstrdup (file->name);
1146 }
1147 lto_file.handle = file->handle;
1148
1149 *claimed = 0;
1150 obj.file = file;
1151 obj.found = 0;
1152 obj.offload = 0;
1153 obj.out = &lto_file.symtab;
1154 errmsg = NULL;
1155 obj.objfile = simple_object_start_read (file->fd, file->offset, LTO_SEGMENT_NAME,
1156 &errmsg, &err);
1157 /* No file, but also no error code means unrecognized format; just skip it. */
1158 if (!obj.objfile && !err)
1159 goto err;
1160
1161 if (obj.objfile)
1162 {
1163 errmsg = simple_object_find_sections (obj.objfile, process_symtab, &obj,
1164 &err);
1165 /* Parsing symtab extension should be done only for add_symbols_v2 and
1166 later versions. */
1167 if (!errmsg && add_symbols_v2 != NULL)
1168 errmsg = simple_object_find_sections (obj.objfile,
1169 process_symtab_extension,
1170 &obj, &err);
1171 }
1172
1173 if (!obj.objfile || errmsg)
1174 {
1175 if (err && message)
1176 message (LDPL_FATAL, "%s: %s: %s", file->name, errmsg,
1177 xstrerror (err));
1178 else if (message)
1179 message (LDPL_FATAL, "%s: %s", file->name, errmsg);
1180 goto err;
1181 }
1182
1183 if (obj.objfile)
1184 simple_object_find_sections (obj.objfile, process_offload_section,
1185 &obj, &err);
1186
1187 if (obj.found == 0 && obj.offload == 0)
1188 goto err;
1189
1190 if (obj.found > 1)
1191 resolve_conflicts (&lto_file.symtab, &lto_file.conflicts);
1192
1193 if (obj.found > 0)
1194 {
1195 if (add_symbols_v2)
1196 status = add_symbols_v2 (file->handle, lto_file.symtab.nsyms,
1197 lto_file.symtab.syms);
1198 else
1199 status = add_symbols (file->handle, lto_file.symtab.nsyms,
1200 lto_file.symtab.syms);
1201 check (status == LDPS_OK, LDPL_FATAL, "could not add symbols");
1202
1203 num_claimed_files++;
1204 claimed_files =
1205 xrealloc (claimed_files,
1206 num_claimed_files * sizeof (struct plugin_file_info));
1207 claimed_files[num_claimed_files - 1] = lto_file;
1208
1209 *claimed = 1;
1210 }
1211
1212 if (offload_files == NULL)
1213 {
1214 /* Add dummy item to the start of the list. */
1215 offload_files = xmalloc (sizeof (struct plugin_offload_file));
1216 offload_files->name = NULL;
1217 offload_files->next = NULL;
1218 offload_files_last = offload_files;
1219 }
1220
1221 /* If this is an LTO file without offload, and it is the first LTO file, save
1222 the pointer to the last offload file in the list. Further offload LTO
1223 files will be inserted after it, if any. */
1224 if (*claimed && obj.offload == 0 && offload_files_last_lto == NULL)
1225 offload_files_last_lto = offload_files_last;
1226
1227 if (obj.offload == 1)
1228 {
1229 /* Add file to the list. The order must be exactly the same as the final
1230 order after recompilation and linking, otherwise host and target tables
1231 with addresses wouldn't match. If a static library contains both LTO
1232 and non-LTO objects, ld and gold link them in a different order. */
1233 struct plugin_offload_file *ofld
1234 = xmalloc (sizeof (struct plugin_offload_file));
1235 ofld->name = lto_file.name;
1236 ofld->next = NULL;
1237
1238 if (*claimed && offload_files_last_lto == NULL && file->offset != 0
1239 && gold_version == -1)
1240 {
1241 /* ld only: insert first LTO file from the archive after the last real
1242 object file immediately preceding the archive, or at the begin of
1243 the list if there was no real objects before archives. */
1244 if (offload_files_last_obj != NULL)
1245 {
1246 ofld->next = offload_files_last_obj->next;
1247 offload_files_last_obj->next = ofld;
1248 }
1249 else
1250 {
1251 ofld->next = offload_files->next;
1252 offload_files->next = ofld;
1253 }
1254 }
1255 else if (*claimed && offload_files_last_lto != NULL)
1256 {
1257 /* Insert LTO file after the last LTO file in the list. */
1258 ofld->next = offload_files_last_lto->next;
1259 offload_files_last_lto->next = ofld;
1260 }
1261 else
1262 /* Add non-LTO file or first non-archive LTO file to the end of the
1263 list. */
1264 offload_files_last->next = ofld;
1265
1266 if (ofld->next == NULL)
1267 offload_files_last = ofld;
1268 if (file->offset == 0)
1269 offload_files_last_obj = ofld;
1270 if (*claimed)
1271 offload_files_last_lto = ofld;
1272 num_offload_files++;
1273 }
1274
1275 goto cleanup;
1276
1277 err:
1278 non_claimed_files++;
1279 free (lto_file.name);
1280
1281 cleanup:
1282 if (obj.objfile)
1283 simple_object_release_read (obj.objfile);
1284
1285 return LDPS_OK;
1286 }
1287
1288 /* Parse the plugin options. */
1289
1290 static void
1291 process_option (const char *option)
1292 {
1293 if (strcmp (option, "-linker-output-known") == 0)
1294 linker_output_known = 1;
1295 if (strcmp (option, "-debug") == 0)
1296 debug = true;
1297 else if ((strcmp (option, "-v") == 0)
1298 || (strcmp (option, "--verbose") == 0))
1299 verbose = true;
1300 else if (strcmp (option, "-save-temps") == 0)
1301 save_temps = true;
1302 else if (strcmp (option, "-nop") == 0)
1303 nop = 1;
1304 else if (!strncmp (option, "-pass-through=", strlen("-pass-through=")))
1305 {
1306 num_pass_through_items++;
1307 pass_through_items = xrealloc (pass_through_items,
1308 num_pass_through_items * sizeof (char *));
1309 pass_through_items[num_pass_through_items - 1] =
1310 xstrdup (option + strlen ("-pass-through="));
1311 }
1312 else if (!strncmp (option, "-sym-style=", sizeof ("-sym-style=") - 1))
1313 {
1314 switch (option[sizeof ("-sym-style=") - 1])
1315 {
1316 case 'w':
1317 sym_style = ss_win32;
1318 break;
1319 case 'u':
1320 sym_style = ss_uscore;
1321 break;
1322 default:
1323 sym_style = ss_none;
1324 break;
1325 }
1326 }
1327 else
1328 {
1329 int size;
1330 char *opt = xstrdup (option);
1331 lto_wrapper_num_args += 1;
1332 size = lto_wrapper_num_args * sizeof (char *);
1333 lto_wrapper_argv = (char **) xrealloc (lto_wrapper_argv, size);
1334 lto_wrapper_argv[lto_wrapper_num_args - 1] = opt;
1335 if (strncmp (option, "-fresolution=", sizeof ("-fresolution=") - 1) == 0)
1336 resolution_file = opt + sizeof ("-fresolution=") - 1;
1337 }
1338 save_temps = save_temps || debug;
1339 verbose = verbose || debug;
1340 }
1341
1342 /* Called by gold after loading the plugin. TV is the transfer vector. */
1343
1344 enum ld_plugin_status
1345 onload (struct ld_plugin_tv *tv)
1346 {
1347 struct ld_plugin_tv *p;
1348 enum ld_plugin_status status;
1349
1350 p = tv;
1351 while (p->tv_tag)
1352 {
1353 switch (p->tv_tag)
1354 {
1355 case LDPT_MESSAGE:
1356 message = p->tv_u.tv_message;
1357 break;
1358 case LDPT_REGISTER_CLAIM_FILE_HOOK:
1359 register_claim_file = p->tv_u.tv_register_claim_file;
1360 break;
1361 case LDPT_ADD_SYMBOLS_V2:
1362 add_symbols_v2 = p->tv_u.tv_add_symbols;
1363 break;
1364 case LDPT_ADD_SYMBOLS:
1365 add_symbols = p->tv_u.tv_add_symbols;
1366 break;
1367 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
1368 register_all_symbols_read = p->tv_u.tv_register_all_symbols_read;
1369 break;
1370 case LDPT_GET_SYMBOLS_V2:
1371 get_symbols_v2 = p->tv_u.tv_get_symbols;
1372 break;
1373 case LDPT_GET_SYMBOLS:
1374 get_symbols = p->tv_u.tv_get_symbols;
1375 break;
1376 case LDPT_REGISTER_CLEANUP_HOOK:
1377 register_cleanup = p->tv_u.tv_register_cleanup;
1378 break;
1379 case LDPT_ADD_INPUT_FILE:
1380 add_input_file = p->tv_u.tv_add_input_file;
1381 break;
1382 case LDPT_ADD_INPUT_LIBRARY:
1383 add_input_library = p->tv_u.tv_add_input_library;
1384 break;
1385 case LDPT_OPTION:
1386 process_option (p->tv_u.tv_string);
1387 break;
1388 case LDPT_GOLD_VERSION:
1389 gold_version = p->tv_u.tv_val;
1390 break;
1391 case LDPT_LINKER_OUTPUT:
1392 linker_output = (enum ld_plugin_output_file_type) p->tv_u.tv_val;
1393 linker_output_set = 1;
1394 break;
1395 case LDPT_OUTPUT_NAME:
1396 /* We only use this to make user-friendly temp file names. */
1397 link_output_name = p->tv_u.tv_string;
1398 break;
1399 default:
1400 break;
1401 }
1402 p++;
1403 }
1404
1405 check (register_claim_file, LDPL_FATAL, "register_claim_file not found");
1406 check (add_symbols, LDPL_FATAL, "add_symbols not found");
1407 status = register_claim_file (claim_file_handler);
1408 check (status == LDPS_OK, LDPL_FATAL,
1409 "could not register the claim_file callback");
1410
1411 if (register_cleanup)
1412 {
1413 status = register_cleanup (cleanup_handler);
1414 check (status == LDPS_OK, LDPL_FATAL,
1415 "could not register the cleanup callback");
1416 }
1417
1418 if (register_all_symbols_read)
1419 {
1420 check (get_symbols, LDPL_FATAL, "get_symbols not found");
1421 status = register_all_symbols_read (all_symbols_read_handler);
1422 check (status == LDPS_OK, LDPL_FATAL,
1423 "could not register the all_symbols_read callback");
1424 }
1425
1426 char *collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
1427 if (collect_gcc_options)
1428 {
1429 /* Support -fno-use-linker-plugin by failing to load the plugin
1430 for the case where it is auto-loaded by BFD. */
1431 if (strstr (collect_gcc_options, "'-fno-use-linker-plugin'"))
1432 return LDPS_ERR;
1433
1434 if ( strstr (collect_gcc_options, "'-save-temps'"))
1435 save_temps = true;
1436
1437 if (strstr (collect_gcc_options, "'-v'")
1438 || strstr (collect_gcc_options, "'--verbose'"))
1439 verbose = true;
1440 }
1441
1442 return LDPS_OK;
1443 }