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