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