* plugin.c (is_ir_dummy_bfd): Don't segfault on NULL abfd.
[external/binutils.git] / ld / plugin.c
1 /* Plugin control for the GNU linker.
2    Copyright 2010 Free Software Foundation, Inc.
3
4    This file is part of the GNU Binutils.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20
21 #include "sysdep.h"
22 #include "libiberty.h"
23 #include "bfd.h"
24 #include "bfdlink.h"
25 #include "bfdver.h"
26 #include "ld.h"
27 #include "ldmain.h"
28 #include "ldmisc.h"
29 #include "ldexp.h"
30 #include "ldlang.h"
31 #include "ldfile.h"
32 #include "plugin.h"
33 #include "plugin-api.h"
34 #include "elf-bfd.h"
35 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
36 #include <Windows.h>
37 #endif
38
39 /* The suffix to append to the name of the real (claimed) object file
40    when generating a dummy BFD to hold the IR symbols sent from the
41    plugin.  */
42 #define IRONLY_SUFFIX           ".ironly\004"
43
44 /* This is sizeof an array of chars, not sizeof a const char *.  We
45    also have to avoid inadvertently counting the trailing NUL.  */
46 #define IRONLY_SUFFIX_LEN       (sizeof (IRONLY_SUFFIX) - 1)
47
48 /* Stores a single argument passed to a plugin.  */
49 typedef struct plugin_arg
50 {
51   struct plugin_arg *next;
52   const char *arg;
53 } plugin_arg_t;
54
55 /* Holds all details of a single plugin.  */
56 typedef struct plugin
57 {
58   /* Next on the list of plugins, or NULL at end of chain.  */
59   struct plugin *next;
60   /* The argument string given to --plugin.  */
61   const char *name;
62   /* The shared library handle returned by dlopen.  */
63   void *dlhandle;
64   /* The list of argument string given to --plugin-opt.  */
65   plugin_arg_t *args;
66   /* Number of args in the list, for convenience.  */
67   size_t n_args;
68   /* The plugin's event handlers.  */
69   ld_plugin_claim_file_handler claim_file_handler;
70   ld_plugin_all_symbols_read_handler all_symbols_read_handler;
71   ld_plugin_cleanup_handler cleanup_handler;
72   /* TRUE if the cleanup handlers have been called.  */
73   bfd_boolean cleanup_done;
74 } plugin_t;
75
76 /* The master list of all plugins.  */
77 static plugin_t *plugins_list = NULL;
78
79 /* We keep a tail pointer for easy linking on the end.  */
80 static plugin_t **plugins_tail_chain_ptr = &plugins_list;
81
82 /* The last plugin added to the list, for receiving args.  */
83 static plugin_t *last_plugin = NULL;
84
85 /* The tail of the arg chain of the last plugin added to the list.  */
86 static plugin_arg_t **last_plugin_args_tail_chain_ptr = NULL;
87
88 /* The plugin which is currently having a callback executed.  */
89 static plugin_t *called_plugin = NULL;
90
91 /* Last plugin to cause an error, if any.  */
92 static const char *error_plugin = NULL;
93
94 /* A hash table that records symbols referenced by non-IR files.  Used
95    at get_symbols time to determine whether any prevailing defs from
96    IR files are referenced only from other IR files, so tthat we can
97    we can distinguish the LDPR_PREVAILING_DEF and LDPR_PREVAILING_DEF_IRONLY
98    cases when establishing symbol resolutions.  */
99 static struct bfd_hash_table *non_ironly_hash = NULL;
100
101 /* Set at all symbols read time, to avoid recursively offering the plugin
102    its own newly-added input files and libs to claim.  */
103 static bfd_boolean no_more_claiming = FALSE;
104
105 /* If the --allow-multiple-definition command-line option is active, we
106    have to disable it so that BFD always calls our hook, and simulate the
107    effect (when not resolving IR vs. real symbols) ourselves by ensuring
108    TRUE is returned from the hook.  */
109 static bfd_boolean plugin_cached_allow_multiple_defs = FALSE;
110
111 /* List of tags to set in the constant leading part of the tv array. */
112 static const enum ld_plugin_tag tv_header_tags[] =
113 {
114   LDPT_MESSAGE,
115   LDPT_API_VERSION,
116   LDPT_GNU_LD_VERSION,
117   LDPT_LINKER_OUTPUT,
118   LDPT_OUTPUT_NAME,
119   LDPT_REGISTER_CLAIM_FILE_HOOK,
120   LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK,
121   LDPT_REGISTER_CLEANUP_HOOK,
122   LDPT_ADD_SYMBOLS,
123   LDPT_GET_INPUT_FILE,
124   LDPT_RELEASE_INPUT_FILE,
125   LDPT_GET_SYMBOLS,
126   LDPT_ADD_INPUT_FILE,
127   LDPT_ADD_INPUT_LIBRARY,
128   LDPT_SET_EXTRA_LIBRARY_PATH
129 };
130
131 /* How many entries in the constant leading part of the tv array.  */
132 static const size_t tv_header_size = ARRAY_SIZE (tv_header_tags);
133
134 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
135
136 #define RTLD_NOW 0      /* Dummy value.  */
137
138 static void *
139 dlopen (const char *file, int mode ATTRIBUTE_UNUSED)
140 {
141   return LoadLibrary (file);
142 }
143
144 static void *
145 dlsym (void *handle, const char *name)
146 {
147   return GetProcAddress (handle, name);
148 }
149
150 static int
151 dlclose (void *handle)
152 {
153   FreeLibrary (handle);
154   return 0;
155 }
156
157 #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)  */
158
159 /* Helper function for exiting with error status.  */
160 static int
161 set_plugin_error (const char *plugin)
162 {
163   error_plugin = plugin;
164   return -1;
165 }
166
167 /* Test if an error occurred.  */
168 static bfd_boolean
169 plugin_error_p (void)
170 {
171   return error_plugin != NULL;
172 }
173
174 /* Return name of plugin which caused an error if any.  */
175 const char *plugin_error_plugin (void)
176 {
177   return error_plugin ? error_plugin : _("<no plugin>");
178 }
179
180 /* Handle -plugin arg: find and load plugin, or return error.  */
181 int plugin_opt_plugin (const char *plugin)
182 {
183   plugin_t *newplug;
184
185   newplug = xmalloc (sizeof *newplug);
186   memset (newplug, 0, sizeof *newplug);
187   newplug->name = plugin;
188   newplug->dlhandle = dlopen (plugin, RTLD_NOW);
189   if (!newplug->dlhandle)
190     return set_plugin_error (plugin);
191
192   /* Chain on end, so when we run list it is in command-line order.  */
193   *plugins_tail_chain_ptr = newplug;
194   plugins_tail_chain_ptr = &newplug->next;
195
196   /* Record it as current plugin for receiving args.  */
197   last_plugin = newplug;
198   last_plugin_args_tail_chain_ptr = &newplug->args;
199   return 0;
200 }
201
202 /* Accumulate option arguments for last-loaded plugin, or return
203    error if none.  */
204 int plugin_opt_plugin_arg (const char *arg)
205 {
206   plugin_arg_t *newarg;
207
208   if (!last_plugin)
209     return set_plugin_error (_("<no plugin>"));
210
211   newarg = xmalloc (sizeof *newarg);
212   newarg->arg = arg;
213   newarg->next = NULL;
214
215   /* Chain on end to preserve command-line order.  */
216   *last_plugin_args_tail_chain_ptr = newarg;
217   last_plugin_args_tail_chain_ptr = &newarg->next;
218   last_plugin->n_args++;
219   return 0;
220 }
221
222 /* Create a dummy BFD.  */
223 bfd *
224 plugin_get_ir_dummy_bfd (const char *name, bfd *srctemplate)
225 {
226   asection *sec;
227   bfd *abfd;
228
229   bfd_use_reserved_id = 1;
230   abfd = bfd_create (concat (name, IRONLY_SUFFIX, (const char *)NULL),
231                      srctemplate);
232   bfd_set_arch_info (abfd, bfd_get_arch_info (srctemplate));
233   bfd_make_writable (abfd);
234   /* Create a minimal set of sections to own the symbols.  */
235   sec = bfd_make_section_old_way (abfd, ".text");
236   bfd_set_section_flags (abfd, sec,
237                          (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY
238                           | SEC_ALLOC | SEC_LOAD | SEC_KEEP));
239   sec->output_section = sec;
240   sec->output_offset = 0;
241   return abfd;
242 }
243
244 /* Check if the BFD is an IR dummy.  */
245 bfd_boolean
246 is_ir_dummy_bfd (const bfd *abfd)
247 {
248   size_t namlen;
249
250   if (abfd == NULL)
251     return FALSE;
252   namlen = strlen (abfd->filename);
253   if (namlen < IRONLY_SUFFIX_LEN)
254     return FALSE;
255   return !strcmp (abfd->filename + namlen - IRONLY_SUFFIX_LEN, IRONLY_SUFFIX);
256 }
257
258 /* Helpers to convert between BFD and GOLD symbol formats.  */
259 static enum ld_plugin_status
260 asymbol_from_plugin_symbol (bfd *abfd, asymbol *asym,
261                                 const struct ld_plugin_symbol *ldsym)
262 {
263   flagword flags = BSF_NO_FLAGS;
264   struct bfd_section *section;
265
266   asym->the_bfd = abfd;
267   asym->name = ldsym->version
268                 ? concat (ldsym->name, "@", ldsym->version, NULL)
269                 : ldsym->name;
270   asym->value = 0;
271   switch (ldsym->def)
272     {
273     case LDPK_WEAKDEF:
274       flags = BSF_WEAK;
275       /* FALLTHRU */
276     case LDPK_DEF:
277       flags |= BSF_GLOBAL;
278       section = bfd_get_section_by_name (abfd, ".text");
279       break;
280
281     case LDPK_WEAKUNDEF:
282       flags = BSF_WEAK;
283       /* FALLTHRU */
284     case LDPK_UNDEF:
285       section = bfd_und_section_ptr;
286       break;
287
288     case LDPK_COMMON:
289       flags = BSF_GLOBAL;
290       section = bfd_com_section_ptr;
291       asym->value = ldsym->size;
292       break;
293
294     default:
295       return LDPS_ERR;
296     }
297   asym->flags = flags;
298   asym->section = section;
299
300   /* Visibility only applies on ELF targets.  */
301   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
302     {
303       elf_symbol_type *elfsym = elf_symbol_from (abfd, asym);
304       if (!elfsym)
305         einfo (_("%P%F: %s: non-ELF symbol in ELF BFD!"), asym->name);
306       elfsym->internal_elf_sym.st_other &= ~3;
307       elfsym->internal_elf_sym.st_other |= ldsym->visibility;
308     }
309
310   return LDPS_OK;
311 }
312
313 /* Register a claim-file handler.  */
314 static enum ld_plugin_status
315 register_claim_file (ld_plugin_claim_file_handler handler)
316 {
317   ASSERT (called_plugin);
318   called_plugin->claim_file_handler = handler;
319   return LDPS_OK;
320 }
321
322 /* Register an all-symbols-read handler.  */
323 static enum ld_plugin_status
324 register_all_symbols_read (ld_plugin_all_symbols_read_handler handler)
325 {
326   ASSERT (called_plugin);
327   called_plugin->all_symbols_read_handler = handler;
328   return LDPS_OK;
329 }
330
331 /* Register a cleanup handler.  */
332 static enum ld_plugin_status
333 register_cleanup (ld_plugin_cleanup_handler handler)
334 {
335   ASSERT (called_plugin);
336   called_plugin->cleanup_handler = handler;
337   return LDPS_OK;
338 }
339
340 /* Add symbols from a plugin-claimed input file.  */
341 static enum ld_plugin_status
342 add_symbols (void *handle, int nsyms, const struct ld_plugin_symbol *syms)
343 {
344   asymbol **symptrs;
345   bfd *abfd = handle;
346   int n;
347   ASSERT (called_plugin);
348   symptrs = xmalloc (nsyms * sizeof *symptrs);
349   for (n = 0; n < nsyms; n++)
350     {
351       enum ld_plugin_status rv;
352       asymbol *bfdsym = bfd_make_empty_symbol (abfd);
353       symptrs[n] = bfdsym;
354       rv = asymbol_from_plugin_symbol (abfd, bfdsym, syms + n);
355       if (rv != LDPS_OK)
356         return rv;
357     }
358   bfd_set_symtab (abfd, symptrs, nsyms);
359   return LDPS_OK;
360 }
361
362 /* Get the input file information with an open (possibly re-opened)
363    file descriptor.  */
364 static enum ld_plugin_status
365 get_input_file (const void *handle, struct ld_plugin_input_file *file)
366 {
367   ASSERT (called_plugin);
368   handle = handle;
369   file = file;
370   return LDPS_ERR;
371 }
372
373 /* Release the input file.  */
374 static enum ld_plugin_status
375 release_input_file (const void *handle)
376 {
377   ASSERT (called_plugin);
378   handle = handle;
379   return LDPS_ERR;
380 }
381
382 /* Get the symbol resolution info for a plugin-claimed input file.  */
383 static enum ld_plugin_status
384 get_symbols (const void *handle, int nsyms, struct ld_plugin_symbol *syms)
385 {
386   const bfd *abfd = handle;
387   int n;
388   ASSERT (called_plugin);
389   for (n = 0; n < nsyms; n++)
390     {
391       struct bfd_link_hash_entry *blhe;
392       bfd_boolean ironly;
393
394       blhe = bfd_link_hash_lookup (link_info.hash, syms[n].name,
395                                 FALSE, FALSE, TRUE);
396       if (!blhe)
397         {
398           syms[n].resolution = LDPR_UNKNOWN;
399           continue;
400         }
401
402       /* Determine resolution from blhe type and symbol's original type.  */
403       if (blhe->type == bfd_link_hash_undefined
404                 || blhe->type == bfd_link_hash_undefweak)
405         {
406           syms[n].resolution = LDPR_UNDEF;
407           continue;
408         }
409       if (blhe->type != bfd_link_hash_defined
410                 && blhe->type != bfd_link_hash_defweak
411                 && blhe->type != bfd_link_hash_common)
412         {
413           /* We should not have a new, indirect or warning symbol here.  */
414           einfo ("%P%F: %s: plugin symbol table corrupt (sym type %d)",
415                 called_plugin->name, blhe->type);
416         }
417
418       /* We need to know if the sym is referenced from non-IR files.  */
419       ironly = !bfd_hash_lookup (non_ironly_hash, syms[n].name, FALSE, FALSE);
420
421       /* If it was originally undefined or common, then it has been
422          resolved; determine how.  */
423       if (syms[n].def == LDPK_UNDEF || syms[n].def == LDPK_WEAKUNDEF
424           || syms[n].def == LDPK_COMMON)
425         {
426           asection *owner_sec = (blhe->type == bfd_link_hash_common)
427                                 ? blhe->u.c.p->section
428                                 : blhe->u.def.section;
429           if (owner_sec->owner == link_info.output_bfd)
430             syms[n].resolution = LDPR_RESOLVED_EXEC;
431           else if (owner_sec->owner == abfd)
432             syms[n].resolution = (ironly)
433                                 ? LDPR_PREVAILING_DEF_IRONLY
434                                 : LDPR_PREVAILING_DEF;
435           else if (is_ir_dummy_bfd (owner_sec->owner))
436             syms[n].resolution = LDPR_RESOLVED_IR;
437           else if (owner_sec->owner->flags & DYNAMIC)
438             syms[n].resolution =  LDPR_RESOLVED_DYN;
439           else
440             syms[n].resolution = LDPR_RESOLVED_EXEC;
441           continue;
442         }
443
444       /* Was originally def, or weakdef.  Does it prevail?  If the
445          owner is the original dummy bfd that supplied it, then this
446          is the definition that has prevailed.  */
447       if (blhe->u.def.section->owner == link_info.output_bfd)
448         syms[n].resolution = LDPR_PREEMPTED_REG;
449       else if (blhe->u.def.section->owner == abfd)
450         {
451           syms[n].resolution = (ironly)
452                                 ? LDPR_PREVAILING_DEF_IRONLY
453                                 : LDPR_PREVAILING_DEF;
454           continue;
455         }
456
457       /* Was originally def, weakdef, or common, but has been pre-empted.  */
458       syms[n].resolution = is_ir_dummy_bfd (blhe->u.def.section->owner)
459                                 ? LDPR_PREEMPTED_IR
460                                 : LDPR_PREEMPTED_REG;
461     }
462   return LDPS_OK;
463 }
464
465 /* Add a new (real) input file generated by a plugin.  */
466 static enum ld_plugin_status
467 add_input_file (const char *pathname)
468 {
469   ASSERT (called_plugin);
470   if (!lang_add_input_file (xstrdup (pathname), lang_input_file_is_file_enum,
471         NULL))
472     return LDPS_ERR;
473   return LDPS_OK;
474 }
475
476 /* Add a new (real) library required by a plugin.  */
477 static enum ld_plugin_status
478 add_input_library (const char *pathname)
479 {
480   ASSERT (called_plugin);
481   if (!lang_add_input_file (xstrdup (pathname), lang_input_file_is_l_enum,
482         NULL))
483     return LDPS_ERR;
484   return LDPS_OK;
485 }
486
487 /* Set the extra library path to be used by libraries added via
488    add_input_library.  */
489 static enum ld_plugin_status
490 set_extra_library_path (const char *path)
491 {
492   ASSERT (called_plugin);
493   ldfile_add_library_path (xstrdup (path), FALSE);
494   return LDPS_OK;
495 }
496
497 /* Issue a diagnostic message from a plugin.  */
498 static enum ld_plugin_status
499 message (int level, const char *format, ...)
500 {
501   va_list args;
502   va_start (args, format);
503
504   switch (level)
505     {
506     case LDPL_INFO:
507       vfinfo (stdout, format, args, FALSE);
508       break;
509     case LDPL_WARNING:
510       vfinfo (stdout, format, args, TRUE);
511       break;
512     case LDPL_FATAL:
513     case LDPL_ERROR:
514     default:
515       {
516         char *newfmt = ACONCAT ((level == LDPL_FATAL ? "%F" : "%X",
517                                 format, NULL));
518         vfinfo (stderr, newfmt, args, TRUE);
519       }
520       break;
521     }
522
523   va_end (args);
524   return LDPS_OK;
525 }
526
527 /* Helper to size leading part of tv array and set it up. */
528 static size_t
529 set_tv_header (struct ld_plugin_tv *tv)
530 {
531   size_t i;
532
533   /* Version info.  */
534   static const unsigned int major = (unsigned)(BFD_VERSION / 100000000UL);
535   static const unsigned int minor = (unsigned)(BFD_VERSION / 1000000UL) % 100;
536
537   if (!tv)
538     return tv_header_size;
539
540   for (i = 0; i < tv_header_size; i++)
541     {
542       tv[i].tv_tag = tv_header_tags[i];
543 #define TVU(x) tv[i].tv_u.tv_ ## x
544       switch (tv[i].tv_tag)
545         {
546           case LDPT_MESSAGE:
547             TVU(message) = message;
548             break;
549           case LDPT_API_VERSION:
550             TVU(val) = LD_PLUGIN_API_VERSION;
551             break;
552           case LDPT_GNU_LD_VERSION:
553             TVU(val) = major * 100 + minor;
554             break;
555           case LDPT_LINKER_OUTPUT:
556             TVU(val) = link_info.relocatable ? LDPO_REL
557                         : (link_info.shared ? LDPO_DYN : LDPO_EXEC);
558             break;
559           case LDPT_OUTPUT_NAME:
560             TVU(string) = output_filename;
561             break;
562           case LDPT_REGISTER_CLAIM_FILE_HOOK:
563             TVU(register_claim_file) = register_claim_file;
564             break;
565           case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
566             TVU(register_all_symbols_read) = register_all_symbols_read;
567             break;
568           case LDPT_REGISTER_CLEANUP_HOOK:
569             TVU(register_cleanup) = register_cleanup;
570             break;
571           case LDPT_ADD_SYMBOLS:
572             TVU(add_symbols) = add_symbols;
573             break;
574           case LDPT_GET_INPUT_FILE:
575             TVU(get_input_file) = get_input_file;
576             break;
577           case LDPT_RELEASE_INPUT_FILE:
578             TVU(release_input_file) = release_input_file;
579             break;
580           case LDPT_GET_SYMBOLS:
581             TVU(get_symbols) = get_symbols;
582             break;
583           case LDPT_ADD_INPUT_FILE:
584             TVU(add_input_file) = add_input_file;
585             break;
586           case LDPT_ADD_INPUT_LIBRARY:
587             TVU(add_input_library) = add_input_library;
588             break;
589           case LDPT_SET_EXTRA_LIBRARY_PATH:
590             TVU(set_extra_library_path) = set_extra_library_path;
591             break;
592           default:
593             /* Added a new entry to the array without adding
594                a new case to set up its value is a bug.  */
595             FAIL ();
596         }
597 #undef TVU
598     }
599   return tv_header_size;
600 }
601
602 /* Append the per-plugin args list and trailing LDPT_NULL to tv.  */
603 static void
604 set_tv_plugin_args (plugin_t *plugin, struct ld_plugin_tv *tv)
605 {
606   plugin_arg_t *arg = plugin->args;
607   while (arg)
608     {
609       tv->tv_tag = LDPT_OPTION;
610       tv->tv_u.tv_string = arg->arg;
611       arg = arg->next;
612       tv++;
613     }
614   tv->tv_tag = LDPT_NULL;
615   tv->tv_u.tv_val = 0;
616 }
617
618 /* Load up and initialise all plugins after argument parsing.  */
619 int plugin_load_plugins (void)
620 {
621   struct ld_plugin_tv *my_tv;
622   unsigned int max_args = 0;
623   plugin_t *curplug = plugins_list;
624
625   /* If there are no plugins, we need do nothing this run.  */
626   if (!curplug)
627     return 0;
628
629   /* First pass over plugins to find max # args needed so that we
630      can size and allocate the tv array.  */
631   while (curplug)
632     {
633       if (curplug->n_args > max_args)
634         max_args = curplug->n_args;
635       curplug = curplug->next;
636     }
637
638   /* Allocate tv array and initialise constant part.  */
639   my_tv = xmalloc ((max_args + 1 + tv_header_size) * sizeof *my_tv);
640   set_tv_header (my_tv);
641
642   /* Pass over plugins again, activating them.  */
643   curplug = plugins_list;
644   while (curplug)
645     {
646       enum ld_plugin_status rv;
647       ld_plugin_onload onloadfn = dlsym (curplug->dlhandle, "onload");
648       if (!onloadfn)
649         onloadfn = dlsym (curplug->dlhandle, "_onload");
650       if (!onloadfn)
651         return set_plugin_error (curplug->name);
652       set_tv_plugin_args (curplug, &my_tv[tv_header_size]);
653       called_plugin = curplug;
654       rv = (*onloadfn) (my_tv);
655       called_plugin = NULL;
656       if (rv != LDPS_OK)
657         return set_plugin_error (curplug->name);
658       curplug = curplug->next;
659     }
660
661   /* Since plugin(s) inited ok, assume they're going to want symbol
662      resolutions, which needs us to track which symbols are referenced
663      by non-IR files using the linker's notice callback.  */
664   link_info.notice_all = TRUE;
665
666   return 0;
667 }
668
669 /* Call 'claim file' hook for all plugins.  */
670 int
671 plugin_call_claim_file (const struct ld_plugin_input_file *file, int *claimed)
672 {
673   plugin_t *curplug = plugins_list;
674   *claimed = FALSE;
675   if (no_more_claiming)
676     return 0;
677   while (curplug && !*claimed)
678     {
679       if (curplug->claim_file_handler)
680         {
681           enum ld_plugin_status rv;
682           called_plugin = curplug;
683           rv = (*curplug->claim_file_handler) (file, claimed);
684           called_plugin = NULL;
685           if (rv != LDPS_OK)
686             set_plugin_error (curplug->name);
687         }
688       curplug = curplug->next;
689     }
690   return plugin_error_p () ? -1 : 0;
691 }
692
693 /* Call 'all symbols read' hook for all plugins.  */
694 int
695 plugin_call_all_symbols_read (void)
696 {
697   plugin_t *curplug = plugins_list;
698
699   /* Disable any further file-claiming.  */
700   no_more_claiming = TRUE;
701
702   /* If --allow-multiple-definition is in effect, we need to disable it,
703      as the plugin infrastructure relies on the multiple_definition
704      callback to swap out the dummy IR-only BFDs for new real ones
705      when it starts opening the files added during this callback.  */
706   plugin_cached_allow_multiple_defs = link_info.allow_multiple_definition;
707   link_info.allow_multiple_definition = FALSE;
708
709   while (curplug)
710     {
711       if (curplug->all_symbols_read_handler)
712         {
713           enum ld_plugin_status rv;
714           called_plugin = curplug;
715           rv = (*curplug->all_symbols_read_handler) ();
716           called_plugin = NULL;
717           if (rv != LDPS_OK)
718             set_plugin_error (curplug->name);
719         }
720       curplug = curplug->next;
721     }
722   return plugin_error_p () ? -1 : 0;
723 }
724
725 /* Call 'cleanup' hook for all plugins.  */
726 int
727 plugin_call_cleanup (void)
728 {
729   plugin_t *curplug = plugins_list;
730   while (curplug)
731     {
732       if (curplug->cleanup_handler && !curplug->cleanup_done)
733         {
734           enum ld_plugin_status rv;
735           curplug->cleanup_done = TRUE;
736           called_plugin = curplug;
737           rv = (*curplug->cleanup_handler) ();
738           called_plugin = NULL;
739           if (rv != LDPS_OK)
740             set_plugin_error (curplug->name);
741           dlclose (curplug->dlhandle);
742         }
743       curplug = curplug->next;
744     }
745   return plugin_error_p () ? -1 : 0;
746 }
747
748 /* Lazily init the non_ironly hash table.  */
749 static void
750 init_non_ironly_hash (void)
751 {
752   if (non_ironly_hash == NULL)
753     {
754       non_ironly_hash =
755           (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
756       if (!bfd_hash_table_init_n (non_ironly_hash,
757                                   bfd_hash_newfunc,
758                                   sizeof (struct bfd_hash_entry),
759                                   61))
760         einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
761     }
762 }
763
764 /* To determine which symbols should be resolved LDPR_PREVAILING_DEF
765    and which LDPR_PREVAILING_DEF_IRONLY, we notice all the symbols as
766    the linker adds them to the linker hash table.  If we see a symbol
767    being referenced from a non-IR file, we add it to the non_ironly hash
768    table.  If we can't find it there at get_symbols time, we know that
769    it was referenced only by IR files.  We have to notice_all symbols,
770    because we won't necessarily know until later which ones will be
771    contributed by IR files.  */
772 bfd_boolean
773 plugin_notice (struct bfd_link_info *info ATTRIBUTE_UNUSED,
774                 const char *name, bfd *abfd,
775                 asection *section, bfd_vma value ATTRIBUTE_UNUSED)
776 {
777   bfd_boolean is_ref = bfd_is_und_section (section);
778   bfd_boolean is_dummy = is_ir_dummy_bfd (abfd);
779   init_non_ironly_hash ();
780   /* We only care about refs, not defs, indicated by section pointing
781      to the undefined section (according to the bfd linker notice callback
782      interface definition).  */
783   if (is_ref && !is_dummy)
784     {
785       /* This is a ref from a non-IR file, so note the ref'd symbol
786          in the non-IR-only hash.  */
787       if (!bfd_hash_lookup (non_ironly_hash, name, TRUE, TRUE))
788         einfo (_("%P%X: %s: hash table failure adding symbol %s"),
789                 abfd->filename, name);
790     }
791   else if (!is_ref && is_dummy)
792     {
793       /* No further processing since this is a def from an IR dummy BFD.  */
794       return FALSE;
795     }
796
797   /* Continue with cref/nocrossref/trace-sym processing.  */
798   return TRUE;
799 }
800
801 /* When we add new object files to the link at all symbols read time,
802    these contain the real code and symbols generated from the IR files,
803    and so duplicate all the definitions already supplied by the dummy
804    IR-only BFDs that we created at claim files time.  We use the linker's
805    multiple-definitions callback hook to fix up the clash, discarding
806    the symbol from the IR-only BFD in favour of the symbol from the
807    real BFD.  We return true if this was not-really-a-clash because
808    we've fixed it up, or anyway if --allow-multiple-definition was in
809    effect (before we disabled it to ensure we got called back).  */
810 bfd_boolean
811 plugin_multiple_definition (struct bfd_link_info *info, const char *name,
812                 bfd *obfd, asection *osec ATTRIBUTE_UNUSED,
813                 bfd_vma oval ATTRIBUTE_UNUSED,
814                 bfd *nbfd, asection *nsec, bfd_vma nval)
815 {
816   if (is_ir_dummy_bfd (obfd))
817     {
818       struct bfd_link_hash_entry *blhe = bfd_link_hash_lookup (info->hash,
819                                         name, FALSE, FALSE, FALSE);
820       if (!blhe)
821         einfo (_("%P%X: %s: can't find IR symbol '%s'"), nbfd->filename,
822                 name);
823       else if (blhe->type != bfd_link_hash_defined)
824         einfo (_("%P%x: %s: bad IR symbol type %d"), name, blhe->type);
825       /* Replace it with new details.  */
826       blhe->u.def.section = nsec;
827       blhe->u.def.value = nval;
828       return TRUE;
829     }
830   return plugin_cached_allow_multiple_defs;
831 }