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