1 /* Plugin control for the GNU linker.
2 Copyright (C) 2010-2015 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"
34 #include "plugin-api.h"
37 # include <sys/mman.h>
39 # define MAP_FAILED ((void *) -1)
45 # define MAP_PRIVATE 0
49 #if !(defined(errno) || defined(_MSC_VER) && defined(_INC_ERRNO))
52 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
56 /* Report plugin symbols. */
57 bfd_boolean report_plugin_symbols;
59 /* The suffix to append to the name of the real (claimed) object file
60 when generating a dummy BFD to hold the IR symbols sent from the
61 plugin. For cosmetic use only; appears in maps, crefs etc. */
62 #define IRONLY_SUFFIX " (symbol from plugin)"
64 /* Stores a single argument passed to a plugin. */
65 typedef struct plugin_arg
67 struct plugin_arg *next;
71 /* Holds all details of a single plugin. */
74 /* Next on the list of plugins, or NULL at end of chain. */
76 /* The argument string given to --plugin. */
78 /* The shared library handle returned by dlopen. */
80 /* The list of argument string given to --plugin-opt. */
82 /* Number of args in the list, for convenience. */
84 /* The plugin's event handlers. */
85 ld_plugin_claim_file_handler claim_file_handler;
86 ld_plugin_all_symbols_read_handler all_symbols_read_handler;
87 ld_plugin_cleanup_handler cleanup_handler;
88 /* TRUE if the cleanup handlers have been called. */
89 bfd_boolean cleanup_done;
92 typedef struct view_buffer
99 /* The internal version of struct ld_plugin_input_file with a BFD
101 typedef struct plugin_input_file
104 view_buffer_t view_buffer;
109 } plugin_input_file_t;
111 /* The master list of all plugins. */
112 static plugin_t *plugins_list = NULL;
114 /* We keep a tail pointer for easy linking on the end. */
115 static plugin_t **plugins_tail_chain_ptr = &plugins_list;
117 /* The last plugin added to the list, for receiving args. */
118 static plugin_t *last_plugin = NULL;
120 /* The tail of the arg chain of the last plugin added to the list. */
121 static plugin_arg_t **last_plugin_args_tail_chain_ptr = NULL;
123 /* The plugin which is currently having a callback executed. */
124 static plugin_t *called_plugin = NULL;
126 /* Last plugin to cause an error, if any. */
127 static const char *error_plugin = NULL;
129 /* State of linker "notice" interface before we poked at it. */
130 static bfd_boolean orig_notice_all;
132 /* Original linker callbacks, and the plugin version. */
133 static const struct bfd_link_callbacks *orig_callbacks;
134 static struct bfd_link_callbacks plugin_callbacks;
136 /* Set at all symbols read time, to avoid recursively offering the plugin
137 its own newly-added input files and libs to claim. */
138 bfd_boolean no_more_claiming = FALSE;
140 /* List of tags to set in the constant leading part of the tv array. */
141 static const enum ld_plugin_tag tv_header_tags[] =
148 LDPT_REGISTER_CLAIM_FILE_HOOK,
149 LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK,
150 LDPT_REGISTER_CLEANUP_HOOK,
154 LDPT_RELEASE_INPUT_FILE,
158 LDPT_ADD_INPUT_LIBRARY,
159 LDPT_SET_EXTRA_LIBRARY_PATH
162 /* How many entries in the constant leading part of the tv array. */
163 static const size_t tv_header_size = ARRAY_SIZE (tv_header_tags);
165 /* Forward references. */
166 static bfd_boolean plugin_notice (struct bfd_link_info *,
167 struct bfd_link_hash_entry *,
168 struct bfd_link_hash_entry *,
169 bfd *, asection *, bfd_vma, flagword);
171 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
173 #define RTLD_NOW 0 /* Dummy value. */
176 dlopen (const char *file, int mode ATTRIBUTE_UNUSED)
178 return LoadLibrary (file);
182 dlsym (void *handle, const char *name)
184 return GetProcAddress (handle, name);
188 dlclose (void *handle)
190 FreeLibrary (handle);
194 #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */
204 /* Helper function for exiting with error status. */
206 set_plugin_error (const char *plugin)
208 error_plugin = plugin;
212 /* Test if an error occurred. */
214 plugin_error_p (void)
216 return error_plugin != NULL;
219 /* Return name of plugin which caused an error if any. */
221 plugin_error_plugin (void)
223 return error_plugin ? error_plugin : _("<no plugin>");
226 /* Handle -plugin arg: find and load plugin, or return error. */
228 plugin_opt_plugin (const char *plugin)
232 newplug = xmalloc (sizeof *newplug);
233 memset (newplug, 0, sizeof *newplug);
234 newplug->name = plugin;
235 newplug->dlhandle = dlopen (plugin, RTLD_NOW);
236 if (!newplug->dlhandle)
237 einfo (_("%P%F: %s: error loading plugin: %s\n"), plugin, dlerror ());
239 /* Chain on end, so when we run list it is in command-line order. */
240 *plugins_tail_chain_ptr = newplug;
241 plugins_tail_chain_ptr = &newplug->next;
243 /* Record it as current plugin for receiving args. */
244 last_plugin = newplug;
245 last_plugin_args_tail_chain_ptr = &newplug->args;
248 /* Accumulate option arguments for last-loaded plugin, or return
251 plugin_opt_plugin_arg (const char *arg)
253 plugin_arg_t *newarg;
256 return set_plugin_error (_("<no plugin>"));
258 /* Ignore -pass-through= from GCC driver. */
261 const char *p = arg + 1;
265 if (strncmp (p, "pass-through=", 13) == 0)
269 newarg = xmalloc (sizeof *newarg);
273 /* Chain on end to preserve command-line order. */
274 *last_plugin_args_tail_chain_ptr = newarg;
275 last_plugin_args_tail_chain_ptr = &newarg->next;
276 last_plugin->n_args++;
280 /* Generate a dummy BFD to represent an IR file, for any callers of
281 plugin_call_claim_file to use as the handle in the ld_plugin_input_file
282 struct that they build to pass in. The BFD is initially writable, so
283 that symbols can be added to it; it must be made readable after the
284 add_symbols hook has been called so that it can be read when linking. */
286 plugin_get_ir_dummy_bfd (const char *name, bfd *srctemplate)
290 bfd_use_reserved_id = 1;
291 abfd = bfd_create (concat (name, IRONLY_SUFFIX, (const char *) NULL),
295 abfd->flags |= BFD_LINKER_CREATED | BFD_PLUGIN;
296 bfd_set_arch_info (abfd, bfd_get_arch_info (srctemplate));
297 bfd_set_gp_size (abfd, bfd_get_gp_size (srctemplate));
298 if (bfd_make_writable (abfd)
299 && bfd_copy_private_bfd_data (srctemplate, abfd))
303 /* Create section to own the symbols. */
304 flags = (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY
305 | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_EXCLUDE);
306 if (bfd_make_section_anyway_with_flags (abfd, ".text", flags))
310 einfo (_("could not create dummy IR bfd: %F%E\n"));
314 /* Check if the BFD passed in is an IR dummy object file. */
315 static inline bfd_boolean
316 is_ir_dummy_bfd (const bfd *abfd)
318 /* ABFD can sometimes legitimately be NULL, e.g. when called from one
319 of the linker callbacks for a symbol in the *ABS* or *UND* sections. */
320 return abfd != NULL && (abfd->flags & BFD_PLUGIN) != 0;
323 /* Helpers to convert between BFD and GOLD symbol formats. */
324 static enum ld_plugin_status
325 asymbol_from_plugin_symbol (bfd *abfd, asymbol *asym,
326 const struct ld_plugin_symbol *ldsym)
328 flagword flags = BSF_NO_FLAGS;
329 struct bfd_section *section;
331 asym->the_bfd = abfd;
332 asym->name = (ldsym->version
333 ? concat (ldsym->name, "@", ldsym->version, (const char *) NULL)
343 if (ldsym->comdat_key)
345 char *name = concat (".gnu.linkonce.t.", ldsym->comdat_key,
346 (const char *) NULL);
347 section = bfd_get_section_by_name (abfd, name);
354 sflags = (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY
355 | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_EXCLUDE
356 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD);
357 section = bfd_make_section_anyway_with_flags (abfd, name, sflags);
363 section = bfd_get_section_by_name (abfd, ".text");
370 section = bfd_und_section_ptr;
375 section = bfd_com_section_ptr;
376 asym->value = ldsym->size;
377 /* For ELF targets, set alignment of common symbol to 1. */
378 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
380 ((elf_symbol_type *) asym)->internal_elf_sym.st_shndx = SHN_COMMON;
381 ((elf_symbol_type *) asym)->internal_elf_sym.st_value = 1;
389 asym->section = section;
391 /* Visibility only applies on ELF targets. */
392 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
394 elf_symbol_type *elfsym = elf_symbol_from (abfd, asym);
395 unsigned char visibility;
398 einfo (_("%P%F: %s: non-ELF symbol in ELF BFD!\n"), asym->name);
399 switch (ldsym->visibility)
402 einfo (_("%P%F: unknown ELF symbol visibility: %d!\n"),
405 visibility = STV_DEFAULT;
408 visibility = STV_PROTECTED;
411 visibility = STV_INTERNAL;
414 visibility = STV_HIDDEN;
417 elfsym->internal_elf_sym.st_other
418 = (visibility | (elfsym->internal_elf_sym.st_other
419 & ~ELF_ST_VISIBILITY (-1)));
425 /* Register a claim-file handler. */
426 static enum ld_plugin_status
427 register_claim_file (ld_plugin_claim_file_handler handler)
429 ASSERT (called_plugin);
430 called_plugin->claim_file_handler = handler;
434 /* Register an all-symbols-read handler. */
435 static enum ld_plugin_status
436 register_all_symbols_read (ld_plugin_all_symbols_read_handler handler)
438 ASSERT (called_plugin);
439 called_plugin->all_symbols_read_handler = handler;
443 /* Register a cleanup handler. */
444 static enum ld_plugin_status
445 register_cleanup (ld_plugin_cleanup_handler handler)
447 ASSERT (called_plugin);
448 called_plugin->cleanup_handler = handler;
452 /* Add symbols from a plugin-claimed input file. */
453 static enum ld_plugin_status
454 add_symbols (void *handle, int nsyms, const struct ld_plugin_symbol *syms)
457 plugin_input_file_t *input = handle;
458 bfd *abfd = input->abfd;
461 ASSERT (called_plugin);
462 symptrs = xmalloc (nsyms * sizeof *symptrs);
463 for (n = 0; n < nsyms; n++)
465 enum ld_plugin_status rv;
468 bfdsym = bfd_make_empty_symbol (abfd);
470 rv = asymbol_from_plugin_symbol (abfd, bfdsym, syms + n);
474 bfd_set_symtab (abfd, symptrs, nsyms);
478 /* Get the input file information with an open (possibly re-opened)
480 static enum ld_plugin_status
481 get_input_file (const void *handle, struct ld_plugin_input_file *file)
483 const plugin_input_file_t *input = handle;
485 ASSERT (called_plugin);
487 file->name = input->name;
488 file->offset = input->offset;
489 file->filesize = input->filesize;
490 file->handle = (void *) handle;
495 /* Get view of the input file. */
496 static enum ld_plugin_status
497 get_view (const void *handle, const void **viewp)
499 plugin_input_file_t *input = (plugin_input_file_t *) handle;
501 size_t size = input->filesize;
503 ASSERT (called_plugin);
505 /* FIXME: einfo should support %lld. */
506 if ((off_t) size != input->filesize)
507 einfo (_("%P%F: unsupported input file size: %s (%ld bytes)\n"),
508 input->name, (long) input->filesize);
510 /* Check the cached view buffer. */
511 if (input->view_buffer.addr != NULL
512 && input->view_buffer.filesize == size
513 && input->view_buffer.offset == input->offset)
515 *viewp = input->view_buffer.addr;
519 input->view_buffer.filesize = size;
520 input->view_buffer.offset = input->offset;
523 buffer = mmap (NULL, size, PROT_READ, MAP_PRIVATE, input->fd,
525 if (buffer == MAP_FAILED)
530 if (lseek (input->fd, input->offset, SEEK_SET) < 0)
533 buffer = bfd_alloc (input->abfd, size);
540 ssize_t got = read (input->fd, p, size);
548 else if (errno != EINTR)
554 input->view_buffer.addr = buffer;
560 /* Release the input file. */
561 static enum ld_plugin_status
562 release_input_file (const void *handle)
564 plugin_input_file_t *input = (plugin_input_file_t *) handle;
565 ASSERT (called_plugin);
574 /* Return TRUE if a defined symbol might be reachable from outside the
575 universe of claimed objects. */
576 static inline bfd_boolean
577 is_visible_from_outside (struct ld_plugin_symbol *lsym,
578 struct bfd_link_hash_entry *blhe)
580 struct bfd_sym_chain *sym;
582 if (link_info.relocatable)
584 if (link_info.export_dynamic || !link_info.executable)
586 /* Check if symbol is hidden by version script. */
587 if (bfd_hide_sym_by_version (link_info.version_info,
590 /* Only ELF symbols really have visibility. */
591 if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
593 struct elf_link_hash_entry *el = (struct elf_link_hash_entry *)blhe;
594 int vis = ELF_ST_VISIBILITY (el->other);
595 return vis == STV_DEFAULT || vis == STV_PROTECTED;
597 /* On non-ELF targets, we can safely make inferences by considering
598 what visibility the plugin would have liked to apply when it first
599 sent us the symbol. During ELF symbol processing, visibility only
600 ever becomes more restrictive, not less, when symbols are merged,
601 so this is a conservative estimate; it may give false positives,
602 declaring something visible from outside when it in fact would
603 not have been, but this will only lead to missed optimisation
604 opportunities during LTRANS at worst; it will not give false
605 negatives, which can lead to the disastrous conclusion that the
606 related symbol is IRONLY. (See GCC PR46319 for an example.) */
607 return (lsym->visibility == LDPV_DEFAULT
608 || lsym->visibility == LDPV_PROTECTED);
611 for (sym = &entry_symbol; sym != NULL; sym = sym->next)
613 && strcmp (sym->name, blhe->root.string) == 0)
619 /* Get the symbol resolution info for a plugin-claimed input file. */
620 static enum ld_plugin_status
621 get_symbols (const void *handle, int nsyms, struct ld_plugin_symbol *syms,
624 const plugin_input_file_t *input = handle;
625 const bfd *abfd = (const bfd *) input->abfd;
628 ASSERT (called_plugin);
629 for (n = 0; n < nsyms; n++)
631 struct bfd_link_hash_entry *blhe;
635 if (syms[n].def != LDPK_UNDEF)
636 blhe = bfd_link_hash_lookup (link_info.hash, syms[n].name,
639 blhe = bfd_wrapped_link_hash_lookup (link_info.output_bfd, &link_info,
640 syms[n].name, FALSE, FALSE, TRUE);
647 /* Determine resolution from blhe type and symbol's original type. */
648 if (blhe->type == bfd_link_hash_undefined
649 || blhe->type == bfd_link_hash_undefweak)
654 if (blhe->type != bfd_link_hash_defined
655 && blhe->type != bfd_link_hash_defweak
656 && blhe->type != bfd_link_hash_common)
658 /* We should not have a new, indirect or warning symbol here. */
659 einfo ("%P%F: %s: plugin symbol table corrupt (sym type %d)\n",
660 called_plugin->name, blhe->type);
663 /* Find out which section owns the symbol. Since it's not undef,
664 it must have an owner; if it's not a common symbol, both defs
665 and weakdefs keep it in the same place. */
666 owner_sec = (blhe->type == bfd_link_hash_common
667 ? blhe->u.c.p->section
668 : blhe->u.def.section);
671 /* If it was originally undefined or common, then it has been
672 resolved; determine how. */
673 if (syms[n].def == LDPK_UNDEF
674 || syms[n].def == LDPK_WEAKUNDEF
675 || syms[n].def == LDPK_COMMON)
677 if (owner_sec->owner == link_info.output_bfd)
678 res = LDPR_RESOLVED_EXEC;
679 else if (owner_sec->owner == abfd)
680 res = LDPR_PREVAILING_DEF_IRONLY;
681 else if (is_ir_dummy_bfd (owner_sec->owner))
682 res = LDPR_RESOLVED_IR;
683 else if (owner_sec->owner != NULL
684 && (owner_sec->owner->flags & DYNAMIC) != 0)
685 res = LDPR_RESOLVED_DYN;
687 res = LDPR_RESOLVED_EXEC;
690 /* Was originally def, or weakdef. Does it prevail? If the
691 owner is the original dummy bfd that supplied it, then this
692 is the definition that has prevailed. */
693 else if (owner_sec->owner == link_info.output_bfd)
694 res = LDPR_PREEMPTED_REG;
695 else if (owner_sec->owner == abfd)
696 res = LDPR_PREVAILING_DEF_IRONLY;
698 /* Was originally def, weakdef, or common, but has been pre-empted. */
699 else if (is_ir_dummy_bfd (owner_sec->owner))
700 res = LDPR_PREEMPTED_IR;
702 res = LDPR_PREEMPTED_REG;
704 if (res == LDPR_PREVAILING_DEF_IRONLY)
706 /* We need to know if the sym is referenced from non-IR files. Or
707 even potentially-referenced, perhaps in a future final link if
708 this is a partial one, perhaps dynamically at load-time if the
709 symbol is externally visible. */
710 if (blhe->non_ir_ref)
711 res = LDPR_PREVAILING_DEF;
712 else if (is_visible_from_outside (&syms[n], blhe))
713 res = def_ironly_exp;
717 syms[n].resolution = res;
718 if (report_plugin_symbols)
719 einfo (_("%P: %B: symbol `%s' "
720 "definition: %d, visibility: %d, resolution: %d\n"),
722 syms[n].def, syms[n].visibility, res);
727 static enum ld_plugin_status
728 get_symbols_v1 (const void *handle, int nsyms, struct ld_plugin_symbol *syms)
730 return get_symbols (handle, nsyms, syms, LDPR_PREVAILING_DEF);
733 static enum ld_plugin_status
734 get_symbols_v2 (const void *handle, int nsyms, struct ld_plugin_symbol *syms)
736 return get_symbols (handle, nsyms, syms, LDPR_PREVAILING_DEF_IRONLY_EXP);
739 /* Add a new (real) input file generated by a plugin. */
740 static enum ld_plugin_status
741 add_input_file (const char *pathname)
743 ASSERT (called_plugin);
744 if (!lang_add_input_file (xstrdup (pathname), lang_input_file_is_file_enum,
750 /* Add a new (real) library required by a plugin. */
751 static enum ld_plugin_status
752 add_input_library (const char *pathname)
754 ASSERT (called_plugin);
755 if (!lang_add_input_file (xstrdup (pathname), lang_input_file_is_l_enum,
761 /* Set the extra library path to be used by libraries added via
762 add_input_library. */
763 static enum ld_plugin_status
764 set_extra_library_path (const char *path)
766 ASSERT (called_plugin);
767 ldfile_add_library_path (xstrdup (path), FALSE);
771 /* Issue a diagnostic message from a plugin. */
772 static enum ld_plugin_status
773 message (int level, const char *format, ...)
776 va_start (args, format);
781 vfinfo (stdout, format, args, FALSE);
785 vfinfo (stdout, format, args, TRUE);
792 char *newfmt = ACONCAT ((level == LDPL_FATAL ? "%P%F: " : "%P%X: ",
793 format, "\n", (const char *) NULL));
795 vfinfo (stderr, newfmt, args, TRUE);
805 /* Helper to size leading part of tv array and set it up. */
807 set_tv_header (struct ld_plugin_tv *tv)
812 static const unsigned int major = (unsigned)(BFD_VERSION / 100000000UL);
813 static const unsigned int minor = (unsigned)(BFD_VERSION / 1000000UL) % 100;
815 for (i = 0; i < tv_header_size; i++)
817 tv[i].tv_tag = tv_header_tags[i];
818 #define TVU(x) tv[i].tv_u.tv_ ## x
819 switch (tv[i].tv_tag)
822 TVU(message) = message;
824 case LDPT_API_VERSION:
825 TVU(val) = LD_PLUGIN_API_VERSION;
827 case LDPT_GNU_LD_VERSION:
828 TVU(val) = major * 100 + minor;
830 case LDPT_LINKER_OUTPUT:
831 TVU(val) = (link_info.relocatable
833 : (link_info.executable
834 ? (link_info.pie ? LDPO_PIE : LDPO_EXEC)
837 case LDPT_OUTPUT_NAME:
838 TVU(string) = output_filename;
840 case LDPT_REGISTER_CLAIM_FILE_HOOK:
841 TVU(register_claim_file) = register_claim_file;
843 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
844 TVU(register_all_symbols_read) = register_all_symbols_read;
846 case LDPT_REGISTER_CLEANUP_HOOK:
847 TVU(register_cleanup) = register_cleanup;
849 case LDPT_ADD_SYMBOLS:
850 TVU(add_symbols) = add_symbols;
852 case LDPT_GET_INPUT_FILE:
853 TVU(get_input_file) = get_input_file;
856 TVU(get_view) = get_view;
858 case LDPT_RELEASE_INPUT_FILE:
859 TVU(release_input_file) = release_input_file;
861 case LDPT_GET_SYMBOLS:
862 TVU(get_symbols) = get_symbols_v1;
864 case LDPT_GET_SYMBOLS_V2:
865 TVU(get_symbols) = get_symbols_v2;
867 case LDPT_ADD_INPUT_FILE:
868 TVU(add_input_file) = add_input_file;
870 case LDPT_ADD_INPUT_LIBRARY:
871 TVU(add_input_library) = add_input_library;
873 case LDPT_SET_EXTRA_LIBRARY_PATH:
874 TVU(set_extra_library_path) = set_extra_library_path;
877 /* Added a new entry to the array without adding
878 a new case to set up its value is a bug. */
885 /* Append the per-plugin args list and trailing LDPT_NULL to tv. */
887 set_tv_plugin_args (plugin_t *plugin, struct ld_plugin_tv *tv)
889 plugin_arg_t *arg = plugin->args;
892 tv->tv_tag = LDPT_OPTION;
893 tv->tv_u.tv_string = arg->arg;
897 tv->tv_tag = LDPT_NULL;
901 /* Load up and initialise all plugins after argument parsing. */
903 plugin_load_plugins (void)
905 struct ld_plugin_tv *my_tv;
906 unsigned int max_args = 0;
907 plugin_t *curplug = plugins_list;
909 /* If there are no plugins, we need do nothing this run. */
913 /* First pass over plugins to find max # args needed so that we
914 can size and allocate the tv array. */
917 if (curplug->n_args > max_args)
918 max_args = curplug->n_args;
919 curplug = curplug->next;
922 /* Allocate tv array and initialise constant part. */
923 my_tv = xmalloc ((max_args + 1 + tv_header_size) * sizeof *my_tv);
924 set_tv_header (my_tv);
926 /* Pass over plugins again, activating them. */
927 curplug = plugins_list;
930 enum ld_plugin_status rv;
931 ld_plugin_onload onloadfn;
933 onloadfn = (ld_plugin_onload) dlsym (curplug->dlhandle, "onload");
935 onloadfn = (ld_plugin_onload) dlsym (curplug->dlhandle, "_onload");
937 einfo (_("%P%F: %s: error loading plugin: %s\n"),
938 curplug->name, dlerror ());
939 set_tv_plugin_args (curplug, &my_tv[tv_header_size]);
940 called_plugin = curplug;
941 rv = (*onloadfn) (my_tv);
942 called_plugin = NULL;
944 einfo (_("%P%F: %s: plugin error: %d\n"), curplug->name, rv);
945 curplug = curplug->next;
948 /* Since plugin(s) inited ok, assume they're going to want symbol
949 resolutions, which needs us to track which symbols are referenced
950 by non-IR files using the linker's notice callback. */
951 orig_notice_all = link_info.notice_all;
952 orig_callbacks = link_info.callbacks;
953 plugin_callbacks = *orig_callbacks;
954 plugin_callbacks.notice = &plugin_notice;
955 link_info.notice_all = TRUE;
956 link_info.lto_plugin_active = TRUE;
957 link_info.callbacks = &plugin_callbacks;
960 /* Call 'claim file' hook for all plugins. */
962 plugin_call_claim_file (const struct ld_plugin_input_file *file, int *claimed)
964 plugin_t *curplug = plugins_list;
966 if (no_more_claiming)
968 while (curplug && !*claimed)
970 if (curplug->claim_file_handler)
972 enum ld_plugin_status rv;
973 called_plugin = curplug;
974 rv = (*curplug->claim_file_handler) (file, claimed);
975 called_plugin = NULL;
977 set_plugin_error (curplug->name);
979 curplug = curplug->next;
981 return plugin_error_p () ? -1 : 0;
984 /* Duplicates a character string with memory attached to ABFD. */
987 plugin_strdup (bfd *abfd, const char *str)
991 strlength = strlen (str) + 1;
992 copy = bfd_alloc (abfd, strlength);
994 einfo (_("%P%F: plugin_strdup failed to allocate memory: %s\n"),
996 memcpy (copy, str, strlength);
1001 plugin_maybe_claim (lang_input_statement_type *entry)
1004 plugin_input_file_t *input;
1005 off_t offset, filesize;
1006 struct ld_plugin_input_file file;
1008 bfd *ibfd = entry->the_bfd;
1009 bfd_boolean inarchive = bfd_my_archive (ibfd) != NULL;
1011 = inarchive ? bfd_my_archive (ibfd)->filename : ibfd->filename;
1012 int fd = open (name, O_RDONLY | O_BINARY);
1017 /* We create a dummy BFD, initially empty, to house whatever symbols
1018 the plugin may want to add. */
1019 abfd = plugin_get_ir_dummy_bfd (ibfd->filename, ibfd);
1021 input = bfd_alloc (abfd, sizeof (*input));
1023 einfo (_("%P%F: plugin failed to allocate memory for input: %s\n"),
1028 /* Offset and filesize must refer to the individual archive
1029 member, not the whole file, and must exclude the header.
1030 Fortunately for us, that is how the data is stored in the
1031 origin field of the bfd and in the arelt_data. */
1032 offset = ibfd->origin;
1033 filesize = arelt_size (ibfd);
1038 filesize = lseek (fd, 0, SEEK_END);
1040 /* We must copy filename attached to ibfd if it is not an archive
1041 member since it may be freed by bfd_close below. */
1042 name = plugin_strdup (abfd, name);
1046 file.offset = offset;
1047 file.filesize = filesize;
1049 file.handle = input;
1052 input->view_buffer.addr = NULL;
1053 input->view_buffer.filesize = 0;
1054 input->view_buffer.offset = 0;
1056 input->offset = offset;
1057 input->filesize = filesize;
1058 input->name = plugin_strdup (abfd, ibfd->filename);
1060 if (plugin_call_claim_file (&file, &claimed))
1061 einfo (_("%P%F: %s: plugin reported error claiming file\n"),
1062 plugin_error_plugin ());
1064 if (input->fd != -1 && bfd_check_format (ibfd, bfd_object))
1066 /* FIXME: fd belongs to us, not the plugin. IR for GCC plugin,
1067 which doesn't need fd after plugin_call_claim_file, is
1068 stored in bfd_object file. Since GCC plugin before GCC 5
1069 doesn't call release_input_file, we close it here. IR for
1070 LLVM plugin, which needs fd after plugin_call_claim_file and
1071 calls release_input_file after it is done, is stored in
1072 non-bfd_object file. This scheme doesn't work when a plugin
1073 needs fd and its IR is stored in bfd_object file. */
1080 /* Discard the real file's BFD and substitute the dummy one. */
1082 /* BFD archive handling caches elements so we can't call
1083 bfd_close for archives. */
1086 bfd_make_readable (abfd);
1087 entry->the_bfd = abfd;
1088 entry->flags.claimed = TRUE;
1092 /* If plugin didn't claim the file, we don't need the dummy bfd.
1093 Can't avoid speculatively creating it, alas. */
1094 bfd_close_all_done (abfd);
1095 entry->flags.claimed = FALSE;
1099 /* Call 'all symbols read' hook for all plugins. */
1101 plugin_call_all_symbols_read (void)
1103 plugin_t *curplug = plugins_list;
1105 /* Disable any further file-claiming. */
1106 no_more_claiming = TRUE;
1110 if (curplug->all_symbols_read_handler)
1112 enum ld_plugin_status rv;
1113 called_plugin = curplug;
1114 rv = (*curplug->all_symbols_read_handler) ();
1115 called_plugin = NULL;
1117 set_plugin_error (curplug->name);
1119 curplug = curplug->next;
1121 return plugin_error_p () ? -1 : 0;
1124 /* Call 'cleanup' hook for all plugins at exit. */
1126 plugin_call_cleanup (void)
1128 plugin_t *curplug = plugins_list;
1131 if (curplug->cleanup_handler && !curplug->cleanup_done)
1133 enum ld_plugin_status rv;
1134 curplug->cleanup_done = TRUE;
1135 called_plugin = curplug;
1136 rv = (*curplug->cleanup_handler) ();
1137 called_plugin = NULL;
1139 info_msg (_("%P: %s: error in plugin cleanup: %d (ignored)\n"),
1141 dlclose (curplug->dlhandle);
1143 curplug = curplug->next;
1147 /* To determine which symbols should be resolved LDPR_PREVAILING_DEF
1148 and which LDPR_PREVAILING_DEF_IRONLY, we notice all the symbols as
1149 the linker adds them to the linker hash table. Mark those
1150 referenced from a non-IR file with non_ir_ref. We have to
1151 notice_all symbols, because we won't necessarily know until later
1152 which ones will be contributed by IR files. */
1154 plugin_notice (struct bfd_link_info *info,
1155 struct bfd_link_hash_entry *h,
1156 struct bfd_link_hash_entry *inh,
1162 struct bfd_link_hash_entry *orig_h = h;
1168 if (h->type == bfd_link_hash_warning)
1171 /* Nothing to do here if this def/ref is from an IR dummy BFD. */
1172 if (is_ir_dummy_bfd (abfd))
1175 /* Making an indirect symbol counts as a reference unless this
1176 is a brand new symbol. */
1177 else if (bfd_is_ind_section (section)
1178 || (flags & BSF_INDIRECT) != 0)
1180 /* ??? Some of this is questionable. See comments in
1181 _bfd_generic_link_add_one_symbol for case IND. */
1182 if (h->type != bfd_link_hash_new)
1184 h->non_ir_ref = TRUE;
1185 inh->non_ir_ref = TRUE;
1187 else if (inh->type == bfd_link_hash_new)
1188 inh->non_ir_ref = TRUE;
1191 /* Nothing to do here for warning symbols. */
1192 else if ((flags & BSF_WARNING) != 0)
1195 /* Nothing to do here for constructor symbols. */
1196 else if ((flags & BSF_CONSTRUCTOR) != 0)
1199 /* If this is a ref, set non_ir_ref. */
1200 else if (bfd_is_und_section (section))
1202 /* Replace the undefined dummy bfd with the real one. */
1203 if ((h->type == bfd_link_hash_undefined
1204 || h->type == bfd_link_hash_undefweak)
1205 && (h->u.undef.abfd == NULL
1206 || (h->u.undef.abfd->flags & BFD_PLUGIN) != 0))
1207 h->u.undef.abfd = abfd;
1208 h->non_ir_ref = TRUE;
1211 /* Otherwise, it must be a new def. Ensure any symbol defined
1212 in an IR dummy BFD takes on a new value from a real BFD.
1213 Weak symbols are not normally overridden by a new weak
1214 definition, and strong symbols will normally cause multiple
1215 definition errors. Avoid this by making the symbol appear
1217 else if (((h->type == bfd_link_hash_defweak
1218 || h->type == bfd_link_hash_defined)
1219 && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
1220 || (h->type == bfd_link_hash_common
1221 && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner)))
1223 h->type = bfd_link_hash_undefweak;
1224 h->u.undef.abfd = sym_bfd;
1228 /* Continue with cref/nocrossref/trace-sym processing. */
1231 || (info->notice_hash != NULL
1232 && bfd_hash_lookup (info->notice_hash, orig_h->root.string,
1233 FALSE, FALSE) != NULL))
1234 return (*orig_callbacks->notice) (info, orig_h, inh,
1235 abfd, section, value, flags);