1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007 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. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
31 /* Define a symbol in a dynamic linkage section. */
33 struct elf_link_hash_entry *
34 _bfd_elf_define_linkage_sym (bfd *abfd,
35 struct bfd_link_info *info,
39 struct elf_link_hash_entry *h;
40 struct bfd_link_hash_entry *bh;
41 const struct elf_backend_data *bed;
43 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
46 /* Zap symbol defined in an as-needed lib that wasn't linked.
47 This is a symptom of a larger problem: Absolute symbols
48 defined in shared libraries can't be overridden, because we
49 lose the link to the bfd which is via the symbol section. */
50 h->root.type = bfd_link_hash_new;
54 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
56 get_elf_backend_data (abfd)->collect,
59 h = (struct elf_link_hash_entry *) bh;
62 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
64 bed = get_elf_backend_data (abfd);
65 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
70 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
74 struct elf_link_hash_entry *h;
75 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
78 /* This function may be called more than once. */
79 s = bfd_get_section_by_name (abfd, ".got");
80 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
83 switch (bed->s->arch_size)
94 bfd_set_error (bfd_error_bad_value);
98 flags = bed->dynamic_sec_flags;
100 s = bfd_make_section_with_flags (abfd, ".got", flags);
102 || !bfd_set_section_alignment (abfd, s, ptralign))
105 if (bed->want_got_plt)
107 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
109 || !bfd_set_section_alignment (abfd, s, ptralign))
113 if (bed->want_got_sym)
115 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
116 (or .got.plt) section. We don't do this in the linker script
117 because we don't want to define the symbol if we are not creating
118 a global offset table. */
119 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
120 elf_hash_table (info)->hgot = h;
125 /* The first bit of the global offset table is the header. */
126 s->size += bed->got_header_size;
131 /* Create a strtab to hold the dynamic symbol names. */
133 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
135 struct elf_link_hash_table *hash_table;
137 hash_table = elf_hash_table (info);
138 if (hash_table->dynobj == NULL)
139 hash_table->dynobj = abfd;
141 if (hash_table->dynstr == NULL)
143 hash_table->dynstr = _bfd_elf_strtab_init ();
144 if (hash_table->dynstr == NULL)
150 /* Create some sections which will be filled in with dynamic linking
151 information. ABFD is an input file which requires dynamic sections
152 to be created. The dynamic sections take up virtual memory space
153 when the final executable is run, so we need to create them before
154 addresses are assigned to the output sections. We work out the
155 actual contents and size of these sections later. */
158 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
161 register asection *s;
162 const struct elf_backend_data *bed;
164 if (! is_elf_hash_table (info->hash))
167 if (elf_hash_table (info)->dynamic_sections_created)
170 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
173 abfd = elf_hash_table (info)->dynobj;
174 bed = get_elf_backend_data (abfd);
176 flags = bed->dynamic_sec_flags;
178 /* A dynamically linked executable has a .interp section, but a
179 shared library does not. */
180 if (info->executable)
182 s = bfd_make_section_with_flags (abfd, ".interp",
183 flags | SEC_READONLY);
188 /* Create sections to hold version informations. These are removed
189 if they are not needed. */
190 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
191 flags | SEC_READONLY);
193 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
196 s = bfd_make_section_with_flags (abfd, ".gnu.version",
197 flags | SEC_READONLY);
199 || ! bfd_set_section_alignment (abfd, s, 1))
202 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
203 flags | SEC_READONLY);
205 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
208 s = bfd_make_section_with_flags (abfd, ".dynsym",
209 flags | SEC_READONLY);
211 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
214 s = bfd_make_section_with_flags (abfd, ".dynstr",
215 flags | SEC_READONLY);
219 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
221 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
224 /* The special symbol _DYNAMIC is always set to the start of the
225 .dynamic section. We could set _DYNAMIC in a linker script, but we
226 only want to define it if we are, in fact, creating a .dynamic
227 section. We don't want to define it if there is no .dynamic
228 section, since on some ELF platforms the start up code examines it
229 to decide how to initialize the process. */
230 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
235 s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
237 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
239 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
242 if (info->emit_gnu_hash)
244 s = bfd_make_section_with_flags (abfd, ".gnu.hash",
245 flags | SEC_READONLY);
247 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
249 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
250 4 32-bit words followed by variable count of 64-bit words, then
251 variable count of 32-bit words. */
252 if (bed->s->arch_size == 64)
253 elf_section_data (s)->this_hdr.sh_entsize = 0;
255 elf_section_data (s)->this_hdr.sh_entsize = 4;
258 /* Let the backend create the rest of the sections. This lets the
259 backend set the right flags. The backend will normally create
260 the .got and .plt sections. */
261 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
264 elf_hash_table (info)->dynamic_sections_created = TRUE;
269 /* Create dynamic sections when linking against a dynamic object. */
272 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
274 flagword flags, pltflags;
275 struct elf_link_hash_entry *h;
277 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
279 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
280 .rel[a].bss sections. */
281 flags = bed->dynamic_sec_flags;
284 if (bed->plt_not_loaded)
285 /* We do not clear SEC_ALLOC here because we still want the OS to
286 allocate space for the section; it's just that there's nothing
287 to read in from the object file. */
288 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
290 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
291 if (bed->plt_readonly)
292 pltflags |= SEC_READONLY;
294 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
296 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
299 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
301 if (bed->want_plt_sym)
303 h = _bfd_elf_define_linkage_sym (abfd, info, s,
304 "_PROCEDURE_LINKAGE_TABLE_");
305 elf_hash_table (info)->hplt = h;
310 s = bfd_make_section_with_flags (abfd,
311 (bed->default_use_rela_p
312 ? ".rela.plt" : ".rel.plt"),
313 flags | SEC_READONLY);
315 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
318 if (! _bfd_elf_create_got_section (abfd, info))
321 if (bed->want_dynbss)
323 /* The .dynbss section is a place to put symbols which are defined
324 by dynamic objects, are referenced by regular objects, and are
325 not functions. We must allocate space for them in the process
326 image and use a R_*_COPY reloc to tell the dynamic linker to
327 initialize them at run time. The linker script puts the .dynbss
328 section into the .bss section of the final image. */
329 s = bfd_make_section_with_flags (abfd, ".dynbss",
331 | SEC_LINKER_CREATED));
335 /* The .rel[a].bss section holds copy relocs. This section is not
336 normally needed. We need to create it here, though, so that the
337 linker will map it to an output section. We can't just create it
338 only if we need it, because we will not know whether we need it
339 until we have seen all the input files, and the first time the
340 main linker code calls BFD after examining all the input files
341 (size_dynamic_sections) the input sections have already been
342 mapped to the output sections. If the section turns out not to
343 be needed, we can discard it later. We will never need this
344 section when generating a shared object, since they do not use
348 s = bfd_make_section_with_flags (abfd,
349 (bed->default_use_rela_p
350 ? ".rela.bss" : ".rel.bss"),
351 flags | SEC_READONLY);
353 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
361 /* Record a new dynamic symbol. We record the dynamic symbols as we
362 read the input files, since we need to have a list of all of them
363 before we can determine the final sizes of the output sections.
364 Note that we may actually call this function even though we are not
365 going to output any dynamic symbols; in some cases we know that a
366 symbol should be in the dynamic symbol table, but only if there is
370 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
371 struct elf_link_hash_entry *h)
373 if (h->dynindx == -1)
375 struct elf_strtab_hash *dynstr;
380 /* XXX: The ABI draft says the linker must turn hidden and
381 internal symbols into STB_LOCAL symbols when producing the
382 DSO. However, if ld.so honors st_other in the dynamic table,
383 this would not be necessary. */
384 switch (ELF_ST_VISIBILITY (h->other))
388 if (h->root.type != bfd_link_hash_undefined
389 && h->root.type != bfd_link_hash_undefweak)
392 if (!elf_hash_table (info)->is_relocatable_executable)
400 h->dynindx = elf_hash_table (info)->dynsymcount;
401 ++elf_hash_table (info)->dynsymcount;
403 dynstr = elf_hash_table (info)->dynstr;
406 /* Create a strtab to hold the dynamic symbol names. */
407 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
412 /* We don't put any version information in the dynamic string
414 name = h->root.root.string;
415 p = strchr (name, ELF_VER_CHR);
417 /* We know that the p points into writable memory. In fact,
418 there are only a few symbols that have read-only names, being
419 those like _GLOBAL_OFFSET_TABLE_ that are created specially
420 by the backends. Most symbols will have names pointing into
421 an ELF string table read from a file, or to objalloc memory. */
424 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
429 if (indx == (bfd_size_type) -1)
431 h->dynstr_index = indx;
437 /* Mark a symbol dynamic. */
440 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
441 struct elf_link_hash_entry *h,
442 Elf_Internal_Sym *sym)
444 struct bfd_elf_dynamic_list *d = info->dynamic_list;
446 /* It may be called more than once on the same H. */
447 if(h->dynamic || info->relocatable)
450 if ((info->dynamic_data
451 && (h->type == STT_OBJECT
453 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
455 && h->root.type == bfd_link_hash_new
456 && (*d->match) (&d->head, NULL, h->root.root.string)))
460 /* Record an assignment to a symbol made by a linker script. We need
461 this in case some dynamic object refers to this symbol. */
464 bfd_elf_record_link_assignment (bfd *output_bfd,
465 struct bfd_link_info *info,
470 struct elf_link_hash_entry *h;
471 struct elf_link_hash_table *htab;
473 if (!is_elf_hash_table (info->hash))
476 htab = elf_hash_table (info);
477 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
481 /* Since we're defining the symbol, don't let it seem to have not
482 been defined. record_dynamic_symbol and size_dynamic_sections
483 may depend on this. */
484 if (h->root.type == bfd_link_hash_undefweak
485 || h->root.type == bfd_link_hash_undefined)
487 h->root.type = bfd_link_hash_new;
488 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
489 bfd_link_repair_undef_list (&htab->root);
492 if (h->root.type == bfd_link_hash_new)
494 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
498 /* If this symbol is being provided by the linker script, and it is
499 currently defined by a dynamic object, but not by a regular
500 object, then mark it as undefined so that the generic linker will
501 force the correct value. */
505 h->root.type = bfd_link_hash_undefined;
507 /* If this symbol is not being provided by the linker script, and it is
508 currently defined by a dynamic object, but not by a regular object,
509 then clear out any version information because the symbol will not be
510 associated with the dynamic object any more. */
514 h->verinfo.verdef = NULL;
518 if (provide && hidden)
520 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
522 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
523 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
526 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
528 if (!info->relocatable
530 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
531 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
537 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
540 if (! bfd_elf_link_record_dynamic_symbol (info, h))
543 /* If this is a weak defined symbol, and we know a corresponding
544 real symbol from the same dynamic object, make sure the real
545 symbol is also made into a dynamic symbol. */
546 if (h->u.weakdef != NULL
547 && h->u.weakdef->dynindx == -1)
549 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
557 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
558 success, and 2 on a failure caused by attempting to record a symbol
559 in a discarded section, eg. a discarded link-once section symbol. */
562 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
567 struct elf_link_local_dynamic_entry *entry;
568 struct elf_link_hash_table *eht;
569 struct elf_strtab_hash *dynstr;
570 unsigned long dynstr_index;
572 Elf_External_Sym_Shndx eshndx;
573 char esym[sizeof (Elf64_External_Sym)];
575 if (! is_elf_hash_table (info->hash))
578 /* See if the entry exists already. */
579 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
580 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
583 amt = sizeof (*entry);
584 entry = bfd_alloc (input_bfd, amt);
588 /* Go find the symbol, so that we can find it's name. */
589 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
590 1, input_indx, &entry->isym, esym, &eshndx))
592 bfd_release (input_bfd, entry);
596 if (entry->isym.st_shndx != SHN_UNDEF
597 && (entry->isym.st_shndx < SHN_LORESERVE
598 || entry->isym.st_shndx > SHN_HIRESERVE))
602 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
603 if (s == NULL || bfd_is_abs_section (s->output_section))
605 /* We can still bfd_release here as nothing has done another
606 bfd_alloc. We can't do this later in this function. */
607 bfd_release (input_bfd, entry);
612 name = (bfd_elf_string_from_elf_section
613 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
614 entry->isym.st_name));
616 dynstr = elf_hash_table (info)->dynstr;
619 /* Create a strtab to hold the dynamic symbol names. */
620 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
625 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
626 if (dynstr_index == (unsigned long) -1)
628 entry->isym.st_name = dynstr_index;
630 eht = elf_hash_table (info);
632 entry->next = eht->dynlocal;
633 eht->dynlocal = entry;
634 entry->input_bfd = input_bfd;
635 entry->input_indx = input_indx;
638 /* Whatever binding the symbol had before, it's now local. */
640 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
642 /* The dynindx will be set at the end of size_dynamic_sections. */
647 /* Return the dynindex of a local dynamic symbol. */
650 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
654 struct elf_link_local_dynamic_entry *e;
656 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
657 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
662 /* This function is used to renumber the dynamic symbols, if some of
663 them are removed because they are marked as local. This is called
664 via elf_link_hash_traverse. */
667 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
670 size_t *count = data;
672 if (h->root.type == bfd_link_hash_warning)
673 h = (struct elf_link_hash_entry *) h->root.u.i.link;
678 if (h->dynindx != -1)
679 h->dynindx = ++(*count);
685 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
686 STB_LOCAL binding. */
689 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
692 size_t *count = data;
694 if (h->root.type == bfd_link_hash_warning)
695 h = (struct elf_link_hash_entry *) h->root.u.i.link;
697 if (!h->forced_local)
700 if (h->dynindx != -1)
701 h->dynindx = ++(*count);
706 /* Return true if the dynamic symbol for a given section should be
707 omitted when creating a shared library. */
709 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
710 struct bfd_link_info *info,
713 struct elf_link_hash_table *htab;
715 switch (elf_section_data (p)->this_hdr.sh_type)
719 /* If sh_type is yet undecided, assume it could be
720 SHT_PROGBITS/SHT_NOBITS. */
722 htab = elf_hash_table (info);
723 if (p == htab->tls_sec)
726 if (htab->text_index_section != NULL)
727 return p != htab->text_index_section && p != htab->data_index_section;
729 if (strcmp (p->name, ".got") == 0
730 || strcmp (p->name, ".got.plt") == 0
731 || strcmp (p->name, ".plt") == 0)
735 if (htab->dynobj != NULL
736 && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL
737 && (ip->flags & SEC_LINKER_CREATED)
738 && ip->output_section == p)
743 /* There shouldn't be section relative relocations
744 against any other section. */
750 /* Assign dynsym indices. In a shared library we generate a section
751 symbol for each output section, which come first. Next come symbols
752 which have been forced to local binding. Then all of the back-end
753 allocated local dynamic syms, followed by the rest of the global
757 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
758 struct bfd_link_info *info,
759 unsigned long *section_sym_count)
761 unsigned long dynsymcount = 0;
763 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
765 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
767 for (p = output_bfd->sections; p ; p = p->next)
768 if ((p->flags & SEC_EXCLUDE) == 0
769 && (p->flags & SEC_ALLOC) != 0
770 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
771 elf_section_data (p)->dynindx = ++dynsymcount;
773 elf_section_data (p)->dynindx = 0;
775 *section_sym_count = dynsymcount;
777 elf_link_hash_traverse (elf_hash_table (info),
778 elf_link_renumber_local_hash_table_dynsyms,
781 if (elf_hash_table (info)->dynlocal)
783 struct elf_link_local_dynamic_entry *p;
784 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
785 p->dynindx = ++dynsymcount;
788 elf_link_hash_traverse (elf_hash_table (info),
789 elf_link_renumber_hash_table_dynsyms,
792 /* There is an unused NULL entry at the head of the table which
793 we must account for in our count. Unless there weren't any
794 symbols, which means we'll have no table at all. */
795 if (dynsymcount != 0)
798 elf_hash_table (info)->dynsymcount = dynsymcount;
802 /* This function is called when we want to define a new symbol. It
803 handles the various cases which arise when we find a definition in
804 a dynamic object, or when there is already a definition in a
805 dynamic object. The new symbol is described by NAME, SYM, PSEC,
806 and PVALUE. We set SYM_HASH to the hash table entry. We set
807 OVERRIDE if the old symbol is overriding a new definition. We set
808 TYPE_CHANGE_OK if it is OK for the type to change. We set
809 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
810 change, we mean that we shouldn't warn if the type or size does
811 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
812 object is overridden by a regular object. */
815 _bfd_elf_merge_symbol (bfd *abfd,
816 struct bfd_link_info *info,
818 Elf_Internal_Sym *sym,
821 unsigned int *pold_alignment,
822 struct elf_link_hash_entry **sym_hash,
824 bfd_boolean *override,
825 bfd_boolean *type_change_ok,
826 bfd_boolean *size_change_ok)
828 asection *sec, *oldsec;
829 struct elf_link_hash_entry *h;
830 struct elf_link_hash_entry *flip;
833 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
834 bfd_boolean newweak, oldweak;
835 const struct elf_backend_data *bed;
841 bind = ELF_ST_BIND (sym->st_info);
843 /* Silently discard TLS symbols from --just-syms. There's no way to
844 combine a static TLS block with a new TLS block for this executable. */
845 if (ELF_ST_TYPE (sym->st_info) == STT_TLS
846 && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
852 if (! bfd_is_und_section (sec))
853 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
855 h = ((struct elf_link_hash_entry *)
856 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
861 /* This code is for coping with dynamic objects, and is only useful
862 if we are doing an ELF link. */
863 if (info->hash->creator != abfd->xvec)
866 /* For merging, we only care about real symbols. */
868 while (h->root.type == bfd_link_hash_indirect
869 || h->root.type == bfd_link_hash_warning)
870 h = (struct elf_link_hash_entry *) h->root.u.i.link;
872 /* We have to check it for every instance since the first few may be
873 refereences and not all compilers emit symbol type for undefined
875 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
877 /* If we just created the symbol, mark it as being an ELF symbol.
878 Other than that, there is nothing to do--there is no merge issue
879 with a newly defined symbol--so we just return. */
881 if (h->root.type == bfd_link_hash_new)
887 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
890 switch (h->root.type)
897 case bfd_link_hash_undefined:
898 case bfd_link_hash_undefweak:
899 oldbfd = h->root.u.undef.abfd;
903 case bfd_link_hash_defined:
904 case bfd_link_hash_defweak:
905 oldbfd = h->root.u.def.section->owner;
906 oldsec = h->root.u.def.section;
909 case bfd_link_hash_common:
910 oldbfd = h->root.u.c.p->section->owner;
911 oldsec = h->root.u.c.p->section;
915 /* In cases involving weak versioned symbols, we may wind up trying
916 to merge a symbol with itself. Catch that here, to avoid the
917 confusion that results if we try to override a symbol with
918 itself. The additional tests catch cases like
919 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
920 dynamic object, which we do want to handle here. */
922 && ((abfd->flags & DYNAMIC) == 0
926 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
927 respectively, is from a dynamic object. */
929 newdyn = (abfd->flags & DYNAMIC) != 0;
933 olddyn = (oldbfd->flags & DYNAMIC) != 0;
934 else if (oldsec != NULL)
936 /* This handles the special SHN_MIPS_{TEXT,DATA} section
937 indices used by MIPS ELF. */
938 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
941 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
942 respectively, appear to be a definition rather than reference. */
944 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
946 olddef = (h->root.type != bfd_link_hash_undefined
947 && h->root.type != bfd_link_hash_undefweak
948 && h->root.type != bfd_link_hash_common);
950 bed = get_elf_backend_data (abfd);
951 /* When we try to create a default indirect symbol from the dynamic
952 definition with the default version, we skip it if its type and
953 the type of existing regular definition mismatch. We only do it
954 if the existing regular definition won't be dynamic. */
955 if (pold_alignment == NULL
957 && !info->export_dynamic
962 && (olddef || h->root.type == bfd_link_hash_common)
963 && ELF_ST_TYPE (sym->st_info) != h->type
964 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
965 && h->type != STT_NOTYPE
966 && !(bed->is_function_type (ELF_ST_TYPE (sym->st_info))
967 && bed->is_function_type (h->type)))
973 /* Check TLS symbol. We don't check undefined symbol introduced by
975 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
976 && ELF_ST_TYPE (sym->st_info) != h->type
980 bfd_boolean ntdef, tdef;
981 asection *ntsec, *tsec;
983 if (h->type == STT_TLS)
1003 (*_bfd_error_handler)
1004 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1005 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1006 else if (!tdef && !ntdef)
1007 (*_bfd_error_handler)
1008 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1009 tbfd, ntbfd, h->root.root.string);
1011 (*_bfd_error_handler)
1012 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1013 tbfd, tsec, ntbfd, h->root.root.string);
1015 (*_bfd_error_handler)
1016 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1017 tbfd, ntbfd, ntsec, h->root.root.string);
1019 bfd_set_error (bfd_error_bad_value);
1023 /* We need to remember if a symbol has a definition in a dynamic
1024 object or is weak in all dynamic objects. Internal and hidden
1025 visibility will make it unavailable to dynamic objects. */
1026 if (newdyn && !h->dynamic_def)
1028 if (!bfd_is_und_section (sec))
1032 /* Check if this symbol is weak in all dynamic objects. If it
1033 is the first time we see it in a dynamic object, we mark
1034 if it is weak. Otherwise, we clear it. */
1035 if (!h->ref_dynamic)
1037 if (bind == STB_WEAK)
1038 h->dynamic_weak = 1;
1040 else if (bind != STB_WEAK)
1041 h->dynamic_weak = 0;
1045 /* If the old symbol has non-default visibility, we ignore the new
1046 definition from a dynamic object. */
1048 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1049 && !bfd_is_und_section (sec))
1052 /* Make sure this symbol is dynamic. */
1054 /* A protected symbol has external availability. Make sure it is
1055 recorded as dynamic.
1057 FIXME: Should we check type and size for protected symbol? */
1058 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1059 return bfd_elf_link_record_dynamic_symbol (info, h);
1064 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1067 /* If the new symbol with non-default visibility comes from a
1068 relocatable file and the old definition comes from a dynamic
1069 object, we remove the old definition. */
1070 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1072 /* Handle the case where the old dynamic definition is
1073 default versioned. We need to copy the symbol info from
1074 the symbol with default version to the normal one if it
1075 was referenced before. */
1078 const struct elf_backend_data *bed
1079 = get_elf_backend_data (abfd);
1080 struct elf_link_hash_entry *vh = *sym_hash;
1081 vh->root.type = h->root.type;
1082 h->root.type = bfd_link_hash_indirect;
1083 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1084 /* Protected symbols will override the dynamic definition
1085 with default version. */
1086 if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1088 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1089 vh->dynamic_def = 1;
1090 vh->ref_dynamic = 1;
1094 h->root.type = vh->root.type;
1095 vh->ref_dynamic = 0;
1096 /* We have to hide it here since it was made dynamic
1097 global with extra bits when the symbol info was
1098 copied from the old dynamic definition. */
1099 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1107 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1108 && bfd_is_und_section (sec))
1110 /* If the new symbol is undefined and the old symbol was
1111 also undefined before, we need to make sure
1112 _bfd_generic_link_add_one_symbol doesn't mess
1113 up the linker hash table undefs list. Since the old
1114 definition came from a dynamic object, it is still on the
1116 h->root.type = bfd_link_hash_undefined;
1117 h->root.u.undef.abfd = abfd;
1121 h->root.type = bfd_link_hash_new;
1122 h->root.u.undef.abfd = NULL;
1131 /* FIXME: Should we check type and size for protected symbol? */
1137 /* Differentiate strong and weak symbols. */
1138 newweak = bind == STB_WEAK;
1139 oldweak = (h->root.type == bfd_link_hash_defweak
1140 || h->root.type == bfd_link_hash_undefweak);
1142 /* If a new weak symbol definition comes from a regular file and the
1143 old symbol comes from a dynamic library, we treat the new one as
1144 strong. Similarly, an old weak symbol definition from a regular
1145 file is treated as strong when the new symbol comes from a dynamic
1146 library. Further, an old weak symbol from a dynamic library is
1147 treated as strong if the new symbol is from a dynamic library.
1148 This reflects the way glibc's ld.so works.
1150 Do this before setting *type_change_ok or *size_change_ok so that
1151 we warn properly when dynamic library symbols are overridden. */
1153 if (newdef && !newdyn && olddyn)
1155 if (olddef && newdyn)
1158 /* Allow changes between different types of funciton symbol. */
1159 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))
1160 && bed->is_function_type (h->type))
1161 *type_change_ok = TRUE;
1163 /* It's OK to change the type if either the existing symbol or the
1164 new symbol is weak. A type change is also OK if the old symbol
1165 is undefined and the new symbol is defined. */
1170 && h->root.type == bfd_link_hash_undefined))
1171 *type_change_ok = TRUE;
1173 /* It's OK to change the size if either the existing symbol or the
1174 new symbol is weak, or if the old symbol is undefined. */
1177 || h->root.type == bfd_link_hash_undefined)
1178 *size_change_ok = TRUE;
1180 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1181 symbol, respectively, appears to be a common symbol in a dynamic
1182 object. If a symbol appears in an uninitialized section, and is
1183 not weak, and is not a function, then it may be a common symbol
1184 which was resolved when the dynamic object was created. We want
1185 to treat such symbols specially, because they raise special
1186 considerations when setting the symbol size: if the symbol
1187 appears as a common symbol in a regular object, and the size in
1188 the regular object is larger, we must make sure that we use the
1189 larger size. This problematic case can always be avoided in C,
1190 but it must be handled correctly when using Fortran shared
1193 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1194 likewise for OLDDYNCOMMON and OLDDEF.
1196 Note that this test is just a heuristic, and that it is quite
1197 possible to have an uninitialized symbol in a shared object which
1198 is really a definition, rather than a common symbol. This could
1199 lead to some minor confusion when the symbol really is a common
1200 symbol in some regular object. However, I think it will be
1206 && (sec->flags & SEC_ALLOC) != 0
1207 && (sec->flags & SEC_LOAD) == 0
1209 && !bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
1210 newdyncommon = TRUE;
1212 newdyncommon = FALSE;
1216 && h->root.type == bfd_link_hash_defined
1218 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1219 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1221 && !bed->is_function_type (h->type))
1222 olddyncommon = TRUE;
1224 olddyncommon = FALSE;
1226 /* We now know everything about the old and new symbols. We ask the
1227 backend to check if we can merge them. */
1228 if (bed->merge_symbol
1229 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1230 pold_alignment, skip, override,
1231 type_change_ok, size_change_ok,
1232 &newdyn, &newdef, &newdyncommon, &newweak,
1234 &olddyn, &olddef, &olddyncommon, &oldweak,
1238 /* If both the old and the new symbols look like common symbols in a
1239 dynamic object, set the size of the symbol to the larger of the
1244 && sym->st_size != h->size)
1246 /* Since we think we have two common symbols, issue a multiple
1247 common warning if desired. Note that we only warn if the
1248 size is different. If the size is the same, we simply let
1249 the old symbol override the new one as normally happens with
1250 symbols defined in dynamic objects. */
1252 if (! ((*info->callbacks->multiple_common)
1253 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1254 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1257 if (sym->st_size > h->size)
1258 h->size = sym->st_size;
1260 *size_change_ok = TRUE;
1263 /* If we are looking at a dynamic object, and we have found a
1264 definition, we need to see if the symbol was already defined by
1265 some other object. If so, we want to use the existing
1266 definition, and we do not want to report a multiple symbol
1267 definition error; we do this by clobbering *PSEC to be
1268 bfd_und_section_ptr.
1270 We treat a common symbol as a definition if the symbol in the
1271 shared library is a function, since common symbols always
1272 represent variables; this can cause confusion in principle, but
1273 any such confusion would seem to indicate an erroneous program or
1274 shared library. We also permit a common symbol in a regular
1275 object to override a weak symbol in a shared object. */
1280 || (h->root.type == bfd_link_hash_common
1282 || bed->is_function_type (ELF_ST_TYPE (sym->st_info))))))
1286 newdyncommon = FALSE;
1288 *psec = sec = bfd_und_section_ptr;
1289 *size_change_ok = TRUE;
1291 /* If we get here when the old symbol is a common symbol, then
1292 we are explicitly letting it override a weak symbol or
1293 function in a dynamic object, and we don't want to warn about
1294 a type change. If the old symbol is a defined symbol, a type
1295 change warning may still be appropriate. */
1297 if (h->root.type == bfd_link_hash_common)
1298 *type_change_ok = TRUE;
1301 /* Handle the special case of an old common symbol merging with a
1302 new symbol which looks like a common symbol in a shared object.
1303 We change *PSEC and *PVALUE to make the new symbol look like a
1304 common symbol, and let _bfd_generic_link_add_one_symbol do the
1308 && h->root.type == bfd_link_hash_common)
1312 newdyncommon = FALSE;
1313 *pvalue = sym->st_size;
1314 *psec = sec = bed->common_section (oldsec);
1315 *size_change_ok = TRUE;
1318 /* Skip weak definitions of symbols that are already defined. */
1319 if (newdef && olddef && newweak)
1322 /* If the old symbol is from a dynamic object, and the new symbol is
1323 a definition which is not from a dynamic object, then the new
1324 symbol overrides the old symbol. Symbols from regular files
1325 always take precedence over symbols from dynamic objects, even if
1326 they are defined after the dynamic object in the link.
1328 As above, we again permit a common symbol in a regular object to
1329 override a definition in a shared object if the shared object
1330 symbol is a function or is weak. */
1335 || (bfd_is_com_section (sec)
1337 || bed->is_function_type (h->type))))
1342 /* Change the hash table entry to undefined, and let
1343 _bfd_generic_link_add_one_symbol do the right thing with the
1346 h->root.type = bfd_link_hash_undefined;
1347 h->root.u.undef.abfd = h->root.u.def.section->owner;
1348 *size_change_ok = TRUE;
1351 olddyncommon = FALSE;
1353 /* We again permit a type change when a common symbol may be
1354 overriding a function. */
1356 if (bfd_is_com_section (sec))
1357 *type_change_ok = TRUE;
1359 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1362 /* This union may have been set to be non-NULL when this symbol
1363 was seen in a dynamic object. We must force the union to be
1364 NULL, so that it is correct for a regular symbol. */
1365 h->verinfo.vertree = NULL;
1368 /* Handle the special case of a new common symbol merging with an
1369 old symbol that looks like it might be a common symbol defined in
1370 a shared object. Note that we have already handled the case in
1371 which a new common symbol should simply override the definition
1372 in the shared library. */
1375 && bfd_is_com_section (sec)
1378 /* It would be best if we could set the hash table entry to a
1379 common symbol, but we don't know what to use for the section
1380 or the alignment. */
1381 if (! ((*info->callbacks->multiple_common)
1382 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1383 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1386 /* If the presumed common symbol in the dynamic object is
1387 larger, pretend that the new symbol has its size. */
1389 if (h->size > *pvalue)
1392 /* We need to remember the alignment required by the symbol
1393 in the dynamic object. */
1394 BFD_ASSERT (pold_alignment);
1395 *pold_alignment = h->root.u.def.section->alignment_power;
1398 olddyncommon = FALSE;
1400 h->root.type = bfd_link_hash_undefined;
1401 h->root.u.undef.abfd = h->root.u.def.section->owner;
1403 *size_change_ok = TRUE;
1404 *type_change_ok = TRUE;
1406 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1409 h->verinfo.vertree = NULL;
1414 /* Handle the case where we had a versioned symbol in a dynamic
1415 library and now find a definition in a normal object. In this
1416 case, we make the versioned symbol point to the normal one. */
1417 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1418 flip->root.type = h->root.type;
1419 h->root.type = bfd_link_hash_indirect;
1420 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1421 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1422 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1426 flip->ref_dynamic = 1;
1433 /* This function is called to create an indirect symbol from the
1434 default for the symbol with the default version if needed. The
1435 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1436 set DYNSYM if the new indirect symbol is dynamic. */
1439 _bfd_elf_add_default_symbol (bfd *abfd,
1440 struct bfd_link_info *info,
1441 struct elf_link_hash_entry *h,
1443 Elf_Internal_Sym *sym,
1446 bfd_boolean *dynsym,
1447 bfd_boolean override)
1449 bfd_boolean type_change_ok;
1450 bfd_boolean size_change_ok;
1453 struct elf_link_hash_entry *hi;
1454 struct bfd_link_hash_entry *bh;
1455 const struct elf_backend_data *bed;
1456 bfd_boolean collect;
1457 bfd_boolean dynamic;
1459 size_t len, shortlen;
1462 /* If this symbol has a version, and it is the default version, we
1463 create an indirect symbol from the default name to the fully
1464 decorated name. This will cause external references which do not
1465 specify a version to be bound to this version of the symbol. */
1466 p = strchr (name, ELF_VER_CHR);
1467 if (p == NULL || p[1] != ELF_VER_CHR)
1472 /* We are overridden by an old definition. We need to check if we
1473 need to create the indirect symbol from the default name. */
1474 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1476 BFD_ASSERT (hi != NULL);
1479 while (hi->root.type == bfd_link_hash_indirect
1480 || hi->root.type == bfd_link_hash_warning)
1482 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1488 bed = get_elf_backend_data (abfd);
1489 collect = bed->collect;
1490 dynamic = (abfd->flags & DYNAMIC) != 0;
1492 shortlen = p - name;
1493 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1494 if (shortname == NULL)
1496 memcpy (shortname, name, shortlen);
1497 shortname[shortlen] = '\0';
1499 /* We are going to create a new symbol. Merge it with any existing
1500 symbol with this name. For the purposes of the merge, act as
1501 though we were defining the symbol we just defined, although we
1502 actually going to define an indirect symbol. */
1503 type_change_ok = FALSE;
1504 size_change_ok = FALSE;
1506 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1507 NULL, &hi, &skip, &override,
1508 &type_change_ok, &size_change_ok))
1517 if (! (_bfd_generic_link_add_one_symbol
1518 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1519 0, name, FALSE, collect, &bh)))
1521 hi = (struct elf_link_hash_entry *) bh;
1525 /* In this case the symbol named SHORTNAME is overriding the
1526 indirect symbol we want to add. We were planning on making
1527 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1528 is the name without a version. NAME is the fully versioned
1529 name, and it is the default version.
1531 Overriding means that we already saw a definition for the
1532 symbol SHORTNAME in a regular object, and it is overriding
1533 the symbol defined in the dynamic object.
1535 When this happens, we actually want to change NAME, the
1536 symbol we just added, to refer to SHORTNAME. This will cause
1537 references to NAME in the shared object to become references
1538 to SHORTNAME in the regular object. This is what we expect
1539 when we override a function in a shared object: that the
1540 references in the shared object will be mapped to the
1541 definition in the regular object. */
1543 while (hi->root.type == bfd_link_hash_indirect
1544 || hi->root.type == bfd_link_hash_warning)
1545 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1547 h->root.type = bfd_link_hash_indirect;
1548 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1552 hi->ref_dynamic = 1;
1556 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1561 /* Now set HI to H, so that the following code will set the
1562 other fields correctly. */
1566 /* Check if HI is a warning symbol. */
1567 if (hi->root.type == bfd_link_hash_warning)
1568 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1570 /* If there is a duplicate definition somewhere, then HI may not
1571 point to an indirect symbol. We will have reported an error to
1572 the user in that case. */
1574 if (hi->root.type == bfd_link_hash_indirect)
1576 struct elf_link_hash_entry *ht;
1578 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1579 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1581 /* See if the new flags lead us to realize that the symbol must
1593 if (hi->ref_regular)
1599 /* We also need to define an indirection from the nondefault version
1603 len = strlen (name);
1604 shortname = bfd_hash_allocate (&info->hash->table, len);
1605 if (shortname == NULL)
1607 memcpy (shortname, name, shortlen);
1608 memcpy (shortname + shortlen, p + 1, len - shortlen);
1610 /* Once again, merge with any existing symbol. */
1611 type_change_ok = FALSE;
1612 size_change_ok = FALSE;
1614 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1615 NULL, &hi, &skip, &override,
1616 &type_change_ok, &size_change_ok))
1624 /* Here SHORTNAME is a versioned name, so we don't expect to see
1625 the type of override we do in the case above unless it is
1626 overridden by a versioned definition. */
1627 if (hi->root.type != bfd_link_hash_defined
1628 && hi->root.type != bfd_link_hash_defweak)
1629 (*_bfd_error_handler)
1630 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1636 if (! (_bfd_generic_link_add_one_symbol
1637 (info, abfd, shortname, BSF_INDIRECT,
1638 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1640 hi = (struct elf_link_hash_entry *) bh;
1642 /* If there is a duplicate definition somewhere, then HI may not
1643 point to an indirect symbol. We will have reported an error
1644 to the user in that case. */
1646 if (hi->root.type == bfd_link_hash_indirect)
1648 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1650 /* See if the new flags lead us to realize that the symbol
1662 if (hi->ref_regular)
1672 /* This routine is used to export all defined symbols into the dynamic
1673 symbol table. It is called via elf_link_hash_traverse. */
1676 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1678 struct elf_info_failed *eif = data;
1680 /* Ignore this if we won't export it. */
1681 if (!eif->info->export_dynamic && !h->dynamic)
1684 /* Ignore indirect symbols. These are added by the versioning code. */
1685 if (h->root.type == bfd_link_hash_indirect)
1688 if (h->root.type == bfd_link_hash_warning)
1689 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1691 if (h->dynindx == -1
1695 struct bfd_elf_version_tree *t;
1696 struct bfd_elf_version_expr *d;
1698 for (t = eif->verdefs; t != NULL; t = t->next)
1700 if (t->globals.list != NULL)
1702 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1707 if (t->locals.list != NULL)
1709 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1718 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1729 /* Look through the symbols which are defined in other shared
1730 libraries and referenced here. Update the list of version
1731 dependencies. This will be put into the .gnu.version_r section.
1732 This function is called via elf_link_hash_traverse. */
1735 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1738 struct elf_find_verdep_info *rinfo = data;
1739 Elf_Internal_Verneed *t;
1740 Elf_Internal_Vernaux *a;
1743 if (h->root.type == bfd_link_hash_warning)
1744 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1746 /* We only care about symbols defined in shared objects with version
1751 || h->verinfo.verdef == NULL)
1754 /* See if we already know about this version. */
1755 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1757 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1760 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1761 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1767 /* This is a new version. Add it to tree we are building. */
1772 t = bfd_zalloc (rinfo->output_bfd, amt);
1775 rinfo->failed = TRUE;
1779 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1780 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1781 elf_tdata (rinfo->output_bfd)->verref = t;
1785 a = bfd_zalloc (rinfo->output_bfd, amt);
1787 /* Note that we are copying a string pointer here, and testing it
1788 above. If bfd_elf_string_from_elf_section is ever changed to
1789 discard the string data when low in memory, this will have to be
1791 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1793 a->vna_flags = h->verinfo.verdef->vd_flags;
1794 a->vna_nextptr = t->vn_auxptr;
1796 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1799 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1806 /* Figure out appropriate versions for all the symbols. We may not
1807 have the version number script until we have read all of the input
1808 files, so until that point we don't know which symbols should be
1809 local. This function is called via elf_link_hash_traverse. */
1812 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1814 struct elf_assign_sym_version_info *sinfo;
1815 struct bfd_link_info *info;
1816 const struct elf_backend_data *bed;
1817 struct elf_info_failed eif;
1824 if (h->root.type == bfd_link_hash_warning)
1825 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1827 /* Fix the symbol flags. */
1830 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1833 sinfo->failed = TRUE;
1837 /* We only need version numbers for symbols defined in regular
1839 if (!h->def_regular)
1842 bed = get_elf_backend_data (sinfo->output_bfd);
1843 p = strchr (h->root.root.string, ELF_VER_CHR);
1844 if (p != NULL && h->verinfo.vertree == NULL)
1846 struct bfd_elf_version_tree *t;
1851 /* There are two consecutive ELF_VER_CHR characters if this is
1852 not a hidden symbol. */
1854 if (*p == ELF_VER_CHR)
1860 /* If there is no version string, we can just return out. */
1868 /* Look for the version. If we find it, it is no longer weak. */
1869 for (t = sinfo->verdefs; t != NULL; t = t->next)
1871 if (strcmp (t->name, p) == 0)
1875 struct bfd_elf_version_expr *d;
1877 len = p - h->root.root.string;
1878 alc = bfd_malloc (len);
1881 memcpy (alc, h->root.root.string, len - 1);
1882 alc[len - 1] = '\0';
1883 if (alc[len - 2] == ELF_VER_CHR)
1884 alc[len - 2] = '\0';
1886 h->verinfo.vertree = t;
1890 if (t->globals.list != NULL)
1891 d = (*t->match) (&t->globals, NULL, alc);
1893 /* See if there is anything to force this symbol to
1895 if (d == NULL && t->locals.list != NULL)
1897 d = (*t->match) (&t->locals, NULL, alc);
1900 && ! info->export_dynamic)
1901 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1909 /* If we are building an application, we need to create a
1910 version node for this version. */
1911 if (t == NULL && info->executable)
1913 struct bfd_elf_version_tree **pp;
1916 /* If we aren't going to export this symbol, we don't need
1917 to worry about it. */
1918 if (h->dynindx == -1)
1922 t = bfd_zalloc (sinfo->output_bfd, amt);
1925 sinfo->failed = TRUE;
1930 t->name_indx = (unsigned int) -1;
1934 /* Don't count anonymous version tag. */
1935 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1937 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1939 t->vernum = version_index;
1943 h->verinfo.vertree = t;
1947 /* We could not find the version for a symbol when
1948 generating a shared archive. Return an error. */
1949 (*_bfd_error_handler)
1950 (_("%B: undefined versioned symbol name %s"),
1951 sinfo->output_bfd, h->root.root.string);
1952 bfd_set_error (bfd_error_bad_value);
1953 sinfo->failed = TRUE;
1961 /* If we don't have a version for this symbol, see if we can find
1963 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1965 struct bfd_elf_version_tree *t;
1966 struct bfd_elf_version_tree *local_ver;
1967 struct bfd_elf_version_expr *d;
1969 /* See if can find what version this symbol is in. If the
1970 symbol is supposed to be local, then don't actually register
1973 for (t = sinfo->verdefs; t != NULL; t = t->next)
1975 if (t->globals.list != NULL)
1977 bfd_boolean matched;
1981 while ((d = (*t->match) (&t->globals, d,
1982 h->root.root.string)) != NULL)
1987 /* There is a version without definition. Make
1988 the symbol the default definition for this
1990 h->verinfo.vertree = t;
1998 /* There is no undefined version for this symbol. Hide the
2000 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2003 if (t->locals.list != NULL)
2006 while ((d = (*t->match) (&t->locals, d,
2007 h->root.root.string)) != NULL)
2010 /* If the match is "*", keep looking for a more
2011 explicit, perhaps even global, match.
2012 XXX: Shouldn't this be !d->wildcard instead? */
2013 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
2022 if (local_ver != NULL)
2024 h->verinfo.vertree = local_ver;
2025 if (h->dynindx != -1
2026 && ! info->export_dynamic)
2028 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2036 /* Read and swap the relocs from the section indicated by SHDR. This
2037 may be either a REL or a RELA section. The relocations are
2038 translated into RELA relocations and stored in INTERNAL_RELOCS,
2039 which should have already been allocated to contain enough space.
2040 The EXTERNAL_RELOCS are a buffer where the external form of the
2041 relocations should be stored.
2043 Returns FALSE if something goes wrong. */
2046 elf_link_read_relocs_from_section (bfd *abfd,
2048 Elf_Internal_Shdr *shdr,
2049 void *external_relocs,
2050 Elf_Internal_Rela *internal_relocs)
2052 const struct elf_backend_data *bed;
2053 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2054 const bfd_byte *erela;
2055 const bfd_byte *erelaend;
2056 Elf_Internal_Rela *irela;
2057 Elf_Internal_Shdr *symtab_hdr;
2060 /* Position ourselves at the start of the section. */
2061 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2064 /* Read the relocations. */
2065 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2068 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2069 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
2071 bed = get_elf_backend_data (abfd);
2073 /* Convert the external relocations to the internal format. */
2074 if (shdr->sh_entsize == bed->s->sizeof_rel)
2075 swap_in = bed->s->swap_reloc_in;
2076 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2077 swap_in = bed->s->swap_reloca_in;
2080 bfd_set_error (bfd_error_wrong_format);
2084 erela = external_relocs;
2085 erelaend = erela + shdr->sh_size;
2086 irela = internal_relocs;
2087 while (erela < erelaend)
2091 (*swap_in) (abfd, erela, irela);
2092 r_symndx = ELF32_R_SYM (irela->r_info);
2093 if (bed->s->arch_size == 64)
2095 if ((size_t) r_symndx >= nsyms)
2097 (*_bfd_error_handler)
2098 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2099 " for offset 0x%lx in section `%A'"),
2101 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2102 bfd_set_error (bfd_error_bad_value);
2105 irela += bed->s->int_rels_per_ext_rel;
2106 erela += shdr->sh_entsize;
2112 /* Read and swap the relocs for a section O. They may have been
2113 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2114 not NULL, they are used as buffers to read into. They are known to
2115 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2116 the return value is allocated using either malloc or bfd_alloc,
2117 according to the KEEP_MEMORY argument. If O has two relocation
2118 sections (both REL and RELA relocations), then the REL_HDR
2119 relocations will appear first in INTERNAL_RELOCS, followed by the
2120 REL_HDR2 relocations. */
2123 _bfd_elf_link_read_relocs (bfd *abfd,
2125 void *external_relocs,
2126 Elf_Internal_Rela *internal_relocs,
2127 bfd_boolean keep_memory)
2129 Elf_Internal_Shdr *rel_hdr;
2130 void *alloc1 = NULL;
2131 Elf_Internal_Rela *alloc2 = NULL;
2132 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2134 if (elf_section_data (o)->relocs != NULL)
2135 return elf_section_data (o)->relocs;
2137 if (o->reloc_count == 0)
2140 rel_hdr = &elf_section_data (o)->rel_hdr;
2142 if (internal_relocs == NULL)
2146 size = o->reloc_count;
2147 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2149 internal_relocs = bfd_alloc (abfd, size);
2151 internal_relocs = alloc2 = bfd_malloc (size);
2152 if (internal_relocs == NULL)
2156 if (external_relocs == NULL)
2158 bfd_size_type size = rel_hdr->sh_size;
2160 if (elf_section_data (o)->rel_hdr2)
2161 size += elf_section_data (o)->rel_hdr2->sh_size;
2162 alloc1 = bfd_malloc (size);
2165 external_relocs = alloc1;
2168 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2172 if (elf_section_data (o)->rel_hdr2
2173 && (!elf_link_read_relocs_from_section
2175 elf_section_data (o)->rel_hdr2,
2176 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2177 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2178 * bed->s->int_rels_per_ext_rel))))
2181 /* Cache the results for next time, if we can. */
2183 elf_section_data (o)->relocs = internal_relocs;
2188 /* Don't free alloc2, since if it was allocated we are passing it
2189 back (under the name of internal_relocs). */
2191 return internal_relocs;
2201 /* Compute the size of, and allocate space for, REL_HDR which is the
2202 section header for a section containing relocations for O. */
2205 _bfd_elf_link_size_reloc_section (bfd *abfd,
2206 Elf_Internal_Shdr *rel_hdr,
2209 bfd_size_type reloc_count;
2210 bfd_size_type num_rel_hashes;
2212 /* Figure out how many relocations there will be. */
2213 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2214 reloc_count = elf_section_data (o)->rel_count;
2216 reloc_count = elf_section_data (o)->rel_count2;
2218 num_rel_hashes = o->reloc_count;
2219 if (num_rel_hashes < reloc_count)
2220 num_rel_hashes = reloc_count;
2222 /* That allows us to calculate the size of the section. */
2223 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2225 /* The contents field must last into write_object_contents, so we
2226 allocate it with bfd_alloc rather than malloc. Also since we
2227 cannot be sure that the contents will actually be filled in,
2228 we zero the allocated space. */
2229 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2230 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2233 /* We only allocate one set of hash entries, so we only do it the
2234 first time we are called. */
2235 if (elf_section_data (o)->rel_hashes == NULL
2238 struct elf_link_hash_entry **p;
2240 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2244 elf_section_data (o)->rel_hashes = p;
2250 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2251 originated from the section given by INPUT_REL_HDR) to the
2255 _bfd_elf_link_output_relocs (bfd *output_bfd,
2256 asection *input_section,
2257 Elf_Internal_Shdr *input_rel_hdr,
2258 Elf_Internal_Rela *internal_relocs,
2259 struct elf_link_hash_entry **rel_hash
2262 Elf_Internal_Rela *irela;
2263 Elf_Internal_Rela *irelaend;
2265 Elf_Internal_Shdr *output_rel_hdr;
2266 asection *output_section;
2267 unsigned int *rel_countp = NULL;
2268 const struct elf_backend_data *bed;
2269 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2271 output_section = input_section->output_section;
2272 output_rel_hdr = NULL;
2274 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2275 == input_rel_hdr->sh_entsize)
2277 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2278 rel_countp = &elf_section_data (output_section)->rel_count;
2280 else if (elf_section_data (output_section)->rel_hdr2
2281 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2282 == input_rel_hdr->sh_entsize))
2284 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2285 rel_countp = &elf_section_data (output_section)->rel_count2;
2289 (*_bfd_error_handler)
2290 (_("%B: relocation size mismatch in %B section %A"),
2291 output_bfd, input_section->owner, input_section);
2292 bfd_set_error (bfd_error_wrong_object_format);
2296 bed = get_elf_backend_data (output_bfd);
2297 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2298 swap_out = bed->s->swap_reloc_out;
2299 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2300 swap_out = bed->s->swap_reloca_out;
2304 erel = output_rel_hdr->contents;
2305 erel += *rel_countp * input_rel_hdr->sh_entsize;
2306 irela = internal_relocs;
2307 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2308 * bed->s->int_rels_per_ext_rel);
2309 while (irela < irelaend)
2311 (*swap_out) (output_bfd, irela, erel);
2312 irela += bed->s->int_rels_per_ext_rel;
2313 erel += input_rel_hdr->sh_entsize;
2316 /* Bump the counter, so that we know where to add the next set of
2318 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2323 /* Make weak undefined symbols in PIE dynamic. */
2326 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2327 struct elf_link_hash_entry *h)
2331 && h->root.type == bfd_link_hash_undefweak)
2332 return bfd_elf_link_record_dynamic_symbol (info, h);
2337 /* Fix up the flags for a symbol. This handles various cases which
2338 can only be fixed after all the input files are seen. This is
2339 currently called by both adjust_dynamic_symbol and
2340 assign_sym_version, which is unnecessary but perhaps more robust in
2341 the face of future changes. */
2344 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2345 struct elf_info_failed *eif)
2347 const struct elf_backend_data *bed = NULL;
2349 /* If this symbol was mentioned in a non-ELF file, try to set
2350 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2351 permit a non-ELF file to correctly refer to a symbol defined in
2352 an ELF dynamic object. */
2355 while (h->root.type == bfd_link_hash_indirect)
2356 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2358 if (h->root.type != bfd_link_hash_defined
2359 && h->root.type != bfd_link_hash_defweak)
2362 h->ref_regular_nonweak = 1;
2366 if (h->root.u.def.section->owner != NULL
2367 && (bfd_get_flavour (h->root.u.def.section->owner)
2368 == bfd_target_elf_flavour))
2371 h->ref_regular_nonweak = 1;
2377 if (h->dynindx == -1
2381 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2390 /* Unfortunately, NON_ELF is only correct if the symbol
2391 was first seen in a non-ELF file. Fortunately, if the symbol
2392 was first seen in an ELF file, we're probably OK unless the
2393 symbol was defined in a non-ELF file. Catch that case here.
2394 FIXME: We're still in trouble if the symbol was first seen in
2395 a dynamic object, and then later in a non-ELF regular object. */
2396 if ((h->root.type == bfd_link_hash_defined
2397 || h->root.type == bfd_link_hash_defweak)
2399 && (h->root.u.def.section->owner != NULL
2400 ? (bfd_get_flavour (h->root.u.def.section->owner)
2401 != bfd_target_elf_flavour)
2402 : (bfd_is_abs_section (h->root.u.def.section)
2403 && !h->def_dynamic)))
2407 /* Backend specific symbol fixup. */
2408 if (elf_hash_table (eif->info)->dynobj)
2410 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2411 if (bed->elf_backend_fixup_symbol
2412 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2416 /* If this is a final link, and the symbol was defined as a common
2417 symbol in a regular object file, and there was no definition in
2418 any dynamic object, then the linker will have allocated space for
2419 the symbol in a common section but the DEF_REGULAR
2420 flag will not have been set. */
2421 if (h->root.type == bfd_link_hash_defined
2425 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2428 /* If -Bsymbolic was used (which means to bind references to global
2429 symbols to the definition within the shared object), and this
2430 symbol was defined in a regular object, then it actually doesn't
2431 need a PLT entry. Likewise, if the symbol has non-default
2432 visibility. If the symbol has hidden or internal visibility, we
2433 will force it local. */
2435 && eif->info->shared
2436 && is_elf_hash_table (eif->info->hash)
2437 && (SYMBOLIC_BIND (eif->info, h)
2438 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2441 bfd_boolean force_local;
2443 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2444 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2445 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2448 /* If a weak undefined symbol has non-default visibility, we also
2449 hide it from the dynamic linker. */
2450 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2451 && h->root.type == bfd_link_hash_undefweak)
2453 const struct elf_backend_data *bed;
2454 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2455 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2458 /* If this is a weak defined symbol in a dynamic object, and we know
2459 the real definition in the dynamic object, copy interesting flags
2460 over to the real definition. */
2461 if (h->u.weakdef != NULL)
2463 struct elf_link_hash_entry *weakdef;
2465 weakdef = h->u.weakdef;
2466 if (h->root.type == bfd_link_hash_indirect)
2467 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2469 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2470 || h->root.type == bfd_link_hash_defweak);
2471 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2472 || weakdef->root.type == bfd_link_hash_defweak);
2473 BFD_ASSERT (weakdef->def_dynamic);
2475 /* If the real definition is defined by a regular object file,
2476 don't do anything special. See the longer description in
2477 _bfd_elf_adjust_dynamic_symbol, below. */
2478 if (weakdef->def_regular)
2479 h->u.weakdef = NULL;
2481 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef,
2488 /* Make the backend pick a good value for a dynamic symbol. This is
2489 called via elf_link_hash_traverse, and also calls itself
2493 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2495 struct elf_info_failed *eif = data;
2497 const struct elf_backend_data *bed;
2499 if (! is_elf_hash_table (eif->info->hash))
2502 if (h->root.type == bfd_link_hash_warning)
2504 h->got = elf_hash_table (eif->info)->init_got_offset;
2505 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2507 /* When warning symbols are created, they **replace** the "real"
2508 entry in the hash table, thus we never get to see the real
2509 symbol in a hash traversal. So look at it now. */
2510 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2513 /* Ignore indirect symbols. These are added by the versioning code. */
2514 if (h->root.type == bfd_link_hash_indirect)
2517 /* Fix the symbol flags. */
2518 if (! _bfd_elf_fix_symbol_flags (h, eif))
2521 /* If this symbol does not require a PLT entry, and it is not
2522 defined by a dynamic object, or is not referenced by a regular
2523 object, ignore it. We do have to handle a weak defined symbol,
2524 even if no regular object refers to it, if we decided to add it
2525 to the dynamic symbol table. FIXME: Do we normally need to worry
2526 about symbols which are defined by one dynamic object and
2527 referenced by another one? */
2532 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2534 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2538 /* If we've already adjusted this symbol, don't do it again. This
2539 can happen via a recursive call. */
2540 if (h->dynamic_adjusted)
2543 /* Don't look at this symbol again. Note that we must set this
2544 after checking the above conditions, because we may look at a
2545 symbol once, decide not to do anything, and then get called
2546 recursively later after REF_REGULAR is set below. */
2547 h->dynamic_adjusted = 1;
2549 /* If this is a weak definition, and we know a real definition, and
2550 the real symbol is not itself defined by a regular object file,
2551 then get a good value for the real definition. We handle the
2552 real symbol first, for the convenience of the backend routine.
2554 Note that there is a confusing case here. If the real definition
2555 is defined by a regular object file, we don't get the real symbol
2556 from the dynamic object, but we do get the weak symbol. If the
2557 processor backend uses a COPY reloc, then if some routine in the
2558 dynamic object changes the real symbol, we will not see that
2559 change in the corresponding weak symbol. This is the way other
2560 ELF linkers work as well, and seems to be a result of the shared
2563 I will clarify this issue. Most SVR4 shared libraries define the
2564 variable _timezone and define timezone as a weak synonym. The
2565 tzset call changes _timezone. If you write
2566 extern int timezone;
2568 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2569 you might expect that, since timezone is a synonym for _timezone,
2570 the same number will print both times. However, if the processor
2571 backend uses a COPY reloc, then actually timezone will be copied
2572 into your process image, and, since you define _timezone
2573 yourself, _timezone will not. Thus timezone and _timezone will
2574 wind up at different memory locations. The tzset call will set
2575 _timezone, leaving timezone unchanged. */
2577 if (h->u.weakdef != NULL)
2579 /* If we get to this point, we know there is an implicit
2580 reference by a regular object file via the weak symbol H.
2581 FIXME: Is this really true? What if the traversal finds
2582 H->U.WEAKDEF before it finds H? */
2583 h->u.weakdef->ref_regular = 1;
2585 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2589 /* If a symbol has no type and no size and does not require a PLT
2590 entry, then we are probably about to do the wrong thing here: we
2591 are probably going to create a COPY reloc for an empty object.
2592 This case can arise when a shared object is built with assembly
2593 code, and the assembly code fails to set the symbol type. */
2595 && h->type == STT_NOTYPE
2597 (*_bfd_error_handler)
2598 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2599 h->root.root.string);
2601 dynobj = elf_hash_table (eif->info)->dynobj;
2602 bed = get_elf_backend_data (dynobj);
2603 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2612 /* Adjust all external symbols pointing into SEC_MERGE sections
2613 to reflect the object merging within the sections. */
2616 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2620 if (h->root.type == bfd_link_hash_warning)
2621 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2623 if ((h->root.type == bfd_link_hash_defined
2624 || h->root.type == bfd_link_hash_defweak)
2625 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2626 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2628 bfd *output_bfd = data;
2630 h->root.u.def.value =
2631 _bfd_merged_section_offset (output_bfd,
2632 &h->root.u.def.section,
2633 elf_section_data (sec)->sec_info,
2634 h->root.u.def.value);
2640 /* Returns false if the symbol referred to by H should be considered
2641 to resolve local to the current module, and true if it should be
2642 considered to bind dynamically. */
2645 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2646 struct bfd_link_info *info,
2647 bfd_boolean ignore_protected)
2649 bfd_boolean binding_stays_local_p;
2650 const struct elf_backend_data *bed;
2651 struct elf_link_hash_table *hash_table;
2656 while (h->root.type == bfd_link_hash_indirect
2657 || h->root.type == bfd_link_hash_warning)
2658 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2660 /* If it was forced local, then clearly it's not dynamic. */
2661 if (h->dynindx == -1)
2663 if (h->forced_local)
2666 /* Identify the cases where name binding rules say that a
2667 visible symbol resolves locally. */
2668 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2670 switch (ELF_ST_VISIBILITY (h->other))
2677 hash_table = elf_hash_table (info);
2678 if (!is_elf_hash_table (hash_table))
2681 bed = get_elf_backend_data (hash_table->dynobj);
2683 /* Proper resolution for function pointer equality may require
2684 that these symbols perhaps be resolved dynamically, even though
2685 we should be resolving them to the current module. */
2686 if (!ignore_protected || !bed->is_function_type (h->type))
2687 binding_stays_local_p = TRUE;
2694 /* If it isn't defined locally, then clearly it's dynamic. */
2695 if (!h->def_regular)
2698 /* Otherwise, the symbol is dynamic if binding rules don't tell
2699 us that it remains local. */
2700 return !binding_stays_local_p;
2703 /* Return true if the symbol referred to by H should be considered
2704 to resolve local to the current module, and false otherwise. Differs
2705 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2706 undefined symbols and weak symbols. */
2709 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2710 struct bfd_link_info *info,
2711 bfd_boolean local_protected)
2713 const struct elf_backend_data *bed;
2714 struct elf_link_hash_table *hash_table;
2716 /* If it's a local sym, of course we resolve locally. */
2720 /* Common symbols that become definitions don't get the DEF_REGULAR
2721 flag set, so test it first, and don't bail out. */
2722 if (ELF_COMMON_DEF_P (h))
2724 /* If we don't have a definition in a regular file, then we can't
2725 resolve locally. The sym is either undefined or dynamic. */
2726 else if (!h->def_regular)
2729 /* Forced local symbols resolve locally. */
2730 if (h->forced_local)
2733 /* As do non-dynamic symbols. */
2734 if (h->dynindx == -1)
2737 /* At this point, we know the symbol is defined and dynamic. In an
2738 executable it must resolve locally, likewise when building symbolic
2739 shared libraries. */
2740 if (info->executable || SYMBOLIC_BIND (info, h))
2743 /* Now deal with defined dynamic symbols in shared libraries. Ones
2744 with default visibility might not resolve locally. */
2745 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2748 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2749 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2752 hash_table = elf_hash_table (info);
2753 if (!is_elf_hash_table (hash_table))
2756 bed = get_elf_backend_data (hash_table->dynobj);
2758 /* STV_PROTECTED non-function symbols are local. */
2759 if (!bed->is_function_type (h->type))
2762 /* Function pointer equality tests may require that STV_PROTECTED
2763 symbols be treated as dynamic symbols, even when we know that the
2764 dynamic linker will resolve them locally. */
2765 return local_protected;
2768 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2769 aligned. Returns the first TLS output section. */
2771 struct bfd_section *
2772 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2774 struct bfd_section *sec, *tls;
2775 unsigned int align = 0;
2777 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2778 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2782 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2783 if (sec->alignment_power > align)
2784 align = sec->alignment_power;
2786 elf_hash_table (info)->tls_sec = tls;
2788 /* Ensure the alignment of the first section is the largest alignment,
2789 so that the tls segment starts aligned. */
2791 tls->alignment_power = align;
2796 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2798 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2799 Elf_Internal_Sym *sym)
2801 const struct elf_backend_data *bed;
2803 /* Local symbols do not count, but target specific ones might. */
2804 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2805 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2808 bed = get_elf_backend_data (abfd);
2809 /* Function symbols do not count. */
2810 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
2813 /* If the section is undefined, then so is the symbol. */
2814 if (sym->st_shndx == SHN_UNDEF)
2817 /* If the symbol is defined in the common section, then
2818 it is a common definition and so does not count. */
2819 if (bed->common_definition (sym))
2822 /* If the symbol is in a target specific section then we
2823 must rely upon the backend to tell us what it is. */
2824 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2825 /* FIXME - this function is not coded yet:
2827 return _bfd_is_global_symbol_definition (abfd, sym);
2829 Instead for now assume that the definition is not global,
2830 Even if this is wrong, at least the linker will behave
2831 in the same way that it used to do. */
2837 /* Search the symbol table of the archive element of the archive ABFD
2838 whose archive map contains a mention of SYMDEF, and determine if
2839 the symbol is defined in this element. */
2841 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2843 Elf_Internal_Shdr * hdr;
2844 bfd_size_type symcount;
2845 bfd_size_type extsymcount;
2846 bfd_size_type extsymoff;
2847 Elf_Internal_Sym *isymbuf;
2848 Elf_Internal_Sym *isym;
2849 Elf_Internal_Sym *isymend;
2852 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2856 if (! bfd_check_format (abfd, bfd_object))
2859 /* If we have already included the element containing this symbol in the
2860 link then we do not need to include it again. Just claim that any symbol
2861 it contains is not a definition, so that our caller will not decide to
2862 (re)include this element. */
2863 if (abfd->archive_pass)
2866 /* Select the appropriate symbol table. */
2867 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2868 hdr = &elf_tdata (abfd)->symtab_hdr;
2870 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2872 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2874 /* The sh_info field of the symtab header tells us where the
2875 external symbols start. We don't care about the local symbols. */
2876 if (elf_bad_symtab (abfd))
2878 extsymcount = symcount;
2883 extsymcount = symcount - hdr->sh_info;
2884 extsymoff = hdr->sh_info;
2887 if (extsymcount == 0)
2890 /* Read in the symbol table. */
2891 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2893 if (isymbuf == NULL)
2896 /* Scan the symbol table looking for SYMDEF. */
2898 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2902 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2907 if (strcmp (name, symdef->name) == 0)
2909 result = is_global_data_symbol_definition (abfd, isym);
2919 /* Add an entry to the .dynamic table. */
2922 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2926 struct elf_link_hash_table *hash_table;
2927 const struct elf_backend_data *bed;
2929 bfd_size_type newsize;
2930 bfd_byte *newcontents;
2931 Elf_Internal_Dyn dyn;
2933 hash_table = elf_hash_table (info);
2934 if (! is_elf_hash_table (hash_table))
2937 bed = get_elf_backend_data (hash_table->dynobj);
2938 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2939 BFD_ASSERT (s != NULL);
2941 newsize = s->size + bed->s->sizeof_dyn;
2942 newcontents = bfd_realloc (s->contents, newsize);
2943 if (newcontents == NULL)
2947 dyn.d_un.d_val = val;
2948 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2951 s->contents = newcontents;
2956 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2957 otherwise just check whether one already exists. Returns -1 on error,
2958 1 if a DT_NEEDED tag already exists, and 0 on success. */
2961 elf_add_dt_needed_tag (bfd *abfd,
2962 struct bfd_link_info *info,
2966 struct elf_link_hash_table *hash_table;
2967 bfd_size_type oldsize;
2968 bfd_size_type strindex;
2970 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
2973 hash_table = elf_hash_table (info);
2974 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2975 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2976 if (strindex == (bfd_size_type) -1)
2979 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2982 const struct elf_backend_data *bed;
2985 bed = get_elf_backend_data (hash_table->dynobj);
2986 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2988 for (extdyn = sdyn->contents;
2989 extdyn < sdyn->contents + sdyn->size;
2990 extdyn += bed->s->sizeof_dyn)
2992 Elf_Internal_Dyn dyn;
2994 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2995 if (dyn.d_tag == DT_NEEDED
2996 && dyn.d_un.d_val == strindex)
2998 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3006 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3009 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3013 /* We were just checking for existence of the tag. */
3014 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3019 /* Sort symbol by value and section. */
3021 elf_sort_symbol (const void *arg1, const void *arg2)
3023 const struct elf_link_hash_entry *h1;
3024 const struct elf_link_hash_entry *h2;
3025 bfd_signed_vma vdiff;
3027 h1 = *(const struct elf_link_hash_entry **) arg1;
3028 h2 = *(const struct elf_link_hash_entry **) arg2;
3029 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3031 return vdiff > 0 ? 1 : -1;
3034 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3036 return sdiff > 0 ? 1 : -1;
3041 /* This function is used to adjust offsets into .dynstr for
3042 dynamic symbols. This is called via elf_link_hash_traverse. */
3045 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3047 struct elf_strtab_hash *dynstr = data;
3049 if (h->root.type == bfd_link_hash_warning)
3050 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3052 if (h->dynindx != -1)
3053 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3057 /* Assign string offsets in .dynstr, update all structures referencing
3061 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3063 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3064 struct elf_link_local_dynamic_entry *entry;
3065 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3066 bfd *dynobj = hash_table->dynobj;
3069 const struct elf_backend_data *bed;
3072 _bfd_elf_strtab_finalize (dynstr);
3073 size = _bfd_elf_strtab_size (dynstr);
3075 bed = get_elf_backend_data (dynobj);
3076 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3077 BFD_ASSERT (sdyn != NULL);
3079 /* Update all .dynamic entries referencing .dynstr strings. */
3080 for (extdyn = sdyn->contents;
3081 extdyn < sdyn->contents + sdyn->size;
3082 extdyn += bed->s->sizeof_dyn)
3084 Elf_Internal_Dyn dyn;
3086 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3090 dyn.d_un.d_val = size;
3098 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3103 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3106 /* Now update local dynamic symbols. */
3107 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3108 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3109 entry->isym.st_name);
3111 /* And the rest of dynamic symbols. */
3112 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3114 /* Adjust version definitions. */
3115 if (elf_tdata (output_bfd)->cverdefs)
3120 Elf_Internal_Verdef def;
3121 Elf_Internal_Verdaux defaux;
3123 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3127 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3129 p += sizeof (Elf_External_Verdef);
3130 if (def.vd_aux != sizeof (Elf_External_Verdef))
3132 for (i = 0; i < def.vd_cnt; ++i)
3134 _bfd_elf_swap_verdaux_in (output_bfd,
3135 (Elf_External_Verdaux *) p, &defaux);
3136 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3138 _bfd_elf_swap_verdaux_out (output_bfd,
3139 &defaux, (Elf_External_Verdaux *) p);
3140 p += sizeof (Elf_External_Verdaux);
3143 while (def.vd_next);
3146 /* Adjust version references. */
3147 if (elf_tdata (output_bfd)->verref)
3152 Elf_Internal_Verneed need;
3153 Elf_Internal_Vernaux needaux;
3155 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3159 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3161 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3162 _bfd_elf_swap_verneed_out (output_bfd, &need,
3163 (Elf_External_Verneed *) p);
3164 p += sizeof (Elf_External_Verneed);
3165 for (i = 0; i < need.vn_cnt; ++i)
3167 _bfd_elf_swap_vernaux_in (output_bfd,
3168 (Elf_External_Vernaux *) p, &needaux);
3169 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3171 _bfd_elf_swap_vernaux_out (output_bfd,
3173 (Elf_External_Vernaux *) p);
3174 p += sizeof (Elf_External_Vernaux);
3177 while (need.vn_next);
3183 /* Add symbols from an ELF object file to the linker hash table. */
3186 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3188 Elf_Internal_Shdr *hdr;
3189 bfd_size_type symcount;
3190 bfd_size_type extsymcount;
3191 bfd_size_type extsymoff;
3192 struct elf_link_hash_entry **sym_hash;
3193 bfd_boolean dynamic;
3194 Elf_External_Versym *extversym = NULL;
3195 Elf_External_Versym *ever;
3196 struct elf_link_hash_entry *weaks;
3197 struct elf_link_hash_entry **nondeflt_vers = NULL;
3198 bfd_size_type nondeflt_vers_cnt = 0;
3199 Elf_Internal_Sym *isymbuf = NULL;
3200 Elf_Internal_Sym *isym;
3201 Elf_Internal_Sym *isymend;
3202 const struct elf_backend_data *bed;
3203 bfd_boolean add_needed;
3204 struct elf_link_hash_table *htab;
3206 void *alloc_mark = NULL;
3207 struct bfd_hash_entry **old_table = NULL;
3208 unsigned int old_size = 0;
3209 unsigned int old_count = 0;
3210 void *old_tab = NULL;
3213 struct bfd_link_hash_entry *old_undefs = NULL;
3214 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3215 long old_dynsymcount = 0;
3217 size_t hashsize = 0;
3219 htab = elf_hash_table (info);
3220 bed = get_elf_backend_data (abfd);
3222 if ((abfd->flags & DYNAMIC) == 0)
3228 /* You can't use -r against a dynamic object. Also, there's no
3229 hope of using a dynamic object which does not exactly match
3230 the format of the output file. */
3231 if (info->relocatable
3232 || !is_elf_hash_table (htab)
3233 || htab->root.creator != abfd->xvec)
3235 if (info->relocatable)
3236 bfd_set_error (bfd_error_invalid_operation);
3238 bfd_set_error (bfd_error_wrong_format);
3243 /* As a GNU extension, any input sections which are named
3244 .gnu.warning.SYMBOL are treated as warning symbols for the given
3245 symbol. This differs from .gnu.warning sections, which generate
3246 warnings when they are included in an output file. */
3247 if (info->executable)
3251 for (s = abfd->sections; s != NULL; s = s->next)
3255 name = bfd_get_section_name (abfd, s);
3256 if (CONST_STRNEQ (name, ".gnu.warning."))
3261 name += sizeof ".gnu.warning." - 1;
3263 /* If this is a shared object, then look up the symbol
3264 in the hash table. If it is there, and it is already
3265 been defined, then we will not be using the entry
3266 from this shared object, so we don't need to warn.
3267 FIXME: If we see the definition in a regular object
3268 later on, we will warn, but we shouldn't. The only
3269 fix is to keep track of what warnings we are supposed
3270 to emit, and then handle them all at the end of the
3274 struct elf_link_hash_entry *h;
3276 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3278 /* FIXME: What about bfd_link_hash_common? */
3280 && (h->root.type == bfd_link_hash_defined
3281 || h->root.type == bfd_link_hash_defweak))
3283 /* We don't want to issue this warning. Clobber
3284 the section size so that the warning does not
3285 get copied into the output file. */
3292 msg = bfd_alloc (abfd, sz + 1);
3296 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3301 if (! (_bfd_generic_link_add_one_symbol
3302 (info, abfd, name, BSF_WARNING, s, 0, msg,
3303 FALSE, bed->collect, NULL)))
3306 if (! info->relocatable)
3308 /* Clobber the section size so that the warning does
3309 not get copied into the output file. */
3312 /* Also set SEC_EXCLUDE, so that symbols defined in
3313 the warning section don't get copied to the output. */
3314 s->flags |= SEC_EXCLUDE;
3323 /* If we are creating a shared library, create all the dynamic
3324 sections immediately. We need to attach them to something,
3325 so we attach them to this BFD, provided it is the right
3326 format. FIXME: If there are no input BFD's of the same
3327 format as the output, we can't make a shared library. */
3329 && is_elf_hash_table (htab)
3330 && htab->root.creator == abfd->xvec
3331 && !htab->dynamic_sections_created)
3333 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3337 else if (!is_elf_hash_table (htab))
3342 const char *soname = NULL;
3343 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3346 /* ld --just-symbols and dynamic objects don't mix very well.
3347 ld shouldn't allow it. */
3348 if ((s = abfd->sections) != NULL
3349 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3352 /* If this dynamic lib was specified on the command line with
3353 --as-needed in effect, then we don't want to add a DT_NEEDED
3354 tag unless the lib is actually used. Similary for libs brought
3355 in by another lib's DT_NEEDED. When --no-add-needed is used
3356 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3357 any dynamic library in DT_NEEDED tags in the dynamic lib at
3359 add_needed = (elf_dyn_lib_class (abfd)
3360 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3361 | DYN_NO_NEEDED)) == 0;
3363 s = bfd_get_section_by_name (abfd, ".dynamic");
3369 unsigned long shlink;
3371 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3372 goto error_free_dyn;
3374 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3376 goto error_free_dyn;
3377 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3379 for (extdyn = dynbuf;
3380 extdyn < dynbuf + s->size;
3381 extdyn += bed->s->sizeof_dyn)
3383 Elf_Internal_Dyn dyn;
3385 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3386 if (dyn.d_tag == DT_SONAME)
3388 unsigned int tagv = dyn.d_un.d_val;
3389 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3391 goto error_free_dyn;
3393 if (dyn.d_tag == DT_NEEDED)
3395 struct bfd_link_needed_list *n, **pn;
3397 unsigned int tagv = dyn.d_un.d_val;
3399 amt = sizeof (struct bfd_link_needed_list);
3400 n = bfd_alloc (abfd, amt);
3401 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3402 if (n == NULL || fnm == NULL)
3403 goto error_free_dyn;
3404 amt = strlen (fnm) + 1;
3405 anm = bfd_alloc (abfd, amt);
3407 goto error_free_dyn;
3408 memcpy (anm, fnm, amt);
3412 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3416 if (dyn.d_tag == DT_RUNPATH)
3418 struct bfd_link_needed_list *n, **pn;
3420 unsigned int tagv = dyn.d_un.d_val;
3422 amt = sizeof (struct bfd_link_needed_list);
3423 n = bfd_alloc (abfd, amt);
3424 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3425 if (n == NULL || fnm == NULL)
3426 goto error_free_dyn;
3427 amt = strlen (fnm) + 1;
3428 anm = bfd_alloc (abfd, amt);
3430 goto error_free_dyn;
3431 memcpy (anm, fnm, amt);
3435 for (pn = & runpath;
3441 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3442 if (!runpath && dyn.d_tag == DT_RPATH)
3444 struct bfd_link_needed_list *n, **pn;
3446 unsigned int tagv = dyn.d_un.d_val;
3448 amt = sizeof (struct bfd_link_needed_list);
3449 n = bfd_alloc (abfd, amt);
3450 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3451 if (n == NULL || fnm == NULL)
3452 goto error_free_dyn;
3453 amt = strlen (fnm) + 1;
3454 anm = bfd_alloc (abfd, amt);
3461 memcpy (anm, fnm, amt);
3476 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3477 frees all more recently bfd_alloc'd blocks as well. */
3483 struct bfd_link_needed_list **pn;
3484 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3489 /* We do not want to include any of the sections in a dynamic
3490 object in the output file. We hack by simply clobbering the
3491 list of sections in the BFD. This could be handled more
3492 cleanly by, say, a new section flag; the existing
3493 SEC_NEVER_LOAD flag is not the one we want, because that one
3494 still implies that the section takes up space in the output
3496 bfd_section_list_clear (abfd);
3498 /* Find the name to use in a DT_NEEDED entry that refers to this
3499 object. If the object has a DT_SONAME entry, we use it.
3500 Otherwise, if the generic linker stuck something in
3501 elf_dt_name, we use that. Otherwise, we just use the file
3503 if (soname == NULL || *soname == '\0')
3505 soname = elf_dt_name (abfd);
3506 if (soname == NULL || *soname == '\0')
3507 soname = bfd_get_filename (abfd);
3510 /* Save the SONAME because sometimes the linker emulation code
3511 will need to know it. */
3512 elf_dt_name (abfd) = soname;
3514 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3518 /* If we have already included this dynamic object in the
3519 link, just ignore it. There is no reason to include a
3520 particular dynamic object more than once. */
3525 /* If this is a dynamic object, we always link against the .dynsym
3526 symbol table, not the .symtab symbol table. The dynamic linker
3527 will only see the .dynsym symbol table, so there is no reason to
3528 look at .symtab for a dynamic object. */
3530 if (! dynamic || elf_dynsymtab (abfd) == 0)
3531 hdr = &elf_tdata (abfd)->symtab_hdr;
3533 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3535 symcount = hdr->sh_size / bed->s->sizeof_sym;
3537 /* The sh_info field of the symtab header tells us where the
3538 external symbols start. We don't care about the local symbols at
3540 if (elf_bad_symtab (abfd))
3542 extsymcount = symcount;
3547 extsymcount = symcount - hdr->sh_info;
3548 extsymoff = hdr->sh_info;
3552 if (extsymcount != 0)
3554 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3556 if (isymbuf == NULL)
3559 /* We store a pointer to the hash table entry for each external
3561 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3562 sym_hash = bfd_alloc (abfd, amt);
3563 if (sym_hash == NULL)
3564 goto error_free_sym;
3565 elf_sym_hashes (abfd) = sym_hash;
3570 /* Read in any version definitions. */
3571 if (!_bfd_elf_slurp_version_tables (abfd,
3572 info->default_imported_symver))
3573 goto error_free_sym;
3575 /* Read in the symbol versions, but don't bother to convert them
3576 to internal format. */
3577 if (elf_dynversym (abfd) != 0)
3579 Elf_Internal_Shdr *versymhdr;
3581 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3582 extversym = bfd_malloc (versymhdr->sh_size);
3583 if (extversym == NULL)
3584 goto error_free_sym;
3585 amt = versymhdr->sh_size;
3586 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3587 || bfd_bread (extversym, amt, abfd) != amt)
3588 goto error_free_vers;
3592 /* If we are loading an as-needed shared lib, save the symbol table
3593 state before we start adding symbols. If the lib turns out
3594 to be unneeded, restore the state. */
3595 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3600 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3602 struct bfd_hash_entry *p;
3603 struct elf_link_hash_entry *h;
3605 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3607 h = (struct elf_link_hash_entry *) p;
3608 entsize += htab->root.table.entsize;
3609 if (h->root.type == bfd_link_hash_warning)
3610 entsize += htab->root.table.entsize;
3614 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3615 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3616 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3617 if (old_tab == NULL)
3618 goto error_free_vers;
3620 /* Remember the current objalloc pointer, so that all mem for
3621 symbols added can later be reclaimed. */
3622 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3623 if (alloc_mark == NULL)
3624 goto error_free_vers;
3626 /* Make a special call to the linker "notice" function to
3627 tell it that we are about to handle an as-needed lib. */
3628 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
3633 /* Clone the symbol table and sym hashes. Remember some
3634 pointers into the symbol table, and dynamic symbol count. */
3635 old_hash = (char *) old_tab + tabsize;
3636 old_ent = (char *) old_hash + hashsize;
3637 memcpy (old_tab, htab->root.table.table, tabsize);
3638 memcpy (old_hash, sym_hash, hashsize);
3639 old_undefs = htab->root.undefs;
3640 old_undefs_tail = htab->root.undefs_tail;
3641 old_table = htab->root.table.table;
3642 old_size = htab->root.table.size;
3643 old_count = htab->root.table.count;
3644 old_dynsymcount = htab->dynsymcount;
3646 for (i = 0; i < htab->root.table.size; i++)
3648 struct bfd_hash_entry *p;
3649 struct elf_link_hash_entry *h;
3651 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3653 memcpy (old_ent, p, htab->root.table.entsize);
3654 old_ent = (char *) old_ent + htab->root.table.entsize;
3655 h = (struct elf_link_hash_entry *) p;
3656 if (h->root.type == bfd_link_hash_warning)
3658 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3659 old_ent = (char *) old_ent + htab->root.table.entsize;
3666 ever = extversym != NULL ? extversym + extsymoff : NULL;
3667 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3669 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3673 asection *sec, *new_sec;
3676 struct elf_link_hash_entry *h;
3677 bfd_boolean definition;
3678 bfd_boolean size_change_ok;
3679 bfd_boolean type_change_ok;
3680 bfd_boolean new_weakdef;
3681 bfd_boolean override;
3683 unsigned int old_alignment;
3688 flags = BSF_NO_FLAGS;
3690 value = isym->st_value;
3692 common = bed->common_definition (isym);
3694 bind = ELF_ST_BIND (isym->st_info);
3695 if (bind == STB_LOCAL)
3697 /* This should be impossible, since ELF requires that all
3698 global symbols follow all local symbols, and that sh_info
3699 point to the first global symbol. Unfortunately, Irix 5
3703 else if (bind == STB_GLOBAL)
3705 if (isym->st_shndx != SHN_UNDEF && !common)
3708 else if (bind == STB_WEAK)
3712 /* Leave it up to the processor backend. */
3715 if (isym->st_shndx == SHN_UNDEF)
3716 sec = bfd_und_section_ptr;
3717 else if (isym->st_shndx < SHN_LORESERVE
3718 || isym->st_shndx > SHN_HIRESERVE)
3720 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3722 sec = bfd_abs_section_ptr;
3723 else if (sec->kept_section)
3725 /* Symbols from discarded section are undefined. We keep
3727 sec = bfd_und_section_ptr;
3728 isym->st_shndx = SHN_UNDEF;
3730 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3733 else if (isym->st_shndx == SHN_ABS)
3734 sec = bfd_abs_section_ptr;
3735 else if (isym->st_shndx == SHN_COMMON)
3737 sec = bfd_com_section_ptr;
3738 /* What ELF calls the size we call the value. What ELF
3739 calls the value we call the alignment. */
3740 value = isym->st_size;
3744 /* Leave it up to the processor backend. */
3747 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3750 goto error_free_vers;
3752 if (isym->st_shndx == SHN_COMMON
3753 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3754 && !info->relocatable)
3756 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3760 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3763 | SEC_LINKER_CREATED
3764 | SEC_THREAD_LOCAL));
3766 goto error_free_vers;
3770 else if (bed->elf_add_symbol_hook)
3772 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3774 goto error_free_vers;
3776 /* The hook function sets the name to NULL if this symbol
3777 should be skipped for some reason. */
3782 /* Sanity check that all possibilities were handled. */
3785 bfd_set_error (bfd_error_bad_value);
3786 goto error_free_vers;
3789 if (bfd_is_und_section (sec)
3790 || bfd_is_com_section (sec))
3795 size_change_ok = FALSE;
3796 type_change_ok = bed->type_change_ok;
3801 if (is_elf_hash_table (htab))
3803 Elf_Internal_Versym iver;
3804 unsigned int vernum = 0;
3809 if (info->default_imported_symver)
3810 /* Use the default symbol version created earlier. */
3811 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3816 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3818 vernum = iver.vs_vers & VERSYM_VERSION;
3820 /* If this is a hidden symbol, or if it is not version
3821 1, we append the version name to the symbol name.
3822 However, we do not modify a non-hidden absolute symbol
3823 if it is not a function, because it might be the version
3824 symbol itself. FIXME: What if it isn't? */
3825 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3827 && (!bfd_is_abs_section (sec)
3828 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
3831 size_t namelen, verlen, newlen;
3834 if (isym->st_shndx != SHN_UNDEF)
3836 if (vernum > elf_tdata (abfd)->cverdefs)
3838 else if (vernum > 1)
3840 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3846 (*_bfd_error_handler)
3847 (_("%B: %s: invalid version %u (max %d)"),
3849 elf_tdata (abfd)->cverdefs);
3850 bfd_set_error (bfd_error_bad_value);
3851 goto error_free_vers;
3856 /* We cannot simply test for the number of
3857 entries in the VERNEED section since the
3858 numbers for the needed versions do not start
3860 Elf_Internal_Verneed *t;
3863 for (t = elf_tdata (abfd)->verref;
3867 Elf_Internal_Vernaux *a;
3869 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3871 if (a->vna_other == vernum)
3873 verstr = a->vna_nodename;
3882 (*_bfd_error_handler)
3883 (_("%B: %s: invalid needed version %d"),
3884 abfd, name, vernum);
3885 bfd_set_error (bfd_error_bad_value);
3886 goto error_free_vers;
3890 namelen = strlen (name);
3891 verlen = strlen (verstr);
3892 newlen = namelen + verlen + 2;
3893 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3894 && isym->st_shndx != SHN_UNDEF)
3897 newname = bfd_hash_allocate (&htab->root.table, newlen);
3898 if (newname == NULL)
3899 goto error_free_vers;
3900 memcpy (newname, name, namelen);
3901 p = newname + namelen;
3903 /* If this is a defined non-hidden version symbol,
3904 we add another @ to the name. This indicates the
3905 default version of the symbol. */
3906 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3907 && isym->st_shndx != SHN_UNDEF)
3909 memcpy (p, verstr, verlen + 1);
3914 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
3915 &value, &old_alignment,
3916 sym_hash, &skip, &override,
3917 &type_change_ok, &size_change_ok))
3918 goto error_free_vers;
3927 while (h->root.type == bfd_link_hash_indirect
3928 || h->root.type == bfd_link_hash_warning)
3929 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3931 /* Remember the old alignment if this is a common symbol, so
3932 that we don't reduce the alignment later on. We can't
3933 check later, because _bfd_generic_link_add_one_symbol
3934 will set a default for the alignment which we want to
3935 override. We also remember the old bfd where the existing
3936 definition comes from. */
3937 switch (h->root.type)
3942 case bfd_link_hash_defined:
3943 case bfd_link_hash_defweak:
3944 old_bfd = h->root.u.def.section->owner;
3947 case bfd_link_hash_common:
3948 old_bfd = h->root.u.c.p->section->owner;
3949 old_alignment = h->root.u.c.p->alignment_power;
3953 if (elf_tdata (abfd)->verdef != NULL
3957 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3960 if (! (_bfd_generic_link_add_one_symbol
3961 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
3962 (struct bfd_link_hash_entry **) sym_hash)))
3963 goto error_free_vers;
3966 while (h->root.type == bfd_link_hash_indirect
3967 || h->root.type == bfd_link_hash_warning)
3968 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3971 new_weakdef = FALSE;
3974 && (flags & BSF_WEAK) != 0
3975 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
3976 && is_elf_hash_table (htab)
3977 && h->u.weakdef == NULL)
3979 /* Keep a list of all weak defined non function symbols from
3980 a dynamic object, using the weakdef field. Later in this
3981 function we will set the weakdef field to the correct
3982 value. We only put non-function symbols from dynamic
3983 objects on this list, because that happens to be the only
3984 time we need to know the normal symbol corresponding to a
3985 weak symbol, and the information is time consuming to
3986 figure out. If the weakdef field is not already NULL,
3987 then this symbol was already defined by some previous
3988 dynamic object, and we will be using that previous
3989 definition anyhow. */
3991 h->u.weakdef = weaks;
3996 /* Set the alignment of a common symbol. */
3997 if ((common || bfd_is_com_section (sec))
3998 && h->root.type == bfd_link_hash_common)
4003 align = bfd_log2 (isym->st_value);
4006 /* The new symbol is a common symbol in a shared object.
4007 We need to get the alignment from the section. */
4008 align = new_sec->alignment_power;
4010 if (align > old_alignment
4011 /* Permit an alignment power of zero if an alignment of one
4012 is specified and no other alignments have been specified. */
4013 || (isym->st_value == 1 && old_alignment == 0))
4014 h->root.u.c.p->alignment_power = align;
4016 h->root.u.c.p->alignment_power = old_alignment;
4019 if (is_elf_hash_table (htab))
4023 /* Check the alignment when a common symbol is involved. This
4024 can change when a common symbol is overridden by a normal
4025 definition or a common symbol is ignored due to the old
4026 normal definition. We need to make sure the maximum
4027 alignment is maintained. */
4028 if ((old_alignment || common)
4029 && h->root.type != bfd_link_hash_common)
4031 unsigned int common_align;
4032 unsigned int normal_align;
4033 unsigned int symbol_align;
4037 symbol_align = ffs (h->root.u.def.value) - 1;
4038 if (h->root.u.def.section->owner != NULL
4039 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4041 normal_align = h->root.u.def.section->alignment_power;
4042 if (normal_align > symbol_align)
4043 normal_align = symbol_align;
4046 normal_align = symbol_align;
4050 common_align = old_alignment;
4051 common_bfd = old_bfd;
4056 common_align = bfd_log2 (isym->st_value);
4058 normal_bfd = old_bfd;
4061 if (normal_align < common_align)
4063 /* PR binutils/2735 */
4064 if (normal_bfd == NULL)
4065 (*_bfd_error_handler)
4066 (_("Warning: alignment %u of common symbol `%s' in %B"
4067 " is greater than the alignment (%u) of its section %A"),
4068 common_bfd, h->root.u.def.section,
4069 1 << common_align, name, 1 << normal_align);
4071 (*_bfd_error_handler)
4072 (_("Warning: alignment %u of symbol `%s' in %B"
4073 " is smaller than %u in %B"),
4074 normal_bfd, common_bfd,
4075 1 << normal_align, name, 1 << common_align);
4079 /* Remember the symbol size if it isn't undefined. */
4080 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4081 && (definition || h->size == 0))
4084 && h->size != isym->st_size
4085 && ! size_change_ok)
4086 (*_bfd_error_handler)
4087 (_("Warning: size of symbol `%s' changed"
4088 " from %lu in %B to %lu in %B"),
4090 name, (unsigned long) h->size,
4091 (unsigned long) isym->st_size);
4093 h->size = isym->st_size;
4096 /* If this is a common symbol, then we always want H->SIZE
4097 to be the size of the common symbol. The code just above
4098 won't fix the size if a common symbol becomes larger. We
4099 don't warn about a size change here, because that is
4100 covered by --warn-common. Allow changed between different
4102 if (h->root.type == bfd_link_hash_common)
4103 h->size = h->root.u.c.size;
4105 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4106 && (definition || h->type == STT_NOTYPE))
4108 if (h->type != STT_NOTYPE
4109 && h->type != ELF_ST_TYPE (isym->st_info)
4110 && ! type_change_ok)
4111 (*_bfd_error_handler)
4112 (_("Warning: type of symbol `%s' changed"
4113 " from %d to %d in %B"),
4114 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
4116 h->type = ELF_ST_TYPE (isym->st_info);
4119 /* If st_other has a processor-specific meaning, specific
4120 code might be needed here. We never merge the visibility
4121 attribute with the one from a dynamic object. */
4122 if (bed->elf_backend_merge_symbol_attribute)
4123 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
4126 /* If this symbol has default visibility and the user has requested
4127 we not re-export it, then mark it as hidden. */
4128 if (definition && !dynamic
4130 || (abfd->my_archive && abfd->my_archive->no_export))
4131 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4132 isym->st_other = (STV_HIDDEN
4133 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4135 if (ELF_ST_VISIBILITY (isym->st_other) != 0 && !dynamic)
4137 unsigned char hvis, symvis, other, nvis;
4139 /* Only merge the visibility. Leave the remainder of the
4140 st_other field to elf_backend_merge_symbol_attribute. */
4141 other = h->other & ~ELF_ST_VISIBILITY (-1);
4143 /* Combine visibilities, using the most constraining one. */
4144 hvis = ELF_ST_VISIBILITY (h->other);
4145 symvis = ELF_ST_VISIBILITY (isym->st_other);
4151 nvis = hvis < symvis ? hvis : symvis;
4153 h->other = other | nvis;
4156 /* Set a flag in the hash table entry indicating the type of
4157 reference or definition we just found. Keep a count of
4158 the number of dynamic symbols we find. A dynamic symbol
4159 is one which is referenced or defined by both a regular
4160 object and a shared object. */
4167 if (bind != STB_WEAK)
4168 h->ref_regular_nonweak = 1;
4172 if (! info->executable
4185 || (h->u.weakdef != NULL
4187 && h->u.weakdef->dynindx != -1))
4191 if (definition && (sec->flags & SEC_DEBUGGING))
4193 /* We don't want to make debug symbol dynamic. */
4194 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4198 /* Check to see if we need to add an indirect symbol for
4199 the default name. */
4200 if (definition || h->root.type == bfd_link_hash_common)
4201 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4202 &sec, &value, &dynsym,
4204 goto error_free_vers;
4206 if (definition && !dynamic)
4208 char *p = strchr (name, ELF_VER_CHR);
4209 if (p != NULL && p[1] != ELF_VER_CHR)
4211 /* Queue non-default versions so that .symver x, x@FOO
4212 aliases can be checked. */
4215 amt = ((isymend - isym + 1)
4216 * sizeof (struct elf_link_hash_entry *));
4217 nondeflt_vers = bfd_malloc (amt);
4219 nondeflt_vers[nondeflt_vers_cnt++] = h;
4223 if (dynsym && h->dynindx == -1)
4225 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4226 goto error_free_vers;
4227 if (h->u.weakdef != NULL
4229 && h->u.weakdef->dynindx == -1)
4231 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4232 goto error_free_vers;
4235 else if (dynsym && h->dynindx != -1)
4236 /* If the symbol already has a dynamic index, but
4237 visibility says it should not be visible, turn it into
4239 switch (ELF_ST_VISIBILITY (h->other))
4243 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4254 const char *soname = elf_dt_name (abfd);
4256 /* A symbol from a library loaded via DT_NEEDED of some
4257 other library is referenced by a regular object.
4258 Add a DT_NEEDED entry for it. Issue an error if
4259 --no-add-needed is used. */
4260 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4262 (*_bfd_error_handler)
4263 (_("%s: invalid DSO for symbol `%s' definition"),
4265 bfd_set_error (bfd_error_bad_value);
4266 goto error_free_vers;
4269 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4272 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4274 goto error_free_vers;
4276 BFD_ASSERT (ret == 0);
4281 if (extversym != NULL)
4287 if (isymbuf != NULL)
4293 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4297 /* Restore the symbol table. */
4298 if (bed->as_needed_cleanup)
4299 (*bed->as_needed_cleanup) (abfd, info);
4300 old_hash = (char *) old_tab + tabsize;
4301 old_ent = (char *) old_hash + hashsize;
4302 sym_hash = elf_sym_hashes (abfd);
4303 htab->root.table.table = old_table;
4304 htab->root.table.size = old_size;
4305 htab->root.table.count = old_count;
4306 memcpy (htab->root.table.table, old_tab, tabsize);
4307 memcpy (sym_hash, old_hash, hashsize);
4308 htab->root.undefs = old_undefs;
4309 htab->root.undefs_tail = old_undefs_tail;
4310 for (i = 0; i < htab->root.table.size; i++)
4312 struct bfd_hash_entry *p;
4313 struct elf_link_hash_entry *h;
4315 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4317 h = (struct elf_link_hash_entry *) p;
4318 if (h->root.type == bfd_link_hash_warning)
4319 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4320 if (h->dynindx >= old_dynsymcount)
4321 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4323 memcpy (p, old_ent, htab->root.table.entsize);
4324 old_ent = (char *) old_ent + htab->root.table.entsize;
4325 h = (struct elf_link_hash_entry *) p;
4326 if (h->root.type == bfd_link_hash_warning)
4328 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4329 old_ent = (char *) old_ent + htab->root.table.entsize;
4334 /* Make a special call to the linker "notice" function to
4335 tell it that symbols added for crefs may need to be removed. */
4336 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4341 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4343 if (nondeflt_vers != NULL)
4344 free (nondeflt_vers);
4348 if (old_tab != NULL)
4350 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4357 /* Now that all the symbols from this input file are created, handle
4358 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4359 if (nondeflt_vers != NULL)
4361 bfd_size_type cnt, symidx;
4363 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4365 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4366 char *shortname, *p;
4368 p = strchr (h->root.root.string, ELF_VER_CHR);
4370 || (h->root.type != bfd_link_hash_defined
4371 && h->root.type != bfd_link_hash_defweak))
4374 amt = p - h->root.root.string;
4375 shortname = bfd_malloc (amt + 1);
4376 memcpy (shortname, h->root.root.string, amt);
4377 shortname[amt] = '\0';
4379 hi = (struct elf_link_hash_entry *)
4380 bfd_link_hash_lookup (&htab->root, shortname,
4381 FALSE, FALSE, FALSE);
4383 && hi->root.type == h->root.type
4384 && hi->root.u.def.value == h->root.u.def.value
4385 && hi->root.u.def.section == h->root.u.def.section)
4387 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4388 hi->root.type = bfd_link_hash_indirect;
4389 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4390 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4391 sym_hash = elf_sym_hashes (abfd);
4393 for (symidx = 0; symidx < extsymcount; ++symidx)
4394 if (sym_hash[symidx] == hi)
4396 sym_hash[symidx] = h;
4402 free (nondeflt_vers);
4403 nondeflt_vers = NULL;
4406 /* Now set the weakdefs field correctly for all the weak defined
4407 symbols we found. The only way to do this is to search all the
4408 symbols. Since we only need the information for non functions in
4409 dynamic objects, that's the only time we actually put anything on
4410 the list WEAKS. We need this information so that if a regular
4411 object refers to a symbol defined weakly in a dynamic object, the
4412 real symbol in the dynamic object is also put in the dynamic
4413 symbols; we also must arrange for both symbols to point to the
4414 same memory location. We could handle the general case of symbol
4415 aliasing, but a general symbol alias can only be generated in
4416 assembler code, handling it correctly would be very time
4417 consuming, and other ELF linkers don't handle general aliasing
4421 struct elf_link_hash_entry **hpp;
4422 struct elf_link_hash_entry **hppend;
4423 struct elf_link_hash_entry **sorted_sym_hash;
4424 struct elf_link_hash_entry *h;
4427 /* Since we have to search the whole symbol list for each weak
4428 defined symbol, search time for N weak defined symbols will be
4429 O(N^2). Binary search will cut it down to O(NlogN). */
4430 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4431 sorted_sym_hash = bfd_malloc (amt);
4432 if (sorted_sym_hash == NULL)
4434 sym_hash = sorted_sym_hash;
4435 hpp = elf_sym_hashes (abfd);
4436 hppend = hpp + extsymcount;
4438 for (; hpp < hppend; hpp++)
4442 && h->root.type == bfd_link_hash_defined
4443 && !bed->is_function_type (h->type))
4451 qsort (sorted_sym_hash, sym_count,
4452 sizeof (struct elf_link_hash_entry *),
4455 while (weaks != NULL)
4457 struct elf_link_hash_entry *hlook;
4464 weaks = hlook->u.weakdef;
4465 hlook->u.weakdef = NULL;
4467 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4468 || hlook->root.type == bfd_link_hash_defweak
4469 || hlook->root.type == bfd_link_hash_common
4470 || hlook->root.type == bfd_link_hash_indirect);
4471 slook = hlook->root.u.def.section;
4472 vlook = hlook->root.u.def.value;
4479 bfd_signed_vma vdiff;
4481 h = sorted_sym_hash [idx];
4482 vdiff = vlook - h->root.u.def.value;
4489 long sdiff = slook->id - h->root.u.def.section->id;
4502 /* We didn't find a value/section match. */
4506 for (i = ilook; i < sym_count; i++)
4508 h = sorted_sym_hash [i];
4510 /* Stop if value or section doesn't match. */
4511 if (h->root.u.def.value != vlook
4512 || h->root.u.def.section != slook)
4514 else if (h != hlook)
4516 hlook->u.weakdef = h;
4518 /* If the weak definition is in the list of dynamic
4519 symbols, make sure the real definition is put
4521 if (hlook->dynindx != -1 && h->dynindx == -1)
4523 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4527 /* If the real definition is in the list of dynamic
4528 symbols, make sure the weak definition is put
4529 there as well. If we don't do this, then the
4530 dynamic loader might not merge the entries for the
4531 real definition and the weak definition. */
4532 if (h->dynindx != -1 && hlook->dynindx == -1)
4534 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4542 free (sorted_sym_hash);
4545 if (bed->check_directives)
4546 (*bed->check_directives) (abfd, info);
4548 /* If this object is the same format as the output object, and it is
4549 not a shared library, then let the backend look through the
4552 This is required to build global offset table entries and to
4553 arrange for dynamic relocs. It is not required for the
4554 particular common case of linking non PIC code, even when linking
4555 against shared libraries, but unfortunately there is no way of
4556 knowing whether an object file has been compiled PIC or not.
4557 Looking through the relocs is not particularly time consuming.
4558 The problem is that we must either (1) keep the relocs in memory,
4559 which causes the linker to require additional runtime memory or
4560 (2) read the relocs twice from the input file, which wastes time.
4561 This would be a good case for using mmap.
4563 I have no idea how to handle linking PIC code into a file of a
4564 different format. It probably can't be done. */
4566 && is_elf_hash_table (htab)
4567 && htab->root.creator == abfd->xvec
4568 && bed->check_relocs != NULL)
4572 for (o = abfd->sections; o != NULL; o = o->next)
4574 Elf_Internal_Rela *internal_relocs;
4577 if ((o->flags & SEC_RELOC) == 0
4578 || o->reloc_count == 0
4579 || ((info->strip == strip_all || info->strip == strip_debugger)
4580 && (o->flags & SEC_DEBUGGING) != 0)
4581 || bfd_is_abs_section (o->output_section))
4584 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4586 if (internal_relocs == NULL)
4589 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4591 if (elf_section_data (o)->relocs != internal_relocs)
4592 free (internal_relocs);
4599 /* If this is a non-traditional link, try to optimize the handling
4600 of the .stab/.stabstr sections. */
4602 && ! info->traditional_format
4603 && is_elf_hash_table (htab)
4604 && (info->strip != strip_all && info->strip != strip_debugger))
4608 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4609 if (stabstr != NULL)
4611 bfd_size_type string_offset = 0;
4614 for (stab = abfd->sections; stab; stab = stab->next)
4615 if (CONST_STRNEQ (stab->name, ".stab")
4616 && (!stab->name[5] ||
4617 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4618 && (stab->flags & SEC_MERGE) == 0
4619 && !bfd_is_abs_section (stab->output_section))
4621 struct bfd_elf_section_data *secdata;
4623 secdata = elf_section_data (stab);
4624 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4625 stabstr, &secdata->sec_info,
4628 if (secdata->sec_info)
4629 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4634 if (is_elf_hash_table (htab) && add_needed)
4636 /* Add this bfd to the loaded list. */
4637 struct elf_link_loaded_list *n;
4639 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4643 n->next = htab->loaded;
4650 if (old_tab != NULL)
4652 if (nondeflt_vers != NULL)
4653 free (nondeflt_vers);
4654 if (extversym != NULL)
4657 if (isymbuf != NULL)
4663 /* Return the linker hash table entry of a symbol that might be
4664 satisfied by an archive symbol. Return -1 on error. */
4666 struct elf_link_hash_entry *
4667 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4668 struct bfd_link_info *info,
4671 struct elf_link_hash_entry *h;
4675 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4679 /* If this is a default version (the name contains @@), look up the
4680 symbol again with only one `@' as well as without the version.
4681 The effect is that references to the symbol with and without the
4682 version will be matched by the default symbol in the archive. */
4684 p = strchr (name, ELF_VER_CHR);
4685 if (p == NULL || p[1] != ELF_VER_CHR)
4688 /* First check with only one `@'. */
4689 len = strlen (name);
4690 copy = bfd_alloc (abfd, len);
4692 return (struct elf_link_hash_entry *) 0 - 1;
4694 first = p - name + 1;
4695 memcpy (copy, name, first);
4696 memcpy (copy + first, name + first + 1, len - first);
4698 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4701 /* We also need to check references to the symbol without the
4703 copy[first - 1] = '\0';
4704 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4705 FALSE, FALSE, FALSE);
4708 bfd_release (abfd, copy);
4712 /* Add symbols from an ELF archive file to the linker hash table. We
4713 don't use _bfd_generic_link_add_archive_symbols because of a
4714 problem which arises on UnixWare. The UnixWare libc.so is an
4715 archive which includes an entry libc.so.1 which defines a bunch of
4716 symbols. The libc.so archive also includes a number of other
4717 object files, which also define symbols, some of which are the same
4718 as those defined in libc.so.1. Correct linking requires that we
4719 consider each object file in turn, and include it if it defines any
4720 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4721 this; it looks through the list of undefined symbols, and includes
4722 any object file which defines them. When this algorithm is used on
4723 UnixWare, it winds up pulling in libc.so.1 early and defining a
4724 bunch of symbols. This means that some of the other objects in the
4725 archive are not included in the link, which is incorrect since they
4726 precede libc.so.1 in the archive.
4728 Fortunately, ELF archive handling is simpler than that done by
4729 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4730 oddities. In ELF, if we find a symbol in the archive map, and the
4731 symbol is currently undefined, we know that we must pull in that
4734 Unfortunately, we do have to make multiple passes over the symbol
4735 table until nothing further is resolved. */
4738 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4741 bfd_boolean *defined = NULL;
4742 bfd_boolean *included = NULL;
4746 const struct elf_backend_data *bed;
4747 struct elf_link_hash_entry * (*archive_symbol_lookup)
4748 (bfd *, struct bfd_link_info *, const char *);
4750 if (! bfd_has_map (abfd))
4752 /* An empty archive is a special case. */
4753 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4755 bfd_set_error (bfd_error_no_armap);
4759 /* Keep track of all symbols we know to be already defined, and all
4760 files we know to be already included. This is to speed up the
4761 second and subsequent passes. */
4762 c = bfd_ardata (abfd)->symdef_count;
4766 amt *= sizeof (bfd_boolean);
4767 defined = bfd_zmalloc (amt);
4768 included = bfd_zmalloc (amt);
4769 if (defined == NULL || included == NULL)
4772 symdefs = bfd_ardata (abfd)->symdefs;
4773 bed = get_elf_backend_data (abfd);
4774 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4787 symdefend = symdef + c;
4788 for (i = 0; symdef < symdefend; symdef++, i++)
4790 struct elf_link_hash_entry *h;
4792 struct bfd_link_hash_entry *undefs_tail;
4795 if (defined[i] || included[i])
4797 if (symdef->file_offset == last)
4803 h = archive_symbol_lookup (abfd, info, symdef->name);
4804 if (h == (struct elf_link_hash_entry *) 0 - 1)
4810 if (h->root.type == bfd_link_hash_common)
4812 /* We currently have a common symbol. The archive map contains
4813 a reference to this symbol, so we may want to include it. We
4814 only want to include it however, if this archive element
4815 contains a definition of the symbol, not just another common
4818 Unfortunately some archivers (including GNU ar) will put
4819 declarations of common symbols into their archive maps, as
4820 well as real definitions, so we cannot just go by the archive
4821 map alone. Instead we must read in the element's symbol
4822 table and check that to see what kind of symbol definition
4824 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4827 else if (h->root.type != bfd_link_hash_undefined)
4829 if (h->root.type != bfd_link_hash_undefweak)
4834 /* We need to include this archive member. */
4835 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4836 if (element == NULL)
4839 if (! bfd_check_format (element, bfd_object))
4842 /* Doublecheck that we have not included this object
4843 already--it should be impossible, but there may be
4844 something wrong with the archive. */
4845 if (element->archive_pass != 0)
4847 bfd_set_error (bfd_error_bad_value);
4850 element->archive_pass = 1;
4852 undefs_tail = info->hash->undefs_tail;
4854 if (! (*info->callbacks->add_archive_element) (info, element,
4857 if (! bfd_link_add_symbols (element, info))
4860 /* If there are any new undefined symbols, we need to make
4861 another pass through the archive in order to see whether
4862 they can be defined. FIXME: This isn't perfect, because
4863 common symbols wind up on undefs_tail and because an
4864 undefined symbol which is defined later on in this pass
4865 does not require another pass. This isn't a bug, but it
4866 does make the code less efficient than it could be. */
4867 if (undefs_tail != info->hash->undefs_tail)
4870 /* Look backward to mark all symbols from this object file
4871 which we have already seen in this pass. */
4875 included[mark] = TRUE;
4880 while (symdefs[mark].file_offset == symdef->file_offset);
4882 /* We mark subsequent symbols from this object file as we go
4883 on through the loop. */
4884 last = symdef->file_offset;
4895 if (defined != NULL)
4897 if (included != NULL)
4902 /* Given an ELF BFD, add symbols to the global hash table as
4906 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4908 switch (bfd_get_format (abfd))
4911 return elf_link_add_object_symbols (abfd, info);
4913 return elf_link_add_archive_symbols (abfd, info);
4915 bfd_set_error (bfd_error_wrong_format);
4920 /* This function will be called though elf_link_hash_traverse to store
4921 all hash value of the exported symbols in an array. */
4924 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4926 unsigned long **valuep = data;
4932 if (h->root.type == bfd_link_hash_warning)
4933 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4935 /* Ignore indirect symbols. These are added by the versioning code. */
4936 if (h->dynindx == -1)
4939 name = h->root.root.string;
4940 p = strchr (name, ELF_VER_CHR);
4943 alc = bfd_malloc (p - name + 1);
4944 memcpy (alc, name, p - name);
4945 alc[p - name] = '\0';
4949 /* Compute the hash value. */
4950 ha = bfd_elf_hash (name);
4952 /* Store the found hash value in the array given as the argument. */
4955 /* And store it in the struct so that we can put it in the hash table
4957 h->u.elf_hash_value = ha;
4965 struct collect_gnu_hash_codes
4968 const struct elf_backend_data *bed;
4969 unsigned long int nsyms;
4970 unsigned long int maskbits;
4971 unsigned long int *hashcodes;
4972 unsigned long int *hashval;
4973 unsigned long int *indx;
4974 unsigned long int *counts;
4977 long int min_dynindx;
4978 unsigned long int bucketcount;
4979 unsigned long int symindx;
4980 long int local_indx;
4981 long int shift1, shift2;
4982 unsigned long int mask;
4985 /* This function will be called though elf_link_hash_traverse to store
4986 all hash value of the exported symbols in an array. */
4989 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
4991 struct collect_gnu_hash_codes *s = data;
4997 if (h->root.type == bfd_link_hash_warning)
4998 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5000 /* Ignore indirect symbols. These are added by the versioning code. */
5001 if (h->dynindx == -1)
5004 /* Ignore also local symbols and undefined symbols. */
5005 if (! (*s->bed->elf_hash_symbol) (h))
5008 name = h->root.root.string;
5009 p = strchr (name, ELF_VER_CHR);
5012 alc = bfd_malloc (p - name + 1);
5013 memcpy (alc, name, p - name);
5014 alc[p - name] = '\0';
5018 /* Compute the hash value. */
5019 ha = bfd_elf_gnu_hash (name);
5021 /* Store the found hash value in the array for compute_bucket_count,
5022 and also for .dynsym reordering purposes. */
5023 s->hashcodes[s->nsyms] = ha;
5024 s->hashval[h->dynindx] = ha;
5026 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5027 s->min_dynindx = h->dynindx;
5035 /* This function will be called though elf_link_hash_traverse to do
5036 final dynaminc symbol renumbering. */
5039 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5041 struct collect_gnu_hash_codes *s = data;
5042 unsigned long int bucket;
5043 unsigned long int val;
5045 if (h->root.type == bfd_link_hash_warning)
5046 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5048 /* Ignore indirect symbols. */
5049 if (h->dynindx == -1)
5052 /* Ignore also local symbols and undefined symbols. */
5053 if (! (*s->bed->elf_hash_symbol) (h))
5055 if (h->dynindx >= s->min_dynindx)
5056 h->dynindx = s->local_indx++;
5060 bucket = s->hashval[h->dynindx] % s->bucketcount;
5061 val = (s->hashval[h->dynindx] >> s->shift1)
5062 & ((s->maskbits >> s->shift1) - 1);
5063 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5065 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5066 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5067 if (s->counts[bucket] == 1)
5068 /* Last element terminates the chain. */
5070 bfd_put_32 (s->output_bfd, val,
5071 s->contents + (s->indx[bucket] - s->symindx) * 4);
5072 --s->counts[bucket];
5073 h->dynindx = s->indx[bucket]++;
5077 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5080 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5082 return !(h->forced_local
5083 || h->root.type == bfd_link_hash_undefined
5084 || h->root.type == bfd_link_hash_undefweak
5085 || ((h->root.type == bfd_link_hash_defined
5086 || h->root.type == bfd_link_hash_defweak)
5087 && h->root.u.def.section->output_section == NULL));
5090 /* Array used to determine the number of hash table buckets to use
5091 based on the number of symbols there are. If there are fewer than
5092 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5093 fewer than 37 we use 17 buckets, and so forth. We never use more
5094 than 32771 buckets. */
5096 static const size_t elf_buckets[] =
5098 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5102 /* Compute bucket count for hashing table. We do not use a static set
5103 of possible tables sizes anymore. Instead we determine for all
5104 possible reasonable sizes of the table the outcome (i.e., the
5105 number of collisions etc) and choose the best solution. The
5106 weighting functions are not too simple to allow the table to grow
5107 without bounds. Instead one of the weighting factors is the size.
5108 Therefore the result is always a good payoff between few collisions
5109 (= short chain lengths) and table size. */
5111 compute_bucket_count (struct bfd_link_info *info, unsigned long int *hashcodes,
5112 unsigned long int nsyms, int gnu_hash)
5114 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5115 size_t best_size = 0;
5116 unsigned long int i;
5119 /* We have a problem here. The following code to optimize the table
5120 size requires an integer type with more the 32 bits. If
5121 BFD_HOST_U_64_BIT is set we know about such a type. */
5122 #ifdef BFD_HOST_U_64_BIT
5127 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5128 bfd *dynobj = elf_hash_table (info)->dynobj;
5129 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5130 unsigned long int *counts;
5132 /* Possible optimization parameters: if we have NSYMS symbols we say
5133 that the hashing table must at least have NSYMS/4 and at most
5135 minsize = nsyms / 4;
5138 best_size = maxsize = nsyms * 2;
5143 if ((best_size & 31) == 0)
5147 /* Create array where we count the collisions in. We must use bfd_malloc
5148 since the size could be large. */
5150 amt *= sizeof (unsigned long int);
5151 counts = bfd_malloc (amt);
5155 /* Compute the "optimal" size for the hash table. The criteria is a
5156 minimal chain length. The minor criteria is (of course) the size
5158 for (i = minsize; i < maxsize; ++i)
5160 /* Walk through the array of hashcodes and count the collisions. */
5161 BFD_HOST_U_64_BIT max;
5162 unsigned long int j;
5163 unsigned long int fact;
5165 if (gnu_hash && (i & 31) == 0)
5168 memset (counts, '\0', i * sizeof (unsigned long int));
5170 /* Determine how often each hash bucket is used. */
5171 for (j = 0; j < nsyms; ++j)
5172 ++counts[hashcodes[j] % i];
5174 /* For the weight function we need some information about the
5175 pagesize on the target. This is information need not be 100%
5176 accurate. Since this information is not available (so far) we
5177 define it here to a reasonable default value. If it is crucial
5178 to have a better value some day simply define this value. */
5179 # ifndef BFD_TARGET_PAGESIZE
5180 # define BFD_TARGET_PAGESIZE (4096)
5183 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5185 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5188 /* Variant 1: optimize for short chains. We add the squares
5189 of all the chain lengths (which favors many small chain
5190 over a few long chains). */
5191 for (j = 0; j < i; ++j)
5192 max += counts[j] * counts[j];
5194 /* This adds penalties for the overall size of the table. */
5195 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5198 /* Variant 2: Optimize a lot more for small table. Here we
5199 also add squares of the size but we also add penalties for
5200 empty slots (the +1 term). */
5201 for (j = 0; j < i; ++j)
5202 max += (1 + counts[j]) * (1 + counts[j]);
5204 /* The overall size of the table is considered, but not as
5205 strong as in variant 1, where it is squared. */
5206 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5210 /* Compare with current best results. */
5211 if (max < best_chlen)
5221 #endif /* defined (BFD_HOST_U_64_BIT) */
5223 /* This is the fallback solution if no 64bit type is available or if we
5224 are not supposed to spend much time on optimizations. We select the
5225 bucket count using a fixed set of numbers. */
5226 for (i = 0; elf_buckets[i] != 0; i++)
5228 best_size = elf_buckets[i];
5229 if (nsyms < elf_buckets[i + 1])
5232 if (gnu_hash && best_size < 2)
5239 /* Set up the sizes and contents of the ELF dynamic sections. This is
5240 called by the ELF linker emulation before_allocation routine. We
5241 must set the sizes of the sections before the linker sets the
5242 addresses of the various sections. */
5245 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5248 const char *filter_shlib,
5249 const char * const *auxiliary_filters,
5250 struct bfd_link_info *info,
5251 asection **sinterpptr,
5252 struct bfd_elf_version_tree *verdefs)
5254 bfd_size_type soname_indx;
5256 const struct elf_backend_data *bed;
5257 struct elf_assign_sym_version_info asvinfo;
5261 soname_indx = (bfd_size_type) -1;
5263 if (!is_elf_hash_table (info->hash))
5266 bed = get_elf_backend_data (output_bfd);
5267 elf_tdata (output_bfd)->relro = info->relro;
5268 if (info->execstack)
5269 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5270 else if (info->noexecstack)
5271 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5275 asection *notesec = NULL;
5278 for (inputobj = info->input_bfds;
5280 inputobj = inputobj->link_next)
5284 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
5286 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5289 if (s->flags & SEC_CODE)
5293 else if (bed->default_execstack)
5298 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5299 if (exec && info->relocatable
5300 && notesec->output_section != bfd_abs_section_ptr)
5301 notesec->output_section->flags |= SEC_CODE;
5305 /* Any syms created from now on start with -1 in
5306 got.refcount/offset and plt.refcount/offset. */
5307 elf_hash_table (info)->init_got_refcount
5308 = elf_hash_table (info)->init_got_offset;
5309 elf_hash_table (info)->init_plt_refcount
5310 = elf_hash_table (info)->init_plt_offset;
5312 /* The backend may have to create some sections regardless of whether
5313 we're dynamic or not. */
5314 if (bed->elf_backend_always_size_sections
5315 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5318 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5321 dynobj = elf_hash_table (info)->dynobj;
5323 /* If there were no dynamic objects in the link, there is nothing to
5328 if (elf_hash_table (info)->dynamic_sections_created)
5330 struct elf_info_failed eif;
5331 struct elf_link_hash_entry *h;
5333 struct bfd_elf_version_tree *t;
5334 struct bfd_elf_version_expr *d;
5336 bfd_boolean all_defined;
5338 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5339 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5343 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5345 if (soname_indx == (bfd_size_type) -1
5346 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5352 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5354 info->flags |= DF_SYMBOLIC;
5361 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5363 if (indx == (bfd_size_type) -1
5364 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5367 if (info->new_dtags)
5369 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5370 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5375 if (filter_shlib != NULL)
5379 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5380 filter_shlib, TRUE);
5381 if (indx == (bfd_size_type) -1
5382 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5386 if (auxiliary_filters != NULL)
5388 const char * const *p;
5390 for (p = auxiliary_filters; *p != NULL; p++)
5394 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5396 if (indx == (bfd_size_type) -1
5397 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5403 eif.verdefs = verdefs;
5406 /* If we are supposed to export all symbols into the dynamic symbol
5407 table (this is not the normal case), then do so. */
5408 if (info->export_dynamic
5409 || (info->executable && info->dynamic))
5411 elf_link_hash_traverse (elf_hash_table (info),
5412 _bfd_elf_export_symbol,
5418 /* Make all global versions with definition. */
5419 for (t = verdefs; t != NULL; t = t->next)
5420 for (d = t->globals.list; d != NULL; d = d->next)
5421 if (!d->symver && d->symbol)
5423 const char *verstr, *name;
5424 size_t namelen, verlen, newlen;
5426 struct elf_link_hash_entry *newh;
5429 namelen = strlen (name);
5431 verlen = strlen (verstr);
5432 newlen = namelen + verlen + 3;
5434 newname = bfd_malloc (newlen);
5435 if (newname == NULL)
5437 memcpy (newname, name, namelen);
5439 /* Check the hidden versioned definition. */
5440 p = newname + namelen;
5442 memcpy (p, verstr, verlen + 1);
5443 newh = elf_link_hash_lookup (elf_hash_table (info),
5444 newname, FALSE, FALSE,
5447 || (newh->root.type != bfd_link_hash_defined
5448 && newh->root.type != bfd_link_hash_defweak))
5450 /* Check the default versioned definition. */
5452 memcpy (p, verstr, verlen + 1);
5453 newh = elf_link_hash_lookup (elf_hash_table (info),
5454 newname, FALSE, FALSE,
5459 /* Mark this version if there is a definition and it is
5460 not defined in a shared object. */
5462 && !newh->def_dynamic
5463 && (newh->root.type == bfd_link_hash_defined
5464 || newh->root.type == bfd_link_hash_defweak))
5468 /* Attach all the symbols to their version information. */
5469 asvinfo.output_bfd = output_bfd;
5470 asvinfo.info = info;
5471 asvinfo.verdefs = verdefs;
5472 asvinfo.failed = FALSE;
5474 elf_link_hash_traverse (elf_hash_table (info),
5475 _bfd_elf_link_assign_sym_version,
5480 if (!info->allow_undefined_version)
5482 /* Check if all global versions have a definition. */
5484 for (t = verdefs; t != NULL; t = t->next)
5485 for (d = t->globals.list; d != NULL; d = d->next)
5486 if (!d->symver && !d->script)
5488 (*_bfd_error_handler)
5489 (_("%s: undefined version: %s"),
5490 d->pattern, t->name);
5491 all_defined = FALSE;
5496 bfd_set_error (bfd_error_bad_value);
5501 /* Find all symbols which were defined in a dynamic object and make
5502 the backend pick a reasonable value for them. */
5503 elf_link_hash_traverse (elf_hash_table (info),
5504 _bfd_elf_adjust_dynamic_symbol,
5509 /* Add some entries to the .dynamic section. We fill in some of the
5510 values later, in bfd_elf_final_link, but we must add the entries
5511 now so that we know the final size of the .dynamic section. */
5513 /* If there are initialization and/or finalization functions to
5514 call then add the corresponding DT_INIT/DT_FINI entries. */
5515 h = (info->init_function
5516 ? elf_link_hash_lookup (elf_hash_table (info),
5517 info->init_function, FALSE,
5524 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5527 h = (info->fini_function
5528 ? elf_link_hash_lookup (elf_hash_table (info),
5529 info->fini_function, FALSE,
5536 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5540 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5541 if (s != NULL && s->linker_has_input)
5543 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5544 if (! info->executable)
5549 for (sub = info->input_bfds; sub != NULL;
5550 sub = sub->link_next)
5551 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5552 for (o = sub->sections; o != NULL; o = o->next)
5553 if (elf_section_data (o)->this_hdr.sh_type
5554 == SHT_PREINIT_ARRAY)
5556 (*_bfd_error_handler)
5557 (_("%B: .preinit_array section is not allowed in DSO"),
5562 bfd_set_error (bfd_error_nonrepresentable_section);
5566 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5567 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5570 s = bfd_get_section_by_name (output_bfd, ".init_array");
5571 if (s != NULL && s->linker_has_input)
5573 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5574 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5577 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5578 if (s != NULL && s->linker_has_input)
5580 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5581 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5585 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5586 /* If .dynstr is excluded from the link, we don't want any of
5587 these tags. Strictly, we should be checking each section
5588 individually; This quick check covers for the case where
5589 someone does a /DISCARD/ : { *(*) }. */
5590 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5592 bfd_size_type strsize;
5594 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5595 if ((info->emit_hash
5596 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5597 || (info->emit_gnu_hash
5598 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5599 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5600 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5601 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5602 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5603 bed->s->sizeof_sym))
5608 /* The backend must work out the sizes of all the other dynamic
5610 if (bed->elf_backend_size_dynamic_sections
5611 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5614 if (elf_hash_table (info)->dynamic_sections_created)
5616 unsigned long section_sym_count;
5619 /* Set up the version definition section. */
5620 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5621 BFD_ASSERT (s != NULL);
5623 /* We may have created additional version definitions if we are
5624 just linking a regular application. */
5625 verdefs = asvinfo.verdefs;
5627 /* Skip anonymous version tag. */
5628 if (verdefs != NULL && verdefs->vernum == 0)
5629 verdefs = verdefs->next;
5631 if (verdefs == NULL && !info->create_default_symver)
5632 s->flags |= SEC_EXCLUDE;
5637 struct bfd_elf_version_tree *t;
5639 Elf_Internal_Verdef def;
5640 Elf_Internal_Verdaux defaux;
5641 struct bfd_link_hash_entry *bh;
5642 struct elf_link_hash_entry *h;
5648 /* Make space for the base version. */
5649 size += sizeof (Elf_External_Verdef);
5650 size += sizeof (Elf_External_Verdaux);
5653 /* Make space for the default version. */
5654 if (info->create_default_symver)
5656 size += sizeof (Elf_External_Verdef);
5660 for (t = verdefs; t != NULL; t = t->next)
5662 struct bfd_elf_version_deps *n;
5664 size += sizeof (Elf_External_Verdef);
5665 size += sizeof (Elf_External_Verdaux);
5668 for (n = t->deps; n != NULL; n = n->next)
5669 size += sizeof (Elf_External_Verdaux);
5673 s->contents = bfd_alloc (output_bfd, s->size);
5674 if (s->contents == NULL && s->size != 0)
5677 /* Fill in the version definition section. */
5681 def.vd_version = VER_DEF_CURRENT;
5682 def.vd_flags = VER_FLG_BASE;
5685 if (info->create_default_symver)
5687 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5688 def.vd_next = sizeof (Elf_External_Verdef);
5692 def.vd_aux = sizeof (Elf_External_Verdef);
5693 def.vd_next = (sizeof (Elf_External_Verdef)
5694 + sizeof (Elf_External_Verdaux));
5697 if (soname_indx != (bfd_size_type) -1)
5699 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5701 def.vd_hash = bfd_elf_hash (soname);
5702 defaux.vda_name = soname_indx;
5709 name = lbasename (output_bfd->filename);
5710 def.vd_hash = bfd_elf_hash (name);
5711 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5713 if (indx == (bfd_size_type) -1)
5715 defaux.vda_name = indx;
5717 defaux.vda_next = 0;
5719 _bfd_elf_swap_verdef_out (output_bfd, &def,
5720 (Elf_External_Verdef *) p);
5721 p += sizeof (Elf_External_Verdef);
5722 if (info->create_default_symver)
5724 /* Add a symbol representing this version. */
5726 if (! (_bfd_generic_link_add_one_symbol
5727 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5729 get_elf_backend_data (dynobj)->collect, &bh)))
5731 h = (struct elf_link_hash_entry *) bh;
5734 h->type = STT_OBJECT;
5735 h->verinfo.vertree = NULL;
5737 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5740 /* Create a duplicate of the base version with the same
5741 aux block, but different flags. */
5744 def.vd_aux = sizeof (Elf_External_Verdef);
5746 def.vd_next = (sizeof (Elf_External_Verdef)
5747 + sizeof (Elf_External_Verdaux));
5750 _bfd_elf_swap_verdef_out (output_bfd, &def,
5751 (Elf_External_Verdef *) p);
5752 p += sizeof (Elf_External_Verdef);
5754 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5755 (Elf_External_Verdaux *) p);
5756 p += sizeof (Elf_External_Verdaux);
5758 for (t = verdefs; t != NULL; t = t->next)
5761 struct bfd_elf_version_deps *n;
5764 for (n = t->deps; n != NULL; n = n->next)
5767 /* Add a symbol representing this version. */
5769 if (! (_bfd_generic_link_add_one_symbol
5770 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5772 get_elf_backend_data (dynobj)->collect, &bh)))
5774 h = (struct elf_link_hash_entry *) bh;
5777 h->type = STT_OBJECT;
5778 h->verinfo.vertree = t;
5780 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5783 def.vd_version = VER_DEF_CURRENT;
5785 if (t->globals.list == NULL
5786 && t->locals.list == NULL
5788 def.vd_flags |= VER_FLG_WEAK;
5789 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5790 def.vd_cnt = cdeps + 1;
5791 def.vd_hash = bfd_elf_hash (t->name);
5792 def.vd_aux = sizeof (Elf_External_Verdef);
5794 if (t->next != NULL)
5795 def.vd_next = (sizeof (Elf_External_Verdef)
5796 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5798 _bfd_elf_swap_verdef_out (output_bfd, &def,
5799 (Elf_External_Verdef *) p);
5800 p += sizeof (Elf_External_Verdef);
5802 defaux.vda_name = h->dynstr_index;
5803 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5805 defaux.vda_next = 0;
5806 if (t->deps != NULL)
5807 defaux.vda_next = sizeof (Elf_External_Verdaux);
5808 t->name_indx = defaux.vda_name;
5810 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5811 (Elf_External_Verdaux *) p);
5812 p += sizeof (Elf_External_Verdaux);
5814 for (n = t->deps; n != NULL; n = n->next)
5816 if (n->version_needed == NULL)
5818 /* This can happen if there was an error in the
5820 defaux.vda_name = 0;
5824 defaux.vda_name = n->version_needed->name_indx;
5825 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5828 if (n->next == NULL)
5829 defaux.vda_next = 0;
5831 defaux.vda_next = sizeof (Elf_External_Verdaux);
5833 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5834 (Elf_External_Verdaux *) p);
5835 p += sizeof (Elf_External_Verdaux);
5839 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5840 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5843 elf_tdata (output_bfd)->cverdefs = cdefs;
5846 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5848 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5851 else if (info->flags & DF_BIND_NOW)
5853 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5859 if (info->executable)
5860 info->flags_1 &= ~ (DF_1_INITFIRST
5863 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5867 /* Work out the size of the version reference section. */
5869 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5870 BFD_ASSERT (s != NULL);
5872 struct elf_find_verdep_info sinfo;
5874 sinfo.output_bfd = output_bfd;
5876 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5877 if (sinfo.vers == 0)
5879 sinfo.failed = FALSE;
5881 elf_link_hash_traverse (elf_hash_table (info),
5882 _bfd_elf_link_find_version_dependencies,
5885 if (elf_tdata (output_bfd)->verref == NULL)
5886 s->flags |= SEC_EXCLUDE;
5889 Elf_Internal_Verneed *t;
5894 /* Build the version definition section. */
5897 for (t = elf_tdata (output_bfd)->verref;
5901 Elf_Internal_Vernaux *a;
5903 size += sizeof (Elf_External_Verneed);
5905 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5906 size += sizeof (Elf_External_Vernaux);
5910 s->contents = bfd_alloc (output_bfd, s->size);
5911 if (s->contents == NULL)
5915 for (t = elf_tdata (output_bfd)->verref;
5920 Elf_Internal_Vernaux *a;
5924 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5927 t->vn_version = VER_NEED_CURRENT;
5929 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5930 elf_dt_name (t->vn_bfd) != NULL
5931 ? elf_dt_name (t->vn_bfd)
5932 : lbasename (t->vn_bfd->filename),
5934 if (indx == (bfd_size_type) -1)
5937 t->vn_aux = sizeof (Elf_External_Verneed);
5938 if (t->vn_nextref == NULL)
5941 t->vn_next = (sizeof (Elf_External_Verneed)
5942 + caux * sizeof (Elf_External_Vernaux));
5944 _bfd_elf_swap_verneed_out (output_bfd, t,
5945 (Elf_External_Verneed *) p);
5946 p += sizeof (Elf_External_Verneed);
5948 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5950 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5951 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5952 a->vna_nodename, FALSE);
5953 if (indx == (bfd_size_type) -1)
5956 if (a->vna_nextptr == NULL)
5959 a->vna_next = sizeof (Elf_External_Vernaux);
5961 _bfd_elf_swap_vernaux_out (output_bfd, a,
5962 (Elf_External_Vernaux *) p);
5963 p += sizeof (Elf_External_Vernaux);
5967 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5968 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5971 elf_tdata (output_bfd)->cverrefs = crefs;
5975 if ((elf_tdata (output_bfd)->cverrefs == 0
5976 && elf_tdata (output_bfd)->cverdefs == 0)
5977 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5978 §ion_sym_count) == 0)
5980 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5981 s->flags |= SEC_EXCLUDE;
5987 /* Find the first non-excluded output section. We'll use its
5988 section symbol for some emitted relocs. */
5990 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
5994 for (s = output_bfd->sections; s != NULL; s = s->next)
5995 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
5996 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
5998 elf_hash_table (info)->text_index_section = s;
6003 /* Find two non-excluded output sections, one for code, one for data.
6004 We'll use their section symbols for some emitted relocs. */
6006 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6010 for (s = output_bfd->sections; s != NULL; s = s->next)
6011 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6012 == (SEC_ALLOC | SEC_READONLY))
6013 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6015 elf_hash_table (info)->text_index_section = s;
6019 for (s = output_bfd->sections; s != NULL; s = s->next)
6020 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6021 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6023 elf_hash_table (info)->data_index_section = s;
6027 if (elf_hash_table (info)->text_index_section == NULL)
6028 elf_hash_table (info)->text_index_section
6029 = elf_hash_table (info)->data_index_section;
6033 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6035 const struct elf_backend_data *bed;
6037 if (!is_elf_hash_table (info->hash))
6040 bed = get_elf_backend_data (output_bfd);
6041 (*bed->elf_backend_init_index_section) (output_bfd, info);
6043 if (elf_hash_table (info)->dynamic_sections_created)
6047 bfd_size_type dynsymcount;
6048 unsigned long section_sym_count;
6049 unsigned int dtagcount;
6051 dynobj = elf_hash_table (info)->dynobj;
6053 /* Assign dynsym indicies. In a shared library we generate a
6054 section symbol for each output section, which come first.
6055 Next come all of the back-end allocated local dynamic syms,
6056 followed by the rest of the global symbols. */
6058 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6059 §ion_sym_count);
6061 /* Work out the size of the symbol version section. */
6062 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6063 BFD_ASSERT (s != NULL);
6064 if (dynsymcount != 0
6065 && (s->flags & SEC_EXCLUDE) == 0)
6067 s->size = dynsymcount * sizeof (Elf_External_Versym);
6068 s->contents = bfd_zalloc (output_bfd, s->size);
6069 if (s->contents == NULL)
6072 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6076 /* Set the size of the .dynsym and .hash sections. We counted
6077 the number of dynamic symbols in elf_link_add_object_symbols.
6078 We will build the contents of .dynsym and .hash when we build
6079 the final symbol table, because until then we do not know the
6080 correct value to give the symbols. We built the .dynstr
6081 section as we went along in elf_link_add_object_symbols. */
6082 s = bfd_get_section_by_name (dynobj, ".dynsym");
6083 BFD_ASSERT (s != NULL);
6084 s->size = dynsymcount * bed->s->sizeof_sym;
6086 if (dynsymcount != 0)
6088 s->contents = bfd_alloc (output_bfd, s->size);
6089 if (s->contents == NULL)
6092 /* The first entry in .dynsym is a dummy symbol.
6093 Clear all the section syms, in case we don't output them all. */
6094 ++section_sym_count;
6095 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6098 elf_hash_table (info)->bucketcount = 0;
6100 /* Compute the size of the hashing table. As a side effect this
6101 computes the hash values for all the names we export. */
6102 if (info->emit_hash)
6104 unsigned long int *hashcodes;
6105 unsigned long int *hashcodesp;
6107 unsigned long int nsyms;
6109 size_t hash_entry_size;
6111 /* Compute the hash values for all exported symbols. At the same
6112 time store the values in an array so that we could use them for
6114 amt = dynsymcount * sizeof (unsigned long int);
6115 hashcodes = bfd_malloc (amt);
6116 if (hashcodes == NULL)
6118 hashcodesp = hashcodes;
6120 /* Put all hash values in HASHCODES. */
6121 elf_link_hash_traverse (elf_hash_table (info),
6122 elf_collect_hash_codes, &hashcodesp);
6124 nsyms = hashcodesp - hashcodes;
6126 = compute_bucket_count (info, hashcodes, nsyms, 0);
6129 if (bucketcount == 0)
6132 elf_hash_table (info)->bucketcount = bucketcount;
6134 s = bfd_get_section_by_name (dynobj, ".hash");
6135 BFD_ASSERT (s != NULL);
6136 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6137 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6138 s->contents = bfd_zalloc (output_bfd, s->size);
6139 if (s->contents == NULL)
6142 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6143 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6144 s->contents + hash_entry_size);
6147 if (info->emit_gnu_hash)
6150 unsigned char *contents;
6151 struct collect_gnu_hash_codes cinfo;
6155 memset (&cinfo, 0, sizeof (cinfo));
6157 /* Compute the hash values for all exported symbols. At the same
6158 time store the values in an array so that we could use them for
6160 amt = dynsymcount * 2 * sizeof (unsigned long int);
6161 cinfo.hashcodes = bfd_malloc (amt);
6162 if (cinfo.hashcodes == NULL)
6165 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6166 cinfo.min_dynindx = -1;
6167 cinfo.output_bfd = output_bfd;
6170 /* Put all hash values in HASHCODES. */
6171 elf_link_hash_traverse (elf_hash_table (info),
6172 elf_collect_gnu_hash_codes, &cinfo);
6175 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6177 if (bucketcount == 0)
6179 free (cinfo.hashcodes);
6183 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6184 BFD_ASSERT (s != NULL);
6186 if (cinfo.nsyms == 0)
6188 /* Empty .gnu.hash section is special. */
6189 BFD_ASSERT (cinfo.min_dynindx == -1);
6190 free (cinfo.hashcodes);
6191 s->size = 5 * 4 + bed->s->arch_size / 8;
6192 contents = bfd_zalloc (output_bfd, s->size);
6193 if (contents == NULL)
6195 s->contents = contents;
6196 /* 1 empty bucket. */
6197 bfd_put_32 (output_bfd, 1, contents);
6198 /* SYMIDX above the special symbol 0. */
6199 bfd_put_32 (output_bfd, 1, contents + 4);
6200 /* Just one word for bitmask. */
6201 bfd_put_32 (output_bfd, 1, contents + 8);
6202 /* Only hash fn bloom filter. */
6203 bfd_put_32 (output_bfd, 0, contents + 12);
6204 /* No hashes are valid - empty bitmask. */
6205 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6206 /* No hashes in the only bucket. */
6207 bfd_put_32 (output_bfd, 0,
6208 contents + 16 + bed->s->arch_size / 8);
6212 unsigned long int maskwords, maskbitslog2;
6213 BFD_ASSERT (cinfo.min_dynindx != -1);
6215 maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1;
6216 if (maskbitslog2 < 3)
6218 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6219 maskbitslog2 = maskbitslog2 + 3;
6221 maskbitslog2 = maskbitslog2 + 2;
6222 if (bed->s->arch_size == 64)
6224 if (maskbitslog2 == 5)
6230 cinfo.mask = (1 << cinfo.shift1) - 1;
6231 cinfo.shift2 = maskbitslog2;
6232 cinfo.maskbits = 1 << maskbitslog2;
6233 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6234 amt = bucketcount * sizeof (unsigned long int) * 2;
6235 amt += maskwords * sizeof (bfd_vma);
6236 cinfo.bitmask = bfd_malloc (amt);
6237 if (cinfo.bitmask == NULL)
6239 free (cinfo.hashcodes);
6243 cinfo.counts = (void *) (cinfo.bitmask + maskwords);
6244 cinfo.indx = cinfo.counts + bucketcount;
6245 cinfo.symindx = dynsymcount - cinfo.nsyms;
6246 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6248 /* Determine how often each hash bucket is used. */
6249 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6250 for (i = 0; i < cinfo.nsyms; ++i)
6251 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6253 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6254 if (cinfo.counts[i] != 0)
6256 cinfo.indx[i] = cnt;
6257 cnt += cinfo.counts[i];
6259 BFD_ASSERT (cnt == dynsymcount);
6260 cinfo.bucketcount = bucketcount;
6261 cinfo.local_indx = cinfo.min_dynindx;
6263 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6264 s->size += cinfo.maskbits / 8;
6265 contents = bfd_zalloc (output_bfd, s->size);
6266 if (contents == NULL)
6268 free (cinfo.bitmask);
6269 free (cinfo.hashcodes);
6273 s->contents = contents;
6274 bfd_put_32 (output_bfd, bucketcount, contents);
6275 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6276 bfd_put_32 (output_bfd, maskwords, contents + 8);
6277 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6278 contents += 16 + cinfo.maskbits / 8;
6280 for (i = 0; i < bucketcount; ++i)
6282 if (cinfo.counts[i] == 0)
6283 bfd_put_32 (output_bfd, 0, contents);
6285 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6289 cinfo.contents = contents;
6291 /* Renumber dynamic symbols, populate .gnu.hash section. */
6292 elf_link_hash_traverse (elf_hash_table (info),
6293 elf_renumber_gnu_hash_syms, &cinfo);
6295 contents = s->contents + 16;
6296 for (i = 0; i < maskwords; ++i)
6298 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6300 contents += bed->s->arch_size / 8;
6303 free (cinfo.bitmask);
6304 free (cinfo.hashcodes);
6308 s = bfd_get_section_by_name (dynobj, ".dynstr");
6309 BFD_ASSERT (s != NULL);
6311 elf_finalize_dynstr (output_bfd, info);
6313 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6315 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6316 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6323 /* Final phase of ELF linker. */
6325 /* A structure we use to avoid passing large numbers of arguments. */
6327 struct elf_final_link_info
6329 /* General link information. */
6330 struct bfd_link_info *info;
6333 /* Symbol string table. */
6334 struct bfd_strtab_hash *symstrtab;
6335 /* .dynsym section. */
6336 asection *dynsym_sec;
6337 /* .hash section. */
6339 /* symbol version section (.gnu.version). */
6340 asection *symver_sec;
6341 /* Buffer large enough to hold contents of any section. */
6343 /* Buffer large enough to hold external relocs of any section. */
6344 void *external_relocs;
6345 /* Buffer large enough to hold internal relocs of any section. */
6346 Elf_Internal_Rela *internal_relocs;
6347 /* Buffer large enough to hold external local symbols of any input
6349 bfd_byte *external_syms;
6350 /* And a buffer for symbol section indices. */
6351 Elf_External_Sym_Shndx *locsym_shndx;
6352 /* Buffer large enough to hold internal local symbols of any input
6354 Elf_Internal_Sym *internal_syms;
6355 /* Array large enough to hold a symbol index for each local symbol
6356 of any input BFD. */
6358 /* Array large enough to hold a section pointer for each local
6359 symbol of any input BFD. */
6360 asection **sections;
6361 /* Buffer to hold swapped out symbols. */
6363 /* And one for symbol section indices. */
6364 Elf_External_Sym_Shndx *symshndxbuf;
6365 /* Number of swapped out symbols in buffer. */
6366 size_t symbuf_count;
6367 /* Number of symbols which fit in symbuf. */
6369 /* And same for symshndxbuf. */
6370 size_t shndxbuf_size;
6373 /* This struct is used to pass information to elf_link_output_extsym. */
6375 struct elf_outext_info
6378 bfd_boolean localsyms;
6379 struct elf_final_link_info *finfo;
6383 /* Support for evaluating a complex relocation.
6385 Complex relocations are generalized, self-describing relocations. The
6386 implementation of them consists of two parts: complex symbols, and the
6387 relocations themselves.
6389 The relocations are use a reserved elf-wide relocation type code (R_RELC
6390 external / BFD_RELOC_RELC internal) and an encoding of relocation field
6391 information (start bit, end bit, word width, etc) into the addend. This
6392 information is extracted from CGEN-generated operand tables within gas.
6394 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
6395 internal) representing prefix-notation expressions, including but not
6396 limited to those sorts of expressions normally encoded as addends in the
6397 addend field. The symbol mangling format is:
6400 | <unary-operator> ':' <node>
6401 | <binary-operator> ':' <node> ':' <node>
6404 <literal> := 's' <digits=N> ':' <N character symbol name>
6405 | 'S' <digits=N> ':' <N character section name>
6409 <binary-operator> := as in C
6410 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
6413 set_symbol_value (bfd * bfd_with_globals,
6414 struct elf_final_link_info * finfo,
6418 bfd_boolean is_local;
6419 Elf_Internal_Sym * sym;
6420 struct elf_link_hash_entry ** sym_hashes;
6421 struct elf_link_hash_entry * h;
6423 sym_hashes = elf_sym_hashes (bfd_with_globals);
6424 sym = finfo->internal_syms + symidx;
6425 is_local = ELF_ST_BIND(sym->st_info) == STB_LOCAL;
6429 /* It is a local symbol: move it to the
6430 "absolute" section and give it a value. */
6431 sym->st_shndx = SHN_ABS;
6432 sym->st_value = val;
6436 /* It is a global symbol: set its link type
6437 to "defined" and give it a value. */
6438 h = sym_hashes [symidx];
6439 while (h->root.type == bfd_link_hash_indirect
6440 || h->root.type == bfd_link_hash_warning)
6441 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6442 h->root.type = bfd_link_hash_defined;
6443 h->root.u.def.value = val;
6444 h->root.u.def.section = bfd_abs_section_ptr;
6449 resolve_symbol (const char * name,
6451 struct elf_final_link_info * finfo,
6455 Elf_Internal_Sym * sym;
6456 struct bfd_link_hash_entry * global_entry;
6457 const char * candidate = NULL;
6458 Elf_Internal_Shdr * symtab_hdr;
6459 asection * sec = NULL;
6462 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
6464 for (i = 0; i < locsymcount; ++ i)
6466 sym = finfo->internal_syms + i;
6467 sec = finfo->sections [i];
6469 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
6472 candidate = bfd_elf_string_from_elf_section (input_bfd,
6473 symtab_hdr->sh_link,
6476 printf ("Comparing string: '%s' vs. '%s' = 0x%x\n",
6477 name, candidate, (unsigned int)sym->st_value);
6479 if (candidate && strcmp (candidate, name) == 0)
6481 * result = sym->st_value;
6483 if (sym->st_shndx > SHN_UNDEF &&
6484 sym->st_shndx < SHN_LORESERVE)
6487 printf ("adjusting for sec '%s' @ 0x%x + 0x%x\n",
6488 sec->output_section->name,
6489 (unsigned int)sec->output_section->vma,
6490 (unsigned int)sec->output_offset);
6492 * result += sec->output_offset + sec->output_section->vma;
6495 printf ("Found symbol with effective value %8.8x\n", (unsigned int)* result);
6501 /* Hmm, haven't found it yet. perhaps it is a global. */
6502 global_entry = bfd_link_hash_lookup (finfo->info->hash, name, FALSE, FALSE, TRUE);
6506 if (global_entry->type == bfd_link_hash_defined
6507 || global_entry->type == bfd_link_hash_defweak)
6509 * result = global_entry->u.def.value
6510 + global_entry->u.def.section->output_section->vma
6511 + global_entry->u.def.section->output_offset;
6513 printf ("Found GLOBAL symbol '%s' with value %8.8x\n",
6514 global_entry->root.string, (unsigned int)*result);
6519 if (global_entry->type == bfd_link_hash_common)
6521 *result = global_entry->u.def.value +
6522 bfd_com_section_ptr->output_section->vma +
6523 bfd_com_section_ptr->output_offset;
6525 printf ("Found COMMON symbol '%s' with value %8.8x\n",
6526 global_entry->root.string, (unsigned int)*result);
6535 resolve_section (const char * name,
6536 asection * sections,
6542 for (curr = sections; curr; curr = curr->next)
6543 if (strcmp (curr->name, name) == 0)
6545 *result = curr->vma;
6549 /* Hmm. still haven't found it. try pseudo-section names. */
6550 for (curr = sections; curr; curr = curr->next)
6552 len = strlen (curr->name);
6553 if (len > strlen (name))
6556 if (strncmp (curr->name, name, len) == 0)
6558 if (strncmp (".end", name + len, 4) == 0)
6560 *result = curr->vma + curr->size;
6564 /* Insert more pseudo-section names here, if you like. */
6572 undefined_reference (const char * reftype,
6575 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), reftype, name);
6579 eval_symbol (bfd_vma * result,
6583 struct elf_final_link_info * finfo,
6585 bfd_vma section_offset,
6593 const int bufsz = 4096;
6594 char symbuf [bufsz];
6595 const char * symend;
6596 bfd_boolean symbol_is_section = FALSE;
6601 if (len < 1 || len > bufsz)
6603 bfd_set_error (bfd_error_invalid_operation);
6610 * result = addr + section_offset;
6611 * advanced = sym + 1;
6616 * result = strtoul (sym, advanced, 16);
6620 symbol_is_section = TRUE;
6623 symlen = strtol (sym, &sym, 10);
6624 ++ sym; /* Skip the trailing ':'. */
6626 if ((symend < sym) || ((symlen + 1) > bufsz))
6628 bfd_set_error (bfd_error_invalid_operation);
6632 memcpy (symbuf, sym, symlen);
6633 symbuf [symlen] = '\0';
6634 * advanced = sym + symlen;
6636 /* Is it always possible, with complex symbols, that gas "mis-guessed"
6637 the symbol as a section, or vice-versa. so we're pretty liberal in our
6638 interpretation here; section means "try section first", not "must be a
6639 section", and likewise with symbol. */
6641 if (symbol_is_section)
6643 if ((resolve_section (symbuf, finfo->output_bfd->sections, result) != TRUE)
6644 && (resolve_symbol (symbuf, input_bfd, finfo, result, locsymcount) != TRUE))
6646 undefined_reference ("section", symbuf);
6652 if ((resolve_symbol (symbuf, input_bfd, finfo, result, locsymcount) != TRUE)
6653 && (resolve_section (symbuf, finfo->output_bfd->sections,
6656 undefined_reference ("symbol", symbuf);
6663 /* All that remains are operators. */
6665 #define UNARY_OP(op) \
6666 if (strncmp (sym, #op, strlen (#op)) == 0) \
6668 sym += strlen (#op); \
6671 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
6672 section_offset, locsymcount, signed_p) \
6676 * result = op ((signed)a); \
6683 #define BINARY_OP(op) \
6684 if (strncmp (sym, #op, strlen (#op)) == 0) \
6686 sym += strlen (#op); \
6689 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
6690 section_offset, locsymcount, signed_p) \
6694 if (eval_symbol (& b, sym, & sym, input_bfd, finfo, addr, \
6695 section_offset, locsymcount, signed_p) \
6699 * result = ((signed) a) op ((signed) b); \
6701 * result = a op b; \
6730 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
6731 bfd_set_error (bfd_error_invalid_operation);
6736 /* Entry point to evaluator, called from elf_link_input_bfd. */
6739 evaluate_complex_relocation_symbols (bfd * input_bfd,
6740 struct elf_final_link_info * finfo,
6743 const struct elf_backend_data * bed;
6744 Elf_Internal_Shdr * symtab_hdr;
6745 struct elf_link_hash_entry ** sym_hashes;
6746 asection * reloc_sec;
6747 bfd_boolean result = TRUE;
6749 /* For each section, we're going to check and see if it has any
6750 complex relocations, and we're going to evaluate any of them
6753 if (finfo->info->relocatable)
6756 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
6757 sym_hashes = elf_sym_hashes (input_bfd);
6758 bed = get_elf_backend_data (input_bfd);
6760 for (reloc_sec = input_bfd->sections; reloc_sec; reloc_sec = reloc_sec->next)
6762 Elf_Internal_Rela * internal_relocs;
6765 /* This section was omitted from the link. */
6766 if (! reloc_sec->linker_mark)
6769 /* Only process sections containing relocs. */
6770 if ((reloc_sec->flags & SEC_RELOC) == 0)
6773 if (reloc_sec->reloc_count == 0)
6776 /* Read in the relocs for this section. */
6778 = _bfd_elf_link_read_relocs (input_bfd, reloc_sec, NULL,
6779 (Elf_Internal_Rela *) NULL,
6781 if (internal_relocs == NULL)
6784 for (i = reloc_sec->reloc_count; i--;)
6786 Elf_Internal_Rela * rel;
6789 Elf_Internal_Sym * sym;
6791 bfd_vma section_offset;
6795 rel = internal_relocs + i;
6796 section_offset = reloc_sec->output_section->vma
6797 + reloc_sec->output_offset;
6798 addr = rel->r_offset;
6800 index = ELF32_R_SYM (rel->r_info);
6801 if (bed->s->arch_size == 64)
6804 if (index == STN_UNDEF)
6807 if (index < locsymcount)
6809 /* The symbol is local. */
6810 sym = finfo->internal_syms + index;
6812 /* We're only processing STT_RELC or STT_SRELC type symbols. */
6813 if ((ELF_ST_TYPE (sym->st_info) != STT_RELC) &&
6814 (ELF_ST_TYPE (sym->st_info) != STT_SRELC))
6817 sym_name = bfd_elf_string_from_elf_section
6818 (input_bfd, symtab_hdr->sh_link, sym->st_name);
6820 signed_p = (ELF_ST_TYPE (sym->st_info) == STT_SRELC);
6824 /* The symbol is global. */
6825 struct elf_link_hash_entry * h;
6827 if (elf_bad_symtab (input_bfd))
6830 h = sym_hashes [index - locsymcount];
6831 while ( h->root.type == bfd_link_hash_indirect
6832 || h->root.type == bfd_link_hash_warning)
6833 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6835 if (h->type != STT_RELC && h->type != STT_SRELC)
6838 signed_p = (h->type == STT_SRELC);
6839 sym_name = (char *) h->root.root.string;
6842 printf ("Encountered a complex symbol!");
6843 printf (" (input_bfd %s, section %s, reloc %ld\n",
6844 input_bfd->filename, reloc_sec->name, i);
6845 printf (" symbol: idx %8.8lx, name %s\n",
6847 printf (" reloc : info %8.8lx, addr %8.8lx\n",
6849 printf (" Evaluating '%s' ...\n ", sym_name);
6851 if (eval_symbol (& result, sym_name, & sym_name, input_bfd,
6852 finfo, addr, section_offset, locsymcount,
6854 /* Symbol evaluated OK. Update to absolute value. */
6855 set_symbol_value (input_bfd, finfo, index, result);
6861 if (internal_relocs != elf_section_data (reloc_sec)->relocs)
6862 free (internal_relocs);
6865 /* If nothing went wrong, then we adjusted
6866 everything we wanted to adjust. */
6871 put_value (bfd_vma size,
6872 unsigned long chunksz,
6875 bfd_byte * location)
6877 location += (size - chunksz);
6879 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
6887 bfd_put_8 (input_bfd, x, location);
6890 bfd_put_16 (input_bfd, x, location);
6893 bfd_put_32 (input_bfd, x, location);
6897 bfd_put_64 (input_bfd, x, location);
6907 get_value (bfd_vma size,
6908 unsigned long chunksz,
6910 bfd_byte * location)
6914 for (; size; size -= chunksz, location += chunksz)
6922 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
6925 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
6928 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
6932 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
6943 decode_complex_addend
6944 (unsigned long * start, /* in bits */
6945 unsigned long * oplen, /* in bits */
6946 unsigned long * len, /* in bits */
6947 unsigned long * wordsz, /* in bytes */
6948 unsigned long * chunksz, /* in bytes */
6949 unsigned long * lsb0_p,
6950 unsigned long * signed_p,
6951 unsigned long * trunc_p,
6952 unsigned long encoded)
6954 * start = encoded & 0x3F;
6955 * len = (encoded >> 6) & 0x3F;
6956 * oplen = (encoded >> 12) & 0x3F;
6957 * wordsz = (encoded >> 18) & 0xF;
6958 * chunksz = (encoded >> 22) & 0xF;
6959 * lsb0_p = (encoded >> 27) & 1;
6960 * signed_p = (encoded >> 28) & 1;
6961 * trunc_p = (encoded >> 29) & 1;
6965 bfd_elf_perform_complex_relocation
6966 (bfd * output_bfd ATTRIBUTE_UNUSED,
6967 struct bfd_link_info * info,
6969 asection * input_section,
6970 bfd_byte * contents,
6971 Elf_Internal_Rela * rel,
6972 Elf_Internal_Sym * local_syms,
6973 asection ** local_sections)
6975 const struct elf_backend_data * bed;
6976 Elf_Internal_Shdr * symtab_hdr;
6978 bfd_vma relocation = 0, shift, x;
6981 unsigned long start, oplen, len, wordsz,
6982 chunksz, lsb0_p, signed_p, trunc_p;
6984 /* Perform this reloc, since it is complex.
6985 (this is not to say that it necessarily refers to a complex
6986 symbol; merely that it is a self-describing CGEN based reloc.
6987 i.e. the addend has the complete reloc information (bit start, end,
6988 word size, etc) encoded within it.). */
6989 r_symndx = ELF32_R_SYM (rel->r_info);
6990 bed = get_elf_backend_data (input_bfd);
6991 if (bed->s->arch_size == 64)
6995 printf ("Performing complex relocation %ld...\n", r_symndx);
6998 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
6999 if (r_symndx < symtab_hdr->sh_info)
7001 /* The symbol is local. */
7002 Elf_Internal_Sym * sym;
7004 sym = local_syms + r_symndx;
7005 sec = local_sections [r_symndx];
7006 relocation = sym->st_value;
7007 if (sym->st_shndx > SHN_UNDEF &&
7008 sym->st_shndx < SHN_LORESERVE)
7009 relocation += (sec->output_offset +
7010 sec->output_section->vma);
7014 /* The symbol is global. */
7015 struct elf_link_hash_entry **sym_hashes;
7016 struct elf_link_hash_entry * h;
7018 sym_hashes = elf_sym_hashes (input_bfd);
7019 h = sym_hashes [r_symndx];
7021 while (h->root.type == bfd_link_hash_indirect
7022 || h->root.type == bfd_link_hash_warning)
7023 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7025 if (h->root.type == bfd_link_hash_defined
7026 || h->root.type == bfd_link_hash_defweak)
7028 sec = h->root.u.def.section;
7029 relocation = h->root.u.def.value;
7031 if (! bfd_is_abs_section (sec))
7032 relocation += (sec->output_section->vma
7033 + sec->output_offset);
7035 if (h->root.type == bfd_link_hash_undefined
7036 && !((*info->callbacks->undefined_symbol)
7037 (info, h->root.root.string, input_bfd,
7038 input_section, rel->r_offset,
7039 info->unresolved_syms_in_objects == RM_GENERATE_ERROR
7040 || ELF_ST_VISIBILITY (h->other))))
7044 decode_complex_addend (& start, & oplen, & len, & wordsz,
7045 & chunksz, & lsb0_p, & signed_p,
7046 & trunc_p, rel->r_addend);
7048 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7051 shift = (start + 1) - len;
7053 shift = (8 * wordsz) - (start + len);
7055 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7058 printf ("Doing complex reloc: "
7059 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7060 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7061 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7062 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7063 oplen, x, mask, relocation);
7068 /* Now do an overflow check. */
7069 if (bfd_check_overflow ((signed_p ?
7070 complain_overflow_signed :
7071 complain_overflow_unsigned),
7072 len, 0, (8 * wordsz),
7073 relocation) == bfd_reloc_overflow)
7074 (*_bfd_error_handler)
7075 ("%s (%s + 0x%lx): relocation overflow: 0x%lx %sdoes not fit "
7077 input_bfd->filename, input_section->name, rel->r_offset,
7078 relocation, (signed_p ? "(signed) " : ""), mask);
7082 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7085 printf (" relocation: %8.8lx\n"
7086 " shifted mask: %8.8lx\n"
7087 " shifted/masked reloc: %8.8lx\n"
7088 " result: %8.8lx\n",
7089 relocation, (mask << shift),
7090 ((relocation & mask) << shift), x);
7092 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
7095 /* When performing a relocatable link, the input relocations are
7096 preserved. But, if they reference global symbols, the indices
7097 referenced must be updated. Update all the relocations in
7098 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7101 elf_link_adjust_relocs (bfd *abfd,
7102 Elf_Internal_Shdr *rel_hdr,
7104 struct elf_link_hash_entry **rel_hash)
7107 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7109 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7110 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7111 bfd_vma r_type_mask;
7114 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
7116 swap_in = bed->s->swap_reloc_in;
7117 swap_out = bed->s->swap_reloc_out;
7119 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
7121 swap_in = bed->s->swap_reloca_in;
7122 swap_out = bed->s->swap_reloca_out;
7127 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7130 if (bed->s->arch_size == 32)
7137 r_type_mask = 0xffffffff;
7141 erela = rel_hdr->contents;
7142 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
7144 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7147 if (*rel_hash == NULL)
7150 BFD_ASSERT ((*rel_hash)->indx >= 0);
7152 (*swap_in) (abfd, erela, irela);
7153 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7154 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7155 | (irela[j].r_info & r_type_mask));
7156 (*swap_out) (abfd, irela, erela);
7160 struct elf_link_sort_rela
7166 enum elf_reloc_type_class type;
7167 /* We use this as an array of size int_rels_per_ext_rel. */
7168 Elf_Internal_Rela rela[1];
7172 elf_link_sort_cmp1 (const void *A, const void *B)
7174 const struct elf_link_sort_rela *a = A;
7175 const struct elf_link_sort_rela *b = B;
7176 int relativea, relativeb;
7178 relativea = a->type == reloc_class_relative;
7179 relativeb = b->type == reloc_class_relative;
7181 if (relativea < relativeb)
7183 if (relativea > relativeb)
7185 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
7187 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
7189 if (a->rela->r_offset < b->rela->r_offset)
7191 if (a->rela->r_offset > b->rela->r_offset)
7197 elf_link_sort_cmp2 (const void *A, const void *B)
7199 const struct elf_link_sort_rela *a = A;
7200 const struct elf_link_sort_rela *b = B;
7203 if (a->u.offset < b->u.offset)
7205 if (a->u.offset > b->u.offset)
7207 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
7208 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
7213 if (a->rela->r_offset < b->rela->r_offset)
7215 if (a->rela->r_offset > b->rela->r_offset)
7221 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
7224 bfd_size_type count, size;
7225 size_t i, ret, sort_elt, ext_size;
7226 bfd_byte *sort, *s_non_relative, *p;
7227 struct elf_link_sort_rela *sq;
7228 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7229 int i2e = bed->s->int_rels_per_ext_rel;
7230 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7231 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7232 struct bfd_link_order *lo;
7235 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
7236 if (reldyn == NULL || reldyn->size == 0)
7238 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
7239 if (reldyn == NULL || reldyn->size == 0)
7241 ext_size = bed->s->sizeof_rel;
7242 swap_in = bed->s->swap_reloc_in;
7243 swap_out = bed->s->swap_reloc_out;
7247 ext_size = bed->s->sizeof_rela;
7248 swap_in = bed->s->swap_reloca_in;
7249 swap_out = bed->s->swap_reloca_out;
7251 count = reldyn->size / ext_size;
7254 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
7255 if (lo->type == bfd_indirect_link_order)
7257 asection *o = lo->u.indirect.section;
7261 if (size != reldyn->size)
7264 sort_elt = (sizeof (struct elf_link_sort_rela)
7265 + (i2e - 1) * sizeof (Elf_Internal_Rela));
7266 sort = bfd_zmalloc (sort_elt * count);
7269 (*info->callbacks->warning)
7270 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
7274 if (bed->s->arch_size == 32)
7275 r_sym_mask = ~(bfd_vma) 0xff;
7277 r_sym_mask = ~(bfd_vma) 0xffffffff;
7279 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
7280 if (lo->type == bfd_indirect_link_order)
7282 bfd_byte *erel, *erelend;
7283 asection *o = lo->u.indirect.section;
7285 if (o->contents == NULL && o->size != 0)
7287 /* This is a reloc section that is being handled as a normal
7288 section. See bfd_section_from_shdr. We can't combine
7289 relocs in this case. */
7294 erelend = o->contents + o->size;
7295 p = sort + o->output_offset / ext_size * sort_elt;
7296 while (erel < erelend)
7298 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
7299 (*swap_in) (abfd, erel, s->rela);
7300 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
7301 s->u.sym_mask = r_sym_mask;
7307 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
7309 for (i = 0, p = sort; i < count; i++, p += sort_elt)
7311 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
7312 if (s->type != reloc_class_relative)
7318 sq = (struct elf_link_sort_rela *) s_non_relative;
7319 for (; i < count; i++, p += sort_elt)
7321 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
7322 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
7324 sp->u.offset = sq->rela->r_offset;
7327 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
7329 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
7330 if (lo->type == bfd_indirect_link_order)
7332 bfd_byte *erel, *erelend;
7333 asection *o = lo->u.indirect.section;
7336 erelend = o->contents + o->size;
7337 p = sort + o->output_offset / ext_size * sort_elt;
7338 while (erel < erelend)
7340 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
7341 (*swap_out) (abfd, s->rela, erel);
7352 /* Flush the output symbols to the file. */
7355 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
7356 const struct elf_backend_data *bed)
7358 if (finfo->symbuf_count > 0)
7360 Elf_Internal_Shdr *hdr;
7364 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
7365 pos = hdr->sh_offset + hdr->sh_size;
7366 amt = finfo->symbuf_count * bed->s->sizeof_sym;
7367 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
7368 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
7371 hdr->sh_size += amt;
7372 finfo->symbuf_count = 0;
7378 /* Add a symbol to the output symbol table. */
7381 elf_link_output_sym (struct elf_final_link_info *finfo,
7383 Elf_Internal_Sym *elfsym,
7384 asection *input_sec,
7385 struct elf_link_hash_entry *h)
7388 Elf_External_Sym_Shndx *destshndx;
7389 bfd_boolean (*output_symbol_hook)
7390 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
7391 struct elf_link_hash_entry *);
7392 const struct elf_backend_data *bed;
7394 bed = get_elf_backend_data (finfo->output_bfd);
7395 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
7396 if (output_symbol_hook != NULL)
7398 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
7402 if (name == NULL || *name == '\0')
7403 elfsym->st_name = 0;
7404 else if (input_sec->flags & SEC_EXCLUDE)
7405 elfsym->st_name = 0;
7408 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
7410 if (elfsym->st_name == (unsigned long) -1)
7414 if (finfo->symbuf_count >= finfo->symbuf_size)
7416 if (! elf_link_flush_output_syms (finfo, bed))
7420 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
7421 destshndx = finfo->symshndxbuf;
7422 if (destshndx != NULL)
7424 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
7428 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
7429 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
7430 if (destshndx == NULL)
7432 memset ((char *) destshndx + amt, 0, amt);
7433 finfo->shndxbuf_size *= 2;
7435 destshndx += bfd_get_symcount (finfo->output_bfd);
7438 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
7439 finfo->symbuf_count += 1;
7440 bfd_get_symcount (finfo->output_bfd) += 1;
7445 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
7448 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
7450 if (sym->st_shndx > SHN_HIRESERVE)
7452 /* The gABI doesn't support dynamic symbols in output sections
7454 (*_bfd_error_handler)
7455 (_("%B: Too many sections: %d (>= %d)"),
7456 abfd, bfd_count_sections (abfd), SHN_LORESERVE);
7457 bfd_set_error (bfd_error_nonrepresentable_section);
7463 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
7464 allowing an unsatisfied unversioned symbol in the DSO to match a
7465 versioned symbol that would normally require an explicit version.
7466 We also handle the case that a DSO references a hidden symbol
7467 which may be satisfied by a versioned symbol in another DSO. */
7470 elf_link_check_versioned_symbol (struct bfd_link_info *info,
7471 const struct elf_backend_data *bed,
7472 struct elf_link_hash_entry *h)
7475 struct elf_link_loaded_list *loaded;
7477 if (!is_elf_hash_table (info->hash))
7480 switch (h->root.type)
7486 case bfd_link_hash_undefined:
7487 case bfd_link_hash_undefweak:
7488 abfd = h->root.u.undef.abfd;
7489 if ((abfd->flags & DYNAMIC) == 0
7490 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
7494 case bfd_link_hash_defined:
7495 case bfd_link_hash_defweak:
7496 abfd = h->root.u.def.section->owner;
7499 case bfd_link_hash_common:
7500 abfd = h->root.u.c.p->section->owner;
7503 BFD_ASSERT (abfd != NULL);
7505 for (loaded = elf_hash_table (info)->loaded;
7507 loaded = loaded->next)
7510 Elf_Internal_Shdr *hdr;
7511 bfd_size_type symcount;
7512 bfd_size_type extsymcount;
7513 bfd_size_type extsymoff;
7514 Elf_Internal_Shdr *versymhdr;
7515 Elf_Internal_Sym *isym;
7516 Elf_Internal_Sym *isymend;
7517 Elf_Internal_Sym *isymbuf;
7518 Elf_External_Versym *ever;
7519 Elf_External_Versym *extversym;
7521 input = loaded->abfd;
7523 /* We check each DSO for a possible hidden versioned definition. */
7525 || (input->flags & DYNAMIC) == 0
7526 || elf_dynversym (input) == 0)
7529 hdr = &elf_tdata (input)->dynsymtab_hdr;
7531 symcount = hdr->sh_size / bed->s->sizeof_sym;
7532 if (elf_bad_symtab (input))
7534 extsymcount = symcount;
7539 extsymcount = symcount - hdr->sh_info;
7540 extsymoff = hdr->sh_info;
7543 if (extsymcount == 0)
7546 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
7548 if (isymbuf == NULL)
7551 /* Read in any version definitions. */
7552 versymhdr = &elf_tdata (input)->dynversym_hdr;
7553 extversym = bfd_malloc (versymhdr->sh_size);
7554 if (extversym == NULL)
7557 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
7558 || (bfd_bread (extversym, versymhdr->sh_size, input)
7559 != versymhdr->sh_size))
7567 ever = extversym + extsymoff;
7568 isymend = isymbuf + extsymcount;
7569 for (isym = isymbuf; isym < isymend; isym++, ever++)
7572 Elf_Internal_Versym iver;
7573 unsigned short version_index;
7575 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
7576 || isym->st_shndx == SHN_UNDEF)
7579 name = bfd_elf_string_from_elf_section (input,
7582 if (strcmp (name, h->root.root.string) != 0)
7585 _bfd_elf_swap_versym_in (input, ever, &iver);
7587 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
7589 /* If we have a non-hidden versioned sym, then it should
7590 have provided a definition for the undefined sym. */
7594 version_index = iver.vs_vers & VERSYM_VERSION;
7595 if (version_index == 1 || version_index == 2)
7597 /* This is the base or first version. We can use it. */
7611 /* Add an external symbol to the symbol table. This is called from
7612 the hash table traversal routine. When generating a shared object,
7613 we go through the symbol table twice. The first time we output
7614 anything that might have been forced to local scope in a version
7615 script. The second time we output the symbols that are still
7619 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
7621 struct elf_outext_info *eoinfo = data;
7622 struct elf_final_link_info *finfo = eoinfo->finfo;
7624 Elf_Internal_Sym sym;
7625 asection *input_sec;
7626 const struct elf_backend_data *bed;
7628 if (h->root.type == bfd_link_hash_warning)
7630 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7631 if (h->root.type == bfd_link_hash_new)
7635 /* Decide whether to output this symbol in this pass. */
7636 if (eoinfo->localsyms)
7638 if (!h->forced_local)
7643 if (h->forced_local)
7647 bed = get_elf_backend_data (finfo->output_bfd);
7649 if (h->root.type == bfd_link_hash_undefined)
7651 /* If we have an undefined symbol reference here then it must have
7652 come from a shared library that is being linked in. (Undefined
7653 references in regular files have already been handled). */
7654 bfd_boolean ignore_undef = FALSE;
7656 /* Some symbols may be special in that the fact that they're
7657 undefined can be safely ignored - let backend determine that. */
7658 if (bed->elf_backend_ignore_undef_symbol)
7659 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
7661 /* If we are reporting errors for this situation then do so now. */
7662 if (ignore_undef == FALSE
7665 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
7666 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
7668 if (! (finfo->info->callbacks->undefined_symbol
7669 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
7670 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
7672 eoinfo->failed = TRUE;
7678 /* We should also warn if a forced local symbol is referenced from
7679 shared libraries. */
7680 if (! finfo->info->relocatable
7681 && (! finfo->info->shared)
7686 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
7688 (*_bfd_error_handler)
7689 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
7691 h->root.u.def.section == bfd_abs_section_ptr
7692 ? finfo->output_bfd : h->root.u.def.section->owner,
7693 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
7695 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
7696 ? "hidden" : "local",
7697 h->root.root.string);
7698 eoinfo->failed = TRUE;
7702 /* We don't want to output symbols that have never been mentioned by
7703 a regular file, or that we have been told to strip. However, if
7704 h->indx is set to -2, the symbol is used by a reloc and we must
7708 else if ((h->def_dynamic
7710 || h->root.type == bfd_link_hash_new)
7714 else if (finfo->info->strip == strip_all)
7716 else if (finfo->info->strip == strip_some
7717 && bfd_hash_lookup (finfo->info->keep_hash,
7718 h->root.root.string, FALSE, FALSE) == NULL)
7720 else if (finfo->info->strip_discarded
7721 && (h->root.type == bfd_link_hash_defined
7722 || h->root.type == bfd_link_hash_defweak)
7723 && elf_discarded_section (h->root.u.def.section))
7728 /* If we're stripping it, and it's not a dynamic symbol, there's
7729 nothing else to do unless it is a forced local symbol. */
7732 && !h->forced_local)
7736 sym.st_size = h->size;
7737 sym.st_other = h->other;
7738 if (h->forced_local)
7739 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
7740 else if (h->root.type == bfd_link_hash_undefweak
7741 || h->root.type == bfd_link_hash_defweak)
7742 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
7744 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
7746 switch (h->root.type)
7749 case bfd_link_hash_new:
7750 case bfd_link_hash_warning:
7754 case bfd_link_hash_undefined:
7755 case bfd_link_hash_undefweak:
7756 input_sec = bfd_und_section_ptr;
7757 sym.st_shndx = SHN_UNDEF;
7760 case bfd_link_hash_defined:
7761 case bfd_link_hash_defweak:
7763 input_sec = h->root.u.def.section;
7764 if (input_sec->output_section != NULL)
7767 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
7768 input_sec->output_section);
7769 if (sym.st_shndx == SHN_BAD)
7771 (*_bfd_error_handler)
7772 (_("%B: could not find output section %A for input section %A"),
7773 finfo->output_bfd, input_sec->output_section, input_sec);
7774 eoinfo->failed = TRUE;
7778 /* ELF symbols in relocatable files are section relative,
7779 but in nonrelocatable files they are virtual
7781 sym.st_value = h->root.u.def.value + input_sec->output_offset;
7782 if (! finfo->info->relocatable)
7784 sym.st_value += input_sec->output_section->vma;
7785 if (h->type == STT_TLS)
7787 /* STT_TLS symbols are relative to PT_TLS segment
7789 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
7790 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
7796 BFD_ASSERT (input_sec->owner == NULL
7797 || (input_sec->owner->flags & DYNAMIC) != 0);
7798 sym.st_shndx = SHN_UNDEF;
7799 input_sec = bfd_und_section_ptr;
7804 case bfd_link_hash_common:
7805 input_sec = h->root.u.c.p->section;
7806 sym.st_shndx = bed->common_section_index (input_sec);
7807 sym.st_value = 1 << h->root.u.c.p->alignment_power;
7810 case bfd_link_hash_indirect:
7811 /* These symbols are created by symbol versioning. They point
7812 to the decorated version of the name. For example, if the
7813 symbol foo@@GNU_1.2 is the default, which should be used when
7814 foo is used with no version, then we add an indirect symbol
7815 foo which points to foo@@GNU_1.2. We ignore these symbols,
7816 since the indirected symbol is already in the hash table. */
7820 /* Give the processor backend a chance to tweak the symbol value,
7821 and also to finish up anything that needs to be done for this
7822 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
7823 forced local syms when non-shared is due to a historical quirk. */
7824 if ((h->dynindx != -1
7826 && ((finfo->info->shared
7827 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7828 || h->root.type != bfd_link_hash_undefweak))
7829 || !h->forced_local)
7830 && elf_hash_table (finfo->info)->dynamic_sections_created)
7832 if (! ((*bed->elf_backend_finish_dynamic_symbol)
7833 (finfo->output_bfd, finfo->info, h, &sym)))
7835 eoinfo->failed = TRUE;
7840 /* If we are marking the symbol as undefined, and there are no
7841 non-weak references to this symbol from a regular object, then
7842 mark the symbol as weak undefined; if there are non-weak
7843 references, mark the symbol as strong. We can't do this earlier,
7844 because it might not be marked as undefined until the
7845 finish_dynamic_symbol routine gets through with it. */
7846 if (sym.st_shndx == SHN_UNDEF
7848 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
7849 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
7853 if (h->ref_regular_nonweak)
7854 bindtype = STB_GLOBAL;
7856 bindtype = STB_WEAK;
7857 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
7860 /* If a non-weak symbol with non-default visibility is not defined
7861 locally, it is a fatal error. */
7862 if (! finfo->info->relocatable
7863 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
7864 && ELF_ST_BIND (sym.st_info) != STB_WEAK
7865 && h->root.type == bfd_link_hash_undefined
7868 (*_bfd_error_handler)
7869 (_("%B: %s symbol `%s' isn't defined"),
7871 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
7873 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
7874 ? "internal" : "hidden",
7875 h->root.root.string);
7876 eoinfo->failed = TRUE;
7880 /* If this symbol should be put in the .dynsym section, then put it
7881 there now. We already know the symbol index. We also fill in
7882 the entry in the .hash section. */
7883 if (h->dynindx != -1
7884 && elf_hash_table (finfo->info)->dynamic_sections_created)
7888 sym.st_name = h->dynstr_index;
7889 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
7890 if (! check_dynsym (finfo->output_bfd, &sym))
7892 eoinfo->failed = TRUE;
7895 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
7897 if (finfo->hash_sec != NULL)
7899 size_t hash_entry_size;
7900 bfd_byte *bucketpos;
7905 bucketcount = elf_hash_table (finfo->info)->bucketcount;
7906 bucket = h->u.elf_hash_value % bucketcount;
7909 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
7910 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
7911 + (bucket + 2) * hash_entry_size);
7912 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
7913 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
7914 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
7915 ((bfd_byte *) finfo->hash_sec->contents
7916 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
7919 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
7921 Elf_Internal_Versym iversym;
7922 Elf_External_Versym *eversym;
7924 if (!h->def_regular)
7926 if (h->verinfo.verdef == NULL)
7927 iversym.vs_vers = 0;
7929 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
7933 if (h->verinfo.vertree == NULL)
7934 iversym.vs_vers = 1;
7936 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
7937 if (finfo->info->create_default_symver)
7942 iversym.vs_vers |= VERSYM_HIDDEN;
7944 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
7945 eversym += h->dynindx;
7946 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
7950 /* If we're stripping it, then it was just a dynamic symbol, and
7951 there's nothing else to do. */
7952 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
7955 h->indx = bfd_get_symcount (finfo->output_bfd);
7957 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
7959 eoinfo->failed = TRUE;
7966 /* Return TRUE if special handling is done for relocs in SEC against
7967 symbols defined in discarded sections. */
7970 elf_section_ignore_discarded_relocs (asection *sec)
7972 const struct elf_backend_data *bed;
7974 switch (sec->sec_info_type)
7976 case ELF_INFO_TYPE_STABS:
7977 case ELF_INFO_TYPE_EH_FRAME:
7983 bed = get_elf_backend_data (sec->owner);
7984 if (bed->elf_backend_ignore_discarded_relocs != NULL
7985 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
7991 /* Return a mask saying how ld should treat relocations in SEC against
7992 symbols defined in discarded sections. If this function returns
7993 COMPLAIN set, ld will issue a warning message. If this function
7994 returns PRETEND set, and the discarded section was link-once and the
7995 same size as the kept link-once section, ld will pretend that the
7996 symbol was actually defined in the kept section. Otherwise ld will
7997 zero the reloc (at least that is the intent, but some cooperation by
7998 the target dependent code is needed, particularly for REL targets). */
8001 _bfd_elf_default_action_discarded (asection *sec)
8003 if (sec->flags & SEC_DEBUGGING)
8006 if (strcmp (".eh_frame", sec->name) == 0)
8009 if (strcmp (".gcc_except_table", sec->name) == 0)
8012 return COMPLAIN | PRETEND;
8015 /* Find a match between a section and a member of a section group. */
8018 match_group_member (asection *sec, asection *group,
8019 struct bfd_link_info *info)
8021 asection *first = elf_next_in_group (group);
8022 asection *s = first;
8026 if (bfd_elf_match_symbols_in_sections (s, sec, info))
8029 s = elf_next_in_group (s);
8037 /* Check if the kept section of a discarded section SEC can be used
8038 to replace it. Return the replacement if it is OK. Otherwise return
8042 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
8046 kept = sec->kept_section;
8049 if ((kept->flags & SEC_GROUP) != 0)
8050 kept = match_group_member (sec, kept, info);
8051 if (kept != NULL && sec->size != kept->size)
8053 sec->kept_section = kept;
8058 /* Link an input file into the linker output file. This function
8059 handles all the sections and relocations of the input file at once.
8060 This is so that we only have to read the local symbols once, and
8061 don't have to keep them in memory. */
8064 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
8066 bfd_boolean (*relocate_section)
8067 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
8068 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
8070 Elf_Internal_Shdr *symtab_hdr;
8073 Elf_Internal_Sym *isymbuf;
8074 Elf_Internal_Sym *isym;
8075 Elf_Internal_Sym *isymend;
8077 asection **ppsection;
8079 const struct elf_backend_data *bed;
8080 bfd_boolean emit_relocs;
8081 struct elf_link_hash_entry **sym_hashes;
8083 output_bfd = finfo->output_bfd;
8084 bed = get_elf_backend_data (output_bfd);
8085 relocate_section = bed->elf_backend_relocate_section;
8087 /* If this is a dynamic object, we don't want to do anything here:
8088 we don't want the local symbols, and we don't want the section
8090 if ((input_bfd->flags & DYNAMIC) != 0)
8093 emit_relocs = (finfo->info->relocatable
8094 || finfo->info->emitrelocations);
8096 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8097 if (elf_bad_symtab (input_bfd))
8099 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8104 locsymcount = symtab_hdr->sh_info;
8105 extsymoff = symtab_hdr->sh_info;
8108 /* Read the local symbols. */
8109 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8110 if (isymbuf == NULL && locsymcount != 0)
8112 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8113 finfo->internal_syms,
8114 finfo->external_syms,
8115 finfo->locsym_shndx);
8116 if (isymbuf == NULL)
8119 /* evaluate_complex_relocation_symbols looks for symbols in
8120 finfo->internal_syms. */
8121 else if (isymbuf != NULL && locsymcount != 0)
8123 bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8124 finfo->internal_syms,
8125 finfo->external_syms,
8126 finfo->locsym_shndx);
8129 /* Find local symbol sections and adjust values of symbols in
8130 SEC_MERGE sections. Write out those local symbols we know are
8131 going into the output file. */
8132 isymend = isymbuf + locsymcount;
8133 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
8135 isym++, pindex++, ppsection++)
8139 Elf_Internal_Sym osym;
8143 if (elf_bad_symtab (input_bfd))
8145 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
8152 if (isym->st_shndx == SHN_UNDEF)
8153 isec = bfd_und_section_ptr;
8154 else if (isym->st_shndx < SHN_LORESERVE
8155 || isym->st_shndx > SHN_HIRESERVE)
8157 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
8159 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
8160 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
8162 _bfd_merged_section_offset (output_bfd, &isec,
8163 elf_section_data (isec)->sec_info,
8166 else if (isym->st_shndx == SHN_ABS)
8167 isec = bfd_abs_section_ptr;
8168 else if (isym->st_shndx == SHN_COMMON)
8169 isec = bfd_com_section_ptr;
8172 /* Don't attempt to output symbols with st_shnx in the
8173 reserved range other than SHN_ABS and SHN_COMMON. */
8180 /* Don't output the first, undefined, symbol. */
8181 if (ppsection == finfo->sections)
8184 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
8186 /* We never output section symbols. Instead, we use the
8187 section symbol of the corresponding section in the output
8192 /* If we are stripping all symbols, we don't want to output this
8194 if (finfo->info->strip == strip_all)
8197 /* If we are discarding all local symbols, we don't want to
8198 output this one. If we are generating a relocatable output
8199 file, then some of the local symbols may be required by
8200 relocs; we output them below as we discover that they are
8202 if (finfo->info->discard == discard_all)
8205 /* If this symbol is defined in a section which we are
8206 discarding, we don't need to keep it. */
8207 if (isym->st_shndx != SHN_UNDEF
8208 && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
8210 || bfd_section_removed_from_list (output_bfd,
8211 isec->output_section)))
8214 /* Get the name of the symbol. */
8215 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
8220 /* See if we are discarding symbols with this name. */
8221 if ((finfo->info->strip == strip_some
8222 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
8224 || (((finfo->info->discard == discard_sec_merge
8225 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
8226 || finfo->info->discard == discard_l)
8227 && bfd_is_local_label_name (input_bfd, name)))
8230 /* If we get here, we are going to output this symbol. */
8234 /* Adjust the section index for the output file. */
8235 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
8236 isec->output_section);
8237 if (osym.st_shndx == SHN_BAD)
8240 *pindex = bfd_get_symcount (output_bfd);
8242 /* ELF symbols in relocatable files are section relative, but
8243 in executable files they are virtual addresses. Note that
8244 this code assumes that all ELF sections have an associated
8245 BFD section with a reasonable value for output_offset; below
8246 we assume that they also have a reasonable value for
8247 output_section. Any special sections must be set up to meet
8248 these requirements. */
8249 osym.st_value += isec->output_offset;
8250 if (! finfo->info->relocatable)
8252 osym.st_value += isec->output_section->vma;
8253 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
8255 /* STT_TLS symbols are relative to PT_TLS segment base. */
8256 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
8257 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
8261 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
8265 if (! evaluate_complex_relocation_symbols (input_bfd, finfo, locsymcount))
8268 /* Relocate the contents of each section. */
8269 sym_hashes = elf_sym_hashes (input_bfd);
8270 for (o = input_bfd->sections; o != NULL; o = o->next)
8274 if (! o->linker_mark)
8276 /* This section was omitted from the link. */
8280 if ((o->flags & SEC_HAS_CONTENTS) == 0
8281 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
8284 if ((o->flags & SEC_LINKER_CREATED) != 0)
8286 /* Section was created by _bfd_elf_link_create_dynamic_sections
8291 /* Get the contents of the section. They have been cached by a
8292 relaxation routine. Note that o is a section in an input
8293 file, so the contents field will not have been set by any of
8294 the routines which work on output files. */
8295 if (elf_section_data (o)->this_hdr.contents != NULL)
8296 contents = elf_section_data (o)->this_hdr.contents;
8299 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
8301 contents = finfo->contents;
8302 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
8306 if ((o->flags & SEC_RELOC) != 0)
8308 Elf_Internal_Rela *internal_relocs;
8309 bfd_vma r_type_mask;
8312 /* Get the swapped relocs. */
8314 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
8315 finfo->internal_relocs, FALSE);
8316 if (internal_relocs == NULL
8317 && o->reloc_count > 0)
8320 if (bed->s->arch_size == 32)
8327 r_type_mask = 0xffffffff;
8331 /* Run through the relocs looking for any against symbols
8332 from discarded sections and section symbols from
8333 removed link-once sections. Complain about relocs
8334 against discarded sections. Zero relocs against removed
8335 link-once sections. */
8336 if (!elf_section_ignore_discarded_relocs (o))
8338 Elf_Internal_Rela *rel, *relend;
8339 unsigned int action = (*bed->action_discarded) (o);
8341 rel = internal_relocs;
8342 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
8343 for ( ; rel < relend; rel++)
8345 unsigned long r_symndx = rel->r_info >> r_sym_shift;
8346 asection **ps, *sec;
8347 struct elf_link_hash_entry *h = NULL;
8348 const char *sym_name;
8350 if (r_symndx == STN_UNDEF)
8353 if (r_symndx >= locsymcount
8354 || (elf_bad_symtab (input_bfd)
8355 && finfo->sections[r_symndx] == NULL))
8357 h = sym_hashes[r_symndx - extsymoff];
8359 /* Badly formatted input files can contain relocs that
8360 reference non-existant symbols. Check here so that
8361 we do not seg fault. */
8366 sprintf_vma (buffer, rel->r_info);
8367 (*_bfd_error_handler)
8368 (_("error: %B contains a reloc (0x%s) for section %A "
8369 "that references a non-existent global symbol"),
8370 input_bfd, o, buffer);
8371 bfd_set_error (bfd_error_bad_value);
8375 while (h->root.type == bfd_link_hash_indirect
8376 || h->root.type == bfd_link_hash_warning)
8377 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8379 if (h->root.type != bfd_link_hash_defined
8380 && h->root.type != bfd_link_hash_defweak)
8383 ps = &h->root.u.def.section;
8384 sym_name = h->root.root.string;
8388 Elf_Internal_Sym *sym = isymbuf + r_symndx;
8389 ps = &finfo->sections[r_symndx];
8390 sym_name = bfd_elf_sym_name (input_bfd,
8395 /* Complain if the definition comes from a
8396 discarded section. */
8397 if ((sec = *ps) != NULL && elf_discarded_section (sec))
8399 BFD_ASSERT (r_symndx != 0);
8400 if (action & COMPLAIN)
8401 (*finfo->info->callbacks->einfo)
8402 (_("%X`%s' referenced in section `%A' of %B: "
8403 "defined in discarded section `%A' of %B\n"),
8404 sym_name, o, input_bfd, sec, sec->owner);
8406 /* Try to do the best we can to support buggy old
8407 versions of gcc. Pretend that the symbol is
8408 really defined in the kept linkonce section.
8409 FIXME: This is quite broken. Modifying the
8410 symbol here means we will be changing all later
8411 uses of the symbol, not just in this section. */
8412 if (action & PRETEND)
8416 kept = _bfd_elf_check_kept_section (sec,
8428 /* Relocate the section by invoking a back end routine.
8430 The back end routine is responsible for adjusting the
8431 section contents as necessary, and (if using Rela relocs
8432 and generating a relocatable output file) adjusting the
8433 reloc addend as necessary.
8435 The back end routine does not have to worry about setting
8436 the reloc address or the reloc symbol index.
8438 The back end routine is given a pointer to the swapped in
8439 internal symbols, and can access the hash table entries
8440 for the external symbols via elf_sym_hashes (input_bfd).
8442 When generating relocatable output, the back end routine
8443 must handle STB_LOCAL/STT_SECTION symbols specially. The
8444 output symbol is going to be a section symbol
8445 corresponding to the output section, which will require
8446 the addend to be adjusted. */
8448 if (! (*relocate_section) (output_bfd, finfo->info,
8449 input_bfd, o, contents,
8457 Elf_Internal_Rela *irela;
8458 Elf_Internal_Rela *irelaend;
8459 bfd_vma last_offset;
8460 struct elf_link_hash_entry **rel_hash;
8461 struct elf_link_hash_entry **rel_hash_list;
8462 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
8463 unsigned int next_erel;
8464 bfd_boolean rela_normal;
8466 input_rel_hdr = &elf_section_data (o)->rel_hdr;
8467 rela_normal = (bed->rela_normal
8468 && (input_rel_hdr->sh_entsize
8469 == bed->s->sizeof_rela));
8471 /* Adjust the reloc addresses and symbol indices. */
8473 irela = internal_relocs;
8474 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
8475 rel_hash = (elf_section_data (o->output_section)->rel_hashes
8476 + elf_section_data (o->output_section)->rel_count
8477 + elf_section_data (o->output_section)->rel_count2);
8478 rel_hash_list = rel_hash;
8479 last_offset = o->output_offset;
8480 if (!finfo->info->relocatable)
8481 last_offset += o->output_section->vma;
8482 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
8484 unsigned long r_symndx;
8486 Elf_Internal_Sym sym;
8488 if (next_erel == bed->s->int_rels_per_ext_rel)
8494 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8497 if (irela->r_offset >= (bfd_vma) -2)
8499 /* This is a reloc for a deleted entry or somesuch.
8500 Turn it into an R_*_NONE reloc, at the same
8501 offset as the last reloc. elf_eh_frame.c and
8502 bfd_elf_discard_info rely on reloc offsets
8504 irela->r_offset = last_offset;
8506 irela->r_addend = 0;
8510 irela->r_offset += o->output_offset;
8512 /* Relocs in an executable have to be virtual addresses. */
8513 if (!finfo->info->relocatable)
8514 irela->r_offset += o->output_section->vma;
8516 last_offset = irela->r_offset;
8518 r_symndx = irela->r_info >> r_sym_shift;
8519 if (r_symndx == STN_UNDEF)
8522 if (r_symndx >= locsymcount
8523 || (elf_bad_symtab (input_bfd)
8524 && finfo->sections[r_symndx] == NULL))
8526 struct elf_link_hash_entry *rh;
8529 /* This is a reloc against a global symbol. We
8530 have not yet output all the local symbols, so
8531 we do not know the symbol index of any global
8532 symbol. We set the rel_hash entry for this
8533 reloc to point to the global hash table entry
8534 for this symbol. The symbol index is then
8535 set at the end of bfd_elf_final_link. */
8536 indx = r_symndx - extsymoff;
8537 rh = elf_sym_hashes (input_bfd)[indx];
8538 while (rh->root.type == bfd_link_hash_indirect
8539 || rh->root.type == bfd_link_hash_warning)
8540 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
8542 /* Setting the index to -2 tells
8543 elf_link_output_extsym that this symbol is
8545 BFD_ASSERT (rh->indx < 0);
8553 /* This is a reloc against a local symbol. */
8556 sym = isymbuf[r_symndx];
8557 sec = finfo->sections[r_symndx];
8558 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
8560 /* I suppose the backend ought to fill in the
8561 section of any STT_SECTION symbol against a
8562 processor specific section. */
8564 if (bfd_is_abs_section (sec))
8566 else if (sec == NULL || sec->owner == NULL)
8568 bfd_set_error (bfd_error_bad_value);
8573 asection *osec = sec->output_section;
8575 /* If we have discarded a section, the output
8576 section will be the absolute section. In
8577 case of discarded SEC_MERGE sections, use
8578 the kept section. relocate_section should
8579 have already handled discarded linkonce
8581 if (bfd_is_abs_section (osec)
8582 && sec->kept_section != NULL
8583 && sec->kept_section->output_section != NULL)
8585 osec = sec->kept_section->output_section;
8586 irela->r_addend -= osec->vma;
8589 if (!bfd_is_abs_section (osec))
8591 r_symndx = osec->target_index;
8594 struct elf_link_hash_table *htab;
8597 htab = elf_hash_table (finfo->info);
8598 oi = htab->text_index_section;
8599 if ((osec->flags & SEC_READONLY) == 0
8600 && htab->data_index_section != NULL)
8601 oi = htab->data_index_section;
8605 irela->r_addend += osec->vma - oi->vma;
8606 r_symndx = oi->target_index;
8610 BFD_ASSERT (r_symndx != 0);
8614 /* Adjust the addend according to where the
8615 section winds up in the output section. */
8617 irela->r_addend += sec->output_offset;
8621 if (finfo->indices[r_symndx] == -1)
8623 unsigned long shlink;
8627 if (finfo->info->strip == strip_all)
8629 /* You can't do ld -r -s. */
8630 bfd_set_error (bfd_error_invalid_operation);
8634 /* This symbol was skipped earlier, but
8635 since it is needed by a reloc, we
8636 must output it now. */
8637 shlink = symtab_hdr->sh_link;
8638 name = (bfd_elf_string_from_elf_section
8639 (input_bfd, shlink, sym.st_name));
8643 osec = sec->output_section;
8645 _bfd_elf_section_from_bfd_section (output_bfd,
8647 if (sym.st_shndx == SHN_BAD)
8650 sym.st_value += sec->output_offset;
8651 if (! finfo->info->relocatable)
8653 sym.st_value += osec->vma;
8654 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
8656 /* STT_TLS symbols are relative to PT_TLS
8658 BFD_ASSERT (elf_hash_table (finfo->info)
8660 sym.st_value -= (elf_hash_table (finfo->info)
8665 finfo->indices[r_symndx]
8666 = bfd_get_symcount (output_bfd);
8668 if (! elf_link_output_sym (finfo, name, &sym, sec,
8673 r_symndx = finfo->indices[r_symndx];
8676 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
8677 | (irela->r_info & r_type_mask));
8680 /* Swap out the relocs. */
8681 if (input_rel_hdr->sh_size != 0
8682 && !bed->elf_backend_emit_relocs (output_bfd, o,
8688 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
8689 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
8691 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
8692 * bed->s->int_rels_per_ext_rel);
8693 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
8694 if (!bed->elf_backend_emit_relocs (output_bfd, o,
8703 /* Write out the modified section contents. */
8704 if (bed->elf_backend_write_section
8705 && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
8708 /* Section written out. */
8710 else switch (o->sec_info_type)
8712 case ELF_INFO_TYPE_STABS:
8713 if (! (_bfd_write_section_stabs
8715 &elf_hash_table (finfo->info)->stab_info,
8716 o, &elf_section_data (o)->sec_info, contents)))
8719 case ELF_INFO_TYPE_MERGE:
8720 if (! _bfd_write_merged_section (output_bfd, o,
8721 elf_section_data (o)->sec_info))
8724 case ELF_INFO_TYPE_EH_FRAME:
8726 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
8733 if (! (o->flags & SEC_EXCLUDE)
8734 && ! bfd_set_section_contents (output_bfd, o->output_section,
8736 (file_ptr) o->output_offset,
8747 /* Generate a reloc when linking an ELF file. This is a reloc
8748 requested by the linker, and does not come from any input file. This
8749 is used to build constructor and destructor tables when linking
8753 elf_reloc_link_order (bfd *output_bfd,
8754 struct bfd_link_info *info,
8755 asection *output_section,
8756 struct bfd_link_order *link_order)
8758 reloc_howto_type *howto;
8762 struct elf_link_hash_entry **rel_hash_ptr;
8763 Elf_Internal_Shdr *rel_hdr;
8764 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
8765 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
8769 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
8772 bfd_set_error (bfd_error_bad_value);
8776 addend = link_order->u.reloc.p->addend;
8778 /* Figure out the symbol index. */
8779 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
8780 + elf_section_data (output_section)->rel_count
8781 + elf_section_data (output_section)->rel_count2);
8782 if (link_order->type == bfd_section_reloc_link_order)
8784 indx = link_order->u.reloc.p->u.section->target_index;
8785 BFD_ASSERT (indx != 0);
8786 *rel_hash_ptr = NULL;
8790 struct elf_link_hash_entry *h;
8792 /* Treat a reloc against a defined symbol as though it were
8793 actually against the section. */
8794 h = ((struct elf_link_hash_entry *)
8795 bfd_wrapped_link_hash_lookup (output_bfd, info,
8796 link_order->u.reloc.p->u.name,
8797 FALSE, FALSE, TRUE));
8799 && (h->root.type == bfd_link_hash_defined
8800 || h->root.type == bfd_link_hash_defweak))
8804 section = h->root.u.def.section;
8805 indx = section->output_section->target_index;
8806 *rel_hash_ptr = NULL;
8807 /* It seems that we ought to add the symbol value to the
8808 addend here, but in practice it has already been added
8809 because it was passed to constructor_callback. */
8810 addend += section->output_section->vma + section->output_offset;
8814 /* Setting the index to -2 tells elf_link_output_extsym that
8815 this symbol is used by a reloc. */
8822 if (! ((*info->callbacks->unattached_reloc)
8823 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
8829 /* If this is an inplace reloc, we must write the addend into the
8831 if (howto->partial_inplace && addend != 0)
8834 bfd_reloc_status_type rstat;
8837 const char *sym_name;
8839 size = bfd_get_reloc_size (howto);
8840 buf = bfd_zmalloc (size);
8843 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
8850 case bfd_reloc_outofrange:
8853 case bfd_reloc_overflow:
8854 if (link_order->type == bfd_section_reloc_link_order)
8855 sym_name = bfd_section_name (output_bfd,
8856 link_order->u.reloc.p->u.section);
8858 sym_name = link_order->u.reloc.p->u.name;
8859 if (! ((*info->callbacks->reloc_overflow)
8860 (info, NULL, sym_name, howto->name, addend, NULL,
8861 NULL, (bfd_vma) 0)))
8868 ok = bfd_set_section_contents (output_bfd, output_section, buf,
8869 link_order->offset, size);
8875 /* The address of a reloc is relative to the section in a
8876 relocatable file, and is a virtual address in an executable
8878 offset = link_order->offset;
8879 if (! info->relocatable)
8880 offset += output_section->vma;
8882 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
8884 irel[i].r_offset = offset;
8886 irel[i].r_addend = 0;
8888 if (bed->s->arch_size == 32)
8889 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
8891 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
8893 rel_hdr = &elf_section_data (output_section)->rel_hdr;
8894 erel = rel_hdr->contents;
8895 if (rel_hdr->sh_type == SHT_REL)
8897 erel += (elf_section_data (output_section)->rel_count
8898 * bed->s->sizeof_rel);
8899 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
8903 irel[0].r_addend = addend;
8904 erel += (elf_section_data (output_section)->rel_count
8905 * bed->s->sizeof_rela);
8906 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
8909 ++elf_section_data (output_section)->rel_count;
8915 /* Get the output vma of the section pointed to by the sh_link field. */
8918 elf_get_linked_section_vma (struct bfd_link_order *p)
8920 Elf_Internal_Shdr **elf_shdrp;
8924 s = p->u.indirect.section;
8925 elf_shdrp = elf_elfsections (s->owner);
8926 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
8927 elfsec = elf_shdrp[elfsec]->sh_link;
8929 The Intel C compiler generates SHT_IA_64_UNWIND with
8930 SHF_LINK_ORDER. But it doesn't set the sh_link or
8931 sh_info fields. Hence we could get the situation
8932 where elfsec is 0. */
8935 const struct elf_backend_data *bed
8936 = get_elf_backend_data (s->owner);
8937 if (bed->link_order_error_handler)
8938 bed->link_order_error_handler
8939 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
8944 s = elf_shdrp[elfsec]->bfd_section;
8945 return s->output_section->vma + s->output_offset;
8950 /* Compare two sections based on the locations of the sections they are
8951 linked to. Used by elf_fixup_link_order. */
8954 compare_link_order (const void * a, const void * b)
8959 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
8960 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
8967 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
8968 order as their linked sections. Returns false if this could not be done
8969 because an output section includes both ordered and unordered
8970 sections. Ideally we'd do this in the linker proper. */
8973 elf_fixup_link_order (bfd *abfd, asection *o)
8978 struct bfd_link_order *p;
8980 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8982 struct bfd_link_order **sections;
8983 asection *s, *other_sec, *linkorder_sec;
8987 linkorder_sec = NULL;
8990 for (p = o->map_head.link_order; p != NULL; p = p->next)
8992 if (p->type == bfd_indirect_link_order)
8994 s = p->u.indirect.section;
8996 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
8997 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
8998 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
8999 && elfsec < elf_numsections (sub)
9000 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
9014 if (seen_other && seen_linkorder)
9016 if (other_sec && linkorder_sec)
9017 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9019 linkorder_sec->owner, other_sec,
9022 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
9024 bfd_set_error (bfd_error_bad_value);
9029 if (!seen_linkorder)
9032 sections = (struct bfd_link_order **)
9033 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
9036 for (p = o->map_head.link_order; p != NULL; p = p->next)
9038 sections[seen_linkorder++] = p;
9040 /* Sort the input sections in the order of their linked section. */
9041 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
9042 compare_link_order);
9044 /* Change the offsets of the sections. */
9046 for (n = 0; n < seen_linkorder; n++)
9048 s = sections[n]->u.indirect.section;
9049 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
9050 s->output_offset = offset;
9051 sections[n]->offset = offset;
9052 offset += sections[n]->size;
9059 /* Do the final step of an ELF link. */
9062 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
9064 bfd_boolean dynamic;
9065 bfd_boolean emit_relocs;
9067 struct elf_final_link_info finfo;
9068 register asection *o;
9069 register struct bfd_link_order *p;
9071 bfd_size_type max_contents_size;
9072 bfd_size_type max_external_reloc_size;
9073 bfd_size_type max_internal_reloc_count;
9074 bfd_size_type max_sym_count;
9075 bfd_size_type max_sym_shndx_count;
9077 Elf_Internal_Sym elfsym;
9079 Elf_Internal_Shdr *symtab_hdr;
9080 Elf_Internal_Shdr *symtab_shndx_hdr;
9081 Elf_Internal_Shdr *symstrtab_hdr;
9082 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9083 struct elf_outext_info eoinfo;
9085 size_t relativecount = 0;
9086 asection *reldyn = 0;
9089 if (! is_elf_hash_table (info->hash))
9093 abfd->flags |= DYNAMIC;
9095 dynamic = elf_hash_table (info)->dynamic_sections_created;
9096 dynobj = elf_hash_table (info)->dynobj;
9098 emit_relocs = (info->relocatable
9099 || info->emitrelocations);
9102 finfo.output_bfd = abfd;
9103 finfo.symstrtab = _bfd_elf_stringtab_init ();
9104 if (finfo.symstrtab == NULL)
9109 finfo.dynsym_sec = NULL;
9110 finfo.hash_sec = NULL;
9111 finfo.symver_sec = NULL;
9115 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
9116 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
9117 BFD_ASSERT (finfo.dynsym_sec != NULL);
9118 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
9119 /* Note that it is OK if symver_sec is NULL. */
9122 finfo.contents = NULL;
9123 finfo.external_relocs = NULL;
9124 finfo.internal_relocs = NULL;
9125 finfo.external_syms = NULL;
9126 finfo.locsym_shndx = NULL;
9127 finfo.internal_syms = NULL;
9128 finfo.indices = NULL;
9129 finfo.sections = NULL;
9130 finfo.symbuf = NULL;
9131 finfo.symshndxbuf = NULL;
9132 finfo.symbuf_count = 0;
9133 finfo.shndxbuf_size = 0;
9135 /* Count up the number of relocations we will output for each output
9136 section, so that we know the sizes of the reloc sections. We
9137 also figure out some maximum sizes. */
9138 max_contents_size = 0;
9139 max_external_reloc_size = 0;
9140 max_internal_reloc_count = 0;
9142 max_sym_shndx_count = 0;
9144 for (o = abfd->sections; o != NULL; o = o->next)
9146 struct bfd_elf_section_data *esdo = elf_section_data (o);
9149 for (p = o->map_head.link_order; p != NULL; p = p->next)
9151 unsigned int reloc_count = 0;
9152 struct bfd_elf_section_data *esdi = NULL;
9153 unsigned int *rel_count1;
9155 if (p->type == bfd_section_reloc_link_order
9156 || p->type == bfd_symbol_reloc_link_order)
9158 else if (p->type == bfd_indirect_link_order)
9162 sec = p->u.indirect.section;
9163 esdi = elf_section_data (sec);
9165 /* Mark all sections which are to be included in the
9166 link. This will normally be every section. We need
9167 to do this so that we can identify any sections which
9168 the linker has decided to not include. */
9169 sec->linker_mark = TRUE;
9171 if (sec->flags & SEC_MERGE)
9174 if (info->relocatable || info->emitrelocations)
9175 reloc_count = sec->reloc_count;
9176 else if (bed->elf_backend_count_relocs)
9178 Elf_Internal_Rela * relocs;
9180 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
9183 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
9185 if (elf_section_data (o)->relocs != relocs)
9189 if (sec->rawsize > max_contents_size)
9190 max_contents_size = sec->rawsize;
9191 if (sec->size > max_contents_size)
9192 max_contents_size = sec->size;
9194 /* We are interested in just local symbols, not all
9196 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
9197 && (sec->owner->flags & DYNAMIC) == 0)
9201 if (elf_bad_symtab (sec->owner))
9202 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
9203 / bed->s->sizeof_sym);
9205 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
9207 if (sym_count > max_sym_count)
9208 max_sym_count = sym_count;
9210 if (sym_count > max_sym_shndx_count
9211 && elf_symtab_shndx (sec->owner) != 0)
9212 max_sym_shndx_count = sym_count;
9214 if ((sec->flags & SEC_RELOC) != 0)
9218 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
9219 if (ext_size > max_external_reloc_size)
9220 max_external_reloc_size = ext_size;
9221 if (sec->reloc_count > max_internal_reloc_count)
9222 max_internal_reloc_count = sec->reloc_count;
9227 if (reloc_count == 0)
9230 o->reloc_count += reloc_count;
9232 /* MIPS may have a mix of REL and RELA relocs on sections.
9233 To support this curious ABI we keep reloc counts in
9234 elf_section_data too. We must be careful to add the
9235 relocations from the input section to the right output
9236 count. FIXME: Get rid of one count. We have
9237 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
9238 rel_count1 = &esdo->rel_count;
9241 bfd_boolean same_size;
9242 bfd_size_type entsize1;
9244 entsize1 = esdi->rel_hdr.sh_entsize;
9245 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
9246 || entsize1 == bed->s->sizeof_rela);
9247 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
9250 rel_count1 = &esdo->rel_count2;
9252 if (esdi->rel_hdr2 != NULL)
9254 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
9255 unsigned int alt_count;
9256 unsigned int *rel_count2;
9258 BFD_ASSERT (entsize2 != entsize1
9259 && (entsize2 == bed->s->sizeof_rel
9260 || entsize2 == bed->s->sizeof_rela));
9262 rel_count2 = &esdo->rel_count2;
9264 rel_count2 = &esdo->rel_count;
9266 /* The following is probably too simplistic if the
9267 backend counts output relocs unusually. */
9268 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
9269 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
9270 *rel_count2 += alt_count;
9271 reloc_count -= alt_count;
9274 *rel_count1 += reloc_count;
9277 if (o->reloc_count > 0)
9278 o->flags |= SEC_RELOC;
9281 /* Explicitly clear the SEC_RELOC flag. The linker tends to
9282 set it (this is probably a bug) and if it is set
9283 assign_section_numbers will create a reloc section. */
9284 o->flags &=~ SEC_RELOC;
9287 /* If the SEC_ALLOC flag is not set, force the section VMA to
9288 zero. This is done in elf_fake_sections as well, but forcing
9289 the VMA to 0 here will ensure that relocs against these
9290 sections are handled correctly. */
9291 if ((o->flags & SEC_ALLOC) == 0
9292 && ! o->user_set_vma)
9296 if (! info->relocatable && merged)
9297 elf_link_hash_traverse (elf_hash_table (info),
9298 _bfd_elf_link_sec_merge_syms, abfd);
9300 /* Figure out the file positions for everything but the symbol table
9301 and the relocs. We set symcount to force assign_section_numbers
9302 to create a symbol table. */
9303 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
9304 BFD_ASSERT (! abfd->output_has_begun);
9305 if (! _bfd_elf_compute_section_file_positions (abfd, info))
9308 /* Set sizes, and assign file positions for reloc sections. */
9309 for (o = abfd->sections; o != NULL; o = o->next)
9311 if ((o->flags & SEC_RELOC) != 0)
9313 if (!(_bfd_elf_link_size_reloc_section
9314 (abfd, &elf_section_data (o)->rel_hdr, o)))
9317 if (elf_section_data (o)->rel_hdr2
9318 && !(_bfd_elf_link_size_reloc_section
9319 (abfd, elf_section_data (o)->rel_hdr2, o)))
9323 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
9324 to count upwards while actually outputting the relocations. */
9325 elf_section_data (o)->rel_count = 0;
9326 elf_section_data (o)->rel_count2 = 0;
9329 _bfd_elf_assign_file_positions_for_relocs (abfd);
9331 /* We have now assigned file positions for all the sections except
9332 .symtab and .strtab. We start the .symtab section at the current
9333 file position, and write directly to it. We build the .strtab
9334 section in memory. */
9335 bfd_get_symcount (abfd) = 0;
9336 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9337 /* sh_name is set in prep_headers. */
9338 symtab_hdr->sh_type = SHT_SYMTAB;
9339 /* sh_flags, sh_addr and sh_size all start off zero. */
9340 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
9341 /* sh_link is set in assign_section_numbers. */
9342 /* sh_info is set below. */
9343 /* sh_offset is set just below. */
9344 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
9346 off = elf_tdata (abfd)->next_file_pos;
9347 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
9349 /* Note that at this point elf_tdata (abfd)->next_file_pos is
9350 incorrect. We do not yet know the size of the .symtab section.
9351 We correct next_file_pos below, after we do know the size. */
9353 /* Allocate a buffer to hold swapped out symbols. This is to avoid
9354 continuously seeking to the right position in the file. */
9355 if (! info->keep_memory || max_sym_count < 20)
9356 finfo.symbuf_size = 20;
9358 finfo.symbuf_size = max_sym_count;
9359 amt = finfo.symbuf_size;
9360 amt *= bed->s->sizeof_sym;
9361 finfo.symbuf = bfd_malloc (amt);
9362 if (finfo.symbuf == NULL)
9364 if (elf_numsections (abfd) > SHN_LORESERVE)
9366 /* Wild guess at number of output symbols. realloc'd as needed. */
9367 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
9368 finfo.shndxbuf_size = amt;
9369 amt *= sizeof (Elf_External_Sym_Shndx);
9370 finfo.symshndxbuf = bfd_zmalloc (amt);
9371 if (finfo.symshndxbuf == NULL)
9375 /* Start writing out the symbol table. The first symbol is always a
9377 if (info->strip != strip_all
9380 elfsym.st_value = 0;
9383 elfsym.st_other = 0;
9384 elfsym.st_shndx = SHN_UNDEF;
9385 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
9390 /* Output a symbol for each section. We output these even if we are
9391 discarding local symbols, since they are used for relocs. These
9392 symbols have no names. We store the index of each one in the
9393 index field of the section, so that we can find it again when
9394 outputting relocs. */
9395 if (info->strip != strip_all
9399 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
9400 elfsym.st_other = 0;
9401 elfsym.st_value = 0;
9402 for (i = 1; i < elf_numsections (abfd); i++)
9404 o = bfd_section_from_elf_index (abfd, i);
9407 o->target_index = bfd_get_symcount (abfd);
9408 elfsym.st_shndx = i;
9409 if (!info->relocatable)
9410 elfsym.st_value = o->vma;
9411 if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
9414 if (i == SHN_LORESERVE - 1)
9415 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
9419 /* Allocate some memory to hold information read in from the input
9421 if (max_contents_size != 0)
9423 finfo.contents = bfd_malloc (max_contents_size);
9424 if (finfo.contents == NULL)
9428 if (max_external_reloc_size != 0)
9430 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
9431 if (finfo.external_relocs == NULL)
9435 if (max_internal_reloc_count != 0)
9437 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
9438 amt *= sizeof (Elf_Internal_Rela);
9439 finfo.internal_relocs = bfd_malloc (amt);
9440 if (finfo.internal_relocs == NULL)
9444 if (max_sym_count != 0)
9446 amt = max_sym_count * bed->s->sizeof_sym;
9447 finfo.external_syms = bfd_malloc (amt);
9448 if (finfo.external_syms == NULL)
9451 amt = max_sym_count * sizeof (Elf_Internal_Sym);
9452 finfo.internal_syms = bfd_malloc (amt);
9453 if (finfo.internal_syms == NULL)
9456 amt = max_sym_count * sizeof (long);
9457 finfo.indices = bfd_malloc (amt);
9458 if (finfo.indices == NULL)
9461 amt = max_sym_count * sizeof (asection *);
9462 finfo.sections = bfd_malloc (amt);
9463 if (finfo.sections == NULL)
9467 if (max_sym_shndx_count != 0)
9469 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
9470 finfo.locsym_shndx = bfd_malloc (amt);
9471 if (finfo.locsym_shndx == NULL)
9475 if (elf_hash_table (info)->tls_sec)
9477 bfd_vma base, end = 0;
9480 for (sec = elf_hash_table (info)->tls_sec;
9481 sec && (sec->flags & SEC_THREAD_LOCAL);
9484 bfd_size_type size = sec->size;
9487 && (sec->flags & SEC_HAS_CONTENTS) == 0)
9489 struct bfd_link_order *o = sec->map_tail.link_order;
9491 size = o->offset + o->size;
9493 end = sec->vma + size;
9495 base = elf_hash_table (info)->tls_sec->vma;
9496 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
9497 elf_hash_table (info)->tls_size = end - base;
9500 /* Reorder SHF_LINK_ORDER sections. */
9501 for (o = abfd->sections; o != NULL; o = o->next)
9503 if (!elf_fixup_link_order (abfd, o))
9507 /* Since ELF permits relocations to be against local symbols, we
9508 must have the local symbols available when we do the relocations.
9509 Since we would rather only read the local symbols once, and we
9510 would rather not keep them in memory, we handle all the
9511 relocations for a single input file at the same time.
9513 Unfortunately, there is no way to know the total number of local
9514 symbols until we have seen all of them, and the local symbol
9515 indices precede the global symbol indices. This means that when
9516 we are generating relocatable output, and we see a reloc against
9517 a global symbol, we can not know the symbol index until we have
9518 finished examining all the local symbols to see which ones we are
9519 going to output. To deal with this, we keep the relocations in
9520 memory, and don't output them until the end of the link. This is
9521 an unfortunate waste of memory, but I don't see a good way around
9522 it. Fortunately, it only happens when performing a relocatable
9523 link, which is not the common case. FIXME: If keep_memory is set
9524 we could write the relocs out and then read them again; I don't
9525 know how bad the memory loss will be. */
9527 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9528 sub->output_has_begun = FALSE;
9529 for (o = abfd->sections; o != NULL; o = o->next)
9531 for (p = o->map_head.link_order; p != NULL; p = p->next)
9533 if (p->type == bfd_indirect_link_order
9534 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
9535 == bfd_target_elf_flavour)
9536 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
9538 if (! sub->output_has_begun)
9540 if (! elf_link_input_bfd (&finfo, sub))
9542 sub->output_has_begun = TRUE;
9545 else if (p->type == bfd_section_reloc_link_order
9546 || p->type == bfd_symbol_reloc_link_order)
9548 if (! elf_reloc_link_order (abfd, info, o, p))
9553 if (! _bfd_default_link_order (abfd, info, o, p))
9559 /* Free symbol buffer if needed. */
9560 if (!info->reduce_memory_overheads)
9562 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9563 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
9564 && elf_tdata (sub)->symbuf)
9566 free (elf_tdata (sub)->symbuf);
9567 elf_tdata (sub)->symbuf = NULL;
9571 /* Output any global symbols that got converted to local in a
9572 version script or due to symbol visibility. We do this in a
9573 separate step since ELF requires all local symbols to appear
9574 prior to any global symbols. FIXME: We should only do this if
9575 some global symbols were, in fact, converted to become local.
9576 FIXME: Will this work correctly with the Irix 5 linker? */
9577 eoinfo.failed = FALSE;
9578 eoinfo.finfo = &finfo;
9579 eoinfo.localsyms = TRUE;
9580 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
9585 /* If backend needs to output some local symbols not present in the hash
9586 table, do it now. */
9587 if (bed->elf_backend_output_arch_local_syms)
9589 typedef bfd_boolean (*out_sym_func)
9590 (void *, const char *, Elf_Internal_Sym *, asection *,
9591 struct elf_link_hash_entry *);
9593 if (! ((*bed->elf_backend_output_arch_local_syms)
9594 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
9598 /* That wrote out all the local symbols. Finish up the symbol table
9599 with the global symbols. Even if we want to strip everything we
9600 can, we still need to deal with those global symbols that got
9601 converted to local in a version script. */
9603 /* The sh_info field records the index of the first non local symbol. */
9604 symtab_hdr->sh_info = bfd_get_symcount (abfd);
9607 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
9609 Elf_Internal_Sym sym;
9610 bfd_byte *dynsym = finfo.dynsym_sec->contents;
9611 long last_local = 0;
9613 /* Write out the section symbols for the output sections. */
9614 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
9620 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
9623 for (s = abfd->sections; s != NULL; s = s->next)
9629 dynindx = elf_section_data (s)->dynindx;
9632 indx = elf_section_data (s)->this_idx;
9633 BFD_ASSERT (indx > 0);
9634 sym.st_shndx = indx;
9635 if (! check_dynsym (abfd, &sym))
9637 sym.st_value = s->vma;
9638 dest = dynsym + dynindx * bed->s->sizeof_sym;
9639 if (last_local < dynindx)
9640 last_local = dynindx;
9641 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
9645 /* Write out the local dynsyms. */
9646 if (elf_hash_table (info)->dynlocal)
9648 struct elf_link_local_dynamic_entry *e;
9649 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
9654 sym.st_size = e->isym.st_size;
9655 sym.st_other = e->isym.st_other;
9657 /* Copy the internal symbol as is.
9658 Note that we saved a word of storage and overwrote
9659 the original st_name with the dynstr_index. */
9662 if (e->isym.st_shndx != SHN_UNDEF
9663 && (e->isym.st_shndx < SHN_LORESERVE
9664 || e->isym.st_shndx > SHN_HIRESERVE))
9666 s = bfd_section_from_elf_index (e->input_bfd,
9670 elf_section_data (s->output_section)->this_idx;
9671 if (! check_dynsym (abfd, &sym))
9673 sym.st_value = (s->output_section->vma
9675 + e->isym.st_value);
9678 if (last_local < e->dynindx)
9679 last_local = e->dynindx;
9681 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
9682 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
9686 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
9690 /* We get the global symbols from the hash table. */
9691 eoinfo.failed = FALSE;
9692 eoinfo.localsyms = FALSE;
9693 eoinfo.finfo = &finfo;
9694 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
9699 /* If backend needs to output some symbols not present in the hash
9700 table, do it now. */
9701 if (bed->elf_backend_output_arch_syms)
9703 typedef bfd_boolean (*out_sym_func)
9704 (void *, const char *, Elf_Internal_Sym *, asection *,
9705 struct elf_link_hash_entry *);
9707 if (! ((*bed->elf_backend_output_arch_syms)
9708 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
9712 /* Flush all symbols to the file. */
9713 if (! elf_link_flush_output_syms (&finfo, bed))
9716 /* Now we know the size of the symtab section. */
9717 off += symtab_hdr->sh_size;
9719 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
9720 if (symtab_shndx_hdr->sh_name != 0)
9722 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
9723 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
9724 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
9725 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
9726 symtab_shndx_hdr->sh_size = amt;
9728 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
9731 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
9732 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
9737 /* Finish up and write out the symbol string table (.strtab)
9739 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
9740 /* sh_name was set in prep_headers. */
9741 symstrtab_hdr->sh_type = SHT_STRTAB;
9742 symstrtab_hdr->sh_flags = 0;
9743 symstrtab_hdr->sh_addr = 0;
9744 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
9745 symstrtab_hdr->sh_entsize = 0;
9746 symstrtab_hdr->sh_link = 0;
9747 symstrtab_hdr->sh_info = 0;
9748 /* sh_offset is set just below. */
9749 symstrtab_hdr->sh_addralign = 1;
9751 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
9752 elf_tdata (abfd)->next_file_pos = off;
9754 if (bfd_get_symcount (abfd) > 0)
9756 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
9757 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
9761 /* Adjust the relocs to have the correct symbol indices. */
9762 for (o = abfd->sections; o != NULL; o = o->next)
9764 if ((o->flags & SEC_RELOC) == 0)
9767 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
9768 elf_section_data (o)->rel_count,
9769 elf_section_data (o)->rel_hashes);
9770 if (elf_section_data (o)->rel_hdr2 != NULL)
9771 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
9772 elf_section_data (o)->rel_count2,
9773 (elf_section_data (o)->rel_hashes
9774 + elf_section_data (o)->rel_count));
9776 /* Set the reloc_count field to 0 to prevent write_relocs from
9777 trying to swap the relocs out itself. */
9781 if (dynamic && info->combreloc && dynobj != NULL)
9782 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
9784 /* If we are linking against a dynamic object, or generating a
9785 shared library, finish up the dynamic linking information. */
9788 bfd_byte *dyncon, *dynconend;
9790 /* Fix up .dynamic entries. */
9791 o = bfd_get_section_by_name (dynobj, ".dynamic");
9792 BFD_ASSERT (o != NULL);
9794 dyncon = o->contents;
9795 dynconend = o->contents + o->size;
9796 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
9798 Elf_Internal_Dyn dyn;
9802 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
9809 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
9811 switch (elf_section_data (reldyn)->this_hdr.sh_type)
9813 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
9814 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
9817 dyn.d_un.d_val = relativecount;
9824 name = info->init_function;
9827 name = info->fini_function;
9830 struct elf_link_hash_entry *h;
9832 h = elf_link_hash_lookup (elf_hash_table (info), name,
9833 FALSE, FALSE, TRUE);
9835 && (h->root.type == bfd_link_hash_defined
9836 || h->root.type == bfd_link_hash_defweak))
9838 dyn.d_un.d_val = h->root.u.def.value;
9839 o = h->root.u.def.section;
9840 if (o->output_section != NULL)
9841 dyn.d_un.d_val += (o->output_section->vma
9842 + o->output_offset);
9845 /* The symbol is imported from another shared
9846 library and does not apply to this one. */
9854 case DT_PREINIT_ARRAYSZ:
9855 name = ".preinit_array";
9857 case DT_INIT_ARRAYSZ:
9858 name = ".init_array";
9860 case DT_FINI_ARRAYSZ:
9861 name = ".fini_array";
9863 o = bfd_get_section_by_name (abfd, name);
9866 (*_bfd_error_handler)
9867 (_("%B: could not find output section %s"), abfd, name);
9871 (*_bfd_error_handler)
9872 (_("warning: %s section has zero size"), name);
9873 dyn.d_un.d_val = o->size;
9876 case DT_PREINIT_ARRAY:
9877 name = ".preinit_array";
9880 name = ".init_array";
9883 name = ".fini_array";
9899 name = ".gnu.version_d";
9902 name = ".gnu.version_r";
9905 name = ".gnu.version";
9907 o = bfd_get_section_by_name (abfd, name);
9910 (*_bfd_error_handler)
9911 (_("%B: could not find output section %s"), abfd, name);
9914 dyn.d_un.d_ptr = o->vma;
9921 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
9926 for (i = 1; i < elf_numsections (abfd); i++)
9928 Elf_Internal_Shdr *hdr;
9930 hdr = elf_elfsections (abfd)[i];
9931 if (hdr->sh_type == type
9932 && (hdr->sh_flags & SHF_ALLOC) != 0)
9934 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
9935 dyn.d_un.d_val += hdr->sh_size;
9938 if (dyn.d_un.d_val == 0
9939 || hdr->sh_addr < dyn.d_un.d_val)
9940 dyn.d_un.d_val = hdr->sh_addr;
9946 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
9950 /* If we have created any dynamic sections, then output them. */
9953 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
9956 /* Check for DT_TEXTREL (late, in case the backend removes it). */
9957 if (info->warn_shared_textrel && info->shared)
9959 bfd_byte *dyncon, *dynconend;
9961 /* Fix up .dynamic entries. */
9962 o = bfd_get_section_by_name (dynobj, ".dynamic");
9963 BFD_ASSERT (o != NULL);
9965 dyncon = o->contents;
9966 dynconend = o->contents + o->size;
9967 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
9969 Elf_Internal_Dyn dyn;
9971 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
9973 if (dyn.d_tag == DT_TEXTREL)
9975 info->callbacks->einfo
9976 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
9982 for (o = dynobj->sections; o != NULL; o = o->next)
9984 if ((o->flags & SEC_HAS_CONTENTS) == 0
9986 || o->output_section == bfd_abs_section_ptr)
9988 if ((o->flags & SEC_LINKER_CREATED) == 0)
9990 /* At this point, we are only interested in sections
9991 created by _bfd_elf_link_create_dynamic_sections. */
9994 if (elf_hash_table (info)->stab_info.stabstr == o)
9996 if (elf_hash_table (info)->eh_info.hdr_sec == o)
9998 if ((elf_section_data (o->output_section)->this_hdr.sh_type
10000 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
10002 if (! bfd_set_section_contents (abfd, o->output_section,
10004 (file_ptr) o->output_offset,
10010 /* The contents of the .dynstr section are actually in a
10012 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
10013 if (bfd_seek (abfd, off, SEEK_SET) != 0
10014 || ! _bfd_elf_strtab_emit (abfd,
10015 elf_hash_table (info)->dynstr))
10021 if (info->relocatable)
10023 bfd_boolean failed = FALSE;
10025 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
10030 /* If we have optimized stabs strings, output them. */
10031 if (elf_hash_table (info)->stab_info.stabstr != NULL)
10033 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
10037 if (info->eh_frame_hdr)
10039 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
10043 if (finfo.symstrtab != NULL)
10044 _bfd_stringtab_free (finfo.symstrtab);
10045 if (finfo.contents != NULL)
10046 free (finfo.contents);
10047 if (finfo.external_relocs != NULL)
10048 free (finfo.external_relocs);
10049 if (finfo.internal_relocs != NULL)
10050 free (finfo.internal_relocs);
10051 if (finfo.external_syms != NULL)
10052 free (finfo.external_syms);
10053 if (finfo.locsym_shndx != NULL)
10054 free (finfo.locsym_shndx);
10055 if (finfo.internal_syms != NULL)
10056 free (finfo.internal_syms);
10057 if (finfo.indices != NULL)
10058 free (finfo.indices);
10059 if (finfo.sections != NULL)
10060 free (finfo.sections);
10061 if (finfo.symbuf != NULL)
10062 free (finfo.symbuf);
10063 if (finfo.symshndxbuf != NULL)
10064 free (finfo.symshndxbuf);
10065 for (o = abfd->sections; o != NULL; o = o->next)
10067 if ((o->flags & SEC_RELOC) != 0
10068 && elf_section_data (o)->rel_hashes != NULL)
10069 free (elf_section_data (o)->rel_hashes);
10072 elf_tdata (abfd)->linker = TRUE;
10077 if (finfo.symstrtab != NULL)
10078 _bfd_stringtab_free (finfo.symstrtab);
10079 if (finfo.contents != NULL)
10080 free (finfo.contents);
10081 if (finfo.external_relocs != NULL)
10082 free (finfo.external_relocs);
10083 if (finfo.internal_relocs != NULL)
10084 free (finfo.internal_relocs);
10085 if (finfo.external_syms != NULL)
10086 free (finfo.external_syms);
10087 if (finfo.locsym_shndx != NULL)
10088 free (finfo.locsym_shndx);
10089 if (finfo.internal_syms != NULL)
10090 free (finfo.internal_syms);
10091 if (finfo.indices != NULL)
10092 free (finfo.indices);
10093 if (finfo.sections != NULL)
10094 free (finfo.sections);
10095 if (finfo.symbuf != NULL)
10096 free (finfo.symbuf);
10097 if (finfo.symshndxbuf != NULL)
10098 free (finfo.symshndxbuf);
10099 for (o = abfd->sections; o != NULL; o = o->next)
10101 if ((o->flags & SEC_RELOC) != 0
10102 && elf_section_data (o)->rel_hashes != NULL)
10103 free (elf_section_data (o)->rel_hashes);
10109 /* Garbage collect unused sections. */
10111 /* Default gc_mark_hook. */
10114 _bfd_elf_gc_mark_hook (asection *sec,
10115 struct bfd_link_info *info ATTRIBUTE_UNUSED,
10116 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
10117 struct elf_link_hash_entry *h,
10118 Elf_Internal_Sym *sym)
10122 switch (h->root.type)
10124 case bfd_link_hash_defined:
10125 case bfd_link_hash_defweak:
10126 return h->root.u.def.section;
10128 case bfd_link_hash_common:
10129 return h->root.u.c.p->section;
10136 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
10141 /* The mark phase of garbage collection. For a given section, mark
10142 it and any sections in this section's group, and all the sections
10143 which define symbols to which it refers. */
10146 _bfd_elf_gc_mark (struct bfd_link_info *info,
10148 elf_gc_mark_hook_fn gc_mark_hook)
10152 asection *group_sec;
10156 /* Mark all the sections in the group. */
10157 group_sec = elf_section_data (sec)->next_in_group;
10158 if (group_sec && !group_sec->gc_mark)
10159 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
10162 /* Look through the section relocs. */
10164 is_eh = strcmp (sec->name, ".eh_frame") == 0;
10165 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
10167 Elf_Internal_Rela *relstart, *rel, *relend;
10168 Elf_Internal_Shdr *symtab_hdr;
10169 struct elf_link_hash_entry **sym_hashes;
10172 bfd *input_bfd = sec->owner;
10173 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
10174 Elf_Internal_Sym *isym = NULL;
10177 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10178 sym_hashes = elf_sym_hashes (input_bfd);
10180 /* Read the local symbols. */
10181 if (elf_bad_symtab (input_bfd))
10183 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
10187 extsymoff = nlocsyms = symtab_hdr->sh_info;
10189 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
10190 if (isym == NULL && nlocsyms != 0)
10192 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
10198 /* Read the relocations. */
10199 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
10200 info->keep_memory);
10201 if (relstart == NULL)
10206 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
10208 if (bed->s->arch_size == 32)
10213 for (rel = relstart; rel < relend; rel++)
10215 unsigned long r_symndx;
10217 struct elf_link_hash_entry *h;
10219 r_symndx = rel->r_info >> r_sym_shift;
10223 if (r_symndx >= nlocsyms
10224 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
10226 h = sym_hashes[r_symndx - extsymoff];
10227 while (h->root.type == bfd_link_hash_indirect
10228 || h->root.type == bfd_link_hash_warning)
10229 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10230 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
10234 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
10237 if (rsec && !rsec->gc_mark)
10239 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
10242 rsec->gc_mark_from_eh = 1;
10243 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
10252 if (elf_section_data (sec)->relocs != relstart)
10255 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
10257 if (! info->keep_memory)
10260 symtab_hdr->contents = (unsigned char *) isym;
10267 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
10269 struct elf_gc_sweep_symbol_info
10271 struct bfd_link_info *info;
10272 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
10277 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
10279 if (h->root.type == bfd_link_hash_warning)
10280 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10282 if ((h->root.type == bfd_link_hash_defined
10283 || h->root.type == bfd_link_hash_defweak)
10284 && !h->root.u.def.section->gc_mark
10285 && !(h->root.u.def.section->owner->flags & DYNAMIC))
10287 struct elf_gc_sweep_symbol_info *inf = data;
10288 (*inf->hide_symbol) (inf->info, h, TRUE);
10294 /* The sweep phase of garbage collection. Remove all garbage sections. */
10296 typedef bfd_boolean (*gc_sweep_hook_fn)
10297 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
10300 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
10303 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10304 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
10305 unsigned long section_sym_count;
10306 struct elf_gc_sweep_symbol_info sweep_info;
10308 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10312 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
10315 for (o = sub->sections; o != NULL; o = o->next)
10317 /* Keep debug and special sections. */
10318 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
10319 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
10325 /* Skip sweeping sections already excluded. */
10326 if (o->flags & SEC_EXCLUDE)
10329 /* Since this is early in the link process, it is simple
10330 to remove a section from the output. */
10331 o->flags |= SEC_EXCLUDE;
10333 if (info->print_gc_sections == TRUE)
10334 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
10336 /* But we also have to update some of the relocation
10337 info we collected before. */
10339 && (o->flags & SEC_RELOC) != 0
10340 && o->reloc_count > 0
10341 && !bfd_is_abs_section (o->output_section))
10343 Elf_Internal_Rela *internal_relocs;
10347 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
10348 info->keep_memory);
10349 if (internal_relocs == NULL)
10352 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
10354 if (elf_section_data (o)->relocs != internal_relocs)
10355 free (internal_relocs);
10363 /* Remove the symbols that were in the swept sections from the dynamic
10364 symbol table. GCFIXME: Anyone know how to get them out of the
10365 static symbol table as well? */
10366 sweep_info.info = info;
10367 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
10368 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
10371 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
10375 /* Propagate collected vtable information. This is called through
10376 elf_link_hash_traverse. */
10379 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
10381 if (h->root.type == bfd_link_hash_warning)
10382 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10384 /* Those that are not vtables. */
10385 if (h->vtable == NULL || h->vtable->parent == NULL)
10388 /* Those vtables that do not have parents, we cannot merge. */
10389 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
10392 /* If we've already been done, exit. */
10393 if (h->vtable->used && h->vtable->used[-1])
10396 /* Make sure the parent's table is up to date. */
10397 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
10399 if (h->vtable->used == NULL)
10401 /* None of this table's entries were referenced. Re-use the
10403 h->vtable->used = h->vtable->parent->vtable->used;
10404 h->vtable->size = h->vtable->parent->vtable->size;
10409 bfd_boolean *cu, *pu;
10411 /* Or the parent's entries into ours. */
10412 cu = h->vtable->used;
10414 pu = h->vtable->parent->vtable->used;
10417 const struct elf_backend_data *bed;
10418 unsigned int log_file_align;
10420 bed = get_elf_backend_data (h->root.u.def.section->owner);
10421 log_file_align = bed->s->log_file_align;
10422 n = h->vtable->parent->vtable->size >> log_file_align;
10437 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
10440 bfd_vma hstart, hend;
10441 Elf_Internal_Rela *relstart, *relend, *rel;
10442 const struct elf_backend_data *bed;
10443 unsigned int log_file_align;
10445 if (h->root.type == bfd_link_hash_warning)
10446 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10448 /* Take care of both those symbols that do not describe vtables as
10449 well as those that are not loaded. */
10450 if (h->vtable == NULL || h->vtable->parent == NULL)
10453 BFD_ASSERT (h->root.type == bfd_link_hash_defined
10454 || h->root.type == bfd_link_hash_defweak);
10456 sec = h->root.u.def.section;
10457 hstart = h->root.u.def.value;
10458 hend = hstart + h->size;
10460 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
10462 return *(bfd_boolean *) okp = FALSE;
10463 bed = get_elf_backend_data (sec->owner);
10464 log_file_align = bed->s->log_file_align;
10466 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
10468 for (rel = relstart; rel < relend; ++rel)
10469 if (rel->r_offset >= hstart && rel->r_offset < hend)
10471 /* If the entry is in use, do nothing. */
10472 if (h->vtable->used
10473 && (rel->r_offset - hstart) < h->vtable->size)
10475 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
10476 if (h->vtable->used[entry])
10479 /* Otherwise, kill it. */
10480 rel->r_offset = rel->r_info = rel->r_addend = 0;
10486 /* Mark sections containing dynamically referenced symbols. When
10487 building shared libraries, we must assume that any visible symbol is
10491 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
10493 struct bfd_link_info *info = (struct bfd_link_info *) inf;
10495 if (h->root.type == bfd_link_hash_warning)
10496 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10498 if ((h->root.type == bfd_link_hash_defined
10499 || h->root.type == bfd_link_hash_defweak)
10501 || (!info->executable
10503 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
10504 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
10505 h->root.u.def.section->flags |= SEC_KEEP;
10510 /* Do mark and sweep of unused sections. */
10513 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
10515 bfd_boolean ok = TRUE;
10517 elf_gc_mark_hook_fn gc_mark_hook;
10518 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10520 if (!bed->can_gc_sections
10521 || info->relocatable
10522 || info->emitrelocations
10523 || !is_elf_hash_table (info->hash))
10525 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
10529 /* Apply transitive closure to the vtable entry usage info. */
10530 elf_link_hash_traverse (elf_hash_table (info),
10531 elf_gc_propagate_vtable_entries_used,
10536 /* Kill the vtable relocations that were not used. */
10537 elf_link_hash_traverse (elf_hash_table (info),
10538 elf_gc_smash_unused_vtentry_relocs,
10543 /* Mark dynamically referenced symbols. */
10544 if (elf_hash_table (info)->dynamic_sections_created)
10545 elf_link_hash_traverse (elf_hash_table (info),
10546 bed->gc_mark_dynamic_ref,
10549 /* Grovel through relocs to find out who stays ... */
10550 gc_mark_hook = bed->gc_mark_hook;
10551 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10555 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
10558 for (o = sub->sections; o != NULL; o = o->next)
10559 if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark)
10560 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
10564 /* Allow the backend to mark additional target specific sections. */
10565 if (bed->gc_mark_extra_sections)
10566 bed->gc_mark_extra_sections(info, gc_mark_hook);
10568 /* ... again for sections marked from eh_frame. */
10569 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10573 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
10576 /* Keep .gcc_except_table.* if the associated .text.* (or the
10577 associated .gnu.linkonce.t.* if .text.* doesn't exist) is
10578 marked. This isn't very nice, but the proper solution,
10579 splitting .eh_frame up and using comdat doesn't pan out
10580 easily due to needing special relocs to handle the
10581 difference of two symbols in separate sections.
10582 Don't keep code sections referenced by .eh_frame. */
10583 #define TEXT_PREFIX ".text."
10584 #define TEXT_PREFIX2 ".gnu.linkonce.t."
10585 #define GCC_EXCEPT_TABLE_PREFIX ".gcc_except_table."
10586 for (o = sub->sections; o != NULL; o = o->next)
10587 if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)
10589 if (CONST_STRNEQ (o->name, GCC_EXCEPT_TABLE_PREFIX))
10592 const char *sec_name;
10594 unsigned o_name_prefix_len , fn_name_prefix_len, tmp;
10596 o_name_prefix_len = strlen (GCC_EXCEPT_TABLE_PREFIX);
10597 sec_name = o->name + o_name_prefix_len;
10598 fn_name_prefix_len = strlen (TEXT_PREFIX);
10599 tmp = strlen (TEXT_PREFIX2);
10600 if (tmp > fn_name_prefix_len)
10601 fn_name_prefix_len = tmp;
10603 = bfd_malloc (fn_name_prefix_len + strlen (sec_name) + 1);
10604 if (fn_name == NULL)
10607 /* Try the first prefix. */
10608 sprintf (fn_name, "%s%s", TEXT_PREFIX, sec_name);
10609 fn_text = bfd_get_section_by_name (sub, fn_name);
10611 /* Try the second prefix. */
10612 if (fn_text == NULL)
10614 sprintf (fn_name, "%s%s", TEXT_PREFIX2, sec_name);
10615 fn_text = bfd_get_section_by_name (sub, fn_name);
10619 if (fn_text == NULL || !fn_text->gc_mark)
10623 /* If not using specially named exception table section,
10624 then keep whatever we are using. */
10625 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
10630 /* ... and mark SEC_EXCLUDE for those that go. */
10631 return elf_gc_sweep (abfd, info);
10634 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
10637 bfd_elf_gc_record_vtinherit (bfd *abfd,
10639 struct elf_link_hash_entry *h,
10642 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
10643 struct elf_link_hash_entry **search, *child;
10644 bfd_size_type extsymcount;
10645 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10647 /* The sh_info field of the symtab header tells us where the
10648 external symbols start. We don't care about the local symbols at
10650 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
10651 if (!elf_bad_symtab (abfd))
10652 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
10654 sym_hashes = elf_sym_hashes (abfd);
10655 sym_hashes_end = sym_hashes + extsymcount;
10657 /* Hunt down the child symbol, which is in this section at the same
10658 offset as the relocation. */
10659 for (search = sym_hashes; search != sym_hashes_end; ++search)
10661 if ((child = *search) != NULL
10662 && (child->root.type == bfd_link_hash_defined
10663 || child->root.type == bfd_link_hash_defweak)
10664 && child->root.u.def.section == sec
10665 && child->root.u.def.value == offset)
10669 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
10670 abfd, sec, (unsigned long) offset);
10671 bfd_set_error (bfd_error_invalid_operation);
10675 if (!child->vtable)
10677 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
10678 if (!child->vtable)
10683 /* This *should* only be the absolute section. It could potentially
10684 be that someone has defined a non-global vtable though, which
10685 would be bad. It isn't worth paging in the local symbols to be
10686 sure though; that case should simply be handled by the assembler. */
10688 child->vtable->parent = (struct elf_link_hash_entry *) -1;
10691 child->vtable->parent = h;
10696 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
10699 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
10700 asection *sec ATTRIBUTE_UNUSED,
10701 struct elf_link_hash_entry *h,
10704 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10705 unsigned int log_file_align = bed->s->log_file_align;
10709 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
10714 if (addend >= h->vtable->size)
10716 size_t size, bytes, file_align;
10717 bfd_boolean *ptr = h->vtable->used;
10719 /* While the symbol is undefined, we have to be prepared to handle
10721 file_align = 1 << log_file_align;
10722 if (h->root.type == bfd_link_hash_undefined)
10723 size = addend + file_align;
10727 if (addend >= size)
10729 /* Oops! We've got a reference past the defined end of
10730 the table. This is probably a bug -- shall we warn? */
10731 size = addend + file_align;
10734 size = (size + file_align - 1) & -file_align;
10736 /* Allocate one extra entry for use as a "done" flag for the
10737 consolidation pass. */
10738 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
10742 ptr = bfd_realloc (ptr - 1, bytes);
10748 oldbytes = (((h->vtable->size >> log_file_align) + 1)
10749 * sizeof (bfd_boolean));
10750 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
10754 ptr = bfd_zmalloc (bytes);
10759 /* And arrange for that done flag to be at index -1. */
10760 h->vtable->used = ptr + 1;
10761 h->vtable->size = size;
10764 h->vtable->used[addend >> log_file_align] = TRUE;
10769 struct alloc_got_off_arg {
10771 unsigned int got_elt_size;
10774 /* We need a special top-level link routine to convert got reference counts
10775 to real got offsets. */
10778 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
10780 struct alloc_got_off_arg *gofarg = arg;
10782 if (h->root.type == bfd_link_hash_warning)
10783 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10785 if (h->got.refcount > 0)
10787 h->got.offset = gofarg->gotoff;
10788 gofarg->gotoff += gofarg->got_elt_size;
10791 h->got.offset = (bfd_vma) -1;
10796 /* And an accompanying bit to work out final got entry offsets once
10797 we're done. Should be called from final_link. */
10800 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
10801 struct bfd_link_info *info)
10804 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10806 unsigned int got_elt_size = bed->s->arch_size / 8;
10807 struct alloc_got_off_arg gofarg;
10809 if (! is_elf_hash_table (info->hash))
10812 /* The GOT offset is relative to the .got section, but the GOT header is
10813 put into the .got.plt section, if the backend uses it. */
10814 if (bed->want_got_plt)
10817 gotoff = bed->got_header_size;
10819 /* Do the local .got entries first. */
10820 for (i = info->input_bfds; i; i = i->link_next)
10822 bfd_signed_vma *local_got;
10823 bfd_size_type j, locsymcount;
10824 Elf_Internal_Shdr *symtab_hdr;
10826 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
10829 local_got = elf_local_got_refcounts (i);
10833 symtab_hdr = &elf_tdata (i)->symtab_hdr;
10834 if (elf_bad_symtab (i))
10835 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10837 locsymcount = symtab_hdr->sh_info;
10839 for (j = 0; j < locsymcount; ++j)
10841 if (local_got[j] > 0)
10843 local_got[j] = gotoff;
10844 gotoff += got_elt_size;
10847 local_got[j] = (bfd_vma) -1;
10851 /* Then the global .got entries. .plt refcounts are handled by
10852 adjust_dynamic_symbol */
10853 gofarg.gotoff = gotoff;
10854 gofarg.got_elt_size = got_elt_size;
10855 elf_link_hash_traverse (elf_hash_table (info),
10856 elf_gc_allocate_got_offsets,
10861 /* Many folk need no more in the way of final link than this, once
10862 got entry reference counting is enabled. */
10865 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
10867 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
10870 /* Invoke the regular ELF backend linker to do all the work. */
10871 return bfd_elf_final_link (abfd, info);
10875 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
10877 struct elf_reloc_cookie *rcookie = cookie;
10879 if (rcookie->bad_symtab)
10880 rcookie->rel = rcookie->rels;
10882 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
10884 unsigned long r_symndx;
10886 if (! rcookie->bad_symtab)
10887 if (rcookie->rel->r_offset > offset)
10889 if (rcookie->rel->r_offset != offset)
10892 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
10893 if (r_symndx == SHN_UNDEF)
10896 if (r_symndx >= rcookie->locsymcount
10897 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
10899 struct elf_link_hash_entry *h;
10901 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
10903 while (h->root.type == bfd_link_hash_indirect
10904 || h->root.type == bfd_link_hash_warning)
10905 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10907 if ((h->root.type == bfd_link_hash_defined
10908 || h->root.type == bfd_link_hash_defweak)
10909 && elf_discarded_section (h->root.u.def.section))
10916 /* It's not a relocation against a global symbol,
10917 but it could be a relocation against a local
10918 symbol for a discarded section. */
10920 Elf_Internal_Sym *isym;
10922 /* Need to: get the symbol; get the section. */
10923 isym = &rcookie->locsyms[r_symndx];
10924 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
10926 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
10927 if (isec != NULL && elf_discarded_section (isec))
10936 /* Discard unneeded references to discarded sections.
10937 Returns TRUE if any section's size was changed. */
10938 /* This function assumes that the relocations are in sorted order,
10939 which is true for all known assemblers. */
10942 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
10944 struct elf_reloc_cookie cookie;
10945 asection *stab, *eh;
10946 Elf_Internal_Shdr *symtab_hdr;
10947 const struct elf_backend_data *bed;
10949 unsigned int count;
10950 bfd_boolean ret = FALSE;
10952 if (info->traditional_format
10953 || !is_elf_hash_table (info->hash))
10956 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
10958 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
10961 bed = get_elf_backend_data (abfd);
10963 if ((abfd->flags & DYNAMIC) != 0)
10967 if (!info->relocatable)
10969 eh = bfd_get_section_by_name (abfd, ".eh_frame");
10972 || bfd_is_abs_section (eh->output_section)))
10976 stab = bfd_get_section_by_name (abfd, ".stab");
10978 && (stab->size == 0
10979 || bfd_is_abs_section (stab->output_section)
10980 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
10985 && bed->elf_backend_discard_info == NULL)
10988 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10989 cookie.abfd = abfd;
10990 cookie.sym_hashes = elf_sym_hashes (abfd);
10991 cookie.bad_symtab = elf_bad_symtab (abfd);
10992 if (cookie.bad_symtab)
10994 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10995 cookie.extsymoff = 0;
10999 cookie.locsymcount = symtab_hdr->sh_info;
11000 cookie.extsymoff = symtab_hdr->sh_info;
11003 if (bed->s->arch_size == 32)
11004 cookie.r_sym_shift = 8;
11006 cookie.r_sym_shift = 32;
11008 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11009 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
11011 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11012 cookie.locsymcount, 0,
11014 if (cookie.locsyms == NULL)
11020 cookie.rels = NULL;
11021 count = stab->reloc_count;
11023 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
11024 info->keep_memory);
11025 if (cookie.rels != NULL)
11027 cookie.rel = cookie.rels;
11028 cookie.relend = cookie.rels;
11029 cookie.relend += count * bed->s->int_rels_per_ext_rel;
11030 if (_bfd_discard_section_stabs (abfd, stab,
11031 elf_section_data (stab)->sec_info,
11032 bfd_elf_reloc_symbol_deleted_p,
11035 if (elf_section_data (stab)->relocs != cookie.rels)
11036 free (cookie.rels);
11042 cookie.rels = NULL;
11043 count = eh->reloc_count;
11045 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
11046 info->keep_memory);
11047 cookie.rel = cookie.rels;
11048 cookie.relend = cookie.rels;
11049 if (cookie.rels != NULL)
11050 cookie.relend += count * bed->s->int_rels_per_ext_rel;
11052 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
11053 bfd_elf_reloc_symbol_deleted_p,
11057 if (cookie.rels != NULL
11058 && elf_section_data (eh)->relocs != cookie.rels)
11059 free (cookie.rels);
11062 if (bed->elf_backend_discard_info != NULL
11063 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
11066 if (cookie.locsyms != NULL
11067 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
11069 if (! info->keep_memory)
11070 free (cookie.locsyms);
11072 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
11076 if (info->eh_frame_hdr
11077 && !info->relocatable
11078 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
11085 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section *sec,
11086 struct bfd_link_info *info)
11089 const char *name, *p;
11090 struct bfd_section_already_linked *l;
11091 struct bfd_section_already_linked_hash_entry *already_linked_list;
11093 if (sec->output_section == bfd_abs_section_ptr)
11096 flags = sec->flags;
11098 /* Return if it isn't a linkonce section. A comdat group section
11099 also has SEC_LINK_ONCE set. */
11100 if ((flags & SEC_LINK_ONCE) == 0)
11103 /* Don't put group member sections on our list of already linked
11104 sections. They are handled as a group via their group section. */
11105 if (elf_sec_group (sec) != NULL)
11108 /* FIXME: When doing a relocatable link, we may have trouble
11109 copying relocations in other sections that refer to local symbols
11110 in the section being discarded. Those relocations will have to
11111 be converted somehow; as of this writing I'm not sure that any of
11112 the backends handle that correctly.
11114 It is tempting to instead not discard link once sections when
11115 doing a relocatable link (technically, they should be discarded
11116 whenever we are building constructors). However, that fails,
11117 because the linker winds up combining all the link once sections
11118 into a single large link once section, which defeats the purpose
11119 of having link once sections in the first place.
11121 Also, not merging link once sections in a relocatable link
11122 causes trouble for MIPS ELF, which relies on link once semantics
11123 to handle the .reginfo section correctly. */
11125 name = bfd_get_section_name (abfd, sec);
11127 if (CONST_STRNEQ (name, ".gnu.linkonce.")
11128 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
11133 already_linked_list = bfd_section_already_linked_table_lookup (p);
11135 for (l = already_linked_list->entry; l != NULL; l = l->next)
11137 /* We may have 2 different types of sections on the list: group
11138 sections and linkonce sections. Match like sections. */
11139 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
11140 && strcmp (name, l->sec->name) == 0
11141 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
11143 /* The section has already been linked. See if we should
11144 issue a warning. */
11145 switch (flags & SEC_LINK_DUPLICATES)
11150 case SEC_LINK_DUPLICATES_DISCARD:
11153 case SEC_LINK_DUPLICATES_ONE_ONLY:
11154 (*_bfd_error_handler)
11155 (_("%B: ignoring duplicate section `%A'"),
11159 case SEC_LINK_DUPLICATES_SAME_SIZE:
11160 if (sec->size != l->sec->size)
11161 (*_bfd_error_handler)
11162 (_("%B: duplicate section `%A' has different size"),
11166 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
11167 if (sec->size != l->sec->size)
11168 (*_bfd_error_handler)
11169 (_("%B: duplicate section `%A' has different size"),
11171 else if (sec->size != 0)
11173 bfd_byte *sec_contents, *l_sec_contents;
11175 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
11176 (*_bfd_error_handler)
11177 (_("%B: warning: could not read contents of section `%A'"),
11179 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
11181 (*_bfd_error_handler)
11182 (_("%B: warning: could not read contents of section `%A'"),
11183 l->sec->owner, l->sec);
11184 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
11185 (*_bfd_error_handler)
11186 (_("%B: warning: duplicate section `%A' has different contents"),
11190 free (sec_contents);
11191 if (l_sec_contents)
11192 free (l_sec_contents);
11197 /* Set the output_section field so that lang_add_section
11198 does not create a lang_input_section structure for this
11199 section. Since there might be a symbol in the section
11200 being discarded, we must retain a pointer to the section
11201 which we are really going to use. */
11202 sec->output_section = bfd_abs_section_ptr;
11203 sec->kept_section = l->sec;
11205 if (flags & SEC_GROUP)
11207 asection *first = elf_next_in_group (sec);
11208 asection *s = first;
11212 s->output_section = bfd_abs_section_ptr;
11213 /* Record which group discards it. */
11214 s->kept_section = l->sec;
11215 s = elf_next_in_group (s);
11216 /* These lists are circular. */
11226 /* A single member comdat group section may be discarded by a
11227 linkonce section and vice versa. */
11229 if ((flags & SEC_GROUP) != 0)
11231 asection *first = elf_next_in_group (sec);
11233 if (first != NULL && elf_next_in_group (first) == first)
11234 /* Check this single member group against linkonce sections. */
11235 for (l = already_linked_list->entry; l != NULL; l = l->next)
11236 if ((l->sec->flags & SEC_GROUP) == 0
11237 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
11238 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
11240 first->output_section = bfd_abs_section_ptr;
11241 first->kept_section = l->sec;
11242 sec->output_section = bfd_abs_section_ptr;
11247 /* Check this linkonce section against single member groups. */
11248 for (l = already_linked_list->entry; l != NULL; l = l->next)
11249 if (l->sec->flags & SEC_GROUP)
11251 asection *first = elf_next_in_group (l->sec);
11254 && elf_next_in_group (first) == first
11255 && bfd_elf_match_symbols_in_sections (first, sec, info))
11257 sec->output_section = bfd_abs_section_ptr;
11258 sec->kept_section = first;
11263 /* This is the first section with this name. Record it. */
11264 bfd_section_already_linked_table_insert (already_linked_list, sec);
11268 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
11270 return sym->st_shndx == SHN_COMMON;
11274 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
11280 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
11282 return bfd_com_section_ptr;