Upload Tizen:Base source
[external/binutils.git] / ld / plugin.c
1 /* Plugin control for the GNU linker.
2    Copyright 2010, 2011 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 #include "bfdlink.h"
25 #include "bfdver.h"
26 #include "ld.h"
27 #include "ldmain.h"
28 #include "ldmisc.h"
29 #include "ldexp.h"
30 #include "ldlang.h"
31 #include "ldfile.h"
32 #include "plugin.h"
33 #include "plugin-api.h"
34 #include "elf-bfd.h"
35 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
36 #include <windows.h>
37 #endif
38
39 /* Report plugin symbols.  */
40 bfd_boolean report_plugin_symbols;
41
42 /* The suffix to append to the name of the real (claimed) object file
43    when generating a dummy BFD to hold the IR symbols sent from the
44    plugin.  For cosmetic use only; appears in maps, crefs etc.  */
45 #define IRONLY_SUFFIX " (symbol from plugin)"
46
47 /* Stores a single argument passed to a plugin.  */
48 typedef struct plugin_arg
49 {
50   struct plugin_arg *next;
51   const char *arg;
52 } plugin_arg_t;
53
54 /* Holds all details of a single plugin.  */
55 typedef struct plugin
56 {
57   /* Next on the list of plugins, or NULL at end of chain.  */
58   struct plugin *next;
59   /* The argument string given to --plugin.  */
60   const char *name;
61   /* The shared library handle returned by dlopen.  */
62   void *dlhandle;
63   /* The list of argument string given to --plugin-opt.  */
64   plugin_arg_t *args;
65   /* Number of args in the list, for convenience.  */
66   size_t n_args;
67   /* The plugin's event handlers.  */
68   ld_plugin_claim_file_handler claim_file_handler;
69   ld_plugin_all_symbols_read_handler all_symbols_read_handler;
70   ld_plugin_cleanup_handler cleanup_handler;
71   /* TRUE if the cleanup handlers have been called.  */
72   bfd_boolean cleanup_done;
73 } plugin_t;
74
75 /* The master list of all plugins.  */
76 static plugin_t *plugins_list = NULL;
77
78 /* We keep a tail pointer for easy linking on the end.  */
79 static plugin_t **plugins_tail_chain_ptr = &plugins_list;
80
81 /* The last plugin added to the list, for receiving args.  */
82 static plugin_t *last_plugin = NULL;
83
84 /* The tail of the arg chain of the last plugin added to the list.  */
85 static plugin_arg_t **last_plugin_args_tail_chain_ptr = NULL;
86
87 /* The plugin which is currently having a callback executed.  */
88 static plugin_t *called_plugin = NULL;
89
90 /* Last plugin to cause an error, if any.  */
91 static const char *error_plugin = NULL;
92
93 /* A hash table that records symbols referenced by non-IR files.  Used
94    at get_symbols time to determine whether any prevailing defs from
95    IR files are referenced only from other IR files, so tthat we can
96    we can distinguish the LDPR_PREVAILING_DEF and LDPR_PREVAILING_DEF_IRONLY
97    cases when establishing symbol resolutions.  */
98 static struct bfd_hash_table *non_ironly_hash = NULL;
99
100 /* State of linker "notice" interface before we poked at it.  */
101 static bfd_boolean orig_notice_all;
102
103 /* Original linker callbacks, and the plugin version.  */
104 static const struct bfd_link_callbacks *orig_callbacks;
105 static struct bfd_link_callbacks plugin_callbacks;
106
107 /* Set at all symbols read time, to avoid recursively offering the plugin
108    its own newly-added input files and libs to claim.  */
109 bfd_boolean no_more_claiming = FALSE;
110
111 /* List of tags to set in the constant leading part of the tv array. */
112 static const enum ld_plugin_tag tv_header_tags[] =
113 {
114   LDPT_MESSAGE,
115   LDPT_API_VERSION,
116   LDPT_GNU_LD_VERSION,
117   LDPT_LINKER_OUTPUT,
118   LDPT_OUTPUT_NAME,
119   LDPT_REGISTER_CLAIM_FILE_HOOK,
120   LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK,
121   LDPT_REGISTER_CLEANUP_HOOK,
122   LDPT_ADD_SYMBOLS,
123   LDPT_GET_INPUT_FILE,
124   LDPT_RELEASE_INPUT_FILE,
125   LDPT_GET_SYMBOLS,
126   LDPT_ADD_INPUT_FILE,
127   LDPT_ADD_INPUT_LIBRARY,
128   LDPT_SET_EXTRA_LIBRARY_PATH
129 };
130
131 /* How many entries in the constant leading part of the tv array.  */
132 static const size_t tv_header_size = ARRAY_SIZE (tv_header_tags);
133
134 /* Forward references.  */
135 static bfd_boolean plugin_notice (struct bfd_link_info *info,
136                                   const char *name, bfd *abfd,
137                                   asection *section, bfd_vma value);
138 static bfd_boolean plugin_multiple_definition (struct bfd_link_info *info,
139                                                struct bfd_link_hash_entry *h,
140                                                bfd *nbfd,
141                                                asection *nsec,
142                                                bfd_vma nval);
143 static bfd_boolean plugin_multiple_common (struct bfd_link_info *info,
144                                            struct bfd_link_hash_entry *h,
145                                            bfd *nbfd,
146                                            enum bfd_link_hash_type ntype,
147                                            bfd_vma nsize);
148
149 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
150
151 #define RTLD_NOW 0      /* Dummy value.  */
152
153 static void *
154 dlopen (const char *file, int mode ATTRIBUTE_UNUSED)
155 {
156   return LoadLibrary (file);
157 }
158
159 static void *
160 dlsym (void *handle, const char *name)
161 {
162   return GetProcAddress (handle, name);
163 }
164
165 static int
166 dlclose (void *handle)
167 {
168   FreeLibrary (handle);
169   return 0;
170 }
171
172 #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)  */
173
174 /* Helper function for exiting with error status.  */
175 static int
176 set_plugin_error (const char *plugin)
177 {
178   error_plugin = plugin;
179   return -1;
180 }
181
182 /* Test if an error occurred.  */
183 static bfd_boolean
184 plugin_error_p (void)
185 {
186   return error_plugin != NULL;
187 }
188
189 /* Return name of plugin which caused an error if any.  */
190 const char *
191 plugin_error_plugin (void)
192 {
193   return error_plugin ? error_plugin : _("<no plugin>");
194 }
195
196 /* Handle -plugin arg: find and load plugin, or return error.  */
197 int
198 plugin_opt_plugin (const char *plugin)
199 {
200   plugin_t *newplug;
201
202   newplug = xmalloc (sizeof *newplug);
203   memset (newplug, 0, sizeof *newplug);
204   newplug->name = plugin;
205   newplug->dlhandle = dlopen (plugin, RTLD_NOW);
206   if (!newplug->dlhandle)
207     return set_plugin_error (plugin);
208
209   /* Chain on end, so when we run list it is in command-line order.  */
210   *plugins_tail_chain_ptr = newplug;
211   plugins_tail_chain_ptr = &newplug->next;
212
213   /* Record it as current plugin for receiving args.  */
214   last_plugin = newplug;
215   last_plugin_args_tail_chain_ptr = &newplug->args;
216   return 0;
217 }
218
219 /* Accumulate option arguments for last-loaded plugin, or return
220    error if none.  */
221 int
222 plugin_opt_plugin_arg (const char *arg)
223 {
224   plugin_arg_t *newarg;
225
226   if (!last_plugin)
227     return set_plugin_error (_("<no plugin>"));
228
229   newarg = xmalloc (sizeof *newarg);
230   newarg->arg = arg;
231   newarg->next = NULL;
232
233   /* Chain on end to preserve command-line order.  */
234   *last_plugin_args_tail_chain_ptr = newarg;
235   last_plugin_args_tail_chain_ptr = &newarg->next;
236   last_plugin->n_args++;
237   return 0;
238 }
239
240 /* Create a dummy BFD.  */
241 bfd *
242 plugin_get_ir_dummy_bfd (const char *name, bfd *srctemplate)
243 {
244   bfd *abfd;
245
246   bfd_use_reserved_id = 1;
247   abfd = bfd_create (concat (name, IRONLY_SUFFIX, (const char *) NULL),
248                      srctemplate);
249   if (abfd != NULL)
250     {
251       abfd->flags |= BFD_LINKER_CREATED | BFD_PLUGIN;
252       bfd_set_arch_info (abfd, bfd_get_arch_info (srctemplate));
253       bfd_set_gp_size (abfd, bfd_get_gp_size (srctemplate));
254       if (bfd_make_writable (abfd)
255           && bfd_copy_private_bfd_data (srctemplate, abfd))
256         {
257           flagword flags;
258
259           /* Create sections to own the symbols.  */
260           flags = (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY
261                    | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_EXCLUDE);
262           if (bfd_make_section_anyway_with_flags (abfd, ".text", flags))
263             return abfd;
264         }
265     }
266   einfo (_("could not create dummy IR bfd: %F%E\n"));
267   return NULL;
268 }
269
270 /* Check if the BFD passed in is an IR dummy object file.  */
271 static bfd_boolean
272 is_ir_dummy_bfd (const bfd *abfd)
273 {
274   /* ABFD can sometimes legitimately be NULL, e.g. when called from one
275      of the linker callbacks for a symbol in the *ABS* or *UND* sections.
276      Likewise, the usrdata field may be NULL if ABFD was added by the
277      backend without a corresponding input statement, as happens e.g.
278      when processing DT_NEEDED dependencies.  */
279   return (abfd
280           && abfd->usrdata
281           && ((lang_input_statement_type *)(abfd->usrdata))->claimed);
282 }
283
284 /* Helpers to convert between BFD and GOLD symbol formats.  */
285 static enum ld_plugin_status
286 asymbol_from_plugin_symbol (bfd *abfd, asymbol *asym,
287                             const struct ld_plugin_symbol *ldsym)
288 {
289   flagword flags = BSF_NO_FLAGS;
290   struct bfd_section *section;
291
292   asym->the_bfd = abfd;
293   asym->name = (ldsym->version
294                 ? concat (ldsym->name, "@", ldsym->version, (const char *) NULL)
295                 : ldsym->name);
296   asym->value = 0;
297   switch (ldsym->def)
298     {
299     case LDPK_WEAKDEF:
300       flags = BSF_WEAK;
301       /* FALLTHRU */
302     case LDPK_DEF:
303       flags |= BSF_GLOBAL;
304       section = bfd_get_section_by_name (abfd, ".text");
305       break;
306
307     case LDPK_WEAKUNDEF:
308       flags = BSF_WEAK;
309       /* FALLTHRU */
310     case LDPK_UNDEF:
311       section = bfd_und_section_ptr;
312       break;
313
314     case LDPK_COMMON:
315       flags = BSF_GLOBAL;
316       section = bfd_com_section_ptr;
317       asym->value = ldsym->size;
318       /* For ELF targets, set alignment of common symbol to 1.  */
319       if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
320         {
321           ((elf_symbol_type *) asym)->internal_elf_sym.st_shndx = SHN_COMMON;
322           ((elf_symbol_type *) asym)->internal_elf_sym.st_value = 1;
323         }
324       break;
325
326     default:
327       return LDPS_ERR;
328     }
329   asym->flags = flags;
330   asym->section = section;
331
332   /* Visibility only applies on ELF targets.  */
333   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
334     {
335       elf_symbol_type *elfsym = elf_symbol_from (abfd, asym);
336       unsigned char visibility;
337
338       if (!elfsym)
339         einfo (_("%P%F: %s: non-ELF symbol in ELF BFD!\n"), asym->name);
340       switch (ldsym->visibility)
341         {
342         default:
343           einfo (_("%P%F: unknown ELF symbol visibility: %d!\n"),
344                  ldsym->visibility);
345         case LDPV_DEFAULT:
346           visibility = STV_DEFAULT;
347           break;
348         case LDPV_PROTECTED:
349           visibility = STV_PROTECTED;
350           break;
351         case LDPV_INTERNAL:
352           visibility = STV_INTERNAL;
353           break;
354         case LDPV_HIDDEN:
355           visibility = STV_HIDDEN;
356           break;
357         }
358       elfsym->internal_elf_sym.st_other
359         = (visibility | (elfsym->internal_elf_sym.st_other
360                          & ~ELF_ST_VISIBILITY (-1)));
361     }
362
363   return LDPS_OK;
364 }
365
366 /* Register a claim-file handler.  */
367 static enum ld_plugin_status
368 register_claim_file (ld_plugin_claim_file_handler handler)
369 {
370   ASSERT (called_plugin);
371   called_plugin->claim_file_handler = handler;
372   return LDPS_OK;
373 }
374
375 /* Register an all-symbols-read handler.  */
376 static enum ld_plugin_status
377 register_all_symbols_read (ld_plugin_all_symbols_read_handler handler)
378 {
379   ASSERT (called_plugin);
380   called_plugin->all_symbols_read_handler = handler;
381   return LDPS_OK;
382 }
383
384 /* Register a cleanup handler.  */
385 static enum ld_plugin_status
386 register_cleanup (ld_plugin_cleanup_handler handler)
387 {
388   ASSERT (called_plugin);
389   called_plugin->cleanup_handler = handler;
390   return LDPS_OK;
391 }
392
393 /* Add symbols from a plugin-claimed input file.  */
394 static enum ld_plugin_status
395 add_symbols (void *handle, int nsyms, const struct ld_plugin_symbol *syms)
396 {
397   asymbol **symptrs;
398   bfd *abfd = handle;
399   int n;
400   ASSERT (called_plugin);
401   symptrs = xmalloc (nsyms * sizeof *symptrs);
402   for (n = 0; n < nsyms; n++)
403     {
404       enum ld_plugin_status rv;
405       asymbol *bfdsym = bfd_make_empty_symbol (abfd);
406       symptrs[n] = bfdsym;
407       rv = asymbol_from_plugin_symbol (abfd, bfdsym, syms + n);
408       if (rv != LDPS_OK)
409         return rv;
410     }
411   bfd_set_symtab (abfd, symptrs, nsyms);
412   return LDPS_OK;
413 }
414
415 /* Get the input file information with an open (possibly re-opened)
416    file descriptor.  */
417 static enum ld_plugin_status
418 get_input_file (const void *handle, struct ld_plugin_input_file *file)
419 {
420   ASSERT (called_plugin);
421   handle = handle;
422   file = file;
423   return LDPS_ERR;
424 }
425
426 /* Release the input file.  */
427 static enum ld_plugin_status
428 release_input_file (const void *handle)
429 {
430   ASSERT (called_plugin);
431   handle = handle;
432   return LDPS_ERR;
433 }
434
435 /* Return TRUE if a defined symbol might be reachable from outside the
436    universe of claimed objects.  */
437 static inline bfd_boolean
438 is_visible_from_outside (struct ld_plugin_symbol *lsym, asection *section,
439                          struct bfd_link_hash_entry *blhe)
440 {
441   /* Section's owner may be NULL if it is the absolute
442      section, fortunately is_ir_dummy_bfd handles that.  */
443   if (!is_ir_dummy_bfd (section->owner))
444     return TRUE;
445   if (link_info.relocatable)
446     return TRUE;
447   if (link_info.export_dynamic || link_info.shared)
448     {
449       /* Only ELF symbols really have visibility.  */
450       if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
451         {
452           struct elf_link_hash_entry *el = (struct elf_link_hash_entry *)blhe;
453           int vis = ELF_ST_VISIBILITY (el->other);
454           return vis == STV_DEFAULT || vis == STV_PROTECTED;
455         }
456       /* On non-ELF targets, we can safely make inferences by considering
457          what visibility the plugin would have liked to apply when it first
458          sent us the symbol.  During ELF symbol processing, visibility only
459          ever becomes more restrictive, not less, when symbols are merged,
460          so this is a conservative estimate; it may give false positives,
461          declaring something visible from outside when it in fact would
462          not have been, but this will only lead to missed optimisation
463          opportunities during LTRANS at worst; it will not give false
464          negatives, which can lead to the disastrous conclusion that the
465          related symbol is IRONLY.  (See GCC PR46319 for an example.)  */
466       return (lsym->visibility == LDPV_DEFAULT
467               || lsym->visibility == LDPV_PROTECTED);
468     }
469   return FALSE;
470 }
471
472 /* Get the symbol resolution info for a plugin-claimed input file.  */
473 static enum ld_plugin_status
474 get_symbols (const void *handle, int nsyms, struct ld_plugin_symbol *syms)
475 {
476   const bfd *abfd = handle;
477   int n;
478   ASSERT (called_plugin);
479   for (n = 0; n < nsyms; n++)
480     {
481       struct bfd_link_hash_entry *blhe;
482       bfd_boolean ironly;
483       asection *owner_sec;
484       if (syms[n].def != LDPK_UNDEF)
485         blhe = bfd_link_hash_lookup (link_info.hash, syms[n].name,
486                                      FALSE, FALSE, TRUE);
487       else
488         blhe = bfd_wrapped_link_hash_lookup (link_info.output_bfd, &link_info,
489                                              syms[n].name, FALSE, FALSE, TRUE);
490       if (!blhe)
491         {
492           syms[n].resolution = LDPR_UNKNOWN;
493           goto report_symbol;
494         }
495
496       /* Determine resolution from blhe type and symbol's original type.  */
497       if (blhe->type == bfd_link_hash_undefined
498           || blhe->type == bfd_link_hash_undefweak)
499         {
500           syms[n].resolution = LDPR_UNDEF;
501           goto report_symbol;
502         }
503       if (blhe->type != bfd_link_hash_defined
504           && blhe->type != bfd_link_hash_defweak
505           && blhe->type != bfd_link_hash_common)
506         {
507           /* We should not have a new, indirect or warning symbol here.  */
508           einfo ("%P%F: %s: plugin symbol table corrupt (sym type %d)\n",
509                  called_plugin->name, blhe->type);
510         }
511
512       /* Find out which section owns the symbol.  Since it's not undef,
513          it must have an owner; if it's not a common symbol, both defs
514          and weakdefs keep it in the same place. */
515       owner_sec = (blhe->type == bfd_link_hash_common
516                    ? blhe->u.c.p->section
517                    : blhe->u.def.section);
518
519       /* We need to know if the sym is referenced from non-IR files.  Or
520          even potentially-referenced, perhaps in a future final link if
521          this is a partial one, perhaps dynamically at load-time if the
522          symbol is externally visible.  */
523       ironly = (!is_visible_from_outside (&syms[n], owner_sec, blhe)
524                 && !bfd_hash_lookup (non_ironly_hash, syms[n].name,
525                                      FALSE, FALSE));
526
527       /* If it was originally undefined or common, then it has been
528          resolved; determine how.  */
529       if (syms[n].def == LDPK_UNDEF
530           || syms[n].def == LDPK_WEAKUNDEF
531           || syms[n].def == LDPK_COMMON)
532         {
533           if (owner_sec->owner == link_info.output_bfd)
534             syms[n].resolution = LDPR_RESOLVED_EXEC;
535           else if (owner_sec->owner == abfd)
536             syms[n].resolution = (ironly
537                                   ? LDPR_PREVAILING_DEF_IRONLY
538                                   : LDPR_PREVAILING_DEF);
539           else if (is_ir_dummy_bfd (owner_sec->owner))
540             syms[n].resolution = LDPR_RESOLVED_IR;
541           else if (owner_sec->owner != NULL
542                    && (owner_sec->owner->flags & DYNAMIC) != 0)
543             syms[n].resolution =  LDPR_RESOLVED_DYN;
544           else
545             syms[n].resolution = LDPR_RESOLVED_EXEC;
546           goto report_symbol;
547         }
548
549       /* Was originally def, or weakdef.  Does it prevail?  If the
550          owner is the original dummy bfd that supplied it, then this
551          is the definition that has prevailed.  */
552       if (owner_sec->owner == link_info.output_bfd)
553         syms[n].resolution = LDPR_PREEMPTED_REG;
554       else if (owner_sec->owner == abfd)
555         {
556           syms[n].resolution = (ironly
557                                 ? LDPR_PREVAILING_DEF_IRONLY
558                                 : LDPR_PREVAILING_DEF);
559           goto report_symbol;
560         }
561
562       /* Was originally def, weakdef, or common, but has been pre-empted.  */
563       syms[n].resolution = (is_ir_dummy_bfd (owner_sec->owner)
564                             ? LDPR_PREEMPTED_IR
565                             : LDPR_PREEMPTED_REG);
566
567     report_symbol:
568       if (report_plugin_symbols)
569         einfo (_("%P: %B: symbol `%s' "
570                  "definition: %d, visibility: %d, resolution: %d\n"),
571                abfd, syms[n].name,
572                syms[n].def, syms[n].visibility, syms[n].resolution);
573     }
574   return LDPS_OK;
575 }
576
577 /* Add a new (real) input file generated by a plugin.  */
578 static enum ld_plugin_status
579 add_input_file (const char *pathname)
580 {
581   ASSERT (called_plugin);
582   if (!lang_add_input_file (xstrdup (pathname), lang_input_file_is_file_enum,
583                             NULL))
584     return LDPS_ERR;
585   return LDPS_OK;
586 }
587
588 /* Add a new (real) library required by a plugin.  */
589 static enum ld_plugin_status
590 add_input_library (const char *pathname)
591 {
592   ASSERT (called_plugin);
593   if (!lang_add_input_file (xstrdup (pathname), lang_input_file_is_l_enum,
594                             NULL))
595     return LDPS_ERR;
596   return LDPS_OK;
597 }
598
599 /* Set the extra library path to be used by libraries added via
600    add_input_library.  */
601 static enum ld_plugin_status
602 set_extra_library_path (const char *path)
603 {
604   ASSERT (called_plugin);
605   ldfile_add_library_path (xstrdup (path), FALSE);
606   return LDPS_OK;
607 }
608
609 /* Issue a diagnostic message from a plugin.  */
610 static enum ld_plugin_status
611 message (int level, const char *format, ...)
612 {
613   va_list args;
614   va_start (args, format);
615
616   switch (level)
617     {
618     case LDPL_INFO:
619       vfinfo (stdout, format, args, FALSE);
620       putchar ('\n');
621       break;
622     case LDPL_WARNING:
623       vfinfo (stdout, format, args, TRUE);
624       putchar ('\n');
625       break;
626     case LDPL_FATAL:
627     case LDPL_ERROR:
628     default:
629       {
630         char *newfmt = ACONCAT ((level == LDPL_FATAL ? "%P%F: " : "%P%X: ",
631                                  format, "\n", (const char *) NULL));
632         fflush (stdout);
633         vfinfo (stderr, newfmt, args, TRUE);
634         fflush (stderr);
635       }
636       break;
637     }
638
639   va_end (args);
640   return LDPS_OK;
641 }
642
643 /* Helper to size leading part of tv array and set it up. */
644 static size_t
645 set_tv_header (struct ld_plugin_tv *tv)
646 {
647   size_t i;
648
649   /* Version info.  */
650   static const unsigned int major = (unsigned)(BFD_VERSION / 100000000UL);
651   static const unsigned int minor = (unsigned)(BFD_VERSION / 1000000UL) % 100;
652
653   if (!tv)
654     return tv_header_size;
655
656   for (i = 0; i < tv_header_size; i++)
657     {
658       tv[i].tv_tag = tv_header_tags[i];
659 #define TVU(x) tv[i].tv_u.tv_ ## x
660       switch (tv[i].tv_tag)
661         {
662         case LDPT_MESSAGE:
663           TVU(message) = message;
664           break;
665         case LDPT_API_VERSION:
666           TVU(val) = LD_PLUGIN_API_VERSION;
667           break;
668         case LDPT_GNU_LD_VERSION:
669           TVU(val) = major * 100 + minor;
670           break;
671         case LDPT_LINKER_OUTPUT:
672           TVU(val) = (link_info.relocatable
673                       ? LDPO_REL
674                       : (link_info.shared ? LDPO_DYN : LDPO_EXEC));
675           break;
676         case LDPT_OUTPUT_NAME:
677           TVU(string) = output_filename;
678           break;
679         case LDPT_REGISTER_CLAIM_FILE_HOOK:
680           TVU(register_claim_file) = register_claim_file;
681           break;
682         case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
683           TVU(register_all_symbols_read) = register_all_symbols_read;
684           break;
685         case LDPT_REGISTER_CLEANUP_HOOK:
686           TVU(register_cleanup) = register_cleanup;
687           break;
688         case LDPT_ADD_SYMBOLS:
689           TVU(add_symbols) = add_symbols;
690           break;
691         case LDPT_GET_INPUT_FILE:
692           TVU(get_input_file) = get_input_file;
693           break;
694         case LDPT_RELEASE_INPUT_FILE:
695           TVU(release_input_file) = release_input_file;
696           break;
697         case LDPT_GET_SYMBOLS:
698           TVU(get_symbols) = get_symbols;
699           break;
700         case LDPT_ADD_INPUT_FILE:
701           TVU(add_input_file) = add_input_file;
702           break;
703         case LDPT_ADD_INPUT_LIBRARY:
704           TVU(add_input_library) = add_input_library;
705           break;
706         case LDPT_SET_EXTRA_LIBRARY_PATH:
707           TVU(set_extra_library_path) = set_extra_library_path;
708           break;
709         default:
710           /* Added a new entry to the array without adding
711              a new case to set up its value is a bug.  */
712           FAIL ();
713         }
714 #undef TVU
715     }
716   return tv_header_size;
717 }
718
719 /* Append the per-plugin args list and trailing LDPT_NULL to tv.  */
720 static void
721 set_tv_plugin_args (plugin_t *plugin, struct ld_plugin_tv *tv)
722 {
723   plugin_arg_t *arg = plugin->args;
724   while (arg)
725     {
726       tv->tv_tag = LDPT_OPTION;
727       tv->tv_u.tv_string = arg->arg;
728       arg = arg->next;
729       tv++;
730     }
731   tv->tv_tag = LDPT_NULL;
732   tv->tv_u.tv_val = 0;
733 }
734
735 /* Return true if any plugins are active this run.  Only valid
736    after options have been processed.  */
737 bfd_boolean
738 plugin_active_plugins_p (void)
739 {
740   return plugins_list != NULL;
741 }
742
743 /* Init the non_ironly hash table.  */
744 static void
745 init_non_ironly_hash (void)
746 {
747   struct bfd_sym_chain *sym;
748
749   non_ironly_hash
750     = (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
751   if (!bfd_hash_table_init_n (non_ironly_hash,
752                               bfd_hash_newfunc,
753                               sizeof (struct bfd_hash_entry),
754                               61))
755     einfo (_("%P%F: bfd_hash_table_init failed: %E\n"));
756
757   for (sym = &entry_symbol; sym != NULL; sym = sym->next)
758     if (sym->name
759         && !bfd_hash_lookup (non_ironly_hash, sym->name, TRUE, TRUE))
760       einfo (_("%P%X: hash table failure adding symbol %s\n"),
761              sym->name);
762 }
763
764 /* Load up and initialise all plugins after argument parsing.  */
765 int
766 plugin_load_plugins (void)
767 {
768   struct ld_plugin_tv *my_tv;
769   unsigned int max_args = 0;
770   plugin_t *curplug = plugins_list;
771
772   /* If there are no plugins, we need do nothing this run.  */
773   if (!curplug)
774     return 0;
775
776   /* First pass over plugins to find max # args needed so that we
777      can size and allocate the tv array.  */
778   while (curplug)
779     {
780       if (curplug->n_args > max_args)
781         max_args = curplug->n_args;
782       curplug = curplug->next;
783     }
784
785   /* Allocate tv array and initialise constant part.  */
786   my_tv = xmalloc ((max_args + 1 + tv_header_size) * sizeof *my_tv);
787   set_tv_header (my_tv);
788
789   /* Pass over plugins again, activating them.  */
790   curplug = plugins_list;
791   while (curplug)
792     {
793       enum ld_plugin_status rv;
794       ld_plugin_onload onloadfn = dlsym (curplug->dlhandle, "onload");
795       if (!onloadfn)
796         onloadfn = dlsym (curplug->dlhandle, "_onload");
797       if (!onloadfn)
798         return set_plugin_error (curplug->name);
799       set_tv_plugin_args (curplug, &my_tv[tv_header_size]);
800       called_plugin = curplug;
801       rv = (*onloadfn) (my_tv);
802       called_plugin = NULL;
803       if (rv != LDPS_OK)
804         return set_plugin_error (curplug->name);
805       curplug = curplug->next;
806     }
807
808   /* Since plugin(s) inited ok, assume they're going to want symbol
809      resolutions, which needs us to track which symbols are referenced
810      by non-IR files using the linker's notice callback.  */
811   orig_notice_all = link_info.notice_all;
812   orig_callbacks = link_info.callbacks;
813   plugin_callbacks = *orig_callbacks;
814   plugin_callbacks.notice = &plugin_notice;
815   link_info.notice_all = TRUE;
816   link_info.callbacks = &plugin_callbacks;
817   init_non_ironly_hash ();
818
819   return 0;
820 }
821
822 /* Call 'claim file' hook for all plugins.  */
823 static int
824 plugin_call_claim_file (const struct ld_plugin_input_file *file, int *claimed)
825 {
826   plugin_t *curplug = plugins_list;
827   *claimed = FALSE;
828   if (no_more_claiming)
829     return 0;
830   while (curplug && !*claimed)
831     {
832       if (curplug->claim_file_handler)
833         {
834           enum ld_plugin_status rv;
835           called_plugin = curplug;
836           rv = (*curplug->claim_file_handler) (file, claimed);
837           called_plugin = NULL;
838           if (rv != LDPS_OK)
839             set_plugin_error (curplug->name);
840         }
841       curplug = curplug->next;
842     }
843   return plugin_error_p () ? -1 : 0;
844 }
845
846 void
847 plugin_maybe_claim (struct ld_plugin_input_file *file,
848                     lang_input_statement_type *entry)
849 {
850   int claimed = 0;
851
852   /* We create a dummy BFD, initially empty, to house whatever symbols
853      the plugin may want to add.  */
854   file->handle = plugin_get_ir_dummy_bfd (entry->the_bfd->filename,
855                                           entry->the_bfd);
856   if (plugin_call_claim_file (file, &claimed))
857     einfo (_("%P%F: %s: plugin reported error claiming file\n"),
858            plugin_error_plugin ());
859   /* fd belongs to us, not the plugin; but we don't need it.  */
860   close (file->fd);
861   if (claimed)
862     {
863       /* Discard the real file's BFD and substitute the dummy one.  */
864
865       /* BFD archive handling caches elements so we can't call
866          bfd_close for archives.  */
867       if (entry->the_bfd->my_archive == NULL)
868         bfd_close (entry->the_bfd);
869       entry->the_bfd = file->handle;
870       entry->claimed = TRUE;
871       bfd_make_readable (entry->the_bfd);
872     }
873   else
874     {
875       /* If plugin didn't claim the file, we don't need the dummy bfd.
876          Can't avoid speculatively creating it, alas.  */
877       bfd_close_all_done (file->handle);
878       entry->claimed = FALSE;
879     }
880 }
881
882 /* Call 'all symbols read' hook for all plugins.  */
883 int
884 plugin_call_all_symbols_read (void)
885 {
886   plugin_t *curplug = plugins_list;
887
888   /* Disable any further file-claiming.  */
889   no_more_claiming = TRUE;
890
891   plugin_callbacks.multiple_definition = &plugin_multiple_definition;
892   plugin_callbacks.multiple_common = &plugin_multiple_common;
893
894   while (curplug)
895     {
896       if (curplug->all_symbols_read_handler)
897         {
898           enum ld_plugin_status rv;
899           called_plugin = curplug;
900           rv = (*curplug->all_symbols_read_handler) ();
901           called_plugin = NULL;
902           if (rv != LDPS_OK)
903             set_plugin_error (curplug->name);
904         }
905       curplug = curplug->next;
906     }
907   return plugin_error_p () ? -1 : 0;
908 }
909
910 /* Call 'cleanup' hook for all plugins at exit.  */
911 void
912 plugin_call_cleanup (void)
913 {
914   plugin_t *curplug = plugins_list;
915   while (curplug)
916     {
917       if (curplug->cleanup_handler && !curplug->cleanup_done)
918         {
919           enum ld_plugin_status rv;
920           curplug->cleanup_done = TRUE;
921           called_plugin = curplug;
922           rv = (*curplug->cleanup_handler) ();
923           called_plugin = NULL;
924           if (rv != LDPS_OK)
925             set_plugin_error (curplug->name);
926           dlclose (curplug->dlhandle);
927         }
928       curplug = curplug->next;
929     }
930   if (plugin_error_p ())
931     info_msg (_("%P: %s: error in plugin cleanup (ignored)\n"),
932               plugin_error_plugin ());
933 }
934
935 /* To determine which symbols should be resolved LDPR_PREVAILING_DEF
936    and which LDPR_PREVAILING_DEF_IRONLY, we notice all the symbols as
937    the linker adds them to the linker hash table.  If we see a symbol
938    being referenced from a non-IR file, we add it to the non_ironly hash
939    table.  If we can't find it there at get_symbols time, we know that
940    it was referenced only by IR files.  We have to notice_all symbols,
941    because we won't necessarily know until later which ones will be
942    contributed by IR files.  */
943 static bfd_boolean
944 plugin_notice (struct bfd_link_info *info,
945                const char *name,
946                bfd *abfd,
947                asection *section,
948                bfd_vma value)
949 {
950   if (name != NULL)
951     {
952       /* No further processing if this def/ref is from an IR dummy BFD.  */
953       if (is_ir_dummy_bfd (abfd))
954         return TRUE;
955
956       /* We only care about refs, not defs, indicated by section
957          pointing to the undefined section (according to the bfd
958          linker notice callback interface definition).  */
959       if (bfd_is_und_section (section))
960         {
961           /* This is a ref from a non-IR file, so note the ref'd
962              symbol in the non-IR-only hash.  */
963           if (!bfd_hash_lookup (non_ironly_hash, name, TRUE, TRUE))
964             einfo (_("%P%X: %s: hash table failure adding symbol %s\n"),
965                    abfd->filename, name);
966         }
967     }
968
969   /* Continue with cref/nocrossref/trace-sym processing.  */
970   if (name == NULL
971       || orig_notice_all
972       || (info->notice_hash != NULL
973           && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL))
974     return (*orig_callbacks->notice) (info, name, abfd, section, value);
975   return TRUE;
976 }
977
978 /* When we add new object files to the link at all symbols read time,
979    these contain the real code and symbols generated from the IR files,
980    and so duplicate all the definitions already supplied by the dummy
981    IR-only BFDs that we created at claim files time.  We use the linker's
982    multiple-definitions callback hook to fix up the clash, discarding
983    the symbol from the IR-only BFD in favour of the symbol from the
984    real BFD.  We return true if this was not-really-a-clash because
985    we've fixed it up, or anyway if --allow-multiple-definition was in
986    effect (before we disabled it to ensure we got called back).  */
987 static bfd_boolean
988 plugin_multiple_definition (struct bfd_link_info *info,
989                             struct bfd_link_hash_entry *h,
990                             bfd *nbfd, asection *nsec, bfd_vma nval)
991 {
992   if (h->type == bfd_link_hash_defined
993       && is_ir_dummy_bfd (h->u.def.section->owner))
994     {
995       /* Replace it with new details.  */
996       h->u.def.section = nsec;
997       h->u.def.value = nval;
998       return TRUE;
999     }
1000
1001   return (*orig_callbacks->multiple_definition) (info, h, nbfd, nsec, nval);
1002 }
1003
1004 static bfd_boolean
1005 plugin_multiple_common (struct bfd_link_info *info,
1006                         struct bfd_link_hash_entry *h,
1007                         bfd *nbfd, enum bfd_link_hash_type ntype, bfd_vma nsize)
1008 {
1009   if (h->type == bfd_link_hash_common
1010       && is_ir_dummy_bfd (h->u.c.p->section->owner)
1011       && ntype == bfd_link_hash_common
1012       && !is_ir_dummy_bfd (nbfd))
1013     {
1014       /* Arrange to have it replaced.  */
1015       ASSERT (nsize != 0);
1016       h->u.c.size = 0;
1017       return TRUE;
1018     }
1019
1020   return (*orig_callbacks->multiple_common) (info, h, nbfd, ntype, nsize);
1021 }