Automatic date update in version.in
[external/binutils.git] / bfd / plugin.c
1 /* Plugin support for BFD.
2    Copyright (C) 2009-2019 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_close_and_cleanup                  _bfd_generic_close_and_cleanup
73 #define bfd_plugin_bfd_free_cached_info               _bfd_generic_bfd_free_cached_info
74 #define bfd_plugin_new_section_hook                   _bfd_generic_new_section_hook
75 #define bfd_plugin_get_section_contents               _bfd_generic_get_section_contents
76 #define bfd_plugin_get_section_contents_in_window     _bfd_generic_get_section_contents_in_window
77 #define bfd_plugin_bfd_copy_private_header_data       _bfd_generic_bfd_copy_private_header_data
78 #define bfd_plugin_bfd_merge_private_bfd_data         _bfd_generic_bfd_merge_private_bfd_data
79 #define bfd_plugin_bfd_copy_private_header_data       _bfd_generic_bfd_copy_private_header_data
80 #define bfd_plugin_bfd_set_private_flags              _bfd_generic_bfd_set_private_flags
81 #define bfd_plugin_core_file_matches_executable_p     generic_core_file_matches_executable_p
82 #define bfd_plugin_bfd_is_local_label_name            _bfd_nosymbols_bfd_is_local_label_name
83 #define bfd_plugin_bfd_is_target_special_symbol       _bfd_bool_bfd_asymbol_false
84 #define bfd_plugin_get_lineno                         _bfd_nosymbols_get_lineno
85 #define bfd_plugin_find_nearest_line                  _bfd_nosymbols_find_nearest_line
86 #define bfd_plugin_find_line                          _bfd_nosymbols_find_line
87 #define bfd_plugin_find_inliner_info                  _bfd_nosymbols_find_inliner_info
88 #define bfd_plugin_get_symbol_version_string          _bfd_nosymbols_get_symbol_version_string
89 #define bfd_plugin_bfd_make_debug_symbol              _bfd_nosymbols_bfd_make_debug_symbol
90 #define bfd_plugin_read_minisymbols                   _bfd_generic_read_minisymbols
91 #define bfd_plugin_minisymbol_to_symbol               _bfd_generic_minisymbol_to_symbol
92 #define bfd_plugin_set_arch_mach                      bfd_default_set_arch_mach
93 #define bfd_plugin_set_section_contents               _bfd_generic_set_section_contents
94 #define bfd_plugin_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
95 #define bfd_plugin_bfd_relax_section                  bfd_generic_relax_section
96 #define bfd_plugin_bfd_link_hash_table_create         _bfd_generic_link_hash_table_create
97 #define bfd_plugin_bfd_link_add_symbols               _bfd_generic_link_add_symbols
98 #define bfd_plugin_bfd_link_just_syms                 _bfd_generic_link_just_syms
99 #define bfd_plugin_bfd_final_link                     _bfd_generic_final_link
100 #define bfd_plugin_bfd_link_split_section             _bfd_generic_link_split_section
101 #define bfd_plugin_bfd_gc_sections                    bfd_generic_gc_sections
102 #define bfd_plugin_bfd_lookup_section_flags           bfd_generic_lookup_section_flags
103 #define bfd_plugin_bfd_merge_sections                 bfd_generic_merge_sections
104 #define bfd_plugin_bfd_is_group_section               bfd_generic_is_group_section
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 /* Register a claim-file handler. */
127 static ld_plugin_claim_file_handler claim_file = NULL;
128
129 static enum ld_plugin_status
130 register_claim_file (ld_plugin_claim_file_handler handler)
131 {
132   claim_file = handler;
133   return LDPS_OK;
134 }
135
136 static enum ld_plugin_status
137 add_symbols (void * handle,
138              int nsyms,
139              const struct ld_plugin_symbol * syms)
140 {
141   bfd *abfd = handle;
142   struct plugin_data_struct *plugin_data =
143     bfd_alloc (abfd, sizeof (plugin_data_struct));
144
145   plugin_data->nsyms = nsyms;
146   plugin_data->syms = syms;
147
148   if (nsyms != 0)
149     abfd->flags |= HAS_SYMS;
150
151   abfd->tdata.plugin_data = plugin_data;
152   return LDPS_OK;
153 }
154
155 static const char *plugin_program_name;
156
157 void
158 bfd_plugin_set_program_name (const char *program_name)
159 {
160   plugin_program_name = program_name;
161 }
162
163 int
164 bfd_plugin_open_input (bfd *ibfd, struct ld_plugin_input_file *file)
165 {
166   bfd *iobfd;
167
168   iobfd = ibfd;
169   while (iobfd->my_archive
170          && !bfd_is_thin_archive (iobfd->my_archive))
171     iobfd = iobfd->my_archive;
172   file->name = iobfd->filename;
173
174   if (!iobfd->iostream && !bfd_open_file (iobfd))
175     return 0;
176
177   /* The plugin API expects that the file descriptor won't be closed
178      and reused as done by the bfd file cache.  So open it again.
179      dup isn't good enough.  plugin IO uses lseek/read while BFD uses
180      fseek/fread.  It isn't wise to mix the unistd and stdio calls on
181      the same underlying file descriptor.  */
182   file->fd = open (file->name, O_RDONLY | O_BINARY);
183   if (file->fd < 0)
184     return 0;
185
186   if (iobfd == ibfd)
187     {
188       struct stat stat_buf;
189
190       if (fstat (file->fd, &stat_buf))
191         {
192           close(file->fd);
193           return 0;
194         }
195
196       file->offset = 0;
197       file->filesize = stat_buf.st_size;
198     }
199   else
200     {
201       file->offset = ibfd->origin;
202       file->filesize = arelt_size (ibfd);
203     }
204   return 1;
205 }
206
207 static int
208 try_claim (bfd *abfd)
209 {
210   int claimed = 0;
211   struct ld_plugin_input_file file;
212
213   file.handle = abfd;
214   if (!bfd_plugin_open_input (abfd, &file))
215     return 0;
216   if (claim_file)
217     claim_file (&file, &claimed);
218   close (file.fd);
219   return claimed;
220 }
221
222 struct plugin_list_entry
223 {
224   void *                        handle;
225   ld_plugin_claim_file_handler  claim_file;
226   struct plugin_list_entry *    next;
227 };
228
229 static struct plugin_list_entry * plugin_list = NULL;
230
231 static int
232 try_load_plugin (const char *pname, bfd *abfd, int *has_plugin_p)
233 {
234   void *plugin_handle = NULL;
235   struct ld_plugin_tv tv[4];
236   int i;
237   ld_plugin_onload onload;
238   enum ld_plugin_status status;
239   struct plugin_list_entry *plugin_list_iter;
240
241   *has_plugin_p = 0;
242
243   plugin_handle = dlopen (pname, RTLD_NOW);
244   if (!plugin_handle)
245     {
246       _bfd_error_handler ("%s\n", dlerror ());
247       return 0;
248     }
249
250   for (plugin_list_iter = plugin_list;
251        plugin_list_iter;
252        plugin_list_iter = plugin_list_iter->next)
253     {
254       if (plugin_handle == plugin_list_iter->handle)
255         {
256           dlclose (plugin_handle);
257           if (!plugin_list_iter->claim_file)
258             return 0;
259
260           register_claim_file (plugin_list_iter->claim_file);
261           goto have_claim_file;
262         }
263     }
264
265   plugin_list_iter = (struct plugin_list_entry *) xmalloc (sizeof *plugin_list_iter);
266   plugin_list_iter->handle = plugin_handle;
267   plugin_list_iter->claim_file = NULL;
268   plugin_list_iter->next = plugin_list;
269   plugin_list = plugin_list_iter;
270
271   onload = dlsym (plugin_handle, "onload");
272   if (!onload)
273     return 0;
274
275   i = 0;
276   tv[i].tv_tag = LDPT_MESSAGE;
277   tv[i].tv_u.tv_message = message;
278
279   ++i;
280   tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
281   tv[i].tv_u.tv_register_claim_file = register_claim_file;
282
283   ++i;
284   tv[i].tv_tag = LDPT_ADD_SYMBOLS;
285   tv[i].tv_u.tv_add_symbols = add_symbols;
286
287   ++i;
288   tv[i].tv_tag = LDPT_NULL;
289   tv[i].tv_u.tv_val = 0;
290
291   status = (*onload)(tv);
292
293   if (status != LDPS_OK)
294     return 0;
295
296   plugin_list_iter->claim_file = claim_file;
297
298 have_claim_file:
299   *has_plugin_p = 1;
300
301   abfd->plugin_format = bfd_plugin_no;
302
303   if (!claim_file)
304     return 0;
305
306   if (!try_claim (abfd))
307     return 0;
308
309   abfd->plugin_format = bfd_plugin_yes;
310   return 1;
311 }
312
313 /* There may be plugin libraries in lib/bfd-plugins.  */
314
315 static int has_plugin = -1;
316
317 static const bfd_target *(*ld_plugin_object_p) (bfd *);
318
319 static const char *plugin_name;
320
321 void
322 bfd_plugin_set_plugin (const char *p)
323 {
324   plugin_name = p;
325   has_plugin = p != NULL;
326 }
327
328 /* Return TRUE if a plugin library is used.  */
329
330 bfd_boolean
331 bfd_plugin_specified_p (void)
332 {
333   return has_plugin > 0;
334 }
335
336 /* Return TRUE if ABFD can be claimed by linker LTO plugin.  */
337
338 bfd_boolean
339 bfd_link_plugin_object_p (bfd *abfd)
340 {
341   if (ld_plugin_object_p)
342     return ld_plugin_object_p (abfd) != NULL;
343   return FALSE;
344 }
345
346 extern const bfd_target plugin_vec;
347
348 /* Return TRUE if TARGET is a pointer to plugin_vec.  */
349
350 bfd_boolean
351 bfd_plugin_target_p (const bfd_target *target)
352 {
353   return target == &plugin_vec;
354 }
355
356 /* Register OBJECT_P to be used by bfd_plugin_object_p.  */
357
358 void
359 register_ld_plugin_object_p (const bfd_target *(*object_p) (bfd *))
360 {
361   ld_plugin_object_p = object_p;
362 }
363
364 static int
365 load_plugin (bfd *abfd)
366 {
367   char *plugin_dir;
368   char *p;
369   DIR *d;
370   struct dirent *ent;
371   int found = 0;
372
373   if (!has_plugin)
374     return found;
375
376   if (plugin_name)
377     return try_load_plugin (plugin_name, abfd, &has_plugin);
378
379   if (plugin_program_name == NULL)
380     return found;
381
382   plugin_dir = concat (BINDIR, "/../lib/bfd-plugins", NULL);
383   p = make_relative_prefix (plugin_program_name,
384                             BINDIR,
385                             plugin_dir);
386   free (plugin_dir);
387   plugin_dir = NULL;
388
389   d = opendir (p);
390   if (!d)
391     goto out;
392
393   while ((ent = readdir (d)))
394     {
395       char *full_name;
396       struct stat s;
397       int valid_plugin;
398
399       full_name = concat (p, "/", ent->d_name, NULL);
400       if (stat (full_name, &s) == 0 && S_ISREG (s.st_mode))
401         found = try_load_plugin (full_name, abfd, &valid_plugin);
402       if (has_plugin <= 0)
403         has_plugin = valid_plugin;
404       free (full_name);
405       if (found)
406         break;
407     }
408
409  out:
410   free (p);
411   if (d)
412     closedir (d);
413
414   return found;
415 }
416
417
418 static const bfd_target *
419 bfd_plugin_object_p (bfd *abfd)
420 {
421   if (ld_plugin_object_p)
422     return ld_plugin_object_p (abfd);
423
424   if (abfd->plugin_format == bfd_plugin_unknown && !load_plugin (abfd))
425     return NULL;
426
427   return abfd->plugin_format == bfd_plugin_yes ? abfd->xvec : NULL;
428 }
429
430 /* Copy any private info we understand from the input bfd
431    to the output bfd.  */
432
433 static bfd_boolean
434 bfd_plugin_bfd_copy_private_bfd_data (bfd *ibfd ATTRIBUTE_UNUSED,
435                                       bfd *obfd ATTRIBUTE_UNUSED)
436 {
437   BFD_ASSERT (0);
438   return TRUE;
439 }
440
441 /* Copy any private info we understand from the input section
442    to the output section.  */
443
444 static bfd_boolean
445 bfd_plugin_bfd_copy_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
446                                           asection *isection ATTRIBUTE_UNUSED,
447                                           bfd *obfd ATTRIBUTE_UNUSED,
448                                           asection *osection ATTRIBUTE_UNUSED)
449 {
450   BFD_ASSERT (0);
451   return TRUE;
452 }
453
454 /* Copy any private info we understand from the input symbol
455    to the output symbol.  */
456
457 static bfd_boolean
458 bfd_plugin_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
459                                          asymbol *isymbol ATTRIBUTE_UNUSED,
460                                          bfd *obfd ATTRIBUTE_UNUSED,
461                                          asymbol *osymbol ATTRIBUTE_UNUSED)
462 {
463   BFD_ASSERT (0);
464   return TRUE;
465 }
466
467 static bfd_boolean
468 bfd_plugin_bfd_print_private_bfd_data (bfd *abfd ATTRIBUTE_UNUSED, PTR ptr ATTRIBUTE_UNUSED)
469 {
470   BFD_ASSERT (0);
471   return TRUE;
472 }
473
474 static char *
475 bfd_plugin_core_file_failing_command (bfd *abfd ATTRIBUTE_UNUSED)
476 {
477   BFD_ASSERT (0);
478   return NULL;
479 }
480
481 static int
482 bfd_plugin_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
483 {
484   BFD_ASSERT (0);
485   return 0;
486 }
487
488 static int
489 bfd_plugin_core_file_pid (bfd *abfd ATTRIBUTE_UNUSED)
490 {
491   BFD_ASSERT (0);
492   return 0;
493 }
494
495 static long
496 bfd_plugin_get_symtab_upper_bound (bfd *abfd)
497 {
498   struct plugin_data_struct *plugin_data = abfd->tdata.plugin_data;
499   long nsyms = plugin_data->nsyms;
500
501   BFD_ASSERT (nsyms >= 0);
502
503   return ((nsyms + 1) * sizeof (asymbol *));
504 }
505
506 static flagword
507 convert_flags (const struct ld_plugin_symbol *sym)
508 {
509  switch (sym->def)
510    {
511    case LDPK_DEF:
512    case LDPK_COMMON:
513    case LDPK_UNDEF:
514      return BSF_GLOBAL;
515
516    case LDPK_WEAKUNDEF:
517    case LDPK_WEAKDEF:
518      return BSF_GLOBAL | BSF_WEAK;
519
520    default:
521      BFD_ASSERT (0);
522      return 0;
523    }
524 }
525
526 static long
527 bfd_plugin_canonicalize_symtab (bfd *abfd,
528                                 asymbol **alocation)
529 {
530   struct plugin_data_struct *plugin_data = abfd->tdata.plugin_data;
531   long nsyms = plugin_data->nsyms;
532   const struct ld_plugin_symbol *syms = plugin_data->syms;
533   static asection fake_section;
534   static asection fake_common_section;
535   int i;
536
537   fake_section.name = ".text";
538   fake_common_section.flags = SEC_IS_COMMON;
539
540   for (i = 0; i < nsyms; i++)
541     {
542       asymbol *s = bfd_alloc (abfd, sizeof (asymbol));
543
544       BFD_ASSERT (s);
545       alocation[i] = s;
546
547       s->the_bfd = abfd;
548       s->name = syms[i].name;
549       s->value = 0;
550       s->flags = convert_flags (&syms[i]);
551       switch (syms[i].def)
552         {
553         case LDPK_COMMON:
554           s->section = &fake_common_section;
555           break;
556         case LDPK_UNDEF:
557         case LDPK_WEAKUNDEF:
558           s->section = bfd_und_section_ptr;
559           break;
560         case LDPK_DEF:
561         case LDPK_WEAKDEF:
562           s->section = &fake_section;
563           break;
564         default:
565           BFD_ASSERT (0);
566         }
567
568       s->udata.p = (void *) &syms[i];
569     }
570
571   return nsyms;
572 }
573
574 static void
575 bfd_plugin_print_symbol (bfd *abfd ATTRIBUTE_UNUSED,
576                          PTR afile ATTRIBUTE_UNUSED,
577                          asymbol *symbol ATTRIBUTE_UNUSED,
578                          bfd_print_symbol_type how ATTRIBUTE_UNUSED)
579 {
580   BFD_ASSERT (0);
581 }
582
583 static void
584 bfd_plugin_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
585                             asymbol *symbol,
586                             symbol_info *ret)
587 {
588   bfd_symbol_info (symbol, ret);
589 }
590
591 /* Make an empty symbol. */
592
593 static asymbol *
594 bfd_plugin_make_empty_symbol (bfd *abfd)
595 {
596   asymbol *new_symbol = bfd_zalloc (abfd, sizeof (asymbol));
597   if (new_symbol == NULL)
598     return new_symbol;
599   new_symbol->the_bfd = abfd;
600   return new_symbol;
601 }
602
603 static int
604 bfd_plugin_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
605                            struct bfd_link_info *info ATTRIBUTE_UNUSED)
606 {
607   BFD_ASSERT (0);
608   return 0;
609 }
610
611 const bfd_target plugin_vec =
612 {
613   "plugin",                     /* Name.  */
614   bfd_target_unknown_flavour,
615   BFD_ENDIAN_LITTLE,            /* Target byte order.  */
616   BFD_ENDIAN_LITTLE,            /* Target headers byte order.  */
617   (HAS_RELOC | EXEC_P |         /* Object flags.  */
618    HAS_LINENO | HAS_DEBUG |
619    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
620   (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
621    | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* Section flags.  */
622   0,                            /* symbol_leading_char.  */
623   '/',                          /* ar_pad_char.  */
624   15,                           /* ar_max_namelen.  */
625   255,                          /* match priority.  */
626
627   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
628   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
629   bfd_getl16, bfd_getl_signed_16, bfd_putl16,   /* data */
630   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
631   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
632   bfd_getl16, bfd_getl_signed_16, bfd_putl16,   /* hdrs */
633
634   {                             /* bfd_check_format.  */
635     _bfd_dummy_target,
636     bfd_plugin_object_p,
637     bfd_generic_archive_p,
638     _bfd_dummy_target
639   },
640   {                             /* bfd_set_format.  */
641     _bfd_bool_bfd_false_error,
642     _bfd_bool_bfd_false_error,
643     _bfd_generic_mkarchive,
644     _bfd_bool_bfd_false_error,
645   },
646   {                             /* bfd_write_contents.  */
647     _bfd_bool_bfd_false_error,
648     _bfd_bool_bfd_false_error,
649     _bfd_write_archive_contents,
650     _bfd_bool_bfd_false_error,
651   },
652
653   BFD_JUMP_TABLE_GENERIC (bfd_plugin),
654   BFD_JUMP_TABLE_COPY (bfd_plugin),
655   BFD_JUMP_TABLE_CORE (bfd_plugin),
656 #ifdef USE_64_BIT_ARCHIVE
657   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_64_bit),
658 #else
659   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
660 #endif
661   BFD_JUMP_TABLE_SYMBOLS (bfd_plugin),
662   BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
663   BFD_JUMP_TABLE_WRITE (bfd_plugin),
664   BFD_JUMP_TABLE_LINK (bfd_plugin),
665   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
666
667   NULL,
668
669   NULL                          /* backend_data.  */
670 };
671 #endif /* BFD_SUPPORTS_PLUGIN */