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