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