1 /* Plugin control for the GNU linker.
2 Copyright 2010, 2011, 2012 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 /* State of linker "notice" interface before we poked at it. */
94 static bfd_boolean orig_notice_all;
96 /* Original linker callbacks, and the plugin version. */
97 static const struct bfd_link_callbacks *orig_callbacks;
98 static struct bfd_link_callbacks plugin_callbacks;
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 bfd_boolean no_more_claiming = FALSE;
104 /* List of tags to set in the constant leading part of the tv array. */
105 static const enum ld_plugin_tag tv_header_tags[] =
112 LDPT_REGISTER_CLAIM_FILE_HOOK,
113 LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK,
114 LDPT_REGISTER_CLEANUP_HOOK,
117 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 section 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))->flags.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 if (ldsym->comdat_key)
290 char *name = concat (".gnu.linkonce.t.", ldsym->comdat_key,
291 (const char *) NULL);
292 section = bfd_get_section_by_name (abfd, name);
299 sflags = (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY
300 | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_EXCLUDE
301 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD);
302 section = bfd_make_section_anyway_with_flags (abfd, name, sflags);
308 section = bfd_get_section_by_name (abfd, ".text");
315 section = bfd_und_section_ptr;
320 section = bfd_com_section_ptr;
321 asym->value = ldsym->size;
322 /* For ELF targets, set alignment of common symbol to 1. */
323 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
325 ((elf_symbol_type *) asym)->internal_elf_sym.st_shndx = SHN_COMMON;
326 ((elf_symbol_type *) asym)->internal_elf_sym.st_value = 1;
334 asym->section = section;
336 /* Visibility only applies on ELF targets. */
337 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
339 elf_symbol_type *elfsym = elf_symbol_from (abfd, asym);
340 unsigned char visibility;
343 einfo (_("%P%F: %s: non-ELF symbol in ELF BFD!\n"), asym->name);
344 switch (ldsym->visibility)
347 einfo (_("%P%F: unknown ELF symbol visibility: %d!\n"),
350 visibility = STV_DEFAULT;
353 visibility = STV_PROTECTED;
356 visibility = STV_INTERNAL;
359 visibility = STV_HIDDEN;
362 elfsym->internal_elf_sym.st_other
363 = (visibility | (elfsym->internal_elf_sym.st_other
364 & ~ELF_ST_VISIBILITY (-1)));
370 /* Register a claim-file handler. */
371 static enum ld_plugin_status
372 register_claim_file (ld_plugin_claim_file_handler handler)
374 ASSERT (called_plugin);
375 called_plugin->claim_file_handler = handler;
379 /* Register an all-symbols-read handler. */
380 static enum ld_plugin_status
381 register_all_symbols_read (ld_plugin_all_symbols_read_handler handler)
383 ASSERT (called_plugin);
384 called_plugin->all_symbols_read_handler = handler;
388 /* Register a cleanup handler. */
389 static enum ld_plugin_status
390 register_cleanup (ld_plugin_cleanup_handler handler)
392 ASSERT (called_plugin);
393 called_plugin->cleanup_handler = handler;
397 /* Add symbols from a plugin-claimed input file. */
398 static enum ld_plugin_status
399 add_symbols (void *handle, int nsyms, const struct ld_plugin_symbol *syms)
405 ASSERT (called_plugin);
406 symptrs = xmalloc (nsyms * sizeof *symptrs);
407 for (n = 0; n < nsyms; n++)
409 enum ld_plugin_status rv;
412 bfdsym = bfd_make_empty_symbol (abfd);
414 rv = asymbol_from_plugin_symbol (abfd, bfdsym, syms + n);
418 bfd_set_symtab (abfd, symptrs, nsyms);
422 /* Get the input file information with an open (possibly re-opened)
424 static enum ld_plugin_status
425 get_input_file (const void *handle, struct ld_plugin_input_file *file)
427 ASSERT (called_plugin);
433 /* Release the input file. */
434 static enum ld_plugin_status
435 release_input_file (const void *handle)
437 ASSERT (called_plugin);
442 /* Return TRUE if a defined symbol might be reachable from outside the
443 universe of claimed objects. */
444 static inline bfd_boolean
445 is_visible_from_outside (struct ld_plugin_symbol *lsym,
446 struct bfd_link_hash_entry *blhe)
448 struct bfd_sym_chain *sym;
450 if (link_info.relocatable)
452 if (link_info.export_dynamic || !link_info.executable)
454 /* Check if symbol is hidden by version script. */
455 if (bfd_hide_sym_by_version (link_info.version_info,
458 /* Only ELF symbols really have visibility. */
459 if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
461 struct elf_link_hash_entry *el = (struct elf_link_hash_entry *)blhe;
462 int vis = ELF_ST_VISIBILITY (el->other);
463 return vis == STV_DEFAULT || vis == STV_PROTECTED;
465 /* On non-ELF targets, we can safely make inferences by considering
466 what visibility the plugin would have liked to apply when it first
467 sent us the symbol. During ELF symbol processing, visibility only
468 ever becomes more restrictive, not less, when symbols are merged,
469 so this is a conservative estimate; it may give false positives,
470 declaring something visible from outside when it in fact would
471 not have been, but this will only lead to missed optimisation
472 opportunities during LTRANS at worst; it will not give false
473 negatives, which can lead to the disastrous conclusion that the
474 related symbol is IRONLY. (See GCC PR46319 for an example.) */
475 return (lsym->visibility == LDPV_DEFAULT
476 || lsym->visibility == LDPV_PROTECTED);
479 for (sym = &entry_symbol; sym != NULL; sym = sym->next)
481 && strcmp (sym->name, blhe->root.string) == 0)
487 /* Get the symbol resolution info for a plugin-claimed input file. */
488 static enum ld_plugin_status
489 get_symbols (const void *handle, int nsyms, struct ld_plugin_symbol *syms,
492 const bfd *abfd = handle;
495 ASSERT (called_plugin);
496 for (n = 0; n < nsyms; n++)
498 struct bfd_link_hash_entry *blhe;
502 if (syms[n].def != LDPK_UNDEF)
503 blhe = bfd_link_hash_lookup (link_info.hash, syms[n].name,
506 blhe = bfd_wrapped_link_hash_lookup (link_info.output_bfd, &link_info,
507 syms[n].name, FALSE, FALSE, TRUE);
514 /* Determine resolution from blhe type and symbol's original type. */
515 if (blhe->type == bfd_link_hash_undefined
516 || blhe->type == bfd_link_hash_undefweak)
521 if (blhe->type != bfd_link_hash_defined
522 && blhe->type != bfd_link_hash_defweak
523 && blhe->type != bfd_link_hash_common)
525 /* We should not have a new, indirect or warning symbol here. */
526 einfo ("%P%F: %s: plugin symbol table corrupt (sym type %d)\n",
527 called_plugin->name, blhe->type);
530 /* Find out which section owns the symbol. Since it's not undef,
531 it must have an owner; if it's not a common symbol, both defs
532 and weakdefs keep it in the same place. */
533 owner_sec = (blhe->type == bfd_link_hash_common
534 ? blhe->u.c.p->section
535 : blhe->u.def.section);
538 /* If it was originally undefined or common, then it has been
539 resolved; determine how. */
540 if (syms[n].def == LDPK_UNDEF
541 || syms[n].def == LDPK_WEAKUNDEF
542 || syms[n].def == LDPK_COMMON)
544 if (owner_sec->owner == link_info.output_bfd)
545 res = LDPR_RESOLVED_EXEC;
546 else if (owner_sec->owner == abfd)
547 res = LDPR_PREVAILING_DEF_IRONLY;
548 else if (is_ir_dummy_bfd (owner_sec->owner))
549 res = LDPR_RESOLVED_IR;
550 else if (owner_sec->owner != NULL
551 && (owner_sec->owner->flags & DYNAMIC) != 0)
552 res = LDPR_RESOLVED_DYN;
554 res = LDPR_RESOLVED_EXEC;
557 /* Was originally def, or weakdef. Does it prevail? If the
558 owner is the original dummy bfd that supplied it, then this
559 is the definition that has prevailed. */
560 else if (owner_sec->owner == link_info.output_bfd)
561 res = LDPR_PREEMPTED_REG;
562 else if (owner_sec->owner == abfd)
563 res = LDPR_PREVAILING_DEF_IRONLY;
565 /* Was originally def, weakdef, or common, but has been pre-empted. */
566 else if (is_ir_dummy_bfd (owner_sec->owner))
567 res = LDPR_PREEMPTED_IR;
569 res = LDPR_PREEMPTED_REG;
571 if (res == LDPR_PREVAILING_DEF_IRONLY)
573 /* We need to know if the sym is referenced from non-IR files. Or
574 even potentially-referenced, perhaps in a future final link if
575 this is a partial one, perhaps dynamically at load-time if the
576 symbol is externally visible. */
577 if (blhe->non_ir_ref)
578 res = LDPR_PREVAILING_DEF;
579 else if (is_visible_from_outside (&syms[n], blhe))
580 res = def_ironly_exp;
584 syms[n].resolution = res;
585 if (report_plugin_symbols)
586 einfo (_("%P: %B: symbol `%s' "
587 "definition: %d, visibility: %d, resolution: %d\n"),
589 syms[n].def, syms[n].visibility, res);
594 static enum ld_plugin_status
595 get_symbols_v1 (const void *handle, int nsyms, struct ld_plugin_symbol *syms)
597 return get_symbols (handle, nsyms, syms, LDPR_PREVAILING_DEF);
600 static enum ld_plugin_status
601 get_symbols_v2 (const void *handle, int nsyms, struct ld_plugin_symbol *syms)
603 return get_symbols (handle, nsyms, syms, LDPR_PREVAILING_DEF_IRONLY_EXP);
606 /* Add a new (real) input file generated by a plugin. */
607 static enum ld_plugin_status
608 add_input_file (const char *pathname)
610 ASSERT (called_plugin);
611 if (!lang_add_input_file (xstrdup (pathname), lang_input_file_is_file_enum,
617 /* Add a new (real) library required by a plugin. */
618 static enum ld_plugin_status
619 add_input_library (const char *pathname)
621 ASSERT (called_plugin);
622 if (!lang_add_input_file (xstrdup (pathname), lang_input_file_is_l_enum,
628 /* Set the extra library path to be used by libraries added via
629 add_input_library. */
630 static enum ld_plugin_status
631 set_extra_library_path (const char *path)
633 ASSERT (called_plugin);
634 ldfile_add_library_path (xstrdup (path), FALSE);
638 /* Issue a diagnostic message from a plugin. */
639 static enum ld_plugin_status
640 message (int level, const char *format, ...)
643 va_start (args, format);
648 vfinfo (stdout, format, args, FALSE);
652 vfinfo (stdout, format, args, TRUE);
659 char *newfmt = ACONCAT ((level == LDPL_FATAL ? "%P%F: " : "%P%X: ",
660 format, "\n", (const char *) NULL));
662 vfinfo (stderr, newfmt, args, TRUE);
672 /* Helper to size leading part of tv array and set it up. */
674 set_tv_header (struct ld_plugin_tv *tv)
679 static const unsigned int major = (unsigned)(BFD_VERSION / 100000000UL);
680 static const unsigned int minor = (unsigned)(BFD_VERSION / 1000000UL) % 100;
682 for (i = 0; i < tv_header_size; i++)
684 tv[i].tv_tag = tv_header_tags[i];
685 #define TVU(x) tv[i].tv_u.tv_ ## x
686 switch (tv[i].tv_tag)
689 TVU(message) = message;
691 case LDPT_API_VERSION:
692 TVU(val) = LD_PLUGIN_API_VERSION;
694 case LDPT_GNU_LD_VERSION:
695 TVU(val) = major * 100 + minor;
697 case LDPT_LINKER_OUTPUT:
698 TVU(val) = (link_info.relocatable
700 : (link_info.executable
701 ? (link_info.pie ? LDPO_PIE : LDPO_EXEC)
704 case LDPT_OUTPUT_NAME:
705 TVU(string) = output_filename;
707 case LDPT_REGISTER_CLAIM_FILE_HOOK:
708 TVU(register_claim_file) = register_claim_file;
710 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
711 TVU(register_all_symbols_read) = register_all_symbols_read;
713 case LDPT_REGISTER_CLEANUP_HOOK:
714 TVU(register_cleanup) = register_cleanup;
716 case LDPT_ADD_SYMBOLS:
717 TVU(add_symbols) = add_symbols;
719 case LDPT_GET_INPUT_FILE:
720 TVU(get_input_file) = get_input_file;
722 case LDPT_RELEASE_INPUT_FILE:
723 TVU(release_input_file) = release_input_file;
725 case LDPT_GET_SYMBOLS:
726 TVU(get_symbols) = get_symbols_v1;
728 case LDPT_GET_SYMBOLS_V2:
729 TVU(get_symbols) = get_symbols_v2;
731 case LDPT_ADD_INPUT_FILE:
732 TVU(add_input_file) = add_input_file;
734 case LDPT_ADD_INPUT_LIBRARY:
735 TVU(add_input_library) = add_input_library;
737 case LDPT_SET_EXTRA_LIBRARY_PATH:
738 TVU(set_extra_library_path) = set_extra_library_path;
741 /* Added a new entry to the array without adding
742 a new case to set up its value is a bug. */
749 /* Append the per-plugin args list and trailing LDPT_NULL to tv. */
751 set_tv_plugin_args (plugin_t *plugin, struct ld_plugin_tv *tv)
753 plugin_arg_t *arg = plugin->args;
756 tv->tv_tag = LDPT_OPTION;
757 tv->tv_u.tv_string = arg->arg;
761 tv->tv_tag = LDPT_NULL;
765 /* Return true if any plugins are active this run. Only valid
766 after options have been processed. */
768 plugin_active_plugins_p (void)
770 return plugins_list != NULL;
773 /* Load up and initialise all plugins after argument parsing. */
775 plugin_load_plugins (void)
777 struct ld_plugin_tv *my_tv;
778 unsigned int max_args = 0;
779 plugin_t *curplug = plugins_list;
781 /* If there are no plugins, we need do nothing this run. */
785 /* First pass over plugins to find max # args needed so that we
786 can size and allocate the tv array. */
789 if (curplug->n_args > max_args)
790 max_args = curplug->n_args;
791 curplug = curplug->next;
794 /* Allocate tv array and initialise constant part. */
795 my_tv = xmalloc ((max_args + 1 + tv_header_size) * sizeof *my_tv);
796 set_tv_header (my_tv);
798 /* Pass over plugins again, activating them. */
799 curplug = plugins_list;
802 enum ld_plugin_status rv;
803 ld_plugin_onload onloadfn = dlsym (curplug->dlhandle, "onload");
805 onloadfn = dlsym (curplug->dlhandle, "_onload");
807 return set_plugin_error (curplug->name);
808 set_tv_plugin_args (curplug, &my_tv[tv_header_size]);
809 called_plugin = curplug;
810 rv = (*onloadfn) (my_tv);
811 called_plugin = NULL;
813 return set_plugin_error (curplug->name);
814 curplug = curplug->next;
817 /* Since plugin(s) inited ok, assume they're going to want symbol
818 resolutions, which needs us to track which symbols are referenced
819 by non-IR files using the linker's notice callback. */
820 orig_notice_all = link_info.notice_all;
821 orig_callbacks = link_info.callbacks;
822 plugin_callbacks = *orig_callbacks;
823 plugin_callbacks.notice = &plugin_notice;
824 link_info.notice_all = TRUE;
825 link_info.callbacks = &plugin_callbacks;
830 /* Call 'claim file' hook for all plugins. */
832 plugin_call_claim_file (const struct ld_plugin_input_file *file, int *claimed)
834 plugin_t *curplug = plugins_list;
836 if (no_more_claiming)
838 while (curplug && !*claimed)
840 if (curplug->claim_file_handler)
842 enum ld_plugin_status rv;
843 called_plugin = curplug;
844 rv = (*curplug->claim_file_handler) (file, claimed);
845 called_plugin = NULL;
847 set_plugin_error (curplug->name);
849 curplug = curplug->next;
851 return plugin_error_p () ? -1 : 0;
855 plugin_maybe_claim (struct ld_plugin_input_file *file,
856 lang_input_statement_type *entry)
860 /* We create a dummy BFD, initially empty, to house whatever symbols
861 the plugin may want to add. */
862 file->handle = plugin_get_ir_dummy_bfd (entry->the_bfd->filename,
864 if (plugin_call_claim_file (file, &claimed))
865 einfo (_("%P%F: %s: plugin reported error claiming file\n"),
866 plugin_error_plugin ());
867 /* fd belongs to us, not the plugin; but we don't need it. */
871 /* Discard the real file's BFD and substitute the dummy one. */
873 /* BFD archive handling caches elements so we can't call
874 bfd_close for archives. */
875 if (entry->the_bfd->my_archive == NULL)
876 bfd_close (entry->the_bfd);
877 entry->the_bfd = file->handle;
878 entry->flags.claimed = TRUE;
879 bfd_make_readable (entry->the_bfd);
883 /* If plugin didn't claim the file, we don't need the dummy bfd.
884 Can't avoid speculatively creating it, alas. */
885 bfd_close_all_done (file->handle);
886 entry->flags.claimed = FALSE;
890 /* Call 'all symbols read' hook for all plugins. */
892 plugin_call_all_symbols_read (void)
894 plugin_t *curplug = plugins_list;
896 /* Disable any further file-claiming. */
897 no_more_claiming = TRUE;
901 if (curplug->all_symbols_read_handler)
903 enum ld_plugin_status rv;
904 called_plugin = curplug;
905 rv = (*curplug->all_symbols_read_handler) ();
906 called_plugin = NULL;
908 set_plugin_error (curplug->name);
910 curplug = curplug->next;
912 return plugin_error_p () ? -1 : 0;
915 /* Call 'cleanup' hook for all plugins at exit. */
917 plugin_call_cleanup (void)
919 plugin_t *curplug = plugins_list;
922 if (curplug->cleanup_handler && !curplug->cleanup_done)
924 enum ld_plugin_status rv;
925 curplug->cleanup_done = TRUE;
926 called_plugin = curplug;
927 rv = (*curplug->cleanup_handler) ();
928 called_plugin = NULL;
930 set_plugin_error (curplug->name);
931 dlclose (curplug->dlhandle);
933 curplug = curplug->next;
935 if (plugin_error_p ())
936 info_msg (_("%P: %s: error in plugin cleanup (ignored)\n"),
937 plugin_error_plugin ());
940 /* To determine which symbols should be resolved LDPR_PREVAILING_DEF
941 and which LDPR_PREVAILING_DEF_IRONLY, we notice all the symbols as
942 the linker adds them to the linker hash table. Mark those
943 referenced from a non-IR file with non_ir_ref. We have to
944 notice_all symbols, because we won't necessarily know until later
945 which ones will be contributed by IR files. */
947 plugin_notice (struct bfd_link_info *info,
948 struct bfd_link_hash_entry *h,
959 /* No further processing if this def/ref is from an IR dummy BFD. */
960 if (is_ir_dummy_bfd (abfd))
963 /* Making an indirect symbol counts as a reference unless this
964 is a brand new symbol. */
965 if (bfd_is_ind_section (section)
966 || (flags & BSF_INDIRECT) != 0)
968 if (h->type != bfd_link_hash_new)
970 struct bfd_link_hash_entry *inh;
972 h->non_ir_ref = TRUE;
973 inh = bfd_wrapped_link_hash_lookup (abfd, info, string, FALSE,
976 inh->non_ir_ref = TRUE;
980 /* Nothing to do here for warning symbols. */
981 else if ((flags & BSF_WARNING) != 0)
984 /* Nothing to do here for constructor symbols. */
985 else if ((flags & BSF_CONSTRUCTOR) != 0)
988 /* If this is a ref, set non_ir_ref. */
989 else if (bfd_is_und_section (section))
990 h->non_ir_ref = TRUE;
992 /* Otherwise, it must be a new def. Ensure any symbol defined
993 in an IR dummy BFD takes on a new value from a real BFD.
994 Weak symbols are not normally overridden by a new weak
995 definition, and strong symbols will normally cause multiple
996 definition errors. Avoid this by making the symbol appear
998 else if (((h->type == bfd_link_hash_defweak
999 || h->type == bfd_link_hash_defined)
1000 && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
1001 || (h->type == bfd_link_hash_common
1002 && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner)))
1004 h->type = bfd_link_hash_undefweak;
1005 h->u.undef.abfd = sym_bfd;
1009 /* Continue with cref/nocrossref/trace-sym processing. */
1012 || (info->notice_hash != NULL
1013 && bfd_hash_lookup (info->notice_hash, h->root.string,
1014 FALSE, FALSE) != NULL))
1015 return (*orig_callbacks->notice) (info, h,
1016 abfd, section, value, flags, string);
1020 /* Return true if bfd is a dynamic library that should be reloaded. */
1023 plugin_should_reload (bfd *abfd)
1025 return ((abfd->flags & DYNAMIC) != 0
1026 && bfd_get_flavour (abfd) == bfd_target_elf_flavour
1027 && bfd_get_format (abfd) == bfd_object
1028 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0);