Commit | Line | Data |
---|---|---|
ce3c775b NC |
1 | /* Plugin support for BFD. |
2 | Copyright 2009 | |
3 | Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of BFD, the Binary File Descriptor library. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
20 | MA 02110-1301, USA. */ | |
21 | ||
22 | #include "config.h" | |
23 | #include <assert.h> | |
24 | #include <dlfcn.h> | |
25 | #include <stdarg.h> | |
26 | #include "plugin-api.h" | |
27 | #include "sysdep.h" | |
28 | #include "plugin.h" | |
29 | #include "libbfd.h" | |
30 | #include "libiberty.h" | |
31 | #include <dirent.h> | |
32 | ||
33 | #define bfd_plugin_close_and_cleanup _bfd_generic_close_and_cleanup | |
34 | #define bfd_plugin_bfd_free_cached_info _bfd_generic_bfd_free_cached_info | |
35 | #define bfd_plugin_new_section_hook _bfd_generic_new_section_hook | |
36 | #define bfd_plugin_get_section_contents _bfd_generic_get_section_contents | |
37 | #define bfd_plugin_get_section_contents_in_window _bfd_generic_get_section_contents_in_window | |
38 | #define bfd_plugin_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data | |
39 | #define bfd_plugin_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data | |
40 | #define bfd_plugin_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data | |
41 | #define bfd_plugin_bfd_set_private_flags _bfd_generic_bfd_set_private_flags | |
42 | #define bfd_plugin_core_file_matches_executable_p generic_core_file_matches_executable_p | |
43 | #define bfd_plugin_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name | |
44 | #define bfd_plugin_bfd_is_target_special_symbol ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false) | |
45 | #define bfd_plugin_get_lineno _bfd_nosymbols_get_lineno | |
46 | #define bfd_plugin_find_nearest_line _bfd_nosymbols_find_nearest_line | |
47 | #define bfd_plugin_find_inliner_info _bfd_nosymbols_find_inliner_info | |
48 | #define bfd_plugin_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol | |
49 | #define bfd_plugin_read_minisymbols _bfd_generic_read_minisymbols | |
50 | #define bfd_plugin_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol | |
51 | #define bfd_plugin_set_arch_mach bfd_default_set_arch_mach | |
52 | #define bfd_plugin_set_section_contents _bfd_generic_set_section_contents | |
53 | #define bfd_plugin_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents | |
54 | #define bfd_plugin_bfd_relax_section bfd_generic_relax_section | |
55 | #define bfd_plugin_bfd_link_hash_table_create _bfd_generic_link_hash_table_create | |
56 | #define bfd_plugin_bfd_link_hash_table_free _bfd_generic_link_hash_table_free | |
57 | #define bfd_plugin_bfd_link_add_symbols _bfd_generic_link_add_symbols | |
58 | #define bfd_plugin_bfd_link_just_syms _bfd_generic_link_just_syms | |
59 | #define bfd_plugin_bfd_final_link _bfd_generic_final_link | |
60 | #define bfd_plugin_bfd_link_split_section _bfd_generic_link_split_section | |
61 | #define bfd_plugin_bfd_gc_sections bfd_generic_gc_sections | |
62 | #define bfd_plugin_bfd_merge_sections bfd_generic_merge_sections | |
63 | #define bfd_plugin_bfd_is_group_section bfd_generic_is_group_section | |
64 | #define bfd_plugin_bfd_discard_group bfd_generic_discard_group | |
65 | #define bfd_plugin_section_already_linked _bfd_generic_section_already_linked | |
66 | #define bfd_plugin_bfd_define_common_symbol bfd_generic_define_common_symbol | |
67 | ||
68 | static enum ld_plugin_status | |
69 | message (int level ATTRIBUTE_UNUSED, | |
70 | const char * format, ...) | |
71 | { | |
72 | va_list args; | |
73 | va_start (args, format); | |
74 | printf ("bfd plugin: "); | |
75 | vprintf (format, args); | |
76 | va_end (args); | |
77 | return LDPS_OK; | |
78 | } | |
79 | ||
80 | /* Register a claim-file handler. */ | |
81 | static ld_plugin_claim_file_handler claim_file; | |
82 | ||
83 | static enum ld_plugin_status | |
84 | register_claim_file (ld_plugin_claim_file_handler handler) | |
85 | { | |
86 | claim_file = handler; | |
87 | return LDPS_OK; | |
88 | } | |
89 | ||
90 | static enum ld_plugin_status | |
91 | add_symbols (void * handle, | |
92 | int nsyms, | |
93 | const struct ld_plugin_symbol * syms) | |
94 | { | |
95 | bfd *abfd = handle; | |
96 | struct plugin_data_struct *plugin_data = | |
97 | bfd_alloc (abfd, sizeof (plugin_data_struct));; | |
98 | ||
99 | plugin_data->nsyms = nsyms; | |
100 | plugin_data->syms = syms; | |
101 | ||
102 | if (nsyms != 0) | |
103 | abfd->flags |= HAS_SYMS; | |
104 | ||
105 | abfd->tdata.plugin_data = plugin_data; | |
106 | return LDPS_OK; | |
107 | } | |
108 | ||
109 | extern char *program_name __attribute__ ((weak)); | |
110 | ||
111 | static int | |
112 | try_load_plugin (const char *pname) | |
113 | { | |
114 | static void *plugin_handle; | |
115 | int tv_size = 4; | |
116 | struct ld_plugin_tv tv[tv_size]; | |
117 | int i; | |
118 | ld_plugin_onload onload; | |
119 | enum ld_plugin_status status; | |
120 | ||
121 | plugin_handle = dlopen (pname, RTLD_NOW); | |
122 | if (!plugin_handle) | |
123 | { | |
124 | (*_bfd_error_handler)("%s\n", dlerror ()); | |
125 | return 0; | |
126 | } | |
127 | ||
128 | onload = dlsym (plugin_handle, "onload"); | |
129 | if (!onload) | |
130 | goto err; | |
131 | ||
132 | i = 0; | |
133 | tv[i].tv_tag = LDPT_MESSAGE; | |
134 | tv[i].tv_u.tv_message = message; | |
135 | ||
136 | ++i; | |
137 | tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK; | |
138 | tv[i].tv_u.tv_register_claim_file = register_claim_file; | |
139 | ||
140 | ++i; | |
141 | tv[i].tv_tag = LDPT_ADD_SYMBOLS; | |
142 | tv[i].tv_u.tv_add_symbols = add_symbols; | |
143 | ||
144 | ++i; | |
145 | tv[i].tv_tag = LDPT_NULL; | |
146 | tv[i].tv_u.tv_val = 0; | |
147 | ||
148 | status = (*onload)(tv); | |
149 | ||
150 | if (status != LDPS_OK) | |
151 | goto err; | |
152 | ||
153 | if (!claim_file) | |
154 | goto err; | |
155 | ||
156 | return 1; | |
157 | ||
158 | err: | |
159 | plugin_handle = NULL; | |
160 | return 0; | |
161 | } | |
162 | ||
163 | static const char *plugin_name; | |
164 | ||
165 | void | |
166 | bfd_plugin_set_plugin (const char *p) | |
167 | { | |
168 | plugin_name = p; | |
169 | } | |
170 | ||
171 | static int | |
172 | load_plugin (void) | |
173 | { | |
174 | char *plugin_dir; | |
175 | char *p; | |
176 | DIR *d; | |
177 | struct dirent *ent; | |
178 | int found = 0; | |
179 | ||
180 | if (plugin_name) | |
181 | return try_load_plugin (plugin_name); | |
182 | ||
183 | if (!program_name) | |
184 | return 0; | |
185 | ||
186 | plugin_dir = concat (BINDIR, "/../lib/bfd-plugins", NULL); | |
187 | p = make_relative_prefix (program_name, | |
188 | BINDIR, | |
189 | plugin_dir); | |
190 | free (plugin_dir); | |
191 | plugin_dir = NULL; | |
192 | ||
193 | d = opendir (p); | |
194 | if (!d) | |
195 | goto out; | |
196 | ||
197 | while ((ent = readdir (d))) | |
198 | { | |
199 | char *full_name; | |
200 | if (ent->d_type != DT_REG && ent->d_type != DT_LNK) | |
201 | continue; | |
202 | ||
203 | full_name = concat (p, "/", ent->d_name, NULL); | |
204 | found = try_load_plugin (full_name); | |
205 | free (full_name); | |
206 | if (found) | |
207 | break; | |
208 | } | |
209 | ||
210 | out: | |
211 | free (p); | |
212 | if (d) | |
213 | closedir (d); | |
214 | ||
215 | return found; | |
216 | } | |
217 | ||
218 | ||
219 | static const bfd_target * | |
220 | bfd_plugin_object_p (bfd *abfd) | |
221 | { | |
222 | int claimed = 0; | |
223 | int t = load_plugin (); | |
224 | struct ld_plugin_input_file file; | |
225 | if (!t) | |
226 | return NULL; | |
227 | ||
228 | file.name = abfd->filename; | |
229 | ||
230 | if (abfd->iostream) | |
231 | { | |
232 | file.fd = fileno (abfd->iostream); | |
233 | file.offset = 0; | |
234 | file.filesize = 0; /*FIXME*/ | |
235 | } | |
236 | else | |
237 | { | |
238 | bfd *archive = abfd->my_archive; | |
239 | BFD_ASSERT (archive); | |
240 | file.fd = fileno (archive->iostream); | |
241 | file.offset = abfd->origin; | |
242 | file.filesize = arelt_size (abfd); | |
243 | ||
244 | } | |
245 | file.handle = abfd; | |
246 | claim_file (&file, &claimed); | |
247 | if (!claimed) | |
248 | return NULL; | |
249 | ||
250 | return abfd->xvec; | |
251 | } | |
252 | ||
253 | /* Copy any private info we understand from the input bfd | |
254 | to the output bfd. */ | |
255 | ||
256 | static bfd_boolean | |
257 | bfd_plugin_bfd_copy_private_bfd_data (bfd *ibfd ATTRIBUTE_UNUSED, | |
258 | bfd *obfd ATTRIBUTE_UNUSED) | |
259 | { | |
260 | BFD_ASSERT (0); | |
261 | return TRUE; | |
262 | } | |
263 | ||
264 | /* Copy any private info we understand from the input section | |
265 | to the output section. */ | |
266 | ||
267 | static bfd_boolean | |
268 | bfd_plugin_bfd_copy_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED, | |
269 | asection *isection ATTRIBUTE_UNUSED, | |
270 | bfd *obfd ATTRIBUTE_UNUSED, | |
271 | asection *osection ATTRIBUTE_UNUSED) | |
272 | { | |
273 | BFD_ASSERT (0); | |
274 | return TRUE; | |
275 | } | |
276 | ||
277 | /* Copy any private info we understand from the input symbol | |
278 | to the output symbol. */ | |
279 | ||
280 | static bfd_boolean | |
281 | bfd_plugin_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED, | |
282 | asymbol *isymbol ATTRIBUTE_UNUSED, | |
283 | bfd *obfd ATTRIBUTE_UNUSED, | |
284 | asymbol *osymbol ATTRIBUTE_UNUSED) | |
285 | { | |
286 | BFD_ASSERT (0); | |
287 | return TRUE; | |
288 | } | |
289 | ||
290 | static bfd_boolean | |
291 | bfd_plugin_bfd_print_private_bfd_data (bfd *abfd ATTRIBUTE_UNUSED, PTR ptr ATTRIBUTE_UNUSED) | |
292 | { | |
293 | BFD_ASSERT (0); | |
294 | return TRUE; | |
295 | } | |
296 | ||
297 | static char * | |
298 | bfd_plugin_core_file_failing_command (bfd *abfd ATTRIBUTE_UNUSED) | |
299 | { | |
300 | BFD_ASSERT (0); | |
301 | return NULL; | |
302 | } | |
303 | ||
304 | static int | |
305 | bfd_plugin_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED) | |
306 | { | |
307 | BFD_ASSERT (0); | |
308 | return 0; | |
309 | } | |
310 | ||
311 | static long | |
312 | bfd_plugin_get_symtab_upper_bound (bfd *abfd) | |
313 | { | |
314 | struct plugin_data_struct *plugin_data = abfd->tdata.plugin_data; | |
315 | long nsyms = plugin_data->nsyms; | |
316 | ||
317 | BFD_ASSERT (nsyms >= 0); | |
318 | ||
319 | return ((nsyms + 1) * sizeof (asymbol *)); | |
320 | } | |
321 | ||
322 | static flagword | |
323 | convert_flags (const struct ld_plugin_symbol *sym) | |
324 | { | |
325 | switch (sym->def) | |
326 | { | |
327 | case LDPK_DEF: | |
328 | case LDPK_COMMON: | |
329 | case LDPK_UNDEF: | |
330 | return BSF_GLOBAL; | |
331 | ||
332 | case LDPK_WEAKUNDEF: | |
333 | case LDPK_WEAKDEF: | |
334 | return BSF_GLOBAL | BSF_WEAK; | |
335 | ||
336 | default: | |
337 | BFD_ASSERT (0); | |
338 | return 0; | |
339 | } | |
340 | } | |
341 | ||
342 | static long | |
343 | bfd_plugin_canonicalize_symtab (bfd *abfd, | |
344 | asymbol **alocation) | |
345 | { | |
346 | struct plugin_data_struct *plugin_data = abfd->tdata.plugin_data; | |
347 | long nsyms = plugin_data->nsyms; | |
348 | const struct ld_plugin_symbol *syms = plugin_data->syms; | |
349 | static asection fake_section; | |
350 | static asection fake_common_section; | |
351 | int i; | |
352 | ||
353 | fake_section.name = ".text"; | |
354 | fake_common_section.flags = SEC_IS_COMMON; | |
355 | ||
356 | for (i = 0; i < nsyms; i++) | |
357 | { | |
358 | asymbol *s = bfd_alloc (abfd, sizeof (asymbol)); | |
359 | ||
360 | BFD_ASSERT (s); | |
361 | alocation[i] = s; | |
362 | ||
363 | s->the_bfd = abfd; | |
364 | s->name = syms[i].name; | |
365 | s->value = 0; | |
366 | s->flags = convert_flags (&syms[i]); | |
367 | switch (syms[i].def) | |
368 | { | |
369 | case LDPK_COMMON: | |
370 | s->section = &fake_common_section; | |
371 | break; | |
372 | case LDPK_UNDEF: | |
373 | case LDPK_WEAKUNDEF: | |
374 | s->section = bfd_und_section_ptr; | |
375 | break; | |
376 | case LDPK_DEF: | |
377 | case LDPK_WEAKDEF: | |
378 | s->section = &fake_section; | |
379 | break; | |
380 | default: | |
381 | BFD_ASSERT (0); | |
382 | } | |
383 | ||
384 | s->udata.p = (void *) &syms[i]; | |
385 | } | |
386 | ||
387 | return nsyms; | |
388 | } | |
389 | ||
390 | static void | |
391 | bfd_plugin_print_symbol (bfd *abfd ATTRIBUTE_UNUSED, | |
392 | PTR afile ATTRIBUTE_UNUSED, | |
393 | asymbol *symbol ATTRIBUTE_UNUSED, | |
394 | bfd_print_symbol_type how ATTRIBUTE_UNUSED) | |
395 | { | |
396 | BFD_ASSERT (0); | |
397 | } | |
398 | ||
399 | static void | |
400 | bfd_plugin_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, | |
401 | asymbol *symbol, | |
402 | symbol_info *ret) | |
403 | { | |
404 | bfd_symbol_info (symbol, ret); | |
405 | } | |
406 | ||
407 | /* Make an empty symbol. */ | |
408 | ||
409 | static asymbol * | |
410 | bfd_plugin_make_empty_symbol (bfd *abfd) | |
411 | { | |
412 | asymbol *new = bfd_zalloc (abfd, sizeof (asymbol)); | |
413 | if (new == NULL) | |
414 | return new; | |
415 | new->the_bfd = abfd; | |
416 | return new; | |
417 | } | |
418 | ||
419 | static int | |
420 | bfd_plugin_sizeof_headers (bfd *a ATTRIBUTE_UNUSED, | |
421 | struct bfd_link_info *info ATTRIBUTE_UNUSED) | |
422 | { | |
423 | BFD_ASSERT (0); | |
424 | return 0; | |
425 | } | |
426 | ||
427 | static bfd_boolean | |
428 | bfd_plugin_mkobject (bfd *abfd ATTRIBUTE_UNUSED) | |
429 | { | |
430 | BFD_ASSERT (0); | |
431 | return 0; | |
432 | } | |
433 | ||
434 | const bfd_target plugin_vec = | |
435 | { | |
436 | "plugin", /* Name. */ | |
437 | bfd_target_unknown_flavour, | |
438 | BFD_ENDIAN_LITTLE, /* Target byte order. */ | |
439 | BFD_ENDIAN_LITTLE, /* Target headers byte order. */ | |
440 | (HAS_RELOC | EXEC_P | /* Object flags. */ | |
441 | HAS_LINENO | HAS_DEBUG | | |
442 | HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED), | |
443 | (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS | |
444 | | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* Section flags. */ | |
445 | 0, /* symbol_leading_char. */ | |
446 | '/', /* ar_pad_char. */ | |
447 | 15, /* ar_max_namelen. */ | |
448 | ||
449 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, | |
450 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, | |
451 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */ | |
452 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, | |
453 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, | |
454 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */ | |
455 | ||
456 | { /* bfd_check_format. */ | |
457 | _bfd_dummy_target, | |
458 | bfd_plugin_object_p, | |
459 | bfd_generic_archive_p, | |
460 | _bfd_dummy_target | |
461 | }, | |
462 | { /* bfd_set_format. */ | |
463 | bfd_false, | |
464 | bfd_plugin_mkobject, | |
465 | _bfd_generic_mkarchive, | |
466 | bfd_false, | |
467 | }, | |
468 | { /* bfd_write_contents. */ | |
469 | bfd_false, | |
470 | bfd_false, | |
471 | _bfd_write_archive_contents, | |
472 | bfd_false, | |
473 | }, | |
474 | ||
475 | BFD_JUMP_TABLE_GENERIC (bfd_plugin), | |
476 | BFD_JUMP_TABLE_COPY (bfd_plugin), | |
477 | BFD_JUMP_TABLE_CORE (bfd_plugin), | |
478 | BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff), | |
479 | BFD_JUMP_TABLE_SYMBOLS (bfd_plugin), | |
480 | BFD_JUMP_TABLE_RELOCS (_bfd_norelocs), | |
481 | BFD_JUMP_TABLE_WRITE (bfd_plugin), | |
482 | BFD_JUMP_TABLE_LINK (bfd_plugin), | |
483 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), | |
484 | ||
485 | NULL, | |
486 | ||
487 | NULL /* backend_data. */ | |
488 | }; |