1 /* Plugin control for the GNU linker.
2 Copyright (C) 2010-2015 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
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.
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.
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. */
22 #include "libiberty.h"
34 #include "plugin-api.h"
37 # include <sys/mman.h>
39 # define MAP_FAILED ((void *) -1)
45 # define MAP_PRIVATE 0
49 #if !(defined(errno) || defined(_MSC_VER) && defined(_INC_ERRNO))
52 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
56 /* Report plugin symbols. */
57 bfd_boolean report_plugin_symbols
;
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)"
64 /* Stores a single argument passed to a plugin. */
65 typedef struct plugin_arg
67 struct plugin_arg
*next
;
71 /* Holds all details of a single plugin. */
74 /* Next on the list of plugins, or NULL at end of chain. */
76 /* The argument string given to --plugin. */
78 /* The shared library handle returned by dlopen. */
80 /* The list of argument string given to --plugin-opt. */
82 /* Number of args in the list, for convenience. */
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
;
92 typedef struct view_buffer
99 /* The internal version of struct ld_plugin_input_file with a BFD
101 typedef struct plugin_input_file
104 view_buffer_t view_buffer
;
109 } plugin_input_file_t
;
111 /* The master list of all plugins. */
112 static plugin_t
*plugins_list
= NULL
;
114 /* We keep a tail pointer for easy linking on the end. */
115 static plugin_t
**plugins_tail_chain_ptr
= &plugins_list
;
117 /* The last plugin added to the list, for receiving args. */
118 static plugin_t
*last_plugin
= NULL
;
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
;
123 /* The plugin which is currently having a callback executed. */
124 static plugin_t
*called_plugin
= NULL
;
126 /* Last plugin to cause an error, if any. */
127 static const char *error_plugin
= NULL
;
129 /* State of linker "notice" interface before we poked at it. */
130 static bfd_boolean orig_notice_all
;
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
;
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
;
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
[] =
148 LDPT_REGISTER_CLAIM_FILE_HOOK
,
149 LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK
,
150 LDPT_REGISTER_CLEANUP_HOOK
,
154 LDPT_RELEASE_INPUT_FILE
,
158 LDPT_ADD_INPUT_LIBRARY
,
159 LDPT_SET_EXTRA_LIBRARY_PATH
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
);
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
);
171 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
173 #define RTLD_NOW 0 /* Dummy value. */
176 dlopen (const char *file
, int mode ATTRIBUTE_UNUSED
)
178 return LoadLibrary (file
);
182 dlsym (void *handle
, const char *name
)
184 return GetProcAddress (handle
, name
);
188 dlclose (void *handle
)
190 FreeLibrary (handle
);
194 #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */
204 /* Helper function for exiting with error status. */
206 set_plugin_error (const char *plugin
)
208 error_plugin
= plugin
;
212 /* Test if an error occurred. */
214 plugin_error_p (void)
216 return error_plugin
!= NULL
;
219 /* Return name of plugin which caused an error if any. */
221 plugin_error_plugin (void)
223 return error_plugin
? error_plugin
: _("<no plugin>");
226 /* Handle -plugin arg: find and load plugin, or return error. */
228 plugin_opt_plugin (const char *plugin
)
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 ());
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
;
243 /* Record it as current plugin for receiving args. */
244 last_plugin
= newplug
;
245 last_plugin_args_tail_chain_ptr
= &newplug
->args
;
248 /* Accumulate option arguments for last-loaded plugin, or return
251 plugin_opt_plugin_arg (const char *arg
)
253 plugin_arg_t
*newarg
;
256 return set_plugin_error (_("<no plugin>"));
258 /* Ignore -pass-through= from GCC driver. */
261 const char *p
= arg
+ 1;
265 if (strncmp (p
, "pass-through=", 13) == 0)
269 newarg
= xmalloc (sizeof *newarg
);
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
++;
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. */
286 plugin_get_ir_dummy_bfd (const char *name
, bfd
*srctemplate
)
290 bfd_use_reserved_id
= 1;
291 abfd
= bfd_create (concat (name
, IRONLY_SUFFIX
, (const char *) NULL
),
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
))
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
))
310 einfo (_("could not create dummy IR bfd: %F%E\n"));
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
)
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;
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
)
328 flagword flags
= BSF_NO_FLAGS
;
329 struct bfd_section
*section
;
331 asym
->the_bfd
= abfd
;
332 asym
->name
= (ldsym
->version
333 ? concat (ldsym
->name
, "@", ldsym
->version
, (const char *) NULL
)
343 if (ldsym
->comdat_key
)
345 char *name
= concat (".gnu.linkonce.t.", ldsym
->comdat_key
,
346 (const char *) NULL
);
347 section
= bfd_get_section_by_name (abfd
, name
);
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
);
363 section
= bfd_get_section_by_name (abfd
, ".text");
370 section
= bfd_und_section_ptr
;
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
)
380 ((elf_symbol_type
*) asym
)->internal_elf_sym
.st_shndx
= SHN_COMMON
;
381 ((elf_symbol_type
*) asym
)->internal_elf_sym
.st_value
= 1;
389 asym
->section
= section
;
391 /* Visibility only applies on ELF targets. */
392 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
394 elf_symbol_type
*elfsym
= elf_symbol_from (abfd
, asym
);
395 unsigned char visibility
;
398 einfo (_("%P%F: %s: non-ELF symbol in ELF BFD!\n"), asym
->name
);
399 switch (ldsym
->visibility
)
402 einfo (_("%P%F: unknown ELF symbol visibility: %d!\n"),
405 visibility
= STV_DEFAULT
;
408 visibility
= STV_PROTECTED
;
411 visibility
= STV_INTERNAL
;
414 visibility
= STV_HIDDEN
;
417 elfsym
->internal_elf_sym
.st_other
418 = (visibility
| (elfsym
->internal_elf_sym
.st_other
419 & ~ELF_ST_VISIBILITY (-1)));
425 /* Register a claim-file handler. */
426 static enum ld_plugin_status
427 register_claim_file (ld_plugin_claim_file_handler handler
)
429 ASSERT (called_plugin
);
430 called_plugin
->claim_file_handler
= handler
;
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
)
438 ASSERT (called_plugin
);
439 called_plugin
->all_symbols_read_handler
= handler
;
443 /* Register a cleanup handler. */
444 static enum ld_plugin_status
445 register_cleanup (ld_plugin_cleanup_handler handler
)
447 ASSERT (called_plugin
);
448 called_plugin
->cleanup_handler
= handler
;
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
)
457 plugin_input_file_t
*input
= handle
;
458 bfd
*abfd
= input
->abfd
;
461 ASSERT (called_plugin
);
462 symptrs
= xmalloc (nsyms
* sizeof *symptrs
);
463 for (n
= 0; n
< nsyms
; n
++)
465 enum ld_plugin_status rv
;
468 bfdsym
= bfd_make_empty_symbol (abfd
);
470 rv
= asymbol_from_plugin_symbol (abfd
, bfdsym
, syms
+ n
);
474 bfd_set_symtab (abfd
, symptrs
, nsyms
);
478 /* Get the input file information with an open (possibly re-opened)
480 static enum ld_plugin_status
481 get_input_file (const void *handle
, struct ld_plugin_input_file
*file
)
483 const plugin_input_file_t
*input
= handle
;
485 ASSERT (called_plugin
);
487 file
->name
= input
->name
;
488 file
->offset
= input
->offset
;
489 file
->filesize
= input
->filesize
;
490 file
->handle
= (void *) handle
;
495 /* Get view of the input file. */
496 static enum ld_plugin_status
497 get_view (const void *handle
, const void **viewp
)
499 plugin_input_file_t
*input
= (plugin_input_file_t
*) handle
;
501 size_t size
= input
->filesize
;
506 ASSERT (called_plugin
);
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
);
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
)
518 *viewp
= input
->view_buffer
.addr
;
522 input
->view_buffer
.filesize
= size
;
523 input
->view_buffer
.offset
= input
->offset
;
526 # if HAVE_GETPAGESIZE
527 bias
= input
->offset
% getpagesize ();;
528 offset
= input
->offset
- bias
;
531 buffer
= mmap (NULL
, size
, PROT_READ
, MAP_PRIVATE
, input
->fd
, offset
);
532 if (buffer
!= MAP_FAILED
)
539 if (lseek (input
->fd
, input
->offset
, SEEK_SET
) < 0)
542 buffer
= bfd_alloc (input
->abfd
, size
);
549 ssize_t got
= read (input
->fd
, p
, size
);
557 else if (errno
!= EINTR
)
563 input
->view_buffer
.addr
= buffer
;
569 /* Release the input file. */
570 static enum ld_plugin_status
571 release_input_file (const void *handle
)
573 plugin_input_file_t
*input
= (plugin_input_file_t
*) handle
;
574 ASSERT (called_plugin
);
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
)
589 struct bfd_sym_chain
*sym
;
591 if (link_info
.relocatable
)
593 if (link_info
.export_dynamic
|| !link_info
.executable
)
595 /* Check if symbol is hidden by version script. */
596 if (bfd_hide_sym_by_version (link_info
.version_info
,
599 /* Only ELF symbols really have visibility. */
600 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
)
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
;
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
);
620 for (sym
= &entry_symbol
; sym
!= NULL
; sym
= sym
->next
)
622 && strcmp (sym
->name
, blhe
->root
.string
) == 0)
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
,
633 const plugin_input_file_t
*input
= handle
;
634 const bfd
*abfd
= (const bfd
*) input
->abfd
;
637 ASSERT (called_plugin
);
638 for (n
= 0; n
< nsyms
; n
++)
640 struct bfd_link_hash_entry
*blhe
;
644 if (syms
[n
].def
!= LDPK_UNDEF
)
645 blhe
= bfd_link_hash_lookup (link_info
.hash
, syms
[n
].name
,
648 blhe
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
, &link_info
,
649 syms
[n
].name
, FALSE
, FALSE
, TRUE
);
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
)
663 if (blhe
->type
!= bfd_link_hash_defined
664 && blhe
->type
!= bfd_link_hash_defweak
665 && blhe
->type
!= bfd_link_hash_common
)
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
);
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
);
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
)
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
;
696 res
= LDPR_RESOLVED_EXEC
;
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
;
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
;
711 res
= LDPR_PREEMPTED_REG
;
713 if (res
== LDPR_PREVAILING_DEF_IRONLY
)
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
;
726 syms
[n
].resolution
= res
;
727 if (report_plugin_symbols
)
728 einfo (_("%P: %B: symbol `%s' "
729 "definition: %d, visibility: %d, resolution: %d\n"),
731 syms
[n
].def
, syms
[n
].visibility
, res
);
736 static enum ld_plugin_status
737 get_symbols_v1 (const void *handle
, int nsyms
, struct ld_plugin_symbol
*syms
)
739 return get_symbols (handle
, nsyms
, syms
, LDPR_PREVAILING_DEF
);
742 static enum ld_plugin_status
743 get_symbols_v2 (const void *handle
, int nsyms
, struct ld_plugin_symbol
*syms
)
745 return get_symbols (handle
, nsyms
, syms
, LDPR_PREVAILING_DEF_IRONLY_EXP
);
748 /* Add a new (real) input file generated by a plugin. */
749 static enum ld_plugin_status
750 add_input_file (const char *pathname
)
752 ASSERT (called_plugin
);
753 if (!lang_add_input_file (xstrdup (pathname
), lang_input_file_is_file_enum
,
759 /* Add a new (real) library required by a plugin. */
760 static enum ld_plugin_status
761 add_input_library (const char *pathname
)
763 ASSERT (called_plugin
);
764 if (!lang_add_input_file (xstrdup (pathname
), lang_input_file_is_l_enum
,
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
)
775 ASSERT (called_plugin
);
776 ldfile_add_library_path (xstrdup (path
), FALSE
);
780 /* Issue a diagnostic message from a plugin. */
781 static enum ld_plugin_status
782 message (int level
, const char *format
, ...)
785 va_start (args
, format
);
790 vfinfo (stdout
, format
, args
, FALSE
);
794 vfinfo (stdout
, format
, args
, TRUE
);
801 char *newfmt
= ACONCAT ((level
== LDPL_FATAL
? "%P%F: " : "%P%X: ",
802 format
, "\n", (const char *) NULL
));
804 vfinfo (stderr
, newfmt
, args
, TRUE
);
814 /* Helper to size leading part of tv array and set it up. */
816 set_tv_header (struct ld_plugin_tv
*tv
)
821 static const unsigned int major
= (unsigned)(BFD_VERSION
/ 100000000UL);
822 static const unsigned int minor
= (unsigned)(BFD_VERSION
/ 1000000UL) % 100;
824 for (i
= 0; i
< tv_header_size
; i
++)
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
)
831 TVU(message
) = message
;
833 case LDPT_API_VERSION
:
834 TVU(val
) = LD_PLUGIN_API_VERSION
;
836 case LDPT_GNU_LD_VERSION
:
837 TVU(val
) = major
* 100 + minor
;
839 case LDPT_LINKER_OUTPUT
:
840 TVU(val
) = (link_info
.relocatable
842 : (link_info
.executable
843 ? (link_info
.pie
? LDPO_PIE
: LDPO_EXEC
)
846 case LDPT_OUTPUT_NAME
:
847 TVU(string
) = output_filename
;
849 case LDPT_REGISTER_CLAIM_FILE_HOOK
:
850 TVU(register_claim_file
) = register_claim_file
;
852 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK
:
853 TVU(register_all_symbols_read
) = register_all_symbols_read
;
855 case LDPT_REGISTER_CLEANUP_HOOK
:
856 TVU(register_cleanup
) = register_cleanup
;
858 case LDPT_ADD_SYMBOLS
:
859 TVU(add_symbols
) = add_symbols
;
861 case LDPT_GET_INPUT_FILE
:
862 TVU(get_input_file
) = get_input_file
;
865 TVU(get_view
) = get_view
;
867 case LDPT_RELEASE_INPUT_FILE
:
868 TVU(release_input_file
) = release_input_file
;
870 case LDPT_GET_SYMBOLS
:
871 TVU(get_symbols
) = get_symbols_v1
;
873 case LDPT_GET_SYMBOLS_V2
:
874 TVU(get_symbols
) = get_symbols_v2
;
876 case LDPT_ADD_INPUT_FILE
:
877 TVU(add_input_file
) = add_input_file
;
879 case LDPT_ADD_INPUT_LIBRARY
:
880 TVU(add_input_library
) = add_input_library
;
882 case LDPT_SET_EXTRA_LIBRARY_PATH
:
883 TVU(set_extra_library_path
) = set_extra_library_path
;
886 /* Added a new entry to the array without adding
887 a new case to set up its value is a bug. */
894 /* Append the per-plugin args list and trailing LDPT_NULL to tv. */
896 set_tv_plugin_args (plugin_t
*plugin
, struct ld_plugin_tv
*tv
)
898 plugin_arg_t
*arg
= plugin
->args
;
901 tv
->tv_tag
= LDPT_OPTION
;
902 tv
->tv_u
.tv_string
= arg
->arg
;
906 tv
->tv_tag
= LDPT_NULL
;
910 /* Load up and initialise all plugins after argument parsing. */
912 plugin_load_plugins (void)
914 struct ld_plugin_tv
*my_tv
;
915 unsigned int max_args
= 0;
916 plugin_t
*curplug
= plugins_list
;
918 /* If there are no plugins, we need do nothing this run. */
922 /* First pass over plugins to find max # args needed so that we
923 can size and allocate the tv array. */
926 if (curplug
->n_args
> max_args
)
927 max_args
= curplug
->n_args
;
928 curplug
= curplug
->next
;
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
);
935 /* Pass over plugins again, activating them. */
936 curplug
= plugins_list
;
939 enum ld_plugin_status rv
;
940 ld_plugin_onload onloadfn
;
942 onloadfn
= (ld_plugin_onload
) dlsym (curplug
->dlhandle
, "onload");
944 onloadfn
= (ld_plugin_onload
) dlsym (curplug
->dlhandle
, "_onload");
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
;
953 einfo (_("%P%F: %s: plugin error: %d\n"), curplug
->name
, rv
);
954 curplug
= curplug
->next
;
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
;
969 /* Call 'claim file' hook for all plugins. */
971 plugin_call_claim_file (const struct ld_plugin_input_file
*file
, int *claimed
)
973 plugin_t
*curplug
= plugins_list
;
975 if (no_more_claiming
)
977 while (curplug
&& !*claimed
)
979 if (curplug
->claim_file_handler
)
981 enum ld_plugin_status rv
;
982 called_plugin
= curplug
;
983 rv
= (*curplug
->claim_file_handler
) (file
, claimed
);
984 called_plugin
= NULL
;
986 set_plugin_error (curplug
->name
);
988 curplug
= curplug
->next
;
990 return plugin_error_p () ? -1 : 0;
993 /* Duplicates a character string with memory attached to ABFD. */
996 plugin_strdup (bfd
*abfd
, const char *str
)
1000 strlength
= strlen (str
) + 1;
1001 copy
= bfd_alloc (abfd
, strlength
);
1003 einfo (_("%P%F: plugin_strdup failed to allocate memory: %s\n"),
1005 memcpy (copy
, str
, strlength
);
1010 plugin_maybe_claim (lang_input_statement_type
*entry
)
1013 plugin_input_file_t
*input
;
1014 off_t offset
, filesize
;
1015 struct ld_plugin_input_file file
;
1017 bfd
*ibfd
= entry
->the_bfd
;
1018 bfd_boolean inarchive
= bfd_my_archive (ibfd
) != NULL
;
1020 = inarchive
? bfd_my_archive (ibfd
)->filename
: ibfd
->filename
;
1021 int fd
= open (name
, O_RDONLY
| O_BINARY
);
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
);
1030 input
= bfd_alloc (abfd
, sizeof (*input
));
1032 einfo (_("%P%F: plugin failed to allocate memory for input: %s\n"),
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
);
1047 filesize
= lseek (fd
, 0, SEEK_END
);
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
);
1055 file
.offset
= offset
;
1056 file
.filesize
= filesize
;
1058 file
.handle
= input
;
1061 input
->view_buffer
.addr
= NULL
;
1062 input
->view_buffer
.filesize
= 0;
1063 input
->view_buffer
.offset
= 0;
1065 input
->offset
= offset
;
1066 input
->filesize
= filesize
;
1067 input
->name
= plugin_strdup (abfd
, ibfd
->filename
);
1069 if (plugin_call_claim_file (&file
, &claimed
))
1070 einfo (_("%P%F: %s: plugin reported error claiming file\n"),
1071 plugin_error_plugin ());
1073 if (input
->fd
!= -1 && ibfd
->format
== bfd_object
)
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. */
1089 /* Discard the real file's BFD and substitute the dummy one. */
1091 /* BFD archive handling caches elements so we can't call
1092 bfd_close for archives. */
1095 bfd_make_readable (abfd
);
1096 entry
->the_bfd
= abfd
;
1097 entry
->flags
.claimed
= TRUE
;
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
;
1108 /* Call 'all symbols read' hook for all plugins. */
1110 plugin_call_all_symbols_read (void)
1112 plugin_t
*curplug
= plugins_list
;
1114 /* Disable any further file-claiming. */
1115 no_more_claiming
= TRUE
;
1119 if (curplug
->all_symbols_read_handler
)
1121 enum ld_plugin_status rv
;
1122 called_plugin
= curplug
;
1123 rv
= (*curplug
->all_symbols_read_handler
) ();
1124 called_plugin
= NULL
;
1126 set_plugin_error (curplug
->name
);
1128 curplug
= curplug
->next
;
1130 return plugin_error_p () ? -1 : 0;
1133 /* Call 'cleanup' hook for all plugins at exit. */
1135 plugin_call_cleanup (void)
1137 plugin_t
*curplug
= plugins_list
;
1140 if (curplug
->cleanup_handler
&& !curplug
->cleanup_done
)
1142 enum ld_plugin_status rv
;
1143 curplug
->cleanup_done
= TRUE
;
1144 called_plugin
= curplug
;
1145 rv
= (*curplug
->cleanup_handler
) ();
1146 called_plugin
= NULL
;
1148 info_msg (_("%P: %s: error in plugin cleanup: %d (ignored)\n"),
1150 dlclose (curplug
->dlhandle
);
1152 curplug
= curplug
->next
;
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. */
1163 plugin_notice (struct bfd_link_info
*info
,
1164 struct bfd_link_hash_entry
*h
,
1165 struct bfd_link_hash_entry
*inh
,
1171 struct bfd_link_hash_entry
*orig_h
= h
;
1177 if (h
->type
== bfd_link_hash_warning
)
1180 /* Nothing to do here if this def/ref is from an IR dummy BFD. */
1181 if (is_ir_dummy_bfd (abfd
))
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)
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
)
1193 h
->non_ir_ref
= TRUE
;
1194 inh
->non_ir_ref
= TRUE
;
1196 else if (inh
->type
== bfd_link_hash_new
)
1197 inh
->non_ir_ref
= TRUE
;
1200 /* Nothing to do here for warning symbols. */
1201 else if ((flags
& BSF_WARNING
) != 0)
1204 /* Nothing to do here for constructor symbols. */
1205 else if ((flags
& BSF_CONSTRUCTOR
) != 0)
1208 /* If this is a ref, set non_ir_ref. */
1209 else if (bfd_is_und_section (section
))
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
;
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
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
)))
1232 h
->type
= bfd_link_hash_undefweak
;
1233 h
->u
.undef
.abfd
= sym_bfd
;
1237 /* Continue with cref/nocrossref/trace-sym processing. */
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
);