~ubuntu-branches/ubuntu/utopic/binutils-arm64-cross/utopic

« back to all changes in this revision

Viewing changes to binutils-2.23.52.20130611/ld/plugin.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-06-20 17:38:09 UTC
  • Revision ID: package-import@ubuntu.com-20130620173809-app8lzgvymy5fg6c
Tags: 0.7
Build-depend on binutils-source (>= 2.23.52.20130620-1~).

Show diffs side-by-side

added added

removed removed

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