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