Clean up some minor white space issues in trans-decl.c and trans-expr.c
[official-gcc.git] / lto-plugin / lto-plugin.c
blob1ed0f0863c019e215c49f637e2700fe8a74beaba
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).
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 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. */
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"
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
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
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. */
83 #define LTO_SEGMENT_NAME "__GNU_LTO"
85 /* LTO magic section name. */
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)
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. */
97 struct sym_aux
99 uint32_t slot;
100 unsigned long long id;
101 unsigned next_conflict;
104 struct plugin_symtab
106 int nsyms;
107 struct sym_aux *aux;
108 struct ld_plugin_symbol *syms;
109 unsigned long long id;
112 /* Encapsulates object file data during symbol scan. */
113 struct plugin_objfile
115 int found;
116 int offload;
117 simple_object_read *objfile;
118 struct plugin_symtab *out;
119 const struct ld_plugin_input_file *file;
122 /* All that we have to remember about a file. */
124 struct plugin_file_info
126 char *name;
127 void *handle;
128 struct plugin_symtab symtab;
129 struct plugin_symtab conflicts;
132 /* Until ASM_OUTPUT_LABELREF can be hookized and decoupled from
133 stdio file streams, we do simple label translation here. */
135 enum symbol_style
137 ss_none, /* No underscore prefix. */
138 ss_win32, /* Underscore prefix any symbol not beginning with '@'. */
139 ss_uscore, /* Underscore prefix all symbols. */
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_add_input_library add_input_library;
149 static ld_plugin_message message;
150 static ld_plugin_add_symbols add_symbols;
152 static struct plugin_file_info *claimed_files = NULL;
153 static unsigned int num_claimed_files = 0;
155 static struct plugin_file_info *offload_files = NULL;
156 static unsigned int num_offload_files = 0;
158 static char **output_files = NULL;
159 static unsigned int num_output_files = 0;
161 static char **lto_wrapper_argv;
162 static int lto_wrapper_num_args;
164 static char **pass_through_items = NULL;
165 static unsigned int num_pass_through_items;
167 static char debug;
168 static char nop;
169 static char *resolution_file = NULL;
170 static enum ld_plugin_output_file_type linker_output;
171 static int linker_output_set;
173 /* The version of gold being used, or -1 if not gold. The number is
174 MAJOR * 100 + MINOR. */
175 static int gold_version = -1;
177 /* Not used by default, but can be overridden at runtime
178 by using -plugin-opt=-sym-style={none,win32,underscore|uscore}
179 (in fact, only first letter of style arg is checked.) */
180 static enum symbol_style sym_style = ss_none;
182 static void
183 check_1 (int gate, enum ld_plugin_level level, const char *text)
185 if (gate)
186 return;
188 if (message)
189 message (level, text);
190 else
192 /* If there is no nicer way to inform the user, fallback to stderr. */
193 fprintf (stderr, "%s\n", text);
194 if (level == LDPL_FATAL)
195 abort ();
199 /* This little wrapper allows check to be called with a non-integer
200 first argument, such as a pointer that must be non-NULL. We can't
201 use c99 bool type to coerce it into range, so we explicitly test. */
202 #define check(GATE, LEVEL, TEXT) check_1 (((GATE) != 0), (LEVEL), (TEXT))
204 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
205 by P and the result is written in ENTRY. The slot number is stored in SLOT.
206 Returns the address of the next entry. */
208 static char *
209 parse_table_entry (char *p, struct ld_plugin_symbol *entry,
210 struct sym_aux *aux)
212 unsigned char t;
213 enum ld_plugin_symbol_kind translate_kind[] =
215 LDPK_DEF,
216 LDPK_WEAKDEF,
217 LDPK_UNDEF,
218 LDPK_WEAKUNDEF,
219 LDPK_COMMON
222 enum ld_plugin_symbol_visibility translate_visibility[] =
224 LDPV_DEFAULT,
225 LDPV_PROTECTED,
226 LDPV_INTERNAL,
227 LDPV_HIDDEN
230 switch (sym_style)
232 case ss_win32:
233 if (p[0] == '@')
235 /* cf. Duff's device. */
236 case ss_none:
237 entry->name = xstrdup (p);
238 break;
240 /* FALL-THROUGH. */
241 case ss_uscore:
242 entry->name = concat ("_", p, NULL);
243 break;
244 default:
245 check (0, LDPL_FATAL, "invalid symbol style requested");
246 break;
248 while (*p)
249 p++;
250 p++;
252 entry->version = NULL;
254 entry->comdat_key = p;
255 while (*p)
256 p++;
257 p++;
259 if (strlen (entry->comdat_key) == 0)
260 entry->comdat_key = NULL;
261 else
262 entry->comdat_key = xstrdup (entry->comdat_key);
264 t = *p;
265 check (t <= 4, LDPL_FATAL, "invalid symbol kind found");
266 entry->def = translate_kind[t];
267 p++;
269 t = *p;
270 check (t <= 3, LDPL_FATAL, "invalid symbol visibility found");
271 entry->visibility = translate_visibility[t];
272 p++;
274 memcpy (&entry->size, p, sizeof (uint64_t));
275 p += 8;
277 memcpy (&aux->slot, p, sizeof (uint32_t));
278 p += 4;
280 entry->resolution = LDPR_UNKNOWN;
282 aux->next_conflict = -1;
284 return p;
287 /* Translate the IL symbol table located between DATA and END. Append the
288 slots and symbols to OUT. */
290 static void
291 translate (char *data, char *end, struct plugin_symtab *out)
293 struct sym_aux *aux;
294 struct ld_plugin_symbol *syms = NULL;
295 int n, len;
297 /* This overestimates the output buffer sizes, but at least
298 the algorithm is O(1) now. */
300 len = (end - data)/8 + out->nsyms + 1;
301 syms = xrealloc (out->syms, len * sizeof (struct ld_plugin_symbol));
302 aux = xrealloc (out->aux, len * sizeof (struct sym_aux));
304 for (n = out->nsyms; data < end; n++)
306 aux[n].id = out->id;
307 data = parse_table_entry (data, &syms[n], &aux[n]);
310 assert(n < len);
312 out->nsyms = n;
313 out->syms = syms;
314 out->aux = aux;
317 /* Free all memory that is no longer needed after writing the symbol
318 resolution. */
320 static void
321 free_1 (struct plugin_file_info *files, unsigned num_files)
323 unsigned int i;
324 for (i = 0; i < num_files; i++)
326 struct plugin_file_info *info = &files[i];
327 struct plugin_symtab *symtab = &info->symtab;
328 unsigned int j;
329 for (j = 0; j < symtab->nsyms; j++)
331 struct ld_plugin_symbol *s = &symtab->syms[j];
332 free (s->name);
333 free (s->comdat_key);
335 free (symtab->syms);
336 symtab->syms = NULL;
340 /* Free all remaining memory. */
342 static void
343 free_2 (void)
345 unsigned int i;
346 for (i = 0; i < num_claimed_files; i++)
348 struct plugin_file_info *info = &claimed_files[i];
349 struct plugin_symtab *symtab = &info->symtab;
350 free (symtab->aux);
351 free (info->name);
354 for (i = 0; i < num_offload_files; i++)
356 struct plugin_file_info *info = &offload_files[i];
357 struct plugin_symtab *symtab = &info->symtab;
358 free (symtab->aux);
359 free (info->name);
362 for (i = 0; i < num_output_files; i++)
363 free (output_files[i]);
364 free (output_files);
366 free (claimed_files);
367 claimed_files = NULL;
368 num_claimed_files = 0;
370 free (offload_files);
371 offload_files = NULL;
372 num_offload_files = 0;
374 free (arguments_file_name);
375 arguments_file_name = NULL;
378 /* Dump SYMTAB to resolution file F. */
380 static void
381 dump_symtab (FILE *f, struct plugin_symtab *symtab)
383 unsigned j;
385 for (j = 0; j < symtab->nsyms; j++)
387 uint32_t slot = symtab->aux[j].slot;
388 unsigned int resolution = symtab->syms[j].resolution;
390 assert (resolution != LDPR_UNKNOWN);
392 fprintf (f, "%u %" PRI_LL "x %s %s\n",
393 (unsigned int) slot, symtab->aux[j].id,
394 lto_resolution_str[resolution],
395 symtab->syms[j].name);
399 /* Finish the conflicts' resolution information after the linker resolved
400 the original symbols */
402 static void
403 finish_conflict_resolution (struct plugin_symtab *symtab,
404 struct plugin_symtab *conflicts)
406 int i, j;
408 if (conflicts->nsyms == 0)
409 return;
411 for (i = 0; i < symtab->nsyms; i++)
413 int resolution = LDPR_UNKNOWN;
415 if (symtab->aux[i].next_conflict == -1)
416 continue;
418 switch (symtab->syms[i].def)
420 case LDPK_DEF:
421 case LDPK_COMMON: /* ??? */
422 resolution = LDPR_RESOLVED_IR;
423 break;
424 case LDPK_WEAKDEF:
425 resolution = LDPR_PREEMPTED_IR;
426 break;
427 case LDPK_UNDEF:
428 case LDPK_WEAKUNDEF:
429 resolution = symtab->syms[i].resolution;
430 break;
431 default:
432 assert (0);
435 assert (resolution != LDPR_UNKNOWN);
437 for (j = symtab->aux[i].next_conflict;
438 j != -1;
439 j = conflicts->aux[j].next_conflict)
440 conflicts->syms[j].resolution = resolution;
444 /* Free symbol table SYMTAB. */
446 static void
447 free_symtab (struct plugin_symtab *symtab)
449 free (symtab->syms);
450 symtab->syms = NULL;
451 free (symtab->aux);
452 symtab->aux = NULL;
455 /* Writes the relocations to disk. */
457 static void
458 write_resolution (void)
460 unsigned int i;
461 FILE *f;
463 check (resolution_file, LDPL_FATAL, "resolution file not specified");
464 f = fopen (resolution_file, "w");
465 check (f, LDPL_FATAL, "could not open file");
467 fprintf (f, "%d\n", num_claimed_files);
469 for (i = 0; i < num_claimed_files; i++)
471 struct plugin_file_info *info = &claimed_files[i];
472 struct plugin_symtab *symtab = &info->symtab;
473 struct ld_plugin_symbol *syms = symtab->syms;
475 /* Version 2 of API supports IRONLY_EXP resolution that is
476 accepted by GCC-4.7 and newer. */
477 if (get_symbols_v2)
478 get_symbols_v2 (info->handle, symtab->nsyms, syms);
479 else
480 get_symbols (info->handle, symtab->nsyms, syms);
482 finish_conflict_resolution (symtab, &info->conflicts);
484 fprintf (f, "%s %d\n", info->name, symtab->nsyms + info->conflicts.nsyms);
485 dump_symtab (f, symtab);
486 if (info->conflicts.nsyms)
488 dump_symtab (f, &info->conflicts);
489 free_symtab (&info->conflicts);
492 fclose (f);
495 /* Pass files generated by the lto-wrapper to the linker. FD is lto-wrapper's
496 stdout. */
498 static void
499 add_output_files (FILE *f)
501 for (;;)
503 const unsigned piece = 32;
504 char *buf, *s = xmalloc (piece);
505 size_t len;
507 buf = s;
508 cont:
509 if (!fgets (buf, piece, f))
511 free (s);
512 break;
514 len = strlen (s);
515 if (s[len - 1] != '\n')
517 s = xrealloc (s, len + piece);
518 buf = s + len;
519 goto cont;
521 s[len - 1] = '\0';
523 num_output_files++;
524 output_files
525 = xrealloc (output_files, num_output_files * sizeof (char *));
526 output_files[num_output_files - 1] = s;
527 add_input_file (output_files[num_output_files - 1]);
531 /* Execute the lto-wrapper. ARGV[0] is the binary. The rest of ARGV is the
532 argument list. */
534 static void
535 exec_lto_wrapper (char *argv[])
537 int t, i;
538 int status;
539 char *at_args;
540 FILE *args;
541 FILE *wrapper_output;
542 char *new_argv[3];
543 struct pex_obj *pex;
544 const char *errmsg;
546 /* Write argv to a file to avoid a command line that is too long. */
547 arguments_file_name = make_temp_file ("");
548 check (arguments_file_name, LDPL_FATAL,
549 "Failed to generate a temorary file name");
551 args = fopen (arguments_file_name, "w");
552 check (args, LDPL_FATAL, "could not open arguments file");
554 t = writeargv (&argv[1], args);
555 check (t == 0, LDPL_FATAL, "could not write arguments");
556 t = fclose (args);
557 check (t == 0, LDPL_FATAL, "could not close arguments file");
559 at_args = concat ("@", arguments_file_name, NULL);
560 check (at_args, LDPL_FATAL, "could not allocate");
562 for (i = 1; argv[i]; i++)
564 char *a = argv[i];
565 if (a[0] == '-' && a[1] == 'v' && a[2] == '\0')
567 for (i = 0; argv[i]; i++)
568 fprintf (stderr, "%s ", argv[i]);
569 fprintf (stderr, "\n");
570 break;
574 new_argv[0] = argv[0];
575 new_argv[1] = at_args;
576 new_argv[2] = NULL;
578 if (debug)
580 for (i = 0; new_argv[i]; i++)
581 fprintf (stderr, "%s ", new_argv[i]);
582 fprintf (stderr, "\n");
586 pex = pex_init (PEX_USE_PIPES, "lto-wrapper", NULL);
587 check (pex != NULL, LDPL_FATAL, "could not pex_init lto-wrapper");
589 errmsg = pex_run (pex, 0, new_argv[0], new_argv, NULL, NULL, &t);
590 check (errmsg == NULL, LDPL_FATAL, "could not run lto-wrapper");
591 check (t == 0, LDPL_FATAL, "could not run lto-wrapper");
593 wrapper_output = pex_read_output (pex, 0);
594 check (wrapper_output, LDPL_FATAL, "could not read lto-wrapper output");
596 add_output_files (wrapper_output);
598 t = pex_get_status (pex, 1, &status);
599 check (t == 1, LDPL_FATAL, "could not get lto-wrapper exit status");
600 check (WIFEXITED (status) && WEXITSTATUS (status) == 0, LDPL_FATAL,
601 "lto-wrapper failed");
603 pex_free (pex);
605 free (at_args);
608 /* Pass the original files back to the linker. */
610 static void
611 use_original_files (void)
613 unsigned i;
614 for (i = 0; i < num_claimed_files; i++)
616 struct plugin_file_info *info = &claimed_files[i];
617 add_input_file (info->name);
622 /* Called by the linker once all symbols have been read. */
624 static enum ld_plugin_status
625 all_symbols_read_handler (void)
627 unsigned i;
628 unsigned num_lto_args
629 = num_claimed_files + num_offload_files + lto_wrapper_num_args + 2;
630 char **lto_argv;
631 const char *linker_output_str = NULL;
632 const char **lto_arg_ptr;
633 if (num_claimed_files + num_offload_files == 0)
634 return LDPS_OK;
636 if (nop)
638 use_original_files ();
639 return LDPS_OK;
642 lto_argv = (char **) xcalloc (sizeof (char *), num_lto_args);
643 lto_arg_ptr = (const char **) lto_argv;
644 assert (lto_wrapper_argv);
646 write_resolution ();
648 free_1 (claimed_files, num_claimed_files);
649 free_1 (offload_files, num_offload_files);
651 for (i = 0; i < lto_wrapper_num_args; i++)
652 *lto_arg_ptr++ = lto_wrapper_argv[i];
654 assert (linker_output_set);
655 switch (linker_output)
657 case LDPO_REL:
658 linker_output_str = "-flinker-output=rel";
659 break;
660 case LDPO_DYN:
661 linker_output_str = "-flinker-output=dyn";
662 break;
663 case LDPO_PIE:
664 linker_output_str = "-flinker-output=pie";
665 break;
666 case LDPO_EXEC:
667 linker_output_str = "-flinker-output=exec";
668 break;
669 default:
670 message (LDPL_FATAL, "unsupported linker output %i", linker_output);
671 break;
673 *lto_arg_ptr++ = xstrdup (linker_output_str);
674 for (i = 0; i < num_claimed_files; i++)
676 struct plugin_file_info *info = &claimed_files[i];
678 *lto_arg_ptr++ = info->name;
681 for (i = 0; i < num_offload_files; i++)
683 struct plugin_file_info *info = &offload_files[i];
685 *lto_arg_ptr++ = info->name;
688 *lto_arg_ptr++ = NULL;
689 exec_lto_wrapper (lto_argv);
691 free (lto_argv);
693 /* --pass-through is not needed when using gold 1.11 or later. */
694 if (pass_through_items && gold_version < 111)
696 unsigned int i;
697 for (i = 0; i < num_pass_through_items; i++)
699 if (strncmp (pass_through_items[i], "-l", 2) == 0)
700 add_input_library (pass_through_items[i] + 2);
701 else
702 add_input_file (pass_through_items[i]);
703 free (pass_through_items[i]);
704 pass_through_items[i] = NULL;
706 free (pass_through_items);
707 pass_through_items = NULL;
710 return LDPS_OK;
713 /* Remove temporary files at the end of the link. */
715 static enum ld_plugin_status
716 cleanup_handler (void)
718 unsigned int i;
719 int t;
721 if (debug)
722 return LDPS_OK;
724 if (arguments_file_name)
726 t = unlink (arguments_file_name);
727 check (t == 0, LDPL_FATAL, "could not unlink arguments file");
730 for (i = 0; i < num_output_files; i++)
732 t = unlink (output_files[i]);
733 check (t == 0, LDPL_FATAL, "could not unlink output file");
736 free_2 ();
737 return LDPS_OK;
740 #define SWAP(type, a, b) \
741 do { type tmp_; tmp_ = (a); (a) = (b); (b) = tmp_; } while(0)
743 /* Compare two hash table entries */
745 static int eq_sym (const void *a, const void *b)
747 const struct ld_plugin_symbol *as = (const struct ld_plugin_symbol *)a;
748 const struct ld_plugin_symbol *bs = (const struct ld_plugin_symbol *)b;
750 return !strcmp (as->name, bs->name);
753 /* Hash a symbol */
755 static hashval_t hash_sym (const void *a)
757 const struct ld_plugin_symbol *as = (const struct ld_plugin_symbol *)a;
759 return htab_hash_string (as->name);
762 /* Determine how strong a symbol is */
764 static int symbol_strength (struct ld_plugin_symbol *s)
766 switch (s->def)
768 case LDPK_UNDEF:
769 case LDPK_WEAKUNDEF:
770 return 0;
771 case LDPK_WEAKDEF:
772 return 1;
773 default:
774 return 2;
778 /* In the ld -r case we can get dups in the LTO symbol tables, where
779 the same symbol can have different resolutions (e.g. undefined and defined).
781 We have to keep that in the LTO symbol tables, but the dups confuse
782 gold and then finally gcc by supplying incorrect resolutions.
784 Problem is that the main gold symbol table doesn't know about subids
785 and does not distingush the same symbols in different states.
787 So we drop duplicates from the linker visible symbol table
788 and keep them in a private table. Then later do own symbol
789 resolution for the duplicated based on the results for the
790 originals.
792 Then when writing out the resolution file readd the dropped symbols.
794 XXX how to handle common? */
796 static void
797 resolve_conflicts (struct plugin_symtab *t, struct plugin_symtab *conflicts)
799 htab_t symtab = htab_create (t->nsyms, hash_sym, eq_sym, NULL);
800 int i;
801 int out;
802 int outlen;
804 outlen = t->nsyms;
805 conflicts->syms = xmalloc (sizeof (struct ld_plugin_symbol) * outlen);
806 conflicts->aux = xmalloc (sizeof (struct sym_aux) * outlen);
808 /* Move all duplicate symbols into the auxiliary conflicts table. */
809 out = 0;
810 for (i = 0; i < t->nsyms; i++)
812 struct ld_plugin_symbol *s = &t->syms[i];
813 struct sym_aux *aux = &t->aux[i];
814 void **slot;
816 slot = htab_find_slot (symtab, s, INSERT);
817 if (*slot != NULL)
819 int cnf;
820 struct ld_plugin_symbol *orig = (struct ld_plugin_symbol *)*slot;
821 struct sym_aux *orig_aux = &t->aux[orig - t->syms];
823 /* Always let the linker resolve the strongest symbol */
824 if (symbol_strength (orig) < symbol_strength (s))
826 SWAP (struct ld_plugin_symbol, *orig, *s);
827 SWAP (uint32_t, orig_aux->slot, aux->slot);
828 SWAP (unsigned long long, orig_aux->id, aux->id);
829 /* Don't swap conflict chain pointer */
832 /* Move current symbol into the conflicts table */
833 cnf = conflicts->nsyms++;
834 conflicts->syms[cnf] = *s;
835 conflicts->aux[cnf] = *aux;
836 aux = &conflicts->aux[cnf];
838 /* Update conflicts chain of the original symbol */
839 aux->next_conflict = orig_aux->next_conflict;
840 orig_aux->next_conflict = cnf;
842 continue;
845 /* Remove previous duplicates in the main table */
846 if (out < i)
848 t->syms[out] = *s;
849 t->aux[out] = *aux;
852 /* Put original into the hash table */
853 *slot = &t->syms[out];
854 out++;
857 assert (conflicts->nsyms <= outlen);
858 assert (conflicts->nsyms + out == t->nsyms);
860 t->nsyms = out;
861 htab_delete (symtab);
864 /* Process one section of an object file. */
866 static int
867 process_symtab (void *data, const char *name, off_t offset, off_t length)
869 struct plugin_objfile *obj = (struct plugin_objfile *)data;
870 char *s;
871 char *secdatastart, *secdata;
873 if (strncmp (name, LTO_SECTION_PREFIX, LTO_SECTION_PREFIX_LEN) != 0)
874 return 1;
876 s = strrchr (name, '.');
877 if (s)
878 sscanf (s, ".%" PRI_LL "x", &obj->out->id);
879 secdata = secdatastart = xmalloc (length);
880 offset += obj->file->offset;
881 if (offset != lseek (obj->file->fd, offset, SEEK_SET))
882 goto err;
886 ssize_t got = read (obj->file->fd, secdata, length);
887 if (got == 0)
888 break;
889 else if (got > 0)
891 secdata += got;
892 length -= got;
894 else if (errno != EINTR)
895 goto err;
897 while (length > 0);
898 if (length > 0)
899 goto err;
901 translate (secdatastart, secdata, obj->out);
902 obj->found++;
903 free (secdatastart);
904 return 1;
906 err:
907 if (message)
908 message (LDPL_FATAL, "%s: corrupt object file", obj->file->name);
909 /* Force claim_file_handler to abandon this file. */
910 obj->found = 0;
911 free (secdatastart);
912 return 0;
915 /* Find an offload section of an object file. */
917 static int
918 process_offload_section (void *data, const char *name, off_t offset, off_t len)
920 if (!strncmp (name, OFFLOAD_SECTION, OFFLOAD_SECTION_LEN))
922 struct plugin_objfile *obj = (struct plugin_objfile *) data;
923 obj->offload = 1;
924 return 0;
927 return 1;
930 /* Callback used by gold to check if the plugin will claim FILE. Writes
931 the result in CLAIMED. */
933 static enum ld_plugin_status
934 claim_file_handler (const struct ld_plugin_input_file *file, int *claimed)
936 enum ld_plugin_status status;
937 struct plugin_objfile obj;
938 struct plugin_file_info lto_file;
939 int err;
940 const char *errmsg;
942 memset (&lto_file, 0, sizeof (struct plugin_file_info));
944 if (file->offset != 0)
946 char *objname;
947 /* We pass the offset of the actual file, not the archive header.
948 Can't use PRIx64, because that's C99, so we have to print the
949 64-bit hex int as two 32-bit ones. */
950 int lo, hi, t;
951 lo = file->offset & 0xffffffff;
952 hi = ((int64_t)file->offset >> 32) & 0xffffffff;
953 t = hi ? asprintf (&objname, "%s@0x%x%08x", file->name, lo, hi)
954 : asprintf (&objname, "%s@0x%x", file->name, lo);
955 check (t >= 0, LDPL_FATAL, "asprintf failed");
956 lto_file.name = objname;
958 else
960 lto_file.name = xstrdup (file->name);
962 lto_file.handle = file->handle;
964 *claimed = 0;
965 obj.file = file;
966 obj.found = 0;
967 obj.offload = 0;
968 obj.out = &lto_file.symtab;
969 errmsg = NULL;
970 obj.objfile = simple_object_start_read (file->fd, file->offset, LTO_SEGMENT_NAME,
971 &errmsg, &err);
972 /* No file, but also no error code means unrecognized format; just skip it. */
973 if (!obj.objfile && !err)
974 goto err;
976 if (obj.objfile)
977 errmsg = simple_object_find_sections (obj.objfile, process_symtab, &obj, &err);
979 if (!obj.objfile || errmsg)
981 if (err && message)
982 message (LDPL_FATAL, "%s: %s: %s", file->name, errmsg,
983 xstrerror (err));
984 else if (message)
985 message (LDPL_FATAL, "%s: %s", file->name, errmsg);
986 goto err;
989 if (obj.objfile)
990 simple_object_find_sections (obj.objfile, process_offload_section,
991 &obj, &err);
993 if (obj.found == 0 && obj.offload == 0)
994 goto err;
996 if (obj.found > 1)
997 resolve_conflicts (&lto_file.symtab, &lto_file.conflicts);
999 if (obj.found > 0)
1001 status = add_symbols (file->handle, lto_file.symtab.nsyms,
1002 lto_file.symtab.syms);
1003 check (status == LDPS_OK, LDPL_FATAL, "could not add symbols");
1005 num_claimed_files++;
1006 claimed_files =
1007 xrealloc (claimed_files,
1008 num_claimed_files * sizeof (struct plugin_file_info));
1009 claimed_files[num_claimed_files - 1] = lto_file;
1012 if (obj.found == 0 && obj.offload == 1)
1014 num_offload_files++;
1015 offload_files =
1016 xrealloc (offload_files,
1017 num_offload_files * sizeof (struct plugin_file_info));
1018 offload_files[num_offload_files - 1] = lto_file;
1021 *claimed = 1;
1023 goto cleanup;
1025 err:
1026 free (lto_file.name);
1028 cleanup:
1029 if (obj.objfile)
1030 simple_object_release_read (obj.objfile);
1032 return LDPS_OK;
1035 /* Parse the plugin options. */
1037 static void
1038 process_option (const char *option)
1040 if (strcmp (option, "-debug") == 0)
1041 debug = 1;
1042 else if (strcmp (option, "-nop") == 0)
1043 nop = 1;
1044 else if (!strncmp (option, "-pass-through=", strlen("-pass-through=")))
1046 num_pass_through_items++;
1047 pass_through_items = xrealloc (pass_through_items,
1048 num_pass_through_items * sizeof (char *));
1049 pass_through_items[num_pass_through_items - 1] =
1050 xstrdup (option + strlen ("-pass-through="));
1052 else if (!strncmp (option, "-sym-style=", sizeof ("-sym-style=") - 1))
1054 switch (option[sizeof ("-sym-style=") - 1])
1056 case 'w':
1057 sym_style = ss_win32;
1058 break;
1059 case 'u':
1060 sym_style = ss_uscore;
1061 break;
1062 default:
1063 sym_style = ss_none;
1064 break;
1067 else
1069 int size;
1070 char *opt = xstrdup (option);
1071 lto_wrapper_num_args += 1;
1072 size = lto_wrapper_num_args * sizeof (char *);
1073 lto_wrapper_argv = (char **) xrealloc (lto_wrapper_argv, size);
1074 lto_wrapper_argv[lto_wrapper_num_args - 1] = opt;
1075 if (strncmp (option, "-fresolution=", sizeof ("-fresolution=") - 1) == 0)
1076 resolution_file = opt + sizeof ("-fresolution=") - 1;
1080 /* Called by gold after loading the plugin. TV is the transfer vector. */
1082 enum ld_plugin_status
1083 onload (struct ld_plugin_tv *tv)
1085 struct ld_plugin_tv *p;
1086 enum ld_plugin_status status;
1088 p = tv;
1089 while (p->tv_tag)
1091 switch (p->tv_tag)
1093 case LDPT_MESSAGE:
1094 message = p->tv_u.tv_message;
1095 break;
1096 case LDPT_REGISTER_CLAIM_FILE_HOOK:
1097 register_claim_file = p->tv_u.tv_register_claim_file;
1098 break;
1099 case LDPT_ADD_SYMBOLS:
1100 add_symbols = p->tv_u.tv_add_symbols;
1101 break;
1102 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
1103 register_all_symbols_read = p->tv_u.tv_register_all_symbols_read;
1104 break;
1105 case LDPT_GET_SYMBOLS_V2:
1106 get_symbols_v2 = p->tv_u.tv_get_symbols;
1107 break;
1108 case LDPT_GET_SYMBOLS:
1109 get_symbols = p->tv_u.tv_get_symbols;
1110 break;
1111 case LDPT_REGISTER_CLEANUP_HOOK:
1112 register_cleanup = p->tv_u.tv_register_cleanup;
1113 break;
1114 case LDPT_ADD_INPUT_FILE:
1115 add_input_file = p->tv_u.tv_add_input_file;
1116 break;
1117 case LDPT_ADD_INPUT_LIBRARY:
1118 add_input_library = p->tv_u.tv_add_input_library;
1119 break;
1120 case LDPT_OPTION:
1121 process_option (p->tv_u.tv_string);
1122 break;
1123 case LDPT_GOLD_VERSION:
1124 gold_version = p->tv_u.tv_val;
1125 break;
1126 case LDPT_LINKER_OUTPUT:
1127 linker_output = (enum ld_plugin_output_file_type) p->tv_u.tv_val;
1128 linker_output_set = 1;
1129 break;
1130 default:
1131 break;
1133 p++;
1136 check (register_claim_file, LDPL_FATAL, "register_claim_file not found");
1137 check (add_symbols, LDPL_FATAL, "add_symbols not found");
1138 status = register_claim_file (claim_file_handler);
1139 check (status == LDPS_OK, LDPL_FATAL,
1140 "could not register the claim_file callback");
1142 if (register_cleanup)
1144 status = register_cleanup (cleanup_handler);
1145 check (status == LDPS_OK, LDPL_FATAL,
1146 "could not register the cleanup callback");
1149 if (register_all_symbols_read)
1151 check (get_symbols, LDPL_FATAL, "get_symbols not found");
1152 status = register_all_symbols_read (all_symbols_read_handler);
1153 check (status == LDPS_OK, LDPL_FATAL,
1154 "could not register the all_symbols_read callback");
1157 /* Support -fno-use-linker-plugin by failing to load the plugin
1158 for the case where it is auto-loaded by BFD. */
1159 char *collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
1160 if (collect_gcc_options
1161 && strstr (collect_gcc_options, "'-fno-use-linker-plugin'"))
1162 return LDPS_ERR;
1164 return LDPS_OK;