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