PR25593, --as-needed breaks DT_NEEDED order with linker plugin
[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
ecda9016
L
150static const char *plugin_program_name;
151static int need_lto_wrapper_p;
152
153void
154bfd_plugin_set_program_name (const char *program_name,
155 int need_lto_wrapper)
156{
157 plugin_program_name = program_name;
158 need_lto_wrapper_p = need_lto_wrapper;
159}
160
0aa99dcd
L
161/* Use GCC LTO wrapper to covert LTO IR object to the real object. */
162
163static bfd_boolean
164get_lto_wrapper (struct plugin_list_entry *plugin)
165{
166 struct stat st;
167 const char *real_name;
168 const char *base_name;
169 size_t length;
170 const char *target_start = NULL;
171 const char *target_end = NULL;
172 size_t target_length = 0;
173 char *gcc_name;
174 char *wrapper_name;
175 char *p;
176 char dir_seperator = '\0';
177 char *resolution_file;
178
ecda9016
L
179 if (!need_lto_wrapper_p)
180 return FALSE;
181
0aa99dcd
L
182 if (plugin->initialized)
183 {
184 if (plugin->lto_wrapper)
185 {
186 resolution_file = make_temp_file (".res");
187 if (resolution_file)
188 {
189 plugin->resolution_file = resolution_file;
190 plugin->resolution_option = concat ("-fresolution=",
191 resolution_file, NULL);
192 return TRUE;
193 }
194 else
195 {
196 /* Something is wrong. Give up. */
197 free (plugin->gcc);
198 free (plugin->lto_wrapper);
199 free (plugin->gcc_env);
200 plugin->gcc = NULL;
201 plugin->gcc_env = NULL;
202 plugin->lto_wrapper = NULL;
203 }
204 }
205
206 return FALSE;
207 }
208
209 plugin->initialized = TRUE;
210
211 /* Check for PREFIX/libexec/gcc/TARGET/VERSION/liblto_plugin.so. */
212 real_name = lrealpath (plugin->plugin_name);
213 base_name = lbasename (real_name);
214
215 /* The directory length in plugin pathname. */
216 length = base_name - real_name;
217
218 /* Skip if there is no PREFIX. */
219 if (!length)
220 return FALSE;
221
222 p = (char *) real_name + length - 1;
223 if (IS_DIR_SEPARATOR (*p))
224 {
225 int level = 0;
226 for (; p != real_name; p--)
227 if (IS_DIR_SEPARATOR (*p))
228 {
229 level++;
230 if (level == 2)
231 target_end = p;
232 else if (level == 3)
233 {
234 target_start = p + 1;
235 target_length = target_end - target_start;
236 }
237 else if (level == 5)
238 {
239 dir_seperator = *p;
240 break;
241 }
242 }
243 }
244
245 /* Skip if there is no TARGET nor PREFIX. */
246 if (!target_length || !dir_seperator)
247 return FALSE;
248
249#ifdef HAVE_EXECUTABLE_SUFFIX
250# define GCC_EXECUTABLE "gcc" EXECUTABLE_SUFFIX
251# define LTO_WRAPPER_EXECUTABLE "lto-wrapper" EXECUTABLE_SUFFIX
252#else
253# define GCC_EXECUTABLE "gcc"
254# define LTO_WRAPPER_EXECUTABLE "lto-wrapper"
255#endif
256 gcc_name = bfd_malloc (length + target_length
257 + sizeof (GCC_EXECUTABLE));
258 if (gcc_name == NULL)
259 return FALSE;
260 memcpy (gcc_name, real_name, length);
261
262 /* Get PREFIX/bin/. */
263 p += gcc_name - real_name;
264 memcpy (p + 1, "bin", 3);
265 p[4] = dir_seperator;
266
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))
273 {
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))
277 {
278 free (gcc_name);
279 return FALSE;
280 }
281 }
282
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)
286 {
287 free (gcc_name);
288 return FALSE;
289 }
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))
294 {
295 resolution_file = make_temp_file (".res");
296 if (resolution_file)
297 {
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);
304 return TRUE;
305 }
306 }
307
308 free (gcc_name);
309 free (wrapper_name);
310 return FALSE;
311}
312
313/* Set environment variables for GCC LTO wrapper to covert LTO IR
314 object to the real object. */
315
316static int
317setup_lto_wrapper_env (struct plugin_list_entry *plugin)
318{
319 return (putenv (plugin->gcc_env)
320 || putenv ("COLLECT_GCC_OPTIONS="));
321}
322
323static struct plugin_list_entry *plugin_list = NULL;
324static struct plugin_list_entry *current_plugin = NULL;
325
ce3c775b 326/* Register a claim-file handler. */
ce3c775b
NC
327
328static enum ld_plugin_status
329register_claim_file (ld_plugin_claim_file_handler handler)
330{
0aa99dcd
L
331 current_plugin->claim_file = handler;
332 return LDPS_OK;
333}
334
335/* Register an all-symbols-read handler. */
336
337static enum ld_plugin_status
338register_all_symbols_read (ld_plugin_all_symbols_read_handler handler)
339{
340 current_plugin->all_symbols_read = handler;
341 return LDPS_OK;
342}
343
344/* Register a cleanup handler. */
345
346static enum ld_plugin_status
347register_cleanup (ld_plugin_all_symbols_read_handler handler)
348{
349 current_plugin->cleanup_handler = handler;
350 return LDPS_OK;
351}
352
353/* Get the symbol resolution info for a plugin-claimed input file. */
354
355static enum ld_plugin_status
356get_symbols (const void *handle ATTRIBUTE_UNUSED, int nsyms,
357 struct ld_plugin_symbol *syms)
358{
359 if (syms)
360 {
361 int n;
362 for (n = 0; n < nsyms; n++)
363 {
364 switch (syms[n].def)
365 {
366 default:
367 BFD_ASSERT (0);
368 break;
369 case LDPK_UNDEF:
370 case LDPK_WEAKUNDEF:
371 syms[n].resolution = LDPR_UNDEF;
372 break;
373 case LDPK_DEF:
374 case LDPK_WEAKDEF:
375 case LDPK_COMMON:
376 /* Tell plugin that LTO symbol has references from regular
377 object code. */
378 syms[n].resolution = LDPR_PREVAILING_DEF;
379 break;
380 }
381 }
382 }
383
384 return LDPS_OK;
385}
386
387/* Add a new (real) input file generated by a plugin. */
388
389static enum ld_plugin_status
390add_input_file (const char *pathname)
391{
392 /* Get symbols from the real LTO object. */
393 char **matching;
394 long real_symsize;
395 long real_nsyms;
396 asymbol **real_syms;
397 int lto_nsyms;
398 bfd_boolean lto_symbol_found = FALSE;
399 const struct ld_plugin_symbol *lto_syms;
400 bfd *rbfd;
401 int i, j;
402
403 rbfd = bfd_openr (pathname, NULL);
404 if (!bfd_check_format_matches (rbfd, bfd_object, &matching))
405 BFD_ASSERT (0);
406
407 real_symsize = bfd_get_symtab_upper_bound (rbfd);
408 if (real_symsize < 0)
409 BFD_ASSERT (0);
410
411 real_syms = (asymbol **) bfd_malloc (real_symsize);
412 if (real_syms)
413 {
414 real_nsyms = bfd_canonicalize_symtab (rbfd, real_syms);
415 if (real_nsyms < 0)
416 BFD_ASSERT (0);
417
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)
426 {
427 lto_symbol_found = TRUE;
428 break;
429 }
430 }
431
432 if (lto_symbol_found)
433 {
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;
438 }
439 else
440 {
441 bfd_close (rbfd);
442 free (real_syms);
443 }
444
ce3c775b
NC
445 return LDPS_OK;
446}
447
448static enum ld_plugin_status
449add_symbols (void * handle,
450 int nsyms,
451 const struct ld_plugin_symbol * syms)
452{
453 bfd *abfd = handle;
454 struct plugin_data_struct *plugin_data =
5bb3703f 455 bfd_alloc (abfd, sizeof (plugin_data_struct));
ce3c775b 456
0aa99dcd
L
457 if (plugin_data)
458 {
459 struct ld_plugin_symbol *sym_info;
460 char *strtab;
461 size_t sym_info_size, name_length;
462 int i;
463
1d07a805
L
464 memset (plugin_data, 0, sizeof (*plugin_data));
465
0aa99dcd
L
466 abfd->tdata.plugin_data = plugin_data;
467
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);
471
472 /* Allocate a string table */
473 for (i = 0; i < nsyms; i++)
474 sym_info_size += strlen (syms[i].name) + 1;
475
476 sym_info = bfd_alloc (abfd, sym_info_size);
477 if (sym_info)
478 {
479 /* Copy symbol table. */
480 memcpy (sym_info, syms, nsyms * sizeof (*syms));
481
482 /* Copy symbol names in symbol table. */
483 strtab = (char *) (sym_info + nsyms);
484 for (i = 0; i < nsyms; i++)
485 {
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;
490 }
491
492 plugin_data->nsyms = nsyms;
493 plugin_data->syms = sym_info;
494
495 current_plugin->lto_nsyms = nsyms;
496 current_plugin->lto_syms = sym_info;
497 }
498 }
ce3c775b
NC
499
500 if (nsyms != 0)
501 abfd->flags |= HAS_SYMS;
502
ce3c775b
NC
503 return LDPS_OK;
504}
505
7d0b9ebc
AM
506int
507bfd_plugin_open_input (bfd *ibfd, struct ld_plugin_input_file *file)
ce3c775b 508{
e44f5bef
MT
509 bfd *iobfd;
510
7d0b9ebc 511 iobfd = ibfd;
27b07675
AM
512 while (iobfd->my_archive
513 && !bfd_is_thin_archive (iobfd->my_archive))
514 iobfd = iobfd->my_archive;
7d0b9ebc 515 file->name = iobfd->filename;
e44f5bef
MT
516
517 if (!iobfd->iostream && !bfd_open_file (iobfd))
518 return 0;
519
27b07675
AM
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);
526 if (file->fd < 0)
527 return 0;
e44f5bef 528
7d0b9ebc 529 if (iobfd == ibfd)
e44f5bef
MT
530 {
531 struct stat stat_buf;
103da91b 532
7d0b9ebc 533 if (fstat (file->fd, &stat_buf))
103da91b
NC
534 {
535 close(file->fd);
536 return 0;
537 }
538
7d0b9ebc
AM
539 file->offset = 0;
540 file->filesize = stat_buf.st_size;
541 }
542 else
543 {
544 file->offset = ibfd->origin;
545 file->filesize = arelt_size (ibfd);
e44f5bef 546 }
7d0b9ebc
AM
547 return 1;
548}
549
550static int
551try_claim (bfd *abfd)
552{
553 int claimed = 0;
554 struct ld_plugin_input_file file;
e44f5bef 555
27b07675 556 file.handle = abfd;
22fe7df8
L
557 if (bfd_plugin_open_input (abfd, &file)
558 && current_plugin->claim_file)
0aa99dcd
L
559 {
560 current_plugin->claim_file (&file, &claimed);
561 if (claimed)
562 {
563 if (current_plugin->all_symbols_read)
564 {
565 struct plugin_data_struct *plugin_data
566 = abfd->tdata.plugin_data;
567 if (plugin_data)
568 {
569 /* Get real symbols from LTO wrapper. */
570 current_plugin->all_symbols_read ();
571
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;
576
577 /* Clean up LTO plugin. */
578 if (current_plugin->cleanup_handler)
579 current_plugin->cleanup_handler ();
580 }
581 }
582 }
583
22fe7df8 584 close (file.fd);
0aa99dcd 585 }
22fe7df8
L
586
587 if (current_plugin->lto_wrapper)
588 {
589 /* Clean up for LTO wrapper. NB: Resolution file and option
590 have been created regardless if an IR object is claimed or
591 not. */
592 unlink (current_plugin->resolution_file);
593 free (current_plugin->resolution_option);
594 }
595
7d0b9ebc 596 return claimed;
e44f5bef
MT
597}
598
599static int
99845b3b
L
600try_load_plugin (const char *pname,
601 struct plugin_list_entry *plugin_list_iter,
602 bfd *abfd, bfd_boolean build_list_p)
e44f5bef 603{
dcf06b89 604 void *plugin_handle;
0aa99dcd 605 struct ld_plugin_tv tv[12];
ce3c775b
NC
606 int i;
607 ld_plugin_onload onload;
608 enum ld_plugin_status status;
dcf06b89 609 int result = 0;
5ae0078c 610
22fe7df8
L
611 /* NB: Each object is independent. Reuse the previous plugin from
612 the last run will lead to wrong result. */
613 if (current_plugin)
dcf06b89
L
614 memset (current_plugin, 0,
615 offsetof (struct plugin_list_entry, next));
0aa99dcd 616
99845b3b
L
617 if (plugin_list_iter)
618 pname = plugin_list_iter->plugin_name;
619
ce3c775b
NC
620 plugin_handle = dlopen (pname, RTLD_NOW);
621 if (!plugin_handle)
622 {
4eca0228 623 _bfd_error_handler ("%s\n", dlerror ());
ce3c775b
NC
624 return 0;
625 }
626
22fe7df8 627 if (plugin_list_iter == NULL)
b0ceb98a 628 {
22fe7df8
L
629 size_t length_plugin_name = strlen (pname) + 1;
630 char *plugin_name = bfd_malloc (length_plugin_name);
631 if (plugin_name == NULL)
dcf06b89 632 goto short_circuit;
22fe7df8
L
633 plugin_list_iter = bfd_malloc (sizeof *plugin_list_iter);
634 if (plugin_list_iter == NULL)
4b24dd1a 635 {
22fe7df8 636 free (plugin_name);
dcf06b89 637 goto short_circuit;
4b24dd1a 638 }
22fe7df8
L
639 /* Make a copy of PNAME since PNAME from load_plugin () will be
640 freed. */
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;
b0ceb98a
Z
646 }
647
dcf06b89 648 current_plugin = plugin_list_iter;
99845b3b 649 if (build_list_p)
dcf06b89 650 goto short_circuit;
0aa99dcd 651
ce3c775b
NC
652 onload = dlsym (plugin_handle, "onload");
653 if (!onload)
dcf06b89 654 goto short_circuit;
ce3c775b
NC
655
656 i = 0;
657 tv[i].tv_tag = LDPT_MESSAGE;
658 tv[i].tv_u.tv_message = message;
659
660 ++i;
661 tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
662 tv[i].tv_u.tv_register_claim_file = register_claim_file;
663
664 ++i;
665 tv[i].tv_tag = LDPT_ADD_SYMBOLS;
666 tv[i].tv_u.tv_add_symbols = add_symbols;
667
0aa99dcd
L
668 if (get_lto_wrapper (plugin_list_iter))
669 {
670 ++i;
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;
673
674 ++i;
675 tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
676 tv[i].tv_u.tv_register_cleanup = register_cleanup;
677
678 ++i;
679 tv[i].tv_tag = LDPT_GET_SYMBOLS;
680 tv[i].tv_u.tv_get_symbols = get_symbols;
681
682 ++i;
683 tv[i].tv_tag = LDPT_GET_SYMBOLS_V2;
684 tv[i].tv_u.tv_get_symbols = get_symbols;
685
686 ++i;
687 tv[i].tv_tag = LDPT_OPTION;
688 tv[i].tv_u.tv_string = plugin_list_iter->lto_wrapper;
689
690 ++i;
691 tv[i].tv_tag = LDPT_OPTION;
692 tv[i].tv_u.tv_string = plugin_list_iter->resolution_option;
693
694 ++i;
695 tv[i].tv_tag = LDPT_LINKER_OUTPUT;
696 tv[i].tv_u.tv_val = LDPO_EXEC;
697
698 ++i;
699 tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
700 tv[i].tv_u.tv_add_input_file = add_input_file;
701 }
702
ce3c775b
NC
703 ++i;
704 tv[i].tv_tag = LDPT_NULL;
705 tv[i].tv_u.tv_val = 0;
706
0aa99dcd 707 /* LTO plugin will call handler hooks to set up plugin handlers. */
ce3c775b
NC
708 status = (*onload)(tv);
709
710 if (status != LDPS_OK)
dcf06b89 711 goto short_circuit;
b0ceb98a 712
0aa99dcd
L
713 if (current_plugin->lto_wrapper
714 && setup_lto_wrapper_env (current_plugin))
dcf06b89 715 goto short_circuit;
ce3c775b 716
5ae0078c
L
717 abfd->plugin_format = bfd_plugin_no;
718
0aa99dcd 719 if (!current_plugin->claim_file)
dcf06b89 720 goto short_circuit;
ce3c775b 721
e44f5bef 722 if (!try_claim (abfd))
dcf06b89 723 goto short_circuit;
e44f5bef 724
5ae0078c 725 abfd->plugin_format = bfd_plugin_yes;
dcf06b89
L
726 result = 1;
727
728short_circuit:
729 dlclose (plugin_handle);
730 return result;
ce3c775b
NC
731}
732
5ae0078c 733/* There may be plugin libraries in lib/bfd-plugins. */
99845b3b 734static int has_plugin_list = -1;
5ae0078c
L
735
736static const bfd_target *(*ld_plugin_object_p) (bfd *);
737
ce3c775b
NC
738static const char *plugin_name;
739
740void
741bfd_plugin_set_plugin (const char *p)
742{
743 plugin_name = p;
5ae0078c
L
744}
745
746/* Return TRUE if a plugin library is used. */
747
748bfd_boolean
749bfd_plugin_specified_p (void)
750{
99845b3b 751 return plugin_list != NULL;
5ae0078c
L
752}
753
7dc3990e
L
754/* Return TRUE if ABFD can be claimed by linker LTO plugin. */
755
756bfd_boolean
757bfd_link_plugin_object_p (bfd *abfd)
758{
759 if (ld_plugin_object_p)
760 return ld_plugin_object_p (abfd) != NULL;
761 return FALSE;
762}
763
5ae0078c
L
764extern const bfd_target plugin_vec;
765
766/* Return TRUE if TARGET is a pointer to plugin_vec. */
767
768bfd_boolean
769bfd_plugin_target_p (const bfd_target *target)
770{
771 return target == &plugin_vec;
772}
773
774/* Register OBJECT_P to be used by bfd_plugin_object_p. */
775
776void
777register_ld_plugin_object_p (const bfd_target *(*object_p) (bfd *))
778{
779 ld_plugin_object_p = object_p;
ce3c775b
NC
780}
781
99845b3b
L
782static void
783build_plugin_list (bfd *abfd)
ce3c775b 784{
41f37a6f
AM
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" };
791 struct stat last_st;
41f37a6f 792 unsigned int i;
ce3c775b 793
99845b3b
L
794 if (has_plugin_list >= 0)
795 return;
ce3c775b 796
41f37a6f
AM
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. */
801 last_st.st_dev = 0;
802 last_st.st_ino = 0;
803 for (i = 0; i < sizeof (path) / sizeof (path[0]); i++)
ce3c775b 804 {
41f37a6f
AM
805 char *plugin_dir = make_relative_prefix (plugin_program_name,
806 BINDIR,
807 path[i]);
808 if (plugin_dir)
809 {
810 struct stat st;
811 DIR *d;
812
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
817 && st.st_ino != 0)
818 && (d = opendir (plugin_dir)) != NULL)
819 {
820 struct dirent *ent;
821
822 last_st.st_dev = st.st_dev;
823 last_st.st_ino = st.st_ino;
824 while ((ent = readdir (d)) != NULL)
825 {
826 char *full_name;
827
828 full_name = concat (plugin_dir, "/", ent->d_name, NULL);
829 if (stat (full_name, &st) == 0 && S_ISREG (st.st_mode))
99845b3b 830 try_load_plugin (full_name, NULL, abfd, TRUE);
41f37a6f 831 free (full_name);
41f37a6f
AM
832 }
833 closedir (d);
834 }
835 free (plugin_dir);
836 }
ce3c775b
NC
837 }
838
99845b3b
L
839 has_plugin_list = plugin_list != NULL;
840}
841
842static int
843load_plugin (bfd *abfd)
844{
845 struct plugin_list_entry *plugin_list_iter;
846
847 if (plugin_name)
848 return try_load_plugin (plugin_name, plugin_list, abfd, FALSE);
849
850 if (plugin_program_name == NULL)
851 return 0;
852
853 build_plugin_list (abfd);
854
855 for (plugin_list_iter = plugin_list;
856 plugin_list_iter;
857 plugin_list_iter = plugin_list_iter->next)
858 if (try_load_plugin (NULL, plugin_list_iter, abfd, FALSE))
859 return 1;
860
861 return 0;
ce3c775b
NC
862}
863
864
865static const bfd_target *
866bfd_plugin_object_p (bfd *abfd)
867{
5ae0078c
L
868 if (ld_plugin_object_p)
869 return ld_plugin_object_p (abfd);
870
49f30d83 871 if (abfd->plugin_format == bfd_plugin_unknown && !load_plugin (abfd))
ce3c775b
NC
872 return NULL;
873
5ae0078c 874 return abfd->plugin_format == bfd_plugin_yes ? abfd->xvec : NULL;
ce3c775b
NC
875}
876
877/* Copy any private info we understand from the input bfd
878 to the output bfd. */
879
880static bfd_boolean
881bfd_plugin_bfd_copy_private_bfd_data (bfd *ibfd ATTRIBUTE_UNUSED,
882 bfd *obfd ATTRIBUTE_UNUSED)
883{
884 BFD_ASSERT (0);
885 return TRUE;
886}
887
888/* Copy any private info we understand from the input section
889 to the output section. */
890
891static bfd_boolean
892bfd_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)
896{
897 BFD_ASSERT (0);
898 return TRUE;
899}
900
901/* Copy any private info we understand from the input symbol
902 to the output symbol. */
903
904static bfd_boolean
905bfd_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)
909{
910 BFD_ASSERT (0);
911 return TRUE;
912}
913
914static bfd_boolean
915bfd_plugin_bfd_print_private_bfd_data (bfd *abfd ATTRIBUTE_UNUSED, PTR ptr ATTRIBUTE_UNUSED)
916{
917 BFD_ASSERT (0);
918 return TRUE;
919}
920
921static char *
922bfd_plugin_core_file_failing_command (bfd *abfd ATTRIBUTE_UNUSED)
923{
924 BFD_ASSERT (0);
925 return NULL;
926}
927
928static int
929bfd_plugin_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
261b8d08
PA
930{
931 BFD_ASSERT (0);
932 return 0;
933}
934
935static int
936bfd_plugin_core_file_pid (bfd *abfd ATTRIBUTE_UNUSED)
ce3c775b
NC
937{
938 BFD_ASSERT (0);
939 return 0;
940}
941
942static long
943bfd_plugin_get_symtab_upper_bound (bfd *abfd)
944{
945 struct plugin_data_struct *plugin_data = abfd->tdata.plugin_data;
946 long nsyms = plugin_data->nsyms;
947
948 BFD_ASSERT (nsyms >= 0);
949
950 return ((nsyms + 1) * sizeof (asymbol *));
951}
952
953static flagword
954convert_flags (const struct ld_plugin_symbol *sym)
955{
956 switch (sym->def)
68ffbac6 957 {
ce3c775b
NC
958 case LDPK_DEF:
959 case LDPK_COMMON:
960 case LDPK_UNDEF:
961 return BSF_GLOBAL;
962
963 case LDPK_WEAKUNDEF:
964 case LDPK_WEAKDEF:
965 return BSF_GLOBAL | BSF_WEAK;
966
967 default:
968 BFD_ASSERT (0);
969 return 0;
970 }
971}
972
973static long
974bfd_plugin_canonicalize_symtab (bfd *abfd,
975 asymbol **alocation)
976{
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;
a288c270
AM
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);
0aa99dcd
L
985 int i, j;
986 long real_nsyms;
987 asymbol **real_syms;
988
989 real_syms = plugin_data->real_syms;
990 if (real_syms)
991 real_nsyms = plugin_data->real_nsyms;
992 else
993 real_nsyms = 0;
ce3c775b 994
ce3c775b
NC
995 for (i = 0; i < nsyms; i++)
996 {
68ffbac6 997 asymbol *s = bfd_alloc (abfd, sizeof (asymbol));
ce3c775b
NC
998
999 BFD_ASSERT (s);
1000 alocation[i] = s;
1001
1002 s->the_bfd = abfd;
1003 s->name = syms[i].name;
1004 s->value = 0;
1005 s->flags = convert_flags (&syms[i]);
1006 switch (syms[i].def)
1007 {
1008 case LDPK_COMMON:
1009 s->section = &fake_common_section;
1010 break;
1011 case LDPK_UNDEF:
1012 case LDPK_WEAKUNDEF:
1013 s->section = bfd_und_section_ptr;
1014 break;
1015 case LDPK_DEF:
1016 case LDPK_WEAKDEF:
1017 s->section = &fake_section;
0aa99dcd
L
1018 if (real_nsyms)
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)
1023 {
1024 s->section = real_syms[j]->section;
1025 break;
1026 }
ce3c775b
NC
1027 break;
1028 default:
1029 BFD_ASSERT (0);
1030 }
1031
1032 s->udata.p = (void *) &syms[i];
1033 }
1034
1035 return nsyms;
1036}
1037
1038static void
1039bfd_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)
1043{
1044 BFD_ASSERT (0);
1045}
1046
1047static void
1048bfd_plugin_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
1049 asymbol *symbol,
1050 symbol_info *ret)
1051{
1052 bfd_symbol_info (symbol, ret);
1053}
1054
1055/* Make an empty symbol. */
1056
1057static asymbol *
1058bfd_plugin_make_empty_symbol (bfd *abfd)
1059{
d3ce72d0
NC
1060 asymbol *new_symbol = bfd_zalloc (abfd, sizeof (asymbol));
1061 if (new_symbol == NULL)
1062 return new_symbol;
1063 new_symbol->the_bfd = abfd;
1064 return new_symbol;
ce3c775b
NC
1065}
1066
1067static int
1068bfd_plugin_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
1069 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1070{
1071 BFD_ASSERT (0);
1072 return 0;
1073}
1074
0aa99dcd
L
1075static bfd_boolean
1076bfd_plugin_close_and_cleanup (bfd *abfd)
1077{
1078 struct plugin_data_struct *plugin_data;
1079
1080 if (abfd->format != bfd_archive
1081 && (plugin_data = abfd->tdata.plugin_data))
1082 {
1083 if (plugin_data->real_bfd)
1084 bfd_close (plugin_data->real_bfd);
1085
1086 if (plugin_data->real_syms)
1087 free (plugin_data->real_syms);
1088 }
1089
1090 return _bfd_generic_close_and_cleanup (abfd);
1091}
1092
ce3c775b
NC
1093const bfd_target plugin_vec =
1094{
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. */
64bfc258 1107 255, /* match priority. */
ce3c775b
NC
1108
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 */
1115
1116 { /* bfd_check_format. */
1117 _bfd_dummy_target,
1118 bfd_plugin_object_p,
1119 bfd_generic_archive_p,
1120 _bfd_dummy_target
1121 },
1122 { /* bfd_set_format. */
d00dd7dc
AM
1123 _bfd_bool_bfd_false_error,
1124 _bfd_bool_bfd_false_error,
ce3c775b 1125 _bfd_generic_mkarchive,
d00dd7dc 1126 _bfd_bool_bfd_false_error,
ce3c775b
NC
1127 },
1128 { /* bfd_write_contents. */
d00dd7dc
AM
1129 _bfd_bool_bfd_false_error,
1130 _bfd_bool_bfd_false_error,
ce3c775b 1131 _bfd_write_archive_contents,
d00dd7dc 1132 _bfd_bool_bfd_false_error,
ce3c775b
NC
1133 },
1134
1135 BFD_JUMP_TABLE_GENERIC (bfd_plugin),
1136 BFD_JUMP_TABLE_COPY (bfd_plugin),
1137 BFD_JUMP_TABLE_CORE (bfd_plugin),
e6cc316a
L
1138#ifdef USE_64_BIT_ARCHIVE
1139 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_64_bit),
1140#else
ce3c775b 1141 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
e6cc316a 1142#endif
ce3c775b
NC
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),
1148
1149 NULL,
1150
07d6d2b8 1151 NULL /* backend_data. */
ce3c775b 1152};
98950613 1153#endif /* BFD_SUPPORTS_PLUGIN */
This page took 0.651912 seconds and 4 git commands to generate.