Subversion Repositories Kolibri OS

Rev

Rev 5197 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5197 serge 1
/* ELF linking support for BFD.
6324 serge 2
   Copyright (C) 1995-2015 Free Software Foundation, Inc.
5197 serge 3
 
4
   This file is part of BFD, the Binary File Descriptor library.
5
 
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3 of the License, or
9
   (at your option) any later version.
10
 
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
 
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19
   MA 02110-1301, USA.  */
20
 
21
#include "sysdep.h"
22
#include "bfd.h"
6324 serge 23
#include "bfd_stdint.h"
5197 serge 24
#include "bfdlink.h"
25
#include "libbfd.h"
26
#define ARCH_SIZE 0
27
#include "elf-bfd.h"
28
#include "safe-ctype.h"
29
#include "libiberty.h"
30
#include "objalloc.h"
31
 
32
/* This struct is used to pass information to routines called via
33
   elf_link_hash_traverse which must return failure.  */
34
 
35
struct elf_info_failed
36
{
37
  struct bfd_link_info *info;
38
  bfd_boolean failed;
39
};
40
 
41
/* This structure is used to pass information to
42
   _bfd_elf_link_find_version_dependencies.  */
43
 
44
struct elf_find_verdep_info
45
{
46
  /* General link information.  */
47
  struct bfd_link_info *info;
48
  /* The number of dependencies.  */
49
  unsigned int vers;
50
  /* Whether we had a failure.  */
51
  bfd_boolean failed;
52
};
53
 
54
static bfd_boolean _bfd_elf_fix_symbol_flags
55
  (struct elf_link_hash_entry *, struct elf_info_failed *);
56
 
6324 serge 57
asection *
58
_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
59
			     unsigned long r_symndx,
60
			     bfd_boolean discard)
61
{
62
  if (r_symndx >= cookie->locsymcount
63
      || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
64
    {
65
      struct elf_link_hash_entry *h;
66
 
67
      h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
68
 
69
      while (h->root.type == bfd_link_hash_indirect
70
	     || h->root.type == bfd_link_hash_warning)
71
	h = (struct elf_link_hash_entry *) h->root.u.i.link;
72
 
73
      if ((h->root.type == bfd_link_hash_defined
74
	   || h->root.type == bfd_link_hash_defweak)
75
	   && discarded_section (h->root.u.def.section))
76
        return h->root.u.def.section;
77
      else
78
	return NULL;
79
    }
80
  else
81
    {
82
      /* It's not a relocation against a global symbol,
83
	 but it could be a relocation against a local
84
	 symbol for a discarded section.  */
85
      asection *isec;
86
      Elf_Internal_Sym *isym;
87
 
88
      /* Need to: get the symbol; get the section.  */
89
      isym = &cookie->locsyms[r_symndx];
90
      isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
91
      if (isec != NULL
92
	  && discard ? discarded_section (isec) : 1)
93
	return isec;
94
     }
95
  return NULL;
96
}
97
 
5197 serge 98
/* Define a symbol in a dynamic linkage section.  */
99
 
100
struct elf_link_hash_entry *
101
_bfd_elf_define_linkage_sym (bfd *abfd,
102
			     struct bfd_link_info *info,
103
			     asection *sec,
104
			     const char *name)
105
{
106
  struct elf_link_hash_entry *h;
107
  struct bfd_link_hash_entry *bh;
108
  const struct elf_backend_data *bed;
109
 
110
  h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
111
  if (h != NULL)
112
    {
113
      /* Zap symbol defined in an as-needed lib that wasn't linked.
114
	 This is a symptom of a larger problem:  Absolute symbols
115
	 defined in shared libraries can't be overridden, because we
116
	 lose the link to the bfd which is via the symbol section.  */
117
      h->root.type = bfd_link_hash_new;
118
    }
119
 
120
  bh = &h->root;
6324 serge 121
  bed = get_elf_backend_data (abfd);
5197 serge 122
  if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
6324 serge 123
					 sec, 0, NULL, FALSE, bed->collect,
5197 serge 124
					 &bh))
125
    return NULL;
126
  h = (struct elf_link_hash_entry *) bh;
127
  h->def_regular = 1;
128
  h->non_elf = 0;
6324 serge 129
  h->root.linker_def = 1;
5197 serge 130
  h->type = STT_OBJECT;
131
  if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
132
    h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
133
 
134
  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
135
  return h;
136
}
137
 
138
bfd_boolean
139
_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
140
{
141
  flagword flags;
142
  asection *s;
143
  struct elf_link_hash_entry *h;
144
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
145
  struct elf_link_hash_table *htab = elf_hash_table (info);
146
 
147
  /* This function may be called more than once.  */
148
  s = bfd_get_linker_section (abfd, ".got");
149
  if (s != NULL)
150
    return TRUE;
151
 
152
  flags = bed->dynamic_sec_flags;
153
 
154
  s = bfd_make_section_anyway_with_flags (abfd,
155
					  (bed->rela_plts_and_copies_p
156
					   ? ".rela.got" : ".rel.got"),
157
					  (bed->dynamic_sec_flags
158
					   | SEC_READONLY));
159
  if (s == NULL
160
      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
161
    return FALSE;
162
  htab->srelgot = s;
163
 
164
  s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
165
  if (s == NULL
166
      || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167
    return FALSE;
168
  htab->sgot = s;
169
 
170
  if (bed->want_got_plt)
171
    {
172
      s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
173
      if (s == NULL
174
	  || !bfd_set_section_alignment (abfd, s,
175
					 bed->s->log_file_align))
176
	return FALSE;
177
      htab->sgotplt = s;
178
    }
179
 
180
  /* The first bit of the global offset table is the header.  */
181
  s->size += bed->got_header_size;
182
 
183
  if (bed->want_got_sym)
184
    {
185
      /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
186
	 (or .got.plt) section.  We don't do this in the linker script
187
	 because we don't want to define the symbol if we are not creating
188
	 a global offset table.  */
189
      h = _bfd_elf_define_linkage_sym (abfd, info, s,
190
				       "_GLOBAL_OFFSET_TABLE_");
191
      elf_hash_table (info)->hgot = h;
192
      if (h == NULL)
193
	return FALSE;
194
    }
195
 
196
  return TRUE;
197
}
198
 
199
/* Create a strtab to hold the dynamic symbol names.  */
200
static bfd_boolean
201
_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
202
{
203
  struct elf_link_hash_table *hash_table;
204
 
205
  hash_table = elf_hash_table (info);
206
  if (hash_table->dynobj == NULL)
207
    hash_table->dynobj = abfd;
208
 
209
  if (hash_table->dynstr == NULL)
210
    {
211
      hash_table->dynstr = _bfd_elf_strtab_init ();
212
      if (hash_table->dynstr == NULL)
213
	return FALSE;
214
    }
215
  return TRUE;
216
}
217
 
218
/* Create some sections which will be filled in with dynamic linking
219
   information.  ABFD is an input file which requires dynamic sections
220
   to be created.  The dynamic sections take up virtual memory space
221
   when the final executable is run, so we need to create them before
222
   addresses are assigned to the output sections.  We work out the
223
   actual contents and size of these sections later.  */
224
 
225
bfd_boolean
226
_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
227
{
228
  flagword flags;
229
  asection *s;
230
  const struct elf_backend_data *bed;
231
  struct elf_link_hash_entry *h;
232
 
233
  if (! is_elf_hash_table (info->hash))
234
    return FALSE;
235
 
236
  if (elf_hash_table (info)->dynamic_sections_created)
237
    return TRUE;
238
 
239
  if (!_bfd_elf_link_create_dynstrtab (abfd, info))
240
    return FALSE;
241
 
242
  abfd = elf_hash_table (info)->dynobj;
243
  bed = get_elf_backend_data (abfd);
244
 
245
  flags = bed->dynamic_sec_flags;
246
 
247
  /* A dynamically linked executable has a .interp section, but a
248
     shared library does not.  */
6324 serge 249
  if (bfd_link_executable (info) && !info->nointerp)
5197 serge 250
    {
251
      s = bfd_make_section_anyway_with_flags (abfd, ".interp",
252
					      flags | SEC_READONLY);
253
      if (s == NULL)
254
	return FALSE;
255
    }
256
 
257
  /* Create sections to hold version informations.  These are removed
258
     if they are not needed.  */
259
  s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
260
					  flags | SEC_READONLY);
261
  if (s == NULL
262
      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
263
    return FALSE;
264
 
265
  s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
266
					  flags | SEC_READONLY);
267
  if (s == NULL
268
      || ! bfd_set_section_alignment (abfd, s, 1))
269
    return FALSE;
270
 
271
  s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
272
					  flags | SEC_READONLY);
273
  if (s == NULL
274
      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
275
    return FALSE;
276
 
277
  s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
278
					  flags | SEC_READONLY);
279
  if (s == NULL
280
      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
281
    return FALSE;
6324 serge 282
  elf_hash_table (info)->dynsym = s;
5197 serge 283
 
284
  s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
285
					  flags | SEC_READONLY);
286
  if (s == NULL)
287
    return FALSE;
288
 
289
  s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
290
  if (s == NULL
291
      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
292
    return FALSE;
293
 
294
  /* The special symbol _DYNAMIC is always set to the start of the
295
     .dynamic section.  We could set _DYNAMIC in a linker script, but we
296
     only want to define it if we are, in fact, creating a .dynamic
297
     section.  We don't want to define it if there is no .dynamic
298
     section, since on some ELF platforms the start up code examines it
299
     to decide how to initialize the process.  */
300
  h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
301
  elf_hash_table (info)->hdynamic = h;
302
  if (h == NULL)
303
    return FALSE;
304
 
305
  if (info->emit_hash)
306
    {
307
      s = bfd_make_section_anyway_with_flags (abfd, ".hash",
308
					      flags | SEC_READONLY);
309
      if (s == NULL
310
	  || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
311
	return FALSE;
312
      elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
313
    }
314
 
315
  if (info->emit_gnu_hash)
316
    {
317
      s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
318
					      flags | SEC_READONLY);
319
      if (s == NULL
320
	  || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
321
	return FALSE;
322
      /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
323
	 4 32-bit words followed by variable count of 64-bit words, then
324
	 variable count of 32-bit words.  */
325
      if (bed->s->arch_size == 64)
326
	elf_section_data (s)->this_hdr.sh_entsize = 0;
327
      else
328
	elf_section_data (s)->this_hdr.sh_entsize = 4;
329
    }
330
 
331
  /* Let the backend create the rest of the sections.  This lets the
332
     backend set the right flags.  The backend will normally create
333
     the .got and .plt sections.  */
334
  if (bed->elf_backend_create_dynamic_sections == NULL
335
      || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
336
    return FALSE;
337
 
338
  elf_hash_table (info)->dynamic_sections_created = TRUE;
339
 
340
  return TRUE;
341
}
342
 
343
/* Create dynamic sections when linking against a dynamic object.  */
344
 
345
bfd_boolean
346
_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
347
{
348
  flagword flags, pltflags;
349
  struct elf_link_hash_entry *h;
350
  asection *s;
351
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
352
  struct elf_link_hash_table *htab = elf_hash_table (info);
353
 
354
  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
355
     .rel[a].bss sections.  */
356
  flags = bed->dynamic_sec_flags;
357
 
358
  pltflags = flags;
359
  if (bed->plt_not_loaded)
360
    /* We do not clear SEC_ALLOC here because we still want the OS to
361
       allocate space for the section; it's just that there's nothing
362
       to read in from the object file.  */
363
    pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
364
  else
365
    pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
366
  if (bed->plt_readonly)
367
    pltflags |= SEC_READONLY;
368
 
369
  s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
370
  if (s == NULL
371
      || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
372
    return FALSE;
373
  htab->splt = s;
374
 
375
  /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
376
     .plt section.  */
377
  if (bed->want_plt_sym)
378
    {
379
      h = _bfd_elf_define_linkage_sym (abfd, info, s,
380
				       "_PROCEDURE_LINKAGE_TABLE_");
381
      elf_hash_table (info)->hplt = h;
382
      if (h == NULL)
383
	return FALSE;
384
    }
385
 
386
  s = bfd_make_section_anyway_with_flags (abfd,
387
					  (bed->rela_plts_and_copies_p
388
					   ? ".rela.plt" : ".rel.plt"),
389
					  flags | SEC_READONLY);
390
  if (s == NULL
391
      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
392
    return FALSE;
393
  htab->srelplt = s;
394
 
395
  if (! _bfd_elf_create_got_section (abfd, info))
396
    return FALSE;
397
 
398
  if (bed->want_dynbss)
399
    {
400
      /* The .dynbss section is a place to put symbols which are defined
401
	 by dynamic objects, are referenced by regular objects, and are
402
	 not functions.  We must allocate space for them in the process
403
	 image and use a R_*_COPY reloc to tell the dynamic linker to
404
	 initialize them at run time.  The linker script puts the .dynbss
405
	 section into the .bss section of the final image.  */
406
      s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
407
					      (SEC_ALLOC | SEC_LINKER_CREATED));
408
      if (s == NULL)
409
	return FALSE;
410
 
411
      /* The .rel[a].bss section holds copy relocs.  This section is not
412
	 normally needed.  We need to create it here, though, so that the
413
	 linker will map it to an output section.  We can't just create it
414
	 only if we need it, because we will not know whether we need it
415
	 until we have seen all the input files, and the first time the
416
	 main linker code calls BFD after examining all the input files
417
	 (size_dynamic_sections) the input sections have already been
418
	 mapped to the output sections.  If the section turns out not to
419
	 be needed, we can discard it later.  We will never need this
420
	 section when generating a shared object, since they do not use
421
	 copy relocs.  */
6324 serge 422
      if (! bfd_link_pic (info))
5197 serge 423
	{
424
	  s = bfd_make_section_anyway_with_flags (abfd,
425
						  (bed->rela_plts_and_copies_p
426
						   ? ".rela.bss" : ".rel.bss"),
427
						  flags | SEC_READONLY);
428
	  if (s == NULL
429
	      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
430
	    return FALSE;
431
	}
432
    }
433
 
434
  return TRUE;
435
}
436
 
437
/* Record a new dynamic symbol.  We record the dynamic symbols as we
438
   read the input files, since we need to have a list of all of them
439
   before we can determine the final sizes of the output sections.
440
   Note that we may actually call this function even though we are not
441
   going to output any dynamic symbols; in some cases we know that a
442
   symbol should be in the dynamic symbol table, but only if there is
443
   one.  */
444
 
445
bfd_boolean
446
bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
447
				    struct elf_link_hash_entry *h)
448
{
449
  if (h->dynindx == -1)
450
    {
451
      struct elf_strtab_hash *dynstr;
452
      char *p;
453
      const char *name;
454
      bfd_size_type indx;
455
 
456
      /* XXX: The ABI draft says the linker must turn hidden and
457
	 internal symbols into STB_LOCAL symbols when producing the
458
	 DSO. However, if ld.so honors st_other in the dynamic table,
459
	 this would not be necessary.  */
460
      switch (ELF_ST_VISIBILITY (h->other))
461
	{
462
	case STV_INTERNAL:
463
	case STV_HIDDEN:
464
	  if (h->root.type != bfd_link_hash_undefined
465
	      && h->root.type != bfd_link_hash_undefweak)
466
	    {
467
	      h->forced_local = 1;
468
	      if (!elf_hash_table (info)->is_relocatable_executable)
469
		return TRUE;
470
	    }
471
 
472
	default:
473
	  break;
474
	}
475
 
476
      h->dynindx = elf_hash_table (info)->dynsymcount;
477
      ++elf_hash_table (info)->dynsymcount;
478
 
479
      dynstr = elf_hash_table (info)->dynstr;
480
      if (dynstr == NULL)
481
	{
482
	  /* Create a strtab to hold the dynamic symbol names.  */
483
	  elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
484
	  if (dynstr == NULL)
485
	    return FALSE;
486
	}
487
 
488
      /* We don't put any version information in the dynamic string
489
	 table.  */
490
      name = h->root.root.string;
491
      p = strchr (name, ELF_VER_CHR);
492
      if (p != NULL)
493
	/* We know that the p points into writable memory.  In fact,
494
	   there are only a few symbols that have read-only names, being
495
	   those like _GLOBAL_OFFSET_TABLE_ that are created specially
496
	   by the backends.  Most symbols will have names pointing into
497
	   an ELF string table read from a file, or to objalloc memory.  */
498
	*p = 0;
499
 
500
      indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
501
 
502
      if (p != NULL)
503
	*p = ELF_VER_CHR;
504
 
505
      if (indx == (bfd_size_type) -1)
506
	return FALSE;
507
      h->dynstr_index = indx;
508
    }
509
 
510
  return TRUE;
511
}
512
 
513
/* Mark a symbol dynamic.  */
514
 
515
static void
516
bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
517
				  struct elf_link_hash_entry *h,
518
				  Elf_Internal_Sym *sym)
519
{
520
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
521
 
522
  /* It may be called more than once on the same H.  */
6324 serge 523
  if(h->dynamic || bfd_link_relocatable (info))
5197 serge 524
    return;
525
 
526
  if ((info->dynamic_data
527
       && (h->type == STT_OBJECT
528
	   || (sym != NULL
529
	       && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
530
      || (d != NULL
531
	  && h->root.type == bfd_link_hash_new
532
	  && (*d->match) (&d->head, NULL, h->root.root.string)))
533
    h->dynamic = 1;
534
}
535
 
536
/* Record an assignment to a symbol made by a linker script.  We need
537
   this in case some dynamic object refers to this symbol.  */
538
 
539
bfd_boolean
540
bfd_elf_record_link_assignment (bfd *output_bfd,
541
				struct bfd_link_info *info,
542
				const char *name,
543
				bfd_boolean provide,
544
				bfd_boolean hidden)
545
{
546
  struct elf_link_hash_entry *h, *hv;
547
  struct elf_link_hash_table *htab;
548
  const struct elf_backend_data *bed;
549
 
550
  if (!is_elf_hash_table (info->hash))
551
    return TRUE;
552
 
553
  htab = elf_hash_table (info);
554
  h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
555
  if (h == NULL)
556
    return provide;
557
 
558
  switch (h->root.type)
559
    {
560
    case bfd_link_hash_defined:
561
    case bfd_link_hash_defweak:
562
    case bfd_link_hash_common:
563
      break;
564
    case bfd_link_hash_undefweak:
565
    case bfd_link_hash_undefined:
566
      /* Since we're defining the symbol, don't let it seem to have not
567
	 been defined.  record_dynamic_symbol and size_dynamic_sections
568
	 may depend on this.  */
569
      h->root.type = bfd_link_hash_new;
570
      if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
571
	bfd_link_repair_undef_list (&htab->root);
572
      break;
573
    case bfd_link_hash_new:
574
      bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
575
      h->non_elf = 0;
576
      break;
577
    case bfd_link_hash_indirect:
578
      /* We had a versioned symbol in a dynamic library.  We make the
579
	 the versioned symbol point to this one.  */
580
      bed = get_elf_backend_data (output_bfd);
581
      hv = h;
582
      while (hv->root.type == bfd_link_hash_indirect
583
	     || hv->root.type == bfd_link_hash_warning)
584
	hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
585
      /* We don't need to update h->root.u since linker will set them
586
	 later.  */
587
      h->root.type = bfd_link_hash_undefined;
588
      hv->root.type = bfd_link_hash_indirect;
589
      hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
590
      (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
591
      break;
592
    case bfd_link_hash_warning:
593
      abort ();
594
      break;
595
    }
596
 
597
  /* If this symbol is being provided by the linker script, and it is
598
     currently defined by a dynamic object, but not by a regular
599
     object, then mark it as undefined so that the generic linker will
600
     force the correct value.  */
601
  if (provide
602
      && h->def_dynamic
603
      && !h->def_regular)
604
    h->root.type = bfd_link_hash_undefined;
605
 
606
  /* If this symbol is not being provided by the linker script, and it is
607
     currently defined by a dynamic object, but not by a regular object,
608
     then clear out any version information because the symbol will not be
609
     associated with the dynamic object any more.  */
610
  if (!provide
611
      && h->def_dynamic
612
      && !h->def_regular)
613
    h->verinfo.verdef = NULL;
614
 
615
  h->def_regular = 1;
616
 
617
  if (hidden)
618
    {
619
      bed = get_elf_backend_data (output_bfd);
620
      if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
621
	h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
622
      (*bed->elf_backend_hide_symbol) (info, h, TRUE);
623
    }
624
 
625
  /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
626
     and executables.  */
6324 serge 627
  if (!bfd_link_relocatable (info)
5197 serge 628
      && h->dynindx != -1
629
      && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
630
	  || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
631
    h->forced_local = 1;
632
 
633
  if ((h->def_dynamic
634
       || h->ref_dynamic
6324 serge 635
       || bfd_link_pic (info)
636
       || (bfd_link_pde (info)
637
	   && elf_hash_table (info)->is_relocatable_executable))
5197 serge 638
      && h->dynindx == -1)
639
    {
640
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
641
	return FALSE;
642
 
643
      /* If this is a weak defined symbol, and we know a corresponding
644
	 real symbol from the same dynamic object, make sure the real
645
	 symbol is also made into a dynamic symbol.  */
646
      if (h->u.weakdef != NULL
647
	  && h->u.weakdef->dynindx == -1)
648
	{
649
	  if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
650
	    return FALSE;
651
	}
652
    }
653
 
654
  return TRUE;
655
}
656
 
657
/* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
658
   success, and 2 on a failure caused by attempting to record a symbol
659
   in a discarded section, eg. a discarded link-once section symbol.  */
660
 
661
int
662
bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
663
					  bfd *input_bfd,
664
					  long input_indx)
665
{
666
  bfd_size_type amt;
667
  struct elf_link_local_dynamic_entry *entry;
668
  struct elf_link_hash_table *eht;
669
  struct elf_strtab_hash *dynstr;
670
  unsigned long dynstr_index;
671
  char *name;
672
  Elf_External_Sym_Shndx eshndx;
673
  char esym[sizeof (Elf64_External_Sym)];
674
 
675
  if (! is_elf_hash_table (info->hash))
676
    return 0;
677
 
678
  /* See if the entry exists already.  */
679
  for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
680
    if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
681
      return 1;
682
 
683
  amt = sizeof (*entry);
684
  entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
685
  if (entry == NULL)
686
    return 0;
687
 
688
  /* Go find the symbol, so that we can find it's name.  */
689
  if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
690
			     1, input_indx, &entry->isym, esym, &eshndx))
691
    {
692
      bfd_release (input_bfd, entry);
693
      return 0;
694
    }
695
 
696
  if (entry->isym.st_shndx != SHN_UNDEF
697
      && entry->isym.st_shndx < SHN_LORESERVE)
698
    {
699
      asection *s;
700
 
701
      s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
702
      if (s == NULL || bfd_is_abs_section (s->output_section))
703
	{
704
	  /* We can still bfd_release here as nothing has done another
705
	     bfd_alloc.  We can't do this later in this function.  */
706
	  bfd_release (input_bfd, entry);
707
	  return 2;
708
	}
709
    }
710
 
711
  name = (bfd_elf_string_from_elf_section
712
	  (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
713
	   entry->isym.st_name));
714
 
715
  dynstr = elf_hash_table (info)->dynstr;
716
  if (dynstr == NULL)
717
    {
718
      /* Create a strtab to hold the dynamic symbol names.  */
719
      elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
720
      if (dynstr == NULL)
721
	return 0;
722
    }
723
 
724
  dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
725
  if (dynstr_index == (unsigned long) -1)
726
    return 0;
727
  entry->isym.st_name = dynstr_index;
728
 
729
  eht = elf_hash_table (info);
730
 
731
  entry->next = eht->dynlocal;
732
  eht->dynlocal = entry;
733
  entry->input_bfd = input_bfd;
734
  entry->input_indx = input_indx;
735
  eht->dynsymcount++;
736
 
737
  /* Whatever binding the symbol had before, it's now local.  */
738
  entry->isym.st_info
739
    = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
740
 
741
  /* The dynindx will be set at the end of size_dynamic_sections.  */
742
 
743
  return 1;
744
}
745
 
746
/* Return the dynindex of a local dynamic symbol.  */
747
 
748
long
749
_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
750
				    bfd *input_bfd,
751
				    long input_indx)
752
{
753
  struct elf_link_local_dynamic_entry *e;
754
 
755
  for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
756
    if (e->input_bfd == input_bfd && e->input_indx == input_indx)
757
      return e->dynindx;
758
  return -1;
759
}
760
 
761
/* This function is used to renumber the dynamic symbols, if some of
762
   them are removed because they are marked as local.  This is called
763
   via elf_link_hash_traverse.  */
764
 
765
static bfd_boolean
766
elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
767
				      void *data)
768
{
769
  size_t *count = (size_t *) data;
770
 
771
  if (h->forced_local)
772
    return TRUE;
773
 
774
  if (h->dynindx != -1)
775
    h->dynindx = ++(*count);
776
 
777
  return TRUE;
778
}
779
 
780
 
781
/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
782
   STB_LOCAL binding.  */
783
 
784
static bfd_boolean
785
elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
786
					    void *data)
787
{
788
  size_t *count = (size_t *) data;
789
 
790
  if (!h->forced_local)
791
    return TRUE;
792
 
793
  if (h->dynindx != -1)
794
    h->dynindx = ++(*count);
795
 
796
  return TRUE;
797
}
798
 
799
/* Return true if the dynamic symbol for a given section should be
800
   omitted when creating a shared library.  */
801
bfd_boolean
802
_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
803
				   struct bfd_link_info *info,
804
				   asection *p)
805
{
806
  struct elf_link_hash_table *htab;
6324 serge 807
  asection *ip;
5197 serge 808
 
809
  switch (elf_section_data (p)->this_hdr.sh_type)
810
    {
811
    case SHT_PROGBITS:
812
    case SHT_NOBITS:
813
      /* If sh_type is yet undecided, assume it could be
814
	 SHT_PROGBITS/SHT_NOBITS.  */
815
    case SHT_NULL:
816
      htab = elf_hash_table (info);
817
      if (p == htab->tls_sec)
818
	return FALSE;
819
 
820
      if (htab->text_index_section != NULL)
821
	return p != htab->text_index_section && p != htab->data_index_section;
822
 
6324 serge 823
      return (htab->dynobj != NULL
5197 serge 824
	      && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
6324 serge 825
	      && ip->output_section == p);
5197 serge 826
 
827
      /* There shouldn't be section relative relocations
828
	 against any other section.  */
829
    default:
830
      return TRUE;
831
    }
832
}
833
 
834
/* Assign dynsym indices.  In a shared library we generate a section
835
   symbol for each output section, which come first.  Next come symbols
836
   which have been forced to local binding.  Then all of the back-end
837
   allocated local dynamic syms, followed by the rest of the global
838
   symbols.  */
839
 
840
static unsigned long
841
_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
842
				struct bfd_link_info *info,
843
				unsigned long *section_sym_count)
844
{
845
  unsigned long dynsymcount = 0;
846
 
6324 serge 847
  if (bfd_link_pic (info)
848
      || elf_hash_table (info)->is_relocatable_executable)
5197 serge 849
    {
850
      const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
851
      asection *p;
852
      for (p = output_bfd->sections; p ; p = p->next)
853
	if ((p->flags & SEC_EXCLUDE) == 0
854
	    && (p->flags & SEC_ALLOC) != 0
855
	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
856
	  elf_section_data (p)->dynindx = ++dynsymcount;
857
	else
858
	  elf_section_data (p)->dynindx = 0;
859
    }
860
  *section_sym_count = dynsymcount;
861
 
862
  elf_link_hash_traverse (elf_hash_table (info),
863
			  elf_link_renumber_local_hash_table_dynsyms,
864
			  &dynsymcount);
865
 
866
  if (elf_hash_table (info)->dynlocal)
867
    {
868
      struct elf_link_local_dynamic_entry *p;
869
      for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
870
	p->dynindx = ++dynsymcount;
871
    }
872
 
873
  elf_link_hash_traverse (elf_hash_table (info),
874
			  elf_link_renumber_hash_table_dynsyms,
875
			  &dynsymcount);
876
 
877
  /* There is an unused NULL entry at the head of the table which
878
     we must account for in our count.  Unless there weren't any
879
     symbols, which means we'll have no table at all.  */
880
  if (dynsymcount != 0)
881
    ++dynsymcount;
882
 
883
  elf_hash_table (info)->dynsymcount = dynsymcount;
884
  return dynsymcount;
885
}
886
 
887
/* Merge st_other field.  */
888
 
889
static void
890
elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
6324 serge 891
		    const Elf_Internal_Sym *isym, asection *sec,
892
		    bfd_boolean definition, bfd_boolean dynamic)
5197 serge 893
{
894
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
895
 
896
  /* If st_other has a processor-specific meaning, specific
6324 serge 897
     code might be needed here.  */
5197 serge 898
  if (bed->elf_backend_merge_symbol_attribute)
899
    (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
900
						dynamic);
901
 
6324 serge 902
  if (!dynamic)
5197 serge 903
    {
6324 serge 904
      unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
905
      unsigned hvis = ELF_ST_VISIBILITY (h->other);
5197 serge 906
 
6324 serge 907
      /* Keep the most constraining visibility.  Leave the remainder
908
	 of the st_other field to elf_backend_merge_symbol_attribute.  */
909
      if (symvis - 1 < hvis - 1)
910
	h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
5197 serge 911
    }
6324 serge 912
  else if (definition
913
	   && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
914
	   && (sec->flags & SEC_READONLY) == 0)
915
    h->protected_def = 1;
5197 serge 916
}
917
 
918
/* This function is called when we want to merge a new symbol with an
919
   existing symbol.  It handles the various cases which arise when we
920
   find a definition in a dynamic object, or when there is already a
921
   definition in a dynamic object.  The new symbol is described by
922
   NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
923
   entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
924
   if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
925
   of an old common symbol.  We set OVERRIDE if the old symbol is
926
   overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
927
   the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
928
   to change.  By OK to change, we mean that we shouldn't warn if the
929
   type or size does change.  */
930
 
931
static bfd_boolean
932
_bfd_elf_merge_symbol (bfd *abfd,
933
		       struct bfd_link_info *info,
934
		       const char *name,
935
		       Elf_Internal_Sym *sym,
936
		       asection **psec,
937
		       bfd_vma *pvalue,
938
		       struct elf_link_hash_entry **sym_hash,
939
		       bfd **poldbfd,
940
		       bfd_boolean *pold_weak,
941
		       unsigned int *pold_alignment,
942
		       bfd_boolean *skip,
943
		       bfd_boolean *override,
944
		       bfd_boolean *type_change_ok,
6324 serge 945
		       bfd_boolean *size_change_ok,
946
		       bfd_boolean *matched)
5197 serge 947
{
948
  asection *sec, *oldsec;
949
  struct elf_link_hash_entry *h;
950
  struct elf_link_hash_entry *hi;
951
  struct elf_link_hash_entry *flip;
952
  int bind;
953
  bfd *oldbfd;
954
  bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
955
  bfd_boolean newweak, oldweak, newfunc, oldfunc;
956
  const struct elf_backend_data *bed;
6324 serge 957
  char *new_version;
5197 serge 958
 
959
  *skip = FALSE;
960
  *override = FALSE;
961
 
962
  sec = *psec;
963
  bind = ELF_ST_BIND (sym->st_info);
964
 
965
  if (! bfd_is_und_section (sec))
966
    h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
967
  else
968
    h = ((struct elf_link_hash_entry *)
969
	 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
970
  if (h == NULL)
971
    return FALSE;
972
  *sym_hash = h;
973
 
974
  bed = get_elf_backend_data (abfd);
975
 
6324 serge 976
  /* NEW_VERSION is the symbol version of the new symbol.  */
977
  if (h->versioned != unversioned)
978
    {
979
      /* Symbol version is unknown or versioned.  */
980
      new_version = strrchr (name, ELF_VER_CHR);
981
      if (new_version)
982
	{
983
	  if (h->versioned == unknown)
984
	    {
985
	      if (new_version > name && new_version[-1] != ELF_VER_CHR)
986
		h->versioned = versioned_hidden;
987
	      else
988
		h->versioned = versioned;
989
	    }
990
	  new_version += 1;
991
	  if (new_version[0] == '\0')
992
	    new_version = NULL;
993
	}
994
      else
995
	h->versioned = unversioned;
996
    }
997
  else
998
    new_version = NULL;
999
 
5197 serge 1000
  /* For merging, we only care about real symbols.  But we need to make
1001
     sure that indirect symbol dynamic flags are updated.  */
1002
  hi = h;
1003
  while (h->root.type == bfd_link_hash_indirect
1004
	 || h->root.type == bfd_link_hash_warning)
1005
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
1006
 
6324 serge 1007
  if (!*matched)
1008
    {
1009
      if (hi == h || h->root.type == bfd_link_hash_new)
1010
	*matched = TRUE;
1011
      else
1012
	{
1013
	  /* OLD_HIDDEN is true if the existing symbol is only visible
1014
	     to the symbol with the same symbol version.  NEW_HIDDEN is
1015
	     true if the new symbol is only visible to the symbol with
1016
	     the same symbol version.  */
1017
	  bfd_boolean old_hidden = h->versioned == versioned_hidden;
1018
	  bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1019
	  if (!old_hidden && !new_hidden)
1020
	    /* The new symbol matches the existing symbol if both
1021
	       aren't hidden.  */
1022
	    *matched = TRUE;
1023
	  else
1024
	    {
1025
	      /* OLD_VERSION is the symbol version of the existing
1026
		 symbol. */
1027
	      char *old_version;
1028
 
1029
	      if (h->versioned >= versioned)
1030
		old_version = strrchr (h->root.root.string,
1031
				       ELF_VER_CHR) + 1;
1032
	      else
1033
		 old_version = NULL;
1034
 
1035
	      /* The new symbol matches the existing symbol if they
1036
		 have the same symbol version.  */
1037
	      *matched = (old_version == new_version
1038
			  || (old_version != NULL
1039
			      && new_version != NULL
1040
			      && strcmp (old_version, new_version) == 0));
1041
	    }
1042
	}
1043
    }
1044
 
5197 serge 1045
  /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1046
     existing symbol.  */
1047
 
1048
  oldbfd = NULL;
1049
  oldsec = NULL;
1050
  switch (h->root.type)
1051
    {
1052
    default:
1053
      break;
1054
 
1055
    case bfd_link_hash_undefined:
1056
    case bfd_link_hash_undefweak:
1057
      oldbfd = h->root.u.undef.abfd;
1058
      break;
1059
 
1060
    case bfd_link_hash_defined:
1061
    case bfd_link_hash_defweak:
1062
      oldbfd = h->root.u.def.section->owner;
1063
      oldsec = h->root.u.def.section;
1064
      break;
1065
 
1066
    case bfd_link_hash_common:
1067
      oldbfd = h->root.u.c.p->section->owner;
1068
      oldsec = h->root.u.c.p->section;
1069
      if (pold_alignment)
1070
	*pold_alignment = h->root.u.c.p->alignment_power;
1071
      break;
1072
    }
1073
  if (poldbfd && *poldbfd == NULL)
1074
    *poldbfd = oldbfd;
1075
 
1076
  /* Differentiate strong and weak symbols.  */
1077
  newweak = bind == STB_WEAK;
1078
  oldweak = (h->root.type == bfd_link_hash_defweak
1079
	     || h->root.type == bfd_link_hash_undefweak);
1080
  if (pold_weak)
1081
    *pold_weak = oldweak;
1082
 
1083
  /* This code is for coping with dynamic objects, and is only useful
1084
     if we are doing an ELF link.  */
1085
  if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1086
    return TRUE;
1087
 
1088
  /* We have to check it for every instance since the first few may be
1089
     references and not all compilers emit symbol type for undefined
1090
     symbols.  */
1091
  bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1092
 
1093
  /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1094
     respectively, is from a dynamic object.  */
1095
 
1096
  newdyn = (abfd->flags & DYNAMIC) != 0;
1097
 
1098
  /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1099
     syms and defined syms in dynamic libraries respectively.
1100
     ref_dynamic on the other hand can be set for a symbol defined in
1101
     a dynamic library, and def_dynamic may not be set;  When the
1102
     definition in a dynamic lib is overridden by a definition in the
1103
     executable use of the symbol in the dynamic lib becomes a
1104
     reference to the executable symbol.  */
1105
  if (newdyn)
1106
    {
1107
      if (bfd_is_und_section (sec))
1108
	{
1109
	  if (bind != STB_WEAK)
1110
	    {
1111
	      h->ref_dynamic_nonweak = 1;
1112
	      hi->ref_dynamic_nonweak = 1;
1113
	    }
1114
	}
1115
      else
1116
	{
6324 serge 1117
	  /* Update the existing symbol only if they match. */
1118
	  if (*matched)
1119
	    h->dynamic_def = 1;
5197 serge 1120
	  hi->dynamic_def = 1;
1121
	}
1122
    }
1123
 
1124
  /* If we just created the symbol, mark it as being an ELF symbol.
1125
     Other than that, there is nothing to do--there is no merge issue
1126
     with a newly defined symbol--so we just return.  */
1127
 
1128
  if (h->root.type == bfd_link_hash_new)
1129
    {
1130
      h->non_elf = 0;
1131
      return TRUE;
1132
    }
1133
 
1134
  /* In cases involving weak versioned symbols, we may wind up trying
1135
     to merge a symbol with itself.  Catch that here, to avoid the
1136
     confusion that results if we try to override a symbol with
1137
     itself.  The additional tests catch cases like
1138
     _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1139
     dynamic object, which we do want to handle here.  */
1140
  if (abfd == oldbfd
1141
      && (newweak || oldweak)
1142
      && ((abfd->flags & DYNAMIC) == 0
1143
	  || !h->def_regular))
1144
    return TRUE;
1145
 
1146
  olddyn = FALSE;
1147
  if (oldbfd != NULL)
1148
    olddyn = (oldbfd->flags & DYNAMIC) != 0;
1149
  else if (oldsec != NULL)
1150
    {
1151
      /* This handles the special SHN_MIPS_{TEXT,DATA} section
1152
	 indices used by MIPS ELF.  */
1153
      olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1154
    }
1155
 
1156
  /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1157
     respectively, appear to be a definition rather than reference.  */
1158
 
1159
  newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1160
 
1161
  olddef = (h->root.type != bfd_link_hash_undefined
1162
	    && h->root.type != bfd_link_hash_undefweak
1163
	    && h->root.type != bfd_link_hash_common);
1164
 
1165
  /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1166
     respectively, appear to be a function.  */
1167
 
1168
  newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1169
	     && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1170
 
1171
  oldfunc = (h->type != STT_NOTYPE
1172
	     && bed->is_function_type (h->type));
1173
 
1174
  /* When we try to create a default indirect symbol from the dynamic
1175
     definition with the default version, we skip it if its type and
6324 serge 1176
     the type of existing regular definition mismatch.  */
5197 serge 1177
  if (pold_alignment == NULL
1178
      && newdyn
1179
      && newdef
1180
      && !olddyn
6324 serge 1181
      && (((olddef || h->root.type == bfd_link_hash_common)
1182
	   && ELF_ST_TYPE (sym->st_info) != h->type
1183
	   && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1184
	   && h->type != STT_NOTYPE
1185
	   && !(newfunc && oldfunc))
1186
	  || (olddef
1187
	      && ((h->type == STT_GNU_IFUNC)
1188
		  != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
5197 serge 1189
    {
1190
      *skip = TRUE;
1191
      return TRUE;
1192
    }
1193
 
6324 serge 1194
  /* Check TLS symbols.  We don't check undefined symbols introduced
1195
     by "ld -u" which have no type (and oldbfd NULL), and we don't
1196
     check symbols from plugins because they also have no type.  */
1197
  if (oldbfd != NULL
1198
      && (oldbfd->flags & BFD_PLUGIN) == 0
1199
      && (abfd->flags & BFD_PLUGIN) == 0
1200
      && ELF_ST_TYPE (sym->st_info) != h->type
1201
      && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
5197 serge 1202
    {
1203
      bfd *ntbfd, *tbfd;
1204
      bfd_boolean ntdef, tdef;
1205
      asection *ntsec, *tsec;
1206
 
1207
      if (h->type == STT_TLS)
1208
	{
1209
	  ntbfd = abfd;
1210
	  ntsec = sec;
1211
	  ntdef = newdef;
1212
	  tbfd = oldbfd;
1213
	  tsec = oldsec;
1214
	  tdef = olddef;
1215
	}
1216
      else
1217
	{
1218
	  ntbfd = oldbfd;
1219
	  ntsec = oldsec;
1220
	  ntdef = olddef;
1221
	  tbfd = abfd;
1222
	  tsec = sec;
1223
	  tdef = newdef;
1224
	}
1225
 
1226
      if (tdef && ntdef)
1227
	(*_bfd_error_handler)
1228
	  (_("%s: TLS definition in %B section %A "
1229
	     "mismatches non-TLS definition in %B section %A"),
1230
	   tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1231
      else if (!tdef && !ntdef)
1232
	(*_bfd_error_handler)
1233
	  (_("%s: TLS reference in %B "
1234
	     "mismatches non-TLS reference in %B"),
1235
	   tbfd, ntbfd, h->root.root.string);
1236
      else if (tdef)
1237
	(*_bfd_error_handler)
1238
	  (_("%s: TLS definition in %B section %A "
1239
	     "mismatches non-TLS reference in %B"),
1240
	   tbfd, tsec, ntbfd, h->root.root.string);
1241
      else
1242
	(*_bfd_error_handler)
1243
	  (_("%s: TLS reference in %B "
1244
	     "mismatches non-TLS definition in %B section %A"),
1245
	   tbfd, ntbfd, ntsec, h->root.root.string);
1246
 
1247
      bfd_set_error (bfd_error_bad_value);
1248
      return FALSE;
1249
    }
1250
 
1251
  /* If the old symbol has non-default visibility, we ignore the new
1252
     definition from a dynamic object.  */
1253
  if (newdyn
1254
      && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1255
      && !bfd_is_und_section (sec))
1256
    {
1257
      *skip = TRUE;
1258
      /* Make sure this symbol is dynamic.  */
1259
      h->ref_dynamic = 1;
1260
      hi->ref_dynamic = 1;
1261
      /* A protected symbol has external availability. Make sure it is
1262
	 recorded as dynamic.
1263
 
1264
	 FIXME: Should we check type and size for protected symbol?  */
1265
      if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1266
	return bfd_elf_link_record_dynamic_symbol (info, h);
1267
      else
1268
	return TRUE;
1269
    }
1270
  else if (!newdyn
1271
	   && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1272
	   && h->def_dynamic)
1273
    {
1274
      /* If the new symbol with non-default visibility comes from a
1275
	 relocatable file and the old definition comes from a dynamic
1276
	 object, we remove the old definition.  */
1277
      if (hi->root.type == bfd_link_hash_indirect)
1278
	{
1279
	  /* Handle the case where the old dynamic definition is
1280
	     default versioned.  We need to copy the symbol info from
1281
	     the symbol with default version to the normal one if it
1282
	     was referenced before.  */
1283
	  if (h->ref_regular)
1284
	    {
1285
	      hi->root.type = h->root.type;
1286
	      h->root.type = bfd_link_hash_indirect;
1287
	      (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1288
 
1289
	      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1290
	      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1291
		{
1292
		  /* If the new symbol is hidden or internal, completely undo
1293
		     any dynamic link state.  */
1294
		  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1295
		  h->forced_local = 0;
1296
		  h->ref_dynamic = 0;
1297
		}
1298
	      else
1299
		h->ref_dynamic = 1;
1300
 
1301
	      h->def_dynamic = 0;
1302
	      /* FIXME: Should we check type and size for protected symbol?  */
1303
	      h->size = 0;
1304
	      h->type = 0;
1305
 
1306
	      h = hi;
1307
	    }
1308
	  else
1309
	    h = hi;
1310
	}
1311
 
1312
      /* If the old symbol was undefined before, then it will still be
1313
	 on the undefs list.  If the new symbol is undefined or
1314
	 common, we can't make it bfd_link_hash_new here, because new
1315
	 undefined or common symbols will be added to the undefs list
1316
	 by _bfd_generic_link_add_one_symbol.  Symbols may not be
1317
	 added twice to the undefs list.  Also, if the new symbol is
1318
	 undefweak then we don't want to lose the strong undef.  */
1319
      if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1320
	{
1321
	  h->root.type = bfd_link_hash_undefined;
1322
	  h->root.u.undef.abfd = abfd;
1323
	}
1324
      else
1325
	{
1326
	  h->root.type = bfd_link_hash_new;
1327
	  h->root.u.undef.abfd = NULL;
1328
	}
1329
 
1330
      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1331
	{
1332
	  /* If the new symbol is hidden or internal, completely undo
1333
	     any dynamic link state.  */
1334
	  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1335
	  h->forced_local = 0;
1336
	  h->ref_dynamic = 0;
1337
	}
1338
      else
1339
	h->ref_dynamic = 1;
1340
      h->def_dynamic = 0;
1341
      /* FIXME: Should we check type and size for protected symbol?  */
1342
      h->size = 0;
1343
      h->type = 0;
1344
      return TRUE;
1345
    }
1346
 
1347
  /* If a new weak symbol definition comes from a regular file and the
1348
     old symbol comes from a dynamic library, we treat the new one as
1349
     strong.  Similarly, an old weak symbol definition from a regular
1350
     file is treated as strong when the new symbol comes from a dynamic
1351
     library.  Further, an old weak symbol from a dynamic library is
1352
     treated as strong if the new symbol is from a dynamic library.
1353
     This reflects the way glibc's ld.so works.
1354
 
1355
     Do this before setting *type_change_ok or *size_change_ok so that
1356
     we warn properly when dynamic library symbols are overridden.  */
1357
 
1358
  if (newdef && !newdyn && olddyn)
1359
    newweak = FALSE;
1360
  if (olddef && newdyn)
1361
    oldweak = FALSE;
1362
 
1363
  /* Allow changes between different types of function symbol.  */
1364
  if (newfunc && oldfunc)
1365
    *type_change_ok = TRUE;
1366
 
1367
  /* It's OK to change the type if either the existing symbol or the
1368
     new symbol is weak.  A type change is also OK if the old symbol
1369
     is undefined and the new symbol is defined.  */
1370
 
1371
  if (oldweak
1372
      || newweak
1373
      || (newdef
1374
	  && h->root.type == bfd_link_hash_undefined))
1375
    *type_change_ok = TRUE;
1376
 
1377
  /* It's OK to change the size if either the existing symbol or the
1378
     new symbol is weak, or if the old symbol is undefined.  */
1379
 
1380
  if (*type_change_ok
1381
      || h->root.type == bfd_link_hash_undefined)
1382
    *size_change_ok = TRUE;
1383
 
1384
  /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1385
     symbol, respectively, appears to be a common symbol in a dynamic
1386
     object.  If a symbol appears in an uninitialized section, and is
1387
     not weak, and is not a function, then it may be a common symbol
1388
     which was resolved when the dynamic object was created.  We want
1389
     to treat such symbols specially, because they raise special
1390
     considerations when setting the symbol size: if the symbol
1391
     appears as a common symbol in a regular object, and the size in
1392
     the regular object is larger, we must make sure that we use the
1393
     larger size.  This problematic case can always be avoided in C,
1394
     but it must be handled correctly when using Fortran shared
1395
     libraries.
1396
 
1397
     Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1398
     likewise for OLDDYNCOMMON and OLDDEF.
1399
 
1400
     Note that this test is just a heuristic, and that it is quite
1401
     possible to have an uninitialized symbol in a shared object which
1402
     is really a definition, rather than a common symbol.  This could
1403
     lead to some minor confusion when the symbol really is a common
1404
     symbol in some regular object.  However, I think it will be
1405
     harmless.  */
1406
 
1407
  if (newdyn
1408
      && newdef
1409
      && !newweak
1410
      && (sec->flags & SEC_ALLOC) != 0
1411
      && (sec->flags & SEC_LOAD) == 0
1412
      && sym->st_size > 0
1413
      && !newfunc)
1414
    newdyncommon = TRUE;
1415
  else
1416
    newdyncommon = FALSE;
1417
 
1418
  if (olddyn
1419
      && olddef
1420
      && h->root.type == bfd_link_hash_defined
1421
      && h->def_dynamic
1422
      && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1423
      && (h->root.u.def.section->flags & SEC_LOAD) == 0
1424
      && h->size > 0
1425
      && !oldfunc)
1426
    olddyncommon = TRUE;
1427
  else
1428
    olddyncommon = FALSE;
1429
 
1430
  /* We now know everything about the old and new symbols.  We ask the
1431
     backend to check if we can merge them.  */
1432
  if (bed->merge_symbol != NULL)
1433
    {
1434
      if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1435
	return FALSE;
1436
      sec = *psec;
1437
    }
1438
 
1439
  /* If both the old and the new symbols look like common symbols in a
1440
     dynamic object, set the size of the symbol to the larger of the
1441
     two.  */
1442
 
1443
  if (olddyncommon
1444
      && newdyncommon
1445
      && sym->st_size != h->size)
1446
    {
1447
      /* Since we think we have two common symbols, issue a multiple
1448
	 common warning if desired.  Note that we only warn if the
1449
	 size is different.  If the size is the same, we simply let
1450
	 the old symbol override the new one as normally happens with
1451
	 symbols defined in dynamic objects.  */
1452
 
1453
      if (! ((*info->callbacks->multiple_common)
1454
	     (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1455
	return FALSE;
1456
 
1457
      if (sym->st_size > h->size)
1458
	h->size = sym->st_size;
1459
 
1460
      *size_change_ok = TRUE;
1461
    }
1462
 
1463
  /* If we are looking at a dynamic object, and we have found a
1464
     definition, we need to see if the symbol was already defined by
1465
     some other object.  If so, we want to use the existing
1466
     definition, and we do not want to report a multiple symbol
1467
     definition error; we do this by clobbering *PSEC to be
1468
     bfd_und_section_ptr.
1469
 
1470
     We treat a common symbol as a definition if the symbol in the
1471
     shared library is a function, since common symbols always
1472
     represent variables; this can cause confusion in principle, but
1473
     any such confusion would seem to indicate an erroneous program or
1474
     shared library.  We also permit a common symbol in a regular
1475
     object to override a weak symbol in a shared object.  */
1476
 
1477
  if (newdyn
1478
      && newdef
1479
      && (olddef
1480
	  || (h->root.type == bfd_link_hash_common
1481
	      && (newweak || newfunc))))
1482
    {
1483
      *override = TRUE;
1484
      newdef = FALSE;
1485
      newdyncommon = FALSE;
1486
 
1487
      *psec = sec = bfd_und_section_ptr;
1488
      *size_change_ok = TRUE;
1489
 
1490
      /* If we get here when the old symbol is a common symbol, then
1491
	 we are explicitly letting it override a weak symbol or
1492
	 function in a dynamic object, and we don't want to warn about
1493
	 a type change.  If the old symbol is a defined symbol, a type
1494
	 change warning may still be appropriate.  */
1495
 
1496
      if (h->root.type == bfd_link_hash_common)
1497
	*type_change_ok = TRUE;
1498
    }
1499
 
1500
  /* Handle the special case of an old common symbol merging with a
1501
     new symbol which looks like a common symbol in a shared object.
1502
     We change *PSEC and *PVALUE to make the new symbol look like a
1503
     common symbol, and let _bfd_generic_link_add_one_symbol do the
1504
     right thing.  */
1505
 
1506
  if (newdyncommon
1507
      && h->root.type == bfd_link_hash_common)
1508
    {
1509
      *override = TRUE;
1510
      newdef = FALSE;
1511
      newdyncommon = FALSE;
1512
      *pvalue = sym->st_size;
1513
      *psec = sec = bed->common_section (oldsec);
1514
      *size_change_ok = TRUE;
1515
    }
1516
 
1517
  /* Skip weak definitions of symbols that are already defined.  */
1518
  if (newdef && olddef && newweak)
1519
    {
1520
      /* Don't skip new non-IR weak syms.  */
1521
      if (!(oldbfd != NULL
1522
	    && (oldbfd->flags & BFD_PLUGIN) != 0
1523
	    && (abfd->flags & BFD_PLUGIN) == 0))
6324 serge 1524
	{
1525
	  newdef = FALSE;
1526
	  *skip = TRUE;
1527
	}
5197 serge 1528
 
1529
      /* Merge st_other.  If the symbol already has a dynamic index,
1530
	 but visibility says it should not be visible, turn it into a
1531
	 local symbol.  */
6324 serge 1532
      elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
5197 serge 1533
      if (h->dynindx != -1)
1534
	switch (ELF_ST_VISIBILITY (h->other))
1535
	  {
1536
	  case STV_INTERNAL:
1537
	  case STV_HIDDEN:
1538
	    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1539
	    break;
1540
	  }
1541
    }
1542
 
1543
  /* If the old symbol is from a dynamic object, and the new symbol is
1544
     a definition which is not from a dynamic object, then the new
1545
     symbol overrides the old symbol.  Symbols from regular files
1546
     always take precedence over symbols from dynamic objects, even if
1547
     they are defined after the dynamic object in the link.
1548
 
1549
     As above, we again permit a common symbol in a regular object to
1550
     override a definition in a shared object if the shared object
1551
     symbol is a function or is weak.  */
1552
 
1553
  flip = NULL;
1554
  if (!newdyn
1555
      && (newdef
1556
	  || (bfd_is_com_section (sec)
1557
	      && (oldweak || oldfunc)))
1558
      && olddyn
1559
      && olddef
1560
      && h->def_dynamic)
1561
    {
1562
      /* Change the hash table entry to undefined, and let
1563
	 _bfd_generic_link_add_one_symbol do the right thing with the
1564
	 new definition.  */
1565
 
1566
      h->root.type = bfd_link_hash_undefined;
1567
      h->root.u.undef.abfd = h->root.u.def.section->owner;
1568
      *size_change_ok = TRUE;
1569
 
1570
      olddef = FALSE;
1571
      olddyncommon = FALSE;
1572
 
1573
      /* We again permit a type change when a common symbol may be
1574
	 overriding a function.  */
1575
 
1576
      if (bfd_is_com_section (sec))
1577
	{
1578
	  if (oldfunc)
1579
	    {
1580
	      /* If a common symbol overrides a function, make sure
1581
		 that it isn't defined dynamically nor has type
1582
		 function.  */
1583
	      h->def_dynamic = 0;
1584
	      h->type = STT_NOTYPE;
1585
	    }
1586
	  *type_change_ok = TRUE;
1587
	}
1588
 
1589
      if (hi->root.type == bfd_link_hash_indirect)
1590
	flip = hi;
1591
      else
1592
	/* This union may have been set to be non-NULL when this symbol
1593
	   was seen in a dynamic object.  We must force the union to be
1594
	   NULL, so that it is correct for a regular symbol.  */
1595
	h->verinfo.vertree = NULL;
1596
    }
1597
 
1598
  /* Handle the special case of a new common symbol merging with an
1599
     old symbol that looks like it might be a common symbol defined in
1600
     a shared object.  Note that we have already handled the case in
1601
     which a new common symbol should simply override the definition
1602
     in the shared library.  */
1603
 
1604
  if (! newdyn
1605
      && bfd_is_com_section (sec)
1606
      && olddyncommon)
1607
    {
1608
      /* It would be best if we could set the hash table entry to a
1609
	 common symbol, but we don't know what to use for the section
1610
	 or the alignment.  */
1611
      if (! ((*info->callbacks->multiple_common)
1612
	     (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1613
	return FALSE;
1614
 
1615
      /* If the presumed common symbol in the dynamic object is
1616
	 larger, pretend that the new symbol has its size.  */
1617
 
1618
      if (h->size > *pvalue)
1619
	*pvalue = h->size;
1620
 
1621
      /* We need to remember the alignment required by the symbol
1622
	 in the dynamic object.  */
1623
      BFD_ASSERT (pold_alignment);
1624
      *pold_alignment = h->root.u.def.section->alignment_power;
1625
 
1626
      olddef = FALSE;
1627
      olddyncommon = FALSE;
1628
 
1629
      h->root.type = bfd_link_hash_undefined;
1630
      h->root.u.undef.abfd = h->root.u.def.section->owner;
1631
 
1632
      *size_change_ok = TRUE;
1633
      *type_change_ok = TRUE;
1634
 
1635
      if (hi->root.type == bfd_link_hash_indirect)
1636
	flip = hi;
1637
      else
1638
	h->verinfo.vertree = NULL;
1639
    }
1640
 
1641
  if (flip != NULL)
1642
    {
1643
      /* Handle the case where we had a versioned symbol in a dynamic
1644
	 library and now find a definition in a normal object.  In this
1645
	 case, we make the versioned symbol point to the normal one.  */
1646
      flip->root.type = h->root.type;
1647
      flip->root.u.undef.abfd = h->root.u.undef.abfd;
1648
      h->root.type = bfd_link_hash_indirect;
1649
      h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1650
      (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1651
      if (h->def_dynamic)
1652
	{
1653
	  h->def_dynamic = 0;
1654
	  flip->ref_dynamic = 1;
1655
	}
1656
    }
1657
 
1658
  return TRUE;
1659
}
1660
 
1661
/* This function is called to create an indirect symbol from the
1662
   default for the symbol with the default version if needed. The
1663
   symbol is described by H, NAME, SYM, SEC, and VALUE.  We
1664
   set DYNSYM if the new indirect symbol is dynamic.  */
1665
 
1666
static bfd_boolean
1667
_bfd_elf_add_default_symbol (bfd *abfd,
1668
			     struct bfd_link_info *info,
1669
			     struct elf_link_hash_entry *h,
1670
			     const char *name,
1671
			     Elf_Internal_Sym *sym,
1672
			     asection *sec,
1673
			     bfd_vma value,
1674
			     bfd **poldbfd,
1675
			     bfd_boolean *dynsym)
1676
{
1677
  bfd_boolean type_change_ok;
1678
  bfd_boolean size_change_ok;
1679
  bfd_boolean skip;
1680
  char *shortname;
1681
  struct elf_link_hash_entry *hi;
1682
  struct bfd_link_hash_entry *bh;
1683
  const struct elf_backend_data *bed;
1684
  bfd_boolean collect;
1685
  bfd_boolean dynamic;
1686
  bfd_boolean override;
1687
  char *p;
1688
  size_t len, shortlen;
1689
  asection *tmp_sec;
6324 serge 1690
  bfd_boolean matched;
5197 serge 1691
 
6324 serge 1692
  if (h->versioned == unversioned || h->versioned == versioned_hidden)
1693
    return TRUE;
1694
 
5197 serge 1695
  /* If this symbol has a version, and it is the default version, we
1696
     create an indirect symbol from the default name to the fully
1697
     decorated name.  This will cause external references which do not
1698
     specify a version to be bound to this version of the symbol.  */
1699
  p = strchr (name, ELF_VER_CHR);
6324 serge 1700
  if (h->versioned == unknown)
1701
    {
1702
      if (p == NULL)
1703
	{
1704
	  h->versioned = unversioned;
1705
	  return TRUE;
1706
	}
1707
      else
1708
	{
1709
	  if (p[1] != ELF_VER_CHR)
1710
	    {
1711
	      h->versioned = versioned_hidden;
1712
	      return TRUE;
1713
	    }
1714
	  else
1715
	    h->versioned = versioned;
1716
	}
1717
    }
1718
  else
1719
    {
1720
      /* PR ld/19073: We may see an unversioned definition after the
1721
	 default version.  */
1722
      if (p == NULL)
1723
	return TRUE;
1724
    }
5197 serge 1725
 
1726
  bed = get_elf_backend_data (abfd);
1727
  collect = bed->collect;
1728
  dynamic = (abfd->flags & DYNAMIC) != 0;
1729
 
1730
  shortlen = p - name;
1731
  shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1732
  if (shortname == NULL)
1733
    return FALSE;
1734
  memcpy (shortname, name, shortlen);
1735
  shortname[shortlen] = '\0';
1736
 
1737
  /* We are going to create a new symbol.  Merge it with any existing
1738
     symbol with this name.  For the purposes of the merge, act as
1739
     though we were defining the symbol we just defined, although we
1740
     actually going to define an indirect symbol.  */
1741
  type_change_ok = FALSE;
1742
  size_change_ok = FALSE;
6324 serge 1743
  matched = TRUE;
5197 serge 1744
  tmp_sec = sec;
1745
  if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1746
			      &hi, poldbfd, NULL, NULL, &skip, &override,
6324 serge 1747
			      &type_change_ok, &size_change_ok, &matched))
5197 serge 1748
    return FALSE;
1749
 
1750
  if (skip)
1751
    goto nondefault;
1752
 
1753
  if (! override)
1754
    {
6324 serge 1755
      /* Add the default symbol if not performing a relocatable link.  */
1756
      if (! bfd_link_relocatable (info))
1757
	{
1758
	  bh = &hi->root;
1759
	  if (! (_bfd_generic_link_add_one_symbol
1760
		 (info, abfd, shortname, BSF_INDIRECT,
1761
		  bfd_ind_section_ptr,
1762
		  0, name, FALSE, collect, &bh)))
1763
	    return FALSE;
1764
	  hi = (struct elf_link_hash_entry *) bh;
1765
	}
5197 serge 1766
    }
1767
  else
1768
    {
1769
      /* In this case the symbol named SHORTNAME is overriding the
1770
	 indirect symbol we want to add.  We were planning on making
1771
	 SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
1772
	 is the name without a version.  NAME is the fully versioned
1773
	 name, and it is the default version.
1774
 
1775
	 Overriding means that we already saw a definition for the
1776
	 symbol SHORTNAME in a regular object, and it is overriding
1777
	 the symbol defined in the dynamic object.
1778
 
1779
	 When this happens, we actually want to change NAME, the
1780
	 symbol we just added, to refer to SHORTNAME.  This will cause
1781
	 references to NAME in the shared object to become references
1782
	 to SHORTNAME in the regular object.  This is what we expect
1783
	 when we override a function in a shared object: that the
1784
	 references in the shared object will be mapped to the
1785
	 definition in the regular object.  */
1786
 
1787
      while (hi->root.type == bfd_link_hash_indirect
1788
	     || hi->root.type == bfd_link_hash_warning)
1789
	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1790
 
1791
      h->root.type = bfd_link_hash_indirect;
1792
      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1793
      if (h->def_dynamic)
1794
	{
1795
	  h->def_dynamic = 0;
1796
	  hi->ref_dynamic = 1;
1797
	  if (hi->ref_regular
1798
	      || hi->def_regular)
1799
	    {
1800
	      if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1801
		return FALSE;
1802
	    }
1803
	}
1804
 
1805
      /* Now set HI to H, so that the following code will set the
1806
	 other fields correctly.  */
1807
      hi = h;
1808
    }
1809
 
1810
  /* Check if HI is a warning symbol.  */
1811
  if (hi->root.type == bfd_link_hash_warning)
1812
    hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1813
 
1814
  /* If there is a duplicate definition somewhere, then HI may not
1815
     point to an indirect symbol.  We will have reported an error to
1816
     the user in that case.  */
1817
 
1818
  if (hi->root.type == bfd_link_hash_indirect)
1819
    {
1820
      struct elf_link_hash_entry *ht;
1821
 
1822
      ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1823
      (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1824
 
6324 serge 1825
      /* A reference to the SHORTNAME symbol from a dynamic library
1826
	 will be satisfied by the versioned symbol at runtime.  In
1827
	 effect, we have a reference to the versioned symbol.  */
1828
      ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1829
      hi->dynamic_def |= ht->dynamic_def;
1830
 
5197 serge 1831
      /* See if the new flags lead us to realize that the symbol must
1832
	 be dynamic.  */
1833
      if (! *dynsym)
1834
	{
1835
	  if (! dynamic)
1836
	    {
6324 serge 1837
	      if (! bfd_link_executable (info)
5197 serge 1838
		  || hi->def_dynamic
1839
		  || hi->ref_dynamic)
1840
		*dynsym = TRUE;
1841
	    }
1842
	  else
1843
	    {
1844
	      if (hi->ref_regular)
1845
		*dynsym = TRUE;
1846
	    }
1847
	}
1848
    }
1849
 
1850
  /* We also need to define an indirection from the nondefault version
1851
     of the symbol.  */
1852
 
1853
nondefault:
1854
  len = strlen (name);
1855
  shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1856
  if (shortname == NULL)
1857
    return FALSE;
1858
  memcpy (shortname, name, shortlen);
1859
  memcpy (shortname + shortlen, p + 1, len - shortlen);
1860
 
1861
  /* Once again, merge with any existing symbol.  */
1862
  type_change_ok = FALSE;
1863
  size_change_ok = FALSE;
1864
  tmp_sec = sec;
1865
  if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
6324 serge 1866
			      &hi, poldbfd, NULL, NULL, &skip, &override,
1867
			      &type_change_ok, &size_change_ok, &matched))
5197 serge 1868
    return FALSE;
1869
 
1870
  if (skip)
1871
    return TRUE;
1872
 
1873
  if (override)
1874
    {
1875
      /* Here SHORTNAME is a versioned name, so we don't expect to see
1876
	 the type of override we do in the case above unless it is
1877
	 overridden by a versioned definition.  */
1878
      if (hi->root.type != bfd_link_hash_defined
1879
	  && hi->root.type != bfd_link_hash_defweak)
1880
	(*_bfd_error_handler)
1881
	  (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1882
	   abfd, shortname);
1883
    }
1884
  else
1885
    {
1886
      bh = &hi->root;
1887
      if (! (_bfd_generic_link_add_one_symbol
1888
	     (info, abfd, shortname, BSF_INDIRECT,
1889
	      bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1890
	return FALSE;
1891
      hi = (struct elf_link_hash_entry *) bh;
1892
 
1893
      /* If there is a duplicate definition somewhere, then HI may not
1894
	 point to an indirect symbol.  We will have reported an error
1895
	 to the user in that case.  */
1896
 
1897
      if (hi->root.type == bfd_link_hash_indirect)
1898
	{
1899
	  (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
6324 serge 1900
	  h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1901
	  hi->dynamic_def |= h->dynamic_def;
5197 serge 1902
 
1903
	  /* See if the new flags lead us to realize that the symbol
1904
	     must be dynamic.  */
1905
	  if (! *dynsym)
1906
	    {
1907
	      if (! dynamic)
1908
		{
6324 serge 1909
		  if (! bfd_link_executable (info)
5197 serge 1910
		      || hi->ref_dynamic)
1911
		    *dynsym = TRUE;
1912
		}
1913
	      else
1914
		{
1915
		  if (hi->ref_regular)
1916
		    *dynsym = TRUE;
1917
		}
1918
	    }
1919
	}
1920
    }
1921
 
1922
  return TRUE;
1923
}
1924
 
1925
/* This routine is used to export all defined symbols into the dynamic
1926
   symbol table.  It is called via elf_link_hash_traverse.  */
1927
 
1928
static bfd_boolean
1929
_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1930
{
1931
  struct elf_info_failed *eif = (struct elf_info_failed *) data;
1932
 
1933
  /* Ignore indirect symbols.  These are added by the versioning code.  */
1934
  if (h->root.type == bfd_link_hash_indirect)
1935
    return TRUE;
1936
 
1937
  /* Ignore this if we won't export it.  */
1938
  if (!eif->info->export_dynamic && !h->dynamic)
1939
    return TRUE;
1940
 
1941
  if (h->dynindx == -1
1942
      && (h->def_regular || h->ref_regular)
1943
      && ! bfd_hide_sym_by_version (eif->info->version_info,
1944
				    h->root.root.string))
1945
    {
1946
      if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1947
	{
1948
	  eif->failed = TRUE;
1949
	  return FALSE;
1950
	}
1951
    }
1952
 
1953
  return TRUE;
1954
}
1955
 
1956
/* Look through the symbols which are defined in other shared
1957
   libraries and referenced here.  Update the list of version
1958
   dependencies.  This will be put into the .gnu.version_r section.
1959
   This function is called via elf_link_hash_traverse.  */
1960
 
1961
static bfd_boolean
1962
_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1963
					 void *data)
1964
{
1965
  struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
1966
  Elf_Internal_Verneed *t;
1967
  Elf_Internal_Vernaux *a;
1968
  bfd_size_type amt;
1969
 
1970
  /* We only care about symbols defined in shared objects with version
1971
     information.  */
1972
  if (!h->def_dynamic
1973
      || h->def_regular
1974
      || h->dynindx == -1
6324 serge 1975
      || h->verinfo.verdef == NULL
1976
      || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
1977
	  & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
5197 serge 1978
    return TRUE;
1979
 
1980
  /* See if we already know about this version.  */
1981
  for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1982
       t != NULL;
1983
       t = t->vn_nextref)
1984
    {
1985
      if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1986
	continue;
1987
 
1988
      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1989
	if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1990
	  return TRUE;
1991
 
1992
      break;
1993
    }
1994
 
1995
  /* This is a new version.  Add it to tree we are building.  */
1996
 
1997
  if (t == NULL)
1998
    {
1999
      amt = sizeof *t;
2000
      t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2001
      if (t == NULL)
2002
	{
2003
	  rinfo->failed = TRUE;
2004
	  return FALSE;
2005
	}
2006
 
2007
      t->vn_bfd = h->verinfo.verdef->vd_bfd;
2008
      t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2009
      elf_tdata (rinfo->info->output_bfd)->verref = t;
2010
    }
2011
 
2012
  amt = sizeof *a;
2013
  a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2014
  if (a == NULL)
2015
    {
2016
      rinfo->failed = TRUE;
2017
      return FALSE;
2018
    }
2019
 
2020
  /* Note that we are copying a string pointer here, and testing it
2021
     above.  If bfd_elf_string_from_elf_section is ever changed to
2022
     discard the string data when low in memory, this will have to be
2023
     fixed.  */
2024
  a->vna_nodename = h->verinfo.verdef->vd_nodename;
2025
 
2026
  a->vna_flags = h->verinfo.verdef->vd_flags;
2027
  a->vna_nextptr = t->vn_auxptr;
2028
 
2029
  h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2030
  ++rinfo->vers;
2031
 
2032
  a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2033
 
2034
  t->vn_auxptr = a;
2035
 
2036
  return TRUE;
2037
}
2038
 
2039
/* Figure out appropriate versions for all the symbols.  We may not
2040
   have the version number script until we have read all of the input
2041
   files, so until that point we don't know which symbols should be
2042
   local.  This function is called via elf_link_hash_traverse.  */
2043
 
2044
static bfd_boolean
2045
_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2046
{
2047
  struct elf_info_failed *sinfo;
2048
  struct bfd_link_info *info;
2049
  const struct elf_backend_data *bed;
2050
  struct elf_info_failed eif;
2051
  char *p;
2052
  bfd_size_type amt;
2053
 
2054
  sinfo = (struct elf_info_failed *) data;
2055
  info = sinfo->info;
2056
 
2057
  /* Fix the symbol flags.  */
2058
  eif.failed = FALSE;
2059
  eif.info = info;
2060
  if (! _bfd_elf_fix_symbol_flags (h, &eif))
2061
    {
2062
      if (eif.failed)
2063
	sinfo->failed = TRUE;
2064
      return FALSE;
2065
    }
2066
 
2067
  /* We only need version numbers for symbols defined in regular
2068
     objects.  */
2069
  if (!h->def_regular)
2070
    return TRUE;
2071
 
2072
  bed = get_elf_backend_data (info->output_bfd);
2073
  p = strchr (h->root.root.string, ELF_VER_CHR);
2074
  if (p != NULL && h->verinfo.vertree == NULL)
2075
    {
2076
      struct bfd_elf_version_tree *t;
2077
 
2078
      ++p;
2079
      if (*p == ELF_VER_CHR)
6324 serge 2080
	++p;
5197 serge 2081
 
2082
      /* If there is no version string, we can just return out.  */
2083
      if (*p == '\0')
6324 serge 2084
	return TRUE;
5197 serge 2085
 
2086
      /* Look for the version.  If we find it, it is no longer weak.  */
2087
      for (t = sinfo->info->version_info; t != NULL; t = t->next)
2088
	{
2089
	  if (strcmp (t->name, p) == 0)
2090
	    {
2091
	      size_t len;
2092
	      char *alc;
2093
	      struct bfd_elf_version_expr *d;
2094
 
2095
	      len = p - h->root.root.string;
2096
	      alc = (char *) bfd_malloc (len);
2097
	      if (alc == NULL)
2098
		{
2099
		  sinfo->failed = TRUE;
2100
		  return FALSE;
2101
		}
2102
	      memcpy (alc, h->root.root.string, len - 1);
2103
	      alc[len - 1] = '\0';
2104
	      if (alc[len - 2] == ELF_VER_CHR)
2105
		alc[len - 2] = '\0';
2106
 
2107
	      h->verinfo.vertree = t;
2108
	      t->used = TRUE;
2109
	      d = NULL;
2110
 
2111
	      if (t->globals.list != NULL)
2112
		d = (*t->match) (&t->globals, NULL, alc);
2113
 
2114
	      /* See if there is anything to force this symbol to
2115
		 local scope.  */
2116
	      if (d == NULL && t->locals.list != NULL)
2117
		{
2118
		  d = (*t->match) (&t->locals, NULL, alc);
2119
		  if (d != NULL
2120
		      && h->dynindx != -1
2121
		      && ! info->export_dynamic)
2122
		    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2123
		}
2124
 
2125
	      free (alc);
2126
	      break;
2127
	    }
2128
	}
2129
 
2130
      /* If we are building an application, we need to create a
2131
	 version node for this version.  */
6324 serge 2132
      if (t == NULL && bfd_link_executable (info))
5197 serge 2133
	{
2134
	  struct bfd_elf_version_tree **pp;
2135
	  int version_index;
2136
 
2137
	  /* If we aren't going to export this symbol, we don't need
2138
	     to worry about it.  */
2139
	  if (h->dynindx == -1)
2140
	    return TRUE;
2141
 
2142
	  amt = sizeof *t;
2143
	  t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
2144
	  if (t == NULL)
2145
	    {
2146
	      sinfo->failed = TRUE;
2147
	      return FALSE;
2148
	    }
2149
 
2150
	  t->name = p;
2151
	  t->name_indx = (unsigned int) -1;
2152
	  t->used = TRUE;
2153
 
2154
	  version_index = 1;
2155
	  /* Don't count anonymous version tag.  */
2156
	  if (sinfo->info->version_info != NULL
2157
	      && sinfo->info->version_info->vernum == 0)
2158
	    version_index = 0;
2159
	  for (pp = &sinfo->info->version_info;
2160
	       *pp != NULL;
2161
	       pp = &(*pp)->next)
2162
	    ++version_index;
2163
	  t->vernum = version_index;
2164
 
2165
	  *pp = t;
2166
 
2167
	  h->verinfo.vertree = t;
2168
	}
2169
      else if (t == NULL)
2170
	{
2171
	  /* We could not find the version for a symbol when
2172
	     generating a shared archive.  Return an error.  */
2173
	  (*_bfd_error_handler)
2174
	    (_("%B: version node not found for symbol %s"),
2175
	     info->output_bfd, h->root.root.string);
2176
	  bfd_set_error (bfd_error_bad_value);
2177
	  sinfo->failed = TRUE;
2178
	  return FALSE;
2179
	}
2180
    }
2181
 
2182
  /* If we don't have a version for this symbol, see if we can find
2183
     something.  */
2184
  if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2185
    {
2186
      bfd_boolean hide;
2187
 
2188
      h->verinfo.vertree
2189
	= bfd_find_version_for_sym (sinfo->info->version_info,
2190
				    h->root.root.string, &hide);
2191
      if (h->verinfo.vertree != NULL && hide)
2192
	(*bed->elf_backend_hide_symbol) (info, h, TRUE);
2193
    }
2194
 
2195
  return TRUE;
2196
}
2197
 
2198
/* Read and swap the relocs from the section indicated by SHDR.  This
2199
   may be either a REL or a RELA section.  The relocations are
2200
   translated into RELA relocations and stored in INTERNAL_RELOCS,
2201
   which should have already been allocated to contain enough space.
2202
   The EXTERNAL_RELOCS are a buffer where the external form of the
2203
   relocations should be stored.
2204
 
2205
   Returns FALSE if something goes wrong.  */
2206
 
2207
static bfd_boolean
2208
elf_link_read_relocs_from_section (bfd *abfd,
2209
				   asection *sec,
2210
				   Elf_Internal_Shdr *shdr,
2211
				   void *external_relocs,
2212
				   Elf_Internal_Rela *internal_relocs)
2213
{
2214
  const struct elf_backend_data *bed;
2215
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2216
  const bfd_byte *erela;
2217
  const bfd_byte *erelaend;
2218
  Elf_Internal_Rela *irela;
2219
  Elf_Internal_Shdr *symtab_hdr;
2220
  size_t nsyms;
2221
 
2222
  /* Position ourselves at the start of the section.  */
2223
  if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2224
    return FALSE;
2225
 
2226
  /* Read the relocations.  */
2227
  if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2228
    return FALSE;
2229
 
2230
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2231
  nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2232
 
2233
  bed = get_elf_backend_data (abfd);
2234
 
2235
  /* Convert the external relocations to the internal format.  */
2236
  if (shdr->sh_entsize == bed->s->sizeof_rel)
2237
    swap_in = bed->s->swap_reloc_in;
2238
  else if (shdr->sh_entsize == bed->s->sizeof_rela)
2239
    swap_in = bed->s->swap_reloca_in;
2240
  else
2241
    {
2242
      bfd_set_error (bfd_error_wrong_format);
2243
      return FALSE;
2244
    }
2245
 
2246
  erela = (const bfd_byte *) external_relocs;
2247
  erelaend = erela + shdr->sh_size;
2248
  irela = internal_relocs;
2249
  while (erela < erelaend)
2250
    {
2251
      bfd_vma r_symndx;
2252
 
2253
      (*swap_in) (abfd, erela, irela);
2254
      r_symndx = ELF32_R_SYM (irela->r_info);
2255
      if (bed->s->arch_size == 64)
2256
	r_symndx >>= 24;
2257
      if (nsyms > 0)
2258
	{
2259
	  if ((size_t) r_symndx >= nsyms)
2260
	    {
2261
	      (*_bfd_error_handler)
2262
		(_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2263
		   " for offset 0x%lx in section `%A'"),
2264
		 abfd, sec,
2265
		 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2266
	      bfd_set_error (bfd_error_bad_value);
2267
	      return FALSE;
2268
	    }
2269
	}
2270
      else if (r_symndx != STN_UNDEF)
2271
	{
2272
	  (*_bfd_error_handler)
2273
	    (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2274
	       " when the object file has no symbol table"),
2275
	     abfd, sec,
2276
	     (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2277
	  bfd_set_error (bfd_error_bad_value);
2278
	  return FALSE;
2279
	}
2280
      irela += bed->s->int_rels_per_ext_rel;
2281
      erela += shdr->sh_entsize;
2282
    }
2283
 
2284
  return TRUE;
2285
}
2286
 
2287
/* Read and swap the relocs for a section O.  They may have been
2288
   cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2289
   not NULL, they are used as buffers to read into.  They are known to
2290
   be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
2291
   the return value is allocated using either malloc or bfd_alloc,
2292
   according to the KEEP_MEMORY argument.  If O has two relocation
2293
   sections (both REL and RELA relocations), then the REL_HDR
2294
   relocations will appear first in INTERNAL_RELOCS, followed by the
2295
   RELA_HDR relocations.  */
2296
 
2297
Elf_Internal_Rela *
2298
_bfd_elf_link_read_relocs (bfd *abfd,
2299
			   asection *o,
2300
			   void *external_relocs,
2301
			   Elf_Internal_Rela *internal_relocs,
2302
			   bfd_boolean keep_memory)
2303
{
2304
  void *alloc1 = NULL;
2305
  Elf_Internal_Rela *alloc2 = NULL;
2306
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2307
  struct bfd_elf_section_data *esdo = elf_section_data (o);
2308
  Elf_Internal_Rela *internal_rela_relocs;
2309
 
2310
  if (esdo->relocs != NULL)
2311
    return esdo->relocs;
2312
 
2313
  if (o->reloc_count == 0)
2314
    return NULL;
2315
 
2316
  if (internal_relocs == NULL)
2317
    {
2318
      bfd_size_type size;
2319
 
2320
      size = o->reloc_count;
2321
      size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2322
      if (keep_memory)
2323
	internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2324
      else
2325
	internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2326
      if (internal_relocs == NULL)
2327
	goto error_return;
2328
    }
2329
 
2330
  if (external_relocs == NULL)
2331
    {
2332
      bfd_size_type size = 0;
2333
 
2334
      if (esdo->rel.hdr)
2335
	size += esdo->rel.hdr->sh_size;
2336
      if (esdo->rela.hdr)
2337
	size += esdo->rela.hdr->sh_size;
2338
 
2339
      alloc1 = bfd_malloc (size);
2340
      if (alloc1 == NULL)
2341
	goto error_return;
2342
      external_relocs = alloc1;
2343
    }
2344
 
2345
  internal_rela_relocs = internal_relocs;
2346
  if (esdo->rel.hdr)
2347
    {
2348
      if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2349
					      external_relocs,
2350
					      internal_relocs))
2351
	goto error_return;
2352
      external_relocs = (((bfd_byte *) external_relocs)
2353
			 + esdo->rel.hdr->sh_size);
2354
      internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2355
			       * bed->s->int_rels_per_ext_rel);
2356
    }
2357
 
2358
  if (esdo->rela.hdr
2359
      && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2360
					      external_relocs,
2361
					      internal_rela_relocs)))
2362
    goto error_return;
2363
 
2364
  /* Cache the results for next time, if we can.  */
2365
  if (keep_memory)
2366
    esdo->relocs = internal_relocs;
2367
 
2368
  if (alloc1 != NULL)
2369
    free (alloc1);
2370
 
2371
  /* Don't free alloc2, since if it was allocated we are passing it
2372
     back (under the name of internal_relocs).  */
2373
 
2374
  return internal_relocs;
2375
 
2376
 error_return:
2377
  if (alloc1 != NULL)
2378
    free (alloc1);
2379
  if (alloc2 != NULL)
2380
    {
2381
      if (keep_memory)
2382
	bfd_release (abfd, alloc2);
2383
      else
2384
	free (alloc2);
2385
    }
2386
  return NULL;
2387
}
2388
 
2389
/* Compute the size of, and allocate space for, REL_HDR which is the
2390
   section header for a section containing relocations for O.  */
2391
 
2392
static bfd_boolean
2393
_bfd_elf_link_size_reloc_section (bfd *abfd,
2394
				  struct bfd_elf_section_reloc_data *reldata)
2395
{
2396
  Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2397
 
2398
  /* That allows us to calculate the size of the section.  */
2399
  rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2400
 
2401
  /* The contents field must last into write_object_contents, so we
2402
     allocate it with bfd_alloc rather than malloc.  Also since we
2403
     cannot be sure that the contents will actually be filled in,
2404
     we zero the allocated space.  */
2405
  rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2406
  if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2407
    return FALSE;
2408
 
2409
  if (reldata->hashes == NULL && reldata->count)
2410
    {
2411
      struct elf_link_hash_entry **p;
2412
 
6324 serge 2413
      p = ((struct elf_link_hash_entry **)
2414
	   bfd_zmalloc (reldata->count * sizeof (*p)));
5197 serge 2415
      if (p == NULL)
2416
	return FALSE;
2417
 
2418
      reldata->hashes = p;
2419
    }
2420
 
2421
  return TRUE;
2422
}
2423
 
2424
/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2425
   originated from the section given by INPUT_REL_HDR) to the
2426
   OUTPUT_BFD.  */
2427
 
2428
bfd_boolean
2429
_bfd_elf_link_output_relocs (bfd *output_bfd,
2430
			     asection *input_section,
2431
			     Elf_Internal_Shdr *input_rel_hdr,
2432
			     Elf_Internal_Rela *internal_relocs,
2433
			     struct elf_link_hash_entry **rel_hash
2434
			       ATTRIBUTE_UNUSED)
2435
{
2436
  Elf_Internal_Rela *irela;
2437
  Elf_Internal_Rela *irelaend;
2438
  bfd_byte *erel;
2439
  struct bfd_elf_section_reloc_data *output_reldata;
2440
  asection *output_section;
2441
  const struct elf_backend_data *bed;
2442
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2443
  struct bfd_elf_section_data *esdo;
2444
 
2445
  output_section = input_section->output_section;
2446
 
2447
  bed = get_elf_backend_data (output_bfd);
2448
  esdo = elf_section_data (output_section);
2449
  if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2450
    {
2451
      output_reldata = &esdo->rel;
2452
      swap_out = bed->s->swap_reloc_out;
2453
    }
2454
  else if (esdo->rela.hdr
2455
	   && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2456
    {
2457
      output_reldata = &esdo->rela;
2458
      swap_out = bed->s->swap_reloca_out;
2459
    }
2460
  else
2461
    {
2462
      (*_bfd_error_handler)
2463
	(_("%B: relocation size mismatch in %B section %A"),
2464
	 output_bfd, input_section->owner, input_section);
2465
      bfd_set_error (bfd_error_wrong_format);
2466
      return FALSE;
2467
    }
2468
 
2469
  erel = output_reldata->hdr->contents;
2470
  erel += output_reldata->count * input_rel_hdr->sh_entsize;
2471
  irela = internal_relocs;
2472
  irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2473
		      * bed->s->int_rels_per_ext_rel);
2474
  while (irela < irelaend)
2475
    {
2476
      (*swap_out) (output_bfd, irela, erel);
2477
      irela += bed->s->int_rels_per_ext_rel;
2478
      erel += input_rel_hdr->sh_entsize;
2479
    }
2480
 
2481
  /* Bump the counter, so that we know where to add the next set of
2482
     relocations.  */
2483
  output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2484
 
2485
  return TRUE;
2486
}
2487
 
2488
/* Make weak undefined symbols in PIE dynamic.  */
2489
 
2490
bfd_boolean
2491
_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2492
				 struct elf_link_hash_entry *h)
2493
{
6324 serge 2494
  if (bfd_link_pie (info)
5197 serge 2495
      && h->dynindx == -1
2496
      && h->root.type == bfd_link_hash_undefweak)
2497
    return bfd_elf_link_record_dynamic_symbol (info, h);
2498
 
2499
  return TRUE;
2500
}
2501
 
2502
/* Fix up the flags for a symbol.  This handles various cases which
2503
   can only be fixed after all the input files are seen.  This is
2504
   currently called by both adjust_dynamic_symbol and
2505
   assign_sym_version, which is unnecessary but perhaps more robust in
2506
   the face of future changes.  */
2507
 
2508
static bfd_boolean
2509
_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2510
			   struct elf_info_failed *eif)
2511
{
2512
  const struct elf_backend_data *bed;
2513
 
2514
  /* If this symbol was mentioned in a non-ELF file, try to set
2515
     DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
2516
     permit a non-ELF file to correctly refer to a symbol defined in
2517
     an ELF dynamic object.  */
2518
  if (h->non_elf)
2519
    {
2520
      while (h->root.type == bfd_link_hash_indirect)
2521
	h = (struct elf_link_hash_entry *) h->root.u.i.link;
2522
 
2523
      if (h->root.type != bfd_link_hash_defined
2524
	  && h->root.type != bfd_link_hash_defweak)
2525
	{
2526
	  h->ref_regular = 1;
2527
	  h->ref_regular_nonweak = 1;
2528
	}
2529
      else
2530
	{
2531
	  if (h->root.u.def.section->owner != NULL
2532
	      && (bfd_get_flavour (h->root.u.def.section->owner)
2533
		  == bfd_target_elf_flavour))
2534
	    {
2535
	      h->ref_regular = 1;
2536
	      h->ref_regular_nonweak = 1;
2537
	    }
2538
	  else
2539
	    h->def_regular = 1;
2540
	}
2541
 
2542
      if (h->dynindx == -1
2543
	  && (h->def_dynamic
2544
	      || h->ref_dynamic))
2545
	{
2546
	  if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2547
	    {
2548
	      eif->failed = TRUE;
2549
	      return FALSE;
2550
	    }
2551
	}
2552
    }
2553
  else
2554
    {
2555
      /* Unfortunately, NON_ELF is only correct if the symbol
2556
	 was first seen in a non-ELF file.  Fortunately, if the symbol
2557
	 was first seen in an ELF file, we're probably OK unless the
2558
	 symbol was defined in a non-ELF file.  Catch that case here.
2559
	 FIXME: We're still in trouble if the symbol was first seen in
2560
	 a dynamic object, and then later in a non-ELF regular object.  */
2561
      if ((h->root.type == bfd_link_hash_defined
2562
	   || h->root.type == bfd_link_hash_defweak)
2563
	  && !h->def_regular
2564
	  && (h->root.u.def.section->owner != NULL
2565
	      ? (bfd_get_flavour (h->root.u.def.section->owner)
2566
		 != bfd_target_elf_flavour)
2567
	      : (bfd_is_abs_section (h->root.u.def.section)
2568
		 && !h->def_dynamic)))
2569
	h->def_regular = 1;
2570
    }
2571
 
2572
  /* Backend specific symbol fixup.  */
2573
  bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2574
  if (bed->elf_backend_fixup_symbol
2575
      && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2576
    return FALSE;
2577
 
2578
  /* If this is a final link, and the symbol was defined as a common
2579
     symbol in a regular object file, and there was no definition in
2580
     any dynamic object, then the linker will have allocated space for
2581
     the symbol in a common section but the DEF_REGULAR
2582
     flag will not have been set.  */
2583
  if (h->root.type == bfd_link_hash_defined
2584
      && !h->def_regular
2585
      && h->ref_regular
2586
      && !h->def_dynamic
2587
      && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2588
    h->def_regular = 1;
2589
 
2590
  /* If -Bsymbolic was used (which means to bind references to global
2591
     symbols to the definition within the shared object), and this
2592
     symbol was defined in a regular object, then it actually doesn't
2593
     need a PLT entry.  Likewise, if the symbol has non-default
2594
     visibility.  If the symbol has hidden or internal visibility, we
2595
     will force it local.  */
2596
  if (h->needs_plt
6324 serge 2597
      && bfd_link_pic (eif->info)
5197 serge 2598
      && is_elf_hash_table (eif->info->hash)
2599
      && (SYMBOLIC_BIND (eif->info, h)
2600
	  || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2601
      && h->def_regular)
2602
    {
2603
      bfd_boolean force_local;
2604
 
2605
      force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2606
		     || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2607
      (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2608
    }
2609
 
2610
  /* If a weak undefined symbol has non-default visibility, we also
2611
     hide it from the dynamic linker.  */
2612
  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2613
      && h->root.type == bfd_link_hash_undefweak)
2614
    (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2615
 
2616
  /* If this is a weak defined symbol in a dynamic object, and we know
2617
     the real definition in the dynamic object, copy interesting flags
2618
     over to the real definition.  */
2619
  if (h->u.weakdef != NULL)
2620
    {
2621
      /* If the real definition is defined by a regular object file,
2622
	 don't do anything special.  See the longer description in
2623
	 _bfd_elf_adjust_dynamic_symbol, below.  */
2624
      if (h->u.weakdef->def_regular)
2625
	h->u.weakdef = NULL;
2626
      else
2627
	{
2628
	  struct elf_link_hash_entry *weakdef = h->u.weakdef;
2629
 
2630
	  while (h->root.type == bfd_link_hash_indirect)
2631
	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
2632
 
2633
	  BFD_ASSERT (h->root.type == bfd_link_hash_defined
2634
		      || h->root.type == bfd_link_hash_defweak);
2635
	  BFD_ASSERT (weakdef->def_dynamic);
2636
	  BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2637
		      || weakdef->root.type == bfd_link_hash_defweak);
2638
	  (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2639
	}
2640
    }
2641
 
2642
  return TRUE;
2643
}
2644
 
2645
/* Make the backend pick a good value for a dynamic symbol.  This is
2646
   called via elf_link_hash_traverse, and also calls itself
2647
   recursively.  */
2648
 
2649
static bfd_boolean
2650
_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2651
{
2652
  struct elf_info_failed *eif = (struct elf_info_failed *) data;
2653
  bfd *dynobj;
2654
  const struct elf_backend_data *bed;
2655
 
2656
  if (! is_elf_hash_table (eif->info->hash))
2657
    return FALSE;
2658
 
2659
  /* Ignore indirect symbols.  These are added by the versioning code.  */
2660
  if (h->root.type == bfd_link_hash_indirect)
2661
    return TRUE;
2662
 
2663
  /* Fix the symbol flags.  */
2664
  if (! _bfd_elf_fix_symbol_flags (h, eif))
2665
    return FALSE;
2666
 
2667
  /* If this symbol does not require a PLT entry, and it is not
2668
     defined by a dynamic object, or is not referenced by a regular
2669
     object, ignore it.  We do have to handle a weak defined symbol,
2670
     even if no regular object refers to it, if we decided to add it
2671
     to the dynamic symbol table.  FIXME: Do we normally need to worry
2672
     about symbols which are defined by one dynamic object and
2673
     referenced by another one?  */
2674
  if (!h->needs_plt
2675
      && h->type != STT_GNU_IFUNC
2676
      && (h->def_regular
2677
	  || !h->def_dynamic
2678
	  || (!h->ref_regular
2679
	      && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2680
    {
2681
      h->plt = elf_hash_table (eif->info)->init_plt_offset;
2682
      return TRUE;
2683
    }
2684
 
2685
  /* If we've already adjusted this symbol, don't do it again.  This
2686
     can happen via a recursive call.  */
2687
  if (h->dynamic_adjusted)
2688
    return TRUE;
2689
 
2690
  /* Don't look at this symbol again.  Note that we must set this
2691
     after checking the above conditions, because we may look at a
2692
     symbol once, decide not to do anything, and then get called
2693
     recursively later after REF_REGULAR is set below.  */
2694
  h->dynamic_adjusted = 1;
2695
 
2696
  /* If this is a weak definition, and we know a real definition, and
2697
     the real symbol is not itself defined by a regular object file,
2698
     then get a good value for the real definition.  We handle the
2699
     real symbol first, for the convenience of the backend routine.
2700
 
2701
     Note that there is a confusing case here.  If the real definition
2702
     is defined by a regular object file, we don't get the real symbol
2703
     from the dynamic object, but we do get the weak symbol.  If the
2704
     processor backend uses a COPY reloc, then if some routine in the
2705
     dynamic object changes the real symbol, we will not see that
2706
     change in the corresponding weak symbol.  This is the way other
2707
     ELF linkers work as well, and seems to be a result of the shared
2708
     library model.
2709
 
2710
     I will clarify this issue.  Most SVR4 shared libraries define the
2711
     variable _timezone and define timezone as a weak synonym.  The
2712
     tzset call changes _timezone.  If you write
2713
       extern int timezone;
2714
       int _timezone = 5;
2715
       int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2716
     you might expect that, since timezone is a synonym for _timezone,
2717
     the same number will print both times.  However, if the processor
2718
     backend uses a COPY reloc, then actually timezone will be copied
2719
     into your process image, and, since you define _timezone
2720
     yourself, _timezone will not.  Thus timezone and _timezone will
2721
     wind up at different memory locations.  The tzset call will set
2722
     _timezone, leaving timezone unchanged.  */
2723
 
2724
  if (h->u.weakdef != NULL)
2725
    {
2726
      /* If we get to this point, there is an implicit reference to
2727
	 H->U.WEAKDEF by a regular object file via the weak symbol H.  */
2728
      h->u.weakdef->ref_regular = 1;
2729
 
2730
      /* Ensure that the backend adjust_dynamic_symbol function sees
2731
	 H->U.WEAKDEF before H by recursively calling ourselves.  */
2732
      if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2733
	return FALSE;
2734
    }
2735
 
2736
  /* If a symbol has no type and no size and does not require a PLT
2737
     entry, then we are probably about to do the wrong thing here: we
2738
     are probably going to create a COPY reloc for an empty object.
2739
     This case can arise when a shared object is built with assembly
2740
     code, and the assembly code fails to set the symbol type.  */
2741
  if (h->size == 0
2742
      && h->type == STT_NOTYPE
2743
      && !h->needs_plt)
2744
    (*_bfd_error_handler)
2745
      (_("warning: type and size of dynamic symbol `%s' are not defined"),
2746
       h->root.root.string);
2747
 
2748
  dynobj = elf_hash_table (eif->info)->dynobj;
2749
  bed = get_elf_backend_data (dynobj);
2750
 
2751
  if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2752
    {
2753
      eif->failed = TRUE;
2754
      return FALSE;
2755
    }
2756
 
2757
  return TRUE;
2758
}
2759
 
2760
/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2761
   DYNBSS.  */
2762
 
2763
bfd_boolean
6324 serge 2764
_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2765
			      struct elf_link_hash_entry *h,
5197 serge 2766
			      asection *dynbss)
2767
{
2768
  unsigned int power_of_two;
2769
  bfd_vma mask;
2770
  asection *sec = h->root.u.def.section;
2771
 
2772
  /* The section aligment of definition is the maximum alignment
2773
     requirement of symbols defined in the section.  Since we don't
2774
     know the symbol alignment requirement, we start with the
2775
     maximum alignment and check low bits of the symbol address
2776
     for the minimum alignment.  */
2777
  power_of_two = bfd_get_section_alignment (sec->owner, sec);
2778
  mask = ((bfd_vma) 1 << power_of_two) - 1;
2779
  while ((h->root.u.def.value & mask) != 0)
2780
    {
2781
       mask >>= 1;
2782
       --power_of_two;
2783
    }
2784
 
2785
  if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2786
						dynbss))
2787
    {
2788
      /* Adjust the section alignment if needed.  */
2789
      if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2790
				       power_of_two))
2791
	return FALSE;
2792
    }
2793
 
2794
  /* We make sure that the symbol will be aligned properly.  */
2795
  dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2796
 
2797
  /* Define the symbol as being at this point in DYNBSS.  */
2798
  h->root.u.def.section = dynbss;
2799
  h->root.u.def.value = dynbss->size;
2800
 
2801
  /* Increment the size of DYNBSS to make room for the symbol.  */
2802
  dynbss->size += h->size;
2803
 
6324 serge 2804
  /* No error if extern_protected_data is true.  */
2805
  if (h->protected_def
2806
      && (!info->extern_protected_data
2807
	  || (info->extern_protected_data < 0
2808
	      && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2809
    info->callbacks->einfo
2810
      (_("%P: copy reloc against protected `%T' is dangerous\n"),
2811
       h->root.root.string);
2812
 
5197 serge 2813
  return TRUE;
2814
}
2815
 
2816
/* Adjust all external symbols pointing into SEC_MERGE sections
2817
   to reflect the object merging within the sections.  */
2818
 
2819
static bfd_boolean
2820
_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2821
{
2822
  asection *sec;
2823
 
2824
  if ((h->root.type == bfd_link_hash_defined
2825
       || h->root.type == bfd_link_hash_defweak)
2826
      && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2827
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2828
    {
2829
      bfd *output_bfd = (bfd *) data;
2830
 
2831
      h->root.u.def.value =
2832
	_bfd_merged_section_offset (output_bfd,
2833
				    &h->root.u.def.section,
2834
				    elf_section_data (sec)->sec_info,
2835
				    h->root.u.def.value);
2836
    }
2837
 
2838
  return TRUE;
2839
}
2840
 
2841
/* Returns false if the symbol referred to by H should be considered
2842
   to resolve local to the current module, and true if it should be
2843
   considered to bind dynamically.  */
2844
 
2845
bfd_boolean
2846
_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2847
			   struct bfd_link_info *info,
2848
			   bfd_boolean not_local_protected)
2849
{
2850
  bfd_boolean binding_stays_local_p;
2851
  const struct elf_backend_data *bed;
2852
  struct elf_link_hash_table *hash_table;
2853
 
2854
  if (h == NULL)
2855
    return FALSE;
2856
 
2857
  while (h->root.type == bfd_link_hash_indirect
2858
	 || h->root.type == bfd_link_hash_warning)
2859
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
2860
 
2861
  /* If it was forced local, then clearly it's not dynamic.  */
2862
  if (h->dynindx == -1)
2863
    return FALSE;
2864
  if (h->forced_local)
2865
    return FALSE;
2866
 
2867
  /* Identify the cases where name binding rules say that a
2868
     visible symbol resolves locally.  */
6324 serge 2869
  binding_stays_local_p = (bfd_link_executable (info)
2870
			   || SYMBOLIC_BIND (info, h));
5197 serge 2871
 
2872
  switch (ELF_ST_VISIBILITY (h->other))
2873
    {
2874
    case STV_INTERNAL:
2875
    case STV_HIDDEN:
2876
      return FALSE;
2877
 
2878
    case STV_PROTECTED:
2879
      hash_table = elf_hash_table (info);
2880
      if (!is_elf_hash_table (hash_table))
2881
	return FALSE;
2882
 
2883
      bed = get_elf_backend_data (hash_table->dynobj);
2884
 
2885
      /* Proper resolution for function pointer equality may require
2886
	 that these symbols perhaps be resolved dynamically, even though
2887
	 we should be resolving them to the current module.  */
2888
      if (!not_local_protected || !bed->is_function_type (h->type))
2889
	binding_stays_local_p = TRUE;
2890
      break;
2891
 
2892
    default:
2893
      break;
2894
    }
2895
 
2896
  /* If it isn't defined locally, then clearly it's dynamic.  */
2897
  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2898
    return TRUE;
2899
 
2900
  /* Otherwise, the symbol is dynamic if binding rules don't tell
2901
     us that it remains local.  */
2902
  return !binding_stays_local_p;
2903
}
2904
 
2905
/* Return true if the symbol referred to by H should be considered
2906
   to resolve local to the current module, and false otherwise.  Differs
2907
   from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2908
   undefined symbols.  The two functions are virtually identical except
2909
   for the place where forced_local and dynindx == -1 are tested.  If
2910
   either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2911
   the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2912
   the symbol is local only for defined symbols.
2913
   It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2914
   !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2915
   treatment of undefined weak symbols.  For those that do not make
2916
   undefined weak symbols dynamic, both functions may return false.  */
2917
 
2918
bfd_boolean
2919
_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2920
			      struct bfd_link_info *info,
2921
			      bfd_boolean local_protected)
2922
{
2923
  const struct elf_backend_data *bed;
2924
  struct elf_link_hash_table *hash_table;
2925
 
2926
  /* If it's a local sym, of course we resolve locally.  */
2927
  if (h == NULL)
2928
    return TRUE;
2929
 
2930
  /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
2931
  if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2932
      || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2933
    return TRUE;
2934
 
2935
  /* Common symbols that become definitions don't get the DEF_REGULAR
2936
     flag set, so test it first, and don't bail out.  */
2937
  if (ELF_COMMON_DEF_P (h))
2938
    /* Do nothing.  */;
2939
  /* If we don't have a definition in a regular file, then we can't
2940
     resolve locally.  The sym is either undefined or dynamic.  */
2941
  else if (!h->def_regular)
2942
    return FALSE;
2943
 
2944
  /* Forced local symbols resolve locally.  */
2945
  if (h->forced_local)
2946
    return TRUE;
2947
 
2948
  /* As do non-dynamic symbols.  */
2949
  if (h->dynindx == -1)
2950
    return TRUE;
2951
 
2952
  /* At this point, we know the symbol is defined and dynamic.  In an
2953
     executable it must resolve locally, likewise when building symbolic
2954
     shared libraries.  */
6324 serge 2955
  if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
5197 serge 2956
    return TRUE;
2957
 
2958
  /* Now deal with defined dynamic symbols in shared libraries.  Ones
2959
     with default visibility might not resolve locally.  */
2960
  if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2961
    return FALSE;
2962
 
2963
  hash_table = elf_hash_table (info);
2964
  if (!is_elf_hash_table (hash_table))
2965
    return TRUE;
2966
 
2967
  bed = get_elf_backend_data (hash_table->dynobj);
2968
 
6324 serge 2969
  /* If extern_protected_data is false, STV_PROTECTED non-function
2970
     symbols are local.  */
2971
  if ((!info->extern_protected_data
2972
       || (info->extern_protected_data < 0
2973
	   && !bed->extern_protected_data))
2974
      && !bed->is_function_type (h->type))
5197 serge 2975
    return TRUE;
2976
 
2977
  /* Function pointer equality tests may require that STV_PROTECTED
2978
     symbols be treated as dynamic symbols.  If the address of a
2979
     function not defined in an executable is set to that function's
2980
     plt entry in the executable, then the address of the function in
2981
     a shared library must also be the plt entry in the executable.  */
2982
  return local_protected;
2983
}
2984
 
2985
/* Caches some TLS segment info, and ensures that the TLS segment vma is
2986
   aligned.  Returns the first TLS output section.  */
2987
 
2988
struct bfd_section *
2989
_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2990
{
2991
  struct bfd_section *sec, *tls;
2992
  unsigned int align = 0;
2993
 
2994
  for (sec = obfd->sections; sec != NULL; sec = sec->next)
2995
    if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2996
      break;
2997
  tls = sec;
2998
 
2999
  for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3000
    if (sec->alignment_power > align)
3001
      align = sec->alignment_power;
3002
 
3003
  elf_hash_table (info)->tls_sec = tls;
3004
 
3005
  /* Ensure the alignment of the first section is the largest alignment,
3006
     so that the tls segment starts aligned.  */
3007
  if (tls != NULL)
3008
    tls->alignment_power = align;
3009
 
3010
  return tls;
3011
}
3012
 
3013
/* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
3014
static bfd_boolean
3015
is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3016
				  Elf_Internal_Sym *sym)
3017
{
3018
  const struct elf_backend_data *bed;
3019
 
3020
  /* Local symbols do not count, but target specific ones might.  */
3021
  if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3022
      && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3023
    return FALSE;
3024
 
3025
  bed = get_elf_backend_data (abfd);
3026
  /* Function symbols do not count.  */
3027
  if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3028
    return FALSE;
3029
 
3030
  /* If the section is undefined, then so is the symbol.  */
3031
  if (sym->st_shndx == SHN_UNDEF)
3032
    return FALSE;
3033
 
3034
  /* If the symbol is defined in the common section, then
3035
     it is a common definition and so does not count.  */
3036
  if (bed->common_definition (sym))
3037
    return FALSE;
3038
 
3039
  /* If the symbol is in a target specific section then we
3040
     must rely upon the backend to tell us what it is.  */
3041
  if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3042
    /* FIXME - this function is not coded yet:
3043
 
3044
       return _bfd_is_global_symbol_definition (abfd, sym);
3045
 
3046
       Instead for now assume that the definition is not global,
3047
       Even if this is wrong, at least the linker will behave
3048
       in the same way that it used to do.  */
3049
    return FALSE;
3050
 
3051
  return TRUE;
3052
}
3053
 
3054
/* Search the symbol table of the archive element of the archive ABFD
3055
   whose archive map contains a mention of SYMDEF, and determine if
3056
   the symbol is defined in this element.  */
3057
static bfd_boolean
3058
elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3059
{
3060
  Elf_Internal_Shdr * hdr;
3061
  bfd_size_type symcount;
3062
  bfd_size_type extsymcount;
3063
  bfd_size_type extsymoff;
3064
  Elf_Internal_Sym *isymbuf;
3065
  Elf_Internal_Sym *isym;
3066
  Elf_Internal_Sym *isymend;
3067
  bfd_boolean result;
3068
 
3069
  abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3070
  if (abfd == NULL)
3071
    return FALSE;
3072
 
6324 serge 3073
  /* Return FALSE if the object has been claimed by plugin.  */
3074
  if (abfd->plugin_format == bfd_plugin_yes)
5197 serge 3075
    return FALSE;
3076
 
6324 serge 3077
  if (! bfd_check_format (abfd, bfd_object))
5197 serge 3078
    return FALSE;
3079
 
3080
  /* Select the appropriate symbol table.  */
3081
  if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3082
    hdr = &elf_tdata (abfd)->symtab_hdr;
3083
  else
3084
    hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3085
 
3086
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3087
 
3088
  /* The sh_info field of the symtab header tells us where the
3089
     external symbols start.  We don't care about the local symbols.  */
3090
  if (elf_bad_symtab (abfd))
3091
    {
3092
      extsymcount = symcount;
3093
      extsymoff = 0;
3094
    }
3095
  else
3096
    {
3097
      extsymcount = symcount - hdr->sh_info;
3098
      extsymoff = hdr->sh_info;
3099
    }
3100
 
3101
  if (extsymcount == 0)
3102
    return FALSE;
3103
 
3104
  /* Read in the symbol table.  */
3105
  isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3106
				  NULL, NULL, NULL);
3107
  if (isymbuf == NULL)
3108
    return FALSE;
3109
 
3110
  /* Scan the symbol table looking for SYMDEF.  */
3111
  result = FALSE;
3112
  for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3113
    {
3114
      const char *name;
3115
 
3116
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3117
					      isym->st_name);
3118
      if (name == NULL)
3119
	break;
3120
 
3121
      if (strcmp (name, symdef->name) == 0)
3122
	{
3123
	  result = is_global_data_symbol_definition (abfd, isym);
3124
	  break;
3125
	}
3126
    }
3127
 
3128
  free (isymbuf);
3129
 
3130
  return result;
3131
}
3132
 
3133
/* Add an entry to the .dynamic table.  */
3134
 
3135
bfd_boolean
3136
_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3137
			    bfd_vma tag,
3138
			    bfd_vma val)
3139
{
3140
  struct elf_link_hash_table *hash_table;
3141
  const struct elf_backend_data *bed;
3142
  asection *s;
3143
  bfd_size_type newsize;
3144
  bfd_byte *newcontents;
3145
  Elf_Internal_Dyn dyn;
3146
 
3147
  hash_table = elf_hash_table (info);
3148
  if (! is_elf_hash_table (hash_table))
3149
    return FALSE;
3150
 
3151
  bed = get_elf_backend_data (hash_table->dynobj);
3152
  s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3153
  BFD_ASSERT (s != NULL);
3154
 
3155
  newsize = s->size + bed->s->sizeof_dyn;
3156
  newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3157
  if (newcontents == NULL)
3158
    return FALSE;
3159
 
3160
  dyn.d_tag = tag;
3161
  dyn.d_un.d_val = val;
3162
  bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3163
 
3164
  s->size = newsize;
3165
  s->contents = newcontents;
3166
 
3167
  return TRUE;
3168
}
3169
 
3170
/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3171
   otherwise just check whether one already exists.  Returns -1 on error,
3172
   1 if a DT_NEEDED tag already exists, and 0 on success.  */
3173
 
3174
static int
3175
elf_add_dt_needed_tag (bfd *abfd,
3176
		       struct bfd_link_info *info,
3177
		       const char *soname,
3178
		       bfd_boolean do_it)
3179
{
3180
  struct elf_link_hash_table *hash_table;
3181
  bfd_size_type strindex;
3182
 
3183
  if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3184
    return -1;
3185
 
3186
  hash_table = elf_hash_table (info);
3187
  strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3188
  if (strindex == (bfd_size_type) -1)
3189
    return -1;
3190
 
3191
  if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3192
    {
3193
      asection *sdyn;
3194
      const struct elf_backend_data *bed;
3195
      bfd_byte *extdyn;
3196
 
3197
      bed = get_elf_backend_data (hash_table->dynobj);
3198
      sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3199
      if (sdyn != NULL)
3200
	for (extdyn = sdyn->contents;
3201
	     extdyn < sdyn->contents + sdyn->size;
3202
	     extdyn += bed->s->sizeof_dyn)
3203
	  {
3204
	    Elf_Internal_Dyn dyn;
3205
 
3206
	    bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3207
	    if (dyn.d_tag == DT_NEEDED
3208
		&& dyn.d_un.d_val == strindex)
3209
	      {
3210
		_bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3211
		return 1;
3212
	      }
3213
	  }
3214
    }
3215
 
3216
  if (do_it)
3217
    {
3218
      if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3219
	return -1;
3220
 
3221
      if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3222
	return -1;
3223
    }
3224
  else
3225
    /* We were just checking for existence of the tag.  */
3226
    _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3227
 
3228
  return 0;
3229
}
3230
 
3231
static bfd_boolean
3232
on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3233
{
3234
  for (; needed != NULL; needed = needed->next)
6324 serge 3235
    if ((elf_dyn_lib_class (needed->by) & DYN_AS_NEEDED) == 0
3236
	&& strcmp (soname, needed->name) == 0)
5197 serge 3237
      return TRUE;
3238
 
3239
  return FALSE;
3240
}
3241
 
3242
/* Sort symbol by value, section, and size.  */
3243
static int
3244
elf_sort_symbol (const void *arg1, const void *arg2)
3245
{
3246
  const struct elf_link_hash_entry *h1;
3247
  const struct elf_link_hash_entry *h2;
3248
  bfd_signed_vma vdiff;
3249
 
3250
  h1 = *(const struct elf_link_hash_entry **) arg1;
3251
  h2 = *(const struct elf_link_hash_entry **) arg2;
3252
  vdiff = h1->root.u.def.value - h2->root.u.def.value;
3253
  if (vdiff != 0)
3254
    return vdiff > 0 ? 1 : -1;
3255
  else
3256
    {
6324 serge 3257
      int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
5197 serge 3258
      if (sdiff != 0)
3259
	return sdiff > 0 ? 1 : -1;
3260
    }
3261
  vdiff = h1->size - h2->size;
3262
  return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3263
}
3264
 
3265
/* This function is used to adjust offsets into .dynstr for
3266
   dynamic symbols.  This is called via elf_link_hash_traverse.  */
3267
 
3268
static bfd_boolean
3269
elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3270
{
3271
  struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3272
 
3273
  if (h->dynindx != -1)
3274
    h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3275
  return TRUE;
3276
}
3277
 
3278
/* Assign string offsets in .dynstr, update all structures referencing
3279
   them.  */
3280
 
3281
static bfd_boolean
3282
elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3283
{
3284
  struct elf_link_hash_table *hash_table = elf_hash_table (info);
3285
  struct elf_link_local_dynamic_entry *entry;
3286
  struct elf_strtab_hash *dynstr = hash_table->dynstr;
3287
  bfd *dynobj = hash_table->dynobj;
3288
  asection *sdyn;
3289
  bfd_size_type size;
3290
  const struct elf_backend_data *bed;
3291
  bfd_byte *extdyn;
3292
 
3293
  _bfd_elf_strtab_finalize (dynstr);
3294
  size = _bfd_elf_strtab_size (dynstr);
3295
 
3296
  bed = get_elf_backend_data (dynobj);
3297
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3298
  BFD_ASSERT (sdyn != NULL);
3299
 
3300
  /* Update all .dynamic entries referencing .dynstr strings.  */
3301
  for (extdyn = sdyn->contents;
3302
       extdyn < sdyn->contents + sdyn->size;
3303
       extdyn += bed->s->sizeof_dyn)
3304
    {
3305
      Elf_Internal_Dyn dyn;
3306
 
3307
      bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3308
      switch (dyn.d_tag)
3309
	{
3310
	case DT_STRSZ:
3311
	  dyn.d_un.d_val = size;
3312
	  break;
3313
	case DT_NEEDED:
3314
	case DT_SONAME:
3315
	case DT_RPATH:
3316
	case DT_RUNPATH:
3317
	case DT_FILTER:
3318
	case DT_AUXILIARY:
3319
	case DT_AUDIT:
3320
	case DT_DEPAUDIT:
3321
	  dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3322
	  break;
3323
	default:
3324
	  continue;
3325
	}
3326
      bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3327
    }
3328
 
3329
  /* Now update local dynamic symbols.  */
3330
  for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3331
    entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3332
						  entry->isym.st_name);
3333
 
3334
  /* And the rest of dynamic symbols.  */
3335
  elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3336
 
3337
  /* Adjust version definitions.  */
3338
  if (elf_tdata (output_bfd)->cverdefs)
3339
    {
3340
      asection *s;
3341
      bfd_byte *p;
3342
      bfd_size_type i;
3343
      Elf_Internal_Verdef def;
3344
      Elf_Internal_Verdaux defaux;
3345
 
3346
      s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3347
      p = s->contents;
3348
      do
3349
	{
3350
	  _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3351
				   &def);
3352
	  p += sizeof (Elf_External_Verdef);
3353
	  if (def.vd_aux != sizeof (Elf_External_Verdef))
3354
	    continue;
3355
	  for (i = 0; i < def.vd_cnt; ++i)
3356
	    {
3357
	      _bfd_elf_swap_verdaux_in (output_bfd,
3358
					(Elf_External_Verdaux *) p, &defaux);
3359
	      defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3360
							defaux.vda_name);
3361
	      _bfd_elf_swap_verdaux_out (output_bfd,
3362
					 &defaux, (Elf_External_Verdaux *) p);
3363
	      p += sizeof (Elf_External_Verdaux);
3364
	    }
3365
	}
3366
      while (def.vd_next);
3367
    }
3368
 
3369
  /* Adjust version references.  */
3370
  if (elf_tdata (output_bfd)->verref)
3371
    {
3372
      asection *s;
3373
      bfd_byte *p;
3374
      bfd_size_type i;
3375
      Elf_Internal_Verneed need;
3376
      Elf_Internal_Vernaux needaux;
3377
 
3378
      s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3379
      p = s->contents;
3380
      do
3381
	{
3382
	  _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3383
				    &need);
3384
	  need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3385
	  _bfd_elf_swap_verneed_out (output_bfd, &need,
3386
				     (Elf_External_Verneed *) p);
3387
	  p += sizeof (Elf_External_Verneed);
3388
	  for (i = 0; i < need.vn_cnt; ++i)
3389
	    {
3390
	      _bfd_elf_swap_vernaux_in (output_bfd,
3391
					(Elf_External_Vernaux *) p, &needaux);
3392
	      needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3393
							 needaux.vna_name);
3394
	      _bfd_elf_swap_vernaux_out (output_bfd,
3395
					 &needaux,
3396
					 (Elf_External_Vernaux *) p);
3397
	      p += sizeof (Elf_External_Vernaux);
3398
	    }
3399
	}
3400
      while (need.vn_next);
3401
    }
3402
 
3403
  return TRUE;
3404
}
3405
 
3406
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3407
   The default is to only match when the INPUT and OUTPUT are exactly
3408
   the same target.  */
3409
 
3410
bfd_boolean
3411
_bfd_elf_default_relocs_compatible (const bfd_target *input,
3412
				    const bfd_target *output)
3413
{
3414
  return input == output;
3415
}
3416
 
3417
/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3418
   This version is used when different targets for the same architecture
3419
   are virtually identical.  */
3420
 
3421
bfd_boolean
3422
_bfd_elf_relocs_compatible (const bfd_target *input,
3423
			    const bfd_target *output)
3424
{
3425
  const struct elf_backend_data *obed, *ibed;
3426
 
3427
  if (input == output)
3428
    return TRUE;
3429
 
3430
  ibed = xvec_get_elf_backend_data (input);
3431
  obed = xvec_get_elf_backend_data (output);
3432
 
3433
  if (ibed->arch != obed->arch)
3434
    return FALSE;
3435
 
3436
  /* If both backends are using this function, deem them compatible.  */
3437
  return ibed->relocs_compatible == obed->relocs_compatible;
3438
}
3439
 
3440
/* Make a special call to the linker "notice" function to tell it that
3441
   we are about to handle an as-needed lib, or have finished
6324 serge 3442
   processing the lib.  */
5197 serge 3443
 
3444
bfd_boolean
3445
_bfd_elf_notice_as_needed (bfd *ibfd,
3446
			   struct bfd_link_info *info,
3447
			   enum notice_asneeded_action act)
3448
{
6324 serge 3449
  return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
5197 serge 3450
}
3451
 
3452
/* Add symbols from an ELF object file to the linker hash table.  */
3453
 
3454
static bfd_boolean
3455
elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3456
{
3457
  Elf_Internal_Ehdr *ehdr;
3458
  Elf_Internal_Shdr *hdr;
3459
  bfd_size_type symcount;
3460
  bfd_size_type extsymcount;
3461
  bfd_size_type extsymoff;
3462
  struct elf_link_hash_entry **sym_hash;
3463
  bfd_boolean dynamic;
3464
  Elf_External_Versym *extversym = NULL;
3465
  Elf_External_Versym *ever;
3466
  struct elf_link_hash_entry *weaks;
3467
  struct elf_link_hash_entry **nondeflt_vers = NULL;
3468
  bfd_size_type nondeflt_vers_cnt = 0;
3469
  Elf_Internal_Sym *isymbuf = NULL;
3470
  Elf_Internal_Sym *isym;
3471
  Elf_Internal_Sym *isymend;
3472
  const struct elf_backend_data *bed;
3473
  bfd_boolean add_needed;
3474
  struct elf_link_hash_table *htab;
3475
  bfd_size_type amt;
3476
  void *alloc_mark = NULL;
3477
  struct bfd_hash_entry **old_table = NULL;
3478
  unsigned int old_size = 0;
3479
  unsigned int old_count = 0;
3480
  void *old_tab = NULL;
3481
  void *old_ent;
3482
  struct bfd_link_hash_entry *old_undefs = NULL;
3483
  struct bfd_link_hash_entry *old_undefs_tail = NULL;
3484
  long old_dynsymcount = 0;
3485
  bfd_size_type old_dynstr_size = 0;
3486
  size_t tabsize = 0;
3487
  asection *s;
6324 serge 3488
  bfd_boolean just_syms;
5197 serge 3489
 
3490
  htab = elf_hash_table (info);
3491
  bed = get_elf_backend_data (abfd);
3492
 
3493
  if ((abfd->flags & DYNAMIC) == 0)
3494
    dynamic = FALSE;
3495
  else
3496
    {
3497
      dynamic = TRUE;
3498
 
3499
      /* You can't use -r against a dynamic object.  Also, there's no
3500
	 hope of using a dynamic object which does not exactly match
3501
	 the format of the output file.  */
6324 serge 3502
      if (bfd_link_relocatable (info)
5197 serge 3503
	  || !is_elf_hash_table (htab)
3504
	  || info->output_bfd->xvec != abfd->xvec)
3505
	{
6324 serge 3506
	  if (bfd_link_relocatable (info))
5197 serge 3507
	    bfd_set_error (bfd_error_invalid_operation);
3508
	  else
3509
	    bfd_set_error (bfd_error_wrong_format);
3510
	  goto error_return;
3511
	}
3512
    }
3513
 
3514
  ehdr = elf_elfheader (abfd);
3515
  if (info->warn_alternate_em
3516
      && bed->elf_machine_code != ehdr->e_machine
3517
      && ((bed->elf_machine_alt1 != 0
3518
	   && ehdr->e_machine == bed->elf_machine_alt1)
3519
	  || (bed->elf_machine_alt2 != 0
3520
	      && ehdr->e_machine == bed->elf_machine_alt2)))
3521
    info->callbacks->einfo
3522
      (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3523
       ehdr->e_machine, abfd, bed->elf_machine_code);
3524
 
3525
  /* As a GNU extension, any input sections which are named
3526
     .gnu.warning.SYMBOL are treated as warning symbols for the given
3527
     symbol.  This differs from .gnu.warning sections, which generate
3528
     warnings when they are included in an output file.  */
3529
  /* PR 12761: Also generate this warning when building shared libraries.  */
3530
  for (s = abfd->sections; s != NULL; s = s->next)
3531
    {
3532
      const char *name;
3533
 
3534
      name = bfd_get_section_name (abfd, s);
3535
      if (CONST_STRNEQ (name, ".gnu.warning."))
3536
	{
3537
	  char *msg;
3538
	  bfd_size_type sz;
3539
 
3540
	  name += sizeof ".gnu.warning." - 1;
3541
 
3542
	  /* If this is a shared object, then look up the symbol
3543
	     in the hash table.  If it is there, and it is already
3544
	     been defined, then we will not be using the entry
3545
	     from this shared object, so we don't need to warn.
3546
	     FIXME: If we see the definition in a regular object
3547
	     later on, we will warn, but we shouldn't.  The only
3548
	     fix is to keep track of what warnings we are supposed
3549
	     to emit, and then handle them all at the end of the
3550
	     link.  */
3551
	  if (dynamic)
3552
	    {
3553
	      struct elf_link_hash_entry *h;
3554
 
3555
	      h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3556
 
3557
	      /* FIXME: What about bfd_link_hash_common?  */
3558
	      if (h != NULL
3559
		  && (h->root.type == bfd_link_hash_defined
3560
		      || h->root.type == bfd_link_hash_defweak))
3561
		continue;
3562
	    }
3563
 
3564
	  sz = s->size;
3565
	  msg = (char *) bfd_alloc (abfd, sz + 1);
3566
	  if (msg == NULL)
3567
	    goto error_return;
3568
 
3569
	  if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3570
	    goto error_return;
3571
 
3572
	  msg[sz] = '\0';
3573
 
3574
	  if (! (_bfd_generic_link_add_one_symbol
3575
		 (info, abfd, name, BSF_WARNING, s, 0, msg,
3576
		  FALSE, bed->collect, NULL)))
3577
	    goto error_return;
3578
 
6324 serge 3579
	  if (bfd_link_executable (info))
5197 serge 3580
	    {
3581
	      /* Clobber the section size so that the warning does
3582
		 not get copied into the output file.  */
3583
	      s->size = 0;
3584
 
3585
	      /* Also set SEC_EXCLUDE, so that symbols defined in
3586
		 the warning section don't get copied to the output.  */
3587
	      s->flags |= SEC_EXCLUDE;
3588
	    }
3589
	}
3590
    }
3591
 
6324 serge 3592
  just_syms = ((s = abfd->sections) != NULL
3593
	       && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3594
 
5197 serge 3595
  add_needed = TRUE;
3596
  if (! dynamic)
3597
    {
3598
      /* If we are creating a shared library, create all the dynamic
3599
	 sections immediately.  We need to attach them to something,
3600
	 so we attach them to this BFD, provided it is the right
6324 serge 3601
	 format and is not from ld --just-symbols.  FIXME: If there
3602
	 are no input BFD's of the same format as the output, we can't
3603
	 make a shared library.  */
3604
      if (!just_syms
3605
	  && bfd_link_pic (info)
5197 serge 3606
	  && is_elf_hash_table (htab)
3607
	  && info->output_bfd->xvec == abfd->xvec
3608
	  && !htab->dynamic_sections_created)
3609
	{
3610
	  if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3611
	    goto error_return;
3612
	}
3613
    }
3614
  else if (!is_elf_hash_table (htab))
3615
    goto error_return;
3616
  else
3617
    {
3618
      const char *soname = NULL;
3619
      char *audit = NULL;
3620
      struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3621
      int ret;
3622
 
3623
      /* ld --just-symbols and dynamic objects don't mix very well.
3624
	 ld shouldn't allow it.  */
6324 serge 3625
      if (just_syms)
5197 serge 3626
	abort ();
3627
 
3628
      /* If this dynamic lib was specified on the command line with
3629
	 --as-needed in effect, then we don't want to add a DT_NEEDED
3630
	 tag unless the lib is actually used.  Similary for libs brought
3631
	 in by another lib's DT_NEEDED.  When --no-add-needed is used
3632
	 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3633
	 any dynamic library in DT_NEEDED tags in the dynamic lib at
3634
	 all.  */
3635
      add_needed = (elf_dyn_lib_class (abfd)
3636
		    & (DYN_AS_NEEDED | DYN_DT_NEEDED
3637
		       | DYN_NO_NEEDED)) == 0;
3638
 
3639
      s = bfd_get_section_by_name (abfd, ".dynamic");
3640
      if (s != NULL)
3641
	{
3642
	  bfd_byte *dynbuf;
3643
	  bfd_byte *extdyn;
3644
	  unsigned int elfsec;
3645
	  unsigned long shlink;
3646
 
3647
	  if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3648
	    {
3649
error_free_dyn:
3650
	      free (dynbuf);
3651
	      goto error_return;
3652
	    }
3653
 
3654
	  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3655
	  if (elfsec == SHN_BAD)
3656
	    goto error_free_dyn;
3657
	  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3658
 
3659
	  for (extdyn = dynbuf;
3660
	       extdyn < dynbuf + s->size;
3661
	       extdyn += bed->s->sizeof_dyn)
3662
	    {
3663
	      Elf_Internal_Dyn dyn;
3664
 
3665
	      bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3666
	      if (dyn.d_tag == DT_SONAME)
3667
		{
3668
		  unsigned int tagv = dyn.d_un.d_val;
3669
		  soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3670
		  if (soname == NULL)
3671
		    goto error_free_dyn;
3672
		}
3673
	      if (dyn.d_tag == DT_NEEDED)
3674
		{
3675
		  struct bfd_link_needed_list *n, **pn;
3676
		  char *fnm, *anm;
3677
		  unsigned int tagv = dyn.d_un.d_val;
3678
 
3679
		  amt = sizeof (struct bfd_link_needed_list);
3680
		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3681
		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3682
		  if (n == NULL || fnm == NULL)
3683
		    goto error_free_dyn;
3684
		  amt = strlen (fnm) + 1;
3685
		  anm = (char *) bfd_alloc (abfd, amt);
3686
		  if (anm == NULL)
3687
		    goto error_free_dyn;
3688
		  memcpy (anm, fnm, amt);
3689
		  n->name = anm;
3690
		  n->by = abfd;
3691
		  n->next = NULL;
3692
		  for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3693
		    ;
3694
		  *pn = n;
3695
		}
3696
	      if (dyn.d_tag == DT_RUNPATH)
3697
		{
3698
		  struct bfd_link_needed_list *n, **pn;
3699
		  char *fnm, *anm;
3700
		  unsigned int tagv = dyn.d_un.d_val;
3701
 
3702
		  amt = sizeof (struct bfd_link_needed_list);
3703
		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3704
		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3705
		  if (n == NULL || fnm == NULL)
3706
		    goto error_free_dyn;
3707
		  amt = strlen (fnm) + 1;
3708
		  anm = (char *) bfd_alloc (abfd, amt);
3709
		  if (anm == NULL)
3710
		    goto error_free_dyn;
3711
		  memcpy (anm, fnm, amt);
3712
		  n->name = anm;
3713
		  n->by = abfd;
3714
		  n->next = NULL;
3715
		  for (pn = & runpath;
3716
		       *pn != NULL;
3717
		       pn = &(*pn)->next)
3718
		    ;
3719
		  *pn = n;
3720
		}
3721
	      /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
3722
	      if (!runpath && dyn.d_tag == DT_RPATH)
3723
		{
3724
		  struct bfd_link_needed_list *n, **pn;
3725
		  char *fnm, *anm;
3726
		  unsigned int tagv = dyn.d_un.d_val;
3727
 
3728
		  amt = sizeof (struct bfd_link_needed_list);
3729
		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3730
		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3731
		  if (n == NULL || fnm == NULL)
3732
		    goto error_free_dyn;
3733
		  amt = strlen (fnm) + 1;
3734
		  anm = (char *) bfd_alloc (abfd, amt);
3735
		  if (anm == NULL)
3736
		    goto error_free_dyn;
3737
		  memcpy (anm, fnm, amt);
3738
		  n->name = anm;
3739
		  n->by = abfd;
3740
		  n->next = NULL;
3741
		  for (pn = & rpath;
3742
		       *pn != NULL;
3743
		       pn = &(*pn)->next)
3744
		    ;
3745
		  *pn = n;
3746
		}
3747
	      if (dyn.d_tag == DT_AUDIT)
3748
		{
3749
		  unsigned int tagv = dyn.d_un.d_val;
3750
		  audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3751
		}
3752
	    }
3753
 
3754
	  free (dynbuf);
3755
	}
3756
 
3757
      /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
3758
	 frees all more recently bfd_alloc'd blocks as well.  */
3759
      if (runpath)
3760
	rpath = runpath;
3761
 
3762
      if (rpath)
3763
	{
3764
	  struct bfd_link_needed_list **pn;
3765
	  for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3766
	    ;
3767
	  *pn = rpath;
3768
	}
3769
 
3770
      /* We do not want to include any of the sections in a dynamic
3771
	 object in the output file.  We hack by simply clobbering the
3772
	 list of sections in the BFD.  This could be handled more
3773
	 cleanly by, say, a new section flag; the existing
3774
	 SEC_NEVER_LOAD flag is not the one we want, because that one
3775
	 still implies that the section takes up space in the output
3776
	 file.  */
3777
      bfd_section_list_clear (abfd);
3778
 
3779
      /* Find the name to use in a DT_NEEDED entry that refers to this
3780
	 object.  If the object has a DT_SONAME entry, we use it.
3781
	 Otherwise, if the generic linker stuck something in
3782
	 elf_dt_name, we use that.  Otherwise, we just use the file
3783
	 name.  */
3784
      if (soname == NULL || *soname == '\0')
3785
	{
3786
	  soname = elf_dt_name (abfd);
3787
	  if (soname == NULL || *soname == '\0')
3788
	    soname = bfd_get_filename (abfd);
3789
	}
3790
 
3791
      /* Save the SONAME because sometimes the linker emulation code
3792
	 will need to know it.  */
3793
      elf_dt_name (abfd) = soname;
3794
 
3795
      ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3796
      if (ret < 0)
3797
	goto error_return;
3798
 
3799
      /* If we have already included this dynamic object in the
3800
	 link, just ignore it.  There is no reason to include a
3801
	 particular dynamic object more than once.  */
3802
      if (ret > 0)
3803
	return TRUE;
3804
 
3805
      /* Save the DT_AUDIT entry for the linker emulation code. */
3806
      elf_dt_audit (abfd) = audit;
3807
    }
3808
 
3809
  /* If this is a dynamic object, we always link against the .dynsym
3810
     symbol table, not the .symtab symbol table.  The dynamic linker
3811
     will only see the .dynsym symbol table, so there is no reason to
3812
     look at .symtab for a dynamic object.  */
3813
 
3814
  if (! dynamic || elf_dynsymtab (abfd) == 0)
3815
    hdr = &elf_tdata (abfd)->symtab_hdr;
3816
  else
3817
    hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3818
 
3819
  symcount = hdr->sh_size / bed->s->sizeof_sym;
3820
 
3821
  /* The sh_info field of the symtab header tells us where the
3822
     external symbols start.  We don't care about the local symbols at
3823
     this point.  */
3824
  if (elf_bad_symtab (abfd))
3825
    {
3826
      extsymcount = symcount;
3827
      extsymoff = 0;
3828
    }
3829
  else
3830
    {
3831
      extsymcount = symcount - hdr->sh_info;
3832
      extsymoff = hdr->sh_info;
3833
    }
3834
 
3835
  sym_hash = elf_sym_hashes (abfd);
3836
  if (extsymcount != 0)
3837
    {
3838
      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3839
				      NULL, NULL, NULL);
3840
      if (isymbuf == NULL)
3841
	goto error_return;
3842
 
3843
      if (sym_hash == NULL)
3844
	{
3845
	  /* We store a pointer to the hash table entry for each
3846
	     external symbol.  */
3847
	  amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3848
	  sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3849
	  if (sym_hash == NULL)
3850
	    goto error_free_sym;
3851
	  elf_sym_hashes (abfd) = sym_hash;
3852
	}
3853
    }
3854
 
3855
  if (dynamic)
3856
    {
3857
      /* Read in any version definitions.  */
3858
      if (!_bfd_elf_slurp_version_tables (abfd,
3859
					  info->default_imported_symver))
3860
	goto error_free_sym;
3861
 
3862
      /* Read in the symbol versions, but don't bother to convert them
3863
	 to internal format.  */
3864
      if (elf_dynversym (abfd) != 0)
3865
	{
3866
	  Elf_Internal_Shdr *versymhdr;
3867
 
3868
	  versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3869
	  extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
3870
	  if (extversym == NULL)
3871
	    goto error_free_sym;
3872
	  amt = versymhdr->sh_size;
3873
	  if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3874
	      || bfd_bread (extversym, amt, abfd) != amt)
3875
	    goto error_free_vers;
3876
	}
3877
    }
3878
 
3879
  /* If we are loading an as-needed shared lib, save the symbol table
3880
     state before we start adding symbols.  If the lib turns out
3881
     to be unneeded, restore the state.  */
3882
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3883
    {
3884
      unsigned int i;
3885
      size_t entsize;
3886
 
3887
      for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3888
	{
3889
	  struct bfd_hash_entry *p;
3890
	  struct elf_link_hash_entry *h;
3891
 
3892
	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3893
	    {
3894
	      h = (struct elf_link_hash_entry *) p;
3895
	      entsize += htab->root.table.entsize;
3896
	      if (h->root.type == bfd_link_hash_warning)
3897
		entsize += htab->root.table.entsize;
3898
	    }
3899
	}
3900
 
3901
      tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3902
      old_tab = bfd_malloc (tabsize + entsize);
3903
      if (old_tab == NULL)
3904
	goto error_free_vers;
3905
 
3906
      /* Remember the current objalloc pointer, so that all mem for
3907
	 symbols added can later be reclaimed.  */
3908
      alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3909
      if (alloc_mark == NULL)
3910
	goto error_free_vers;
3911
 
3912
      /* Make a special call to the linker "notice" function to
3913
	 tell it that we are about to handle an as-needed lib.  */
3914
      if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
3915
	goto error_free_vers;
3916
 
3917
      /* Clone the symbol table.  Remember some pointers into the
3918
	 symbol table, and dynamic symbol count.  */
3919
      old_ent = (char *) old_tab + tabsize;
3920
      memcpy (old_tab, htab->root.table.table, tabsize);
3921
      old_undefs = htab->root.undefs;
3922
      old_undefs_tail = htab->root.undefs_tail;
3923
      old_table = htab->root.table.table;
3924
      old_size = htab->root.table.size;
3925
      old_count = htab->root.table.count;
3926
      old_dynsymcount = htab->dynsymcount;
3927
      old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
3928
 
3929
      for (i = 0; i < htab->root.table.size; i++)
3930
	{
3931
	  struct bfd_hash_entry *p;
3932
	  struct elf_link_hash_entry *h;
3933
 
3934
	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3935
	    {
3936
	      memcpy (old_ent, p, htab->root.table.entsize);
3937
	      old_ent = (char *) old_ent + htab->root.table.entsize;
3938
	      h = (struct elf_link_hash_entry *) p;
3939
	      if (h->root.type == bfd_link_hash_warning)
3940
		{
3941
		  memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3942
		  old_ent = (char *) old_ent + htab->root.table.entsize;
3943
		}
3944
	    }
3945
	}
3946
    }
3947
 
3948
  weaks = NULL;
3949
  ever = extversym != NULL ? extversym + extsymoff : NULL;
3950
  for (isym = isymbuf, isymend = isymbuf + extsymcount;
3951
       isym < isymend;
3952
       isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3953
    {
3954
      int bind;
3955
      bfd_vma value;
3956
      asection *sec, *new_sec;
3957
      flagword flags;
3958
      const char *name;
3959
      struct elf_link_hash_entry *h;
3960
      struct elf_link_hash_entry *hi;
3961
      bfd_boolean definition;
3962
      bfd_boolean size_change_ok;
3963
      bfd_boolean type_change_ok;
3964
      bfd_boolean new_weakdef;
3965
      bfd_boolean new_weak;
3966
      bfd_boolean old_weak;
3967
      bfd_boolean override;
3968
      bfd_boolean common;
3969
      unsigned int old_alignment;
3970
      bfd *old_bfd;
6324 serge 3971
      bfd_boolean matched;
5197 serge 3972
 
3973
      override = FALSE;
3974
 
3975
      flags = BSF_NO_FLAGS;
3976
      sec = NULL;
3977
      value = isym->st_value;
3978
      common = bed->common_definition (isym);
3979
 
3980
      bind = ELF_ST_BIND (isym->st_info);
3981
      switch (bind)
3982
	{
3983
	case STB_LOCAL:
3984
	  /* This should be impossible, since ELF requires that all
3985
	     global symbols follow all local symbols, and that sh_info
3986
	     point to the first global symbol.  Unfortunately, Irix 5
3987
	     screws this up.  */
3988
	  continue;
3989
 
3990
	case STB_GLOBAL:
3991
	  if (isym->st_shndx != SHN_UNDEF && !common)
3992
	    flags = BSF_GLOBAL;
3993
	  break;
3994
 
3995
	case STB_WEAK:
3996
	  flags = BSF_WEAK;
3997
	  break;
3998
 
3999
	case STB_GNU_UNIQUE:
4000
	  flags = BSF_GNU_UNIQUE;
4001
	  break;
4002
 
4003
	default:
4004
	  /* Leave it up to the processor backend.  */
4005
	  break;
4006
	}
4007
 
4008
      if (isym->st_shndx == SHN_UNDEF)
4009
	sec = bfd_und_section_ptr;
4010
      else if (isym->st_shndx == SHN_ABS)
4011
	sec = bfd_abs_section_ptr;
4012
      else if (isym->st_shndx == SHN_COMMON)
4013
	{
4014
	  sec = bfd_com_section_ptr;
4015
	  /* What ELF calls the size we call the value.  What ELF
4016
	     calls the value we call the alignment.  */
4017
	  value = isym->st_size;
4018
	}
4019
      else
4020
	{
4021
	  sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4022
	  if (sec == NULL)
4023
	    sec = bfd_abs_section_ptr;
4024
	  else if (discarded_section (sec))
4025
	    {
4026
	      /* Symbols from discarded section are undefined.  We keep
4027
		 its visibility.  */
4028
	      sec = bfd_und_section_ptr;
4029
	      isym->st_shndx = SHN_UNDEF;
4030
	    }
4031
	  else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4032
	    value -= sec->vma;
4033
	}
4034
 
4035
      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4036
					      isym->st_name);
4037
      if (name == NULL)
4038
	goto error_free_vers;
4039
 
4040
      if (isym->st_shndx == SHN_COMMON
4041
	  && (abfd->flags & BFD_PLUGIN) != 0)
4042
	{
4043
	  asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4044
 
4045
	  if (xc == NULL)
4046
	    {
4047
	      flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4048
				 | SEC_EXCLUDE);
4049
	      xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4050
	      if (xc == NULL)
4051
		goto error_free_vers;
4052
	    }
4053
	  sec = xc;
4054
	}
4055
      else if (isym->st_shndx == SHN_COMMON
4056
	       && ELF_ST_TYPE (isym->st_info) == STT_TLS
6324 serge 4057
	       && !bfd_link_relocatable (info))
5197 serge 4058
	{
4059
	  asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4060
 
4061
	  if (tcomm == NULL)
4062
	    {
4063
	      flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4064
				 | SEC_LINKER_CREATED);
4065
	      tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4066
	      if (tcomm == NULL)
4067
		goto error_free_vers;
4068
	    }
4069
	  sec = tcomm;
4070
	}
4071
      else if (bed->elf_add_symbol_hook)
4072
	{
4073
	  if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4074
					     &sec, &value))
4075
	    goto error_free_vers;
4076
 
4077
	  /* The hook function sets the name to NULL if this symbol
4078
	     should be skipped for some reason.  */
4079
	  if (name == NULL)
4080
	    continue;
4081
	}
4082
 
4083
      /* Sanity check that all possibilities were handled.  */
4084
      if (sec == NULL)
4085
	{
4086
	  bfd_set_error (bfd_error_bad_value);
4087
	  goto error_free_vers;
4088
	}
4089
 
4090
      /* Silently discard TLS symbols from --just-syms.  There's
4091
	 no way to combine a static TLS block with a new TLS block
4092
	 for this executable.  */
4093
      if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4094
	  && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4095
	continue;
4096
 
4097
      if (bfd_is_und_section (sec)
4098
	  || bfd_is_com_section (sec))
4099
	definition = FALSE;
4100
      else
4101
	definition = TRUE;
4102
 
4103
      size_change_ok = FALSE;
4104
      type_change_ok = bed->type_change_ok;
4105
      old_weak = FALSE;
6324 serge 4106
      matched = FALSE;
5197 serge 4107
      old_alignment = 0;
4108
      old_bfd = NULL;
4109
      new_sec = sec;
4110
 
4111
      if (is_elf_hash_table (htab))
4112
	{
4113
	  Elf_Internal_Versym iver;
4114
	  unsigned int vernum = 0;
4115
	  bfd_boolean skip;
4116
 
4117
	  if (ever == NULL)
4118
	    {
4119
	      if (info->default_imported_symver)
4120
		/* Use the default symbol version created earlier.  */
4121
		iver.vs_vers = elf_tdata (abfd)->cverdefs;
4122
	      else
4123
		iver.vs_vers = 0;
4124
	    }
4125
	  else
4126
	    _bfd_elf_swap_versym_in (abfd, ever, &iver);
4127
 
4128
	  vernum = iver.vs_vers & VERSYM_VERSION;
4129
 
4130
	  /* If this is a hidden symbol, or if it is not version
4131
	     1, we append the version name to the symbol name.
4132
	     However, we do not modify a non-hidden absolute symbol
4133
	     if it is not a function, because it might be the version
4134
	     symbol itself.  FIXME: What if it isn't?  */
4135
	  if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4136
	      || (vernum > 1
4137
		  && (!bfd_is_abs_section (sec)
4138
		      || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4139
	    {
4140
	      const char *verstr;
4141
	      size_t namelen, verlen, newlen;
4142
	      char *newname, *p;
4143
 
4144
	      if (isym->st_shndx != SHN_UNDEF)
4145
		{
4146
		  if (vernum > elf_tdata (abfd)->cverdefs)
4147
		    verstr = NULL;
4148
		  else if (vernum > 1)
4149
		    verstr =
4150
		      elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4151
		  else
4152
		    verstr = "";
4153
 
4154
		  if (verstr == NULL)
4155
		    {
4156
		      (*_bfd_error_handler)
4157
			(_("%B: %s: invalid version %u (max %d)"),
4158
			 abfd, name, vernum,
4159
			 elf_tdata (abfd)->cverdefs);
4160
		      bfd_set_error (bfd_error_bad_value);
4161
		      goto error_free_vers;
4162
		    }
4163
		}
4164
	      else
4165
		{
4166
		  /* We cannot simply test for the number of
4167
		     entries in the VERNEED section since the
4168
		     numbers for the needed versions do not start
4169
		     at 0.  */
4170
		  Elf_Internal_Verneed *t;
4171
 
4172
		  verstr = NULL;
4173
		  for (t = elf_tdata (abfd)->verref;
4174
		       t != NULL;
4175
		       t = t->vn_nextref)
4176
		    {
4177
		      Elf_Internal_Vernaux *a;
4178
 
4179
		      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4180
			{
4181
			  if (a->vna_other == vernum)
4182
			    {
4183
			      verstr = a->vna_nodename;
4184
			      break;
4185
			    }
4186
			}
4187
		      if (a != NULL)
4188
			break;
4189
		    }
4190
		  if (verstr == NULL)
4191
		    {
4192
		      (*_bfd_error_handler)
4193
			(_("%B: %s: invalid needed version %d"),
4194
			 abfd, name, vernum);
4195
		      bfd_set_error (bfd_error_bad_value);
4196
		      goto error_free_vers;
4197
		    }
4198
		}
4199
 
4200
	      namelen = strlen (name);
4201
	      verlen = strlen (verstr);
4202
	      newlen = namelen + verlen + 2;
4203
	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4204
		  && isym->st_shndx != SHN_UNDEF)
4205
		++newlen;
4206
 
4207
	      newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4208
	      if (newname == NULL)
4209
		goto error_free_vers;
4210
	      memcpy (newname, name, namelen);
4211
	      p = newname + namelen;
4212
	      *p++ = ELF_VER_CHR;
4213
	      /* If this is a defined non-hidden version symbol,
4214
		 we add another @ to the name.  This indicates the
4215
		 default version of the symbol.  */
4216
	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4217
		  && isym->st_shndx != SHN_UNDEF)
4218
		*p++ = ELF_VER_CHR;
4219
	      memcpy (p, verstr, verlen + 1);
4220
 
4221
	      name = newname;
4222
	    }
4223
 
6324 serge 4224
	  /* If this symbol has default visibility and the user has
4225
	     requested we not re-export it, then mark it as hidden.  */
4226
	  if (!bfd_is_und_section (sec)
4227
	      && !dynamic
4228
	      && abfd->no_export
4229
	      && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4230
	    isym->st_other = (STV_HIDDEN
4231
			      | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4232
 
5197 serge 4233
	  if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4234
				      sym_hash, &old_bfd, &old_weak,
4235
				      &old_alignment, &skip, &override,
6324 serge 4236
				      &type_change_ok, &size_change_ok,
4237
				      &matched))
5197 serge 4238
	    goto error_free_vers;
4239
 
4240
	  if (skip)
4241
	    continue;
4242
 
6324 serge 4243
	  /* Override a definition only if the new symbol matches the
4244
	     existing one.  */
4245
	  if (override && matched)
5197 serge 4246
	    definition = FALSE;
4247
 
4248
	  h = *sym_hash;
4249
	  while (h->root.type == bfd_link_hash_indirect
4250
		 || h->root.type == bfd_link_hash_warning)
4251
	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
4252
 
4253
	  if (elf_tdata (abfd)->verdef != NULL
4254
	      && vernum > 1
4255
	      && definition)
4256
	    h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4257
	}
4258
 
4259
      if (! (_bfd_generic_link_add_one_symbol
4260
	     (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4261
	      (struct bfd_link_hash_entry **) sym_hash)))
4262
	goto error_free_vers;
4263
 
4264
      h = *sym_hash;
4265
      /* We need to make sure that indirect symbol dynamic flags are
4266
	 updated.  */
4267
      hi = h;
4268
      while (h->root.type == bfd_link_hash_indirect
4269
	     || h->root.type == bfd_link_hash_warning)
4270
	h = (struct elf_link_hash_entry *) h->root.u.i.link;
4271
 
4272
      *sym_hash = h;
4273
 
4274
      new_weak = (flags & BSF_WEAK) != 0;
4275
      new_weakdef = FALSE;
4276
      if (dynamic
4277
	  && definition
4278
	  && new_weak
4279
	  && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4280
	  && is_elf_hash_table (htab)
4281
	  && h->u.weakdef == NULL)
4282
	{
4283
	  /* Keep a list of all weak defined non function symbols from
4284
	     a dynamic object, using the weakdef field.  Later in this
4285
	     function we will set the weakdef field to the correct
4286
	     value.  We only put non-function symbols from dynamic
4287
	     objects on this list, because that happens to be the only
4288
	     time we need to know the normal symbol corresponding to a
4289
	     weak symbol, and the information is time consuming to
4290
	     figure out.  If the weakdef field is not already NULL,
4291
	     then this symbol was already defined by some previous
4292
	     dynamic object, and we will be using that previous
4293
	     definition anyhow.  */
4294
 
4295
	  h->u.weakdef = weaks;
4296
	  weaks = h;
4297
	  new_weakdef = TRUE;
4298
	}
4299
 
4300
      /* Set the alignment of a common symbol.  */
4301
      if ((common || bfd_is_com_section (sec))
4302
	  && h->root.type == bfd_link_hash_common)
4303
	{
4304
	  unsigned int align;
4305
 
4306
	  if (common)
4307
	    align = bfd_log2 (isym->st_value);
4308
	  else
4309
	    {
4310
	      /* The new symbol is a common symbol in a shared object.
4311
		 We need to get the alignment from the section.  */
4312
	      align = new_sec->alignment_power;
4313
	    }
4314
	  if (align > old_alignment)
4315
	    h->root.u.c.p->alignment_power = align;
4316
	  else
4317
	    h->root.u.c.p->alignment_power = old_alignment;
4318
	}
4319
 
4320
      if (is_elf_hash_table (htab))
4321
	{
4322
	  /* Set a flag in the hash table entry indicating the type of
4323
	     reference or definition we just found.  A dynamic symbol
4324
	     is one which is referenced or defined by both a regular
4325
	     object and a shared object.  */
4326
	  bfd_boolean dynsym = FALSE;
4327
 
4328
	  /* Plugin symbols aren't normal.  Don't set def_regular or
4329
	     ref_regular for them, or make them dynamic.  */
4330
	  if ((abfd->flags & BFD_PLUGIN) != 0)
4331
	    ;
4332
	  else if (! dynamic)
4333
	    {
4334
	      if (! definition)
4335
		{
4336
		  h->ref_regular = 1;
4337
		  if (bind != STB_WEAK)
4338
		    h->ref_regular_nonweak = 1;
4339
		}
4340
	      else
4341
		{
4342
		  h->def_regular = 1;
4343
		  if (h->def_dynamic)
4344
		    {
4345
		      h->def_dynamic = 0;
4346
		      h->ref_dynamic = 1;
4347
		    }
4348
		}
4349
 
4350
	      /* If the indirect symbol has been forced local, don't
4351
		 make the real symbol dynamic.  */
4352
	      if ((h == hi || !hi->forced_local)
6324 serge 4353
		  && (bfd_link_dll (info)
5197 serge 4354
		      || h->def_dynamic
4355
		      || h->ref_dynamic))
4356
		dynsym = TRUE;
4357
	    }
4358
	  else
4359
	    {
4360
	      if (! definition)
4361
		{
4362
		  h->ref_dynamic = 1;
4363
		  hi->ref_dynamic = 1;
4364
		}
4365
	      else
4366
		{
4367
		  h->def_dynamic = 1;
4368
		  hi->def_dynamic = 1;
4369
		}
4370
 
4371
	      /* If the indirect symbol has been forced local, don't
4372
		 make the real symbol dynamic.  */
4373
	      if ((h == hi || !hi->forced_local)
4374
		  && (h->def_regular
4375
		      || h->ref_regular
4376
		      || (h->u.weakdef != NULL
4377
			  && ! new_weakdef
4378
			  && h->u.weakdef->dynindx != -1)))
4379
		dynsym = TRUE;
4380
	    }
4381
 
4382
	  /* Check to see if we need to add an indirect symbol for
4383
	     the default name.  */
4384
	  if (definition
4385
	      || (!override && h->root.type == bfd_link_hash_common))
4386
	    if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4387
					      sec, value, &old_bfd, &dynsym))
4388
	      goto error_free_vers;
4389
 
4390
	  /* Check the alignment when a common symbol is involved. This
4391
	     can change when a common symbol is overridden by a normal
4392
	     definition or a common symbol is ignored due to the old
4393
	     normal definition. We need to make sure the maximum
4394
	     alignment is maintained.  */
4395
	  if ((old_alignment || common)
4396
	      && h->root.type != bfd_link_hash_common)
4397
	    {
4398
	      unsigned int common_align;
4399
	      unsigned int normal_align;
4400
	      unsigned int symbol_align;
4401
	      bfd *normal_bfd;
4402
	      bfd *common_bfd;
4403
 
4404
	      BFD_ASSERT (h->root.type == bfd_link_hash_defined
4405
			  || h->root.type == bfd_link_hash_defweak);
4406
 
4407
	      symbol_align = ffs (h->root.u.def.value) - 1;
4408
	      if (h->root.u.def.section->owner != NULL
4409
		  && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4410
		{
4411
		  normal_align = h->root.u.def.section->alignment_power;
4412
		  if (normal_align > symbol_align)
4413
		    normal_align = symbol_align;
4414
		}
4415
	      else
4416
		normal_align = symbol_align;
4417
 
4418
	      if (old_alignment)
4419
		{
4420
		  common_align = old_alignment;
4421
		  common_bfd = old_bfd;
4422
		  normal_bfd = abfd;
4423
		}
4424
	      else
4425
		{
4426
		  common_align = bfd_log2 (isym->st_value);
4427
		  common_bfd = abfd;
4428
		  normal_bfd = old_bfd;
4429
		}
4430
 
4431
	      if (normal_align < common_align)
4432
		{
4433
		  /* PR binutils/2735 */
4434
		  if (normal_bfd == NULL)
4435
		    (*_bfd_error_handler)
4436
		      (_("Warning: alignment %u of common symbol `%s' in %B is"
4437
			 " greater than the alignment (%u) of its section %A"),
4438
		       common_bfd, h->root.u.def.section,
4439
		       1 << common_align, name, 1 << normal_align);
4440
		  else
4441
		    (*_bfd_error_handler)
4442
		      (_("Warning: alignment %u of symbol `%s' in %B"
4443
			 " is smaller than %u in %B"),
4444
		       normal_bfd, common_bfd,
4445
		       1 << normal_align, name, 1 << common_align);
4446
		}
4447
	    }
4448
 
4449
	  /* Remember the symbol size if it isn't undefined.  */
4450
	  if (isym->st_size != 0
4451
	      && isym->st_shndx != SHN_UNDEF
4452
	      && (definition || h->size == 0))
4453
	    {
4454
	      if (h->size != 0
4455
		  && h->size != isym->st_size
4456
		  && ! size_change_ok)
4457
		(*_bfd_error_handler)
4458
		  (_("Warning: size of symbol `%s' changed"
4459
		     " from %lu in %B to %lu in %B"),
4460
		   old_bfd, abfd,
4461
		   name, (unsigned long) h->size,
4462
		   (unsigned long) isym->st_size);
4463
 
4464
	      h->size = isym->st_size;
4465
	    }
4466
 
4467
	  /* If this is a common symbol, then we always want H->SIZE
4468
	     to be the size of the common symbol.  The code just above
4469
	     won't fix the size if a common symbol becomes larger.  We
4470
	     don't warn about a size change here, because that is
4471
	     covered by --warn-common.  Allow changes between different
4472
	     function types.  */
4473
	  if (h->root.type == bfd_link_hash_common)
4474
	    h->size = h->root.u.c.size;
4475
 
4476
	  if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4477
	      && ((definition && !new_weak)
4478
		  || (old_weak && h->root.type == bfd_link_hash_common)
4479
		  || h->type == STT_NOTYPE))
4480
	    {
4481
	      unsigned int type = ELF_ST_TYPE (isym->st_info);
4482
 
4483
	      /* Turn an IFUNC symbol from a DSO into a normal FUNC
4484
		 symbol.  */
4485
	      if (type == STT_GNU_IFUNC
4486
		  && (abfd->flags & DYNAMIC) != 0)
4487
		type = STT_FUNC;
4488
 
4489
	      if (h->type != type)
4490
		{
4491
		  if (h->type != STT_NOTYPE && ! type_change_ok)
4492
		    (*_bfd_error_handler)
4493
		      (_("Warning: type of symbol `%s' changed"
4494
			 " from %d to %d in %B"),
4495
		       abfd, name, h->type, type);
4496
 
4497
		  h->type = type;
4498
		}
4499
	    }
4500
 
4501
	  /* Merge st_other field.  */
6324 serge 4502
	  elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
5197 serge 4503
 
4504
	  /* We don't want to make debug symbol dynamic.  */
6324 serge 4505
	  if (definition
4506
	      && (sec->flags & SEC_DEBUGGING)
4507
	      && !bfd_link_relocatable (info))
5197 serge 4508
	    dynsym = FALSE;
4509
 
4510
	  /* Nor should we make plugin symbols dynamic.  */
4511
	  if ((abfd->flags & BFD_PLUGIN) != 0)
4512
	    dynsym = FALSE;
4513
 
4514
	  if (definition)
4515
	    {
4516
	      h->target_internal = isym->st_target_internal;
4517
	      h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4518
	    }
4519
 
4520
	  if (definition && !dynamic)
4521
	    {
4522
	      char *p = strchr (name, ELF_VER_CHR);
4523
	      if (p != NULL && p[1] != ELF_VER_CHR)
4524
		{
4525
		  /* Queue non-default versions so that .symver x, x@FOO
4526
		     aliases can be checked.  */
4527
		  if (!nondeflt_vers)
4528
		    {
4529
		      amt = ((isymend - isym + 1)
4530
			     * sizeof (struct elf_link_hash_entry *));
6324 serge 4531
		      nondeflt_vers
4532
			= (struct elf_link_hash_entry **) bfd_malloc (amt);
5197 serge 4533
		      if (!nondeflt_vers)
4534
			goto error_free_vers;
4535
		    }
4536
		  nondeflt_vers[nondeflt_vers_cnt++] = h;
4537
		}
4538
	    }
4539
 
4540
	  if (dynsym && h->dynindx == -1)
4541
	    {
4542
	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
4543
		goto error_free_vers;
4544
	      if (h->u.weakdef != NULL
4545
		  && ! new_weakdef
4546
		  && h->u.weakdef->dynindx == -1)
4547
		{
4548
		  if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4549
		    goto error_free_vers;
4550
		}
4551
	    }
4552
	  else if (dynsym && h->dynindx != -1)
4553
	    /* If the symbol already has a dynamic index, but
4554
	       visibility says it should not be visible, turn it into
4555
	       a local symbol.  */
4556
	    switch (ELF_ST_VISIBILITY (h->other))
4557
	      {
4558
	      case STV_INTERNAL:
4559
	      case STV_HIDDEN:
4560
		(*bed->elf_backend_hide_symbol) (info, h, TRUE);
4561
		dynsym = FALSE;
4562
		break;
4563
	      }
4564
 
4565
	  /* Don't add DT_NEEDED for references from the dummy bfd.  */
4566
	  if (!add_needed
4567
	      && definition
4568
	      && ((dynsym
4569
		   && h->ref_regular_nonweak
4570
		   && (old_bfd == NULL
4571
		       || (old_bfd->flags & BFD_PLUGIN) == 0))
4572
		  || (h->ref_dynamic_nonweak
4573
		      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4574
		      && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4575
	    {
4576
	      int ret;
4577
	      const char *soname = elf_dt_name (abfd);
4578
 
6324 serge 4579
	      info->callbacks->minfo ("%!", soname, old_bfd,
4580
				      h->root.root.string);
4581
 
5197 serge 4582
	      /* A symbol from a library loaded via DT_NEEDED of some
4583
		 other library is referenced by a regular object.
4584
		 Add a DT_NEEDED entry for it.  Issue an error if
4585
		 --no-add-needed is used and the reference was not
4586
		 a weak one.  */
4587
	      if (old_bfd != NULL
4588
		  && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4589
		{
4590
		  (*_bfd_error_handler)
4591
		    (_("%B: undefined reference to symbol '%s'"),
4592
		     old_bfd, name);
4593
		  bfd_set_error (bfd_error_missing_dso);
4594
		  goto error_free_vers;
4595
		}
4596
 
4597
	      elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
6324 serge 4598
		(elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5197 serge 4599
 
4600
	      add_needed = TRUE;
4601
	      ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4602
	      if (ret < 0)
4603
		goto error_free_vers;
4604
 
4605
	      BFD_ASSERT (ret == 0);
4606
	    }
4607
	}
4608
    }
4609
 
4610
  if (extversym != NULL)
4611
    {
4612
      free (extversym);
4613
      extversym = NULL;
4614
    }
4615
 
4616
  if (isymbuf != NULL)
4617
    {
4618
      free (isymbuf);
4619
      isymbuf = NULL;
4620
    }
4621
 
4622
  if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4623
    {
4624
      unsigned int i;
4625
 
4626
      /* Restore the symbol table.  */
4627
      old_ent = (char *) old_tab + tabsize;
4628
      memset (elf_sym_hashes (abfd), 0,
4629
	      extsymcount * sizeof (struct elf_link_hash_entry *));
4630
      htab->root.table.table = old_table;
4631
      htab->root.table.size = old_size;
4632
      htab->root.table.count = old_count;
4633
      memcpy (htab->root.table.table, old_tab, tabsize);
4634
      htab->root.undefs = old_undefs;
4635
      htab->root.undefs_tail = old_undefs_tail;
4636
      _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
4637
      for (i = 0; i < htab->root.table.size; i++)
4638
	{
4639
	  struct bfd_hash_entry *p;
4640
	  struct elf_link_hash_entry *h;
4641
	  bfd_size_type size;
4642
	  unsigned int alignment_power;
4643
 
4644
	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4645
	    {
4646
	      h = (struct elf_link_hash_entry *) p;
4647
	      if (h->root.type == bfd_link_hash_warning)
4648
		h = (struct elf_link_hash_entry *) h->root.u.i.link;
4649
	      if (h->dynindx >= old_dynsymcount
4650
		  && h->dynstr_index < old_dynstr_size)
4651
		_bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4652
 
4653
	      /* Preserve the maximum alignment and size for common
4654
		 symbols even if this dynamic lib isn't on DT_NEEDED
4655
		 since it can still be loaded at run time by another
4656
		 dynamic lib.  */
4657
	      if (h->root.type == bfd_link_hash_common)
4658
		{
4659
		  size = h->root.u.c.size;
4660
		  alignment_power = h->root.u.c.p->alignment_power;
4661
		}
4662
	      else
4663
		{
4664
		  size = 0;
4665
		  alignment_power = 0;
4666
		}
4667
	      memcpy (p, old_ent, htab->root.table.entsize);
4668
	      old_ent = (char *) old_ent + htab->root.table.entsize;
4669
	      h = (struct elf_link_hash_entry *) p;
4670
	      if (h->root.type == bfd_link_hash_warning)
4671
		{
4672
		  memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4673
		  old_ent = (char *) old_ent + htab->root.table.entsize;
4674
		  h = (struct elf_link_hash_entry *) h->root.u.i.link;
4675
		}
4676
	      if (h->root.type == bfd_link_hash_common)
4677
		{
4678
		  if (size > h->root.u.c.size)
4679
		    h->root.u.c.size = size;
4680
		  if (alignment_power > h->root.u.c.p->alignment_power)
4681
		    h->root.u.c.p->alignment_power = alignment_power;
4682
		}
4683
	    }
4684
	}
4685
 
4686
      /* Make a special call to the linker "notice" function to
4687
	 tell it that symbols added for crefs may need to be removed.  */
4688
      if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4689
	goto error_free_vers;
4690
 
4691
      free (old_tab);
4692
      objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4693
			   alloc_mark);
4694
      if (nondeflt_vers != NULL)
4695
	free (nondeflt_vers);
4696
      return TRUE;
4697
    }
4698
 
4699
  if (old_tab != NULL)
4700
    {
4701
      if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4702
	goto error_free_vers;
4703
      free (old_tab);
4704
      old_tab = NULL;
4705
    }
4706
 
6324 serge 4707
  /* Now that all the symbols from this input file are created, if
4708
     not performing a relocatable link, handle .symver foo, foo@BAR
4709
     such that any relocs against foo become foo@BAR.  */
4710
  if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5197 serge 4711
    {
4712
      bfd_size_type cnt, symidx;
4713
 
4714
      for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4715
	{
4716
	  struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4717
	  char *shortname, *p;
4718
 
4719
	  p = strchr (h->root.root.string, ELF_VER_CHR);
4720
	  if (p == NULL
4721
	      || (h->root.type != bfd_link_hash_defined
4722
		  && h->root.type != bfd_link_hash_defweak))
4723
	    continue;
4724
 
4725
	  amt = p - h->root.root.string;
4726
	  shortname = (char *) bfd_malloc (amt + 1);
4727
	  if (!shortname)
4728
	    goto error_free_vers;
4729
	  memcpy (shortname, h->root.root.string, amt);
4730
	  shortname[amt] = '\0';
4731
 
4732
	  hi = (struct elf_link_hash_entry *)
4733
	       bfd_link_hash_lookup (&htab->root, shortname,
4734
				     FALSE, FALSE, FALSE);
4735
	  if (hi != NULL
4736
	      && hi->root.type == h->root.type
4737
	      && hi->root.u.def.value == h->root.u.def.value
4738
	      && hi->root.u.def.section == h->root.u.def.section)
4739
	    {
4740
	      (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4741
	      hi->root.type = bfd_link_hash_indirect;
4742
	      hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4743
	      (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4744
	      sym_hash = elf_sym_hashes (abfd);
4745
	      if (sym_hash)
4746
		for (symidx = 0; symidx < extsymcount; ++symidx)
4747
		  if (sym_hash[symidx] == hi)
4748
		    {
4749
		      sym_hash[symidx] = h;
4750
		      break;
4751
		    }
4752
	    }
4753
	  free (shortname);
4754
	}
4755
      free (nondeflt_vers);
4756
      nondeflt_vers = NULL;
4757
    }
4758
 
4759
  /* Now set the weakdefs field correctly for all the weak defined
4760
     symbols we found.  The only way to do this is to search all the
4761
     symbols.  Since we only need the information for non functions in
4762
     dynamic objects, that's the only time we actually put anything on
4763
     the list WEAKS.  We need this information so that if a regular
4764
     object refers to a symbol defined weakly in a dynamic object, the
4765
     real symbol in the dynamic object is also put in the dynamic
4766
     symbols; we also must arrange for both symbols to point to the
4767
     same memory location.  We could handle the general case of symbol
4768
     aliasing, but a general symbol alias can only be generated in
4769
     assembler code, handling it correctly would be very time
4770
     consuming, and other ELF linkers don't handle general aliasing
4771
     either.  */
4772
  if (weaks != NULL)
4773
    {
4774
      struct elf_link_hash_entry **hpp;
4775
      struct elf_link_hash_entry **hppend;
4776
      struct elf_link_hash_entry **sorted_sym_hash;
4777
      struct elf_link_hash_entry *h;
4778
      size_t sym_count;
4779
 
4780
      /* Since we have to search the whole symbol list for each weak
4781
	 defined symbol, search time for N weak defined symbols will be
4782
	 O(N^2). Binary search will cut it down to O(NlogN).  */
4783
      amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4784
      sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4785
      if (sorted_sym_hash == NULL)
4786
	goto error_return;
4787
      sym_hash = sorted_sym_hash;
4788
      hpp = elf_sym_hashes (abfd);
4789
      hppend = hpp + extsymcount;
4790
      sym_count = 0;
4791
      for (; hpp < hppend; hpp++)
4792
	{
4793
	  h = *hpp;
4794
	  if (h != NULL
4795
	      && h->root.type == bfd_link_hash_defined
4796
	      && !bed->is_function_type (h->type))
4797
	    {
4798
	      *sym_hash = h;
4799
	      sym_hash++;
4800
	      sym_count++;
4801
	    }
4802
	}
4803
 
4804
      qsort (sorted_sym_hash, sym_count,
4805
	     sizeof (struct elf_link_hash_entry *),
4806
	     elf_sort_symbol);
4807
 
4808
      while (weaks != NULL)
4809
	{
4810
	  struct elf_link_hash_entry *hlook;
4811
	  asection *slook;
4812
	  bfd_vma vlook;
4813
	  size_t i, j, idx = 0;
4814
 
4815
	  hlook = weaks;
4816
	  weaks = hlook->u.weakdef;
4817
	  hlook->u.weakdef = NULL;
4818
 
4819
	  BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4820
		      || hlook->root.type == bfd_link_hash_defweak
4821
		      || hlook->root.type == bfd_link_hash_common
4822
		      || hlook->root.type == bfd_link_hash_indirect);
4823
	  slook = hlook->root.u.def.section;
4824
	  vlook = hlook->root.u.def.value;
4825
 
4826
	  i = 0;
4827
	  j = sym_count;
4828
	  while (i != j)
4829
	    {
4830
	      bfd_signed_vma vdiff;
4831
	      idx = (i + j) / 2;
4832
	      h = sorted_sym_hash[idx];
4833
	      vdiff = vlook - h->root.u.def.value;
4834
	      if (vdiff < 0)
4835
		j = idx;
4836
	      else if (vdiff > 0)
4837
		i = idx + 1;
4838
	      else
4839
		{
6324 serge 4840
		  int sdiff = slook->id - h->root.u.def.section->id;
5197 serge 4841
		  if (sdiff < 0)
4842
		    j = idx;
4843
		  else if (sdiff > 0)
4844
		    i = idx + 1;
4845
		  else
4846
		    break;
4847
		}
4848
	    }
4849
 
4850
	  /* We didn't find a value/section match.  */
4851
	  if (i == j)
4852
	    continue;
4853
 
4854
	  /* With multiple aliases, or when the weak symbol is already
4855
	     strongly defined, we have multiple matching symbols and
4856
	     the binary search above may land on any of them.  Step
4857
	     one past the matching symbol(s).  */
4858
	  while (++idx != j)
4859
	    {
4860
	      h = sorted_sym_hash[idx];
4861
	      if (h->root.u.def.section != slook
4862
		  || h->root.u.def.value != vlook)
4863
		break;
4864
	    }
4865
 
4866
	  /* Now look back over the aliases.  Since we sorted by size
4867
	     as well as value and section, we'll choose the one with
4868
	     the largest size.  */
4869
	  while (idx-- != i)
4870
	    {
4871
	      h = sorted_sym_hash[idx];
4872
 
4873
	      /* Stop if value or section doesn't match.  */
4874
	      if (h->root.u.def.section != slook
4875
		  || h->root.u.def.value != vlook)
4876
		break;
4877
	      else if (h != hlook)
4878
		{
4879
		  hlook->u.weakdef = h;
4880
 
4881
		  /* If the weak definition is in the list of dynamic
4882
		     symbols, make sure the real definition is put
4883
		     there as well.  */
4884
		  if (hlook->dynindx != -1 && h->dynindx == -1)
4885
		    {
4886
		      if (! bfd_elf_link_record_dynamic_symbol (info, h))
4887
			{
4888
			err_free_sym_hash:
4889
			  free (sorted_sym_hash);
4890
			  goto error_return;
4891
			}
4892
		    }
4893
 
4894
		  /* If the real definition is in the list of dynamic
4895
		     symbols, make sure the weak definition is put
4896
		     there as well.  If we don't do this, then the
4897
		     dynamic loader might not merge the entries for the
4898
		     real definition and the weak definition.  */
4899
		  if (h->dynindx != -1 && hlook->dynindx == -1)
4900
		    {
4901
		      if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4902
			goto err_free_sym_hash;
4903
		    }
4904
		  break;
4905
		}
4906
	    }
4907
	}
4908
 
4909
      free (sorted_sym_hash);
4910
    }
4911
 
4912
  if (bed->check_directives
4913
      && !(*bed->check_directives) (abfd, info))
4914
    return FALSE;
4915
 
4916
  /* If this object is the same format as the output object, and it is
4917
     not a shared library, then let the backend look through the
4918
     relocs.
4919
 
4920
     This is required to build global offset table entries and to
4921
     arrange for dynamic relocs.  It is not required for the
4922
     particular common case of linking non PIC code, even when linking
4923
     against shared libraries, but unfortunately there is no way of
4924
     knowing whether an object file has been compiled PIC or not.
4925
     Looking through the relocs is not particularly time consuming.
4926
     The problem is that we must either (1) keep the relocs in memory,
4927
     which causes the linker to require additional runtime memory or
4928
     (2) read the relocs twice from the input file, which wastes time.
4929
     This would be a good case for using mmap.
4930
 
4931
     I have no idea how to handle linking PIC code into a file of a
4932
     different format.  It probably can't be done.  */
4933
  if (! dynamic
4934
      && is_elf_hash_table (htab)
4935
      && bed->check_relocs != NULL
4936
      && elf_object_id (abfd) == elf_hash_table_id (htab)
4937
      && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4938
    {
4939
      asection *o;
4940
 
4941
      for (o = abfd->sections; o != NULL; o = o->next)
4942
	{
4943
	  Elf_Internal_Rela *internal_relocs;
4944
	  bfd_boolean ok;
4945
 
4946
	  if ((o->flags & SEC_RELOC) == 0
4947
	      || o->reloc_count == 0
4948
	      || ((info->strip == strip_all || info->strip == strip_debugger)
4949
		  && (o->flags & SEC_DEBUGGING) != 0)
4950
	      || bfd_is_abs_section (o->output_section))
4951
	    continue;
4952
 
4953
	  internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4954
						       info->keep_memory);
4955
	  if (internal_relocs == NULL)
4956
	    goto error_return;
4957
 
4958
	  ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4959
 
4960
	  if (elf_section_data (o)->relocs != internal_relocs)
4961
	    free (internal_relocs);
4962
 
4963
	  if (! ok)
4964
	    goto error_return;
4965
	}
4966
    }
4967
 
4968
  /* If this is a non-traditional link, try to optimize the handling
4969
     of the .stab/.stabstr sections.  */
4970
  if (! dynamic
4971
      && ! info->traditional_format
4972
      && is_elf_hash_table (htab)
4973
      && (info->strip != strip_all && info->strip != strip_debugger))
4974
    {
4975
      asection *stabstr;
4976
 
4977
      stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4978
      if (stabstr != NULL)
4979
	{
4980
	  bfd_size_type string_offset = 0;
4981
	  asection *stab;
4982
 
4983
	  for (stab = abfd->sections; stab; stab = stab->next)
4984
	    if (CONST_STRNEQ (stab->name, ".stab")
4985
		&& (!stab->name[5] ||
4986
		    (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4987
		&& (stab->flags & SEC_MERGE) == 0
4988
		&& !bfd_is_abs_section (stab->output_section))
4989
	      {
4990
		struct bfd_elf_section_data *secdata;
4991
 
4992
		secdata = elf_section_data (stab);
4993
		if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4994
					       stabstr, &secdata->sec_info,
4995
					       &string_offset))
4996
		  goto error_return;
4997
		if (secdata->sec_info)
4998
		  stab->sec_info_type = SEC_INFO_TYPE_STABS;
4999
	    }
5000
	}
5001
    }
5002
 
5003
  if (is_elf_hash_table (htab) && add_needed)
5004
    {
5005
      /* Add this bfd to the loaded list.  */
5006
      struct elf_link_loaded_list *n;
5007
 
6324 serge 5008
      n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5197 serge 5009
      if (n == NULL)
5010
	goto error_return;
5011
      n->abfd = abfd;
5012
      n->next = htab->loaded;
5013
      htab->loaded = n;
5014
    }
5015
 
5016
  return TRUE;
5017
 
5018
 error_free_vers:
5019
  if (old_tab != NULL)
5020
    free (old_tab);
5021
  if (nondeflt_vers != NULL)
5022
    free (nondeflt_vers);
5023
  if (extversym != NULL)
5024
    free (extversym);
5025
 error_free_sym:
5026
  if (isymbuf != NULL)
5027
    free (isymbuf);
5028
 error_return:
5029
  return FALSE;
5030
}
5031
 
5032
/* Return the linker hash table entry of a symbol that might be
5033
   satisfied by an archive symbol.  Return -1 on error.  */
5034
 
5035
struct elf_link_hash_entry *
5036
_bfd_elf_archive_symbol_lookup (bfd *abfd,
5037
				struct bfd_link_info *info,
5038
				const char *name)
5039
{
5040
  struct elf_link_hash_entry *h;
5041
  char *p, *copy;
5042
  size_t len, first;
5043
 
5044
  h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5045
  if (h != NULL)
5046
    return h;
5047
 
5048
  /* If this is a default version (the name contains @@), look up the
5049
     symbol again with only one `@' as well as without the version.
5050
     The effect is that references to the symbol with and without the
5051
     version will be matched by the default symbol in the archive.  */
5052
 
5053
  p = strchr (name, ELF_VER_CHR);
5054
  if (p == NULL || p[1] != ELF_VER_CHR)
5055
    return h;
5056
 
5057
  /* First check with only one `@'.  */
5058
  len = strlen (name);
5059
  copy = (char *) bfd_alloc (abfd, len);
5060
  if (copy == NULL)
5061
    return (struct elf_link_hash_entry *) 0 - 1;
5062
 
5063
  first = p - name + 1;
5064
  memcpy (copy, name, first);
5065
  memcpy (copy + first, name + first + 1, len - first);
5066
 
5067
  h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5068
  if (h == NULL)
5069
    {
5070
      /* We also need to check references to the symbol without the
5071
	 version.  */
5072
      copy[first - 1] = '\0';
5073
      h = elf_link_hash_lookup (elf_hash_table (info), copy,
5074
				FALSE, FALSE, TRUE);
5075
    }
5076
 
5077
  bfd_release (abfd, copy);
5078
  return h;
5079
}
5080
 
5081
/* Add symbols from an ELF archive file to the linker hash table.  We
6324 serge 5082
   don't use _bfd_generic_link_add_archive_symbols because we need to
5083
   handle versioned symbols.
5197 serge 5084
 
5085
   Fortunately, ELF archive handling is simpler than that done by
5086
   _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5087
   oddities.  In ELF, if we find a symbol in the archive map, and the
5088
   symbol is currently undefined, we know that we must pull in that
5089
   object file.
5090
 
5091
   Unfortunately, we do have to make multiple passes over the symbol
5092
   table until nothing further is resolved.  */
5093
 
5094
static bfd_boolean
5095
elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5096
{
5097
  symindex c;
6324 serge 5098
  unsigned char *included = NULL;
5197 serge 5099
  carsym *symdefs;
5100
  bfd_boolean loop;
5101
  bfd_size_type amt;
5102
  const struct elf_backend_data *bed;
5103
  struct elf_link_hash_entry * (*archive_symbol_lookup)
5104
    (bfd *, struct bfd_link_info *, const char *);
5105
 
5106
  if (! bfd_has_map (abfd))
5107
    {
5108
      /* An empty archive is a special case.  */
5109
      if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5110
	return TRUE;
5111
      bfd_set_error (bfd_error_no_armap);
5112
      return FALSE;
5113
    }
5114
 
5115
  /* Keep track of all symbols we know to be already defined, and all
5116
     files we know to be already included.  This is to speed up the
5117
     second and subsequent passes.  */
5118
  c = bfd_ardata (abfd)->symdef_count;
5119
  if (c == 0)
5120
    return TRUE;
5121
  amt = c;
6324 serge 5122
  amt *= sizeof (*included);
5123
  included = (unsigned char *) bfd_zmalloc (amt);
5124
  if (included == NULL)
5125
    return FALSE;
5197 serge 5126
 
5127
  symdefs = bfd_ardata (abfd)->symdefs;
5128
  bed = get_elf_backend_data (abfd);
5129
  archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5130
 
5131
  do
5132
    {
5133
      file_ptr last;
5134
      symindex i;
5135
      carsym *symdef;
5136
      carsym *symdefend;
5137
 
5138
      loop = FALSE;
5139
      last = -1;
5140
 
5141
      symdef = symdefs;
5142
      symdefend = symdef + c;
5143
      for (i = 0; symdef < symdefend; symdef++, i++)
5144
	{
5145
	  struct elf_link_hash_entry *h;
5146
	  bfd *element;
5147
	  struct bfd_link_hash_entry *undefs_tail;
5148
	  symindex mark;
5149
 
6324 serge 5150
	  if (included[i])
5197 serge 5151
	    continue;
5152
	  if (symdef->file_offset == last)
5153
	    {
5154
	      included[i] = TRUE;
5155
	      continue;
5156
	    }
5157
 
5158
	  h = archive_symbol_lookup (abfd, info, symdef->name);
5159
	  if (h == (struct elf_link_hash_entry *) 0 - 1)
5160
	    goto error_return;
5161
 
5162
	  if (h == NULL)
5163
	    continue;
5164
 
5165
	  if (h->root.type == bfd_link_hash_common)
5166
	    {
5167
	      /* We currently have a common symbol.  The archive map contains
5168
		 a reference to this symbol, so we may want to include it.  We
5169
		 only want to include it however, if this archive element
5170
		 contains a definition of the symbol, not just another common
5171
		 declaration of it.
5172
 
5173
		 Unfortunately some archivers (including GNU ar) will put
5174
		 declarations of common symbols into their archive maps, as
5175
		 well as real definitions, so we cannot just go by the archive
5176
		 map alone.  Instead we must read in the element's symbol
5177
		 table and check that to see what kind of symbol definition
5178
		 this is.  */
5179
	      if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5180
		continue;
5181
	    }
5182
	  else if (h->root.type != bfd_link_hash_undefined)
5183
	    {
5184
	      if (h->root.type != bfd_link_hash_undefweak)
6324 serge 5185
		/* Symbol must be defined.  Don't check it again.  */
5186
		included[i] = TRUE;
5197 serge 5187
	      continue;
5188
	    }
5189
 
5190
	  /* We need to include this archive member.  */
5191
	  element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5192
	  if (element == NULL)
5193
	    goto error_return;
5194
 
5195
	  if (! bfd_check_format (element, bfd_object))
5196
	    goto error_return;
5197
 
5198
	  undefs_tail = info->hash->undefs_tail;
5199
 
5200
	  if (!(*info->callbacks
5201
		->add_archive_element) (info, element, symdef->name, &element))
5202
	    goto error_return;
5203
	  if (!bfd_link_add_symbols (element, info))
5204
	    goto error_return;
5205
 
5206
	  /* If there are any new undefined symbols, we need to make
5207
	     another pass through the archive in order to see whether
5208
	     they can be defined.  FIXME: This isn't perfect, because
5209
	     common symbols wind up on undefs_tail and because an
5210
	     undefined symbol which is defined later on in this pass
5211
	     does not require another pass.  This isn't a bug, but it
5212
	     does make the code less efficient than it could be.  */
5213
	  if (undefs_tail != info->hash->undefs_tail)
5214
	    loop = TRUE;
5215
 
5216
	  /* Look backward to mark all symbols from this object file
5217
	     which we have already seen in this pass.  */
5218
	  mark = i;
5219
	  do
5220
	    {
5221
	      included[mark] = TRUE;
5222
	      if (mark == 0)
5223
		break;
5224
	      --mark;
5225
	    }
5226
	  while (symdefs[mark].file_offset == symdef->file_offset);
5227
 
5228
	  /* We mark subsequent symbols from this object file as we go
5229
	     on through the loop.  */
5230
	  last = symdef->file_offset;
5231
	}
5232
    }
5233
  while (loop);
5234
 
5235
  free (included);
5236
 
5237
  return TRUE;
5238
 
5239
 error_return:
5240
  if (included != NULL)
5241
    free (included);
5242
  return FALSE;
5243
}
5244
 
5245
/* Given an ELF BFD, add symbols to the global hash table as
5246
   appropriate.  */
5247
 
5248
bfd_boolean
5249
bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5250
{
5251
  switch (bfd_get_format (abfd))
5252
    {
5253
    case bfd_object:
5254
      return elf_link_add_object_symbols (abfd, info);
5255
    case bfd_archive:
5256
      return elf_link_add_archive_symbols (abfd, info);
5257
    default:
5258
      bfd_set_error (bfd_error_wrong_format);
5259
      return FALSE;
5260
    }
5261
}
5262
 
5263
struct hash_codes_info
5264
{
5265
  unsigned long *hashcodes;
5266
  bfd_boolean error;
5267
};
5268
 
5269
/* This function will be called though elf_link_hash_traverse to store
5270
   all hash value of the exported symbols in an array.  */
5271
 
5272
static bfd_boolean
5273
elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5274
{
5275
  struct hash_codes_info *inf = (struct hash_codes_info *) data;
5276
  const char *name;
5277
  unsigned long ha;
5278
  char *alc = NULL;
5279
 
5280
  /* Ignore indirect symbols.  These are added by the versioning code.  */
5281
  if (h->dynindx == -1)
5282
    return TRUE;
5283
 
5284
  name = h->root.root.string;
6324 serge 5285
  if (h->versioned >= versioned)
5197 serge 5286
    {
6324 serge 5287
      char *p = strchr (name, ELF_VER_CHR);
5288
      if (p != NULL)
5197 serge 5289
	{
6324 serge 5290
	  alc = (char *) bfd_malloc (p - name + 1);
5291
	  if (alc == NULL)
5292
	    {
5293
	      inf->error = TRUE;
5294
	      return FALSE;
5295
	    }
5296
	  memcpy (alc, name, p - name);
5297
	  alc[p - name] = '\0';
5298
	  name = alc;
5197 serge 5299
	}
5300
    }
5301
 
5302
  /* Compute the hash value.  */
5303
  ha = bfd_elf_hash (name);
5304
 
5305
  /* Store the found hash value in the array given as the argument.  */
5306
  *(inf->hashcodes)++ = ha;
5307
 
5308
  /* And store it in the struct so that we can put it in the hash table
5309
     later.  */
5310
  h->u.elf_hash_value = ha;
5311
 
5312
  if (alc != NULL)
5313
    free (alc);
5314
 
5315
  return TRUE;
5316
}
5317
 
5318
struct collect_gnu_hash_codes
5319
{
5320
  bfd *output_bfd;
5321
  const struct elf_backend_data *bed;
5322
  unsigned long int nsyms;
5323
  unsigned long int maskbits;
5324
  unsigned long int *hashcodes;
5325
  unsigned long int *hashval;
5326
  unsigned long int *indx;
5327
  unsigned long int *counts;
5328
  bfd_vma *bitmask;
5329
  bfd_byte *contents;
5330
  long int min_dynindx;
5331
  unsigned long int bucketcount;
5332
  unsigned long int symindx;
5333
  long int local_indx;
5334
  long int shift1, shift2;
5335
  unsigned long int mask;
5336
  bfd_boolean error;
5337
};
5338
 
5339
/* This function will be called though elf_link_hash_traverse to store
5340
   all hash value of the exported symbols in an array.  */
5341
 
5342
static bfd_boolean
5343
elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5344
{
5345
  struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5346
  const char *name;
5347
  unsigned long ha;
5348
  char *alc = NULL;
5349
 
5350
  /* Ignore indirect symbols.  These are added by the versioning code.  */
5351
  if (h->dynindx == -1)
5352
    return TRUE;
5353
 
5354
  /* Ignore also local symbols and undefined symbols.  */
5355
  if (! (*s->bed->elf_hash_symbol) (h))
5356
    return TRUE;
5357
 
5358
  name = h->root.root.string;
6324 serge 5359
  if (h->versioned >= versioned)
5197 serge 5360
    {
6324 serge 5361
      char *p = strchr (name, ELF_VER_CHR);
5362
      if (p != NULL)
5197 serge 5363
	{
6324 serge 5364
	  alc = (char *) bfd_malloc (p - name + 1);
5365
	  if (alc == NULL)
5366
	    {
5367
	      s->error = TRUE;
5368
	      return FALSE;
5369
	    }
5370
	  memcpy (alc, name, p - name);
5371
	  alc[p - name] = '\0';
5372
	  name = alc;
5197 serge 5373
	}
5374
    }
5375
 
5376
  /* Compute the hash value.  */
5377
  ha = bfd_elf_gnu_hash (name);
5378
 
5379
  /* Store the found hash value in the array for compute_bucket_count,
5380
     and also for .dynsym reordering purposes.  */
5381
  s->hashcodes[s->nsyms] = ha;
5382
  s->hashval[h->dynindx] = ha;
5383
  ++s->nsyms;
5384
  if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5385
    s->min_dynindx = h->dynindx;
5386
 
5387
  if (alc != NULL)
5388
    free (alc);
5389
 
5390
  return TRUE;
5391
}
5392
 
5393
/* This function will be called though elf_link_hash_traverse to do
5394
   final dynaminc symbol renumbering.  */
5395
 
5396
static bfd_boolean
5397
elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5398
{
5399
  struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5400
  unsigned long int bucket;
5401
  unsigned long int val;
5402
 
5403
  /* Ignore indirect symbols.  */
5404
  if (h->dynindx == -1)
5405
    return TRUE;
5406
 
5407
  /* Ignore also local symbols and undefined symbols.  */
5408
  if (! (*s->bed->elf_hash_symbol) (h))
5409
    {
5410
      if (h->dynindx >= s->min_dynindx)
5411
	h->dynindx = s->local_indx++;
5412
      return TRUE;
5413
    }
5414
 
5415
  bucket = s->hashval[h->dynindx] % s->bucketcount;
5416
  val = (s->hashval[h->dynindx] >> s->shift1)
5417
	& ((s->maskbits >> s->shift1) - 1);
5418
  s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5419
  s->bitmask[val]
5420
    |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5421
  val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5422
  if (s->counts[bucket] == 1)
5423
    /* Last element terminates the chain.  */
5424
    val |= 1;
5425
  bfd_put_32 (s->output_bfd, val,
5426
	      s->contents + (s->indx[bucket] - s->symindx) * 4);
5427
  --s->counts[bucket];
5428
  h->dynindx = s->indx[bucket]++;
5429
  return TRUE;
5430
}
5431
 
5432
/* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
5433
 
5434
bfd_boolean
5435
_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5436
{
5437
  return !(h->forced_local
5438
	   || h->root.type == bfd_link_hash_undefined
5439
	   || h->root.type == bfd_link_hash_undefweak
5440
	   || ((h->root.type == bfd_link_hash_defined
5441
		|| h->root.type == bfd_link_hash_defweak)
5442
	       && h->root.u.def.section->output_section == NULL));
5443
}
5444
 
5445
/* Array used to determine the number of hash table buckets to use
5446
   based on the number of symbols there are.  If there are fewer than
5447
   3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5448
   fewer than 37 we use 17 buckets, and so forth.  We never use more
5449
   than 32771 buckets.  */
5450
 
5451
static const size_t elf_buckets[] =
5452
{
5453
  1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5454
  16411, 32771, 0
5455
};
5456
 
5457
/* Compute bucket count for hashing table.  We do not use a static set
5458
   of possible tables sizes anymore.  Instead we determine for all
5459
   possible reasonable sizes of the table the outcome (i.e., the
5460
   number of collisions etc) and choose the best solution.  The
5461
   weighting functions are not too simple to allow the table to grow
5462
   without bounds.  Instead one of the weighting factors is the size.
5463
   Therefore the result is always a good payoff between few collisions
5464
   (= short chain lengths) and table size.  */
5465
static size_t
5466
compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5467
		      unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5468
		      unsigned long int nsyms,
5469
		      int gnu_hash)
5470
{
5471
  size_t best_size = 0;
5472
  unsigned long int i;
5473
 
5474
  /* We have a problem here.  The following code to optimize the table
5475
     size requires an integer type with more the 32 bits.  If
5476
     BFD_HOST_U_64_BIT is set we know about such a type.  */
5477
#ifdef BFD_HOST_U_64_BIT
5478
  if (info->optimize)
5479
    {
5480
      size_t minsize;
5481
      size_t maxsize;
5482
      BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5483
      bfd *dynobj = elf_hash_table (info)->dynobj;
5484
      size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5485
      const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5486
      unsigned long int *counts;
5487
      bfd_size_type amt;
5488
      unsigned int no_improvement_count = 0;
5489
 
5490
      /* Possible optimization parameters: if we have NSYMS symbols we say
5491
	 that the hashing table must at least have NSYMS/4 and at most
5492
	 2*NSYMS buckets.  */
5493
      minsize = nsyms / 4;
5494
      if (minsize == 0)
5495
	minsize = 1;
5496
      best_size = maxsize = nsyms * 2;
5497
      if (gnu_hash)
5498
	{
5499
	  if (minsize < 2)
5500
	    minsize = 2;
5501
	  if ((best_size & 31) == 0)
5502
	    ++best_size;
5503
	}
5504
 
5505
      /* Create array where we count the collisions in.  We must use bfd_malloc
5506
	 since the size could be large.  */
5507
      amt = maxsize;
5508
      amt *= sizeof (unsigned long int);
5509
      counts = (unsigned long int *) bfd_malloc (amt);
5510
      if (counts == NULL)
5511
	return 0;
5512
 
5513
      /* Compute the "optimal" size for the hash table.  The criteria is a
5514
	 minimal chain length.  The minor criteria is (of course) the size
5515
	 of the table.  */
5516
      for (i = minsize; i < maxsize; ++i)
5517
	{
5518
	  /* Walk through the array of hashcodes and count the collisions.  */
5519
	  BFD_HOST_U_64_BIT max;
5520
	  unsigned long int j;
5521
	  unsigned long int fact;
5522
 
5523
	  if (gnu_hash && (i & 31) == 0)
5524
	    continue;
5525
 
5526
	  memset (counts, '\0', i * sizeof (unsigned long int));
5527
 
5528
	  /* Determine how often each hash bucket is used.  */
5529
	  for (j = 0; j < nsyms; ++j)
5530
	    ++counts[hashcodes[j] % i];
5531
 
5532
	  /* For the weight function we need some information about the
5533
	     pagesize on the target.  This is information need not be 100%
5534
	     accurate.  Since this information is not available (so far) we
5535
	     define it here to a reasonable default value.  If it is crucial
5536
	     to have a better value some day simply define this value.  */
5537
# ifndef BFD_TARGET_PAGESIZE
5538
#  define BFD_TARGET_PAGESIZE	(4096)
5539
# endif
5540
 
5541
	  /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5542
	     and the chains.  */
5543
	  max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5544
 
5545
# if 1
5546
	  /* Variant 1: optimize for short chains.  We add the squares
5547
	     of all the chain lengths (which favors many small chain
5548
	     over a few long chains).  */
5549
	  for (j = 0; j < i; ++j)
5550
	    max += counts[j] * counts[j];
5551
 
5552
	  /* This adds penalties for the overall size of the table.  */
5553
	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5554
	  max *= fact * fact;
5555
# else
5556
	  /* Variant 2: Optimize a lot more for small table.  Here we
5557
	     also add squares of the size but we also add penalties for
5558
	     empty slots (the +1 term).  */
5559
	  for (j = 0; j < i; ++j)
5560
	    max += (1 + counts[j]) * (1 + counts[j]);
5561
 
5562
	  /* The overall size of the table is considered, but not as
5563
	     strong as in variant 1, where it is squared.  */
5564
	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5565
	  max *= fact;
5566
# endif
5567
 
5568
	  /* Compare with current best results.  */
5569
	  if (max < best_chlen)
5570
	    {
5571
	      best_chlen = max;
5572
	      best_size = i;
6324 serge 5573
	      no_improvement_count = 0;
5197 serge 5574
	    }
5575
	  /* PR 11843: Avoid futile long searches for the best bucket size
5576
	     when there are a large number of symbols.  */
5577
	  else if (++no_improvement_count == 100)
5578
	    break;
5579
	}
5580
 
5581
      free (counts);
5582
    }
5583
  else
5584
#endif /* defined (BFD_HOST_U_64_BIT) */
5585
    {
5586
      /* This is the fallback solution if no 64bit type is available or if we
5587
	 are not supposed to spend much time on optimizations.  We select the
5588
	 bucket count using a fixed set of numbers.  */
5589
      for (i = 0; elf_buckets[i] != 0; i++)
5590
	{
5591
	  best_size = elf_buckets[i];
5592
	  if (nsyms < elf_buckets[i + 1])
5593
	    break;
5594
	}
5595
      if (gnu_hash && best_size < 2)
5596
	best_size = 2;
5597
    }
5598
 
5599
  return best_size;
5600
}
5601
 
5602
/* Size any SHT_GROUP section for ld -r.  */
5603
 
5604
bfd_boolean
5605
_bfd_elf_size_group_sections (struct bfd_link_info *info)
5606
{
5607
  bfd *ibfd;
5608
 
6324 serge 5609
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5197 serge 5610
    if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5611
	&& !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5612
      return FALSE;
5613
  return TRUE;
5614
}
5615
 
5616
/* Set a default stack segment size.  The value in INFO wins.  If it
5617
   is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5618
   undefined it is initialized.  */
5619
 
5620
bfd_boolean
5621
bfd_elf_stack_segment_size (bfd *output_bfd,
5622
			    struct bfd_link_info *info,
5623
			    const char *legacy_symbol,
5624
			    bfd_vma default_size)
5625
{
5626
  struct elf_link_hash_entry *h = NULL;
5627
 
5628
  /* Look for legacy symbol.  */
5629
  if (legacy_symbol)
5630
    h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5631
			      FALSE, FALSE, FALSE);
5632
  if (h && (h->root.type == bfd_link_hash_defined
5633
	    || h->root.type == bfd_link_hash_defweak)
5634
      && h->def_regular
5635
      && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5636
    {
5637
      /* The symbol has no type if specified on the command line.  */
5638
      h->type = STT_OBJECT;
5639
      if (info->stacksize)
5640
	(*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5641
			       output_bfd, legacy_symbol);
5642
      else if (h->root.u.def.section != bfd_abs_section_ptr)
5643
	(*_bfd_error_handler) (_("%B: %s not absolute"),
5644
			       output_bfd, legacy_symbol);
5645
      else
5646
	info->stacksize = h->root.u.def.value;
5647
    }
5648
 
5649
  if (!info->stacksize)
5650
    /* If the user didn't set a size, or explicitly inhibit the
5651
       size, set it now.  */
5652
    info->stacksize = default_size;
5653
 
5654
  /* Provide the legacy symbol, if it is referenced.  */
5655
  if (h && (h->root.type == bfd_link_hash_undefined
5656
	    || h->root.type == bfd_link_hash_undefweak))
5657
    {
5658
      struct bfd_link_hash_entry *bh = NULL;
5659
 
5660
      if (!(_bfd_generic_link_add_one_symbol
5661
	    (info, output_bfd, legacy_symbol,
5662
	     BSF_GLOBAL, bfd_abs_section_ptr,
5663
	     info->stacksize >= 0 ? info->stacksize : 0,
5664
	     NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5665
	return FALSE;
5666
 
5667
      h = (struct elf_link_hash_entry *) bh;
5668
      h->def_regular = 1;
5669
      h->type = STT_OBJECT;
5670
    }
5671
 
5672
  return TRUE;
5673
}
5674
 
5675
/* Set up the sizes and contents of the ELF dynamic sections.  This is
5676
   called by the ELF linker emulation before_allocation routine.  We
5677
   must set the sizes of the sections before the linker sets the
5678
   addresses of the various sections.  */
5679
 
5680
bfd_boolean
5681
bfd_elf_size_dynamic_sections (bfd *output_bfd,
5682
			       const char *soname,
5683
			       const char *rpath,
5684
			       const char *filter_shlib,
5685
			       const char *audit,
5686
			       const char *depaudit,
5687
			       const char * const *auxiliary_filters,
5688
			       struct bfd_link_info *info,
5689
			       asection **sinterpptr)
5690
{
5691
  bfd_size_type soname_indx;
5692
  bfd *dynobj;
5693
  const struct elf_backend_data *bed;
5694
  struct elf_info_failed asvinfo;
5695
 
5696
  *sinterpptr = NULL;
5697
 
5698
  soname_indx = (bfd_size_type) -1;
5699
 
5700
  if (!is_elf_hash_table (info->hash))
5701
    return TRUE;
5702
 
5703
  bed = get_elf_backend_data (output_bfd);
5704
 
5705
  /* Any syms created from now on start with -1 in
5706
     got.refcount/offset and plt.refcount/offset.  */
5707
  elf_hash_table (info)->init_got_refcount
5708
    = elf_hash_table (info)->init_got_offset;
5709
  elf_hash_table (info)->init_plt_refcount
5710
    = elf_hash_table (info)->init_plt_offset;
5711
 
6324 serge 5712
  if (bfd_link_relocatable (info)
5197 serge 5713
      && !_bfd_elf_size_group_sections (info))
5714
    return FALSE;
5715
 
5716
  /* The backend may have to create some sections regardless of whether
5717
     we're dynamic or not.  */
5718
  if (bed->elf_backend_always_size_sections
5719
      && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5720
    return FALSE;
5721
 
5722
  /* Determine any GNU_STACK segment requirements, after the backend
5723
     has had a chance to set a default segment size.  */
5724
  if (info->execstack)
5725
    elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5726
  else if (info->noexecstack)
5727
    elf_stack_flags (output_bfd) = PF_R | PF_W;
5728
  else
5729
    {
5730
      bfd *inputobj;
5731
      asection *notesec = NULL;
5732
      int exec = 0;
5733
 
5734
      for (inputobj = info->input_bfds;
5735
	   inputobj;
6324 serge 5736
	   inputobj = inputobj->link.next)
5197 serge 5737
	{
5738
	  asection *s;
5739
 
5740
	  if (inputobj->flags
5741
	      & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5742
	    continue;
5743
	  s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5744
	  if (s)
5745
	    {
5746
	      if (s->flags & SEC_CODE)
5747
		exec = PF_X;
5748
	      notesec = s;
5749
	    }
5750
	  else if (bed->default_execstack)
5751
	    exec = PF_X;
5752
	}
5753
      if (notesec || info->stacksize > 0)
5754
	elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6324 serge 5755
      if (notesec && exec && bfd_link_relocatable (info)
5197 serge 5756
	  && notesec->output_section != bfd_abs_section_ptr)
5757
	notesec->output_section->flags |= SEC_CODE;
5758
    }
5759
 
5760
  dynobj = elf_hash_table (info)->dynobj;
5761
 
5762
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5763
    {
5764
      struct elf_info_failed eif;
5765
      struct elf_link_hash_entry *h;
5766
      asection *dynstr;
5767
      struct bfd_elf_version_tree *t;
5768
      struct bfd_elf_version_expr *d;
5769
      asection *s;
5770
      bfd_boolean all_defined;
5771
 
5772
      *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6324 serge 5773
      BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5197 serge 5774
 
5775
      if (soname != NULL)
5776
	{
5777
	  soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5778
					     soname, TRUE);
5779
	  if (soname_indx == (bfd_size_type) -1
5780
	      || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5781
	    return FALSE;
5782
	}
5783
 
5784
      if (info->symbolic)
5785
	{
5786
	  if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5787
	    return FALSE;
5788
	  info->flags |= DF_SYMBOLIC;
5789
	}
5790
 
5791
      if (rpath != NULL)
5792
	{
5793
	  bfd_size_type indx;
5794
	  bfd_vma tag;
5795
 
5796
	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5797
				      TRUE);
5798
	  if (indx == (bfd_size_type) -1)
5799
	    return FALSE;
5800
 
5801
	  tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5802
	  if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5803
	    return FALSE;
5804
	}
5805
 
5806
      if (filter_shlib != NULL)
5807
	{
5808
	  bfd_size_type indx;
5809
 
5810
	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5811
				      filter_shlib, TRUE);
5812
	  if (indx == (bfd_size_type) -1
5813
	      || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5814
	    return FALSE;
5815
	}
5816
 
5817
      if (auxiliary_filters != NULL)
5818
	{
5819
	  const char * const *p;
5820
 
5821
	  for (p = auxiliary_filters; *p != NULL; p++)
5822
	    {
5823
	      bfd_size_type indx;
5824
 
5825
	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5826
					  *p, TRUE);
5827
	      if (indx == (bfd_size_type) -1
5828
		  || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5829
		return FALSE;
5830
	    }
5831
	}
5832
 
5833
      if (audit != NULL)
5834
	{
5835
	  bfd_size_type indx;
5836
 
5837
	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5838
				      TRUE);
5839
	  if (indx == (bfd_size_type) -1
5840
	      || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5841
	    return FALSE;
5842
	}
5843
 
5844
      if (depaudit != NULL)
5845
	{
5846
	  bfd_size_type indx;
5847
 
5848
	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5849
				      TRUE);
5850
	  if (indx == (bfd_size_type) -1
5851
	      || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5852
	    return FALSE;
5853
	}
5854
 
5855
      eif.info = info;
5856
      eif.failed = FALSE;
5857
 
5858
      /* If we are supposed to export all symbols into the dynamic symbol
5859
	 table (this is not the normal case), then do so.  */
5860
      if (info->export_dynamic
6324 serge 5861
	  || (bfd_link_executable (info) && info->dynamic))
5197 serge 5862
	{
5863
	  elf_link_hash_traverse (elf_hash_table (info),
5864
				  _bfd_elf_export_symbol,
5865
				  &eif);
5866
	  if (eif.failed)
5867
	    return FALSE;
5868
	}
5869
 
5870
      /* Make all global versions with definition.  */
5871
      for (t = info->version_info; t != NULL; t = t->next)
5872
	for (d = t->globals.list; d != NULL; d = d->next)
5873
	  if (!d->symver && d->literal)
5874
	    {
5875
	      const char *verstr, *name;
5876
	      size_t namelen, verlen, newlen;
5877
	      char *newname, *p, leading_char;
5878
	      struct elf_link_hash_entry *newh;
5879
 
5880
	      leading_char = bfd_get_symbol_leading_char (output_bfd);
5881
	      name = d->pattern;
5882
	      namelen = strlen (name) + (leading_char != '\0');
5883
	      verstr = t->name;
5884
	      verlen = strlen (verstr);
5885
	      newlen = namelen + verlen + 3;
5886
 
5887
	      newname = (char *) bfd_malloc (newlen);
5888
	      if (newname == NULL)
5889
		return FALSE;
5890
	      newname[0] = leading_char;
5891
	      memcpy (newname + (leading_char != '\0'), name, namelen);
5892
 
5893
	      /* Check the hidden versioned definition.  */
5894
	      p = newname + namelen;
5895
	      *p++ = ELF_VER_CHR;
5896
	      memcpy (p, verstr, verlen + 1);
5897
	      newh = elf_link_hash_lookup (elf_hash_table (info),
5898
					   newname, FALSE, FALSE,
5899
					   FALSE);
5900
	      if (newh == NULL
5901
		  || (newh->root.type != bfd_link_hash_defined
5902
		      && newh->root.type != bfd_link_hash_defweak))
5903
		{
5904
		  /* Check the default versioned definition.  */
5905
		  *p++ = ELF_VER_CHR;
5906
		  memcpy (p, verstr, verlen + 1);
5907
		  newh = elf_link_hash_lookup (elf_hash_table (info),
5908
					       newname, FALSE, FALSE,
5909
					       FALSE);
5910
		}
5911
	      free (newname);
5912
 
5913
	      /* Mark this version if there is a definition and it is
5914
		 not defined in a shared object.  */
5915
	      if (newh != NULL
5916
		  && !newh->def_dynamic
5917
		  && (newh->root.type == bfd_link_hash_defined
5918
		      || newh->root.type == bfd_link_hash_defweak))
5919
		d->symver = 1;
5920
	    }
5921
 
5922
      /* Attach all the symbols to their version information.  */
5923
      asvinfo.info = info;
5924
      asvinfo.failed = FALSE;
5925
 
5926
      elf_link_hash_traverse (elf_hash_table (info),
5927
			      _bfd_elf_link_assign_sym_version,
5928
			      &asvinfo);
5929
      if (asvinfo.failed)
5930
	return FALSE;
5931
 
5932
      if (!info->allow_undefined_version)
5933
	{
5934
	  /* Check if all global versions have a definition.  */
5935
	  all_defined = TRUE;
5936
	  for (t = info->version_info; t != NULL; t = t->next)
5937
	    for (d = t->globals.list; d != NULL; d = d->next)
5938
	      if (d->literal && !d->symver && !d->script)
5939
		{
5940
		  (*_bfd_error_handler)
5941
		    (_("%s: undefined version: %s"),
5942
		     d->pattern, t->name);
5943
		  all_defined = FALSE;
5944
		}
5945
 
5946
	  if (!all_defined)
5947
	    {
5948
	      bfd_set_error (bfd_error_bad_value);
5949
	      return FALSE;
5950
	    }
5951
	}
5952
 
5953
      /* Find all symbols which were defined in a dynamic object and make
5954
	 the backend pick a reasonable value for them.  */
5955
      elf_link_hash_traverse (elf_hash_table (info),
5956
			      _bfd_elf_adjust_dynamic_symbol,
5957
			      &eif);
5958
      if (eif.failed)
5959
	return FALSE;
5960
 
5961
      /* Add some entries to the .dynamic section.  We fill in some of the
5962
	 values later, in bfd_elf_final_link, but we must add the entries
5963
	 now so that we know the final size of the .dynamic section.  */
5964
 
5965
      /* If there are initialization and/or finalization functions to
5966
	 call then add the corresponding DT_INIT/DT_FINI entries.  */
5967
      h = (info->init_function
5968
	   ? elf_link_hash_lookup (elf_hash_table (info),
5969
				   info->init_function, FALSE,
5970
				   FALSE, FALSE)
5971
	   : NULL);
5972
      if (h != NULL
5973
	  && (h->ref_regular
5974
	      || h->def_regular))
5975
	{
5976
	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5977
	    return FALSE;
5978
	}
5979
      h = (info->fini_function
5980
	   ? elf_link_hash_lookup (elf_hash_table (info),
5981
				   info->fini_function, FALSE,
5982
				   FALSE, FALSE)
5983
	   : NULL);
5984
      if (h != NULL
5985
	  && (h->ref_regular
5986
	      || h->def_regular))
5987
	{
5988
	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5989
	    return FALSE;
5990
	}
5991
 
5992
      s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5993
      if (s != NULL && s->linker_has_input)
5994
	{
5995
	  /* DT_PREINIT_ARRAY is not allowed in shared library.  */
6324 serge 5996
	  if (! bfd_link_executable (info))
5197 serge 5997
	    {
5998
	      bfd *sub;
5999
	      asection *o;
6000
 
6001
	      for (sub = info->input_bfds; sub != NULL;
6324 serge 6002
		   sub = sub->link.next)
5197 serge 6003
		if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6004
		  for (o = sub->sections; o != NULL; o = o->next)
6005
		    if (elf_section_data (o)->this_hdr.sh_type
6006
			== SHT_PREINIT_ARRAY)
6007
		      {
6008
			(*_bfd_error_handler)
6009
			  (_("%B: .preinit_array section is not allowed in DSO"),
6010
			   sub);
6011
			break;
6012
		      }
6013
 
6014
	      bfd_set_error (bfd_error_nonrepresentable_section);
6015
	      return FALSE;
6016
	    }
6017
 
6018
	  if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6019
	      || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6020
	    return FALSE;
6021
	}
6022
      s = bfd_get_section_by_name (output_bfd, ".init_array");
6023
      if (s != NULL && s->linker_has_input)
6024
	{
6025
	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6026
	      || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6027
	    return FALSE;
6028
	}
6029
      s = bfd_get_section_by_name (output_bfd, ".fini_array");
6030
      if (s != NULL && s->linker_has_input)
6031
	{
6032
	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6033
	      || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6034
	    return FALSE;
6035
	}
6036
 
6037
      dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6038
      /* If .dynstr is excluded from the link, we don't want any of
6039
	 these tags.  Strictly, we should be checking each section
6040
	 individually;  This quick check covers for the case where
6041
	 someone does a /DISCARD/ : { *(*) }.  */
6042
      if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6043
	{
6044
	  bfd_size_type strsize;
6045
 
6046
	  strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6047
	  if ((info->emit_hash
6048
	       && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6049
	      || (info->emit_gnu_hash
6050
		  && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6051
	      || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6052
	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6053
	      || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6054
	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6055
					      bed->s->sizeof_sym))
6056
	    return FALSE;
6057
	}
6058
    }
6059
 
6324 serge 6060
  if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6061
    return FALSE;
6062
 
5197 serge 6063
  /* The backend must work out the sizes of all the other dynamic
6064
     sections.  */
6065
  if (dynobj != NULL
6066
      && bed->elf_backend_size_dynamic_sections != NULL
6067
      && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6068
    return FALSE;
6069
 
6070
  if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6071
    {
6072
      unsigned long section_sym_count;
6073
      struct bfd_elf_version_tree *verdefs;
6074
      asection *s;
6075
 
6076
      /* Set up the version definition section.  */
6077
      s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6078
      BFD_ASSERT (s != NULL);
6079
 
6080
      /* We may have created additional version definitions if we are
6081
	 just linking a regular application.  */
6082
      verdefs = info->version_info;
6083
 
6084
      /* Skip anonymous version tag.  */
6085
      if (verdefs != NULL && verdefs->vernum == 0)
6086
	verdefs = verdefs->next;
6087
 
6088
      if (verdefs == NULL && !info->create_default_symver)
6089
	s->flags |= SEC_EXCLUDE;
6090
      else
6091
	{
6092
	  unsigned int cdefs;
6093
	  bfd_size_type size;
6094
	  struct bfd_elf_version_tree *t;
6095
	  bfd_byte *p;
6096
	  Elf_Internal_Verdef def;
6097
	  Elf_Internal_Verdaux defaux;
6098
	  struct bfd_link_hash_entry *bh;
6099
	  struct elf_link_hash_entry *h;
6100
	  const char *name;
6101
 
6102
	  cdefs = 0;
6103
	  size = 0;
6104
 
6105
	  /* Make space for the base version.  */
6106
	  size += sizeof (Elf_External_Verdef);
6107
	  size += sizeof (Elf_External_Verdaux);
6108
	  ++cdefs;
6109
 
6110
	  /* Make space for the default version.  */
6111
	  if (info->create_default_symver)
6112
	    {
6113
	      size += sizeof (Elf_External_Verdef);
6114
	      ++cdefs;
6115
	    }
6116
 
6117
	  for (t = verdefs; t != NULL; t = t->next)
6118
	    {
6119
	      struct bfd_elf_version_deps *n;
6120
 
6121
	      /* Don't emit base version twice.  */
6122
	      if (t->vernum == 0)
6123
		continue;
6124
 
6125
	      size += sizeof (Elf_External_Verdef);
6126
	      size += sizeof (Elf_External_Verdaux);
6127
	      ++cdefs;
6128
 
6129
	      for (n = t->deps; n != NULL; n = n->next)
6130
		size += sizeof (Elf_External_Verdaux);
6131
	    }
6132
 
6133
	  s->size = size;
6134
	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6135
	  if (s->contents == NULL && s->size != 0)
6136
	    return FALSE;
6137
 
6138
	  /* Fill in the version definition section.  */
6139
 
6140
	  p = s->contents;
6141
 
6142
	  def.vd_version = VER_DEF_CURRENT;
6143
	  def.vd_flags = VER_FLG_BASE;
6144
	  def.vd_ndx = 1;
6145
	  def.vd_cnt = 1;
6146
	  if (info->create_default_symver)
6147
	    {
6148
	      def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6149
	      def.vd_next = sizeof (Elf_External_Verdef);
6150
	    }
6151
	  else
6152
	    {
6153
	      def.vd_aux = sizeof (Elf_External_Verdef);
6154
	      def.vd_next = (sizeof (Elf_External_Verdef)
6155
			     + sizeof (Elf_External_Verdaux));
6156
	    }
6157
 
6158
	  if (soname_indx != (bfd_size_type) -1)
6159
	    {
6160
	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6161
				      soname_indx);
6162
	      def.vd_hash = bfd_elf_hash (soname);
6163
	      defaux.vda_name = soname_indx;
6164
	      name = soname;
6165
	    }
6166
	  else
6167
	    {
6168
	      bfd_size_type indx;
6169
 
6170
	      name = lbasename (output_bfd->filename);
6171
	      def.vd_hash = bfd_elf_hash (name);
6172
	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6173
					  name, FALSE);
6174
	      if (indx == (bfd_size_type) -1)
6175
		return FALSE;
6176
	      defaux.vda_name = indx;
6177
	    }
6178
	  defaux.vda_next = 0;
6179
 
6180
	  _bfd_elf_swap_verdef_out (output_bfd, &def,
6181
				    (Elf_External_Verdef *) p);
6182
	  p += sizeof (Elf_External_Verdef);
6183
	  if (info->create_default_symver)
6184
	    {
6185
	      /* Add a symbol representing this version.  */
6186
	      bh = NULL;
6187
	      if (! (_bfd_generic_link_add_one_symbol
6188
		     (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6189
		      0, NULL, FALSE,
6190
		      get_elf_backend_data (dynobj)->collect, &bh)))
6191
		return FALSE;
6192
	      h = (struct elf_link_hash_entry *) bh;
6193
	      h->non_elf = 0;
6194
	      h->def_regular = 1;
6195
	      h->type = STT_OBJECT;
6196
	      h->verinfo.vertree = NULL;
6197
 
6198
	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
6199
		return FALSE;
6200
 
6201
	      /* Create a duplicate of the base version with the same
6202
		 aux block, but different flags.  */
6203
	      def.vd_flags = 0;
6204
	      def.vd_ndx = 2;
6205
	      def.vd_aux = sizeof (Elf_External_Verdef);
6206
	      if (verdefs)
6207
		def.vd_next = (sizeof (Elf_External_Verdef)
6208
			       + sizeof (Elf_External_Verdaux));
6209
	      else
6210
		def.vd_next = 0;
6211
	      _bfd_elf_swap_verdef_out (output_bfd, &def,
6212
					(Elf_External_Verdef *) p);
6213
	      p += sizeof (Elf_External_Verdef);
6214
	    }
6215
	  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6216
				     (Elf_External_Verdaux *) p);
6217
	  p += sizeof (Elf_External_Verdaux);
6218
 
6219
	  for (t = verdefs; t != NULL; t = t->next)
6220
	    {
6221
	      unsigned int cdeps;
6222
	      struct bfd_elf_version_deps *n;
6223
 
6224
	      /* Don't emit the base version twice.  */
6225
	      if (t->vernum == 0)
6226
		continue;
6227
 
6228
	      cdeps = 0;
6229
	      for (n = t->deps; n != NULL; n = n->next)
6230
		++cdeps;
6231
 
6232
	      /* Add a symbol representing this version.  */
6233
	      bh = NULL;
6234
	      if (! (_bfd_generic_link_add_one_symbol
6235
		     (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6236
		      0, NULL, FALSE,
6237
		      get_elf_backend_data (dynobj)->collect, &bh)))
6238
		return FALSE;
6239
	      h = (struct elf_link_hash_entry *) bh;
6240
	      h->non_elf = 0;
6241
	      h->def_regular = 1;
6242
	      h->type = STT_OBJECT;
6243
	      h->verinfo.vertree = t;
6244
 
6245
	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
6246
		return FALSE;
6247
 
6248
	      def.vd_version = VER_DEF_CURRENT;
6249
	      def.vd_flags = 0;
6250
	      if (t->globals.list == NULL
6251
		  && t->locals.list == NULL
6252
		  && ! t->used)
6253
		def.vd_flags |= VER_FLG_WEAK;
6254
	      def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6255
	      def.vd_cnt = cdeps + 1;
6256
	      def.vd_hash = bfd_elf_hash (t->name);
6257
	      def.vd_aux = sizeof (Elf_External_Verdef);
6258
	      def.vd_next = 0;
6259
 
6260
	      /* If a basever node is next, it *must* be the last node in
6261
		 the chain, otherwise Verdef construction breaks.  */
6262
	      if (t->next != NULL && t->next->vernum == 0)
6263
		BFD_ASSERT (t->next->next == NULL);
6264
 
6265
	      if (t->next != NULL && t->next->vernum != 0)
6266
		def.vd_next = (sizeof (Elf_External_Verdef)
6267
			       + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6268
 
6269
	      _bfd_elf_swap_verdef_out (output_bfd, &def,
6270
					(Elf_External_Verdef *) p);
6271
	      p += sizeof (Elf_External_Verdef);
6272
 
6273
	      defaux.vda_name = h->dynstr_index;
6274
	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6275
				      h->dynstr_index);
6276
	      defaux.vda_next = 0;
6277
	      if (t->deps != NULL)
6278
		defaux.vda_next = sizeof (Elf_External_Verdaux);
6279
	      t->name_indx = defaux.vda_name;
6280
 
6281
	      _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6282
					 (Elf_External_Verdaux *) p);
6283
	      p += sizeof (Elf_External_Verdaux);
6284
 
6285
	      for (n = t->deps; n != NULL; n = n->next)
6286
		{
6287
		  if (n->version_needed == NULL)
6288
		    {
6289
		      /* This can happen if there was an error in the
6290
			 version script.  */
6291
		      defaux.vda_name = 0;
6292
		    }
6293
		  else
6294
		    {
6295
		      defaux.vda_name = n->version_needed->name_indx;
6296
		      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6297
					      defaux.vda_name);
6298
		    }
6299
		  if (n->next == NULL)
6300
		    defaux.vda_next = 0;
6301
		  else
6302
		    defaux.vda_next = sizeof (Elf_External_Verdaux);
6303
 
6304
		  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6305
					     (Elf_External_Verdaux *) p);
6306
		  p += sizeof (Elf_External_Verdaux);
6307
		}
6308
	    }
6309
 
6310
	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6311
	      || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6312
	    return FALSE;
6313
 
6314
	  elf_tdata (output_bfd)->cverdefs = cdefs;
6315
	}
6316
 
6317
      if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6318
	{
6319
	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6320
	    return FALSE;
6321
	}
6322
      else if (info->flags & DF_BIND_NOW)
6323
	{
6324
	  if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6325
	    return FALSE;
6326
	}
6327
 
6328
      if (info->flags_1)
6329
	{
6324 serge 6330
	  if (bfd_link_executable (info))
5197 serge 6331
	    info->flags_1 &= ~ (DF_1_INITFIRST
6332
				| DF_1_NODELETE
6333
				| DF_1_NOOPEN);
6334
	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6335
	    return FALSE;
6336
	}
6337
 
6338
      /* Work out the size of the version reference section.  */
6339
 
6340
      s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6341
      BFD_ASSERT (s != NULL);
6342
      {
6343
	struct elf_find_verdep_info sinfo;
6344
 
6345
	sinfo.info = info;
6346
	sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6347
	if (sinfo.vers == 0)
6348
	  sinfo.vers = 1;
6349
	sinfo.failed = FALSE;
6350
 
6351
	elf_link_hash_traverse (elf_hash_table (info),
6352
				_bfd_elf_link_find_version_dependencies,
6353
				&sinfo);
6354
	if (sinfo.failed)
6355
	  return FALSE;
6356
 
6357
	if (elf_tdata (output_bfd)->verref == NULL)
6358
	  s->flags |= SEC_EXCLUDE;
6359
	else
6360
	  {
6361
	    Elf_Internal_Verneed *t;
6362
	    unsigned int size;
6363
	    unsigned int crefs;
6364
	    bfd_byte *p;
6365
 
6366
	    /* Build the version dependency section.  */
6367
	    size = 0;
6368
	    crefs = 0;
6369
	    for (t = elf_tdata (output_bfd)->verref;
6370
		 t != NULL;
6371
		 t = t->vn_nextref)
6372
	      {
6373
		Elf_Internal_Vernaux *a;
6374
 
6375
		size += sizeof (Elf_External_Verneed);
6376
		++crefs;
6377
		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6378
		  size += sizeof (Elf_External_Vernaux);
6379
	      }
6380
 
6381
	    s->size = size;
6382
	    s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6383
	    if (s->contents == NULL)
6384
	      return FALSE;
6385
 
6386
	    p = s->contents;
6387
	    for (t = elf_tdata (output_bfd)->verref;
6388
		 t != NULL;
6389
		 t = t->vn_nextref)
6390
	      {
6391
		unsigned int caux;
6392
		Elf_Internal_Vernaux *a;
6393
		bfd_size_type indx;
6394
 
6395
		caux = 0;
6396
		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6397
		  ++caux;
6398
 
6399
		t->vn_version = VER_NEED_CURRENT;
6400
		t->vn_cnt = caux;
6401
		indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6402
					    elf_dt_name (t->vn_bfd) != NULL
6403
					    ? elf_dt_name (t->vn_bfd)
6404
					    : lbasename (t->vn_bfd->filename),
6405
					    FALSE);
6406
		if (indx == (bfd_size_type) -1)
6407
		  return FALSE;
6408
		t->vn_file = indx;
6409
		t->vn_aux = sizeof (Elf_External_Verneed);
6410
		if (t->vn_nextref == NULL)
6411
		  t->vn_next = 0;
6412
		else
6413
		  t->vn_next = (sizeof (Elf_External_Verneed)
6414
				+ caux * sizeof (Elf_External_Vernaux));
6415
 
6416
		_bfd_elf_swap_verneed_out (output_bfd, t,
6417
					   (Elf_External_Verneed *) p);
6418
		p += sizeof (Elf_External_Verneed);
6419
 
6420
		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6421
		  {
6422
		    a->vna_hash = bfd_elf_hash (a->vna_nodename);
6423
		    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6424
						a->vna_nodename, FALSE);
6425
		    if (indx == (bfd_size_type) -1)
6426
		      return FALSE;
6427
		    a->vna_name = indx;
6428
		    if (a->vna_nextptr == NULL)
6429
		      a->vna_next = 0;
6430
		    else
6431
		      a->vna_next = sizeof (Elf_External_Vernaux);
6432
 
6433
		    _bfd_elf_swap_vernaux_out (output_bfd, a,
6434
					       (Elf_External_Vernaux *) p);
6435
		    p += sizeof (Elf_External_Vernaux);
6436
		  }
6437
	      }
6438
 
6439
	    if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6440
		|| !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6441
	      return FALSE;
6442
 
6443
	    elf_tdata (output_bfd)->cverrefs = crefs;
6444
	  }
6445
      }
6446
 
6447
      if ((elf_tdata (output_bfd)->cverrefs == 0
6448
	   && elf_tdata (output_bfd)->cverdefs == 0)
6449
	  || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6450
					     §ion_sym_count) == 0)
6451
	{
6452
	  s = bfd_get_linker_section (dynobj, ".gnu.version");
6453
	  s->flags |= SEC_EXCLUDE;
6454
	}
6455
    }
6456
  return TRUE;
6457
}
6458
 
6459
/* Find the first non-excluded output section.  We'll use its
6460
   section symbol for some emitted relocs.  */
6461
void
6462
_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6463
{
6464
  asection *s;
6465
 
6466
  for (s = output_bfd->sections; s != NULL; s = s->next)
6467
    if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6468
	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6469
      {
6470
	elf_hash_table (info)->text_index_section = s;
6471
	break;
6472
      }
6473
}
6474
 
6475
/* Find two non-excluded output sections, one for code, one for data.
6476
   We'll use their section symbols for some emitted relocs.  */
6477
void
6478
_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6479
{
6480
  asection *s;
6481
 
6482
  /* Data first, since setting text_index_section changes
6483
     _bfd_elf_link_omit_section_dynsym.  */
6484
  for (s = output_bfd->sections; s != NULL; s = s->next)
6485
    if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6486
	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6487
      {
6488
	elf_hash_table (info)->data_index_section = s;
6489
	break;
6490
      }
6491
 
6492
  for (s = output_bfd->sections; s != NULL; s = s->next)
6493
    if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6494
	 == (SEC_ALLOC | SEC_READONLY))
6495
	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6496
      {
6497
	elf_hash_table (info)->text_index_section = s;
6498
	break;
6499
      }
6500
 
6501
  if (elf_hash_table (info)->text_index_section == NULL)
6502
    elf_hash_table (info)->text_index_section
6503
      = elf_hash_table (info)->data_index_section;
6504
}
6505
 
6506
bfd_boolean
6507
bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6508
{
6509
  const struct elf_backend_data *bed;
6510
 
6511
  if (!is_elf_hash_table (info->hash))
6512
    return TRUE;
6513
 
6514
  bed = get_elf_backend_data (output_bfd);
6515
  (*bed->elf_backend_init_index_section) (output_bfd, info);
6516
 
6517
  if (elf_hash_table (info)->dynamic_sections_created)
6518
    {
6519
      bfd *dynobj;
6520
      asection *s;
6521
      bfd_size_type dynsymcount;
6522
      unsigned long section_sym_count;
6523
      unsigned int dtagcount;
6524
 
6525
      dynobj = elf_hash_table (info)->dynobj;
6526
 
6527
      /* Assign dynsym indicies.  In a shared library we generate a
6528
	 section symbol for each output section, which come first.
6529
	 Next come all of the back-end allocated local dynamic syms,
6530
	 followed by the rest of the global symbols.  */
6531
 
6532
      dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6533
						    §ion_sym_count);
6534
 
6535
      /* Work out the size of the symbol version section.  */
6536
      s = bfd_get_linker_section (dynobj, ".gnu.version");
6537
      BFD_ASSERT (s != NULL);
6538
      if (dynsymcount != 0
6539
	  && (s->flags & SEC_EXCLUDE) == 0)
6540
	{
6541
	  s->size = dynsymcount * sizeof (Elf_External_Versym);
6542
	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6543
	  if (s->contents == NULL)
6544
	    return FALSE;
6545
 
6546
	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6547
	    return FALSE;
6548
	}
6549
 
6550
      /* Set the size of the .dynsym and .hash sections.  We counted
6551
	 the number of dynamic symbols in elf_link_add_object_symbols.
6552
	 We will build the contents of .dynsym and .hash when we build
6553
	 the final symbol table, because until then we do not know the
6554
	 correct value to give the symbols.  We built the .dynstr
6555
	 section as we went along in elf_link_add_object_symbols.  */
6324 serge 6556
      s = elf_hash_table (info)->dynsym;
5197 serge 6557
      BFD_ASSERT (s != NULL);
6558
      s->size = dynsymcount * bed->s->sizeof_sym;
6559
 
6560
      if (dynsymcount != 0)
6561
	{
6562
	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6563
	  if (s->contents == NULL)
6564
	    return FALSE;
6565
 
6566
	  /* The first entry in .dynsym is a dummy symbol.
6567
	     Clear all the section syms, in case we don't output them all.  */
6568
	  ++section_sym_count;
6569
	  memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6570
	}
6571
 
6572
      elf_hash_table (info)->bucketcount = 0;
6573
 
6574
      /* Compute the size of the hashing table.  As a side effect this
6575
	 computes the hash values for all the names we export.  */
6576
      if (info->emit_hash)
6577
	{
6578
	  unsigned long int *hashcodes;
6579
	  struct hash_codes_info hashinf;
6580
	  bfd_size_type amt;
6581
	  unsigned long int nsyms;
6582
	  size_t bucketcount;
6583
	  size_t hash_entry_size;
6584
 
6585
	  /* Compute the hash values for all exported symbols.  At the same
6586
	     time store the values in an array so that we could use them for
6587
	     optimizations.  */
6588
	  amt = dynsymcount * sizeof (unsigned long int);
6589
	  hashcodes = (unsigned long int *) bfd_malloc (amt);
6590
	  if (hashcodes == NULL)
6591
	    return FALSE;
6592
	  hashinf.hashcodes = hashcodes;
6593
	  hashinf.error = FALSE;
6594
 
6595
	  /* Put all hash values in HASHCODES.  */
6596
	  elf_link_hash_traverse (elf_hash_table (info),
6597
				  elf_collect_hash_codes, &hashinf);
6598
	  if (hashinf.error)
6599
	    {
6600
	      free (hashcodes);
6601
	      return FALSE;
6602
	    }
6603
 
6604
	  nsyms = hashinf.hashcodes - hashcodes;
6605
	  bucketcount
6606
	    = compute_bucket_count (info, hashcodes, nsyms, 0);
6607
	  free (hashcodes);
6608
 
6609
	  if (bucketcount == 0)
6610
	    return FALSE;
6611
 
6612
	  elf_hash_table (info)->bucketcount = bucketcount;
6613
 
6614
	  s = bfd_get_linker_section (dynobj, ".hash");
6615
	  BFD_ASSERT (s != NULL);
6616
	  hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6617
	  s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6618
	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6619
	  if (s->contents == NULL)
6620
	    return FALSE;
6621
 
6622
	  bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6623
	  bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6624
		   s->contents + hash_entry_size);
6625
	}
6626
 
6627
      if (info->emit_gnu_hash)
6628
	{
6629
	  size_t i, cnt;
6630
	  unsigned char *contents;
6631
	  struct collect_gnu_hash_codes cinfo;
6632
	  bfd_size_type amt;
6633
	  size_t bucketcount;
6634
 
6635
	  memset (&cinfo, 0, sizeof (cinfo));
6636
 
6637
	  /* Compute the hash values for all exported symbols.  At the same
6638
	     time store the values in an array so that we could use them for
6639
	     optimizations.  */
6640
	  amt = dynsymcount * 2 * sizeof (unsigned long int);
6641
	  cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6642
	  if (cinfo.hashcodes == NULL)
6643
	    return FALSE;
6644
 
6645
	  cinfo.hashval = cinfo.hashcodes + dynsymcount;
6646
	  cinfo.min_dynindx = -1;
6647
	  cinfo.output_bfd = output_bfd;
6648
	  cinfo.bed = bed;
6649
 
6650
	  /* Put all hash values in HASHCODES.  */
6651
	  elf_link_hash_traverse (elf_hash_table (info),
6652
				  elf_collect_gnu_hash_codes, &cinfo);
6653
	  if (cinfo.error)
6654
	    {
6655
	      free (cinfo.hashcodes);
6656
	      return FALSE;
6657
	    }
6658
 
6659
	  bucketcount
6660
	    = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6661
 
6662
	  if (bucketcount == 0)
6663
	    {
6664
	      free (cinfo.hashcodes);
6665
	      return FALSE;
6666
	    }
6667
 
6668
	  s = bfd_get_linker_section (dynobj, ".gnu.hash");
6669
	  BFD_ASSERT (s != NULL);
6670
 
6671
	  if (cinfo.nsyms == 0)
6672
	    {
6673
	      /* Empty .gnu.hash section is special.  */
6674
	      BFD_ASSERT (cinfo.min_dynindx == -1);
6675
	      free (cinfo.hashcodes);
6676
	      s->size = 5 * 4 + bed->s->arch_size / 8;
6677
	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6678
	      if (contents == NULL)
6679
		return FALSE;
6680
	      s->contents = contents;
6681
	      /* 1 empty bucket.  */
6682
	      bfd_put_32 (output_bfd, 1, contents);
6683
	      /* SYMIDX above the special symbol 0.  */
6684
	      bfd_put_32 (output_bfd, 1, contents + 4);
6685
	      /* Just one word for bitmask.  */
6686
	      bfd_put_32 (output_bfd, 1, contents + 8);
6687
	      /* Only hash fn bloom filter.  */
6688
	      bfd_put_32 (output_bfd, 0, contents + 12);
6689
	      /* No hashes are valid - empty bitmask.  */
6690
	      bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6691
	      /* No hashes in the only bucket.  */
6692
	      bfd_put_32 (output_bfd, 0,
6693
			  contents + 16 + bed->s->arch_size / 8);
6694
	    }
6695
	  else
6696
	    {
6697
	      unsigned long int maskwords, maskbitslog2, x;
6698
	      BFD_ASSERT (cinfo.min_dynindx != -1);
6699
 
6700
	      x = cinfo.nsyms;
6701
	      maskbitslog2 = 1;
6702
	      while ((x >>= 1) != 0)
6703
		++maskbitslog2;
6704
	      if (maskbitslog2 < 3)
6705
		maskbitslog2 = 5;
6706
	      else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6707
		maskbitslog2 = maskbitslog2 + 3;
6708
	      else
6709
		maskbitslog2 = maskbitslog2 + 2;
6710
	      if (bed->s->arch_size == 64)
6711
		{
6712
		  if (maskbitslog2 == 5)
6713
		    maskbitslog2 = 6;
6714
		  cinfo.shift1 = 6;
6715
		}
6716
	      else
6717
		cinfo.shift1 = 5;
6718
	      cinfo.mask = (1 << cinfo.shift1) - 1;
6719
	      cinfo.shift2 = maskbitslog2;
6720
	      cinfo.maskbits = 1 << maskbitslog2;
6721
	      maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6722
	      amt = bucketcount * sizeof (unsigned long int) * 2;
6723
	      amt += maskwords * sizeof (bfd_vma);
6724
	      cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6725
	      if (cinfo.bitmask == NULL)
6726
		{
6727
		  free (cinfo.hashcodes);
6728
		  return FALSE;
6729
		}
6730
 
6731
	      cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6732
	      cinfo.indx = cinfo.counts + bucketcount;
6733
	      cinfo.symindx = dynsymcount - cinfo.nsyms;
6734
	      memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6735
 
6736
	      /* Determine how often each hash bucket is used.  */
6737
	      memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6738
	      for (i = 0; i < cinfo.nsyms; ++i)
6739
		++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6740
 
6741
	      for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6742
		if (cinfo.counts[i] != 0)
6743
		  {
6744
		    cinfo.indx[i] = cnt;
6745
		    cnt += cinfo.counts[i];
6746
		  }
6747
	      BFD_ASSERT (cnt == dynsymcount);
6748
	      cinfo.bucketcount = bucketcount;
6749
	      cinfo.local_indx = cinfo.min_dynindx;
6750
 
6751
	      s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6752
	      s->size += cinfo.maskbits / 8;
6753
	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6754
	      if (contents == NULL)
6755
		{
6756
		  free (cinfo.bitmask);
6757
		  free (cinfo.hashcodes);
6758
		  return FALSE;
6759
		}
6760
 
6761
	      s->contents = contents;
6762
	      bfd_put_32 (output_bfd, bucketcount, contents);
6763
	      bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6764
	      bfd_put_32 (output_bfd, maskwords, contents + 8);
6765
	      bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6766
	      contents += 16 + cinfo.maskbits / 8;
6767
 
6768
	      for (i = 0; i < bucketcount; ++i)
6769
		{
6770
		  if (cinfo.counts[i] == 0)
6771
		    bfd_put_32 (output_bfd, 0, contents);
6772
		  else
6773
		    bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6774
		  contents += 4;
6775
		}
6776
 
6777
	      cinfo.contents = contents;
6778
 
6779
	      /* Renumber dynamic symbols, populate .gnu.hash section.  */
6780
	      elf_link_hash_traverse (elf_hash_table (info),
6781
				      elf_renumber_gnu_hash_syms, &cinfo);
6782
 
6783
	      contents = s->contents + 16;
6784
	      for (i = 0; i < maskwords; ++i)
6785
		{
6786
		  bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6787
			   contents);
6788
		  contents += bed->s->arch_size / 8;
6789
		}
6790
 
6791
	      free (cinfo.bitmask);
6792
	      free (cinfo.hashcodes);
6793
	    }
6794
	}
6795
 
6796
      s = bfd_get_linker_section (dynobj, ".dynstr");
6797
      BFD_ASSERT (s != NULL);
6798
 
6799
      elf_finalize_dynstr (output_bfd, info);
6800
 
6801
      s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6802
 
6803
      for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6804
	if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6805
	  return FALSE;
6806
    }
6807
 
6808
  return TRUE;
6809
}
6810
 
6811
/* Make sure sec_info_type is cleared if sec_info is cleared too.  */
6812
 
6813
static void
6814
merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6815
			    asection *sec)
6816
{
6817
  BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6818
  sec->sec_info_type = SEC_INFO_TYPE_NONE;
6819
}
6820
 
6821
/* Finish SHF_MERGE section merging.  */
6822
 
6823
bfd_boolean
6324 serge 6824
_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
5197 serge 6825
{
6826
  bfd *ibfd;
6827
  asection *sec;
6828
 
6829
  if (!is_elf_hash_table (info->hash))
6830
    return FALSE;
6831
 
6324 serge 6832
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6833
    if ((ibfd->flags & DYNAMIC) == 0
6834
	&& bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6835
	&& (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6836
	    == get_elf_backend_data (obfd)->s->elfclass))
5197 serge 6837
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6838
	if ((sec->flags & SEC_MERGE) != 0
6839
	    && !bfd_is_abs_section (sec->output_section))
6840
	  {
6841
	    struct bfd_elf_section_data *secdata;
6842
 
6843
	    secdata = elf_section_data (sec);
6324 serge 6844
	    if (! _bfd_add_merge_section (obfd,
5197 serge 6845
					  &elf_hash_table (info)->merge_info,
6846
					  sec, &secdata->sec_info))
6847
	      return FALSE;
6848
	    else if (secdata->sec_info)
6849
	      sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6850
	  }
6851
 
6852
  if (elf_hash_table (info)->merge_info != NULL)
6324 serge 6853
    _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
5197 serge 6854
			 merge_sections_remove_hook);
6855
  return TRUE;
6856
}
6857
 
6858
/* Create an entry in an ELF linker hash table.  */
6859
 
6860
struct bfd_hash_entry *
6861
_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6862
			    struct bfd_hash_table *table,
6863
			    const char *string)
6864
{
6865
  /* Allocate the structure if it has not already been allocated by a
6866
     subclass.  */
6867
  if (entry == NULL)
6868
    {
6869
      entry = (struct bfd_hash_entry *)
6324 serge 6870
	bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
5197 serge 6871
      if (entry == NULL)
6872
	return entry;
6873
    }
6874
 
6875
  /* Call the allocation method of the superclass.  */
6876
  entry = _bfd_link_hash_newfunc (entry, table, string);
6877
  if (entry != NULL)
6878
    {
6879
      struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6880
      struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6881
 
6882
      /* Set local fields.  */
6883
      ret->indx = -1;
6884
      ret->dynindx = -1;
6885
      ret->got = htab->init_got_refcount;
6886
      ret->plt = htab->init_plt_refcount;
6887
      memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6888
			      - offsetof (struct elf_link_hash_entry, size)));
6889
      /* Assume that we have been called by a non-ELF symbol reader.
6890
	 This flag is then reset by the code which reads an ELF input
6891
	 file.  This ensures that a symbol created by a non-ELF symbol
6892
	 reader will have the flag set correctly.  */
6893
      ret->non_elf = 1;
6894
    }
6895
 
6896
  return entry;
6897
}
6898
 
6899
/* Copy data from an indirect symbol to its direct symbol, hiding the
6900
   old indirect symbol.  Also used for copying flags to a weakdef.  */
6901
 
6902
void
6903
_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6904
				  struct elf_link_hash_entry *dir,
6905
				  struct elf_link_hash_entry *ind)
6906
{
6907
  struct elf_link_hash_table *htab;
6908
 
6909
  /* Copy down any references that we may have already seen to the
6324 serge 6910
     symbol which just became indirect if DIR isn't a hidden versioned
6911
     symbol.  */
5197 serge 6912
 
6324 serge 6913
  if (dir->versioned != versioned_hidden)
6914
    {
6915
      dir->ref_dynamic |= ind->ref_dynamic;
6916
      dir->ref_regular |= ind->ref_regular;
6917
      dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6918
      dir->non_got_ref |= ind->non_got_ref;
6919
      dir->needs_plt |= ind->needs_plt;
6920
      dir->pointer_equality_needed |= ind->pointer_equality_needed;
6921
    }
5197 serge 6922
 
6923
  if (ind->root.type != bfd_link_hash_indirect)
6924
    return;
6925
 
6926
  /* Copy over the global and procedure linkage table refcount entries.
6927
     These may have been already set up by a check_relocs routine.  */
6928
  htab = elf_hash_table (info);
6929
  if (ind->got.refcount > htab->init_got_refcount.refcount)
6930
    {
6931
      if (dir->got.refcount < 0)
6932
	dir->got.refcount = 0;
6933
      dir->got.refcount += ind->got.refcount;
6934
      ind->got.refcount = htab->init_got_refcount.refcount;
6935
    }
6936
 
6937
  if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6938
    {
6939
      if (dir->plt.refcount < 0)
6940
	dir->plt.refcount = 0;
6941
      dir->plt.refcount += ind->plt.refcount;
6942
      ind->plt.refcount = htab->init_plt_refcount.refcount;
6943
    }
6944
 
6945
  if (ind->dynindx != -1)
6946
    {
6947
      if (dir->dynindx != -1)
6948
	_bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6949
      dir->dynindx = ind->dynindx;
6950
      dir->dynstr_index = ind->dynstr_index;
6951
      ind->dynindx = -1;
6952
      ind->dynstr_index = 0;
6953
    }
6954
}
6955
 
6956
void
6957
_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6958
				struct elf_link_hash_entry *h,
6959
				bfd_boolean force_local)
6960
{
6961
  /* STT_GNU_IFUNC symbol must go through PLT.  */
6962
  if (h->type != STT_GNU_IFUNC)
6963
    {
6964
      h->plt = elf_hash_table (info)->init_plt_offset;
6965
      h->needs_plt = 0;
6966
    }
6967
  if (force_local)
6968
    {
6969
      h->forced_local = 1;
6970
      if (h->dynindx != -1)
6971
	{
6972
	  h->dynindx = -1;
6973
	  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6974
				  h->dynstr_index);
6975
	}
6976
    }
6977
}
6978
 
6979
/* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
6980
   caller.  */
6981
 
6982
bfd_boolean
6983
_bfd_elf_link_hash_table_init
6984
  (struct elf_link_hash_table *table,
6985
   bfd *abfd,
6986
   struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6987
				      struct bfd_hash_table *,
6988
				      const char *),
6989
   unsigned int entsize,
6990
   enum elf_target_id target_id)
6991
{
6992
  bfd_boolean ret;
6993
  int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6994
 
6995
  table->init_got_refcount.refcount = can_refcount - 1;
6996
  table->init_plt_refcount.refcount = can_refcount - 1;
6997
  table->init_got_offset.offset = -(bfd_vma) 1;
6998
  table->init_plt_offset.offset = -(bfd_vma) 1;
6999
  /* The first dynamic symbol is a dummy.  */
7000
  table->dynsymcount = 1;
7001
 
7002
  ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7003
 
7004
  table->root.type = bfd_link_elf_hash_table;
7005
  table->hash_table_id = target_id;
7006
 
7007
  return ret;
7008
}
7009
 
7010
/* Create an ELF linker hash table.  */
7011
 
7012
struct bfd_link_hash_table *
7013
_bfd_elf_link_hash_table_create (bfd *abfd)
7014
{
7015
  struct elf_link_hash_table *ret;
7016
  bfd_size_type amt = sizeof (struct elf_link_hash_table);
7017
 
7018
  ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7019
  if (ret == NULL)
7020
    return NULL;
7021
 
7022
  if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7023
				       sizeof (struct elf_link_hash_entry),
7024
				       GENERIC_ELF_DATA))
7025
    {
7026
      free (ret);
7027
      return NULL;
7028
    }
6324 serge 7029
  ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
5197 serge 7030
 
7031
  return &ret->root;
7032
}
7033
 
7034
/* Destroy an ELF linker hash table.  */
7035
 
7036
void
6324 serge 7037
_bfd_elf_link_hash_table_free (bfd *obfd)
5197 serge 7038
{
6324 serge 7039
  struct elf_link_hash_table *htab;
7040
 
7041
  htab = (struct elf_link_hash_table *) obfd->link.hash;
5197 serge 7042
  if (htab->dynstr != NULL)
7043
    _bfd_elf_strtab_free (htab->dynstr);
7044
  _bfd_merge_sections_free (htab->merge_info);
6324 serge 7045
  _bfd_generic_link_hash_table_free (obfd);
5197 serge 7046
}
7047
 
7048
/* This is a hook for the ELF emulation code in the generic linker to
7049
   tell the backend linker what file name to use for the DT_NEEDED
7050
   entry for a dynamic object.  */
7051
 
7052
void
7053
bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7054
{
7055
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7056
      && bfd_get_format (abfd) == bfd_object)
7057
    elf_dt_name (abfd) = name;
7058
}
7059
 
7060
int
7061
bfd_elf_get_dyn_lib_class (bfd *abfd)
7062
{
7063
  int lib_class;
7064
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7065
      && bfd_get_format (abfd) == bfd_object)
7066
    lib_class = elf_dyn_lib_class (abfd);
7067
  else
7068
    lib_class = 0;
7069
  return lib_class;
7070
}
7071
 
7072
void
7073
bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7074
{
7075
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7076
      && bfd_get_format (abfd) == bfd_object)
7077
    elf_dyn_lib_class (abfd) = lib_class;
7078
}
7079
 
7080
/* Get the list of DT_NEEDED entries for a link.  This is a hook for
7081
   the linker ELF emulation code.  */
7082
 
7083
struct bfd_link_needed_list *
7084
bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7085
			 struct bfd_link_info *info)
7086
{
7087
  if (! is_elf_hash_table (info->hash))
7088
    return NULL;
7089
  return elf_hash_table (info)->needed;
7090
}
7091
 
7092
/* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
7093
   hook for the linker ELF emulation code.  */
7094
 
7095
struct bfd_link_needed_list *
7096
bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7097
			  struct bfd_link_info *info)
7098
{
7099
  if (! is_elf_hash_table (info->hash))
7100
    return NULL;
7101
  return elf_hash_table (info)->runpath;
7102
}
7103
 
7104
/* Get the name actually used for a dynamic object for a link.  This
7105
   is the SONAME entry if there is one.  Otherwise, it is the string
7106
   passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
7107
 
7108
const char *
7109
bfd_elf_get_dt_soname (bfd *abfd)
7110
{
7111
  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7112
      && bfd_get_format (abfd) == bfd_object)
7113
    return elf_dt_name (abfd);
7114
  return NULL;
7115
}
7116
 
7117
/* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
7118
   the ELF linker emulation code.  */
7119
 
7120
bfd_boolean
7121
bfd_elf_get_bfd_needed_list (bfd *abfd,
7122
			     struct bfd_link_needed_list **pneeded)
7123
{
7124
  asection *s;
7125
  bfd_byte *dynbuf = NULL;
7126
  unsigned int elfsec;
7127
  unsigned long shlink;
7128
  bfd_byte *extdyn, *extdynend;
7129
  size_t extdynsize;
7130
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7131
 
7132
  *pneeded = NULL;
7133
 
7134
  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7135
      || bfd_get_format (abfd) != bfd_object)
7136
    return TRUE;
7137
 
7138
  s = bfd_get_section_by_name (abfd, ".dynamic");
7139
  if (s == NULL || s->size == 0)
7140
    return TRUE;
7141
 
7142
  if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7143
    goto error_return;
7144
 
7145
  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7146
  if (elfsec == SHN_BAD)
7147
    goto error_return;
7148
 
7149
  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7150
 
7151
  extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7152
  swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7153
 
7154
  extdyn = dynbuf;
7155
  extdynend = extdyn + s->size;
7156
  for (; extdyn < extdynend; extdyn += extdynsize)
7157
    {
7158
      Elf_Internal_Dyn dyn;
7159
 
7160
      (*swap_dyn_in) (abfd, extdyn, &dyn);
7161
 
7162
      if (dyn.d_tag == DT_NULL)
7163
	break;
7164
 
7165
      if (dyn.d_tag == DT_NEEDED)
7166
	{
7167
	  const char *string;
7168
	  struct bfd_link_needed_list *l;
7169
	  unsigned int tagv = dyn.d_un.d_val;
7170
	  bfd_size_type amt;
7171
 
7172
	  string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7173
	  if (string == NULL)
7174
	    goto error_return;
7175
 
7176
	  amt = sizeof *l;
7177
	  l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7178
	  if (l == NULL)
7179
	    goto error_return;
7180
 
7181
	  l->by = abfd;
7182
	  l->name = string;
7183
	  l->next = *pneeded;
7184
	  *pneeded = l;
7185
	}
7186
    }
7187
 
7188
  free (dynbuf);
7189
 
7190
  return TRUE;
7191
 
7192
 error_return:
7193
  if (dynbuf != NULL)
7194
    free (dynbuf);
7195
  return FALSE;
7196
}
7197
 
7198
struct elf_symbuf_symbol
7199
{
7200
  unsigned long st_name;	/* Symbol name, index in string tbl */
7201
  unsigned char st_info;	/* Type and binding attributes */
7202
  unsigned char st_other;	/* Visibilty, and target specific */
7203
};
7204
 
7205
struct elf_symbuf_head
7206
{
7207
  struct elf_symbuf_symbol *ssym;
7208
  bfd_size_type count;
7209
  unsigned int st_shndx;
7210
};
7211
 
7212
struct elf_symbol
7213
{
7214
  union
7215
    {
7216
      Elf_Internal_Sym *isym;
7217
      struct elf_symbuf_symbol *ssym;
7218
    } u;
7219
  const char *name;
7220
};
7221
 
7222
/* Sort references to symbols by ascending section number.  */
7223
 
7224
static int
7225
elf_sort_elf_symbol (const void *arg1, const void *arg2)
7226
{
7227
  const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7228
  const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7229
 
7230
  return s1->st_shndx - s2->st_shndx;
7231
}
7232
 
7233
static int
7234
elf_sym_name_compare (const void *arg1, const void *arg2)
7235
{
7236
  const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7237
  const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7238
  return strcmp (s1->name, s2->name);
7239
}
7240
 
7241
static struct elf_symbuf_head *
7242
elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7243
{
7244
  Elf_Internal_Sym **ind, **indbufend, **indbuf;
7245
  struct elf_symbuf_symbol *ssym;
7246
  struct elf_symbuf_head *ssymbuf, *ssymhead;
7247
  bfd_size_type i, shndx_count, total_size;
7248
 
7249
  indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7250
  if (indbuf == NULL)
7251
    return NULL;
7252
 
7253
  for (ind = indbuf, i = 0; i < symcount; i++)
7254
    if (isymbuf[i].st_shndx != SHN_UNDEF)
7255
      *ind++ = &isymbuf[i];
7256
  indbufend = ind;
7257
 
7258
  qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7259
	 elf_sort_elf_symbol);
7260
 
7261
  shndx_count = 0;
7262
  if (indbufend > indbuf)
7263
    for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7264
      if (ind[0]->st_shndx != ind[1]->st_shndx)
7265
	shndx_count++;
7266
 
7267
  total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7268
		+ (indbufend - indbuf) * sizeof (*ssym));
7269
  ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7270
  if (ssymbuf == NULL)
7271
    {
7272
      free (indbuf);
7273
      return NULL;
7274
    }
7275
 
7276
  ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7277
  ssymbuf->ssym = NULL;
7278
  ssymbuf->count = shndx_count;
7279
  ssymbuf->st_shndx = 0;
7280
  for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7281
    {
7282
      if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7283
	{
7284
	  ssymhead++;
7285
	  ssymhead->ssym = ssym;
7286
	  ssymhead->count = 0;
7287
	  ssymhead->st_shndx = (*ind)->st_shndx;
7288
	}
7289
      ssym->st_name = (*ind)->st_name;
7290
      ssym->st_info = (*ind)->st_info;
7291
      ssym->st_other = (*ind)->st_other;
7292
      ssymhead->count++;
7293
    }
7294
  BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7295
	      && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7296
		  == total_size));
7297
 
7298
  free (indbuf);
7299
  return ssymbuf;
7300
}
7301
 
7302
/* Check if 2 sections define the same set of local and global
7303
   symbols.  */
7304
 
7305
static bfd_boolean
7306
bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7307
				   struct bfd_link_info *info)
7308
{
7309
  bfd *bfd1, *bfd2;
7310
  const struct elf_backend_data *bed1, *bed2;
7311
  Elf_Internal_Shdr *hdr1, *hdr2;
7312
  bfd_size_type symcount1, symcount2;
7313
  Elf_Internal_Sym *isymbuf1, *isymbuf2;
7314
  struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7315
  Elf_Internal_Sym *isym, *isymend;
7316
  struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7317
  bfd_size_type count1, count2, i;
7318
  unsigned int shndx1, shndx2;
7319
  bfd_boolean result;
7320
 
7321
  bfd1 = sec1->owner;
7322
  bfd2 = sec2->owner;
7323
 
7324
  /* Both sections have to be in ELF.  */
7325
  if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7326
      || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7327
    return FALSE;
7328
 
7329
  if (elf_section_type (sec1) != elf_section_type (sec2))
7330
    return FALSE;
7331
 
7332
  shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7333
  shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7334
  if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7335
    return FALSE;
7336
 
7337
  bed1 = get_elf_backend_data (bfd1);
7338
  bed2 = get_elf_backend_data (bfd2);
7339
  hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7340
  symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7341
  hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7342
  symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7343
 
7344
  if (symcount1 == 0 || symcount2 == 0)
7345
    return FALSE;
7346
 
7347
  result = FALSE;
7348
  isymbuf1 = NULL;
7349
  isymbuf2 = NULL;
7350
  ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7351
  ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7352
 
7353
  if (ssymbuf1 == NULL)
7354
    {
7355
      isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7356
				       NULL, NULL, NULL);
7357
      if (isymbuf1 == NULL)
7358
	goto done;
7359
 
7360
      if (!info->reduce_memory_overheads)
7361
	elf_tdata (bfd1)->symbuf = ssymbuf1
7362
	  = elf_create_symbuf (symcount1, isymbuf1);
7363
    }
7364
 
7365
  if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7366
    {
7367
      isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7368
				       NULL, NULL, NULL);
7369
      if (isymbuf2 == NULL)
7370
	goto done;
7371
 
7372
      if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7373
	elf_tdata (bfd2)->symbuf = ssymbuf2
7374
	  = elf_create_symbuf (symcount2, isymbuf2);
7375
    }
7376
 
7377
  if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7378
    {
7379
      /* Optimized faster version.  */
7380
      bfd_size_type lo, hi, mid;
7381
      struct elf_symbol *symp;
7382
      struct elf_symbuf_symbol *ssym, *ssymend;
7383
 
7384
      lo = 0;
7385
      hi = ssymbuf1->count;
7386
      ssymbuf1++;
7387
      count1 = 0;
7388
      while (lo < hi)
7389
	{
7390
	  mid = (lo + hi) / 2;
7391
	  if (shndx1 < ssymbuf1[mid].st_shndx)
7392
	    hi = mid;
7393
	  else if (shndx1 > ssymbuf1[mid].st_shndx)
7394
	    lo = mid + 1;
7395
	  else
7396
	    {
7397
	      count1 = ssymbuf1[mid].count;
7398
	      ssymbuf1 += mid;
7399
	      break;
7400
	    }
7401
	}
7402
 
7403
      lo = 0;
7404
      hi = ssymbuf2->count;
7405
      ssymbuf2++;
7406
      count2 = 0;
7407
      while (lo < hi)
7408
	{
7409
	  mid = (lo + hi) / 2;
7410
	  if (shndx2 < ssymbuf2[mid].st_shndx)
7411
	    hi = mid;
7412
	  else if (shndx2 > ssymbuf2[mid].st_shndx)
7413
	    lo = mid + 1;
7414
	  else
7415
	    {
7416
	      count2 = ssymbuf2[mid].count;
7417
	      ssymbuf2 += mid;
7418
	      break;
7419
	    }
7420
	}
7421
 
7422
      if (count1 == 0 || count2 == 0 || count1 != count2)
7423
	goto done;
7424
 
6324 serge 7425
      symtable1
7426
	= (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7427
      symtable2
7428
	= (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
5197 serge 7429
      if (symtable1 == NULL || symtable2 == NULL)
7430
	goto done;
7431
 
7432
      symp = symtable1;
7433
      for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7434
	   ssym < ssymend; ssym++, symp++)
7435
	{
7436
	  symp->u.ssym = ssym;
7437
	  symp->name = bfd_elf_string_from_elf_section (bfd1,
7438
							hdr1->sh_link,
7439
							ssym->st_name);
7440
	}
7441
 
7442
      symp = symtable2;
7443
      for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7444
	   ssym < ssymend; ssym++, symp++)
7445
	{
7446
	  symp->u.ssym = ssym;
7447
	  symp->name = bfd_elf_string_from_elf_section (bfd2,
7448
							hdr2->sh_link,
7449
							ssym->st_name);
7450
	}
7451
 
7452
      /* Sort symbol by name.  */
7453
      qsort (symtable1, count1, sizeof (struct elf_symbol),
7454
	     elf_sym_name_compare);
7455
      qsort (symtable2, count1, sizeof (struct elf_symbol),
7456
	     elf_sym_name_compare);
7457
 
7458
      for (i = 0; i < count1; i++)
7459
	/* Two symbols must have the same binding, type and name.  */
7460
	if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7461
	    || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7462
	    || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7463
	  goto done;
7464
 
7465
      result = TRUE;
7466
      goto done;
7467
    }
7468
 
7469
  symtable1 = (struct elf_symbol *)
7470
      bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7471
  symtable2 = (struct elf_symbol *)
7472
      bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7473
  if (symtable1 == NULL || symtable2 == NULL)
7474
    goto done;
7475
 
7476
  /* Count definitions in the section.  */
7477
  count1 = 0;
7478
  for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7479
    if (isym->st_shndx == shndx1)
7480
      symtable1[count1++].u.isym = isym;
7481
 
7482
  count2 = 0;
7483
  for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7484
    if (isym->st_shndx == shndx2)
7485
      symtable2[count2++].u.isym = isym;
7486
 
7487
  if (count1 == 0 || count2 == 0 || count1 != count2)
7488
    goto done;
7489
 
7490
  for (i = 0; i < count1; i++)
7491
    symtable1[i].name
7492
      = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7493
					 symtable1[i].u.isym->st_name);
7494
 
7495
  for (i = 0; i < count2; i++)
7496
    symtable2[i].name
7497
      = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7498
					 symtable2[i].u.isym->st_name);
7499
 
7500
  /* Sort symbol by name.  */
7501
  qsort (symtable1, count1, sizeof (struct elf_symbol),
7502
	 elf_sym_name_compare);
7503
  qsort (symtable2, count1, sizeof (struct elf_symbol),
7504
	 elf_sym_name_compare);
7505
 
7506
  for (i = 0; i < count1; i++)
7507
    /* Two symbols must have the same binding, type and name.  */
7508
    if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7509
	|| symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7510
	|| strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7511
      goto done;
7512
 
7513
  result = TRUE;
7514
 
7515
done:
7516
  if (symtable1)
7517
    free (symtable1);
7518
  if (symtable2)
7519
    free (symtable2);
7520
  if (isymbuf1)
7521
    free (isymbuf1);
7522
  if (isymbuf2)
7523
    free (isymbuf2);
7524
 
7525
  return result;
7526
}
7527
 
7528
/* Return TRUE if 2 section types are compatible.  */
7529
 
7530
bfd_boolean
7531
_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7532
				 bfd *bbfd, const asection *bsec)
7533
{
7534
  if (asec == NULL
7535
      || bsec == NULL
7536
      || abfd->xvec->flavour != bfd_target_elf_flavour
7537
      || bbfd->xvec->flavour != bfd_target_elf_flavour)
7538
    return TRUE;
7539
 
7540
  return elf_section_type (asec) == elf_section_type (bsec);
7541
}
7542
 
7543
/* Final phase of ELF linker.  */
7544
 
7545
/* A structure we use to avoid passing large numbers of arguments.  */
7546
 
7547
struct elf_final_link_info
7548
{
7549
  /* General link information.  */
7550
  struct bfd_link_info *info;
7551
  /* Output BFD.  */
7552
  bfd *output_bfd;
7553
  /* Symbol string table.  */
6324 serge 7554
  struct elf_strtab_hash *symstrtab;
5197 serge 7555
  /* .hash section.  */
7556
  asection *hash_sec;
7557
  /* symbol version section (.gnu.version).  */
7558
  asection *symver_sec;
7559
  /* Buffer large enough to hold contents of any section.  */
7560
  bfd_byte *contents;
7561
  /* Buffer large enough to hold external relocs of any section.  */
7562
  void *external_relocs;
7563
  /* Buffer large enough to hold internal relocs of any section.  */
7564
  Elf_Internal_Rela *internal_relocs;
7565
  /* Buffer large enough to hold external local symbols of any input
7566
     BFD.  */
7567
  bfd_byte *external_syms;
7568
  /* And a buffer for symbol section indices.  */
7569
  Elf_External_Sym_Shndx *locsym_shndx;
7570
  /* Buffer large enough to hold internal local symbols of any input
7571
     BFD.  */
7572
  Elf_Internal_Sym *internal_syms;
7573
  /* Array large enough to hold a symbol index for each local symbol
7574
     of any input BFD.  */
7575
  long *indices;
7576
  /* Array large enough to hold a section pointer for each local
7577
     symbol of any input BFD.  */
7578
  asection **sections;
6324 serge 7579
  /* Buffer for SHT_SYMTAB_SHNDX section.  */
5197 serge 7580
  Elf_External_Sym_Shndx *symshndxbuf;
7581
  /* Number of STT_FILE syms seen.  */
7582
  size_t filesym_count;
7583
};
7584
 
7585
/* This struct is used to pass information to elf_link_output_extsym.  */
7586
 
7587
struct elf_outext_info
7588
{
7589
  bfd_boolean failed;
7590
  bfd_boolean localsyms;
6324 serge 7591
  bfd_boolean file_sym_done;
5197 serge 7592
  struct elf_final_link_info *flinfo;
7593
};
7594
 
7595
 
7596
/* Support for evaluating a complex relocation.
7597
 
7598
   Complex relocations are generalized, self-describing relocations.  The
7599
   implementation of them consists of two parts: complex symbols, and the
7600
   relocations themselves.
7601
 
7602
   The relocations are use a reserved elf-wide relocation type code (R_RELC
7603
   external / BFD_RELOC_RELC internal) and an encoding of relocation field
7604
   information (start bit, end bit, word width, etc) into the addend.  This
7605
   information is extracted from CGEN-generated operand tables within gas.
7606
 
7607
   Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7608
   internal) representing prefix-notation expressions, including but not
7609
   limited to those sorts of expressions normally encoded as addends in the
7610
   addend field.  The symbol mangling format is:
7611
 
7612
    := 
7613
          |   ':' 
7614
          |   ':'  ':' 
7615
	  ;
7616
 
7617
    := 's'  ':' 
7618
             |  'S'  ':' 
7619
	     |  '#' 
7620
	     ;
7621
 
7622
    := as in C
7623
    := as in C, plus "0-" for unambiguous negation.  */
7624
 
7625
static void
7626
set_symbol_value (bfd *bfd_with_globals,
7627
		  Elf_Internal_Sym *isymbuf,
7628
		  size_t locsymcount,
7629
		  size_t symidx,
7630
		  bfd_vma val)
7631
{
7632
  struct elf_link_hash_entry **sym_hashes;
7633
  struct elf_link_hash_entry *h;
7634
  size_t extsymoff = locsymcount;
7635
 
7636
  if (symidx < locsymcount)
7637
    {
7638
      Elf_Internal_Sym *sym;
7639
 
7640
      sym = isymbuf + symidx;
7641
      if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7642
	{
7643
	  /* It is a local symbol: move it to the
7644
	     "absolute" section and give it a value.  */
7645
	  sym->st_shndx = SHN_ABS;
7646
	  sym->st_value = val;
7647
	  return;
7648
	}
7649
      BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7650
      extsymoff = 0;
7651
    }
7652
 
7653
  /* It is a global symbol: set its link type
7654
     to "defined" and give it a value.  */
7655
 
7656
  sym_hashes = elf_sym_hashes (bfd_with_globals);
7657
  h = sym_hashes [symidx - extsymoff];
7658
  while (h->root.type == bfd_link_hash_indirect
7659
	 || h->root.type == bfd_link_hash_warning)
7660
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
7661
  h->root.type = bfd_link_hash_defined;
7662
  h->root.u.def.value = val;
7663
  h->root.u.def.section = bfd_abs_section_ptr;
7664
}
7665
 
7666
static bfd_boolean
7667
resolve_symbol (const char *name,
7668
		bfd *input_bfd,
7669
		struct elf_final_link_info *flinfo,
7670
		bfd_vma *result,
7671
		Elf_Internal_Sym *isymbuf,
7672
		size_t locsymcount)
7673
{
7674
  Elf_Internal_Sym *sym;
7675
  struct bfd_link_hash_entry *global_entry;
7676
  const char *candidate = NULL;
7677
  Elf_Internal_Shdr *symtab_hdr;
7678
  size_t i;
7679
 
7680
  symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7681
 
7682
  for (i = 0; i < locsymcount; ++ i)
7683
    {
7684
      sym = isymbuf + i;
7685
 
7686
      if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7687
	continue;
7688
 
7689
      candidate = bfd_elf_string_from_elf_section (input_bfd,
7690
						   symtab_hdr->sh_link,
7691
						   sym->st_name);
7692
#ifdef DEBUG
7693
      printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7694
	      name, candidate, (unsigned long) sym->st_value);
7695
#endif
7696
      if (candidate && strcmp (candidate, name) == 0)
7697
	{
7698
	  asection *sec = flinfo->sections [i];
7699
 
7700
	  *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7701
	  *result += sec->output_offset + sec->output_section->vma;
7702
#ifdef DEBUG
7703
	  printf ("Found symbol with value %8.8lx\n",
7704
		  (unsigned long) *result);
7705
#endif
7706
	  return TRUE;
7707
	}
7708
    }
7709
 
7710
  /* Hmm, haven't found it yet. perhaps it is a global.  */
7711
  global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7712
				       FALSE, FALSE, TRUE);
7713
  if (!global_entry)
7714
    return FALSE;
7715
 
7716
  if (global_entry->type == bfd_link_hash_defined
7717
      || global_entry->type == bfd_link_hash_defweak)
7718
    {
7719
      *result = (global_entry->u.def.value
7720
		 + global_entry->u.def.section->output_section->vma
7721
		 + global_entry->u.def.section->output_offset);
7722
#ifdef DEBUG
7723
      printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7724
	      global_entry->root.string, (unsigned long) *result);
7725
#endif
7726
      return TRUE;
7727
    }
7728
 
7729
  return FALSE;
7730
}
7731
 
7732
static bfd_boolean
7733
resolve_section (const char *name,
7734
		 asection *sections,
7735
		 bfd_vma *result)
7736
{
7737
  asection *curr;
7738
  unsigned int len;
7739
 
7740
  for (curr = sections; curr; curr = curr->next)
7741
    if (strcmp (curr->name, name) == 0)
7742
      {
7743
	*result = curr->vma;
7744
	return TRUE;
7745
      }
7746
 
7747
  /* Hmm. still haven't found it. try pseudo-section names.  */
7748
  for (curr = sections; curr; curr = curr->next)
7749
    {
7750
      len = strlen (curr->name);
7751
      if (len > strlen (name))
7752
	continue;
7753
 
7754
      if (strncmp (curr->name, name, len) == 0)
7755
	{
7756
	  if (strncmp (".end", name + len, 4) == 0)
7757
	    {
7758
	      *result = curr->vma + curr->size;
7759
	      return TRUE;
7760
	    }
7761
 
7762
	  /* Insert more pseudo-section names here, if you like.  */
7763
	}
7764
    }
7765
 
7766
  return FALSE;
7767
}
7768
 
7769
static void
7770
undefined_reference (const char *reftype, const char *name)
7771
{
7772
  _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7773
		      reftype, name);
7774
}
7775
 
7776
static bfd_boolean
7777
eval_symbol (bfd_vma *result,
7778
	     const char **symp,
7779
	     bfd *input_bfd,
7780
	     struct elf_final_link_info *flinfo,
7781
	     bfd_vma dot,
7782
	     Elf_Internal_Sym *isymbuf,
7783
	     size_t locsymcount,
7784
	     int signed_p)
7785
{
7786
  size_t len;
7787
  size_t symlen;
7788
  bfd_vma a;
7789
  bfd_vma b;
7790
  char symbuf[4096];
7791
  const char *sym = *symp;
7792
  const char *symend;
7793
  bfd_boolean symbol_is_section = FALSE;
7794
 
7795
  len = strlen (sym);
7796
  symend = sym + len;
7797
 
7798
  if (len < 1 || len > sizeof (symbuf))
7799
    {
7800
      bfd_set_error (bfd_error_invalid_operation);
7801
      return FALSE;
7802
    }
7803
 
7804
  switch (* sym)
7805
    {
7806
    case '.':
7807
      *result = dot;
7808
      *symp = sym + 1;
7809
      return TRUE;
7810
 
7811
    case '#':
7812
      ++sym;
7813
      *result = strtoul (sym, (char **) symp, 16);
7814
      return TRUE;
7815
 
7816
    case 'S':
7817
      symbol_is_section = TRUE;
7818
    case 's':
7819
      ++sym;
7820
      symlen = strtol (sym, (char **) symp, 10);
7821
      sym = *symp + 1; /* Skip the trailing ':'.  */
7822
 
7823
      if (symend < sym || symlen + 1 > sizeof (symbuf))
7824
	{
7825
	  bfd_set_error (bfd_error_invalid_operation);
7826
	  return FALSE;
7827
	}
7828
 
7829
      memcpy (symbuf, sym, symlen);
7830
      symbuf[symlen] = '\0';
7831
      *symp = sym + symlen;
7832
 
7833
      /* Is it always possible, with complex symbols, that gas "mis-guessed"
7834
	 the symbol as a section, or vice-versa. so we're pretty liberal in our
7835
	 interpretation here; section means "try section first", not "must be a
7836
	 section", and likewise with symbol.  */
7837
 
7838
      if (symbol_is_section)
7839
	{
7840
	  if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
7841
	      && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7842
				  isymbuf, locsymcount))
7843
	    {
7844
	      undefined_reference ("section", symbuf);
7845
	      return FALSE;
7846
	    }
7847
	}
7848
      else
7849
	{
7850
	  if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7851
			       isymbuf, locsymcount)
7852
	      && !resolve_section (symbuf, flinfo->output_bfd->sections,
7853
				   result))
7854
	    {
7855
	      undefined_reference ("symbol", symbuf);
7856
	      return FALSE;
7857
	    }
7858
	}
7859
 
7860
      return TRUE;
7861
 
7862
      /* All that remains are operators.  */
7863
 
7864
#define UNARY_OP(op)						\
7865
  if (strncmp (sym, #op, strlen (#op)) == 0)			\
7866
    {								\
7867
      sym += strlen (#op);					\
7868
      if (*sym == ':')						\
7869
	++sym;							\
7870
      *symp = sym;						\
7871
      if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
7872
			isymbuf, locsymcount, signed_p))	\
7873
	return FALSE;						\
7874
      if (signed_p)						\
7875
	*result = op ((bfd_signed_vma) a);			\
7876
      else							\
7877
	*result = op a;						\
7878
      return TRUE;						\
7879
    }
7880
 
7881
#define BINARY_OP(op)						\
7882
  if (strncmp (sym, #op, strlen (#op)) == 0)			\
7883
    {								\
7884
      sym += strlen (#op);					\
7885
      if (*sym == ':')						\
7886
	++sym;							\
7887
      *symp = sym;						\
7888
      if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
7889
			isymbuf, locsymcount, signed_p))	\
7890
	return FALSE;						\
7891
      ++*symp;							\
7892
      if (!eval_symbol (&b, symp, input_bfd, flinfo, dot,	\
7893
			isymbuf, locsymcount, signed_p))	\
7894
	return FALSE;						\
7895
      if (signed_p)						\
7896
	*result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b);	\
7897
      else							\
7898
	*result = a op b;					\
7899
      return TRUE;						\
7900
    }
7901
 
7902
    default:
7903
      UNARY_OP  (0-);
7904
      BINARY_OP (<<);
7905
      BINARY_OP (>>);
7906
      BINARY_OP (==);
7907
      BINARY_OP (!=);
7908
      BINARY_OP (<=);
7909
      BINARY_OP (>=);
7910
      BINARY_OP (&&);
7911
      BINARY_OP (||);
7912
      UNARY_OP  (~);
7913
      UNARY_OP  (!);
7914
      BINARY_OP (*);
7915
      BINARY_OP (/);
7916
      BINARY_OP (%);
7917
      BINARY_OP (^);
7918
      BINARY_OP (|);
7919
      BINARY_OP (&);
7920
      BINARY_OP (+);
7921
      BINARY_OP (-);
7922
      BINARY_OP (<);
7923
      BINARY_OP (>);
7924
#undef UNARY_OP
7925
#undef BINARY_OP
7926
      _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7927
      bfd_set_error (bfd_error_invalid_operation);
7928
      return FALSE;
7929
    }
7930
}
7931
 
7932
static void
7933
put_value (bfd_vma size,
7934
	   unsigned long chunksz,
7935
	   bfd *input_bfd,
7936
	   bfd_vma x,
7937
	   bfd_byte *location)
7938
{
7939
  location += (size - chunksz);
7940
 
6324 serge 7941
  for (; size; size -= chunksz, location -= chunksz)
5197 serge 7942
    {
7943
      switch (chunksz)
7944
	{
7945
	case 1:
7946
	  bfd_put_8 (input_bfd, x, location);
6324 serge 7947
	  x >>= 8;
5197 serge 7948
	  break;
7949
	case 2:
7950
	  bfd_put_16 (input_bfd, x, location);
6324 serge 7951
	  x >>= 16;
5197 serge 7952
	  break;
7953
	case 4:
7954
	  bfd_put_32 (input_bfd, x, location);
6324 serge 7955
	  /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
7956
	  x >>= 16;
7957
	  x >>= 16;
5197 serge 7958
	  break;
6324 serge 7959
#ifdef BFD64
5197 serge 7960
	case 8:
7961
	  bfd_put_64 (input_bfd, x, location);
6324 serge 7962
	  /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
7963
	  x >>= 32;
7964
	  x >>= 32;
7965
	  break;
7966
#endif
7967
	default:
5197 serge 7968
	  abort ();
7969
	  break;
7970
	}
7971
    }
7972
}
7973
 
7974
static bfd_vma
7975
get_value (bfd_vma size,
7976
	   unsigned long chunksz,
7977
	   bfd *input_bfd,
7978
	   bfd_byte *location)
7979
{
7980
  int shift;
7981
  bfd_vma x = 0;
7982
 
7983
  /* Sanity checks.  */
7984
  BFD_ASSERT (chunksz <= sizeof (x)
7985
	      && size >= chunksz
7986
	      && chunksz != 0
7987
	      && (size % chunksz) == 0
7988
	      && input_bfd != NULL
7989
	      && location != NULL);
7990
 
7991
  if (chunksz == sizeof (x))
7992
    {
7993
      BFD_ASSERT (size == chunksz);
7994
 
7995
      /* Make sure that we do not perform an undefined shift operation.
7996
	 We know that size == chunksz so there will only be one iteration
7997
	 of the loop below.  */
7998
      shift = 0;
7999
    }
8000
  else
8001
    shift = 8 * chunksz;
8002
 
8003
  for (; size; size -= chunksz, location += chunksz)
8004
    {
8005
      switch (chunksz)
8006
	{
8007
	case 1:
8008
	  x = (x << shift) | bfd_get_8 (input_bfd, location);
8009
	  break;
8010
	case 2:
8011
	  x = (x << shift) | bfd_get_16 (input_bfd, location);
8012
	  break;
8013
	case 4:
8014
	  x = (x << shift) | bfd_get_32 (input_bfd, location);
8015
	  break;
8016
#ifdef BFD64
8017
	case 8:
8018
	  x = (x << shift) | bfd_get_64 (input_bfd, location);
8019
	  break;
8020
#endif
8021
	default:
8022
	  abort ();
8023
	}
8024
    }
8025
  return x;
8026
}
8027
 
8028
static void
8029
decode_complex_addend (unsigned long *start,   /* in bits */
8030
		       unsigned long *oplen,   /* in bits */
8031
		       unsigned long *len,     /* in bits */
8032
		       unsigned long *wordsz,  /* in bytes */
8033
		       unsigned long *chunksz, /* in bytes */
8034
		       unsigned long *lsb0_p,
8035
		       unsigned long *signed_p,
8036
		       unsigned long *trunc_p,
8037
		       unsigned long encoded)
8038
{
8039
  * start     =  encoded        & 0x3F;
8040
  * len       = (encoded >>  6) & 0x3F;
8041
  * oplen     = (encoded >> 12) & 0x3F;
8042
  * wordsz    = (encoded >> 18) & 0xF;
8043
  * chunksz   = (encoded >> 22) & 0xF;
8044
  * lsb0_p    = (encoded >> 27) & 1;
8045
  * signed_p  = (encoded >> 28) & 1;
8046
  * trunc_p   = (encoded >> 29) & 1;
8047
}
8048
 
8049
bfd_reloc_status_type
8050
bfd_elf_perform_complex_relocation (bfd *input_bfd,
8051
				    asection *input_section ATTRIBUTE_UNUSED,
8052
				    bfd_byte *contents,
8053
				    Elf_Internal_Rela *rel,
8054
				    bfd_vma relocation)
8055
{
8056
  bfd_vma shift, x, mask;
8057
  unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8058
  bfd_reloc_status_type r;
8059
 
8060
  /*  Perform this reloc, since it is complex.
8061
      (this is not to say that it necessarily refers to a complex
8062
      symbol; merely that it is a self-describing CGEN based reloc.
8063
      i.e. the addend has the complete reloc information (bit start, end,
8064
      word size, etc) encoded within it.).  */
8065
 
8066
  decode_complex_addend (&start, &oplen, &len, &wordsz,
8067
			 &chunksz, &lsb0_p, &signed_p,
8068
			 &trunc_p, rel->r_addend);
8069
 
8070
  mask = (((1L << (len - 1)) - 1) << 1) | 1;
8071
 
8072
  if (lsb0_p)
8073
    shift = (start + 1) - len;
8074
  else
8075
    shift = (8 * wordsz) - (start + len);
8076
 
8077
  /* FIXME: octets_per_byte.  */
8078
  x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
8079
 
8080
#ifdef DEBUG
8081
  printf ("Doing complex reloc: "
8082
	  "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8083
	  "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8084
	  "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8085
	  lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8086
	  oplen, (unsigned long) x, (unsigned long) mask,
8087
	  (unsigned long) relocation);
8088
#endif
8089
 
8090
  r = bfd_reloc_ok;
8091
  if (! trunc_p)
8092
    /* Now do an overflow check.  */
8093
    r = bfd_check_overflow ((signed_p
8094
			     ? complain_overflow_signed
8095
			     : complain_overflow_unsigned),
8096
			    len, 0, (8 * wordsz),
8097
			    relocation);
8098
 
8099
  /* Do the deed.  */
8100
  x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8101
 
8102
#ifdef DEBUG
8103
  printf ("           relocation: %8.8lx\n"
8104
	  "         shifted mask: %8.8lx\n"
8105
	  " shifted/masked reloc: %8.8lx\n"
8106
	  "               result: %8.8lx\n",
8107
	  (unsigned long) relocation, (unsigned long) (mask << shift),
8108
	  (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8109
#endif
8110
  /* FIXME: octets_per_byte.  */
8111
  put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
8112
  return r;
8113
}
8114
 
6324 serge 8115
/* Functions to read r_offset from external (target order) reloc
8116
   entry.  Faster than bfd_getl32 et al, because we let the compiler
8117
   know the value is aligned.  */
8118
 
8119
static bfd_vma
8120
ext32l_r_offset (const void *p)
8121
{
8122
  union aligned32
8123
  {
8124
    uint32_t v;
8125
    unsigned char c[4];
8126
  };
8127
  const union aligned32 *a
8128
    = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8129
 
8130
  uint32_t aval = (  (uint32_t) a->c[0]
8131
		   | (uint32_t) a->c[1] << 8
8132
		   | (uint32_t) a->c[2] << 16
8133
		   | (uint32_t) a->c[3] << 24);
8134
  return aval;
8135
}
8136
 
8137
static bfd_vma
8138
ext32b_r_offset (const void *p)
8139
{
8140
  union aligned32
8141
  {
8142
    uint32_t v;
8143
    unsigned char c[4];
8144
  };
8145
  const union aligned32 *a
8146
    = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8147
 
8148
  uint32_t aval = (  (uint32_t) a->c[0] << 24
8149
		   | (uint32_t) a->c[1] << 16
8150
		   | (uint32_t) a->c[2] << 8
8151
		   | (uint32_t) a->c[3]);
8152
  return aval;
8153
}
8154
 
8155
#ifdef BFD_HOST_64_BIT
8156
static bfd_vma
8157
ext64l_r_offset (const void *p)
8158
{
8159
  union aligned64
8160
  {
8161
    uint64_t v;
8162
    unsigned char c[8];
8163
  };
8164
  const union aligned64 *a
8165
    = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8166
 
8167
  uint64_t aval = (  (uint64_t) a->c[0]
8168
		   | (uint64_t) a->c[1] << 8
8169
		   | (uint64_t) a->c[2] << 16
8170
		   | (uint64_t) a->c[3] << 24
8171
		   | (uint64_t) a->c[4] << 32
8172
		   | (uint64_t) a->c[5] << 40
8173
		   | (uint64_t) a->c[6] << 48
8174
		   | (uint64_t) a->c[7] << 56);
8175
  return aval;
8176
}
8177
 
8178
static bfd_vma
8179
ext64b_r_offset (const void *p)
8180
{
8181
  union aligned64
8182
  {
8183
    uint64_t v;
8184
    unsigned char c[8];
8185
  };
8186
  const union aligned64 *a
8187
    = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8188
 
8189
  uint64_t aval = (  (uint64_t) a->c[0] << 56
8190
		   | (uint64_t) a->c[1] << 48
8191
		   | (uint64_t) a->c[2] << 40
8192
		   | (uint64_t) a->c[3] << 32
8193
		   | (uint64_t) a->c[4] << 24
8194
		   | (uint64_t) a->c[5] << 16
8195
		   | (uint64_t) a->c[6] << 8
8196
		   | (uint64_t) a->c[7]);
8197
  return aval;
8198
}
8199
#endif
8200
 
5197 serge 8201
/* When performing a relocatable link, the input relocations are
8202
   preserved.  But, if they reference global symbols, the indices
8203
   referenced must be updated.  Update all the relocations found in
8204
   RELDATA.  */
8205
 
6324 serge 8206
static bfd_boolean
5197 serge 8207
elf_link_adjust_relocs (bfd *abfd,
6324 serge 8208
			struct bfd_elf_section_reloc_data *reldata,
8209
			bfd_boolean sort)
5197 serge 8210
{
8211
  unsigned int i;
8212
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8213
  bfd_byte *erela;
8214
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8215
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8216
  bfd_vma r_type_mask;
8217
  int r_sym_shift;
8218
  unsigned int count = reldata->count;
8219
  struct elf_link_hash_entry **rel_hash = reldata->hashes;
8220
 
8221
  if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8222
    {
8223
      swap_in = bed->s->swap_reloc_in;
8224
      swap_out = bed->s->swap_reloc_out;
8225
    }
8226
  else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8227
    {
8228
      swap_in = bed->s->swap_reloca_in;
8229
      swap_out = bed->s->swap_reloca_out;
8230
    }
8231
  else
8232
    abort ();
8233
 
8234
  if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8235
    abort ();
8236
 
8237
  if (bed->s->arch_size == 32)
8238
    {
8239
      r_type_mask = 0xff;
8240
      r_sym_shift = 8;
8241
    }
8242
  else
8243
    {
8244
      r_type_mask = 0xffffffff;
8245
      r_sym_shift = 32;
8246
    }
8247
 
8248
  erela = reldata->hdr->contents;
8249
  for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8250
    {
8251
      Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8252
      unsigned int j;
8253
 
8254
      if (*rel_hash == NULL)
8255
	continue;
8256
 
8257
      BFD_ASSERT ((*rel_hash)->indx >= 0);
8258
 
8259
      (*swap_in) (abfd, erela, irela);
8260
      for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8261
	irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8262
			   | (irela[j].r_info & r_type_mask));
8263
      (*swap_out) (abfd, irela, erela);
8264
    }
6324 serge 8265
 
8266
  if (sort && count != 0)
8267
    {
8268
      bfd_vma (*ext_r_off) (const void *);
8269
      bfd_vma r_off;
8270
      size_t elt_size;
8271
      bfd_byte *base, *end, *p, *loc;
8272
      bfd_byte *buf = NULL;
8273
 
8274
      if (bed->s->arch_size == 32)
8275
	{
8276
	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8277
	    ext_r_off = ext32l_r_offset;
8278
	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8279
	    ext_r_off = ext32b_r_offset;
8280
	  else
8281
	    abort ();
8282
	}
8283
      else
8284
	{
8285
#ifdef BFD_HOST_64_BIT
8286
	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8287
	    ext_r_off = ext64l_r_offset;
8288
	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8289
	    ext_r_off = ext64b_r_offset;
8290
	  else
8291
#endif
8292
	    abort ();
8293
	}
8294
 
8295
      /*  Must use a stable sort here.  A modified insertion sort,
8296
	  since the relocs are mostly sorted already.  */
8297
      elt_size = reldata->hdr->sh_entsize;
8298
      base = reldata->hdr->contents;
8299
      end = base + count * elt_size;
8300
      if (elt_size > sizeof (Elf64_External_Rela))
8301
	abort ();
8302
 
8303
      /* Ensure the first element is lowest.  This acts as a sentinel,
8304
	 speeding the main loop below.  */
8305
      r_off = (*ext_r_off) (base);
8306
      for (p = loc = base; (p += elt_size) < end; )
8307
	{
8308
	  bfd_vma r_off2 = (*ext_r_off) (p);
8309
	  if (r_off > r_off2)
8310
	    {
8311
	      r_off = r_off2;
8312
	      loc = p;
8313
	    }
8314
	}
8315
      if (loc != base)
8316
	{
8317
	  /* Don't just swap *base and *loc as that changes the order
8318
	     of the original base[0] and base[1] if they happen to
8319
	     have the same r_offset.  */
8320
	  bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8321
	  memcpy (onebuf, loc, elt_size);
8322
	  memmove (base + elt_size, base, loc - base);
8323
	  memcpy (base, onebuf, elt_size);
8324
	}
8325
 
8326
      for (p = base + elt_size; (p += elt_size) < end; )
8327
	{
8328
	  /* base to p is sorted, *p is next to insert.  */
8329
	  r_off = (*ext_r_off) (p);
8330
	  /* Search the sorted region for location to insert.  */
8331
	  loc = p - elt_size;
8332
	  while (r_off < (*ext_r_off) (loc))
8333
	    loc -= elt_size;
8334
	  loc += elt_size;
8335
	  if (loc != p)
8336
	    {
8337
	      /* Chances are there is a run of relocs to insert here,
8338
		 from one of more input files.  Files are not always
8339
		 linked in order due to the way elf_link_input_bfd is
8340
		 called.  See pr17666.  */
8341
	      size_t sortlen = p - loc;
8342
	      bfd_vma r_off2 = (*ext_r_off) (loc);
8343
	      size_t runlen = elt_size;
8344
	      size_t buf_size = 96 * 1024;
8345
	      while (p + runlen < end
8346
		     && (sortlen <= buf_size
8347
			 || runlen + elt_size <= buf_size)
8348
		     && r_off2 > (*ext_r_off) (p + runlen))
8349
		runlen += elt_size;
8350
	      if (buf == NULL)
8351
		{
8352
		  buf = bfd_malloc (buf_size);
8353
		  if (buf == NULL)
8354
		    return FALSE;
8355
		}
8356
	      if (runlen < sortlen)
8357
		{
8358
		  memcpy (buf, p, runlen);
8359
		  memmove (loc + runlen, loc, sortlen);
8360
		  memcpy (loc, buf, runlen);
8361
		}
8362
	      else
8363
		{
8364
		  memcpy (buf, loc, sortlen);
8365
		  memmove (loc, p, runlen);
8366
		  memcpy (loc + runlen, buf, sortlen);
8367
		}
8368
	      p += runlen - elt_size;
8369
	    }
8370
	}
8371
      /* Hashes are no longer valid.  */
8372
      free (reldata->hashes);
8373
      reldata->hashes = NULL;
8374
      free (buf);
8375
    }
8376
  return TRUE;
5197 serge 8377
}
8378
 
8379
struct elf_link_sort_rela
8380
{
8381
  union {
8382
    bfd_vma offset;
8383
    bfd_vma sym_mask;
8384
  } u;
8385
  enum elf_reloc_type_class type;
8386
  /* We use this as an array of size int_rels_per_ext_rel.  */
8387
  Elf_Internal_Rela rela[1];
8388
};
8389
 
8390
static int
8391
elf_link_sort_cmp1 (const void *A, const void *B)
8392
{
8393
  const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8394
  const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8395
  int relativea, relativeb;
8396
 
8397
  relativea = a->type == reloc_class_relative;
8398
  relativeb = b->type == reloc_class_relative;
8399
 
8400
  if (relativea < relativeb)
8401
    return 1;
8402
  if (relativea > relativeb)
8403
    return -1;
8404
  if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8405
    return -1;
8406
  if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8407
    return 1;
8408
  if (a->rela->r_offset < b->rela->r_offset)
8409
    return -1;
8410
  if (a->rela->r_offset > b->rela->r_offset)
8411
    return 1;
8412
  return 0;
8413
}
8414
 
8415
static int
8416
elf_link_sort_cmp2 (const void *A, const void *B)
8417
{
8418
  const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8419
  const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8420
 
8421
  if (a->type < b->type)
8422
    return -1;
8423
  if (a->type > b->type)
8424
    return 1;
8425
  if (a->u.offset < b->u.offset)
8426
    return -1;
8427
  if (a->u.offset > b->u.offset)
8428
    return 1;
8429
  if (a->rela->r_offset < b->rela->r_offset)
8430
    return -1;
8431
  if (a->rela->r_offset > b->rela->r_offset)
8432
    return 1;
8433
  return 0;
8434
}
8435
 
8436
static size_t
8437
elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8438
{
8439
  asection *dynamic_relocs;
8440
  asection *rela_dyn;
8441
  asection *rel_dyn;
8442
  bfd_size_type count, size;
8443
  size_t i, ret, sort_elt, ext_size;
8444
  bfd_byte *sort, *s_non_relative, *p;
8445
  struct elf_link_sort_rela *sq;
8446
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8447
  int i2e = bed->s->int_rels_per_ext_rel;
8448
  void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8449
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8450
  struct bfd_link_order *lo;
8451
  bfd_vma r_sym_mask;
8452
  bfd_boolean use_rela;
8453
 
8454
  /* Find a dynamic reloc section.  */
8455
  rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8456
  rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
8457
  if (rela_dyn != NULL && rela_dyn->size > 0
8458
      && rel_dyn != NULL && rel_dyn->size > 0)
8459
    {
8460
      bfd_boolean use_rela_initialised = FALSE;
8461
 
8462
      /* This is just here to stop gcc from complaining.
8463
	 It's initialization checking code is not perfect.  */
8464
      use_rela = TRUE;
8465
 
8466
      /* Both sections are present.  Examine the sizes
8467
	 of the indirect sections to help us choose.  */
8468
      for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8469
	if (lo->type == bfd_indirect_link_order)
8470
	  {
8471
	    asection *o = lo->u.indirect.section;
8472
 
8473
	    if ((o->size % bed->s->sizeof_rela) == 0)
8474
	      {
8475
		if ((o->size % bed->s->sizeof_rel) == 0)
8476
		  /* Section size is divisible by both rel and rela sizes.
8477
		     It is of no help to us.  */
8478
		  ;
8479
		else
8480
		  {
8481
		    /* Section size is only divisible by rela.  */
8482
		    if (use_rela_initialised && (use_rela == FALSE))
8483
		      {
8484
			_bfd_error_handler
8485
			  (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8486
			bfd_set_error (bfd_error_invalid_operation);
8487
			return 0;
8488
		      }
8489
		    else
8490
		      {
8491
			use_rela = TRUE;
8492
			use_rela_initialised = TRUE;
8493
		      }
8494
		  }
8495
	      }
8496
	    else if ((o->size % bed->s->sizeof_rel) == 0)
8497
	      {
8498
		/* Section size is only divisible by rel.  */
8499
		if (use_rela_initialised && (use_rela == TRUE))
8500
		  {
8501
		    _bfd_error_handler
8502
		      (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8503
		    bfd_set_error (bfd_error_invalid_operation);
8504
		    return 0;
8505
		  }
8506
		else
8507
		  {
8508
		    use_rela = FALSE;
8509
		    use_rela_initialised = TRUE;
8510
		  }
8511
	      }
8512
	    else
8513
	      {
8514
		/* The section size is not divisible by either - something is wrong.  */
8515
		_bfd_error_handler
8516
		  (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8517
		bfd_set_error (bfd_error_invalid_operation);
8518
		return 0;
8519
	      }
8520
	  }
8521
 
8522
      for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8523
	if (lo->type == bfd_indirect_link_order)
8524
	  {
8525
	    asection *o = lo->u.indirect.section;
8526
 
8527
	    if ((o->size % bed->s->sizeof_rela) == 0)
8528
	      {
8529
		if ((o->size % bed->s->sizeof_rel) == 0)
8530
		  /* Section size is divisible by both rel and rela sizes.
8531
		     It is of no help to us.  */
8532
		  ;
8533
		else
8534
		  {
8535
		    /* Section size is only divisible by rela.  */
8536
		    if (use_rela_initialised && (use_rela == FALSE))
8537
		      {
8538
			_bfd_error_handler
8539
			  (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8540
			bfd_set_error (bfd_error_invalid_operation);
8541
			return 0;
8542
		      }
8543
		    else
8544
		      {
8545
			use_rela = TRUE;
8546
			use_rela_initialised = TRUE;
8547
		      }
8548
		  }
8549
	      }
8550
	    else if ((o->size % bed->s->sizeof_rel) == 0)
8551
	      {
8552
		/* Section size is only divisible by rel.  */
8553
		if (use_rela_initialised && (use_rela == TRUE))
8554
		  {
8555
		    _bfd_error_handler
8556
		      (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8557
		    bfd_set_error (bfd_error_invalid_operation);
8558
		    return 0;
8559
		  }
8560
		else
8561
		  {
8562
		    use_rela = FALSE;
8563
		    use_rela_initialised = TRUE;
8564
		  }
8565
	      }
8566
	    else
8567
	      {
8568
		/* The section size is not divisible by either - something is wrong.  */
8569
		_bfd_error_handler
8570
		  (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8571
		bfd_set_error (bfd_error_invalid_operation);
8572
		return 0;
8573
	      }
8574
	  }
8575
 
8576
      if (! use_rela_initialised)
8577
	/* Make a guess.  */
8578
	use_rela = TRUE;
8579
    }
8580
  else if (rela_dyn != NULL && rela_dyn->size > 0)
8581
    use_rela = TRUE;
8582
  else if (rel_dyn != NULL && rel_dyn->size > 0)
8583
    use_rela = FALSE;
8584
  else
8585
    return 0;
8586
 
8587
  if (use_rela)
8588
    {
8589
      dynamic_relocs = rela_dyn;
8590
      ext_size = bed->s->sizeof_rela;
8591
      swap_in = bed->s->swap_reloca_in;
8592
      swap_out = bed->s->swap_reloca_out;
8593
    }
8594
  else
8595
    {
8596
      dynamic_relocs = rel_dyn;
8597
      ext_size = bed->s->sizeof_rel;
8598
      swap_in = bed->s->swap_reloc_in;
8599
      swap_out = bed->s->swap_reloc_out;
8600
    }
8601
 
8602
  size = 0;
8603
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8604
    if (lo->type == bfd_indirect_link_order)
8605
      size += lo->u.indirect.section->size;
8606
 
8607
  if (size != dynamic_relocs->size)
8608
    return 0;
8609
 
8610
  sort_elt = (sizeof (struct elf_link_sort_rela)
8611
	      + (i2e - 1) * sizeof (Elf_Internal_Rela));
8612
 
8613
  count = dynamic_relocs->size / ext_size;
8614
  if (count == 0)
8615
    return 0;
8616
  sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8617
 
8618
  if (sort == NULL)
8619
    {
8620
      (*info->callbacks->warning)
8621
	(info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8622
      return 0;
8623
    }
8624
 
8625
  if (bed->s->arch_size == 32)
8626
    r_sym_mask = ~(bfd_vma) 0xff;
8627
  else
8628
    r_sym_mask = ~(bfd_vma) 0xffffffff;
8629
 
8630
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8631
    if (lo->type == bfd_indirect_link_order)
8632
      {
8633
	bfd_byte *erel, *erelend;
8634
	asection *o = lo->u.indirect.section;
8635
 
8636
	if (o->contents == NULL && o->size != 0)
8637
	  {
8638
	    /* This is a reloc section that is being handled as a normal
8639
	       section.  See bfd_section_from_shdr.  We can't combine
8640
	       relocs in this case.  */
8641
	    free (sort);
8642
	    return 0;
8643
	  }
8644
	erel = o->contents;
8645
	erelend = o->contents + o->size;
8646
	/* FIXME: octets_per_byte.  */
8647
	p = sort + o->output_offset / ext_size * sort_elt;
8648
 
8649
	while (erel < erelend)
8650
	  {
8651
	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8652
 
8653
	    (*swap_in) (abfd, erel, s->rela);
8654
	    s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8655
	    s->u.sym_mask = r_sym_mask;
8656
	    p += sort_elt;
8657
	    erel += ext_size;
8658
	  }
8659
      }
8660
 
8661
  qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8662
 
8663
  for (i = 0, p = sort; i < count; i++, p += sort_elt)
8664
    {
8665
      struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8666
      if (s->type != reloc_class_relative)
8667
	break;
8668
    }
8669
  ret = i;
8670
  s_non_relative = p;
8671
 
8672
  sq = (struct elf_link_sort_rela *) s_non_relative;
8673
  for (; i < count; i++, p += sort_elt)
8674
    {
8675
      struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8676
      if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8677
	sq = sp;
8678
      sp->u.offset = sq->rela->r_offset;
8679
    }
8680
 
8681
  qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8682
 
8683
  for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8684
    if (lo->type == bfd_indirect_link_order)
8685
      {
8686
	bfd_byte *erel, *erelend;
8687
	asection *o = lo->u.indirect.section;
8688
 
8689
	erel = o->contents;
8690
	erelend = o->contents + o->size;
8691
	/* FIXME: octets_per_byte.  */
8692
	p = sort + o->output_offset / ext_size * sort_elt;
8693
	while (erel < erelend)
8694
	  {
8695
	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8696
	    (*swap_out) (abfd, s->rela, erel);
8697
	    p += sort_elt;
8698
	    erel += ext_size;
8699
	  }
8700
      }
8701
 
8702
  free (sort);
8703
  *psec = dynamic_relocs;
8704
  return ret;
8705
}
8706
 
6324 serge 8707
/* Add a symbol to the output symbol string table.  */
5197 serge 8708
 
8709
static int
6324 serge 8710
elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8711
			   const char *name,
8712
			   Elf_Internal_Sym *elfsym,
8713
			   asection *input_sec,
8714
			   struct elf_link_hash_entry *h)
5197 serge 8715
{
8716
  int (*output_symbol_hook)
8717
    (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8718
     struct elf_link_hash_entry *);
6324 serge 8719
  struct elf_link_hash_table *hash_table;
5197 serge 8720
  const struct elf_backend_data *bed;
6324 serge 8721
  bfd_size_type strtabsize;
5197 serge 8722
 
6324 serge 8723
  BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8724
 
5197 serge 8725
  bed = get_elf_backend_data (flinfo->output_bfd);
8726
  output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8727
  if (output_symbol_hook != NULL)
8728
    {
8729
      int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8730
      if (ret != 1)
8731
	return ret;
8732
    }
8733
 
6324 serge 8734
  if (name == NULL
8735
      || *name == '\0'
8736
      || (input_sec->flags & SEC_EXCLUDE))
8737
    elfsym->st_name = (unsigned long) -1;
5197 serge 8738
  else
8739
    {
6324 serge 8740
      /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8741
	 to get the final offset for st_name.  */
8742
      elfsym->st_name
8743
	= (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8744
					       name, FALSE);
5197 serge 8745
      if (elfsym->st_name == (unsigned long) -1)
8746
	return 0;
8747
    }
8748
 
6324 serge 8749
  hash_table = elf_hash_table (flinfo->info);
8750
  strtabsize = hash_table->strtabsize;
8751
  if (strtabsize <= hash_table->strtabcount)
5197 serge 8752
    {
6324 serge 8753
      strtabsize += strtabsize;
8754
      hash_table->strtabsize = strtabsize;
8755
      strtabsize *= sizeof (*hash_table->strtab);
8756
      hash_table->strtab
8757
	= (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8758
						 strtabsize);
8759
      if (hash_table->strtab == NULL)
5197 serge 8760
	return 0;
8761
    }
6324 serge 8762
  hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8763
  hash_table->strtab[hash_table->strtabcount].dest_index
8764
    = hash_table->strtabcount;
8765
  hash_table->strtab[hash_table->strtabcount].destshndx_index
8766
    = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
5197 serge 8767
 
6324 serge 8768
  bfd_get_symcount (flinfo->output_bfd) += 1;
8769
  hash_table->strtabcount += 1;
8770
 
8771
  return 1;
8772
}
8773
 
8774
/* Swap symbols out to the symbol table and flush the output symbols to
8775
   the file.  */
8776
 
8777
static bfd_boolean
8778
elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8779
{
8780
  struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8781
  bfd_size_type amt, i;
8782
  const struct elf_backend_data *bed;
8783
  bfd_byte *symbuf;
8784
  Elf_Internal_Shdr *hdr;
8785
  file_ptr pos;
8786
  bfd_boolean ret;
8787
 
8788
  if (!hash_table->strtabcount)
8789
    return TRUE;
8790
 
8791
  BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8792
 
8793
  bed = get_elf_backend_data (flinfo->output_bfd);
8794
 
8795
  amt = bed->s->sizeof_sym * hash_table->strtabcount;
8796
  symbuf = (bfd_byte *) bfd_malloc (amt);
8797
  if (symbuf == NULL)
8798
    return FALSE;
8799
 
8800
  if (flinfo->symshndxbuf)
5197 serge 8801
    {
6324 serge 8802
      amt = (sizeof (Elf_External_Sym_Shndx)
8803
	     * (bfd_get_symcount (flinfo->output_bfd)));
8804
      flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8805
      if (flinfo->symshndxbuf == NULL)
5197 serge 8806
	{
6324 serge 8807
	  free (symbuf);
8808
	  return FALSE;
5197 serge 8809
	}
8810
    }
8811
 
6324 serge 8812
  for (i = 0; i < hash_table->strtabcount; i++)
8813
    {
8814
      struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8815
      if (elfsym->sym.st_name == (unsigned long) -1)
8816
	elfsym->sym.st_name = 0;
8817
      else
8818
	elfsym->sym.st_name
8819
	  = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8820
						    elfsym->sym.st_name);
8821
      bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8822
			       ((bfd_byte *) symbuf
8823
				+ (elfsym->dest_index
8824
				   * bed->s->sizeof_sym)),
8825
			       (flinfo->symshndxbuf
8826
				+ elfsym->destshndx_index));
8827
    }
5197 serge 8828
 
6324 serge 8829
  hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8830
  pos = hdr->sh_offset + hdr->sh_size;
8831
  amt = hash_table->strtabcount * bed->s->sizeof_sym;
8832
  if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8833
      && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8834
    {
8835
      hdr->sh_size += amt;
8836
      ret = TRUE;
8837
    }
8838
  else
8839
    ret = FALSE;
8840
 
8841
  free (symbuf);
8842
 
8843
  free (hash_table->strtab);
8844
  hash_table->strtab = NULL;
8845
 
8846
  return ret;
5197 serge 8847
}
8848
 
8849
/* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
8850
 
8851
static bfd_boolean
8852
check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8853
{
8854
  if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8855
      && sym->st_shndx < SHN_LORESERVE)
8856
    {
8857
      /* The gABI doesn't support dynamic symbols in output sections
8858
	 beyond 64k.  */
8859
      (*_bfd_error_handler)
8860
	(_("%B: Too many sections: %d (>= %d)"),
8861
	 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8862
      bfd_set_error (bfd_error_nonrepresentable_section);
8863
      return FALSE;
8864
    }
8865
  return TRUE;
8866
}
8867
 
8868
/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8869
   allowing an unsatisfied unversioned symbol in the DSO to match a
8870
   versioned symbol that would normally require an explicit version.
8871
   We also handle the case that a DSO references a hidden symbol
8872
   which may be satisfied by a versioned symbol in another DSO.  */
8873
 
8874
static bfd_boolean
8875
elf_link_check_versioned_symbol (struct bfd_link_info *info,
8876
				 const struct elf_backend_data *bed,
8877
				 struct elf_link_hash_entry *h)
8878
{
8879
  bfd *abfd;
8880
  struct elf_link_loaded_list *loaded;
8881
 
8882
  if (!is_elf_hash_table (info->hash))
8883
    return FALSE;
8884
 
8885
  /* Check indirect symbol.  */
8886
  while (h->root.type == bfd_link_hash_indirect)
8887
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
8888
 
8889
  switch (h->root.type)
8890
    {
8891
    default:
8892
      abfd = NULL;
8893
      break;
8894
 
8895
    case bfd_link_hash_undefined:
8896
    case bfd_link_hash_undefweak:
8897
      abfd = h->root.u.undef.abfd;
8898
      if ((abfd->flags & DYNAMIC) == 0
8899
	  || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8900
	return FALSE;
8901
      break;
8902
 
8903
    case bfd_link_hash_defined:
8904
    case bfd_link_hash_defweak:
8905
      abfd = h->root.u.def.section->owner;
8906
      break;
8907
 
8908
    case bfd_link_hash_common:
8909
      abfd = h->root.u.c.p->section->owner;
8910
      break;
8911
    }
8912
  BFD_ASSERT (abfd != NULL);
8913
 
8914
  for (loaded = elf_hash_table (info)->loaded;
8915
       loaded != NULL;
8916
       loaded = loaded->next)
8917
    {
8918
      bfd *input;
8919
      Elf_Internal_Shdr *hdr;
8920
      bfd_size_type symcount;
8921
      bfd_size_type extsymcount;
8922
      bfd_size_type extsymoff;
8923
      Elf_Internal_Shdr *versymhdr;
8924
      Elf_Internal_Sym *isym;
8925
      Elf_Internal_Sym *isymend;
8926
      Elf_Internal_Sym *isymbuf;
8927
      Elf_External_Versym *ever;
8928
      Elf_External_Versym *extversym;
8929
 
8930
      input = loaded->abfd;
8931
 
8932
      /* We check each DSO for a possible hidden versioned definition.  */
8933
      if (input == abfd
8934
	  || (input->flags & DYNAMIC) == 0
8935
	  || elf_dynversym (input) == 0)
8936
	continue;
8937
 
8938
      hdr = &elf_tdata (input)->dynsymtab_hdr;
8939
 
8940
      symcount = hdr->sh_size / bed->s->sizeof_sym;
8941
      if (elf_bad_symtab (input))
8942
	{
8943
	  extsymcount = symcount;
8944
	  extsymoff = 0;
8945
	}
8946
      else
8947
	{
8948
	  extsymcount = symcount - hdr->sh_info;
8949
	  extsymoff = hdr->sh_info;
8950
	}
8951
 
8952
      if (extsymcount == 0)
8953
	continue;
8954
 
8955
      isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8956
				      NULL, NULL, NULL);
8957
      if (isymbuf == NULL)
8958
	return FALSE;
8959
 
8960
      /* Read in any version definitions.  */
8961
      versymhdr = &elf_tdata (input)->dynversym_hdr;
8962
      extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
8963
      if (extversym == NULL)
8964
	goto error_ret;
8965
 
8966
      if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8967
	  || (bfd_bread (extversym, versymhdr->sh_size, input)
8968
	      != versymhdr->sh_size))
8969
	{
8970
	  free (extversym);
8971
	error_ret:
8972
	  free (isymbuf);
8973
	  return FALSE;
8974
	}
8975
 
8976
      ever = extversym + extsymoff;
8977
      isymend = isymbuf + extsymcount;
8978
      for (isym = isymbuf; isym < isymend; isym++, ever++)
8979
	{
8980
	  const char *name;
8981
	  Elf_Internal_Versym iver;
8982
	  unsigned short version_index;
8983
 
8984
	  if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8985
	      || isym->st_shndx == SHN_UNDEF)
8986
	    continue;
8987
 
8988
	  name = bfd_elf_string_from_elf_section (input,
8989
						  hdr->sh_link,
8990
						  isym->st_name);
8991
	  if (strcmp (name, h->root.root.string) != 0)
8992
	    continue;
8993
 
8994
	  _bfd_elf_swap_versym_in (input, ever, &iver);
8995
 
8996
	  if ((iver.vs_vers & VERSYM_HIDDEN) == 0
8997
	      && !(h->def_regular
8998
		   && h->forced_local))
8999
	    {
9000
	      /* If we have a non-hidden versioned sym, then it should
9001
		 have provided a definition for the undefined sym unless
9002
		 it is defined in a non-shared object and forced local.
9003
	       */
9004
	      abort ();
9005
	    }
9006
 
9007
	  version_index = iver.vs_vers & VERSYM_VERSION;
9008
	  if (version_index == 1 || version_index == 2)
9009
	    {
9010
	      /* This is the base or first version.  We can use it.  */
9011
	      free (extversym);
9012
	      free (isymbuf);
9013
	      return TRUE;
9014
	    }
9015
	}
9016
 
9017
      free (extversym);
9018
      free (isymbuf);
9019
    }
9020
 
9021
  return FALSE;
9022
}
9023
 
9024
/* Add an external symbol to the symbol table.  This is called from
9025
   the hash table traversal routine.  When generating a shared object,
9026
   we go through the symbol table twice.  The first time we output
9027
   anything that might have been forced to local scope in a version
9028
   script.  The second time we output the symbols that are still
9029
   global symbols.  */
9030
 
9031
static bfd_boolean
9032
elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9033
{
9034
  struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9035
  struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9036
  struct elf_final_link_info *flinfo = eoinfo->flinfo;
9037
  bfd_boolean strip;
9038
  Elf_Internal_Sym sym;
9039
  asection *input_sec;
9040
  const struct elf_backend_data *bed;
9041
  long indx;
9042
  int ret;
6324 serge 9043
  /* A symbol is bound locally if it is forced local or it is locally
9044
     defined, hidden versioned, not referenced by shared library and
9045
     not exported when linking executable.  */
9046
  bfd_boolean local_bind = (h->forced_local
9047
			    || (bfd_link_executable (flinfo->info)
9048
				&& !flinfo->info->export_dynamic
9049
				&& !h->dynamic
9050
				&& !h->ref_dynamic
9051
				&& h->def_regular
9052
				&& h->versioned == versioned_hidden));
5197 serge 9053
 
9054
  if (h->root.type == bfd_link_hash_warning)
9055
    {
9056
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
9057
      if (h->root.type == bfd_link_hash_new)
9058
	return TRUE;
9059
    }
9060
 
9061
  /* Decide whether to output this symbol in this pass.  */
9062
  if (eoinfo->localsyms)
9063
    {
6324 serge 9064
      if (!local_bind)
5197 serge 9065
	return TRUE;
9066
    }
9067
  else
9068
    {
6324 serge 9069
      if (local_bind)
5197 serge 9070
	return TRUE;
9071
    }
9072
 
9073
  bed = get_elf_backend_data (flinfo->output_bfd);
9074
 
9075
  if (h->root.type == bfd_link_hash_undefined)
9076
    {
9077
      /* If we have an undefined symbol reference here then it must have
9078
	 come from a shared library that is being linked in.  (Undefined
9079
	 references in regular files have already been handled unless
9080
	 they are in unreferenced sections which are removed by garbage
9081
	 collection).  */
9082
      bfd_boolean ignore_undef = FALSE;
9083
 
9084
      /* Some symbols may be special in that the fact that they're
9085
	 undefined can be safely ignored - let backend determine that.  */
9086
      if (bed->elf_backend_ignore_undef_symbol)
9087
	ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9088
 
9089
      /* If we are reporting errors for this situation then do so now.  */
9090
      if (!ignore_undef
9091
	  && h->ref_dynamic
9092
	  && (!h->ref_regular || flinfo->info->gc_sections)
9093
	  && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9094
	  && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9095
	{
9096
	  if (!(flinfo->info->callbacks->undefined_symbol
9097
		(flinfo->info, h->root.root.string,
9098
		 h->ref_regular ? NULL : h->root.u.undef.abfd,
9099
		 NULL, 0,
9100
		 (flinfo->info->unresolved_syms_in_shared_libs
9101
		  == RM_GENERATE_ERROR))))
9102
	    {
9103
	      bfd_set_error (bfd_error_bad_value);
9104
	      eoinfo->failed = TRUE;
9105
	      return FALSE;
9106
	    }
9107
	}
9108
    }
9109
 
9110
  /* We should also warn if a forced local symbol is referenced from
9111
     shared libraries.  */
6324 serge 9112
  if (bfd_link_executable (flinfo->info)
5197 serge 9113
      && h->forced_local
9114
      && h->ref_dynamic
9115
      && h->def_regular
9116
      && !h->dynamic_def
9117
      && h->ref_dynamic_nonweak
9118
      && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9119
    {
9120
      bfd *def_bfd;
9121
      const char *msg;
9122
      struct elf_link_hash_entry *hi = h;
9123
 
9124
      /* Check indirect symbol.  */
9125
      while (hi->root.type == bfd_link_hash_indirect)
9126
	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9127
 
9128
      if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9129
	msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9130
      else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9131
	msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9132
      else
9133
	msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9134
      def_bfd = flinfo->output_bfd;
9135
      if (hi->root.u.def.section != bfd_abs_section_ptr)
9136
	def_bfd = hi->root.u.def.section->owner;
9137
      (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
9138
			     h->root.root.string);
9139
      bfd_set_error (bfd_error_bad_value);
9140
      eoinfo->failed = TRUE;
9141
      return FALSE;
9142
    }
9143
 
9144
  /* We don't want to output symbols that have never been mentioned by
9145
     a regular file, or that we have been told to strip.  However, if
9146
     h->indx is set to -2, the symbol is used by a reloc and we must
9147
     output it.  */
9148
  if (h->indx == -2)
9149
    strip = FALSE;
9150
  else if ((h->def_dynamic
9151
	    || h->ref_dynamic
9152
	    || h->root.type == bfd_link_hash_new)
9153
	   && !h->def_regular
9154
	   && !h->ref_regular)
9155
    strip = TRUE;
9156
  else if (flinfo->info->strip == strip_all)
9157
    strip = TRUE;
9158
  else if (flinfo->info->strip == strip_some
9159
	   && bfd_hash_lookup (flinfo->info->keep_hash,
9160
			       h->root.root.string, FALSE, FALSE) == NULL)
9161
    strip = TRUE;
9162
  else if ((h->root.type == bfd_link_hash_defined
9163
	    || h->root.type == bfd_link_hash_defweak)
9164
	   && ((flinfo->info->strip_discarded
9165
		&& discarded_section (h->root.u.def.section))
6324 serge 9166
	       || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9167
		   && h->root.u.def.section->owner != NULL
5197 serge 9168
		   && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9169
    strip = TRUE;
9170
  else if ((h->root.type == bfd_link_hash_undefined
9171
	    || h->root.type == bfd_link_hash_undefweak)
9172
	   && h->root.u.undef.abfd != NULL
9173
	   && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9174
    strip = TRUE;
9175
  else
9176
    strip = FALSE;
9177
 
9178
  /* If we're stripping it, and it's not a dynamic symbol, there's
9179
     nothing else to do unless it is a forced local symbol or a
9180
     STT_GNU_IFUNC symbol.  */
9181
  if (strip
9182
      && h->dynindx == -1
9183
      && h->type != STT_GNU_IFUNC
9184
      && !h->forced_local)
9185
    return TRUE;
9186
 
9187
  sym.st_value = 0;
9188
  sym.st_size = h->size;
9189
  sym.st_other = h->other;
6324 serge 9190
  if (local_bind)
5197 serge 9191
    {
9192
      sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
9193
      /* Turn off visibility on local symbol.  */
9194
      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9195
    }
9196
  /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
9197
  else if (h->unique_global && h->def_regular)
9198
    sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
9199
  else if (h->root.type == bfd_link_hash_undefweak
9200
	   || h->root.type == bfd_link_hash_defweak)
9201
    sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
9202
  else
9203
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
9204
  sym.st_target_internal = h->target_internal;
9205
 
9206
  switch (h->root.type)
9207
    {
9208
    default:
9209
    case bfd_link_hash_new:
9210
    case bfd_link_hash_warning:
9211
      abort ();
9212
      return FALSE;
9213
 
9214
    case bfd_link_hash_undefined:
9215
    case bfd_link_hash_undefweak:
9216
      input_sec = bfd_und_section_ptr;
9217
      sym.st_shndx = SHN_UNDEF;
9218
      break;
9219
 
9220
    case bfd_link_hash_defined:
9221
    case bfd_link_hash_defweak:
9222
      {
9223
	input_sec = h->root.u.def.section;
9224
	if (input_sec->output_section != NULL)
9225
	  {
9226
	    sym.st_shndx =
9227
	      _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9228
						 input_sec->output_section);
9229
	    if (sym.st_shndx == SHN_BAD)
9230
	      {
9231
		(*_bfd_error_handler)
9232
		  (_("%B: could not find output section %A for input section %A"),
9233
		   flinfo->output_bfd, input_sec->output_section, input_sec);
9234
		bfd_set_error (bfd_error_nonrepresentable_section);
9235
		eoinfo->failed = TRUE;
9236
		return FALSE;
9237
	      }
9238
 
9239
	    /* ELF symbols in relocatable files are section relative,
9240
	       but in nonrelocatable files they are virtual
9241
	       addresses.  */
9242
	    sym.st_value = h->root.u.def.value + input_sec->output_offset;
6324 serge 9243
	    if (!bfd_link_relocatable (flinfo->info))
5197 serge 9244
	      {
9245
		sym.st_value += input_sec->output_section->vma;
9246
		if (h->type == STT_TLS)
9247
		  {
9248
		    asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9249
		    if (tls_sec != NULL)
9250
		      sym.st_value -= tls_sec->vma;
9251
		  }
9252
	      }
9253
	  }
9254
	else
9255
	  {
9256
	    BFD_ASSERT (input_sec->owner == NULL
9257
			|| (input_sec->owner->flags & DYNAMIC) != 0);
9258
	    sym.st_shndx = SHN_UNDEF;
9259
	    input_sec = bfd_und_section_ptr;
9260
	  }
9261
      }
9262
      break;
9263
 
9264
    case bfd_link_hash_common:
9265
      input_sec = h->root.u.c.p->section;
9266
      sym.st_shndx = bed->common_section_index (input_sec);
9267
      sym.st_value = 1 << h->root.u.c.p->alignment_power;
9268
      break;
9269
 
9270
    case bfd_link_hash_indirect:
9271
      /* These symbols are created by symbol versioning.  They point
9272
	 to the decorated version of the name.  For example, if the
9273
	 symbol foo@@GNU_1.2 is the default, which should be used when
9274
	 foo is used with no version, then we add an indirect symbol
9275
	 foo which points to foo@@GNU_1.2.  We ignore these symbols,
9276
	 since the indirected symbol is already in the hash table.  */
9277
      return TRUE;
9278
    }
9279
 
9280
  /* Give the processor backend a chance to tweak the symbol value,
9281
     and also to finish up anything that needs to be done for this
9282
     symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
9283
     forced local syms when non-shared is due to a historical quirk.
9284
     STT_GNU_IFUNC symbol must go through PLT.  */
9285
  if ((h->type == STT_GNU_IFUNC
9286
       && h->def_regular
6324 serge 9287
       && !bfd_link_relocatable (flinfo->info))
5197 serge 9288
      || ((h->dynindx != -1
9289
	   || h->forced_local)
6324 serge 9290
	  && ((bfd_link_pic (flinfo->info)
5197 serge 9291
	       && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9292
		   || h->root.type != bfd_link_hash_undefweak))
9293
	      || !h->forced_local)
9294
	  && elf_hash_table (flinfo->info)->dynamic_sections_created))
9295
    {
9296
      if (! ((*bed->elf_backend_finish_dynamic_symbol)
9297
	     (flinfo->output_bfd, flinfo->info, h, &sym)))
9298
	{
9299
	  eoinfo->failed = TRUE;
9300
	  return FALSE;
9301
	}
9302
    }
9303
 
9304
  /* If we are marking the symbol as undefined, and there are no
9305
     non-weak references to this symbol from a regular object, then
9306
     mark the symbol as weak undefined; if there are non-weak
9307
     references, mark the symbol as strong.  We can't do this earlier,
9308
     because it might not be marked as undefined until the
9309
     finish_dynamic_symbol routine gets through with it.  */
9310
  if (sym.st_shndx == SHN_UNDEF
9311
      && h->ref_regular
9312
      && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9313
	  || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9314
    {
9315
      int bindtype;
9316
      unsigned int type = ELF_ST_TYPE (sym.st_info);
9317
 
9318
      /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9319
      if (type == STT_GNU_IFUNC)
9320
	type = STT_FUNC;
9321
 
9322
      if (h->ref_regular_nonweak)
9323
	bindtype = STB_GLOBAL;
9324
      else
9325
	bindtype = STB_WEAK;
9326
      sym.st_info = ELF_ST_INFO (bindtype, type);
9327
    }
9328
 
9329
  /* If this is a symbol defined in a dynamic library, don't use the
9330
     symbol size from the dynamic library.  Relinking an executable
9331
     against a new library may introduce gratuitous changes in the
9332
     executable's symbols if we keep the size.  */
9333
  if (sym.st_shndx == SHN_UNDEF
9334
      && !h->def_regular
9335
      && h->def_dynamic)
9336
    sym.st_size = 0;
9337
 
9338
  /* If a non-weak symbol with non-default visibility is not defined
9339
     locally, it is a fatal error.  */
6324 serge 9340
  if (!bfd_link_relocatable (flinfo->info)
5197 serge 9341
      && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9342
      && ELF_ST_BIND (sym.st_info) != STB_WEAK
9343
      && h->root.type == bfd_link_hash_undefined
9344
      && !h->def_regular)
9345
    {
9346
      const char *msg;
9347
 
9348
      if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9349
	msg = _("%B: protected symbol `%s' isn't defined");
9350
      else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9351
	msg = _("%B: internal symbol `%s' isn't defined");
9352
      else
9353
	msg = _("%B: hidden symbol `%s' isn't defined");
9354
      (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
9355
      bfd_set_error (bfd_error_bad_value);
9356
      eoinfo->failed = TRUE;
9357
      return FALSE;
9358
    }
9359
 
9360
  /* If this symbol should be put in the .dynsym section, then put it
9361
     there now.  We already know the symbol index.  We also fill in
9362
     the entry in the .hash section.  */
6324 serge 9363
  if (elf_hash_table (flinfo->info)->dynsym != NULL
5197 serge 9364
      && h->dynindx != -1
9365
      && elf_hash_table (flinfo->info)->dynamic_sections_created)
9366
    {
9367
      bfd_byte *esym;
9368
 
9369
      /* Since there is no version information in the dynamic string,
9370
	 if there is no version info in symbol version section, we will
6324 serge 9371
	 have a run-time problem if not linking executable, referenced
9372
	 by shared library, not locally defined, or not bound locally.
9373
      */
9374
      if (h->verinfo.verdef == NULL
9375
	  && !local_bind
9376
	  && (!bfd_link_executable (flinfo->info)
9377
	      || h->ref_dynamic
9378
	      || !h->def_regular))
5197 serge 9379
	{
9380
	  char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9381
 
9382
	  if (p && p [1] != '\0')
9383
	    {
9384
	      (*_bfd_error_handler)
9385
		(_("%B: No symbol version section for versioned symbol `%s'"),
9386
		 flinfo->output_bfd, h->root.root.string);
9387
	      eoinfo->failed = TRUE;
9388
	      return FALSE;
9389
	    }
9390
	}
9391
 
9392
      sym.st_name = h->dynstr_index;
6324 serge 9393
      esym = (elf_hash_table (flinfo->info)->dynsym->contents
9394
	      + h->dynindx * bed->s->sizeof_sym);
5197 serge 9395
      if (!check_dynsym (flinfo->output_bfd, &sym))
9396
	{
9397
	  eoinfo->failed = TRUE;
9398
	  return FALSE;
9399
	}
9400
      bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9401
 
9402
      if (flinfo->hash_sec != NULL)
9403
	{
9404
	  size_t hash_entry_size;
9405
	  bfd_byte *bucketpos;
9406
	  bfd_vma chain;
9407
	  size_t bucketcount;
9408
	  size_t bucket;
9409
 
9410
	  bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9411
	  bucket = h->u.elf_hash_value % bucketcount;
9412
 
9413
	  hash_entry_size
9414
	    = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9415
	  bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9416
		       + (bucket + 2) * hash_entry_size);
9417
	  chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9418
	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9419
		   bucketpos);
9420
	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9421
		   ((bfd_byte *) flinfo->hash_sec->contents
9422
		    + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9423
	}
9424
 
9425
      if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9426
	{
9427
	  Elf_Internal_Versym iversym;
9428
	  Elf_External_Versym *eversym;
9429
 
9430
	  if (!h->def_regular)
9431
	    {
6324 serge 9432
	      if (h->verinfo.verdef == NULL
9433
		  || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9434
		      & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
5197 serge 9435
		iversym.vs_vers = 0;
9436
	      else
9437
		iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9438
	    }
9439
	  else
9440
	    {
9441
	      if (h->verinfo.vertree == NULL)
9442
		iversym.vs_vers = 1;
9443
	      else
9444
		iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9445
	      if (flinfo->info->create_default_symver)
9446
		iversym.vs_vers++;
9447
	    }
9448
 
6324 serge 9449
	  /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9450
	     defined locally.  */
9451
	  if (h->versioned == versioned_hidden && h->def_regular)
5197 serge 9452
	    iversym.vs_vers |= VERSYM_HIDDEN;
9453
 
9454
	  eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9455
	  eversym += h->dynindx;
9456
	  _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9457
	}
9458
    }
9459
 
9460
  /* If we're stripping it, then it was just a dynamic symbol, and
9461
     there's nothing else to do.  */
9462
  if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
9463
    return TRUE;
9464
 
6324 serge 9465
  /* Output a FILE symbol so that following locals are not associated
9466
     with the wrong input file.  We need one for forced local symbols
9467
     if we've seen more than one FILE symbol or when we have exactly
9468
     one FILE symbol but global symbols are present in a file other
9469
     than the one with the FILE symbol.  We also need one if linker
9470
     defined symbols are present.  In practice these conditions are
9471
     always met, so just emit the FILE symbol unconditionally.  */
9472
  if (eoinfo->localsyms
9473
      && !eoinfo->file_sym_done
9474
      && eoinfo->flinfo->filesym_count != 0)
9475
    {
9476
      Elf_Internal_Sym fsym;
9477
 
9478
      memset (&fsym, 0, sizeof (fsym));
9479
      fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9480
      fsym.st_shndx = SHN_ABS;
9481
      if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9482
				      bfd_und_section_ptr, NULL))
9483
	return FALSE;
9484
 
9485
      eoinfo->file_sym_done = TRUE;
9486
    }
9487
 
5197 serge 9488
  indx = bfd_get_symcount (flinfo->output_bfd);
6324 serge 9489
  ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9490
				   input_sec, h);
5197 serge 9491
  if (ret == 0)
9492
    {
9493
      eoinfo->failed = TRUE;
9494
      return FALSE;
9495
    }
9496
  else if (ret == 1)
9497
    h->indx = indx;
9498
  else if (h->indx == -2)
9499
    abort();
9500
 
9501
  return TRUE;
9502
}
9503
 
9504
/* Return TRUE if special handling is done for relocs in SEC against
9505
   symbols defined in discarded sections.  */
9506
 
9507
static bfd_boolean
9508
elf_section_ignore_discarded_relocs (asection *sec)
9509
{
9510
  const struct elf_backend_data *bed;
9511
 
9512
  switch (sec->sec_info_type)
9513
    {
9514
    case SEC_INFO_TYPE_STABS:
9515
    case SEC_INFO_TYPE_EH_FRAME:
6324 serge 9516
    case SEC_INFO_TYPE_EH_FRAME_ENTRY:
5197 serge 9517
      return TRUE;
9518
    default:
9519
      break;
9520
    }
9521
 
9522
  bed = get_elf_backend_data (sec->owner);
9523
  if (bed->elf_backend_ignore_discarded_relocs != NULL
9524
      && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9525
    return TRUE;
9526
 
9527
  return FALSE;
9528
}
9529
 
9530
/* Return a mask saying how ld should treat relocations in SEC against
9531
   symbols defined in discarded sections.  If this function returns
9532
   COMPLAIN set, ld will issue a warning message.  If this function
9533
   returns PRETEND set, and the discarded section was link-once and the
9534
   same size as the kept link-once section, ld will pretend that the
9535
   symbol was actually defined in the kept section.  Otherwise ld will
9536
   zero the reloc (at least that is the intent, but some cooperation by
9537
   the target dependent code is needed, particularly for REL targets).  */
9538
 
9539
unsigned int
9540
_bfd_elf_default_action_discarded (asection *sec)
9541
{
9542
  if (sec->flags & SEC_DEBUGGING)
9543
    return PRETEND;
9544
 
9545
  if (strcmp (".eh_frame", sec->name) == 0)
9546
    return 0;
9547
 
9548
  if (strcmp (".gcc_except_table", sec->name) == 0)
9549
    return 0;
9550
 
9551
  return COMPLAIN | PRETEND;
9552
}
9553
 
9554
/* Find a match between a section and a member of a section group.  */
9555
 
9556
static asection *
9557
match_group_member (asection *sec, asection *group,
9558
		    struct bfd_link_info *info)
9559
{
9560
  asection *first = elf_next_in_group (group);
9561
  asection *s = first;
9562
 
9563
  while (s != NULL)
9564
    {
9565
      if (bfd_elf_match_symbols_in_sections (s, sec, info))
9566
	return s;
9567
 
9568
      s = elf_next_in_group (s);
9569
      if (s == first)
9570
	break;
9571
    }
9572
 
9573
  return NULL;
9574
}
9575
 
9576
/* Check if the kept section of a discarded section SEC can be used
9577
   to replace it.  Return the replacement if it is OK.  Otherwise return
9578
   NULL.  */
9579
 
9580
asection *
9581
_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9582
{
9583
  asection *kept;
9584
 
9585
  kept = sec->kept_section;
9586
  if (kept != NULL)
9587
    {
9588
      if ((kept->flags & SEC_GROUP) != 0)
9589
	kept = match_group_member (sec, kept, info);
9590
      if (kept != NULL
9591
	  && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9592
	      != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9593
	kept = NULL;
9594
      sec->kept_section = kept;
9595
    }
9596
  return kept;
9597
}
9598
 
9599
/* Link an input file into the linker output file.  This function
9600
   handles all the sections and relocations of the input file at once.
9601
   This is so that we only have to read the local symbols once, and
9602
   don't have to keep them in memory.  */
9603
 
9604
static bfd_boolean
9605
elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9606
{
9607
  int (*relocate_section)
9608
    (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9609
     Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9610
  bfd *output_bfd;
9611
  Elf_Internal_Shdr *symtab_hdr;
9612
  size_t locsymcount;
9613
  size_t extsymoff;
9614
  Elf_Internal_Sym *isymbuf;
9615
  Elf_Internal_Sym *isym;
9616
  Elf_Internal_Sym *isymend;
9617
  long *pindex;
9618
  asection **ppsection;
9619
  asection *o;
9620
  const struct elf_backend_data *bed;
9621
  struct elf_link_hash_entry **sym_hashes;
9622
  bfd_size_type address_size;
9623
  bfd_vma r_type_mask;
9624
  int r_sym_shift;
9625
  bfd_boolean have_file_sym = FALSE;
9626
 
9627
  output_bfd = flinfo->output_bfd;
9628
  bed = get_elf_backend_data (output_bfd);
9629
  relocate_section = bed->elf_backend_relocate_section;
9630
 
9631
  /* If this is a dynamic object, we don't want to do anything here:
9632
     we don't want the local symbols, and we don't want the section
9633
     contents.  */
9634
  if ((input_bfd->flags & DYNAMIC) != 0)
9635
    return TRUE;
9636
 
9637
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9638
  if (elf_bad_symtab (input_bfd))
9639
    {
9640
      locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9641
      extsymoff = 0;
9642
    }
9643
  else
9644
    {
9645
      locsymcount = symtab_hdr->sh_info;
9646
      extsymoff = symtab_hdr->sh_info;
9647
    }
9648
 
9649
  /* Read the local symbols.  */
9650
  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9651
  if (isymbuf == NULL && locsymcount != 0)
9652
    {
9653
      isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9654
				      flinfo->internal_syms,
9655
				      flinfo->external_syms,
9656
				      flinfo->locsym_shndx);
9657
      if (isymbuf == NULL)
9658
	return FALSE;
9659
    }
9660
 
9661
  /* Find local symbol sections and adjust values of symbols in
9662
     SEC_MERGE sections.  Write out those local symbols we know are
9663
     going into the output file.  */
9664
  isymend = isymbuf + locsymcount;
9665
  for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9666
       isym < isymend;
9667
       isym++, pindex++, ppsection++)
9668
    {
9669
      asection *isec;
9670
      const char *name;
9671
      Elf_Internal_Sym osym;
9672
      long indx;
9673
      int ret;
9674
 
9675
      *pindex = -1;
9676
 
9677
      if (elf_bad_symtab (input_bfd))
9678
	{
9679
	  if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9680
	    {
9681
	      *ppsection = NULL;
9682
	      continue;
9683
	    }
9684
	}
9685
 
9686
      if (isym->st_shndx == SHN_UNDEF)
9687
	isec = bfd_und_section_ptr;
9688
      else if (isym->st_shndx == SHN_ABS)
9689
	isec = bfd_abs_section_ptr;
9690
      else if (isym->st_shndx == SHN_COMMON)
9691
	isec = bfd_com_section_ptr;
9692
      else
9693
	{
9694
	  isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9695
	  if (isec == NULL)
9696
	    {
9697
	      /* Don't attempt to output symbols with st_shnx in the
9698
		 reserved range other than SHN_ABS and SHN_COMMON.  */
9699
	      *ppsection = NULL;
9700
	      continue;
9701
	    }
9702
	  else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9703
		   && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9704
	    isym->st_value =
9705
	      _bfd_merged_section_offset (output_bfd, &isec,
9706
					  elf_section_data (isec)->sec_info,
9707
					  isym->st_value);
9708
	}
9709
 
9710
      *ppsection = isec;
9711
 
9712
      /* Don't output the first, undefined, symbol.  */
9713
      if (ppsection == flinfo->sections)
9714
	continue;
9715
 
9716
      if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9717
	{
9718
	  /* We never output section symbols.  Instead, we use the
9719
	     section symbol of the corresponding section in the output
9720
	     file.  */
9721
	  continue;
9722
	}
9723
 
9724
      /* If we are stripping all symbols, we don't want to output this
9725
	 one.  */
9726
      if (flinfo->info->strip == strip_all)
9727
	continue;
9728
 
9729
      /* If we are discarding all local symbols, we don't want to
9730
	 output this one.  If we are generating a relocatable output
9731
	 file, then some of the local symbols may be required by
9732
	 relocs; we output them below as we discover that they are
9733
	 needed.  */
9734
      if (flinfo->info->discard == discard_all)
9735
	continue;
9736
 
9737
      /* If this symbol is defined in a section which we are
9738
	 discarding, we don't need to keep it.  */
9739
      if (isym->st_shndx != SHN_UNDEF
9740
	  && isym->st_shndx < SHN_LORESERVE
9741
	  && bfd_section_removed_from_list (output_bfd,
9742
					    isec->output_section))
9743
	continue;
9744
 
9745
      /* Get the name of the symbol.  */
9746
      name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9747
					      isym->st_name);
9748
      if (name == NULL)
9749
	return FALSE;
9750
 
9751
      /* See if we are discarding symbols with this name.  */
9752
      if ((flinfo->info->strip == strip_some
9753
	   && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9754
	       == NULL))
9755
	  || (((flinfo->info->discard == discard_sec_merge
6324 serge 9756
		&& (isec->flags & SEC_MERGE)
9757
		&& !bfd_link_relocatable (flinfo->info))
5197 serge 9758
	       || flinfo->info->discard == discard_l)
9759
	      && bfd_is_local_label_name (input_bfd, name)))
9760
	continue;
9761
 
9762
      if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9763
	{
6324 serge 9764
	  if (input_bfd->lto_output)
9765
	    /* -flto puts a temp file name here.  This means builds
9766
	       are not reproducible.  Discard the symbol.  */
9767
	    continue;
5197 serge 9768
	  have_file_sym = TRUE;
9769
	  flinfo->filesym_count += 1;
9770
	}
9771
      if (!have_file_sym)
9772
	{
9773
	  /* In the absence of debug info, bfd_find_nearest_line uses
9774
	     FILE symbols to determine the source file for local
9775
	     function symbols.  Provide a FILE symbol here if input
9776
	     files lack such, so that their symbols won't be
9777
	     associated with a previous input file.  It's not the
9778
	     source file, but the best we can do.  */
9779
	  have_file_sym = TRUE;
9780
	  flinfo->filesym_count += 1;
9781
	  memset (&osym, 0, sizeof (osym));
9782
	  osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9783
	  osym.st_shndx = SHN_ABS;
6324 serge 9784
	  if (!elf_link_output_symstrtab (flinfo,
9785
					  (input_bfd->lto_output ? NULL
9786
					   : input_bfd->filename),
9787
					  &osym, bfd_abs_section_ptr,
9788
					  NULL))
5197 serge 9789
	    return FALSE;
9790
	}
9791
 
9792
      osym = *isym;
9793
 
9794
      /* Adjust the section index for the output file.  */
9795
      osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9796
							 isec->output_section);
9797
      if (osym.st_shndx == SHN_BAD)
9798
	return FALSE;
9799
 
9800
      /* ELF symbols in relocatable files are section relative, but
9801
	 in executable files they are virtual addresses.  Note that
9802
	 this code assumes that all ELF sections have an associated
9803
	 BFD section with a reasonable value for output_offset; below
9804
	 we assume that they also have a reasonable value for
9805
	 output_section.  Any special sections must be set up to meet
9806
	 these requirements.  */
9807
      osym.st_value += isec->output_offset;
6324 serge 9808
      if (!bfd_link_relocatable (flinfo->info))
5197 serge 9809
	{
9810
	  osym.st_value += isec->output_section->vma;
9811
	  if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9812
	    {
9813
	      /* STT_TLS symbols are relative to PT_TLS segment base.  */
9814
	      BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9815
	      osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
9816
	    }
9817
	}
9818
 
9819
      indx = bfd_get_symcount (output_bfd);
6324 serge 9820
      ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
5197 serge 9821
      if (ret == 0)
9822
	return FALSE;
9823
      else if (ret == 1)
9824
	*pindex = indx;
9825
    }
9826
 
9827
  if (bed->s->arch_size == 32)
9828
    {
9829
      r_type_mask = 0xff;
9830
      r_sym_shift = 8;
9831
      address_size = 4;
9832
    }
9833
  else
9834
    {
9835
      r_type_mask = 0xffffffff;
9836
      r_sym_shift = 32;
9837
      address_size = 8;
9838
    }
9839
 
9840
  /* Relocate the contents of each section.  */
9841
  sym_hashes = elf_sym_hashes (input_bfd);
9842
  for (o = input_bfd->sections; o != NULL; o = o->next)
9843
    {
9844
      bfd_byte *contents;
9845
 
9846
      if (! o->linker_mark)
9847
	{
9848
	  /* This section was omitted from the link.  */
9849
	  continue;
9850
	}
9851
 
6324 serge 9852
      if (bfd_link_relocatable (flinfo->info)
5197 serge 9853
	  && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9854
	{
9855
	  /* Deal with the group signature symbol.  */
9856
	  struct bfd_elf_section_data *sec_data = elf_section_data (o);
9857
	  unsigned long symndx = sec_data->this_hdr.sh_info;
9858
	  asection *osec = o->output_section;
9859
 
9860
	  if (symndx >= locsymcount
9861
	      || (elf_bad_symtab (input_bfd)
9862
		  && flinfo->sections[symndx] == NULL))
9863
	    {
9864
	      struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9865
	      while (h->root.type == bfd_link_hash_indirect
9866
		     || h->root.type == bfd_link_hash_warning)
9867
		h = (struct elf_link_hash_entry *) h->root.u.i.link;
9868
	      /* Arrange for symbol to be output.  */
9869
	      h->indx = -2;
9870
	      elf_section_data (osec)->this_hdr.sh_info = -2;
9871
	    }
9872
	  else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9873
	    {
9874
	      /* We'll use the output section target_index.  */
9875
	      asection *sec = flinfo->sections[symndx]->output_section;
9876
	      elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9877
	    }
9878
	  else
9879
	    {
9880
	      if (flinfo->indices[symndx] == -1)
9881
		{
9882
		  /* Otherwise output the local symbol now.  */
9883
		  Elf_Internal_Sym sym = isymbuf[symndx];
9884
		  asection *sec = flinfo->sections[symndx]->output_section;
9885
		  const char *name;
9886
		  long indx;
9887
		  int ret;
9888
 
9889
		  name = bfd_elf_string_from_elf_section (input_bfd,
9890
							  symtab_hdr->sh_link,
9891
							  sym.st_name);
9892
		  if (name == NULL)
9893
		    return FALSE;
9894
 
9895
		  sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9896
								    sec);
9897
		  if (sym.st_shndx == SHN_BAD)
9898
		    return FALSE;
9899
 
9900
		  sym.st_value += o->output_offset;
9901
 
9902
		  indx = bfd_get_symcount (output_bfd);
6324 serge 9903
		  ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
9904
						   NULL);
5197 serge 9905
		  if (ret == 0)
9906
		    return FALSE;
9907
		  else if (ret == 1)
9908
		    flinfo->indices[symndx] = indx;
9909
		  else
9910
		    abort ();
9911
		}
9912
	      elf_section_data (osec)->this_hdr.sh_info
9913
		= flinfo->indices[symndx];
9914
	    }
9915
	}
9916
 
9917
      if ((o->flags & SEC_HAS_CONTENTS) == 0
9918
	  || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9919
	continue;
9920
 
9921
      if ((o->flags & SEC_LINKER_CREATED) != 0)
9922
	{
9923
	  /* Section was created by _bfd_elf_link_create_dynamic_sections
9924
	     or somesuch.  */
9925
	  continue;
9926
	}
9927
 
9928
      /* Get the contents of the section.  They have been cached by a
9929
	 relaxation routine.  Note that o is a section in an input
9930
	 file, so the contents field will not have been set by any of
9931
	 the routines which work on output files.  */
9932
      if (elf_section_data (o)->this_hdr.contents != NULL)
6324 serge 9933
	{
9934
	  contents = elf_section_data (o)->this_hdr.contents;
9935
	  if (bed->caches_rawsize
9936
	      && o->rawsize != 0
9937
	      && o->rawsize < o->size)
9938
	    {
9939
	      memcpy (flinfo->contents, contents, o->rawsize);
9940
	      contents = flinfo->contents;
9941
	    }
9942
	}
5197 serge 9943
      else
9944
	{
9945
	  contents = flinfo->contents;
9946
	  if (! bfd_get_full_section_contents (input_bfd, o, &contents))
9947
	    return FALSE;
9948
	}
9949
 
9950
      if ((o->flags & SEC_RELOC) != 0)
9951
	{
9952
	  Elf_Internal_Rela *internal_relocs;
9953
	  Elf_Internal_Rela *rel, *relend;
9954
	  int action_discarded;
9955
	  int ret;
9956
 
9957
	  /* Get the swapped relocs.  */
9958
	  internal_relocs
9959
	    = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
9960
					 flinfo->internal_relocs, FALSE);
9961
	  if (internal_relocs == NULL
9962
	      && o->reloc_count > 0)
9963
	    return FALSE;
9964
 
9965
	  /* We need to reverse-copy input .ctors/.dtors sections if
9966
	     they are placed in .init_array/.finit_array for output.  */
9967
	  if (o->size > address_size
9968
	      && ((strncmp (o->name, ".ctors", 6) == 0
9969
		   && strcmp (o->output_section->name,
9970
			      ".init_array") == 0)
9971
		  || (strncmp (o->name, ".dtors", 6) == 0
9972
		      && strcmp (o->output_section->name,
9973
				 ".fini_array") == 0))
9974
	      && (o->name[6] == 0 || o->name[6] == '.'))
9975
	    {
9976
	      if (o->size != o->reloc_count * address_size)
9977
		{
9978
		  (*_bfd_error_handler)
9979
		    (_("error: %B: size of section %A is not "
9980
		       "multiple of address size"),
9981
		     input_bfd, o);
9982
		  bfd_set_error (bfd_error_on_input);
9983
		  return FALSE;
9984
		}
9985
	      o->flags |= SEC_ELF_REVERSE_COPY;
9986
	    }
9987
 
9988
	  action_discarded = -1;
9989
	  if (!elf_section_ignore_discarded_relocs (o))
9990
	    action_discarded = (*bed->action_discarded) (o);
9991
 
9992
	  /* Run through the relocs evaluating complex reloc symbols and
9993
	     looking for relocs against symbols from discarded sections
9994
	     or section symbols from removed link-once sections.
9995
	     Complain about relocs against discarded sections.  Zero
9996
	     relocs against removed link-once sections.  */
9997
 
9998
	  rel = internal_relocs;
9999
	  relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10000
	  for ( ; rel < relend; rel++)
10001
	    {
10002
	      unsigned long r_symndx = rel->r_info >> r_sym_shift;
10003
	      unsigned int s_type;
10004
	      asection **ps, *sec;
10005
	      struct elf_link_hash_entry *h = NULL;
10006
	      const char *sym_name;
10007
 
10008
	      if (r_symndx == STN_UNDEF)
10009
		continue;
10010
 
10011
	      if (r_symndx >= locsymcount
10012
		  || (elf_bad_symtab (input_bfd)
10013
		      && flinfo->sections[r_symndx] == NULL))
10014
		{
10015
		  h = sym_hashes[r_symndx - extsymoff];
10016
 
10017
		  /* Badly formatted input files can contain relocs that
10018
		     reference non-existant symbols.  Check here so that
10019
		     we do not seg fault.  */
10020
		  if (h == NULL)
10021
		    {
10022
		      char buffer [32];
10023
 
10024
		      sprintf_vma (buffer, rel->r_info);
10025
		      (*_bfd_error_handler)
10026
			(_("error: %B contains a reloc (0x%s) for section %A "
10027
			   "that references a non-existent global symbol"),
10028
			 input_bfd, o, buffer);
10029
		      bfd_set_error (bfd_error_bad_value);
10030
		      return FALSE;
10031
		    }
10032
 
10033
		  while (h->root.type == bfd_link_hash_indirect
10034
			 || h->root.type == bfd_link_hash_warning)
10035
		    h = (struct elf_link_hash_entry *) h->root.u.i.link;
10036
 
10037
		  s_type = h->type;
10038
 
6324 serge 10039
		  /* If a plugin symbol is referenced from a non-IR file,
10040
		     mark the symbol as undefined.  Note that the
10041
		     linker may attach linker created dynamic sections
10042
		     to the plugin bfd.  Symbols defined in linker
10043
		     created sections are not plugin symbols.  */
10044
		  if (h->root.non_ir_ref
10045
		      && (h->root.type == bfd_link_hash_defined
10046
			  || h->root.type == bfd_link_hash_defweak)
10047
		      && (h->root.u.def.section->flags
10048
			  & SEC_LINKER_CREATED) == 0
10049
		      && h->root.u.def.section->owner != NULL
10050
		      && (h->root.u.def.section->owner->flags
10051
			  & BFD_PLUGIN) != 0)
10052
		    {
10053
		      h->root.type = bfd_link_hash_undefined;
10054
		      h->root.u.undef.abfd = h->root.u.def.section->owner;
10055
		    }
10056
 
5197 serge 10057
		  ps = NULL;
10058
		  if (h->root.type == bfd_link_hash_defined
10059
		      || h->root.type == bfd_link_hash_defweak)
10060
		    ps = &h->root.u.def.section;
10061
 
10062
		  sym_name = h->root.root.string;
10063
		}
10064
	      else
10065
		{
10066
		  Elf_Internal_Sym *sym = isymbuf + r_symndx;
10067
 
10068
		  s_type = ELF_ST_TYPE (sym->st_info);
10069
		  ps = &flinfo->sections[r_symndx];
10070
		  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10071
					       sym, *ps);
10072
		}
10073
 
10074
	      if ((s_type == STT_RELC || s_type == STT_SRELC)
6324 serge 10075
		  && !bfd_link_relocatable (flinfo->info))
5197 serge 10076
		{
10077
		  bfd_vma val;
10078
		  bfd_vma dot = (rel->r_offset
10079
				 + o->output_offset + o->output_section->vma);
10080
#ifdef DEBUG
10081
		  printf ("Encountered a complex symbol!");
10082
		  printf (" (input_bfd %s, section %s, reloc %ld\n",
10083
			  input_bfd->filename, o->name,
10084
			  (long) (rel - internal_relocs));
10085
		  printf (" symbol: idx  %8.8lx, name %s\n",
10086
			  r_symndx, sym_name);
10087
		  printf (" reloc : info %8.8lx, addr %8.8lx\n",
10088
			  (unsigned long) rel->r_info,
10089
			  (unsigned long) rel->r_offset);
10090
#endif
10091
		  if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10092
				    isymbuf, locsymcount, s_type == STT_SRELC))
10093
		    return FALSE;
10094
 
10095
		  /* Symbol evaluated OK.  Update to absolute value.  */
10096
		  set_symbol_value (input_bfd, isymbuf, locsymcount,
10097
				    r_symndx, val);
10098
		  continue;
10099
		}
10100
 
10101
	      if (action_discarded != -1 && ps != NULL)
10102
		{
10103
		  /* Complain if the definition comes from a
10104
		     discarded section.  */
10105
		  if ((sec = *ps) != NULL && discarded_section (sec))
10106
		    {
10107
		      BFD_ASSERT (r_symndx != STN_UNDEF);
10108
		      if (action_discarded & COMPLAIN)
10109
			(*flinfo->info->callbacks->einfo)
10110
			  (_("%X`%s' referenced in section `%A' of %B: "
10111
			     "defined in discarded section `%A' of %B\n"),
10112
			   sym_name, o, input_bfd, sec, sec->owner);
10113
 
10114
		      /* Try to do the best we can to support buggy old
10115
			 versions of gcc.  Pretend that the symbol is
10116
			 really defined in the kept linkonce section.
10117
			 FIXME: This is quite broken.  Modifying the
10118
			 symbol here means we will be changing all later
10119
			 uses of the symbol, not just in this section.  */
10120
		      if (action_discarded & PRETEND)
10121
			{
10122
			  asection *kept;
10123
 
10124
			  kept = _bfd_elf_check_kept_section (sec,
10125
							      flinfo->info);
10126
			  if (kept != NULL)
10127
			    {
10128
			      *ps = kept;
10129
			      continue;
10130
			    }
10131
			}
10132
		    }
10133
		}
10134
	    }
10135
 
10136
	  /* Relocate the section by invoking a back end routine.
10137
 
10138
	     The back end routine is responsible for adjusting the
10139
	     section contents as necessary, and (if using Rela relocs
10140
	     and generating a relocatable output file) adjusting the
10141
	     reloc addend as necessary.
10142
 
10143
	     The back end routine does not have to worry about setting
10144
	     the reloc address or the reloc symbol index.
10145
 
10146
	     The back end routine is given a pointer to the swapped in
10147
	     internal symbols, and can access the hash table entries
10148
	     for the external symbols via elf_sym_hashes (input_bfd).
10149
 
10150
	     When generating relocatable output, the back end routine
10151
	     must handle STB_LOCAL/STT_SECTION symbols specially.  The
10152
	     output symbol is going to be a section symbol
10153
	     corresponding to the output section, which will require
10154
	     the addend to be adjusted.  */
10155
 
10156
	  ret = (*relocate_section) (output_bfd, flinfo->info,
10157
				     input_bfd, o, contents,
10158
				     internal_relocs,
10159
				     isymbuf,
10160
				     flinfo->sections);
10161
	  if (!ret)
10162
	    return FALSE;
10163
 
10164
	  if (ret == 2
6324 serge 10165
	      || bfd_link_relocatable (flinfo->info)
5197 serge 10166
	      || flinfo->info->emitrelocations)
10167
	    {
10168
	      Elf_Internal_Rela *irela;
10169
	      Elf_Internal_Rela *irelaend, *irelamid;
10170
	      bfd_vma last_offset;
10171
	      struct elf_link_hash_entry **rel_hash;
10172
	      struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10173
	      Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10174
	      unsigned int next_erel;
10175
	      bfd_boolean rela_normal;
10176
	      struct bfd_elf_section_data *esdi, *esdo;
10177
 
10178
	      esdi = elf_section_data (o);
10179
	      esdo = elf_section_data (o->output_section);
10180
	      rela_normal = FALSE;
10181
 
10182
	      /* Adjust the reloc addresses and symbol indices.  */
10183
 
10184
	      irela = internal_relocs;
10185
	      irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10186
	      rel_hash = esdo->rel.hashes + esdo->rel.count;
10187
	      /* We start processing the REL relocs, if any.  When we reach
10188
		 IRELAMID in the loop, we switch to the RELA relocs.  */
10189
	      irelamid = irela;
10190
	      if (esdi->rel.hdr != NULL)
10191
		irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10192
			     * bed->s->int_rels_per_ext_rel);
10193
	      rel_hash_list = rel_hash;
10194
	      rela_hash_list = NULL;
10195
	      last_offset = o->output_offset;
6324 serge 10196
	      if (!bfd_link_relocatable (flinfo->info))
5197 serge 10197
		last_offset += o->output_section->vma;
10198
	      for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10199
		{
10200
		  unsigned long r_symndx;
10201
		  asection *sec;
10202
		  Elf_Internal_Sym sym;
10203
 
10204
		  if (next_erel == bed->s->int_rels_per_ext_rel)
10205
		    {
10206
		      rel_hash++;
10207
		      next_erel = 0;
10208
		    }
10209
 
10210
		  if (irela == irelamid)
10211
		    {
10212
		      rel_hash = esdo->rela.hashes + esdo->rela.count;
10213
		      rela_hash_list = rel_hash;
10214
		      rela_normal = bed->rela_normal;
10215
		    }
10216
 
10217
		  irela->r_offset = _bfd_elf_section_offset (output_bfd,
10218
							     flinfo->info, o,
10219
							     irela->r_offset);
10220
		  if (irela->r_offset >= (bfd_vma) -2)
10221
		    {
10222
		      /* This is a reloc for a deleted entry or somesuch.
10223
			 Turn it into an R_*_NONE reloc, at the same
10224
			 offset as the last reloc.  elf_eh_frame.c and
10225
			 bfd_elf_discard_info rely on reloc offsets
10226
			 being ordered.  */
10227
		      irela->r_offset = last_offset;
10228
		      irela->r_info = 0;
10229
		      irela->r_addend = 0;
10230
		      continue;
10231
		    }
10232
 
10233
		  irela->r_offset += o->output_offset;
10234
 
10235
		  /* Relocs in an executable have to be virtual addresses.  */
6324 serge 10236
		  if (!bfd_link_relocatable (flinfo->info))
5197 serge 10237
		    irela->r_offset += o->output_section->vma;
10238
 
10239
		  last_offset = irela->r_offset;
10240
 
10241
		  r_symndx = irela->r_info >> r_sym_shift;
10242
		  if (r_symndx == STN_UNDEF)
10243
		    continue;
10244
 
10245
		  if (r_symndx >= locsymcount
10246
		      || (elf_bad_symtab (input_bfd)
10247
			  && flinfo->sections[r_symndx] == NULL))
10248
		    {
10249
		      struct elf_link_hash_entry *rh;
10250
		      unsigned long indx;
10251
 
10252
		      /* This is a reloc against a global symbol.  We
10253
			 have not yet output all the local symbols, so
10254
			 we do not know the symbol index of any global
10255
			 symbol.  We set the rel_hash entry for this
10256
			 reloc to point to the global hash table entry
10257
			 for this symbol.  The symbol index is then
10258
			 set at the end of bfd_elf_final_link.  */
10259
		      indx = r_symndx - extsymoff;
10260
		      rh = elf_sym_hashes (input_bfd)[indx];
10261
		      while (rh->root.type == bfd_link_hash_indirect
10262
			     || rh->root.type == bfd_link_hash_warning)
10263
			rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10264
 
10265
		      /* Setting the index to -2 tells
10266
			 elf_link_output_extsym that this symbol is
10267
			 used by a reloc.  */
10268
		      BFD_ASSERT (rh->indx < 0);
10269
		      rh->indx = -2;
10270
 
10271
		      *rel_hash = rh;
10272
 
10273
		      continue;
10274
		    }
10275
 
10276
		  /* This is a reloc against a local symbol.  */
10277
 
10278
		  *rel_hash = NULL;
10279
		  sym = isymbuf[r_symndx];
10280
		  sec = flinfo->sections[r_symndx];
10281
		  if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10282
		    {
10283
		      /* I suppose the backend ought to fill in the
10284
			 section of any STT_SECTION symbol against a
10285
			 processor specific section.  */
10286
		      r_symndx = STN_UNDEF;
10287
		      if (bfd_is_abs_section (sec))
10288
			;
10289
		      else if (sec == NULL || sec->owner == NULL)
10290
			{
10291
			  bfd_set_error (bfd_error_bad_value);
10292
			  return FALSE;
10293
			}
10294
		      else
10295
			{
10296
			  asection *osec = sec->output_section;
10297
 
10298
			  /* If we have discarded a section, the output
10299
			     section will be the absolute section.  In
10300
			     case of discarded SEC_MERGE sections, use
10301
			     the kept section.  relocate_section should
10302
			     have already handled discarded linkonce
10303
			     sections.  */
10304
			  if (bfd_is_abs_section (osec)
10305
			      && sec->kept_section != NULL
10306
			      && sec->kept_section->output_section != NULL)
10307
			    {
10308
			      osec = sec->kept_section->output_section;
10309
			      irela->r_addend -= osec->vma;
10310
			    }
10311
 
10312
			  if (!bfd_is_abs_section (osec))
10313
			    {
10314
			      r_symndx = osec->target_index;
10315
			      if (r_symndx == STN_UNDEF)
10316
				{
10317
				  irela->r_addend += osec->vma;
10318
				  osec = _bfd_nearby_section (output_bfd, osec,
10319
							      osec->vma);
10320
				  irela->r_addend -= osec->vma;
10321
				  r_symndx = osec->target_index;
10322
				}
10323
			    }
10324
			}
10325
 
10326
		      /* Adjust the addend according to where the
10327
			 section winds up in the output section.  */
10328
		      if (rela_normal)
10329
			irela->r_addend += sec->output_offset;
10330
		    }
10331
		  else
10332
		    {
10333
		      if (flinfo->indices[r_symndx] == -1)
10334
			{
10335
			  unsigned long shlink;
10336
			  const char *name;
10337
			  asection *osec;
10338
			  long indx;
10339
 
10340
			  if (flinfo->info->strip == strip_all)
10341
			    {
10342
			      /* You can't do ld -r -s.  */
10343
			      bfd_set_error (bfd_error_invalid_operation);
10344
			      return FALSE;
10345
			    }
10346
 
10347
			  /* This symbol was skipped earlier, but
10348
			     since it is needed by a reloc, we
10349
			     must output it now.  */
10350
			  shlink = symtab_hdr->sh_link;
10351
			  name = (bfd_elf_string_from_elf_section
10352
				  (input_bfd, shlink, sym.st_name));
10353
			  if (name == NULL)
10354
			    return FALSE;
10355
 
10356
			  osec = sec->output_section;
10357
			  sym.st_shndx =
10358
			    _bfd_elf_section_from_bfd_section (output_bfd,
10359
							       osec);
10360
			  if (sym.st_shndx == SHN_BAD)
10361
			    return FALSE;
10362
 
10363
			  sym.st_value += sec->output_offset;
6324 serge 10364
			  if (!bfd_link_relocatable (flinfo->info))
5197 serge 10365
			    {
10366
			      sym.st_value += osec->vma;
10367
			      if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10368
				{
10369
				  /* STT_TLS symbols are relative to PT_TLS
10370
				     segment base.  */
10371
				  BFD_ASSERT (elf_hash_table (flinfo->info)
10372
					      ->tls_sec != NULL);
10373
				  sym.st_value -= (elf_hash_table (flinfo->info)
10374
						   ->tls_sec->vma);
10375
				}
10376
			    }
10377
 
10378
			  indx = bfd_get_symcount (output_bfd);
6324 serge 10379
			  ret = elf_link_output_symstrtab (flinfo, name,
10380
							   &sym, sec,
10381
							   NULL);
5197 serge 10382
			  if (ret == 0)
10383
			    return FALSE;
10384
			  else if (ret == 1)
10385
			    flinfo->indices[r_symndx] = indx;
10386
			  else
10387
			    abort ();
10388
			}
10389
 
10390
		      r_symndx = flinfo->indices[r_symndx];
10391
		    }
10392
 
10393
		  irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10394
				   | (irela->r_info & r_type_mask));
10395
		}
10396
 
10397
	      /* Swap out the relocs.  */
10398
	      input_rel_hdr = esdi->rel.hdr;
10399
	      if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10400
		{
10401
		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
10402
						     input_rel_hdr,
10403
						     internal_relocs,
10404
						     rel_hash_list))
10405
		    return FALSE;
10406
		  internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10407
				      * bed->s->int_rels_per_ext_rel);
10408
		  rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10409
		}
10410
 
10411
	      input_rela_hdr = esdi->rela.hdr;
10412
	      if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10413
		{
10414
		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
10415
						     input_rela_hdr,
10416
						     internal_relocs,
10417
						     rela_hash_list))
10418
		    return FALSE;
10419
		}
10420
	    }
10421
	}
10422
 
10423
      /* Write out the modified section contents.  */
10424
      if (bed->elf_backend_write_section
10425
	  && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10426
						contents))
10427
	{
10428
	  /* Section written out.  */
10429
	}
10430
      else switch (o->sec_info_type)
10431
	{
10432
	case SEC_INFO_TYPE_STABS:
10433
	  if (! (_bfd_write_section_stabs
10434
		 (output_bfd,
10435
		  &elf_hash_table (flinfo->info)->stab_info,
10436
		  o, &elf_section_data (o)->sec_info, contents)))
10437
	    return FALSE;
10438
	  break;
10439
	case SEC_INFO_TYPE_MERGE:
10440
	  if (! _bfd_write_merged_section (output_bfd, o,
10441
					   elf_section_data (o)->sec_info))
10442
	    return FALSE;
10443
	  break;
10444
	case SEC_INFO_TYPE_EH_FRAME:
10445
	  {
10446
	    if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10447
						   o, contents))
10448
	      return FALSE;
10449
	  }
10450
	  break;
6324 serge 10451
	case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10452
	  {
10453
	    if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10454
							 flinfo->info,
10455
							 o, contents))
10456
	      return FALSE;
10457
	  }
10458
	  break;
5197 serge 10459
	default:
10460
	  {
10461
	    /* FIXME: octets_per_byte.  */
10462
	    if (! (o->flags & SEC_EXCLUDE))
10463
	      {
10464
		file_ptr offset = (file_ptr) o->output_offset;
10465
		bfd_size_type todo = o->size;
10466
		if ((o->flags & SEC_ELF_REVERSE_COPY))
10467
		  {
10468
		    /* Reverse-copy input section to output.  */
10469
		    do
10470
		      {
10471
			todo -= address_size;
10472
			if (! bfd_set_section_contents (output_bfd,
10473
							o->output_section,
10474
							contents + todo,
10475
							offset,
10476
							address_size))
10477
			  return FALSE;
10478
			if (todo == 0)
10479
			  break;
10480
			offset += address_size;
10481
		      }
10482
		    while (1);
10483
		  }
10484
		else if (! bfd_set_section_contents (output_bfd,
10485
						     o->output_section,
10486
						     contents,
10487
						     offset, todo))
10488
		  return FALSE;
10489
	      }
10490
	  }
10491
	  break;
10492
	}
10493
    }
10494
 
10495
  return TRUE;
10496
}
10497
 
10498
/* Generate a reloc when linking an ELF file.  This is a reloc
10499
   requested by the linker, and does not come from any input file.  This
10500
   is used to build constructor and destructor tables when linking
10501
   with -Ur.  */
10502
 
10503
static bfd_boolean
10504
elf_reloc_link_order (bfd *output_bfd,
10505
		      struct bfd_link_info *info,
10506
		      asection *output_section,
10507
		      struct bfd_link_order *link_order)
10508
{
10509
  reloc_howto_type *howto;
10510
  long indx;
10511
  bfd_vma offset;
10512
  bfd_vma addend;
10513
  struct bfd_elf_section_reloc_data *reldata;
10514
  struct elf_link_hash_entry **rel_hash_ptr;
10515
  Elf_Internal_Shdr *rel_hdr;
10516
  const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10517
  Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10518
  bfd_byte *erel;
10519
  unsigned int i;
10520
  struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10521
 
10522
  howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10523
  if (howto == NULL)
10524
    {
10525
      bfd_set_error (bfd_error_bad_value);
10526
      return FALSE;
10527
    }
10528
 
10529
  addend = link_order->u.reloc.p->addend;
10530
 
10531
  if (esdo->rel.hdr)
10532
    reldata = &esdo->rel;
10533
  else if (esdo->rela.hdr)
10534
    reldata = &esdo->rela;
10535
  else
10536
    {
10537
      reldata = NULL;
10538
      BFD_ASSERT (0);
10539
    }
10540
 
10541
  /* Figure out the symbol index.  */
10542
  rel_hash_ptr = reldata->hashes + reldata->count;
10543
  if (link_order->type == bfd_section_reloc_link_order)
10544
    {
10545
      indx = link_order->u.reloc.p->u.section->target_index;
10546
      BFD_ASSERT (indx != 0);
10547
      *rel_hash_ptr = NULL;
10548
    }
10549
  else
10550
    {
10551
      struct elf_link_hash_entry *h;
10552
 
10553
      /* Treat a reloc against a defined symbol as though it were
10554
	 actually against the section.  */
10555
      h = ((struct elf_link_hash_entry *)
10556
	   bfd_wrapped_link_hash_lookup (output_bfd, info,
10557
					 link_order->u.reloc.p->u.name,
10558
					 FALSE, FALSE, TRUE));
10559
      if (h != NULL
10560
	  && (h->root.type == bfd_link_hash_defined
10561
	      || h->root.type == bfd_link_hash_defweak))
10562
	{
10563
	  asection *section;
10564
 
10565
	  section = h->root.u.def.section;
10566
	  indx = section->output_section->target_index;
10567
	  *rel_hash_ptr = NULL;
10568
	  /* It seems that we ought to add the symbol value to the
10569
	     addend here, but in practice it has already been added
10570
	     because it was passed to constructor_callback.  */
10571
	  addend += section->output_section->vma + section->output_offset;
10572
	}
10573
      else if (h != NULL)
10574
	{
10575
	  /* Setting the index to -2 tells elf_link_output_extsym that
10576
	     this symbol is used by a reloc.  */
10577
	  h->indx = -2;
10578
	  *rel_hash_ptr = h;
10579
	  indx = 0;
10580
	}
10581
      else
10582
	{
10583
	  if (! ((*info->callbacks->unattached_reloc)
10584
		 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10585
	    return FALSE;
10586
	  indx = 0;
10587
	}
10588
    }
10589
 
10590
  /* If this is an inplace reloc, we must write the addend into the
10591
     object file.  */
10592
  if (howto->partial_inplace && addend != 0)
10593
    {
10594
      bfd_size_type size;
10595
      bfd_reloc_status_type rstat;
10596
      bfd_byte *buf;
10597
      bfd_boolean ok;
10598
      const char *sym_name;
10599
 
10600
      size = (bfd_size_type) bfd_get_reloc_size (howto);
10601
      buf = (bfd_byte *) bfd_zmalloc (size);
6324 serge 10602
      if (buf == NULL && size != 0)
5197 serge 10603
	return FALSE;
10604
      rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10605
      switch (rstat)
10606
	{
10607
	case bfd_reloc_ok:
10608
	  break;
10609
 
10610
	default:
10611
	case bfd_reloc_outofrange:
10612
	  abort ();
10613
 
10614
	case bfd_reloc_overflow:
10615
	  if (link_order->type == bfd_section_reloc_link_order)
10616
	    sym_name = bfd_section_name (output_bfd,
10617
					 link_order->u.reloc.p->u.section);
10618
	  else
10619
	    sym_name = link_order->u.reloc.p->u.name;
10620
	  if (! ((*info->callbacks->reloc_overflow)
10621
		 (info, NULL, sym_name, howto->name, addend, NULL,
10622
		  NULL, (bfd_vma) 0)))
10623
	    {
10624
	      free (buf);
10625
	      return FALSE;
10626
	    }
10627
	  break;
10628
	}
10629
      ok = bfd_set_section_contents (output_bfd, output_section, buf,
10630
				     link_order->offset, size);
10631
      free (buf);
10632
      if (! ok)
10633
	return FALSE;
10634
    }
10635
 
10636
  /* The address of a reloc is relative to the section in a
10637
     relocatable file, and is a virtual address in an executable
10638
     file.  */
10639
  offset = link_order->offset;
6324 serge 10640
  if (! bfd_link_relocatable (info))
5197 serge 10641
    offset += output_section->vma;
10642
 
10643
  for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10644
    {
10645
      irel[i].r_offset = offset;
10646
      irel[i].r_info = 0;
10647
      irel[i].r_addend = 0;
10648
    }
10649
  if (bed->s->arch_size == 32)
10650
    irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10651
  else
10652
    irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10653
 
10654
  rel_hdr = reldata->hdr;
10655
  erel = rel_hdr->contents;
10656
  if (rel_hdr->sh_type == SHT_REL)
10657
    {
10658
      erel += reldata->count * bed->s->sizeof_rel;
10659
      (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10660
    }
10661
  else
10662
    {
10663
      irel[0].r_addend = addend;
10664
      erel += reldata->count * bed->s->sizeof_rela;
10665
      (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10666
    }
10667
 
10668
  ++reldata->count;
10669
 
10670
  return TRUE;
10671
}
10672
 
10673
 
10674
/* Get the output vma of the section pointed to by the sh_link field.  */
10675
 
10676
static bfd_vma
10677
elf_get_linked_section_vma (struct bfd_link_order *p)
10678
{
10679
  Elf_Internal_Shdr **elf_shdrp;
10680
  asection *s;
10681
  int elfsec;
10682
 
10683
  s = p->u.indirect.section;
10684
  elf_shdrp = elf_elfsections (s->owner);
10685
  elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10686
  elfsec = elf_shdrp[elfsec]->sh_link;
10687
  /* PR 290:
10688
     The Intel C compiler generates SHT_IA_64_UNWIND with
10689
     SHF_LINK_ORDER.  But it doesn't set the sh_link or
10690
     sh_info fields.  Hence we could get the situation
10691
     where elfsec is 0.  */
10692
  if (elfsec == 0)
10693
    {
10694
      const struct elf_backend_data *bed
10695
	= get_elf_backend_data (s->owner);
10696
      if (bed->link_order_error_handler)
10697
	bed->link_order_error_handler
10698
	  (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10699
      return 0;
10700
    }
10701
  else
10702
    {
10703
      s = elf_shdrp[elfsec]->bfd_section;
10704
      return s->output_section->vma + s->output_offset;
10705
    }
10706
}
10707
 
10708
 
10709
/* Compare two sections based on the locations of the sections they are
10710
   linked to.  Used by elf_fixup_link_order.  */
10711
 
10712
static int
10713
compare_link_order (const void * a, const void * b)
10714
{
10715
  bfd_vma apos;
10716
  bfd_vma bpos;
10717
 
10718
  apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10719
  bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10720
  if (apos < bpos)
10721
    return -1;
10722
  return apos > bpos;
10723
}
10724
 
10725
 
10726
/* Looks for sections with SHF_LINK_ORDER set.  Rearranges them into the same
10727
   order as their linked sections.  Returns false if this could not be done
10728
   because an output section includes both ordered and unordered
10729
   sections.  Ideally we'd do this in the linker proper.  */
10730
 
10731
static bfd_boolean
10732
elf_fixup_link_order (bfd *abfd, asection *o)
10733
{
10734
  int seen_linkorder;
10735
  int seen_other;
10736
  int n;
10737
  struct bfd_link_order *p;
10738
  bfd *sub;
10739
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10740
  unsigned elfsec;
10741
  struct bfd_link_order **sections;
10742
  asection *s, *other_sec, *linkorder_sec;
10743
  bfd_vma offset;
10744
 
10745
  other_sec = NULL;
10746
  linkorder_sec = NULL;
10747
  seen_other = 0;
10748
  seen_linkorder = 0;
10749
  for (p = o->map_head.link_order; p != NULL; p = p->next)
10750
    {
10751
      if (p->type == bfd_indirect_link_order)
10752
	{
10753
	  s = p->u.indirect.section;
10754
	  sub = s->owner;
10755
	  if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10756
	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10757
	      && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10758
	      && elfsec < elf_numsections (sub)
10759
	      && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10760
	      && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10761
	    {
10762
	      seen_linkorder++;
10763
	      linkorder_sec = s;
10764
	    }
10765
	  else
10766
	    {
10767
	      seen_other++;
10768
	      other_sec = s;
10769
	    }
10770
	}
10771
      else
10772
	seen_other++;
10773
 
10774
      if (seen_other && seen_linkorder)
10775
	{
10776
	  if (other_sec && linkorder_sec)
10777
	    (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10778
				   o, linkorder_sec,
10779
				   linkorder_sec->owner, other_sec,
10780
				   other_sec->owner);
10781
	  else
10782
	    (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10783
				   o);
10784
	  bfd_set_error (bfd_error_bad_value);
10785
	  return FALSE;
10786
	}
10787
    }
10788
 
10789
  if (!seen_linkorder)
10790
    return TRUE;
10791
 
10792
  sections = (struct bfd_link_order **)
10793
    bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10794
  if (sections == NULL)
10795
    return FALSE;
10796
  seen_linkorder = 0;
10797
 
10798
  for (p = o->map_head.link_order; p != NULL; p = p->next)
10799
    {
10800
      sections[seen_linkorder++] = p;
10801
    }
10802
  /* Sort the input sections in the order of their linked section.  */
10803
  qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10804
	 compare_link_order);
10805
 
10806
  /* Change the offsets of the sections.  */
10807
  offset = 0;
10808
  for (n = 0; n < seen_linkorder; n++)
10809
    {
10810
      s = sections[n]->u.indirect.section;
10811
      offset &= ~(bfd_vma) 0 << s->alignment_power;
10812
      s->output_offset = offset;
10813
      sections[n]->offset = offset;
10814
      /* FIXME: octets_per_byte.  */
10815
      offset += sections[n]->size;
10816
    }
10817
 
10818
  free (sections);
10819
  return TRUE;
10820
}
10821
 
10822
static void
10823
elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
10824
{
10825
  asection *o;
10826
 
10827
  if (flinfo->symstrtab != NULL)
6324 serge 10828
    _bfd_elf_strtab_free (flinfo->symstrtab);
5197 serge 10829
  if (flinfo->contents != NULL)
10830
    free (flinfo->contents);
10831
  if (flinfo->external_relocs != NULL)
10832
    free (flinfo->external_relocs);
10833
  if (flinfo->internal_relocs != NULL)
10834
    free (flinfo->internal_relocs);
10835
  if (flinfo->external_syms != NULL)
10836
    free (flinfo->external_syms);
10837
  if (flinfo->locsym_shndx != NULL)
10838
    free (flinfo->locsym_shndx);
10839
  if (flinfo->internal_syms != NULL)
10840
    free (flinfo->internal_syms);
10841
  if (flinfo->indices != NULL)
10842
    free (flinfo->indices);
10843
  if (flinfo->sections != NULL)
10844
    free (flinfo->sections);
10845
  if (flinfo->symshndxbuf != NULL)
10846
    free (flinfo->symshndxbuf);
10847
  for (o = obfd->sections; o != NULL; o = o->next)
10848
    {
10849
      struct bfd_elf_section_data *esdo = elf_section_data (o);
10850
      if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
10851
	free (esdo->rel.hashes);
10852
      if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
10853
	free (esdo->rela.hashes);
10854
    }
10855
}
10856
 
10857
/* Do the final step of an ELF link.  */
10858
 
10859
bfd_boolean
10860
bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10861
{
10862
  bfd_boolean dynamic;
10863
  bfd_boolean emit_relocs;
10864
  bfd *dynobj;
10865
  struct elf_final_link_info flinfo;
10866
  asection *o;
10867
  struct bfd_link_order *p;
10868
  bfd *sub;
10869
  bfd_size_type max_contents_size;
10870
  bfd_size_type max_external_reloc_size;
10871
  bfd_size_type max_internal_reloc_count;
10872
  bfd_size_type max_sym_count;
10873
  bfd_size_type max_sym_shndx_count;
10874
  Elf_Internal_Sym elfsym;
10875
  unsigned int i;
10876
  Elf_Internal_Shdr *symtab_hdr;
10877
  Elf_Internal_Shdr *symtab_shndx_hdr;
10878
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10879
  struct elf_outext_info eoinfo;
10880
  bfd_boolean merged;
10881
  size_t relativecount = 0;
10882
  asection *reldyn = 0;
10883
  bfd_size_type amt;
10884
  asection *attr_section = NULL;
10885
  bfd_vma attr_size = 0;
10886
  const char *std_attrs_section;
10887
 
10888
  if (! is_elf_hash_table (info->hash))
10889
    return FALSE;
10890
 
6324 serge 10891
  if (bfd_link_pic (info))
5197 serge 10892
    abfd->flags |= DYNAMIC;
10893
 
10894
  dynamic = elf_hash_table (info)->dynamic_sections_created;
10895
  dynobj = elf_hash_table (info)->dynobj;
10896
 
6324 serge 10897
  emit_relocs = (bfd_link_relocatable (info)
5197 serge 10898
		 || info->emitrelocations);
10899
 
10900
  flinfo.info = info;
10901
  flinfo.output_bfd = abfd;
6324 serge 10902
  flinfo.symstrtab = _bfd_elf_strtab_init ();
5197 serge 10903
  if (flinfo.symstrtab == NULL)
10904
    return FALSE;
10905
 
10906
  if (! dynamic)
10907
    {
10908
      flinfo.hash_sec = NULL;
10909
      flinfo.symver_sec = NULL;
10910
    }
10911
  else
10912
    {
10913
      flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
10914
      /* Note that dynsym_sec can be NULL (on VMS).  */
10915
      flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
10916
      /* Note that it is OK if symver_sec is NULL.  */
10917
    }
10918
 
10919
  flinfo.contents = NULL;
10920
  flinfo.external_relocs = NULL;
10921
  flinfo.internal_relocs = NULL;
10922
  flinfo.external_syms = NULL;
10923
  flinfo.locsym_shndx = NULL;
10924
  flinfo.internal_syms = NULL;
10925
  flinfo.indices = NULL;
10926
  flinfo.sections = NULL;
10927
  flinfo.symshndxbuf = NULL;
10928
  flinfo.filesym_count = 0;
10929
 
10930
  /* The object attributes have been merged.  Remove the input
10931
     sections from the link, and set the contents of the output
10932
     secton.  */
10933
  std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10934
  for (o = abfd->sections; o != NULL; o = o->next)
10935
    {
10936
      if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10937
	  || strcmp (o->name, ".gnu.attributes") == 0)
10938
	{
10939
	  for (p = o->map_head.link_order; p != NULL; p = p->next)
10940
	    {
10941
	      asection *input_section;
10942
 
10943
	      if (p->type != bfd_indirect_link_order)
10944
		continue;
10945
	      input_section = p->u.indirect.section;
10946
	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
10947
		 elf_link_input_bfd ignores this section.  */
10948
	      input_section->flags &= ~SEC_HAS_CONTENTS;
10949
	    }
10950
 
10951
	  attr_size = bfd_elf_obj_attr_size (abfd);
10952
	  if (attr_size)
10953
	    {
10954
	      bfd_set_section_size (abfd, o, attr_size);
10955
	      attr_section = o;
10956
	      /* Skip this section later on.  */
10957
	      o->map_head.link_order = NULL;
10958
	    }
10959
	  else
10960
	    o->flags |= SEC_EXCLUDE;
10961
	}
10962
    }
10963
 
10964
  /* Count up the number of relocations we will output for each output
10965
     section, so that we know the sizes of the reloc sections.  We
10966
     also figure out some maximum sizes.  */
10967
  max_contents_size = 0;
10968
  max_external_reloc_size = 0;
10969
  max_internal_reloc_count = 0;
10970
  max_sym_count = 0;
10971
  max_sym_shndx_count = 0;
10972
  merged = FALSE;
10973
  for (o = abfd->sections; o != NULL; o = o->next)
10974
    {
10975
      struct bfd_elf_section_data *esdo = elf_section_data (o);
10976
      o->reloc_count = 0;
10977
 
10978
      for (p = o->map_head.link_order; p != NULL; p = p->next)
10979
	{
10980
	  unsigned int reloc_count = 0;
10981
	  struct bfd_elf_section_data *esdi = NULL;
10982
 
10983
	  if (p->type == bfd_section_reloc_link_order
10984
	      || p->type == bfd_symbol_reloc_link_order)
10985
	    reloc_count = 1;
10986
	  else if (p->type == bfd_indirect_link_order)
10987
	    {
10988
	      asection *sec;
10989
 
10990
	      sec = p->u.indirect.section;
10991
	      esdi = elf_section_data (sec);
10992
 
10993
	      /* Mark all sections which are to be included in the
10994
		 link.  This will normally be every section.  We need
10995
		 to do this so that we can identify any sections which
10996
		 the linker has decided to not include.  */
10997
	      sec->linker_mark = TRUE;
10998
 
10999
	      if (sec->flags & SEC_MERGE)
11000
		merged = TRUE;
11001
 
11002
	      if (esdo->this_hdr.sh_type == SHT_REL
11003
		  || esdo->this_hdr.sh_type == SHT_RELA)
11004
		/* Some backends use reloc_count in relocation sections
11005
		   to count particular types of relocs.  Of course,
11006
		   reloc sections themselves can't have relocations.  */
11007
		reloc_count = 0;
6324 serge 11008
	      else if (emit_relocs)
5197 serge 11009
		reloc_count = sec->reloc_count;
11010
	      else if (bed->elf_backend_count_relocs)
11011
		reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11012
 
11013
	      if (sec->rawsize > max_contents_size)
11014
		max_contents_size = sec->rawsize;
11015
	      if (sec->size > max_contents_size)
11016
		max_contents_size = sec->size;
11017
 
11018
	      /* We are interested in just local symbols, not all
11019
		 symbols.  */
11020
	      if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11021
		  && (sec->owner->flags & DYNAMIC) == 0)
11022
		{
11023
		  size_t sym_count;
11024
 
11025
		  if (elf_bad_symtab (sec->owner))
11026
		    sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11027
				 / bed->s->sizeof_sym);
11028
		  else
11029
		    sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11030
 
11031
		  if (sym_count > max_sym_count)
11032
		    max_sym_count = sym_count;
11033
 
11034
		  if (sym_count > max_sym_shndx_count
6324 serge 11035
		      && elf_symtab_shndx_list (sec->owner) != NULL)
5197 serge 11036
		    max_sym_shndx_count = sym_count;
11037
 
11038
		  if ((sec->flags & SEC_RELOC) != 0)
11039
		    {
11040
		      size_t ext_size = 0;
11041
 
11042
		      if (esdi->rel.hdr != NULL)
11043
			ext_size = esdi->rel.hdr->sh_size;
11044
		      if (esdi->rela.hdr != NULL)
11045
			ext_size += esdi->rela.hdr->sh_size;
11046
 
11047
		      if (ext_size > max_external_reloc_size)
11048
			max_external_reloc_size = ext_size;
11049
		      if (sec->reloc_count > max_internal_reloc_count)
11050
			max_internal_reloc_count = sec->reloc_count;
11051
		    }
11052
		}
11053
	    }
11054
 
11055
	  if (reloc_count == 0)
11056
	    continue;
11057
 
11058
	  o->reloc_count += reloc_count;
11059
 
6324 serge 11060
	  if (p->type == bfd_indirect_link_order && emit_relocs)
5197 serge 11061
	    {
11062
	      if (esdi->rel.hdr)
11063
		esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11064
	      if (esdi->rela.hdr)
11065
		esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11066
	    }
11067
	  else
11068
	    {
11069
	      if (o->use_rela_p)
11070
		esdo->rela.count += reloc_count;
11071
	      else
11072
		esdo->rel.count += reloc_count;
11073
	    }
11074
	}
11075
 
11076
      if (o->reloc_count > 0)
11077
	o->flags |= SEC_RELOC;
11078
      else
11079
	{
11080
	  /* Explicitly clear the SEC_RELOC flag.  The linker tends to
11081
	     set it (this is probably a bug) and if it is set
11082
	     assign_section_numbers will create a reloc section.  */
11083
	  o->flags &=~ SEC_RELOC;
11084
	}
11085
 
11086
      /* If the SEC_ALLOC flag is not set, force the section VMA to
11087
	 zero.  This is done in elf_fake_sections as well, but forcing
11088
	 the VMA to 0 here will ensure that relocs against these
11089
	 sections are handled correctly.  */
11090
      if ((o->flags & SEC_ALLOC) == 0
11091
	  && ! o->user_set_vma)
11092
	o->vma = 0;
11093
    }
11094
 
6324 serge 11095
  if (! bfd_link_relocatable (info) && merged)
5197 serge 11096
    elf_link_hash_traverse (elf_hash_table (info),
11097
			    _bfd_elf_link_sec_merge_syms, abfd);
11098
 
11099
  /* Figure out the file positions for everything but the symbol table
11100
     and the relocs.  We set symcount to force assign_section_numbers
11101
     to create a symbol table.  */
6324 serge 11102
  bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
5197 serge 11103
  BFD_ASSERT (! abfd->output_has_begun);
11104
  if (! _bfd_elf_compute_section_file_positions (abfd, info))
11105
    goto error_return;
11106
 
11107
  /* Set sizes, and assign file positions for reloc sections.  */
11108
  for (o = abfd->sections; o != NULL; o = o->next)
11109
    {
11110
      struct bfd_elf_section_data *esdo = elf_section_data (o);
11111
      if ((o->flags & SEC_RELOC) != 0)
11112
	{
11113
	  if (esdo->rel.hdr
11114
	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11115
	    goto error_return;
11116
 
11117
	  if (esdo->rela.hdr
11118
	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11119
	    goto error_return;
11120
	}
11121
 
11122
      /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11123
	 to count upwards while actually outputting the relocations.  */
11124
      esdo->rel.count = 0;
11125
      esdo->rela.count = 0;
6324 serge 11126
 
11127
      if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11128
	{
11129
	  /* Cache the section contents so that they can be compressed
11130
	     later.  Use bfd_malloc since it will be freed by
11131
	     bfd_compress_section_contents.  */
11132
	  unsigned char *contents = esdo->this_hdr.contents;
11133
	  if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11134
	    abort ();
11135
	  contents
11136
	    = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11137
	  if (contents == NULL)
11138
	    goto error_return;
11139
	  esdo->this_hdr.contents = contents;
11140
	}
5197 serge 11141
    }
11142
 
11143
  /* We have now assigned file positions for all the sections except
6324 serge 11144
     .symtab, .strtab, and non-loaded reloc sections.  We start the
11145
     .symtab section at the current file position, and write directly
11146
     to it.  We build the .strtab section in memory.  */
5197 serge 11147
  bfd_get_symcount (abfd) = 0;
11148
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11149
  /* sh_name is set in prep_headers.  */
11150
  symtab_hdr->sh_type = SHT_SYMTAB;
11151
  /* sh_flags, sh_addr and sh_size all start off zero.  */
11152
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11153
  /* sh_link is set in assign_section_numbers.  */
11154
  /* sh_info is set below.  */
11155
  /* sh_offset is set just below.  */
11156
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11157
 
6324 serge 11158
  if (max_sym_count < 20)
11159
    max_sym_count = 20;
11160
  elf_hash_table (info)->strtabsize = max_sym_count;
11161
  amt = max_sym_count * sizeof (struct elf_sym_strtab);
11162
  elf_hash_table (info)->strtab
11163
    = (struct elf_sym_strtab *) bfd_malloc (amt);
11164
  if (elf_hash_table (info)->strtab == NULL)
11165
    goto error_return;
11166
  /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
11167
  flinfo.symshndxbuf
11168
    = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11169
       ? (Elf_External_Sym_Shndx *) -1 : NULL);
5197 serge 11170
 
6324 serge 11171
  if (info->strip != strip_all || emit_relocs)
5197 serge 11172
    {
6324 serge 11173
      file_ptr off = elf_next_file_pos (abfd);
5197 serge 11174
 
6324 serge 11175
      _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11176
 
11177
      /* Note that at this point elf_next_file_pos (abfd) is
11178
	 incorrect.  We do not yet know the size of the .symtab section.
11179
	 We correct next_file_pos below, after we do know the size.  */
11180
 
11181
      /* Start writing out the symbol table.  The first symbol is always a
11182
	 dummy symbol.  */
5197 serge 11183
      elfsym.st_value = 0;
11184
      elfsym.st_size = 0;
11185
      elfsym.st_info = 0;
11186
      elfsym.st_other = 0;
11187
      elfsym.st_shndx = SHN_UNDEF;
11188
      elfsym.st_target_internal = 0;
6324 serge 11189
      if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11190
				     bfd_und_section_ptr, NULL) != 1)
5197 serge 11191
	goto error_return;
11192
 
6324 serge 11193
      /* Output a symbol for each section.  We output these even if we are
11194
	 discarding local symbols, since they are used for relocs.  These
11195
	 symbols have no names.  We store the index of each one in the
11196
	 index field of the section, so that we can find it again when
11197
	 outputting relocs.  */
11198
 
5197 serge 11199
      elfsym.st_size = 0;
11200
      elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11201
      elfsym.st_other = 0;
11202
      elfsym.st_value = 0;
11203
      elfsym.st_target_internal = 0;
11204
      for (i = 1; i < elf_numsections (abfd); i++)
11205
	{
11206
	  o = bfd_section_from_elf_index (abfd, i);
11207
	  if (o != NULL)
11208
	    {
11209
	      o->target_index = bfd_get_symcount (abfd);
11210
	      elfsym.st_shndx = i;
6324 serge 11211
	      if (!bfd_link_relocatable (info))
5197 serge 11212
		elfsym.st_value = o->vma;
6324 serge 11213
	      if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11214
					     NULL) != 1)
5197 serge 11215
		goto error_return;
11216
	    }
11217
	}
11218
    }
11219
 
11220
  /* Allocate some memory to hold information read in from the input
11221
     files.  */
11222
  if (max_contents_size != 0)
11223
    {
11224
      flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11225
      if (flinfo.contents == NULL)
11226
	goto error_return;
11227
    }
11228
 
11229
  if (max_external_reloc_size != 0)
11230
    {
11231
      flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11232
      if (flinfo.external_relocs == NULL)
11233
	goto error_return;
11234
    }
11235
 
11236
  if (max_internal_reloc_count != 0)
11237
    {
11238
      amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11239
      amt *= sizeof (Elf_Internal_Rela);
11240
      flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11241
      if (flinfo.internal_relocs == NULL)
11242
	goto error_return;
11243
    }
11244
 
11245
  if (max_sym_count != 0)
11246
    {
11247
      amt = max_sym_count * bed->s->sizeof_sym;
11248
      flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11249
      if (flinfo.external_syms == NULL)
11250
	goto error_return;
11251
 
11252
      amt = max_sym_count * sizeof (Elf_Internal_Sym);
11253
      flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11254
      if (flinfo.internal_syms == NULL)
11255
	goto error_return;
11256
 
11257
      amt = max_sym_count * sizeof (long);
11258
      flinfo.indices = (long int *) bfd_malloc (amt);
11259
      if (flinfo.indices == NULL)
11260
	goto error_return;
11261
 
11262
      amt = max_sym_count * sizeof (asection *);
11263
      flinfo.sections = (asection **) bfd_malloc (amt);
11264
      if (flinfo.sections == NULL)
11265
	goto error_return;
11266
    }
11267
 
11268
  if (max_sym_shndx_count != 0)
11269
    {
11270
      amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11271
      flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11272
      if (flinfo.locsym_shndx == NULL)
11273
	goto error_return;
11274
    }
11275
 
11276
  if (elf_hash_table (info)->tls_sec)
11277
    {
11278
      bfd_vma base, end = 0;
11279
      asection *sec;
11280
 
11281
      for (sec = elf_hash_table (info)->tls_sec;
11282
	   sec && (sec->flags & SEC_THREAD_LOCAL);
11283
	   sec = sec->next)
11284
	{
11285
	  bfd_size_type size = sec->size;
11286
 
11287
	  if (size == 0
11288
	      && (sec->flags & SEC_HAS_CONTENTS) == 0)
11289
	    {
11290
	      struct bfd_link_order *ord = sec->map_tail.link_order;
11291
 
11292
	      if (ord != NULL)
11293
		size = ord->offset + ord->size;
11294
	    }
11295
	  end = sec->vma + size;
11296
	}
11297
      base = elf_hash_table (info)->tls_sec->vma;
11298
      /* Only align end of TLS section if static TLS doesn't have special
11299
	 alignment requirements.  */
11300
      if (bed->static_tls_alignment == 1)
11301
	end = align_power (end,
11302
			   elf_hash_table (info)->tls_sec->alignment_power);
11303
      elf_hash_table (info)->tls_size = end - base;
11304
    }
11305
 
11306
  /* Reorder SHF_LINK_ORDER sections.  */
11307
  for (o = abfd->sections; o != NULL; o = o->next)
11308
    {
11309
      if (!elf_fixup_link_order (abfd, o))
11310
	return FALSE;
11311
    }
11312
 
6324 serge 11313
  if (!_bfd_elf_fixup_eh_frame_hdr (info))
11314
    return FALSE;
11315
 
5197 serge 11316
  /* Since ELF permits relocations to be against local symbols, we
11317
     must have the local symbols available when we do the relocations.
11318
     Since we would rather only read the local symbols once, and we
11319
     would rather not keep them in memory, we handle all the
11320
     relocations for a single input file at the same time.
11321
 
11322
     Unfortunately, there is no way to know the total number of local
11323
     symbols until we have seen all of them, and the local symbol
11324
     indices precede the global symbol indices.  This means that when
11325
     we are generating relocatable output, and we see a reloc against
11326
     a global symbol, we can not know the symbol index until we have
11327
     finished examining all the local symbols to see which ones we are
11328
     going to output.  To deal with this, we keep the relocations in
11329
     memory, and don't output them until the end of the link.  This is
11330
     an unfortunate waste of memory, but I don't see a good way around
11331
     it.  Fortunately, it only happens when performing a relocatable
11332
     link, which is not the common case.  FIXME: If keep_memory is set
11333
     we could write the relocs out and then read them again; I don't
11334
     know how bad the memory loss will be.  */
11335
 
6324 serge 11336
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
5197 serge 11337
    sub->output_has_begun = FALSE;
11338
  for (o = abfd->sections; o != NULL; o = o->next)
11339
    {
11340
      for (p = o->map_head.link_order; p != NULL; p = p->next)
11341
	{
11342
	  if (p->type == bfd_indirect_link_order
11343
	      && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11344
		  == bfd_target_elf_flavour)
11345
	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11346
	    {
11347
	      if (! sub->output_has_begun)
11348
		{
11349
		  if (! elf_link_input_bfd (&flinfo, sub))
11350
		    goto error_return;
11351
		  sub->output_has_begun = TRUE;
11352
		}
11353
	    }
11354
	  else if (p->type == bfd_section_reloc_link_order
11355
		   || p->type == bfd_symbol_reloc_link_order)
11356
	    {
11357
	      if (! elf_reloc_link_order (abfd, info, o, p))
11358
		goto error_return;
11359
	    }
11360
	  else
11361
	    {
11362
	      if (! _bfd_default_link_order (abfd, info, o, p))
11363
		{
11364
		  if (p->type == bfd_indirect_link_order
11365
		      && (bfd_get_flavour (sub)
11366
			  == bfd_target_elf_flavour)
11367
		      && (elf_elfheader (sub)->e_ident[EI_CLASS]
11368
			  != bed->s->elfclass))
11369
		    {
11370
		      const char *iclass, *oclass;
11371
 
11372
		      if (bed->s->elfclass == ELFCLASS64)
11373
			{
11374
			  iclass = "ELFCLASS32";
11375
			  oclass = "ELFCLASS64";
11376
			}
11377
		      else
11378
			{
11379
			  iclass = "ELFCLASS64";
11380
			  oclass = "ELFCLASS32";
11381
			}
11382
 
11383
		      bfd_set_error (bfd_error_wrong_format);
11384
		      (*_bfd_error_handler)
11385
			(_("%B: file class %s incompatible with %s"),
11386
			 sub, iclass, oclass);
11387
		    }
11388
 
11389
		  goto error_return;
11390
		}
11391
	    }
11392
	}
11393
    }
11394
 
11395
  /* Free symbol buffer if needed.  */
11396
  if (!info->reduce_memory_overheads)
11397
    {
6324 serge 11398
      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
5197 serge 11399
	if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11400
	    && elf_tdata (sub)->symbuf)
11401
	  {
11402
	    free (elf_tdata (sub)->symbuf);
11403
	    elf_tdata (sub)->symbuf = NULL;
11404
	  }
11405
    }
11406
 
11407
  /* Output any global symbols that got converted to local in a
11408
     version script or due to symbol visibility.  We do this in a
11409
     separate step since ELF requires all local symbols to appear
11410
     prior to any global symbols.  FIXME: We should only do this if
11411
     some global symbols were, in fact, converted to become local.
11412
     FIXME: Will this work correctly with the Irix 5 linker?  */
11413
  eoinfo.failed = FALSE;
11414
  eoinfo.flinfo = &flinfo;
11415
  eoinfo.localsyms = TRUE;
6324 serge 11416
  eoinfo.file_sym_done = FALSE;
5197 serge 11417
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11418
  if (eoinfo.failed)
11419
    return FALSE;
11420
 
11421
  /* If backend needs to output some local symbols not present in the hash
11422
     table, do it now.  */
6324 serge 11423
  if (bed->elf_backend_output_arch_local_syms
11424
      && (info->strip != strip_all || emit_relocs))
5197 serge 11425
    {
11426
      typedef int (*out_sym_func)
11427
	(void *, const char *, Elf_Internal_Sym *, asection *,
11428
	 struct elf_link_hash_entry *);
11429
 
11430
      if (! ((*bed->elf_backend_output_arch_local_syms)
6324 serge 11431
	     (abfd, info, &flinfo,
11432
	      (out_sym_func) elf_link_output_symstrtab)))
5197 serge 11433
	return FALSE;
11434
    }
11435
 
11436
  /* That wrote out all the local symbols.  Finish up the symbol table
11437
     with the global symbols. Even if we want to strip everything we
11438
     can, we still need to deal with those global symbols that got
11439
     converted to local in a version script.  */
11440
 
11441
  /* The sh_info field records the index of the first non local symbol.  */
11442
  symtab_hdr->sh_info = bfd_get_symcount (abfd);
11443
 
11444
  if (dynamic
6324 serge 11445
      && elf_hash_table (info)->dynsym != NULL
11446
      && (elf_hash_table (info)->dynsym->output_section
11447
	  != bfd_abs_section_ptr))
5197 serge 11448
    {
11449
      Elf_Internal_Sym sym;
6324 serge 11450
      bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
5197 serge 11451
      long last_local = 0;
11452
 
11453
      /* Write out the section symbols for the output sections.  */
6324 serge 11454
      if (bfd_link_pic (info)
11455
	  || elf_hash_table (info)->is_relocatable_executable)
5197 serge 11456
	{
11457
	  asection *s;
11458
 
11459
	  sym.st_size = 0;
11460
	  sym.st_name = 0;
11461
	  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11462
	  sym.st_other = 0;
11463
	  sym.st_target_internal = 0;
11464
 
11465
	  for (s = abfd->sections; s != NULL; s = s->next)
11466
	    {
11467
	      int indx;
11468
	      bfd_byte *dest;
11469
	      long dynindx;
11470
 
11471
	      dynindx = elf_section_data (s)->dynindx;
11472
	      if (dynindx <= 0)
11473
		continue;
11474
	      indx = elf_section_data (s)->this_idx;
11475
	      BFD_ASSERT (indx > 0);
11476
	      sym.st_shndx = indx;
11477
	      if (! check_dynsym (abfd, &sym))
11478
		return FALSE;
11479
	      sym.st_value = s->vma;
11480
	      dest = dynsym + dynindx * bed->s->sizeof_sym;
11481
	      if (last_local < dynindx)
11482
		last_local = dynindx;
11483
	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11484
	    }
11485
	}
11486
 
11487
      /* Write out the local dynsyms.  */
11488
      if (elf_hash_table (info)->dynlocal)
11489
	{
11490
	  struct elf_link_local_dynamic_entry *e;
11491
	  for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11492
	    {
11493
	      asection *s;
11494
	      bfd_byte *dest;
11495
 
11496
	      /* Copy the internal symbol and turn off visibility.
11497
		 Note that we saved a word of storage and overwrote
11498
		 the original st_name with the dynstr_index.  */
11499
	      sym = e->isym;
11500
	      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11501
 
11502
	      s = bfd_section_from_elf_index (e->input_bfd,
11503
					      e->isym.st_shndx);
11504
	      if (s != NULL)
11505
		{
11506
		  sym.st_shndx =
11507
		    elf_section_data (s->output_section)->this_idx;
11508
		  if (! check_dynsym (abfd, &sym))
11509
		    return FALSE;
11510
		  sym.st_value = (s->output_section->vma
11511
				  + s->output_offset
11512
				  + e->isym.st_value);
11513
		}
11514
 
11515
	      if (last_local < e->dynindx)
11516
		last_local = e->dynindx;
11517
 
11518
	      dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11519
	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11520
	    }
11521
	}
11522
 
6324 serge 11523
      elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
5197 serge 11524
	last_local + 1;
11525
    }
11526
 
11527
  /* We get the global symbols from the hash table.  */
11528
  eoinfo.failed = FALSE;
11529
  eoinfo.localsyms = FALSE;
11530
  eoinfo.flinfo = &flinfo;
11531
  bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11532
  if (eoinfo.failed)
11533
    return FALSE;
11534
 
11535
  /* If backend needs to output some symbols not present in the hash
11536
     table, do it now.  */
6324 serge 11537
  if (bed->elf_backend_output_arch_syms
11538
      && (info->strip != strip_all || emit_relocs))
5197 serge 11539
    {
11540
      typedef int (*out_sym_func)
11541
	(void *, const char *, Elf_Internal_Sym *, asection *,
11542
	 struct elf_link_hash_entry *);
11543
 
11544
      if (! ((*bed->elf_backend_output_arch_syms)
6324 serge 11545
	     (abfd, info, &flinfo,
11546
	      (out_sym_func) elf_link_output_symstrtab)))
5197 serge 11547
	return FALSE;
11548
    }
11549
 
6324 serge 11550
  /* Finalize the .strtab section.  */
11551
  _bfd_elf_strtab_finalize (flinfo.symstrtab);
11552
 
11553
  /* Swap out the .strtab section. */
11554
  if (!elf_link_swap_symbols_out (&flinfo))
5197 serge 11555
    return FALSE;
11556
 
11557
  /* Now we know the size of the symtab section.  */
6324 serge 11558
  if (bfd_get_symcount (abfd) > 0)
5197 serge 11559
    {
6324 serge 11560
      /* Finish up and write out the symbol string table (.strtab)
11561
	 section.  */
11562
      Elf_Internal_Shdr *symstrtab_hdr;
11563
      file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
5197 serge 11564
 
6324 serge 11565
      symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11566
      if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
11567
	{
11568
	  symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11569
	  symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11570
	  symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11571
	  amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11572
	  symtab_shndx_hdr->sh_size = amt;
5197 serge 11573
 
6324 serge 11574
	  off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11575
							   off, TRUE);
5197 serge 11576
 
6324 serge 11577
	  if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11578
	      || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11579
	    return FALSE;
11580
	}
5197 serge 11581
 
6324 serge 11582
      symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11583
      /* sh_name was set in prep_headers.  */
11584
      symstrtab_hdr->sh_type = SHT_STRTAB;
11585
      symstrtab_hdr->sh_flags = 0;
11586
      symstrtab_hdr->sh_addr = 0;
11587
      symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11588
      symstrtab_hdr->sh_entsize = 0;
11589
      symstrtab_hdr->sh_link = 0;
11590
      symstrtab_hdr->sh_info = 0;
11591
      /* sh_offset is set just below.  */
11592
      symstrtab_hdr->sh_addralign = 1;
5197 serge 11593
 
6324 serge 11594
      off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11595
						       off, TRUE);
11596
      elf_next_file_pos (abfd) = off;
5197 serge 11597
 
11598
      if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
6324 serge 11599
	  || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
5197 serge 11600
	return FALSE;
11601
    }
11602
 
11603
  /* Adjust the relocs to have the correct symbol indices.  */
11604
  for (o = abfd->sections; o != NULL; o = o->next)
11605
    {
11606
      struct bfd_elf_section_data *esdo = elf_section_data (o);
6324 serge 11607
      bfd_boolean sort;
5197 serge 11608
      if ((o->flags & SEC_RELOC) == 0)
11609
	continue;
11610
 
6324 serge 11611
      sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11612
      if (esdo->rel.hdr != NULL
11613
	  && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11614
	return FALSE;
11615
      if (esdo->rela.hdr != NULL
11616
	  && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11617
	return FALSE;
5197 serge 11618
 
11619
      /* Set the reloc_count field to 0 to prevent write_relocs from
11620
	 trying to swap the relocs out itself.  */
11621
      o->reloc_count = 0;
11622
    }
11623
 
11624
  if (dynamic && info->combreloc && dynobj != NULL)
11625
    relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11626
 
11627
  /* If we are linking against a dynamic object, or generating a
11628
     shared library, finish up the dynamic linking information.  */
11629
  if (dynamic)
11630
    {
11631
      bfd_byte *dyncon, *dynconend;
11632
 
11633
      /* Fix up .dynamic entries.  */
11634
      o = bfd_get_linker_section (dynobj, ".dynamic");
11635
      BFD_ASSERT (o != NULL);
11636
 
11637
      dyncon = o->contents;
11638
      dynconend = o->contents + o->size;
11639
      for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11640
	{
11641
	  Elf_Internal_Dyn dyn;
11642
	  const char *name;
11643
	  unsigned int type;
11644
 
11645
	  bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11646
 
11647
	  switch (dyn.d_tag)
11648
	    {
11649
	    default:
11650
	      continue;
11651
	    case DT_NULL:
11652
	      if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11653
		{
11654
		  switch (elf_section_data (reldyn)->this_hdr.sh_type)
11655
		    {
11656
		    case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11657
		    case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11658
		    default: continue;
11659
		    }
11660
		  dyn.d_un.d_val = relativecount;
11661
		  relativecount = 0;
11662
		  break;
11663
		}
11664
	      continue;
11665
 
11666
	    case DT_INIT:
11667
	      name = info->init_function;
11668
	      goto get_sym;
11669
	    case DT_FINI:
11670
	      name = info->fini_function;
11671
	    get_sym:
11672
	      {
11673
		struct elf_link_hash_entry *h;
11674
 
11675
		h = elf_link_hash_lookup (elf_hash_table (info), name,
11676
					  FALSE, FALSE, TRUE);
11677
		if (h != NULL
11678
		    && (h->root.type == bfd_link_hash_defined
11679
			|| h->root.type == bfd_link_hash_defweak))
11680
		  {
11681
		    dyn.d_un.d_ptr = h->root.u.def.value;
11682
		    o = h->root.u.def.section;
11683
		    if (o->output_section != NULL)
11684
		      dyn.d_un.d_ptr += (o->output_section->vma
11685
					 + o->output_offset);
11686
		    else
11687
		      {
11688
			/* The symbol is imported from another shared
11689
			   library and does not apply to this one.  */
11690
			dyn.d_un.d_ptr = 0;
11691
		      }
11692
		    break;
11693
		  }
11694
	      }
11695
	      continue;
11696
 
11697
	    case DT_PREINIT_ARRAYSZ:
11698
	      name = ".preinit_array";
11699
	      goto get_size;
11700
	    case DT_INIT_ARRAYSZ:
11701
	      name = ".init_array";
11702
	      goto get_size;
11703
	    case DT_FINI_ARRAYSZ:
11704
	      name = ".fini_array";
11705
	    get_size:
11706
	      o = bfd_get_section_by_name (abfd, name);
11707
	      if (o == NULL)
11708
		{
11709
		  (*_bfd_error_handler)
11710
		    (_("%B: could not find output section %s"), abfd, name);
11711
		  goto error_return;
11712
		}
11713
	      if (o->size == 0)
11714
		(*_bfd_error_handler)
11715
		  (_("warning: %s section has zero size"), name);
11716
	      dyn.d_un.d_val = o->size;
11717
	      break;
11718
 
11719
	    case DT_PREINIT_ARRAY:
11720
	      name = ".preinit_array";
11721
	      goto get_vma;
11722
	    case DT_INIT_ARRAY:
11723
	      name = ".init_array";
11724
	      goto get_vma;
11725
	    case DT_FINI_ARRAY:
11726
	      name = ".fini_array";
11727
	      goto get_vma;
11728
 
11729
	    case DT_HASH:
11730
	      name = ".hash";
11731
	      goto get_vma;
11732
	    case DT_GNU_HASH:
11733
	      name = ".gnu.hash";
11734
	      goto get_vma;
11735
	    case DT_STRTAB:
11736
	      name = ".dynstr";
11737
	      goto get_vma;
11738
	    case DT_SYMTAB:
11739
	      name = ".dynsym";
11740
	      goto get_vma;
11741
	    case DT_VERDEF:
11742
	      name = ".gnu.version_d";
11743
	      goto get_vma;
11744
	    case DT_VERNEED:
11745
	      name = ".gnu.version_r";
11746
	      goto get_vma;
11747
	    case DT_VERSYM:
11748
	      name = ".gnu.version";
11749
	    get_vma:
11750
	      o = bfd_get_section_by_name (abfd, name);
11751
	      if (o == NULL)
11752
		{
11753
		  (*_bfd_error_handler)
11754
		    (_("%B: could not find output section %s"), abfd, name);
11755
		  goto error_return;
11756
		}
11757
	      if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11758
		{
11759
		  (*_bfd_error_handler)
11760
		    (_("warning: section '%s' is being made into a note"), name);
11761
		  bfd_set_error (bfd_error_nonrepresentable_section);
11762
		  goto error_return;
11763
		}
11764
	      dyn.d_un.d_ptr = o->vma;
11765
	      break;
11766
 
11767
	    case DT_REL:
11768
	    case DT_RELA:
11769
	    case DT_RELSZ:
11770
	    case DT_RELASZ:
11771
	      if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11772
		type = SHT_REL;
11773
	      else
11774
		type = SHT_RELA;
11775
	      dyn.d_un.d_val = 0;
11776
	      dyn.d_un.d_ptr = 0;
11777
	      for (i = 1; i < elf_numsections (abfd); i++)
11778
		{
11779
		  Elf_Internal_Shdr *hdr;
11780
 
11781
		  hdr = elf_elfsections (abfd)[i];
11782
		  if (hdr->sh_type == type
11783
		      && (hdr->sh_flags & SHF_ALLOC) != 0)
11784
		    {
11785
		      if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11786
			dyn.d_un.d_val += hdr->sh_size;
11787
		      else
11788
			{
11789
			  if (dyn.d_un.d_ptr == 0
11790
			      || hdr->sh_addr < dyn.d_un.d_ptr)
11791
			    dyn.d_un.d_ptr = hdr->sh_addr;
11792
			}
11793
		    }
11794
		}
11795
	      break;
11796
	    }
11797
	  bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11798
	}
11799
    }
11800
 
11801
  /* If we have created any dynamic sections, then output them.  */
11802
  if (dynobj != NULL)
11803
    {
11804
      if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11805
	goto error_return;
11806
 
11807
      /* Check for DT_TEXTREL (late, in case the backend removes it).  */
6324 serge 11808
      if (((info->warn_shared_textrel && bfd_link_pic (info))
5197 serge 11809
	   || info->error_textrel)
11810
	  && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
11811
	{
11812
	  bfd_byte *dyncon, *dynconend;
11813
 
11814
	  dyncon = o->contents;
11815
	  dynconend = o->contents + o->size;
11816
	  for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11817
	    {
11818
	      Elf_Internal_Dyn dyn;
11819
 
11820
	      bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11821
 
11822
	      if (dyn.d_tag == DT_TEXTREL)
11823
		{
11824
		  if (info->error_textrel)
11825
		    info->callbacks->einfo
11826
		      (_("%P%X: read-only segment has dynamic relocations.\n"));
11827
		  else
11828
		    info->callbacks->einfo
11829
		      (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
11830
		  break;
11831
		}
11832
	    }
11833
	}
11834
 
11835
      for (o = dynobj->sections; o != NULL; o = o->next)
11836
	{
11837
	  if ((o->flags & SEC_HAS_CONTENTS) == 0
11838
	      || o->size == 0
11839
	      || o->output_section == bfd_abs_section_ptr)
11840
	    continue;
11841
	  if ((o->flags & SEC_LINKER_CREATED) == 0)
11842
	    {
11843
	      /* At this point, we are only interested in sections
11844
		 created by _bfd_elf_link_create_dynamic_sections.  */
11845
	      continue;
11846
	    }
11847
	  if (elf_hash_table (info)->stab_info.stabstr == o)
11848
	    continue;
11849
	  if (elf_hash_table (info)->eh_info.hdr_sec == o)
11850
	    continue;
11851
	  if (strcmp (o->name, ".dynstr") != 0)
11852
	    {
11853
	      /* FIXME: octets_per_byte.  */
11854
	      if (! bfd_set_section_contents (abfd, o->output_section,
11855
					      o->contents,
11856
					      (file_ptr) o->output_offset,
11857
					      o->size))
11858
		goto error_return;
11859
	    }
11860
	  else
11861
	    {
11862
	      /* The contents of the .dynstr section are actually in a
11863
		 stringtab.  */
6324 serge 11864
	      file_ptr off;
11865
 
5197 serge 11866
	      off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11867
	      if (bfd_seek (abfd, off, SEEK_SET) != 0
11868
		  || ! _bfd_elf_strtab_emit (abfd,
11869
					     elf_hash_table (info)->dynstr))
11870
		goto error_return;
11871
	    }
11872
	}
11873
    }
11874
 
6324 serge 11875
  if (bfd_link_relocatable (info))
5197 serge 11876
    {
11877
      bfd_boolean failed = FALSE;
11878
 
11879
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11880
      if (failed)
11881
	goto error_return;
11882
    }
11883
 
11884
  /* If we have optimized stabs strings, output them.  */
11885
  if (elf_hash_table (info)->stab_info.stabstr != NULL)
11886
    {
11887
      if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11888
	goto error_return;
11889
    }
11890
 
11891
  if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11892
    goto error_return;
11893
 
11894
  elf_final_link_free (abfd, &flinfo);
11895
 
11896
  elf_linker (abfd) = TRUE;
11897
 
11898
  if (attr_section)
11899
    {
11900
      bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
11901
      if (contents == NULL)
11902
	return FALSE;	/* Bail out and fail.  */
11903
      bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11904
      bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11905
      free (contents);
11906
    }
11907
 
11908
  return TRUE;
11909
 
11910
 error_return:
11911
  elf_final_link_free (abfd, &flinfo);
11912
  return FALSE;
11913
}
11914
 
11915
/* Initialize COOKIE for input bfd ABFD.  */
11916
 
11917
static bfd_boolean
11918
init_reloc_cookie (struct elf_reloc_cookie *cookie,
11919
		   struct bfd_link_info *info, bfd *abfd)
11920
{
11921
  Elf_Internal_Shdr *symtab_hdr;
11922
  const struct elf_backend_data *bed;
11923
 
11924
  bed = get_elf_backend_data (abfd);
11925
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11926
 
11927
  cookie->abfd = abfd;
11928
  cookie->sym_hashes = elf_sym_hashes (abfd);
11929
  cookie->bad_symtab = elf_bad_symtab (abfd);
11930
  if (cookie->bad_symtab)
11931
    {
11932
      cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11933
      cookie->extsymoff = 0;
11934
    }
11935
  else
11936
    {
11937
      cookie->locsymcount = symtab_hdr->sh_info;
11938
      cookie->extsymoff = symtab_hdr->sh_info;
11939
    }
11940
 
11941
  if (bed->s->arch_size == 32)
11942
    cookie->r_sym_shift = 8;
11943
  else
11944
    cookie->r_sym_shift = 32;
11945
 
11946
  cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11947
  if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11948
    {
11949
      cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11950
					      cookie->locsymcount, 0,
11951
					      NULL, NULL, NULL);
11952
      if (cookie->locsyms == NULL)
11953
	{
11954
	  info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11955
	  return FALSE;
11956
	}
11957
      if (info->keep_memory)
11958
	symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11959
    }
11960
  return TRUE;
11961
}
11962
 
11963
/* Free the memory allocated by init_reloc_cookie, if appropriate.  */
11964
 
11965
static void
11966
fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11967
{
11968
  Elf_Internal_Shdr *symtab_hdr;
11969
 
11970
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11971
  if (cookie->locsyms != NULL
11972
      && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11973
    free (cookie->locsyms);
11974
}
11975
 
11976
/* Initialize the relocation information in COOKIE for input section SEC
11977
   of input bfd ABFD.  */
11978
 
11979
static bfd_boolean
11980
init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11981
			struct bfd_link_info *info, bfd *abfd,
11982
			asection *sec)
11983
{
11984
  const struct elf_backend_data *bed;
11985
 
11986
  if (sec->reloc_count == 0)
11987
    {
11988
      cookie->rels = NULL;
11989
      cookie->relend = NULL;
11990
    }
11991
  else
11992
    {
11993
      bed = get_elf_backend_data (abfd);
11994
 
11995
      cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11996
						info->keep_memory);
11997
      if (cookie->rels == NULL)
11998
	return FALSE;
11999
      cookie->rel = cookie->rels;
12000
      cookie->relend = (cookie->rels
12001
			+ sec->reloc_count * bed->s->int_rels_per_ext_rel);
12002
    }
12003
  cookie->rel = cookie->rels;
12004
  return TRUE;
12005
}
12006
 
12007
/* Free the memory allocated by init_reloc_cookie_rels,
12008
   if appropriate.  */
12009
 
12010
static void
12011
fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12012
			asection *sec)
12013
{
12014
  if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12015
    free (cookie->rels);
12016
}
12017
 
12018
/* Initialize the whole of COOKIE for input section SEC.  */
12019
 
12020
static bfd_boolean
12021
init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12022
			       struct bfd_link_info *info,
12023
			       asection *sec)
12024
{
12025
  if (!init_reloc_cookie (cookie, info, sec->owner))
12026
    goto error1;
12027
  if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12028
    goto error2;
12029
  return TRUE;
12030
 
12031
 error2:
12032
  fini_reloc_cookie (cookie, sec->owner);
12033
 error1:
12034
  return FALSE;
12035
}
12036
 
12037
/* Free the memory allocated by init_reloc_cookie_for_section,
12038
   if appropriate.  */
12039
 
12040
static void
12041
fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12042
			       asection *sec)
12043
{
12044
  fini_reloc_cookie_rels (cookie, sec);
12045
  fini_reloc_cookie (cookie, sec->owner);
12046
}
12047
 
12048
/* Garbage collect unused sections.  */
12049
 
12050
/* Default gc_mark_hook.  */
12051
 
12052
asection *
12053
_bfd_elf_gc_mark_hook (asection *sec,
12054
		       struct bfd_link_info *info ATTRIBUTE_UNUSED,
12055
		       Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12056
		       struct elf_link_hash_entry *h,
12057
		       Elf_Internal_Sym *sym)
12058
{
12059
  if (h != NULL)
12060
    {
12061
      switch (h->root.type)
12062
	{
12063
	case bfd_link_hash_defined:
12064
	case bfd_link_hash_defweak:
12065
	  return h->root.u.def.section;
12066
 
12067
	case bfd_link_hash_common:
12068
	  return h->root.u.c.p->section;
12069
 
12070
	default:
12071
	  break;
12072
	}
12073
    }
12074
  else
12075
    return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12076
 
12077
  return NULL;
12078
}
12079
 
12080
/* COOKIE->rel describes a relocation against section SEC, which is
12081
   a section we've decided to keep.  Return the section that contains
12082
   the relocation symbol, or NULL if no section contains it.  */
12083
 
12084
asection *
12085
_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12086
		       elf_gc_mark_hook_fn gc_mark_hook,
6324 serge 12087
		       struct elf_reloc_cookie *cookie,
12088
		       bfd_boolean *start_stop)
5197 serge 12089
{
12090
  unsigned long r_symndx;
12091
  struct elf_link_hash_entry *h;
12092
 
12093
  r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12094
  if (r_symndx == STN_UNDEF)
12095
    return NULL;
12096
 
12097
  if (r_symndx >= cookie->locsymcount
12098
      || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12099
    {
12100
      h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
6324 serge 12101
      if (h == NULL)
12102
	{
12103
	  info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12104
				  sec->owner);
12105
	  return NULL;
12106
	}
5197 serge 12107
      while (h->root.type == bfd_link_hash_indirect
12108
	     || h->root.type == bfd_link_hash_warning)
12109
	h = (struct elf_link_hash_entry *) h->root.u.i.link;
12110
      h->mark = 1;
12111
      /* If this symbol is weak and there is a non-weak definition, we
12112
	 keep the non-weak definition because many backends put
12113
	 dynamic reloc info on the non-weak definition for code
12114
	 handling copy relocs.  */
12115
      if (h->u.weakdef != NULL)
12116
	h->u.weakdef->mark = 1;
6324 serge 12117
 
12118
      if (start_stop != NULL
12119
	  && (h->root.type == bfd_link_hash_undefined
12120
	      || h->root.type == bfd_link_hash_undefweak))
12121
	{
12122
	  /* To work around a glibc bug, mark all XXX input sections
12123
	     when there is an as yet undefined reference to __start_XXX
12124
	     or __stop_XXX symbols.  The linker will later define such
12125
	     symbols for orphan input sections that have a name
12126
	     representable as a C identifier.  */
12127
	  const char *sec_name = NULL;
12128
	  if (strncmp (h->root.root.string, "__start_", 8) == 0)
12129
	    sec_name = h->root.root.string + 8;
12130
	  else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12131
	    sec_name = h->root.root.string + 7;
12132
 
12133
	  if (sec_name != NULL && *sec_name != '\0')
12134
	    {
12135
	      bfd *i;
12136
 
12137
	      for (i = info->input_bfds; i != NULL; i = i->link.next)
12138
		{
12139
		  asection *s = bfd_get_section_by_name (i, sec_name);
12140
		  if (s != NULL && !s->gc_mark)
12141
		    {
12142
		      *start_stop = TRUE;
12143
		      return s;
12144
		    }
12145
		}
12146
	    }
12147
	}
12148
 
5197 serge 12149
      return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12150
    }
12151
 
12152
  return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12153
			  &cookie->locsyms[r_symndx]);
12154
}
12155
 
12156
/* COOKIE->rel describes a relocation against section SEC, which is
12157
   a section we've decided to keep.  Mark the section that contains
12158
   the relocation symbol.  */
12159
 
12160
bfd_boolean
12161
_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12162
			asection *sec,
12163
			elf_gc_mark_hook_fn gc_mark_hook,
12164
			struct elf_reloc_cookie *cookie)
12165
{
12166
  asection *rsec;
6324 serge 12167
  bfd_boolean start_stop = FALSE;
5197 serge 12168
 
6324 serge 12169
  rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12170
  while (rsec != NULL)
5197 serge 12171
    {
6324 serge 12172
      if (!rsec->gc_mark)
12173
	{
12174
	  if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12175
	      || (rsec->owner->flags & DYNAMIC) != 0)
12176
	    rsec->gc_mark = 1;
12177
	  else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12178
	    return FALSE;
12179
	}
12180
      if (!start_stop)
12181
	break;
12182
      rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5197 serge 12183
    }
12184
  return TRUE;
12185
}
12186
 
12187
/* The mark phase of garbage collection.  For a given section, mark
12188
   it and any sections in this section's group, and all the sections
12189
   which define symbols to which it refers.  */
12190
 
12191
bfd_boolean
12192
_bfd_elf_gc_mark (struct bfd_link_info *info,
12193
		  asection *sec,
12194
		  elf_gc_mark_hook_fn gc_mark_hook)
12195
{
12196
  bfd_boolean ret;
12197
  asection *group_sec, *eh_frame;
12198
 
12199
  sec->gc_mark = 1;
12200
 
12201
  /* Mark all the sections in the group.  */
12202
  group_sec = elf_section_data (sec)->next_in_group;
12203
  if (group_sec && !group_sec->gc_mark)
12204
    if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12205
      return FALSE;
12206
 
12207
  /* Look through the section relocs.  */
12208
  ret = TRUE;
12209
  eh_frame = elf_eh_frame_section (sec->owner);
12210
  if ((sec->flags & SEC_RELOC) != 0
12211
      && sec->reloc_count > 0
12212
      && sec != eh_frame)
12213
    {
12214
      struct elf_reloc_cookie cookie;
12215
 
12216
      if (!init_reloc_cookie_for_section (&cookie, info, sec))
12217
	ret = FALSE;
12218
      else
12219
	{
12220
	  for (; cookie.rel < cookie.relend; cookie.rel++)
12221
	    if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12222
	      {
12223
		ret = FALSE;
12224
		break;
12225
	      }
12226
	  fini_reloc_cookie_for_section (&cookie, sec);
12227
	}
12228
    }
12229
 
12230
  if (ret && eh_frame && elf_fde_list (sec))
12231
    {
12232
      struct elf_reloc_cookie cookie;
12233
 
12234
      if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12235
	ret = FALSE;
12236
      else
12237
	{
12238
	  if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12239
				      gc_mark_hook, &cookie))
12240
	    ret = FALSE;
12241
	  fini_reloc_cookie_for_section (&cookie, eh_frame);
12242
	}
12243
    }
12244
 
6324 serge 12245
  eh_frame = elf_section_eh_frame_entry (sec);
12246
  if (ret && eh_frame && !eh_frame->gc_mark)
12247
    if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12248
      ret = FALSE;
12249
 
5197 serge 12250
  return ret;
12251
}
12252
 
6324 serge 12253
/* Scan and mark sections in a special or debug section group.  */
12254
 
12255
static void
12256
_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12257
{
12258
  /* Point to first section of section group.  */
12259
  asection *ssec;
12260
  /* Used to iterate the section group.  */
12261
  asection *msec;
12262
 
12263
  bfd_boolean is_special_grp = TRUE;
12264
  bfd_boolean is_debug_grp = TRUE;
12265
 
12266
  /* First scan to see if group contains any section other than debug
12267
     and special section.  */
12268
  ssec = msec = elf_next_in_group (grp);
12269
  do
12270
    {
12271
      if ((msec->flags & SEC_DEBUGGING) == 0)
12272
	is_debug_grp = FALSE;
12273
 
12274
      if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12275
	is_special_grp = FALSE;
12276
 
12277
      msec = elf_next_in_group (msec);
12278
    }
12279
  while (msec != ssec);
12280
 
12281
  /* If this is a pure debug section group or pure special section group,
12282
     keep all sections in this group.  */
12283
  if (is_debug_grp || is_special_grp)
12284
    {
12285
      do
12286
	{
12287
	  msec->gc_mark = 1;
12288
	  msec = elf_next_in_group (msec);
12289
	}
12290
      while (msec != ssec);
12291
    }
12292
}
12293
 
5197 serge 12294
/* Keep debug and special sections.  */
12295
 
12296
bfd_boolean
12297
_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12298
				 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12299
{
12300
  bfd *ibfd;
12301
 
6324 serge 12302
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5197 serge 12303
    {
12304
      asection *isec;
12305
      bfd_boolean some_kept;
12306
      bfd_boolean debug_frag_seen;
12307
 
12308
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12309
	continue;
12310
 
12311
      /* Ensure all linker created sections are kept,
12312
	 see if any other section is already marked,
12313
	 and note if we have any fragmented debug sections.  */
12314
      debug_frag_seen = some_kept = FALSE;
12315
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12316
	{
12317
	  if ((isec->flags & SEC_LINKER_CREATED) != 0)
12318
	    isec->gc_mark = 1;
12319
	  else if (isec->gc_mark)
12320
	    some_kept = TRUE;
12321
 
12322
	  if (debug_frag_seen == FALSE
12323
	      && (isec->flags & SEC_DEBUGGING)
12324
	      && CONST_STRNEQ (isec->name, ".debug_line."))
12325
	    debug_frag_seen = TRUE;
12326
	}
12327
 
12328
      /* If no section in this file will be kept, then we can
12329
	 toss out the debug and special sections.  */
12330
      if (!some_kept)
12331
	continue;
12332
 
12333
      /* Keep debug and special sections like .comment when they are
6324 serge 12334
	 not part of a group.  Also keep section groups that contain
12335
	 just debug sections or special sections.  */
5197 serge 12336
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6324 serge 12337
	{
12338
	  if ((isec->flags & SEC_GROUP) != 0)
12339
	    _bfd_elf_gc_mark_debug_special_section_group (isec);
12340
	  else if (((isec->flags & SEC_DEBUGGING) != 0
12341
		    || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12342
		   && elf_next_in_group (isec) == NULL)
12343
	    isec->gc_mark = 1;
12344
	}
5197 serge 12345
 
12346
      if (! debug_frag_seen)
12347
	continue;
12348
 
12349
      /* Look for CODE sections which are going to be discarded,
12350
	 and find and discard any fragmented debug sections which
12351
	 are associated with that code section.  */
12352
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12353
	if ((isec->flags & SEC_CODE) != 0
12354
	    && isec->gc_mark == 0)
12355
	  {
12356
	    unsigned int ilen;
12357
	    asection *dsec;
12358
 
12359
	    ilen = strlen (isec->name);
12360
 
12361
	    /* Association is determined by the name of the debug section
12362
	       containing the name of the code section as a suffix.  For
12363
	       example .debug_line.text.foo is a debug section associated
12364
	       with .text.foo.  */
12365
	    for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12366
	      {
12367
		unsigned int dlen;
12368
 
12369
		if (dsec->gc_mark == 0
12370
		    || (dsec->flags & SEC_DEBUGGING) == 0)
12371
		  continue;
12372
 
12373
		dlen = strlen (dsec->name);
12374
 
12375
		if (dlen > ilen
12376
		    && strncmp (dsec->name + (dlen - ilen),
12377
				isec->name, ilen) == 0)
12378
		  {
12379
		    dsec->gc_mark = 0;
12380
		  }
12381
	      }
12382
	  }
12383
    }
12384
  return TRUE;
12385
}
12386
 
12387
/* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
12388
 
12389
struct elf_gc_sweep_symbol_info
12390
{
12391
  struct bfd_link_info *info;
12392
  void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12393
		       bfd_boolean);
12394
};
12395
 
12396
static bfd_boolean
12397
elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12398
{
12399
  if (!h->mark
12400
      && (((h->root.type == bfd_link_hash_defined
12401
	    || h->root.type == bfd_link_hash_defweak)
6324 serge 12402
	   && !((h->def_regular || ELF_COMMON_DEF_P (h))
5197 serge 12403
		&& h->root.u.def.section->gc_mark))
12404
	  || h->root.type == bfd_link_hash_undefined
12405
	  || h->root.type == bfd_link_hash_undefweak))
12406
    {
12407
      struct elf_gc_sweep_symbol_info *inf;
12408
 
12409
      inf = (struct elf_gc_sweep_symbol_info *) data;
12410
      (*inf->hide_symbol) (inf->info, h, TRUE);
12411
      h->def_regular = 0;
12412
      h->ref_regular = 0;
12413
      h->ref_regular_nonweak = 0;
12414
    }
12415
 
12416
  return TRUE;
12417
}
12418
 
12419
/* The sweep phase of garbage collection.  Remove all garbage sections.  */
12420
 
12421
typedef bfd_boolean (*gc_sweep_hook_fn)
12422
  (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12423
 
12424
static bfd_boolean
12425
elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12426
{
12427
  bfd *sub;
12428
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12429
  gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12430
  unsigned long section_sym_count;
12431
  struct elf_gc_sweep_symbol_info sweep_info;
12432
 
6324 serge 12433
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
5197 serge 12434
    {
12435
      asection *o;
12436
 
6324 serge 12437
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12438
	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
5197 serge 12439
	continue;
12440
 
12441
      for (o = sub->sections; o != NULL; o = o->next)
12442
	{
12443
	  /* When any section in a section group is kept, we keep all
12444
	     sections in the section group.  If the first member of
12445
	     the section group is excluded, we will also exclude the
12446
	     group section.  */
12447
	  if (o->flags & SEC_GROUP)
12448
	    {
12449
	      asection *first = elf_next_in_group (o);
12450
	      o->gc_mark = first->gc_mark;
12451
	    }
12452
 
12453
	  if (o->gc_mark)
12454
	    continue;
12455
 
12456
	  /* Skip sweeping sections already excluded.  */
12457
	  if (o->flags & SEC_EXCLUDE)
12458
	    continue;
12459
 
12460
	  /* Since this is early in the link process, it is simple
12461
	     to remove a section from the output.  */
12462
	  o->flags |= SEC_EXCLUDE;
12463
 
12464
	  if (info->print_gc_sections && o->size != 0)
12465
	    _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12466
 
12467
	  /* But we also have to update some of the relocation
12468
	     info we collected before.  */
12469
	  if (gc_sweep_hook
12470
	      && (o->flags & SEC_RELOC) != 0
6324 serge 12471
	      && o->reloc_count != 0
12472
	      && !((info->strip == strip_all || info->strip == strip_debugger)
12473
		   && (o->flags & SEC_DEBUGGING) != 0)
5197 serge 12474
	      && !bfd_is_abs_section (o->output_section))
12475
	    {
12476
	      Elf_Internal_Rela *internal_relocs;
12477
	      bfd_boolean r;
12478
 
12479
	      internal_relocs
12480
		= _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12481
					     info->keep_memory);
12482
	      if (internal_relocs == NULL)
12483
		return FALSE;
12484
 
12485
	      r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12486
 
12487
	      if (elf_section_data (o)->relocs != internal_relocs)
12488
		free (internal_relocs);
12489
 
12490
	      if (!r)
12491
		return FALSE;
12492
	    }
12493
	}
12494
    }
12495
 
12496
  /* Remove the symbols that were in the swept sections from the dynamic
12497
     symbol table.  GCFIXME: Anyone know how to get them out of the
12498
     static symbol table as well?  */
12499
  sweep_info.info = info;
12500
  sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12501
  elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12502
			  &sweep_info);
12503
 
12504
  _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
12505
  return TRUE;
12506
}
12507
 
12508
/* Propagate collected vtable information.  This is called through
12509
   elf_link_hash_traverse.  */
12510
 
12511
static bfd_boolean
12512
elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12513
{
12514
  /* Those that are not vtables.  */
12515
  if (h->vtable == NULL || h->vtable->parent == NULL)
12516
    return TRUE;
12517
 
12518
  /* Those vtables that do not have parents, we cannot merge.  */
12519
  if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12520
    return TRUE;
12521
 
12522
  /* If we've already been done, exit.  */
12523
  if (h->vtable->used && h->vtable->used[-1])
12524
    return TRUE;
12525
 
12526
  /* Make sure the parent's table is up to date.  */
12527
  elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12528
 
12529
  if (h->vtable->used == NULL)
12530
    {
12531
      /* None of this table's entries were referenced.  Re-use the
12532
	 parent's table.  */
12533
      h->vtable->used = h->vtable->parent->vtable->used;
12534
      h->vtable->size = h->vtable->parent->vtable->size;
12535
    }
12536
  else
12537
    {
12538
      size_t n;
12539
      bfd_boolean *cu, *pu;
12540
 
12541
      /* Or the parent's entries into ours.  */
12542
      cu = h->vtable->used;
12543
      cu[-1] = TRUE;
12544
      pu = h->vtable->parent->vtable->used;
12545
      if (pu != NULL)
12546
	{
12547
	  const struct elf_backend_data *bed;
12548
	  unsigned int log_file_align;
12549
 
12550
	  bed = get_elf_backend_data (h->root.u.def.section->owner);
12551
	  log_file_align = bed->s->log_file_align;
12552
	  n = h->vtable->parent->vtable->size >> log_file_align;
12553
	  while (n--)
12554
	    {
12555
	      if (*pu)
12556
		*cu = TRUE;
12557
	      pu++;
12558
	      cu++;
12559
	    }
12560
	}
12561
    }
12562
 
12563
  return TRUE;
12564
}
12565
 
12566
static bfd_boolean
12567
elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12568
{
12569
  asection *sec;
12570
  bfd_vma hstart, hend;
12571
  Elf_Internal_Rela *relstart, *relend, *rel;
12572
  const struct elf_backend_data *bed;
12573
  unsigned int log_file_align;
12574
 
12575
  /* Take care of both those symbols that do not describe vtables as
12576
     well as those that are not loaded.  */
12577
  if (h->vtable == NULL || h->vtable->parent == NULL)
12578
    return TRUE;
12579
 
12580
  BFD_ASSERT (h->root.type == bfd_link_hash_defined
12581
	      || h->root.type == bfd_link_hash_defweak);
12582
 
12583
  sec = h->root.u.def.section;
12584
  hstart = h->root.u.def.value;
12585
  hend = hstart + h->size;
12586
 
12587
  relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12588
  if (!relstart)
12589
    return *(bfd_boolean *) okp = FALSE;
12590
  bed = get_elf_backend_data (sec->owner);
12591
  log_file_align = bed->s->log_file_align;
12592
 
12593
  relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12594
 
12595
  for (rel = relstart; rel < relend; ++rel)
12596
    if (rel->r_offset >= hstart && rel->r_offset < hend)
12597
      {
12598
	/* If the entry is in use, do nothing.  */
12599
	if (h->vtable->used
12600
	    && (rel->r_offset - hstart) < h->vtable->size)
12601
	  {
12602
	    bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12603
	    if (h->vtable->used[entry])
12604
	      continue;
12605
	  }
12606
	/* Otherwise, kill it.  */
12607
	rel->r_offset = rel->r_info = rel->r_addend = 0;
12608
      }
12609
 
12610
  return TRUE;
12611
}
12612
 
12613
/* Mark sections containing dynamically referenced symbols.  When
12614
   building shared libraries, we must assume that any visible symbol is
12615
   referenced.  */
12616
 
12617
bfd_boolean
12618
bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12619
{
12620
  struct bfd_link_info *info = (struct bfd_link_info *) inf;
6324 serge 12621
  struct bfd_elf_dynamic_list *d = info->dynamic_list;
5197 serge 12622
 
12623
  if ((h->root.type == bfd_link_hash_defined
12624
       || h->root.type == bfd_link_hash_defweak)
12625
      && (h->ref_dynamic
6324 serge 12626
	  || ((h->def_regular || ELF_COMMON_DEF_P (h))
5197 serge 12627
	      && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12628
	      && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
6324 serge 12629
	      && (!bfd_link_executable (info)
12630
		  || info->export_dynamic
12631
		  || (h->dynamic
12632
		      && d != NULL
12633
		      && (*d->match) (&d->head, NULL, h->root.root.string)))
12634
	      && (h->versioned >= versioned
5197 serge 12635
		  || !bfd_hide_sym_by_version (info->version_info,
12636
					       h->root.root.string)))))
12637
    h->root.u.def.section->flags |= SEC_KEEP;
12638
 
12639
  return TRUE;
12640
}
12641
 
12642
/* Keep all sections containing symbols undefined on the command-line,
12643
   and the section containing the entry symbol.  */
12644
 
12645
void
12646
_bfd_elf_gc_keep (struct bfd_link_info *info)
12647
{
12648
  struct bfd_sym_chain *sym;
12649
 
12650
  for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12651
    {
12652
      struct elf_link_hash_entry *h;
12653
 
12654
      h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12655
				FALSE, FALSE, FALSE);
12656
 
12657
      if (h != NULL
12658
	  && (h->root.type == bfd_link_hash_defined
12659
	      || h->root.type == bfd_link_hash_defweak)
12660
	  && !bfd_is_abs_section (h->root.u.def.section))
12661
	h->root.u.def.section->flags |= SEC_KEEP;
12662
    }
12663
}
12664
 
6324 serge 12665
bfd_boolean
12666
bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12667
				struct bfd_link_info *info)
12668
{
12669
  bfd *ibfd = info->input_bfds;
12670
 
12671
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12672
    {
12673
      asection *sec;
12674
      struct elf_reloc_cookie cookie;
12675
 
12676
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12677
	continue;
12678
 
12679
      if (!init_reloc_cookie (&cookie, info, ibfd))
12680
	return FALSE;
12681
 
12682
      for (sec = ibfd->sections; sec; sec = sec->next)
12683
	{
12684
	  if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12685
	      && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12686
	    {
12687
	      _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12688
	      fini_reloc_cookie_rels (&cookie, sec);
12689
	    }
12690
	}
12691
    }
12692
  return TRUE;
12693
}
12694
 
5197 serge 12695
/* Do mark and sweep of unused sections.  */
12696
 
12697
bfd_boolean
12698
bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12699
{
12700
  bfd_boolean ok = TRUE;
12701
  bfd *sub;
12702
  elf_gc_mark_hook_fn gc_mark_hook;
12703
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6324 serge 12704
  struct elf_link_hash_table *htab;
5197 serge 12705
 
12706
  if (!bed->can_gc_sections
12707
      || !is_elf_hash_table (info->hash))
12708
    {
12709
      (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12710
      return TRUE;
12711
    }
12712
 
12713
  bed->gc_keep (info);
6324 serge 12714
  htab = elf_hash_table (info);
5197 serge 12715
 
12716
  /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
12717
     at the .eh_frame section if we can mark the FDEs individually.  */
6324 serge 12718
  for (sub = info->input_bfds;
12719
       info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12720
       sub = sub->link.next)
5197 serge 12721
    {
12722
      asection *sec;
12723
      struct elf_reloc_cookie cookie;
12724
 
12725
      sec = bfd_get_section_by_name (sub, ".eh_frame");
12726
      while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
12727
	{
12728
	  _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
12729
	  if (elf_section_data (sec)->sec_info
12730
	      && (sec->flags & SEC_LINKER_CREATED) == 0)
12731
	    elf_eh_frame_section (sub) = sec;
12732
	  fini_reloc_cookie_for_section (&cookie, sec);
6324 serge 12733
	  sec = bfd_get_next_section_by_name (NULL, sec);
5197 serge 12734
	}
12735
    }
12736
 
12737
  /* Apply transitive closure to the vtable entry usage info.  */
6324 serge 12738
  elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
5197 serge 12739
  if (!ok)
12740
    return FALSE;
12741
 
12742
  /* Kill the vtable relocations that were not used.  */
6324 serge 12743
  elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
5197 serge 12744
  if (!ok)
12745
    return FALSE;
12746
 
12747
  /* Mark dynamically referenced symbols.  */
6324 serge 12748
  if (htab->dynamic_sections_created)
12749
    elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
5197 serge 12750
 
12751
  /* Grovel through relocs to find out who stays ...  */
12752
  gc_mark_hook = bed->gc_mark_hook;
6324 serge 12753
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
5197 serge 12754
    {
12755
      asection *o;
12756
 
6324 serge 12757
      if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12758
	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
5197 serge 12759
	continue;
12760
 
12761
      /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12762
	 Also treat note sections as a root, if the section is not part
12763
	 of a group.  */
12764
      for (o = sub->sections; o != NULL; o = o->next)
12765
	if (!o->gc_mark
12766
	    && (o->flags & SEC_EXCLUDE) == 0
12767
	    && ((o->flags & SEC_KEEP) != 0
12768
		|| (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12769
		    && elf_next_in_group (o) == NULL )))
12770
	  {
12771
	    if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12772
	      return FALSE;
12773
	  }
12774
    }
12775
 
12776
  /* Allow the backend to mark additional target specific sections.  */
12777
  bed->gc_mark_extra_sections (info, gc_mark_hook);
12778
 
12779
  /* ... and mark SEC_EXCLUDE for those that go.  */
12780
  return elf_gc_sweep (abfd, info);
12781
}
12782
 
12783
/* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
12784
 
12785
bfd_boolean
12786
bfd_elf_gc_record_vtinherit (bfd *abfd,
12787
			     asection *sec,
12788
			     struct elf_link_hash_entry *h,
12789
			     bfd_vma offset)
12790
{
12791
  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12792
  struct elf_link_hash_entry **search, *child;
12793
  bfd_size_type extsymcount;
12794
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12795
 
12796
  /* The sh_info field of the symtab header tells us where the
12797
     external symbols start.  We don't care about the local symbols at
12798
     this point.  */
12799
  extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12800
  if (!elf_bad_symtab (abfd))
12801
    extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12802
 
12803
  sym_hashes = elf_sym_hashes (abfd);
12804
  sym_hashes_end = sym_hashes + extsymcount;
12805
 
12806
  /* Hunt down the child symbol, which is in this section at the same
12807
     offset as the relocation.  */
12808
  for (search = sym_hashes; search != sym_hashes_end; ++search)
12809
    {
12810
      if ((child = *search) != NULL
12811
	  && (child->root.type == bfd_link_hash_defined
12812
	      || child->root.type == bfd_link_hash_defweak)
12813
	  && child->root.u.def.section == sec
12814
	  && child->root.u.def.value == offset)
12815
	goto win;
12816
    }
12817
 
12818
  (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12819
			 abfd, sec, (unsigned long) offset);
12820
  bfd_set_error (bfd_error_invalid_operation);
12821
  return FALSE;
12822
 
12823
 win:
12824
  if (!child->vtable)
12825
    {
6324 serge 12826
      child->vtable = ((struct elf_link_virtual_table_entry *)
12827
		       bfd_zalloc (abfd, sizeof (*child->vtable)));
5197 serge 12828
      if (!child->vtable)
12829
	return FALSE;
12830
    }
12831
  if (!h)
12832
    {
12833
      /* This *should* only be the absolute section.  It could potentially
12834
	 be that someone has defined a non-global vtable though, which
12835
	 would be bad.  It isn't worth paging in the local symbols to be
12836
	 sure though; that case should simply be handled by the assembler.  */
12837
 
12838
      child->vtable->parent = (struct elf_link_hash_entry *) -1;
12839
    }
12840
  else
12841
    child->vtable->parent = h;
12842
 
12843
  return TRUE;
12844
}
12845
 
12846
/* Called from check_relocs to record the existence of a VTENTRY reloc.  */
12847
 
12848
bfd_boolean
12849
bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12850
			   asection *sec ATTRIBUTE_UNUSED,
12851
			   struct elf_link_hash_entry *h,
12852
			   bfd_vma addend)
12853
{
12854
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12855
  unsigned int log_file_align = bed->s->log_file_align;
12856
 
12857
  if (!h->vtable)
12858
    {
6324 serge 12859
      h->vtable = ((struct elf_link_virtual_table_entry *)
12860
		   bfd_zalloc (abfd, sizeof (*h->vtable)));
5197 serge 12861
      if (!h->vtable)
12862
	return FALSE;
12863
    }
12864
 
12865
  if (addend >= h->vtable->size)
12866
    {
12867
      size_t size, bytes, file_align;
12868
      bfd_boolean *ptr = h->vtable->used;
12869
 
12870
      /* While the symbol is undefined, we have to be prepared to handle
12871
	 a zero size.  */
12872
      file_align = 1 << log_file_align;
12873
      if (h->root.type == bfd_link_hash_undefined)
12874
	size = addend + file_align;
12875
      else
12876
	{
12877
	  size = h->size;
12878
	  if (addend >= size)
12879
	    {
12880
	      /* Oops!  We've got a reference past the defined end of
12881
		 the table.  This is probably a bug -- shall we warn?  */
12882
	      size = addend + file_align;
12883
	    }
12884
	}
12885
      size = (size + file_align - 1) & -file_align;
12886
 
12887
      /* Allocate one extra entry for use as a "done" flag for the
12888
	 consolidation pass.  */
12889
      bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12890
 
12891
      if (ptr)
12892
	{
12893
	  ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
12894
 
12895
	  if (ptr != NULL)
12896
	    {
12897
	      size_t oldbytes;
12898
 
12899
	      oldbytes = (((h->vtable->size >> log_file_align) + 1)
12900
			  * sizeof (bfd_boolean));
12901
	      memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12902
	    }
12903
	}
12904
      else
12905
	ptr = (bfd_boolean *) bfd_zmalloc (bytes);
12906
 
12907
      if (ptr == NULL)
12908
	return FALSE;
12909
 
12910
      /* And arrange for that done flag to be at index -1.  */
12911
      h->vtable->used = ptr + 1;
12912
      h->vtable->size = size;
12913
    }
12914
 
12915
  h->vtable->used[addend >> log_file_align] = TRUE;
12916
 
12917
  return TRUE;
12918
}
12919
 
12920
/* Map an ELF section header flag to its corresponding string.  */
12921
typedef struct
12922
{
12923
  char *flag_name;
12924
  flagword flag_value;
12925
} elf_flags_to_name_table;
12926
 
12927
static elf_flags_to_name_table elf_flags_to_names [] =
12928
{
12929
  { "SHF_WRITE", SHF_WRITE },
12930
  { "SHF_ALLOC", SHF_ALLOC },
12931
  { "SHF_EXECINSTR", SHF_EXECINSTR },
12932
  { "SHF_MERGE", SHF_MERGE },
12933
  { "SHF_STRINGS", SHF_STRINGS },
12934
  { "SHF_INFO_LINK", SHF_INFO_LINK},
12935
  { "SHF_LINK_ORDER", SHF_LINK_ORDER},
12936
  { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
12937
  { "SHF_GROUP", SHF_GROUP },
12938
  { "SHF_TLS", SHF_TLS },
12939
  { "SHF_MASKOS", SHF_MASKOS },
12940
  { "SHF_EXCLUDE", SHF_EXCLUDE },
12941
};
12942
 
12943
/* Returns TRUE if the section is to be included, otherwise FALSE.  */
12944
bfd_boolean
12945
bfd_elf_lookup_section_flags (struct bfd_link_info *info,
12946
			      struct flag_info *flaginfo,
12947
			      asection *section)
12948
{
12949
  const bfd_vma sh_flags = elf_section_flags (section);
12950
 
12951
  if (!flaginfo->flags_initialized)
12952
    {
12953
      bfd *obfd = info->output_bfd;
12954
      const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12955
      struct flag_info_list *tf = flaginfo->flag_list;
12956
      int with_hex = 0;
12957
      int without_hex = 0;
12958
 
12959
      for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
12960
	{
12961
	  unsigned i;
12962
	  flagword (*lookup) (char *);
12963
 
12964
	  lookup = bed->elf_backend_lookup_section_flags_hook;
12965
	  if (lookup != NULL)
12966
	    {
12967
	      flagword hexval = (*lookup) ((char *) tf->name);
12968
 
12969
	      if (hexval != 0)
12970
		{
12971
		  if (tf->with == with_flags)
12972
		    with_hex |= hexval;
12973
		  else if (tf->with == without_flags)
12974
		    without_hex |= hexval;
12975
		  tf->valid = TRUE;
12976
		  continue;
12977
		}
12978
	    }
12979
	  for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
12980
	    {
12981
	      if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
12982
		{
12983
		  if (tf->with == with_flags)
12984
		    with_hex |= elf_flags_to_names[i].flag_value;
12985
		  else if (tf->with == without_flags)
12986
		    without_hex |= elf_flags_to_names[i].flag_value;
12987
		  tf->valid = TRUE;
12988
		  break;
12989
		}
12990
	    }
12991
	  if (!tf->valid)
12992
	    {
12993
	      info->callbacks->einfo
12994
		(_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
12995
	      return FALSE;
12996
	    }
12997
	}
12998
      flaginfo->flags_initialized = TRUE;
12999
      flaginfo->only_with_flags |= with_hex;
13000
      flaginfo->not_with_flags |= without_hex;
13001
    }
13002
 
13003
  if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13004
    return FALSE;
13005
 
13006
  if ((flaginfo->not_with_flags & sh_flags) != 0)
13007
    return FALSE;
13008
 
13009
  return TRUE;
13010
}
13011
 
13012
struct alloc_got_off_arg {
13013
  bfd_vma gotoff;
13014
  struct bfd_link_info *info;
13015
};
13016
 
13017
/* We need a special top-level link routine to convert got reference counts
13018
   to real got offsets.  */
13019
 
13020
static bfd_boolean
13021
elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13022
{
13023
  struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13024
  bfd *obfd = gofarg->info->output_bfd;
13025
  const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13026
 
13027
  if (h->got.refcount > 0)
13028
    {
13029
      h->got.offset = gofarg->gotoff;
13030
      gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13031
    }
13032
  else
13033
    h->got.offset = (bfd_vma) -1;
13034
 
13035
  return TRUE;
13036
}
13037
 
13038
/* And an accompanying bit to work out final got entry offsets once
13039
   we're done.  Should be called from final_link.  */
13040
 
13041
bfd_boolean
13042
bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13043
					struct bfd_link_info *info)
13044
{
13045
  bfd *i;
13046
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13047
  bfd_vma gotoff;
13048
  struct alloc_got_off_arg gofarg;
13049
 
13050
  BFD_ASSERT (abfd == info->output_bfd);
13051
 
13052
  if (! is_elf_hash_table (info->hash))
13053
    return FALSE;
13054
 
13055
  /* The GOT offset is relative to the .got section, but the GOT header is
13056
     put into the .got.plt section, if the backend uses it.  */
13057
  if (bed->want_got_plt)
13058
    gotoff = 0;
13059
  else
13060
    gotoff = bed->got_header_size;
13061
 
13062
  /* Do the local .got entries first.  */
6324 serge 13063
  for (i = info->input_bfds; i; i = i->link.next)
5197 serge 13064
    {
13065
      bfd_signed_vma *local_got;
13066
      bfd_size_type j, locsymcount;
13067
      Elf_Internal_Shdr *symtab_hdr;
13068
 
13069
      if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13070
	continue;
13071
 
13072
      local_got = elf_local_got_refcounts (i);
13073
      if (!local_got)
13074
	continue;
13075
 
13076
      symtab_hdr = &elf_tdata (i)->symtab_hdr;
13077
      if (elf_bad_symtab (i))
13078
	locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13079
      else
13080
	locsymcount = symtab_hdr->sh_info;
13081
 
13082
      for (j = 0; j < locsymcount; ++j)
13083
	{
13084
	  if (local_got[j] > 0)
13085
	    {
13086
	      local_got[j] = gotoff;
13087
	      gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13088
	    }
13089
	  else
13090
	    local_got[j] = (bfd_vma) -1;
13091
	}
13092
    }
13093
 
13094
  /* Then the global .got entries.  .plt refcounts are handled by
13095
     adjust_dynamic_symbol  */
13096
  gofarg.gotoff = gotoff;
13097
  gofarg.info = info;
13098
  elf_link_hash_traverse (elf_hash_table (info),
13099
			  elf_gc_allocate_got_offsets,
13100
			  &gofarg);
13101
  return TRUE;
13102
}
13103
 
13104
/* Many folk need no more in the way of final link than this, once
13105
   got entry reference counting is enabled.  */
13106
 
13107
bfd_boolean
13108
bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13109
{
13110
  if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13111
    return FALSE;
13112
 
13113
  /* Invoke the regular ELF backend linker to do all the work.  */
13114
  return bfd_elf_final_link (abfd, info);
13115
}
13116
 
13117
bfd_boolean
13118
bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13119
{
13120
  struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13121
 
13122
  if (rcookie->bad_symtab)
13123
    rcookie->rel = rcookie->rels;
13124
 
13125
  for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13126
    {
13127
      unsigned long r_symndx;
13128
 
13129
      if (! rcookie->bad_symtab)
13130
	if (rcookie->rel->r_offset > offset)
13131
	  return FALSE;
13132
      if (rcookie->rel->r_offset != offset)
13133
	continue;
13134
 
13135
      r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13136
      if (r_symndx == STN_UNDEF)
13137
	return TRUE;
13138
 
13139
      if (r_symndx >= rcookie->locsymcount
13140
	  || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13141
	{
13142
	  struct elf_link_hash_entry *h;
13143
 
13144
	  h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13145
 
13146
	  while (h->root.type == bfd_link_hash_indirect
13147
		 || h->root.type == bfd_link_hash_warning)
13148
	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
13149
 
13150
	  if ((h->root.type == bfd_link_hash_defined
13151
	       || h->root.type == bfd_link_hash_defweak)
6324 serge 13152
	      && (h->root.u.def.section->owner != rcookie->abfd
13153
		  || h->root.u.def.section->kept_section != NULL
13154
		  || discarded_section (h->root.u.def.section)))
5197 serge 13155
	    return TRUE;
13156
	}
13157
      else
13158
	{
13159
	  /* It's not a relocation against a global symbol,
13160
	     but it could be a relocation against a local
13161
	     symbol for a discarded section.  */
13162
	  asection *isec;
13163
	  Elf_Internal_Sym *isym;
13164
 
13165
	  /* Need to: get the symbol; get the section.  */
13166
	  isym = &rcookie->locsyms[r_symndx];
13167
	  isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
6324 serge 13168
	  if (isec != NULL
13169
	      && (isec->kept_section != NULL
13170
		  || discarded_section (isec)))
5197 serge 13171
	    return TRUE;
13172
	}
13173
      return FALSE;
13174
    }
13175
  return FALSE;
13176
}
13177
 
13178
/* Discard unneeded references to discarded sections.
6324 serge 13179
   Returns -1 on error, 1 if any section's size was changed, 0 if
13180
   nothing changed.  This function assumes that the relocations are in
13181
   sorted order, which is true for all known assemblers.  */
5197 serge 13182
 
6324 serge 13183
int
5197 serge 13184
bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13185
{
13186
  struct elf_reloc_cookie cookie;
6324 serge 13187
  asection *o;
5197 serge 13188
  bfd *abfd;
6324 serge 13189
  int changed = 0;
5197 serge 13190
 
13191
  if (info->traditional_format
13192
      || !is_elf_hash_table (info->hash))
6324 serge 13193
    return 0;
5197 serge 13194
 
6324 serge 13195
  o = bfd_get_section_by_name (output_bfd, ".stab");
13196
  if (o != NULL)
5197 serge 13197
    {
6324 serge 13198
      asection *i;
5197 serge 13199
 
6324 serge 13200
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
5197 serge 13201
	{
6324 serge 13202
	  if (i->size == 0
13203
	      || i->reloc_count == 0
13204
	      || i->sec_info_type != SEC_INFO_TYPE_STABS)
13205
	    continue;
5197 serge 13206
 
6324 serge 13207
	  abfd = i->owner;
13208
	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13209
	    continue;
5197 serge 13210
 
6324 serge 13211
	  if (!init_reloc_cookie_for_section (&cookie, info, i))
13212
	    return -1;
5197 serge 13213
 
6324 serge 13214
	  if (_bfd_discard_section_stabs (abfd, i,
13215
					  elf_section_data (i)->sec_info,
5197 serge 13216
					  bfd_elf_reloc_symbol_deleted_p,
13217
					  &cookie))
6324 serge 13218
	    changed = 1;
13219
 
13220
	  fini_reloc_cookie_for_section (&cookie, i);
5197 serge 13221
	}
6324 serge 13222
    }
5197 serge 13223
 
6324 serge 13224
  o = NULL;
13225
  if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13226
    o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13227
  if (o != NULL)
13228
    {
13229
      asection *i;
13230
 
13231
      for (i = o->map_head.s; i != NULL; i = i->map_head.s)
5197 serge 13232
	{
6324 serge 13233
	  if (i->size == 0)
13234
	    continue;
13235
 
13236
	  abfd = i->owner;
13237
	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13238
	    continue;
13239
 
13240
	  if (!init_reloc_cookie_for_section (&cookie, info, i))
13241
	    return -1;
13242
 
13243
	  _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13244
	  if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
5197 serge 13245
						 bfd_elf_reloc_symbol_deleted_p,
13246
						 &cookie))
6324 serge 13247
	    changed = 1;
13248
 
13249
	  fini_reloc_cookie_for_section (&cookie, i);
5197 serge 13250
	}
6324 serge 13251
    }
5197 serge 13252
 
6324 serge 13253
  for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13254
    {
13255
      const struct elf_backend_data *bed;
5197 serge 13256
 
6324 serge 13257
      if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13258
	continue;
13259
 
13260
      bed = get_elf_backend_data (abfd);
13261
 
13262
      if (bed->elf_backend_discard_info != NULL)
13263
	{
13264
	  if (!init_reloc_cookie (&cookie, info, abfd))
13265
	    return -1;
13266
 
13267
	  if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13268
	    changed = 1;
13269
 
13270
	  fini_reloc_cookie (&cookie, abfd);
13271
	}
5197 serge 13272
    }
13273
 
6324 serge 13274
  if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13275
    _bfd_elf_end_eh_frame_parsing (info);
13276
 
13277
  if (info->eh_frame_hdr_type
13278
      && !bfd_link_relocatable (info)
5197 serge 13279
      && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
6324 serge 13280
    changed = 1;
5197 serge 13281
 
6324 serge 13282
  return changed;
5197 serge 13283
}
13284
 
13285
bfd_boolean
13286
_bfd_elf_section_already_linked (bfd *abfd,
13287
				 asection *sec,
13288
				 struct bfd_link_info *info)
13289
{
13290
  flagword flags;
13291
  const char *name, *key;
13292
  struct bfd_section_already_linked *l;
13293
  struct bfd_section_already_linked_hash_entry *already_linked_list;
13294
 
13295
  if (sec->output_section == bfd_abs_section_ptr)
13296
    return FALSE;
13297
 
13298
  flags = sec->flags;
13299
 
13300
  /* Return if it isn't a linkonce section.  A comdat group section
13301
     also has SEC_LINK_ONCE set.  */
13302
  if ((flags & SEC_LINK_ONCE) == 0)
13303
    return FALSE;
13304
 
13305
  /* Don't put group member sections on our list of already linked
13306
     sections.  They are handled as a group via their group section.  */
13307
  if (elf_sec_group (sec) != NULL)
13308
    return FALSE;
13309
 
13310
  /* For a SHT_GROUP section, use the group signature as the key.  */
13311
  name = sec->name;
13312
  if ((flags & SEC_GROUP) != 0
13313
      && elf_next_in_group (sec) != NULL
13314
      && elf_group_name (elf_next_in_group (sec)) != NULL)
13315
    key = elf_group_name (elf_next_in_group (sec));
13316
  else
13317
    {
13318
      /* Otherwise we should have a .gnu.linkonce.. section.  */
13319
      if (CONST_STRNEQ (name, ".gnu.linkonce.")
13320
	  && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13321
	key++;
13322
      else
13323
	/* Must be a user linkonce section that doesn't follow gcc's
13324
	   naming convention.  In this case we won't be matching
13325
	   single member groups.  */
13326
	key = name;
13327
    }
13328
 
13329
  already_linked_list = bfd_section_already_linked_table_lookup (key);
13330
 
13331
  for (l = already_linked_list->entry; l != NULL; l = l->next)
13332
    {
13333
      /* We may have 2 different types of sections on the list: group
13334
	 sections with a signature of  ( is some string),
13335
	 and linkonce sections named .gnu.linkonce...
13336
	 Match like sections.  LTO plugin sections are an exception.
13337
	 They are always named .gnu.linkonce.t. and match either
13338
	 type of section.  */
13339
      if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13340
	   && ((flags & SEC_GROUP) != 0
13341
	       || strcmp (name, l->sec->name) == 0))
13342
	  || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13343
	{
13344
	  /* The section has already been linked.  See if we should
13345
	     issue a warning.  */
13346
	  if (!_bfd_handle_already_linked (sec, l, info))
13347
	    return FALSE;
13348
 
13349
	  if (flags & SEC_GROUP)
13350
	    {
13351
	      asection *first = elf_next_in_group (sec);
13352
	      asection *s = first;
13353
 
13354
	      while (s != NULL)
13355
		{
13356
		  s->output_section = bfd_abs_section_ptr;
13357
		  /* Record which group discards it.  */
13358
		  s->kept_section = l->sec;
13359
		  s = elf_next_in_group (s);
13360
		  /* These lists are circular.  */
13361
		  if (s == first)
13362
		    break;
13363
		}
13364
	    }
13365
 
13366
	  return TRUE;
13367
	}
13368
    }
13369
 
13370
  /* A single member comdat group section may be discarded by a
13371
     linkonce section and vice versa.  */
13372
  if ((flags & SEC_GROUP) != 0)
13373
    {
13374
      asection *first = elf_next_in_group (sec);
13375
 
13376
      if (first != NULL && elf_next_in_group (first) == first)
13377
	/* Check this single member group against linkonce sections.  */
13378
	for (l = already_linked_list->entry; l != NULL; l = l->next)
13379
	  if ((l->sec->flags & SEC_GROUP) == 0
13380
	      && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13381
	    {
13382
	      first->output_section = bfd_abs_section_ptr;
13383
	      first->kept_section = l->sec;
13384
	      sec->output_section = bfd_abs_section_ptr;
13385
	      break;
13386
	    }
13387
    }
13388
  else
13389
    /* Check this linkonce section against single member groups.  */
13390
    for (l = already_linked_list->entry; l != NULL; l = l->next)
13391
      if (l->sec->flags & SEC_GROUP)
13392
	{
13393
	  asection *first = elf_next_in_group (l->sec);
13394
 
13395
	  if (first != NULL
13396
	      && elf_next_in_group (first) == first
13397
	      && bfd_elf_match_symbols_in_sections (first, sec, info))
13398
	    {
13399
	      sec->output_section = bfd_abs_section_ptr;
13400
	      sec->kept_section = first;
13401
	      break;
13402
	    }
13403
	}
13404
 
13405
  /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13406
     referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13407
     specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13408
     prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
13409
     matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
13410
     but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13411
     `.gnu.linkonce.t.F' section from a different bfd not requiring any
13412
     `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
13413
     The reverse order cannot happen as there is never a bfd with only the
13414
     `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
13415
     matter as here were are looking only for cross-bfd sections.  */
13416
 
13417
  if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13418
    for (l = already_linked_list->entry; l != NULL; l = l->next)
13419
      if ((l->sec->flags & SEC_GROUP) == 0
13420
	  && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13421
	{
13422
	  if (abfd != l->sec->owner)
13423
	    sec->output_section = bfd_abs_section_ptr;
13424
	  break;
13425
	}
13426
 
13427
  /* This is the first section with this name.  Record it.  */
13428
  if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13429
    info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13430
  return sec->output_section == bfd_abs_section_ptr;
13431
}
13432
 
13433
bfd_boolean
13434
_bfd_elf_common_definition (Elf_Internal_Sym *sym)
13435
{
13436
  return sym->st_shndx == SHN_COMMON;
13437
}
13438
 
13439
unsigned int
13440
_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13441
{
13442
  return SHN_COMMON;
13443
}
13444
 
13445
asection *
13446
_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13447
{
13448
  return bfd_com_section_ptr;
13449
}
13450
 
13451
bfd_vma
13452
_bfd_elf_default_got_elt_size (bfd *abfd,
13453
			       struct bfd_link_info *info ATTRIBUTE_UNUSED,
13454
			       struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13455
			       bfd *ibfd ATTRIBUTE_UNUSED,
13456
			       unsigned long symndx ATTRIBUTE_UNUSED)
13457
{
13458
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13459
  return bed->s->arch_size / 8;
13460
}
13461
 
13462
/* Routines to support the creation of dynamic relocs.  */
13463
 
13464
/* Returns the name of the dynamic reloc section associated with SEC.  */
13465
 
13466
static const char *
13467
get_dynamic_reloc_section_name (bfd *       abfd,
13468
				asection *  sec,
13469
				bfd_boolean is_rela)
13470
{
13471
  char *name;
13472
  const char *old_name = bfd_get_section_name (NULL, sec);
13473
  const char *prefix = is_rela ? ".rela" : ".rel";
13474
 
13475
  if (old_name == NULL)
13476
    return NULL;
13477
 
13478
  name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13479
  sprintf (name, "%s%s", prefix, old_name);
13480
 
13481
  return name;
13482
}
13483
 
13484
/* Returns the dynamic reloc section associated with SEC.
13485
   If necessary compute the name of the dynamic reloc section based
13486
   on SEC's name (looked up in ABFD's string table) and the setting
13487
   of IS_RELA.  */
13488
 
13489
asection *
13490
_bfd_elf_get_dynamic_reloc_section (bfd *       abfd,
13491
				    asection *  sec,
13492
				    bfd_boolean is_rela)
13493
{
13494
  asection * reloc_sec = elf_section_data (sec)->sreloc;
13495
 
13496
  if (reloc_sec == NULL)
13497
    {
13498
      const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13499
 
13500
      if (name != NULL)
13501
	{
13502
	  reloc_sec = bfd_get_linker_section (abfd, name);
13503
 
13504
	  if (reloc_sec != NULL)
13505
	    elf_section_data (sec)->sreloc = reloc_sec;
13506
	}
13507
    }
13508
 
13509
  return reloc_sec;
13510
}
13511
 
13512
/* Returns the dynamic reloc section associated with SEC.  If the
13513
   section does not exist it is created and attached to the DYNOBJ
13514
   bfd and stored in the SRELOC field of SEC's elf_section_data
13515
   structure.
13516
 
13517
   ALIGNMENT is the alignment for the newly created section and
13518
   IS_RELA defines whether the name should be .rela.
13519
   or .rel..  The section name is looked up in the
13520
   string table associated with ABFD.  */
13521
 
13522
asection *
6324 serge 13523
_bfd_elf_make_dynamic_reloc_section (asection *sec,
13524
				     bfd *dynobj,
13525
				     unsigned int alignment,
13526
				     bfd *abfd,
13527
				     bfd_boolean is_rela)
5197 serge 13528
{
13529
  asection * reloc_sec = elf_section_data (sec)->sreloc;
13530
 
13531
  if (reloc_sec == NULL)
13532
    {
13533
      const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13534
 
13535
      if (name == NULL)
13536
	return NULL;
13537
 
13538
      reloc_sec = bfd_get_linker_section (dynobj, name);
13539
 
13540
      if (reloc_sec == NULL)
13541
	{
13542
	  flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13543
			    | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13544
	  if ((sec->flags & SEC_ALLOC) != 0)
13545
	    flags |= SEC_ALLOC | SEC_LOAD;
13546
 
13547
	  reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13548
	  if (reloc_sec != NULL)
13549
	    {
13550
	      /* _bfd_elf_get_sec_type_attr chooses a section type by
13551
		 name.  Override as it may be wrong, eg. for a user
13552
		 section named "auto" we'll get ".relauto" which is
13553
		 seen to be a .rela section.  */
13554
	      elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13555
	      if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13556
		reloc_sec = NULL;
13557
	    }
13558
	}
13559
 
13560
      elf_section_data (sec)->sreloc = reloc_sec;
13561
    }
13562
 
13563
  return reloc_sec;
13564
}
13565
 
6324 serge 13566
/* Copy the ELF symbol type and other attributes for a linker script
13567
   assignment from HSRC to HDEST.  Generally this should be treated as
13568
   if we found a strong non-dynamic definition for HDEST (except that
13569
   ld ignores multiple definition errors).  */
5197 serge 13570
void
6324 serge 13571
_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13572
				     struct bfd_link_hash_entry *hdest,
13573
				     struct bfd_link_hash_entry *hsrc)
5197 serge 13574
{
6324 serge 13575
  struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13576
  struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13577
  Elf_Internal_Sym isym;
5197 serge 13578
 
13579
  ehdest->type = ehsrc->type;
13580
  ehdest->target_internal = ehsrc->target_internal;
6324 serge 13581
 
13582
  isym.st_other = ehsrc->other;
13583
  elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
5197 serge 13584
}
13585
 
13586
/* Append a RELA relocation REL to section S in BFD.  */
13587
 
13588
void
13589
elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13590
{
13591
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13592
  bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13593
  BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13594
  bed->s->swap_reloca_out (abfd, rel, loc);
13595
}
13596
 
13597
/* Append a REL relocation REL to section S in BFD.  */
13598
 
13599
void
13600
elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13601
{
13602
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13603
  bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13604
  BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
13605
  bed->s->swap_reloc_out (abfd, rel, loc);
13606
}