Commit | Line | Data |
---|---|---|
5d3236ee | 1 | /* Plugin control for the GNU linker. |
e922bcab | 2 | Copyright 2010, 2011 Free Software Foundation, Inc. |
5d3236ee DK |
3 | |
4 | This file is part of the GNU Binutils. | |
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 "libiberty.h" | |
23 | #include "bfd.h" | |
24 | #include "bfdlink.h" | |
25 | #include "bfdver.h" | |
26 | #include "ld.h" | |
27 | #include "ldmain.h" | |
28 | #include "ldmisc.h" | |
29 | #include "ldexp.h" | |
30 | #include "ldlang.h" | |
31 | #include "ldfile.h" | |
32 | #include "plugin.h" | |
33 | #include "plugin-api.h" | |
34 | #include "elf-bfd.h" | |
3917d5d5 | 35 | #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) |
f31d24a0 | 36 | #include <windows.h> |
3917d5d5 | 37 | #endif |
5d3236ee | 38 | |
1715a13c L |
39 | /* Report plugin symbols. */ |
40 | bfd_boolean report_plugin_symbols; | |
41 | ||
5d3236ee DK |
42 | /* The suffix to append to the name of the real (claimed) object file |
43 | when generating a dummy BFD to hold the IR symbols sent from the | |
cf4dc96f DK |
44 | plugin. For cosmetic use only; appears in maps, crefs etc. */ |
45 | #define IRONLY_SUFFIX " (symbol from plugin)" | |
5d3236ee DK |
46 | |
47 | /* Stores a single argument passed to a plugin. */ | |
48 | typedef struct plugin_arg | |
49 | { | |
50 | struct plugin_arg *next; | |
51 | const char *arg; | |
52 | } plugin_arg_t; | |
53 | ||
54 | /* Holds all details of a single plugin. */ | |
55 | typedef struct plugin | |
56 | { | |
57 | /* Next on the list of plugins, or NULL at end of chain. */ | |
58 | struct plugin *next; | |
59 | /* The argument string given to --plugin. */ | |
60 | const char *name; | |
61 | /* The shared library handle returned by dlopen. */ | |
62 | void *dlhandle; | |
63 | /* The list of argument string given to --plugin-opt. */ | |
64 | plugin_arg_t *args; | |
65 | /* Number of args in the list, for convenience. */ | |
66 | size_t n_args; | |
67 | /* The plugin's event handlers. */ | |
68 | ld_plugin_claim_file_handler claim_file_handler; | |
69 | ld_plugin_all_symbols_read_handler all_symbols_read_handler; | |
70 | ld_plugin_cleanup_handler cleanup_handler; | |
71 | /* TRUE if the cleanup handlers have been called. */ | |
72 | bfd_boolean cleanup_done; | |
73 | } plugin_t; | |
74 | ||
75 | /* The master list of all plugins. */ | |
76 | static plugin_t *plugins_list = NULL; | |
77 | ||
78 | /* We keep a tail pointer for easy linking on the end. */ | |
79 | static plugin_t **plugins_tail_chain_ptr = &plugins_list; | |
80 | ||
81 | /* The last plugin added to the list, for receiving args. */ | |
82 | static plugin_t *last_plugin = NULL; | |
83 | ||
84 | /* The tail of the arg chain of the last plugin added to the list. */ | |
85 | static plugin_arg_t **last_plugin_args_tail_chain_ptr = NULL; | |
86 | ||
87 | /* The plugin which is currently having a callback executed. */ | |
88 | static plugin_t *called_plugin = NULL; | |
89 | ||
90 | /* Last plugin to cause an error, if any. */ | |
91 | static const char *error_plugin = NULL; | |
92 | ||
24f58f47 | 93 | /* State of linker "notice" interface before we poked at it. */ |
9e2278f5 | 94 | static bfd_boolean orig_notice_all; |
9e2278f5 AM |
95 | |
96 | /* Original linker callbacks, and the plugin version. */ | |
97 | static const struct bfd_link_callbacks *orig_callbacks; | |
98 | static struct bfd_link_callbacks plugin_callbacks; | |
99 | ||
5d3236ee DK |
100 | /* Set at all symbols read time, to avoid recursively offering the plugin |
101 | its own newly-added input files and libs to claim. */ | |
9e2278f5 | 102 | bfd_boolean no_more_claiming = FALSE; |
5d3236ee DK |
103 | |
104 | /* List of tags to set in the constant leading part of the tv array. */ | |
105 | static const enum ld_plugin_tag tv_header_tags[] = | |
106 | { | |
107 | LDPT_MESSAGE, | |
108 | LDPT_API_VERSION, | |
109 | LDPT_GNU_LD_VERSION, | |
110 | LDPT_LINKER_OUTPUT, | |
111 | LDPT_OUTPUT_NAME, | |
112 | LDPT_REGISTER_CLAIM_FILE_HOOK, | |
113 | LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK, | |
114 | LDPT_REGISTER_CLEANUP_HOOK, | |
115 | LDPT_ADD_SYMBOLS, | |
116 | LDPT_GET_INPUT_FILE, | |
117 | LDPT_RELEASE_INPUT_FILE, | |
118 | LDPT_GET_SYMBOLS, | |
119 | LDPT_ADD_INPUT_FILE, | |
120 | LDPT_ADD_INPUT_LIBRARY, | |
121 | LDPT_SET_EXTRA_LIBRARY_PATH | |
122 | }; | |
123 | ||
124 | /* How many entries in the constant leading part of the tv array. */ | |
125 | static const size_t tv_header_size = ARRAY_SIZE (tv_header_tags); | |
126 | ||
9e2278f5 | 127 | /* Forward references. */ |
16d96b5b AM |
128 | static bfd_boolean plugin_notice (struct bfd_link_info *, |
129 | struct bfd_link_hash_entry *, bfd *, | |
130 | asection *, bfd_vma, flagword, const char *); | |
9e2278f5 | 131 | |
3917d5d5 DK |
132 | #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) |
133 | ||
134 | #define RTLD_NOW 0 /* Dummy value. */ | |
135 | ||
136 | static void * | |
137 | dlopen (const char *file, int mode ATTRIBUTE_UNUSED) | |
138 | { | |
139 | return LoadLibrary (file); | |
140 | } | |
141 | ||
142 | static void * | |
143 | dlsym (void *handle, const char *name) | |
144 | { | |
145 | return GetProcAddress (handle, name); | |
146 | } | |
147 | ||
148 | static int | |
149 | dlclose (void *handle) | |
150 | { | |
151 | FreeLibrary (handle); | |
152 | return 0; | |
153 | } | |
154 | ||
155 | #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */ | |
156 | ||
5d3236ee DK |
157 | /* Helper function for exiting with error status. */ |
158 | static int | |
159 | set_plugin_error (const char *plugin) | |
160 | { | |
161 | error_plugin = plugin; | |
162 | return -1; | |
163 | } | |
164 | ||
165 | /* Test if an error occurred. */ | |
166 | static bfd_boolean | |
167 | plugin_error_p (void) | |
168 | { | |
169 | return error_plugin != NULL; | |
170 | } | |
171 | ||
172 | /* Return name of plugin which caused an error if any. */ | |
d44ad554 DK |
173 | const char * |
174 | plugin_error_plugin (void) | |
5d3236ee DK |
175 | { |
176 | return error_plugin ? error_plugin : _("<no plugin>"); | |
177 | } | |
178 | ||
179 | /* Handle -plugin arg: find and load plugin, or return error. */ | |
d44ad554 DK |
180 | int |
181 | plugin_opt_plugin (const char *plugin) | |
5d3236ee DK |
182 | { |
183 | plugin_t *newplug; | |
184 | ||
185 | newplug = xmalloc (sizeof *newplug); | |
186 | memset (newplug, 0, sizeof *newplug); | |
187 | newplug->name = plugin; | |
188 | newplug->dlhandle = dlopen (plugin, RTLD_NOW); | |
189 | if (!newplug->dlhandle) | |
190 | return set_plugin_error (plugin); | |
191 | ||
192 | /* Chain on end, so when we run list it is in command-line order. */ | |
193 | *plugins_tail_chain_ptr = newplug; | |
194 | plugins_tail_chain_ptr = &newplug->next; | |
195 | ||
196 | /* Record it as current plugin for receiving args. */ | |
197 | last_plugin = newplug; | |
198 | last_plugin_args_tail_chain_ptr = &newplug->args; | |
199 | return 0; | |
200 | } | |
201 | ||
202 | /* Accumulate option arguments for last-loaded plugin, or return | |
203 | error if none. */ | |
d44ad554 DK |
204 | int |
205 | plugin_opt_plugin_arg (const char *arg) | |
5d3236ee DK |
206 | { |
207 | plugin_arg_t *newarg; | |
208 | ||
209 | if (!last_plugin) | |
210 | return set_plugin_error (_("<no plugin>")); | |
211 | ||
212 | newarg = xmalloc (sizeof *newarg); | |
213 | newarg->arg = arg; | |
214 | newarg->next = NULL; | |
215 | ||
216 | /* Chain on end to preserve command-line order. */ | |
217 | *last_plugin_args_tail_chain_ptr = newarg; | |
218 | last_plugin_args_tail_chain_ptr = &newarg->next; | |
219 | last_plugin->n_args++; | |
220 | return 0; | |
221 | } | |
222 | ||
223 | /* Create a dummy BFD. */ | |
224 | bfd * | |
225 | plugin_get_ir_dummy_bfd (const char *name, bfd *srctemplate) | |
226 | { | |
bc110b6e AM |
227 | bfd *abfd; |
228 | ||
229 | bfd_use_reserved_id = 1; | |
9e2278f5 | 230 | abfd = bfd_create (concat (name, IRONLY_SUFFIX, (const char *) NULL), |
bc110b6e | 231 | srctemplate); |
9e2278f5 AM |
232 | if (abfd != NULL) |
233 | { | |
234 | abfd->flags |= BFD_LINKER_CREATED | BFD_PLUGIN; | |
235 | bfd_set_arch_info (abfd, bfd_get_arch_info (srctemplate)); | |
236 | bfd_set_gp_size (abfd, bfd_get_gp_size (srctemplate)); | |
237 | if (bfd_make_writable (abfd) | |
238 | && bfd_copy_private_bfd_data (srctemplate, abfd)) | |
239 | { | |
240 | flagword flags; | |
241 | ||
c77ec726 | 242 | /* Create section to own the symbols. */ |
9e2278f5 AM |
243 | flags = (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY |
244 | | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_EXCLUDE); | |
245 | if (bfd_make_section_anyway_with_flags (abfd, ".text", flags)) | |
246 | return abfd; | |
247 | } | |
248 | } | |
249 | einfo (_("could not create dummy IR bfd: %F%E\n")); | |
250 | return NULL; | |
5d3236ee DK |
251 | } |
252 | ||
d44ad554 DK |
253 | /* Check if the BFD passed in is an IR dummy object file. */ |
254 | static bfd_boolean | |
5d3236ee DK |
255 | is_ir_dummy_bfd (const bfd *abfd) |
256 | { | |
cf4dc96f DK |
257 | /* ABFD can sometimes legitimately be NULL, e.g. when called from one |
258 | of the linker callbacks for a symbol in the *ABS* or *UND* sections. | |
259 | Likewise, the usrdata field may be NULL if ABFD was added by the | |
260 | backend without a corresponding input statement, as happens e.g. | |
261 | when processing DT_NEEDED dependencies. */ | |
9e2278f5 AM |
262 | return (abfd |
263 | && abfd->usrdata | |
264 | && ((lang_input_statement_type *)(abfd->usrdata))->claimed); | |
5d3236ee DK |
265 | } |
266 | ||
267 | /* Helpers to convert between BFD and GOLD symbol formats. */ | |
268 | static enum ld_plugin_status | |
269 | asymbol_from_plugin_symbol (bfd *abfd, asymbol *asym, | |
f84854b6 | 270 | const struct ld_plugin_symbol *ldsym) |
5d3236ee DK |
271 | { |
272 | flagword flags = BSF_NO_FLAGS; | |
273 | struct bfd_section *section; | |
274 | ||
275 | asym->the_bfd = abfd; | |
f84854b6 | 276 | asym->name = (ldsym->version |
9e2278f5 | 277 | ? concat (ldsym->name, "@", ldsym->version, (const char *) NULL) |
f84854b6 | 278 | : ldsym->name); |
5d3236ee DK |
279 | asym->value = 0; |
280 | switch (ldsym->def) | |
281 | { | |
282 | case LDPK_WEAKDEF: | |
283 | flags = BSF_WEAK; | |
284 | /* FALLTHRU */ | |
285 | case LDPK_DEF: | |
286 | flags |= BSF_GLOBAL; | |
c77ec726 AM |
287 | if (ldsym->comdat_key) |
288 | { | |
289 | char *name = concat (".gnu.linkonce.t.", ldsym->comdat_key, | |
290 | (const char *) NULL); | |
291 | section = bfd_get_section_by_name (abfd, name); | |
292 | if (section != NULL) | |
293 | free (name); | |
294 | else | |
295 | { | |
296 | flagword sflags; | |
297 | ||
298 | sflags = (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY | |
299 | | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_EXCLUDE | |
300 | | SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD); | |
301 | section = bfd_make_section_anyway_with_flags (abfd, name, sflags); | |
302 | if (section == NULL) | |
303 | return LDPS_ERR; | |
304 | } | |
305 | } | |
306 | else | |
307 | section = bfd_get_section_by_name (abfd, ".text"); | |
5d3236ee DK |
308 | break; |
309 | ||
310 | case LDPK_WEAKUNDEF: | |
311 | flags = BSF_WEAK; | |
312 | /* FALLTHRU */ | |
313 | case LDPK_UNDEF: | |
314 | section = bfd_und_section_ptr; | |
315 | break; | |
316 | ||
317 | case LDPK_COMMON: | |
318 | flags = BSF_GLOBAL; | |
319 | section = bfd_com_section_ptr; | |
320 | asym->value = ldsym->size; | |
5c08b7d4 L |
321 | /* For ELF targets, set alignment of common symbol to 1. */ |
322 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) | |
02d00247 AM |
323 | { |
324 | ((elf_symbol_type *) asym)->internal_elf_sym.st_shndx = SHN_COMMON; | |
325 | ((elf_symbol_type *) asym)->internal_elf_sym.st_value = 1; | |
326 | } | |
5d3236ee DK |
327 | break; |
328 | ||
329 | default: | |
330 | return LDPS_ERR; | |
331 | } | |
332 | asym->flags = flags; | |
333 | asym->section = section; | |
334 | ||
335 | /* Visibility only applies on ELF targets. */ | |
336 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) | |
337 | { | |
338 | elf_symbol_type *elfsym = elf_symbol_from (abfd, asym); | |
cfac8028 L |
339 | unsigned char visibility; |
340 | ||
5d3236ee | 341 | if (!elfsym) |
ea360572 | 342 | einfo (_("%P%F: %s: non-ELF symbol in ELF BFD!\n"), asym->name); |
cfac8028 L |
343 | switch (ldsym->visibility) |
344 | { | |
345 | default: | |
ea360572 | 346 | einfo (_("%P%F: unknown ELF symbol visibility: %d!\n"), |
cfac8028 L |
347 | ldsym->visibility); |
348 | case LDPV_DEFAULT: | |
349 | visibility = STV_DEFAULT; | |
350 | break; | |
351 | case LDPV_PROTECTED: | |
352 | visibility = STV_PROTECTED; | |
353 | break; | |
354 | case LDPV_INTERNAL: | |
355 | visibility = STV_INTERNAL; | |
356 | break; | |
357 | case LDPV_HIDDEN: | |
358 | visibility = STV_HIDDEN; | |
359 | break; | |
360 | } | |
361 | elfsym->internal_elf_sym.st_other | |
362 | = (visibility | (elfsym->internal_elf_sym.st_other | |
363 | & ~ELF_ST_VISIBILITY (-1))); | |
5d3236ee DK |
364 | } |
365 | ||
366 | return LDPS_OK; | |
367 | } | |
368 | ||
369 | /* Register a claim-file handler. */ | |
370 | static enum ld_plugin_status | |
371 | register_claim_file (ld_plugin_claim_file_handler handler) | |
372 | { | |
373 | ASSERT (called_plugin); | |
374 | called_plugin->claim_file_handler = handler; | |
375 | return LDPS_OK; | |
376 | } | |
377 | ||
378 | /* Register an all-symbols-read handler. */ | |
379 | static enum ld_plugin_status | |
380 | register_all_symbols_read (ld_plugin_all_symbols_read_handler handler) | |
381 | { | |
382 | ASSERT (called_plugin); | |
383 | called_plugin->all_symbols_read_handler = handler; | |
384 | return LDPS_OK; | |
385 | } | |
386 | ||
387 | /* Register a cleanup handler. */ | |
388 | static enum ld_plugin_status | |
389 | register_cleanup (ld_plugin_cleanup_handler handler) | |
390 | { | |
391 | ASSERT (called_plugin); | |
392 | called_plugin->cleanup_handler = handler; | |
393 | return LDPS_OK; | |
394 | } | |
395 | ||
396 | /* Add symbols from a plugin-claimed input file. */ | |
397 | static enum ld_plugin_status | |
398 | add_symbols (void *handle, int nsyms, const struct ld_plugin_symbol *syms) | |
399 | { | |
400 | asymbol **symptrs; | |
401 | bfd *abfd = handle; | |
7fe550fc | 402 | int n; |
43e1669b | 403 | |
5d3236ee DK |
404 | ASSERT (called_plugin); |
405 | symptrs = xmalloc (nsyms * sizeof *symptrs); | |
7fe550fc | 406 | for (n = 0; n < nsyms; n++) |
5d3236ee DK |
407 | { |
408 | enum ld_plugin_status rv; | |
0c511000 AM |
409 | asymbol *bfdsym; |
410 | ||
0c511000 | 411 | bfdsym = bfd_make_empty_symbol (abfd); |
7fe550fc | 412 | symptrs[n] = bfdsym; |
5d3236ee DK |
413 | rv = asymbol_from_plugin_symbol (abfd, bfdsym, syms + n); |
414 | if (rv != LDPS_OK) | |
415 | return rv; | |
416 | } | |
7fe550fc | 417 | bfd_set_symtab (abfd, symptrs, nsyms); |
5d3236ee DK |
418 | return LDPS_OK; |
419 | } | |
420 | ||
421 | /* Get the input file information with an open (possibly re-opened) | |
422 | file descriptor. */ | |
423 | static enum ld_plugin_status | |
424 | get_input_file (const void *handle, struct ld_plugin_input_file *file) | |
425 | { | |
426 | ASSERT (called_plugin); | |
427 | handle = handle; | |
428 | file = file; | |
429 | return LDPS_ERR; | |
430 | } | |
431 | ||
432 | /* Release the input file. */ | |
433 | static enum ld_plugin_status | |
434 | release_input_file (const void *handle) | |
435 | { | |
436 | ASSERT (called_plugin); | |
437 | handle = handle; | |
438 | return LDPS_ERR; | |
439 | } | |
440 | ||
42a851a9 DK |
441 | /* Return TRUE if a defined symbol might be reachable from outside the |
442 | universe of claimed objects. */ | |
443 | static inline bfd_boolean | |
444 | is_visible_from_outside (struct ld_plugin_symbol *lsym, asection *section, | |
f84854b6 | 445 | struct bfd_link_hash_entry *blhe) |
42a851a9 | 446 | { |
35ed3f94 AM |
447 | struct bfd_sym_chain *sym; |
448 | ||
42a851a9 DK |
449 | /* Section's owner may be NULL if it is the absolute |
450 | section, fortunately is_ir_dummy_bfd handles that. */ | |
451 | if (!is_ir_dummy_bfd (section->owner)) | |
452 | return TRUE; | |
453 | if (link_info.relocatable) | |
454 | return TRUE; | |
455 | if (link_info.export_dynamic || link_info.shared) | |
456 | { | |
fd91d419 L |
457 | /* Check if symbol is hidden by version script. */ |
458 | if (bfd_hide_sym_by_version (link_info.version_info, | |
459 | blhe->root.string)) | |
460 | return FALSE; | |
42a851a9 DK |
461 | /* Only ELF symbols really have visibility. */ |
462 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour) | |
463 | { | |
464 | struct elf_link_hash_entry *el = (struct elf_link_hash_entry *)blhe; | |
465 | int vis = ELF_ST_VISIBILITY (el->other); | |
466 | return vis == STV_DEFAULT || vis == STV_PROTECTED; | |
467 | } | |
468 | /* On non-ELF targets, we can safely make inferences by considering | |
f84854b6 | 469 | what visibility the plugin would have liked to apply when it first |
42a851a9 DK |
470 | sent us the symbol. During ELF symbol processing, visibility only |
471 | ever becomes more restrictive, not less, when symbols are merged, | |
472 | so this is a conservative estimate; it may give false positives, | |
473 | declaring something visible from outside when it in fact would | |
474 | not have been, but this will only lead to missed optimisation | |
475 | opportunities during LTRANS at worst; it will not give false | |
476 | negatives, which can lead to the disastrous conclusion that the | |
477 | related symbol is IRONLY. (See GCC PR46319 for an example.) */ | |
cfac8028 L |
478 | return (lsym->visibility == LDPV_DEFAULT |
479 | || lsym->visibility == LDPV_PROTECTED); | |
42a851a9 | 480 | } |
35ed3f94 AM |
481 | |
482 | for (sym = &entry_symbol; sym != NULL; sym = sym->next) | |
483 | if (sym->name | |
484 | && strcmp (sym->name, blhe->root.string) == 0) | |
485 | return TRUE; | |
486 | ||
42a851a9 DK |
487 | return FALSE; |
488 | } | |
489 | ||
5d3236ee DK |
490 | /* Get the symbol resolution info for a plugin-claimed input file. */ |
491 | static enum ld_plugin_status | |
492 | get_symbols (const void *handle, int nsyms, struct ld_plugin_symbol *syms) | |
493 | { | |
494 | const bfd *abfd = handle; | |
495 | int n; | |
496 | ASSERT (called_plugin); | |
497 | for (n = 0; n < nsyms; n++) | |
498 | { | |
499 | struct bfd_link_hash_entry *blhe; | |
500 | bfd_boolean ironly; | |
42a851a9 | 501 | asection *owner_sec; |
10be1b6a DK |
502 | if (syms[n].def != LDPK_UNDEF) |
503 | blhe = bfd_link_hash_lookup (link_info.hash, syms[n].name, | |
504 | FALSE, FALSE, TRUE); | |
505 | else | |
506 | blhe = bfd_wrapped_link_hash_lookup (link_info.output_bfd, &link_info, | |
507 | syms[n].name, FALSE, FALSE, TRUE); | |
5d3236ee DK |
508 | if (!blhe) |
509 | { | |
510 | syms[n].resolution = LDPR_UNKNOWN; | |
1715a13c | 511 | goto report_symbol; |
5d3236ee DK |
512 | } |
513 | ||
514 | /* Determine resolution from blhe type and symbol's original type. */ | |
515 | if (blhe->type == bfd_link_hash_undefined | |
f84854b6 | 516 | || blhe->type == bfd_link_hash_undefweak) |
5d3236ee DK |
517 | { |
518 | syms[n].resolution = LDPR_UNDEF; | |
1715a13c | 519 | goto report_symbol; |
5d3236ee DK |
520 | } |
521 | if (blhe->type != bfd_link_hash_defined | |
f84854b6 L |
522 | && blhe->type != bfd_link_hash_defweak |
523 | && blhe->type != bfd_link_hash_common) | |
5d3236ee DK |
524 | { |
525 | /* We should not have a new, indirect or warning symbol here. */ | |
ea360572 | 526 | einfo ("%P%F: %s: plugin symbol table corrupt (sym type %d)\n", |
f84854b6 | 527 | called_plugin->name, blhe->type); |
5d3236ee DK |
528 | } |
529 | ||
42a851a9 DK |
530 | /* Find out which section owns the symbol. Since it's not undef, |
531 | it must have an owner; if it's not a common symbol, both defs | |
532 | and weakdefs keep it in the same place. */ | |
9e2278f5 AM |
533 | owner_sec = (blhe->type == bfd_link_hash_common |
534 | ? blhe->u.c.p->section | |
535 | : blhe->u.def.section); | |
42a851a9 DK |
536 | |
537 | /* We need to know if the sym is referenced from non-IR files. Or | |
f84854b6 | 538 | even potentially-referenced, perhaps in a future final link if |
42a851a9 DK |
539 | this is a partial one, perhaps dynamically at load-time if the |
540 | symbol is externally visible. */ | |
35ed3f94 AM |
541 | ironly = !(blhe->non_ir_ref |
542 | || is_visible_from_outside (&syms[n], owner_sec, blhe)); | |
5d3236ee DK |
543 | |
544 | /* If it was originally undefined or common, then it has been | |
f84854b6 L |
545 | resolved; determine how. */ |
546 | if (syms[n].def == LDPK_UNDEF | |
547 | || syms[n].def == LDPK_WEAKUNDEF | |
5d3236ee DK |
548 | || syms[n].def == LDPK_COMMON) |
549 | { | |
5d3236ee DK |
550 | if (owner_sec->owner == link_info.output_bfd) |
551 | syms[n].resolution = LDPR_RESOLVED_EXEC; | |
552 | else if (owner_sec->owner == abfd) | |
f84854b6 L |
553 | syms[n].resolution = (ironly |
554 | ? LDPR_PREVAILING_DEF_IRONLY | |
555 | : LDPR_PREVAILING_DEF); | |
5d3236ee DK |
556 | else if (is_ir_dummy_bfd (owner_sec->owner)) |
557 | syms[n].resolution = LDPR_RESOLVED_IR; | |
cc322803 L |
558 | else if (owner_sec->owner != NULL |
559 | && (owner_sec->owner->flags & DYNAMIC) != 0) | |
5d3236ee DK |
560 | syms[n].resolution = LDPR_RESOLVED_DYN; |
561 | else | |
562 | syms[n].resolution = LDPR_RESOLVED_EXEC; | |
1715a13c | 563 | goto report_symbol; |
5d3236ee DK |
564 | } |
565 | ||
566 | /* Was originally def, or weakdef. Does it prevail? If the | |
f84854b6 | 567 | owner is the original dummy bfd that supplied it, then this |
5d3236ee | 568 | is the definition that has prevailed. */ |
42a851a9 | 569 | if (owner_sec->owner == link_info.output_bfd) |
5d3236ee | 570 | syms[n].resolution = LDPR_PREEMPTED_REG; |
42a851a9 | 571 | else if (owner_sec->owner == abfd) |
5d3236ee | 572 | { |
f84854b6 | 573 | syms[n].resolution = (ironly |
5d3236ee | 574 | ? LDPR_PREVAILING_DEF_IRONLY |
f84854b6 | 575 | : LDPR_PREVAILING_DEF); |
1715a13c | 576 | goto report_symbol; |
5d3236ee DK |
577 | } |
578 | ||
579 | /* Was originally def, weakdef, or common, but has been pre-empted. */ | |
1715a13c L |
580 | syms[n].resolution = (is_ir_dummy_bfd (owner_sec->owner) |
581 | ? LDPR_PREEMPTED_IR | |
582 | : LDPR_PREEMPTED_REG); | |
583 | ||
9e2278f5 | 584 | report_symbol: |
1715a13c | 585 | if (report_plugin_symbols) |
9e2278f5 AM |
586 | einfo (_("%P: %B: symbol `%s' " |
587 | "definition: %d, visibility: %d, resolution: %d\n"), | |
588 | abfd, syms[n].name, | |
589 | syms[n].def, syms[n].visibility, syms[n].resolution); | |
5d3236ee DK |
590 | } |
591 | return LDPS_OK; | |
592 | } | |
593 | ||
594 | /* Add a new (real) input file generated by a plugin. */ | |
595 | static enum ld_plugin_status | |
596 | add_input_file (const char *pathname) | |
597 | { | |
598 | ASSERT (called_plugin); | |
d4cb7acd | 599 | if (!lang_add_input_file (xstrdup (pathname), lang_input_file_is_file_enum, |
f84854b6 | 600 | NULL)) |
5d3236ee DK |
601 | return LDPS_ERR; |
602 | return LDPS_OK; | |
603 | } | |
604 | ||
605 | /* Add a new (real) library required by a plugin. */ | |
606 | static enum ld_plugin_status | |
607 | add_input_library (const char *pathname) | |
608 | { | |
609 | ASSERT (called_plugin); | |
d4cb7acd | 610 | if (!lang_add_input_file (xstrdup (pathname), lang_input_file_is_l_enum, |
f84854b6 | 611 | NULL)) |
5d3236ee DK |
612 | return LDPS_ERR; |
613 | return LDPS_OK; | |
614 | } | |
615 | ||
616 | /* Set the extra library path to be used by libraries added via | |
617 | add_input_library. */ | |
618 | static enum ld_plugin_status | |
619 | set_extra_library_path (const char *path) | |
620 | { | |
621 | ASSERT (called_plugin); | |
d4cb7acd | 622 | ldfile_add_library_path (xstrdup (path), FALSE); |
5d3236ee DK |
623 | return LDPS_OK; |
624 | } | |
625 | ||
626 | /* Issue a diagnostic message from a plugin. */ | |
627 | static enum ld_plugin_status | |
628 | message (int level, const char *format, ...) | |
629 | { | |
630 | va_list args; | |
631 | va_start (args, format); | |
632 | ||
633 | switch (level) | |
634 | { | |
635 | case LDPL_INFO: | |
636 | vfinfo (stdout, format, args, FALSE); | |
d251c5c4 | 637 | putchar ('\n'); |
5d3236ee DK |
638 | break; |
639 | case LDPL_WARNING: | |
640 | vfinfo (stdout, format, args, TRUE); | |
d251c5c4 | 641 | putchar ('\n'); |
5d3236ee DK |
642 | break; |
643 | case LDPL_FATAL: | |
644 | case LDPL_ERROR: | |
645 | default: | |
9e2278f5 AM |
646 | { |
647 | char *newfmt = ACONCAT ((level == LDPL_FATAL ? "%P%F: " : "%P%X: ", | |
648 | format, "\n", (const char *) NULL)); | |
649 | fflush (stdout); | |
650 | vfinfo (stderr, newfmt, args, TRUE); | |
651 | fflush (stderr); | |
652 | } | |
5d3236ee DK |
653 | break; |
654 | } | |
655 | ||
656 | va_end (args); | |
657 | return LDPS_OK; | |
658 | } | |
659 | ||
660 | /* Helper to size leading part of tv array and set it up. */ | |
661 | static size_t | |
662 | set_tv_header (struct ld_plugin_tv *tv) | |
663 | { | |
664 | size_t i; | |
665 | ||
666 | /* Version info. */ | |
667 | static const unsigned int major = (unsigned)(BFD_VERSION / 100000000UL); | |
668 | static const unsigned int minor = (unsigned)(BFD_VERSION / 1000000UL) % 100; | |
669 | ||
670 | if (!tv) | |
671 | return tv_header_size; | |
672 | ||
673 | for (i = 0; i < tv_header_size; i++) | |
674 | { | |
675 | tv[i].tv_tag = tv_header_tags[i]; | |
676 | #define TVU(x) tv[i].tv_u.tv_ ## x | |
677 | switch (tv[i].tv_tag) | |
678 | { | |
f84854b6 L |
679 | case LDPT_MESSAGE: |
680 | TVU(message) = message; | |
681 | break; | |
682 | case LDPT_API_VERSION: | |
683 | TVU(val) = LD_PLUGIN_API_VERSION; | |
684 | break; | |
685 | case LDPT_GNU_LD_VERSION: | |
686 | TVU(val) = major * 100 + minor; | |
687 | break; | |
688 | case LDPT_LINKER_OUTPUT: | |
689 | TVU(val) = (link_info.relocatable | |
690 | ? LDPO_REL | |
691 | : (link_info.shared ? LDPO_DYN : LDPO_EXEC)); | |
692 | break; | |
693 | case LDPT_OUTPUT_NAME: | |
694 | TVU(string) = output_filename; | |
695 | break; | |
696 | case LDPT_REGISTER_CLAIM_FILE_HOOK: | |
697 | TVU(register_claim_file) = register_claim_file; | |
698 | break; | |
699 | case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: | |
700 | TVU(register_all_symbols_read) = register_all_symbols_read; | |
701 | break; | |
702 | case LDPT_REGISTER_CLEANUP_HOOK: | |
703 | TVU(register_cleanup) = register_cleanup; | |
704 | break; | |
705 | case LDPT_ADD_SYMBOLS: | |
706 | TVU(add_symbols) = add_symbols; | |
707 | break; | |
708 | case LDPT_GET_INPUT_FILE: | |
709 | TVU(get_input_file) = get_input_file; | |
710 | break; | |
711 | case LDPT_RELEASE_INPUT_FILE: | |
712 | TVU(release_input_file) = release_input_file; | |
713 | break; | |
714 | case LDPT_GET_SYMBOLS: | |
715 | TVU(get_symbols) = get_symbols; | |
716 | break; | |
717 | case LDPT_ADD_INPUT_FILE: | |
718 | TVU(add_input_file) = add_input_file; | |
719 | break; | |
720 | case LDPT_ADD_INPUT_LIBRARY: | |
721 | TVU(add_input_library) = add_input_library; | |
722 | break; | |
723 | case LDPT_SET_EXTRA_LIBRARY_PATH: | |
724 | TVU(set_extra_library_path) = set_extra_library_path; | |
725 | break; | |
726 | default: | |
727 | /* Added a new entry to the array without adding | |
728 | a new case to set up its value is a bug. */ | |
729 | FAIL (); | |
5d3236ee DK |
730 | } |
731 | #undef TVU | |
732 | } | |
733 | return tv_header_size; | |
734 | } | |
735 | ||
736 | /* Append the per-plugin args list and trailing LDPT_NULL to tv. */ | |
737 | static void | |
738 | set_tv_plugin_args (plugin_t *plugin, struct ld_plugin_tv *tv) | |
739 | { | |
740 | plugin_arg_t *arg = plugin->args; | |
741 | while (arg) | |
742 | { | |
743 | tv->tv_tag = LDPT_OPTION; | |
744 | tv->tv_u.tv_string = arg->arg; | |
745 | arg = arg->next; | |
746 | tv++; | |
747 | } | |
748 | tv->tv_tag = LDPT_NULL; | |
749 | tv->tv_u.tv_val = 0; | |
750 | } | |
751 | ||
d44ad554 DK |
752 | /* Return true if any plugins are active this run. Only valid |
753 | after options have been processed. */ | |
754 | bfd_boolean | |
755 | plugin_active_plugins_p (void) | |
756 | { | |
757 | return plugins_list != NULL; | |
758 | } | |
759 | ||
5d3236ee | 760 | /* Load up and initialise all plugins after argument parsing. */ |
d44ad554 DK |
761 | int |
762 | plugin_load_plugins (void) | |
5d3236ee DK |
763 | { |
764 | struct ld_plugin_tv *my_tv; | |
765 | unsigned int max_args = 0; | |
766 | plugin_t *curplug = plugins_list; | |
767 | ||
768 | /* If there are no plugins, we need do nothing this run. */ | |
769 | if (!curplug) | |
770 | return 0; | |
771 | ||
772 | /* First pass over plugins to find max # args needed so that we | |
773 | can size and allocate the tv array. */ | |
774 | while (curplug) | |
775 | { | |
776 | if (curplug->n_args > max_args) | |
777 | max_args = curplug->n_args; | |
778 | curplug = curplug->next; | |
779 | } | |
780 | ||
781 | /* Allocate tv array and initialise constant part. */ | |
782 | my_tv = xmalloc ((max_args + 1 + tv_header_size) * sizeof *my_tv); | |
783 | set_tv_header (my_tv); | |
784 | ||
785 | /* Pass over plugins again, activating them. */ | |
786 | curplug = plugins_list; | |
787 | while (curplug) | |
788 | { | |
789 | enum ld_plugin_status rv; | |
790 | ld_plugin_onload onloadfn = dlsym (curplug->dlhandle, "onload"); | |
791 | if (!onloadfn) | |
792 | onloadfn = dlsym (curplug->dlhandle, "_onload"); | |
793 | if (!onloadfn) | |
f84854b6 | 794 | return set_plugin_error (curplug->name); |
5d3236ee DK |
795 | set_tv_plugin_args (curplug, &my_tv[tv_header_size]); |
796 | called_plugin = curplug; | |
797 | rv = (*onloadfn) (my_tv); | |
798 | called_plugin = NULL; | |
799 | if (rv != LDPS_OK) | |
f84854b6 | 800 | return set_plugin_error (curplug->name); |
5d3236ee DK |
801 | curplug = curplug->next; |
802 | } | |
803 | ||
804 | /* Since plugin(s) inited ok, assume they're going to want symbol | |
805 | resolutions, which needs us to track which symbols are referenced | |
806 | by non-IR files using the linker's notice callback. */ | |
9e2278f5 AM |
807 | orig_notice_all = link_info.notice_all; |
808 | orig_callbacks = link_info.callbacks; | |
809 | plugin_callbacks = *orig_callbacks; | |
810 | plugin_callbacks.notice = &plugin_notice; | |
5d3236ee | 811 | link_info.notice_all = TRUE; |
9e2278f5 | 812 | link_info.callbacks = &plugin_callbacks; |
5d3236ee DK |
813 | |
814 | return 0; | |
815 | } | |
816 | ||
817 | /* Call 'claim file' hook for all plugins. */ | |
02d00247 | 818 | static int |
5d3236ee DK |
819 | plugin_call_claim_file (const struct ld_plugin_input_file *file, int *claimed) |
820 | { | |
821 | plugin_t *curplug = plugins_list; | |
822 | *claimed = FALSE; | |
823 | if (no_more_claiming) | |
824 | return 0; | |
825 | while (curplug && !*claimed) | |
826 | { | |
827 | if (curplug->claim_file_handler) | |
828 | { | |
829 | enum ld_plugin_status rv; | |
830 | called_plugin = curplug; | |
831 | rv = (*curplug->claim_file_handler) (file, claimed); | |
832 | called_plugin = NULL; | |
833 | if (rv != LDPS_OK) | |
834 | set_plugin_error (curplug->name); | |
835 | } | |
836 | curplug = curplug->next; | |
837 | } | |
838 | return plugin_error_p () ? -1 : 0; | |
839 | } | |
840 | ||
02d00247 AM |
841 | void |
842 | plugin_maybe_claim (struct ld_plugin_input_file *file, | |
843 | lang_input_statement_type *entry) | |
844 | { | |
845 | int claimed = 0; | |
846 | ||
847 | /* We create a dummy BFD, initially empty, to house whatever symbols | |
848 | the plugin may want to add. */ | |
849 | file->handle = plugin_get_ir_dummy_bfd (entry->the_bfd->filename, | |
850 | entry->the_bfd); | |
851 | if (plugin_call_claim_file (file, &claimed)) | |
852 | einfo (_("%P%F: %s: plugin reported error claiming file\n"), | |
853 | plugin_error_plugin ()); | |
854 | /* fd belongs to us, not the plugin; but we don't need it. */ | |
855 | close (file->fd); | |
856 | if (claimed) | |
857 | { | |
858 | /* Discard the real file's BFD and substitute the dummy one. */ | |
859 | ||
860 | /* BFD archive handling caches elements so we can't call | |
861 | bfd_close for archives. */ | |
862 | if (entry->the_bfd->my_archive == NULL) | |
863 | bfd_close (entry->the_bfd); | |
864 | entry->the_bfd = file->handle; | |
865 | entry->claimed = TRUE; | |
866 | bfd_make_readable (entry->the_bfd); | |
867 | } | |
868 | else | |
869 | { | |
870 | /* If plugin didn't claim the file, we don't need the dummy bfd. | |
871 | Can't avoid speculatively creating it, alas. */ | |
872 | bfd_close_all_done (file->handle); | |
873 | entry->claimed = FALSE; | |
874 | } | |
875 | } | |
876 | ||
5d3236ee DK |
877 | /* Call 'all symbols read' hook for all plugins. */ |
878 | int | |
879 | plugin_call_all_symbols_read (void) | |
880 | { | |
881 | plugin_t *curplug = plugins_list; | |
882 | ||
883 | /* Disable any further file-claiming. */ | |
884 | no_more_claiming = TRUE; | |
885 | ||
5d3236ee DK |
886 | while (curplug) |
887 | { | |
888 | if (curplug->all_symbols_read_handler) | |
889 | { | |
890 | enum ld_plugin_status rv; | |
891 | called_plugin = curplug; | |
892 | rv = (*curplug->all_symbols_read_handler) (); | |
893 | called_plugin = NULL; | |
894 | if (rv != LDPS_OK) | |
895 | set_plugin_error (curplug->name); | |
896 | } | |
897 | curplug = curplug->next; | |
898 | } | |
899 | return plugin_error_p () ? -1 : 0; | |
900 | } | |
901 | ||
e73d965c | 902 | /* Call 'cleanup' hook for all plugins at exit. */ |
498cd2a0 | 903 | void |
5d3236ee DK |
904 | plugin_call_cleanup (void) |
905 | { | |
906 | plugin_t *curplug = plugins_list; | |
907 | while (curplug) | |
908 | { | |
909 | if (curplug->cleanup_handler && !curplug->cleanup_done) | |
910 | { | |
911 | enum ld_plugin_status rv; | |
912 | curplug->cleanup_done = TRUE; | |
913 | called_plugin = curplug; | |
914 | rv = (*curplug->cleanup_handler) (); | |
915 | called_plugin = NULL; | |
916 | if (rv != LDPS_OK) | |
917 | set_plugin_error (curplug->name); | |
918 | dlclose (curplug->dlhandle); | |
919 | } | |
920 | curplug = curplug->next; | |
921 | } | |
e73d965c L |
922 | if (plugin_error_p ()) |
923 | info_msg (_("%P: %s: error in plugin cleanup (ignored)\n"), | |
924 | plugin_error_plugin ()); | |
5d3236ee DK |
925 | } |
926 | ||
5d3236ee DK |
927 | /* To determine which symbols should be resolved LDPR_PREVAILING_DEF |
928 | and which LDPR_PREVAILING_DEF_IRONLY, we notice all the symbols as | |
35ed3f94 AM |
929 | the linker adds them to the linker hash table. Mark those |
930 | referenced from a non-IR file with non_ir_ref. We have to | |
931 | notice_all symbols, because we won't necessarily know until later | |
932 | which ones will be contributed by IR files. */ | |
9e2278f5 AM |
933 | static bfd_boolean |
934 | plugin_notice (struct bfd_link_info *info, | |
35ed3f94 | 935 | struct bfd_link_hash_entry *h, |
9e2278f5 AM |
936 | bfd *abfd, |
937 | asection *section, | |
16d96b5b AM |
938 | bfd_vma value, |
939 | flagword flags, | |
940 | const char *string) | |
5d3236ee | 941 | { |
35ed3f94 | 942 | if (h != NULL) |
5d3236ee | 943 | { |
cd6eee13 AM |
944 | bfd *sym_bfd; |
945 | ||
9e2278f5 AM |
946 | /* No further processing if this def/ref is from an IR dummy BFD. */ |
947 | if (is_ir_dummy_bfd (abfd)) | |
948 | return TRUE; | |
949 | ||
16d96b5b AM |
950 | /* Making an indirect symbol counts as a reference unless this |
951 | is a brand new symbol. */ | |
952 | if (bfd_is_ind_section (section) | |
953 | || (flags & BSF_INDIRECT) != 0) | |
954 | { | |
955 | if (h->type != bfd_link_hash_new) | |
956 | { | |
957 | struct bfd_link_hash_entry *inh; | |
958 | ||
959 | h->non_ir_ref = TRUE; | |
960 | inh = bfd_wrapped_link_hash_lookup (abfd, info, string, FALSE, | |
961 | FALSE, FALSE); | |
962 | if (inh != NULL) | |
963 | inh->non_ir_ref = TRUE; | |
964 | } | |
965 | } | |
966 | ||
967 | /* Nothing to do here for warning symbols. */ | |
968 | else if ((flags & BSF_WARNING) != 0) | |
969 | ; | |
970 | ||
971 | /* Nothing to do here for constructor symbols. */ | |
972 | else if ((flags & BSF_CONSTRUCTOR) != 0) | |
973 | ; | |
974 | ||
35ed3f94 | 975 | /* If this is a ref, set non_ir_ref. */ |
16d96b5b | 976 | else if (bfd_is_und_section (section)) |
35ed3f94 AM |
977 | h->non_ir_ref = TRUE; |
978 | ||
979 | /* Otherwise, it must be a new def. Ensure any symbol defined | |
980 | in an IR dummy BFD takes on a new value from a real BFD. | |
981 | Weak symbols are not normally overridden by a new weak | |
982 | definition, and strong symbols will normally cause multiple | |
983 | definition errors. Avoid this by making the symbol appear | |
984 | to be undefined. */ | |
985 | else if (((h->type == bfd_link_hash_defweak | |
986 | || h->type == bfd_link_hash_defined) | |
cd6eee13 | 987 | && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner)) |
35ed3f94 | 988 | || (h->type == bfd_link_hash_common |
cd6eee13 AM |
989 | && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner))) |
990 | { | |
991 | h->type = bfd_link_hash_undefweak; | |
992 | h->u.undef.abfd = sym_bfd; | |
993 | } | |
5d3236ee DK |
994 | } |
995 | ||
996 | /* Continue with cref/nocrossref/trace-sym processing. */ | |
35ed3f94 | 997 | if (h == NULL |
9e2278f5 AM |
998 | || orig_notice_all |
999 | || (info->notice_hash != NULL | |
35ed3f94 AM |
1000 | && bfd_hash_lookup (info->notice_hash, h->root.string, |
1001 | FALSE, FALSE) != NULL)) | |
16d96b5b AM |
1002 | return (*orig_callbacks->notice) (info, h, |
1003 | abfd, section, value, flags, string); | |
5d3236ee DK |
1004 | return TRUE; |
1005 | } |