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