]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/plugin.c
Update copyright years
[thirdparty/binutils-gdb.git] / bfd / plugin.c
CommitLineData
ce3c775b 1/* Plugin support for BFD.
4b95cf5c 2 Copyright (C) 2009-2014 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
ce3c775b
NC
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_boolean (*) (bfd *, asymbol *)) bfd_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_inliner_info _bfd_nosymbols_find_inliner_info
87#define bfd_plugin_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
88#define bfd_plugin_read_minisymbols _bfd_generic_read_minisymbols
89#define bfd_plugin_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
90#define bfd_plugin_set_arch_mach bfd_default_set_arch_mach
91#define bfd_plugin_set_section_contents _bfd_generic_set_section_contents
92#define bfd_plugin_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
93#define bfd_plugin_bfd_relax_section bfd_generic_relax_section
94#define bfd_plugin_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
95#define bfd_plugin_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
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
ae17ab41 101#define bfd_plugin_bfd_lookup_section_flags bfd_generic_lookup_section_flags
ce3c775b
NC
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_discard_group bfd_generic_discard_group
105#define bfd_plugin_section_already_linked _bfd_generic_section_already_linked
106#define bfd_plugin_bfd_define_common_symbol bfd_generic_define_common_symbol
69a630b5 107#define bfd_plugin_bfd_copy_link_hash_symbol_type _bfd_generic_copy_link_hash_symbol_type
ce3c775b
NC
108
109static enum ld_plugin_status
110message (int level ATTRIBUTE_UNUSED,
111 const char * format, ...)
112{
113 va_list args;
114 va_start (args, format);
115 printf ("bfd plugin: ");
116 vprintf (format, args);
d9b21463 117 putchar ('\n');
ce3c775b
NC
118 va_end (args);
119 return LDPS_OK;
120}
121
122/* Register a claim-file handler. */
123static ld_plugin_claim_file_handler claim_file;
124
125static enum ld_plugin_status
126register_claim_file (ld_plugin_claim_file_handler handler)
127{
128 claim_file = handler;
129 return LDPS_OK;
130}
131
132static enum ld_plugin_status
133add_symbols (void * handle,
134 int nsyms,
135 const struct ld_plugin_symbol * syms)
136{
137 bfd *abfd = handle;
138 struct plugin_data_struct *plugin_data =
5bb3703f 139 bfd_alloc (abfd, sizeof (plugin_data_struct));
ce3c775b
NC
140
141 plugin_data->nsyms = nsyms;
142 plugin_data->syms = syms;
143
144 if (nsyms != 0)
145 abfd->flags |= HAS_SYMS;
146
147 abfd->tdata.plugin_data = plugin_data;
148 return LDPS_OK;
149}
150
fc579192
NC
151static const char *plugin_program_name;
152
153void
154bfd_plugin_set_program_name (const char *program_name)
155{
156 plugin_program_name = program_name;
157}
ce3c775b
NC
158
159static int
160try_load_plugin (const char *pname)
161{
162 static void *plugin_handle;
163 int tv_size = 4;
164 struct ld_plugin_tv tv[tv_size];
165 int i;
166 ld_plugin_onload onload;
167 enum ld_plugin_status status;
168
169 plugin_handle = dlopen (pname, RTLD_NOW);
170 if (!plugin_handle)
171 {
172 (*_bfd_error_handler)("%s\n", dlerror ());
173 return 0;
174 }
175
176 onload = dlsym (plugin_handle, "onload");
177 if (!onload)
178 goto err;
179
180 i = 0;
181 tv[i].tv_tag = LDPT_MESSAGE;
182 tv[i].tv_u.tv_message = message;
183
184 ++i;
185 tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
186 tv[i].tv_u.tv_register_claim_file = register_claim_file;
187
188 ++i;
189 tv[i].tv_tag = LDPT_ADD_SYMBOLS;
190 tv[i].tv_u.tv_add_symbols = add_symbols;
191
192 ++i;
193 tv[i].tv_tag = LDPT_NULL;
194 tv[i].tv_u.tv_val = 0;
195
196 status = (*onload)(tv);
197
198 if (status != LDPS_OK)
199 goto err;
200
201 if (!claim_file)
202 goto err;
203
204 return 1;
205
206 err:
207 plugin_handle = NULL;
208 return 0;
209}
210
211static const char *plugin_name;
212
213void
214bfd_plugin_set_plugin (const char *p)
215{
216 plugin_name = p;
217}
218
219static int
220load_plugin (void)
221{
222 char *plugin_dir;
223 char *p;
224 DIR *d;
225 struct dirent *ent;
226 int found = 0;
227
228 if (plugin_name)
229 return try_load_plugin (plugin_name);
230
fc579192 231 if (plugin_program_name == NULL)
ce3c775b
NC
232 return 0;
233
234 plugin_dir = concat (BINDIR, "/../lib/bfd-plugins", NULL);
fc579192 235 p = make_relative_prefix (plugin_program_name,
ce3c775b
NC
236 BINDIR,
237 plugin_dir);
238 free (plugin_dir);
239 plugin_dir = NULL;
240
241 d = opendir (p);
242 if (!d)
243 goto out;
244
245 while ((ent = readdir (d)))
246 {
247 char *full_name;
c41ba836 248 struct stat s;
ce3c775b
NC
249
250 full_name = concat (p, "/", ent->d_name, NULL);
c41ba836
DK
251 if (stat(full_name, &s) == 0 && S_ISREG (s.st_mode))
252 found = try_load_plugin (full_name);
ce3c775b
NC
253 free (full_name);
254 if (found)
255 break;
256 }
257
258 out:
259 free (p);
260 if (d)
261 closedir (d);
262
263 return found;
264}
265
266
267static const bfd_target *
268bfd_plugin_object_p (bfd *abfd)
269{
270 int claimed = 0;
ce3c775b 271 struct ld_plugin_input_file file;
028ed2b2 272 bfd *iobfd;
80549d04
RÁE
273 static int have_loaded = 0;
274 static int have_plugin = 0;
028ed2b2 275
80549d04
RÁE
276 if (!have_loaded)
277 {
278 have_loaded = 1;
279 have_plugin = load_plugin ();
280 }
281 if (!have_plugin)
ce3c775b
NC
282 return NULL;
283
284 file.name = abfd->filename;
285
028ed2b2 286 if (abfd->my_archive)
ce3c775b 287 {
028ed2b2
L
288 iobfd = abfd->my_archive;
289 file.offset = abfd->origin;
290 file.filesize = arelt_size (abfd);
ce3c775b
NC
291 }
292 else
293 {
028ed2b2
L
294 iobfd = abfd;
295 file.offset = 0;
80549d04 296 file.filesize = 0;
ce3c775b 297 }
028ed2b2
L
298
299 if (!iobfd->iostream && !bfd_open_file (iobfd))
300 return NULL;
301
302 file.fd = fileno ((FILE *) iobfd->iostream);
303
80549d04
RÁE
304 if (!abfd->my_archive)
305 {
306 struct stat stat_buf;
307 if (fstat (file.fd, &stat_buf))
308 return NULL;
309 file.filesize = stat_buf.st_size;
310 }
311
ce3c775b 312 file.handle = abfd;
80549d04 313 off_t cur_offset = lseek(file.fd, 0, SEEK_CUR);
ce3c775b 314 claim_file (&file, &claimed);
80549d04 315 lseek(file.fd, cur_offset, SEEK_SET);
ce3c775b
NC
316 if (!claimed)
317 return NULL;
318
319 return abfd->xvec;
320}
321
322/* Copy any private info we understand from the input bfd
323 to the output bfd. */
324
325static bfd_boolean
326bfd_plugin_bfd_copy_private_bfd_data (bfd *ibfd ATTRIBUTE_UNUSED,
327 bfd *obfd ATTRIBUTE_UNUSED)
328{
329 BFD_ASSERT (0);
330 return TRUE;
331}
332
333/* Copy any private info we understand from the input section
334 to the output section. */
335
336static bfd_boolean
337bfd_plugin_bfd_copy_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
338 asection *isection ATTRIBUTE_UNUSED,
339 bfd *obfd ATTRIBUTE_UNUSED,
340 asection *osection ATTRIBUTE_UNUSED)
341{
342 BFD_ASSERT (0);
343 return TRUE;
344}
345
346/* Copy any private info we understand from the input symbol
347 to the output symbol. */
348
349static bfd_boolean
350bfd_plugin_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
351 asymbol *isymbol ATTRIBUTE_UNUSED,
352 bfd *obfd ATTRIBUTE_UNUSED,
353 asymbol *osymbol ATTRIBUTE_UNUSED)
354{
355 BFD_ASSERT (0);
356 return TRUE;
357}
358
359static bfd_boolean
360bfd_plugin_bfd_print_private_bfd_data (bfd *abfd ATTRIBUTE_UNUSED, PTR ptr ATTRIBUTE_UNUSED)
361{
362 BFD_ASSERT (0);
363 return TRUE;
364}
365
366static char *
367bfd_plugin_core_file_failing_command (bfd *abfd ATTRIBUTE_UNUSED)
368{
369 BFD_ASSERT (0);
370 return NULL;
371}
372
373static int
374bfd_plugin_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
261b8d08
PA
375{
376 BFD_ASSERT (0);
377 return 0;
378}
379
380static int
381bfd_plugin_core_file_pid (bfd *abfd ATTRIBUTE_UNUSED)
ce3c775b
NC
382{
383 BFD_ASSERT (0);
384 return 0;
385}
386
387static long
388bfd_plugin_get_symtab_upper_bound (bfd *abfd)
389{
390 struct plugin_data_struct *plugin_data = abfd->tdata.plugin_data;
391 long nsyms = plugin_data->nsyms;
392
393 BFD_ASSERT (nsyms >= 0);
394
395 return ((nsyms + 1) * sizeof (asymbol *));
396}
397
398static flagword
399convert_flags (const struct ld_plugin_symbol *sym)
400{
401 switch (sym->def)
68ffbac6 402 {
ce3c775b
NC
403 case LDPK_DEF:
404 case LDPK_COMMON:
405 case LDPK_UNDEF:
406 return BSF_GLOBAL;
407
408 case LDPK_WEAKUNDEF:
409 case LDPK_WEAKDEF:
410 return BSF_GLOBAL | BSF_WEAK;
411
412 default:
413 BFD_ASSERT (0);
414 return 0;
415 }
416}
417
418static long
419bfd_plugin_canonicalize_symtab (bfd *abfd,
420 asymbol **alocation)
421{
422 struct plugin_data_struct *plugin_data = abfd->tdata.plugin_data;
423 long nsyms = plugin_data->nsyms;
424 const struct ld_plugin_symbol *syms = plugin_data->syms;
425 static asection fake_section;
426 static asection fake_common_section;
427 int i;
428
429 fake_section.name = ".text";
430 fake_common_section.flags = SEC_IS_COMMON;
431
432 for (i = 0; i < nsyms; i++)
433 {
68ffbac6 434 asymbol *s = bfd_alloc (abfd, sizeof (asymbol));
ce3c775b
NC
435
436 BFD_ASSERT (s);
437 alocation[i] = s;
438
439 s->the_bfd = abfd;
440 s->name = syms[i].name;
441 s->value = 0;
442 s->flags = convert_flags (&syms[i]);
443 switch (syms[i].def)
444 {
445 case LDPK_COMMON:
446 s->section = &fake_common_section;
447 break;
448 case LDPK_UNDEF:
449 case LDPK_WEAKUNDEF:
450 s->section = bfd_und_section_ptr;
451 break;
452 case LDPK_DEF:
453 case LDPK_WEAKDEF:
454 s->section = &fake_section;
455 break;
456 default:
457 BFD_ASSERT (0);
458 }
459
460 s->udata.p = (void *) &syms[i];
461 }
462
463 return nsyms;
464}
465
466static void
467bfd_plugin_print_symbol (bfd *abfd ATTRIBUTE_UNUSED,
468 PTR afile ATTRIBUTE_UNUSED,
469 asymbol *symbol ATTRIBUTE_UNUSED,
470 bfd_print_symbol_type how ATTRIBUTE_UNUSED)
471{
472 BFD_ASSERT (0);
473}
474
475static void
476bfd_plugin_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
477 asymbol *symbol,
478 symbol_info *ret)
479{
480 bfd_symbol_info (symbol, ret);
481}
482
483/* Make an empty symbol. */
484
485static asymbol *
486bfd_plugin_make_empty_symbol (bfd *abfd)
487{
d3ce72d0
NC
488 asymbol *new_symbol = bfd_zalloc (abfd, sizeof (asymbol));
489 if (new_symbol == NULL)
490 return new_symbol;
491 new_symbol->the_bfd = abfd;
492 return new_symbol;
ce3c775b
NC
493}
494
495static int
496bfd_plugin_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
497 struct bfd_link_info *info ATTRIBUTE_UNUSED)
498{
499 BFD_ASSERT (0);
500 return 0;
501}
502
ce3c775b
NC
503const bfd_target plugin_vec =
504{
505 "plugin", /* Name. */
506 bfd_target_unknown_flavour,
507 BFD_ENDIAN_LITTLE, /* Target byte order. */
508 BFD_ENDIAN_LITTLE, /* Target headers byte order. */
509 (HAS_RELOC | EXEC_P | /* Object flags. */
510 HAS_LINENO | HAS_DEBUG |
511 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
512 (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
513 | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* Section flags. */
514 0, /* symbol_leading_char. */
515 '/', /* ar_pad_char. */
516 15, /* ar_max_namelen. */
0aabe54e 517 0, /* match priority. */
ce3c775b
NC
518
519 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
520 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
521 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
522 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
523 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
524 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
525
526 { /* bfd_check_format. */
527 _bfd_dummy_target,
528 bfd_plugin_object_p,
529 bfd_generic_archive_p,
530 _bfd_dummy_target
531 },
532 { /* bfd_set_format. */
533 bfd_false,
4846b70a 534 bfd_false,
ce3c775b
NC
535 _bfd_generic_mkarchive,
536 bfd_false,
537 },
538 { /* bfd_write_contents. */
539 bfd_false,
540 bfd_false,
541 _bfd_write_archive_contents,
542 bfd_false,
543 },
544
545 BFD_JUMP_TABLE_GENERIC (bfd_plugin),
546 BFD_JUMP_TABLE_COPY (bfd_plugin),
547 BFD_JUMP_TABLE_CORE (bfd_plugin),
548 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
549 BFD_JUMP_TABLE_SYMBOLS (bfd_plugin),
550 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
551 BFD_JUMP_TABLE_WRITE (bfd_plugin),
552 BFD_JUMP_TABLE_LINK (bfd_plugin),
553 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
554
555 NULL,
556
557 NULL /* backend_data. */
558};
98950613 559#endif /* BFD_SUPPORTS_PLUGIN */