c++: Add test for C++23 narrowing conv to bool
[official-gcc.git] / lto-plugin / lto-plugin.c
blobff79f4ac462e82aa76569580b9a1ef521697dae6
1 /* LTO plugin for gold and/or GNU ld.
2 Copyright (C) 2009-2021 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 /* 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 int 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 bool debug;
194 static bool save_temps;
195 static bool verbose;
196 static char nop;
197 static char *resolution_file = NULL;
198 static enum ld_plugin_output_file_type linker_output;
199 static bool linker_output_set;
200 static bool linker_output_known;
201 static bool linker_output_auto_nolto_rel;
202 static const char *link_output_name = NULL;
204 /* This indicates link_output_name already contains the dot of the
205 suffix, so we can skip it in extensions. */
206 static int skip_in_suffix = 0;
208 /* The version of gold being used, or -1 if not gold. The number is
209 MAJOR * 100 + MINOR. */
210 static int gold_version = -1;
212 /* Not used by default, but can be overridden at runtime
213 by using -plugin-opt=-sym-style={none,win32,underscore|uscore}
214 (in fact, only first letter of style arg is checked.) */
215 static enum symbol_style sym_style = ss_none;
217 static void
218 check_1 (int gate, enum ld_plugin_level level, const char *text)
220 if (gate)
221 return;
223 if (message)
224 message (level, text);
225 else
227 /* If there is no nicer way to inform the user, fallback to stderr. */
228 fprintf (stderr, "%s\n", text);
229 if (level == LDPL_FATAL)
230 abort ();
234 /* This little wrapper allows check to be called with a non-integer
235 first argument, such as a pointer that must be non-NULL. We can't
236 use c99 bool type to coerce it into range, so we explicitly test. */
237 #define check(GATE, LEVEL, TEXT) check_1 (((GATE) != 0), (LEVEL), (TEXT))
239 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
240 by P and the result is written in ENTRY. The slot number is stored in SLOT.
241 Returns the address of the next entry. */
243 static char *
244 parse_table_entry (char *p, struct ld_plugin_symbol *entry,
245 struct sym_aux *aux)
247 unsigned char t;
248 enum ld_plugin_symbol_kind translate_kind[] =
250 LDPK_DEF,
251 LDPK_WEAKDEF,
252 LDPK_UNDEF,
253 LDPK_WEAKUNDEF,
254 LDPK_COMMON
257 enum ld_plugin_symbol_visibility translate_visibility[] =
259 LDPV_DEFAULT,
260 LDPV_PROTECTED,
261 LDPV_INTERNAL,
262 LDPV_HIDDEN
265 switch (sym_style)
267 case ss_win32:
268 if (p[0] == '@')
270 /* cf. Duff's device. */
271 case ss_none:
272 entry->name = xstrdup (p);
273 break;
275 /* FALL-THROUGH. */
276 case ss_uscore:
277 entry->name = concat ("_", p, NULL);
278 break;
279 default:
280 check (0, LDPL_FATAL, "invalid symbol style requested");
281 break;
283 while (*p)
284 p++;
285 p++;
287 entry->version = NULL;
289 entry->comdat_key = p;
290 while (*p)
291 p++;
292 p++;
294 if (strlen (entry->comdat_key) == 0)
295 entry->comdat_key = NULL;
296 else
297 entry->comdat_key = xstrdup (entry->comdat_key);
299 entry->unused = entry->section_kind = entry->symbol_type = 0;
301 t = *p;
302 check (t <= 4, LDPL_FATAL, "invalid symbol kind found");
303 entry->def = translate_kind[t];
304 p++;
306 t = *p;
307 check (t <= 3, LDPL_FATAL, "invalid symbol visibility found");
308 entry->visibility = translate_visibility[t];
309 p++;
311 memcpy (&entry->size, p, sizeof (uint64_t));
312 p += 8;
314 memcpy (&aux->slot, p, sizeof (uint32_t));
315 p += 4;
317 entry->resolution = LDPR_UNKNOWN;
319 aux->next_conflict = -1;
321 return p;
324 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
325 by P and the result is written in ENTRY. The slot number is stored in SLOT.
326 Returns the address of the next entry. */
328 static char *
329 parse_table_entry_extension (char *p, struct ld_plugin_symbol *entry)
331 unsigned char t;
332 enum ld_plugin_symbol_type symbol_types[] =
334 LDST_UNKNOWN,
335 LDST_FUNCTION,
336 LDST_VARIABLE,
339 t = *p;
340 check (t <= 2, LDPL_FATAL, "invalid symbol type found");
341 entry->symbol_type = symbol_types[t];
342 p++;
343 entry->section_kind = *p;
344 p++;
346 return p;
350 /* Translate the IL symbol table located between DATA and END. Append the
351 slots and symbols to OUT. */
353 static void
354 translate (char *data, char *end, struct plugin_symtab *out)
356 struct sym_aux *aux;
357 struct ld_plugin_symbol *syms = NULL;
358 int n, len;
360 /* This overestimates the output buffer sizes, but at least
361 the algorithm is O(1) now. */
363 len = (end - data)/8 + out->nsyms + 1;
364 syms = xrealloc (out->syms, len * sizeof (struct ld_plugin_symbol));
365 aux = xrealloc (out->aux, len * sizeof (struct sym_aux));
367 for (n = out->nsyms; data < end; n++)
369 aux[n].id = out->id;
370 data = parse_table_entry (data, &syms[n], &aux[n]);
373 assert(n < len);
375 out->nsyms = n;
376 out->syms = syms;
377 out->aux = aux;
380 static void
381 parse_symtab_extension (char *data, char *end, struct plugin_symtab *out)
383 unsigned long i;
384 unsigned char version;
386 if (data >= end)
387 /* FIXME: Issue an error ? */
388 return;
390 version = *data;
391 data++;
393 if (version != 1)
394 return;
396 /* Version 1 contains the following data per entry:
397 - symbol_type
398 - section_kind
399 . */
401 unsigned long nsyms = (end - data) / 2;
403 for (i = 0; i < nsyms; i++)
404 data = parse_table_entry_extension (data, out->syms + i + out->last_sym);
406 out->last_sym += nsyms;
409 /* Free all memory that is no longer needed after writing the symbol
410 resolution. */
412 static void
413 free_1 (struct plugin_file_info *files, unsigned num_files)
415 unsigned int i;
416 for (i = 0; i < num_files; i++)
418 struct plugin_file_info *info = &files[i];
419 struct plugin_symtab *symtab = &info->symtab;
420 unsigned int j;
421 for (j = 0; j < symtab->nsyms; j++)
423 struct ld_plugin_symbol *s = &symtab->syms[j];
424 free (s->name);
425 free (s->comdat_key);
427 free (symtab->syms);
428 symtab->syms = NULL;
432 /* Free all remaining memory. */
434 static void
435 free_2 (void)
437 unsigned int i;
438 for (i = 0; i < num_claimed_files; i++)
440 struct plugin_file_info *info = &claimed_files[i];
441 struct plugin_symtab *symtab = &info->symtab;
442 free (symtab->aux);
443 free (info->name);
446 for (i = 0; i < num_output_files; i++)
447 free (output_files[i]);
448 free (output_files);
450 free (claimed_files);
451 claimed_files = NULL;
452 num_claimed_files = 0;
454 while (offload_files)
456 struct plugin_offload_file *ofld = offload_files;
457 offload_files = offload_files->next;
458 free (ofld);
460 num_offload_files = 0;
462 free (arguments_file_name);
463 arguments_file_name = NULL;
466 /* Dump SYMTAB to resolution file F. */
468 static void
469 dump_symtab (FILE *f, struct plugin_symtab *symtab)
471 unsigned j;
473 for (j = 0; j < symtab->nsyms; j++)
475 uint32_t slot = symtab->aux[j].slot;
476 unsigned int resolution = symtab->syms[j].resolution;
478 assert (resolution != LDPR_UNKNOWN);
480 fprintf (f, "%u %" PRI_LL "x %s %s\n",
481 (unsigned int) slot, symtab->aux[j].id,
482 lto_resolution_str[resolution],
483 symtab->syms[j].name);
487 /* Finish the conflicts' resolution information after the linker resolved
488 the original symbols */
490 static void
491 finish_conflict_resolution (struct plugin_symtab *symtab,
492 struct plugin_symtab *conflicts)
494 int i, j;
496 if (conflicts->nsyms == 0)
497 return;
499 for (i = 0; i < symtab->nsyms; i++)
501 char resolution = LDPR_UNKNOWN;
503 if (symtab->aux[i].next_conflict == -1)
504 continue;
506 switch (symtab->syms[i].def)
508 case LDPK_DEF:
509 case LDPK_COMMON: /* ??? */
510 resolution = LDPR_RESOLVED_IR;
511 break;
512 case LDPK_WEAKDEF:
513 resolution = LDPR_PREEMPTED_IR;
514 break;
515 case LDPK_UNDEF:
516 case LDPK_WEAKUNDEF:
517 resolution = symtab->syms[i].resolution;
518 break;
519 default:
520 assert (0);
523 assert (resolution != LDPR_UNKNOWN);
525 for (j = symtab->aux[i].next_conflict;
526 j != -1;
527 j = conflicts->aux[j].next_conflict)
528 conflicts->syms[j].resolution = resolution;
532 /* Free symbol table SYMTAB. */
534 static void
535 free_symtab (struct plugin_symtab *symtab)
537 free (symtab->syms);
538 symtab->syms = NULL;
539 free (symtab->aux);
540 symtab->aux = NULL;
543 /* Writes the relocations to disk. */
545 static void
546 write_resolution (void)
548 unsigned int i;
549 FILE *f;
551 check (resolution_file, LDPL_FATAL, "resolution file not specified");
552 f = fopen (resolution_file, "w");
553 check (f, LDPL_FATAL, "could not open file");
555 fprintf (f, "%d\n", num_claimed_files);
557 for (i = 0; i < num_claimed_files; i++)
559 struct plugin_file_info *info = &claimed_files[i];
560 struct plugin_symtab *symtab = &info->symtab;
561 struct ld_plugin_symbol *syms = symtab->syms;
563 /* Version 2 of API supports IRONLY_EXP resolution that is
564 accepted by GCC-4.7 and newer. */
565 if (get_symbols_v2)
566 get_symbols_v2 (info->handle, symtab->nsyms, syms);
567 else
568 get_symbols (info->handle, symtab->nsyms, syms);
570 finish_conflict_resolution (symtab, &info->conflicts);
572 fprintf (f, "%s %d\n", info->name, symtab->nsyms + info->conflicts.nsyms);
573 dump_symtab (f, symtab);
574 if (info->conflicts.nsyms)
576 dump_symtab (f, &info->conflicts);
577 free_symtab (&info->conflicts);
580 fclose (f);
583 /* Pass files generated by the lto-wrapper to the linker. FD is lto-wrapper's
584 stdout. */
586 static void
587 add_output_files (FILE *f)
589 for (;;)
591 const unsigned piece = 32;
592 char *buf, *s = xmalloc (piece);
593 size_t len;
595 buf = s;
596 cont:
597 if (!fgets (buf, piece, f))
599 free (s);
600 break;
602 len = strlen (s);
603 if (s[len - 1] != '\n')
605 s = xrealloc (s, len + piece);
606 buf = s + len;
607 goto cont;
609 s[len - 1] = '\0';
611 num_output_files++;
612 output_files
613 = xrealloc (output_files, num_output_files * sizeof (char *));
614 output_files[num_output_files - 1] = s;
615 add_input_file (output_files[num_output_files - 1]);
619 /* Execute the lto-wrapper. ARGV[0] is the binary. The rest of ARGV is the
620 argument list. */
622 static void
623 exec_lto_wrapper (char *argv[])
625 int t, i;
626 int status;
627 char *at_args;
628 FILE *args;
629 FILE *wrapper_output;
630 char *new_argv[3];
631 struct pex_obj *pex;
632 const char *errmsg;
634 /* Write argv to a file to avoid a command line that is too long
635 Save the file locally on save-temps. */
636 const char *suffix = ".lto_wrapper_args";
637 suffix += skip_in_suffix;
638 if (save_temps && link_output_name)
639 arguments_file_name = concat (link_output_name, suffix, NULL);
640 else
641 arguments_file_name = make_temp_file (".lto_wrapper_args");
642 check (arguments_file_name, LDPL_FATAL,
643 "Failed to generate a temorary file name");
645 args = fopen (arguments_file_name, "w");
646 check (args, LDPL_FATAL, "could not open arguments file");
648 t = writeargv (&argv[1], args);
649 check (t == 0, LDPL_FATAL, "could not write arguments");
650 t = fclose (args);
651 check (t == 0, LDPL_FATAL, "could not close arguments file");
653 at_args = concat ("@", arguments_file_name, NULL);
654 check (at_args, LDPL_FATAL, "could not allocate");
656 for (i = 1; argv[i]; i++)
658 char *a = argv[i];
659 /* Check the input argument list for a verbose marker too. */
660 if (a[0] == '-' && a[1] == 'v' && a[2] == '\0')
662 verbose = true;
663 break;
667 if (verbose)
669 for (i = 0; argv[i]; i++)
670 fprintf (stderr, "%s ", argv[i]);
671 fprintf (stderr, "\n");
674 new_argv[0] = argv[0];
675 new_argv[1] = at_args;
676 new_argv[2] = NULL;
678 if (debug)
680 for (i = 0; new_argv[i]; i++)
681 fprintf (stderr, "%s ", new_argv[i]);
682 fprintf (stderr, "\n");
685 pex = pex_init (PEX_USE_PIPES, "lto-wrapper", NULL);
686 check (pex != NULL, LDPL_FATAL, "could not pex_init lto-wrapper");
688 errmsg = pex_run (pex, 0, new_argv[0], new_argv, NULL, NULL, &t);
689 check (errmsg == NULL, LDPL_FATAL, "could not run lto-wrapper");
690 check (t == 0, LDPL_FATAL, "could not run lto-wrapper");
692 wrapper_output = pex_read_output (pex, 0);
693 check (wrapper_output, LDPL_FATAL, "could not read lto-wrapper output");
695 add_output_files (wrapper_output);
697 t = pex_get_status (pex, 1, &status);
698 check (t == 1, LDPL_FATAL, "could not get lto-wrapper exit status");
699 check (WIFEXITED (status) && WEXITSTATUS (status) == 0, LDPL_FATAL,
700 "lto-wrapper failed");
702 pex_free (pex);
704 free (at_args);
707 /* Pass the original files back to the linker. */
709 static void
710 use_original_files (void)
712 unsigned i;
713 for (i = 0; i < num_claimed_files; i++)
715 struct plugin_file_info *info = &claimed_files[i];
716 add_input_file (info->name);
721 /* Called by the linker once all symbols have been read. */
723 static enum ld_plugin_status
724 all_symbols_read_handler (void)
726 const unsigned num_lto_args
727 = num_claimed_files + lto_wrapper_num_args + 2
728 + !linker_output_known + !linker_output_auto_nolto_rel;
729 unsigned i;
730 char **lto_argv;
731 const char *linker_output_str = NULL;
732 const char **lto_arg_ptr;
733 if (num_claimed_files + num_offload_files == 0)
734 return LDPS_OK;
736 if (nop)
738 use_original_files ();
739 return LDPS_OK;
742 lto_argv = (char **) xcalloc (sizeof (char *), num_lto_args);
743 lto_arg_ptr = (const char **) lto_argv;
744 assert (lto_wrapper_argv);
746 write_resolution ();
748 free_1 (claimed_files, num_claimed_files);
750 for (i = 0; i < lto_wrapper_num_args; i++)
751 *lto_arg_ptr++ = lto_wrapper_argv[i];
753 if (!linker_output_known)
755 assert (linker_output_set);
756 switch (linker_output)
758 case LDPO_REL:
759 if (non_claimed_files)
761 if (!linker_output_auto_nolto_rel)
762 message (LDPL_WARNING, "incremental linking of LTO and non-LTO"
763 " objects; using -flinker-output=nolto-rel which will"
764 " bypass whole program optimization");
765 linker_output_str = "-flinker-output=nolto-rel";
767 else
768 linker_output_str = "-flinker-output=rel";
769 break;
770 case LDPO_DYN:
771 linker_output_str = "-flinker-output=dyn";
772 break;
773 case LDPO_PIE:
774 linker_output_str = "-flinker-output=pie";
775 break;
776 case LDPO_EXEC:
777 linker_output_str = "-flinker-output=exec";
778 break;
779 default:
780 message (LDPL_FATAL, "unsupported linker output %i", linker_output);
781 break;
783 *lto_arg_ptr++ = xstrdup (linker_output_str);
786 if (num_offload_files > 0)
788 FILE *f;
789 char *arg;
790 char *offload_objects_file_name;
791 struct plugin_offload_file *ofld;
793 offload_objects_file_name = make_temp_file (".ofldlist");
794 check (offload_objects_file_name, LDPL_FATAL,
795 "Failed to generate a temporary file name");
796 f = fopen (offload_objects_file_name, "w");
797 check (f, LDPL_FATAL, "could not open file with offload objects");
798 fprintf (f, "%u\n", num_offload_files);
800 /* Skip the dummy item at the start of the list. */
801 ofld = offload_files->next;
802 while (ofld)
804 fprintf (f, "%s\n", ofld->name);
805 ofld = ofld->next;
807 fclose (f);
809 arg = concat ("-foffload-objects=", offload_objects_file_name, NULL);
810 check (arg, LDPL_FATAL, "could not allocate");
811 *lto_arg_ptr++ = arg;
814 for (i = 0; i < num_claimed_files; i++)
816 struct plugin_file_info *info = &claimed_files[i];
818 *lto_arg_ptr++ = info->name;
821 *lto_arg_ptr++ = NULL;
822 exec_lto_wrapper (lto_argv);
824 free (lto_argv);
826 /* --pass-through is not needed when using gold 1.11 or later. */
827 if (pass_through_items && gold_version < 111)
829 unsigned int i;
830 for (i = 0; i < num_pass_through_items; i++)
832 if (startswith (pass_through_items[i], "-l"))
833 add_input_library (pass_through_items[i] + 2);
834 else
835 add_input_file (pass_through_items[i]);
836 free (pass_through_items[i]);
837 pass_through_items[i] = NULL;
839 free (pass_through_items);
840 pass_through_items = NULL;
843 return LDPS_OK;
846 /* Helper, as used in collect2. */
847 static int
848 file_exists (const char *name)
850 return access (name, R_OK) == 0;
853 /* Unlink FILE unless we have save-temps set.
854 Note that we're saving files if verbose output is set. */
856 static void
857 maybe_unlink (const char *file)
859 if (save_temps && file_exists (file))
861 if (verbose)
862 fprintf (stderr, "[Leaving %s]\n", file);
863 return;
866 unlink_if_ordinary (file);
869 /* Remove temporary files at the end of the link. */
871 static enum ld_plugin_status
872 cleanup_handler (void)
874 unsigned int i;
876 if (debug)
877 return LDPS_OK;
879 if (arguments_file_name)
880 maybe_unlink (arguments_file_name);
882 for (i = 0; i < num_output_files; i++)
883 maybe_unlink (output_files[i]);
885 free_2 ();
886 return LDPS_OK;
889 #define SWAP(type, a, b) \
890 do { type tmp_; tmp_ = (a); (a) = (b); (b) = tmp_; } while(0)
892 /* Compare two hash table entries */
894 static int eq_sym (const void *a, const void *b)
896 const struct ld_plugin_symbol *as = (const struct ld_plugin_symbol *)a;
897 const struct ld_plugin_symbol *bs = (const struct ld_plugin_symbol *)b;
899 return !strcmp (as->name, bs->name);
902 /* Hash a symbol */
904 static hashval_t hash_sym (const void *a)
906 const struct ld_plugin_symbol *as = (const struct ld_plugin_symbol *)a;
908 return htab_hash_string (as->name);
911 /* Determine how strong a symbol is */
913 static int symbol_strength (struct ld_plugin_symbol *s)
915 switch (s->def)
917 case LDPK_UNDEF:
918 case LDPK_WEAKUNDEF:
919 return 0;
920 case LDPK_WEAKDEF:
921 return 1;
922 default:
923 return 2;
927 /* In the ld -r case we can get dups in the LTO symbol tables, where
928 the same symbol can have different resolutions (e.g. undefined and defined).
930 We have to keep that in the LTO symbol tables, but the dups confuse
931 gold and then finally gcc by supplying incorrect resolutions.
933 Problem is that the main gold symbol table doesn't know about subids
934 and does not distingush the same symbols in different states.
936 So we drop duplicates from the linker visible symbol table
937 and keep them in a private table. Then later do own symbol
938 resolution for the duplicated based on the results for the
939 originals.
941 Then when writing out the resolution file readd the dropped symbols.
943 XXX how to handle common? */
945 static void
946 resolve_conflicts (struct plugin_symtab *t, struct plugin_symtab *conflicts)
948 htab_t symtab = htab_create (t->nsyms, hash_sym, eq_sym, NULL);
949 int i;
950 int out;
951 int outlen;
953 outlen = t->nsyms;
954 conflicts->syms = xmalloc (sizeof (struct ld_plugin_symbol) * outlen);
955 conflicts->aux = xmalloc (sizeof (struct sym_aux) * outlen);
957 /* Move all duplicate symbols into the auxiliary conflicts table. */
958 out = 0;
959 for (i = 0; i < t->nsyms; i++)
961 struct ld_plugin_symbol *s = &t->syms[i];
962 struct sym_aux *aux = &t->aux[i];
963 void **slot;
965 slot = htab_find_slot (symtab, s, INSERT);
966 if (*slot != NULL)
968 int cnf;
969 struct ld_plugin_symbol *orig = (struct ld_plugin_symbol *)*slot;
970 struct sym_aux *orig_aux = &t->aux[orig - t->syms];
972 /* Always let the linker resolve the strongest symbol */
973 if (symbol_strength (orig) < symbol_strength (s))
975 SWAP (struct ld_plugin_symbol, *orig, *s);
976 SWAP (uint32_t, orig_aux->slot, aux->slot);
977 SWAP (unsigned long long, orig_aux->id, aux->id);
978 /* Don't swap conflict chain pointer */
981 /* Move current symbol into the conflicts table */
982 cnf = conflicts->nsyms++;
983 conflicts->syms[cnf] = *s;
984 conflicts->aux[cnf] = *aux;
985 aux = &conflicts->aux[cnf];
987 /* Update conflicts chain of the original symbol */
988 aux->next_conflict = orig_aux->next_conflict;
989 orig_aux->next_conflict = cnf;
991 continue;
994 /* Remove previous duplicates in the main table */
995 if (out < i)
997 t->syms[out] = *s;
998 t->aux[out] = *aux;
1001 /* Put original into the hash table */
1002 *slot = &t->syms[out];
1003 out++;
1006 assert (conflicts->nsyms <= outlen);
1007 assert (conflicts->nsyms + out == t->nsyms);
1009 t->nsyms = out;
1010 htab_delete (symtab);
1013 /* Process one section of an object file. */
1015 static int
1016 process_symtab (void *data, const char *name, off_t offset, off_t length)
1018 struct plugin_objfile *obj = (struct plugin_objfile *)data;
1019 char *s;
1020 char *secdatastart, *secdata;
1022 if (!startswith (name, ".gnu.lto_.symtab"))
1023 return 1;
1025 s = strrchr (name, '.');
1026 if (s)
1027 sscanf (s, ".%" PRI_LL "x", &obj->out->id);
1028 secdata = secdatastart = xmalloc (length);
1029 offset += obj->file->offset;
1030 if (offset != lseek (obj->file->fd, offset, SEEK_SET))
1031 goto err;
1035 ssize_t got = read (obj->file->fd, secdata, length);
1036 if (got == 0)
1037 break;
1038 else if (got > 0)
1040 secdata += got;
1041 length -= got;
1043 else if (errno != EINTR)
1044 goto err;
1046 while (length > 0);
1047 if (length > 0)
1048 goto err;
1050 translate (secdatastart, secdata, obj->out);
1051 obj->found++;
1052 free (secdatastart);
1053 return 1;
1055 err:
1056 if (message)
1057 message (LDPL_FATAL, "%s: corrupt object file", obj->file->name);
1058 /* Force claim_file_handler to abandon this file. */
1059 obj->found = 0;
1060 free (secdatastart);
1061 return 0;
1064 /* Process one section of an object file. */
1066 static int
1067 process_symtab_extension (void *data, const char *name, off_t offset,
1068 off_t length)
1070 struct plugin_objfile *obj = (struct plugin_objfile *)data;
1071 char *s;
1072 char *secdatastart, *secdata;
1074 if (!startswith (name, ".gnu.lto_.ext_symtab"))
1075 return 1;
1077 s = strrchr (name, '.');
1078 if (s)
1079 sscanf (s, ".%" PRI_LL "x", &obj->out->id);
1080 secdata = secdatastart = xmalloc (length);
1081 offset += obj->file->offset;
1082 if (offset != lseek (obj->file->fd, offset, SEEK_SET))
1083 goto err;
1087 ssize_t got = read (obj->file->fd, secdata, length);
1088 if (got == 0)
1089 break;
1090 else if (got > 0)
1092 secdata += got;
1093 length -= got;
1095 else if (errno != EINTR)
1096 goto err;
1098 while (length > 0);
1099 if (length > 0)
1100 goto err;
1102 parse_symtab_extension (secdatastart, secdata, obj->out);
1103 obj->found++;
1104 free (secdatastart);
1105 return 1;
1107 err:
1108 if (message)
1109 message (LDPL_FATAL, "%s: corrupt object file", obj->file->name);
1110 /* Force claim_file_handler to abandon this file. */
1111 obj->found = 0;
1112 free (secdatastart);
1113 return 0;
1117 /* Find an offload section of an object file. */
1119 static int
1120 process_offload_section (void *data, const char *name, off_t offset, off_t len)
1122 if (startswith (name, ".gnu.offload_lto_.opts"))
1124 struct plugin_objfile *obj = (struct plugin_objfile *) data;
1125 obj->offload = 1;
1126 return 0;
1129 return 1;
1132 /* Callback used by gold to check if the plugin will claim FILE. Writes
1133 the result in CLAIMED. */
1135 static enum ld_plugin_status
1136 claim_file_handler (const struct ld_plugin_input_file *file, int *claimed)
1138 enum ld_plugin_status status;
1139 struct plugin_objfile obj;
1140 struct plugin_file_info lto_file;
1141 int err;
1142 const char *errmsg;
1144 memset (&lto_file, 0, sizeof (struct plugin_file_info));
1146 if (file->offset != 0)
1148 /* We pass the offset of the actual file, not the archive header.
1149 Can't use PRIx64, because that's C99, so we have to print the
1150 64-bit hex int as two 32-bit ones. Use xasprintf instead of
1151 asprintf because asprintf doesn't work as expected on some older
1152 mingw32 hosts. */
1153 int lo, hi;
1154 lo = file->offset & 0xffffffff;
1155 hi = ((int64_t)file->offset >> 32) & 0xffffffff;
1156 lto_file.name = hi ? xasprintf ("%s@0x%x%08x", file->name, hi, lo)
1157 : xasprintf ("%s@0x%x", file->name, lo);
1159 else
1161 lto_file.name = xstrdup (file->name);
1163 lto_file.handle = file->handle;
1165 *claimed = 0;
1166 obj.file = file;
1167 obj.found = 0;
1168 obj.offload = 0;
1169 obj.out = &lto_file.symtab;
1170 errmsg = NULL;
1171 obj.objfile = simple_object_start_read (file->fd, file->offset, LTO_SEGMENT_NAME,
1172 &errmsg, &err);
1173 /* No file, but also no error code means unrecognized format; just skip it. */
1174 if (!obj.objfile && !err)
1175 goto err;
1177 if (obj.objfile)
1179 errmsg = simple_object_find_sections (obj.objfile, process_symtab, &obj,
1180 &err);
1181 /* Parsing symtab extension should be done only for add_symbols_v2 and
1182 later versions. */
1183 if (!errmsg && add_symbols_v2 != NULL)
1185 obj.out->last_sym = 0;
1186 errmsg = simple_object_find_sections (obj.objfile,
1187 process_symtab_extension,
1188 &obj, &err);
1192 if (!obj.objfile || errmsg)
1194 if (err && message)
1195 message (LDPL_FATAL, "%s: %s: %s", file->name, errmsg,
1196 xstrerror (err));
1197 else if (message)
1198 message (LDPL_FATAL, "%s: %s", file->name, errmsg);
1199 goto err;
1202 if (obj.objfile)
1203 simple_object_find_sections (obj.objfile, process_offload_section,
1204 &obj, &err);
1206 if (obj.found == 0 && obj.offload == 0)
1207 goto err;
1209 if (obj.found > 1)
1210 resolve_conflicts (&lto_file.symtab, &lto_file.conflicts);
1212 if (obj.found > 0)
1214 if (add_symbols_v2)
1215 status = add_symbols_v2 (file->handle, lto_file.symtab.nsyms,
1216 lto_file.symtab.syms);
1217 else
1218 status = add_symbols (file->handle, lto_file.symtab.nsyms,
1219 lto_file.symtab.syms);
1220 check (status == LDPS_OK, LDPL_FATAL, "could not add symbols");
1222 num_claimed_files++;
1223 claimed_files =
1224 xrealloc (claimed_files,
1225 num_claimed_files * sizeof (struct plugin_file_info));
1226 claimed_files[num_claimed_files - 1] = lto_file;
1228 *claimed = 1;
1231 if (offload_files == NULL)
1233 /* Add dummy item to the start of the list. */
1234 offload_files = xmalloc (sizeof (struct plugin_offload_file));
1235 offload_files->name = NULL;
1236 offload_files->next = NULL;
1237 offload_files_last = offload_files;
1240 /* If this is an LTO file without offload, and it is the first LTO file, save
1241 the pointer to the last offload file in the list. Further offload LTO
1242 files will be inserted after it, if any. */
1243 if (*claimed && obj.offload == 0 && offload_files_last_lto == NULL)
1244 offload_files_last_lto = offload_files_last;
1246 if (obj.offload == 1)
1248 /* Add file to the list. The order must be exactly the same as the final
1249 order after recompilation and linking, otherwise host and target tables
1250 with addresses wouldn't match. If a static library contains both LTO
1251 and non-LTO objects, ld and gold link them in a different order. */
1252 struct plugin_offload_file *ofld
1253 = xmalloc (sizeof (struct plugin_offload_file));
1254 ofld->name = lto_file.name;
1255 ofld->next = NULL;
1257 if (*claimed && offload_files_last_lto == NULL && file->offset != 0
1258 && gold_version == -1)
1260 /* ld only: insert first LTO file from the archive after the last real
1261 object file immediately preceding the archive, or at the begin of
1262 the list if there was no real objects before archives. */
1263 if (offload_files_last_obj != NULL)
1265 ofld->next = offload_files_last_obj->next;
1266 offload_files_last_obj->next = ofld;
1268 else
1270 ofld->next = offload_files->next;
1271 offload_files->next = ofld;
1274 else if (*claimed && offload_files_last_lto != NULL)
1276 /* Insert LTO file after the last LTO file in the list. */
1277 ofld->next = offload_files_last_lto->next;
1278 offload_files_last_lto->next = ofld;
1280 else
1281 /* Add non-LTO file or first non-archive LTO file to the end of the
1282 list. */
1283 offload_files_last->next = ofld;
1285 if (ofld->next == NULL)
1286 offload_files_last = ofld;
1287 if (file->offset == 0)
1288 offload_files_last_obj = ofld;
1289 if (*claimed)
1290 offload_files_last_lto = ofld;
1291 num_offload_files++;
1294 goto cleanup;
1296 err:
1297 non_claimed_files++;
1298 free (lto_file.name);
1300 cleanup:
1301 if (obj.objfile)
1302 simple_object_release_read (obj.objfile);
1304 return LDPS_OK;
1307 /* Parse the plugin options. */
1309 static void
1310 process_option (const char *option)
1312 if (strcmp (option, "-linker-output-known") == 0)
1313 linker_output_known = true;
1314 else if (strcmp (option, "-linker-output-auto-notlo-rel") == 0)
1315 linker_output_auto_nolto_rel = true;
1316 else if (strcmp (option, "-debug") == 0)
1317 debug = true;
1318 else if ((strcmp (option, "-v") == 0)
1319 || (strcmp (option, "--verbose") == 0))
1320 verbose = true;
1321 else if (strcmp (option, "-save-temps") == 0)
1322 save_temps = true;
1323 else if (strcmp (option, "-nop") == 0)
1324 nop = 1;
1325 else if (startswith (option, "-pass-through="))
1327 num_pass_through_items++;
1328 pass_through_items = xrealloc (pass_through_items,
1329 num_pass_through_items * sizeof (char *));
1330 pass_through_items[num_pass_through_items - 1] =
1331 xstrdup (option + strlen ("-pass-through="));
1333 else if (startswith (option, "-sym-style="))
1335 switch (option[sizeof ("-sym-style=") - 1])
1337 case 'w':
1338 sym_style = ss_win32;
1339 break;
1340 case 'u':
1341 sym_style = ss_uscore;
1342 break;
1343 default:
1344 sym_style = ss_none;
1345 break;
1348 else
1350 int size;
1351 char *opt = xstrdup (option);
1352 lto_wrapper_num_args += 1;
1353 size = lto_wrapper_num_args * sizeof (char *);
1354 lto_wrapper_argv = (char **) xrealloc (lto_wrapper_argv, size);
1355 lto_wrapper_argv[lto_wrapper_num_args - 1] = opt;
1356 if (startswith (option, "-fresolution="))
1357 resolution_file = opt + sizeof ("-fresolution=") - 1;
1359 save_temps = save_temps || debug;
1360 verbose = verbose || debug;
1363 /* Called by gold after loading the plugin. TV is the transfer vector. */
1365 enum ld_plugin_status
1366 onload (struct ld_plugin_tv *tv)
1368 struct ld_plugin_tv *p;
1369 enum ld_plugin_status status;
1371 p = tv;
1372 while (p->tv_tag)
1374 switch (p->tv_tag)
1376 case LDPT_MESSAGE:
1377 message = p->tv_u.tv_message;
1378 break;
1379 case LDPT_REGISTER_CLAIM_FILE_HOOK:
1380 register_claim_file = p->tv_u.tv_register_claim_file;
1381 break;
1382 case LDPT_ADD_SYMBOLS_V2:
1383 add_symbols_v2 = p->tv_u.tv_add_symbols;
1384 break;
1385 case LDPT_ADD_SYMBOLS:
1386 add_symbols = p->tv_u.tv_add_symbols;
1387 break;
1388 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
1389 register_all_symbols_read = p->tv_u.tv_register_all_symbols_read;
1390 break;
1391 case LDPT_GET_SYMBOLS_V2:
1392 get_symbols_v2 = p->tv_u.tv_get_symbols;
1393 break;
1394 case LDPT_GET_SYMBOLS:
1395 get_symbols = p->tv_u.tv_get_symbols;
1396 break;
1397 case LDPT_REGISTER_CLEANUP_HOOK:
1398 register_cleanup = p->tv_u.tv_register_cleanup;
1399 break;
1400 case LDPT_ADD_INPUT_FILE:
1401 add_input_file = p->tv_u.tv_add_input_file;
1402 break;
1403 case LDPT_ADD_INPUT_LIBRARY:
1404 add_input_library = p->tv_u.tv_add_input_library;
1405 break;
1406 case LDPT_OPTION:
1407 process_option (p->tv_u.tv_string);
1408 break;
1409 case LDPT_GOLD_VERSION:
1410 gold_version = p->tv_u.tv_val;
1411 break;
1412 case LDPT_LINKER_OUTPUT:
1413 linker_output = (enum ld_plugin_output_file_type) p->tv_u.tv_val;
1414 linker_output_set = true;
1415 break;
1416 case LDPT_OUTPUT_NAME:
1417 /* We only use this to make user-friendly temp file names. */
1418 link_output_name = p->tv_u.tv_string;
1419 break;
1420 default:
1421 break;
1423 p++;
1426 check (register_claim_file, LDPL_FATAL, "register_claim_file not found");
1427 check (add_symbols, LDPL_FATAL, "add_symbols not found");
1428 status = register_claim_file (claim_file_handler);
1429 check (status == LDPS_OK, LDPL_FATAL,
1430 "could not register the claim_file callback");
1432 if (register_cleanup)
1434 status = register_cleanup (cleanup_handler);
1435 check (status == LDPS_OK, LDPL_FATAL,
1436 "could not register the cleanup callback");
1439 if (register_all_symbols_read)
1441 check (get_symbols, LDPL_FATAL, "get_symbols not found");
1442 status = register_all_symbols_read (all_symbols_read_handler);
1443 check (status == LDPS_OK, LDPL_FATAL,
1444 "could not register the all_symbols_read callback");
1447 char *collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
1448 if (collect_gcc_options)
1450 /* Support -fno-use-linker-plugin by failing to load the plugin
1451 for the case where it is auto-loaded by BFD. */
1452 if (strstr (collect_gcc_options, "'-fno-use-linker-plugin'"))
1453 return LDPS_ERR;
1455 if (strstr (collect_gcc_options, "'-save-temps'"))
1456 save_temps = true;
1458 if (strstr (collect_gcc_options, "'-v'")
1459 || strstr (collect_gcc_options, "'--verbose'"))
1460 verbose = true;
1462 const char *p;
1463 if ((p = strstr (collect_gcc_options, "'-dumpdir'")))
1465 p += sizeof ("'-dumpdir'");
1466 while (*p == ' ')
1467 p++;
1468 const char *start = p;
1469 int ticks = 0, escapes = 0;
1470 /* Count ticks (') and escaped (\.) characters. Stop at the
1471 end of the options or at a blank after an even number of
1472 ticks (not counting escaped ones. */
1473 for (p = start; *p; p++)
1475 if (*p == '\'')
1477 ticks++;
1478 continue;
1480 else if ((ticks % 2) != 0)
1482 if (*p == ' ')
1483 break;
1484 if (*p == '\\')
1486 if (*++p)
1487 escapes++;
1488 else
1489 p--;
1494 /* Now allocate a new link_output_name and decode dumpdir
1495 into it. The loop uses the same logic, except it counts
1496 ticks and escapes backwards (so ticks is adjusted if we
1497 find an odd number of them), and it copies characters
1498 that are escaped or not otherwise skipped. */
1499 int len = p - start - ticks - escapes + 1;
1500 char *q = xmalloc (len);
1501 link_output_name = q;
1502 int oddticks = (ticks % 2);
1503 ticks += oddticks;
1504 for (p = start; *p; p++)
1506 if (*p == '\'')
1508 ticks--;
1509 continue;
1511 else if ((ticks % 2) != 0)
1513 if (*p == ' ')
1514 break;
1515 if (*p == '\\')
1517 if (*++p)
1518 escapes--;
1519 else
1520 p--;
1523 *q++ = *p;
1525 *q = '\0';
1526 assert (escapes == 0);
1527 assert (ticks == oddticks);
1528 assert (q - link_output_name == len - 1);
1529 skip_in_suffix = 1;
1533 return LDPS_OK;