OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [bfd/] [elflink.c] - Blame information for rev 154

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 khays
/* ELF linking support for BFD.
2
   Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3
   2005, 2006, 2007, 2008, 2009, 2010, 2011
4
   Free Software Foundation, Inc.
5
 
6
   This file is part of BFD, the Binary File Descriptor library.
7
 
8
   This program is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 3 of the License, or
11
   (at your option) any later version.
12
 
13
   This program is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
17
 
18
   You should have received a copy of the GNU General Public License
19
   along with this program; if not, write to the Free Software
20
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21
   MA 02110-1301, USA.  */
22
 
23
#include "sysdep.h"
24
#include "bfd.h"
25
#include "bfdlink.h"
26
#include "libbfd.h"
27
#define ARCH_SIZE 0
28
#include "elf-bfd.h"
29
#include "safe-ctype.h"
30
#include "libiberty.h"
31
#include "objalloc.h"
32
 
33
/* This struct is used to pass information to routines called via
34
   elf_link_hash_traverse which must return failure.  */
35
 
36
struct elf_info_failed
37
{
38
  struct bfd_link_info *info;
39
  struct bfd_elf_version_tree *verdefs;
40
  bfd_boolean failed;
41
};
42
 
43
/* This structure is used to pass information to
44
   _bfd_elf_link_find_version_dependencies.  */
45
 
46
struct elf_find_verdep_info
47
{
48
  /* General link information.  */
49
  struct bfd_link_info *info;
50
  /* The number of dependencies.  */
51
  unsigned int vers;
52
  /* Whether we had a failure.  */
53
  bfd_boolean failed;
54
};
55
 
56
static bfd_boolean _bfd_elf_fix_symbol_flags
57
  (struct elf_link_hash_entry *, struct elf_info_failed *);
58
 
59
/* Define a symbol in a dynamic linkage section.  */
60
 
61
struct elf_link_hash_entry *
62
_bfd_elf_define_linkage_sym (bfd *abfd,
63
                             struct bfd_link_info *info,
64
                             asection *sec,
65
                             const char *name)
66
{
67
  struct elf_link_hash_entry *h;
68
  struct bfd_link_hash_entry *bh;
69
  const struct elf_backend_data *bed;
70
 
71
  h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
72
  if (h != NULL)
73
    {
74
      /* Zap symbol defined in an as-needed lib that wasn't linked.
75
         This is a symptom of a larger problem:  Absolute symbols
76
         defined in shared libraries can't be overridden, because we
77
         lose the link to the bfd which is via the symbol section.  */
78
      h->root.type = bfd_link_hash_new;
79
    }
80
 
81
  bh = &h->root;
82
  if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
83
                                         sec, 0, NULL, FALSE,
84
                                         get_elf_backend_data (abfd)->collect,
85
                                         &bh))
86
    return NULL;
87
  h = (struct elf_link_hash_entry *) bh;
88
  h->def_regular = 1;
89
  h->non_elf = 0;
90
  h->type = STT_OBJECT;
91
  h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
92
 
93
  bed = get_elf_backend_data (abfd);
94
  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
95
  return h;
96
}
97
 
98
bfd_boolean
99
_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
100
{
101
  flagword flags;
102
  asection *s;
103
  struct elf_link_hash_entry *h;
104
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
105
  struct elf_link_hash_table *htab = elf_hash_table (info);
106
 
107
  /* This function may be called more than once.  */
108
  s = bfd_get_section_by_name (abfd, ".got");
109
  if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
110
    return TRUE;
111
 
112
  flags = bed->dynamic_sec_flags;
113
 
114
  s = bfd_make_section_with_flags (abfd,
115
                                   (bed->rela_plts_and_copies_p
116
                                    ? ".rela.got" : ".rel.got"),
117
                                   (bed->dynamic_sec_flags
118
                                    | SEC_READONLY));
119
  if (s == NULL
120
      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
121
    return FALSE;
122
  htab->srelgot = s;
123
 
124
  s = bfd_make_section_with_flags (abfd, ".got", flags);
125
  if (s == NULL
126
      || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
127
    return FALSE;
128
  htab->sgot = s;
129
 
130
  if (bed->want_got_plt)
131
    {
132
      s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
133
      if (s == NULL
134
          || !bfd_set_section_alignment (abfd, s,
135
                                         bed->s->log_file_align))
136
        return FALSE;
137
      htab->sgotplt = s;
138
    }
139
 
140
  /* The first bit of the global offset table is the header.  */
141
  s->size += bed->got_header_size;
142
 
143
  if (bed->want_got_sym)
144
    {
145
      /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
146
         (or .got.plt) section.  We don't do this in the linker script
147
         because we don't want to define the symbol if we are not creating
148
         a global offset table.  */
149
      h = _bfd_elf_define_linkage_sym (abfd, info, s,
150
                                       "_GLOBAL_OFFSET_TABLE_");
151
      elf_hash_table (info)->hgot = h;
152
      if (h == NULL)
153
        return FALSE;
154
    }
155
 
156
  return TRUE;
157
}
158
 
159
/* Create a strtab to hold the dynamic symbol names.  */
160
static bfd_boolean
161
_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
162
{
163
  struct elf_link_hash_table *hash_table;
164
 
165
  hash_table = elf_hash_table (info);
166
  if (hash_table->dynobj == NULL)
167
    hash_table->dynobj = abfd;
168
 
169
  if (hash_table->dynstr == NULL)
170
    {
171
      hash_table->dynstr = _bfd_elf_strtab_init ();
172
      if (hash_table->dynstr == NULL)
173
        return FALSE;
174
    }
175
  return TRUE;
176
}
177
 
178
/* Create some sections which will be filled in with dynamic linking
179
   information.  ABFD is an input file which requires dynamic sections
180
   to be created.  The dynamic sections take up virtual memory space
181
   when the final executable is run, so we need to create them before
182
   addresses are assigned to the output sections.  We work out the
183
   actual contents and size of these sections later.  */
184
 
185
bfd_boolean
186
_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
187
{
188
  flagword flags;
189
  asection *s;
190
  const struct elf_backend_data *bed;
191
 
192
  if (! is_elf_hash_table (info->hash))
193
    return FALSE;
194
 
195
  if (elf_hash_table (info)->dynamic_sections_created)
196
    return TRUE;
197
 
198
  if (!_bfd_elf_link_create_dynstrtab (abfd, info))
199
    return FALSE;
200
 
201
  abfd = elf_hash_table (info)->dynobj;
202
  bed = get_elf_backend_data (abfd);
203
 
204
  flags = bed->dynamic_sec_flags;
205
 
206
  /* A dynamically linked executable has a .interp section, but a
207
     shared library does not.  */
208
  if (info->executable)
209
    {
210
      s = bfd_make_section_with_flags (abfd, ".interp",
211
                                       flags | SEC_READONLY);
212
      if (s == NULL)
213
        return FALSE;
214
    }
215
 
216
  /* Create sections to hold version informations.  These are removed
217
     if they are not needed.  */
218
  s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
219
                                   flags | SEC_READONLY);
220
  if (s == NULL
221
      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
222
    return FALSE;
223
 
224
  s = bfd_make_section_with_flags (abfd, ".gnu.version",
225
                                   flags | SEC_READONLY);
226
  if (s == NULL
227
      || ! bfd_set_section_alignment (abfd, s, 1))
228
    return FALSE;
229
 
230
  s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
231
                                   flags | SEC_READONLY);
232
  if (s == NULL
233
      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
234
    return FALSE;
235
 
236
  s = bfd_make_section_with_flags (abfd, ".dynsym",
237
                                   flags | SEC_READONLY);
238
  if (s == NULL
239
      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
240
    return FALSE;
241
 
242
  s = bfd_make_section_with_flags (abfd, ".dynstr",
243
                                   flags | SEC_READONLY);
244
  if (s == NULL)
245
    return FALSE;
246
 
247
  s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
248
  if (s == NULL
249
      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
250
    return FALSE;
251
 
252
  /* The special symbol _DYNAMIC is always set to the start of the
253
     .dynamic section.  We could set _DYNAMIC in a linker script, but we
254
     only want to define it if we are, in fact, creating a .dynamic
255
     section.  We don't want to define it if there is no .dynamic
256
     section, since on some ELF platforms the start up code examines it
257
     to decide how to initialize the process.  */
258
  if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
259
    return FALSE;
260
 
261
  if (info->emit_hash)
262
    {
263
      s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
264
      if (s == NULL
265
          || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
266
        return FALSE;
267
      elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
268
    }
269
 
270
  if (info->emit_gnu_hash)
271
    {
272
      s = bfd_make_section_with_flags (abfd, ".gnu.hash",
273
                                       flags | SEC_READONLY);
274
      if (s == NULL
275
          || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
276
        return FALSE;
277
      /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
278
         4 32-bit words followed by variable count of 64-bit words, then
279
         variable count of 32-bit words.  */
280
      if (bed->s->arch_size == 64)
281
        elf_section_data (s)->this_hdr.sh_entsize = 0;
282
      else
283
        elf_section_data (s)->this_hdr.sh_entsize = 4;
284
    }
285
 
286
  /* Let the backend create the rest of the sections.  This lets the
287
     backend set the right flags.  The backend will normally create
288
     the .got and .plt sections.  */
289 148 khays
  if (bed->elf_backend_create_dynamic_sections == NULL
290
      || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
291 14 khays
    return FALSE;
292
 
293
  elf_hash_table (info)->dynamic_sections_created = TRUE;
294
 
295
  return TRUE;
296
}
297
 
298
/* Create dynamic sections when linking against a dynamic object.  */
299
 
300
bfd_boolean
301
_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
302
{
303
  flagword flags, pltflags;
304
  struct elf_link_hash_entry *h;
305
  asection *s;
306
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
307
  struct elf_link_hash_table *htab = elf_hash_table (info);
308
 
309
  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
310
     .rel[a].bss sections.  */
311
  flags = bed->dynamic_sec_flags;
312
 
313
  pltflags = flags;
314
  if (bed->plt_not_loaded)
315
    /* We do not clear SEC_ALLOC here because we still want the OS to
316
       allocate space for the section; it's just that there's nothing
317
       to read in from the object file.  */
318
    pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
319
  else
320
    pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
321
  if (bed->plt_readonly)
322
    pltflags |= SEC_READONLY;
323
 
324
  s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
325
  if (s == NULL
326
      || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
327
    return FALSE;
328
  htab->splt = s;
329
 
330
  /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
331
     .plt section.  */
332
  if (bed->want_plt_sym)
333
    {
334
      h = _bfd_elf_define_linkage_sym (abfd, info, s,
335
                                       "_PROCEDURE_LINKAGE_TABLE_");
336
      elf_hash_table (info)->hplt = h;
337
      if (h == NULL)
338
        return FALSE;
339
    }
340
 
341
  s = bfd_make_section_with_flags (abfd,
342
                                   (bed->rela_plts_and_copies_p
343
                                    ? ".rela.plt" : ".rel.plt"),
344
                                   flags | SEC_READONLY);
345
  if (s == NULL
346
      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
347
    return FALSE;
348
  htab->srelplt = s;
349
 
350
  if (! _bfd_elf_create_got_section (abfd, info))
351
    return FALSE;
352
 
353
  if (bed->want_dynbss)
354
    {
355
      /* The .dynbss section is a place to put symbols which are defined
356
         by dynamic objects, are referenced by regular objects, and are
357
         not functions.  We must allocate space for them in the process
358
         image and use a R_*_COPY reloc to tell the dynamic linker to
359
         initialize them at run time.  The linker script puts the .dynbss
360
         section into the .bss section of the final image.  */
361
      s = bfd_make_section_with_flags (abfd, ".dynbss",
362
                                       (SEC_ALLOC
363
                                        | SEC_LINKER_CREATED));
364
      if (s == NULL)
365
        return FALSE;
366
 
367
      /* The .rel[a].bss section holds copy relocs.  This section is not
368
         normally needed.  We need to create it here, though, so that the
369
         linker will map it to an output section.  We can't just create it
370
         only if we need it, because we will not know whether we need it
371
         until we have seen all the input files, and the first time the
372
         main linker code calls BFD after examining all the input files
373
         (size_dynamic_sections) the input sections have already been
374
         mapped to the output sections.  If the section turns out not to
375
         be needed, we can discard it later.  We will never need this
376
         section when generating a shared object, since they do not use
377
         copy relocs.  */
378
      if (! info->shared)
379
        {
380
          s = bfd_make_section_with_flags (abfd,
381
                                           (bed->rela_plts_and_copies_p
382
                                            ? ".rela.bss" : ".rel.bss"),
383
                                           flags | SEC_READONLY);
384
          if (s == NULL
385
              || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
386
            return FALSE;
387
        }
388
    }
389
 
390
  return TRUE;
391
}
392
 
393
/* Record a new dynamic symbol.  We record the dynamic symbols as we
394
   read the input files, since we need to have a list of all of them
395
   before we can determine the final sizes of the output sections.
396
   Note that we may actually call this function even though we are not
397
   going to output any dynamic symbols; in some cases we know that a
398
   symbol should be in the dynamic symbol table, but only if there is
399
   one.  */
400
 
401
bfd_boolean
402
bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
403
                                    struct elf_link_hash_entry *h)
404
{
405
  if (h->dynindx == -1)
406
    {
407
      struct elf_strtab_hash *dynstr;
408
      char *p;
409
      const char *name;
410
      bfd_size_type indx;
411
 
412
      /* XXX: The ABI draft says the linker must turn hidden and
413
         internal symbols into STB_LOCAL symbols when producing the
414
         DSO. However, if ld.so honors st_other in the dynamic table,
415
         this would not be necessary.  */
416
      switch (ELF_ST_VISIBILITY (h->other))
417
        {
418
        case STV_INTERNAL:
419
        case STV_HIDDEN:
420
          if (h->root.type != bfd_link_hash_undefined
421
              && h->root.type != bfd_link_hash_undefweak)
422
            {
423
              h->forced_local = 1;
424
              if (!elf_hash_table (info)->is_relocatable_executable)
425
                return TRUE;
426
            }
427
 
428
        default:
429
          break;
430
        }
431
 
432
      h->dynindx = elf_hash_table (info)->dynsymcount;
433
      ++elf_hash_table (info)->dynsymcount;
434
 
435
      dynstr = elf_hash_table (info)->dynstr;
436
      if (dynstr == NULL)
437
        {
438
          /* Create a strtab to hold the dynamic symbol names.  */
439
          elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
440
          if (dynstr == NULL)
441
            return FALSE;
442
        }
443
 
444
      /* We don't put any version information in the dynamic string
445
         table.  */
446
      name = h->root.root.string;
447
      p = strchr (name, ELF_VER_CHR);
448
      if (p != NULL)
449
        /* We know that the p points into writable memory.  In fact,
450
           there are only a few symbols that have read-only names, being
451
           those like _GLOBAL_OFFSET_TABLE_ that are created specially
452
           by the backends.  Most symbols will have names pointing into
453
           an ELF string table read from a file, or to objalloc memory.  */
454
        *p = 0;
455
 
456
      indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
457
 
458
      if (p != NULL)
459
        *p = ELF_VER_CHR;
460
 
461
      if (indx == (bfd_size_type) -1)
462
        return FALSE;
463
      h->dynstr_index = indx;
464
    }
465
 
466
  return TRUE;
467
}
468
 
469
/* Mark a symbol dynamic.  */
470
 
471
static void
472
bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
473
                                  struct elf_link_hash_entry *h,
474
                                  Elf_Internal_Sym *sym)
475
{
476
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
477
 
478
  /* It may be called more than once on the same H.  */
479
  if(h->dynamic || info->relocatable)
480
    return;
481
 
482
  if ((info->dynamic_data
483
       && (h->type == STT_OBJECT
484
           || (sym != NULL
485
               && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
486
      || (d != NULL
487
          && h->root.type == bfd_link_hash_new
488
          && (*d->match) (&d->head, NULL, h->root.root.string)))
489
    h->dynamic = 1;
490
}
491
 
492
/* Record an assignment to a symbol made by a linker script.  We need
493
   this in case some dynamic object refers to this symbol.  */
494
 
495
bfd_boolean
496
bfd_elf_record_link_assignment (bfd *output_bfd,
497
                                struct bfd_link_info *info,
498
                                const char *name,
499
                                bfd_boolean provide,
500
                                bfd_boolean hidden)
501
{
502
  struct elf_link_hash_entry *h, *hv;
503
  struct elf_link_hash_table *htab;
504
  const struct elf_backend_data *bed;
505
 
506
  if (!is_elf_hash_table (info->hash))
507
    return TRUE;
508
 
509
  htab = elf_hash_table (info);
510
  h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
511
  if (h == NULL)
512
    return provide;
513
 
514
  switch (h->root.type)
515
    {
516
    case bfd_link_hash_defined:
517
    case bfd_link_hash_defweak:
518
    case bfd_link_hash_common:
519
      break;
520
    case bfd_link_hash_undefweak:
521
    case bfd_link_hash_undefined:
522
      /* Since we're defining the symbol, don't let it seem to have not
523
         been defined.  record_dynamic_symbol and size_dynamic_sections
524
         may depend on this.  */
525
      h->root.type = bfd_link_hash_new;
526
      if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
527
        bfd_link_repair_undef_list (&htab->root);
528
      break;
529
    case bfd_link_hash_new:
530
      bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
531
      h->non_elf = 0;
532
      break;
533
    case bfd_link_hash_indirect:
534
      /* We had a versioned symbol in a dynamic library.  We make the
535
         the versioned symbol point to this one.  */
536
      bed = get_elf_backend_data (output_bfd);
537
      hv = h;
538
      while (hv->root.type == bfd_link_hash_indirect
539
             || hv->root.type == bfd_link_hash_warning)
540
        hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
541
      /* We don't need to update h->root.u since linker will set them
542
         later.  */
543
      h->root.type = bfd_link_hash_undefined;
544
      hv->root.type = bfd_link_hash_indirect;
545
      hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
546
      (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
547
      break;
548
    case bfd_link_hash_warning:
549
      abort ();
550
      break;
551
    }
552
 
553
  /* If this symbol is being provided by the linker script, and it is
554
     currently defined by a dynamic object, but not by a regular
555
     object, then mark it as undefined so that the generic linker will
556
     force the correct value.  */
557
  if (provide
558
      && h->def_dynamic
559
      && !h->def_regular)
560
    h->root.type = bfd_link_hash_undefined;
561
 
562
  /* If this symbol is not being provided by the linker script, and it is
563
     currently defined by a dynamic object, but not by a regular object,
564
     then clear out any version information because the symbol will not be
565
     associated with the dynamic object any more.  */
566
  if (!provide
567
      && h->def_dynamic
568
      && !h->def_regular)
569
    h->verinfo.verdef = NULL;
570
 
571
  h->def_regular = 1;
572
 
573
  if (provide && hidden)
574
    {
575
      bed = get_elf_backend_data (output_bfd);
576
      h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
577
      (*bed->elf_backend_hide_symbol) (info, h, TRUE);
578
    }
579
 
580
  /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
581
     and executables.  */
582
  if (!info->relocatable
583
      && h->dynindx != -1
584
      && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
585
          || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
586
    h->forced_local = 1;
587
 
588
  if ((h->def_dynamic
589
       || h->ref_dynamic
590
       || info->shared
591
       || (info->executable && elf_hash_table (info)->is_relocatable_executable))
592
      && h->dynindx == -1)
593
    {
594
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
595
        return FALSE;
596
 
597
      /* If this is a weak defined symbol, and we know a corresponding
598
         real symbol from the same dynamic object, make sure the real
599
         symbol is also made into a dynamic symbol.  */
600
      if (h->u.weakdef != NULL
601
          && h->u.weakdef->dynindx == -1)
602
        {
603
          if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
604
            return FALSE;
605
        }
606
    }
607
 
608
  return TRUE;
609
}
610
 
611
/* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
612
   success, and 2 on a failure caused by attempting to record a symbol
613
   in a discarded section, eg. a discarded link-once section symbol.  */
614
 
615
int
616
bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
617
                                          bfd *input_bfd,
618
                                          long input_indx)
619
{
620
  bfd_size_type amt;
621
  struct elf_link_local_dynamic_entry *entry;
622
  struct elf_link_hash_table *eht;
623
  struct elf_strtab_hash *dynstr;
624
  unsigned long dynstr_index;
625
  char *name;
626
  Elf_External_Sym_Shndx eshndx;
627
  char esym[sizeof (Elf64_External_Sym)];
628
 
629
  if (! is_elf_hash_table (info->hash))
630
    return 0;
631
 
632
  /* See if the entry exists already.  */
633
  for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
634
    if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
635
      return 1;
636
 
637
  amt = sizeof (*entry);
638
  entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
639
  if (entry == NULL)
640
    return 0;
641
 
642
  /* Go find the symbol, so that we can find it's name.  */
643
  if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
644
                             1, input_indx, &entry->isym, esym, &eshndx))
645
    {
646
      bfd_release (input_bfd, entry);
647
      return 0;
648
    }
649
 
650
  if (entry->isym.st_shndx != SHN_UNDEF
651
      && entry->isym.st_shndx < SHN_LORESERVE)
652
    {
653
      asection *s;
654
 
655
      s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
656
      if (s == NULL || bfd_is_abs_section (s->output_section))
657
        {
658
          /* We can still bfd_release here as nothing has done another
659
             bfd_alloc.  We can't do this later in this function.  */
660
          bfd_release (input_bfd, entry);
661
          return 2;
662
        }
663
    }
664
 
665
  name = (bfd_elf_string_from_elf_section
666
          (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
667
           entry->isym.st_name));
668
 
669
  dynstr = elf_hash_table (info)->dynstr;
670
  if (dynstr == NULL)
671
    {
672
      /* Create a strtab to hold the dynamic symbol names.  */
673
      elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
674
      if (dynstr == NULL)
675
        return 0;
676
    }
677
 
678
  dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
679
  if (dynstr_index == (unsigned long) -1)
680
    return 0;
681
  entry->isym.st_name = dynstr_index;
682
 
683
  eht = elf_hash_table (info);
684
 
685
  entry->next = eht->dynlocal;
686
  eht->dynlocal = entry;
687
  entry->input_bfd = input_bfd;
688
  entry->input_indx = input_indx;
689
  eht->dynsymcount++;
690
 
691
  /* Whatever binding the symbol had before, it's now local.  */
692
  entry->isym.st_info
693
    = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
694
 
695
  /* The dynindx will be set at the end of size_dynamic_sections.  */
696
 
697
  return 1;
698
}
699
 
700
/* Return the dynindex of a local dynamic symbol.  */
701
 
702
long
703
_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
704
                                    bfd *input_bfd,
705
                                    long input_indx)
706
{
707
  struct elf_link_local_dynamic_entry *e;
708
 
709
  for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
710
    if (e->input_bfd == input_bfd && e->input_indx == input_indx)
711
      return e->dynindx;
712
  return -1;
713
}
714
 
715
/* This function is used to renumber the dynamic symbols, if some of
716
   them are removed because they are marked as local.  This is called
717
   via elf_link_hash_traverse.  */
718
 
719
static bfd_boolean
720
elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
721
                                      void *data)
722
{
723
  size_t *count = (size_t *) data;
724
 
725
  if (h->forced_local)
726
    return TRUE;
727
 
728
  if (h->dynindx != -1)
729
    h->dynindx = ++(*count);
730
 
731
  return TRUE;
732
}
733
 
734
 
735
/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
736
   STB_LOCAL binding.  */
737
 
738
static bfd_boolean
739
elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
740
                                            void *data)
741
{
742
  size_t *count = (size_t *) data;
743
 
744
  if (!h->forced_local)
745
    return TRUE;
746
 
747
  if (h->dynindx != -1)
748
    h->dynindx = ++(*count);
749
 
750
  return TRUE;
751
}
752
 
753
/* Return true if the dynamic symbol for a given section should be
754
   omitted when creating a shared library.  */
755
bfd_boolean
756
_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
757
                                   struct bfd_link_info *info,
758
                                   asection *p)
759
{
760
  struct elf_link_hash_table *htab;
761
 
762
  switch (elf_section_data (p)->this_hdr.sh_type)
763
    {
764
    case SHT_PROGBITS:
765
    case SHT_NOBITS:
766
      /* If sh_type is yet undecided, assume it could be
767
         SHT_PROGBITS/SHT_NOBITS.  */
768
    case SHT_NULL:
769
      htab = elf_hash_table (info);
770
      if (p == htab->tls_sec)
771
        return FALSE;
772
 
773
      if (htab->text_index_section != NULL)
774
        return p != htab->text_index_section && p != htab->data_index_section;
775
 
776
      if (strcmp (p->name, ".got") == 0
777
          || strcmp (p->name, ".got.plt") == 0
778
          || strcmp (p->name, ".plt") == 0)
779
        {
780
          asection *ip;
781
 
782
          if (htab->dynobj != NULL
783
              && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL
784
              && (ip->flags & SEC_LINKER_CREATED)
785
              && ip->output_section == p)
786
            return TRUE;
787
        }
788
      return FALSE;
789
 
790
      /* There shouldn't be section relative relocations
791
         against any other section.  */
792
    default:
793
      return TRUE;
794
    }
795
}
796
 
797
/* Assign dynsym indices.  In a shared library we generate a section
798
   symbol for each output section, which come first.  Next come symbols
799
   which have been forced to local binding.  Then all of the back-end
800
   allocated local dynamic syms, followed by the rest of the global
801
   symbols.  */
802
 
803
static unsigned long
804
_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
805
                                struct bfd_link_info *info,
806
                                unsigned long *section_sym_count)
807
{
808
  unsigned long dynsymcount = 0;
809
 
810
  if (info->shared || elf_hash_table (info)->is_relocatable_executable)
811
    {
812
      const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
813
      asection *p;
814
      for (p = output_bfd->sections; p ; p = p->next)
815
        if ((p->flags & SEC_EXCLUDE) == 0
816
            && (p->flags & SEC_ALLOC) != 0
817
            && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
818
          elf_section_data (p)->dynindx = ++dynsymcount;
819
        else
820
          elf_section_data (p)->dynindx = 0;
821
    }
822
  *section_sym_count = dynsymcount;
823
 
824
  elf_link_hash_traverse (elf_hash_table (info),
825
                          elf_link_renumber_local_hash_table_dynsyms,
826
                          &dynsymcount);
827
 
828
  if (elf_hash_table (info)->dynlocal)
829
    {
830
      struct elf_link_local_dynamic_entry *p;
831
      for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
832
        p->dynindx = ++dynsymcount;
833
    }
834
 
835
  elf_link_hash_traverse (elf_hash_table (info),
836
                          elf_link_renumber_hash_table_dynsyms,
837
                          &dynsymcount);
838
 
839
  /* There is an unused NULL entry at the head of the table which
840
     we must account for in our count.  Unless there weren't any
841
     symbols, which means we'll have no table at all.  */
842
  if (dynsymcount != 0)
843
    ++dynsymcount;
844
 
845
  elf_hash_table (info)->dynsymcount = dynsymcount;
846
  return dynsymcount;
847
}
848
 
849
/* Merge st_other field.  */
850
 
851
static void
852
elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
853
                    Elf_Internal_Sym *isym, bfd_boolean definition,
854
                    bfd_boolean dynamic)
855
{
856
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
857
 
858
  /* If st_other has a processor-specific meaning, specific
859
     code might be needed here. We never merge the visibility
860
     attribute with the one from a dynamic object.  */
861
  if (bed->elf_backend_merge_symbol_attribute)
862
    (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
863
                                                dynamic);
864
 
865
  /* If this symbol has default visibility and the user has requested
866
     we not re-export it, then mark it as hidden.  */
867
  if (definition
868
      && !dynamic
869
      && (abfd->no_export
870
          || (abfd->my_archive && abfd->my_archive->no_export))
871
      && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
872
    isym->st_other = (STV_HIDDEN
873
                      | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
874
 
875
  if (!dynamic && ELF_ST_VISIBILITY (isym->st_other) != 0)
876
    {
877
      unsigned char hvis, symvis, other, nvis;
878
 
879
      /* Only merge the visibility. Leave the remainder of the
880
         st_other field to elf_backend_merge_symbol_attribute.  */
881
      other = h->other & ~ELF_ST_VISIBILITY (-1);
882
 
883
      /* Combine visibilities, using the most constraining one.  */
884
      hvis = ELF_ST_VISIBILITY (h->other);
885
      symvis = ELF_ST_VISIBILITY (isym->st_other);
886
      if (! hvis)
887
        nvis = symvis;
888
      else if (! symvis)
889
        nvis = hvis;
890
      else
891
        nvis = hvis < symvis ? hvis : symvis;
892
 
893
      h->other = other | nvis;
894
    }
895
}
896
 
897
/* This function is called when we want to define a new symbol.  It
898
   handles the various cases which arise when we find a definition in
899
   a dynamic object, or when there is already a definition in a
900
   dynamic object.  The new symbol is described by NAME, SYM, PSEC,
901
   and PVALUE.  We set SYM_HASH to the hash table entry.  We set
902
   OVERRIDE if the old symbol is overriding a new definition.  We set
903
   TYPE_CHANGE_OK if it is OK for the type to change.  We set
904
   SIZE_CHANGE_OK if it is OK for the size to change.  By OK to
905
   change, we mean that we shouldn't warn if the type or size does
906
   change.  We set POLD_ALIGNMENT if an old common symbol in a dynamic
907
   object is overridden by a regular object.  */
908
 
909
bfd_boolean
910
_bfd_elf_merge_symbol (bfd *abfd,
911
                       struct bfd_link_info *info,
912
                       const char *name,
913
                       Elf_Internal_Sym *sym,
914
                       asection **psec,
915
                       bfd_vma *pvalue,
916
                       unsigned int *pold_alignment,
917
                       struct elf_link_hash_entry **sym_hash,
918
                       bfd_boolean *skip,
919
                       bfd_boolean *override,
920
                       bfd_boolean *type_change_ok,
921
                       bfd_boolean *size_change_ok)
922
{
923
  asection *sec, *oldsec;
924
  struct elf_link_hash_entry *h;
925
  struct elf_link_hash_entry *flip;
926
  int bind;
927
  bfd *oldbfd;
928
  bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
929
  bfd_boolean newweak, oldweak, newfunc, oldfunc;
930
  const struct elf_backend_data *bed;
931
 
932
  *skip = FALSE;
933
  *override = FALSE;
934
 
935
  sec = *psec;
936
  bind = ELF_ST_BIND (sym->st_info);
937
 
938
  /* Silently discard TLS symbols from --just-syms.  There's no way to
939
     combine a static TLS block with a new TLS block for this executable.  */
940
  if (ELF_ST_TYPE (sym->st_info) == STT_TLS
941
      && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
942
    {
943
      *skip = TRUE;
944
      return TRUE;
945
    }
946
 
947
  if (! bfd_is_und_section (sec))
948
    h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
949
  else
950
    h = ((struct elf_link_hash_entry *)
951
         bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
952
  if (h == NULL)
953
    return FALSE;
954
  *sym_hash = h;
955
 
956
  bed = get_elf_backend_data (abfd);
957
 
958
  /* This code is for coping with dynamic objects, and is only useful
959
     if we are doing an ELF link.  */
960
  if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
961
    return TRUE;
962
 
963
  /* For merging, we only care about real symbols.  */
964
 
965
  while (h->root.type == bfd_link_hash_indirect
966
         || h->root.type == bfd_link_hash_warning)
967
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
968
 
969
  /* We have to check it for every instance since the first few may be
970
     refereences and not all compilers emit symbol type for undefined
971
     symbols.  */
972
  bfd_elf_link_mark_dynamic_symbol (info, h, sym);
973
 
974
  /* If we just created the symbol, mark it as being an ELF symbol.
975
     Other than that, there is nothing to do--there is no merge issue
976
     with a newly defined symbol--so we just return.  */
977
 
978
  if (h->root.type == bfd_link_hash_new)
979
    {
980
      h->non_elf = 0;
981
      return TRUE;
982
    }
983
 
984
  /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
985
     existing symbol.  */
986
 
987
  switch (h->root.type)
988
    {
989
    default:
990
      oldbfd = NULL;
991
      oldsec = NULL;
992
      break;
993
 
994
    case bfd_link_hash_undefined:
995
    case bfd_link_hash_undefweak:
996
      oldbfd = h->root.u.undef.abfd;
997
      oldsec = NULL;
998
      break;
999
 
1000
    case bfd_link_hash_defined:
1001
    case bfd_link_hash_defweak:
1002
      oldbfd = h->root.u.def.section->owner;
1003
      oldsec = h->root.u.def.section;
1004
      break;
1005
 
1006
    case bfd_link_hash_common:
1007
      oldbfd = h->root.u.c.p->section->owner;
1008
      oldsec = h->root.u.c.p->section;
1009
      break;
1010
    }
1011
 
1012
  /* Differentiate strong and weak symbols.  */
1013
  newweak = bind == STB_WEAK;
1014
  oldweak = (h->root.type == bfd_link_hash_defweak
1015
             || h->root.type == bfd_link_hash_undefweak);
1016
 
1017
  /* In cases involving weak versioned symbols, we may wind up trying
1018
     to merge a symbol with itself.  Catch that here, to avoid the
1019
     confusion that results if we try to override a symbol with
1020
     itself.  The additional tests catch cases like
1021
     _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1022
     dynamic object, which we do want to handle here.  */
1023
  if (abfd == oldbfd
1024
      && (newweak || oldweak)
1025
      && ((abfd->flags & DYNAMIC) == 0
1026
          || !h->def_regular))
1027
    return TRUE;
1028
 
1029
  /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1030
     respectively, is from a dynamic object.  */
1031
 
1032
  newdyn = (abfd->flags & DYNAMIC) != 0;
1033
 
1034
  olddyn = FALSE;
1035
  if (oldbfd != NULL)
1036
    olddyn = (oldbfd->flags & DYNAMIC) != 0;
1037
  else if (oldsec != NULL)
1038
    {
1039
      /* This handles the special SHN_MIPS_{TEXT,DATA} section
1040
         indices used by MIPS ELF.  */
1041
      olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1042
    }
1043
 
1044
  /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1045
     respectively, appear to be a definition rather than reference.  */
1046
 
1047
  newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1048
 
1049
  olddef = (h->root.type != bfd_link_hash_undefined
1050
            && h->root.type != bfd_link_hash_undefweak
1051
            && h->root.type != bfd_link_hash_common);
1052
 
1053
  /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1054
     respectively, appear to be a function.  */
1055
 
1056
  newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1057
             && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1058
 
1059
  oldfunc = (h->type != STT_NOTYPE
1060
             && bed->is_function_type (h->type));
1061
 
1062
  /* When we try to create a default indirect symbol from the dynamic
1063
     definition with the default version, we skip it if its type and
1064
     the type of existing regular definition mismatch.  We only do it
1065
     if the existing regular definition won't be dynamic.  */
1066
  if (pold_alignment == NULL
1067
      && !info->shared
1068
      && !info->export_dynamic
1069
      && !h->ref_dynamic
1070
      && newdyn
1071
      && newdef
1072
      && !olddyn
1073
      && (olddef || h->root.type == bfd_link_hash_common)
1074
      && ELF_ST_TYPE (sym->st_info) != h->type
1075
      && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1076
      && h->type != STT_NOTYPE
1077
      && !(newfunc && oldfunc))
1078
    {
1079
      *skip = TRUE;
1080
      return TRUE;
1081
    }
1082
 
1083
  /* Check TLS symbol.  We don't check undefined symbol introduced by
1084
     "ld -u".  */
1085
  if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
1086
      && ELF_ST_TYPE (sym->st_info) != h->type
1087
      && oldbfd != NULL)
1088
    {
1089
      bfd *ntbfd, *tbfd;
1090
      bfd_boolean ntdef, tdef;
1091
      asection *ntsec, *tsec;
1092
 
1093
      if (h->type == STT_TLS)
1094
        {
1095
          ntbfd = abfd;
1096
          ntsec = sec;
1097
          ntdef = newdef;
1098
          tbfd = oldbfd;
1099
          tsec = oldsec;
1100
          tdef = olddef;
1101
        }
1102
      else
1103
        {
1104
          ntbfd = oldbfd;
1105
          ntsec = oldsec;
1106
          ntdef = olddef;
1107
          tbfd = abfd;
1108
          tsec = sec;
1109
          tdef = newdef;
1110
        }
1111
 
1112
      if (tdef && ntdef)
1113
        (*_bfd_error_handler)
1114
          (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1115
           tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1116
      else if (!tdef && !ntdef)
1117
        (*_bfd_error_handler)
1118
          (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1119
           tbfd, ntbfd, h->root.root.string);
1120
      else if (tdef)
1121
        (*_bfd_error_handler)
1122
          (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1123
           tbfd, tsec, ntbfd, h->root.root.string);
1124
      else
1125
        (*_bfd_error_handler)
1126
          (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1127
           tbfd, ntbfd, ntsec, h->root.root.string);
1128
 
1129
      bfd_set_error (bfd_error_bad_value);
1130
      return FALSE;
1131
    }
1132
 
1133
  /* We need to remember if a symbol has a definition in a dynamic
1134
     object or is weak in all dynamic objects. Internal and hidden
1135
     visibility will make it unavailable to dynamic objects.  */
1136
  if (newdyn && !h->dynamic_def)
1137
    {
1138
      if (!bfd_is_und_section (sec))
1139
        h->dynamic_def = 1;
1140
      else
1141
        {
1142
          /* Check if this symbol is weak in all dynamic objects. If it
1143
             is the first time we see it in a dynamic object, we mark
1144
             if it is weak. Otherwise, we clear it.  */
1145
          if (!h->ref_dynamic)
1146
            {
1147
              if (bind == STB_WEAK)
1148
                h->dynamic_weak = 1;
1149
            }
1150
          else if (bind != STB_WEAK)
1151
            h->dynamic_weak = 0;
1152
        }
1153
    }
1154
 
1155
  /* If the old symbol has non-default visibility, we ignore the new
1156
     definition from a dynamic object.  */
1157
  if (newdyn
1158
      && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1159
      && !bfd_is_und_section (sec))
1160
    {
1161
      *skip = TRUE;
1162
      /* Make sure this symbol is dynamic.  */
1163
      h->ref_dynamic = 1;
1164
      /* A protected symbol has external availability. Make sure it is
1165
         recorded as dynamic.
1166
 
1167
         FIXME: Should we check type and size for protected symbol?  */
1168
      if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1169
        return bfd_elf_link_record_dynamic_symbol (info, h);
1170
      else
1171
        return TRUE;
1172
    }
1173
  else if (!newdyn
1174
           && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1175
           && h->def_dynamic)
1176
    {
1177
      /* If the new symbol with non-default visibility comes from a
1178
         relocatable file and the old definition comes from a dynamic
1179
         object, we remove the old definition.  */
1180
      if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1181
        {
1182
          /* Handle the case where the old dynamic definition is
1183
             default versioned.  We need to copy the symbol info from
1184
             the symbol with default version to the normal one if it
1185
             was referenced before.  */
1186
          if (h->ref_regular)
1187
            {
1188
              struct elf_link_hash_entry *vh = *sym_hash;
1189
 
1190
              vh->root.type = h->root.type;
1191
              h->root.type = bfd_link_hash_indirect;
1192
              (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1193
              /* Protected symbols will override the dynamic definition
1194
                 with default version.  */
1195
              if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1196
                {
1197
                  h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1198
                  vh->dynamic_def = 1;
1199
                  vh->ref_dynamic = 1;
1200
                }
1201
              else
1202
                {
1203
                  h->root.type = vh->root.type;
1204
                  vh->ref_dynamic = 0;
1205
                  /* We have to hide it here since it was made dynamic
1206
                     global with extra bits when the symbol info was
1207
                     copied from the old dynamic definition.  */
1208
                  (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1209
                }
1210
              h = vh;
1211
            }
1212
          else
1213
            h = *sym_hash;
1214
        }
1215
 
1216
      if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1217
          && bfd_is_und_section (sec))
1218
        {
1219
          /* If the new symbol is undefined and the old symbol was
1220
             also undefined before, we need to make sure
1221
             _bfd_generic_link_add_one_symbol doesn't mess
1222
             up the linker hash table undefs list.  Since the old
1223
             definition came from a dynamic object, it is still on the
1224
             undefs list.  */
1225
          h->root.type = bfd_link_hash_undefined;
1226
          h->root.u.undef.abfd = abfd;
1227
        }
1228
      else
1229
        {
1230
          h->root.type = bfd_link_hash_new;
1231
          h->root.u.undef.abfd = NULL;
1232
        }
1233
 
1234
      if (h->def_dynamic)
1235
        {
1236
          h->def_dynamic = 0;
1237
          h->ref_dynamic = 1;
1238
          h->dynamic_def = 1;
1239
        }
1240
      /* FIXME: Should we check type and size for protected symbol?  */
1241
      h->size = 0;
1242
      h->type = 0;
1243
      return TRUE;
1244
    }
1245
 
1246
  if (bind == STB_GNU_UNIQUE)
1247
    h->unique_global = 1;
1248
 
1249
  /* If a new weak symbol definition comes from a regular file and the
1250
     old symbol comes from a dynamic library, we treat the new one as
1251
     strong.  Similarly, an old weak symbol definition from a regular
1252
     file is treated as strong when the new symbol comes from a dynamic
1253
     library.  Further, an old weak symbol from a dynamic library is
1254
     treated as strong if the new symbol is from a dynamic library.
1255
     This reflects the way glibc's ld.so works.
1256
 
1257
     Do this before setting *type_change_ok or *size_change_ok so that
1258
     we warn properly when dynamic library symbols are overridden.  */
1259
 
1260
  if (newdef && !newdyn && olddyn)
1261
    newweak = FALSE;
1262
  if (olddef && newdyn)
1263
    oldweak = FALSE;
1264
 
1265
  /* Allow changes between different types of function symbol.  */
1266
  if (newfunc && oldfunc)
1267
    *type_change_ok = TRUE;
1268
 
1269
  /* It's OK to change the type if either the existing symbol or the
1270
     new symbol is weak.  A type change is also OK if the old symbol
1271
     is undefined and the new symbol is defined.  */
1272
 
1273
  if (oldweak
1274
      || newweak
1275
      || (newdef
1276
          && h->root.type == bfd_link_hash_undefined))
1277
    *type_change_ok = TRUE;
1278
 
1279
  /* It's OK to change the size if either the existing symbol or the
1280
     new symbol is weak, or if the old symbol is undefined.  */
1281
 
1282
  if (*type_change_ok
1283
      || h->root.type == bfd_link_hash_undefined)
1284
    *size_change_ok = TRUE;
1285
 
1286
  /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1287
     symbol, respectively, appears to be a common symbol in a dynamic
1288
     object.  If a symbol appears in an uninitialized section, and is
1289
     not weak, and is not a function, then it may be a common symbol
1290
     which was resolved when the dynamic object was created.  We want
1291
     to treat such symbols specially, because they raise special
1292
     considerations when setting the symbol size: if the symbol
1293
     appears as a common symbol in a regular object, and the size in
1294
     the regular object is larger, we must make sure that we use the
1295
     larger size.  This problematic case can always be avoided in C,
1296
     but it must be handled correctly when using Fortran shared
1297
     libraries.
1298
 
1299
     Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1300
     likewise for OLDDYNCOMMON and OLDDEF.
1301
 
1302
     Note that this test is just a heuristic, and that it is quite
1303
     possible to have an uninitialized symbol in a shared object which
1304
     is really a definition, rather than a common symbol.  This could
1305
     lead to some minor confusion when the symbol really is a common
1306
     symbol in some regular object.  However, I think it will be
1307
     harmless.  */
1308
 
1309
  if (newdyn
1310
      && newdef
1311
      && !newweak
1312
      && (sec->flags & SEC_ALLOC) != 0
1313
      && (sec->flags & SEC_LOAD) == 0
1314
      && sym->st_size > 0
1315
      && !newfunc)
1316
    newdyncommon = TRUE;
1317
  else
1318
    newdyncommon = FALSE;
1319
 
1320
  if (olddyn
1321
      && olddef
1322
      && h->root.type == bfd_link_hash_defined
1323
      && h->def_dynamic
1324
      && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1325
      && (h->root.u.def.section->flags & SEC_LOAD) == 0
1326
      && h->size > 0
1327
      && !oldfunc)
1328
    olddyncommon = TRUE;
1329
  else
1330
    olddyncommon = FALSE;
1331
 
1332
  /* We now know everything about the old and new symbols.  We ask the
1333
     backend to check if we can merge them.  */
1334
  if (bed->merge_symbol
1335
      && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1336
                             pold_alignment, skip, override,
1337
                             type_change_ok, size_change_ok,
1338
                             &newdyn, &newdef, &newdyncommon, &newweak,
1339
                             abfd, &sec,
1340
                             &olddyn, &olddef, &olddyncommon, &oldweak,
1341
                             oldbfd, &oldsec))
1342
    return FALSE;
1343
 
1344
  /* If both the old and the new symbols look like common symbols in a
1345
     dynamic object, set the size of the symbol to the larger of the
1346
     two.  */
1347
 
1348
  if (olddyncommon
1349
      && newdyncommon
1350
      && sym->st_size != h->size)
1351
    {
1352
      /* Since we think we have two common symbols, issue a multiple
1353
         common warning if desired.  Note that we only warn if the
1354
         size is different.  If the size is the same, we simply let
1355
         the old symbol override the new one as normally happens with
1356
         symbols defined in dynamic objects.  */
1357
 
1358
      if (! ((*info->callbacks->multiple_common)
1359
             (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1360
        return FALSE;
1361
 
1362
      if (sym->st_size > h->size)
1363
        h->size = sym->st_size;
1364
 
1365
      *size_change_ok = TRUE;
1366
    }
1367
 
1368
  /* If we are looking at a dynamic object, and we have found a
1369
     definition, we need to see if the symbol was already defined by
1370
     some other object.  If so, we want to use the existing
1371
     definition, and we do not want to report a multiple symbol
1372
     definition error; we do this by clobbering *PSEC to be
1373
     bfd_und_section_ptr.
1374
 
1375
     We treat a common symbol as a definition if the symbol in the
1376
     shared library is a function, since common symbols always
1377
     represent variables; this can cause confusion in principle, but
1378
     any such confusion would seem to indicate an erroneous program or
1379
     shared library.  We also permit a common symbol in a regular
1380
     object to override a weak symbol in a shared object.  */
1381
 
1382
  if (newdyn
1383
      && newdef
1384
      && (olddef
1385
          || (h->root.type == bfd_link_hash_common
1386
              && (newweak || newfunc))))
1387
    {
1388
      *override = TRUE;
1389
      newdef = FALSE;
1390
      newdyncommon = FALSE;
1391
 
1392
      *psec = sec = bfd_und_section_ptr;
1393
      *size_change_ok = TRUE;
1394
 
1395
      /* If we get here when the old symbol is a common symbol, then
1396
         we are explicitly letting it override a weak symbol or
1397
         function in a dynamic object, and we don't want to warn about
1398
         a type change.  If the old symbol is a defined symbol, a type
1399
         change warning may still be appropriate.  */
1400
 
1401
      if (h->root.type == bfd_link_hash_common)
1402
        *type_change_ok = TRUE;
1403
    }
1404
 
1405
  /* Handle the special case of an old common symbol merging with a
1406
     new symbol which looks like a common symbol in a shared object.
1407
     We change *PSEC and *PVALUE to make the new symbol look like a
1408
     common symbol, and let _bfd_generic_link_add_one_symbol do the
1409
     right thing.  */
1410
 
1411
  if (newdyncommon
1412
      && h->root.type == bfd_link_hash_common)
1413
    {
1414
      *override = TRUE;
1415
      newdef = FALSE;
1416
      newdyncommon = FALSE;
1417
      *pvalue = sym->st_size;
1418
      *psec = sec = bed->common_section (oldsec);
1419
      *size_change_ok = TRUE;
1420
    }
1421
 
1422
  /* Skip weak definitions of symbols that are already defined.  */
1423
  if (newdef && olddef && newweak)
1424
    {
1425
      /* Don't skip new non-IR weak syms.  */
1426
      if (!((oldbfd->flags & BFD_PLUGIN) != 0
1427
            && (abfd->flags & BFD_PLUGIN) == 0))
1428
        *skip = TRUE;
1429
 
1430
      /* Merge st_other.  If the symbol already has a dynamic index,
1431
         but visibility says it should not be visible, turn it into a
1432
         local symbol.  */
1433
      elf_merge_st_other (abfd, h, sym, newdef, newdyn);
1434
      if (h->dynindx != -1)
1435
        switch (ELF_ST_VISIBILITY (h->other))
1436
          {
1437
          case STV_INTERNAL:
1438
          case STV_HIDDEN:
1439
            (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1440
            break;
1441
          }
1442
    }
1443
 
1444
  /* If the old symbol is from a dynamic object, and the new symbol is
1445
     a definition which is not from a dynamic object, then the new
1446
     symbol overrides the old symbol.  Symbols from regular files
1447
     always take precedence over symbols from dynamic objects, even if
1448
     they are defined after the dynamic object in the link.
1449
 
1450
     As above, we again permit a common symbol in a regular object to
1451
     override a definition in a shared object if the shared object
1452
     symbol is a function or is weak.  */
1453
 
1454
  flip = NULL;
1455
  if (!newdyn
1456
      && (newdef
1457
          || (bfd_is_com_section (sec)
1458
              && (oldweak || oldfunc)))
1459
      && olddyn
1460
      && olddef
1461
      && h->def_dynamic)
1462
    {
1463
      /* Change the hash table entry to undefined, and let
1464
         _bfd_generic_link_add_one_symbol do the right thing with the
1465
         new definition.  */
1466
 
1467
      h->root.type = bfd_link_hash_undefined;
1468
      h->root.u.undef.abfd = h->root.u.def.section->owner;
1469
      *size_change_ok = TRUE;
1470
 
1471
      olddef = FALSE;
1472
      olddyncommon = FALSE;
1473
 
1474
      /* We again permit a type change when a common symbol may be
1475
         overriding a function.  */
1476
 
1477
      if (bfd_is_com_section (sec))
1478
        {
1479
          if (oldfunc)
1480
            {
1481
              /* If a common symbol overrides a function, make sure
1482
                 that it isn't defined dynamically nor has type
1483
                 function.  */
1484
              h->def_dynamic = 0;
1485
              h->type = STT_NOTYPE;
1486
            }
1487
          *type_change_ok = TRUE;
1488
        }
1489
 
1490
      if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1491
        flip = *sym_hash;
1492
      else
1493
        /* This union may have been set to be non-NULL when this symbol
1494
           was seen in a dynamic object.  We must force the union to be
1495
           NULL, so that it is correct for a regular symbol.  */
1496
        h->verinfo.vertree = NULL;
1497
    }
1498
 
1499
  /* Handle the special case of a new common symbol merging with an
1500
     old symbol that looks like it might be a common symbol defined in
1501
     a shared object.  Note that we have already handled the case in
1502
     which a new common symbol should simply override the definition
1503
     in the shared library.  */
1504
 
1505
  if (! newdyn
1506
      && bfd_is_com_section (sec)
1507
      && olddyncommon)
1508
    {
1509
      /* It would be best if we could set the hash table entry to a
1510
         common symbol, but we don't know what to use for the section
1511
         or the alignment.  */
1512
      if (! ((*info->callbacks->multiple_common)
1513
             (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1514
        return FALSE;
1515
 
1516
      /* If the presumed common symbol in the dynamic object is
1517
         larger, pretend that the new symbol has its size.  */
1518
 
1519
      if (h->size > *pvalue)
1520
        *pvalue = h->size;
1521
 
1522
      /* We need to remember the alignment required by the symbol
1523
         in the dynamic object.  */
1524
      BFD_ASSERT (pold_alignment);
1525
      *pold_alignment = h->root.u.def.section->alignment_power;
1526
 
1527
      olddef = FALSE;
1528
      olddyncommon = FALSE;
1529
 
1530
      h->root.type = bfd_link_hash_undefined;
1531
      h->root.u.undef.abfd = h->root.u.def.section->owner;
1532
 
1533
      *size_change_ok = TRUE;
1534
      *type_change_ok = TRUE;
1535
 
1536
      if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1537
        flip = *sym_hash;
1538
      else
1539
        h->verinfo.vertree = NULL;
1540
    }
1541
 
1542
  if (flip != NULL)
1543
    {
1544
      /* Handle the case where we had a versioned symbol in a dynamic
1545
         library and now find a definition in a normal object.  In this
1546
         case, we make the versioned symbol point to the normal one.  */
1547
      flip->root.type = h->root.type;
1548
      flip->root.u.undef.abfd = h->root.u.undef.abfd;
1549
      h->root.type = bfd_link_hash_indirect;
1550
      h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1551
      (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1552
      if (h->def_dynamic)
1553
        {
1554
          h->def_dynamic = 0;
1555
          flip->ref_dynamic = 1;
1556
        }
1557
    }
1558
 
1559
  return TRUE;
1560
}
1561
 
1562
/* This function is called to create an indirect symbol from the
1563
   default for the symbol with the default version if needed. The
1564
   symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE.  We
1565
   set DYNSYM if the new indirect symbol is dynamic.  */
1566
 
1567
static bfd_boolean
1568
_bfd_elf_add_default_symbol (bfd *abfd,
1569
                             struct bfd_link_info *info,
1570
                             struct elf_link_hash_entry *h,
1571
                             const char *name,
1572
                             Elf_Internal_Sym *sym,
1573
                             asection **psec,
1574
                             bfd_vma *value,
1575
                             bfd_boolean *dynsym,
1576
                             bfd_boolean override)
1577
{
1578
  bfd_boolean type_change_ok;
1579
  bfd_boolean size_change_ok;
1580
  bfd_boolean skip;
1581
  char *shortname;
1582
  struct elf_link_hash_entry *hi;
1583
  struct bfd_link_hash_entry *bh;
1584
  const struct elf_backend_data *bed;
1585
  bfd_boolean collect;
1586
  bfd_boolean dynamic;
1587
  char *p;
1588
  size_t len, shortlen;
1589
  asection *sec;
1590
 
1591
  /* If this symbol has a version, and it is the default version, we
1592
     create an indirect symbol from the default name to the fully
1593
     decorated name.  This will cause external references which do not
1594
     specify a version to be bound to this version of the symbol.  */
1595
  p = strchr (name, ELF_VER_CHR);
1596
  if (p == NULL || p[1] != ELF_VER_CHR)
1597
    return TRUE;
1598
 
1599
  if (override)
1600
    {
1601
      /* We are overridden by an old definition. We need to check if we
1602
         need to create the indirect symbol from the default name.  */
1603
      hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1604
                                 FALSE, FALSE);
1605
      BFD_ASSERT (hi != NULL);
1606
      if (hi == h)
1607
        return TRUE;
1608
      while (hi->root.type == bfd_link_hash_indirect
1609
             || hi->root.type == bfd_link_hash_warning)
1610
        {
1611
          hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1612
          if (hi == h)
1613
            return TRUE;
1614
        }
1615
    }
1616
 
1617
  bed = get_elf_backend_data (abfd);
1618
  collect = bed->collect;
1619
  dynamic = (abfd->flags & DYNAMIC) != 0;
1620
 
1621
  shortlen = p - name;
1622
  shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1623
  if (shortname == NULL)
1624
    return FALSE;
1625
  memcpy (shortname, name, shortlen);
1626
  shortname[shortlen] = '\0';
1627
 
1628
  /* We are going to create a new symbol.  Merge it with any existing
1629
     symbol with this name.  For the purposes of the merge, act as
1630
     though we were defining the symbol we just defined, although we
1631
     actually going to define an indirect symbol.  */
1632
  type_change_ok = FALSE;
1633
  size_change_ok = FALSE;
1634
  sec = *psec;
1635
  if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1636
                              NULL, &hi, &skip, &override,
1637
                              &type_change_ok, &size_change_ok))
1638
    return FALSE;
1639
 
1640
  if (skip)
1641
    goto nondefault;
1642
 
1643
  if (! override)
1644
    {
1645
      bh = &hi->root;
1646
      if (! (_bfd_generic_link_add_one_symbol
1647
             (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1648
              0, name, FALSE, collect, &bh)))
1649
        return FALSE;
1650
      hi = (struct elf_link_hash_entry *) bh;
1651
    }
1652
  else
1653
    {
1654
      /* In this case the symbol named SHORTNAME is overriding the
1655
         indirect symbol we want to add.  We were planning on making
1656
         SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
1657
         is the name without a version.  NAME is the fully versioned
1658
         name, and it is the default version.
1659
 
1660
         Overriding means that we already saw a definition for the
1661
         symbol SHORTNAME in a regular object, and it is overriding
1662
         the symbol defined in the dynamic object.
1663
 
1664
         When this happens, we actually want to change NAME, the
1665
         symbol we just added, to refer to SHORTNAME.  This will cause
1666
         references to NAME in the shared object to become references
1667
         to SHORTNAME in the regular object.  This is what we expect
1668
         when we override a function in a shared object: that the
1669
         references in the shared object will be mapped to the
1670
         definition in the regular object.  */
1671
 
1672
      while (hi->root.type == bfd_link_hash_indirect
1673
             || hi->root.type == bfd_link_hash_warning)
1674
        hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1675
 
1676
      h->root.type = bfd_link_hash_indirect;
1677
      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1678
      if (h->def_dynamic)
1679
        {
1680
          h->def_dynamic = 0;
1681
          hi->ref_dynamic = 1;
1682
          if (hi->ref_regular
1683
              || hi->def_regular)
1684
            {
1685
              if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1686
                return FALSE;
1687
            }
1688
        }
1689
 
1690
      /* Now set HI to H, so that the following code will set the
1691
         other fields correctly.  */
1692
      hi = h;
1693
    }
1694
 
1695
  /* Check if HI is a warning symbol.  */
1696
  if (hi->root.type == bfd_link_hash_warning)
1697
    hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1698
 
1699
  /* If there is a duplicate definition somewhere, then HI may not
1700
     point to an indirect symbol.  We will have reported an error to
1701
     the user in that case.  */
1702
 
1703
  if (hi->root.type == bfd_link_hash_indirect)
1704
    {
1705
      struct elf_link_hash_entry *ht;
1706
 
1707
      ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1708
      (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1709
 
1710
      /* See if the new flags lead us to realize that the symbol must
1711
         be dynamic.  */
1712
      if (! *dynsym)
1713
        {
1714
          if (! dynamic)
1715
            {
1716
              if (! info->executable
1717
                  || hi->ref_dynamic)
1718
                *dynsym = TRUE;
1719
            }
1720
          else
1721
            {
1722
              if (hi->ref_regular)
1723
                *dynsym = TRUE;
1724
            }
1725
        }
1726
    }
1727
 
1728
  /* We also need to define an indirection from the nondefault version
1729
     of the symbol.  */
1730
 
1731
nondefault:
1732
  len = strlen (name);
1733
  shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1734
  if (shortname == NULL)
1735
    return FALSE;
1736
  memcpy (shortname, name, shortlen);
1737
  memcpy (shortname + shortlen, p + 1, len - shortlen);
1738
 
1739
  /* Once again, merge with any existing symbol.  */
1740
  type_change_ok = FALSE;
1741
  size_change_ok = FALSE;
1742
  sec = *psec;
1743
  if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1744
                              NULL, &hi, &skip, &override,
1745
                              &type_change_ok, &size_change_ok))
1746
    return FALSE;
1747
 
1748
  if (skip)
1749
    return TRUE;
1750
 
1751
  if (override)
1752
    {
1753
      /* Here SHORTNAME is a versioned name, so we don't expect to see
1754
         the type of override we do in the case above unless it is
1755
         overridden by a versioned definition.  */
1756
      if (hi->root.type != bfd_link_hash_defined
1757
          && hi->root.type != bfd_link_hash_defweak)
1758
        (*_bfd_error_handler)
1759
          (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1760
           abfd, shortname);
1761
    }
1762
  else
1763
    {
1764
      bh = &hi->root;
1765
      if (! (_bfd_generic_link_add_one_symbol
1766
             (info, abfd, shortname, BSF_INDIRECT,
1767
              bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1768
        return FALSE;
1769
      hi = (struct elf_link_hash_entry *) bh;
1770
 
1771
      /* If there is a duplicate definition somewhere, then HI may not
1772
         point to an indirect symbol.  We will have reported an error
1773
         to the user in that case.  */
1774
 
1775
      if (hi->root.type == bfd_link_hash_indirect)
1776
        {
1777
          (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1778
 
1779
          /* See if the new flags lead us to realize that the symbol
1780
             must be dynamic.  */
1781
          if (! *dynsym)
1782
            {
1783
              if (! dynamic)
1784
                {
1785
                  if (! info->executable
1786
                      || hi->ref_dynamic)
1787
                    *dynsym = TRUE;
1788
                }
1789
              else
1790
                {
1791
                  if (hi->ref_regular)
1792
                    *dynsym = TRUE;
1793
                }
1794
            }
1795
        }
1796
    }
1797
 
1798
  return TRUE;
1799
}
1800
 
1801
/* This routine is used to export all defined symbols into the dynamic
1802
   symbol table.  It is called via elf_link_hash_traverse.  */
1803
 
1804
static bfd_boolean
1805
_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1806
{
1807
  struct elf_info_failed *eif = (struct elf_info_failed *) data;
1808
 
1809
  /* Ignore indirect symbols.  These are added by the versioning code.  */
1810
  if (h->root.type == bfd_link_hash_indirect)
1811
    return TRUE;
1812
 
1813 148 khays
  /* Ignore this if we won't export it.  */
1814
  if (!eif->info->export_dynamic && !h->dynamic)
1815
    return TRUE;
1816 14 khays
 
1817
  if (h->dynindx == -1
1818
      && (h->def_regular
1819
          || h->ref_regular))
1820
    {
1821
      bfd_boolean hide;
1822
 
1823
      if (eif->verdefs == NULL
1824
          || (bfd_find_version_for_sym (eif->verdefs, h->root.root.string, &hide)
1825
              && !hide))
1826
        {
1827
          if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1828
            {
1829
              eif->failed = TRUE;
1830
              return FALSE;
1831
            }
1832
        }
1833
    }
1834
 
1835
  return TRUE;
1836
}
1837
 
1838
/* Look through the symbols which are defined in other shared
1839
   libraries and referenced here.  Update the list of version
1840
   dependencies.  This will be put into the .gnu.version_r section.
1841
   This function is called via elf_link_hash_traverse.  */
1842
 
1843
static bfd_boolean
1844
_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1845
                                         void *data)
1846
{
1847
  struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
1848
  Elf_Internal_Verneed *t;
1849
  Elf_Internal_Vernaux *a;
1850
  bfd_size_type amt;
1851
 
1852
  /* We only care about symbols defined in shared objects with version
1853
     information.  */
1854
  if (!h->def_dynamic
1855
      || h->def_regular
1856
      || h->dynindx == -1
1857
      || h->verinfo.verdef == NULL)
1858
    return TRUE;
1859
 
1860
  /* See if we already know about this version.  */
1861
  for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1862
       t != NULL;
1863
       t = t->vn_nextref)
1864
    {
1865
      if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1866
        continue;
1867
 
1868
      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1869
        if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1870
          return TRUE;
1871
 
1872
      break;
1873
    }
1874
 
1875
  /* This is a new version.  Add it to tree we are building.  */
1876
 
1877
  if (t == NULL)
1878
    {
1879
      amt = sizeof *t;
1880
      t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
1881
      if (t == NULL)
1882
        {
1883
          rinfo->failed = TRUE;
1884
          return FALSE;
1885
        }
1886
 
1887
      t->vn_bfd = h->verinfo.verdef->vd_bfd;
1888
      t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
1889
      elf_tdata (rinfo->info->output_bfd)->verref = t;
1890
    }
1891
 
1892
  amt = sizeof *a;
1893
  a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
1894
  if (a == NULL)
1895
    {
1896
      rinfo->failed = TRUE;
1897
      return FALSE;
1898
    }
1899
 
1900
  /* Note that we are copying a string pointer here, and testing it
1901
     above.  If bfd_elf_string_from_elf_section is ever changed to
1902
     discard the string data when low in memory, this will have to be
1903
     fixed.  */
1904
  a->vna_nodename = h->verinfo.verdef->vd_nodename;
1905
 
1906
  a->vna_flags = h->verinfo.verdef->vd_flags;
1907
  a->vna_nextptr = t->vn_auxptr;
1908
 
1909
  h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1910
  ++rinfo->vers;
1911
 
1912
  a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1913
 
1914
  t->vn_auxptr = a;
1915
 
1916
  return TRUE;
1917
}
1918
 
1919
/* Figure out appropriate versions for all the symbols.  We may not
1920
   have the version number script until we have read all of the input
1921
   files, so until that point we don't know which symbols should be
1922
   local.  This function is called via elf_link_hash_traverse.  */
1923
 
1924
static bfd_boolean
1925
_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1926
{
1927
  struct elf_info_failed *sinfo;
1928
  struct bfd_link_info *info;
1929
  const struct elf_backend_data *bed;
1930
  struct elf_info_failed eif;
1931
  char *p;
1932
  bfd_size_type amt;
1933
 
1934
  sinfo = (struct elf_info_failed *) data;
1935
  info = sinfo->info;
1936
 
1937
  /* Fix the symbol flags.  */
1938
  eif.failed = FALSE;
1939
  eif.info = info;
1940
  if (! _bfd_elf_fix_symbol_flags (h, &eif))
1941
    {
1942
      if (eif.failed)
1943
        sinfo->failed = TRUE;
1944
      return FALSE;
1945
    }
1946
 
1947
  /* We only need version numbers for symbols defined in regular
1948
     objects.  */
1949
  if (!h->def_regular)
1950
    return TRUE;
1951
 
1952
  bed = get_elf_backend_data (info->output_bfd);
1953
  p = strchr (h->root.root.string, ELF_VER_CHR);
1954
  if (p != NULL && h->verinfo.vertree == NULL)
1955
    {
1956
      struct bfd_elf_version_tree *t;
1957
      bfd_boolean hidden;
1958
 
1959
      hidden = TRUE;
1960
 
1961
      /* There are two consecutive ELF_VER_CHR characters if this is
1962
         not a hidden symbol.  */
1963
      ++p;
1964
      if (*p == ELF_VER_CHR)
1965
        {
1966
          hidden = FALSE;
1967
          ++p;
1968
        }
1969
 
1970
      /* If there is no version string, we can just return out.  */
1971
      if (*p == '\0')
1972
        {
1973
          if (hidden)
1974
            h->hidden = 1;
1975
          return TRUE;
1976
        }
1977
 
1978
      /* Look for the version.  If we find it, it is no longer weak.  */
1979
      for (t = sinfo->verdefs; t != NULL; t = t->next)
1980
        {
1981
          if (strcmp (t->name, p) == 0)
1982
            {
1983
              size_t len;
1984
              char *alc;
1985
              struct bfd_elf_version_expr *d;
1986
 
1987
              len = p - h->root.root.string;
1988
              alc = (char *) bfd_malloc (len);
1989
              if (alc == NULL)
1990
                {
1991
                  sinfo->failed = TRUE;
1992
                  return FALSE;
1993
                }
1994
              memcpy (alc, h->root.root.string, len - 1);
1995
              alc[len - 1] = '\0';
1996
              if (alc[len - 2] == ELF_VER_CHR)
1997
                alc[len - 2] = '\0';
1998
 
1999
              h->verinfo.vertree = t;
2000
              t->used = TRUE;
2001
              d = NULL;
2002
 
2003
              if (t->globals.list != NULL)
2004
                d = (*t->match) (&t->globals, NULL, alc);
2005
 
2006
              /* See if there is anything to force this symbol to
2007
                 local scope.  */
2008
              if (d == NULL && t->locals.list != NULL)
2009
                {
2010
                  d = (*t->match) (&t->locals, NULL, alc);
2011
                  if (d != NULL
2012
                      && h->dynindx != -1
2013
                      && ! info->export_dynamic)
2014
                    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2015
                }
2016
 
2017
              free (alc);
2018
              break;
2019
            }
2020
        }
2021
 
2022
      /* If we are building an application, we need to create a
2023
         version node for this version.  */
2024
      if (t == NULL && info->executable)
2025
        {
2026
          struct bfd_elf_version_tree **pp;
2027
          int version_index;
2028
 
2029
          /* If we aren't going to export this symbol, we don't need
2030
             to worry about it.  */
2031
          if (h->dynindx == -1)
2032
            return TRUE;
2033
 
2034
          amt = sizeof *t;
2035
          t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
2036
          if (t == NULL)
2037
            {
2038
              sinfo->failed = TRUE;
2039
              return FALSE;
2040
            }
2041
 
2042
          t->name = p;
2043
          t->name_indx = (unsigned int) -1;
2044
          t->used = TRUE;
2045
 
2046
          version_index = 1;
2047
          /* Don't count anonymous version tag.  */
2048
          if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
2049
            version_index = 0;
2050
          for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
2051
            ++version_index;
2052
          t->vernum = version_index;
2053
 
2054
          *pp = t;
2055
 
2056
          h->verinfo.vertree = t;
2057
        }
2058
      else if (t == NULL)
2059
        {
2060
          /* We could not find the version for a symbol when
2061
             generating a shared archive.  Return an error.  */
2062
          (*_bfd_error_handler)
2063
            (_("%B: version node not found for symbol %s"),
2064
             info->output_bfd, h->root.root.string);
2065
          bfd_set_error (bfd_error_bad_value);
2066
          sinfo->failed = TRUE;
2067
          return FALSE;
2068
        }
2069
 
2070
      if (hidden)
2071
        h->hidden = 1;
2072
    }
2073
 
2074
  /* If we don't have a version for this symbol, see if we can find
2075
     something.  */
2076
  if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
2077
    {
2078
      bfd_boolean hide;
2079
 
2080
      h->verinfo.vertree = bfd_find_version_for_sym (sinfo->verdefs,
2081
                                                 h->root.root.string, &hide);
2082
      if (h->verinfo.vertree != NULL && hide)
2083
        (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2084
    }
2085
 
2086
  return TRUE;
2087
}
2088
 
2089
/* Read and swap the relocs from the section indicated by SHDR.  This
2090
   may be either a REL or a RELA section.  The relocations are
2091
   translated into RELA relocations and stored in INTERNAL_RELOCS,
2092
   which should have already been allocated to contain enough space.
2093
   The EXTERNAL_RELOCS are a buffer where the external form of the
2094
   relocations should be stored.
2095
 
2096
   Returns FALSE if something goes wrong.  */
2097
 
2098
static bfd_boolean
2099
elf_link_read_relocs_from_section (bfd *abfd,
2100
                                   asection *sec,
2101
                                   Elf_Internal_Shdr *shdr,
2102
                                   void *external_relocs,
2103
                                   Elf_Internal_Rela *internal_relocs)
2104
{
2105
  const struct elf_backend_data *bed;
2106
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2107
  const bfd_byte *erela;
2108
  const bfd_byte *erelaend;
2109
  Elf_Internal_Rela *irela;
2110
  Elf_Internal_Shdr *symtab_hdr;
2111
  size_t nsyms;
2112
 
2113
  /* Position ourselves at the start of the section.  */
2114
  if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2115
    return FALSE;
2116
 
2117
  /* Read the relocations.  */
2118
  if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2119
    return FALSE;
2120
 
2121
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2122
  nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2123
 
2124
  bed = get_elf_backend_data (abfd);
2125
 
2126
  /* Convert the external relocations to the internal format.  */
2127
  if (shdr->sh_entsize == bed->s->sizeof_rel)
2128
    swap_in = bed->s->swap_reloc_in;
2129
  else if (shdr->sh_entsize == bed->s->sizeof_rela)
2130
    swap_in = bed->s->swap_reloca_in;
2131
  else
2132
    {
2133
      bfd_set_error (bfd_error_wrong_format);
2134
      return FALSE;
2135
    }
2136
 
2137
  erela = (const bfd_byte *) external_relocs;
2138
  erelaend = erela + shdr->sh_size;
2139
  irela = internal_relocs;
2140
  while (erela < erelaend)
2141
    {
2142
      bfd_vma r_symndx;
2143
 
2144
      (*swap_in) (abfd, erela, irela);
2145
      r_symndx = ELF32_R_SYM (irela->r_info);
2146
      if (bed->s->arch_size == 64)
2147
        r_symndx >>= 24;
2148
      if (nsyms > 0)
2149
        {
2150
          if ((size_t) r_symndx >= nsyms)
2151
            {
2152
              (*_bfd_error_handler)
2153
                (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2154
                   " for offset 0x%lx in section `%A'"),
2155
                 abfd, sec,
2156
                 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2157
              bfd_set_error (bfd_error_bad_value);
2158
              return FALSE;
2159
            }
2160
        }
2161
      else if (r_symndx != STN_UNDEF)
2162
        {
2163
          (*_bfd_error_handler)
2164
            (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2165
               " when the object file has no symbol table"),
2166
             abfd, sec,
2167
             (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2168
          bfd_set_error (bfd_error_bad_value);
2169
          return FALSE;
2170
        }
2171
      irela += bed->s->int_rels_per_ext_rel;
2172
      erela += shdr->sh_entsize;
2173
    }
2174
 
2175
  return TRUE;
2176
}
2177
 
2178
/* Read and swap the relocs for a section O.  They may have been
2179
   cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2180
   not NULL, they are used as buffers to read into.  They are known to
2181
   be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
2182
   the return value is allocated using either malloc or bfd_alloc,
2183
   according to the KEEP_MEMORY argument.  If O has two relocation
2184
   sections (both REL and RELA relocations), then the REL_HDR
2185
   relocations will appear first in INTERNAL_RELOCS, followed by the
2186
   RELA_HDR relocations.  */
2187
 
2188
Elf_Internal_Rela *
2189
_bfd_elf_link_read_relocs (bfd *abfd,
2190
                           asection *o,
2191
                           void *external_relocs,
2192
                           Elf_Internal_Rela *internal_relocs,
2193
                           bfd_boolean keep_memory)
2194
{
2195
  void *alloc1 = NULL;
2196
  Elf_Internal_Rela *alloc2 = NULL;
2197
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2198
  struct bfd_elf_section_data *esdo = elf_section_data (o);
2199
  Elf_Internal_Rela *internal_rela_relocs;
2200
 
2201
  if (esdo->relocs != NULL)
2202
    return esdo->relocs;
2203
 
2204
  if (o->reloc_count == 0)
2205
    return NULL;
2206
 
2207
  if (internal_relocs == NULL)
2208
    {
2209
      bfd_size_type size;
2210
 
2211
      size = o->reloc_count;
2212
      size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2213
      if (keep_memory)
2214
        internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2215
      else
2216
        internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2217
      if (internal_relocs == NULL)
2218
        goto error_return;
2219
    }
2220
 
2221
  if (external_relocs == NULL)
2222
    {
2223
      bfd_size_type size = 0;
2224
 
2225
      if (esdo->rel.hdr)
2226
        size += esdo->rel.hdr->sh_size;
2227
      if (esdo->rela.hdr)
2228
        size += esdo->rela.hdr->sh_size;
2229
 
2230
      alloc1 = bfd_malloc (size);
2231
      if (alloc1 == NULL)
2232
        goto error_return;
2233
      external_relocs = alloc1;
2234
    }
2235
 
2236
  internal_rela_relocs = internal_relocs;
2237
  if (esdo->rel.hdr)
2238
    {
2239
      if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2240
                                              external_relocs,
2241
                                              internal_relocs))
2242
        goto error_return;
2243
      external_relocs = (((bfd_byte *) external_relocs)
2244
                         + esdo->rel.hdr->sh_size);
2245
      internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2246
                               * bed->s->int_rels_per_ext_rel);
2247
    }
2248
 
2249
  if (esdo->rela.hdr
2250
      && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2251
                                              external_relocs,
2252
                                              internal_rela_relocs)))
2253
    goto error_return;
2254
 
2255
  /* Cache the results for next time, if we can.  */
2256
  if (keep_memory)
2257
    esdo->relocs = internal_relocs;
2258
 
2259
  if (alloc1 != NULL)
2260
    free (alloc1);
2261
 
2262
  /* Don't free alloc2, since if it was allocated we are passing it
2263
     back (under the name of internal_relocs).  */
2264
 
2265
  return internal_relocs;
2266
 
2267
 error_return:
2268
  if (alloc1 != NULL)
2269
    free (alloc1);
2270
  if (alloc2 != NULL)
2271
    {
2272
      if (keep_memory)
2273
        bfd_release (abfd, alloc2);
2274
      else
2275
        free (alloc2);
2276
    }
2277
  return NULL;
2278
}
2279
 
2280
/* Compute the size of, and allocate space for, REL_HDR which is the
2281
   section header for a section containing relocations for O.  */
2282
 
2283
static bfd_boolean
2284
_bfd_elf_link_size_reloc_section (bfd *abfd,
2285
                                  struct bfd_elf_section_reloc_data *reldata)
2286
{
2287
  Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2288
 
2289
  /* That allows us to calculate the size of the section.  */
2290
  rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2291
 
2292
  /* The contents field must last into write_object_contents, so we
2293
     allocate it with bfd_alloc rather than malloc.  Also since we
2294
     cannot be sure that the contents will actually be filled in,
2295
     we zero the allocated space.  */
2296
  rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2297
  if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2298
    return FALSE;
2299
 
2300
  if (reldata->hashes == NULL && reldata->count)
2301
    {
2302
      struct elf_link_hash_entry **p;
2303
 
2304
      p = (struct elf_link_hash_entry **)
2305
          bfd_zmalloc (reldata->count * sizeof (struct elf_link_hash_entry *));
2306
      if (p == NULL)
2307
        return FALSE;
2308
 
2309
      reldata->hashes = p;
2310
    }
2311
 
2312
  return TRUE;
2313
}
2314
 
2315
/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2316
   originated from the section given by INPUT_REL_HDR) to the
2317
   OUTPUT_BFD.  */
2318
 
2319
bfd_boolean
2320
_bfd_elf_link_output_relocs (bfd *output_bfd,
2321
                             asection *input_section,
2322
                             Elf_Internal_Shdr *input_rel_hdr,
2323
                             Elf_Internal_Rela *internal_relocs,
2324
                             struct elf_link_hash_entry **rel_hash
2325
                               ATTRIBUTE_UNUSED)
2326
{
2327
  Elf_Internal_Rela *irela;
2328
  Elf_Internal_Rela *irelaend;
2329
  bfd_byte *erel;
2330
  struct bfd_elf_section_reloc_data *output_reldata;
2331
  asection *output_section;
2332
  const struct elf_backend_data *bed;
2333
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2334
  struct bfd_elf_section_data *esdo;
2335
 
2336
  output_section = input_section->output_section;
2337
 
2338
  bed = get_elf_backend_data (output_bfd);
2339
  esdo = elf_section_data (output_section);
2340
  if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2341
    {
2342
      output_reldata = &esdo->rel;
2343
      swap_out = bed->s->swap_reloc_out;
2344
    }
2345
  else if (esdo->rela.hdr
2346
           && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2347
    {
2348
      output_reldata = &esdo->rela;
2349
      swap_out = bed->s->swap_reloca_out;
2350
    }
2351
  else
2352
    {
2353
      (*_bfd_error_handler)
2354
        (_("%B: relocation size mismatch in %B section %A"),
2355
         output_bfd, input_section->owner, input_section);
2356
      bfd_set_error (bfd_error_wrong_format);
2357
      return FALSE;
2358
    }
2359
 
2360
  erel = output_reldata->hdr->contents;
2361
  erel += output_reldata->count * input_rel_hdr->sh_entsize;
2362
  irela = internal_relocs;
2363
  irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2364
                      * bed->s->int_rels_per_ext_rel);
2365
  while (irela < irelaend)
2366
    {
2367
      (*swap_out) (output_bfd, irela, erel);
2368
      irela += bed->s->int_rels_per_ext_rel;
2369
      erel += input_rel_hdr->sh_entsize;
2370
    }
2371
 
2372
  /* Bump the counter, so that we know where to add the next set of
2373
     relocations.  */
2374
  output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2375
 
2376
  return TRUE;
2377
}
2378
 
2379
/* Make weak undefined symbols in PIE dynamic.  */
2380
 
2381
bfd_boolean
2382
_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2383
                                 struct elf_link_hash_entry *h)
2384
{
2385
  if (info->pie
2386
      && h->dynindx == -1
2387
      && h->root.type == bfd_link_hash_undefweak)
2388
    return bfd_elf_link_record_dynamic_symbol (info, h);
2389
 
2390
  return TRUE;
2391
}
2392
 
2393
/* Fix up the flags for a symbol.  This handles various cases which
2394
   can only be fixed after all the input files are seen.  This is
2395
   currently called by both adjust_dynamic_symbol and
2396
   assign_sym_version, which is unnecessary but perhaps more robust in
2397
   the face of future changes.  */
2398
 
2399
static bfd_boolean
2400
_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2401
                           struct elf_info_failed *eif)
2402
{
2403
  const struct elf_backend_data *bed;
2404
 
2405
  /* If this symbol was mentioned in a non-ELF file, try to set
2406
     DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
2407
     permit a non-ELF file to correctly refer to a symbol defined in
2408
     an ELF dynamic object.  */
2409
  if (h->non_elf)
2410
    {
2411
      while (h->root.type == bfd_link_hash_indirect)
2412
        h = (struct elf_link_hash_entry *) h->root.u.i.link;
2413
 
2414
      if (h->root.type != bfd_link_hash_defined
2415
          && h->root.type != bfd_link_hash_defweak)
2416
        {
2417
          h->ref_regular = 1;
2418
          h->ref_regular_nonweak = 1;
2419
        }
2420
      else
2421
        {
2422
          if (h->root.u.def.section->owner != NULL
2423
              && (bfd_get_flavour (h->root.u.def.section->owner)
2424
                  == bfd_target_elf_flavour))
2425
            {
2426
              h->ref_regular = 1;
2427
              h->ref_regular_nonweak = 1;
2428
            }
2429
          else
2430
            h->def_regular = 1;
2431
        }
2432
 
2433
      if (h->dynindx == -1
2434
          && (h->def_dynamic
2435
              || h->ref_dynamic))
2436
        {
2437
          if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2438
            {
2439
              eif->failed = TRUE;
2440
              return FALSE;
2441
            }
2442
        }
2443
    }
2444
  else
2445
    {
2446
      /* Unfortunately, NON_ELF is only correct if the symbol
2447
         was first seen in a non-ELF file.  Fortunately, if the symbol
2448
         was first seen in an ELF file, we're probably OK unless the
2449
         symbol was defined in a non-ELF file.  Catch that case here.
2450
         FIXME: We're still in trouble if the symbol was first seen in
2451
         a dynamic object, and then later in a non-ELF regular object.  */
2452
      if ((h->root.type == bfd_link_hash_defined
2453
           || h->root.type == bfd_link_hash_defweak)
2454
          && !h->def_regular
2455
          && (h->root.u.def.section->owner != NULL
2456
              ? (bfd_get_flavour (h->root.u.def.section->owner)
2457
                 != bfd_target_elf_flavour)
2458
              : (bfd_is_abs_section (h->root.u.def.section)
2459
                 && !h->def_dynamic)))
2460
        h->def_regular = 1;
2461
    }
2462
 
2463
  /* Backend specific symbol fixup.  */
2464
  bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2465
  if (bed->elf_backend_fixup_symbol
2466
      && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2467
    return FALSE;
2468
 
2469
  /* If this is a final link, and the symbol was defined as a common
2470
     symbol in a regular object file, and there was no definition in
2471
     any dynamic object, then the linker will have allocated space for
2472
     the symbol in a common section but the DEF_REGULAR
2473
     flag will not have been set.  */
2474
  if (h->root.type == bfd_link_hash_defined
2475
      && !h->def_regular
2476
      && h->ref_regular
2477
      && !h->def_dynamic
2478
      && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2479
    h->def_regular = 1;
2480
 
2481
  /* If -Bsymbolic was used (which means to bind references to global
2482
     symbols to the definition within the shared object), and this
2483
     symbol was defined in a regular object, then it actually doesn't
2484
     need a PLT entry.  Likewise, if the symbol has non-default
2485
     visibility.  If the symbol has hidden or internal visibility, we
2486
     will force it local.  */
2487
  if (h->needs_plt
2488
      && eif->info->shared
2489
      && is_elf_hash_table (eif->info->hash)
2490
      && (SYMBOLIC_BIND (eif->info, h)
2491
          || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2492
      && h->def_regular)
2493
    {
2494
      bfd_boolean force_local;
2495
 
2496
      force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2497
                     || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2498
      (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2499
    }
2500
 
2501
  /* If a weak undefined symbol has non-default visibility, we also
2502
     hide it from the dynamic linker.  */
2503
  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2504
      && h->root.type == bfd_link_hash_undefweak)
2505
    (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2506
 
2507
  /* If this is a weak defined symbol in a dynamic object, and we know
2508
     the real definition in the dynamic object, copy interesting flags
2509
     over to the real definition.  */
2510
  if (h->u.weakdef != NULL)
2511
    {
2512
      struct elf_link_hash_entry *weakdef;
2513
 
2514
      weakdef = h->u.weakdef;
2515
      if (h->root.type == bfd_link_hash_indirect)
2516
        h = (struct elf_link_hash_entry *) h->root.u.i.link;
2517
 
2518
      BFD_ASSERT (h->root.type == bfd_link_hash_defined
2519
                  || h->root.type == bfd_link_hash_defweak);
2520
      BFD_ASSERT (weakdef->def_dynamic);
2521
 
2522
      /* If the real definition is defined by a regular object file,
2523
         don't do anything special.  See the longer description in
2524
         _bfd_elf_adjust_dynamic_symbol, below.  */
2525
      if (weakdef->def_regular)
2526
        h->u.weakdef = NULL;
2527
      else
2528
        {
2529
          BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2530
                      || weakdef->root.type == bfd_link_hash_defweak);
2531
          (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2532
        }
2533
    }
2534
 
2535
  return TRUE;
2536
}
2537
 
2538
/* Make the backend pick a good value for a dynamic symbol.  This is
2539
   called via elf_link_hash_traverse, and also calls itself
2540
   recursively.  */
2541
 
2542
static bfd_boolean
2543
_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2544
{
2545
  struct elf_info_failed *eif = (struct elf_info_failed *) data;
2546
  bfd *dynobj;
2547
  const struct elf_backend_data *bed;
2548
 
2549
  if (! is_elf_hash_table (eif->info->hash))
2550
    return FALSE;
2551
 
2552
  /* Ignore indirect symbols.  These are added by the versioning code.  */
2553
  if (h->root.type == bfd_link_hash_indirect)
2554
    return TRUE;
2555
 
2556
  /* Fix the symbol flags.  */
2557
  if (! _bfd_elf_fix_symbol_flags (h, eif))
2558
    return FALSE;
2559
 
2560
  /* If this symbol does not require a PLT entry, and it is not
2561
     defined by a dynamic object, or is not referenced by a regular
2562
     object, ignore it.  We do have to handle a weak defined symbol,
2563
     even if no regular object refers to it, if we decided to add it
2564
     to the dynamic symbol table.  FIXME: Do we normally need to worry
2565
     about symbols which are defined by one dynamic object and
2566
     referenced by another one?  */
2567
  if (!h->needs_plt
2568
      && h->type != STT_GNU_IFUNC
2569
      && (h->def_regular
2570
          || !h->def_dynamic
2571
          || (!h->ref_regular
2572
              && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2573
    {
2574
      h->plt = elf_hash_table (eif->info)->init_plt_offset;
2575
      return TRUE;
2576
    }
2577
 
2578
  /* If we've already adjusted this symbol, don't do it again.  This
2579
     can happen via a recursive call.  */
2580
  if (h->dynamic_adjusted)
2581
    return TRUE;
2582
 
2583
  /* Don't look at this symbol again.  Note that we must set this
2584
     after checking the above conditions, because we may look at a
2585
     symbol once, decide not to do anything, and then get called
2586
     recursively later after REF_REGULAR is set below.  */
2587
  h->dynamic_adjusted = 1;
2588
 
2589
  /* If this is a weak definition, and we know a real definition, and
2590
     the real symbol is not itself defined by a regular object file,
2591
     then get a good value for the real definition.  We handle the
2592
     real symbol first, for the convenience of the backend routine.
2593
 
2594
     Note that there is a confusing case here.  If the real definition
2595
     is defined by a regular object file, we don't get the real symbol
2596
     from the dynamic object, but we do get the weak symbol.  If the
2597
     processor backend uses a COPY reloc, then if some routine in the
2598
     dynamic object changes the real symbol, we will not see that
2599
     change in the corresponding weak symbol.  This is the way other
2600
     ELF linkers work as well, and seems to be a result of the shared
2601
     library model.
2602
 
2603
     I will clarify this issue.  Most SVR4 shared libraries define the
2604
     variable _timezone and define timezone as a weak synonym.  The
2605
     tzset call changes _timezone.  If you write
2606
       extern int timezone;
2607
       int _timezone = 5;
2608
       int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2609
     you might expect that, since timezone is a synonym for _timezone,
2610
     the same number will print both times.  However, if the processor
2611
     backend uses a COPY reloc, then actually timezone will be copied
2612
     into your process image, and, since you define _timezone
2613
     yourself, _timezone will not.  Thus timezone and _timezone will
2614
     wind up at different memory locations.  The tzset call will set
2615
     _timezone, leaving timezone unchanged.  */
2616
 
2617
  if (h->u.weakdef != NULL)
2618
    {
2619
      /* If we get to this point, we know there is an implicit
2620
         reference by a regular object file via the weak symbol H.
2621
         FIXME: Is this really true?  What if the traversal finds
2622
         H->U.WEAKDEF before it finds H?  */
2623
      h->u.weakdef->ref_regular = 1;
2624
 
2625
      if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2626
        return FALSE;
2627
    }
2628
 
2629
  /* If a symbol has no type and no size and does not require a PLT
2630
     entry, then we are probably about to do the wrong thing here: we
2631
     are probably going to create a COPY reloc for an empty object.
2632
     This case can arise when a shared object is built with assembly
2633
     code, and the assembly code fails to set the symbol type.  */
2634
  if (h->size == 0
2635
      && h->type == STT_NOTYPE
2636
      && !h->needs_plt)
2637
    (*_bfd_error_handler)
2638
      (_("warning: type and size of dynamic symbol `%s' are not defined"),
2639
       h->root.root.string);
2640
 
2641
  dynobj = elf_hash_table (eif->info)->dynobj;
2642
  bed = get_elf_backend_data (dynobj);
2643
 
2644
  if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2645
    {
2646
      eif->failed = TRUE;
2647
      return FALSE;
2648
    }
2649
 
2650
  return TRUE;
2651
}
2652
 
2653
/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2654
   DYNBSS.  */
2655
 
2656
bfd_boolean
2657
_bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2658
                              asection *dynbss)
2659
{
2660
  unsigned int power_of_two;
2661
  bfd_vma mask;
2662
  asection *sec = h->root.u.def.section;
2663
 
2664
  /* The section aligment of definition is the maximum alignment
2665
     requirement of symbols defined in the section.  Since we don't
2666
     know the symbol alignment requirement, we start with the
2667
     maximum alignment and check low bits of the symbol address
2668
     for the minimum alignment.  */
2669
  power_of_two = bfd_get_section_alignment (sec->owner, sec);
2670
  mask = ((bfd_vma) 1 << power_of_two) - 1;
2671
  while ((h->root.u.def.value & mask) != 0)
2672
    {
2673
       mask >>= 1;
2674
       --power_of_two;
2675
    }
2676
 
2677
  if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2678
                                                dynbss))
2679
    {
2680
      /* Adjust the section alignment if needed.  */
2681
      if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2682
                                       power_of_two))
2683
        return FALSE;
2684
    }
2685
 
2686
  /* We make sure that the symbol will be aligned properly.  */
2687
  dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2688
 
2689
  /* Define the symbol as being at this point in DYNBSS.  */
2690
  h->root.u.def.section = dynbss;
2691
  h->root.u.def.value = dynbss->size;
2692
 
2693
  /* Increment the size of DYNBSS to make room for the symbol.  */
2694
  dynbss->size += h->size;
2695
 
2696
  return TRUE;
2697
}
2698
 
2699
/* Adjust all external symbols pointing into SEC_MERGE sections
2700
   to reflect the object merging within the sections.  */
2701
 
2702
static bfd_boolean
2703
_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2704
{
2705
  asection *sec;
2706
 
2707
  if ((h->root.type == bfd_link_hash_defined
2708
       || h->root.type == bfd_link_hash_defweak)
2709
      && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2710
      && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2711
    {
2712
      bfd *output_bfd = (bfd *) data;
2713
 
2714
      h->root.u.def.value =
2715
        _bfd_merged_section_offset (output_bfd,
2716
                                    &h->root.u.def.section,
2717
                                    elf_section_data (sec)->sec_info,
2718
                                    h->root.u.def.value);
2719
    }
2720
 
2721
  return TRUE;
2722
}
2723
 
2724
/* Returns false if the symbol referred to by H should be considered
2725
   to resolve local to the current module, and true if it should be
2726
   considered to bind dynamically.  */
2727
 
2728
bfd_boolean
2729
_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2730
                           struct bfd_link_info *info,
2731
                           bfd_boolean not_local_protected)
2732
{
2733
  bfd_boolean binding_stays_local_p;
2734
  const struct elf_backend_data *bed;
2735
  struct elf_link_hash_table *hash_table;
2736
 
2737
  if (h == NULL)
2738
    return FALSE;
2739
 
2740
  while (h->root.type == bfd_link_hash_indirect
2741
         || h->root.type == bfd_link_hash_warning)
2742
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
2743
 
2744
  /* If it was forced local, then clearly it's not dynamic.  */
2745
  if (h->dynindx == -1)
2746
    return FALSE;
2747
  if (h->forced_local)
2748
    return FALSE;
2749
 
2750
  /* Identify the cases where name binding rules say that a
2751
     visible symbol resolves locally.  */
2752
  binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2753
 
2754
  switch (ELF_ST_VISIBILITY (h->other))
2755
    {
2756
    case STV_INTERNAL:
2757
    case STV_HIDDEN:
2758
      return FALSE;
2759
 
2760
    case STV_PROTECTED:
2761
      hash_table = elf_hash_table (info);
2762
      if (!is_elf_hash_table (hash_table))
2763
        return FALSE;
2764
 
2765
      bed = get_elf_backend_data (hash_table->dynobj);
2766
 
2767
      /* Proper resolution for function pointer equality may require
2768
         that these symbols perhaps be resolved dynamically, even though
2769
         we should be resolving them to the current module.  */
2770
      if (!not_local_protected || !bed->is_function_type (h->type))
2771
        binding_stays_local_p = TRUE;
2772
      break;
2773
 
2774
    default:
2775
      break;
2776
    }
2777
 
2778
  /* If it isn't defined locally, then clearly it's dynamic.  */
2779
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2780
    return TRUE;
2781
 
2782
  /* Otherwise, the symbol is dynamic if binding rules don't tell
2783
     us that it remains local.  */
2784
  return !binding_stays_local_p;
2785
}
2786
 
2787
/* Return true if the symbol referred to by H should be considered
2788
   to resolve local to the current module, and false otherwise.  Differs
2789
   from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2790
   undefined symbols.  The two functions are virtually identical except
2791
   for the place where forced_local and dynindx == -1 are tested.  If
2792
   either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2793
   the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2794
   the symbol is local only for defined symbols.
2795
   It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2796
   !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2797
   treatment of undefined weak symbols.  For those that do not make
2798
   undefined weak symbols dynamic, both functions may return false.  */
2799
 
2800
bfd_boolean
2801
_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2802
                              struct bfd_link_info *info,
2803
                              bfd_boolean local_protected)
2804
{
2805
  const struct elf_backend_data *bed;
2806
  struct elf_link_hash_table *hash_table;
2807
 
2808
  /* If it's a local sym, of course we resolve locally.  */
2809
  if (h == NULL)
2810
    return TRUE;
2811
 
2812
  /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
2813
  if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2814
      || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2815
    return TRUE;
2816
 
2817
  /* Common symbols that become definitions don't get the DEF_REGULAR
2818
     flag set, so test it first, and don't bail out.  */
2819
  if (ELF_COMMON_DEF_P (h))
2820
    /* Do nothing.  */;
2821
  /* If we don't have a definition in a regular file, then we can't
2822
     resolve locally.  The sym is either undefined or dynamic.  */
2823
  else if (!h->def_regular)
2824
    return FALSE;
2825
 
2826
  /* Forced local symbols resolve locally.  */
2827
  if (h->forced_local)
2828
    return TRUE;
2829
 
2830
  /* As do non-dynamic symbols.  */
2831
  if (h->dynindx == -1)
2832
    return TRUE;
2833
 
2834
  /* At this point, we know the symbol is defined and dynamic.  In an
2835
     executable it must resolve locally, likewise when building symbolic
2836
     shared libraries.  */
2837
  if (info->executable || SYMBOLIC_BIND (info, h))
2838
    return TRUE;
2839
 
2840
  /* Now deal with defined dynamic symbols in shared libraries.  Ones
2841
     with default visibility might not resolve locally.  */
2842
  if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2843
    return FALSE;
2844
 
2845
  hash_table = elf_hash_table (info);
2846
  if (!is_elf_hash_table (hash_table))
2847
    return TRUE;
2848
 
2849
  bed = get_elf_backend_data (hash_table->dynobj);
2850
 
2851
  /* STV_PROTECTED non-function symbols are local.  */
2852
  if (!bed->is_function_type (h->type))
2853
    return TRUE;
2854
 
2855
  /* Function pointer equality tests may require that STV_PROTECTED
2856
     symbols be treated as dynamic symbols.  If the address of a
2857
     function not defined in an executable is set to that function's
2858
     plt entry in the executable, then the address of the function in
2859
     a shared library must also be the plt entry in the executable.  */
2860
  return local_protected;
2861
}
2862
 
2863
/* Caches some TLS segment info, and ensures that the TLS segment vma is
2864
   aligned.  Returns the first TLS output section.  */
2865
 
2866
struct bfd_section *
2867
_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2868
{
2869
  struct bfd_section *sec, *tls;
2870
  unsigned int align = 0;
2871
 
2872
  for (sec = obfd->sections; sec != NULL; sec = sec->next)
2873
    if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2874
      break;
2875
  tls = sec;
2876
 
2877
  for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2878
    if (sec->alignment_power > align)
2879
      align = sec->alignment_power;
2880
 
2881
  elf_hash_table (info)->tls_sec = tls;
2882
 
2883
  /* Ensure the alignment of the first section is the largest alignment,
2884
     so that the tls segment starts aligned.  */
2885
  if (tls != NULL)
2886
    tls->alignment_power = align;
2887
 
2888
  return tls;
2889
}
2890
 
2891
/* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
2892
static bfd_boolean
2893
is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2894
                                  Elf_Internal_Sym *sym)
2895
{
2896
  const struct elf_backend_data *bed;
2897
 
2898
  /* Local symbols do not count, but target specific ones might.  */
2899
  if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2900
      && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2901
    return FALSE;
2902
 
2903
  bed = get_elf_backend_data (abfd);
2904
  /* Function symbols do not count.  */
2905
  if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
2906
    return FALSE;
2907
 
2908
  /* If the section is undefined, then so is the symbol.  */
2909
  if (sym->st_shndx == SHN_UNDEF)
2910
    return FALSE;
2911
 
2912
  /* If the symbol is defined in the common section, then
2913
     it is a common definition and so does not count.  */
2914
  if (bed->common_definition (sym))
2915
    return FALSE;
2916
 
2917
  /* If the symbol is in a target specific section then we
2918
     must rely upon the backend to tell us what it is.  */
2919
  if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2920
    /* FIXME - this function is not coded yet:
2921
 
2922
       return _bfd_is_global_symbol_definition (abfd, sym);
2923
 
2924
       Instead for now assume that the definition is not global,
2925
       Even if this is wrong, at least the linker will behave
2926
       in the same way that it used to do.  */
2927
    return FALSE;
2928
 
2929
  return TRUE;
2930
}
2931
 
2932
/* Search the symbol table of the archive element of the archive ABFD
2933
   whose archive map contains a mention of SYMDEF, and determine if
2934
   the symbol is defined in this element.  */
2935
static bfd_boolean
2936
elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2937
{
2938
  Elf_Internal_Shdr * hdr;
2939
  bfd_size_type symcount;
2940
  bfd_size_type extsymcount;
2941
  bfd_size_type extsymoff;
2942
  Elf_Internal_Sym *isymbuf;
2943
  Elf_Internal_Sym *isym;
2944
  Elf_Internal_Sym *isymend;
2945
  bfd_boolean result;
2946
 
2947
  abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2948
  if (abfd == NULL)
2949
    return FALSE;
2950
 
2951
  if (! bfd_check_format (abfd, bfd_object))
2952
    return FALSE;
2953
 
2954
  /* If we have already included the element containing this symbol in the
2955
     link then we do not need to include it again.  Just claim that any symbol
2956
     it contains is not a definition, so that our caller will not decide to
2957
     (re)include this element.  */
2958
  if (abfd->archive_pass)
2959
    return FALSE;
2960
 
2961
  /* Select the appropriate symbol table.  */
2962
  if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2963
    hdr = &elf_tdata (abfd)->symtab_hdr;
2964
  else
2965
    hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2966
 
2967
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2968
 
2969
  /* The sh_info field of the symtab header tells us where the
2970
     external symbols start.  We don't care about the local symbols.  */
2971
  if (elf_bad_symtab (abfd))
2972
    {
2973
      extsymcount = symcount;
2974
      extsymoff = 0;
2975
    }
2976
  else
2977
    {
2978
      extsymcount = symcount - hdr->sh_info;
2979
      extsymoff = hdr->sh_info;
2980
    }
2981
 
2982
  if (extsymcount == 0)
2983
    return FALSE;
2984
 
2985
  /* Read in the symbol table.  */
2986
  isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2987
                                  NULL, NULL, NULL);
2988
  if (isymbuf == NULL)
2989
    return FALSE;
2990
 
2991
  /* Scan the symbol table looking for SYMDEF.  */
2992
  result = FALSE;
2993
  for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2994
    {
2995
      const char *name;
2996
 
2997
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2998
                                              isym->st_name);
2999
      if (name == NULL)
3000
        break;
3001
 
3002
      if (strcmp (name, symdef->name) == 0)
3003
        {
3004
          result = is_global_data_symbol_definition (abfd, isym);
3005
          break;
3006
        }
3007
    }
3008
 
3009
  free (isymbuf);
3010
 
3011
  return result;
3012
}
3013
 
3014
/* Add an entry to the .dynamic table.  */
3015
 
3016
bfd_boolean
3017
_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3018
                            bfd_vma tag,
3019
                            bfd_vma val)
3020
{
3021
  struct elf_link_hash_table *hash_table;
3022
  const struct elf_backend_data *bed;
3023
  asection *s;
3024
  bfd_size_type newsize;
3025
  bfd_byte *newcontents;
3026
  Elf_Internal_Dyn dyn;
3027
 
3028
  hash_table = elf_hash_table (info);
3029
  if (! is_elf_hash_table (hash_table))
3030
    return FALSE;
3031
 
3032
  bed = get_elf_backend_data (hash_table->dynobj);
3033
  s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3034
  BFD_ASSERT (s != NULL);
3035
 
3036
  newsize = s->size + bed->s->sizeof_dyn;
3037
  newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3038
  if (newcontents == NULL)
3039
    return FALSE;
3040
 
3041
  dyn.d_tag = tag;
3042
  dyn.d_un.d_val = val;
3043
  bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3044
 
3045
  s->size = newsize;
3046
  s->contents = newcontents;
3047
 
3048
  return TRUE;
3049
}
3050
 
3051
/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3052
   otherwise just check whether one already exists.  Returns -1 on error,
3053
   1 if a DT_NEEDED tag already exists, and 0 on success.  */
3054
 
3055
static int
3056
elf_add_dt_needed_tag (bfd *abfd,
3057
                       struct bfd_link_info *info,
3058
                       const char *soname,
3059
                       bfd_boolean do_it)
3060
{
3061
  struct elf_link_hash_table *hash_table;
3062
  bfd_size_type oldsize;
3063
  bfd_size_type strindex;
3064
 
3065
  if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3066
    return -1;
3067
 
3068
  hash_table = elf_hash_table (info);
3069
  oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3070
  strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3071
  if (strindex == (bfd_size_type) -1)
3072
    return -1;
3073
 
3074
  if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3075
    {
3076
      asection *sdyn;
3077
      const struct elf_backend_data *bed;
3078
      bfd_byte *extdyn;
3079
 
3080
      bed = get_elf_backend_data (hash_table->dynobj);
3081
      sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3082
      if (sdyn != NULL)
3083
        for (extdyn = sdyn->contents;
3084
             extdyn < sdyn->contents + sdyn->size;
3085
             extdyn += bed->s->sizeof_dyn)
3086
          {
3087
            Elf_Internal_Dyn dyn;
3088
 
3089
            bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3090
            if (dyn.d_tag == DT_NEEDED
3091
                && dyn.d_un.d_val == strindex)
3092
              {
3093
                _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3094
                return 1;
3095
              }
3096
          }
3097
    }
3098
 
3099
  if (do_it)
3100
    {
3101
      if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3102
        return -1;
3103
 
3104
      if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3105
        return -1;
3106
    }
3107
  else
3108
    /* We were just checking for existence of the tag.  */
3109
    _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3110
 
3111
  return 0;
3112
}
3113
 
3114
static bfd_boolean
3115
on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3116
{
3117
  for (; needed != NULL; needed = needed->next)
3118
    if (strcmp (soname, needed->name) == 0)
3119
      return TRUE;
3120
 
3121
  return FALSE;
3122
}
3123
 
3124
/* Sort symbol by value and section.  */
3125
static int
3126
elf_sort_symbol (const void *arg1, const void *arg2)
3127
{
3128
  const struct elf_link_hash_entry *h1;
3129
  const struct elf_link_hash_entry *h2;
3130
  bfd_signed_vma vdiff;
3131
 
3132
  h1 = *(const struct elf_link_hash_entry **) arg1;
3133
  h2 = *(const struct elf_link_hash_entry **) arg2;
3134
  vdiff = h1->root.u.def.value - h2->root.u.def.value;
3135
  if (vdiff != 0)
3136
    return vdiff > 0 ? 1 : -1;
3137
  else
3138
    {
3139
      long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3140
      if (sdiff != 0)
3141
        return sdiff > 0 ? 1 : -1;
3142
    }
3143
  return 0;
3144
}
3145
 
3146
/* This function is used to adjust offsets into .dynstr for
3147
   dynamic symbols.  This is called via elf_link_hash_traverse.  */
3148
 
3149
static bfd_boolean
3150
elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3151
{
3152
  struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3153
 
3154
  if (h->dynindx != -1)
3155
    h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3156
  return TRUE;
3157
}
3158
 
3159
/* Assign string offsets in .dynstr, update all structures referencing
3160
   them.  */
3161
 
3162
static bfd_boolean
3163
elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3164
{
3165
  struct elf_link_hash_table *hash_table = elf_hash_table (info);
3166
  struct elf_link_local_dynamic_entry *entry;
3167
  struct elf_strtab_hash *dynstr = hash_table->dynstr;
3168
  bfd *dynobj = hash_table->dynobj;
3169
  asection *sdyn;
3170
  bfd_size_type size;
3171
  const struct elf_backend_data *bed;
3172
  bfd_byte *extdyn;
3173
 
3174
  _bfd_elf_strtab_finalize (dynstr);
3175
  size = _bfd_elf_strtab_size (dynstr);
3176
 
3177
  bed = get_elf_backend_data (dynobj);
3178
  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3179
  BFD_ASSERT (sdyn != NULL);
3180
 
3181
  /* Update all .dynamic entries referencing .dynstr strings.  */
3182
  for (extdyn = sdyn->contents;
3183
       extdyn < sdyn->contents + sdyn->size;
3184
       extdyn += bed->s->sizeof_dyn)
3185
    {
3186
      Elf_Internal_Dyn dyn;
3187
 
3188
      bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3189
      switch (dyn.d_tag)
3190
        {
3191
        case DT_STRSZ:
3192
          dyn.d_un.d_val = size;
3193
          break;
3194
        case DT_NEEDED:
3195
        case DT_SONAME:
3196
        case DT_RPATH:
3197
        case DT_RUNPATH:
3198
        case DT_FILTER:
3199
        case DT_AUXILIARY:
3200
        case DT_AUDIT:
3201
        case DT_DEPAUDIT:
3202
          dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3203
          break;
3204
        default:
3205
          continue;
3206
        }
3207
      bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3208
    }
3209
 
3210
  /* Now update local dynamic symbols.  */
3211
  for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3212
    entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3213
                                                  entry->isym.st_name);
3214
 
3215
  /* And the rest of dynamic symbols.  */
3216
  elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3217
 
3218
  /* Adjust version definitions.  */
3219
  if (elf_tdata (output_bfd)->cverdefs)
3220
    {
3221
      asection *s;
3222
      bfd_byte *p;
3223
      bfd_size_type i;
3224
      Elf_Internal_Verdef def;
3225
      Elf_Internal_Verdaux defaux;
3226
 
3227
      s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3228
      p = s->contents;
3229
      do
3230
        {
3231
          _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3232
                                   &def);
3233
          p += sizeof (Elf_External_Verdef);
3234
          if (def.vd_aux != sizeof (Elf_External_Verdef))
3235
            continue;
3236
          for (i = 0; i < def.vd_cnt; ++i)
3237
            {
3238
              _bfd_elf_swap_verdaux_in (output_bfd,
3239
                                        (Elf_External_Verdaux *) p, &defaux);
3240
              defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3241
                                                        defaux.vda_name);
3242
              _bfd_elf_swap_verdaux_out (output_bfd,
3243
                                         &defaux, (Elf_External_Verdaux *) p);
3244
              p += sizeof (Elf_External_Verdaux);
3245
            }
3246
        }
3247
      while (def.vd_next);
3248
    }
3249
 
3250
  /* Adjust version references.  */
3251
  if (elf_tdata (output_bfd)->verref)
3252
    {
3253
      asection *s;
3254
      bfd_byte *p;
3255
      bfd_size_type i;
3256
      Elf_Internal_Verneed need;
3257
      Elf_Internal_Vernaux needaux;
3258
 
3259
      s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3260
      p = s->contents;
3261
      do
3262
        {
3263
          _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3264
                                    &need);
3265
          need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3266
          _bfd_elf_swap_verneed_out (output_bfd, &need,
3267
                                     (Elf_External_Verneed *) p);
3268
          p += sizeof (Elf_External_Verneed);
3269
          for (i = 0; i < need.vn_cnt; ++i)
3270
            {
3271
              _bfd_elf_swap_vernaux_in (output_bfd,
3272
                                        (Elf_External_Vernaux *) p, &needaux);
3273
              needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3274
                                                         needaux.vna_name);
3275
              _bfd_elf_swap_vernaux_out (output_bfd,
3276
                                         &needaux,
3277
                                         (Elf_External_Vernaux *) p);
3278
              p += sizeof (Elf_External_Vernaux);
3279
            }
3280
        }
3281
      while (need.vn_next);
3282
    }
3283
 
3284
  return TRUE;
3285
}
3286
 
3287
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3288
   The default is to only match when the INPUT and OUTPUT are exactly
3289
   the same target.  */
3290
 
3291
bfd_boolean
3292
_bfd_elf_default_relocs_compatible (const bfd_target *input,
3293
                                    const bfd_target *output)
3294
{
3295
  return input == output;
3296
}
3297
 
3298
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3299
   This version is used when different targets for the same architecture
3300
   are virtually identical.  */
3301
 
3302
bfd_boolean
3303
_bfd_elf_relocs_compatible (const bfd_target *input,
3304
                            const bfd_target *output)
3305
{
3306
  const struct elf_backend_data *obed, *ibed;
3307
 
3308
  if (input == output)
3309
    return TRUE;
3310
 
3311
  ibed = xvec_get_elf_backend_data (input);
3312
  obed = xvec_get_elf_backend_data (output);
3313
 
3314
  if (ibed->arch != obed->arch)
3315
    return FALSE;
3316
 
3317
  /* If both backends are using this function, deem them compatible.  */
3318
  return ibed->relocs_compatible == obed->relocs_compatible;
3319
}
3320
 
3321
/* Add symbols from an ELF object file to the linker hash table.  */
3322
 
3323
static bfd_boolean
3324
elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3325
{
3326
  Elf_Internal_Ehdr *ehdr;
3327
  Elf_Internal_Shdr *hdr;
3328
  bfd_size_type symcount;
3329
  bfd_size_type extsymcount;
3330
  bfd_size_type extsymoff;
3331
  struct elf_link_hash_entry **sym_hash;
3332
  bfd_boolean dynamic;
3333
  Elf_External_Versym *extversym = NULL;
3334
  Elf_External_Versym *ever;
3335
  struct elf_link_hash_entry *weaks;
3336
  struct elf_link_hash_entry **nondeflt_vers = NULL;
3337
  bfd_size_type nondeflt_vers_cnt = 0;
3338
  Elf_Internal_Sym *isymbuf = NULL;
3339
  Elf_Internal_Sym *isym;
3340
  Elf_Internal_Sym *isymend;
3341
  const struct elf_backend_data *bed;
3342
  bfd_boolean add_needed;
3343
  struct elf_link_hash_table *htab;
3344
  bfd_size_type amt;
3345
  void *alloc_mark = NULL;
3346
  struct bfd_hash_entry **old_table = NULL;
3347
  unsigned int old_size = 0;
3348
  unsigned int old_count = 0;
3349
  void *old_tab = NULL;
3350
  void *old_hash;
3351
  void *old_ent;
3352
  struct bfd_link_hash_entry *old_undefs = NULL;
3353
  struct bfd_link_hash_entry *old_undefs_tail = NULL;
3354
  long old_dynsymcount = 0;
3355
  size_t tabsize = 0;
3356
  size_t hashsize = 0;
3357
 
3358
  htab = elf_hash_table (info);
3359
  bed = get_elf_backend_data (abfd);
3360
 
3361
  if ((abfd->flags & DYNAMIC) == 0)
3362
    dynamic = FALSE;
3363
  else
3364
    {
3365
      dynamic = TRUE;
3366
 
3367
      /* You can't use -r against a dynamic object.  Also, there's no
3368
         hope of using a dynamic object which does not exactly match
3369
         the format of the output file.  */
3370
      if (info->relocatable
3371
          || !is_elf_hash_table (htab)
3372
          || info->output_bfd->xvec != abfd->xvec)
3373
        {
3374
          if (info->relocatable)
3375
            bfd_set_error (bfd_error_invalid_operation);
3376
          else
3377
            bfd_set_error (bfd_error_wrong_format);
3378
          goto error_return;
3379
        }
3380
    }
3381
 
3382
  ehdr = elf_elfheader (abfd);
3383
  if (info->warn_alternate_em
3384
      && bed->elf_machine_code != ehdr->e_machine
3385
      && ((bed->elf_machine_alt1 != 0
3386
           && ehdr->e_machine == bed->elf_machine_alt1)
3387
          || (bed->elf_machine_alt2 != 0
3388
              && ehdr->e_machine == bed->elf_machine_alt2)))
3389
    info->callbacks->einfo
3390
      (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3391
       ehdr->e_machine, abfd, bed->elf_machine_code);
3392
 
3393
  /* As a GNU extension, any input sections which are named
3394
     .gnu.warning.SYMBOL are treated as warning symbols for the given
3395
     symbol.  This differs from .gnu.warning sections, which generate
3396
     warnings when they are included in an output file.  */
3397
  /* PR 12761: Also generate this warning when building shared libraries.  */
3398
  if (info->executable || info->shared)
3399
    {
3400
      asection *s;
3401
 
3402
      for (s = abfd->sections; s != NULL; s = s->next)
3403
        {
3404
          const char *name;
3405
 
3406
          name = bfd_get_section_name (abfd, s);
3407
          if (CONST_STRNEQ (name, ".gnu.warning."))
3408
            {
3409
              char *msg;
3410
              bfd_size_type sz;
3411
 
3412
              name += sizeof ".gnu.warning." - 1;
3413
 
3414
              /* If this is a shared object, then look up the symbol
3415
                 in the hash table.  If it is there, and it is already
3416
                 been defined, then we will not be using the entry
3417
                 from this shared object, so we don't need to warn.
3418
                 FIXME: If we see the definition in a regular object
3419
                 later on, we will warn, but we shouldn't.  The only
3420
                 fix is to keep track of what warnings we are supposed
3421
                 to emit, and then handle them all at the end of the
3422
                 link.  */
3423
              if (dynamic)
3424
                {
3425
                  struct elf_link_hash_entry *h;
3426
 
3427
                  h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3428
 
3429
                  /* FIXME: What about bfd_link_hash_common?  */
3430
                  if (h != NULL
3431
                      && (h->root.type == bfd_link_hash_defined
3432
                          || h->root.type == bfd_link_hash_defweak))
3433
                    {
3434
                      /* We don't want to issue this warning.  Clobber
3435
                         the section size so that the warning does not
3436
                         get copied into the output file.  */
3437
                      s->size = 0;
3438
                      continue;
3439
                    }
3440
                }
3441
 
3442
              sz = s->size;
3443
              msg = (char *) bfd_alloc (abfd, sz + 1);
3444
              if (msg == NULL)
3445
                goto error_return;
3446
 
3447
              if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3448
                goto error_return;
3449
 
3450
              msg[sz] = '\0';
3451
 
3452
              if (! (_bfd_generic_link_add_one_symbol
3453
                     (info, abfd, name, BSF_WARNING, s, 0, msg,
3454
                      FALSE, bed->collect, NULL)))
3455
                goto error_return;
3456
 
3457
              if (! info->relocatable)
3458
                {
3459
                  /* Clobber the section size so that the warning does
3460
                     not get copied into the output file.  */
3461
                  s->size = 0;
3462
 
3463
                  /* Also set SEC_EXCLUDE, so that symbols defined in
3464
                     the warning section don't get copied to the output.  */
3465
                  s->flags |= SEC_EXCLUDE;
3466
                }
3467
            }
3468
        }
3469
    }
3470
 
3471
  add_needed = TRUE;
3472
  if (! dynamic)
3473
    {
3474
      /* If we are creating a shared library, create all the dynamic
3475
         sections immediately.  We need to attach them to something,
3476
         so we attach them to this BFD, provided it is the right
3477
         format.  FIXME: If there are no input BFD's of the same
3478
         format as the output, we can't make a shared library.  */
3479
      if (info->shared
3480
          && is_elf_hash_table (htab)
3481
          && info->output_bfd->xvec == abfd->xvec
3482
          && !htab->dynamic_sections_created)
3483
        {
3484
          if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3485
            goto error_return;
3486
        }
3487
    }
3488
  else if (!is_elf_hash_table (htab))
3489
    goto error_return;
3490
  else
3491
    {
3492
      asection *s;
3493
      const char *soname = NULL;
3494
      char *audit = NULL;
3495
      struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3496
      int ret;
3497
 
3498
      /* ld --just-symbols and dynamic objects don't mix very well.
3499
         ld shouldn't allow it.  */
3500
      if ((s = abfd->sections) != NULL
3501
          && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3502
        abort ();
3503
 
3504
      /* If this dynamic lib was specified on the command line with
3505
         --as-needed in effect, then we don't want to add a DT_NEEDED
3506
         tag unless the lib is actually used.  Similary for libs brought
3507
         in by another lib's DT_NEEDED.  When --no-add-needed is used
3508
         on a dynamic lib, we don't want to add a DT_NEEDED entry for
3509
         any dynamic library in DT_NEEDED tags in the dynamic lib at
3510
         all.  */
3511
      add_needed = (elf_dyn_lib_class (abfd)
3512
                    & (DYN_AS_NEEDED | DYN_DT_NEEDED
3513
                       | DYN_NO_NEEDED)) == 0;
3514
 
3515
      s = bfd_get_section_by_name (abfd, ".dynamic");
3516
      if (s != NULL)
3517
        {
3518
          bfd_byte *dynbuf;
3519
          bfd_byte *extdyn;
3520
          unsigned int elfsec;
3521
          unsigned long shlink;
3522
 
3523
          if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3524
            {
3525
error_free_dyn:
3526
              free (dynbuf);
3527
              goto error_return;
3528
            }
3529
 
3530
          elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3531
          if (elfsec == SHN_BAD)
3532
            goto error_free_dyn;
3533
          shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3534
 
3535
          for (extdyn = dynbuf;
3536
               extdyn < dynbuf + s->size;
3537
               extdyn += bed->s->sizeof_dyn)
3538
            {
3539
              Elf_Internal_Dyn dyn;
3540
 
3541
              bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3542
              if (dyn.d_tag == DT_SONAME)
3543
                {
3544
                  unsigned int tagv = dyn.d_un.d_val;
3545
                  soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3546
                  if (soname == NULL)
3547
                    goto error_free_dyn;
3548
                }
3549
              if (dyn.d_tag == DT_NEEDED)
3550
                {
3551
                  struct bfd_link_needed_list *n, **pn;
3552
                  char *fnm, *anm;
3553
                  unsigned int tagv = dyn.d_un.d_val;
3554
 
3555
                  amt = sizeof (struct bfd_link_needed_list);
3556
                  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3557
                  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3558
                  if (n == NULL || fnm == NULL)
3559
                    goto error_free_dyn;
3560
                  amt = strlen (fnm) + 1;
3561
                  anm = (char *) bfd_alloc (abfd, amt);
3562
                  if (anm == NULL)
3563
                    goto error_free_dyn;
3564
                  memcpy (anm, fnm, amt);
3565
                  n->name = anm;
3566
                  n->by = abfd;
3567
                  n->next = NULL;
3568
                  for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3569
                    ;
3570
                  *pn = n;
3571
                }
3572
              if (dyn.d_tag == DT_RUNPATH)
3573
                {
3574
                  struct bfd_link_needed_list *n, **pn;
3575
                  char *fnm, *anm;
3576
                  unsigned int tagv = dyn.d_un.d_val;
3577
 
3578
                  amt = sizeof (struct bfd_link_needed_list);
3579
                  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3580
                  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3581
                  if (n == NULL || fnm == NULL)
3582
                    goto error_free_dyn;
3583
                  amt = strlen (fnm) + 1;
3584
                  anm = (char *) bfd_alloc (abfd, amt);
3585
                  if (anm == NULL)
3586
                    goto error_free_dyn;
3587
                  memcpy (anm, fnm, amt);
3588
                  n->name = anm;
3589
                  n->by = abfd;
3590
                  n->next = NULL;
3591
                  for (pn = & runpath;
3592
                       *pn != NULL;
3593
                       pn = &(*pn)->next)
3594
                    ;
3595
                  *pn = n;
3596
                }
3597
              /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
3598
              if (!runpath && dyn.d_tag == DT_RPATH)
3599
                {
3600
                  struct bfd_link_needed_list *n, **pn;
3601
                  char *fnm, *anm;
3602
                  unsigned int tagv = dyn.d_un.d_val;
3603
 
3604
                  amt = sizeof (struct bfd_link_needed_list);
3605
                  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3606
                  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3607
                  if (n == NULL || fnm == NULL)
3608
                    goto error_free_dyn;
3609
                  amt = strlen (fnm) + 1;
3610
                  anm = (char *) bfd_alloc (abfd, amt);
3611
                  if (anm == NULL)
3612
                    goto error_free_dyn;
3613
                  memcpy (anm, fnm, amt);
3614
                  n->name = anm;
3615
                  n->by = abfd;
3616
                  n->next = NULL;
3617
                  for (pn = & rpath;
3618
                       *pn != NULL;
3619
                       pn = &(*pn)->next)
3620
                    ;
3621
                  *pn = n;
3622
                }
3623
              if (dyn.d_tag == DT_AUDIT)
3624
                {
3625
                  unsigned int tagv = dyn.d_un.d_val;
3626
                  audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3627
                }
3628
            }
3629
 
3630
          free (dynbuf);
3631
        }
3632
 
3633
      /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
3634
         frees all more recently bfd_alloc'd blocks as well.  */
3635
      if (runpath)
3636
        rpath = runpath;
3637
 
3638
      if (rpath)
3639
        {
3640
          struct bfd_link_needed_list **pn;
3641
          for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3642
            ;
3643
          *pn = rpath;
3644
        }
3645
 
3646
      /* We do not want to include any of the sections in a dynamic
3647
         object in the output file.  We hack by simply clobbering the
3648
         list of sections in the BFD.  This could be handled more
3649
         cleanly by, say, a new section flag; the existing
3650
         SEC_NEVER_LOAD flag is not the one we want, because that one
3651
         still implies that the section takes up space in the output
3652
         file.  */
3653
      bfd_section_list_clear (abfd);
3654
 
3655
      /* Find the name to use in a DT_NEEDED entry that refers to this
3656
         object.  If the object has a DT_SONAME entry, we use it.
3657
         Otherwise, if the generic linker stuck something in
3658
         elf_dt_name, we use that.  Otherwise, we just use the file
3659
         name.  */
3660
      if (soname == NULL || *soname == '\0')
3661
        {
3662
          soname = elf_dt_name (abfd);
3663
          if (soname == NULL || *soname == '\0')
3664
            soname = bfd_get_filename (abfd);
3665
        }
3666
 
3667
      /* Save the SONAME because sometimes the linker emulation code
3668
         will need to know it.  */
3669
      elf_dt_name (abfd) = soname;
3670
 
3671
      ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3672
      if (ret < 0)
3673
        goto error_return;
3674
 
3675
      /* If we have already included this dynamic object in the
3676
         link, just ignore it.  There is no reason to include a
3677
         particular dynamic object more than once.  */
3678
      if (ret > 0)
3679
        return TRUE;
3680
 
3681
      /* Save the DT_AUDIT entry for the linker emulation code. */
3682
      elf_dt_audit (abfd) = audit;
3683
    }
3684
 
3685
  /* If this is a dynamic object, we always link against the .dynsym
3686
     symbol table, not the .symtab symbol table.  The dynamic linker
3687
     will only see the .dynsym symbol table, so there is no reason to
3688
     look at .symtab for a dynamic object.  */
3689
 
3690
  if (! dynamic || elf_dynsymtab (abfd) == 0)
3691
    hdr = &elf_tdata (abfd)->symtab_hdr;
3692
  else
3693
    hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3694
 
3695
  symcount = hdr->sh_size / bed->s->sizeof_sym;
3696
 
3697
  /* The sh_info field of the symtab header tells us where the
3698
     external symbols start.  We don't care about the local symbols at
3699
     this point.  */
3700
  if (elf_bad_symtab (abfd))
3701
    {
3702
      extsymcount = symcount;
3703
      extsymoff = 0;
3704
    }
3705
  else
3706
    {
3707
      extsymcount = symcount - hdr->sh_info;
3708
      extsymoff = hdr->sh_info;
3709
    }
3710
 
3711
  sym_hash = NULL;
3712
  if (extsymcount != 0)
3713
    {
3714
      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3715
                                      NULL, NULL, NULL);
3716
      if (isymbuf == NULL)
3717
        goto error_return;
3718
 
3719
      /* We store a pointer to the hash table entry for each external
3720
         symbol.  */
3721
      amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3722
      sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
3723
      if (sym_hash == NULL)
3724
        goto error_free_sym;
3725
      elf_sym_hashes (abfd) = sym_hash;
3726
    }
3727
 
3728
  if (dynamic)
3729
    {
3730
      /* Read in any version definitions.  */
3731
      if (!_bfd_elf_slurp_version_tables (abfd,
3732
                                          info->default_imported_symver))
3733
        goto error_free_sym;
3734
 
3735
      /* Read in the symbol versions, but don't bother to convert them
3736
         to internal format.  */
3737
      if (elf_dynversym (abfd) != 0)
3738
        {
3739
          Elf_Internal_Shdr *versymhdr;
3740
 
3741
          versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3742
          extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
3743
          if (extversym == NULL)
3744
            goto error_free_sym;
3745
          amt = versymhdr->sh_size;
3746
          if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3747
              || bfd_bread (extversym, amt, abfd) != amt)
3748
            goto error_free_vers;
3749
        }
3750
    }
3751
 
3752
  /* If we are loading an as-needed shared lib, save the symbol table
3753
     state before we start adding symbols.  If the lib turns out
3754
     to be unneeded, restore the state.  */
3755
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3756
    {
3757
      unsigned int i;
3758
      size_t entsize;
3759
 
3760
      for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3761
        {
3762
          struct bfd_hash_entry *p;
3763
          struct elf_link_hash_entry *h;
3764
 
3765
          for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3766
            {
3767
              h = (struct elf_link_hash_entry *) p;
3768
              entsize += htab->root.table.entsize;
3769
              if (h->root.type == bfd_link_hash_warning)
3770
                entsize += htab->root.table.entsize;
3771
            }
3772
        }
3773
 
3774
      tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3775
      hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3776
      old_tab = bfd_malloc (tabsize + entsize + hashsize);
3777
      if (old_tab == NULL)
3778
        goto error_free_vers;
3779
 
3780
      /* Remember the current objalloc pointer, so that all mem for
3781
         symbols added can later be reclaimed.  */
3782
      alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3783
      if (alloc_mark == NULL)
3784
        goto error_free_vers;
3785
 
3786
      /* Make a special call to the linker "notice" function to
3787
         tell it that we are about to handle an as-needed lib.  */
3788
      if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
3789
                                       notice_as_needed, 0, NULL))
3790
        goto error_free_vers;
3791
 
3792
      /* Clone the symbol table and sym hashes.  Remember some
3793
         pointers into the symbol table, and dynamic symbol count.  */
3794
      old_hash = (char *) old_tab + tabsize;
3795
      old_ent = (char *) old_hash + hashsize;
3796
      memcpy (old_tab, htab->root.table.table, tabsize);
3797
      memcpy (old_hash, sym_hash, hashsize);
3798
      old_undefs = htab->root.undefs;
3799
      old_undefs_tail = htab->root.undefs_tail;
3800
      old_table = htab->root.table.table;
3801
      old_size = htab->root.table.size;
3802
      old_count = htab->root.table.count;
3803
      old_dynsymcount = htab->dynsymcount;
3804
 
3805
      for (i = 0; i < htab->root.table.size; i++)
3806
        {
3807
          struct bfd_hash_entry *p;
3808
          struct elf_link_hash_entry *h;
3809
 
3810
          for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3811
            {
3812
              memcpy (old_ent, p, htab->root.table.entsize);
3813
              old_ent = (char *) old_ent + htab->root.table.entsize;
3814
              h = (struct elf_link_hash_entry *) p;
3815
              if (h->root.type == bfd_link_hash_warning)
3816
                {
3817
                  memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3818
                  old_ent = (char *) old_ent + htab->root.table.entsize;
3819
                }
3820
            }
3821
        }
3822
    }
3823
 
3824
  weaks = NULL;
3825
  ever = extversym != NULL ? extversym + extsymoff : NULL;
3826
  for (isym = isymbuf, isymend = isymbuf + extsymcount;
3827
       isym < isymend;
3828
       isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3829
    {
3830
      int bind;
3831
      bfd_vma value;
3832
      asection *sec, *new_sec;
3833
      flagword flags;
3834
      const char *name;
3835
      struct elf_link_hash_entry *h;
3836
      bfd_boolean definition;
3837
      bfd_boolean size_change_ok;
3838
      bfd_boolean type_change_ok;
3839
      bfd_boolean new_weakdef;
3840
      bfd_boolean override;
3841
      bfd_boolean common;
3842
      unsigned int old_alignment;
3843
      bfd *old_bfd;
3844
      bfd * undef_bfd = NULL;
3845
 
3846
      override = FALSE;
3847
 
3848
      flags = BSF_NO_FLAGS;
3849
      sec = NULL;
3850
      value = isym->st_value;
3851
      *sym_hash = NULL;
3852
      common = bed->common_definition (isym);
3853
 
3854
      bind = ELF_ST_BIND (isym->st_info);
3855
      switch (bind)
3856
        {
3857
        case STB_LOCAL:
3858
          /* This should be impossible, since ELF requires that all
3859
             global symbols follow all local symbols, and that sh_info
3860
             point to the first global symbol.  Unfortunately, Irix 5
3861
             screws this up.  */
3862
          continue;
3863
 
3864
        case STB_GLOBAL:
3865
          if (isym->st_shndx != SHN_UNDEF && !common)
3866
            flags = BSF_GLOBAL;
3867
          break;
3868
 
3869
        case STB_WEAK:
3870
          flags = BSF_WEAK;
3871
          break;
3872
 
3873
        case STB_GNU_UNIQUE:
3874
          flags = BSF_GNU_UNIQUE;
3875
          break;
3876
 
3877
        default:
3878
          /* Leave it up to the processor backend.  */
3879
          break;
3880
        }
3881
 
3882
      if (isym->st_shndx == SHN_UNDEF)
3883
        sec = bfd_und_section_ptr;
3884
      else if (isym->st_shndx == SHN_ABS)
3885
        sec = bfd_abs_section_ptr;
3886
      else if (isym->st_shndx == SHN_COMMON)
3887
        {
3888
          sec = bfd_com_section_ptr;
3889
          /* What ELF calls the size we call the value.  What ELF
3890
             calls the value we call the alignment.  */
3891
          value = isym->st_size;
3892
        }
3893
      else
3894
        {
3895
          sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3896
          if (sec == NULL)
3897
            sec = bfd_abs_section_ptr;
3898
          else if (sec->kept_section)
3899
            {
3900
              /* Symbols from discarded section are undefined.  We keep
3901
                 its visibility.  */
3902
              sec = bfd_und_section_ptr;
3903
              isym->st_shndx = SHN_UNDEF;
3904
            }
3905
          else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3906
            value -= sec->vma;
3907
        }
3908
 
3909
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3910
                                              isym->st_name);
3911
      if (name == NULL)
3912
        goto error_free_vers;
3913
 
3914
      if (isym->st_shndx == SHN_COMMON
3915
          && (abfd->flags & BFD_PLUGIN) != 0)
3916
        {
3917
          asection *xc = bfd_get_section_by_name (abfd, "COMMON");
3918
 
3919
          if (xc == NULL)
3920
            {
3921
              flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
3922
                                 | SEC_EXCLUDE);
3923
              xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
3924
              if (xc == NULL)
3925
                goto error_free_vers;
3926
            }
3927
          sec = xc;
3928
        }
3929
      else if (isym->st_shndx == SHN_COMMON
3930
               && ELF_ST_TYPE (isym->st_info) == STT_TLS
3931
               && !info->relocatable)
3932
        {
3933
          asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3934
 
3935
          if (tcomm == NULL)
3936
            {
3937
              flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
3938
                                 | SEC_LINKER_CREATED);
3939
              tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3940
              if (tcomm == NULL)
3941
                goto error_free_vers;
3942
            }
3943
          sec = tcomm;
3944
        }
3945
      else if (bed->elf_add_symbol_hook)
3946
        {
3947
          if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3948
                                             &sec, &value))
3949
            goto error_free_vers;
3950
 
3951
          /* The hook function sets the name to NULL if this symbol
3952
             should be skipped for some reason.  */
3953
          if (name == NULL)
3954
            continue;
3955
        }
3956
 
3957
      /* Sanity check that all possibilities were handled.  */
3958
      if (sec == NULL)
3959
        {
3960
          bfd_set_error (bfd_error_bad_value);
3961
          goto error_free_vers;
3962
        }
3963
 
3964
      if (bfd_is_und_section (sec)
3965
          || bfd_is_com_section (sec))
3966
        definition = FALSE;
3967
      else
3968
        definition = TRUE;
3969
 
3970
      size_change_ok = FALSE;
3971
      type_change_ok = bed->type_change_ok;
3972
      old_alignment = 0;
3973
      old_bfd = NULL;
3974
      new_sec = sec;
3975
 
3976
      if (is_elf_hash_table (htab))
3977
        {
3978
          Elf_Internal_Versym iver;
3979
          unsigned int vernum = 0;
3980
          bfd_boolean skip;
3981
 
3982
          /* If this is a definition of a symbol which was previously
3983
             referenced in a non-weak manner then make a note of the bfd
3984
             that contained the reference.  This is used if we need to
3985
             refer to the source of the reference later on.  */
3986
          if (! bfd_is_und_section (sec))
3987
            {
3988
              h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
3989
 
3990
              if (h != NULL
3991
                  && h->root.type == bfd_link_hash_undefined
3992
                  && h->root.u.undef.abfd)
3993
                undef_bfd = h->root.u.undef.abfd;
3994
            }
3995
 
3996
          if (ever == NULL)
3997
            {
3998
              if (info->default_imported_symver)
3999
                /* Use the default symbol version created earlier.  */
4000
                iver.vs_vers = elf_tdata (abfd)->cverdefs;
4001
              else
4002
                iver.vs_vers = 0;
4003
            }
4004
          else
4005
            _bfd_elf_swap_versym_in (abfd, ever, &iver);
4006
 
4007
          vernum = iver.vs_vers & VERSYM_VERSION;
4008
 
4009
          /* If this is a hidden symbol, or if it is not version
4010
             1, we append the version name to the symbol name.
4011
             However, we do not modify a non-hidden absolute symbol
4012
             if it is not a function, because it might be the version
4013
             symbol itself.  FIXME: What if it isn't?  */
4014
          if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4015
              || (vernum > 1
4016
                  && (!bfd_is_abs_section (sec)
4017
                      || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4018
            {
4019
              const char *verstr;
4020
              size_t namelen, verlen, newlen;
4021
              char *newname, *p;
4022
 
4023
              if (isym->st_shndx != SHN_UNDEF)
4024
                {
4025
                  if (vernum > elf_tdata (abfd)->cverdefs)
4026
                    verstr = NULL;
4027
                  else if (vernum > 1)
4028
                    verstr =
4029
                      elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4030
                  else
4031
                    verstr = "";
4032
 
4033
                  if (verstr == NULL)
4034
                    {
4035
                      (*_bfd_error_handler)
4036
                        (_("%B: %s: invalid version %u (max %d)"),
4037
                         abfd, name, vernum,
4038
                         elf_tdata (abfd)->cverdefs);
4039
                      bfd_set_error (bfd_error_bad_value);
4040
                      goto error_free_vers;
4041
                    }
4042
                }
4043
              else
4044
                {
4045
                  /* We cannot simply test for the number of
4046
                     entries in the VERNEED section since the
4047
                     numbers for the needed versions do not start
4048
                     at 0.  */
4049
                  Elf_Internal_Verneed *t;
4050
 
4051
                  verstr = NULL;
4052
                  for (t = elf_tdata (abfd)->verref;
4053
                       t != NULL;
4054
                       t = t->vn_nextref)
4055
                    {
4056
                      Elf_Internal_Vernaux *a;
4057
 
4058
                      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4059
                        {
4060
                          if (a->vna_other == vernum)
4061
                            {
4062
                              verstr = a->vna_nodename;
4063
                              break;
4064
                            }
4065
                        }
4066
                      if (a != NULL)
4067
                        break;
4068
                    }
4069
                  if (verstr == NULL)
4070
                    {
4071
                      (*_bfd_error_handler)
4072
                        (_("%B: %s: invalid needed version %d"),
4073
                         abfd, name, vernum);
4074
                      bfd_set_error (bfd_error_bad_value);
4075
                      goto error_free_vers;
4076
                    }
4077
                }
4078
 
4079
              namelen = strlen (name);
4080
              verlen = strlen (verstr);
4081
              newlen = namelen + verlen + 2;
4082
              if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4083
                  && isym->st_shndx != SHN_UNDEF)
4084
                ++newlen;
4085
 
4086
              newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4087
              if (newname == NULL)
4088
                goto error_free_vers;
4089
              memcpy (newname, name, namelen);
4090
              p = newname + namelen;
4091
              *p++ = ELF_VER_CHR;
4092
              /* If this is a defined non-hidden version symbol,
4093
                 we add another @ to the name.  This indicates the
4094
                 default version of the symbol.  */
4095
              if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4096
                  && isym->st_shndx != SHN_UNDEF)
4097
                *p++ = ELF_VER_CHR;
4098
              memcpy (p, verstr, verlen + 1);
4099
 
4100
              name = newname;
4101
            }
4102
 
4103
          /* If necessary, make a second attempt to locate the bfd
4104
             containing an unresolved, non-weak reference to the
4105
             current symbol.  */
4106
          if (! bfd_is_und_section (sec) && undef_bfd == NULL)
4107
            {
4108
              h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4109
 
4110
              if (h != NULL
4111
                  && h->root.type == bfd_link_hash_undefined
4112
                  && h->root.u.undef.abfd)
4113
                undef_bfd = h->root.u.undef.abfd;
4114
            }
4115
 
4116
          if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
4117
                                      &value, &old_alignment,
4118
                                      sym_hash, &skip, &override,
4119
                                      &type_change_ok, &size_change_ok))
4120
            goto error_free_vers;
4121
 
4122
          if (skip)
4123
            continue;
4124
 
4125
          if (override)
4126
            definition = FALSE;
4127
 
4128
          h = *sym_hash;
4129
          while (h->root.type == bfd_link_hash_indirect
4130
                 || h->root.type == bfd_link_hash_warning)
4131
            h = (struct elf_link_hash_entry *) h->root.u.i.link;
4132
 
4133
          /* Remember the old alignment if this is a common symbol, so
4134
             that we don't reduce the alignment later on.  We can't
4135
             check later, because _bfd_generic_link_add_one_symbol
4136
             will set a default for the alignment which we want to
4137
             override. We also remember the old bfd where the existing
4138
             definition comes from.  */
4139
          switch (h->root.type)
4140
            {
4141
            default:
4142
              break;
4143
 
4144
            case bfd_link_hash_defined:
4145
            case bfd_link_hash_defweak:
4146
              old_bfd = h->root.u.def.section->owner;
4147
              break;
4148
 
4149
            case bfd_link_hash_common:
4150
              old_bfd = h->root.u.c.p->section->owner;
4151
              old_alignment = h->root.u.c.p->alignment_power;
4152
              break;
4153
            }
4154
 
4155
          if (elf_tdata (abfd)->verdef != NULL
4156
              && ! override
4157
              && vernum > 1
4158
              && definition)
4159
            h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4160
        }
4161
 
4162
      if (! (_bfd_generic_link_add_one_symbol
4163
             (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4164
              (struct bfd_link_hash_entry **) sym_hash)))
4165
        goto error_free_vers;
4166
 
4167
      h = *sym_hash;
4168
      while (h->root.type == bfd_link_hash_indirect
4169
             || h->root.type == bfd_link_hash_warning)
4170
        h = (struct elf_link_hash_entry *) h->root.u.i.link;
4171
 
4172
      *sym_hash = h;
4173
      if (is_elf_hash_table (htab))
4174
        h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4175
 
4176
      new_weakdef = FALSE;
4177
      if (dynamic
4178
          && definition
4179
          && (flags & BSF_WEAK) != 0
4180
          && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4181
          && is_elf_hash_table (htab)
4182
          && h->u.weakdef == NULL)
4183
        {
4184
          /* Keep a list of all weak defined non function symbols from
4185
             a dynamic object, using the weakdef field.  Later in this
4186
             function we will set the weakdef field to the correct
4187
             value.  We only put non-function symbols from dynamic
4188
             objects on this list, because that happens to be the only
4189
             time we need to know the normal symbol corresponding to a
4190
             weak symbol, and the information is time consuming to
4191
             figure out.  If the weakdef field is not already NULL,
4192
             then this symbol was already defined by some previous
4193
             dynamic object, and we will be using that previous
4194
             definition anyhow.  */
4195
 
4196
          h->u.weakdef = weaks;
4197
          weaks = h;
4198
          new_weakdef = TRUE;
4199
        }
4200
 
4201
      /* Set the alignment of a common symbol.  */
4202
      if ((common || bfd_is_com_section (sec))
4203
          && h->root.type == bfd_link_hash_common)
4204
        {
4205
          unsigned int align;
4206
 
4207
          if (common)
4208
            align = bfd_log2 (isym->st_value);
4209
          else
4210
            {
4211
              /* The new symbol is a common symbol in a shared object.
4212
                 We need to get the alignment from the section.  */
4213
              align = new_sec->alignment_power;
4214
            }
4215
          if (align > old_alignment)
4216
            h->root.u.c.p->alignment_power = align;
4217
          else
4218
            h->root.u.c.p->alignment_power = old_alignment;
4219
        }
4220
 
4221
      if (is_elf_hash_table (htab))
4222
        {
4223
          bfd_boolean dynsym;
4224
 
4225
          /* Check the alignment when a common symbol is involved. This
4226
             can change when a common symbol is overridden by a normal
4227
             definition or a common symbol is ignored due to the old
4228
             normal definition. We need to make sure the maximum
4229
             alignment is maintained.  */
4230
          if ((old_alignment || common)
4231
              && h->root.type != bfd_link_hash_common)
4232
            {
4233
              unsigned int common_align;
4234
              unsigned int normal_align;
4235
              unsigned int symbol_align;
4236
              bfd *normal_bfd;
4237
              bfd *common_bfd;
4238
 
4239
              symbol_align = ffs (h->root.u.def.value) - 1;
4240
              if (h->root.u.def.section->owner != NULL
4241
                  && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4242
                {
4243
                  normal_align = h->root.u.def.section->alignment_power;
4244
                  if (normal_align > symbol_align)
4245
                    normal_align = symbol_align;
4246
                }
4247
              else
4248
                normal_align = symbol_align;
4249
 
4250
              if (old_alignment)
4251
                {
4252
                  common_align = old_alignment;
4253
                  common_bfd = old_bfd;
4254
                  normal_bfd = abfd;
4255
                }
4256
              else
4257
                {
4258
                  common_align = bfd_log2 (isym->st_value);
4259
                  common_bfd = abfd;
4260
                  normal_bfd = old_bfd;
4261
                }
4262
 
4263
              if (normal_align < common_align)
4264
                {
4265
                  /* PR binutils/2735 */
4266
                  if (normal_bfd == NULL)
4267
                    (*_bfd_error_handler)
4268
                      (_("Warning: alignment %u of common symbol `%s' in %B"
4269
                         " is greater than the alignment (%u) of its section %A"),
4270
                       common_bfd, h->root.u.def.section,
4271
                       1 << common_align, name, 1 << normal_align);
4272
                  else
4273
                    (*_bfd_error_handler)
4274
                      (_("Warning: alignment %u of symbol `%s' in %B"
4275
                         " is smaller than %u in %B"),
4276
                       normal_bfd, common_bfd,
4277
                       1 << normal_align, name, 1 << common_align);
4278
                }
4279
            }
4280
 
4281
          /* Remember the symbol size if it isn't undefined.  */
4282
          if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4283
              && (definition || h->size == 0))
4284
            {
4285
              if (h->size != 0
4286
                  && h->size != isym->st_size
4287
                  && ! size_change_ok)
4288
                (*_bfd_error_handler)
4289
                  (_("Warning: size of symbol `%s' changed"
4290
                     " from %lu in %B to %lu in %B"),
4291
                   old_bfd, abfd,
4292
                   name, (unsigned long) h->size,
4293
                   (unsigned long) isym->st_size);
4294
 
4295
              h->size = isym->st_size;
4296
            }
4297
 
4298
          /* If this is a common symbol, then we always want H->SIZE
4299
             to be the size of the common symbol.  The code just above
4300
             won't fix the size if a common symbol becomes larger.  We
4301
             don't warn about a size change here, because that is
4302
             covered by --warn-common.  Allow changed between different
4303
             function types.  */
4304
          if (h->root.type == bfd_link_hash_common)
4305
            h->size = h->root.u.c.size;
4306
 
4307
          if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4308
              && (definition || h->type == STT_NOTYPE))
4309
            {
4310
              unsigned int type = ELF_ST_TYPE (isym->st_info);
4311
 
4312
              /* Turn an IFUNC symbol from a DSO into a normal FUNC
4313
                 symbol.  */
4314
              if (type == STT_GNU_IFUNC
4315
                  && (abfd->flags & DYNAMIC) != 0)
4316
                type = STT_FUNC;
4317
 
4318
              if (h->type != type)
4319
                {
4320
                  if (h->type != STT_NOTYPE && ! type_change_ok)
4321
                    (*_bfd_error_handler)
4322
                      (_("Warning: type of symbol `%s' changed"
4323
                         " from %d to %d in %B"),
4324
                       abfd, name, h->type, type);
4325
 
4326
                  h->type = type;
4327
                }
4328
            }
4329
 
4330
          /* Merge st_other field.  */
4331
          elf_merge_st_other (abfd, h, isym, definition, dynamic);
4332
 
4333
          /* Set a flag in the hash table entry indicating the type of
4334
             reference or definition we just found.  Keep a count of
4335
             the number of dynamic symbols we find.  A dynamic symbol
4336
             is one which is referenced or defined by both a regular
4337
             object and a shared object.  */
4338
          dynsym = FALSE;
4339
          if (! dynamic)
4340
            {
4341
              if (! definition)
4342
                {
4343
                  h->ref_regular = 1;
4344
                  if (bind != STB_WEAK)
4345
                    h->ref_regular_nonweak = 1;
4346
                }
4347
              else
4348
                {
4349
                  h->def_regular = 1;
4350
                  if (h->def_dynamic)
4351
                    {
4352
                      h->def_dynamic = 0;
4353
                      h->ref_dynamic = 1;
4354
                      h->dynamic_def = 1;
4355
                    }
4356
                }
4357
              if (! info->executable
4358
                  || h->def_dynamic
4359
                  || h->ref_dynamic)
4360
                dynsym = TRUE;
4361
            }
4362
          else
4363
            {
4364
              if (! definition)
4365
                h->ref_dynamic = 1;
4366
              else
4367
                h->def_dynamic = 1;
4368
              if (h->def_regular
4369
                  || h->ref_regular
4370
                  || (h->u.weakdef != NULL
4371
                      && ! new_weakdef
4372
                      && h->u.weakdef->dynindx != -1))
4373
                dynsym = TRUE;
4374
            }
4375
 
4376
          if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
4377
            {
4378
              /* We don't want to make debug symbol dynamic.  */
4379
              dynsym = FALSE;
4380
            }
4381
 
4382
          if (definition)
4383
            h->target_internal = isym->st_target_internal;
4384
 
4385
          /* Check to see if we need to add an indirect symbol for
4386
             the default name.  */
4387
          if (definition || h->root.type == bfd_link_hash_common)
4388
            if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4389
                                              &sec, &value, &dynsym,
4390
                                              override))
4391
              goto error_free_vers;
4392
 
4393
          if (definition && !dynamic)
4394
            {
4395
              char *p = strchr (name, ELF_VER_CHR);
4396
              if (p != NULL && p[1] != ELF_VER_CHR)
4397
                {
4398
                  /* Queue non-default versions so that .symver x, x@FOO
4399
                     aliases can be checked.  */
4400
                  if (!nondeflt_vers)
4401
                    {
4402
                      amt = ((isymend - isym + 1)
4403
                             * sizeof (struct elf_link_hash_entry *));
4404
                      nondeflt_vers =
4405
                          (struct elf_link_hash_entry **) bfd_malloc (amt);
4406
                      if (!nondeflt_vers)
4407
                        goto error_free_vers;
4408
                    }
4409
                  nondeflt_vers[nondeflt_vers_cnt++] = h;
4410
                }
4411
            }
4412
 
4413
          if (dynsym && h->dynindx == -1)
4414
            {
4415
              if (! bfd_elf_link_record_dynamic_symbol (info, h))
4416
                goto error_free_vers;
4417
              if (h->u.weakdef != NULL
4418
                  && ! new_weakdef
4419
                  && h->u.weakdef->dynindx == -1)
4420
                {
4421
                  if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4422
                    goto error_free_vers;
4423
                }
4424
            }
4425
          else if (dynsym && h->dynindx != -1)
4426
            /* If the symbol already has a dynamic index, but
4427
               visibility says it should not be visible, turn it into
4428
               a local symbol.  */
4429
            switch (ELF_ST_VISIBILITY (h->other))
4430
              {
4431
              case STV_INTERNAL:
4432
              case STV_HIDDEN:
4433
                (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4434
                dynsym = FALSE;
4435
                break;
4436
              }
4437
 
4438
          if (!add_needed
4439
              && definition
4440
              && ((dynsym
4441
                   && h->ref_regular)
4442
                  || (h->ref_dynamic
4443
                      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4444
                      && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4445
            {
4446
              int ret;
4447
              const char *soname = elf_dt_name (abfd);
4448
 
4449
              /* A symbol from a library loaded via DT_NEEDED of some
4450
                 other library is referenced by a regular object.
4451
                 Add a DT_NEEDED entry for it.  Issue an error if
4452
                 --no-add-needed is used and the reference was not
4453
                 a weak one.  */
4454
              if (undef_bfd != NULL
4455
                  && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4456
                {
4457
                  (*_bfd_error_handler)
4458
                    (_("%B: undefined reference to symbol '%s'"),
4459
                     undef_bfd, name);
4460
                  (*_bfd_error_handler)
4461
                    (_("note: '%s' is defined in DSO %B so try adding it to the linker command line"),
4462
                     abfd, name);
4463
                  bfd_set_error (bfd_error_invalid_operation);
4464
                  goto error_free_vers;
4465
                }
4466
 
4467
              elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4468
                  (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4469
 
4470
              add_needed = TRUE;
4471
              ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4472
              if (ret < 0)
4473
                goto error_free_vers;
4474
 
4475
              BFD_ASSERT (ret == 0);
4476
            }
4477
        }
4478
    }
4479
 
4480
  if (extversym != NULL)
4481
    {
4482
      free (extversym);
4483
      extversym = NULL;
4484
    }
4485
 
4486
  if (isymbuf != NULL)
4487
    {
4488
      free (isymbuf);
4489
      isymbuf = NULL;
4490
    }
4491
 
4492
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4493
    {
4494
      unsigned int i;
4495
 
4496
      /* Restore the symbol table.  */
4497
      if (bed->as_needed_cleanup)
4498
        (*bed->as_needed_cleanup) (abfd, info);
4499
      old_hash = (char *) old_tab + tabsize;
4500
      old_ent = (char *) old_hash + hashsize;
4501
      sym_hash = elf_sym_hashes (abfd);
4502
      htab->root.table.table = old_table;
4503
      htab->root.table.size = old_size;
4504
      htab->root.table.count = old_count;
4505
      memcpy (htab->root.table.table, old_tab, tabsize);
4506
      memcpy (sym_hash, old_hash, hashsize);
4507
      htab->root.undefs = old_undefs;
4508
      htab->root.undefs_tail = old_undefs_tail;
4509
      for (i = 0; i < htab->root.table.size; i++)
4510
        {
4511
          struct bfd_hash_entry *p;
4512
          struct elf_link_hash_entry *h;
4513
 
4514
          for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4515
            {
4516
              h = (struct elf_link_hash_entry *) p;
4517
              if (h->root.type == bfd_link_hash_warning)
4518
                h = (struct elf_link_hash_entry *) h->root.u.i.link;
4519
              if (h->dynindx >= old_dynsymcount)
4520
                _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4521
 
4522
              memcpy (p, old_ent, htab->root.table.entsize);
4523
              old_ent = (char *) old_ent + htab->root.table.entsize;
4524
              h = (struct elf_link_hash_entry *) p;
4525
              if (h->root.type == bfd_link_hash_warning)
4526
                {
4527
                  memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4528
                  old_ent = (char *) old_ent + htab->root.table.entsize;
4529
                }
4530
            }
4531
        }
4532
 
4533
      /* Make a special call to the linker "notice" function to
4534
         tell it that symbols added for crefs may need to be removed.  */
4535
      if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4536
                                       notice_not_needed, 0, NULL))
4537
        goto error_free_vers;
4538
 
4539
      free (old_tab);
4540
      objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4541
                           alloc_mark);
4542
      if (nondeflt_vers != NULL)
4543
        free (nondeflt_vers);
4544
      return TRUE;
4545
    }
4546
 
4547
  if (old_tab != NULL)
4548
    {
4549
      if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4550
                                       notice_needed, 0, NULL))
4551
        goto error_free_vers;
4552
      free (old_tab);
4553
      old_tab = NULL;
4554
    }
4555
 
4556
  /* Now that all the symbols from this input file are created, handle
4557
     .symver foo, foo@BAR such that any relocs against foo become foo@BAR.  */
4558
  if (nondeflt_vers != NULL)
4559
    {
4560
      bfd_size_type cnt, symidx;
4561
 
4562
      for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4563
        {
4564
          struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4565
          char *shortname, *p;
4566
 
4567
          p = strchr (h->root.root.string, ELF_VER_CHR);
4568
          if (p == NULL
4569
              || (h->root.type != bfd_link_hash_defined
4570
                  && h->root.type != bfd_link_hash_defweak))
4571
            continue;
4572
 
4573
          amt = p - h->root.root.string;
4574
          shortname = (char *) bfd_malloc (amt + 1);
4575
          if (!shortname)
4576
            goto error_free_vers;
4577
          memcpy (shortname, h->root.root.string, amt);
4578
          shortname[amt] = '\0';
4579
 
4580
          hi = (struct elf_link_hash_entry *)
4581
               bfd_link_hash_lookup (&htab->root, shortname,
4582
                                     FALSE, FALSE, FALSE);
4583
          if (hi != NULL
4584
              && hi->root.type == h->root.type
4585
              && hi->root.u.def.value == h->root.u.def.value
4586
              && hi->root.u.def.section == h->root.u.def.section)
4587
            {
4588
              (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4589
              hi->root.type = bfd_link_hash_indirect;
4590
              hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4591
              (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4592
              sym_hash = elf_sym_hashes (abfd);
4593
              if (sym_hash)
4594
                for (symidx = 0; symidx < extsymcount; ++symidx)
4595
                  if (sym_hash[symidx] == hi)
4596
                    {
4597
                      sym_hash[symidx] = h;
4598
                      break;
4599
                    }
4600
            }
4601
          free (shortname);
4602
        }
4603
      free (nondeflt_vers);
4604
      nondeflt_vers = NULL;
4605
    }
4606
 
4607
  /* Now set the weakdefs field correctly for all the weak defined
4608
     symbols we found.  The only way to do this is to search all the
4609
     symbols.  Since we only need the information for non functions in
4610
     dynamic objects, that's the only time we actually put anything on
4611
     the list WEAKS.  We need this information so that if a regular
4612
     object refers to a symbol defined weakly in a dynamic object, the
4613
     real symbol in the dynamic object is also put in the dynamic
4614
     symbols; we also must arrange for both symbols to point to the
4615
     same memory location.  We could handle the general case of symbol
4616
     aliasing, but a general symbol alias can only be generated in
4617
     assembler code, handling it correctly would be very time
4618
     consuming, and other ELF linkers don't handle general aliasing
4619
     either.  */
4620
  if (weaks != NULL)
4621
    {
4622
      struct elf_link_hash_entry **hpp;
4623
      struct elf_link_hash_entry **hppend;
4624
      struct elf_link_hash_entry **sorted_sym_hash;
4625
      struct elf_link_hash_entry *h;
4626
      size_t sym_count;
4627
 
4628
      /* Since we have to search the whole symbol list for each weak
4629
         defined symbol, search time for N weak defined symbols will be
4630
         O(N^2). Binary search will cut it down to O(NlogN).  */
4631
      amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4632
      sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4633
      if (sorted_sym_hash == NULL)
4634
        goto error_return;
4635
      sym_hash = sorted_sym_hash;
4636
      hpp = elf_sym_hashes (abfd);
4637
      hppend = hpp + extsymcount;
4638
      sym_count = 0;
4639
      for (; hpp < hppend; hpp++)
4640
        {
4641
          h = *hpp;
4642
          if (h != NULL
4643
              && h->root.type == bfd_link_hash_defined
4644
              && !bed->is_function_type (h->type))
4645
            {
4646
              *sym_hash = h;
4647
              sym_hash++;
4648
              sym_count++;
4649
            }
4650
        }
4651
 
4652
      qsort (sorted_sym_hash, sym_count,
4653
             sizeof (struct elf_link_hash_entry *),
4654
             elf_sort_symbol);
4655
 
4656
      while (weaks != NULL)
4657
        {
4658
          struct elf_link_hash_entry *hlook;
4659
          asection *slook;
4660
          bfd_vma vlook;
4661
          long ilook;
4662
          size_t i, j, idx;
4663
 
4664
          hlook = weaks;
4665
          weaks = hlook->u.weakdef;
4666
          hlook->u.weakdef = NULL;
4667
 
4668
          BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4669
                      || hlook->root.type == bfd_link_hash_defweak
4670
                      || hlook->root.type == bfd_link_hash_common
4671
                      || hlook->root.type == bfd_link_hash_indirect);
4672
          slook = hlook->root.u.def.section;
4673
          vlook = hlook->root.u.def.value;
4674
 
4675
          ilook = -1;
4676
          i = 0;
4677
          j = sym_count;
4678
          while (i < j)
4679
            {
4680
              bfd_signed_vma vdiff;
4681
              idx = (i + j) / 2;
4682
              h = sorted_sym_hash [idx];
4683
              vdiff = vlook - h->root.u.def.value;
4684
              if (vdiff < 0)
4685
                j = idx;
4686
              else if (vdiff > 0)
4687
                i = idx + 1;
4688
              else
4689
                {
4690
                  long sdiff = slook->id - h->root.u.def.section->id;
4691
                  if (sdiff < 0)
4692
                    j = idx;
4693
                  else if (sdiff > 0)
4694
                    i = idx + 1;
4695
                  else
4696
                    {
4697
                      ilook = idx;
4698
                      break;
4699
                    }
4700
                }
4701
            }
4702
 
4703
          /* We didn't find a value/section match.  */
4704
          if (ilook == -1)
4705
            continue;
4706
 
4707
          for (i = ilook; i < sym_count; i++)
4708
            {
4709
              h = sorted_sym_hash [i];
4710
 
4711
              /* Stop if value or section doesn't match.  */
4712
              if (h->root.u.def.value != vlook
4713
                  || h->root.u.def.section != slook)
4714
                break;
4715
              else if (h != hlook)
4716
                {
4717
                  hlook->u.weakdef = h;
4718
 
4719
                  /* If the weak definition is in the list of dynamic
4720
                     symbols, make sure the real definition is put
4721
                     there as well.  */
4722
                  if (hlook->dynindx != -1 && h->dynindx == -1)
4723
                    {
4724
                      if (! bfd_elf_link_record_dynamic_symbol (info, h))
4725
                        {
4726
                        err_free_sym_hash:
4727
                          free (sorted_sym_hash);
4728
                          goto error_return;
4729
                        }
4730
                    }
4731
 
4732
                  /* If the real definition is in the list of dynamic
4733
                     symbols, make sure the weak definition is put
4734
                     there as well.  If we don't do this, then the
4735
                     dynamic loader might not merge the entries for the
4736
                     real definition and the weak definition.  */
4737
                  if (h->dynindx != -1 && hlook->dynindx == -1)
4738
                    {
4739
                      if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4740
                        goto err_free_sym_hash;
4741
                    }
4742
                  break;
4743
                }
4744
            }
4745
        }
4746
 
4747
      free (sorted_sym_hash);
4748
    }
4749
 
4750
  if (bed->check_directives
4751
      && !(*bed->check_directives) (abfd, info))
4752
    return FALSE;
4753
 
4754
  /* If this object is the same format as the output object, and it is
4755
     not a shared library, then let the backend look through the
4756
     relocs.
4757
 
4758
     This is required to build global offset table entries and to
4759
     arrange for dynamic relocs.  It is not required for the
4760
     particular common case of linking non PIC code, even when linking
4761
     against shared libraries, but unfortunately there is no way of
4762
     knowing whether an object file has been compiled PIC or not.
4763
     Looking through the relocs is not particularly time consuming.
4764
     The problem is that we must either (1) keep the relocs in memory,
4765
     which causes the linker to require additional runtime memory or
4766
     (2) read the relocs twice from the input file, which wastes time.
4767
     This would be a good case for using mmap.
4768
 
4769
     I have no idea how to handle linking PIC code into a file of a
4770
     different format.  It probably can't be done.  */
4771
  if (! dynamic
4772
      && is_elf_hash_table (htab)
4773
      && bed->check_relocs != NULL
4774
      && elf_object_id (abfd) == elf_hash_table_id (htab)
4775
      && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4776
    {
4777
      asection *o;
4778
 
4779
      for (o = abfd->sections; o != NULL; o = o->next)
4780
        {
4781
          Elf_Internal_Rela *internal_relocs;
4782
          bfd_boolean ok;
4783
 
4784
          if ((o->flags & SEC_RELOC) == 0
4785
              || o->reloc_count == 0
4786
              || ((info->strip == strip_all || info->strip == strip_debugger)
4787
                  && (o->flags & SEC_DEBUGGING) != 0)
4788
              || bfd_is_abs_section (o->output_section))
4789
            continue;
4790
 
4791
          internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4792
                                                       info->keep_memory);
4793
          if (internal_relocs == NULL)
4794
            goto error_return;
4795
 
4796
          ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4797
 
4798
          if (elf_section_data (o)->relocs != internal_relocs)
4799
            free (internal_relocs);
4800
 
4801
          if (! ok)
4802
            goto error_return;
4803
        }
4804
    }
4805
 
4806
  /* If this is a non-traditional link, try to optimize the handling
4807
     of the .stab/.stabstr sections.  */
4808
  if (! dynamic
4809
      && ! info->traditional_format
4810
      && is_elf_hash_table (htab)
4811
      && (info->strip != strip_all && info->strip != strip_debugger))
4812
    {
4813
      asection *stabstr;
4814
 
4815
      stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4816
      if (stabstr != NULL)
4817
        {
4818
          bfd_size_type string_offset = 0;
4819
          asection *stab;
4820
 
4821
          for (stab = abfd->sections; stab; stab = stab->next)
4822
            if (CONST_STRNEQ (stab->name, ".stab")
4823
                && (!stab->name[5] ||
4824
                    (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4825
                && (stab->flags & SEC_MERGE) == 0
4826
                && !bfd_is_abs_section (stab->output_section))
4827
              {
4828
                struct bfd_elf_section_data *secdata;
4829
 
4830
                secdata = elf_section_data (stab);
4831
                if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4832
                                               stabstr, &secdata->sec_info,
4833
                                               &string_offset))
4834
                  goto error_return;
4835
                if (secdata->sec_info)
4836
                  stab->sec_info_type = ELF_INFO_TYPE_STABS;
4837
            }
4838
        }
4839
    }
4840
 
4841
  if (is_elf_hash_table (htab) && add_needed)
4842
    {
4843
      /* Add this bfd to the loaded list.  */
4844
      struct elf_link_loaded_list *n;
4845
 
4846
      n = (struct elf_link_loaded_list *)
4847
          bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4848
      if (n == NULL)
4849
        goto error_return;
4850
      n->abfd = abfd;
4851
      n->next = htab->loaded;
4852
      htab->loaded = n;
4853
    }
4854
 
4855
  return TRUE;
4856
 
4857
 error_free_vers:
4858
  if (old_tab != NULL)
4859
    free (old_tab);
4860
  if (nondeflt_vers != NULL)
4861
    free (nondeflt_vers);
4862
  if (extversym != NULL)
4863
    free (extversym);
4864
 error_free_sym:
4865
  if (isymbuf != NULL)
4866
    free (isymbuf);
4867
 error_return:
4868
  return FALSE;
4869
}
4870
 
4871
/* Return the linker hash table entry of a symbol that might be
4872
   satisfied by an archive symbol.  Return -1 on error.  */
4873
 
4874
struct elf_link_hash_entry *
4875
_bfd_elf_archive_symbol_lookup (bfd *abfd,
4876
                                struct bfd_link_info *info,
4877
                                const char *name)
4878
{
4879
  struct elf_link_hash_entry *h;
4880
  char *p, *copy;
4881
  size_t len, first;
4882
 
4883 148 khays
  h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
4884 14 khays
  if (h != NULL)
4885
    return h;
4886
 
4887
  /* If this is a default version (the name contains @@), look up the
4888
     symbol again with only one `@' as well as without the version.
4889
     The effect is that references to the symbol with and without the
4890
     version will be matched by the default symbol in the archive.  */
4891
 
4892
  p = strchr (name, ELF_VER_CHR);
4893
  if (p == NULL || p[1] != ELF_VER_CHR)
4894
    return h;
4895
 
4896
  /* First check with only one `@'.  */
4897
  len = strlen (name);
4898
  copy = (char *) bfd_alloc (abfd, len);
4899
  if (copy == NULL)
4900
    return (struct elf_link_hash_entry *) 0 - 1;
4901
 
4902
  first = p - name + 1;
4903
  memcpy (copy, name, first);
4904
  memcpy (copy + first, name + first + 1, len - first);
4905
 
4906 148 khays
  h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
4907 14 khays
  if (h == NULL)
4908
    {
4909
      /* We also need to check references to the symbol without the
4910
         version.  */
4911
      copy[first - 1] = '\0';
4912
      h = elf_link_hash_lookup (elf_hash_table (info), copy,
4913 148 khays
                                FALSE, FALSE, TRUE);
4914 14 khays
    }
4915
 
4916
  bfd_release (abfd, copy);
4917
  return h;
4918
}
4919
 
4920
/* Add symbols from an ELF archive file to the linker hash table.  We
4921
   don't use _bfd_generic_link_add_archive_symbols because of a
4922
   problem which arises on UnixWare.  The UnixWare libc.so is an
4923
   archive which includes an entry libc.so.1 which defines a bunch of
4924
   symbols.  The libc.so archive also includes a number of other
4925
   object files, which also define symbols, some of which are the same
4926
   as those defined in libc.so.1.  Correct linking requires that we
4927
   consider each object file in turn, and include it if it defines any
4928
   symbols we need.  _bfd_generic_link_add_archive_symbols does not do
4929
   this; it looks through the list of undefined symbols, and includes
4930
   any object file which defines them.  When this algorithm is used on
4931
   UnixWare, it winds up pulling in libc.so.1 early and defining a
4932
   bunch of symbols.  This means that some of the other objects in the
4933
   archive are not included in the link, which is incorrect since they
4934
   precede libc.so.1 in the archive.
4935
 
4936
   Fortunately, ELF archive handling is simpler than that done by
4937
   _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4938
   oddities.  In ELF, if we find a symbol in the archive map, and the
4939
   symbol is currently undefined, we know that we must pull in that
4940
   object file.
4941
 
4942
   Unfortunately, we do have to make multiple passes over the symbol
4943
   table until nothing further is resolved.  */
4944
 
4945
static bfd_boolean
4946
elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4947
{
4948
  symindex c;
4949
  bfd_boolean *defined = NULL;
4950
  bfd_boolean *included = NULL;
4951
  carsym *symdefs;
4952
  bfd_boolean loop;
4953
  bfd_size_type amt;
4954
  const struct elf_backend_data *bed;
4955
  struct elf_link_hash_entry * (*archive_symbol_lookup)
4956
    (bfd *, struct bfd_link_info *, const char *);
4957
 
4958
  if (! bfd_has_map (abfd))
4959
    {
4960
      /* An empty archive is a special case.  */
4961
      if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4962
        return TRUE;
4963
      bfd_set_error (bfd_error_no_armap);
4964
      return FALSE;
4965
    }
4966
 
4967
  /* Keep track of all symbols we know to be already defined, and all
4968
     files we know to be already included.  This is to speed up the
4969
     second and subsequent passes.  */
4970
  c = bfd_ardata (abfd)->symdef_count;
4971
  if (c == 0)
4972
    return TRUE;
4973
  amt = c;
4974
  amt *= sizeof (bfd_boolean);
4975
  defined = (bfd_boolean *) bfd_zmalloc (amt);
4976
  included = (bfd_boolean *) bfd_zmalloc (amt);
4977
  if (defined == NULL || included == NULL)
4978
    goto error_return;
4979
 
4980
  symdefs = bfd_ardata (abfd)->symdefs;
4981
  bed = get_elf_backend_data (abfd);
4982
  archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4983
 
4984
  do
4985
    {
4986
      file_ptr last;
4987
      symindex i;
4988
      carsym *symdef;
4989
      carsym *symdefend;
4990
 
4991
      loop = FALSE;
4992
      last = -1;
4993
 
4994
      symdef = symdefs;
4995
      symdefend = symdef + c;
4996
      for (i = 0; symdef < symdefend; symdef++, i++)
4997
        {
4998
          struct elf_link_hash_entry *h;
4999
          bfd *element;
5000
          struct bfd_link_hash_entry *undefs_tail;
5001
          symindex mark;
5002
 
5003
          if (defined[i] || included[i])
5004
            continue;
5005
          if (symdef->file_offset == last)
5006
            {
5007
              included[i] = TRUE;
5008
              continue;
5009
            }
5010
 
5011
          h = archive_symbol_lookup (abfd, info, symdef->name);
5012
          if (h == (struct elf_link_hash_entry *) 0 - 1)
5013
            goto error_return;
5014
 
5015
          if (h == NULL)
5016
            continue;
5017
 
5018
          if (h->root.type == bfd_link_hash_common)
5019
            {
5020
              /* We currently have a common symbol.  The archive map contains
5021
                 a reference to this symbol, so we may want to include it.  We
5022
                 only want to include it however, if this archive element
5023
                 contains a definition of the symbol, not just another common
5024
                 declaration of it.
5025
 
5026
                 Unfortunately some archivers (including GNU ar) will put
5027
                 declarations of common symbols into their archive maps, as
5028
                 well as real definitions, so we cannot just go by the archive
5029
                 map alone.  Instead we must read in the element's symbol
5030
                 table and check that to see what kind of symbol definition
5031
                 this is.  */
5032
              if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5033
                continue;
5034
            }
5035
          else if (h->root.type != bfd_link_hash_undefined)
5036
            {
5037
              if (h->root.type != bfd_link_hash_undefweak)
5038
                defined[i] = TRUE;
5039
              continue;
5040
            }
5041
 
5042
          /* We need to include this archive member.  */
5043
          element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5044
          if (element == NULL)
5045
            goto error_return;
5046
 
5047
          if (! bfd_check_format (element, bfd_object))
5048
            goto error_return;
5049
 
5050
          /* Doublecheck that we have not included this object
5051
             already--it should be impossible, but there may be
5052
             something wrong with the archive.  */
5053
          if (element->archive_pass != 0)
5054
            {
5055
              bfd_set_error (bfd_error_bad_value);
5056
              goto error_return;
5057
            }
5058
          element->archive_pass = 1;
5059
 
5060
          undefs_tail = info->hash->undefs_tail;
5061
 
5062
          if (!(*info->callbacks
5063
                ->add_archive_element) (info, element, symdef->name, &element))
5064
            goto error_return;
5065
          if (!bfd_link_add_symbols (element, info))
5066
            goto error_return;
5067
 
5068
          /* If there are any new undefined symbols, we need to make
5069
             another pass through the archive in order to see whether
5070
             they can be defined.  FIXME: This isn't perfect, because
5071
             common symbols wind up on undefs_tail and because an
5072
             undefined symbol which is defined later on in this pass
5073
             does not require another pass.  This isn't a bug, but it
5074
             does make the code less efficient than it could be.  */
5075
          if (undefs_tail != info->hash->undefs_tail)
5076
            loop = TRUE;
5077
 
5078
          /* Look backward to mark all symbols from this object file
5079
             which we have already seen in this pass.  */
5080
          mark = i;
5081
          do
5082
            {
5083
              included[mark] = TRUE;
5084
              if (mark == 0)
5085
                break;
5086
              --mark;
5087
            }
5088
          while (symdefs[mark].file_offset == symdef->file_offset);
5089
 
5090
          /* We mark subsequent symbols from this object file as we go
5091
             on through the loop.  */
5092
          last = symdef->file_offset;
5093
        }
5094
    }
5095
  while (loop);
5096
 
5097
  free (defined);
5098
  free (included);
5099
 
5100
  return TRUE;
5101
 
5102
 error_return:
5103
  if (defined != NULL)
5104
    free (defined);
5105
  if (included != NULL)
5106
    free (included);
5107
  return FALSE;
5108
}
5109
 
5110
/* Given an ELF BFD, add symbols to the global hash table as
5111
   appropriate.  */
5112
 
5113
bfd_boolean
5114
bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5115
{
5116
  switch (bfd_get_format (abfd))
5117
    {
5118
    case bfd_object:
5119
      return elf_link_add_object_symbols (abfd, info);
5120
    case bfd_archive:
5121
      return elf_link_add_archive_symbols (abfd, info);
5122
    default:
5123
      bfd_set_error (bfd_error_wrong_format);
5124
      return FALSE;
5125
    }
5126
}
5127
 
5128
struct hash_codes_info
5129
{
5130
  unsigned long *hashcodes;
5131
  bfd_boolean error;
5132
};
5133
 
5134
/* This function will be called though elf_link_hash_traverse to store
5135
   all hash value of the exported symbols in an array.  */
5136
 
5137
static bfd_boolean
5138
elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5139
{
5140
  struct hash_codes_info *inf = (struct hash_codes_info *) data;
5141
  const char *name;
5142
  char *p;
5143
  unsigned long ha;
5144
  char *alc = NULL;
5145
 
5146
  /* Ignore indirect symbols.  These are added by the versioning code.  */
5147
  if (h->dynindx == -1)
5148
    return TRUE;
5149
 
5150
  name = h->root.root.string;
5151
  p = strchr (name, ELF_VER_CHR);
5152
  if (p != NULL)
5153
    {
5154
      alc = (char *) bfd_malloc (p - name + 1);
5155
      if (alc == NULL)
5156
        {
5157
          inf->error = TRUE;
5158
          return FALSE;
5159
        }
5160
      memcpy (alc, name, p - name);
5161
      alc[p - name] = '\0';
5162
      name = alc;
5163
    }
5164
 
5165
  /* Compute the hash value.  */
5166
  ha = bfd_elf_hash (name);
5167
 
5168
  /* Store the found hash value in the array given as the argument.  */
5169
  *(inf->hashcodes)++ = ha;
5170
 
5171
  /* And store it in the struct so that we can put it in the hash table
5172
     later.  */
5173
  h->u.elf_hash_value = ha;
5174
 
5175
  if (alc != NULL)
5176
    free (alc);
5177
 
5178
  return TRUE;
5179
}
5180
 
5181
struct collect_gnu_hash_codes
5182
{
5183
  bfd *output_bfd;
5184
  const struct elf_backend_data *bed;
5185
  unsigned long int nsyms;
5186
  unsigned long int maskbits;
5187
  unsigned long int *hashcodes;
5188
  unsigned long int *hashval;
5189
  unsigned long int *indx;
5190
  unsigned long int *counts;
5191
  bfd_vma *bitmask;
5192
  bfd_byte *contents;
5193
  long int min_dynindx;
5194
  unsigned long int bucketcount;
5195
  unsigned long int symindx;
5196
  long int local_indx;
5197
  long int shift1, shift2;
5198
  unsigned long int mask;
5199
  bfd_boolean error;
5200
};
5201
 
5202
/* This function will be called though elf_link_hash_traverse to store
5203
   all hash value of the exported symbols in an array.  */
5204
 
5205
static bfd_boolean
5206
elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5207
{
5208
  struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5209
  const char *name;
5210
  char *p;
5211
  unsigned long ha;
5212
  char *alc = NULL;
5213
 
5214
  /* Ignore indirect symbols.  These are added by the versioning code.  */
5215
  if (h->dynindx == -1)
5216
    return TRUE;
5217
 
5218
  /* Ignore also local symbols and undefined symbols.  */
5219
  if (! (*s->bed->elf_hash_symbol) (h))
5220
    return TRUE;
5221
 
5222
  name = h->root.root.string;
5223
  p = strchr (name, ELF_VER_CHR);
5224
  if (p != NULL)
5225
    {
5226
      alc = (char *) bfd_malloc (p - name + 1);
5227
      if (alc == NULL)
5228
        {
5229
          s->error = TRUE;
5230
          return FALSE;
5231
        }
5232
      memcpy (alc, name, p - name);
5233
      alc[p - name] = '\0';
5234
      name = alc;
5235
    }
5236
 
5237
  /* Compute the hash value.  */
5238
  ha = bfd_elf_gnu_hash (name);
5239
 
5240
  /* Store the found hash value in the array for compute_bucket_count,
5241
     and also for .dynsym reordering purposes.  */
5242
  s->hashcodes[s->nsyms] = ha;
5243
  s->hashval[h->dynindx] = ha;
5244
  ++s->nsyms;
5245
  if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5246
    s->min_dynindx = h->dynindx;
5247
 
5248
  if (alc != NULL)
5249
    free (alc);
5250
 
5251
  return TRUE;
5252
}
5253
 
5254
/* This function will be called though elf_link_hash_traverse to do
5255
   final dynaminc symbol renumbering.  */
5256
 
5257
static bfd_boolean
5258
elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5259
{
5260
  struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5261
  unsigned long int bucket;
5262
  unsigned long int val;
5263
 
5264
  /* Ignore indirect symbols.  */
5265
  if (h->dynindx == -1)
5266
    return TRUE;
5267
 
5268
  /* Ignore also local symbols and undefined symbols.  */
5269
  if (! (*s->bed->elf_hash_symbol) (h))
5270
    {
5271
      if (h->dynindx >= s->min_dynindx)
5272
        h->dynindx = s->local_indx++;
5273
      return TRUE;
5274
    }
5275
 
5276
  bucket = s->hashval[h->dynindx] % s->bucketcount;
5277
  val = (s->hashval[h->dynindx] >> s->shift1)
5278
        & ((s->maskbits >> s->shift1) - 1);
5279
  s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5280
  s->bitmask[val]
5281
    |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5282
  val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5283
  if (s->counts[bucket] == 1)
5284
    /* Last element terminates the chain.  */
5285
    val |= 1;
5286
  bfd_put_32 (s->output_bfd, val,
5287
              s->contents + (s->indx[bucket] - s->symindx) * 4);
5288
  --s->counts[bucket];
5289
  h->dynindx = s->indx[bucket]++;
5290
  return TRUE;
5291
}
5292
 
5293
/* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
5294
 
5295
bfd_boolean
5296
_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5297
{
5298
  return !(h->forced_local
5299
           || h->root.type == bfd_link_hash_undefined
5300
           || h->root.type == bfd_link_hash_undefweak
5301
           || ((h->root.type == bfd_link_hash_defined
5302
                || h->root.type == bfd_link_hash_defweak)
5303
               && h->root.u.def.section->output_section == NULL));
5304
}
5305
 
5306
/* Array used to determine the number of hash table buckets to use
5307
   based on the number of symbols there are.  If there are fewer than
5308
   3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5309
   fewer than 37 we use 17 buckets, and so forth.  We never use more
5310
   than 32771 buckets.  */
5311
 
5312
static const size_t elf_buckets[] =
5313
{
5314
  1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5315
  16411, 32771, 0
5316
};
5317
 
5318
/* Compute bucket count for hashing table.  We do not use a static set
5319
   of possible tables sizes anymore.  Instead we determine for all
5320
   possible reasonable sizes of the table the outcome (i.e., the
5321
   number of collisions etc) and choose the best solution.  The
5322
   weighting functions are not too simple to allow the table to grow
5323
   without bounds.  Instead one of the weighting factors is the size.
5324
   Therefore the result is always a good payoff between few collisions
5325
   (= short chain lengths) and table size.  */
5326
static size_t
5327
compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5328
                      unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5329
                      unsigned long int nsyms,
5330
                      int gnu_hash)
5331
{
5332
  size_t best_size = 0;
5333
  unsigned long int i;
5334
 
5335
  /* We have a problem here.  The following code to optimize the table
5336
     size requires an integer type with more the 32 bits.  If
5337
     BFD_HOST_U_64_BIT is set we know about such a type.  */
5338
#ifdef BFD_HOST_U_64_BIT
5339
  if (info->optimize)
5340
    {
5341
      size_t minsize;
5342
      size_t maxsize;
5343
      BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5344
      bfd *dynobj = elf_hash_table (info)->dynobj;
5345
      size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5346
      const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5347
      unsigned long int *counts;
5348
      bfd_size_type amt;
5349
      unsigned int no_improvement_count = 0;
5350
 
5351
      /* Possible optimization parameters: if we have NSYMS symbols we say
5352
         that the hashing table must at least have NSYMS/4 and at most
5353
         2*NSYMS buckets.  */
5354
      minsize = nsyms / 4;
5355
      if (minsize == 0)
5356
        minsize = 1;
5357
      best_size = maxsize = nsyms * 2;
5358
      if (gnu_hash)
5359
        {
5360
          if (minsize < 2)
5361
            minsize = 2;
5362
          if ((best_size & 31) == 0)
5363
            ++best_size;
5364
        }
5365
 
5366
      /* Create array where we count the collisions in.  We must use bfd_malloc
5367
         since the size could be large.  */
5368
      amt = maxsize;
5369
      amt *= sizeof (unsigned long int);
5370
      counts = (unsigned long int *) bfd_malloc (amt);
5371
      if (counts == NULL)
5372
        return 0;
5373
 
5374
      /* Compute the "optimal" size for the hash table.  The criteria is a
5375
         minimal chain length.  The minor criteria is (of course) the size
5376
         of the table.  */
5377
      for (i = minsize; i < maxsize; ++i)
5378
        {
5379
          /* Walk through the array of hashcodes and count the collisions.  */
5380
          BFD_HOST_U_64_BIT max;
5381
          unsigned long int j;
5382
          unsigned long int fact;
5383
 
5384
          if (gnu_hash && (i & 31) == 0)
5385
            continue;
5386
 
5387
          memset (counts, '\0', i * sizeof (unsigned long int));
5388
 
5389
          /* Determine how often each hash bucket is used.  */
5390
          for (j = 0; j < nsyms; ++j)
5391
            ++counts[hashcodes[j] % i];
5392
 
5393
          /* For the weight function we need some information about the
5394
             pagesize on the target.  This is information need not be 100%
5395
             accurate.  Since this information is not available (so far) we
5396
             define it here to a reasonable default value.  If it is crucial
5397
             to have a better value some day simply define this value.  */
5398
# ifndef BFD_TARGET_PAGESIZE
5399
#  define BFD_TARGET_PAGESIZE   (4096)
5400
# endif
5401
 
5402
          /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5403
             and the chains.  */
5404
          max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5405
 
5406
# if 1
5407
          /* Variant 1: optimize for short chains.  We add the squares
5408
             of all the chain lengths (which favors many small chain
5409
             over a few long chains).  */
5410
          for (j = 0; j < i; ++j)
5411
            max += counts[j] * counts[j];
5412
 
5413
          /* This adds penalties for the overall size of the table.  */
5414
          fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5415
          max *= fact * fact;
5416
# else
5417
          /* Variant 2: Optimize a lot more for small table.  Here we
5418
             also add squares of the size but we also add penalties for
5419
             empty slots (the +1 term).  */
5420
          for (j = 0; j < i; ++j)
5421
            max += (1 + counts[j]) * (1 + counts[j]);
5422
 
5423
          /* The overall size of the table is considered, but not as
5424
             strong as in variant 1, where it is squared.  */
5425
          fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5426
          max *= fact;
5427
# endif
5428
 
5429
          /* Compare with current best results.  */
5430
          if (max < best_chlen)
5431
            {
5432
              best_chlen = max;
5433
              best_size = i;
5434
              no_improvement_count = 0;
5435
            }
5436
          /* PR 11843: Avoid futile long searches for the best bucket size
5437
             when there are a large number of symbols.  */
5438
          else if (++no_improvement_count == 100)
5439
            break;
5440
        }
5441
 
5442
      free (counts);
5443
    }
5444
  else
5445
#endif /* defined (BFD_HOST_U_64_BIT) */
5446
    {
5447
      /* This is the fallback solution if no 64bit type is available or if we
5448
         are not supposed to spend much time on optimizations.  We select the
5449
         bucket count using a fixed set of numbers.  */
5450
      for (i = 0; elf_buckets[i] != 0; i++)
5451
        {
5452
          best_size = elf_buckets[i];
5453
          if (nsyms < elf_buckets[i + 1])
5454
            break;
5455
        }
5456
      if (gnu_hash && best_size < 2)
5457
        best_size = 2;
5458
    }
5459
 
5460
  return best_size;
5461
}
5462
 
5463
/* Size any SHT_GROUP section for ld -r.  */
5464
 
5465
bfd_boolean
5466
_bfd_elf_size_group_sections (struct bfd_link_info *info)
5467
{
5468
  bfd *ibfd;
5469
 
5470
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5471
    if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5472
        && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5473
      return FALSE;
5474
  return TRUE;
5475
}
5476
 
5477
/* Set up the sizes and contents of the ELF dynamic sections.  This is
5478
   called by the ELF linker emulation before_allocation routine.  We
5479
   must set the sizes of the sections before the linker sets the
5480
   addresses of the various sections.  */
5481
 
5482
bfd_boolean
5483
bfd_elf_size_dynamic_sections (bfd *output_bfd,
5484
                               const char *soname,
5485
                               const char *rpath,
5486
                               const char *filter_shlib,
5487
                               const char *audit,
5488
                               const char *depaudit,
5489
                               const char * const *auxiliary_filters,
5490
                               struct bfd_link_info *info,
5491
                               asection **sinterpptr,
5492
                               struct bfd_elf_version_tree *verdefs)
5493
{
5494
  bfd_size_type soname_indx;
5495
  bfd *dynobj;
5496
  const struct elf_backend_data *bed;
5497
  struct elf_info_failed asvinfo;
5498
 
5499
  *sinterpptr = NULL;
5500
 
5501
  soname_indx = (bfd_size_type) -1;
5502
 
5503
  if (!is_elf_hash_table (info->hash))
5504
    return TRUE;
5505
 
5506
  bed = get_elf_backend_data (output_bfd);
5507
  if (info->execstack)
5508
    elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5509
  else if (info->noexecstack)
5510
    elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5511
  else
5512
    {
5513
      bfd *inputobj;
5514
      asection *notesec = NULL;
5515
      int exec = 0;
5516
 
5517
      for (inputobj = info->input_bfds;
5518
           inputobj;
5519
           inputobj = inputobj->link_next)
5520
        {
5521
          asection *s;
5522
 
5523
          if (inputobj->flags & (DYNAMIC | EXEC_P | BFD_LINKER_CREATED))
5524
            continue;
5525
          s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5526
          if (s)
5527
            {
5528
              if (s->flags & SEC_CODE)
5529
                exec = PF_X;
5530
              notesec = s;
5531
            }
5532
          else if (bed->default_execstack)
5533
            exec = PF_X;
5534
        }
5535
      if (notesec)
5536
        {
5537
          elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5538
          if (exec && info->relocatable
5539
              && notesec->output_section != bfd_abs_section_ptr)
5540
            notesec->output_section->flags |= SEC_CODE;
5541
        }
5542
    }
5543
 
5544
  /* Any syms created from now on start with -1 in
5545
     got.refcount/offset and plt.refcount/offset.  */
5546
  elf_hash_table (info)->init_got_refcount
5547
    = elf_hash_table (info)->init_got_offset;
5548
  elf_hash_table (info)->init_plt_refcount
5549
    = elf_hash_table (info)->init_plt_offset;
5550
 
5551
  if (info->relocatable
5552
      && !_bfd_elf_size_group_sections (info))
5553
    return FALSE;
5554
 
5555
  /* The backend may have to create some sections regardless of whether
5556
     we're dynamic or not.  */
5557
  if (bed->elf_backend_always_size_sections
5558
      && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5559
    return FALSE;
5560
 
5561
  if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5562
    return FALSE;
5563
 
5564
  dynobj = elf_hash_table (info)->dynobj;
5565
 
5566
  /* If there were no dynamic objects in the link, there is nothing to
5567
     do here.  */
5568
  if (dynobj == NULL)
5569
    return TRUE;
5570
 
5571
  if (elf_hash_table (info)->dynamic_sections_created)
5572
    {
5573
      struct elf_info_failed eif;
5574
      struct elf_link_hash_entry *h;
5575
      asection *dynstr;
5576
      struct bfd_elf_version_tree *t;
5577
      struct bfd_elf_version_expr *d;
5578
      asection *s;
5579
      bfd_boolean all_defined;
5580
 
5581
      *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5582
      BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5583
 
5584
      if (soname != NULL)
5585
        {
5586
          soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5587
                                             soname, TRUE);
5588
          if (soname_indx == (bfd_size_type) -1
5589
              || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5590
            return FALSE;
5591
        }
5592
 
5593
      if (info->symbolic)
5594
        {
5595
          if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5596
            return FALSE;
5597
          info->flags |= DF_SYMBOLIC;
5598
        }
5599
 
5600
      if (rpath != NULL)
5601
        {
5602
          bfd_size_type indx;
5603
 
5604
          indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5605
                                      TRUE);
5606
          if (indx == (bfd_size_type) -1
5607
              || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5608
            return FALSE;
5609
 
5610
          if  (info->new_dtags)
5611
            {
5612
              _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5613
              if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5614
                return FALSE;
5615
            }
5616
        }
5617
 
5618
      if (filter_shlib != NULL)
5619
        {
5620
          bfd_size_type indx;
5621
 
5622
          indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5623
                                      filter_shlib, TRUE);
5624
          if (indx == (bfd_size_type) -1
5625
              || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5626
            return FALSE;
5627
        }
5628
 
5629
      if (auxiliary_filters != NULL)
5630
        {
5631
          const char * const *p;
5632
 
5633
          for (p = auxiliary_filters; *p != NULL; p++)
5634
            {
5635
              bfd_size_type indx;
5636
 
5637
              indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5638
                                          *p, TRUE);
5639
              if (indx == (bfd_size_type) -1
5640
                  || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5641
                return FALSE;
5642
            }
5643
        }
5644
 
5645
      if (audit != NULL)
5646
        {
5647
          bfd_size_type indx;
5648
 
5649
          indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5650
                                      TRUE);
5651
          if (indx == (bfd_size_type) -1
5652
              || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5653
            return FALSE;
5654
        }
5655
 
5656
      if (depaudit != NULL)
5657
        {
5658
          bfd_size_type indx;
5659
 
5660
          indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5661
                                      TRUE);
5662
          if (indx == (bfd_size_type) -1
5663
              || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5664
            return FALSE;
5665
        }
5666
 
5667
      eif.info = info;
5668
      eif.verdefs = verdefs;
5669
      eif.failed = FALSE;
5670
 
5671
      /* If we are supposed to export all symbols into the dynamic symbol
5672
         table (this is not the normal case), then do so.  */
5673
      if (info->export_dynamic
5674
          || (info->executable && info->dynamic))
5675
        {
5676
          elf_link_hash_traverse (elf_hash_table (info),
5677
                                  _bfd_elf_export_symbol,
5678
                                  &eif);
5679
          if (eif.failed)
5680
            return FALSE;
5681
        }
5682
 
5683
      /* Make all global versions with definition.  */
5684
      for (t = verdefs; t != NULL; t = t->next)
5685
        for (d = t->globals.list; d != NULL; d = d->next)
5686
          if (!d->symver && d->literal)
5687
            {
5688
              const char *verstr, *name;
5689
              size_t namelen, verlen, newlen;
5690
              char *newname, *p, leading_char;
5691
              struct elf_link_hash_entry *newh;
5692
 
5693
              leading_char = bfd_get_symbol_leading_char (output_bfd);
5694
              name = d->pattern;
5695
              namelen = strlen (name) + (leading_char != '\0');
5696
              verstr = t->name;
5697
              verlen = strlen (verstr);
5698
              newlen = namelen + verlen + 3;
5699
 
5700
              newname = (char *) bfd_malloc (newlen);
5701
              if (newname == NULL)
5702
                return FALSE;
5703
              newname[0] = leading_char;
5704
              memcpy (newname + (leading_char != '\0'), name, namelen);
5705
 
5706
              /* Check the hidden versioned definition.  */
5707
              p = newname + namelen;
5708
              *p++ = ELF_VER_CHR;
5709
              memcpy (p, verstr, verlen + 1);
5710
              newh = elf_link_hash_lookup (elf_hash_table (info),
5711
                                           newname, FALSE, FALSE,
5712
                                           FALSE);
5713
              if (newh == NULL
5714
                  || (newh->root.type != bfd_link_hash_defined
5715
                      && newh->root.type != bfd_link_hash_defweak))
5716
                {
5717
                  /* Check the default versioned definition.  */
5718
                  *p++ = ELF_VER_CHR;
5719
                  memcpy (p, verstr, verlen + 1);
5720
                  newh = elf_link_hash_lookup (elf_hash_table (info),
5721
                                               newname, FALSE, FALSE,
5722
                                               FALSE);
5723
                }
5724
              free (newname);
5725
 
5726
              /* Mark this version if there is a definition and it is
5727
                 not defined in a shared object.  */
5728
              if (newh != NULL
5729
                  && !newh->def_dynamic
5730
                  && (newh->root.type == bfd_link_hash_defined
5731
                      || newh->root.type == bfd_link_hash_defweak))
5732
                d->symver = 1;
5733
            }
5734
 
5735
      /* Attach all the symbols to their version information.  */
5736
      asvinfo.info = info;
5737
      asvinfo.verdefs = verdefs;
5738
      asvinfo.failed = FALSE;
5739
 
5740
      elf_link_hash_traverse (elf_hash_table (info),
5741
                              _bfd_elf_link_assign_sym_version,
5742
                              &asvinfo);
5743
      if (asvinfo.failed)
5744
        return FALSE;
5745
 
5746
      if (!info->allow_undefined_version)
5747
        {
5748
          /* Check if all global versions have a definition.  */
5749
          all_defined = TRUE;
5750
          for (t = verdefs; t != NULL; t = t->next)
5751
            for (d = t->globals.list; d != NULL; d = d->next)
5752
              if (d->literal && !d->symver && !d->script)
5753
                {
5754
                  (*_bfd_error_handler)
5755
                    (_("%s: undefined version: %s"),
5756
                     d->pattern, t->name);
5757
                  all_defined = FALSE;
5758
                }
5759
 
5760
          if (!all_defined)
5761
            {
5762
              bfd_set_error (bfd_error_bad_value);
5763
              return FALSE;
5764
            }
5765
        }
5766
 
5767
      /* Find all symbols which were defined in a dynamic object and make
5768
         the backend pick a reasonable value for them.  */
5769
      elf_link_hash_traverse (elf_hash_table (info),
5770
                              _bfd_elf_adjust_dynamic_symbol,
5771
                              &eif);
5772
      if (eif.failed)
5773
        return FALSE;
5774
 
5775
      /* Add some entries to the .dynamic section.  We fill in some of the
5776
         values later, in bfd_elf_final_link, but we must add the entries
5777
         now so that we know the final size of the .dynamic section.  */
5778
 
5779
      /* If there are initialization and/or finalization functions to
5780
         call then add the corresponding DT_INIT/DT_FINI entries.  */
5781
      h = (info->init_function
5782
           ? elf_link_hash_lookup (elf_hash_table (info),
5783
                                   info->init_function, FALSE,
5784
                                   FALSE, FALSE)
5785
           : NULL);
5786
      if (h != NULL
5787
          && (h->ref_regular
5788
              || h->def_regular))
5789
        {
5790
          if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5791
            return FALSE;
5792
        }
5793
      h = (info->fini_function
5794
           ? elf_link_hash_lookup (elf_hash_table (info),
5795
                                   info->fini_function, FALSE,
5796
                                   FALSE, FALSE)
5797
           : NULL);
5798
      if (h != NULL
5799
          && (h->ref_regular
5800
              || h->def_regular))
5801
        {
5802
          if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5803
            return FALSE;
5804
        }
5805
 
5806
      s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5807
      if (s != NULL && s->linker_has_input)
5808
        {
5809
          /* DT_PREINIT_ARRAY is not allowed in shared library.  */
5810
          if (! info->executable)
5811
            {
5812
              bfd *sub;
5813
              asection *o;
5814
 
5815
              for (sub = info->input_bfds; sub != NULL;
5816
                   sub = sub->link_next)
5817
                if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5818
                  for (o = sub->sections; o != NULL; o = o->next)
5819
                    if (elf_section_data (o)->this_hdr.sh_type
5820
                        == SHT_PREINIT_ARRAY)
5821
                      {
5822
                        (*_bfd_error_handler)
5823
                          (_("%B: .preinit_array section is not allowed in DSO"),
5824
                           sub);
5825
                        break;
5826
                      }
5827
 
5828
              bfd_set_error (bfd_error_nonrepresentable_section);
5829
              return FALSE;
5830
            }
5831
 
5832
          if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5833
              || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5834
            return FALSE;
5835
        }
5836
      s = bfd_get_section_by_name (output_bfd, ".init_array");
5837
      if (s != NULL && s->linker_has_input)
5838
        {
5839
          if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5840
              || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5841
            return FALSE;
5842
        }
5843
      s = bfd_get_section_by_name (output_bfd, ".fini_array");
5844
      if (s != NULL && s->linker_has_input)
5845
        {
5846
          if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5847
              || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5848
            return FALSE;
5849
        }
5850
 
5851
      dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5852
      /* If .dynstr is excluded from the link, we don't want any of
5853
         these tags.  Strictly, we should be checking each section
5854
         individually;  This quick check covers for the case where
5855
         someone does a /DISCARD/ : { *(*) }.  */
5856
      if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5857
        {
5858
          bfd_size_type strsize;
5859
 
5860
          strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5861
          if ((info->emit_hash
5862
               && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5863
              || (info->emit_gnu_hash
5864
                  && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5865
              || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5866
              || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5867
              || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5868
              || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5869
                                              bed->s->sizeof_sym))
5870
            return FALSE;
5871
        }
5872
    }
5873
 
5874
  /* The backend must work out the sizes of all the other dynamic
5875
     sections.  */
5876
  if (bed->elf_backend_size_dynamic_sections
5877
      && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5878
    return FALSE;
5879
 
5880
  if (elf_hash_table (info)->dynamic_sections_created)
5881
    {
5882
      unsigned long section_sym_count;
5883
      asection *s;
5884
 
5885
      /* Set up the version definition section.  */
5886
      s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5887
      BFD_ASSERT (s != NULL);
5888
 
5889
      /* We may have created additional version definitions if we are
5890
         just linking a regular application.  */
5891
      verdefs = asvinfo.verdefs;
5892
 
5893
      /* Skip anonymous version tag.  */
5894
      if (verdefs != NULL && verdefs->vernum == 0)
5895
        verdefs = verdefs->next;
5896
 
5897
      if (verdefs == NULL && !info->create_default_symver)
5898
        s->flags |= SEC_EXCLUDE;
5899
      else
5900
        {
5901
          unsigned int cdefs;
5902
          bfd_size_type size;
5903
          struct bfd_elf_version_tree *t;
5904
          bfd_byte *p;
5905
          Elf_Internal_Verdef def;
5906
          Elf_Internal_Verdaux defaux;
5907
          struct bfd_link_hash_entry *bh;
5908
          struct elf_link_hash_entry *h;
5909
          const char *name;
5910
 
5911
          cdefs = 0;
5912
          size = 0;
5913
 
5914
          /* Make space for the base version.  */
5915
          size += sizeof (Elf_External_Verdef);
5916
          size += sizeof (Elf_External_Verdaux);
5917
          ++cdefs;
5918
 
5919
          /* Make space for the default version.  */
5920
          if (info->create_default_symver)
5921
            {
5922
              size += sizeof (Elf_External_Verdef);
5923
              ++cdefs;
5924
            }
5925
 
5926
          for (t = verdefs; t != NULL; t = t->next)
5927
            {
5928
              struct bfd_elf_version_deps *n;
5929
 
5930
              /* Don't emit base version twice.  */
5931
              if (t->vernum == 0)
5932
                continue;
5933
 
5934
              size += sizeof (Elf_External_Verdef);
5935
              size += sizeof (Elf_External_Verdaux);
5936
              ++cdefs;
5937
 
5938
              for (n = t->deps; n != NULL; n = n->next)
5939
                size += sizeof (Elf_External_Verdaux);
5940
            }
5941
 
5942
          s->size = size;
5943
          s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5944
          if (s->contents == NULL && s->size != 0)
5945
            return FALSE;
5946
 
5947
          /* Fill in the version definition section.  */
5948
 
5949
          p = s->contents;
5950
 
5951
          def.vd_version = VER_DEF_CURRENT;
5952
          def.vd_flags = VER_FLG_BASE;
5953
          def.vd_ndx = 1;
5954
          def.vd_cnt = 1;
5955
          if (info->create_default_symver)
5956
            {
5957
              def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5958
              def.vd_next = sizeof (Elf_External_Verdef);
5959
            }
5960
          else
5961
            {
5962
              def.vd_aux = sizeof (Elf_External_Verdef);
5963
              def.vd_next = (sizeof (Elf_External_Verdef)
5964
                             + sizeof (Elf_External_Verdaux));
5965
            }
5966
 
5967
          if (soname_indx != (bfd_size_type) -1)
5968
            {
5969
              _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5970
                                      soname_indx);
5971
              def.vd_hash = bfd_elf_hash (soname);
5972
              defaux.vda_name = soname_indx;
5973
              name = soname;
5974
            }
5975
          else
5976
            {
5977
              bfd_size_type indx;
5978
 
5979
              name = lbasename (output_bfd->filename);
5980
              def.vd_hash = bfd_elf_hash (name);
5981
              indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5982
                                          name, FALSE);
5983
              if (indx == (bfd_size_type) -1)
5984
                return FALSE;
5985
              defaux.vda_name = indx;
5986
            }
5987
          defaux.vda_next = 0;
5988
 
5989
          _bfd_elf_swap_verdef_out (output_bfd, &def,
5990
                                    (Elf_External_Verdef *) p);
5991
          p += sizeof (Elf_External_Verdef);
5992
          if (info->create_default_symver)
5993
            {
5994
              /* Add a symbol representing this version.  */
5995
              bh = NULL;
5996
              if (! (_bfd_generic_link_add_one_symbol
5997
                     (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5998
                      0, NULL, FALSE,
5999
                      get_elf_backend_data (dynobj)->collect, &bh)))
6000
                return FALSE;
6001
              h = (struct elf_link_hash_entry *) bh;
6002
              h->non_elf = 0;
6003
              h->def_regular = 1;
6004
              h->type = STT_OBJECT;
6005
              h->verinfo.vertree = NULL;
6006
 
6007
              if (! bfd_elf_link_record_dynamic_symbol (info, h))
6008
                return FALSE;
6009
 
6010
              /* Create a duplicate of the base version with the same
6011
                 aux block, but different flags.  */
6012
              def.vd_flags = 0;
6013
              def.vd_ndx = 2;
6014
              def.vd_aux = sizeof (Elf_External_Verdef);
6015
              if (verdefs)
6016
                def.vd_next = (sizeof (Elf_External_Verdef)
6017
                               + sizeof (Elf_External_Verdaux));
6018
              else
6019
                def.vd_next = 0;
6020
              _bfd_elf_swap_verdef_out (output_bfd, &def,
6021
                                        (Elf_External_Verdef *) p);
6022
              p += sizeof (Elf_External_Verdef);
6023
            }
6024
          _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6025
                                     (Elf_External_Verdaux *) p);
6026
          p += sizeof (Elf_External_Verdaux);
6027
 
6028
          for (t = verdefs; t != NULL; t = t->next)
6029
            {
6030
              unsigned int cdeps;
6031
              struct bfd_elf_version_deps *n;
6032
 
6033
              /* Don't emit the base version twice.  */
6034
              if (t->vernum == 0)
6035
                continue;
6036
 
6037
              cdeps = 0;
6038
              for (n = t->deps; n != NULL; n = n->next)
6039
                ++cdeps;
6040
 
6041
              /* Add a symbol representing this version.  */
6042
              bh = NULL;
6043
              if (! (_bfd_generic_link_add_one_symbol
6044
                     (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6045
                      0, NULL, FALSE,
6046
                      get_elf_backend_data (dynobj)->collect, &bh)))
6047
                return FALSE;
6048
              h = (struct elf_link_hash_entry *) bh;
6049
              h->non_elf = 0;
6050
              h->def_regular = 1;
6051
              h->type = STT_OBJECT;
6052
              h->verinfo.vertree = t;
6053
 
6054
              if (! bfd_elf_link_record_dynamic_symbol (info, h))
6055
                return FALSE;
6056
 
6057
              def.vd_version = VER_DEF_CURRENT;
6058
              def.vd_flags = 0;
6059
              if (t->globals.list == NULL
6060
                  && t->locals.list == NULL
6061
                  && ! t->used)
6062
                def.vd_flags |= VER_FLG_WEAK;
6063
              def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6064
              def.vd_cnt = cdeps + 1;
6065
              def.vd_hash = bfd_elf_hash (t->name);
6066
              def.vd_aux = sizeof (Elf_External_Verdef);
6067
              def.vd_next = 0;
6068
 
6069
              /* If a basever node is next, it *must* be the last node in
6070
                 the chain, otherwise Verdef construction breaks.  */
6071
              if (t->next != NULL && t->next->vernum == 0)
6072
                BFD_ASSERT (t->next->next == NULL);
6073
 
6074
              if (t->next != NULL && t->next->vernum != 0)
6075
                def.vd_next = (sizeof (Elf_External_Verdef)
6076
                               + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6077
 
6078
              _bfd_elf_swap_verdef_out (output_bfd, &def,
6079
                                        (Elf_External_Verdef *) p);
6080
              p += sizeof (Elf_External_Verdef);
6081
 
6082
              defaux.vda_name = h->dynstr_index;
6083
              _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6084
                                      h->dynstr_index);
6085
              defaux.vda_next = 0;
6086
              if (t->deps != NULL)
6087
                defaux.vda_next = sizeof (Elf_External_Verdaux);
6088
              t->name_indx = defaux.vda_name;
6089
 
6090
              _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6091
                                         (Elf_External_Verdaux *) p);
6092
              p += sizeof (Elf_External_Verdaux);
6093
 
6094
              for (n = t->deps; n != NULL; n = n->next)
6095
                {
6096
                  if (n->version_needed == NULL)
6097
                    {
6098
                      /* This can happen if there was an error in the
6099
                         version script.  */
6100
                      defaux.vda_name = 0;
6101
                    }
6102
                  else
6103
                    {
6104
                      defaux.vda_name = n->version_needed->name_indx;
6105
                      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6106
                                              defaux.vda_name);
6107
                    }
6108
                  if (n->next == NULL)
6109
                    defaux.vda_next = 0;
6110
                  else
6111
                    defaux.vda_next = sizeof (Elf_External_Verdaux);
6112
 
6113
                  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6114
                                             (Elf_External_Verdaux *) p);
6115
                  p += sizeof (Elf_External_Verdaux);
6116
                }
6117
            }
6118
 
6119
          if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6120
              || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6121
            return FALSE;
6122
 
6123
          elf_tdata (output_bfd)->cverdefs = cdefs;
6124
        }
6125
 
6126
      if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6127
        {
6128
          if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6129
            return FALSE;
6130
        }
6131
      else if (info->flags & DF_BIND_NOW)
6132
        {
6133
          if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6134
            return FALSE;
6135
        }
6136
 
6137
      if (info->flags_1)
6138
        {
6139
          if (info->executable)
6140
            info->flags_1 &= ~ (DF_1_INITFIRST
6141
                                | DF_1_NODELETE
6142
                                | DF_1_NOOPEN);
6143
          if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6144
            return FALSE;
6145
        }
6146
 
6147
      /* Work out the size of the version reference section.  */
6148
 
6149
      s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
6150
      BFD_ASSERT (s != NULL);
6151
      {
6152
        struct elf_find_verdep_info sinfo;
6153
 
6154
        sinfo.info = info;
6155
        sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6156
        if (sinfo.vers == 0)
6157
          sinfo.vers = 1;
6158
        sinfo.failed = FALSE;
6159
 
6160
        elf_link_hash_traverse (elf_hash_table (info),
6161
                                _bfd_elf_link_find_version_dependencies,
6162
                                &sinfo);
6163
        if (sinfo.failed)
6164
          return FALSE;
6165
 
6166
        if (elf_tdata (output_bfd)->verref == NULL)
6167
          s->flags |= SEC_EXCLUDE;
6168
        else
6169
          {
6170
            Elf_Internal_Verneed *t;
6171
            unsigned int size;
6172
            unsigned int crefs;
6173
            bfd_byte *p;
6174
 
6175
            /* Build the version dependency section.  */
6176
            size = 0;
6177
            crefs = 0;
6178
            for (t = elf_tdata (output_bfd)->verref;
6179
                 t != NULL;
6180
                 t = t->vn_nextref)
6181
              {
6182
                Elf_Internal_Vernaux *a;
6183
 
6184
                size += sizeof (Elf_External_Verneed);
6185
                ++crefs;
6186
                for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6187
                  size += sizeof (Elf_External_Vernaux);
6188
              }
6189
 
6190
            s->size = size;
6191
            s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6192
            if (s->contents == NULL)
6193
              return FALSE;
6194
 
6195
            p = s->contents;
6196
            for (t = elf_tdata (output_bfd)->verref;
6197
                 t != NULL;
6198
                 t = t->vn_nextref)
6199
              {
6200
                unsigned int caux;
6201
                Elf_Internal_Vernaux *a;
6202
                bfd_size_type indx;
6203
 
6204
                caux = 0;
6205
                for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6206
                  ++caux;
6207
 
6208
                t->vn_version = VER_NEED_CURRENT;
6209
                t->vn_cnt = caux;
6210
                indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6211
                                            elf_dt_name (t->vn_bfd) != NULL
6212
                                            ? elf_dt_name (t->vn_bfd)
6213
                                            : lbasename (t->vn_bfd->filename),
6214
                                            FALSE);
6215
                if (indx == (bfd_size_type) -1)
6216
                  return FALSE;
6217
                t->vn_file = indx;
6218
                t->vn_aux = sizeof (Elf_External_Verneed);
6219
                if (t->vn_nextref == NULL)
6220
                  t->vn_next = 0;
6221
                else
6222
                  t->vn_next = (sizeof (Elf_External_Verneed)
6223
                                + caux * sizeof (Elf_External_Vernaux));
6224
 
6225
                _bfd_elf_swap_verneed_out (output_bfd, t,
6226
                                           (Elf_External_Verneed *) p);
6227
                p += sizeof (Elf_External_Verneed);
6228
 
6229
                for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6230
                  {
6231
                    a->vna_hash = bfd_elf_hash (a->vna_nodename);
6232
                    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6233
                                                a->vna_nodename, FALSE);
6234
                    if (indx == (bfd_size_type) -1)
6235
                      return FALSE;
6236
                    a->vna_name = indx;
6237
                    if (a->vna_nextptr == NULL)
6238
                      a->vna_next = 0;
6239
                    else
6240
                      a->vna_next = sizeof (Elf_External_Vernaux);
6241
 
6242
                    _bfd_elf_swap_vernaux_out (output_bfd, a,
6243
                                               (Elf_External_Vernaux *) p);
6244
                    p += sizeof (Elf_External_Vernaux);
6245
                  }
6246
              }
6247
 
6248
            if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6249
                || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6250
              return FALSE;
6251
 
6252
            elf_tdata (output_bfd)->cverrefs = crefs;
6253
          }
6254
      }
6255
 
6256
      if ((elf_tdata (output_bfd)->cverrefs == 0
6257
           && elf_tdata (output_bfd)->cverdefs == 0)
6258
          || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6259
                                             &section_sym_count) == 0)
6260
        {
6261
          s = bfd_get_section_by_name (dynobj, ".gnu.version");
6262
          s->flags |= SEC_EXCLUDE;
6263
        }
6264
    }
6265
  return TRUE;
6266
}
6267
 
6268
/* Find the first non-excluded output section.  We'll use its
6269
   section symbol for some emitted relocs.  */
6270
void
6271
_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6272
{
6273
  asection *s;
6274
 
6275
  for (s = output_bfd->sections; s != NULL; s = s->next)
6276
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6277
        && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6278
      {
6279
        elf_hash_table (info)->text_index_section = s;
6280
        break;
6281
      }
6282
}
6283
 
6284
/* Find two non-excluded output sections, one for code, one for data.
6285
   We'll use their section symbols for some emitted relocs.  */
6286
void
6287
_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6288
{
6289
  asection *s;
6290
 
6291
  /* Data first, since setting text_index_section changes
6292
     _bfd_elf_link_omit_section_dynsym.  */
6293
  for (s = output_bfd->sections; s != NULL; s = s->next)
6294
    if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6295
        && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6296
      {
6297
        elf_hash_table (info)->data_index_section = s;
6298
        break;
6299
      }
6300
 
6301
  for (s = output_bfd->sections; s != NULL; s = s->next)
6302
    if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6303
         == (SEC_ALLOC | SEC_READONLY))
6304
        && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6305
      {
6306
        elf_hash_table (info)->text_index_section = s;
6307
        break;
6308
      }
6309
 
6310
  if (elf_hash_table (info)->text_index_section == NULL)
6311
    elf_hash_table (info)->text_index_section
6312
      = elf_hash_table (info)->data_index_section;
6313
}
6314
 
6315
bfd_boolean
6316
bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6317
{
6318
  const struct elf_backend_data *bed;
6319
 
6320
  if (!is_elf_hash_table (info->hash))
6321
    return TRUE;
6322
 
6323
  bed = get_elf_backend_data (output_bfd);
6324
  (*bed->elf_backend_init_index_section) (output_bfd, info);
6325
 
6326
  if (elf_hash_table (info)->dynamic_sections_created)
6327
    {
6328
      bfd *dynobj;
6329
      asection *s;
6330
      bfd_size_type dynsymcount;
6331
      unsigned long section_sym_count;
6332
      unsigned int dtagcount;
6333
 
6334
      dynobj = elf_hash_table (info)->dynobj;
6335
 
6336
      /* Assign dynsym indicies.  In a shared library we generate a
6337
         section symbol for each output section, which come first.
6338
         Next come all of the back-end allocated local dynamic syms,
6339
         followed by the rest of the global symbols.  */
6340
 
6341
      dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6342
                                                    &section_sym_count);
6343
 
6344
      /* Work out the size of the symbol version section.  */
6345
      s = bfd_get_section_by_name (dynobj, ".gnu.version");
6346
      BFD_ASSERT (s != NULL);
6347
      if (dynsymcount != 0
6348
          && (s->flags & SEC_EXCLUDE) == 0)
6349
        {
6350
          s->size = dynsymcount * sizeof (Elf_External_Versym);
6351
          s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6352
          if (s->contents == NULL)
6353
            return FALSE;
6354
 
6355
          if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6356
            return FALSE;
6357
        }
6358
 
6359
      /* Set the size of the .dynsym and .hash sections.  We counted
6360
         the number of dynamic symbols in elf_link_add_object_symbols.
6361
         We will build the contents of .dynsym and .hash when we build
6362
         the final symbol table, because until then we do not know the
6363
         correct value to give the symbols.  We built the .dynstr
6364
         section as we went along in elf_link_add_object_symbols.  */
6365
      s = bfd_get_section_by_name (dynobj, ".dynsym");
6366
      BFD_ASSERT (s != NULL);
6367
      s->size = dynsymcount * bed->s->sizeof_sym;
6368
 
6369
      if (dynsymcount != 0)
6370
        {
6371
          s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6372
          if (s->contents == NULL)
6373
            return FALSE;
6374
 
6375
          /* The first entry in .dynsym is a dummy symbol.
6376
             Clear all the section syms, in case we don't output them all.  */
6377
          ++section_sym_count;
6378
          memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6379
        }
6380
 
6381
      elf_hash_table (info)->bucketcount = 0;
6382
 
6383
      /* Compute the size of the hashing table.  As a side effect this
6384
         computes the hash values for all the names we export.  */
6385
      if (info->emit_hash)
6386
        {
6387
          unsigned long int *hashcodes;
6388
          struct hash_codes_info hashinf;
6389
          bfd_size_type amt;
6390
          unsigned long int nsyms;
6391
          size_t bucketcount;
6392
          size_t hash_entry_size;
6393
 
6394
          /* Compute the hash values for all exported symbols.  At the same
6395
             time store the values in an array so that we could use them for
6396
             optimizations.  */
6397
          amt = dynsymcount * sizeof (unsigned long int);
6398
          hashcodes = (unsigned long int *) bfd_malloc (amt);
6399
          if (hashcodes == NULL)
6400
            return FALSE;
6401
          hashinf.hashcodes = hashcodes;
6402
          hashinf.error = FALSE;
6403
 
6404
          /* Put all hash values in HASHCODES.  */
6405
          elf_link_hash_traverse (elf_hash_table (info),
6406
                                  elf_collect_hash_codes, &hashinf);
6407
          if (hashinf.error)
6408
            {
6409
              free (hashcodes);
6410
              return FALSE;
6411
            }
6412
 
6413
          nsyms = hashinf.hashcodes - hashcodes;
6414
          bucketcount
6415
            = compute_bucket_count (info, hashcodes, nsyms, 0);
6416
          free (hashcodes);
6417
 
6418
          if (bucketcount == 0)
6419
            return FALSE;
6420
 
6421
          elf_hash_table (info)->bucketcount = bucketcount;
6422
 
6423
          s = bfd_get_section_by_name (dynobj, ".hash");
6424
          BFD_ASSERT (s != NULL);
6425
          hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6426
          s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6427
          s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6428
          if (s->contents == NULL)
6429
            return FALSE;
6430
 
6431
          bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6432
          bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6433
                   s->contents + hash_entry_size);
6434
        }
6435
 
6436
      if (info->emit_gnu_hash)
6437
        {
6438
          size_t i, cnt;
6439
          unsigned char *contents;
6440
          struct collect_gnu_hash_codes cinfo;
6441
          bfd_size_type amt;
6442
          size_t bucketcount;
6443
 
6444
          memset (&cinfo, 0, sizeof (cinfo));
6445
 
6446
          /* Compute the hash values for all exported symbols.  At the same
6447
             time store the values in an array so that we could use them for
6448
             optimizations.  */
6449
          amt = dynsymcount * 2 * sizeof (unsigned long int);
6450
          cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6451
          if (cinfo.hashcodes == NULL)
6452
            return FALSE;
6453
 
6454
          cinfo.hashval = cinfo.hashcodes + dynsymcount;
6455
          cinfo.min_dynindx = -1;
6456
          cinfo.output_bfd = output_bfd;
6457
          cinfo.bed = bed;
6458
 
6459
          /* Put all hash values in HASHCODES.  */
6460
          elf_link_hash_traverse (elf_hash_table (info),
6461
                                  elf_collect_gnu_hash_codes, &cinfo);
6462
          if (cinfo.error)
6463
            {
6464
              free (cinfo.hashcodes);
6465
              return FALSE;
6466
            }
6467
 
6468
          bucketcount
6469
            = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6470
 
6471
          if (bucketcount == 0)
6472
            {
6473
              free (cinfo.hashcodes);
6474
              return FALSE;
6475
            }
6476
 
6477
          s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6478
          BFD_ASSERT (s != NULL);
6479
 
6480
          if (cinfo.nsyms == 0)
6481
            {
6482
              /* Empty .gnu.hash section is special.  */
6483
              BFD_ASSERT (cinfo.min_dynindx == -1);
6484
              free (cinfo.hashcodes);
6485
              s->size = 5 * 4 + bed->s->arch_size / 8;
6486
              contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6487
              if (contents == NULL)
6488
                return FALSE;
6489
              s->contents = contents;
6490
              /* 1 empty bucket.  */
6491
              bfd_put_32 (output_bfd, 1, contents);
6492
              /* SYMIDX above the special symbol 0.  */
6493
              bfd_put_32 (output_bfd, 1, contents + 4);
6494
              /* Just one word for bitmask.  */
6495
              bfd_put_32 (output_bfd, 1, contents + 8);
6496
              /* Only hash fn bloom filter.  */
6497
              bfd_put_32 (output_bfd, 0, contents + 12);
6498
              /* No hashes are valid - empty bitmask.  */
6499
              bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6500
              /* No hashes in the only bucket.  */
6501
              bfd_put_32 (output_bfd, 0,
6502
                          contents + 16 + bed->s->arch_size / 8);
6503
            }
6504
          else
6505
            {
6506
              unsigned long int maskwords, maskbitslog2, x;
6507
              BFD_ASSERT (cinfo.min_dynindx != -1);
6508
 
6509
              x = cinfo.nsyms;
6510
              maskbitslog2 = 1;
6511
              while ((x >>= 1) != 0)
6512
                ++maskbitslog2;
6513
              if (maskbitslog2 < 3)
6514
                maskbitslog2 = 5;
6515
              else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6516
                maskbitslog2 = maskbitslog2 + 3;
6517
              else
6518
                maskbitslog2 = maskbitslog2 + 2;
6519
              if (bed->s->arch_size == 64)
6520
                {
6521
                  if (maskbitslog2 == 5)
6522
                    maskbitslog2 = 6;
6523
                  cinfo.shift1 = 6;
6524
                }
6525
              else
6526
                cinfo.shift1 = 5;
6527
              cinfo.mask = (1 << cinfo.shift1) - 1;
6528
              cinfo.shift2 = maskbitslog2;
6529
              cinfo.maskbits = 1 << maskbitslog2;
6530
              maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6531
              amt = bucketcount * sizeof (unsigned long int) * 2;
6532
              amt += maskwords * sizeof (bfd_vma);
6533
              cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6534
              if (cinfo.bitmask == NULL)
6535
                {
6536
                  free (cinfo.hashcodes);
6537
                  return FALSE;
6538
                }
6539
 
6540
              cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6541
              cinfo.indx = cinfo.counts + bucketcount;
6542
              cinfo.symindx = dynsymcount - cinfo.nsyms;
6543
              memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6544
 
6545
              /* Determine how often each hash bucket is used.  */
6546
              memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6547
              for (i = 0; i < cinfo.nsyms; ++i)
6548
                ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6549
 
6550
              for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6551
                if (cinfo.counts[i] != 0)
6552
                  {
6553
                    cinfo.indx[i] = cnt;
6554
                    cnt += cinfo.counts[i];
6555
                  }
6556
              BFD_ASSERT (cnt == dynsymcount);
6557
              cinfo.bucketcount = bucketcount;
6558
              cinfo.local_indx = cinfo.min_dynindx;
6559
 
6560
              s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6561
              s->size += cinfo.maskbits / 8;
6562
              contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6563
              if (contents == NULL)
6564
                {
6565
                  free (cinfo.bitmask);
6566
                  free (cinfo.hashcodes);
6567
                  return FALSE;
6568
                }
6569
 
6570
              s->contents = contents;
6571
              bfd_put_32 (output_bfd, bucketcount, contents);
6572
              bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6573
              bfd_put_32 (output_bfd, maskwords, contents + 8);
6574
              bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6575
              contents += 16 + cinfo.maskbits / 8;
6576
 
6577
              for (i = 0; i < bucketcount; ++i)
6578
                {
6579
                  if (cinfo.counts[i] == 0)
6580
                    bfd_put_32 (output_bfd, 0, contents);
6581
                  else
6582
                    bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6583
                  contents += 4;
6584
                }
6585
 
6586
              cinfo.contents = contents;
6587
 
6588
              /* Renumber dynamic symbols, populate .gnu.hash section.  */
6589
              elf_link_hash_traverse (elf_hash_table (info),
6590
                                      elf_renumber_gnu_hash_syms, &cinfo);
6591
 
6592
              contents = s->contents + 16;
6593
              for (i = 0; i < maskwords; ++i)
6594
                {
6595
                  bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6596
                           contents);
6597
                  contents += bed->s->arch_size / 8;
6598
                }
6599
 
6600
              free (cinfo.bitmask);
6601
              free (cinfo.hashcodes);
6602
            }
6603
        }
6604
 
6605
      s = bfd_get_section_by_name (dynobj, ".dynstr");
6606
      BFD_ASSERT (s != NULL);
6607
 
6608
      elf_finalize_dynstr (output_bfd, info);
6609
 
6610
      s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6611
 
6612
      for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6613
        if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6614
          return FALSE;
6615
    }
6616
 
6617
  return TRUE;
6618
}
6619
 
6620
/* Indicate that we are only retrieving symbol values from this
6621
   section.  */
6622
 
6623
void
6624
_bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
6625
{
6626
  if (is_elf_hash_table (info->hash))
6627
    sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
6628
  _bfd_generic_link_just_syms (sec, info);
6629
}
6630
 
6631
/* Make sure sec_info_type is cleared if sec_info is cleared too.  */
6632
 
6633
static void
6634
merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6635
                            asection *sec)
6636
{
6637
  BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
6638
  sec->sec_info_type = ELF_INFO_TYPE_NONE;
6639
}
6640
 
6641
/* Finish SHF_MERGE section merging.  */
6642
 
6643
bfd_boolean
6644
_bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6645
{
6646
  bfd *ibfd;
6647
  asection *sec;
6648
 
6649
  if (!is_elf_hash_table (info->hash))
6650
    return FALSE;
6651
 
6652
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6653
    if ((ibfd->flags & DYNAMIC) == 0)
6654
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6655
        if ((sec->flags & SEC_MERGE) != 0
6656
            && !bfd_is_abs_section (sec->output_section))
6657
          {
6658
            struct bfd_elf_section_data *secdata;
6659
 
6660
            secdata = elf_section_data (sec);
6661
            if (! _bfd_add_merge_section (abfd,
6662
                                          &elf_hash_table (info)->merge_info,
6663
                                          sec, &secdata->sec_info))
6664
              return FALSE;
6665
            else if (secdata->sec_info)
6666
              sec->sec_info_type = ELF_INFO_TYPE_MERGE;
6667
          }
6668
 
6669
  if (elf_hash_table (info)->merge_info != NULL)
6670
    _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6671
                         merge_sections_remove_hook);
6672
  return TRUE;
6673
}
6674
 
6675
/* Create an entry in an ELF linker hash table.  */
6676
 
6677
struct bfd_hash_entry *
6678
_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6679
                            struct bfd_hash_table *table,
6680
                            const char *string)
6681
{
6682
  /* Allocate the structure if it has not already been allocated by a
6683
     subclass.  */
6684
  if (entry == NULL)
6685
    {
6686
      entry = (struct bfd_hash_entry *)
6687
          bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6688
      if (entry == NULL)
6689
        return entry;
6690
    }
6691
 
6692
  /* Call the allocation method of the superclass.  */
6693
  entry = _bfd_link_hash_newfunc (entry, table, string);
6694
  if (entry != NULL)
6695
    {
6696
      struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6697
      struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6698
 
6699
      /* Set local fields.  */
6700
      ret->indx = -1;
6701
      ret->dynindx = -1;
6702
      ret->got = htab->init_got_refcount;
6703
      ret->plt = htab->init_plt_refcount;
6704
      memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6705
                              - offsetof (struct elf_link_hash_entry, size)));
6706
      /* Assume that we have been called by a non-ELF symbol reader.
6707
         This flag is then reset by the code which reads an ELF input
6708
         file.  This ensures that a symbol created by a non-ELF symbol
6709
         reader will have the flag set correctly.  */
6710
      ret->non_elf = 1;
6711
    }
6712
 
6713
  return entry;
6714
}
6715
 
6716
/* Copy data from an indirect symbol to its direct symbol, hiding the
6717
   old indirect symbol.  Also used for copying flags to a weakdef.  */
6718
 
6719
void
6720
_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6721
                                  struct elf_link_hash_entry *dir,
6722
                                  struct elf_link_hash_entry *ind)
6723
{
6724
  struct elf_link_hash_table *htab;
6725
 
6726
  /* Copy down any references that we may have already seen to the
6727
     symbol which just became indirect.  */
6728
 
6729
  dir->ref_dynamic |= ind->ref_dynamic;
6730
  dir->ref_regular |= ind->ref_regular;
6731
  dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6732
  dir->non_got_ref |= ind->non_got_ref;
6733
  dir->needs_plt |= ind->needs_plt;
6734
  dir->pointer_equality_needed |= ind->pointer_equality_needed;
6735
 
6736
  if (ind->root.type != bfd_link_hash_indirect)
6737
    return;
6738
 
6739
  /* Copy over the global and procedure linkage table refcount entries.
6740
     These may have been already set up by a check_relocs routine.  */
6741
  htab = elf_hash_table (info);
6742
  if (ind->got.refcount > htab->init_got_refcount.refcount)
6743
    {
6744
      if (dir->got.refcount < 0)
6745
        dir->got.refcount = 0;
6746
      dir->got.refcount += ind->got.refcount;
6747
      ind->got.refcount = htab->init_got_refcount.refcount;
6748
    }
6749
 
6750
  if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6751
    {
6752
      if (dir->plt.refcount < 0)
6753
        dir->plt.refcount = 0;
6754
      dir->plt.refcount += ind->plt.refcount;
6755
      ind->plt.refcount = htab->init_plt_refcount.refcount;
6756
    }
6757
 
6758
  if (ind->dynindx != -1)
6759
    {
6760
      if (dir->dynindx != -1)
6761
        _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6762
      dir->dynindx = ind->dynindx;
6763
      dir->dynstr_index = ind->dynstr_index;
6764
      ind->dynindx = -1;
6765
      ind->dynstr_index = 0;
6766
    }
6767
}
6768
 
6769
void
6770
_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6771
                                struct elf_link_hash_entry *h,
6772
                                bfd_boolean force_local)
6773
{
6774
  /* STT_GNU_IFUNC symbol must go through PLT.  */
6775
  if (h->type != STT_GNU_IFUNC)
6776
    {
6777
      h->plt = elf_hash_table (info)->init_plt_offset;
6778
      h->needs_plt = 0;
6779
    }
6780
  if (force_local)
6781
    {
6782
      h->forced_local = 1;
6783
      if (h->dynindx != -1)
6784
        {
6785
          h->dynindx = -1;
6786
          _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6787
                                  h->dynstr_index);
6788
        }
6789
    }
6790
}
6791
 
6792
/* Initialize an ELF linker hash table.  */
6793
 
6794
bfd_boolean
6795
_bfd_elf_link_hash_table_init
6796
  (struct elf_link_hash_table *table,
6797
   bfd *abfd,
6798
   struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6799
                                      struct bfd_hash_table *,
6800
                                      const char *),
6801
   unsigned int entsize,
6802
   enum elf_target_id target_id)
6803
{
6804
  bfd_boolean ret;
6805
  int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6806
 
6807
  memset (table, 0, sizeof * table);
6808
  table->init_got_refcount.refcount = can_refcount - 1;
6809
  table->init_plt_refcount.refcount = can_refcount - 1;
6810
  table->init_got_offset.offset = -(bfd_vma) 1;
6811
  table->init_plt_offset.offset = -(bfd_vma) 1;
6812
  /* The first dynamic symbol is a dummy.  */
6813
  table->dynsymcount = 1;
6814
 
6815
  ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
6816
 
6817
  table->root.type = bfd_link_elf_hash_table;
6818
  table->hash_table_id = target_id;
6819
 
6820
  return ret;
6821
}
6822
 
6823
/* Create an ELF linker hash table.  */
6824
 
6825
struct bfd_link_hash_table *
6826
_bfd_elf_link_hash_table_create (bfd *abfd)
6827
{
6828
  struct elf_link_hash_table *ret;
6829
  bfd_size_type amt = sizeof (struct elf_link_hash_table);
6830
 
6831
  ret = (struct elf_link_hash_table *) bfd_malloc (amt);
6832
  if (ret == NULL)
6833
    return NULL;
6834
 
6835
  if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
6836
                                       sizeof (struct elf_link_hash_entry),
6837
                                       GENERIC_ELF_DATA))
6838
    {
6839
      free (ret);
6840
      return NULL;
6841
    }
6842
 
6843
  return &ret->root;
6844
}
6845
 
6846
/* This is a hook for the ELF emulation code in the generic linker to
6847
   tell the backend linker what file name to use for the DT_NEEDED
6848
   entry for a dynamic object.  */
6849
 
6850
void
6851
bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6852
{
6853
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6854
      && bfd_get_format (abfd) == bfd_object)
6855
    elf_dt_name (abfd) = name;
6856
}
6857
 
6858
int
6859
bfd_elf_get_dyn_lib_class (bfd *abfd)
6860
{
6861
  int lib_class;
6862
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6863
      && bfd_get_format (abfd) == bfd_object)
6864
    lib_class = elf_dyn_lib_class (abfd);
6865
  else
6866
    lib_class = 0;
6867
  return lib_class;
6868
}
6869
 
6870
void
6871
bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6872
{
6873
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6874
      && bfd_get_format (abfd) == bfd_object)
6875
    elf_dyn_lib_class (abfd) = lib_class;
6876
}
6877
 
6878
/* Get the list of DT_NEEDED entries for a link.  This is a hook for
6879
   the linker ELF emulation code.  */
6880
 
6881
struct bfd_link_needed_list *
6882
bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6883
                         struct bfd_link_info *info)
6884
{
6885
  if (! is_elf_hash_table (info->hash))
6886
    return NULL;
6887
  return elf_hash_table (info)->needed;
6888
}
6889
 
6890
/* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
6891
   hook for the linker ELF emulation code.  */
6892
 
6893
struct bfd_link_needed_list *
6894
bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6895
                          struct bfd_link_info *info)
6896
{
6897
  if (! is_elf_hash_table (info->hash))
6898
    return NULL;
6899
  return elf_hash_table (info)->runpath;
6900
}
6901
 
6902
/* Get the name actually used for a dynamic object for a link.  This
6903
   is the SONAME entry if there is one.  Otherwise, it is the string
6904
   passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
6905
 
6906
const char *
6907
bfd_elf_get_dt_soname (bfd *abfd)
6908
{
6909
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6910
      && bfd_get_format (abfd) == bfd_object)
6911
    return elf_dt_name (abfd);
6912
  return NULL;
6913
}
6914
 
6915
/* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
6916
   the ELF linker emulation code.  */
6917
 
6918
bfd_boolean
6919
bfd_elf_get_bfd_needed_list (bfd *abfd,
6920
                             struct bfd_link_needed_list **pneeded)
6921
{
6922
  asection *s;
6923
  bfd_byte *dynbuf = NULL;
6924
  unsigned int elfsec;
6925
  unsigned long shlink;
6926
  bfd_byte *extdyn, *extdynend;
6927
  size_t extdynsize;
6928
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
6929
 
6930
  *pneeded = NULL;
6931
 
6932
  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
6933
      || bfd_get_format (abfd) != bfd_object)
6934
    return TRUE;
6935
 
6936
  s = bfd_get_section_by_name (abfd, ".dynamic");
6937
  if (s == NULL || s->size == 0)
6938
    return TRUE;
6939
 
6940
  if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
6941
    goto error_return;
6942
 
6943
  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
6944
  if (elfsec == SHN_BAD)
6945
    goto error_return;
6946
 
6947
  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
6948
 
6949
  extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
6950
  swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
6951
 
6952
  extdyn = dynbuf;
6953
  extdynend = extdyn + s->size;
6954
  for (; extdyn < extdynend; extdyn += extdynsize)
6955
    {
6956
      Elf_Internal_Dyn dyn;
6957
 
6958
      (*swap_dyn_in) (abfd, extdyn, &dyn);
6959
 
6960
      if (dyn.d_tag == DT_NULL)
6961
        break;
6962
 
6963
      if (dyn.d_tag == DT_NEEDED)
6964
        {
6965
          const char *string;
6966
          struct bfd_link_needed_list *l;
6967
          unsigned int tagv = dyn.d_un.d_val;
6968
          bfd_size_type amt;
6969
 
6970
          string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
6971
          if (string == NULL)
6972
            goto error_return;
6973
 
6974
          amt = sizeof *l;
6975
          l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
6976
          if (l == NULL)
6977
            goto error_return;
6978
 
6979
          l->by = abfd;
6980
          l->name = string;
6981
          l->next = *pneeded;
6982
          *pneeded = l;
6983
        }
6984
    }
6985
 
6986
  free (dynbuf);
6987
 
6988
  return TRUE;
6989
 
6990
 error_return:
6991
  if (dynbuf != NULL)
6992
    free (dynbuf);
6993
  return FALSE;
6994
}
6995
 
6996
struct elf_symbuf_symbol
6997
{
6998
  unsigned long st_name;        /* Symbol name, index in string tbl */
6999
  unsigned char st_info;        /* Type and binding attributes */
7000
  unsigned char st_other;       /* Visibilty, and target specific */
7001
};
7002
 
7003
struct elf_symbuf_head
7004
{
7005
  struct elf_symbuf_symbol *ssym;
7006
  bfd_size_type count;
7007
  unsigned int st_shndx;
7008
};
7009
 
7010
struct elf_symbol
7011
{
7012
  union
7013
    {
7014
      Elf_Internal_Sym *isym;
7015
      struct elf_symbuf_symbol *ssym;
7016
    } u;
7017
  const char *name;
7018
};
7019
 
7020
/* Sort references to symbols by ascending section number.  */
7021
 
7022
static int
7023
elf_sort_elf_symbol (const void *arg1, const void *arg2)
7024
{
7025
  const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7026
  const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7027
 
7028
  return s1->st_shndx - s2->st_shndx;
7029
}
7030
 
7031
static int
7032
elf_sym_name_compare (const void *arg1, const void *arg2)
7033
{
7034
  const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7035
  const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7036
  return strcmp (s1->name, s2->name);
7037
}
7038
 
7039
static struct elf_symbuf_head *
7040
elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7041
{
7042
  Elf_Internal_Sym **ind, **indbufend, **indbuf;
7043
  struct elf_symbuf_symbol *ssym;
7044
  struct elf_symbuf_head *ssymbuf, *ssymhead;
7045
  bfd_size_type i, shndx_count, total_size;
7046
 
7047
  indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7048
  if (indbuf == NULL)
7049
    return NULL;
7050
 
7051
  for (ind = indbuf, i = 0; i < symcount; i++)
7052
    if (isymbuf[i].st_shndx != SHN_UNDEF)
7053
      *ind++ = &isymbuf[i];
7054
  indbufend = ind;
7055
 
7056
  qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7057
         elf_sort_elf_symbol);
7058
 
7059
  shndx_count = 0;
7060
  if (indbufend > indbuf)
7061
    for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7062
      if (ind[0]->st_shndx != ind[1]->st_shndx)
7063
        shndx_count++;
7064
 
7065
  total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7066
                + (indbufend - indbuf) * sizeof (*ssym));
7067
  ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7068
  if (ssymbuf == NULL)
7069
    {
7070
      free (indbuf);
7071
      return NULL;
7072
    }
7073
 
7074
  ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7075
  ssymbuf->ssym = NULL;
7076
  ssymbuf->count = shndx_count;
7077
  ssymbuf->st_shndx = 0;
7078
  for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7079
    {
7080
      if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7081
        {
7082
          ssymhead++;
7083
          ssymhead->ssym = ssym;
7084
          ssymhead->count = 0;
7085
          ssymhead->st_shndx = (*ind)->st_shndx;
7086
        }
7087
      ssym->st_name = (*ind)->st_name;
7088
      ssym->st_info = (*ind)->st_info;
7089
      ssym->st_other = (*ind)->st_other;
7090
      ssymhead->count++;
7091
    }
7092
  BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7093
              && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7094
                  == total_size));
7095
 
7096
  free (indbuf);
7097
  return ssymbuf;
7098
}
7099
 
7100
/* Check if 2 sections define the same set of local and global
7101
   symbols.  */
7102
 
7103
static bfd_boolean
7104
bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7105
                                   struct bfd_link_info *info)
7106
{
7107
  bfd *bfd1, *bfd2;
7108
  const struct elf_backend_data *bed1, *bed2;
7109
  Elf_Internal_Shdr *hdr1, *hdr2;
7110
  bfd_size_type symcount1, symcount2;
7111
  Elf_Internal_Sym *isymbuf1, *isymbuf2;
7112
  struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7113
  Elf_Internal_Sym *isym, *isymend;
7114
  struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7115
  bfd_size_type count1, count2, i;
7116
  unsigned int shndx1, shndx2;
7117
  bfd_boolean result;
7118
 
7119
  bfd1 = sec1->owner;
7120
  bfd2 = sec2->owner;
7121
 
7122
  /* Both sections have to be in ELF.  */
7123
  if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7124
      || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7125
    return FALSE;
7126
 
7127
  if (elf_section_type (sec1) != elf_section_type (sec2))
7128
    return FALSE;
7129
 
7130
  shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7131
  shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7132
  if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7133
    return FALSE;
7134
 
7135
  bed1 = get_elf_backend_data (bfd1);
7136
  bed2 = get_elf_backend_data (bfd2);
7137
  hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7138
  symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7139
  hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7140
  symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7141
 
7142
  if (symcount1 == 0 || symcount2 == 0)
7143
    return FALSE;
7144
 
7145
  result = FALSE;
7146
  isymbuf1 = NULL;
7147
  isymbuf2 = NULL;
7148
  ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7149
  ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7150
 
7151
  if (ssymbuf1 == NULL)
7152
    {
7153
      isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7154
                                       NULL, NULL, NULL);
7155
      if (isymbuf1 == NULL)
7156
        goto done;
7157
 
7158
      if (!info->reduce_memory_overheads)
7159
        elf_tdata (bfd1)->symbuf = ssymbuf1
7160
          = elf_create_symbuf (symcount1, isymbuf1);
7161
    }
7162
 
7163
  if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7164
    {
7165
      isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7166
                                       NULL, NULL, NULL);
7167
      if (isymbuf2 == NULL)
7168
        goto done;
7169
 
7170
      if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7171
        elf_tdata (bfd2)->symbuf = ssymbuf2
7172
          = elf_create_symbuf (symcount2, isymbuf2);
7173
    }
7174
 
7175
  if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7176
    {
7177
      /* Optimized faster version.  */
7178
      bfd_size_type lo, hi, mid;
7179
      struct elf_symbol *symp;
7180
      struct elf_symbuf_symbol *ssym, *ssymend;
7181
 
7182
      lo = 0;
7183
      hi = ssymbuf1->count;
7184
      ssymbuf1++;
7185
      count1 = 0;
7186
      while (lo < hi)
7187
        {
7188
          mid = (lo + hi) / 2;
7189
          if (shndx1 < ssymbuf1[mid].st_shndx)
7190
            hi = mid;
7191
          else if (shndx1 > ssymbuf1[mid].st_shndx)
7192
            lo = mid + 1;
7193
          else
7194
            {
7195
              count1 = ssymbuf1[mid].count;
7196
              ssymbuf1 += mid;
7197
              break;
7198
            }
7199
        }
7200
 
7201
      lo = 0;
7202
      hi = ssymbuf2->count;
7203
      ssymbuf2++;
7204
      count2 = 0;
7205
      while (lo < hi)
7206
        {
7207
          mid = (lo + hi) / 2;
7208
          if (shndx2 < ssymbuf2[mid].st_shndx)
7209
            hi = mid;
7210
          else if (shndx2 > ssymbuf2[mid].st_shndx)
7211
            lo = mid + 1;
7212
          else
7213
            {
7214
              count2 = ssymbuf2[mid].count;
7215
              ssymbuf2 += mid;
7216
              break;
7217
            }
7218
        }
7219
 
7220
      if (count1 == 0 || count2 == 0 || count1 != count2)
7221
        goto done;
7222
 
7223
      symtable1 = (struct elf_symbol *)
7224
          bfd_malloc (count1 * sizeof (struct elf_symbol));
7225
      symtable2 = (struct elf_symbol *)
7226
          bfd_malloc (count2 * sizeof (struct elf_symbol));
7227
      if (symtable1 == NULL || symtable2 == NULL)
7228
        goto done;
7229
 
7230
      symp = symtable1;
7231
      for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7232
           ssym < ssymend; ssym++, symp++)
7233
        {
7234
          symp->u.ssym = ssym;
7235
          symp->name = bfd_elf_string_from_elf_section (bfd1,
7236
                                                        hdr1->sh_link,
7237
                                                        ssym->st_name);
7238
        }
7239
 
7240
      symp = symtable2;
7241
      for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7242
           ssym < ssymend; ssym++, symp++)
7243
        {
7244
          symp->u.ssym = ssym;
7245
          symp->name = bfd_elf_string_from_elf_section (bfd2,
7246
                                                        hdr2->sh_link,
7247
                                                        ssym->st_name);
7248
        }
7249
 
7250
      /* Sort symbol by name.  */
7251
      qsort (symtable1, count1, sizeof (struct elf_symbol),
7252
             elf_sym_name_compare);
7253
      qsort (symtable2, count1, sizeof (struct elf_symbol),
7254
             elf_sym_name_compare);
7255
 
7256
      for (i = 0; i < count1; i++)
7257
        /* Two symbols must have the same binding, type and name.  */
7258
        if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7259
            || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7260
            || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7261
          goto done;
7262
 
7263
      result = TRUE;
7264
      goto done;
7265
    }
7266
 
7267
  symtable1 = (struct elf_symbol *)
7268
      bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7269
  symtable2 = (struct elf_symbol *)
7270
      bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7271
  if (symtable1 == NULL || symtable2 == NULL)
7272
    goto done;
7273
 
7274
  /* Count definitions in the section.  */
7275
  count1 = 0;
7276
  for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7277
    if (isym->st_shndx == shndx1)
7278
      symtable1[count1++].u.isym = isym;
7279
 
7280
  count2 = 0;
7281
  for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7282
    if (isym->st_shndx == shndx2)
7283
      symtable2[count2++].u.isym = isym;
7284
 
7285
  if (count1 == 0 || count2 == 0 || count1 != count2)
7286
    goto done;
7287
 
7288
  for (i = 0; i < count1; i++)
7289
    symtable1[i].name
7290
      = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7291
                                         symtable1[i].u.isym->st_name);
7292
 
7293
  for (i = 0; i < count2; i++)
7294
    symtable2[i].name
7295
      = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7296
                                         symtable2[i].u.isym->st_name);
7297
 
7298
  /* Sort symbol by name.  */
7299
  qsort (symtable1, count1, sizeof (struct elf_symbol),
7300
         elf_sym_name_compare);
7301
  qsort (symtable2, count1, sizeof (struct elf_symbol),
7302
         elf_sym_name_compare);
7303
 
7304
  for (i = 0; i < count1; i++)
7305
    /* Two symbols must have the same binding, type and name.  */
7306
    if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7307
        || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7308
        || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7309
      goto done;
7310
 
7311
  result = TRUE;
7312
 
7313
done:
7314
  if (symtable1)
7315
    free (symtable1);
7316
  if (symtable2)
7317
    free (symtable2);
7318
  if (isymbuf1)
7319
    free (isymbuf1);
7320
  if (isymbuf2)
7321
    free (isymbuf2);
7322
 
7323
  return result;
7324
}
7325
 
7326
/* Return TRUE if 2 section types are compatible.  */
7327
 
7328
bfd_boolean
7329
_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7330
                                 bfd *bbfd, const asection *bsec)
7331
{
7332
  if (asec == NULL
7333
      || bsec == NULL
7334
      || abfd->xvec->flavour != bfd_target_elf_flavour
7335
      || bbfd->xvec->flavour != bfd_target_elf_flavour)
7336
    return TRUE;
7337
 
7338
  return elf_section_type (asec) == elf_section_type (bsec);
7339
}
7340
 
7341
/* Final phase of ELF linker.  */
7342
 
7343
/* A structure we use to avoid passing large numbers of arguments.  */
7344
 
7345
struct elf_final_link_info
7346
{
7347
  /* General link information.  */
7348
  struct bfd_link_info *info;
7349
  /* Output BFD.  */
7350
  bfd *output_bfd;
7351
  /* Symbol string table.  */
7352
  struct bfd_strtab_hash *symstrtab;
7353
  /* .dynsym section.  */
7354
  asection *dynsym_sec;
7355
  /* .hash section.  */
7356
  asection *hash_sec;
7357
  /* symbol version section (.gnu.version).  */
7358
  asection *symver_sec;
7359
  /* Buffer large enough to hold contents of any section.  */
7360
  bfd_byte *contents;
7361
  /* Buffer large enough to hold external relocs of any section.  */
7362
  void *external_relocs;
7363
  /* Buffer large enough to hold internal relocs of any section.  */
7364
  Elf_Internal_Rela *internal_relocs;
7365
  /* Buffer large enough to hold external local symbols of any input
7366
     BFD.  */
7367
  bfd_byte *external_syms;
7368
  /* And a buffer for symbol section indices.  */
7369
  Elf_External_Sym_Shndx *locsym_shndx;
7370
  /* Buffer large enough to hold internal local symbols of any input
7371
     BFD.  */
7372
  Elf_Internal_Sym *internal_syms;
7373
  /* Array large enough to hold a symbol index for each local symbol
7374
     of any input BFD.  */
7375
  long *indices;
7376
  /* Array large enough to hold a section pointer for each local
7377
     symbol of any input BFD.  */
7378
  asection **sections;
7379
  /* Buffer to hold swapped out symbols.  */
7380
  bfd_byte *symbuf;
7381
  /* And one for symbol section indices.  */
7382
  Elf_External_Sym_Shndx *symshndxbuf;
7383
  /* Number of swapped out symbols in buffer.  */
7384
  size_t symbuf_count;
7385
  /* Number of symbols which fit in symbuf.  */
7386
  size_t symbuf_size;
7387
  /* And same for symshndxbuf.  */
7388
  size_t shndxbuf_size;
7389
};
7390
 
7391
/* This struct is used to pass information to elf_link_output_extsym.  */
7392
 
7393
struct elf_outext_info
7394
{
7395
  bfd_boolean failed;
7396
  bfd_boolean localsyms;
7397
  struct elf_final_link_info *finfo;
7398
};
7399
 
7400
 
7401
/* Support for evaluating a complex relocation.
7402
 
7403
   Complex relocations are generalized, self-describing relocations.  The
7404
   implementation of them consists of two parts: complex symbols, and the
7405
   relocations themselves.
7406
 
7407
   The relocations are use a reserved elf-wide relocation type code (R_RELC
7408
   external / BFD_RELOC_RELC internal) and an encoding of relocation field
7409
   information (start bit, end bit, word width, etc) into the addend.  This
7410
   information is extracted from CGEN-generated operand tables within gas.
7411
 
7412
   Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7413
   internal) representing prefix-notation expressions, including but not
7414
   limited to those sorts of expressions normally encoded as addends in the
7415
   addend field.  The symbol mangling format is:
7416
 
7417
   <node> := <literal>
7418
          |  <unary-operator> ':' <node>
7419
          |  <binary-operator> ':' <node> ':' <node>
7420
          ;
7421
 
7422
   <literal> := 's' <digits=N> ':' <N character symbol name>
7423
             |  'S' <digits=N> ':' <N character section name>
7424
             |  '#' <hexdigits>
7425
             ;
7426
 
7427
   <binary-operator> := as in C
7428
   <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
7429
 
7430
static void
7431
set_symbol_value (bfd *bfd_with_globals,
7432
                  Elf_Internal_Sym *isymbuf,
7433
                  size_t locsymcount,
7434
                  size_t symidx,
7435
                  bfd_vma val)
7436
{
7437
  struct elf_link_hash_entry **sym_hashes;
7438
  struct elf_link_hash_entry *h;
7439
  size_t extsymoff = locsymcount;
7440
 
7441
  if (symidx < locsymcount)
7442
    {
7443
      Elf_Internal_Sym *sym;
7444
 
7445
      sym = isymbuf + symidx;
7446
      if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7447
        {
7448
          /* It is a local symbol: move it to the
7449
             "absolute" section and give it a value.  */
7450
          sym->st_shndx = SHN_ABS;
7451
          sym->st_value = val;
7452
          return;
7453
        }
7454
      BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7455
      extsymoff = 0;
7456
    }
7457
 
7458
  /* It is a global symbol: set its link type
7459
     to "defined" and give it a value.  */
7460
 
7461
  sym_hashes = elf_sym_hashes (bfd_with_globals);
7462
  h = sym_hashes [symidx - extsymoff];
7463
  while (h->root.type == bfd_link_hash_indirect
7464
         || h->root.type == bfd_link_hash_warning)
7465
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
7466
  h->root.type = bfd_link_hash_defined;
7467
  h->root.u.def.value = val;
7468
  h->root.u.def.section = bfd_abs_section_ptr;
7469
}
7470
 
7471
static bfd_boolean
7472
resolve_symbol (const char *name,
7473
                bfd *input_bfd,
7474
                struct elf_final_link_info *finfo,
7475
                bfd_vma *result,
7476
                Elf_Internal_Sym *isymbuf,
7477
                size_t locsymcount)
7478
{
7479
  Elf_Internal_Sym *sym;
7480
  struct bfd_link_hash_entry *global_entry;
7481
  const char *candidate = NULL;
7482
  Elf_Internal_Shdr *symtab_hdr;
7483
  size_t i;
7484
 
7485
  symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7486
 
7487
  for (i = 0; i < locsymcount; ++ i)
7488
    {
7489
      sym = isymbuf + i;
7490
 
7491
      if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7492
        continue;
7493
 
7494
      candidate = bfd_elf_string_from_elf_section (input_bfd,
7495
                                                   symtab_hdr->sh_link,
7496
                                                   sym->st_name);
7497
#ifdef DEBUG
7498
      printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7499
              name, candidate, (unsigned long) sym->st_value);
7500
#endif
7501
      if (candidate && strcmp (candidate, name) == 0)
7502
        {
7503
          asection *sec = finfo->sections [i];
7504
 
7505
          *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7506
          *result += sec->output_offset + sec->output_section->vma;
7507
#ifdef DEBUG
7508
          printf ("Found symbol with value %8.8lx\n",
7509
                  (unsigned long) *result);
7510
#endif
7511
          return TRUE;
7512
        }
7513
    }
7514
 
7515
  /* Hmm, haven't found it yet. perhaps it is a global.  */
7516
  global_entry = bfd_link_hash_lookup (finfo->info->hash, name,
7517
                                       FALSE, FALSE, TRUE);
7518
  if (!global_entry)
7519
    return FALSE;
7520
 
7521
  if (global_entry->type == bfd_link_hash_defined
7522
      || global_entry->type == bfd_link_hash_defweak)
7523
    {
7524
      *result = (global_entry->u.def.value
7525
                 + global_entry->u.def.section->output_section->vma
7526
                 + global_entry->u.def.section->output_offset);
7527
#ifdef DEBUG
7528
      printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7529
              global_entry->root.string, (unsigned long) *result);
7530
#endif
7531
      return TRUE;
7532
    }
7533
 
7534
  return FALSE;
7535
}
7536
 
7537
static bfd_boolean
7538
resolve_section (const char *name,
7539
                 asection *sections,
7540
                 bfd_vma *result)
7541
{
7542
  asection *curr;
7543
  unsigned int len;
7544
 
7545
  for (curr = sections; curr; curr = curr->next)
7546
    if (strcmp (curr->name, name) == 0)
7547
      {
7548
        *result = curr->vma;
7549
        return TRUE;
7550
      }
7551
 
7552
  /* Hmm. still haven't found it. try pseudo-section names.  */
7553
  for (curr = sections; curr; curr = curr->next)
7554
    {
7555
      len = strlen (curr->name);
7556
      if (len > strlen (name))
7557
        continue;
7558
 
7559
      if (strncmp (curr->name, name, len) == 0)
7560
        {
7561
          if (strncmp (".end", name + len, 4) == 0)
7562
            {
7563
              *result = curr->vma + curr->size;
7564
              return TRUE;
7565
            }
7566
 
7567
          /* Insert more pseudo-section names here, if you like.  */
7568
        }
7569
    }
7570
 
7571
  return FALSE;
7572
}
7573
 
7574
static void
7575
undefined_reference (const char *reftype, const char *name)
7576
{
7577
  _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7578
                      reftype, name);
7579
}
7580
 
7581
static bfd_boolean
7582
eval_symbol (bfd_vma *result,
7583
             const char **symp,
7584
             bfd *input_bfd,
7585
             struct elf_final_link_info *finfo,
7586
             bfd_vma dot,
7587
             Elf_Internal_Sym *isymbuf,
7588
             size_t locsymcount,
7589
             int signed_p)
7590
{
7591
  size_t len;
7592
  size_t symlen;
7593
  bfd_vma a;
7594
  bfd_vma b;
7595
  char symbuf[4096];
7596
  const char *sym = *symp;
7597
  const char *symend;
7598
  bfd_boolean symbol_is_section = FALSE;
7599
 
7600
  len = strlen (sym);
7601
  symend = sym + len;
7602
 
7603
  if (len < 1 || len > sizeof (symbuf))
7604
    {
7605
      bfd_set_error (bfd_error_invalid_operation);
7606
      return FALSE;
7607
    }
7608
 
7609
  switch (* sym)
7610
    {
7611
    case '.':
7612
      *result = dot;
7613
      *symp = sym + 1;
7614
      return TRUE;
7615
 
7616
    case '#':
7617
      ++sym;
7618
      *result = strtoul (sym, (char **) symp, 16);
7619
      return TRUE;
7620
 
7621
    case 'S':
7622
      symbol_is_section = TRUE;
7623
    case 's':
7624
      ++sym;
7625
      symlen = strtol (sym, (char **) symp, 10);
7626
      sym = *symp + 1; /* Skip the trailing ':'.  */
7627
 
7628
      if (symend < sym || symlen + 1 > sizeof (symbuf))
7629
        {
7630
          bfd_set_error (bfd_error_invalid_operation);
7631
          return FALSE;
7632
        }
7633
 
7634
      memcpy (symbuf, sym, symlen);
7635
      symbuf[symlen] = '\0';
7636
      *symp = sym + symlen;
7637
 
7638
      /* Is it always possible, with complex symbols, that gas "mis-guessed"
7639
         the symbol as a section, or vice-versa. so we're pretty liberal in our
7640
         interpretation here; section means "try section first", not "must be a
7641
         section", and likewise with symbol.  */
7642
 
7643
      if (symbol_is_section)
7644
        {
7645
          if (!resolve_section (symbuf, finfo->output_bfd->sections, result)
7646
              && !resolve_symbol (symbuf, input_bfd, finfo, result,
7647
                                  isymbuf, locsymcount))
7648
            {
7649
              undefined_reference ("section", symbuf);
7650
              return FALSE;
7651
            }
7652
        }
7653
      else
7654
        {
7655
          if (!resolve_symbol (symbuf, input_bfd, finfo, result,
7656
                               isymbuf, locsymcount)
7657
              && !resolve_section (symbuf, finfo->output_bfd->sections,
7658
                                   result))
7659
            {
7660
              undefined_reference ("symbol", symbuf);
7661
              return FALSE;
7662
            }
7663
        }
7664
 
7665
      return TRUE;
7666
 
7667
      /* All that remains are operators.  */
7668
 
7669
#define UNARY_OP(op)                                            \
7670
  if (strncmp (sym, #op, strlen (#op)) == 0)                    \
7671
    {                                                           \
7672
      sym += strlen (#op);                                      \
7673
      if (*sym == ':')                                          \
7674
        ++sym;                                                  \
7675
      *symp = sym;                                              \
7676
      if (!eval_symbol (&a, symp, input_bfd, finfo, dot,        \
7677
                        isymbuf, locsymcount, signed_p))        \
7678
        return FALSE;                                           \
7679
      if (signed_p)                                             \
7680
        *result = op ((bfd_signed_vma) a);                      \
7681
      else                                                      \
7682
        *result = op a;                                         \
7683
      return TRUE;                                              \
7684
    }
7685
 
7686
#define BINARY_OP(op)                                           \
7687
  if (strncmp (sym, #op, strlen (#op)) == 0)                    \
7688
    {                                                           \
7689
      sym += strlen (#op);                                      \
7690
      if (*sym == ':')                                          \
7691
        ++sym;                                                  \
7692
      *symp = sym;                                              \
7693
      if (!eval_symbol (&a, symp, input_bfd, finfo, dot,        \
7694
                        isymbuf, locsymcount, signed_p))        \
7695
        return FALSE;                                           \
7696
      ++*symp;                                                  \
7697
      if (!eval_symbol (&b, symp, input_bfd, finfo, dot,        \
7698
                        isymbuf, locsymcount, signed_p))        \
7699
        return FALSE;                                           \
7700
      if (signed_p)                                             \
7701
        *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7702
      else                                                      \
7703
        *result = a op b;                                       \
7704
      return TRUE;                                              \
7705
    }
7706
 
7707
    default:
7708
      UNARY_OP  (0-);
7709
      BINARY_OP (<<);
7710
      BINARY_OP (>>);
7711
      BINARY_OP (==);
7712
      BINARY_OP (!=);
7713
      BINARY_OP (<=);
7714
      BINARY_OP (>=);
7715
      BINARY_OP (&&);
7716
      BINARY_OP (||);
7717
      UNARY_OP  (~);
7718
      UNARY_OP  (!);
7719
      BINARY_OP (*);
7720
      BINARY_OP (/);
7721
      BINARY_OP (%);
7722
      BINARY_OP (^);
7723
      BINARY_OP (|);
7724
      BINARY_OP (&);
7725
      BINARY_OP (+);
7726
      BINARY_OP (-);
7727
      BINARY_OP (<);
7728
      BINARY_OP (>);
7729
#undef UNARY_OP
7730
#undef BINARY_OP
7731
      _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7732
      bfd_set_error (bfd_error_invalid_operation);
7733
      return FALSE;
7734
    }
7735
}
7736
 
7737
static void
7738
put_value (bfd_vma size,
7739
           unsigned long chunksz,
7740
           bfd *input_bfd,
7741
           bfd_vma x,
7742
           bfd_byte *location)
7743
{
7744
  location += (size - chunksz);
7745
 
7746
  for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
7747
    {
7748
      switch (chunksz)
7749
        {
7750
        default:
7751
        case 0:
7752
          abort ();
7753
        case 1:
7754
          bfd_put_8 (input_bfd, x, location);
7755
          break;
7756
        case 2:
7757
          bfd_put_16 (input_bfd, x, location);
7758
          break;
7759
        case 4:
7760
          bfd_put_32 (input_bfd, x, location);
7761
          break;
7762
        case 8:
7763
#ifdef BFD64
7764
          bfd_put_64 (input_bfd, x, location);
7765
#else
7766
          abort ();
7767
#endif
7768
          break;
7769
        }
7770
    }
7771
}
7772
 
7773
static bfd_vma
7774
get_value (bfd_vma size,
7775
           unsigned long chunksz,
7776
           bfd *input_bfd,
7777
           bfd_byte *location)
7778
{
7779
  bfd_vma x = 0;
7780
 
7781
  for (; size; size -= chunksz, location += chunksz)
7782
    {
7783
      switch (chunksz)
7784
        {
7785
        default:
7786
        case 0:
7787
          abort ();
7788
        case 1:
7789
          x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7790
          break;
7791
        case 2:
7792
          x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7793
          break;
7794
        case 4:
7795
          x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7796
          break;
7797
        case 8:
7798
#ifdef BFD64
7799
          x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7800
#else
7801
          abort ();
7802
#endif
7803
          break;
7804
        }
7805
    }
7806
  return x;
7807
}
7808
 
7809
static void
7810
decode_complex_addend (unsigned long *start,   /* in bits */
7811
                       unsigned long *oplen,   /* in bits */
7812
                       unsigned long *len,     /* in bits */
7813
                       unsigned long *wordsz,  /* in bytes */
7814
                       unsigned long *chunksz, /* in bytes */
7815
                       unsigned long *lsb0_p,
7816
                       unsigned long *signed_p,
7817
                       unsigned long *trunc_p,
7818
                       unsigned long encoded)
7819
{
7820
  * start     =  encoded        & 0x3F;
7821
  * len       = (encoded >>  6) & 0x3F;
7822
  * oplen     = (encoded >> 12) & 0x3F;
7823
  * wordsz    = (encoded >> 18) & 0xF;
7824
  * chunksz   = (encoded >> 22) & 0xF;
7825
  * lsb0_p    = (encoded >> 27) & 1;
7826
  * signed_p  = (encoded >> 28) & 1;
7827
  * trunc_p   = (encoded >> 29) & 1;
7828
}
7829
 
7830
bfd_reloc_status_type
7831
bfd_elf_perform_complex_relocation (bfd *input_bfd,
7832
                                    asection *input_section ATTRIBUTE_UNUSED,
7833
                                    bfd_byte *contents,
7834
                                    Elf_Internal_Rela *rel,
7835
                                    bfd_vma relocation)
7836
{
7837
  bfd_vma shift, x, mask;
7838
  unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
7839
  bfd_reloc_status_type r;
7840
 
7841
  /*  Perform this reloc, since it is complex.
7842
      (this is not to say that it necessarily refers to a complex
7843
      symbol; merely that it is a self-describing CGEN based reloc.
7844
      i.e. the addend has the complete reloc information (bit start, end,
7845
      word size, etc) encoded within it.).  */
7846
 
7847
  decode_complex_addend (&start, &oplen, &len, &wordsz,
7848
                         &chunksz, &lsb0_p, &signed_p,
7849
                         &trunc_p, rel->r_addend);
7850
 
7851
  mask = (((1L << (len - 1)) - 1) << 1) | 1;
7852
 
7853
  if (lsb0_p)
7854
    shift = (start + 1) - len;
7855
  else
7856
    shift = (8 * wordsz) - (start + len);
7857
 
7858
  /* FIXME: octets_per_byte.  */
7859
  x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7860
 
7861
#ifdef DEBUG
7862
  printf ("Doing complex reloc: "
7863
          "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7864
          "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7865
          "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7866
          lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7867
          oplen, (unsigned long) x, (unsigned long) mask,
7868
          (unsigned long) relocation);
7869
#endif
7870
 
7871
  r = bfd_reloc_ok;
7872
  if (! trunc_p)
7873
    /* Now do an overflow check.  */
7874
    r = bfd_check_overflow ((signed_p
7875
                             ? complain_overflow_signed
7876
                             : complain_overflow_unsigned),
7877
                            len, 0, (8 * wordsz),
7878
                            relocation);
7879
 
7880
  /* Do the deed.  */
7881
  x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7882
 
7883
#ifdef DEBUG
7884
  printf ("           relocation: %8.8lx\n"
7885
          "         shifted mask: %8.8lx\n"
7886
          " shifted/masked reloc: %8.8lx\n"
7887
          "               result: %8.8lx\n",
7888
          (unsigned long) relocation, (unsigned long) (mask << shift),
7889
          (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
7890
#endif
7891
  /* FIXME: octets_per_byte.  */
7892
  put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
7893
  return r;
7894
}
7895
 
7896
/* When performing a relocatable link, the input relocations are
7897
   preserved.  But, if they reference global symbols, the indices
7898
   referenced must be updated.  Update all the relocations found in
7899
   RELDATA.  */
7900
 
7901
static void
7902
elf_link_adjust_relocs (bfd *abfd,
7903
                        struct bfd_elf_section_reloc_data *reldata)
7904
{
7905
  unsigned int i;
7906
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7907
  bfd_byte *erela;
7908
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7909
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7910
  bfd_vma r_type_mask;
7911
  int r_sym_shift;
7912
  unsigned int count = reldata->count;
7913
  struct elf_link_hash_entry **rel_hash = reldata->hashes;
7914
 
7915
  if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
7916
    {
7917
      swap_in = bed->s->swap_reloc_in;
7918
      swap_out = bed->s->swap_reloc_out;
7919
    }
7920
  else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
7921
    {
7922
      swap_in = bed->s->swap_reloca_in;
7923
      swap_out = bed->s->swap_reloca_out;
7924
    }
7925
  else
7926
    abort ();
7927
 
7928
  if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7929
    abort ();
7930
 
7931
  if (bed->s->arch_size == 32)
7932
    {
7933
      r_type_mask = 0xff;
7934
      r_sym_shift = 8;
7935
    }
7936
  else
7937
    {
7938
      r_type_mask = 0xffffffff;
7939
      r_sym_shift = 32;
7940
    }
7941
 
7942
  erela = reldata->hdr->contents;
7943
  for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
7944
    {
7945
      Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7946
      unsigned int j;
7947
 
7948
      if (*rel_hash == NULL)
7949
        continue;
7950
 
7951
      BFD_ASSERT ((*rel_hash)->indx >= 0);
7952
 
7953
      (*swap_in) (abfd, erela, irela);
7954
      for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7955
        irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7956
                           | (irela[j].r_info & r_type_mask));
7957
      (*swap_out) (abfd, irela, erela);
7958
    }
7959
}
7960
 
7961
struct elf_link_sort_rela
7962
{
7963
  union {
7964
    bfd_vma offset;
7965
    bfd_vma sym_mask;
7966
  } u;
7967
  enum elf_reloc_type_class type;
7968
  /* We use this as an array of size int_rels_per_ext_rel.  */
7969
  Elf_Internal_Rela rela[1];
7970
};
7971
 
7972
static int
7973
elf_link_sort_cmp1 (const void *A, const void *B)
7974
{
7975
  const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
7976
  const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
7977
  int relativea, relativeb;
7978
 
7979
  relativea = a->type == reloc_class_relative;
7980
  relativeb = b->type == reloc_class_relative;
7981
 
7982
  if (relativea < relativeb)
7983
    return 1;
7984
  if (relativea > relativeb)
7985
    return -1;
7986
  if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
7987
    return -1;
7988
  if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
7989
    return 1;
7990
  if (a->rela->r_offset < b->rela->r_offset)
7991
    return -1;
7992
  if (a->rela->r_offset > b->rela->r_offset)
7993
    return 1;
7994
  return 0;
7995
}
7996
 
7997
static int
7998
elf_link_sort_cmp2 (const void *A, const void *B)
7999
{
8000
  const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8001
  const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8002
  int copya, copyb;
8003
 
8004
  if (a->u.offset < b->u.offset)
8005
    return -1;
8006
  if (a->u.offset > b->u.offset)
8007
    return 1;
8008
  copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
8009
  copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
8010
  if (copya < copyb)
8011
    return -1;
8012
  if (copya > copyb)
8013
    return 1;
8014
  if (a->rela->r_offset < b->rela->r_offset)
8015
    return -1;
8016
  if (a->rela->r_offset > b->rela->r_offset)
8017
    return 1;
8018
  return 0;
8019
}
8020
 
8021
static size_t
8022
elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8023
{
8024
  asection *dynamic_relocs;
8025
  asection *rela_dyn;
8026
  asection *rel_dyn;
8027
  bfd_size_type count, size;
8028
  size_t i, ret, sort_elt, ext_size;
8029
  bfd_byte *sort, *s_non_relative, *p;
8030
  struct elf_link_sort_rela *sq;
8031
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8032
  int i2e = bed->s->int_rels_per_ext_rel;
8033
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8034
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8035
  struct bfd_link_order *lo;
8036
  bfd_vma r_sym_mask;
8037
  bfd_boolean use_rela;
8038
 
8039
  /* Find a dynamic reloc section.  */
8040
  rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8041
  rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
8042
  if (rela_dyn != NULL && rela_dyn->size > 0
8043
      && rel_dyn != NULL && rel_dyn->size > 0)
8044
    {
8045
      bfd_boolean use_rela_initialised = FALSE;
8046
 
8047
      /* This is just here to stop gcc from complaining.
8048
         It's initialization checking code is not perfect.  */
8049
      use_rela = TRUE;
8050
 
8051
      /* Both sections are present.  Examine the sizes
8052
         of the indirect sections to help us choose.  */
8053
      for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8054
        if (lo->type == bfd_indirect_link_order)
8055
          {
8056
            asection *o = lo->u.indirect.section;
8057
 
8058
            if ((o->size % bed->s->sizeof_rela) == 0)
8059
              {
8060
                if ((o->size % bed->s->sizeof_rel) == 0)
8061
                  /* Section size is divisible by both rel and rela sizes.
8062
                     It is of no help to us.  */
8063
                  ;
8064
                else
8065
                  {
8066
                    /* Section size is only divisible by rela.  */
8067
                    if (use_rela_initialised && (use_rela == FALSE))
8068
                      {
8069
                        _bfd_error_handler
8070
                          (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8071
                        bfd_set_error (bfd_error_invalid_operation);
8072
                        return 0;
8073
                      }
8074
                    else
8075
                      {
8076
                        use_rela = TRUE;
8077
                        use_rela_initialised = TRUE;
8078
                      }
8079
                  }
8080
              }
8081
            else if ((o->size % bed->s->sizeof_rel) == 0)
8082
              {
8083
                /* Section size is only divisible by rel.  */
8084
                if (use_rela_initialised && (use_rela == TRUE))
8085
                  {
8086
                    _bfd_error_handler
8087
                      (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8088
                    bfd_set_error (bfd_error_invalid_operation);
8089
                    return 0;
8090
                  }
8091
                else
8092
                  {
8093
                    use_rela = FALSE;
8094
                    use_rela_initialised = TRUE;
8095
                  }
8096
              }
8097
            else
8098
              {
8099
                /* The section size is not divisible by either - something is wrong.  */
8100
                _bfd_error_handler
8101
                  (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8102
                bfd_set_error (bfd_error_invalid_operation);
8103
                return 0;
8104
              }
8105
          }
8106
 
8107
      for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8108
        if (lo->type == bfd_indirect_link_order)
8109
          {
8110
            asection *o = lo->u.indirect.section;
8111
 
8112
            if ((o->size % bed->s->sizeof_rela) == 0)
8113
              {
8114
                if ((o->size % bed->s->sizeof_rel) == 0)
8115
                  /* Section size is divisible by both rel and rela sizes.
8116
                     It is of no help to us.  */
8117
                  ;
8118
                else
8119
                  {
8120
                    /* Section size is only divisible by rela.  */
8121
                    if (use_rela_initialised && (use_rela == FALSE))
8122
                      {
8123
                        _bfd_error_handler
8124
                          (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8125
                        bfd_set_error (bfd_error_invalid_operation);
8126
                        return 0;
8127
                      }
8128
                    else
8129
                      {
8130
                        use_rela = TRUE;
8131
                        use_rela_initialised = TRUE;
8132
                      }
8133
                  }
8134
              }
8135
            else if ((o->size % bed->s->sizeof_rel) == 0)
8136
              {
8137
                /* Section size is only divisible by rel.  */
8138
                if (use_rela_initialised && (use_rela == TRUE))
8139
                  {
8140
                    _bfd_error_handler
8141
                      (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8142
                    bfd_set_error (bfd_error_invalid_operation);
8143
                    return 0;
8144
                  }
8145
                else
8146
                  {
8147
                    use_rela = FALSE;
8148
                    use_rela_initialised = TRUE;
8149
                  }
8150
              }
8151
            else
8152
              {
8153
                /* The section size is not divisible by either - something is wrong.  */
8154
                _bfd_error_handler
8155
                  (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8156
                bfd_set_error (bfd_error_invalid_operation);
8157
                return 0;
8158
              }
8159
          }
8160
 
8161
      if (! use_rela_initialised)
8162
        /* Make a guess.  */
8163
        use_rela = TRUE;
8164
    }
8165
  else if (rela_dyn != NULL && rela_dyn->size > 0)
8166
    use_rela = TRUE;
8167
  else if (rel_dyn != NULL && rel_dyn->size > 0)
8168
    use_rela = FALSE;
8169
  else
8170
    return 0;
8171
 
8172
  if (use_rela)
8173
    {
8174
      dynamic_relocs = rela_dyn;
8175
      ext_size = bed->s->sizeof_rela;
8176
      swap_in = bed->s->swap_reloca_in;
8177
      swap_out = bed->s->swap_reloca_out;
8178
    }
8179
  else
8180
    {
8181
      dynamic_relocs = rel_dyn;
8182
      ext_size = bed->s->sizeof_rel;
8183
      swap_in = bed->s->swap_reloc_in;
8184
      swap_out = bed->s->swap_reloc_out;
8185
    }
8186
 
8187
  size = 0;
8188
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8189
    if (lo->type == bfd_indirect_link_order)
8190
      size += lo->u.indirect.section->size;
8191
 
8192
  if (size != dynamic_relocs->size)
8193
    return 0;
8194
 
8195
  sort_elt = (sizeof (struct elf_link_sort_rela)
8196
              + (i2e - 1) * sizeof (Elf_Internal_Rela));
8197
 
8198
  count = dynamic_relocs->size / ext_size;
8199
  if (count == 0)
8200
    return 0;
8201
  sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8202
 
8203
  if (sort == NULL)
8204
    {
8205
      (*info->callbacks->warning)
8206
        (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8207
      return 0;
8208
    }
8209
 
8210
  if (bed->s->arch_size == 32)
8211
    r_sym_mask = ~(bfd_vma) 0xff;
8212
  else
8213
    r_sym_mask = ~(bfd_vma) 0xffffffff;
8214
 
8215
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8216
    if (lo->type == bfd_indirect_link_order)
8217
      {
8218
        bfd_byte *erel, *erelend;
8219
        asection *o = lo->u.indirect.section;
8220
 
8221
        if (o->contents == NULL && o->size != 0)
8222
          {
8223
            /* This is a reloc section that is being handled as a normal
8224
               section.  See bfd_section_from_shdr.  We can't combine
8225
               relocs in this case.  */
8226
            free (sort);
8227
            return 0;
8228
          }
8229
        erel = o->contents;
8230
        erelend = o->contents + o->size;
8231
        /* FIXME: octets_per_byte.  */
8232
        p = sort + o->output_offset / ext_size * sort_elt;
8233
 
8234
        while (erel < erelend)
8235
          {
8236
            struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8237
 
8238
            (*swap_in) (abfd, erel, s->rela);
8239
            s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
8240
            s->u.sym_mask = r_sym_mask;
8241
            p += sort_elt;
8242
            erel += ext_size;
8243
          }
8244
      }
8245
 
8246
  qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8247
 
8248
  for (i = 0, p = sort; i < count; i++, p += sort_elt)
8249
    {
8250
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8251
      if (s->type != reloc_class_relative)
8252
        break;
8253
    }
8254
  ret = i;
8255
  s_non_relative = p;
8256
 
8257
  sq = (struct elf_link_sort_rela *) s_non_relative;
8258
  for (; i < count; i++, p += sort_elt)
8259
    {
8260
      struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8261
      if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8262
        sq = sp;
8263
      sp->u.offset = sq->rela->r_offset;
8264
    }
8265
 
8266
  qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8267
 
8268
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8269
    if (lo->type == bfd_indirect_link_order)
8270
      {
8271
        bfd_byte *erel, *erelend;
8272
        asection *o = lo->u.indirect.section;
8273
 
8274
        erel = o->contents;
8275
        erelend = o->contents + o->size;
8276
        /* FIXME: octets_per_byte.  */
8277
        p = sort + o->output_offset / ext_size * sort_elt;
8278
        while (erel < erelend)
8279
          {
8280
            struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8281
            (*swap_out) (abfd, s->rela, erel);
8282
            p += sort_elt;
8283
            erel += ext_size;
8284
          }
8285
      }
8286
 
8287
  free (sort);
8288
  *psec = dynamic_relocs;
8289
  return ret;
8290
}
8291
 
8292
/* Flush the output symbols to the file.  */
8293
 
8294
static bfd_boolean
8295
elf_link_flush_output_syms (struct elf_final_link_info *finfo,
8296
                            const struct elf_backend_data *bed)
8297
{
8298
  if (finfo->symbuf_count > 0)
8299
    {
8300
      Elf_Internal_Shdr *hdr;
8301
      file_ptr pos;
8302
      bfd_size_type amt;
8303
 
8304
      hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
8305
      pos = hdr->sh_offset + hdr->sh_size;
8306
      amt = finfo->symbuf_count * bed->s->sizeof_sym;
8307
      if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
8308
          || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
8309
        return FALSE;
8310
 
8311
      hdr->sh_size += amt;
8312
      finfo->symbuf_count = 0;
8313
    }
8314
 
8315
  return TRUE;
8316
}
8317
 
8318
/* Add a symbol to the output symbol table.  */
8319
 
8320
static int
8321
elf_link_output_sym (struct elf_final_link_info *finfo,
8322
                     const char *name,
8323
                     Elf_Internal_Sym *elfsym,
8324
                     asection *input_sec,
8325
                     struct elf_link_hash_entry *h)
8326
{
8327
  bfd_byte *dest;
8328
  Elf_External_Sym_Shndx *destshndx;
8329
  int (*output_symbol_hook)
8330
    (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8331
     struct elf_link_hash_entry *);
8332
  const struct elf_backend_data *bed;
8333
 
8334
  bed = get_elf_backend_data (finfo->output_bfd);
8335
  output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8336
  if (output_symbol_hook != NULL)
8337
    {
8338
      int ret = (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h);
8339
      if (ret != 1)
8340
        return ret;
8341
    }
8342
 
8343
  if (name == NULL || *name == '\0')
8344
    elfsym->st_name = 0;
8345
  else if (input_sec->flags & SEC_EXCLUDE)
8346
    elfsym->st_name = 0;
8347
  else
8348
    {
8349
      elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
8350
                                                            name, TRUE, FALSE);
8351
      if (elfsym->st_name == (unsigned long) -1)
8352
        return 0;
8353
    }
8354
 
8355
  if (finfo->symbuf_count >= finfo->symbuf_size)
8356
    {
8357
      if (! elf_link_flush_output_syms (finfo, bed))
8358
        return 0;
8359
    }
8360
 
8361
  dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
8362
  destshndx = finfo->symshndxbuf;
8363
  if (destshndx != NULL)
8364
    {
8365
      if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
8366
        {
8367
          bfd_size_type amt;
8368
 
8369
          amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
8370
          destshndx = (Elf_External_Sym_Shndx *) bfd_realloc (destshndx,
8371
                                                              amt * 2);
8372
          if (destshndx == NULL)
8373
            return 0;
8374
          finfo->symshndxbuf = destshndx;
8375
          memset ((char *) destshndx + amt, 0, amt);
8376
          finfo->shndxbuf_size *= 2;
8377
        }
8378
      destshndx += bfd_get_symcount (finfo->output_bfd);
8379
    }
8380
 
8381
  bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
8382
  finfo->symbuf_count += 1;
8383
  bfd_get_symcount (finfo->output_bfd) += 1;
8384
 
8385
  return 1;
8386
}
8387
 
8388
/* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
8389
 
8390
static bfd_boolean
8391
check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8392
{
8393
  if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8394
      && sym->st_shndx < SHN_LORESERVE)
8395
    {
8396
      /* The gABI doesn't support dynamic symbols in output sections
8397
         beyond 64k.  */
8398
      (*_bfd_error_handler)
8399
        (_("%B: Too many sections: %d (>= %d)"),
8400
         abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8401
      bfd_set_error (bfd_error_nonrepresentable_section);
8402
      return FALSE;
8403
    }
8404
  return TRUE;
8405
}
8406
 
8407
/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8408
   allowing an unsatisfied unversioned symbol in the DSO to match a
8409
   versioned symbol that would normally require an explicit version.
8410
   We also handle the case that a DSO references a hidden symbol
8411
   which may be satisfied by a versioned symbol in another DSO.  */
8412
 
8413
static bfd_boolean
8414
elf_link_check_versioned_symbol (struct bfd_link_info *info,
8415
                                 const struct elf_backend_data *bed,
8416
                                 struct elf_link_hash_entry *h)
8417
{
8418
  bfd *abfd;
8419
  struct elf_link_loaded_list *loaded;
8420
 
8421
  if (!is_elf_hash_table (info->hash))
8422
    return FALSE;
8423
 
8424
  switch (h->root.type)
8425
    {
8426
    default:
8427
      abfd = NULL;
8428
      break;
8429
 
8430
    case bfd_link_hash_undefined:
8431
    case bfd_link_hash_undefweak:
8432
      abfd = h->root.u.undef.abfd;
8433
      if ((abfd->flags & DYNAMIC) == 0
8434
          || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8435
        return FALSE;
8436
      break;
8437
 
8438
    case bfd_link_hash_defined:
8439
    case bfd_link_hash_defweak:
8440
      abfd = h->root.u.def.section->owner;
8441
      break;
8442
 
8443
    case bfd_link_hash_common:
8444
      abfd = h->root.u.c.p->section->owner;
8445
      break;
8446
    }
8447
  BFD_ASSERT (abfd != NULL);
8448
 
8449
  for (loaded = elf_hash_table (info)->loaded;
8450
       loaded != NULL;
8451
       loaded = loaded->next)
8452
    {
8453
      bfd *input;
8454
      Elf_Internal_Shdr *hdr;
8455
      bfd_size_type symcount;
8456
      bfd_size_type extsymcount;
8457
      bfd_size_type extsymoff;
8458
      Elf_Internal_Shdr *versymhdr;
8459
      Elf_Internal_Sym *isym;
8460
      Elf_Internal_Sym *isymend;
8461
      Elf_Internal_Sym *isymbuf;
8462
      Elf_External_Versym *ever;
8463
      Elf_External_Versym *extversym;
8464
 
8465
      input = loaded->abfd;
8466
 
8467
      /* We check each DSO for a possible hidden versioned definition.  */
8468
      if (input == abfd
8469
          || (input->flags & DYNAMIC) == 0
8470
          || elf_dynversym (input) == 0)
8471
        continue;
8472
 
8473
      hdr = &elf_tdata (input)->dynsymtab_hdr;
8474
 
8475
      symcount = hdr->sh_size / bed->s->sizeof_sym;
8476
      if (elf_bad_symtab (input))
8477
        {
8478
          extsymcount = symcount;
8479
          extsymoff = 0;
8480
        }
8481
      else
8482
        {
8483
          extsymcount = symcount - hdr->sh_info;
8484
          extsymoff = hdr->sh_info;
8485
        }
8486
 
8487
      if (extsymcount == 0)
8488
        continue;
8489
 
8490
      isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8491
                                      NULL, NULL, NULL);
8492
      if (isymbuf == NULL)
8493
        return FALSE;
8494
 
8495
      /* Read in any version definitions.  */
8496
      versymhdr = &elf_tdata (input)->dynversym_hdr;
8497
      extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
8498
      if (extversym == NULL)
8499
        goto error_ret;
8500
 
8501
      if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8502
          || (bfd_bread (extversym, versymhdr->sh_size, input)
8503
              != versymhdr->sh_size))
8504
        {
8505
          free (extversym);
8506
        error_ret:
8507
          free (isymbuf);
8508
          return FALSE;
8509
        }
8510
 
8511
      ever = extversym + extsymoff;
8512
      isymend = isymbuf + extsymcount;
8513
      for (isym = isymbuf; isym < isymend; isym++, ever++)
8514
        {
8515
          const char *name;
8516
          Elf_Internal_Versym iver;
8517
          unsigned short version_index;
8518
 
8519
          if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8520
              || isym->st_shndx == SHN_UNDEF)
8521
            continue;
8522
 
8523
          name = bfd_elf_string_from_elf_section (input,
8524
                                                  hdr->sh_link,
8525
                                                  isym->st_name);
8526
          if (strcmp (name, h->root.root.string) != 0)
8527
            continue;
8528
 
8529
          _bfd_elf_swap_versym_in (input, ever, &iver);
8530
 
8531
          if ((iver.vs_vers & VERSYM_HIDDEN) == 0
8532
              && !(h->def_regular
8533
                   && h->forced_local))
8534
            {
8535
              /* If we have a non-hidden versioned sym, then it should
8536
                 have provided a definition for the undefined sym unless
8537
                 it is defined in a non-shared object and forced local.
8538
               */
8539
              abort ();
8540
            }
8541
 
8542
          version_index = iver.vs_vers & VERSYM_VERSION;
8543
          if (version_index == 1 || version_index == 2)
8544
            {
8545
              /* This is the base or first version.  We can use it.  */
8546
              free (extversym);
8547
              free (isymbuf);
8548
              return TRUE;
8549
            }
8550
        }
8551
 
8552
      free (extversym);
8553
      free (isymbuf);
8554
    }
8555
 
8556
  return FALSE;
8557
}
8558
 
8559
/* Add an external symbol to the symbol table.  This is called from
8560
   the hash table traversal routine.  When generating a shared object,
8561
   we go through the symbol table twice.  The first time we output
8562
   anything that might have been forced to local scope in a version
8563
   script.  The second time we output the symbols that are still
8564
   global symbols.  */
8565
 
8566
static bfd_boolean
8567 148 khays
elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
8568 14 khays
{
8569 148 khays
  struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
8570 14 khays
  struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8571
  struct elf_final_link_info *finfo = eoinfo->finfo;
8572
  bfd_boolean strip;
8573
  Elf_Internal_Sym sym;
8574
  asection *input_sec;
8575
  const struct elf_backend_data *bed;
8576
  long indx;
8577
  int ret;
8578
 
8579
  if (h->root.type == bfd_link_hash_warning)
8580
    {
8581
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
8582
      if (h->root.type == bfd_link_hash_new)
8583
        return TRUE;
8584
    }
8585
 
8586
  /* Decide whether to output this symbol in this pass.  */
8587
  if (eoinfo->localsyms)
8588
    {
8589
      if (!h->forced_local)
8590
        return TRUE;
8591
    }
8592
  else
8593
    {
8594
      if (h->forced_local)
8595
        return TRUE;
8596
    }
8597
 
8598
  bed = get_elf_backend_data (finfo->output_bfd);
8599
 
8600
  if (h->root.type == bfd_link_hash_undefined)
8601
    {
8602
      /* If we have an undefined symbol reference here then it must have
8603
         come from a shared library that is being linked in.  (Undefined
8604
         references in regular files have already been handled unless
8605
         they are in unreferenced sections which are removed by garbage
8606
         collection).  */
8607
      bfd_boolean ignore_undef = FALSE;
8608
 
8609
      /* Some symbols may be special in that the fact that they're
8610
         undefined can be safely ignored - let backend determine that.  */
8611
      if (bed->elf_backend_ignore_undef_symbol)
8612
        ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8613
 
8614
      /* If we are reporting errors for this situation then do so now.  */
8615
      if (!ignore_undef
8616
          && h->ref_dynamic
8617
          && (!h->ref_regular || finfo->info->gc_sections)
8618
          && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
8619
          && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
8620
        {
8621
          if (! (finfo->info->callbacks->undefined_symbol
8622
                 (finfo->info, h->root.root.string,
8623
                  h->ref_regular ? NULL : h->root.u.undef.abfd,
8624
                  NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
8625
            {
8626
              bfd_set_error (bfd_error_bad_value);
8627
              eoinfo->failed = TRUE;
8628
              return FALSE;
8629
            }
8630
        }
8631
    }
8632
 
8633
  /* We should also warn if a forced local symbol is referenced from
8634
     shared libraries.  */
8635
  if (! finfo->info->relocatable
8636
      && (! finfo->info->shared)
8637
      && h->forced_local
8638
      && h->ref_dynamic
8639
      && !h->dynamic_def
8640
      && !h->dynamic_weak
8641
      && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
8642
    {
8643
      bfd *def_bfd;
8644
      const char *msg;
8645
 
8646
      if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
8647
        msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
8648
      else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
8649
        msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
8650
      else
8651
        msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8652
      def_bfd = finfo->output_bfd;
8653
      if (h->root.u.def.section != bfd_abs_section_ptr)
8654
        def_bfd = h->root.u.def.section->owner;
8655
      (*_bfd_error_handler) (msg, finfo->output_bfd, def_bfd,
8656
                             h->root.root.string);
8657
      bfd_set_error (bfd_error_bad_value);
8658
      eoinfo->failed = TRUE;
8659
      return FALSE;
8660
    }
8661
 
8662
  /* We don't want to output symbols that have never been mentioned by
8663
     a regular file, or that we have been told to strip.  However, if
8664
     h->indx is set to -2, the symbol is used by a reloc and we must
8665
     output it.  */
8666
  if (h->indx == -2)
8667
    strip = FALSE;
8668
  else if ((h->def_dynamic
8669
            || h->ref_dynamic
8670
            || h->root.type == bfd_link_hash_new)
8671
           && !h->def_regular
8672
           && !h->ref_regular)
8673
    strip = TRUE;
8674
  else if (finfo->info->strip == strip_all)
8675
    strip = TRUE;
8676
  else if (finfo->info->strip == strip_some
8677
           && bfd_hash_lookup (finfo->info->keep_hash,
8678
                               h->root.root.string, FALSE, FALSE) == NULL)
8679
    strip = TRUE;
8680
  else if (finfo->info->strip_discarded
8681
           && (h->root.type == bfd_link_hash_defined
8682
               || h->root.type == bfd_link_hash_defweak)
8683
           && elf_discarded_section (h->root.u.def.section))
8684
    strip = TRUE;
8685
  else if ((h->root.type == bfd_link_hash_undefined
8686
            || h->root.type == bfd_link_hash_undefweak)
8687
           && h->root.u.undef.abfd != NULL
8688
           && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
8689
    strip = TRUE;
8690
  else
8691
    strip = FALSE;
8692
 
8693
  /* If we're stripping it, and it's not a dynamic symbol, there's
8694
     nothing else to do unless it is a forced local symbol or a
8695
     STT_GNU_IFUNC symbol.  */
8696
  if (strip
8697
      && h->dynindx == -1
8698
      && h->type != STT_GNU_IFUNC
8699
      && !h->forced_local)
8700
    return TRUE;
8701
 
8702
  sym.st_value = 0;
8703
  sym.st_size = h->size;
8704
  sym.st_other = h->other;
8705
  if (h->forced_local)
8706
    {
8707
      sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8708
      /* Turn off visibility on local symbol.  */
8709
      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
8710
    }
8711
  else if (h->unique_global)
8712
    sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
8713
  else if (h->root.type == bfd_link_hash_undefweak
8714
           || h->root.type == bfd_link_hash_defweak)
8715
    sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8716
  else
8717
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
8718
  sym.st_target_internal = h->target_internal;
8719
 
8720
  switch (h->root.type)
8721
    {
8722
    default:
8723
    case bfd_link_hash_new:
8724
    case bfd_link_hash_warning:
8725
      abort ();
8726
      return FALSE;
8727
 
8728
    case bfd_link_hash_undefined:
8729
    case bfd_link_hash_undefweak:
8730
      input_sec = bfd_und_section_ptr;
8731
      sym.st_shndx = SHN_UNDEF;
8732
      break;
8733
 
8734
    case bfd_link_hash_defined:
8735
    case bfd_link_hash_defweak:
8736
      {
8737
        input_sec = h->root.u.def.section;
8738
        if (input_sec->output_section != NULL)
8739
          {
8740
            sym.st_shndx =
8741
              _bfd_elf_section_from_bfd_section (finfo->output_bfd,
8742
                                                 input_sec->output_section);
8743
            if (sym.st_shndx == SHN_BAD)
8744
              {
8745
                (*_bfd_error_handler)
8746
                  (_("%B: could not find output section %A for input section %A"),
8747
                   finfo->output_bfd, input_sec->output_section, input_sec);
8748
                bfd_set_error (bfd_error_nonrepresentable_section);
8749
                eoinfo->failed = TRUE;
8750
                return FALSE;
8751
              }
8752
 
8753
            /* ELF symbols in relocatable files are section relative,
8754
               but in nonrelocatable files they are virtual
8755
               addresses.  */
8756
            sym.st_value = h->root.u.def.value + input_sec->output_offset;
8757
            if (! finfo->info->relocatable)
8758
              {
8759
                sym.st_value += input_sec->output_section->vma;
8760
                if (h->type == STT_TLS)
8761
                  {
8762
                    asection *tls_sec = elf_hash_table (finfo->info)->tls_sec;
8763
                    if (tls_sec != NULL)
8764
                      sym.st_value -= tls_sec->vma;
8765
                    else
8766
                      {
8767
                        /* The TLS section may have been garbage collected.  */
8768
                        BFD_ASSERT (finfo->info->gc_sections
8769
                                    && !input_sec->gc_mark);
8770
                      }
8771
                  }
8772
              }
8773
          }
8774
        else
8775
          {
8776
            BFD_ASSERT (input_sec->owner == NULL
8777
                        || (input_sec->owner->flags & DYNAMIC) != 0);
8778
            sym.st_shndx = SHN_UNDEF;
8779
            input_sec = bfd_und_section_ptr;
8780
          }
8781
      }
8782
      break;
8783
 
8784
    case bfd_link_hash_common:
8785
      input_sec = h->root.u.c.p->section;
8786
      sym.st_shndx = bed->common_section_index (input_sec);
8787
      sym.st_value = 1 << h->root.u.c.p->alignment_power;
8788
      break;
8789
 
8790
    case bfd_link_hash_indirect:
8791
      /* These symbols are created by symbol versioning.  They point
8792
         to the decorated version of the name.  For example, if the
8793
         symbol foo@@GNU_1.2 is the default, which should be used when
8794
         foo is used with no version, then we add an indirect symbol
8795
         foo which points to foo@@GNU_1.2.  We ignore these symbols,
8796
         since the indirected symbol is already in the hash table.  */
8797
      return TRUE;
8798
    }
8799
 
8800
  /* Give the processor backend a chance to tweak the symbol value,
8801
     and also to finish up anything that needs to be done for this
8802
     symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
8803
     forced local syms when non-shared is due to a historical quirk.
8804
     STT_GNU_IFUNC symbol must go through PLT.  */
8805
  if ((h->type == STT_GNU_IFUNC
8806
       && h->def_regular
8807
       && !finfo->info->relocatable)
8808
      || ((h->dynindx != -1
8809
           || h->forced_local)
8810
          && ((finfo->info->shared
8811
               && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8812
                   || h->root.type != bfd_link_hash_undefweak))
8813
              || !h->forced_local)
8814
          && elf_hash_table (finfo->info)->dynamic_sections_created))
8815
    {
8816
      if (! ((*bed->elf_backend_finish_dynamic_symbol)
8817
             (finfo->output_bfd, finfo->info, h, &sym)))
8818
        {
8819
          eoinfo->failed = TRUE;
8820
          return FALSE;
8821
        }
8822
    }
8823
 
8824
  /* If we are marking the symbol as undefined, and there are no
8825
     non-weak references to this symbol from a regular object, then
8826
     mark the symbol as weak undefined; if there are non-weak
8827
     references, mark the symbol as strong.  We can't do this earlier,
8828
     because it might not be marked as undefined until the
8829
     finish_dynamic_symbol routine gets through with it.  */
8830
  if (sym.st_shndx == SHN_UNDEF
8831
      && h->ref_regular
8832
      && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8833
          || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8834
    {
8835
      int bindtype;
8836
      unsigned int type = ELF_ST_TYPE (sym.st_info);
8837
 
8838
      /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
8839
      if (type == STT_GNU_IFUNC)
8840
        type = STT_FUNC;
8841
 
8842
      if (h->ref_regular_nonweak)
8843
        bindtype = STB_GLOBAL;
8844
      else
8845
        bindtype = STB_WEAK;
8846
      sym.st_info = ELF_ST_INFO (bindtype, type);
8847
    }
8848
 
8849
  /* If this is a symbol defined in a dynamic library, don't use the
8850
     symbol size from the dynamic library.  Relinking an executable
8851
     against a new library may introduce gratuitous changes in the
8852
     executable's symbols if we keep the size.  */
8853
  if (sym.st_shndx == SHN_UNDEF
8854
      && !h->def_regular
8855
      && h->def_dynamic)
8856
    sym.st_size = 0;
8857
 
8858
  /* If a non-weak symbol with non-default visibility is not defined
8859
     locally, it is a fatal error.  */
8860
  if (! finfo->info->relocatable
8861
      && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8862
      && ELF_ST_BIND (sym.st_info) != STB_WEAK
8863
      && h->root.type == bfd_link_hash_undefined
8864
      && !h->def_regular)
8865
    {
8866
      const char *msg;
8867
 
8868
      if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
8869
        msg = _("%B: protected symbol `%s' isn't defined");
8870
      else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
8871
        msg = _("%B: internal symbol `%s' isn't defined");
8872
      else
8873
        msg = _("%B: hidden symbol `%s' isn't defined");
8874
      (*_bfd_error_handler) (msg, finfo->output_bfd, h->root.root.string);
8875
      bfd_set_error (bfd_error_bad_value);
8876
      eoinfo->failed = TRUE;
8877
      return FALSE;
8878
    }
8879
 
8880
  /* If this symbol should be put in the .dynsym section, then put it
8881
     there now.  We already know the symbol index.  We also fill in
8882
     the entry in the .hash section.  */
8883
  if (h->dynindx != -1
8884
      && elf_hash_table (finfo->info)->dynamic_sections_created)
8885
    {
8886
      bfd_byte *esym;
8887
 
8888
      sym.st_name = h->dynstr_index;
8889
      esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
8890
      if (! check_dynsym (finfo->output_bfd, &sym))
8891
        {
8892
          eoinfo->failed = TRUE;
8893
          return FALSE;
8894
        }
8895
      bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
8896
 
8897
      if (finfo->hash_sec != NULL)
8898
        {
8899
          size_t hash_entry_size;
8900
          bfd_byte *bucketpos;
8901
          bfd_vma chain;
8902
          size_t bucketcount;
8903
          size_t bucket;
8904
 
8905
          bucketcount = elf_hash_table (finfo->info)->bucketcount;
8906
          bucket = h->u.elf_hash_value % bucketcount;
8907
 
8908
          hash_entry_size
8909
            = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
8910
          bucketpos = ((bfd_byte *) finfo->hash_sec->contents
8911
                       + (bucket + 2) * hash_entry_size);
8912
          chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
8913
          bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
8914
          bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
8915
                   ((bfd_byte *) finfo->hash_sec->contents
8916
                    + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8917
        }
8918
 
8919
      if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
8920
        {
8921
          Elf_Internal_Versym iversym;
8922
          Elf_External_Versym *eversym;
8923
 
8924
          if (!h->def_regular)
8925
            {
8926
              if (h->verinfo.verdef == NULL)
8927
                iversym.vs_vers = 0;
8928
              else
8929
                iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8930
            }
8931
          else
8932
            {
8933
              if (h->verinfo.vertree == NULL)
8934
                iversym.vs_vers = 1;
8935
              else
8936
                iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8937
              if (finfo->info->create_default_symver)
8938
                iversym.vs_vers++;
8939
            }
8940
 
8941
          if (h->hidden)
8942
            iversym.vs_vers |= VERSYM_HIDDEN;
8943
 
8944
          eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
8945
          eversym += h->dynindx;
8946
          _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
8947
        }
8948
    }
8949
 
8950
  /* If we're stripping it, then it was just a dynamic symbol, and
8951
     there's nothing else to do.  */
8952
  if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8953
    return TRUE;
8954
 
8955
  indx = bfd_get_symcount (finfo->output_bfd);
8956
  ret = elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h);
8957
  if (ret == 0)
8958
    {
8959
      eoinfo->failed = TRUE;
8960
      return FALSE;
8961
    }
8962
  else if (ret == 1)
8963
    h->indx = indx;
8964
  else if (h->indx == -2)
8965
    abort();
8966
 
8967
  return TRUE;
8968
}
8969
 
8970
/* Return TRUE if special handling is done for relocs in SEC against
8971
   symbols defined in discarded sections.  */
8972
 
8973
static bfd_boolean
8974
elf_section_ignore_discarded_relocs (asection *sec)
8975
{
8976
  const struct elf_backend_data *bed;
8977
 
8978
  switch (sec->sec_info_type)
8979
    {
8980
    case ELF_INFO_TYPE_STABS:
8981
    case ELF_INFO_TYPE_EH_FRAME:
8982
      return TRUE;
8983
    default:
8984
      break;
8985
    }
8986
 
8987
  bed = get_elf_backend_data (sec->owner);
8988
  if (bed->elf_backend_ignore_discarded_relocs != NULL
8989
      && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8990
    return TRUE;
8991
 
8992
  return FALSE;
8993
}
8994
 
8995
/* Return a mask saying how ld should treat relocations in SEC against
8996
   symbols defined in discarded sections.  If this function returns
8997
   COMPLAIN set, ld will issue a warning message.  If this function
8998
   returns PRETEND set, and the discarded section was link-once and the
8999
   same size as the kept link-once section, ld will pretend that the
9000
   symbol was actually defined in the kept section.  Otherwise ld will
9001
   zero the reloc (at least that is the intent, but some cooperation by
9002
   the target dependent code is needed, particularly for REL targets).  */
9003
 
9004
unsigned int
9005
_bfd_elf_default_action_discarded (asection *sec)
9006
{
9007
  if (sec->flags & SEC_DEBUGGING)
9008
    return PRETEND;
9009
 
9010
  if (strcmp (".eh_frame", sec->name) == 0)
9011
    return 0;
9012
 
9013
  if (strcmp (".gcc_except_table", sec->name) == 0)
9014
    return 0;
9015
 
9016
  return COMPLAIN | PRETEND;
9017
}
9018
 
9019
/* Find a match between a section and a member of a section group.  */
9020
 
9021
static asection *
9022
match_group_member (asection *sec, asection *group,
9023
                    struct bfd_link_info *info)
9024
{
9025
  asection *first = elf_next_in_group (group);
9026
  asection *s = first;
9027
 
9028
  while (s != NULL)
9029
    {
9030
      if (bfd_elf_match_symbols_in_sections (s, sec, info))
9031
        return s;
9032
 
9033
      s = elf_next_in_group (s);
9034
      if (s == first)
9035
        break;
9036
    }
9037
 
9038
  return NULL;
9039
}
9040
 
9041
/* Check if the kept section of a discarded section SEC can be used
9042
   to replace it.  Return the replacement if it is OK.  Otherwise return
9043
   NULL.  */
9044
 
9045
asection *
9046
_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9047
{
9048
  asection *kept;
9049
 
9050
  kept = sec->kept_section;
9051
  if (kept != NULL)
9052
    {
9053
      if ((kept->flags & SEC_GROUP) != 0)
9054
        kept = match_group_member (sec, kept, info);
9055
      if (kept != NULL
9056
          && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9057
              != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9058
        kept = NULL;
9059
      sec->kept_section = kept;
9060
    }
9061
  return kept;
9062
}
9063
 
9064
/* Link an input file into the linker output file.  This function
9065
   handles all the sections and relocations of the input file at once.
9066
   This is so that we only have to read the local symbols once, and
9067
   don't have to keep them in memory.  */
9068
 
9069
static bfd_boolean
9070
elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
9071
{
9072
  int (*relocate_section)
9073
    (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9074
     Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9075
  bfd *output_bfd;
9076
  Elf_Internal_Shdr *symtab_hdr;
9077
  size_t locsymcount;
9078
  size_t extsymoff;
9079
  Elf_Internal_Sym *isymbuf;
9080
  Elf_Internal_Sym *isym;
9081
  Elf_Internal_Sym *isymend;
9082
  long *pindex;
9083
  asection **ppsection;
9084
  asection *o;
9085
  const struct elf_backend_data *bed;
9086
  struct elf_link_hash_entry **sym_hashes;
9087
  bfd_size_type address_size;
9088
  bfd_vma r_type_mask;
9089
  int r_sym_shift;
9090
 
9091
  output_bfd = finfo->output_bfd;
9092
  bed = get_elf_backend_data (output_bfd);
9093
  relocate_section = bed->elf_backend_relocate_section;
9094
 
9095
  /* If this is a dynamic object, we don't want to do anything here:
9096
     we don't want the local symbols, and we don't want the section
9097
     contents.  */
9098
  if ((input_bfd->flags & DYNAMIC) != 0)
9099
    return TRUE;
9100
 
9101
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9102
  if (elf_bad_symtab (input_bfd))
9103
    {
9104
      locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9105
      extsymoff = 0;
9106
    }
9107
  else
9108
    {
9109
      locsymcount = symtab_hdr->sh_info;
9110
      extsymoff = symtab_hdr->sh_info;
9111
    }
9112
 
9113
  /* Read the local symbols.  */
9114
  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9115
  if (isymbuf == NULL && locsymcount != 0)
9116
    {
9117
      isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9118
                                      finfo->internal_syms,
9119
                                      finfo->external_syms,
9120
                                      finfo->locsym_shndx);
9121
      if (isymbuf == NULL)
9122
        return FALSE;
9123
    }
9124
 
9125
  /* Find local symbol sections and adjust values of symbols in
9126
     SEC_MERGE sections.  Write out those local symbols we know are
9127
     going into the output file.  */
9128
  isymend = isymbuf + locsymcount;
9129
  for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
9130
       isym < isymend;
9131
       isym++, pindex++, ppsection++)
9132
    {
9133
      asection *isec;
9134
      const char *name;
9135
      Elf_Internal_Sym osym;
9136
      long indx;
9137
      int ret;
9138
 
9139
      *pindex = -1;
9140
 
9141
      if (elf_bad_symtab (input_bfd))
9142
        {
9143
          if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9144
            {
9145
              *ppsection = NULL;
9146
              continue;
9147
            }
9148
        }
9149
 
9150
      if (isym->st_shndx == SHN_UNDEF)
9151
        isec = bfd_und_section_ptr;
9152
      else if (isym->st_shndx == SHN_ABS)
9153
        isec = bfd_abs_section_ptr;
9154
      else if (isym->st_shndx == SHN_COMMON)
9155
        isec = bfd_com_section_ptr;
9156
      else
9157
        {
9158
          isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9159
          if (isec == NULL)
9160
            {
9161
              /* Don't attempt to output symbols with st_shnx in the
9162
                 reserved range other than SHN_ABS and SHN_COMMON.  */
9163
              *ppsection = NULL;
9164
              continue;
9165
            }
9166
          else if (isec->sec_info_type == ELF_INFO_TYPE_MERGE
9167
                   && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9168
            isym->st_value =
9169
              _bfd_merged_section_offset (output_bfd, &isec,
9170
                                          elf_section_data (isec)->sec_info,
9171
                                          isym->st_value);
9172
        }
9173
 
9174
      *ppsection = isec;
9175
 
9176
      /* Don't output the first, undefined, symbol.  */
9177
      if (ppsection == finfo->sections)
9178
        continue;
9179
 
9180
      if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9181
        {
9182
          /* We never output section symbols.  Instead, we use the
9183
             section symbol of the corresponding section in the output
9184
             file.  */
9185
          continue;
9186
        }
9187
 
9188
      /* If we are stripping all symbols, we don't want to output this
9189
         one.  */
9190
      if (finfo->info->strip == strip_all)
9191
        continue;
9192
 
9193
      /* If we are discarding all local symbols, we don't want to
9194
         output this one.  If we are generating a relocatable output
9195
         file, then some of the local symbols may be required by
9196
         relocs; we output them below as we discover that they are
9197
         needed.  */
9198
      if (finfo->info->discard == discard_all)
9199
        continue;
9200
 
9201
      /* If this symbol is defined in a section which we are
9202
         discarding, we don't need to keep it.  */
9203
      if (isym->st_shndx != SHN_UNDEF
9204
          && isym->st_shndx < SHN_LORESERVE
9205
          && bfd_section_removed_from_list (output_bfd,
9206
                                            isec->output_section))
9207
        continue;
9208
 
9209
      /* Get the name of the symbol.  */
9210
      name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9211
                                              isym->st_name);
9212
      if (name == NULL)
9213
        return FALSE;
9214
 
9215
      /* See if we are discarding symbols with this name.  */
9216
      if ((finfo->info->strip == strip_some
9217
           && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
9218
               == NULL))
9219
          || (((finfo->info->discard == discard_sec_merge
9220
                && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
9221
               || finfo->info->discard == discard_l)
9222
              && bfd_is_local_label_name (input_bfd, name)))
9223
        continue;
9224
 
9225
      osym = *isym;
9226
 
9227
      /* Adjust the section index for the output file.  */
9228
      osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9229
                                                         isec->output_section);
9230
      if (osym.st_shndx == SHN_BAD)
9231
        return FALSE;
9232
 
9233
      /* ELF symbols in relocatable files are section relative, but
9234
         in executable files they are virtual addresses.  Note that
9235
         this code assumes that all ELF sections have an associated
9236
         BFD section with a reasonable value for output_offset; below
9237
         we assume that they also have a reasonable value for
9238
         output_section.  Any special sections must be set up to meet
9239
         these requirements.  */
9240
      osym.st_value += isec->output_offset;
9241
      if (! finfo->info->relocatable)
9242
        {
9243
          osym.st_value += isec->output_section->vma;
9244
          if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9245
            {
9246
              /* STT_TLS symbols are relative to PT_TLS segment base.  */
9247
              BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
9248
              osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
9249
            }
9250
        }
9251
 
9252
      indx = bfd_get_symcount (output_bfd);
9253
      ret = elf_link_output_sym (finfo, name, &osym, isec, NULL);
9254
      if (ret == 0)
9255
        return FALSE;
9256
      else if (ret == 1)
9257
        *pindex = indx;
9258
    }
9259
 
9260
  if (bed->s->arch_size == 32)
9261
    {
9262
      r_type_mask = 0xff;
9263
      r_sym_shift = 8;
9264
      address_size = 4;
9265
    }
9266
  else
9267
    {
9268
      r_type_mask = 0xffffffff;
9269
      r_sym_shift = 32;
9270
      address_size = 8;
9271
    }
9272
 
9273
  /* Relocate the contents of each section.  */
9274
  sym_hashes = elf_sym_hashes (input_bfd);
9275
  for (o = input_bfd->sections; o != NULL; o = o->next)
9276
    {
9277
      bfd_byte *contents;
9278
 
9279
      if (! o->linker_mark)
9280
        {
9281
          /* This section was omitted from the link.  */
9282
          continue;
9283
        }
9284
 
9285
      if (finfo->info->relocatable
9286
          && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9287
        {
9288
          /* Deal with the group signature symbol.  */
9289
          struct bfd_elf_section_data *sec_data = elf_section_data (o);
9290
          unsigned long symndx = sec_data->this_hdr.sh_info;
9291
          asection *osec = o->output_section;
9292
 
9293
          if (symndx >= locsymcount
9294
              || (elf_bad_symtab (input_bfd)
9295
                  && finfo->sections[symndx] == NULL))
9296
            {
9297
              struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9298
              while (h->root.type == bfd_link_hash_indirect
9299
                     || h->root.type == bfd_link_hash_warning)
9300
                h = (struct elf_link_hash_entry *) h->root.u.i.link;
9301
              /* Arrange for symbol to be output.  */
9302
              h->indx = -2;
9303
              elf_section_data (osec)->this_hdr.sh_info = -2;
9304
            }
9305
          else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9306
            {
9307
              /* We'll use the output section target_index.  */
9308
              asection *sec = finfo->sections[symndx]->output_section;
9309
              elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9310
            }
9311
          else
9312
            {
9313
              if (finfo->indices[symndx] == -1)
9314
                {
9315
                  /* Otherwise output the local symbol now.  */
9316
                  Elf_Internal_Sym sym = isymbuf[symndx];
9317
                  asection *sec = finfo->sections[symndx]->output_section;
9318
                  const char *name;
9319
                  long indx;
9320
                  int ret;
9321
 
9322
                  name = bfd_elf_string_from_elf_section (input_bfd,
9323
                                                          symtab_hdr->sh_link,
9324
                                                          sym.st_name);
9325
                  if (name == NULL)
9326
                    return FALSE;
9327
 
9328
                  sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9329
                                                                    sec);
9330
                  if (sym.st_shndx == SHN_BAD)
9331
                    return FALSE;
9332
 
9333
                  sym.st_value += o->output_offset;
9334
 
9335
                  indx = bfd_get_symcount (output_bfd);
9336
                  ret = elf_link_output_sym (finfo, name, &sym, o, NULL);
9337
                  if (ret == 0)
9338
                    return FALSE;
9339
                  else if (ret == 1)
9340
                    finfo->indices[symndx] = indx;
9341
                  else
9342
                    abort ();
9343
                }
9344
              elf_section_data (osec)->this_hdr.sh_info
9345
                = finfo->indices[symndx];
9346
            }
9347
        }
9348
 
9349
      if ((o->flags & SEC_HAS_CONTENTS) == 0
9350
          || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9351
        continue;
9352
 
9353
      if ((o->flags & SEC_LINKER_CREATED) != 0)
9354
        {
9355
          /* Section was created by _bfd_elf_link_create_dynamic_sections
9356
             or somesuch.  */
9357
          continue;
9358
        }
9359
 
9360
      /* Get the contents of the section.  They have been cached by a
9361
         relaxation routine.  Note that o is a section in an input
9362
         file, so the contents field will not have been set by any of
9363
         the routines which work on output files.  */
9364
      if (elf_section_data (o)->this_hdr.contents != NULL)
9365
        contents = elf_section_data (o)->this_hdr.contents;
9366
      else
9367
        {
9368
          contents = finfo->contents;
9369
          if (! bfd_get_full_section_contents (input_bfd, o, &contents))
9370
            return FALSE;
9371
        }
9372
 
9373
      if ((o->flags & SEC_RELOC) != 0)
9374
        {
9375
          Elf_Internal_Rela *internal_relocs;
9376
          Elf_Internal_Rela *rel, *relend;
9377
          int action_discarded;
9378
          int ret;
9379
 
9380
          /* Get the swapped relocs.  */
9381
          internal_relocs
9382
            = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
9383
                                         finfo->internal_relocs, FALSE);
9384
          if (internal_relocs == NULL
9385
              && o->reloc_count > 0)
9386
            return FALSE;
9387
 
9388
          /* We need to reverse-copy input .ctors/.dtors sections if
9389
             they are placed in .init_array/.finit_array for output.  */
9390
          if (o->size > address_size
9391
              && ((strncmp (o->name, ".ctors", 6) == 0
9392
                   && strcmp (o->output_section->name,
9393
                              ".init_array") == 0)
9394
                  || (strncmp (o->name, ".dtors", 6) == 0
9395
                      && strcmp (o->output_section->name,
9396
                                 ".fini_array") == 0))
9397
              && (o->name[6] == 0 || o->name[6] == '.'))
9398
            {
9399
              if (o->size != o->reloc_count * address_size)
9400
                {
9401
                  (*_bfd_error_handler)
9402
                    (_("error: %B: size of section %A is not "
9403
                       "multiple of address size"),
9404
                     input_bfd, o);
9405
                  bfd_set_error (bfd_error_on_input);
9406
                  return FALSE;
9407
                }
9408
              o->flags |= SEC_ELF_REVERSE_COPY;
9409
            }
9410
 
9411
          action_discarded = -1;
9412
          if (!elf_section_ignore_discarded_relocs (o))
9413
            action_discarded = (*bed->action_discarded) (o);
9414
 
9415
          /* Run through the relocs evaluating complex reloc symbols and
9416
             looking for relocs against symbols from discarded sections
9417
             or section symbols from removed link-once sections.
9418
             Complain about relocs against discarded sections.  Zero
9419
             relocs against removed link-once sections.  */
9420
 
9421
          rel = internal_relocs;
9422
          relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9423
          for ( ; rel < relend; rel++)
9424
            {
9425
              unsigned long r_symndx = rel->r_info >> r_sym_shift;
9426
              unsigned int s_type;
9427
              asection **ps, *sec;
9428
              struct elf_link_hash_entry *h = NULL;
9429
              const char *sym_name;
9430
 
9431
              if (r_symndx == STN_UNDEF)
9432
                continue;
9433
 
9434
              if (r_symndx >= locsymcount
9435
                  || (elf_bad_symtab (input_bfd)
9436
                      && finfo->sections[r_symndx] == NULL))
9437
                {
9438
                  h = sym_hashes[r_symndx - extsymoff];
9439
 
9440
                  /* Badly formatted input files can contain relocs that
9441
                     reference non-existant symbols.  Check here so that
9442
                     we do not seg fault.  */
9443
                  if (h == NULL)
9444
                    {
9445
                      char buffer [32];
9446
 
9447
                      sprintf_vma (buffer, rel->r_info);
9448
                      (*_bfd_error_handler)
9449
                        (_("error: %B contains a reloc (0x%s) for section %A "
9450
                           "that references a non-existent global symbol"),
9451
                         input_bfd, o, buffer);
9452
                      bfd_set_error (bfd_error_bad_value);
9453
                      return FALSE;
9454
                    }
9455
 
9456
                  while (h->root.type == bfd_link_hash_indirect
9457
                         || h->root.type == bfd_link_hash_warning)
9458
                    h = (struct elf_link_hash_entry *) h->root.u.i.link;
9459
 
9460
                  s_type = h->type;
9461
 
9462
                  ps = NULL;
9463
                  if (h->root.type == bfd_link_hash_defined
9464
                      || h->root.type == bfd_link_hash_defweak)
9465
                    ps = &h->root.u.def.section;
9466
 
9467
                  sym_name = h->root.root.string;
9468
                }
9469
              else
9470
                {
9471
                  Elf_Internal_Sym *sym = isymbuf + r_symndx;
9472
 
9473
                  s_type = ELF_ST_TYPE (sym->st_info);
9474
                  ps = &finfo->sections[r_symndx];
9475
                  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9476
                                               sym, *ps);
9477
                }
9478
 
9479
              if ((s_type == STT_RELC || s_type == STT_SRELC)
9480
                  && !finfo->info->relocatable)
9481
                {
9482
                  bfd_vma val;
9483
                  bfd_vma dot = (rel->r_offset
9484
                                 + o->output_offset + o->output_section->vma);
9485
#ifdef DEBUG
9486
                  printf ("Encountered a complex symbol!");
9487
                  printf (" (input_bfd %s, section %s, reloc %ld\n",
9488
                          input_bfd->filename, o->name,
9489
                          (long) (rel - internal_relocs));
9490
                  printf (" symbol: idx  %8.8lx, name %s\n",
9491
                          r_symndx, sym_name);
9492
                  printf (" reloc : info %8.8lx, addr %8.8lx\n",
9493
                          (unsigned long) rel->r_info,
9494
                          (unsigned long) rel->r_offset);
9495
#endif
9496
                  if (!eval_symbol (&val, &sym_name, input_bfd, finfo, dot,
9497
                                    isymbuf, locsymcount, s_type == STT_SRELC))
9498
                    return FALSE;
9499
 
9500
                  /* Symbol evaluated OK.  Update to absolute value.  */
9501
                  set_symbol_value (input_bfd, isymbuf, locsymcount,
9502
                                    r_symndx, val);
9503
                  continue;
9504
                }
9505
 
9506
              if (action_discarded != -1 && ps != NULL)
9507
                {
9508
                  /* Complain if the definition comes from a
9509
                     discarded section.  */
9510
                  if ((sec = *ps) != NULL && elf_discarded_section (sec))
9511
                    {
9512
                      BFD_ASSERT (r_symndx != STN_UNDEF);
9513
                      if (action_discarded & COMPLAIN)
9514
                        (*finfo->info->callbacks->einfo)
9515
                          (_("%X`%s' referenced in section `%A' of %B: "
9516
                             "defined in discarded section `%A' of %B\n"),
9517
                           sym_name, o, input_bfd, sec, sec->owner);
9518
 
9519
                      /* Try to do the best we can to support buggy old
9520
                         versions of gcc.  Pretend that the symbol is
9521
                         really defined in the kept linkonce section.
9522
                         FIXME: This is quite broken.  Modifying the
9523
                         symbol here means we will be changing all later
9524
                         uses of the symbol, not just in this section.  */
9525
                      if (action_discarded & PRETEND)
9526
                        {
9527
                          asection *kept;
9528
 
9529
                          kept = _bfd_elf_check_kept_section (sec,
9530
                                                              finfo->info);
9531
                          if (kept != NULL)
9532
                            {
9533
                              *ps = kept;
9534
                              continue;
9535
                            }
9536
                        }
9537
                    }
9538
                }
9539
            }
9540
 
9541
          /* Relocate the section by invoking a back end routine.
9542
 
9543
             The back end routine is responsible for adjusting the
9544
             section contents as necessary, and (if using Rela relocs
9545
             and generating a relocatable output file) adjusting the
9546
             reloc addend as necessary.
9547
 
9548
             The back end routine does not have to worry about setting
9549
             the reloc address or the reloc symbol index.
9550
 
9551
             The back end routine is given a pointer to the swapped in
9552
             internal symbols, and can access the hash table entries
9553
             for the external symbols via elf_sym_hashes (input_bfd).
9554
 
9555
             When generating relocatable output, the back end routine
9556
             must handle STB_LOCAL/STT_SECTION symbols specially.  The
9557
             output symbol is going to be a section symbol
9558
             corresponding to the output section, which will require
9559
             the addend to be adjusted.  */
9560
 
9561
          ret = (*relocate_section) (output_bfd, finfo->info,
9562
                                     input_bfd, o, contents,
9563
                                     internal_relocs,
9564
                                     isymbuf,
9565
                                     finfo->sections);
9566
          if (!ret)
9567
            return FALSE;
9568
 
9569
          if (ret == 2
9570
              || finfo->info->relocatable
9571
              || finfo->info->emitrelocations)
9572
            {
9573
              Elf_Internal_Rela *irela;
9574
              Elf_Internal_Rela *irelaend, *irelamid;
9575
              bfd_vma last_offset;
9576
              struct elf_link_hash_entry **rel_hash;
9577
              struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
9578
              Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
9579
              unsigned int next_erel;
9580
              bfd_boolean rela_normal;
9581
              struct bfd_elf_section_data *esdi, *esdo;
9582
 
9583
              esdi = elf_section_data (o);
9584
              esdo = elf_section_data (o->output_section);
9585
              rela_normal = FALSE;
9586
 
9587
              /* Adjust the reloc addresses and symbol indices.  */
9588
 
9589
              irela = internal_relocs;
9590
              irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
9591
              rel_hash = esdo->rel.hashes + esdo->rel.count;
9592
              /* We start processing the REL relocs, if any.  When we reach
9593
                 IRELAMID in the loop, we switch to the RELA relocs.  */
9594
              irelamid = irela;
9595
              if (esdi->rel.hdr != NULL)
9596
                irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
9597
                             * bed->s->int_rels_per_ext_rel);
9598
              rel_hash_list = rel_hash;
9599
              rela_hash_list = NULL;
9600
              last_offset = o->output_offset;
9601
              if (!finfo->info->relocatable)
9602
                last_offset += o->output_section->vma;
9603
              for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9604
                {
9605
                  unsigned long r_symndx;
9606
                  asection *sec;
9607
                  Elf_Internal_Sym sym;
9608
 
9609
                  if (next_erel == bed->s->int_rels_per_ext_rel)
9610
                    {
9611
                      rel_hash++;
9612
                      next_erel = 0;
9613
                    }
9614
 
9615
                  if (irela == irelamid)
9616
                    {
9617
                      rel_hash = esdo->rela.hashes + esdo->rela.count;
9618
                      rela_hash_list = rel_hash;
9619
                      rela_normal = bed->rela_normal;
9620
                    }
9621
 
9622
                  irela->r_offset = _bfd_elf_section_offset (output_bfd,
9623
                                                             finfo->info, o,
9624
                                                             irela->r_offset);
9625
                  if (irela->r_offset >= (bfd_vma) -2)
9626
                    {
9627
                      /* This is a reloc for a deleted entry or somesuch.
9628
                         Turn it into an R_*_NONE reloc, at the same
9629
                         offset as the last reloc.  elf_eh_frame.c and
9630
                         bfd_elf_discard_info rely on reloc offsets
9631
                         being ordered.  */
9632
                      irela->r_offset = last_offset;
9633
                      irela->r_info = 0;
9634
                      irela->r_addend = 0;
9635
                      continue;
9636
                    }
9637
 
9638
                  irela->r_offset += o->output_offset;
9639
 
9640
                  /* Relocs in an executable have to be virtual addresses.  */
9641
                  if (!finfo->info->relocatable)
9642
                    irela->r_offset += o->output_section->vma;
9643
 
9644
                  last_offset = irela->r_offset;
9645
 
9646
                  r_symndx = irela->r_info >> r_sym_shift;
9647
                  if (r_symndx == STN_UNDEF)
9648
                    continue;
9649
 
9650
                  if (r_symndx >= locsymcount
9651
                      || (elf_bad_symtab (input_bfd)
9652
                          && finfo->sections[r_symndx] == NULL))
9653
                    {
9654
                      struct elf_link_hash_entry *rh;
9655
                      unsigned long indx;
9656
 
9657
                      /* This is a reloc against a global symbol.  We
9658
                         have not yet output all the local symbols, so
9659
                         we do not know the symbol index of any global
9660
                         symbol.  We set the rel_hash entry for this
9661
                         reloc to point to the global hash table entry
9662
                         for this symbol.  The symbol index is then
9663
                         set at the end of bfd_elf_final_link.  */
9664
                      indx = r_symndx - extsymoff;
9665
                      rh = elf_sym_hashes (input_bfd)[indx];
9666
                      while (rh->root.type == bfd_link_hash_indirect
9667
                             || rh->root.type == bfd_link_hash_warning)
9668
                        rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9669
 
9670
                      /* Setting the index to -2 tells
9671
                         elf_link_output_extsym that this symbol is
9672
                         used by a reloc.  */
9673
                      BFD_ASSERT (rh->indx < 0);
9674
                      rh->indx = -2;
9675
 
9676
                      *rel_hash = rh;
9677
 
9678
                      continue;
9679
                    }
9680
 
9681
                  /* This is a reloc against a local symbol.  */
9682
 
9683
                  *rel_hash = NULL;
9684
                  sym = isymbuf[r_symndx];
9685
                  sec = finfo->sections[r_symndx];
9686
                  if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9687
                    {
9688
                      /* I suppose the backend ought to fill in the
9689
                         section of any STT_SECTION symbol against a
9690
                         processor specific section.  */
9691
                      r_symndx = STN_UNDEF;
9692
                      if (bfd_is_abs_section (sec))
9693
                        ;
9694
                      else if (sec == NULL || sec->owner == NULL)
9695
                        {
9696
                          bfd_set_error (bfd_error_bad_value);
9697
                          return FALSE;
9698
                        }
9699
                      else
9700
                        {
9701
                          asection *osec = sec->output_section;
9702
 
9703
                          /* If we have discarded a section, the output
9704
                             section will be the absolute section.  In
9705
                             case of discarded SEC_MERGE sections, use
9706
                             the kept section.  relocate_section should
9707
                             have already handled discarded linkonce
9708
                             sections.  */
9709
                          if (bfd_is_abs_section (osec)
9710
                              && sec->kept_section != NULL
9711
                              && sec->kept_section->output_section != NULL)
9712
                            {
9713
                              osec = sec->kept_section->output_section;
9714
                              irela->r_addend -= osec->vma;
9715
                            }
9716
 
9717
                          if (!bfd_is_abs_section (osec))
9718
                            {
9719
                              r_symndx = osec->target_index;
9720
                              if (r_symndx == STN_UNDEF)
9721
                                {
9722
                                  struct elf_link_hash_table *htab;
9723
                                  asection *oi;
9724
 
9725
                                  htab = elf_hash_table (finfo->info);
9726
                                  oi = htab->text_index_section;
9727
                                  if ((osec->flags & SEC_READONLY) == 0
9728
                                      && htab->data_index_section != NULL)
9729
                                    oi = htab->data_index_section;
9730
 
9731
                                  if (oi != NULL)
9732
                                    {
9733
                                      irela->r_addend += osec->vma - oi->vma;
9734
                                      r_symndx = oi->target_index;
9735
                                    }
9736
                                }
9737
 
9738
                              BFD_ASSERT (r_symndx != STN_UNDEF);
9739
                            }
9740
                        }
9741
 
9742
                      /* Adjust the addend according to where the
9743
                         section winds up in the output section.  */
9744
                      if (rela_normal)
9745
                        irela->r_addend += sec->output_offset;
9746
                    }
9747
                  else
9748
                    {
9749
                      if (finfo->indices[r_symndx] == -1)
9750
                        {
9751
                          unsigned long shlink;
9752
                          const char *name;
9753
                          asection *osec;
9754
                          long indx;
9755
 
9756
                          if (finfo->info->strip == strip_all)
9757
                            {
9758
                              /* You can't do ld -r -s.  */
9759
                              bfd_set_error (bfd_error_invalid_operation);
9760
                              return FALSE;
9761
                            }
9762
 
9763
                          /* This symbol was skipped earlier, but
9764
                             since it is needed by a reloc, we
9765
                             must output it now.  */
9766
                          shlink = symtab_hdr->sh_link;
9767
                          name = (bfd_elf_string_from_elf_section
9768
                                  (input_bfd, shlink, sym.st_name));
9769
                          if (name == NULL)
9770
                            return FALSE;
9771
 
9772
                          osec = sec->output_section;
9773
                          sym.st_shndx =
9774
                            _bfd_elf_section_from_bfd_section (output_bfd,
9775
                                                               osec);
9776
                          if (sym.st_shndx == SHN_BAD)
9777
                            return FALSE;
9778
 
9779
                          sym.st_value += sec->output_offset;
9780
                          if (! finfo->info->relocatable)
9781
                            {
9782
                              sym.st_value += osec->vma;
9783
                              if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9784
                                {
9785
                                  /* STT_TLS symbols are relative to PT_TLS
9786
                                     segment base.  */
9787
                                  BFD_ASSERT (elf_hash_table (finfo->info)
9788
                                              ->tls_sec != NULL);
9789
                                  sym.st_value -= (elf_hash_table (finfo->info)
9790
                                                   ->tls_sec->vma);
9791
                                }
9792
                            }
9793
 
9794
                          indx = bfd_get_symcount (output_bfd);
9795
                          ret = elf_link_output_sym (finfo, name, &sym, sec,
9796
                                                     NULL);
9797
                          if (ret == 0)
9798
                            return FALSE;
9799
                          else if (ret == 1)
9800
                            finfo->indices[r_symndx] = indx;
9801
                          else
9802
                            abort ();
9803
                        }
9804
 
9805
                      r_symndx = finfo->indices[r_symndx];
9806
                    }
9807
 
9808
                  irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9809
                                   | (irela->r_info & r_type_mask));
9810
                }
9811
 
9812
              /* Swap out the relocs.  */
9813
              input_rel_hdr = esdi->rel.hdr;
9814
              if (input_rel_hdr && input_rel_hdr->sh_size != 0)
9815
                {
9816
                  if (!bed->elf_backend_emit_relocs (output_bfd, o,
9817
                                                     input_rel_hdr,
9818
                                                     internal_relocs,
9819
                                                     rel_hash_list))
9820
                    return FALSE;
9821
                  internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
9822
                                      * bed->s->int_rels_per_ext_rel);
9823
                  rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
9824
                }
9825
 
9826
              input_rela_hdr = esdi->rela.hdr;
9827
              if (input_rela_hdr && input_rela_hdr->sh_size != 0)
9828
                {
9829
                  if (!bed->elf_backend_emit_relocs (output_bfd, o,
9830
                                                     input_rela_hdr,
9831
                                                     internal_relocs,
9832
                                                     rela_hash_list))
9833
                    return FALSE;
9834
                }
9835
            }
9836
        }
9837
 
9838
      /* Write out the modified section contents.  */
9839
      if (bed->elf_backend_write_section
9840
          && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
9841
                                                contents))
9842
        {
9843
          /* Section written out.  */
9844
        }
9845
      else switch (o->sec_info_type)
9846
        {
9847
        case ELF_INFO_TYPE_STABS:
9848
          if (! (_bfd_write_section_stabs
9849
                 (output_bfd,
9850
                  &elf_hash_table (finfo->info)->stab_info,
9851
                  o, &elf_section_data (o)->sec_info, contents)))
9852
            return FALSE;
9853
          break;
9854
        case ELF_INFO_TYPE_MERGE:
9855
          if (! _bfd_write_merged_section (output_bfd, o,
9856
                                           elf_section_data (o)->sec_info))
9857
            return FALSE;
9858
          break;
9859
        case ELF_INFO_TYPE_EH_FRAME:
9860
          {
9861
            if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
9862
                                                   o, contents))
9863
              return FALSE;
9864
          }
9865
          break;
9866
        default:
9867
          {
9868
            /* FIXME: octets_per_byte.  */
9869
            if (! (o->flags & SEC_EXCLUDE))
9870
              {
9871
                file_ptr offset = (file_ptr) o->output_offset;
9872
                bfd_size_type todo = o->size;
9873
                if ((o->flags & SEC_ELF_REVERSE_COPY))
9874
                  {
9875
                    /* Reverse-copy input section to output.  */
9876
                    do
9877
                      {
9878
                        todo -= address_size;
9879
                        if (! bfd_set_section_contents (output_bfd,
9880
                                                        o->output_section,
9881
                                                        contents + todo,
9882
                                                        offset,
9883
                                                        address_size))
9884
                          return FALSE;
9885
                        if (todo == 0)
9886
                          break;
9887
                        offset += address_size;
9888
                      }
9889
                    while (1);
9890
                  }
9891
                else if (! bfd_set_section_contents (output_bfd,
9892
                                                     o->output_section,
9893
                                                     contents,
9894
                                                     offset, todo))
9895
                  return FALSE;
9896
              }
9897
          }
9898
          break;
9899
        }
9900
    }
9901
 
9902
  return TRUE;
9903
}
9904
 
9905
/* Generate a reloc when linking an ELF file.  This is a reloc
9906
   requested by the linker, and does not come from any input file.  This
9907
   is used to build constructor and destructor tables when linking
9908
   with -Ur.  */
9909
 
9910
static bfd_boolean
9911
elf_reloc_link_order (bfd *output_bfd,
9912
                      struct bfd_link_info *info,
9913
                      asection *output_section,
9914
                      struct bfd_link_order *link_order)
9915
{
9916
  reloc_howto_type *howto;
9917
  long indx;
9918
  bfd_vma offset;
9919
  bfd_vma addend;
9920
  struct bfd_elf_section_reloc_data *reldata;
9921
  struct elf_link_hash_entry **rel_hash_ptr;
9922
  Elf_Internal_Shdr *rel_hdr;
9923
  const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9924
  Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
9925
  bfd_byte *erel;
9926
  unsigned int i;
9927
  struct bfd_elf_section_data *esdo = elf_section_data (output_section);
9928
 
9929
  howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
9930
  if (howto == NULL)
9931
    {
9932
      bfd_set_error (bfd_error_bad_value);
9933
      return FALSE;
9934
    }
9935
 
9936
  addend = link_order->u.reloc.p->addend;
9937
 
9938
  if (esdo->rel.hdr)
9939
    reldata = &esdo->rel;
9940
  else if (esdo->rela.hdr)
9941
    reldata = &esdo->rela;
9942
  else
9943
    {
9944
      reldata = NULL;
9945
      BFD_ASSERT (0);
9946
    }
9947
 
9948
  /* Figure out the symbol index.  */
9949
  rel_hash_ptr = reldata->hashes + reldata->count;
9950
  if (link_order->type == bfd_section_reloc_link_order)
9951
    {
9952
      indx = link_order->u.reloc.p->u.section->target_index;
9953
      BFD_ASSERT (indx != 0);
9954
      *rel_hash_ptr = NULL;
9955
    }
9956
  else
9957
    {
9958
      struct elf_link_hash_entry *h;
9959
 
9960
      /* Treat a reloc against a defined symbol as though it were
9961
         actually against the section.  */
9962
      h = ((struct elf_link_hash_entry *)
9963
           bfd_wrapped_link_hash_lookup (output_bfd, info,
9964
                                         link_order->u.reloc.p->u.name,
9965
                                         FALSE, FALSE, TRUE));
9966
      if (h != NULL
9967
          && (h->root.type == bfd_link_hash_defined
9968
              || h->root.type == bfd_link_hash_defweak))
9969
        {
9970
          asection *section;
9971
 
9972
          section = h->root.u.def.section;
9973
          indx = section->output_section->target_index;
9974
          *rel_hash_ptr = NULL;
9975
          /* It seems that we ought to add the symbol value to the
9976
             addend here, but in practice it has already been added
9977
             because it was passed to constructor_callback.  */
9978
          addend += section->output_section->vma + section->output_offset;
9979
        }
9980
      else if (h != NULL)
9981
        {
9982
          /* Setting the index to -2 tells elf_link_output_extsym that
9983
             this symbol is used by a reloc.  */
9984
          h->indx = -2;
9985
          *rel_hash_ptr = h;
9986
          indx = 0;
9987
        }
9988
      else
9989
        {
9990
          if (! ((*info->callbacks->unattached_reloc)
9991
                 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
9992
            return FALSE;
9993
          indx = 0;
9994
        }
9995
    }
9996
 
9997
  /* If this is an inplace reloc, we must write the addend into the
9998
     object file.  */
9999
  if (howto->partial_inplace && addend != 0)
10000
    {
10001
      bfd_size_type size;
10002
      bfd_reloc_status_type rstat;
10003
      bfd_byte *buf;
10004
      bfd_boolean ok;
10005
      const char *sym_name;
10006
 
10007
      size = (bfd_size_type) bfd_get_reloc_size (howto);
10008
      buf = (bfd_byte *) bfd_zmalloc (size);
10009
      if (buf == NULL)
10010
        return FALSE;
10011
      rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10012
      switch (rstat)
10013
        {
10014
        case bfd_reloc_ok:
10015
          break;
10016
 
10017
        default:
10018
        case bfd_reloc_outofrange:
10019
          abort ();
10020
 
10021
        case bfd_reloc_overflow:
10022
          if (link_order->type == bfd_section_reloc_link_order)
10023
            sym_name = bfd_section_name (output_bfd,
10024
                                         link_order->u.reloc.p->u.section);
10025
          else
10026
            sym_name = link_order->u.reloc.p->u.name;
10027
          if (! ((*info->callbacks->reloc_overflow)
10028
                 (info, NULL, sym_name, howto->name, addend, NULL,
10029
                  NULL, (bfd_vma) 0)))
10030
            {
10031
              free (buf);
10032
              return FALSE;
10033
            }
10034
          break;
10035
        }
10036
      ok = bfd_set_section_contents (output_bfd, output_section, buf,
10037
                                     link_order->offset, size);
10038
      free (buf);
10039
      if (! ok)
10040
        return FALSE;
10041
    }
10042
 
10043
  /* The address of a reloc is relative to the section in a
10044
     relocatable file, and is a virtual address in an executable
10045
     file.  */
10046
  offset = link_order->offset;
10047
  if (! info->relocatable)
10048
    offset += output_section->vma;
10049
 
10050
  for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10051
    {
10052
      irel[i].r_offset = offset;
10053
      irel[i].r_info = 0;
10054
      irel[i].r_addend = 0;
10055
    }
10056
  if (bed->s->arch_size == 32)
10057
    irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10058
  else
10059
    irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10060
 
10061
  rel_hdr = reldata->hdr;
10062
  erel = rel_hdr->contents;
10063
  if (rel_hdr->sh_type == SHT_REL)
10064
    {
10065
      erel += reldata->count * bed->s->sizeof_rel;
10066
      (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10067
    }
10068
  else
10069
    {
10070
      irel[0].r_addend = addend;
10071
      erel += reldata->count * bed->s->sizeof_rela;
10072
      (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10073
    }
10074
 
10075
  ++reldata->count;
10076
 
10077
  return TRUE;
10078
}
10079
 
10080
 
10081
/* Get the output vma of the section pointed to by the sh_link field.  */
10082
 
10083
static bfd_vma
10084
elf_get_linked_section_vma (struct bfd_link_order *p)
10085
{
10086
  Elf_Internal_Shdr **elf_shdrp;
10087
  asection *s;
10088
  int elfsec;
10089
 
10090
  s = p->u.indirect.section;
10091
  elf_shdrp = elf_elfsections (s->owner);
10092
  elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10093
  elfsec = elf_shdrp[elfsec]->sh_link;
10094
  /* PR 290:
10095
     The Intel C compiler generates SHT_IA_64_UNWIND with
10096
     SHF_LINK_ORDER.  But it doesn't set the sh_link or
10097
     sh_info fields.  Hence we could get the situation
10098
     where elfsec is 0.  */
10099
  if (elfsec == 0)
10100
    {
10101
      const struct elf_backend_data *bed
10102
        = get_elf_backend_data (s->owner);
10103
      if (bed->link_order_error_handler)
10104
        bed->link_order_error_handler
10105
          (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10106
      return 0;
10107
    }
10108
  else
10109
    {
10110
      s = elf_shdrp[elfsec]->bfd_section;
10111
      return s->output_section->vma + s->output_offset;
10112
    }
10113
}
10114
 
10115
 
10116
/* Compare two sections based on the locations of the sections they are
10117
   linked to.  Used by elf_fixup_link_order.  */
10118
 
10119
static int
10120
compare_link_order (const void * a, const void * b)
10121
{
10122
  bfd_vma apos;
10123
  bfd_vma bpos;
10124
 
10125
  apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10126
  bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10127
  if (apos < bpos)
10128
    return -1;
10129
  return apos > bpos;
10130
}
10131
 
10132
 
10133
/* Looks for sections with SHF_LINK_ORDER set.  Rearranges them into the same
10134
   order as their linked sections.  Returns false if this could not be done
10135
   because an output section includes both ordered and unordered
10136
   sections.  Ideally we'd do this in the linker proper.  */
10137
 
10138
static bfd_boolean
10139
elf_fixup_link_order (bfd *abfd, asection *o)
10140
{
10141
  int seen_linkorder;
10142
  int seen_other;
10143
  int n;
10144
  struct bfd_link_order *p;
10145
  bfd *sub;
10146
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10147
  unsigned elfsec;
10148
  struct bfd_link_order **sections;
10149
  asection *s, *other_sec, *linkorder_sec;
10150
  bfd_vma offset;
10151
 
10152
  other_sec = NULL;
10153
  linkorder_sec = NULL;
10154
  seen_other = 0;
10155
  seen_linkorder = 0;
10156
  for (p = o->map_head.link_order; p != NULL; p = p->next)
10157
    {
10158
      if (p->type == bfd_indirect_link_order)
10159
        {
10160
          s = p->u.indirect.section;
10161
          sub = s->owner;
10162
          if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10163
              && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10164
              && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10165
              && elfsec < elf_numsections (sub)
10166
              && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10167
              && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10168
            {
10169
              seen_linkorder++;
10170
              linkorder_sec = s;
10171
            }
10172
          else
10173
            {
10174
              seen_other++;
10175
              other_sec = s;
10176
            }
10177
        }
10178
      else
10179
        seen_other++;
10180
 
10181
      if (seen_other && seen_linkorder)
10182
        {
10183
          if (other_sec && linkorder_sec)
10184
            (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10185
                                   o, linkorder_sec,
10186
                                   linkorder_sec->owner, other_sec,
10187
                                   other_sec->owner);
10188
          else
10189
            (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10190
                                   o);
10191
          bfd_set_error (bfd_error_bad_value);
10192
          return FALSE;
10193
        }
10194
    }
10195
 
10196
  if (!seen_linkorder)
10197
    return TRUE;
10198
 
10199
  sections = (struct bfd_link_order **)
10200
    bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10201
  if (sections == NULL)
10202
    return FALSE;
10203
  seen_linkorder = 0;
10204
 
10205
  for (p = o->map_head.link_order; p != NULL; p = p->next)
10206
    {
10207
      sections[seen_linkorder++] = p;
10208
    }
10209
  /* Sort the input sections in the order of their linked section.  */
10210
  qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10211
         compare_link_order);
10212
 
10213
  /* Change the offsets of the sections.  */
10214
  offset = 0;
10215
  for (n = 0; n < seen_linkorder; n++)
10216
    {
10217
      s = sections[n]->u.indirect.section;
10218
      offset &= ~(bfd_vma) 0 << s->alignment_power;
10219
      s->output_offset = offset;
10220
      sections[n]->offset = offset;
10221
      /* FIXME: octets_per_byte.  */
10222
      offset += sections[n]->size;
10223
    }
10224
 
10225
  free (sections);
10226
  return TRUE;
10227
}
10228
 
10229
 
10230
/* Do the final step of an ELF link.  */
10231
 
10232
bfd_boolean
10233
bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10234
{
10235
  bfd_boolean dynamic;
10236
  bfd_boolean emit_relocs;
10237
  bfd *dynobj;
10238
  struct elf_final_link_info finfo;
10239
  asection *o;
10240
  struct bfd_link_order *p;
10241
  bfd *sub;
10242
  bfd_size_type max_contents_size;
10243
  bfd_size_type max_external_reloc_size;
10244
  bfd_size_type max_internal_reloc_count;
10245
  bfd_size_type max_sym_count;
10246
  bfd_size_type max_sym_shndx_count;
10247
  file_ptr off;
10248
  Elf_Internal_Sym elfsym;
10249
  unsigned int i;
10250
  Elf_Internal_Shdr *symtab_hdr;
10251
  Elf_Internal_Shdr *symtab_shndx_hdr;
10252
  Elf_Internal_Shdr *symstrtab_hdr;
10253
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10254
  struct elf_outext_info eoinfo;
10255
  bfd_boolean merged;
10256
  size_t relativecount = 0;
10257
  asection *reldyn = 0;
10258
  bfd_size_type amt;
10259
  asection *attr_section = NULL;
10260
  bfd_vma attr_size = 0;
10261
  const char *std_attrs_section;
10262
 
10263
  if (! is_elf_hash_table (info->hash))
10264
    return FALSE;
10265
 
10266
  if (info->shared)
10267
    abfd->flags |= DYNAMIC;
10268
 
10269
  dynamic = elf_hash_table (info)->dynamic_sections_created;
10270
  dynobj = elf_hash_table (info)->dynobj;
10271
 
10272
  emit_relocs = (info->relocatable
10273
                 || info->emitrelocations);
10274
 
10275
  finfo.info = info;
10276
  finfo.output_bfd = abfd;
10277
  finfo.symstrtab = _bfd_elf_stringtab_init ();
10278
  if (finfo.symstrtab == NULL)
10279
    return FALSE;
10280
 
10281
  if (! dynamic)
10282
    {
10283
      finfo.dynsym_sec = NULL;
10284
      finfo.hash_sec = NULL;
10285
      finfo.symver_sec = NULL;
10286
    }
10287
  else
10288
    {
10289
      finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
10290
      finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
10291
      BFD_ASSERT (finfo.dynsym_sec != NULL);
10292
      finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
10293
      /* Note that it is OK if symver_sec is NULL.  */
10294
    }
10295
 
10296
  finfo.contents = NULL;
10297
  finfo.external_relocs = NULL;
10298
  finfo.internal_relocs = NULL;
10299
  finfo.external_syms = NULL;
10300
  finfo.locsym_shndx = NULL;
10301
  finfo.internal_syms = NULL;
10302
  finfo.indices = NULL;
10303
  finfo.sections = NULL;
10304
  finfo.symbuf = NULL;
10305
  finfo.symshndxbuf = NULL;
10306
  finfo.symbuf_count = 0;
10307
  finfo.shndxbuf_size = 0;
10308
 
10309
  /* The object attributes have been merged.  Remove the input
10310
     sections from the link, and set the contents of the output
10311
     secton.  */
10312
  std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10313
  for (o = abfd->sections; o != NULL; o = o->next)
10314
    {
10315
      if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10316
          || strcmp (o->name, ".gnu.attributes") == 0)
10317
        {
10318
          for (p = o->map_head.link_order; p != NULL; p = p->next)
10319
            {
10320
              asection *input_section;
10321
 
10322
              if (p->type != bfd_indirect_link_order)
10323
                continue;
10324
              input_section = p->u.indirect.section;
10325
              /* Hack: reset the SEC_HAS_CONTENTS flag so that
10326
                 elf_link_input_bfd ignores this section.  */
10327
              input_section->flags &= ~SEC_HAS_CONTENTS;
10328
            }
10329
 
10330
          attr_size = bfd_elf_obj_attr_size (abfd);
10331
          if (attr_size)
10332
            {
10333
              bfd_set_section_size (abfd, o, attr_size);
10334
              attr_section = o;
10335
              /* Skip this section later on.  */
10336
              o->map_head.link_order = NULL;
10337
            }
10338
          else
10339
            o->flags |= SEC_EXCLUDE;
10340
        }
10341
    }
10342
 
10343
  /* Count up the number of relocations we will output for each output
10344
     section, so that we know the sizes of the reloc sections.  We
10345
     also figure out some maximum sizes.  */
10346
  max_contents_size = 0;
10347
  max_external_reloc_size = 0;
10348
  max_internal_reloc_count = 0;
10349
  max_sym_count = 0;
10350
  max_sym_shndx_count = 0;
10351
  merged = FALSE;
10352
  for (o = abfd->sections; o != NULL; o = o->next)
10353
    {
10354
      struct bfd_elf_section_data *esdo = elf_section_data (o);
10355
      o->reloc_count = 0;
10356
 
10357
      for (p = o->map_head.link_order; p != NULL; p = p->next)
10358
        {
10359
          unsigned int reloc_count = 0;
10360
          struct bfd_elf_section_data *esdi = NULL;
10361
 
10362
          if (p->type == bfd_section_reloc_link_order
10363
              || p->type == bfd_symbol_reloc_link_order)
10364
            reloc_count = 1;
10365
          else if (p->type == bfd_indirect_link_order)
10366
            {
10367
              asection *sec;
10368
 
10369
              sec = p->u.indirect.section;
10370
              esdi = elf_section_data (sec);
10371
 
10372
              /* Mark all sections which are to be included in the
10373
                 link.  This will normally be every section.  We need
10374
                 to do this so that we can identify any sections which
10375
                 the linker has decided to not include.  */
10376
              sec->linker_mark = TRUE;
10377
 
10378
              if (sec->flags & SEC_MERGE)
10379
                merged = TRUE;
10380
 
10381
              if (info->relocatable || info->emitrelocations)
10382
                reloc_count = sec->reloc_count;
10383
              else if (bed->elf_backend_count_relocs)
10384
                reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
10385
 
10386
              if (sec->rawsize > max_contents_size)
10387
                max_contents_size = sec->rawsize;
10388
              if (sec->size > max_contents_size)
10389
                max_contents_size = sec->size;
10390
 
10391
              /* We are interested in just local symbols, not all
10392
                 symbols.  */
10393
              if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10394
                  && (sec->owner->flags & DYNAMIC) == 0)
10395
                {
10396
                  size_t sym_count;
10397
 
10398
                  if (elf_bad_symtab (sec->owner))
10399
                    sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10400
                                 / bed->s->sizeof_sym);
10401
                  else
10402
                    sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10403
 
10404
                  if (sym_count > max_sym_count)
10405
                    max_sym_count = sym_count;
10406
 
10407
                  if (sym_count > max_sym_shndx_count
10408
                      && elf_symtab_shndx (sec->owner) != 0)
10409
                    max_sym_shndx_count = sym_count;
10410
 
10411
                  if ((sec->flags & SEC_RELOC) != 0)
10412
                    {
10413
                      size_t ext_size = 0;
10414
 
10415
                      if (esdi->rel.hdr != NULL)
10416
                        ext_size = esdi->rel.hdr->sh_size;
10417
                      if (esdi->rela.hdr != NULL)
10418
                        ext_size += esdi->rela.hdr->sh_size;
10419
 
10420
                      if (ext_size > max_external_reloc_size)
10421
                        max_external_reloc_size = ext_size;
10422
                      if (sec->reloc_count > max_internal_reloc_count)
10423
                        max_internal_reloc_count = sec->reloc_count;
10424
                    }
10425
                }
10426
            }
10427
 
10428
          if (reloc_count == 0)
10429
            continue;
10430
 
10431
          o->reloc_count += reloc_count;
10432
 
10433
          if (p->type == bfd_indirect_link_order
10434
              && (info->relocatable || info->emitrelocations))
10435
            {
10436
              if (esdi->rel.hdr)
10437
                esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
10438
              if (esdi->rela.hdr)
10439
                esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
10440
            }
10441
          else
10442
            {
10443
              if (o->use_rela_p)
10444
                esdo->rela.count += reloc_count;
10445
              else
10446
                esdo->rel.count += reloc_count;
10447
            }
10448
        }
10449
 
10450
      if (o->reloc_count > 0)
10451
        o->flags |= SEC_RELOC;
10452
      else
10453
        {
10454
          /* Explicitly clear the SEC_RELOC flag.  The linker tends to
10455
             set it (this is probably a bug) and if it is set
10456
             assign_section_numbers will create a reloc section.  */
10457
          o->flags &=~ SEC_RELOC;
10458
        }
10459
 
10460
      /* If the SEC_ALLOC flag is not set, force the section VMA to
10461
         zero.  This is done in elf_fake_sections as well, but forcing
10462
         the VMA to 0 here will ensure that relocs against these
10463
         sections are handled correctly.  */
10464
      if ((o->flags & SEC_ALLOC) == 0
10465
          && ! o->user_set_vma)
10466
        o->vma = 0;
10467
    }
10468
 
10469
  if (! info->relocatable && merged)
10470
    elf_link_hash_traverse (elf_hash_table (info),
10471
                            _bfd_elf_link_sec_merge_syms, abfd);
10472
 
10473
  /* Figure out the file positions for everything but the symbol table
10474
     and the relocs.  We set symcount to force assign_section_numbers
10475
     to create a symbol table.  */
10476
  bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10477
  BFD_ASSERT (! abfd->output_has_begun);
10478
  if (! _bfd_elf_compute_section_file_positions (abfd, info))
10479
    goto error_return;
10480
 
10481
  /* Set sizes, and assign file positions for reloc sections.  */
10482
  for (o = abfd->sections; o != NULL; o = o->next)
10483
    {
10484
      struct bfd_elf_section_data *esdo = elf_section_data (o);
10485
      if ((o->flags & SEC_RELOC) != 0)
10486
        {
10487
          if (esdo->rel.hdr
10488
              && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
10489
            goto error_return;
10490
 
10491
          if (esdo->rela.hdr
10492
              && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
10493
            goto error_return;
10494
        }
10495
 
10496
      /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10497
         to count upwards while actually outputting the relocations.  */
10498
      esdo->rel.count = 0;
10499
      esdo->rela.count = 0;
10500
    }
10501
 
10502
  _bfd_elf_assign_file_positions_for_relocs (abfd);
10503
 
10504
  /* We have now assigned file positions for all the sections except
10505
     .symtab and .strtab.  We start the .symtab section at the current
10506
     file position, and write directly to it.  We build the .strtab
10507
     section in memory.  */
10508
  bfd_get_symcount (abfd) = 0;
10509
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10510
  /* sh_name is set in prep_headers.  */
10511
  symtab_hdr->sh_type = SHT_SYMTAB;
10512
  /* sh_flags, sh_addr and sh_size all start off zero.  */
10513
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10514
  /* sh_link is set in assign_section_numbers.  */
10515
  /* sh_info is set below.  */
10516
  /* sh_offset is set just below.  */
10517
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
10518
 
10519
  off = elf_tdata (abfd)->next_file_pos;
10520
  off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10521
 
10522
  /* Note that at this point elf_tdata (abfd)->next_file_pos is
10523
     incorrect.  We do not yet know the size of the .symtab section.
10524
     We correct next_file_pos below, after we do know the size.  */
10525
 
10526
  /* Allocate a buffer to hold swapped out symbols.  This is to avoid
10527
     continuously seeking to the right position in the file.  */
10528
  if (! info->keep_memory || max_sym_count < 20)
10529
    finfo.symbuf_size = 20;
10530
  else
10531
    finfo.symbuf_size = max_sym_count;
10532
  amt = finfo.symbuf_size;
10533
  amt *= bed->s->sizeof_sym;
10534
  finfo.symbuf = (bfd_byte *) bfd_malloc (amt);
10535
  if (finfo.symbuf == NULL)
10536
    goto error_return;
10537
  if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
10538
    {
10539
      /* Wild guess at number of output symbols.  realloc'd as needed.  */
10540
      amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
10541
      finfo.shndxbuf_size = amt;
10542
      amt *= sizeof (Elf_External_Sym_Shndx);
10543
      finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10544
      if (finfo.symshndxbuf == NULL)
10545
        goto error_return;
10546
    }
10547
 
10548
  /* Start writing out the symbol table.  The first symbol is always a
10549
     dummy symbol.  */
10550
  if (info->strip != strip_all
10551
      || emit_relocs)
10552
    {
10553
      elfsym.st_value = 0;
10554
      elfsym.st_size = 0;
10555
      elfsym.st_info = 0;
10556
      elfsym.st_other = 0;
10557
      elfsym.st_shndx = SHN_UNDEF;
10558
      elfsym.st_target_internal = 0;
10559
      if (elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
10560
                               NULL) != 1)
10561
        goto error_return;
10562
    }
10563
 
10564
  /* Output a symbol for each section.  We output these even if we are
10565
     discarding local symbols, since they are used for relocs.  These
10566
     symbols have no names.  We store the index of each one in the
10567
     index field of the section, so that we can find it again when
10568
     outputting relocs.  */
10569
  if (info->strip != strip_all
10570
      || emit_relocs)
10571
    {
10572
      elfsym.st_size = 0;
10573
      elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10574
      elfsym.st_other = 0;
10575
      elfsym.st_value = 0;
10576
      elfsym.st_target_internal = 0;
10577
      for (i = 1; i < elf_numsections (abfd); i++)
10578
        {
10579
          o = bfd_section_from_elf_index (abfd, i);
10580
          if (o != NULL)
10581
            {
10582
              o->target_index = bfd_get_symcount (abfd);
10583
              elfsym.st_shndx = i;
10584
              if (!info->relocatable)
10585
                elfsym.st_value = o->vma;
10586
              if (elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL) != 1)
10587
                goto error_return;
10588
            }
10589
        }
10590
    }
10591
 
10592
  /* Allocate some memory to hold information read in from the input
10593
     files.  */
10594
  if (max_contents_size != 0)
10595
    {
10596
      finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
10597
      if (finfo.contents == NULL)
10598
        goto error_return;
10599
    }
10600
 
10601
  if (max_external_reloc_size != 0)
10602
    {
10603
      finfo.external_relocs = bfd_malloc (max_external_reloc_size);
10604
      if (finfo.external_relocs == NULL)
10605
        goto error_return;
10606
    }
10607
 
10608
  if (max_internal_reloc_count != 0)
10609
    {
10610
      amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10611
      amt *= sizeof (Elf_Internal_Rela);
10612
      finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
10613
      if (finfo.internal_relocs == NULL)
10614
        goto error_return;
10615
    }
10616
 
10617
  if (max_sym_count != 0)
10618
    {
10619
      amt = max_sym_count * bed->s->sizeof_sym;
10620
      finfo.external_syms = (bfd_byte *) bfd_malloc (amt);
10621
      if (finfo.external_syms == NULL)
10622
        goto error_return;
10623
 
10624
      amt = max_sym_count * sizeof (Elf_Internal_Sym);
10625
      finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
10626
      if (finfo.internal_syms == NULL)
10627
        goto error_return;
10628
 
10629
      amt = max_sym_count * sizeof (long);
10630
      finfo.indices = (long int *) bfd_malloc (amt);
10631
      if (finfo.indices == NULL)
10632
        goto error_return;
10633
 
10634
      amt = max_sym_count * sizeof (asection *);
10635
      finfo.sections = (asection **) bfd_malloc (amt);
10636
      if (finfo.sections == NULL)
10637
        goto error_return;
10638
    }
10639
 
10640
  if (max_sym_shndx_count != 0)
10641
    {
10642
      amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
10643
      finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
10644
      if (finfo.locsym_shndx == NULL)
10645
        goto error_return;
10646
    }
10647
 
10648
  if (elf_hash_table (info)->tls_sec)
10649
    {
10650
      bfd_vma base, end = 0;
10651
      asection *sec;
10652
 
10653
      for (sec = elf_hash_table (info)->tls_sec;
10654
           sec && (sec->flags & SEC_THREAD_LOCAL);
10655
           sec = sec->next)
10656
        {
10657
          bfd_size_type size = sec->size;
10658
 
10659
          if (size == 0
10660
              && (sec->flags & SEC_HAS_CONTENTS) == 0)
10661
            {
10662
              struct bfd_link_order *ord = sec->map_tail.link_order;
10663
 
10664
              if (ord != NULL)
10665
                size = ord->offset + ord->size;
10666
            }
10667
          end = sec->vma + size;
10668
        }
10669
      base = elf_hash_table (info)->tls_sec->vma;
10670
      /* Only align end of TLS section if static TLS doesn't have special
10671
         alignment requirements.  */
10672
      if (bed->static_tls_alignment == 1)
10673
        end = align_power (end,
10674
                           elf_hash_table (info)->tls_sec->alignment_power);
10675
      elf_hash_table (info)->tls_size = end - base;
10676
    }
10677
 
10678
  /* Reorder SHF_LINK_ORDER sections.  */
10679
  for (o = abfd->sections; o != NULL; o = o->next)
10680
    {
10681
      if (!elf_fixup_link_order (abfd, o))
10682
        return FALSE;
10683
    }
10684
 
10685
  /* Since ELF permits relocations to be against local symbols, we
10686
     must have the local symbols available when we do the relocations.
10687
     Since we would rather only read the local symbols once, and we
10688
     would rather not keep them in memory, we handle all the
10689
     relocations for a single input file at the same time.
10690
 
10691
     Unfortunately, there is no way to know the total number of local
10692
     symbols until we have seen all of them, and the local symbol
10693
     indices precede the global symbol indices.  This means that when
10694
     we are generating relocatable output, and we see a reloc against
10695
     a global symbol, we can not know the symbol index until we have
10696
     finished examining all the local symbols to see which ones we are
10697
     going to output.  To deal with this, we keep the relocations in
10698
     memory, and don't output them until the end of the link.  This is
10699
     an unfortunate waste of memory, but I don't see a good way around
10700
     it.  Fortunately, it only happens when performing a relocatable
10701
     link, which is not the common case.  FIXME: If keep_memory is set
10702
     we could write the relocs out and then read them again; I don't
10703
     know how bad the memory loss will be.  */
10704
 
10705
  for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10706
    sub->output_has_begun = FALSE;
10707
  for (o = abfd->sections; o != NULL; o = o->next)
10708
    {
10709
      for (p = o->map_head.link_order; p != NULL; p = p->next)
10710
        {
10711
          if (p->type == bfd_indirect_link_order
10712
              && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10713
                  == bfd_target_elf_flavour)
10714
              && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10715
            {
10716
              if (! sub->output_has_begun)
10717
                {
10718
                  if (! elf_link_input_bfd (&finfo, sub))
10719
                    goto error_return;
10720
                  sub->output_has_begun = TRUE;
10721
                }
10722
            }
10723
          else if (p->type == bfd_section_reloc_link_order
10724
                   || p->type == bfd_symbol_reloc_link_order)
10725
            {
10726
              if (! elf_reloc_link_order (abfd, info, o, p))
10727
                goto error_return;
10728
            }
10729
          else
10730
            {
10731
              if (! _bfd_default_link_order (abfd, info, o, p))
10732
                {
10733
                  if (p->type == bfd_indirect_link_order
10734
                      && (bfd_get_flavour (sub)
10735
                          == bfd_target_elf_flavour)
10736
                      && (elf_elfheader (sub)->e_ident[EI_CLASS]
10737
                          != bed->s->elfclass))
10738
                    {
10739
                      const char *iclass, *oclass;
10740
 
10741
                      if (bed->s->elfclass == ELFCLASS64)
10742
                        {
10743
                          iclass = "ELFCLASS32";
10744
                          oclass = "ELFCLASS64";
10745
                        }
10746
                      else
10747
                        {
10748
                          iclass = "ELFCLASS64";
10749
                          oclass = "ELFCLASS32";
10750
                        }
10751
 
10752
                      bfd_set_error (bfd_error_wrong_format);
10753
                      (*_bfd_error_handler)
10754
                        (_("%B: file class %s incompatible with %s"),
10755
                         sub, iclass, oclass);
10756
                    }
10757
 
10758
                  goto error_return;
10759
                }
10760
            }
10761
        }
10762
    }
10763
 
10764
  /* Free symbol buffer if needed.  */
10765
  if (!info->reduce_memory_overheads)
10766
    {
10767
      for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10768
        if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10769
            && elf_tdata (sub)->symbuf)
10770
          {
10771
            free (elf_tdata (sub)->symbuf);
10772
            elf_tdata (sub)->symbuf = NULL;
10773
          }
10774
    }
10775
 
10776
  /* Output any global symbols that got converted to local in a
10777
     version script or due to symbol visibility.  We do this in a
10778
     separate step since ELF requires all local symbols to appear
10779
     prior to any global symbols.  FIXME: We should only do this if
10780
     some global symbols were, in fact, converted to become local.
10781
     FIXME: Will this work correctly with the Irix 5 linker?  */
10782
  eoinfo.failed = FALSE;
10783
  eoinfo.finfo = &finfo;
10784
  eoinfo.localsyms = TRUE;
10785 148 khays
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
10786 14 khays
  if (eoinfo.failed)
10787
    return FALSE;
10788
 
10789
  /* If backend needs to output some local symbols not present in the hash
10790
     table, do it now.  */
10791
  if (bed->elf_backend_output_arch_local_syms)
10792
    {
10793
      typedef int (*out_sym_func)
10794
        (void *, const char *, Elf_Internal_Sym *, asection *,
10795
         struct elf_link_hash_entry *);
10796
 
10797
      if (! ((*bed->elf_backend_output_arch_local_syms)
10798
             (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10799
        return FALSE;
10800
    }
10801
 
10802
  /* That wrote out all the local symbols.  Finish up the symbol table
10803
     with the global symbols. Even if we want to strip everything we
10804
     can, we still need to deal with those global symbols that got
10805
     converted to local in a version script.  */
10806
 
10807
  /* The sh_info field records the index of the first non local symbol.  */
10808
  symtab_hdr->sh_info = bfd_get_symcount (abfd);
10809
 
10810
  if (dynamic
10811
      && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
10812
    {
10813
      Elf_Internal_Sym sym;
10814
      bfd_byte *dynsym = finfo.dynsym_sec->contents;
10815
      long last_local = 0;
10816
 
10817
      /* Write out the section symbols for the output sections.  */
10818
      if (info->shared || elf_hash_table (info)->is_relocatable_executable)
10819
        {
10820
          asection *s;
10821
 
10822
          sym.st_size = 0;
10823
          sym.st_name = 0;
10824
          sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10825
          sym.st_other = 0;
10826
          sym.st_target_internal = 0;
10827
 
10828
          for (s = abfd->sections; s != NULL; s = s->next)
10829
            {
10830
              int indx;
10831
              bfd_byte *dest;
10832
              long dynindx;
10833
 
10834
              dynindx = elf_section_data (s)->dynindx;
10835
              if (dynindx <= 0)
10836
                continue;
10837
              indx = elf_section_data (s)->this_idx;
10838
              BFD_ASSERT (indx > 0);
10839
              sym.st_shndx = indx;
10840
              if (! check_dynsym (abfd, &sym))
10841
                return FALSE;
10842
              sym.st_value = s->vma;
10843
              dest = dynsym + dynindx * bed->s->sizeof_sym;
10844
              if (last_local < dynindx)
10845
                last_local = dynindx;
10846
              bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10847
            }
10848
        }
10849
 
10850
      /* Write out the local dynsyms.  */
10851
      if (elf_hash_table (info)->dynlocal)
10852
        {
10853
          struct elf_link_local_dynamic_entry *e;
10854
          for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
10855
            {
10856
              asection *s;
10857
              bfd_byte *dest;
10858
 
10859
              /* Copy the internal symbol and turn off visibility.
10860
                 Note that we saved a word of storage and overwrote
10861
                 the original st_name with the dynstr_index.  */
10862
              sym = e->isym;
10863
              sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10864
 
10865
              s = bfd_section_from_elf_index (e->input_bfd,
10866
                                              e->isym.st_shndx);
10867
              if (s != NULL)
10868
                {
10869
                  sym.st_shndx =
10870
                    elf_section_data (s->output_section)->this_idx;
10871
                  if (! check_dynsym (abfd, &sym))
10872
                    return FALSE;
10873
                  sym.st_value = (s->output_section->vma
10874
                                  + s->output_offset
10875
                                  + e->isym.st_value);
10876
                }
10877
 
10878
              if (last_local < e->dynindx)
10879
                last_local = e->dynindx;
10880
 
10881
              dest = dynsym + e->dynindx * bed->s->sizeof_sym;
10882
              bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10883
            }
10884
        }
10885
 
10886
      elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
10887
        last_local + 1;
10888
    }
10889
 
10890
  /* We get the global symbols from the hash table.  */
10891
  eoinfo.failed = FALSE;
10892
  eoinfo.localsyms = FALSE;
10893
  eoinfo.finfo = &finfo;
10894 148 khays
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
10895 14 khays
  if (eoinfo.failed)
10896
    return FALSE;
10897
 
10898
  /* If backend needs to output some symbols not present in the hash
10899
     table, do it now.  */
10900
  if (bed->elf_backend_output_arch_syms)
10901
    {
10902
      typedef int (*out_sym_func)
10903
        (void *, const char *, Elf_Internal_Sym *, asection *,
10904
         struct elf_link_hash_entry *);
10905
 
10906
      if (! ((*bed->elf_backend_output_arch_syms)
10907
             (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10908
        return FALSE;
10909
    }
10910
 
10911
  /* Flush all symbols to the file.  */
10912
  if (! elf_link_flush_output_syms (&finfo, bed))
10913
    return FALSE;
10914
 
10915
  /* Now we know the size of the symtab section.  */
10916
  off += symtab_hdr->sh_size;
10917
 
10918
  symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
10919
  if (symtab_shndx_hdr->sh_name != 0)
10920
    {
10921
      symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
10922
      symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
10923
      symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
10924
      amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
10925
      symtab_shndx_hdr->sh_size = amt;
10926
 
10927
      off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
10928
                                                       off, TRUE);
10929
 
10930
      if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
10931
          || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
10932
        return FALSE;
10933
    }
10934
 
10935
 
10936
  /* Finish up and write out the symbol string table (.strtab)
10937
     section.  */
10938
  symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
10939
  /* sh_name was set in prep_headers.  */
10940
  symstrtab_hdr->sh_type = SHT_STRTAB;
10941
  symstrtab_hdr->sh_flags = 0;
10942
  symstrtab_hdr->sh_addr = 0;
10943
  symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
10944
  symstrtab_hdr->sh_entsize = 0;
10945
  symstrtab_hdr->sh_link = 0;
10946
  symstrtab_hdr->sh_info = 0;
10947
  /* sh_offset is set just below.  */
10948
  symstrtab_hdr->sh_addralign = 1;
10949
 
10950
  off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
10951
  elf_tdata (abfd)->next_file_pos = off;
10952
 
10953
  if (bfd_get_symcount (abfd) > 0)
10954
    {
10955
      if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
10956
          || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
10957
        return FALSE;
10958
    }
10959
 
10960
  /* Adjust the relocs to have the correct symbol indices.  */
10961
  for (o = abfd->sections; o != NULL; o = o->next)
10962
    {
10963
      struct bfd_elf_section_data *esdo = elf_section_data (o);
10964
      if ((o->flags & SEC_RELOC) == 0)
10965
        continue;
10966
 
10967
      if (esdo->rel.hdr != NULL)
10968
        elf_link_adjust_relocs (abfd, &esdo->rel);
10969
      if (esdo->rela.hdr != NULL)
10970
        elf_link_adjust_relocs (abfd, &esdo->rela);
10971
 
10972
      /* Set the reloc_count field to 0 to prevent write_relocs from
10973
         trying to swap the relocs out itself.  */
10974
      o->reloc_count = 0;
10975
    }
10976
 
10977
  if (dynamic && info->combreloc && dynobj != NULL)
10978
    relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
10979
 
10980
  /* If we are linking against a dynamic object, or generating a
10981
     shared library, finish up the dynamic linking information.  */
10982
  if (dynamic)
10983
    {
10984
      bfd_byte *dyncon, *dynconend;
10985
 
10986
      /* Fix up .dynamic entries.  */
10987
      o = bfd_get_section_by_name (dynobj, ".dynamic");
10988
      BFD_ASSERT (o != NULL);
10989
 
10990
      dyncon = o->contents;
10991
      dynconend = o->contents + o->size;
10992
      for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10993
        {
10994
          Elf_Internal_Dyn dyn;
10995
          const char *name;
10996
          unsigned int type;
10997
 
10998
          bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10999
 
11000
          switch (dyn.d_tag)
11001
            {
11002
            default:
11003
              continue;
11004
            case DT_NULL:
11005
              if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11006
                {
11007
                  switch (elf_section_data (reldyn)->this_hdr.sh_type)
11008
                    {
11009
                    case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11010
                    case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11011
                    default: continue;
11012
                    }
11013
                  dyn.d_un.d_val = relativecount;
11014
                  relativecount = 0;
11015
                  break;
11016
                }
11017
              continue;
11018
 
11019
            case DT_INIT:
11020
              name = info->init_function;
11021
              goto get_sym;
11022
            case DT_FINI:
11023
              name = info->fini_function;
11024
            get_sym:
11025
              {
11026
                struct elf_link_hash_entry *h;
11027
 
11028
                h = elf_link_hash_lookup (elf_hash_table (info), name,
11029
                                          FALSE, FALSE, TRUE);
11030
                if (h != NULL
11031
                    && (h->root.type == bfd_link_hash_defined
11032
                        || h->root.type == bfd_link_hash_defweak))
11033
                  {
11034
                    dyn.d_un.d_ptr = h->root.u.def.value;
11035
                    o = h->root.u.def.section;
11036
                    if (o->output_section != NULL)
11037
                      dyn.d_un.d_ptr += (o->output_section->vma
11038
                                         + o->output_offset);
11039
                    else
11040
                      {
11041
                        /* The symbol is imported from another shared
11042
                           library and does not apply to this one.  */
11043
                        dyn.d_un.d_ptr = 0;
11044
                      }
11045
                    break;
11046
                  }
11047
              }
11048
              continue;
11049
 
11050
            case DT_PREINIT_ARRAYSZ:
11051
              name = ".preinit_array";
11052
              goto get_size;
11053
            case DT_INIT_ARRAYSZ:
11054
              name = ".init_array";
11055
              goto get_size;
11056
            case DT_FINI_ARRAYSZ:
11057
              name = ".fini_array";
11058
            get_size:
11059
              o = bfd_get_section_by_name (abfd, name);
11060
              if (o == NULL)
11061
                {
11062
                  (*_bfd_error_handler)
11063
                    (_("%B: could not find output section %s"), abfd, name);
11064
                  goto error_return;
11065
                }
11066
              if (o->size == 0)
11067
                (*_bfd_error_handler)
11068
                  (_("warning: %s section has zero size"), name);
11069
              dyn.d_un.d_val = o->size;
11070
              break;
11071
 
11072
            case DT_PREINIT_ARRAY:
11073
              name = ".preinit_array";
11074
              goto get_vma;
11075
            case DT_INIT_ARRAY:
11076
              name = ".init_array";
11077
              goto get_vma;
11078
            case DT_FINI_ARRAY:
11079
              name = ".fini_array";
11080
              goto get_vma;
11081
 
11082
            case DT_HASH:
11083
              name = ".hash";
11084
              goto get_vma;
11085
            case DT_GNU_HASH:
11086
              name = ".gnu.hash";
11087
              goto get_vma;
11088
            case DT_STRTAB:
11089
              name = ".dynstr";
11090
              goto get_vma;
11091
            case DT_SYMTAB:
11092
              name = ".dynsym";
11093
              goto get_vma;
11094
            case DT_VERDEF:
11095
              name = ".gnu.version_d";
11096
              goto get_vma;
11097
            case DT_VERNEED:
11098
              name = ".gnu.version_r";
11099
              goto get_vma;
11100
            case DT_VERSYM:
11101
              name = ".gnu.version";
11102
            get_vma:
11103
              o = bfd_get_section_by_name (abfd, name);
11104
              if (o == NULL)
11105
                {
11106
                  (*_bfd_error_handler)
11107
                    (_("%B: could not find output section %s"), abfd, name);
11108
                  goto error_return;
11109
                }
11110 148 khays
              if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11111
                {
11112
                  (*_bfd_error_handler)
11113
                    (_("warning: section '%s' is being made into a note"), name);
11114
                  bfd_set_error (bfd_error_nonrepresentable_section);
11115
                  goto error_return;
11116
                }
11117 14 khays
              dyn.d_un.d_ptr = o->vma;
11118
              break;
11119
 
11120
            case DT_REL:
11121
            case DT_RELA:
11122
            case DT_RELSZ:
11123
            case DT_RELASZ:
11124
              if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11125
                type = SHT_REL;
11126
              else
11127
                type = SHT_RELA;
11128
              dyn.d_un.d_val = 0;
11129
              dyn.d_un.d_ptr = 0;
11130
              for (i = 1; i < elf_numsections (abfd); i++)
11131
                {
11132
                  Elf_Internal_Shdr *hdr;
11133
 
11134
                  hdr = elf_elfsections (abfd)[i];
11135
                  if (hdr->sh_type == type
11136
                      && (hdr->sh_flags & SHF_ALLOC) != 0)
11137
                    {
11138
                      if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11139
                        dyn.d_un.d_val += hdr->sh_size;
11140
                      else
11141
                        {
11142
                          if (dyn.d_un.d_ptr == 0
11143
                              || hdr->sh_addr < dyn.d_un.d_ptr)
11144
                            dyn.d_un.d_ptr = hdr->sh_addr;
11145
                        }
11146
                    }
11147
                }
11148
              break;
11149
            }
11150
          bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11151
        }
11152
    }
11153
 
11154
  /* If we have created any dynamic sections, then output them.  */
11155
  if (dynobj != NULL)
11156
    {
11157
      if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11158
        goto error_return;
11159
 
11160
      /* Check for DT_TEXTREL (late, in case the backend removes it).  */
11161
      if (info->warn_shared_textrel && info->shared)
11162
        {
11163
          bfd_byte *dyncon, *dynconend;
11164
 
11165
          /* Fix up .dynamic entries.  */
11166
          o = bfd_get_section_by_name (dynobj, ".dynamic");
11167
          BFD_ASSERT (o != NULL);
11168
 
11169
          dyncon = o->contents;
11170
          dynconend = o->contents + o->size;
11171
          for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11172
            {
11173
              Elf_Internal_Dyn dyn;
11174
 
11175
              bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11176
 
11177
              if (dyn.d_tag == DT_TEXTREL)
11178
                {
11179
                 info->callbacks->einfo
11180
                    (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
11181
                  break;
11182
                }
11183
            }
11184
        }
11185
 
11186
      for (o = dynobj->sections; o != NULL; o = o->next)
11187
        {
11188
          if ((o->flags & SEC_HAS_CONTENTS) == 0
11189
              || o->size == 0
11190
              || o->output_section == bfd_abs_section_ptr)
11191
            continue;
11192
          if ((o->flags & SEC_LINKER_CREATED) == 0)
11193
            {
11194
              /* At this point, we are only interested in sections
11195
                 created by _bfd_elf_link_create_dynamic_sections.  */
11196
              continue;
11197
            }
11198
          if (elf_hash_table (info)->stab_info.stabstr == o)
11199
            continue;
11200
          if (elf_hash_table (info)->eh_info.hdr_sec == o)
11201
            continue;
11202
          if ((elf_section_data (o->output_section)->this_hdr.sh_type
11203
               != SHT_STRTAB)
11204 148 khays
              && (strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0))
11205 14 khays
            {
11206
              /* FIXME: octets_per_byte.  */
11207
              if (! bfd_set_section_contents (abfd, o->output_section,
11208
                                              o->contents,
11209
                                              (file_ptr) o->output_offset,
11210
                                              o->size))
11211
                goto error_return;
11212
            }
11213
          else
11214
            {
11215
              /* The contents of the .dynstr section are actually in a
11216
                 stringtab.  */
11217
              off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11218
              if (bfd_seek (abfd, off, SEEK_SET) != 0
11219
                  || ! _bfd_elf_strtab_emit (abfd,
11220
                                             elf_hash_table (info)->dynstr))
11221
                goto error_return;
11222
            }
11223
        }
11224
    }
11225
 
11226
  if (info->relocatable)
11227
    {
11228
      bfd_boolean failed = FALSE;
11229
 
11230
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11231
      if (failed)
11232
        goto error_return;
11233
    }
11234
 
11235
  /* If we have optimized stabs strings, output them.  */
11236
  if (elf_hash_table (info)->stab_info.stabstr != NULL)
11237
    {
11238
      if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11239
        goto error_return;
11240
    }
11241
 
11242
  if (info->eh_frame_hdr)
11243
    {
11244
      if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11245
        goto error_return;
11246
    }
11247
 
11248
  if (finfo.symstrtab != NULL)
11249
    _bfd_stringtab_free (finfo.symstrtab);
11250
  if (finfo.contents != NULL)
11251
    free (finfo.contents);
11252
  if (finfo.external_relocs != NULL)
11253
    free (finfo.external_relocs);
11254
  if (finfo.internal_relocs != NULL)
11255
    free (finfo.internal_relocs);
11256
  if (finfo.external_syms != NULL)
11257
    free (finfo.external_syms);
11258
  if (finfo.locsym_shndx != NULL)
11259
    free (finfo.locsym_shndx);
11260
  if (finfo.internal_syms != NULL)
11261
    free (finfo.internal_syms);
11262
  if (finfo.indices != NULL)
11263
    free (finfo.indices);
11264
  if (finfo.sections != NULL)
11265
    free (finfo.sections);
11266
  if (finfo.symbuf != NULL)
11267
    free (finfo.symbuf);
11268
  if (finfo.symshndxbuf != NULL)
11269
    free (finfo.symshndxbuf);
11270
  for (o = abfd->sections; o != NULL; o = o->next)
11271
    {
11272
      struct bfd_elf_section_data *esdo = elf_section_data (o);
11273
      if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11274
        free (esdo->rel.hashes);
11275
      if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11276
        free (esdo->rela.hashes);
11277
    }
11278
 
11279
  elf_tdata (abfd)->linker = TRUE;
11280
 
11281
  if (attr_section)
11282
    {
11283
      bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
11284
      if (contents == NULL)
11285
        return FALSE;   /* Bail out and fail.  */
11286
      bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11287
      bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11288
      free (contents);
11289
    }
11290
 
11291
  return TRUE;
11292
 
11293
 error_return:
11294
  if (finfo.symstrtab != NULL)
11295
    _bfd_stringtab_free (finfo.symstrtab);
11296
  if (finfo.contents != NULL)
11297
    free (finfo.contents);
11298
  if (finfo.external_relocs != NULL)
11299
    free (finfo.external_relocs);
11300
  if (finfo.internal_relocs != NULL)
11301
    free (finfo.internal_relocs);
11302
  if (finfo.external_syms != NULL)
11303
    free (finfo.external_syms);
11304
  if (finfo.locsym_shndx != NULL)
11305
    free (finfo.locsym_shndx);
11306
  if (finfo.internal_syms != NULL)
11307
    free (finfo.internal_syms);
11308
  if (finfo.indices != NULL)
11309
    free (finfo.indices);
11310
  if (finfo.sections != NULL)
11311
    free (finfo.sections);
11312
  if (finfo.symbuf != NULL)
11313
    free (finfo.symbuf);
11314
  if (finfo.symshndxbuf != NULL)
11315
    free (finfo.symshndxbuf);
11316
  for (o = abfd->sections; o != NULL; o = o->next)
11317
    {
11318
      struct bfd_elf_section_data *esdo = elf_section_data (o);
11319
      if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11320
        free (esdo->rel.hashes);
11321
      if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11322
        free (esdo->rela.hashes);
11323
    }
11324
 
11325
  return FALSE;
11326
}
11327
 
11328
/* Initialize COOKIE for input bfd ABFD.  */
11329
 
11330
static bfd_boolean
11331
init_reloc_cookie (struct elf_reloc_cookie *cookie,
11332
                   struct bfd_link_info *info, bfd *abfd)
11333
{
11334
  Elf_Internal_Shdr *symtab_hdr;
11335
  const struct elf_backend_data *bed;
11336
 
11337
  bed = get_elf_backend_data (abfd);
11338
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11339
 
11340
  cookie->abfd = abfd;
11341
  cookie->sym_hashes = elf_sym_hashes (abfd);
11342
  cookie->bad_symtab = elf_bad_symtab (abfd);
11343
  if (cookie->bad_symtab)
11344
    {
11345
      cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11346
      cookie->extsymoff = 0;
11347
    }
11348
  else
11349
    {
11350
      cookie->locsymcount = symtab_hdr->sh_info;
11351
      cookie->extsymoff = symtab_hdr->sh_info;
11352
    }
11353
 
11354
  if (bed->s->arch_size == 32)
11355
    cookie->r_sym_shift = 8;
11356
  else
11357
    cookie->r_sym_shift = 32;
11358
 
11359
  cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11360
  if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11361
    {
11362
      cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11363
                                              cookie->locsymcount, 0,
11364
                                              NULL, NULL, NULL);
11365
      if (cookie->locsyms == NULL)
11366
        {
11367
          info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11368
          return FALSE;
11369
        }
11370
      if (info->keep_memory)
11371
        symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11372
    }
11373
  return TRUE;
11374
}
11375
 
11376
/* Free the memory allocated by init_reloc_cookie, if appropriate.  */
11377
 
11378
static void
11379
fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11380
{
11381
  Elf_Internal_Shdr *symtab_hdr;
11382
 
11383
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11384
  if (cookie->locsyms != NULL
11385
      && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11386
    free (cookie->locsyms);
11387
}
11388
 
11389
/* Initialize the relocation information in COOKIE for input section SEC
11390
   of input bfd ABFD.  */
11391
 
11392
static bfd_boolean
11393
init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11394
                        struct bfd_link_info *info, bfd *abfd,
11395
                        asection *sec)
11396
{
11397
  const struct elf_backend_data *bed;
11398
 
11399
  if (sec->reloc_count == 0)
11400
    {
11401
      cookie->rels = NULL;
11402
      cookie->relend = NULL;
11403
    }
11404
  else
11405
    {
11406
      bed = get_elf_backend_data (abfd);
11407
 
11408
      cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11409
                                                info->keep_memory);
11410
      if (cookie->rels == NULL)
11411
        return FALSE;
11412
      cookie->rel = cookie->rels;
11413
      cookie->relend = (cookie->rels
11414
                        + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11415
    }
11416
  cookie->rel = cookie->rels;
11417
  return TRUE;
11418
}
11419
 
11420
/* Free the memory allocated by init_reloc_cookie_rels,
11421
   if appropriate.  */
11422
 
11423
static void
11424
fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11425
                        asection *sec)
11426
{
11427
  if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11428
    free (cookie->rels);
11429
}
11430
 
11431
/* Initialize the whole of COOKIE for input section SEC.  */
11432
 
11433
static bfd_boolean
11434
init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11435
                               struct bfd_link_info *info,
11436
                               asection *sec)
11437
{
11438
  if (!init_reloc_cookie (cookie, info, sec->owner))
11439
    goto error1;
11440
  if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11441
    goto error2;
11442
  return TRUE;
11443
 
11444
 error2:
11445
  fini_reloc_cookie (cookie, sec->owner);
11446
 error1:
11447
  return FALSE;
11448
}
11449
 
11450
/* Free the memory allocated by init_reloc_cookie_for_section,
11451
   if appropriate.  */
11452
 
11453
static void
11454
fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11455
                               asection *sec)
11456
{
11457
  fini_reloc_cookie_rels (cookie, sec);
11458
  fini_reloc_cookie (cookie, sec->owner);
11459
}
11460
 
11461
/* Garbage collect unused sections.  */
11462
 
11463
/* Default gc_mark_hook.  */
11464
 
11465
asection *
11466
_bfd_elf_gc_mark_hook (asection *sec,
11467
                       struct bfd_link_info *info ATTRIBUTE_UNUSED,
11468
                       Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11469
                       struct elf_link_hash_entry *h,
11470
                       Elf_Internal_Sym *sym)
11471
{
11472
  const char *sec_name;
11473
 
11474
  if (h != NULL)
11475
    {
11476
      switch (h->root.type)
11477
        {
11478
        case bfd_link_hash_defined:
11479
        case bfd_link_hash_defweak:
11480
          return h->root.u.def.section;
11481
 
11482
        case bfd_link_hash_common:
11483
          return h->root.u.c.p->section;
11484
 
11485
        case bfd_link_hash_undefined:
11486
        case bfd_link_hash_undefweak:
11487
          /* To work around a glibc bug, keep all XXX input sections
11488
             when there is an as yet undefined reference to __start_XXX
11489
             or __stop_XXX symbols.  The linker will later define such
11490
             symbols for orphan input sections that have a name
11491
             representable as a C identifier.  */
11492
          if (strncmp (h->root.root.string, "__start_", 8) == 0)
11493
            sec_name = h->root.root.string + 8;
11494
          else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
11495
            sec_name = h->root.root.string + 7;
11496
          else
11497
            sec_name = NULL;
11498
 
11499
          if (sec_name && *sec_name != '\0')
11500
            {
11501
              bfd *i;
11502
 
11503
              for (i = info->input_bfds; i; i = i->link_next)
11504
                {
11505
                  sec = bfd_get_section_by_name (i, sec_name);
11506
                  if (sec)
11507
                    sec->flags |= SEC_KEEP;
11508
                }
11509
            }
11510
          break;
11511
 
11512
        default:
11513
          break;
11514
        }
11515
    }
11516
  else
11517
    return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11518
 
11519
  return NULL;
11520
}
11521
 
11522
/* COOKIE->rel describes a relocation against section SEC, which is
11523
   a section we've decided to keep.  Return the section that contains
11524
   the relocation symbol, or NULL if no section contains it.  */
11525
 
11526
asection *
11527
_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11528
                       elf_gc_mark_hook_fn gc_mark_hook,
11529
                       struct elf_reloc_cookie *cookie)
11530
{
11531
  unsigned long r_symndx;
11532
  struct elf_link_hash_entry *h;
11533
 
11534
  r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
11535
  if (r_symndx == STN_UNDEF)
11536
    return NULL;
11537
 
11538
  if (r_symndx >= cookie->locsymcount
11539
      || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11540
    {
11541
      h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11542
      while (h->root.type == bfd_link_hash_indirect
11543
             || h->root.type == bfd_link_hash_warning)
11544
        h = (struct elf_link_hash_entry *) h->root.u.i.link;
11545
      return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
11546
    }
11547
 
11548
  return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
11549
                          &cookie->locsyms[r_symndx]);
11550
}
11551
 
11552
/* COOKIE->rel describes a relocation against section SEC, which is
11553
   a section we've decided to keep.  Mark the section that contains
11554
   the relocation symbol.  */
11555
 
11556
bfd_boolean
11557
_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
11558
                        asection *sec,
11559
                        elf_gc_mark_hook_fn gc_mark_hook,
11560
                        struct elf_reloc_cookie *cookie)
11561
{
11562
  asection *rsec;
11563
 
11564
  rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
11565
  if (rsec && !rsec->gc_mark)
11566
    {
11567
      if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
11568
        rsec->gc_mark = 1;
11569
      else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
11570
        return FALSE;
11571
    }
11572
  return TRUE;
11573
}
11574
 
11575
/* The mark phase of garbage collection.  For a given section, mark
11576
   it and any sections in this section's group, and all the sections
11577
   which define symbols to which it refers.  */
11578
 
11579
bfd_boolean
11580
_bfd_elf_gc_mark (struct bfd_link_info *info,
11581
                  asection *sec,
11582
                  elf_gc_mark_hook_fn gc_mark_hook)
11583
{
11584
  bfd_boolean ret;
11585
  asection *group_sec, *eh_frame;
11586
 
11587
  sec->gc_mark = 1;
11588
 
11589
  /* Mark all the sections in the group.  */
11590
  group_sec = elf_section_data (sec)->next_in_group;
11591
  if (group_sec && !group_sec->gc_mark)
11592
    if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
11593
      return FALSE;
11594
 
11595
  /* Look through the section relocs.  */
11596
  ret = TRUE;
11597
  eh_frame = elf_eh_frame_section (sec->owner);
11598
  if ((sec->flags & SEC_RELOC) != 0
11599
      && sec->reloc_count > 0
11600
      && sec != eh_frame)
11601
    {
11602
      struct elf_reloc_cookie cookie;
11603
 
11604
      if (!init_reloc_cookie_for_section (&cookie, info, sec))
11605
        ret = FALSE;
11606
      else
11607
        {
11608
          for (; cookie.rel < cookie.relend; cookie.rel++)
11609
            if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
11610
              {
11611
                ret = FALSE;
11612
                break;
11613
              }
11614
          fini_reloc_cookie_for_section (&cookie, sec);
11615
        }
11616
    }
11617
 
11618
  if (ret && eh_frame && elf_fde_list (sec))
11619
    {
11620
      struct elf_reloc_cookie cookie;
11621
 
11622
      if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
11623
        ret = FALSE;
11624
      else
11625
        {
11626
          if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
11627
                                      gc_mark_hook, &cookie))
11628
            ret = FALSE;
11629
          fini_reloc_cookie_for_section (&cookie, eh_frame);
11630
        }
11631
    }
11632
 
11633
  return ret;
11634
}
11635
 
11636
/* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
11637
 
11638
struct elf_gc_sweep_symbol_info
11639
{
11640
  struct bfd_link_info *info;
11641
  void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11642
                       bfd_boolean);
11643
};
11644
 
11645
static bfd_boolean
11646
elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
11647
{
11648
  if ((h->root.type == bfd_link_hash_defined
11649
       || h->root.type == bfd_link_hash_defweak)
11650
      && !h->root.u.def.section->gc_mark
11651
      && !(h->root.u.def.section->owner->flags & DYNAMIC))
11652
    {
11653
      struct elf_gc_sweep_symbol_info *inf =
11654
          (struct elf_gc_sweep_symbol_info *) data;
11655
      (*inf->hide_symbol) (inf->info, h, TRUE);
11656
    }
11657
 
11658
  return TRUE;
11659
}
11660
 
11661
/* The sweep phase of garbage collection.  Remove all garbage sections.  */
11662
 
11663
typedef bfd_boolean (*gc_sweep_hook_fn)
11664
  (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11665
 
11666
static bfd_boolean
11667
elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
11668
{
11669
  bfd *sub;
11670
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11671
  gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11672
  unsigned long section_sym_count;
11673
  struct elf_gc_sweep_symbol_info sweep_info;
11674
 
11675
  for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11676
    {
11677
      asection *o;
11678
 
11679
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11680
        continue;
11681
 
11682
      for (o = sub->sections; o != NULL; o = o->next)
11683
        {
11684
          /* When any section in a section group is kept, we keep all
11685
             sections in the section group.  If the first member of
11686
             the section group is excluded, we will also exclude the
11687
             group section.  */
11688
          if (o->flags & SEC_GROUP)
11689
            {
11690
              asection *first = elf_next_in_group (o);
11691
              o->gc_mark = first->gc_mark;
11692
            }
11693
          else if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
11694 148 khays
                   || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
11695 14 khays
            {
11696 148 khays
              /* Keep debug and special sections.  */
11697 14 khays
              o->gc_mark = 1;
11698
            }
11699
 
11700
          if (o->gc_mark)
11701
            continue;
11702
 
11703
          /* Skip sweeping sections already excluded.  */
11704
          if (o->flags & SEC_EXCLUDE)
11705
            continue;
11706
 
11707
          /* Since this is early in the link process, it is simple
11708
             to remove a section from the output.  */
11709
          o->flags |= SEC_EXCLUDE;
11710
 
11711
          if (info->print_gc_sections && o->size != 0)
11712
            _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11713
 
11714
          /* But we also have to update some of the relocation
11715
             info we collected before.  */
11716
          if (gc_sweep_hook
11717
              && (o->flags & SEC_RELOC) != 0
11718
              && o->reloc_count > 0
11719
              && !bfd_is_abs_section (o->output_section))
11720
            {
11721
              Elf_Internal_Rela *internal_relocs;
11722
              bfd_boolean r;
11723
 
11724
              internal_relocs
11725
                = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
11726
                                             info->keep_memory);
11727
              if (internal_relocs == NULL)
11728
                return FALSE;
11729
 
11730
              r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
11731
 
11732
              if (elf_section_data (o)->relocs != internal_relocs)
11733
                free (internal_relocs);
11734
 
11735
              if (!r)
11736
                return FALSE;
11737
            }
11738
        }
11739
    }
11740
 
11741
  /* Remove the symbols that were in the swept sections from the dynamic
11742
     symbol table.  GCFIXME: Anyone know how to get them out of the
11743
     static symbol table as well?  */
11744
  sweep_info.info = info;
11745
  sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
11746
  elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
11747
                          &sweep_info);
11748
 
11749
  _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
11750
  return TRUE;
11751
}
11752
 
11753
/* Propagate collected vtable information.  This is called through
11754
   elf_link_hash_traverse.  */
11755
 
11756
static bfd_boolean
11757
elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
11758
{
11759
  /* Those that are not vtables.  */
11760
  if (h->vtable == NULL || h->vtable->parent == NULL)
11761
    return TRUE;
11762
 
11763
  /* Those vtables that do not have parents, we cannot merge.  */
11764
  if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
11765
    return TRUE;
11766
 
11767
  /* If we've already been done, exit.  */
11768
  if (h->vtable->used && h->vtable->used[-1])
11769
    return TRUE;
11770
 
11771
  /* Make sure the parent's table is up to date.  */
11772
  elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
11773
 
11774
  if (h->vtable->used == NULL)
11775
    {
11776
      /* None of this table's entries were referenced.  Re-use the
11777
         parent's table.  */
11778
      h->vtable->used = h->vtable->parent->vtable->used;
11779
      h->vtable->size = h->vtable->parent->vtable->size;
11780
    }
11781
  else
11782
    {
11783
      size_t n;
11784
      bfd_boolean *cu, *pu;
11785
 
11786
      /* Or the parent's entries into ours.  */
11787
      cu = h->vtable->used;
11788
      cu[-1] = TRUE;
11789
      pu = h->vtable->parent->vtable->used;
11790
      if (pu != NULL)
11791
        {
11792
          const struct elf_backend_data *bed;
11793
          unsigned int log_file_align;
11794
 
11795
          bed = get_elf_backend_data (h->root.u.def.section->owner);
11796
          log_file_align = bed->s->log_file_align;
11797
          n = h->vtable->parent->vtable->size >> log_file_align;
11798
          while (n--)
11799
            {
11800
              if (*pu)
11801
                *cu = TRUE;
11802
              pu++;
11803
              cu++;
11804
            }
11805
        }
11806
    }
11807
 
11808
  return TRUE;
11809
}
11810
 
11811
static bfd_boolean
11812
elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
11813
{
11814
  asection *sec;
11815
  bfd_vma hstart, hend;
11816
  Elf_Internal_Rela *relstart, *relend, *rel;
11817
  const struct elf_backend_data *bed;
11818
  unsigned int log_file_align;
11819
 
11820
  /* Take care of both those symbols that do not describe vtables as
11821
     well as those that are not loaded.  */
11822
  if (h->vtable == NULL || h->vtable->parent == NULL)
11823
    return TRUE;
11824
 
11825
  BFD_ASSERT (h->root.type == bfd_link_hash_defined
11826
              || h->root.type == bfd_link_hash_defweak);
11827
 
11828
  sec = h->root.u.def.section;
11829
  hstart = h->root.u.def.value;
11830
  hend = hstart + h->size;
11831
 
11832
  relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
11833
  if (!relstart)
11834
    return *(bfd_boolean *) okp = FALSE;
11835
  bed = get_elf_backend_data (sec->owner);
11836
  log_file_align = bed->s->log_file_align;
11837
 
11838
  relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
11839
 
11840
  for (rel = relstart; rel < relend; ++rel)
11841
    if (rel->r_offset >= hstart && rel->r_offset < hend)
11842
      {
11843
        /* If the entry is in use, do nothing.  */
11844
        if (h->vtable->used
11845
            && (rel->r_offset - hstart) < h->vtable->size)
11846
          {
11847
            bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
11848
            if (h->vtable->used[entry])
11849
              continue;
11850
          }
11851
        /* Otherwise, kill it.  */
11852
        rel->r_offset = rel->r_info = rel->r_addend = 0;
11853
      }
11854
 
11855
  return TRUE;
11856
}
11857
 
11858
/* Mark sections containing dynamically referenced symbols.  When
11859
   building shared libraries, we must assume that any visible symbol is
11860
   referenced.  */
11861
 
11862
bfd_boolean
11863
bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
11864
{
11865
  struct bfd_link_info *info = (struct bfd_link_info *) inf;
11866
 
11867
  if ((h->root.type == bfd_link_hash_defined
11868
       || h->root.type == bfd_link_hash_defweak)
11869
      && (h->ref_dynamic
11870
          || (!info->executable
11871
              && h->def_regular
11872
              && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
11873
              && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
11874
    h->root.u.def.section->flags |= SEC_KEEP;
11875
 
11876
  return TRUE;
11877
}
11878
 
11879
/* Keep all sections containing symbols undefined on the command-line,
11880
   and the section containing the entry symbol.  */
11881
 
11882
void
11883
_bfd_elf_gc_keep (struct bfd_link_info *info)
11884
{
11885
  struct bfd_sym_chain *sym;
11886
 
11887
  for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
11888
    {
11889
      struct elf_link_hash_entry *h;
11890
 
11891
      h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
11892
                                FALSE, FALSE, FALSE);
11893
 
11894
      if (h != NULL
11895
          && (h->root.type == bfd_link_hash_defined
11896
              || h->root.type == bfd_link_hash_defweak)
11897
          && !bfd_is_abs_section (h->root.u.def.section))
11898
        h->root.u.def.section->flags |= SEC_KEEP;
11899
    }
11900
}
11901
 
11902
/* Do mark and sweep of unused sections.  */
11903
 
11904
bfd_boolean
11905
bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
11906
{
11907
  bfd_boolean ok = TRUE;
11908
  bfd *sub;
11909
  elf_gc_mark_hook_fn gc_mark_hook;
11910
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11911
 
11912
  if (!bed->can_gc_sections
11913
      || !is_elf_hash_table (info->hash))
11914
    {
11915
      (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
11916
      return TRUE;
11917
    }
11918
 
11919
  bed->gc_keep (info);
11920
 
11921
  /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
11922
     at the .eh_frame section if we can mark the FDEs individually.  */
11923
  _bfd_elf_begin_eh_frame_parsing (info);
11924
  for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11925
    {
11926
      asection *sec;
11927
      struct elf_reloc_cookie cookie;
11928
 
11929
      sec = bfd_get_section_by_name (sub, ".eh_frame");
11930
      if (sec && init_reloc_cookie_for_section (&cookie, info, sec))
11931
        {
11932
          _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
11933
          if (elf_section_data (sec)->sec_info)
11934
            elf_eh_frame_section (sub) = sec;
11935
          fini_reloc_cookie_for_section (&cookie, sec);
11936
        }
11937
    }
11938
  _bfd_elf_end_eh_frame_parsing (info);
11939
 
11940
  /* Apply transitive closure to the vtable entry usage info.  */
11941
  elf_link_hash_traverse (elf_hash_table (info),
11942
                          elf_gc_propagate_vtable_entries_used,
11943
                          &ok);
11944
  if (!ok)
11945
    return FALSE;
11946
 
11947
  /* Kill the vtable relocations that were not used.  */
11948
  elf_link_hash_traverse (elf_hash_table (info),
11949
                          elf_gc_smash_unused_vtentry_relocs,
11950
                          &ok);
11951
  if (!ok)
11952
    return FALSE;
11953
 
11954
  /* Mark dynamically referenced symbols.  */
11955
  if (elf_hash_table (info)->dynamic_sections_created)
11956
    elf_link_hash_traverse (elf_hash_table (info),
11957
                            bed->gc_mark_dynamic_ref,
11958
                            info);
11959
 
11960
  /* Grovel through relocs to find out who stays ...  */
11961
  gc_mark_hook = bed->gc_mark_hook;
11962
  for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11963
    {
11964
      asection *o;
11965
 
11966
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11967
        continue;
11968
 
11969 148 khays
      /* Also keep SHT_NOTE sections.  */
11970 14 khays
      for (o = sub->sections; o != NULL; o = o->next)
11971 148 khays
        if ((o->flags & SEC_EXCLUDE) == 0
11972
            && ((o->flags & SEC_KEEP) != 0
11973
                || elf_section_data (o)->this_hdr.sh_type == SHT_NOTE)
11974
            && !o->gc_mark)
11975 14 khays
          if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
11976
            return FALSE;
11977
    }
11978
 
11979
  /* Allow the backend to mark additional target specific sections.  */
11980
  if (bed->gc_mark_extra_sections)
11981
    bed->gc_mark_extra_sections (info, gc_mark_hook);
11982
 
11983
  /* ... and mark SEC_EXCLUDE for those that go.  */
11984
  return elf_gc_sweep (abfd, info);
11985
}
11986
 
11987
/* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
11988
 
11989
bfd_boolean
11990
bfd_elf_gc_record_vtinherit (bfd *abfd,
11991
                             asection *sec,
11992
                             struct elf_link_hash_entry *h,
11993
                             bfd_vma offset)
11994
{
11995
  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
11996
  struct elf_link_hash_entry **search, *child;
11997
  bfd_size_type extsymcount;
11998
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11999
 
12000
  /* The sh_info field of the symtab header tells us where the
12001
     external symbols start.  We don't care about the local symbols at
12002
     this point.  */
12003
  extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12004
  if (!elf_bad_symtab (abfd))
12005
    extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12006
 
12007
  sym_hashes = elf_sym_hashes (abfd);
12008
  sym_hashes_end = sym_hashes + extsymcount;
12009
 
12010
  /* Hunt down the child symbol, which is in this section at the same
12011
     offset as the relocation.  */
12012
  for (search = sym_hashes; search != sym_hashes_end; ++search)
12013
    {
12014
      if ((child = *search) != NULL
12015
          && (child->root.type == bfd_link_hash_defined
12016
              || child->root.type == bfd_link_hash_defweak)
12017
          && child->root.u.def.section == sec
12018
          && child->root.u.def.value == offset)
12019
        goto win;
12020
    }
12021
 
12022
  (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12023
                         abfd, sec, (unsigned long) offset);
12024
  bfd_set_error (bfd_error_invalid_operation);
12025
  return FALSE;
12026
 
12027
 win:
12028
  if (!child->vtable)
12029
    {
12030
      child->vtable = (struct elf_link_virtual_table_entry *)
12031
          bfd_zalloc (abfd, sizeof (*child->vtable));
12032
      if (!child->vtable)
12033
        return FALSE;
12034
    }
12035
  if (!h)
12036
    {
12037
      /* This *should* only be the absolute section.  It could potentially
12038
         be that someone has defined a non-global vtable though, which
12039
         would be bad.  It isn't worth paging in the local symbols to be
12040
         sure though; that case should simply be handled by the assembler.  */
12041
 
12042
      child->vtable->parent = (struct elf_link_hash_entry *) -1;
12043
    }
12044
  else
12045
    child->vtable->parent = h;
12046
 
12047
  return TRUE;
12048
}
12049
 
12050
/* Called from check_relocs to record the existence of a VTENTRY reloc.  */
12051
 
12052
bfd_boolean
12053
bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12054
                           asection *sec ATTRIBUTE_UNUSED,
12055
                           struct elf_link_hash_entry *h,
12056
                           bfd_vma addend)
12057
{
12058
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12059
  unsigned int log_file_align = bed->s->log_file_align;
12060
 
12061
  if (!h->vtable)
12062
    {
12063
      h->vtable = (struct elf_link_virtual_table_entry *)
12064
          bfd_zalloc (abfd, sizeof (*h->vtable));
12065
      if (!h->vtable)
12066
        return FALSE;
12067
    }
12068
 
12069
  if (addend >= h->vtable->size)
12070
    {
12071
      size_t size, bytes, file_align;
12072
      bfd_boolean *ptr = h->vtable->used;
12073
 
12074
      /* While the symbol is undefined, we have to be prepared to handle
12075
         a zero size.  */
12076
      file_align = 1 << log_file_align;
12077
      if (h->root.type == bfd_link_hash_undefined)
12078
        size = addend + file_align;
12079
      else
12080
        {
12081
          size = h->size;
12082
          if (addend >= size)
12083
            {
12084
              /* Oops!  We've got a reference past the defined end of
12085
                 the table.  This is probably a bug -- shall we warn?  */
12086
              size = addend + file_align;
12087
            }
12088
        }
12089
      size = (size + file_align - 1) & -file_align;
12090
 
12091
      /* Allocate one extra entry for use as a "done" flag for the
12092
         consolidation pass.  */
12093
      bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12094
 
12095
      if (ptr)
12096
        {
12097
          ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
12098
 
12099
          if (ptr != NULL)
12100
            {
12101
              size_t oldbytes;
12102
 
12103
              oldbytes = (((h->vtable->size >> log_file_align) + 1)
12104
                          * sizeof (bfd_boolean));
12105
              memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12106
            }
12107
        }
12108
      else
12109
        ptr = (bfd_boolean *) bfd_zmalloc (bytes);
12110
 
12111
      if (ptr == NULL)
12112
        return FALSE;
12113
 
12114
      /* And arrange for that done flag to be at index -1.  */
12115
      h->vtable->used = ptr + 1;
12116
      h->vtable->size = size;
12117
    }
12118
 
12119
  h->vtable->used[addend >> log_file_align] = TRUE;
12120
 
12121
  return TRUE;
12122
}
12123
 
12124
struct alloc_got_off_arg {
12125
  bfd_vma gotoff;
12126
  struct bfd_link_info *info;
12127
};
12128
 
12129
/* We need a special top-level link routine to convert got reference counts
12130
   to real got offsets.  */
12131
 
12132
static bfd_boolean
12133
elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
12134
{
12135
  struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
12136
  bfd *obfd = gofarg->info->output_bfd;
12137
  const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12138
 
12139
  if (h->got.refcount > 0)
12140
    {
12141
      h->got.offset = gofarg->gotoff;
12142
      gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
12143
    }
12144
  else
12145
    h->got.offset = (bfd_vma) -1;
12146
 
12147
  return TRUE;
12148
}
12149
 
12150
/* And an accompanying bit to work out final got entry offsets once
12151
   we're done.  Should be called from final_link.  */
12152
 
12153
bfd_boolean
12154
bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
12155
                                        struct bfd_link_info *info)
12156
{
12157
  bfd *i;
12158
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12159
  bfd_vma gotoff;
12160
  struct alloc_got_off_arg gofarg;
12161
 
12162
  BFD_ASSERT (abfd == info->output_bfd);
12163
 
12164
  if (! is_elf_hash_table (info->hash))
12165
    return FALSE;
12166
 
12167
  /* The GOT offset is relative to the .got section, but the GOT header is
12168
     put into the .got.plt section, if the backend uses it.  */
12169
  if (bed->want_got_plt)
12170
    gotoff = 0;
12171
  else
12172
    gotoff = bed->got_header_size;
12173
 
12174
  /* Do the local .got entries first.  */
12175
  for (i = info->input_bfds; i; i = i->link_next)
12176
    {
12177
      bfd_signed_vma *local_got;
12178
      bfd_size_type j, locsymcount;
12179
      Elf_Internal_Shdr *symtab_hdr;
12180
 
12181
      if (bfd_get_flavour (i) != bfd_target_elf_flavour)
12182
        continue;
12183
 
12184
      local_got = elf_local_got_refcounts (i);
12185
      if (!local_got)
12186
        continue;
12187
 
12188
      symtab_hdr = &elf_tdata (i)->symtab_hdr;
12189
      if (elf_bad_symtab (i))
12190
        locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12191
      else
12192
        locsymcount = symtab_hdr->sh_info;
12193
 
12194
      for (j = 0; j < locsymcount; ++j)
12195
        {
12196
          if (local_got[j] > 0)
12197
            {
12198
              local_got[j] = gotoff;
12199
              gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
12200
            }
12201
          else
12202
            local_got[j] = (bfd_vma) -1;
12203
        }
12204
    }
12205
 
12206
  /* Then the global .got entries.  .plt refcounts are handled by
12207
     adjust_dynamic_symbol  */
12208
  gofarg.gotoff = gotoff;
12209
  gofarg.info = info;
12210
  elf_link_hash_traverse (elf_hash_table (info),
12211
                          elf_gc_allocate_got_offsets,
12212
                          &gofarg);
12213
  return TRUE;
12214
}
12215
 
12216
/* Many folk need no more in the way of final link than this, once
12217
   got entry reference counting is enabled.  */
12218
 
12219
bfd_boolean
12220
bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
12221
{
12222
  if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
12223
    return FALSE;
12224
 
12225
  /* Invoke the regular ELF backend linker to do all the work.  */
12226
  return bfd_elf_final_link (abfd, info);
12227
}
12228
 
12229
bfd_boolean
12230
bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
12231
{
12232
  struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
12233
 
12234
  if (rcookie->bad_symtab)
12235
    rcookie->rel = rcookie->rels;
12236
 
12237
  for (; rcookie->rel < rcookie->relend; rcookie->rel++)
12238
    {
12239
      unsigned long r_symndx;
12240
 
12241
      if (! rcookie->bad_symtab)
12242
        if (rcookie->rel->r_offset > offset)
12243
          return FALSE;
12244
      if (rcookie->rel->r_offset != offset)
12245
        continue;
12246
 
12247
      r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
12248
      if (r_symndx == STN_UNDEF)
12249
        return TRUE;
12250
 
12251
      if (r_symndx >= rcookie->locsymcount
12252
          || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12253
        {
12254
          struct elf_link_hash_entry *h;
12255
 
12256
          h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
12257
 
12258
          while (h->root.type == bfd_link_hash_indirect
12259
                 || h->root.type == bfd_link_hash_warning)
12260
            h = (struct elf_link_hash_entry *) h->root.u.i.link;
12261
 
12262
          if ((h->root.type == bfd_link_hash_defined
12263
               || h->root.type == bfd_link_hash_defweak)
12264
              && elf_discarded_section (h->root.u.def.section))
12265
            return TRUE;
12266
          else
12267
            return FALSE;
12268
        }
12269
      else
12270
        {
12271
          /* It's not a relocation against a global symbol,
12272
             but it could be a relocation against a local
12273
             symbol for a discarded section.  */
12274
          asection *isec;
12275
          Elf_Internal_Sym *isym;
12276
 
12277
          /* Need to: get the symbol; get the section.  */
12278
          isym = &rcookie->locsyms[r_symndx];
12279
          isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
12280
          if (isec != NULL && elf_discarded_section (isec))
12281
            return TRUE;
12282
        }
12283
      return FALSE;
12284
    }
12285
  return FALSE;
12286
}
12287
 
12288
/* Discard unneeded references to discarded sections.
12289
   Returns TRUE if any section's size was changed.  */
12290
/* This function assumes that the relocations are in sorted order,
12291
   which is true for all known assemblers.  */
12292
 
12293
bfd_boolean
12294
bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
12295
{
12296
  struct elf_reloc_cookie cookie;
12297
  asection *stab, *eh;
12298
  const struct elf_backend_data *bed;
12299
  bfd *abfd;
12300
  bfd_boolean ret = FALSE;
12301
 
12302
  if (info->traditional_format
12303
      || !is_elf_hash_table (info->hash))
12304
    return FALSE;
12305
 
12306
  _bfd_elf_begin_eh_frame_parsing (info);
12307
  for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
12308
    {
12309
      if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
12310
        continue;
12311
 
12312
      bed = get_elf_backend_data (abfd);
12313
 
12314
      if ((abfd->flags & DYNAMIC) != 0)
12315
        continue;
12316
 
12317
      eh = NULL;
12318
      if (!info->relocatable)
12319
        {
12320
          eh = bfd_get_section_by_name (abfd, ".eh_frame");
12321
          if (eh != NULL
12322
              && (eh->size == 0
12323
                  || bfd_is_abs_section (eh->output_section)))
12324
            eh = NULL;
12325
        }
12326
 
12327
      stab = bfd_get_section_by_name (abfd, ".stab");
12328
      if (stab != NULL
12329
          && (stab->size == 0
12330
              || bfd_is_abs_section (stab->output_section)
12331
              || stab->sec_info_type != ELF_INFO_TYPE_STABS))
12332
        stab = NULL;
12333
 
12334
      if (stab == NULL
12335
          && eh == NULL
12336
          && bed->elf_backend_discard_info == NULL)
12337
        continue;
12338
 
12339
      if (!init_reloc_cookie (&cookie, info, abfd))
12340
        return FALSE;
12341
 
12342
      if (stab != NULL
12343
          && stab->reloc_count > 0
12344
          && init_reloc_cookie_rels (&cookie, info, abfd, stab))
12345
        {
12346
          if (_bfd_discard_section_stabs (abfd, stab,
12347
                                          elf_section_data (stab)->sec_info,
12348
                                          bfd_elf_reloc_symbol_deleted_p,
12349
                                          &cookie))
12350
            ret = TRUE;
12351
          fini_reloc_cookie_rels (&cookie, stab);
12352
        }
12353
 
12354
      if (eh != NULL
12355
          && init_reloc_cookie_rels (&cookie, info, abfd, eh))
12356
        {
12357
          _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
12358
          if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
12359
                                                 bfd_elf_reloc_symbol_deleted_p,
12360
                                                 &cookie))
12361
            ret = TRUE;
12362
          fini_reloc_cookie_rels (&cookie, eh);
12363
        }
12364
 
12365
      if (bed->elf_backend_discard_info != NULL
12366
          && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
12367
        ret = TRUE;
12368
 
12369
      fini_reloc_cookie (&cookie, abfd);
12370
    }
12371
  _bfd_elf_end_eh_frame_parsing (info);
12372
 
12373
  if (info->eh_frame_hdr
12374
      && !info->relocatable
12375
      && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
12376
    ret = TRUE;
12377
 
12378
  return ret;
12379
}
12380
 
12381
/* For a SHT_GROUP section, return the group signature.  For other
12382
   sections, return the normal section name.  */
12383
 
12384
static const char *
12385
section_signature (asection *sec)
12386
{
12387
  if ((sec->flags & SEC_GROUP) != 0
12388
      && elf_next_in_group (sec) != NULL
12389
      && elf_group_name (elf_next_in_group (sec)) != NULL)
12390
    return elf_group_name (elf_next_in_group (sec));
12391
  return sec->name;
12392
}
12393
 
12394
void
12395
_bfd_elf_section_already_linked (bfd *abfd, asection *sec,
12396
                                 struct bfd_link_info *info)
12397
{
12398
  flagword flags;
12399
  const char *name, *p;
12400
  struct bfd_section_already_linked *l;
12401
  struct bfd_section_already_linked_hash_entry *already_linked_list;
12402
 
12403
  if (sec->output_section == bfd_abs_section_ptr)
12404
    return;
12405
 
12406
  flags = sec->flags;
12407
 
12408
  /* Return if it isn't a linkonce section.  A comdat group section
12409
     also has SEC_LINK_ONCE set.  */
12410
  if ((flags & SEC_LINK_ONCE) == 0)
12411
    return;
12412
 
12413
  /* Don't put group member sections on our list of already linked
12414
     sections.  They are handled as a group via their group section.  */
12415
  if (elf_sec_group (sec) != NULL)
12416
    return;
12417
 
12418
  /* FIXME: When doing a relocatable link, we may have trouble
12419
     copying relocations in other sections that refer to local symbols
12420
     in the section being discarded.  Those relocations will have to
12421
     be converted somehow; as of this writing I'm not sure that any of
12422
     the backends handle that correctly.
12423
 
12424
     It is tempting to instead not discard link once sections when
12425
     doing a relocatable link (technically, they should be discarded
12426
     whenever we are building constructors).  However, that fails,
12427
     because the linker winds up combining all the link once sections
12428
     into a single large link once section, which defeats the purpose
12429
     of having link once sections in the first place.
12430
 
12431
     Also, not merging link once sections in a relocatable link
12432
     causes trouble for MIPS ELF, which relies on link once semantics
12433
     to handle the .reginfo section correctly.  */
12434
 
12435
  name = section_signature (sec);
12436
 
12437
  if (CONST_STRNEQ (name, ".gnu.linkonce.")
12438
      && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
12439
    p++;
12440
  else
12441
    p = name;
12442
 
12443
  already_linked_list = bfd_section_already_linked_table_lookup (p);
12444
 
12445
  for (l = already_linked_list->entry; l != NULL; l = l->next)
12446
    {
12447
      /* We may have 2 different types of sections on the list: group
12448
         sections and linkonce sections.  Match like sections.  */
12449
      if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
12450
          && strcmp (name, section_signature (l->sec)) == 0
12451
          && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
12452
        {
12453
          /* The section has already been linked.  See if we should
12454
             issue a warning.  */
12455
          switch (flags & SEC_LINK_DUPLICATES)
12456
            {
12457
            default:
12458
              abort ();
12459
 
12460
            case SEC_LINK_DUPLICATES_DISCARD:
12461
              break;
12462
 
12463
            case SEC_LINK_DUPLICATES_ONE_ONLY:
12464
              (*_bfd_error_handler)
12465
                (_("%B: ignoring duplicate section `%A'"),
12466
                 abfd, sec);
12467
              break;
12468
 
12469
            case SEC_LINK_DUPLICATES_SAME_SIZE:
12470
              if (sec->size != l->sec->size)
12471
                (*_bfd_error_handler)
12472
                  (_("%B: duplicate section `%A' has different size"),
12473
                   abfd, sec);
12474
              break;
12475
 
12476
            case SEC_LINK_DUPLICATES_SAME_CONTENTS:
12477
              if (sec->size != l->sec->size)
12478
                (*_bfd_error_handler)
12479
                  (_("%B: duplicate section `%A' has different size"),
12480
                   abfd, sec);
12481
              else if (sec->size != 0)
12482
                {
12483
                  bfd_byte *sec_contents, *l_sec_contents;
12484
 
12485
                  if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
12486
                    (*_bfd_error_handler)
12487
                      (_("%B: warning: could not read contents of section `%A'"),
12488
                       abfd, sec);
12489
                  else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
12490
                                                        &l_sec_contents))
12491
                    (*_bfd_error_handler)
12492
                      (_("%B: warning: could not read contents of section `%A'"),
12493
                       l->sec->owner, l->sec);
12494
                  else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
12495
                    (*_bfd_error_handler)
12496
                      (_("%B: warning: duplicate section `%A' has different contents"),
12497
                       abfd, sec);
12498
 
12499
                  if (sec_contents)
12500
                    free (sec_contents);
12501
                  if (l_sec_contents)
12502
                    free (l_sec_contents);
12503
                }
12504
              break;
12505
            }
12506
 
12507
          /* Set the output_section field so that lang_add_section
12508
             does not create a lang_input_section structure for this
12509
             section.  Since there might be a symbol in the section
12510
             being discarded, we must retain a pointer to the section
12511
             which we are really going to use.  */
12512
          sec->output_section = bfd_abs_section_ptr;
12513
          sec->kept_section = l->sec;
12514
 
12515
          if (flags & SEC_GROUP)
12516
            {
12517
              asection *first = elf_next_in_group (sec);
12518
              asection *s = first;
12519
 
12520
              while (s != NULL)
12521
                {
12522
                  s->output_section = bfd_abs_section_ptr;
12523
                  /* Record which group discards it.  */
12524
                  s->kept_section = l->sec;
12525
                  s = elf_next_in_group (s);
12526
                  /* These lists are circular.  */
12527
                  if (s == first)
12528
                    break;
12529
                }
12530
            }
12531
 
12532
          return;
12533
        }
12534
    }
12535
 
12536
  /* A single member comdat group section may be discarded by a
12537
     linkonce section and vice versa.  */
12538
 
12539
  if ((flags & SEC_GROUP) != 0)
12540
    {
12541
      asection *first = elf_next_in_group (sec);
12542
 
12543
      if (first != NULL && elf_next_in_group (first) == first)
12544
        /* Check this single member group against linkonce sections.  */
12545
        for (l = already_linked_list->entry; l != NULL; l = l->next)
12546
          if ((l->sec->flags & SEC_GROUP) == 0
12547
              && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
12548
              && bfd_elf_match_symbols_in_sections (l->sec, first, info))
12549
            {
12550
              first->output_section = bfd_abs_section_ptr;
12551
              first->kept_section = l->sec;
12552
              sec->output_section = bfd_abs_section_ptr;
12553
              break;
12554
            }
12555
    }
12556
  else
12557
    /* Check this linkonce section against single member groups.  */
12558
    for (l = already_linked_list->entry; l != NULL; l = l->next)
12559
      if (l->sec->flags & SEC_GROUP)
12560
        {
12561
          asection *first = elf_next_in_group (l->sec);
12562
 
12563
          if (first != NULL
12564
              && elf_next_in_group (first) == first
12565
              && bfd_elf_match_symbols_in_sections (first, sec, info))
12566
            {
12567
              sec->output_section = bfd_abs_section_ptr;
12568
              sec->kept_section = first;
12569
              break;
12570
            }
12571
        }
12572
 
12573
  /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
12574
     referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
12575
     specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
12576
     prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
12577
     matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
12578
     but its `.gnu.linkonce.t.F' is discarded means we chose one-only
12579
     `.gnu.linkonce.t.F' section from a different bfd not requiring any
12580
     `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
12581
     The reverse order cannot happen as there is never a bfd with only the
12582
     `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
12583
     matter as here were are looking only for cross-bfd sections.  */
12584
 
12585
  if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
12586
    for (l = already_linked_list->entry; l != NULL; l = l->next)
12587
      if ((l->sec->flags & SEC_GROUP) == 0
12588
          && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
12589
        {
12590
          if (abfd != l->sec->owner)
12591
            sec->output_section = bfd_abs_section_ptr;
12592
          break;
12593
        }
12594
 
12595
  /* This is the first section with this name.  Record it.  */
12596
  if (! bfd_section_already_linked_table_insert (already_linked_list, sec))
12597
    info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
12598
}
12599
 
12600
bfd_boolean
12601
_bfd_elf_common_definition (Elf_Internal_Sym *sym)
12602
{
12603
  return sym->st_shndx == SHN_COMMON;
12604
}
12605
 
12606
unsigned int
12607
_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12608
{
12609
  return SHN_COMMON;
12610
}
12611
 
12612
asection *
12613
_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12614
{
12615
  return bfd_com_section_ptr;
12616
}
12617
 
12618
bfd_vma
12619
_bfd_elf_default_got_elt_size (bfd *abfd,
12620
                               struct bfd_link_info *info ATTRIBUTE_UNUSED,
12621
                               struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
12622
                               bfd *ibfd ATTRIBUTE_UNUSED,
12623
                               unsigned long symndx ATTRIBUTE_UNUSED)
12624
{
12625
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12626
  return bed->s->arch_size / 8;
12627
}
12628
 
12629
/* Routines to support the creation of dynamic relocs.  */
12630
 
12631
/* Returns the name of the dynamic reloc section associated with SEC.  */
12632
 
12633
static const char *
12634
get_dynamic_reloc_section_name (bfd *       abfd,
12635
                                asection *  sec,
12636
                                bfd_boolean is_rela)
12637
{
12638
  char *name;
12639
  const char *old_name = bfd_get_section_name (NULL, sec);
12640
  const char *prefix = is_rela ? ".rela" : ".rel";
12641
 
12642
  if (old_name == NULL)
12643
    return NULL;
12644
 
12645
  name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
12646
  sprintf (name, "%s%s", prefix, old_name);
12647
 
12648
  return name;
12649
}
12650
 
12651
/* Returns the dynamic reloc section associated with SEC.
12652
   If necessary compute the name of the dynamic reloc section based
12653
   on SEC's name (looked up in ABFD's string table) and the setting
12654
   of IS_RELA.  */
12655
 
12656
asection *
12657
_bfd_elf_get_dynamic_reloc_section (bfd *       abfd,
12658
                                    asection *  sec,
12659
                                    bfd_boolean is_rela)
12660
{
12661
  asection * reloc_sec = elf_section_data (sec)->sreloc;
12662
 
12663
  if (reloc_sec == NULL)
12664
    {
12665
      const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12666
 
12667
      if (name != NULL)
12668
        {
12669
          reloc_sec = bfd_get_section_by_name (abfd, name);
12670
 
12671
          if (reloc_sec != NULL)
12672
            elf_section_data (sec)->sreloc = reloc_sec;
12673
        }
12674
    }
12675
 
12676
  return reloc_sec;
12677
}
12678
 
12679
/* Returns the dynamic reloc section associated with SEC.  If the
12680
   section does not exist it is created and attached to the DYNOBJ
12681
   bfd and stored in the SRELOC field of SEC's elf_section_data
12682
   structure.
12683
 
12684
   ALIGNMENT is the alignment for the newly created section and
12685
   IS_RELA defines whether the name should be .rela.<SEC's name>
12686
   or .rel.<SEC's name>.  The section name is looked up in the
12687
   string table associated with ABFD.  */
12688
 
12689
asection *
12690
_bfd_elf_make_dynamic_reloc_section (asection *         sec,
12691
                                     bfd *              dynobj,
12692
                                     unsigned int       alignment,
12693
                                     bfd *              abfd,
12694
                                     bfd_boolean        is_rela)
12695
{
12696
  asection * reloc_sec = elf_section_data (sec)->sreloc;
12697
 
12698
  if (reloc_sec == NULL)
12699
    {
12700
      const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12701
 
12702
      if (name == NULL)
12703
        return NULL;
12704
 
12705
      reloc_sec = bfd_get_section_by_name (dynobj, name);
12706
 
12707
      if (reloc_sec == NULL)
12708
        {
12709
          flagword flags;
12710
 
12711
          flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_IN_MEMORY | SEC_LINKER_CREATED);
12712
          if ((sec->flags & SEC_ALLOC) != 0)
12713
            flags |= SEC_ALLOC | SEC_LOAD;
12714
 
12715
          reloc_sec = bfd_make_section_with_flags (dynobj, name, flags);
12716
          if (reloc_sec != NULL)
12717
            {
12718
              if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
12719
                reloc_sec = NULL;
12720
            }
12721
        }
12722
 
12723
      elf_section_data (sec)->sreloc = reloc_sec;
12724
    }
12725
 
12726
  return reloc_sec;
12727
}
12728
 
12729
/* Copy the ELF symbol type associated with a linker hash entry.  */
12730
void
12731
_bfd_elf_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
12732
    struct bfd_link_hash_entry * hdest,
12733
    struct bfd_link_hash_entry * hsrc)
12734
{
12735
  struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *)hdest;
12736
  struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *)hsrc;
12737
 
12738
  ehdest->type = ehsrc->type;
12739
  ehdest->target_internal = ehsrc->target_internal;
12740
}
12741
 
12742
/* Append a RELA relocation REL to section S in BFD.  */
12743
 
12744
void
12745
elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
12746
{
12747
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12748
  bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
12749
  BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
12750
  bed->s->swap_reloca_out (abfd, rel, loc);
12751
}
12752
 
12753
/* Append a REL relocation REL to section S in BFD.  */
12754
 
12755
void
12756
elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
12757
{
12758
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12759
  bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
12760
  BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
12761
  bed->s->swap_reloca_out (abfd, rel, loc);
12762
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.