2005-12-20 Paolo Bonzini <bonzini@gnu.org>
[binutils.git] / bfd / elflink.c
blob127f002bedcc85b9980dba502e756d3016950aeb
1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
30 /* Define a symbol in a dynamic linkage section. */
32 struct elf_link_hash_entry *
33 _bfd_elf_define_linkage_sym (bfd *abfd,
34 struct bfd_link_info *info,
35 asection *sec,
36 const char *name)
38 struct elf_link_hash_entry *h;
39 struct bfd_link_hash_entry *bh;
40 const struct elf_backend_data *bed;
42 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
43 if (h != NULL)
45 /* Zap symbol defined in an as-needed lib that wasn't linked.
46 This is a symptom of a larger problem: Absolute symbols
47 defined in shared libraries can't be overridden, because we
48 lose the link to the bfd which is via the symbol section. */
49 h->root.type = bfd_link_hash_new;
52 bh = &h->root;
53 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
54 sec, 0, NULL, FALSE,
55 get_elf_backend_data (abfd)->collect,
56 &bh))
57 return NULL;
58 h = (struct elf_link_hash_entry *) bh;
59 h->def_regular = 1;
60 h->type = STT_OBJECT;
61 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
63 bed = get_elf_backend_data (abfd);
64 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
65 return h;
68 bfd_boolean
69 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
71 flagword flags;
72 asection *s;
73 struct elf_link_hash_entry *h;
74 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
75 int ptralign;
77 /* This function may be called more than once. */
78 s = bfd_get_section_by_name (abfd, ".got");
79 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
80 return TRUE;
82 switch (bed->s->arch_size)
84 case 32:
85 ptralign = 2;
86 break;
88 case 64:
89 ptralign = 3;
90 break;
92 default:
93 bfd_set_error (bfd_error_bad_value);
94 return FALSE;
97 flags = bed->dynamic_sec_flags;
99 s = bfd_make_section_with_flags (abfd, ".got", flags);
100 if (s == NULL
101 || !bfd_set_section_alignment (abfd, s, ptralign))
102 return FALSE;
104 if (bed->want_got_plt)
106 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
107 if (s == NULL
108 || !bfd_set_section_alignment (abfd, s, ptralign))
109 return FALSE;
112 if (bed->want_got_sym)
114 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
115 (or .got.plt) section. We don't do this in the linker script
116 because we don't want to define the symbol if we are not creating
117 a global offset table. */
118 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
119 elf_hash_table (info)->hgot = h;
120 if (h == NULL)
121 return FALSE;
124 /* The first bit of the global offset table is the header. */
125 s->size += bed->got_header_size;
127 return TRUE;
130 /* Create a strtab to hold the dynamic symbol names. */
131 static bfd_boolean
132 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
134 struct elf_link_hash_table *hash_table;
136 hash_table = elf_hash_table (info);
137 if (hash_table->dynobj == NULL)
138 hash_table->dynobj = abfd;
140 if (hash_table->dynstr == NULL)
142 hash_table->dynstr = _bfd_elf_strtab_init ();
143 if (hash_table->dynstr == NULL)
144 return FALSE;
146 return TRUE;
149 /* Create some sections which will be filled in with dynamic linking
150 information. ABFD is an input file which requires dynamic sections
151 to be created. The dynamic sections take up virtual memory space
152 when the final executable is run, so we need to create them before
153 addresses are assigned to the output sections. We work out the
154 actual contents and size of these sections later. */
156 bfd_boolean
157 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
159 flagword flags;
160 register asection *s;
161 const struct elf_backend_data *bed;
163 if (! is_elf_hash_table (info->hash))
164 return FALSE;
166 if (elf_hash_table (info)->dynamic_sections_created)
167 return TRUE;
169 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
170 return FALSE;
172 abfd = elf_hash_table (info)->dynobj;
173 bed = get_elf_backend_data (abfd);
175 flags = bed->dynamic_sec_flags;
177 /* A dynamically linked executable has a .interp section, but a
178 shared library does not. */
179 if (info->executable)
181 s = bfd_make_section_with_flags (abfd, ".interp",
182 flags | SEC_READONLY);
183 if (s == NULL)
184 return FALSE;
187 if (! info->traditional_format)
189 s = bfd_make_section_with_flags (abfd, ".eh_frame_hdr",
190 flags | SEC_READONLY);
191 if (s == NULL
192 || ! bfd_set_section_alignment (abfd, s, 2))
193 return FALSE;
194 elf_hash_table (info)->eh_info.hdr_sec = s;
197 /* Create sections to hold version informations. These are removed
198 if they are not needed. */
199 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
200 flags | SEC_READONLY);
201 if (s == NULL
202 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
203 return FALSE;
205 s = bfd_make_section_with_flags (abfd, ".gnu.version",
206 flags | SEC_READONLY);
207 if (s == NULL
208 || ! bfd_set_section_alignment (abfd, s, 1))
209 return FALSE;
211 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
212 flags | SEC_READONLY);
213 if (s == NULL
214 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
215 return FALSE;
217 s = bfd_make_section_with_flags (abfd, ".dynsym",
218 flags | SEC_READONLY);
219 if (s == NULL
220 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
221 return FALSE;
223 s = bfd_make_section_with_flags (abfd, ".dynstr",
224 flags | SEC_READONLY);
225 if (s == NULL)
226 return FALSE;
228 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
229 if (s == NULL
230 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
231 return FALSE;
233 /* The special symbol _DYNAMIC is always set to the start of the
234 .dynamic section. We could set _DYNAMIC in a linker script, but we
235 only want to define it if we are, in fact, creating a .dynamic
236 section. We don't want to define it if there is no .dynamic
237 section, since on some ELF platforms the start up code examines it
238 to decide how to initialize the process. */
239 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
240 return FALSE;
242 s = bfd_make_section_with_flags (abfd, ".hash",
243 flags | SEC_READONLY);
244 if (s == NULL
245 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
246 return FALSE;
247 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
249 /* Let the backend create the rest of the sections. This lets the
250 backend set the right flags. The backend will normally create
251 the .got and .plt sections. */
252 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
253 return FALSE;
255 elf_hash_table (info)->dynamic_sections_created = TRUE;
257 return TRUE;
260 /* Create dynamic sections when linking against a dynamic object. */
262 bfd_boolean
263 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
265 flagword flags, pltflags;
266 asection *s;
267 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
269 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
270 .rel[a].bss sections. */
271 flags = bed->dynamic_sec_flags;
273 pltflags = flags;
274 if (bed->plt_not_loaded)
275 /* We do not clear SEC_ALLOC here because we still want the OS to
276 allocate space for the section; it's just that there's nothing
277 to read in from the object file. */
278 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
279 else
280 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
281 if (bed->plt_readonly)
282 pltflags |= SEC_READONLY;
284 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
285 if (s == NULL
286 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
287 return FALSE;
289 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
290 .plt section. */
291 if (bed->want_plt_sym
292 && !_bfd_elf_define_linkage_sym (abfd, info, s,
293 "_PROCEDURE_LINKAGE_TABLE_"))
294 return FALSE;
296 s = bfd_make_section_with_flags (abfd,
297 (bed->default_use_rela_p
298 ? ".rela.plt" : ".rel.plt"),
299 flags | SEC_READONLY);
300 if (s == NULL
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 return FALSE;
304 if (! _bfd_elf_create_got_section (abfd, info))
305 return FALSE;
307 if (bed->want_dynbss)
309 /* The .dynbss section is a place to put symbols which are defined
310 by dynamic objects, are referenced by regular objects, and are
311 not functions. We must allocate space for them in the process
312 image and use a R_*_COPY reloc to tell the dynamic linker to
313 initialize them at run time. The linker script puts the .dynbss
314 section into the .bss section of the final image. */
315 s = bfd_make_section_with_flags (abfd, ".dynbss",
316 (SEC_ALLOC
317 | SEC_LINKER_CREATED));
318 if (s == NULL)
319 return FALSE;
321 /* The .rel[a].bss section holds copy relocs. This section is not
322 normally needed. We need to create it here, though, so that the
323 linker will map it to an output section. We can't just create it
324 only if we need it, because we will not know whether we need it
325 until we have seen all the input files, and the first time the
326 main linker code calls BFD after examining all the input files
327 (size_dynamic_sections) the input sections have already been
328 mapped to the output sections. If the section turns out not to
329 be needed, we can discard it later. We will never need this
330 section when generating a shared object, since they do not use
331 copy relocs. */
332 if (! info->shared)
334 s = bfd_make_section_with_flags (abfd,
335 (bed->default_use_rela_p
336 ? ".rela.bss" : ".rel.bss"),
337 flags | SEC_READONLY);
338 if (s == NULL
339 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
340 return FALSE;
344 return TRUE;
347 /* Record a new dynamic symbol. We record the dynamic symbols as we
348 read the input files, since we need to have a list of all of them
349 before we can determine the final sizes of the output sections.
350 Note that we may actually call this function even though we are not
351 going to output any dynamic symbols; in some cases we know that a
352 symbol should be in the dynamic symbol table, but only if there is
353 one. */
355 bfd_boolean
356 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
357 struct elf_link_hash_entry *h)
359 if (h->dynindx == -1)
361 struct elf_strtab_hash *dynstr;
362 char *p;
363 const char *name;
364 bfd_size_type indx;
366 /* XXX: The ABI draft says the linker must turn hidden and
367 internal symbols into STB_LOCAL symbols when producing the
368 DSO. However, if ld.so honors st_other in the dynamic table,
369 this would not be necessary. */
370 switch (ELF_ST_VISIBILITY (h->other))
372 case STV_INTERNAL:
373 case STV_HIDDEN:
374 if (h->root.type != bfd_link_hash_undefined
375 && h->root.type != bfd_link_hash_undefweak)
377 h->forced_local = 1;
378 if (!elf_hash_table (info)->is_relocatable_executable)
379 return TRUE;
382 default:
383 break;
386 h->dynindx = elf_hash_table (info)->dynsymcount;
387 ++elf_hash_table (info)->dynsymcount;
389 dynstr = elf_hash_table (info)->dynstr;
390 if (dynstr == NULL)
392 /* Create a strtab to hold the dynamic symbol names. */
393 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
394 if (dynstr == NULL)
395 return FALSE;
398 /* We don't put any version information in the dynamic string
399 table. */
400 name = h->root.root.string;
401 p = strchr (name, ELF_VER_CHR);
402 if (p != NULL)
403 /* We know that the p points into writable memory. In fact,
404 there are only a few symbols that have read-only names, being
405 those like _GLOBAL_OFFSET_TABLE_ that are created specially
406 by the backends. Most symbols will have names pointing into
407 an ELF string table read from a file, or to objalloc memory. */
408 *p = 0;
410 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
412 if (p != NULL)
413 *p = ELF_VER_CHR;
415 if (indx == (bfd_size_type) -1)
416 return FALSE;
417 h->dynstr_index = indx;
420 return TRUE;
423 /* Record an assignment to a symbol made by a linker script. We need
424 this in case some dynamic object refers to this symbol. */
426 bfd_boolean
427 bfd_elf_record_link_assignment (bfd *output_bfd,
428 struct bfd_link_info *info,
429 const char *name,
430 bfd_boolean provide,
431 bfd_boolean hidden)
433 struct elf_link_hash_entry *h;
434 struct elf_link_hash_table *htab;
436 if (!is_elf_hash_table (info->hash))
437 return TRUE;
439 htab = elf_hash_table (info);
440 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
441 if (h == NULL)
442 return provide;
444 /* Since we're defining the symbol, don't let it seem to have not
445 been defined. record_dynamic_symbol and size_dynamic_sections
446 may depend on this. */
447 if (h->root.type == bfd_link_hash_undefweak
448 || h->root.type == bfd_link_hash_undefined)
450 h->root.type = bfd_link_hash_new;
451 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
452 bfd_link_repair_undef_list (&htab->root);
455 if (h->root.type == bfd_link_hash_new)
456 h->non_elf = 0;
458 /* If this symbol is being provided by the linker script, and it is
459 currently defined by a dynamic object, but not by a regular
460 object, then mark it as undefined so that the generic linker will
461 force the correct value. */
462 if (provide
463 && h->def_dynamic
464 && !h->def_regular)
465 h->root.type = bfd_link_hash_undefined;
467 /* If this symbol is not being provided by the linker script, and it is
468 currently defined by a dynamic object, but not by a regular object,
469 then clear out any version information because the symbol will not be
470 associated with the dynamic object any more. */
471 if (!provide
472 && h->def_dynamic
473 && !h->def_regular)
474 h->verinfo.verdef = NULL;
476 h->def_regular = 1;
478 if (provide && hidden)
480 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
482 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
483 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
486 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
487 and executables. */
488 if (!info->relocatable
489 && h->dynindx != -1
490 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
491 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
492 h->forced_local = 1;
494 if ((h->def_dynamic
495 || h->ref_dynamic
496 || info->shared
497 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
498 && h->dynindx == -1)
500 if (! bfd_elf_link_record_dynamic_symbol (info, h))
501 return FALSE;
503 /* If this is a weak defined symbol, and we know a corresponding
504 real symbol from the same dynamic object, make sure the real
505 symbol is also made into a dynamic symbol. */
506 if (h->u.weakdef != NULL
507 && h->u.weakdef->dynindx == -1)
509 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
510 return FALSE;
514 return TRUE;
517 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
518 success, and 2 on a failure caused by attempting to record a symbol
519 in a discarded section, eg. a discarded link-once section symbol. */
522 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
523 bfd *input_bfd,
524 long input_indx)
526 bfd_size_type amt;
527 struct elf_link_local_dynamic_entry *entry;
528 struct elf_link_hash_table *eht;
529 struct elf_strtab_hash *dynstr;
530 unsigned long dynstr_index;
531 char *name;
532 Elf_External_Sym_Shndx eshndx;
533 char esym[sizeof (Elf64_External_Sym)];
535 if (! is_elf_hash_table (info->hash))
536 return 0;
538 /* See if the entry exists already. */
539 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
540 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
541 return 1;
543 amt = sizeof (*entry);
544 entry = bfd_alloc (input_bfd, amt);
545 if (entry == NULL)
546 return 0;
548 /* Go find the symbol, so that we can find it's name. */
549 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
550 1, input_indx, &entry->isym, esym, &eshndx))
552 bfd_release (input_bfd, entry);
553 return 0;
556 if (entry->isym.st_shndx != SHN_UNDEF
557 && (entry->isym.st_shndx < SHN_LORESERVE
558 || entry->isym.st_shndx > SHN_HIRESERVE))
560 asection *s;
562 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
563 if (s == NULL || bfd_is_abs_section (s->output_section))
565 /* We can still bfd_release here as nothing has done another
566 bfd_alloc. We can't do this later in this function. */
567 bfd_release (input_bfd, entry);
568 return 2;
572 name = (bfd_elf_string_from_elf_section
573 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
574 entry->isym.st_name));
576 dynstr = elf_hash_table (info)->dynstr;
577 if (dynstr == NULL)
579 /* Create a strtab to hold the dynamic symbol names. */
580 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
581 if (dynstr == NULL)
582 return 0;
585 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
586 if (dynstr_index == (unsigned long) -1)
587 return 0;
588 entry->isym.st_name = dynstr_index;
590 eht = elf_hash_table (info);
592 entry->next = eht->dynlocal;
593 eht->dynlocal = entry;
594 entry->input_bfd = input_bfd;
595 entry->input_indx = input_indx;
596 eht->dynsymcount++;
598 /* Whatever binding the symbol had before, it's now local. */
599 entry->isym.st_info
600 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
602 /* The dynindx will be set at the end of size_dynamic_sections. */
604 return 1;
607 /* Return the dynindex of a local dynamic symbol. */
609 long
610 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
611 bfd *input_bfd,
612 long input_indx)
614 struct elf_link_local_dynamic_entry *e;
616 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
617 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
618 return e->dynindx;
619 return -1;
622 /* This function is used to renumber the dynamic symbols, if some of
623 them are removed because they are marked as local. This is called
624 via elf_link_hash_traverse. */
626 static bfd_boolean
627 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
628 void *data)
630 size_t *count = data;
632 if (h->root.type == bfd_link_hash_warning)
633 h = (struct elf_link_hash_entry *) h->root.u.i.link;
635 if (h->forced_local)
636 return TRUE;
638 if (h->dynindx != -1)
639 h->dynindx = ++(*count);
641 return TRUE;
645 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
646 STB_LOCAL binding. */
648 static bfd_boolean
649 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
650 void *data)
652 size_t *count = data;
654 if (h->root.type == bfd_link_hash_warning)
655 h = (struct elf_link_hash_entry *) h->root.u.i.link;
657 if (!h->forced_local)
658 return TRUE;
660 if (h->dynindx != -1)
661 h->dynindx = ++(*count);
663 return TRUE;
666 /* Return true if the dynamic symbol for a given section should be
667 omitted when creating a shared library. */
668 bfd_boolean
669 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
670 struct bfd_link_info *info,
671 asection *p)
673 switch (elf_section_data (p)->this_hdr.sh_type)
675 case SHT_PROGBITS:
676 case SHT_NOBITS:
677 /* If sh_type is yet undecided, assume it could be
678 SHT_PROGBITS/SHT_NOBITS. */
679 case SHT_NULL:
680 if (strcmp (p->name, ".got") == 0
681 || strcmp (p->name, ".got.plt") == 0
682 || strcmp (p->name, ".plt") == 0)
684 asection *ip;
685 bfd *dynobj = elf_hash_table (info)->dynobj;
687 if (dynobj != NULL
688 && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL
689 && (ip->flags & SEC_LINKER_CREATED)
690 && ip->output_section == p)
691 return TRUE;
693 return FALSE;
695 /* There shouldn't be section relative relocations
696 against any other section. */
697 default:
698 return TRUE;
702 /* Assign dynsym indices. In a shared library we generate a section
703 symbol for each output section, which come first. Next come symbols
704 which have been forced to local binding. Then all of the back-end
705 allocated local dynamic syms, followed by the rest of the global
706 symbols. */
708 static unsigned long
709 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
710 struct bfd_link_info *info,
711 unsigned long *section_sym_count)
713 unsigned long dynsymcount = 0;
715 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
717 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
718 asection *p;
719 for (p = output_bfd->sections; p ; p = p->next)
720 if ((p->flags & SEC_EXCLUDE) == 0
721 && (p->flags & SEC_ALLOC) != 0
722 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
723 elf_section_data (p)->dynindx = ++dynsymcount;
725 *section_sym_count = dynsymcount;
727 elf_link_hash_traverse (elf_hash_table (info),
728 elf_link_renumber_local_hash_table_dynsyms,
729 &dynsymcount);
731 if (elf_hash_table (info)->dynlocal)
733 struct elf_link_local_dynamic_entry *p;
734 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
735 p->dynindx = ++dynsymcount;
738 elf_link_hash_traverse (elf_hash_table (info),
739 elf_link_renumber_hash_table_dynsyms,
740 &dynsymcount);
742 /* There is an unused NULL entry at the head of the table which
743 we must account for in our count. Unless there weren't any
744 symbols, which means we'll have no table at all. */
745 if (dynsymcount != 0)
746 ++dynsymcount;
748 elf_hash_table (info)->dynsymcount = dynsymcount;
749 return dynsymcount;
752 /* This function is called when we want to define a new symbol. It
753 handles the various cases which arise when we find a definition in
754 a dynamic object, or when there is already a definition in a
755 dynamic object. The new symbol is described by NAME, SYM, PSEC,
756 and PVALUE. We set SYM_HASH to the hash table entry. We set
757 OVERRIDE if the old symbol is overriding a new definition. We set
758 TYPE_CHANGE_OK if it is OK for the type to change. We set
759 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
760 change, we mean that we shouldn't warn if the type or size does
761 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
762 object is overridden by a regular object. */
764 bfd_boolean
765 _bfd_elf_merge_symbol (bfd *abfd,
766 struct bfd_link_info *info,
767 const char *name,
768 Elf_Internal_Sym *sym,
769 asection **psec,
770 bfd_vma *pvalue,
771 unsigned int *pold_alignment,
772 struct elf_link_hash_entry **sym_hash,
773 bfd_boolean *skip,
774 bfd_boolean *override,
775 bfd_boolean *type_change_ok,
776 bfd_boolean *size_change_ok)
778 asection *sec, *oldsec;
779 struct elf_link_hash_entry *h;
780 struct elf_link_hash_entry *flip;
781 int bind;
782 bfd *oldbfd;
783 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
784 bfd_boolean newweak, oldweak;
785 const struct elf_backend_data *bed;
787 *skip = FALSE;
788 *override = FALSE;
790 sec = *psec;
791 bind = ELF_ST_BIND (sym->st_info);
793 if (! bfd_is_und_section (sec))
794 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
795 else
796 h = ((struct elf_link_hash_entry *)
797 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
798 if (h == NULL)
799 return FALSE;
800 *sym_hash = h;
802 /* This code is for coping with dynamic objects, and is only useful
803 if we are doing an ELF link. */
804 if (info->hash->creator != abfd->xvec)
805 return TRUE;
807 /* For merging, we only care about real symbols. */
809 while (h->root.type == bfd_link_hash_indirect
810 || h->root.type == bfd_link_hash_warning)
811 h = (struct elf_link_hash_entry *) h->root.u.i.link;
813 /* If we just created the symbol, mark it as being an ELF symbol.
814 Other than that, there is nothing to do--there is no merge issue
815 with a newly defined symbol--so we just return. */
817 if (h->root.type == bfd_link_hash_new)
819 h->non_elf = 0;
820 return TRUE;
823 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
824 existing symbol. */
826 switch (h->root.type)
828 default:
829 oldbfd = NULL;
830 oldsec = NULL;
831 break;
833 case bfd_link_hash_undefined:
834 case bfd_link_hash_undefweak:
835 oldbfd = h->root.u.undef.abfd;
836 oldsec = NULL;
837 break;
839 case bfd_link_hash_defined:
840 case bfd_link_hash_defweak:
841 oldbfd = h->root.u.def.section->owner;
842 oldsec = h->root.u.def.section;
843 break;
845 case bfd_link_hash_common:
846 oldbfd = h->root.u.c.p->section->owner;
847 oldsec = h->root.u.c.p->section;
848 break;
851 /* In cases involving weak versioned symbols, we may wind up trying
852 to merge a symbol with itself. Catch that here, to avoid the
853 confusion that results if we try to override a symbol with
854 itself. The additional tests catch cases like
855 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
856 dynamic object, which we do want to handle here. */
857 if (abfd == oldbfd
858 && ((abfd->flags & DYNAMIC) == 0
859 || !h->def_regular))
860 return TRUE;
862 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
863 respectively, is from a dynamic object. */
865 newdyn = (abfd->flags & DYNAMIC) != 0;
867 olddyn = FALSE;
868 if (oldbfd != NULL)
869 olddyn = (oldbfd->flags & DYNAMIC) != 0;
870 else if (oldsec != NULL)
872 /* This handles the special SHN_MIPS_{TEXT,DATA} section
873 indices used by MIPS ELF. */
874 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
877 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
878 respectively, appear to be a definition rather than reference. */
880 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
882 olddef = (h->root.type != bfd_link_hash_undefined
883 && h->root.type != bfd_link_hash_undefweak
884 && h->root.type != bfd_link_hash_common);
886 /* Check TLS symbol. We don't check undefined symbol introduced by
887 "ld -u". */
888 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
889 && ELF_ST_TYPE (sym->st_info) != h->type
890 && oldbfd != NULL)
892 bfd *ntbfd, *tbfd;
893 bfd_boolean ntdef, tdef;
894 asection *ntsec, *tsec;
896 if (h->type == STT_TLS)
898 ntbfd = abfd;
899 ntsec = sec;
900 ntdef = newdef;
901 tbfd = oldbfd;
902 tsec = oldsec;
903 tdef = olddef;
905 else
907 ntbfd = oldbfd;
908 ntsec = oldsec;
909 ntdef = olddef;
910 tbfd = abfd;
911 tsec = sec;
912 tdef = newdef;
915 if (tdef && ntdef)
916 (*_bfd_error_handler)
917 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
918 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
919 else if (!tdef && !ntdef)
920 (*_bfd_error_handler)
921 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
922 tbfd, ntbfd, h->root.root.string);
923 else if (tdef)
924 (*_bfd_error_handler)
925 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
926 tbfd, tsec, ntbfd, h->root.root.string);
927 else
928 (*_bfd_error_handler)
929 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
930 tbfd, ntbfd, ntsec, h->root.root.string);
932 bfd_set_error (bfd_error_bad_value);
933 return FALSE;
936 /* We need to remember if a symbol has a definition in a dynamic
937 object or is weak in all dynamic objects. Internal and hidden
938 visibility will make it unavailable to dynamic objects. */
939 if (newdyn && !h->dynamic_def)
941 if (!bfd_is_und_section (sec))
942 h->dynamic_def = 1;
943 else
945 /* Check if this symbol is weak in all dynamic objects. If it
946 is the first time we see it in a dynamic object, we mark
947 if it is weak. Otherwise, we clear it. */
948 if (!h->ref_dynamic)
950 if (bind == STB_WEAK)
951 h->dynamic_weak = 1;
953 else if (bind != STB_WEAK)
954 h->dynamic_weak = 0;
958 /* If the old symbol has non-default visibility, we ignore the new
959 definition from a dynamic object. */
960 if (newdyn
961 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
962 && !bfd_is_und_section (sec))
964 *skip = TRUE;
965 /* Make sure this symbol is dynamic. */
966 h->ref_dynamic = 1;
967 /* A protected symbol has external availability. Make sure it is
968 recorded as dynamic.
970 FIXME: Should we check type and size for protected symbol? */
971 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
972 return bfd_elf_link_record_dynamic_symbol (info, h);
973 else
974 return TRUE;
976 else if (!newdyn
977 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
978 && h->def_dynamic)
980 /* If the new symbol with non-default visibility comes from a
981 relocatable file and the old definition comes from a dynamic
982 object, we remove the old definition. */
983 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
984 h = *sym_hash;
986 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
987 && bfd_is_und_section (sec))
989 /* If the new symbol is undefined and the old symbol was
990 also undefined before, we need to make sure
991 _bfd_generic_link_add_one_symbol doesn't mess
992 up the linker hash table undefs list. Since the old
993 definition came from a dynamic object, it is still on the
994 undefs list. */
995 h->root.type = bfd_link_hash_undefined;
996 h->root.u.undef.abfd = abfd;
998 else
1000 h->root.type = bfd_link_hash_new;
1001 h->root.u.undef.abfd = NULL;
1004 if (h->def_dynamic)
1006 h->def_dynamic = 0;
1007 h->ref_dynamic = 1;
1008 h->dynamic_def = 1;
1010 /* FIXME: Should we check type and size for protected symbol? */
1011 h->size = 0;
1012 h->type = 0;
1013 return TRUE;
1016 /* Differentiate strong and weak symbols. */
1017 newweak = bind == STB_WEAK;
1018 oldweak = (h->root.type == bfd_link_hash_defweak
1019 || h->root.type == bfd_link_hash_undefweak);
1021 /* If a new weak symbol definition comes from a regular file and the
1022 old symbol comes from a dynamic library, we treat the new one as
1023 strong. Similarly, an old weak symbol definition from a regular
1024 file is treated as strong when the new symbol comes from a dynamic
1025 library. Further, an old weak symbol from a dynamic library is
1026 treated as strong if the new symbol is from a dynamic library.
1027 This reflects the way glibc's ld.so works.
1029 Do this before setting *type_change_ok or *size_change_ok so that
1030 we warn properly when dynamic library symbols are overridden. */
1032 if (newdef && !newdyn && olddyn)
1033 newweak = FALSE;
1034 if (olddef && newdyn)
1035 oldweak = FALSE;
1037 /* It's OK to change the type if either the existing symbol or the
1038 new symbol is weak. A type change is also OK if the old symbol
1039 is undefined and the new symbol is defined. */
1041 if (oldweak
1042 || newweak
1043 || (newdef
1044 && h->root.type == bfd_link_hash_undefined))
1045 *type_change_ok = TRUE;
1047 /* It's OK to change the size if either the existing symbol or the
1048 new symbol is weak, or if the old symbol is undefined. */
1050 if (*type_change_ok
1051 || h->root.type == bfd_link_hash_undefined)
1052 *size_change_ok = TRUE;
1054 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1055 symbol, respectively, appears to be a common symbol in a dynamic
1056 object. If a symbol appears in an uninitialized section, and is
1057 not weak, and is not a function, then it may be a common symbol
1058 which was resolved when the dynamic object was created. We want
1059 to treat such symbols specially, because they raise special
1060 considerations when setting the symbol size: if the symbol
1061 appears as a common symbol in a regular object, and the size in
1062 the regular object is larger, we must make sure that we use the
1063 larger size. This problematic case can always be avoided in C,
1064 but it must be handled correctly when using Fortran shared
1065 libraries.
1067 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1068 likewise for OLDDYNCOMMON and OLDDEF.
1070 Note that this test is just a heuristic, and that it is quite
1071 possible to have an uninitialized symbol in a shared object which
1072 is really a definition, rather than a common symbol. This could
1073 lead to some minor confusion when the symbol really is a common
1074 symbol in some regular object. However, I think it will be
1075 harmless. */
1077 if (newdyn
1078 && newdef
1079 && !newweak
1080 && (sec->flags & SEC_ALLOC) != 0
1081 && (sec->flags & SEC_LOAD) == 0
1082 && sym->st_size > 0
1083 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1084 newdyncommon = TRUE;
1085 else
1086 newdyncommon = FALSE;
1088 if (olddyn
1089 && olddef
1090 && h->root.type == bfd_link_hash_defined
1091 && h->def_dynamic
1092 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1093 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1094 && h->size > 0
1095 && h->type != STT_FUNC)
1096 olddyncommon = TRUE;
1097 else
1098 olddyncommon = FALSE;
1100 /* We now know everything about the old and new symbols. We ask the
1101 backend to check if we can merge them. */
1102 bed = get_elf_backend_data (abfd);
1103 if (bed->merge_symbol
1104 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1105 pold_alignment, skip, override,
1106 type_change_ok, size_change_ok,
1107 &newdyn, &newdef, &newdyncommon, &newweak,
1108 abfd, &sec,
1109 &olddyn, &olddef, &olddyncommon, &oldweak,
1110 oldbfd, &oldsec))
1111 return FALSE;
1113 /* If both the old and the new symbols look like common symbols in a
1114 dynamic object, set the size of the symbol to the larger of the
1115 two. */
1117 if (olddyncommon
1118 && newdyncommon
1119 && sym->st_size != h->size)
1121 /* Since we think we have two common symbols, issue a multiple
1122 common warning if desired. Note that we only warn if the
1123 size is different. If the size is the same, we simply let
1124 the old symbol override the new one as normally happens with
1125 symbols defined in dynamic objects. */
1127 if (! ((*info->callbacks->multiple_common)
1128 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1129 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1130 return FALSE;
1132 if (sym->st_size > h->size)
1133 h->size = sym->st_size;
1135 *size_change_ok = TRUE;
1138 /* If we are looking at a dynamic object, and we have found a
1139 definition, we need to see if the symbol was already defined by
1140 some other object. If so, we want to use the existing
1141 definition, and we do not want to report a multiple symbol
1142 definition error; we do this by clobbering *PSEC to be
1143 bfd_und_section_ptr.
1145 We treat a common symbol as a definition if the symbol in the
1146 shared library is a function, since common symbols always
1147 represent variables; this can cause confusion in principle, but
1148 any such confusion would seem to indicate an erroneous program or
1149 shared library. We also permit a common symbol in a regular
1150 object to override a weak symbol in a shared object. */
1152 if (newdyn
1153 && newdef
1154 && (olddef
1155 || (h->root.type == bfd_link_hash_common
1156 && (newweak
1157 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1159 *override = TRUE;
1160 newdef = FALSE;
1161 newdyncommon = FALSE;
1163 *psec = sec = bfd_und_section_ptr;
1164 *size_change_ok = TRUE;
1166 /* If we get here when the old symbol is a common symbol, then
1167 we are explicitly letting it override a weak symbol or
1168 function in a dynamic object, and we don't want to warn about
1169 a type change. If the old symbol is a defined symbol, a type
1170 change warning may still be appropriate. */
1172 if (h->root.type == bfd_link_hash_common)
1173 *type_change_ok = TRUE;
1176 /* Handle the special case of an old common symbol merging with a
1177 new symbol which looks like a common symbol in a shared object.
1178 We change *PSEC and *PVALUE to make the new symbol look like a
1179 common symbol, and let _bfd_generic_link_add_one_symbol do the
1180 right thing. */
1182 if (newdyncommon
1183 && h->root.type == bfd_link_hash_common)
1185 *override = TRUE;
1186 newdef = FALSE;
1187 newdyncommon = FALSE;
1188 *pvalue = sym->st_size;
1189 *psec = sec = bed->common_section (oldsec);
1190 *size_change_ok = TRUE;
1193 /* Skip weak definitions of symbols that are already defined. */
1194 if (newdef && olddef && newweak)
1195 *skip = TRUE;
1197 /* If the old symbol is from a dynamic object, and the new symbol is
1198 a definition which is not from a dynamic object, then the new
1199 symbol overrides the old symbol. Symbols from regular files
1200 always take precedence over symbols from dynamic objects, even if
1201 they are defined after the dynamic object in the link.
1203 As above, we again permit a common symbol in a regular object to
1204 override a definition in a shared object if the shared object
1205 symbol is a function or is weak. */
1207 flip = NULL;
1208 if (!newdyn
1209 && (newdef
1210 || (bfd_is_com_section (sec)
1211 && (oldweak
1212 || h->type == STT_FUNC)))
1213 && olddyn
1214 && olddef
1215 && h->def_dynamic)
1217 /* Change the hash table entry to undefined, and let
1218 _bfd_generic_link_add_one_symbol do the right thing with the
1219 new definition. */
1221 h->root.type = bfd_link_hash_undefined;
1222 h->root.u.undef.abfd = h->root.u.def.section->owner;
1223 *size_change_ok = TRUE;
1225 olddef = FALSE;
1226 olddyncommon = FALSE;
1228 /* We again permit a type change when a common symbol may be
1229 overriding a function. */
1231 if (bfd_is_com_section (sec))
1232 *type_change_ok = TRUE;
1234 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1235 flip = *sym_hash;
1236 else
1237 /* This union may have been set to be non-NULL when this symbol
1238 was seen in a dynamic object. We must force the union to be
1239 NULL, so that it is correct for a regular symbol. */
1240 h->verinfo.vertree = NULL;
1243 /* Handle the special case of a new common symbol merging with an
1244 old symbol that looks like it might be a common symbol defined in
1245 a shared object. Note that we have already handled the case in
1246 which a new common symbol should simply override the definition
1247 in the shared library. */
1249 if (! newdyn
1250 && bfd_is_com_section (sec)
1251 && olddyncommon)
1253 /* It would be best if we could set the hash table entry to a
1254 common symbol, but we don't know what to use for the section
1255 or the alignment. */
1256 if (! ((*info->callbacks->multiple_common)
1257 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1258 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1259 return FALSE;
1261 /* If the presumed common symbol in the dynamic object is
1262 larger, pretend that the new symbol has its size. */
1264 if (h->size > *pvalue)
1265 *pvalue = h->size;
1267 /* We need to remember the alignment required by the symbol
1268 in the dynamic object. */
1269 BFD_ASSERT (pold_alignment);
1270 *pold_alignment = h->root.u.def.section->alignment_power;
1272 olddef = FALSE;
1273 olddyncommon = FALSE;
1275 h->root.type = bfd_link_hash_undefined;
1276 h->root.u.undef.abfd = h->root.u.def.section->owner;
1278 *size_change_ok = TRUE;
1279 *type_change_ok = TRUE;
1281 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1282 flip = *sym_hash;
1283 else
1284 h->verinfo.vertree = NULL;
1287 if (flip != NULL)
1289 /* Handle the case where we had a versioned symbol in a dynamic
1290 library and now find a definition in a normal object. In this
1291 case, we make the versioned symbol point to the normal one. */
1292 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1293 flip->root.type = h->root.type;
1294 h->root.type = bfd_link_hash_indirect;
1295 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1296 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1297 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1298 if (h->def_dynamic)
1300 h->def_dynamic = 0;
1301 flip->ref_dynamic = 1;
1305 return TRUE;
1308 /* This function is called to create an indirect symbol from the
1309 default for the symbol with the default version if needed. The
1310 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1311 set DYNSYM if the new indirect symbol is dynamic. */
1313 bfd_boolean
1314 _bfd_elf_add_default_symbol (bfd *abfd,
1315 struct bfd_link_info *info,
1316 struct elf_link_hash_entry *h,
1317 const char *name,
1318 Elf_Internal_Sym *sym,
1319 asection **psec,
1320 bfd_vma *value,
1321 bfd_boolean *dynsym,
1322 bfd_boolean override)
1324 bfd_boolean type_change_ok;
1325 bfd_boolean size_change_ok;
1326 bfd_boolean skip;
1327 char *shortname;
1328 struct elf_link_hash_entry *hi;
1329 struct bfd_link_hash_entry *bh;
1330 const struct elf_backend_data *bed;
1331 bfd_boolean collect;
1332 bfd_boolean dynamic;
1333 char *p;
1334 size_t len, shortlen;
1335 asection *sec;
1337 /* If this symbol has a version, and it is the default version, we
1338 create an indirect symbol from the default name to the fully
1339 decorated name. This will cause external references which do not
1340 specify a version to be bound to this version of the symbol. */
1341 p = strchr (name, ELF_VER_CHR);
1342 if (p == NULL || p[1] != ELF_VER_CHR)
1343 return TRUE;
1345 if (override)
1347 /* We are overridden by an old definition. We need to check if we
1348 need to create the indirect symbol from the default name. */
1349 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1350 FALSE, FALSE);
1351 BFD_ASSERT (hi != NULL);
1352 if (hi == h)
1353 return TRUE;
1354 while (hi->root.type == bfd_link_hash_indirect
1355 || hi->root.type == bfd_link_hash_warning)
1357 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1358 if (hi == h)
1359 return TRUE;
1363 bed = get_elf_backend_data (abfd);
1364 collect = bed->collect;
1365 dynamic = (abfd->flags & DYNAMIC) != 0;
1367 shortlen = p - name;
1368 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1369 if (shortname == NULL)
1370 return FALSE;
1371 memcpy (shortname, name, shortlen);
1372 shortname[shortlen] = '\0';
1374 /* We are going to create a new symbol. Merge it with any existing
1375 symbol with this name. For the purposes of the merge, act as
1376 though we were defining the symbol we just defined, although we
1377 actually going to define an indirect symbol. */
1378 type_change_ok = FALSE;
1379 size_change_ok = FALSE;
1380 sec = *psec;
1381 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1382 NULL, &hi, &skip, &override,
1383 &type_change_ok, &size_change_ok))
1384 return FALSE;
1386 if (skip)
1387 goto nondefault;
1389 if (! override)
1391 bh = &hi->root;
1392 if (! (_bfd_generic_link_add_one_symbol
1393 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1394 0, name, FALSE, collect, &bh)))
1395 return FALSE;
1396 hi = (struct elf_link_hash_entry *) bh;
1398 else
1400 /* In this case the symbol named SHORTNAME is overriding the
1401 indirect symbol we want to add. We were planning on making
1402 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1403 is the name without a version. NAME is the fully versioned
1404 name, and it is the default version.
1406 Overriding means that we already saw a definition for the
1407 symbol SHORTNAME in a regular object, and it is overriding
1408 the symbol defined in the dynamic object.
1410 When this happens, we actually want to change NAME, the
1411 symbol we just added, to refer to SHORTNAME. This will cause
1412 references to NAME in the shared object to become references
1413 to SHORTNAME in the regular object. This is what we expect
1414 when we override a function in a shared object: that the
1415 references in the shared object will be mapped to the
1416 definition in the regular object. */
1418 while (hi->root.type == bfd_link_hash_indirect
1419 || hi->root.type == bfd_link_hash_warning)
1420 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1422 h->root.type = bfd_link_hash_indirect;
1423 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1424 if (h->def_dynamic)
1426 h->def_dynamic = 0;
1427 hi->ref_dynamic = 1;
1428 if (hi->ref_regular
1429 || hi->def_regular)
1431 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1432 return FALSE;
1436 /* Now set HI to H, so that the following code will set the
1437 other fields correctly. */
1438 hi = h;
1441 /* If there is a duplicate definition somewhere, then HI may not
1442 point to an indirect symbol. We will have reported an error to
1443 the user in that case. */
1445 if (hi->root.type == bfd_link_hash_indirect)
1447 struct elf_link_hash_entry *ht;
1449 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1450 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1452 /* See if the new flags lead us to realize that the symbol must
1453 be dynamic. */
1454 if (! *dynsym)
1456 if (! dynamic)
1458 if (info->shared
1459 || hi->ref_dynamic)
1460 *dynsym = TRUE;
1462 else
1464 if (hi->ref_regular)
1465 *dynsym = TRUE;
1470 /* We also need to define an indirection from the nondefault version
1471 of the symbol. */
1473 nondefault:
1474 len = strlen (name);
1475 shortname = bfd_hash_allocate (&info->hash->table, len);
1476 if (shortname == NULL)
1477 return FALSE;
1478 memcpy (shortname, name, shortlen);
1479 memcpy (shortname + shortlen, p + 1, len - shortlen);
1481 /* Once again, merge with any existing symbol. */
1482 type_change_ok = FALSE;
1483 size_change_ok = FALSE;
1484 sec = *psec;
1485 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1486 NULL, &hi, &skip, &override,
1487 &type_change_ok, &size_change_ok))
1488 return FALSE;
1490 if (skip)
1491 return TRUE;
1493 if (override)
1495 /* Here SHORTNAME is a versioned name, so we don't expect to see
1496 the type of override we do in the case above unless it is
1497 overridden by a versioned definition. */
1498 if (hi->root.type != bfd_link_hash_defined
1499 && hi->root.type != bfd_link_hash_defweak)
1500 (*_bfd_error_handler)
1501 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1502 abfd, shortname);
1504 else
1506 bh = &hi->root;
1507 if (! (_bfd_generic_link_add_one_symbol
1508 (info, abfd, shortname, BSF_INDIRECT,
1509 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1510 return FALSE;
1511 hi = (struct elf_link_hash_entry *) bh;
1513 /* If there is a duplicate definition somewhere, then HI may not
1514 point to an indirect symbol. We will have reported an error
1515 to the user in that case. */
1517 if (hi->root.type == bfd_link_hash_indirect)
1519 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1521 /* See if the new flags lead us to realize that the symbol
1522 must be dynamic. */
1523 if (! *dynsym)
1525 if (! dynamic)
1527 if (info->shared
1528 || hi->ref_dynamic)
1529 *dynsym = TRUE;
1531 else
1533 if (hi->ref_regular)
1534 *dynsym = TRUE;
1540 return TRUE;
1543 /* This routine is used to export all defined symbols into the dynamic
1544 symbol table. It is called via elf_link_hash_traverse. */
1546 bfd_boolean
1547 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1549 struct elf_info_failed *eif = data;
1551 /* Ignore indirect symbols. These are added by the versioning code. */
1552 if (h->root.type == bfd_link_hash_indirect)
1553 return TRUE;
1555 if (h->root.type == bfd_link_hash_warning)
1556 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1558 if (h->dynindx == -1
1559 && (h->def_regular
1560 || h->ref_regular))
1562 struct bfd_elf_version_tree *t;
1563 struct bfd_elf_version_expr *d;
1565 for (t = eif->verdefs; t != NULL; t = t->next)
1567 if (t->globals.list != NULL)
1569 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1570 if (d != NULL)
1571 goto doit;
1574 if (t->locals.list != NULL)
1576 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1577 if (d != NULL)
1578 return TRUE;
1582 if (!eif->verdefs)
1584 doit:
1585 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1587 eif->failed = TRUE;
1588 return FALSE;
1593 return TRUE;
1596 /* Look through the symbols which are defined in other shared
1597 libraries and referenced here. Update the list of version
1598 dependencies. This will be put into the .gnu.version_r section.
1599 This function is called via elf_link_hash_traverse. */
1601 bfd_boolean
1602 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1603 void *data)
1605 struct elf_find_verdep_info *rinfo = data;
1606 Elf_Internal_Verneed *t;
1607 Elf_Internal_Vernaux *a;
1608 bfd_size_type amt;
1610 if (h->root.type == bfd_link_hash_warning)
1611 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1613 /* We only care about symbols defined in shared objects with version
1614 information. */
1615 if (!h->def_dynamic
1616 || h->def_regular
1617 || h->dynindx == -1
1618 || h->verinfo.verdef == NULL)
1619 return TRUE;
1621 /* See if we already know about this version. */
1622 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1624 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1625 continue;
1627 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1628 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1629 return TRUE;
1631 break;
1634 /* This is a new version. Add it to tree we are building. */
1636 if (t == NULL)
1638 amt = sizeof *t;
1639 t = bfd_zalloc (rinfo->output_bfd, amt);
1640 if (t == NULL)
1642 rinfo->failed = TRUE;
1643 return FALSE;
1646 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1647 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1648 elf_tdata (rinfo->output_bfd)->verref = t;
1651 amt = sizeof *a;
1652 a = bfd_zalloc (rinfo->output_bfd, amt);
1654 /* Note that we are copying a string pointer here, and testing it
1655 above. If bfd_elf_string_from_elf_section is ever changed to
1656 discard the string data when low in memory, this will have to be
1657 fixed. */
1658 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1660 a->vna_flags = h->verinfo.verdef->vd_flags;
1661 a->vna_nextptr = t->vn_auxptr;
1663 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1664 ++rinfo->vers;
1666 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1668 t->vn_auxptr = a;
1670 return TRUE;
1673 /* Figure out appropriate versions for all the symbols. We may not
1674 have the version number script until we have read all of the input
1675 files, so until that point we don't know which symbols should be
1676 local. This function is called via elf_link_hash_traverse. */
1678 bfd_boolean
1679 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1681 struct elf_assign_sym_version_info *sinfo;
1682 struct bfd_link_info *info;
1683 const struct elf_backend_data *bed;
1684 struct elf_info_failed eif;
1685 char *p;
1686 bfd_size_type amt;
1688 sinfo = data;
1689 info = sinfo->info;
1691 if (h->root.type == bfd_link_hash_warning)
1692 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1694 /* Fix the symbol flags. */
1695 eif.failed = FALSE;
1696 eif.info = info;
1697 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1699 if (eif.failed)
1700 sinfo->failed = TRUE;
1701 return FALSE;
1704 /* We only need version numbers for symbols defined in regular
1705 objects. */
1706 if (!h->def_regular)
1707 return TRUE;
1709 bed = get_elf_backend_data (sinfo->output_bfd);
1710 p = strchr (h->root.root.string, ELF_VER_CHR);
1711 if (p != NULL && h->verinfo.vertree == NULL)
1713 struct bfd_elf_version_tree *t;
1714 bfd_boolean hidden;
1716 hidden = TRUE;
1718 /* There are two consecutive ELF_VER_CHR characters if this is
1719 not a hidden symbol. */
1720 ++p;
1721 if (*p == ELF_VER_CHR)
1723 hidden = FALSE;
1724 ++p;
1727 /* If there is no version string, we can just return out. */
1728 if (*p == '\0')
1730 if (hidden)
1731 h->hidden = 1;
1732 return TRUE;
1735 /* Look for the version. If we find it, it is no longer weak. */
1736 for (t = sinfo->verdefs; t != NULL; t = t->next)
1738 if (strcmp (t->name, p) == 0)
1740 size_t len;
1741 char *alc;
1742 struct bfd_elf_version_expr *d;
1744 len = p - h->root.root.string;
1745 alc = bfd_malloc (len);
1746 if (alc == NULL)
1747 return FALSE;
1748 memcpy (alc, h->root.root.string, len - 1);
1749 alc[len - 1] = '\0';
1750 if (alc[len - 2] == ELF_VER_CHR)
1751 alc[len - 2] = '\0';
1753 h->verinfo.vertree = t;
1754 t->used = TRUE;
1755 d = NULL;
1757 if (t->globals.list != NULL)
1758 d = (*t->match) (&t->globals, NULL, alc);
1760 /* See if there is anything to force this symbol to
1761 local scope. */
1762 if (d == NULL && t->locals.list != NULL)
1764 d = (*t->match) (&t->locals, NULL, alc);
1765 if (d != NULL
1766 && h->dynindx != -1
1767 && ! info->export_dynamic)
1768 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1771 free (alc);
1772 break;
1776 /* If we are building an application, we need to create a
1777 version node for this version. */
1778 if (t == NULL && info->executable)
1780 struct bfd_elf_version_tree **pp;
1781 int version_index;
1783 /* If we aren't going to export this symbol, we don't need
1784 to worry about it. */
1785 if (h->dynindx == -1)
1786 return TRUE;
1788 amt = sizeof *t;
1789 t = bfd_zalloc (sinfo->output_bfd, amt);
1790 if (t == NULL)
1792 sinfo->failed = TRUE;
1793 return FALSE;
1796 t->name = p;
1797 t->name_indx = (unsigned int) -1;
1798 t->used = TRUE;
1800 version_index = 1;
1801 /* Don't count anonymous version tag. */
1802 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1803 version_index = 0;
1804 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1805 ++version_index;
1806 t->vernum = version_index;
1808 *pp = t;
1810 h->verinfo.vertree = t;
1812 else if (t == NULL)
1814 /* We could not find the version for a symbol when
1815 generating a shared archive. Return an error. */
1816 (*_bfd_error_handler)
1817 (_("%B: undefined versioned symbol name %s"),
1818 sinfo->output_bfd, h->root.root.string);
1819 bfd_set_error (bfd_error_bad_value);
1820 sinfo->failed = TRUE;
1821 return FALSE;
1824 if (hidden)
1825 h->hidden = 1;
1828 /* If we don't have a version for this symbol, see if we can find
1829 something. */
1830 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1832 struct bfd_elf_version_tree *t;
1833 struct bfd_elf_version_tree *local_ver;
1834 struct bfd_elf_version_expr *d;
1836 /* See if can find what version this symbol is in. If the
1837 symbol is supposed to be local, then don't actually register
1838 it. */
1839 local_ver = NULL;
1840 for (t = sinfo->verdefs; t != NULL; t = t->next)
1842 if (t->globals.list != NULL)
1844 bfd_boolean matched;
1846 matched = FALSE;
1847 d = NULL;
1848 while ((d = (*t->match) (&t->globals, d,
1849 h->root.root.string)) != NULL)
1850 if (d->symver)
1851 matched = TRUE;
1852 else
1854 /* There is a version without definition. Make
1855 the symbol the default definition for this
1856 version. */
1857 h->verinfo.vertree = t;
1858 local_ver = NULL;
1859 d->script = 1;
1860 break;
1862 if (d != NULL)
1863 break;
1864 else if (matched)
1865 /* There is no undefined version for this symbol. Hide the
1866 default one. */
1867 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1870 if (t->locals.list != NULL)
1872 d = NULL;
1873 while ((d = (*t->match) (&t->locals, d,
1874 h->root.root.string)) != NULL)
1876 local_ver = t;
1877 /* If the match is "*", keep looking for a more
1878 explicit, perhaps even global, match.
1879 XXX: Shouldn't this be !d->wildcard instead? */
1880 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1881 break;
1884 if (d != NULL)
1885 break;
1889 if (local_ver != NULL)
1891 h->verinfo.vertree = local_ver;
1892 if (h->dynindx != -1
1893 && ! info->export_dynamic)
1895 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1900 return TRUE;
1903 /* Read and swap the relocs from the section indicated by SHDR. This
1904 may be either a REL or a RELA section. The relocations are
1905 translated into RELA relocations and stored in INTERNAL_RELOCS,
1906 which should have already been allocated to contain enough space.
1907 The EXTERNAL_RELOCS are a buffer where the external form of the
1908 relocations should be stored.
1910 Returns FALSE if something goes wrong. */
1912 static bfd_boolean
1913 elf_link_read_relocs_from_section (bfd *abfd,
1914 asection *sec,
1915 Elf_Internal_Shdr *shdr,
1916 void *external_relocs,
1917 Elf_Internal_Rela *internal_relocs)
1919 const struct elf_backend_data *bed;
1920 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1921 const bfd_byte *erela;
1922 const bfd_byte *erelaend;
1923 Elf_Internal_Rela *irela;
1924 Elf_Internal_Shdr *symtab_hdr;
1925 size_t nsyms;
1927 /* Position ourselves at the start of the section. */
1928 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1929 return FALSE;
1931 /* Read the relocations. */
1932 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1933 return FALSE;
1935 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1936 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1938 bed = get_elf_backend_data (abfd);
1940 /* Convert the external relocations to the internal format. */
1941 if (shdr->sh_entsize == bed->s->sizeof_rel)
1942 swap_in = bed->s->swap_reloc_in;
1943 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1944 swap_in = bed->s->swap_reloca_in;
1945 else
1947 bfd_set_error (bfd_error_wrong_format);
1948 return FALSE;
1951 erela = external_relocs;
1952 erelaend = erela + shdr->sh_size;
1953 irela = internal_relocs;
1954 while (erela < erelaend)
1956 bfd_vma r_symndx;
1958 (*swap_in) (abfd, erela, irela);
1959 r_symndx = ELF32_R_SYM (irela->r_info);
1960 if (bed->s->arch_size == 64)
1961 r_symndx >>= 24;
1962 if ((size_t) r_symndx >= nsyms)
1964 (*_bfd_error_handler)
1965 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1966 " for offset 0x%lx in section `%A'"),
1967 abfd, sec,
1968 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1969 bfd_set_error (bfd_error_bad_value);
1970 return FALSE;
1972 irela += bed->s->int_rels_per_ext_rel;
1973 erela += shdr->sh_entsize;
1976 return TRUE;
1979 /* Read and swap the relocs for a section O. They may have been
1980 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1981 not NULL, they are used as buffers to read into. They are known to
1982 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1983 the return value is allocated using either malloc or bfd_alloc,
1984 according to the KEEP_MEMORY argument. If O has two relocation
1985 sections (both REL and RELA relocations), then the REL_HDR
1986 relocations will appear first in INTERNAL_RELOCS, followed by the
1987 REL_HDR2 relocations. */
1989 Elf_Internal_Rela *
1990 _bfd_elf_link_read_relocs (bfd *abfd,
1991 asection *o,
1992 void *external_relocs,
1993 Elf_Internal_Rela *internal_relocs,
1994 bfd_boolean keep_memory)
1996 Elf_Internal_Shdr *rel_hdr;
1997 void *alloc1 = NULL;
1998 Elf_Internal_Rela *alloc2 = NULL;
1999 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2001 if (elf_section_data (o)->relocs != NULL)
2002 return elf_section_data (o)->relocs;
2004 if (o->reloc_count == 0)
2005 return NULL;
2007 rel_hdr = &elf_section_data (o)->rel_hdr;
2009 if (internal_relocs == NULL)
2011 bfd_size_type size;
2013 size = o->reloc_count;
2014 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2015 if (keep_memory)
2016 internal_relocs = bfd_alloc (abfd, size);
2017 else
2018 internal_relocs = alloc2 = bfd_malloc (size);
2019 if (internal_relocs == NULL)
2020 goto error_return;
2023 if (external_relocs == NULL)
2025 bfd_size_type size = rel_hdr->sh_size;
2027 if (elf_section_data (o)->rel_hdr2)
2028 size += elf_section_data (o)->rel_hdr2->sh_size;
2029 alloc1 = bfd_malloc (size);
2030 if (alloc1 == NULL)
2031 goto error_return;
2032 external_relocs = alloc1;
2035 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2036 external_relocs,
2037 internal_relocs))
2038 goto error_return;
2039 if (elf_section_data (o)->rel_hdr2
2040 && (!elf_link_read_relocs_from_section
2041 (abfd, o,
2042 elf_section_data (o)->rel_hdr2,
2043 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2044 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2045 * bed->s->int_rels_per_ext_rel))))
2046 goto error_return;
2048 /* Cache the results for next time, if we can. */
2049 if (keep_memory)
2050 elf_section_data (o)->relocs = internal_relocs;
2052 if (alloc1 != NULL)
2053 free (alloc1);
2055 /* Don't free alloc2, since if it was allocated we are passing it
2056 back (under the name of internal_relocs). */
2058 return internal_relocs;
2060 error_return:
2061 if (alloc1 != NULL)
2062 free (alloc1);
2063 if (alloc2 != NULL)
2064 free (alloc2);
2065 return NULL;
2068 /* Compute the size of, and allocate space for, REL_HDR which is the
2069 section header for a section containing relocations for O. */
2071 bfd_boolean
2072 _bfd_elf_link_size_reloc_section (bfd *abfd,
2073 Elf_Internal_Shdr *rel_hdr,
2074 asection *o)
2076 bfd_size_type reloc_count;
2077 bfd_size_type num_rel_hashes;
2079 /* Figure out how many relocations there will be. */
2080 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2081 reloc_count = elf_section_data (o)->rel_count;
2082 else
2083 reloc_count = elf_section_data (o)->rel_count2;
2085 num_rel_hashes = o->reloc_count;
2086 if (num_rel_hashes < reloc_count)
2087 num_rel_hashes = reloc_count;
2089 /* That allows us to calculate the size of the section. */
2090 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2092 /* The contents field must last into write_object_contents, so we
2093 allocate it with bfd_alloc rather than malloc. Also since we
2094 cannot be sure that the contents will actually be filled in,
2095 we zero the allocated space. */
2096 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2097 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2098 return FALSE;
2100 /* We only allocate one set of hash entries, so we only do it the
2101 first time we are called. */
2102 if (elf_section_data (o)->rel_hashes == NULL
2103 && num_rel_hashes)
2105 struct elf_link_hash_entry **p;
2107 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2108 if (p == NULL)
2109 return FALSE;
2111 elf_section_data (o)->rel_hashes = p;
2114 return TRUE;
2117 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2118 originated from the section given by INPUT_REL_HDR) to the
2119 OUTPUT_BFD. */
2121 bfd_boolean
2122 _bfd_elf_link_output_relocs (bfd *output_bfd,
2123 asection *input_section,
2124 Elf_Internal_Shdr *input_rel_hdr,
2125 Elf_Internal_Rela *internal_relocs,
2126 struct elf_link_hash_entry **rel_hash
2127 ATTRIBUTE_UNUSED)
2129 Elf_Internal_Rela *irela;
2130 Elf_Internal_Rela *irelaend;
2131 bfd_byte *erel;
2132 Elf_Internal_Shdr *output_rel_hdr;
2133 asection *output_section;
2134 unsigned int *rel_countp = NULL;
2135 const struct elf_backend_data *bed;
2136 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2138 output_section = input_section->output_section;
2139 output_rel_hdr = NULL;
2141 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2142 == input_rel_hdr->sh_entsize)
2144 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2145 rel_countp = &elf_section_data (output_section)->rel_count;
2147 else if (elf_section_data (output_section)->rel_hdr2
2148 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2149 == input_rel_hdr->sh_entsize))
2151 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2152 rel_countp = &elf_section_data (output_section)->rel_count2;
2154 else
2156 (*_bfd_error_handler)
2157 (_("%B: relocation size mismatch in %B section %A"),
2158 output_bfd, input_section->owner, input_section);
2159 bfd_set_error (bfd_error_wrong_object_format);
2160 return FALSE;
2163 bed = get_elf_backend_data (output_bfd);
2164 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2165 swap_out = bed->s->swap_reloc_out;
2166 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2167 swap_out = bed->s->swap_reloca_out;
2168 else
2169 abort ();
2171 erel = output_rel_hdr->contents;
2172 erel += *rel_countp * input_rel_hdr->sh_entsize;
2173 irela = internal_relocs;
2174 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2175 * bed->s->int_rels_per_ext_rel);
2176 while (irela < irelaend)
2178 (*swap_out) (output_bfd, irela, erel);
2179 irela += bed->s->int_rels_per_ext_rel;
2180 erel += input_rel_hdr->sh_entsize;
2183 /* Bump the counter, so that we know where to add the next set of
2184 relocations. */
2185 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2187 return TRUE;
2190 /* Fix up the flags for a symbol. This handles various cases which
2191 can only be fixed after all the input files are seen. This is
2192 currently called by both adjust_dynamic_symbol and
2193 assign_sym_version, which is unnecessary but perhaps more robust in
2194 the face of future changes. */
2196 bfd_boolean
2197 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2198 struct elf_info_failed *eif)
2200 /* If this symbol was mentioned in a non-ELF file, try to set
2201 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2202 permit a non-ELF file to correctly refer to a symbol defined in
2203 an ELF dynamic object. */
2204 if (h->non_elf)
2206 while (h->root.type == bfd_link_hash_indirect)
2207 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2209 if (h->root.type != bfd_link_hash_defined
2210 && h->root.type != bfd_link_hash_defweak)
2212 h->ref_regular = 1;
2213 h->ref_regular_nonweak = 1;
2215 else
2217 if (h->root.u.def.section->owner != NULL
2218 && (bfd_get_flavour (h->root.u.def.section->owner)
2219 == bfd_target_elf_flavour))
2221 h->ref_regular = 1;
2222 h->ref_regular_nonweak = 1;
2224 else
2225 h->def_regular = 1;
2228 if (h->dynindx == -1
2229 && (h->def_dynamic
2230 || h->ref_dynamic))
2232 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2234 eif->failed = TRUE;
2235 return FALSE;
2239 else
2241 /* Unfortunately, NON_ELF is only correct if the symbol
2242 was first seen in a non-ELF file. Fortunately, if the symbol
2243 was first seen in an ELF file, we're probably OK unless the
2244 symbol was defined in a non-ELF file. Catch that case here.
2245 FIXME: We're still in trouble if the symbol was first seen in
2246 a dynamic object, and then later in a non-ELF regular object. */
2247 if ((h->root.type == bfd_link_hash_defined
2248 || h->root.type == bfd_link_hash_defweak)
2249 && !h->def_regular
2250 && (h->root.u.def.section->owner != NULL
2251 ? (bfd_get_flavour (h->root.u.def.section->owner)
2252 != bfd_target_elf_flavour)
2253 : (bfd_is_abs_section (h->root.u.def.section)
2254 && !h->def_dynamic)))
2255 h->def_regular = 1;
2258 /* If this is a final link, and the symbol was defined as a common
2259 symbol in a regular object file, and there was no definition in
2260 any dynamic object, then the linker will have allocated space for
2261 the symbol in a common section but the DEF_REGULAR
2262 flag will not have been set. */
2263 if (h->root.type == bfd_link_hash_defined
2264 && !h->def_regular
2265 && h->ref_regular
2266 && !h->def_dynamic
2267 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2268 h->def_regular = 1;
2270 /* If -Bsymbolic was used (which means to bind references to global
2271 symbols to the definition within the shared object), and this
2272 symbol was defined in a regular object, then it actually doesn't
2273 need a PLT entry. Likewise, if the symbol has non-default
2274 visibility. If the symbol has hidden or internal visibility, we
2275 will force it local. */
2276 if (h->needs_plt
2277 && eif->info->shared
2278 && is_elf_hash_table (eif->info->hash)
2279 && (eif->info->symbolic
2280 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2281 && h->def_regular)
2283 const struct elf_backend_data *bed;
2284 bfd_boolean force_local;
2286 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2288 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2289 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2290 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2293 /* If a weak undefined symbol has non-default visibility, we also
2294 hide it from the dynamic linker. */
2295 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2296 && h->root.type == bfd_link_hash_undefweak)
2298 const struct elf_backend_data *bed;
2299 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2300 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2303 /* If this is a weak defined symbol in a dynamic object, and we know
2304 the real definition in the dynamic object, copy interesting flags
2305 over to the real definition. */
2306 if (h->u.weakdef != NULL)
2308 struct elf_link_hash_entry *weakdef;
2310 weakdef = h->u.weakdef;
2311 if (h->root.type == bfd_link_hash_indirect)
2312 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2314 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2315 || h->root.type == bfd_link_hash_defweak);
2316 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2317 || weakdef->root.type == bfd_link_hash_defweak);
2318 BFD_ASSERT (weakdef->def_dynamic);
2320 /* If the real definition is defined by a regular object file,
2321 don't do anything special. See the longer description in
2322 _bfd_elf_adjust_dynamic_symbol, below. */
2323 if (weakdef->def_regular)
2324 h->u.weakdef = NULL;
2325 else
2327 const struct elf_backend_data *bed;
2329 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2330 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2334 return TRUE;
2337 /* Make the backend pick a good value for a dynamic symbol. This is
2338 called via elf_link_hash_traverse, and also calls itself
2339 recursively. */
2341 bfd_boolean
2342 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2344 struct elf_info_failed *eif = data;
2345 bfd *dynobj;
2346 const struct elf_backend_data *bed;
2348 if (! is_elf_hash_table (eif->info->hash))
2349 return FALSE;
2351 if (h->root.type == bfd_link_hash_warning)
2353 h->got = elf_hash_table (eif->info)->init_got_offset;
2354 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2356 /* When warning symbols are created, they **replace** the "real"
2357 entry in the hash table, thus we never get to see the real
2358 symbol in a hash traversal. So look at it now. */
2359 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2362 /* Ignore indirect symbols. These are added by the versioning code. */
2363 if (h->root.type == bfd_link_hash_indirect)
2364 return TRUE;
2366 /* Fix the symbol flags. */
2367 if (! _bfd_elf_fix_symbol_flags (h, eif))
2368 return FALSE;
2370 /* If this symbol does not require a PLT entry, and it is not
2371 defined by a dynamic object, or is not referenced by a regular
2372 object, ignore it. We do have to handle a weak defined symbol,
2373 even if no regular object refers to it, if we decided to add it
2374 to the dynamic symbol table. FIXME: Do we normally need to worry
2375 about symbols which are defined by one dynamic object and
2376 referenced by another one? */
2377 if (!h->needs_plt
2378 && (h->def_regular
2379 || !h->def_dynamic
2380 || (!h->ref_regular
2381 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2383 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2384 return TRUE;
2387 /* If we've already adjusted this symbol, don't do it again. This
2388 can happen via a recursive call. */
2389 if (h->dynamic_adjusted)
2390 return TRUE;
2392 /* Don't look at this symbol again. Note that we must set this
2393 after checking the above conditions, because we may look at a
2394 symbol once, decide not to do anything, and then get called
2395 recursively later after REF_REGULAR is set below. */
2396 h->dynamic_adjusted = 1;
2398 /* If this is a weak definition, and we know a real definition, and
2399 the real symbol is not itself defined by a regular object file,
2400 then get a good value for the real definition. We handle the
2401 real symbol first, for the convenience of the backend routine.
2403 Note that there is a confusing case here. If the real definition
2404 is defined by a regular object file, we don't get the real symbol
2405 from the dynamic object, but we do get the weak symbol. If the
2406 processor backend uses a COPY reloc, then if some routine in the
2407 dynamic object changes the real symbol, we will not see that
2408 change in the corresponding weak symbol. This is the way other
2409 ELF linkers work as well, and seems to be a result of the shared
2410 library model.
2412 I will clarify this issue. Most SVR4 shared libraries define the
2413 variable _timezone and define timezone as a weak synonym. The
2414 tzset call changes _timezone. If you write
2415 extern int timezone;
2416 int _timezone = 5;
2417 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2418 you might expect that, since timezone is a synonym for _timezone,
2419 the same number will print both times. However, if the processor
2420 backend uses a COPY reloc, then actually timezone will be copied
2421 into your process image, and, since you define _timezone
2422 yourself, _timezone will not. Thus timezone and _timezone will
2423 wind up at different memory locations. The tzset call will set
2424 _timezone, leaving timezone unchanged. */
2426 if (h->u.weakdef != NULL)
2428 /* If we get to this point, we know there is an implicit
2429 reference by a regular object file via the weak symbol H.
2430 FIXME: Is this really true? What if the traversal finds
2431 H->U.WEAKDEF before it finds H? */
2432 h->u.weakdef->ref_regular = 1;
2434 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2435 return FALSE;
2438 /* If a symbol has no type and no size and does not require a PLT
2439 entry, then we are probably about to do the wrong thing here: we
2440 are probably going to create a COPY reloc for an empty object.
2441 This case can arise when a shared object is built with assembly
2442 code, and the assembly code fails to set the symbol type. */
2443 if (h->size == 0
2444 && h->type == STT_NOTYPE
2445 && !h->needs_plt)
2446 (*_bfd_error_handler)
2447 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2448 h->root.root.string);
2450 dynobj = elf_hash_table (eif->info)->dynobj;
2451 bed = get_elf_backend_data (dynobj);
2452 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2454 eif->failed = TRUE;
2455 return FALSE;
2458 return TRUE;
2461 /* Adjust all external symbols pointing into SEC_MERGE sections
2462 to reflect the object merging within the sections. */
2464 bfd_boolean
2465 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2467 asection *sec;
2469 if (h->root.type == bfd_link_hash_warning)
2470 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2472 if ((h->root.type == bfd_link_hash_defined
2473 || h->root.type == bfd_link_hash_defweak)
2474 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2475 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2477 bfd *output_bfd = data;
2479 h->root.u.def.value =
2480 _bfd_merged_section_offset (output_bfd,
2481 &h->root.u.def.section,
2482 elf_section_data (sec)->sec_info,
2483 h->root.u.def.value);
2486 return TRUE;
2489 /* Returns false if the symbol referred to by H should be considered
2490 to resolve local to the current module, and true if it should be
2491 considered to bind dynamically. */
2493 bfd_boolean
2494 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2495 struct bfd_link_info *info,
2496 bfd_boolean ignore_protected)
2498 bfd_boolean binding_stays_local_p;
2500 if (h == NULL)
2501 return FALSE;
2503 while (h->root.type == bfd_link_hash_indirect
2504 || h->root.type == bfd_link_hash_warning)
2505 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2507 /* If it was forced local, then clearly it's not dynamic. */
2508 if (h->dynindx == -1)
2509 return FALSE;
2510 if (h->forced_local)
2511 return FALSE;
2513 /* Identify the cases where name binding rules say that a
2514 visible symbol resolves locally. */
2515 binding_stays_local_p = info->executable || info->symbolic;
2517 switch (ELF_ST_VISIBILITY (h->other))
2519 case STV_INTERNAL:
2520 case STV_HIDDEN:
2521 return FALSE;
2523 case STV_PROTECTED:
2524 /* Proper resolution for function pointer equality may require
2525 that these symbols perhaps be resolved dynamically, even though
2526 we should be resolving them to the current module. */
2527 if (!ignore_protected || h->type != STT_FUNC)
2528 binding_stays_local_p = TRUE;
2529 break;
2531 default:
2532 break;
2535 /* If it isn't defined locally, then clearly it's dynamic. */
2536 if (!h->def_regular)
2537 return TRUE;
2539 /* Otherwise, the symbol is dynamic if binding rules don't tell
2540 us that it remains local. */
2541 return !binding_stays_local_p;
2544 /* Return true if the symbol referred to by H should be considered
2545 to resolve local to the current module, and false otherwise. Differs
2546 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2547 undefined symbols and weak symbols. */
2549 bfd_boolean
2550 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2551 struct bfd_link_info *info,
2552 bfd_boolean local_protected)
2554 /* If it's a local sym, of course we resolve locally. */
2555 if (h == NULL)
2556 return TRUE;
2558 /* Common symbols that become definitions don't get the DEF_REGULAR
2559 flag set, so test it first, and don't bail out. */
2560 if (ELF_COMMON_DEF_P (h))
2561 /* Do nothing. */;
2562 /* If we don't have a definition in a regular file, then we can't
2563 resolve locally. The sym is either undefined or dynamic. */
2564 else if (!h->def_regular)
2565 return FALSE;
2567 /* Forced local symbols resolve locally. */
2568 if (h->forced_local)
2569 return TRUE;
2571 /* As do non-dynamic symbols. */
2572 if (h->dynindx == -1)
2573 return TRUE;
2575 /* At this point, we know the symbol is defined and dynamic. In an
2576 executable it must resolve locally, likewise when building symbolic
2577 shared libraries. */
2578 if (info->executable || info->symbolic)
2579 return TRUE;
2581 /* Now deal with defined dynamic symbols in shared libraries. Ones
2582 with default visibility might not resolve locally. */
2583 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2584 return FALSE;
2586 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2587 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2588 return TRUE;
2590 /* STV_PROTECTED non-function symbols are local. */
2591 if (h->type != STT_FUNC)
2592 return TRUE;
2594 /* Function pointer equality tests may require that STV_PROTECTED
2595 symbols be treated as dynamic symbols, even when we know that the
2596 dynamic linker will resolve them locally. */
2597 return local_protected;
2600 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2601 aligned. Returns the first TLS output section. */
2603 struct bfd_section *
2604 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2606 struct bfd_section *sec, *tls;
2607 unsigned int align = 0;
2609 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2610 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2611 break;
2612 tls = sec;
2614 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2615 if (sec->alignment_power > align)
2616 align = sec->alignment_power;
2618 elf_hash_table (info)->tls_sec = tls;
2620 /* Ensure the alignment of the first section is the largest alignment,
2621 so that the tls segment starts aligned. */
2622 if (tls != NULL)
2623 tls->alignment_power = align;
2625 return tls;
2628 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2629 static bfd_boolean
2630 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2631 Elf_Internal_Sym *sym)
2633 const struct elf_backend_data *bed;
2635 /* Local symbols do not count, but target specific ones might. */
2636 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2637 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2638 return FALSE;
2640 /* Function symbols do not count. */
2641 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2642 return FALSE;
2644 /* If the section is undefined, then so is the symbol. */
2645 if (sym->st_shndx == SHN_UNDEF)
2646 return FALSE;
2648 /* If the symbol is defined in the common section, then
2649 it is a common definition and so does not count. */
2650 bed = get_elf_backend_data (abfd);
2651 if (bed->common_definition (sym))
2652 return FALSE;
2654 /* If the symbol is in a target specific section then we
2655 must rely upon the backend to tell us what it is. */
2656 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2657 /* FIXME - this function is not coded yet:
2659 return _bfd_is_global_symbol_definition (abfd, sym);
2661 Instead for now assume that the definition is not global,
2662 Even if this is wrong, at least the linker will behave
2663 in the same way that it used to do. */
2664 return FALSE;
2666 return TRUE;
2669 /* Search the symbol table of the archive element of the archive ABFD
2670 whose archive map contains a mention of SYMDEF, and determine if
2671 the symbol is defined in this element. */
2672 static bfd_boolean
2673 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2675 Elf_Internal_Shdr * hdr;
2676 bfd_size_type symcount;
2677 bfd_size_type extsymcount;
2678 bfd_size_type extsymoff;
2679 Elf_Internal_Sym *isymbuf;
2680 Elf_Internal_Sym *isym;
2681 Elf_Internal_Sym *isymend;
2682 bfd_boolean result;
2684 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2685 if (abfd == NULL)
2686 return FALSE;
2688 if (! bfd_check_format (abfd, bfd_object))
2689 return FALSE;
2691 /* If we have already included the element containing this symbol in the
2692 link then we do not need to include it again. Just claim that any symbol
2693 it contains is not a definition, so that our caller will not decide to
2694 (re)include this element. */
2695 if (abfd->archive_pass)
2696 return FALSE;
2698 /* Select the appropriate symbol table. */
2699 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2700 hdr = &elf_tdata (abfd)->symtab_hdr;
2701 else
2702 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2704 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2706 /* The sh_info field of the symtab header tells us where the
2707 external symbols start. We don't care about the local symbols. */
2708 if (elf_bad_symtab (abfd))
2710 extsymcount = symcount;
2711 extsymoff = 0;
2713 else
2715 extsymcount = symcount - hdr->sh_info;
2716 extsymoff = hdr->sh_info;
2719 if (extsymcount == 0)
2720 return FALSE;
2722 /* Read in the symbol table. */
2723 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2724 NULL, NULL, NULL);
2725 if (isymbuf == NULL)
2726 return FALSE;
2728 /* Scan the symbol table looking for SYMDEF. */
2729 result = FALSE;
2730 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2732 const char *name;
2734 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2735 isym->st_name);
2736 if (name == NULL)
2737 break;
2739 if (strcmp (name, symdef->name) == 0)
2741 result = is_global_data_symbol_definition (abfd, isym);
2742 break;
2746 free (isymbuf);
2748 return result;
2751 /* Add an entry to the .dynamic table. */
2753 bfd_boolean
2754 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2755 bfd_vma tag,
2756 bfd_vma val)
2758 struct elf_link_hash_table *hash_table;
2759 const struct elf_backend_data *bed;
2760 asection *s;
2761 bfd_size_type newsize;
2762 bfd_byte *newcontents;
2763 Elf_Internal_Dyn dyn;
2765 hash_table = elf_hash_table (info);
2766 if (! is_elf_hash_table (hash_table))
2767 return FALSE;
2769 if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL)
2770 _bfd_error_handler
2771 (_("warning: creating a DT_TEXTREL in a shared object."));
2773 bed = get_elf_backend_data (hash_table->dynobj);
2774 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2775 BFD_ASSERT (s != NULL);
2777 newsize = s->size + bed->s->sizeof_dyn;
2778 newcontents = bfd_realloc (s->contents, newsize);
2779 if (newcontents == NULL)
2780 return FALSE;
2782 dyn.d_tag = tag;
2783 dyn.d_un.d_val = val;
2784 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2786 s->size = newsize;
2787 s->contents = newcontents;
2789 return TRUE;
2792 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2793 otherwise just check whether one already exists. Returns -1 on error,
2794 1 if a DT_NEEDED tag already exists, and 0 on success. */
2796 static int
2797 elf_add_dt_needed_tag (bfd *abfd,
2798 struct bfd_link_info *info,
2799 const char *soname,
2800 bfd_boolean do_it)
2802 struct elf_link_hash_table *hash_table;
2803 bfd_size_type oldsize;
2804 bfd_size_type strindex;
2806 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
2807 return -1;
2809 hash_table = elf_hash_table (info);
2810 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2811 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2812 if (strindex == (bfd_size_type) -1)
2813 return -1;
2815 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2817 asection *sdyn;
2818 const struct elf_backend_data *bed;
2819 bfd_byte *extdyn;
2821 bed = get_elf_backend_data (hash_table->dynobj);
2822 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2823 if (sdyn != NULL)
2824 for (extdyn = sdyn->contents;
2825 extdyn < sdyn->contents + sdyn->size;
2826 extdyn += bed->s->sizeof_dyn)
2828 Elf_Internal_Dyn dyn;
2830 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2831 if (dyn.d_tag == DT_NEEDED
2832 && dyn.d_un.d_val == strindex)
2834 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2835 return 1;
2840 if (do_it)
2842 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
2843 return -1;
2845 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2846 return -1;
2848 else
2849 /* We were just checking for existence of the tag. */
2850 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2852 return 0;
2855 /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols
2856 belonging to NOT_NEEDED to bfd_link_hash_new. We know there are no
2857 references from regular objects to these symbols.
2859 ??? Should we do something about references from other dynamic
2860 obects? If not, we potentially lose some warnings about undefined
2861 symbols. But how can we recover the initial undefined / undefweak
2862 state? */
2864 struct elf_smash_syms_data
2866 bfd *not_needed;
2867 struct elf_link_hash_table *htab;
2868 bfd_boolean twiddled;
2871 static bfd_boolean
2872 elf_smash_syms (struct elf_link_hash_entry *h, void *data)
2874 struct elf_smash_syms_data *inf = (struct elf_smash_syms_data *) data;
2875 struct bfd_link_hash_entry *bh;
2877 switch (h->root.type)
2879 default:
2880 case bfd_link_hash_new:
2881 return TRUE;
2883 case bfd_link_hash_undefined:
2884 if (h->root.u.undef.abfd != inf->not_needed)
2885 return TRUE;
2886 if (h->root.u.undef.weak != NULL
2887 && h->root.u.undef.weak != inf->not_needed)
2889 /* Symbol was undefweak in u.undef.weak bfd, and has become
2890 undefined in as-needed lib. Restore weak. */
2891 h->root.type = bfd_link_hash_undefweak;
2892 h->root.u.undef.abfd = h->root.u.undef.weak;
2893 if (h->root.u.undef.next != NULL
2894 || inf->htab->root.undefs_tail == &h->root)
2895 inf->twiddled = TRUE;
2896 return TRUE;
2898 break;
2900 case bfd_link_hash_undefweak:
2901 if (h->root.u.undef.abfd != inf->not_needed)
2902 return TRUE;
2903 break;
2905 case bfd_link_hash_defined:
2906 case bfd_link_hash_defweak:
2907 if (h->root.u.def.section->owner != inf->not_needed)
2908 return TRUE;
2909 break;
2911 case bfd_link_hash_common:
2912 if (h->root.u.c.p->section->owner != inf->not_needed)
2913 return TRUE;
2914 break;
2916 case bfd_link_hash_warning:
2917 case bfd_link_hash_indirect:
2918 elf_smash_syms ((struct elf_link_hash_entry *) h->root.u.i.link, data);
2919 if (h->root.u.i.link->type != bfd_link_hash_new)
2920 return TRUE;
2921 if (h->root.u.i.link->u.undef.abfd != inf->not_needed)
2922 return TRUE;
2923 break;
2926 /* There is no way we can undo symbol table state from defined or
2927 defweak back to undefined. */
2928 if (h->ref_regular)
2929 abort ();
2931 /* Set sym back to newly created state, but keep undef.next if it is
2932 being used as a list pointer. */
2933 bh = h->root.u.undef.next;
2934 if (bh == &h->root)
2935 bh = NULL;
2936 if (bh != NULL || inf->htab->root.undefs_tail == &h->root)
2937 inf->twiddled = TRUE;
2938 (*inf->htab->root.table.newfunc) (&h->root.root,
2939 &inf->htab->root.table,
2940 h->root.root.string);
2941 h->root.u.undef.next = bh;
2942 h->root.u.undef.abfd = inf->not_needed;
2943 h->non_elf = 0;
2944 return TRUE;
2947 /* Sort symbol by value and section. */
2948 static int
2949 elf_sort_symbol (const void *arg1, const void *arg2)
2951 const struct elf_link_hash_entry *h1;
2952 const struct elf_link_hash_entry *h2;
2953 bfd_signed_vma vdiff;
2955 h1 = *(const struct elf_link_hash_entry **) arg1;
2956 h2 = *(const struct elf_link_hash_entry **) arg2;
2957 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2958 if (vdiff != 0)
2959 return vdiff > 0 ? 1 : -1;
2960 else
2962 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2963 if (sdiff != 0)
2964 return sdiff > 0 ? 1 : -1;
2966 return 0;
2969 /* This function is used to adjust offsets into .dynstr for
2970 dynamic symbols. This is called via elf_link_hash_traverse. */
2972 static bfd_boolean
2973 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2975 struct elf_strtab_hash *dynstr = data;
2977 if (h->root.type == bfd_link_hash_warning)
2978 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2980 if (h->dynindx != -1)
2981 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2982 return TRUE;
2985 /* Assign string offsets in .dynstr, update all structures referencing
2986 them. */
2988 static bfd_boolean
2989 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2991 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2992 struct elf_link_local_dynamic_entry *entry;
2993 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2994 bfd *dynobj = hash_table->dynobj;
2995 asection *sdyn;
2996 bfd_size_type size;
2997 const struct elf_backend_data *bed;
2998 bfd_byte *extdyn;
3000 _bfd_elf_strtab_finalize (dynstr);
3001 size = _bfd_elf_strtab_size (dynstr);
3003 bed = get_elf_backend_data (dynobj);
3004 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3005 BFD_ASSERT (sdyn != NULL);
3007 /* Update all .dynamic entries referencing .dynstr strings. */
3008 for (extdyn = sdyn->contents;
3009 extdyn < sdyn->contents + sdyn->size;
3010 extdyn += bed->s->sizeof_dyn)
3012 Elf_Internal_Dyn dyn;
3014 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3015 switch (dyn.d_tag)
3017 case DT_STRSZ:
3018 dyn.d_un.d_val = size;
3019 break;
3020 case DT_NEEDED:
3021 case DT_SONAME:
3022 case DT_RPATH:
3023 case DT_RUNPATH:
3024 case DT_FILTER:
3025 case DT_AUXILIARY:
3026 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3027 break;
3028 default:
3029 continue;
3031 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3034 /* Now update local dynamic symbols. */
3035 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3036 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3037 entry->isym.st_name);
3039 /* And the rest of dynamic symbols. */
3040 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3042 /* Adjust version definitions. */
3043 if (elf_tdata (output_bfd)->cverdefs)
3045 asection *s;
3046 bfd_byte *p;
3047 bfd_size_type i;
3048 Elf_Internal_Verdef def;
3049 Elf_Internal_Verdaux defaux;
3051 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3052 p = s->contents;
3055 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3056 &def);
3057 p += sizeof (Elf_External_Verdef);
3058 if (def.vd_aux != sizeof (Elf_External_Verdef))
3059 continue;
3060 for (i = 0; i < def.vd_cnt; ++i)
3062 _bfd_elf_swap_verdaux_in (output_bfd,
3063 (Elf_External_Verdaux *) p, &defaux);
3064 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3065 defaux.vda_name);
3066 _bfd_elf_swap_verdaux_out (output_bfd,
3067 &defaux, (Elf_External_Verdaux *) p);
3068 p += sizeof (Elf_External_Verdaux);
3071 while (def.vd_next);
3074 /* Adjust version references. */
3075 if (elf_tdata (output_bfd)->verref)
3077 asection *s;
3078 bfd_byte *p;
3079 bfd_size_type i;
3080 Elf_Internal_Verneed need;
3081 Elf_Internal_Vernaux needaux;
3083 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3084 p = s->contents;
3087 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3088 &need);
3089 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3090 _bfd_elf_swap_verneed_out (output_bfd, &need,
3091 (Elf_External_Verneed *) p);
3092 p += sizeof (Elf_External_Verneed);
3093 for (i = 0; i < need.vn_cnt; ++i)
3095 _bfd_elf_swap_vernaux_in (output_bfd,
3096 (Elf_External_Vernaux *) p, &needaux);
3097 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3098 needaux.vna_name);
3099 _bfd_elf_swap_vernaux_out (output_bfd,
3100 &needaux,
3101 (Elf_External_Vernaux *) p);
3102 p += sizeof (Elf_External_Vernaux);
3105 while (need.vn_next);
3108 return TRUE;
3111 /* Add symbols from an ELF object file to the linker hash table. */
3113 static bfd_boolean
3114 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3116 bfd_boolean (*add_symbol_hook)
3117 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
3118 const char **, flagword *, asection **, bfd_vma *);
3119 bfd_boolean (*check_relocs)
3120 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
3121 bfd_boolean (*check_directives)
3122 (bfd *, struct bfd_link_info *);
3123 bfd_boolean collect;
3124 Elf_Internal_Shdr *hdr;
3125 bfd_size_type symcount;
3126 bfd_size_type extsymcount;
3127 bfd_size_type extsymoff;
3128 struct elf_link_hash_entry **sym_hash;
3129 bfd_boolean dynamic;
3130 Elf_External_Versym *extversym = NULL;
3131 Elf_External_Versym *ever;
3132 struct elf_link_hash_entry *weaks;
3133 struct elf_link_hash_entry **nondeflt_vers = NULL;
3134 bfd_size_type nondeflt_vers_cnt = 0;
3135 Elf_Internal_Sym *isymbuf = NULL;
3136 Elf_Internal_Sym *isym;
3137 Elf_Internal_Sym *isymend;
3138 const struct elf_backend_data *bed;
3139 bfd_boolean add_needed;
3140 struct elf_link_hash_table * hash_table;
3141 bfd_size_type amt;
3143 hash_table = elf_hash_table (info);
3145 bed = get_elf_backend_data (abfd);
3146 add_symbol_hook = bed->elf_add_symbol_hook;
3147 collect = bed->collect;
3149 if ((abfd->flags & DYNAMIC) == 0)
3150 dynamic = FALSE;
3151 else
3153 dynamic = TRUE;
3155 /* You can't use -r against a dynamic object. Also, there's no
3156 hope of using a dynamic object which does not exactly match
3157 the format of the output file. */
3158 if (info->relocatable
3159 || !is_elf_hash_table (hash_table)
3160 || hash_table->root.creator != abfd->xvec)
3162 if (info->relocatable)
3163 bfd_set_error (bfd_error_invalid_operation);
3164 else
3165 bfd_set_error (bfd_error_wrong_format);
3166 goto error_return;
3170 /* As a GNU extension, any input sections which are named
3171 .gnu.warning.SYMBOL are treated as warning symbols for the given
3172 symbol. This differs from .gnu.warning sections, which generate
3173 warnings when they are included in an output file. */
3174 if (info->executable)
3176 asection *s;
3178 for (s = abfd->sections; s != NULL; s = s->next)
3180 const char *name;
3182 name = bfd_get_section_name (abfd, s);
3183 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3185 char *msg;
3186 bfd_size_type sz;
3188 name += sizeof ".gnu.warning." - 1;
3190 /* If this is a shared object, then look up the symbol
3191 in the hash table. If it is there, and it is already
3192 been defined, then we will not be using the entry
3193 from this shared object, so we don't need to warn.
3194 FIXME: If we see the definition in a regular object
3195 later on, we will warn, but we shouldn't. The only
3196 fix is to keep track of what warnings we are supposed
3197 to emit, and then handle them all at the end of the
3198 link. */
3199 if (dynamic)
3201 struct elf_link_hash_entry *h;
3203 h = elf_link_hash_lookup (hash_table, name,
3204 FALSE, FALSE, TRUE);
3206 /* FIXME: What about bfd_link_hash_common? */
3207 if (h != NULL
3208 && (h->root.type == bfd_link_hash_defined
3209 || h->root.type == bfd_link_hash_defweak))
3211 /* We don't want to issue this warning. Clobber
3212 the section size so that the warning does not
3213 get copied into the output file. */
3214 s->size = 0;
3215 continue;
3219 sz = s->size;
3220 msg = bfd_alloc (abfd, sz + 1);
3221 if (msg == NULL)
3222 goto error_return;
3224 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3225 goto error_return;
3227 msg[sz] = '\0';
3229 if (! (_bfd_generic_link_add_one_symbol
3230 (info, abfd, name, BSF_WARNING, s, 0, msg,
3231 FALSE, collect, NULL)))
3232 goto error_return;
3234 if (! info->relocatable)
3236 /* Clobber the section size so that the warning does
3237 not get copied into the output file. */
3238 s->size = 0;
3240 /* Also set SEC_EXCLUDE, so that symbols defined in
3241 the warning section don't get copied to the output. */
3242 s->flags |= SEC_EXCLUDE;
3248 add_needed = TRUE;
3249 if (! dynamic)
3251 /* If we are creating a shared library, create all the dynamic
3252 sections immediately. We need to attach them to something,
3253 so we attach them to this BFD, provided it is the right
3254 format. FIXME: If there are no input BFD's of the same
3255 format as the output, we can't make a shared library. */
3256 if (info->shared
3257 && is_elf_hash_table (hash_table)
3258 && hash_table->root.creator == abfd->xvec
3259 && ! hash_table->dynamic_sections_created)
3261 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3262 goto error_return;
3265 else if (!is_elf_hash_table (hash_table))
3266 goto error_return;
3267 else
3269 asection *s;
3270 const char *soname = NULL;
3271 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3272 int ret;
3274 /* ld --just-symbols and dynamic objects don't mix very well.
3275 ld shouldn't allow it. */
3276 if ((s = abfd->sections) != NULL
3277 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3278 abort ();
3280 /* If this dynamic lib was specified on the command line with
3281 --as-needed in effect, then we don't want to add a DT_NEEDED
3282 tag unless the lib is actually used. Similary for libs brought
3283 in by another lib's DT_NEEDED. When --no-add-needed is used
3284 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3285 any dynamic library in DT_NEEDED tags in the dynamic lib at
3286 all. */
3287 add_needed = (elf_dyn_lib_class (abfd)
3288 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3289 | DYN_NO_NEEDED)) == 0;
3291 s = bfd_get_section_by_name (abfd, ".dynamic");
3292 if (s != NULL)
3294 bfd_byte *dynbuf;
3295 bfd_byte *extdyn;
3296 int elfsec;
3297 unsigned long shlink;
3299 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3300 goto error_free_dyn;
3302 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3303 if (elfsec == -1)
3304 goto error_free_dyn;
3305 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3307 for (extdyn = dynbuf;
3308 extdyn < dynbuf + s->size;
3309 extdyn += bed->s->sizeof_dyn)
3311 Elf_Internal_Dyn dyn;
3313 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3314 if (dyn.d_tag == DT_SONAME)
3316 unsigned int tagv = dyn.d_un.d_val;
3317 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3318 if (soname == NULL)
3319 goto error_free_dyn;
3321 if (dyn.d_tag == DT_NEEDED)
3323 struct bfd_link_needed_list *n, **pn;
3324 char *fnm, *anm;
3325 unsigned int tagv = dyn.d_un.d_val;
3327 amt = sizeof (struct bfd_link_needed_list);
3328 n = bfd_alloc (abfd, amt);
3329 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3330 if (n == NULL || fnm == NULL)
3331 goto error_free_dyn;
3332 amt = strlen (fnm) + 1;
3333 anm = bfd_alloc (abfd, amt);
3334 if (anm == NULL)
3335 goto error_free_dyn;
3336 memcpy (anm, fnm, amt);
3337 n->name = anm;
3338 n->by = abfd;
3339 n->next = NULL;
3340 for (pn = & hash_table->needed;
3341 *pn != NULL;
3342 pn = &(*pn)->next)
3344 *pn = n;
3346 if (dyn.d_tag == DT_RUNPATH)
3348 struct bfd_link_needed_list *n, **pn;
3349 char *fnm, *anm;
3350 unsigned int tagv = dyn.d_un.d_val;
3352 amt = sizeof (struct bfd_link_needed_list);
3353 n = bfd_alloc (abfd, amt);
3354 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3355 if (n == NULL || fnm == NULL)
3356 goto error_free_dyn;
3357 amt = strlen (fnm) + 1;
3358 anm = bfd_alloc (abfd, amt);
3359 if (anm == NULL)
3360 goto error_free_dyn;
3361 memcpy (anm, fnm, amt);
3362 n->name = anm;
3363 n->by = abfd;
3364 n->next = NULL;
3365 for (pn = & runpath;
3366 *pn != NULL;
3367 pn = &(*pn)->next)
3369 *pn = n;
3371 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3372 if (!runpath && dyn.d_tag == DT_RPATH)
3374 struct bfd_link_needed_list *n, **pn;
3375 char *fnm, *anm;
3376 unsigned int tagv = dyn.d_un.d_val;
3378 amt = sizeof (struct bfd_link_needed_list);
3379 n = bfd_alloc (abfd, amt);
3380 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3381 if (n == NULL || fnm == NULL)
3382 goto error_free_dyn;
3383 amt = strlen (fnm) + 1;
3384 anm = bfd_alloc (abfd, amt);
3385 if (anm == NULL)
3387 error_free_dyn:
3388 free (dynbuf);
3389 goto error_return;
3391 memcpy (anm, fnm, amt);
3392 n->name = anm;
3393 n->by = abfd;
3394 n->next = NULL;
3395 for (pn = & rpath;
3396 *pn != NULL;
3397 pn = &(*pn)->next)
3399 *pn = n;
3403 free (dynbuf);
3406 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3407 frees all more recently bfd_alloc'd blocks as well. */
3408 if (runpath)
3409 rpath = runpath;
3411 if (rpath)
3413 struct bfd_link_needed_list **pn;
3414 for (pn = & hash_table->runpath;
3415 *pn != NULL;
3416 pn = &(*pn)->next)
3418 *pn = rpath;
3421 /* We do not want to include any of the sections in a dynamic
3422 object in the output file. We hack by simply clobbering the
3423 list of sections in the BFD. This could be handled more
3424 cleanly by, say, a new section flag; the existing
3425 SEC_NEVER_LOAD flag is not the one we want, because that one
3426 still implies that the section takes up space in the output
3427 file. */
3428 bfd_section_list_clear (abfd);
3430 /* Find the name to use in a DT_NEEDED entry that refers to this
3431 object. If the object has a DT_SONAME entry, we use it.
3432 Otherwise, if the generic linker stuck something in
3433 elf_dt_name, we use that. Otherwise, we just use the file
3434 name. */
3435 if (soname == NULL || *soname == '\0')
3437 soname = elf_dt_name (abfd);
3438 if (soname == NULL || *soname == '\0')
3439 soname = bfd_get_filename (abfd);
3442 /* Save the SONAME because sometimes the linker emulation code
3443 will need to know it. */
3444 elf_dt_name (abfd) = soname;
3446 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3447 if (ret < 0)
3448 goto error_return;
3450 /* If we have already included this dynamic object in the
3451 link, just ignore it. There is no reason to include a
3452 particular dynamic object more than once. */
3453 if (ret > 0)
3454 return TRUE;
3457 /* If this is a dynamic object, we always link against the .dynsym
3458 symbol table, not the .symtab symbol table. The dynamic linker
3459 will only see the .dynsym symbol table, so there is no reason to
3460 look at .symtab for a dynamic object. */
3462 if (! dynamic || elf_dynsymtab (abfd) == 0)
3463 hdr = &elf_tdata (abfd)->symtab_hdr;
3464 else
3465 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3467 symcount = hdr->sh_size / bed->s->sizeof_sym;
3469 /* The sh_info field of the symtab header tells us where the
3470 external symbols start. We don't care about the local symbols at
3471 this point. */
3472 if (elf_bad_symtab (abfd))
3474 extsymcount = symcount;
3475 extsymoff = 0;
3477 else
3479 extsymcount = symcount - hdr->sh_info;
3480 extsymoff = hdr->sh_info;
3483 sym_hash = NULL;
3484 if (extsymcount != 0)
3486 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3487 NULL, NULL, NULL);
3488 if (isymbuf == NULL)
3489 goto error_return;
3491 /* We store a pointer to the hash table entry for each external
3492 symbol. */
3493 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3494 sym_hash = bfd_alloc (abfd, amt);
3495 if (sym_hash == NULL)
3496 goto error_free_sym;
3497 elf_sym_hashes (abfd) = sym_hash;
3500 if (dynamic)
3502 /* Read in any version definitions. */
3503 if (!_bfd_elf_slurp_version_tables (abfd,
3504 info->default_imported_symver))
3505 goto error_free_sym;
3507 /* Read in the symbol versions, but don't bother to convert them
3508 to internal format. */
3509 if (elf_dynversym (abfd) != 0)
3511 Elf_Internal_Shdr *versymhdr;
3513 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3514 extversym = bfd_malloc (versymhdr->sh_size);
3515 if (extversym == NULL)
3516 goto error_free_sym;
3517 amt = versymhdr->sh_size;
3518 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3519 || bfd_bread (extversym, amt, abfd) != amt)
3520 goto error_free_vers;
3524 weaks = NULL;
3526 ever = extversym != NULL ? extversym + extsymoff : NULL;
3527 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3528 isym < isymend;
3529 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3531 int bind;
3532 bfd_vma value;
3533 asection *sec, *new_sec;
3534 flagword flags;
3535 const char *name;
3536 struct elf_link_hash_entry *h;
3537 bfd_boolean definition;
3538 bfd_boolean size_change_ok;
3539 bfd_boolean type_change_ok;
3540 bfd_boolean new_weakdef;
3541 bfd_boolean override;
3542 bfd_boolean common;
3543 unsigned int old_alignment;
3544 bfd *old_bfd;
3546 override = FALSE;
3548 flags = BSF_NO_FLAGS;
3549 sec = NULL;
3550 value = isym->st_value;
3551 *sym_hash = NULL;
3552 common = bed->common_definition (isym);
3554 bind = ELF_ST_BIND (isym->st_info);
3555 if (bind == STB_LOCAL)
3557 /* This should be impossible, since ELF requires that all
3558 global symbols follow all local symbols, and that sh_info
3559 point to the first global symbol. Unfortunately, Irix 5
3560 screws this up. */
3561 continue;
3563 else if (bind == STB_GLOBAL)
3565 if (isym->st_shndx != SHN_UNDEF && !common)
3566 flags = BSF_GLOBAL;
3568 else if (bind == STB_WEAK)
3569 flags = BSF_WEAK;
3570 else
3572 /* Leave it up to the processor backend. */
3575 if (isym->st_shndx == SHN_UNDEF)
3576 sec = bfd_und_section_ptr;
3577 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3579 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3580 if (sec == NULL)
3581 sec = bfd_abs_section_ptr;
3582 else if (sec->kept_section)
3584 /* Symbols from discarded section are undefined, and have
3585 default visibility. */
3586 sec = bfd_und_section_ptr;
3587 isym->st_shndx = SHN_UNDEF;
3588 isym->st_other = STV_DEFAULT
3589 | (isym->st_other & ~ ELF_ST_VISIBILITY(-1));
3591 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3592 value -= sec->vma;
3594 else if (isym->st_shndx == SHN_ABS)
3595 sec = bfd_abs_section_ptr;
3596 else if (isym->st_shndx == SHN_COMMON)
3598 sec = bfd_com_section_ptr;
3599 /* What ELF calls the size we call the value. What ELF
3600 calls the value we call the alignment. */
3601 value = isym->st_size;
3603 else
3605 /* Leave it up to the processor backend. */
3608 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3609 isym->st_name);
3610 if (name == NULL)
3611 goto error_free_vers;
3613 if (isym->st_shndx == SHN_COMMON
3614 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3616 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3618 if (tcomm == NULL)
3620 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3621 (SEC_ALLOC
3622 | SEC_IS_COMMON
3623 | SEC_LINKER_CREATED
3624 | SEC_THREAD_LOCAL));
3625 if (tcomm == NULL)
3626 goto error_free_vers;
3628 sec = tcomm;
3630 else if (add_symbol_hook)
3632 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3633 &value))
3634 goto error_free_vers;
3636 /* The hook function sets the name to NULL if this symbol
3637 should be skipped for some reason. */
3638 if (name == NULL)
3639 continue;
3642 /* Sanity check that all possibilities were handled. */
3643 if (sec == NULL)
3645 bfd_set_error (bfd_error_bad_value);
3646 goto error_free_vers;
3649 if (bfd_is_und_section (sec)
3650 || bfd_is_com_section (sec))
3651 definition = FALSE;
3652 else
3653 definition = TRUE;
3655 size_change_ok = FALSE;
3656 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3657 old_alignment = 0;
3658 old_bfd = NULL;
3659 new_sec = sec;
3661 if (is_elf_hash_table (hash_table))
3663 Elf_Internal_Versym iver;
3664 unsigned int vernum = 0;
3665 bfd_boolean skip;
3667 if (ever == NULL)
3669 if (info->default_imported_symver)
3670 /* Use the default symbol version created earlier. */
3671 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3672 else
3673 iver.vs_vers = 0;
3675 else
3676 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3678 vernum = iver.vs_vers & VERSYM_VERSION;
3680 /* If this is a hidden symbol, or if it is not version
3681 1, we append the version name to the symbol name.
3682 However, we do not modify a non-hidden absolute symbol
3683 if it is not a function, because it might be the version
3684 symbol itself. FIXME: What if it isn't? */
3685 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3686 || (vernum > 1 && (! bfd_is_abs_section (sec)
3687 || ELF_ST_TYPE (isym->st_info) == STT_FUNC)))
3689 const char *verstr;
3690 size_t namelen, verlen, newlen;
3691 char *newname, *p;
3693 if (isym->st_shndx != SHN_UNDEF)
3695 if (vernum > elf_tdata (abfd)->cverdefs)
3696 verstr = NULL;
3697 else if (vernum > 1)
3698 verstr =
3699 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3700 else
3701 verstr = "";
3703 if (verstr == NULL)
3705 (*_bfd_error_handler)
3706 (_("%B: %s: invalid version %u (max %d)"),
3707 abfd, name, vernum,
3708 elf_tdata (abfd)->cverdefs);
3709 bfd_set_error (bfd_error_bad_value);
3710 goto error_free_vers;
3713 else
3715 /* We cannot simply test for the number of
3716 entries in the VERNEED section since the
3717 numbers for the needed versions do not start
3718 at 0. */
3719 Elf_Internal_Verneed *t;
3721 verstr = NULL;
3722 for (t = elf_tdata (abfd)->verref;
3723 t != NULL;
3724 t = t->vn_nextref)
3726 Elf_Internal_Vernaux *a;
3728 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3730 if (a->vna_other == vernum)
3732 verstr = a->vna_nodename;
3733 break;
3736 if (a != NULL)
3737 break;
3739 if (verstr == NULL)
3741 (*_bfd_error_handler)
3742 (_("%B: %s: invalid needed version %d"),
3743 abfd, name, vernum);
3744 bfd_set_error (bfd_error_bad_value);
3745 goto error_free_vers;
3749 namelen = strlen (name);
3750 verlen = strlen (verstr);
3751 newlen = namelen + verlen + 2;
3752 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3753 && isym->st_shndx != SHN_UNDEF)
3754 ++newlen;
3756 newname = bfd_alloc (abfd, newlen);
3757 if (newname == NULL)
3758 goto error_free_vers;
3759 memcpy (newname, name, namelen);
3760 p = newname + namelen;
3761 *p++ = ELF_VER_CHR;
3762 /* If this is a defined non-hidden version symbol,
3763 we add another @ to the name. This indicates the
3764 default version of the symbol. */
3765 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3766 && isym->st_shndx != SHN_UNDEF)
3767 *p++ = ELF_VER_CHR;
3768 memcpy (p, verstr, verlen + 1);
3770 name = newname;
3773 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
3774 &value, &old_alignment,
3775 sym_hash, &skip, &override,
3776 &type_change_ok, &size_change_ok))
3777 goto error_free_vers;
3779 if (skip)
3780 continue;
3782 if (override)
3783 definition = FALSE;
3785 h = *sym_hash;
3786 while (h->root.type == bfd_link_hash_indirect
3787 || h->root.type == bfd_link_hash_warning)
3788 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3790 /* Remember the old alignment if this is a common symbol, so
3791 that we don't reduce the alignment later on. We can't
3792 check later, because _bfd_generic_link_add_one_symbol
3793 will set a default for the alignment which we want to
3794 override. We also remember the old bfd where the existing
3795 definition comes from. */
3796 switch (h->root.type)
3798 default:
3799 break;
3801 case bfd_link_hash_defined:
3802 case bfd_link_hash_defweak:
3803 old_bfd = h->root.u.def.section->owner;
3804 break;
3806 case bfd_link_hash_common:
3807 old_bfd = h->root.u.c.p->section->owner;
3808 old_alignment = h->root.u.c.p->alignment_power;
3809 break;
3812 if (elf_tdata (abfd)->verdef != NULL
3813 && ! override
3814 && vernum > 1
3815 && definition)
3816 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3819 if (! (_bfd_generic_link_add_one_symbol
3820 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3821 (struct bfd_link_hash_entry **) sym_hash)))
3822 goto error_free_vers;
3824 h = *sym_hash;
3825 while (h->root.type == bfd_link_hash_indirect
3826 || h->root.type == bfd_link_hash_warning)
3827 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3828 *sym_hash = h;
3830 new_weakdef = FALSE;
3831 if (dynamic
3832 && definition
3833 && (flags & BSF_WEAK) != 0
3834 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3835 && is_elf_hash_table (hash_table)
3836 && h->u.weakdef == NULL)
3838 /* Keep a list of all weak defined non function symbols from
3839 a dynamic object, using the weakdef field. Later in this
3840 function we will set the weakdef field to the correct
3841 value. We only put non-function symbols from dynamic
3842 objects on this list, because that happens to be the only
3843 time we need to know the normal symbol corresponding to a
3844 weak symbol, and the information is time consuming to
3845 figure out. If the weakdef field is not already NULL,
3846 then this symbol was already defined by some previous
3847 dynamic object, and we will be using that previous
3848 definition anyhow. */
3850 h->u.weakdef = weaks;
3851 weaks = h;
3852 new_weakdef = TRUE;
3855 /* Set the alignment of a common symbol. */
3856 if ((common || bfd_is_com_section (sec))
3857 && h->root.type == bfd_link_hash_common)
3859 unsigned int align;
3861 if (common)
3862 align = bfd_log2 (isym->st_value);
3863 else
3865 /* The new symbol is a common symbol in a shared object.
3866 We need to get the alignment from the section. */
3867 align = new_sec->alignment_power;
3869 if (align > old_alignment
3870 /* Permit an alignment power of zero if an alignment of one
3871 is specified and no other alignments have been specified. */
3872 || (isym->st_value == 1 && old_alignment == 0))
3873 h->root.u.c.p->alignment_power = align;
3874 else
3875 h->root.u.c.p->alignment_power = old_alignment;
3878 if (is_elf_hash_table (hash_table))
3880 bfd_boolean dynsym;
3882 /* Check the alignment when a common symbol is involved. This
3883 can change when a common symbol is overridden by a normal
3884 definition or a common symbol is ignored due to the old
3885 normal definition. We need to make sure the maximum
3886 alignment is maintained. */
3887 if ((old_alignment || common)
3888 && h->root.type != bfd_link_hash_common)
3890 unsigned int common_align;
3891 unsigned int normal_align;
3892 unsigned int symbol_align;
3893 bfd *normal_bfd;
3894 bfd *common_bfd;
3896 symbol_align = ffs (h->root.u.def.value) - 1;
3897 if (h->root.u.def.section->owner != NULL
3898 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3900 normal_align = h->root.u.def.section->alignment_power;
3901 if (normal_align > symbol_align)
3902 normal_align = symbol_align;
3904 else
3905 normal_align = symbol_align;
3907 if (old_alignment)
3909 common_align = old_alignment;
3910 common_bfd = old_bfd;
3911 normal_bfd = abfd;
3913 else
3915 common_align = bfd_log2 (isym->st_value);
3916 common_bfd = abfd;
3917 normal_bfd = old_bfd;
3920 if (normal_align < common_align)
3921 (*_bfd_error_handler)
3922 (_("Warning: alignment %u of symbol `%s' in %B"
3923 " is smaller than %u in %B"),
3924 normal_bfd, common_bfd,
3925 1 << normal_align, name, 1 << common_align);
3928 /* Remember the symbol size and type. */
3929 if (isym->st_size != 0
3930 && (definition || h->size == 0))
3932 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3933 (*_bfd_error_handler)
3934 (_("Warning: size of symbol `%s' changed"
3935 " from %lu in %B to %lu in %B"),
3936 old_bfd, abfd,
3937 name, (unsigned long) h->size,
3938 (unsigned long) isym->st_size);
3940 h->size = isym->st_size;
3943 /* If this is a common symbol, then we always want H->SIZE
3944 to be the size of the common symbol. The code just above
3945 won't fix the size if a common symbol becomes larger. We
3946 don't warn about a size change here, because that is
3947 covered by --warn-common. */
3948 if (h->root.type == bfd_link_hash_common)
3949 h->size = h->root.u.c.size;
3951 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3952 && (definition || h->type == STT_NOTYPE))
3954 if (h->type != STT_NOTYPE
3955 && h->type != ELF_ST_TYPE (isym->st_info)
3956 && ! type_change_ok)
3957 (*_bfd_error_handler)
3958 (_("Warning: type of symbol `%s' changed"
3959 " from %d to %d in %B"),
3960 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3962 h->type = ELF_ST_TYPE (isym->st_info);
3965 /* If st_other has a processor-specific meaning, specific
3966 code might be needed here. We never merge the visibility
3967 attribute with the one from a dynamic object. */
3968 if (bed->elf_backend_merge_symbol_attribute)
3969 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3970 dynamic);
3972 /* If this symbol has default visibility and the user has requested
3973 we not re-export it, then mark it as hidden. */
3974 if (definition && !dynamic
3975 && (abfd->no_export
3976 || (abfd->my_archive && abfd->my_archive->no_export))
3977 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
3978 isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1));
3980 if (isym->st_other != 0 && !dynamic)
3982 unsigned char hvis, symvis, other, nvis;
3984 /* Take the balance of OTHER from the definition. */
3985 other = (definition ? isym->st_other : h->other);
3986 other &= ~ ELF_ST_VISIBILITY (-1);
3988 /* Combine visibilities, using the most constraining one. */
3989 hvis = ELF_ST_VISIBILITY (h->other);
3990 symvis = ELF_ST_VISIBILITY (isym->st_other);
3991 if (! hvis)
3992 nvis = symvis;
3993 else if (! symvis)
3994 nvis = hvis;
3995 else
3996 nvis = hvis < symvis ? hvis : symvis;
3998 h->other = other | nvis;
4001 /* Set a flag in the hash table entry indicating the type of
4002 reference or definition we just found. Keep a count of
4003 the number of dynamic symbols we find. A dynamic symbol
4004 is one which is referenced or defined by both a regular
4005 object and a shared object. */
4006 dynsym = FALSE;
4007 if (! dynamic)
4009 if (! definition)
4011 h->ref_regular = 1;
4012 if (bind != STB_WEAK)
4013 h->ref_regular_nonweak = 1;
4015 else
4016 h->def_regular = 1;
4017 if (! info->executable
4018 || h->def_dynamic
4019 || h->ref_dynamic)
4020 dynsym = TRUE;
4022 else
4024 if (! definition)
4025 h->ref_dynamic = 1;
4026 else
4027 h->def_dynamic = 1;
4028 if (h->def_regular
4029 || h->ref_regular
4030 || (h->u.weakdef != NULL
4031 && ! new_weakdef
4032 && h->u.weakdef->dynindx != -1))
4033 dynsym = TRUE;
4036 /* Check to see if we need to add an indirect symbol for
4037 the default name. */
4038 if (definition || h->root.type == bfd_link_hash_common)
4039 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4040 &sec, &value, &dynsym,
4041 override))
4042 goto error_free_vers;
4044 if (definition && !dynamic)
4046 char *p = strchr (name, ELF_VER_CHR);
4047 if (p != NULL && p[1] != ELF_VER_CHR)
4049 /* Queue non-default versions so that .symver x, x@FOO
4050 aliases can be checked. */
4051 if (! nondeflt_vers)
4053 amt = (isymend - isym + 1)
4054 * sizeof (struct elf_link_hash_entry *);
4055 nondeflt_vers = bfd_malloc (amt);
4057 nondeflt_vers [nondeflt_vers_cnt++] = h;
4061 if (dynsym && h->dynindx == -1)
4063 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4064 goto error_free_vers;
4065 if (h->u.weakdef != NULL
4066 && ! new_weakdef
4067 && h->u.weakdef->dynindx == -1)
4069 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4070 goto error_free_vers;
4073 else if (dynsym && h->dynindx != -1)
4074 /* If the symbol already has a dynamic index, but
4075 visibility says it should not be visible, turn it into
4076 a local symbol. */
4077 switch (ELF_ST_VISIBILITY (h->other))
4079 case STV_INTERNAL:
4080 case STV_HIDDEN:
4081 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4082 dynsym = FALSE;
4083 break;
4086 if (!add_needed
4087 && definition
4088 && dynsym
4089 && h->ref_regular)
4091 int ret;
4092 const char *soname = elf_dt_name (abfd);
4094 /* A symbol from a library loaded via DT_NEEDED of some
4095 other library is referenced by a regular object.
4096 Add a DT_NEEDED entry for it. Issue an error if
4097 --no-add-needed is used. */
4098 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4100 (*_bfd_error_handler)
4101 (_("%s: invalid DSO for symbol `%s' definition"),
4102 abfd, name);
4103 bfd_set_error (bfd_error_bad_value);
4104 goto error_free_vers;
4107 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4109 add_needed = TRUE;
4110 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4111 if (ret < 0)
4112 goto error_free_vers;
4114 BFD_ASSERT (ret == 0);
4119 /* Now that all the symbols from this input file are created, handle
4120 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4121 if (nondeflt_vers != NULL)
4123 bfd_size_type cnt, symidx;
4125 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4127 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4128 char *shortname, *p;
4130 p = strchr (h->root.root.string, ELF_VER_CHR);
4131 if (p == NULL
4132 || (h->root.type != bfd_link_hash_defined
4133 && h->root.type != bfd_link_hash_defweak))
4134 continue;
4136 amt = p - h->root.root.string;
4137 shortname = bfd_malloc (amt + 1);
4138 memcpy (shortname, h->root.root.string, amt);
4139 shortname[amt] = '\0';
4141 hi = (struct elf_link_hash_entry *)
4142 bfd_link_hash_lookup (&hash_table->root, shortname,
4143 FALSE, FALSE, FALSE);
4144 if (hi != NULL
4145 && hi->root.type == h->root.type
4146 && hi->root.u.def.value == h->root.u.def.value
4147 && hi->root.u.def.section == h->root.u.def.section)
4149 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4150 hi->root.type = bfd_link_hash_indirect;
4151 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4152 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4153 sym_hash = elf_sym_hashes (abfd);
4154 if (sym_hash)
4155 for (symidx = 0; symidx < extsymcount; ++symidx)
4156 if (sym_hash[symidx] == hi)
4158 sym_hash[symidx] = h;
4159 break;
4162 free (shortname);
4164 free (nondeflt_vers);
4165 nondeflt_vers = NULL;
4168 if (extversym != NULL)
4170 free (extversym);
4171 extversym = NULL;
4174 if (isymbuf != NULL)
4175 free (isymbuf);
4176 isymbuf = NULL;
4178 if (!add_needed
4179 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4181 /* Remove symbols defined in an as-needed shared lib that wasn't
4182 needed. */
4183 struct elf_smash_syms_data inf;
4184 inf.not_needed = abfd;
4185 inf.htab = hash_table;
4186 inf.twiddled = FALSE;
4187 elf_link_hash_traverse (hash_table, elf_smash_syms, &inf);
4188 if (inf.twiddled)
4189 bfd_link_repair_undef_list (&hash_table->root);
4190 weaks = NULL;
4193 /* Now set the weakdefs field correctly for all the weak defined
4194 symbols we found. The only way to do this is to search all the
4195 symbols. Since we only need the information for non functions in
4196 dynamic objects, that's the only time we actually put anything on
4197 the list WEAKS. We need this information so that if a regular
4198 object refers to a symbol defined weakly in a dynamic object, the
4199 real symbol in the dynamic object is also put in the dynamic
4200 symbols; we also must arrange for both symbols to point to the
4201 same memory location. We could handle the general case of symbol
4202 aliasing, but a general symbol alias can only be generated in
4203 assembler code, handling it correctly would be very time
4204 consuming, and other ELF linkers don't handle general aliasing
4205 either. */
4206 if (weaks != NULL)
4208 struct elf_link_hash_entry **hpp;
4209 struct elf_link_hash_entry **hppend;
4210 struct elf_link_hash_entry **sorted_sym_hash;
4211 struct elf_link_hash_entry *h;
4212 size_t sym_count;
4214 /* Since we have to search the whole symbol list for each weak
4215 defined symbol, search time for N weak defined symbols will be
4216 O(N^2). Binary search will cut it down to O(NlogN). */
4217 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4218 sorted_sym_hash = bfd_malloc (amt);
4219 if (sorted_sym_hash == NULL)
4220 goto error_return;
4221 sym_hash = sorted_sym_hash;
4222 hpp = elf_sym_hashes (abfd);
4223 hppend = hpp + extsymcount;
4224 sym_count = 0;
4225 for (; hpp < hppend; hpp++)
4227 h = *hpp;
4228 if (h != NULL
4229 && h->root.type == bfd_link_hash_defined
4230 && h->type != STT_FUNC)
4232 *sym_hash = h;
4233 sym_hash++;
4234 sym_count++;
4238 qsort (sorted_sym_hash, sym_count,
4239 sizeof (struct elf_link_hash_entry *),
4240 elf_sort_symbol);
4242 while (weaks != NULL)
4244 struct elf_link_hash_entry *hlook;
4245 asection *slook;
4246 bfd_vma vlook;
4247 long ilook;
4248 size_t i, j, idx;
4250 hlook = weaks;
4251 weaks = hlook->u.weakdef;
4252 hlook->u.weakdef = NULL;
4254 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4255 || hlook->root.type == bfd_link_hash_defweak
4256 || hlook->root.type == bfd_link_hash_common
4257 || hlook->root.type == bfd_link_hash_indirect);
4258 slook = hlook->root.u.def.section;
4259 vlook = hlook->root.u.def.value;
4261 ilook = -1;
4262 i = 0;
4263 j = sym_count;
4264 while (i < j)
4266 bfd_signed_vma vdiff;
4267 idx = (i + j) / 2;
4268 h = sorted_sym_hash [idx];
4269 vdiff = vlook - h->root.u.def.value;
4270 if (vdiff < 0)
4271 j = idx;
4272 else if (vdiff > 0)
4273 i = idx + 1;
4274 else
4276 long sdiff = slook->id - h->root.u.def.section->id;
4277 if (sdiff < 0)
4278 j = idx;
4279 else if (sdiff > 0)
4280 i = idx + 1;
4281 else
4283 ilook = idx;
4284 break;
4289 /* We didn't find a value/section match. */
4290 if (ilook == -1)
4291 continue;
4293 for (i = ilook; i < sym_count; i++)
4295 h = sorted_sym_hash [i];
4297 /* Stop if value or section doesn't match. */
4298 if (h->root.u.def.value != vlook
4299 || h->root.u.def.section != slook)
4300 break;
4301 else if (h != hlook)
4303 hlook->u.weakdef = h;
4305 /* If the weak definition is in the list of dynamic
4306 symbols, make sure the real definition is put
4307 there as well. */
4308 if (hlook->dynindx != -1 && h->dynindx == -1)
4310 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4311 goto error_return;
4314 /* If the real definition is in the list of dynamic
4315 symbols, make sure the weak definition is put
4316 there as well. If we don't do this, then the
4317 dynamic loader might not merge the entries for the
4318 real definition and the weak definition. */
4319 if (h->dynindx != -1 && hlook->dynindx == -1)
4321 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4322 goto error_return;
4324 break;
4329 free (sorted_sym_hash);
4332 check_directives = get_elf_backend_data (abfd)->check_directives;
4333 if (check_directives)
4334 check_directives (abfd, info);
4336 /* If this object is the same format as the output object, and it is
4337 not a shared library, then let the backend look through the
4338 relocs.
4340 This is required to build global offset table entries and to
4341 arrange for dynamic relocs. It is not required for the
4342 particular common case of linking non PIC code, even when linking
4343 against shared libraries, but unfortunately there is no way of
4344 knowing whether an object file has been compiled PIC or not.
4345 Looking through the relocs is not particularly time consuming.
4346 The problem is that we must either (1) keep the relocs in memory,
4347 which causes the linker to require additional runtime memory or
4348 (2) read the relocs twice from the input file, which wastes time.
4349 This would be a good case for using mmap.
4351 I have no idea how to handle linking PIC code into a file of a
4352 different format. It probably can't be done. */
4353 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4354 if (! dynamic
4355 && is_elf_hash_table (hash_table)
4356 && hash_table->root.creator == abfd->xvec
4357 && check_relocs != NULL)
4359 asection *o;
4361 for (o = abfd->sections; o != NULL; o = o->next)
4363 Elf_Internal_Rela *internal_relocs;
4364 bfd_boolean ok;
4366 if ((o->flags & SEC_RELOC) == 0
4367 || o->reloc_count == 0
4368 || ((info->strip == strip_all || info->strip == strip_debugger)
4369 && (o->flags & SEC_DEBUGGING) != 0)
4370 || bfd_is_abs_section (o->output_section))
4371 continue;
4373 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4374 info->keep_memory);
4375 if (internal_relocs == NULL)
4376 goto error_return;
4378 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4380 if (elf_section_data (o)->relocs != internal_relocs)
4381 free (internal_relocs);
4383 if (! ok)
4384 goto error_return;
4388 /* If this is a non-traditional link, try to optimize the handling
4389 of the .stab/.stabstr sections. */
4390 if (! dynamic
4391 && ! info->traditional_format
4392 && is_elf_hash_table (hash_table)
4393 && (info->strip != strip_all && info->strip != strip_debugger))
4395 asection *stabstr;
4397 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4398 if (stabstr != NULL)
4400 bfd_size_type string_offset = 0;
4401 asection *stab;
4403 for (stab = abfd->sections; stab; stab = stab->next)
4404 if (strncmp (".stab", stab->name, 5) == 0
4405 && (!stab->name[5] ||
4406 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4407 && (stab->flags & SEC_MERGE) == 0
4408 && !bfd_is_abs_section (stab->output_section))
4410 struct bfd_elf_section_data *secdata;
4412 secdata = elf_section_data (stab);
4413 if (! _bfd_link_section_stabs (abfd,
4414 &hash_table->stab_info,
4415 stab, stabstr,
4416 &secdata->sec_info,
4417 &string_offset))
4418 goto error_return;
4419 if (secdata->sec_info)
4420 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4425 if (is_elf_hash_table (hash_table) && add_needed)
4427 /* Add this bfd to the loaded list. */
4428 struct elf_link_loaded_list *n;
4430 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4431 if (n == NULL)
4432 goto error_return;
4433 n->abfd = abfd;
4434 n->next = hash_table->loaded;
4435 hash_table->loaded = n;
4438 return TRUE;
4440 error_free_vers:
4441 if (nondeflt_vers != NULL)
4442 free (nondeflt_vers);
4443 if (extversym != NULL)
4444 free (extversym);
4445 error_free_sym:
4446 if (isymbuf != NULL)
4447 free (isymbuf);
4448 error_return:
4449 return FALSE;
4452 /* Return the linker hash table entry of a symbol that might be
4453 satisfied by an archive symbol. Return -1 on error. */
4455 struct elf_link_hash_entry *
4456 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4457 struct bfd_link_info *info,
4458 const char *name)
4460 struct elf_link_hash_entry *h;
4461 char *p, *copy;
4462 size_t len, first;
4464 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4465 if (h != NULL)
4466 return h;
4468 /* If this is a default version (the name contains @@), look up the
4469 symbol again with only one `@' as well as without the version.
4470 The effect is that references to the symbol with and without the
4471 version will be matched by the default symbol in the archive. */
4473 p = strchr (name, ELF_VER_CHR);
4474 if (p == NULL || p[1] != ELF_VER_CHR)
4475 return h;
4477 /* First check with only one `@'. */
4478 len = strlen (name);
4479 copy = bfd_alloc (abfd, len);
4480 if (copy == NULL)
4481 return (struct elf_link_hash_entry *) 0 - 1;
4483 first = p - name + 1;
4484 memcpy (copy, name, first);
4485 memcpy (copy + first, name + first + 1, len - first);
4487 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4488 if (h == NULL)
4490 /* We also need to check references to the symbol without the
4491 version. */
4492 copy[first - 1] = '\0';
4493 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4494 FALSE, FALSE, FALSE);
4497 bfd_release (abfd, copy);
4498 return h;
4501 /* Add symbols from an ELF archive file to the linker hash table. We
4502 don't use _bfd_generic_link_add_archive_symbols because of a
4503 problem which arises on UnixWare. The UnixWare libc.so is an
4504 archive which includes an entry libc.so.1 which defines a bunch of
4505 symbols. The libc.so archive also includes a number of other
4506 object files, which also define symbols, some of which are the same
4507 as those defined in libc.so.1. Correct linking requires that we
4508 consider each object file in turn, and include it if it defines any
4509 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4510 this; it looks through the list of undefined symbols, and includes
4511 any object file which defines them. When this algorithm is used on
4512 UnixWare, it winds up pulling in libc.so.1 early and defining a
4513 bunch of symbols. This means that some of the other objects in the
4514 archive are not included in the link, which is incorrect since they
4515 precede libc.so.1 in the archive.
4517 Fortunately, ELF archive handling is simpler than that done by
4518 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4519 oddities. In ELF, if we find a symbol in the archive map, and the
4520 symbol is currently undefined, we know that we must pull in that
4521 object file.
4523 Unfortunately, we do have to make multiple passes over the symbol
4524 table until nothing further is resolved. */
4526 static bfd_boolean
4527 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4529 symindex c;
4530 bfd_boolean *defined = NULL;
4531 bfd_boolean *included = NULL;
4532 carsym *symdefs;
4533 bfd_boolean loop;
4534 bfd_size_type amt;
4535 const struct elf_backend_data *bed;
4536 struct elf_link_hash_entry * (*archive_symbol_lookup)
4537 (bfd *, struct bfd_link_info *, const char *);
4539 if (! bfd_has_map (abfd))
4541 /* An empty archive is a special case. */
4542 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4543 return TRUE;
4544 bfd_set_error (bfd_error_no_armap);
4545 return FALSE;
4548 /* Keep track of all symbols we know to be already defined, and all
4549 files we know to be already included. This is to speed up the
4550 second and subsequent passes. */
4551 c = bfd_ardata (abfd)->symdef_count;
4552 if (c == 0)
4553 return TRUE;
4554 amt = c;
4555 amt *= sizeof (bfd_boolean);
4556 defined = bfd_zmalloc (amt);
4557 included = bfd_zmalloc (amt);
4558 if (defined == NULL || included == NULL)
4559 goto error_return;
4561 symdefs = bfd_ardata (abfd)->symdefs;
4562 bed = get_elf_backend_data (abfd);
4563 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4567 file_ptr last;
4568 symindex i;
4569 carsym *symdef;
4570 carsym *symdefend;
4572 loop = FALSE;
4573 last = -1;
4575 symdef = symdefs;
4576 symdefend = symdef + c;
4577 for (i = 0; symdef < symdefend; symdef++, i++)
4579 struct elf_link_hash_entry *h;
4580 bfd *element;
4581 struct bfd_link_hash_entry *undefs_tail;
4582 symindex mark;
4584 if (defined[i] || included[i])
4585 continue;
4586 if (symdef->file_offset == last)
4588 included[i] = TRUE;
4589 continue;
4592 h = archive_symbol_lookup (abfd, info, symdef->name);
4593 if (h == (struct elf_link_hash_entry *) 0 - 1)
4594 goto error_return;
4596 if (h == NULL)
4597 continue;
4599 if (h->root.type == bfd_link_hash_common)
4601 /* We currently have a common symbol. The archive map contains
4602 a reference to this symbol, so we may want to include it. We
4603 only want to include it however, if this archive element
4604 contains a definition of the symbol, not just another common
4605 declaration of it.
4607 Unfortunately some archivers (including GNU ar) will put
4608 declarations of common symbols into their archive maps, as
4609 well as real definitions, so we cannot just go by the archive
4610 map alone. Instead we must read in the element's symbol
4611 table and check that to see what kind of symbol definition
4612 this is. */
4613 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4614 continue;
4616 else if (h->root.type != bfd_link_hash_undefined)
4618 if (h->root.type != bfd_link_hash_undefweak)
4619 defined[i] = TRUE;
4620 continue;
4623 /* We need to include this archive member. */
4624 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4625 if (element == NULL)
4626 goto error_return;
4628 if (! bfd_check_format (element, bfd_object))
4629 goto error_return;
4631 /* Doublecheck that we have not included this object
4632 already--it should be impossible, but there may be
4633 something wrong with the archive. */
4634 if (element->archive_pass != 0)
4636 bfd_set_error (bfd_error_bad_value);
4637 goto error_return;
4639 element->archive_pass = 1;
4641 undefs_tail = info->hash->undefs_tail;
4643 if (! (*info->callbacks->add_archive_element) (info, element,
4644 symdef->name))
4645 goto error_return;
4646 if (! bfd_link_add_symbols (element, info))
4647 goto error_return;
4649 /* If there are any new undefined symbols, we need to make
4650 another pass through the archive in order to see whether
4651 they can be defined. FIXME: This isn't perfect, because
4652 common symbols wind up on undefs_tail and because an
4653 undefined symbol which is defined later on in this pass
4654 does not require another pass. This isn't a bug, but it
4655 does make the code less efficient than it could be. */
4656 if (undefs_tail != info->hash->undefs_tail)
4657 loop = TRUE;
4659 /* Look backward to mark all symbols from this object file
4660 which we have already seen in this pass. */
4661 mark = i;
4664 included[mark] = TRUE;
4665 if (mark == 0)
4666 break;
4667 --mark;
4669 while (symdefs[mark].file_offset == symdef->file_offset);
4671 /* We mark subsequent symbols from this object file as we go
4672 on through the loop. */
4673 last = symdef->file_offset;
4676 while (loop);
4678 free (defined);
4679 free (included);
4681 return TRUE;
4683 error_return:
4684 if (defined != NULL)
4685 free (defined);
4686 if (included != NULL)
4687 free (included);
4688 return FALSE;
4691 /* Given an ELF BFD, add symbols to the global hash table as
4692 appropriate. */
4694 bfd_boolean
4695 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4697 switch (bfd_get_format (abfd))
4699 case bfd_object:
4700 return elf_link_add_object_symbols (abfd, info);
4701 case bfd_archive:
4702 return elf_link_add_archive_symbols (abfd, info);
4703 default:
4704 bfd_set_error (bfd_error_wrong_format);
4705 return FALSE;
4709 /* This function will be called though elf_link_hash_traverse to store
4710 all hash value of the exported symbols in an array. */
4712 static bfd_boolean
4713 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4715 unsigned long **valuep = data;
4716 const char *name;
4717 char *p;
4718 unsigned long ha;
4719 char *alc = NULL;
4721 if (h->root.type == bfd_link_hash_warning)
4722 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4724 /* Ignore indirect symbols. These are added by the versioning code. */
4725 if (h->dynindx == -1)
4726 return TRUE;
4728 name = h->root.root.string;
4729 p = strchr (name, ELF_VER_CHR);
4730 if (p != NULL)
4732 alc = bfd_malloc (p - name + 1);
4733 memcpy (alc, name, p - name);
4734 alc[p - name] = '\0';
4735 name = alc;
4738 /* Compute the hash value. */
4739 ha = bfd_elf_hash (name);
4741 /* Store the found hash value in the array given as the argument. */
4742 *(*valuep)++ = ha;
4744 /* And store it in the struct so that we can put it in the hash table
4745 later. */
4746 h->u.elf_hash_value = ha;
4748 if (alc != NULL)
4749 free (alc);
4751 return TRUE;
4754 /* Array used to determine the number of hash table buckets to use
4755 based on the number of symbols there are. If there are fewer than
4756 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4757 fewer than 37 we use 17 buckets, and so forth. We never use more
4758 than 32771 buckets. */
4760 static const size_t elf_buckets[] =
4762 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4763 16411, 32771, 0
4766 /* Compute bucket count for hashing table. We do not use a static set
4767 of possible tables sizes anymore. Instead we determine for all
4768 possible reasonable sizes of the table the outcome (i.e., the
4769 number of collisions etc) and choose the best solution. The
4770 weighting functions are not too simple to allow the table to grow
4771 without bounds. Instead one of the weighting factors is the size.
4772 Therefore the result is always a good payoff between few collisions
4773 (= short chain lengths) and table size. */
4774 static size_t
4775 compute_bucket_count (struct bfd_link_info *info)
4777 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4778 size_t best_size = 0;
4779 unsigned long int *hashcodes;
4780 unsigned long int *hashcodesp;
4781 unsigned long int i;
4782 bfd_size_type amt;
4784 /* Compute the hash values for all exported symbols. At the same
4785 time store the values in an array so that we could use them for
4786 optimizations. */
4787 amt = dynsymcount;
4788 amt *= sizeof (unsigned long int);
4789 hashcodes = bfd_malloc (amt);
4790 if (hashcodes == NULL)
4791 return 0;
4792 hashcodesp = hashcodes;
4794 /* Put all hash values in HASHCODES. */
4795 elf_link_hash_traverse (elf_hash_table (info),
4796 elf_collect_hash_codes, &hashcodesp);
4798 /* We have a problem here. The following code to optimize the table
4799 size requires an integer type with more the 32 bits. If
4800 BFD_HOST_U_64_BIT is set we know about such a type. */
4801 #ifdef BFD_HOST_U_64_BIT
4802 if (info->optimize)
4804 unsigned long int nsyms = hashcodesp - hashcodes;
4805 size_t minsize;
4806 size_t maxsize;
4807 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4808 unsigned long int *counts ;
4809 bfd *dynobj = elf_hash_table (info)->dynobj;
4810 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4812 /* Possible optimization parameters: if we have NSYMS symbols we say
4813 that the hashing table must at least have NSYMS/4 and at most
4814 2*NSYMS buckets. */
4815 minsize = nsyms / 4;
4816 if (minsize == 0)
4817 minsize = 1;
4818 best_size = maxsize = nsyms * 2;
4820 /* Create array where we count the collisions in. We must use bfd_malloc
4821 since the size could be large. */
4822 amt = maxsize;
4823 amt *= sizeof (unsigned long int);
4824 counts = bfd_malloc (amt);
4825 if (counts == NULL)
4827 free (hashcodes);
4828 return 0;
4831 /* Compute the "optimal" size for the hash table. The criteria is a
4832 minimal chain length. The minor criteria is (of course) the size
4833 of the table. */
4834 for (i = minsize; i < maxsize; ++i)
4836 /* Walk through the array of hashcodes and count the collisions. */
4837 BFD_HOST_U_64_BIT max;
4838 unsigned long int j;
4839 unsigned long int fact;
4841 memset (counts, '\0', i * sizeof (unsigned long int));
4843 /* Determine how often each hash bucket is used. */
4844 for (j = 0; j < nsyms; ++j)
4845 ++counts[hashcodes[j] % i];
4847 /* For the weight function we need some information about the
4848 pagesize on the target. This is information need not be 100%
4849 accurate. Since this information is not available (so far) we
4850 define it here to a reasonable default value. If it is crucial
4851 to have a better value some day simply define this value. */
4852 # ifndef BFD_TARGET_PAGESIZE
4853 # define BFD_TARGET_PAGESIZE (4096)
4854 # endif
4856 /* We in any case need 2 + NSYMS entries for the size values and
4857 the chains. */
4858 max = (2 + nsyms) * (bed->s->arch_size / 8);
4860 # if 1
4861 /* Variant 1: optimize for short chains. We add the squares
4862 of all the chain lengths (which favors many small chain
4863 over a few long chains). */
4864 for (j = 0; j < i; ++j)
4865 max += counts[j] * counts[j];
4867 /* This adds penalties for the overall size of the table. */
4868 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4869 max *= fact * fact;
4870 # else
4871 /* Variant 2: Optimize a lot more for small table. Here we
4872 also add squares of the size but we also add penalties for
4873 empty slots (the +1 term). */
4874 for (j = 0; j < i; ++j)
4875 max += (1 + counts[j]) * (1 + counts[j]);
4877 /* The overall size of the table is considered, but not as
4878 strong as in variant 1, where it is squared. */
4879 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4880 max *= fact;
4881 # endif
4883 /* Compare with current best results. */
4884 if (max < best_chlen)
4886 best_chlen = max;
4887 best_size = i;
4891 free (counts);
4893 else
4894 #endif /* defined (BFD_HOST_U_64_BIT) */
4896 /* This is the fallback solution if no 64bit type is available or if we
4897 are not supposed to spend much time on optimizations. We select the
4898 bucket count using a fixed set of numbers. */
4899 for (i = 0; elf_buckets[i] != 0; i++)
4901 best_size = elf_buckets[i];
4902 if (dynsymcount < elf_buckets[i + 1])
4903 break;
4907 /* Free the arrays we needed. */
4908 free (hashcodes);
4910 return best_size;
4913 /* Set up the sizes and contents of the ELF dynamic sections. This is
4914 called by the ELF linker emulation before_allocation routine. We
4915 must set the sizes of the sections before the linker sets the
4916 addresses of the various sections. */
4918 bfd_boolean
4919 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4920 const char *soname,
4921 const char *rpath,
4922 const char *filter_shlib,
4923 const char * const *auxiliary_filters,
4924 struct bfd_link_info *info,
4925 asection **sinterpptr,
4926 struct bfd_elf_version_tree *verdefs)
4928 bfd_size_type soname_indx;
4929 bfd *dynobj;
4930 const struct elf_backend_data *bed;
4931 struct elf_assign_sym_version_info asvinfo;
4933 *sinterpptr = NULL;
4935 soname_indx = (bfd_size_type) -1;
4937 if (!is_elf_hash_table (info->hash))
4938 return TRUE;
4940 elf_tdata (output_bfd)->relro = info->relro;
4941 if (info->execstack)
4942 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4943 else if (info->noexecstack)
4944 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4945 else
4947 bfd *inputobj;
4948 asection *notesec = NULL;
4949 int exec = 0;
4951 for (inputobj = info->input_bfds;
4952 inputobj;
4953 inputobj = inputobj->link_next)
4955 asection *s;
4957 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
4958 continue;
4959 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4960 if (s)
4962 if (s->flags & SEC_CODE)
4963 exec = PF_X;
4964 notesec = s;
4966 else
4967 exec = PF_X;
4969 if (notesec)
4971 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4972 if (exec && info->relocatable
4973 && notesec->output_section != bfd_abs_section_ptr)
4974 notesec->output_section->flags |= SEC_CODE;
4978 /* Any syms created from now on start with -1 in
4979 got.refcount/offset and plt.refcount/offset. */
4980 elf_hash_table (info)->init_got_refcount
4981 = elf_hash_table (info)->init_got_offset;
4982 elf_hash_table (info)->init_plt_refcount
4983 = elf_hash_table (info)->init_plt_offset;
4985 /* The backend may have to create some sections regardless of whether
4986 we're dynamic or not. */
4987 bed = get_elf_backend_data (output_bfd);
4988 if (bed->elf_backend_always_size_sections
4989 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4990 return FALSE;
4992 dynobj = elf_hash_table (info)->dynobj;
4994 /* If there were no dynamic objects in the link, there is nothing to
4995 do here. */
4996 if (dynobj == NULL)
4997 return TRUE;
4999 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5000 return FALSE;
5002 if (elf_hash_table (info)->dynamic_sections_created)
5004 struct elf_info_failed eif;
5005 struct elf_link_hash_entry *h;
5006 asection *dynstr;
5007 struct bfd_elf_version_tree *t;
5008 struct bfd_elf_version_expr *d;
5009 asection *s;
5010 bfd_boolean all_defined;
5012 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5013 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5015 if (soname != NULL)
5017 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5018 soname, TRUE);
5019 if (soname_indx == (bfd_size_type) -1
5020 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5021 return FALSE;
5024 if (info->symbolic)
5026 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5027 return FALSE;
5028 info->flags |= DF_SYMBOLIC;
5031 if (rpath != NULL)
5033 bfd_size_type indx;
5035 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5036 TRUE);
5037 if (indx == (bfd_size_type) -1
5038 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5039 return FALSE;
5041 if (info->new_dtags)
5043 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5044 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5045 return FALSE;
5049 if (filter_shlib != NULL)
5051 bfd_size_type indx;
5053 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5054 filter_shlib, TRUE);
5055 if (indx == (bfd_size_type) -1
5056 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5057 return FALSE;
5060 if (auxiliary_filters != NULL)
5062 const char * const *p;
5064 for (p = auxiliary_filters; *p != NULL; p++)
5066 bfd_size_type indx;
5068 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5069 *p, TRUE);
5070 if (indx == (bfd_size_type) -1
5071 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5072 return FALSE;
5076 eif.info = info;
5077 eif.verdefs = verdefs;
5078 eif.failed = FALSE;
5080 /* If we are supposed to export all symbols into the dynamic symbol
5081 table (this is not the normal case), then do so. */
5082 if (info->export_dynamic)
5084 elf_link_hash_traverse (elf_hash_table (info),
5085 _bfd_elf_export_symbol,
5086 &eif);
5087 if (eif.failed)
5088 return FALSE;
5091 /* Make all global versions with definition. */
5092 for (t = verdefs; t != NULL; t = t->next)
5093 for (d = t->globals.list; d != NULL; d = d->next)
5094 if (!d->symver && d->symbol)
5096 const char *verstr, *name;
5097 size_t namelen, verlen, newlen;
5098 char *newname, *p;
5099 struct elf_link_hash_entry *newh;
5101 name = d->symbol;
5102 namelen = strlen (name);
5103 verstr = t->name;
5104 verlen = strlen (verstr);
5105 newlen = namelen + verlen + 3;
5107 newname = bfd_malloc (newlen);
5108 if (newname == NULL)
5109 return FALSE;
5110 memcpy (newname, name, namelen);
5112 /* Check the hidden versioned definition. */
5113 p = newname + namelen;
5114 *p++ = ELF_VER_CHR;
5115 memcpy (p, verstr, verlen + 1);
5116 newh = elf_link_hash_lookup (elf_hash_table (info),
5117 newname, FALSE, FALSE,
5118 FALSE);
5119 if (newh == NULL
5120 || (newh->root.type != bfd_link_hash_defined
5121 && newh->root.type != bfd_link_hash_defweak))
5123 /* Check the default versioned definition. */
5124 *p++ = ELF_VER_CHR;
5125 memcpy (p, verstr, verlen + 1);
5126 newh = elf_link_hash_lookup (elf_hash_table (info),
5127 newname, FALSE, FALSE,
5128 FALSE);
5130 free (newname);
5132 /* Mark this version if there is a definition and it is
5133 not defined in a shared object. */
5134 if (newh != NULL
5135 && !newh->def_dynamic
5136 && (newh->root.type == bfd_link_hash_defined
5137 || newh->root.type == bfd_link_hash_defweak))
5138 d->symver = 1;
5141 /* Attach all the symbols to their version information. */
5142 asvinfo.output_bfd = output_bfd;
5143 asvinfo.info = info;
5144 asvinfo.verdefs = verdefs;
5145 asvinfo.failed = FALSE;
5147 elf_link_hash_traverse (elf_hash_table (info),
5148 _bfd_elf_link_assign_sym_version,
5149 &asvinfo);
5150 if (asvinfo.failed)
5151 return FALSE;
5153 if (!info->allow_undefined_version)
5155 /* Check if all global versions have a definition. */
5156 all_defined = TRUE;
5157 for (t = verdefs; t != NULL; t = t->next)
5158 for (d = t->globals.list; d != NULL; d = d->next)
5159 if (!d->symver && !d->script)
5161 (*_bfd_error_handler)
5162 (_("%s: undefined version: %s"),
5163 d->pattern, t->name);
5164 all_defined = FALSE;
5167 if (!all_defined)
5169 bfd_set_error (bfd_error_bad_value);
5170 return FALSE;
5174 /* Find all symbols which were defined in a dynamic object and make
5175 the backend pick a reasonable value for them. */
5176 elf_link_hash_traverse (elf_hash_table (info),
5177 _bfd_elf_adjust_dynamic_symbol,
5178 &eif);
5179 if (eif.failed)
5180 return FALSE;
5182 /* Add some entries to the .dynamic section. We fill in some of the
5183 values later, in bfd_elf_final_link, but we must add the entries
5184 now so that we know the final size of the .dynamic section. */
5186 /* If there are initialization and/or finalization functions to
5187 call then add the corresponding DT_INIT/DT_FINI entries. */
5188 h = (info->init_function
5189 ? elf_link_hash_lookup (elf_hash_table (info),
5190 info->init_function, FALSE,
5191 FALSE, FALSE)
5192 : NULL);
5193 if (h != NULL
5194 && (h->ref_regular
5195 || h->def_regular))
5197 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5198 return FALSE;
5200 h = (info->fini_function
5201 ? elf_link_hash_lookup (elf_hash_table (info),
5202 info->fini_function, FALSE,
5203 FALSE, FALSE)
5204 : NULL);
5205 if (h != NULL
5206 && (h->ref_regular
5207 || h->def_regular))
5209 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5210 return FALSE;
5213 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5214 if (s != NULL && s->linker_has_input)
5216 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5217 if (! info->executable)
5219 bfd *sub;
5220 asection *o;
5222 for (sub = info->input_bfds; sub != NULL;
5223 sub = sub->link_next)
5224 for (o = sub->sections; o != NULL; o = o->next)
5225 if (elf_section_data (o)->this_hdr.sh_type
5226 == SHT_PREINIT_ARRAY)
5228 (*_bfd_error_handler)
5229 (_("%B: .preinit_array section is not allowed in DSO"),
5230 sub);
5231 break;
5234 bfd_set_error (bfd_error_nonrepresentable_section);
5235 return FALSE;
5238 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5239 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5240 return FALSE;
5242 s = bfd_get_section_by_name (output_bfd, ".init_array");
5243 if (s != NULL && s->linker_has_input)
5245 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5246 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5247 return FALSE;
5249 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5250 if (s != NULL && s->linker_has_input)
5252 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5253 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5254 return FALSE;
5257 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5258 /* If .dynstr is excluded from the link, we don't want any of
5259 these tags. Strictly, we should be checking each section
5260 individually; This quick check covers for the case where
5261 someone does a /DISCARD/ : { *(*) }. */
5262 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5264 bfd_size_type strsize;
5266 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5267 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
5268 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5269 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5270 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5271 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5272 bed->s->sizeof_sym))
5273 return FALSE;
5277 /* The backend must work out the sizes of all the other dynamic
5278 sections. */
5279 if (bed->elf_backend_size_dynamic_sections
5280 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5281 return FALSE;
5283 if (elf_hash_table (info)->dynamic_sections_created)
5285 unsigned long section_sym_count;
5286 asection *s;
5288 /* Set up the version definition section. */
5289 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5290 BFD_ASSERT (s != NULL);
5292 /* We may have created additional version definitions if we are
5293 just linking a regular application. */
5294 verdefs = asvinfo.verdefs;
5296 /* Skip anonymous version tag. */
5297 if (verdefs != NULL && verdefs->vernum == 0)
5298 verdefs = verdefs->next;
5300 if (verdefs == NULL && !info->create_default_symver)
5301 s->flags |= SEC_EXCLUDE;
5302 else
5304 unsigned int cdefs;
5305 bfd_size_type size;
5306 struct bfd_elf_version_tree *t;
5307 bfd_byte *p;
5308 Elf_Internal_Verdef def;
5309 Elf_Internal_Verdaux defaux;
5310 struct bfd_link_hash_entry *bh;
5311 struct elf_link_hash_entry *h;
5312 const char *name;
5314 cdefs = 0;
5315 size = 0;
5317 /* Make space for the base version. */
5318 size += sizeof (Elf_External_Verdef);
5319 size += sizeof (Elf_External_Verdaux);
5320 ++cdefs;
5322 /* Make space for the default version. */
5323 if (info->create_default_symver)
5325 size += sizeof (Elf_External_Verdef);
5326 ++cdefs;
5329 for (t = verdefs; t != NULL; t = t->next)
5331 struct bfd_elf_version_deps *n;
5333 size += sizeof (Elf_External_Verdef);
5334 size += sizeof (Elf_External_Verdaux);
5335 ++cdefs;
5337 for (n = t->deps; n != NULL; n = n->next)
5338 size += sizeof (Elf_External_Verdaux);
5341 s->size = size;
5342 s->contents = bfd_alloc (output_bfd, s->size);
5343 if (s->contents == NULL && s->size != 0)
5344 return FALSE;
5346 /* Fill in the version definition section. */
5348 p = s->contents;
5350 def.vd_version = VER_DEF_CURRENT;
5351 def.vd_flags = VER_FLG_BASE;
5352 def.vd_ndx = 1;
5353 def.vd_cnt = 1;
5354 if (info->create_default_symver)
5356 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5357 def.vd_next = sizeof (Elf_External_Verdef);
5359 else
5361 def.vd_aux = sizeof (Elf_External_Verdef);
5362 def.vd_next = (sizeof (Elf_External_Verdef)
5363 + sizeof (Elf_External_Verdaux));
5366 if (soname_indx != (bfd_size_type) -1)
5368 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5369 soname_indx);
5370 def.vd_hash = bfd_elf_hash (soname);
5371 defaux.vda_name = soname_indx;
5372 name = soname;
5374 else
5376 bfd_size_type indx;
5378 name = lbasename (output_bfd->filename);
5379 def.vd_hash = bfd_elf_hash (name);
5380 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5381 name, FALSE);
5382 if (indx == (bfd_size_type) -1)
5383 return FALSE;
5384 defaux.vda_name = indx;
5386 defaux.vda_next = 0;
5388 _bfd_elf_swap_verdef_out (output_bfd, &def,
5389 (Elf_External_Verdef *) p);
5390 p += sizeof (Elf_External_Verdef);
5391 if (info->create_default_symver)
5393 /* Add a symbol representing this version. */
5394 bh = NULL;
5395 if (! (_bfd_generic_link_add_one_symbol
5396 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5397 0, NULL, FALSE,
5398 get_elf_backend_data (dynobj)->collect, &bh)))
5399 return FALSE;
5400 h = (struct elf_link_hash_entry *) bh;
5401 h->non_elf = 0;
5402 h->def_regular = 1;
5403 h->type = STT_OBJECT;
5404 h->verinfo.vertree = NULL;
5406 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5407 return FALSE;
5409 /* Create a duplicate of the base version with the same
5410 aux block, but different flags. */
5411 def.vd_flags = 0;
5412 def.vd_ndx = 2;
5413 def.vd_aux = sizeof (Elf_External_Verdef);
5414 if (verdefs)
5415 def.vd_next = (sizeof (Elf_External_Verdef)
5416 + sizeof (Elf_External_Verdaux));
5417 else
5418 def.vd_next = 0;
5419 _bfd_elf_swap_verdef_out (output_bfd, &def,
5420 (Elf_External_Verdef *) p);
5421 p += sizeof (Elf_External_Verdef);
5423 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5424 (Elf_External_Verdaux *) p);
5425 p += sizeof (Elf_External_Verdaux);
5427 for (t = verdefs; t != NULL; t = t->next)
5429 unsigned int cdeps;
5430 struct bfd_elf_version_deps *n;
5432 cdeps = 0;
5433 for (n = t->deps; n != NULL; n = n->next)
5434 ++cdeps;
5436 /* Add a symbol representing this version. */
5437 bh = NULL;
5438 if (! (_bfd_generic_link_add_one_symbol
5439 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5440 0, NULL, FALSE,
5441 get_elf_backend_data (dynobj)->collect, &bh)))
5442 return FALSE;
5443 h = (struct elf_link_hash_entry *) bh;
5444 h->non_elf = 0;
5445 h->def_regular = 1;
5446 h->type = STT_OBJECT;
5447 h->verinfo.vertree = t;
5449 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5450 return FALSE;
5452 def.vd_version = VER_DEF_CURRENT;
5453 def.vd_flags = 0;
5454 if (t->globals.list == NULL
5455 && t->locals.list == NULL
5456 && ! t->used)
5457 def.vd_flags |= VER_FLG_WEAK;
5458 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5459 def.vd_cnt = cdeps + 1;
5460 def.vd_hash = bfd_elf_hash (t->name);
5461 def.vd_aux = sizeof (Elf_External_Verdef);
5462 def.vd_next = 0;
5463 if (t->next != NULL)
5464 def.vd_next = (sizeof (Elf_External_Verdef)
5465 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5467 _bfd_elf_swap_verdef_out (output_bfd, &def,
5468 (Elf_External_Verdef *) p);
5469 p += sizeof (Elf_External_Verdef);
5471 defaux.vda_name = h->dynstr_index;
5472 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5473 h->dynstr_index);
5474 defaux.vda_next = 0;
5475 if (t->deps != NULL)
5476 defaux.vda_next = sizeof (Elf_External_Verdaux);
5477 t->name_indx = defaux.vda_name;
5479 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5480 (Elf_External_Verdaux *) p);
5481 p += sizeof (Elf_External_Verdaux);
5483 for (n = t->deps; n != NULL; n = n->next)
5485 if (n->version_needed == NULL)
5487 /* This can happen if there was an error in the
5488 version script. */
5489 defaux.vda_name = 0;
5491 else
5493 defaux.vda_name = n->version_needed->name_indx;
5494 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5495 defaux.vda_name);
5497 if (n->next == NULL)
5498 defaux.vda_next = 0;
5499 else
5500 defaux.vda_next = sizeof (Elf_External_Verdaux);
5502 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5503 (Elf_External_Verdaux *) p);
5504 p += sizeof (Elf_External_Verdaux);
5508 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5509 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5510 return FALSE;
5512 elf_tdata (output_bfd)->cverdefs = cdefs;
5515 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5517 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5518 return FALSE;
5520 else if (info->flags & DF_BIND_NOW)
5522 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5523 return FALSE;
5526 if (info->flags_1)
5528 if (info->executable)
5529 info->flags_1 &= ~ (DF_1_INITFIRST
5530 | DF_1_NODELETE
5531 | DF_1_NOOPEN);
5532 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5533 return FALSE;
5536 /* Work out the size of the version reference section. */
5538 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5539 BFD_ASSERT (s != NULL);
5541 struct elf_find_verdep_info sinfo;
5543 sinfo.output_bfd = output_bfd;
5544 sinfo.info = info;
5545 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5546 if (sinfo.vers == 0)
5547 sinfo.vers = 1;
5548 sinfo.failed = FALSE;
5550 elf_link_hash_traverse (elf_hash_table (info),
5551 _bfd_elf_link_find_version_dependencies,
5552 &sinfo);
5554 if (elf_tdata (output_bfd)->verref == NULL)
5555 s->flags |= SEC_EXCLUDE;
5556 else
5558 Elf_Internal_Verneed *t;
5559 unsigned int size;
5560 unsigned int crefs;
5561 bfd_byte *p;
5563 /* Build the version definition section. */
5564 size = 0;
5565 crefs = 0;
5566 for (t = elf_tdata (output_bfd)->verref;
5567 t != NULL;
5568 t = t->vn_nextref)
5570 Elf_Internal_Vernaux *a;
5572 size += sizeof (Elf_External_Verneed);
5573 ++crefs;
5574 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5575 size += sizeof (Elf_External_Vernaux);
5578 s->size = size;
5579 s->contents = bfd_alloc (output_bfd, s->size);
5580 if (s->contents == NULL)
5581 return FALSE;
5583 p = s->contents;
5584 for (t = elf_tdata (output_bfd)->verref;
5585 t != NULL;
5586 t = t->vn_nextref)
5588 unsigned int caux;
5589 Elf_Internal_Vernaux *a;
5590 bfd_size_type indx;
5592 caux = 0;
5593 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5594 ++caux;
5596 t->vn_version = VER_NEED_CURRENT;
5597 t->vn_cnt = caux;
5598 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5599 elf_dt_name (t->vn_bfd) != NULL
5600 ? elf_dt_name (t->vn_bfd)
5601 : lbasename (t->vn_bfd->filename),
5602 FALSE);
5603 if (indx == (bfd_size_type) -1)
5604 return FALSE;
5605 t->vn_file = indx;
5606 t->vn_aux = sizeof (Elf_External_Verneed);
5607 if (t->vn_nextref == NULL)
5608 t->vn_next = 0;
5609 else
5610 t->vn_next = (sizeof (Elf_External_Verneed)
5611 + caux * sizeof (Elf_External_Vernaux));
5613 _bfd_elf_swap_verneed_out (output_bfd, t,
5614 (Elf_External_Verneed *) p);
5615 p += sizeof (Elf_External_Verneed);
5617 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5619 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5620 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5621 a->vna_nodename, FALSE);
5622 if (indx == (bfd_size_type) -1)
5623 return FALSE;
5624 a->vna_name = indx;
5625 if (a->vna_nextptr == NULL)
5626 a->vna_next = 0;
5627 else
5628 a->vna_next = sizeof (Elf_External_Vernaux);
5630 _bfd_elf_swap_vernaux_out (output_bfd, a,
5631 (Elf_External_Vernaux *) p);
5632 p += sizeof (Elf_External_Vernaux);
5636 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5637 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5638 return FALSE;
5640 elf_tdata (output_bfd)->cverrefs = crefs;
5644 if ((elf_tdata (output_bfd)->cverrefs == 0
5645 && elf_tdata (output_bfd)->cverdefs == 0)
5646 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5647 &section_sym_count) == 0)
5649 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5650 s->flags |= SEC_EXCLUDE;
5653 return TRUE;
5656 bfd_boolean
5657 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5659 if (!is_elf_hash_table (info->hash))
5660 return TRUE;
5662 if (elf_hash_table (info)->dynamic_sections_created)
5664 bfd *dynobj;
5665 const struct elf_backend_data *bed;
5666 asection *s;
5667 bfd_size_type dynsymcount;
5668 unsigned long section_sym_count;
5669 size_t bucketcount = 0;
5670 size_t hash_entry_size;
5671 unsigned int dtagcount;
5673 dynobj = elf_hash_table (info)->dynobj;
5675 /* Assign dynsym indicies. In a shared library we generate a
5676 section symbol for each output section, which come first.
5677 Next come all of the back-end allocated local dynamic syms,
5678 followed by the rest of the global symbols. */
5680 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5681 &section_sym_count);
5683 /* Work out the size of the symbol version section. */
5684 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5685 BFD_ASSERT (s != NULL);
5686 if (dynsymcount != 0
5687 && (s->flags & SEC_EXCLUDE) == 0)
5689 s->size = dynsymcount * sizeof (Elf_External_Versym);
5690 s->contents = bfd_zalloc (output_bfd, s->size);
5691 if (s->contents == NULL)
5692 return FALSE;
5694 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5695 return FALSE;
5698 /* Set the size of the .dynsym and .hash sections. We counted
5699 the number of dynamic symbols in elf_link_add_object_symbols.
5700 We will build the contents of .dynsym and .hash when we build
5701 the final symbol table, because until then we do not know the
5702 correct value to give the symbols. We built the .dynstr
5703 section as we went along in elf_link_add_object_symbols. */
5704 s = bfd_get_section_by_name (dynobj, ".dynsym");
5705 BFD_ASSERT (s != NULL);
5706 bed = get_elf_backend_data (output_bfd);
5707 s->size = dynsymcount * bed->s->sizeof_sym;
5709 if (dynsymcount != 0)
5711 s->contents = bfd_alloc (output_bfd, s->size);
5712 if (s->contents == NULL)
5713 return FALSE;
5715 /* The first entry in .dynsym is a dummy symbol.
5716 Clear all the section syms, in case we don't output them all. */
5717 ++section_sym_count;
5718 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5721 /* Compute the size of the hashing table. As a side effect this
5722 computes the hash values for all the names we export. */
5723 bucketcount = compute_bucket_count (info);
5725 s = bfd_get_section_by_name (dynobj, ".hash");
5726 BFD_ASSERT (s != NULL);
5727 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5728 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5729 s->contents = bfd_zalloc (output_bfd, s->size);
5730 if (s->contents == NULL)
5731 return FALSE;
5733 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5734 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5735 s->contents + hash_entry_size);
5737 elf_hash_table (info)->bucketcount = bucketcount;
5739 s = bfd_get_section_by_name (dynobj, ".dynstr");
5740 BFD_ASSERT (s != NULL);
5742 elf_finalize_dynstr (output_bfd, info);
5744 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5746 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5747 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5748 return FALSE;
5751 return TRUE;
5754 /* Final phase of ELF linker. */
5756 /* A structure we use to avoid passing large numbers of arguments. */
5758 struct elf_final_link_info
5760 /* General link information. */
5761 struct bfd_link_info *info;
5762 /* Output BFD. */
5763 bfd *output_bfd;
5764 /* Symbol string table. */
5765 struct bfd_strtab_hash *symstrtab;
5766 /* .dynsym section. */
5767 asection *dynsym_sec;
5768 /* .hash section. */
5769 asection *hash_sec;
5770 /* symbol version section (.gnu.version). */
5771 asection *symver_sec;
5772 /* Buffer large enough to hold contents of any section. */
5773 bfd_byte *contents;
5774 /* Buffer large enough to hold external relocs of any section. */
5775 void *external_relocs;
5776 /* Buffer large enough to hold internal relocs of any section. */
5777 Elf_Internal_Rela *internal_relocs;
5778 /* Buffer large enough to hold external local symbols of any input
5779 BFD. */
5780 bfd_byte *external_syms;
5781 /* And a buffer for symbol section indices. */
5782 Elf_External_Sym_Shndx *locsym_shndx;
5783 /* Buffer large enough to hold internal local symbols of any input
5784 BFD. */
5785 Elf_Internal_Sym *internal_syms;
5786 /* Array large enough to hold a symbol index for each local symbol
5787 of any input BFD. */
5788 long *indices;
5789 /* Array large enough to hold a section pointer for each local
5790 symbol of any input BFD. */
5791 asection **sections;
5792 /* Buffer to hold swapped out symbols. */
5793 bfd_byte *symbuf;
5794 /* And one for symbol section indices. */
5795 Elf_External_Sym_Shndx *symshndxbuf;
5796 /* Number of swapped out symbols in buffer. */
5797 size_t symbuf_count;
5798 /* Number of symbols which fit in symbuf. */
5799 size_t symbuf_size;
5800 /* And same for symshndxbuf. */
5801 size_t shndxbuf_size;
5804 /* This struct is used to pass information to elf_link_output_extsym. */
5806 struct elf_outext_info
5808 bfd_boolean failed;
5809 bfd_boolean localsyms;
5810 struct elf_final_link_info *finfo;
5813 /* When performing a relocatable link, the input relocations are
5814 preserved. But, if they reference global symbols, the indices
5815 referenced must be updated. Update all the relocations in
5816 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5818 static void
5819 elf_link_adjust_relocs (bfd *abfd,
5820 Elf_Internal_Shdr *rel_hdr,
5821 unsigned int count,
5822 struct elf_link_hash_entry **rel_hash)
5824 unsigned int i;
5825 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5826 bfd_byte *erela;
5827 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5828 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5829 bfd_vma r_type_mask;
5830 int r_sym_shift;
5832 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5834 swap_in = bed->s->swap_reloc_in;
5835 swap_out = bed->s->swap_reloc_out;
5837 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5839 swap_in = bed->s->swap_reloca_in;
5840 swap_out = bed->s->swap_reloca_out;
5842 else
5843 abort ();
5845 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5846 abort ();
5848 if (bed->s->arch_size == 32)
5850 r_type_mask = 0xff;
5851 r_sym_shift = 8;
5853 else
5855 r_type_mask = 0xffffffff;
5856 r_sym_shift = 32;
5859 erela = rel_hdr->contents;
5860 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5862 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5863 unsigned int j;
5865 if (*rel_hash == NULL)
5866 continue;
5868 BFD_ASSERT ((*rel_hash)->indx >= 0);
5870 (*swap_in) (abfd, erela, irela);
5871 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5872 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5873 | (irela[j].r_info & r_type_mask));
5874 (*swap_out) (abfd, irela, erela);
5878 struct elf_link_sort_rela
5880 union {
5881 bfd_vma offset;
5882 bfd_vma sym_mask;
5883 } u;
5884 enum elf_reloc_type_class type;
5885 /* We use this as an array of size int_rels_per_ext_rel. */
5886 Elf_Internal_Rela rela[1];
5889 static int
5890 elf_link_sort_cmp1 (const void *A, const void *B)
5892 const struct elf_link_sort_rela *a = A;
5893 const struct elf_link_sort_rela *b = B;
5894 int relativea, relativeb;
5896 relativea = a->type == reloc_class_relative;
5897 relativeb = b->type == reloc_class_relative;
5899 if (relativea < relativeb)
5900 return 1;
5901 if (relativea > relativeb)
5902 return -1;
5903 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5904 return -1;
5905 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5906 return 1;
5907 if (a->rela->r_offset < b->rela->r_offset)
5908 return -1;
5909 if (a->rela->r_offset > b->rela->r_offset)
5910 return 1;
5911 return 0;
5914 static int
5915 elf_link_sort_cmp2 (const void *A, const void *B)
5917 const struct elf_link_sort_rela *a = A;
5918 const struct elf_link_sort_rela *b = B;
5919 int copya, copyb;
5921 if (a->u.offset < b->u.offset)
5922 return -1;
5923 if (a->u.offset > b->u.offset)
5924 return 1;
5925 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5926 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5927 if (copya < copyb)
5928 return -1;
5929 if (copya > copyb)
5930 return 1;
5931 if (a->rela->r_offset < b->rela->r_offset)
5932 return -1;
5933 if (a->rela->r_offset > b->rela->r_offset)
5934 return 1;
5935 return 0;
5938 static size_t
5939 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5941 asection *reldyn;
5942 bfd_size_type count, size;
5943 size_t i, ret, sort_elt, ext_size;
5944 bfd_byte *sort, *s_non_relative, *p;
5945 struct elf_link_sort_rela *sq;
5946 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5947 int i2e = bed->s->int_rels_per_ext_rel;
5948 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5949 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5950 struct bfd_link_order *lo;
5951 bfd_vma r_sym_mask;
5953 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5954 if (reldyn == NULL || reldyn->size == 0)
5956 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5957 if (reldyn == NULL || reldyn->size == 0)
5958 return 0;
5959 ext_size = bed->s->sizeof_rel;
5960 swap_in = bed->s->swap_reloc_in;
5961 swap_out = bed->s->swap_reloc_out;
5963 else
5965 ext_size = bed->s->sizeof_rela;
5966 swap_in = bed->s->swap_reloca_in;
5967 swap_out = bed->s->swap_reloca_out;
5969 count = reldyn->size / ext_size;
5971 size = 0;
5972 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
5973 if (lo->type == bfd_indirect_link_order)
5975 asection *o = lo->u.indirect.section;
5976 size += o->size;
5979 if (size != reldyn->size)
5980 return 0;
5982 sort_elt = (sizeof (struct elf_link_sort_rela)
5983 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5984 sort = bfd_zmalloc (sort_elt * count);
5985 if (sort == NULL)
5987 (*info->callbacks->warning)
5988 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5989 return 0;
5992 if (bed->s->arch_size == 32)
5993 r_sym_mask = ~(bfd_vma) 0xff;
5994 else
5995 r_sym_mask = ~(bfd_vma) 0xffffffff;
5997 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
5998 if (lo->type == bfd_indirect_link_order)
6000 bfd_byte *erel, *erelend;
6001 asection *o = lo->u.indirect.section;
6003 if (o->contents == NULL && o->size != 0)
6005 /* This is a reloc section that is being handled as a normal
6006 section. See bfd_section_from_shdr. We can't combine
6007 relocs in this case. */
6008 free (sort);
6009 return 0;
6011 erel = o->contents;
6012 erelend = o->contents + o->size;
6013 p = sort + o->output_offset / ext_size * sort_elt;
6014 while (erel < erelend)
6016 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6017 (*swap_in) (abfd, erel, s->rela);
6018 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
6019 s->u.sym_mask = r_sym_mask;
6020 p += sort_elt;
6021 erel += ext_size;
6025 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
6027 for (i = 0, p = sort; i < count; i++, p += sort_elt)
6029 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6030 if (s->type != reloc_class_relative)
6031 break;
6033 ret = i;
6034 s_non_relative = p;
6036 sq = (struct elf_link_sort_rela *) s_non_relative;
6037 for (; i < count; i++, p += sort_elt)
6039 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
6040 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
6041 sq = sp;
6042 sp->u.offset = sq->rela->r_offset;
6045 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
6047 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6048 if (lo->type == bfd_indirect_link_order)
6050 bfd_byte *erel, *erelend;
6051 asection *o = lo->u.indirect.section;
6053 erel = o->contents;
6054 erelend = o->contents + o->size;
6055 p = sort + o->output_offset / ext_size * sort_elt;
6056 while (erel < erelend)
6058 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6059 (*swap_out) (abfd, s->rela, erel);
6060 p += sort_elt;
6061 erel += ext_size;
6065 free (sort);
6066 *psec = reldyn;
6067 return ret;
6070 /* Flush the output symbols to the file. */
6072 static bfd_boolean
6073 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
6074 const struct elf_backend_data *bed)
6076 if (finfo->symbuf_count > 0)
6078 Elf_Internal_Shdr *hdr;
6079 file_ptr pos;
6080 bfd_size_type amt;
6082 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6083 pos = hdr->sh_offset + hdr->sh_size;
6084 amt = finfo->symbuf_count * bed->s->sizeof_sym;
6085 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6086 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
6087 return FALSE;
6089 hdr->sh_size += amt;
6090 finfo->symbuf_count = 0;
6093 return TRUE;
6096 /* Add a symbol to the output symbol table. */
6098 static bfd_boolean
6099 elf_link_output_sym (struct elf_final_link_info *finfo,
6100 const char *name,
6101 Elf_Internal_Sym *elfsym,
6102 asection *input_sec,
6103 struct elf_link_hash_entry *h)
6105 bfd_byte *dest;
6106 Elf_External_Sym_Shndx *destshndx;
6107 bfd_boolean (*output_symbol_hook)
6108 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
6109 struct elf_link_hash_entry *);
6110 const struct elf_backend_data *bed;
6112 bed = get_elf_backend_data (finfo->output_bfd);
6113 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
6114 if (output_symbol_hook != NULL)
6116 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
6117 return FALSE;
6120 if (name == NULL || *name == '\0')
6121 elfsym->st_name = 0;
6122 else if (input_sec->flags & SEC_EXCLUDE)
6123 elfsym->st_name = 0;
6124 else
6126 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
6127 name, TRUE, FALSE);
6128 if (elfsym->st_name == (unsigned long) -1)
6129 return FALSE;
6132 if (finfo->symbuf_count >= finfo->symbuf_size)
6134 if (! elf_link_flush_output_syms (finfo, bed))
6135 return FALSE;
6138 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
6139 destshndx = finfo->symshndxbuf;
6140 if (destshndx != NULL)
6142 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
6144 bfd_size_type amt;
6146 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
6147 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
6148 if (destshndx == NULL)
6149 return FALSE;
6150 memset ((char *) destshndx + amt, 0, amt);
6151 finfo->shndxbuf_size *= 2;
6153 destshndx += bfd_get_symcount (finfo->output_bfd);
6156 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
6157 finfo->symbuf_count += 1;
6158 bfd_get_symcount (finfo->output_bfd) += 1;
6160 return TRUE;
6163 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6164 allowing an unsatisfied unversioned symbol in the DSO to match a
6165 versioned symbol that would normally require an explicit version.
6166 We also handle the case that a DSO references a hidden symbol
6167 which may be satisfied by a versioned symbol in another DSO. */
6169 static bfd_boolean
6170 elf_link_check_versioned_symbol (struct bfd_link_info *info,
6171 const struct elf_backend_data *bed,
6172 struct elf_link_hash_entry *h)
6174 bfd *abfd;
6175 struct elf_link_loaded_list *loaded;
6177 if (!is_elf_hash_table (info->hash))
6178 return FALSE;
6180 switch (h->root.type)
6182 default:
6183 abfd = NULL;
6184 break;
6186 case bfd_link_hash_undefined:
6187 case bfd_link_hash_undefweak:
6188 abfd = h->root.u.undef.abfd;
6189 if ((abfd->flags & DYNAMIC) == 0
6190 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
6191 return FALSE;
6192 break;
6194 case bfd_link_hash_defined:
6195 case bfd_link_hash_defweak:
6196 abfd = h->root.u.def.section->owner;
6197 break;
6199 case bfd_link_hash_common:
6200 abfd = h->root.u.c.p->section->owner;
6201 break;
6203 BFD_ASSERT (abfd != NULL);
6205 for (loaded = elf_hash_table (info)->loaded;
6206 loaded != NULL;
6207 loaded = loaded->next)
6209 bfd *input;
6210 Elf_Internal_Shdr *hdr;
6211 bfd_size_type symcount;
6212 bfd_size_type extsymcount;
6213 bfd_size_type extsymoff;
6214 Elf_Internal_Shdr *versymhdr;
6215 Elf_Internal_Sym *isym;
6216 Elf_Internal_Sym *isymend;
6217 Elf_Internal_Sym *isymbuf;
6218 Elf_External_Versym *ever;
6219 Elf_External_Versym *extversym;
6221 input = loaded->abfd;
6223 /* We check each DSO for a possible hidden versioned definition. */
6224 if (input == abfd
6225 || (input->flags & DYNAMIC) == 0
6226 || elf_dynversym (input) == 0)
6227 continue;
6229 hdr = &elf_tdata (input)->dynsymtab_hdr;
6231 symcount = hdr->sh_size / bed->s->sizeof_sym;
6232 if (elf_bad_symtab (input))
6234 extsymcount = symcount;
6235 extsymoff = 0;
6237 else
6239 extsymcount = symcount - hdr->sh_info;
6240 extsymoff = hdr->sh_info;
6243 if (extsymcount == 0)
6244 continue;
6246 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6247 NULL, NULL, NULL);
6248 if (isymbuf == NULL)
6249 return FALSE;
6251 /* Read in any version definitions. */
6252 versymhdr = &elf_tdata (input)->dynversym_hdr;
6253 extversym = bfd_malloc (versymhdr->sh_size);
6254 if (extversym == NULL)
6255 goto error_ret;
6257 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6258 || (bfd_bread (extversym, versymhdr->sh_size, input)
6259 != versymhdr->sh_size))
6261 free (extversym);
6262 error_ret:
6263 free (isymbuf);
6264 return FALSE;
6267 ever = extversym + extsymoff;
6268 isymend = isymbuf + extsymcount;
6269 for (isym = isymbuf; isym < isymend; isym++, ever++)
6271 const char *name;
6272 Elf_Internal_Versym iver;
6273 unsigned short version_index;
6275 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6276 || isym->st_shndx == SHN_UNDEF)
6277 continue;
6279 name = bfd_elf_string_from_elf_section (input,
6280 hdr->sh_link,
6281 isym->st_name);
6282 if (strcmp (name, h->root.root.string) != 0)
6283 continue;
6285 _bfd_elf_swap_versym_in (input, ever, &iver);
6287 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6289 /* If we have a non-hidden versioned sym, then it should
6290 have provided a definition for the undefined sym. */
6291 abort ();
6294 version_index = iver.vs_vers & VERSYM_VERSION;
6295 if (version_index == 1 || version_index == 2)
6297 /* This is the base or first version. We can use it. */
6298 free (extversym);
6299 free (isymbuf);
6300 return TRUE;
6304 free (extversym);
6305 free (isymbuf);
6308 return FALSE;
6311 /* Add an external symbol to the symbol table. This is called from
6312 the hash table traversal routine. When generating a shared object,
6313 we go through the symbol table twice. The first time we output
6314 anything that might have been forced to local scope in a version
6315 script. The second time we output the symbols that are still
6316 global symbols. */
6318 static bfd_boolean
6319 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6321 struct elf_outext_info *eoinfo = data;
6322 struct elf_final_link_info *finfo = eoinfo->finfo;
6323 bfd_boolean strip;
6324 Elf_Internal_Sym sym;
6325 asection *input_sec;
6326 const struct elf_backend_data *bed;
6328 if (h->root.type == bfd_link_hash_warning)
6330 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6331 if (h->root.type == bfd_link_hash_new)
6332 return TRUE;
6335 /* Decide whether to output this symbol in this pass. */
6336 if (eoinfo->localsyms)
6338 if (!h->forced_local)
6339 return TRUE;
6341 else
6343 if (h->forced_local)
6344 return TRUE;
6347 bed = get_elf_backend_data (finfo->output_bfd);
6349 /* If we have an undefined symbol reference here then it must have
6350 come from a shared library that is being linked in. (Undefined
6351 references in regular files have already been handled). If we
6352 are reporting errors for this situation then do so now. */
6353 if (h->root.type == bfd_link_hash_undefined
6354 && h->ref_dynamic
6355 && !h->ref_regular
6356 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6357 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6359 if (! ((*finfo->info->callbacks->undefined_symbol)
6360 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6361 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6363 eoinfo->failed = TRUE;
6364 return FALSE;
6368 /* We should also warn if a forced local symbol is referenced from
6369 shared libraries. */
6370 if (! finfo->info->relocatable
6371 && (! finfo->info->shared)
6372 && h->forced_local
6373 && h->ref_dynamic
6374 && !h->dynamic_def
6375 && !h->dynamic_weak
6376 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6378 (*_bfd_error_handler)
6379 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6380 finfo->output_bfd,
6381 h->root.u.def.section == bfd_abs_section_ptr
6382 ? finfo->output_bfd : h->root.u.def.section->owner,
6383 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6384 ? "internal"
6385 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6386 ? "hidden" : "local",
6387 h->root.root.string);
6388 eoinfo->failed = TRUE;
6389 return FALSE;
6392 /* We don't want to output symbols that have never been mentioned by
6393 a regular file, or that we have been told to strip. However, if
6394 h->indx is set to -2, the symbol is used by a reloc and we must
6395 output it. */
6396 if (h->indx == -2)
6397 strip = FALSE;
6398 else if ((h->def_dynamic
6399 || h->ref_dynamic
6400 || h->root.type == bfd_link_hash_new)
6401 && !h->def_regular
6402 && !h->ref_regular)
6403 strip = TRUE;
6404 else if (finfo->info->strip == strip_all)
6405 strip = TRUE;
6406 else if (finfo->info->strip == strip_some
6407 && bfd_hash_lookup (finfo->info->keep_hash,
6408 h->root.root.string, FALSE, FALSE) == NULL)
6409 strip = TRUE;
6410 else if (finfo->info->strip_discarded
6411 && (h->root.type == bfd_link_hash_defined
6412 || h->root.type == bfd_link_hash_defweak)
6413 && elf_discarded_section (h->root.u.def.section))
6414 strip = TRUE;
6415 else
6416 strip = FALSE;
6418 /* If we're stripping it, and it's not a dynamic symbol, there's
6419 nothing else to do unless it is a forced local symbol. */
6420 if (strip
6421 && h->dynindx == -1
6422 && !h->forced_local)
6423 return TRUE;
6425 sym.st_value = 0;
6426 sym.st_size = h->size;
6427 sym.st_other = h->other;
6428 if (h->forced_local)
6429 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6430 else if (h->root.type == bfd_link_hash_undefweak
6431 || h->root.type == bfd_link_hash_defweak)
6432 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6433 else
6434 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6436 switch (h->root.type)
6438 default:
6439 case bfd_link_hash_new:
6440 case bfd_link_hash_warning:
6441 abort ();
6442 return FALSE;
6444 case bfd_link_hash_undefined:
6445 case bfd_link_hash_undefweak:
6446 input_sec = bfd_und_section_ptr;
6447 sym.st_shndx = SHN_UNDEF;
6448 break;
6450 case bfd_link_hash_defined:
6451 case bfd_link_hash_defweak:
6453 input_sec = h->root.u.def.section;
6454 if (input_sec->output_section != NULL)
6456 sym.st_shndx =
6457 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6458 input_sec->output_section);
6459 if (sym.st_shndx == SHN_BAD)
6461 (*_bfd_error_handler)
6462 (_("%B: could not find output section %A for input section %A"),
6463 finfo->output_bfd, input_sec->output_section, input_sec);
6464 eoinfo->failed = TRUE;
6465 return FALSE;
6468 /* ELF symbols in relocatable files are section relative,
6469 but in nonrelocatable files they are virtual
6470 addresses. */
6471 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6472 if (! finfo->info->relocatable)
6474 sym.st_value += input_sec->output_section->vma;
6475 if (h->type == STT_TLS)
6477 /* STT_TLS symbols are relative to PT_TLS segment
6478 base. */
6479 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6480 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6484 else
6486 BFD_ASSERT (input_sec->owner == NULL
6487 || (input_sec->owner->flags & DYNAMIC) != 0);
6488 sym.st_shndx = SHN_UNDEF;
6489 input_sec = bfd_und_section_ptr;
6492 break;
6494 case bfd_link_hash_common:
6495 input_sec = h->root.u.c.p->section;
6496 sym.st_shndx = bed->common_section_index (input_sec);
6497 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6498 break;
6500 case bfd_link_hash_indirect:
6501 /* These symbols are created by symbol versioning. They point
6502 to the decorated version of the name. For example, if the
6503 symbol foo@@GNU_1.2 is the default, which should be used when
6504 foo is used with no version, then we add an indirect symbol
6505 foo which points to foo@@GNU_1.2. We ignore these symbols,
6506 since the indirected symbol is already in the hash table. */
6507 return TRUE;
6510 /* Give the processor backend a chance to tweak the symbol value,
6511 and also to finish up anything that needs to be done for this
6512 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6513 forced local syms when non-shared is due to a historical quirk. */
6514 if ((h->dynindx != -1
6515 || h->forced_local)
6516 && ((finfo->info->shared
6517 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6518 || h->root.type != bfd_link_hash_undefweak))
6519 || !h->forced_local)
6520 && elf_hash_table (finfo->info)->dynamic_sections_created)
6522 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6523 (finfo->output_bfd, finfo->info, h, &sym)))
6525 eoinfo->failed = TRUE;
6526 return FALSE;
6530 /* If we are marking the symbol as undefined, and there are no
6531 non-weak references to this symbol from a regular object, then
6532 mark the symbol as weak undefined; if there are non-weak
6533 references, mark the symbol as strong. We can't do this earlier,
6534 because it might not be marked as undefined until the
6535 finish_dynamic_symbol routine gets through with it. */
6536 if (sym.st_shndx == SHN_UNDEF
6537 && h->ref_regular
6538 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6539 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6541 int bindtype;
6543 if (h->ref_regular_nonweak)
6544 bindtype = STB_GLOBAL;
6545 else
6546 bindtype = STB_WEAK;
6547 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6550 /* If a non-weak symbol with non-default visibility is not defined
6551 locally, it is a fatal error. */
6552 if (! finfo->info->relocatable
6553 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6554 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6555 && h->root.type == bfd_link_hash_undefined
6556 && !h->def_regular)
6558 (*_bfd_error_handler)
6559 (_("%B: %s symbol `%s' isn't defined"),
6560 finfo->output_bfd,
6561 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6562 ? "protected"
6563 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6564 ? "internal" : "hidden",
6565 h->root.root.string);
6566 eoinfo->failed = TRUE;
6567 return FALSE;
6570 /* If this symbol should be put in the .dynsym section, then put it
6571 there now. We already know the symbol index. We also fill in
6572 the entry in the .hash section. */
6573 if (h->dynindx != -1
6574 && elf_hash_table (finfo->info)->dynamic_sections_created)
6576 size_t bucketcount;
6577 size_t bucket;
6578 size_t hash_entry_size;
6579 bfd_byte *bucketpos;
6580 bfd_vma chain;
6581 bfd_byte *esym;
6583 sym.st_name = h->dynstr_index;
6584 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6585 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6587 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6588 bucket = h->u.elf_hash_value % bucketcount;
6589 hash_entry_size
6590 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6591 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6592 + (bucket + 2) * hash_entry_size);
6593 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6594 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6595 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6596 ((bfd_byte *) finfo->hash_sec->contents
6597 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6599 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6601 Elf_Internal_Versym iversym;
6602 Elf_External_Versym *eversym;
6604 if (!h->def_regular)
6606 if (h->verinfo.verdef == NULL)
6607 iversym.vs_vers = 0;
6608 else
6609 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6611 else
6613 if (h->verinfo.vertree == NULL)
6614 iversym.vs_vers = 1;
6615 else
6616 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6617 if (finfo->info->create_default_symver)
6618 iversym.vs_vers++;
6621 if (h->hidden)
6622 iversym.vs_vers |= VERSYM_HIDDEN;
6624 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6625 eversym += h->dynindx;
6626 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6630 /* If we're stripping it, then it was just a dynamic symbol, and
6631 there's nothing else to do. */
6632 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6633 return TRUE;
6635 h->indx = bfd_get_symcount (finfo->output_bfd);
6637 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6639 eoinfo->failed = TRUE;
6640 return FALSE;
6643 return TRUE;
6646 /* Return TRUE if special handling is done for relocs in SEC against
6647 symbols defined in discarded sections. */
6649 static bfd_boolean
6650 elf_section_ignore_discarded_relocs (asection *sec)
6652 const struct elf_backend_data *bed;
6654 switch (sec->sec_info_type)
6656 case ELF_INFO_TYPE_STABS:
6657 case ELF_INFO_TYPE_EH_FRAME:
6658 return TRUE;
6659 default:
6660 break;
6663 bed = get_elf_backend_data (sec->owner);
6664 if (bed->elf_backend_ignore_discarded_relocs != NULL
6665 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6666 return TRUE;
6668 return FALSE;
6671 /* Return a mask saying how ld should treat relocations in SEC against
6672 symbols defined in discarded sections. If this function returns
6673 COMPLAIN set, ld will issue a warning message. If this function
6674 returns PRETEND set, and the discarded section was link-once and the
6675 same size as the kept link-once section, ld will pretend that the
6676 symbol was actually defined in the kept section. Otherwise ld will
6677 zero the reloc (at least that is the intent, but some cooperation by
6678 the target dependent code is needed, particularly for REL targets). */
6680 unsigned int
6681 _bfd_elf_default_action_discarded (asection *sec)
6683 if (sec->flags & SEC_DEBUGGING)
6684 return PRETEND;
6686 if (strcmp (".eh_frame", sec->name) == 0)
6687 return 0;
6689 if (strcmp (".gcc_except_table", sec->name) == 0)
6690 return 0;
6692 return COMPLAIN | PRETEND;
6695 /* Find a match between a section and a member of a section group. */
6697 static asection *
6698 match_group_member (asection *sec, asection *group)
6700 asection *first = elf_next_in_group (group);
6701 asection *s = first;
6703 while (s != NULL)
6705 if (bfd_elf_match_symbols_in_sections (s, sec))
6706 return s;
6708 if (s == first)
6709 break;
6712 return NULL;
6715 /* Check if the kept section of a discarded section SEC can be used
6716 to replace it. Return the replacement if it is OK. Otherwise return
6717 NULL. */
6719 asection *
6720 _bfd_elf_check_kept_section (asection *sec)
6722 asection *kept;
6724 kept = sec->kept_section;
6725 if (kept != NULL)
6727 if (elf_sec_group (sec) != NULL)
6728 kept = match_group_member (sec, kept);
6729 if (kept != NULL && sec->size != kept->size)
6730 kept = NULL;
6732 return kept;
6735 /* Link an input file into the linker output file. This function
6736 handles all the sections and relocations of the input file at once.
6737 This is so that we only have to read the local symbols once, and
6738 don't have to keep them in memory. */
6740 static bfd_boolean
6741 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6743 bfd_boolean (*relocate_section)
6744 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6745 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6746 bfd *output_bfd;
6747 Elf_Internal_Shdr *symtab_hdr;
6748 size_t locsymcount;
6749 size_t extsymoff;
6750 Elf_Internal_Sym *isymbuf;
6751 Elf_Internal_Sym *isym;
6752 Elf_Internal_Sym *isymend;
6753 long *pindex;
6754 asection **ppsection;
6755 asection *o;
6756 const struct elf_backend_data *bed;
6757 bfd_boolean emit_relocs;
6758 struct elf_link_hash_entry **sym_hashes;
6760 output_bfd = finfo->output_bfd;
6761 bed = get_elf_backend_data (output_bfd);
6762 relocate_section = bed->elf_backend_relocate_section;
6764 /* If this is a dynamic object, we don't want to do anything here:
6765 we don't want the local symbols, and we don't want the section
6766 contents. */
6767 if ((input_bfd->flags & DYNAMIC) != 0)
6768 return TRUE;
6770 emit_relocs = (finfo->info->relocatable
6771 || finfo->info->emitrelocations);
6773 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6774 if (elf_bad_symtab (input_bfd))
6776 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6777 extsymoff = 0;
6779 else
6781 locsymcount = symtab_hdr->sh_info;
6782 extsymoff = symtab_hdr->sh_info;
6785 /* Read the local symbols. */
6786 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6787 if (isymbuf == NULL && locsymcount != 0)
6789 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6790 finfo->internal_syms,
6791 finfo->external_syms,
6792 finfo->locsym_shndx);
6793 if (isymbuf == NULL)
6794 return FALSE;
6797 /* Find local symbol sections and adjust values of symbols in
6798 SEC_MERGE sections. Write out those local symbols we know are
6799 going into the output file. */
6800 isymend = isymbuf + locsymcount;
6801 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6802 isym < isymend;
6803 isym++, pindex++, ppsection++)
6805 asection *isec;
6806 const char *name;
6807 Elf_Internal_Sym osym;
6809 *pindex = -1;
6811 if (elf_bad_symtab (input_bfd))
6813 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6815 *ppsection = NULL;
6816 continue;
6820 if (isym->st_shndx == SHN_UNDEF)
6821 isec = bfd_und_section_ptr;
6822 else if (isym->st_shndx < SHN_LORESERVE
6823 || isym->st_shndx > SHN_HIRESERVE)
6825 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6826 if (isec
6827 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6828 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6829 isym->st_value =
6830 _bfd_merged_section_offset (output_bfd, &isec,
6831 elf_section_data (isec)->sec_info,
6832 isym->st_value);
6834 else if (isym->st_shndx == SHN_ABS)
6835 isec = bfd_abs_section_ptr;
6836 else if (isym->st_shndx == SHN_COMMON)
6837 isec = bfd_com_section_ptr;
6838 else
6840 /* Don't attempt to output symbols with st_shnx in the
6841 reserved range other than SHN_ABS and SHN_COMMON. */
6842 *ppsection = NULL;
6843 continue;
6846 *ppsection = isec;
6848 /* Don't output the first, undefined, symbol. */
6849 if (ppsection == finfo->sections)
6850 continue;
6852 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6854 /* We never output section symbols. Instead, we use the
6855 section symbol of the corresponding section in the output
6856 file. */
6857 continue;
6860 /* If we are stripping all symbols, we don't want to output this
6861 one. */
6862 if (finfo->info->strip == strip_all)
6863 continue;
6865 /* If we are discarding all local symbols, we don't want to
6866 output this one. If we are generating a relocatable output
6867 file, then some of the local symbols may be required by
6868 relocs; we output them below as we discover that they are
6869 needed. */
6870 if (finfo->info->discard == discard_all)
6871 continue;
6873 /* If this symbol is defined in a section which we are
6874 discarding, we don't need to keep it. */
6875 if (isym->st_shndx != SHN_UNDEF
6876 && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6877 && (isec == NULL
6878 || bfd_section_removed_from_list (output_bfd,
6879 isec->output_section)))
6880 continue;
6882 /* Get the name of the symbol. */
6883 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6884 isym->st_name);
6885 if (name == NULL)
6886 return FALSE;
6888 /* See if we are discarding symbols with this name. */
6889 if ((finfo->info->strip == strip_some
6890 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6891 == NULL))
6892 || (((finfo->info->discard == discard_sec_merge
6893 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6894 || finfo->info->discard == discard_l)
6895 && bfd_is_local_label_name (input_bfd, name)))
6896 continue;
6898 /* If we get here, we are going to output this symbol. */
6900 osym = *isym;
6902 /* Adjust the section index for the output file. */
6903 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6904 isec->output_section);
6905 if (osym.st_shndx == SHN_BAD)
6906 return FALSE;
6908 *pindex = bfd_get_symcount (output_bfd);
6910 /* ELF symbols in relocatable files are section relative, but
6911 in executable files they are virtual addresses. Note that
6912 this code assumes that all ELF sections have an associated
6913 BFD section with a reasonable value for output_offset; below
6914 we assume that they also have a reasonable value for
6915 output_section. Any special sections must be set up to meet
6916 these requirements. */
6917 osym.st_value += isec->output_offset;
6918 if (! finfo->info->relocatable)
6920 osym.st_value += isec->output_section->vma;
6921 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6923 /* STT_TLS symbols are relative to PT_TLS segment base. */
6924 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6925 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6929 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6930 return FALSE;
6933 /* Relocate the contents of each section. */
6934 sym_hashes = elf_sym_hashes (input_bfd);
6935 for (o = input_bfd->sections; o != NULL; o = o->next)
6937 bfd_byte *contents;
6939 if (! o->linker_mark)
6941 /* This section was omitted from the link. */
6942 continue;
6945 if ((o->flags & SEC_HAS_CONTENTS) == 0
6946 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6947 continue;
6949 if ((o->flags & SEC_LINKER_CREATED) != 0)
6951 /* Section was created by _bfd_elf_link_create_dynamic_sections
6952 or somesuch. */
6953 continue;
6956 /* Get the contents of the section. They have been cached by a
6957 relaxation routine. Note that o is a section in an input
6958 file, so the contents field will not have been set by any of
6959 the routines which work on output files. */
6960 if (elf_section_data (o)->this_hdr.contents != NULL)
6961 contents = elf_section_data (o)->this_hdr.contents;
6962 else
6964 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6966 contents = finfo->contents;
6967 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
6968 return FALSE;
6971 if ((o->flags & SEC_RELOC) != 0)
6973 Elf_Internal_Rela *internal_relocs;
6974 bfd_vma r_type_mask;
6975 int r_sym_shift;
6977 /* Get the swapped relocs. */
6978 internal_relocs
6979 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
6980 finfo->internal_relocs, FALSE);
6981 if (internal_relocs == NULL
6982 && o->reloc_count > 0)
6983 return FALSE;
6985 if (bed->s->arch_size == 32)
6987 r_type_mask = 0xff;
6988 r_sym_shift = 8;
6990 else
6992 r_type_mask = 0xffffffff;
6993 r_sym_shift = 32;
6996 /* Run through the relocs looking for any against symbols
6997 from discarded sections and section symbols from
6998 removed link-once sections. Complain about relocs
6999 against discarded sections. Zero relocs against removed
7000 link-once sections. Preserve debug information as much
7001 as we can. */
7002 if (!elf_section_ignore_discarded_relocs (o))
7004 Elf_Internal_Rela *rel, *relend;
7005 unsigned int action = (*bed->action_discarded) (o);
7007 rel = internal_relocs;
7008 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
7009 for ( ; rel < relend; rel++)
7011 unsigned long r_symndx = rel->r_info >> r_sym_shift;
7012 asection **ps, *sec;
7013 struct elf_link_hash_entry *h = NULL;
7014 const char *sym_name;
7016 if (r_symndx == STN_UNDEF)
7017 continue;
7019 if (r_symndx >= locsymcount
7020 || (elf_bad_symtab (input_bfd)
7021 && finfo->sections[r_symndx] == NULL))
7023 h = sym_hashes[r_symndx - extsymoff];
7025 /* Badly formatted input files can contain relocs that
7026 reference non-existant symbols. Check here so that
7027 we do not seg fault. */
7028 if (h == NULL)
7030 char buffer [32];
7032 sprintf_vma (buffer, rel->r_info);
7033 (*_bfd_error_handler)
7034 (_("error: %B contains a reloc (0x%s) for section %A "
7035 "that references a non-existent global symbol"),
7036 input_bfd, o, buffer);
7037 bfd_set_error (bfd_error_bad_value);
7038 return FALSE;
7041 while (h->root.type == bfd_link_hash_indirect
7042 || h->root.type == bfd_link_hash_warning)
7043 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7045 if (h->root.type != bfd_link_hash_defined
7046 && h->root.type != bfd_link_hash_defweak)
7047 continue;
7049 ps = &h->root.u.def.section;
7050 sym_name = h->root.root.string;
7052 else
7054 Elf_Internal_Sym *sym = isymbuf + r_symndx;
7055 ps = &finfo->sections[r_symndx];
7056 sym_name = bfd_elf_sym_name (input_bfd,
7057 symtab_hdr,
7058 sym, *ps);
7061 /* Complain if the definition comes from a
7062 discarded section. */
7063 if ((sec = *ps) != NULL && elf_discarded_section (sec))
7065 BFD_ASSERT (r_symndx != 0);
7066 if (action & COMPLAIN)
7067 (*finfo->info->callbacks->einfo)
7068 (_("%X`%s' referenced in section `%A' of %B: "
7069 "defined in discarded section `%A' of %B\n"),
7070 sym_name, o, input_bfd, sec, sec->owner);
7072 /* Try to do the best we can to support buggy old
7073 versions of gcc. If we've warned, or this is
7074 debugging info, pretend that the symbol is
7075 really defined in the kept linkonce section.
7076 FIXME: This is quite broken. Modifying the
7077 symbol here means we will be changing all later
7078 uses of the symbol, not just in this section.
7079 The only thing that makes this half reasonable
7080 is that we warn in non-debug sections, and
7081 debug sections tend to come after other
7082 sections. */
7083 if (action & PRETEND)
7085 asection *kept;
7087 kept = _bfd_elf_check_kept_section (sec);
7088 if (kept != NULL)
7090 *ps = kept;
7091 continue;
7095 /* Remove the symbol reference from the reloc, but
7096 don't kill the reloc completely. This is so that
7097 a zero value will be written into the section,
7098 which may have non-zero contents put there by the
7099 assembler. Zero in things like an eh_frame fde
7100 pc_begin allows stack unwinders to recognize the
7101 fde as bogus. */
7102 rel->r_info &= r_type_mask;
7103 rel->r_addend = 0;
7108 /* Relocate the section by invoking a back end routine.
7110 The back end routine is responsible for adjusting the
7111 section contents as necessary, and (if using Rela relocs
7112 and generating a relocatable output file) adjusting the
7113 reloc addend as necessary.
7115 The back end routine does not have to worry about setting
7116 the reloc address or the reloc symbol index.
7118 The back end routine is given a pointer to the swapped in
7119 internal symbols, and can access the hash table entries
7120 for the external symbols via elf_sym_hashes (input_bfd).
7122 When generating relocatable output, the back end routine
7123 must handle STB_LOCAL/STT_SECTION symbols specially. The
7124 output symbol is going to be a section symbol
7125 corresponding to the output section, which will require
7126 the addend to be adjusted. */
7128 if (! (*relocate_section) (output_bfd, finfo->info,
7129 input_bfd, o, contents,
7130 internal_relocs,
7131 isymbuf,
7132 finfo->sections))
7133 return FALSE;
7135 if (emit_relocs)
7137 Elf_Internal_Rela *irela;
7138 Elf_Internal_Rela *irelaend;
7139 bfd_vma last_offset;
7140 struct elf_link_hash_entry **rel_hash;
7141 struct elf_link_hash_entry **rel_hash_list;
7142 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
7143 unsigned int next_erel;
7144 bfd_boolean rela_normal;
7146 input_rel_hdr = &elf_section_data (o)->rel_hdr;
7147 rela_normal = (bed->rela_normal
7148 && (input_rel_hdr->sh_entsize
7149 == bed->s->sizeof_rela));
7151 /* Adjust the reloc addresses and symbol indices. */
7153 irela = internal_relocs;
7154 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
7155 rel_hash = (elf_section_data (o->output_section)->rel_hashes
7156 + elf_section_data (o->output_section)->rel_count
7157 + elf_section_data (o->output_section)->rel_count2);
7158 rel_hash_list = rel_hash;
7159 last_offset = o->output_offset;
7160 if (!finfo->info->relocatable)
7161 last_offset += o->output_section->vma;
7162 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
7164 unsigned long r_symndx;
7165 asection *sec;
7166 Elf_Internal_Sym sym;
7168 if (next_erel == bed->s->int_rels_per_ext_rel)
7170 rel_hash++;
7171 next_erel = 0;
7174 irela->r_offset = _bfd_elf_section_offset (output_bfd,
7175 finfo->info, o,
7176 irela->r_offset);
7177 if (irela->r_offset >= (bfd_vma) -2)
7179 /* This is a reloc for a deleted entry or somesuch.
7180 Turn it into an R_*_NONE reloc, at the same
7181 offset as the last reloc. elf_eh_frame.c and
7182 elf_bfd_discard_info rely on reloc offsets
7183 being ordered. */
7184 irela->r_offset = last_offset;
7185 irela->r_info = 0;
7186 irela->r_addend = 0;
7187 continue;
7190 irela->r_offset += o->output_offset;
7192 /* Relocs in an executable have to be virtual addresses. */
7193 if (!finfo->info->relocatable)
7194 irela->r_offset += o->output_section->vma;
7196 last_offset = irela->r_offset;
7198 r_symndx = irela->r_info >> r_sym_shift;
7199 if (r_symndx == STN_UNDEF)
7200 continue;
7202 if (r_symndx >= locsymcount
7203 || (elf_bad_symtab (input_bfd)
7204 && finfo->sections[r_symndx] == NULL))
7206 struct elf_link_hash_entry *rh;
7207 unsigned long indx;
7209 /* This is a reloc against a global symbol. We
7210 have not yet output all the local symbols, so
7211 we do not know the symbol index of any global
7212 symbol. We set the rel_hash entry for this
7213 reloc to point to the global hash table entry
7214 for this symbol. The symbol index is then
7215 set at the end of bfd_elf_final_link. */
7216 indx = r_symndx - extsymoff;
7217 rh = elf_sym_hashes (input_bfd)[indx];
7218 while (rh->root.type == bfd_link_hash_indirect
7219 || rh->root.type == bfd_link_hash_warning)
7220 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7222 /* Setting the index to -2 tells
7223 elf_link_output_extsym that this symbol is
7224 used by a reloc. */
7225 BFD_ASSERT (rh->indx < 0);
7226 rh->indx = -2;
7228 *rel_hash = rh;
7230 continue;
7233 /* This is a reloc against a local symbol. */
7235 *rel_hash = NULL;
7236 sym = isymbuf[r_symndx];
7237 sec = finfo->sections[r_symndx];
7238 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7240 /* I suppose the backend ought to fill in the
7241 section of any STT_SECTION symbol against a
7242 processor specific section. */
7243 r_symndx = 0;
7244 if (bfd_is_abs_section (sec))
7246 else if (sec == NULL || sec->owner == NULL)
7248 bfd_set_error (bfd_error_bad_value);
7249 return FALSE;
7251 else
7253 asection *osec = sec->output_section;
7255 /* If we have discarded a section, the output
7256 section will be the absolute section. In
7257 case of discarded link-once and discarded
7258 SEC_MERGE sections, use the kept section. */
7259 if (bfd_is_abs_section (osec)
7260 && sec->kept_section != NULL
7261 && sec->kept_section->output_section != NULL)
7263 osec = sec->kept_section->output_section;
7264 irela->r_addend -= osec->vma;
7267 if (!bfd_is_abs_section (osec))
7269 r_symndx = osec->target_index;
7270 BFD_ASSERT (r_symndx != 0);
7274 /* Adjust the addend according to where the
7275 section winds up in the output section. */
7276 if (rela_normal)
7277 irela->r_addend += sec->output_offset;
7279 else
7281 if (finfo->indices[r_symndx] == -1)
7283 unsigned long shlink;
7284 const char *name;
7285 asection *osec;
7287 if (finfo->info->strip == strip_all)
7289 /* You can't do ld -r -s. */
7290 bfd_set_error (bfd_error_invalid_operation);
7291 return FALSE;
7294 /* This symbol was skipped earlier, but
7295 since it is needed by a reloc, we
7296 must output it now. */
7297 shlink = symtab_hdr->sh_link;
7298 name = (bfd_elf_string_from_elf_section
7299 (input_bfd, shlink, sym.st_name));
7300 if (name == NULL)
7301 return FALSE;
7303 osec = sec->output_section;
7304 sym.st_shndx =
7305 _bfd_elf_section_from_bfd_section (output_bfd,
7306 osec);
7307 if (sym.st_shndx == SHN_BAD)
7308 return FALSE;
7310 sym.st_value += sec->output_offset;
7311 if (! finfo->info->relocatable)
7313 sym.st_value += osec->vma;
7314 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7316 /* STT_TLS symbols are relative to PT_TLS
7317 segment base. */
7318 BFD_ASSERT (elf_hash_table (finfo->info)
7319 ->tls_sec != NULL);
7320 sym.st_value -= (elf_hash_table (finfo->info)
7321 ->tls_sec->vma);
7325 finfo->indices[r_symndx]
7326 = bfd_get_symcount (output_bfd);
7328 if (! elf_link_output_sym (finfo, name, &sym, sec,
7329 NULL))
7330 return FALSE;
7333 r_symndx = finfo->indices[r_symndx];
7336 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7337 | (irela->r_info & r_type_mask));
7340 /* Swap out the relocs. */
7341 if (input_rel_hdr->sh_size != 0
7342 && !bed->elf_backend_emit_relocs (output_bfd, o,
7343 input_rel_hdr,
7344 internal_relocs,
7345 rel_hash_list))
7346 return FALSE;
7348 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7349 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7351 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7352 * bed->s->int_rels_per_ext_rel);
7353 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
7354 if (!bed->elf_backend_emit_relocs (output_bfd, o,
7355 input_rel_hdr2,
7356 internal_relocs,
7357 rel_hash_list))
7358 return FALSE;
7363 /* Write out the modified section contents. */
7364 if (bed->elf_backend_write_section
7365 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7367 /* Section written out. */
7369 else switch (o->sec_info_type)
7371 case ELF_INFO_TYPE_STABS:
7372 if (! (_bfd_write_section_stabs
7373 (output_bfd,
7374 &elf_hash_table (finfo->info)->stab_info,
7375 o, &elf_section_data (o)->sec_info, contents)))
7376 return FALSE;
7377 break;
7378 case ELF_INFO_TYPE_MERGE:
7379 if (! _bfd_write_merged_section (output_bfd, o,
7380 elf_section_data (o)->sec_info))
7381 return FALSE;
7382 break;
7383 case ELF_INFO_TYPE_EH_FRAME:
7385 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7386 o, contents))
7387 return FALSE;
7389 break;
7390 default:
7392 if (! (o->flags & SEC_EXCLUDE)
7393 && ! bfd_set_section_contents (output_bfd, o->output_section,
7394 contents,
7395 (file_ptr) o->output_offset,
7396 o->size))
7397 return FALSE;
7399 break;
7403 return TRUE;
7406 /* Generate a reloc when linking an ELF file. This is a reloc
7407 requested by the linker, and does not come from any input file. This
7408 is used to build constructor and destructor tables when linking
7409 with -Ur. */
7411 static bfd_boolean
7412 elf_reloc_link_order (bfd *output_bfd,
7413 struct bfd_link_info *info,
7414 asection *output_section,
7415 struct bfd_link_order *link_order)
7417 reloc_howto_type *howto;
7418 long indx;
7419 bfd_vma offset;
7420 bfd_vma addend;
7421 struct elf_link_hash_entry **rel_hash_ptr;
7422 Elf_Internal_Shdr *rel_hdr;
7423 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7424 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7425 bfd_byte *erel;
7426 unsigned int i;
7428 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7429 if (howto == NULL)
7431 bfd_set_error (bfd_error_bad_value);
7432 return FALSE;
7435 addend = link_order->u.reloc.p->addend;
7437 /* Figure out the symbol index. */
7438 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7439 + elf_section_data (output_section)->rel_count
7440 + elf_section_data (output_section)->rel_count2);
7441 if (link_order->type == bfd_section_reloc_link_order)
7443 indx = link_order->u.reloc.p->u.section->target_index;
7444 BFD_ASSERT (indx != 0);
7445 *rel_hash_ptr = NULL;
7447 else
7449 struct elf_link_hash_entry *h;
7451 /* Treat a reloc against a defined symbol as though it were
7452 actually against the section. */
7453 h = ((struct elf_link_hash_entry *)
7454 bfd_wrapped_link_hash_lookup (output_bfd, info,
7455 link_order->u.reloc.p->u.name,
7456 FALSE, FALSE, TRUE));
7457 if (h != NULL
7458 && (h->root.type == bfd_link_hash_defined
7459 || h->root.type == bfd_link_hash_defweak))
7461 asection *section;
7463 section = h->root.u.def.section;
7464 indx = section->output_section->target_index;
7465 *rel_hash_ptr = NULL;
7466 /* It seems that we ought to add the symbol value to the
7467 addend here, but in practice it has already been added
7468 because it was passed to constructor_callback. */
7469 addend += section->output_section->vma + section->output_offset;
7471 else if (h != NULL)
7473 /* Setting the index to -2 tells elf_link_output_extsym that
7474 this symbol is used by a reloc. */
7475 h->indx = -2;
7476 *rel_hash_ptr = h;
7477 indx = 0;
7479 else
7481 if (! ((*info->callbacks->unattached_reloc)
7482 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7483 return FALSE;
7484 indx = 0;
7488 /* If this is an inplace reloc, we must write the addend into the
7489 object file. */
7490 if (howto->partial_inplace && addend != 0)
7492 bfd_size_type size;
7493 bfd_reloc_status_type rstat;
7494 bfd_byte *buf;
7495 bfd_boolean ok;
7496 const char *sym_name;
7498 size = bfd_get_reloc_size (howto);
7499 buf = bfd_zmalloc (size);
7500 if (buf == NULL)
7501 return FALSE;
7502 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7503 switch (rstat)
7505 case bfd_reloc_ok:
7506 break;
7508 default:
7509 case bfd_reloc_outofrange:
7510 abort ();
7512 case bfd_reloc_overflow:
7513 if (link_order->type == bfd_section_reloc_link_order)
7514 sym_name = bfd_section_name (output_bfd,
7515 link_order->u.reloc.p->u.section);
7516 else
7517 sym_name = link_order->u.reloc.p->u.name;
7518 if (! ((*info->callbacks->reloc_overflow)
7519 (info, NULL, sym_name, howto->name, addend, NULL,
7520 NULL, (bfd_vma) 0)))
7522 free (buf);
7523 return FALSE;
7525 break;
7527 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7528 link_order->offset, size);
7529 free (buf);
7530 if (! ok)
7531 return FALSE;
7534 /* The address of a reloc is relative to the section in a
7535 relocatable file, and is a virtual address in an executable
7536 file. */
7537 offset = link_order->offset;
7538 if (! info->relocatable)
7539 offset += output_section->vma;
7541 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7543 irel[i].r_offset = offset;
7544 irel[i].r_info = 0;
7545 irel[i].r_addend = 0;
7547 if (bed->s->arch_size == 32)
7548 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7549 else
7550 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7552 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7553 erel = rel_hdr->contents;
7554 if (rel_hdr->sh_type == SHT_REL)
7556 erel += (elf_section_data (output_section)->rel_count
7557 * bed->s->sizeof_rel);
7558 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7560 else
7562 irel[0].r_addend = addend;
7563 erel += (elf_section_data (output_section)->rel_count
7564 * bed->s->sizeof_rela);
7565 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7568 ++elf_section_data (output_section)->rel_count;
7570 return TRUE;
7574 /* Get the output vma of the section pointed to by the sh_link field. */
7576 static bfd_vma
7577 elf_get_linked_section_vma (struct bfd_link_order *p)
7579 Elf_Internal_Shdr **elf_shdrp;
7580 asection *s;
7581 int elfsec;
7583 s = p->u.indirect.section;
7584 elf_shdrp = elf_elfsections (s->owner);
7585 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7586 elfsec = elf_shdrp[elfsec]->sh_link;
7587 /* PR 290:
7588 The Intel C compiler generates SHT_IA_64_UNWIND with
7589 SHF_LINK_ORDER. But it doesn't set the sh_link or
7590 sh_info fields. Hence we could get the situation
7591 where elfsec is 0. */
7592 if (elfsec == 0)
7594 const struct elf_backend_data *bed
7595 = get_elf_backend_data (s->owner);
7596 if (bed->link_order_error_handler)
7597 bed->link_order_error_handler
7598 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7599 return 0;
7601 else
7603 s = elf_shdrp[elfsec]->bfd_section;
7604 return s->output_section->vma + s->output_offset;
7609 /* Compare two sections based on the locations of the sections they are
7610 linked to. Used by elf_fixup_link_order. */
7612 static int
7613 compare_link_order (const void * a, const void * b)
7615 bfd_vma apos;
7616 bfd_vma bpos;
7618 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7619 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7620 if (apos < bpos)
7621 return -1;
7622 return apos > bpos;
7626 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7627 order as their linked sections. Returns false if this could not be done
7628 because an output section includes both ordered and unordered
7629 sections. Ideally we'd do this in the linker proper. */
7631 static bfd_boolean
7632 elf_fixup_link_order (bfd *abfd, asection *o)
7634 int seen_linkorder;
7635 int seen_other;
7636 int n;
7637 struct bfd_link_order *p;
7638 bfd *sub;
7639 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7640 int elfsec;
7641 struct bfd_link_order **sections;
7642 asection *s, *other_sec, *linkorder_sec;
7643 bfd_vma offset;
7645 other_sec = NULL;
7646 linkorder_sec = NULL;
7647 seen_other = 0;
7648 seen_linkorder = 0;
7649 for (p = o->map_head.link_order; p != NULL; p = p->next)
7651 if (p->type == bfd_indirect_link_order)
7653 s = p->u.indirect.section;
7654 sub = s->owner;
7655 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7656 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
7657 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) != -1
7658 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7660 seen_linkorder++;
7661 linkorder_sec = s;
7663 else
7665 seen_other++;
7666 other_sec = s;
7669 else
7670 seen_other++;
7672 if (seen_other && seen_linkorder)
7674 if (other_sec && linkorder_sec)
7675 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
7676 o, linkorder_sec,
7677 linkorder_sec->owner, other_sec,
7678 other_sec->owner);
7679 else
7680 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7682 bfd_set_error (bfd_error_bad_value);
7683 return FALSE;
7687 if (!seen_linkorder)
7688 return TRUE;
7690 sections = (struct bfd_link_order **)
7691 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7692 seen_linkorder = 0;
7694 for (p = o->map_head.link_order; p != NULL; p = p->next)
7696 sections[seen_linkorder++] = p;
7698 /* Sort the input sections in the order of their linked section. */
7699 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7700 compare_link_order);
7702 /* Change the offsets of the sections. */
7703 offset = 0;
7704 for (n = 0; n < seen_linkorder; n++)
7706 s = sections[n]->u.indirect.section;
7707 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7708 s->output_offset = offset;
7709 sections[n]->offset = offset;
7710 offset += sections[n]->size;
7713 return TRUE;
7717 /* Do the final step of an ELF link. */
7719 bfd_boolean
7720 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7722 bfd_boolean dynamic;
7723 bfd_boolean emit_relocs;
7724 bfd *dynobj;
7725 struct elf_final_link_info finfo;
7726 register asection *o;
7727 register struct bfd_link_order *p;
7728 register bfd *sub;
7729 bfd_size_type max_contents_size;
7730 bfd_size_type max_external_reloc_size;
7731 bfd_size_type max_internal_reloc_count;
7732 bfd_size_type max_sym_count;
7733 bfd_size_type max_sym_shndx_count;
7734 file_ptr off;
7735 Elf_Internal_Sym elfsym;
7736 unsigned int i;
7737 Elf_Internal_Shdr *symtab_hdr;
7738 Elf_Internal_Shdr *symtab_shndx_hdr;
7739 Elf_Internal_Shdr *symstrtab_hdr;
7740 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7741 struct elf_outext_info eoinfo;
7742 bfd_boolean merged;
7743 size_t relativecount = 0;
7744 asection *reldyn = 0;
7745 bfd_size_type amt;
7747 if (! is_elf_hash_table (info->hash))
7748 return FALSE;
7750 if (info->shared)
7751 abfd->flags |= DYNAMIC;
7753 dynamic = elf_hash_table (info)->dynamic_sections_created;
7754 dynobj = elf_hash_table (info)->dynobj;
7756 emit_relocs = (info->relocatable
7757 || info->emitrelocations
7758 || bed->elf_backend_emit_relocs);
7760 finfo.info = info;
7761 finfo.output_bfd = abfd;
7762 finfo.symstrtab = _bfd_elf_stringtab_init ();
7763 if (finfo.symstrtab == NULL)
7764 return FALSE;
7766 if (! dynamic)
7768 finfo.dynsym_sec = NULL;
7769 finfo.hash_sec = NULL;
7770 finfo.symver_sec = NULL;
7772 else
7774 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7775 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7776 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7777 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7778 /* Note that it is OK if symver_sec is NULL. */
7781 finfo.contents = NULL;
7782 finfo.external_relocs = NULL;
7783 finfo.internal_relocs = NULL;
7784 finfo.external_syms = NULL;
7785 finfo.locsym_shndx = NULL;
7786 finfo.internal_syms = NULL;
7787 finfo.indices = NULL;
7788 finfo.sections = NULL;
7789 finfo.symbuf = NULL;
7790 finfo.symshndxbuf = NULL;
7791 finfo.symbuf_count = 0;
7792 finfo.shndxbuf_size = 0;
7794 /* Count up the number of relocations we will output for each output
7795 section, so that we know the sizes of the reloc sections. We
7796 also figure out some maximum sizes. */
7797 max_contents_size = 0;
7798 max_external_reloc_size = 0;
7799 max_internal_reloc_count = 0;
7800 max_sym_count = 0;
7801 max_sym_shndx_count = 0;
7802 merged = FALSE;
7803 for (o = abfd->sections; o != NULL; o = o->next)
7805 struct bfd_elf_section_data *esdo = elf_section_data (o);
7806 o->reloc_count = 0;
7808 for (p = o->map_head.link_order; p != NULL; p = p->next)
7810 unsigned int reloc_count = 0;
7811 struct bfd_elf_section_data *esdi = NULL;
7812 unsigned int *rel_count1;
7814 if (p->type == bfd_section_reloc_link_order
7815 || p->type == bfd_symbol_reloc_link_order)
7816 reloc_count = 1;
7817 else if (p->type == bfd_indirect_link_order)
7819 asection *sec;
7821 sec = p->u.indirect.section;
7822 esdi = elf_section_data (sec);
7824 /* Mark all sections which are to be included in the
7825 link. This will normally be every section. We need
7826 to do this so that we can identify any sections which
7827 the linker has decided to not include. */
7828 sec->linker_mark = TRUE;
7830 if (sec->flags & SEC_MERGE)
7831 merged = TRUE;
7833 if (info->relocatable || info->emitrelocations)
7834 reloc_count = sec->reloc_count;
7835 else if (bed->elf_backend_count_relocs)
7837 Elf_Internal_Rela * relocs;
7839 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7840 info->keep_memory);
7842 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7844 if (elf_section_data (o)->relocs != relocs)
7845 free (relocs);
7848 if (sec->rawsize > max_contents_size)
7849 max_contents_size = sec->rawsize;
7850 if (sec->size > max_contents_size)
7851 max_contents_size = sec->size;
7853 /* We are interested in just local symbols, not all
7854 symbols. */
7855 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7856 && (sec->owner->flags & DYNAMIC) == 0)
7858 size_t sym_count;
7860 if (elf_bad_symtab (sec->owner))
7861 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7862 / bed->s->sizeof_sym);
7863 else
7864 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7866 if (sym_count > max_sym_count)
7867 max_sym_count = sym_count;
7869 if (sym_count > max_sym_shndx_count
7870 && elf_symtab_shndx (sec->owner) != 0)
7871 max_sym_shndx_count = sym_count;
7873 if ((sec->flags & SEC_RELOC) != 0)
7875 size_t ext_size;
7877 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7878 if (ext_size > max_external_reloc_size)
7879 max_external_reloc_size = ext_size;
7880 if (sec->reloc_count > max_internal_reloc_count)
7881 max_internal_reloc_count = sec->reloc_count;
7886 if (reloc_count == 0)
7887 continue;
7889 o->reloc_count += reloc_count;
7891 /* MIPS may have a mix of REL and RELA relocs on sections.
7892 To support this curious ABI we keep reloc counts in
7893 elf_section_data too. We must be careful to add the
7894 relocations from the input section to the right output
7895 count. FIXME: Get rid of one count. We have
7896 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7897 rel_count1 = &esdo->rel_count;
7898 if (esdi != NULL)
7900 bfd_boolean same_size;
7901 bfd_size_type entsize1;
7903 entsize1 = esdi->rel_hdr.sh_entsize;
7904 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7905 || entsize1 == bed->s->sizeof_rela);
7906 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7908 if (!same_size)
7909 rel_count1 = &esdo->rel_count2;
7911 if (esdi->rel_hdr2 != NULL)
7913 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7914 unsigned int alt_count;
7915 unsigned int *rel_count2;
7917 BFD_ASSERT (entsize2 != entsize1
7918 && (entsize2 == bed->s->sizeof_rel
7919 || entsize2 == bed->s->sizeof_rela));
7921 rel_count2 = &esdo->rel_count2;
7922 if (!same_size)
7923 rel_count2 = &esdo->rel_count;
7925 /* The following is probably too simplistic if the
7926 backend counts output relocs unusually. */
7927 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7928 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7929 *rel_count2 += alt_count;
7930 reloc_count -= alt_count;
7933 *rel_count1 += reloc_count;
7936 if (o->reloc_count > 0)
7937 o->flags |= SEC_RELOC;
7938 else
7940 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7941 set it (this is probably a bug) and if it is set
7942 assign_section_numbers will create a reloc section. */
7943 o->flags &=~ SEC_RELOC;
7946 /* If the SEC_ALLOC flag is not set, force the section VMA to
7947 zero. This is done in elf_fake_sections as well, but forcing
7948 the VMA to 0 here will ensure that relocs against these
7949 sections are handled correctly. */
7950 if ((o->flags & SEC_ALLOC) == 0
7951 && ! o->user_set_vma)
7952 o->vma = 0;
7955 if (! info->relocatable && merged)
7956 elf_link_hash_traverse (elf_hash_table (info),
7957 _bfd_elf_link_sec_merge_syms, abfd);
7959 /* Figure out the file positions for everything but the symbol table
7960 and the relocs. We set symcount to force assign_section_numbers
7961 to create a symbol table. */
7962 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7963 BFD_ASSERT (! abfd->output_has_begun);
7964 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7965 goto error_return;
7967 /* Set sizes, and assign file positions for reloc sections. */
7968 for (o = abfd->sections; o != NULL; o = o->next)
7970 if ((o->flags & SEC_RELOC) != 0)
7972 if (!(_bfd_elf_link_size_reloc_section
7973 (abfd, &elf_section_data (o)->rel_hdr, o)))
7974 goto error_return;
7976 if (elf_section_data (o)->rel_hdr2
7977 && !(_bfd_elf_link_size_reloc_section
7978 (abfd, elf_section_data (o)->rel_hdr2, o)))
7979 goto error_return;
7982 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7983 to count upwards while actually outputting the relocations. */
7984 elf_section_data (o)->rel_count = 0;
7985 elf_section_data (o)->rel_count2 = 0;
7988 _bfd_elf_assign_file_positions_for_relocs (abfd);
7990 /* We have now assigned file positions for all the sections except
7991 .symtab and .strtab. We start the .symtab section at the current
7992 file position, and write directly to it. We build the .strtab
7993 section in memory. */
7994 bfd_get_symcount (abfd) = 0;
7995 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7996 /* sh_name is set in prep_headers. */
7997 symtab_hdr->sh_type = SHT_SYMTAB;
7998 /* sh_flags, sh_addr and sh_size all start off zero. */
7999 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8000 /* sh_link is set in assign_section_numbers. */
8001 /* sh_info is set below. */
8002 /* sh_offset is set just below. */
8003 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
8005 off = elf_tdata (abfd)->next_file_pos;
8006 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
8008 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8009 incorrect. We do not yet know the size of the .symtab section.
8010 We correct next_file_pos below, after we do know the size. */
8012 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8013 continuously seeking to the right position in the file. */
8014 if (! info->keep_memory || max_sym_count < 20)
8015 finfo.symbuf_size = 20;
8016 else
8017 finfo.symbuf_size = max_sym_count;
8018 amt = finfo.symbuf_size;
8019 amt *= bed->s->sizeof_sym;
8020 finfo.symbuf = bfd_malloc (amt);
8021 if (finfo.symbuf == NULL)
8022 goto error_return;
8023 if (elf_numsections (abfd) > SHN_LORESERVE)
8025 /* Wild guess at number of output symbols. realloc'd as needed. */
8026 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
8027 finfo.shndxbuf_size = amt;
8028 amt *= sizeof (Elf_External_Sym_Shndx);
8029 finfo.symshndxbuf = bfd_zmalloc (amt);
8030 if (finfo.symshndxbuf == NULL)
8031 goto error_return;
8034 /* Start writing out the symbol table. The first symbol is always a
8035 dummy symbol. */
8036 if (info->strip != strip_all
8037 || emit_relocs)
8039 elfsym.st_value = 0;
8040 elfsym.st_size = 0;
8041 elfsym.st_info = 0;
8042 elfsym.st_other = 0;
8043 elfsym.st_shndx = SHN_UNDEF;
8044 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
8045 NULL))
8046 goto error_return;
8049 /* Output a symbol for each section. We output these even if we are
8050 discarding local symbols, since they are used for relocs. These
8051 symbols have no names. We store the index of each one in the
8052 index field of the section, so that we can find it again when
8053 outputting relocs. */
8054 if (info->strip != strip_all
8055 || emit_relocs)
8057 elfsym.st_size = 0;
8058 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8059 elfsym.st_other = 0;
8060 for (i = 1; i < elf_numsections (abfd); i++)
8062 o = bfd_section_from_elf_index (abfd, i);
8063 if (o != NULL)
8064 o->target_index = bfd_get_symcount (abfd);
8065 elfsym.st_shndx = i;
8066 if (info->relocatable || o == NULL)
8067 elfsym.st_value = 0;
8068 else
8069 elfsym.st_value = o->vma;
8070 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
8071 goto error_return;
8072 if (i == SHN_LORESERVE - 1)
8073 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
8077 /* Allocate some memory to hold information read in from the input
8078 files. */
8079 if (max_contents_size != 0)
8081 finfo.contents = bfd_malloc (max_contents_size);
8082 if (finfo.contents == NULL)
8083 goto error_return;
8086 if (max_external_reloc_size != 0)
8088 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
8089 if (finfo.external_relocs == NULL)
8090 goto error_return;
8093 if (max_internal_reloc_count != 0)
8095 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
8096 amt *= sizeof (Elf_Internal_Rela);
8097 finfo.internal_relocs = bfd_malloc (amt);
8098 if (finfo.internal_relocs == NULL)
8099 goto error_return;
8102 if (max_sym_count != 0)
8104 amt = max_sym_count * bed->s->sizeof_sym;
8105 finfo.external_syms = bfd_malloc (amt);
8106 if (finfo.external_syms == NULL)
8107 goto error_return;
8109 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8110 finfo.internal_syms = bfd_malloc (amt);
8111 if (finfo.internal_syms == NULL)
8112 goto error_return;
8114 amt = max_sym_count * sizeof (long);
8115 finfo.indices = bfd_malloc (amt);
8116 if (finfo.indices == NULL)
8117 goto error_return;
8119 amt = max_sym_count * sizeof (asection *);
8120 finfo.sections = bfd_malloc (amt);
8121 if (finfo.sections == NULL)
8122 goto error_return;
8125 if (max_sym_shndx_count != 0)
8127 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8128 finfo.locsym_shndx = bfd_malloc (amt);
8129 if (finfo.locsym_shndx == NULL)
8130 goto error_return;
8133 if (elf_hash_table (info)->tls_sec)
8135 bfd_vma base, end = 0;
8136 asection *sec;
8138 for (sec = elf_hash_table (info)->tls_sec;
8139 sec && (sec->flags & SEC_THREAD_LOCAL);
8140 sec = sec->next)
8142 bfd_size_type size = sec->size;
8144 if (size == 0
8145 && (sec->flags & SEC_HAS_CONTENTS) == 0)
8147 struct bfd_link_order *o = sec->map_tail.link_order;
8148 if (o != NULL)
8149 size = o->offset + o->size;
8151 end = sec->vma + size;
8153 base = elf_hash_table (info)->tls_sec->vma;
8154 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
8155 elf_hash_table (info)->tls_size = end - base;
8158 /* Reorder SHF_LINK_ORDER sections. */
8159 for (o = abfd->sections; o != NULL; o = o->next)
8161 if (!elf_fixup_link_order (abfd, o))
8162 return FALSE;
8165 /* Since ELF permits relocations to be against local symbols, we
8166 must have the local symbols available when we do the relocations.
8167 Since we would rather only read the local symbols once, and we
8168 would rather not keep them in memory, we handle all the
8169 relocations for a single input file at the same time.
8171 Unfortunately, there is no way to know the total number of local
8172 symbols until we have seen all of them, and the local symbol
8173 indices precede the global symbol indices. This means that when
8174 we are generating relocatable output, and we see a reloc against
8175 a global symbol, we can not know the symbol index until we have
8176 finished examining all the local symbols to see which ones we are
8177 going to output. To deal with this, we keep the relocations in
8178 memory, and don't output them until the end of the link. This is
8179 an unfortunate waste of memory, but I don't see a good way around
8180 it. Fortunately, it only happens when performing a relocatable
8181 link, which is not the common case. FIXME: If keep_memory is set
8182 we could write the relocs out and then read them again; I don't
8183 know how bad the memory loss will be. */
8185 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8186 sub->output_has_begun = FALSE;
8187 for (o = abfd->sections; o != NULL; o = o->next)
8189 for (p = o->map_head.link_order; p != NULL; p = p->next)
8191 if (p->type == bfd_indirect_link_order
8192 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
8193 == bfd_target_elf_flavour)
8194 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
8196 if (! sub->output_has_begun)
8198 if (! elf_link_input_bfd (&finfo, sub))
8199 goto error_return;
8200 sub->output_has_begun = TRUE;
8203 else if (p->type == bfd_section_reloc_link_order
8204 || p->type == bfd_symbol_reloc_link_order)
8206 if (! elf_reloc_link_order (abfd, info, o, p))
8207 goto error_return;
8209 else
8211 if (! _bfd_default_link_order (abfd, info, o, p))
8212 goto error_return;
8217 /* Output any global symbols that got converted to local in a
8218 version script or due to symbol visibility. We do this in a
8219 separate step since ELF requires all local symbols to appear
8220 prior to any global symbols. FIXME: We should only do this if
8221 some global symbols were, in fact, converted to become local.
8222 FIXME: Will this work correctly with the Irix 5 linker? */
8223 eoinfo.failed = FALSE;
8224 eoinfo.finfo = &finfo;
8225 eoinfo.localsyms = TRUE;
8226 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8227 &eoinfo);
8228 if (eoinfo.failed)
8229 return FALSE;
8231 /* That wrote out all the local symbols. Finish up the symbol table
8232 with the global symbols. Even if we want to strip everything we
8233 can, we still need to deal with those global symbols that got
8234 converted to local in a version script. */
8236 /* The sh_info field records the index of the first non local symbol. */
8237 symtab_hdr->sh_info = bfd_get_symcount (abfd);
8239 if (dynamic
8240 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8242 Elf_Internal_Sym sym;
8243 bfd_byte *dynsym = finfo.dynsym_sec->contents;
8244 long last_local = 0;
8246 /* Write out the section symbols for the output sections. */
8247 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
8249 asection *s;
8251 sym.st_size = 0;
8252 sym.st_name = 0;
8253 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8254 sym.st_other = 0;
8256 for (s = abfd->sections; s != NULL; s = s->next)
8258 int indx;
8259 bfd_byte *dest;
8260 long dynindx;
8262 dynindx = elf_section_data (s)->dynindx;
8263 if (dynindx <= 0)
8264 continue;
8265 indx = elf_section_data (s)->this_idx;
8266 BFD_ASSERT (indx > 0);
8267 sym.st_shndx = indx;
8268 sym.st_value = s->vma;
8269 dest = dynsym + dynindx * bed->s->sizeof_sym;
8270 if (last_local < dynindx)
8271 last_local = dynindx;
8272 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8276 /* Write out the local dynsyms. */
8277 if (elf_hash_table (info)->dynlocal)
8279 struct elf_link_local_dynamic_entry *e;
8280 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8282 asection *s;
8283 bfd_byte *dest;
8285 sym.st_size = e->isym.st_size;
8286 sym.st_other = e->isym.st_other;
8288 /* Copy the internal symbol as is.
8289 Note that we saved a word of storage and overwrote
8290 the original st_name with the dynstr_index. */
8291 sym = e->isym;
8293 if (e->isym.st_shndx != SHN_UNDEF
8294 && (e->isym.st_shndx < SHN_LORESERVE
8295 || e->isym.st_shndx > SHN_HIRESERVE))
8297 s = bfd_section_from_elf_index (e->input_bfd,
8298 e->isym.st_shndx);
8300 sym.st_shndx =
8301 elf_section_data (s->output_section)->this_idx;
8302 sym.st_value = (s->output_section->vma
8303 + s->output_offset
8304 + e->isym.st_value);
8307 if (last_local < e->dynindx)
8308 last_local = e->dynindx;
8310 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8311 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8315 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8316 last_local + 1;
8319 /* We get the global symbols from the hash table. */
8320 eoinfo.failed = FALSE;
8321 eoinfo.localsyms = FALSE;
8322 eoinfo.finfo = &finfo;
8323 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8324 &eoinfo);
8325 if (eoinfo.failed)
8326 return FALSE;
8328 /* If backend needs to output some symbols not present in the hash
8329 table, do it now. */
8330 if (bed->elf_backend_output_arch_syms)
8332 typedef bfd_boolean (*out_sym_func)
8333 (void *, const char *, Elf_Internal_Sym *, asection *,
8334 struct elf_link_hash_entry *);
8336 if (! ((*bed->elf_backend_output_arch_syms)
8337 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8338 return FALSE;
8341 /* Flush all symbols to the file. */
8342 if (! elf_link_flush_output_syms (&finfo, bed))
8343 return FALSE;
8345 /* Now we know the size of the symtab section. */
8346 off += symtab_hdr->sh_size;
8348 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8349 if (symtab_shndx_hdr->sh_name != 0)
8351 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8352 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8353 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8354 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8355 symtab_shndx_hdr->sh_size = amt;
8357 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8358 off, TRUE);
8360 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8361 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8362 return FALSE;
8366 /* Finish up and write out the symbol string table (.strtab)
8367 section. */
8368 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8369 /* sh_name was set in prep_headers. */
8370 symstrtab_hdr->sh_type = SHT_STRTAB;
8371 symstrtab_hdr->sh_flags = 0;
8372 symstrtab_hdr->sh_addr = 0;
8373 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8374 symstrtab_hdr->sh_entsize = 0;
8375 symstrtab_hdr->sh_link = 0;
8376 symstrtab_hdr->sh_info = 0;
8377 /* sh_offset is set just below. */
8378 symstrtab_hdr->sh_addralign = 1;
8380 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8381 elf_tdata (abfd)->next_file_pos = off;
8383 if (bfd_get_symcount (abfd) > 0)
8385 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8386 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8387 return FALSE;
8390 /* Adjust the relocs to have the correct symbol indices. */
8391 for (o = abfd->sections; o != NULL; o = o->next)
8393 if ((o->flags & SEC_RELOC) == 0)
8394 continue;
8396 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8397 elf_section_data (o)->rel_count,
8398 elf_section_data (o)->rel_hashes);
8399 if (elf_section_data (o)->rel_hdr2 != NULL)
8400 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8401 elf_section_data (o)->rel_count2,
8402 (elf_section_data (o)->rel_hashes
8403 + elf_section_data (o)->rel_count));
8405 /* Set the reloc_count field to 0 to prevent write_relocs from
8406 trying to swap the relocs out itself. */
8407 o->reloc_count = 0;
8410 if (dynamic && info->combreloc && dynobj != NULL)
8411 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8413 /* If we are linking against a dynamic object, or generating a
8414 shared library, finish up the dynamic linking information. */
8415 if (dynamic)
8417 bfd_byte *dyncon, *dynconend;
8419 /* Fix up .dynamic entries. */
8420 o = bfd_get_section_by_name (dynobj, ".dynamic");
8421 BFD_ASSERT (o != NULL);
8423 dyncon = o->contents;
8424 dynconend = o->contents + o->size;
8425 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8427 Elf_Internal_Dyn dyn;
8428 const char *name;
8429 unsigned int type;
8431 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8433 switch (dyn.d_tag)
8435 default:
8436 continue;
8437 case DT_NULL:
8438 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8440 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8442 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8443 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8444 default: continue;
8446 dyn.d_un.d_val = relativecount;
8447 relativecount = 0;
8448 break;
8450 continue;
8452 case DT_INIT:
8453 name = info->init_function;
8454 goto get_sym;
8455 case DT_FINI:
8456 name = info->fini_function;
8457 get_sym:
8459 struct elf_link_hash_entry *h;
8461 h = elf_link_hash_lookup (elf_hash_table (info), name,
8462 FALSE, FALSE, TRUE);
8463 if (h != NULL
8464 && (h->root.type == bfd_link_hash_defined
8465 || h->root.type == bfd_link_hash_defweak))
8467 dyn.d_un.d_val = h->root.u.def.value;
8468 o = h->root.u.def.section;
8469 if (o->output_section != NULL)
8470 dyn.d_un.d_val += (o->output_section->vma
8471 + o->output_offset);
8472 else
8474 /* The symbol is imported from another shared
8475 library and does not apply to this one. */
8476 dyn.d_un.d_val = 0;
8478 break;
8481 continue;
8483 case DT_PREINIT_ARRAYSZ:
8484 name = ".preinit_array";
8485 goto get_size;
8486 case DT_INIT_ARRAYSZ:
8487 name = ".init_array";
8488 goto get_size;
8489 case DT_FINI_ARRAYSZ:
8490 name = ".fini_array";
8491 get_size:
8492 o = bfd_get_section_by_name (abfd, name);
8493 if (o == NULL)
8495 (*_bfd_error_handler)
8496 (_("%B: could not find output section %s"), abfd, name);
8497 goto error_return;
8499 if (o->size == 0)
8500 (*_bfd_error_handler)
8501 (_("warning: %s section has zero size"), name);
8502 dyn.d_un.d_val = o->size;
8503 break;
8505 case DT_PREINIT_ARRAY:
8506 name = ".preinit_array";
8507 goto get_vma;
8508 case DT_INIT_ARRAY:
8509 name = ".init_array";
8510 goto get_vma;
8511 case DT_FINI_ARRAY:
8512 name = ".fini_array";
8513 goto get_vma;
8515 case DT_HASH:
8516 name = ".hash";
8517 goto get_vma;
8518 case DT_STRTAB:
8519 name = ".dynstr";
8520 goto get_vma;
8521 case DT_SYMTAB:
8522 name = ".dynsym";
8523 goto get_vma;
8524 case DT_VERDEF:
8525 name = ".gnu.version_d";
8526 goto get_vma;
8527 case DT_VERNEED:
8528 name = ".gnu.version_r";
8529 goto get_vma;
8530 case DT_VERSYM:
8531 name = ".gnu.version";
8532 get_vma:
8533 o = bfd_get_section_by_name (abfd, name);
8534 if (o == NULL)
8536 (*_bfd_error_handler)
8537 (_("%B: could not find output section %s"), abfd, name);
8538 goto error_return;
8540 dyn.d_un.d_ptr = o->vma;
8541 break;
8543 case DT_REL:
8544 case DT_RELA:
8545 case DT_RELSZ:
8546 case DT_RELASZ:
8547 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8548 type = SHT_REL;
8549 else
8550 type = SHT_RELA;
8551 dyn.d_un.d_val = 0;
8552 for (i = 1; i < elf_numsections (abfd); i++)
8554 Elf_Internal_Shdr *hdr;
8556 hdr = elf_elfsections (abfd)[i];
8557 if (hdr->sh_type == type
8558 && (hdr->sh_flags & SHF_ALLOC) != 0)
8560 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8561 dyn.d_un.d_val += hdr->sh_size;
8562 else
8564 if (dyn.d_un.d_val == 0
8565 || hdr->sh_addr < dyn.d_un.d_val)
8566 dyn.d_un.d_val = hdr->sh_addr;
8570 break;
8572 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8576 /* If we have created any dynamic sections, then output them. */
8577 if (dynobj != NULL)
8579 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8580 goto error_return;
8582 for (o = dynobj->sections; o != NULL; o = o->next)
8584 if ((o->flags & SEC_HAS_CONTENTS) == 0
8585 || o->size == 0
8586 || o->output_section == bfd_abs_section_ptr)
8587 continue;
8588 if ((o->flags & SEC_LINKER_CREATED) == 0)
8590 /* At this point, we are only interested in sections
8591 created by _bfd_elf_link_create_dynamic_sections. */
8592 continue;
8594 if (elf_hash_table (info)->stab_info.stabstr == o)
8595 continue;
8596 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8597 continue;
8598 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8599 != SHT_STRTAB)
8600 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8602 if (! bfd_set_section_contents (abfd, o->output_section,
8603 o->contents,
8604 (file_ptr) o->output_offset,
8605 o->size))
8606 goto error_return;
8608 else
8610 /* The contents of the .dynstr section are actually in a
8611 stringtab. */
8612 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8613 if (bfd_seek (abfd, off, SEEK_SET) != 0
8614 || ! _bfd_elf_strtab_emit (abfd,
8615 elf_hash_table (info)->dynstr))
8616 goto error_return;
8621 if (info->relocatable)
8623 bfd_boolean failed = FALSE;
8625 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8626 if (failed)
8627 goto error_return;
8630 /* If we have optimized stabs strings, output them. */
8631 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8633 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8634 goto error_return;
8637 if (info->eh_frame_hdr)
8639 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8640 goto error_return;
8643 if (finfo.symstrtab != NULL)
8644 _bfd_stringtab_free (finfo.symstrtab);
8645 if (finfo.contents != NULL)
8646 free (finfo.contents);
8647 if (finfo.external_relocs != NULL)
8648 free (finfo.external_relocs);
8649 if (finfo.internal_relocs != NULL)
8650 free (finfo.internal_relocs);
8651 if (finfo.external_syms != NULL)
8652 free (finfo.external_syms);
8653 if (finfo.locsym_shndx != NULL)
8654 free (finfo.locsym_shndx);
8655 if (finfo.internal_syms != NULL)
8656 free (finfo.internal_syms);
8657 if (finfo.indices != NULL)
8658 free (finfo.indices);
8659 if (finfo.sections != NULL)
8660 free (finfo.sections);
8661 if (finfo.symbuf != NULL)
8662 free (finfo.symbuf);
8663 if (finfo.symshndxbuf != NULL)
8664 free (finfo.symshndxbuf);
8665 for (o = abfd->sections; o != NULL; o = o->next)
8667 if ((o->flags & SEC_RELOC) != 0
8668 && elf_section_data (o)->rel_hashes != NULL)
8669 free (elf_section_data (o)->rel_hashes);
8672 elf_tdata (abfd)->linker = TRUE;
8674 return TRUE;
8676 error_return:
8677 if (finfo.symstrtab != NULL)
8678 _bfd_stringtab_free (finfo.symstrtab);
8679 if (finfo.contents != NULL)
8680 free (finfo.contents);
8681 if (finfo.external_relocs != NULL)
8682 free (finfo.external_relocs);
8683 if (finfo.internal_relocs != NULL)
8684 free (finfo.internal_relocs);
8685 if (finfo.external_syms != NULL)
8686 free (finfo.external_syms);
8687 if (finfo.locsym_shndx != NULL)
8688 free (finfo.locsym_shndx);
8689 if (finfo.internal_syms != NULL)
8690 free (finfo.internal_syms);
8691 if (finfo.indices != NULL)
8692 free (finfo.indices);
8693 if (finfo.sections != NULL)
8694 free (finfo.sections);
8695 if (finfo.symbuf != NULL)
8696 free (finfo.symbuf);
8697 if (finfo.symshndxbuf != NULL)
8698 free (finfo.symshndxbuf);
8699 for (o = abfd->sections; o != NULL; o = o->next)
8701 if ((o->flags & SEC_RELOC) != 0
8702 && elf_section_data (o)->rel_hashes != NULL)
8703 free (elf_section_data (o)->rel_hashes);
8706 return FALSE;
8709 /* Garbage collect unused sections. */
8711 /* The mark phase of garbage collection. For a given section, mark
8712 it and any sections in this section's group, and all the sections
8713 which define symbols to which it refers. */
8715 typedef asection * (*gc_mark_hook_fn)
8716 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8717 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8719 bfd_boolean
8720 _bfd_elf_gc_mark (struct bfd_link_info *info,
8721 asection *sec,
8722 gc_mark_hook_fn gc_mark_hook)
8724 bfd_boolean ret;
8725 bfd_boolean is_eh;
8726 asection *group_sec;
8728 sec->gc_mark = 1;
8730 /* Mark all the sections in the group. */
8731 group_sec = elf_section_data (sec)->next_in_group;
8732 if (group_sec && !group_sec->gc_mark)
8733 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8734 return FALSE;
8736 /* Look through the section relocs. */
8737 ret = TRUE;
8738 is_eh = strcmp (sec->name, ".eh_frame") == 0;
8739 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8741 Elf_Internal_Rela *relstart, *rel, *relend;
8742 Elf_Internal_Shdr *symtab_hdr;
8743 struct elf_link_hash_entry **sym_hashes;
8744 size_t nlocsyms;
8745 size_t extsymoff;
8746 bfd *input_bfd = sec->owner;
8747 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8748 Elf_Internal_Sym *isym = NULL;
8749 int r_sym_shift;
8751 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8752 sym_hashes = elf_sym_hashes (input_bfd);
8754 /* Read the local symbols. */
8755 if (elf_bad_symtab (input_bfd))
8757 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8758 extsymoff = 0;
8760 else
8761 extsymoff = nlocsyms = symtab_hdr->sh_info;
8763 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8764 if (isym == NULL && nlocsyms != 0)
8766 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8767 NULL, NULL, NULL);
8768 if (isym == NULL)
8769 return FALSE;
8772 /* Read the relocations. */
8773 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8774 info->keep_memory);
8775 if (relstart == NULL)
8777 ret = FALSE;
8778 goto out1;
8780 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8782 if (bed->s->arch_size == 32)
8783 r_sym_shift = 8;
8784 else
8785 r_sym_shift = 32;
8787 for (rel = relstart; rel < relend; rel++)
8789 unsigned long r_symndx;
8790 asection *rsec;
8791 struct elf_link_hash_entry *h;
8793 r_symndx = rel->r_info >> r_sym_shift;
8794 if (r_symndx == 0)
8795 continue;
8797 if (r_symndx >= nlocsyms
8798 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8800 h = sym_hashes[r_symndx - extsymoff];
8801 while (h->root.type == bfd_link_hash_indirect
8802 || h->root.type == bfd_link_hash_warning)
8803 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8804 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8806 else
8808 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8811 if (rsec && !rsec->gc_mark)
8813 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8814 rsec->gc_mark = 1;
8815 else if (is_eh)
8816 rsec->gc_mark_from_eh = 1;
8817 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8819 ret = FALSE;
8820 goto out2;
8825 out2:
8826 if (elf_section_data (sec)->relocs != relstart)
8827 free (relstart);
8828 out1:
8829 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8831 if (! info->keep_memory)
8832 free (isym);
8833 else
8834 symtab_hdr->contents = (unsigned char *) isym;
8838 return ret;
8841 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8843 struct elf_gc_sweep_symbol_info {
8844 struct bfd_link_info *info;
8845 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
8846 bfd_boolean);
8849 static bfd_boolean
8850 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
8852 if (h->root.type == bfd_link_hash_warning)
8853 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8855 if ((h->root.type == bfd_link_hash_defined
8856 || h->root.type == bfd_link_hash_defweak)
8857 && !h->root.u.def.section->gc_mark
8858 && !(h->root.u.def.section->owner->flags & DYNAMIC))
8860 struct elf_gc_sweep_symbol_info *inf = data;
8861 (*inf->hide_symbol) (inf->info, h, TRUE);
8864 return TRUE;
8867 /* The sweep phase of garbage collection. Remove all garbage sections. */
8869 typedef bfd_boolean (*gc_sweep_hook_fn)
8870 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8872 static bfd_boolean
8873 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
8875 bfd *sub;
8876 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8877 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
8878 unsigned long section_sym_count;
8879 struct elf_gc_sweep_symbol_info sweep_info;
8881 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8883 asection *o;
8885 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8886 continue;
8888 for (o = sub->sections; o != NULL; o = o->next)
8890 /* Keep debug and special sections. */
8891 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8892 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8893 o->gc_mark = 1;
8895 if (o->gc_mark)
8896 continue;
8898 /* Skip sweeping sections already excluded. */
8899 if (o->flags & SEC_EXCLUDE)
8900 continue;
8902 /* Since this is early in the link process, it is simple
8903 to remove a section from the output. */
8904 o->flags |= SEC_EXCLUDE;
8906 /* But we also have to update some of the relocation
8907 info we collected before. */
8908 if (gc_sweep_hook
8909 && (o->flags & SEC_RELOC) != 0
8910 && o->reloc_count > 0
8911 && !bfd_is_abs_section (o->output_section))
8913 Elf_Internal_Rela *internal_relocs;
8914 bfd_boolean r;
8916 internal_relocs
8917 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8918 info->keep_memory);
8919 if (internal_relocs == NULL)
8920 return FALSE;
8922 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8924 if (elf_section_data (o)->relocs != internal_relocs)
8925 free (internal_relocs);
8927 if (!r)
8928 return FALSE;
8933 /* Remove the symbols that were in the swept sections from the dynamic
8934 symbol table. GCFIXME: Anyone know how to get them out of the
8935 static symbol table as well? */
8936 sweep_info.info = info;
8937 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
8938 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
8939 &sweep_info);
8941 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
8942 return TRUE;
8945 /* Propagate collected vtable information. This is called through
8946 elf_link_hash_traverse. */
8948 static bfd_boolean
8949 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8951 if (h->root.type == bfd_link_hash_warning)
8952 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8954 /* Those that are not vtables. */
8955 if (h->vtable == NULL || h->vtable->parent == NULL)
8956 return TRUE;
8958 /* Those vtables that do not have parents, we cannot merge. */
8959 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
8960 return TRUE;
8962 /* If we've already been done, exit. */
8963 if (h->vtable->used && h->vtable->used[-1])
8964 return TRUE;
8966 /* Make sure the parent's table is up to date. */
8967 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
8969 if (h->vtable->used == NULL)
8971 /* None of this table's entries were referenced. Re-use the
8972 parent's table. */
8973 h->vtable->used = h->vtable->parent->vtable->used;
8974 h->vtable->size = h->vtable->parent->vtable->size;
8976 else
8978 size_t n;
8979 bfd_boolean *cu, *pu;
8981 /* Or the parent's entries into ours. */
8982 cu = h->vtable->used;
8983 cu[-1] = TRUE;
8984 pu = h->vtable->parent->vtable->used;
8985 if (pu != NULL)
8987 const struct elf_backend_data *bed;
8988 unsigned int log_file_align;
8990 bed = get_elf_backend_data (h->root.u.def.section->owner);
8991 log_file_align = bed->s->log_file_align;
8992 n = h->vtable->parent->vtable->size >> log_file_align;
8993 while (n--)
8995 if (*pu)
8996 *cu = TRUE;
8997 pu++;
8998 cu++;
9003 return TRUE;
9006 static bfd_boolean
9007 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
9009 asection *sec;
9010 bfd_vma hstart, hend;
9011 Elf_Internal_Rela *relstart, *relend, *rel;
9012 const struct elf_backend_data *bed;
9013 unsigned int log_file_align;
9015 if (h->root.type == bfd_link_hash_warning)
9016 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9018 /* Take care of both those symbols that do not describe vtables as
9019 well as those that are not loaded. */
9020 if (h->vtable == NULL || h->vtable->parent == NULL)
9021 return TRUE;
9023 BFD_ASSERT (h->root.type == bfd_link_hash_defined
9024 || h->root.type == bfd_link_hash_defweak);
9026 sec = h->root.u.def.section;
9027 hstart = h->root.u.def.value;
9028 hend = hstart + h->size;
9030 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
9031 if (!relstart)
9032 return *(bfd_boolean *) okp = FALSE;
9033 bed = get_elf_backend_data (sec->owner);
9034 log_file_align = bed->s->log_file_align;
9036 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
9038 for (rel = relstart; rel < relend; ++rel)
9039 if (rel->r_offset >= hstart && rel->r_offset < hend)
9041 /* If the entry is in use, do nothing. */
9042 if (h->vtable->used
9043 && (rel->r_offset - hstart) < h->vtable->size)
9045 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
9046 if (h->vtable->used[entry])
9047 continue;
9049 /* Otherwise, kill it. */
9050 rel->r_offset = rel->r_info = rel->r_addend = 0;
9053 return TRUE;
9056 /* Mark sections containing dynamically referenced symbols. When
9057 building shared libraries, we must assume that any visible symbol is
9058 referenced. */
9060 static bfd_boolean
9061 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
9063 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9065 if (h->root.type == bfd_link_hash_warning)
9066 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9068 if ((h->root.type == bfd_link_hash_defined
9069 || h->root.type == bfd_link_hash_defweak)
9070 && (h->ref_dynamic
9071 || (!info->executable
9072 && h->def_regular
9073 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
9074 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
9075 h->root.u.def.section->flags |= SEC_KEEP;
9077 return TRUE;
9080 /* Do mark and sweep of unused sections. */
9082 bfd_boolean
9083 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
9085 bfd_boolean ok = TRUE;
9086 bfd *sub;
9087 asection * (*gc_mark_hook)
9088 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
9089 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
9091 if (!get_elf_backend_data (abfd)->can_gc_sections
9092 || info->relocatable
9093 || info->emitrelocations
9094 || !is_elf_hash_table (info->hash))
9096 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
9097 return TRUE;
9100 /* Apply transitive closure to the vtable entry usage info. */
9101 elf_link_hash_traverse (elf_hash_table (info),
9102 elf_gc_propagate_vtable_entries_used,
9103 &ok);
9104 if (!ok)
9105 return FALSE;
9107 /* Kill the vtable relocations that were not used. */
9108 elf_link_hash_traverse (elf_hash_table (info),
9109 elf_gc_smash_unused_vtentry_relocs,
9110 &ok);
9111 if (!ok)
9112 return FALSE;
9114 /* Mark dynamically referenced symbols. */
9115 if (elf_hash_table (info)->dynamic_sections_created)
9116 elf_link_hash_traverse (elf_hash_table (info),
9117 elf_gc_mark_dynamic_ref_symbol,
9118 info);
9120 /* Grovel through relocs to find out who stays ... */
9121 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
9122 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9124 asection *o;
9126 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9127 continue;
9129 for (o = sub->sections; o != NULL; o = o->next)
9130 if ((o->flags & SEC_KEEP) != 0 && !o->gc_mark)
9131 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9132 return FALSE;
9135 /* ... again for sections marked from eh_frame. */
9136 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9138 asection *o;
9140 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9141 continue;
9143 /* Keep .gcc_except_table.* if the associated .text.* is
9144 marked. This isn't very nice, but the proper solution,
9145 splitting .eh_frame up and using comdat doesn't pan out
9146 easily due to needing special relocs to handle the
9147 difference of two symbols in separate sections.
9148 Don't keep code sections referenced by .eh_frame. */
9149 for (o = sub->sections; o != NULL; o = o->next)
9150 if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)
9152 if (strncmp (o->name, ".gcc_except_table.", 18) == 0)
9154 unsigned long len;
9155 char *fn_name;
9156 asection *fn_text;
9158 len = strlen (o->name + 18) + 1;
9159 fn_name = bfd_malloc (len + 6);
9160 if (fn_name == NULL)
9161 return FALSE;
9162 memcpy (fn_name, ".text.", 6);
9163 memcpy (fn_name + 6, o->name + 18, len);
9164 fn_text = bfd_get_section_by_name (sub, fn_name);
9165 free (fn_name);
9166 if (fn_text == NULL || !fn_text->gc_mark)
9167 continue;
9170 /* If not using specially named exception table section,
9171 then keep whatever we are using. */
9172 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9173 return FALSE;
9177 /* ... and mark SEC_EXCLUDE for those that go. */
9178 return elf_gc_sweep (abfd, info);
9181 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9183 bfd_boolean
9184 bfd_elf_gc_record_vtinherit (bfd *abfd,
9185 asection *sec,
9186 struct elf_link_hash_entry *h,
9187 bfd_vma offset)
9189 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
9190 struct elf_link_hash_entry **search, *child;
9191 bfd_size_type extsymcount;
9192 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9194 /* The sh_info field of the symtab header tells us where the
9195 external symbols start. We don't care about the local symbols at
9196 this point. */
9197 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
9198 if (!elf_bad_symtab (abfd))
9199 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
9201 sym_hashes = elf_sym_hashes (abfd);
9202 sym_hashes_end = sym_hashes + extsymcount;
9204 /* Hunt down the child symbol, which is in this section at the same
9205 offset as the relocation. */
9206 for (search = sym_hashes; search != sym_hashes_end; ++search)
9208 if ((child = *search) != NULL
9209 && (child->root.type == bfd_link_hash_defined
9210 || child->root.type == bfd_link_hash_defweak)
9211 && child->root.u.def.section == sec
9212 && child->root.u.def.value == offset)
9213 goto win;
9216 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
9217 abfd, sec, (unsigned long) offset);
9218 bfd_set_error (bfd_error_invalid_operation);
9219 return FALSE;
9221 win:
9222 if (!child->vtable)
9224 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
9225 if (!child->vtable)
9226 return FALSE;
9228 if (!h)
9230 /* This *should* only be the absolute section. It could potentially
9231 be that someone has defined a non-global vtable though, which
9232 would be bad. It isn't worth paging in the local symbols to be
9233 sure though; that case should simply be handled by the assembler. */
9235 child->vtable->parent = (struct elf_link_hash_entry *) -1;
9237 else
9238 child->vtable->parent = h;
9240 return TRUE;
9243 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9245 bfd_boolean
9246 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
9247 asection *sec ATTRIBUTE_UNUSED,
9248 struct elf_link_hash_entry *h,
9249 bfd_vma addend)
9251 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9252 unsigned int log_file_align = bed->s->log_file_align;
9254 if (!h->vtable)
9256 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
9257 if (!h->vtable)
9258 return FALSE;
9261 if (addend >= h->vtable->size)
9263 size_t size, bytes, file_align;
9264 bfd_boolean *ptr = h->vtable->used;
9266 /* While the symbol is undefined, we have to be prepared to handle
9267 a zero size. */
9268 file_align = 1 << log_file_align;
9269 if (h->root.type == bfd_link_hash_undefined)
9270 size = addend + file_align;
9271 else
9273 size = h->size;
9274 if (addend >= size)
9276 /* Oops! We've got a reference past the defined end of
9277 the table. This is probably a bug -- shall we warn? */
9278 size = addend + file_align;
9281 size = (size + file_align - 1) & -file_align;
9283 /* Allocate one extra entry for use as a "done" flag for the
9284 consolidation pass. */
9285 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9287 if (ptr)
9289 ptr = bfd_realloc (ptr - 1, bytes);
9291 if (ptr != NULL)
9293 size_t oldbytes;
9295 oldbytes = (((h->vtable->size >> log_file_align) + 1)
9296 * sizeof (bfd_boolean));
9297 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9300 else
9301 ptr = bfd_zmalloc (bytes);
9303 if (ptr == NULL)
9304 return FALSE;
9306 /* And arrange for that done flag to be at index -1. */
9307 h->vtable->used = ptr + 1;
9308 h->vtable->size = size;
9311 h->vtable->used[addend >> log_file_align] = TRUE;
9313 return TRUE;
9316 struct alloc_got_off_arg {
9317 bfd_vma gotoff;
9318 unsigned int got_elt_size;
9321 /* We need a special top-level link routine to convert got reference counts
9322 to real got offsets. */
9324 static bfd_boolean
9325 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
9327 struct alloc_got_off_arg *gofarg = arg;
9329 if (h->root.type == bfd_link_hash_warning)
9330 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9332 if (h->got.refcount > 0)
9334 h->got.offset = gofarg->gotoff;
9335 gofarg->gotoff += gofarg->got_elt_size;
9337 else
9338 h->got.offset = (bfd_vma) -1;
9340 return TRUE;
9343 /* And an accompanying bit to work out final got entry offsets once
9344 we're done. Should be called from final_link. */
9346 bfd_boolean
9347 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
9348 struct bfd_link_info *info)
9350 bfd *i;
9351 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9352 bfd_vma gotoff;
9353 unsigned int got_elt_size = bed->s->arch_size / 8;
9354 struct alloc_got_off_arg gofarg;
9356 if (! is_elf_hash_table (info->hash))
9357 return FALSE;
9359 /* The GOT offset is relative to the .got section, but the GOT header is
9360 put into the .got.plt section, if the backend uses it. */
9361 if (bed->want_got_plt)
9362 gotoff = 0;
9363 else
9364 gotoff = bed->got_header_size;
9366 /* Do the local .got entries first. */
9367 for (i = info->input_bfds; i; i = i->link_next)
9369 bfd_signed_vma *local_got;
9370 bfd_size_type j, locsymcount;
9371 Elf_Internal_Shdr *symtab_hdr;
9373 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9374 continue;
9376 local_got = elf_local_got_refcounts (i);
9377 if (!local_got)
9378 continue;
9380 symtab_hdr = &elf_tdata (i)->symtab_hdr;
9381 if (elf_bad_symtab (i))
9382 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9383 else
9384 locsymcount = symtab_hdr->sh_info;
9386 for (j = 0; j < locsymcount; ++j)
9388 if (local_got[j] > 0)
9390 local_got[j] = gotoff;
9391 gotoff += got_elt_size;
9393 else
9394 local_got[j] = (bfd_vma) -1;
9398 /* Then the global .got entries. .plt refcounts are handled by
9399 adjust_dynamic_symbol */
9400 gofarg.gotoff = gotoff;
9401 gofarg.got_elt_size = got_elt_size;
9402 elf_link_hash_traverse (elf_hash_table (info),
9403 elf_gc_allocate_got_offsets,
9404 &gofarg);
9405 return TRUE;
9408 /* Many folk need no more in the way of final link than this, once
9409 got entry reference counting is enabled. */
9411 bfd_boolean
9412 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9414 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9415 return FALSE;
9417 /* Invoke the regular ELF backend linker to do all the work. */
9418 return bfd_elf_final_link (abfd, info);
9421 bfd_boolean
9422 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9424 struct elf_reloc_cookie *rcookie = cookie;
9426 if (rcookie->bad_symtab)
9427 rcookie->rel = rcookie->rels;
9429 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9431 unsigned long r_symndx;
9433 if (! rcookie->bad_symtab)
9434 if (rcookie->rel->r_offset > offset)
9435 return FALSE;
9436 if (rcookie->rel->r_offset != offset)
9437 continue;
9439 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9440 if (r_symndx == SHN_UNDEF)
9441 return TRUE;
9443 if (r_symndx >= rcookie->locsymcount
9444 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9446 struct elf_link_hash_entry *h;
9448 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9450 while (h->root.type == bfd_link_hash_indirect
9451 || h->root.type == bfd_link_hash_warning)
9452 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9454 if ((h->root.type == bfd_link_hash_defined
9455 || h->root.type == bfd_link_hash_defweak)
9456 && elf_discarded_section (h->root.u.def.section))
9457 return TRUE;
9458 else
9459 return FALSE;
9461 else
9463 /* It's not a relocation against a global symbol,
9464 but it could be a relocation against a local
9465 symbol for a discarded section. */
9466 asection *isec;
9467 Elf_Internal_Sym *isym;
9469 /* Need to: get the symbol; get the section. */
9470 isym = &rcookie->locsyms[r_symndx];
9471 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9473 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9474 if (isec != NULL && elf_discarded_section (isec))
9475 return TRUE;
9478 return FALSE;
9480 return FALSE;
9483 /* Discard unneeded references to discarded sections.
9484 Returns TRUE if any section's size was changed. */
9485 /* This function assumes that the relocations are in sorted order,
9486 which is true for all known assemblers. */
9488 bfd_boolean
9489 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9491 struct elf_reloc_cookie cookie;
9492 asection *stab, *eh;
9493 Elf_Internal_Shdr *symtab_hdr;
9494 const struct elf_backend_data *bed;
9495 bfd *abfd;
9496 unsigned int count;
9497 bfd_boolean ret = FALSE;
9499 if (info->traditional_format
9500 || !is_elf_hash_table (info->hash))
9501 return FALSE;
9503 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9505 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9506 continue;
9508 bed = get_elf_backend_data (abfd);
9510 if ((abfd->flags & DYNAMIC) != 0)
9511 continue;
9513 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9514 if (info->relocatable
9515 || (eh != NULL
9516 && (eh->size == 0
9517 || bfd_is_abs_section (eh->output_section))))
9518 eh = NULL;
9520 stab = bfd_get_section_by_name (abfd, ".stab");
9521 if (stab != NULL
9522 && (stab->size == 0
9523 || bfd_is_abs_section (stab->output_section)
9524 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9525 stab = NULL;
9527 if (stab == NULL
9528 && eh == NULL
9529 && bed->elf_backend_discard_info == NULL)
9530 continue;
9532 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9533 cookie.abfd = abfd;
9534 cookie.sym_hashes = elf_sym_hashes (abfd);
9535 cookie.bad_symtab = elf_bad_symtab (abfd);
9536 if (cookie.bad_symtab)
9538 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9539 cookie.extsymoff = 0;
9541 else
9543 cookie.locsymcount = symtab_hdr->sh_info;
9544 cookie.extsymoff = symtab_hdr->sh_info;
9547 if (bed->s->arch_size == 32)
9548 cookie.r_sym_shift = 8;
9549 else
9550 cookie.r_sym_shift = 32;
9552 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9553 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9555 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9556 cookie.locsymcount, 0,
9557 NULL, NULL, NULL);
9558 if (cookie.locsyms == NULL)
9559 return FALSE;
9562 if (stab != NULL)
9564 cookie.rels = NULL;
9565 count = stab->reloc_count;
9566 if (count != 0)
9567 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9568 info->keep_memory);
9569 if (cookie.rels != NULL)
9571 cookie.rel = cookie.rels;
9572 cookie.relend = cookie.rels;
9573 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9574 if (_bfd_discard_section_stabs (abfd, stab,
9575 elf_section_data (stab)->sec_info,
9576 bfd_elf_reloc_symbol_deleted_p,
9577 &cookie))
9578 ret = TRUE;
9579 if (elf_section_data (stab)->relocs != cookie.rels)
9580 free (cookie.rels);
9584 if (eh != NULL)
9586 cookie.rels = NULL;
9587 count = eh->reloc_count;
9588 if (count != 0)
9589 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9590 info->keep_memory);
9591 cookie.rel = cookie.rels;
9592 cookie.relend = cookie.rels;
9593 if (cookie.rels != NULL)
9594 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9596 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9597 bfd_elf_reloc_symbol_deleted_p,
9598 &cookie))
9599 ret = TRUE;
9601 if (cookie.rels != NULL
9602 && elf_section_data (eh)->relocs != cookie.rels)
9603 free (cookie.rels);
9606 if (bed->elf_backend_discard_info != NULL
9607 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9608 ret = TRUE;
9610 if (cookie.locsyms != NULL
9611 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9613 if (! info->keep_memory)
9614 free (cookie.locsyms);
9615 else
9616 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9620 if (info->eh_frame_hdr
9621 && !info->relocatable
9622 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9623 ret = TRUE;
9625 return ret;
9628 void
9629 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9631 flagword flags;
9632 const char *name, *p;
9633 struct bfd_section_already_linked *l;
9634 struct bfd_section_already_linked_hash_entry *already_linked_list;
9635 asection *group;
9637 /* A single member comdat group section may be discarded by a
9638 linkonce section. See below. */
9639 if (sec->output_section == bfd_abs_section_ptr)
9640 return;
9642 flags = sec->flags;
9644 /* Check if it belongs to a section group. */
9645 group = elf_sec_group (sec);
9647 /* Return if it isn't a linkonce section nor a member of a group. A
9648 comdat group section also has SEC_LINK_ONCE set. */
9649 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9650 return;
9652 if (group)
9654 /* If this is the member of a single member comdat group, check if
9655 the group should be discarded. */
9656 if (elf_next_in_group (sec) == sec
9657 && (group->flags & SEC_LINK_ONCE) != 0)
9658 sec = group;
9659 else
9660 return;
9663 /* FIXME: When doing a relocatable link, we may have trouble
9664 copying relocations in other sections that refer to local symbols
9665 in the section being discarded. Those relocations will have to
9666 be converted somehow; as of this writing I'm not sure that any of
9667 the backends handle that correctly.
9669 It is tempting to instead not discard link once sections when
9670 doing a relocatable link (technically, they should be discarded
9671 whenever we are building constructors). However, that fails,
9672 because the linker winds up combining all the link once sections
9673 into a single large link once section, which defeats the purpose
9674 of having link once sections in the first place.
9676 Also, not merging link once sections in a relocatable link
9677 causes trouble for MIPS ELF, which relies on link once semantics
9678 to handle the .reginfo section correctly. */
9680 name = bfd_get_section_name (abfd, sec);
9682 if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9683 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
9684 p++;
9685 else
9686 p = name;
9688 already_linked_list = bfd_section_already_linked_table_lookup (p);
9690 for (l = already_linked_list->entry; l != NULL; l = l->next)
9692 /* We may have 3 different sections on the list: group section,
9693 comdat section and linkonce section. SEC may be a linkonce or
9694 group section. We match a group section with a group section,
9695 a linkonce section with a linkonce section, and ignore comdat
9696 section. */
9697 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9698 && strcmp (name, l->sec->name) == 0
9699 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9701 /* The section has already been linked. See if we should
9702 issue a warning. */
9703 switch (flags & SEC_LINK_DUPLICATES)
9705 default:
9706 abort ();
9708 case SEC_LINK_DUPLICATES_DISCARD:
9709 break;
9711 case SEC_LINK_DUPLICATES_ONE_ONLY:
9712 (*_bfd_error_handler)
9713 (_("%B: ignoring duplicate section `%A'"),
9714 abfd, sec);
9715 break;
9717 case SEC_LINK_DUPLICATES_SAME_SIZE:
9718 if (sec->size != l->sec->size)
9719 (*_bfd_error_handler)
9720 (_("%B: duplicate section `%A' has different size"),
9721 abfd, sec);
9722 break;
9724 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9725 if (sec->size != l->sec->size)
9726 (*_bfd_error_handler)
9727 (_("%B: duplicate section `%A' has different size"),
9728 abfd, sec);
9729 else if (sec->size != 0)
9731 bfd_byte *sec_contents, *l_sec_contents;
9733 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9734 (*_bfd_error_handler)
9735 (_("%B: warning: could not read contents of section `%A'"),
9736 abfd, sec);
9737 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9738 &l_sec_contents))
9739 (*_bfd_error_handler)
9740 (_("%B: warning: could not read contents of section `%A'"),
9741 l->sec->owner, l->sec);
9742 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9743 (*_bfd_error_handler)
9744 (_("%B: warning: duplicate section `%A' has different contents"),
9745 abfd, sec);
9747 if (sec_contents)
9748 free (sec_contents);
9749 if (l_sec_contents)
9750 free (l_sec_contents);
9752 break;
9755 /* Set the output_section field so that lang_add_section
9756 does not create a lang_input_section structure for this
9757 section. Since there might be a symbol in the section
9758 being discarded, we must retain a pointer to the section
9759 which we are really going to use. */
9760 sec->output_section = bfd_abs_section_ptr;
9761 sec->kept_section = l->sec;
9763 if (flags & SEC_GROUP)
9765 asection *first = elf_next_in_group (sec);
9766 asection *s = first;
9768 while (s != NULL)
9770 s->output_section = bfd_abs_section_ptr;
9771 /* Record which group discards it. */
9772 s->kept_section = l->sec;
9773 s = elf_next_in_group (s);
9774 /* These lists are circular. */
9775 if (s == first)
9776 break;
9780 return;
9784 if (group)
9786 /* If this is the member of a single member comdat group and the
9787 group hasn't be discarded, we check if it matches a linkonce
9788 section. We only record the discarded comdat group. Otherwise
9789 the undiscarded group will be discarded incorrectly later since
9790 itself has been recorded. */
9791 for (l = already_linked_list->entry; l != NULL; l = l->next)
9792 if ((l->sec->flags & SEC_GROUP) == 0
9793 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9794 && bfd_elf_match_symbols_in_sections (l->sec,
9795 elf_next_in_group (sec)))
9797 elf_next_in_group (sec)->output_section = bfd_abs_section_ptr;
9798 elf_next_in_group (sec)->kept_section = l->sec;
9799 group->output_section = bfd_abs_section_ptr;
9800 break;
9802 if (l == NULL)
9803 return;
9805 else
9806 /* There is no direct match. But for linkonce section, we should
9807 check if there is a match with comdat group member. We always
9808 record the linkonce section, discarded or not. */
9809 for (l = already_linked_list->entry; l != NULL; l = l->next)
9810 if (l->sec->flags & SEC_GROUP)
9812 asection *first = elf_next_in_group (l->sec);
9814 if (first != NULL
9815 && elf_next_in_group (first) == first
9816 && bfd_elf_match_symbols_in_sections (first, sec))
9818 sec->output_section = bfd_abs_section_ptr;
9819 sec->kept_section = l->sec;
9820 break;
9824 /* This is the first section with this name. Record it. */
9825 bfd_section_already_linked_table_insert (already_linked_list, sec);
9828 bfd_boolean
9829 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
9831 return sym->st_shndx == SHN_COMMON;
9834 unsigned int
9835 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
9837 return SHN_COMMON;
9840 asection *
9841 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
9843 return bfd_com_section_ptr;