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