Ignore broken plugins
[platform/upstream/binutils.git] / ld / plugin.c
1 /* Plugin control for the GNU linker.
2    Copyright (C) 2010-2023 Free Software Foundation, Inc.
3
4    This file is part of the GNU Binutils.
5
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.
10
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.
15
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.  */
20
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
59
60 /* Report plugin symbols.  */
61 bool report_plugin_symbols;
62
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)"
67
68 /* Stores a single argument passed to a plugin.  */
69 typedef struct plugin_arg
70 {
71   struct plugin_arg *next;
72   const char *arg;
73 } plugin_arg_t;
74
75 /* Holds all details of a single plugin.  */
76 typedef struct plugin
77 {
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_all_symbols_read_handler all_symbols_read_handler;
91   ld_plugin_cleanup_handler cleanup_handler;
92   /* TRUE if the cleanup handlers have been called.  */
93   bool cleanup_done;
94 } plugin_t;
95
96 typedef struct view_buffer
97 {
98   char *addr;
99   size_t filesize;
100   off_t offset;
101 } view_buffer_t;
102
103 /* The internal version of struct ld_plugin_input_file with a BFD
104    pointer.  */
105 typedef struct plugin_input_file
106 {
107   /* The dummy BFD.  */
108   bfd *abfd;
109   /* The original input BFD.  Non-NULL if it is an archive member.  */
110   bfd *ibfd;
111   view_buffer_t view_buffer;
112   char *name;
113   int fd;
114   bool use_mmap;
115   off_t offset;
116   off_t filesize;
117 } plugin_input_file_t;
118
119 /* The master list of all plugins.  */
120 static plugin_t *plugins_list = NULL;
121
122 /* We keep a tail pointer for easy linking on the end.  */
123 static plugin_t **plugins_tail_chain_ptr = &plugins_list;
124
125 /* The last plugin added to the list, for receiving args.  */
126 static plugin_t *last_plugin = NULL;
127
128 /* This is set when we ignored last plugin */
129 static bfd_boolean last_plugin_ignored;
130
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;
133
134 /* The plugin which is currently having a callback executed.  */
135 static plugin_t *called_plugin = NULL;
136
137 /* Last plugin to cause an error, if any.  */
138 static const char *error_plugin = NULL;
139
140 /* State of linker "notice" interface before we poked at it.  */
141 static bool orig_notice_all;
142
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;
146
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;
150
151 #if HAVE_MMAP && HAVE_GETPAGESIZE
152 /* Page size used by mmap.  */
153 static off_t plugin_pagesize;
154 #endif
155
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[] =
158 {
159   LDPT_MESSAGE,
160   LDPT_API_VERSION,
161   LDPT_GNU_LD_VERSION,
162   LDPT_LINKER_OUTPUT,
163   LDPT_OUTPUT_NAME,
164   LDPT_REGISTER_CLAIM_FILE_HOOK,
165   LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK,
166   LDPT_REGISTER_CLEANUP_HOOK,
167   LDPT_ADD_SYMBOLS,
168   LDPT_GET_INPUT_FILE,
169   LDPT_GET_VIEW,
170   LDPT_RELEASE_INPUT_FILE,
171   LDPT_GET_SYMBOLS,
172   LDPT_GET_SYMBOLS_V2,
173   LDPT_ADD_INPUT_FILE,
174   LDPT_ADD_INPUT_LIBRARY,
175   LDPT_SET_EXTRA_LIBRARY_PATH
176 };
177
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);
180
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);
186
187 static bfd_cleanup plugin_object_p (bfd *);
188
189 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
190
191 #define RTLD_NOW 0      /* Dummy value.  */
192
193 static void *
194 dlopen (const char *file, int mode ATTRIBUTE_UNUSED)
195 {
196   return LoadLibrary (file);
197 }
198
199 static void *
200 dlsym (void *handle, const char *name)
201 {
202   return GetProcAddress (handle, name);
203 }
204
205 static int
206 dlclose (void *handle)
207 {
208   FreeLibrary (handle);
209   return 0;
210 }
211
212 #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)  */
213
214 #ifndef HAVE_DLFCN_H
215 static const char *
216 dlerror (void)
217 {
218   return "";
219 }
220 #endif
221
222 /* Helper function for exiting with error status.  */
223 static int
224 set_plugin_error (const char *plugin)
225 {
226   error_plugin = plugin;
227   return -1;
228 }
229
230 /* Test if an error occurred.  */
231 static bool
232 plugin_error_p (void)
233 {
234   return error_plugin != NULL;
235 }
236
237 /* Return name of plugin which caused an error if any.  */
238 const char *
239 plugin_error_plugin (void)
240 {
241   return error_plugin ? error_plugin : _("<no plugin>");
242 }
243
244 /* Handle -plugin arg: find and load plugin, or return error.  */
245 void
246 plugin_opt_plugin (const char *plugin)
247 {
248   plugin_t *newplug;
249   plugin_t *curplug = plugins_list;
250
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;
259     free(newplug);
260     return;
261 #else
262     einfo (_("%P%F: %s: error loading plugin: %s\n"), plugin, dlerror ());
263 #endif
264   }
265
266   /* Check if plugin has been loaded already.  */
267   while (curplug)
268     {
269       if (newplug->dlhandle == curplug->dlhandle)
270         {
271           einfo (_("%P: %s: duplicated plugin\n"), plugin);
272           free (newplug);
273           return;
274         }
275       curplug = curplug->next;
276     }
277
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;
281
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;
287 #endif
288 }
289
290 /* Accumulate option arguments for last-loaded plugin, or return
291    error if none.  */
292 int
293 plugin_opt_plugin_arg (const char *arg)
294 {
295 #ifdef IGNORE_BROKEN_PLUGINS
296   /* Ignore options for ignored plugin */
297   if (last_plugin_ignored)
298     return 0;
299 #endif
300   plugin_arg_t *newarg;
301
302   if (!last_plugin)
303     return set_plugin_error (_("<no plugin>"));
304
305   /* Ignore -pass-through= from GCC driver.  */
306   if (*arg == '-')
307     {
308       const char *p = arg + 1;
309
310       if (*p == '-')
311         ++p;
312       if (strncmp (p, "pass-through=", 13) == 0)
313         return 0;
314     }
315
316   newarg = xmalloc (sizeof *newarg);
317   newarg->arg = arg;
318   newarg->next = NULL;
319
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++;
324   return 0;
325 }
326
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.  */
332 static bfd *
333 plugin_get_ir_dummy_bfd (const char *name, bfd *srctemplate)
334 {
335   bfd *abfd;
336   bool bfd_plugin_target;
337
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);
342   if (abfd != NULL)
343     {
344       abfd->flags |= BFD_LINKER_CREATED | BFD_PLUGIN;
345       if (!bfd_make_writable (abfd))
346         goto report_error;
347       if (!bfd_plugin_target)
348         {
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))
352             goto report_error;
353         }
354         {
355           flagword flags;
356
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))
361             return abfd;
362         }
363     }
364  report_error:
365   einfo (_("%F%P: could not create dummy IR bfd: %E\n"));
366   return NULL;
367 }
368
369 /* Check if the BFD passed in is an IR dummy object file.  */
370 static inline bool
371 is_ir_dummy_bfd (const bfd *abfd)
372 {
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;
376 }
377
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)
382 {
383   flagword flags = BSF_NO_FLAGS;
384   struct bfd_section *section;
385
386   asym->the_bfd = abfd;
387   asym->name = (ldsym->version
388                 ? concat (ldsym->name, "@", ldsym->version, (const char *) NULL)
389                 : ldsym->name);
390   asym->value = 0;
391   switch (ldsym->def)
392     {
393     case LDPK_WEAKDEF:
394       flags = BSF_WEAK;
395       /* FALLTHRU */
396     case LDPK_DEF:
397       flags |= BSF_GLOBAL;
398       if (ldsym->comdat_key)
399         {
400           char *name = concat (".gnu.linkonce.t.", ldsym->comdat_key,
401                                (const char *) NULL);
402           section = bfd_get_section_by_name (abfd, name);
403           if (section != NULL)
404             free (name);
405           else
406             {
407               flagword sflags;
408
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);
413               if (section == NULL)
414                 return LDPS_ERR;
415             }
416         }
417       else
418         section = bfd_get_section_by_name (abfd, ".text");
419       break;
420
421     case LDPK_WEAKUNDEF:
422       flags = BSF_WEAK;
423       /* FALLTHRU */
424     case LDPK_UNDEF:
425       section = bfd_und_section_ptr;
426       break;
427
428     case LDPK_COMMON:
429       flags = BSF_GLOBAL;
430       section = bfd_com_section_ptr;
431       asym->value = ldsym->size;
432       break;
433
434     default:
435       return LDPS_ERR;
436     }
437   asym->flags = flags;
438   asym->section = section;
439
440   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
441     {
442       elf_symbol_type *elfsym = elf_symbol_from (asym);
443       unsigned char visibility;
444
445       if (!elfsym)
446         einfo (_("%F%P: %s: non-ELF symbol in ELF BFD!\n"), asym->name);
447
448       if (ldsym->def == LDPK_COMMON)
449         {
450           elfsym->internal_elf_sym.st_shndx = SHN_COMMON;
451           elfsym->internal_elf_sym.st_value = 1;
452         }
453
454       switch (ldsym->visibility)
455         {
456         default:
457           einfo (_("%F%P: unknown ELF symbol visibility: %d!\n"),
458                  ldsym->visibility);
459           return LDPS_ERR;
460
461         case LDPV_DEFAULT:
462           visibility = STV_DEFAULT;
463           break;
464         case LDPV_PROTECTED:
465           visibility = STV_PROTECTED;
466           break;
467         case LDPV_INTERNAL:
468           visibility = STV_INTERNAL;
469           break;
470         case LDPV_HIDDEN:
471           visibility = STV_HIDDEN;
472           break;
473         }
474       elfsym->internal_elf_sym.st_other |= visibility;
475     }
476
477   return LDPS_OK;
478 }
479
480 /* Register a claim-file handler.  */
481 static enum ld_plugin_status
482 register_claim_file (ld_plugin_claim_file_handler handler)
483 {
484   ASSERT (called_plugin);
485   called_plugin->claim_file_handler = handler;
486   return LDPS_OK;
487 }
488
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)
492 {
493   ASSERT (called_plugin);
494   called_plugin->all_symbols_read_handler = handler;
495   return LDPS_OK;
496 }
497
498 /* Register a cleanup handler.  */
499 static enum ld_plugin_status
500 register_cleanup (ld_plugin_cleanup_handler handler)
501 {
502   ASSERT (called_plugin);
503   called_plugin->cleanup_handler = handler;
504   return LDPS_OK;
505 }
506
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)
510 {
511   asymbol **symptrs;
512   plugin_input_file_t *input = handle;
513   bfd *abfd = input->abfd;
514   int n;
515
516   ASSERT (called_plugin);
517   symptrs = xmalloc (nsyms * sizeof *symptrs);
518   for (n = 0; n < nsyms; n++)
519     {
520       enum ld_plugin_status rv;
521       asymbol *bfdsym;
522
523       bfdsym = bfd_make_empty_symbol (abfd);
524       symptrs[n] = bfdsym;
525       rv = asymbol_from_plugin_symbol (abfd, bfdsym, syms + n);
526       if (rv != LDPS_OK)
527         return rv;
528     }
529   bfd_set_symtab (abfd, symptrs, nsyms);
530   return LDPS_OK;
531 }
532
533 /* Get the input file information with an open (possibly re-opened)
534    file descriptor.  */
535 static enum ld_plugin_status
536 get_input_file (const void *handle, struct ld_plugin_input_file *file)
537 {
538   const plugin_input_file_t *input = handle;
539
540   ASSERT (called_plugin);
541
542   file->name = input->name;
543   file->offset = input->offset;
544   file->filesize = input->filesize;
545   file->handle = (void *) handle;
546
547   return LDPS_OK;
548 }
549
550 /* Get view of the input file.  */
551 static enum ld_plugin_status
552 get_view (const void *handle, const void **viewp)
553 {
554   plugin_input_file_t *input = (plugin_input_file_t *) handle;
555   char *buffer;
556   size_t size = input->filesize;
557   off_t offset = input->offset;
558 #if HAVE_MMAP && HAVE_GETPAGESIZE
559   off_t bias;
560 #endif
561
562   ASSERT (called_plugin);
563
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);
568
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)
573     {
574       *viewp = input->view_buffer.addr;
575       return LDPS_OK;
576     }
577
578   input->view_buffer.filesize = size;
579   input->view_buffer.offset = offset;
580
581 #if HAVE_MMAP
582 # if HAVE_GETPAGESIZE
583   bias = offset % plugin_pagesize;
584   offset -= bias;
585   size += bias;
586 # endif
587   buffer = mmap (NULL, size, PROT_READ, MAP_PRIVATE, input->fd, offset);
588   if (buffer != MAP_FAILED)
589     {
590       input->use_mmap = true;
591 # if HAVE_GETPAGESIZE
592       buffer += bias;
593 # endif
594     }
595   else
596 #endif
597     {
598       char *p;
599
600       input->use_mmap = false;
601
602       if (lseek (input->fd, offset, SEEK_SET) < 0)
603         return LDPS_ERR;
604
605       buffer = bfd_alloc (input->abfd, size);
606       if (buffer == NULL)
607         return LDPS_ERR;
608
609       p = buffer;
610       do
611         {
612           ssize_t got = read (input->fd, p, size);
613           if (got == 0)
614             break;
615           else if (got > 0)
616             {
617               p += got;
618               size -= got;
619             }
620           else if (errno != EINTR)
621             return LDPS_ERR;
622         }
623       while (size > 0);
624     }
625
626   input->view_buffer.addr = buffer;
627   *viewp = buffer;
628
629   return LDPS_OK;
630 }
631
632 /* Release plugin file descriptor.  */
633
634 static void
635 release_plugin_file_descriptor (plugin_input_file_t *input)
636 {
637   if (input->fd != -1)
638     {
639       bfd_plugin_close_file_descriptor (input->ibfd, input->fd);
640       input->fd = -1;
641     }
642 }
643
644 /* Release the input file.  */
645 static enum ld_plugin_status
646 release_input_file (const void *handle)
647 {
648   plugin_input_file_t *input = (plugin_input_file_t *) handle;
649   ASSERT (called_plugin);
650   release_plugin_file_descriptor (input);
651   return LDPS_OK;
652 }
653
654 /* Return TRUE if a defined symbol might be reachable from outside the
655    universe of claimed objects.  */
656 static inline bool
657 is_visible_from_outside (struct ld_plugin_symbol *lsym,
658                          struct bfd_link_hash_entry *blhe)
659 {
660   if (bfd_link_relocatable (&link_info))
661     return true;
662   if (blhe->non_ir_ref_dynamic
663       || link_info.export_dynamic
664       || bfd_link_dll (&link_info))
665     {
666       /* Check if symbol is hidden by version script.  */
667       if (bfd_hide_sym_by_version (link_info.version_info,
668                                    blhe->root.string))
669         return false;
670       /* Only ELF symbols really have visibility.  */
671       if (is_elf_hash_table (link_info.hash))
672         {
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;
676         }
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);
689     }
690
691   return false;
692 }
693
694 /* Return LTO kind string name that corresponds to IDX enum value.  */
695 static const char *
696 get_lto_kind (unsigned int idx)
697 {
698   static char buffer[64];
699   const char *lto_kind_str[5] =
700   {
701     "DEF",
702     "WEAKDEF",
703     "UNDEF",
704     "WEAKUNDEF",
705     "COMMON"
706   };
707
708   if (idx < ARRAY_SIZE (lto_kind_str))
709     return lto_kind_str [idx];
710
711   sprintf (buffer, _("unknown LTO kind value %x"), idx);
712   return buffer;
713 }
714
715 /* Return LTO resolution string name that corresponds to IDX enum value.  */
716 static const char *
717 get_lto_resolution (unsigned int idx)
718 {
719   static char buffer[64];
720   static const char *lto_resolution_str[10] =
721   {
722     "UNKNOWN",
723     "UNDEF",
724     "PREVAILING_DEF",
725     "PREVAILING_DEF_IRONLY",
726     "PREEMPTED_REG",
727     "PREEMPTED_IR",
728     "RESOLVED_IR",
729     "RESOLVED_EXEC",
730     "RESOLVED_DYN",
731     "PREVAILING_DEF_IRONLY_EXP",
732   };
733
734   if (idx < ARRAY_SIZE (lto_resolution_str))
735     return lto_resolution_str [idx];
736
737   sprintf (buffer, _("unknown LTO resolution value %x"), idx);
738   return buffer;
739 }
740
741 /* Return LTO visibility string name that corresponds to IDX enum value.  */
742 static const char *
743 get_lto_visibility (unsigned int idx)
744 {
745   static char buffer[64];
746   const char *lto_visibility_str[4] =
747   {
748     "DEFAULT",
749     "PROTECTED",
750     "INTERNAL",
751     "HIDDEN"
752   };
753
754   if (idx < ARRAY_SIZE (lto_visibility_str))
755     return lto_visibility_str [idx];
756
757   sprintf (buffer, _("unknown LTO visibility value %x"), idx);
758   return buffer;
759 }
760
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,
764              int def_ironly_exp)
765 {
766   const plugin_input_file_t *input = handle;
767   const bfd *abfd = (const bfd *) input->abfd;
768   int n;
769
770   ASSERT (called_plugin);
771   for (n = 0; n < nsyms; n++)
772     {
773       struct bfd_link_hash_entry *blhe;
774       asection *owner_sec;
775       int res;
776       struct bfd_link_hash_entry *h
777         = bfd_link_hash_lookup (link_info.hash, syms[n].name,
778                                 false, false, true);
779       enum { wrap_none, wrapper, wrapped } wrap_status = wrap_none;
780
781       if (syms[n].def != LDPK_UNDEF && syms[n].def != LDPK_WEAKUNDEF)
782         {
783           blhe = h;
784           if (blhe && link_info.wrap_hash != NULL)
785             {
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;
791              }
792         }
793       else
794         {
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;
801         }
802       if (!blhe)
803         {
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)
809             {
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;
821             }
822           goto report_symbol;
823         }
824
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)
828         {
829           res = LDPR_UNDEF;
830           goto report_symbol;
831         }
832       if (blhe->type != bfd_link_hash_defined
833           && blhe->type != bfd_link_hash_defweak
834           && blhe->type != bfd_link_hash_common)
835         {
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);
839         }
840
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);
847
848
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)
854         {
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;
866         }
867
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;
875
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;
881
882       if (res == LDPR_PREVAILING_DEF_IRONLY)
883         {
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;
897         }
898
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));
908     }
909   return LDPS_OK;
910 }
911
912 static enum ld_plugin_status
913 get_symbols_v1 (const void *handle, int nsyms, struct ld_plugin_symbol *syms)
914 {
915   return get_symbols (handle, nsyms, syms, LDPR_PREVAILING_DEF);
916 }
917
918 static enum ld_plugin_status
919 get_symbols_v2 (const void *handle, int nsyms, struct ld_plugin_symbol *syms)
920 {
921   return get_symbols (handle, nsyms, syms, LDPR_PREVAILING_DEF_IRONLY_EXP);
922 }
923
924 /* Add a new (real) input file generated by a plugin.  */
925 static enum ld_plugin_status
926 add_input_file (const char *pathname)
927 {
928   lang_input_statement_type *is;
929
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;
937 }
938
939 /* Add a new (real) library required by a plugin.  */
940 static enum ld_plugin_status
941 add_input_library (const char *pathname)
942 {
943   lang_input_statement_type *is;
944
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;
952 }
953
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)
958 {
959   ASSERT (called_plugin);
960   ldfile_add_library_path (xstrdup (path), false);
961   return LDPS_OK;
962 }
963
964 /* Issue a diagnostic message from a plugin.  */
965 static enum ld_plugin_status
966 message (int level, const char *format, ...)
967 {
968   va_list args;
969   va_start (args, format);
970
971   switch (level)
972     {
973     case LDPL_INFO:
974       vfinfo (stdout, format, args, false);
975       putchar ('\n');
976       break;
977     case LDPL_WARNING:
978       {
979         char *newfmt = concat (_("%P: warning: "), format, "\n",
980                                (const char *) NULL);
981         vfinfo (stdout, newfmt, args, true);
982         free (newfmt);
983       }
984       break;
985     case LDPL_FATAL:
986     case LDPL_ERROR:
987     default:
988       {
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);
996       }
997       break;
998     }
999
1000   va_end (args);
1001   return LDPS_OK;
1002 }
1003
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)
1007 {
1008   size_t i;
1009
1010   /* Version info.  */
1011   static const unsigned int major = (unsigned)(BFD_VERSION / 100000000UL);
1012   static const unsigned int minor = (unsigned)(BFD_VERSION / 1000000UL) % 100;
1013
1014   for (i = 0; i < tv_header_size; i++)
1015     {
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)
1019         {
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_ALL_SYMBOLS_READ_HOOK:
1042           TVU(register_all_symbols_read) = register_all_symbols_read;
1043           break;
1044         case LDPT_REGISTER_CLEANUP_HOOK:
1045           TVU(register_cleanup) = register_cleanup;
1046           break;
1047         case LDPT_ADD_SYMBOLS:
1048           TVU(add_symbols) = add_symbols;
1049           break;
1050         case LDPT_GET_INPUT_FILE:
1051           TVU(get_input_file) = get_input_file;
1052           break;
1053         case LDPT_GET_VIEW:
1054           TVU(get_view) = get_view;
1055           break;
1056         case LDPT_RELEASE_INPUT_FILE:
1057           TVU(release_input_file) = release_input_file;
1058           break;
1059         case LDPT_GET_SYMBOLS:
1060           TVU(get_symbols) = get_symbols_v1;
1061           break;
1062         case LDPT_GET_SYMBOLS_V2:
1063           TVU(get_symbols) = get_symbols_v2;
1064           break;
1065         case LDPT_ADD_INPUT_FILE:
1066           TVU(add_input_file) = add_input_file;
1067           break;
1068         case LDPT_ADD_INPUT_LIBRARY:
1069           TVU(add_input_library) = add_input_library;
1070           break;
1071         case LDPT_SET_EXTRA_LIBRARY_PATH:
1072           TVU(set_extra_library_path) = set_extra_library_path;
1073           break;
1074         default:
1075           /* Added a new entry to the array without adding
1076              a new case to set up its value is a bug.  */
1077           FAIL ();
1078         }
1079 #undef TVU
1080     }
1081 }
1082
1083 /* Append the per-plugin args list and trailing LDPT_NULL to tv.  */
1084 static void
1085 set_tv_plugin_args (plugin_t *plugin, struct ld_plugin_tv *tv)
1086 {
1087   plugin_arg_t *arg = plugin->args;
1088   while (arg)
1089     {
1090       tv->tv_tag = LDPT_OPTION;
1091       tv->tv_u.tv_string = arg->arg;
1092       arg = arg->next;
1093       tv++;
1094     }
1095   tv->tv_tag = LDPT_NULL;
1096   tv->tv_u.tv_val = 0;
1097 }
1098
1099 /* Load up and initialise all plugins after argument parsing.  */
1100 void
1101 plugin_load_plugins (void)
1102 {
1103   struct ld_plugin_tv *my_tv;
1104   unsigned int max_args = 0;
1105   plugin_t *curplug = plugins_list;
1106
1107   /* If there are no plugins, we need do nothing this run.  */
1108   if (!curplug)
1109     return;
1110
1111   /* First pass over plugins to find max # args needed so that we
1112      can size and allocate the tv array.  */
1113   while (curplug)
1114     {
1115       if (curplug->n_args > max_args)
1116         max_args = curplug->n_args;
1117       curplug = curplug->next;
1118     }
1119
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);
1123
1124   /* Pass over plugins again, activating them.  */
1125   curplug = plugins_list;
1126   while (curplug)
1127     {
1128       enum ld_plugin_status rv;
1129       ld_plugin_onload onloadfn;
1130
1131       onloadfn = (ld_plugin_onload) dlsym (curplug->dlhandle, "onload");
1132       if (!onloadfn)
1133         onloadfn = (ld_plugin_onload) dlsym (curplug->dlhandle, "_onload");
1134       if (!onloadfn)
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;
1141       if (rv != LDPS_OK)
1142         einfo (_("%F%P: %s: plugin error: %d\n"), curplug->name, rv);
1143       curplug = curplug->next;
1144     }
1145
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;
1156
1157   register_ld_plugin_object_p (plugin_object_p);
1158
1159 #if HAVE_MMAP && HAVE_GETPAGESIZE
1160   plugin_pagesize = getpagesize ();
1161 #endif
1162 }
1163
1164 /* Call 'claim file' hook for all plugins.  */
1165 static int
1166 plugin_call_claim_file (const struct ld_plugin_input_file *file, int *claimed)
1167 {
1168   plugin_t *curplug = plugins_list;
1169   *claimed = false;
1170   while (curplug && !*claimed)
1171     {
1172       if (curplug->claim_file_handler)
1173         {
1174           enum ld_plugin_status rv;
1175
1176           called_plugin = curplug;
1177           rv = (*curplug->claim_file_handler) (file, claimed);
1178           called_plugin = NULL;
1179           if (rv != LDPS_OK)
1180             set_plugin_error (curplug->name);
1181         }
1182       curplug = curplug->next;
1183     }
1184   return plugin_error_p () ? -1 : 0;
1185 }
1186
1187 /* Duplicates a character string with memory attached to ABFD.  */
1188
1189 static char *
1190 plugin_strdup (bfd *abfd, const char *str)
1191 {
1192   size_t strlength;
1193   char *copy;
1194   strlength = strlen (str) + 1;
1195   copy = bfd_alloc (abfd, strlength);
1196   if (copy == NULL)
1197     einfo (_("%F%P: plugin_strdup failed to allocate memory: %s\n"),
1198            bfd_get_error ());
1199   memcpy (copy, str, strlength);
1200   return copy;
1201 }
1202
1203 static void
1204 plugin_cleanup (bfd *abfd ATTRIBUTE_UNUSED)
1205 {
1206 }
1207
1208 static bfd_cleanup
1209 plugin_object_p (bfd *ibfd)
1210 {
1211   int claimed;
1212   plugin_input_file_t *input;
1213   struct ld_plugin_input_file file;
1214   bfd *abfd;
1215
1216   /* Don't try the dummy object file.  */
1217   if ((ibfd->flags & BFD_PLUGIN) != 0)
1218     return NULL;
1219
1220   if (ibfd->plugin_format != bfd_plugin_unknown)
1221     {
1222       if (ibfd->plugin_format == bfd_plugin_yes)
1223         return plugin_cleanup;
1224       else
1225         return NULL;
1226     }
1227
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);
1231
1232   input = bfd_alloc (abfd, sizeof (*input));
1233   if (input == NULL)
1234     einfo (_("%F%P: plugin failed to allocate memory for input: %s\n"),
1235            bfd_get_error ());
1236
1237   if (!bfd_plugin_open_input (ibfd, &file))
1238     return NULL;
1239
1240   if (file.name == bfd_get_filename (ibfd))
1241     {
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);
1245     }
1246
1247   file.handle = input;
1248   input->abfd = abfd;
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));
1258
1259   claimed = 0;
1260
1261   if (plugin_call_claim_file (&file, &claimed))
1262     einfo (_("%F%P: %s: plugin reported error claiming file\n"),
1263            plugin_error_plugin ());
1264
1265   if (input->fd != -1
1266       && (!claimed || !bfd_plugin_target_p (ibfd->xvec)))
1267     {
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);
1277     }
1278
1279   if (claimed)
1280     {
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;
1286     }
1287   else
1288     {
1289 #if HAVE_MMAP
1290       if (input->use_mmap)
1291         {
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;
1297           size += bias;
1298           addr -= bias;
1299 # endif
1300           munmap (addr, size);
1301         }
1302 #endif
1303
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);
1308       return NULL;
1309     }
1310 }
1311
1312 void
1313 plugin_maybe_claim (lang_input_statement_type *entry)
1314 {
1315   ASSERT (entry->header.type == lang_input_statement_enum);
1316   if (plugin_object_p (entry->the_bfd))
1317     {
1318       bfd *abfd = entry->the_bfd->plugin_dummy_bfd;
1319
1320       /* Discard the real file's BFD and substitute the dummy one.  */
1321
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;
1330     }
1331 }
1332
1333 /* Call 'all symbols read' hook for all plugins.  */
1334 int
1335 plugin_call_all_symbols_read (void)
1336 {
1337   plugin_t *curplug = plugins_list;
1338
1339   /* Disable any further file-claiming.  */
1340   no_more_claiming = true;
1341
1342   while (curplug)
1343     {
1344       if (curplug->all_symbols_read_handler)
1345         {
1346           enum ld_plugin_status rv;
1347           called_plugin = curplug;
1348           rv = (*curplug->all_symbols_read_handler) ();
1349           called_plugin = NULL;
1350           if (rv != LDPS_OK)
1351             set_plugin_error (curplug->name);
1352         }
1353       curplug = curplug->next;
1354     }
1355   return plugin_error_p () ? -1 : 0;
1356 }
1357
1358 /* Call 'cleanup' hook for all plugins at exit.  */
1359 void
1360 plugin_call_cleanup (void)
1361 {
1362   plugin_t *curplug = plugins_list;
1363   while (curplug)
1364     {
1365       if (curplug->cleanup_handler && !curplug->cleanup_done)
1366         {
1367           enum ld_plugin_status rv;
1368           curplug->cleanup_done = true;
1369           called_plugin = curplug;
1370           rv = (*curplug->cleanup_handler) ();
1371           called_plugin = NULL;
1372           if (rv != LDPS_OK)
1373             info_msg (_("%P: %s: error in plugin cleanup: %d (ignored)\n"),
1374                       curplug->name, rv);
1375           dlclose (curplug->dlhandle);
1376         }
1377       curplug = curplug->next;
1378     }
1379 }
1380
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.  */
1388 static bool
1389 plugin_notice (struct bfd_link_info *info,
1390                struct bfd_link_hash_entry *h,
1391                struct bfd_link_hash_entry *inh,
1392                bfd *abfd,
1393                asection *section,
1394                bfd_vma value,
1395                flagword flags)
1396 {
1397   struct bfd_link_hash_entry *orig_h = h;
1398
1399   if (h != NULL)
1400     {
1401       bfd *sym_bfd;
1402       bool ref = false;
1403
1404       if (h->type == bfd_link_hash_warning)
1405         h = h->u.i.link;
1406
1407       /* Nothing to do here if this def/ref is from an IR dummy BFD.  */
1408       if (is_ir_dummy_bfd (abfd))
1409         ;
1410
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)
1415         {
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)
1420             {
1421               if ((abfd->flags & DYNAMIC) == 0)
1422                 inh->non_ir_ref_regular = true;
1423               else
1424                 inh->non_ir_ref_dynamic = true;
1425             }
1426
1427           if (h->type != bfd_link_hash_new)
1428             ref = true;
1429         }
1430
1431       /* Nothing to do here for warning symbols.  */
1432       else if ((flags & BSF_WARNING) != 0)
1433         ;
1434
1435       /* Nothing to do here for constructor symbols.  */
1436       else if ((flags & BSF_CONSTRUCTOR) != 0)
1437         ;
1438
1439       /* If this is a ref, set non_ir_ref.  */
1440       else if (bfd_is_und_section (section))
1441         {
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;
1448           ref = true;
1449         }
1450
1451
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))
1457         {
1458           if (h->type == bfd_link_hash_common
1459               && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner))
1460             {
1461               h->type = bfd_link_hash_undefweak;
1462               h->u.undef.abfd = sym_bfd;
1463             }
1464           ref = true;
1465         }
1466
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.
1473
1474          NB: We change the previous definition in the IR object to
1475          undefweak only after all LTO symbols have been read or for
1476          non-ELF targets.  */
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))))
1484         {
1485           h->type = bfd_link_hash_undefweak;
1486           h->u.undef.abfd = sym_bfd;
1487         }
1488
1489       if (ref)
1490         {
1491           if ((abfd->flags & DYNAMIC) == 0)
1492             h->non_ir_ref_regular = true;
1493           else
1494             h->non_ir_ref_dynamic = true;
1495         }
1496     }
1497
1498   /* Continue with cref/nocrossref/trace-sym processing.  */
1499   if (orig_h == NULL
1500       || orig_notice_all
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);
1506   return true;
1507 }
1508 #endif /* BFD_SUPPORTS_PLUGINS */