325481791d6212fe57c3f4cbbf3c5aa498078939
[deliverable/binutils-gdb.git] / ld / plugin.c
1 /* Plugin control for the GNU linker.
2 Copyright (C) 2010-2015 Free Software Foundation, Inc.
3
4 This file is part of the GNU Binutils.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "libiberty.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "bfdlink.h"
26 #include "bfdver.h"
27 #include "ld.h"
28 #include "ldmain.h"
29 #include "ldmisc.h"
30 #include "ldexp.h"
31 #include "ldlang.h"
32 #include "ldfile.h"
33 #include "plugin.h"
34 #include "plugin-api.h"
35 #include "elf-bfd.h"
36 #if HAVE_MMAP
37 # include <sys/mman.h>
38 # ifndef MAP_FAILED
39 # define MAP_FAILED ((void *) -1)
40 # endif
41 # ifndef PROT_READ
42 # define PROT_READ 0
43 # endif
44 # ifndef MAP_PRIVATE
45 # define MAP_PRIVATE 0
46 # endif
47 #endif
48 #include <errno.h>
49 #if !(defined(errno) || defined(_MSC_VER) && defined(_INC_ERRNO))
50 extern int errno;
51 #endif
52 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
53 #include <windows.h>
54 #endif
55
56 /* Report plugin symbols. */
57 bfd_boolean report_plugin_symbols;
58
59 /* The suffix to append to the name of the real (claimed) object file
60 when generating a dummy BFD to hold the IR symbols sent from the
61 plugin. For cosmetic use only; appears in maps, crefs etc. */
62 #define IRONLY_SUFFIX " (symbol from plugin)"
63
64 /* Stores a single argument passed to a plugin. */
65 typedef struct plugin_arg
66 {
67 struct plugin_arg *next;
68 const char *arg;
69 } plugin_arg_t;
70
71 /* Holds all details of a single plugin. */
72 typedef struct plugin
73 {
74 /* Next on the list of plugins, or NULL at end of chain. */
75 struct plugin *next;
76 /* The argument string given to --plugin. */
77 const char *name;
78 /* The shared library handle returned by dlopen. */
79 void *dlhandle;
80 /* The list of argument string given to --plugin-opt. */
81 plugin_arg_t *args;
82 /* Number of args in the list, for convenience. */
83 size_t n_args;
84 /* The plugin's event handlers. */
85 ld_plugin_claim_file_handler claim_file_handler;
86 ld_plugin_all_symbols_read_handler all_symbols_read_handler;
87 ld_plugin_cleanup_handler cleanup_handler;
88 /* TRUE if the cleanup handlers have been called. */
89 bfd_boolean cleanup_done;
90 } plugin_t;
91
92 typedef struct view_buffer
93 {
94 char *addr;
95 size_t filesize;
96 off_t offset;
97 } view_buffer_t;
98
99 /* The internal version of struct ld_plugin_input_file with a BFD
100 pointer. */
101 typedef struct plugin_input_file
102 {
103 bfd *abfd;
104 view_buffer_t view_buffer;
105 char *name;
106 int fd;
107 off_t offset;
108 off_t filesize;
109 } plugin_input_file_t;
110
111 /* The master list of all plugins. */
112 static plugin_t *plugins_list = NULL;
113
114 /* We keep a tail pointer for easy linking on the end. */
115 static plugin_t **plugins_tail_chain_ptr = &plugins_list;
116
117 /* The last plugin added to the list, for receiving args. */
118 static plugin_t *last_plugin = NULL;
119
120 /* The tail of the arg chain of the last plugin added to the list. */
121 static plugin_arg_t **last_plugin_args_tail_chain_ptr = NULL;
122
123 /* The plugin which is currently having a callback executed. */
124 static plugin_t *called_plugin = NULL;
125
126 /* Last plugin to cause an error, if any. */
127 static const char *error_plugin = NULL;
128
129 /* State of linker "notice" interface before we poked at it. */
130 static bfd_boolean orig_notice_all;
131
132 /* Original linker callbacks, and the plugin version. */
133 static const struct bfd_link_callbacks *orig_callbacks;
134 static struct bfd_link_callbacks plugin_callbacks;
135
136 /* Set at all symbols read time, to avoid recursively offering the plugin
137 its own newly-added input files and libs to claim. */
138 bfd_boolean no_more_claiming = FALSE;
139
140 /* List of tags to set in the constant leading part of the tv array. */
141 static const enum ld_plugin_tag tv_header_tags[] =
142 {
143 LDPT_MESSAGE,
144 LDPT_API_VERSION,
145 LDPT_GNU_LD_VERSION,
146 LDPT_LINKER_OUTPUT,
147 LDPT_OUTPUT_NAME,
148 LDPT_REGISTER_CLAIM_FILE_HOOK,
149 LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK,
150 LDPT_REGISTER_CLEANUP_HOOK,
151 LDPT_ADD_SYMBOLS,
152 LDPT_GET_INPUT_FILE,
153 LDPT_GET_VIEW,
154 LDPT_RELEASE_INPUT_FILE,
155 LDPT_GET_SYMBOLS,
156 LDPT_GET_SYMBOLS_V2,
157 LDPT_ADD_INPUT_FILE,
158 LDPT_ADD_INPUT_LIBRARY,
159 LDPT_SET_EXTRA_LIBRARY_PATH
160 };
161
162 /* How many entries in the constant leading part of the tv array. */
163 static const size_t tv_header_size = ARRAY_SIZE (tv_header_tags);
164
165 /* Forward references. */
166 static bfd_boolean plugin_notice (struct bfd_link_info *,
167 struct bfd_link_hash_entry *,
168 struct bfd_link_hash_entry *,
169 bfd *, asection *, bfd_vma, flagword);
170
171 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
172
173 #define RTLD_NOW 0 /* Dummy value. */
174
175 static void *
176 dlopen (const char *file, int mode ATTRIBUTE_UNUSED)
177 {
178 return LoadLibrary (file);
179 }
180
181 static void *
182 dlsym (void *handle, const char *name)
183 {
184 return GetProcAddress (handle, name);
185 }
186
187 static int
188 dlclose (void *handle)
189 {
190 FreeLibrary (handle);
191 return 0;
192 }
193
194 #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */
195
196 #ifndef HAVE_DLFCN_H
197 static const char *
198 dlerror (void)
199 {
200 return "";
201 }
202 #endif
203
204 /* Helper function for exiting with error status. */
205 static int
206 set_plugin_error (const char *plugin)
207 {
208 error_plugin = plugin;
209 return -1;
210 }
211
212 /* Test if an error occurred. */
213 static bfd_boolean
214 plugin_error_p (void)
215 {
216 return error_plugin != NULL;
217 }
218
219 /* Return name of plugin which caused an error if any. */
220 const char *
221 plugin_error_plugin (void)
222 {
223 return error_plugin ? error_plugin : _("<no plugin>");
224 }
225
226 /* Handle -plugin arg: find and load plugin, or return error. */
227 void
228 plugin_opt_plugin (const char *plugin)
229 {
230 plugin_t *newplug;
231
232 newplug = xmalloc (sizeof *newplug);
233 memset (newplug, 0, sizeof *newplug);
234 newplug->name = plugin;
235 newplug->dlhandle = dlopen (plugin, RTLD_NOW);
236 if (!newplug->dlhandle)
237 einfo (_("%P%F: %s: error loading plugin: %s\n"), plugin, dlerror ());
238
239 /* Chain on end, so when we run list it is in command-line order. */
240 *plugins_tail_chain_ptr = newplug;
241 plugins_tail_chain_ptr = &newplug->next;
242
243 /* Record it as current plugin for receiving args. */
244 last_plugin = newplug;
245 last_plugin_args_tail_chain_ptr = &newplug->args;
246 }
247
248 /* Accumulate option arguments for last-loaded plugin, or return
249 error if none. */
250 int
251 plugin_opt_plugin_arg (const char *arg)
252 {
253 plugin_arg_t *newarg;
254
255 if (!last_plugin)
256 return set_plugin_error (_("<no plugin>"));
257
258 /* Ignore -pass-through= from GCC driver. */
259 if (*arg == '-')
260 {
261 const char *p = arg + 1;
262
263 if (*p == '-')
264 ++p;
265 if (strncmp (p, "pass-through=", 13) == 0)
266 return 0;
267 }
268
269 newarg = xmalloc (sizeof *newarg);
270 newarg->arg = arg;
271 newarg->next = NULL;
272
273 /* Chain on end to preserve command-line order. */
274 *last_plugin_args_tail_chain_ptr = newarg;
275 last_plugin_args_tail_chain_ptr = &newarg->next;
276 last_plugin->n_args++;
277 return 0;
278 }
279
280 /* Generate a dummy BFD to represent an IR file, for any callers of
281 plugin_call_claim_file to use as the handle in the ld_plugin_input_file
282 struct that they build to pass in. The BFD is initially writable, so
283 that symbols can be added to it; it must be made readable after the
284 add_symbols hook has been called so that it can be read when linking. */
285 static bfd *
286 plugin_get_ir_dummy_bfd (const char *name, bfd *srctemplate)
287 {
288 bfd *abfd;
289
290 bfd_use_reserved_id = 1;
291 abfd = bfd_create (concat (name, IRONLY_SUFFIX, (const char *) NULL),
292 srctemplate);
293 if (abfd != NULL)
294 {
295 abfd->flags |= BFD_LINKER_CREATED | BFD_PLUGIN;
296 bfd_set_arch_info (abfd, bfd_get_arch_info (srctemplate));
297 bfd_set_gp_size (abfd, bfd_get_gp_size (srctemplate));
298 if (bfd_make_writable (abfd)
299 && bfd_copy_private_bfd_data (srctemplate, abfd))
300 {
301 flagword flags;
302
303 /* Create section to own the symbols. */
304 flags = (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY
305 | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_EXCLUDE);
306 if (bfd_make_section_anyway_with_flags (abfd, ".text", flags))
307 return abfd;
308 }
309 }
310 einfo (_("could not create dummy IR bfd: %F%E\n"));
311 return NULL;
312 }
313
314 /* Check if the BFD passed in is an IR dummy object file. */
315 static inline bfd_boolean
316 is_ir_dummy_bfd (const bfd *abfd)
317 {
318 /* ABFD can sometimes legitimately be NULL, e.g. when called from one
319 of the linker callbacks for a symbol in the *ABS* or *UND* sections. */
320 return abfd != NULL && (abfd->flags & BFD_PLUGIN) != 0;
321 }
322
323 /* Helpers to convert between BFD and GOLD symbol formats. */
324 static enum ld_plugin_status
325 asymbol_from_plugin_symbol (bfd *abfd, asymbol *asym,
326 const struct ld_plugin_symbol *ldsym)
327 {
328 flagword flags = BSF_NO_FLAGS;
329 struct bfd_section *section;
330
331 asym->the_bfd = abfd;
332 asym->name = (ldsym->version
333 ? concat (ldsym->name, "@", ldsym->version, (const char *) NULL)
334 : ldsym->name);
335 asym->value = 0;
336 switch (ldsym->def)
337 {
338 case LDPK_WEAKDEF:
339 flags = BSF_WEAK;
340 /* FALLTHRU */
341 case LDPK_DEF:
342 flags |= BSF_GLOBAL;
343 if (ldsym->comdat_key)
344 {
345 char *name = concat (".gnu.linkonce.t.", ldsym->comdat_key,
346 (const char *) NULL);
347 section = bfd_get_section_by_name (abfd, name);
348 if (section != NULL)
349 free (name);
350 else
351 {
352 flagword sflags;
353
354 sflags = (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY
355 | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_EXCLUDE
356 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD);
357 section = bfd_make_section_anyway_with_flags (abfd, name, sflags);
358 if (section == NULL)
359 return LDPS_ERR;
360 }
361 }
362 else
363 section = bfd_get_section_by_name (abfd, ".text");
364 break;
365
366 case LDPK_WEAKUNDEF:
367 flags = BSF_WEAK;
368 /* FALLTHRU */
369 case LDPK_UNDEF:
370 section = bfd_und_section_ptr;
371 break;
372
373 case LDPK_COMMON:
374 flags = BSF_GLOBAL;
375 section = bfd_com_section_ptr;
376 asym->value = ldsym->size;
377 /* For ELF targets, set alignment of common symbol to 1. */
378 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
379 {
380 ((elf_symbol_type *) asym)->internal_elf_sym.st_shndx = SHN_COMMON;
381 ((elf_symbol_type *) asym)->internal_elf_sym.st_value = 1;
382 }
383 break;
384
385 default:
386 return LDPS_ERR;
387 }
388 asym->flags = flags;
389 asym->section = section;
390
391 /* Visibility only applies on ELF targets. */
392 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
393 {
394 elf_symbol_type *elfsym = elf_symbol_from (abfd, asym);
395 unsigned char visibility;
396
397 if (!elfsym)
398 einfo (_("%P%F: %s: non-ELF symbol in ELF BFD!\n"), asym->name);
399 switch (ldsym->visibility)
400 {
401 default:
402 einfo (_("%P%F: unknown ELF symbol visibility: %d!\n"),
403 ldsym->visibility);
404 case LDPV_DEFAULT:
405 visibility = STV_DEFAULT;
406 break;
407 case LDPV_PROTECTED:
408 visibility = STV_PROTECTED;
409 break;
410 case LDPV_INTERNAL:
411 visibility = STV_INTERNAL;
412 break;
413 case LDPV_HIDDEN:
414 visibility = STV_HIDDEN;
415 break;
416 }
417 elfsym->internal_elf_sym.st_other
418 = (visibility | (elfsym->internal_elf_sym.st_other
419 & ~ELF_ST_VISIBILITY (-1)));
420 }
421
422 return LDPS_OK;
423 }
424
425 /* Register a claim-file handler. */
426 static enum ld_plugin_status
427 register_claim_file (ld_plugin_claim_file_handler handler)
428 {
429 ASSERT (called_plugin);
430 called_plugin->claim_file_handler = handler;
431 return LDPS_OK;
432 }
433
434 /* Register an all-symbols-read handler. */
435 static enum ld_plugin_status
436 register_all_symbols_read (ld_plugin_all_symbols_read_handler handler)
437 {
438 ASSERT (called_plugin);
439 called_plugin->all_symbols_read_handler = handler;
440 return LDPS_OK;
441 }
442
443 /* Register a cleanup handler. */
444 static enum ld_plugin_status
445 register_cleanup (ld_plugin_cleanup_handler handler)
446 {
447 ASSERT (called_plugin);
448 called_plugin->cleanup_handler = handler;
449 return LDPS_OK;
450 }
451
452 /* Add symbols from a plugin-claimed input file. */
453 static enum ld_plugin_status
454 add_symbols (void *handle, int nsyms, const struct ld_plugin_symbol *syms)
455 {
456 asymbol **symptrs;
457 plugin_input_file_t *input = handle;
458 bfd *abfd = input->abfd;
459 int n;
460
461 ASSERT (called_plugin);
462 symptrs = xmalloc (nsyms * sizeof *symptrs);
463 for (n = 0; n < nsyms; n++)
464 {
465 enum ld_plugin_status rv;
466 asymbol *bfdsym;
467
468 bfdsym = bfd_make_empty_symbol (abfd);
469 symptrs[n] = bfdsym;
470 rv = asymbol_from_plugin_symbol (abfd, bfdsym, syms + n);
471 if (rv != LDPS_OK)
472 return rv;
473 }
474 bfd_set_symtab (abfd, symptrs, nsyms);
475 return LDPS_OK;
476 }
477
478 /* Get the input file information with an open (possibly re-opened)
479 file descriptor. */
480 static enum ld_plugin_status
481 get_input_file (const void *handle, struct ld_plugin_input_file *file)
482 {
483 const plugin_input_file_t *input = handle;
484
485 ASSERT (called_plugin);
486
487 file->name = input->name;
488 file->offset = input->offset;
489 file->filesize = input->filesize;
490 file->handle = (void *) handle;
491
492 return LDPS_OK;
493 }
494
495 /* Get view of the input file. */
496 static enum ld_plugin_status
497 get_view (const void *handle, const void **viewp)
498 {
499 plugin_input_file_t *input = (plugin_input_file_t *) handle;
500 char *buffer;
501 size_t size = input->filesize;
502 #if HAVE_GETPAGESIZE
503 off_t offset, bias;
504 #endif
505
506 ASSERT (called_plugin);
507
508 /* FIXME: einfo should support %lld. */
509 if ((off_t) size != input->filesize)
510 einfo (_("%P%F: unsupported input file size: %s (%ld bytes)\n"),
511 input->name, (long) input->filesize);
512
513 /* Check the cached view buffer. */
514 if (input->view_buffer.addr != NULL
515 && input->view_buffer.filesize == size
516 && input->view_buffer.offset == input->offset)
517 {
518 *viewp = input->view_buffer.addr;
519 return LDPS_OK;
520 }
521
522 input->view_buffer.filesize = size;
523 input->view_buffer.offset = input->offset;
524
525 #if HAVE_MMAP
526 # if HAVE_GETPAGESIZE
527 bias = input->offset % getpagesize ();;
528 offset = input->offset - bias;
529 size += bias;
530 # endif
531 buffer = mmap (NULL, size, PROT_READ, MAP_PRIVATE, input->fd, offset);
532 if (buffer != MAP_FAILED)
533 buffer += bias;
534 else
535 #endif
536 {
537 char *p;
538
539 if (lseek (input->fd, input->offset, SEEK_SET) < 0)
540 return LDPS_ERR;
541
542 buffer = bfd_alloc (input->abfd, size);
543 if (buffer == NULL)
544 return LDPS_ERR;
545
546 p = buffer;
547 do
548 {
549 ssize_t got = read (input->fd, p, size);
550 if (got == 0)
551 break;
552 else if (got > 0)
553 {
554 p += got;
555 size -= got;
556 }
557 else if (errno != EINTR)
558 return LDPS_ERR;
559 }
560 while (size > 0);
561 }
562
563 input->view_buffer.addr = buffer;
564 *viewp = buffer;
565
566 return LDPS_OK;
567 }
568
569 /* Release the input file. */
570 static enum ld_plugin_status
571 release_input_file (const void *handle)
572 {
573 plugin_input_file_t *input = (plugin_input_file_t *) handle;
574 ASSERT (called_plugin);
575 if (input->fd != -1)
576 {
577 close (input->fd);
578 input->fd = -1;
579 }
580 return LDPS_OK;
581 }
582
583 /* Return TRUE if a defined symbol might be reachable from outside the
584 universe of claimed objects. */
585 static inline bfd_boolean
586 is_visible_from_outside (struct ld_plugin_symbol *lsym,
587 struct bfd_link_hash_entry *blhe)
588 {
589 struct bfd_sym_chain *sym;
590
591 if (link_info.relocatable)
592 return TRUE;
593 if (link_info.export_dynamic || !link_info.executable)
594 {
595 /* Check if symbol is hidden by version script. */
596 if (bfd_hide_sym_by_version (link_info.version_info,
597 blhe->root.string))
598 return FALSE;
599 /* Only ELF symbols really have visibility. */
600 if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
601 {
602 struct elf_link_hash_entry *el = (struct elf_link_hash_entry *)blhe;
603 int vis = ELF_ST_VISIBILITY (el->other);
604 return vis == STV_DEFAULT || vis == STV_PROTECTED;
605 }
606 /* On non-ELF targets, we can safely make inferences by considering
607 what visibility the plugin would have liked to apply when it first
608 sent us the symbol. During ELF symbol processing, visibility only
609 ever becomes more restrictive, not less, when symbols are merged,
610 so this is a conservative estimate; it may give false positives,
611 declaring something visible from outside when it in fact would
612 not have been, but this will only lead to missed optimisation
613 opportunities during LTRANS at worst; it will not give false
614 negatives, which can lead to the disastrous conclusion that the
615 related symbol is IRONLY. (See GCC PR46319 for an example.) */
616 return (lsym->visibility == LDPV_DEFAULT
617 || lsym->visibility == LDPV_PROTECTED);
618 }
619
620 for (sym = &entry_symbol; sym != NULL; sym = sym->next)
621 if (sym->name
622 && strcmp (sym->name, blhe->root.string) == 0)
623 return TRUE;
624
625 return FALSE;
626 }
627
628 /* Get the symbol resolution info for a plugin-claimed input file. */
629 static enum ld_plugin_status
630 get_symbols (const void *handle, int nsyms, struct ld_plugin_symbol *syms,
631 int def_ironly_exp)
632 {
633 const plugin_input_file_t *input = handle;
634 const bfd *abfd = (const bfd *) input->abfd;
635 int n;
636
637 ASSERT (called_plugin);
638 for (n = 0; n < nsyms; n++)
639 {
640 struct bfd_link_hash_entry *blhe;
641 asection *owner_sec;
642 int res;
643
644 if (syms[n].def != LDPK_UNDEF)
645 blhe = bfd_link_hash_lookup (link_info.hash, syms[n].name,
646 FALSE, FALSE, TRUE);
647 else
648 blhe = bfd_wrapped_link_hash_lookup (link_info.output_bfd, &link_info,
649 syms[n].name, FALSE, FALSE, TRUE);
650 if (!blhe)
651 {
652 res = LDPR_UNKNOWN;
653 goto report_symbol;
654 }
655
656 /* Determine resolution from blhe type and symbol's original type. */
657 if (blhe->type == bfd_link_hash_undefined
658 || blhe->type == bfd_link_hash_undefweak)
659 {
660 res = LDPR_UNDEF;
661 goto report_symbol;
662 }
663 if (blhe->type != bfd_link_hash_defined
664 && blhe->type != bfd_link_hash_defweak
665 && blhe->type != bfd_link_hash_common)
666 {
667 /* We should not have a new, indirect or warning symbol here. */
668 einfo ("%P%F: %s: plugin symbol table corrupt (sym type %d)\n",
669 called_plugin->name, blhe->type);
670 }
671
672 /* Find out which section owns the symbol. Since it's not undef,
673 it must have an owner; if it's not a common symbol, both defs
674 and weakdefs keep it in the same place. */
675 owner_sec = (blhe->type == bfd_link_hash_common
676 ? blhe->u.c.p->section
677 : blhe->u.def.section);
678
679
680 /* If it was originally undefined or common, then it has been
681 resolved; determine how. */
682 if (syms[n].def == LDPK_UNDEF
683 || syms[n].def == LDPK_WEAKUNDEF
684 || syms[n].def == LDPK_COMMON)
685 {
686 if (owner_sec->owner == link_info.output_bfd)
687 res = LDPR_RESOLVED_EXEC;
688 else if (owner_sec->owner == abfd)
689 res = LDPR_PREVAILING_DEF_IRONLY;
690 else if (is_ir_dummy_bfd (owner_sec->owner))
691 res = LDPR_RESOLVED_IR;
692 else if (owner_sec->owner != NULL
693 && (owner_sec->owner->flags & DYNAMIC) != 0)
694 res = LDPR_RESOLVED_DYN;
695 else
696 res = LDPR_RESOLVED_EXEC;
697 }
698
699 /* Was originally def, or weakdef. Does it prevail? If the
700 owner is the original dummy bfd that supplied it, then this
701 is the definition that has prevailed. */
702 else if (owner_sec->owner == link_info.output_bfd)
703 res = LDPR_PREEMPTED_REG;
704 else if (owner_sec->owner == abfd)
705 res = LDPR_PREVAILING_DEF_IRONLY;
706
707 /* Was originally def, weakdef, or common, but has been pre-empted. */
708 else if (is_ir_dummy_bfd (owner_sec->owner))
709 res = LDPR_PREEMPTED_IR;
710 else
711 res = LDPR_PREEMPTED_REG;
712
713 if (res == LDPR_PREVAILING_DEF_IRONLY)
714 {
715 /* We need to know if the sym is referenced from non-IR files. Or
716 even potentially-referenced, perhaps in a future final link if
717 this is a partial one, perhaps dynamically at load-time if the
718 symbol is externally visible. */
719 if (blhe->non_ir_ref)
720 res = LDPR_PREVAILING_DEF;
721 else if (is_visible_from_outside (&syms[n], blhe))
722 res = def_ironly_exp;
723 }
724
725 report_symbol:
726 syms[n].resolution = res;
727 if (report_plugin_symbols)
728 einfo (_("%P: %B: symbol `%s' "
729 "definition: %d, visibility: %d, resolution: %d\n"),
730 abfd, syms[n].name,
731 syms[n].def, syms[n].visibility, res);
732 }
733 return LDPS_OK;
734 }
735
736 static enum ld_plugin_status
737 get_symbols_v1 (const void *handle, int nsyms, struct ld_plugin_symbol *syms)
738 {
739 return get_symbols (handle, nsyms, syms, LDPR_PREVAILING_DEF);
740 }
741
742 static enum ld_plugin_status
743 get_symbols_v2 (const void *handle, int nsyms, struct ld_plugin_symbol *syms)
744 {
745 return get_symbols (handle, nsyms, syms, LDPR_PREVAILING_DEF_IRONLY_EXP);
746 }
747
748 /* Add a new (real) input file generated by a plugin. */
749 static enum ld_plugin_status
750 add_input_file (const char *pathname)
751 {
752 ASSERT (called_plugin);
753 if (!lang_add_input_file (xstrdup (pathname), lang_input_file_is_file_enum,
754 NULL))
755 return LDPS_ERR;
756 return LDPS_OK;
757 }
758
759 /* Add a new (real) library required by a plugin. */
760 static enum ld_plugin_status
761 add_input_library (const char *pathname)
762 {
763 ASSERT (called_plugin);
764 if (!lang_add_input_file (xstrdup (pathname), lang_input_file_is_l_enum,
765 NULL))
766 return LDPS_ERR;
767 return LDPS_OK;
768 }
769
770 /* Set the extra library path to be used by libraries added via
771 add_input_library. */
772 static enum ld_plugin_status
773 set_extra_library_path (const char *path)
774 {
775 ASSERT (called_plugin);
776 ldfile_add_library_path (xstrdup (path), FALSE);
777 return LDPS_OK;
778 }
779
780 /* Issue a diagnostic message from a plugin. */
781 static enum ld_plugin_status
782 message (int level, const char *format, ...)
783 {
784 va_list args;
785 va_start (args, format);
786
787 switch (level)
788 {
789 case LDPL_INFO:
790 vfinfo (stdout, format, args, FALSE);
791 putchar ('\n');
792 break;
793 case LDPL_WARNING:
794 vfinfo (stdout, format, args, TRUE);
795 putchar ('\n');
796 break;
797 case LDPL_FATAL:
798 case LDPL_ERROR:
799 default:
800 {
801 char *newfmt = ACONCAT ((level == LDPL_FATAL ? "%P%F: " : "%P%X: ",
802 format, "\n", (const char *) NULL));
803 fflush (stdout);
804 vfinfo (stderr, newfmt, args, TRUE);
805 fflush (stderr);
806 }
807 break;
808 }
809
810 va_end (args);
811 return LDPS_OK;
812 }
813
814 /* Helper to size leading part of tv array and set it up. */
815 static void
816 set_tv_header (struct ld_plugin_tv *tv)
817 {
818 size_t i;
819
820 /* Version info. */
821 static const unsigned int major = (unsigned)(BFD_VERSION / 100000000UL);
822 static const unsigned int minor = (unsigned)(BFD_VERSION / 1000000UL) % 100;
823
824 for (i = 0; i < tv_header_size; i++)
825 {
826 tv[i].tv_tag = tv_header_tags[i];
827 #define TVU(x) tv[i].tv_u.tv_ ## x
828 switch (tv[i].tv_tag)
829 {
830 case LDPT_MESSAGE:
831 TVU(message) = message;
832 break;
833 case LDPT_API_VERSION:
834 TVU(val) = LD_PLUGIN_API_VERSION;
835 break;
836 case LDPT_GNU_LD_VERSION:
837 TVU(val) = major * 100 + minor;
838 break;
839 case LDPT_LINKER_OUTPUT:
840 TVU(val) = (link_info.relocatable
841 ? LDPO_REL
842 : (link_info.executable
843 ? (link_info.pie ? LDPO_PIE : LDPO_EXEC)
844 : LDPO_DYN));
845 break;
846 case LDPT_OUTPUT_NAME:
847 TVU(string) = output_filename;
848 break;
849 case LDPT_REGISTER_CLAIM_FILE_HOOK:
850 TVU(register_claim_file) = register_claim_file;
851 break;
852 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
853 TVU(register_all_symbols_read) = register_all_symbols_read;
854 break;
855 case LDPT_REGISTER_CLEANUP_HOOK:
856 TVU(register_cleanup) = register_cleanup;
857 break;
858 case LDPT_ADD_SYMBOLS:
859 TVU(add_symbols) = add_symbols;
860 break;
861 case LDPT_GET_INPUT_FILE:
862 TVU(get_input_file) = get_input_file;
863 break;
864 case LDPT_GET_VIEW:
865 TVU(get_view) = get_view;
866 break;
867 case LDPT_RELEASE_INPUT_FILE:
868 TVU(release_input_file) = release_input_file;
869 break;
870 case LDPT_GET_SYMBOLS:
871 TVU(get_symbols) = get_symbols_v1;
872 break;
873 case LDPT_GET_SYMBOLS_V2:
874 TVU(get_symbols) = get_symbols_v2;
875 break;
876 case LDPT_ADD_INPUT_FILE:
877 TVU(add_input_file) = add_input_file;
878 break;
879 case LDPT_ADD_INPUT_LIBRARY:
880 TVU(add_input_library) = add_input_library;
881 break;
882 case LDPT_SET_EXTRA_LIBRARY_PATH:
883 TVU(set_extra_library_path) = set_extra_library_path;
884 break;
885 default:
886 /* Added a new entry to the array without adding
887 a new case to set up its value is a bug. */
888 FAIL ();
889 }
890 #undef TVU
891 }
892 }
893
894 /* Append the per-plugin args list and trailing LDPT_NULL to tv. */
895 static void
896 set_tv_plugin_args (plugin_t *plugin, struct ld_plugin_tv *tv)
897 {
898 plugin_arg_t *arg = plugin->args;
899 while (arg)
900 {
901 tv->tv_tag = LDPT_OPTION;
902 tv->tv_u.tv_string = arg->arg;
903 arg = arg->next;
904 tv++;
905 }
906 tv->tv_tag = LDPT_NULL;
907 tv->tv_u.tv_val = 0;
908 }
909
910 /* Load up and initialise all plugins after argument parsing. */
911 void
912 plugin_load_plugins (void)
913 {
914 struct ld_plugin_tv *my_tv;
915 unsigned int max_args = 0;
916 plugin_t *curplug = plugins_list;
917
918 /* If there are no plugins, we need do nothing this run. */
919 if (!curplug)
920 return;
921
922 /* First pass over plugins to find max # args needed so that we
923 can size and allocate the tv array. */
924 while (curplug)
925 {
926 if (curplug->n_args > max_args)
927 max_args = curplug->n_args;
928 curplug = curplug->next;
929 }
930
931 /* Allocate tv array and initialise constant part. */
932 my_tv = xmalloc ((max_args + 1 + tv_header_size) * sizeof *my_tv);
933 set_tv_header (my_tv);
934
935 /* Pass over plugins again, activating them. */
936 curplug = plugins_list;
937 while (curplug)
938 {
939 enum ld_plugin_status rv;
940 ld_plugin_onload onloadfn;
941
942 onloadfn = (ld_plugin_onload) dlsym (curplug->dlhandle, "onload");
943 if (!onloadfn)
944 onloadfn = (ld_plugin_onload) dlsym (curplug->dlhandle, "_onload");
945 if (!onloadfn)
946 einfo (_("%P%F: %s: error loading plugin: %s\n"),
947 curplug->name, dlerror ());
948 set_tv_plugin_args (curplug, &my_tv[tv_header_size]);
949 called_plugin = curplug;
950 rv = (*onloadfn) (my_tv);
951 called_plugin = NULL;
952 if (rv != LDPS_OK)
953 einfo (_("%P%F: %s: plugin error: %d\n"), curplug->name, rv);
954 curplug = curplug->next;
955 }
956
957 /* Since plugin(s) inited ok, assume they're going to want symbol
958 resolutions, which needs us to track which symbols are referenced
959 by non-IR files using the linker's notice callback. */
960 orig_notice_all = link_info.notice_all;
961 orig_callbacks = link_info.callbacks;
962 plugin_callbacks = *orig_callbacks;
963 plugin_callbacks.notice = &plugin_notice;
964 link_info.notice_all = TRUE;
965 link_info.lto_plugin_active = TRUE;
966 link_info.callbacks = &plugin_callbacks;
967 }
968
969 /* Call 'claim file' hook for all plugins. */
970 static int
971 plugin_call_claim_file (const struct ld_plugin_input_file *file, int *claimed)
972 {
973 plugin_t *curplug = plugins_list;
974 *claimed = FALSE;
975 if (no_more_claiming)
976 return 0;
977 while (curplug && !*claimed)
978 {
979 if (curplug->claim_file_handler)
980 {
981 enum ld_plugin_status rv;
982 called_plugin = curplug;
983 rv = (*curplug->claim_file_handler) (file, claimed);
984 called_plugin = NULL;
985 if (rv != LDPS_OK)
986 set_plugin_error (curplug->name);
987 }
988 curplug = curplug->next;
989 }
990 return plugin_error_p () ? -1 : 0;
991 }
992
993 /* Duplicates a character string with memory attached to ABFD. */
994
995 static char *
996 plugin_strdup (bfd *abfd, const char *str)
997 {
998 size_t strlength;
999 char *copy;
1000 strlength = strlen (str) + 1;
1001 copy = bfd_alloc (abfd, strlength);
1002 if (copy == NULL)
1003 einfo (_("%P%F: plugin_strdup failed to allocate memory: %s\n"),
1004 bfd_get_error ());
1005 memcpy (copy, str, strlength);
1006 return copy;
1007 }
1008
1009 void
1010 plugin_maybe_claim (lang_input_statement_type *entry)
1011 {
1012 int claimed = 0;
1013 plugin_input_file_t *input;
1014 off_t offset, filesize;
1015 struct ld_plugin_input_file file;
1016 bfd *abfd;
1017 bfd *ibfd = entry->the_bfd;
1018 bfd_boolean inarchive = bfd_my_archive (ibfd) != NULL;
1019 const char *name
1020 = inarchive ? bfd_my_archive (ibfd)->filename : ibfd->filename;
1021 int fd = open (name, O_RDONLY | O_BINARY);
1022
1023 if (fd < 0)
1024 return;
1025
1026 /* We create a dummy BFD, initially empty, to house whatever symbols
1027 the plugin may want to add. */
1028 abfd = plugin_get_ir_dummy_bfd (ibfd->filename, ibfd);
1029
1030 input = bfd_alloc (abfd, sizeof (*input));
1031 if (input == NULL)
1032 einfo (_("%P%F: plugin failed to allocate memory for input: %s\n"),
1033 bfd_get_error ());
1034
1035 if (inarchive)
1036 {
1037 /* Offset and filesize must refer to the individual archive
1038 member, not the whole file, and must exclude the header.
1039 Fortunately for us, that is how the data is stored in the
1040 origin field of the bfd and in the arelt_data. */
1041 offset = ibfd->origin;
1042 filesize = arelt_size (ibfd);
1043 }
1044 else
1045 {
1046 offset = 0;
1047 filesize = lseek (fd, 0, SEEK_END);
1048
1049 /* We must copy filename attached to ibfd if it is not an archive
1050 member since it may be freed by bfd_close below. */
1051 name = plugin_strdup (abfd, name);
1052 }
1053
1054 file.name = name;
1055 file.offset = offset;
1056 file.filesize = filesize;
1057 file.fd = fd;
1058 file.handle = input;
1059
1060 input->abfd = abfd;
1061 input->view_buffer.addr = NULL;
1062 input->view_buffer.filesize = 0;
1063 input->view_buffer.offset = 0;
1064 input->fd = fd;
1065 input->offset = offset;
1066 input->filesize = filesize;
1067 input->name = plugin_strdup (abfd, ibfd->filename);
1068
1069 if (plugin_call_claim_file (&file, &claimed))
1070 einfo (_("%P%F: %s: plugin reported error claiming file\n"),
1071 plugin_error_plugin ());
1072
1073 if (input->fd != -1 && ibfd->format == bfd_object)
1074 {
1075 /* FIXME: fd belongs to us, not the plugin. IR for GCC plugin,
1076 which doesn't need fd after plugin_call_claim_file, is
1077 stored in bfd_object file. Since GCC plugin before GCC 5
1078 doesn't call release_input_file, we close it here. IR for
1079 LLVM plugin, which needs fd after plugin_call_claim_file and
1080 calls release_input_file after it is done, is stored in
1081 non-bfd_object file. This scheme doesn't work when a plugin
1082 needs fd and its IR is stored in bfd_object file. */
1083 close (fd);
1084 input->fd = -1;
1085 }
1086
1087 if (claimed)
1088 {
1089 /* Discard the real file's BFD and substitute the dummy one. */
1090
1091 /* BFD archive handling caches elements so we can't call
1092 bfd_close for archives. */
1093 if (!inarchive)
1094 bfd_close (ibfd);
1095 bfd_make_readable (abfd);
1096 entry->the_bfd = abfd;
1097 entry->flags.claimed = TRUE;
1098 }
1099 else
1100 {
1101 /* If plugin didn't claim the file, we don't need the dummy bfd.
1102 Can't avoid speculatively creating it, alas. */
1103 bfd_close_all_done (abfd);
1104 entry->flags.claimed = FALSE;
1105 }
1106 }
1107
1108 /* Call 'all symbols read' hook for all plugins. */
1109 int
1110 plugin_call_all_symbols_read (void)
1111 {
1112 plugin_t *curplug = plugins_list;
1113
1114 /* Disable any further file-claiming. */
1115 no_more_claiming = TRUE;
1116
1117 while (curplug)
1118 {
1119 if (curplug->all_symbols_read_handler)
1120 {
1121 enum ld_plugin_status rv;
1122 called_plugin = curplug;
1123 rv = (*curplug->all_symbols_read_handler) ();
1124 called_plugin = NULL;
1125 if (rv != LDPS_OK)
1126 set_plugin_error (curplug->name);
1127 }
1128 curplug = curplug->next;
1129 }
1130 return plugin_error_p () ? -1 : 0;
1131 }
1132
1133 /* Call 'cleanup' hook for all plugins at exit. */
1134 void
1135 plugin_call_cleanup (void)
1136 {
1137 plugin_t *curplug = plugins_list;
1138 while (curplug)
1139 {
1140 if (curplug->cleanup_handler && !curplug->cleanup_done)
1141 {
1142 enum ld_plugin_status rv;
1143 curplug->cleanup_done = TRUE;
1144 called_plugin = curplug;
1145 rv = (*curplug->cleanup_handler) ();
1146 called_plugin = NULL;
1147 if (rv != LDPS_OK)
1148 info_msg (_("%P: %s: error in plugin cleanup: %d (ignored)\n"),
1149 curplug->name, rv);
1150 dlclose (curplug->dlhandle);
1151 }
1152 curplug = curplug->next;
1153 }
1154 }
1155
1156 /* To determine which symbols should be resolved LDPR_PREVAILING_DEF
1157 and which LDPR_PREVAILING_DEF_IRONLY, we notice all the symbols as
1158 the linker adds them to the linker hash table. Mark those
1159 referenced from a non-IR file with non_ir_ref. We have to
1160 notice_all symbols, because we won't necessarily know until later
1161 which ones will be contributed by IR files. */
1162 static bfd_boolean
1163 plugin_notice (struct bfd_link_info *info,
1164 struct bfd_link_hash_entry *h,
1165 struct bfd_link_hash_entry *inh,
1166 bfd *abfd,
1167 asection *section,
1168 bfd_vma value,
1169 flagword flags)
1170 {
1171 struct bfd_link_hash_entry *orig_h = h;
1172
1173 if (h != NULL)
1174 {
1175 bfd *sym_bfd;
1176
1177 if (h->type == bfd_link_hash_warning)
1178 h = h->u.i.link;
1179
1180 /* Nothing to do here if this def/ref is from an IR dummy BFD. */
1181 if (is_ir_dummy_bfd (abfd))
1182 ;
1183
1184 /* Making an indirect symbol counts as a reference unless this
1185 is a brand new symbol. */
1186 else if (bfd_is_ind_section (section)
1187 || (flags & BSF_INDIRECT) != 0)
1188 {
1189 /* ??? Some of this is questionable. See comments in
1190 _bfd_generic_link_add_one_symbol for case IND. */
1191 if (h->type != bfd_link_hash_new)
1192 {
1193 h->non_ir_ref = TRUE;
1194 inh->non_ir_ref = TRUE;
1195 }
1196 else if (inh->type == bfd_link_hash_new)
1197 inh->non_ir_ref = TRUE;
1198 }
1199
1200 /* Nothing to do here for warning symbols. */
1201 else if ((flags & BSF_WARNING) != 0)
1202 ;
1203
1204 /* Nothing to do here for constructor symbols. */
1205 else if ((flags & BSF_CONSTRUCTOR) != 0)
1206 ;
1207
1208 /* If this is a ref, set non_ir_ref. */
1209 else if (bfd_is_und_section (section))
1210 {
1211 /* Replace the undefined dummy bfd with the real one. */
1212 if ((h->type == bfd_link_hash_undefined
1213 || h->type == bfd_link_hash_undefweak)
1214 && (h->u.undef.abfd == NULL
1215 || (h->u.undef.abfd->flags & BFD_PLUGIN) != 0))
1216 h->u.undef.abfd = abfd;
1217 h->non_ir_ref = TRUE;
1218 }
1219
1220 /* Otherwise, it must be a new def. Ensure any symbol defined
1221 in an IR dummy BFD takes on a new value from a real BFD.
1222 Weak symbols are not normally overridden by a new weak
1223 definition, and strong symbols will normally cause multiple
1224 definition errors. Avoid this by making the symbol appear
1225 to be undefined. */
1226 else if (((h->type == bfd_link_hash_defweak
1227 || h->type == bfd_link_hash_defined)
1228 && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
1229 || (h->type == bfd_link_hash_common
1230 && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner)))
1231 {
1232 h->type = bfd_link_hash_undefweak;
1233 h->u.undef.abfd = sym_bfd;
1234 }
1235 }
1236
1237 /* Continue with cref/nocrossref/trace-sym processing. */
1238 if (orig_h == NULL
1239 || orig_notice_all
1240 || (info->notice_hash != NULL
1241 && bfd_hash_lookup (info->notice_hash, orig_h->root.string,
1242 FALSE, FALSE) != NULL))
1243 return (*orig_callbacks->notice) (info, orig_h, inh,
1244 abfd, section, value, flags);
1245 return TRUE;
1246 }
This page took 0.05507 seconds and 4 git commands to generate.