Automatic date update in version.in
[deliverable/binutils-gdb.git] / bfd / plugin.c
CommitLineData
ce3c775b 1/* Plugin support for BFD.
b3adc24a 2 Copyright (C) 2009-2020 Free Software Foundation, Inc.
ce3c775b
NC
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
691bf19c 21#include "sysdep.h"
98950613
AM
22#include "bfd.h"
23
24#if BFD_SUPPORTS_PLUGINS
25
ce3c775b 26#include <assert.h>
17d5dae2 27#ifdef HAVE_DLFCN_H
ce3c775b 28#include <dlfcn.h>
17d5dae2
KT
29#elif defined (HAVE_WINDOWS_H)
30#include <windows.h>
31#else
32#error Unknown how to handle dynamic-load-libraries.
33#endif
ce3c775b
NC
34#include <stdarg.h>
35#include "plugin-api.h"
ce3c775b
NC
36#include "plugin.h"
37#include "libbfd.h"
38#include "libiberty.h"
39#include <dirent.h>
40
17d5dae2
KT
41#if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
42
43#define RTLD_NOW 0 /* Dummy value. */
44
45static void *
46dlopen (const char *file, int mode ATTRIBUTE_UNUSED)
47{
48 return LoadLibrary (file);
49}
50
51static void *
52dlsym (void *handle, const char *name)
53{
54 return GetProcAddress (handle, name);
55}
56
57static int ATTRIBUTE_UNUSED
58dlclose (void *handle)
59{
60 FreeLibrary (handle);
61 return 0;
62}
63
64static const char *
65dlerror (void)
66{
67 return "Unable to load DLL.";
68}
69
70#endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */
71
07d6d2b8
AM
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
ce3c775b 75#define bfd_plugin_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
07d6d2b8
AM
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
ce3c775b 80#define bfd_plugin_core_file_matches_executable_p generic_core_file_matches_executable_p
07d6d2b8 81#define bfd_plugin_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
d00dd7dc 82#define bfd_plugin_bfd_is_target_special_symbol _bfd_bool_bfd_asymbol_false
07d6d2b8
AM
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
60bb06bc 87#define bfd_plugin_get_symbol_version_string _bfd_nosymbols_get_symbol_version_string
07d6d2b8
AM
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
ce3c775b 93#define bfd_plugin_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
07d6d2b8
AM
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
cb7f4b29 104#define bfd_plugin_bfd_group_name bfd_generic_group_name
07d6d2b8
AM
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
34a87bb0 108#define bfd_plugin_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
07d6d2b8 109#define bfd_plugin_bfd_define_start_stop bfd_generic_define_start_stop
69a630b5 110#define bfd_plugin_bfd_copy_link_hash_symbol_type _bfd_generic_copy_link_hash_symbol_type
07d6d2b8 111#define bfd_plugin_bfd_link_check_relocs _bfd_generic_link_check_relocs
ce3c775b
NC
112
113static enum ld_plugin_status
114message (int level ATTRIBUTE_UNUSED,
115 const char * format, ...)
116{
117 va_list args;
118 va_start (args, format);
119 printf ("bfd plugin: ");
120 vprintf (format, args);
d9b21463 121 putchar ('\n');
ce3c775b
NC
122 va_end (args);
123 return LDPS_OK;
124}
125
0aa99dcd
L
126struct plugin_list_entry
127{
128 /* These must be initialized for each IR object with LTO wrapper. */
0aa99dcd
L
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;
134 bfd *real_bfd;
135 long real_nsyms;
136 asymbol **real_syms;
137 int lto_nsyms;
138 const struct ld_plugin_symbol *lto_syms;
139
140 struct plugin_list_entry *next;
141
142 /* These can be reused for all IR objects. */
143 const char *plugin_name;
144 char *gcc;
145 char *lto_wrapper;
146 char *gcc_env;
147 bfd_boolean initialized;
148};
149
150/* Use GCC LTO wrapper to covert LTO IR object to the real object. */
151
152static bfd_boolean
153get_lto_wrapper (struct plugin_list_entry *plugin)
154{
155 struct stat st;
156 const char *real_name;
157 const char *base_name;
158 size_t length;
159 const char *target_start = NULL;
160 const char *target_end = NULL;
161 size_t target_length = 0;
162 char *gcc_name;
163 char *wrapper_name;
164 char *p;
165 char dir_seperator = '\0';
166 char *resolution_file;
167
168 if (plugin->initialized)
169 {
170 if (plugin->lto_wrapper)
171 {
172 resolution_file = make_temp_file (".res");
173 if (resolution_file)
174 {
175 plugin->resolution_file = resolution_file;
176 plugin->resolution_option = concat ("-fresolution=",
177 resolution_file, NULL);
178 return TRUE;
179 }
180 else
181 {
182 /* Something is wrong. Give up. */
183 free (plugin->gcc);
184 free (plugin->lto_wrapper);
185 free (plugin->gcc_env);
186 plugin->gcc = NULL;
187 plugin->gcc_env = NULL;
188 plugin->lto_wrapper = NULL;
189 }
190 }
191
192 return FALSE;
193 }
194
195 plugin->initialized = TRUE;
196
197 /* Check for PREFIX/libexec/gcc/TARGET/VERSION/liblto_plugin.so. */
198 real_name = lrealpath (plugin->plugin_name);
199 base_name = lbasename (real_name);
200
201 /* The directory length in plugin pathname. */
202 length = base_name - real_name;
203
204 /* Skip if there is no PREFIX. */
205 if (!length)
206 return FALSE;
207
208 p = (char *) real_name + length - 1;
209 if (IS_DIR_SEPARATOR (*p))
210 {
211 int level = 0;
212 for (; p != real_name; p--)
213 if (IS_DIR_SEPARATOR (*p))
214 {
215 level++;
216 if (level == 2)
217 target_end = p;
218 else if (level == 3)
219 {
220 target_start = p + 1;
221 target_length = target_end - target_start;
222 }
223 else if (level == 5)
224 {
225 dir_seperator = *p;
226 break;
227 }
228 }
229 }
230
231 /* Skip if there is no TARGET nor PREFIX. */
232 if (!target_length || !dir_seperator)
233 return FALSE;
234
235#ifdef HAVE_EXECUTABLE_SUFFIX
236# define GCC_EXECUTABLE "gcc" EXECUTABLE_SUFFIX
237# define LTO_WRAPPER_EXECUTABLE "lto-wrapper" EXECUTABLE_SUFFIX
238#else
239# define GCC_EXECUTABLE "gcc"
240# define LTO_WRAPPER_EXECUTABLE "lto-wrapper"
241#endif
242 gcc_name = bfd_malloc (length + target_length
243 + sizeof (GCC_EXECUTABLE));
244 if (gcc_name == NULL)
245 return FALSE;
246 memcpy (gcc_name, real_name, length);
247
248 /* Get PREFIX/bin/. */
249 p += gcc_name - real_name;
250 memcpy (p + 1, "bin", 3);
251 p[4] = dir_seperator;
252
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))
259 {
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))
263 {
264 free (gcc_name);
265 return FALSE;
266 }
267 }
268
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)
272 {
273 free (gcc_name);
274 return FALSE;
275 }
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))
280 {
281 resolution_file = make_temp_file (".res");
282 if (resolution_file)
283 {
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);
290 return TRUE;
291 }
292 }
293
294 free (gcc_name);
295 free (wrapper_name);
296 return FALSE;
297}
298
299/* Set environment variables for GCC LTO wrapper to covert LTO IR
300 object to the real object. */
301
302static int
303setup_lto_wrapper_env (struct plugin_list_entry *plugin)
304{
305 return (putenv (plugin->gcc_env)
306 || putenv ("COLLECT_GCC_OPTIONS="));
307}
308
309static struct plugin_list_entry *plugin_list = NULL;
310static struct plugin_list_entry *current_plugin = NULL;
311
ce3c775b 312/* Register a claim-file handler. */
ce3c775b
NC
313
314static enum ld_plugin_status
315register_claim_file (ld_plugin_claim_file_handler handler)
316{
0aa99dcd
L
317 current_plugin->claim_file = handler;
318 return LDPS_OK;
319}
320
321/* Register an all-symbols-read handler. */
322
323static enum ld_plugin_status
324register_all_symbols_read (ld_plugin_all_symbols_read_handler handler)
325{
326 current_plugin->all_symbols_read = handler;
327 return LDPS_OK;
328}
329
330/* Register a cleanup handler. */
331
332static enum ld_plugin_status
333register_cleanup (ld_plugin_all_symbols_read_handler handler)
334{
335 current_plugin->cleanup_handler = handler;
336 return LDPS_OK;
337}
338
339/* Get the symbol resolution info for a plugin-claimed input file. */
340
341static enum ld_plugin_status
342get_symbols (const void *handle ATTRIBUTE_UNUSED, int nsyms,
343 struct ld_plugin_symbol *syms)
344{
345 if (syms)
346 {
347 int n;
348 for (n = 0; n < nsyms; n++)
349 {
350 switch (syms[n].def)
351 {
352 default:
353 BFD_ASSERT (0);
354 break;
355 case LDPK_UNDEF:
356 case LDPK_WEAKUNDEF:
357 syms[n].resolution = LDPR_UNDEF;
358 break;
359 case LDPK_DEF:
360 case LDPK_WEAKDEF:
361 case LDPK_COMMON:
362 /* Tell plugin that LTO symbol has references from regular
363 object code. */
364 syms[n].resolution = LDPR_PREVAILING_DEF;
365 break;
366 }
367 }
368 }
369
370 return LDPS_OK;
371}
372
373/* Add a new (real) input file generated by a plugin. */
374
375static enum ld_plugin_status
376add_input_file (const char *pathname)
377{
378 /* Get symbols from the real LTO object. */
379 char **matching;
380 long real_symsize;
381 long real_nsyms;
382 asymbol **real_syms;
383 int lto_nsyms;
384 bfd_boolean lto_symbol_found = FALSE;
385 const struct ld_plugin_symbol *lto_syms;
386 bfd *rbfd;
387 int i, j;
388
389 rbfd = bfd_openr (pathname, NULL);
390 if (!bfd_check_format_matches (rbfd, bfd_object, &matching))
391 BFD_ASSERT (0);
392
393 real_symsize = bfd_get_symtab_upper_bound (rbfd);
394 if (real_symsize < 0)
395 BFD_ASSERT (0);
396
397 real_syms = (asymbol **) bfd_malloc (real_symsize);
398 if (real_syms)
399 {
400 real_nsyms = bfd_canonicalize_symtab (rbfd, real_syms);
401 if (real_nsyms < 0)
402 BFD_ASSERT (0);
403
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)
412 {
413 lto_symbol_found = TRUE;
414 break;
415 }
416 }
417
418 if (lto_symbol_found)
419 {
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;
424 }
425 else
426 {
427 bfd_close (rbfd);
428 free (real_syms);
429 }
430
ce3c775b
NC
431 return LDPS_OK;
432}
433
434static enum ld_plugin_status
435add_symbols (void * handle,
436 int nsyms,
437 const struct ld_plugin_symbol * syms)
438{
439 bfd *abfd = handle;
440 struct plugin_data_struct *plugin_data =
5bb3703f 441 bfd_alloc (abfd, sizeof (plugin_data_struct));
ce3c775b 442
0aa99dcd
L
443 if (plugin_data)
444 {
445 struct ld_plugin_symbol *sym_info;
446 char *strtab;
447 size_t sym_info_size, name_length;
448 int i;
449
1d07a805
L
450 memset (plugin_data, 0, sizeof (*plugin_data));
451
0aa99dcd
L
452 abfd->tdata.plugin_data = plugin_data;
453
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);
457
458 /* Allocate a string table */
459 for (i = 0; i < nsyms; i++)
460 sym_info_size += strlen (syms[i].name) + 1;
461
462 sym_info = bfd_alloc (abfd, sym_info_size);
463 if (sym_info)
464 {
465 /* Copy symbol table. */
466 memcpy (sym_info, syms, nsyms * sizeof (*syms));
467
468 /* Copy symbol names in symbol table. */
469 strtab = (char *) (sym_info + nsyms);
470 for (i = 0; i < nsyms; i++)
471 {
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;
476 }
477
478 plugin_data->nsyms = nsyms;
479 plugin_data->syms = sym_info;
480
481 current_plugin->lto_nsyms = nsyms;
482 current_plugin->lto_syms = sym_info;
483 }
484 }
ce3c775b
NC
485
486 if (nsyms != 0)
487 abfd->flags |= HAS_SYMS;
488
ce3c775b
NC
489 return LDPS_OK;
490}
491
fc579192
NC
492static const char *plugin_program_name;
493
494void
495bfd_plugin_set_program_name (const char *program_name)
496{
497 plugin_program_name = program_name;
498}
ce3c775b 499
7d0b9ebc
AM
500int
501bfd_plugin_open_input (bfd *ibfd, struct ld_plugin_input_file *file)
ce3c775b 502{
e44f5bef
MT
503 bfd *iobfd;
504
7d0b9ebc 505 iobfd = ibfd;
27b07675
AM
506 while (iobfd->my_archive
507 && !bfd_is_thin_archive (iobfd->my_archive))
508 iobfd = iobfd->my_archive;
7d0b9ebc 509 file->name = iobfd->filename;
e44f5bef
MT
510
511 if (!iobfd->iostream && !bfd_open_file (iobfd))
512 return 0;
513
27b07675
AM
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);
520 if (file->fd < 0)
521 return 0;
e44f5bef 522
7d0b9ebc 523 if (iobfd == ibfd)
e44f5bef
MT
524 {
525 struct stat stat_buf;
103da91b 526
7d0b9ebc 527 if (fstat (file->fd, &stat_buf))
103da91b
NC
528 {
529 close(file->fd);
530 return 0;
531 }
532
7d0b9ebc
AM
533 file->offset = 0;
534 file->filesize = stat_buf.st_size;
535 }
536 else
537 {
538 file->offset = ibfd->origin;
539 file->filesize = arelt_size (ibfd);
e44f5bef 540 }
7d0b9ebc
AM
541 return 1;
542}
543
544static int
545try_claim (bfd *abfd)
546{
547 int claimed = 0;
548 struct ld_plugin_input_file file;
e44f5bef 549
27b07675 550 file.handle = abfd;
22fe7df8
L
551 if (bfd_plugin_open_input (abfd, &file)
552 && current_plugin->claim_file)
0aa99dcd
L
553 {
554 current_plugin->claim_file (&file, &claimed);
555 if (claimed)
556 {
557 if (current_plugin->all_symbols_read)
558 {
559 struct plugin_data_struct *plugin_data
560 = abfd->tdata.plugin_data;
561 if (plugin_data)
562 {
563 /* Get real symbols from LTO wrapper. */
564 current_plugin->all_symbols_read ();
565
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;
570
571 /* Clean up LTO plugin. */
572 if (current_plugin->cleanup_handler)
573 current_plugin->cleanup_handler ();
574 }
575 }
576 }
577
22fe7df8 578 close (file.fd);
0aa99dcd 579 }
22fe7df8
L
580
581 if (current_plugin->lto_wrapper)
582 {
583 /* Clean up for LTO wrapper. NB: Resolution file and option
584 have been created regardless if an IR object is claimed or
585 not. */
586 unlink (current_plugin->resolution_file);
587 free (current_plugin->resolution_option);
588 }
589
7d0b9ebc 590 return claimed;
e44f5bef
MT
591}
592
593static int
99845b3b
L
594try_load_plugin (const char *pname,
595 struct plugin_list_entry *plugin_list_iter,
596 bfd *abfd, bfd_boolean build_list_p)
e44f5bef 597{
dcf06b89 598 void *plugin_handle;
0aa99dcd 599 struct ld_plugin_tv tv[12];
ce3c775b
NC
600 int i;
601 ld_plugin_onload onload;
602 enum ld_plugin_status status;
dcf06b89 603 int result = 0;
5ae0078c 604
22fe7df8
L
605 /* NB: Each object is independent. Reuse the previous plugin from
606 the last run will lead to wrong result. */
607 if (current_plugin)
dcf06b89
L
608 memset (current_plugin, 0,
609 offsetof (struct plugin_list_entry, next));
0aa99dcd 610
99845b3b
L
611 if (plugin_list_iter)
612 pname = plugin_list_iter->plugin_name;
613
ce3c775b
NC
614 plugin_handle = dlopen (pname, RTLD_NOW);
615 if (!plugin_handle)
616 {
4eca0228 617 _bfd_error_handler ("%s\n", dlerror ());
ce3c775b
NC
618 return 0;
619 }
620
22fe7df8 621 if (plugin_list_iter == NULL)
b0ceb98a 622 {
22fe7df8
L
623 size_t length_plugin_name = strlen (pname) + 1;
624 char *plugin_name = bfd_malloc (length_plugin_name);
625 if (plugin_name == NULL)
dcf06b89 626 goto short_circuit;
22fe7df8
L
627 plugin_list_iter = bfd_malloc (sizeof *plugin_list_iter);
628 if (plugin_list_iter == NULL)
4b24dd1a 629 {
22fe7df8 630 free (plugin_name);
dcf06b89 631 goto short_circuit;
4b24dd1a 632 }
22fe7df8
L
633 /* Make a copy of PNAME since PNAME from load_plugin () will be
634 freed. */
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;
b0ceb98a
Z
640 }
641
dcf06b89 642 current_plugin = plugin_list_iter;
99845b3b 643 if (build_list_p)
dcf06b89 644 goto short_circuit;
0aa99dcd 645
ce3c775b
NC
646 onload = dlsym (plugin_handle, "onload");
647 if (!onload)
dcf06b89 648 goto short_circuit;
ce3c775b
NC
649
650 i = 0;
651 tv[i].tv_tag = LDPT_MESSAGE;
652 tv[i].tv_u.tv_message = message;
653
654 ++i;
655 tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
656 tv[i].tv_u.tv_register_claim_file = register_claim_file;
657
658 ++i;
659 tv[i].tv_tag = LDPT_ADD_SYMBOLS;
660 tv[i].tv_u.tv_add_symbols = add_symbols;
661
0aa99dcd
L
662 if (get_lto_wrapper (plugin_list_iter))
663 {
664 ++i;
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;
667
668 ++i;
669 tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
670 tv[i].tv_u.tv_register_cleanup = register_cleanup;
671
672 ++i;
673 tv[i].tv_tag = LDPT_GET_SYMBOLS;
674 tv[i].tv_u.tv_get_symbols = get_symbols;
675
676 ++i;
677 tv[i].tv_tag = LDPT_GET_SYMBOLS_V2;
678 tv[i].tv_u.tv_get_symbols = get_symbols;
679
680 ++i;
681 tv[i].tv_tag = LDPT_OPTION;
682 tv[i].tv_u.tv_string = plugin_list_iter->lto_wrapper;
683
684 ++i;
685 tv[i].tv_tag = LDPT_OPTION;
686 tv[i].tv_u.tv_string = plugin_list_iter->resolution_option;
687
688 ++i;
689 tv[i].tv_tag = LDPT_LINKER_OUTPUT;
690 tv[i].tv_u.tv_val = LDPO_EXEC;
691
692 ++i;
693 tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
694 tv[i].tv_u.tv_add_input_file = add_input_file;
695 }
696
ce3c775b
NC
697 ++i;
698 tv[i].tv_tag = LDPT_NULL;
699 tv[i].tv_u.tv_val = 0;
700
0aa99dcd 701 /* LTO plugin will call handler hooks to set up plugin handlers. */
ce3c775b
NC
702 status = (*onload)(tv);
703
704 if (status != LDPS_OK)
dcf06b89 705 goto short_circuit;
b0ceb98a 706
0aa99dcd
L
707 if (current_plugin->lto_wrapper
708 && setup_lto_wrapper_env (current_plugin))
dcf06b89 709 goto short_circuit;
ce3c775b 710
5ae0078c
L
711 abfd->plugin_format = bfd_plugin_no;
712
0aa99dcd 713 if (!current_plugin->claim_file)
dcf06b89 714 goto short_circuit;
ce3c775b 715
e44f5bef 716 if (!try_claim (abfd))
dcf06b89 717 goto short_circuit;
e44f5bef 718
5ae0078c 719 abfd->plugin_format = bfd_plugin_yes;
dcf06b89
L
720 result = 1;
721
722short_circuit:
723 dlclose (plugin_handle);
724 return result;
ce3c775b
NC
725}
726
5ae0078c 727/* There may be plugin libraries in lib/bfd-plugins. */
99845b3b 728static int has_plugin_list = -1;
5ae0078c
L
729
730static const bfd_target *(*ld_plugin_object_p) (bfd *);
731
ce3c775b
NC
732static const char *plugin_name;
733
734void
735bfd_plugin_set_plugin (const char *p)
736{
737 plugin_name = p;
5ae0078c
L
738}
739
740/* Return TRUE if a plugin library is used. */
741
742bfd_boolean
743bfd_plugin_specified_p (void)
744{
99845b3b 745 return plugin_list != NULL;
5ae0078c
L
746}
747
7dc3990e
L
748/* Return TRUE if ABFD can be claimed by linker LTO plugin. */
749
750bfd_boolean
751bfd_link_plugin_object_p (bfd *abfd)
752{
753 if (ld_plugin_object_p)
754 return ld_plugin_object_p (abfd) != NULL;
755 return FALSE;
756}
757
5ae0078c
L
758extern const bfd_target plugin_vec;
759
760/* Return TRUE if TARGET is a pointer to plugin_vec. */
761
762bfd_boolean
763bfd_plugin_target_p (const bfd_target *target)
764{
765 return target == &plugin_vec;
766}
767
768/* Register OBJECT_P to be used by bfd_plugin_object_p. */
769
770void
771register_ld_plugin_object_p (const bfd_target *(*object_p) (bfd *))
772{
773 ld_plugin_object_p = object_p;
ce3c775b
NC
774}
775
99845b3b
L
776static void
777build_plugin_list (bfd *abfd)
ce3c775b 778{
41f37a6f
AM
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" };
785 struct stat last_st;
41f37a6f 786 unsigned int i;
ce3c775b 787
99845b3b
L
788 if (has_plugin_list >= 0)
789 return;
ce3c775b 790
41f37a6f
AM
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. */
795 last_st.st_dev = 0;
796 last_st.st_ino = 0;
797 for (i = 0; i < sizeof (path) / sizeof (path[0]); i++)
ce3c775b 798 {
41f37a6f
AM
799 char *plugin_dir = make_relative_prefix (plugin_program_name,
800 BINDIR,
801 path[i]);
802 if (plugin_dir)
803 {
804 struct stat st;
805 DIR *d;
806
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
811 && st.st_ino != 0)
812 && (d = opendir (plugin_dir)) != NULL)
813 {
814 struct dirent *ent;
815
816 last_st.st_dev = st.st_dev;
817 last_st.st_ino = st.st_ino;
818 while ((ent = readdir (d)) != NULL)
819 {
820 char *full_name;
821
822 full_name = concat (plugin_dir, "/", ent->d_name, NULL);
823 if (stat (full_name, &st) == 0 && S_ISREG (st.st_mode))
99845b3b 824 try_load_plugin (full_name, NULL, abfd, TRUE);
41f37a6f 825 free (full_name);
41f37a6f
AM
826 }
827 closedir (d);
828 }
829 free (plugin_dir);
830 }
ce3c775b
NC
831 }
832
99845b3b
L
833 has_plugin_list = plugin_list != NULL;
834}
835
836static int
837load_plugin (bfd *abfd)
838{
839 struct plugin_list_entry *plugin_list_iter;
840
841 if (plugin_name)
842 return try_load_plugin (plugin_name, plugin_list, abfd, FALSE);
843
844 if (plugin_program_name == NULL)
845 return 0;
846
847 build_plugin_list (abfd);
848
849 for (plugin_list_iter = plugin_list;
850 plugin_list_iter;
851 plugin_list_iter = plugin_list_iter->next)
852 if (try_load_plugin (NULL, plugin_list_iter, abfd, FALSE))
853 return 1;
854
855 return 0;
ce3c775b
NC
856}
857
858
859static const bfd_target *
860bfd_plugin_object_p (bfd *abfd)
861{
5ae0078c
L
862 if (ld_plugin_object_p)
863 return ld_plugin_object_p (abfd);
864
49f30d83 865 if (abfd->plugin_format == bfd_plugin_unknown && !load_plugin (abfd))
ce3c775b
NC
866 return NULL;
867
5ae0078c 868 return abfd->plugin_format == bfd_plugin_yes ? abfd->xvec : NULL;
ce3c775b
NC
869}
870
871/* Copy any private info we understand from the input bfd
872 to the output bfd. */
873
874static bfd_boolean
875bfd_plugin_bfd_copy_private_bfd_data (bfd *ibfd ATTRIBUTE_UNUSED,
876 bfd *obfd ATTRIBUTE_UNUSED)
877{
878 BFD_ASSERT (0);
879 return TRUE;
880}
881
882/* Copy any private info we understand from the input section
883 to the output section. */
884
885static bfd_boolean
886bfd_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)
890{
891 BFD_ASSERT (0);
892 return TRUE;
893}
894
895/* Copy any private info we understand from the input symbol
896 to the output symbol. */
897
898static bfd_boolean
899bfd_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)
903{
904 BFD_ASSERT (0);
905 return TRUE;
906}
907
908static bfd_boolean
909bfd_plugin_bfd_print_private_bfd_data (bfd *abfd ATTRIBUTE_UNUSED, PTR ptr ATTRIBUTE_UNUSED)
910{
911 BFD_ASSERT (0);
912 return TRUE;
913}
914
915static char *
916bfd_plugin_core_file_failing_command (bfd *abfd ATTRIBUTE_UNUSED)
917{
918 BFD_ASSERT (0);
919 return NULL;
920}
921
922static int
923bfd_plugin_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
261b8d08
PA
924{
925 BFD_ASSERT (0);
926 return 0;
927}
928
929static int
930bfd_plugin_core_file_pid (bfd *abfd ATTRIBUTE_UNUSED)
ce3c775b
NC
931{
932 BFD_ASSERT (0);
933 return 0;
934}
935
936static long
937bfd_plugin_get_symtab_upper_bound (bfd *abfd)
938{
939 struct plugin_data_struct *plugin_data = abfd->tdata.plugin_data;
940 long nsyms = plugin_data->nsyms;
941
942 BFD_ASSERT (nsyms >= 0);
943
944 return ((nsyms + 1) * sizeof (asymbol *));
945}
946
947static flagword
948convert_flags (const struct ld_plugin_symbol *sym)
949{
950 switch (sym->def)
68ffbac6 951 {
ce3c775b
NC
952 case LDPK_DEF:
953 case LDPK_COMMON:
954 case LDPK_UNDEF:
955 return BSF_GLOBAL;
956
957 case LDPK_WEAKUNDEF:
958 case LDPK_WEAKDEF:
959 return BSF_GLOBAL | BSF_WEAK;
960
961 default:
962 BFD_ASSERT (0);
963 return 0;
964 }
965}
966
967static long
968bfd_plugin_canonicalize_symtab (bfd *abfd,
969 asymbol **alocation)
970{
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;
a288c270
AM
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);
0aa99dcd
L
979 int i, j;
980 long real_nsyms;
981 asymbol **real_syms;
982
983 real_syms = plugin_data->real_syms;
984 if (real_syms)
985 real_nsyms = plugin_data->real_nsyms;
986 else
987 real_nsyms = 0;
ce3c775b 988
ce3c775b
NC
989 for (i = 0; i < nsyms; i++)
990 {
68ffbac6 991 asymbol *s = bfd_alloc (abfd, sizeof (asymbol));
ce3c775b
NC
992
993 BFD_ASSERT (s);
994 alocation[i] = s;
995
996 s->the_bfd = abfd;
997 s->name = syms[i].name;
998 s->value = 0;
999 s->flags = convert_flags (&syms[i]);
1000 switch (syms[i].def)
1001 {
1002 case LDPK_COMMON:
1003 s->section = &fake_common_section;
1004 break;
1005 case LDPK_UNDEF:
1006 case LDPK_WEAKUNDEF:
1007 s->section = bfd_und_section_ptr;
1008 break;
1009 case LDPK_DEF:
1010 case LDPK_WEAKDEF:
1011 s->section = &fake_section;
0aa99dcd
L
1012 if (real_nsyms)
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)
1017 {
1018 s->section = real_syms[j]->section;
1019 break;
1020 }
ce3c775b
NC
1021 break;
1022 default:
1023 BFD_ASSERT (0);
1024 }
1025
1026 s->udata.p = (void *) &syms[i];
1027 }
1028
1029 return nsyms;
1030}
1031
1032static void
1033bfd_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)
1037{
1038 BFD_ASSERT (0);
1039}
1040
1041static void
1042bfd_plugin_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
1043 asymbol *symbol,
1044 symbol_info *ret)
1045{
1046 bfd_symbol_info (symbol, ret);
1047}
1048
1049/* Make an empty symbol. */
1050
1051static asymbol *
1052bfd_plugin_make_empty_symbol (bfd *abfd)
1053{
d3ce72d0
NC
1054 asymbol *new_symbol = bfd_zalloc (abfd, sizeof (asymbol));
1055 if (new_symbol == NULL)
1056 return new_symbol;
1057 new_symbol->the_bfd = abfd;
1058 return new_symbol;
ce3c775b
NC
1059}
1060
1061static int
1062bfd_plugin_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
1063 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1064{
1065 BFD_ASSERT (0);
1066 return 0;
1067}
1068
0aa99dcd
L
1069static bfd_boolean
1070bfd_plugin_close_and_cleanup (bfd *abfd)
1071{
1072 struct plugin_data_struct *plugin_data;
1073
1074 if (abfd->format != bfd_archive
1075 && (plugin_data = abfd->tdata.plugin_data))
1076 {
1077 if (plugin_data->real_bfd)
1078 bfd_close (plugin_data->real_bfd);
1079
1080 if (plugin_data->real_syms)
1081 free (plugin_data->real_syms);
1082 }
1083
1084 return _bfd_generic_close_and_cleanup (abfd);
1085}
1086
ce3c775b
NC
1087const bfd_target plugin_vec =
1088{
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. */
64bfc258 1101 255, /* match priority. */
ce3c775b
NC
1102
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 */
1109
1110 { /* bfd_check_format. */
1111 _bfd_dummy_target,
1112 bfd_plugin_object_p,
1113 bfd_generic_archive_p,
1114 _bfd_dummy_target
1115 },
1116 { /* bfd_set_format. */
d00dd7dc
AM
1117 _bfd_bool_bfd_false_error,
1118 _bfd_bool_bfd_false_error,
ce3c775b 1119 _bfd_generic_mkarchive,
d00dd7dc 1120 _bfd_bool_bfd_false_error,
ce3c775b
NC
1121 },
1122 { /* bfd_write_contents. */
d00dd7dc
AM
1123 _bfd_bool_bfd_false_error,
1124 _bfd_bool_bfd_false_error,
ce3c775b 1125 _bfd_write_archive_contents,
d00dd7dc 1126 _bfd_bool_bfd_false_error,
ce3c775b
NC
1127 },
1128
1129 BFD_JUMP_TABLE_GENERIC (bfd_plugin),
1130 BFD_JUMP_TABLE_COPY (bfd_plugin),
1131 BFD_JUMP_TABLE_CORE (bfd_plugin),
e6cc316a
L
1132#ifdef USE_64_BIT_ARCHIVE
1133 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_64_bit),
1134#else
ce3c775b 1135 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
e6cc316a 1136#endif
ce3c775b
NC
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),
1142
1143 NULL,
1144
07d6d2b8 1145 NULL /* backend_data. */
ce3c775b 1146};
98950613 1147#endif /* BFD_SUPPORTS_PLUGIN */
This page took 0.742456 seconds and 4 git commands to generate.