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