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. */
130 ld_plugin_claim_file_handler claim_file
;
131 ld_plugin_all_symbols_read_handler all_symbols_read
;
132 ld_plugin_all_symbols_read_handler cleanup_handler
;
133 char *resolution_file
;
134 char *resolution_option
;
139 const struct ld_plugin_symbol
*lto_syms
;
141 struct plugin_list_entry
*next
;
143 /* These can be reused for all IR objects. */
144 const char *plugin_name
;
148 bfd_boolean initialized
;
151 /* Use GCC LTO wrapper to covert LTO IR object to the real object. */
154 get_lto_wrapper (struct plugin_list_entry
*plugin
)
157 const char *real_name
;
158 const char *base_name
;
160 const char *target_start
= NULL
;
161 const char *target_end
= NULL
;
162 size_t target_length
= 0;
166 char dir_seperator
= '\0';
167 char *resolution_file
;
169 if (plugin
->initialized
)
171 if (plugin
->lto_wrapper
)
173 resolution_file
= make_temp_file (".res");
176 plugin
->resolution_file
= resolution_file
;
177 plugin
->resolution_option
= concat ("-fresolution=",
178 resolution_file
, NULL
);
183 /* Something is wrong. Give up. */
185 free (plugin
->lto_wrapper
);
186 free (plugin
->gcc_env
);
188 plugin
->gcc_env
= NULL
;
189 plugin
->lto_wrapper
= NULL
;
196 plugin
->initialized
= TRUE
;
198 /* Check for PREFIX/libexec/gcc/TARGET/VERSION/liblto_plugin.so. */
199 real_name
= lrealpath (plugin
->plugin_name
);
200 base_name
= lbasename (real_name
);
202 /* The directory length in plugin pathname. */
203 length
= base_name
- real_name
;
205 /* Skip if there is no PREFIX. */
209 p
= (char *) real_name
+ length
- 1;
210 if (IS_DIR_SEPARATOR (*p
))
213 for (; p
!= real_name
; p
--)
214 if (IS_DIR_SEPARATOR (*p
))
221 target_start
= p
+ 1;
222 target_length
= target_end
- target_start
;
232 /* Skip if there is no TARGET nor PREFIX. */
233 if (!target_length
|| !dir_seperator
)
236 #ifdef HAVE_EXECUTABLE_SUFFIX
237 # define GCC_EXECUTABLE "gcc" EXECUTABLE_SUFFIX
238 # define LTO_WRAPPER_EXECUTABLE "lto-wrapper" EXECUTABLE_SUFFIX
240 # define GCC_EXECUTABLE "gcc"
241 # define LTO_WRAPPER_EXECUTABLE "lto-wrapper"
243 gcc_name
= bfd_malloc (length
+ target_length
244 + sizeof (GCC_EXECUTABLE
));
245 if (gcc_name
== NULL
)
247 memcpy (gcc_name
, real_name
, length
);
249 /* Get PREFIX/bin/. */
250 p
+= gcc_name
- real_name
;
251 memcpy (p
+ 1, "bin", 3);
252 p
[4] = dir_seperator
;
254 /* Try PREFIX/bin/TARGET-gcc first. */
255 memcpy (p
+ 5, target_start
, target_length
);
256 p
[5 + target_length
] = '-';
257 memcpy (p
+ 5 + target_length
+ 1, GCC_EXECUTABLE
,
258 sizeof (GCC_EXECUTABLE
));
259 if (stat (gcc_name
, &st
) != 0 || !S_ISREG (st
.st_mode
))
261 /* Then try PREFIX/bin/gcc. */
262 memcpy (p
+ 5, GCC_EXECUTABLE
, sizeof (GCC_EXECUTABLE
));
263 if (stat (gcc_name
, &st
) != 0 || !S_ISREG (st
.st_mode
))
270 /* lto-wrapper should be in the same directory with LTO plugin. */
271 wrapper_name
= bfd_malloc (length
+ sizeof (LTO_WRAPPER_EXECUTABLE
));
272 if (wrapper_name
== NULL
)
277 memcpy (wrapper_name
, real_name
, length
);
278 memcpy (wrapper_name
+ length
, LTO_WRAPPER_EXECUTABLE
,
279 sizeof (LTO_WRAPPER_EXECUTABLE
));
280 if (stat (wrapper_name
, &st
) == 0 && S_ISREG (st
.st_mode
))
282 resolution_file
= make_temp_file (".res");
285 plugin
->gcc
= gcc_name
;
286 plugin
->lto_wrapper
= wrapper_name
;
287 plugin
->gcc_env
= concat ("COLLECT_GCC=", gcc_name
, NULL
);
288 plugin
->resolution_file
= resolution_file
;
289 plugin
->resolution_option
= concat ("-fresolution=",
290 resolution_file
, NULL
);
300 /* Set environment variables for GCC LTO wrapper to covert LTO IR
301 object to the real object. */
304 setup_lto_wrapper_env (struct plugin_list_entry
*plugin
)
306 return (putenv (plugin
->gcc_env
)
307 || putenv ("COLLECT_GCC_OPTIONS="));
310 static struct plugin_list_entry
*plugin_list
= NULL
;
311 static struct plugin_list_entry
*current_plugin
= NULL
;
313 /* Register a claim-file handler. */
315 static enum ld_plugin_status
316 register_claim_file (ld_plugin_claim_file_handler handler
)
318 current_plugin
->claim_file
= handler
;
322 /* Register an all-symbols-read handler. */
324 static enum ld_plugin_status
325 register_all_symbols_read (ld_plugin_all_symbols_read_handler handler
)
327 current_plugin
->all_symbols_read
= handler
;
331 /* Register a cleanup handler. */
333 static enum ld_plugin_status
334 register_cleanup (ld_plugin_all_symbols_read_handler handler
)
336 current_plugin
->cleanup_handler
= handler
;
340 /* Get the symbol resolution info for a plugin-claimed input file. */
342 static enum ld_plugin_status
343 get_symbols (const void *handle ATTRIBUTE_UNUSED
, int nsyms
,
344 struct ld_plugin_symbol
*syms
)
349 for (n
= 0; n
< nsyms
; n
++)
358 syms
[n
].resolution
= LDPR_UNDEF
;
363 /* Tell plugin that LTO symbol has references from regular
365 syms
[n
].resolution
= LDPR_PREVAILING_DEF
;
374 /* Add a new (real) input file generated by a plugin. */
376 static enum ld_plugin_status
377 add_input_file (const char *pathname
)
379 /* Get symbols from the real LTO object. */
385 bfd_boolean lto_symbol_found
= FALSE
;
386 const struct ld_plugin_symbol
*lto_syms
;
390 rbfd
= bfd_openr (pathname
, NULL
);
391 if (!bfd_check_format_matches (rbfd
, bfd_object
, &matching
))
394 real_symsize
= bfd_get_symtab_upper_bound (rbfd
);
395 if (real_symsize
< 0)
398 real_syms
= (asymbol
**) bfd_malloc (real_symsize
);
401 real_nsyms
= bfd_canonicalize_symtab (rbfd
, real_syms
);
405 /* NB: LTO plugin may generate more than one real object from one
406 LTO IR object. We use the one which contains LTO symbols. */
407 lto_syms
= current_plugin
->lto_syms
;
408 lto_nsyms
= current_plugin
->lto_nsyms
;
409 for (i
= 0; i
< lto_nsyms
; i
++)
410 for (j
= 0; j
< real_nsyms
; j
++)
411 if (real_syms
[j
]->name
412 && strcmp (lto_syms
[i
].name
, real_syms
[j
]->name
) == 0)
414 lto_symbol_found
= TRUE
;
419 if (lto_symbol_found
)
421 current_plugin
->real_nsyms
= real_nsyms
;
422 current_plugin
->real_syms
= real_syms
;
423 /* NB: We can't close RBFD which own the real symbol info. */
424 current_plugin
->real_bfd
= rbfd
;
435 static enum ld_plugin_status
436 add_symbols (void * handle
,
438 const struct ld_plugin_symbol
* syms
)
441 struct plugin_data_struct
*plugin_data
=
442 bfd_alloc (abfd
, sizeof (plugin_data_struct
));
446 struct ld_plugin_symbol
*sym_info
;
448 size_t sym_info_size
, name_length
;
451 memset (plugin_data
, 0, sizeof (*plugin_data
));
453 abfd
->tdata
.plugin_data
= plugin_data
;
455 /* NB: LTO symbols are owned by LTO plugin. Create a copy so
456 that we can use it in bfd_plugin_canonicalize_symtab. */
457 sym_info_size
= nsyms
* sizeof (*syms
);
459 /* Allocate a string table */
460 for (i
= 0; i
< nsyms
; i
++)
461 sym_info_size
+= strlen (syms
[i
].name
) + 1;
463 sym_info
= bfd_alloc (abfd
, sym_info_size
);
466 /* Copy symbol table. */
467 memcpy (sym_info
, syms
, nsyms
* sizeof (*syms
));
469 /* Copy symbol names in symbol table. */
470 strtab
= (char *) (sym_info
+ nsyms
);
471 for (i
= 0; i
< nsyms
; i
++)
473 name_length
= strlen (syms
[i
].name
);
474 memcpy (strtab
, syms
[i
].name
, name_length
+ 1);
475 sym_info
[i
].name
= strtab
;
476 strtab
+= name_length
+ 1;
479 plugin_data
->nsyms
= nsyms
;
480 plugin_data
->syms
= sym_info
;
482 current_plugin
->lto_nsyms
= nsyms
;
483 current_plugin
->lto_syms
= sym_info
;
488 abfd
->flags
|= HAS_SYMS
;
493 static const char *plugin_program_name
;
496 bfd_plugin_set_program_name (const char *program_name
)
498 plugin_program_name
= program_name
;
502 bfd_plugin_open_input (bfd
*ibfd
, struct ld_plugin_input_file
*file
)
507 while (iobfd
->my_archive
508 && !bfd_is_thin_archive (iobfd
->my_archive
))
509 iobfd
= iobfd
->my_archive
;
510 file
->name
= iobfd
->filename
;
512 if (!iobfd
->iostream
&& !bfd_open_file (iobfd
))
515 /* The plugin API expects that the file descriptor won't be closed
516 and reused as done by the bfd file cache. So open it again.
517 dup isn't good enough. plugin IO uses lseek/read while BFD uses
518 fseek/fread. It isn't wise to mix the unistd and stdio calls on
519 the same underlying file descriptor. */
520 file
->fd
= open (file
->name
, O_RDONLY
| O_BINARY
);
526 struct stat stat_buf
;
528 if (fstat (file
->fd
, &stat_buf
))
535 file
->filesize
= stat_buf
.st_size
;
539 file
->offset
= ibfd
->origin
;
540 file
->filesize
= arelt_size (ibfd
);
546 try_claim (bfd
*abfd
)
549 struct ld_plugin_input_file file
;
552 if (bfd_plugin_open_input (abfd
, &file
)
553 && current_plugin
->claim_file
)
555 current_plugin
->claim_file (&file
, &claimed
);
558 if (current_plugin
->all_symbols_read
)
560 struct plugin_data_struct
*plugin_data
561 = abfd
->tdata
.plugin_data
;
564 /* Get real symbols from LTO wrapper. */
565 current_plugin
->all_symbols_read ();
567 /* Copy real symbols to plugin_data. */
568 plugin_data
->real_bfd
= current_plugin
->real_bfd
;
569 plugin_data
->real_nsyms
= current_plugin
->real_nsyms
;
570 plugin_data
->real_syms
= current_plugin
->real_syms
;
572 /* Clean up LTO plugin. */
573 if (current_plugin
->cleanup_handler
)
574 current_plugin
->cleanup_handler ();
582 if (current_plugin
->lto_wrapper
)
584 /* Clean up for LTO wrapper. NB: Resolution file and option
585 have been created regardless if an IR object is claimed or
587 unlink (current_plugin
->resolution_file
);
588 free (current_plugin
->resolution_option
);
595 try_load_plugin (const char *pname
, bfd
*abfd
, int *has_plugin_p
)
597 void *plugin_handle
= NULL
;
598 struct ld_plugin_tv tv
[12];
600 ld_plugin_onload onload
;
601 enum ld_plugin_status status
;
602 struct plugin_list_entry
*plugin_list_iter
;
606 /* NB: Each object is independent. Reuse the previous plugin from
607 the last run will lead to wrong result. */
610 if (current_plugin
->handle
)
611 dlclose (current_plugin
->handle
);
612 memset (current_plugin
, 0,
613 offsetof (struct plugin_list_entry
, next
));
614 current_plugin
= NULL
;
617 plugin_handle
= dlopen (pname
, RTLD_NOW
);
620 _bfd_error_handler ("%s\n", dlerror ());
624 for (plugin_list_iter
= plugin_list
;
626 plugin_list_iter
= plugin_list_iter
->next
)
627 if (strcmp (plugin_list_iter
->plugin_name
, pname
) == 0)
630 if (plugin_list_iter
== NULL
)
632 size_t length_plugin_name
= strlen (pname
) + 1;
633 char *plugin_name
= bfd_malloc (length_plugin_name
);
634 if (plugin_name
== NULL
)
636 plugin_list_iter
= bfd_malloc (sizeof *plugin_list_iter
);
637 if (plugin_list_iter
== NULL
)
642 /* Make a copy of PNAME since PNAME from load_plugin () will be
644 memcpy (plugin_name
, pname
, length_plugin_name
);
645 memset (plugin_list_iter
, 0, sizeof (*plugin_list_iter
));
646 plugin_list_iter
->plugin_name
= plugin_name
;
647 plugin_list_iter
->next
= plugin_list
;
648 plugin_list
= plugin_list_iter
;
651 plugin_list_iter
->handle
= plugin_handle
;
653 onload
= dlsym (plugin_handle
, "onload");
658 tv
[i
].tv_tag
= LDPT_MESSAGE
;
659 tv
[i
].tv_u
.tv_message
= message
;
662 tv
[i
].tv_tag
= LDPT_REGISTER_CLAIM_FILE_HOOK
;
663 tv
[i
].tv_u
.tv_register_claim_file
= register_claim_file
;
666 tv
[i
].tv_tag
= LDPT_ADD_SYMBOLS
;
667 tv
[i
].tv_u
.tv_add_symbols
= add_symbols
;
669 if (get_lto_wrapper (plugin_list_iter
))
672 tv
[i
].tv_tag
= LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK
;
673 tv
[i
].tv_u
.tv_register_all_symbols_read
= register_all_symbols_read
;
676 tv
[i
].tv_tag
= LDPT_REGISTER_CLEANUP_HOOK
;
677 tv
[i
].tv_u
.tv_register_cleanup
= register_cleanup
;
680 tv
[i
].tv_tag
= LDPT_GET_SYMBOLS
;
681 tv
[i
].tv_u
.tv_get_symbols
= get_symbols
;
684 tv
[i
].tv_tag
= LDPT_GET_SYMBOLS_V2
;
685 tv
[i
].tv_u
.tv_get_symbols
= get_symbols
;
688 tv
[i
].tv_tag
= LDPT_OPTION
;
689 tv
[i
].tv_u
.tv_string
= plugin_list_iter
->lto_wrapper
;
692 tv
[i
].tv_tag
= LDPT_OPTION
;
693 tv
[i
].tv_u
.tv_string
= plugin_list_iter
->resolution_option
;
696 tv
[i
].tv_tag
= LDPT_LINKER_OUTPUT
;
697 tv
[i
].tv_u
.tv_val
= LDPO_EXEC
;
700 tv
[i
].tv_tag
= LDPT_ADD_INPUT_FILE
;
701 tv
[i
].tv_u
.tv_add_input_file
= add_input_file
;
705 tv
[i
].tv_tag
= LDPT_NULL
;
706 tv
[i
].tv_u
.tv_val
= 0;
708 current_plugin
= plugin_list_iter
;
710 /* LTO plugin will call handler hooks to set up plugin handlers. */
711 status
= (*onload
)(tv
);
713 if (status
!= LDPS_OK
)
716 if (current_plugin
->lto_wrapper
717 && setup_lto_wrapper_env (current_plugin
))
722 abfd
->plugin_format
= bfd_plugin_no
;
724 if (!current_plugin
->claim_file
)
727 if (!try_claim (abfd
))
730 abfd
->plugin_format
= bfd_plugin_yes
;
734 /* There may be plugin libraries in lib/bfd-plugins. */
736 static int has_plugin
= -1;
738 static const bfd_target
*(*ld_plugin_object_p
) (bfd
*);
740 static const char *plugin_name
;
743 bfd_plugin_set_plugin (const char *p
)
746 has_plugin
= p
!= NULL
;
749 /* Return TRUE if a plugin library is used. */
752 bfd_plugin_specified_p (void)
754 return has_plugin
> 0;
757 /* Return TRUE if ABFD can be claimed by linker LTO plugin. */
760 bfd_link_plugin_object_p (bfd
*abfd
)
762 if (ld_plugin_object_p
)
763 return ld_plugin_object_p (abfd
) != NULL
;
767 extern const bfd_target plugin_vec
;
769 /* Return TRUE if TARGET is a pointer to plugin_vec. */
772 bfd_plugin_target_p (const bfd_target
*target
)
774 return target
== &plugin_vec
;
777 /* Register OBJECT_P to be used by bfd_plugin_object_p. */
780 register_ld_plugin_object_p (const bfd_target
*(*object_p
) (bfd
*))
782 ld_plugin_object_p
= object_p
;
786 load_plugin (bfd
*abfd
)
788 /* The intent was to search ${libdir}/bfd-plugins for plugins, but
789 unfortunately the original implementation wasn't precisely that
790 when configuring binutils using --libdir. Search in the proper
791 path first, then the old one for backwards compatibility. */
792 static const char *path
[]
793 = { LIBDIR
"/bfd-plugins", BINDIR
"/../lib/bfd-plugins" };
802 return try_load_plugin (plugin_name
, abfd
, &has_plugin
);
804 if (plugin_program_name
== NULL
)
807 /* Try not to search the same dir twice, by looking at st_dev and
808 st_ino for the dir. If we are on a file system that always sets
809 st_ino to zero or the actual st_ino is zero we might waste some
810 time, but that doesn't matter too much. */
813 for (i
= 0; i
< sizeof (path
) / sizeof (path
[0]); i
++)
815 char *plugin_dir
= make_relative_prefix (plugin_program_name
,
823 if (stat (plugin_dir
, &st
) == 0
824 && S_ISDIR (st
.st_mode
)
825 && !(last_st
.st_dev
== st
.st_dev
826 && last_st
.st_ino
== st
.st_ino
828 && (d
= opendir (plugin_dir
)) != NULL
)
832 last_st
.st_dev
= st
.st_dev
;
833 last_st
.st_ino
= st
.st_ino
;
834 while ((ent
= readdir (d
)) != NULL
)
838 full_name
= concat (plugin_dir
, "/", ent
->d_name
, NULL
);
839 if (stat (full_name
, &st
) == 0 && S_ISREG (st
.st_mode
))
843 found
= try_load_plugin (full_name
, abfd
, &valid_plugin
);
845 has_plugin
= valid_plugin
;
863 static const bfd_target
*
864 bfd_plugin_object_p (bfd
*abfd
)
866 if (ld_plugin_object_p
)
867 return ld_plugin_object_p (abfd
);
869 if (abfd
->plugin_format
== bfd_plugin_unknown
&& !load_plugin (abfd
))
872 return abfd
->plugin_format
== bfd_plugin_yes
? abfd
->xvec
: NULL
;
875 /* Copy any private info we understand from the input bfd
876 to the output bfd. */
879 bfd_plugin_bfd_copy_private_bfd_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
880 bfd
*obfd ATTRIBUTE_UNUSED
)
886 /* Copy any private info we understand from the input section
887 to the output section. */
890 bfd_plugin_bfd_copy_private_section_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
891 asection
*isection ATTRIBUTE_UNUSED
,
892 bfd
*obfd ATTRIBUTE_UNUSED
,
893 asection
*osection ATTRIBUTE_UNUSED
)
899 /* Copy any private info we understand from the input symbol
900 to the output symbol. */
903 bfd_plugin_bfd_copy_private_symbol_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
904 asymbol
*isymbol ATTRIBUTE_UNUSED
,
905 bfd
*obfd ATTRIBUTE_UNUSED
,
906 asymbol
*osymbol ATTRIBUTE_UNUSED
)
913 bfd_plugin_bfd_print_private_bfd_data (bfd
*abfd ATTRIBUTE_UNUSED
, PTR ptr ATTRIBUTE_UNUSED
)
920 bfd_plugin_core_file_failing_command (bfd
*abfd ATTRIBUTE_UNUSED
)
927 bfd_plugin_core_file_failing_signal (bfd
*abfd ATTRIBUTE_UNUSED
)
934 bfd_plugin_core_file_pid (bfd
*abfd ATTRIBUTE_UNUSED
)
941 bfd_plugin_get_symtab_upper_bound (bfd
*abfd
)
943 struct plugin_data_struct
*plugin_data
= abfd
->tdata
.plugin_data
;
944 long nsyms
= plugin_data
->nsyms
;
946 BFD_ASSERT (nsyms
>= 0);
948 return ((nsyms
+ 1) * sizeof (asymbol
*));
952 convert_flags (const struct ld_plugin_symbol
*sym
)
963 return BSF_GLOBAL
| BSF_WEAK
;
972 bfd_plugin_canonicalize_symtab (bfd
*abfd
,
975 struct plugin_data_struct
*plugin_data
= abfd
->tdata
.plugin_data
;
976 long nsyms
= plugin_data
->nsyms
;
977 const struct ld_plugin_symbol
*syms
= plugin_data
->syms
;
978 static asection fake_section
979 = BFD_FAKE_SECTION (fake_section
, NULL
, "plug", 0,
980 SEC_ALLOC
| SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
);
981 static asection fake_common_section
982 = BFD_FAKE_SECTION (fake_common_section
, NULL
, "plug", 0, SEC_IS_COMMON
);
987 real_syms
= plugin_data
->real_syms
;
989 real_nsyms
= plugin_data
->real_nsyms
;
993 for (i
= 0; i
< nsyms
; i
++)
995 asymbol
*s
= bfd_alloc (abfd
, sizeof (asymbol
));
1001 s
->name
= syms
[i
].name
;
1003 s
->flags
= convert_flags (&syms
[i
]);
1004 switch (syms
[i
].def
)
1007 s
->section
= &fake_common_section
;
1010 case LDPK_WEAKUNDEF
:
1011 s
->section
= bfd_und_section_ptr
;
1015 s
->section
= &fake_section
;
1017 /* Use real LTO symbols if possible. */
1018 for (j
= 0; j
< real_nsyms
; j
++)
1019 if (real_syms
[j
]->name
1020 && strcmp (syms
[i
].name
, real_syms
[j
]->name
) == 0)
1022 s
->section
= real_syms
[j
]->section
;
1030 s
->udata
.p
= (void *) &syms
[i
];
1037 bfd_plugin_print_symbol (bfd
*abfd ATTRIBUTE_UNUSED
,
1038 PTR afile ATTRIBUTE_UNUSED
,
1039 asymbol
*symbol ATTRIBUTE_UNUSED
,
1040 bfd_print_symbol_type how ATTRIBUTE_UNUSED
)
1046 bfd_plugin_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
1050 bfd_symbol_info (symbol
, ret
);
1053 /* Make an empty symbol. */
1056 bfd_plugin_make_empty_symbol (bfd
*abfd
)
1058 asymbol
*new_symbol
= bfd_zalloc (abfd
, sizeof (asymbol
));
1059 if (new_symbol
== NULL
)
1061 new_symbol
->the_bfd
= abfd
;
1066 bfd_plugin_sizeof_headers (bfd
*a ATTRIBUTE_UNUSED
,
1067 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
1074 bfd_plugin_close_and_cleanup (bfd
*abfd
)
1076 struct plugin_data_struct
*plugin_data
;
1078 if (abfd
->format
!= bfd_archive
1079 && (plugin_data
= abfd
->tdata
.plugin_data
))
1081 if (plugin_data
->real_bfd
)
1082 bfd_close (plugin_data
->real_bfd
);
1084 if (plugin_data
->real_syms
)
1085 free (plugin_data
->real_syms
);
1088 return _bfd_generic_close_and_cleanup (abfd
);
1091 const bfd_target plugin_vec
=
1093 "plugin", /* Name. */
1094 bfd_target_unknown_flavour
,
1095 BFD_ENDIAN_LITTLE
, /* Target byte order. */
1096 BFD_ENDIAN_LITTLE
, /* Target headers byte order. */
1097 (HAS_RELOC
| EXEC_P
| /* Object flags. */
1098 HAS_LINENO
| HAS_DEBUG
|
1099 HAS_SYMS
| HAS_LOCALS
| DYNAMIC
| WP_TEXT
| D_PAGED
),
1100 (SEC_CODE
| SEC_DATA
| SEC_ROM
| SEC_HAS_CONTENTS
1101 | SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* Section flags. */
1102 0, /* symbol_leading_char. */
1103 '/', /* ar_pad_char. */
1104 15, /* ar_max_namelen. */
1105 255, /* match priority. */
1107 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
1108 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
1109 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
1110 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
1111 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
1112 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
1114 { /* bfd_check_format. */
1116 bfd_plugin_object_p
,
1117 bfd_generic_archive_p
,
1120 { /* bfd_set_format. */
1121 _bfd_bool_bfd_false_error
,
1122 _bfd_bool_bfd_false_error
,
1123 _bfd_generic_mkarchive
,
1124 _bfd_bool_bfd_false_error
,
1126 { /* bfd_write_contents. */
1127 _bfd_bool_bfd_false_error
,
1128 _bfd_bool_bfd_false_error
,
1129 _bfd_write_archive_contents
,
1130 _bfd_bool_bfd_false_error
,
1133 BFD_JUMP_TABLE_GENERIC (bfd_plugin
),
1134 BFD_JUMP_TABLE_COPY (bfd_plugin
),
1135 BFD_JUMP_TABLE_CORE (bfd_plugin
),
1136 #ifdef USE_64_BIT_ARCHIVE
1137 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_64_bit
),
1139 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
1141 BFD_JUMP_TABLE_SYMBOLS (bfd_plugin
),
1142 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs
),
1143 BFD_JUMP_TABLE_WRITE (bfd_plugin
),
1144 BFD_JUMP_TABLE_LINK (bfd_plugin
),
1145 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
1149 NULL
/* backend_data. */
1151 #endif /* BFD_SUPPORTS_PLUGIN */