arm: fix checking ICE in arm_print_operand [PR106004]
[official-gcc.git] / lto-plugin / lto-plugin.c
blob47378435612aee920cd6cdd459a7d9fcdfb33602
1 /* LTO plugin for linkers like gold, GNU ld or mold.
2 Copyright (C) 2009-2022 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. A linker passes it an array of
20 function that the plugin uses to communicate back to the linker.
22 With the functions provided by the linker, the plugin can be notified when
23 the linker first analyzes a file and pass a symbol table back to the linker. 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-nolto-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 /* Return true if STR string starts with PREFIX. */
94 static inline bool
95 startswith (const char *str, const char *prefix)
97 return strncmp (str, prefix, strlen (prefix)) == 0;
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. */
105 struct sym_aux
107 uint32_t slot;
108 unsigned long long id;
109 unsigned next_conflict;
112 struct plugin_symtab
114 int nsyms;
115 int last_sym;
116 struct sym_aux *aux;
117 struct ld_plugin_symbol *syms;
118 unsigned long long id;
121 /* Encapsulates object file data during symbol scan. */
122 struct plugin_objfile
124 int found;
125 bool offload;
126 simple_object_read *objfile;
127 struct plugin_symtab *out;
128 const struct ld_plugin_input_file *file;
131 /* All that we have to remember about a file. */
133 struct plugin_file_info
135 char *name;
136 void *handle;
137 struct plugin_symtab symtab;
138 struct plugin_symtab conflicts;
141 /* List item with name of the file with offloading. */
143 struct plugin_offload_file
145 char *name;
146 struct plugin_offload_file *next;
149 /* Until ASM_OUTPUT_LABELREF can be hookized and decoupled from
150 stdio file streams, we do simple label translation here. */
152 enum symbol_style
154 ss_none, /* No underscore prefix. */
155 ss_win32, /* Underscore prefix any symbol not beginning with '@'. */
156 ss_uscore, /* Underscore prefix all symbols. */
159 static char *arguments_file_name;
160 static ld_plugin_register_claim_file register_claim_file;
161 static ld_plugin_register_all_symbols_read register_all_symbols_read;
162 static ld_plugin_get_symbols get_symbols, get_symbols_v2;
163 static ld_plugin_register_cleanup register_cleanup;
164 static ld_plugin_add_input_file add_input_file;
165 static ld_plugin_add_input_library add_input_library;
166 static ld_plugin_message message;
167 static ld_plugin_add_symbols add_symbols, add_symbols_v2;
169 static struct plugin_file_info *claimed_files = NULL;
170 static unsigned int num_claimed_files = 0;
171 static unsigned int non_claimed_files = 0;
173 /* List of files with offloading. */
174 static struct plugin_offload_file *offload_files;
175 /* Last file in the list. */
176 static struct plugin_offload_file *offload_files_last;
177 /* Last non-archive file in the list. */
178 static struct plugin_offload_file *offload_files_last_obj;
179 /* Last LTO file in the list. */
180 static struct plugin_offload_file *offload_files_last_lto;
181 /* Total number of files with offloading. */
182 static unsigned num_offload_files;
184 static char **output_files = NULL;
185 static unsigned int num_output_files = 0;
187 static char **lto_wrapper_argv;
188 static int lto_wrapper_num_args;
190 static char **pass_through_items = NULL;
191 static unsigned int num_pass_through_items;
193 static char *ltrans_objects = NULL;
195 static bool debug;
196 static bool save_temps;
197 static bool verbose;
198 static char nop;
199 static char *resolution_file = NULL;
200 static enum ld_plugin_output_file_type linker_output;
201 static bool linker_output_set;
202 static bool linker_output_known;
203 static bool linker_output_auto_nolto_rel;
204 static const char *link_output_name = NULL;
206 /* This indicates link_output_name already contains the dot of the
207 suffix, so we can skip it in extensions. */
208 static bool skip_in_suffix = false;
210 /* The version of gold being used, or -1 if not gold. The number is
211 MAJOR * 100 + MINOR. */
212 static int gold_version = -1;
214 /* Not used by default, but can be overridden at runtime
215 by using -plugin-opt=-sym-style={none,win32,underscore|uscore}
216 (in fact, only first letter of style arg is checked.) */
217 static enum symbol_style sym_style = ss_none;
219 static void
220 check_1 (int gate, enum ld_plugin_level level, const char *text)
222 if (gate)
223 return;
225 if (message)
226 message (level, text);
227 else
229 /* If there is no nicer way to inform the user, fallback to stderr. */
230 fprintf (stderr, "%s\n", text);
231 if (level == LDPL_FATAL)
232 abort ();
236 /* This little wrapper allows check to be called with a non-integer
237 first argument, such as a pointer that must be non-NULL. We can't
238 use c99 bool type to coerce it into range, so we explicitly test. */
239 #define check(GATE, LEVEL, TEXT) check_1 (((GATE) != 0), (LEVEL), (TEXT))
241 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
242 by P and the result is written in ENTRY. The slot number is stored in SLOT.
243 Returns the address of the next entry. */
245 static char *
246 parse_table_entry (char *p, struct ld_plugin_symbol *entry,
247 struct sym_aux *aux)
249 unsigned char t;
250 enum ld_plugin_symbol_kind translate_kind[] =
252 LDPK_DEF,
253 LDPK_WEAKDEF,
254 LDPK_UNDEF,
255 LDPK_WEAKUNDEF,
256 LDPK_COMMON
259 enum ld_plugin_symbol_visibility translate_visibility[] =
261 LDPV_DEFAULT,
262 LDPV_PROTECTED,
263 LDPV_INTERNAL,
264 LDPV_HIDDEN
267 switch (sym_style)
269 case ss_win32:
270 if (p[0] == '@')
272 /* cf. Duff's device. */
273 case ss_none:
274 entry->name = xstrdup (p);
275 break;
277 /* FALL-THROUGH. */
278 case ss_uscore:
279 entry->name = concat ("_", p, NULL);
280 break;
281 default:
282 check (0, LDPL_FATAL, "invalid symbol style requested");
283 break;
285 while (*p)
286 p++;
287 p++;
289 entry->version = NULL;
291 entry->comdat_key = p;
292 while (*p)
293 p++;
294 p++;
296 if (strlen (entry->comdat_key) == 0)
297 entry->comdat_key = NULL;
298 else
299 entry->comdat_key = xstrdup (entry->comdat_key);
301 entry->unused = entry->section_kind = entry->symbol_type = 0;
303 t = *p;
304 check (t <= 4, LDPL_FATAL, "invalid symbol kind found");
305 entry->def = translate_kind[t];
306 p++;
308 t = *p;
309 check (t <= 3, LDPL_FATAL, "invalid symbol visibility found");
310 entry->visibility = translate_visibility[t];
311 p++;
313 memcpy (&entry->size, p, sizeof (uint64_t));
314 p += 8;
316 memcpy (&aux->slot, p, sizeof (uint32_t));
317 p += 4;
319 entry->resolution = LDPR_UNKNOWN;
321 aux->next_conflict = -1;
323 return p;
326 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
327 by P and the result is written in ENTRY. The slot number is stored in SLOT.
328 Returns the address of the next entry. */
330 static char *
331 parse_table_entry_extension (char *p, struct ld_plugin_symbol *entry)
333 unsigned char t;
334 enum ld_plugin_symbol_type symbol_types[] =
336 LDST_UNKNOWN,
337 LDST_FUNCTION,
338 LDST_VARIABLE,
341 t = *p;
342 check (t <= 2, LDPL_FATAL, "invalid symbol type found");
343 entry->symbol_type = symbol_types[t];
344 p++;
345 entry->section_kind = *p;
346 p++;
348 return p;
352 /* Translate the IL symbol table located between DATA and END. Append the
353 slots and symbols to OUT. */
355 static void
356 translate (char *data, char *end, struct plugin_symtab *out)
358 struct sym_aux *aux;
359 struct ld_plugin_symbol *syms = NULL;
360 int n, len;
362 /* This overestimates the output buffer sizes, but at least
363 the algorithm is O(1) now. */
365 len = (end - data)/8 + out->nsyms + 1;
366 syms = xrealloc (out->syms, len * sizeof (struct ld_plugin_symbol));
367 aux = xrealloc (out->aux, len * sizeof (struct sym_aux));
369 for (n = out->nsyms; data < end; n++)
371 aux[n].id = out->id;
372 data = parse_table_entry (data, &syms[n], &aux[n]);
375 assert(n < len);
377 out->nsyms = n;
378 out->syms = syms;
379 out->aux = aux;
382 static void
383 parse_symtab_extension (char *data, char *end, struct plugin_symtab *out)
385 unsigned long i;
386 unsigned char version;
388 if (data >= end)
389 /* FIXME: Issue an error ? */
390 return;
392 version = *data;
393 data++;
395 if (version != 1)
396 return;
398 /* Version 1 contains the following data per entry:
399 - symbol_type
400 - section_kind
401 . */
403 unsigned long nsyms = (end - data) / 2;
405 for (i = 0; i < nsyms; i++)
406 data = parse_table_entry_extension (data, out->syms + i + out->last_sym);
408 out->last_sym += nsyms;
411 /* Free all memory that is no longer needed after writing the symbol
412 resolution. */
414 static void
415 free_1 (struct plugin_file_info *files, unsigned num_files)
417 unsigned int i;
418 for (i = 0; i < num_files; i++)
420 struct plugin_file_info *info = &files[i];
421 struct plugin_symtab *symtab = &info->symtab;
422 unsigned int j;
423 for (j = 0; j < symtab->nsyms; j++)
425 struct ld_plugin_symbol *s = &symtab->syms[j];
426 free (s->name);
427 free (s->comdat_key);
429 free (symtab->syms);
430 symtab->syms = NULL;
434 /* Free all remaining memory. */
436 static void
437 free_2 (void)
439 unsigned int i;
440 for (i = 0; i < num_claimed_files; i++)
442 struct plugin_file_info *info = &claimed_files[i];
443 struct plugin_symtab *symtab = &info->symtab;
444 free (symtab->aux);
445 free (info->name);
448 for (i = 0; i < num_output_files; i++)
449 free (output_files[i]);
450 free (output_files);
452 free (claimed_files);
453 claimed_files = NULL;
454 num_claimed_files = 0;
456 while (offload_files)
458 struct plugin_offload_file *ofld = offload_files;
459 offload_files = offload_files->next;
460 free (ofld);
462 num_offload_files = 0;
464 free (arguments_file_name);
465 arguments_file_name = NULL;
468 /* Dump SYMTAB to resolution file F. */
470 static void
471 dump_symtab (FILE *f, struct plugin_symtab *symtab)
473 unsigned j;
475 for (j = 0; j < symtab->nsyms; j++)
477 uint32_t slot = symtab->aux[j].slot;
478 unsigned int resolution = symtab->syms[j].resolution;
480 assert (resolution != LDPR_UNKNOWN);
482 fprintf (f, "%u %" PRI_LL "x %s %s\n",
483 (unsigned int) slot, symtab->aux[j].id,
484 lto_resolution_str[resolution],
485 symtab->syms[j].name);
489 /* Finish the conflicts' resolution information after the linker resolved
490 the original symbols */
492 static void
493 finish_conflict_resolution (struct plugin_symtab *symtab,
494 struct plugin_symtab *conflicts)
496 int i, j;
498 if (conflicts->nsyms == 0)
499 return;
501 for (i = 0; i < symtab->nsyms; i++)
503 char resolution = LDPR_UNKNOWN;
505 if (symtab->aux[i].next_conflict == -1)
506 continue;
508 switch (symtab->syms[i].def)
510 case LDPK_DEF:
511 case LDPK_COMMON: /* ??? */
512 resolution = LDPR_RESOLVED_IR;
513 break;
514 case LDPK_WEAKDEF:
515 resolution = LDPR_PREEMPTED_IR;
516 break;
517 case LDPK_UNDEF:
518 case LDPK_WEAKUNDEF:
519 resolution = symtab->syms[i].resolution;
520 break;
521 default:
522 assert (0);
525 assert (resolution != LDPR_UNKNOWN);
527 for (j = symtab->aux[i].next_conflict;
528 j != -1;
529 j = conflicts->aux[j].next_conflict)
530 conflicts->syms[j].resolution = resolution;
534 /* Free symbol table SYMTAB. */
536 static void
537 free_symtab (struct plugin_symtab *symtab)
539 free (symtab->syms);
540 symtab->syms = NULL;
541 free (symtab->aux);
542 symtab->aux = NULL;
545 /* Writes the relocations to disk. */
547 static void
548 write_resolution (void)
550 unsigned int i;
551 FILE *f;
553 check (resolution_file, LDPL_FATAL, "resolution file not specified");
554 f = fopen (resolution_file, "w");
555 check (f, LDPL_FATAL, "could not open file");
557 fprintf (f, "%d\n", num_claimed_files);
559 for (i = 0; i < num_claimed_files; i++)
561 struct plugin_file_info *info = &claimed_files[i];
562 struct plugin_symtab *symtab = &info->symtab;
563 struct ld_plugin_symbol *syms = symtab->syms;
565 /* Version 2 of API supports IRONLY_EXP resolution that is
566 accepted by GCC-4.7 and newer. */
567 if (get_symbols_v2)
568 get_symbols_v2 (info->handle, symtab->nsyms, syms);
569 else
570 get_symbols (info->handle, symtab->nsyms, syms);
572 finish_conflict_resolution (symtab, &info->conflicts);
574 fprintf (f, "%s %d\n", info->name, symtab->nsyms + info->conflicts.nsyms);
575 dump_symtab (f, symtab);
576 if (info->conflicts.nsyms)
578 dump_symtab (f, &info->conflicts);
579 free_symtab (&info->conflicts);
582 fclose (f);
585 /* Pass files generated by the lto-wrapper to the linker. FD is lto-wrapper's
586 stdout. */
588 static void
589 add_output_files (FILE *f)
591 for (;;)
593 const unsigned piece = 32;
594 char *buf, *s = xmalloc (piece);
595 size_t len;
597 buf = s;
598 cont:
599 if (!fgets (buf, piece, f))
601 free (s);
602 break;
604 len = strlen (s);
605 if (s[len - 1] != '\n')
607 s = xrealloc (s, len + piece);
608 buf = s + len;
609 goto cont;
611 s[len - 1] = '\0';
613 num_output_files++;
614 output_files
615 = xrealloc (output_files, num_output_files * sizeof (char *));
616 output_files[num_output_files - 1] = s;
617 add_input_file (output_files[num_output_files - 1]);
621 /* Execute the lto-wrapper. ARGV[0] is the binary. The rest of ARGV is the
622 argument list. */
624 static void
625 exec_lto_wrapper (char *argv[])
627 int t, i;
628 int status;
629 char *at_args;
630 FILE *args;
631 FILE *wrapper_output;
632 char *new_argv[3];
633 struct pex_obj *pex;
634 const char *errmsg;
636 /* Write argv to a file to avoid a command line that is too long
637 Save the file locally on save-temps. */
638 const char *suffix = ".lto_wrapper_args";
639 if (skip_in_suffix)
640 suffix++;
641 if (save_temps && link_output_name)
642 arguments_file_name = concat (link_output_name, 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 if (ltrans_objects)
747 FILE *objs = fopen (ltrans_objects, "r");
748 add_output_files (objs);
749 fclose (objs);
750 return LDPS_OK;
753 lto_argv = (char **) xcalloc (sizeof (char *), num_lto_args);
754 lto_arg_ptr = (const char **) lto_argv;
755 assert (lto_wrapper_argv);
757 write_resolution ();
759 free_1 (claimed_files, num_claimed_files);
761 for (i = 0; i < lto_wrapper_num_args; i++)
762 *lto_arg_ptr++ = lto_wrapper_argv[i];
764 if (!linker_output_known)
766 assert (linker_output_set);
767 switch (linker_output)
769 case LDPO_REL:
770 if (non_claimed_files)
772 if (!linker_output_auto_nolto_rel)
773 message (LDPL_WARNING, "incremental linking of LTO and non-LTO"
774 " objects; using -flinker-output=nolto-rel which will"
775 " bypass whole program optimization");
776 linker_output_str = "-flinker-output=nolto-rel";
778 else
779 linker_output_str = "-flinker-output=rel";
780 break;
781 case LDPO_DYN:
782 linker_output_str = "-flinker-output=dyn";
783 break;
784 case LDPO_PIE:
785 linker_output_str = "-flinker-output=pie";
786 break;
787 case LDPO_EXEC:
788 linker_output_str = "-flinker-output=exec";
789 break;
790 default:
791 message (LDPL_FATAL, "unsupported linker output %i", linker_output);
792 break;
794 *lto_arg_ptr++ = xstrdup (linker_output_str);
797 if (num_offload_files > 0)
799 FILE *f;
800 char *arg;
801 char *offload_objects_file_name;
802 struct plugin_offload_file *ofld;
803 const char *suffix = ".ofldlist";
805 if (save_temps && link_output_name)
807 suffix += skip_in_suffix;
808 offload_objects_file_name = concat (link_output_name, suffix, NULL);
810 else
811 offload_objects_file_name = make_temp_file (suffix);
812 check (offload_objects_file_name, LDPL_FATAL,
813 "Failed to generate a temporary file name");
814 f = fopen (offload_objects_file_name, "w");
815 check (f, LDPL_FATAL, "could not open file with offload objects");
816 fprintf (f, "%u\n", num_offload_files);
818 /* Skip the dummy item at the start of the list. */
819 ofld = offload_files->next;
820 while (ofld)
822 fprintf (f, "%s\n", ofld->name);
823 ofld = ofld->next;
825 fclose (f);
827 arg = concat ("-foffload-objects=", offload_objects_file_name, NULL);
828 check (arg, LDPL_FATAL, "could not allocate");
829 *lto_arg_ptr++ = arg;
832 for (i = 0; i < num_claimed_files; i++)
834 struct plugin_file_info *info = &claimed_files[i];
836 *lto_arg_ptr++ = info->name;
839 *lto_arg_ptr++ = NULL;
840 exec_lto_wrapper (lto_argv);
842 free (lto_argv);
844 /* --pass-through is not needed when using gold 1.11 or later. */
845 if (pass_through_items && gold_version < 111)
847 unsigned int i;
848 for (i = 0; i < num_pass_through_items; i++)
850 if (startswith (pass_through_items[i], "-l"))
851 add_input_library (pass_through_items[i] + 2);
852 else
853 add_input_file (pass_through_items[i]);
854 free (pass_through_items[i]);
855 pass_through_items[i] = NULL;
857 free (pass_through_items);
858 pass_through_items = NULL;
861 return LDPS_OK;
864 /* Helper, as used in collect2. */
865 static int
866 file_exists (const char *name)
868 return access (name, R_OK) == 0;
871 /* Unlink FILE unless we have save-temps set.
872 Note that we're saving files if verbose output is set. */
874 static void
875 maybe_unlink (const char *file)
877 if (save_temps && file_exists (file))
879 if (verbose)
880 fprintf (stderr, "[Leaving %s]\n", file);
881 return;
884 unlink_if_ordinary (file);
887 /* Remove temporary files at the end of the link. */
889 static enum ld_plugin_status
890 cleanup_handler (void)
892 unsigned int i;
894 if (debug)
895 return LDPS_OK;
897 if (arguments_file_name)
898 maybe_unlink (arguments_file_name);
900 for (i = 0; i < num_output_files; i++)
901 maybe_unlink (output_files[i]);
903 free_2 ();
904 return LDPS_OK;
907 #define SWAP(type, a, b) \
908 do { type tmp_; tmp_ = (a); (a) = (b); (b) = tmp_; } while(0)
910 /* Compare two hash table entries */
912 static int eq_sym (const void *a, const void *b)
914 const struct ld_plugin_symbol *as = (const struct ld_plugin_symbol *)a;
915 const struct ld_plugin_symbol *bs = (const struct ld_plugin_symbol *)b;
917 return !strcmp (as->name, bs->name);
920 /* Hash a symbol */
922 static hashval_t hash_sym (const void *a)
924 const struct ld_plugin_symbol *as = (const struct ld_plugin_symbol *)a;
926 return htab_hash_string (as->name);
929 /* Determine how strong a symbol is */
931 static int symbol_strength (struct ld_plugin_symbol *s)
933 switch (s->def)
935 case LDPK_UNDEF:
936 case LDPK_WEAKUNDEF:
937 return 0;
938 case LDPK_WEAKDEF:
939 return 1;
940 default:
941 return 2;
945 /* In the ld -r case we can get dups in the LTO symbol tables, where
946 the same symbol can have different resolutions (e.g. undefined and defined).
948 We have to keep that in the LTO symbol tables, but the dups confuse
949 gold and then finally gcc by supplying incorrect resolutions.
951 Problem is that the main gold symbol table doesn't know about subids
952 and does not distingush the same symbols in different states.
954 So we drop duplicates from the linker visible symbol table
955 and keep them in a private table. Then later do own symbol
956 resolution for the duplicated based on the results for the
957 originals.
959 Then when writing out the resolution file readd the dropped symbols.
961 XXX how to handle common? */
963 static void
964 resolve_conflicts (struct plugin_symtab *t, struct plugin_symtab *conflicts)
966 htab_t symtab = htab_create (t->nsyms, hash_sym, eq_sym, NULL);
967 int i;
968 int out;
969 int outlen;
971 outlen = t->nsyms;
972 conflicts->syms = xmalloc (sizeof (struct ld_plugin_symbol) * outlen);
973 conflicts->aux = xmalloc (sizeof (struct sym_aux) * outlen);
975 /* Move all duplicate symbols into the auxiliary conflicts table. */
976 out = 0;
977 for (i = 0; i < t->nsyms; i++)
979 struct ld_plugin_symbol *s = &t->syms[i];
980 struct sym_aux *aux = &t->aux[i];
981 void **slot;
983 slot = htab_find_slot (symtab, s, INSERT);
984 if (*slot != NULL)
986 int cnf;
987 struct ld_plugin_symbol *orig = (struct ld_plugin_symbol *)*slot;
988 struct sym_aux *orig_aux = &t->aux[orig - t->syms];
990 /* Always let the linker resolve the strongest symbol */
991 if (symbol_strength (orig) < symbol_strength (s))
993 SWAP (struct ld_plugin_symbol, *orig, *s);
994 SWAP (uint32_t, orig_aux->slot, aux->slot);
995 SWAP (unsigned long long, orig_aux->id, aux->id);
996 /* Don't swap conflict chain pointer */
999 /* Move current symbol into the conflicts table */
1000 cnf = conflicts->nsyms++;
1001 conflicts->syms[cnf] = *s;
1002 conflicts->aux[cnf] = *aux;
1003 aux = &conflicts->aux[cnf];
1005 /* Update conflicts chain of the original symbol */
1006 aux->next_conflict = orig_aux->next_conflict;
1007 orig_aux->next_conflict = cnf;
1009 continue;
1012 /* Remove previous duplicates in the main table */
1013 if (out < i)
1015 t->syms[out] = *s;
1016 t->aux[out] = *aux;
1019 /* Put original into the hash table */
1020 *slot = &t->syms[out];
1021 out++;
1024 assert (conflicts->nsyms <= outlen);
1025 assert (conflicts->nsyms + out == t->nsyms);
1027 t->nsyms = out;
1028 htab_delete (symtab);
1031 /* Process one section of an object file. */
1033 static int
1034 process_symtab (void *data, const char *name, off_t offset, off_t length)
1036 struct plugin_objfile *obj = (struct plugin_objfile *)data;
1037 char *s;
1038 char *secdatastart, *secdata;
1040 if (!startswith (name, ".gnu.lto_.symtab"))
1041 return 1;
1043 s = strrchr (name, '.');
1044 if (s)
1045 sscanf (s, ".%" PRI_LL "x", &obj->out->id);
1046 secdata = secdatastart = xmalloc (length);
1047 offset += obj->file->offset;
1048 if (offset != lseek (obj->file->fd, offset, SEEK_SET))
1049 goto err;
1053 ssize_t got = read (obj->file->fd, secdata, length);
1054 if (got == 0)
1055 break;
1056 else if (got > 0)
1058 secdata += got;
1059 length -= got;
1061 else if (errno != EINTR)
1062 goto err;
1064 while (length > 0);
1065 if (length > 0)
1066 goto err;
1068 translate (secdatastart, secdata, obj->out);
1069 obj->found++;
1070 free (secdatastart);
1071 return 1;
1073 err:
1074 if (message)
1075 message (LDPL_FATAL, "%s: corrupt object file", obj->file->name);
1076 /* Force claim_file_handler to abandon this file. */
1077 obj->found = 0;
1078 free (secdatastart);
1079 return 0;
1082 /* Process one section of an object file. */
1084 static int
1085 process_symtab_extension (void *data, const char *name, off_t offset,
1086 off_t length)
1088 struct plugin_objfile *obj = (struct plugin_objfile *)data;
1089 char *s;
1090 char *secdatastart, *secdata;
1092 if (!startswith (name, ".gnu.lto_.ext_symtab"))
1093 return 1;
1095 s = strrchr (name, '.');
1096 if (s)
1097 sscanf (s, ".%" PRI_LL "x", &obj->out->id);
1098 secdata = secdatastart = xmalloc (length);
1099 offset += obj->file->offset;
1100 if (offset != lseek (obj->file->fd, offset, SEEK_SET))
1101 goto err;
1105 ssize_t got = read (obj->file->fd, secdata, length);
1106 if (got == 0)
1107 break;
1108 else if (got > 0)
1110 secdata += got;
1111 length -= got;
1113 else if (errno != EINTR)
1114 goto err;
1116 while (length > 0);
1117 if (length > 0)
1118 goto err;
1120 parse_symtab_extension (secdatastart, secdata, obj->out);
1121 obj->found++;
1122 free (secdatastart);
1123 return 1;
1125 err:
1126 if (message)
1127 message (LDPL_FATAL, "%s: corrupt object file", obj->file->name);
1128 /* Force claim_file_handler to abandon this file. */
1129 obj->found = 0;
1130 free (secdatastart);
1131 return 0;
1135 /* Find an offload section of an object file. */
1137 static int
1138 process_offload_section (void *data, const char *name, off_t offset, off_t len)
1140 if (startswith (name, ".gnu.offload_lto_.opts"))
1142 struct plugin_objfile *obj = (struct plugin_objfile *) data;
1143 obj->offload = true;
1144 return 0;
1147 return 1;
1150 /* Callback used by a linker to check if the plugin will claim FILE. Writes
1151 the result in CLAIMED. */
1153 static enum ld_plugin_status
1154 claim_file_handler (const struct ld_plugin_input_file *file, int *claimed)
1156 enum ld_plugin_status status;
1157 struct plugin_objfile obj;
1158 struct plugin_file_info lto_file;
1159 int err;
1160 const char *errmsg;
1162 memset (&lto_file, 0, sizeof (struct plugin_file_info));
1164 if (file->offset != 0)
1166 /* We pass the offset of the actual file, not the archive header.
1167 Can't use PRIx64, because that's C99, so we have to print the
1168 64-bit hex int as two 32-bit ones. Use xasprintf instead of
1169 asprintf because asprintf doesn't work as expected on some older
1170 mingw32 hosts. */
1171 int lo, hi;
1172 lo = file->offset & 0xffffffff;
1173 hi = ((int64_t)file->offset >> 32) & 0xffffffff;
1174 lto_file.name = hi ? xasprintf ("%s@0x%x%08x", file->name, hi, lo)
1175 : xasprintf ("%s@0x%x", file->name, lo);
1177 else
1179 lto_file.name = xstrdup (file->name);
1181 lto_file.handle = file->handle;
1183 *claimed = 0;
1184 obj.file = file;
1185 obj.found = 0;
1186 obj.offload = false;
1187 obj.out = &lto_file.symtab;
1188 errmsg = NULL;
1189 obj.objfile = simple_object_start_read (file->fd, file->offset, LTO_SEGMENT_NAME,
1190 &errmsg, &err);
1191 /* No file, but also no error code means unrecognized format; just skip it. */
1192 if (!obj.objfile && !err)
1193 goto err;
1195 if (obj.objfile)
1197 errmsg = simple_object_find_sections (obj.objfile, process_symtab, &obj,
1198 &err);
1199 /* Parsing symtab extension should be done only for add_symbols_v2 and
1200 later versions. */
1201 if (!errmsg && add_symbols_v2 != NULL)
1203 obj.out->last_sym = 0;
1204 errmsg = simple_object_find_sections (obj.objfile,
1205 process_symtab_extension,
1206 &obj, &err);
1210 if (!obj.objfile || errmsg)
1212 if (err && message)
1213 message (LDPL_FATAL, "%s: %s: %s", file->name, errmsg,
1214 xstrerror (err));
1215 else if (message)
1216 message (LDPL_FATAL, "%s: %s", file->name, errmsg);
1217 goto err;
1220 if (obj.objfile)
1221 simple_object_find_sections (obj.objfile, process_offload_section,
1222 &obj, &err);
1224 if (obj.found == 0 && !obj.offload)
1225 goto err;
1227 if (obj.found > 1)
1228 resolve_conflicts (&lto_file.symtab, &lto_file.conflicts);
1230 if (obj.found > 0)
1232 if (add_symbols_v2)
1233 status = add_symbols_v2 (file->handle, lto_file.symtab.nsyms,
1234 lto_file.symtab.syms);
1235 else
1236 status = add_symbols (file->handle, lto_file.symtab.nsyms,
1237 lto_file.symtab.syms);
1238 check (status == LDPS_OK, LDPL_FATAL, "could not add symbols");
1240 num_claimed_files++;
1241 claimed_files =
1242 xrealloc (claimed_files,
1243 num_claimed_files * sizeof (struct plugin_file_info));
1244 claimed_files[num_claimed_files - 1] = lto_file;
1246 *claimed = 1;
1249 if (offload_files == NULL)
1251 /* Add dummy item to the start of the list. */
1252 offload_files = xmalloc (sizeof (struct plugin_offload_file));
1253 offload_files->name = NULL;
1254 offload_files->next = NULL;
1255 offload_files_last = offload_files;
1258 /* If this is an LTO file without offload, and it is the first LTO file, save
1259 the pointer to the last offload file in the list. Further offload LTO
1260 files will be inserted after it, if any. */
1261 if (*claimed && !obj.offload && offload_files_last_lto == NULL)
1262 offload_files_last_lto = offload_files_last;
1264 if (obj.offload)
1266 /* Add file to the list. The order must be exactly the same as the final
1267 order after recompilation and linking, otherwise host and target tables
1268 with addresses wouldn't match. If a static library contains both LTO
1269 and non-LTO objects, ld and gold link them in a different order. */
1270 struct plugin_offload_file *ofld
1271 = xmalloc (sizeof (struct plugin_offload_file));
1272 ofld->name = lto_file.name;
1273 ofld->next = NULL;
1275 if (*claimed && offload_files_last_lto == NULL && file->offset != 0
1276 && gold_version == -1)
1278 /* ld only: insert first LTO file from the archive after the last real
1279 object file immediately preceding the archive, or at the begin of
1280 the list if there was no real objects before archives. */
1281 if (offload_files_last_obj != NULL)
1283 ofld->next = offload_files_last_obj->next;
1284 offload_files_last_obj->next = ofld;
1286 else
1288 ofld->next = offload_files->next;
1289 offload_files->next = ofld;
1292 else if (*claimed && offload_files_last_lto != NULL)
1294 /* Insert LTO file after the last LTO file in the list. */
1295 ofld->next = offload_files_last_lto->next;
1296 offload_files_last_lto->next = ofld;
1298 else
1299 /* Add non-LTO file or first non-archive LTO file to the end of the
1300 list. */
1301 offload_files_last->next = ofld;
1303 if (ofld->next == NULL)
1304 offload_files_last = ofld;
1305 if (file->offset == 0)
1306 offload_files_last_obj = ofld;
1307 if (*claimed)
1308 offload_files_last_lto = ofld;
1309 num_offload_files++;
1312 goto cleanup;
1314 err:
1315 non_claimed_files++;
1316 free (lto_file.name);
1318 cleanup:
1319 if (obj.objfile)
1320 simple_object_release_read (obj.objfile);
1322 return LDPS_OK;
1325 /* Parse the plugin options. */
1327 static void
1328 process_option (const char *option)
1330 if (strcmp (option, "-linker-output-known") == 0)
1331 linker_output_known = true;
1332 /* Also accept "notlo" for backwards compatibility. */
1333 else if ((strcmp (option, "-linker-output-auto-nolto-rel") == 0)
1334 || (strcmp (option, "-linker-output-auto-notlo-rel") == 0))
1335 linker_output_auto_nolto_rel = true;
1336 else if (strcmp (option, "-debug") == 0)
1337 debug = true;
1338 else if ((strcmp (option, "-v") == 0)
1339 || (strcmp (option, "--verbose") == 0))
1340 verbose = true;
1341 else if (strcmp (option, "-save-temps") == 0)
1342 save_temps = true;
1343 else if (strcmp (option, "-nop") == 0)
1344 nop = 1;
1345 else if (startswith (option, "-pass-through="))
1347 num_pass_through_items++;
1348 pass_through_items = xrealloc (pass_through_items,
1349 num_pass_through_items * sizeof (char *));
1350 pass_through_items[num_pass_through_items - 1] =
1351 xstrdup (option + strlen ("-pass-through="));
1353 else if (startswith (option, "-sym-style="))
1355 switch (option[sizeof ("-sym-style=") - 1])
1357 case 'w':
1358 sym_style = ss_win32;
1359 break;
1360 case 'u':
1361 sym_style = ss_uscore;
1362 break;
1363 default:
1364 sym_style = ss_none;
1365 break;
1368 else if (startswith (option, "-ltrans-objects="))
1369 ltrans_objects = xstrdup (option + strlen ("-ltrans-objects="));
1370 else
1372 int size;
1373 char *opt = xstrdup (option);
1374 lto_wrapper_num_args += 1;
1375 size = lto_wrapper_num_args * sizeof (char *);
1376 lto_wrapper_argv = (char **) xrealloc (lto_wrapper_argv, size);
1377 lto_wrapper_argv[lto_wrapper_num_args - 1] = opt;
1378 if (startswith (option, "-fresolution="))
1379 resolution_file = opt + sizeof ("-fresolution=") - 1;
1381 save_temps = save_temps || debug;
1382 verbose = verbose || debug;
1385 /* Called by a linker after loading the plugin. TV is the transfer vector. */
1387 enum ld_plugin_status
1388 onload (struct ld_plugin_tv *tv)
1390 struct ld_plugin_tv *p;
1391 enum ld_plugin_status status;
1393 p = tv;
1394 while (p->tv_tag)
1396 switch (p->tv_tag)
1398 case LDPT_MESSAGE:
1399 message = p->tv_u.tv_message;
1400 break;
1401 case LDPT_REGISTER_CLAIM_FILE_HOOK:
1402 register_claim_file = p->tv_u.tv_register_claim_file;
1403 break;
1404 case LDPT_ADD_SYMBOLS_V2:
1405 add_symbols_v2 = p->tv_u.tv_add_symbols;
1406 break;
1407 case LDPT_ADD_SYMBOLS:
1408 add_symbols = p->tv_u.tv_add_symbols;
1409 break;
1410 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
1411 register_all_symbols_read = p->tv_u.tv_register_all_symbols_read;
1412 break;
1413 case LDPT_GET_SYMBOLS_V2:
1414 get_symbols_v2 = p->tv_u.tv_get_symbols;
1415 break;
1416 case LDPT_GET_SYMBOLS:
1417 get_symbols = p->tv_u.tv_get_symbols;
1418 break;
1419 case LDPT_REGISTER_CLEANUP_HOOK:
1420 register_cleanup = p->tv_u.tv_register_cleanup;
1421 break;
1422 case LDPT_ADD_INPUT_FILE:
1423 add_input_file = p->tv_u.tv_add_input_file;
1424 break;
1425 case LDPT_ADD_INPUT_LIBRARY:
1426 add_input_library = p->tv_u.tv_add_input_library;
1427 break;
1428 case LDPT_OPTION:
1429 process_option (p->tv_u.tv_string);
1430 break;
1431 case LDPT_GOLD_VERSION:
1432 gold_version = p->tv_u.tv_val;
1433 break;
1434 case LDPT_LINKER_OUTPUT:
1435 linker_output = (enum ld_plugin_output_file_type) p->tv_u.tv_val;
1436 linker_output_set = true;
1437 break;
1438 case LDPT_OUTPUT_NAME:
1439 /* We only use this to make user-friendly temp file names. */
1440 link_output_name = p->tv_u.tv_string;
1441 break;
1442 default:
1443 break;
1445 p++;
1448 check (register_claim_file, LDPL_FATAL, "register_claim_file not found");
1449 check (add_symbols, LDPL_FATAL, "add_symbols not found");
1450 status = register_claim_file (claim_file_handler);
1451 check (status == LDPS_OK, LDPL_FATAL,
1452 "could not register the claim_file callback");
1454 if (register_cleanup)
1456 status = register_cleanup (cleanup_handler);
1457 check (status == LDPS_OK, LDPL_FATAL,
1458 "could not register the cleanup callback");
1461 if (register_all_symbols_read)
1463 check (get_symbols, LDPL_FATAL, "get_symbols not found");
1464 status = register_all_symbols_read (all_symbols_read_handler);
1465 check (status == LDPS_OK, LDPL_FATAL,
1466 "could not register the all_symbols_read callback");
1469 char *collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
1470 if (collect_gcc_options)
1472 /* Support -fno-use-linker-plugin by failing to load the plugin
1473 for the case where it is auto-loaded by BFD. */
1474 if (strstr (collect_gcc_options, "'-fno-use-linker-plugin'"))
1475 return LDPS_ERR;
1477 if (strstr (collect_gcc_options, "'-save-temps'"))
1478 save_temps = true;
1480 if (strstr (collect_gcc_options, "'-v'")
1481 || strstr (collect_gcc_options, "'--verbose'"))
1482 verbose = true;
1484 const char *p;
1485 if ((p = strstr (collect_gcc_options, "'-dumpdir'")))
1487 p += sizeof ("'-dumpdir'");
1488 while (*p == ' ')
1489 p++;
1490 const char *start = p;
1491 int ticks = 0, escapes = 0;
1492 /* Count ticks (') and escaped (\.) characters. Stop at the
1493 end of the options or at a blank after an even number of
1494 ticks (not counting escaped ones. */
1495 for (p = start; *p; p++)
1497 if (*p == '\'')
1499 ticks++;
1500 continue;
1502 else if ((ticks % 2) != 0)
1504 if (*p == ' ')
1505 break;
1506 if (*p == '\\')
1508 if (*++p)
1509 escapes++;
1510 else
1511 p--;
1516 /* Now allocate a new link_output_name and decode dumpdir
1517 into it. The loop uses the same logic, except it counts
1518 ticks and escapes backwards (so ticks is adjusted if we
1519 find an odd number of them), and it copies characters
1520 that are escaped or not otherwise skipped. */
1521 int len = p - start - ticks - escapes + 1;
1522 char *q = xmalloc (len);
1523 link_output_name = q;
1524 int oddticks = (ticks % 2);
1525 ticks += oddticks;
1526 for (p = start; *p; p++)
1528 if (*p == '\'')
1530 ticks--;
1531 continue;
1533 else if ((ticks % 2) != 0)
1535 if (*p == ' ')
1536 break;
1537 if (*p == '\\')
1539 if (*++p)
1540 escapes--;
1541 else
1542 p--;
1545 *q++ = *p;
1547 *q = '\0';
1548 assert (escapes == 0);
1549 assert (ticks == oddticks);
1550 assert (q - link_output_name == len - 1);
1551 skip_in_suffix = true;
1555 return LDPS_OK;