1 /* $MirOS: src/gnu/usr.bin/binutils/bfd/elflink.c,v 1.5 2005/07/07 16:22:40 tg Exp $ */
2 
3 /* ELF linking support for BFD.
4    Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
5    Free Software Foundation, Inc.
6 
7    This file is part of BFD, the Binary File Descriptor library.
8 
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
22 
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #define ARCH_SIZE 0
28 #include "elf-bfd.h"
29 #include "safe-ctype.h"
30 #include "libiberty.h"
31 
32 __RCSID("$MirOS: src/gnu/usr.bin/binutils/bfd/elflink.c,v 1.5 2005/07/07 16:22:40 tg Exp $");
33 
34 bfd_boolean
_bfd_elf_create_got_section(bfd * abfd,struct bfd_link_info * info)35 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
36 {
37   flagword flags;
38   asection *s;
39   struct elf_link_hash_entry *h;
40   struct bfd_link_hash_entry *bh;
41   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
42   int ptralign;
43 
44   /* This function may be called more than once.  */
45   s = bfd_get_section_by_name (abfd, ".got");
46   if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
47     return TRUE;
48 
49   switch (bed->s->arch_size)
50     {
51     case 32:
52       ptralign = 2;
53       break;
54 
55     case 64:
56       ptralign = 3;
57       break;
58 
59     default:
60       bfd_set_error (bfd_error_bad_value);
61       return FALSE;
62     }
63 
64   flags = bed->dynamic_sec_flags;
65 
66   s = bfd_make_section_with_flags (abfd, ".got", flags);
67   if (s == NULL
68       || !bfd_set_section_alignment (abfd, s, ptralign))
69     return FALSE;
70 
71   if (bed->want_got_plt)
72     {
73       s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
74       if (s == NULL
75 	  || !bfd_set_section_alignment (abfd, s, ptralign))
76 	return FALSE;
77     }
78 
79   if (bed->want_got_sym)
80     {
81       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
82 	 (or .got.plt) section.  We don't do this in the linker script
83 	 because we don't want to define the symbol if we are not creating
84 	 a global offset table.  */
85       bh = NULL;
86       if (!(_bfd_generic_link_add_one_symbol
87 	    (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
88 	     0, NULL, FALSE, bed->collect, &bh)))
89 	return FALSE;
90       h = (struct elf_link_hash_entry *) bh;
91       h->def_regular = 1;
92       h->type = STT_OBJECT;
93       h->other = STV_HIDDEN;
94 
95       if (! info->executable
96 	  && ! bfd_elf_link_record_dynamic_symbol (info, h))
97 	return FALSE;
98 
99       elf_hash_table (info)->hgot = h;
100     }
101 
102   /* The first bit of the global offset table is the header.  */
103   s->size += bed->got_header_size;
104 
105   return TRUE;
106 }
107 
108 /* Create a strtab to hold the dynamic symbol names.  */
109 static bfd_boolean
_bfd_elf_link_create_dynstrtab(bfd * abfd,struct bfd_link_info * info)110 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
111 {
112   struct elf_link_hash_table *hash_table;
113 
114   hash_table = elf_hash_table (info);
115   if (hash_table->dynobj == NULL)
116     hash_table->dynobj = abfd;
117 
118   if (hash_table->dynstr == NULL)
119     {
120       hash_table->dynstr = _bfd_elf_strtab_init ();
121       if (hash_table->dynstr == NULL)
122 	return FALSE;
123     }
124   return TRUE;
125 }
126 
127 /* Create some sections which will be filled in with dynamic linking
128    information.  ABFD is an input file which requires dynamic sections
129    to be created.  The dynamic sections take up virtual memory space
130    when the final executable is run, so we need to create them before
131    addresses are assigned to the output sections.  We work out the
132    actual contents and size of these sections later.  */
133 
134 bfd_boolean
_bfd_elf_link_create_dynamic_sections(bfd * abfd,struct bfd_link_info * info)135 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
136 {
137   flagword flags;
138   register asection *s;
139   struct elf_link_hash_entry *h;
140   struct bfd_link_hash_entry *bh;
141   const struct elf_backend_data *bed;
142 
143   if (! is_elf_hash_table (info->hash))
144     return FALSE;
145 
146   if (elf_hash_table (info)->dynamic_sections_created)
147     return TRUE;
148 
149   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
150     return FALSE;
151 
152   abfd = elf_hash_table (info)->dynobj;
153   bed = get_elf_backend_data (abfd);
154 
155   flags = bed->dynamic_sec_flags;
156 
157   /* A dynamically linked executable has a .interp section, but a
158      shared library does not.  */
159   if (info->executable)
160     {
161       s = bfd_make_section_with_flags (abfd, ".interp",
162 				       flags | SEC_READONLY);
163       if (s == NULL)
164 	return FALSE;
165     }
166 
167   if (! info->traditional_format)
168     {
169       s = bfd_make_section_with_flags (abfd, ".eh_frame_hdr",
170 				       flags | SEC_READONLY);
171       if (s == NULL
172 	  || ! bfd_set_section_alignment (abfd, s, 2))
173 	return FALSE;
174       elf_hash_table (info)->eh_info.hdr_sec = s;
175     }
176 
177   /* Create sections to hold version informations.  These are removed
178      if they are not needed.  */
179   s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
180 				   flags | SEC_READONLY);
181   if (s == NULL
182       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
183     return FALSE;
184 
185   s = bfd_make_section_with_flags (abfd, ".gnu.version",
186 				   flags | SEC_READONLY);
187   if (s == NULL
188       || ! bfd_set_section_alignment (abfd, s, 1))
189     return FALSE;
190 
191   s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
192 				   flags | SEC_READONLY);
193   if (s == NULL
194       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
195     return FALSE;
196 
197   s = bfd_make_section_with_flags (abfd, ".dynsym",
198 				   flags | SEC_READONLY);
199   if (s == NULL
200       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
201     return FALSE;
202 
203   s = bfd_make_section_with_flags (abfd, ".dynstr",
204 				   flags | SEC_READONLY);
205   if (s == NULL)
206     return FALSE;
207 
208   s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
209   if (s == NULL
210       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
211     return FALSE;
212 
213   /* The special symbol _DYNAMIC is always set to the start of the
214      .dynamic section.  We could set _DYNAMIC in a linker script, but we
215      only want to define it if we are, in fact, creating a .dynamic
216      section.  We don't want to define it if there is no .dynamic
217      section, since on some ELF platforms the start up code examines it
218      to decide how to initialize the process.  */
219   h = elf_link_hash_lookup (elf_hash_table (info), "_DYNAMIC",
220 			    FALSE, FALSE, FALSE);
221   if (h != NULL)
222     {
223       /* Zap symbol defined in an as-needed lib that wasn't linked.
224 	 This is a symptom of a larger problem:  Absolute symbols
225 	 defined in shared libraries can't be overridden, because we
226 	 lose the link to the bfd which is via the symbol section.  */
227       h->root.type = bfd_link_hash_new;
228     }
229   bh = &h->root;
230   if (! (_bfd_generic_link_add_one_symbol
231 	 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE,
232 	  get_elf_backend_data (abfd)->collect, &bh)))
233     return FALSE;
234   h = (struct elf_link_hash_entry *) bh;
235   h->def_regular = 1;
236   h->type = STT_OBJECT;
237 
238   if (! info->executable
239       && ! bfd_elf_link_record_dynamic_symbol (info, h))
240     return FALSE;
241 
242   s = bfd_make_section_with_flags (abfd, ".hash",
243 				   flags | SEC_READONLY);
244   if (s == NULL
245       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
246     return FALSE;
247   elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
248 
249   /* Let the backend create the rest of the sections.  This lets the
250      backend set the right flags.  The backend will normally create
251      the .got and .plt sections.  */
252   if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
253     return FALSE;
254 
255   elf_hash_table (info)->dynamic_sections_created = TRUE;
256 
257   return TRUE;
258 }
259 
260 /* Create dynamic sections when linking against a dynamic object.  */
261 
262 bfd_boolean
_bfd_elf_create_dynamic_sections(bfd * abfd,struct bfd_link_info * info)263 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
264 {
265   flagword flags, pltflags;
266   asection *s;
267   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
268 
269   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
270      .rel[a].bss sections.  */
271   flags = bed->dynamic_sec_flags;
272 
273   pltflags = flags;
274   if (bed->plt_not_loaded)
275     /* We do not clear SEC_ALLOC here because we still want the OS to
276        allocate space for the section; it's just that there's nothing
277        to read in from the object file.  */
278     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
279   else
280     pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
281   if (bed->plt_readonly)
282     pltflags |= SEC_READONLY;
283 
284   s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
285   if (s == NULL
286       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
287     return FALSE;
288 
289   if (bed->want_plt_sym)
290     {
291       /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
292 	 .plt section.  */
293       struct elf_link_hash_entry *h;
294       struct bfd_link_hash_entry *bh = NULL;
295 
296       if (! (_bfd_generic_link_add_one_symbol
297 	     (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
298 	      FALSE, get_elf_backend_data (abfd)->collect, &bh)))
299 	return FALSE;
300       h = (struct elf_link_hash_entry *) bh;
301       h->def_regular = 1;
302       h->type = STT_OBJECT;
303 
304       if (! info->executable
305 	  && ! bfd_elf_link_record_dynamic_symbol (info, h))
306 	return FALSE;
307     }
308 
309   s = bfd_make_section_with_flags (abfd,
310 				   (bed->default_use_rela_p
311 				    ? ".rela.plt" : ".rel.plt"),
312 				   flags | SEC_READONLY);
313   if (s == NULL
314       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
315     return FALSE;
316 
317   if (! _bfd_elf_create_got_section (abfd, info))
318     return FALSE;
319 
320   if (bed->want_dynbss)
321     {
322       /* The .dynbss section is a place to put symbols which are defined
323 	 by dynamic objects, are referenced by regular objects, and are
324 	 not functions.  We must allocate space for them in the process
325 	 image and use a R_*_COPY reloc to tell the dynamic linker to
326 	 initialize them at run time.  The linker script puts the .dynbss
327 	 section into the .bss section of the final image.  */
328       s = bfd_make_section_with_flags (abfd, ".dynbss",
329 				       (SEC_ALLOC
330 					| SEC_LINKER_CREATED));
331       if (s == NULL)
332 	return FALSE;
333 
334       /* The .rel[a].bss section holds copy relocs.  This section is not
335 	 normally needed.  We need to create it here, though, so that the
336 	 linker will map it to an output section.  We can't just create it
337 	 only if we need it, because we will not know whether we need it
338 	 until we have seen all the input files, and the first time the
339 	 main linker code calls BFD after examining all the input files
340 	 (size_dynamic_sections) the input sections have already been
341 	 mapped to the output sections.  If the section turns out not to
342 	 be needed, we can discard it later.  We will never need this
343 	 section when generating a shared object, since they do not use
344 	 copy relocs.  */
345       if (! info->shared)
346 	{
347 	  s = bfd_make_section_with_flags (abfd,
348 					   (bed->default_use_rela_p
349 					    ? ".rela.bss" : ".rel.bss"),
350 					   flags | SEC_READONLY);
351 	  if (s == NULL
352 	      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
353 	    return FALSE;
354 	}
355     }
356 
357   return TRUE;
358 }
359 
360 /* Record a new dynamic symbol.  We record the dynamic symbols as we
361    read the input files, since we need to have a list of all of them
362    before we can determine the final sizes of the output sections.
363    Note that we may actually call this function even though we are not
364    going to output any dynamic symbols; in some cases we know that a
365    symbol should be in the dynamic symbol table, but only if there is
366    one.  */
367 
368 bfd_boolean
bfd_elf_link_record_dynamic_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * h)369 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
370 				    struct elf_link_hash_entry *h)
371 {
372   if (h->dynindx == -1)
373     {
374       struct elf_strtab_hash *dynstr;
375       char *p;
376       const char *name;
377       bfd_size_type indx;
378 
379       /* XXX: The ABI draft says the linker must turn hidden and
380 	 internal symbols into STB_LOCAL symbols when producing the
381 	 DSO. However, if ld.so honors st_other in the dynamic table,
382 	 this would not be necessary.  */
383       switch (ELF_ST_VISIBILITY (h->other))
384 	{
385 	case STV_INTERNAL:
386 	case STV_HIDDEN:
387 	  if (h->root.type != bfd_link_hash_undefined
388 	      && h->root.type != bfd_link_hash_undefweak)
389 	    {
390 	      h->forced_local = 1;
391 	      if (!elf_hash_table (info)->is_relocatable_executable)
392 		return TRUE;
393 	    }
394 
395 	default:
396 	  break;
397 	}
398 
399       h->dynindx = elf_hash_table (info)->dynsymcount;
400       ++elf_hash_table (info)->dynsymcount;
401 
402       dynstr = elf_hash_table (info)->dynstr;
403       if (dynstr == NULL)
404 	{
405 	  /* Create a strtab to hold the dynamic symbol names.  */
406 	  elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
407 	  if (dynstr == NULL)
408 	    return FALSE;
409 	}
410 
411       /* We don't put any version information in the dynamic string
412 	 table.  */
413       name = h->root.root.string;
414       p = strchr (name, ELF_VER_CHR);
415       if (p != NULL)
416 	/* We know that the p points into writable memory.  In fact,
417 	   there are only a few symbols that have read-only names, being
418 	   those like _GLOBAL_OFFSET_TABLE_ that are created specially
419 	   by the backends.  Most symbols will have names pointing into
420 	   an ELF string table read from a file, or to objalloc memory.  */
421 	*p = 0;
422 
423       indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
424 
425       if (p != NULL)
426 	*p = ELF_VER_CHR;
427 
428       if (indx == (bfd_size_type) -1)
429 	return FALSE;
430       h->dynstr_index = indx;
431     }
432 
433   return TRUE;
434 }
435 
436 /* Record an assignment to a symbol made by a linker script.  We need
437    this in case some dynamic object refers to this symbol.  */
438 
439 bfd_boolean
bfd_elf_record_link_assignment(bfd * output_bfd ATTRIBUTE_UNUSED,struct bfd_link_info * info,const char * name,bfd_boolean provide)440 bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED,
441 				struct bfd_link_info *info,
442 				const char *name,
443 				bfd_boolean provide)
444 {
445   struct elf_link_hash_entry *h;
446   struct elf_link_hash_table *htab;
447 
448   if (!is_elf_hash_table (info->hash))
449     return TRUE;
450 
451   htab = elf_hash_table (info);
452   h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
453   if (h == NULL)
454     return provide;
455 
456   /* Since we're defining the symbol, don't let it seem to have not
457      been defined.  record_dynamic_symbol and size_dynamic_sections
458      may depend on this.  */
459   if (h->root.type == bfd_link_hash_undefweak
460       || h->root.type == bfd_link_hash_undefined)
461     {
462       h->root.type = bfd_link_hash_new;
463       if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
464 	bfd_link_repair_undef_list (&htab->root);
465     }
466 
467   if (h->root.type == bfd_link_hash_new)
468     h->non_elf = 0;
469 
470   /* If this symbol is being provided by the linker script, and it is
471      currently defined by a dynamic object, but not by a regular
472      object, then mark it as undefined so that the generic linker will
473      force the correct value.  */
474   if (provide
475       && h->def_dynamic
476       && !h->def_regular)
477     h->root.type = bfd_link_hash_undefined;
478 
479   /* If this symbol is not being provided by the linker script, and it is
480      currently defined by a dynamic object, but not by a regular object,
481      then clear out any version information because the symbol will not be
482      associated with the dynamic object any more.  */
483   if (!provide
484       && h->def_dynamic
485       && !h->def_regular)
486     h->verinfo.verdef = NULL;
487 
488   h->def_regular = 1;
489 
490   /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
491      and executables.  */
492   if (!info->relocatable
493       && h->dynindx != -1
494       && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
495 	  || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
496     h->forced_local = 1;
497 
498   if ((h->def_dynamic
499        || h->ref_dynamic
500        || info->shared
501        || (info->executable && elf_hash_table (info)->is_relocatable_executable))
502       && h->dynindx == -1)
503     {
504       if (! bfd_elf_link_record_dynamic_symbol (info, h))
505 	return FALSE;
506 
507       /* If this is a weak defined symbol, and we know a corresponding
508 	 real symbol from the same dynamic object, make sure the real
509 	 symbol is also made into a dynamic symbol.  */
510       if (h->u.weakdef != NULL
511 	  && h->u.weakdef->dynindx == -1)
512 	{
513 	  if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
514 	    return FALSE;
515 	}
516     }
517 
518   return TRUE;
519 }
520 
521 /* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
522    success, and 2 on a failure caused by attempting to record a symbol
523    in a discarded section, eg. a discarded link-once section symbol.  */
524 
525 int
bfd_elf_link_record_local_dynamic_symbol(struct bfd_link_info * info,bfd * input_bfd,long input_indx)526 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
527 					  bfd *input_bfd,
528 					  long input_indx)
529 {
530   bfd_size_type amt;
531   struct elf_link_local_dynamic_entry *entry;
532   struct elf_link_hash_table *eht;
533   struct elf_strtab_hash *dynstr;
534   unsigned long dynstr_index;
535   char *name;
536   Elf_External_Sym_Shndx eshndx;
537   char esym[sizeof (Elf64_External_Sym)];
538 
539   if (! is_elf_hash_table (info->hash))
540     return 0;
541 
542   /* See if the entry exists already.  */
543   for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
544     if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
545       return 1;
546 
547   amt = sizeof (*entry);
548   entry = bfd_alloc (input_bfd, amt);
549   if (entry == NULL)
550     return 0;
551 
552   /* Go find the symbol, so that we can find it's name.  */
553   if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
554 			     1, input_indx, &entry->isym, esym, &eshndx))
555     {
556       bfd_release (input_bfd, entry);
557       return 0;
558     }
559 
560   if (entry->isym.st_shndx != SHN_UNDEF
561       && (entry->isym.st_shndx < SHN_LORESERVE
562 	  || entry->isym.st_shndx > SHN_HIRESERVE))
563     {
564       asection *s;
565 
566       s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
567       if (s == NULL || bfd_is_abs_section (s->output_section))
568 	{
569 	  /* We can still bfd_release here as nothing has done another
570 	     bfd_alloc.  We can't do this later in this function.  */
571 	  bfd_release (input_bfd, entry);
572 	  return 2;
573 	}
574     }
575 
576   name = (bfd_elf_string_from_elf_section
577 	  (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
578 	   entry->isym.st_name));
579 
580   dynstr = elf_hash_table (info)->dynstr;
581   if (dynstr == NULL)
582     {
583       /* Create a strtab to hold the dynamic symbol names.  */
584       elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
585       if (dynstr == NULL)
586 	return 0;
587     }
588 
589   dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
590   if (dynstr_index == (unsigned long) -1)
591     return 0;
592   entry->isym.st_name = dynstr_index;
593 
594   eht = elf_hash_table (info);
595 
596   entry->next = eht->dynlocal;
597   eht->dynlocal = entry;
598   entry->input_bfd = input_bfd;
599   entry->input_indx = input_indx;
600   eht->dynsymcount++;
601 
602   /* Whatever binding the symbol had before, it's now local.  */
603   entry->isym.st_info
604     = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
605 
606   /* The dynindx will be set at the end of size_dynamic_sections.  */
607 
608   return 1;
609 }
610 
611 /* Return the dynindex of a local dynamic symbol.  */
612 
613 long
_bfd_elf_link_lookup_local_dynindx(struct bfd_link_info * info,bfd * input_bfd,long input_indx)614 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
615 				    bfd *input_bfd,
616 				    long input_indx)
617 {
618   struct elf_link_local_dynamic_entry *e;
619 
620   for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
621     if (e->input_bfd == input_bfd && e->input_indx == input_indx)
622       return e->dynindx;
623   return -1;
624 }
625 
626 /* This function is used to renumber the dynamic symbols, if some of
627    them are removed because they are marked as local.  This is called
628    via elf_link_hash_traverse.  */
629 
630 static bfd_boolean
elf_link_renumber_hash_table_dynsyms(struct elf_link_hash_entry * h,void * data)631 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
632 				      void *data)
633 {
634   size_t *count = data;
635 
636   if (h->root.type == bfd_link_hash_warning)
637     h = (struct elf_link_hash_entry *) h->root.u.i.link;
638 
639   if (h->forced_local)
640     return TRUE;
641 
642   if (h->dynindx != -1)
643     h->dynindx = ++(*count);
644 
645   return TRUE;
646 }
647 
648 
649 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
650    STB_LOCAL binding.  */
651 
652 static bfd_boolean
elf_link_renumber_local_hash_table_dynsyms(struct elf_link_hash_entry * h,void * data)653 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
654 					    void *data)
655 {
656   size_t *count = data;
657 
658   if (h->root.type == bfd_link_hash_warning)
659     h = (struct elf_link_hash_entry *) h->root.u.i.link;
660 
661   if (!h->forced_local)
662     return TRUE;
663 
664   if (h->dynindx != -1)
665     h->dynindx = ++(*count);
666 
667   return TRUE;
668 }
669 
670 /* Return true if the dynamic symbol for a given section should be
671    omitted when creating a shared library.  */
672 bfd_boolean
_bfd_elf_link_omit_section_dynsym(bfd * output_bfd ATTRIBUTE_UNUSED,struct bfd_link_info * info,asection * p)673 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
674 				   struct bfd_link_info *info,
675 				   asection *p)
676 {
677   switch (elf_section_data (p)->this_hdr.sh_type)
678     {
679     case SHT_PROGBITS:
680     case SHT_NOBITS:
681       /* If sh_type is yet undecided, assume it could be
682 	 SHT_PROGBITS/SHT_NOBITS.  */
683     case SHT_NULL:
684       if (strcmp (p->name, ".got") == 0
685 	  || strcmp (p->name, ".got.plt") == 0
686 	  || strcmp (p->name, ".plt") == 0)
687 	{
688 	  asection *ip;
689 	  bfd *dynobj = elf_hash_table (info)->dynobj;
690 
691 	  if (dynobj != NULL
692 	      && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL
693 	      && (ip->flags & SEC_LINKER_CREATED)
694 	      && ip->output_section == p)
695 	    return TRUE;
696 	}
697       return FALSE;
698 
699       /* There shouldn't be section relative relocations
700 	 against any other section.  */
701     default:
702       return TRUE;
703     }
704 }
705 
706 /* Assign dynsym indices.  In a shared library we generate a section
707    symbol for each output section, which come first.  Next come symbols
708    which have been forced to local binding.  Then all of the back-end
709    allocated local dynamic syms, followed by the rest of the global
710    symbols.  */
711 
712 static unsigned long
_bfd_elf_link_renumber_dynsyms(bfd * output_bfd,struct bfd_link_info * info,unsigned long * section_sym_count)713 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
714 				struct bfd_link_info *info,
715 				unsigned long *section_sym_count)
716 {
717   unsigned long dynsymcount = 0;
718 
719   if (info->shared || elf_hash_table (info)->is_relocatable_executable)
720     {
721       const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
722       asection *p;
723       for (p = output_bfd->sections; p ; p = p->next)
724 	if ((p->flags & SEC_EXCLUDE) == 0
725 	    && (p->flags & SEC_ALLOC) != 0
726 	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
727 	  elf_section_data (p)->dynindx = ++dynsymcount;
728     }
729   *section_sym_count = dynsymcount;
730 
731   elf_link_hash_traverse (elf_hash_table (info),
732 			  elf_link_renumber_local_hash_table_dynsyms,
733 			  &dynsymcount);
734 
735   if (elf_hash_table (info)->dynlocal)
736     {
737       struct elf_link_local_dynamic_entry *p;
738       for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
739 	p->dynindx = ++dynsymcount;
740     }
741 
742   elf_link_hash_traverse (elf_hash_table (info),
743 			  elf_link_renumber_hash_table_dynsyms,
744 			  &dynsymcount);
745 
746   /* There is an unused NULL entry at the head of the table which
747      we must account for in our count.  Unless there weren't any
748      symbols, which means we'll have no table at all.  */
749   if (dynsymcount != 0)
750     ++dynsymcount;
751 
752   return elf_hash_table (info)->dynsymcount = dynsymcount;
753 }
754 
755 /* This function is called when we want to define a new symbol.  It
756    handles the various cases which arise when we find a definition in
757    a dynamic object, or when there is already a definition in a
758    dynamic object.  The new symbol is described by NAME, SYM, PSEC,
759    and PVALUE.  We set SYM_HASH to the hash table entry.  We set
760    OVERRIDE if the old symbol is overriding a new definition.  We set
761    TYPE_CHANGE_OK if it is OK for the type to change.  We set
762    SIZE_CHANGE_OK if it is OK for the size to change.  By OK to
763    change, we mean that we shouldn't warn if the type or size does
764    change.  We set POLD_ALIGNMENT if an old common symbol in a dynamic
765    object is overridden by a regular object.  */
766 
767 bfd_boolean
_bfd_elf_merge_symbol(bfd * abfd,struct bfd_link_info * info,const char * name,Elf_Internal_Sym * sym,asection ** psec,bfd_vma * pvalue,unsigned int * pold_alignment,struct elf_link_hash_entry ** sym_hash,bfd_boolean * skip,bfd_boolean * override,bfd_boolean * type_change_ok,bfd_boolean * size_change_ok)768 _bfd_elf_merge_symbol (bfd *abfd,
769 		       struct bfd_link_info *info,
770 		       const char *name,
771 		       Elf_Internal_Sym *sym,
772 		       asection **psec,
773 		       bfd_vma *pvalue,
774 		       unsigned int *pold_alignment,
775 		       struct elf_link_hash_entry **sym_hash,
776 		       bfd_boolean *skip,
777 		       bfd_boolean *override,
778 		       bfd_boolean *type_change_ok,
779 		       bfd_boolean *size_change_ok)
780 {
781   asection *sec, *oldsec;
782   struct elf_link_hash_entry *h;
783   struct elf_link_hash_entry *flip;
784   int bind;
785   bfd *oldbfd;
786   bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
787   bfd_boolean newweak, oldweak;
788 
789   *skip = FALSE;
790   *override = FALSE;
791 
792   sec = *psec;
793   bind = ELF_ST_BIND (sym->st_info);
794 
795   if (! bfd_is_und_section (sec))
796     h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
797   else
798     h = ((struct elf_link_hash_entry *)
799 	 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
800   if (h == NULL)
801     return FALSE;
802   *sym_hash = h;
803 
804   /* This code is for coping with dynamic objects, and is only useful
805      if we are doing an ELF link.  */
806   if (info->hash->creator != abfd->xvec)
807     return TRUE;
808 
809   /* For merging, we only care about real symbols.  */
810 
811   while (h->root.type == bfd_link_hash_indirect
812 	 || h->root.type == bfd_link_hash_warning)
813     h = (struct elf_link_hash_entry *) h->root.u.i.link;
814 
815   /* If we just created the symbol, mark it as being an ELF symbol.
816      Other than that, there is nothing to do--there is no merge issue
817      with a newly defined symbol--so we just return.  */
818 
819   if (h->root.type == bfd_link_hash_new)
820     {
821       h->non_elf = 0;
822       return TRUE;
823     }
824 
825   /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
826      existing symbol.  */
827 
828   switch (h->root.type)
829     {
830     default:
831       oldbfd = NULL;
832       oldsec = NULL;
833       break;
834 
835     case bfd_link_hash_undefined:
836     case bfd_link_hash_undefweak:
837       oldbfd = h->root.u.undef.abfd;
838       oldsec = NULL;
839       break;
840 
841     case bfd_link_hash_defined:
842     case bfd_link_hash_defweak:
843       oldbfd = h->root.u.def.section->owner;
844       oldsec = h->root.u.def.section;
845       break;
846 
847     case bfd_link_hash_common:
848       oldbfd = h->root.u.c.p->section->owner;
849       oldsec = h->root.u.c.p->section;
850       break;
851     }
852 
853   /* In cases involving weak versioned symbols, we may wind up trying
854      to merge a symbol with itself.  Catch that here, to avoid the
855      confusion that results if we try to override a symbol with
856      itself.  The additional tests catch cases like
857      _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
858      dynamic object, which we do want to handle here.  */
859   if (abfd == oldbfd
860       && ((abfd->flags & DYNAMIC) == 0
861 	  || !h->def_regular))
862     return TRUE;
863 
864   /* NEWDYN and OLDDYN indicate whether the new or old symbol,
865      respectively, is from a dynamic object.  */
866 
867   if ((abfd->flags & DYNAMIC) != 0)
868     newdyn = TRUE;
869   else
870     newdyn = FALSE;
871 
872   if (oldbfd != NULL)
873     olddyn = (oldbfd->flags & DYNAMIC) != 0;
874   else
875     {
876       asection *hsec;
877 
878       /* This code handles the special SHN_MIPS_{TEXT,DATA} section
879 	 indices used by MIPS ELF.  */
880       switch (h->root.type)
881 	{
882 	default:
883 	  hsec = NULL;
884 	  break;
885 
886 	case bfd_link_hash_defined:
887 	case bfd_link_hash_defweak:
888 	  hsec = h->root.u.def.section;
889 	  break;
890 
891 	case bfd_link_hash_common:
892 	  hsec = h->root.u.c.p->section;
893 	  break;
894 	}
895 
896       if (hsec == NULL)
897 	olddyn = FALSE;
898       else
899 	olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
900     }
901 
902   /* NEWDEF and OLDDEF indicate whether the new or old symbol,
903      respectively, appear to be a definition rather than reference.  */
904 
905   if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
906     newdef = FALSE;
907   else
908     newdef = TRUE;
909 
910   if (h->root.type == bfd_link_hash_undefined
911       || h->root.type == bfd_link_hash_undefweak
912       || h->root.type == bfd_link_hash_common)
913     olddef = FALSE;
914   else
915     olddef = TRUE;
916 
917   /* Check TLS symbol.  */
918   if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
919       && ELF_ST_TYPE (sym->st_info) != h->type)
920     {
921       bfd *ntbfd, *tbfd;
922       bfd_boolean ntdef, tdef;
923       asection *ntsec, *tsec;
924 
925       if (h->type == STT_TLS)
926 	{
927 	  ntbfd = abfd;
928 	  ntsec = sec;
929 	  ntdef = newdef;
930 	  tbfd = oldbfd;
931 	  tsec = oldsec;
932 	  tdef = olddef;
933 	}
934       else
935 	{
936 	  ntbfd = oldbfd;
937 	  ntsec = oldsec;
938 	  ntdef = olddef;
939 	  tbfd = abfd;
940 	  tsec = sec;
941 	  tdef = newdef;
942 	}
943 
944       if (tdef && ntdef)
945 	(*_bfd_error_handler)
946 	  (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
947 	   tbfd, tsec, ntbfd, ntsec, h->root.root.string);
948       else if (!tdef && !ntdef)
949 	(*_bfd_error_handler)
950 	  (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
951 	   tbfd, ntbfd, h->root.root.string);
952       else if (tdef)
953 	(*_bfd_error_handler)
954 	  (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
955 	   tbfd, tsec, ntbfd, h->root.root.string);
956       else
957 	(*_bfd_error_handler)
958 	  (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
959 	   tbfd, ntbfd, ntsec, h->root.root.string);
960 
961       bfd_set_error (bfd_error_bad_value);
962       return FALSE;
963     }
964 
965   /* We need to remember if a symbol has a definition in a dynamic
966      object or is weak in all dynamic objects. Internal and hidden
967      visibility will make it unavailable to dynamic objects.  */
968   if (newdyn && !h->dynamic_def)
969     {
970       if (!bfd_is_und_section (sec))
971 	h->dynamic_def = 1;
972       else
973 	{
974 	  /* Check if this symbol is weak in all dynamic objects. If it
975 	     is the first time we see it in a dynamic object, we mark
976 	     if it is weak. Otherwise, we clear it.  */
977 	  if (!h->ref_dynamic)
978 	    {
979 	      if (bind == STB_WEAK)
980 		h->dynamic_weak = 1;
981 	    }
982 	  else if (bind != STB_WEAK)
983 	    h->dynamic_weak = 0;
984 	}
985     }
986 
987   /* If the old symbol has non-default visibility, we ignore the new
988      definition from a dynamic object.  */
989   if (newdyn
990       && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
991       && !bfd_is_und_section (sec))
992     {
993       *skip = TRUE;
994       /* Make sure this symbol is dynamic.  */
995       h->ref_dynamic = 1;
996       /* A protected symbol has external availability. Make sure it is
997 	 recorded as dynamic.
998 
999 	 FIXME: Should we check type and size for protected symbol?  */
1000       if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1001 	return bfd_elf_link_record_dynamic_symbol (info, h);
1002       else
1003 	return TRUE;
1004     }
1005   else if (!newdyn
1006 	   && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1007 	   && h->def_dynamic)
1008     {
1009       /* If the new symbol with non-default visibility comes from a
1010 	 relocatable file and the old definition comes from a dynamic
1011 	 object, we remove the old definition.  */
1012       if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1013 	h = *sym_hash;
1014 
1015       if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1016 	  && bfd_is_und_section (sec))
1017 	{
1018 	  /* If the new symbol is undefined and the old symbol was
1019 	     also undefined before, we need to make sure
1020 	     _bfd_generic_link_add_one_symbol doesn't mess
1021 	     up the linker hash table undefs list.  Since the old
1022 	     definition came from a dynamic object, it is still on the
1023 	     undefs list.  */
1024 	  h->root.type = bfd_link_hash_undefined;
1025 	  h->root.u.undef.abfd = abfd;
1026 	}
1027       else
1028 	{
1029 	  h->root.type = bfd_link_hash_new;
1030 	  h->root.u.undef.abfd = NULL;
1031 	}
1032 
1033       if (h->def_dynamic)
1034 	{
1035 	  h->def_dynamic = 0;
1036 	  h->ref_dynamic = 1;
1037 	  h->dynamic_def = 1;
1038 	}
1039       /* FIXME: Should we check type and size for protected symbol?  */
1040       h->size = 0;
1041       h->type = 0;
1042       return TRUE;
1043     }
1044 
1045   /* Differentiate strong and weak symbols.  */
1046   newweak = bind == STB_WEAK;
1047   oldweak = (h->root.type == bfd_link_hash_defweak
1048 	     || h->root.type == bfd_link_hash_undefweak);
1049 
1050   /* If a new weak symbol definition comes from a regular file and the
1051      old symbol comes from a dynamic library, we treat the new one as
1052      strong.  Similarly, an old weak symbol definition from a regular
1053      file is treated as strong when the new symbol comes from a dynamic
1054      library.  Further, an old weak symbol from a dynamic library is
1055      treated as strong if the new symbol is from a dynamic library.
1056      This reflects the way glibc's ld.so works.
1057 
1058      Do this before setting *type_change_ok or *size_change_ok so that
1059      we warn properly when dynamic library symbols are overridden.  */
1060 
1061   if (newdef && !newdyn && olddyn)
1062     newweak = FALSE;
1063   if (olddef && newdyn)
1064     oldweak = FALSE;
1065 
1066   /* It's OK to change the type if either the existing symbol or the
1067      new symbol is weak.  A type change is also OK if the old symbol
1068      is undefined and the new symbol is defined.  */
1069 
1070   if (oldweak
1071       || newweak
1072       || (newdef
1073 	  && h->root.type == bfd_link_hash_undefined))
1074     *type_change_ok = TRUE;
1075 
1076   /* It's OK to change the size if either the existing symbol or the
1077      new symbol is weak, or if the old symbol is undefined.  */
1078 
1079   if (*type_change_ok
1080       || h->root.type == bfd_link_hash_undefined)
1081     *size_change_ok = TRUE;
1082 
1083   /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1084      symbol, respectively, appears to be a common symbol in a dynamic
1085      object.  If a symbol appears in an uninitialized section, and is
1086      not weak, and is not a function, then it may be a common symbol
1087      which was resolved when the dynamic object was created.  We want
1088      to treat such symbols specially, because they raise special
1089      considerations when setting the symbol size: if the symbol
1090      appears as a common symbol in a regular object, and the size in
1091      the regular object is larger, we must make sure that we use the
1092      larger size.  This problematic case can always be avoided in C,
1093      but it must be handled correctly when using Fortran shared
1094      libraries.
1095 
1096      Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1097      likewise for OLDDYNCOMMON and OLDDEF.
1098 
1099      Note that this test is just a heuristic, and that it is quite
1100      possible to have an uninitialized symbol in a shared object which
1101      is really a definition, rather than a common symbol.  This could
1102      lead to some minor confusion when the symbol really is a common
1103      symbol in some regular object.  However, I think it will be
1104      harmless.  */
1105 
1106   if (newdyn
1107       && newdef
1108       && !newweak
1109       && (sec->flags & SEC_ALLOC) != 0
1110       && (sec->flags & SEC_LOAD) == 0
1111       && sym->st_size > 0
1112       && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1113     newdyncommon = TRUE;
1114   else
1115     newdyncommon = FALSE;
1116 
1117   if (olddyn
1118       && olddef
1119       && h->root.type == bfd_link_hash_defined
1120       && h->def_dynamic
1121       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1122       && (h->root.u.def.section->flags & SEC_LOAD) == 0
1123       && h->size > 0
1124       && h->type != STT_FUNC)
1125     olddyncommon = TRUE;
1126   else
1127     olddyncommon = FALSE;
1128 
1129   /* If both the old and the new symbols look like common symbols in a
1130      dynamic object, set the size of the symbol to the larger of the
1131      two.  */
1132 
1133   if (olddyncommon
1134       && newdyncommon
1135       && sym->st_size != h->size)
1136     {
1137       /* Since we think we have two common symbols, issue a multiple
1138 	 common warning if desired.  Note that we only warn if the
1139 	 size is different.  If the size is the same, we simply let
1140 	 the old symbol override the new one as normally happens with
1141 	 symbols defined in dynamic objects.  */
1142 
1143       if (! ((*info->callbacks->multiple_common)
1144 	     (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1145 	      h->size, abfd, bfd_link_hash_common, sym->st_size)))
1146 	return FALSE;
1147 
1148       if (sym->st_size > h->size)
1149 	h->size = sym->st_size;
1150 
1151       *size_change_ok = TRUE;
1152     }
1153 
1154   /* If we are looking at a dynamic object, and we have found a
1155      definition, we need to see if the symbol was already defined by
1156      some other object.  If so, we want to use the existing
1157      definition, and we do not want to report a multiple symbol
1158      definition error; we do this by clobbering *PSEC to be
1159      bfd_und_section_ptr.
1160 
1161      We treat a common symbol as a definition if the symbol in the
1162      shared library is a function, since common symbols always
1163      represent variables; this can cause confusion in principle, but
1164      any such confusion would seem to indicate an erroneous program or
1165      shared library.  We also permit a common symbol in a regular
1166      object to override a weak symbol in a shared object.  */
1167 
1168   if (newdyn
1169       && newdef
1170       && (olddef
1171 	  || (h->root.type == bfd_link_hash_common
1172 	      && (newweak
1173 		  || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1174     {
1175       *override = TRUE;
1176       newdef = FALSE;
1177       newdyncommon = FALSE;
1178 
1179       *psec = sec = bfd_und_section_ptr;
1180       *size_change_ok = TRUE;
1181 
1182       /* If we get here when the old symbol is a common symbol, then
1183 	 we are explicitly letting it override a weak symbol or
1184 	 function in a dynamic object, and we don't want to warn about
1185 	 a type change.  If the old symbol is a defined symbol, a type
1186 	 change warning may still be appropriate.  */
1187 
1188       if (h->root.type == bfd_link_hash_common)
1189 	*type_change_ok = TRUE;
1190     }
1191 
1192   /* Handle the special case of an old common symbol merging with a
1193      new symbol which looks like a common symbol in a shared object.
1194      We change *PSEC and *PVALUE to make the new symbol look like a
1195      common symbol, and let _bfd_generic_link_add_one_symbol do the
1196      right thing.  */
1197 
1198   if (newdyncommon
1199       && h->root.type == bfd_link_hash_common)
1200     {
1201       *override = TRUE;
1202       newdef = FALSE;
1203       newdyncommon = FALSE;
1204       *pvalue = sym->st_size;
1205       *psec = sec = bfd_com_section_ptr;
1206       *size_change_ok = TRUE;
1207     }
1208 
1209   /* Skip weak definitions of symbols that are already defined.  */
1210   if (newdef && olddef && newweak && !oldweak)
1211     *skip = TRUE;
1212 
1213   /* If the old symbol is from a dynamic object, and the new symbol is
1214      a definition which is not from a dynamic object, then the new
1215      symbol overrides the old symbol.  Symbols from regular files
1216      always take precedence over symbols from dynamic objects, even if
1217      they are defined after the dynamic object in the link.
1218 
1219      As above, we again permit a common symbol in a regular object to
1220      override a definition in a shared object if the shared object
1221      symbol is a function or is weak.  */
1222 
1223   flip = NULL;
1224   if (!newdyn
1225       && (newdef
1226 	  || (bfd_is_com_section (sec)
1227 	      && (oldweak
1228 		  || h->type == STT_FUNC)))
1229       && olddyn
1230       && olddef
1231       && h->def_dynamic)
1232     {
1233       /* Change the hash table entry to undefined, and let
1234 	 _bfd_generic_link_add_one_symbol do the right thing with the
1235 	 new definition.  */
1236 
1237       h->root.type = bfd_link_hash_undefined;
1238       h->root.u.undef.abfd = h->root.u.def.section->owner;
1239       *size_change_ok = TRUE;
1240 
1241       olddef = FALSE;
1242       olddyncommon = FALSE;
1243 
1244       /* We again permit a type change when a common symbol may be
1245 	 overriding a function.  */
1246 
1247       if (bfd_is_com_section (sec))
1248 	*type_change_ok = TRUE;
1249 
1250       if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1251 	flip = *sym_hash;
1252       else
1253 	/* This union may have been set to be non-NULL when this symbol
1254 	   was seen in a dynamic object.  We must force the union to be
1255 	   NULL, so that it is correct for a regular symbol.  */
1256 	h->verinfo.vertree = NULL;
1257     }
1258 
1259   /* Handle the special case of a new common symbol merging with an
1260      old symbol that looks like it might be a common symbol defined in
1261      a shared object.  Note that we have already handled the case in
1262      which a new common symbol should simply override the definition
1263      in the shared library.  */
1264 
1265   if (! newdyn
1266       && bfd_is_com_section (sec)
1267       && olddyncommon)
1268     {
1269       /* It would be best if we could set the hash table entry to a
1270 	 common symbol, but we don't know what to use for the section
1271 	 or the alignment.  */
1272       if (! ((*info->callbacks->multiple_common)
1273 	     (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1274 	      h->size, abfd, bfd_link_hash_common, sym->st_size)))
1275 	return FALSE;
1276 
1277       /* If the presumed common symbol in the dynamic object is
1278 	 larger, pretend that the new symbol has its size.  */
1279 
1280       if (h->size > *pvalue)
1281 	*pvalue = h->size;
1282 
1283       /* We need to remember the alignment required by the symbol
1284 	 in the dynamic object.  */
1285       BFD_ASSERT (pold_alignment);
1286       *pold_alignment = h->root.u.def.section->alignment_power;
1287 
1288       olddef = FALSE;
1289       olddyncommon = FALSE;
1290 
1291       h->root.type = bfd_link_hash_undefined;
1292       h->root.u.undef.abfd = h->root.u.def.section->owner;
1293 
1294       *size_change_ok = TRUE;
1295       *type_change_ok = TRUE;
1296 
1297       if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1298 	flip = *sym_hash;
1299       else
1300 	h->verinfo.vertree = NULL;
1301     }
1302 
1303   if (flip != NULL)
1304     {
1305       /* Handle the case where we had a versioned symbol in a dynamic
1306 	 library and now find a definition in a normal object.  In this
1307 	 case, we make the versioned symbol point to the normal one.  */
1308       const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1309       flip->root.type = h->root.type;
1310       h->root.type = bfd_link_hash_indirect;
1311       h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1312       (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1313       flip->root.u.undef.abfd = h->root.u.undef.abfd;
1314       if (h->def_dynamic)
1315 	{
1316 	  h->def_dynamic = 0;
1317 	  flip->ref_dynamic = 1;
1318 	}
1319     }
1320 
1321   return TRUE;
1322 }
1323 
1324 /* This function is called to create an indirect symbol from the
1325    default for the symbol with the default version if needed. The
1326    symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE.  We
1327    set DYNSYM if the new indirect symbol is dynamic.  */
1328 
1329 bfd_boolean
_bfd_elf_add_default_symbol(bfd * abfd,struct bfd_link_info * info,struct elf_link_hash_entry * h,const char * name,Elf_Internal_Sym * sym,asection ** psec,bfd_vma * value,bfd_boolean * dynsym,bfd_boolean override)1330 _bfd_elf_add_default_symbol (bfd *abfd,
1331 			     struct bfd_link_info *info,
1332 			     struct elf_link_hash_entry *h,
1333 			     const char *name,
1334 			     Elf_Internal_Sym *sym,
1335 			     asection **psec,
1336 			     bfd_vma *value,
1337 			     bfd_boolean *dynsym,
1338 			     bfd_boolean override)
1339 {
1340   bfd_boolean type_change_ok;
1341   bfd_boolean size_change_ok;
1342   bfd_boolean skip;
1343   char *shortname;
1344   struct elf_link_hash_entry *hi;
1345   struct bfd_link_hash_entry *bh;
1346   const struct elf_backend_data *bed;
1347   bfd_boolean collect;
1348   bfd_boolean dynamic;
1349   char *p;
1350   size_t len, shortlen;
1351   asection *sec;
1352 
1353   /* If this symbol has a version, and it is the default version, we
1354      create an indirect symbol from the default name to the fully
1355      decorated name.  This will cause external references which do not
1356      specify a version to be bound to this version of the symbol.  */
1357   p = strchr (name, ELF_VER_CHR);
1358   if (p == NULL || p[1] != ELF_VER_CHR)
1359     return TRUE;
1360 
1361   if (override)
1362     {
1363       /* We are overridden by an old definition. We need to check if we
1364 	 need to create the indirect symbol from the default name.  */
1365       hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1366 				 FALSE, FALSE);
1367       BFD_ASSERT (hi != NULL);
1368       if (hi == h)
1369 	return TRUE;
1370       while (hi->root.type == bfd_link_hash_indirect
1371 	     || hi->root.type == bfd_link_hash_warning)
1372 	{
1373 	  hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1374 	  if (hi == h)
1375 	    return TRUE;
1376 	}
1377     }
1378 
1379   bed = get_elf_backend_data (abfd);
1380   collect = bed->collect;
1381   dynamic = (abfd->flags & DYNAMIC) != 0;
1382 
1383   shortlen = p - name;
1384   shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1385   if (shortname == NULL)
1386     return FALSE;
1387   memcpy (shortname, name, shortlen);
1388   shortname[shortlen] = '\0';
1389 
1390   /* We are going to create a new symbol.  Merge it with any existing
1391      symbol with this name.  For the purposes of the merge, act as
1392      though we were defining the symbol we just defined, although we
1393      actually going to define an indirect symbol.  */
1394   type_change_ok = FALSE;
1395   size_change_ok = FALSE;
1396   sec = *psec;
1397   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1398 			      NULL, &hi, &skip, &override,
1399 			      &type_change_ok, &size_change_ok))
1400     return FALSE;
1401 
1402   if (skip)
1403     goto nondefault;
1404 
1405   if (! override)
1406     {
1407       bh = &hi->root;
1408       if (! (_bfd_generic_link_add_one_symbol
1409 	     (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1410 	      0, name, FALSE, collect, &bh)))
1411 	return FALSE;
1412       hi = (struct elf_link_hash_entry *) bh;
1413     }
1414   else
1415     {
1416       /* In this case the symbol named SHORTNAME is overriding the
1417 	 indirect symbol we want to add.  We were planning on making
1418 	 SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
1419 	 is the name without a version.  NAME is the fully versioned
1420 	 name, and it is the default version.
1421 
1422 	 Overriding means that we already saw a definition for the
1423 	 symbol SHORTNAME in a regular object, and it is overriding
1424 	 the symbol defined in the dynamic object.
1425 
1426 	 When this happens, we actually want to change NAME, the
1427 	 symbol we just added, to refer to SHORTNAME.  This will cause
1428 	 references to NAME in the shared object to become references
1429 	 to SHORTNAME in the regular object.  This is what we expect
1430 	 when we override a function in a shared object: that the
1431 	 references in the shared object will be mapped to the
1432 	 definition in the regular object.  */
1433 
1434       while (hi->root.type == bfd_link_hash_indirect
1435 	     || hi->root.type == bfd_link_hash_warning)
1436 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1437 
1438       h->root.type = bfd_link_hash_indirect;
1439       h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1440       if (h->def_dynamic)
1441 	{
1442 	  h->def_dynamic = 0;
1443 	  hi->ref_dynamic = 1;
1444 	  if (hi->ref_regular
1445 	      || hi->def_regular)
1446 	    {
1447 	      if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1448 		return FALSE;
1449 	    }
1450 	}
1451 
1452       /* Now set HI to H, so that the following code will set the
1453 	 other fields correctly.  */
1454       hi = h;
1455     }
1456 
1457   /* If there is a duplicate definition somewhere, then HI may not
1458      point to an indirect symbol.  We will have reported an error to
1459      the user in that case.  */
1460 
1461   if (hi->root.type == bfd_link_hash_indirect)
1462     {
1463       struct elf_link_hash_entry *ht;
1464 
1465       ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1466       (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1467 
1468       /* See if the new flags lead us to realize that the symbol must
1469 	 be dynamic.  */
1470       if (! *dynsym)
1471 	{
1472 	  if (! dynamic)
1473 	    {
1474 	      if (info->shared
1475 		  || hi->ref_dynamic)
1476 		*dynsym = TRUE;
1477 	    }
1478 	  else
1479 	    {
1480 	      if (hi->ref_regular)
1481 		*dynsym = TRUE;
1482 	    }
1483 	}
1484     }
1485 
1486   /* We also need to define an indirection from the nondefault version
1487      of the symbol.  */
1488 
1489 nondefault:
1490   len = strlen (name);
1491   shortname = bfd_hash_allocate (&info->hash->table, len);
1492   if (shortname == NULL)
1493     return FALSE;
1494   memcpy (shortname, name, shortlen);
1495   memcpy (shortname + shortlen, p + 1, len - shortlen);
1496 
1497   /* Once again, merge with any existing symbol.  */
1498   type_change_ok = FALSE;
1499   size_change_ok = FALSE;
1500   sec = *psec;
1501   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1502 			      NULL, &hi, &skip, &override,
1503 			      &type_change_ok, &size_change_ok))
1504     return FALSE;
1505 
1506   if (skip)
1507     return TRUE;
1508 
1509   if (override)
1510     {
1511       /* Here SHORTNAME is a versioned name, so we don't expect to see
1512 	 the type of override we do in the case above unless it is
1513 	 overridden by a versioned definition.  */
1514       if (hi->root.type != bfd_link_hash_defined
1515 	  && hi->root.type != bfd_link_hash_defweak)
1516 	(*_bfd_error_handler)
1517 	  (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1518 	   abfd, shortname);
1519     }
1520   else
1521     {
1522       bh = &hi->root;
1523       if (! (_bfd_generic_link_add_one_symbol
1524 	     (info, abfd, shortname, BSF_INDIRECT,
1525 	      bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1526 	return FALSE;
1527       hi = (struct elf_link_hash_entry *) bh;
1528 
1529       /* If there is a duplicate definition somewhere, then HI may not
1530 	 point to an indirect symbol.  We will have reported an error
1531 	 to the user in that case.  */
1532 
1533       if (hi->root.type == bfd_link_hash_indirect)
1534 	{
1535 	  (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1536 
1537 	  /* See if the new flags lead us to realize that the symbol
1538 	     must be dynamic.  */
1539 	  if (! *dynsym)
1540 	    {
1541 	      if (! dynamic)
1542 		{
1543 		  if (info->shared
1544 		      || hi->ref_dynamic)
1545 		    *dynsym = TRUE;
1546 		}
1547 	      else
1548 		{
1549 		  if (hi->ref_regular)
1550 		    *dynsym = TRUE;
1551 		}
1552 	    }
1553 	}
1554     }
1555 
1556   return TRUE;
1557 }
1558 
1559 /* This routine is used to export all defined symbols into the dynamic
1560    symbol table.  It is called via elf_link_hash_traverse.  */
1561 
1562 bfd_boolean
_bfd_elf_export_symbol(struct elf_link_hash_entry * h,void * data)1563 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1564 {
1565   struct elf_info_failed *eif = data;
1566 
1567   /* Ignore indirect symbols.  These are added by the versioning code.  */
1568   if (h->root.type == bfd_link_hash_indirect)
1569     return TRUE;
1570 
1571   if (h->root.type == bfd_link_hash_warning)
1572     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1573 
1574   if (h->dynindx == -1
1575       && (h->def_regular
1576 	  || h->ref_regular))
1577     {
1578       struct bfd_elf_version_tree *t;
1579       struct bfd_elf_version_expr *d;
1580 
1581       for (t = eif->verdefs; t != NULL; t = t->next)
1582 	{
1583 	  if (t->globals.list != NULL)
1584 	    {
1585 	      d = (*t->match) (&t->globals, NULL, h->root.root.string);
1586 	      if (d != NULL)
1587 		goto doit;
1588 	    }
1589 
1590 	  if (t->locals.list != NULL)
1591 	    {
1592 	      d = (*t->match) (&t->locals, NULL, h->root.root.string);
1593 	      if (d != NULL)
1594 		return TRUE;
1595 	    }
1596 	}
1597 
1598       if (!eif->verdefs)
1599 	{
1600 	doit:
1601 	  if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1602 	    {
1603 	      eif->failed = TRUE;
1604 	      return FALSE;
1605 	    }
1606 	}
1607     }
1608 
1609   return TRUE;
1610 }
1611 
1612 /* Look through the symbols which are defined in other shared
1613    libraries and referenced here.  Update the list of version
1614    dependencies.  This will be put into the .gnu.version_r section.
1615    This function is called via elf_link_hash_traverse.  */
1616 
1617 bfd_boolean
_bfd_elf_link_find_version_dependencies(struct elf_link_hash_entry * h,void * data)1618 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1619 					 void *data)
1620 {
1621   struct elf_find_verdep_info *rinfo = data;
1622   Elf_Internal_Verneed *t;
1623   Elf_Internal_Vernaux *a;
1624   bfd_size_type amt;
1625 
1626   if (h->root.type == bfd_link_hash_warning)
1627     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1628 
1629   /* We only care about symbols defined in shared objects with version
1630      information.  */
1631   if (!h->def_dynamic
1632       || h->def_regular
1633       || h->dynindx == -1
1634       || h->verinfo.verdef == NULL)
1635     return TRUE;
1636 
1637   /* See if we already know about this version.  */
1638   for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1639     {
1640       if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1641 	continue;
1642 
1643       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1644 	if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1645 	  return TRUE;
1646 
1647       break;
1648     }
1649 
1650   /* This is a new version.  Add it to tree we are building.  */
1651 
1652   if (t == NULL)
1653     {
1654       amt = sizeof *t;
1655       t = bfd_zalloc (rinfo->output_bfd, amt);
1656       if (t == NULL)
1657 	{
1658 	  rinfo->failed = TRUE;
1659 	  return FALSE;
1660 	}
1661 
1662       t->vn_bfd = h->verinfo.verdef->vd_bfd;
1663       t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1664       elf_tdata (rinfo->output_bfd)->verref = t;
1665     }
1666 
1667   amt = sizeof *a;
1668   a = bfd_zalloc (rinfo->output_bfd, amt);
1669 
1670   /* Note that we are copying a string pointer here, and testing it
1671      above.  If bfd_elf_string_from_elf_section is ever changed to
1672      discard the string data when low in memory, this will have to be
1673      fixed.  */
1674   a->vna_nodename = h->verinfo.verdef->vd_nodename;
1675 
1676   a->vna_flags = h->verinfo.verdef->vd_flags;
1677   a->vna_nextptr = t->vn_auxptr;
1678 
1679   h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1680   ++rinfo->vers;
1681 
1682   a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1683 
1684   t->vn_auxptr = a;
1685 
1686   return TRUE;
1687 }
1688 
1689 /* Figure out appropriate versions for all the symbols.  We may not
1690    have the version number script until we have read all of the input
1691    files, so until that point we don't know which symbols should be
1692    local.  This function is called via elf_link_hash_traverse.  */
1693 
1694 bfd_boolean
_bfd_elf_link_assign_sym_version(struct elf_link_hash_entry * h,void * data)1695 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1696 {
1697   struct elf_assign_sym_version_info *sinfo;
1698   struct bfd_link_info *info;
1699   const struct elf_backend_data *bed;
1700   struct elf_info_failed eif;
1701   char *p;
1702   bfd_size_type amt;
1703 
1704   sinfo = data;
1705   info = sinfo->info;
1706 
1707   if (h->root.type == bfd_link_hash_warning)
1708     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1709 
1710   /* Fix the symbol flags.  */
1711   eif.failed = FALSE;
1712   eif.info = info;
1713   if (! _bfd_elf_fix_symbol_flags (h, &eif))
1714     {
1715       if (eif.failed)
1716 	sinfo->failed = TRUE;
1717       return FALSE;
1718     }
1719 
1720   /* We only need version numbers for symbols defined in regular
1721      objects.  */
1722   if (!h->def_regular)
1723     return TRUE;
1724 
1725   bed = get_elf_backend_data (sinfo->output_bfd);
1726   p = strchr (h->root.root.string, ELF_VER_CHR);
1727   if (p != NULL && h->verinfo.vertree == NULL)
1728     {
1729       struct bfd_elf_version_tree *t;
1730       bfd_boolean hidden;
1731 
1732       hidden = TRUE;
1733 
1734       /* There are two consecutive ELF_VER_CHR characters if this is
1735 	 not a hidden symbol.  */
1736       ++p;
1737       if (*p == ELF_VER_CHR)
1738 	{
1739 	  hidden = FALSE;
1740 	  ++p;
1741 	}
1742 
1743       /* If there is no version string, we can just return out.  */
1744       if (*p == '\0')
1745 	{
1746 	  if (hidden)
1747 	    h->hidden = 1;
1748 	  return TRUE;
1749 	}
1750 
1751       /* Look for the version.  If we find it, it is no longer weak.  */
1752       for (t = sinfo->verdefs; t != NULL; t = t->next)
1753 	{
1754 	  if (strcmp (t->name, p) == 0)
1755 	    {
1756 	      size_t len;
1757 	      char *alc;
1758 	      struct bfd_elf_version_expr *d;
1759 
1760 	      len = p - h->root.root.string;
1761 	      alc = bfd_malloc (len);
1762 	      if (alc == NULL)
1763 		return FALSE;
1764 	      memcpy (alc, h->root.root.string, len - 1);
1765 	      alc[len - 1] = '\0';
1766 	      if (alc[len - 2] == ELF_VER_CHR)
1767 		alc[len - 2] = '\0';
1768 
1769 	      h->verinfo.vertree = t;
1770 	      t->used = TRUE;
1771 	      d = NULL;
1772 
1773 	      if (t->globals.list != NULL)
1774 		d = (*t->match) (&t->globals, NULL, alc);
1775 
1776 	      /* See if there is anything to force this symbol to
1777 		 local scope.  */
1778 	      if (d == NULL && t->locals.list != NULL)
1779 		{
1780 		  d = (*t->match) (&t->locals, NULL, alc);
1781 		  if (d != NULL
1782 		      && h->dynindx != -1
1783 		      && ! info->export_dynamic)
1784 		    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1785 		}
1786 
1787 	      free (alc);
1788 	      break;
1789 	    }
1790 	}
1791 
1792       /* If we are building an application, we need to create a
1793 	 version node for this version.  */
1794       if (t == NULL && info->executable)
1795 	{
1796 	  struct bfd_elf_version_tree **pp;
1797 	  int version_index;
1798 
1799 	  /* If we aren't going to export this symbol, we don't need
1800 	     to worry about it.  */
1801 	  if (h->dynindx == -1)
1802 	    return TRUE;
1803 
1804 	  amt = sizeof *t;
1805 	  t = bfd_zalloc (sinfo->output_bfd, amt);
1806 	  if (t == NULL)
1807 	    {
1808 	      sinfo->failed = TRUE;
1809 	      return FALSE;
1810 	    }
1811 
1812 	  t->name = p;
1813 	  t->name_indx = (unsigned int) -1;
1814 	  t->used = TRUE;
1815 
1816 	  version_index = 1;
1817 	  /* Don't count anonymous version tag.  */
1818 	  if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1819 	    version_index = 0;
1820 	  for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1821 	    ++version_index;
1822 	  t->vernum = version_index;
1823 
1824 	  *pp = t;
1825 
1826 	  h->verinfo.vertree = t;
1827 	}
1828       else if (t == NULL)
1829 	{
1830 	  /* We could not find the version for a symbol when
1831 	     generating a shared archive.  Return an error.  */
1832 	  (*_bfd_error_handler)
1833 	    (_("%B: undefined versioned symbol name %s"),
1834 	     sinfo->output_bfd, h->root.root.string);
1835 	  bfd_set_error (bfd_error_bad_value);
1836 	  sinfo->failed = TRUE;
1837 	  return FALSE;
1838 	}
1839 
1840       if (hidden)
1841 	h->hidden = 1;
1842     }
1843 
1844   /* If we don't have a version for this symbol, see if we can find
1845      something.  */
1846   if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1847     {
1848       struct bfd_elf_version_tree *t;
1849       struct bfd_elf_version_tree *local_ver;
1850       struct bfd_elf_version_expr *d;
1851 
1852       /* See if can find what version this symbol is in.  If the
1853 	 symbol is supposed to be local, then don't actually register
1854 	 it.  */
1855       local_ver = NULL;
1856       for (t = sinfo->verdefs; t != NULL; t = t->next)
1857 	{
1858 	  if (t->globals.list != NULL)
1859 	    {
1860 	      bfd_boolean matched;
1861 
1862 	      matched = FALSE;
1863 	      d = NULL;
1864 	      while ((d = (*t->match) (&t->globals, d,
1865 				       h->root.root.string)) != NULL)
1866 		if (d->symver)
1867 		  matched = TRUE;
1868 		else
1869 		  {
1870 		    /* There is a version without definition.  Make
1871 		       the symbol the default definition for this
1872 		       version.  */
1873 		    h->verinfo.vertree = t;
1874 		    local_ver = NULL;
1875 		    d->script = 1;
1876 		    break;
1877 		  }
1878 	      if (d != NULL)
1879 		break;
1880 	      else if (matched)
1881 		/* There is no undefined version for this symbol. Hide the
1882 		   default one.  */
1883 		(*bed->elf_backend_hide_symbol) (info, h, TRUE);
1884 	    }
1885 
1886 	  if (t->locals.list != NULL)
1887 	    {
1888 	      d = NULL;
1889 	      while ((d = (*t->match) (&t->locals, d,
1890 				       h->root.root.string)) != NULL)
1891 		{
1892 		  local_ver = t;
1893 		  /* If the match is "*", keep looking for a more
1894 		     explicit, perhaps even global, match.
1895 		     XXX: Shouldn't this be !d->wildcard instead?  */
1896 		  if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1897 		    break;
1898 		}
1899 
1900 	      if (d != NULL)
1901 		break;
1902 	    }
1903 	}
1904 
1905       if (local_ver != NULL)
1906 	{
1907 	  h->verinfo.vertree = local_ver;
1908 	  if (h->dynindx != -1
1909 	      && ! info->export_dynamic)
1910 	    {
1911 	      (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1912 	    }
1913 	}
1914     }
1915 
1916   return TRUE;
1917 }
1918 
1919 /* Read and swap the relocs from the section indicated by SHDR.  This
1920    may be either a REL or a RELA section.  The relocations are
1921    translated into RELA relocations and stored in INTERNAL_RELOCS,
1922    which should have already been allocated to contain enough space.
1923    The EXTERNAL_RELOCS are a buffer where the external form of the
1924    relocations should be stored.
1925 
1926    Returns FALSE if something goes wrong.  */
1927 
1928 static bfd_boolean
elf_link_read_relocs_from_section(bfd * abfd,asection * sec,Elf_Internal_Shdr * shdr,void * external_relocs,Elf_Internal_Rela * internal_relocs)1929 elf_link_read_relocs_from_section (bfd *abfd,
1930 				   asection *sec,
1931 				   Elf_Internal_Shdr *shdr,
1932 				   void *external_relocs,
1933 				   Elf_Internal_Rela *internal_relocs)
1934 {
1935   const struct elf_backend_data *bed;
1936   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1937   const bfd_byte *erela;
1938   const bfd_byte *erelaend;
1939   Elf_Internal_Rela *irela;
1940   Elf_Internal_Shdr *symtab_hdr;
1941   size_t nsyms;
1942 
1943   /* Position ourselves at the start of the section.  */
1944   if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1945     return FALSE;
1946 
1947   /* Read the relocations.  */
1948   if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1949     return FALSE;
1950 
1951   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1952   nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1953 
1954   bed = get_elf_backend_data (abfd);
1955 
1956   /* Convert the external relocations to the internal format.  */
1957   if (shdr->sh_entsize == bed->s->sizeof_rel)
1958     swap_in = bed->s->swap_reloc_in;
1959   else if (shdr->sh_entsize == bed->s->sizeof_rela)
1960     swap_in = bed->s->swap_reloca_in;
1961   else
1962     {
1963       bfd_set_error (bfd_error_wrong_format);
1964       return FALSE;
1965     }
1966 
1967   erela = external_relocs;
1968   erelaend = erela + shdr->sh_size;
1969   irela = internal_relocs;
1970   while (erela < erelaend)
1971     {
1972       bfd_vma r_symndx;
1973 
1974       (*swap_in) (abfd, erela, irela);
1975       r_symndx = ELF32_R_SYM (irela->r_info);
1976       if (bed->s->arch_size == 64)
1977 	r_symndx >>= 24;
1978       if ((size_t) r_symndx >= nsyms)
1979 	{
1980 	  (*_bfd_error_handler)
1981 	    (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1982 	       " for offset 0x%lx in section `%A'"),
1983 	     abfd, sec,
1984 	     (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1985 	  bfd_set_error (bfd_error_bad_value);
1986 	  return FALSE;
1987 	}
1988       irela += bed->s->int_rels_per_ext_rel;
1989       erela += shdr->sh_entsize;
1990     }
1991 
1992   return TRUE;
1993 }
1994 
1995 /* Read and swap the relocs for a section O.  They may have been
1996    cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1997    not NULL, they are used as buffers to read into.  They are known to
1998    be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
1999    the return value is allocated using either malloc or bfd_alloc,
2000    according to the KEEP_MEMORY argument.  If O has two relocation
2001    sections (both REL and RELA relocations), then the REL_HDR
2002    relocations will appear first in INTERNAL_RELOCS, followed by the
2003    REL_HDR2 relocations.  */
2004 
2005 Elf_Internal_Rela *
_bfd_elf_link_read_relocs(bfd * abfd,asection * o,void * external_relocs,Elf_Internal_Rela * internal_relocs,bfd_boolean keep_memory)2006 _bfd_elf_link_read_relocs (bfd *abfd,
2007 			   asection *o,
2008 			   void *external_relocs,
2009 			   Elf_Internal_Rela *internal_relocs,
2010 			   bfd_boolean keep_memory)
2011 {
2012   Elf_Internal_Shdr *rel_hdr;
2013   void *alloc1 = NULL;
2014   Elf_Internal_Rela *alloc2 = NULL;
2015   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2016 
2017   if (elf_section_data (o)->relocs != NULL)
2018     return elf_section_data (o)->relocs;
2019 
2020   if (o->reloc_count == 0)
2021     return NULL;
2022 
2023   rel_hdr = &elf_section_data (o)->rel_hdr;
2024 
2025   if (internal_relocs == NULL)
2026     {
2027       bfd_size_type size;
2028 
2029       size = o->reloc_count;
2030       size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2031       if (keep_memory)
2032 	internal_relocs = bfd_alloc (abfd, size);
2033       else
2034 	internal_relocs = alloc2 = bfd_malloc (size);
2035       if (internal_relocs == NULL)
2036 	goto error_return;
2037     }
2038 
2039   if (external_relocs == NULL)
2040     {
2041       bfd_size_type size = rel_hdr->sh_size;
2042 
2043       if (elf_section_data (o)->rel_hdr2)
2044 	size += elf_section_data (o)->rel_hdr2->sh_size;
2045       alloc1 = bfd_malloc (size);
2046       if (alloc1 == NULL)
2047 	goto error_return;
2048       external_relocs = alloc1;
2049     }
2050 
2051   if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2052 					  external_relocs,
2053 					  internal_relocs))
2054     goto error_return;
2055   if (elf_section_data (o)->rel_hdr2
2056       && (!elf_link_read_relocs_from_section
2057 	  (abfd, o,
2058 	   elf_section_data (o)->rel_hdr2,
2059 	   ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2060 	   internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2061 			      * bed->s->int_rels_per_ext_rel))))
2062     goto error_return;
2063 
2064   /* Cache the results for next time, if we can.  */
2065   if (keep_memory)
2066     elf_section_data (o)->relocs = internal_relocs;
2067 
2068   if (alloc1 != NULL)
2069     free (alloc1);
2070 
2071   /* Don't free alloc2, since if it was allocated we are passing it
2072      back (under the name of internal_relocs).  */
2073 
2074   return internal_relocs;
2075 
2076  error_return:
2077   if (alloc1 != NULL)
2078     free (alloc1);
2079   if (alloc2 != NULL)
2080     free (alloc2);
2081   return NULL;
2082 }
2083 
2084 /* Compute the size of, and allocate space for, REL_HDR which is the
2085    section header for a section containing relocations for O.  */
2086 
2087 bfd_boolean
_bfd_elf_link_size_reloc_section(bfd * abfd,Elf_Internal_Shdr * rel_hdr,asection * o)2088 _bfd_elf_link_size_reloc_section (bfd *abfd,
2089 				  Elf_Internal_Shdr *rel_hdr,
2090 				  asection *o)
2091 {
2092   bfd_size_type reloc_count;
2093   bfd_size_type num_rel_hashes;
2094 
2095   /* Figure out how many relocations there will be.  */
2096   if (rel_hdr == &elf_section_data (o)->rel_hdr)
2097     reloc_count = elf_section_data (o)->rel_count;
2098   else
2099     reloc_count = elf_section_data (o)->rel_count2;
2100 
2101   num_rel_hashes = o->reloc_count;
2102   if (num_rel_hashes < reloc_count)
2103     num_rel_hashes = reloc_count;
2104 
2105   /* That allows us to calculate the size of the section.  */
2106   rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2107 
2108   /* The contents field must last into write_object_contents, so we
2109      allocate it with bfd_alloc rather than malloc.  Also since we
2110      cannot be sure that the contents will actually be filled in,
2111      we zero the allocated space.  */
2112   rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2113   if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2114     return FALSE;
2115 
2116   /* We only allocate one set of hash entries, so we only do it the
2117      first time we are called.  */
2118   if (elf_section_data (o)->rel_hashes == NULL
2119       && num_rel_hashes)
2120     {
2121       struct elf_link_hash_entry **p;
2122 
2123       p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2124       if (p == NULL)
2125 	return FALSE;
2126 
2127       elf_section_data (o)->rel_hashes = p;
2128     }
2129 
2130   return TRUE;
2131 }
2132 
2133 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2134    originated from the section given by INPUT_REL_HDR) to the
2135    OUTPUT_BFD.  */
2136 
2137 bfd_boolean
_bfd_elf_link_output_relocs(bfd * output_bfd,asection * input_section,Elf_Internal_Shdr * input_rel_hdr,Elf_Internal_Rela * internal_relocs,struct elf_link_hash_entry ** rel_hash ATTRIBUTE_UNUSED)2138 _bfd_elf_link_output_relocs (bfd *output_bfd,
2139 			     asection *input_section,
2140 			     Elf_Internal_Shdr *input_rel_hdr,
2141 			     Elf_Internal_Rela *internal_relocs,
2142 			     struct elf_link_hash_entry **rel_hash
2143 			       ATTRIBUTE_UNUSED)
2144 {
2145   Elf_Internal_Rela *irela;
2146   Elf_Internal_Rela *irelaend;
2147   bfd_byte *erel;
2148   Elf_Internal_Shdr *output_rel_hdr;
2149   asection *output_section;
2150   unsigned int *rel_countp = NULL;
2151   const struct elf_backend_data *bed;
2152   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2153 
2154   output_section = input_section->output_section;
2155   output_rel_hdr = NULL;
2156 
2157   if (elf_section_data (output_section)->rel_hdr.sh_entsize
2158       == input_rel_hdr->sh_entsize)
2159     {
2160       output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2161       rel_countp = &elf_section_data (output_section)->rel_count;
2162     }
2163   else if (elf_section_data (output_section)->rel_hdr2
2164 	   && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2165 	       == input_rel_hdr->sh_entsize))
2166     {
2167       output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2168       rel_countp = &elf_section_data (output_section)->rel_count2;
2169     }
2170   else
2171     {
2172       (*_bfd_error_handler)
2173 	(_("%B: relocation size mismatch in %B section %A"),
2174 	 output_bfd, input_section->owner, input_section);
2175       bfd_set_error (bfd_error_wrong_object_format);
2176       return FALSE;
2177     }
2178 
2179   bed = get_elf_backend_data (output_bfd);
2180   if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2181     swap_out = bed->s->swap_reloc_out;
2182   else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2183     swap_out = bed->s->swap_reloca_out;
2184   else
2185     abort ();
2186 
2187   erel = output_rel_hdr->contents;
2188   erel += *rel_countp * input_rel_hdr->sh_entsize;
2189   irela = internal_relocs;
2190   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2191 		      * bed->s->int_rels_per_ext_rel);
2192   while (irela < irelaend)
2193     {
2194       (*swap_out) (output_bfd, irela, erel);
2195       irela += bed->s->int_rels_per_ext_rel;
2196       erel += input_rel_hdr->sh_entsize;
2197     }
2198 
2199   /* Bump the counter, so that we know where to add the next set of
2200      relocations.  */
2201   *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2202 
2203   return TRUE;
2204 }
2205 
2206 /* Fix up the flags for a symbol.  This handles various cases which
2207    can only be fixed after all the input files are seen.  This is
2208    currently called by both adjust_dynamic_symbol and
2209    assign_sym_version, which is unnecessary but perhaps more robust in
2210    the face of future changes.  */
2211 
2212 bfd_boolean
_bfd_elf_fix_symbol_flags(struct elf_link_hash_entry * h,struct elf_info_failed * eif)2213 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2214 			   struct elf_info_failed *eif)
2215 {
2216   /* If this symbol was mentioned in a non-ELF file, try to set
2217      DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
2218      permit a non-ELF file to correctly refer to a symbol defined in
2219      an ELF dynamic object.  */
2220   if (h->non_elf)
2221     {
2222       while (h->root.type == bfd_link_hash_indirect)
2223 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
2224 
2225       if (h->root.type != bfd_link_hash_defined
2226 	  && h->root.type != bfd_link_hash_defweak)
2227 	{
2228 	  h->ref_regular = 1;
2229 	  h->ref_regular_nonweak = 1;
2230 	}
2231       else
2232 	{
2233 	  if (h->root.u.def.section->owner != NULL
2234 	      && (bfd_get_flavour (h->root.u.def.section->owner)
2235 		  == bfd_target_elf_flavour))
2236 	    {
2237 	      h->ref_regular = 1;
2238 	      h->ref_regular_nonweak = 1;
2239 	    }
2240 	  else
2241 	    h->def_regular = 1;
2242 	}
2243 
2244       if (h->dynindx == -1
2245 	  && (h->def_dynamic
2246 	      || h->ref_dynamic))
2247 	{
2248 	  if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2249 	    {
2250 	      eif->failed = TRUE;
2251 	      return FALSE;
2252 	    }
2253 	}
2254     }
2255   else
2256     {
2257       /* Unfortunately, NON_ELF is only correct if the symbol
2258 	 was first seen in a non-ELF file.  Fortunately, if the symbol
2259 	 was first seen in an ELF file, we're probably OK unless the
2260 	 symbol was defined in a non-ELF file.  Catch that case here.
2261 	 FIXME: We're still in trouble if the symbol was first seen in
2262 	 a dynamic object, and then later in a non-ELF regular object.  */
2263       if ((h->root.type == bfd_link_hash_defined
2264 	   || h->root.type == bfd_link_hash_defweak)
2265 	  && !h->def_regular
2266 	  && (h->root.u.def.section->owner != NULL
2267 	      ? (bfd_get_flavour (h->root.u.def.section->owner)
2268 		 != bfd_target_elf_flavour)
2269 	      : (bfd_is_abs_section (h->root.u.def.section)
2270 		 && !h->def_dynamic)))
2271 	h->def_regular = 1;
2272     }
2273 
2274   /* If this is a final link, and the symbol was defined as a common
2275      symbol in a regular object file, and there was no definition in
2276      any dynamic object, then the linker will have allocated space for
2277      the symbol in a common section but the DEF_REGULAR
2278      flag will not have been set.  */
2279   if (h->root.type == bfd_link_hash_defined
2280       && !h->def_regular
2281       && h->ref_regular
2282       && !h->def_dynamic
2283       && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2284     h->def_regular = 1;
2285 
2286   /* If -Bsymbolic was used (which means to bind references to global
2287      symbols to the definition within the shared object), and this
2288      symbol was defined in a regular object, then it actually doesn't
2289      need a PLT entry.  Likewise, if the symbol has non-default
2290      visibility.  If the symbol has hidden or internal visibility, we
2291      will force it local.  */
2292   if (h->needs_plt
2293       && eif->info->shared
2294       && is_elf_hash_table (eif->info->hash)
2295       && (eif->info->symbolic
2296 	  || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2297       && h->def_regular)
2298     {
2299       const struct elf_backend_data *bed;
2300       bfd_boolean force_local;
2301 
2302       bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2303 
2304       force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2305 		     || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2306       (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2307     }
2308 
2309   /* If a weak undefined symbol has non-default visibility, we also
2310      hide it from the dynamic linker.  */
2311   if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2312       && h->root.type == bfd_link_hash_undefweak)
2313     {
2314       const struct elf_backend_data *bed;
2315       bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2316       (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2317     }
2318 
2319   /* If this is a weak defined symbol in a dynamic object, and we know
2320      the real definition in the dynamic object, copy interesting flags
2321      over to the real definition.  */
2322   if (h->u.weakdef != NULL)
2323     {
2324       struct elf_link_hash_entry *weakdef;
2325 
2326       weakdef = h->u.weakdef;
2327       if (h->root.type == bfd_link_hash_indirect)
2328 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
2329 
2330       BFD_ASSERT (h->root.type == bfd_link_hash_defined
2331 		  || h->root.type == bfd_link_hash_defweak);
2332       BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2333 		  || weakdef->root.type == bfd_link_hash_defweak);
2334       BFD_ASSERT (weakdef->def_dynamic);
2335 
2336       /* If the real definition is defined by a regular object file,
2337 	 don't do anything special.  See the longer description in
2338 	 _bfd_elf_adjust_dynamic_symbol, below.  */
2339       if (weakdef->def_regular)
2340 	h->u.weakdef = NULL;
2341       else
2342 	{
2343 	  const struct elf_backend_data *bed;
2344 
2345 	  bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2346 	  (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2347 	}
2348     }
2349 
2350   return TRUE;
2351 }
2352 
2353 /* Make the backend pick a good value for a dynamic symbol.  This is
2354    called via elf_link_hash_traverse, and also calls itself
2355    recursively.  */
2356 
2357 bfd_boolean
_bfd_elf_adjust_dynamic_symbol(struct elf_link_hash_entry * h,void * data)2358 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2359 {
2360   struct elf_info_failed *eif = data;
2361   bfd *dynobj;
2362   const struct elf_backend_data *bed;
2363 
2364   if (! is_elf_hash_table (eif->info->hash))
2365     return FALSE;
2366 
2367   if (h->root.type == bfd_link_hash_warning)
2368     {
2369       h->got = elf_hash_table (eif->info)->init_got_offset;
2370       h->plt = elf_hash_table (eif->info)->init_plt_offset;
2371 
2372       /* When warning symbols are created, they **replace** the "real"
2373 	 entry in the hash table, thus we never get to see the real
2374 	 symbol in a hash traversal.  So look at it now.  */
2375       h = (struct elf_link_hash_entry *) h->root.u.i.link;
2376     }
2377 
2378   /* Ignore indirect symbols.  These are added by the versioning code.  */
2379   if (h->root.type == bfd_link_hash_indirect)
2380     return TRUE;
2381 
2382   /* Fix the symbol flags.  */
2383   if (! _bfd_elf_fix_symbol_flags (h, eif))
2384     return FALSE;
2385 
2386   /* If this symbol does not require a PLT entry, and it is not
2387      defined by a dynamic object, or is not referenced by a regular
2388      object, ignore it.  We do have to handle a weak defined symbol,
2389      even if no regular object refers to it, if we decided to add it
2390      to the dynamic symbol table.  FIXME: Do we normally need to worry
2391      about symbols which are defined by one dynamic object and
2392      referenced by another one?  */
2393   if (!h->needs_plt
2394       && (h->def_regular
2395 	  || !h->def_dynamic
2396 	  || (!h->ref_regular
2397 	      && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2398     {
2399       h->plt = elf_hash_table (eif->info)->init_plt_offset;
2400       return TRUE;
2401     }
2402 
2403   /* If we've already adjusted this symbol, don't do it again.  This
2404      can happen via a recursive call.  */
2405   if (h->dynamic_adjusted)
2406     return TRUE;
2407 
2408   /* Don't look at this symbol again.  Note that we must set this
2409      after checking the above conditions, because we may look at a
2410      symbol once, decide not to do anything, and then get called
2411      recursively later after REF_REGULAR is set below.  */
2412   h->dynamic_adjusted = 1;
2413 
2414   /* If this is a weak definition, and we know a real definition, and
2415      the real symbol is not itself defined by a regular object file,
2416      then get a good value for the real definition.  We handle the
2417      real symbol first, for the convenience of the backend routine.
2418 
2419      Note that there is a confusing case here.  If the real definition
2420      is defined by a regular object file, we don't get the real symbol
2421      from the dynamic object, but we do get the weak symbol.  If the
2422      processor backend uses a COPY reloc, then if some routine in the
2423      dynamic object changes the real symbol, we will not see that
2424      change in the corresponding weak symbol.  This is the way other
2425      ELF linkers work as well, and seems to be a result of the shared
2426      library model.
2427 
2428      I will clarify this issue.  Most SVR4 shared libraries define the
2429      variable _timezone and define timezone as a weak synonym.  The
2430      tzset call changes _timezone.  If you write
2431        extern int timezone;
2432        int _timezone = 5;
2433        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2434      you might expect that, since timezone is a synonym for _timezone,
2435      the same number will print both times.  However, if the processor
2436      backend uses a COPY reloc, then actually timezone will be copied
2437      into your process image, and, since you define _timezone
2438      yourself, _timezone will not.  Thus timezone and _timezone will
2439      wind up at different memory locations.  The tzset call will set
2440      _timezone, leaving timezone unchanged.  */
2441 
2442   if (h->u.weakdef != NULL)
2443     {
2444       /* If we get to this point, we know there is an implicit
2445 	 reference by a regular object file via the weak symbol H.
2446 	 FIXME: Is this really true?  What if the traversal finds
2447 	 H->U.WEAKDEF before it finds H?  */
2448       h->u.weakdef->ref_regular = 1;
2449 
2450       if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2451 	return FALSE;
2452     }
2453 
2454   /* If a symbol has no type and no size and does not require a PLT
2455      entry, then we are probably about to do the wrong thing here: we
2456      are probably going to create a COPY reloc for an empty object.
2457      This case can arise when a shared object is built with assembly
2458      code, and the assembly code fails to set the symbol type.  */
2459   if (h->size == 0
2460       && h->type == STT_NOTYPE
2461       && !h->needs_plt)
2462     (*_bfd_error_handler)
2463       (_("warning: type and size of dynamic symbol `%s' are not defined"),
2464        h->root.root.string);
2465 
2466   dynobj = elf_hash_table (eif->info)->dynobj;
2467   bed = get_elf_backend_data (dynobj);
2468   if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2469     {
2470       eif->failed = TRUE;
2471       return FALSE;
2472     }
2473 
2474   return TRUE;
2475 }
2476 
2477 /* Adjust all external symbols pointing into SEC_MERGE sections
2478    to reflect the object merging within the sections.  */
2479 
2480 bfd_boolean
_bfd_elf_link_sec_merge_syms(struct elf_link_hash_entry * h,void * data)2481 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2482 {
2483   asection *sec;
2484 
2485   if (h->root.type == bfd_link_hash_warning)
2486     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2487 
2488   if ((h->root.type == bfd_link_hash_defined
2489        || h->root.type == bfd_link_hash_defweak)
2490       && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2491       && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2492     {
2493       bfd *output_bfd = data;
2494 
2495       h->root.u.def.value =
2496 	_bfd_merged_section_offset (output_bfd,
2497 				    &h->root.u.def.section,
2498 				    elf_section_data (sec)->sec_info,
2499 				    h->root.u.def.value);
2500     }
2501 
2502   return TRUE;
2503 }
2504 
2505 /* Returns false if the symbol referred to by H should be considered
2506    to resolve local to the current module, and true if it should be
2507    considered to bind dynamically.  */
2508 
2509 bfd_boolean
_bfd_elf_dynamic_symbol_p(struct elf_link_hash_entry * h,struct bfd_link_info * info,bfd_boolean ignore_protected)2510 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2511 			   struct bfd_link_info *info,
2512 			   bfd_boolean ignore_protected)
2513 {
2514   bfd_boolean binding_stays_local_p;
2515 
2516   if (h == NULL)
2517     return FALSE;
2518 
2519   while (h->root.type == bfd_link_hash_indirect
2520 	 || h->root.type == bfd_link_hash_warning)
2521     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2522 
2523   /* If it was forced local, then clearly it's not dynamic.  */
2524   if (h->dynindx == -1)
2525     return FALSE;
2526   if (h->forced_local)
2527     return FALSE;
2528 
2529   /* Identify the cases where name binding rules say that a
2530      visible symbol resolves locally.  */
2531   binding_stays_local_p = info->executable || info->symbolic;
2532 
2533   switch (ELF_ST_VISIBILITY (h->other))
2534     {
2535     case STV_INTERNAL:
2536     case STV_HIDDEN:
2537       return FALSE;
2538 
2539     case STV_PROTECTED:
2540       /* Proper resolution for function pointer equality may require
2541 	 that these symbols perhaps be resolved dynamically, even though
2542 	 we should be resolving them to the current module.  */
2543       if (!ignore_protected || h->type != STT_FUNC)
2544 	binding_stays_local_p = TRUE;
2545       break;
2546 
2547     default:
2548       break;
2549     }
2550 
2551   /* If it isn't defined locally, then clearly it's dynamic.  */
2552   if (!h->def_regular)
2553     return TRUE;
2554 
2555   /* Otherwise, the symbol is dynamic if binding rules don't tell
2556      us that it remains local.  */
2557   return !binding_stays_local_p;
2558 }
2559 
2560 /* Return true if the symbol referred to by H should be considered
2561    to resolve local to the current module, and false otherwise.  Differs
2562    from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2563    undefined symbols and weak symbols.  */
2564 
2565 bfd_boolean
_bfd_elf_symbol_refs_local_p(struct elf_link_hash_entry * h,struct bfd_link_info * info,bfd_boolean local_protected)2566 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2567 			      struct bfd_link_info *info,
2568 			      bfd_boolean local_protected)
2569 {
2570   /* If it's a local sym, of course we resolve locally.  */
2571   if (h == NULL)
2572     return TRUE;
2573 
2574   /* Common symbols that become definitions don't get the DEF_REGULAR
2575      flag set, so test it first, and don't bail out.  */
2576   if (ELF_COMMON_DEF_P (h))
2577     /* Do nothing.  */;
2578   /* If we don't have a definition in a regular file, then we can't
2579      resolve locally.  The sym is either undefined or dynamic.  */
2580   else if (!h->def_regular)
2581     return FALSE;
2582 
2583   /* Forced local symbols resolve locally.  */
2584   if (h->forced_local)
2585     return TRUE;
2586 
2587   /* As do non-dynamic symbols.  */
2588   if (h->dynindx == -1)
2589     return TRUE;
2590 
2591   /* At this point, we know the symbol is defined and dynamic.  In an
2592      executable it must resolve locally, likewise when building symbolic
2593      shared libraries.  */
2594   if (info->executable || info->symbolic)
2595     return TRUE;
2596 
2597   /* Now deal with defined dynamic symbols in shared libraries.  Ones
2598      with default visibility might not resolve locally.  */
2599   if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2600     return FALSE;
2601 
2602   /* However, STV_HIDDEN or STV_INTERNAL ones must be local.  */
2603   if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2604     return TRUE;
2605 
2606   /* STV_PROTECTED non-function symbols are local.  */
2607   if (h->type != STT_FUNC)
2608     return TRUE;
2609 
2610   /* Function pointer equality tests may require that STV_PROTECTED
2611      symbols be treated as dynamic symbols, even when we know that the
2612      dynamic linker will resolve them locally.  */
2613   return local_protected;
2614 }
2615 
2616 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2617    aligned.  Returns the first TLS output section.  */
2618 
2619 struct bfd_section *
_bfd_elf_tls_setup(bfd * obfd,struct bfd_link_info * info)2620 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2621 {
2622   struct bfd_section *sec, *tls;
2623   unsigned int align = 0;
2624 
2625   for (sec = obfd->sections; sec != NULL; sec = sec->next)
2626     if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2627       break;
2628   tls = sec;
2629 
2630   for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2631     if (sec->alignment_power > align)
2632       align = sec->alignment_power;
2633 
2634   elf_hash_table (info)->tls_sec = tls;
2635 
2636   /* Ensure the alignment of the first section is the largest alignment,
2637      so that the tls segment starts aligned.  */
2638   if (tls != NULL)
2639     tls->alignment_power = align;
2640 
2641   return tls;
2642 }
2643 
2644 /* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
2645 static bfd_boolean
is_global_data_symbol_definition(bfd * abfd ATTRIBUTE_UNUSED,Elf_Internal_Sym * sym)2646 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2647 				  Elf_Internal_Sym *sym)
2648 {
2649   /* Local symbols do not count, but target specific ones might.  */
2650   if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2651       && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2652     return FALSE;
2653 
2654   /* Function symbols do not count.  */
2655   if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2656     return FALSE;
2657 
2658   /* If the section is undefined, then so is the symbol.  */
2659   if (sym->st_shndx == SHN_UNDEF)
2660     return FALSE;
2661 
2662   /* If the symbol is defined in the common section, then
2663      it is a common definition and so does not count.  */
2664   if (sym->st_shndx == SHN_COMMON)
2665     return FALSE;
2666 
2667   /* If the symbol is in a target specific section then we
2668      must rely upon the backend to tell us what it is.  */
2669   if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2670     /* FIXME - this function is not coded yet:
2671 
2672        return _bfd_is_global_symbol_definition (abfd, sym);
2673 
2674        Instead for now assume that the definition is not global,
2675        Even if this is wrong, at least the linker will behave
2676        in the same way that it used to do.  */
2677     return FALSE;
2678 
2679   return TRUE;
2680 }
2681 
2682 /* Search the symbol table of the archive element of the archive ABFD
2683    whose archive map contains a mention of SYMDEF, and determine if
2684    the symbol is defined in this element.  */
2685 static bfd_boolean
elf_link_is_defined_archive_symbol(bfd * abfd,carsym * symdef)2686 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2687 {
2688   Elf_Internal_Shdr * hdr;
2689   bfd_size_type symcount;
2690   bfd_size_type extsymcount;
2691   bfd_size_type extsymoff;
2692   Elf_Internal_Sym *isymbuf;
2693   Elf_Internal_Sym *isym;
2694   Elf_Internal_Sym *isymend;
2695   bfd_boolean result;
2696 
2697   abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2698   if (abfd == NULL)
2699     return FALSE;
2700 
2701   if (! bfd_check_format (abfd, bfd_object))
2702     return FALSE;
2703 
2704   /* If we have already included the element containing this symbol in the
2705      link then we do not need to include it again.  Just claim that any symbol
2706      it contains is not a definition, so that our caller will not decide to
2707      (re)include this element.  */
2708   if (abfd->archive_pass)
2709     return FALSE;
2710 
2711   /* Select the appropriate symbol table.  */
2712   if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2713     hdr = &elf_tdata (abfd)->symtab_hdr;
2714   else
2715     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2716 
2717   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2718 
2719   /* The sh_info field of the symtab header tells us where the
2720      external symbols start.  We don't care about the local symbols.  */
2721   if (elf_bad_symtab (abfd))
2722     {
2723       extsymcount = symcount;
2724       extsymoff = 0;
2725     }
2726   else
2727     {
2728       extsymcount = symcount - hdr->sh_info;
2729       extsymoff = hdr->sh_info;
2730     }
2731 
2732   if (extsymcount == 0)
2733     return FALSE;
2734 
2735   /* Read in the symbol table.  */
2736   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2737 				  NULL, NULL, NULL);
2738   if (isymbuf == NULL)
2739     return FALSE;
2740 
2741   /* Scan the symbol table looking for SYMDEF.  */
2742   result = FALSE;
2743   for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2744     {
2745       const char *name;
2746 
2747       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2748 					      isym->st_name);
2749       if (name == NULL)
2750 	break;
2751 
2752       if (strcmp (name, symdef->name) == 0)
2753 	{
2754 	  result = is_global_data_symbol_definition (abfd, isym);
2755 	  break;
2756 	}
2757     }
2758 
2759   free (isymbuf);
2760 
2761   return result;
2762 }
2763 
2764 /* Add an entry to the .dynamic table.  */
2765 
2766 bfd_boolean
_bfd_elf_add_dynamic_entry(struct bfd_link_info * info,bfd_vma tag,bfd_vma val)2767 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2768 			    bfd_vma tag,
2769 			    bfd_vma val)
2770 {
2771   struct elf_link_hash_table *hash_table;
2772   const struct elf_backend_data *bed;
2773   asection *s;
2774   bfd_size_type newsize;
2775   bfd_byte *newcontents;
2776   Elf_Internal_Dyn dyn;
2777 
2778   hash_table = elf_hash_table (info);
2779   if (! is_elf_hash_table (hash_table))
2780     return FALSE;
2781 
2782   if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL)
2783     _bfd_error_handler
2784       (_("warning: creating a DT_TEXTREL in a shared object."));
2785 
2786   bed = get_elf_backend_data (hash_table->dynobj);
2787   s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2788   BFD_ASSERT (s != NULL);
2789 
2790   newsize = s->size + bed->s->sizeof_dyn;
2791   newcontents = bfd_realloc (s->contents, newsize);
2792   if (newcontents == NULL)
2793     return FALSE;
2794 
2795   dyn.d_tag = tag;
2796   dyn.d_un.d_val = val;
2797   bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2798 
2799   s->size = newsize;
2800   s->contents = newcontents;
2801 
2802   return TRUE;
2803 }
2804 
2805 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2806    otherwise just check whether one already exists.  Returns -1 on error,
2807    1 if a DT_NEEDED tag already exists, and 0 on success.  */
2808 
2809 static int
elf_add_dt_needed_tag(bfd * abfd,struct bfd_link_info * info,const char * soname,bfd_boolean do_it)2810 elf_add_dt_needed_tag (bfd *abfd,
2811 		       struct bfd_link_info *info,
2812 		       const char *soname,
2813 		       bfd_boolean do_it)
2814 {
2815   struct elf_link_hash_table *hash_table;
2816   bfd_size_type oldsize;
2817   bfd_size_type strindex;
2818 
2819   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
2820     return -1;
2821 
2822   hash_table = elf_hash_table (info);
2823   oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2824   strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2825   if (strindex == (bfd_size_type) -1)
2826     return -1;
2827 
2828   if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2829     {
2830       asection *sdyn;
2831       const struct elf_backend_data *bed;
2832       bfd_byte *extdyn;
2833 
2834       bed = get_elf_backend_data (hash_table->dynobj);
2835       sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2836       if (sdyn != NULL)
2837 	for (extdyn = sdyn->contents;
2838 	     extdyn < sdyn->contents + sdyn->size;
2839 	     extdyn += bed->s->sizeof_dyn)
2840 	  {
2841 	    Elf_Internal_Dyn dyn;
2842 
2843 	    bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2844 	    if (dyn.d_tag == DT_NEEDED
2845 		&& dyn.d_un.d_val == strindex)
2846 	      {
2847 		_bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2848 		return 1;
2849 	      }
2850 	  }
2851     }
2852 
2853   if (do_it)
2854     {
2855       if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
2856 	return -1;
2857 
2858       if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2859 	return -1;
2860     }
2861   else
2862     /* We were just checking for existence of the tag.  */
2863     _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2864 
2865   return 0;
2866 }
2867 
2868 /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols
2869    belonging to NOT_NEEDED to bfd_link_hash_new.  We know there are no
2870    references from regular objects to these symbols.
2871 
2872    ??? Should we do something about references from other dynamic
2873    obects?  If not, we potentially lose some warnings about undefined
2874    symbols.  But how can we recover the initial undefined / undefweak
2875    state?  */
2876 
2877 struct elf_smash_syms_data
2878 {
2879   bfd *not_needed;
2880   struct elf_link_hash_table *htab;
2881   bfd_boolean twiddled;
2882 };
2883 
2884 static bfd_boolean
elf_smash_syms(struct elf_link_hash_entry * h,void * data)2885 elf_smash_syms (struct elf_link_hash_entry *h, void *data)
2886 {
2887   struct elf_smash_syms_data *inf = (struct elf_smash_syms_data *) data;
2888   struct bfd_link_hash_entry *bh;
2889 
2890   switch (h->root.type)
2891     {
2892     default:
2893     case bfd_link_hash_new:
2894       return TRUE;
2895 
2896     case bfd_link_hash_undefined:
2897       if (h->root.u.undef.abfd != inf->not_needed)
2898 	return TRUE;
2899       if (h->root.u.undef.weak != NULL
2900 	  && h->root.u.undef.weak != inf->not_needed)
2901 	{
2902 	  /* Symbol was undefweak in u.undef.weak bfd, and has become
2903 	     undefined in as-needed lib.  Restore weak.  */
2904 	  h->root.type = bfd_link_hash_undefweak;
2905 	  h->root.u.undef.abfd = h->root.u.undef.weak;
2906 	  if (h->root.u.undef.next != NULL
2907 	      || inf->htab->root.undefs_tail == &h->root)
2908 	    inf->twiddled = TRUE;
2909 	  return TRUE;
2910 	}
2911       break;
2912 
2913     case bfd_link_hash_undefweak:
2914       if (h->root.u.undef.abfd != inf->not_needed)
2915 	return TRUE;
2916       break;
2917 
2918     case bfd_link_hash_defined:
2919     case bfd_link_hash_defweak:
2920       if (h->root.u.def.section->owner != inf->not_needed)
2921 	return TRUE;
2922       break;
2923 
2924     case bfd_link_hash_common:
2925       if (h->root.u.c.p->section->owner != inf->not_needed)
2926 	return TRUE;
2927       break;
2928 
2929     case bfd_link_hash_warning:
2930     case bfd_link_hash_indirect:
2931       elf_smash_syms ((struct elf_link_hash_entry *) h->root.u.i.link, data);
2932       if (h->root.u.i.link->type != bfd_link_hash_new)
2933 	return TRUE;
2934       if (h->root.u.i.link->u.undef.abfd != inf->not_needed)
2935 	return TRUE;
2936       break;
2937     }
2938 
2939   /* There is no way we can undo symbol table state from defined or
2940      defweak back to undefined.  */
2941   if (h->ref_regular)
2942     abort ();
2943 
2944   /* Set sym back to newly created state, but keep undef.next if it is
2945      being used as a list pointer.  */
2946   bh = h->root.u.undef.next;
2947   if (bh == &h->root)
2948     bh = NULL;
2949   if (bh != NULL || inf->htab->root.undefs_tail == &h->root)
2950     inf->twiddled = TRUE;
2951   (*inf->htab->root.table.newfunc) (&h->root.root,
2952 				    &inf->htab->root.table,
2953 				    h->root.root.string);
2954   h->root.u.undef.next = bh;
2955   h->root.u.undef.abfd = inf->not_needed;
2956   h->non_elf = 0;
2957   return TRUE;
2958 }
2959 
2960 /* Sort symbol by value and section.  */
2961 static int
elf_sort_symbol(const void * arg1,const void * arg2)2962 elf_sort_symbol (const void *arg1, const void *arg2)
2963 {
2964   const struct elf_link_hash_entry *h1;
2965   const struct elf_link_hash_entry *h2;
2966   bfd_signed_vma vdiff;
2967 
2968   h1 = *(const struct elf_link_hash_entry **) arg1;
2969   h2 = *(const struct elf_link_hash_entry **) arg2;
2970   vdiff = h1->root.u.def.value - h2->root.u.def.value;
2971   if (vdiff != 0)
2972     return vdiff > 0 ? 1 : -1;
2973   else
2974     {
2975       long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2976       if (sdiff != 0)
2977 	return sdiff > 0 ? 1 : -1;
2978     }
2979   return 0;
2980 }
2981 
2982 /* This function is used to adjust offsets into .dynstr for
2983    dynamic symbols.  This is called via elf_link_hash_traverse.  */
2984 
2985 static bfd_boolean
elf_adjust_dynstr_offsets(struct elf_link_hash_entry * h,void * data)2986 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2987 {
2988   struct elf_strtab_hash *dynstr = data;
2989 
2990   if (h->root.type == bfd_link_hash_warning)
2991     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2992 
2993   if (h->dynindx != -1)
2994     h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2995   return TRUE;
2996 }
2997 
2998 /* Assign string offsets in .dynstr, update all structures referencing
2999    them.  */
3000 
3001 static bfd_boolean
elf_finalize_dynstr(bfd * output_bfd,struct bfd_link_info * info)3002 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3003 {
3004   struct elf_link_hash_table *hash_table = elf_hash_table (info);
3005   struct elf_link_local_dynamic_entry *entry;
3006   struct elf_strtab_hash *dynstr = hash_table->dynstr;
3007   bfd *dynobj = hash_table->dynobj;
3008   asection *sdyn;
3009   bfd_size_type size;
3010   const struct elf_backend_data *bed;
3011   bfd_byte *extdyn;
3012 
3013   _bfd_elf_strtab_finalize (dynstr);
3014   size = _bfd_elf_strtab_size (dynstr);
3015 
3016   bed = get_elf_backend_data (dynobj);
3017   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3018   BFD_ASSERT (sdyn != NULL);
3019 
3020   /* Update all .dynamic entries referencing .dynstr strings.  */
3021   for (extdyn = sdyn->contents;
3022        extdyn < sdyn->contents + sdyn->size;
3023        extdyn += bed->s->sizeof_dyn)
3024     {
3025       Elf_Internal_Dyn dyn;
3026 
3027       bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3028       switch (dyn.d_tag)
3029 	{
3030 	case DT_STRSZ:
3031 	  dyn.d_un.d_val = size;
3032 	  break;
3033 	case DT_NEEDED:
3034 	case DT_SONAME:
3035 	case DT_RPATH:
3036 	case DT_RUNPATH:
3037 	case DT_FILTER:
3038 	case DT_AUXILIARY:
3039 	  dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3040 	  break;
3041 	default:
3042 	  continue;
3043 	}
3044       bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3045     }
3046 
3047   /* Now update local dynamic symbols.  */
3048   for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3049     entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3050 						  entry->isym.st_name);
3051 
3052   /* And the rest of dynamic symbols.  */
3053   elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3054 
3055   /* Adjust version definitions.  */
3056   if (elf_tdata (output_bfd)->cverdefs)
3057     {
3058       asection *s;
3059       bfd_byte *p;
3060       bfd_size_type i;
3061       Elf_Internal_Verdef def;
3062       Elf_Internal_Verdaux defaux;
3063 
3064       s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3065       p = s->contents;
3066       do
3067 	{
3068 	  _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3069 				   &def);
3070 	  p += sizeof (Elf_External_Verdef);
3071 	  if (def.vd_aux != sizeof (Elf_External_Verdef))
3072 	    continue;
3073 	  for (i = 0; i < def.vd_cnt; ++i)
3074 	    {
3075 	      _bfd_elf_swap_verdaux_in (output_bfd,
3076 					(Elf_External_Verdaux *) p, &defaux);
3077 	      defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3078 							defaux.vda_name);
3079 	      _bfd_elf_swap_verdaux_out (output_bfd,
3080 					 &defaux, (Elf_External_Verdaux *) p);
3081 	      p += sizeof (Elf_External_Verdaux);
3082 	    }
3083 	}
3084       while (def.vd_next);
3085     }
3086 
3087   /* Adjust version references.  */
3088   if (elf_tdata (output_bfd)->verref)
3089     {
3090       asection *s;
3091       bfd_byte *p;
3092       bfd_size_type i;
3093       Elf_Internal_Verneed need;
3094       Elf_Internal_Vernaux needaux;
3095 
3096       s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3097       p = s->contents;
3098       do
3099 	{
3100 	  _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3101 				    &need);
3102 	  need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3103 	  _bfd_elf_swap_verneed_out (output_bfd, &need,
3104 				     (Elf_External_Verneed *) p);
3105 	  p += sizeof (Elf_External_Verneed);
3106 	  for (i = 0; i < need.vn_cnt; ++i)
3107 	    {
3108 	      _bfd_elf_swap_vernaux_in (output_bfd,
3109 					(Elf_External_Vernaux *) p, &needaux);
3110 	      needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3111 							 needaux.vna_name);
3112 	      _bfd_elf_swap_vernaux_out (output_bfd,
3113 					 &needaux,
3114 					 (Elf_External_Vernaux *) p);
3115 	      p += sizeof (Elf_External_Vernaux);
3116 	    }
3117 	}
3118       while (need.vn_next);
3119     }
3120 
3121   return TRUE;
3122 }
3123 
3124 /* Add symbols from an ELF object file to the linker hash table.  */
3125 
3126 static bfd_boolean
elf_link_add_object_symbols(bfd * abfd,struct bfd_link_info * info)3127 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3128 {
3129   bfd_boolean (*add_symbol_hook)
3130     (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
3131      const char **, flagword *, asection **, bfd_vma *);
3132   bfd_boolean (*check_relocs)
3133     (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
3134   bfd_boolean (*check_directives)
3135     (bfd *, struct bfd_link_info *);
3136   bfd_boolean collect;
3137   Elf_Internal_Shdr *hdr;
3138   bfd_size_type symcount;
3139   bfd_size_type extsymcount;
3140   bfd_size_type extsymoff;
3141   struct elf_link_hash_entry **sym_hash;
3142   bfd_boolean dynamic;
3143   Elf_External_Versym *extversym = NULL;
3144   Elf_External_Versym *ever;
3145   struct elf_link_hash_entry *weaks;
3146   struct elf_link_hash_entry **nondeflt_vers = NULL;
3147   bfd_size_type nondeflt_vers_cnt = 0;
3148   Elf_Internal_Sym *isymbuf = NULL;
3149   Elf_Internal_Sym *isym;
3150   Elf_Internal_Sym *isymend;
3151   const struct elf_backend_data *bed;
3152   bfd_boolean add_needed;
3153   struct elf_link_hash_table * hash_table;
3154   bfd_size_type amt;
3155 
3156   hash_table = elf_hash_table (info);
3157 
3158   bed = get_elf_backend_data (abfd);
3159   add_symbol_hook = bed->elf_add_symbol_hook;
3160   collect = bed->collect;
3161 
3162   if ((abfd->flags & DYNAMIC) == 0)
3163     dynamic = FALSE;
3164   else
3165     {
3166       dynamic = TRUE;
3167 
3168       /* You can't use -r against a dynamic object.  Also, there's no
3169 	 hope of using a dynamic object which does not exactly match
3170 	 the format of the output file.  */
3171       if (info->relocatable
3172 	  || !is_elf_hash_table (hash_table)
3173 	  || hash_table->root.creator != abfd->xvec)
3174 	{
3175 	  if (info->relocatable)
3176 	    bfd_set_error (bfd_error_invalid_operation);
3177 	  else
3178 	    bfd_set_error (bfd_error_wrong_format);
3179 	  goto error_return;
3180 	}
3181     }
3182 
3183   /* As a GNU extension, any input sections which are named
3184      .gnu.warning.SYMBOL are treated as warning symbols for the given
3185      symbol.  This differs from .gnu.warning sections, which generate
3186      warnings when they are included in an output file.  */
3187   /* As a MirOS extension, any input sections which are named
3188      .gnu.warning.*SYMBOL are generating a warning on each use of the
3189      symbol, even if it is not contained in the shared library.  */
3190   if (info->executable)
3191     {
3192       asection *s;
3193 
3194       for (s = abfd->sections; s != NULL; s = s->next)
3195 	{
3196 	  const char *name;
3197 
3198 	  name = bfd_get_section_name (abfd, s);
3199 	  if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3200 	    {
3201 	      char *msg;
3202 	      bfd_size_type sz;
3203 
3204 	      name += sizeof ".gnu.warning." - 1;
3205 
3206 	      /* If the name starts with '*', always warn.  */
3207 	      if (*name == '*')
3208 		{
3209 		  ++name;
3210 		}
3211 	      else
3212 		/* fall through */
3213 
3214 	      /* If this is a shared object, then look up the symbol
3215 		 in the hash table.  If it is there, and it is already
3216 		 been defined, then we will not be using the entry
3217 		 from this shared object, so we don't need to warn.
3218 		 FIXME: If we see the definition in a regular object
3219 		 later on, we will warn, but we shouldn't.  The only
3220 		 fix is to keep track of what warnings we are supposed
3221 		 to emit, and then handle them all at the end of the
3222 		 link.  */
3223 	      if (dynamic)
3224 		{
3225 		  struct elf_link_hash_entry *h;
3226 
3227 		  h = elf_link_hash_lookup (hash_table, name,
3228 					    FALSE, FALSE, TRUE);
3229 
3230 		  /* FIXME: What about bfd_link_hash_common?  */
3231 		  if (h != NULL
3232 		      && (h->root.type == bfd_link_hash_defined
3233 			  || h->root.type == bfd_link_hash_defweak))
3234 		    {
3235 		      /* We don't want to issue this warning.  Clobber
3236 			 the section size so that the warning does not
3237 			 get copied into the output file.  */
3238 		      s->size = 0;
3239 		      continue;
3240 		    }
3241 		}
3242 
3243 	      sz = s->size;
3244 	      msg = bfd_alloc (abfd, sz + 1);
3245 	      if (msg == NULL)
3246 		goto error_return;
3247 
3248 	      if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3249 		goto error_return;
3250 
3251 	      msg[sz] = '\0';
3252 
3253 	      if (! (_bfd_generic_link_add_one_symbol
3254 		     (info, abfd, name, BSF_WARNING, s, 0, msg,
3255 		      FALSE, collect, NULL)))
3256 		goto error_return;
3257 
3258 	      if (! info->relocatable)
3259 		{
3260 		  /* Clobber the section size so that the warning does
3261 		     not get copied into the output file.  */
3262 		  s->size = 0;
3263 
3264 		  /* Also set SEC_EXCLUDE, so that symbols defined in
3265 		     the warning section don't get copied to the output.  */
3266 		  s->flags |= SEC_EXCLUDE;
3267 		}
3268 	    }
3269 	}
3270     }
3271 
3272   add_needed = TRUE;
3273   if (! dynamic)
3274     {
3275       /* If we are creating a shared library, create all the dynamic
3276 	 sections immediately.  We need to attach them to something,
3277 	 so we attach them to this BFD, provided it is the right
3278 	 format.  FIXME: If there are no input BFD's of the same
3279 	 format as the output, we can't make a shared library.  */
3280       if (info->shared
3281 	  && is_elf_hash_table (hash_table)
3282 	  && hash_table->root.creator == abfd->xvec
3283 	  && ! hash_table->dynamic_sections_created)
3284 	{
3285 	  if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3286 	    goto error_return;
3287 	}
3288     }
3289   else if (!is_elf_hash_table (hash_table))
3290     goto error_return;
3291   else
3292     {
3293       asection *s;
3294       const char *soname = NULL;
3295       struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3296       int ret;
3297 
3298       /* ld --just-symbols and dynamic objects don't mix very well.
3299 	 Test for --just-symbols by looking at info set up by
3300 	 _bfd_elf_link_just_syms.  */
3301       if ((s = abfd->sections) != NULL
3302 	  && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3303 	goto error_return;
3304 
3305       /* If this dynamic lib was specified on the command line with
3306 	 --as-needed in effect, then we don't want to add a DT_NEEDED
3307 	 tag unless the lib is actually used.  Similary for libs brought
3308 	 in by another lib's DT_NEEDED.  When --no-add-needed is used
3309 	 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3310 	 any dynamic library in DT_NEEDED tags in the dynamic lib at
3311 	 all.  */
3312       add_needed = (elf_dyn_lib_class (abfd)
3313 		    & (DYN_AS_NEEDED | DYN_DT_NEEDED
3314 		       | DYN_NO_NEEDED)) == 0;
3315 
3316       s = bfd_get_section_by_name (abfd, ".dynamic");
3317       if (s != NULL)
3318 	{
3319 	  bfd_byte *dynbuf;
3320 	  bfd_byte *extdyn;
3321 	  int elfsec;
3322 	  unsigned long shlink;
3323 
3324 	  if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3325 	    goto error_free_dyn;
3326 
3327 	  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3328 	  if (elfsec == -1)
3329 	    goto error_free_dyn;
3330 	  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3331 
3332 	  for (extdyn = dynbuf;
3333 	       extdyn < dynbuf + s->size;
3334 	       extdyn += bed->s->sizeof_dyn)
3335 	    {
3336 	      Elf_Internal_Dyn dyn;
3337 
3338 	      bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3339 	      if (dyn.d_tag == DT_SONAME)
3340 		{
3341 		  unsigned int tagv = dyn.d_un.d_val;
3342 		  soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3343 		  if (soname == NULL)
3344 		    goto error_free_dyn;
3345 		}
3346 	      if (dyn.d_tag == DT_NEEDED)
3347 		{
3348 		  struct bfd_link_needed_list *n, **pn;
3349 		  char *fnm, *anm;
3350 		  unsigned int tagv = dyn.d_un.d_val;
3351 
3352 		  amt = sizeof (struct bfd_link_needed_list);
3353 		  n = bfd_alloc (abfd, amt);
3354 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3355 		  if (n == NULL || fnm == NULL)
3356 		    goto error_free_dyn;
3357 		  amt = strlen (fnm) + 1;
3358 		  anm = bfd_alloc (abfd, amt);
3359 		  if (anm == NULL)
3360 		    goto error_free_dyn;
3361 		  memcpy (anm, fnm, amt);
3362 		  n->name = anm;
3363 		  n->by = abfd;
3364 		  n->next = NULL;
3365 		  for (pn = & hash_table->needed;
3366 		       *pn != NULL;
3367 		       pn = &(*pn)->next)
3368 		    ;
3369 		  *pn = n;
3370 		}
3371 	      if (dyn.d_tag == DT_RUNPATH)
3372 		{
3373 		  struct bfd_link_needed_list *n, **pn;
3374 		  char *fnm, *anm;
3375 		  unsigned int tagv = dyn.d_un.d_val;
3376 
3377 		  amt = sizeof (struct bfd_link_needed_list);
3378 		  n = bfd_alloc (abfd, amt);
3379 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3380 		  if (n == NULL || fnm == NULL)
3381 		    goto error_free_dyn;
3382 		  amt = strlen (fnm) + 1;
3383 		  anm = bfd_alloc (abfd, amt);
3384 		  if (anm == NULL)
3385 		    goto error_free_dyn;
3386 		  memcpy (anm, fnm, amt);
3387 		  n->name = anm;
3388 		  n->by = abfd;
3389 		  n->next = NULL;
3390 		  for (pn = & runpath;
3391 		       *pn != NULL;
3392 		       pn = &(*pn)->next)
3393 		    ;
3394 		  *pn = n;
3395 		}
3396 	      /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
3397 	      if (!runpath && dyn.d_tag == DT_RPATH)
3398 		{
3399 		  struct bfd_link_needed_list *n, **pn;
3400 		  char *fnm, *anm;
3401 		  unsigned int tagv = dyn.d_un.d_val;
3402 
3403 		  amt = sizeof (struct bfd_link_needed_list);
3404 		  n = bfd_alloc (abfd, amt);
3405 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3406 		  if (n == NULL || fnm == NULL)
3407 		    goto error_free_dyn;
3408 		  amt = strlen (fnm) + 1;
3409 		  anm = bfd_alloc (abfd, amt);
3410 		  if (anm == NULL)
3411 		    {
3412 		    error_free_dyn:
3413 		      free (dynbuf);
3414 		      goto error_return;
3415 		    }
3416 		  memcpy (anm, fnm, amt);
3417 		  n->name = anm;
3418 		  n->by = abfd;
3419 		  n->next = NULL;
3420 		  for (pn = & rpath;
3421 		       *pn != NULL;
3422 		       pn = &(*pn)->next)
3423 		    ;
3424 		  *pn = n;
3425 		}
3426 	    }
3427 
3428 	  free (dynbuf);
3429 	}
3430 
3431       /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
3432 	 frees all more recently bfd_alloc'd blocks as well.  */
3433       if (runpath)
3434 	rpath = runpath;
3435 
3436       if (rpath)
3437 	{
3438 	  struct bfd_link_needed_list **pn;
3439 	  for (pn = & hash_table->runpath;
3440 	       *pn != NULL;
3441 	       pn = &(*pn)->next)
3442 	    ;
3443 	  *pn = rpath;
3444 	}
3445 
3446       /* We do not want to include any of the sections in a dynamic
3447 	 object in the output file.  We hack by simply clobbering the
3448 	 list of sections in the BFD.  This could be handled more
3449 	 cleanly by, say, a new section flag; the existing
3450 	 SEC_NEVER_LOAD flag is not the one we want, because that one
3451 	 still implies that the section takes up space in the output
3452 	 file.  */
3453       bfd_section_list_clear (abfd);
3454 
3455       /* Find the name to use in a DT_NEEDED entry that refers to this
3456 	 object.  If the object has a DT_SONAME entry, we use it.
3457 	 Otherwise, if the generic linker stuck something in
3458 	 elf_dt_name, we use that.  Otherwise, we just use the file
3459 	 name.  */
3460       if (soname == NULL || *soname == '\0')
3461 	{
3462 	  soname = elf_dt_name (abfd);
3463 	  if (soname == NULL || *soname == '\0')
3464 	    soname = bfd_get_filename (abfd);
3465 	}
3466 
3467       /* Save the SONAME because sometimes the linker emulation code
3468 	 will need to know it.  */
3469       elf_dt_name (abfd) = soname;
3470 
3471       ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3472       if (ret < 0)
3473 	goto error_return;
3474 
3475       /* If we have already included this dynamic object in the
3476 	 link, just ignore it.  There is no reason to include a
3477 	 particular dynamic object more than once.  */
3478       if (ret > 0)
3479 	return TRUE;
3480     }
3481 
3482   /* If this is a dynamic object, we always link against the .dynsym
3483      symbol table, not the .symtab symbol table.  The dynamic linker
3484      will only see the .dynsym symbol table, so there is no reason to
3485      look at .symtab for a dynamic object.  */
3486 
3487   if (! dynamic || elf_dynsymtab (abfd) == 0)
3488     hdr = &elf_tdata (abfd)->symtab_hdr;
3489   else
3490     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3491 
3492   symcount = hdr->sh_size / bed->s->sizeof_sym;
3493 
3494   /* The sh_info field of the symtab header tells us where the
3495      external symbols start.  We don't care about the local symbols at
3496      this point.  */
3497   if (elf_bad_symtab (abfd))
3498     {
3499       extsymcount = symcount;
3500       extsymoff = 0;
3501     }
3502   else
3503     {
3504       extsymcount = symcount - hdr->sh_info;
3505       extsymoff = hdr->sh_info;
3506     }
3507 
3508   sym_hash = NULL;
3509   if (extsymcount != 0)
3510     {
3511       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3512 				      NULL, NULL, NULL);
3513       if (isymbuf == NULL)
3514 	goto error_return;
3515 
3516       /* We store a pointer to the hash table entry for each external
3517 	 symbol.  */
3518       amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3519       sym_hash = bfd_alloc (abfd, amt);
3520       if (sym_hash == NULL)
3521 	goto error_free_sym;
3522       elf_sym_hashes (abfd) = sym_hash;
3523     }
3524 
3525   if (dynamic)
3526     {
3527       /* Read in any version definitions.  */
3528       if (!_bfd_elf_slurp_version_tables (abfd,
3529 					  info->default_imported_symver))
3530 	goto error_free_sym;
3531 
3532       /* Read in the symbol versions, but don't bother to convert them
3533 	 to internal format.  */
3534       if (elf_dynversym (abfd) != 0)
3535 	{
3536 	  Elf_Internal_Shdr *versymhdr;
3537 
3538 	  versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3539 	  extversym = bfd_malloc (versymhdr->sh_size);
3540 	  if (extversym == NULL)
3541 	    goto error_free_sym;
3542 	  amt = versymhdr->sh_size;
3543 	  if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3544 	      || bfd_bread (extversym, amt, abfd) != amt)
3545 	    goto error_free_vers;
3546 	}
3547     }
3548 
3549   weaks = NULL;
3550 
3551   ever = extversym != NULL ? extversym + extsymoff : NULL;
3552   for (isym = isymbuf, isymend = isymbuf + extsymcount;
3553        isym < isymend;
3554        isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3555     {
3556       int bind;
3557       bfd_vma value;
3558       asection *sec, *new_sec;
3559       flagword flags;
3560       const char *name;
3561       struct elf_link_hash_entry *h;
3562       bfd_boolean definition;
3563       bfd_boolean size_change_ok;
3564       bfd_boolean type_change_ok;
3565       bfd_boolean new_weakdef;
3566       bfd_boolean override;
3567       unsigned int old_alignment;
3568       bfd *old_bfd;
3569 
3570       override = FALSE;
3571 
3572       flags = BSF_NO_FLAGS;
3573       sec = NULL;
3574       value = isym->st_value;
3575       *sym_hash = NULL;
3576 
3577       bind = ELF_ST_BIND (isym->st_info);
3578       if (bind == STB_LOCAL)
3579 	{
3580 	  /* This should be impossible, since ELF requires that all
3581 	     global symbols follow all local symbols, and that sh_info
3582 	     point to the first global symbol.  Unfortunately, Irix 5
3583 	     screws this up.  */
3584 	  continue;
3585 	}
3586       else if (bind == STB_GLOBAL)
3587 	{
3588 	  if (isym->st_shndx != SHN_UNDEF
3589 	      && isym->st_shndx != SHN_COMMON)
3590 	    flags = BSF_GLOBAL;
3591 	}
3592       else if (bind == STB_WEAK)
3593 	flags = BSF_WEAK;
3594       else
3595 	{
3596 	  /* Leave it up to the processor backend.  */
3597 	}
3598 
3599       if (isym->st_shndx == SHN_UNDEF)
3600 	sec = bfd_und_section_ptr;
3601       else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3602 	{
3603 	  sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3604 	  if (sec == NULL)
3605 	    sec = bfd_abs_section_ptr;
3606 	  else if (sec->kept_section)
3607 	    {
3608 	      /* Symbols from discarded section are undefined, and have
3609 		 default visibility.  */
3610 	      sec = bfd_und_section_ptr;
3611 	      isym->st_shndx = SHN_UNDEF;
3612 	      isym->st_other = STV_DEFAULT
3613 			       | (isym->st_other & ~ ELF_ST_VISIBILITY(-1));
3614 	    }
3615 	  else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3616 	    value -= sec->vma;
3617 	}
3618       else if (isym->st_shndx == SHN_ABS)
3619 	sec = bfd_abs_section_ptr;
3620       else if (isym->st_shndx == SHN_COMMON)
3621 	{
3622 	  sec = bfd_com_section_ptr;
3623 	  /* What ELF calls the size we call the value.  What ELF
3624 	     calls the value we call the alignment.  */
3625 	  value = isym->st_size;
3626 	}
3627       else
3628 	{
3629 	  /* Leave it up to the processor backend.  */
3630 	}
3631 
3632       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3633 					      isym->st_name);
3634       if (name == NULL)
3635 	goto error_free_vers;
3636 
3637       if (isym->st_shndx == SHN_COMMON
3638 	  && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3639 	{
3640 	  asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3641 
3642 	  if (tcomm == NULL)
3643 	    {
3644 	      tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3645 						   (SEC_ALLOC
3646 						    | SEC_IS_COMMON
3647 						    | SEC_LINKER_CREATED
3648 						    | SEC_THREAD_LOCAL));
3649 	      if (tcomm == NULL)
3650 		goto error_free_vers;
3651 	    }
3652 	  sec = tcomm;
3653 	}
3654       else if (add_symbol_hook)
3655 	{
3656 	  if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3657 				    &value))
3658 	    goto error_free_vers;
3659 
3660 	  /* The hook function sets the name to NULL if this symbol
3661 	     should be skipped for some reason.  */
3662 	  if (name == NULL)
3663 	    continue;
3664 	}
3665 
3666       /* Sanity check that all possibilities were handled.  */
3667       if (sec == NULL)
3668 	{
3669 	  bfd_set_error (bfd_error_bad_value);
3670 	  goto error_free_vers;
3671 	}
3672 
3673       if (bfd_is_und_section (sec)
3674 	  || bfd_is_com_section (sec))
3675 	definition = FALSE;
3676       else
3677 	definition = TRUE;
3678 
3679       size_change_ok = FALSE;
3680       type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3681       old_alignment = 0;
3682       old_bfd = NULL;
3683       new_sec = sec;
3684 
3685       if (is_elf_hash_table (hash_table))
3686 	{
3687 	  Elf_Internal_Versym iver;
3688 	  unsigned int vernum = 0;
3689 	  bfd_boolean skip;
3690 
3691 	  if (ever == NULL)
3692 	    {
3693 	      if (info->default_imported_symver)
3694 		/* Use the default symbol version created earlier.  */
3695 		iver.vs_vers = elf_tdata (abfd)->cverdefs;
3696 	      else
3697 		iver.vs_vers = 0;
3698 	    }
3699 	  else
3700 	    _bfd_elf_swap_versym_in (abfd, ever, &iver);
3701 
3702 	  vernum = iver.vs_vers & VERSYM_VERSION;
3703 
3704 	  /* If this is a hidden symbol, or if it is not version
3705 	     1, we append the version name to the symbol name.
3706 	     However, we do not modify a non-hidden absolute
3707 	     symbol, because it might be the version symbol
3708 	     itself.  FIXME: What if it isn't?  */
3709 	  if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3710 	      || (vernum > 1 && ! bfd_is_abs_section (sec)))
3711 	    {
3712 	      const char *verstr;
3713 	      size_t namelen, verlen, newlen;
3714 	      char *newname, *p;
3715 
3716 	      if (isym->st_shndx != SHN_UNDEF)
3717 		{
3718 		  if (vernum > elf_tdata (abfd)->cverdefs)
3719 		    verstr = NULL;
3720 		  else if (vernum > 1)
3721 		    verstr =
3722 		      elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3723 		  else
3724 		    verstr = "";
3725 
3726 		  if (verstr == NULL)
3727 		    {
3728 		      (*_bfd_error_handler)
3729 			(_("%B: %s: invalid version %u (max %d)"),
3730 			 abfd, name, vernum,
3731 			 elf_tdata (abfd)->cverdefs);
3732 		      bfd_set_error (bfd_error_bad_value);
3733 		      goto error_free_vers;
3734 		    }
3735 		}
3736 	      else
3737 		{
3738 		  /* We cannot simply test for the number of
3739 		     entries in the VERNEED section since the
3740 		     numbers for the needed versions do not start
3741 		     at 0.  */
3742 		  Elf_Internal_Verneed *t;
3743 
3744 		  verstr = NULL;
3745 		  for (t = elf_tdata (abfd)->verref;
3746 		       t != NULL;
3747 		       t = t->vn_nextref)
3748 		    {
3749 		      Elf_Internal_Vernaux *a;
3750 
3751 		      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3752 			{
3753 			  if (a->vna_other == vernum)
3754 			    {
3755 			      verstr = a->vna_nodename;
3756 			      break;
3757 			    }
3758 			}
3759 		      if (a != NULL)
3760 			break;
3761 		    }
3762 		  if (verstr == NULL)
3763 		    {
3764 		      (*_bfd_error_handler)
3765 			(_("%B: %s: invalid needed version %d"),
3766 			 abfd, name, vernum);
3767 		      bfd_set_error (bfd_error_bad_value);
3768 		      goto error_free_vers;
3769 		    }
3770 		}
3771 
3772 	      namelen = strlen (name);
3773 	      verlen = strlen (verstr);
3774 	      newlen = namelen + verlen + 2;
3775 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3776 		  && isym->st_shndx != SHN_UNDEF)
3777 		++newlen;
3778 
3779 	      newname = bfd_alloc (abfd, newlen);
3780 	      if (newname == NULL)
3781 		goto error_free_vers;
3782 	      memcpy (newname, name, namelen);
3783 	      p = newname + namelen;
3784 	      *p++ = ELF_VER_CHR;
3785 	      /* If this is a defined non-hidden version symbol,
3786 		 we add another @ to the name.  This indicates the
3787 		 default version of the symbol.  */
3788 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3789 		  && isym->st_shndx != SHN_UNDEF)
3790 		*p++ = ELF_VER_CHR;
3791 	      memcpy (p, verstr, verlen + 1);
3792 
3793 	      name = newname;
3794 	    }
3795 
3796 	  if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
3797 				      &value, &old_alignment,
3798 				      sym_hash, &skip, &override,
3799 				      &type_change_ok, &size_change_ok))
3800 	    goto error_free_vers;
3801 
3802 	  if (skip)
3803 	    continue;
3804 
3805 	  if (override)
3806 	    definition = FALSE;
3807 
3808 	  h = *sym_hash;
3809 	  while (h->root.type == bfd_link_hash_indirect
3810 		 || h->root.type == bfd_link_hash_warning)
3811 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
3812 
3813 	  /* Remember the old alignment if this is a common symbol, so
3814 	     that we don't reduce the alignment later on.  We can't
3815 	     check later, because _bfd_generic_link_add_one_symbol
3816 	     will set a default for the alignment which we want to
3817 	     override. We also remember the old bfd where the existing
3818 	     definition comes from.  */
3819 	  switch (h->root.type)
3820 	    {
3821 	    default:
3822 	      break;
3823 
3824 	    case bfd_link_hash_defined:
3825 	    case bfd_link_hash_defweak:
3826 	      old_bfd = h->root.u.def.section->owner;
3827 	      break;
3828 
3829 	    case bfd_link_hash_common:
3830 	      old_bfd = h->root.u.c.p->section->owner;
3831 	      old_alignment = h->root.u.c.p->alignment_power;
3832 	      break;
3833 	    }
3834 
3835 	  if (elf_tdata (abfd)->verdef != NULL
3836 	      && ! override
3837 	      && vernum > 1
3838 	      && definition)
3839 	    h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3840 	}
3841 
3842       if (! (_bfd_generic_link_add_one_symbol
3843 	     (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3844 	      (struct bfd_link_hash_entry **) sym_hash)))
3845 	goto error_free_vers;
3846 
3847       h = *sym_hash;
3848       while (h->root.type == bfd_link_hash_indirect
3849 	     || h->root.type == bfd_link_hash_warning)
3850 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
3851       *sym_hash = h;
3852 
3853       new_weakdef = FALSE;
3854       if (dynamic
3855 	  && definition
3856 	  && (flags & BSF_WEAK) != 0
3857 	  && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3858 	  && is_elf_hash_table (hash_table)
3859 	  && h->u.weakdef == NULL)
3860 	{
3861 	  /* Keep a list of all weak defined non function symbols from
3862 	     a dynamic object, using the weakdef field.  Later in this
3863 	     function we will set the weakdef field to the correct
3864 	     value.  We only put non-function symbols from dynamic
3865 	     objects on this list, because that happens to be the only
3866 	     time we need to know the normal symbol corresponding to a
3867 	     weak symbol, and the information is time consuming to
3868 	     figure out.  If the weakdef field is not already NULL,
3869 	     then this symbol was already defined by some previous
3870 	     dynamic object, and we will be using that previous
3871 	     definition anyhow.  */
3872 
3873 	  h->u.weakdef = weaks;
3874 	  weaks = h;
3875 	  new_weakdef = TRUE;
3876 	}
3877 
3878       /* Set the alignment of a common symbol.  */
3879       if ((isym->st_shndx == SHN_COMMON
3880 	   || bfd_is_com_section (sec))
3881 	  && h->root.type == bfd_link_hash_common)
3882 	{
3883 	  unsigned int align;
3884 
3885 	  if (isym->st_shndx == SHN_COMMON)
3886 	    align = bfd_log2 (isym->st_value);
3887 	  else
3888 	    {
3889 	      /* The new symbol is a common symbol in a shared object.
3890 		 We need to get the alignment from the section.  */
3891 	      align = new_sec->alignment_power;
3892 	    }
3893 	  if (align > old_alignment
3894 	      /* Permit an alignment power of zero if an alignment of one
3895 		 is specified and no other alignments have been specified.  */
3896 	      || (isym->st_value == 1 && old_alignment == 0))
3897 	    h->root.u.c.p->alignment_power = align;
3898 	  else
3899 	    h->root.u.c.p->alignment_power = old_alignment;
3900 	}
3901 
3902       if (is_elf_hash_table (hash_table))
3903 	{
3904 	  bfd_boolean dynsym;
3905 
3906 	  /* Check the alignment when a common symbol is involved. This
3907 	     can change when a common symbol is overridden by a normal
3908 	     definition or a common symbol is ignored due to the old
3909 	     normal definition. We need to make sure the maximum
3910 	     alignment is maintained.  */
3911 	  if ((old_alignment || isym->st_shndx == SHN_COMMON)
3912 	      && h->root.type != bfd_link_hash_common)
3913 	    {
3914 	      unsigned int common_align;
3915 	      unsigned int normal_align;
3916 	      unsigned int symbol_align;
3917 	      bfd *normal_bfd;
3918 	      bfd *common_bfd;
3919 
3920 	      symbol_align = ffs (h->root.u.def.value) - 1;
3921 	      if (h->root.u.def.section->owner != NULL
3922 		  && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3923 		{
3924 		  normal_align = h->root.u.def.section->alignment_power;
3925 		  if (normal_align > symbol_align)
3926 		    normal_align = symbol_align;
3927 		}
3928 	      else
3929 		normal_align = symbol_align;
3930 
3931 	      if (old_alignment)
3932 		{
3933 		  common_align = old_alignment;
3934 		  common_bfd = old_bfd;
3935 		  normal_bfd = abfd;
3936 		}
3937 	      else
3938 		{
3939 		  common_align = bfd_log2 (isym->st_value);
3940 		  common_bfd = abfd;
3941 		  normal_bfd = old_bfd;
3942 		}
3943 
3944 	      if (normal_align < common_align)
3945 		(*_bfd_error_handler)
3946 		  (_("Warning: alignment %u of symbol `%s' in %B"
3947 		     " is smaller than %u in %B"),
3948 		   normal_bfd, common_bfd,
3949 		   1 << normal_align, name, 1 << common_align);
3950 	    }
3951 
3952 	  /* Remember the symbol size and type.  */
3953 	  if (isym->st_size != 0
3954 	      && (definition || h->size == 0))
3955 	    {
3956 	      if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3957 		(*_bfd_error_handler)
3958 		  (_("Warning: size of symbol `%s' changed"
3959 		     " from %lu in %B to %lu in %B"),
3960 		   old_bfd, abfd,
3961 		   name, (unsigned long) h->size,
3962 		   (unsigned long) isym->st_size);
3963 
3964 	      h->size = isym->st_size;
3965 	    }
3966 
3967 	  /* If this is a common symbol, then we always want H->SIZE
3968 	     to be the size of the common symbol.  The code just above
3969 	     won't fix the size if a common symbol becomes larger.  We
3970 	     don't warn about a size change here, because that is
3971 	     covered by --warn-common.  */
3972 	  if (h->root.type == bfd_link_hash_common)
3973 	    h->size = h->root.u.c.size;
3974 
3975 	  if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3976 	      && (definition || h->type == STT_NOTYPE))
3977 	    {
3978 	      if (h->type != STT_NOTYPE
3979 		  && h->type != ELF_ST_TYPE (isym->st_info)
3980 		  && ! type_change_ok)
3981 		(*_bfd_error_handler)
3982 		  (_("Warning: type of symbol `%s' changed"
3983 		     " from %d to %d in %B"),
3984 		   abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3985 
3986 	      h->type = ELF_ST_TYPE (isym->st_info);
3987 	    }
3988 
3989 	  /* If st_other has a processor-specific meaning, specific
3990 	     code might be needed here. We never merge the visibility
3991 	     attribute with the one from a dynamic object.  */
3992 	  if (bed->elf_backend_merge_symbol_attribute)
3993 	    (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3994 							dynamic);
3995 
3996 	  /* If this symbol has default visibility and the user has requested
3997 	     we not re-export it, then mark it as hidden.  */
3998 	  if (definition && !dynamic
3999 	      && (abfd->no_export
4000 		  || (abfd->my_archive && abfd->my_archive->no_export))
4001 	      && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4002 	    isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1));
4003 
4004 	  if (isym->st_other != 0 && !dynamic)
4005 	    {
4006 	      unsigned char hvis, symvis, other, nvis;
4007 
4008 	      /* Take the balance of OTHER from the definition.  */
4009 	      other = (definition ? isym->st_other : h->other);
4010 	      other &= ~ ELF_ST_VISIBILITY (-1);
4011 
4012 	      /* Combine visibilities, using the most constraining one.  */
4013 	      hvis   = ELF_ST_VISIBILITY (h->other);
4014 	      symvis = ELF_ST_VISIBILITY (isym->st_other);
4015 	      if (! hvis)
4016 		nvis = symvis;
4017 	      else if (! symvis)
4018 		nvis = hvis;
4019 	      else
4020 		nvis = hvis < symvis ? hvis : symvis;
4021 
4022 	      h->other = other | nvis;
4023 	    }
4024 
4025 	  /* Set a flag in the hash table entry indicating the type of
4026 	     reference or definition we just found.  Keep a count of
4027 	     the number of dynamic symbols we find.  A dynamic symbol
4028 	     is one which is referenced or defined by both a regular
4029 	     object and a shared object.  */
4030 	  dynsym = FALSE;
4031 	  if (! dynamic)
4032 	    {
4033 	      if (! definition)
4034 		{
4035 		  h->ref_regular = 1;
4036 		  if (bind != STB_WEAK)
4037 		    h->ref_regular_nonweak = 1;
4038 		}
4039 	      else
4040 		h->def_regular = 1;
4041 	      if (! info->executable
4042 		  || h->def_dynamic
4043 		  || h->ref_dynamic)
4044 		dynsym = TRUE;
4045 	    }
4046 	  else
4047 	    {
4048 	      if (! definition)
4049 		h->ref_dynamic = 1;
4050 	      else
4051 		h->def_dynamic = 1;
4052 	      if (h->def_regular
4053 		  || h->ref_regular
4054 		  || (h->u.weakdef != NULL
4055 		      && ! new_weakdef
4056 		      && h->u.weakdef->dynindx != -1))
4057 		dynsym = TRUE;
4058 	    }
4059 
4060 	  /* Check to see if we need to add an indirect symbol for
4061 	     the default name.  */
4062 	  if (definition || h->root.type == bfd_link_hash_common)
4063 	    if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4064 					      &sec, &value, &dynsym,
4065 					      override))
4066 	      goto error_free_vers;
4067 
4068 	  if (definition && !dynamic)
4069 	    {
4070 	      char *p = strchr (name, ELF_VER_CHR);
4071 	      if (p != NULL && p[1] != ELF_VER_CHR)
4072 		{
4073 		  /* Queue non-default versions so that .symver x, x@FOO
4074 		     aliases can be checked.  */
4075 		  if (! nondeflt_vers)
4076 		    {
4077 		      amt = (isymend - isym + 1)
4078 			    * sizeof (struct elf_link_hash_entry *);
4079 		      nondeflt_vers = bfd_malloc (amt);
4080 		    }
4081 		  nondeflt_vers [nondeflt_vers_cnt++] = h;
4082 		}
4083 	    }
4084 
4085 	  if (dynsym && h->dynindx == -1)
4086 	    {
4087 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
4088 		goto error_free_vers;
4089 	      if (h->u.weakdef != NULL
4090 		  && ! new_weakdef
4091 		  && h->u.weakdef->dynindx == -1)
4092 		{
4093 		  if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4094 		    goto error_free_vers;
4095 		}
4096 	    }
4097 	  else if (dynsym && h->dynindx != -1)
4098 	    /* If the symbol already has a dynamic index, but
4099 	       visibility says it should not be visible, turn it into
4100 	       a local symbol.  */
4101 	    switch (ELF_ST_VISIBILITY (h->other))
4102 	      {
4103 	      case STV_INTERNAL:
4104 	      case STV_HIDDEN:
4105 		(*bed->elf_backend_hide_symbol) (info, h, TRUE);
4106 		dynsym = FALSE;
4107 		break;
4108 	      }
4109 
4110 	  if (!add_needed
4111 	      && definition
4112 	      && dynsym
4113 	      && h->ref_regular)
4114 	    {
4115 	      int ret;
4116 	      const char *soname = elf_dt_name (abfd);
4117 
4118 	      /* A symbol from a library loaded via DT_NEEDED of some
4119 		 other library is referenced by a regular object.
4120 		 Add a DT_NEEDED entry for it.  Issue an error if
4121 		 --no-add-needed is used.  */
4122 	      if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4123 		{
4124 		  (*_bfd_error_handler)
4125 		    (_("%s: invalid DSO for symbol `%s' definition"),
4126 		     abfd, name);
4127 		  bfd_set_error (bfd_error_bad_value);
4128 		  goto error_free_vers;
4129 		}
4130 
4131 	      elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4132 
4133 	      add_needed = TRUE;
4134 	      ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4135 	      if (ret < 0)
4136 		goto error_free_vers;
4137 
4138 	      BFD_ASSERT (ret == 0);
4139 	    }
4140 	}
4141     }
4142 
4143   /* Now that all the symbols from this input file are created, handle
4144      .symver foo, foo@BAR such that any relocs against foo become foo@BAR.  */
4145   if (nondeflt_vers != NULL)
4146     {
4147       bfd_size_type cnt, symidx;
4148 
4149       for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4150 	{
4151 	  struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4152 	  char *shortname, *p;
4153 
4154 	  p = strchr (h->root.root.string, ELF_VER_CHR);
4155 	  if (p == NULL
4156 	      || (h->root.type != bfd_link_hash_defined
4157 		  && h->root.type != bfd_link_hash_defweak))
4158 	    continue;
4159 
4160 	  amt = p - h->root.root.string;
4161 	  shortname = bfd_malloc (amt + 1);
4162 	  memcpy (shortname, h->root.root.string, amt);
4163 	  shortname[amt] = '\0';
4164 
4165 	  hi = (struct elf_link_hash_entry *)
4166 	       bfd_link_hash_lookup (&hash_table->root, shortname,
4167 				     FALSE, FALSE, FALSE);
4168 	  if (hi != NULL
4169 	      && hi->root.type == h->root.type
4170 	      && hi->root.u.def.value == h->root.u.def.value
4171 	      && hi->root.u.def.section == h->root.u.def.section)
4172 	    {
4173 	      (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4174 	      hi->root.type = bfd_link_hash_indirect;
4175 	      hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4176 	      (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
4177 	      sym_hash = elf_sym_hashes (abfd);
4178 	      if (sym_hash)
4179 		for (symidx = 0; symidx < extsymcount; ++symidx)
4180 		  if (sym_hash[symidx] == hi)
4181 		    {
4182 		      sym_hash[symidx] = h;
4183 		      break;
4184 		    }
4185 	    }
4186 	  free (shortname);
4187 	}
4188       free (nondeflt_vers);
4189       nondeflt_vers = NULL;
4190     }
4191 
4192   if (extversym != NULL)
4193     {
4194       free (extversym);
4195       extversym = NULL;
4196     }
4197 
4198   if (isymbuf != NULL)
4199     free (isymbuf);
4200   isymbuf = NULL;
4201 
4202   if (!add_needed
4203       && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4204     {
4205       /* Remove symbols defined in an as-needed shared lib that wasn't
4206 	 needed.  */
4207       struct elf_smash_syms_data inf;
4208       inf.not_needed = abfd;
4209       inf.htab = hash_table;
4210       inf.twiddled = FALSE;
4211       elf_link_hash_traverse (hash_table, elf_smash_syms, &inf);
4212       if (inf.twiddled)
4213 	bfd_link_repair_undef_list (&hash_table->root);
4214       weaks = NULL;
4215     }
4216 
4217   /* Now set the weakdefs field correctly for all the weak defined
4218      symbols we found.  The only way to do this is to search all the
4219      symbols.  Since we only need the information for non functions in
4220      dynamic objects, that's the only time we actually put anything on
4221      the list WEAKS.  We need this information so that if a regular
4222      object refers to a symbol defined weakly in a dynamic object, the
4223      real symbol in the dynamic object is also put in the dynamic
4224      symbols; we also must arrange for both symbols to point to the
4225      same memory location.  We could handle the general case of symbol
4226      aliasing, but a general symbol alias can only be generated in
4227      assembler code, handling it correctly would be very time
4228      consuming, and other ELF linkers don't handle general aliasing
4229      either.  */
4230   if (weaks != NULL)
4231     {
4232       struct elf_link_hash_entry **hpp;
4233       struct elf_link_hash_entry **hppend;
4234       struct elf_link_hash_entry **sorted_sym_hash;
4235       struct elf_link_hash_entry *h;
4236       size_t sym_count;
4237 
4238       /* Since we have to search the whole symbol list for each weak
4239 	 defined symbol, search time for N weak defined symbols will be
4240 	 O(N^2). Binary search will cut it down to O(NlogN).  */
4241       amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4242       sorted_sym_hash = bfd_malloc (amt);
4243       if (sorted_sym_hash == NULL)
4244 	goto error_return;
4245       sym_hash = sorted_sym_hash;
4246       hpp = elf_sym_hashes (abfd);
4247       hppend = hpp + extsymcount;
4248       sym_count = 0;
4249       for (; hpp < hppend; hpp++)
4250 	{
4251 	  h = *hpp;
4252 	  if (h != NULL
4253 	      && h->root.type == bfd_link_hash_defined
4254 	      && h->type != STT_FUNC)
4255 	    {
4256 	      *sym_hash = h;
4257 	      sym_hash++;
4258 	      sym_count++;
4259 	    }
4260 	}
4261 
4262       qsort (sorted_sym_hash, sym_count,
4263 	     sizeof (struct elf_link_hash_entry *),
4264 	     elf_sort_symbol);
4265 
4266       while (weaks != NULL)
4267 	{
4268 	  struct elf_link_hash_entry *hlook;
4269 	  asection *slook;
4270 	  bfd_vma vlook;
4271 	  long ilook;
4272 	  size_t i, j, idx;
4273 
4274 	  hlook = weaks;
4275 	  weaks = hlook->u.weakdef;
4276 	  hlook->u.weakdef = NULL;
4277 
4278 	  BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4279 		      || hlook->root.type == bfd_link_hash_defweak
4280 		      || hlook->root.type == bfd_link_hash_common
4281 		      || hlook->root.type == bfd_link_hash_indirect);
4282 	  slook = hlook->root.u.def.section;
4283 	  vlook = hlook->root.u.def.value;
4284 
4285 	  ilook = -1;
4286 	  i = 0;
4287 	  j = sym_count;
4288 	  while (i < j)
4289 	    {
4290 	      bfd_signed_vma vdiff;
4291 	      idx = (i + j) / 2;
4292 	      h = sorted_sym_hash [idx];
4293 	      vdiff = vlook - h->root.u.def.value;
4294 	      if (vdiff < 0)
4295 		j = idx;
4296 	      else if (vdiff > 0)
4297 		i = idx + 1;
4298 	      else
4299 		{
4300 		  long sdiff = slook->id - h->root.u.def.section->id;
4301 		  if (sdiff < 0)
4302 		    j = idx;
4303 		  else if (sdiff > 0)
4304 		    i = idx + 1;
4305 		  else
4306 		    {
4307 		      ilook = idx;
4308 		      break;
4309 		    }
4310 		}
4311 	    }
4312 
4313 	  /* We didn't find a value/section match.  */
4314 	  if (ilook == -1)
4315 	    continue;
4316 
4317 	  for (i = ilook; i < sym_count; i++)
4318 	    {
4319 	      h = sorted_sym_hash [i];
4320 
4321 	      /* Stop if value or section doesn't match.  */
4322 	      if (h->root.u.def.value != vlook
4323 		  || h->root.u.def.section != slook)
4324 		break;
4325 	      else if (h != hlook)
4326 		{
4327 		  hlook->u.weakdef = h;
4328 
4329 		  /* If the weak definition is in the list of dynamic
4330 		     symbols, make sure the real definition is put
4331 		     there as well.  */
4332 		  if (hlook->dynindx != -1 && h->dynindx == -1)
4333 		    {
4334 		      if (! bfd_elf_link_record_dynamic_symbol (info, h))
4335 			goto error_return;
4336 		    }
4337 
4338 		  /* If the real definition is in the list of dynamic
4339 		     symbols, make sure the weak definition is put
4340 		     there as well.  If we don't do this, then the
4341 		     dynamic loader might not merge the entries for the
4342 		     real definition and the weak definition.  */
4343 		  if (h->dynindx != -1 && hlook->dynindx == -1)
4344 		    {
4345 		      if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4346 			goto error_return;
4347 		    }
4348 		  break;
4349 		}
4350 	    }
4351 	}
4352 
4353       free (sorted_sym_hash);
4354     }
4355 
4356   check_directives = get_elf_backend_data (abfd)->check_directives;
4357   if (check_directives)
4358     check_directives (abfd, info);
4359 
4360   /* If this object is the same format as the output object, and it is
4361      not a shared library, then let the backend look through the
4362      relocs.
4363 
4364      This is required to build global offset table entries and to
4365      arrange for dynamic relocs.  It is not required for the
4366      particular common case of linking non PIC code, even when linking
4367      against shared libraries, but unfortunately there is no way of
4368      knowing whether an object file has been compiled PIC or not.
4369      Looking through the relocs is not particularly time consuming.
4370      The problem is that we must either (1) keep the relocs in memory,
4371      which causes the linker to require additional runtime memory or
4372      (2) read the relocs twice from the input file, which wastes time.
4373      This would be a good case for using mmap.
4374 
4375      I have no idea how to handle linking PIC code into a file of a
4376      different format.  It probably can't be done.  */
4377   check_relocs = get_elf_backend_data (abfd)->check_relocs;
4378   if (! dynamic
4379       && is_elf_hash_table (hash_table)
4380       && hash_table->root.creator == abfd->xvec
4381       && check_relocs != NULL)
4382     {
4383       asection *o;
4384 
4385       for (o = abfd->sections; o != NULL; o = o->next)
4386 	{
4387 	  Elf_Internal_Rela *internal_relocs;
4388 	  bfd_boolean ok;
4389 
4390 	  if ((o->flags & SEC_RELOC) == 0
4391 	      || o->reloc_count == 0
4392 	      || ((info->strip == strip_all || info->strip == strip_debugger)
4393 		  && (o->flags & SEC_DEBUGGING) != 0)
4394 	      || bfd_is_abs_section (o->output_section))
4395 	    continue;
4396 
4397 	  internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4398 						       info->keep_memory);
4399 	  if (internal_relocs == NULL)
4400 	    goto error_return;
4401 
4402 	  ok = (*check_relocs) (abfd, info, o, internal_relocs);
4403 
4404 	  if (elf_section_data (o)->relocs != internal_relocs)
4405 	    free (internal_relocs);
4406 
4407 	  if (! ok)
4408 	    goto error_return;
4409 	}
4410     }
4411 
4412   /* If this is a non-traditional link, try to optimize the handling
4413      of the .stab/.stabstr sections.  */
4414   if (! dynamic
4415       && ! info->traditional_format
4416       && is_elf_hash_table (hash_table)
4417       && (info->strip != strip_all && info->strip != strip_debugger))
4418     {
4419       asection *stabstr;
4420 
4421       stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4422       if (stabstr != NULL)
4423 	{
4424 	  bfd_size_type string_offset = 0;
4425 	  asection *stab;
4426 
4427 	  for (stab = abfd->sections; stab; stab = stab->next)
4428 	    if (strncmp (".stab", stab->name, 5) == 0
4429 		&& (!stab->name[5] ||
4430 		    (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4431 		&& (stab->flags & SEC_MERGE) == 0
4432 		&& !bfd_is_abs_section (stab->output_section))
4433 	      {
4434 		struct bfd_elf_section_data *secdata;
4435 
4436 		secdata = elf_section_data (stab);
4437 		if (! _bfd_link_section_stabs (abfd,
4438 					       &hash_table->stab_info,
4439 					       stab, stabstr,
4440 					       &secdata->sec_info,
4441 					       &string_offset))
4442 		  goto error_return;
4443 		if (secdata->sec_info)
4444 		  stab->sec_info_type = ELF_INFO_TYPE_STABS;
4445 	    }
4446 	}
4447     }
4448 
4449   if (is_elf_hash_table (hash_table) && add_needed)
4450     {
4451       /* Add this bfd to the loaded list.  */
4452       struct elf_link_loaded_list *n;
4453 
4454       n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4455       if (n == NULL)
4456 	goto error_return;
4457       n->abfd = abfd;
4458       n->next = hash_table->loaded;
4459       hash_table->loaded = n;
4460     }
4461 
4462   return TRUE;
4463 
4464  error_free_vers:
4465   if (nondeflt_vers != NULL)
4466     free (nondeflt_vers);
4467   if (extversym != NULL)
4468     free (extversym);
4469  error_free_sym:
4470   if (isymbuf != NULL)
4471     free (isymbuf);
4472  error_return:
4473   return FALSE;
4474 }
4475 
4476 /* Return the linker hash table entry of a symbol that might be
4477    satisfied by an archive symbol.  Return -1 on error.  */
4478 
4479 struct elf_link_hash_entry *
_bfd_elf_archive_symbol_lookup(bfd * abfd,struct bfd_link_info * info,const char * name)4480 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4481 				struct bfd_link_info *info,
4482 				const char *name)
4483 {
4484   struct elf_link_hash_entry *h;
4485   char *p, *copy;
4486   size_t len, first;
4487 
4488   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4489   if (h != NULL)
4490     return h;
4491 
4492   /* If this is a default version (the name contains @@), look up the
4493      symbol again with only one `@' as well as without the version.
4494      The effect is that references to the symbol with and without the
4495      version will be matched by the default symbol in the archive.  */
4496 
4497   p = strchr (name, ELF_VER_CHR);
4498   if (p == NULL || p[1] != ELF_VER_CHR)
4499     return h;
4500 
4501   /* First check with only one `@'.  */
4502   len = strlen (name);
4503   copy = bfd_alloc (abfd, len);
4504   if (copy == NULL)
4505     return (struct elf_link_hash_entry *) 0 - 1;
4506 
4507   first = p - name + 1;
4508   memcpy (copy, name, first);
4509   memcpy (copy + first, name + first + 1, len - first);
4510 
4511   h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4512   if (h == NULL)
4513     {
4514       /* We also need to check references to the symbol without the
4515 	 version.  */
4516       copy[first - 1] = '\0';
4517       h = elf_link_hash_lookup (elf_hash_table (info), copy,
4518 				FALSE, FALSE, FALSE);
4519     }
4520 
4521   bfd_release (abfd, copy);
4522   return h;
4523 }
4524 
4525 /* Add symbols from an ELF archive file to the linker hash table.  We
4526    don't use _bfd_generic_link_add_archive_symbols because of a
4527    problem which arises on UnixWare.  The UnixWare libc.so is an
4528    archive which includes an entry libc.so.1 which defines a bunch of
4529    symbols.  The libc.so archive also includes a number of other
4530    object files, which also define symbols, some of which are the same
4531    as those defined in libc.so.1.  Correct linking requires that we
4532    consider each object file in turn, and include it if it defines any
4533    symbols we need.  _bfd_generic_link_add_archive_symbols does not do
4534    this; it looks through the list of undefined symbols, and includes
4535    any object file which defines them.  When this algorithm is used on
4536    UnixWare, it winds up pulling in libc.so.1 early and defining a
4537    bunch of symbols.  This means that some of the other objects in the
4538    archive are not included in the link, which is incorrect since they
4539    precede libc.so.1 in the archive.
4540 
4541    Fortunately, ELF archive handling is simpler than that done by
4542    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4543    oddities.  In ELF, if we find a symbol in the archive map, and the
4544    symbol is currently undefined, we know that we must pull in that
4545    object file.
4546 
4547    Unfortunately, we do have to make multiple passes over the symbol
4548    table until nothing further is resolved.  */
4549 
4550 static bfd_boolean
elf_link_add_archive_symbols(bfd * abfd,struct bfd_link_info * info)4551 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4552 {
4553   symindex c;
4554   bfd_boolean *defined = NULL;
4555   bfd_boolean *included = NULL;
4556   carsym *symdefs;
4557   bfd_boolean loop;
4558   bfd_size_type amt;
4559   const struct elf_backend_data *bed;
4560   struct elf_link_hash_entry * (*archive_symbol_lookup)
4561     (bfd *, struct bfd_link_info *, const char *);
4562 
4563   if (! bfd_has_map (abfd))
4564     {
4565       /* An empty archive is a special case.  */
4566       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4567 	return TRUE;
4568       bfd_set_error (bfd_error_no_armap);
4569       return FALSE;
4570     }
4571 
4572   /* Keep track of all symbols we know to be already defined, and all
4573      files we know to be already included.  This is to speed up the
4574      second and subsequent passes.  */
4575   c = bfd_ardata (abfd)->symdef_count;
4576   if (c == 0)
4577     return TRUE;
4578   amt = c;
4579   amt *= sizeof (bfd_boolean);
4580   defined = bfd_zmalloc (amt);
4581   included = bfd_zmalloc (amt);
4582   if (defined == NULL || included == NULL)
4583     goto error_return;
4584 
4585   symdefs = bfd_ardata (abfd)->symdefs;
4586   bed = get_elf_backend_data (abfd);
4587   archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4588 
4589   do
4590     {
4591       file_ptr last;
4592       symindex i;
4593       carsym *symdef;
4594       carsym *symdefend;
4595 
4596       loop = FALSE;
4597       last = -1;
4598 
4599       symdef = symdefs;
4600       symdefend = symdef + c;
4601       for (i = 0; symdef < symdefend; symdef++, i++)
4602 	{
4603 	  struct elf_link_hash_entry *h;
4604 	  bfd *element;
4605 	  struct bfd_link_hash_entry *undefs_tail;
4606 	  symindex mark;
4607 
4608 	  if (defined[i] || included[i])
4609 	    continue;
4610 	  if (symdef->file_offset == last)
4611 	    {
4612 	      included[i] = TRUE;
4613 	      continue;
4614 	    }
4615 
4616 	  h = archive_symbol_lookup (abfd, info, symdef->name);
4617 	  if (h == (struct elf_link_hash_entry *) 0 - 1)
4618 	    goto error_return;
4619 
4620 	  if (h == NULL)
4621 	    continue;
4622 
4623 	  if (h->root.type == bfd_link_hash_common)
4624 	    {
4625 	      /* We currently have a common symbol.  The archive map contains
4626 		 a reference to this symbol, so we may want to include it.  We
4627 		 only want to include it however, if this archive element
4628 		 contains a definition of the symbol, not just another common
4629 		 declaration of it.
4630 
4631 		 Unfortunately some archivers (including GNU ar) will put
4632 		 declarations of common symbols into their archive maps, as
4633 		 well as real definitions, so we cannot just go by the archive
4634 		 map alone.  Instead we must read in the element's symbol
4635 		 table and check that to see what kind of symbol definition
4636 		 this is.  */
4637 	      if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4638 		continue;
4639 	    }
4640 	  else if (h->root.type != bfd_link_hash_undefined)
4641 	    {
4642 	      if (h->root.type != bfd_link_hash_undefweak)
4643 		defined[i] = TRUE;
4644 	      continue;
4645 	    }
4646 
4647 	  /* We need to include this archive member.  */
4648 	  element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4649 	  if (element == NULL)
4650 	    goto error_return;
4651 
4652 	  if (! bfd_check_format (element, bfd_object))
4653 	    goto error_return;
4654 
4655 	  /* Doublecheck that we have not included this object
4656 	     already--it should be impossible, but there may be
4657 	     something wrong with the archive.  */
4658 	  if (element->archive_pass != 0)
4659 	    {
4660 	      bfd_set_error (bfd_error_bad_value);
4661 	      goto error_return;
4662 	    }
4663 	  element->archive_pass = 1;
4664 
4665 	  undefs_tail = info->hash->undefs_tail;
4666 
4667 	  if (! (*info->callbacks->add_archive_element) (info, element,
4668 							 symdef->name))
4669 	    goto error_return;
4670 	  if (! bfd_link_add_symbols (element, info))
4671 	    goto error_return;
4672 
4673 	  /* If there are any new undefined symbols, we need to make
4674 	     another pass through the archive in order to see whether
4675 	     they can be defined.  FIXME: This isn't perfect, because
4676 	     common symbols wind up on undefs_tail and because an
4677 	     undefined symbol which is defined later on in this pass
4678 	     does not require another pass.  This isn't a bug, but it
4679 	     does make the code less efficient than it could be.  */
4680 	  if (undefs_tail != info->hash->undefs_tail)
4681 	    loop = TRUE;
4682 
4683 	  /* Look backward to mark all symbols from this object file
4684 	     which we have already seen in this pass.  */
4685 	  mark = i;
4686 	  do
4687 	    {
4688 	      included[mark] = TRUE;
4689 	      if (mark == 0)
4690 		break;
4691 	      --mark;
4692 	    }
4693 	  while (symdefs[mark].file_offset == symdef->file_offset);
4694 
4695 	  /* We mark subsequent symbols from this object file as we go
4696 	     on through the loop.  */
4697 	  last = symdef->file_offset;
4698 	}
4699     }
4700   while (loop);
4701 
4702   free (defined);
4703   free (included);
4704 
4705   return TRUE;
4706 
4707  error_return:
4708   if (defined != NULL)
4709     free (defined);
4710   if (included != NULL)
4711     free (included);
4712   return FALSE;
4713 }
4714 
4715 /* Given an ELF BFD, add symbols to the global hash table as
4716    appropriate.  */
4717 
4718 bfd_boolean
bfd_elf_link_add_symbols(bfd * abfd,struct bfd_link_info * info)4719 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4720 {
4721   switch (bfd_get_format (abfd))
4722     {
4723     case bfd_object:
4724       return elf_link_add_object_symbols (abfd, info);
4725     case bfd_archive:
4726       return elf_link_add_archive_symbols (abfd, info);
4727     default:
4728       bfd_set_error (bfd_error_wrong_format);
4729       return FALSE;
4730     }
4731 }
4732 
4733 /* This function will be called though elf_link_hash_traverse to store
4734    all hash value of the exported symbols in an array.  */
4735 
4736 static bfd_boolean
elf_collect_hash_codes(struct elf_link_hash_entry * h,void * data)4737 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4738 {
4739   unsigned long **valuep = data;
4740   const char *name;
4741   char *p;
4742   unsigned long ha;
4743   char *alc = NULL;
4744 
4745   if (h->root.type == bfd_link_hash_warning)
4746     h = (struct elf_link_hash_entry *) h->root.u.i.link;
4747 
4748   /* Ignore indirect symbols.  These are added by the versioning code.  */
4749   if (h->dynindx == -1)
4750     return TRUE;
4751 
4752   name = h->root.root.string;
4753   p = strchr (name, ELF_VER_CHR);
4754   if (p != NULL)
4755     {
4756       alc = bfd_malloc (p - name + 1);
4757       memcpy (alc, name, p - name);
4758       alc[p - name] = '\0';
4759       name = alc;
4760     }
4761 
4762   /* Compute the hash value.  */
4763   ha = bfd_elf_hash (name);
4764 
4765   /* Store the found hash value in the array given as the argument.  */
4766   *(*valuep)++ = ha;
4767 
4768   /* And store it in the struct so that we can put it in the hash table
4769      later.  */
4770   h->u.elf_hash_value = ha;
4771 
4772   if (alc != NULL)
4773     free (alc);
4774 
4775   return TRUE;
4776 }
4777 
4778 /* Array used to determine the number of hash table buckets to use
4779    based on the number of symbols there are.  If there are fewer than
4780    3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4781    fewer than 37 we use 17 buckets, and so forth.  We never use more
4782    than 32771 buckets.  */
4783 
4784 static const size_t elf_buckets[] =
4785 {
4786   1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4787   16411, 32771, 0
4788 };
4789 
4790 /* Compute bucket count for hashing table.  We do not use a static set
4791    of possible tables sizes anymore.  Instead we determine for all
4792    possible reasonable sizes of the table the outcome (i.e., the
4793    number of collisions etc) and choose the best solution.  The
4794    weighting functions are not too simple to allow the table to grow
4795    without bounds.  Instead one of the weighting factors is the size.
4796    Therefore the result is always a good payoff between few collisions
4797    (= short chain lengths) and table size.  */
4798 static size_t
compute_bucket_count(struct bfd_link_info * info)4799 compute_bucket_count (struct bfd_link_info *info)
4800 {
4801   size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4802   size_t best_size = 0;
4803   unsigned long int *hashcodes;
4804   unsigned long int *hashcodesp;
4805   unsigned long int i;
4806   bfd_size_type amt;
4807 
4808   /* Compute the hash values for all exported symbols.  At the same
4809      time store the values in an array so that we could use them for
4810      optimizations.  */
4811   amt = dynsymcount;
4812   amt *= sizeof (unsigned long int);
4813   hashcodes = bfd_malloc (amt);
4814   if (hashcodes == NULL)
4815     return 0;
4816   hashcodesp = hashcodes;
4817 
4818   /* Put all hash values in HASHCODES.  */
4819   elf_link_hash_traverse (elf_hash_table (info),
4820 			  elf_collect_hash_codes, &hashcodesp);
4821 
4822   /* We have a problem here.  The following code to optimize the table
4823      size requires an integer type with more the 32 bits.  If
4824      BFD_HOST_U_64_BIT is set we know about such a type.  */
4825 #ifdef BFD_HOST_U_64_BIT
4826   if (info->optimize)
4827     {
4828       unsigned long int nsyms = hashcodesp - hashcodes;
4829       size_t minsize;
4830       size_t maxsize;
4831       BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4832       unsigned long int *counts ;
4833       bfd *dynobj = elf_hash_table (info)->dynobj;
4834       const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4835 
4836       /* Possible optimization parameters: if we have NSYMS symbols we say
4837 	 that the hashing table must at least have NSYMS/4 and at most
4838 	 2*NSYMS buckets.  */
4839       minsize = nsyms / 4;
4840       if (minsize == 0)
4841 	minsize = 1;
4842       best_size = maxsize = nsyms * 2;
4843 
4844       /* Create array where we count the collisions in.  We must use bfd_malloc
4845 	 since the size could be large.  */
4846       amt = maxsize;
4847       amt *= sizeof (unsigned long int);
4848       counts = bfd_malloc (amt);
4849       if (counts == NULL)
4850 	{
4851 	  free (hashcodes);
4852 	  return 0;
4853 	}
4854 
4855       /* Compute the "optimal" size for the hash table.  The criteria is a
4856 	 minimal chain length.  The minor criteria is (of course) the size
4857 	 of the table.  */
4858       for (i = minsize; i < maxsize; ++i)
4859 	{
4860 	  /* Walk through the array of hashcodes and count the collisions.  */
4861 	  BFD_HOST_U_64_BIT max;
4862 	  unsigned long int j;
4863 	  unsigned long int fact;
4864 
4865 	  memset (counts, '\0', i * sizeof (unsigned long int));
4866 
4867 	  /* Determine how often each hash bucket is used.  */
4868 	  for (j = 0; j < nsyms; ++j)
4869 	    ++counts[hashcodes[j] % i];
4870 
4871 	  /* For the weight function we need some information about the
4872 	     pagesize on the target.  This is information need not be 100%
4873 	     accurate.  Since this information is not available (so far) we
4874 	     define it here to a reasonable default value.  If it is crucial
4875 	     to have a better value some day simply define this value.  */
4876 # ifndef BFD_TARGET_PAGESIZE
4877 #  define BFD_TARGET_PAGESIZE	(4096)
4878 # endif
4879 
4880 	  /* We in any case need 2 + NSYMS entries for the size values and
4881 	     the chains.  */
4882 	  max = (2 + nsyms) * (bed->s->arch_size / 8);
4883 
4884 # if 1
4885 	  /* Variant 1: optimize for short chains.  We add the squares
4886 	     of all the chain lengths (which favors many small chain
4887 	     over a few long chains).  */
4888 	  for (j = 0; j < i; ++j)
4889 	    max += counts[j] * counts[j];
4890 
4891 	  /* This adds penalties for the overall size of the table.  */
4892 	  fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4893 	  max *= fact * fact;
4894 # else
4895 	  /* Variant 2: Optimize a lot more for small table.  Here we
4896 	     also add squares of the size but we also add penalties for
4897 	     empty slots (the +1 term).  */
4898 	  for (j = 0; j < i; ++j)
4899 	    max += (1 + counts[j]) * (1 + counts[j]);
4900 
4901 	  /* The overall size of the table is considered, but not as
4902 	     strong as in variant 1, where it is squared.  */
4903 	  fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4904 	  max *= fact;
4905 # endif
4906 
4907 	  /* Compare with current best results.  */
4908 	  if (max < best_chlen)
4909 	    {
4910 	      best_chlen = max;
4911 	      best_size = i;
4912 	    }
4913 	}
4914 
4915       free (counts);
4916     }
4917   else
4918 #endif /* defined (BFD_HOST_U_64_BIT) */
4919     {
4920       /* This is the fallback solution if no 64bit type is available or if we
4921 	 are not supposed to spend much time on optimizations.  We select the
4922 	 bucket count using a fixed set of numbers.  */
4923       for (i = 0; elf_buckets[i] != 0; i++)
4924 	{
4925 	  best_size = elf_buckets[i];
4926 	  if (dynsymcount < elf_buckets[i + 1])
4927 	    break;
4928 	}
4929     }
4930 
4931   /* Free the arrays we needed.  */
4932   free (hashcodes);
4933 
4934   return best_size;
4935 }
4936 
4937 /* Set up the sizes and contents of the ELF dynamic sections.  This is
4938    called by the ELF linker emulation before_allocation routine.  We
4939    must set the sizes of the sections before the linker sets the
4940    addresses of the various sections.  */
4941 
4942 bfd_boolean
bfd_elf_size_dynamic_sections(bfd * output_bfd,const char * soname,const char * rpath,const char * filter_shlib,const char * const * auxiliary_filters,struct bfd_link_info * info,asection ** sinterpptr,struct bfd_elf_version_tree * verdefs)4943 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4944 			       const char *soname,
4945 			       const char *rpath,
4946 			       const char *filter_shlib,
4947 			       const char * const *auxiliary_filters,
4948 			       struct bfd_link_info *info,
4949 			       asection **sinterpptr,
4950 			       struct bfd_elf_version_tree *verdefs)
4951 {
4952   bfd_size_type soname_indx;
4953   bfd *dynobj;
4954   const struct elf_backend_data *bed;
4955   struct elf_assign_sym_version_info asvinfo;
4956 
4957   *sinterpptr = NULL;
4958 
4959   soname_indx = (bfd_size_type) -1;
4960 
4961   if (!is_elf_hash_table (info->hash))
4962     return TRUE;
4963 
4964   elf_tdata (output_bfd)->relro = info->relro;
4965   if (info->execstack)
4966     elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4967   else if (info->noexecstack)
4968     elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4969   else
4970     {
4971       bfd *inputobj;
4972       asection *notesec = NULL;
4973       int exec = 0;
4974 
4975       for (inputobj = info->input_bfds;
4976 	   inputobj;
4977 	   inputobj = inputobj->link_next)
4978 	{
4979 	  asection *s;
4980 
4981 	  if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
4982 	    continue;
4983 	  s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4984 	  if (s)
4985 	    {
4986 	      if (s->flags & SEC_CODE)
4987 		exec = PF_X;
4988 	      notesec = s;
4989 	    }
4990 	  else
4991 	    exec = PF_X;
4992 	}
4993       if (notesec)
4994 	{
4995 	  elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4996 	  if (exec && info->relocatable
4997 	      && notesec->output_section != bfd_abs_section_ptr)
4998 	    notesec->output_section->flags |= SEC_CODE;
4999 	}
5000     }
5001 
5002   /* Any syms created from now on start with -1 in
5003      got.refcount/offset and plt.refcount/offset.  */
5004   elf_hash_table (info)->init_got_refcount
5005     = elf_hash_table (info)->init_got_offset;
5006   elf_hash_table (info)->init_plt_refcount
5007     = elf_hash_table (info)->init_plt_offset;
5008 
5009   /* The backend may have to create some sections regardless of whether
5010      we're dynamic or not.  */
5011   bed = get_elf_backend_data (output_bfd);
5012   if (bed->elf_backend_always_size_sections
5013       && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5014     return FALSE;
5015 
5016   dynobj = elf_hash_table (info)->dynobj;
5017 
5018   /* If there were no dynamic objects in the link, there is nothing to
5019      do here.  */
5020   if (dynobj == NULL)
5021     return TRUE;
5022 
5023   if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5024     return FALSE;
5025 
5026   if (elf_hash_table (info)->dynamic_sections_created)
5027     {
5028       struct elf_info_failed eif;
5029       struct elf_link_hash_entry *h;
5030       asection *dynstr;
5031       struct bfd_elf_version_tree *t;
5032       struct bfd_elf_version_expr *d;
5033       bfd_boolean all_defined;
5034 
5035       *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5036       BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5037 
5038       if (soname != NULL)
5039 	{
5040 	  soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5041 					     soname, TRUE);
5042 	  if (soname_indx == (bfd_size_type) -1
5043 	      || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5044 	    return FALSE;
5045 	}
5046 
5047       if (info->symbolic)
5048 	{
5049 	  if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5050 	    return FALSE;
5051 	  info->flags |= DF_SYMBOLIC;
5052 	}
5053 
5054       if (rpath != NULL)
5055 	{
5056 	  bfd_size_type indx;
5057 
5058 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5059 				      TRUE);
5060 	  if (indx == (bfd_size_type) -1
5061 	      || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5062 	    return FALSE;
5063 
5064 	  if  (info->new_dtags)
5065 	    {
5066 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5067 	      if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5068 		return FALSE;
5069 	    }
5070 	}
5071 
5072       if (filter_shlib != NULL)
5073 	{
5074 	  bfd_size_type indx;
5075 
5076 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5077 				      filter_shlib, TRUE);
5078 	  if (indx == (bfd_size_type) -1
5079 	      || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5080 	    return FALSE;
5081 	}
5082 
5083       if (auxiliary_filters != NULL)
5084 	{
5085 	  const char * const *p;
5086 
5087 	  for (p = auxiliary_filters; *p != NULL; p++)
5088 	    {
5089 	      bfd_size_type indx;
5090 
5091 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5092 					  *p, TRUE);
5093 	      if (indx == (bfd_size_type) -1
5094 		  || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5095 		return FALSE;
5096 	    }
5097 	}
5098 
5099       eif.info = info;
5100       eif.verdefs = verdefs;
5101       eif.failed = FALSE;
5102 
5103       /* If we are supposed to export all symbols into the dynamic symbol
5104 	 table (this is not the normal case), then do so.  */
5105       if (info->export_dynamic)
5106 	{
5107 	  elf_link_hash_traverse (elf_hash_table (info),
5108 				  _bfd_elf_export_symbol,
5109 				  &eif);
5110 	  if (eif.failed)
5111 	    return FALSE;
5112 	}
5113 
5114       /* Make all global versions with definition.  */
5115       for (t = verdefs; t != NULL; t = t->next)
5116 	for (d = t->globals.list; d != NULL; d = d->next)
5117 	  if (!d->symver && d->symbol)
5118 	    {
5119 	      const char *verstr, *name;
5120 	      size_t namelen, verlen, newlen;
5121 	      char *newname, *p;
5122 	      struct elf_link_hash_entry *newh;
5123 
5124 	      name = d->symbol;
5125 	      namelen = strlen (name);
5126 	      verstr = t->name;
5127 	      verlen = strlen (verstr);
5128 	      newlen = namelen + verlen + 3;
5129 
5130 	      newname = bfd_malloc (newlen);
5131 	      if (newname == NULL)
5132 		return FALSE;
5133 	      memcpy (newname, name, namelen);
5134 
5135 	      /* Check the hidden versioned definition.  */
5136 	      p = newname + namelen;
5137 	      *p++ = ELF_VER_CHR;
5138 	      memcpy (p, verstr, verlen + 1);
5139 	      newh = elf_link_hash_lookup (elf_hash_table (info),
5140 					   newname, FALSE, FALSE,
5141 					   FALSE);
5142 	      if (newh == NULL
5143 		  || (newh->root.type != bfd_link_hash_defined
5144 		      && newh->root.type != bfd_link_hash_defweak))
5145 		{
5146 		  /* Check the default versioned definition.  */
5147 		  *p++ = ELF_VER_CHR;
5148 		  memcpy (p, verstr, verlen + 1);
5149 		  newh = elf_link_hash_lookup (elf_hash_table (info),
5150 					       newname, FALSE, FALSE,
5151 					       FALSE);
5152 		}
5153 	      free (newname);
5154 
5155 	      /* Mark this version if there is a definition and it is
5156 		 not defined in a shared object.  */
5157 	      if (newh != NULL
5158 		  && !newh->def_dynamic
5159 		  && (newh->root.type == bfd_link_hash_defined
5160 		      || newh->root.type == bfd_link_hash_defweak))
5161 		d->symver = 1;
5162 	    }
5163 
5164       /* Attach all the symbols to their version information.  */
5165       asvinfo.output_bfd = output_bfd;
5166       asvinfo.info = info;
5167       asvinfo.verdefs = verdefs;
5168       asvinfo.failed = FALSE;
5169 
5170       elf_link_hash_traverse (elf_hash_table (info),
5171 			      _bfd_elf_link_assign_sym_version,
5172 			      &asvinfo);
5173       if (asvinfo.failed)
5174 	return FALSE;
5175 
5176       if (!info->allow_undefined_version)
5177 	{
5178 	  /* Check if all global versions have a definition.  */
5179 	  all_defined = TRUE;
5180 	  for (t = verdefs; t != NULL; t = t->next)
5181 	    for (d = t->globals.list; d != NULL; d = d->next)
5182 	      if (!d->symver && !d->script)
5183 		{
5184 		  (*_bfd_error_handler)
5185 		    (_("%s: undefined version: %s"),
5186 		     d->pattern, t->name);
5187 		  all_defined = FALSE;
5188 		}
5189 
5190 	  if (!all_defined)
5191 	    {
5192 	      bfd_set_error (bfd_error_bad_value);
5193 	      return FALSE;
5194 	    }
5195 	}
5196 
5197       /* Find all symbols which were defined in a dynamic object and make
5198 	 the backend pick a reasonable value for them.  */
5199       elf_link_hash_traverse (elf_hash_table (info),
5200 			      _bfd_elf_adjust_dynamic_symbol,
5201 			      &eif);
5202       if (eif.failed)
5203 	return FALSE;
5204 
5205       /* Add some entries to the .dynamic section.  We fill in some of the
5206 	 values later, in bfd_elf_final_link, but we must add the entries
5207 	 now so that we know the final size of the .dynamic section.  */
5208 
5209       /* If there are initialization and/or finalization functions to
5210 	 call then add the corresponding DT_INIT/DT_FINI entries.  */
5211       h = (info->init_function
5212 	   ? elf_link_hash_lookup (elf_hash_table (info),
5213 				   info->init_function, FALSE,
5214 				   FALSE, FALSE)
5215 	   : NULL);
5216       if (h != NULL
5217 	  && (h->ref_regular
5218 	      || h->def_regular))
5219 	{
5220 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5221 	    return FALSE;
5222 	}
5223       h = (info->fini_function
5224 	   ? elf_link_hash_lookup (elf_hash_table (info),
5225 				   info->fini_function, FALSE,
5226 				   FALSE, FALSE)
5227 	   : NULL);
5228       if (h != NULL
5229 	  && (h->ref_regular
5230 	      || h->def_regular))
5231 	{
5232 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5233 	    return FALSE;
5234 	}
5235 
5236       if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
5237 	{
5238 	  /* DT_PREINIT_ARRAY is not allowed in shared library.  */
5239 	  if (! info->executable)
5240 	    {
5241 	      bfd *sub;
5242 	      asection *o;
5243 
5244 	      for (sub = info->input_bfds; sub != NULL;
5245 		   sub = sub->link_next)
5246 		for (o = sub->sections; o != NULL; o = o->next)
5247 		  if (elf_section_data (o)->this_hdr.sh_type
5248 		      == SHT_PREINIT_ARRAY)
5249 		    {
5250 		      (*_bfd_error_handler)
5251 			(_("%B: .preinit_array section is not allowed in DSO"),
5252 			 sub);
5253 		      break;
5254 		    }
5255 
5256 	      bfd_set_error (bfd_error_nonrepresentable_section);
5257 	      return FALSE;
5258 	    }
5259 
5260 	  if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5261 	      || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5262 	    return FALSE;
5263 	}
5264       if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
5265 	{
5266 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5267 	      || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5268 	    return FALSE;
5269 	}
5270       if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
5271 	{
5272 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5273 	      || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5274 	    return FALSE;
5275 	}
5276 
5277       dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5278       /* If .dynstr is excluded from the link, we don't want any of
5279 	 these tags.  Strictly, we should be checking each section
5280 	 individually;  This quick check covers for the case where
5281 	 someone does a /DISCARD/ : { *(*) }.  */
5282       if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5283 	{
5284 	  bfd_size_type strsize;
5285 
5286 	  strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5287 	  if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
5288 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5289 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5290 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5291 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5292 					      bed->s->sizeof_sym))
5293 	    return FALSE;
5294 	}
5295     }
5296 
5297   /* The backend must work out the sizes of all the other dynamic
5298      sections.  */
5299   if (bed->elf_backend_size_dynamic_sections
5300       && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5301     return FALSE;
5302 
5303   if (elf_hash_table (info)->dynamic_sections_created)
5304     {
5305       unsigned long section_sym_count;
5306       asection *s;
5307 
5308       /* Set up the version definition section.  */
5309       s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5310       BFD_ASSERT (s != NULL);
5311 
5312       /* We may have created additional version definitions if we are
5313 	 just linking a regular application.  */
5314       verdefs = asvinfo.verdefs;
5315 
5316       /* Skip anonymous version tag.  */
5317       if (verdefs != NULL && verdefs->vernum == 0)
5318 	verdefs = verdefs->next;
5319 
5320       if (verdefs == NULL && !info->create_default_symver)
5321 	s->flags |= SEC_EXCLUDE;
5322       else
5323 	{
5324 	  unsigned int cdefs;
5325 	  bfd_size_type size;
5326 	  struct bfd_elf_version_tree *t;
5327 	  bfd_byte *p;
5328 	  Elf_Internal_Verdef def;
5329 	  Elf_Internal_Verdaux defaux;
5330 	  struct bfd_link_hash_entry *bh;
5331 	  struct elf_link_hash_entry *h;
5332 	  const char *name;
5333 
5334 	  cdefs = 0;
5335 	  size = 0;
5336 
5337 	  /* Make space for the base version.  */
5338 	  size += sizeof (Elf_External_Verdef);
5339 	  size += sizeof (Elf_External_Verdaux);
5340 	  ++cdefs;
5341 
5342 	  /* Make space for the default version.  */
5343 	  if (info->create_default_symver)
5344 	    {
5345 	      size += sizeof (Elf_External_Verdef);
5346 	      ++cdefs;
5347 	    }
5348 
5349 	  for (t = verdefs; t != NULL; t = t->next)
5350 	    {
5351 	      struct bfd_elf_version_deps *n;
5352 
5353 	      size += sizeof (Elf_External_Verdef);
5354 	      size += sizeof (Elf_External_Verdaux);
5355 	      ++cdefs;
5356 
5357 	      for (n = t->deps; n != NULL; n = n->next)
5358 		size += sizeof (Elf_External_Verdaux);
5359 	    }
5360 
5361 	  s->size = size;
5362 	  s->contents = bfd_alloc (output_bfd, s->size);
5363 	  if (s->contents == NULL && s->size != 0)
5364 	    return FALSE;
5365 
5366 	  /* Fill in the version definition section.  */
5367 
5368 	  p = s->contents;
5369 
5370 	  def.vd_version = VER_DEF_CURRENT;
5371 	  def.vd_flags = VER_FLG_BASE;
5372 	  def.vd_ndx = 1;
5373 	  def.vd_cnt = 1;
5374 	  if (info->create_default_symver)
5375 	    {
5376 	      def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5377 	      def.vd_next = sizeof (Elf_External_Verdef);
5378 	    }
5379 	  else
5380 	    {
5381 	      def.vd_aux = sizeof (Elf_External_Verdef);
5382 	      def.vd_next = (sizeof (Elf_External_Verdef)
5383 			     + sizeof (Elf_External_Verdaux));
5384 	    }
5385 
5386 	  if (soname_indx != (bfd_size_type) -1)
5387 	    {
5388 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5389 				      soname_indx);
5390 	      def.vd_hash = bfd_elf_hash (soname);
5391 	      defaux.vda_name = soname_indx;
5392 	      name = soname;
5393 	    }
5394 	  else
5395 	    {
5396 	      bfd_size_type indx;
5397 
5398 	      name = lbasename (output_bfd->filename);
5399 	      def.vd_hash = bfd_elf_hash (name);
5400 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5401 					  name, FALSE);
5402 	      if (indx == (bfd_size_type) -1)
5403 		return FALSE;
5404 	      defaux.vda_name = indx;
5405 	    }
5406 	  defaux.vda_next = 0;
5407 
5408 	  _bfd_elf_swap_verdef_out (output_bfd, &def,
5409 				    (Elf_External_Verdef *) p);
5410 	  p += sizeof (Elf_External_Verdef);
5411 	  if (info->create_default_symver)
5412 	    {
5413 	      /* Add a symbol representing this version.  */
5414 	      bh = NULL;
5415 	      if (! (_bfd_generic_link_add_one_symbol
5416 		     (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5417 		      0, NULL, FALSE,
5418 		      get_elf_backend_data (dynobj)->collect, &bh)))
5419 		return FALSE;
5420 	      h = (struct elf_link_hash_entry *) bh;
5421 	      h->non_elf = 0;
5422 	      h->def_regular = 1;
5423 	      h->type = STT_OBJECT;
5424 	      h->verinfo.vertree = NULL;
5425 
5426 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
5427 		return FALSE;
5428 
5429 	      /* Create a duplicate of the base version with the same
5430 		 aux block, but different flags.  */
5431 	      def.vd_flags = 0;
5432 	      def.vd_ndx = 2;
5433 	      def.vd_aux = sizeof (Elf_External_Verdef);
5434 	      if (verdefs)
5435 		def.vd_next = (sizeof (Elf_External_Verdef)
5436 			       + sizeof (Elf_External_Verdaux));
5437 	      else
5438 		def.vd_next = 0;
5439 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
5440 					(Elf_External_Verdef *) p);
5441 	      p += sizeof (Elf_External_Verdef);
5442 	    }
5443 	  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5444 				     (Elf_External_Verdaux *) p);
5445 	  p += sizeof (Elf_External_Verdaux);
5446 
5447 	  for (t = verdefs; t != NULL; t = t->next)
5448 	    {
5449 	      unsigned int cdeps;
5450 	      struct bfd_elf_version_deps *n;
5451 
5452 	      cdeps = 0;
5453 	      for (n = t->deps; n != NULL; n = n->next)
5454 		++cdeps;
5455 
5456 	      /* Add a symbol representing this version.  */
5457 	      bh = NULL;
5458 	      if (! (_bfd_generic_link_add_one_symbol
5459 		     (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5460 		      0, NULL, FALSE,
5461 		      get_elf_backend_data (dynobj)->collect, &bh)))
5462 		return FALSE;
5463 	      h = (struct elf_link_hash_entry *) bh;
5464 	      h->non_elf = 0;
5465 	      h->def_regular = 1;
5466 	      h->type = STT_OBJECT;
5467 	      h->verinfo.vertree = t;
5468 
5469 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
5470 		return FALSE;
5471 
5472 	      def.vd_version = VER_DEF_CURRENT;
5473 	      def.vd_flags = 0;
5474 	      if (t->globals.list == NULL
5475 		  && t->locals.list == NULL
5476 		  && ! t->used)
5477 		def.vd_flags |= VER_FLG_WEAK;
5478 	      def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5479 	      def.vd_cnt = cdeps + 1;
5480 	      def.vd_hash = bfd_elf_hash (t->name);
5481 	      def.vd_aux = sizeof (Elf_External_Verdef);
5482 	      def.vd_next = 0;
5483 	      if (t->next != NULL)
5484 		def.vd_next = (sizeof (Elf_External_Verdef)
5485 			       + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5486 
5487 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
5488 					(Elf_External_Verdef *) p);
5489 	      p += sizeof (Elf_External_Verdef);
5490 
5491 	      defaux.vda_name = h->dynstr_index;
5492 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5493 				      h->dynstr_index);
5494 	      defaux.vda_next = 0;
5495 	      if (t->deps != NULL)
5496 		defaux.vda_next = sizeof (Elf_External_Verdaux);
5497 	      t->name_indx = defaux.vda_name;
5498 
5499 	      _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5500 					 (Elf_External_Verdaux *) p);
5501 	      p += sizeof (Elf_External_Verdaux);
5502 
5503 	      for (n = t->deps; n != NULL; n = n->next)
5504 		{
5505 		  if (n->version_needed == NULL)
5506 		    {
5507 		      /* This can happen if there was an error in the
5508 			 version script.  */
5509 		      defaux.vda_name = 0;
5510 		    }
5511 		  else
5512 		    {
5513 		      defaux.vda_name = n->version_needed->name_indx;
5514 		      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5515 					      defaux.vda_name);
5516 		    }
5517 		  if (n->next == NULL)
5518 		    defaux.vda_next = 0;
5519 		  else
5520 		    defaux.vda_next = sizeof (Elf_External_Verdaux);
5521 
5522 		  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5523 					     (Elf_External_Verdaux *) p);
5524 		  p += sizeof (Elf_External_Verdaux);
5525 		}
5526 	    }
5527 
5528 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5529 	      || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5530 	    return FALSE;
5531 
5532 	  elf_tdata (output_bfd)->cverdefs = cdefs;
5533 	}
5534 
5535       if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5536 	{
5537 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5538 	    return FALSE;
5539 	}
5540       else if (info->flags & DF_BIND_NOW)
5541 	{
5542 	  if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5543 	    return FALSE;
5544 	}
5545 
5546       if (info->flags_1)
5547 	{
5548 	  if (info->executable)
5549 	    info->flags_1 &= ~ (DF_1_INITFIRST
5550 				| DF_1_NODELETE
5551 				| DF_1_NOOPEN);
5552 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5553 	    return FALSE;
5554 	}
5555 
5556       /* Work out the size of the version reference section.  */
5557 
5558       s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5559       BFD_ASSERT (s != NULL);
5560       {
5561 	struct elf_find_verdep_info sinfo;
5562 
5563 	sinfo.output_bfd = output_bfd;
5564 	sinfo.info = info;
5565 	sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5566 	if (sinfo.vers == 0)
5567 	  sinfo.vers = 1;
5568 	sinfo.failed = FALSE;
5569 
5570 	elf_link_hash_traverse (elf_hash_table (info),
5571 				_bfd_elf_link_find_version_dependencies,
5572 				&sinfo);
5573 
5574 	if (elf_tdata (output_bfd)->verref == NULL)
5575 	  s->flags |= SEC_EXCLUDE;
5576 	else
5577 	  {
5578 	    Elf_Internal_Verneed *t;
5579 	    unsigned int size;
5580 	    unsigned int crefs;
5581 	    bfd_byte *p;
5582 
5583 	    /* Build the version definition section.  */
5584 	    size = 0;
5585 	    crefs = 0;
5586 	    for (t = elf_tdata (output_bfd)->verref;
5587 		 t != NULL;
5588 		 t = t->vn_nextref)
5589 	      {
5590 		Elf_Internal_Vernaux *a;
5591 
5592 		size += sizeof (Elf_External_Verneed);
5593 		++crefs;
5594 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5595 		  size += sizeof (Elf_External_Vernaux);
5596 	      }
5597 
5598 	    s->size = size;
5599 	    s->contents = bfd_alloc (output_bfd, s->size);
5600 	    if (s->contents == NULL)
5601 	      return FALSE;
5602 
5603 	    p = s->contents;
5604 	    for (t = elf_tdata (output_bfd)->verref;
5605 		 t != NULL;
5606 		 t = t->vn_nextref)
5607 	      {
5608 		unsigned int caux;
5609 		Elf_Internal_Vernaux *a;
5610 		bfd_size_type indx;
5611 
5612 		caux = 0;
5613 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5614 		  ++caux;
5615 
5616 		t->vn_version = VER_NEED_CURRENT;
5617 		t->vn_cnt = caux;
5618 		indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5619 					    elf_dt_name (t->vn_bfd) != NULL
5620 					    ? elf_dt_name (t->vn_bfd)
5621 					    : lbasename (t->vn_bfd->filename),
5622 					    FALSE);
5623 		if (indx == (bfd_size_type) -1)
5624 		  return FALSE;
5625 		t->vn_file = indx;
5626 		t->vn_aux = sizeof (Elf_External_Verneed);
5627 		if (t->vn_nextref == NULL)
5628 		  t->vn_next = 0;
5629 		else
5630 		  t->vn_next = (sizeof (Elf_External_Verneed)
5631 				+ caux * sizeof (Elf_External_Vernaux));
5632 
5633 		_bfd_elf_swap_verneed_out (output_bfd, t,
5634 					   (Elf_External_Verneed *) p);
5635 		p += sizeof (Elf_External_Verneed);
5636 
5637 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5638 		  {
5639 		    a->vna_hash = bfd_elf_hash (a->vna_nodename);
5640 		    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5641 						a->vna_nodename, FALSE);
5642 		    if (indx == (bfd_size_type) -1)
5643 		      return FALSE;
5644 		    a->vna_name = indx;
5645 		    if (a->vna_nextptr == NULL)
5646 		      a->vna_next = 0;
5647 		    else
5648 		      a->vna_next = sizeof (Elf_External_Vernaux);
5649 
5650 		    _bfd_elf_swap_vernaux_out (output_bfd, a,
5651 					       (Elf_External_Vernaux *) p);
5652 		    p += sizeof (Elf_External_Vernaux);
5653 		  }
5654 	      }
5655 
5656 	    if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5657 		|| !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5658 	      return FALSE;
5659 
5660 	    elf_tdata (output_bfd)->cverrefs = crefs;
5661 	  }
5662       }
5663 
5664       if ((elf_tdata (output_bfd)->cverrefs == 0
5665 	   && elf_tdata (output_bfd)->cverdefs == 0)
5666 	  || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5667 					     &section_sym_count) == 0)
5668 	{
5669 	  s = bfd_get_section_by_name (dynobj, ".gnu.version");
5670 	  s->flags |= SEC_EXCLUDE;
5671 	}
5672     }
5673   return TRUE;
5674 }
5675 
5676 bfd_boolean
bfd_elf_size_dynsym_hash_dynstr(bfd * output_bfd,struct bfd_link_info * info)5677 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5678 {
5679   if (!is_elf_hash_table (info->hash))
5680     return TRUE;
5681 
5682   if (elf_hash_table (info)->dynamic_sections_created)
5683     {
5684       bfd *dynobj;
5685       const struct elf_backend_data *bed;
5686       asection *s;
5687       bfd_size_type dynsymcount;
5688       unsigned long section_sym_count;
5689       size_t bucketcount = 0;
5690       size_t hash_entry_size;
5691       unsigned int dtagcount;
5692 
5693       dynobj = elf_hash_table (info)->dynobj;
5694 
5695       /* Assign dynsym indicies.  In a shared library we generate a
5696 	 section symbol for each output section, which come first.
5697 	 Next come all of the back-end allocated local dynamic syms,
5698 	 followed by the rest of the global symbols.  */
5699 
5700       dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5701 						    &section_sym_count);
5702 
5703       /* Work out the size of the symbol version section.  */
5704       s = bfd_get_section_by_name (dynobj, ".gnu.version");
5705       BFD_ASSERT (s != NULL);
5706       if (dynsymcount != 0
5707 	  && (s->flags & SEC_EXCLUDE) == 0)
5708 	{
5709 	  s->size = dynsymcount * sizeof (Elf_External_Versym);
5710 	  s->contents = bfd_zalloc (output_bfd, s->size);
5711 	  if (s->contents == NULL)
5712 	    return FALSE;
5713 
5714 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5715 	    return FALSE;
5716 	}
5717 
5718       /* Set the size of the .dynsym and .hash sections.  We counted
5719 	 the number of dynamic symbols in elf_link_add_object_symbols.
5720 	 We will build the contents of .dynsym and .hash when we build
5721 	 the final symbol table, because until then we do not know the
5722 	 correct value to give the symbols.  We built the .dynstr
5723 	 section as we went along in elf_link_add_object_symbols.  */
5724       s = bfd_get_section_by_name (dynobj, ".dynsym");
5725       BFD_ASSERT (s != NULL);
5726       bed = get_elf_backend_data (output_bfd);
5727       s->size = dynsymcount * bed->s->sizeof_sym;
5728 
5729       if (dynsymcount != 0)
5730 	{
5731 	  s->contents = bfd_alloc (output_bfd, s->size);
5732 	  if (s->contents == NULL)
5733 	    return FALSE;
5734 
5735 	  /* The first entry in .dynsym is a dummy symbol.
5736 	     Clear all the section syms, in case we don't output them all.  */
5737 	  ++section_sym_count;
5738 	  memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5739 	}
5740 
5741       /* Compute the size of the hashing table.  As a side effect this
5742 	 computes the hash values for all the names we export.  */
5743       bucketcount = compute_bucket_count (info);
5744 
5745       s = bfd_get_section_by_name (dynobj, ".hash");
5746       BFD_ASSERT (s != NULL);
5747       hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5748       s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5749       s->contents = bfd_zalloc (output_bfd, s->size);
5750       if (s->contents == NULL)
5751 	return FALSE;
5752 
5753       bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5754       bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5755 	       s->contents + hash_entry_size);
5756 
5757       elf_hash_table (info)->bucketcount = bucketcount;
5758 
5759       s = bfd_get_section_by_name (dynobj, ".dynstr");
5760       BFD_ASSERT (s != NULL);
5761 
5762       elf_finalize_dynstr (output_bfd, info);
5763 
5764       s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5765 
5766       for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5767 	if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5768 	  return FALSE;
5769     }
5770 
5771   return TRUE;
5772 }
5773 
5774 /* Final phase of ELF linker.  */
5775 
5776 /* A structure we use to avoid passing large numbers of arguments.  */
5777 
5778 struct elf_final_link_info
5779 {
5780   /* General link information.  */
5781   struct bfd_link_info *info;
5782   /* Output BFD.  */
5783   bfd *output_bfd;
5784   /* Symbol string table.  */
5785   struct bfd_strtab_hash *symstrtab;
5786   /* .dynsym section.  */
5787   asection *dynsym_sec;
5788   /* .hash section.  */
5789   asection *hash_sec;
5790   /* symbol version section (.gnu.version).  */
5791   asection *symver_sec;
5792   /* Buffer large enough to hold contents of any section.  */
5793   bfd_byte *contents;
5794   /* Buffer large enough to hold external relocs of any section.  */
5795   void *external_relocs;
5796   /* Buffer large enough to hold internal relocs of any section.  */
5797   Elf_Internal_Rela *internal_relocs;
5798   /* Buffer large enough to hold external local symbols of any input
5799      BFD.  */
5800   bfd_byte *external_syms;
5801   /* And a buffer for symbol section indices.  */
5802   Elf_External_Sym_Shndx *locsym_shndx;
5803   /* Buffer large enough to hold internal local symbols of any input
5804      BFD.  */
5805   Elf_Internal_Sym *internal_syms;
5806   /* Array large enough to hold a symbol index for each local symbol
5807      of any input BFD.  */
5808   long *indices;
5809   /* Array large enough to hold a section pointer for each local
5810      symbol of any input BFD.  */
5811   asection **sections;
5812   /* Buffer to hold swapped out symbols.  */
5813   bfd_byte *symbuf;
5814   /* And one for symbol section indices.  */
5815   Elf_External_Sym_Shndx *symshndxbuf;
5816   /* Number of swapped out symbols in buffer.  */
5817   size_t symbuf_count;
5818   /* Number of symbols which fit in symbuf.  */
5819   size_t symbuf_size;
5820   /* And same for symshndxbuf.  */
5821   size_t shndxbuf_size;
5822 };
5823 
5824 /* This struct is used to pass information to elf_link_output_extsym.  */
5825 
5826 struct elf_outext_info
5827 {
5828   bfd_boolean failed;
5829   bfd_boolean localsyms;
5830   struct elf_final_link_info *finfo;
5831 };
5832 
5833 /* When performing a relocatable link, the input relocations are
5834    preserved.  But, if they reference global symbols, the indices
5835    referenced must be updated.  Update all the relocations in
5836    REL_HDR (there are COUNT of them), using the data in REL_HASH.  */
5837 
5838 static void
elf_link_adjust_relocs(bfd * abfd,Elf_Internal_Shdr * rel_hdr,unsigned int count,struct elf_link_hash_entry ** rel_hash)5839 elf_link_adjust_relocs (bfd *abfd,
5840 			Elf_Internal_Shdr *rel_hdr,
5841 			unsigned int count,
5842 			struct elf_link_hash_entry **rel_hash)
5843 {
5844   unsigned int i;
5845   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5846   bfd_byte *erela;
5847   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5848   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5849   bfd_vma r_type_mask;
5850   int r_sym_shift;
5851 
5852   if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5853     {
5854       swap_in = bed->s->swap_reloc_in;
5855       swap_out = bed->s->swap_reloc_out;
5856     }
5857   else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5858     {
5859       swap_in = bed->s->swap_reloca_in;
5860       swap_out = bed->s->swap_reloca_out;
5861     }
5862   else
5863     abort ();
5864 
5865   if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5866     abort ();
5867 
5868   if (bed->s->arch_size == 32)
5869     {
5870       r_type_mask = 0xff;
5871       r_sym_shift = 8;
5872     }
5873   else
5874     {
5875       r_type_mask = 0xffffffff;
5876       r_sym_shift = 32;
5877     }
5878 
5879   erela = rel_hdr->contents;
5880   for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5881     {
5882       Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5883       unsigned int j;
5884 
5885       if (*rel_hash == NULL)
5886 	continue;
5887 
5888       BFD_ASSERT ((*rel_hash)->indx >= 0);
5889 
5890       (*swap_in) (abfd, erela, irela);
5891       for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5892 	irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5893 			   | (irela[j].r_info & r_type_mask));
5894       (*swap_out) (abfd, irela, erela);
5895     }
5896 }
5897 
5898 struct elf_link_sort_rela
5899 {
5900   union {
5901     bfd_vma offset;
5902     bfd_vma sym_mask;
5903   } u;
5904   enum elf_reloc_type_class type;
5905   /* We use this as an array of size int_rels_per_ext_rel.  */
5906   Elf_Internal_Rela rela[1];
5907 };
5908 
5909 static int
elf_link_sort_cmp1(const void * A,const void * B)5910 elf_link_sort_cmp1 (const void *A, const void *B)
5911 {
5912   const struct elf_link_sort_rela *a = A;
5913   const struct elf_link_sort_rela *b = B;
5914   int relativea, relativeb;
5915 
5916   relativea = a->type == reloc_class_relative;
5917   relativeb = b->type == reloc_class_relative;
5918 
5919   if (relativea < relativeb)
5920     return 1;
5921   if (relativea > relativeb)
5922     return -1;
5923   if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5924     return -1;
5925   if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5926     return 1;
5927   if (a->rela->r_offset < b->rela->r_offset)
5928     return -1;
5929   if (a->rela->r_offset > b->rela->r_offset)
5930     return 1;
5931   return 0;
5932 }
5933 
5934 static int
elf_link_sort_cmp2(const void * A,const void * B)5935 elf_link_sort_cmp2 (const void *A, const void *B)
5936 {
5937   const struct elf_link_sort_rela *a = A;
5938   const struct elf_link_sort_rela *b = B;
5939   int copya, copyb;
5940 
5941   if (a->u.offset < b->u.offset)
5942     return -1;
5943   if (a->u.offset > b->u.offset)
5944     return 1;
5945   copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5946   copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5947   if (copya < copyb)
5948     return -1;
5949   if (copya > copyb)
5950     return 1;
5951   if (a->rela->r_offset < b->rela->r_offset)
5952     return -1;
5953   if (a->rela->r_offset > b->rela->r_offset)
5954     return 1;
5955   return 0;
5956 }
5957 
5958 static size_t
elf_link_sort_relocs(bfd * abfd,struct bfd_link_info * info,asection ** psec)5959 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5960 {
5961   asection *reldyn;
5962   bfd_size_type count, size;
5963   size_t i, ret, sort_elt, ext_size;
5964   bfd_byte *sort, *s_non_relative, *p;
5965   struct elf_link_sort_rela *sq;
5966   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5967   int i2e = bed->s->int_rels_per_ext_rel;
5968   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5969   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5970   struct bfd_link_order *lo;
5971   bfd_vma r_sym_mask;
5972 
5973   reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5974   if (reldyn == NULL || reldyn->size == 0)
5975     {
5976       reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5977       if (reldyn == NULL || reldyn->size == 0)
5978 	return 0;
5979       ext_size = bed->s->sizeof_rel;
5980       swap_in = bed->s->swap_reloc_in;
5981       swap_out = bed->s->swap_reloc_out;
5982     }
5983   else
5984     {
5985       ext_size = bed->s->sizeof_rela;
5986       swap_in = bed->s->swap_reloca_in;
5987       swap_out = bed->s->swap_reloca_out;
5988     }
5989   count = reldyn->size / ext_size;
5990 
5991   size = 0;
5992   for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
5993     if (lo->type == bfd_indirect_link_order)
5994       {
5995 	asection *o = lo->u.indirect.section;
5996 	size += o->size;
5997       }
5998 
5999   if (size != reldyn->size)
6000     return 0;
6001 
6002   sort_elt = (sizeof (struct elf_link_sort_rela)
6003 	      + (i2e - 1) * sizeof (Elf_Internal_Rela));
6004   sort = bfd_zmalloc (sort_elt * count);
6005   if (sort == NULL)
6006     {
6007       (*info->callbacks->warning)
6008 	(info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
6009       return 0;
6010     }
6011 
6012   if (bed->s->arch_size == 32)
6013     r_sym_mask = ~(bfd_vma) 0xff;
6014   else
6015     r_sym_mask = ~(bfd_vma) 0xffffffff;
6016 
6017   for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6018     if (lo->type == bfd_indirect_link_order)
6019       {
6020 	bfd_byte *erel, *erelend;
6021 	asection *o = lo->u.indirect.section;
6022 
6023 	if (o->contents == NULL && o->size != 0)
6024 	  {
6025 	    /* This is a reloc section that is being handled as a normal
6026 	       section.  See bfd_section_from_shdr.  We can't combine
6027 	       relocs in this case.  */
6028 	    free (sort);
6029 	    return 0;
6030 	  }
6031 	erel = o->contents;
6032 	erelend = o->contents + o->size;
6033 	p = sort + o->output_offset / ext_size * sort_elt;
6034 	while (erel < erelend)
6035 	  {
6036 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6037 	    (*swap_in) (abfd, erel, s->rela);
6038 	    s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
6039 	    s->u.sym_mask = r_sym_mask;
6040 	    p += sort_elt;
6041 	    erel += ext_size;
6042 	  }
6043       }
6044 
6045   qsort (sort, count, sort_elt, elf_link_sort_cmp1);
6046 
6047   for (i = 0, p = sort; i < count; i++, p += sort_elt)
6048     {
6049       struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6050       if (s->type != reloc_class_relative)
6051 	break;
6052     }
6053   ret = i;
6054   s_non_relative = p;
6055 
6056   sq = (struct elf_link_sort_rela *) s_non_relative;
6057   for (; i < count; i++, p += sort_elt)
6058     {
6059       struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
6060       if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
6061 	sq = sp;
6062       sp->u.offset = sq->rela->r_offset;
6063     }
6064 
6065   qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
6066 
6067   for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6068     if (lo->type == bfd_indirect_link_order)
6069       {
6070 	bfd_byte *erel, *erelend;
6071 	asection *o = lo->u.indirect.section;
6072 
6073 	erel = o->contents;
6074 	erelend = o->contents + o->size;
6075 	p = sort + o->output_offset / ext_size * sort_elt;
6076 	while (erel < erelend)
6077 	  {
6078 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6079 	    (*swap_out) (abfd, s->rela, erel);
6080 	    p += sort_elt;
6081 	    erel += ext_size;
6082 	  }
6083       }
6084 
6085   free (sort);
6086   *psec = reldyn;
6087   return ret;
6088 }
6089 
6090 /* Flush the output symbols to the file.  */
6091 
6092 static bfd_boolean
elf_link_flush_output_syms(struct elf_final_link_info * finfo,const struct elf_backend_data * bed)6093 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
6094 			    const struct elf_backend_data *bed)
6095 {
6096   if (finfo->symbuf_count > 0)
6097     {
6098       Elf_Internal_Shdr *hdr;
6099       file_ptr pos;
6100       bfd_size_type amt;
6101 
6102       hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6103       pos = hdr->sh_offset + hdr->sh_size;
6104       amt = finfo->symbuf_count * bed->s->sizeof_sym;
6105       if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6106 	  || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
6107 	return FALSE;
6108 
6109       hdr->sh_size += amt;
6110       finfo->symbuf_count = 0;
6111     }
6112 
6113   return TRUE;
6114 }
6115 
6116 /* Add a symbol to the output symbol table.  */
6117 
6118 static bfd_boolean
elf_link_output_sym(struct elf_final_link_info * finfo,const char * name,Elf_Internal_Sym * elfsym,asection * input_sec,struct elf_link_hash_entry * h)6119 elf_link_output_sym (struct elf_final_link_info *finfo,
6120 		     const char *name,
6121 		     Elf_Internal_Sym *elfsym,
6122 		     asection *input_sec,
6123 		     struct elf_link_hash_entry *h)
6124 {
6125   bfd_byte *dest;
6126   Elf_External_Sym_Shndx *destshndx;
6127   bfd_boolean (*output_symbol_hook)
6128     (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
6129      struct elf_link_hash_entry *);
6130   const struct elf_backend_data *bed;
6131 
6132   bed = get_elf_backend_data (finfo->output_bfd);
6133   output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
6134   if (output_symbol_hook != NULL)
6135     {
6136       if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
6137 	return FALSE;
6138     }
6139 
6140   if (name == NULL || *name == '\0')
6141     elfsym->st_name = 0;
6142   else if (input_sec->flags & SEC_EXCLUDE)
6143     elfsym->st_name = 0;
6144   else
6145     {
6146       elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
6147 							    name, TRUE, FALSE);
6148       if (elfsym->st_name == (unsigned long) -1)
6149 	return FALSE;
6150     }
6151 
6152   if (finfo->symbuf_count >= finfo->symbuf_size)
6153     {
6154       if (! elf_link_flush_output_syms (finfo, bed))
6155 	return FALSE;
6156     }
6157 
6158   dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
6159   destshndx = finfo->symshndxbuf;
6160   if (destshndx != NULL)
6161     {
6162       if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
6163 	{
6164 	  bfd_size_type amt;
6165 
6166 	  amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
6167 	  finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
6168 	  if (destshndx == NULL)
6169 	    return FALSE;
6170 	  memset ((char *) destshndx + amt, 0, amt);
6171 	  finfo->shndxbuf_size *= 2;
6172 	}
6173       destshndx += bfd_get_symcount (finfo->output_bfd);
6174     }
6175 
6176   bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
6177   finfo->symbuf_count += 1;
6178   bfd_get_symcount (finfo->output_bfd) += 1;
6179 
6180   return TRUE;
6181 }
6182 
6183 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6184    allowing an unsatisfied unversioned symbol in the DSO to match a
6185    versioned symbol that would normally require an explicit version.
6186    We also handle the case that a DSO references a hidden symbol
6187    which may be satisfied by a versioned symbol in another DSO.  */
6188 
6189 static bfd_boolean
elf_link_check_versioned_symbol(struct bfd_link_info * info,const struct elf_backend_data * bed,struct elf_link_hash_entry * h)6190 elf_link_check_versioned_symbol (struct bfd_link_info *info,
6191 				 const struct elf_backend_data *bed,
6192 				 struct elf_link_hash_entry *h)
6193 {
6194   bfd *abfd;
6195   struct elf_link_loaded_list *loaded;
6196 
6197   if (!is_elf_hash_table (info->hash))
6198     return FALSE;
6199 
6200   switch (h->root.type)
6201     {
6202     default:
6203       abfd = NULL;
6204       break;
6205 
6206     case bfd_link_hash_undefined:
6207     case bfd_link_hash_undefweak:
6208       abfd = h->root.u.undef.abfd;
6209       if ((abfd->flags & DYNAMIC) == 0
6210 	  || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
6211 	return FALSE;
6212       break;
6213 
6214     case bfd_link_hash_defined:
6215     case bfd_link_hash_defweak:
6216       abfd = h->root.u.def.section->owner;
6217       break;
6218 
6219     case bfd_link_hash_common:
6220       abfd = h->root.u.c.p->section->owner;
6221       break;
6222     }
6223   BFD_ASSERT (abfd != NULL);
6224 
6225   for (loaded = elf_hash_table (info)->loaded;
6226        loaded != NULL;
6227        loaded = loaded->next)
6228     {
6229       bfd *input;
6230       Elf_Internal_Shdr *hdr;
6231       bfd_size_type symcount;
6232       bfd_size_type extsymcount;
6233       bfd_size_type extsymoff;
6234       Elf_Internal_Shdr *versymhdr;
6235       Elf_Internal_Sym *isym;
6236       Elf_Internal_Sym *isymend;
6237       Elf_Internal_Sym *isymbuf;
6238       Elf_External_Versym *ever;
6239       Elf_External_Versym *extversym;
6240 
6241       input = loaded->abfd;
6242 
6243       /* We check each DSO for a possible hidden versioned definition.  */
6244       if (input == abfd
6245 	  || (input->flags & DYNAMIC) == 0
6246 	  || elf_dynversym (input) == 0)
6247 	continue;
6248 
6249       hdr = &elf_tdata (input)->dynsymtab_hdr;
6250 
6251       symcount = hdr->sh_size / bed->s->sizeof_sym;
6252       if (elf_bad_symtab (input))
6253 	{
6254 	  extsymcount = symcount;
6255 	  extsymoff = 0;
6256 	}
6257       else
6258 	{
6259 	  extsymcount = symcount - hdr->sh_info;
6260 	  extsymoff = hdr->sh_info;
6261 	}
6262 
6263       if (extsymcount == 0)
6264 	continue;
6265 
6266       isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6267 				      NULL, NULL, NULL);
6268       if (isymbuf == NULL)
6269 	return FALSE;
6270 
6271       /* Read in any version definitions.  */
6272       versymhdr = &elf_tdata (input)->dynversym_hdr;
6273       extversym = bfd_malloc (versymhdr->sh_size);
6274       if (extversym == NULL)
6275 	goto error_ret;
6276 
6277       if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6278 	  || (bfd_bread (extversym, versymhdr->sh_size, input)
6279 	      != versymhdr->sh_size))
6280 	{
6281 	  free (extversym);
6282 	error_ret:
6283 	  free (isymbuf);
6284 	  return FALSE;
6285 	}
6286 
6287       ever = extversym + extsymoff;
6288       isymend = isymbuf + extsymcount;
6289       for (isym = isymbuf; isym < isymend; isym++, ever++)
6290 	{
6291 	  const char *name;
6292 	  Elf_Internal_Versym iver;
6293 	  unsigned short version_index;
6294 
6295 	  if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6296 	      || isym->st_shndx == SHN_UNDEF)
6297 	    continue;
6298 
6299 	  name = bfd_elf_string_from_elf_section (input,
6300 						  hdr->sh_link,
6301 						  isym->st_name);
6302 	  if (strcmp (name, h->root.root.string) != 0)
6303 	    continue;
6304 
6305 	  _bfd_elf_swap_versym_in (input, ever, &iver);
6306 
6307 	  if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6308 	    {
6309 	      /* If we have a non-hidden versioned sym, then it should
6310 		 have provided a definition for the undefined sym.  */
6311 	      abort ();
6312 	    }
6313 
6314 	  version_index = iver.vs_vers & VERSYM_VERSION;
6315 	  if (version_index == 1 || version_index == 2)
6316 	    {
6317 	      /* This is the base or first version.  We can use it.  */
6318 	      free (extversym);
6319 	      free (isymbuf);
6320 	      return TRUE;
6321 	    }
6322 	}
6323 
6324       free (extversym);
6325       free (isymbuf);
6326     }
6327 
6328   return FALSE;
6329 }
6330 
6331 /* Add an external symbol to the symbol table.  This is called from
6332    the hash table traversal routine.  When generating a shared object,
6333    we go through the symbol table twice.  The first time we output
6334    anything that might have been forced to local scope in a version
6335    script.  The second time we output the symbols that are still
6336    global symbols.  */
6337 
6338 static bfd_boolean
elf_link_output_extsym(struct elf_link_hash_entry * h,void * data)6339 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6340 {
6341   struct elf_outext_info *eoinfo = data;
6342   struct elf_final_link_info *finfo = eoinfo->finfo;
6343   bfd_boolean strip;
6344   Elf_Internal_Sym sym;
6345   asection *input_sec;
6346   const struct elf_backend_data *bed;
6347 
6348   if (h->root.type == bfd_link_hash_warning)
6349     {
6350       h = (struct elf_link_hash_entry *) h->root.u.i.link;
6351       if (h->root.type == bfd_link_hash_new)
6352 	return TRUE;
6353     }
6354 
6355   /* Decide whether to output this symbol in this pass.  */
6356   if (eoinfo->localsyms)
6357     {
6358       if (!h->forced_local)
6359 	return TRUE;
6360     }
6361   else
6362     {
6363       if (h->forced_local)
6364 	return TRUE;
6365     }
6366 
6367   bed = get_elf_backend_data (finfo->output_bfd);
6368 
6369   /* If we have an undefined symbol reference here then it must have
6370      come from a shared library that is being linked in.  (Undefined
6371      references in regular files have already been handled).  If we
6372      are reporting errors for this situation then do so now.  */
6373   if (h->root.type == bfd_link_hash_undefined
6374       && h->ref_dynamic
6375       && !h->ref_regular
6376       && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6377       && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6378     {
6379       if (! ((*finfo->info->callbacks->undefined_symbol)
6380 	     (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6381 	      NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6382 	{
6383 	  eoinfo->failed = TRUE;
6384 	  return FALSE;
6385 	}
6386     }
6387 
6388   /* We should also warn if a forced local symbol is referenced from
6389      shared libraries.  */
6390   if (! finfo->info->relocatable
6391       && (! finfo->info->shared)
6392       && h->forced_local
6393       && h->ref_dynamic
6394       && !h->dynamic_def
6395       && !h->dynamic_weak
6396       && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6397     {
6398       (*_bfd_error_handler)
6399 	(_("%B: %s symbol `%s' in %B is referenced by DSO"),
6400 	 finfo->output_bfd,
6401 	 h->root.u.def.section == bfd_abs_section_ptr
6402 	 ? finfo->output_bfd : h->root.u.def.section->owner,
6403 	 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6404 	 ? "internal"
6405 	 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6406 	 ? "hidden" : "local",
6407 	 h->root.root.string);
6408       eoinfo->failed = TRUE;
6409       return FALSE;
6410     }
6411 
6412   /* We don't want to output symbols that have never been mentioned by
6413      a regular file, or that we have been told to strip.  However, if
6414      h->indx is set to -2, the symbol is used by a reloc and we must
6415      output it.  */
6416   if (h->indx == -2)
6417     strip = FALSE;
6418   else if ((h->def_dynamic
6419 	    || h->ref_dynamic
6420 	    || h->root.type == bfd_link_hash_new)
6421 	   && !h->def_regular
6422 	   && !h->ref_regular)
6423     strip = TRUE;
6424   else if (finfo->info->strip == strip_all)
6425     strip = TRUE;
6426   else if (finfo->info->strip == strip_some
6427 	   && bfd_hash_lookup (finfo->info->keep_hash,
6428 			       h->root.root.string, FALSE, FALSE) == NULL)
6429     strip = TRUE;
6430   else if (finfo->info->strip_discarded
6431 	   && (h->root.type == bfd_link_hash_defined
6432 	       || h->root.type == bfd_link_hash_defweak)
6433 	   && elf_discarded_section (h->root.u.def.section))
6434     strip = TRUE;
6435   else
6436     strip = FALSE;
6437 
6438   /* If we're stripping it, and it's not a dynamic symbol, there's
6439      nothing else to do unless it is a forced local symbol.  */
6440   if (strip
6441       && h->dynindx == -1
6442       && !h->forced_local)
6443     return TRUE;
6444 
6445   sym.st_value = 0;
6446   sym.st_size = h->size;
6447   sym.st_other = h->other;
6448   if (h->forced_local)
6449     sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6450   else if (h->root.type == bfd_link_hash_undefweak
6451 	   || h->root.type == bfd_link_hash_defweak)
6452     sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6453   else
6454     sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6455 
6456   switch (h->root.type)
6457     {
6458     default:
6459     case bfd_link_hash_new:
6460     case bfd_link_hash_warning:
6461       abort ();
6462       return FALSE;
6463 
6464     case bfd_link_hash_undefined:
6465     case bfd_link_hash_undefweak:
6466       input_sec = bfd_und_section_ptr;
6467       sym.st_shndx = SHN_UNDEF;
6468       break;
6469 
6470     case bfd_link_hash_defined:
6471     case bfd_link_hash_defweak:
6472       {
6473 	input_sec = h->root.u.def.section;
6474 	if (input_sec->output_section != NULL)
6475 	  {
6476 	    sym.st_shndx =
6477 	      _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6478 						 input_sec->output_section);
6479 	    if (sym.st_shndx == SHN_BAD)
6480 	      {
6481 		(*_bfd_error_handler)
6482 		  (_("%B: could not find output section %A for input section %A"),
6483 		   finfo->output_bfd, input_sec->output_section, input_sec);
6484 		eoinfo->failed = TRUE;
6485 		return FALSE;
6486 	      }
6487 
6488 	    /* ELF symbols in relocatable files are section relative,
6489 	       but in nonrelocatable files they are virtual
6490 	       addresses.  */
6491 	    sym.st_value = h->root.u.def.value + input_sec->output_offset;
6492 	    if (! finfo->info->relocatable)
6493 	      {
6494 		sym.st_value += input_sec->output_section->vma;
6495 		if (h->type == STT_TLS)
6496 		  {
6497 		    /* STT_TLS symbols are relative to PT_TLS segment
6498 		       base.  */
6499 		    BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6500 		    sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6501 		  }
6502 	      }
6503 	  }
6504 	else
6505 	  {
6506 	    BFD_ASSERT (input_sec->owner == NULL
6507 			|| (input_sec->owner->flags & DYNAMIC) != 0);
6508 	    sym.st_shndx = SHN_UNDEF;
6509 	    input_sec = bfd_und_section_ptr;
6510 	  }
6511       }
6512       break;
6513 
6514     case bfd_link_hash_common:
6515       input_sec = h->root.u.c.p->section;
6516       sym.st_shndx = SHN_COMMON;
6517       sym.st_value = 1 << h->root.u.c.p->alignment_power;
6518       break;
6519 
6520     case bfd_link_hash_indirect:
6521       /* These symbols are created by symbol versioning.  They point
6522 	 to the decorated version of the name.  For example, if the
6523 	 symbol foo@@GNU_1.2 is the default, which should be used when
6524 	 foo is used with no version, then we add an indirect symbol
6525 	 foo which points to foo@@GNU_1.2.  We ignore these symbols,
6526 	 since the indirected symbol is already in the hash table.  */
6527       return TRUE;
6528     }
6529 
6530   /* Give the processor backend a chance to tweak the symbol value,
6531      and also to finish up anything that needs to be done for this
6532      symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
6533      forced local syms when non-shared is due to a historical quirk.  */
6534   if ((h->dynindx != -1
6535        || h->forced_local)
6536       && ((finfo->info->shared
6537 	   && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6538 	       || h->root.type != bfd_link_hash_undefweak))
6539 	  || !h->forced_local)
6540       && elf_hash_table (finfo->info)->dynamic_sections_created)
6541     {
6542       if (! ((*bed->elf_backend_finish_dynamic_symbol)
6543 	     (finfo->output_bfd, finfo->info, h, &sym)))
6544 	{
6545 	  eoinfo->failed = TRUE;
6546 	  return FALSE;
6547 	}
6548     }
6549 
6550   /* If we are marking the symbol as undefined, and there are no
6551      non-weak references to this symbol from a regular object, then
6552      mark the symbol as weak undefined; if there are non-weak
6553      references, mark the symbol as strong.  We can't do this earlier,
6554      because it might not be marked as undefined until the
6555      finish_dynamic_symbol routine gets through with it.  */
6556   if (sym.st_shndx == SHN_UNDEF
6557       && h->ref_regular
6558       && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6559 	  || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6560     {
6561       int bindtype;
6562 
6563       if (h->ref_regular_nonweak)
6564 	bindtype = STB_GLOBAL;
6565       else
6566 	bindtype = STB_WEAK;
6567       sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6568     }
6569 
6570   /* If a non-weak symbol with non-default visibility is not defined
6571      locally, it is a fatal error.  */
6572   if (! finfo->info->relocatable
6573       && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6574       && ELF_ST_BIND (sym.st_info) != STB_WEAK
6575       && h->root.type == bfd_link_hash_undefined
6576       && !h->def_regular)
6577     {
6578       (*_bfd_error_handler)
6579 	(_("%B: %s symbol `%s' isn't defined"),
6580 	 finfo->output_bfd,
6581 	 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6582 	 ? "protected"
6583 	 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6584 	 ? "internal" : "hidden",
6585 	 h->root.root.string);
6586       eoinfo->failed = TRUE;
6587       return FALSE;
6588     }
6589 
6590   /* If this symbol should be put in the .dynsym section, then put it
6591      there now.  We already know the symbol index.  We also fill in
6592      the entry in the .hash section.  */
6593   if (h->dynindx != -1
6594       && elf_hash_table (finfo->info)->dynamic_sections_created)
6595     {
6596       size_t bucketcount;
6597       size_t bucket;
6598       size_t hash_entry_size;
6599       bfd_byte *bucketpos;
6600       bfd_vma chain;
6601       bfd_byte *esym;
6602 
6603       sym.st_name = h->dynstr_index;
6604       esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6605       bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6606 
6607       bucketcount = elf_hash_table (finfo->info)->bucketcount;
6608       bucket = h->u.elf_hash_value % bucketcount;
6609       hash_entry_size
6610 	= elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6611       bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6612 		   + (bucket + 2) * hash_entry_size);
6613       chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6614       bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6615       bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6616 	       ((bfd_byte *) finfo->hash_sec->contents
6617 		+ (bucketcount + 2 + h->dynindx) * hash_entry_size));
6618 
6619       if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6620 	{
6621 	  Elf_Internal_Versym iversym;
6622 	  Elf_External_Versym *eversym;
6623 
6624 	  if (!h->def_regular)
6625 	    {
6626 	      if (h->verinfo.verdef == NULL)
6627 		iversym.vs_vers = 0;
6628 	      else
6629 		iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6630 	    }
6631 	  else
6632 	    {
6633 	      if (h->verinfo.vertree == NULL)
6634 		iversym.vs_vers = 1;
6635 	      else
6636 		iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6637 	      if (finfo->info->create_default_symver)
6638 		iversym.vs_vers++;
6639 	    }
6640 
6641 	  if (h->hidden)
6642 	    iversym.vs_vers |= VERSYM_HIDDEN;
6643 
6644 	  eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6645 	  eversym += h->dynindx;
6646 	  _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6647 	}
6648     }
6649 
6650   /* If we're stripping it, then it was just a dynamic symbol, and
6651      there's nothing else to do.  */
6652   if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6653     return TRUE;
6654 
6655   h->indx = bfd_get_symcount (finfo->output_bfd);
6656 
6657   if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6658     {
6659       eoinfo->failed = TRUE;
6660       return FALSE;
6661     }
6662 
6663   return TRUE;
6664 }
6665 
6666 /* Return TRUE if special handling is done for relocs in SEC against
6667    symbols defined in discarded sections.  */
6668 
6669 static bfd_boolean
elf_section_ignore_discarded_relocs(asection * sec)6670 elf_section_ignore_discarded_relocs (asection *sec)
6671 {
6672   const struct elf_backend_data *bed;
6673 
6674   switch (sec->sec_info_type)
6675     {
6676     case ELF_INFO_TYPE_STABS:
6677     case ELF_INFO_TYPE_EH_FRAME:
6678       return TRUE;
6679     default:
6680       break;
6681     }
6682 
6683   bed = get_elf_backend_data (sec->owner);
6684   if (bed->elf_backend_ignore_discarded_relocs != NULL
6685       && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6686     return TRUE;
6687 
6688   return FALSE;
6689 }
6690 
6691 enum action_discarded
6692   {
6693     COMPLAIN = 1,
6694     PRETEND = 2
6695   };
6696 
6697 /* Return a mask saying how ld should treat relocations in SEC against
6698    symbols defined in discarded sections.  If this function returns
6699    COMPLAIN set, ld will issue a warning message.  If this function
6700    returns PRETEND set, and the discarded section was link-once and the
6701    same size as the kept link-once section, ld will pretend that the
6702    symbol was actually defined in the kept section.  Otherwise ld will
6703    zero the reloc (at least that is the intent, but some cooperation by
6704    the target dependent code is needed, particularly for REL targets).  */
6705 
6706 static unsigned int
elf_action_discarded(asection * sec)6707 elf_action_discarded (asection *sec)
6708 {
6709   if (sec->flags & SEC_DEBUGGING)
6710     return PRETEND;
6711 
6712   if (strcmp (".eh_frame", sec->name) == 0)
6713     return 0;
6714 
6715   if (strcmp (".gcc_except_table", sec->name) == 0)
6716     return 0;
6717 
6718   if (strcmp (".PARISC.unwind", sec->name) == 0)
6719     return 0;
6720 
6721   if (strcmp (".fixup", sec->name) == 0)
6722     return 0;
6723 
6724   return COMPLAIN | PRETEND;
6725 }
6726 
6727 /* Find a match between a section and a member of a section group.  */
6728 
6729 static asection *
match_group_member(asection * sec,asection * group)6730 match_group_member (asection *sec, asection *group)
6731 {
6732   asection *first = elf_next_in_group (group);
6733   asection *s = first;
6734 
6735   while (s != NULL)
6736     {
6737       if (bfd_elf_match_symbols_in_sections (s, sec))
6738 	return s;
6739 
6740       if (s == first)
6741 	break;
6742     }
6743 
6744   return NULL;
6745 }
6746 
6747 /* Check if the kept section of a discarded section SEC can be used
6748    to replace it. Return the replacement if it is OK. Otherwise return
6749    NULL. */
6750 
6751 asection *
_bfd_elf_check_kept_section(asection * sec)6752 _bfd_elf_check_kept_section (asection *sec)
6753 {
6754   asection *kept;
6755 
6756   kept = sec->kept_section;
6757   if (kept != NULL)
6758     {
6759       if (elf_sec_group (sec) != NULL)
6760 	kept = match_group_member (sec, kept);
6761       if (kept != NULL && sec->size != kept->size)
6762 	kept = NULL;
6763     }
6764   return kept;
6765 }
6766 
6767 /* Link an input file into the linker output file.  This function
6768    handles all the sections and relocations of the input file at once.
6769    This is so that we only have to read the local symbols once, and
6770    don't have to keep them in memory.  */
6771 
6772 static bfd_boolean
elf_link_input_bfd(struct elf_final_link_info * finfo,bfd * input_bfd)6773 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6774 {
6775   bfd_boolean (*relocate_section)
6776     (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6777      Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6778   bfd *output_bfd;
6779   Elf_Internal_Shdr *symtab_hdr;
6780   size_t locsymcount;
6781   size_t extsymoff;
6782   Elf_Internal_Sym *isymbuf;
6783   Elf_Internal_Sym *isym;
6784   Elf_Internal_Sym *isymend;
6785   long *pindex;
6786   asection **ppsection;
6787   asection *o;
6788   const struct elf_backend_data *bed;
6789   bfd_boolean emit_relocs;
6790   struct elf_link_hash_entry **sym_hashes;
6791 
6792   output_bfd = finfo->output_bfd;
6793   bed = get_elf_backend_data (output_bfd);
6794   relocate_section = bed->elf_backend_relocate_section;
6795 
6796   /* If this is a dynamic object, we don't want to do anything here:
6797      we don't want the local symbols, and we don't want the section
6798      contents.  */
6799   if ((input_bfd->flags & DYNAMIC) != 0)
6800     return TRUE;
6801 
6802   emit_relocs = (finfo->info->relocatable
6803 		 || finfo->info->emitrelocations);
6804 
6805   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6806   if (elf_bad_symtab (input_bfd))
6807     {
6808       locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6809       extsymoff = 0;
6810     }
6811   else
6812     {
6813       locsymcount = symtab_hdr->sh_info;
6814       extsymoff = symtab_hdr->sh_info;
6815     }
6816 
6817   /* Read the local symbols.  */
6818   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6819   if (isymbuf == NULL && locsymcount != 0)
6820     {
6821       isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6822 				      finfo->internal_syms,
6823 				      finfo->external_syms,
6824 				      finfo->locsym_shndx);
6825       if (isymbuf == NULL)
6826 	return FALSE;
6827     }
6828 
6829   /* Find local symbol sections and adjust values of symbols in
6830      SEC_MERGE sections.  Write out those local symbols we know are
6831      going into the output file.  */
6832   isymend = isymbuf + locsymcount;
6833   for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6834        isym < isymend;
6835        isym++, pindex++, ppsection++)
6836     {
6837       asection *isec;
6838       const char *name;
6839       Elf_Internal_Sym osym;
6840 
6841       *pindex = -1;
6842 
6843       if (elf_bad_symtab (input_bfd))
6844 	{
6845 	  if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6846 	    {
6847 	      *ppsection = NULL;
6848 	      continue;
6849 	    }
6850 	}
6851 
6852       if (isym->st_shndx == SHN_UNDEF)
6853 	isec = bfd_und_section_ptr;
6854       else if (isym->st_shndx < SHN_LORESERVE
6855 	       || isym->st_shndx > SHN_HIRESERVE)
6856 	{
6857 	  isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6858 	  if (isec
6859 	      && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6860 	      && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6861 	    isym->st_value =
6862 	      _bfd_merged_section_offset (output_bfd, &isec,
6863 					  elf_section_data (isec)->sec_info,
6864 					  isym->st_value);
6865 	}
6866       else if (isym->st_shndx == SHN_ABS)
6867 	isec = bfd_abs_section_ptr;
6868       else if (isym->st_shndx == SHN_COMMON)
6869 	isec = bfd_com_section_ptr;
6870       else
6871 	{
6872 	  /* Who knows?  */
6873 	  isec = NULL;
6874 	}
6875 
6876       *ppsection = isec;
6877 
6878       /* Don't output the first, undefined, symbol.  */
6879       if (ppsection == finfo->sections)
6880 	continue;
6881 
6882       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6883 	{
6884 	  /* We never output section symbols.  Instead, we use the
6885 	     section symbol of the corresponding section in the output
6886 	     file.  */
6887 	  continue;
6888 	}
6889 
6890       /* If we are stripping all symbols, we don't want to output this
6891 	 one.  */
6892       if (finfo->info->strip == strip_all)
6893 	continue;
6894 
6895       /* If we are discarding all local symbols, we don't want to
6896 	 output this one.  If we are generating a relocatable output
6897 	 file, then some of the local symbols may be required by
6898 	 relocs; we output them below as we discover that they are
6899 	 needed.  */
6900       if (finfo->info->discard == discard_all)
6901 	continue;
6902 
6903       /* If this symbol is defined in a section which we are
6904 	 discarding, we don't need to keep it, but note that
6905 	 linker_mark is only reliable for sections that have contents.
6906 	 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6907 	 as well as linker_mark.  */
6908       if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6909 	  && (isec == NULL
6910 	      || (! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6911 	      || (! finfo->info->relocatable
6912 		  && (isec->flags & SEC_EXCLUDE) != 0)))
6913 	continue;
6914 
6915       /* If the section is not in the output BFD's section list, it is not
6916 	 being output.  */
6917       if (bfd_section_removed_from_list (output_bfd, isec->output_section))
6918 	continue;
6919 
6920       /* Get the name of the symbol.  */
6921       name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6922 					      isym->st_name);
6923       if (name == NULL)
6924 	return FALSE;
6925 
6926       /* See if we are discarding symbols with this name.  */
6927       if ((finfo->info->strip == strip_some
6928 	   && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6929 	       == NULL))
6930 	  || (((finfo->info->discard == discard_sec_merge
6931 		&& (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6932 	       || finfo->info->discard == discard_l)
6933 	      && bfd_is_local_label_name (input_bfd, name)))
6934 	continue;
6935 
6936       /* If we get here, we are going to output this symbol.  */
6937 
6938       osym = *isym;
6939 
6940       /* Adjust the section index for the output file.  */
6941       osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6942 							 isec->output_section);
6943       if (osym.st_shndx == SHN_BAD)
6944 	return FALSE;
6945 
6946       *pindex = bfd_get_symcount (output_bfd);
6947 
6948       /* ELF symbols in relocatable files are section relative, but
6949 	 in executable files they are virtual addresses.  Note that
6950 	 this code assumes that all ELF sections have an associated
6951 	 BFD section with a reasonable value for output_offset; below
6952 	 we assume that they also have a reasonable value for
6953 	 output_section.  Any special sections must be set up to meet
6954 	 these requirements.  */
6955       osym.st_value += isec->output_offset;
6956       if (! finfo->info->relocatable)
6957 	{
6958 	  osym.st_value += isec->output_section->vma;
6959 	  if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6960 	    {
6961 	      /* STT_TLS symbols are relative to PT_TLS segment base.  */
6962 	      BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6963 	      osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6964 	    }
6965 	}
6966 
6967       if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6968 	return FALSE;
6969     }
6970 
6971   /* Relocate the contents of each section.  */
6972   sym_hashes = elf_sym_hashes (input_bfd);
6973   for (o = input_bfd->sections; o != NULL; o = o->next)
6974     {
6975       bfd_byte *contents;
6976 
6977       if (! o->linker_mark)
6978 	{
6979 	  /* This section was omitted from the link.  */
6980 	  continue;
6981 	}
6982 
6983       if ((o->flags & SEC_HAS_CONTENTS) == 0
6984 	  || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6985 	continue;
6986 
6987       if ((o->flags & SEC_LINKER_CREATED) != 0)
6988 	{
6989 	  /* Section was created by _bfd_elf_link_create_dynamic_sections
6990 	     or somesuch.  */
6991 	  continue;
6992 	}
6993 
6994       /* Get the contents of the section.  They have been cached by a
6995 	 relaxation routine.  Note that o is a section in an input
6996 	 file, so the contents field will not have been set by any of
6997 	 the routines which work on output files.  */
6998       if (elf_section_data (o)->this_hdr.contents != NULL)
6999 	contents = elf_section_data (o)->this_hdr.contents;
7000       else
7001 	{
7002 	  bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
7003 
7004 	  contents = finfo->contents;
7005 	  if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
7006 	    return FALSE;
7007 	}
7008 
7009       if ((o->flags & SEC_RELOC) != 0)
7010 	{
7011 	  Elf_Internal_Rela *internal_relocs;
7012 	  bfd_vma r_type_mask;
7013 	  int r_sym_shift;
7014 
7015 	  /* Get the swapped relocs.  */
7016 	  internal_relocs
7017 	    = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
7018 					 finfo->internal_relocs, FALSE);
7019 	  if (internal_relocs == NULL
7020 	      && o->reloc_count > 0)
7021 	    return FALSE;
7022 
7023 	  if (bed->s->arch_size == 32)
7024 	    {
7025 	      r_type_mask = 0xff;
7026 	      r_sym_shift = 8;
7027 	    }
7028 	  else
7029 	    {
7030 	      r_type_mask = 0xffffffff;
7031 	      r_sym_shift = 32;
7032 	    }
7033 
7034 	  /* Run through the relocs looking for any against symbols
7035 	     from discarded sections and section symbols from
7036 	     removed link-once sections.  Complain about relocs
7037 	     against discarded sections.  Zero relocs against removed
7038 	     link-once sections.  Preserve debug information as much
7039 	     as we can.  */
7040 	  if (!elf_section_ignore_discarded_relocs (o))
7041 	    {
7042 	      Elf_Internal_Rela *rel, *relend;
7043 	      unsigned int action = elf_action_discarded (o);
7044 
7045 	      rel = internal_relocs;
7046 	      relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
7047 	      for ( ; rel < relend; rel++)
7048 		{
7049 		  unsigned long r_symndx = rel->r_info >> r_sym_shift;
7050 		  asection **ps, *sec;
7051 		  struct elf_link_hash_entry *h = NULL;
7052 		  const char *sym_name;
7053 
7054 		  if (r_symndx == STN_UNDEF)
7055 		    continue;
7056 
7057 		  if (r_symndx >= locsymcount
7058 		      || (elf_bad_symtab (input_bfd)
7059 			  && finfo->sections[r_symndx] == NULL))
7060 		    {
7061 		      h = sym_hashes[r_symndx - extsymoff];
7062 
7063 		      /* Badly formatted input files can contain relocs that
7064 			 reference non-existant symbols.  Check here so that
7065 			 we do not seg fault.  */
7066 		      if (h == NULL)
7067 			{
7068 			  char buffer [32];
7069 
7070 			  sprintf_vma (buffer, rel->r_info);
7071 			  (*_bfd_error_handler)
7072 			    (_("error: %B contains a reloc (0x%s) for section %A "
7073 			       "that references a non-existent global symbol"),
7074 			     input_bfd, o, buffer);
7075 			  bfd_set_error (bfd_error_bad_value);
7076 			  return FALSE;
7077 			}
7078 
7079 		      while (h->root.type == bfd_link_hash_indirect
7080 			     || h->root.type == bfd_link_hash_warning)
7081 			h = (struct elf_link_hash_entry *) h->root.u.i.link;
7082 
7083 		      if (h->root.type != bfd_link_hash_defined
7084 			  && h->root.type != bfd_link_hash_defweak)
7085 			continue;
7086 
7087 		      ps = &h->root.u.def.section;
7088 		      sym_name = h->root.root.string;
7089 		    }
7090 		  else
7091 		    {
7092 		      Elf_Internal_Sym *sym = isymbuf + r_symndx;
7093 		      ps = &finfo->sections[r_symndx];
7094 		      sym_name = bfd_elf_sym_name (input_bfd,
7095 						   symtab_hdr,
7096 						   sym, *ps);
7097 		    }
7098 
7099 		  /* Complain if the definition comes from a
7100 		     discarded section.  */
7101 		  if ((sec = *ps) != NULL && elf_discarded_section (sec))
7102 		    {
7103 		      BFD_ASSERT (r_symndx != 0);
7104 		      if (action & COMPLAIN)
7105 			(*finfo->info->callbacks->einfo)
7106 			  (_("%X`%s' referenced in section `%A' of %B: "
7107 			     "defined in discarded section `%A' of %B"),
7108 			   sym_name, o, input_bfd, sec, sec->owner);
7109 
7110 		      /* Try to do the best we can to support buggy old
7111 			 versions of gcc.  If we've warned, or this is
7112 			 debugging info, pretend that the symbol is
7113 			 really defined in the kept linkonce section.
7114 			 FIXME: This is quite broken.  Modifying the
7115 			 symbol here means we will be changing all later
7116 			 uses of the symbol, not just in this section.
7117 			 The only thing that makes this half reasonable
7118 			 is that we warn in non-debug sections, and
7119 			 debug sections tend to come after other
7120 			 sections.  */
7121 		      if (action & PRETEND)
7122 			{
7123 			  asection *kept;
7124 
7125 			  kept = _bfd_elf_check_kept_section (sec);
7126 			  if (kept != NULL)
7127 			    {
7128 			      *ps = kept;
7129 			      continue;
7130 			    }
7131 			}
7132 
7133 		      /* Remove the symbol reference from the reloc, but
7134 			 don't kill the reloc completely.  This is so that
7135 			 a zero value will be written into the section,
7136 			 which may have non-zero contents put there by the
7137 			 assembler.  Zero in things like an eh_frame fde
7138 			 pc_begin allows stack unwinders to recognize the
7139 			 fde as bogus.  */
7140 		      rel->r_info &= r_type_mask;
7141 		      rel->r_addend = 0;
7142 		    }
7143 		}
7144 	    }
7145 
7146 	  /* Relocate the section by invoking a back end routine.
7147 
7148 	     The back end routine is responsible for adjusting the
7149 	     section contents as necessary, and (if using Rela relocs
7150 	     and generating a relocatable output file) adjusting the
7151 	     reloc addend as necessary.
7152 
7153 	     The back end routine does not have to worry about setting
7154 	     the reloc address or the reloc symbol index.
7155 
7156 	     The back end routine is given a pointer to the swapped in
7157 	     internal symbols, and can access the hash table entries
7158 	     for the external symbols via elf_sym_hashes (input_bfd).
7159 
7160 	     When generating relocatable output, the back end routine
7161 	     must handle STB_LOCAL/STT_SECTION symbols specially.  The
7162 	     output symbol is going to be a section symbol
7163 	     corresponding to the output section, which will require
7164 	     the addend to be adjusted.  */
7165 
7166 	  if (! (*relocate_section) (output_bfd, finfo->info,
7167 				     input_bfd, o, contents,
7168 				     internal_relocs,
7169 				     isymbuf,
7170 				     finfo->sections))
7171 	    return FALSE;
7172 
7173 	  if (emit_relocs)
7174 	    {
7175 	      Elf_Internal_Rela *irela;
7176 	      Elf_Internal_Rela *irelaend;
7177 	      bfd_vma last_offset;
7178 	      struct elf_link_hash_entry **rel_hash;
7179 	      struct elf_link_hash_entry **rel_hash_list;
7180 	      Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
7181 	      unsigned int next_erel;
7182 	      bfd_boolean rela_normal;
7183 
7184 	      input_rel_hdr = &elf_section_data (o)->rel_hdr;
7185 	      rela_normal = (bed->rela_normal
7186 			     && (input_rel_hdr->sh_entsize
7187 				 == bed->s->sizeof_rela));
7188 
7189 	      /* Adjust the reloc addresses and symbol indices.  */
7190 
7191 	      irela = internal_relocs;
7192 	      irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
7193 	      rel_hash = (elf_section_data (o->output_section)->rel_hashes
7194 			  + elf_section_data (o->output_section)->rel_count
7195 			  + elf_section_data (o->output_section)->rel_count2);
7196 	      rel_hash_list = rel_hash;
7197 	      last_offset = o->output_offset;
7198 	      if (!finfo->info->relocatable)
7199 		last_offset += o->output_section->vma;
7200 	      for (next_erel = 0; irela < irelaend; irela++, next_erel++)
7201 		{
7202 		  unsigned long r_symndx;
7203 		  asection *sec;
7204 		  Elf_Internal_Sym sym;
7205 
7206 		  if (next_erel == bed->s->int_rels_per_ext_rel)
7207 		    {
7208 		      rel_hash++;
7209 		      next_erel = 0;
7210 		    }
7211 
7212 		  irela->r_offset = _bfd_elf_section_offset (output_bfd,
7213 							     finfo->info, o,
7214 							     irela->r_offset);
7215 		  if (irela->r_offset >= (bfd_vma) -2)
7216 		    {
7217 		      /* This is a reloc for a deleted entry or somesuch.
7218 			 Turn it into an R_*_NONE reloc, at the same
7219 			 offset as the last reloc.  elf_eh_frame.c and
7220 			 elf_bfd_discard_info rely on reloc offsets
7221 			 being ordered.  */
7222 		      irela->r_offset = last_offset;
7223 		      irela->r_info = 0;
7224 		      irela->r_addend = 0;
7225 		      continue;
7226 		    }
7227 
7228 		  irela->r_offset += o->output_offset;
7229 
7230 		  /* Relocs in an executable have to be virtual addresses.  */
7231 		  if (!finfo->info->relocatable)
7232 		    irela->r_offset += o->output_section->vma;
7233 
7234 		  last_offset = irela->r_offset;
7235 
7236 		  r_symndx = irela->r_info >> r_sym_shift;
7237 		  if (r_symndx == STN_UNDEF)
7238 		    continue;
7239 
7240 		  if (r_symndx >= locsymcount
7241 		      || (elf_bad_symtab (input_bfd)
7242 			  && finfo->sections[r_symndx] == NULL))
7243 		    {
7244 		      struct elf_link_hash_entry *rh;
7245 		      unsigned long indx;
7246 
7247 		      /* This is a reloc against a global symbol.  We
7248 			 have not yet output all the local symbols, so
7249 			 we do not know the symbol index of any global
7250 			 symbol.  We set the rel_hash entry for this
7251 			 reloc to point to the global hash table entry
7252 			 for this symbol.  The symbol index is then
7253 			 set at the end of bfd_elf_final_link.  */
7254 		      indx = r_symndx - extsymoff;
7255 		      rh = elf_sym_hashes (input_bfd)[indx];
7256 		      while (rh->root.type == bfd_link_hash_indirect
7257 			     || rh->root.type == bfd_link_hash_warning)
7258 			rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7259 
7260 		      /* Setting the index to -2 tells
7261 			 elf_link_output_extsym that this symbol is
7262 			 used by a reloc.  */
7263 		      BFD_ASSERT (rh->indx < 0);
7264 		      rh->indx = -2;
7265 
7266 		      *rel_hash = rh;
7267 
7268 		      continue;
7269 		    }
7270 
7271 		  /* This is a reloc against a local symbol.  */
7272 
7273 		  *rel_hash = NULL;
7274 		  sym = isymbuf[r_symndx];
7275 		  sec = finfo->sections[r_symndx];
7276 		  if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7277 		    {
7278 		      /* I suppose the backend ought to fill in the
7279 			 section of any STT_SECTION symbol against a
7280 			 processor specific section.  */
7281 		      r_symndx = 0;
7282 		      if (bfd_is_abs_section (sec))
7283 			;
7284 		      else if (sec == NULL || sec->owner == NULL)
7285 			{
7286 			  bfd_set_error (bfd_error_bad_value);
7287 			  return FALSE;
7288 			}
7289 		      else
7290 			{
7291 			  asection *osec = sec->output_section;
7292 
7293 			  /* If we have discarded a section, the output
7294 			     section will be the absolute section.  In
7295 			     case of discarded link-once and discarded
7296 			     SEC_MERGE sections, use the kept section.  */
7297 			  if (bfd_is_abs_section (osec)
7298 			      && sec->kept_section != NULL
7299 			      && sec->kept_section->output_section != NULL)
7300 			    {
7301 			      osec = sec->kept_section->output_section;
7302 			      irela->r_addend -= osec->vma;
7303 			    }
7304 
7305 			  if (!bfd_is_abs_section (osec))
7306 			    {
7307 			      r_symndx = osec->target_index;
7308 			      BFD_ASSERT (r_symndx != 0);
7309 			    }
7310 			}
7311 
7312 		      /* Adjust the addend according to where the
7313 			 section winds up in the output section.  */
7314 		      if (rela_normal)
7315 			irela->r_addend += sec->output_offset;
7316 		    }
7317 		  else
7318 		    {
7319 		      if (finfo->indices[r_symndx] == -1)
7320 			{
7321 			  unsigned long shlink;
7322 			  const char *name;
7323 			  asection *osec;
7324 
7325 			  if (finfo->info->strip == strip_all)
7326 			    {
7327 			      /* You can't do ld -r -s.  */
7328 			      bfd_set_error (bfd_error_invalid_operation);
7329 			      return FALSE;
7330 			    }
7331 
7332 			  /* This symbol was skipped earlier, but
7333 			     since it is needed by a reloc, we
7334 			     must output it now.  */
7335 			  shlink = symtab_hdr->sh_link;
7336 			  name = (bfd_elf_string_from_elf_section
7337 				  (input_bfd, shlink, sym.st_name));
7338 			  if (name == NULL)
7339 			    return FALSE;
7340 
7341 			  osec = sec->output_section;
7342 			  sym.st_shndx =
7343 			    _bfd_elf_section_from_bfd_section (output_bfd,
7344 							       osec);
7345 			  if (sym.st_shndx == SHN_BAD)
7346 			    return FALSE;
7347 
7348 			  sym.st_value += sec->output_offset;
7349 			  if (! finfo->info->relocatable)
7350 			    {
7351 			      sym.st_value += osec->vma;
7352 			      if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7353 				{
7354 				  /* STT_TLS symbols are relative to PT_TLS
7355 				     segment base.  */
7356 				  BFD_ASSERT (elf_hash_table (finfo->info)
7357 					      ->tls_sec != NULL);
7358 				  sym.st_value -= (elf_hash_table (finfo->info)
7359 						   ->tls_sec->vma);
7360 				}
7361 			    }
7362 
7363 			  finfo->indices[r_symndx]
7364 			    = bfd_get_symcount (output_bfd);
7365 
7366 			  if (! elf_link_output_sym (finfo, name, &sym, sec,
7367 						     NULL))
7368 			    return FALSE;
7369 			}
7370 
7371 		      r_symndx = finfo->indices[r_symndx];
7372 		    }
7373 
7374 		  irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7375 				   | (irela->r_info & r_type_mask));
7376 		}
7377 
7378 	      /* Swap out the relocs.  */
7379 	      if (input_rel_hdr->sh_size != 0
7380 		  && !bed->elf_backend_emit_relocs (output_bfd, o,
7381 						    input_rel_hdr,
7382 						    internal_relocs,
7383 						    rel_hash_list))
7384 		return FALSE;
7385 
7386 	      input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7387 	      if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7388 		{
7389 		  internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7390 				      * bed->s->int_rels_per_ext_rel);
7391 		  rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
7392 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
7393 						     input_rel_hdr2,
7394 						     internal_relocs,
7395 						     rel_hash_list))
7396 		    return FALSE;
7397 		}
7398 	    }
7399 	}
7400 
7401       /* Write out the modified section contents.  */
7402       if (bed->elf_backend_write_section
7403 	  && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7404 	{
7405 	  /* Section written out.  */
7406 	}
7407       else switch (o->sec_info_type)
7408 	{
7409 	case ELF_INFO_TYPE_STABS:
7410 	  if (! (_bfd_write_section_stabs
7411 		 (output_bfd,
7412 		  &elf_hash_table (finfo->info)->stab_info,
7413 		  o, &elf_section_data (o)->sec_info, contents)))
7414 	    return FALSE;
7415 	  break;
7416 	case ELF_INFO_TYPE_MERGE:
7417 	  if (! _bfd_write_merged_section (output_bfd, o,
7418 					   elf_section_data (o)->sec_info))
7419 	    return FALSE;
7420 	  break;
7421 	case ELF_INFO_TYPE_EH_FRAME:
7422 	  {
7423 	    if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7424 						   o, contents))
7425 	      return FALSE;
7426 	  }
7427 	  break;
7428 	default:
7429 	  {
7430 	    if (! (o->flags & SEC_EXCLUDE)
7431 		&& ! bfd_set_section_contents (output_bfd, o->output_section,
7432 					       contents,
7433 					       (file_ptr) o->output_offset,
7434 					       o->size))
7435 	      return FALSE;
7436 	  }
7437 	  break;
7438 	}
7439     }
7440 
7441   return TRUE;
7442 }
7443 
7444 /* Generate a reloc when linking an ELF file.  This is a reloc
7445    requested by the linker, and does come from any input file.  This
7446    is used to build constructor and destructor tables when linking
7447    with -Ur.  */
7448 
7449 static bfd_boolean
elf_reloc_link_order(bfd * output_bfd,struct bfd_link_info * info,asection * output_section,struct bfd_link_order * link_order)7450 elf_reloc_link_order (bfd *output_bfd,
7451 		      struct bfd_link_info *info,
7452 		      asection *output_section,
7453 		      struct bfd_link_order *link_order)
7454 {
7455   reloc_howto_type *howto;
7456   long indx;
7457   bfd_vma offset;
7458   bfd_vma addend;
7459   struct elf_link_hash_entry **rel_hash_ptr;
7460   Elf_Internal_Shdr *rel_hdr;
7461   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7462   Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7463   bfd_byte *erel;
7464   unsigned int i;
7465 
7466   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7467   if (howto == NULL)
7468     {
7469       bfd_set_error (bfd_error_bad_value);
7470       return FALSE;
7471     }
7472 
7473   addend = link_order->u.reloc.p->addend;
7474 
7475   /* Figure out the symbol index.  */
7476   rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7477 		  + elf_section_data (output_section)->rel_count
7478 		  + elf_section_data (output_section)->rel_count2);
7479   if (link_order->type == bfd_section_reloc_link_order)
7480     {
7481       indx = link_order->u.reloc.p->u.section->target_index;
7482       BFD_ASSERT (indx != 0);
7483       *rel_hash_ptr = NULL;
7484     }
7485   else
7486     {
7487       struct elf_link_hash_entry *h;
7488 
7489       /* Treat a reloc against a defined symbol as though it were
7490 	 actually against the section.  */
7491       h = ((struct elf_link_hash_entry *)
7492 	   bfd_wrapped_link_hash_lookup (output_bfd, info,
7493 					 link_order->u.reloc.p->u.name,
7494 					 FALSE, FALSE, TRUE));
7495       if (h != NULL
7496 	  && (h->root.type == bfd_link_hash_defined
7497 	      || h->root.type == bfd_link_hash_defweak))
7498 	{
7499 	  asection *section;
7500 
7501 	  section = h->root.u.def.section;
7502 	  indx = section->output_section->target_index;
7503 	  *rel_hash_ptr = NULL;
7504 	  /* It seems that we ought to add the symbol value to the
7505 	     addend here, but in practice it has already been added
7506 	     because it was passed to constructor_callback.  */
7507 	  addend += section->output_section->vma + section->output_offset;
7508 	}
7509       else if (h != NULL)
7510 	{
7511 	  /* Setting the index to -2 tells elf_link_output_extsym that
7512 	     this symbol is used by a reloc.  */
7513 	  h->indx = -2;
7514 	  *rel_hash_ptr = h;
7515 	  indx = 0;
7516 	}
7517       else
7518 	{
7519 	  if (! ((*info->callbacks->unattached_reloc)
7520 		 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7521 	    return FALSE;
7522 	  indx = 0;
7523 	}
7524     }
7525 
7526   /* If this is an inplace reloc, we must write the addend into the
7527      object file.  */
7528   if (howto->partial_inplace && addend != 0)
7529     {
7530       bfd_size_type size;
7531       bfd_reloc_status_type rstat;
7532       bfd_byte *buf;
7533       bfd_boolean ok;
7534       const char *sym_name;
7535 
7536       size = bfd_get_reloc_size (howto);
7537       buf = bfd_zmalloc (size);
7538       if (buf == NULL)
7539 	return FALSE;
7540       rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7541       switch (rstat)
7542 	{
7543 	case bfd_reloc_ok:
7544 	  break;
7545 
7546 	default:
7547 	case bfd_reloc_outofrange:
7548 	  abort ();
7549 
7550 	case bfd_reloc_overflow:
7551 	  if (link_order->type == bfd_section_reloc_link_order)
7552 	    sym_name = bfd_section_name (output_bfd,
7553 					 link_order->u.reloc.p->u.section);
7554 	  else
7555 	    sym_name = link_order->u.reloc.p->u.name;
7556 	  if (! ((*info->callbacks->reloc_overflow)
7557 		 (info, NULL, sym_name, howto->name, addend, NULL,
7558 		  NULL, (bfd_vma) 0)))
7559 	    {
7560 	      free (buf);
7561 	      return FALSE;
7562 	    }
7563 	  break;
7564 	}
7565       ok = bfd_set_section_contents (output_bfd, output_section, buf,
7566 				     link_order->offset, size);
7567       free (buf);
7568       if (! ok)
7569 	return FALSE;
7570     }
7571 
7572   /* The address of a reloc is relative to the section in a
7573      relocatable file, and is a virtual address in an executable
7574      file.  */
7575   offset = link_order->offset;
7576   if (! info->relocatable)
7577     offset += output_section->vma;
7578 
7579   for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7580     {
7581       irel[i].r_offset = offset;
7582       irel[i].r_info = 0;
7583       irel[i].r_addend = 0;
7584     }
7585   if (bed->s->arch_size == 32)
7586     irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7587   else
7588     irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7589 
7590   rel_hdr = &elf_section_data (output_section)->rel_hdr;
7591   erel = rel_hdr->contents;
7592   if (rel_hdr->sh_type == SHT_REL)
7593     {
7594       erel += (elf_section_data (output_section)->rel_count
7595 	       * bed->s->sizeof_rel);
7596       (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7597     }
7598   else
7599     {
7600       irel[0].r_addend = addend;
7601       erel += (elf_section_data (output_section)->rel_count
7602 	       * bed->s->sizeof_rela);
7603       (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7604     }
7605 
7606   ++elf_section_data (output_section)->rel_count;
7607 
7608   return TRUE;
7609 }
7610 
7611 
7612 /* Get the output vma of the section pointed to by the sh_link field.  */
7613 
7614 static bfd_vma
elf_get_linked_section_vma(struct bfd_link_order * p)7615 elf_get_linked_section_vma (struct bfd_link_order *p)
7616 {
7617   Elf_Internal_Shdr **elf_shdrp;
7618   asection *s;
7619   int elfsec;
7620 
7621   s = p->u.indirect.section;
7622   elf_shdrp = elf_elfsections (s->owner);
7623   elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7624   elfsec = elf_shdrp[elfsec]->sh_link;
7625   /* PR 290:
7626      The Intel C compiler generates SHT_IA_64_UNWIND with
7627      SHF_LINK_ORDER.  But it doesn't set theh sh_link or
7628      sh_info fields.  Hence we could get the situation
7629      where elfsec is 0.  */
7630   if (elfsec == 0)
7631     {
7632       const struct elf_backend_data *bed
7633 	= get_elf_backend_data (s->owner);
7634       if (bed->link_order_error_handler)
7635 	bed->link_order_error_handler
7636 	  (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7637       return 0;
7638     }
7639   else
7640     {
7641       s = elf_shdrp[elfsec]->bfd_section;
7642       return s->output_section->vma + s->output_offset;
7643     }
7644 }
7645 
7646 
7647 /* Compare two sections based on the locations of the sections they are
7648    linked to.  Used by elf_fixup_link_order.  */
7649 
7650 static int
compare_link_order(const void * a,const void * b)7651 compare_link_order (const void * a, const void * b)
7652 {
7653   bfd_vma apos;
7654   bfd_vma bpos;
7655 
7656   apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7657   bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7658   if (apos < bpos)
7659     return -1;
7660   return apos > bpos;
7661 }
7662 
7663 
7664 /* Looks for sections with SHF_LINK_ORDER set.  Rearranges them into the same
7665    order as their linked sections.  Returns false if this could not be done
7666    because an output section includes both ordered and unordered
7667    sections.  Ideally we'd do this in the linker proper.  */
7668 
7669 static bfd_boolean
elf_fixup_link_order(bfd * abfd,asection * o)7670 elf_fixup_link_order (bfd *abfd, asection *o)
7671 {
7672   int seen_linkorder;
7673   int seen_other;
7674   int n;
7675   struct bfd_link_order *p;
7676   bfd *sub;
7677   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7678   int elfsec;
7679   struct bfd_link_order **sections;
7680   asection *s;
7681   bfd_vma offset;
7682 
7683   seen_other = 0;
7684   seen_linkorder = 0;
7685   for (p = o->map_head.link_order; p != NULL; p = p->next)
7686     {
7687       if (p->type == bfd_indirect_link_order
7688 	  && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7689 	      == bfd_target_elf_flavour)
7690 	  && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7691 	{
7692 	  s = p->u.indirect.section;
7693 	  elfsec = _bfd_elf_section_from_bfd_section (sub, s);
7694 	  if (elfsec != -1
7695 	      && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7696 	    seen_linkorder++;
7697 	  else
7698 	    seen_other++;
7699 	}
7700       else
7701 	seen_other++;
7702     }
7703 
7704   if (!seen_linkorder)
7705     return TRUE;
7706 
7707   if (seen_other && seen_linkorder)
7708     {
7709       (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7710 			     o);
7711       bfd_set_error (bfd_error_bad_value);
7712       return FALSE;
7713     }
7714 
7715   sections = (struct bfd_link_order **)
7716     xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7717   seen_linkorder = 0;
7718 
7719   for (p = o->map_head.link_order; p != NULL; p = p->next)
7720     {
7721       sections[seen_linkorder++] = p;
7722     }
7723   /* Sort the input sections in the order of their linked section.  */
7724   qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7725 	 compare_link_order);
7726 
7727   /* Change the offsets of the sections.  */
7728   offset = 0;
7729   for (n = 0; n < seen_linkorder; n++)
7730     {
7731       s = sections[n]->u.indirect.section;
7732       offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7733       s->output_offset = offset;
7734       sections[n]->offset = offset;
7735       offset += sections[n]->size;
7736     }
7737 
7738   return TRUE;
7739 }
7740 
7741 
7742 /* Do the final step of an ELF link.  */
7743 
7744 bfd_boolean
bfd_elf_final_link(bfd * abfd,struct bfd_link_info * info)7745 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7746 {
7747   bfd_boolean dynamic;
7748   bfd_boolean emit_relocs;
7749   bfd *dynobj;
7750   struct elf_final_link_info finfo;
7751   register asection *o;
7752   register struct bfd_link_order *p;
7753   register bfd *sub;
7754   bfd_size_type max_contents_size;
7755   bfd_size_type max_external_reloc_size;
7756   bfd_size_type max_internal_reloc_count;
7757   bfd_size_type max_sym_count;
7758   bfd_size_type max_sym_shndx_count;
7759   file_ptr off;
7760   Elf_Internal_Sym elfsym;
7761   unsigned int i;
7762   Elf_Internal_Shdr *symtab_hdr;
7763   Elf_Internal_Shdr *symtab_shndx_hdr;
7764   Elf_Internal_Shdr *symstrtab_hdr;
7765   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7766   struct elf_outext_info eoinfo;
7767   bfd_boolean merged;
7768   size_t relativecount = 0;
7769   asection *reldyn = 0;
7770   bfd_size_type amt;
7771 
7772   if (! is_elf_hash_table (info->hash))
7773     return FALSE;
7774 
7775   if (info->shared)
7776     abfd->flags |= DYNAMIC;
7777 
7778   dynamic = elf_hash_table (info)->dynamic_sections_created;
7779   dynobj = elf_hash_table (info)->dynobj;
7780 
7781   emit_relocs = (info->relocatable
7782 		 || info->emitrelocations
7783 		 || bed->elf_backend_emit_relocs);
7784 
7785   finfo.info = info;
7786   finfo.output_bfd = abfd;
7787   finfo.symstrtab = _bfd_elf_stringtab_init ();
7788   if (finfo.symstrtab == NULL)
7789     return FALSE;
7790 
7791   if (! dynamic)
7792     {
7793       finfo.dynsym_sec = NULL;
7794       finfo.hash_sec = NULL;
7795       finfo.symver_sec = NULL;
7796     }
7797   else
7798     {
7799       finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7800       finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7801       BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7802       finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7803       /* Note that it is OK if symver_sec is NULL.  */
7804     }
7805 
7806   finfo.contents = NULL;
7807   finfo.external_relocs = NULL;
7808   finfo.internal_relocs = NULL;
7809   finfo.external_syms = NULL;
7810   finfo.locsym_shndx = NULL;
7811   finfo.internal_syms = NULL;
7812   finfo.indices = NULL;
7813   finfo.sections = NULL;
7814   finfo.symbuf = NULL;
7815   finfo.symshndxbuf = NULL;
7816   finfo.symbuf_count = 0;
7817   finfo.shndxbuf_size = 0;
7818 
7819   /* Count up the number of relocations we will output for each output
7820      section, so that we know the sizes of the reloc sections.  We
7821      also figure out some maximum sizes.  */
7822   max_contents_size = 0;
7823   max_external_reloc_size = 0;
7824   max_internal_reloc_count = 0;
7825   max_sym_count = 0;
7826   max_sym_shndx_count = 0;
7827   merged = FALSE;
7828   for (o = abfd->sections; o != NULL; o = o->next)
7829     {
7830       struct bfd_elf_section_data *esdo = elf_section_data (o);
7831       o->reloc_count = 0;
7832 
7833       for (p = o->map_head.link_order; p != NULL; p = p->next)
7834 	{
7835 	  unsigned int reloc_count = 0;
7836 	  struct bfd_elf_section_data *esdi = NULL;
7837 	  unsigned int *rel_count1;
7838 
7839 	  if (p->type == bfd_section_reloc_link_order
7840 	      || p->type == bfd_symbol_reloc_link_order)
7841 	    reloc_count = 1;
7842 	  else if (p->type == bfd_indirect_link_order)
7843 	    {
7844 	      asection *sec;
7845 
7846 	      sec = p->u.indirect.section;
7847 	      esdi = elf_section_data (sec);
7848 
7849 	      /* Mark all sections which are to be included in the
7850 		 link.  This will normally be every section.  We need
7851 		 to do this so that we can identify any sections which
7852 		 the linker has decided to not include.  */
7853 	      sec->linker_mark = TRUE;
7854 
7855 	      if (sec->flags & SEC_MERGE)
7856 		merged = TRUE;
7857 
7858 	      if (info->relocatable || info->emitrelocations)
7859 		reloc_count = sec->reloc_count;
7860 	      else if (bed->elf_backend_count_relocs)
7861 		{
7862 		  Elf_Internal_Rela * relocs;
7863 
7864 		  relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7865 						      info->keep_memory);
7866 
7867 		  reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7868 
7869 		  if (elf_section_data (o)->relocs != relocs)
7870 		    free (relocs);
7871 		}
7872 
7873 	      if (sec->rawsize > max_contents_size)
7874 		max_contents_size = sec->rawsize;
7875 	      if (sec->size > max_contents_size)
7876 		max_contents_size = sec->size;
7877 
7878 	      /* We are interested in just local symbols, not all
7879 		 symbols.  */
7880 	      if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7881 		  && (sec->owner->flags & DYNAMIC) == 0)
7882 		{
7883 		  size_t sym_count;
7884 
7885 		  if (elf_bad_symtab (sec->owner))
7886 		    sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7887 				 / bed->s->sizeof_sym);
7888 		  else
7889 		    sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7890 
7891 		  if (sym_count > max_sym_count)
7892 		    max_sym_count = sym_count;
7893 
7894 		  if (sym_count > max_sym_shndx_count
7895 		      && elf_symtab_shndx (sec->owner) != 0)
7896 		    max_sym_shndx_count = sym_count;
7897 
7898 		  if ((sec->flags & SEC_RELOC) != 0)
7899 		    {
7900 		      size_t ext_size;
7901 
7902 		      ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7903 		      if (ext_size > max_external_reloc_size)
7904 			max_external_reloc_size = ext_size;
7905 		      if (sec->reloc_count > max_internal_reloc_count)
7906 			max_internal_reloc_count = sec->reloc_count;
7907 		    }
7908 		}
7909 	    }
7910 
7911 	  if (reloc_count == 0)
7912 	    continue;
7913 
7914 	  o->reloc_count += reloc_count;
7915 
7916 	  /* MIPS may have a mix of REL and RELA relocs on sections.
7917 	     To support this curious ABI we keep reloc counts in
7918 	     elf_section_data too.  We must be careful to add the
7919 	     relocations from the input section to the right output
7920 	     count.  FIXME: Get rid of one count.  We have
7921 	     o->reloc_count == esdo->rel_count + esdo->rel_count2.  */
7922 	  rel_count1 = &esdo->rel_count;
7923 	  if (esdi != NULL)
7924 	    {
7925 	      bfd_boolean same_size;
7926 	      bfd_size_type entsize1;
7927 
7928 	      entsize1 = esdi->rel_hdr.sh_entsize;
7929 	      BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7930 			  || entsize1 == bed->s->sizeof_rela);
7931 	      same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7932 
7933 	      if (!same_size)
7934 		rel_count1 = &esdo->rel_count2;
7935 
7936 	      if (esdi->rel_hdr2 != NULL)
7937 		{
7938 		  bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7939 		  unsigned int alt_count;
7940 		  unsigned int *rel_count2;
7941 
7942 		  BFD_ASSERT (entsize2 != entsize1
7943 			      && (entsize2 == bed->s->sizeof_rel
7944 				  || entsize2 == bed->s->sizeof_rela));
7945 
7946 		  rel_count2 = &esdo->rel_count2;
7947 		  if (!same_size)
7948 		    rel_count2 = &esdo->rel_count;
7949 
7950 		  /* The following is probably too simplistic if the
7951 		     backend counts output relocs unusually.  */
7952 		  BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7953 		  alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7954 		  *rel_count2 += alt_count;
7955 		  reloc_count -= alt_count;
7956 		}
7957 	    }
7958 	  *rel_count1 += reloc_count;
7959 	}
7960 
7961       if (o->reloc_count > 0)
7962 	o->flags |= SEC_RELOC;
7963       else
7964 	{
7965 	  /* Explicitly clear the SEC_RELOC flag.  The linker tends to
7966 	     set it (this is probably a bug) and if it is set
7967 	     assign_section_numbers will create a reloc section.  */
7968 	  o->flags &=~ SEC_RELOC;
7969 	}
7970 
7971       /* If the SEC_ALLOC flag is not set, force the section VMA to
7972 	 zero.  This is done in elf_fake_sections as well, but forcing
7973 	 the VMA to 0 here will ensure that relocs against these
7974 	 sections are handled correctly.  */
7975       if ((o->flags & SEC_ALLOC) == 0
7976 	  && ! o->user_set_vma)
7977 	o->vma = 0;
7978     }
7979 
7980   if (! info->relocatable && merged)
7981     elf_link_hash_traverse (elf_hash_table (info),
7982 			    _bfd_elf_link_sec_merge_syms, abfd);
7983 
7984   /* Figure out the file positions for everything but the symbol table
7985      and the relocs.  We set symcount to force assign_section_numbers
7986      to create a symbol table.  */
7987   bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7988   BFD_ASSERT (! abfd->output_has_begun);
7989   if (! _bfd_elf_compute_section_file_positions (abfd, info))
7990     goto error_return;
7991 
7992   /* Set sizes, and assign file positions for reloc sections.  */
7993   for (o = abfd->sections; o != NULL; o = o->next)
7994     {
7995       if ((o->flags & SEC_RELOC) != 0)
7996 	{
7997 	  if (!(_bfd_elf_link_size_reloc_section
7998 		(abfd, &elf_section_data (o)->rel_hdr, o)))
7999 	    goto error_return;
8000 
8001 	  if (elf_section_data (o)->rel_hdr2
8002 	      && !(_bfd_elf_link_size_reloc_section
8003 		   (abfd, elf_section_data (o)->rel_hdr2, o)))
8004 	    goto error_return;
8005 	}
8006 
8007       /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
8008 	 to count upwards while actually outputting the relocations.  */
8009       elf_section_data (o)->rel_count = 0;
8010       elf_section_data (o)->rel_count2 = 0;
8011     }
8012 
8013   _bfd_elf_assign_file_positions_for_relocs (abfd);
8014 
8015   /* We have now assigned file positions for all the sections except
8016      .symtab and .strtab.  We start the .symtab section at the current
8017      file position, and write directly to it.  We build the .strtab
8018      section in memory.  */
8019   bfd_get_symcount (abfd) = 0;
8020   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8021   /* sh_name is set in prep_headers.  */
8022   symtab_hdr->sh_type = SHT_SYMTAB;
8023   /* sh_flags, sh_addr and sh_size all start off zero.  */
8024   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8025   /* sh_link is set in assign_section_numbers.  */
8026   /* sh_info is set below.  */
8027   /* sh_offset is set just below.  */
8028   symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
8029 
8030   off = elf_tdata (abfd)->next_file_pos;
8031   off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
8032 
8033   /* Note that at this point elf_tdata (abfd)->next_file_pos is
8034      incorrect.  We do not yet know the size of the .symtab section.
8035      We correct next_file_pos below, after we do know the size.  */
8036 
8037   /* Allocate a buffer to hold swapped out symbols.  This is to avoid
8038      continuously seeking to the right position in the file.  */
8039   if (! info->keep_memory || max_sym_count < 20)
8040     finfo.symbuf_size = 20;
8041   else
8042     finfo.symbuf_size = max_sym_count;
8043   amt = finfo.symbuf_size;
8044   amt *= bed->s->sizeof_sym;
8045   finfo.symbuf = bfd_malloc (amt);
8046   if (finfo.symbuf == NULL)
8047     goto error_return;
8048   if (elf_numsections (abfd) > SHN_LORESERVE)
8049     {
8050       /* Wild guess at number of output symbols.  realloc'd as needed.  */
8051       amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
8052       finfo.shndxbuf_size = amt;
8053       amt *= sizeof (Elf_External_Sym_Shndx);
8054       finfo.symshndxbuf = bfd_zmalloc (amt);
8055       if (finfo.symshndxbuf == NULL)
8056 	goto error_return;
8057     }
8058 
8059   /* Start writing out the symbol table.  The first symbol is always a
8060      dummy symbol.  */
8061   if (info->strip != strip_all
8062       || emit_relocs)
8063     {
8064       elfsym.st_value = 0;
8065       elfsym.st_size = 0;
8066       elfsym.st_info = 0;
8067       elfsym.st_other = 0;
8068       elfsym.st_shndx = SHN_UNDEF;
8069       if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
8070 				 NULL))
8071 	goto error_return;
8072     }
8073 
8074   /* Output a symbol for each section.  We output these even if we are
8075      discarding local symbols, since they are used for relocs.  These
8076      symbols have no names.  We store the index of each one in the
8077      index field of the section, so that we can find it again when
8078      outputting relocs.  */
8079   if (info->strip != strip_all
8080       || emit_relocs)
8081     {
8082       elfsym.st_size = 0;
8083       elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8084       elfsym.st_other = 0;
8085       for (i = 1; i < elf_numsections (abfd); i++)
8086 	{
8087 	  o = bfd_section_from_elf_index (abfd, i);
8088 	  if (o != NULL)
8089 	    o->target_index = bfd_get_symcount (abfd);
8090 	  elfsym.st_shndx = i;
8091 	  if (info->relocatable || o == NULL)
8092 	    elfsym.st_value = 0;
8093 	  else
8094 	    elfsym.st_value = o->vma;
8095 	  if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
8096 	    goto error_return;
8097 	  if (i == SHN_LORESERVE - 1)
8098 	    i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
8099 	}
8100     }
8101 
8102   /* Allocate some memory to hold information read in from the input
8103      files.  */
8104   if (max_contents_size != 0)
8105     {
8106       finfo.contents = bfd_malloc (max_contents_size);
8107       if (finfo.contents == NULL)
8108 	goto error_return;
8109     }
8110 
8111   if (max_external_reloc_size != 0)
8112     {
8113       finfo.external_relocs = bfd_malloc (max_external_reloc_size);
8114       if (finfo.external_relocs == NULL)
8115 	goto error_return;
8116     }
8117 
8118   if (max_internal_reloc_count != 0)
8119     {
8120       amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
8121       amt *= sizeof (Elf_Internal_Rela);
8122       finfo.internal_relocs = bfd_malloc (amt);
8123       if (finfo.internal_relocs == NULL)
8124 	goto error_return;
8125     }
8126 
8127   if (max_sym_count != 0)
8128     {
8129       amt = max_sym_count * bed->s->sizeof_sym;
8130       finfo.external_syms = bfd_malloc (amt);
8131       if (finfo.external_syms == NULL)
8132 	goto error_return;
8133 
8134       amt = max_sym_count * sizeof (Elf_Internal_Sym);
8135       finfo.internal_syms = bfd_malloc (amt);
8136       if (finfo.internal_syms == NULL)
8137 	goto error_return;
8138 
8139       amt = max_sym_count * sizeof (long);
8140       finfo.indices = bfd_malloc (amt);
8141       if (finfo.indices == NULL)
8142 	goto error_return;
8143 
8144       amt = max_sym_count * sizeof (asection *);
8145       finfo.sections = bfd_malloc (amt);
8146       if (finfo.sections == NULL)
8147 	goto error_return;
8148     }
8149 
8150   if (max_sym_shndx_count != 0)
8151     {
8152       amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8153       finfo.locsym_shndx = bfd_malloc (amt);
8154       if (finfo.locsym_shndx == NULL)
8155 	goto error_return;
8156     }
8157 
8158   if (elf_hash_table (info)->tls_sec)
8159     {
8160       bfd_vma base, end = 0;
8161       asection *sec;
8162 
8163       for (sec = elf_hash_table (info)->tls_sec;
8164 	   sec && (sec->flags & SEC_THREAD_LOCAL);
8165 	   sec = sec->next)
8166 	{
8167 	  bfd_vma size = sec->size;
8168 
8169 	  if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
8170 	    {
8171 	      struct bfd_link_order *o;
8172 
8173 	      for (o = sec->map_head.link_order; o != NULL; o = o->next)
8174 		if (size < o->offset + o->size)
8175 		  size = o->offset + o->size;
8176 	    }
8177 	  end = sec->vma + size;
8178 	}
8179       base = elf_hash_table (info)->tls_sec->vma;
8180       end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
8181       elf_hash_table (info)->tls_size = end - base;
8182     }
8183 
8184   /* Reorder SHF_LINK_ORDER sections.  */
8185   for (o = abfd->sections; o != NULL; o = o->next)
8186     {
8187       if (!elf_fixup_link_order (abfd, o))
8188 	return FALSE;
8189     }
8190 
8191   /* Since ELF permits relocations to be against local symbols, we
8192      must have the local symbols available when we do the relocations.
8193      Since we would rather only read the local symbols once, and we
8194      would rather not keep them in memory, we handle all the
8195      relocations for a single input file at the same time.
8196 
8197      Unfortunately, there is no way to know the total number of local
8198      symbols until we have seen all of them, and the local symbol
8199      indices precede the global symbol indices.  This means that when
8200      we are generating relocatable output, and we see a reloc against
8201      a global symbol, we can not know the symbol index until we have
8202      finished examining all the local symbols to see which ones we are
8203      going to output.  To deal with this, we keep the relocations in
8204      memory, and don't output them until the end of the link.  This is
8205      an unfortunate waste of memory, but I don't see a good way around
8206      it.  Fortunately, it only happens when performing a relocatable
8207      link, which is not the common case.  FIXME: If keep_memory is set
8208      we could write the relocs out and then read them again; I don't
8209      know how bad the memory loss will be.  */
8210 
8211   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8212     sub->output_has_begun = FALSE;
8213   for (o = abfd->sections; o != NULL; o = o->next)
8214     {
8215       for (p = o->map_head.link_order; p != NULL; p = p->next)
8216 	{
8217 	  if (p->type == bfd_indirect_link_order
8218 	      && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
8219 		  == bfd_target_elf_flavour)
8220 	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
8221 	    {
8222 	      if (! sub->output_has_begun)
8223 		{
8224 		  if (! elf_link_input_bfd (&finfo, sub))
8225 		    goto error_return;
8226 		  sub->output_has_begun = TRUE;
8227 		}
8228 	    }
8229 	  else if (p->type == bfd_section_reloc_link_order
8230 		   || p->type == bfd_symbol_reloc_link_order)
8231 	    {
8232 	      if (! elf_reloc_link_order (abfd, info, o, p))
8233 		goto error_return;
8234 	    }
8235 	  else
8236 	    {
8237 	      if (! _bfd_default_link_order (abfd, info, o, p))
8238 		goto error_return;
8239 	    }
8240 	}
8241     }
8242 
8243   /* Output any global symbols that got converted to local in a
8244      version script or due to symbol visibility.  We do this in a
8245      separate step since ELF requires all local symbols to appear
8246      prior to any global symbols.  FIXME: We should only do this if
8247      some global symbols were, in fact, converted to become local.
8248      FIXME: Will this work correctly with the Irix 5 linker?  */
8249   eoinfo.failed = FALSE;
8250   eoinfo.finfo = &finfo;
8251   eoinfo.localsyms = TRUE;
8252   elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8253 			  &eoinfo);
8254   if (eoinfo.failed)
8255     return FALSE;
8256 
8257   /* That wrote out all the local symbols.  Finish up the symbol table
8258      with the global symbols. Even if we want to strip everything we
8259      can, we still need to deal with those global symbols that got
8260      converted to local in a version script.  */
8261 
8262   /* The sh_info field records the index of the first non local symbol.  */
8263   symtab_hdr->sh_info = bfd_get_symcount (abfd);
8264 
8265   if (dynamic
8266       && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8267     {
8268       Elf_Internal_Sym sym;
8269       bfd_byte *dynsym = finfo.dynsym_sec->contents;
8270       long last_local = 0;
8271 
8272       /* Write out the section symbols for the output sections.  */
8273       if (info->shared || elf_hash_table (info)->is_relocatable_executable)
8274 	{
8275 	  asection *s;
8276 
8277 	  sym.st_size = 0;
8278 	  sym.st_name = 0;
8279 	  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8280 	  sym.st_other = 0;
8281 
8282 	  for (s = abfd->sections; s != NULL; s = s->next)
8283 	    {
8284 	      int indx;
8285 	      bfd_byte *dest;
8286 	      long dynindx;
8287 
8288 	      dynindx = elf_section_data (s)->dynindx;
8289 	      if (dynindx <= 0)
8290 		continue;
8291 	      indx = elf_section_data (s)->this_idx;
8292 	      BFD_ASSERT (indx > 0);
8293 	      sym.st_shndx = indx;
8294 	      sym.st_value = s->vma;
8295 	      dest = dynsym + dynindx * bed->s->sizeof_sym;
8296 	      if (last_local < dynindx)
8297 		last_local = dynindx;
8298 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8299 	    }
8300 	}
8301 
8302       /* Write out the local dynsyms.  */
8303       if (elf_hash_table (info)->dynlocal)
8304 	{
8305 	  struct elf_link_local_dynamic_entry *e;
8306 	  for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8307 	    {
8308 	      asection *s;
8309 	      bfd_byte *dest;
8310 
8311 	      sym.st_size = e->isym.st_size;
8312 	      sym.st_other = e->isym.st_other;
8313 
8314 	      /* Copy the internal symbol as is.
8315 		 Note that we saved a word of storage and overwrote
8316 		 the original st_name with the dynstr_index.  */
8317 	      sym = e->isym;
8318 
8319 	      if (e->isym.st_shndx != SHN_UNDEF
8320 		  && (e->isym.st_shndx < SHN_LORESERVE
8321 		      || e->isym.st_shndx > SHN_HIRESERVE))
8322 		{
8323 		  s = bfd_section_from_elf_index (e->input_bfd,
8324 						  e->isym.st_shndx);
8325 
8326 		  sym.st_shndx =
8327 		    elf_section_data (s->output_section)->this_idx;
8328 		  sym.st_value = (s->output_section->vma
8329 				  + s->output_offset
8330 				  + e->isym.st_value);
8331 		}
8332 
8333 	      if (last_local < e->dynindx)
8334 		last_local = e->dynindx;
8335 
8336 	      dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8337 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8338 	    }
8339 	}
8340 
8341       elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8342 	last_local + 1;
8343     }
8344 
8345   /* We get the global symbols from the hash table.  */
8346   eoinfo.failed = FALSE;
8347   eoinfo.localsyms = FALSE;
8348   eoinfo.finfo = &finfo;
8349   elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8350 			  &eoinfo);
8351   if (eoinfo.failed)
8352     return FALSE;
8353 
8354   /* If backend needs to output some symbols not present in the hash
8355      table, do it now.  */
8356   if (bed->elf_backend_output_arch_syms)
8357     {
8358       typedef bfd_boolean (*out_sym_func)
8359 	(void *, const char *, Elf_Internal_Sym *, asection *,
8360 	 struct elf_link_hash_entry *);
8361 
8362       if (! ((*bed->elf_backend_output_arch_syms)
8363 	     (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8364 	return FALSE;
8365     }
8366 
8367   /* Flush all symbols to the file.  */
8368   if (! elf_link_flush_output_syms (&finfo, bed))
8369     return FALSE;
8370 
8371   /* Now we know the size of the symtab section.  */
8372   off += symtab_hdr->sh_size;
8373 
8374   symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8375   if (symtab_shndx_hdr->sh_name != 0)
8376     {
8377       symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8378       symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8379       symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8380       amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8381       symtab_shndx_hdr->sh_size = amt;
8382 
8383       off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8384 						       off, TRUE);
8385 
8386       if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8387 	  || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8388 	return FALSE;
8389     }
8390 
8391 
8392   /* Finish up and write out the symbol string table (.strtab)
8393      section.  */
8394   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8395   /* sh_name was set in prep_headers.  */
8396   symstrtab_hdr->sh_type = SHT_STRTAB;
8397   symstrtab_hdr->sh_flags = 0;
8398   symstrtab_hdr->sh_addr = 0;
8399   symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8400   symstrtab_hdr->sh_entsize = 0;
8401   symstrtab_hdr->sh_link = 0;
8402   symstrtab_hdr->sh_info = 0;
8403   /* sh_offset is set just below.  */
8404   symstrtab_hdr->sh_addralign = 1;
8405 
8406   off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8407   elf_tdata (abfd)->next_file_pos = off;
8408 
8409   if (bfd_get_symcount (abfd) > 0)
8410     {
8411       if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8412 	  || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8413 	return FALSE;
8414     }
8415 
8416   /* Adjust the relocs to have the correct symbol indices.  */
8417   for (o = abfd->sections; o != NULL; o = o->next)
8418     {
8419       if ((o->flags & SEC_RELOC) == 0)
8420 	continue;
8421 
8422       elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8423 			      elf_section_data (o)->rel_count,
8424 			      elf_section_data (o)->rel_hashes);
8425       if (elf_section_data (o)->rel_hdr2 != NULL)
8426 	elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8427 				elf_section_data (o)->rel_count2,
8428 				(elf_section_data (o)->rel_hashes
8429 				 + elf_section_data (o)->rel_count));
8430 
8431       /* Set the reloc_count field to 0 to prevent write_relocs from
8432 	 trying to swap the relocs out itself.  */
8433       o->reloc_count = 0;
8434     }
8435 
8436   if (dynamic && info->combreloc && dynobj != NULL)
8437     relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8438 
8439   /* If we are linking against a dynamic object, or generating a
8440      shared library, finish up the dynamic linking information.  */
8441   if (dynamic)
8442     {
8443       bfd_byte *dyncon, *dynconend;
8444 
8445       /* Fix up .dynamic entries.  */
8446       o = bfd_get_section_by_name (dynobj, ".dynamic");
8447       BFD_ASSERT (o != NULL);
8448 
8449       dyncon = o->contents;
8450       dynconend = o->contents + o->size;
8451       for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8452 	{
8453 	  Elf_Internal_Dyn dyn;
8454 	  const char *name;
8455 	  unsigned int type;
8456 
8457 	  bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8458 
8459 	  switch (dyn.d_tag)
8460 	    {
8461 	    default:
8462 	      continue;
8463 	    case DT_NULL:
8464 	      if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8465 		{
8466 		  switch (elf_section_data (reldyn)->this_hdr.sh_type)
8467 		    {
8468 		    case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8469 		    case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8470 		    default: continue;
8471 		    }
8472 		  dyn.d_un.d_val = relativecount;
8473 		  relativecount = 0;
8474 		  break;
8475 		}
8476 	      continue;
8477 
8478 	    case DT_INIT:
8479 	      name = info->init_function;
8480 	      goto get_sym;
8481 	    case DT_FINI:
8482 	      name = info->fini_function;
8483 	    get_sym:
8484 	      {
8485 		struct elf_link_hash_entry *h;
8486 
8487 		h = elf_link_hash_lookup (elf_hash_table (info), name,
8488 					  FALSE, FALSE, TRUE);
8489 		if (h != NULL
8490 		    && (h->root.type == bfd_link_hash_defined
8491 			|| h->root.type == bfd_link_hash_defweak))
8492 		  {
8493 		    dyn.d_un.d_val = h->root.u.def.value;
8494 		    o = h->root.u.def.section;
8495 		    if (o->output_section != NULL)
8496 		      dyn.d_un.d_val += (o->output_section->vma
8497 					 + o->output_offset);
8498 		    else
8499 		      {
8500 			/* The symbol is imported from another shared
8501 			   library and does not apply to this one.  */
8502 			dyn.d_un.d_val = 0;
8503 		      }
8504 		    break;
8505 		  }
8506 	      }
8507 	      continue;
8508 
8509 	    case DT_PREINIT_ARRAYSZ:
8510 	      name = ".preinit_array";
8511 	      goto get_size;
8512 	    case DT_INIT_ARRAYSZ:
8513 	      name = ".init_array";
8514 	      goto get_size;
8515 	    case DT_FINI_ARRAYSZ:
8516 	      name = ".fini_array";
8517 	    get_size:
8518 	      o = bfd_get_section_by_name (abfd, name);
8519 	      if (o == NULL)
8520 		{
8521 		  (*_bfd_error_handler)
8522 		    (_("%B: could not find output section %s"), abfd, name);
8523 		  goto error_return;
8524 		}
8525 	      if (o->size == 0)
8526 		(*_bfd_error_handler)
8527 		  (_("warning: %s section has zero size"), name);
8528 	      dyn.d_un.d_val = o->size;
8529 	      break;
8530 
8531 	    case DT_PREINIT_ARRAY:
8532 	      name = ".preinit_array";
8533 	      goto get_vma;
8534 	    case DT_INIT_ARRAY:
8535 	      name = ".init_array";
8536 	      goto get_vma;
8537 	    case DT_FINI_ARRAY:
8538 	      name = ".fini_array";
8539 	      goto get_vma;
8540 
8541 	    case DT_HASH:
8542 	      name = ".hash";
8543 	      goto get_vma;
8544 	    case DT_STRTAB:
8545 	      name = ".dynstr";
8546 	      goto get_vma;
8547 	    case DT_SYMTAB:
8548 	      name = ".dynsym";
8549 	      goto get_vma;
8550 	    case DT_VERDEF:
8551 	      name = ".gnu.version_d";
8552 	      goto get_vma;
8553 	    case DT_VERNEED:
8554 	      name = ".gnu.version_r";
8555 	      goto get_vma;
8556 	    case DT_VERSYM:
8557 	      name = ".gnu.version";
8558 	    get_vma:
8559 	      o = bfd_get_section_by_name (abfd, name);
8560 	      if (o == NULL)
8561 		{
8562 		  (*_bfd_error_handler)
8563 		    (_("%B: could not find output section %s"), abfd, name);
8564 		  goto error_return;
8565 		}
8566 	      dyn.d_un.d_ptr = o->vma;
8567 	      break;
8568 
8569 	    case DT_REL:
8570 	    case DT_RELA:
8571 	    case DT_RELSZ:
8572 	    case DT_RELASZ:
8573 	      if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8574 		type = SHT_REL;
8575 	      else
8576 		type = SHT_RELA;
8577 	      dyn.d_un.d_val = 0;
8578 	      for (i = 1; i < elf_numsections (abfd); i++)
8579 		{
8580 		  Elf_Internal_Shdr *hdr;
8581 
8582 		  hdr = elf_elfsections (abfd)[i];
8583 		  if (hdr->sh_type == type
8584 		      && (hdr->sh_flags & SHF_ALLOC) != 0)
8585 		    {
8586 		      if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8587 			dyn.d_un.d_val += hdr->sh_size;
8588 		      else
8589 			{
8590 			  if (dyn.d_un.d_val == 0
8591 			      || hdr->sh_addr < dyn.d_un.d_val)
8592 			    dyn.d_un.d_val = hdr->sh_addr;
8593 			}
8594 		    }
8595 		}
8596 	      break;
8597 	    }
8598 	  bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8599 	}
8600     }
8601 
8602   /* If we have created any dynamic sections, then output them.  */
8603   if (dynobj != NULL)
8604     {
8605       if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8606 	goto error_return;
8607 
8608       for (o = dynobj->sections; o != NULL; o = o->next)
8609 	{
8610 	  if ((o->flags & SEC_HAS_CONTENTS) == 0
8611 	      || o->size == 0
8612 	      || o->output_section == bfd_abs_section_ptr)
8613 	    continue;
8614 	  if ((o->flags & SEC_LINKER_CREATED) == 0)
8615 	    {
8616 	      /* At this point, we are only interested in sections
8617 		 created by _bfd_elf_link_create_dynamic_sections.  */
8618 	      continue;
8619 	    }
8620 	  if (elf_hash_table (info)->stab_info.stabstr == o)
8621 	    continue;
8622 	  if (elf_hash_table (info)->eh_info.hdr_sec == o)
8623 	    continue;
8624 	  if ((elf_section_data (o->output_section)->this_hdr.sh_type
8625 	       != SHT_STRTAB)
8626 	      || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8627 	    {
8628 	      if (! bfd_set_section_contents (abfd, o->output_section,
8629 					      o->contents,
8630 					      (file_ptr) o->output_offset,
8631 					      o->size))
8632 		goto error_return;
8633 	    }
8634 	  else
8635 	    {
8636 	      /* The contents of the .dynstr section are actually in a
8637 		 stringtab.  */
8638 	      off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8639 	      if (bfd_seek (abfd, off, SEEK_SET) != 0
8640 		  || ! _bfd_elf_strtab_emit (abfd,
8641 					     elf_hash_table (info)->dynstr))
8642 		goto error_return;
8643 	    }
8644 	}
8645     }
8646 
8647   if (info->relocatable)
8648     {
8649       bfd_boolean failed = FALSE;
8650 
8651       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8652       if (failed)
8653 	goto error_return;
8654     }
8655 
8656   /* If we have optimized stabs strings, output them.  */
8657   if (elf_hash_table (info)->stab_info.stabstr != NULL)
8658     {
8659       if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8660 	goto error_return;
8661     }
8662 
8663   if (info->eh_frame_hdr)
8664     {
8665       if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8666 	goto error_return;
8667     }
8668 
8669   if (finfo.symstrtab != NULL)
8670     _bfd_stringtab_free (finfo.symstrtab);
8671   if (finfo.contents != NULL)
8672     free (finfo.contents);
8673   if (finfo.external_relocs != NULL)
8674     free (finfo.external_relocs);
8675   if (finfo.internal_relocs != NULL)
8676     free (finfo.internal_relocs);
8677   if (finfo.external_syms != NULL)
8678     free (finfo.external_syms);
8679   if (finfo.locsym_shndx != NULL)
8680     free (finfo.locsym_shndx);
8681   if (finfo.internal_syms != NULL)
8682     free (finfo.internal_syms);
8683   if (finfo.indices != NULL)
8684     free (finfo.indices);
8685   if (finfo.sections != NULL)
8686     free (finfo.sections);
8687   if (finfo.symbuf != NULL)
8688     free (finfo.symbuf);
8689   if (finfo.symshndxbuf != NULL)
8690     free (finfo.symshndxbuf);
8691   for (o = abfd->sections; o != NULL; o = o->next)
8692     {
8693       if ((o->flags & SEC_RELOC) != 0
8694 	  && elf_section_data (o)->rel_hashes != NULL)
8695 	free (elf_section_data (o)->rel_hashes);
8696     }
8697 
8698   elf_tdata (abfd)->linker = TRUE;
8699 
8700   return TRUE;
8701 
8702  error_return:
8703   if (finfo.symstrtab != NULL)
8704     _bfd_stringtab_free (finfo.symstrtab);
8705   if (finfo.contents != NULL)
8706     free (finfo.contents);
8707   if (finfo.external_relocs != NULL)
8708     free (finfo.external_relocs);
8709   if (finfo.internal_relocs != NULL)
8710     free (finfo.internal_relocs);
8711   if (finfo.external_syms != NULL)
8712     free (finfo.external_syms);
8713   if (finfo.locsym_shndx != NULL)
8714     free (finfo.locsym_shndx);
8715   if (finfo.internal_syms != NULL)
8716     free (finfo.internal_syms);
8717   if (finfo.indices != NULL)
8718     free (finfo.indices);
8719   if (finfo.sections != NULL)
8720     free (finfo.sections);
8721   if (finfo.symbuf != NULL)
8722     free (finfo.symbuf);
8723   if (finfo.symshndxbuf != NULL)
8724     free (finfo.symshndxbuf);
8725   for (o = abfd->sections; o != NULL; o = o->next)
8726     {
8727       if ((o->flags & SEC_RELOC) != 0
8728 	  && elf_section_data (o)->rel_hashes != NULL)
8729 	free (elf_section_data (o)->rel_hashes);
8730     }
8731 
8732   return FALSE;
8733 }
8734 
8735 /* Garbage collect unused sections.  */
8736 
8737 /* The mark phase of garbage collection.  For a given section, mark
8738    it and any sections in this section's group, and all the sections
8739    which define symbols to which it refers.  */
8740 
8741 typedef asection * (*gc_mark_hook_fn)
8742   (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8743    struct elf_link_hash_entry *, Elf_Internal_Sym *);
8744 
8745 bfd_boolean
_bfd_elf_gc_mark(struct bfd_link_info * info,asection * sec,gc_mark_hook_fn gc_mark_hook)8746 _bfd_elf_gc_mark (struct bfd_link_info *info,
8747 		  asection *sec,
8748 		  gc_mark_hook_fn gc_mark_hook)
8749 {
8750   bfd_boolean ret;
8751   bfd_boolean is_eh;
8752   asection *group_sec;
8753 
8754   sec->gc_mark = 1;
8755 
8756   /* Mark all the sections in the group.  */
8757   group_sec = elf_section_data (sec)->next_in_group;
8758   if (group_sec && !group_sec->gc_mark)
8759     if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8760       return FALSE;
8761 
8762   /* Look through the section relocs.  */
8763   ret = TRUE;
8764   is_eh = strcmp (sec->name, ".eh_frame") == 0;
8765   if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8766     {
8767       Elf_Internal_Rela *relstart, *rel, *relend;
8768       Elf_Internal_Shdr *symtab_hdr;
8769       struct elf_link_hash_entry **sym_hashes;
8770       size_t nlocsyms;
8771       size_t extsymoff;
8772       bfd *input_bfd = sec->owner;
8773       const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8774       Elf_Internal_Sym *isym = NULL;
8775       int r_sym_shift;
8776 
8777       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8778       sym_hashes = elf_sym_hashes (input_bfd);
8779 
8780       /* Read the local symbols.  */
8781       if (elf_bad_symtab (input_bfd))
8782 	{
8783 	  nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8784 	  extsymoff = 0;
8785 	}
8786       else
8787 	extsymoff = nlocsyms = symtab_hdr->sh_info;
8788 
8789       isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8790       if (isym == NULL && nlocsyms != 0)
8791 	{
8792 	  isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8793 				       NULL, NULL, NULL);
8794 	  if (isym == NULL)
8795 	    return FALSE;
8796 	}
8797 
8798       /* Read the relocations.  */
8799       relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8800 					    info->keep_memory);
8801       if (relstart == NULL)
8802 	{
8803 	  ret = FALSE;
8804 	  goto out1;
8805 	}
8806       relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8807 
8808       if (bed->s->arch_size == 32)
8809 	r_sym_shift = 8;
8810       else
8811 	r_sym_shift = 32;
8812 
8813       for (rel = relstart; rel < relend; rel++)
8814 	{
8815 	  unsigned long r_symndx;
8816 	  asection *rsec;
8817 	  struct elf_link_hash_entry *h;
8818 
8819 	  r_symndx = rel->r_info >> r_sym_shift;
8820 	  if (r_symndx == 0)
8821 	    continue;
8822 
8823 	  if (r_symndx >= nlocsyms
8824 	      || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8825 	    {
8826 	      h = sym_hashes[r_symndx - extsymoff];
8827 	      while (h->root.type == bfd_link_hash_indirect
8828 		     || h->root.type == bfd_link_hash_warning)
8829 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
8830 	      rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8831 	    }
8832 	  else
8833 	    {
8834 	      rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8835 	    }
8836 
8837 	  if (rsec && !rsec->gc_mark)
8838 	    {
8839 	      if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8840 		rsec->gc_mark = 1;
8841 	      else if (is_eh)
8842 		rsec->gc_mark_from_eh = 1;
8843 	      else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8844 		{
8845 		  ret = FALSE;
8846 		  goto out2;
8847 		}
8848 	    }
8849 	}
8850 
8851     out2:
8852       if (elf_section_data (sec)->relocs != relstart)
8853 	free (relstart);
8854     out1:
8855       if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8856 	{
8857 	  if (! info->keep_memory)
8858 	    free (isym);
8859 	  else
8860 	    symtab_hdr->contents = (unsigned char *) isym;
8861 	}
8862     }
8863 
8864   return ret;
8865 }
8866 
8867 /* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
8868 
8869 static bfd_boolean
elf_gc_sweep_symbol(struct elf_link_hash_entry * h,void * idxptr)8870 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
8871 {
8872   int *idx = idxptr;
8873 
8874   if (h->root.type == bfd_link_hash_warning)
8875     h = (struct elf_link_hash_entry *) h->root.u.i.link;
8876 
8877   if (h->dynindx != -1
8878       && ((h->root.type != bfd_link_hash_defined
8879 	   && h->root.type != bfd_link_hash_defweak)
8880 	  || h->root.u.def.section->gc_mark))
8881     h->dynindx = (*idx)++;
8882 
8883   return TRUE;
8884 }
8885 
8886 /* The sweep phase of garbage collection.  Remove all garbage sections.  */
8887 
8888 typedef bfd_boolean (*gc_sweep_hook_fn)
8889   (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8890 
8891 static bfd_boolean
elf_gc_sweep(struct bfd_link_info * info,gc_sweep_hook_fn gc_sweep_hook)8892 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
8893 {
8894   bfd *sub;
8895 
8896   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8897     {
8898       asection *o;
8899 
8900       if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8901 	continue;
8902 
8903       for (o = sub->sections; o != NULL; o = o->next)
8904 	{
8905 	  /* Keep debug and special sections.  */
8906 	  if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8907 	      || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8908 	    o->gc_mark = 1;
8909 
8910 	  if (o->gc_mark)
8911 	    continue;
8912 
8913 	  /* Keep .gcc_except_table.* if the associated .text.* is
8914 	     marked.  This isn't very nice, but the proper solution,
8915 	     splitting .eh_frame up and using comdat doesn't pan out
8916 	     easily due to needing special relocs to handle the
8917 	     difference of two symbols in separate sections.
8918 	     Don't keep code sections referenced by .eh_frame.  */
8919 	  if (o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)
8920 	    {
8921 	      if (strncmp (o->name, ".gcc_except_table.", 18) == 0)
8922 		{
8923 		  unsigned long len;
8924 		  char *fn_name;
8925 		  asection *fn_text;
8926 
8927 		  len = strlen (o->name + 18) + 1;
8928 		  fn_name = bfd_malloc (len + 6);
8929 		  if (fn_name == NULL)
8930 		    return FALSE;
8931 		  memcpy (fn_name, ".text.", 6);
8932 		  memcpy (fn_name + 6, o->name + 18, len);
8933 		  fn_text = bfd_get_section_by_name (sub, fn_name);
8934 		  free (fn_name);
8935 		  if (fn_text != NULL && fn_text->gc_mark)
8936 		    o->gc_mark = 1;
8937 		}
8938 
8939 	      /* If not using specially named exception table section,
8940 		 then keep whatever we are using.  */
8941 	      else
8942 		o->gc_mark = 1;
8943 
8944 	      if (o->gc_mark)
8945 		continue;
8946 	    }
8947 
8948 	  /* Skip sweeping sections already excluded.  */
8949 	  if (o->flags & SEC_EXCLUDE)
8950 	    continue;
8951 
8952 	  /* Since this is early in the link process, it is simple
8953 	     to remove a section from the output.  */
8954 	  o->flags |= SEC_EXCLUDE;
8955 
8956 	  /* But we also have to update some of the relocation
8957 	     info we collected before.  */
8958 	  if (gc_sweep_hook
8959 	      && (o->flags & SEC_RELOC) != 0
8960 	      && o->reloc_count > 0
8961 	      && !bfd_is_abs_section (o->output_section))
8962 	    {
8963 	      Elf_Internal_Rela *internal_relocs;
8964 	      bfd_boolean r;
8965 
8966 	      internal_relocs
8967 		= _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8968 					     info->keep_memory);
8969 	      if (internal_relocs == NULL)
8970 		return FALSE;
8971 
8972 	      r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8973 
8974 	      if (elf_section_data (o)->relocs != internal_relocs)
8975 		free (internal_relocs);
8976 
8977 	      if (!r)
8978 		return FALSE;
8979 	    }
8980 	}
8981     }
8982 
8983   /* Remove the symbols that were in the swept sections from the dynamic
8984      symbol table.  GCFIXME: Anyone know how to get them out of the
8985      static symbol table as well?  */
8986   {
8987     int i = 0;
8988 
8989     elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
8990 
8991     elf_hash_table (info)->dynsymcount = i;
8992   }
8993 
8994   return TRUE;
8995 }
8996 
8997 /* Propagate collected vtable information.  This is called through
8998    elf_link_hash_traverse.  */
8999 
9000 static bfd_boolean
elf_gc_propagate_vtable_entries_used(struct elf_link_hash_entry * h,void * okp)9001 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
9002 {
9003   if (h->root.type == bfd_link_hash_warning)
9004     h = (struct elf_link_hash_entry *) h->root.u.i.link;
9005 
9006   /* Those that are not vtables.  */
9007   if (h->vtable == NULL || h->vtable->parent == NULL)
9008     return TRUE;
9009 
9010   /* Those vtables that do not have parents, we cannot merge.  */
9011   if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
9012     return TRUE;
9013 
9014   /* If we've already been done, exit.  */
9015   if (h->vtable->used && h->vtable->used[-1])
9016     return TRUE;
9017 
9018   /* Make sure the parent's table is up to date.  */
9019   elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
9020 
9021   if (h->vtable->used == NULL)
9022     {
9023       /* None of this table's entries were referenced.  Re-use the
9024 	 parent's table.  */
9025       h->vtable->used = h->vtable->parent->vtable->used;
9026       h->vtable->size = h->vtable->parent->vtable->size;
9027     }
9028   else
9029     {
9030       size_t n;
9031       bfd_boolean *cu, *pu;
9032 
9033       /* Or the parent's entries into ours.  */
9034       cu = h->vtable->used;
9035       cu[-1] = TRUE;
9036       pu = h->vtable->parent->vtable->used;
9037       if (pu != NULL)
9038 	{
9039 	  const struct elf_backend_data *bed;
9040 	  unsigned int log_file_align;
9041 
9042 	  bed = get_elf_backend_data (h->root.u.def.section->owner);
9043 	  log_file_align = bed->s->log_file_align;
9044 	  n = h->vtable->parent->vtable->size >> log_file_align;
9045 	  while (n--)
9046 	    {
9047 	      if (*pu)
9048 		*cu = TRUE;
9049 	      pu++;
9050 	      cu++;
9051 	    }
9052 	}
9053     }
9054 
9055   return TRUE;
9056 }
9057 
9058 static bfd_boolean
elf_gc_smash_unused_vtentry_relocs(struct elf_link_hash_entry * h,void * okp)9059 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
9060 {
9061   asection *sec;
9062   bfd_vma hstart, hend;
9063   Elf_Internal_Rela *relstart, *relend, *rel;
9064   const struct elf_backend_data *bed;
9065   unsigned int log_file_align;
9066 
9067   if (h->root.type == bfd_link_hash_warning)
9068     h = (struct elf_link_hash_entry *) h->root.u.i.link;
9069 
9070   /* Take care of both those symbols that do not describe vtables as
9071      well as those that are not loaded.  */
9072   if (h->vtable == NULL || h->vtable->parent == NULL)
9073     return TRUE;
9074 
9075   BFD_ASSERT (h->root.type == bfd_link_hash_defined
9076 	      || h->root.type == bfd_link_hash_defweak);
9077 
9078   sec = h->root.u.def.section;
9079   hstart = h->root.u.def.value;
9080   hend = hstart + h->size;
9081 
9082   relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
9083   if (!relstart)
9084     return *(bfd_boolean *) okp = FALSE;
9085   bed = get_elf_backend_data (sec->owner);
9086   log_file_align = bed->s->log_file_align;
9087 
9088   relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
9089 
9090   for (rel = relstart; rel < relend; ++rel)
9091     if (rel->r_offset >= hstart && rel->r_offset < hend)
9092       {
9093 	/* If the entry is in use, do nothing.  */
9094 	if (h->vtable->used
9095 	    && (rel->r_offset - hstart) < h->vtable->size)
9096 	  {
9097 	    bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
9098 	    if (h->vtable->used[entry])
9099 	      continue;
9100 	  }
9101 	/* Otherwise, kill it.  */
9102 	rel->r_offset = rel->r_info = rel->r_addend = 0;
9103       }
9104 
9105   return TRUE;
9106 }
9107 
9108 /* Mark sections containing dynamically referenced symbols.  This is called
9109    through elf_link_hash_traverse.  */
9110 
9111 static bfd_boolean
elf_gc_mark_dynamic_ref_symbol(struct elf_link_hash_entry * h,void * okp ATTRIBUTE_UNUSED)9112 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h,
9113 				void *okp ATTRIBUTE_UNUSED)
9114 {
9115   if (h->root.type == bfd_link_hash_warning)
9116     h = (struct elf_link_hash_entry *) h->root.u.i.link;
9117 
9118   if ((h->root.type == bfd_link_hash_defined
9119        || h->root.type == bfd_link_hash_defweak)
9120       && h->ref_dynamic)
9121     h->root.u.def.section->flags |= SEC_KEEP;
9122 
9123   return TRUE;
9124 }
9125 
9126 /* Do mark and sweep of unused sections.  */
9127 
9128 bfd_boolean
bfd_elf_gc_sections(bfd * abfd,struct bfd_link_info * info)9129 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
9130 {
9131   bfd_boolean ok = TRUE;
9132   bfd *sub;
9133   asection * (*gc_mark_hook)
9134     (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
9135      struct elf_link_hash_entry *h, Elf_Internal_Sym *);
9136 
9137   if (!info->gc_sections)
9138     return bfd_generic_gc_sections (abfd, info);
9139 
9140   if (!get_elf_backend_data (abfd)->can_gc_sections
9141       || info->relocatable
9142       || info->emitrelocations
9143       || info->shared
9144       || !is_elf_hash_table (info->hash))
9145     {
9146       (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
9147       return TRUE;
9148     }
9149 
9150   /* Apply transitive closure to the vtable entry usage info.  */
9151   elf_link_hash_traverse (elf_hash_table (info),
9152 			  elf_gc_propagate_vtable_entries_used,
9153 			  &ok);
9154   if (!ok)
9155     return FALSE;
9156 
9157   /* Kill the vtable relocations that were not used.  */
9158   elf_link_hash_traverse (elf_hash_table (info),
9159 			  elf_gc_smash_unused_vtentry_relocs,
9160 			  &ok);
9161   if (!ok)
9162     return FALSE;
9163 
9164   /* Mark dynamically referenced symbols.  */
9165   if (elf_hash_table (info)->dynamic_sections_created)
9166     elf_link_hash_traverse (elf_hash_table (info),
9167 			    elf_gc_mark_dynamic_ref_symbol,
9168 			    &ok);
9169   if (!ok)
9170     return FALSE;
9171 
9172   /* Grovel through relocs to find out who stays ...  */
9173   gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
9174   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9175     {
9176       asection *o;
9177 
9178       if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9179 	continue;
9180 
9181       for (o = sub->sections; o != NULL; o = o->next)
9182 	if ((o->flags & SEC_KEEP) != 0 && !o->gc_mark)
9183 	  if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9184 	    return FALSE;
9185     }
9186 
9187   /* ... and mark SEC_EXCLUDE for those that go.  */
9188   if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
9189     return FALSE;
9190 
9191   return TRUE;
9192 }
9193 
9194 /* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
9195 
9196 bfd_boolean
bfd_elf_gc_record_vtinherit(bfd * abfd,asection * sec,struct elf_link_hash_entry * h,bfd_vma offset)9197 bfd_elf_gc_record_vtinherit (bfd *abfd,
9198 			     asection *sec,
9199 			     struct elf_link_hash_entry *h,
9200 			     bfd_vma offset)
9201 {
9202   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
9203   struct elf_link_hash_entry **search, *child;
9204   bfd_size_type extsymcount;
9205   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9206 
9207   /* The sh_info field of the symtab header tells us where the
9208      external symbols start.  We don't care about the local symbols at
9209      this point.  */
9210   extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
9211   if (!elf_bad_symtab (abfd))
9212     extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
9213 
9214   sym_hashes = elf_sym_hashes (abfd);
9215   sym_hashes_end = sym_hashes + extsymcount;
9216 
9217   /* Hunt down the child symbol, which is in this section at the same
9218      offset as the relocation.  */
9219   for (search = sym_hashes; search != sym_hashes_end; ++search)
9220     {
9221       if ((child = *search) != NULL
9222 	  && (child->root.type == bfd_link_hash_defined
9223 	      || child->root.type == bfd_link_hash_defweak)
9224 	  && child->root.u.def.section == sec
9225 	  && child->root.u.def.value == offset)
9226 	goto win;
9227     }
9228 
9229   (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
9230 			 abfd, sec, (unsigned long) offset);
9231   bfd_set_error (bfd_error_invalid_operation);
9232   return FALSE;
9233 
9234  win:
9235   if (!child->vtable)
9236     {
9237       child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
9238       if (!child->vtable)
9239 	return FALSE;
9240     }
9241   if (!h)
9242     {
9243       /* This *should* only be the absolute section.  It could potentially
9244 	 be that someone has defined a non-global vtable though, which
9245 	 would be bad.  It isn't worth paging in the local symbols to be
9246 	 sure though; that case should simply be handled by the assembler.  */
9247 
9248       child->vtable->parent = (struct elf_link_hash_entry *) -1;
9249     }
9250   else
9251     child->vtable->parent = h;
9252 
9253   return TRUE;
9254 }
9255 
9256 /* Called from check_relocs to record the existence of a VTENTRY reloc.  */
9257 
9258 bfd_boolean
bfd_elf_gc_record_vtentry(bfd * abfd ATTRIBUTE_UNUSED,asection * sec ATTRIBUTE_UNUSED,struct elf_link_hash_entry * h,bfd_vma addend)9259 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
9260 			   asection *sec ATTRIBUTE_UNUSED,
9261 			   struct elf_link_hash_entry *h,
9262 			   bfd_vma addend)
9263 {
9264   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9265   unsigned int log_file_align = bed->s->log_file_align;
9266 
9267   if (!h->vtable)
9268     {
9269       h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
9270       if (!h->vtable)
9271 	return FALSE;
9272     }
9273 
9274   if (addend >= h->vtable->size)
9275     {
9276       size_t size, bytes, file_align;
9277       bfd_boolean *ptr = h->vtable->used;
9278 
9279       /* While the symbol is undefined, we have to be prepared to handle
9280 	 a zero size.  */
9281       file_align = 1 << log_file_align;
9282       if (h->root.type == bfd_link_hash_undefined)
9283 	size = addend + file_align;
9284       else
9285 	{
9286 	  size = h->size;
9287 	  if (addend >= size)
9288 	    {
9289 	      /* Oops!  We've got a reference past the defined end of
9290 		 the table.  This is probably a bug -- shall we warn?  */
9291 	      size = addend + file_align;
9292 	    }
9293 	}
9294       size = (size + file_align - 1) & -file_align;
9295 
9296       /* Allocate one extra entry for use as a "done" flag for the
9297 	 consolidation pass.  */
9298       bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9299 
9300       if (ptr)
9301 	{
9302 	  ptr = bfd_realloc (ptr - 1, bytes);
9303 
9304 	  if (ptr != NULL)
9305 	    {
9306 	      size_t oldbytes;
9307 
9308 	      oldbytes = (((h->vtable->size >> log_file_align) + 1)
9309 			  * sizeof (bfd_boolean));
9310 	      memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9311 	    }
9312 	}
9313       else
9314 	ptr = bfd_zmalloc (bytes);
9315 
9316       if (ptr == NULL)
9317 	return FALSE;
9318 
9319       /* And arrange for that done flag to be at index -1.  */
9320       h->vtable->used = ptr + 1;
9321       h->vtable->size = size;
9322     }
9323 
9324   h->vtable->used[addend >> log_file_align] = TRUE;
9325 
9326   return TRUE;
9327 }
9328 
9329 struct alloc_got_off_arg {
9330   bfd_vma gotoff;
9331   unsigned int got_elt_size;
9332 };
9333 
9334 /* We need a special top-level link routine to convert got reference counts
9335    to real got offsets.  */
9336 
9337 static bfd_boolean
elf_gc_allocate_got_offsets(struct elf_link_hash_entry * h,void * arg)9338 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
9339 {
9340   struct alloc_got_off_arg *gofarg = arg;
9341 
9342   if (h->root.type == bfd_link_hash_warning)
9343     h = (struct elf_link_hash_entry *) h->root.u.i.link;
9344 
9345   if (h->got.refcount > 0)
9346     {
9347       h->got.offset = gofarg->gotoff;
9348       gofarg->gotoff += gofarg->got_elt_size;
9349     }
9350   else
9351     h->got.offset = (bfd_vma) -1;
9352 
9353   return TRUE;
9354 }
9355 
9356 /* And an accompanying bit to work out final got entry offsets once
9357    we're done.  Should be called from final_link.  */
9358 
9359 bfd_boolean
bfd_elf_gc_common_finalize_got_offsets(bfd * abfd,struct bfd_link_info * info)9360 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
9361 					struct bfd_link_info *info)
9362 {
9363   bfd *i;
9364   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9365   bfd_vma gotoff;
9366   unsigned int got_elt_size = bed->s->arch_size / 8;
9367   struct alloc_got_off_arg gofarg;
9368 
9369   if (! is_elf_hash_table (info->hash))
9370     return FALSE;
9371 
9372   /* The GOT offset is relative to the .got section, but the GOT header is
9373      put into the .got.plt section, if the backend uses it.  */
9374   if (bed->want_got_plt)
9375     gotoff = 0;
9376   else
9377     gotoff = bed->got_header_size;
9378 
9379   /* Do the local .got entries first.  */
9380   for (i = info->input_bfds; i; i = i->link_next)
9381     {
9382       bfd_signed_vma *local_got;
9383       bfd_size_type j, locsymcount;
9384       Elf_Internal_Shdr *symtab_hdr;
9385 
9386       if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9387 	continue;
9388 
9389       local_got = elf_local_got_refcounts (i);
9390       if (!local_got)
9391 	continue;
9392 
9393       symtab_hdr = &elf_tdata (i)->symtab_hdr;
9394       if (elf_bad_symtab (i))
9395 	locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9396       else
9397 	locsymcount = symtab_hdr->sh_info;
9398 
9399       for (j = 0; j < locsymcount; ++j)
9400 	{
9401 	  if (local_got[j] > 0)
9402 	    {
9403 	      local_got[j] = gotoff;
9404 	      gotoff += got_elt_size;
9405 	    }
9406 	  else
9407 	    local_got[j] = (bfd_vma) -1;
9408 	}
9409     }
9410 
9411   /* Then the global .got entries.  .plt refcounts are handled by
9412      adjust_dynamic_symbol  */
9413   gofarg.gotoff = gotoff;
9414   gofarg.got_elt_size = got_elt_size;
9415   elf_link_hash_traverse (elf_hash_table (info),
9416 			  elf_gc_allocate_got_offsets,
9417 			  &gofarg);
9418   return TRUE;
9419 }
9420 
9421 /* Many folk need no more in the way of final link than this, once
9422    got entry reference counting is enabled.  */
9423 
9424 bfd_boolean
bfd_elf_gc_common_final_link(bfd * abfd,struct bfd_link_info * info)9425 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9426 {
9427   if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9428     return FALSE;
9429 
9430   /* Invoke the regular ELF backend linker to do all the work.  */
9431   return bfd_elf_final_link (abfd, info);
9432 }
9433 
9434 bfd_boolean
bfd_elf_reloc_symbol_deleted_p(bfd_vma offset,void * cookie)9435 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9436 {
9437   struct elf_reloc_cookie *rcookie = cookie;
9438 
9439   if (rcookie->bad_symtab)
9440     rcookie->rel = rcookie->rels;
9441 
9442   for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9443     {
9444       unsigned long r_symndx;
9445 
9446       if (! rcookie->bad_symtab)
9447 	if (rcookie->rel->r_offset > offset)
9448 	  return FALSE;
9449       if (rcookie->rel->r_offset != offset)
9450 	continue;
9451 
9452       r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9453       if (r_symndx == SHN_UNDEF)
9454 	return TRUE;
9455 
9456       if (r_symndx >= rcookie->locsymcount
9457 	  || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9458 	{
9459 	  struct elf_link_hash_entry *h;
9460 
9461 	  h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9462 
9463 	  while (h->root.type == bfd_link_hash_indirect
9464 		 || h->root.type == bfd_link_hash_warning)
9465 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
9466 
9467 	  if ((h->root.type == bfd_link_hash_defined
9468 	       || h->root.type == bfd_link_hash_defweak)
9469 	      && elf_discarded_section (h->root.u.def.section))
9470 	    return TRUE;
9471 	  else
9472 	    return FALSE;
9473 	}
9474       else
9475 	{
9476 	  /* It's not a relocation against a global symbol,
9477 	     but it could be a relocation against a local
9478 	     symbol for a discarded section.  */
9479 	  asection *isec;
9480 	  Elf_Internal_Sym *isym;
9481 
9482 	  /* Need to: get the symbol; get the section.  */
9483 	  isym = &rcookie->locsyms[r_symndx];
9484 	  if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9485 	    {
9486 	      isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9487 	      if (isec != NULL && elf_discarded_section (isec))
9488 		return TRUE;
9489 	    }
9490 	}
9491       return FALSE;
9492     }
9493   return FALSE;
9494 }
9495 
9496 /* Discard unneeded references to discarded sections.
9497    Returns TRUE if any section's size was changed.  */
9498 /* This function assumes that the relocations are in sorted order,
9499    which is true for all known assemblers.  */
9500 
9501 bfd_boolean
bfd_elf_discard_info(bfd * output_bfd,struct bfd_link_info * info)9502 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9503 {
9504   struct elf_reloc_cookie cookie;
9505   asection *stab, *eh;
9506   Elf_Internal_Shdr *symtab_hdr;
9507   const struct elf_backend_data *bed;
9508   bfd *abfd;
9509   unsigned int count;
9510   bfd_boolean ret = FALSE;
9511 
9512   if (info->traditional_format
9513       || !is_elf_hash_table (info->hash))
9514     return FALSE;
9515 
9516   for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9517     {
9518       if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9519 	continue;
9520 
9521       bed = get_elf_backend_data (abfd);
9522 
9523       if ((abfd->flags & DYNAMIC) != 0)
9524 	continue;
9525 
9526       eh = bfd_get_section_by_name (abfd, ".eh_frame");
9527       if (info->relocatable
9528 	  || (eh != NULL
9529 	      && (eh->size == 0
9530 		  || bfd_is_abs_section (eh->output_section))))
9531 	eh = NULL;
9532 
9533       stab = bfd_get_section_by_name (abfd, ".stab");
9534       if (stab != NULL
9535 	  && (stab->size == 0
9536 	      || bfd_is_abs_section (stab->output_section)
9537 	      || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9538 	stab = NULL;
9539 
9540       if (stab == NULL
9541 	  && eh == NULL
9542 	  && bed->elf_backend_discard_info == NULL)
9543 	continue;
9544 
9545       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9546       cookie.abfd = abfd;
9547       cookie.sym_hashes = elf_sym_hashes (abfd);
9548       cookie.bad_symtab = elf_bad_symtab (abfd);
9549       if (cookie.bad_symtab)
9550 	{
9551 	  cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9552 	  cookie.extsymoff = 0;
9553 	}
9554       else
9555 	{
9556 	  cookie.locsymcount = symtab_hdr->sh_info;
9557 	  cookie.extsymoff = symtab_hdr->sh_info;
9558 	}
9559 
9560       if (bed->s->arch_size == 32)
9561 	cookie.r_sym_shift = 8;
9562       else
9563 	cookie.r_sym_shift = 32;
9564 
9565       cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9566       if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9567 	{
9568 	  cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9569 						 cookie.locsymcount, 0,
9570 						 NULL, NULL, NULL);
9571 	  if (cookie.locsyms == NULL)
9572 	    return FALSE;
9573 	}
9574 
9575       if (stab != NULL)
9576 	{
9577 	  cookie.rels = NULL;
9578 	  count = stab->reloc_count;
9579 	  if (count != 0)
9580 	    cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9581 						     info->keep_memory);
9582 	  if (cookie.rels != NULL)
9583 	    {
9584 	      cookie.rel = cookie.rels;
9585 	      cookie.relend = cookie.rels;
9586 	      cookie.relend += count * bed->s->int_rels_per_ext_rel;
9587 	      if (_bfd_discard_section_stabs (abfd, stab,
9588 					      elf_section_data (stab)->sec_info,
9589 					      bfd_elf_reloc_symbol_deleted_p,
9590 					      &cookie))
9591 		ret = TRUE;
9592 	      if (elf_section_data (stab)->relocs != cookie.rels)
9593 		free (cookie.rels);
9594 	    }
9595 	}
9596 
9597       if (eh != NULL)
9598 	{
9599 	  cookie.rels = NULL;
9600 	  count = eh->reloc_count;
9601 	  if (count != 0)
9602 	    cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9603 						     info->keep_memory);
9604 	  cookie.rel = cookie.rels;
9605 	  cookie.relend = cookie.rels;
9606 	  if (cookie.rels != NULL)
9607 	    cookie.relend += count * bed->s->int_rels_per_ext_rel;
9608 
9609 	  if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9610 						 bfd_elf_reloc_symbol_deleted_p,
9611 						 &cookie))
9612 	    ret = TRUE;
9613 
9614 	  if (cookie.rels != NULL
9615 	      && elf_section_data (eh)->relocs != cookie.rels)
9616 	    free (cookie.rels);
9617 	}
9618 
9619       if (bed->elf_backend_discard_info != NULL
9620 	  && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9621 	ret = TRUE;
9622 
9623       if (cookie.locsyms != NULL
9624 	  && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9625 	{
9626 	  if (! info->keep_memory)
9627 	    free (cookie.locsyms);
9628 	  else
9629 	    symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9630 	}
9631     }
9632 
9633   if (info->eh_frame_hdr
9634       && !info->relocatable
9635       && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9636     ret = TRUE;
9637 
9638   return ret;
9639 }
9640 
9641 void
_bfd_elf_section_already_linked(bfd * abfd,struct bfd_section * sec)9642 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9643 {
9644   flagword flags;
9645   const char *name, *p;
9646   struct bfd_section_already_linked *l;
9647   struct bfd_section_already_linked_hash_entry *already_linked_list;
9648   asection *group;
9649 
9650   /* A single member comdat group section may be discarded by a
9651      linkonce section. See below.  */
9652   if (sec->output_section == bfd_abs_section_ptr)
9653     return;
9654 
9655   flags = sec->flags;
9656 
9657   /* Check if it belongs to a section group.  */
9658   group = elf_sec_group (sec);
9659 
9660   /* Return if it isn't a linkonce section nor a member of a group.  A
9661      comdat group section also has SEC_LINK_ONCE set.  */
9662   if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9663     return;
9664 
9665   if (group)
9666     {
9667       /* If this is the member of a single member comdat group, check if
9668 	 the group should be discarded.  */
9669       if (elf_next_in_group (sec) == sec
9670 	  && (group->flags & SEC_LINK_ONCE) != 0)
9671 	sec = group;
9672       else
9673 	return;
9674     }
9675 
9676   /* FIXME: When doing a relocatable link, we may have trouble
9677      copying relocations in other sections that refer to local symbols
9678      in the section being discarded.  Those relocations will have to
9679      be converted somehow; as of this writing I'm not sure that any of
9680      the backends handle that correctly.
9681 
9682      It is tempting to instead not discard link once sections when
9683      doing a relocatable link (technically, they should be discarded
9684      whenever we are building constructors).  However, that fails,
9685      because the linker winds up combining all the link once sections
9686      into a single large link once section, which defeats the purpose
9687      of having link once sections in the first place.
9688 
9689      Also, not merging link once sections in a relocatable link
9690      causes trouble for MIPS ELF, which relies on link once semantics
9691      to handle the .reginfo section correctly.  */
9692 
9693   name = bfd_get_section_name (abfd, sec);
9694 
9695   if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9696       && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
9697     p++;
9698   else
9699     p = name;
9700 
9701   already_linked_list = bfd_section_already_linked_table_lookup (p);
9702 
9703   for (l = already_linked_list->entry; l != NULL; l = l->next)
9704     {
9705       /* We may have 3 different sections on the list: group section,
9706 	 comdat section and linkonce section. SEC may be a linkonce or
9707 	 group section. We match a group section with a group section,
9708 	 a linkonce section with a linkonce section, and ignore comdat
9709 	 section.  */
9710       if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9711 	  && strcmp (name, l->sec->name) == 0
9712 	  && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9713 	{
9714 	  /* The section has already been linked.  See if we should
9715 	     issue a warning.  */
9716 	  switch (flags & SEC_LINK_DUPLICATES)
9717 	    {
9718 	    default:
9719 	      abort ();
9720 
9721 	    case SEC_LINK_DUPLICATES_DISCARD:
9722 	      break;
9723 
9724 	    case SEC_LINK_DUPLICATES_ONE_ONLY:
9725 	      (*_bfd_error_handler)
9726 		(_("%B: ignoring duplicate section `%A'"),
9727 		 abfd, sec);
9728 	      break;
9729 
9730 	    case SEC_LINK_DUPLICATES_SAME_SIZE:
9731 	      if (sec->size != l->sec->size)
9732 		(*_bfd_error_handler)
9733 		  (_("%B: duplicate section `%A' has different size"),
9734 		   abfd, sec);
9735 	      break;
9736 
9737 	    case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9738 	      if (sec->size != l->sec->size)
9739 		(*_bfd_error_handler)
9740 		  (_("%B: duplicate section `%A' has different size"),
9741 		   abfd, sec);
9742 	      else if (sec->size != 0)
9743 		{
9744 		  bfd_byte *sec_contents, *l_sec_contents;
9745 
9746 		  if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9747 		    (*_bfd_error_handler)
9748 		      (_("%B: warning: could not read contents of section `%A'"),
9749 		       abfd, sec);
9750 		  else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9751 							&l_sec_contents))
9752 		    (*_bfd_error_handler)
9753 		      (_("%B: warning: could not read contents of section `%A'"),
9754 		       l->sec->owner, l->sec);
9755 		  else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9756 		    (*_bfd_error_handler)
9757 		      (_("%B: warning: duplicate section `%A' has different contents"),
9758 		       abfd, sec);
9759 
9760 		  if (sec_contents)
9761 		    free (sec_contents);
9762 		  if (l_sec_contents)
9763 		    free (l_sec_contents);
9764 		}
9765 	      break;
9766 	    }
9767 
9768 	  /* Set the output_section field so that lang_add_section
9769 	     does not create a lang_input_section structure for this
9770 	     section.  Since there might be a symbol in the section
9771 	     being discarded, we must retain a pointer to the section
9772 	     which we are really going to use.  */
9773 	  sec->output_section = bfd_abs_section_ptr;
9774 	  sec->kept_section = l->sec;
9775 
9776 	  if (flags & SEC_GROUP)
9777 	    {
9778 	      asection *first = elf_next_in_group (sec);
9779 	      asection *s = first;
9780 
9781 	      while (s != NULL)
9782 		{
9783 		  s->output_section = bfd_abs_section_ptr;
9784 		  /* Record which group discards it.  */
9785 		  s->kept_section = l->sec;
9786 		  s = elf_next_in_group (s);
9787 		  /* These lists are circular.  */
9788 		  if (s == first)
9789 		    break;
9790 		}
9791 	    }
9792 
9793 	  return;
9794 	}
9795     }
9796 
9797   if (group)
9798     {
9799       /* If this is the member of a single member comdat group and the
9800 	 group hasn't be discarded, we check if it matches a linkonce
9801 	 section. We only record the discarded comdat group. Otherwise
9802 	 the undiscarded group will be discarded incorrectly later since
9803 	 itself has been recorded.  */
9804       for (l = already_linked_list->entry; l != NULL; l = l->next)
9805 	if ((l->sec->flags & SEC_GROUP) == 0
9806 	    && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9807 	    && bfd_elf_match_symbols_in_sections (l->sec,
9808 						  elf_next_in_group (sec)))
9809 	  {
9810 	    elf_next_in_group (sec)->output_section = bfd_abs_section_ptr;
9811 	    elf_next_in_group (sec)->kept_section = l->sec;
9812 	    group->output_section = bfd_abs_section_ptr;
9813 	    break;
9814 	  }
9815       if (l == NULL)
9816 	return;
9817     }
9818   else
9819     /* There is no direct match. But for linkonce section, we should
9820        check if there is a match with comdat group member. We always
9821        record the linkonce section, discarded or not.  */
9822     for (l = already_linked_list->entry; l != NULL; l = l->next)
9823       if (l->sec->flags & SEC_GROUP)
9824 	{
9825 	  asection *first = elf_next_in_group (l->sec);
9826 
9827 	  if (first != NULL
9828 	      && elf_next_in_group (first) == first
9829 	      && bfd_elf_match_symbols_in_sections (first, sec))
9830 	    {
9831 	      sec->output_section = bfd_abs_section_ptr;
9832 	      sec->kept_section = l->sec;
9833 	      break;
9834 	    }
9835 	}
9836 
9837   /* This is the first section with this name.  Record it.  */
9838   bfd_section_already_linked_table_insert (already_linked_list, sec);
9839 }
9840 
9841 static void
bfd_elf_set_symbol(struct elf_link_hash_entry * h,bfd_vma val,struct bfd_section * s)9842 bfd_elf_set_symbol (struct elf_link_hash_entry *h, bfd_vma val,
9843 		    struct bfd_section *s)
9844 {
9845   h->root.type = bfd_link_hash_defined;
9846   h->root.u.def.section = s ? s : bfd_abs_section_ptr;
9847   h->root.u.def.value = val;
9848   h->def_regular = 1;
9849   h->type = STT_OBJECT;
9850   h->other = STV_HIDDEN | (h->other & ~ ELF_ST_VISIBILITY (-1));
9851   h->forced_local = 1;
9852 }
9853 
9854 /* Set NAME to VAL if the symbol exists and is not defined in a regular
9855    object file.  If S is NULL it is an absolute symbol, otherwise it is
9856    relative to that section.  */
9857 
9858 void
_bfd_elf_provide_symbol(struct bfd_link_info * info,const char * name,bfd_vma val,struct bfd_section * s)9859 _bfd_elf_provide_symbol (struct bfd_link_info *info, const char *name,
9860 			 bfd_vma val, struct bfd_section *s)
9861 {
9862   struct elf_link_hash_entry *h;
9863 
9864   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE,
9865 			    FALSE);
9866   if (h != NULL && !h->def_regular)
9867     bfd_elf_set_symbol (h, val, s);
9868 }
9869 
9870 /* Set START and END to boundaries of SEC if they exist and are not
9871    defined in regular object files.  */
9872 
9873 void
_bfd_elf_provide_section_bound_symbols(struct bfd_link_info * info,asection * sec,const char * start,const char * end)9874 _bfd_elf_provide_section_bound_symbols (struct bfd_link_info *info,
9875 					asection *sec,
9876 					const char *start,
9877 					const char *end)
9878 {
9879   struct elf_link_hash_entry *hs, *he;
9880   bfd_vma start_val, end_val;
9881   bfd_boolean do_start, do_end;
9882 
9883   /* Check if we need them or not first.  */
9884   hs = elf_link_hash_lookup (elf_hash_table (info), start, FALSE,
9885 			     FALSE, FALSE);
9886   do_start = hs != NULL && !hs->def_regular;
9887 
9888   he = elf_link_hash_lookup (elf_hash_table (info), end, FALSE,
9889 			     FALSE, FALSE);
9890   do_end = he != NULL && !he->def_regular;
9891 
9892   if (!do_start && !do_end)
9893     return;
9894 
9895   if (sec != NULL)
9896     {
9897       start_val = sec->vma;
9898       end_val = start_val + sec->size;
9899     }
9900   else
9901     {
9902       /* We have to choose those values very carefully.  Some targets,
9903 	 like alpha, may have relocation overflow with 0. "__bss_start"
9904 	 should be defined in all cases.  */
9905       struct elf_link_hash_entry *h
9906 	= elf_link_hash_lookup (elf_hash_table (info), "__bss_start",
9907 				FALSE, FALSE, FALSE);
9908       if (h != NULL && h->root.type == bfd_link_hash_defined)
9909 	start_val = h->root.u.def.value;
9910       else
9911 	start_val = 0;
9912       end_val = start_val;
9913     }
9914 
9915   if (do_start)
9916     bfd_elf_set_symbol (hs, start_val, NULL);
9917 
9918   if (do_end)
9919     bfd_elf_set_symbol (he, end_val, NULL);
9920 }
9921