1 /* Plugin control for the GNU linker.
2 Copyright 2010, 2011 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
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.
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.
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. */
22 #include "libiberty.h"
33 #include "plugin-api.h"
35 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
39 /* Report plugin symbols. */
40 bfd_boolean report_plugin_symbols;
42 /* The suffix to append to the name of the real (claimed) object file
43 when generating a dummy BFD to hold the IR symbols sent from the
44 plugin. For cosmetic use only; appears in maps, crefs etc. */
45 #define IRONLY_SUFFIX " (symbol from plugin)"
47 /* Stores a single argument passed to a plugin. */
48 typedef struct plugin_arg
50 struct plugin_arg *next;
54 /* Holds all details of a single plugin. */
57 /* Next on the list of plugins, or NULL at end of chain. */
59 /* The argument string given to --plugin. */
61 /* The shared library handle returned by dlopen. */
63 /* The list of argument string given to --plugin-opt. */
65 /* Number of args in the list, for convenience. */
67 /* The plugin's event handlers. */
68 ld_plugin_claim_file_handler claim_file_handler;
69 ld_plugin_all_symbols_read_handler all_symbols_read_handler;
70 ld_plugin_cleanup_handler cleanup_handler;
71 /* TRUE if the cleanup handlers have been called. */
72 bfd_boolean cleanup_done;
75 /* The master list of all plugins. */
76 static plugin_t *plugins_list = NULL;
78 /* We keep a tail pointer for easy linking on the end. */
79 static plugin_t **plugins_tail_chain_ptr = &plugins_list;
81 /* The last plugin added to the list, for receiving args. */
82 static plugin_t *last_plugin = NULL;
84 /* The tail of the arg chain of the last plugin added to the list. */
85 static plugin_arg_t **last_plugin_args_tail_chain_ptr = NULL;
87 /* The plugin which is currently having a callback executed. */
88 static plugin_t *called_plugin = NULL;
90 /* Last plugin to cause an error, if any. */
91 static const char *error_plugin = NULL;
93 /* A hash table that records symbols referenced by non-IR files. Used
94 at get_symbols time to determine whether any prevailing defs from
95 IR files are referenced only from other IR files, so tthat we can
96 we can distinguish the LDPR_PREVAILING_DEF and LDPR_PREVAILING_DEF_IRONLY
97 cases when establishing symbol resolutions. */
98 static struct bfd_hash_table *non_ironly_hash = NULL;
100 /* Set at all symbols read time, to avoid recursively offering the plugin
101 its own newly-added input files and libs to claim. */
102 static bfd_boolean no_more_claiming = FALSE;
104 /* If the --allow-multiple-definition command-line option is active, we
105 have to disable it so that BFD always calls our hook, and simulate the
106 effect (when not resolving IR vs. real symbols) ourselves by ensuring
107 TRUE is returned from the hook. */
108 static bfd_boolean plugin_cached_allow_multiple_defs = FALSE;
110 /* List of tags to set in the constant leading part of the tv array. */
111 static const enum ld_plugin_tag tv_header_tags[] =
118 LDPT_REGISTER_CLAIM_FILE_HOOK,
119 LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK,
120 LDPT_REGISTER_CLEANUP_HOOK,
123 LDPT_RELEASE_INPUT_FILE,
126 LDPT_ADD_INPUT_LIBRARY,
127 LDPT_SET_EXTRA_LIBRARY_PATH
130 /* How many entries in the constant leading part of the tv array. */
131 static const size_t tv_header_size = ARRAY_SIZE (tv_header_tags);
133 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
135 #define RTLD_NOW 0 /* Dummy value. */
138 dlopen (const char *file, int mode ATTRIBUTE_UNUSED)
140 return LoadLibrary (file);
144 dlsym (void *handle, const char *name)
146 return GetProcAddress (handle, name);
150 dlclose (void *handle)
152 FreeLibrary (handle);
156 #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */
158 /* Helper function for exiting with error status. */
160 set_plugin_error (const char *plugin)
162 error_plugin = plugin;
166 /* Test if an error occurred. */
168 plugin_error_p (void)
170 return error_plugin != NULL;
173 /* Return name of plugin which caused an error if any. */
175 plugin_error_plugin (void)
177 return error_plugin ? error_plugin : _("<no plugin>");
180 /* Handle -plugin arg: find and load plugin, or return error. */
182 plugin_opt_plugin (const char *plugin)
186 newplug = xmalloc (sizeof *newplug);
187 memset (newplug, 0, sizeof *newplug);
188 newplug->name = plugin;
189 newplug->dlhandle = dlopen (plugin, RTLD_NOW);
190 if (!newplug->dlhandle)
191 return set_plugin_error (plugin);
193 /* Chain on end, so when we run list it is in command-line order. */
194 *plugins_tail_chain_ptr = newplug;
195 plugins_tail_chain_ptr = &newplug->next;
197 /* Record it as current plugin for receiving args. */
198 last_plugin = newplug;
199 last_plugin_args_tail_chain_ptr = &newplug->args;
203 /* Accumulate option arguments for last-loaded plugin, or return
206 plugin_opt_plugin_arg (const char *arg)
208 plugin_arg_t *newarg;
211 return set_plugin_error (_("<no plugin>"));
213 newarg = xmalloc (sizeof *newarg);
217 /* Chain on end to preserve command-line order. */
218 *last_plugin_args_tail_chain_ptr = newarg;
219 last_plugin_args_tail_chain_ptr = &newarg->next;
220 last_plugin->n_args++;
224 /* Create a dummy BFD. */
226 plugin_get_ir_dummy_bfd (const char *name, bfd *srctemplate)
231 bfd_use_reserved_id = 1;
232 abfd = bfd_create (concat (name, IRONLY_SUFFIX, (const char *)NULL),
234 bfd_set_arch_info (abfd, bfd_get_arch_info (srctemplate));
235 bfd_make_writable (abfd);
236 bfd_copy_private_bfd_data (srctemplate, abfd);
237 bfd_set_gp_size (abfd, bfd_get_gp_size (abfd));
238 /* Create a minimal set of sections to own the symbols. */
239 sec = bfd_make_section_old_way (abfd, ".text");
240 bfd_set_section_flags (abfd, sec,
241 (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY
242 | SEC_ALLOC | SEC_LOAD | SEC_KEEP));
243 sec->output_section = sec;
244 sec->output_offset = 0;
248 /* Check if the BFD passed in is an IR dummy object file. */
250 is_ir_dummy_bfd (const bfd *abfd)
252 /* ABFD can sometimes legitimately be NULL, e.g. when called from one
253 of the linker callbacks for a symbol in the *ABS* or *UND* sections.
254 Likewise, the usrdata field may be NULL if ABFD was added by the
255 backend without a corresponding input statement, as happens e.g.
256 when processing DT_NEEDED dependencies. */
259 && ((lang_input_statement_type *)(abfd->usrdata))->claimed;
262 /* Helpers to convert between BFD and GOLD symbol formats. */
263 static enum ld_plugin_status
264 asymbol_from_plugin_symbol (bfd *abfd, asymbol *asym,
265 const struct ld_plugin_symbol *ldsym)
267 flagword flags = BSF_NO_FLAGS;
268 struct bfd_section *section;
270 asym->the_bfd = abfd;
271 asym->name = (ldsym->version
272 ? concat (ldsym->name, "@", ldsym->version, NULL)
282 section = bfd_get_section_by_name (abfd, ".text");
289 section = bfd_und_section_ptr;
294 section = bfd_com_section_ptr;
295 asym->value = ldsym->size;
296 /* For ELF targets, set alignment of common symbol to 1. */
297 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
298 ((elf_symbol_type *) asym)->internal_elf_sym.st_value = 1;
305 asym->section = section;
307 /* Visibility only applies on ELF targets. */
308 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
310 elf_symbol_type *elfsym = elf_symbol_from (abfd, asym);
311 unsigned char visibility;
314 einfo (_("%P%F: %s: non-ELF symbol in ELF BFD!\n"), asym->name);
315 switch (ldsym->visibility)
318 einfo (_("%P%F: unknown ELF symbol visibility: %d!\n"),
321 visibility = STV_DEFAULT;
324 visibility = STV_PROTECTED;
327 visibility = STV_INTERNAL;
330 visibility = STV_HIDDEN;
333 elfsym->internal_elf_sym.st_other
334 = (visibility | (elfsym->internal_elf_sym.st_other
335 & ~ELF_ST_VISIBILITY (-1)));
341 /* Register a claim-file handler. */
342 static enum ld_plugin_status
343 register_claim_file (ld_plugin_claim_file_handler handler)
345 ASSERT (called_plugin);
346 called_plugin->claim_file_handler = handler;
350 /* Register an all-symbols-read handler. */
351 static enum ld_plugin_status
352 register_all_symbols_read (ld_plugin_all_symbols_read_handler handler)
354 ASSERT (called_plugin);
355 called_plugin->all_symbols_read_handler = handler;
359 /* Register a cleanup handler. */
360 static enum ld_plugin_status
361 register_cleanup (ld_plugin_cleanup_handler handler)
363 ASSERT (called_plugin);
364 called_plugin->cleanup_handler = handler;
368 /* Add symbols from a plugin-claimed input file. */
369 static enum ld_plugin_status
370 add_symbols (void *handle, int nsyms, const struct ld_plugin_symbol *syms)
375 ASSERT (called_plugin);
376 symptrs = xmalloc (nsyms * sizeof *symptrs);
377 for (n = 0; n < nsyms; n++)
379 enum ld_plugin_status rv;
380 asymbol *bfdsym = bfd_make_empty_symbol (abfd);
382 rv = asymbol_from_plugin_symbol (abfd, bfdsym, syms + n);
386 bfd_set_symtab (abfd, symptrs, nsyms);
390 /* Get the input file information with an open (possibly re-opened)
392 static enum ld_plugin_status
393 get_input_file (const void *handle, struct ld_plugin_input_file *file)
395 ASSERT (called_plugin);
401 /* Release the input file. */
402 static enum ld_plugin_status
403 release_input_file (const void *handle)
405 ASSERT (called_plugin);
410 /* Return TRUE if a defined symbol might be reachable from outside the
411 universe of claimed objects. */
412 static inline bfd_boolean
413 is_visible_from_outside (struct ld_plugin_symbol *lsym, asection *section,
414 struct bfd_link_hash_entry *blhe)
416 /* Section's owner may be NULL if it is the absolute
417 section, fortunately is_ir_dummy_bfd handles that. */
418 if (!is_ir_dummy_bfd (section->owner))
420 if (link_info.relocatable)
422 if (link_info.export_dynamic || link_info.shared)
424 /* Only ELF symbols really have visibility. */
425 if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
427 struct elf_link_hash_entry *el = (struct elf_link_hash_entry *)blhe;
428 int vis = ELF_ST_VISIBILITY (el->other);
429 return vis == STV_DEFAULT || vis == STV_PROTECTED;
431 /* On non-ELF targets, we can safely make inferences by considering
432 what visibility the plugin would have liked to apply when it first
433 sent us the symbol. During ELF symbol processing, visibility only
434 ever becomes more restrictive, not less, when symbols are merged,
435 so this is a conservative estimate; it may give false positives,
436 declaring something visible from outside when it in fact would
437 not have been, but this will only lead to missed optimisation
438 opportunities during LTRANS at worst; it will not give false
439 negatives, which can lead to the disastrous conclusion that the
440 related symbol is IRONLY. (See GCC PR46319 for an example.) */
441 return (lsym->visibility == LDPV_DEFAULT
442 || lsym->visibility == LDPV_PROTECTED);
447 /* Get the symbol resolution info for a plugin-claimed input file. */
448 static enum ld_plugin_status
449 get_symbols (const void *handle, int nsyms, struct ld_plugin_symbol *syms)
451 const bfd *abfd = handle;
453 ASSERT (called_plugin);
454 for (n = 0; n < nsyms; n++)
456 struct bfd_link_hash_entry *blhe;
460 blhe = bfd_link_hash_lookup (link_info.hash, syms[n].name,
464 syms[n].resolution = LDPR_UNKNOWN;
468 /* Determine resolution from blhe type and symbol's original type. */
469 if (blhe->type == bfd_link_hash_undefined
470 || blhe->type == bfd_link_hash_undefweak)
472 syms[n].resolution = LDPR_UNDEF;
475 if (blhe->type != bfd_link_hash_defined
476 && blhe->type != bfd_link_hash_defweak
477 && blhe->type != bfd_link_hash_common)
479 /* We should not have a new, indirect or warning symbol here. */
480 einfo ("%P%F: %s: plugin symbol table corrupt (sym type %d)\n",
481 called_plugin->name, blhe->type);
484 /* Find out which section owns the symbol. Since it's not undef,
485 it must have an owner; if it's not a common symbol, both defs
486 and weakdefs keep it in the same place. */
487 owner_sec = (blhe->type == bfd_link_hash_common)
488 ? blhe->u.c.p->section
489 : blhe->u.def.section;
491 /* We need to know if the sym is referenced from non-IR files. Or
492 even potentially-referenced, perhaps in a future final link if
493 this is a partial one, perhaps dynamically at load-time if the
494 symbol is externally visible. */
495 ironly = (!is_visible_from_outside (&syms[n], owner_sec, blhe)
496 && !bfd_hash_lookup (non_ironly_hash, syms[n].name,
499 /* If it was originally undefined or common, then it has been
500 resolved; determine how. */
501 if (syms[n].def == LDPK_UNDEF
502 || syms[n].def == LDPK_WEAKUNDEF
503 || syms[n].def == LDPK_COMMON)
505 if (owner_sec->owner == link_info.output_bfd)
506 syms[n].resolution = LDPR_RESOLVED_EXEC;
507 else if (owner_sec->owner == abfd)
508 syms[n].resolution = (ironly
509 ? LDPR_PREVAILING_DEF_IRONLY
510 : LDPR_PREVAILING_DEF);
511 else if (is_ir_dummy_bfd (owner_sec->owner))
512 syms[n].resolution = LDPR_RESOLVED_IR;
513 else if (owner_sec->owner != NULL
514 && (owner_sec->owner->flags & DYNAMIC) != 0)
515 syms[n].resolution = LDPR_RESOLVED_DYN;
517 syms[n].resolution = LDPR_RESOLVED_EXEC;
521 /* Was originally def, or weakdef. Does it prevail? If the
522 owner is the original dummy bfd that supplied it, then this
523 is the definition that has prevailed. */
524 if (owner_sec->owner == link_info.output_bfd)
525 syms[n].resolution = LDPR_PREEMPTED_REG;
526 else if (owner_sec->owner == abfd)
528 syms[n].resolution = (ironly
529 ? LDPR_PREVAILING_DEF_IRONLY
530 : LDPR_PREVAILING_DEF);
534 /* Was originally def, weakdef, or common, but has been pre-empted. */
535 syms[n].resolution = (is_ir_dummy_bfd (owner_sec->owner)
537 : LDPR_PREEMPTED_REG);
540 if (report_plugin_symbols)
541 einfo ("%P: %B: symbol `%s' definition: %d, resolution: %d\n",
542 abfd, syms[n].name, syms[n].def, syms[n].resolution);
547 /* Add a new (real) input file generated by a plugin. */
548 static enum ld_plugin_status
549 add_input_file (const char *pathname)
551 ASSERT (called_plugin);
552 if (!lang_add_input_file (xstrdup (pathname), lang_input_file_is_file_enum,
558 /* Add a new (real) library required by a plugin. */
559 static enum ld_plugin_status
560 add_input_library (const char *pathname)
562 ASSERT (called_plugin);
563 if (!lang_add_input_file (xstrdup (pathname), lang_input_file_is_l_enum,
569 /* Set the extra library path to be used by libraries added via
570 add_input_library. */
571 static enum ld_plugin_status
572 set_extra_library_path (const char *path)
574 ASSERT (called_plugin);
575 ldfile_add_library_path (xstrdup (path), FALSE);
579 /* Issue a diagnostic message from a plugin. */
580 static enum ld_plugin_status
581 message (int level, const char *format, ...)
584 va_start (args, format);
589 vfinfo (stdout, format, args, FALSE);
593 vfinfo (stdout, format, args, TRUE);
600 char *newfmt = ACONCAT ((level == LDPL_FATAL
601 ? "%P%F: " : "%P%X: ",
602 format, "\n", NULL));
604 vfinfo (stderr, newfmt, args, TRUE);
614 /* Helper to size leading part of tv array and set it up. */
616 set_tv_header (struct ld_plugin_tv *tv)
621 static const unsigned int major = (unsigned)(BFD_VERSION / 100000000UL);
622 static const unsigned int minor = (unsigned)(BFD_VERSION / 1000000UL) % 100;
625 return tv_header_size;
627 for (i = 0; i < tv_header_size; i++)
629 tv[i].tv_tag = tv_header_tags[i];
630 #define TVU(x) tv[i].tv_u.tv_ ## x
631 switch (tv[i].tv_tag)
634 TVU(message) = message;
636 case LDPT_API_VERSION:
637 TVU(val) = LD_PLUGIN_API_VERSION;
639 case LDPT_GNU_LD_VERSION:
640 TVU(val) = major * 100 + minor;
642 case LDPT_LINKER_OUTPUT:
643 TVU(val) = (link_info.relocatable
645 : (link_info.shared ? LDPO_DYN : LDPO_EXEC));
647 case LDPT_OUTPUT_NAME:
648 TVU(string) = output_filename;
650 case LDPT_REGISTER_CLAIM_FILE_HOOK:
651 TVU(register_claim_file) = register_claim_file;
653 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
654 TVU(register_all_symbols_read) = register_all_symbols_read;
656 case LDPT_REGISTER_CLEANUP_HOOK:
657 TVU(register_cleanup) = register_cleanup;
659 case LDPT_ADD_SYMBOLS:
660 TVU(add_symbols) = add_symbols;
662 case LDPT_GET_INPUT_FILE:
663 TVU(get_input_file) = get_input_file;
665 case LDPT_RELEASE_INPUT_FILE:
666 TVU(release_input_file) = release_input_file;
668 case LDPT_GET_SYMBOLS:
669 TVU(get_symbols) = get_symbols;
671 case LDPT_ADD_INPUT_FILE:
672 TVU(add_input_file) = add_input_file;
674 case LDPT_ADD_INPUT_LIBRARY:
675 TVU(add_input_library) = add_input_library;
677 case LDPT_SET_EXTRA_LIBRARY_PATH:
678 TVU(set_extra_library_path) = set_extra_library_path;
681 /* Added a new entry to the array without adding
682 a new case to set up its value is a bug. */
687 return tv_header_size;
690 /* Append the per-plugin args list and trailing LDPT_NULL to tv. */
692 set_tv_plugin_args (plugin_t *plugin, struct ld_plugin_tv *tv)
694 plugin_arg_t *arg = plugin->args;
697 tv->tv_tag = LDPT_OPTION;
698 tv->tv_u.tv_string = arg->arg;
702 tv->tv_tag = LDPT_NULL;
706 /* Return true if any plugins are active this run. Only valid
707 after options have been processed. */
709 plugin_active_plugins_p (void)
711 return plugins_list != NULL;
714 /* Load up and initialise all plugins after argument parsing. */
716 plugin_load_plugins (void)
718 struct ld_plugin_tv *my_tv;
719 unsigned int max_args = 0;
720 plugin_t *curplug = plugins_list;
722 /* If there are no plugins, we need do nothing this run. */
726 /* First pass over plugins to find max # args needed so that we
727 can size and allocate the tv array. */
730 if (curplug->n_args > max_args)
731 max_args = curplug->n_args;
732 curplug = curplug->next;
735 /* Allocate tv array and initialise constant part. */
736 my_tv = xmalloc ((max_args + 1 + tv_header_size) * sizeof *my_tv);
737 set_tv_header (my_tv);
739 /* Pass over plugins again, activating them. */
740 curplug = plugins_list;
743 enum ld_plugin_status rv;
744 ld_plugin_onload onloadfn = dlsym (curplug->dlhandle, "onload");
746 onloadfn = dlsym (curplug->dlhandle, "_onload");
748 return set_plugin_error (curplug->name);
749 set_tv_plugin_args (curplug, &my_tv[tv_header_size]);
750 called_plugin = curplug;
751 rv = (*onloadfn) (my_tv);
752 called_plugin = NULL;
754 return set_plugin_error (curplug->name);
755 curplug = curplug->next;
758 /* Since plugin(s) inited ok, assume they're going to want symbol
759 resolutions, which needs us to track which symbols are referenced
760 by non-IR files using the linker's notice callback. */
761 link_info.notice_all = TRUE;
766 /* Call 'claim file' hook for all plugins. */
768 plugin_call_claim_file (const struct ld_plugin_input_file *file, int *claimed)
770 plugin_t *curplug = plugins_list;
772 if (no_more_claiming)
774 while (curplug && !*claimed)
776 if (curplug->claim_file_handler)
778 enum ld_plugin_status rv;
779 called_plugin = curplug;
780 rv = (*curplug->claim_file_handler) (file, claimed);
781 called_plugin = NULL;
783 set_plugin_error (curplug->name);
785 curplug = curplug->next;
787 return plugin_error_p () ? -1 : 0;
790 /* Call 'all symbols read' hook for all plugins. */
792 plugin_call_all_symbols_read (void)
794 plugin_t *curplug = plugins_list;
796 /* Disable any further file-claiming. */
797 no_more_claiming = TRUE;
799 /* If --allow-multiple-definition is in effect, we need to disable it,
800 as the plugin infrastructure relies on the multiple_definition
801 callback to swap out the dummy IR-only BFDs for new real ones
802 when it starts opening the files added during this callback. */
803 plugin_cached_allow_multiple_defs = link_info.allow_multiple_definition;
804 link_info.allow_multiple_definition = FALSE;
808 if (curplug->all_symbols_read_handler)
810 enum ld_plugin_status rv;
811 called_plugin = curplug;
812 rv = (*curplug->all_symbols_read_handler) ();
813 called_plugin = NULL;
815 set_plugin_error (curplug->name);
817 curplug = curplug->next;
819 return plugin_error_p () ? -1 : 0;
822 /* Call 'cleanup' hook for all plugins at exit. */
824 plugin_call_cleanup (void)
826 plugin_t *curplug = plugins_list;
829 if (curplug->cleanup_handler && !curplug->cleanup_done)
831 enum ld_plugin_status rv;
832 curplug->cleanup_done = TRUE;
833 called_plugin = curplug;
834 rv = (*curplug->cleanup_handler) ();
835 called_plugin = NULL;
837 set_plugin_error (curplug->name);
838 dlclose (curplug->dlhandle);
840 curplug = curplug->next;
842 if (plugin_error_p ())
843 info_msg (_("%P: %s: error in plugin cleanup (ignored)\n"),
844 plugin_error_plugin ());
847 /* Lazily init the non_ironly hash table. */
849 init_non_ironly_hash (void)
851 struct bfd_sym_chain *sym;
853 if (non_ironly_hash == NULL)
856 (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
857 if (!bfd_hash_table_init_n (non_ironly_hash,
859 sizeof (struct bfd_hash_entry),
861 einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
863 for (sym = &entry_symbol; sym != NULL; sym = sym->next)
865 && !bfd_hash_lookup (non_ironly_hash, sym->name, TRUE, TRUE))
866 einfo (_("%P%X: hash table failure adding symbol %s\n"),
871 /* To determine which symbols should be resolved LDPR_PREVAILING_DEF
872 and which LDPR_PREVAILING_DEF_IRONLY, we notice all the symbols as
873 the linker adds them to the linker hash table. If we see a symbol
874 being referenced from a non-IR file, we add it to the non_ironly hash
875 table. If we can't find it there at get_symbols time, we know that
876 it was referenced only by IR files. We have to notice_all symbols,
877 because we won't necessarily know until later which ones will be
878 contributed by IR files. */
880 plugin_notice (struct bfd_link_info *info ATTRIBUTE_UNUSED,
881 const char *name, bfd *abfd,
882 asection *section, bfd_vma value ATTRIBUTE_UNUSED)
884 bfd_boolean is_ref = bfd_is_und_section (section);
885 bfd_boolean is_dummy = is_ir_dummy_bfd (abfd);
886 init_non_ironly_hash ();
887 /* We only care about refs, not defs, indicated by section pointing
888 to the undefined section (according to the bfd linker notice callback
889 interface definition). */
890 if (is_ref && !is_dummy)
892 /* This is a ref from a non-IR file, so note the ref'd symbol
893 in the non-IR-only hash. */
894 if (!bfd_hash_lookup (non_ironly_hash, name, TRUE, TRUE))
895 einfo (_("%P%X: %s: hash table failure adding symbol %s\n"),
896 abfd->filename, name);
898 else if (!is_ref && is_dummy)
900 /* No further processing since this is a def from an IR dummy BFD. */
904 /* Continue with cref/nocrossref/trace-sym processing. */
908 /* When we add new object files to the link at all symbols read time,
909 these contain the real code and symbols generated from the IR files,
910 and so duplicate all the definitions already supplied by the dummy
911 IR-only BFDs that we created at claim files time. We use the linker's
912 multiple-definitions callback hook to fix up the clash, discarding
913 the symbol from the IR-only BFD in favour of the symbol from the
914 real BFD. We return true if this was not-really-a-clash because
915 we've fixed it up, or anyway if --allow-multiple-definition was in
916 effect (before we disabled it to ensure we got called back). */
918 plugin_multiple_definition (struct bfd_link_info *info, const char *name,
919 bfd *obfd, asection *osec ATTRIBUTE_UNUSED,
920 bfd_vma oval ATTRIBUTE_UNUSED,
921 bfd *nbfd, asection *nsec, bfd_vma nval)
923 if (is_ir_dummy_bfd (obfd))
925 struct bfd_link_hash_entry *blhe
926 = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
928 einfo (_("%P%X: %s: can't find IR symbol '%s'\n"), nbfd->filename,
930 else if (blhe->type != bfd_link_hash_defined)
931 einfo (_("%P%x: %s: bad IR symbol type %d\n"), name, blhe->type);
932 /* Replace it with new details. */
933 blhe->u.def.section = nsec;
934 blhe->u.def.value = nval;
937 return plugin_cached_allow_multiple_defs;