Add translations for various sub-directories
[binutils-gdb.git] / ld / plugin.c
blob8aec84852c038178b789d4910179c2c365f037cd
1 /* Plugin control for the GNU linker.
2 Copyright (C) 2010-2025 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. */
21 #include "sysdep.h"
22 #include "libiberty.h"
23 #include "bfd.h"
24 #if BFD_SUPPORTS_PLUGINS
25 #include "bfdlink.h"
26 #include "bfdver.h"
27 #include "ctf-api.h"
28 #include "ld.h"
29 #include "ldmain.h"
30 #include "ldmisc.h"
31 #include "ldexp.h"
32 #include "ldlang.h"
33 #include "ldfile.h"
34 #include "plugin-api.h"
35 #include "../bfd/plugin.h"
36 #include "plugin.h"
37 #include "elf-bfd.h"
38 #if HAVE_MMAP
39 # include <sys/mman.h>
40 # ifndef MAP_FAILED
41 # define MAP_FAILED ((void *) -1)
42 # endif
43 # ifndef PROT_READ
44 # define PROT_READ 0
45 # endif
46 # ifndef MAP_PRIVATE
47 # define MAP_PRIVATE 0
48 # endif
49 #endif
50 #include <errno.h>
51 #if !(defined(errno) || defined(_MSC_VER) && defined(_INC_ERRNO))
52 extern int errno;
53 #endif
54 #if defined (HAVE_DLFCN_H)
55 #include <dlfcn.h>
56 #elif defined (HAVE_WINDOWS_H)
57 #include <windows.h>
58 #endif
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;
72 const char *arg;
73 } plugin_arg_t;
75 /* Holds all details of a single plugin. */
76 typedef struct plugin
78 /* Next on the list of plugins, or NULL at end of chain. */
79 struct plugin *next;
80 /* The argument string given to --plugin. */
81 const char *name;
82 /* The shared library handle returned by dlopen. */
83 void *dlhandle;
84 /* The list of argument string given to --plugin-opt. */
85 plugin_arg_t *args;
86 /* Number of args in the list, for convenience. */
87 size_t n_args;
88 /* The plugin's event handlers. */
89 ld_plugin_claim_file_handler claim_file_handler;
90 ld_plugin_claim_file_handler_v2 claim_file_handler_v2;
91 ld_plugin_all_symbols_read_handler all_symbols_read_handler;
92 ld_plugin_cleanup_handler cleanup_handler;
93 /* TRUE if the cleanup handlers have been called. */
94 bool cleanup_done;
95 } plugin_t;
97 typedef struct view_buffer
99 char *addr;
100 size_t filesize;
101 off_t offset;
102 } view_buffer_t;
104 /* The internal version of struct ld_plugin_input_file with a BFD
105 pointer. */
106 typedef struct plugin_input_file
108 /* The dummy BFD. */
109 bfd *abfd;
110 /* The original input BFD. Non-NULL if it is an archive member. */
111 bfd *ibfd;
112 view_buffer_t view_buffer;
113 char *name;
114 int fd;
115 bool use_mmap;
116 off_t offset;
117 off_t filesize;
118 } plugin_input_file_t;
120 /* The master list of all plugins. */
121 static plugin_t *plugins_list = NULL;
123 /* We keep a tail pointer for easy linking on the end. */
124 static plugin_t **plugins_tail_chain_ptr = &plugins_list;
126 /* The last plugin added to the list, for receiving args. */
127 static plugin_t *last_plugin = NULL;
129 /* The tail of the arg chain of the last plugin added to the list. */
130 static plugin_arg_t **last_plugin_args_tail_chain_ptr = NULL;
132 /* The plugin which is currently having a callback executed. */
133 static plugin_t *called_plugin = NULL;
135 /* Last plugin to cause an error, if any. */
136 static const char *error_plugin = NULL;
138 /* State of linker "notice" interface before we poked at it. */
139 static bool orig_notice_all;
141 /* Original linker callbacks, and the plugin version. */
142 static const struct bfd_link_callbacks *orig_callbacks;
143 static struct bfd_link_callbacks plugin_callbacks;
145 /* Set at all symbols read time, to avoid recursively offering the plugin
146 its own newly-added input files and libs to claim. */
147 bool no_more_claiming = false;
149 #if HAVE_MMAP && HAVE_GETPAGESIZE
150 /* Page size used by mmap. */
151 static off_t plugin_pagesize;
152 #endif
154 /* List of tags to set in the constant leading part of the tv array. */
155 static const enum ld_plugin_tag tv_header_tags[] =
157 LDPT_MESSAGE,
158 LDPT_API_VERSION,
159 LDPT_GNU_LD_VERSION,
160 LDPT_LINKER_OUTPUT,
161 LDPT_OUTPUT_NAME,
162 LDPT_REGISTER_CLAIM_FILE_HOOK,
163 LDPT_REGISTER_CLAIM_FILE_HOOK_V2,
164 LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK,
165 LDPT_REGISTER_CLEANUP_HOOK,
166 LDPT_ADD_SYMBOLS,
167 LDPT_GET_INPUT_FILE,
168 LDPT_GET_VIEW,
169 LDPT_RELEASE_INPUT_FILE,
170 LDPT_GET_SYMBOLS,
171 LDPT_GET_SYMBOLS_V2,
172 LDPT_ADD_INPUT_FILE,
173 LDPT_ADD_INPUT_LIBRARY,
174 LDPT_SET_EXTRA_LIBRARY_PATH
177 /* How many entries in the constant leading part of the tv array. */
178 static const size_t tv_header_size = ARRAY_SIZE (tv_header_tags);
180 /* Forward references. */
181 static bool plugin_notice (struct bfd_link_info *,
182 struct bfd_link_hash_entry *,
183 struct bfd_link_hash_entry *,
184 bfd *, asection *, bfd_vma, flagword);
186 static bfd_cleanup plugin_object_p (bfd *, bool);
188 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
190 #define RTLD_NOW 0 /* Dummy value. */
192 static void *
193 dlopen (const char *file, int mode ATTRIBUTE_UNUSED)
195 return LoadLibrary (file);
198 static void *
199 dlsym (void *handle, const char *name)
201 return GetProcAddress (handle, name);
204 static int
205 dlclose (void *handle)
207 FreeLibrary (handle);
208 return 0;
211 #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */
213 #ifndef HAVE_DLFCN_H
214 static const char *
215 dlerror (void)
217 return "";
219 #endif
221 /* Helper function for exiting with error status. */
222 static int
223 set_plugin_error (const char *plugin)
225 error_plugin = plugin;
226 return -1;
229 /* Test if an error occurred. */
230 static bool
231 plugin_error_p (void)
233 return error_plugin != NULL;
236 /* Return name of plugin which caused an error if any. */
237 const char *
238 plugin_error_plugin (void)
240 return error_plugin ? error_plugin : _("<no plugin>");
243 /* Handle -plugin arg: find and load plugin, or return error. */
244 void
245 plugin_opt_plugin (const char *plugin)
247 plugin_t *newplug;
248 plugin_t *curplug = plugins_list;
250 newplug = xmalloc (sizeof *newplug);
251 memset (newplug, 0, sizeof *newplug);
252 newplug->name = plugin;
253 newplug->dlhandle = dlopen (plugin, RTLD_NOW);
254 if (!newplug->dlhandle)
255 einfo (_("%F%P: %s: error loading plugin: %s\n"), plugin, dlerror ());
257 /* Check if plugin has been loaded already. */
258 while (curplug)
260 if (newplug->dlhandle == curplug->dlhandle)
262 einfo (_("%P: %s: duplicated plugin\n"), plugin);
263 free (newplug);
264 return;
266 curplug = curplug->next;
269 /* Chain on end, so when we run list it is in command-line order. */
270 *plugins_tail_chain_ptr = newplug;
271 plugins_tail_chain_ptr = &newplug->next;
273 /* Record it as current plugin for receiving args. */
274 last_plugin = newplug;
275 last_plugin_args_tail_chain_ptr = &newplug->args;
278 /* Accumulate option arguments for last-loaded plugin, or return
279 error if none. */
281 plugin_opt_plugin_arg (const char *arg)
283 plugin_arg_t *newarg;
285 if (!last_plugin)
286 return set_plugin_error (_("<no plugin>"));
288 /* Ignore -pass-through= from GCC driver. */
289 if (*arg == '-')
291 const char *p = arg + 1;
293 if (*p == '-')
294 ++p;
295 if (strncmp (p, "pass-through=", 13) == 0)
296 return 0;
299 newarg = xmalloc (sizeof *newarg);
300 newarg->arg = arg;
301 newarg->next = NULL;
303 /* Chain on end to preserve command-line order. */
304 *last_plugin_args_tail_chain_ptr = newarg;
305 last_plugin_args_tail_chain_ptr = &newarg->next;
306 last_plugin->n_args++;
307 return 0;
310 /* Generate a dummy BFD to represent an IR file, for any callers of
311 plugin_call_claim_file to use as the handle in the ld_plugin_input_file
312 struct that they build to pass in. The BFD is initially writable, so
313 that symbols can be added to it; it must be made readable after the
314 add_symbols hook has been called so that it can be read when linking. */
315 static bfd *
316 plugin_get_ir_dummy_bfd (const char *name, bfd *srctemplate)
318 bool bfd_plugin_target = bfd_plugin_target_p (srctemplate->xvec);
319 char *filename = concat (name, IRONLY_SUFFIX, (const char *) NULL);
320 bfd *abfd = bfd_create (filename, (bfd_plugin_target
321 ? link_info.output_bfd : srctemplate));
322 free (filename);
323 if (abfd != NULL)
325 abfd->flags |= BFD_LINKER_CREATED | BFD_PLUGIN;
326 if (!bfd_make_writable (abfd))
327 goto report_error;
328 if (!bfd_plugin_target)
330 bfd_set_arch_info (abfd, bfd_get_arch_info (srctemplate));
331 bfd_set_gp_size (abfd, bfd_get_gp_size (srctemplate));
332 if (!bfd_copy_private_bfd_data (srctemplate, abfd))
333 goto report_error;
336 flagword flags;
338 /* Create section to own the symbols. */
339 flags = (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY
340 | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_EXCLUDE);
341 if (bfd_make_section_anyway_with_flags (abfd, ".text", flags))
342 return abfd;
345 report_error:
346 einfo (_("%F%P: could not create dummy IR bfd: %E\n"));
347 return NULL;
350 /* Check if the BFD passed in is an IR dummy object file. */
351 static inline bool
352 is_ir_dummy_bfd (const bfd *abfd)
354 /* ABFD can sometimes legitimately be NULL, e.g. when called from one
355 of the linker callbacks for a symbol in the *ABS* or *UND* sections. */
356 return abfd != NULL && (abfd->flags & BFD_PLUGIN) != 0;
359 /* Helpers to convert between BFD and GOLD symbol formats. */
360 static enum ld_plugin_status
361 asymbol_from_plugin_symbol (bfd *abfd, asymbol *asym,
362 const struct ld_plugin_symbol *ldsym)
364 flagword flags = BSF_NO_FLAGS;
365 struct bfd_section *section;
367 asym->the_bfd = abfd;
368 asym->name = (ldsym->version
369 ? stat_concat (ldsym->name, "@", ldsym->version,
370 (const char *) NULL)
371 : ldsym->name);
372 asym->value = 0;
373 switch (ldsym->def)
375 case LDPK_WEAKDEF:
376 flags = BSF_WEAK;
377 /* FALLTHRU */
378 case LDPK_DEF:
379 flags |= BSF_GLOBAL;
380 if (ldsym->comdat_key)
382 char *name = stat_concat (".gnu.linkonce.t.", ldsym->comdat_key,
383 (const char *) NULL);
384 section = bfd_get_section_by_name (abfd, name);
385 if (section != NULL)
386 stat_free (name);
387 else
389 flagword sflags;
391 sflags = (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY
392 | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_EXCLUDE
393 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD);
394 section = bfd_make_section_anyway_with_flags (abfd, name, sflags);
395 if (section == NULL)
396 return LDPS_ERR;
399 else
400 section = bfd_get_section_by_name (abfd, ".text");
401 break;
403 case LDPK_WEAKUNDEF:
404 flags = BSF_WEAK;
405 /* FALLTHRU */
406 case LDPK_UNDEF:
407 section = bfd_und_section_ptr;
408 break;
410 case LDPK_COMMON:
411 flags = BSF_GLOBAL;
412 section = bfd_com_section_ptr;
413 asym->value = ldsym->size;
414 break;
416 default:
417 return LDPS_ERR;
419 asym->flags = flags;
420 asym->section = section;
422 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
424 elf_symbol_type *elfsym = elf_symbol_from (asym);
425 unsigned char visibility;
427 if (!elfsym)
428 einfo (_("%F%P: %s: non-ELF symbol in ELF BFD!\n"), asym->name);
430 if (ldsym->def == LDPK_COMMON)
432 elfsym->internal_elf_sym.st_shndx = SHN_COMMON;
433 elfsym->internal_elf_sym.st_value = 1;
436 switch (ldsym->visibility)
438 default:
439 einfo (_("%F%P: unknown ELF symbol visibility: %d!\n"),
440 ldsym->visibility);
441 return LDPS_ERR;
443 case LDPV_DEFAULT:
444 visibility = STV_DEFAULT;
445 break;
446 case LDPV_PROTECTED:
447 visibility = STV_PROTECTED;
448 break;
449 case LDPV_INTERNAL:
450 visibility = STV_INTERNAL;
451 break;
452 case LDPV_HIDDEN:
453 visibility = STV_HIDDEN;
454 break;
456 elfsym->internal_elf_sym.st_other |= visibility;
459 return LDPS_OK;
462 /* Register a claim-file handler. */
463 static enum ld_plugin_status
464 register_claim_file (ld_plugin_claim_file_handler handler)
466 ASSERT (called_plugin);
467 called_plugin->claim_file_handler = handler;
468 return LDPS_OK;
471 /* Register a claim-file version 2 handler. */
472 static enum ld_plugin_status
473 register_claim_file_v2 (ld_plugin_claim_file_handler_v2 handler)
475 ASSERT (called_plugin);
476 called_plugin->claim_file_handler_v2 = handler;
477 return LDPS_OK;
480 /* Register an all-symbols-read handler. */
481 static enum ld_plugin_status
482 register_all_symbols_read (ld_plugin_all_symbols_read_handler handler)
484 ASSERT (called_plugin);
485 called_plugin->all_symbols_read_handler = handler;
486 return LDPS_OK;
489 /* Register a cleanup handler. */
490 static enum ld_plugin_status
491 register_cleanup (ld_plugin_cleanup_handler handler)
493 ASSERT (called_plugin);
494 called_plugin->cleanup_handler = handler;
495 return LDPS_OK;
498 /* Add symbols from a plugin-claimed input file. */
499 static enum ld_plugin_status
500 add_symbols (void *handle, int nsyms, const struct ld_plugin_symbol *syms)
502 asymbol **symptrs;
503 plugin_input_file_t *input = handle;
504 bfd *abfd = input->abfd;
505 int n;
507 ASSERT (called_plugin);
508 symptrs = bfd_alloc (abfd, nsyms * sizeof *symptrs);
509 if (symptrs == NULL)
510 return LDPS_ERR;
511 for (n = 0; n < nsyms; n++)
513 enum ld_plugin_status rv;
514 asymbol *bfdsym;
516 bfdsym = bfd_make_empty_symbol (abfd);
517 symptrs[n] = bfdsym;
518 if (bfdsym == NULL)
519 return LDPS_ERR;
520 rv = asymbol_from_plugin_symbol (abfd, bfdsym, syms + n);
521 if (rv != LDPS_OK)
522 return rv;
524 bfd_set_symtab (abfd, symptrs, nsyms);
525 return LDPS_OK;
528 /* Get the input file information with an open (possibly re-opened)
529 file descriptor. */
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;
542 return LDPS_OK;
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;
550 char *buffer;
551 size_t size = input->filesize;
552 off_t offset = input->offset;
553 #if HAVE_MMAP && HAVE_GETPAGESIZE
554 off_t bias;
555 #endif
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;
570 return LDPS_OK;
573 input->view_buffer.filesize = size;
574 input->view_buffer.offset = offset;
576 #if HAVE_MMAP
577 # if HAVE_GETPAGESIZE
578 bias = offset % plugin_pagesize;
579 offset -= bias;
580 size += bias;
581 # endif
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
587 buffer += bias;
588 # endif
590 else
591 #endif
593 char *p;
595 input->use_mmap = false;
597 if (lseek (input->fd, offset, SEEK_SET) < 0)
598 return LDPS_ERR;
600 buffer = bfd_alloc (input->abfd, size);
601 if (buffer == NULL)
602 return LDPS_ERR;
604 p = buffer;
607 ssize_t got = read (input->fd, p, size);
608 if (got == 0)
609 break;
610 else if (got > 0)
612 p += got;
613 size -= got;
615 else if (errno != EINTR)
616 return LDPS_ERR;
618 while (size > 0);
621 input->view_buffer.addr = buffer;
622 *viewp = buffer;
624 return LDPS_OK;
627 /* Release plugin file descriptor. */
629 static void
630 release_plugin_file_descriptor (plugin_input_file_t *input)
632 if (input->fd != -1)
634 bfd_plugin_close_file_descriptor (input->ibfd, input->fd);
635 input->fd = -1;
639 /* Release the input file. */
640 static enum ld_plugin_status
641 release_input_file (const void *handle)
643 plugin_input_file_t *input = (plugin_input_file_t *) handle;
644 ASSERT (called_plugin);
645 release_plugin_file_descriptor (input);
646 return LDPS_OK;
649 /* Return TRUE if a defined symbol might be reachable from outside the
650 universe of claimed objects. */
651 static inline bool
652 is_visible_from_outside (struct ld_plugin_symbol *lsym,
653 struct bfd_link_hash_entry *blhe)
655 if (bfd_link_relocatable (&link_info))
656 return true;
657 if (blhe->non_ir_ref_dynamic
658 || link_info.export_dynamic
659 || bfd_link_dll (&link_info))
661 /* Check if symbol is hidden by version script. */
662 if (bfd_hide_sym_by_version (link_info.version_info,
663 blhe->root.string))
664 return false;
665 /* Only ELF symbols really have visibility. */
666 if (is_elf_hash_table (link_info.hash))
668 struct elf_link_hash_entry *el = (struct elf_link_hash_entry *)blhe;
669 int vis = ELF_ST_VISIBILITY (el->other);
670 return vis == STV_DEFAULT || vis == STV_PROTECTED;
672 /* On non-ELF targets, we can safely make inferences by considering
673 what visibility the plugin would have liked to apply when it first
674 sent us the symbol. During ELF symbol processing, visibility only
675 ever becomes more restrictive, not less, when symbols are merged,
676 so this is a conservative estimate; it may give false positives,
677 declaring something visible from outside when it in fact would
678 not have been, but this will only lead to missed optimisation
679 opportunities during LTRANS at worst; it will not give false
680 negatives, which can lead to the disastrous conclusion that the
681 related symbol is IRONLY. (See GCC PR46319 for an example.) */
682 return (lsym->visibility == LDPV_DEFAULT
683 || lsym->visibility == LDPV_PROTECTED);
686 return false;
689 /* Return LTO kind string name that corresponds to IDX enum value. */
690 static const char *
691 get_lto_kind (unsigned int idx)
693 static char buffer[64];
694 const char *lto_kind_str[5] =
696 "DEF",
697 "WEAKDEF",
698 "UNDEF",
699 "WEAKUNDEF",
700 "COMMON"
703 if (idx < ARRAY_SIZE (lto_kind_str))
704 return lto_kind_str [idx];
706 sprintf (buffer, _("unknown LTO kind value %x"), idx);
707 return buffer;
710 /* Return LTO resolution string name that corresponds to IDX enum value. */
711 static const char *
712 get_lto_resolution (unsigned int idx)
714 static char buffer[64];
715 static const char *lto_resolution_str[10] =
717 "UNKNOWN",
718 "UNDEF",
719 "PREVAILING_DEF",
720 "PREVAILING_DEF_IRONLY",
721 "PREEMPTED_REG",
722 "PREEMPTED_IR",
723 "RESOLVED_IR",
724 "RESOLVED_EXEC",
725 "RESOLVED_DYN",
726 "PREVAILING_DEF_IRONLY_EXP",
729 if (idx < ARRAY_SIZE (lto_resolution_str))
730 return lto_resolution_str [idx];
732 sprintf (buffer, _("unknown LTO resolution value %x"), idx);
733 return buffer;
736 /* Return LTO visibility string name that corresponds to IDX enum value. */
737 static const char *
738 get_lto_visibility (unsigned int idx)
740 static char buffer[64];
741 const char *lto_visibility_str[4] =
743 "DEFAULT",
744 "PROTECTED",
745 "INTERNAL",
746 "HIDDEN"
749 if (idx < ARRAY_SIZE (lto_visibility_str))
750 return lto_visibility_str [idx];
752 sprintf (buffer, _("unknown LTO visibility value %x"), idx);
753 return buffer;
756 /* Get the symbol resolution info for a plugin-claimed input file. */
757 static enum ld_plugin_status
758 get_symbols (const void *handle, int nsyms, struct ld_plugin_symbol *syms,
759 int def_ironly_exp)
761 const plugin_input_file_t *input = handle;
762 const bfd *abfd = (const bfd *) input->abfd;
763 int n;
765 ASSERT (called_plugin);
766 for (n = 0; n < nsyms; n++)
768 struct bfd_link_hash_entry *blhe;
769 asection *owner_sec;
770 int res;
771 struct bfd_link_hash_entry *h
772 = bfd_link_hash_lookup (link_info.hash, syms[n].name,
773 false, false, true);
774 enum { wrap_none, wrapper, wrapped } wrap_status = wrap_none;
776 if (syms[n].def != LDPK_UNDEF && syms[n].def != LDPK_WEAKUNDEF)
778 blhe = h;
779 /* Check if a symbol is a wrapper symbol. */
780 if (blhe)
782 if (blhe->wrapper_symbol)
783 wrap_status = wrapper;
784 else if (link_info.wrap_hash != NULL)
786 struct bfd_link_hash_entry *unwrap
787 = unwrap_hash_lookup (&link_info, (bfd *) abfd, blhe);
788 if (unwrap != NULL && unwrap != h)
789 wrap_status = wrapper;
793 else
795 blhe = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
796 &link_info, syms[n].name,
797 false, false, true);
798 /* Check if a symbol is a wrapped symbol. */
799 if (blhe && blhe != h)
800 wrap_status = wrapped;
802 if (!blhe)
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
807 IR. */
808 switch (syms[n].def)
810 default:
811 abort ();
812 case LDPK_UNDEF:
813 case LDPK_WEAKUNDEF:
814 res = LDPR_UNDEF;
815 break;
816 case LDPK_DEF:
817 case LDPK_WEAKDEF:
818 case LDPK_COMMON:
819 res = LDPR_PREVAILING_DEF_IRONLY;
820 break;
822 goto report_symbol;
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)
829 res = LDPR_UNDEF;
830 goto report_symbol;
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;
864 else
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;
879 else
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
890 || blhe->ref_real
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;
899 report_symbol:
900 syms[n].resolution = res;
901 if (report_plugin_symbols)
902 einfo (_("%P: %pB: symbol `%s' "
903 "definition: %s, visibility: %s, resolution: %s\n"),
904 abfd, syms[n].name,
905 get_lto_kind (syms[n].def),
906 get_lto_visibility (syms[n].visibility),
907 get_lto_resolution (res));
909 return LDPS_OK;
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,
932 NULL);
933 if (!is)
934 return LDPS_ERR;
935 is->flags.lto_output = 1;
936 return LDPS_OK;
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,
947 NULL);
948 if (!is)
949 return LDPS_ERR;
950 is->flags.lto_output = 1;
951 return LDPS_OK;
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);
961 return LDPS_OK;
964 /* Issue a diagnostic message from a plugin. */
965 static enum ld_plugin_status
966 message (int level, const char *format, ...)
968 va_list args;
969 va_start (args, format);
971 switch (level)
973 case LDPL_INFO:
974 vfinfo (stdout, format, args, false);
975 putchar ('\n');
976 break;
977 case LDPL_WARNING:
979 char *newfmt = concat (_("%P: warning: "), format, "\n",
980 (const char *) NULL);
981 vfinfo (stdout, newfmt, args, true);
982 free (newfmt);
984 break;
985 case LDPL_FATAL:
986 case LDPL_ERROR:
987 default:
989 char *newfmt = concat (level == LDPL_FATAL ? "%F" : "%X",
990 _("%P: error: "), format, "\n",
991 (const char *) NULL);
992 fflush (stdout);
993 vfinfo (stderr, newfmt, args, true);
994 fflush (stderr);
995 free (newfmt);
997 break;
1000 va_end (args);
1001 return LDPS_OK;
1004 /* Helper to size leading part of tv array and set it up. */
1005 static void
1006 set_tv_header (struct ld_plugin_tv *tv)
1008 size_t i;
1010 /* Version info. */
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)
1020 case LDPT_MESSAGE:
1021 TVU(message) = message;
1022 break;
1023 case LDPT_API_VERSION:
1024 TVU(val) = LD_PLUGIN_API_VERSION;
1025 break;
1026 case LDPT_GNU_LD_VERSION:
1027 TVU(val) = major * 100 + minor;
1028 break;
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
1033 : LDPO_DYN);
1034 break;
1035 case LDPT_OUTPUT_NAME:
1036 TVU(string) = output_filename;
1037 break;
1038 case LDPT_REGISTER_CLAIM_FILE_HOOK:
1039 TVU(register_claim_file) = register_claim_file;
1040 break;
1041 case LDPT_REGISTER_CLAIM_FILE_HOOK_V2:
1042 TVU(register_claim_file_v2) = register_claim_file_v2;
1043 break;
1044 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
1045 TVU(register_all_symbols_read) = register_all_symbols_read;
1046 break;
1047 case LDPT_REGISTER_CLEANUP_HOOK:
1048 TVU(register_cleanup) = register_cleanup;
1049 break;
1050 case LDPT_ADD_SYMBOLS:
1051 TVU(add_symbols) = add_symbols;
1052 break;
1053 case LDPT_GET_INPUT_FILE:
1054 TVU(get_input_file) = get_input_file;
1055 break;
1056 case LDPT_GET_VIEW:
1057 TVU(get_view) = get_view;
1058 break;
1059 case LDPT_RELEASE_INPUT_FILE:
1060 TVU(release_input_file) = release_input_file;
1061 break;
1062 case LDPT_GET_SYMBOLS:
1063 TVU(get_symbols) = get_symbols_v1;
1064 break;
1065 case LDPT_GET_SYMBOLS_V2:
1066 TVU(get_symbols) = get_symbols_v2;
1067 break;
1068 case LDPT_ADD_INPUT_FILE:
1069 TVU(add_input_file) = add_input_file;
1070 break;
1071 case LDPT_ADD_INPUT_LIBRARY:
1072 TVU(add_input_library) = add_input_library;
1073 break;
1074 case LDPT_SET_EXTRA_LIBRARY_PATH:
1075 TVU(set_extra_library_path) = set_extra_library_path;
1076 break;
1077 default:
1078 /* Added a new entry to the array without adding
1079 a new case to set up its value is a bug. */
1080 FAIL ();
1082 #undef TVU
1086 /* Append the per-plugin args list and trailing LDPT_NULL to tv. */
1087 static void
1088 set_tv_plugin_args (plugin_t *plugin, struct ld_plugin_tv *tv)
1090 plugin_arg_t *arg = plugin->args;
1091 while (arg)
1093 tv->tv_tag = LDPT_OPTION;
1094 tv->tv_u.tv_string = arg->arg;
1095 arg = arg->next;
1096 tv++;
1098 tv->tv_tag = LDPT_NULL;
1099 tv->tv_u.tv_val = 0;
1102 /* Load up and initialise all plugins after argument parsing. */
1103 void
1104 plugin_load_plugins (void)
1106 struct ld_plugin_tv *my_tv;
1107 unsigned int max_args = 0;
1108 plugin_t *curplug = plugins_list;
1110 /* If there are no plugins, we need do nothing this run. */
1111 if (!curplug)
1112 return;
1114 /* First pass over plugins to find max # args needed so that we
1115 can size and allocate the tv array. */
1116 while (curplug)
1118 if (curplug->n_args > max_args)
1119 max_args = curplug->n_args;
1120 curplug = curplug->next;
1123 /* Allocate tv array and initialise constant part. */
1124 my_tv = stat_alloc ((max_args + 1 + tv_header_size) * sizeof (*my_tv));
1125 set_tv_header (my_tv);
1127 /* Pass over plugins again, activating them. */
1128 curplug = plugins_list;
1129 while (curplug)
1131 enum ld_plugin_status rv;
1132 ld_plugin_onload onloadfn;
1134 onloadfn = (ld_plugin_onload) dlsym (curplug->dlhandle, "onload");
1135 if (!onloadfn)
1136 onloadfn = (ld_plugin_onload) dlsym (curplug->dlhandle, "_onload");
1137 if (!onloadfn)
1138 einfo (_("%F%P: %s: error loading plugin: %s\n"),
1139 curplug->name, dlerror ());
1140 set_tv_plugin_args (curplug, &my_tv[tv_header_size]);
1141 called_plugin = curplug;
1142 rv = (*onloadfn) (my_tv);
1143 called_plugin = NULL;
1144 if (rv != LDPS_OK)
1145 einfo (_("%F%P: %s: plugin error: %d\n"), curplug->name, rv);
1146 curplug = curplug->next;
1149 /* Since plugin(s) inited ok, assume they're going to want symbol
1150 resolutions, which needs us to track which symbols are referenced
1151 by non-IR files using the linker's notice callback. */
1152 orig_notice_all = link_info.notice_all;
1153 orig_callbacks = link_info.callbacks;
1154 plugin_callbacks = *orig_callbacks;
1155 plugin_callbacks.notice = &plugin_notice;
1156 link_info.notice_all = true;
1157 link_info.lto_plugin_active = true;
1158 link_info.callbacks = &plugin_callbacks;
1160 register_ld_plugin_object_p (plugin_object_p);
1162 #if HAVE_MMAP && HAVE_GETPAGESIZE
1163 plugin_pagesize = getpagesize ();
1164 #endif
1167 /* Call 'claim file' hook for all plugins. */
1168 static int
1169 plugin_call_claim_file (const struct ld_plugin_input_file *file, int *claimed,
1170 int *claim_file_handler_v2, bool known_used)
1172 plugin_t *curplug = plugins_list;
1173 *claimed = false;
1174 *claim_file_handler_v2 = false;
1175 while (curplug && !*claimed)
1177 if (curplug->claim_file_handler)
1179 enum ld_plugin_status rv;
1181 called_plugin = curplug;
1182 if (curplug->claim_file_handler_v2)
1184 rv = (*curplug->claim_file_handler_v2) (file, claimed,
1185 known_used);
1186 *claim_file_handler_v2 = true;
1188 else
1189 rv = (*curplug->claim_file_handler) (file, claimed);
1190 called_plugin = NULL;
1191 if (rv != LDPS_OK)
1192 set_plugin_error (curplug->name);
1194 curplug = curplug->next;
1196 return plugin_error_p () ? -1 : 0;
1199 /* Duplicates a character string with memory attached to ABFD. */
1201 static char *
1202 plugin_strdup (bfd *abfd, const char *str)
1204 size_t strlength;
1205 char *copy;
1206 strlength = strlen (str) + 1;
1207 copy = bfd_alloc (abfd, strlength);
1208 if (copy == NULL)
1209 einfo (_("%F%P: plugin_strdup failed to allocate memory: %s\n"),
1210 bfd_get_error ());
1211 memcpy (copy, str, strlength);
1212 return copy;
1215 static void
1216 plugin_cleanup (bfd *abfd ATTRIBUTE_UNUSED)
1220 static bfd_cleanup
1221 plugin_object_p (bfd *ibfd, bool known_used)
1223 int claimed, claim_file_handler_v2;
1224 plugin_input_file_t *input;
1225 struct ld_plugin_input_file file;
1226 bfd *abfd;
1228 /* Don't try the dummy object file. */
1229 if ((ibfd->flags & BFD_PLUGIN) != 0)
1230 return NULL;
1232 /* When KNOWN_USED is false, we call plugin claim_file if plugin_format
1233 is bfd_plugin_unknown and set plugin_format to bfd_plugin_yes_unused
1234 on LTO object. When KNOWN_USED is true, we call plugin claim_file
1235 if plugin_format is bfd_plugin_unknown or bfd_plugin_yes_unused. */
1236 if (ibfd->plugin_format != bfd_plugin_unknown
1237 && (!known_used || ibfd->plugin_format != bfd_plugin_yes_unused))
1239 if (ibfd->plugin_format == bfd_plugin_no)
1240 return NULL;
1241 else
1242 return plugin_cleanup;
1245 /* We create a dummy BFD, initially empty, to house whatever symbols
1246 the plugin may want to add. */
1247 abfd = plugin_get_ir_dummy_bfd (bfd_get_filename (ibfd), ibfd);
1249 input = bfd_alloc (abfd, sizeof (*input));
1250 if (input == NULL)
1251 einfo (_("%F%P: plugin failed to allocate memory for input: %s\n"),
1252 bfd_get_error ());
1254 if (!bfd_plugin_open_input (ibfd, &file))
1255 return NULL;
1257 if (file.name == bfd_get_filename (ibfd))
1259 /* We must copy filename attached to ibfd if it is not an archive
1260 member since it may be freed by bfd_close below. */
1261 file.name = plugin_strdup (abfd, file.name);
1264 file.handle = input;
1265 input->abfd = abfd;
1266 input->ibfd = ibfd->my_archive != NULL ? ibfd : NULL;
1267 input->view_buffer.addr = NULL;
1268 input->view_buffer.filesize = 0;
1269 input->view_buffer.offset = 0;
1270 input->fd = file.fd;
1271 input->use_mmap = false;
1272 input->offset = file.offset;
1273 input->filesize = file.filesize;
1274 input->name = plugin_strdup (abfd, bfd_get_filename (ibfd));
1276 claimed = 0;
1278 if (plugin_call_claim_file (&file, &claimed, &claim_file_handler_v2,
1279 known_used))
1280 einfo (_("%F%P: %s: plugin reported error claiming file\n"),
1281 plugin_error_plugin ());
1283 if (input->fd != -1
1284 && (!claimed || !bfd_plugin_target_p (ibfd->xvec)))
1286 /* FIXME: fd belongs to us, not the plugin. GCC plugin, which
1287 doesn't need fd after plugin_call_claim_file, doesn't use
1288 BFD plugin target vector. Since GCC plugin doesn't call
1289 release_input_file, we close it here. LLVM plugin, which
1290 needs fd after plugin_call_claim_file and calls
1291 release_input_file after it is done, uses BFD plugin target
1292 vector. This scheme doesn't work when a plugin needs fd and
1293 doesn't use BFD plugin target vector neither. */
1294 release_plugin_file_descriptor (input);
1297 if (claimed)
1299 /* Set plugin_format to bfd_plugin_yes_unused if KNOWN_USED is
1300 false for plugin claim_file_v2 to avoid including the unused
1301 LTO archive members in linker output. */
1302 if (known_used || !claim_file_handler_v2)
1303 ibfd->plugin_format = bfd_plugin_yes;
1304 else
1305 ibfd->plugin_format = bfd_plugin_yes_unused;
1306 ibfd->plugin_dummy_bfd = abfd;
1307 bfd_make_readable (abfd);
1308 abfd->no_export = ibfd->no_export;
1309 return plugin_cleanup;
1311 else
1313 #if HAVE_MMAP
1314 if (input->use_mmap)
1316 /* If plugin didn't claim the file, unmap the buffer. */
1317 char *addr = input->view_buffer.addr;
1318 off_t size = input->view_buffer.filesize;
1319 # if HAVE_GETPAGESIZE
1320 off_t bias = input->view_buffer.offset % plugin_pagesize;
1321 size += bias;
1322 addr -= bias;
1323 # endif
1324 munmap (addr, size);
1326 #endif
1328 /* If plugin didn't claim the file, we don't need the dummy bfd.
1329 Can't avoid speculatively creating it, alas. */
1330 ibfd->plugin_format = bfd_plugin_no;
1331 bfd_close_all_done (abfd);
1332 return NULL;
1336 void
1337 plugin_maybe_claim (lang_input_statement_type *entry)
1339 ASSERT (entry->header.type == lang_input_statement_enum);
1340 if (plugin_object_p (entry->the_bfd, true))
1342 bfd *abfd = entry->the_bfd->plugin_dummy_bfd;
1344 /* Check object only section. */
1345 cmdline_check_object_only_section (entry->the_bfd, true);
1347 /* Discard the real file's BFD and substitute the dummy one. */
1349 /* We can't call bfd_close on archives. BFD archive handling
1350 caches elements, and add_archive_element keeps pointers to
1351 the_bfd and the_bfd->filename in a lang_input_statement_type
1352 linker script statement. */
1353 if (entry->the_bfd->my_archive == NULL)
1354 bfd_close (entry->the_bfd);
1355 entry->the_bfd = abfd;
1356 entry->flags.claimed = 1;
1360 /* Call 'all symbols read' hook for all plugins. */
1362 plugin_call_all_symbols_read (void)
1364 plugin_t *curplug = plugins_list;
1366 /* Disable any further file-claiming. */
1367 no_more_claiming = true;
1369 while (curplug)
1371 if (curplug->all_symbols_read_handler)
1373 enum ld_plugin_status rv;
1374 called_plugin = curplug;
1375 rv = (*curplug->all_symbols_read_handler) ();
1376 called_plugin = NULL;
1377 if (rv != LDPS_OK)
1378 set_plugin_error (curplug->name);
1380 curplug = curplug->next;
1382 return plugin_error_p () ? -1 : 0;
1385 /* Call 'cleanup' hook for all plugins at exit. */
1386 void
1387 plugin_call_cleanup (void)
1389 plugin_t *curplug = plugins_list;
1390 while (curplug)
1392 if (curplug->cleanup_handler && !curplug->cleanup_done)
1394 if (!config.plugin_save_temps)
1396 enum ld_plugin_status rv;
1397 curplug->cleanup_done = true;
1398 called_plugin = curplug;
1399 rv = (*curplug->cleanup_handler) ();
1400 called_plugin = NULL;
1401 if (rv != LDPS_OK)
1402 info_msg (_("%P: %s: error in plugin cleanup: %d (ignored)\n"),
1403 curplug->name, rv);
1405 dlclose (curplug->dlhandle);
1407 curplug = curplug->next;
1411 /* To determine which symbols should be resolved LDPR_PREVAILING_DEF
1412 and which LDPR_PREVAILING_DEF_IRONLY, we notice all the symbols as
1413 the linker adds them to the linker hash table. Mark those
1414 referenced from a non-IR file with non_ir_ref_regular or
1415 non_ir_ref_dynamic as appropriate. We have to notice_all symbols,
1416 because we won't necessarily know until later which ones will be
1417 contributed by IR files. */
1418 static bool
1419 plugin_notice (struct bfd_link_info *info,
1420 struct bfd_link_hash_entry *h,
1421 struct bfd_link_hash_entry *inh,
1422 bfd *abfd,
1423 asection *section,
1424 bfd_vma value,
1425 flagword flags)
1427 struct bfd_link_hash_entry *orig_h = h;
1429 if (h != NULL)
1431 bfd *sym_bfd;
1432 bool ref = false;
1434 if (h->type == bfd_link_hash_warning)
1435 h = h->u.i.link;
1437 /* Nothing to do here if this def/ref is from an IR dummy BFD. */
1438 if (is_ir_dummy_bfd (abfd))
1441 /* Making an indirect symbol counts as a reference unless this
1442 is a brand new symbol. */
1443 else if (bfd_is_ind_section (section)
1444 || (flags & BSF_INDIRECT) != 0)
1446 /* ??? Some of this is questionable. See comments in
1447 _bfd_generic_link_add_one_symbol for case IND. */
1448 if (h->type != bfd_link_hash_new
1449 || inh->type == bfd_link_hash_new)
1451 if ((abfd->flags & DYNAMIC) == 0)
1452 inh->non_ir_ref_regular = true;
1453 else
1454 inh->non_ir_ref_dynamic = true;
1457 if (h->type != bfd_link_hash_new)
1458 ref = true;
1461 /* Nothing to do here for warning symbols. */
1462 else if ((flags & BSF_WARNING) != 0)
1465 /* Nothing to do here for constructor symbols. */
1466 else if ((flags & BSF_CONSTRUCTOR) != 0)
1469 /* If this is a ref, set non_ir_ref. */
1470 else if (bfd_is_und_section (section))
1472 /* Replace the undefined dummy bfd with the real one. */
1473 if ((h->type == bfd_link_hash_undefined
1474 || h->type == bfd_link_hash_undefweak)
1475 && (h->u.undef.abfd == NULL
1476 || (h->u.undef.abfd->flags & BFD_PLUGIN) != 0))
1477 h->u.undef.abfd = abfd;
1478 ref = true;
1482 /* A common symbol should be merged with other commons or
1483 defs with the same name. In particular, a common ought
1484 to be overridden by a def in a -flto object. In that
1485 sense a common is also a ref. */
1486 else if (bfd_is_com_section (section))
1488 if (h->type == bfd_link_hash_common
1489 && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner))
1491 h->type = bfd_link_hash_undefweak;
1492 h->u.undef.abfd = sym_bfd;
1494 ref = true;
1497 /* Otherwise, it must be a new def.
1498 Ensure any symbol defined in an IR dummy BFD takes on a
1499 new value from a real BFD. Weak symbols are not normally
1500 overridden by a new weak definition, and strong symbols
1501 will normally cause multiple definition errors. Avoid
1502 this by making the symbol appear to be undefined.
1504 NB: We change the previous definition in the IR object to
1505 undefweak only after all LTO symbols have been read or for
1506 non-ELF targets. */
1507 else if ((info->lto_all_symbols_read
1508 || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
1509 && (((h->type == bfd_link_hash_defweak
1510 || h->type == bfd_link_hash_defined)
1511 && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
1512 || (h->type == bfd_link_hash_common
1513 && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner))))
1515 h->type = bfd_link_hash_undefweak;
1516 h->u.undef.abfd = sym_bfd;
1519 if (ref)
1521 if ((abfd->flags & DYNAMIC) == 0)
1522 h->non_ir_ref_regular = true;
1523 else
1524 h->non_ir_ref_dynamic = true;
1528 /* Continue with cref/nocrossref/trace-sym processing. */
1529 if (orig_h == NULL
1530 || orig_notice_all
1531 || (info->notice_hash != NULL
1532 && bfd_hash_lookup (info->notice_hash, orig_h->root.string,
1533 false, false) != NULL))
1534 return (*orig_callbacks->notice) (info, orig_h, inh,
1535 abfd, section, value, flags);
1536 return true;
1538 #endif /* BFD_SUPPORTS_PLUGINS */