plugin: Use LDPT_ADD_SYMBOLS_V2 to get symbol type
[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;
c3a1714c 139 bfd_boolean has_symbol_type;
0aa99dcd
L
140
141 struct plugin_list_entry *next;
142
143 /* These can be reused for all IR objects. */
144 const char *plugin_name;
145 char *gcc;
146 char *lto_wrapper;
147 char *gcc_env;
148 bfd_boolean initialized;
149};
150
ecda9016
L
151static const char *plugin_program_name;
152static int need_lto_wrapper_p;
153
154void
155bfd_plugin_set_program_name (const char *program_name,
156 int need_lto_wrapper)
157{
158 plugin_program_name = program_name;
159 need_lto_wrapper_p = need_lto_wrapper;
160}
161
0aa99dcd
L
162/* Use GCC LTO wrapper to covert LTO IR object to the real object. */
163
164static bfd_boolean
165get_lto_wrapper (struct plugin_list_entry *plugin)
166{
167 struct stat st;
168 const char *real_name;
169 const char *base_name;
170 size_t length;
171 const char *target_start = NULL;
172 const char *target_end = NULL;
173 size_t target_length = 0;
174 char *gcc_name;
175 char *wrapper_name;
176 char *p;
177 char dir_seperator = '\0';
178 char *resolution_file;
179
ecda9016
L
180 if (!need_lto_wrapper_p)
181 return FALSE;
182
0aa99dcd
L
183 if (plugin->initialized)
184 {
185 if (plugin->lto_wrapper)
186 {
187 resolution_file = make_temp_file (".res");
188 if (resolution_file)
189 {
190 plugin->resolution_file = resolution_file;
191 plugin->resolution_option = concat ("-fresolution=",
192 resolution_file, NULL);
193 return TRUE;
194 }
195 else
196 {
197 /* Something is wrong. Give up. */
198 free (plugin->gcc);
199 free (plugin->lto_wrapper);
200 free (plugin->gcc_env);
201 plugin->gcc = NULL;
202 plugin->gcc_env = NULL;
203 plugin->lto_wrapper = NULL;
204 }
205 }
206
207 return FALSE;
208 }
209
210 plugin->initialized = TRUE;
211
212 /* Check for PREFIX/libexec/gcc/TARGET/VERSION/liblto_plugin.so. */
213 real_name = lrealpath (plugin->plugin_name);
214 base_name = lbasename (real_name);
215
216 /* The directory length in plugin pathname. */
217 length = base_name - real_name;
218
219 /* Skip if there is no PREFIX. */
220 if (!length)
221 return FALSE;
222
223 p = (char *) real_name + length - 1;
224 if (IS_DIR_SEPARATOR (*p))
225 {
226 int level = 0;
227 for (; p != real_name; p--)
228 if (IS_DIR_SEPARATOR (*p))
229 {
230 level++;
231 if (level == 2)
232 target_end = p;
233 else if (level == 3)
234 {
235 target_start = p + 1;
236 target_length = target_end - target_start;
237 }
238 else if (level == 5)
239 {
240 dir_seperator = *p;
241 break;
242 }
243 }
244 }
245
246 /* Skip if there is no TARGET nor PREFIX. */
247 if (!target_length || !dir_seperator)
248 return FALSE;
249
250#ifdef HAVE_EXECUTABLE_SUFFIX
251# define GCC_EXECUTABLE "gcc" EXECUTABLE_SUFFIX
252# define LTO_WRAPPER_EXECUTABLE "lto-wrapper" EXECUTABLE_SUFFIX
253#else
254# define GCC_EXECUTABLE "gcc"
255# define LTO_WRAPPER_EXECUTABLE "lto-wrapper"
256#endif
257 gcc_name = bfd_malloc (length + target_length
258 + sizeof (GCC_EXECUTABLE));
259 if (gcc_name == NULL)
260 return FALSE;
261 memcpy (gcc_name, real_name, length);
262
263 /* Get PREFIX/bin/. */
264 p += gcc_name - real_name;
265 memcpy (p + 1, "bin", 3);
266 p[4] = dir_seperator;
267
268 /* Try PREFIX/bin/TARGET-gcc first. */
269 memcpy (p + 5, target_start, target_length);
270 p[5 + target_length] = '-';
271 memcpy (p + 5 + target_length + 1, GCC_EXECUTABLE,
272 sizeof (GCC_EXECUTABLE));
273 if (stat (gcc_name, &st) != 0 || !S_ISREG (st.st_mode))
274 {
275 /* Then try PREFIX/bin/gcc. */
276 memcpy (p + 5, GCC_EXECUTABLE, sizeof (GCC_EXECUTABLE));
277 if (stat (gcc_name, &st) != 0 || !S_ISREG (st.st_mode))
278 {
279 free (gcc_name);
280 return FALSE;
281 }
282 }
283
284 /* lto-wrapper should be in the same directory with LTO plugin. */
285 wrapper_name = bfd_malloc (length + sizeof (LTO_WRAPPER_EXECUTABLE));
286 if (wrapper_name == NULL)
287 {
288 free (gcc_name);
289 return FALSE;
290 }
291 memcpy (wrapper_name, real_name, length);
292 memcpy (wrapper_name + length, LTO_WRAPPER_EXECUTABLE,
293 sizeof (LTO_WRAPPER_EXECUTABLE));
294 if (stat (wrapper_name, &st) == 0 && S_ISREG (st.st_mode))
295 {
296 resolution_file = make_temp_file (".res");
297 if (resolution_file)
298 {
299 plugin->gcc = gcc_name;
300 plugin->lto_wrapper = wrapper_name;
301 plugin->gcc_env = concat ("COLLECT_GCC=", gcc_name, NULL);
302 plugin->resolution_file = resolution_file;
303 plugin->resolution_option = concat ("-fresolution=",
304 resolution_file, NULL);
305 return TRUE;
306 }
307 }
308
309 free (gcc_name);
310 free (wrapper_name);
311 return FALSE;
312}
313
314/* Set environment variables for GCC LTO wrapper to covert LTO IR
315 object to the real object. */
316
317static int
318setup_lto_wrapper_env (struct plugin_list_entry *plugin)
319{
320 return (putenv (plugin->gcc_env)
321 || putenv ("COLLECT_GCC_OPTIONS="));
322}
323
324static struct plugin_list_entry *plugin_list = NULL;
325static struct plugin_list_entry *current_plugin = NULL;
326
ce3c775b 327/* Register a claim-file handler. */
ce3c775b
NC
328
329static enum ld_plugin_status
330register_claim_file (ld_plugin_claim_file_handler handler)
331{
0aa99dcd
L
332 current_plugin->claim_file = handler;
333 return LDPS_OK;
334}
335
336/* Register an all-symbols-read handler. */
337
338static enum ld_plugin_status
339register_all_symbols_read (ld_plugin_all_symbols_read_handler handler)
340{
341 current_plugin->all_symbols_read = handler;
342 return LDPS_OK;
343}
344
345/* Register a cleanup handler. */
346
347static enum ld_plugin_status
348register_cleanup (ld_plugin_all_symbols_read_handler handler)
349{
350 current_plugin->cleanup_handler = handler;
351 return LDPS_OK;
352}
353
354/* Get the symbol resolution info for a plugin-claimed input file. */
355
356static enum ld_plugin_status
357get_symbols (const void *handle ATTRIBUTE_UNUSED, int nsyms,
358 struct ld_plugin_symbol *syms)
359{
360 if (syms)
361 {
362 int n;
363 for (n = 0; n < nsyms; n++)
364 {
365 switch (syms[n].def)
366 {
367 default:
368 BFD_ASSERT (0);
369 break;
370 case LDPK_UNDEF:
371 case LDPK_WEAKUNDEF:
372 syms[n].resolution = LDPR_UNDEF;
373 break;
374 case LDPK_DEF:
375 case LDPK_WEAKDEF:
376 case LDPK_COMMON:
377 /* Tell plugin that LTO symbol has references from regular
378 object code. */
379 syms[n].resolution = LDPR_PREVAILING_DEF;
380 break;
381 }
382 }
383 }
384
385 return LDPS_OK;
386}
387
388/* Add a new (real) input file generated by a plugin. */
389
390static enum ld_plugin_status
391add_input_file (const char *pathname)
392{
393 /* Get symbols from the real LTO object. */
394 char **matching;
395 long real_symsize;
396 long real_nsyms;
397 asymbol **real_syms;
398 int lto_nsyms;
399 bfd_boolean lto_symbol_found = FALSE;
400 const struct ld_plugin_symbol *lto_syms;
401 bfd *rbfd;
402 int i, j;
403
404 rbfd = bfd_openr (pathname, NULL);
405 if (!bfd_check_format_matches (rbfd, bfd_object, &matching))
406 BFD_ASSERT (0);
407
408 real_symsize = bfd_get_symtab_upper_bound (rbfd);
409 if (real_symsize < 0)
410 BFD_ASSERT (0);
411
412 real_syms = (asymbol **) bfd_malloc (real_symsize);
413 if (real_syms)
414 {
415 real_nsyms = bfd_canonicalize_symtab (rbfd, real_syms);
416 if (real_nsyms < 0)
417 BFD_ASSERT (0);
418
419 /* NB: LTO plugin may generate more than one real object from one
420 LTO IR object. We use the one which contains LTO symbols. */
421 lto_syms = current_plugin->lto_syms;
422 lto_nsyms = current_plugin->lto_nsyms;
423 for (i = 0; i < lto_nsyms; i++)
424 for (j = 0; j < real_nsyms; j++)
425 if (real_syms[j]->name
426 && strcmp (lto_syms[i].name, real_syms[j]->name) == 0)
427 {
428 lto_symbol_found = TRUE;
429 break;
430 }
431 }
432
433 if (lto_symbol_found)
434 {
435 current_plugin->real_nsyms = real_nsyms;
436 current_plugin->real_syms = real_syms;
437 /* NB: We can't close RBFD which own the real symbol info. */
438 current_plugin->real_bfd = rbfd;
439 }
440 else
441 {
442 bfd_close (rbfd);
443 free (real_syms);
444 }
445
ce3c775b
NC
446 return LDPS_OK;
447}
448
449static enum ld_plugin_status
450add_symbols (void * handle,
451 int nsyms,
452 const struct ld_plugin_symbol * syms)
453{
454 bfd *abfd = handle;
455 struct plugin_data_struct *plugin_data =
5bb3703f 456 bfd_alloc (abfd, sizeof (plugin_data_struct));
ce3c775b 457
0aa99dcd
L
458 if (plugin_data)
459 {
460 struct ld_plugin_symbol *sym_info;
461 char *strtab;
462 size_t sym_info_size, name_length;
463 int i;
464
1d07a805
L
465 memset (plugin_data, 0, sizeof (*plugin_data));
466
0aa99dcd
L
467 abfd->tdata.plugin_data = plugin_data;
468
469 /* NB: LTO symbols are owned by LTO plugin. Create a copy so
470 that we can use it in bfd_plugin_canonicalize_symtab. */
471 sym_info_size = nsyms * sizeof (*syms);
472
473 /* Allocate a string table */
474 for (i = 0; i < nsyms; i++)
475 sym_info_size += strlen (syms[i].name) + 1;
476
477 sym_info = bfd_alloc (abfd, sym_info_size);
478 if (sym_info)
479 {
480 /* Copy symbol table. */
481 memcpy (sym_info, syms, nsyms * sizeof (*syms));
482
483 /* Copy symbol names in symbol table. */
484 strtab = (char *) (sym_info + nsyms);
485 for (i = 0; i < nsyms; i++)
486 {
487 name_length = strlen (syms[i].name);
488 memcpy (strtab, syms[i].name, name_length + 1);
489 sym_info[i].name = strtab;
490 strtab += name_length + 1;
491 }
492
493 plugin_data->nsyms = nsyms;
494 plugin_data->syms = sym_info;
495
496 current_plugin->lto_nsyms = nsyms;
497 current_plugin->lto_syms = sym_info;
498 }
499 }
ce3c775b
NC
500
501 if (nsyms != 0)
502 abfd->flags |= HAS_SYMS;
503
ce3c775b
NC
504 return LDPS_OK;
505}
506
c3a1714c
L
507static enum ld_plugin_status
508add_symbols_v2 (void *handle, int nsyms,
509 const struct ld_plugin_symbol *syms)
510{
511 current_plugin->has_symbol_type = TRUE;
512 return add_symbols (handle, nsyms, syms);
513}
514
7d0b9ebc
AM
515int
516bfd_plugin_open_input (bfd *ibfd, struct ld_plugin_input_file *file)
ce3c775b 517{
e44f5bef
MT
518 bfd *iobfd;
519
7d0b9ebc 520 iobfd = ibfd;
27b07675
AM
521 while (iobfd->my_archive
522 && !bfd_is_thin_archive (iobfd->my_archive))
523 iobfd = iobfd->my_archive;
7d0b9ebc 524 file->name = iobfd->filename;
e44f5bef
MT
525
526 if (!iobfd->iostream && !bfd_open_file (iobfd))
527 return 0;
528
27b07675
AM
529 /* The plugin API expects that the file descriptor won't be closed
530 and reused as done by the bfd file cache. So open it again.
531 dup isn't good enough. plugin IO uses lseek/read while BFD uses
532 fseek/fread. It isn't wise to mix the unistd and stdio calls on
533 the same underlying file descriptor. */
534 file->fd = open (file->name, O_RDONLY | O_BINARY);
535 if (file->fd < 0)
536 return 0;
e44f5bef 537
7d0b9ebc 538 if (iobfd == ibfd)
e44f5bef
MT
539 {
540 struct stat stat_buf;
103da91b 541
7d0b9ebc 542 if (fstat (file->fd, &stat_buf))
103da91b
NC
543 {
544 close(file->fd);
545 return 0;
546 }
547
7d0b9ebc
AM
548 file->offset = 0;
549 file->filesize = stat_buf.st_size;
550 }
551 else
552 {
553 file->offset = ibfd->origin;
554 file->filesize = arelt_size (ibfd);
e44f5bef 555 }
7d0b9ebc
AM
556 return 1;
557}
558
559static int
560try_claim (bfd *abfd)
561{
562 int claimed = 0;
563 struct ld_plugin_input_file file;
e44f5bef 564
27b07675 565 file.handle = abfd;
22fe7df8
L
566 if (bfd_plugin_open_input (abfd, &file)
567 && current_plugin->claim_file)
0aa99dcd
L
568 {
569 current_plugin->claim_file (&file, &claimed);
570 if (claimed)
571 {
c3a1714c
L
572 if (current_plugin->all_symbols_read
573 && !current_plugin->has_symbol_type)
0aa99dcd
L
574 {
575 struct plugin_data_struct *plugin_data
576 = abfd->tdata.plugin_data;
577 if (plugin_data)
578 {
579 /* Get real symbols from LTO wrapper. */
580 current_plugin->all_symbols_read ();
581
582 /* Copy real symbols to plugin_data. */
583 plugin_data->real_bfd = current_plugin->real_bfd;
584 plugin_data->real_nsyms = current_plugin->real_nsyms;
585 plugin_data->real_syms = current_plugin->real_syms;
586
587 /* Clean up LTO plugin. */
588 if (current_plugin->cleanup_handler)
589 current_plugin->cleanup_handler ();
590 }
591 }
592 }
593
22fe7df8 594 close (file.fd);
0aa99dcd 595 }
22fe7df8
L
596
597 if (current_plugin->lto_wrapper)
598 {
599 /* Clean up for LTO wrapper. NB: Resolution file and option
600 have been created regardless if an IR object is claimed or
601 not. */
602 unlink (current_plugin->resolution_file);
603 free (current_plugin->resolution_option);
604 }
605
7d0b9ebc 606 return claimed;
e44f5bef
MT
607}
608
609static int
99845b3b
L
610try_load_plugin (const char *pname,
611 struct plugin_list_entry *plugin_list_iter,
612 bfd *abfd, bfd_boolean build_list_p)
e44f5bef 613{
dcf06b89 614 void *plugin_handle;
c3a1714c 615 struct ld_plugin_tv tv[13];
ce3c775b
NC
616 int i;
617 ld_plugin_onload onload;
618 enum ld_plugin_status status;
dcf06b89 619 int result = 0;
5ae0078c 620
22fe7df8
L
621 /* NB: Each object is independent. Reuse the previous plugin from
622 the last run will lead to wrong result. */
623 if (current_plugin)
dcf06b89
L
624 memset (current_plugin, 0,
625 offsetof (struct plugin_list_entry, next));
0aa99dcd 626
99845b3b
L
627 if (plugin_list_iter)
628 pname = plugin_list_iter->plugin_name;
629
ce3c775b
NC
630 plugin_handle = dlopen (pname, RTLD_NOW);
631 if (!plugin_handle)
632 {
4eca0228 633 _bfd_error_handler ("%s\n", dlerror ());
ce3c775b
NC
634 return 0;
635 }
636
22fe7df8 637 if (plugin_list_iter == NULL)
b0ceb98a 638 {
22fe7df8
L
639 size_t length_plugin_name = strlen (pname) + 1;
640 char *plugin_name = bfd_malloc (length_plugin_name);
641 if (plugin_name == NULL)
dcf06b89 642 goto short_circuit;
22fe7df8
L
643 plugin_list_iter = bfd_malloc (sizeof *plugin_list_iter);
644 if (plugin_list_iter == NULL)
4b24dd1a 645 {
22fe7df8 646 free (plugin_name);
dcf06b89 647 goto short_circuit;
4b24dd1a 648 }
22fe7df8
L
649 /* Make a copy of PNAME since PNAME from load_plugin () will be
650 freed. */
651 memcpy (plugin_name, pname, length_plugin_name);
652 memset (plugin_list_iter, 0, sizeof (*plugin_list_iter));
653 plugin_list_iter->plugin_name = plugin_name;
654 plugin_list_iter->next = plugin_list;
655 plugin_list = plugin_list_iter;
b0ceb98a
Z
656 }
657
dcf06b89 658 current_plugin = plugin_list_iter;
99845b3b 659 if (build_list_p)
dcf06b89 660 goto short_circuit;
0aa99dcd 661
ce3c775b
NC
662 onload = dlsym (plugin_handle, "onload");
663 if (!onload)
dcf06b89 664 goto short_circuit;
ce3c775b
NC
665
666 i = 0;
667 tv[i].tv_tag = LDPT_MESSAGE;
668 tv[i].tv_u.tv_message = message;
669
670 ++i;
671 tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
672 tv[i].tv_u.tv_register_claim_file = register_claim_file;
673
674 ++i;
675 tv[i].tv_tag = LDPT_ADD_SYMBOLS;
676 tv[i].tv_u.tv_add_symbols = add_symbols;
677
c3a1714c
L
678 ++i;
679 tv[i].tv_tag = LDPT_ADD_SYMBOLS_V2;
680 tv[i].tv_u.tv_add_symbols = add_symbols_v2;
681
0aa99dcd
L
682 if (get_lto_wrapper (plugin_list_iter))
683 {
684 ++i;
685 tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
686 tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
687
688 ++i;
689 tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
690 tv[i].tv_u.tv_register_cleanup = register_cleanup;
691
692 ++i;
693 tv[i].tv_tag = LDPT_GET_SYMBOLS;
694 tv[i].tv_u.tv_get_symbols = get_symbols;
695
696 ++i;
697 tv[i].tv_tag = LDPT_GET_SYMBOLS_V2;
698 tv[i].tv_u.tv_get_symbols = get_symbols;
699
700 ++i;
701 tv[i].tv_tag = LDPT_OPTION;
702 tv[i].tv_u.tv_string = plugin_list_iter->lto_wrapper;
703
704 ++i;
705 tv[i].tv_tag = LDPT_OPTION;
706 tv[i].tv_u.tv_string = plugin_list_iter->resolution_option;
707
708 ++i;
709 tv[i].tv_tag = LDPT_LINKER_OUTPUT;
710 tv[i].tv_u.tv_val = LDPO_EXEC;
711
712 ++i;
713 tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
714 tv[i].tv_u.tv_add_input_file = add_input_file;
715 }
716
ce3c775b
NC
717 ++i;
718 tv[i].tv_tag = LDPT_NULL;
719 tv[i].tv_u.tv_val = 0;
720
0aa99dcd 721 /* LTO plugin will call handler hooks to set up plugin handlers. */
ce3c775b
NC
722 status = (*onload)(tv);
723
724 if (status != LDPS_OK)
dcf06b89 725 goto short_circuit;
b0ceb98a 726
0aa99dcd
L
727 if (current_plugin->lto_wrapper
728 && setup_lto_wrapper_env (current_plugin))
dcf06b89 729 goto short_circuit;
ce3c775b 730
5ae0078c
L
731 abfd->plugin_format = bfd_plugin_no;
732
0aa99dcd 733 if (!current_plugin->claim_file)
dcf06b89 734 goto short_circuit;
ce3c775b 735
e44f5bef 736 if (!try_claim (abfd))
dcf06b89 737 goto short_circuit;
e44f5bef 738
5ae0078c 739 abfd->plugin_format = bfd_plugin_yes;
dcf06b89
L
740 result = 1;
741
dc1e8a47 742 short_circuit:
dcf06b89
L
743 dlclose (plugin_handle);
744 return result;
ce3c775b
NC
745}
746
5ae0078c 747/* There may be plugin libraries in lib/bfd-plugins. */
99845b3b 748static int has_plugin_list = -1;
5ae0078c 749
cb001c0d 750static bfd_cleanup (*ld_plugin_object_p) (bfd *);
5ae0078c 751
ce3c775b
NC
752static const char *plugin_name;
753
754void
755bfd_plugin_set_plugin (const char *p)
756{
757 plugin_name = p;
5ae0078c
L
758}
759
760/* Return TRUE if a plugin library is used. */
761
762bfd_boolean
763bfd_plugin_specified_p (void)
764{
99845b3b 765 return plugin_list != NULL;
5ae0078c
L
766}
767
7dc3990e
L
768/* Return TRUE if ABFD can be claimed by linker LTO plugin. */
769
770bfd_boolean
771bfd_link_plugin_object_p (bfd *abfd)
772{
773 if (ld_plugin_object_p)
774 return ld_plugin_object_p (abfd) != NULL;
775 return FALSE;
776}
777
5ae0078c
L
778extern const bfd_target plugin_vec;
779
780/* Return TRUE if TARGET is a pointer to plugin_vec. */
781
782bfd_boolean
783bfd_plugin_target_p (const bfd_target *target)
784{
785 return target == &plugin_vec;
786}
787
788/* Register OBJECT_P to be used by bfd_plugin_object_p. */
789
790void
cb001c0d 791register_ld_plugin_object_p (bfd_cleanup (*object_p) (bfd *))
5ae0078c
L
792{
793 ld_plugin_object_p = object_p;
ce3c775b
NC
794}
795
99845b3b
L
796static void
797build_plugin_list (bfd *abfd)
ce3c775b 798{
41f37a6f
AM
799 /* The intent was to search ${libdir}/bfd-plugins for plugins, but
800 unfortunately the original implementation wasn't precisely that
801 when configuring binutils using --libdir. Search in the proper
802 path first, then the old one for backwards compatibility. */
803 static const char *path[]
804 = { LIBDIR "/bfd-plugins", BINDIR "/../lib/bfd-plugins" };
805 struct stat last_st;
41f37a6f 806 unsigned int i;
ce3c775b 807
99845b3b
L
808 if (has_plugin_list >= 0)
809 return;
ce3c775b 810
41f37a6f
AM
811 /* Try not to search the same dir twice, by looking at st_dev and
812 st_ino for the dir. If we are on a file system that always sets
813 st_ino to zero or the actual st_ino is zero we might waste some
814 time, but that doesn't matter too much. */
815 last_st.st_dev = 0;
816 last_st.st_ino = 0;
817 for (i = 0; i < sizeof (path) / sizeof (path[0]); i++)
ce3c775b 818 {
41f37a6f
AM
819 char *plugin_dir = make_relative_prefix (plugin_program_name,
820 BINDIR,
821 path[i]);
822 if (plugin_dir)
823 {
824 struct stat st;
825 DIR *d;
826
827 if (stat (plugin_dir, &st) == 0
828 && S_ISDIR (st.st_mode)
829 && !(last_st.st_dev == st.st_dev
830 && last_st.st_ino == st.st_ino
831 && st.st_ino != 0)
832 && (d = opendir (plugin_dir)) != NULL)
833 {
834 struct dirent *ent;
835
836 last_st.st_dev = st.st_dev;
837 last_st.st_ino = st.st_ino;
838 while ((ent = readdir (d)) != NULL)
839 {
840 char *full_name;
841
842 full_name = concat (plugin_dir, "/", ent->d_name, NULL);
843 if (stat (full_name, &st) == 0 && S_ISREG (st.st_mode))
99845b3b 844 try_load_plugin (full_name, NULL, abfd, TRUE);
41f37a6f 845 free (full_name);
41f37a6f
AM
846 }
847 closedir (d);
848 }
849 free (plugin_dir);
850 }
ce3c775b
NC
851 }
852
99845b3b
L
853 has_plugin_list = plugin_list != NULL;
854}
855
856static int
857load_plugin (bfd *abfd)
858{
859 struct plugin_list_entry *plugin_list_iter;
860
861 if (plugin_name)
862 return try_load_plugin (plugin_name, plugin_list, abfd, FALSE);
863
864 if (plugin_program_name == NULL)
865 return 0;
866
867 build_plugin_list (abfd);
868
869 for (plugin_list_iter = plugin_list;
870 plugin_list_iter;
871 plugin_list_iter = plugin_list_iter->next)
872 if (try_load_plugin (NULL, plugin_list_iter, abfd, FALSE))
873 return 1;
874
875 return 0;
ce3c775b
NC
876}
877
878
cb001c0d 879static bfd_cleanup
ce3c775b
NC
880bfd_plugin_object_p (bfd *abfd)
881{
5ae0078c
L
882 if (ld_plugin_object_p)
883 return ld_plugin_object_p (abfd);
884
49f30d83 885 if (abfd->plugin_format == bfd_plugin_unknown && !load_plugin (abfd))
ce3c775b
NC
886 return NULL;
887
cb001c0d 888 return abfd->plugin_format == bfd_plugin_yes ? _bfd_no_cleanup : NULL;
ce3c775b
NC
889}
890
891/* Copy any private info we understand from the input bfd
892 to the output bfd. */
893
894static bfd_boolean
895bfd_plugin_bfd_copy_private_bfd_data (bfd *ibfd ATTRIBUTE_UNUSED,
896 bfd *obfd ATTRIBUTE_UNUSED)
897{
898 BFD_ASSERT (0);
899 return TRUE;
900}
901
902/* Copy any private info we understand from the input section
903 to the output section. */
904
905static bfd_boolean
906bfd_plugin_bfd_copy_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
907 asection *isection ATTRIBUTE_UNUSED,
908 bfd *obfd ATTRIBUTE_UNUSED,
909 asection *osection ATTRIBUTE_UNUSED)
910{
911 BFD_ASSERT (0);
912 return TRUE;
913}
914
915/* Copy any private info we understand from the input symbol
916 to the output symbol. */
917
918static bfd_boolean
919bfd_plugin_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
920 asymbol *isymbol ATTRIBUTE_UNUSED,
921 bfd *obfd ATTRIBUTE_UNUSED,
922 asymbol *osymbol ATTRIBUTE_UNUSED)
923{
924 BFD_ASSERT (0);
925 return TRUE;
926}
927
928static bfd_boolean
929bfd_plugin_bfd_print_private_bfd_data (bfd *abfd ATTRIBUTE_UNUSED, PTR ptr ATTRIBUTE_UNUSED)
930{
931 BFD_ASSERT (0);
932 return TRUE;
933}
934
935static char *
936bfd_plugin_core_file_failing_command (bfd *abfd ATTRIBUTE_UNUSED)
937{
938 BFD_ASSERT (0);
939 return NULL;
940}
941
942static int
943bfd_plugin_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
261b8d08
PA
944{
945 BFD_ASSERT (0);
946 return 0;
947}
948
949static int
950bfd_plugin_core_file_pid (bfd *abfd ATTRIBUTE_UNUSED)
ce3c775b
NC
951{
952 BFD_ASSERT (0);
953 return 0;
954}
955
956static long
957bfd_plugin_get_symtab_upper_bound (bfd *abfd)
958{
959 struct plugin_data_struct *plugin_data = abfd->tdata.plugin_data;
960 long nsyms = plugin_data->nsyms;
961
962 BFD_ASSERT (nsyms >= 0);
963
964 return ((nsyms + 1) * sizeof (asymbol *));
965}
966
967static flagword
968convert_flags (const struct ld_plugin_symbol *sym)
969{
970 switch (sym->def)
68ffbac6 971 {
ce3c775b
NC
972 case LDPK_DEF:
973 case LDPK_COMMON:
974 case LDPK_UNDEF:
975 return BSF_GLOBAL;
976
977 case LDPK_WEAKUNDEF:
978 case LDPK_WEAKDEF:
979 return BSF_GLOBAL | BSF_WEAK;
980
981 default:
982 BFD_ASSERT (0);
983 return 0;
984 }
985}
986
987static long
988bfd_plugin_canonicalize_symtab (bfd *abfd,
989 asymbol **alocation)
990{
991 struct plugin_data_struct *plugin_data = abfd->tdata.plugin_data;
992 long nsyms = plugin_data->nsyms;
993 const struct ld_plugin_symbol *syms = plugin_data->syms;
c3a1714c
L
994 static asection fake_text_section
995 = BFD_FAKE_SECTION (fake_text_section, NULL, "plug", 0,
a288c270 996 SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS);
c3a1714c
L
997 static asection fake_data_section
998 = BFD_FAKE_SECTION (fake_data_section, NULL, "plug", 0,
999 SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS);
1000 static asection fake_bss_section
1001 = BFD_FAKE_SECTION (fake_bss_section, NULL, "plug", 0,
1002 SEC_ALLOC);
a288c270
AM
1003 static asection fake_common_section
1004 = BFD_FAKE_SECTION (fake_common_section, NULL, "plug", 0, SEC_IS_COMMON);
0aa99dcd
L
1005 int i, j;
1006 long real_nsyms;
1007 asymbol **real_syms;
1008
1009 real_syms = plugin_data->real_syms;
1010 if (real_syms)
1011 real_nsyms = plugin_data->real_nsyms;
1012 else
1013 real_nsyms = 0;
ce3c775b 1014
ce3c775b
NC
1015 for (i = 0; i < nsyms; i++)
1016 {
68ffbac6 1017 asymbol *s = bfd_alloc (abfd, sizeof (asymbol));
ce3c775b
NC
1018
1019 BFD_ASSERT (s);
1020 alocation[i] = s;
1021
1022 s->the_bfd = abfd;
1023 s->name = syms[i].name;
1024 s->value = 0;
1025 s->flags = convert_flags (&syms[i]);
1026 switch (syms[i].def)
1027 {
1028 case LDPK_COMMON:
1029 s->section = &fake_common_section;
1030 break;
1031 case LDPK_UNDEF:
1032 case LDPK_WEAKUNDEF:
1033 s->section = bfd_und_section_ptr;
1034 break;
1035 case LDPK_DEF:
1036 case LDPK_WEAKDEF:
c3a1714c
L
1037 if (current_plugin->has_symbol_type)
1038 switch (syms[i].symbol_type)
1039 {
1040 case LDST_UNKNOWN:
1041 /* What is the best fake section for LDST_UNKNOWN? */
1042 case LDST_FUNCTION:
1043 s->section = &fake_text_section;
1044 break;
1045 case LDST_VARIABLE:
1046 if (syms[i].section_kind == LDSSK_BSS)
1047 s->section = &fake_bss_section;
1048 else
1049 s->section = &fake_data_section;
1050 break;
1051 }
1052 else
1053 {
1054 s->section = &fake_text_section;
1055 if (real_nsyms)
1056 /* Use real LTO symbols if possible. */
1057 for (j = 0; j < real_nsyms; j++)
1058 if (real_syms[j]->name
1059 && strcmp (syms[i].name, real_syms[j]->name) == 0)
1060 {
1061 s->section = real_syms[j]->section;
1062 break;
1063 }
1064 }
ce3c775b
NC
1065 break;
1066 default:
1067 BFD_ASSERT (0);
1068 }
1069
1070 s->udata.p = (void *) &syms[i];
1071 }
1072
1073 return nsyms;
1074}
1075
1076static void
1077bfd_plugin_print_symbol (bfd *abfd ATTRIBUTE_UNUSED,
1078 PTR afile ATTRIBUTE_UNUSED,
1079 asymbol *symbol ATTRIBUTE_UNUSED,
1080 bfd_print_symbol_type how ATTRIBUTE_UNUSED)
1081{
1082 BFD_ASSERT (0);
1083}
1084
1085static void
1086bfd_plugin_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
1087 asymbol *symbol,
1088 symbol_info *ret)
1089{
1090 bfd_symbol_info (symbol, ret);
1091}
1092
1093/* Make an empty symbol. */
1094
1095static asymbol *
1096bfd_plugin_make_empty_symbol (bfd *abfd)
1097{
d3ce72d0
NC
1098 asymbol *new_symbol = bfd_zalloc (abfd, sizeof (asymbol));
1099 if (new_symbol == NULL)
1100 return new_symbol;
1101 new_symbol->the_bfd = abfd;
1102 return new_symbol;
ce3c775b
NC
1103}
1104
1105static int
1106bfd_plugin_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
1107 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1108{
1109 BFD_ASSERT (0);
1110 return 0;
1111}
1112
0aa99dcd
L
1113static bfd_boolean
1114bfd_plugin_close_and_cleanup (bfd *abfd)
1115{
1116 struct plugin_data_struct *plugin_data;
1117
1118 if (abfd->format != bfd_archive
1119 && (plugin_data = abfd->tdata.plugin_data))
1120 {
1121 if (plugin_data->real_bfd)
1122 bfd_close (plugin_data->real_bfd);
1123
1124 if (plugin_data->real_syms)
1125 free (plugin_data->real_syms);
1126 }
1127
1128 return _bfd_generic_close_and_cleanup (abfd);
1129}
1130
ce3c775b
NC
1131const bfd_target plugin_vec =
1132{
1133 "plugin", /* Name. */
1134 bfd_target_unknown_flavour,
1135 BFD_ENDIAN_LITTLE, /* Target byte order. */
1136 BFD_ENDIAN_LITTLE, /* Target headers byte order. */
1137 (HAS_RELOC | EXEC_P | /* Object flags. */
1138 HAS_LINENO | HAS_DEBUG |
1139 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
1140 (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
1141 | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* Section flags. */
1142 0, /* symbol_leading_char. */
1143 '/', /* ar_pad_char. */
1144 15, /* ar_max_namelen. */
64bfc258 1145 255, /* match priority. */
ce3c775b
NC
1146
1147 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1148 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1149 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
1150 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1151 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1152 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
1153
1154 { /* bfd_check_format. */
1155 _bfd_dummy_target,
1156 bfd_plugin_object_p,
1157 bfd_generic_archive_p,
1158 _bfd_dummy_target
1159 },
1160 { /* bfd_set_format. */
d00dd7dc
AM
1161 _bfd_bool_bfd_false_error,
1162 _bfd_bool_bfd_false_error,
ce3c775b 1163 _bfd_generic_mkarchive,
d00dd7dc 1164 _bfd_bool_bfd_false_error,
ce3c775b
NC
1165 },
1166 { /* bfd_write_contents. */
d00dd7dc
AM
1167 _bfd_bool_bfd_false_error,
1168 _bfd_bool_bfd_false_error,
ce3c775b 1169 _bfd_write_archive_contents,
d00dd7dc 1170 _bfd_bool_bfd_false_error,
ce3c775b
NC
1171 },
1172
1173 BFD_JUMP_TABLE_GENERIC (bfd_plugin),
1174 BFD_JUMP_TABLE_COPY (bfd_plugin),
1175 BFD_JUMP_TABLE_CORE (bfd_plugin),
e6cc316a
L
1176#ifdef USE_64_BIT_ARCHIVE
1177 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_64_bit),
1178#else
ce3c775b 1179 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
e6cc316a 1180#endif
ce3c775b
NC
1181 BFD_JUMP_TABLE_SYMBOLS (bfd_plugin),
1182 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
1183 BFD_JUMP_TABLE_WRITE (bfd_plugin),
1184 BFD_JUMP_TABLE_LINK (bfd_plugin),
1185 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
1186
1187 NULL,
1188
07d6d2b8 1189 NULL /* backend_data. */
ce3c775b 1190};
98950613 1191#endif /* BFD_SUPPORTS_PLUGIN */
This page took 0.688136 seconds and 4 git commands to generate.