[RS6000] rs6000_linux64_override_options fix
[official-gcc.git] / lto-plugin / lto-plugin.c
blob6f67552d0758b9dfa4718bd0f15964d3ab281e0f
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).
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.
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.
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/>. */
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.
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.
27 More information at http://gcc.gnu.org/wiki/whopr/driver.
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 -linker-output-auto-notlo-rel: Switch from rel to nolto-rel mode without
36 warning. This is used on systems like VxWorks (kernel) where the link is
37 always partial and repeated incremental linking is generally not used.
38 -sym-style={none,win32,underscore|uscore}
39 -pass-through */
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44 #if HAVE_STDINT_H
45 #include <stdint.h>
46 #endif
47 #include <stdbool.h>
48 #include <assert.h>
49 #include <errno.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <inttypes.h>
54 #include <sys/stat.h>
55 #include <unistd.h>
56 #include <fcntl.h>
57 #include <sys/types.h>
58 #ifdef HAVE_SYS_WAIT_H
59 #include <sys/wait.h>
60 #endif
61 #ifndef WIFEXITED
62 #define WIFEXITED(S) (((S) & 0xff) == 0)
63 #endif
64 #ifndef WEXITSTATUS
65 #define WEXITSTATUS(S) (((S) & 0xff00) >> 8)
66 #endif
67 #include <libiberty.h>
68 #include <hashtab.h>
69 #include "../gcc/lto/common.h"
70 #include "simple-object.h"
71 #include "plugin-api.h"
73 /* We need to use I64 instead of ll width-specifier on native Windows.
74 The reason for this is that older MS-runtimes don't support the ll. */
75 #ifdef __MINGW32__
76 #define PRI_LL "I64"
77 #else
78 #define PRI_LL "ll"
79 #endif
81 /* Handle opening elf files on hosts, such as Windows, that may use
82 text file handling that will break binary access. */
83 #ifndef O_BINARY
84 # define O_BINARY 0
85 #endif
87 /* Segment name for LTO sections. This is only used for Mach-O.
88 FIXME: This needs to be kept in sync with darwin.c. */
90 #define LTO_SEGMENT_NAME "__GNU_LTO"
92 /* LTO magic section name. */
94 #define LTO_SYMTAB_PREFIX ".gnu.lto_.symtab"
95 #define LTO_SYMTAB_PREFIX_LEN (sizeof (LTO_SYMTAB_PREFIX) - 1)
96 #define LTO_SYMTAB_EXT_PREFIX ".gnu.lto_.ext_symtab"
97 #define LTO_SYMTAB_EXT_PREFIX_LEN (sizeof (LTO_SYMTAB_EXT_PREFIX) - 1)
98 #define LTO_LTO_PREFIX ".gnu.lto_.lto"
99 #define LTO_LTO_PREFIX_LEN (sizeof (LTO_LTO_PREFIX) - 1)
100 #define OFFLOAD_SECTION ".gnu.offload_lto_.opts"
101 #define OFFLOAD_SECTION_LEN (sizeof (OFFLOAD_SECTION) - 1)
103 /* The part of the symbol table the plugin has to keep track of. Note that we
104 must keep SYMS until all_symbols_read is called to give the linker time to
105 copy the symbol information.
106 The id must be 64bit to minimze collisions. */
108 struct sym_aux
110 uint32_t slot;
111 unsigned long long id;
112 unsigned next_conflict;
115 struct plugin_symtab
117 int nsyms;
118 int last_sym;
119 struct sym_aux *aux;
120 struct ld_plugin_symbol *syms;
121 unsigned long long id;
124 /* Encapsulates object file data during symbol scan. */
125 struct plugin_objfile
127 int found;
128 int offload;
129 simple_object_read *objfile;
130 struct plugin_symtab *out;
131 const struct ld_plugin_input_file *file;
134 /* All that we have to remember about a file. */
136 struct plugin_file_info
138 char *name;
139 void *handle;
140 struct plugin_symtab symtab;
141 struct plugin_symtab conflicts;
144 /* List item with name of the file with offloading. */
146 struct plugin_offload_file
148 char *name;
149 struct plugin_offload_file *next;
152 /* Until ASM_OUTPUT_LABELREF can be hookized and decoupled from
153 stdio file streams, we do simple label translation here. */
155 enum symbol_style
157 ss_none, /* No underscore prefix. */
158 ss_win32, /* Underscore prefix any symbol not beginning with '@'. */
159 ss_uscore, /* Underscore prefix all symbols. */
162 static char *arguments_file_name;
163 static ld_plugin_register_claim_file register_claim_file;
164 static ld_plugin_register_all_symbols_read register_all_symbols_read;
165 static ld_plugin_get_symbols get_symbols, get_symbols_v2;
166 static ld_plugin_register_cleanup register_cleanup;
167 static ld_plugin_add_input_file add_input_file;
168 static ld_plugin_add_input_library add_input_library;
169 static ld_plugin_message message;
170 static ld_plugin_add_symbols add_symbols, add_symbols_v2;
172 static struct plugin_file_info *claimed_files = NULL;
173 static unsigned int num_claimed_files = 0;
174 static unsigned int non_claimed_files = 0;
176 /* List of files with offloading. */
177 static struct plugin_offload_file *offload_files;
178 /* Last file in the list. */
179 static struct plugin_offload_file *offload_files_last;
180 /* Last non-archive file in the list. */
181 static struct plugin_offload_file *offload_files_last_obj;
182 /* Last LTO file in the list. */
183 static struct plugin_offload_file *offload_files_last_lto;
184 /* Total number of files with offloading. */
185 static unsigned num_offload_files;
187 static char **output_files = NULL;
188 static unsigned int num_output_files = 0;
190 static char **lto_wrapper_argv;
191 static int lto_wrapper_num_args;
193 static char **pass_through_items = NULL;
194 static unsigned int num_pass_through_items;
196 static bool debug;
197 static bool save_temps;
198 static bool verbose;
199 static char nop;
200 static char *resolution_file = NULL;
201 static enum ld_plugin_output_file_type linker_output;
202 static bool linker_output_set;
203 static bool linker_output_known;
204 static bool linker_output_auto_nolto_rel;
205 static const char *link_output_name = NULL;
207 /* This indicates link_output_name already contains the dot of the
208 suffix, so we can skip it in extensions. */
209 static int skip_in_suffix = 0;
211 /* The version of gold being used, or -1 if not gold. The number is
212 MAJOR * 100 + MINOR. */
213 static int gold_version = -1;
215 /* Not used by default, but can be overridden at runtime
216 by using -plugin-opt=-sym-style={none,win32,underscore|uscore}
217 (in fact, only first letter of style arg is checked.) */
218 static enum symbol_style sym_style = ss_none;
220 static void
221 check_1 (int gate, enum ld_plugin_level level, const char *text)
223 if (gate)
224 return;
226 if (message)
227 message (level, text);
228 else
230 /* If there is no nicer way to inform the user, fallback to stderr. */
231 fprintf (stderr, "%s\n", text);
232 if (level == LDPL_FATAL)
233 abort ();
237 /* This little wrapper allows check to be called with a non-integer
238 first argument, such as a pointer that must be non-NULL. We can't
239 use c99 bool type to coerce it into range, so we explicitly test. */
240 #define check(GATE, LEVEL, TEXT) check_1 (((GATE) != 0), (LEVEL), (TEXT))
242 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
243 by P and the result is written in ENTRY. The slot number is stored in SLOT.
244 Returns the address of the next entry. */
246 static char *
247 parse_table_entry (char *p, struct ld_plugin_symbol *entry,
248 struct sym_aux *aux)
250 unsigned char t;
251 enum ld_plugin_symbol_kind translate_kind[] =
253 LDPK_DEF,
254 LDPK_WEAKDEF,
255 LDPK_UNDEF,
256 LDPK_WEAKUNDEF,
257 LDPK_COMMON
260 enum ld_plugin_symbol_visibility translate_visibility[] =
262 LDPV_DEFAULT,
263 LDPV_PROTECTED,
264 LDPV_INTERNAL,
265 LDPV_HIDDEN
268 switch (sym_style)
270 case ss_win32:
271 if (p[0] == '@')
273 /* cf. Duff's device. */
274 case ss_none:
275 entry->name = xstrdup (p);
276 break;
278 /* FALL-THROUGH. */
279 case ss_uscore:
280 entry->name = concat ("_", p, NULL);
281 break;
282 default:
283 check (0, LDPL_FATAL, "invalid symbol style requested");
284 break;
286 while (*p)
287 p++;
288 p++;
290 entry->version = NULL;
292 entry->comdat_key = p;
293 while (*p)
294 p++;
295 p++;
297 if (strlen (entry->comdat_key) == 0)
298 entry->comdat_key = NULL;
299 else
300 entry->comdat_key = xstrdup (entry->comdat_key);
302 entry->unused = entry->section_kind = entry->symbol_type = 0;
304 t = *p;
305 check (t <= 4, LDPL_FATAL, "invalid symbol kind found");
306 entry->def = translate_kind[t];
307 p++;
309 t = *p;
310 check (t <= 3, LDPL_FATAL, "invalid symbol visibility found");
311 entry->visibility = translate_visibility[t];
312 p++;
314 memcpy (&entry->size, p, sizeof (uint64_t));
315 p += 8;
317 memcpy (&aux->slot, p, sizeof (uint32_t));
318 p += 4;
320 entry->resolution = LDPR_UNKNOWN;
322 aux->next_conflict = -1;
324 return p;
327 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
328 by P and the result is written in ENTRY. The slot number is stored in SLOT.
329 Returns the address of the next entry. */
331 static char *
332 parse_table_entry_extension (char *p, struct ld_plugin_symbol *entry)
334 unsigned char t;
335 enum ld_plugin_symbol_type symbol_types[] =
337 LDST_UNKNOWN,
338 LDST_FUNCTION,
339 LDST_VARIABLE,
342 t = *p;
343 check (t <= 2, LDPL_FATAL, "invalid symbol type found");
344 entry->symbol_type = symbol_types[t];
345 p++;
346 entry->section_kind = *p;
347 p++;
349 return p;
353 /* Translate the IL symbol table located between DATA and END. Append the
354 slots and symbols to OUT. */
356 static void
357 translate (char *data, char *end, struct plugin_symtab *out)
359 struct sym_aux *aux;
360 struct ld_plugin_symbol *syms = NULL;
361 int n, len;
363 /* This overestimates the output buffer sizes, but at least
364 the algorithm is O(1) now. */
366 len = (end - data)/8 + out->nsyms + 1;
367 syms = xrealloc (out->syms, len * sizeof (struct ld_plugin_symbol));
368 aux = xrealloc (out->aux, len * sizeof (struct sym_aux));
370 for (n = out->nsyms; data < end; n++)
372 aux[n].id = out->id;
373 data = parse_table_entry (data, &syms[n], &aux[n]);
376 assert(n < len);
378 out->nsyms = n;
379 out->syms = syms;
380 out->aux = aux;
383 static void
384 parse_symtab_extension (char *data, char *end, struct plugin_symtab *out)
386 unsigned long i;
387 unsigned char version;
389 if (data >= end)
390 /* FIXME: Issue an error ? */
391 return;
393 version = *data;
394 data++;
396 if (version != 1)
397 return;
399 /* Version 1 contains the following data per entry:
400 - symbol_type
401 - section_kind
402 . */
404 unsigned long nsyms = (end - data) / 2;
406 for (i = 0; i < nsyms; i++)
407 data = parse_table_entry_extension (data, out->syms + i + out->last_sym);
409 out->last_sym += nsyms;
412 /* Free all memory that is no longer needed after writing the symbol
413 resolution. */
415 static void
416 free_1 (struct plugin_file_info *files, unsigned num_files)
418 unsigned int i;
419 for (i = 0; i < num_files; i++)
421 struct plugin_file_info *info = &files[i];
422 struct plugin_symtab *symtab = &info->symtab;
423 unsigned int j;
424 for (j = 0; j < symtab->nsyms; j++)
426 struct ld_plugin_symbol *s = &symtab->syms[j];
427 free (s->name);
428 free (s->comdat_key);
430 free (symtab->syms);
431 symtab->syms = NULL;
435 /* Free all remaining memory. */
437 static void
438 free_2 (void)
440 unsigned int i;
441 for (i = 0; i < num_claimed_files; i++)
443 struct plugin_file_info *info = &claimed_files[i];
444 struct plugin_symtab *symtab = &info->symtab;
445 free (symtab->aux);
446 free (info->name);
449 for (i = 0; i < num_output_files; i++)
450 free (output_files[i]);
451 free (output_files);
453 free (claimed_files);
454 claimed_files = NULL;
455 num_claimed_files = 0;
457 while (offload_files)
459 struct plugin_offload_file *ofld = offload_files;
460 offload_files = offload_files->next;
461 free (ofld);
463 num_offload_files = 0;
465 free (arguments_file_name);
466 arguments_file_name = NULL;
469 /* Dump SYMTAB to resolution file F. */
471 static void
472 dump_symtab (FILE *f, struct plugin_symtab *symtab)
474 unsigned j;
476 for (j = 0; j < symtab->nsyms; j++)
478 uint32_t slot = symtab->aux[j].slot;
479 unsigned int resolution = symtab->syms[j].resolution;
481 assert (resolution != LDPR_UNKNOWN);
483 fprintf (f, "%u %" PRI_LL "x %s %s\n",
484 (unsigned int) slot, symtab->aux[j].id,
485 lto_resolution_str[resolution],
486 symtab->syms[j].name);
490 /* Finish the conflicts' resolution information after the linker resolved
491 the original symbols */
493 static void
494 finish_conflict_resolution (struct plugin_symtab *symtab,
495 struct plugin_symtab *conflicts)
497 int i, j;
499 if (conflicts->nsyms == 0)
500 return;
502 for (i = 0; i < symtab->nsyms; i++)
504 char resolution = LDPR_UNKNOWN;
506 if (symtab->aux[i].next_conflict == -1)
507 continue;
509 switch (symtab->syms[i].def)
511 case LDPK_DEF:
512 case LDPK_COMMON: /* ??? */
513 resolution = LDPR_RESOLVED_IR;
514 break;
515 case LDPK_WEAKDEF:
516 resolution = LDPR_PREEMPTED_IR;
517 break;
518 case LDPK_UNDEF:
519 case LDPK_WEAKUNDEF:
520 resolution = symtab->syms[i].resolution;
521 break;
522 default:
523 assert (0);
526 assert (resolution != LDPR_UNKNOWN);
528 for (j = symtab->aux[i].next_conflict;
529 j != -1;
530 j = conflicts->aux[j].next_conflict)
531 conflicts->syms[j].resolution = resolution;
535 /* Free symbol table SYMTAB. */
537 static void
538 free_symtab (struct plugin_symtab *symtab)
540 free (symtab->syms);
541 symtab->syms = NULL;
542 free (symtab->aux);
543 symtab->aux = NULL;
546 /* Writes the relocations to disk. */
548 static void
549 write_resolution (void)
551 unsigned int i;
552 FILE *f;
554 check (resolution_file, LDPL_FATAL, "resolution file not specified");
555 f = fopen (resolution_file, "w");
556 check (f, LDPL_FATAL, "could not open file");
558 fprintf (f, "%d\n", num_claimed_files);
560 for (i = 0; i < num_claimed_files; i++)
562 struct plugin_file_info *info = &claimed_files[i];
563 struct plugin_symtab *symtab = &info->symtab;
564 struct ld_plugin_symbol *syms = symtab->syms;
566 /* Version 2 of API supports IRONLY_EXP resolution that is
567 accepted by GCC-4.7 and newer. */
568 if (get_symbols_v2)
569 get_symbols_v2 (info->handle, symtab->nsyms, syms);
570 else
571 get_symbols (info->handle, symtab->nsyms, syms);
573 finish_conflict_resolution (symtab, &info->conflicts);
575 fprintf (f, "%s %d\n", info->name, symtab->nsyms + info->conflicts.nsyms);
576 dump_symtab (f, symtab);
577 if (info->conflicts.nsyms)
579 dump_symtab (f, &info->conflicts);
580 free_symtab (&info->conflicts);
583 fclose (f);
586 /* Pass files generated by the lto-wrapper to the linker. FD is lto-wrapper's
587 stdout. */
589 static void
590 add_output_files (FILE *f)
592 for (;;)
594 const unsigned piece = 32;
595 char *buf, *s = xmalloc (piece);
596 size_t len;
598 buf = s;
599 cont:
600 if (!fgets (buf, piece, f))
602 free (s);
603 break;
605 len = strlen (s);
606 if (s[len - 1] != '\n')
608 s = xrealloc (s, len + piece);
609 buf = s + len;
610 goto cont;
612 s[len - 1] = '\0';
614 num_output_files++;
615 output_files
616 = xrealloc (output_files, num_output_files * sizeof (char *));
617 output_files[num_output_files - 1] = s;
618 add_input_file (output_files[num_output_files - 1]);
622 /* Execute the lto-wrapper. ARGV[0] is the binary. The rest of ARGV is the
623 argument list. */
625 static void
626 exec_lto_wrapper (char *argv[])
628 int t, i;
629 int status;
630 char *at_args;
631 FILE *args;
632 FILE *wrapper_output;
633 char *new_argv[3];
634 struct pex_obj *pex;
635 const char *errmsg;
637 /* Write argv to a file to avoid a command line that is too long
638 Save the file locally on save-temps. */
639 if (save_temps && link_output_name)
640 arguments_file_name = concat (link_output_name,
641 ".lto_wrapper_args"
642 + skip_in_suffix, NULL);
643 else
644 arguments_file_name = make_temp_file (".lto_wrapper_args");
645 check (arguments_file_name, LDPL_FATAL,
646 "Failed to generate a temorary file name");
648 args = fopen (arguments_file_name, "w");
649 check (args, LDPL_FATAL, "could not open arguments file");
651 t = writeargv (&argv[1], args);
652 check (t == 0, LDPL_FATAL, "could not write arguments");
653 t = fclose (args);
654 check (t == 0, LDPL_FATAL, "could not close arguments file");
656 at_args = concat ("@", arguments_file_name, NULL);
657 check (at_args, LDPL_FATAL, "could not allocate");
659 for (i = 1; argv[i]; i++)
661 char *a = argv[i];
662 /* Check the input argument list for a verbose marker too. */
663 if (a[0] == '-' && a[1] == 'v' && a[2] == '\0')
665 verbose = true;
666 break;
670 if (verbose)
672 for (i = 0; argv[i]; i++)
673 fprintf (stderr, "%s ", argv[i]);
674 fprintf (stderr, "\n");
677 new_argv[0] = argv[0];
678 new_argv[1] = at_args;
679 new_argv[2] = NULL;
681 if (debug)
683 for (i = 0; new_argv[i]; i++)
684 fprintf (stderr, "%s ", new_argv[i]);
685 fprintf (stderr, "\n");
688 pex = pex_init (PEX_USE_PIPES, "lto-wrapper", NULL);
689 check (pex != NULL, LDPL_FATAL, "could not pex_init lto-wrapper");
691 errmsg = pex_run (pex, 0, new_argv[0], new_argv, NULL, NULL, &t);
692 check (errmsg == NULL, LDPL_FATAL, "could not run lto-wrapper");
693 check (t == 0, LDPL_FATAL, "could not run lto-wrapper");
695 wrapper_output = pex_read_output (pex, 0);
696 check (wrapper_output, LDPL_FATAL, "could not read lto-wrapper output");
698 add_output_files (wrapper_output);
700 t = pex_get_status (pex, 1, &status);
701 check (t == 1, LDPL_FATAL, "could not get lto-wrapper exit status");
702 check (WIFEXITED (status) && WEXITSTATUS (status) == 0, LDPL_FATAL,
703 "lto-wrapper failed");
705 pex_free (pex);
707 free (at_args);
710 /* Pass the original files back to the linker. */
712 static void
713 use_original_files (void)
715 unsigned i;
716 for (i = 0; i < num_claimed_files; i++)
718 struct plugin_file_info *info = &claimed_files[i];
719 add_input_file (info->name);
724 /* Called by the linker once all symbols have been read. */
726 static enum ld_plugin_status
727 all_symbols_read_handler (void)
729 const unsigned num_lto_args
730 = num_claimed_files + lto_wrapper_num_args + 2
731 + !linker_output_known + !linker_output_auto_nolto_rel;
732 unsigned i;
733 char **lto_argv;
734 const char *linker_output_str = NULL;
735 const char **lto_arg_ptr;
736 if (num_claimed_files + num_offload_files == 0)
737 return LDPS_OK;
739 if (nop)
741 use_original_files ();
742 return LDPS_OK;
745 lto_argv = (char **) xcalloc (sizeof (char *), num_lto_args);
746 lto_arg_ptr = (const char **) lto_argv;
747 assert (lto_wrapper_argv);
749 write_resolution ();
751 free_1 (claimed_files, num_claimed_files);
753 for (i = 0; i < lto_wrapper_num_args; i++)
754 *lto_arg_ptr++ = lto_wrapper_argv[i];
756 if (!linker_output_known)
758 assert (linker_output_set);
759 switch (linker_output)
761 case LDPO_REL:
762 if (non_claimed_files)
764 if (!linker_output_auto_nolto_rel)
765 message (LDPL_WARNING, "incremental linking of LTO and non-LTO"
766 " objects; using -flinker-output=nolto-rel which will"
767 " bypass whole program optimization");
768 linker_output_str = "-flinker-output=nolto-rel";
770 else
771 linker_output_str = "-flinker-output=rel";
772 break;
773 case LDPO_DYN:
774 linker_output_str = "-flinker-output=dyn";
775 break;
776 case LDPO_PIE:
777 linker_output_str = "-flinker-output=pie";
778 break;
779 case LDPO_EXEC:
780 linker_output_str = "-flinker-output=exec";
781 break;
782 default:
783 message (LDPL_FATAL, "unsupported linker output %i", linker_output);
784 break;
786 *lto_arg_ptr++ = xstrdup (linker_output_str);
789 if (num_offload_files > 0)
791 FILE *f;
792 char *arg;
793 char *offload_objects_file_name;
794 struct plugin_offload_file *ofld;
796 offload_objects_file_name = make_temp_file (".ofldlist");
797 check (offload_objects_file_name, LDPL_FATAL,
798 "Failed to generate a temporary file name");
799 f = fopen (offload_objects_file_name, "w");
800 check (f, LDPL_FATAL, "could not open file with offload objects");
801 fprintf (f, "%u\n", num_offload_files);
803 /* Skip the dummy item at the start of the list. */
804 ofld = offload_files->next;
805 while (ofld)
807 fprintf (f, "%s\n", ofld->name);
808 ofld = ofld->next;
810 fclose (f);
812 arg = concat ("-foffload-objects=", offload_objects_file_name, NULL);
813 check (arg, LDPL_FATAL, "could not allocate");
814 *lto_arg_ptr++ = arg;
817 for (i = 0; i < num_claimed_files; i++)
819 struct plugin_file_info *info = &claimed_files[i];
821 *lto_arg_ptr++ = info->name;
824 *lto_arg_ptr++ = NULL;
825 exec_lto_wrapper (lto_argv);
827 free (lto_argv);
829 /* --pass-through is not needed when using gold 1.11 or later. */
830 if (pass_through_items && gold_version < 111)
832 unsigned int i;
833 for (i = 0; i < num_pass_through_items; i++)
835 if (strncmp (pass_through_items[i], "-l", 2) == 0)
836 add_input_library (pass_through_items[i] + 2);
837 else
838 add_input_file (pass_through_items[i]);
839 free (pass_through_items[i]);
840 pass_through_items[i] = NULL;
842 free (pass_through_items);
843 pass_through_items = NULL;
846 return LDPS_OK;
849 /* Helper, as used in collect2. */
850 static int
851 file_exists (const char *name)
853 return access (name, R_OK) == 0;
856 /* Unlink FILE unless we have save-temps set.
857 Note that we're saving files if verbose output is set. */
859 static void
860 maybe_unlink (const char *file)
862 if (save_temps && file_exists (file))
864 if (verbose)
865 fprintf (stderr, "[Leaving %s]\n", file);
866 return;
869 unlink_if_ordinary (file);
872 /* Remove temporary files at the end of the link. */
874 static enum ld_plugin_status
875 cleanup_handler (void)
877 unsigned int i;
879 if (debug)
880 return LDPS_OK;
882 if (arguments_file_name)
883 maybe_unlink (arguments_file_name);
885 for (i = 0; i < num_output_files; i++)
886 maybe_unlink (output_files[i]);
888 free_2 ();
889 return LDPS_OK;
892 #define SWAP(type, a, b) \
893 do { type tmp_; tmp_ = (a); (a) = (b); (b) = tmp_; } while(0)
895 /* Compare two hash table entries */
897 static int eq_sym (const void *a, const void *b)
899 const struct ld_plugin_symbol *as = (const struct ld_plugin_symbol *)a;
900 const struct ld_plugin_symbol *bs = (const struct ld_plugin_symbol *)b;
902 return !strcmp (as->name, bs->name);
905 /* Hash a symbol */
907 static hashval_t hash_sym (const void *a)
909 const struct ld_plugin_symbol *as = (const struct ld_plugin_symbol *)a;
911 return htab_hash_string (as->name);
914 /* Determine how strong a symbol is */
916 static int symbol_strength (struct ld_plugin_symbol *s)
918 switch (s->def)
920 case LDPK_UNDEF:
921 case LDPK_WEAKUNDEF:
922 return 0;
923 case LDPK_WEAKDEF:
924 return 1;
925 default:
926 return 2;
930 /* In the ld -r case we can get dups in the LTO symbol tables, where
931 the same symbol can have different resolutions (e.g. undefined and defined).
933 We have to keep that in the LTO symbol tables, but the dups confuse
934 gold and then finally gcc by supplying incorrect resolutions.
936 Problem is that the main gold symbol table doesn't know about subids
937 and does not distingush the same symbols in different states.
939 So we drop duplicates from the linker visible symbol table
940 and keep them in a private table. Then later do own symbol
941 resolution for the duplicated based on the results for the
942 originals.
944 Then when writing out the resolution file readd the dropped symbols.
946 XXX how to handle common? */
948 static void
949 resolve_conflicts (struct plugin_symtab *t, struct plugin_symtab *conflicts)
951 htab_t symtab = htab_create (t->nsyms, hash_sym, eq_sym, NULL);
952 int i;
953 int out;
954 int outlen;
956 outlen = t->nsyms;
957 conflicts->syms = xmalloc (sizeof (struct ld_plugin_symbol) * outlen);
958 conflicts->aux = xmalloc (sizeof (struct sym_aux) * outlen);
960 /* Move all duplicate symbols into the auxiliary conflicts table. */
961 out = 0;
962 for (i = 0; i < t->nsyms; i++)
964 struct ld_plugin_symbol *s = &t->syms[i];
965 struct sym_aux *aux = &t->aux[i];
966 void **slot;
968 slot = htab_find_slot (symtab, s, INSERT);
969 if (*slot != NULL)
971 int cnf;
972 struct ld_plugin_symbol *orig = (struct ld_plugin_symbol *)*slot;
973 struct sym_aux *orig_aux = &t->aux[orig - t->syms];
975 /* Always let the linker resolve the strongest symbol */
976 if (symbol_strength (orig) < symbol_strength (s))
978 SWAP (struct ld_plugin_symbol, *orig, *s);
979 SWAP (uint32_t, orig_aux->slot, aux->slot);
980 SWAP (unsigned long long, orig_aux->id, aux->id);
981 /* Don't swap conflict chain pointer */
984 /* Move current symbol into the conflicts table */
985 cnf = conflicts->nsyms++;
986 conflicts->syms[cnf] = *s;
987 conflicts->aux[cnf] = *aux;
988 aux = &conflicts->aux[cnf];
990 /* Update conflicts chain of the original symbol */
991 aux->next_conflict = orig_aux->next_conflict;
992 orig_aux->next_conflict = cnf;
994 continue;
997 /* Remove previous duplicates in the main table */
998 if (out < i)
1000 t->syms[out] = *s;
1001 t->aux[out] = *aux;
1004 /* Put original into the hash table */
1005 *slot = &t->syms[out];
1006 out++;
1009 assert (conflicts->nsyms <= outlen);
1010 assert (conflicts->nsyms + out == t->nsyms);
1012 t->nsyms = out;
1013 htab_delete (symtab);
1016 /* Process one section of an object file. */
1018 static int
1019 process_symtab (void *data, const char *name, off_t offset, off_t length)
1021 struct plugin_objfile *obj = (struct plugin_objfile *)data;
1022 char *s;
1023 char *secdatastart, *secdata;
1025 if (strncmp (name, LTO_SYMTAB_PREFIX, LTO_SYMTAB_PREFIX_LEN) != 0)
1026 return 1;
1028 s = strrchr (name, '.');
1029 if (s)
1030 sscanf (s, ".%" PRI_LL "x", &obj->out->id);
1031 secdata = secdatastart = xmalloc (length);
1032 offset += obj->file->offset;
1033 if (offset != lseek (obj->file->fd, offset, SEEK_SET))
1034 goto err;
1038 ssize_t got = read (obj->file->fd, secdata, length);
1039 if (got == 0)
1040 break;
1041 else if (got > 0)
1043 secdata += got;
1044 length -= got;
1046 else if (errno != EINTR)
1047 goto err;
1049 while (length > 0);
1050 if (length > 0)
1051 goto err;
1053 translate (secdatastart, secdata, obj->out);
1054 obj->found++;
1055 free (secdatastart);
1056 return 1;
1058 err:
1059 if (message)
1060 message (LDPL_FATAL, "%s: corrupt object file", obj->file->name);
1061 /* Force claim_file_handler to abandon this file. */
1062 obj->found = 0;
1063 free (secdatastart);
1064 return 0;
1067 /* Process one section of an object file. */
1069 static int
1070 process_symtab_extension (void *data, const char *name, off_t offset,
1071 off_t length)
1073 struct plugin_objfile *obj = (struct plugin_objfile *)data;
1074 char *s;
1075 char *secdatastart, *secdata;
1077 if (strncmp (name, LTO_SYMTAB_EXT_PREFIX, LTO_SYMTAB_EXT_PREFIX_LEN) != 0)
1078 return 1;
1080 s = strrchr (name, '.');
1081 if (s)
1082 sscanf (s, ".%" PRI_LL "x", &obj->out->id);
1083 secdata = secdatastart = xmalloc (length);
1084 offset += obj->file->offset;
1085 if (offset != lseek (obj->file->fd, offset, SEEK_SET))
1086 goto err;
1090 ssize_t got = read (obj->file->fd, secdata, length);
1091 if (got == 0)
1092 break;
1093 else if (got > 0)
1095 secdata += got;
1096 length -= got;
1098 else if (errno != EINTR)
1099 goto err;
1101 while (length > 0);
1102 if (length > 0)
1103 goto err;
1105 parse_symtab_extension (secdatastart, secdata, obj->out);
1106 obj->found++;
1107 free (secdatastart);
1108 return 1;
1110 err:
1111 if (message)
1112 message (LDPL_FATAL, "%s: corrupt object file", obj->file->name);
1113 /* Force claim_file_handler to abandon this file. */
1114 obj->found = 0;
1115 free (secdatastart);
1116 return 0;
1120 /* Find an offload section of an object file. */
1122 static int
1123 process_offload_section (void *data, const char *name, off_t offset, off_t len)
1125 if (!strncmp (name, OFFLOAD_SECTION, OFFLOAD_SECTION_LEN))
1127 struct plugin_objfile *obj = (struct plugin_objfile *) data;
1128 obj->offload = 1;
1129 return 0;
1132 return 1;
1135 /* Callback used by gold to check if the plugin will claim FILE. Writes
1136 the result in CLAIMED. */
1138 static enum ld_plugin_status
1139 claim_file_handler (const struct ld_plugin_input_file *file, int *claimed)
1141 enum ld_plugin_status status;
1142 struct plugin_objfile obj;
1143 struct plugin_file_info lto_file;
1144 int err;
1145 const char *errmsg;
1147 memset (&lto_file, 0, sizeof (struct plugin_file_info));
1149 if (file->offset != 0)
1151 /* We pass the offset of the actual file, not the archive header.
1152 Can't use PRIx64, because that's C99, so we have to print the
1153 64-bit hex int as two 32-bit ones. Use xasprintf instead of
1154 asprintf because asprintf doesn't work as expected on some older
1155 mingw32 hosts. */
1156 int lo, hi;
1157 lo = file->offset & 0xffffffff;
1158 hi = ((int64_t)file->offset >> 32) & 0xffffffff;
1159 lto_file.name = hi ? xasprintf ("%s@0x%x%08x", file->name, hi, lo)
1160 : xasprintf ("%s@0x%x", file->name, lo);
1162 else
1164 lto_file.name = xstrdup (file->name);
1166 lto_file.handle = file->handle;
1168 *claimed = 0;
1169 obj.file = file;
1170 obj.found = 0;
1171 obj.offload = 0;
1172 obj.out = &lto_file.symtab;
1173 errmsg = NULL;
1174 obj.objfile = simple_object_start_read (file->fd, file->offset, LTO_SEGMENT_NAME,
1175 &errmsg, &err);
1176 /* No file, but also no error code means unrecognized format; just skip it. */
1177 if (!obj.objfile && !err)
1178 goto err;
1180 if (obj.objfile)
1182 errmsg = simple_object_find_sections (obj.objfile, process_symtab, &obj,
1183 &err);
1184 /* Parsing symtab extension should be done only for add_symbols_v2 and
1185 later versions. */
1186 if (!errmsg && add_symbols_v2 != NULL)
1188 obj.out->last_sym = 0;
1189 errmsg = simple_object_find_sections (obj.objfile,
1190 process_symtab_extension,
1191 &obj, &err);
1195 if (!obj.objfile || errmsg)
1197 if (err && message)
1198 message (LDPL_FATAL, "%s: %s: %s", file->name, errmsg,
1199 xstrerror (err));
1200 else if (message)
1201 message (LDPL_FATAL, "%s: %s", file->name, errmsg);
1202 goto err;
1205 if (obj.objfile)
1206 simple_object_find_sections (obj.objfile, process_offload_section,
1207 &obj, &err);
1209 if (obj.found == 0 && obj.offload == 0)
1210 goto err;
1212 if (obj.found > 1)
1213 resolve_conflicts (&lto_file.symtab, &lto_file.conflicts);
1215 if (obj.found > 0)
1217 if (add_symbols_v2)
1218 status = add_symbols_v2 (file->handle, lto_file.symtab.nsyms,
1219 lto_file.symtab.syms);
1220 else
1221 status = add_symbols (file->handle, lto_file.symtab.nsyms,
1222 lto_file.symtab.syms);
1223 check (status == LDPS_OK, LDPL_FATAL, "could not add symbols");
1225 num_claimed_files++;
1226 claimed_files =
1227 xrealloc (claimed_files,
1228 num_claimed_files * sizeof (struct plugin_file_info));
1229 claimed_files[num_claimed_files - 1] = lto_file;
1231 *claimed = 1;
1234 if (offload_files == NULL)
1236 /* Add dummy item to the start of the list. */
1237 offload_files = xmalloc (sizeof (struct plugin_offload_file));
1238 offload_files->name = NULL;
1239 offload_files->next = NULL;
1240 offload_files_last = offload_files;
1243 /* If this is an LTO file without offload, and it is the first LTO file, save
1244 the pointer to the last offload file in the list. Further offload LTO
1245 files will be inserted after it, if any. */
1246 if (*claimed && obj.offload == 0 && offload_files_last_lto == NULL)
1247 offload_files_last_lto = offload_files_last;
1249 if (obj.offload == 1)
1251 /* Add file to the list. The order must be exactly the same as the final
1252 order after recompilation and linking, otherwise host and target tables
1253 with addresses wouldn't match. If a static library contains both LTO
1254 and non-LTO objects, ld and gold link them in a different order. */
1255 struct plugin_offload_file *ofld
1256 = xmalloc (sizeof (struct plugin_offload_file));
1257 ofld->name = lto_file.name;
1258 ofld->next = NULL;
1260 if (*claimed && offload_files_last_lto == NULL && file->offset != 0
1261 && gold_version == -1)
1263 /* ld only: insert first LTO file from the archive after the last real
1264 object file immediately preceding the archive, or at the begin of
1265 the list if there was no real objects before archives. */
1266 if (offload_files_last_obj != NULL)
1268 ofld->next = offload_files_last_obj->next;
1269 offload_files_last_obj->next = ofld;
1271 else
1273 ofld->next = offload_files->next;
1274 offload_files->next = ofld;
1277 else if (*claimed && offload_files_last_lto != NULL)
1279 /* Insert LTO file after the last LTO file in the list. */
1280 ofld->next = offload_files_last_lto->next;
1281 offload_files_last_lto->next = ofld;
1283 else
1284 /* Add non-LTO file or first non-archive LTO file to the end of the
1285 list. */
1286 offload_files_last->next = ofld;
1288 if (ofld->next == NULL)
1289 offload_files_last = ofld;
1290 if (file->offset == 0)
1291 offload_files_last_obj = ofld;
1292 if (*claimed)
1293 offload_files_last_lto = ofld;
1294 num_offload_files++;
1297 goto cleanup;
1299 err:
1300 non_claimed_files++;
1301 free (lto_file.name);
1303 cleanup:
1304 if (obj.objfile)
1305 simple_object_release_read (obj.objfile);
1307 return LDPS_OK;
1310 /* Parse the plugin options. */
1312 static void
1313 process_option (const char *option)
1315 if (strcmp (option, "-linker-output-known") == 0)
1316 linker_output_known = true;
1317 else if (strcmp (option, "-linker-output-auto-notlo-rel") == 0)
1318 linker_output_auto_nolto_rel = true;
1319 else if (strcmp (option, "-debug") == 0)
1320 debug = true;
1321 else if ((strcmp (option, "-v") == 0)
1322 || (strcmp (option, "--verbose") == 0))
1323 verbose = true;
1324 else if (strcmp (option, "-save-temps") == 0)
1325 save_temps = true;
1326 else if (strcmp (option, "-nop") == 0)
1327 nop = 1;
1328 else if (!strncmp (option, "-pass-through=", strlen("-pass-through=")))
1330 num_pass_through_items++;
1331 pass_through_items = xrealloc (pass_through_items,
1332 num_pass_through_items * sizeof (char *));
1333 pass_through_items[num_pass_through_items - 1] =
1334 xstrdup (option + strlen ("-pass-through="));
1336 else if (!strncmp (option, "-sym-style=", sizeof ("-sym-style=") - 1))
1338 switch (option[sizeof ("-sym-style=") - 1])
1340 case 'w':
1341 sym_style = ss_win32;
1342 break;
1343 case 'u':
1344 sym_style = ss_uscore;
1345 break;
1346 default:
1347 sym_style = ss_none;
1348 break;
1351 else
1353 int size;
1354 char *opt = xstrdup (option);
1355 lto_wrapper_num_args += 1;
1356 size = lto_wrapper_num_args * sizeof (char *);
1357 lto_wrapper_argv = (char **) xrealloc (lto_wrapper_argv, size);
1358 lto_wrapper_argv[lto_wrapper_num_args - 1] = opt;
1359 if (strncmp (option, "-fresolution=", sizeof ("-fresolution=") - 1) == 0)
1360 resolution_file = opt + sizeof ("-fresolution=") - 1;
1362 save_temps = save_temps || debug;
1363 verbose = verbose || debug;
1366 /* Called by gold after loading the plugin. TV is the transfer vector. */
1368 enum ld_plugin_status
1369 onload (struct ld_plugin_tv *tv)
1371 struct ld_plugin_tv *p;
1372 enum ld_plugin_status status;
1374 p = tv;
1375 while (p->tv_tag)
1377 switch (p->tv_tag)
1379 case LDPT_MESSAGE:
1380 message = p->tv_u.tv_message;
1381 break;
1382 case LDPT_REGISTER_CLAIM_FILE_HOOK:
1383 register_claim_file = p->tv_u.tv_register_claim_file;
1384 break;
1385 case LDPT_ADD_SYMBOLS_V2:
1386 add_symbols_v2 = p->tv_u.tv_add_symbols;
1387 break;
1388 case LDPT_ADD_SYMBOLS:
1389 add_symbols = p->tv_u.tv_add_symbols;
1390 break;
1391 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
1392 register_all_symbols_read = p->tv_u.tv_register_all_symbols_read;
1393 break;
1394 case LDPT_GET_SYMBOLS_V2:
1395 get_symbols_v2 = p->tv_u.tv_get_symbols;
1396 break;
1397 case LDPT_GET_SYMBOLS:
1398 get_symbols = p->tv_u.tv_get_symbols;
1399 break;
1400 case LDPT_REGISTER_CLEANUP_HOOK:
1401 register_cleanup = p->tv_u.tv_register_cleanup;
1402 break;
1403 case LDPT_ADD_INPUT_FILE:
1404 add_input_file = p->tv_u.tv_add_input_file;
1405 break;
1406 case LDPT_ADD_INPUT_LIBRARY:
1407 add_input_library = p->tv_u.tv_add_input_library;
1408 break;
1409 case LDPT_OPTION:
1410 process_option (p->tv_u.tv_string);
1411 break;
1412 case LDPT_GOLD_VERSION:
1413 gold_version = p->tv_u.tv_val;
1414 break;
1415 case LDPT_LINKER_OUTPUT:
1416 linker_output = (enum ld_plugin_output_file_type) p->tv_u.tv_val;
1417 linker_output_set = true;
1418 break;
1419 case LDPT_OUTPUT_NAME:
1420 /* We only use this to make user-friendly temp file names. */
1421 link_output_name = p->tv_u.tv_string;
1422 break;
1423 default:
1424 break;
1426 p++;
1429 check (register_claim_file, LDPL_FATAL, "register_claim_file not found");
1430 check (add_symbols, LDPL_FATAL, "add_symbols not found");
1431 status = register_claim_file (claim_file_handler);
1432 check (status == LDPS_OK, LDPL_FATAL,
1433 "could not register the claim_file callback");
1435 if (register_cleanup)
1437 status = register_cleanup (cleanup_handler);
1438 check (status == LDPS_OK, LDPL_FATAL,
1439 "could not register the cleanup callback");
1442 if (register_all_symbols_read)
1444 check (get_symbols, LDPL_FATAL, "get_symbols not found");
1445 status = register_all_symbols_read (all_symbols_read_handler);
1446 check (status == LDPS_OK, LDPL_FATAL,
1447 "could not register the all_symbols_read callback");
1450 char *collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
1451 if (collect_gcc_options)
1453 /* Support -fno-use-linker-plugin by failing to load the plugin
1454 for the case where it is auto-loaded by BFD. */
1455 if (strstr (collect_gcc_options, "'-fno-use-linker-plugin'"))
1456 return LDPS_ERR;
1458 if (strstr (collect_gcc_options, "'-save-temps'"))
1459 save_temps = true;
1461 if (strstr (collect_gcc_options, "'-v'")
1462 || strstr (collect_gcc_options, "'--verbose'"))
1463 verbose = true;
1465 const char *p;
1466 if ((p = strstr (collect_gcc_options, "'-dumpdir'")))
1468 p += sizeof ("'-dumpdir'");
1469 while (*p == ' ')
1470 p++;
1471 const char *start = p;
1472 int ticks = 0, escapes = 0;
1473 /* Count ticks (') and escaped (\.) characters. Stop at the
1474 end of the options or at a blank after an even number of
1475 ticks (not counting escaped ones. */
1476 for (p = start; *p; p++)
1478 if (*p == '\'')
1480 ticks++;
1481 continue;
1483 else if ((ticks % 2) != 0)
1485 if (*p == ' ')
1486 break;
1487 if (*p == '\\')
1489 if (*++p)
1490 escapes++;
1491 else
1492 p--;
1497 /* Now allocate a new link_output_name and decode dumpdir
1498 into it. The loop uses the same logic, except it counts
1499 ticks and escapes backwards (so ticks is adjusted if we
1500 find an odd number of them), and it copies characters
1501 that are escaped or not otherwise skipped. */
1502 int len = p - start - ticks - escapes + 1;
1503 char *q = xmalloc (len);
1504 link_output_name = q;
1505 int oddticks = (ticks % 2);
1506 ticks += oddticks;
1507 for (p = start; *p; p++)
1509 if (*p == '\'')
1511 ticks--;
1512 continue;
1514 else if ((ticks % 2) != 0)
1516 if (*p == ' ')
1517 break;
1518 if (*p == '\\')
1520 if (*++p)
1521 escapes--;
1522 else
1523 p--;
1526 *q++ = *p;
1528 *q = '\0';
1529 assert (escapes == 0);
1530 assert (ticks == oddticks);
1531 assert (q - link_output_name == len - 1);
1532 skip_in_suffix = 1;
1536 return LDPS_OK;