]> Git Repo - binutils.git/blob - bfd/elflink.c
* elflink.c (_bfd_elf_merge_symbol): Rewrite weak symbol handling.
[binutils.git] / bfd / elflink.c
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
28 bfd_boolean
29 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
30 {
31   flagword flags;
32   asection *s;
33   struct elf_link_hash_entry *h;
34   struct bfd_link_hash_entry *bh;
35   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
36   int ptralign;
37
38   /* This function may be called more than once.  */
39   s = bfd_get_section_by_name (abfd, ".got");
40   if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
41     return TRUE;
42
43   switch (bed->s->arch_size)
44     {
45     case 32:
46       ptralign = 2;
47       break;
48
49     case 64:
50       ptralign = 3;
51       break;
52
53     default:
54       bfd_set_error (bfd_error_bad_value);
55       return FALSE;
56     }
57
58   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
59            | SEC_LINKER_CREATED);
60
61   s = bfd_make_section (abfd, ".got");
62   if (s == NULL
63       || !bfd_set_section_flags (abfd, s, flags)
64       || !bfd_set_section_alignment (abfd, s, ptralign))
65     return FALSE;
66
67   if (bed->want_got_plt)
68     {
69       s = bfd_make_section (abfd, ".got.plt");
70       if (s == NULL
71           || !bfd_set_section_flags (abfd, s, flags)
72           || !bfd_set_section_alignment (abfd, s, ptralign))
73         return FALSE;
74     }
75
76   if (bed->want_got_sym)
77     {
78       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
79          (or .got.plt) section.  We don't do this in the linker script
80          because we don't want to define the symbol if we are not creating
81          a global offset table.  */
82       bh = NULL;
83       if (!(_bfd_generic_link_add_one_symbol
84             (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
85              bed->got_symbol_offset, NULL, FALSE, bed->collect, &bh)))
86         return FALSE;
87       h = (struct elf_link_hash_entry *) bh;
88       h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
89       h->type = STT_OBJECT;
90
91       if (! info->executable
92           && ! _bfd_elf_link_record_dynamic_symbol (info, h))
93         return FALSE;
94
95       elf_hash_table (info)->hgot = h;
96     }
97
98   /* The first bit of the global offset table is the header.  */
99   s->_raw_size += bed->got_header_size + bed->got_symbol_offset;
100
101   return TRUE;
102 }
103 \f
104 /* Create some sections which will be filled in with dynamic linking
105    information.  ABFD is an input file which requires dynamic sections
106    to be created.  The dynamic sections take up virtual memory space
107    when the final executable is run, so we need to create them before
108    addresses are assigned to the output sections.  We work out the
109    actual contents and size of these sections later.  */
110
111 bfd_boolean
112 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
113 {
114   flagword flags;
115   register asection *s;
116   struct elf_link_hash_entry *h;
117   struct bfd_link_hash_entry *bh;
118   const struct elf_backend_data *bed;
119
120   if (! is_elf_hash_table (info->hash))
121     return FALSE;
122
123   if (elf_hash_table (info)->dynamic_sections_created)
124     return TRUE;
125
126   /* Make sure that all dynamic sections use the same input BFD.  */
127   if (elf_hash_table (info)->dynobj == NULL)
128     elf_hash_table (info)->dynobj = abfd;
129   else
130     abfd = elf_hash_table (info)->dynobj;
131
132   /* Note that we set the SEC_IN_MEMORY flag for all of these
133      sections.  */
134   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
135            | SEC_IN_MEMORY | SEC_LINKER_CREATED);
136
137   /* A dynamically linked executable has a .interp section, but a
138      shared library does not.  */
139   if (info->executable)
140     {
141       s = bfd_make_section (abfd, ".interp");
142       if (s == NULL
143           || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
144         return FALSE;
145     }
146
147   if (! info->traditional_format)
148     {
149       s = bfd_make_section (abfd, ".eh_frame_hdr");
150       if (s == NULL
151           || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
152           || ! bfd_set_section_alignment (abfd, s, 2))
153         return FALSE;
154       elf_hash_table (info)->eh_info.hdr_sec = s;
155     }
156
157   bed = get_elf_backend_data (abfd);
158
159   /* Create sections to hold version informations.  These are removed
160      if they are not needed.  */
161   s = bfd_make_section (abfd, ".gnu.version_d");
162   if (s == NULL
163       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
164       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
165     return FALSE;
166
167   s = bfd_make_section (abfd, ".gnu.version");
168   if (s == NULL
169       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
170       || ! bfd_set_section_alignment (abfd, s, 1))
171     return FALSE;
172
173   s = bfd_make_section (abfd, ".gnu.version_r");
174   if (s == NULL
175       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
176       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
177     return FALSE;
178
179   s = bfd_make_section (abfd, ".dynsym");
180   if (s == NULL
181       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
182       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
183     return FALSE;
184
185   s = bfd_make_section (abfd, ".dynstr");
186   if (s == NULL
187       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
188     return FALSE;
189
190   /* Create a strtab to hold the dynamic symbol names.  */
191   if (elf_hash_table (info)->dynstr == NULL)
192     {
193       elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
194       if (elf_hash_table (info)->dynstr == NULL)
195         return FALSE;
196     }
197
198   s = bfd_make_section (abfd, ".dynamic");
199   if (s == NULL
200       || ! bfd_set_section_flags (abfd, s, flags)
201       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
202     return FALSE;
203
204   /* The special symbol _DYNAMIC is always set to the start of the
205      .dynamic section.  This call occurs before we have processed the
206      symbols for any dynamic object, so we don't have to worry about
207      overriding a dynamic definition.  We could set _DYNAMIC in a
208      linker script, but we only want to define it if we are, in fact,
209      creating a .dynamic section.  We don't want to define it if there
210      is no .dynamic section, since on some ELF platforms the start up
211      code examines it to decide how to initialize the process.  */
212   bh = NULL;
213   if (! (_bfd_generic_link_add_one_symbol
214          (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE,
215           get_elf_backend_data (abfd)->collect, &bh)))
216     return FALSE;
217   h = (struct elf_link_hash_entry *) bh;
218   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
219   h->type = STT_OBJECT;
220
221   if (! info->executable
222       && ! _bfd_elf_link_record_dynamic_symbol (info, h))
223     return FALSE;
224
225   s = bfd_make_section (abfd, ".hash");
226   if (s == NULL
227       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
228       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
229     return FALSE;
230   elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
231
232   /* Let the backend create the rest of the sections.  This lets the
233      backend set the right flags.  The backend will normally create
234      the .got and .plt sections.  */
235   if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
236     return FALSE;
237
238   elf_hash_table (info)->dynamic_sections_created = TRUE;
239
240   return TRUE;
241 }
242
243 /* Create dynamic sections when linking against a dynamic object.  */
244
245 bfd_boolean
246 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
247 {
248   flagword flags, pltflags;
249   asection *s;
250   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
251
252   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
253      .rel[a].bss sections.  */
254
255   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
256            | SEC_LINKER_CREATED);
257
258   pltflags = flags;
259   pltflags |= SEC_CODE;
260   if (bed->plt_not_loaded)
261     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
262   if (bed->plt_readonly)
263     pltflags |= SEC_READONLY;
264
265   s = bfd_make_section (abfd, ".plt");
266   if (s == NULL
267       || ! bfd_set_section_flags (abfd, s, pltflags)
268       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
269     return FALSE;
270
271   if (bed->want_plt_sym)
272     {
273       /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
274          .plt section.  */
275       struct elf_link_hash_entry *h;
276       struct bfd_link_hash_entry *bh = NULL;
277
278       if (! (_bfd_generic_link_add_one_symbol
279              (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
280               FALSE, get_elf_backend_data (abfd)->collect, &bh)))
281         return FALSE;
282       h = (struct elf_link_hash_entry *) bh;
283       h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
284       h->type = STT_OBJECT;
285
286       if (! info->executable
287           && ! _bfd_elf_link_record_dynamic_symbol (info, h))
288         return FALSE;
289     }
290
291   s = bfd_make_section (abfd,
292                         bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
293   if (s == NULL
294       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
295       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
296     return FALSE;
297
298   if (! _bfd_elf_create_got_section (abfd, info))
299     return FALSE;
300
301   if (bed->want_dynbss)
302     {
303       /* The .dynbss section is a place to put symbols which are defined
304          by dynamic objects, are referenced by regular objects, and are
305          not functions.  We must allocate space for them in the process
306          image and use a R_*_COPY reloc to tell the dynamic linker to
307          initialize them at run time.  The linker script puts the .dynbss
308          section into the .bss section of the final image.  */
309       s = bfd_make_section (abfd, ".dynbss");
310       if (s == NULL
311           || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
312         return FALSE;
313
314       /* The .rel[a].bss section holds copy relocs.  This section is not
315      normally needed.  We need to create it here, though, so that the
316      linker will map it to an output section.  We can't just create it
317      only if we need it, because we will not know whether we need it
318      until we have seen all the input files, and the first time the
319      main linker code calls BFD after examining all the input files
320      (size_dynamic_sections) the input sections have already been
321      mapped to the output sections.  If the section turns out not to
322      be needed, we can discard it later.  We will never need this
323      section when generating a shared object, since they do not use
324      copy relocs.  */
325       if (! info->shared)
326         {
327           s = bfd_make_section (abfd,
328                                 (bed->default_use_rela_p
329                                  ? ".rela.bss" : ".rel.bss"));
330           if (s == NULL
331               || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
332               || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
333             return FALSE;
334         }
335     }
336
337   return TRUE;
338 }
339 \f
340 /* Record a new dynamic symbol.  We record the dynamic symbols as we
341    read the input files, since we need to have a list of all of them
342    before we can determine the final sizes of the output sections.
343    Note that we may actually call this function even though we are not
344    going to output any dynamic symbols; in some cases we know that a
345    symbol should be in the dynamic symbol table, but only if there is
346    one.  */
347
348 bfd_boolean
349 _bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
350                                      struct elf_link_hash_entry *h)
351 {
352   if (h->dynindx == -1)
353     {
354       struct elf_strtab_hash *dynstr;
355       char *p;
356       const char *name;
357       bfd_size_type indx;
358
359       /* XXX: The ABI draft says the linker must turn hidden and
360          internal symbols into STB_LOCAL symbols when producing the
361          DSO. However, if ld.so honors st_other in the dynamic table,
362          this would not be necessary.  */
363       switch (ELF_ST_VISIBILITY (h->other))
364         {
365         case STV_INTERNAL:
366         case STV_HIDDEN:
367           if (h->root.type != bfd_link_hash_undefined
368               && h->root.type != bfd_link_hash_undefweak)
369             {
370               h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
371               return TRUE;
372             }
373
374         default:
375           break;
376         }
377
378       h->dynindx = elf_hash_table (info)->dynsymcount;
379       ++elf_hash_table (info)->dynsymcount;
380
381       dynstr = elf_hash_table (info)->dynstr;
382       if (dynstr == NULL)
383         {
384           /* Create a strtab to hold the dynamic symbol names.  */
385           elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
386           if (dynstr == NULL)
387             return FALSE;
388         }
389
390       /* We don't put any version information in the dynamic string
391          table.  */
392       name = h->root.root.string;
393       p = strchr (name, ELF_VER_CHR);
394       if (p != NULL)
395         /* We know that the p points into writable memory.  In fact,
396            there are only a few symbols that have read-only names, being
397            those like _GLOBAL_OFFSET_TABLE_ that are created specially
398            by the backends.  Most symbols will have names pointing into
399            an ELF string table read from a file, or to objalloc memory.  */
400         *p = 0;
401
402       indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
403
404       if (p != NULL)
405         *p = ELF_VER_CHR;
406
407       if (indx == (bfd_size_type) -1)
408         return FALSE;
409       h->dynstr_index = indx;
410     }
411
412   return TRUE;
413 }
414 \f
415 /* Record an assignment to a symbol made by a linker script.  We need
416    this in case some dynamic object refers to this symbol.  */
417
418 bfd_boolean
419 bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED,
420                                 struct bfd_link_info *info,
421                                 const char *name,
422                                 bfd_boolean provide)
423 {
424   struct elf_link_hash_entry *h;
425
426   if (!is_elf_hash_table (info->hash))
427     return TRUE;
428
429   h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE);
430   if (h == NULL)
431     return FALSE;
432
433   /* Since we're defining the symbol, don't let it seem to have not
434      been defined.  record_dynamic_symbol and size_dynamic_sections
435      may depend on this.  */
436   if (h->root.type == bfd_link_hash_undefweak
437       || h->root.type == bfd_link_hash_undefined)
438     h->root.type = bfd_link_hash_new;
439
440   if (h->root.type == bfd_link_hash_new)
441     h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
442
443   /* If this symbol is being provided by the linker script, and it is
444      currently defined by a dynamic object, but not by a regular
445      object, then mark it as undefined so that the generic linker will
446      force the correct value.  */
447   if (provide
448       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
449       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
450     h->root.type = bfd_link_hash_undefined;
451
452   /* If this symbol is not being provided by the linker script, and it is
453      currently defined by a dynamic object, but not by a regular object,
454      then clear out any version information because the symbol will not be
455      associated with the dynamic object any more.  */
456   if (!provide
457       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
458       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
459     h->verinfo.verdef = NULL;
460
461   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
462
463   if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
464                                   | ELF_LINK_HASH_REF_DYNAMIC)) != 0
465        || info->shared)
466       && h->dynindx == -1)
467     {
468       if (! _bfd_elf_link_record_dynamic_symbol (info, h))
469         return FALSE;
470
471       /* If this is a weak defined symbol, and we know a corresponding
472          real symbol from the same dynamic object, make sure the real
473          symbol is also made into a dynamic symbol.  */
474       if (h->weakdef != NULL
475           && h->weakdef->dynindx == -1)
476         {
477           if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
478             return FALSE;
479         }
480     }
481
482   return TRUE;
483 }
484
485 /* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
486    success, and 2 on a failure caused by attempting to record a symbol
487    in a discarded section, eg. a discarded link-once section symbol.  */
488
489 int
490 elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
491                                       bfd *input_bfd,
492                                       long input_indx)
493 {
494   bfd_size_type amt;
495   struct elf_link_local_dynamic_entry *entry;
496   struct elf_link_hash_table *eht;
497   struct elf_strtab_hash *dynstr;
498   unsigned long dynstr_index;
499   char *name;
500   Elf_External_Sym_Shndx eshndx;
501   char esym[sizeof (Elf64_External_Sym)];
502
503   if (! is_elf_hash_table (info->hash))
504     return 0;
505
506   /* See if the entry exists already.  */
507   for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
508     if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
509       return 1;
510
511   amt = sizeof (*entry);
512   entry = bfd_alloc (input_bfd, amt);
513   if (entry == NULL)
514     return 0;
515
516   /* Go find the symbol, so that we can find it's name.  */
517   if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
518                              1, input_indx, &entry->isym, esym, &eshndx))
519     {
520       bfd_release (input_bfd, entry);
521       return 0;
522     }
523
524   if (entry->isym.st_shndx != SHN_UNDEF
525       && (entry->isym.st_shndx < SHN_LORESERVE
526           || entry->isym.st_shndx > SHN_HIRESERVE))
527     {
528       asection *s;
529
530       s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
531       if (s == NULL || bfd_is_abs_section (s->output_section))
532         {
533           /* We can still bfd_release here as nothing has done another
534              bfd_alloc.  We can't do this later in this function.  */
535           bfd_release (input_bfd, entry);
536           return 2;
537         }
538     }
539
540   name = (bfd_elf_string_from_elf_section
541           (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
542            entry->isym.st_name));
543
544   dynstr = elf_hash_table (info)->dynstr;
545   if (dynstr == NULL)
546     {
547       /* Create a strtab to hold the dynamic symbol names.  */
548       elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
549       if (dynstr == NULL)
550         return 0;
551     }
552
553   dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
554   if (dynstr_index == (unsigned long) -1)
555     return 0;
556   entry->isym.st_name = dynstr_index;
557
558   eht = elf_hash_table (info);
559
560   entry->next = eht->dynlocal;
561   eht->dynlocal = entry;
562   entry->input_bfd = input_bfd;
563   entry->input_indx = input_indx;
564   eht->dynsymcount++;
565
566   /* Whatever binding the symbol had before, it's now local.  */
567   entry->isym.st_info
568     = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
569
570   /* The dynindx will be set at the end of size_dynamic_sections.  */
571
572   return 1;
573 }
574
575 /* Return the dynindex of a local dynamic symbol.  */
576
577 long
578 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
579                                     bfd *input_bfd,
580                                     long input_indx)
581 {
582   struct elf_link_local_dynamic_entry *e;
583
584   for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
585     if (e->input_bfd == input_bfd && e->input_indx == input_indx)
586       return e->dynindx;
587   return -1;
588 }
589
590 /* This function is used to renumber the dynamic symbols, if some of
591    them are removed because they are marked as local.  This is called
592    via elf_link_hash_traverse.  */
593
594 static bfd_boolean
595 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
596                                       void *data)
597 {
598   size_t *count = data;
599
600   if (h->root.type == bfd_link_hash_warning)
601     h = (struct elf_link_hash_entry *) h->root.u.i.link;
602
603   if (h->dynindx != -1)
604     h->dynindx = ++(*count);
605
606   return TRUE;
607 }
608
609 /* Assign dynsym indices.  In a shared library we generate a section
610    symbol for each output section, which come first.  Next come all of
611    the back-end allocated local dynamic syms, followed by the rest of
612    the global symbols.  */
613
614 unsigned long
615 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
616 {
617   unsigned long dynsymcount = 0;
618
619   if (info->shared)
620     {
621       asection *p;
622       for (p = output_bfd->sections; p ; p = p->next)
623         if ((p->flags & SEC_EXCLUDE) == 0)
624           elf_section_data (p)->dynindx = ++dynsymcount;
625     }
626
627   if (elf_hash_table (info)->dynlocal)
628     {
629       struct elf_link_local_dynamic_entry *p;
630       for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
631         p->dynindx = ++dynsymcount;
632     }
633
634   elf_link_hash_traverse (elf_hash_table (info),
635                           elf_link_renumber_hash_table_dynsyms,
636                           &dynsymcount);
637
638   /* There is an unused NULL entry at the head of the table which
639      we must account for in our count.  Unless there weren't any
640      symbols, which means we'll have no table at all.  */
641   if (dynsymcount != 0)
642     ++dynsymcount;
643
644   return elf_hash_table (info)->dynsymcount = dynsymcount;
645 }
646
647 /* This function is called when we want to define a new symbol.  It
648    handles the various cases which arise when we find a definition in
649    a dynamic object, or when there is already a definition in a
650    dynamic object.  The new symbol is described by NAME, SYM, PSEC,
651    and PVALUE.  We set SYM_HASH to the hash table entry.  We set
652    OVERRIDE if the old symbol is overriding a new definition.  We set
653    TYPE_CHANGE_OK if it is OK for the type to change.  We set
654    SIZE_CHANGE_OK if it is OK for the size to change.  By OK to
655    change, we mean that we shouldn't warn if the type or size does
656    change.  DT_NEEDED indicates if it comes from a DT_NEEDED entry of
657    a shared object.  */
658
659 bfd_boolean
660 _bfd_elf_merge_symbol (bfd *abfd,
661                        struct bfd_link_info *info,
662                        const char *name,
663                        Elf_Internal_Sym *sym,
664                        asection **psec,
665                        bfd_vma *pvalue,
666                        struct elf_link_hash_entry **sym_hash,
667                        bfd_boolean *skip,
668                        bfd_boolean *override,
669                        bfd_boolean *type_change_ok,
670                        bfd_boolean *size_change_ok,
671                        bfd_boolean dt_needed)
672 {
673   asection *sec;
674   struct elf_link_hash_entry *h;
675   struct elf_link_hash_entry *flip;
676   int bind;
677   bfd *oldbfd;
678   bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
679   bfd_boolean newweak, oldweak;
680
681   *skip = FALSE;
682   *override = FALSE;
683
684   sec = *psec;
685   bind = ELF_ST_BIND (sym->st_info);
686
687   if (! bfd_is_und_section (sec))
688     h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
689   else
690     h = ((struct elf_link_hash_entry *)
691          bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
692   if (h == NULL)
693     return FALSE;
694   *sym_hash = h;
695
696   /* This code is for coping with dynamic objects, and is only useful
697      if we are doing an ELF link.  */
698   if (info->hash->creator != abfd->xvec)
699     return TRUE;
700
701   /* For merging, we only care about real symbols.  */
702
703   while (h->root.type == bfd_link_hash_indirect
704          || h->root.type == bfd_link_hash_warning)
705     h = (struct elf_link_hash_entry *) h->root.u.i.link;
706
707   /* If we just created the symbol, mark it as being an ELF symbol.
708      Other than that, there is nothing to do--there is no merge issue
709      with a newly defined symbol--so we just return.  */
710
711   if (h->root.type == bfd_link_hash_new)
712     {
713       h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
714       return TRUE;
715     }
716
717   /* OLDBFD is a BFD associated with the existing symbol.  */
718
719   switch (h->root.type)
720     {
721     default:
722       oldbfd = NULL;
723       break;
724
725     case bfd_link_hash_undefined:
726     case bfd_link_hash_undefweak:
727       oldbfd = h->root.u.undef.abfd;
728       break;
729
730     case bfd_link_hash_defined:
731     case bfd_link_hash_defweak:
732       oldbfd = h->root.u.def.section->owner;
733       break;
734
735     case bfd_link_hash_common:
736       oldbfd = h->root.u.c.p->section->owner;
737       break;
738     }
739
740   /* In cases involving weak versioned symbols, we may wind up trying
741      to merge a symbol with itself.  Catch that here, to avoid the
742      confusion that results if we try to override a symbol with
743      itself.  The additional tests catch cases like
744      _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
745      dynamic object, which we do want to handle here.  */
746   if (abfd == oldbfd
747       && ((abfd->flags & DYNAMIC) == 0
748           || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
749     return TRUE;
750
751   /* NEWDYN and OLDDYN indicate whether the new or old symbol,
752      respectively, is from a dynamic object.  */
753
754   if ((abfd->flags & DYNAMIC) != 0)
755     newdyn = TRUE;
756   else
757     newdyn = FALSE;
758
759   if (oldbfd != NULL)
760     olddyn = (oldbfd->flags & DYNAMIC) != 0;
761   else
762     {
763       asection *hsec;
764
765       /* This code handles the special SHN_MIPS_{TEXT,DATA} section
766          indices used by MIPS ELF.  */
767       switch (h->root.type)
768         {
769         default:
770           hsec = NULL;
771           break;
772
773         case bfd_link_hash_defined:
774         case bfd_link_hash_defweak:
775           hsec = h->root.u.def.section;
776           break;
777
778         case bfd_link_hash_common:
779           hsec = h->root.u.c.p->section;
780           break;
781         }
782
783       if (hsec == NULL)
784         olddyn = FALSE;
785       else
786         olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
787     }
788
789   /* NEWDEF and OLDDEF indicate whether the new or old symbol,
790      respectively, appear to be a definition rather than reference.  */
791
792   if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
793     newdef = FALSE;
794   else
795     newdef = TRUE;
796
797   if (h->root.type == bfd_link_hash_undefined
798       || h->root.type == bfd_link_hash_undefweak
799       || h->root.type == bfd_link_hash_common)
800     olddef = FALSE;
801   else
802     olddef = TRUE;
803
804   /* We need to remember if a symbol has a definition in a dynamic
805      object or is weak in all dynamic objects. Internal and hidden
806      visibility will make it unavailable to dynamic objects.  */
807   if (newdyn && (h->elf_link_hash_flags & ELF_LINK_DYNAMIC_DEF) == 0)
808     {
809       if (!bfd_is_und_section (sec))
810         h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_DEF;
811       else
812         {
813           /* Check if this symbol is weak in all dynamic objects. If it
814              is the first time we see it in a dynamic object, we mark
815              if it is weak. Otherwise, we clear it.  */
816           if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
817             {
818               if (bind == STB_WEAK)
819                 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_WEAK;
820             }
821           else if (bind != STB_WEAK)
822             h->elf_link_hash_flags &= ~ELF_LINK_DYNAMIC_WEAK;
823         }
824     }
825
826   /* If the old symbol has non-default visibility, we ignore the new
827      definition from a dynamic object.  */
828   if (newdyn
829       && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
830       && !bfd_is_und_section (sec))
831     {
832       *skip = TRUE;
833       /* Make sure this symbol is dynamic.  */
834       h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
835       /* A protected symbol has external availability. Make sure it is
836          recorded as dynamic.
837
838          FIXME: Should we check type and size for protected symbol?  */
839       if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
840         return _bfd_elf_link_record_dynamic_symbol (info, h);
841       else
842         return TRUE;
843     }
844   else if (!newdyn
845            && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
846            && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
847     {
848       /* If the new symbol with non-default visibility comes from a
849          relocatable file and the old definition comes from a dynamic
850          object, we remove the old definition.  */
851       if ((*sym_hash)->root.type == bfd_link_hash_indirect)
852         h = *sym_hash;
853
854       if ((h->root.und_next || info->hash->undefs_tail == &h->root)
855           && bfd_is_und_section (sec))
856         {
857           /* If the new symbol is undefined and the old symbol was
858              also undefined before, we need to make sure
859              _bfd_generic_link_add_one_symbol doesn't mess
860              up the linker hash table undefs list. Since the old
861              definition came from a dynamic object, it is still on the
862              undefs list.  */
863           h->root.type = bfd_link_hash_undefined;
864           /* FIXME: What if the new symbol is weak undefined?  */
865           h->root.u.undef.abfd = abfd;
866         }
867       else
868         {
869           h->root.type = bfd_link_hash_new;
870           h->root.u.undef.abfd = NULL;
871         }
872
873       if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
874         {
875           h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
876           h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_DYNAMIC
877                                      | ELF_LINK_DYNAMIC_DEF);
878         }
879       /* FIXME: Should we check type and size for protected symbol?  */
880       h->size = 0;
881       h->type = 0;
882       return TRUE;
883     }
884
885   /* Differentiate strong and weak symbols.  */
886   newweak = bind == STB_WEAK;
887   oldweak = (h->root.type == bfd_link_hash_defweak
888              || h->root.type == bfd_link_hash_undefweak);
889
890   /* It's OK to change the type if either the existing symbol or the
891      new symbol is weak.  A type change is also OK if the old symbol
892      is undefined and the new symbol is defined.  */
893
894   if (oldweak
895       || newweak
896       || (newdef
897           && h->root.type == bfd_link_hash_undefined))
898     *type_change_ok = TRUE;
899
900   /* It's OK to change the size if either the existing symbol or the
901      new symbol is weak, or if the old symbol is undefined.  */
902
903   if (*type_change_ok
904       || h->root.type == bfd_link_hash_undefined)
905     *size_change_ok = TRUE;
906
907   /* If a new weak symbol comes from a regular file and the old symbol
908      comes from a dynamic library, we treat the new one as strong.
909      Similarly, an old weak symbol from a regular file is treated as
910      strong when the new symbol comes from a dynamic library.  Further,
911      an old weak symbol from a dynamic library is treated as strong if
912      the new symbol is from a DT_NEEDED dynamic library.  */
913   if (!newdyn && olddyn)
914     newweak = FALSE;
915   if ((!olddyn || dt_needed) && newdyn)
916     oldweak = FALSE;
917
918   /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
919      symbol, respectively, appears to be a common symbol in a dynamic
920      object.  If a symbol appears in an uninitialized section, and is
921      not weak, and is not a function, then it may be a common symbol
922      which was resolved when the dynamic object was created.  We want
923      to treat such symbols specially, because they raise special
924      considerations when setting the symbol size: if the symbol
925      appears as a common symbol in a regular object, and the size in
926      the regular object is larger, we must make sure that we use the
927      larger size.  This problematic case can always be avoided in C,
928      but it must be handled correctly when using Fortran shared
929      libraries.
930
931      Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
932      likewise for OLDDYNCOMMON and OLDDEF.
933
934      Note that this test is just a heuristic, and that it is quite
935      possible to have an uninitialized symbol in a shared object which
936      is really a definition, rather than a common symbol.  This could
937      lead to some minor confusion when the symbol really is a common
938      symbol in some regular object.  However, I think it will be
939      harmless.  */
940
941   if (newdyn
942       && newdef
943       && !newweak
944       && (sec->flags & SEC_ALLOC) != 0
945       && (sec->flags & SEC_LOAD) == 0
946       && sym->st_size > 0
947       && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
948     newdyncommon = TRUE;
949   else
950     newdyncommon = FALSE;
951
952   if (olddyn
953       && olddef
954       && h->root.type == bfd_link_hash_defined
955       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
956       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
957       && (h->root.u.def.section->flags & SEC_LOAD) == 0
958       && h->size > 0
959       && h->type != STT_FUNC)
960     olddyncommon = TRUE;
961   else
962     olddyncommon = FALSE;
963
964   /* If both the old and the new symbols look like common symbols in a
965      dynamic object, set the size of the symbol to the larger of the
966      two.  */
967
968   if (olddyncommon
969       && newdyncommon
970       && sym->st_size != h->size)
971     {
972       /* Since we think we have two common symbols, issue a multiple
973          common warning if desired.  Note that we only warn if the
974          size is different.  If the size is the same, we simply let
975          the old symbol override the new one as normally happens with
976          symbols defined in dynamic objects.  */
977
978       if (! ((*info->callbacks->multiple_common)
979              (info, h->root.root.string, oldbfd, bfd_link_hash_common,
980               h->size, abfd, bfd_link_hash_common, sym->st_size)))
981         return FALSE;
982
983       if (sym->st_size > h->size)
984         h->size = sym->st_size;
985
986       *size_change_ok = TRUE;
987     }
988
989   /* If we are looking at a dynamic object, and we have found a
990      definition, we need to see if the symbol was already defined by
991      some other object.  If so, we want to use the existing
992      definition, and we do not want to report a multiple symbol
993      definition error; we do this by clobbering *PSEC to be
994      bfd_und_section_ptr.
995
996      We treat a common symbol as a definition if the symbol in the
997      shared library is a function, since common symbols always
998      represent variables; this can cause confusion in principle, but
999      any such confusion would seem to indicate an erroneous program or
1000      shared library.  We also permit a common symbol in a regular
1001      object to override a weak symbol in a shared object.  */
1002
1003   if (newdyn
1004       && newdef
1005       && (olddef
1006           || (h->root.type == bfd_link_hash_common
1007               && (newweak
1008                   || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
1009       && (!oldweak || newweak))
1010     {
1011       *override = TRUE;
1012       newdef = FALSE;
1013       newdyncommon = FALSE;
1014
1015       *psec = sec = bfd_und_section_ptr;
1016       *size_change_ok = TRUE;
1017
1018       /* If we get here when the old symbol is a common symbol, then
1019          we are explicitly letting it override a weak symbol or
1020          function in a dynamic object, and we don't want to warn about
1021          a type change.  If the old symbol is a defined symbol, a type
1022          change warning may still be appropriate.  */
1023
1024       if (h->root.type == bfd_link_hash_common)
1025         *type_change_ok = TRUE;
1026     }
1027
1028   /* Handle the special case of an old common symbol merging with a
1029      new symbol which looks like a common symbol in a shared object.
1030      We change *PSEC and *PVALUE to make the new symbol look like a
1031      common symbol, and let _bfd_generic_link_add_one_symbol will do
1032      the right thing.  */
1033
1034   if (newdyncommon
1035       && h->root.type == bfd_link_hash_common)
1036     {
1037       *override = TRUE;
1038       newdef = FALSE;
1039       newdyncommon = FALSE;
1040       *pvalue = sym->st_size;
1041       *psec = sec = bfd_com_section_ptr;
1042       *size_change_ok = TRUE;
1043     }
1044
1045   /* If the old symbol is from a dynamic object, and the new symbol is
1046      a definition which is not from a dynamic object, then the new
1047      symbol overrides the old symbol.  Symbols from regular files
1048      always take precedence over symbols from dynamic objects, even if
1049      they are defined after the dynamic object in the link.
1050
1051      As above, we again permit a common symbol in a regular object to
1052      override a definition in a shared object if the shared object
1053      symbol is a function or is weak.
1054
1055      As above, we permit a non-weak definition in a shared object to
1056      override a weak definition in a regular object.  */
1057
1058   flip = NULL;
1059   if (! newdyn
1060       && (newdef
1061           || (bfd_is_com_section (sec)
1062               && (oldweak
1063                   || h->type == STT_FUNC)))
1064       && olddyn
1065       && olddef
1066       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1067       && (!newweak || oldweak))
1068     {
1069       /* Change the hash table entry to undefined, and let
1070          _bfd_generic_link_add_one_symbol do the right thing with the
1071          new definition.  */
1072
1073       h->root.type = bfd_link_hash_undefined;
1074       h->root.u.undef.abfd = h->root.u.def.section->owner;
1075       *size_change_ok = TRUE;
1076
1077       olddef = FALSE;
1078       olddyncommon = FALSE;
1079
1080       /* We again permit a type change when a common symbol may be
1081          overriding a function.  */
1082
1083       if (bfd_is_com_section (sec))
1084         *type_change_ok = TRUE;
1085
1086       if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1087         flip = *sym_hash;
1088       else
1089         /* This union may have been set to be non-NULL when this symbol
1090            was seen in a dynamic object.  We must force the union to be
1091            NULL, so that it is correct for a regular symbol.  */
1092         h->verinfo.vertree = NULL;
1093     }
1094
1095   /* Handle the special case of a new common symbol merging with an
1096      old symbol that looks like it might be a common symbol defined in
1097      a shared object.  Note that we have already handled the case in
1098      which a new common symbol should simply override the definition
1099      in the shared library.  */
1100
1101   if (! newdyn
1102       && bfd_is_com_section (sec)
1103       && olddyncommon)
1104     {
1105       /* It would be best if we could set the hash table entry to a
1106          common symbol, but we don't know what to use for the section
1107          or the alignment.  */
1108       if (! ((*info->callbacks->multiple_common)
1109              (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1110               h->size, abfd, bfd_link_hash_common, sym->st_size)))
1111         return FALSE;
1112
1113       /* If the presumed common symbol in the dynamic object is
1114          larger, pretend that the new symbol has its size.  */
1115
1116       if (h->size > *pvalue)
1117         *pvalue = h->size;
1118
1119       /* FIXME: We no longer know the alignment required by the symbol
1120          in the dynamic object, so we just wind up using the one from
1121          the regular object.  */
1122
1123       olddef = FALSE;
1124       olddyncommon = FALSE;
1125
1126       h->root.type = bfd_link_hash_undefined;
1127       h->root.u.undef.abfd = h->root.u.def.section->owner;
1128
1129       *size_change_ok = TRUE;
1130       *type_change_ok = TRUE;
1131
1132       if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1133         flip = *sym_hash;
1134       else
1135         h->verinfo.vertree = NULL;
1136     }
1137
1138   if (flip != NULL)
1139     {
1140       /* Handle the case where we had a versioned symbol in a dynamic
1141          library and now find a definition in a normal object.  In this
1142          case, we make the versioned symbol point to the normal one.  */
1143       const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1144       flip->root.type = h->root.type;
1145       h->root.type = bfd_link_hash_indirect;
1146       h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1147       (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1148       flip->root.u.undef.abfd = h->root.u.undef.abfd;
1149       if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1150         {
1151           h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
1152           flip->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1153         }
1154     }
1155
1156   return TRUE;
1157 }
1158
1159 /* This function is called to create an indirect symbol from the
1160    default for the symbol with the default version if needed. The
1161    symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE.  We
1162    set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
1163    indicates if it comes from a DT_NEEDED entry of a shared object.  */
1164
1165 bfd_boolean
1166 _bfd_elf_add_default_symbol (bfd *abfd,
1167                              struct bfd_link_info *info,
1168                              struct elf_link_hash_entry *h,
1169                              const char *name,
1170                              Elf_Internal_Sym *sym,
1171                              asection **psec,
1172                              bfd_vma *value,
1173                              bfd_boolean *dynsym,
1174                              bfd_boolean override,
1175                              bfd_boolean dt_needed)
1176 {
1177   bfd_boolean type_change_ok;
1178   bfd_boolean size_change_ok;
1179   bfd_boolean skip;
1180   char *shortname;
1181   struct elf_link_hash_entry *hi;
1182   struct bfd_link_hash_entry *bh;
1183   const struct elf_backend_data *bed;
1184   bfd_boolean collect;
1185   bfd_boolean dynamic;
1186   char *p;
1187   size_t len, shortlen;
1188   asection *sec;
1189
1190   /* If this symbol has a version, and it is the default version, we
1191      create an indirect symbol from the default name to the fully
1192      decorated name.  This will cause external references which do not
1193      specify a version to be bound to this version of the symbol.  */
1194   p = strchr (name, ELF_VER_CHR);
1195   if (p == NULL || p[1] != ELF_VER_CHR)
1196     return TRUE;
1197
1198   if (override)
1199     {
1200       /* We are overridden by an old definition. We need to check if we
1201          need to create the indirect symbol from the default name.  */
1202       hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1203                                  FALSE, FALSE);
1204       BFD_ASSERT (hi != NULL);
1205       if (hi == h)
1206         return TRUE;
1207       while (hi->root.type == bfd_link_hash_indirect
1208              || hi->root.type == bfd_link_hash_warning)
1209         {
1210           hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1211           if (hi == h)
1212             return TRUE;
1213         }
1214     }
1215
1216   bed = get_elf_backend_data (abfd);
1217   collect = bed->collect;
1218   dynamic = (abfd->flags & DYNAMIC) != 0;
1219
1220   shortlen = p - name;
1221   shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1222   if (shortname == NULL)
1223     return FALSE;
1224   memcpy (shortname, name, shortlen);
1225   shortname[shortlen] = '\0';
1226
1227   /* We are going to create a new symbol.  Merge it with any existing
1228      symbol with this name.  For the purposes of the merge, act as
1229      though we were defining the symbol we just defined, although we
1230      actually going to define an indirect symbol.  */
1231   type_change_ok = FALSE;
1232   size_change_ok = FALSE;
1233   sec = *psec;
1234   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1235                               &hi, &skip, &override, &type_change_ok,
1236                               &size_change_ok, dt_needed))
1237     return FALSE;
1238
1239   if (skip)
1240     goto nondefault;
1241
1242   if (! override)
1243     {
1244       bh = &hi->root;
1245       if (! (_bfd_generic_link_add_one_symbol
1246              (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1247               0, name, FALSE, collect, &bh)))
1248         return FALSE;
1249       hi = (struct elf_link_hash_entry *) bh;
1250     }
1251   else
1252     {
1253       /* In this case the symbol named SHORTNAME is overriding the
1254          indirect symbol we want to add.  We were planning on making
1255          SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
1256          is the name without a version.  NAME is the fully versioned
1257          name, and it is the default version.
1258
1259          Overriding means that we already saw a definition for the
1260          symbol SHORTNAME in a regular object, and it is overriding
1261          the symbol defined in the dynamic object.
1262
1263          When this happens, we actually want to change NAME, the
1264          symbol we just added, to refer to SHORTNAME.  This will cause
1265          references to NAME in the shared object to become references
1266          to SHORTNAME in the regular object.  This is what we expect
1267          when we override a function in a shared object: that the
1268          references in the shared object will be mapped to the
1269          definition in the regular object.  */
1270
1271       while (hi->root.type == bfd_link_hash_indirect
1272              || hi->root.type == bfd_link_hash_warning)
1273         hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1274
1275       h->root.type = bfd_link_hash_indirect;
1276       h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1277       if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1278         {
1279           h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1280           hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1281           if (hi->elf_link_hash_flags
1282               & (ELF_LINK_HASH_REF_REGULAR
1283                  | ELF_LINK_HASH_DEF_REGULAR))
1284             {
1285               if (! _bfd_elf_link_record_dynamic_symbol (info, hi))
1286                 return FALSE;
1287             }
1288         }
1289
1290       /* Now set HI to H, so that the following code will set the
1291          other fields correctly.  */
1292       hi = h;
1293     }
1294
1295   /* If there is a duplicate definition somewhere, then HI may not
1296      point to an indirect symbol.  We will have reported an error to
1297      the user in that case.  */
1298
1299   if (hi->root.type == bfd_link_hash_indirect)
1300     {
1301       struct elf_link_hash_entry *ht;
1302
1303       ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1304       (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1305
1306       /* See if the new flags lead us to realize that the symbol must
1307          be dynamic.  */
1308       if (! *dynsym)
1309         {
1310           if (! dynamic)
1311             {
1312               if (info->shared
1313                   || ((hi->elf_link_hash_flags
1314                        & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1315                 *dynsym = TRUE;
1316             }
1317           else
1318             {
1319               if ((hi->elf_link_hash_flags
1320                    & ELF_LINK_HASH_REF_REGULAR) != 0)
1321                 *dynsym = TRUE;
1322             }
1323         }
1324     }
1325
1326   /* We also need to define an indirection from the nondefault version
1327      of the symbol.  */
1328
1329 nondefault:
1330   len = strlen (name);
1331   shortname = bfd_hash_allocate (&info->hash->table, len);
1332   if (shortname == NULL)
1333     return FALSE;
1334   memcpy (shortname, name, shortlen);
1335   memcpy (shortname + shortlen, p + 1, len - shortlen);
1336
1337   /* Once again, merge with any existing symbol.  */
1338   type_change_ok = FALSE;
1339   size_change_ok = FALSE;
1340   sec = *psec;
1341   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1342                               &hi, &skip, &override, &type_change_ok,
1343                               &size_change_ok, dt_needed))
1344     return FALSE;
1345
1346   if (skip)
1347     return TRUE;
1348
1349   if (override)
1350     {
1351       /* Here SHORTNAME is a versioned name, so we don't expect to see
1352          the type of override we do in the case above unless it is
1353          overridden by a versioned definition.  */
1354       if (hi->root.type != bfd_link_hash_defined
1355           && hi->root.type != bfd_link_hash_defweak)
1356         (*_bfd_error_handler)
1357           (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1358            bfd_archive_filename (abfd), shortname);
1359     }
1360   else
1361     {
1362       bh = &hi->root;
1363       if (! (_bfd_generic_link_add_one_symbol
1364              (info, abfd, shortname, BSF_INDIRECT,
1365               bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1366         return FALSE;
1367       hi = (struct elf_link_hash_entry *) bh;
1368
1369       /* If there is a duplicate definition somewhere, then HI may not
1370          point to an indirect symbol.  We will have reported an error
1371          to the user in that case.  */
1372
1373       if (hi->root.type == bfd_link_hash_indirect)
1374         {
1375           (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1376
1377           /* See if the new flags lead us to realize that the symbol
1378              must be dynamic.  */
1379           if (! *dynsym)
1380             {
1381               if (! dynamic)
1382                 {
1383                   if (info->shared
1384                       || ((hi->elf_link_hash_flags
1385                            & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1386                     *dynsym = TRUE;
1387                 }
1388               else
1389                 {
1390                   if ((hi->elf_link_hash_flags
1391                        & ELF_LINK_HASH_REF_REGULAR) != 0)
1392                     *dynsym = TRUE;
1393                 }
1394             }
1395         }
1396     }
1397
1398   return TRUE;
1399 }
1400 \f
1401 /* This routine is used to export all defined symbols into the dynamic
1402    symbol table.  It is called via elf_link_hash_traverse.  */
1403
1404 bfd_boolean
1405 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1406 {
1407   struct elf_info_failed *eif = data;
1408
1409   /* Ignore indirect symbols.  These are added by the versioning code.  */
1410   if (h->root.type == bfd_link_hash_indirect)
1411     return TRUE;
1412
1413   if (h->root.type == bfd_link_hash_warning)
1414     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1415
1416   if (h->dynindx == -1
1417       && (h->elf_link_hash_flags
1418           & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
1419     {
1420       struct bfd_elf_version_tree *t;
1421       struct bfd_elf_version_expr *d;
1422
1423       for (t = eif->verdefs; t != NULL; t = t->next)
1424         {
1425           if (t->globals.list != NULL)
1426             {
1427               d = (*t->match) (&t->globals, NULL, h->root.root.string);
1428               if (d != NULL)
1429                 goto doit;
1430             }
1431
1432           if (t->locals.list != NULL)
1433             {
1434               d = (*t->match) (&t->locals, NULL, h->root.root.string);
1435               if (d != NULL)
1436                 return TRUE;
1437             }
1438         }
1439
1440       if (!eif->verdefs)
1441         {
1442         doit:
1443           if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
1444             {
1445               eif->failed = TRUE;
1446               return FALSE;
1447             }
1448         }
1449     }
1450
1451   return TRUE;
1452 }
1453 \f
1454 /* Look through the symbols which are defined in other shared
1455    libraries and referenced here.  Update the list of version
1456    dependencies.  This will be put into the .gnu.version_r section.
1457    This function is called via elf_link_hash_traverse.  */
1458
1459 bfd_boolean
1460 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1461                                          void *data)
1462 {
1463   struct elf_find_verdep_info *rinfo = data;
1464   Elf_Internal_Verneed *t;
1465   Elf_Internal_Vernaux *a;
1466   bfd_size_type amt;
1467
1468   if (h->root.type == bfd_link_hash_warning)
1469     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1470
1471   /* We only care about symbols defined in shared objects with version
1472      information.  */
1473   if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1474       || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1475       || h->dynindx == -1
1476       || h->verinfo.verdef == NULL)
1477     return TRUE;
1478
1479   /* See if we already know about this version.  */
1480   for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1481     {
1482       if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1483         continue;
1484
1485       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1486         if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1487           return TRUE;
1488
1489       break;
1490     }
1491
1492   /* This is a new version.  Add it to tree we are building.  */
1493
1494   if (t == NULL)
1495     {
1496       amt = sizeof *t;
1497       t = bfd_zalloc (rinfo->output_bfd, amt);
1498       if (t == NULL)
1499         {
1500           rinfo->failed = TRUE;
1501           return FALSE;
1502         }
1503
1504       t->vn_bfd = h->verinfo.verdef->vd_bfd;
1505       t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1506       elf_tdata (rinfo->output_bfd)->verref = t;
1507     }
1508
1509   amt = sizeof *a;
1510   a = bfd_zalloc (rinfo->output_bfd, amt);
1511
1512   /* Note that we are copying a string pointer here, and testing it
1513      above.  If bfd_elf_string_from_elf_section is ever changed to
1514      discard the string data when low in memory, this will have to be
1515      fixed.  */
1516   a->vna_nodename = h->verinfo.verdef->vd_nodename;
1517
1518   a->vna_flags = h->verinfo.verdef->vd_flags;
1519   a->vna_nextptr = t->vn_auxptr;
1520
1521   h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1522   ++rinfo->vers;
1523
1524   a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1525
1526   t->vn_auxptr = a;
1527
1528   return TRUE;
1529 }
1530
1531 /* Figure out appropriate versions for all the symbols.  We may not
1532    have the version number script until we have read all of the input
1533    files, so until that point we don't know which symbols should be
1534    local.  This function is called via elf_link_hash_traverse.  */
1535
1536 bfd_boolean
1537 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1538 {
1539   struct elf_assign_sym_version_info *sinfo;
1540   struct bfd_link_info *info;
1541   const struct elf_backend_data *bed;
1542   struct elf_info_failed eif;
1543   char *p;
1544   bfd_size_type amt;
1545
1546   sinfo = data;
1547   info = sinfo->info;
1548
1549   if (h->root.type == bfd_link_hash_warning)
1550     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1551
1552   /* Fix the symbol flags.  */
1553   eif.failed = FALSE;
1554   eif.info = info;
1555   if (! _bfd_elf_fix_symbol_flags (h, &eif))
1556     {
1557       if (eif.failed)
1558         sinfo->failed = TRUE;
1559       return FALSE;
1560     }
1561
1562   /* We only need version numbers for symbols defined in regular
1563      objects.  */
1564   if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1565     return TRUE;
1566
1567   bed = get_elf_backend_data (sinfo->output_bfd);
1568   p = strchr (h->root.root.string, ELF_VER_CHR);
1569   if (p != NULL && h->verinfo.vertree == NULL)
1570     {
1571       struct bfd_elf_version_tree *t;
1572       bfd_boolean hidden;
1573
1574       hidden = TRUE;
1575
1576       /* There are two consecutive ELF_VER_CHR characters if this is
1577          not a hidden symbol.  */
1578       ++p;
1579       if (*p == ELF_VER_CHR)
1580         {
1581           hidden = FALSE;
1582           ++p;
1583         }
1584
1585       /* If there is no version string, we can just return out.  */
1586       if (*p == '\0')
1587         {
1588           if (hidden)
1589             h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1590           return TRUE;
1591         }
1592
1593       /* Look for the version.  If we find it, it is no longer weak.  */
1594       for (t = sinfo->verdefs; t != NULL; t = t->next)
1595         {
1596           if (strcmp (t->name, p) == 0)
1597             {
1598               size_t len;
1599               char *alc;
1600               struct bfd_elf_version_expr *d;
1601
1602               len = p - h->root.root.string;
1603               alc = bfd_malloc (len);
1604               if (alc == NULL)
1605                 return FALSE;
1606               memcpy (alc, h->root.root.string, len - 1);
1607               alc[len - 1] = '\0';
1608               if (alc[len - 2] == ELF_VER_CHR)
1609                 alc[len - 2] = '\0';
1610
1611               h->verinfo.vertree = t;
1612               t->used = TRUE;
1613               d = NULL;
1614
1615               if (t->globals.list != NULL)
1616                 d = (*t->match) (&t->globals, NULL, alc);
1617
1618               /* See if there is anything to force this symbol to
1619                  local scope.  */
1620               if (d == NULL && t->locals.list != NULL)
1621                 {
1622                   d = (*t->match) (&t->locals, NULL, alc);
1623                   if (d != NULL
1624                       && h->dynindx != -1
1625                       && info->shared
1626                       && ! info->export_dynamic)
1627                     (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1628                 }
1629
1630               free (alc);
1631               break;
1632             }
1633         }
1634
1635       /* If we are building an application, we need to create a
1636          version node for this version.  */
1637       if (t == NULL && info->executable)
1638         {
1639           struct bfd_elf_version_tree **pp;
1640           int version_index;
1641
1642           /* If we aren't going to export this symbol, we don't need
1643              to worry about it.  */
1644           if (h->dynindx == -1)
1645             return TRUE;
1646
1647           amt = sizeof *t;
1648           t = bfd_zalloc (sinfo->output_bfd, amt);
1649           if (t == NULL)
1650             {
1651               sinfo->failed = TRUE;
1652               return FALSE;
1653             }
1654
1655           t->name = p;
1656           t->name_indx = (unsigned int) -1;
1657           t->used = TRUE;
1658
1659           version_index = 1;
1660           /* Don't count anonymous version tag.  */
1661           if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1662             version_index = 0;
1663           for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1664             ++version_index;
1665           t->vernum = version_index;
1666
1667           *pp = t;
1668
1669           h->verinfo.vertree = t;
1670         }
1671       else if (t == NULL)
1672         {
1673           /* We could not find the version for a symbol when
1674              generating a shared archive.  Return an error.  */
1675           (*_bfd_error_handler)
1676             (_("%s: undefined versioned symbol name %s"),
1677              bfd_get_filename (sinfo->output_bfd), h->root.root.string);
1678           bfd_set_error (bfd_error_bad_value);
1679           sinfo->failed = TRUE;
1680           return FALSE;
1681         }
1682
1683       if (hidden)
1684         h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1685     }
1686
1687   /* If we don't have a version for this symbol, see if we can find
1688      something.  */
1689   if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1690     {
1691       struct bfd_elf_version_tree *t;
1692       struct bfd_elf_version_tree *local_ver;
1693       struct bfd_elf_version_expr *d;
1694
1695       /* See if can find what version this symbol is in.  If the
1696          symbol is supposed to be local, then don't actually register
1697          it.  */
1698       local_ver = NULL;
1699       for (t = sinfo->verdefs; t != NULL; t = t->next)
1700         {
1701           if (t->globals.list != NULL)
1702             {
1703               bfd_boolean matched;
1704
1705               matched = FALSE;
1706               d = NULL;
1707               while ((d = (*t->match) (&t->globals, d,
1708                                        h->root.root.string)) != NULL)
1709                 if (d->symver)
1710                   matched = TRUE;
1711                 else
1712                   {
1713                     /* There is a version without definition.  Make
1714                        the symbol the default definition for this
1715                        version.  */
1716                     h->verinfo.vertree = t;
1717                     local_ver = NULL;
1718                     d->script = 1;
1719                     break;
1720                   }
1721               if (d != NULL)
1722                 break;
1723               else if (matched)
1724                 /* There is no undefined version for this symbol. Hide the
1725                    default one.  */
1726                 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1727             }
1728
1729           if (t->locals.list != NULL)
1730             {
1731               d = NULL;
1732               while ((d = (*t->match) (&t->locals, d,
1733                                        h->root.root.string)) != NULL)
1734                 {
1735                   local_ver = t;
1736                   /* If the match is "*", keep looking for a more
1737                      explicit, perhaps even global, match.
1738                      XXX: Shouldn't this be !d->wildcard instead?  */
1739                   if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1740                     break;
1741                 }
1742
1743               if (d != NULL)
1744                 break;
1745             }
1746         }
1747
1748       if (local_ver != NULL)
1749         {
1750           h->verinfo.vertree = local_ver;
1751           if (h->dynindx != -1
1752               && info->shared
1753               && ! info->export_dynamic)
1754             {
1755               (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1756             }
1757         }
1758     }
1759
1760   return TRUE;
1761 }
1762 \f
1763 /* Read and swap the relocs from the section indicated by SHDR.  This
1764    may be either a REL or a RELA section.  The relocations are
1765    translated into RELA relocations and stored in INTERNAL_RELOCS,
1766    which should have already been allocated to contain enough space.
1767    The EXTERNAL_RELOCS are a buffer where the external form of the
1768    relocations should be stored.
1769
1770    Returns FALSE if something goes wrong.  */
1771
1772 static bfd_boolean
1773 elf_link_read_relocs_from_section (bfd *abfd,
1774                                    asection *sec,
1775                                    Elf_Internal_Shdr *shdr,
1776                                    void *external_relocs,
1777                                    Elf_Internal_Rela *internal_relocs)
1778 {
1779   const struct elf_backend_data *bed;
1780   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1781   const bfd_byte *erela;
1782   const bfd_byte *erelaend;
1783   Elf_Internal_Rela *irela;
1784   Elf_Internal_Shdr *symtab_hdr;
1785   size_t nsyms;
1786
1787   /* Position ourselves at the start of the section.  */
1788   if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1789     return FALSE;
1790
1791   /* Read the relocations.  */
1792   if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1793     return FALSE;
1794
1795   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1796   nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1797
1798   bed = get_elf_backend_data (abfd);
1799
1800   /* Convert the external relocations to the internal format.  */
1801   if (shdr->sh_entsize == bed->s->sizeof_rel)
1802     swap_in = bed->s->swap_reloc_in;
1803   else if (shdr->sh_entsize == bed->s->sizeof_rela)
1804     swap_in = bed->s->swap_reloca_in;
1805   else
1806     {
1807       bfd_set_error (bfd_error_wrong_format);
1808       return FALSE;
1809     }
1810
1811   erela = external_relocs;
1812   erelaend = erela + shdr->sh_size;
1813   irela = internal_relocs;
1814   while (erela < erelaend)
1815     {
1816       bfd_vma r_symndx;
1817
1818       (*swap_in) (abfd, erela, irela);
1819       r_symndx = ELF32_R_SYM (irela->r_info);
1820       if (bed->s->arch_size == 64)
1821         r_symndx >>= 24;
1822       if ((size_t) r_symndx >= nsyms)
1823         {
1824           (*_bfd_error_handler)
1825             (_("%s: bad reloc symbol index (0x%lx >= 0x%lx) for offset 0x%lx in section `%s'"),
1826              bfd_archive_filename (abfd), (unsigned long) r_symndx,
1827              (unsigned long) nsyms, irela->r_offset, sec->name);
1828           bfd_set_error (bfd_error_bad_value);
1829           return FALSE;
1830         }
1831       irela += bed->s->int_rels_per_ext_rel;
1832       erela += shdr->sh_entsize;
1833     }
1834
1835   return TRUE;
1836 }
1837
1838 /* Read and swap the relocs for a section O.  They may have been
1839    cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1840    not NULL, they are used as buffers to read into.  They are known to
1841    be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
1842    the return value is allocated using either malloc or bfd_alloc,
1843    according to the KEEP_MEMORY argument.  If O has two relocation
1844    sections (both REL and RELA relocations), then the REL_HDR
1845    relocations will appear first in INTERNAL_RELOCS, followed by the
1846    REL_HDR2 relocations.  */
1847
1848 Elf_Internal_Rela *
1849 _bfd_elf_link_read_relocs (bfd *abfd,
1850                            asection *o,
1851                            void *external_relocs,
1852                            Elf_Internal_Rela *internal_relocs,
1853                            bfd_boolean keep_memory)
1854 {
1855   Elf_Internal_Shdr *rel_hdr;
1856   void *alloc1 = NULL;
1857   Elf_Internal_Rela *alloc2 = NULL;
1858   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1859
1860   if (elf_section_data (o)->relocs != NULL)
1861     return elf_section_data (o)->relocs;
1862
1863   if (o->reloc_count == 0)
1864     return NULL;
1865
1866   rel_hdr = &elf_section_data (o)->rel_hdr;
1867
1868   if (internal_relocs == NULL)
1869     {
1870       bfd_size_type size;
1871
1872       size = o->reloc_count;
1873       size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
1874       if (keep_memory)
1875         internal_relocs = bfd_alloc (abfd, size);
1876       else
1877         internal_relocs = alloc2 = bfd_malloc (size);
1878       if (internal_relocs == NULL)
1879         goto error_return;
1880     }
1881
1882   if (external_relocs == NULL)
1883     {
1884       bfd_size_type size = rel_hdr->sh_size;
1885
1886       if (elf_section_data (o)->rel_hdr2)
1887         size += elf_section_data (o)->rel_hdr2->sh_size;
1888       alloc1 = bfd_malloc (size);
1889       if (alloc1 == NULL)
1890         goto error_return;
1891       external_relocs = alloc1;
1892     }
1893
1894   if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
1895                                           external_relocs,
1896                                           internal_relocs))
1897     goto error_return;
1898   if (elf_section_data (o)->rel_hdr2
1899       && (!elf_link_read_relocs_from_section
1900           (abfd, o,
1901            elf_section_data (o)->rel_hdr2,
1902            ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
1903            internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
1904                               * bed->s->int_rels_per_ext_rel))))
1905     goto error_return;
1906
1907   /* Cache the results for next time, if we can.  */
1908   if (keep_memory)
1909     elf_section_data (o)->relocs = internal_relocs;
1910
1911   if (alloc1 != NULL)
1912     free (alloc1);
1913
1914   /* Don't free alloc2, since if it was allocated we are passing it
1915      back (under the name of internal_relocs).  */
1916
1917   return internal_relocs;
1918
1919  error_return:
1920   if (alloc1 != NULL)
1921     free (alloc1);
1922   if (alloc2 != NULL)
1923     free (alloc2);
1924   return NULL;
1925 }
1926
1927 /* Compute the size of, and allocate space for, REL_HDR which is the
1928    section header for a section containing relocations for O.  */
1929
1930 bfd_boolean
1931 _bfd_elf_link_size_reloc_section (bfd *abfd,
1932                                   Elf_Internal_Shdr *rel_hdr,
1933                                   asection *o)
1934 {
1935   bfd_size_type reloc_count;
1936   bfd_size_type num_rel_hashes;
1937
1938   /* Figure out how many relocations there will be.  */
1939   if (rel_hdr == &elf_section_data (o)->rel_hdr)
1940     reloc_count = elf_section_data (o)->rel_count;
1941   else
1942     reloc_count = elf_section_data (o)->rel_count2;
1943
1944   num_rel_hashes = o->reloc_count;
1945   if (num_rel_hashes < reloc_count)
1946     num_rel_hashes = reloc_count;
1947
1948   /* That allows us to calculate the size of the section.  */
1949   rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
1950
1951   /* The contents field must last into write_object_contents, so we
1952      allocate it with bfd_alloc rather than malloc.  Also since we
1953      cannot be sure that the contents will actually be filled in,
1954      we zero the allocated space.  */
1955   rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
1956   if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
1957     return FALSE;
1958
1959   /* We only allocate one set of hash entries, so we only do it the
1960      first time we are called.  */
1961   if (elf_section_data (o)->rel_hashes == NULL
1962       && num_rel_hashes)
1963     {
1964       struct elf_link_hash_entry **p;
1965
1966       p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
1967       if (p == NULL)
1968         return FALSE;
1969
1970       elf_section_data (o)->rel_hashes = p;
1971     }
1972
1973   return TRUE;
1974 }
1975
1976 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
1977    originated from the section given by INPUT_REL_HDR) to the
1978    OUTPUT_BFD.  */
1979
1980 bfd_boolean
1981 _bfd_elf_link_output_relocs (bfd *output_bfd,
1982                              asection *input_section,
1983                              Elf_Internal_Shdr *input_rel_hdr,
1984                              Elf_Internal_Rela *internal_relocs)
1985 {
1986   Elf_Internal_Rela *irela;
1987   Elf_Internal_Rela *irelaend;
1988   bfd_byte *erel;
1989   Elf_Internal_Shdr *output_rel_hdr;
1990   asection *output_section;
1991   unsigned int *rel_countp = NULL;
1992   const struct elf_backend_data *bed;
1993   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
1994
1995   output_section = input_section->output_section;
1996   output_rel_hdr = NULL;
1997
1998   if (elf_section_data (output_section)->rel_hdr.sh_entsize
1999       == input_rel_hdr->sh_entsize)
2000     {
2001       output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2002       rel_countp = &elf_section_data (output_section)->rel_count;
2003     }
2004   else if (elf_section_data (output_section)->rel_hdr2
2005            && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2006                == input_rel_hdr->sh_entsize))
2007     {
2008       output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2009       rel_countp = &elf_section_data (output_section)->rel_count2;
2010     }
2011   else
2012     {
2013       (*_bfd_error_handler)
2014         (_("%s: relocation size mismatch in %s section %s"),
2015          bfd_get_filename (output_bfd),
2016          bfd_archive_filename (input_section->owner),
2017          input_section->name);
2018       bfd_set_error (bfd_error_wrong_object_format);
2019       return FALSE;
2020     }
2021
2022   bed = get_elf_backend_data (output_bfd);
2023   if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2024     swap_out = bed->s->swap_reloc_out;
2025   else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2026     swap_out = bed->s->swap_reloca_out;
2027   else
2028     abort ();
2029
2030   erel = output_rel_hdr->contents;
2031   erel += *rel_countp * input_rel_hdr->sh_entsize;
2032   irela = internal_relocs;
2033   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2034                       * bed->s->int_rels_per_ext_rel);
2035   while (irela < irelaend)
2036     {
2037       (*swap_out) (output_bfd, irela, erel);
2038       irela += bed->s->int_rels_per_ext_rel;
2039       erel += input_rel_hdr->sh_entsize;
2040     }
2041
2042   /* Bump the counter, so that we know where to add the next set of
2043      relocations.  */
2044   *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2045
2046   return TRUE;
2047 }
2048 \f
2049 /* Fix up the flags for a symbol.  This handles various cases which
2050    can only be fixed after all the input files are seen.  This is
2051    currently called by both adjust_dynamic_symbol and
2052    assign_sym_version, which is unnecessary but perhaps more robust in
2053    the face of future changes.  */
2054
2055 bfd_boolean
2056 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2057                            struct elf_info_failed *eif)
2058 {
2059   /* If this symbol was mentioned in a non-ELF file, try to set
2060      DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
2061      permit a non-ELF file to correctly refer to a symbol defined in
2062      an ELF dynamic object.  */
2063   if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
2064     {
2065       while (h->root.type == bfd_link_hash_indirect)
2066         h = (struct elf_link_hash_entry *) h->root.u.i.link;
2067
2068       if (h->root.type != bfd_link_hash_defined
2069           && h->root.type != bfd_link_hash_defweak)
2070         h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2071                                    | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2072       else
2073         {
2074           if (h->root.u.def.section->owner != NULL
2075               && (bfd_get_flavour (h->root.u.def.section->owner)
2076                   == bfd_target_elf_flavour))
2077             h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2078                                        | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2079           else
2080             h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2081         }
2082
2083       if (h->dynindx == -1
2084           && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2085               || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
2086         {
2087           if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
2088             {
2089               eif->failed = TRUE;
2090               return FALSE;
2091             }
2092         }
2093     }
2094   else
2095     {
2096       /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
2097          was first seen in a non-ELF file.  Fortunately, if the symbol
2098          was first seen in an ELF file, we're probably OK unless the
2099          symbol was defined in a non-ELF file.  Catch that case here.
2100          FIXME: We're still in trouble if the symbol was first seen in
2101          a dynamic object, and then later in a non-ELF regular object.  */
2102       if ((h->root.type == bfd_link_hash_defined
2103            || h->root.type == bfd_link_hash_defweak)
2104           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2105           && (h->root.u.def.section->owner != NULL
2106               ? (bfd_get_flavour (h->root.u.def.section->owner)
2107                  != bfd_target_elf_flavour)
2108               : (bfd_is_abs_section (h->root.u.def.section)
2109                  && (h->elf_link_hash_flags
2110                      & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
2111         h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2112     }
2113
2114   /* If this is a final link, and the symbol was defined as a common
2115      symbol in a regular object file, and there was no definition in
2116      any dynamic object, then the linker will have allocated space for
2117      the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2118      flag will not have been set.  */
2119   if (h->root.type == bfd_link_hash_defined
2120       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2121       && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
2122       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2123       && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2124     h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2125
2126   /* If -Bsymbolic was used (which means to bind references to global
2127      symbols to the definition within the shared object), and this
2128      symbol was defined in a regular object, then it actually doesn't
2129      need a PLT entry.  Likewise, if the symbol has non-default
2130      visibility.  If the symbol has hidden or internal visibility, we
2131      will force it local.  */
2132   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
2133       && eif->info->shared
2134       && is_elf_hash_table (eif->info->hash)
2135       && (eif->info->symbolic
2136           || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2137       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2138     {
2139       const struct elf_backend_data *bed;
2140       bfd_boolean force_local;
2141
2142       bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2143
2144       force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2145                      || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2146       (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2147     }
2148
2149   /* If a weak undefined symbol has non-default visibility, we also
2150      hide it from the dynamic linker.  */
2151   if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2152       && h->root.type == bfd_link_hash_undefweak)
2153     {
2154       const struct elf_backend_data *bed;
2155       bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2156       (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2157     }
2158
2159   /* If this is a weak defined symbol in a dynamic object, and we know
2160      the real definition in the dynamic object, copy interesting flags
2161      over to the real definition.  */
2162   if (h->weakdef != NULL)
2163     {
2164       struct elf_link_hash_entry *weakdef;
2165
2166       weakdef = h->weakdef;
2167       if (h->root.type == bfd_link_hash_indirect)
2168         h = (struct elf_link_hash_entry *) h->root.u.i.link;
2169
2170       BFD_ASSERT (h->root.type == bfd_link_hash_defined
2171                   || h->root.type == bfd_link_hash_defweak);
2172       BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2173                   || weakdef->root.type == bfd_link_hash_defweak);
2174       BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
2175
2176       /* If the real definition is defined by a regular object file,
2177          don't do anything special.  See the longer description in
2178          _bfd_elf_adjust_dynamic_symbol, below.  */
2179       if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2180         h->weakdef = NULL;
2181       else
2182         {
2183           const struct elf_backend_data *bed;
2184
2185           bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2186           (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2187         }
2188     }
2189
2190   return TRUE;
2191 }
2192
2193 /* Make the backend pick a good value for a dynamic symbol.  This is
2194    called via elf_link_hash_traverse, and also calls itself
2195    recursively.  */
2196
2197 bfd_boolean
2198 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2199 {
2200   struct elf_info_failed *eif = data;
2201   bfd *dynobj;
2202   const struct elf_backend_data *bed;
2203
2204   if (! is_elf_hash_table (eif->info->hash))
2205     return FALSE;
2206
2207   if (h->root.type == bfd_link_hash_warning)
2208     {
2209       h->plt = elf_hash_table (eif->info)->init_offset;
2210       h->got = elf_hash_table (eif->info)->init_offset;
2211
2212       /* When warning symbols are created, they **replace** the "real"
2213          entry in the hash table, thus we never get to see the real
2214          symbol in a hash traversal.  So look at it now.  */
2215       h = (struct elf_link_hash_entry *) h->root.u.i.link;
2216     }
2217
2218   /* Ignore indirect symbols.  These are added by the versioning code.  */
2219   if (h->root.type == bfd_link_hash_indirect)
2220     return TRUE;
2221
2222   /* Fix the symbol flags.  */
2223   if (! _bfd_elf_fix_symbol_flags (h, eif))
2224     return FALSE;
2225
2226   /* If this symbol does not require a PLT entry, and it is not
2227      defined by a dynamic object, or is not referenced by a regular
2228      object, ignore it.  We do have to handle a weak defined symbol,
2229      even if no regular object refers to it, if we decided to add it
2230      to the dynamic symbol table.  FIXME: Do we normally need to worry
2231      about symbols which are defined by one dynamic object and
2232      referenced by another one?  */
2233   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
2234       && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2235           || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2236           || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
2237               && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
2238     {
2239       h->plt = elf_hash_table (eif->info)->init_offset;
2240       return TRUE;
2241     }
2242
2243   /* If we've already adjusted this symbol, don't do it again.  This
2244      can happen via a recursive call.  */
2245   if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
2246     return TRUE;
2247
2248   /* Don't look at this symbol again.  Note that we must set this
2249      after checking the above conditions, because we may look at a
2250      symbol once, decide not to do anything, and then get called
2251      recursively later after REF_REGULAR is set below.  */
2252   h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
2253
2254   /* If this is a weak definition, and we know a real definition, and
2255      the real symbol is not itself defined by a regular object file,
2256      then get a good value for the real definition.  We handle the
2257      real symbol first, for the convenience of the backend routine.
2258
2259      Note that there is a confusing case here.  If the real definition
2260      is defined by a regular object file, we don't get the real symbol
2261      from the dynamic object, but we do get the weak symbol.  If the
2262      processor backend uses a COPY reloc, then if some routine in the
2263      dynamic object changes the real symbol, we will not see that
2264      change in the corresponding weak symbol.  This is the way other
2265      ELF linkers work as well, and seems to be a result of the shared
2266      library model.
2267
2268      I will clarify this issue.  Most SVR4 shared libraries define the
2269      variable _timezone and define timezone as a weak synonym.  The
2270      tzset call changes _timezone.  If you write
2271        extern int timezone;
2272        int _timezone = 5;
2273        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2274      you might expect that, since timezone is a synonym for _timezone,
2275      the same number will print both times.  However, if the processor
2276      backend uses a COPY reloc, then actually timezone will be copied
2277      into your process image, and, since you define _timezone
2278      yourself, _timezone will not.  Thus timezone and _timezone will
2279      wind up at different memory locations.  The tzset call will set
2280      _timezone, leaving timezone unchanged.  */
2281
2282   if (h->weakdef != NULL)
2283     {
2284       /* If we get to this point, we know there is an implicit
2285          reference by a regular object file via the weak symbol H.
2286          FIXME: Is this really true?  What if the traversal finds
2287          H->WEAKDEF before it finds H?  */
2288       h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2289
2290       if (! _bfd_elf_adjust_dynamic_symbol (h->weakdef, eif))
2291         return FALSE;
2292     }
2293
2294   /* If a symbol has no type and no size and does not require a PLT
2295      entry, then we are probably about to do the wrong thing here: we
2296      are probably going to create a COPY reloc for an empty object.
2297      This case can arise when a shared object is built with assembly
2298      code, and the assembly code fails to set the symbol type.  */
2299   if (h->size == 0
2300       && h->type == STT_NOTYPE
2301       && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
2302     (*_bfd_error_handler)
2303       (_("warning: type and size of dynamic symbol `%s' are not defined"),
2304        h->root.root.string);
2305
2306   dynobj = elf_hash_table (eif->info)->dynobj;
2307   bed = get_elf_backend_data (dynobj);
2308   if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2309     {
2310       eif->failed = TRUE;
2311       return FALSE;
2312     }
2313
2314   return TRUE;
2315 }
2316
2317 /* Adjust all external symbols pointing into SEC_MERGE sections
2318    to reflect the object merging within the sections.  */
2319
2320 bfd_boolean
2321 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2322 {
2323   asection *sec;
2324
2325   if (h->root.type == bfd_link_hash_warning)
2326     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2327
2328   if ((h->root.type == bfd_link_hash_defined
2329        || h->root.type == bfd_link_hash_defweak)
2330       && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2331       && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2332     {
2333       bfd *output_bfd = data;
2334
2335       h->root.u.def.value =
2336         _bfd_merged_section_offset (output_bfd,
2337                                     &h->root.u.def.section,
2338                                     elf_section_data (sec)->sec_info,
2339                                     h->root.u.def.value, 0);
2340     }
2341
2342   return TRUE;
2343 }
2344
2345 /* Returns false if the symbol referred to by H should be considered
2346    to resolve local to the current module, and true if it should be
2347    considered to bind dynamically.  */
2348
2349 bfd_boolean
2350 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2351                            struct bfd_link_info *info,
2352                            bfd_boolean ignore_protected)
2353 {
2354   bfd_boolean binding_stays_local_p;
2355
2356   if (h == NULL)
2357     return FALSE;
2358
2359   while (h->root.type == bfd_link_hash_indirect
2360          || h->root.type == bfd_link_hash_warning)
2361     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2362
2363   /* If it was forced local, then clearly it's not dynamic.  */
2364   if (h->dynindx == -1)
2365     return FALSE;
2366   if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2367     return FALSE;
2368
2369   /* Identify the cases where name binding rules say that a
2370      visible symbol resolves locally.  */
2371   binding_stays_local_p = info->executable || info->symbolic;
2372
2373   switch (ELF_ST_VISIBILITY (h->other))
2374     {
2375     case STV_INTERNAL:
2376     case STV_HIDDEN:
2377       return FALSE;
2378
2379     case STV_PROTECTED:
2380       /* Proper resolution for function pointer equality may require
2381          that these symbols perhaps be resolved dynamically, even though
2382          we should be resolving them to the current module.  */
2383       if (!ignore_protected)
2384         binding_stays_local_p = TRUE;
2385       break;
2386
2387     default:
2388       break;
2389     }
2390
2391   /* If it isn't defined locally, then clearly it's dynamic.  */
2392   if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2393     return TRUE;
2394
2395   /* Otherwise, the symbol is dynamic if binding rules don't tell
2396      us that it remains local.  */
2397   return !binding_stays_local_p;
2398 }
2399
2400 /* Return true if the symbol referred to by H should be considered
2401    to resolve local to the current module, and false otherwise.  Differs
2402    from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2403    undefined symbols and weak symbols.  */
2404
2405 bfd_boolean
2406 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2407                               struct bfd_link_info *info,
2408                               bfd_boolean local_protected)
2409 {
2410   /* If it's a local sym, of course we resolve locally.  */
2411   if (h == NULL)
2412     return TRUE;
2413
2414   /* If we don't have a definition in a regular file, then we can't
2415      resolve locally.  The sym is either undefined or dynamic.  */
2416   if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2417     return FALSE;
2418
2419   /* Forced local symbols resolve locally.  */
2420   if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
2421     return TRUE;
2422
2423   /* As do non-dynamic symbols.  */
2424   if (h->dynindx == -1)
2425     return TRUE;
2426
2427   /* At this point, we know the symbol is defined and dynamic.  In an
2428      executable it must resolve locally, likewise when building symbolic
2429      shared libraries.  */
2430   if (info->executable || info->symbolic)
2431     return TRUE;
2432
2433   /* Now deal with defined dynamic symbols in shared libraries.  Ones
2434      with default visibility might not resolve locally.  */
2435   if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2436     return FALSE;
2437
2438   /* However, STV_HIDDEN or STV_INTERNAL ones must be local.  */
2439   if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2440     return TRUE;
2441
2442   /* Function pointer equality tests may require that STV_PROTECTED
2443      symbols be treated as dynamic symbols, even when we know that the
2444      dynamic linker will resolve them locally.  */
2445   return local_protected;
2446 }
2447
2448 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2449    aligned.  Returns the first TLS output section.  */
2450
2451 struct bfd_section *
2452 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2453 {
2454   struct bfd_section *sec, *tls;
2455   unsigned int align = 0;
2456
2457   for (sec = obfd->sections; sec != NULL; sec = sec->next)
2458     if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2459       break;
2460   tls = sec;
2461
2462   for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2463     if (sec->alignment_power > align)
2464       align = sec->alignment_power;
2465
2466   elf_hash_table (info)->tls_sec = tls;
2467
2468   /* Ensure the alignment of the first section is the largest alignment,
2469      so that the tls segment starts aligned.  */
2470   if (tls != NULL)
2471     tls->alignment_power = align;
2472
2473   return tls;
2474 }
2475
2476 /* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
2477 static bfd_boolean
2478 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2479                                   Elf_Internal_Sym *sym)
2480 {
2481   /* Local symbols do not count, but target specific ones might.  */
2482   if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2483       && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2484     return FALSE;
2485
2486   /* Function symbols do not count.  */
2487   if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2488     return FALSE;
2489
2490   /* If the section is undefined, then so is the symbol.  */
2491   if (sym->st_shndx == SHN_UNDEF)
2492     return FALSE;
2493
2494   /* If the symbol is defined in the common section, then
2495      it is a common definition and so does not count.  */
2496   if (sym->st_shndx == SHN_COMMON)
2497     return FALSE;
2498
2499   /* If the symbol is in a target specific section then we
2500      must rely upon the backend to tell us what it is.  */
2501   if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2502     /* FIXME - this function is not coded yet:
2503
2504        return _bfd_is_global_symbol_definition (abfd, sym);
2505
2506        Instead for now assume that the definition is not global,
2507        Even if this is wrong, at least the linker will behave
2508        in the same way that it used to do.  */
2509     return FALSE;
2510
2511   return TRUE;
2512 }
2513
2514 /* Search the symbol table of the archive element of the archive ABFD
2515    whose archive map contains a mention of SYMDEF, and determine if
2516    the symbol is defined in this element.  */
2517 static bfd_boolean
2518 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2519 {
2520   Elf_Internal_Shdr * hdr;
2521   bfd_size_type symcount;
2522   bfd_size_type extsymcount;
2523   bfd_size_type extsymoff;
2524   Elf_Internal_Sym *isymbuf;
2525   Elf_Internal_Sym *isym;
2526   Elf_Internal_Sym *isymend;
2527   bfd_boolean result;
2528
2529   abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2530   if (abfd == NULL)
2531     return FALSE;
2532
2533   if (! bfd_check_format (abfd, bfd_object))
2534     return FALSE;
2535
2536   /* If we have already included the element containing this symbol in the
2537      link then we do not need to include it again.  Just claim that any symbol
2538      it contains is not a definition, so that our caller will not decide to
2539      (re)include this element.  */
2540   if (abfd->archive_pass)
2541     return FALSE;
2542
2543   /* Select the appropriate symbol table.  */
2544   if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2545     hdr = &elf_tdata (abfd)->symtab_hdr;
2546   else
2547     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2548
2549   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2550
2551   /* The sh_info field of the symtab header tells us where the
2552      external symbols start.  We don't care about the local symbols.  */
2553   if (elf_bad_symtab (abfd))
2554     {
2555       extsymcount = symcount;
2556       extsymoff = 0;
2557     }
2558   else
2559     {
2560       extsymcount = symcount - hdr->sh_info;
2561       extsymoff = hdr->sh_info;
2562     }
2563
2564   if (extsymcount == 0)
2565     return FALSE;
2566
2567   /* Read in the symbol table.  */
2568   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2569                                   NULL, NULL, NULL);
2570   if (isymbuf == NULL)
2571     return FALSE;
2572
2573   /* Scan the symbol table looking for SYMDEF.  */
2574   result = FALSE;
2575   for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2576     {
2577       const char *name;
2578
2579       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2580                                               isym->st_name);
2581       if (name == NULL)
2582         break;
2583
2584       if (strcmp (name, symdef->name) == 0)
2585         {
2586           result = is_global_data_symbol_definition (abfd, isym);
2587           break;
2588         }
2589     }
2590
2591   free (isymbuf);
2592
2593   return result;
2594 }
2595 \f
2596 /* Add symbols from an ELF archive file to the linker hash table.  We
2597    don't use _bfd_generic_link_add_archive_symbols because of a
2598    problem which arises on UnixWare.  The UnixWare libc.so is an
2599    archive which includes an entry libc.so.1 which defines a bunch of
2600    symbols.  The libc.so archive also includes a number of other
2601    object files, which also define symbols, some of which are the same
2602    as those defined in libc.so.1.  Correct linking requires that we
2603    consider each object file in turn, and include it if it defines any
2604    symbols we need.  _bfd_generic_link_add_archive_symbols does not do
2605    this; it looks through the list of undefined symbols, and includes
2606    any object file which defines them.  When this algorithm is used on
2607    UnixWare, it winds up pulling in libc.so.1 early and defining a
2608    bunch of symbols.  This means that some of the other objects in the
2609    archive are not included in the link, which is incorrect since they
2610    precede libc.so.1 in the archive.
2611
2612    Fortunately, ELF archive handling is simpler than that done by
2613    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
2614    oddities.  In ELF, if we find a symbol in the archive map, and the
2615    symbol is currently undefined, we know that we must pull in that
2616    object file.
2617
2618    Unfortunately, we do have to make multiple passes over the symbol
2619    table until nothing further is resolved.  */
2620
2621 bfd_boolean
2622 _bfd_elf_link_add_archive_symbols (bfd *abfd,
2623                                    struct bfd_link_info *info)
2624 {
2625   symindex c;
2626   bfd_boolean *defined = NULL;
2627   bfd_boolean *included = NULL;
2628   carsym *symdefs;
2629   bfd_boolean loop;
2630   bfd_size_type amt;
2631
2632   if (! bfd_has_map (abfd))
2633     {
2634       /* An empty archive is a special case.  */
2635       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
2636         return TRUE;
2637       bfd_set_error (bfd_error_no_armap);
2638       return FALSE;
2639     }
2640
2641   /* Keep track of all symbols we know to be already defined, and all
2642      files we know to be already included.  This is to speed up the
2643      second and subsequent passes.  */
2644   c = bfd_ardata (abfd)->symdef_count;
2645   if (c == 0)
2646     return TRUE;
2647   amt = c;
2648   amt *= sizeof (bfd_boolean);
2649   defined = bfd_zmalloc (amt);
2650   included = bfd_zmalloc (amt);
2651   if (defined == NULL || included == NULL)
2652     goto error_return;
2653
2654   symdefs = bfd_ardata (abfd)->symdefs;
2655
2656   do
2657     {
2658       file_ptr last;
2659       symindex i;
2660       carsym *symdef;
2661       carsym *symdefend;
2662
2663       loop = FALSE;
2664       last = -1;
2665
2666       symdef = symdefs;
2667       symdefend = symdef + c;
2668       for (i = 0; symdef < symdefend; symdef++, i++)
2669         {
2670           struct elf_link_hash_entry *h;
2671           bfd *element;
2672           struct bfd_link_hash_entry *undefs_tail;
2673           symindex mark;
2674
2675           if (defined[i] || included[i])
2676             continue;
2677           if (symdef->file_offset == last)
2678             {
2679               included[i] = TRUE;
2680               continue;
2681             }
2682
2683           h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
2684                                     FALSE, FALSE, FALSE);
2685
2686           if (h == NULL)
2687             {
2688               char *p, *copy;
2689               size_t len, first;
2690
2691               /* If this is a default version (the name contains @@),
2692                  look up the symbol again with only one `@' as well
2693                  as without the version.  The effect is that references
2694                  to the symbol with and without the version will be
2695                  matched by the default symbol in the archive.  */
2696
2697               p = strchr (symdef->name, ELF_VER_CHR);
2698               if (p == NULL || p[1] != ELF_VER_CHR)
2699                 continue;
2700
2701               /* First check with only one `@'.  */
2702               len = strlen (symdef->name);
2703               copy = bfd_alloc (abfd, len);
2704               if (copy == NULL)
2705                 goto error_return;
2706               first = p - symdef->name + 1;
2707               memcpy (copy, symdef->name, first);
2708               memcpy (copy + first, symdef->name + first + 1, len - first);
2709
2710               h = elf_link_hash_lookup (elf_hash_table (info), copy,
2711                                         FALSE, FALSE, FALSE);
2712
2713               if (h == NULL)
2714                 {
2715                   /* We also need to check references to the symbol
2716                      without the version.  */
2717
2718                   copy[first - 1] = '\0';
2719                   h = elf_link_hash_lookup (elf_hash_table (info),
2720                                             copy, FALSE, FALSE, FALSE);
2721                 }
2722
2723               bfd_release (abfd, copy);
2724             }
2725
2726           if (h == NULL)
2727             continue;
2728
2729           if (h->root.type == bfd_link_hash_common)
2730             {
2731               /* We currently have a common symbol.  The archive map contains
2732                  a reference to this symbol, so we may want to include it.  We
2733                  only want to include it however, if this archive element
2734                  contains a definition of the symbol, not just another common
2735                  declaration of it.
2736
2737                  Unfortunately some archivers (including GNU ar) will put
2738                  declarations of common symbols into their archive maps, as
2739                  well as real definitions, so we cannot just go by the archive
2740                  map alone.  Instead we must read in the element's symbol
2741                  table and check that to see what kind of symbol definition
2742                  this is.  */
2743               if (! elf_link_is_defined_archive_symbol (abfd, symdef))
2744                 continue;
2745             }
2746           else if (h->root.type != bfd_link_hash_undefined)
2747             {
2748               if (h->root.type != bfd_link_hash_undefweak)
2749                 defined[i] = TRUE;
2750               continue;
2751             }
2752
2753           /* We need to include this archive member.  */
2754           element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2755           if (element == NULL)
2756             goto error_return;
2757
2758           if (! bfd_check_format (element, bfd_object))
2759             goto error_return;
2760
2761           /* Doublecheck that we have not included this object
2762              already--it should be impossible, but there may be
2763              something wrong with the archive.  */
2764           if (element->archive_pass != 0)
2765             {
2766               bfd_set_error (bfd_error_bad_value);
2767               goto error_return;
2768             }
2769           element->archive_pass = 1;
2770
2771           undefs_tail = info->hash->undefs_tail;
2772
2773           if (! (*info->callbacks->add_archive_element) (info, element,
2774                                                          symdef->name))
2775             goto error_return;
2776           if (! bfd_link_add_symbols (element, info))
2777             goto error_return;
2778
2779           /* If there are any new undefined symbols, we need to make
2780              another pass through the archive in order to see whether
2781              they can be defined.  FIXME: This isn't perfect, because
2782              common symbols wind up on undefs_tail and because an
2783              undefined symbol which is defined later on in this pass
2784              does not require another pass.  This isn't a bug, but it
2785              does make the code less efficient than it could be.  */
2786           if (undefs_tail != info->hash->undefs_tail)
2787             loop = TRUE;
2788
2789           /* Look backward to mark all symbols from this object file
2790              which we have already seen in this pass.  */
2791           mark = i;
2792           do
2793             {
2794               included[mark] = TRUE;
2795               if (mark == 0)
2796                 break;
2797               --mark;
2798             }
2799           while (symdefs[mark].file_offset == symdef->file_offset);
2800
2801           /* We mark subsequent symbols from this object file as we go
2802              on through the loop.  */
2803           last = symdef->file_offset;
2804         }
2805     }
2806   while (loop);
2807
2808   free (defined);
2809   free (included);
2810
2811   return TRUE;
2812
2813  error_return:
2814   if (defined != NULL)
2815     free (defined);
2816   if (included != NULL)
2817     free (included);
2818   return FALSE;
2819 }
This page took 0.181436 seconds and 4 git commands to generate.