Delete file meant for private branch
[official-gcc.git] / lto-plugin / lto-plugin.c
blob37f4bda742a37181584cd51d02a84f8d575dd5e7
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 struct sym_aux *aux;
119 struct ld_plugin_symbol *syms;
120 unsigned long long id;
123 /* Encapsulates object file data during symbol scan. */
124 struct plugin_objfile
126 int found;
127 int offload;
128 simple_object_read *objfile;
129 struct plugin_symtab *out;
130 const struct ld_plugin_input_file *file;
133 /* All that we have to remember about a file. */
135 struct plugin_file_info
137 char *name;
138 void *handle;
139 struct plugin_symtab symtab;
140 struct plugin_symtab conflicts;
143 /* List item with name of the file with offloading. */
145 struct plugin_offload_file
147 char *name;
148 struct plugin_offload_file *next;
151 /* Until ASM_OUTPUT_LABELREF can be hookized and decoupled from
152 stdio file streams, we do simple label translation here. */
154 enum symbol_style
156 ss_none, /* No underscore prefix. */
157 ss_win32, /* Underscore prefix any symbol not beginning with '@'. */
158 ss_uscore, /* Underscore prefix all symbols. */
161 static char *arguments_file_name;
162 static ld_plugin_register_claim_file register_claim_file;
163 static ld_plugin_register_all_symbols_read register_all_symbols_read;
164 static ld_plugin_get_symbols get_symbols, get_symbols_v2;
165 static ld_plugin_register_cleanup register_cleanup;
166 static ld_plugin_add_input_file add_input_file;
167 static ld_plugin_add_input_library add_input_library;
168 static ld_plugin_message message;
169 static ld_plugin_add_symbols add_symbols, add_symbols_v2;
171 static struct plugin_file_info *claimed_files = NULL;
172 static unsigned int num_claimed_files = 0;
173 static unsigned int non_claimed_files = 0;
175 /* List of files with offloading. */
176 static struct plugin_offload_file *offload_files;
177 /* Last file in the list. */
178 static struct plugin_offload_file *offload_files_last;
179 /* Last non-archive file in the list. */
180 static struct plugin_offload_file *offload_files_last_obj;
181 /* Last LTO file in the list. */
182 static struct plugin_offload_file *offload_files_last_lto;
183 /* Total number of files with offloading. */
184 static unsigned num_offload_files;
186 static char **output_files = NULL;
187 static unsigned int num_output_files = 0;
189 static char **lto_wrapper_argv;
190 static int lto_wrapper_num_args;
192 static char **pass_through_items = NULL;
193 static unsigned int num_pass_through_items;
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 /* The version of gold being used, or -1 if not gold. The number is
207 MAJOR * 100 + MINOR. */
208 static int gold_version = -1;
210 /* Not used by default, but can be overridden at runtime
211 by using -plugin-opt=-sym-style={none,win32,underscore|uscore}
212 (in fact, only first letter of style arg is checked.) */
213 static enum symbol_style sym_style = ss_none;
215 static void
216 check_1 (int gate, enum ld_plugin_level level, const char *text)
218 if (gate)
219 return;
221 if (message)
222 message (level, text);
223 else
225 /* If there is no nicer way to inform the user, fallback to stderr. */
226 fprintf (stderr, "%s\n", text);
227 if (level == LDPL_FATAL)
228 abort ();
232 /* This little wrapper allows check to be called with a non-integer
233 first argument, such as a pointer that must be non-NULL. We can't
234 use c99 bool type to coerce it into range, so we explicitly test. */
235 #define check(GATE, LEVEL, TEXT) check_1 (((GATE) != 0), (LEVEL), (TEXT))
237 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
238 by P and the result is written in ENTRY. The slot number is stored in SLOT.
239 Returns the address of the next entry. */
241 static char *
242 parse_table_entry (char *p, struct ld_plugin_symbol *entry,
243 struct sym_aux *aux)
245 unsigned char t;
246 enum ld_plugin_symbol_kind translate_kind[] =
248 LDPK_DEF,
249 LDPK_WEAKDEF,
250 LDPK_UNDEF,
251 LDPK_WEAKUNDEF,
252 LDPK_COMMON
255 enum ld_plugin_symbol_visibility translate_visibility[] =
257 LDPV_DEFAULT,
258 LDPV_PROTECTED,
259 LDPV_INTERNAL,
260 LDPV_HIDDEN
263 switch (sym_style)
265 case ss_win32:
266 if (p[0] == '@')
268 /* cf. Duff's device. */
269 case ss_none:
270 entry->name = xstrdup (p);
271 break;
273 /* FALL-THROUGH. */
274 case ss_uscore:
275 entry->name = concat ("_", p, NULL);
276 break;
277 default:
278 check (0, LDPL_FATAL, "invalid symbol style requested");
279 break;
281 while (*p)
282 p++;
283 p++;
285 entry->version = NULL;
287 entry->comdat_key = p;
288 while (*p)
289 p++;
290 p++;
292 if (strlen (entry->comdat_key) == 0)
293 entry->comdat_key = NULL;
294 else
295 entry->comdat_key = xstrdup (entry->comdat_key);
297 entry->unused = entry->section_kind = entry->symbol_type = 0;
299 t = *p;
300 check (t <= 4, LDPL_FATAL, "invalid symbol kind found");
301 entry->def = translate_kind[t];
302 p++;
304 t = *p;
305 check (t <= 3, LDPL_FATAL, "invalid symbol visibility found");
306 entry->visibility = translate_visibility[t];
307 p++;
309 memcpy (&entry->size, p, sizeof (uint64_t));
310 p += 8;
312 memcpy (&aux->slot, p, sizeof (uint32_t));
313 p += 4;
315 entry->resolution = LDPR_UNKNOWN;
317 aux->next_conflict = -1;
319 return p;
322 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
323 by P and the result is written in ENTRY. The slot number is stored in SLOT.
324 Returns the address of the next entry. */
326 static char *
327 parse_table_entry_extension (char *p, struct ld_plugin_symbol *entry)
329 unsigned char t;
330 enum ld_plugin_symbol_type symbol_types[] =
332 LDST_UNKNOWN,
333 LDST_FUNCTION,
334 LDST_VARIABLE,
337 t = *p;
338 check (t <= 2, LDPL_FATAL, "invalid symbol type found");
339 entry->symbol_type = symbol_types[t];
340 p++;
341 entry->section_kind = *p;
342 p++;
344 return p;
348 /* Translate the IL symbol table located between DATA and END. Append the
349 slots and symbols to OUT. */
351 static void
352 translate (char *data, char *end, struct plugin_symtab *out)
354 struct sym_aux *aux;
355 struct ld_plugin_symbol *syms = NULL;
356 int n, len;
358 /* This overestimates the output buffer sizes, but at least
359 the algorithm is O(1) now. */
361 len = (end - data)/8 + out->nsyms + 1;
362 syms = xrealloc (out->syms, len * sizeof (struct ld_plugin_symbol));
363 aux = xrealloc (out->aux, len * sizeof (struct sym_aux));
365 for (n = out->nsyms; data < end; n++)
367 aux[n].id = out->id;
368 data = parse_table_entry (data, &syms[n], &aux[n]);
371 assert(n < len);
373 out->nsyms = n;
374 out->syms = syms;
375 out->aux = aux;
378 static void
379 parse_symtab_extension (char *data, char *end, struct plugin_symtab *out)
381 unsigned i;
383 unsigned char version = *data;
384 data++;
386 /* Version 1 contains the following data per entry:
387 - symbol_type
388 - section_kind
389 . */
391 if (version == 1)
392 for (i = 0; i < out->nsyms; i++)
393 data = parse_table_entry_extension (data, &out->syms[i]);
396 /* Free all memory that is no longer needed after writing the symbol
397 resolution. */
399 static void
400 free_1 (struct plugin_file_info *files, unsigned num_files)
402 unsigned int i;
403 for (i = 0; i < num_files; i++)
405 struct plugin_file_info *info = &files[i];
406 struct plugin_symtab *symtab = &info->symtab;
407 unsigned int j;
408 for (j = 0; j < symtab->nsyms; j++)
410 struct ld_plugin_symbol *s = &symtab->syms[j];
411 free (s->name);
412 free (s->comdat_key);
414 free (symtab->syms);
415 symtab->syms = NULL;
419 /* Free all remaining memory. */
421 static void
422 free_2 (void)
424 unsigned int i;
425 for (i = 0; i < num_claimed_files; i++)
427 struct plugin_file_info *info = &claimed_files[i];
428 struct plugin_symtab *symtab = &info->symtab;
429 free (symtab->aux);
430 free (info->name);
433 for (i = 0; i < num_output_files; i++)
434 free (output_files[i]);
435 free (output_files);
437 free (claimed_files);
438 claimed_files = NULL;
439 num_claimed_files = 0;
441 while (offload_files)
443 struct plugin_offload_file *ofld = offload_files;
444 offload_files = offload_files->next;
445 free (ofld);
447 num_offload_files = 0;
449 free (arguments_file_name);
450 arguments_file_name = NULL;
453 /* Dump SYMTAB to resolution file F. */
455 static void
456 dump_symtab (FILE *f, struct plugin_symtab *symtab)
458 unsigned j;
460 for (j = 0; j < symtab->nsyms; j++)
462 uint32_t slot = symtab->aux[j].slot;
463 unsigned int resolution = symtab->syms[j].resolution;
465 assert (resolution != LDPR_UNKNOWN);
467 fprintf (f, "%u %" PRI_LL "x %s %s\n",
468 (unsigned int) slot, symtab->aux[j].id,
469 lto_resolution_str[resolution],
470 symtab->syms[j].name);
474 /* Finish the conflicts' resolution information after the linker resolved
475 the original symbols */
477 static void
478 finish_conflict_resolution (struct plugin_symtab *symtab,
479 struct plugin_symtab *conflicts)
481 int i, j;
483 if (conflicts->nsyms == 0)
484 return;
486 for (i = 0; i < symtab->nsyms; i++)
488 char resolution = LDPR_UNKNOWN;
490 if (symtab->aux[i].next_conflict == -1)
491 continue;
493 switch (symtab->syms[i].def)
495 case LDPK_DEF:
496 case LDPK_COMMON: /* ??? */
497 resolution = LDPR_RESOLVED_IR;
498 break;
499 case LDPK_WEAKDEF:
500 resolution = LDPR_PREEMPTED_IR;
501 break;
502 case LDPK_UNDEF:
503 case LDPK_WEAKUNDEF:
504 resolution = symtab->syms[i].resolution;
505 break;
506 default:
507 assert (0);
510 assert (resolution != LDPR_UNKNOWN);
512 for (j = symtab->aux[i].next_conflict;
513 j != -1;
514 j = conflicts->aux[j].next_conflict)
515 conflicts->syms[j].resolution = resolution;
519 /* Free symbol table SYMTAB. */
521 static void
522 free_symtab (struct plugin_symtab *symtab)
524 free (symtab->syms);
525 symtab->syms = NULL;
526 free (symtab->aux);
527 symtab->aux = NULL;
530 /* Writes the relocations to disk. */
532 static void
533 write_resolution (void)
535 unsigned int i;
536 FILE *f;
538 check (resolution_file, LDPL_FATAL, "resolution file not specified");
539 f = fopen (resolution_file, "w");
540 check (f, LDPL_FATAL, "could not open file");
542 fprintf (f, "%d\n", num_claimed_files);
544 for (i = 0; i < num_claimed_files; i++)
546 struct plugin_file_info *info = &claimed_files[i];
547 struct plugin_symtab *symtab = &info->symtab;
548 struct ld_plugin_symbol *syms = symtab->syms;
550 /* Version 2 of API supports IRONLY_EXP resolution that is
551 accepted by GCC-4.7 and newer. */
552 if (get_symbols_v2)
553 get_symbols_v2 (info->handle, symtab->nsyms, syms);
554 else
555 get_symbols (info->handle, symtab->nsyms, syms);
557 finish_conflict_resolution (symtab, &info->conflicts);
559 fprintf (f, "%s %d\n", info->name, symtab->nsyms + info->conflicts.nsyms);
560 dump_symtab (f, symtab);
561 if (info->conflicts.nsyms)
563 dump_symtab (f, &info->conflicts);
564 free_symtab (&info->conflicts);
567 fclose (f);
570 /* Pass files generated by the lto-wrapper to the linker. FD is lto-wrapper's
571 stdout. */
573 static void
574 add_output_files (FILE *f)
576 for (;;)
578 const unsigned piece = 32;
579 char *buf, *s = xmalloc (piece);
580 size_t len;
582 buf = s;
583 cont:
584 if (!fgets (buf, piece, f))
586 free (s);
587 break;
589 len = strlen (s);
590 if (s[len - 1] != '\n')
592 s = xrealloc (s, len + piece);
593 buf = s + len;
594 goto cont;
596 s[len - 1] = '\0';
598 num_output_files++;
599 output_files
600 = xrealloc (output_files, num_output_files * sizeof (char *));
601 output_files[num_output_files - 1] = s;
602 add_input_file (output_files[num_output_files - 1]);
606 /* Execute the lto-wrapper. ARGV[0] is the binary. The rest of ARGV is the
607 argument list. */
609 static void
610 exec_lto_wrapper (char *argv[])
612 int t, i;
613 int status;
614 char *at_args;
615 FILE *args;
616 FILE *wrapper_output;
617 char *new_argv[3];
618 struct pex_obj *pex;
619 const char *errmsg;
621 /* Write argv to a file to avoid a command line that is too long
622 Save the file locally on save-temps. */
623 if (save_temps && link_output_name)
625 arguments_file_name = (char *) xmalloc (strlen (link_output_name)
626 + sizeof (".lto_wrapper_args") + 1);
627 strcpy (arguments_file_name, link_output_name);
628 strcat (arguments_file_name, ".lto_wrapper_args");
630 else
631 arguments_file_name = make_temp_file (".lto_wrapper_args");
632 check (arguments_file_name, LDPL_FATAL,
633 "Failed to generate a temorary file name");
635 args = fopen (arguments_file_name, "w");
636 check (args, LDPL_FATAL, "could not open arguments file");
638 t = writeargv (&argv[1], args);
639 check (t == 0, LDPL_FATAL, "could not write arguments");
640 t = fclose (args);
641 check (t == 0, LDPL_FATAL, "could not close arguments file");
643 at_args = concat ("@", arguments_file_name, NULL);
644 check (at_args, LDPL_FATAL, "could not allocate");
646 for (i = 1; argv[i]; i++)
648 char *a = argv[i];
649 /* Check the input argument list for a verbose marker too. */
650 if (a[0] == '-' && a[1] == 'v' && a[2] == '\0')
652 verbose = true;
653 break;
657 if (verbose)
659 for (i = 0; argv[i]; i++)
660 fprintf (stderr, "%s ", argv[i]);
661 fprintf (stderr, "\n");
664 new_argv[0] = argv[0];
665 new_argv[1] = at_args;
666 new_argv[2] = NULL;
668 if (debug)
670 for (i = 0; new_argv[i]; i++)
671 fprintf (stderr, "%s ", new_argv[i]);
672 fprintf (stderr, "\n");
675 pex = pex_init (PEX_USE_PIPES, "lto-wrapper", NULL);
676 check (pex != NULL, LDPL_FATAL, "could not pex_init lto-wrapper");
678 errmsg = pex_run (pex, 0, new_argv[0], new_argv, NULL, NULL, &t);
679 check (errmsg == NULL, LDPL_FATAL, "could not run lto-wrapper");
680 check (t == 0, LDPL_FATAL, "could not run lto-wrapper");
682 wrapper_output = pex_read_output (pex, 0);
683 check (wrapper_output, LDPL_FATAL, "could not read lto-wrapper output");
685 add_output_files (wrapper_output);
687 t = pex_get_status (pex, 1, &status);
688 check (t == 1, LDPL_FATAL, "could not get lto-wrapper exit status");
689 check (WIFEXITED (status) && WEXITSTATUS (status) == 0, LDPL_FATAL,
690 "lto-wrapper failed");
692 pex_free (pex);
694 free (at_args);
697 /* Pass the original files back to the linker. */
699 static void
700 use_original_files (void)
702 unsigned i;
703 for (i = 0; i < num_claimed_files; i++)
705 struct plugin_file_info *info = &claimed_files[i];
706 add_input_file (info->name);
711 /* Called by the linker once all symbols have been read. */
713 static enum ld_plugin_status
714 all_symbols_read_handler (void)
716 const unsigned num_lto_args
717 = num_claimed_files + lto_wrapper_num_args + 2
718 + !linker_output_known + !linker_output_auto_nolto_rel;
719 unsigned i;
720 char **lto_argv;
721 const char *linker_output_str = NULL;
722 const char **lto_arg_ptr;
723 if (num_claimed_files + num_offload_files == 0)
724 return LDPS_OK;
726 if (nop)
728 use_original_files ();
729 return LDPS_OK;
732 lto_argv = (char **) xcalloc (sizeof (char *), num_lto_args);
733 lto_arg_ptr = (const char **) lto_argv;
734 assert (lto_wrapper_argv);
736 write_resolution ();
738 free_1 (claimed_files, num_claimed_files);
740 for (i = 0; i < lto_wrapper_num_args; i++)
741 *lto_arg_ptr++ = lto_wrapper_argv[i];
743 if (!linker_output_known)
745 assert (linker_output_set);
746 switch (linker_output)
748 case LDPO_REL:
749 if (non_claimed_files)
751 if (!linker_output_auto_nolto_rel)
752 message (LDPL_WARNING, "incremental linking of LTO and non-LTO"
753 " objects; using -flinker-output=nolto-rel which will"
754 " bypass whole program optimization");
755 linker_output_str = "-flinker-output=nolto-rel";
757 else
758 linker_output_str = "-flinker-output=rel";
759 break;
760 case LDPO_DYN:
761 linker_output_str = "-flinker-output=dyn";
762 break;
763 case LDPO_PIE:
764 linker_output_str = "-flinker-output=pie";
765 break;
766 case LDPO_EXEC:
767 linker_output_str = "-flinker-output=exec";
768 break;
769 default:
770 message (LDPL_FATAL, "unsupported linker output %i", linker_output);
771 break;
773 *lto_arg_ptr++ = xstrdup (linker_output_str);
776 if (num_offload_files > 0)
778 FILE *f;
779 char *arg;
780 char *offload_objects_file_name;
781 struct plugin_offload_file *ofld;
783 offload_objects_file_name = make_temp_file (".ofldlist");
784 check (offload_objects_file_name, LDPL_FATAL,
785 "Failed to generate a temporary file name");
786 f = fopen (offload_objects_file_name, "w");
787 check (f, LDPL_FATAL, "could not open file with offload objects");
788 fprintf (f, "%u\n", num_offload_files);
790 /* Skip the dummy item at the start of the list. */
791 ofld = offload_files->next;
792 while (ofld)
794 fprintf (f, "%s\n", ofld->name);
795 ofld = ofld->next;
797 fclose (f);
799 arg = concat ("-foffload-objects=", offload_objects_file_name, NULL);
800 check (arg, LDPL_FATAL, "could not allocate");
801 *lto_arg_ptr++ = arg;
804 for (i = 0; i < num_claimed_files; i++)
806 struct plugin_file_info *info = &claimed_files[i];
808 *lto_arg_ptr++ = info->name;
811 *lto_arg_ptr++ = NULL;
812 exec_lto_wrapper (lto_argv);
814 free (lto_argv);
816 /* --pass-through is not needed when using gold 1.11 or later. */
817 if (pass_through_items && gold_version < 111)
819 unsigned int i;
820 for (i = 0; i < num_pass_through_items; i++)
822 if (strncmp (pass_through_items[i], "-l", 2) == 0)
823 add_input_library (pass_through_items[i] + 2);
824 else
825 add_input_file (pass_through_items[i]);
826 free (pass_through_items[i]);
827 pass_through_items[i] = NULL;
829 free (pass_through_items);
830 pass_through_items = NULL;
833 return LDPS_OK;
836 /* Helper, as used in collect2. */
837 static int
838 file_exists (const char *name)
840 return access (name, R_OK) == 0;
843 /* Unlink FILE unless we have save-temps set.
844 Note that we're saving files if verbose output is set. */
846 static void
847 maybe_unlink (const char *file)
849 if (save_temps && file_exists (file))
851 if (verbose)
852 fprintf (stderr, "[Leaving %s]\n", file);
853 return;
856 unlink_if_ordinary (file);
859 /* Remove temporary files at the end of the link. */
861 static enum ld_plugin_status
862 cleanup_handler (void)
864 unsigned int i;
866 if (debug)
867 return LDPS_OK;
869 if (arguments_file_name)
870 maybe_unlink (arguments_file_name);
872 for (i = 0; i < num_output_files; i++)
873 maybe_unlink (output_files[i]);
875 free_2 ();
876 return LDPS_OK;
879 #define SWAP(type, a, b) \
880 do { type tmp_; tmp_ = (a); (a) = (b); (b) = tmp_; } while(0)
882 /* Compare two hash table entries */
884 static int eq_sym (const void *a, const void *b)
886 const struct ld_plugin_symbol *as = (const struct ld_plugin_symbol *)a;
887 const struct ld_plugin_symbol *bs = (const struct ld_plugin_symbol *)b;
889 return !strcmp (as->name, bs->name);
892 /* Hash a symbol */
894 static hashval_t hash_sym (const void *a)
896 const struct ld_plugin_symbol *as = (const struct ld_plugin_symbol *)a;
898 return htab_hash_string (as->name);
901 /* Determine how strong a symbol is */
903 static int symbol_strength (struct ld_plugin_symbol *s)
905 switch (s->def)
907 case LDPK_UNDEF:
908 case LDPK_WEAKUNDEF:
909 return 0;
910 case LDPK_WEAKDEF:
911 return 1;
912 default:
913 return 2;
917 /* In the ld -r case we can get dups in the LTO symbol tables, where
918 the same symbol can have different resolutions (e.g. undefined and defined).
920 We have to keep that in the LTO symbol tables, but the dups confuse
921 gold and then finally gcc by supplying incorrect resolutions.
923 Problem is that the main gold symbol table doesn't know about subids
924 and does not distingush the same symbols in different states.
926 So we drop duplicates from the linker visible symbol table
927 and keep them in a private table. Then later do own symbol
928 resolution for the duplicated based on the results for the
929 originals.
931 Then when writing out the resolution file readd the dropped symbols.
933 XXX how to handle common? */
935 static void
936 resolve_conflicts (struct plugin_symtab *t, struct plugin_symtab *conflicts)
938 htab_t symtab = htab_create (t->nsyms, hash_sym, eq_sym, NULL);
939 int i;
940 int out;
941 int outlen;
943 outlen = t->nsyms;
944 conflicts->syms = xmalloc (sizeof (struct ld_plugin_symbol) * outlen);
945 conflicts->aux = xmalloc (sizeof (struct sym_aux) * outlen);
947 /* Move all duplicate symbols into the auxiliary conflicts table. */
948 out = 0;
949 for (i = 0; i < t->nsyms; i++)
951 struct ld_plugin_symbol *s = &t->syms[i];
952 struct sym_aux *aux = &t->aux[i];
953 void **slot;
955 slot = htab_find_slot (symtab, s, INSERT);
956 if (*slot != NULL)
958 int cnf;
959 struct ld_plugin_symbol *orig = (struct ld_plugin_symbol *)*slot;
960 struct sym_aux *orig_aux = &t->aux[orig - t->syms];
962 /* Always let the linker resolve the strongest symbol */
963 if (symbol_strength (orig) < symbol_strength (s))
965 SWAP (struct ld_plugin_symbol, *orig, *s);
966 SWAP (uint32_t, orig_aux->slot, aux->slot);
967 SWAP (unsigned long long, orig_aux->id, aux->id);
968 /* Don't swap conflict chain pointer */
971 /* Move current symbol into the conflicts table */
972 cnf = conflicts->nsyms++;
973 conflicts->syms[cnf] = *s;
974 conflicts->aux[cnf] = *aux;
975 aux = &conflicts->aux[cnf];
977 /* Update conflicts chain of the original symbol */
978 aux->next_conflict = orig_aux->next_conflict;
979 orig_aux->next_conflict = cnf;
981 continue;
984 /* Remove previous duplicates in the main table */
985 if (out < i)
987 t->syms[out] = *s;
988 t->aux[out] = *aux;
991 /* Put original into the hash table */
992 *slot = &t->syms[out];
993 out++;
996 assert (conflicts->nsyms <= outlen);
997 assert (conflicts->nsyms + out == t->nsyms);
999 t->nsyms = out;
1000 htab_delete (symtab);
1003 /* Process one section of an object file. */
1005 static int
1006 process_symtab (void *data, const char *name, off_t offset, off_t length)
1008 struct plugin_objfile *obj = (struct plugin_objfile *)data;
1009 char *s;
1010 char *secdatastart, *secdata;
1012 if (strncmp (name, LTO_SYMTAB_PREFIX, LTO_SYMTAB_PREFIX_LEN) != 0)
1013 return 1;
1015 s = strrchr (name, '.');
1016 if (s)
1017 sscanf (s, ".%" PRI_LL "x", &obj->out->id);
1018 secdata = secdatastart = xmalloc (length);
1019 offset += obj->file->offset;
1020 if (offset != lseek (obj->file->fd, offset, SEEK_SET))
1021 goto err;
1025 ssize_t got = read (obj->file->fd, secdata, length);
1026 if (got == 0)
1027 break;
1028 else if (got > 0)
1030 secdata += got;
1031 length -= got;
1033 else if (errno != EINTR)
1034 goto err;
1036 while (length > 0);
1037 if (length > 0)
1038 goto err;
1040 translate (secdatastart, secdata, obj->out);
1041 obj->found++;
1042 free (secdatastart);
1043 return 1;
1045 err:
1046 if (message)
1047 message (LDPL_FATAL, "%s: corrupt object file", obj->file->name);
1048 /* Force claim_file_handler to abandon this file. */
1049 obj->found = 0;
1050 free (secdatastart);
1051 return 0;
1054 /* Process one section of an object file. */
1056 static int
1057 process_symtab_extension (void *data, const char *name, off_t offset,
1058 off_t length)
1060 struct plugin_objfile *obj = (struct plugin_objfile *)data;
1061 char *s;
1062 char *secdatastart, *secdata;
1064 if (strncmp (name, LTO_SYMTAB_EXT_PREFIX, LTO_SYMTAB_EXT_PREFIX_LEN) != 0)
1065 return 1;
1067 s = strrchr (name, '.');
1068 if (s)
1069 sscanf (s, ".%" PRI_LL "x", &obj->out->id);
1070 secdata = secdatastart = xmalloc (length);
1071 offset += obj->file->offset;
1072 if (offset != lseek (obj->file->fd, offset, SEEK_SET))
1073 goto err;
1077 ssize_t got = read (obj->file->fd, secdata, length);
1078 if (got == 0)
1079 break;
1080 else if (got > 0)
1082 secdata += got;
1083 length -= got;
1085 else if (errno != EINTR)
1086 goto err;
1088 while (length > 0);
1089 if (length > 0)
1090 goto err;
1092 parse_symtab_extension (secdatastart, secdata, obj->out);
1093 obj->found++;
1094 free (secdatastart);
1095 return 1;
1097 err:
1098 if (message)
1099 message (LDPL_FATAL, "%s: corrupt object file", obj->file->name);
1100 /* Force claim_file_handler to abandon this file. */
1101 obj->found = 0;
1102 free (secdatastart);
1103 return 0;
1107 /* Find an offload section of an object file. */
1109 static int
1110 process_offload_section (void *data, const char *name, off_t offset, off_t len)
1112 if (!strncmp (name, OFFLOAD_SECTION, OFFLOAD_SECTION_LEN))
1114 struct plugin_objfile *obj = (struct plugin_objfile *) data;
1115 obj->offload = 1;
1116 return 0;
1119 return 1;
1122 /* Callback used by gold to check if the plugin will claim FILE. Writes
1123 the result in CLAIMED. */
1125 static enum ld_plugin_status
1126 claim_file_handler (const struct ld_plugin_input_file *file, int *claimed)
1128 enum ld_plugin_status status;
1129 struct plugin_objfile obj;
1130 struct plugin_file_info lto_file;
1131 int err;
1132 const char *errmsg;
1134 memset (&lto_file, 0, sizeof (struct plugin_file_info));
1136 if (file->offset != 0)
1138 /* We pass the offset of the actual file, not the archive header.
1139 Can't use PRIx64, because that's C99, so we have to print the
1140 64-bit hex int as two 32-bit ones. Use xasprintf instead of
1141 asprintf because asprintf doesn't work as expected on some older
1142 mingw32 hosts. */
1143 int lo, hi;
1144 lo = file->offset & 0xffffffff;
1145 hi = ((int64_t)file->offset >> 32) & 0xffffffff;
1146 lto_file.name = hi ? xasprintf ("%s@0x%x%08x", file->name, hi, lo)
1147 : xasprintf ("%s@0x%x", file->name, lo);
1149 else
1151 lto_file.name = xstrdup (file->name);
1153 lto_file.handle = file->handle;
1155 *claimed = 0;
1156 obj.file = file;
1157 obj.found = 0;
1158 obj.offload = 0;
1159 obj.out = &lto_file.symtab;
1160 errmsg = NULL;
1161 obj.objfile = simple_object_start_read (file->fd, file->offset, LTO_SEGMENT_NAME,
1162 &errmsg, &err);
1163 /* No file, but also no error code means unrecognized format; just skip it. */
1164 if (!obj.objfile && !err)
1165 goto err;
1167 if (obj.objfile)
1169 errmsg = simple_object_find_sections (obj.objfile, process_symtab, &obj,
1170 &err);
1171 /* Parsing symtab extension should be done only for add_symbols_v2 and
1172 later versions. */
1173 if (!errmsg && add_symbols_v2 != NULL)
1174 errmsg = simple_object_find_sections (obj.objfile,
1175 process_symtab_extension,
1176 &obj, &err);
1179 if (!obj.objfile || errmsg)
1181 if (err && message)
1182 message (LDPL_FATAL, "%s: %s: %s", file->name, errmsg,
1183 xstrerror (err));
1184 else if (message)
1185 message (LDPL_FATAL, "%s: %s", file->name, errmsg);
1186 goto err;
1189 if (obj.objfile)
1190 simple_object_find_sections (obj.objfile, process_offload_section,
1191 &obj, &err);
1193 if (obj.found == 0 && obj.offload == 0)
1194 goto err;
1196 if (obj.found > 1)
1197 resolve_conflicts (&lto_file.symtab, &lto_file.conflicts);
1199 if (obj.found > 0)
1201 if (add_symbols_v2)
1202 status = add_symbols_v2 (file->handle, lto_file.symtab.nsyms,
1203 lto_file.symtab.syms);
1204 else
1205 status = add_symbols (file->handle, lto_file.symtab.nsyms,
1206 lto_file.symtab.syms);
1207 check (status == LDPS_OK, LDPL_FATAL, "could not add symbols");
1209 num_claimed_files++;
1210 claimed_files =
1211 xrealloc (claimed_files,
1212 num_claimed_files * sizeof (struct plugin_file_info));
1213 claimed_files[num_claimed_files - 1] = lto_file;
1215 *claimed = 1;
1218 if (offload_files == NULL)
1220 /* Add dummy item to the start of the list. */
1221 offload_files = xmalloc (sizeof (struct plugin_offload_file));
1222 offload_files->name = NULL;
1223 offload_files->next = NULL;
1224 offload_files_last = offload_files;
1227 /* If this is an LTO file without offload, and it is the first LTO file, save
1228 the pointer to the last offload file in the list. Further offload LTO
1229 files will be inserted after it, if any. */
1230 if (*claimed && obj.offload == 0 && offload_files_last_lto == NULL)
1231 offload_files_last_lto = offload_files_last;
1233 if (obj.offload == 1)
1235 /* Add file to the list. The order must be exactly the same as the final
1236 order after recompilation and linking, otherwise host and target tables
1237 with addresses wouldn't match. If a static library contains both LTO
1238 and non-LTO objects, ld and gold link them in a different order. */
1239 struct plugin_offload_file *ofld
1240 = xmalloc (sizeof (struct plugin_offload_file));
1241 ofld->name = lto_file.name;
1242 ofld->next = NULL;
1244 if (*claimed && offload_files_last_lto == NULL && file->offset != 0
1245 && gold_version == -1)
1247 /* ld only: insert first LTO file from the archive after the last real
1248 object file immediately preceding the archive, or at the begin of
1249 the list if there was no real objects before archives. */
1250 if (offload_files_last_obj != NULL)
1252 ofld->next = offload_files_last_obj->next;
1253 offload_files_last_obj->next = ofld;
1255 else
1257 ofld->next = offload_files->next;
1258 offload_files->next = ofld;
1261 else if (*claimed && offload_files_last_lto != NULL)
1263 /* Insert LTO file after the last LTO file in the list. */
1264 ofld->next = offload_files_last_lto->next;
1265 offload_files_last_lto->next = ofld;
1267 else
1268 /* Add non-LTO file or first non-archive LTO file to the end of the
1269 list. */
1270 offload_files_last->next = ofld;
1272 if (ofld->next == NULL)
1273 offload_files_last = ofld;
1274 if (file->offset == 0)
1275 offload_files_last_obj = ofld;
1276 if (*claimed)
1277 offload_files_last_lto = ofld;
1278 num_offload_files++;
1281 goto cleanup;
1283 err:
1284 non_claimed_files++;
1285 free (lto_file.name);
1287 cleanup:
1288 if (obj.objfile)
1289 simple_object_release_read (obj.objfile);
1291 return LDPS_OK;
1294 /* Parse the plugin options. */
1296 static void
1297 process_option (const char *option)
1299 if (strcmp (option, "-linker-output-known") == 0)
1300 linker_output_known = true;
1301 else if (strcmp (option, "-linker-output-auto-notlo-rel") == 0)
1302 linker_output_auto_nolto_rel = true;
1303 else if (strcmp (option, "-debug") == 0)
1304 debug = true;
1305 else if ((strcmp (option, "-v") == 0)
1306 || (strcmp (option, "--verbose") == 0))
1307 verbose = true;
1308 else if (strcmp (option, "-save-temps") == 0)
1309 save_temps = true;
1310 else if (strcmp (option, "-nop") == 0)
1311 nop = 1;
1312 else if (!strncmp (option, "-pass-through=", strlen("-pass-through=")))
1314 num_pass_through_items++;
1315 pass_through_items = xrealloc (pass_through_items,
1316 num_pass_through_items * sizeof (char *));
1317 pass_through_items[num_pass_through_items - 1] =
1318 xstrdup (option + strlen ("-pass-through="));
1320 else if (!strncmp (option, "-sym-style=", sizeof ("-sym-style=") - 1))
1322 switch (option[sizeof ("-sym-style=") - 1])
1324 case 'w':
1325 sym_style = ss_win32;
1326 break;
1327 case 'u':
1328 sym_style = ss_uscore;
1329 break;
1330 default:
1331 sym_style = ss_none;
1332 break;
1335 else
1337 int size;
1338 char *opt = xstrdup (option);
1339 lto_wrapper_num_args += 1;
1340 size = lto_wrapper_num_args * sizeof (char *);
1341 lto_wrapper_argv = (char **) xrealloc (lto_wrapper_argv, size);
1342 lto_wrapper_argv[lto_wrapper_num_args - 1] = opt;
1343 if (strncmp (option, "-fresolution=", sizeof ("-fresolution=") - 1) == 0)
1344 resolution_file = opt + sizeof ("-fresolution=") - 1;
1346 save_temps = save_temps || debug;
1347 verbose = verbose || debug;
1350 /* Called by gold after loading the plugin. TV is the transfer vector. */
1352 enum ld_plugin_status
1353 onload (struct ld_plugin_tv *tv)
1355 struct ld_plugin_tv *p;
1356 enum ld_plugin_status status;
1358 p = tv;
1359 while (p->tv_tag)
1361 switch (p->tv_tag)
1363 case LDPT_MESSAGE:
1364 message = p->tv_u.tv_message;
1365 break;
1366 case LDPT_REGISTER_CLAIM_FILE_HOOK:
1367 register_claim_file = p->tv_u.tv_register_claim_file;
1368 break;
1369 case LDPT_ADD_SYMBOLS_V2:
1370 add_symbols_v2 = p->tv_u.tv_add_symbols;
1371 break;
1372 case LDPT_ADD_SYMBOLS:
1373 add_symbols = p->tv_u.tv_add_symbols;
1374 break;
1375 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
1376 register_all_symbols_read = p->tv_u.tv_register_all_symbols_read;
1377 break;
1378 case LDPT_GET_SYMBOLS_V2:
1379 get_symbols_v2 = p->tv_u.tv_get_symbols;
1380 break;
1381 case LDPT_GET_SYMBOLS:
1382 get_symbols = p->tv_u.tv_get_symbols;
1383 break;
1384 case LDPT_REGISTER_CLEANUP_HOOK:
1385 register_cleanup = p->tv_u.tv_register_cleanup;
1386 break;
1387 case LDPT_ADD_INPUT_FILE:
1388 add_input_file = p->tv_u.tv_add_input_file;
1389 break;
1390 case LDPT_ADD_INPUT_LIBRARY:
1391 add_input_library = p->tv_u.tv_add_input_library;
1392 break;
1393 case LDPT_OPTION:
1394 process_option (p->tv_u.tv_string);
1395 break;
1396 case LDPT_GOLD_VERSION:
1397 gold_version = p->tv_u.tv_val;
1398 break;
1399 case LDPT_LINKER_OUTPUT:
1400 linker_output = (enum ld_plugin_output_file_type) p->tv_u.tv_val;
1401 linker_output_set = true;
1402 break;
1403 case LDPT_OUTPUT_NAME:
1404 /* We only use this to make user-friendly temp file names. */
1405 link_output_name = p->tv_u.tv_string;
1406 break;
1407 default:
1408 break;
1410 p++;
1413 check (register_claim_file, LDPL_FATAL, "register_claim_file not found");
1414 check (add_symbols, LDPL_FATAL, "add_symbols not found");
1415 status = register_claim_file (claim_file_handler);
1416 check (status == LDPS_OK, LDPL_FATAL,
1417 "could not register the claim_file callback");
1419 if (register_cleanup)
1421 status = register_cleanup (cleanup_handler);
1422 check (status == LDPS_OK, LDPL_FATAL,
1423 "could not register the cleanup callback");
1426 if (register_all_symbols_read)
1428 check (get_symbols, LDPL_FATAL, "get_symbols not found");
1429 status = register_all_symbols_read (all_symbols_read_handler);
1430 check (status == LDPS_OK, LDPL_FATAL,
1431 "could not register the all_symbols_read callback");
1434 char *collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
1435 if (collect_gcc_options)
1437 /* Support -fno-use-linker-plugin by failing to load the plugin
1438 for the case where it is auto-loaded by BFD. */
1439 if (strstr (collect_gcc_options, "'-fno-use-linker-plugin'"))
1440 return LDPS_ERR;
1442 if ( strstr (collect_gcc_options, "'-save-temps'"))
1443 save_temps = true;
1445 if (strstr (collect_gcc_options, "'-v'")
1446 || strstr (collect_gcc_options, "'--verbose'"))
1447 verbose = true;
1450 return LDPS_OK;