scanner.c (skip_free_comments, [...]): Use gfc_warning_now instead of ..._now_1.
[official-gcc.git] / lto-plugin / lto-plugin.c
blobfb6555daf9118046d9dbd553101f3e87c290f2f0
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 char **output_files = NULL;
156 static unsigned int num_output_files = 0;
158 static char **lto_wrapper_argv;
159 static int lto_wrapper_num_args;
161 static char **pass_through_items = NULL;
162 static unsigned int num_pass_through_items;
164 static char debug;
165 static char nop;
166 static char *resolution_file = NULL;
168 /* The version of gold being used, or -1 if not gold. The number is
169 MAJOR * 100 + MINOR. */
170 static int gold_version = -1;
172 /* Not used by default, but can be overridden at runtime
173 by using -plugin-opt=-sym-style={none,win32,underscore|uscore}
174 (in fact, only first letter of style arg is checked.) */
175 static enum symbol_style sym_style = ss_none;
177 static void
178 check_1 (int gate, enum ld_plugin_level level, const char *text)
180 if (gate)
181 return;
183 if (message)
184 message (level, text);
185 else
187 /* If there is no nicer way to inform the user, fallback to stderr. */
188 fprintf (stderr, "%s\n", text);
189 if (level == LDPL_FATAL)
190 abort ();
194 /* This little wrapper allows check to be called with a non-integer
195 first argument, such as a pointer that must be non-NULL. We can't
196 use c99 bool type to coerce it into range, so we explicitly test. */
197 #define check(GATE, LEVEL, TEXT) check_1 (((GATE) != 0), (LEVEL), (TEXT))
199 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
200 by P and the result is written in ENTRY. The slot number is stored in SLOT.
201 Returns the address of the next entry. */
203 static char *
204 parse_table_entry (char *p, struct ld_plugin_symbol *entry,
205 struct sym_aux *aux)
207 unsigned char t;
208 enum ld_plugin_symbol_kind translate_kind[] =
210 LDPK_DEF,
211 LDPK_WEAKDEF,
212 LDPK_UNDEF,
213 LDPK_WEAKUNDEF,
214 LDPK_COMMON
217 enum ld_plugin_symbol_visibility translate_visibility[] =
219 LDPV_DEFAULT,
220 LDPV_PROTECTED,
221 LDPV_INTERNAL,
222 LDPV_HIDDEN
225 switch (sym_style)
227 case ss_win32:
228 if (p[0] == '@')
230 /* cf. Duff's device. */
231 case ss_none:
232 entry->name = xstrdup (p);
233 break;
235 /* FALL-THROUGH. */
236 case ss_uscore:
237 entry->name = concat ("_", p, NULL);
238 break;
239 default:
240 check (0, LDPL_FATAL, "invalid symbol style requested");
241 break;
243 while (*p)
244 p++;
245 p++;
247 entry->version = NULL;
249 entry->comdat_key = p;
250 while (*p)
251 p++;
252 p++;
254 if (strlen (entry->comdat_key) == 0)
255 entry->comdat_key = NULL;
256 else
257 entry->comdat_key = xstrdup (entry->comdat_key);
259 t = *p;
260 check (t <= 4, LDPL_FATAL, "invalid symbol kind found");
261 entry->def = translate_kind[t];
262 p++;
264 t = *p;
265 check (t <= 3, LDPL_FATAL, "invalid symbol visibility found");
266 entry->visibility = translate_visibility[t];
267 p++;
269 memcpy (&entry->size, p, sizeof (uint64_t));
270 p += 8;
272 memcpy (&aux->slot, p, sizeof (uint32_t));
273 p += 4;
275 entry->resolution = LDPR_UNKNOWN;
277 aux->next_conflict = -1;
279 return p;
282 /* Translate the IL symbol table located between DATA and END. Append the
283 slots and symbols to OUT. */
285 static void
286 translate (char *data, char *end, struct plugin_symtab *out)
288 struct sym_aux *aux;
289 struct ld_plugin_symbol *syms = NULL;
290 int n, len;
292 /* This overestimates the output buffer sizes, but at least
293 the algorithm is O(1) now. */
295 len = (end - data)/8 + out->nsyms + 1;
296 syms = xrealloc (out->syms, len * sizeof (struct ld_plugin_symbol));
297 aux = xrealloc (out->aux, len * sizeof (struct sym_aux));
299 for (n = out->nsyms; data < end; n++)
301 aux[n].id = out->id;
302 data = parse_table_entry (data, &syms[n], &aux[n]);
305 assert(n < len);
307 out->nsyms = n;
308 out->syms = syms;
309 out->aux = aux;
312 /* Free all memory that is no longer needed after writing the symbol
313 resolution. */
315 static void
316 free_1 (void)
318 unsigned int i;
319 for (i = 0; i < num_claimed_files; i++)
321 struct plugin_file_info *info = &claimed_files[i];
322 struct plugin_symtab *symtab = &info->symtab;
323 unsigned int j;
324 for (j = 0; j < symtab->nsyms; j++)
326 struct ld_plugin_symbol *s = &symtab->syms[j];
327 free (s->name);
328 free (s->comdat_key);
330 free (symtab->syms);
331 symtab->syms = NULL;
335 /* Free all remaining memory. */
337 static void
338 free_2 (void)
340 unsigned int i;
341 for (i = 0; i < num_claimed_files; i++)
343 struct plugin_file_info *info = &claimed_files[i];
344 struct plugin_symtab *symtab = &info->symtab;
345 free (symtab->aux);
346 free (info->name);
349 for (i = 0; i < num_output_files; i++)
350 free (output_files[i]);
351 free (output_files);
353 free (claimed_files);
354 claimed_files = NULL;
355 num_claimed_files = 0;
357 free (arguments_file_name);
358 arguments_file_name = NULL;
361 /* Dump SYMTAB to resolution file F. */
363 static void
364 dump_symtab (FILE *f, struct plugin_symtab *symtab)
366 unsigned j;
368 for (j = 0; j < symtab->nsyms; j++)
370 uint32_t slot = symtab->aux[j].slot;
371 unsigned int resolution = symtab->syms[j].resolution;
373 assert (resolution != LDPR_UNKNOWN);
375 fprintf (f, "%u %" PRI_LL "x %s %s\n",
376 (unsigned int) slot, symtab->aux[j].id,
377 lto_resolution_str[resolution],
378 symtab->syms[j].name);
382 /* Finish the conflicts' resolution information after the linker resolved
383 the original symbols */
385 static void
386 finish_conflict_resolution (struct plugin_symtab *symtab,
387 struct plugin_symtab *conflicts)
389 int i, j;
391 if (conflicts->nsyms == 0)
392 return;
394 for (i = 0; i < symtab->nsyms; i++)
396 int resolution = LDPR_UNKNOWN;
398 if (symtab->aux[i].next_conflict == -1)
399 continue;
401 switch (symtab->syms[i].def)
403 case LDPK_DEF:
404 case LDPK_COMMON: /* ??? */
405 resolution = LDPR_RESOLVED_IR;
406 break;
407 case LDPK_WEAKDEF:
408 resolution = LDPR_PREEMPTED_IR;
409 break;
410 case LDPK_UNDEF:
411 case LDPK_WEAKUNDEF:
412 resolution = symtab->syms[i].resolution;
413 break;
414 default:
415 assert (0);
418 assert (resolution != LDPR_UNKNOWN);
420 for (j = symtab->aux[i].next_conflict;
421 j != -1;
422 j = conflicts->aux[j].next_conflict)
423 conflicts->syms[j].resolution = resolution;
427 /* Free symbol table SYMTAB. */
429 static void
430 free_symtab (struct plugin_symtab *symtab)
432 free (symtab->syms);
433 symtab->syms = NULL;
434 free (symtab->aux);
435 symtab->aux = NULL;
438 /* Writes the relocations to disk. */
440 static void
441 write_resolution (void)
443 unsigned int i;
444 FILE *f;
446 check (resolution_file, LDPL_FATAL, "resolution file not specified");
447 f = fopen (resolution_file, "w");
448 check (f, LDPL_FATAL, "could not open file");
450 fprintf (f, "%d\n", num_claimed_files);
452 for (i = 0; i < num_claimed_files; i++)
454 struct plugin_file_info *info = &claimed_files[i];
455 struct plugin_symtab *symtab = &info->symtab;
456 struct ld_plugin_symbol *syms = symtab->syms;
458 /* Version 2 of API supports IRONLY_EXP resolution that is
459 accepted by GCC-4.7 and newer. */
460 if (get_symbols_v2)
461 get_symbols_v2 (info->handle, symtab->nsyms, syms);
462 else
463 get_symbols (info->handle, symtab->nsyms, syms);
465 finish_conflict_resolution (symtab, &info->conflicts);
467 fprintf (f, "%s %d\n", info->name, symtab->nsyms + info->conflicts.nsyms);
468 dump_symtab (f, symtab);
469 if (info->conflicts.nsyms)
471 dump_symtab (f, &info->conflicts);
472 free_symtab (&info->conflicts);
475 fclose (f);
478 /* Pass files generated by the lto-wrapper to the linker. FD is lto-wrapper's
479 stdout. */
481 static void
482 add_output_files (FILE *f)
484 for (;;)
486 const unsigned piece = 32;
487 char *buf, *s = xmalloc (piece);
488 size_t len;
490 buf = s;
491 cont:
492 if (!fgets (buf, piece, f))
494 free (s);
495 break;
497 len = strlen (s);
498 if (s[len - 1] != '\n')
500 s = xrealloc (s, len + piece);
501 buf = s + len;
502 goto cont;
504 s[len - 1] = '\0';
506 num_output_files++;
507 output_files
508 = xrealloc (output_files, num_output_files * sizeof (char *));
509 output_files[num_output_files - 1] = s;
510 add_input_file (output_files[num_output_files - 1]);
514 /* Execute the lto-wrapper. ARGV[0] is the binary. The rest of ARGV is the
515 argument list. */
517 static void
518 exec_lto_wrapper (char *argv[])
520 int t, i;
521 int status;
522 char *at_args;
523 FILE *args;
524 FILE *wrapper_output;
525 char *new_argv[3];
526 struct pex_obj *pex;
527 const char *errmsg;
529 /* Write argv to a file to avoid a command line that is too long. */
530 arguments_file_name = make_temp_file ("");
531 check (arguments_file_name, LDPL_FATAL,
532 "Failed to generate a temorary file name");
534 args = fopen (arguments_file_name, "w");
535 check (args, LDPL_FATAL, "could not open arguments file");
537 t = writeargv (&argv[1], args);
538 check (t == 0, LDPL_FATAL, "could not write arguments");
539 t = fclose (args);
540 check (t == 0, LDPL_FATAL, "could not close arguments file");
542 at_args = concat ("@", arguments_file_name, NULL);
543 check (at_args, LDPL_FATAL, "could not allocate");
545 for (i = 1; argv[i]; i++)
547 char *a = argv[i];
548 if (a[0] == '-' && a[1] == 'v' && a[2] == '\0')
550 for (i = 0; argv[i]; i++)
551 fprintf (stderr, "%s ", argv[i]);
552 fprintf (stderr, "\n");
553 break;
557 new_argv[0] = argv[0];
558 new_argv[1] = at_args;
559 new_argv[2] = NULL;
561 if (debug)
563 for (i = 0; new_argv[i]; i++)
564 fprintf (stderr, "%s ", new_argv[i]);
565 fprintf (stderr, "\n");
569 pex = pex_init (PEX_USE_PIPES, "lto-wrapper", NULL);
570 check (pex != NULL, LDPL_FATAL, "could not pex_init lto-wrapper");
572 errmsg = pex_run (pex, 0, new_argv[0], new_argv, NULL, NULL, &t);
573 check (errmsg == NULL, LDPL_FATAL, "could not run lto-wrapper");
574 check (t == 0, LDPL_FATAL, "could not run lto-wrapper");
576 wrapper_output = pex_read_output (pex, 0);
577 check (wrapper_output, LDPL_FATAL, "could not read lto-wrapper output");
579 add_output_files (wrapper_output);
581 t = pex_get_status (pex, 1, &status);
582 check (t == 1, LDPL_FATAL, "could not get lto-wrapper exit status");
583 check (WIFEXITED (status) && WEXITSTATUS (status) == 0, LDPL_FATAL,
584 "lto-wrapper failed");
586 pex_free (pex);
588 free (at_args);
591 /* Pass the original files back to the linker. */
593 static void
594 use_original_files (void)
596 unsigned i;
597 for (i = 0; i < num_claimed_files; i++)
599 struct plugin_file_info *info = &claimed_files[i];
600 add_input_file (info->name);
605 /* Called by the linker once all symbols have been read. */
607 static enum ld_plugin_status
608 all_symbols_read_handler (void)
610 unsigned i;
611 unsigned num_lto_args = num_claimed_files + lto_wrapper_num_args + 1;
612 char **lto_argv;
613 const char **lto_arg_ptr;
614 if (num_claimed_files == 0)
615 return LDPS_OK;
617 if (nop)
619 use_original_files ();
620 return LDPS_OK;
623 lto_argv = (char **) xcalloc (sizeof (char *), num_lto_args);
624 lto_arg_ptr = (const char **) lto_argv;
625 assert (lto_wrapper_argv);
627 write_resolution ();
629 free_1 ();
631 for (i = 0; i < lto_wrapper_num_args; i++)
632 *lto_arg_ptr++ = lto_wrapper_argv[i];
634 for (i = 0; i < num_claimed_files; i++)
636 struct plugin_file_info *info = &claimed_files[i];
638 *lto_arg_ptr++ = info->name;
641 *lto_arg_ptr++ = NULL;
642 exec_lto_wrapper (lto_argv);
644 free (lto_argv);
646 /* --pass-through is not needed when using gold 1.11 or later. */
647 if (pass_through_items && gold_version < 111)
649 unsigned int i;
650 for (i = 0; i < num_pass_through_items; i++)
652 if (strncmp (pass_through_items[i], "-l", 2) == 0)
653 add_input_library (pass_through_items[i] + 2);
654 else
655 add_input_file (pass_through_items[i]);
656 free (pass_through_items[i]);
657 pass_through_items[i] = NULL;
659 free (pass_through_items);
660 pass_through_items = NULL;
663 return LDPS_OK;
666 /* Remove temporary files at the end of the link. */
668 static enum ld_plugin_status
669 cleanup_handler (void)
671 unsigned int i;
672 int t;
674 if (debug)
675 return LDPS_OK;
677 if (arguments_file_name)
679 t = unlink (arguments_file_name);
680 check (t == 0, LDPL_FATAL, "could not unlink arguments file");
683 for (i = 0; i < num_output_files; i++)
685 t = unlink (output_files[i]);
686 check (t == 0, LDPL_FATAL, "could not unlink output file");
689 free_2 ();
690 return LDPS_OK;
693 #define SWAP(type, a, b) \
694 do { type tmp_; tmp_ = (a); (a) = (b); (b) = tmp_; } while(0)
696 /* Compare two hash table entries */
698 static int eq_sym (const void *a, const void *b)
700 const struct ld_plugin_symbol *as = (const struct ld_plugin_symbol *)a;
701 const struct ld_plugin_symbol *bs = (const struct ld_plugin_symbol *)b;
703 return !strcmp (as->name, bs->name);
706 /* Hash a symbol */
708 static hashval_t hash_sym (const void *a)
710 const struct ld_plugin_symbol *as = (const struct ld_plugin_symbol *)a;
712 return htab_hash_string (as->name);
715 /* Determine how strong a symbol is */
717 static int symbol_strength (struct ld_plugin_symbol *s)
719 switch (s->def)
721 case LDPK_UNDEF:
722 case LDPK_WEAKUNDEF:
723 return 0;
724 case LDPK_WEAKDEF:
725 return 1;
726 default:
727 return 2;
731 /* In the ld -r case we can get dups in the LTO symbol tables, where
732 the same symbol can have different resolutions (e.g. undefined and defined).
734 We have to keep that in the LTO symbol tables, but the dups confuse
735 gold and then finally gcc by supplying incorrect resolutions.
737 Problem is that the main gold symbol table doesn't know about subids
738 and does not distingush the same symbols in different states.
740 So we drop duplicates from the linker visible symbol table
741 and keep them in a private table. Then later do own symbol
742 resolution for the duplicated based on the results for the
743 originals.
745 Then when writing out the resolution file readd the dropped symbols.
747 XXX how to handle common? */
749 static void
750 resolve_conflicts (struct plugin_symtab *t, struct plugin_symtab *conflicts)
752 htab_t symtab = htab_create (t->nsyms, hash_sym, eq_sym, NULL);
753 int i;
754 int out;
755 int outlen;
757 outlen = t->nsyms;
758 conflicts->syms = xmalloc (sizeof (struct ld_plugin_symbol) * outlen);
759 conflicts->aux = xmalloc (sizeof (struct sym_aux) * outlen);
761 /* Move all duplicate symbols into the auxiliary conflicts table. */
762 out = 0;
763 for (i = 0; i < t->nsyms; i++)
765 struct ld_plugin_symbol *s = &t->syms[i];
766 struct sym_aux *aux = &t->aux[i];
767 void **slot;
769 slot = htab_find_slot (symtab, s, INSERT);
770 if (*slot != NULL)
772 int cnf;
773 struct ld_plugin_symbol *orig = (struct ld_plugin_symbol *)*slot;
774 struct sym_aux *orig_aux = &t->aux[orig - t->syms];
776 /* Always let the linker resolve the strongest symbol */
777 if (symbol_strength (orig) < symbol_strength (s))
779 SWAP (struct ld_plugin_symbol, *orig, *s);
780 SWAP (uint32_t, orig_aux->slot, aux->slot);
781 SWAP (unsigned long long, orig_aux->id, aux->id);
782 /* Don't swap conflict chain pointer */
785 /* Move current symbol into the conflicts table */
786 cnf = conflicts->nsyms++;
787 conflicts->syms[cnf] = *s;
788 conflicts->aux[cnf] = *aux;
789 aux = &conflicts->aux[cnf];
791 /* Update conflicts chain of the original symbol */
792 aux->next_conflict = orig_aux->next_conflict;
793 orig_aux->next_conflict = cnf;
795 continue;
798 /* Remove previous duplicates in the main table */
799 if (out < i)
801 t->syms[out] = *s;
802 t->aux[out] = *aux;
805 /* Put original into the hash table */
806 *slot = &t->syms[out];
807 out++;
810 assert (conflicts->nsyms <= outlen);
811 assert (conflicts->nsyms + out == t->nsyms);
813 t->nsyms = out;
814 htab_delete (symtab);
817 /* Process one section of an object file. */
819 static int
820 process_symtab (void *data, const char *name, off_t offset, off_t length)
822 struct plugin_objfile *obj = (struct plugin_objfile *)data;
823 char *s;
824 char *secdatastart, *secdata;
826 if (strncmp (name, LTO_SECTION_PREFIX, LTO_SECTION_PREFIX_LEN) != 0)
827 return 1;
829 s = strrchr (name, '.');
830 if (s)
831 sscanf (s, ".%" PRI_LL "x", &obj->out->id);
832 secdata = secdatastart = xmalloc (length);
833 offset += obj->file->offset;
834 if (offset != lseek (obj->file->fd, offset, SEEK_SET))
835 goto err;
839 ssize_t got = read (obj->file->fd, secdata, length);
840 if (got == 0)
841 break;
842 else if (got > 0)
844 secdata += got;
845 length -= got;
847 else if (errno != EINTR)
848 goto err;
850 while (length > 0);
851 if (length > 0)
852 goto err;
854 translate (secdatastart, secdata, obj->out);
855 obj->found++;
856 free (secdatastart);
857 return 1;
859 err:
860 if (message)
861 message (LDPL_FATAL, "%s: corrupt object file", obj->file->name);
862 /* Force claim_file_handler to abandon this file. */
863 obj->found = 0;
864 free (secdatastart);
865 return 0;
868 /* Find an offload section of an object file. */
870 static int
871 process_offload_section (void *data, const char *name, off_t offset, off_t len)
873 if (!strncmp (name, OFFLOAD_SECTION, OFFLOAD_SECTION_LEN))
875 struct plugin_objfile *obj = (struct plugin_objfile *) data;
876 obj->offload = 1;
877 return 0;
880 return 1;
883 /* Callback used by gold to check if the plugin will claim FILE. Writes
884 the result in CLAIMED. */
886 static enum ld_plugin_status
887 claim_file_handler (const struct ld_plugin_input_file *file, int *claimed)
889 enum ld_plugin_status status;
890 struct plugin_objfile obj;
891 struct plugin_file_info lto_file;
892 int err;
893 const char *errmsg;
895 memset (&lto_file, 0, sizeof (struct plugin_file_info));
897 if (file->offset != 0)
899 char *objname;
900 /* We pass the offset of the actual file, not the archive header.
901 Can't use PRIx64, because that's C99, so we have to print the
902 64-bit hex int as two 32-bit ones. */
903 int lo, hi, t;
904 lo = file->offset & 0xffffffff;
905 hi = ((int64_t)file->offset >> 32) & 0xffffffff;
906 t = hi ? asprintf (&objname, "%s@0x%x%08x", file->name, lo, hi)
907 : asprintf (&objname, "%s@0x%x", file->name, lo);
908 check (t >= 0, LDPL_FATAL, "asprintf failed");
909 lto_file.name = objname;
911 else
913 lto_file.name = xstrdup (file->name);
915 lto_file.handle = file->handle;
917 *claimed = 0;
918 obj.file = file;
919 obj.found = 0;
920 obj.offload = 0;
921 obj.out = &lto_file.symtab;
922 errmsg = NULL;
923 obj.objfile = simple_object_start_read (file->fd, file->offset, LTO_SEGMENT_NAME,
924 &errmsg, &err);
925 /* No file, but also no error code means unrecognized format; just skip it. */
926 if (!obj.objfile && !err)
927 goto err;
929 if (obj.objfile)
930 errmsg = simple_object_find_sections (obj.objfile, process_symtab, &obj, &err);
932 if (!obj.objfile || errmsg)
934 if (err && message)
935 message (LDPL_FATAL, "%s: %s: %s", file->name, errmsg,
936 xstrerror (err));
937 else if (message)
938 message (LDPL_FATAL, "%s: %s", file->name, errmsg);
939 goto err;
942 if (obj.objfile)
943 simple_object_find_sections (obj.objfile, process_offload_section,
944 &obj, &err);
946 if (obj.found == 0 && obj.offload == 0)
947 goto err;
949 if (obj.found > 1)
950 resolve_conflicts (&lto_file.symtab, &lto_file.conflicts);
952 status = add_symbols (file->handle, lto_file.symtab.nsyms,
953 lto_file.symtab.syms);
954 check (status == LDPS_OK, LDPL_FATAL, "could not add symbols");
956 *claimed = 1;
957 num_claimed_files++;
958 claimed_files =
959 xrealloc (claimed_files,
960 num_claimed_files * sizeof (struct plugin_file_info));
961 claimed_files[num_claimed_files - 1] = lto_file;
963 goto cleanup;
965 err:
966 free (lto_file.name);
968 cleanup:
969 if (obj.objfile)
970 simple_object_release_read (obj.objfile);
972 return LDPS_OK;
975 /* Parse the plugin options. */
977 static void
978 process_option (const char *option)
980 if (strcmp (option, "-debug") == 0)
981 debug = 1;
982 else if (strcmp (option, "-nop") == 0)
983 nop = 1;
984 else if (!strncmp (option, "-pass-through=", strlen("-pass-through=")))
986 num_pass_through_items++;
987 pass_through_items = xrealloc (pass_through_items,
988 num_pass_through_items * sizeof (char *));
989 pass_through_items[num_pass_through_items - 1] =
990 xstrdup (option + strlen ("-pass-through="));
992 else if (!strncmp (option, "-sym-style=", sizeof ("-sym-style=") - 1))
994 switch (option[sizeof ("-sym-style=") - 1])
996 case 'w':
997 sym_style = ss_win32;
998 break;
999 case 'u':
1000 sym_style = ss_uscore;
1001 break;
1002 default:
1003 sym_style = ss_none;
1004 break;
1007 else
1009 int size;
1010 char *opt = xstrdup (option);
1011 lto_wrapper_num_args += 1;
1012 size = lto_wrapper_num_args * sizeof (char *);
1013 lto_wrapper_argv = (char **) xrealloc (lto_wrapper_argv, size);
1014 lto_wrapper_argv[lto_wrapper_num_args - 1] = opt;
1015 if (strncmp (option, "-fresolution=", sizeof ("-fresolution=") - 1) == 0)
1016 resolution_file = opt + sizeof ("-fresolution=") - 1;
1020 /* Called by gold after loading the plugin. TV is the transfer vector. */
1022 enum ld_plugin_status
1023 onload (struct ld_plugin_tv *tv)
1025 struct ld_plugin_tv *p;
1026 enum ld_plugin_status status;
1028 p = tv;
1029 while (p->tv_tag)
1031 switch (p->tv_tag)
1033 case LDPT_MESSAGE:
1034 message = p->tv_u.tv_message;
1035 break;
1036 case LDPT_REGISTER_CLAIM_FILE_HOOK:
1037 register_claim_file = p->tv_u.tv_register_claim_file;
1038 break;
1039 case LDPT_ADD_SYMBOLS:
1040 add_symbols = p->tv_u.tv_add_symbols;
1041 break;
1042 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
1043 register_all_symbols_read = p->tv_u.tv_register_all_symbols_read;
1044 break;
1045 case LDPT_GET_SYMBOLS_V2:
1046 get_symbols_v2 = p->tv_u.tv_get_symbols;
1047 break;
1048 case LDPT_GET_SYMBOLS:
1049 get_symbols = p->tv_u.tv_get_symbols;
1050 break;
1051 case LDPT_REGISTER_CLEANUP_HOOK:
1052 register_cleanup = p->tv_u.tv_register_cleanup;
1053 break;
1054 case LDPT_ADD_INPUT_FILE:
1055 add_input_file = p->tv_u.tv_add_input_file;
1056 break;
1057 case LDPT_ADD_INPUT_LIBRARY:
1058 add_input_library = p->tv_u.tv_add_input_library;
1059 break;
1060 case LDPT_OPTION:
1061 process_option (p->tv_u.tv_string);
1062 break;
1063 case LDPT_GOLD_VERSION:
1064 gold_version = p->tv_u.tv_val;
1065 break;
1066 default:
1067 break;
1069 p++;
1072 check (register_claim_file, LDPL_FATAL, "register_claim_file not found");
1073 check (add_symbols, LDPL_FATAL, "add_symbols not found");
1074 status = register_claim_file (claim_file_handler);
1075 check (status == LDPS_OK, LDPL_FATAL,
1076 "could not register the claim_file callback");
1078 if (register_cleanup)
1080 status = register_cleanup (cleanup_handler);
1081 check (status == LDPS_OK, LDPL_FATAL,
1082 "could not register the cleanup callback");
1085 if (register_all_symbols_read)
1087 check (get_symbols, LDPL_FATAL, "get_symbols not found");
1088 status = register_all_symbols_read (all_symbols_read_handler);
1089 check (status == LDPS_OK, LDPL_FATAL,
1090 "could not register the all_symbols_read callback");
1093 /* Support -fno-use-linker-plugin by failing to load the plugin
1094 for the case where it is auto-loaded by BFD. */
1095 char *collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
1096 if (collect_gcc_options
1097 && strstr (collect_gcc_options, "'-fno-use-linker-plugin'"))
1098 return LDPS_ERR;
1100 return LDPS_OK;