1 /* Plugin control for the GNU linker.
2 Copyright (C) 2010-2023 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"
24 #if BFD_SUPPORTS_PLUGINS
34 #include "plugin-api.h"
35 #include "../bfd/plugin.h"
39 # include <sys/mman.h>
41 # define MAP_FAILED ((void *) -1)
47 # define MAP_PRIVATE 0
51 #if !(defined(errno) || defined(_MSC_VER) && defined(_INC_ERRNO))
54 #if defined (HAVE_DLFCN_H)
56 #elif defined (HAVE_WINDOWS_H)
60 /* Report plugin symbols. */
61 bool report_plugin_symbols;
63 /* The suffix to append to the name of the real (claimed) object file
64 when generating a dummy BFD to hold the IR symbols sent from the
65 plugin. For cosmetic use only; appears in maps, crefs etc. */
66 #define IRONLY_SUFFIX " (symbol from plugin)"
68 /* Stores a single argument passed to a plugin. */
69 typedef struct plugin_arg
71 struct plugin_arg *next;
75 /* Holds all details of a single plugin. */
78 /* Next on the list of plugins, or NULL at end of chain. */
80 /* The argument string given to --plugin. */
82 /* The shared library handle returned by dlopen. */
84 /* The list of argument string given to --plugin-opt. */
86 /* Number of args in the list, for convenience. */
88 /* The plugin's event handlers. */
89 ld_plugin_claim_file_handler claim_file_handler;
90 ld_plugin_all_symbols_read_handler all_symbols_read_handler;
91 ld_plugin_cleanup_handler cleanup_handler;
92 /* TRUE if the cleanup handlers have been called. */
96 typedef struct view_buffer
103 /* The internal version of struct ld_plugin_input_file with a BFD
105 typedef struct plugin_input_file
109 /* The original input BFD. Non-NULL if it is an archive member. */
111 view_buffer_t view_buffer;
117 } plugin_input_file_t;
119 /* The master list of all plugins. */
120 static plugin_t *plugins_list = NULL;
122 /* We keep a tail pointer for easy linking on the end. */
123 static plugin_t **plugins_tail_chain_ptr = &plugins_list;
125 /* The last plugin added to the list, for receiving args. */
126 static plugin_t *last_plugin = NULL;
128 /* This is set when we ignored last plugin */
129 static bfd_boolean last_plugin_ignored;
131 /* The tail of the arg chain of the last plugin added to the list. */
132 static plugin_arg_t **last_plugin_args_tail_chain_ptr = NULL;
134 /* The plugin which is currently having a callback executed. */
135 static plugin_t *called_plugin = NULL;
137 /* Last plugin to cause an error, if any. */
138 static const char *error_plugin = NULL;
140 /* State of linker "notice" interface before we poked at it. */
141 static bool orig_notice_all;
143 /* Original linker callbacks, and the plugin version. */
144 static const struct bfd_link_callbacks *orig_callbacks;
145 static struct bfd_link_callbacks plugin_callbacks;
147 /* Set at all symbols read time, to avoid recursively offering the plugin
148 its own newly-added input files and libs to claim. */
149 bool no_more_claiming = false;
151 #if HAVE_MMAP && HAVE_GETPAGESIZE
152 /* Page size used by mmap. */
153 static off_t plugin_pagesize;
156 /* List of tags to set in the constant leading part of the tv array. */
157 static const enum ld_plugin_tag tv_header_tags[] =
164 LDPT_REGISTER_CLAIM_FILE_HOOK,
165 LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK,
166 LDPT_REGISTER_CLEANUP_HOOK,
170 LDPT_RELEASE_INPUT_FILE,
174 LDPT_ADD_INPUT_LIBRARY,
175 LDPT_SET_EXTRA_LIBRARY_PATH
178 /* How many entries in the constant leading part of the tv array. */
179 static const size_t tv_header_size = ARRAY_SIZE (tv_header_tags);
181 /* Forward references. */
182 static bool plugin_notice (struct bfd_link_info *,
183 struct bfd_link_hash_entry *,
184 struct bfd_link_hash_entry *,
185 bfd *, asection *, bfd_vma, flagword);
187 static bfd_cleanup plugin_object_p (bfd *);
189 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
191 #define RTLD_NOW 0 /* Dummy value. */
194 dlopen (const char *file, int mode ATTRIBUTE_UNUSED)
196 return LoadLibrary (file);
200 dlsym (void *handle, const char *name)
202 return GetProcAddress (handle, name);
206 dlclose (void *handle)
208 FreeLibrary (handle);
212 #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */
222 /* Helper function for exiting with error status. */
224 set_plugin_error (const char *plugin)
226 error_plugin = plugin;
230 /* Test if an error occurred. */
232 plugin_error_p (void)
234 return error_plugin != NULL;
237 /* Return name of plugin which caused an error if any. */
239 plugin_error_plugin (void)
241 return error_plugin ? error_plugin : _("<no plugin>");
244 /* Handle -plugin arg: find and load plugin, or return error. */
246 plugin_opt_plugin (const char *plugin)
249 plugin_t *curplug = plugins_list;
251 newplug = xmalloc (sizeof *newplug);
252 memset (newplug, 0, sizeof *newplug);
253 newplug->name = plugin;
254 newplug->dlhandle = dlopen (plugin, RTLD_NOW);
255 if (!newplug->dlhandle) {
256 #ifdef IGNORE_BROKEN_PLUGINS
257 einfo (_("%P: %s: error loading plugin: %s\n"), plugin, dlerror ());
258 last_plugin_ignored=TRUE;
262 einfo (_("%P%F: %s: error loading plugin: %s\n"), plugin, dlerror ());
266 /* Check if plugin has been loaded already. */
269 if (newplug->dlhandle == curplug->dlhandle)
271 einfo (_("%P: %s: duplicated plugin\n"), plugin);
275 curplug = curplug->next;
278 /* Chain on end, so when we run list it is in command-line order. */
279 *plugins_tail_chain_ptr = newplug;
280 plugins_tail_chain_ptr = &newplug->next;
282 /* Record it as current plugin for receiving args. */
283 last_plugin = newplug;
284 last_plugin_args_tail_chain_ptr = &newplug->args;
285 #ifdef IGNORE_BROKEN_PLUGINS
286 last_plugin_ignored=FALSE;
290 /* Accumulate option arguments for last-loaded plugin, or return
293 plugin_opt_plugin_arg (const char *arg)
295 #ifdef IGNORE_BROKEN_PLUGINS
296 /* Ignore options for ignored plugin */
297 if (last_plugin_ignored)
300 plugin_arg_t *newarg;
303 return set_plugin_error (_("<no plugin>"));
305 /* Ignore -pass-through= from GCC driver. */
308 const char *p = arg + 1;
312 if (strncmp (p, "pass-through=", 13) == 0)
316 newarg = xmalloc (sizeof *newarg);
320 /* Chain on end to preserve command-line order. */
321 *last_plugin_args_tail_chain_ptr = newarg;
322 last_plugin_args_tail_chain_ptr = &newarg->next;
323 last_plugin->n_args++;
327 /* Generate a dummy BFD to represent an IR file, for any callers of
328 plugin_call_claim_file to use as the handle in the ld_plugin_input_file
329 struct that they build to pass in. The BFD is initially writable, so
330 that symbols can be added to it; it must be made readable after the
331 add_symbols hook has been called so that it can be read when linking. */
333 plugin_get_ir_dummy_bfd (const char *name, bfd *srctemplate)
336 bool bfd_plugin_target;
338 bfd_use_reserved_id = 1;
339 bfd_plugin_target = bfd_plugin_target_p (srctemplate->xvec);
340 abfd = bfd_create (concat (name, IRONLY_SUFFIX, (const char *) NULL),
341 bfd_plugin_target ? link_info.output_bfd : srctemplate);
344 abfd->flags |= BFD_LINKER_CREATED | BFD_PLUGIN;
345 if (!bfd_make_writable (abfd))
347 if (!bfd_plugin_target)
349 bfd_set_arch_info (abfd, bfd_get_arch_info (srctemplate));
350 bfd_set_gp_size (abfd, bfd_get_gp_size (srctemplate));
351 if (!bfd_copy_private_bfd_data (srctemplate, abfd))
357 /* Create section to own the symbols. */
358 flags = (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY
359 | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_EXCLUDE);
360 if (bfd_make_section_anyway_with_flags (abfd, ".text", flags))
365 einfo (_("%F%P: could not create dummy IR bfd: %E\n"));
369 /* Check if the BFD passed in is an IR dummy object file. */
371 is_ir_dummy_bfd (const bfd *abfd)
373 /* ABFD can sometimes legitimately be NULL, e.g. when called from one
374 of the linker callbacks for a symbol in the *ABS* or *UND* sections. */
375 return abfd != NULL && (abfd->flags & BFD_PLUGIN) != 0;
378 /* Helpers to convert between BFD and GOLD symbol formats. */
379 static enum ld_plugin_status
380 asymbol_from_plugin_symbol (bfd *abfd, asymbol *asym,
381 const struct ld_plugin_symbol *ldsym)
383 flagword flags = BSF_NO_FLAGS;
384 struct bfd_section *section;
386 asym->the_bfd = abfd;
387 asym->name = (ldsym->version
388 ? concat (ldsym->name, "@", ldsym->version, (const char *) NULL)
398 if (ldsym->comdat_key)
400 char *name = concat (".gnu.linkonce.t.", ldsym->comdat_key,
401 (const char *) NULL);
402 section = bfd_get_section_by_name (abfd, name);
409 sflags = (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY
410 | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_EXCLUDE
411 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD);
412 section = bfd_make_section_anyway_with_flags (abfd, name, sflags);
418 section = bfd_get_section_by_name (abfd, ".text");
425 section = bfd_und_section_ptr;
430 section = bfd_com_section_ptr;
431 asym->value = ldsym->size;
438 asym->section = section;
440 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
442 elf_symbol_type *elfsym = elf_symbol_from (asym);
443 unsigned char visibility;
446 einfo (_("%F%P: %s: non-ELF symbol in ELF BFD!\n"), asym->name);
448 if (ldsym->def == LDPK_COMMON)
450 elfsym->internal_elf_sym.st_shndx = SHN_COMMON;
451 elfsym->internal_elf_sym.st_value = 1;
454 switch (ldsym->visibility)
457 einfo (_("%F%P: unknown ELF symbol visibility: %d!\n"),
462 visibility = STV_DEFAULT;
465 visibility = STV_PROTECTED;
468 visibility = STV_INTERNAL;
471 visibility = STV_HIDDEN;
474 elfsym->internal_elf_sym.st_other |= visibility;
480 /* Register a claim-file handler. */
481 static enum ld_plugin_status
482 register_claim_file (ld_plugin_claim_file_handler handler)
484 ASSERT (called_plugin);
485 called_plugin->claim_file_handler = handler;
489 /* Register an all-symbols-read handler. */
490 static enum ld_plugin_status
491 register_all_symbols_read (ld_plugin_all_symbols_read_handler handler)
493 ASSERT (called_plugin);
494 called_plugin->all_symbols_read_handler = handler;
498 /* Register a cleanup handler. */
499 static enum ld_plugin_status
500 register_cleanup (ld_plugin_cleanup_handler handler)
502 ASSERT (called_plugin);
503 called_plugin->cleanup_handler = handler;
507 /* Add symbols from a plugin-claimed input file. */
508 static enum ld_plugin_status
509 add_symbols (void *handle, int nsyms, const struct ld_plugin_symbol *syms)
512 plugin_input_file_t *input = handle;
513 bfd *abfd = input->abfd;
516 ASSERT (called_plugin);
517 symptrs = xmalloc (nsyms * sizeof *symptrs);
518 for (n = 0; n < nsyms; n++)
520 enum ld_plugin_status rv;
523 bfdsym = bfd_make_empty_symbol (abfd);
525 rv = asymbol_from_plugin_symbol (abfd, bfdsym, syms + n);
529 bfd_set_symtab (abfd, symptrs, nsyms);
533 /* Get the input file information with an open (possibly re-opened)
535 static enum ld_plugin_status
536 get_input_file (const void *handle, struct ld_plugin_input_file *file)
538 const plugin_input_file_t *input = handle;
540 ASSERT (called_plugin);
542 file->name = input->name;
543 file->offset = input->offset;
544 file->filesize = input->filesize;
545 file->handle = (void *) handle;
550 /* Get view of the input file. */
551 static enum ld_plugin_status
552 get_view (const void *handle, const void **viewp)
554 plugin_input_file_t *input = (plugin_input_file_t *) handle;
556 size_t size = input->filesize;
557 off_t offset = input->offset;
558 #if HAVE_MMAP && HAVE_GETPAGESIZE
562 ASSERT (called_plugin);
564 /* FIXME: einfo should support %lld. */
565 if ((off_t) size != input->filesize)
566 einfo (_("%F%P: unsupported input file size: %s (%ld bytes)\n"),
567 input->name, (long) input->filesize);
569 /* Check the cached view buffer. */
570 if (input->view_buffer.addr != NULL
571 && input->view_buffer.filesize == size
572 && input->view_buffer.offset == offset)
574 *viewp = input->view_buffer.addr;
578 input->view_buffer.filesize = size;
579 input->view_buffer.offset = offset;
582 # if HAVE_GETPAGESIZE
583 bias = offset % plugin_pagesize;
587 buffer = mmap (NULL, size, PROT_READ, MAP_PRIVATE, input->fd, offset);
588 if (buffer != MAP_FAILED)
590 input->use_mmap = true;
591 # if HAVE_GETPAGESIZE
600 input->use_mmap = false;
602 if (lseek (input->fd, offset, SEEK_SET) < 0)
605 buffer = bfd_alloc (input->abfd, size);
612 ssize_t got = read (input->fd, p, size);
620 else if (errno != EINTR)
626 input->view_buffer.addr = buffer;
632 /* Release plugin file descriptor. */
635 release_plugin_file_descriptor (plugin_input_file_t *input)
639 bfd_plugin_close_file_descriptor (input->ibfd, input->fd);
644 /* Release the input file. */
645 static enum ld_plugin_status
646 release_input_file (const void *handle)
648 plugin_input_file_t *input = (plugin_input_file_t *) handle;
649 ASSERT (called_plugin);
650 release_plugin_file_descriptor (input);
654 /* Return TRUE if a defined symbol might be reachable from outside the
655 universe of claimed objects. */
657 is_visible_from_outside (struct ld_plugin_symbol *lsym,
658 struct bfd_link_hash_entry *blhe)
660 if (bfd_link_relocatable (&link_info))
662 if (blhe->non_ir_ref_dynamic
663 || link_info.export_dynamic
664 || bfd_link_dll (&link_info))
666 /* Check if symbol is hidden by version script. */
667 if (bfd_hide_sym_by_version (link_info.version_info,
670 /* Only ELF symbols really have visibility. */
671 if (is_elf_hash_table (link_info.hash))
673 struct elf_link_hash_entry *el = (struct elf_link_hash_entry *)blhe;
674 int vis = ELF_ST_VISIBILITY (el->other);
675 return vis == STV_DEFAULT || vis == STV_PROTECTED;
677 /* On non-ELF targets, we can safely make inferences by considering
678 what visibility the plugin would have liked to apply when it first
679 sent us the symbol. During ELF symbol processing, visibility only
680 ever becomes more restrictive, not less, when symbols are merged,
681 so this is a conservative estimate; it may give false positives,
682 declaring something visible from outside when it in fact would
683 not have been, but this will only lead to missed optimisation
684 opportunities during LTRANS at worst; it will not give false
685 negatives, which can lead to the disastrous conclusion that the
686 related symbol is IRONLY. (See GCC PR46319 for an example.) */
687 return (lsym->visibility == LDPV_DEFAULT
688 || lsym->visibility == LDPV_PROTECTED);
694 /* Return LTO kind string name that corresponds to IDX enum value. */
696 get_lto_kind (unsigned int idx)
698 static char buffer[64];
699 const char *lto_kind_str[5] =
708 if (idx < ARRAY_SIZE (lto_kind_str))
709 return lto_kind_str [idx];
711 sprintf (buffer, _("unknown LTO kind value %x"), idx);
715 /* Return LTO resolution string name that corresponds to IDX enum value. */
717 get_lto_resolution (unsigned int idx)
719 static char buffer[64];
720 static const char *lto_resolution_str[10] =
725 "PREVAILING_DEF_IRONLY",
731 "PREVAILING_DEF_IRONLY_EXP",
734 if (idx < ARRAY_SIZE (lto_resolution_str))
735 return lto_resolution_str [idx];
737 sprintf (buffer, _("unknown LTO resolution value %x"), idx);
741 /* Return LTO visibility string name that corresponds to IDX enum value. */
743 get_lto_visibility (unsigned int idx)
745 static char buffer[64];
746 const char *lto_visibility_str[4] =
754 if (idx < ARRAY_SIZE (lto_visibility_str))
755 return lto_visibility_str [idx];
757 sprintf (buffer, _("unknown LTO visibility value %x"), idx);
761 /* Get the symbol resolution info for a plugin-claimed input file. */
762 static enum ld_plugin_status
763 get_symbols (const void *handle, int nsyms, struct ld_plugin_symbol *syms,
766 const plugin_input_file_t *input = handle;
767 const bfd *abfd = (const bfd *) input->abfd;
770 ASSERT (called_plugin);
771 for (n = 0; n < nsyms; n++)
773 struct bfd_link_hash_entry *blhe;
776 struct bfd_link_hash_entry *h
777 = bfd_link_hash_lookup (link_info.hash, syms[n].name,
779 enum { wrap_none, wrapper, wrapped } wrap_status = wrap_none;
781 if (syms[n].def != LDPK_UNDEF && syms[n].def != LDPK_WEAKUNDEF)
784 if (blhe && link_info.wrap_hash != NULL)
786 /* Check if a symbol is a wrapper symbol. */
787 struct bfd_link_hash_entry *unwrap
788 = unwrap_hash_lookup (&link_info, (bfd *) abfd, blhe);
789 if (unwrap && unwrap != h)
790 wrap_status = wrapper;
795 blhe = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
796 &link_info, syms[n].name,
798 /* Check if a symbol is a wrapped symbol. */
799 if (blhe && blhe != h)
800 wrap_status = wrapped;
804 /* The plugin is called to claim symbols in an archive element
805 from plugin_object_p. But those symbols aren't needed to
806 create output. They are defined and referenced only within
819 res = LDPR_PREVAILING_DEF_IRONLY;
825 /* Determine resolution from blhe type and symbol's original type. */
826 if (blhe->type == bfd_link_hash_undefined
827 || blhe->type == bfd_link_hash_undefweak)
832 if (blhe->type != bfd_link_hash_defined
833 && blhe->type != bfd_link_hash_defweak
834 && blhe->type != bfd_link_hash_common)
836 /* We should not have a new, indirect or warning symbol here. */
837 einfo (_("%F%P: %s: plugin symbol table corrupt (sym type %d)\n"),
838 called_plugin->name, blhe->type);
841 /* Find out which section owns the symbol. Since it's not undef,
842 it must have an owner; if it's not a common symbol, both defs
843 and weakdefs keep it in the same place. */
844 owner_sec = (blhe->type == bfd_link_hash_common
845 ? blhe->u.c.p->section
846 : blhe->u.def.section);
849 /* If it was originally undefined or common, then it has been
850 resolved; determine how. */
851 if (syms[n].def == LDPK_UNDEF
852 || syms[n].def == LDPK_WEAKUNDEF
853 || syms[n].def == LDPK_COMMON)
855 if (owner_sec->owner == link_info.output_bfd)
856 res = LDPR_RESOLVED_EXEC;
857 else if (owner_sec->owner == abfd)
858 res = LDPR_PREVAILING_DEF_IRONLY;
859 else if (is_ir_dummy_bfd (owner_sec->owner))
860 res = LDPR_RESOLVED_IR;
861 else if (owner_sec->owner != NULL
862 && (owner_sec->owner->flags & DYNAMIC) != 0)
863 res = LDPR_RESOLVED_DYN;
865 res = LDPR_RESOLVED_EXEC;
868 /* Was originally def, or weakdef. Does it prevail? If the
869 owner is the original dummy bfd that supplied it, then this
870 is the definition that has prevailed. */
871 else if (owner_sec->owner == link_info.output_bfd)
872 res = LDPR_PREEMPTED_REG;
873 else if (owner_sec->owner == abfd)
874 res = LDPR_PREVAILING_DEF_IRONLY;
876 /* Was originally def, weakdef, or common, but has been pre-empted. */
877 else if (is_ir_dummy_bfd (owner_sec->owner))
878 res = LDPR_PREEMPTED_IR;
880 res = LDPR_PREEMPTED_REG;
882 if (res == LDPR_PREVAILING_DEF_IRONLY)
884 /* We need to know if the sym is referenced from non-IR files. Or
885 even potentially-referenced, perhaps in a future final link if
886 this is a partial one, perhaps dynamically at load-time if the
887 symbol is externally visible. Also check for __real_SYM
888 reference and wrapper symbol. */
889 if (blhe->non_ir_ref_regular
891 || wrap_status == wrapper)
892 res = LDPR_PREVAILING_DEF;
893 else if (wrap_status == wrapped)
894 res = LDPR_RESOLVED_IR;
895 else if (is_visible_from_outside (&syms[n], blhe))
896 res = def_ironly_exp;
900 syms[n].resolution = res;
901 if (report_plugin_symbols)
902 einfo (_("%P: %pB: symbol `%s' "
903 "definition: %s, visibility: %s, resolution: %s\n"),
905 get_lto_kind (syms[n].def),
906 get_lto_visibility (syms[n].visibility),
907 get_lto_resolution (res));
912 static enum ld_plugin_status
913 get_symbols_v1 (const void *handle, int nsyms, struct ld_plugin_symbol *syms)
915 return get_symbols (handle, nsyms, syms, LDPR_PREVAILING_DEF);
918 static enum ld_plugin_status
919 get_symbols_v2 (const void *handle, int nsyms, struct ld_plugin_symbol *syms)
921 return get_symbols (handle, nsyms, syms, LDPR_PREVAILING_DEF_IRONLY_EXP);
924 /* Add a new (real) input file generated by a plugin. */
925 static enum ld_plugin_status
926 add_input_file (const char *pathname)
928 lang_input_statement_type *is;
930 ASSERT (called_plugin);
931 is = lang_add_input_file (xstrdup (pathname), lang_input_file_is_file_enum,
935 is->flags.lto_output = 1;
939 /* Add a new (real) library required by a plugin. */
940 static enum ld_plugin_status
941 add_input_library (const char *pathname)
943 lang_input_statement_type *is;
945 ASSERT (called_plugin);
946 is = lang_add_input_file (xstrdup (pathname), lang_input_file_is_l_enum,
950 is->flags.lto_output = 1;
954 /* Set the extra library path to be used by libraries added via
955 add_input_library. */
956 static enum ld_plugin_status
957 set_extra_library_path (const char *path)
959 ASSERT (called_plugin);
960 ldfile_add_library_path (xstrdup (path), false);
964 /* Issue a diagnostic message from a plugin. */
965 static enum ld_plugin_status
966 message (int level, const char *format, ...)
969 va_start (args, format);
974 vfinfo (stdout, format, args, false);
979 char *newfmt = concat (_("%P: warning: "), format, "\n",
980 (const char *) NULL);
981 vfinfo (stdout, newfmt, args, true);
989 char *newfmt = concat (level == LDPL_FATAL ? "%F" : "%X",
990 _("%P: error: "), format, "\n",
991 (const char *) NULL);
993 vfinfo (stderr, newfmt, args, true);
1004 /* Helper to size leading part of tv array and set it up. */
1006 set_tv_header (struct ld_plugin_tv *tv)
1011 static const unsigned int major = (unsigned)(BFD_VERSION / 100000000UL);
1012 static const unsigned int minor = (unsigned)(BFD_VERSION / 1000000UL) % 100;
1014 for (i = 0; i < tv_header_size; i++)
1016 tv[i].tv_tag = tv_header_tags[i];
1017 #define TVU(x) tv[i].tv_u.tv_ ## x
1018 switch (tv[i].tv_tag)
1021 TVU(message) = message;
1023 case LDPT_API_VERSION:
1024 TVU(val) = LD_PLUGIN_API_VERSION;
1026 case LDPT_GNU_LD_VERSION:
1027 TVU(val) = major * 100 + minor;
1029 case LDPT_LINKER_OUTPUT:
1030 TVU(val) = (bfd_link_relocatable (&link_info) ? LDPO_REL
1031 : bfd_link_pde (&link_info) ? LDPO_EXEC
1032 : bfd_link_pie (&link_info) ? LDPO_PIE
1035 case LDPT_OUTPUT_NAME:
1036 TVU(string) = output_filename;
1038 case LDPT_REGISTER_CLAIM_FILE_HOOK:
1039 TVU(register_claim_file) = register_claim_file;
1041 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
1042 TVU(register_all_symbols_read) = register_all_symbols_read;
1044 case LDPT_REGISTER_CLEANUP_HOOK:
1045 TVU(register_cleanup) = register_cleanup;
1047 case LDPT_ADD_SYMBOLS:
1048 TVU(add_symbols) = add_symbols;
1050 case LDPT_GET_INPUT_FILE:
1051 TVU(get_input_file) = get_input_file;
1054 TVU(get_view) = get_view;
1056 case LDPT_RELEASE_INPUT_FILE:
1057 TVU(release_input_file) = release_input_file;
1059 case LDPT_GET_SYMBOLS:
1060 TVU(get_symbols) = get_symbols_v1;
1062 case LDPT_GET_SYMBOLS_V2:
1063 TVU(get_symbols) = get_symbols_v2;
1065 case LDPT_ADD_INPUT_FILE:
1066 TVU(add_input_file) = add_input_file;
1068 case LDPT_ADD_INPUT_LIBRARY:
1069 TVU(add_input_library) = add_input_library;
1071 case LDPT_SET_EXTRA_LIBRARY_PATH:
1072 TVU(set_extra_library_path) = set_extra_library_path;
1075 /* Added a new entry to the array without adding
1076 a new case to set up its value is a bug. */
1083 /* Append the per-plugin args list and trailing LDPT_NULL to tv. */
1085 set_tv_plugin_args (plugin_t *plugin, struct ld_plugin_tv *tv)
1087 plugin_arg_t *arg = plugin->args;
1090 tv->tv_tag = LDPT_OPTION;
1091 tv->tv_u.tv_string = arg->arg;
1095 tv->tv_tag = LDPT_NULL;
1096 tv->tv_u.tv_val = 0;
1099 /* Load up and initialise all plugins after argument parsing. */
1101 plugin_load_plugins (void)
1103 struct ld_plugin_tv *my_tv;
1104 unsigned int max_args = 0;
1105 plugin_t *curplug = plugins_list;
1107 /* If there are no plugins, we need do nothing this run. */
1111 /* First pass over plugins to find max # args needed so that we
1112 can size and allocate the tv array. */
1115 if (curplug->n_args > max_args)
1116 max_args = curplug->n_args;
1117 curplug = curplug->next;
1120 /* Allocate tv array and initialise constant part. */
1121 my_tv = xmalloc ((max_args + 1 + tv_header_size) * sizeof *my_tv);
1122 set_tv_header (my_tv);
1124 /* Pass over plugins again, activating them. */
1125 curplug = plugins_list;
1128 enum ld_plugin_status rv;
1129 ld_plugin_onload onloadfn;
1131 onloadfn = (ld_plugin_onload) dlsym (curplug->dlhandle, "onload");
1133 onloadfn = (ld_plugin_onload) dlsym (curplug->dlhandle, "_onload");
1135 einfo (_("%F%P: %s: error loading plugin: %s\n"),
1136 curplug->name, dlerror ());
1137 set_tv_plugin_args (curplug, &my_tv[tv_header_size]);
1138 called_plugin = curplug;
1139 rv = (*onloadfn) (my_tv);
1140 called_plugin = NULL;
1142 einfo (_("%F%P: %s: plugin error: %d\n"), curplug->name, rv);
1143 curplug = curplug->next;
1146 /* Since plugin(s) inited ok, assume they're going to want symbol
1147 resolutions, which needs us to track which symbols are referenced
1148 by non-IR files using the linker's notice callback. */
1149 orig_notice_all = link_info.notice_all;
1150 orig_callbacks = link_info.callbacks;
1151 plugin_callbacks = *orig_callbacks;
1152 plugin_callbacks.notice = &plugin_notice;
1153 link_info.notice_all = true;
1154 link_info.lto_plugin_active = true;
1155 link_info.callbacks = &plugin_callbacks;
1157 register_ld_plugin_object_p (plugin_object_p);
1159 #if HAVE_MMAP && HAVE_GETPAGESIZE
1160 plugin_pagesize = getpagesize ();
1164 /* Call 'claim file' hook for all plugins. */
1166 plugin_call_claim_file (const struct ld_plugin_input_file *file, int *claimed)
1168 plugin_t *curplug = plugins_list;
1170 while (curplug && !*claimed)
1172 if (curplug->claim_file_handler)
1174 enum ld_plugin_status rv;
1176 called_plugin = curplug;
1177 rv = (*curplug->claim_file_handler) (file, claimed);
1178 called_plugin = NULL;
1180 set_plugin_error (curplug->name);
1182 curplug = curplug->next;
1184 return plugin_error_p () ? -1 : 0;
1187 /* Duplicates a character string with memory attached to ABFD. */
1190 plugin_strdup (bfd *abfd, const char *str)
1194 strlength = strlen (str) + 1;
1195 copy = bfd_alloc (abfd, strlength);
1197 einfo (_("%F%P: plugin_strdup failed to allocate memory: %s\n"),
1199 memcpy (copy, str, strlength);
1204 plugin_cleanup (bfd *abfd ATTRIBUTE_UNUSED)
1209 plugin_object_p (bfd *ibfd)
1212 plugin_input_file_t *input;
1213 struct ld_plugin_input_file file;
1216 /* Don't try the dummy object file. */
1217 if ((ibfd->flags & BFD_PLUGIN) != 0)
1220 if (ibfd->plugin_format != bfd_plugin_unknown)
1222 if (ibfd->plugin_format == bfd_plugin_yes)
1223 return plugin_cleanup;
1228 /* We create a dummy BFD, initially empty, to house whatever symbols
1229 the plugin may want to add. */
1230 abfd = plugin_get_ir_dummy_bfd (bfd_get_filename (ibfd), ibfd);
1232 input = bfd_alloc (abfd, sizeof (*input));
1234 einfo (_("%F%P: plugin failed to allocate memory for input: %s\n"),
1237 if (!bfd_plugin_open_input (ibfd, &file))
1240 if (file.name == bfd_get_filename (ibfd))
1242 /* We must copy filename attached to ibfd if it is not an archive
1243 member since it may be freed by bfd_close below. */
1244 file.name = plugin_strdup (abfd, file.name);
1247 file.handle = input;
1249 input->ibfd = ibfd->my_archive != NULL ? ibfd : NULL;
1250 input->view_buffer.addr = NULL;
1251 input->view_buffer.filesize = 0;
1252 input->view_buffer.offset = 0;
1253 input->fd = file.fd;
1254 input->use_mmap = false;
1255 input->offset = file.offset;
1256 input->filesize = file.filesize;
1257 input->name = plugin_strdup (abfd, bfd_get_filename (ibfd));
1261 if (plugin_call_claim_file (&file, &claimed))
1262 einfo (_("%F%P: %s: plugin reported error claiming file\n"),
1263 plugin_error_plugin ());
1266 && (!claimed || !bfd_plugin_target_p (ibfd->xvec)))
1268 /* FIXME: fd belongs to us, not the plugin. GCC plugin, which
1269 doesn't need fd after plugin_call_claim_file, doesn't use
1270 BFD plugin target vector. Since GCC plugin doesn't call
1271 release_input_file, we close it here. LLVM plugin, which
1272 needs fd after plugin_call_claim_file and calls
1273 release_input_file after it is done, uses BFD plugin target
1274 vector. This scheme doesn't work when a plugin needs fd and
1275 doesn't use BFD plugin target vector neither. */
1276 release_plugin_file_descriptor (input);
1281 ibfd->plugin_format = bfd_plugin_yes;
1282 ibfd->plugin_dummy_bfd = abfd;
1283 bfd_make_readable (abfd);
1284 abfd->no_export = ibfd->no_export;
1285 return plugin_cleanup;
1290 if (input->use_mmap)
1292 /* If plugin didn't claim the file, unmap the buffer. */
1293 char *addr = input->view_buffer.addr;
1294 off_t size = input->view_buffer.filesize;
1295 # if HAVE_GETPAGESIZE
1296 off_t bias = input->view_buffer.offset % plugin_pagesize;
1300 munmap (addr, size);
1304 /* If plugin didn't claim the file, we don't need the dummy bfd.
1305 Can't avoid speculatively creating it, alas. */
1306 ibfd->plugin_format = bfd_plugin_no;
1307 bfd_close_all_done (abfd);
1313 plugin_maybe_claim (lang_input_statement_type *entry)
1315 ASSERT (entry->header.type == lang_input_statement_enum);
1316 if (plugin_object_p (entry->the_bfd))
1318 bfd *abfd = entry->the_bfd->plugin_dummy_bfd;
1320 /* Discard the real file's BFD and substitute the dummy one. */
1322 /* We can't call bfd_close on archives. BFD archive handling
1323 caches elements, and add_archive_element keeps pointers to
1324 the_bfd and the_bfd->filename in a lang_input_statement_type
1325 linker script statement. */
1326 if (entry->the_bfd->my_archive == NULL)
1327 bfd_close (entry->the_bfd);
1328 entry->the_bfd = abfd;
1329 entry->flags.claimed = 1;
1333 /* Call 'all symbols read' hook for all plugins. */
1335 plugin_call_all_symbols_read (void)
1337 plugin_t *curplug = plugins_list;
1339 /* Disable any further file-claiming. */
1340 no_more_claiming = true;
1344 if (curplug->all_symbols_read_handler)
1346 enum ld_plugin_status rv;
1347 called_plugin = curplug;
1348 rv = (*curplug->all_symbols_read_handler) ();
1349 called_plugin = NULL;
1351 set_plugin_error (curplug->name);
1353 curplug = curplug->next;
1355 return plugin_error_p () ? -1 : 0;
1358 /* Call 'cleanup' hook for all plugins at exit. */
1360 plugin_call_cleanup (void)
1362 plugin_t *curplug = plugins_list;
1365 if (curplug->cleanup_handler && !curplug->cleanup_done)
1367 enum ld_plugin_status rv;
1368 curplug->cleanup_done = true;
1369 called_plugin = curplug;
1370 rv = (*curplug->cleanup_handler) ();
1371 called_plugin = NULL;
1373 info_msg (_("%P: %s: error in plugin cleanup: %d (ignored)\n"),
1375 dlclose (curplug->dlhandle);
1377 curplug = curplug->next;
1381 /* To determine which symbols should be resolved LDPR_PREVAILING_DEF
1382 and which LDPR_PREVAILING_DEF_IRONLY, we notice all the symbols as
1383 the linker adds them to the linker hash table. Mark those
1384 referenced from a non-IR file with non_ir_ref_regular or
1385 non_ir_ref_dynamic as appropriate. We have to notice_all symbols,
1386 because we won't necessarily know until later which ones will be
1387 contributed by IR files. */
1389 plugin_notice (struct bfd_link_info *info,
1390 struct bfd_link_hash_entry *h,
1391 struct bfd_link_hash_entry *inh,
1397 struct bfd_link_hash_entry *orig_h = h;
1404 if (h->type == bfd_link_hash_warning)
1407 /* Nothing to do here if this def/ref is from an IR dummy BFD. */
1408 if (is_ir_dummy_bfd (abfd))
1411 /* Making an indirect symbol counts as a reference unless this
1412 is a brand new symbol. */
1413 else if (bfd_is_ind_section (section)
1414 || (flags & BSF_INDIRECT) != 0)
1416 /* ??? Some of this is questionable. See comments in
1417 _bfd_generic_link_add_one_symbol for case IND. */
1418 if (h->type != bfd_link_hash_new
1419 || inh->type == bfd_link_hash_new)
1421 if ((abfd->flags & DYNAMIC) == 0)
1422 inh->non_ir_ref_regular = true;
1424 inh->non_ir_ref_dynamic = true;
1427 if (h->type != bfd_link_hash_new)
1431 /* Nothing to do here for warning symbols. */
1432 else if ((flags & BSF_WARNING) != 0)
1435 /* Nothing to do here for constructor symbols. */
1436 else if ((flags & BSF_CONSTRUCTOR) != 0)
1439 /* If this is a ref, set non_ir_ref. */
1440 else if (bfd_is_und_section (section))
1442 /* Replace the undefined dummy bfd with the real one. */
1443 if ((h->type == bfd_link_hash_undefined
1444 || h->type == bfd_link_hash_undefweak)
1445 && (h->u.undef.abfd == NULL
1446 || (h->u.undef.abfd->flags & BFD_PLUGIN) != 0))
1447 h->u.undef.abfd = abfd;
1452 /* A common symbol should be merged with other commons or
1453 defs with the same name. In particular, a common ought
1454 to be overridden by a def in a -flto object. In that
1455 sense a common is also a ref. */
1456 else if (bfd_is_com_section (section))
1458 if (h->type == bfd_link_hash_common
1459 && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner))
1461 h->type = bfd_link_hash_undefweak;
1462 h->u.undef.abfd = sym_bfd;
1467 /* Otherwise, it must be a new def.
1468 Ensure any symbol defined in an IR dummy BFD takes on a
1469 new value from a real BFD. Weak symbols are not normally
1470 overridden by a new weak definition, and strong symbols
1471 will normally cause multiple definition errors. Avoid
1472 this by making the symbol appear to be undefined.
1474 NB: We change the previous definition in the IR object to
1475 undefweak only after all LTO symbols have been read or for
1477 else if ((info->lto_all_symbols_read
1478 || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
1479 && (((h->type == bfd_link_hash_defweak
1480 || h->type == bfd_link_hash_defined)
1481 && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
1482 || (h->type == bfd_link_hash_common
1483 && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner))))
1485 h->type = bfd_link_hash_undefweak;
1486 h->u.undef.abfd = sym_bfd;
1491 if ((abfd->flags & DYNAMIC) == 0)
1492 h->non_ir_ref_regular = true;
1494 h->non_ir_ref_dynamic = true;
1498 /* Continue with cref/nocrossref/trace-sym processing. */
1501 || (info->notice_hash != NULL
1502 && bfd_hash_lookup (info->notice_hash, orig_h->root.string,
1503 false, false) != NULL))
1504 return (*orig_callbacks->notice) (info, orig_h, inh,
1505 abfd, section, value, flags);
1508 #endif /* BFD_SUPPORTS_PLUGINS */