1 /* Plugin control for the GNU linker.
2 Copyright (C) 2010-2019 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"
32 #include "plugin-api.h"
33 #include "../bfd/plugin.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;
107 bfd_boolean use_mmap;
110 } plugin_input_file_t;
112 /* The master list of all plugins. */
113 static plugin_t *plugins_list = NULL;
115 /* We keep a tail pointer for easy linking on the end. */
116 static plugin_t **plugins_tail_chain_ptr = &plugins_list;
118 /* The last plugin added to the list, for receiving args. */
119 static plugin_t *last_plugin = NULL;
121 /* This is set when we ignored last plugin */
122 static bfd_boolean last_plugin_ignored;
124 /* The tail of the arg chain of the last plugin added to the list. */
125 static plugin_arg_t **last_plugin_args_tail_chain_ptr = NULL;
127 /* The plugin which is currently having a callback executed. */
128 static plugin_t *called_plugin = NULL;
130 /* Last plugin to cause an error, if any. */
131 static const char *error_plugin = NULL;
133 /* State of linker "notice" interface before we poked at it. */
134 static bfd_boolean orig_notice_all;
136 /* Original linker callbacks, and the plugin version. */
137 static const struct bfd_link_callbacks *orig_callbacks;
138 static struct bfd_link_callbacks plugin_callbacks;
140 /* Set at all symbols read time, to avoid recursively offering the plugin
141 its own newly-added input files and libs to claim. */
142 bfd_boolean no_more_claiming = FALSE;
144 #if HAVE_MMAP && HAVE_GETPAGESIZE
145 /* Page size used by mmap. */
146 static off_t plugin_pagesize;
149 /* List of tags to set in the constant leading part of the tv array. */
150 static const enum ld_plugin_tag tv_header_tags[] =
157 LDPT_REGISTER_CLAIM_FILE_HOOK,
158 LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK,
159 LDPT_REGISTER_CLEANUP_HOOK,
163 LDPT_RELEASE_INPUT_FILE,
167 LDPT_ADD_INPUT_LIBRARY,
168 LDPT_SET_EXTRA_LIBRARY_PATH
171 /* How many entries in the constant leading part of the tv array. */
172 static const size_t tv_header_size = ARRAY_SIZE (tv_header_tags);
174 /* Forward references. */
175 static bfd_boolean plugin_notice (struct bfd_link_info *,
176 struct bfd_link_hash_entry *,
177 struct bfd_link_hash_entry *,
178 bfd *, asection *, bfd_vma, flagword);
180 static const bfd_target * plugin_object_p (bfd *);
182 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
184 #define RTLD_NOW 0 /* Dummy value. */
187 dlopen (const char *file, int mode ATTRIBUTE_UNUSED)
189 return LoadLibrary (file);
193 dlsym (void *handle, const char *name)
195 return GetProcAddress (handle, name);
199 dlclose (void *handle)
201 FreeLibrary (handle);
205 #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */
215 /* Helper function for exiting with error status. */
217 set_plugin_error (const char *plugin)
219 error_plugin = plugin;
223 /* Test if an error occurred. */
225 plugin_error_p (void)
227 return error_plugin != NULL;
230 /* Return name of plugin which caused an error if any. */
232 plugin_error_plugin (void)
234 return error_plugin ? error_plugin : _("<no plugin>");
237 /* Handle -plugin arg: find and load plugin, or return error. */
239 plugin_opt_plugin (const char *plugin)
242 plugin_t *curplug = plugins_list;
244 newplug = xmalloc (sizeof *newplug);
245 memset (newplug, 0, sizeof *newplug);
246 newplug->name = plugin;
247 newplug->dlhandle = dlopen (plugin, RTLD_NOW);
248 if (!newplug->dlhandle) {
249 #ifdef IGNORE_BROKEN_PLUGINS
250 einfo (_("%P: %s: error loading plugin: %s\n"), plugin, dlerror ());
251 last_plugin_ignored=TRUE;
255 einfo (_("%P%F: %s: error loading plugin: %s\n"), plugin, dlerror ());
259 /* Check if plugin has been loaded already. */
262 if (newplug->dlhandle == curplug->dlhandle)
264 einfo (_("%P: %s: duplicated plugin\n"), plugin);
268 curplug = curplug->next;
271 /* Chain on end, so when we run list it is in command-line order. */
272 *plugins_tail_chain_ptr = newplug;
273 plugins_tail_chain_ptr = &newplug->next;
275 /* Record it as current plugin for receiving args. */
276 last_plugin = newplug;
277 last_plugin_args_tail_chain_ptr = &newplug->args;
278 #ifdef IGNORE_BROKEN_PLUGINS
279 last_plugin_ignored=FALSE;
283 /* Accumulate option arguments for last-loaded plugin, or return
286 plugin_opt_plugin_arg (const char *arg)
288 #ifdef IGNORE_BROKEN_PLUGINS
289 /* Ignore options for ignored plugin */
290 if (last_plugin_ignored)
293 plugin_arg_t *newarg;
296 return set_plugin_error (_("<no plugin>"));
298 /* Ignore -pass-through= from GCC driver. */
301 const char *p = arg + 1;
305 if (strncmp (p, "pass-through=", 13) == 0)
309 newarg = xmalloc (sizeof *newarg);
313 /* Chain on end to preserve command-line order. */
314 *last_plugin_args_tail_chain_ptr = newarg;
315 last_plugin_args_tail_chain_ptr = &newarg->next;
316 last_plugin->n_args++;
320 /* Generate a dummy BFD to represent an IR file, for any callers of
321 plugin_call_claim_file to use as the handle in the ld_plugin_input_file
322 struct that they build to pass in. The BFD is initially writable, so
323 that symbols can be added to it; it must be made readable after the
324 add_symbols hook has been called so that it can be read when linking. */
326 plugin_get_ir_dummy_bfd (const char *name, bfd *srctemplate)
329 bfd_boolean bfd_plugin_target;
331 bfd_use_reserved_id = 1;
332 bfd_plugin_target = bfd_plugin_target_p (srctemplate->xvec);
333 abfd = bfd_create (concat (name, IRONLY_SUFFIX, (const char *) NULL),
334 bfd_plugin_target ? link_info.output_bfd : srctemplate);
337 abfd->flags |= BFD_LINKER_CREATED | BFD_PLUGIN;
338 if (!bfd_make_writable (abfd))
340 if (!bfd_plugin_target)
342 bfd_set_arch_info (abfd, bfd_get_arch_info (srctemplate));
343 bfd_set_gp_size (abfd, bfd_get_gp_size (srctemplate));
344 if (!bfd_copy_private_bfd_data (srctemplate, abfd))
350 /* Create section to own the symbols. */
351 flags = (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY
352 | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_EXCLUDE);
353 if (bfd_make_section_anyway_with_flags (abfd, ".text", flags))
358 einfo (_("%F%P: could not create dummy IR bfd: %E\n"));
362 /* Check if the BFD passed in is an IR dummy object file. */
363 static inline bfd_boolean
364 is_ir_dummy_bfd (const bfd *abfd)
366 /* ABFD can sometimes legitimately be NULL, e.g. when called from one
367 of the linker callbacks for a symbol in the *ABS* or *UND* sections. */
368 return abfd != NULL && (abfd->flags & BFD_PLUGIN) != 0;
371 /* Helpers to convert between BFD and GOLD symbol formats. */
372 static enum ld_plugin_status
373 asymbol_from_plugin_symbol (bfd *abfd, asymbol *asym,
374 const struct ld_plugin_symbol *ldsym)
376 flagword flags = BSF_NO_FLAGS;
377 struct bfd_section *section;
379 asym->the_bfd = abfd;
380 asym->name = (ldsym->version
381 ? concat (ldsym->name, "@", ldsym->version, (const char *) NULL)
391 if (ldsym->comdat_key)
393 char *name = concat (".gnu.linkonce.t.", ldsym->comdat_key,
394 (const char *) NULL);
395 section = bfd_get_section_by_name (abfd, name);
402 sflags = (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY
403 | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_EXCLUDE
404 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD);
405 section = bfd_make_section_anyway_with_flags (abfd, name, sflags);
411 section = bfd_get_section_by_name (abfd, ".text");
418 section = bfd_und_section_ptr;
423 section = bfd_com_section_ptr;
424 asym->value = ldsym->size;
425 /* For ELF targets, set alignment of common symbol to 1. */
426 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
428 ((elf_symbol_type *) asym)->internal_elf_sym.st_shndx = SHN_COMMON;
429 ((elf_symbol_type *) asym)->internal_elf_sym.st_value = 1;
437 asym->section = section;
439 /* Visibility only applies on ELF targets. */
440 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
442 elf_symbol_type *elfsym = elf_symbol_from (abfd, asym);
443 unsigned char visibility;
446 einfo (_("%F%P: %s: non-ELF symbol in ELF BFD!\n"), asym->name);
447 switch (ldsym->visibility)
450 einfo (_("%F%P: unknown ELF symbol visibility: %d!\n"),
455 visibility = STV_DEFAULT;
458 visibility = STV_PROTECTED;
461 visibility = STV_INTERNAL;
464 visibility = STV_HIDDEN;
467 elfsym->internal_elf_sym.st_other
468 = (visibility | (elfsym->internal_elf_sym.st_other
469 & ~ELF_ST_VISIBILITY (-1)));
475 /* Register a claim-file handler. */
476 static enum ld_plugin_status
477 register_claim_file (ld_plugin_claim_file_handler handler)
479 ASSERT (called_plugin);
480 called_plugin->claim_file_handler = handler;
484 /* Register an all-symbols-read handler. */
485 static enum ld_plugin_status
486 register_all_symbols_read (ld_plugin_all_symbols_read_handler handler)
488 ASSERT (called_plugin);
489 called_plugin->all_symbols_read_handler = handler;
493 /* Register a cleanup handler. */
494 static enum ld_plugin_status
495 register_cleanup (ld_plugin_cleanup_handler handler)
497 ASSERT (called_plugin);
498 called_plugin->cleanup_handler = handler;
502 /* Add symbols from a plugin-claimed input file. */
503 static enum ld_plugin_status
504 add_symbols (void *handle, int nsyms, const struct ld_plugin_symbol *syms)
507 plugin_input_file_t *input = handle;
508 bfd *abfd = input->abfd;
511 ASSERT (called_plugin);
512 symptrs = xmalloc (nsyms * sizeof *symptrs);
513 for (n = 0; n < nsyms; n++)
515 enum ld_plugin_status rv;
518 bfdsym = bfd_make_empty_symbol (abfd);
520 rv = asymbol_from_plugin_symbol (abfd, bfdsym, syms + n);
524 bfd_set_symtab (abfd, symptrs, nsyms);
528 /* Get the input file information with an open (possibly re-opened)
530 static enum ld_plugin_status
531 get_input_file (const void *handle, struct ld_plugin_input_file *file)
533 const plugin_input_file_t *input = handle;
535 ASSERT (called_plugin);
537 file->name = input->name;
538 file->offset = input->offset;
539 file->filesize = input->filesize;
540 file->handle = (void *) handle;
545 /* Get view of the input file. */
546 static enum ld_plugin_status
547 get_view (const void *handle, const void **viewp)
549 plugin_input_file_t *input = (plugin_input_file_t *) handle;
551 size_t size = input->filesize;
552 off_t offset = input->offset;
553 #if HAVE_MMAP && HAVE_GETPAGESIZE
557 ASSERT (called_plugin);
559 /* FIXME: einfo should support %lld. */
560 if ((off_t) size != input->filesize)
561 einfo (_("%F%P: unsupported input file size: %s (%ld bytes)\n"),
562 input->name, (long) input->filesize);
564 /* Check the cached view buffer. */
565 if (input->view_buffer.addr != NULL
566 && input->view_buffer.filesize == size
567 && input->view_buffer.offset == offset)
569 *viewp = input->view_buffer.addr;
573 input->view_buffer.filesize = size;
574 input->view_buffer.offset = offset;
577 # if HAVE_GETPAGESIZE
578 bias = offset % plugin_pagesize;
582 buffer = mmap (NULL, size, PROT_READ, MAP_PRIVATE, input->fd, offset);
583 if (buffer != MAP_FAILED)
585 input->use_mmap = TRUE;
586 # if HAVE_GETPAGESIZE
595 input->use_mmap = FALSE;
597 if (lseek (input->fd, offset, SEEK_SET) < 0)
600 buffer = bfd_alloc (input->abfd, size);
607 ssize_t got = read (input->fd, p, size);
615 else if (errno != EINTR)
621 input->view_buffer.addr = buffer;
627 /* Release the input file. */
628 static enum ld_plugin_status
629 release_input_file (const void *handle)
631 plugin_input_file_t *input = (plugin_input_file_t *) handle;
632 ASSERT (called_plugin);
641 /* Return TRUE if a defined symbol might be reachable from outside the
642 universe of claimed objects. */
643 static inline bfd_boolean
644 is_visible_from_outside (struct ld_plugin_symbol *lsym,
645 struct bfd_link_hash_entry *blhe)
647 if (bfd_link_relocatable (&link_info))
649 if (blhe->non_ir_ref_dynamic
650 || link_info.export_dynamic
651 || bfd_link_dll (&link_info))
653 /* Check if symbol is hidden by version script. */
654 if (bfd_hide_sym_by_version (link_info.version_info,
657 /* Only ELF symbols really have visibility. */
658 if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
660 struct elf_link_hash_entry *el = (struct elf_link_hash_entry *)blhe;
661 int vis = ELF_ST_VISIBILITY (el->other);
662 return vis == STV_DEFAULT || vis == STV_PROTECTED;
664 /* On non-ELF targets, we can safely make inferences by considering
665 what visibility the plugin would have liked to apply when it first
666 sent us the symbol. During ELF symbol processing, visibility only
667 ever becomes more restrictive, not less, when symbols are merged,
668 so this is a conservative estimate; it may give false positives,
669 declaring something visible from outside when it in fact would
670 not have been, but this will only lead to missed optimisation
671 opportunities during LTRANS at worst; it will not give false
672 negatives, which can lead to the disastrous conclusion that the
673 related symbol is IRONLY. (See GCC PR46319 for an example.) */
674 return (lsym->visibility == LDPV_DEFAULT
675 || lsym->visibility == LDPV_PROTECTED);
681 /* Return LTO kind string name that corresponds to IDX enum value. */
683 get_lto_kind (unsigned int idx)
685 static char buffer[64];
686 const char *lto_kind_str[5] =
695 if (idx < ARRAY_SIZE (lto_kind_str))
696 return lto_kind_str [idx];
698 sprintf (buffer, _("unknown LTO kind value %x"), idx);
702 /* Return LTO resolution string name that corresponds to IDX enum value. */
704 get_lto_resolution (unsigned int idx)
706 static char buffer[64];
707 static const char *lto_resolution_str[10] =
712 "PREVAILING_DEF_IRONLY",
718 "PREVAILING_DEF_IRONLY_EXP",
721 if (idx < ARRAY_SIZE (lto_resolution_str))
722 return lto_resolution_str [idx];
724 sprintf (buffer, _("unknown LTO resolution value %x"), idx);
728 /* Return LTO visibility string name that corresponds to IDX enum value. */
730 get_lto_visibility (unsigned int idx)
732 static char buffer[64];
733 const char *lto_visibility_str[4] =
741 if (idx < ARRAY_SIZE (lto_visibility_str))
742 return lto_visibility_str [idx];
744 sprintf (buffer, _("unknown LTO visibility value %x"), idx);
748 /* Get the symbol resolution info for a plugin-claimed input file. */
749 static enum ld_plugin_status
750 get_symbols (const void *handle, int nsyms, struct ld_plugin_symbol *syms,
753 const plugin_input_file_t *input = handle;
754 const bfd *abfd = (const bfd *) input->abfd;
757 ASSERT (called_plugin);
758 for (n = 0; n < nsyms; n++)
760 struct bfd_link_hash_entry *blhe;
763 struct bfd_link_hash_entry *h
764 = bfd_link_hash_lookup (link_info.hash, syms[n].name,
766 enum { wrap_none, wrapper, wrapped } wrap_status = wrap_none;
768 if (syms[n].def != LDPK_UNDEF && syms[n].def != LDPK_WEAKUNDEF)
771 if (blhe && link_info.wrap_hash != NULL)
773 /* Check if a symbol is a wrapper symbol. */
774 struct bfd_link_hash_entry *unwrap
775 = unwrap_hash_lookup (&link_info, (bfd *) abfd, blhe);
776 if (unwrap && unwrap != h)
777 wrap_status = wrapper;
782 blhe = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
783 &link_info, syms[n].name,
785 /* Check if a symbol is a wrapped symbol. */
786 if (blhe && blhe != h)
787 wrap_status = wrapped;
791 /* The plugin is called to claim symbols in an archive element
792 from plugin_object_p. But those symbols aren't needed to
793 create output. They are defined and referenced only within
806 res = LDPR_PREVAILING_DEF_IRONLY;
812 /* Determine resolution from blhe type and symbol's original type. */
813 if (blhe->type == bfd_link_hash_undefined
814 || blhe->type == bfd_link_hash_undefweak)
819 if (blhe->type != bfd_link_hash_defined
820 && blhe->type != bfd_link_hash_defweak
821 && blhe->type != bfd_link_hash_common)
823 /* We should not have a new, indirect or warning symbol here. */
824 einfo (_("%F%P: %s: plugin symbol table corrupt (sym type %d)\n"),
825 called_plugin->name, blhe->type);
828 /* Find out which section owns the symbol. Since it's not undef,
829 it must have an owner; if it's not a common symbol, both defs
830 and weakdefs keep it in the same place. */
831 owner_sec = (blhe->type == bfd_link_hash_common
832 ? blhe->u.c.p->section
833 : blhe->u.def.section);
836 /* If it was originally undefined or common, then it has been
837 resolved; determine how. */
838 if (syms[n].def == LDPK_UNDEF
839 || syms[n].def == LDPK_WEAKUNDEF
840 || syms[n].def == LDPK_COMMON)
842 if (owner_sec->owner == link_info.output_bfd)
843 res = LDPR_RESOLVED_EXEC;
844 else if (owner_sec->owner == abfd)
845 res = LDPR_PREVAILING_DEF_IRONLY;
846 else if (is_ir_dummy_bfd (owner_sec->owner))
847 res = LDPR_RESOLVED_IR;
848 else if (owner_sec->owner != NULL
849 && (owner_sec->owner->flags & DYNAMIC) != 0)
850 res = LDPR_RESOLVED_DYN;
852 res = LDPR_RESOLVED_EXEC;
855 /* Was originally def, or weakdef. Does it prevail? If the
856 owner is the original dummy bfd that supplied it, then this
857 is the definition that has prevailed. */
858 else if (owner_sec->owner == link_info.output_bfd)
859 res = LDPR_PREEMPTED_REG;
860 else if (owner_sec->owner == abfd)
861 res = LDPR_PREVAILING_DEF_IRONLY;
863 /* Was originally def, weakdef, or common, but has been pre-empted. */
864 else if (is_ir_dummy_bfd (owner_sec->owner))
865 res = LDPR_PREEMPTED_IR;
867 res = LDPR_PREEMPTED_REG;
869 if (res == LDPR_PREVAILING_DEF_IRONLY)
871 /* We need to know if the sym is referenced from non-IR files. Or
872 even potentially-referenced, perhaps in a future final link if
873 this is a partial one, perhaps dynamically at load-time if the
874 symbol is externally visible. Also check for wrapper symbol. */
875 if (blhe->non_ir_ref_regular || wrap_status == wrapper)
876 res = LDPR_PREVAILING_DEF;
877 else if (wrap_status == wrapped)
878 res = LDPR_RESOLVED_IR;
879 else if (is_visible_from_outside (&syms[n], blhe))
880 res = def_ironly_exp;
884 syms[n].resolution = res;
885 if (report_plugin_symbols)
886 einfo (_("%P: %pB: symbol `%s' "
887 "definition: %s, visibility: %s, resolution: %s\n"),
889 get_lto_kind (syms[n].def),
890 get_lto_visibility (syms[n].visibility),
891 get_lto_resolution (res));
896 static enum ld_plugin_status
897 get_symbols_v1 (const void *handle, int nsyms, struct ld_plugin_symbol *syms)
899 return get_symbols (handle, nsyms, syms, LDPR_PREVAILING_DEF);
902 static enum ld_plugin_status
903 get_symbols_v2 (const void *handle, int nsyms, struct ld_plugin_symbol *syms)
905 return get_symbols (handle, nsyms, syms, LDPR_PREVAILING_DEF_IRONLY_EXP);
908 /* Add a new (real) input file generated by a plugin. */
909 static enum ld_plugin_status
910 add_input_file (const char *pathname)
912 lang_input_statement_type *is;
914 ASSERT (called_plugin);
915 is = lang_add_input_file (xstrdup (pathname), lang_input_file_is_file_enum,
919 is->flags.lto_output = 1;
923 /* Add a new (real) library required by a plugin. */
924 static enum ld_plugin_status
925 add_input_library (const char *pathname)
927 lang_input_statement_type *is;
929 ASSERT (called_plugin);
930 is = lang_add_input_file (xstrdup (pathname), lang_input_file_is_l_enum,
934 is->flags.lto_output = 1;
938 /* Set the extra library path to be used by libraries added via
939 add_input_library. */
940 static enum ld_plugin_status
941 set_extra_library_path (const char *path)
943 ASSERT (called_plugin);
944 ldfile_add_library_path (xstrdup (path), FALSE);
948 /* Issue a diagnostic message from a plugin. */
949 static enum ld_plugin_status
950 message (int level, const char *format, ...)
953 va_start (args, format);
958 vfinfo (stdout, format, args, FALSE);
963 char *newfmt = concat (_("%P: warning: "), format, "\n",
964 (const char *) NULL);
965 vfinfo (stdout, newfmt, args, TRUE);
973 char *newfmt = concat (level == LDPL_FATAL ? "%F" : "%X",
974 _("%P: error: "), format, "\n",
975 (const char *) NULL);
977 vfinfo (stderr, newfmt, args, TRUE);
988 /* Helper to size leading part of tv array and set it up. */
990 set_tv_header (struct ld_plugin_tv *tv)
995 static const unsigned int major = (unsigned)(BFD_VERSION / 100000000UL);
996 static const unsigned int minor = (unsigned)(BFD_VERSION / 1000000UL) % 100;
998 for (i = 0; i < tv_header_size; i++)
1000 tv[i].tv_tag = tv_header_tags[i];
1001 #define TVU(x) tv[i].tv_u.tv_ ## x
1002 switch (tv[i].tv_tag)
1005 TVU(message) = message;
1007 case LDPT_API_VERSION:
1008 TVU(val) = LD_PLUGIN_API_VERSION;
1010 case LDPT_GNU_LD_VERSION:
1011 TVU(val) = major * 100 + minor;
1013 case LDPT_LINKER_OUTPUT:
1014 TVU(val) = (bfd_link_relocatable (&link_info) ? LDPO_REL
1015 : bfd_link_pde (&link_info) ? LDPO_EXEC
1016 : bfd_link_pie (&link_info) ? LDPO_PIE
1019 case LDPT_OUTPUT_NAME:
1020 TVU(string) = output_filename;
1022 case LDPT_REGISTER_CLAIM_FILE_HOOK:
1023 TVU(register_claim_file) = register_claim_file;
1025 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
1026 TVU(register_all_symbols_read) = register_all_symbols_read;
1028 case LDPT_REGISTER_CLEANUP_HOOK:
1029 TVU(register_cleanup) = register_cleanup;
1031 case LDPT_ADD_SYMBOLS:
1032 TVU(add_symbols) = add_symbols;
1034 case LDPT_GET_INPUT_FILE:
1035 TVU(get_input_file) = get_input_file;
1038 TVU(get_view) = get_view;
1040 case LDPT_RELEASE_INPUT_FILE:
1041 TVU(release_input_file) = release_input_file;
1043 case LDPT_GET_SYMBOLS:
1044 TVU(get_symbols) = get_symbols_v1;
1046 case LDPT_GET_SYMBOLS_V2:
1047 TVU(get_symbols) = get_symbols_v2;
1049 case LDPT_ADD_INPUT_FILE:
1050 TVU(add_input_file) = add_input_file;
1052 case LDPT_ADD_INPUT_LIBRARY:
1053 TVU(add_input_library) = add_input_library;
1055 case LDPT_SET_EXTRA_LIBRARY_PATH:
1056 TVU(set_extra_library_path) = set_extra_library_path;
1059 /* Added a new entry to the array without adding
1060 a new case to set up its value is a bug. */
1067 /* Append the per-plugin args list and trailing LDPT_NULL to tv. */
1069 set_tv_plugin_args (plugin_t *plugin, struct ld_plugin_tv *tv)
1071 plugin_arg_t *arg = plugin->args;
1074 tv->tv_tag = LDPT_OPTION;
1075 tv->tv_u.tv_string = arg->arg;
1079 tv->tv_tag = LDPT_NULL;
1080 tv->tv_u.tv_val = 0;
1083 /* Load up and initialise all plugins after argument parsing. */
1085 plugin_load_plugins (void)
1087 struct ld_plugin_tv *my_tv;
1088 unsigned int max_args = 0;
1089 plugin_t *curplug = plugins_list;
1091 /* If there are no plugins, we need do nothing this run. */
1095 /* First pass over plugins to find max # args needed so that we
1096 can size and allocate the tv array. */
1099 if (curplug->n_args > max_args)
1100 max_args = curplug->n_args;
1101 curplug = curplug->next;
1104 /* Allocate tv array and initialise constant part. */
1105 my_tv = xmalloc ((max_args + 1 + tv_header_size) * sizeof *my_tv);
1106 set_tv_header (my_tv);
1108 /* Pass over plugins again, activating them. */
1109 curplug = plugins_list;
1112 enum ld_plugin_status rv;
1113 ld_plugin_onload onloadfn;
1115 onloadfn = (ld_plugin_onload) dlsym (curplug->dlhandle, "onload");
1117 onloadfn = (ld_plugin_onload) dlsym (curplug->dlhandle, "_onload");
1119 einfo (_("%F%P: %s: error loading plugin: %s\n"),
1120 curplug->name, dlerror ());
1121 set_tv_plugin_args (curplug, &my_tv[tv_header_size]);
1122 called_plugin = curplug;
1123 rv = (*onloadfn) (my_tv);
1124 called_plugin = NULL;
1126 einfo (_("%F%P: %s: plugin error: %d\n"), curplug->name, rv);
1127 curplug = curplug->next;
1130 /* Since plugin(s) inited ok, assume they're going to want symbol
1131 resolutions, which needs us to track which symbols are referenced
1132 by non-IR files using the linker's notice callback. */
1133 orig_notice_all = link_info.notice_all;
1134 orig_callbacks = link_info.callbacks;
1135 plugin_callbacks = *orig_callbacks;
1136 plugin_callbacks.notice = &plugin_notice;
1137 link_info.notice_all = TRUE;
1138 link_info.lto_plugin_active = TRUE;
1139 link_info.callbacks = &plugin_callbacks;
1141 register_ld_plugin_object_p (plugin_object_p);
1143 #if HAVE_MMAP && HAVE_GETPAGESIZE
1144 plugin_pagesize = getpagesize ();
1148 /* Call 'claim file' hook for all plugins. */
1150 plugin_call_claim_file (const struct ld_plugin_input_file *file, int *claimed)
1152 plugin_t *curplug = plugins_list;
1154 while (curplug && !*claimed)
1156 if (curplug->claim_file_handler)
1158 enum ld_plugin_status rv;
1160 called_plugin = curplug;
1161 rv = (*curplug->claim_file_handler) (file, claimed);
1162 called_plugin = NULL;
1164 set_plugin_error (curplug->name);
1166 curplug = curplug->next;
1168 return plugin_error_p () ? -1 : 0;
1171 /* Duplicates a character string with memory attached to ABFD. */
1174 plugin_strdup (bfd *abfd, const char *str)
1178 strlength = strlen (str) + 1;
1179 copy = bfd_alloc (abfd, strlength);
1181 einfo (_("%F%P: plugin_strdup failed to allocate memory: %s\n"),
1183 memcpy (copy, str, strlength);
1187 static const bfd_target *
1188 plugin_object_p (bfd *ibfd)
1191 plugin_input_file_t *input;
1192 struct ld_plugin_input_file file;
1195 /* Don't try the dummy object file. */
1196 if ((ibfd->flags & BFD_PLUGIN) != 0)
1199 if (ibfd->plugin_format != bfd_plugin_unknown)
1201 if (ibfd->plugin_format == bfd_plugin_yes)
1202 return ibfd->plugin_dummy_bfd->xvec;
1207 /* We create a dummy BFD, initially empty, to house whatever symbols
1208 the plugin may want to add. */
1209 abfd = plugin_get_ir_dummy_bfd (ibfd->filename, ibfd);
1211 input = bfd_alloc (abfd, sizeof (*input));
1213 einfo (_("%F%P: plugin failed to allocate memory for input: %s\n"),
1216 if (!bfd_plugin_open_input (ibfd, &file))
1219 if (file.name == ibfd->filename)
1221 /* We must copy filename attached to ibfd if it is not an archive
1222 member since it may be freed by bfd_close below. */
1223 file.name = plugin_strdup (abfd, file.name);
1226 file.handle = input;
1228 input->view_buffer.addr = NULL;
1229 input->view_buffer.filesize = 0;
1230 input->view_buffer.offset = 0;
1231 input->fd = file.fd;
1232 input->use_mmap = FALSE;
1233 input->offset = file.offset;
1234 input->filesize = file.filesize;
1235 input->name = plugin_strdup (abfd, ibfd->filename);
1239 if (plugin_call_claim_file (&file, &claimed))
1240 einfo (_("%F%P: %s: plugin reported error claiming file\n"),
1241 plugin_error_plugin ());
1243 if (input->fd != -1 && !bfd_plugin_target_p (ibfd->xvec))
1245 /* FIXME: fd belongs to us, not the plugin. GCC plugin, which
1246 doesn't need fd after plugin_call_claim_file, doesn't use
1247 BFD plugin target vector. Since GCC plugin doesn't call
1248 release_input_file, we close it here. LLVM plugin, which
1249 needs fd after plugin_call_claim_file and calls
1250 release_input_file after it is done, uses BFD plugin target
1251 vector. This scheme doesn't work when a plugin needs fd and
1252 doesn't use BFD plugin target vector neither. */
1259 ibfd->plugin_format = bfd_plugin_yes;
1260 ibfd->plugin_dummy_bfd = abfd;
1261 bfd_make_readable (abfd);
1267 if (input->use_mmap)
1269 /* If plugin didn't claim the file, unmap the buffer. */
1270 char *addr = input->view_buffer.addr;
1271 off_t size = input->view_buffer.filesize;
1272 # if HAVE_GETPAGESIZE
1273 off_t bias = input->view_buffer.offset % plugin_pagesize;
1277 munmap (addr, size);
1281 /* If plugin didn't claim the file, we don't need the dummy bfd.
1282 Can't avoid speculatively creating it, alas. */
1283 ibfd->plugin_format = bfd_plugin_no;
1284 bfd_close_all_done (abfd);
1290 plugin_maybe_claim (lang_input_statement_type *entry)
1292 ASSERT (entry->header.type == lang_input_statement_enum);
1293 if (plugin_object_p (entry->the_bfd))
1295 bfd *abfd = entry->the_bfd->plugin_dummy_bfd;
1297 /* Discard the real file's BFD and substitute the dummy one. */
1299 /* We can't call bfd_close on archives. BFD archive handling
1300 caches elements, and add_archive_element keeps pointers to
1301 the_bfd and the_bfd->filename in a lang_input_statement_type
1302 linker script statement. */
1303 if (entry->the_bfd->my_archive == NULL)
1304 bfd_close (entry->the_bfd);
1305 entry->the_bfd = abfd;
1306 entry->flags.claimed = 1;
1310 /* Call 'all symbols read' hook for all plugins. */
1312 plugin_call_all_symbols_read (void)
1314 plugin_t *curplug = plugins_list;
1316 /* Disable any further file-claiming. */
1317 no_more_claiming = TRUE;
1321 if (curplug->all_symbols_read_handler)
1323 enum ld_plugin_status rv;
1324 called_plugin = curplug;
1325 rv = (*curplug->all_symbols_read_handler) ();
1326 called_plugin = NULL;
1328 set_plugin_error (curplug->name);
1330 curplug = curplug->next;
1332 return plugin_error_p () ? -1 : 0;
1335 /* Call 'cleanup' hook for all plugins at exit. */
1337 plugin_call_cleanup (void)
1339 plugin_t *curplug = plugins_list;
1342 if (curplug->cleanup_handler && !curplug->cleanup_done)
1344 enum ld_plugin_status rv;
1345 curplug->cleanup_done = TRUE;
1346 called_plugin = curplug;
1347 rv = (*curplug->cleanup_handler) ();
1348 called_plugin = NULL;
1350 info_msg (_("%P: %s: error in plugin cleanup: %d (ignored)\n"),
1352 dlclose (curplug->dlhandle);
1354 curplug = curplug->next;
1358 /* To determine which symbols should be resolved LDPR_PREVAILING_DEF
1359 and which LDPR_PREVAILING_DEF_IRONLY, we notice all the symbols as
1360 the linker adds them to the linker hash table. Mark those
1361 referenced from a non-IR file with non_ir_ref_regular or
1362 non_ir_ref_dynamic as appropriate. We have to notice_all symbols,
1363 because we won't necessarily know until later which ones will be
1364 contributed by IR files. */
1366 plugin_notice (struct bfd_link_info *info,
1367 struct bfd_link_hash_entry *h,
1368 struct bfd_link_hash_entry *inh,
1374 struct bfd_link_hash_entry *orig_h = h;
1379 bfd_boolean ref = FALSE;
1381 if (h->type == bfd_link_hash_warning)
1384 /* Nothing to do here if this def/ref is from an IR dummy BFD. */
1385 if (is_ir_dummy_bfd (abfd))
1388 /* Making an indirect symbol counts as a reference unless this
1389 is a brand new symbol. */
1390 else if (bfd_is_ind_section (section)
1391 || (flags & BSF_INDIRECT) != 0)
1393 /* ??? Some of this is questionable. See comments in
1394 _bfd_generic_link_add_one_symbol for case IND. */
1395 if (h->type != bfd_link_hash_new
1396 || inh->type == bfd_link_hash_new)
1398 if ((abfd->flags & DYNAMIC) == 0)
1399 inh->non_ir_ref_regular = TRUE;
1401 inh->non_ir_ref_dynamic = TRUE;
1404 if (h->type != bfd_link_hash_new)
1408 /* Nothing to do here for warning symbols. */
1409 else if ((flags & BSF_WARNING) != 0)
1412 /* Nothing to do here for constructor symbols. */
1413 else if ((flags & BSF_CONSTRUCTOR) != 0)
1416 /* If this is a ref, set non_ir_ref. */
1417 else if (bfd_is_und_section (section))
1419 /* Replace the undefined dummy bfd with the real one. */
1420 if ((h->type == bfd_link_hash_undefined
1421 || h->type == bfd_link_hash_undefweak)
1422 && (h->u.undef.abfd == NULL
1423 || (h->u.undef.abfd->flags & BFD_PLUGIN) != 0))
1424 h->u.undef.abfd = abfd;
1429 /* A common symbol should be merged with other commons or
1430 defs with the same name. In particular, a common ought
1431 to be overridden by a def in a -flto object. In that
1432 sense a common is also a ref. */
1433 else if (bfd_is_com_section (section))
1435 if (h->type == bfd_link_hash_common
1436 && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner))
1438 h->type = bfd_link_hash_undefweak;
1439 h->u.undef.abfd = sym_bfd;
1444 /* Otherwise, it must be a new def.
1445 Ensure any symbol defined in an IR dummy BFD takes on a
1446 new value from a real BFD. Weak symbols are not normally
1447 overridden by a new weak definition, and strong symbols
1448 will normally cause multiple definition errors. Avoid
1449 this by making the symbol appear to be undefined. */
1450 else if (((h->type == bfd_link_hash_defweak
1451 || h->type == bfd_link_hash_defined)
1452 && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
1453 || (h->type == bfd_link_hash_common
1454 && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner)))
1456 h->type = bfd_link_hash_undefweak;
1457 h->u.undef.abfd = sym_bfd;
1462 if ((abfd->flags & DYNAMIC) == 0)
1463 h->non_ir_ref_regular = TRUE;
1465 h->non_ir_ref_dynamic = TRUE;
1469 /* Continue with cref/nocrossref/trace-sym processing. */
1472 || (info->notice_hash != NULL
1473 && bfd_hash_lookup (info->notice_hash, orig_h->root.string,
1474 FALSE, FALSE) != NULL))
1475 return (*orig_callbacks->notice) (info, orig_h, inh,
1476 abfd, section, value, flags);