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"
36 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
40 /* Report plugin symbols. */
41 bfd_boolean report_plugin_symbols;
43 /* The suffix to append to the name of the real (claimed) object file
44 when generating a dummy BFD to hold the IR symbols sent from the
45 plugin. For cosmetic use only; appears in maps, crefs etc. */
46 #define IRONLY_SUFFIX " (symbol from plugin)"
48 /* Stores a single argument passed to a plugin. */
49 typedef struct plugin_arg
51 struct plugin_arg *next;
55 /* Holds all details of a single plugin. */
58 /* Next on the list of plugins, or NULL at end of chain. */
60 /* The argument string given to --plugin. */
62 /* The shared library handle returned by dlopen. */
64 /* The list of argument string given to --plugin-opt. */
66 /* Number of args in the list, for convenience. */
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;
76 /* The master list of all plugins. */
77 static plugin_t *plugins_list = NULL;
79 /* We keep a tail pointer for easy linking on the end. */
80 static plugin_t **plugins_tail_chain_ptr = &plugins_list;
82 /* The last plugin added to the list, for receiving args. */
83 static plugin_t *last_plugin = NULL;
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;
88 /* The plugin which is currently having a callback executed. */
89 static plugin_t *called_plugin = NULL;
91 /* Last plugin to cause an error, if any. */
92 static const char *error_plugin = NULL;
94 /* State of linker "notice" interface before we poked at it. */
95 static bfd_boolean orig_notice_all;
97 /* Original linker callbacks, and the plugin version. */
98 static const struct bfd_link_callbacks *orig_callbacks;
99 static struct bfd_link_callbacks plugin_callbacks;
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 bfd_boolean no_more_claiming = FALSE;
105 /* List of tags to set in the constant leading part of the tv array. */
106 static const enum ld_plugin_tag tv_header_tags[] =
113 LDPT_REGISTER_CLAIM_FILE_HOOK,
114 LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK,
115 LDPT_REGISTER_CLEANUP_HOOK,
118 LDPT_RELEASE_INPUT_FILE,
121 LDPT_ADD_INPUT_LIBRARY,
122 LDPT_SET_EXTRA_LIBRARY_PATH
125 /* How many entries in the constant leading part of the tv array. */
126 static const size_t tv_header_size = ARRAY_SIZE (tv_header_tags);
128 /* Forward references. */
129 static bfd_boolean plugin_notice (struct bfd_link_info *,
130 struct bfd_link_hash_entry *, bfd *,
131 asection *, bfd_vma, flagword, const char *);
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)
230 bfd_use_reserved_id = 1;
231 abfd = bfd_create (concat (name, IRONLY_SUFFIX, (const char *) NULL),
235 abfd->flags |= BFD_LINKER_CREATED | BFD_PLUGIN;
236 bfd_set_arch_info (abfd, bfd_get_arch_info (srctemplate));
237 bfd_set_gp_size (abfd, bfd_get_gp_size (srctemplate));
238 if (bfd_make_writable (abfd)
239 && bfd_copy_private_bfd_data (srctemplate, abfd))
243 /* Create sections to own the symbols. */
244 flags = (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY
245 | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_EXCLUDE);
246 if (bfd_make_section_anyway_with_flags (abfd, ".text", flags))
250 einfo (_("could not create dummy IR bfd: %F%E\n"));
254 /* Check if the BFD passed in is an IR dummy object file. */
256 is_ir_dummy_bfd (const bfd *abfd)
258 /* ABFD can sometimes legitimately be NULL, e.g. when called from one
259 of the linker callbacks for a symbol in the *ABS* or *UND* sections.
260 Likewise, the usrdata field may be NULL if ABFD was added by the
261 backend without a corresponding input statement, as happens e.g.
262 when processing DT_NEEDED dependencies. */
265 && ((lang_input_statement_type *)(abfd->usrdata))->claimed);
268 /* Helpers to convert between BFD and GOLD symbol formats. */
269 static enum ld_plugin_status
270 asymbol_from_plugin_symbol (bfd *abfd, asymbol *asym,
271 const struct ld_plugin_symbol *ldsym)
273 flagword flags = BSF_NO_FLAGS;
274 struct bfd_section *section;
276 asym->the_bfd = abfd;
277 asym->name = (ldsym->version
278 ? concat (ldsym->name, "@", ldsym->version, (const char *) NULL)
288 section = bfd_get_section_by_name (abfd, ".text");
295 section = bfd_und_section_ptr;
300 section = bfd_com_section_ptr;
301 asym->value = ldsym->size;
302 /* For ELF targets, set alignment of common symbol to 1. */
303 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
305 ((elf_symbol_type *) asym)->internal_elf_sym.st_shndx = SHN_COMMON;
306 ((elf_symbol_type *) asym)->internal_elf_sym.st_value = 1;
314 asym->section = section;
316 /* Visibility only applies on ELF targets. */
317 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
319 elf_symbol_type *elfsym = elf_symbol_from (abfd, asym);
320 unsigned char visibility;
323 einfo (_("%P%F: %s: non-ELF symbol in ELF BFD!\n"), asym->name);
324 switch (ldsym->visibility)
327 einfo (_("%P%F: unknown ELF symbol visibility: %d!\n"),
330 visibility = STV_DEFAULT;
333 visibility = STV_PROTECTED;
336 visibility = STV_INTERNAL;
339 visibility = STV_HIDDEN;
342 elfsym->internal_elf_sym.st_other
343 = (visibility | (elfsym->internal_elf_sym.st_other
344 & ~ELF_ST_VISIBILITY (-1)));
350 /* Register a claim-file handler. */
351 static enum ld_plugin_status
352 register_claim_file (ld_plugin_claim_file_handler handler)
354 ASSERT (called_plugin);
355 called_plugin->claim_file_handler = handler;
359 /* Register an all-symbols-read handler. */
360 static enum ld_plugin_status
361 register_all_symbols_read (ld_plugin_all_symbols_read_handler handler)
363 ASSERT (called_plugin);
364 called_plugin->all_symbols_read_handler = handler;
368 /* Register a cleanup handler. */
369 static enum ld_plugin_status
370 register_cleanup (ld_plugin_cleanup_handler handler)
372 ASSERT (called_plugin);
373 called_plugin->cleanup_handler = handler;
377 /* Add symbols from a plugin-claimed input file. */
378 static enum ld_plugin_status
379 add_symbols (void *handle, int nsyms, const struct ld_plugin_symbol *syms)
384 ASSERT (called_plugin);
385 symptrs = xmalloc (nsyms * sizeof *symptrs);
386 for (n = 0; n < nsyms; n++)
388 enum ld_plugin_status rv;
391 if (syms[n].comdat_key)
393 struct already_linked linked;
394 linked.comdat_key = xstrdup (syms[n].comdat_key);
395 linked.u.abfd = abfd;
396 bfd_section_already_linked (abfd, &linked, &link_info);
398 bfdsym = bfd_make_empty_symbol (abfd);
400 rv = asymbol_from_plugin_symbol (abfd, bfdsym, syms + n);
404 bfd_set_symtab (abfd, symptrs, nsyms);
408 /* Get the input file information with an open (possibly re-opened)
410 static enum ld_plugin_status
411 get_input_file (const void *handle, struct ld_plugin_input_file *file)
413 ASSERT (called_plugin);
419 /* Release the input file. */
420 static enum ld_plugin_status
421 release_input_file (const void *handle)
423 ASSERT (called_plugin);
428 /* Return TRUE if a defined symbol might be reachable from outside the
429 universe of claimed objects. */
430 static inline bfd_boolean
431 is_visible_from_outside (struct ld_plugin_symbol *lsym, asection *section,
432 struct bfd_link_hash_entry *blhe)
434 struct bfd_sym_chain *sym;
436 /* Section's owner may be NULL if it is the absolute
437 section, fortunately is_ir_dummy_bfd handles that. */
438 if (!is_ir_dummy_bfd (section->owner))
440 if (link_info.relocatable)
442 if (link_info.export_dynamic || link_info.shared)
444 /* Only ELF symbols really have visibility. */
445 if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
447 struct elf_link_hash_entry *el = (struct elf_link_hash_entry *)blhe;
448 int vis = ELF_ST_VISIBILITY (el->other);
449 return vis == STV_DEFAULT || vis == STV_PROTECTED;
451 /* On non-ELF targets, we can safely make inferences by considering
452 what visibility the plugin would have liked to apply when it first
453 sent us the symbol. During ELF symbol processing, visibility only
454 ever becomes more restrictive, not less, when symbols are merged,
455 so this is a conservative estimate; it may give false positives,
456 declaring something visible from outside when it in fact would
457 not have been, but this will only lead to missed optimisation
458 opportunities during LTRANS at worst; it will not give false
459 negatives, which can lead to the disastrous conclusion that the
460 related symbol is IRONLY. (See GCC PR46319 for an example.) */
461 return (lsym->visibility == LDPV_DEFAULT
462 || lsym->visibility == LDPV_PROTECTED);
465 for (sym = &entry_symbol; sym != NULL; sym = sym->next)
467 && strcmp (sym->name, blhe->root.string) == 0)
473 /* Get the symbol resolution info for a plugin-claimed input file. */
474 static enum ld_plugin_status
475 get_symbols (const void *handle, int nsyms, struct ld_plugin_symbol *syms)
477 const bfd *abfd = handle;
479 ASSERT (called_plugin);
480 for (n = 0; n < nsyms; n++)
482 struct bfd_link_hash_entry *blhe;
485 if (syms[n].def != LDPK_UNDEF)
486 blhe = bfd_link_hash_lookup (link_info.hash, syms[n].name,
489 blhe = bfd_wrapped_link_hash_lookup (link_info.output_bfd, &link_info,
490 syms[n].name, FALSE, FALSE, TRUE);
493 syms[n].resolution = LDPR_UNKNOWN;
497 /* Determine resolution from blhe type and symbol's original type. */
498 if (blhe->type == bfd_link_hash_undefined
499 || blhe->type == bfd_link_hash_undefweak)
501 syms[n].resolution = LDPR_UNDEF;
504 if (blhe->type != bfd_link_hash_defined
505 && blhe->type != bfd_link_hash_defweak
506 && blhe->type != bfd_link_hash_common)
508 /* We should not have a new, indirect or warning symbol here. */
509 einfo ("%P%F: %s: plugin symbol table corrupt (sym type %d)\n",
510 called_plugin->name, blhe->type);
513 /* Find out which section owns the symbol. Since it's not undef,
514 it must have an owner; if it's not a common symbol, both defs
515 and weakdefs keep it in the same place. */
516 owner_sec = (blhe->type == bfd_link_hash_common
517 ? blhe->u.c.p->section
518 : blhe->u.def.section);
520 /* We need to know if the sym is referenced from non-IR files. Or
521 even potentially-referenced, perhaps in a future final link if
522 this is a partial one, perhaps dynamically at load-time if the
523 symbol is externally visible. */
524 ironly = !(blhe->non_ir_ref
525 || is_visible_from_outside (&syms[n], owner_sec, blhe));
527 /* If it was originally undefined or common, then it has been
528 resolved; determine how. */
529 if (syms[n].def == LDPK_UNDEF
530 || syms[n].def == LDPK_WEAKUNDEF
531 || syms[n].def == LDPK_COMMON)
533 if (owner_sec->owner == link_info.output_bfd)
534 syms[n].resolution = LDPR_RESOLVED_EXEC;
535 else if (owner_sec->owner == abfd)
536 syms[n].resolution = (ironly
537 ? LDPR_PREVAILING_DEF_IRONLY
538 : LDPR_PREVAILING_DEF);
539 else if (is_ir_dummy_bfd (owner_sec->owner))
540 syms[n].resolution = LDPR_RESOLVED_IR;
541 else if (owner_sec->owner != NULL
542 && (owner_sec->owner->flags & DYNAMIC) != 0)
543 syms[n].resolution = LDPR_RESOLVED_DYN;
545 syms[n].resolution = LDPR_RESOLVED_EXEC;
549 /* Was originally def, or weakdef. Does it prevail? If the
550 owner is the original dummy bfd that supplied it, then this
551 is the definition that has prevailed. */
552 if (owner_sec->owner == link_info.output_bfd)
553 syms[n].resolution = LDPR_PREEMPTED_REG;
554 else if (owner_sec->owner == abfd)
556 syms[n].resolution = (ironly
557 ? LDPR_PREVAILING_DEF_IRONLY
558 : LDPR_PREVAILING_DEF);
562 /* Was originally def, weakdef, or common, but has been pre-empted. */
563 syms[n].resolution = (is_ir_dummy_bfd (owner_sec->owner)
565 : LDPR_PREEMPTED_REG);
568 if (report_plugin_symbols)
569 einfo (_("%P: %B: symbol `%s' "
570 "definition: %d, visibility: %d, resolution: %d\n"),
572 syms[n].def, syms[n].visibility, syms[n].resolution);
577 /* Add a new (real) input file generated by a plugin. */
578 static enum ld_plugin_status
579 add_input_file (const char *pathname)
581 ASSERT (called_plugin);
582 if (!lang_add_input_file (xstrdup (pathname), lang_input_file_is_file_enum,
588 /* Add a new (real) library required by a plugin. */
589 static enum ld_plugin_status
590 add_input_library (const char *pathname)
592 ASSERT (called_plugin);
593 if (!lang_add_input_file (xstrdup (pathname), lang_input_file_is_l_enum,
599 /* Set the extra library path to be used by libraries added via
600 add_input_library. */
601 static enum ld_plugin_status
602 set_extra_library_path (const char *path)
604 ASSERT (called_plugin);
605 ldfile_add_library_path (xstrdup (path), FALSE);
609 /* Issue a diagnostic message from a plugin. */
610 static enum ld_plugin_status
611 message (int level, const char *format, ...)
614 va_start (args, format);
619 vfinfo (stdout, format, args, FALSE);
623 vfinfo (stdout, format, args, TRUE);
630 char *newfmt = ACONCAT ((level == LDPL_FATAL ? "%P%F: " : "%P%X: ",
631 format, "\n", (const char *) NULL));
633 vfinfo (stderr, newfmt, args, TRUE);
643 /* Helper to size leading part of tv array and set it up. */
645 set_tv_header (struct ld_plugin_tv *tv)
650 static const unsigned int major = (unsigned)(BFD_VERSION / 100000000UL);
651 static const unsigned int minor = (unsigned)(BFD_VERSION / 1000000UL) % 100;
654 return tv_header_size;
656 for (i = 0; i < tv_header_size; i++)
658 tv[i].tv_tag = tv_header_tags[i];
659 #define TVU(x) tv[i].tv_u.tv_ ## x
660 switch (tv[i].tv_tag)
663 TVU(message) = message;
665 case LDPT_API_VERSION:
666 TVU(val) = LD_PLUGIN_API_VERSION;
668 case LDPT_GNU_LD_VERSION:
669 TVU(val) = major * 100 + minor;
671 case LDPT_LINKER_OUTPUT:
672 TVU(val) = (link_info.relocatable
674 : (link_info.shared ? LDPO_DYN : LDPO_EXEC));
676 case LDPT_OUTPUT_NAME:
677 TVU(string) = output_filename;
679 case LDPT_REGISTER_CLAIM_FILE_HOOK:
680 TVU(register_claim_file) = register_claim_file;
682 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
683 TVU(register_all_symbols_read) = register_all_symbols_read;
685 case LDPT_REGISTER_CLEANUP_HOOK:
686 TVU(register_cleanup) = register_cleanup;
688 case LDPT_ADD_SYMBOLS:
689 TVU(add_symbols) = add_symbols;
691 case LDPT_GET_INPUT_FILE:
692 TVU(get_input_file) = get_input_file;
694 case LDPT_RELEASE_INPUT_FILE:
695 TVU(release_input_file) = release_input_file;
697 case LDPT_GET_SYMBOLS:
698 TVU(get_symbols) = get_symbols;
700 case LDPT_ADD_INPUT_FILE:
701 TVU(add_input_file) = add_input_file;
703 case LDPT_ADD_INPUT_LIBRARY:
704 TVU(add_input_library) = add_input_library;
706 case LDPT_SET_EXTRA_LIBRARY_PATH:
707 TVU(set_extra_library_path) = set_extra_library_path;
710 /* Added a new entry to the array without adding
711 a new case to set up its value is a bug. */
716 return tv_header_size;
719 /* Append the per-plugin args list and trailing LDPT_NULL to tv. */
721 set_tv_plugin_args (plugin_t *plugin, struct ld_plugin_tv *tv)
723 plugin_arg_t *arg = plugin->args;
726 tv->tv_tag = LDPT_OPTION;
727 tv->tv_u.tv_string = arg->arg;
731 tv->tv_tag = LDPT_NULL;
735 /* Return true if any plugins are active this run. Only valid
736 after options have been processed. */
738 plugin_active_plugins_p (void)
740 return plugins_list != NULL;
743 /* Load up and initialise all plugins after argument parsing. */
745 plugin_load_plugins (void)
747 struct ld_plugin_tv *my_tv;
748 unsigned int max_args = 0;
749 plugin_t *curplug = plugins_list;
751 /* If there are no plugins, we need do nothing this run. */
755 /* First pass over plugins to find max # args needed so that we
756 can size and allocate the tv array. */
759 if (curplug->n_args > max_args)
760 max_args = curplug->n_args;
761 curplug = curplug->next;
764 /* Allocate tv array and initialise constant part. */
765 my_tv = xmalloc ((max_args + 1 + tv_header_size) * sizeof *my_tv);
766 set_tv_header (my_tv);
768 /* Pass over plugins again, activating them. */
769 curplug = plugins_list;
772 enum ld_plugin_status rv;
773 ld_plugin_onload onloadfn = dlsym (curplug->dlhandle, "onload");
775 onloadfn = dlsym (curplug->dlhandle, "_onload");
777 return set_plugin_error (curplug->name);
778 set_tv_plugin_args (curplug, &my_tv[tv_header_size]);
779 called_plugin = curplug;
780 rv = (*onloadfn) (my_tv);
781 called_plugin = NULL;
783 return set_plugin_error (curplug->name);
784 curplug = curplug->next;
787 /* Since plugin(s) inited ok, assume they're going to want symbol
788 resolutions, which needs us to track which symbols are referenced
789 by non-IR files using the linker's notice callback. */
790 orig_notice_all = link_info.notice_all;
791 orig_callbacks = link_info.callbacks;
792 plugin_callbacks = *orig_callbacks;
793 plugin_callbacks.notice = &plugin_notice;
794 link_info.notice_all = TRUE;
795 link_info.callbacks = &plugin_callbacks;
800 /* Call 'claim file' hook for all plugins. */
802 plugin_call_claim_file (const struct ld_plugin_input_file *file, int *claimed)
804 plugin_t *curplug = plugins_list;
806 if (no_more_claiming)
808 while (curplug && !*claimed)
810 if (curplug->claim_file_handler)
812 enum ld_plugin_status rv;
813 called_plugin = curplug;
814 rv = (*curplug->claim_file_handler) (file, claimed);
815 called_plugin = NULL;
817 set_plugin_error (curplug->name);
819 curplug = curplug->next;
821 return plugin_error_p () ? -1 : 0;
825 plugin_maybe_claim (struct ld_plugin_input_file *file,
826 lang_input_statement_type *entry)
830 /* We create a dummy BFD, initially empty, to house whatever symbols
831 the plugin may want to add. */
832 file->handle = plugin_get_ir_dummy_bfd (entry->the_bfd->filename,
834 if (plugin_call_claim_file (file, &claimed))
835 einfo (_("%P%F: %s: plugin reported error claiming file\n"),
836 plugin_error_plugin ());
837 /* fd belongs to us, not the plugin; but we don't need it. */
841 /* Discard the real file's BFD and substitute the dummy one. */
843 /* BFD archive handling caches elements so we can't call
844 bfd_close for archives. */
845 if (entry->the_bfd->my_archive == NULL)
846 bfd_close (entry->the_bfd);
847 entry->the_bfd = file->handle;
848 entry->claimed = TRUE;
849 bfd_make_readable (entry->the_bfd);
853 /* If plugin didn't claim the file, we don't need the dummy bfd.
854 Can't avoid speculatively creating it, alas. */
855 bfd_close_all_done (file->handle);
856 entry->claimed = FALSE;
860 /* Call 'all symbols read' hook for all plugins. */
862 plugin_call_all_symbols_read (void)
864 plugin_t *curplug = plugins_list;
866 /* Disable any further file-claiming. */
867 no_more_claiming = TRUE;
871 if (curplug->all_symbols_read_handler)
873 enum ld_plugin_status rv;
874 called_plugin = curplug;
875 rv = (*curplug->all_symbols_read_handler) ();
876 called_plugin = NULL;
878 set_plugin_error (curplug->name);
880 curplug = curplug->next;
882 return plugin_error_p () ? -1 : 0;
885 /* Call 'cleanup' hook for all plugins at exit. */
887 plugin_call_cleanup (void)
889 plugin_t *curplug = plugins_list;
892 if (curplug->cleanup_handler && !curplug->cleanup_done)
894 enum ld_plugin_status rv;
895 curplug->cleanup_done = TRUE;
896 called_plugin = curplug;
897 rv = (*curplug->cleanup_handler) ();
898 called_plugin = NULL;
900 set_plugin_error (curplug->name);
901 dlclose (curplug->dlhandle);
903 curplug = curplug->next;
905 if (plugin_error_p ())
906 info_msg (_("%P: %s: error in plugin cleanup (ignored)\n"),
907 plugin_error_plugin ());
910 /* To determine which symbols should be resolved LDPR_PREVAILING_DEF
911 and which LDPR_PREVAILING_DEF_IRONLY, we notice all the symbols as
912 the linker adds them to the linker hash table. Mark those
913 referenced from a non-IR file with non_ir_ref. We have to
914 notice_all symbols, because we won't necessarily know until later
915 which ones will be contributed by IR files. */
917 plugin_notice (struct bfd_link_info *info,
918 struct bfd_link_hash_entry *h,
929 /* No further processing if this def/ref is from an IR dummy BFD. */
930 if (is_ir_dummy_bfd (abfd))
933 /* Making an indirect symbol counts as a reference unless this
934 is a brand new symbol. */
935 if (bfd_is_ind_section (section)
936 || (flags & BSF_INDIRECT) != 0)
938 if (h->type != bfd_link_hash_new)
940 struct bfd_link_hash_entry *inh;
942 h->non_ir_ref = TRUE;
943 inh = bfd_wrapped_link_hash_lookup (abfd, info, string, FALSE,
946 inh->non_ir_ref = TRUE;
950 /* Nothing to do here for warning symbols. */
951 else if ((flags & BSF_WARNING) != 0)
954 /* Nothing to do here for constructor symbols. */
955 else if ((flags & BSF_CONSTRUCTOR) != 0)
958 /* If this is a ref, set non_ir_ref. */
959 else if (bfd_is_und_section (section))
960 h->non_ir_ref = TRUE;
962 /* Otherwise, it must be a new def. Ensure any symbol defined
963 in an IR dummy BFD takes on a new value from a real BFD.
964 Weak symbols are not normally overridden by a new weak
965 definition, and strong symbols will normally cause multiple
966 definition errors. Avoid this by making the symbol appear
968 else if (((h->type == bfd_link_hash_defweak
969 || h->type == bfd_link_hash_defined)
970 && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
971 || (h->type == bfd_link_hash_common
972 && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner)))
974 h->type = bfd_link_hash_undefweak;
975 h->u.undef.abfd = sym_bfd;
979 /* Continue with cref/nocrossref/trace-sym processing. */
982 || (info->notice_hash != NULL
983 && bfd_hash_lookup (info->notice_hash, h->root.string,
984 FALSE, FALSE) != NULL))
985 return (*orig_callbacks->notice) (info, h,
986 abfd, section, value, flags, string);