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