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 /* Use GCC LTO wrapper to covert LTO IR object to the real object. */
153 get_lto_wrapper (struct plugin_list_entry
*plugin
)
156 const char *real_name
;
157 const char *base_name
;
159 const char *target_start
= NULL
;
160 const char *target_end
= NULL
;
161 size_t target_length
= 0;
165 char dir_seperator
= '\0';
166 char *resolution_file
;
168 if (plugin
->initialized
)
170 if (plugin
->lto_wrapper
)
172 resolution_file
= make_temp_file (".res");
175 plugin
->resolution_file
= resolution_file
;
176 plugin
->resolution_option
= concat ("-fresolution=",
177 resolution_file
, NULL
);
182 /* Something is wrong. Give up. */
184 free (plugin
->lto_wrapper
);
185 free (plugin
->gcc_env
);
187 plugin
->gcc_env
= NULL
;
188 plugin
->lto_wrapper
= NULL
;
195 plugin
->initialized
= TRUE
;
197 /* Check for PREFIX/libexec/gcc/TARGET/VERSION/liblto_plugin.so. */
198 real_name
= lrealpath (plugin
->plugin_name
);
199 base_name
= lbasename (real_name
);
201 /* The directory length in plugin pathname. */
202 length
= base_name
- real_name
;
204 /* Skip if there is no PREFIX. */
208 p
= (char *) real_name
+ length
- 1;
209 if (IS_DIR_SEPARATOR (*p
))
212 for (; p
!= real_name
; p
--)
213 if (IS_DIR_SEPARATOR (*p
))
220 target_start
= p
+ 1;
221 target_length
= target_end
- target_start
;
231 /* Skip if there is no TARGET nor PREFIX. */
232 if (!target_length
|| !dir_seperator
)
235 #ifdef HAVE_EXECUTABLE_SUFFIX
236 # define GCC_EXECUTABLE "gcc" EXECUTABLE_SUFFIX
237 # define LTO_WRAPPER_EXECUTABLE "lto-wrapper" EXECUTABLE_SUFFIX
239 # define GCC_EXECUTABLE "gcc"
240 # define LTO_WRAPPER_EXECUTABLE "lto-wrapper"
242 gcc_name
= bfd_malloc (length
+ target_length
243 + sizeof (GCC_EXECUTABLE
));
244 if (gcc_name
== NULL
)
246 memcpy (gcc_name
, real_name
, length
);
248 /* Get PREFIX/bin/. */
249 p
+= gcc_name
- real_name
;
250 memcpy (p
+ 1, "bin", 3);
251 p
[4] = dir_seperator
;
253 /* Try PREFIX/bin/TARGET-gcc first. */
254 memcpy (p
+ 5, target_start
, target_length
);
255 p
[5 + target_length
] = '-';
256 memcpy (p
+ 5 + target_length
+ 1, GCC_EXECUTABLE
,
257 sizeof (GCC_EXECUTABLE
));
258 if (stat (gcc_name
, &st
) != 0 || !S_ISREG (st
.st_mode
))
260 /* Then try PREFIX/bin/gcc. */
261 memcpy (p
+ 5, GCC_EXECUTABLE
, sizeof (GCC_EXECUTABLE
));
262 if (stat (gcc_name
, &st
) != 0 || !S_ISREG (st
.st_mode
))
269 /* lto-wrapper should be in the same directory with LTO plugin. */
270 wrapper_name
= bfd_malloc (length
+ sizeof (LTO_WRAPPER_EXECUTABLE
));
271 if (wrapper_name
== NULL
)
276 memcpy (wrapper_name
, real_name
, length
);
277 memcpy (wrapper_name
+ length
, LTO_WRAPPER_EXECUTABLE
,
278 sizeof (LTO_WRAPPER_EXECUTABLE
));
279 if (stat (wrapper_name
, &st
) == 0 && S_ISREG (st
.st_mode
))
281 resolution_file
= make_temp_file (".res");
284 plugin
->gcc
= gcc_name
;
285 plugin
->lto_wrapper
= wrapper_name
;
286 plugin
->gcc_env
= concat ("COLLECT_GCC=", gcc_name
, NULL
);
287 plugin
->resolution_file
= resolution_file
;
288 plugin
->resolution_option
= concat ("-fresolution=",
289 resolution_file
, NULL
);
299 /* Set environment variables for GCC LTO wrapper to covert LTO IR
300 object to the real object. */
303 setup_lto_wrapper_env (struct plugin_list_entry
*plugin
)
305 return (putenv (plugin
->gcc_env
)
306 || putenv ("COLLECT_GCC_OPTIONS="));
309 static struct plugin_list_entry
*plugin_list
= NULL
;
310 static struct plugin_list_entry
*current_plugin
= NULL
;
312 /* Register a claim-file handler. */
314 static enum ld_plugin_status
315 register_claim_file (ld_plugin_claim_file_handler handler
)
317 current_plugin
->claim_file
= handler
;
321 /* Register an all-symbols-read handler. */
323 static enum ld_plugin_status
324 register_all_symbols_read (ld_plugin_all_symbols_read_handler handler
)
326 current_plugin
->all_symbols_read
= handler
;
330 /* Register a cleanup handler. */
332 static enum ld_plugin_status
333 register_cleanup (ld_plugin_all_symbols_read_handler handler
)
335 current_plugin
->cleanup_handler
= handler
;
339 /* Get the symbol resolution info for a plugin-claimed input file. */
341 static enum ld_plugin_status
342 get_symbols (const void *handle ATTRIBUTE_UNUSED
, int nsyms
,
343 struct ld_plugin_symbol
*syms
)
348 for (n
= 0; n
< nsyms
; n
++)
357 syms
[n
].resolution
= LDPR_UNDEF
;
362 /* Tell plugin that LTO symbol has references from regular
364 syms
[n
].resolution
= LDPR_PREVAILING_DEF
;
373 /* Add a new (real) input file generated by a plugin. */
375 static enum ld_plugin_status
376 add_input_file (const char *pathname
)
378 /* Get symbols from the real LTO object. */
384 bfd_boolean lto_symbol_found
= FALSE
;
385 const struct ld_plugin_symbol
*lto_syms
;
389 rbfd
= bfd_openr (pathname
, NULL
);
390 if (!bfd_check_format_matches (rbfd
, bfd_object
, &matching
))
393 real_symsize
= bfd_get_symtab_upper_bound (rbfd
);
394 if (real_symsize
< 0)
397 real_syms
= (asymbol
**) bfd_malloc (real_symsize
);
400 real_nsyms
= bfd_canonicalize_symtab (rbfd
, real_syms
);
404 /* NB: LTO plugin may generate more than one real object from one
405 LTO IR object. We use the one which contains LTO symbols. */
406 lto_syms
= current_plugin
->lto_syms
;
407 lto_nsyms
= current_plugin
->lto_nsyms
;
408 for (i
= 0; i
< lto_nsyms
; i
++)
409 for (j
= 0; j
< real_nsyms
; j
++)
410 if (real_syms
[j
]->name
411 && strcmp (lto_syms
[i
].name
, real_syms
[j
]->name
) == 0)
413 lto_symbol_found
= TRUE
;
418 if (lto_symbol_found
)
420 current_plugin
->real_nsyms
= real_nsyms
;
421 current_plugin
->real_syms
= real_syms
;
422 /* NB: We can't close RBFD which own the real symbol info. */
423 current_plugin
->real_bfd
= rbfd
;
434 static enum ld_plugin_status
435 add_symbols (void * handle
,
437 const struct ld_plugin_symbol
* syms
)
440 struct plugin_data_struct
*plugin_data
=
441 bfd_alloc (abfd
, sizeof (plugin_data_struct
));
445 struct ld_plugin_symbol
*sym_info
;
447 size_t sym_info_size
, name_length
;
450 memset (plugin_data
, 0, sizeof (*plugin_data
));
452 abfd
->tdata
.plugin_data
= plugin_data
;
454 /* NB: LTO symbols are owned by LTO plugin. Create a copy so
455 that we can use it in bfd_plugin_canonicalize_symtab. */
456 sym_info_size
= nsyms
* sizeof (*syms
);
458 /* Allocate a string table */
459 for (i
= 0; i
< nsyms
; i
++)
460 sym_info_size
+= strlen (syms
[i
].name
) + 1;
462 sym_info
= bfd_alloc (abfd
, sym_info_size
);
465 /* Copy symbol table. */
466 memcpy (sym_info
, syms
, nsyms
* sizeof (*syms
));
468 /* Copy symbol names in symbol table. */
469 strtab
= (char *) (sym_info
+ nsyms
);
470 for (i
= 0; i
< nsyms
; i
++)
472 name_length
= strlen (syms
[i
].name
);
473 memcpy (strtab
, syms
[i
].name
, name_length
+ 1);
474 sym_info
[i
].name
= strtab
;
475 strtab
+= name_length
+ 1;
478 plugin_data
->nsyms
= nsyms
;
479 plugin_data
->syms
= sym_info
;
481 current_plugin
->lto_nsyms
= nsyms
;
482 current_plugin
->lto_syms
= sym_info
;
487 abfd
->flags
|= HAS_SYMS
;
492 static const char *plugin_program_name
;
495 bfd_plugin_set_program_name (const char *program_name
)
497 plugin_program_name
= program_name
;
501 bfd_plugin_open_input (bfd
*ibfd
, struct ld_plugin_input_file
*file
)
506 while (iobfd
->my_archive
507 && !bfd_is_thin_archive (iobfd
->my_archive
))
508 iobfd
= iobfd
->my_archive
;
509 file
->name
= iobfd
->filename
;
511 if (!iobfd
->iostream
&& !bfd_open_file (iobfd
))
514 /* The plugin API expects that the file descriptor won't be closed
515 and reused as done by the bfd file cache. So open it again.
516 dup isn't good enough. plugin IO uses lseek/read while BFD uses
517 fseek/fread. It isn't wise to mix the unistd and stdio calls on
518 the same underlying file descriptor. */
519 file
->fd
= open (file
->name
, O_RDONLY
| O_BINARY
);
525 struct stat stat_buf
;
527 if (fstat (file
->fd
, &stat_buf
))
534 file
->filesize
= stat_buf
.st_size
;
538 file
->offset
= ibfd
->origin
;
539 file
->filesize
= arelt_size (ibfd
);
545 try_claim (bfd
*abfd
)
548 struct ld_plugin_input_file file
;
551 if (bfd_plugin_open_input (abfd
, &file
)
552 && current_plugin
->claim_file
)
554 current_plugin
->claim_file (&file
, &claimed
);
557 if (current_plugin
->all_symbols_read
)
559 struct plugin_data_struct
*plugin_data
560 = abfd
->tdata
.plugin_data
;
563 /* Get real symbols from LTO wrapper. */
564 current_plugin
->all_symbols_read ();
566 /* Copy real symbols to plugin_data. */
567 plugin_data
->real_bfd
= current_plugin
->real_bfd
;
568 plugin_data
->real_nsyms
= current_plugin
->real_nsyms
;
569 plugin_data
->real_syms
= current_plugin
->real_syms
;
571 /* Clean up LTO plugin. */
572 if (current_plugin
->cleanup_handler
)
573 current_plugin
->cleanup_handler ();
581 if (current_plugin
->lto_wrapper
)
583 /* Clean up for LTO wrapper. NB: Resolution file and option
584 have been created regardless if an IR object is claimed or
586 unlink (current_plugin
->resolution_file
);
587 free (current_plugin
->resolution_option
);
594 try_load_plugin (const char *pname
,
595 struct plugin_list_entry
*plugin_list_iter
,
596 bfd
*abfd
, bfd_boolean build_list_p
)
599 struct ld_plugin_tv tv
[12];
601 ld_plugin_onload onload
;
602 enum ld_plugin_status status
;
605 /* NB: Each object is independent. Reuse the previous plugin from
606 the last run will lead to wrong result. */
608 memset (current_plugin
, 0,
609 offsetof (struct plugin_list_entry
, next
));
611 if (plugin_list_iter
)
612 pname
= plugin_list_iter
->plugin_name
;
614 plugin_handle
= dlopen (pname
, RTLD_NOW
);
617 _bfd_error_handler ("%s\n", dlerror ());
621 if (plugin_list_iter
== NULL
)
623 size_t length_plugin_name
= strlen (pname
) + 1;
624 char *plugin_name
= bfd_malloc (length_plugin_name
);
625 if (plugin_name
== NULL
)
627 plugin_list_iter
= bfd_malloc (sizeof *plugin_list_iter
);
628 if (plugin_list_iter
== NULL
)
633 /* Make a copy of PNAME since PNAME from load_plugin () will be
635 memcpy (plugin_name
, pname
, length_plugin_name
);
636 memset (plugin_list_iter
, 0, sizeof (*plugin_list_iter
));
637 plugin_list_iter
->plugin_name
= plugin_name
;
638 plugin_list_iter
->next
= plugin_list
;
639 plugin_list
= plugin_list_iter
;
642 current_plugin
= plugin_list_iter
;
646 onload
= dlsym (plugin_handle
, "onload");
651 tv
[i
].tv_tag
= LDPT_MESSAGE
;
652 tv
[i
].tv_u
.tv_message
= message
;
655 tv
[i
].tv_tag
= LDPT_REGISTER_CLAIM_FILE_HOOK
;
656 tv
[i
].tv_u
.tv_register_claim_file
= register_claim_file
;
659 tv
[i
].tv_tag
= LDPT_ADD_SYMBOLS
;
660 tv
[i
].tv_u
.tv_add_symbols
= add_symbols
;
662 if (get_lto_wrapper (plugin_list_iter
))
665 tv
[i
].tv_tag
= LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK
;
666 tv
[i
].tv_u
.tv_register_all_symbols_read
= register_all_symbols_read
;
669 tv
[i
].tv_tag
= LDPT_REGISTER_CLEANUP_HOOK
;
670 tv
[i
].tv_u
.tv_register_cleanup
= register_cleanup
;
673 tv
[i
].tv_tag
= LDPT_GET_SYMBOLS
;
674 tv
[i
].tv_u
.tv_get_symbols
= get_symbols
;
677 tv
[i
].tv_tag
= LDPT_GET_SYMBOLS_V2
;
678 tv
[i
].tv_u
.tv_get_symbols
= get_symbols
;
681 tv
[i
].tv_tag
= LDPT_OPTION
;
682 tv
[i
].tv_u
.tv_string
= plugin_list_iter
->lto_wrapper
;
685 tv
[i
].tv_tag
= LDPT_OPTION
;
686 tv
[i
].tv_u
.tv_string
= plugin_list_iter
->resolution_option
;
689 tv
[i
].tv_tag
= LDPT_LINKER_OUTPUT
;
690 tv
[i
].tv_u
.tv_val
= LDPO_EXEC
;
693 tv
[i
].tv_tag
= LDPT_ADD_INPUT_FILE
;
694 tv
[i
].tv_u
.tv_add_input_file
= add_input_file
;
698 tv
[i
].tv_tag
= LDPT_NULL
;
699 tv
[i
].tv_u
.tv_val
= 0;
701 /* LTO plugin will call handler hooks to set up plugin handlers. */
702 status
= (*onload
)(tv
);
704 if (status
!= LDPS_OK
)
707 if (current_plugin
->lto_wrapper
708 && setup_lto_wrapper_env (current_plugin
))
711 abfd
->plugin_format
= bfd_plugin_no
;
713 if (!current_plugin
->claim_file
)
716 if (!try_claim (abfd
))
719 abfd
->plugin_format
= bfd_plugin_yes
;
723 dlclose (plugin_handle
);
727 /* There may be plugin libraries in lib/bfd-plugins. */
728 static int has_plugin_list
= -1;
730 static const bfd_target
*(*ld_plugin_object_p
) (bfd
*);
732 static const char *plugin_name
;
735 bfd_plugin_set_plugin (const char *p
)
740 /* Return TRUE if a plugin library is used. */
743 bfd_plugin_specified_p (void)
745 return plugin_list
!= NULL
;
748 /* Return TRUE if ABFD can be claimed by linker LTO plugin. */
751 bfd_link_plugin_object_p (bfd
*abfd
)
753 if (ld_plugin_object_p
)
754 return ld_plugin_object_p (abfd
) != NULL
;
758 extern const bfd_target plugin_vec
;
760 /* Return TRUE if TARGET is a pointer to plugin_vec. */
763 bfd_plugin_target_p (const bfd_target
*target
)
765 return target
== &plugin_vec
;
768 /* Register OBJECT_P to be used by bfd_plugin_object_p. */
771 register_ld_plugin_object_p (const bfd_target
*(*object_p
) (bfd
*))
773 ld_plugin_object_p
= object_p
;
777 build_plugin_list (bfd
*abfd
)
779 /* The intent was to search ${libdir}/bfd-plugins for plugins, but
780 unfortunately the original implementation wasn't precisely that
781 when configuring binutils using --libdir. Search in the proper
782 path first, then the old one for backwards compatibility. */
783 static const char *path
[]
784 = { LIBDIR
"/bfd-plugins", BINDIR
"/../lib/bfd-plugins" };
788 if (has_plugin_list
>= 0)
791 /* Try not to search the same dir twice, by looking at st_dev and
792 st_ino for the dir. If we are on a file system that always sets
793 st_ino to zero or the actual st_ino is zero we might waste some
794 time, but that doesn't matter too much. */
797 for (i
= 0; i
< sizeof (path
) / sizeof (path
[0]); i
++)
799 char *plugin_dir
= make_relative_prefix (plugin_program_name
,
807 if (stat (plugin_dir
, &st
) == 0
808 && S_ISDIR (st
.st_mode
)
809 && !(last_st
.st_dev
== st
.st_dev
810 && last_st
.st_ino
== st
.st_ino
812 && (d
= opendir (plugin_dir
)) != NULL
)
816 last_st
.st_dev
= st
.st_dev
;
817 last_st
.st_ino
= st
.st_ino
;
818 while ((ent
= readdir (d
)) != NULL
)
822 full_name
= concat (plugin_dir
, "/", ent
->d_name
, NULL
);
823 if (stat (full_name
, &st
) == 0 && S_ISREG (st
.st_mode
))
824 try_load_plugin (full_name
, NULL
, abfd
, TRUE
);
833 has_plugin_list
= plugin_list
!= NULL
;
837 load_plugin (bfd
*abfd
)
839 struct plugin_list_entry
*plugin_list_iter
;
842 return try_load_plugin (plugin_name
, plugin_list
, abfd
, FALSE
);
844 if (plugin_program_name
== NULL
)
847 build_plugin_list (abfd
);
849 for (plugin_list_iter
= plugin_list
;
851 plugin_list_iter
= plugin_list_iter
->next
)
852 if (try_load_plugin (NULL
, plugin_list_iter
, abfd
, FALSE
))
859 static const bfd_target
*
860 bfd_plugin_object_p (bfd
*abfd
)
862 if (ld_plugin_object_p
)
863 return ld_plugin_object_p (abfd
);
865 if (abfd
->plugin_format
== bfd_plugin_unknown
&& !load_plugin (abfd
))
868 return abfd
->plugin_format
== bfd_plugin_yes
? abfd
->xvec
: NULL
;
871 /* Copy any private info we understand from the input bfd
872 to the output bfd. */
875 bfd_plugin_bfd_copy_private_bfd_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
876 bfd
*obfd ATTRIBUTE_UNUSED
)
882 /* Copy any private info we understand from the input section
883 to the output section. */
886 bfd_plugin_bfd_copy_private_section_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
887 asection
*isection ATTRIBUTE_UNUSED
,
888 bfd
*obfd ATTRIBUTE_UNUSED
,
889 asection
*osection ATTRIBUTE_UNUSED
)
895 /* Copy any private info we understand from the input symbol
896 to the output symbol. */
899 bfd_plugin_bfd_copy_private_symbol_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
900 asymbol
*isymbol ATTRIBUTE_UNUSED
,
901 bfd
*obfd ATTRIBUTE_UNUSED
,
902 asymbol
*osymbol ATTRIBUTE_UNUSED
)
909 bfd_plugin_bfd_print_private_bfd_data (bfd
*abfd ATTRIBUTE_UNUSED
, PTR ptr ATTRIBUTE_UNUSED
)
916 bfd_plugin_core_file_failing_command (bfd
*abfd ATTRIBUTE_UNUSED
)
923 bfd_plugin_core_file_failing_signal (bfd
*abfd ATTRIBUTE_UNUSED
)
930 bfd_plugin_core_file_pid (bfd
*abfd ATTRIBUTE_UNUSED
)
937 bfd_plugin_get_symtab_upper_bound (bfd
*abfd
)
939 struct plugin_data_struct
*plugin_data
= abfd
->tdata
.plugin_data
;
940 long nsyms
= plugin_data
->nsyms
;
942 BFD_ASSERT (nsyms
>= 0);
944 return ((nsyms
+ 1) * sizeof (asymbol
*));
948 convert_flags (const struct ld_plugin_symbol
*sym
)
959 return BSF_GLOBAL
| BSF_WEAK
;
968 bfd_plugin_canonicalize_symtab (bfd
*abfd
,
971 struct plugin_data_struct
*plugin_data
= abfd
->tdata
.plugin_data
;
972 long nsyms
= plugin_data
->nsyms
;
973 const struct ld_plugin_symbol
*syms
= plugin_data
->syms
;
974 static asection fake_section
975 = BFD_FAKE_SECTION (fake_section
, NULL
, "plug", 0,
976 SEC_ALLOC
| SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
);
977 static asection fake_common_section
978 = BFD_FAKE_SECTION (fake_common_section
, NULL
, "plug", 0, SEC_IS_COMMON
);
983 real_syms
= plugin_data
->real_syms
;
985 real_nsyms
= plugin_data
->real_nsyms
;
989 for (i
= 0; i
< nsyms
; i
++)
991 asymbol
*s
= bfd_alloc (abfd
, sizeof (asymbol
));
997 s
->name
= syms
[i
].name
;
999 s
->flags
= convert_flags (&syms
[i
]);
1000 switch (syms
[i
].def
)
1003 s
->section
= &fake_common_section
;
1006 case LDPK_WEAKUNDEF
:
1007 s
->section
= bfd_und_section_ptr
;
1011 s
->section
= &fake_section
;
1013 /* Use real LTO symbols if possible. */
1014 for (j
= 0; j
< real_nsyms
; j
++)
1015 if (real_syms
[j
]->name
1016 && strcmp (syms
[i
].name
, real_syms
[j
]->name
) == 0)
1018 s
->section
= real_syms
[j
]->section
;
1026 s
->udata
.p
= (void *) &syms
[i
];
1033 bfd_plugin_print_symbol (bfd
*abfd ATTRIBUTE_UNUSED
,
1034 PTR afile ATTRIBUTE_UNUSED
,
1035 asymbol
*symbol ATTRIBUTE_UNUSED
,
1036 bfd_print_symbol_type how ATTRIBUTE_UNUSED
)
1042 bfd_plugin_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
1046 bfd_symbol_info (symbol
, ret
);
1049 /* Make an empty symbol. */
1052 bfd_plugin_make_empty_symbol (bfd
*abfd
)
1054 asymbol
*new_symbol
= bfd_zalloc (abfd
, sizeof (asymbol
));
1055 if (new_symbol
== NULL
)
1057 new_symbol
->the_bfd
= abfd
;
1062 bfd_plugin_sizeof_headers (bfd
*a ATTRIBUTE_UNUSED
,
1063 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
1070 bfd_plugin_close_and_cleanup (bfd
*abfd
)
1072 struct plugin_data_struct
*plugin_data
;
1074 if (abfd
->format
!= bfd_archive
1075 && (plugin_data
= abfd
->tdata
.plugin_data
))
1077 if (plugin_data
->real_bfd
)
1078 bfd_close (plugin_data
->real_bfd
);
1080 if (plugin_data
->real_syms
)
1081 free (plugin_data
->real_syms
);
1084 return _bfd_generic_close_and_cleanup (abfd
);
1087 const bfd_target plugin_vec
=
1089 "plugin", /* Name. */
1090 bfd_target_unknown_flavour
,
1091 BFD_ENDIAN_LITTLE
, /* Target byte order. */
1092 BFD_ENDIAN_LITTLE
, /* Target headers byte order. */
1093 (HAS_RELOC
| EXEC_P
| /* Object flags. */
1094 HAS_LINENO
| HAS_DEBUG
|
1095 HAS_SYMS
| HAS_LOCALS
| DYNAMIC
| WP_TEXT
| D_PAGED
),
1096 (SEC_CODE
| SEC_DATA
| SEC_ROM
| SEC_HAS_CONTENTS
1097 | SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* Section flags. */
1098 0, /* symbol_leading_char. */
1099 '/', /* ar_pad_char. */
1100 15, /* ar_max_namelen. */
1101 255, /* match priority. */
1103 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
1104 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
1105 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
1106 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
1107 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
1108 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
1110 { /* bfd_check_format. */
1112 bfd_plugin_object_p
,
1113 bfd_generic_archive_p
,
1116 { /* bfd_set_format. */
1117 _bfd_bool_bfd_false_error
,
1118 _bfd_bool_bfd_false_error
,
1119 _bfd_generic_mkarchive
,
1120 _bfd_bool_bfd_false_error
,
1122 { /* bfd_write_contents. */
1123 _bfd_bool_bfd_false_error
,
1124 _bfd_bool_bfd_false_error
,
1125 _bfd_write_archive_contents
,
1126 _bfd_bool_bfd_false_error
,
1129 BFD_JUMP_TABLE_GENERIC (bfd_plugin
),
1130 BFD_JUMP_TABLE_COPY (bfd_plugin
),
1131 BFD_JUMP_TABLE_CORE (bfd_plugin
),
1132 #ifdef USE_64_BIT_ARCHIVE
1133 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_64_bit
),
1135 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
1137 BFD_JUMP_TABLE_SYMBOLS (bfd_plugin
),
1138 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs
),
1139 BFD_JUMP_TABLE_WRITE (bfd_plugin
),
1140 BFD_JUMP_TABLE_LINK (bfd_plugin
),
1141 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
1145 NULL
/* backend_data. */
1147 #endif /* BFD_SUPPORTS_PLUGIN */