1 /* Plugin support for BFD.
2 Copyright (C) 2009-2020 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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. */
24 #if BFD_SUPPORTS_PLUGINS
29 #elif defined (HAVE_WINDOWS_H)
32 #error Unknown how to handle dynamic-load-libraries.
35 #include "plugin-api.h"
38 #include "libiberty.h"
41 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
43 #define RTLD_NOW 0 /* Dummy value. */
46 dlopen (const char *file
, int mode ATTRIBUTE_UNUSED
)
48 return LoadLibrary (file
);
52 dlsym (void *handle
, const char *name
)
54 return GetProcAddress (handle
, name
);
57 static int ATTRIBUTE_UNUSED
58 dlclose (void *handle
)
67 return "Unable to load DLL.";
70 #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */
72 #define bfd_plugin_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
73 #define bfd_plugin_new_section_hook _bfd_generic_new_section_hook
74 #define bfd_plugin_get_section_contents _bfd_generic_get_section_contents
75 #define bfd_plugin_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
76 #define bfd_plugin_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
77 #define bfd_plugin_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
78 #define bfd_plugin_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
79 #define bfd_plugin_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
80 #define bfd_plugin_core_file_matches_executable_p generic_core_file_matches_executable_p
81 #define bfd_plugin_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
82 #define bfd_plugin_bfd_is_target_special_symbol _bfd_bool_bfd_asymbol_false
83 #define bfd_plugin_get_lineno _bfd_nosymbols_get_lineno
84 #define bfd_plugin_find_nearest_line _bfd_nosymbols_find_nearest_line
85 #define bfd_plugin_find_line _bfd_nosymbols_find_line
86 #define bfd_plugin_find_inliner_info _bfd_nosymbols_find_inliner_info
87 #define bfd_plugin_get_symbol_version_string _bfd_nosymbols_get_symbol_version_string
88 #define bfd_plugin_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
89 #define bfd_plugin_read_minisymbols _bfd_generic_read_minisymbols
90 #define bfd_plugin_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
91 #define bfd_plugin_set_arch_mach bfd_default_set_arch_mach
92 #define bfd_plugin_set_section_contents _bfd_generic_set_section_contents
93 #define bfd_plugin_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
94 #define bfd_plugin_bfd_relax_section bfd_generic_relax_section
95 #define bfd_plugin_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
96 #define bfd_plugin_bfd_link_add_symbols _bfd_generic_link_add_symbols
97 #define bfd_plugin_bfd_link_just_syms _bfd_generic_link_just_syms
98 #define bfd_plugin_bfd_final_link _bfd_generic_final_link
99 #define bfd_plugin_bfd_link_split_section _bfd_generic_link_split_section
100 #define bfd_plugin_bfd_gc_sections bfd_generic_gc_sections
101 #define bfd_plugin_bfd_lookup_section_flags bfd_generic_lookup_section_flags
102 #define bfd_plugin_bfd_merge_sections bfd_generic_merge_sections
103 #define bfd_plugin_bfd_is_group_section bfd_generic_is_group_section
104 #define bfd_plugin_bfd_group_name bfd_generic_group_name
105 #define bfd_plugin_bfd_discard_group bfd_generic_discard_group
106 #define bfd_plugin_section_already_linked _bfd_generic_section_already_linked
107 #define bfd_plugin_bfd_define_common_symbol bfd_generic_define_common_symbol
108 #define bfd_plugin_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
109 #define bfd_plugin_bfd_define_start_stop bfd_generic_define_start_stop
110 #define bfd_plugin_bfd_copy_link_hash_symbol_type _bfd_generic_copy_link_hash_symbol_type
111 #define bfd_plugin_bfd_link_check_relocs _bfd_generic_link_check_relocs
113 static enum ld_plugin_status
114 message (int level ATTRIBUTE_UNUSED
,
115 const char * format
, ...)
118 va_start (args
, format
);
119 printf ("bfd plugin: ");
120 vprintf (format
, args
);
126 struct plugin_list_entry
128 /* These must be initialized for each IR object with LTO wrapper. */
129 ld_plugin_claim_file_handler claim_file
;
130 ld_plugin_all_symbols_read_handler all_symbols_read
;
131 ld_plugin_all_symbols_read_handler cleanup_handler
;
132 char *resolution_file
;
133 char *resolution_option
;
138 const struct ld_plugin_symbol
*lto_syms
;
140 struct plugin_list_entry
*next
;
142 /* These can be reused for all IR objects. */
143 const char *plugin_name
;
147 bfd_boolean initialized
;
150 static const char *plugin_program_name
;
151 static int need_lto_wrapper_p
;
154 bfd_plugin_set_program_name (const char *program_name
,
155 int need_lto_wrapper
)
157 plugin_program_name
= program_name
;
158 need_lto_wrapper_p
= need_lto_wrapper
;
161 /* Use GCC LTO wrapper to covert LTO IR object to the real object. */
164 get_lto_wrapper (struct plugin_list_entry
*plugin
)
167 const char *real_name
;
168 const char *base_name
;
170 const char *target_start
= NULL
;
171 const char *target_end
= NULL
;
172 size_t target_length
= 0;
176 char dir_seperator
= '\0';
177 char *resolution_file
;
179 if (!need_lto_wrapper_p
)
182 if (plugin
->initialized
)
184 if (plugin
->lto_wrapper
)
186 resolution_file
= make_temp_file (".res");
189 plugin
->resolution_file
= resolution_file
;
190 plugin
->resolution_option
= concat ("-fresolution=",
191 resolution_file
, NULL
);
196 /* Something is wrong. Give up. */
198 free (plugin
->lto_wrapper
);
199 free (plugin
->gcc_env
);
201 plugin
->gcc_env
= NULL
;
202 plugin
->lto_wrapper
= NULL
;
209 plugin
->initialized
= TRUE
;
211 /* Check for PREFIX/libexec/gcc/TARGET/VERSION/liblto_plugin.so. */
212 real_name
= lrealpath (plugin
->plugin_name
);
213 base_name
= lbasename (real_name
);
215 /* The directory length in plugin pathname. */
216 length
= base_name
- real_name
;
218 /* Skip if there is no PREFIX. */
222 p
= (char *) real_name
+ length
- 1;
223 if (IS_DIR_SEPARATOR (*p
))
226 for (; p
!= real_name
; p
--)
227 if (IS_DIR_SEPARATOR (*p
))
234 target_start
= p
+ 1;
235 target_length
= target_end
- target_start
;
245 /* Skip if there is no TARGET nor PREFIX. */
246 if (!target_length
|| !dir_seperator
)
249 #ifdef HAVE_EXECUTABLE_SUFFIX
250 # define GCC_EXECUTABLE "gcc" EXECUTABLE_SUFFIX
251 # define LTO_WRAPPER_EXECUTABLE "lto-wrapper" EXECUTABLE_SUFFIX
253 # define GCC_EXECUTABLE "gcc"
254 # define LTO_WRAPPER_EXECUTABLE "lto-wrapper"
256 gcc_name
= bfd_malloc (length
+ target_length
257 + sizeof (GCC_EXECUTABLE
));
258 if (gcc_name
== NULL
)
260 memcpy (gcc_name
, real_name
, length
);
262 /* Get PREFIX/bin/. */
263 p
+= gcc_name
- real_name
;
264 memcpy (p
+ 1, "bin", 3);
265 p
[4] = dir_seperator
;
267 /* Try PREFIX/bin/TARGET-gcc first. */
268 memcpy (p
+ 5, target_start
, target_length
);
269 p
[5 + target_length
] = '-';
270 memcpy (p
+ 5 + target_length
+ 1, GCC_EXECUTABLE
,
271 sizeof (GCC_EXECUTABLE
));
272 if (stat (gcc_name
, &st
) != 0 || !S_ISREG (st
.st_mode
))
274 /* Then try PREFIX/bin/gcc. */
275 memcpy (p
+ 5, GCC_EXECUTABLE
, sizeof (GCC_EXECUTABLE
));
276 if (stat (gcc_name
, &st
) != 0 || !S_ISREG (st
.st_mode
))
283 /* lto-wrapper should be in the same directory with LTO plugin. */
284 wrapper_name
= bfd_malloc (length
+ sizeof (LTO_WRAPPER_EXECUTABLE
));
285 if (wrapper_name
== NULL
)
290 memcpy (wrapper_name
, real_name
, length
);
291 memcpy (wrapper_name
+ length
, LTO_WRAPPER_EXECUTABLE
,
292 sizeof (LTO_WRAPPER_EXECUTABLE
));
293 if (stat (wrapper_name
, &st
) == 0 && S_ISREG (st
.st_mode
))
295 resolution_file
= make_temp_file (".res");
298 plugin
->gcc
= gcc_name
;
299 plugin
->lto_wrapper
= wrapper_name
;
300 plugin
->gcc_env
= concat ("COLLECT_GCC=", gcc_name
, NULL
);
301 plugin
->resolution_file
= resolution_file
;
302 plugin
->resolution_option
= concat ("-fresolution=",
303 resolution_file
, NULL
);
313 /* Set environment variables for GCC LTO wrapper to covert LTO IR
314 object to the real object. */
317 setup_lto_wrapper_env (struct plugin_list_entry
*plugin
)
319 return (putenv (plugin
->gcc_env
)
320 || putenv ("COLLECT_GCC_OPTIONS="));
323 static struct plugin_list_entry
*plugin_list
= NULL
;
324 static struct plugin_list_entry
*current_plugin
= NULL
;
326 /* Register a claim-file handler. */
328 static enum ld_plugin_status
329 register_claim_file (ld_plugin_claim_file_handler handler
)
331 current_plugin
->claim_file
= handler
;
335 /* Register an all-symbols-read handler. */
337 static enum ld_plugin_status
338 register_all_symbols_read (ld_plugin_all_symbols_read_handler handler
)
340 current_plugin
->all_symbols_read
= handler
;
344 /* Register a cleanup handler. */
346 static enum ld_plugin_status
347 register_cleanup (ld_plugin_all_symbols_read_handler handler
)
349 current_plugin
->cleanup_handler
= handler
;
353 /* Get the symbol resolution info for a plugin-claimed input file. */
355 static enum ld_plugin_status
356 get_symbols (const void *handle ATTRIBUTE_UNUSED
, int nsyms
,
357 struct ld_plugin_symbol
*syms
)
362 for (n
= 0; n
< nsyms
; n
++)
371 syms
[n
].resolution
= LDPR_UNDEF
;
376 /* Tell plugin that LTO symbol has references from regular
378 syms
[n
].resolution
= LDPR_PREVAILING_DEF
;
387 /* Add a new (real) input file generated by a plugin. */
389 static enum ld_plugin_status
390 add_input_file (const char *pathname
)
392 /* Get symbols from the real LTO object. */
398 bfd_boolean lto_symbol_found
= FALSE
;
399 const struct ld_plugin_symbol
*lto_syms
;
403 rbfd
= bfd_openr (pathname
, NULL
);
404 if (!bfd_check_format_matches (rbfd
, bfd_object
, &matching
))
407 real_symsize
= bfd_get_symtab_upper_bound (rbfd
);
408 if (real_symsize
< 0)
411 real_syms
= (asymbol
**) bfd_malloc (real_symsize
);
414 real_nsyms
= bfd_canonicalize_symtab (rbfd
, real_syms
);
418 /* NB: LTO plugin may generate more than one real object from one
419 LTO IR object. We use the one which contains LTO symbols. */
420 lto_syms
= current_plugin
->lto_syms
;
421 lto_nsyms
= current_plugin
->lto_nsyms
;
422 for (i
= 0; i
< lto_nsyms
; i
++)
423 for (j
= 0; j
< real_nsyms
; j
++)
424 if (real_syms
[j
]->name
425 && strcmp (lto_syms
[i
].name
, real_syms
[j
]->name
) == 0)
427 lto_symbol_found
= TRUE
;
432 if (lto_symbol_found
)
434 current_plugin
->real_nsyms
= real_nsyms
;
435 current_plugin
->real_syms
= real_syms
;
436 /* NB: We can't close RBFD which own the real symbol info. */
437 current_plugin
->real_bfd
= rbfd
;
448 static enum ld_plugin_status
449 add_symbols (void * handle
,
451 const struct ld_plugin_symbol
* syms
)
454 struct plugin_data_struct
*plugin_data
=
455 bfd_alloc (abfd
, sizeof (plugin_data_struct
));
459 struct ld_plugin_symbol
*sym_info
;
461 size_t sym_info_size
, name_length
;
464 memset (plugin_data
, 0, sizeof (*plugin_data
));
466 abfd
->tdata
.plugin_data
= plugin_data
;
468 /* NB: LTO symbols are owned by LTO plugin. Create a copy so
469 that we can use it in bfd_plugin_canonicalize_symtab. */
470 sym_info_size
= nsyms
* sizeof (*syms
);
472 /* Allocate a string table */
473 for (i
= 0; i
< nsyms
; i
++)
474 sym_info_size
+= strlen (syms
[i
].name
) + 1;
476 sym_info
= bfd_alloc (abfd
, sym_info_size
);
479 /* Copy symbol table. */
480 memcpy (sym_info
, syms
, nsyms
* sizeof (*syms
));
482 /* Copy symbol names in symbol table. */
483 strtab
= (char *) (sym_info
+ nsyms
);
484 for (i
= 0; i
< nsyms
; i
++)
486 name_length
= strlen (syms
[i
].name
);
487 memcpy (strtab
, syms
[i
].name
, name_length
+ 1);
488 sym_info
[i
].name
= strtab
;
489 strtab
+= name_length
+ 1;
492 plugin_data
->nsyms
= nsyms
;
493 plugin_data
->syms
= sym_info
;
495 current_plugin
->lto_nsyms
= nsyms
;
496 current_plugin
->lto_syms
= sym_info
;
501 abfd
->flags
|= HAS_SYMS
;
507 bfd_plugin_open_input (bfd
*ibfd
, struct ld_plugin_input_file
*file
)
512 while (iobfd
->my_archive
513 && !bfd_is_thin_archive (iobfd
->my_archive
))
514 iobfd
= iobfd
->my_archive
;
515 file
->name
= iobfd
->filename
;
517 if (!iobfd
->iostream
&& !bfd_open_file (iobfd
))
520 /* The plugin API expects that the file descriptor won't be closed
521 and reused as done by the bfd file cache. So open it again.
522 dup isn't good enough. plugin IO uses lseek/read while BFD uses
523 fseek/fread. It isn't wise to mix the unistd and stdio calls on
524 the same underlying file descriptor. */
525 file
->fd
= open (file
->name
, O_RDONLY
| O_BINARY
);
531 struct stat stat_buf
;
533 if (fstat (file
->fd
, &stat_buf
))
540 file
->filesize
= stat_buf
.st_size
;
544 file
->offset
= ibfd
->origin
;
545 file
->filesize
= arelt_size (ibfd
);
551 try_claim (bfd
*abfd
)
554 struct ld_plugin_input_file file
;
557 if (bfd_plugin_open_input (abfd
, &file
)
558 && current_plugin
->claim_file
)
560 current_plugin
->claim_file (&file
, &claimed
);
563 if (current_plugin
->all_symbols_read
)
565 struct plugin_data_struct
*plugin_data
566 = abfd
->tdata
.plugin_data
;
569 /* Get real symbols from LTO wrapper. */
570 current_plugin
->all_symbols_read ();
572 /* Copy real symbols to plugin_data. */
573 plugin_data
->real_bfd
= current_plugin
->real_bfd
;
574 plugin_data
->real_nsyms
= current_plugin
->real_nsyms
;
575 plugin_data
->real_syms
= current_plugin
->real_syms
;
577 /* Clean up LTO plugin. */
578 if (current_plugin
->cleanup_handler
)
579 current_plugin
->cleanup_handler ();
587 if (current_plugin
->lto_wrapper
)
589 /* Clean up for LTO wrapper. NB: Resolution file and option
590 have been created regardless if an IR object is claimed or
592 unlink (current_plugin
->resolution_file
);
593 free (current_plugin
->resolution_option
);
600 try_load_plugin (const char *pname
,
601 struct plugin_list_entry
*plugin_list_iter
,
602 bfd
*abfd
, bfd_boolean build_list_p
)
605 struct ld_plugin_tv tv
[12];
607 ld_plugin_onload onload
;
608 enum ld_plugin_status status
;
611 /* NB: Each object is independent. Reuse the previous plugin from
612 the last run will lead to wrong result. */
614 memset (current_plugin
, 0,
615 offsetof (struct plugin_list_entry
, next
));
617 if (plugin_list_iter
)
618 pname
= plugin_list_iter
->plugin_name
;
620 plugin_handle
= dlopen (pname
, RTLD_NOW
);
623 _bfd_error_handler ("%s\n", dlerror ());
627 if (plugin_list_iter
== NULL
)
629 size_t length_plugin_name
= strlen (pname
) + 1;
630 char *plugin_name
= bfd_malloc (length_plugin_name
);
631 if (plugin_name
== NULL
)
633 plugin_list_iter
= bfd_malloc (sizeof *plugin_list_iter
);
634 if (plugin_list_iter
== NULL
)
639 /* Make a copy of PNAME since PNAME from load_plugin () will be
641 memcpy (plugin_name
, pname
, length_plugin_name
);
642 memset (plugin_list_iter
, 0, sizeof (*plugin_list_iter
));
643 plugin_list_iter
->plugin_name
= plugin_name
;
644 plugin_list_iter
->next
= plugin_list
;
645 plugin_list
= plugin_list_iter
;
648 current_plugin
= plugin_list_iter
;
652 onload
= dlsym (plugin_handle
, "onload");
657 tv
[i
].tv_tag
= LDPT_MESSAGE
;
658 tv
[i
].tv_u
.tv_message
= message
;
661 tv
[i
].tv_tag
= LDPT_REGISTER_CLAIM_FILE_HOOK
;
662 tv
[i
].tv_u
.tv_register_claim_file
= register_claim_file
;
665 tv
[i
].tv_tag
= LDPT_ADD_SYMBOLS
;
666 tv
[i
].tv_u
.tv_add_symbols
= add_symbols
;
668 if (get_lto_wrapper (plugin_list_iter
))
671 tv
[i
].tv_tag
= LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK
;
672 tv
[i
].tv_u
.tv_register_all_symbols_read
= register_all_symbols_read
;
675 tv
[i
].tv_tag
= LDPT_REGISTER_CLEANUP_HOOK
;
676 tv
[i
].tv_u
.tv_register_cleanup
= register_cleanup
;
679 tv
[i
].tv_tag
= LDPT_GET_SYMBOLS
;
680 tv
[i
].tv_u
.tv_get_symbols
= get_symbols
;
683 tv
[i
].tv_tag
= LDPT_GET_SYMBOLS_V2
;
684 tv
[i
].tv_u
.tv_get_symbols
= get_symbols
;
687 tv
[i
].tv_tag
= LDPT_OPTION
;
688 tv
[i
].tv_u
.tv_string
= plugin_list_iter
->lto_wrapper
;
691 tv
[i
].tv_tag
= LDPT_OPTION
;
692 tv
[i
].tv_u
.tv_string
= plugin_list_iter
->resolution_option
;
695 tv
[i
].tv_tag
= LDPT_LINKER_OUTPUT
;
696 tv
[i
].tv_u
.tv_val
= LDPO_EXEC
;
699 tv
[i
].tv_tag
= LDPT_ADD_INPUT_FILE
;
700 tv
[i
].tv_u
.tv_add_input_file
= add_input_file
;
704 tv
[i
].tv_tag
= LDPT_NULL
;
705 tv
[i
].tv_u
.tv_val
= 0;
707 /* LTO plugin will call handler hooks to set up plugin handlers. */
708 status
= (*onload
)(tv
);
710 if (status
!= LDPS_OK
)
713 if (current_plugin
->lto_wrapper
714 && setup_lto_wrapper_env (current_plugin
))
717 abfd
->plugin_format
= bfd_plugin_no
;
719 if (!current_plugin
->claim_file
)
722 if (!try_claim (abfd
))
725 abfd
->plugin_format
= bfd_plugin_yes
;
729 dlclose (plugin_handle
);
733 /* There may be plugin libraries in lib/bfd-plugins. */
734 static int has_plugin_list
= -1;
736 static bfd_cleanup (*ld_plugin_object_p
) (bfd
*);
738 static const char *plugin_name
;
741 bfd_plugin_set_plugin (const char *p
)
746 /* Return TRUE if a plugin library is used. */
749 bfd_plugin_specified_p (void)
751 return plugin_list
!= NULL
;
754 /* Return TRUE if ABFD can be claimed by linker LTO plugin. */
757 bfd_link_plugin_object_p (bfd
*abfd
)
759 if (ld_plugin_object_p
)
760 return ld_plugin_object_p (abfd
) != NULL
;
764 extern const bfd_target plugin_vec
;
766 /* Return TRUE if TARGET is a pointer to plugin_vec. */
769 bfd_plugin_target_p (const bfd_target
*target
)
771 return target
== &plugin_vec
;
774 /* Register OBJECT_P to be used by bfd_plugin_object_p. */
777 register_ld_plugin_object_p (bfd_cleanup (*object_p
) (bfd
*))
779 ld_plugin_object_p
= object_p
;
783 build_plugin_list (bfd
*abfd
)
785 /* The intent was to search ${libdir}/bfd-plugins for plugins, but
786 unfortunately the original implementation wasn't precisely that
787 when configuring binutils using --libdir. Search in the proper
788 path first, then the old one for backwards compatibility. */
789 static const char *path
[]
790 = { LIBDIR
"/bfd-plugins", BINDIR
"/../lib/bfd-plugins" };
794 if (has_plugin_list
>= 0)
797 /* Try not to search the same dir twice, by looking at st_dev and
798 st_ino for the dir. If we are on a file system that always sets
799 st_ino to zero or the actual st_ino is zero we might waste some
800 time, but that doesn't matter too much. */
803 for (i
= 0; i
< sizeof (path
) / sizeof (path
[0]); i
++)
805 char *plugin_dir
= make_relative_prefix (plugin_program_name
,
813 if (stat (plugin_dir
, &st
) == 0
814 && S_ISDIR (st
.st_mode
)
815 && !(last_st
.st_dev
== st
.st_dev
816 && last_st
.st_ino
== st
.st_ino
818 && (d
= opendir (plugin_dir
)) != NULL
)
822 last_st
.st_dev
= st
.st_dev
;
823 last_st
.st_ino
= st
.st_ino
;
824 while ((ent
= readdir (d
)) != NULL
)
828 full_name
= concat (plugin_dir
, "/", ent
->d_name
, NULL
);
829 if (stat (full_name
, &st
) == 0 && S_ISREG (st
.st_mode
))
830 try_load_plugin (full_name
, NULL
, abfd
, TRUE
);
839 has_plugin_list
= plugin_list
!= NULL
;
843 load_plugin (bfd
*abfd
)
845 struct plugin_list_entry
*plugin_list_iter
;
848 return try_load_plugin (plugin_name
, plugin_list
, abfd
, FALSE
);
850 if (plugin_program_name
== NULL
)
853 build_plugin_list (abfd
);
855 for (plugin_list_iter
= plugin_list
;
857 plugin_list_iter
= plugin_list_iter
->next
)
858 if (try_load_plugin (NULL
, plugin_list_iter
, abfd
, FALSE
))
866 bfd_plugin_object_p (bfd
*abfd
)
868 if (ld_plugin_object_p
)
869 return ld_plugin_object_p (abfd
);
871 if (abfd
->plugin_format
== bfd_plugin_unknown
&& !load_plugin (abfd
))
874 return abfd
->plugin_format
== bfd_plugin_yes
? _bfd_no_cleanup
: NULL
;
877 /* Copy any private info we understand from the input bfd
878 to the output bfd. */
881 bfd_plugin_bfd_copy_private_bfd_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
882 bfd
*obfd ATTRIBUTE_UNUSED
)
888 /* Copy any private info we understand from the input section
889 to the output section. */
892 bfd_plugin_bfd_copy_private_section_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
893 asection
*isection ATTRIBUTE_UNUSED
,
894 bfd
*obfd ATTRIBUTE_UNUSED
,
895 asection
*osection ATTRIBUTE_UNUSED
)
901 /* Copy any private info we understand from the input symbol
902 to the output symbol. */
905 bfd_plugin_bfd_copy_private_symbol_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
906 asymbol
*isymbol ATTRIBUTE_UNUSED
,
907 bfd
*obfd ATTRIBUTE_UNUSED
,
908 asymbol
*osymbol ATTRIBUTE_UNUSED
)
915 bfd_plugin_bfd_print_private_bfd_data (bfd
*abfd ATTRIBUTE_UNUSED
, PTR ptr ATTRIBUTE_UNUSED
)
922 bfd_plugin_core_file_failing_command (bfd
*abfd ATTRIBUTE_UNUSED
)
929 bfd_plugin_core_file_failing_signal (bfd
*abfd ATTRIBUTE_UNUSED
)
936 bfd_plugin_core_file_pid (bfd
*abfd ATTRIBUTE_UNUSED
)
943 bfd_plugin_get_symtab_upper_bound (bfd
*abfd
)
945 struct plugin_data_struct
*plugin_data
= abfd
->tdata
.plugin_data
;
946 long nsyms
= plugin_data
->nsyms
;
948 BFD_ASSERT (nsyms
>= 0);
950 return ((nsyms
+ 1) * sizeof (asymbol
*));
954 convert_flags (const struct ld_plugin_symbol
*sym
)
965 return BSF_GLOBAL
| BSF_WEAK
;
974 bfd_plugin_canonicalize_symtab (bfd
*abfd
,
977 struct plugin_data_struct
*plugin_data
= abfd
->tdata
.plugin_data
;
978 long nsyms
= plugin_data
->nsyms
;
979 const struct ld_plugin_symbol
*syms
= plugin_data
->syms
;
980 static asection fake_section
981 = BFD_FAKE_SECTION (fake_section
, NULL
, "plug", 0,
982 SEC_ALLOC
| SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
);
983 static asection fake_common_section
984 = BFD_FAKE_SECTION (fake_common_section
, NULL
, "plug", 0, SEC_IS_COMMON
);
989 real_syms
= plugin_data
->real_syms
;
991 real_nsyms
= plugin_data
->real_nsyms
;
995 for (i
= 0; i
< nsyms
; i
++)
997 asymbol
*s
= bfd_alloc (abfd
, sizeof (asymbol
));
1003 s
->name
= syms
[i
].name
;
1005 s
->flags
= convert_flags (&syms
[i
]);
1006 switch (syms
[i
].def
)
1009 s
->section
= &fake_common_section
;
1012 case LDPK_WEAKUNDEF
:
1013 s
->section
= bfd_und_section_ptr
;
1017 s
->section
= &fake_section
;
1019 /* Use real LTO symbols if possible. */
1020 for (j
= 0; j
< real_nsyms
; j
++)
1021 if (real_syms
[j
]->name
1022 && strcmp (syms
[i
].name
, real_syms
[j
]->name
) == 0)
1024 s
->section
= real_syms
[j
]->section
;
1032 s
->udata
.p
= (void *) &syms
[i
];
1039 bfd_plugin_print_symbol (bfd
*abfd ATTRIBUTE_UNUSED
,
1040 PTR afile ATTRIBUTE_UNUSED
,
1041 asymbol
*symbol ATTRIBUTE_UNUSED
,
1042 bfd_print_symbol_type how ATTRIBUTE_UNUSED
)
1048 bfd_plugin_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
1052 bfd_symbol_info (symbol
, ret
);
1055 /* Make an empty symbol. */
1058 bfd_plugin_make_empty_symbol (bfd
*abfd
)
1060 asymbol
*new_symbol
= bfd_zalloc (abfd
, sizeof (asymbol
));
1061 if (new_symbol
== NULL
)
1063 new_symbol
->the_bfd
= abfd
;
1068 bfd_plugin_sizeof_headers (bfd
*a ATTRIBUTE_UNUSED
,
1069 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
1076 bfd_plugin_close_and_cleanup (bfd
*abfd
)
1078 struct plugin_data_struct
*plugin_data
;
1080 if (abfd
->format
!= bfd_archive
1081 && (plugin_data
= abfd
->tdata
.plugin_data
))
1083 if (plugin_data
->real_bfd
)
1084 bfd_close (plugin_data
->real_bfd
);
1086 if (plugin_data
->real_syms
)
1087 free (plugin_data
->real_syms
);
1090 return _bfd_generic_close_and_cleanup (abfd
);
1093 const bfd_target plugin_vec
=
1095 "plugin", /* Name. */
1096 bfd_target_unknown_flavour
,
1097 BFD_ENDIAN_LITTLE
, /* Target byte order. */
1098 BFD_ENDIAN_LITTLE
, /* Target headers byte order. */
1099 (HAS_RELOC
| EXEC_P
| /* Object flags. */
1100 HAS_LINENO
| HAS_DEBUG
|
1101 HAS_SYMS
| HAS_LOCALS
| DYNAMIC
| WP_TEXT
| D_PAGED
),
1102 (SEC_CODE
| SEC_DATA
| SEC_ROM
| SEC_HAS_CONTENTS
1103 | SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* Section flags. */
1104 0, /* symbol_leading_char. */
1105 '/', /* ar_pad_char. */
1106 15, /* ar_max_namelen. */
1107 255, /* match priority. */
1109 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
1110 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
1111 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
1112 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
1113 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
1114 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
1116 { /* bfd_check_format. */
1118 bfd_plugin_object_p
,
1119 bfd_generic_archive_p
,
1122 { /* bfd_set_format. */
1123 _bfd_bool_bfd_false_error
,
1124 _bfd_bool_bfd_false_error
,
1125 _bfd_generic_mkarchive
,
1126 _bfd_bool_bfd_false_error
,
1128 { /* bfd_write_contents. */
1129 _bfd_bool_bfd_false_error
,
1130 _bfd_bool_bfd_false_error
,
1131 _bfd_write_archive_contents
,
1132 _bfd_bool_bfd_false_error
,
1135 BFD_JUMP_TABLE_GENERIC (bfd_plugin
),
1136 BFD_JUMP_TABLE_COPY (bfd_plugin
),
1137 BFD_JUMP_TABLE_CORE (bfd_plugin
),
1138 #ifdef USE_64_BIT_ARCHIVE
1139 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_64_bit
),
1141 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
1143 BFD_JUMP_TABLE_SYMBOLS (bfd_plugin
),
1144 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs
),
1145 BFD_JUMP_TABLE_WRITE (bfd_plugin
),
1146 BFD_JUMP_TABLE_LINK (bfd_plugin
),
1147 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
1151 NULL
/* backend_data. */
1153 #endif /* BFD_SUPPORTS_PLUGIN */