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