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 bed = get_elf_backend_data (abfd);
863 /* This code is for coping with dynamic objects, and is only useful
864 if we are doing an ELF link. */
865 if (!(*bed->relocs_compatible) (abfd->xvec, info->hash->creator))
868 /* For merging, we only care about real symbols. */
870 while (h->root.type == bfd_link_hash_indirect
871 || h->root.type == bfd_link_hash_warning)
872 h = (struct elf_link_hash_entry *) h->root.u.i.link;
874 /* We have to check it for every instance since the first few may be
875 refereences and not all compilers emit symbol type for undefined
877 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
879 /* If we just created the symbol, mark it as being an ELF symbol.
880 Other than that, there is nothing to do--there is no merge issue
881 with a newly defined symbol--so we just return. */
883 if (h->root.type == bfd_link_hash_new)
889 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
892 switch (h->root.type)
899 case bfd_link_hash_undefined:
900 case bfd_link_hash_undefweak:
901 oldbfd = h->root.u.undef.abfd;
905 case bfd_link_hash_defined:
906 case bfd_link_hash_defweak:
907 oldbfd = h->root.u.def.section->owner;
908 oldsec = h->root.u.def.section;
911 case bfd_link_hash_common:
912 oldbfd = h->root.u.c.p->section->owner;
913 oldsec = h->root.u.c.p->section;
917 /* In cases involving weak versioned symbols, we may wind up trying
918 to merge a symbol with itself. Catch that here, to avoid the
919 confusion that results if we try to override a symbol with
920 itself. The additional tests catch cases like
921 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
922 dynamic object, which we do want to handle here. */
924 && ((abfd->flags & DYNAMIC) == 0
928 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
929 respectively, is from a dynamic object. */
931 newdyn = (abfd->flags & DYNAMIC) != 0;
935 olddyn = (oldbfd->flags & DYNAMIC) != 0;
936 else if (oldsec != NULL)
938 /* This handles the special SHN_MIPS_{TEXT,DATA} section
939 indices used by MIPS ELF. */
940 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
943 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
944 respectively, appear to be a definition rather than reference. */
946 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
948 olddef = (h->root.type != bfd_link_hash_undefined
949 && h->root.type != bfd_link_hash_undefweak
950 && h->root.type != bfd_link_hash_common);
952 /* When we try to create a default indirect symbol from the dynamic
953 definition with the default version, we skip it if its type and
954 the type of existing regular definition mismatch. We only do it
955 if the existing regular definition won't be dynamic. */
956 if (pold_alignment == NULL
958 && !info->export_dynamic
963 && (olddef || h->root.type == bfd_link_hash_common)
964 && ELF_ST_TYPE (sym->st_info) != h->type
965 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
966 && h->type != STT_NOTYPE
967 && !(bed->is_function_type (ELF_ST_TYPE (sym->st_info))
968 && bed->is_function_type (h->type)))
974 /* Check TLS symbol. We don't check undefined symbol introduced by
976 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
977 && ELF_ST_TYPE (sym->st_info) != h->type
981 bfd_boolean ntdef, tdef;
982 asection *ntsec, *tsec;
984 if (h->type == STT_TLS)
1004 (*_bfd_error_handler)
1005 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1006 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1007 else if (!tdef && !ntdef)
1008 (*_bfd_error_handler)
1009 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1010 tbfd, ntbfd, h->root.root.string);
1012 (*_bfd_error_handler)
1013 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1014 tbfd, tsec, ntbfd, h->root.root.string);
1016 (*_bfd_error_handler)
1017 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1018 tbfd, ntbfd, ntsec, h->root.root.string);
1020 bfd_set_error (bfd_error_bad_value);
1024 /* We need to remember if a symbol has a definition in a dynamic
1025 object or is weak in all dynamic objects. Internal and hidden
1026 visibility will make it unavailable to dynamic objects. */
1027 if (newdyn && !h->dynamic_def)
1029 if (!bfd_is_und_section (sec))
1033 /* Check if this symbol is weak in all dynamic objects. If it
1034 is the first time we see it in a dynamic object, we mark
1035 if it is weak. Otherwise, we clear it. */
1036 if (!h->ref_dynamic)
1038 if (bind == STB_WEAK)
1039 h->dynamic_weak = 1;
1041 else if (bind != STB_WEAK)
1042 h->dynamic_weak = 0;
1046 /* If the old symbol has non-default visibility, we ignore the new
1047 definition from a dynamic object. */
1049 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1050 && !bfd_is_und_section (sec))
1053 /* Make sure this symbol is dynamic. */
1055 /* A protected symbol has external availability. Make sure it is
1056 recorded as dynamic.
1058 FIXME: Should we check type and size for protected symbol? */
1059 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1060 return bfd_elf_link_record_dynamic_symbol (info, h);
1065 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1068 /* If the new symbol with non-default visibility comes from a
1069 relocatable file and the old definition comes from a dynamic
1070 object, we remove the old definition. */
1071 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1073 /* Handle the case where the old dynamic definition is
1074 default versioned. We need to copy the symbol info from
1075 the symbol with default version to the normal one if it
1076 was referenced before. */
1079 const struct elf_backend_data *bed
1080 = get_elf_backend_data (abfd);
1081 struct elf_link_hash_entry *vh = *sym_hash;
1082 vh->root.type = h->root.type;
1083 h->root.type = bfd_link_hash_indirect;
1084 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1085 /* Protected symbols will override the dynamic definition
1086 with default version. */
1087 if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1089 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1090 vh->dynamic_def = 1;
1091 vh->ref_dynamic = 1;
1095 h->root.type = vh->root.type;
1096 vh->ref_dynamic = 0;
1097 /* We have to hide it here since it was made dynamic
1098 global with extra bits when the symbol info was
1099 copied from the old dynamic definition. */
1100 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1108 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1109 && bfd_is_und_section (sec))
1111 /* If the new symbol is undefined and the old symbol was
1112 also undefined before, we need to make sure
1113 _bfd_generic_link_add_one_symbol doesn't mess
1114 up the linker hash table undefs list. Since the old
1115 definition came from a dynamic object, it is still on the
1117 h->root.type = bfd_link_hash_undefined;
1118 h->root.u.undef.abfd = abfd;
1122 h->root.type = bfd_link_hash_new;
1123 h->root.u.undef.abfd = NULL;
1132 /* FIXME: Should we check type and size for protected symbol? */
1138 /* Differentiate strong and weak symbols. */
1139 newweak = bind == STB_WEAK;
1140 oldweak = (h->root.type == bfd_link_hash_defweak
1141 || h->root.type == bfd_link_hash_undefweak);
1143 /* If a new weak symbol definition comes from a regular file and the
1144 old symbol comes from a dynamic library, we treat the new one as
1145 strong. Similarly, an old weak symbol definition from a regular
1146 file is treated as strong when the new symbol comes from a dynamic
1147 library. Further, an old weak symbol from a dynamic library is
1148 treated as strong if the new symbol is from a dynamic library.
1149 This reflects the way glibc's ld.so works.
1151 Do this before setting *type_change_ok or *size_change_ok so that
1152 we warn properly when dynamic library symbols are overridden. */
1154 if (newdef && !newdyn && olddyn)
1156 if (olddef && newdyn)
1159 /* Allow changes between different types of funciton symbol. */
1160 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))
1161 && bed->is_function_type (h->type))
1162 *type_change_ok = TRUE;
1164 /* It's OK to change the type if either the existing symbol or the
1165 new symbol is weak. A type change is also OK if the old symbol
1166 is undefined and the new symbol is defined. */
1171 && h->root.type == bfd_link_hash_undefined))
1172 *type_change_ok = TRUE;
1174 /* It's OK to change the size if either the existing symbol or the
1175 new symbol is weak, or if the old symbol is undefined. */
1178 || h->root.type == bfd_link_hash_undefined)
1179 *size_change_ok = TRUE;
1181 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1182 symbol, respectively, appears to be a common symbol in a dynamic
1183 object. If a symbol appears in an uninitialized section, and is
1184 not weak, and is not a function, then it may be a common symbol
1185 which was resolved when the dynamic object was created. We want
1186 to treat such symbols specially, because they raise special
1187 considerations when setting the symbol size: if the symbol
1188 appears as a common symbol in a regular object, and the size in
1189 the regular object is larger, we must make sure that we use the
1190 larger size. This problematic case can always be avoided in C,
1191 but it must be handled correctly when using Fortran shared
1194 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1195 likewise for OLDDYNCOMMON and OLDDEF.
1197 Note that this test is just a heuristic, and that it is quite
1198 possible to have an uninitialized symbol in a shared object which
1199 is really a definition, rather than a common symbol. This could
1200 lead to some minor confusion when the symbol really is a common
1201 symbol in some regular object. However, I think it will be
1207 && (sec->flags & SEC_ALLOC) != 0
1208 && (sec->flags & SEC_LOAD) == 0
1210 && !bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
1211 newdyncommon = TRUE;
1213 newdyncommon = FALSE;
1217 && h->root.type == bfd_link_hash_defined
1219 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1220 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1222 && !bed->is_function_type (h->type))
1223 olddyncommon = TRUE;
1225 olddyncommon = FALSE;
1227 /* We now know everything about the old and new symbols. We ask the
1228 backend to check if we can merge them. */
1229 if (bed->merge_symbol
1230 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1231 pold_alignment, skip, override,
1232 type_change_ok, size_change_ok,
1233 &newdyn, &newdef, &newdyncommon, &newweak,
1235 &olddyn, &olddef, &olddyncommon, &oldweak,
1239 /* If both the old and the new symbols look like common symbols in a
1240 dynamic object, set the size of the symbol to the larger of the
1245 && sym->st_size != h->size)
1247 /* Since we think we have two common symbols, issue a multiple
1248 common warning if desired. Note that we only warn if the
1249 size is different. If the size is the same, we simply let
1250 the old symbol override the new one as normally happens with
1251 symbols defined in dynamic objects. */
1253 if (! ((*info->callbacks->multiple_common)
1254 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1255 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1258 if (sym->st_size > h->size)
1259 h->size = sym->st_size;
1261 *size_change_ok = TRUE;
1264 /* If we are looking at a dynamic object, and we have found a
1265 definition, we need to see if the symbol was already defined by
1266 some other object. If so, we want to use the existing
1267 definition, and we do not want to report a multiple symbol
1268 definition error; we do this by clobbering *PSEC to be
1269 bfd_und_section_ptr.
1271 We treat a common symbol as a definition if the symbol in the
1272 shared library is a function, since common symbols always
1273 represent variables; this can cause confusion in principle, but
1274 any such confusion would seem to indicate an erroneous program or
1275 shared library. We also permit a common symbol in a regular
1276 object to override a weak symbol in a shared object. */
1281 || (h->root.type == bfd_link_hash_common
1283 || bed->is_function_type (ELF_ST_TYPE (sym->st_info))))))
1287 newdyncommon = FALSE;
1289 *psec = sec = bfd_und_section_ptr;
1290 *size_change_ok = TRUE;
1292 /* If we get here when the old symbol is a common symbol, then
1293 we are explicitly letting it override a weak symbol or
1294 function in a dynamic object, and we don't want to warn about
1295 a type change. If the old symbol is a defined symbol, a type
1296 change warning may still be appropriate. */
1298 if (h->root.type == bfd_link_hash_common)
1299 *type_change_ok = TRUE;
1302 /* Handle the special case of an old common symbol merging with a
1303 new symbol which looks like a common symbol in a shared object.
1304 We change *PSEC and *PVALUE to make the new symbol look like a
1305 common symbol, and let _bfd_generic_link_add_one_symbol do the
1309 && h->root.type == bfd_link_hash_common)
1313 newdyncommon = FALSE;
1314 *pvalue = sym->st_size;
1315 *psec = sec = bed->common_section (oldsec);
1316 *size_change_ok = TRUE;
1319 /* Skip weak definitions of symbols that are already defined. */
1320 if (newdef && olddef && newweak)
1323 /* If the old symbol is from a dynamic object, and the new symbol is
1324 a definition which is not from a dynamic object, then the new
1325 symbol overrides the old symbol. Symbols from regular files
1326 always take precedence over symbols from dynamic objects, even if
1327 they are defined after the dynamic object in the link.
1329 As above, we again permit a common symbol in a regular object to
1330 override a definition in a shared object if the shared object
1331 symbol is a function or is weak. */
1336 || (bfd_is_com_section (sec)
1338 || bed->is_function_type (h->type))))
1343 /* Change the hash table entry to undefined, and let
1344 _bfd_generic_link_add_one_symbol do the right thing with the
1347 h->root.type = bfd_link_hash_undefined;
1348 h->root.u.undef.abfd = h->root.u.def.section->owner;
1349 *size_change_ok = TRUE;
1352 olddyncommon = FALSE;
1354 /* We again permit a type change when a common symbol may be
1355 overriding a function. */
1357 if (bfd_is_com_section (sec))
1358 *type_change_ok = TRUE;
1360 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1363 /* This union may have been set to be non-NULL when this symbol
1364 was seen in a dynamic object. We must force the union to be
1365 NULL, so that it is correct for a regular symbol. */
1366 h->verinfo.vertree = NULL;
1369 /* Handle the special case of a new common symbol merging with an
1370 old symbol that looks like it might be a common symbol defined in
1371 a shared object. Note that we have already handled the case in
1372 which a new common symbol should simply override the definition
1373 in the shared library. */
1376 && bfd_is_com_section (sec)
1379 /* It would be best if we could set the hash table entry to a
1380 common symbol, but we don't know what to use for the section
1381 or the alignment. */
1382 if (! ((*info->callbacks->multiple_common)
1383 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1384 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1387 /* If the presumed common symbol in the dynamic object is
1388 larger, pretend that the new symbol has its size. */
1390 if (h->size > *pvalue)
1393 /* We need to remember the alignment required by the symbol
1394 in the dynamic object. */
1395 BFD_ASSERT (pold_alignment);
1396 *pold_alignment = h->root.u.def.section->alignment_power;
1399 olddyncommon = FALSE;
1401 h->root.type = bfd_link_hash_undefined;
1402 h->root.u.undef.abfd = h->root.u.def.section->owner;
1404 *size_change_ok = TRUE;
1405 *type_change_ok = TRUE;
1407 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1410 h->verinfo.vertree = NULL;
1415 /* Handle the case where we had a versioned symbol in a dynamic
1416 library and now find a definition in a normal object. In this
1417 case, we make the versioned symbol point to the normal one. */
1418 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1419 flip->root.type = h->root.type;
1420 h->root.type = bfd_link_hash_indirect;
1421 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1422 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1423 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1427 flip->ref_dynamic = 1;
1434 /* This function is called to create an indirect symbol from the
1435 default for the symbol with the default version if needed. The
1436 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1437 set DYNSYM if the new indirect symbol is dynamic. */
1440 _bfd_elf_add_default_symbol (bfd *abfd,
1441 struct bfd_link_info *info,
1442 struct elf_link_hash_entry *h,
1444 Elf_Internal_Sym *sym,
1447 bfd_boolean *dynsym,
1448 bfd_boolean override)
1450 bfd_boolean type_change_ok;
1451 bfd_boolean size_change_ok;
1454 struct elf_link_hash_entry *hi;
1455 struct bfd_link_hash_entry *bh;
1456 const struct elf_backend_data *bed;
1457 bfd_boolean collect;
1458 bfd_boolean dynamic;
1460 size_t len, shortlen;
1463 /* If this symbol has a version, and it is the default version, we
1464 create an indirect symbol from the default name to the fully
1465 decorated name. This will cause external references which do not
1466 specify a version to be bound to this version of the symbol. */
1467 p = strchr (name, ELF_VER_CHR);
1468 if (p == NULL || p[1] != ELF_VER_CHR)
1473 /* We are overridden by an old definition. We need to check if we
1474 need to create the indirect symbol from the default name. */
1475 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1477 BFD_ASSERT (hi != NULL);
1480 while (hi->root.type == bfd_link_hash_indirect
1481 || hi->root.type == bfd_link_hash_warning)
1483 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1489 bed = get_elf_backend_data (abfd);
1490 collect = bed->collect;
1491 dynamic = (abfd->flags & DYNAMIC) != 0;
1493 shortlen = p - name;
1494 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1495 if (shortname == NULL)
1497 memcpy (shortname, name, shortlen);
1498 shortname[shortlen] = '\0';
1500 /* We are going to create a new symbol. Merge it with any existing
1501 symbol with this name. For the purposes of the merge, act as
1502 though we were defining the symbol we just defined, although we
1503 actually going to define an indirect symbol. */
1504 type_change_ok = FALSE;
1505 size_change_ok = FALSE;
1507 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1508 NULL, &hi, &skip, &override,
1509 &type_change_ok, &size_change_ok))
1518 if (! (_bfd_generic_link_add_one_symbol
1519 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1520 0, name, FALSE, collect, &bh)))
1522 hi = (struct elf_link_hash_entry *) bh;
1526 /* In this case the symbol named SHORTNAME is overriding the
1527 indirect symbol we want to add. We were planning on making
1528 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1529 is the name without a version. NAME is the fully versioned
1530 name, and it is the default version.
1532 Overriding means that we already saw a definition for the
1533 symbol SHORTNAME in a regular object, and it is overriding
1534 the symbol defined in the dynamic object.
1536 When this happens, we actually want to change NAME, the
1537 symbol we just added, to refer to SHORTNAME. This will cause
1538 references to NAME in the shared object to become references
1539 to SHORTNAME in the regular object. This is what we expect
1540 when we override a function in a shared object: that the
1541 references in the shared object will be mapped to the
1542 definition in the regular object. */
1544 while (hi->root.type == bfd_link_hash_indirect
1545 || hi->root.type == bfd_link_hash_warning)
1546 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1548 h->root.type = bfd_link_hash_indirect;
1549 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1553 hi->ref_dynamic = 1;
1557 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1562 /* Now set HI to H, so that the following code will set the
1563 other fields correctly. */
1567 /* Check if HI is a warning symbol. */
1568 if (hi->root.type == bfd_link_hash_warning)
1569 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1571 /* If there is a duplicate definition somewhere, then HI may not
1572 point to an indirect symbol. We will have reported an error to
1573 the user in that case. */
1575 if (hi->root.type == bfd_link_hash_indirect)
1577 struct elf_link_hash_entry *ht;
1579 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1580 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1582 /* See if the new flags lead us to realize that the symbol must
1594 if (hi->ref_regular)
1600 /* We also need to define an indirection from the nondefault version
1604 len = strlen (name);
1605 shortname = bfd_hash_allocate (&info->hash->table, len);
1606 if (shortname == NULL)
1608 memcpy (shortname, name, shortlen);
1609 memcpy (shortname + shortlen, p + 1, len - shortlen);
1611 /* Once again, merge with any existing symbol. */
1612 type_change_ok = FALSE;
1613 size_change_ok = FALSE;
1615 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1616 NULL, &hi, &skip, &override,
1617 &type_change_ok, &size_change_ok))
1625 /* Here SHORTNAME is a versioned name, so we don't expect to see
1626 the type of override we do in the case above unless it is
1627 overridden by a versioned definition. */
1628 if (hi->root.type != bfd_link_hash_defined
1629 && hi->root.type != bfd_link_hash_defweak)
1630 (*_bfd_error_handler)
1631 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1637 if (! (_bfd_generic_link_add_one_symbol
1638 (info, abfd, shortname, BSF_INDIRECT,
1639 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1641 hi = (struct elf_link_hash_entry *) bh;
1643 /* If there is a duplicate definition somewhere, then HI may not
1644 point to an indirect symbol. We will have reported an error
1645 to the user in that case. */
1647 if (hi->root.type == bfd_link_hash_indirect)
1649 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1651 /* See if the new flags lead us to realize that the symbol
1663 if (hi->ref_regular)
1673 /* This routine is used to export all defined symbols into the dynamic
1674 symbol table. It is called via elf_link_hash_traverse. */
1677 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1679 struct elf_info_failed *eif = data;
1681 /* Ignore this if we won't export it. */
1682 if (!eif->info->export_dynamic && !h->dynamic)
1685 /* Ignore indirect symbols. These are added by the versioning code. */
1686 if (h->root.type == bfd_link_hash_indirect)
1689 if (h->root.type == bfd_link_hash_warning)
1690 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1692 if (h->dynindx == -1
1696 struct bfd_elf_version_tree *t;
1697 struct bfd_elf_version_expr *d;
1699 for (t = eif->verdefs; t != NULL; t = t->next)
1701 if (t->globals.list != NULL)
1703 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1708 if (t->locals.list != NULL)
1710 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1719 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1730 /* Look through the symbols which are defined in other shared
1731 libraries and referenced here. Update the list of version
1732 dependencies. This will be put into the .gnu.version_r section.
1733 This function is called via elf_link_hash_traverse. */
1736 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1739 struct elf_find_verdep_info *rinfo = data;
1740 Elf_Internal_Verneed *t;
1741 Elf_Internal_Vernaux *a;
1744 if (h->root.type == bfd_link_hash_warning)
1745 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1747 /* We only care about symbols defined in shared objects with version
1752 || h->verinfo.verdef == NULL)
1755 /* See if we already know about this version. */
1756 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1758 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1761 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1762 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1768 /* This is a new version. Add it to tree we are building. */
1773 t = bfd_zalloc (rinfo->output_bfd, amt);
1776 rinfo->failed = TRUE;
1780 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1781 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1782 elf_tdata (rinfo->output_bfd)->verref = t;
1786 a = bfd_zalloc (rinfo->output_bfd, amt);
1788 /* Note that we are copying a string pointer here, and testing it
1789 above. If bfd_elf_string_from_elf_section is ever changed to
1790 discard the string data when low in memory, this will have to be
1792 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1794 a->vna_flags = h->verinfo.verdef->vd_flags;
1795 a->vna_nextptr = t->vn_auxptr;
1797 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1800 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1807 /* Figure out appropriate versions for all the symbols. We may not
1808 have the version number script until we have read all of the input
1809 files, so until that point we don't know which symbols should be
1810 local. This function is called via elf_link_hash_traverse. */
1813 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1815 struct elf_assign_sym_version_info *sinfo;
1816 struct bfd_link_info *info;
1817 const struct elf_backend_data *bed;
1818 struct elf_info_failed eif;
1825 if (h->root.type == bfd_link_hash_warning)
1826 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1828 /* Fix the symbol flags. */
1831 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1834 sinfo->failed = TRUE;
1838 /* We only need version numbers for symbols defined in regular
1840 if (!h->def_regular)
1843 bed = get_elf_backend_data (sinfo->output_bfd);
1844 p = strchr (h->root.root.string, ELF_VER_CHR);
1845 if (p != NULL && h->verinfo.vertree == NULL)
1847 struct bfd_elf_version_tree *t;
1852 /* There are two consecutive ELF_VER_CHR characters if this is
1853 not a hidden symbol. */
1855 if (*p == ELF_VER_CHR)
1861 /* If there is no version string, we can just return out. */
1869 /* Look for the version. If we find it, it is no longer weak. */
1870 for (t = sinfo->verdefs; t != NULL; t = t->next)
1872 if (strcmp (t->name, p) == 0)
1876 struct bfd_elf_version_expr *d;
1878 len = p - h->root.root.string;
1879 alc = bfd_malloc (len);
1882 memcpy (alc, h->root.root.string, len - 1);
1883 alc[len - 1] = '\0';
1884 if (alc[len - 2] == ELF_VER_CHR)
1885 alc[len - 2] = '\0';
1887 h->verinfo.vertree = t;
1891 if (t->globals.list != NULL)
1892 d = (*t->match) (&t->globals, NULL, alc);
1894 /* See if there is anything to force this symbol to
1896 if (d == NULL && t->locals.list != NULL)
1898 d = (*t->match) (&t->locals, NULL, alc);
1901 && ! info->export_dynamic)
1902 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1910 /* If we are building an application, we need to create a
1911 version node for this version. */
1912 if (t == NULL && info->executable)
1914 struct bfd_elf_version_tree **pp;
1917 /* If we aren't going to export this symbol, we don't need
1918 to worry about it. */
1919 if (h->dynindx == -1)
1923 t = bfd_zalloc (sinfo->output_bfd, amt);
1926 sinfo->failed = TRUE;
1931 t->name_indx = (unsigned int) -1;
1935 /* Don't count anonymous version tag. */
1936 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1938 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1940 t->vernum = version_index;
1944 h->verinfo.vertree = t;
1948 /* We could not find the version for a symbol when
1949 generating a shared archive. Return an error. */
1950 (*_bfd_error_handler)
1951 (_("%B: version node not found for symbol %s"),
1952 sinfo->output_bfd, h->root.root.string);
1953 bfd_set_error (bfd_error_bad_value);
1954 sinfo->failed = TRUE;
1962 /* If we don't have a version for this symbol, see if we can find
1964 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1966 struct bfd_elf_version_tree *t;
1967 struct bfd_elf_version_tree *local_ver;
1968 struct bfd_elf_version_expr *d;
1970 /* See if can find what version this symbol is in. If the
1971 symbol is supposed to be local, then don't actually register
1974 for (t = sinfo->verdefs; t != NULL; t = t->next)
1976 if (t->globals.list != NULL)
1978 bfd_boolean matched;
1982 while ((d = (*t->match) (&t->globals, d,
1983 h->root.root.string)) != NULL)
1988 /* There is a version without definition. Make
1989 the symbol the default definition for this
1991 h->verinfo.vertree = t;
1999 /* There is no undefined version for this symbol. Hide the
2001 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2004 if (t->locals.list != NULL)
2007 while ((d = (*t->match) (&t->locals, d,
2008 h->root.root.string)) != NULL)
2011 /* If the match is "*", keep looking for a more
2012 explicit, perhaps even global, match.
2013 XXX: Shouldn't this be !d->wildcard instead? */
2014 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
2023 if (local_ver != NULL)
2025 h->verinfo.vertree = local_ver;
2026 if (h->dynindx != -1
2027 && ! info->export_dynamic)
2029 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2037 /* Read and swap the relocs from the section indicated by SHDR. This
2038 may be either a REL or a RELA section. The relocations are
2039 translated into RELA relocations and stored in INTERNAL_RELOCS,
2040 which should have already been allocated to contain enough space.
2041 The EXTERNAL_RELOCS are a buffer where the external form of the
2042 relocations should be stored.
2044 Returns FALSE if something goes wrong. */
2047 elf_link_read_relocs_from_section (bfd *abfd,
2049 Elf_Internal_Shdr *shdr,
2050 void *external_relocs,
2051 Elf_Internal_Rela *internal_relocs)
2053 const struct elf_backend_data *bed;
2054 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2055 const bfd_byte *erela;
2056 const bfd_byte *erelaend;
2057 Elf_Internal_Rela *irela;
2058 Elf_Internal_Shdr *symtab_hdr;
2061 /* Position ourselves at the start of the section. */
2062 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2065 /* Read the relocations. */
2066 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2069 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2070 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
2072 bed = get_elf_backend_data (abfd);
2074 /* Convert the external relocations to the internal format. */
2075 if (shdr->sh_entsize == bed->s->sizeof_rel)
2076 swap_in = bed->s->swap_reloc_in;
2077 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2078 swap_in = bed->s->swap_reloca_in;
2081 bfd_set_error (bfd_error_wrong_format);
2085 erela = external_relocs;
2086 erelaend = erela + shdr->sh_size;
2087 irela = internal_relocs;
2088 while (erela < erelaend)
2092 (*swap_in) (abfd, erela, irela);
2093 r_symndx = ELF32_R_SYM (irela->r_info);
2094 if (bed->s->arch_size == 64)
2096 if ((size_t) r_symndx >= nsyms)
2098 (*_bfd_error_handler)
2099 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2100 " for offset 0x%lx in section `%A'"),
2102 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2103 bfd_set_error (bfd_error_bad_value);
2106 irela += bed->s->int_rels_per_ext_rel;
2107 erela += shdr->sh_entsize;
2113 /* Read and swap the relocs for a section O. They may have been
2114 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2115 not NULL, they are used as buffers to read into. They are known to
2116 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2117 the return value is allocated using either malloc or bfd_alloc,
2118 according to the KEEP_MEMORY argument. If O has two relocation
2119 sections (both REL and RELA relocations), then the REL_HDR
2120 relocations will appear first in INTERNAL_RELOCS, followed by the
2121 REL_HDR2 relocations. */
2124 _bfd_elf_link_read_relocs (bfd *abfd,
2126 void *external_relocs,
2127 Elf_Internal_Rela *internal_relocs,
2128 bfd_boolean keep_memory)
2130 Elf_Internal_Shdr *rel_hdr;
2131 void *alloc1 = NULL;
2132 Elf_Internal_Rela *alloc2 = NULL;
2133 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2135 if (elf_section_data (o)->relocs != NULL)
2136 return elf_section_data (o)->relocs;
2138 if (o->reloc_count == 0)
2141 rel_hdr = &elf_section_data (o)->rel_hdr;
2143 if (internal_relocs == NULL)
2147 size = o->reloc_count;
2148 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2150 internal_relocs = bfd_alloc (abfd, size);
2152 internal_relocs = alloc2 = bfd_malloc (size);
2153 if (internal_relocs == NULL)
2157 if (external_relocs == NULL)
2159 bfd_size_type size = rel_hdr->sh_size;
2161 if (elf_section_data (o)->rel_hdr2)
2162 size += elf_section_data (o)->rel_hdr2->sh_size;
2163 alloc1 = bfd_malloc (size);
2166 external_relocs = alloc1;
2169 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2173 if (elf_section_data (o)->rel_hdr2
2174 && (!elf_link_read_relocs_from_section
2176 elf_section_data (o)->rel_hdr2,
2177 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2178 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2179 * bed->s->int_rels_per_ext_rel))))
2182 /* Cache the results for next time, if we can. */
2184 elf_section_data (o)->relocs = internal_relocs;
2189 /* Don't free alloc2, since if it was allocated we are passing it
2190 back (under the name of internal_relocs). */
2192 return internal_relocs;
2202 /* Compute the size of, and allocate space for, REL_HDR which is the
2203 section header for a section containing relocations for O. */
2206 _bfd_elf_link_size_reloc_section (bfd *abfd,
2207 Elf_Internal_Shdr *rel_hdr,
2210 bfd_size_type reloc_count;
2211 bfd_size_type num_rel_hashes;
2213 /* Figure out how many relocations there will be. */
2214 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2215 reloc_count = elf_section_data (o)->rel_count;
2217 reloc_count = elf_section_data (o)->rel_count2;
2219 num_rel_hashes = o->reloc_count;
2220 if (num_rel_hashes < reloc_count)
2221 num_rel_hashes = reloc_count;
2223 /* That allows us to calculate the size of the section. */
2224 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2226 /* The contents field must last into write_object_contents, so we
2227 allocate it with bfd_alloc rather than malloc. Also since we
2228 cannot be sure that the contents will actually be filled in,
2229 we zero the allocated space. */
2230 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2231 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2234 /* We only allocate one set of hash entries, so we only do it the
2235 first time we are called. */
2236 if (elf_section_data (o)->rel_hashes == NULL
2239 struct elf_link_hash_entry **p;
2241 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2245 elf_section_data (o)->rel_hashes = p;
2251 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2252 originated from the section given by INPUT_REL_HDR) to the
2256 _bfd_elf_link_output_relocs (bfd *output_bfd,
2257 asection *input_section,
2258 Elf_Internal_Shdr *input_rel_hdr,
2259 Elf_Internal_Rela *internal_relocs,
2260 struct elf_link_hash_entry **rel_hash
2263 Elf_Internal_Rela *irela;
2264 Elf_Internal_Rela *irelaend;
2266 Elf_Internal_Shdr *output_rel_hdr;
2267 asection *output_section;
2268 unsigned int *rel_countp = NULL;
2269 const struct elf_backend_data *bed;
2270 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2272 output_section = input_section->output_section;
2273 output_rel_hdr = NULL;
2275 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2276 == input_rel_hdr->sh_entsize)
2278 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2279 rel_countp = &elf_section_data (output_section)->rel_count;
2281 else if (elf_section_data (output_section)->rel_hdr2
2282 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2283 == input_rel_hdr->sh_entsize))
2285 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2286 rel_countp = &elf_section_data (output_section)->rel_count2;
2290 (*_bfd_error_handler)
2291 (_("%B: relocation size mismatch in %B section %A"),
2292 output_bfd, input_section->owner, input_section);
2293 bfd_set_error (bfd_error_wrong_object_format);
2297 bed = get_elf_backend_data (output_bfd);
2298 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2299 swap_out = bed->s->swap_reloc_out;
2300 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2301 swap_out = bed->s->swap_reloca_out;
2305 erel = output_rel_hdr->contents;
2306 erel += *rel_countp * input_rel_hdr->sh_entsize;
2307 irela = internal_relocs;
2308 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2309 * bed->s->int_rels_per_ext_rel);
2310 while (irela < irelaend)
2312 (*swap_out) (output_bfd, irela, erel);
2313 irela += bed->s->int_rels_per_ext_rel;
2314 erel += input_rel_hdr->sh_entsize;
2317 /* Bump the counter, so that we know where to add the next set of
2319 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2324 /* Make weak undefined symbols in PIE dynamic. */
2327 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2328 struct elf_link_hash_entry *h)
2332 && h->root.type == bfd_link_hash_undefweak)
2333 return bfd_elf_link_record_dynamic_symbol (info, h);
2338 /* Fix up the flags for a symbol. This handles various cases which
2339 can only be fixed after all the input files are seen. This is
2340 currently called by both adjust_dynamic_symbol and
2341 assign_sym_version, which is unnecessary but perhaps more robust in
2342 the face of future changes. */
2345 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2346 struct elf_info_failed *eif)
2348 const struct elf_backend_data *bed = NULL;
2350 /* If this symbol was mentioned in a non-ELF file, try to set
2351 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2352 permit a non-ELF file to correctly refer to a symbol defined in
2353 an ELF dynamic object. */
2356 while (h->root.type == bfd_link_hash_indirect)
2357 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2359 if (h->root.type != bfd_link_hash_defined
2360 && h->root.type != bfd_link_hash_defweak)
2363 h->ref_regular_nonweak = 1;
2367 if (h->root.u.def.section->owner != NULL
2368 && (bfd_get_flavour (h->root.u.def.section->owner)
2369 == bfd_target_elf_flavour))
2372 h->ref_regular_nonweak = 1;
2378 if (h->dynindx == -1
2382 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2391 /* Unfortunately, NON_ELF is only correct if the symbol
2392 was first seen in a non-ELF file. Fortunately, if the symbol
2393 was first seen in an ELF file, we're probably OK unless the
2394 symbol was defined in a non-ELF file. Catch that case here.
2395 FIXME: We're still in trouble if the symbol was first seen in
2396 a dynamic object, and then later in a non-ELF regular object. */
2397 if ((h->root.type == bfd_link_hash_defined
2398 || h->root.type == bfd_link_hash_defweak)
2400 && (h->root.u.def.section->owner != NULL
2401 ? (bfd_get_flavour (h->root.u.def.section->owner)
2402 != bfd_target_elf_flavour)
2403 : (bfd_is_abs_section (h->root.u.def.section)
2404 && !h->def_dynamic)))
2408 /* Backend specific symbol fixup. */
2409 if (elf_hash_table (eif->info)->dynobj)
2411 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2412 if (bed->elf_backend_fixup_symbol
2413 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2417 /* If this is a final link, and the symbol was defined as a common
2418 symbol in a regular object file, and there was no definition in
2419 any dynamic object, then the linker will have allocated space for
2420 the symbol in a common section but the DEF_REGULAR
2421 flag will not have been set. */
2422 if (h->root.type == bfd_link_hash_defined
2426 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2429 /* If -Bsymbolic was used (which means to bind references to global
2430 symbols to the definition within the shared object), and this
2431 symbol was defined in a regular object, then it actually doesn't
2432 need a PLT entry. Likewise, if the symbol has non-default
2433 visibility. If the symbol has hidden or internal visibility, we
2434 will force it local. */
2436 && eif->info->shared
2437 && is_elf_hash_table (eif->info->hash)
2438 && (SYMBOLIC_BIND (eif->info, h)
2439 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2442 bfd_boolean force_local;
2444 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2445 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2446 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2449 /* If a weak undefined symbol has non-default visibility, we also
2450 hide it from the dynamic linker. */
2451 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2452 && h->root.type == bfd_link_hash_undefweak)
2454 const struct elf_backend_data *bed;
2455 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2456 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2459 /* If this is a weak defined symbol in a dynamic object, and we know
2460 the real definition in the dynamic object, copy interesting flags
2461 over to the real definition. */
2462 if (h->u.weakdef != NULL)
2464 struct elf_link_hash_entry *weakdef;
2466 weakdef = h->u.weakdef;
2467 if (h->root.type == bfd_link_hash_indirect)
2468 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2470 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2471 || h->root.type == bfd_link_hash_defweak);
2472 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2473 || weakdef->root.type == bfd_link_hash_defweak);
2474 BFD_ASSERT (weakdef->def_dynamic);
2476 /* If the real definition is defined by a regular object file,
2477 don't do anything special. See the longer description in
2478 _bfd_elf_adjust_dynamic_symbol, below. */
2479 if (weakdef->def_regular)
2480 h->u.weakdef = NULL;
2482 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef,
2489 /* Make the backend pick a good value for a dynamic symbol. This is
2490 called via elf_link_hash_traverse, and also calls itself
2494 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2496 struct elf_info_failed *eif = data;
2498 const struct elf_backend_data *bed;
2500 if (! is_elf_hash_table (eif->info->hash))
2503 if (h->root.type == bfd_link_hash_warning)
2505 h->got = elf_hash_table (eif->info)->init_got_offset;
2506 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2508 /* When warning symbols are created, they **replace** the "real"
2509 entry in the hash table, thus we never get to see the real
2510 symbol in a hash traversal. So look at it now. */
2511 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2514 /* Ignore indirect symbols. These are added by the versioning code. */
2515 if (h->root.type == bfd_link_hash_indirect)
2518 /* Fix the symbol flags. */
2519 if (! _bfd_elf_fix_symbol_flags (h, eif))
2522 /* If this symbol does not require a PLT entry, and it is not
2523 defined by a dynamic object, or is not referenced by a regular
2524 object, ignore it. We do have to handle a weak defined symbol,
2525 even if no regular object refers to it, if we decided to add it
2526 to the dynamic symbol table. FIXME: Do we normally need to worry
2527 about symbols which are defined by one dynamic object and
2528 referenced by another one? */
2533 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2535 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2539 /* If we've already adjusted this symbol, don't do it again. This
2540 can happen via a recursive call. */
2541 if (h->dynamic_adjusted)
2544 /* Don't look at this symbol again. Note that we must set this
2545 after checking the above conditions, because we may look at a
2546 symbol once, decide not to do anything, and then get called
2547 recursively later after REF_REGULAR is set below. */
2548 h->dynamic_adjusted = 1;
2550 /* If this is a weak definition, and we know a real definition, and
2551 the real symbol is not itself defined by a regular object file,
2552 then get a good value for the real definition. We handle the
2553 real symbol first, for the convenience of the backend routine.
2555 Note that there is a confusing case here. If the real definition
2556 is defined by a regular object file, we don't get the real symbol
2557 from the dynamic object, but we do get the weak symbol. If the
2558 processor backend uses a COPY reloc, then if some routine in the
2559 dynamic object changes the real symbol, we will not see that
2560 change in the corresponding weak symbol. This is the way other
2561 ELF linkers work as well, and seems to be a result of the shared
2564 I will clarify this issue. Most SVR4 shared libraries define the
2565 variable _timezone and define timezone as a weak synonym. The
2566 tzset call changes _timezone. If you write
2567 extern int timezone;
2569 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2570 you might expect that, since timezone is a synonym for _timezone,
2571 the same number will print both times. However, if the processor
2572 backend uses a COPY reloc, then actually timezone will be copied
2573 into your process image, and, since you define _timezone
2574 yourself, _timezone will not. Thus timezone and _timezone will
2575 wind up at different memory locations. The tzset call will set
2576 _timezone, leaving timezone unchanged. */
2578 if (h->u.weakdef != NULL)
2580 /* If we get to this point, we know there is an implicit
2581 reference by a regular object file via the weak symbol H.
2582 FIXME: Is this really true? What if the traversal finds
2583 H->U.WEAKDEF before it finds H? */
2584 h->u.weakdef->ref_regular = 1;
2586 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2590 /* If a symbol has no type and no size and does not require a PLT
2591 entry, then we are probably about to do the wrong thing here: we
2592 are probably going to create a COPY reloc for an empty object.
2593 This case can arise when a shared object is built with assembly
2594 code, and the assembly code fails to set the symbol type. */
2596 && h->type == STT_NOTYPE
2598 (*_bfd_error_handler)
2599 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2600 h->root.root.string);
2602 dynobj = elf_hash_table (eif->info)->dynobj;
2603 bed = get_elf_backend_data (dynobj);
2604 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2613 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2617 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2620 unsigned int power_of_two;
2622 asection *sec = h->root.u.def.section;
2624 /* The section aligment of definition is the maximum alignment
2625 requirement of symbols defined in the section. Since we don't
2626 know the symbol alignment requirement, we start with the
2627 maximum alignment and check low bits of the symbol address
2628 for the minimum alignment. */
2629 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2630 mask = ((bfd_vma) 1 << power_of_two) - 1;
2631 while ((h->root.u.def.value & mask) != 0)
2637 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2640 /* Adjust the section alignment if needed. */
2641 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2646 /* We make sure that the symbol will be aligned properly. */
2647 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2649 /* Define the symbol as being at this point in DYNBSS. */
2650 h->root.u.def.section = dynbss;
2651 h->root.u.def.value = dynbss->size;
2653 /* Increment the size of DYNBSS to make room for the symbol. */
2654 dynbss->size += h->size;
2659 /* Adjust all external symbols pointing into SEC_MERGE sections
2660 to reflect the object merging within the sections. */
2663 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2667 if (h->root.type == bfd_link_hash_warning)
2668 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2670 if ((h->root.type == bfd_link_hash_defined
2671 || h->root.type == bfd_link_hash_defweak)
2672 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2673 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2675 bfd *output_bfd = data;
2677 h->root.u.def.value =
2678 _bfd_merged_section_offset (output_bfd,
2679 &h->root.u.def.section,
2680 elf_section_data (sec)->sec_info,
2681 h->root.u.def.value);
2687 /* Returns false if the symbol referred to by H should be considered
2688 to resolve local to the current module, and true if it should be
2689 considered to bind dynamically. */
2692 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2693 struct bfd_link_info *info,
2694 bfd_boolean ignore_protected)
2696 bfd_boolean binding_stays_local_p;
2697 const struct elf_backend_data *bed;
2698 struct elf_link_hash_table *hash_table;
2703 while (h->root.type == bfd_link_hash_indirect
2704 || h->root.type == bfd_link_hash_warning)
2705 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2707 /* If it was forced local, then clearly it's not dynamic. */
2708 if (h->dynindx == -1)
2710 if (h->forced_local)
2713 /* Identify the cases where name binding rules say that a
2714 visible symbol resolves locally. */
2715 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2717 switch (ELF_ST_VISIBILITY (h->other))
2724 hash_table = elf_hash_table (info);
2725 if (!is_elf_hash_table (hash_table))
2728 bed = get_elf_backend_data (hash_table->dynobj);
2730 /* Proper resolution for function pointer equality may require
2731 that these symbols perhaps be resolved dynamically, even though
2732 we should be resolving them to the current module. */
2733 if (!ignore_protected || !bed->is_function_type (h->type))
2734 binding_stays_local_p = TRUE;
2741 /* If it isn't defined locally, then clearly it's dynamic. */
2742 if (!h->def_regular)
2745 /* Otherwise, the symbol is dynamic if binding rules don't tell
2746 us that it remains local. */
2747 return !binding_stays_local_p;
2750 /* Return true if the symbol referred to by H should be considered
2751 to resolve local to the current module, and false otherwise. Differs
2752 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2753 undefined symbols and weak symbols. */
2756 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2757 struct bfd_link_info *info,
2758 bfd_boolean local_protected)
2760 const struct elf_backend_data *bed;
2761 struct elf_link_hash_table *hash_table;
2763 /* If it's a local sym, of course we resolve locally. */
2767 /* Common symbols that become definitions don't get the DEF_REGULAR
2768 flag set, so test it first, and don't bail out. */
2769 if (ELF_COMMON_DEF_P (h))
2771 /* If we don't have a definition in a regular file, then we can't
2772 resolve locally. The sym is either undefined or dynamic. */
2773 else if (!h->def_regular)
2776 /* Forced local symbols resolve locally. */
2777 if (h->forced_local)
2780 /* As do non-dynamic symbols. */
2781 if (h->dynindx == -1)
2784 /* At this point, we know the symbol is defined and dynamic. In an
2785 executable it must resolve locally, likewise when building symbolic
2786 shared libraries. */
2787 if (info->executable || SYMBOLIC_BIND (info, h))
2790 /* Now deal with defined dynamic symbols in shared libraries. Ones
2791 with default visibility might not resolve locally. */
2792 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2795 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2796 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2799 hash_table = elf_hash_table (info);
2800 if (!is_elf_hash_table (hash_table))
2803 bed = get_elf_backend_data (hash_table->dynobj);
2805 /* STV_PROTECTED non-function symbols are local. */
2806 if (!bed->is_function_type (h->type))
2809 /* Function pointer equality tests may require that STV_PROTECTED
2810 symbols be treated as dynamic symbols, even when we know that the
2811 dynamic linker will resolve them locally. */
2812 return local_protected;
2815 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2816 aligned. Returns the first TLS output section. */
2818 struct bfd_section *
2819 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2821 struct bfd_section *sec, *tls;
2822 unsigned int align = 0;
2824 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2825 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2829 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2830 if (sec->alignment_power > align)
2831 align = sec->alignment_power;
2833 elf_hash_table (info)->tls_sec = tls;
2835 /* Ensure the alignment of the first section is the largest alignment,
2836 so that the tls segment starts aligned. */
2838 tls->alignment_power = align;
2843 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2845 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2846 Elf_Internal_Sym *sym)
2848 const struct elf_backend_data *bed;
2850 /* Local symbols do not count, but target specific ones might. */
2851 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2852 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2855 bed = get_elf_backend_data (abfd);
2856 /* Function symbols do not count. */
2857 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
2860 /* If the section is undefined, then so is the symbol. */
2861 if (sym->st_shndx == SHN_UNDEF)
2864 /* If the symbol is defined in the common section, then
2865 it is a common definition and so does not count. */
2866 if (bed->common_definition (sym))
2869 /* If the symbol is in a target specific section then we
2870 must rely upon the backend to tell us what it is. */
2871 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2872 /* FIXME - this function is not coded yet:
2874 return _bfd_is_global_symbol_definition (abfd, sym);
2876 Instead for now assume that the definition is not global,
2877 Even if this is wrong, at least the linker will behave
2878 in the same way that it used to do. */
2884 /* Search the symbol table of the archive element of the archive ABFD
2885 whose archive map contains a mention of SYMDEF, and determine if
2886 the symbol is defined in this element. */
2888 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2890 Elf_Internal_Shdr * hdr;
2891 bfd_size_type symcount;
2892 bfd_size_type extsymcount;
2893 bfd_size_type extsymoff;
2894 Elf_Internal_Sym *isymbuf;
2895 Elf_Internal_Sym *isym;
2896 Elf_Internal_Sym *isymend;
2899 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2903 if (! bfd_check_format (abfd, bfd_object))
2906 /* If we have already included the element containing this symbol in the
2907 link then we do not need to include it again. Just claim that any symbol
2908 it contains is not a definition, so that our caller will not decide to
2909 (re)include this element. */
2910 if (abfd->archive_pass)
2913 /* Select the appropriate symbol table. */
2914 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2915 hdr = &elf_tdata (abfd)->symtab_hdr;
2917 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2919 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2921 /* The sh_info field of the symtab header tells us where the
2922 external symbols start. We don't care about the local symbols. */
2923 if (elf_bad_symtab (abfd))
2925 extsymcount = symcount;
2930 extsymcount = symcount - hdr->sh_info;
2931 extsymoff = hdr->sh_info;
2934 if (extsymcount == 0)
2937 /* Read in the symbol table. */
2938 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2940 if (isymbuf == NULL)
2943 /* Scan the symbol table looking for SYMDEF. */
2945 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2949 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2954 if (strcmp (name, symdef->name) == 0)
2956 result = is_global_data_symbol_definition (abfd, isym);
2966 /* Add an entry to the .dynamic table. */
2969 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2973 struct elf_link_hash_table *hash_table;
2974 const struct elf_backend_data *bed;
2976 bfd_size_type newsize;
2977 bfd_byte *newcontents;
2978 Elf_Internal_Dyn dyn;
2980 hash_table = elf_hash_table (info);
2981 if (! is_elf_hash_table (hash_table))
2984 bed = get_elf_backend_data (hash_table->dynobj);
2985 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2986 BFD_ASSERT (s != NULL);
2988 newsize = s->size + bed->s->sizeof_dyn;
2989 newcontents = bfd_realloc (s->contents, newsize);
2990 if (newcontents == NULL)
2994 dyn.d_un.d_val = val;
2995 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2998 s->contents = newcontents;
3003 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3004 otherwise just check whether one already exists. Returns -1 on error,
3005 1 if a DT_NEEDED tag already exists, and 0 on success. */
3008 elf_add_dt_needed_tag (bfd *abfd,
3009 struct bfd_link_info *info,
3013 struct elf_link_hash_table *hash_table;
3014 bfd_size_type oldsize;
3015 bfd_size_type strindex;
3017 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3020 hash_table = elf_hash_table (info);
3021 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3022 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3023 if (strindex == (bfd_size_type) -1)
3026 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3029 const struct elf_backend_data *bed;
3032 bed = get_elf_backend_data (hash_table->dynobj);
3033 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3035 for (extdyn = sdyn->contents;
3036 extdyn < sdyn->contents + sdyn->size;
3037 extdyn += bed->s->sizeof_dyn)
3039 Elf_Internal_Dyn dyn;
3041 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3042 if (dyn.d_tag == DT_NEEDED
3043 && dyn.d_un.d_val == strindex)
3045 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3053 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3056 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3060 /* We were just checking for existence of the tag. */
3061 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3066 /* Sort symbol by value and section. */
3068 elf_sort_symbol (const void *arg1, const void *arg2)
3070 const struct elf_link_hash_entry *h1;
3071 const struct elf_link_hash_entry *h2;
3072 bfd_signed_vma vdiff;
3074 h1 = *(const struct elf_link_hash_entry **) arg1;
3075 h2 = *(const struct elf_link_hash_entry **) arg2;
3076 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3078 return vdiff > 0 ? 1 : -1;
3081 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3083 return sdiff > 0 ? 1 : -1;
3088 /* This function is used to adjust offsets into .dynstr for
3089 dynamic symbols. This is called via elf_link_hash_traverse. */
3092 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3094 struct elf_strtab_hash *dynstr = data;
3096 if (h->root.type == bfd_link_hash_warning)
3097 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3099 if (h->dynindx != -1)
3100 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3104 /* Assign string offsets in .dynstr, update all structures referencing
3108 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3110 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3111 struct elf_link_local_dynamic_entry *entry;
3112 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3113 bfd *dynobj = hash_table->dynobj;
3116 const struct elf_backend_data *bed;
3119 _bfd_elf_strtab_finalize (dynstr);
3120 size = _bfd_elf_strtab_size (dynstr);
3122 bed = get_elf_backend_data (dynobj);
3123 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3124 BFD_ASSERT (sdyn != NULL);
3126 /* Update all .dynamic entries referencing .dynstr strings. */
3127 for (extdyn = sdyn->contents;
3128 extdyn < sdyn->contents + sdyn->size;
3129 extdyn += bed->s->sizeof_dyn)
3131 Elf_Internal_Dyn dyn;
3133 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3137 dyn.d_un.d_val = size;
3145 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3150 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3153 /* Now update local dynamic symbols. */
3154 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3155 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3156 entry->isym.st_name);
3158 /* And the rest of dynamic symbols. */
3159 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3161 /* Adjust version definitions. */
3162 if (elf_tdata (output_bfd)->cverdefs)
3167 Elf_Internal_Verdef def;
3168 Elf_Internal_Verdaux defaux;
3170 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3174 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3176 p += sizeof (Elf_External_Verdef);
3177 if (def.vd_aux != sizeof (Elf_External_Verdef))
3179 for (i = 0; i < def.vd_cnt; ++i)
3181 _bfd_elf_swap_verdaux_in (output_bfd,
3182 (Elf_External_Verdaux *) p, &defaux);
3183 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3185 _bfd_elf_swap_verdaux_out (output_bfd,
3186 &defaux, (Elf_External_Verdaux *) p);
3187 p += sizeof (Elf_External_Verdaux);
3190 while (def.vd_next);
3193 /* Adjust version references. */
3194 if (elf_tdata (output_bfd)->verref)
3199 Elf_Internal_Verneed need;
3200 Elf_Internal_Vernaux needaux;
3202 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3206 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3208 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3209 _bfd_elf_swap_verneed_out (output_bfd, &need,
3210 (Elf_External_Verneed *) p);
3211 p += sizeof (Elf_External_Verneed);
3212 for (i = 0; i < need.vn_cnt; ++i)
3214 _bfd_elf_swap_vernaux_in (output_bfd,
3215 (Elf_External_Vernaux *) p, &needaux);
3216 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3218 _bfd_elf_swap_vernaux_out (output_bfd,
3220 (Elf_External_Vernaux *) p);
3221 p += sizeof (Elf_External_Vernaux);
3224 while (need.vn_next);
3230 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3231 The default is to only match when the INPUT and OUTPUT are exactly
3235 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3236 const bfd_target *output)
3238 return input == output;
3241 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3242 This version is used when different targets for the same architecture
3243 are virtually identical. */
3246 _bfd_elf_relocs_compatible (const bfd_target *input,
3247 const bfd_target *output)
3249 const struct elf_backend_data *obed, *ibed;
3251 if (input == output)
3254 ibed = xvec_get_elf_backend_data (input);
3255 obed = xvec_get_elf_backend_data (output);
3257 if (ibed->arch != obed->arch)
3260 /* If both backends are using this function, deem them compatible. */
3261 return ibed->relocs_compatible == obed->relocs_compatible;
3264 /* Add symbols from an ELF object file to the linker hash table. */
3267 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3269 Elf_Internal_Shdr *hdr;
3270 bfd_size_type symcount;
3271 bfd_size_type extsymcount;
3272 bfd_size_type extsymoff;
3273 struct elf_link_hash_entry **sym_hash;
3274 bfd_boolean dynamic;
3275 Elf_External_Versym *extversym = NULL;
3276 Elf_External_Versym *ever;
3277 struct elf_link_hash_entry *weaks;
3278 struct elf_link_hash_entry **nondeflt_vers = NULL;
3279 bfd_size_type nondeflt_vers_cnt = 0;
3280 Elf_Internal_Sym *isymbuf = NULL;
3281 Elf_Internal_Sym *isym;
3282 Elf_Internal_Sym *isymend;
3283 const struct elf_backend_data *bed;
3284 bfd_boolean add_needed;
3285 struct elf_link_hash_table *htab;
3287 void *alloc_mark = NULL;
3288 struct bfd_hash_entry **old_table = NULL;
3289 unsigned int old_size = 0;
3290 unsigned int old_count = 0;
3291 void *old_tab = NULL;
3294 struct bfd_link_hash_entry *old_undefs = NULL;
3295 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3296 long old_dynsymcount = 0;
3298 size_t hashsize = 0;
3300 htab = elf_hash_table (info);
3301 bed = get_elf_backend_data (abfd);
3303 if ((abfd->flags & DYNAMIC) == 0)
3309 /* You can't use -r against a dynamic object. Also, there's no
3310 hope of using a dynamic object which does not exactly match
3311 the format of the output file. */
3312 if (info->relocatable
3313 || !is_elf_hash_table (htab)
3314 || htab->root.creator != abfd->xvec)
3316 if (info->relocatable)
3317 bfd_set_error (bfd_error_invalid_operation);
3319 bfd_set_error (bfd_error_wrong_format);
3324 /* As a GNU extension, any input sections which are named
3325 .gnu.warning.SYMBOL are treated as warning symbols for the given
3326 symbol. This differs from .gnu.warning sections, which generate
3327 warnings when they are included in an output file. */
3328 if (info->executable)
3332 for (s = abfd->sections; s != NULL; s = s->next)
3336 name = bfd_get_section_name (abfd, s);
3337 if (CONST_STRNEQ (name, ".gnu.warning."))
3342 name += sizeof ".gnu.warning." - 1;
3344 /* If this is a shared object, then look up the symbol
3345 in the hash table. If it is there, and it is already
3346 been defined, then we will not be using the entry
3347 from this shared object, so we don't need to warn.
3348 FIXME: If we see the definition in a regular object
3349 later on, we will warn, but we shouldn't. The only
3350 fix is to keep track of what warnings we are supposed
3351 to emit, and then handle them all at the end of the
3355 struct elf_link_hash_entry *h;
3357 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3359 /* FIXME: What about bfd_link_hash_common? */
3361 && (h->root.type == bfd_link_hash_defined
3362 || h->root.type == bfd_link_hash_defweak))
3364 /* We don't want to issue this warning. Clobber
3365 the section size so that the warning does not
3366 get copied into the output file. */
3373 msg = bfd_alloc (abfd, sz + 1);
3377 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3382 if (! (_bfd_generic_link_add_one_symbol
3383 (info, abfd, name, BSF_WARNING, s, 0, msg,
3384 FALSE, bed->collect, NULL)))
3387 if (! info->relocatable)
3389 /* Clobber the section size so that the warning does
3390 not get copied into the output file. */
3393 /* Also set SEC_EXCLUDE, so that symbols defined in
3394 the warning section don't get copied to the output. */
3395 s->flags |= SEC_EXCLUDE;
3404 /* If we are creating a shared library, create all the dynamic
3405 sections immediately. We need to attach them to something,
3406 so we attach them to this BFD, provided it is the right
3407 format. FIXME: If there are no input BFD's of the same
3408 format as the output, we can't make a shared library. */
3410 && is_elf_hash_table (htab)
3411 && htab->root.creator == abfd->xvec
3412 && !htab->dynamic_sections_created)
3414 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3418 else if (!is_elf_hash_table (htab))
3423 const char *soname = NULL;
3424 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3427 /* ld --just-symbols and dynamic objects don't mix very well.
3428 ld shouldn't allow it. */
3429 if ((s = abfd->sections) != NULL
3430 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3433 /* If this dynamic lib was specified on the command line with
3434 --as-needed in effect, then we don't want to add a DT_NEEDED
3435 tag unless the lib is actually used. Similary for libs brought
3436 in by another lib's DT_NEEDED. When --no-add-needed is used
3437 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3438 any dynamic library in DT_NEEDED tags in the dynamic lib at
3440 add_needed = (elf_dyn_lib_class (abfd)
3441 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3442 | DYN_NO_NEEDED)) == 0;
3444 s = bfd_get_section_by_name (abfd, ".dynamic");
3450 unsigned long shlink;
3452 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3453 goto error_free_dyn;
3455 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3457 goto error_free_dyn;
3458 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3460 for (extdyn = dynbuf;
3461 extdyn < dynbuf + s->size;
3462 extdyn += bed->s->sizeof_dyn)
3464 Elf_Internal_Dyn dyn;
3466 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3467 if (dyn.d_tag == DT_SONAME)
3469 unsigned int tagv = dyn.d_un.d_val;
3470 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3472 goto error_free_dyn;
3474 if (dyn.d_tag == DT_NEEDED)
3476 struct bfd_link_needed_list *n, **pn;
3478 unsigned int tagv = dyn.d_un.d_val;
3480 amt = sizeof (struct bfd_link_needed_list);
3481 n = bfd_alloc (abfd, amt);
3482 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3483 if (n == NULL || fnm == NULL)
3484 goto error_free_dyn;
3485 amt = strlen (fnm) + 1;
3486 anm = bfd_alloc (abfd, amt);
3488 goto error_free_dyn;
3489 memcpy (anm, fnm, amt);
3493 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3497 if (dyn.d_tag == DT_RUNPATH)
3499 struct bfd_link_needed_list *n, **pn;
3501 unsigned int tagv = dyn.d_un.d_val;
3503 amt = sizeof (struct bfd_link_needed_list);
3504 n = bfd_alloc (abfd, amt);
3505 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3506 if (n == NULL || fnm == NULL)
3507 goto error_free_dyn;
3508 amt = strlen (fnm) + 1;
3509 anm = bfd_alloc (abfd, amt);
3511 goto error_free_dyn;
3512 memcpy (anm, fnm, amt);
3516 for (pn = & runpath;
3522 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3523 if (!runpath && dyn.d_tag == DT_RPATH)
3525 struct bfd_link_needed_list *n, **pn;
3527 unsigned int tagv = dyn.d_un.d_val;
3529 amt = sizeof (struct bfd_link_needed_list);
3530 n = bfd_alloc (abfd, amt);
3531 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3532 if (n == NULL || fnm == NULL)
3533 goto error_free_dyn;
3534 amt = strlen (fnm) + 1;
3535 anm = bfd_alloc (abfd, amt);
3542 memcpy (anm, fnm, amt);
3557 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3558 frees all more recently bfd_alloc'd blocks as well. */
3564 struct bfd_link_needed_list **pn;
3565 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3570 /* We do not want to include any of the sections in a dynamic
3571 object in the output file. We hack by simply clobbering the
3572 list of sections in the BFD. This could be handled more
3573 cleanly by, say, a new section flag; the existing
3574 SEC_NEVER_LOAD flag is not the one we want, because that one
3575 still implies that the section takes up space in the output
3577 bfd_section_list_clear (abfd);
3579 /* Find the name to use in a DT_NEEDED entry that refers to this
3580 object. If the object has a DT_SONAME entry, we use it.
3581 Otherwise, if the generic linker stuck something in
3582 elf_dt_name, we use that. Otherwise, we just use the file
3584 if (soname == NULL || *soname == '\0')
3586 soname = elf_dt_name (abfd);
3587 if (soname == NULL || *soname == '\0')
3588 soname = bfd_get_filename (abfd);
3591 /* Save the SONAME because sometimes the linker emulation code
3592 will need to know it. */
3593 elf_dt_name (abfd) = soname;
3595 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3599 /* If we have already included this dynamic object in the
3600 link, just ignore it. There is no reason to include a
3601 particular dynamic object more than once. */
3606 /* If this is a dynamic object, we always link against the .dynsym
3607 symbol table, not the .symtab symbol table. The dynamic linker
3608 will only see the .dynsym symbol table, so there is no reason to
3609 look at .symtab for a dynamic object. */
3611 if (! dynamic || elf_dynsymtab (abfd) == 0)
3612 hdr = &elf_tdata (abfd)->symtab_hdr;
3614 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3616 symcount = hdr->sh_size / bed->s->sizeof_sym;
3618 /* The sh_info field of the symtab header tells us where the
3619 external symbols start. We don't care about the local symbols at
3621 if (elf_bad_symtab (abfd))
3623 extsymcount = symcount;
3628 extsymcount = symcount - hdr->sh_info;
3629 extsymoff = hdr->sh_info;
3633 if (extsymcount != 0)
3635 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3637 if (isymbuf == NULL)
3640 /* We store a pointer to the hash table entry for each external
3642 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3643 sym_hash = bfd_alloc (abfd, amt);
3644 if (sym_hash == NULL)
3645 goto error_free_sym;
3646 elf_sym_hashes (abfd) = sym_hash;
3651 /* Read in any version definitions. */
3652 if (!_bfd_elf_slurp_version_tables (abfd,
3653 info->default_imported_symver))
3654 goto error_free_sym;
3656 /* Read in the symbol versions, but don't bother to convert them
3657 to internal format. */
3658 if (elf_dynversym (abfd) != 0)
3660 Elf_Internal_Shdr *versymhdr;
3662 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3663 extversym = bfd_malloc (versymhdr->sh_size);
3664 if (extversym == NULL)
3665 goto error_free_sym;
3666 amt = versymhdr->sh_size;
3667 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3668 || bfd_bread (extversym, amt, abfd) != amt)
3669 goto error_free_vers;
3673 /* If we are loading an as-needed shared lib, save the symbol table
3674 state before we start adding symbols. If the lib turns out
3675 to be unneeded, restore the state. */
3676 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3681 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3683 struct bfd_hash_entry *p;
3684 struct elf_link_hash_entry *h;
3686 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3688 h = (struct elf_link_hash_entry *) p;
3689 entsize += htab->root.table.entsize;
3690 if (h->root.type == bfd_link_hash_warning)
3691 entsize += htab->root.table.entsize;
3695 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3696 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3697 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3698 if (old_tab == NULL)
3699 goto error_free_vers;
3701 /* Remember the current objalloc pointer, so that all mem for
3702 symbols added can later be reclaimed. */
3703 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3704 if (alloc_mark == NULL)
3705 goto error_free_vers;
3707 /* Make a special call to the linker "notice" function to
3708 tell it that we are about to handle an as-needed lib. */
3709 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
3714 /* Clone the symbol table and sym hashes. Remember some
3715 pointers into the symbol table, and dynamic symbol count. */
3716 old_hash = (char *) old_tab + tabsize;
3717 old_ent = (char *) old_hash + hashsize;
3718 memcpy (old_tab, htab->root.table.table, tabsize);
3719 memcpy (old_hash, sym_hash, hashsize);
3720 old_undefs = htab->root.undefs;
3721 old_undefs_tail = htab->root.undefs_tail;
3722 old_table = htab->root.table.table;
3723 old_size = htab->root.table.size;
3724 old_count = htab->root.table.count;
3725 old_dynsymcount = htab->dynsymcount;
3727 for (i = 0; i < htab->root.table.size; i++)
3729 struct bfd_hash_entry *p;
3730 struct elf_link_hash_entry *h;
3732 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3734 memcpy (old_ent, p, htab->root.table.entsize);
3735 old_ent = (char *) old_ent + htab->root.table.entsize;
3736 h = (struct elf_link_hash_entry *) p;
3737 if (h->root.type == bfd_link_hash_warning)
3739 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3740 old_ent = (char *) old_ent + htab->root.table.entsize;
3747 ever = extversym != NULL ? extversym + extsymoff : NULL;
3748 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3750 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3754 asection *sec, *new_sec;
3757 struct elf_link_hash_entry *h;
3758 bfd_boolean definition;
3759 bfd_boolean size_change_ok;
3760 bfd_boolean type_change_ok;
3761 bfd_boolean new_weakdef;
3762 bfd_boolean override;
3764 unsigned int old_alignment;
3769 flags = BSF_NO_FLAGS;
3771 value = isym->st_value;
3773 common = bed->common_definition (isym);
3775 bind = ELF_ST_BIND (isym->st_info);
3776 if (bind == STB_LOCAL)
3778 /* This should be impossible, since ELF requires that all
3779 global symbols follow all local symbols, and that sh_info
3780 point to the first global symbol. Unfortunately, Irix 5
3784 else if (bind == STB_GLOBAL)
3786 if (isym->st_shndx != SHN_UNDEF && !common)
3789 else if (bind == STB_WEAK)
3793 /* Leave it up to the processor backend. */
3796 if (isym->st_shndx == SHN_UNDEF)
3797 sec = bfd_und_section_ptr;
3798 else if (isym->st_shndx < SHN_LORESERVE
3799 || isym->st_shndx > SHN_HIRESERVE)
3801 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3803 sec = bfd_abs_section_ptr;
3804 else if (sec->kept_section)
3806 /* Symbols from discarded section are undefined. We keep
3808 sec = bfd_und_section_ptr;
3809 isym->st_shndx = SHN_UNDEF;
3811 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3814 else if (isym->st_shndx == SHN_ABS)
3815 sec = bfd_abs_section_ptr;
3816 else if (isym->st_shndx == SHN_COMMON)
3818 sec = bfd_com_section_ptr;
3819 /* What ELF calls the size we call the value. What ELF
3820 calls the value we call the alignment. */
3821 value = isym->st_size;
3825 /* Leave it up to the processor backend. */
3828 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3831 goto error_free_vers;
3833 if (isym->st_shndx == SHN_COMMON
3834 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3835 && !info->relocatable)
3837 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3841 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3844 | SEC_LINKER_CREATED
3845 | SEC_THREAD_LOCAL));
3847 goto error_free_vers;
3851 else if (bed->elf_add_symbol_hook)
3853 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3855 goto error_free_vers;
3857 /* The hook function sets the name to NULL if this symbol
3858 should be skipped for some reason. */
3863 /* Sanity check that all possibilities were handled. */
3866 bfd_set_error (bfd_error_bad_value);
3867 goto error_free_vers;
3870 if (bfd_is_und_section (sec)
3871 || bfd_is_com_section (sec))
3876 size_change_ok = FALSE;
3877 type_change_ok = bed->type_change_ok;
3882 if (is_elf_hash_table (htab))
3884 Elf_Internal_Versym iver;
3885 unsigned int vernum = 0;
3890 if (info->default_imported_symver)
3891 /* Use the default symbol version created earlier. */
3892 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3897 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3899 vernum = iver.vs_vers & VERSYM_VERSION;
3901 /* If this is a hidden symbol, or if it is not version
3902 1, we append the version name to the symbol name.
3903 However, we do not modify a non-hidden absolute symbol
3904 if it is not a function, because it might be the version
3905 symbol itself. FIXME: What if it isn't? */
3906 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3908 && (!bfd_is_abs_section (sec)
3909 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
3912 size_t namelen, verlen, newlen;
3915 if (isym->st_shndx != SHN_UNDEF)
3917 if (vernum > elf_tdata (abfd)->cverdefs)
3919 else if (vernum > 1)
3921 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3927 (*_bfd_error_handler)
3928 (_("%B: %s: invalid version %u (max %d)"),
3930 elf_tdata (abfd)->cverdefs);
3931 bfd_set_error (bfd_error_bad_value);
3932 goto error_free_vers;
3937 /* We cannot simply test for the number of
3938 entries in the VERNEED section since the
3939 numbers for the needed versions do not start
3941 Elf_Internal_Verneed *t;
3944 for (t = elf_tdata (abfd)->verref;
3948 Elf_Internal_Vernaux *a;
3950 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3952 if (a->vna_other == vernum)
3954 verstr = a->vna_nodename;
3963 (*_bfd_error_handler)
3964 (_("%B: %s: invalid needed version %d"),
3965 abfd, name, vernum);
3966 bfd_set_error (bfd_error_bad_value);
3967 goto error_free_vers;
3971 namelen = strlen (name);
3972 verlen = strlen (verstr);
3973 newlen = namelen + verlen + 2;
3974 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3975 && isym->st_shndx != SHN_UNDEF)
3978 newname = bfd_hash_allocate (&htab->root.table, newlen);
3979 if (newname == NULL)
3980 goto error_free_vers;
3981 memcpy (newname, name, namelen);
3982 p = newname + namelen;
3984 /* If this is a defined non-hidden version symbol,
3985 we add another @ to the name. This indicates the
3986 default version of the symbol. */
3987 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3988 && isym->st_shndx != SHN_UNDEF)
3990 memcpy (p, verstr, verlen + 1);
3995 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
3996 &value, &old_alignment,
3997 sym_hash, &skip, &override,
3998 &type_change_ok, &size_change_ok))
3999 goto error_free_vers;
4008 while (h->root.type == bfd_link_hash_indirect
4009 || h->root.type == bfd_link_hash_warning)
4010 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4012 /* Remember the old alignment if this is a common symbol, so
4013 that we don't reduce the alignment later on. We can't
4014 check later, because _bfd_generic_link_add_one_symbol
4015 will set a default for the alignment which we want to
4016 override. We also remember the old bfd where the existing
4017 definition comes from. */
4018 switch (h->root.type)
4023 case bfd_link_hash_defined:
4024 case bfd_link_hash_defweak:
4025 old_bfd = h->root.u.def.section->owner;
4028 case bfd_link_hash_common:
4029 old_bfd = h->root.u.c.p->section->owner;
4030 old_alignment = h->root.u.c.p->alignment_power;
4034 if (elf_tdata (abfd)->verdef != NULL
4038 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4041 if (! (_bfd_generic_link_add_one_symbol
4042 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4043 (struct bfd_link_hash_entry **) sym_hash)))
4044 goto error_free_vers;
4047 while (h->root.type == bfd_link_hash_indirect
4048 || h->root.type == bfd_link_hash_warning)
4049 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4052 new_weakdef = FALSE;
4055 && (flags & BSF_WEAK) != 0
4056 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4057 && is_elf_hash_table (htab)
4058 && h->u.weakdef == NULL)
4060 /* Keep a list of all weak defined non function symbols from
4061 a dynamic object, using the weakdef field. Later in this
4062 function we will set the weakdef field to the correct
4063 value. We only put non-function symbols from dynamic
4064 objects on this list, because that happens to be the only
4065 time we need to know the normal symbol corresponding to a
4066 weak symbol, and the information is time consuming to
4067 figure out. If the weakdef field is not already NULL,
4068 then this symbol was already defined by some previous
4069 dynamic object, and we will be using that previous
4070 definition anyhow. */
4072 h->u.weakdef = weaks;
4077 /* Set the alignment of a common symbol. */
4078 if ((common || bfd_is_com_section (sec))
4079 && h->root.type == bfd_link_hash_common)
4084 align = bfd_log2 (isym->st_value);
4087 /* The new symbol is a common symbol in a shared object.
4088 We need to get the alignment from the section. */
4089 align = new_sec->alignment_power;
4091 if (align > old_alignment
4092 /* Permit an alignment power of zero if an alignment of one
4093 is specified and no other alignments have been specified. */
4094 || (isym->st_value == 1 && old_alignment == 0))
4095 h->root.u.c.p->alignment_power = align;
4097 h->root.u.c.p->alignment_power = old_alignment;
4100 if (is_elf_hash_table (htab))
4104 /* Check the alignment when a common symbol is involved. This
4105 can change when a common symbol is overridden by a normal
4106 definition or a common symbol is ignored due to the old
4107 normal definition. We need to make sure the maximum
4108 alignment is maintained. */
4109 if ((old_alignment || common)
4110 && h->root.type != bfd_link_hash_common)
4112 unsigned int common_align;
4113 unsigned int normal_align;
4114 unsigned int symbol_align;
4118 symbol_align = ffs (h->root.u.def.value) - 1;
4119 if (h->root.u.def.section->owner != NULL
4120 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4122 normal_align = h->root.u.def.section->alignment_power;
4123 if (normal_align > symbol_align)
4124 normal_align = symbol_align;
4127 normal_align = symbol_align;
4131 common_align = old_alignment;
4132 common_bfd = old_bfd;
4137 common_align = bfd_log2 (isym->st_value);
4139 normal_bfd = old_bfd;
4142 if (normal_align < common_align)
4144 /* PR binutils/2735 */
4145 if (normal_bfd == NULL)
4146 (*_bfd_error_handler)
4147 (_("Warning: alignment %u of common symbol `%s' in %B"
4148 " is greater than the alignment (%u) of its section %A"),
4149 common_bfd, h->root.u.def.section,
4150 1 << common_align, name, 1 << normal_align);
4152 (*_bfd_error_handler)
4153 (_("Warning: alignment %u of symbol `%s' in %B"
4154 " is smaller than %u in %B"),
4155 normal_bfd, common_bfd,
4156 1 << normal_align, name, 1 << common_align);
4160 /* Remember the symbol size if it isn't undefined. */
4161 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4162 && (definition || h->size == 0))
4165 && h->size != isym->st_size
4166 && ! size_change_ok)
4167 (*_bfd_error_handler)
4168 (_("Warning: size of symbol `%s' changed"
4169 " from %lu in %B to %lu in %B"),
4171 name, (unsigned long) h->size,
4172 (unsigned long) isym->st_size);
4174 h->size = isym->st_size;
4177 /* If this is a common symbol, then we always want H->SIZE
4178 to be the size of the common symbol. The code just above
4179 won't fix the size if a common symbol becomes larger. We
4180 don't warn about a size change here, because that is
4181 covered by --warn-common. Allow changed between different
4183 if (h->root.type == bfd_link_hash_common)
4184 h->size = h->root.u.c.size;
4186 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4187 && (definition || h->type == STT_NOTYPE))
4189 if (h->type != STT_NOTYPE
4190 && h->type != ELF_ST_TYPE (isym->st_info)
4191 && ! type_change_ok)
4192 (*_bfd_error_handler)
4193 (_("Warning: type of symbol `%s' changed"
4194 " from %d to %d in %B"),
4195 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
4197 h->type = ELF_ST_TYPE (isym->st_info);
4200 /* If st_other has a processor-specific meaning, specific
4201 code might be needed here. We never merge the visibility
4202 attribute with the one from a dynamic object. */
4203 if (bed->elf_backend_merge_symbol_attribute)
4204 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
4207 /* If this symbol has default visibility and the user has requested
4208 we not re-export it, then mark it as hidden. */
4209 if (definition && !dynamic
4211 || (abfd->my_archive && abfd->my_archive->no_export))
4212 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4213 isym->st_other = (STV_HIDDEN
4214 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4216 if (ELF_ST_VISIBILITY (isym->st_other) != 0 && !dynamic)
4218 unsigned char hvis, symvis, other, nvis;
4220 /* Only merge the visibility. Leave the remainder of the
4221 st_other field to elf_backend_merge_symbol_attribute. */
4222 other = h->other & ~ELF_ST_VISIBILITY (-1);
4224 /* Combine visibilities, using the most constraining one. */
4225 hvis = ELF_ST_VISIBILITY (h->other);
4226 symvis = ELF_ST_VISIBILITY (isym->st_other);
4232 nvis = hvis < symvis ? hvis : symvis;
4234 h->other = other | nvis;
4237 /* Set a flag in the hash table entry indicating the type of
4238 reference or definition we just found. Keep a count of
4239 the number of dynamic symbols we find. A dynamic symbol
4240 is one which is referenced or defined by both a regular
4241 object and a shared object. */
4248 if (bind != STB_WEAK)
4249 h->ref_regular_nonweak = 1;
4253 if (! info->executable
4266 || (h->u.weakdef != NULL
4268 && h->u.weakdef->dynindx != -1))
4272 if (definition && (sec->flags & SEC_DEBUGGING))
4274 /* We don't want to make debug symbol dynamic. */
4275 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4279 /* Check to see if we need to add an indirect symbol for
4280 the default name. */
4281 if (definition || h->root.type == bfd_link_hash_common)
4282 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4283 &sec, &value, &dynsym,
4285 goto error_free_vers;
4287 if (definition && !dynamic)
4289 char *p = strchr (name, ELF_VER_CHR);
4290 if (p != NULL && p[1] != ELF_VER_CHR)
4292 /* Queue non-default versions so that .symver x, x@FOO
4293 aliases can be checked. */
4296 amt = ((isymend - isym + 1)
4297 * sizeof (struct elf_link_hash_entry *));
4298 nondeflt_vers = bfd_malloc (amt);
4300 nondeflt_vers[nondeflt_vers_cnt++] = h;
4304 if (dynsym && h->dynindx == -1)
4306 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4307 goto error_free_vers;
4308 if (h->u.weakdef != NULL
4310 && h->u.weakdef->dynindx == -1)
4312 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4313 goto error_free_vers;
4316 else if (dynsym && h->dynindx != -1)
4317 /* If the symbol already has a dynamic index, but
4318 visibility says it should not be visible, turn it into
4320 switch (ELF_ST_VISIBILITY (h->other))
4324 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4335 const char *soname = elf_dt_name (abfd);
4337 /* A symbol from a library loaded via DT_NEEDED of some
4338 other library is referenced by a regular object.
4339 Add a DT_NEEDED entry for it. Issue an error if
4340 --no-add-needed is used. */
4341 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4343 (*_bfd_error_handler)
4344 (_("%s: invalid DSO for symbol `%s' definition"),
4346 bfd_set_error (bfd_error_bad_value);
4347 goto error_free_vers;
4350 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4353 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4355 goto error_free_vers;
4357 BFD_ASSERT (ret == 0);
4362 if (extversym != NULL)
4368 if (isymbuf != NULL)
4374 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4378 /* Restore the symbol table. */
4379 if (bed->as_needed_cleanup)
4380 (*bed->as_needed_cleanup) (abfd, info);
4381 old_hash = (char *) old_tab + tabsize;
4382 old_ent = (char *) old_hash + hashsize;
4383 sym_hash = elf_sym_hashes (abfd);
4384 htab->root.table.table = old_table;
4385 htab->root.table.size = old_size;
4386 htab->root.table.count = old_count;
4387 memcpy (htab->root.table.table, old_tab, tabsize);
4388 memcpy (sym_hash, old_hash, hashsize);
4389 htab->root.undefs = old_undefs;
4390 htab->root.undefs_tail = old_undefs_tail;
4391 for (i = 0; i < htab->root.table.size; i++)
4393 struct bfd_hash_entry *p;
4394 struct elf_link_hash_entry *h;
4396 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4398 h = (struct elf_link_hash_entry *) p;
4399 if (h->root.type == bfd_link_hash_warning)
4400 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4401 if (h->dynindx >= old_dynsymcount)
4402 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4404 memcpy (p, old_ent, htab->root.table.entsize);
4405 old_ent = (char *) old_ent + htab->root.table.entsize;
4406 h = (struct elf_link_hash_entry *) p;
4407 if (h->root.type == bfd_link_hash_warning)
4409 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4410 old_ent = (char *) old_ent + htab->root.table.entsize;
4415 /* Make a special call to the linker "notice" function to
4416 tell it that symbols added for crefs may need to be removed. */
4417 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4422 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4424 if (nondeflt_vers != NULL)
4425 free (nondeflt_vers);
4429 if (old_tab != NULL)
4431 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4438 /* Now that all the symbols from this input file are created, handle
4439 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4440 if (nondeflt_vers != NULL)
4442 bfd_size_type cnt, symidx;
4444 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4446 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4447 char *shortname, *p;
4449 p = strchr (h->root.root.string, ELF_VER_CHR);
4451 || (h->root.type != bfd_link_hash_defined
4452 && h->root.type != bfd_link_hash_defweak))
4455 amt = p - h->root.root.string;
4456 shortname = bfd_malloc (amt + 1);
4457 memcpy (shortname, h->root.root.string, amt);
4458 shortname[amt] = '\0';
4460 hi = (struct elf_link_hash_entry *)
4461 bfd_link_hash_lookup (&htab->root, shortname,
4462 FALSE, FALSE, FALSE);
4464 && hi->root.type == h->root.type
4465 && hi->root.u.def.value == h->root.u.def.value
4466 && hi->root.u.def.section == h->root.u.def.section)
4468 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4469 hi->root.type = bfd_link_hash_indirect;
4470 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4471 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4472 sym_hash = elf_sym_hashes (abfd);
4474 for (symidx = 0; symidx < extsymcount; ++symidx)
4475 if (sym_hash[symidx] == hi)
4477 sym_hash[symidx] = h;
4483 free (nondeflt_vers);
4484 nondeflt_vers = NULL;
4487 /* Now set the weakdefs field correctly for all the weak defined
4488 symbols we found. The only way to do this is to search all the
4489 symbols. Since we only need the information for non functions in
4490 dynamic objects, that's the only time we actually put anything on
4491 the list WEAKS. We need this information so that if a regular
4492 object refers to a symbol defined weakly in a dynamic object, the
4493 real symbol in the dynamic object is also put in the dynamic
4494 symbols; we also must arrange for both symbols to point to the
4495 same memory location. We could handle the general case of symbol
4496 aliasing, but a general symbol alias can only be generated in
4497 assembler code, handling it correctly would be very time
4498 consuming, and other ELF linkers don't handle general aliasing
4502 struct elf_link_hash_entry **hpp;
4503 struct elf_link_hash_entry **hppend;
4504 struct elf_link_hash_entry **sorted_sym_hash;
4505 struct elf_link_hash_entry *h;
4508 /* Since we have to search the whole symbol list for each weak
4509 defined symbol, search time for N weak defined symbols will be
4510 O(N^2). Binary search will cut it down to O(NlogN). */
4511 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4512 sorted_sym_hash = bfd_malloc (amt);
4513 if (sorted_sym_hash == NULL)
4515 sym_hash = sorted_sym_hash;
4516 hpp = elf_sym_hashes (abfd);
4517 hppend = hpp + extsymcount;
4519 for (; hpp < hppend; hpp++)
4523 && h->root.type == bfd_link_hash_defined
4524 && !bed->is_function_type (h->type))
4532 qsort (sorted_sym_hash, sym_count,
4533 sizeof (struct elf_link_hash_entry *),
4536 while (weaks != NULL)
4538 struct elf_link_hash_entry *hlook;
4545 weaks = hlook->u.weakdef;
4546 hlook->u.weakdef = NULL;
4548 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4549 || hlook->root.type == bfd_link_hash_defweak
4550 || hlook->root.type == bfd_link_hash_common
4551 || hlook->root.type == bfd_link_hash_indirect);
4552 slook = hlook->root.u.def.section;
4553 vlook = hlook->root.u.def.value;
4560 bfd_signed_vma vdiff;
4562 h = sorted_sym_hash [idx];
4563 vdiff = vlook - h->root.u.def.value;
4570 long sdiff = slook->id - h->root.u.def.section->id;
4583 /* We didn't find a value/section match. */
4587 for (i = ilook; i < sym_count; i++)
4589 h = sorted_sym_hash [i];
4591 /* Stop if value or section doesn't match. */
4592 if (h->root.u.def.value != vlook
4593 || h->root.u.def.section != slook)
4595 else if (h != hlook)
4597 hlook->u.weakdef = h;
4599 /* If the weak definition is in the list of dynamic
4600 symbols, make sure the real definition is put
4602 if (hlook->dynindx != -1 && h->dynindx == -1)
4604 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4608 /* If the real definition is in the list of dynamic
4609 symbols, make sure the weak definition is put
4610 there as well. If we don't do this, then the
4611 dynamic loader might not merge the entries for the
4612 real definition and the weak definition. */
4613 if (h->dynindx != -1 && hlook->dynindx == -1)
4615 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4623 free (sorted_sym_hash);
4626 if (bed->check_directives)
4627 (*bed->check_directives) (abfd, info);
4629 /* If this object is the same format as the output object, and it is
4630 not a shared library, then let the backend look through the
4633 This is required to build global offset table entries and to
4634 arrange for dynamic relocs. It is not required for the
4635 particular common case of linking non PIC code, even when linking
4636 against shared libraries, but unfortunately there is no way of
4637 knowing whether an object file has been compiled PIC or not.
4638 Looking through the relocs is not particularly time consuming.
4639 The problem is that we must either (1) keep the relocs in memory,
4640 which causes the linker to require additional runtime memory or
4641 (2) read the relocs twice from the input file, which wastes time.
4642 This would be a good case for using mmap.
4644 I have no idea how to handle linking PIC code into a file of a
4645 different format. It probably can't be done. */
4647 && is_elf_hash_table (htab)
4648 && bed->check_relocs != NULL
4649 && (*bed->relocs_compatible) (abfd->xvec, htab->root.creator))
4653 for (o = abfd->sections; o != NULL; o = o->next)
4655 Elf_Internal_Rela *internal_relocs;
4658 if ((o->flags & SEC_RELOC) == 0
4659 || o->reloc_count == 0
4660 || ((info->strip == strip_all || info->strip == strip_debugger)
4661 && (o->flags & SEC_DEBUGGING) != 0)
4662 || bfd_is_abs_section (o->output_section))
4665 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4667 if (internal_relocs == NULL)
4670 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4672 if (elf_section_data (o)->relocs != internal_relocs)
4673 free (internal_relocs);
4680 /* If this is a non-traditional link, try to optimize the handling
4681 of the .stab/.stabstr sections. */
4683 && ! info->traditional_format
4684 && is_elf_hash_table (htab)
4685 && (info->strip != strip_all && info->strip != strip_debugger))
4689 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4690 if (stabstr != NULL)
4692 bfd_size_type string_offset = 0;
4695 for (stab = abfd->sections; stab; stab = stab->next)
4696 if (CONST_STRNEQ (stab->name, ".stab")
4697 && (!stab->name[5] ||
4698 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4699 && (stab->flags & SEC_MERGE) == 0
4700 && !bfd_is_abs_section (stab->output_section))
4702 struct bfd_elf_section_data *secdata;
4704 secdata = elf_section_data (stab);
4705 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4706 stabstr, &secdata->sec_info,
4709 if (secdata->sec_info)
4710 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4715 if (is_elf_hash_table (htab) && add_needed)
4717 /* Add this bfd to the loaded list. */
4718 struct elf_link_loaded_list *n;
4720 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4724 n->next = htab->loaded;
4731 if (old_tab != NULL)
4733 if (nondeflt_vers != NULL)
4734 free (nondeflt_vers);
4735 if (extversym != NULL)
4738 if (isymbuf != NULL)
4744 /* Return the linker hash table entry of a symbol that might be
4745 satisfied by an archive symbol. Return -1 on error. */
4747 struct elf_link_hash_entry *
4748 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4749 struct bfd_link_info *info,
4752 struct elf_link_hash_entry *h;
4756 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4760 /* If this is a default version (the name contains @@), look up the
4761 symbol again with only one `@' as well as without the version.
4762 The effect is that references to the symbol with and without the
4763 version will be matched by the default symbol in the archive. */
4765 p = strchr (name, ELF_VER_CHR);
4766 if (p == NULL || p[1] != ELF_VER_CHR)
4769 /* First check with only one `@'. */
4770 len = strlen (name);
4771 copy = bfd_alloc (abfd, len);
4773 return (struct elf_link_hash_entry *) 0 - 1;
4775 first = p - name + 1;
4776 memcpy (copy, name, first);
4777 memcpy (copy + first, name + first + 1, len - first);
4779 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4782 /* We also need to check references to the symbol without the
4784 copy[first - 1] = '\0';
4785 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4786 FALSE, FALSE, FALSE);
4789 bfd_release (abfd, copy);
4793 /* Add symbols from an ELF archive file to the linker hash table. We
4794 don't use _bfd_generic_link_add_archive_symbols because of a
4795 problem which arises on UnixWare. The UnixWare libc.so is an
4796 archive which includes an entry libc.so.1 which defines a bunch of
4797 symbols. The libc.so archive also includes a number of other
4798 object files, which also define symbols, some of which are the same
4799 as those defined in libc.so.1. Correct linking requires that we
4800 consider each object file in turn, and include it if it defines any
4801 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4802 this; it looks through the list of undefined symbols, and includes
4803 any object file which defines them. When this algorithm is used on
4804 UnixWare, it winds up pulling in libc.so.1 early and defining a
4805 bunch of symbols. This means that some of the other objects in the
4806 archive are not included in the link, which is incorrect since they
4807 precede libc.so.1 in the archive.
4809 Fortunately, ELF archive handling is simpler than that done by
4810 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4811 oddities. In ELF, if we find a symbol in the archive map, and the
4812 symbol is currently undefined, we know that we must pull in that
4815 Unfortunately, we do have to make multiple passes over the symbol
4816 table until nothing further is resolved. */
4819 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4822 bfd_boolean *defined = NULL;
4823 bfd_boolean *included = NULL;
4827 const struct elf_backend_data *bed;
4828 struct elf_link_hash_entry * (*archive_symbol_lookup)
4829 (bfd *, struct bfd_link_info *, const char *);
4831 if (! bfd_has_map (abfd))
4833 /* An empty archive is a special case. */
4834 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4836 bfd_set_error (bfd_error_no_armap);
4840 /* Keep track of all symbols we know to be already defined, and all
4841 files we know to be already included. This is to speed up the
4842 second and subsequent passes. */
4843 c = bfd_ardata (abfd)->symdef_count;
4847 amt *= sizeof (bfd_boolean);
4848 defined = bfd_zmalloc (amt);
4849 included = bfd_zmalloc (amt);
4850 if (defined == NULL || included == NULL)
4853 symdefs = bfd_ardata (abfd)->symdefs;
4854 bed = get_elf_backend_data (abfd);
4855 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4868 symdefend = symdef + c;
4869 for (i = 0; symdef < symdefend; symdef++, i++)
4871 struct elf_link_hash_entry *h;
4873 struct bfd_link_hash_entry *undefs_tail;
4876 if (defined[i] || included[i])
4878 if (symdef->file_offset == last)
4884 h = archive_symbol_lookup (abfd, info, symdef->name);
4885 if (h == (struct elf_link_hash_entry *) 0 - 1)
4891 if (h->root.type == bfd_link_hash_common)
4893 /* We currently have a common symbol. The archive map contains
4894 a reference to this symbol, so we may want to include it. We
4895 only want to include it however, if this archive element
4896 contains a definition of the symbol, not just another common
4899 Unfortunately some archivers (including GNU ar) will put
4900 declarations of common symbols into their archive maps, as
4901 well as real definitions, so we cannot just go by the archive
4902 map alone. Instead we must read in the element's symbol
4903 table and check that to see what kind of symbol definition
4905 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4908 else if (h->root.type != bfd_link_hash_undefined)
4910 if (h->root.type != bfd_link_hash_undefweak)
4915 /* We need to include this archive member. */
4916 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4917 if (element == NULL)
4920 if (! bfd_check_format (element, bfd_object))
4923 /* Doublecheck that we have not included this object
4924 already--it should be impossible, but there may be
4925 something wrong with the archive. */
4926 if (element->archive_pass != 0)
4928 bfd_set_error (bfd_error_bad_value);
4931 element->archive_pass = 1;
4933 undefs_tail = info->hash->undefs_tail;
4935 if (! (*info->callbacks->add_archive_element) (info, element,
4938 if (! bfd_link_add_symbols (element, info))
4941 /* If there are any new undefined symbols, we need to make
4942 another pass through the archive in order to see whether
4943 they can be defined. FIXME: This isn't perfect, because
4944 common symbols wind up on undefs_tail and because an
4945 undefined symbol which is defined later on in this pass
4946 does not require another pass. This isn't a bug, but it
4947 does make the code less efficient than it could be. */
4948 if (undefs_tail != info->hash->undefs_tail)
4951 /* Look backward to mark all symbols from this object file
4952 which we have already seen in this pass. */
4956 included[mark] = TRUE;
4961 while (symdefs[mark].file_offset == symdef->file_offset);
4963 /* We mark subsequent symbols from this object file as we go
4964 on through the loop. */
4965 last = symdef->file_offset;
4976 if (defined != NULL)
4978 if (included != NULL)
4983 /* Given an ELF BFD, add symbols to the global hash table as
4987 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4989 switch (bfd_get_format (abfd))
4992 return elf_link_add_object_symbols (abfd, info);
4994 return elf_link_add_archive_symbols (abfd, info);
4996 bfd_set_error (bfd_error_wrong_format);
5001 /* This function will be called though elf_link_hash_traverse to store
5002 all hash value of the exported symbols in an array. */
5005 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5007 unsigned long **valuep = data;
5013 if (h->root.type == bfd_link_hash_warning)
5014 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5016 /* Ignore indirect symbols. These are added by the versioning code. */
5017 if (h->dynindx == -1)
5020 name = h->root.root.string;
5021 p = strchr (name, ELF_VER_CHR);
5024 alc = bfd_malloc (p - name + 1);
5025 memcpy (alc, name, p - name);
5026 alc[p - name] = '\0';
5030 /* Compute the hash value. */
5031 ha = bfd_elf_hash (name);
5033 /* Store the found hash value in the array given as the argument. */
5036 /* And store it in the struct so that we can put it in the hash table
5038 h->u.elf_hash_value = ha;
5046 struct collect_gnu_hash_codes
5049 const struct elf_backend_data *bed;
5050 unsigned long int nsyms;
5051 unsigned long int maskbits;
5052 unsigned long int *hashcodes;
5053 unsigned long int *hashval;
5054 unsigned long int *indx;
5055 unsigned long int *counts;
5058 long int min_dynindx;
5059 unsigned long int bucketcount;
5060 unsigned long int symindx;
5061 long int local_indx;
5062 long int shift1, shift2;
5063 unsigned long int mask;
5066 /* This function will be called though elf_link_hash_traverse to store
5067 all hash value of the exported symbols in an array. */
5070 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5072 struct collect_gnu_hash_codes *s = data;
5078 if (h->root.type == bfd_link_hash_warning)
5079 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5081 /* Ignore indirect symbols. These are added by the versioning code. */
5082 if (h->dynindx == -1)
5085 /* Ignore also local symbols and undefined symbols. */
5086 if (! (*s->bed->elf_hash_symbol) (h))
5089 name = h->root.root.string;
5090 p = strchr (name, ELF_VER_CHR);
5093 alc = bfd_malloc (p - name + 1);
5094 memcpy (alc, name, p - name);
5095 alc[p - name] = '\0';
5099 /* Compute the hash value. */
5100 ha = bfd_elf_gnu_hash (name);
5102 /* Store the found hash value in the array for compute_bucket_count,
5103 and also for .dynsym reordering purposes. */
5104 s->hashcodes[s->nsyms] = ha;
5105 s->hashval[h->dynindx] = ha;
5107 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5108 s->min_dynindx = h->dynindx;
5116 /* This function will be called though elf_link_hash_traverse to do
5117 final dynaminc symbol renumbering. */
5120 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5122 struct collect_gnu_hash_codes *s = data;
5123 unsigned long int bucket;
5124 unsigned long int val;
5126 if (h->root.type == bfd_link_hash_warning)
5127 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5129 /* Ignore indirect symbols. */
5130 if (h->dynindx == -1)
5133 /* Ignore also local symbols and undefined symbols. */
5134 if (! (*s->bed->elf_hash_symbol) (h))
5136 if (h->dynindx >= s->min_dynindx)
5137 h->dynindx = s->local_indx++;
5141 bucket = s->hashval[h->dynindx] % s->bucketcount;
5142 val = (s->hashval[h->dynindx] >> s->shift1)
5143 & ((s->maskbits >> s->shift1) - 1);
5144 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5146 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5147 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5148 if (s->counts[bucket] == 1)
5149 /* Last element terminates the chain. */
5151 bfd_put_32 (s->output_bfd, val,
5152 s->contents + (s->indx[bucket] - s->symindx) * 4);
5153 --s->counts[bucket];
5154 h->dynindx = s->indx[bucket]++;
5158 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5161 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5163 return !(h->forced_local
5164 || h->root.type == bfd_link_hash_undefined
5165 || h->root.type == bfd_link_hash_undefweak
5166 || ((h->root.type == bfd_link_hash_defined
5167 || h->root.type == bfd_link_hash_defweak)
5168 && h->root.u.def.section->output_section == NULL));
5171 /* Array used to determine the number of hash table buckets to use
5172 based on the number of symbols there are. If there are fewer than
5173 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5174 fewer than 37 we use 17 buckets, and so forth. We never use more
5175 than 32771 buckets. */
5177 static const size_t elf_buckets[] =
5179 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5183 /* Compute bucket count for hashing table. We do not use a static set
5184 of possible tables sizes anymore. Instead we determine for all
5185 possible reasonable sizes of the table the outcome (i.e., the
5186 number of collisions etc) and choose the best solution. The
5187 weighting functions are not too simple to allow the table to grow
5188 without bounds. Instead one of the weighting factors is the size.
5189 Therefore the result is always a good payoff between few collisions
5190 (= short chain lengths) and table size. */
5192 compute_bucket_count (struct bfd_link_info *info, unsigned long int *hashcodes,
5193 unsigned long int nsyms, int gnu_hash)
5195 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5196 size_t best_size = 0;
5197 unsigned long int i;
5200 /* We have a problem here. The following code to optimize the table
5201 size requires an integer type with more the 32 bits. If
5202 BFD_HOST_U_64_BIT is set we know about such a type. */
5203 #ifdef BFD_HOST_U_64_BIT
5208 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5209 bfd *dynobj = elf_hash_table (info)->dynobj;
5210 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5211 unsigned long int *counts;
5213 /* Possible optimization parameters: if we have NSYMS symbols we say
5214 that the hashing table must at least have NSYMS/4 and at most
5216 minsize = nsyms / 4;
5219 best_size = maxsize = nsyms * 2;
5224 if ((best_size & 31) == 0)
5228 /* Create array where we count the collisions in. We must use bfd_malloc
5229 since the size could be large. */
5231 amt *= sizeof (unsigned long int);
5232 counts = bfd_malloc (amt);
5236 /* Compute the "optimal" size for the hash table. The criteria is a
5237 minimal chain length. The minor criteria is (of course) the size
5239 for (i = minsize; i < maxsize; ++i)
5241 /* Walk through the array of hashcodes and count the collisions. */
5242 BFD_HOST_U_64_BIT max;
5243 unsigned long int j;
5244 unsigned long int fact;
5246 if (gnu_hash && (i & 31) == 0)
5249 memset (counts, '\0', i * sizeof (unsigned long int));
5251 /* Determine how often each hash bucket is used. */
5252 for (j = 0; j < nsyms; ++j)
5253 ++counts[hashcodes[j] % i];
5255 /* For the weight function we need some information about the
5256 pagesize on the target. This is information need not be 100%
5257 accurate. Since this information is not available (so far) we
5258 define it here to a reasonable default value. If it is crucial
5259 to have a better value some day simply define this value. */
5260 # ifndef BFD_TARGET_PAGESIZE
5261 # define BFD_TARGET_PAGESIZE (4096)
5264 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5266 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5269 /* Variant 1: optimize for short chains. We add the squares
5270 of all the chain lengths (which favors many small chain
5271 over a few long chains). */
5272 for (j = 0; j < i; ++j)
5273 max += counts[j] * counts[j];
5275 /* This adds penalties for the overall size of the table. */
5276 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5279 /* Variant 2: Optimize a lot more for small table. Here we
5280 also add squares of the size but we also add penalties for
5281 empty slots (the +1 term). */
5282 for (j = 0; j < i; ++j)
5283 max += (1 + counts[j]) * (1 + counts[j]);
5285 /* The overall size of the table is considered, but not as
5286 strong as in variant 1, where it is squared. */
5287 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5291 /* Compare with current best results. */
5292 if (max < best_chlen)
5302 #endif /* defined (BFD_HOST_U_64_BIT) */
5304 /* This is the fallback solution if no 64bit type is available or if we
5305 are not supposed to spend much time on optimizations. We select the
5306 bucket count using a fixed set of numbers. */
5307 for (i = 0; elf_buckets[i] != 0; i++)
5309 best_size = elf_buckets[i];
5310 if (nsyms < elf_buckets[i + 1])
5313 if (gnu_hash && best_size < 2)
5320 /* Set up the sizes and contents of the ELF dynamic sections. This is
5321 called by the ELF linker emulation before_allocation routine. We
5322 must set the sizes of the sections before the linker sets the
5323 addresses of the various sections. */
5326 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5329 const char *filter_shlib,
5330 const char * const *auxiliary_filters,
5331 struct bfd_link_info *info,
5332 asection **sinterpptr,
5333 struct bfd_elf_version_tree *verdefs)
5335 bfd_size_type soname_indx;
5337 const struct elf_backend_data *bed;
5338 struct elf_assign_sym_version_info asvinfo;
5342 soname_indx = (bfd_size_type) -1;
5344 if (!is_elf_hash_table (info->hash))
5347 bed = get_elf_backend_data (output_bfd);
5348 elf_tdata (output_bfd)->relro = info->relro;
5349 if (info->execstack)
5350 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5351 else if (info->noexecstack)
5352 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5356 asection *notesec = NULL;
5359 for (inputobj = info->input_bfds;
5361 inputobj = inputobj->link_next)
5365 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
5367 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5370 if (s->flags & SEC_CODE)
5374 else if (bed->default_execstack)
5379 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5380 if (exec && info->relocatable
5381 && notesec->output_section != bfd_abs_section_ptr)
5382 notesec->output_section->flags |= SEC_CODE;
5386 /* Any syms created from now on start with -1 in
5387 got.refcount/offset and plt.refcount/offset. */
5388 elf_hash_table (info)->init_got_refcount
5389 = elf_hash_table (info)->init_got_offset;
5390 elf_hash_table (info)->init_plt_refcount
5391 = elf_hash_table (info)->init_plt_offset;
5393 /* The backend may have to create some sections regardless of whether
5394 we're dynamic or not. */
5395 if (bed->elf_backend_always_size_sections
5396 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5399 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5402 dynobj = elf_hash_table (info)->dynobj;
5404 /* If there were no dynamic objects in the link, there is nothing to
5409 if (elf_hash_table (info)->dynamic_sections_created)
5411 struct elf_info_failed eif;
5412 struct elf_link_hash_entry *h;
5414 struct bfd_elf_version_tree *t;
5415 struct bfd_elf_version_expr *d;
5417 bfd_boolean all_defined;
5419 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5420 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5424 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5426 if (soname_indx == (bfd_size_type) -1
5427 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5433 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5435 info->flags |= DF_SYMBOLIC;
5442 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5444 if (indx == (bfd_size_type) -1
5445 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5448 if (info->new_dtags)
5450 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5451 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5456 if (filter_shlib != NULL)
5460 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5461 filter_shlib, TRUE);
5462 if (indx == (bfd_size_type) -1
5463 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5467 if (auxiliary_filters != NULL)
5469 const char * const *p;
5471 for (p = auxiliary_filters; *p != NULL; p++)
5475 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5477 if (indx == (bfd_size_type) -1
5478 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5484 eif.verdefs = verdefs;
5487 /* If we are supposed to export all symbols into the dynamic symbol
5488 table (this is not the normal case), then do so. */
5489 if (info->export_dynamic
5490 || (info->executable && info->dynamic))
5492 elf_link_hash_traverse (elf_hash_table (info),
5493 _bfd_elf_export_symbol,
5499 /* Make all global versions with definition. */
5500 for (t = verdefs; t != NULL; t = t->next)
5501 for (d = t->globals.list; d != NULL; d = d->next)
5502 if (!d->symver && d->symbol)
5504 const char *verstr, *name;
5505 size_t namelen, verlen, newlen;
5507 struct elf_link_hash_entry *newh;
5510 namelen = strlen (name);
5512 verlen = strlen (verstr);
5513 newlen = namelen + verlen + 3;
5515 newname = bfd_malloc (newlen);
5516 if (newname == NULL)
5518 memcpy (newname, name, namelen);
5520 /* Check the hidden versioned definition. */
5521 p = newname + namelen;
5523 memcpy (p, verstr, verlen + 1);
5524 newh = elf_link_hash_lookup (elf_hash_table (info),
5525 newname, FALSE, FALSE,
5528 || (newh->root.type != bfd_link_hash_defined
5529 && newh->root.type != bfd_link_hash_defweak))
5531 /* Check the default versioned definition. */
5533 memcpy (p, verstr, verlen + 1);
5534 newh = elf_link_hash_lookup (elf_hash_table (info),
5535 newname, FALSE, FALSE,
5540 /* Mark this version if there is a definition and it is
5541 not defined in a shared object. */
5543 && !newh->def_dynamic
5544 && (newh->root.type == bfd_link_hash_defined
5545 || newh->root.type == bfd_link_hash_defweak))
5549 /* Attach all the symbols to their version information. */
5550 asvinfo.output_bfd = output_bfd;
5551 asvinfo.info = info;
5552 asvinfo.verdefs = verdefs;
5553 asvinfo.failed = FALSE;
5555 elf_link_hash_traverse (elf_hash_table (info),
5556 _bfd_elf_link_assign_sym_version,
5561 if (!info->allow_undefined_version)
5563 /* Check if all global versions have a definition. */
5565 for (t = verdefs; t != NULL; t = t->next)
5566 for (d = t->globals.list; d != NULL; d = d->next)
5567 if (!d->symver && !d->script)
5569 (*_bfd_error_handler)
5570 (_("%s: undefined version: %s"),
5571 d->pattern, t->name);
5572 all_defined = FALSE;
5577 bfd_set_error (bfd_error_bad_value);
5582 /* Find all symbols which were defined in a dynamic object and make
5583 the backend pick a reasonable value for them. */
5584 elf_link_hash_traverse (elf_hash_table (info),
5585 _bfd_elf_adjust_dynamic_symbol,
5590 /* Add some entries to the .dynamic section. We fill in some of the
5591 values later, in bfd_elf_final_link, but we must add the entries
5592 now so that we know the final size of the .dynamic section. */
5594 /* If there are initialization and/or finalization functions to
5595 call then add the corresponding DT_INIT/DT_FINI entries. */
5596 h = (info->init_function
5597 ? elf_link_hash_lookup (elf_hash_table (info),
5598 info->init_function, FALSE,
5605 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5608 h = (info->fini_function
5609 ? elf_link_hash_lookup (elf_hash_table (info),
5610 info->fini_function, FALSE,
5617 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5621 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5622 if (s != NULL && s->linker_has_input)
5624 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5625 if (! info->executable)
5630 for (sub = info->input_bfds; sub != NULL;
5631 sub = sub->link_next)
5632 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5633 for (o = sub->sections; o != NULL; o = o->next)
5634 if (elf_section_data (o)->this_hdr.sh_type
5635 == SHT_PREINIT_ARRAY)
5637 (*_bfd_error_handler)
5638 (_("%B: .preinit_array section is not allowed in DSO"),
5643 bfd_set_error (bfd_error_nonrepresentable_section);
5647 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5648 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5651 s = bfd_get_section_by_name (output_bfd, ".init_array");
5652 if (s != NULL && s->linker_has_input)
5654 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5655 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5658 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5659 if (s != NULL && s->linker_has_input)
5661 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5662 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5666 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5667 /* If .dynstr is excluded from the link, we don't want any of
5668 these tags. Strictly, we should be checking each section
5669 individually; This quick check covers for the case where
5670 someone does a /DISCARD/ : { *(*) }. */
5671 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5673 bfd_size_type strsize;
5675 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5676 if ((info->emit_hash
5677 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5678 || (info->emit_gnu_hash
5679 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5680 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5681 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5682 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5683 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5684 bed->s->sizeof_sym))
5689 /* The backend must work out the sizes of all the other dynamic
5691 if (bed->elf_backend_size_dynamic_sections
5692 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5695 if (elf_hash_table (info)->dynamic_sections_created)
5697 unsigned long section_sym_count;
5700 /* Set up the version definition section. */
5701 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5702 BFD_ASSERT (s != NULL);
5704 /* We may have created additional version definitions if we are
5705 just linking a regular application. */
5706 verdefs = asvinfo.verdefs;
5708 /* Skip anonymous version tag. */
5709 if (verdefs != NULL && verdefs->vernum == 0)
5710 verdefs = verdefs->next;
5712 if (verdefs == NULL && !info->create_default_symver)
5713 s->flags |= SEC_EXCLUDE;
5718 struct bfd_elf_version_tree *t;
5720 Elf_Internal_Verdef def;
5721 Elf_Internal_Verdaux defaux;
5722 struct bfd_link_hash_entry *bh;
5723 struct elf_link_hash_entry *h;
5729 /* Make space for the base version. */
5730 size += sizeof (Elf_External_Verdef);
5731 size += sizeof (Elf_External_Verdaux);
5734 /* Make space for the default version. */
5735 if (info->create_default_symver)
5737 size += sizeof (Elf_External_Verdef);
5741 for (t = verdefs; t != NULL; t = t->next)
5743 struct bfd_elf_version_deps *n;
5745 size += sizeof (Elf_External_Verdef);
5746 size += sizeof (Elf_External_Verdaux);
5749 for (n = t->deps; n != NULL; n = n->next)
5750 size += sizeof (Elf_External_Verdaux);
5754 s->contents = bfd_alloc (output_bfd, s->size);
5755 if (s->contents == NULL && s->size != 0)
5758 /* Fill in the version definition section. */
5762 def.vd_version = VER_DEF_CURRENT;
5763 def.vd_flags = VER_FLG_BASE;
5766 if (info->create_default_symver)
5768 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5769 def.vd_next = sizeof (Elf_External_Verdef);
5773 def.vd_aux = sizeof (Elf_External_Verdef);
5774 def.vd_next = (sizeof (Elf_External_Verdef)
5775 + sizeof (Elf_External_Verdaux));
5778 if (soname_indx != (bfd_size_type) -1)
5780 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5782 def.vd_hash = bfd_elf_hash (soname);
5783 defaux.vda_name = soname_indx;
5790 name = lbasename (output_bfd->filename);
5791 def.vd_hash = bfd_elf_hash (name);
5792 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5794 if (indx == (bfd_size_type) -1)
5796 defaux.vda_name = indx;
5798 defaux.vda_next = 0;
5800 _bfd_elf_swap_verdef_out (output_bfd, &def,
5801 (Elf_External_Verdef *) p);
5802 p += sizeof (Elf_External_Verdef);
5803 if (info->create_default_symver)
5805 /* Add a symbol representing this version. */
5807 if (! (_bfd_generic_link_add_one_symbol
5808 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5810 get_elf_backend_data (dynobj)->collect, &bh)))
5812 h = (struct elf_link_hash_entry *) bh;
5815 h->type = STT_OBJECT;
5816 h->verinfo.vertree = NULL;
5818 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5821 /* Create a duplicate of the base version with the same
5822 aux block, but different flags. */
5825 def.vd_aux = sizeof (Elf_External_Verdef);
5827 def.vd_next = (sizeof (Elf_External_Verdef)
5828 + sizeof (Elf_External_Verdaux));
5831 _bfd_elf_swap_verdef_out (output_bfd, &def,
5832 (Elf_External_Verdef *) p);
5833 p += sizeof (Elf_External_Verdef);
5835 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5836 (Elf_External_Verdaux *) p);
5837 p += sizeof (Elf_External_Verdaux);
5839 for (t = verdefs; t != NULL; t = t->next)
5842 struct bfd_elf_version_deps *n;
5845 for (n = t->deps; n != NULL; n = n->next)
5848 /* Add a symbol representing this version. */
5850 if (! (_bfd_generic_link_add_one_symbol
5851 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5853 get_elf_backend_data (dynobj)->collect, &bh)))
5855 h = (struct elf_link_hash_entry *) bh;
5858 h->type = STT_OBJECT;
5859 h->verinfo.vertree = t;
5861 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5864 def.vd_version = VER_DEF_CURRENT;
5866 if (t->globals.list == NULL
5867 && t->locals.list == NULL
5869 def.vd_flags |= VER_FLG_WEAK;
5870 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5871 def.vd_cnt = cdeps + 1;
5872 def.vd_hash = bfd_elf_hash (t->name);
5873 def.vd_aux = sizeof (Elf_External_Verdef);
5875 if (t->next != NULL)
5876 def.vd_next = (sizeof (Elf_External_Verdef)
5877 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5879 _bfd_elf_swap_verdef_out (output_bfd, &def,
5880 (Elf_External_Verdef *) p);
5881 p += sizeof (Elf_External_Verdef);
5883 defaux.vda_name = h->dynstr_index;
5884 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5886 defaux.vda_next = 0;
5887 if (t->deps != NULL)
5888 defaux.vda_next = sizeof (Elf_External_Verdaux);
5889 t->name_indx = defaux.vda_name;
5891 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5892 (Elf_External_Verdaux *) p);
5893 p += sizeof (Elf_External_Verdaux);
5895 for (n = t->deps; n != NULL; n = n->next)
5897 if (n->version_needed == NULL)
5899 /* This can happen if there was an error in the
5901 defaux.vda_name = 0;
5905 defaux.vda_name = n->version_needed->name_indx;
5906 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5909 if (n->next == NULL)
5910 defaux.vda_next = 0;
5912 defaux.vda_next = sizeof (Elf_External_Verdaux);
5914 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5915 (Elf_External_Verdaux *) p);
5916 p += sizeof (Elf_External_Verdaux);
5920 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5921 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5924 elf_tdata (output_bfd)->cverdefs = cdefs;
5927 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5929 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5932 else if (info->flags & DF_BIND_NOW)
5934 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5940 if (info->executable)
5941 info->flags_1 &= ~ (DF_1_INITFIRST
5944 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5948 /* Work out the size of the version reference section. */
5950 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5951 BFD_ASSERT (s != NULL);
5953 struct elf_find_verdep_info sinfo;
5955 sinfo.output_bfd = output_bfd;
5957 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5958 if (sinfo.vers == 0)
5960 sinfo.failed = FALSE;
5962 elf_link_hash_traverse (elf_hash_table (info),
5963 _bfd_elf_link_find_version_dependencies,
5966 if (elf_tdata (output_bfd)->verref == NULL)
5967 s->flags |= SEC_EXCLUDE;
5970 Elf_Internal_Verneed *t;
5975 /* Build the version definition section. */
5978 for (t = elf_tdata (output_bfd)->verref;
5982 Elf_Internal_Vernaux *a;
5984 size += sizeof (Elf_External_Verneed);
5986 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5987 size += sizeof (Elf_External_Vernaux);
5991 s->contents = bfd_alloc (output_bfd, s->size);
5992 if (s->contents == NULL)
5996 for (t = elf_tdata (output_bfd)->verref;
6001 Elf_Internal_Vernaux *a;
6005 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6008 t->vn_version = VER_NEED_CURRENT;
6010 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6011 elf_dt_name (t->vn_bfd) != NULL
6012 ? elf_dt_name (t->vn_bfd)
6013 : lbasename (t->vn_bfd->filename),
6015 if (indx == (bfd_size_type) -1)
6018 t->vn_aux = sizeof (Elf_External_Verneed);
6019 if (t->vn_nextref == NULL)
6022 t->vn_next = (sizeof (Elf_External_Verneed)
6023 + caux * sizeof (Elf_External_Vernaux));
6025 _bfd_elf_swap_verneed_out (output_bfd, t,
6026 (Elf_External_Verneed *) p);
6027 p += sizeof (Elf_External_Verneed);
6029 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6031 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6032 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6033 a->vna_nodename, FALSE);
6034 if (indx == (bfd_size_type) -1)
6037 if (a->vna_nextptr == NULL)
6040 a->vna_next = sizeof (Elf_External_Vernaux);
6042 _bfd_elf_swap_vernaux_out (output_bfd, a,
6043 (Elf_External_Vernaux *) p);
6044 p += sizeof (Elf_External_Vernaux);
6048 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6049 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6052 elf_tdata (output_bfd)->cverrefs = crefs;
6056 if ((elf_tdata (output_bfd)->cverrefs == 0
6057 && elf_tdata (output_bfd)->cverdefs == 0)
6058 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6059 §ion_sym_count) == 0)
6061 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6062 s->flags |= SEC_EXCLUDE;
6068 /* Find the first non-excluded output section. We'll use its
6069 section symbol for some emitted relocs. */
6071 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6075 for (s = output_bfd->sections; s != NULL; s = s->next)
6076 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6077 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6079 elf_hash_table (info)->text_index_section = s;
6084 /* Find two non-excluded output sections, one for code, one for data.
6085 We'll use their section symbols for some emitted relocs. */
6087 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6091 for (s = output_bfd->sections; s != NULL; s = s->next)
6092 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6093 == (SEC_ALLOC | SEC_READONLY))
6094 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6096 elf_hash_table (info)->text_index_section = s;
6100 for (s = output_bfd->sections; s != NULL; s = s->next)
6101 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6102 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6104 elf_hash_table (info)->data_index_section = s;
6108 if (elf_hash_table (info)->text_index_section == NULL)
6109 elf_hash_table (info)->text_index_section
6110 = elf_hash_table (info)->data_index_section;
6114 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6116 const struct elf_backend_data *bed;
6118 if (!is_elf_hash_table (info->hash))
6121 bed = get_elf_backend_data (output_bfd);
6122 (*bed->elf_backend_init_index_section) (output_bfd, info);
6124 if (elf_hash_table (info)->dynamic_sections_created)
6128 bfd_size_type dynsymcount;
6129 unsigned long section_sym_count;
6130 unsigned int dtagcount;
6132 dynobj = elf_hash_table (info)->dynobj;
6134 /* Assign dynsym indicies. In a shared library we generate a
6135 section symbol for each output section, which come first.
6136 Next come all of the back-end allocated local dynamic syms,
6137 followed by the rest of the global symbols. */
6139 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6140 §ion_sym_count);
6142 /* Work out the size of the symbol version section. */
6143 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6144 BFD_ASSERT (s != NULL);
6145 if (dynsymcount != 0
6146 && (s->flags & SEC_EXCLUDE) == 0)
6148 s->size = dynsymcount * sizeof (Elf_External_Versym);
6149 s->contents = bfd_zalloc (output_bfd, s->size);
6150 if (s->contents == NULL)
6153 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6157 /* Set the size of the .dynsym and .hash sections. We counted
6158 the number of dynamic symbols in elf_link_add_object_symbols.
6159 We will build the contents of .dynsym and .hash when we build
6160 the final symbol table, because until then we do not know the
6161 correct value to give the symbols. We built the .dynstr
6162 section as we went along in elf_link_add_object_symbols. */
6163 s = bfd_get_section_by_name (dynobj, ".dynsym");
6164 BFD_ASSERT (s != NULL);
6165 s->size = dynsymcount * bed->s->sizeof_sym;
6167 if (dynsymcount != 0)
6169 s->contents = bfd_alloc (output_bfd, s->size);
6170 if (s->contents == NULL)
6173 /* The first entry in .dynsym is a dummy symbol.
6174 Clear all the section syms, in case we don't output them all. */
6175 ++section_sym_count;
6176 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6179 elf_hash_table (info)->bucketcount = 0;
6181 /* Compute the size of the hashing table. As a side effect this
6182 computes the hash values for all the names we export. */
6183 if (info->emit_hash)
6185 unsigned long int *hashcodes;
6186 unsigned long int *hashcodesp;
6188 unsigned long int nsyms;
6190 size_t hash_entry_size;
6192 /* Compute the hash values for all exported symbols. At the same
6193 time store the values in an array so that we could use them for
6195 amt = dynsymcount * sizeof (unsigned long int);
6196 hashcodes = bfd_malloc (amt);
6197 if (hashcodes == NULL)
6199 hashcodesp = hashcodes;
6201 /* Put all hash values in HASHCODES. */
6202 elf_link_hash_traverse (elf_hash_table (info),
6203 elf_collect_hash_codes, &hashcodesp);
6205 nsyms = hashcodesp - hashcodes;
6207 = compute_bucket_count (info, hashcodes, nsyms, 0);
6210 if (bucketcount == 0)
6213 elf_hash_table (info)->bucketcount = bucketcount;
6215 s = bfd_get_section_by_name (dynobj, ".hash");
6216 BFD_ASSERT (s != NULL);
6217 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6218 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6219 s->contents = bfd_zalloc (output_bfd, s->size);
6220 if (s->contents == NULL)
6223 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6224 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6225 s->contents + hash_entry_size);
6228 if (info->emit_gnu_hash)
6231 unsigned char *contents;
6232 struct collect_gnu_hash_codes cinfo;
6236 memset (&cinfo, 0, sizeof (cinfo));
6238 /* Compute the hash values for all exported symbols. At the same
6239 time store the values in an array so that we could use them for
6241 amt = dynsymcount * 2 * sizeof (unsigned long int);
6242 cinfo.hashcodes = bfd_malloc (amt);
6243 if (cinfo.hashcodes == NULL)
6246 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6247 cinfo.min_dynindx = -1;
6248 cinfo.output_bfd = output_bfd;
6251 /* Put all hash values in HASHCODES. */
6252 elf_link_hash_traverse (elf_hash_table (info),
6253 elf_collect_gnu_hash_codes, &cinfo);
6256 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6258 if (bucketcount == 0)
6260 free (cinfo.hashcodes);
6264 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6265 BFD_ASSERT (s != NULL);
6267 if (cinfo.nsyms == 0)
6269 /* Empty .gnu.hash section is special. */
6270 BFD_ASSERT (cinfo.min_dynindx == -1);
6271 free (cinfo.hashcodes);
6272 s->size = 5 * 4 + bed->s->arch_size / 8;
6273 contents = bfd_zalloc (output_bfd, s->size);
6274 if (contents == NULL)
6276 s->contents = contents;
6277 /* 1 empty bucket. */
6278 bfd_put_32 (output_bfd, 1, contents);
6279 /* SYMIDX above the special symbol 0. */
6280 bfd_put_32 (output_bfd, 1, contents + 4);
6281 /* Just one word for bitmask. */
6282 bfd_put_32 (output_bfd, 1, contents + 8);
6283 /* Only hash fn bloom filter. */
6284 bfd_put_32 (output_bfd, 0, contents + 12);
6285 /* No hashes are valid - empty bitmask. */
6286 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6287 /* No hashes in the only bucket. */
6288 bfd_put_32 (output_bfd, 0,
6289 contents + 16 + bed->s->arch_size / 8);
6293 unsigned long int maskwords, maskbitslog2;
6294 BFD_ASSERT (cinfo.min_dynindx != -1);
6296 maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1;
6297 if (maskbitslog2 < 3)
6299 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6300 maskbitslog2 = maskbitslog2 + 3;
6302 maskbitslog2 = maskbitslog2 + 2;
6303 if (bed->s->arch_size == 64)
6305 if (maskbitslog2 == 5)
6311 cinfo.mask = (1 << cinfo.shift1) - 1;
6312 cinfo.shift2 = maskbitslog2;
6313 cinfo.maskbits = 1 << maskbitslog2;
6314 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6315 amt = bucketcount * sizeof (unsigned long int) * 2;
6316 amt += maskwords * sizeof (bfd_vma);
6317 cinfo.bitmask = bfd_malloc (amt);
6318 if (cinfo.bitmask == NULL)
6320 free (cinfo.hashcodes);
6324 cinfo.counts = (void *) (cinfo.bitmask + maskwords);
6325 cinfo.indx = cinfo.counts + bucketcount;
6326 cinfo.symindx = dynsymcount - cinfo.nsyms;
6327 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6329 /* Determine how often each hash bucket is used. */
6330 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6331 for (i = 0; i < cinfo.nsyms; ++i)
6332 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6334 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6335 if (cinfo.counts[i] != 0)
6337 cinfo.indx[i] = cnt;
6338 cnt += cinfo.counts[i];
6340 BFD_ASSERT (cnt == dynsymcount);
6341 cinfo.bucketcount = bucketcount;
6342 cinfo.local_indx = cinfo.min_dynindx;
6344 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6345 s->size += cinfo.maskbits / 8;
6346 contents = bfd_zalloc (output_bfd, s->size);
6347 if (contents == NULL)
6349 free (cinfo.bitmask);
6350 free (cinfo.hashcodes);
6354 s->contents = contents;
6355 bfd_put_32 (output_bfd, bucketcount, contents);
6356 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6357 bfd_put_32 (output_bfd, maskwords, contents + 8);
6358 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6359 contents += 16 + cinfo.maskbits / 8;
6361 for (i = 0; i < bucketcount; ++i)
6363 if (cinfo.counts[i] == 0)
6364 bfd_put_32 (output_bfd, 0, contents);
6366 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6370 cinfo.contents = contents;
6372 /* Renumber dynamic symbols, populate .gnu.hash section. */
6373 elf_link_hash_traverse (elf_hash_table (info),
6374 elf_renumber_gnu_hash_syms, &cinfo);
6376 contents = s->contents + 16;
6377 for (i = 0; i < maskwords; ++i)
6379 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6381 contents += bed->s->arch_size / 8;
6384 free (cinfo.bitmask);
6385 free (cinfo.hashcodes);
6389 s = bfd_get_section_by_name (dynobj, ".dynstr");
6390 BFD_ASSERT (s != NULL);
6392 elf_finalize_dynstr (output_bfd, info);
6394 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6396 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6397 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6404 /* Final phase of ELF linker. */
6406 /* A structure we use to avoid passing large numbers of arguments. */
6408 struct elf_final_link_info
6410 /* General link information. */
6411 struct bfd_link_info *info;
6414 /* Symbol string table. */
6415 struct bfd_strtab_hash *symstrtab;
6416 /* .dynsym section. */
6417 asection *dynsym_sec;
6418 /* .hash section. */
6420 /* symbol version section (.gnu.version). */
6421 asection *symver_sec;
6422 /* Buffer large enough to hold contents of any section. */
6424 /* Buffer large enough to hold external relocs of any section. */
6425 void *external_relocs;
6426 /* Buffer large enough to hold internal relocs of any section. */
6427 Elf_Internal_Rela *internal_relocs;
6428 /* Buffer large enough to hold external local symbols of any input
6430 bfd_byte *external_syms;
6431 /* And a buffer for symbol section indices. */
6432 Elf_External_Sym_Shndx *locsym_shndx;
6433 /* Buffer large enough to hold internal local symbols of any input
6435 Elf_Internal_Sym *internal_syms;
6436 /* Array large enough to hold a symbol index for each local symbol
6437 of any input BFD. */
6439 /* Array large enough to hold a section pointer for each local
6440 symbol of any input BFD. */
6441 asection **sections;
6442 /* Buffer to hold swapped out symbols. */
6444 /* And one for symbol section indices. */
6445 Elf_External_Sym_Shndx *symshndxbuf;
6446 /* Number of swapped out symbols in buffer. */
6447 size_t symbuf_count;
6448 /* Number of symbols which fit in symbuf. */
6450 /* And same for symshndxbuf. */
6451 size_t shndxbuf_size;
6454 /* This struct is used to pass information to elf_link_output_extsym. */
6456 struct elf_outext_info
6459 bfd_boolean localsyms;
6460 struct elf_final_link_info *finfo;
6464 /* Support for evaluating a complex relocation.
6466 Complex relocations are generalized, self-describing relocations. The
6467 implementation of them consists of two parts: complex symbols, and the
6468 relocations themselves.
6470 The relocations are use a reserved elf-wide relocation type code (R_RELC
6471 external / BFD_RELOC_RELC internal) and an encoding of relocation field
6472 information (start bit, end bit, word width, etc) into the addend. This
6473 information is extracted from CGEN-generated operand tables within gas.
6475 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
6476 internal) representing prefix-notation expressions, including but not
6477 limited to those sorts of expressions normally encoded as addends in the
6478 addend field. The symbol mangling format is:
6481 | <unary-operator> ':' <node>
6482 | <binary-operator> ':' <node> ':' <node>
6485 <literal> := 's' <digits=N> ':' <N character symbol name>
6486 | 'S' <digits=N> ':' <N character section name>
6490 <binary-operator> := as in C
6491 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
6494 set_symbol_value (bfd * bfd_with_globals,
6495 struct elf_final_link_info * finfo,
6499 bfd_boolean is_local;
6500 Elf_Internal_Sym * sym;
6501 struct elf_link_hash_entry ** sym_hashes;
6502 struct elf_link_hash_entry * h;
6504 sym_hashes = elf_sym_hashes (bfd_with_globals);
6505 sym = finfo->internal_syms + symidx;
6506 is_local = ELF_ST_BIND(sym->st_info) == STB_LOCAL;
6510 /* It is a local symbol: move it to the
6511 "absolute" section and give it a value. */
6512 sym->st_shndx = SHN_ABS;
6513 sym->st_value = val;
6517 /* It is a global symbol: set its link type
6518 to "defined" and give it a value. */
6519 h = sym_hashes [symidx];
6520 while (h->root.type == bfd_link_hash_indirect
6521 || h->root.type == bfd_link_hash_warning)
6522 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6523 h->root.type = bfd_link_hash_defined;
6524 h->root.u.def.value = val;
6525 h->root.u.def.section = bfd_abs_section_ptr;
6530 resolve_symbol (const char * name,
6532 struct elf_final_link_info * finfo,
6536 Elf_Internal_Sym * sym;
6537 struct bfd_link_hash_entry * global_entry;
6538 const char * candidate = NULL;
6539 Elf_Internal_Shdr * symtab_hdr;
6540 asection * sec = NULL;
6543 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
6545 for (i = 0; i < locsymcount; ++ i)
6547 sym = finfo->internal_syms + i;
6548 sec = finfo->sections [i];
6550 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
6553 candidate = bfd_elf_string_from_elf_section (input_bfd,
6554 symtab_hdr->sh_link,
6557 printf ("Comparing string: '%s' vs. '%s' = 0x%x\n",
6558 name, candidate, (unsigned int)sym->st_value);
6560 if (candidate && strcmp (candidate, name) == 0)
6562 * result = sym->st_value;
6564 if (sym->st_shndx > SHN_UNDEF &&
6565 sym->st_shndx < SHN_LORESERVE)
6568 printf ("adjusting for sec '%s' @ 0x%x + 0x%x\n",
6569 sec->output_section->name,
6570 (unsigned int)sec->output_section->vma,
6571 (unsigned int)sec->output_offset);
6573 * result += sec->output_offset + sec->output_section->vma;
6576 printf ("Found symbol with effective value %8.8x\n", (unsigned int)* result);
6582 /* Hmm, haven't found it yet. perhaps it is a global. */
6583 global_entry = bfd_link_hash_lookup (finfo->info->hash, name, FALSE, FALSE, TRUE);
6587 if (global_entry->type == bfd_link_hash_defined
6588 || global_entry->type == bfd_link_hash_defweak)
6590 * result = global_entry->u.def.value
6591 + global_entry->u.def.section->output_section->vma
6592 + global_entry->u.def.section->output_offset;
6594 printf ("Found GLOBAL symbol '%s' with value %8.8x\n",
6595 global_entry->root.string, (unsigned int)*result);
6600 if (global_entry->type == bfd_link_hash_common)
6602 *result = global_entry->u.def.value +
6603 bfd_com_section_ptr->output_section->vma +
6604 bfd_com_section_ptr->output_offset;
6606 printf ("Found COMMON symbol '%s' with value %8.8x\n",
6607 global_entry->root.string, (unsigned int)*result);
6616 resolve_section (const char * name,
6617 asection * sections,
6623 for (curr = sections; curr; curr = curr->next)
6624 if (strcmp (curr->name, name) == 0)
6626 *result = curr->vma;
6630 /* Hmm. still haven't found it. try pseudo-section names. */
6631 for (curr = sections; curr; curr = curr->next)
6633 len = strlen (curr->name);
6634 if (len > strlen (name))
6637 if (strncmp (curr->name, name, len) == 0)
6639 if (strncmp (".end", name + len, 4) == 0)
6641 *result = curr->vma + curr->size;
6645 /* Insert more pseudo-section names here, if you like. */
6653 undefined_reference (const char * reftype,
6656 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), reftype, name);
6660 eval_symbol (bfd_vma * result,
6664 struct elf_final_link_info * finfo,
6666 bfd_vma section_offset,
6674 const int bufsz = 4096;
6675 char symbuf [bufsz];
6676 const char * symend;
6677 bfd_boolean symbol_is_section = FALSE;
6682 if (len < 1 || len > bufsz)
6684 bfd_set_error (bfd_error_invalid_operation);
6691 * result = addr + section_offset;
6692 * advanced = sym + 1;
6697 * result = strtoul (sym, advanced, 16);
6701 symbol_is_section = TRUE;
6704 symlen = strtol (sym, &sym, 10);
6705 ++ sym; /* Skip the trailing ':'. */
6707 if ((symend < sym) || ((symlen + 1) > bufsz))
6709 bfd_set_error (bfd_error_invalid_operation);
6713 memcpy (symbuf, sym, symlen);
6714 symbuf [symlen] = '\0';
6715 * advanced = sym + symlen;
6717 /* Is it always possible, with complex symbols, that gas "mis-guessed"
6718 the symbol as a section, or vice-versa. so we're pretty liberal in our
6719 interpretation here; section means "try section first", not "must be a
6720 section", and likewise with symbol. */
6722 if (symbol_is_section)
6724 if ((resolve_section (symbuf, finfo->output_bfd->sections, result) != TRUE)
6725 && (resolve_symbol (symbuf, input_bfd, finfo, result, locsymcount) != TRUE))
6727 undefined_reference ("section", symbuf);
6733 if ((resolve_symbol (symbuf, input_bfd, finfo, result, locsymcount) != TRUE)
6734 && (resolve_section (symbuf, finfo->output_bfd->sections,
6737 undefined_reference ("symbol", symbuf);
6744 /* All that remains are operators. */
6746 #define UNARY_OP(op) \
6747 if (strncmp (sym, #op, strlen (#op)) == 0) \
6749 sym += strlen (#op); \
6752 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
6753 section_offset, locsymcount, signed_p) \
6757 * result = op ((signed)a); \
6764 #define BINARY_OP(op) \
6765 if (strncmp (sym, #op, strlen (#op)) == 0) \
6767 sym += strlen (#op); \
6770 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
6771 section_offset, locsymcount, signed_p) \
6775 if (eval_symbol (& b, sym, & sym, input_bfd, finfo, addr, \
6776 section_offset, locsymcount, signed_p) \
6780 * result = ((signed) a) op ((signed) b); \
6782 * result = a op b; \
6811 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
6812 bfd_set_error (bfd_error_invalid_operation);
6817 /* Entry point to evaluator, called from elf_link_input_bfd. */
6820 evaluate_complex_relocation_symbols (bfd * input_bfd,
6821 struct elf_final_link_info * finfo,
6824 const struct elf_backend_data * bed;
6825 Elf_Internal_Shdr * symtab_hdr;
6826 struct elf_link_hash_entry ** sym_hashes;
6827 asection * reloc_sec;
6828 bfd_boolean result = TRUE;
6830 /* For each section, we're going to check and see if it has any
6831 complex relocations, and we're going to evaluate any of them
6834 if (finfo->info->relocatable)
6837 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
6838 sym_hashes = elf_sym_hashes (input_bfd);
6839 bed = get_elf_backend_data (input_bfd);
6841 for (reloc_sec = input_bfd->sections; reloc_sec; reloc_sec = reloc_sec->next)
6843 Elf_Internal_Rela * internal_relocs;
6846 /* This section was omitted from the link. */
6847 if (! reloc_sec->linker_mark)
6850 /* Only process sections containing relocs. */
6851 if ((reloc_sec->flags & SEC_RELOC) == 0)
6854 if (reloc_sec->reloc_count == 0)
6857 /* Read in the relocs for this section. */
6859 = _bfd_elf_link_read_relocs (input_bfd, reloc_sec, NULL,
6860 (Elf_Internal_Rela *) NULL,
6862 if (internal_relocs == NULL)
6865 for (i = reloc_sec->reloc_count; i--;)
6867 Elf_Internal_Rela * rel;
6870 Elf_Internal_Sym * sym;
6872 bfd_vma section_offset;
6876 rel = internal_relocs + i;
6877 section_offset = reloc_sec->output_section->vma
6878 + reloc_sec->output_offset;
6879 addr = rel->r_offset;
6881 index = ELF32_R_SYM (rel->r_info);
6882 if (bed->s->arch_size == 64)
6885 if (index == STN_UNDEF)
6888 if (index < locsymcount)
6890 /* The symbol is local. */
6891 sym = finfo->internal_syms + index;
6893 /* We're only processing STT_RELC or STT_SRELC type symbols. */
6894 if ((ELF_ST_TYPE (sym->st_info) != STT_RELC) &&
6895 (ELF_ST_TYPE (sym->st_info) != STT_SRELC))
6898 sym_name = bfd_elf_string_from_elf_section
6899 (input_bfd, symtab_hdr->sh_link, sym->st_name);
6901 signed_p = (ELF_ST_TYPE (sym->st_info) == STT_SRELC);
6905 /* The symbol is global. */
6906 struct elf_link_hash_entry * h;
6908 if (elf_bad_symtab (input_bfd))
6911 h = sym_hashes [index - locsymcount];
6912 while ( h->root.type == bfd_link_hash_indirect
6913 || h->root.type == bfd_link_hash_warning)
6914 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6916 if (h->type != STT_RELC && h->type != STT_SRELC)
6919 signed_p = (h->type == STT_SRELC);
6920 sym_name = (char *) h->root.root.string;
6923 printf ("Encountered a complex symbol!");
6924 printf (" (input_bfd %s, section %s, reloc %ld\n",
6925 input_bfd->filename, reloc_sec->name, i);
6926 printf (" symbol: idx %8.8lx, name %s\n",
6928 printf (" reloc : info %8.8lx, addr %8.8lx\n",
6930 printf (" Evaluating '%s' ...\n ", sym_name);
6932 if (eval_symbol (& result, sym_name, & sym_name, input_bfd,
6933 finfo, addr, section_offset, locsymcount,
6935 /* Symbol evaluated OK. Update to absolute value. */
6936 set_symbol_value (input_bfd, finfo, index, result);
6942 if (internal_relocs != elf_section_data (reloc_sec)->relocs)
6943 free (internal_relocs);
6946 /* If nothing went wrong, then we adjusted
6947 everything we wanted to adjust. */
6952 put_value (bfd_vma size,
6953 unsigned long chunksz,
6956 bfd_byte * location)
6958 location += (size - chunksz);
6960 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
6968 bfd_put_8 (input_bfd, x, location);
6971 bfd_put_16 (input_bfd, x, location);
6974 bfd_put_32 (input_bfd, x, location);
6978 bfd_put_64 (input_bfd, x, location);
6988 get_value (bfd_vma size,
6989 unsigned long chunksz,
6991 bfd_byte * location)
6995 for (; size; size -= chunksz, location += chunksz)
7003 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7006 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7009 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7013 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7024 decode_complex_addend
7025 (unsigned long * start, /* in bits */
7026 unsigned long * oplen, /* in bits */
7027 unsigned long * len, /* in bits */
7028 unsigned long * wordsz, /* in bytes */
7029 unsigned long * chunksz, /* in bytes */
7030 unsigned long * lsb0_p,
7031 unsigned long * signed_p,
7032 unsigned long * trunc_p,
7033 unsigned long encoded)
7035 * start = encoded & 0x3F;
7036 * len = (encoded >> 6) & 0x3F;
7037 * oplen = (encoded >> 12) & 0x3F;
7038 * wordsz = (encoded >> 18) & 0xF;
7039 * chunksz = (encoded >> 22) & 0xF;
7040 * lsb0_p = (encoded >> 27) & 1;
7041 * signed_p = (encoded >> 28) & 1;
7042 * trunc_p = (encoded >> 29) & 1;
7046 bfd_elf_perform_complex_relocation
7047 (bfd * output_bfd ATTRIBUTE_UNUSED,
7048 struct bfd_link_info * info,
7050 asection * input_section,
7051 bfd_byte * contents,
7052 Elf_Internal_Rela * rel,
7053 Elf_Internal_Sym * local_syms,
7054 asection ** local_sections)
7056 const struct elf_backend_data * bed;
7057 Elf_Internal_Shdr * symtab_hdr;
7059 bfd_vma relocation = 0, shift, x;
7062 unsigned long start, oplen, len, wordsz,
7063 chunksz, lsb0_p, signed_p, trunc_p;
7065 /* Perform this reloc, since it is complex.
7066 (this is not to say that it necessarily refers to a complex
7067 symbol; merely that it is a self-describing CGEN based reloc.
7068 i.e. the addend has the complete reloc information (bit start, end,
7069 word size, etc) encoded within it.). */
7070 r_symndx = ELF32_R_SYM (rel->r_info);
7071 bed = get_elf_backend_data (input_bfd);
7072 if (bed->s->arch_size == 64)
7076 printf ("Performing complex relocation %ld...\n", r_symndx);
7079 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7080 if (r_symndx < symtab_hdr->sh_info)
7082 /* The symbol is local. */
7083 Elf_Internal_Sym * sym;
7085 sym = local_syms + r_symndx;
7086 sec = local_sections [r_symndx];
7087 relocation = sym->st_value;
7088 if (sym->st_shndx > SHN_UNDEF &&
7089 sym->st_shndx < SHN_LORESERVE)
7090 relocation += (sec->output_offset +
7091 sec->output_section->vma);
7095 /* The symbol is global. */
7096 struct elf_link_hash_entry **sym_hashes;
7097 struct elf_link_hash_entry * h;
7099 sym_hashes = elf_sym_hashes (input_bfd);
7100 h = sym_hashes [r_symndx];
7102 while (h->root.type == bfd_link_hash_indirect
7103 || h->root.type == bfd_link_hash_warning)
7104 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7106 if (h->root.type == bfd_link_hash_defined
7107 || h->root.type == bfd_link_hash_defweak)
7109 sec = h->root.u.def.section;
7110 relocation = h->root.u.def.value;
7112 if (! bfd_is_abs_section (sec))
7113 relocation += (sec->output_section->vma
7114 + sec->output_offset);
7116 if (h->root.type == bfd_link_hash_undefined
7117 && !((*info->callbacks->undefined_symbol)
7118 (info, h->root.root.string, input_bfd,
7119 input_section, rel->r_offset,
7120 info->unresolved_syms_in_objects == RM_GENERATE_ERROR
7121 || ELF_ST_VISIBILITY (h->other))))
7125 decode_complex_addend (& start, & oplen, & len, & wordsz,
7126 & chunksz, & lsb0_p, & signed_p,
7127 & trunc_p, rel->r_addend);
7129 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7132 shift = (start + 1) - len;
7134 shift = (8 * wordsz) - (start + len);
7136 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7139 printf ("Doing complex reloc: "
7140 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7141 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7142 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7143 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7144 oplen, x, mask, relocation);
7149 /* Now do an overflow check. */
7150 if (bfd_check_overflow ((signed_p ?
7151 complain_overflow_signed :
7152 complain_overflow_unsigned),
7153 len, 0, (8 * wordsz),
7154 relocation) == bfd_reloc_overflow)
7155 (*_bfd_error_handler)
7156 ("%s (%s + 0x%lx): relocation overflow: 0x%lx %sdoes not fit "
7158 input_bfd->filename, input_section->name, rel->r_offset,
7159 relocation, (signed_p ? "(signed) " : ""), mask);
7163 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7166 printf (" relocation: %8.8lx\n"
7167 " shifted mask: %8.8lx\n"
7168 " shifted/masked reloc: %8.8lx\n"
7169 " result: %8.8lx\n",
7170 relocation, (mask << shift),
7171 ((relocation & mask) << shift), x);
7173 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
7176 /* When performing a relocatable link, the input relocations are
7177 preserved. But, if they reference global symbols, the indices
7178 referenced must be updated. Update all the relocations in
7179 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7182 elf_link_adjust_relocs (bfd *abfd,
7183 Elf_Internal_Shdr *rel_hdr,
7185 struct elf_link_hash_entry **rel_hash)
7188 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7190 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7191 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7192 bfd_vma r_type_mask;
7195 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
7197 swap_in = bed->s->swap_reloc_in;
7198 swap_out = bed->s->swap_reloc_out;
7200 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
7202 swap_in = bed->s->swap_reloca_in;
7203 swap_out = bed->s->swap_reloca_out;
7208 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7211 if (bed->s->arch_size == 32)
7218 r_type_mask = 0xffffffff;
7222 erela = rel_hdr->contents;
7223 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
7225 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7228 if (*rel_hash == NULL)
7231 BFD_ASSERT ((*rel_hash)->indx >= 0);
7233 (*swap_in) (abfd, erela, irela);
7234 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7235 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7236 | (irela[j].r_info & r_type_mask));
7237 (*swap_out) (abfd, irela, erela);
7241 struct elf_link_sort_rela
7247 enum elf_reloc_type_class type;
7248 /* We use this as an array of size int_rels_per_ext_rel. */
7249 Elf_Internal_Rela rela[1];
7253 elf_link_sort_cmp1 (const void *A, const void *B)
7255 const struct elf_link_sort_rela *a = A;
7256 const struct elf_link_sort_rela *b = B;
7257 int relativea, relativeb;
7259 relativea = a->type == reloc_class_relative;
7260 relativeb = b->type == reloc_class_relative;
7262 if (relativea < relativeb)
7264 if (relativea > relativeb)
7266 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
7268 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
7270 if (a->rela->r_offset < b->rela->r_offset)
7272 if (a->rela->r_offset > b->rela->r_offset)
7278 elf_link_sort_cmp2 (const void *A, const void *B)
7280 const struct elf_link_sort_rela *a = A;
7281 const struct elf_link_sort_rela *b = B;
7284 if (a->u.offset < b->u.offset)
7286 if (a->u.offset > b->u.offset)
7288 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
7289 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
7294 if (a->rela->r_offset < b->rela->r_offset)
7296 if (a->rela->r_offset > b->rela->r_offset)
7302 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
7304 asection *dynamic_relocs;
7307 bfd_size_type count, size;
7308 size_t i, ret, sort_elt, ext_size;
7309 bfd_byte *sort, *s_non_relative, *p;
7310 struct elf_link_sort_rela *sq;
7311 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7312 int i2e = bed->s->int_rels_per_ext_rel;
7313 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7314 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7315 struct bfd_link_order *lo;
7317 bfd_boolean use_rela;
7319 /* Find a dynamic reloc section. */
7320 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
7321 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
7322 if (rela_dyn != NULL && rela_dyn->size > 0
7323 && rel_dyn != NULL && rel_dyn->size > 0)
7325 bfd_boolean use_rela_initialised = FALSE;
7327 /* This is just here to stop gcc from complaining.
7328 It's initialization checking code is not perfect. */
7331 /* Both sections are present. Examine the sizes
7332 of the indirect sections to help us choose. */
7333 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7334 if (lo->type == bfd_indirect_link_order)
7336 asection *o = lo->u.indirect.section;
7338 if ((o->size % bed->s->sizeof_rela) == 0)
7340 if ((o->size % bed->s->sizeof_rel) == 0)
7341 /* Section size is divisible by both rel and rela sizes.
7342 It is of no help to us. */
7346 /* Section size is only divisible by rela. */
7347 if (use_rela_initialised && (use_rela == FALSE))
7350 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7351 bfd_set_error (bfd_error_invalid_operation);
7357 use_rela_initialised = TRUE;
7361 else if ((o->size % bed->s->sizeof_rel) == 0)
7363 /* Section size is only divisible by rel. */
7364 if (use_rela_initialised && (use_rela == TRUE))
7367 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7368 bfd_set_error (bfd_error_invalid_operation);
7374 use_rela_initialised = TRUE;
7379 /* The section size is not divisible by either - something is wrong. */
7381 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
7382 bfd_set_error (bfd_error_invalid_operation);
7387 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7388 if (lo->type == bfd_indirect_link_order)
7390 asection *o = lo->u.indirect.section;
7392 if ((o->size % bed->s->sizeof_rela) == 0)
7394 if ((o->size % bed->s->sizeof_rel) == 0)
7395 /* Section size is divisible by both rel and rela sizes.
7396 It is of no help to us. */
7400 /* Section size is only divisible by rela. */
7401 if (use_rela_initialised && (use_rela == FALSE))
7404 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7405 bfd_set_error (bfd_error_invalid_operation);
7411 use_rela_initialised = TRUE;
7415 else if ((o->size % bed->s->sizeof_rel) == 0)
7417 /* Section size is only divisible by rel. */
7418 if (use_rela_initialised && (use_rela == TRUE))
7421 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7422 bfd_set_error (bfd_error_invalid_operation);
7428 use_rela_initialised = TRUE;
7433 /* The section size is not divisible by either - something is wrong. */
7435 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
7436 bfd_set_error (bfd_error_invalid_operation);
7441 if (! use_rela_initialised)
7445 else if (rela_dyn != NULL && rela_dyn->size > 0)
7447 else if (rel_dyn != NULL && rel_dyn->size > 0)
7454 dynamic_relocs = rela_dyn;
7455 ext_size = bed->s->sizeof_rela;
7456 swap_in = bed->s->swap_reloca_in;
7457 swap_out = bed->s->swap_reloca_out;
7461 dynamic_relocs = rel_dyn;
7462 ext_size = bed->s->sizeof_rel;
7463 swap_in = bed->s->swap_reloc_in;
7464 swap_out = bed->s->swap_reloc_out;
7468 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
7469 if (lo->type == bfd_indirect_link_order)
7470 size += lo->u.indirect.section->size;
7472 if (size != dynamic_relocs->size)
7475 sort_elt = (sizeof (struct elf_link_sort_rela)
7476 + (i2e - 1) * sizeof (Elf_Internal_Rela));
7478 count = dynamic_relocs->size / ext_size;
7479 sort = bfd_zmalloc (sort_elt * count);
7483 (*info->callbacks->warning)
7484 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
7488 if (bed->s->arch_size == 32)
7489 r_sym_mask = ~(bfd_vma) 0xff;
7491 r_sym_mask = ~(bfd_vma) 0xffffffff;
7493 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
7494 if (lo->type == bfd_indirect_link_order)
7496 bfd_byte *erel, *erelend;
7497 asection *o = lo->u.indirect.section;
7499 if (o->contents == NULL && o->size != 0)
7501 /* This is a reloc section that is being handled as a normal
7502 section. See bfd_section_from_shdr. We can't combine
7503 relocs in this case. */
7508 erelend = o->contents + o->size;
7509 p = sort + o->output_offset / ext_size * sort_elt;
7511 while (erel < erelend)
7513 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
7515 (*swap_in) (abfd, erel, s->rela);
7516 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
7517 s->u.sym_mask = r_sym_mask;
7523 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
7525 for (i = 0, p = sort; i < count; i++, p += sort_elt)
7527 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
7528 if (s->type != reloc_class_relative)
7534 sq = (struct elf_link_sort_rela *) s_non_relative;
7535 for (; i < count; i++, p += sort_elt)
7537 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
7538 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
7540 sp->u.offset = sq->rela->r_offset;
7543 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
7545 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
7546 if (lo->type == bfd_indirect_link_order)
7548 bfd_byte *erel, *erelend;
7549 asection *o = lo->u.indirect.section;
7552 erelend = o->contents + o->size;
7553 p = sort + o->output_offset / ext_size * sort_elt;
7554 while (erel < erelend)
7556 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
7557 (*swap_out) (abfd, s->rela, erel);
7564 *psec = dynamic_relocs;
7568 /* Flush the output symbols to the file. */
7571 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
7572 const struct elf_backend_data *bed)
7574 if (finfo->symbuf_count > 0)
7576 Elf_Internal_Shdr *hdr;
7580 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
7581 pos = hdr->sh_offset + hdr->sh_size;
7582 amt = finfo->symbuf_count * bed->s->sizeof_sym;
7583 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
7584 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
7587 hdr->sh_size += amt;
7588 finfo->symbuf_count = 0;
7594 /* Add a symbol to the output symbol table. */
7597 elf_link_output_sym (struct elf_final_link_info *finfo,
7599 Elf_Internal_Sym *elfsym,
7600 asection *input_sec,
7601 struct elf_link_hash_entry *h)
7604 Elf_External_Sym_Shndx *destshndx;
7605 bfd_boolean (*output_symbol_hook)
7606 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
7607 struct elf_link_hash_entry *);
7608 const struct elf_backend_data *bed;
7610 bed = get_elf_backend_data (finfo->output_bfd);
7611 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
7612 if (output_symbol_hook != NULL)
7614 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
7618 if (name == NULL || *name == '\0')
7619 elfsym->st_name = 0;
7620 else if (input_sec->flags & SEC_EXCLUDE)
7621 elfsym->st_name = 0;
7624 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
7626 if (elfsym->st_name == (unsigned long) -1)
7630 if (finfo->symbuf_count >= finfo->symbuf_size)
7632 if (! elf_link_flush_output_syms (finfo, bed))
7636 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
7637 destshndx = finfo->symshndxbuf;
7638 if (destshndx != NULL)
7640 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
7644 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
7645 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
7646 if (destshndx == NULL)
7648 memset ((char *) destshndx + amt, 0, amt);
7649 finfo->shndxbuf_size *= 2;
7651 destshndx += bfd_get_symcount (finfo->output_bfd);
7654 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
7655 finfo->symbuf_count += 1;
7656 bfd_get_symcount (finfo->output_bfd) += 1;
7661 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
7664 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
7666 if (sym->st_shndx > SHN_HIRESERVE)
7668 /* The gABI doesn't support dynamic symbols in output sections
7670 (*_bfd_error_handler)
7671 (_("%B: Too many sections: %d (>= %d)"),
7672 abfd, bfd_count_sections (abfd), SHN_LORESERVE);
7673 bfd_set_error (bfd_error_nonrepresentable_section);
7679 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
7680 allowing an unsatisfied unversioned symbol in the DSO to match a
7681 versioned symbol that would normally require an explicit version.
7682 We also handle the case that a DSO references a hidden symbol
7683 which may be satisfied by a versioned symbol in another DSO. */
7686 elf_link_check_versioned_symbol (struct bfd_link_info *info,
7687 const struct elf_backend_data *bed,
7688 struct elf_link_hash_entry *h)
7691 struct elf_link_loaded_list *loaded;
7693 if (!is_elf_hash_table (info->hash))
7696 switch (h->root.type)
7702 case bfd_link_hash_undefined:
7703 case bfd_link_hash_undefweak:
7704 abfd = h->root.u.undef.abfd;
7705 if ((abfd->flags & DYNAMIC) == 0
7706 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
7710 case bfd_link_hash_defined:
7711 case bfd_link_hash_defweak:
7712 abfd = h->root.u.def.section->owner;
7715 case bfd_link_hash_common:
7716 abfd = h->root.u.c.p->section->owner;
7719 BFD_ASSERT (abfd != NULL);
7721 for (loaded = elf_hash_table (info)->loaded;
7723 loaded = loaded->next)
7726 Elf_Internal_Shdr *hdr;
7727 bfd_size_type symcount;
7728 bfd_size_type extsymcount;
7729 bfd_size_type extsymoff;
7730 Elf_Internal_Shdr *versymhdr;
7731 Elf_Internal_Sym *isym;
7732 Elf_Internal_Sym *isymend;
7733 Elf_Internal_Sym *isymbuf;
7734 Elf_External_Versym *ever;
7735 Elf_External_Versym *extversym;
7737 input = loaded->abfd;
7739 /* We check each DSO for a possible hidden versioned definition. */
7741 || (input->flags & DYNAMIC) == 0
7742 || elf_dynversym (input) == 0)
7745 hdr = &elf_tdata (input)->dynsymtab_hdr;
7747 symcount = hdr->sh_size / bed->s->sizeof_sym;
7748 if (elf_bad_symtab (input))
7750 extsymcount = symcount;
7755 extsymcount = symcount - hdr->sh_info;
7756 extsymoff = hdr->sh_info;
7759 if (extsymcount == 0)
7762 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
7764 if (isymbuf == NULL)
7767 /* Read in any version definitions. */
7768 versymhdr = &elf_tdata (input)->dynversym_hdr;
7769 extversym = bfd_malloc (versymhdr->sh_size);
7770 if (extversym == NULL)
7773 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
7774 || (bfd_bread (extversym, versymhdr->sh_size, input)
7775 != versymhdr->sh_size))
7783 ever = extversym + extsymoff;
7784 isymend = isymbuf + extsymcount;
7785 for (isym = isymbuf; isym < isymend; isym++, ever++)
7788 Elf_Internal_Versym iver;
7789 unsigned short version_index;
7791 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
7792 || isym->st_shndx == SHN_UNDEF)
7795 name = bfd_elf_string_from_elf_section (input,
7798 if (strcmp (name, h->root.root.string) != 0)
7801 _bfd_elf_swap_versym_in (input, ever, &iver);
7803 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
7805 /* If we have a non-hidden versioned sym, then it should
7806 have provided a definition for the undefined sym. */
7810 version_index = iver.vs_vers & VERSYM_VERSION;
7811 if (version_index == 1 || version_index == 2)
7813 /* This is the base or first version. We can use it. */
7827 /* Add an external symbol to the symbol table. This is called from
7828 the hash table traversal routine. When generating a shared object,
7829 we go through the symbol table twice. The first time we output
7830 anything that might have been forced to local scope in a version
7831 script. The second time we output the symbols that are still
7835 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
7837 struct elf_outext_info *eoinfo = data;
7838 struct elf_final_link_info *finfo = eoinfo->finfo;
7840 Elf_Internal_Sym sym;
7841 asection *input_sec;
7842 const struct elf_backend_data *bed;
7844 if (h->root.type == bfd_link_hash_warning)
7846 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7847 if (h->root.type == bfd_link_hash_new)
7851 /* Decide whether to output this symbol in this pass. */
7852 if (eoinfo->localsyms)
7854 if (!h->forced_local)
7859 if (h->forced_local)
7863 bed = get_elf_backend_data (finfo->output_bfd);
7865 if (h->root.type == bfd_link_hash_undefined)
7867 /* If we have an undefined symbol reference here then it must have
7868 come from a shared library that is being linked in. (Undefined
7869 references in regular files have already been handled). */
7870 bfd_boolean ignore_undef = FALSE;
7872 /* Some symbols may be special in that the fact that they're
7873 undefined can be safely ignored - let backend determine that. */
7874 if (bed->elf_backend_ignore_undef_symbol)
7875 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
7877 /* If we are reporting errors for this situation then do so now. */
7878 if (ignore_undef == FALSE
7881 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
7882 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
7884 if (! (finfo->info->callbacks->undefined_symbol
7885 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
7886 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
7888 eoinfo->failed = TRUE;
7894 /* We should also warn if a forced local symbol is referenced from
7895 shared libraries. */
7896 if (! finfo->info->relocatable
7897 && (! finfo->info->shared)
7902 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
7904 (*_bfd_error_handler)
7905 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
7907 h->root.u.def.section == bfd_abs_section_ptr
7908 ? finfo->output_bfd : h->root.u.def.section->owner,
7909 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
7911 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
7912 ? "hidden" : "local",
7913 h->root.root.string);
7914 eoinfo->failed = TRUE;
7918 /* We don't want to output symbols that have never been mentioned by
7919 a regular file, or that we have been told to strip. However, if
7920 h->indx is set to -2, the symbol is used by a reloc and we must
7924 else if ((h->def_dynamic
7926 || h->root.type == bfd_link_hash_new)
7930 else if (finfo->info->strip == strip_all)
7932 else if (finfo->info->strip == strip_some
7933 && bfd_hash_lookup (finfo->info->keep_hash,
7934 h->root.root.string, FALSE, FALSE) == NULL)
7936 else if (finfo->info->strip_discarded
7937 && (h->root.type == bfd_link_hash_defined
7938 || h->root.type == bfd_link_hash_defweak)
7939 && elf_discarded_section (h->root.u.def.section))
7944 /* If we're stripping it, and it's not a dynamic symbol, there's
7945 nothing else to do unless it is a forced local symbol. */
7948 && !h->forced_local)
7952 sym.st_size = h->size;
7953 sym.st_other = h->other;
7954 if (h->forced_local)
7955 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
7956 else if (h->root.type == bfd_link_hash_undefweak
7957 || h->root.type == bfd_link_hash_defweak)
7958 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
7960 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
7962 switch (h->root.type)
7965 case bfd_link_hash_new:
7966 case bfd_link_hash_warning:
7970 case bfd_link_hash_undefined:
7971 case bfd_link_hash_undefweak:
7972 input_sec = bfd_und_section_ptr;
7973 sym.st_shndx = SHN_UNDEF;
7976 case bfd_link_hash_defined:
7977 case bfd_link_hash_defweak:
7979 input_sec = h->root.u.def.section;
7980 if (input_sec->output_section != NULL)
7983 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
7984 input_sec->output_section);
7985 if (sym.st_shndx == SHN_BAD)
7987 (*_bfd_error_handler)
7988 (_("%B: could not find output section %A for input section %A"),
7989 finfo->output_bfd, input_sec->output_section, input_sec);
7990 eoinfo->failed = TRUE;
7994 /* ELF symbols in relocatable files are section relative,
7995 but in nonrelocatable files they are virtual
7997 sym.st_value = h->root.u.def.value + input_sec->output_offset;
7998 if (! finfo->info->relocatable)
8000 sym.st_value += input_sec->output_section->vma;
8001 if (h->type == STT_TLS)
8003 /* STT_TLS symbols are relative to PT_TLS segment
8005 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
8006 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
8012 BFD_ASSERT (input_sec->owner == NULL
8013 || (input_sec->owner->flags & DYNAMIC) != 0);
8014 sym.st_shndx = SHN_UNDEF;
8015 input_sec = bfd_und_section_ptr;
8020 case bfd_link_hash_common:
8021 input_sec = h->root.u.c.p->section;
8022 sym.st_shndx = bed->common_section_index (input_sec);
8023 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8026 case bfd_link_hash_indirect:
8027 /* These symbols are created by symbol versioning. They point
8028 to the decorated version of the name. For example, if the
8029 symbol foo@@GNU_1.2 is the default, which should be used when
8030 foo is used with no version, then we add an indirect symbol
8031 foo which points to foo@@GNU_1.2. We ignore these symbols,
8032 since the indirected symbol is already in the hash table. */
8036 /* Give the processor backend a chance to tweak the symbol value,
8037 and also to finish up anything that needs to be done for this
8038 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8039 forced local syms when non-shared is due to a historical quirk. */
8040 if ((h->dynindx != -1
8042 && ((finfo->info->shared
8043 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8044 || h->root.type != bfd_link_hash_undefweak))
8045 || !h->forced_local)
8046 && elf_hash_table (finfo->info)->dynamic_sections_created)
8048 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8049 (finfo->output_bfd, finfo->info, h, &sym)))
8051 eoinfo->failed = TRUE;
8056 /* If we are marking the symbol as undefined, and there are no
8057 non-weak references to this symbol from a regular object, then
8058 mark the symbol as weak undefined; if there are non-weak
8059 references, mark the symbol as strong. We can't do this earlier,
8060 because it might not be marked as undefined until the
8061 finish_dynamic_symbol routine gets through with it. */
8062 if (sym.st_shndx == SHN_UNDEF
8064 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8065 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8069 if (h->ref_regular_nonweak)
8070 bindtype = STB_GLOBAL;
8072 bindtype = STB_WEAK;
8073 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
8076 /* If a non-weak symbol with non-default visibility is not defined
8077 locally, it is a fatal error. */
8078 if (! finfo->info->relocatable
8079 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8080 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8081 && h->root.type == bfd_link_hash_undefined
8084 (*_bfd_error_handler)
8085 (_("%B: %s symbol `%s' isn't defined"),
8087 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
8089 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
8090 ? "internal" : "hidden",
8091 h->root.root.string);
8092 eoinfo->failed = TRUE;
8096 /* If this symbol should be put in the .dynsym section, then put it
8097 there now. We already know the symbol index. We also fill in
8098 the entry in the .hash section. */
8099 if (h->dynindx != -1
8100 && elf_hash_table (finfo->info)->dynamic_sections_created)
8104 sym.st_name = h->dynstr_index;
8105 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
8106 if (! check_dynsym (finfo->output_bfd, &sym))
8108 eoinfo->failed = TRUE;
8111 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
8113 if (finfo->hash_sec != NULL)
8115 size_t hash_entry_size;
8116 bfd_byte *bucketpos;
8121 bucketcount = elf_hash_table (finfo->info)->bucketcount;
8122 bucket = h->u.elf_hash_value % bucketcount;
8125 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
8126 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
8127 + (bucket + 2) * hash_entry_size);
8128 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
8129 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
8130 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
8131 ((bfd_byte *) finfo->hash_sec->contents
8132 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8135 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
8137 Elf_Internal_Versym iversym;
8138 Elf_External_Versym *eversym;
8140 if (!h->def_regular)
8142 if (h->verinfo.verdef == NULL)
8143 iversym.vs_vers = 0;
8145 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8149 if (h->verinfo.vertree == NULL)
8150 iversym.vs_vers = 1;
8152 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8153 if (finfo->info->create_default_symver)
8158 iversym.vs_vers |= VERSYM_HIDDEN;
8160 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
8161 eversym += h->dynindx;
8162 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
8166 /* If we're stripping it, then it was just a dynamic symbol, and
8167 there's nothing else to do. */
8168 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8171 h->indx = bfd_get_symcount (finfo->output_bfd);
8173 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
8175 eoinfo->failed = TRUE;
8182 /* Return TRUE if special handling is done for relocs in SEC against
8183 symbols defined in discarded sections. */
8186 elf_section_ignore_discarded_relocs (asection *sec)
8188 const struct elf_backend_data *bed;
8190 switch (sec->sec_info_type)
8192 case ELF_INFO_TYPE_STABS:
8193 case ELF_INFO_TYPE_EH_FRAME:
8199 bed = get_elf_backend_data (sec->owner);
8200 if (bed->elf_backend_ignore_discarded_relocs != NULL
8201 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8207 /* Return a mask saying how ld should treat relocations in SEC against
8208 symbols defined in discarded sections. If this function returns
8209 COMPLAIN set, ld will issue a warning message. If this function
8210 returns PRETEND set, and the discarded section was link-once and the
8211 same size as the kept link-once section, ld will pretend that the
8212 symbol was actually defined in the kept section. Otherwise ld will
8213 zero the reloc (at least that is the intent, but some cooperation by
8214 the target dependent code is needed, particularly for REL targets). */
8217 _bfd_elf_default_action_discarded (asection *sec)
8219 if (sec->flags & SEC_DEBUGGING)
8222 if (strcmp (".eh_frame", sec->name) == 0)
8225 if (strcmp (".gcc_except_table", sec->name) == 0)
8228 return COMPLAIN | PRETEND;
8231 /* Find a match between a section and a member of a section group. */
8234 match_group_member (asection *sec, asection *group,
8235 struct bfd_link_info *info)
8237 asection *first = elf_next_in_group (group);
8238 asection *s = first;
8242 if (bfd_elf_match_symbols_in_sections (s, sec, info))
8245 s = elf_next_in_group (s);
8253 /* Check if the kept section of a discarded section SEC can be used
8254 to replace it. Return the replacement if it is OK. Otherwise return
8258 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
8262 kept = sec->kept_section;
8265 if ((kept->flags & SEC_GROUP) != 0)
8266 kept = match_group_member (sec, kept, info);
8267 if (kept != NULL && sec->size != kept->size)
8269 sec->kept_section = kept;
8274 /* Link an input file into the linker output file. This function
8275 handles all the sections and relocations of the input file at once.
8276 This is so that we only have to read the local symbols once, and
8277 don't have to keep them in memory. */
8280 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
8282 int (*relocate_section)
8283 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
8284 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
8286 Elf_Internal_Shdr *symtab_hdr;
8289 Elf_Internal_Sym *isymbuf;
8290 Elf_Internal_Sym *isym;
8291 Elf_Internal_Sym *isymend;
8293 asection **ppsection;
8295 const struct elf_backend_data *bed;
8296 struct elf_link_hash_entry **sym_hashes;
8298 output_bfd = finfo->output_bfd;
8299 bed = get_elf_backend_data (output_bfd);
8300 relocate_section = bed->elf_backend_relocate_section;
8302 /* If this is a dynamic object, we don't want to do anything here:
8303 we don't want the local symbols, and we don't want the section
8305 if ((input_bfd->flags & DYNAMIC) != 0)
8308 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8309 if (elf_bad_symtab (input_bfd))
8311 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8316 locsymcount = symtab_hdr->sh_info;
8317 extsymoff = symtab_hdr->sh_info;
8320 /* Read the local symbols. */
8321 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8322 if (isymbuf == NULL && locsymcount != 0)
8324 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8325 finfo->internal_syms,
8326 finfo->external_syms,
8327 finfo->locsym_shndx);
8328 if (isymbuf == NULL)
8331 /* evaluate_complex_relocation_symbols looks for symbols in
8332 finfo->internal_syms. */
8333 else if (isymbuf != NULL && locsymcount != 0)
8335 bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8336 finfo->internal_syms,
8337 finfo->external_syms,
8338 finfo->locsym_shndx);
8341 /* Find local symbol sections and adjust values of symbols in
8342 SEC_MERGE sections. Write out those local symbols we know are
8343 going into the output file. */
8344 isymend = isymbuf + locsymcount;
8345 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
8347 isym++, pindex++, ppsection++)
8351 Elf_Internal_Sym osym;
8355 if (elf_bad_symtab (input_bfd))
8357 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
8364 if (isym->st_shndx == SHN_UNDEF)
8365 isec = bfd_und_section_ptr;
8366 else if (isym->st_shndx < SHN_LORESERVE
8367 || isym->st_shndx > SHN_HIRESERVE)
8369 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
8371 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
8372 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
8374 _bfd_merged_section_offset (output_bfd, &isec,
8375 elf_section_data (isec)->sec_info,
8378 else if (isym->st_shndx == SHN_ABS)
8379 isec = bfd_abs_section_ptr;
8380 else if (isym->st_shndx == SHN_COMMON)
8381 isec = bfd_com_section_ptr;
8384 /* Don't attempt to output symbols with st_shnx in the
8385 reserved range other than SHN_ABS and SHN_COMMON. */
8392 /* Don't output the first, undefined, symbol. */
8393 if (ppsection == finfo->sections)
8396 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
8398 /* We never output section symbols. Instead, we use the
8399 section symbol of the corresponding section in the output
8404 /* If we are stripping all symbols, we don't want to output this
8406 if (finfo->info->strip == strip_all)
8409 /* If we are discarding all local symbols, we don't want to
8410 output this one. If we are generating a relocatable output
8411 file, then some of the local symbols may be required by
8412 relocs; we output them below as we discover that they are
8414 if (finfo->info->discard == discard_all)
8417 /* If this symbol is defined in a section which we are
8418 discarding, we don't need to keep it. */
8419 if (isym->st_shndx != SHN_UNDEF
8420 && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
8422 || bfd_section_removed_from_list (output_bfd,
8423 isec->output_section)))
8426 /* Get the name of the symbol. */
8427 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
8432 /* See if we are discarding symbols with this name. */
8433 if ((finfo->info->strip == strip_some
8434 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
8436 || (((finfo->info->discard == discard_sec_merge
8437 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
8438 || finfo->info->discard == discard_l)
8439 && bfd_is_local_label_name (input_bfd, name)))
8442 /* If we get here, we are going to output this symbol. */
8446 /* Adjust the section index for the output file. */
8447 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
8448 isec->output_section);
8449 if (osym.st_shndx == SHN_BAD)
8452 *pindex = bfd_get_symcount (output_bfd);
8454 /* ELF symbols in relocatable files are section relative, but
8455 in executable files they are virtual addresses. Note that
8456 this code assumes that all ELF sections have an associated
8457 BFD section with a reasonable value for output_offset; below
8458 we assume that they also have a reasonable value for
8459 output_section. Any special sections must be set up to meet
8460 these requirements. */
8461 osym.st_value += isec->output_offset;
8462 if (! finfo->info->relocatable)
8464 osym.st_value += isec->output_section->vma;
8465 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
8467 /* STT_TLS symbols are relative to PT_TLS segment base. */
8468 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
8469 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
8473 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
8477 if (! evaluate_complex_relocation_symbols (input_bfd, finfo, locsymcount))
8480 /* Relocate the contents of each section. */
8481 sym_hashes = elf_sym_hashes (input_bfd);
8482 for (o = input_bfd->sections; o != NULL; o = o->next)
8486 if (! o->linker_mark)
8488 /* This section was omitted from the link. */
8492 if ((o->flags & SEC_HAS_CONTENTS) == 0
8493 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
8496 if ((o->flags & SEC_LINKER_CREATED) != 0)
8498 /* Section was created by _bfd_elf_link_create_dynamic_sections
8503 /* Get the contents of the section. They have been cached by a
8504 relaxation routine. Note that o is a section in an input
8505 file, so the contents field will not have been set by any of
8506 the routines which work on output files. */
8507 if (elf_section_data (o)->this_hdr.contents != NULL)
8508 contents = elf_section_data (o)->this_hdr.contents;
8511 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
8513 contents = finfo->contents;
8514 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
8518 if ((o->flags & SEC_RELOC) != 0)
8520 Elf_Internal_Rela *internal_relocs;
8521 bfd_vma r_type_mask;
8525 /* Get the swapped relocs. */
8527 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
8528 finfo->internal_relocs, FALSE);
8529 if (internal_relocs == NULL
8530 && o->reloc_count > 0)
8533 if (bed->s->arch_size == 32)
8540 r_type_mask = 0xffffffff;
8544 /* Run through the relocs looking for any against symbols
8545 from discarded sections and section symbols from
8546 removed link-once sections. Complain about relocs
8547 against discarded sections. Zero relocs against removed
8548 link-once sections. */
8549 if (!elf_section_ignore_discarded_relocs (o))
8551 Elf_Internal_Rela *rel, *relend;
8552 unsigned int action = (*bed->action_discarded) (o);
8554 rel = internal_relocs;
8555 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
8556 for ( ; rel < relend; rel++)
8558 unsigned long r_symndx = rel->r_info >> r_sym_shift;
8559 asection **ps, *sec;
8560 struct elf_link_hash_entry *h = NULL;
8561 const char *sym_name;
8563 if (r_symndx == STN_UNDEF)
8566 if (r_symndx >= locsymcount
8567 || (elf_bad_symtab (input_bfd)
8568 && finfo->sections[r_symndx] == NULL))
8570 h = sym_hashes[r_symndx - extsymoff];
8572 /* Badly formatted input files can contain relocs that
8573 reference non-existant symbols. Check here so that
8574 we do not seg fault. */
8579 sprintf_vma (buffer, rel->r_info);
8580 (*_bfd_error_handler)
8581 (_("error: %B contains a reloc (0x%s) for section %A "
8582 "that references a non-existent global symbol"),
8583 input_bfd, o, buffer);
8584 bfd_set_error (bfd_error_bad_value);
8588 while (h->root.type == bfd_link_hash_indirect
8589 || h->root.type == bfd_link_hash_warning)
8590 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8592 if (h->root.type != bfd_link_hash_defined
8593 && h->root.type != bfd_link_hash_defweak)
8596 ps = &h->root.u.def.section;
8597 sym_name = h->root.root.string;
8601 Elf_Internal_Sym *sym = isymbuf + r_symndx;
8602 ps = &finfo->sections[r_symndx];
8603 sym_name = bfd_elf_sym_name (input_bfd,
8608 /* Complain if the definition comes from a
8609 discarded section. */
8610 if ((sec = *ps) != NULL && elf_discarded_section (sec))
8612 BFD_ASSERT (r_symndx != 0);
8613 if (action & COMPLAIN)
8614 (*finfo->info->callbacks->einfo)
8615 (_("%X`%s' referenced in section `%A' of %B: "
8616 "defined in discarded section `%A' of %B\n"),
8617 sym_name, o, input_bfd, sec, sec->owner);
8619 /* Try to do the best we can to support buggy old
8620 versions of gcc. Pretend that the symbol is
8621 really defined in the kept linkonce section.
8622 FIXME: This is quite broken. Modifying the
8623 symbol here means we will be changing all later
8624 uses of the symbol, not just in this section. */
8625 if (action & PRETEND)
8629 kept = _bfd_elf_check_kept_section (sec,
8641 /* Relocate the section by invoking a back end routine.
8643 The back end routine is responsible for adjusting the
8644 section contents as necessary, and (if using Rela relocs
8645 and generating a relocatable output file) adjusting the
8646 reloc addend as necessary.
8648 The back end routine does not have to worry about setting
8649 the reloc address or the reloc symbol index.
8651 The back end routine is given a pointer to the swapped in
8652 internal symbols, and can access the hash table entries
8653 for the external symbols via elf_sym_hashes (input_bfd).
8655 When generating relocatable output, the back end routine
8656 must handle STB_LOCAL/STT_SECTION symbols specially. The
8657 output symbol is going to be a section symbol
8658 corresponding to the output section, which will require
8659 the addend to be adjusted. */
8661 ret = (*relocate_section) (output_bfd, finfo->info,
8662 input_bfd, o, contents,
8670 || finfo->info->relocatable
8671 || finfo->info->emitrelocations)
8673 Elf_Internal_Rela *irela;
8674 Elf_Internal_Rela *irelaend;
8675 bfd_vma last_offset;
8676 struct elf_link_hash_entry **rel_hash;
8677 struct elf_link_hash_entry **rel_hash_list;
8678 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
8679 unsigned int next_erel;
8680 bfd_boolean rela_normal;
8682 input_rel_hdr = &elf_section_data (o)->rel_hdr;
8683 rela_normal = (bed->rela_normal
8684 && (input_rel_hdr->sh_entsize
8685 == bed->s->sizeof_rela));
8687 /* Adjust the reloc addresses and symbol indices. */
8689 irela = internal_relocs;
8690 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
8691 rel_hash = (elf_section_data (o->output_section)->rel_hashes
8692 + elf_section_data (o->output_section)->rel_count
8693 + elf_section_data (o->output_section)->rel_count2);
8694 rel_hash_list = rel_hash;
8695 last_offset = o->output_offset;
8696 if (!finfo->info->relocatable)
8697 last_offset += o->output_section->vma;
8698 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
8700 unsigned long r_symndx;
8702 Elf_Internal_Sym sym;
8704 if (next_erel == bed->s->int_rels_per_ext_rel)
8710 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8713 if (irela->r_offset >= (bfd_vma) -2)
8715 /* This is a reloc for a deleted entry or somesuch.
8716 Turn it into an R_*_NONE reloc, at the same
8717 offset as the last reloc. elf_eh_frame.c and
8718 bfd_elf_discard_info rely on reloc offsets
8720 irela->r_offset = last_offset;
8722 irela->r_addend = 0;
8726 irela->r_offset += o->output_offset;
8728 /* Relocs in an executable have to be virtual addresses. */
8729 if (!finfo->info->relocatable)
8730 irela->r_offset += o->output_section->vma;
8732 last_offset = irela->r_offset;
8734 r_symndx = irela->r_info >> r_sym_shift;
8735 if (r_symndx == STN_UNDEF)
8738 if (r_symndx >= locsymcount
8739 || (elf_bad_symtab (input_bfd)
8740 && finfo->sections[r_symndx] == NULL))
8742 struct elf_link_hash_entry *rh;
8745 /* This is a reloc against a global symbol. We
8746 have not yet output all the local symbols, so
8747 we do not know the symbol index of any global
8748 symbol. We set the rel_hash entry for this
8749 reloc to point to the global hash table entry
8750 for this symbol. The symbol index is then
8751 set at the end of bfd_elf_final_link. */
8752 indx = r_symndx - extsymoff;
8753 rh = elf_sym_hashes (input_bfd)[indx];
8754 while (rh->root.type == bfd_link_hash_indirect
8755 || rh->root.type == bfd_link_hash_warning)
8756 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
8758 /* Setting the index to -2 tells
8759 elf_link_output_extsym that this symbol is
8761 BFD_ASSERT (rh->indx < 0);
8769 /* This is a reloc against a local symbol. */
8772 sym = isymbuf[r_symndx];
8773 sec = finfo->sections[r_symndx];
8774 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
8776 /* I suppose the backend ought to fill in the
8777 section of any STT_SECTION symbol against a
8778 processor specific section. */
8780 if (bfd_is_abs_section (sec))
8782 else if (sec == NULL || sec->owner == NULL)
8784 bfd_set_error (bfd_error_bad_value);
8789 asection *osec = sec->output_section;
8791 /* If we have discarded a section, the output
8792 section will be the absolute section. In
8793 case of discarded SEC_MERGE sections, use
8794 the kept section. relocate_section should
8795 have already handled discarded linkonce
8797 if (bfd_is_abs_section (osec)
8798 && sec->kept_section != NULL
8799 && sec->kept_section->output_section != NULL)
8801 osec = sec->kept_section->output_section;
8802 irela->r_addend -= osec->vma;
8805 if (!bfd_is_abs_section (osec))
8807 r_symndx = osec->target_index;
8810 struct elf_link_hash_table *htab;
8813 htab = elf_hash_table (finfo->info);
8814 oi = htab->text_index_section;
8815 if ((osec->flags & SEC_READONLY) == 0
8816 && htab->data_index_section != NULL)
8817 oi = htab->data_index_section;
8821 irela->r_addend += osec->vma - oi->vma;
8822 r_symndx = oi->target_index;
8826 BFD_ASSERT (r_symndx != 0);
8830 /* Adjust the addend according to where the
8831 section winds up in the output section. */
8833 irela->r_addend += sec->output_offset;
8837 if (finfo->indices[r_symndx] == -1)
8839 unsigned long shlink;
8843 if (finfo->info->strip == strip_all)
8845 /* You can't do ld -r -s. */
8846 bfd_set_error (bfd_error_invalid_operation);
8850 /* This symbol was skipped earlier, but
8851 since it is needed by a reloc, we
8852 must output it now. */
8853 shlink = symtab_hdr->sh_link;
8854 name = (bfd_elf_string_from_elf_section
8855 (input_bfd, shlink, sym.st_name));
8859 osec = sec->output_section;
8861 _bfd_elf_section_from_bfd_section (output_bfd,
8863 if (sym.st_shndx == SHN_BAD)
8866 sym.st_value += sec->output_offset;
8867 if (! finfo->info->relocatable)
8869 sym.st_value += osec->vma;
8870 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
8872 /* STT_TLS symbols are relative to PT_TLS
8874 BFD_ASSERT (elf_hash_table (finfo->info)
8876 sym.st_value -= (elf_hash_table (finfo->info)
8881 finfo->indices[r_symndx]
8882 = bfd_get_symcount (output_bfd);
8884 if (! elf_link_output_sym (finfo, name, &sym, sec,
8889 r_symndx = finfo->indices[r_symndx];
8892 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
8893 | (irela->r_info & r_type_mask));
8896 /* Swap out the relocs. */
8897 if (input_rel_hdr->sh_size != 0
8898 && !bed->elf_backend_emit_relocs (output_bfd, o,
8904 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
8905 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
8907 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
8908 * bed->s->int_rels_per_ext_rel);
8909 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
8910 if (!bed->elf_backend_emit_relocs (output_bfd, o,
8919 /* Write out the modified section contents. */
8920 if (bed->elf_backend_write_section
8921 && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
8924 /* Section written out. */
8926 else switch (o->sec_info_type)
8928 case ELF_INFO_TYPE_STABS:
8929 if (! (_bfd_write_section_stabs
8931 &elf_hash_table (finfo->info)->stab_info,
8932 o, &elf_section_data (o)->sec_info, contents)))
8935 case ELF_INFO_TYPE_MERGE:
8936 if (! _bfd_write_merged_section (output_bfd, o,
8937 elf_section_data (o)->sec_info))
8940 case ELF_INFO_TYPE_EH_FRAME:
8942 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
8949 if (! (o->flags & SEC_EXCLUDE)
8950 && ! bfd_set_section_contents (output_bfd, o->output_section,
8952 (file_ptr) o->output_offset,
8963 /* Generate a reloc when linking an ELF file. This is a reloc
8964 requested by the linker, and does not come from any input file. This
8965 is used to build constructor and destructor tables when linking
8969 elf_reloc_link_order (bfd *output_bfd,
8970 struct bfd_link_info *info,
8971 asection *output_section,
8972 struct bfd_link_order *link_order)
8974 reloc_howto_type *howto;
8978 struct elf_link_hash_entry **rel_hash_ptr;
8979 Elf_Internal_Shdr *rel_hdr;
8980 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
8981 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
8985 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
8988 bfd_set_error (bfd_error_bad_value);
8992 addend = link_order->u.reloc.p->addend;
8994 /* Figure out the symbol index. */
8995 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
8996 + elf_section_data (output_section)->rel_count
8997 + elf_section_data (output_section)->rel_count2);
8998 if (link_order->type == bfd_section_reloc_link_order)
9000 indx = link_order->u.reloc.p->u.section->target_index;
9001 BFD_ASSERT (indx != 0);
9002 *rel_hash_ptr = NULL;
9006 struct elf_link_hash_entry *h;
9008 /* Treat a reloc against a defined symbol as though it were
9009 actually against the section. */
9010 h = ((struct elf_link_hash_entry *)
9011 bfd_wrapped_link_hash_lookup (output_bfd, info,
9012 link_order->u.reloc.p->u.name,
9013 FALSE, FALSE, TRUE));
9015 && (h->root.type == bfd_link_hash_defined
9016 || h->root.type == bfd_link_hash_defweak))
9020 section = h->root.u.def.section;
9021 indx = section->output_section->target_index;
9022 *rel_hash_ptr = NULL;
9023 /* It seems that we ought to add the symbol value to the
9024 addend here, but in practice it has already been added
9025 because it was passed to constructor_callback. */
9026 addend += section->output_section->vma + section->output_offset;
9030 /* Setting the index to -2 tells elf_link_output_extsym that
9031 this symbol is used by a reloc. */
9038 if (! ((*info->callbacks->unattached_reloc)
9039 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
9045 /* If this is an inplace reloc, we must write the addend into the
9047 if (howto->partial_inplace && addend != 0)
9050 bfd_reloc_status_type rstat;
9053 const char *sym_name;
9055 size = bfd_get_reloc_size (howto);
9056 buf = bfd_zmalloc (size);
9059 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
9066 case bfd_reloc_outofrange:
9069 case bfd_reloc_overflow:
9070 if (link_order->type == bfd_section_reloc_link_order)
9071 sym_name = bfd_section_name (output_bfd,
9072 link_order->u.reloc.p->u.section);
9074 sym_name = link_order->u.reloc.p->u.name;
9075 if (! ((*info->callbacks->reloc_overflow)
9076 (info, NULL, sym_name, howto->name, addend, NULL,
9077 NULL, (bfd_vma) 0)))
9084 ok = bfd_set_section_contents (output_bfd, output_section, buf,
9085 link_order->offset, size);
9091 /* The address of a reloc is relative to the section in a
9092 relocatable file, and is a virtual address in an executable
9094 offset = link_order->offset;
9095 if (! info->relocatable)
9096 offset += output_section->vma;
9098 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
9100 irel[i].r_offset = offset;
9102 irel[i].r_addend = 0;
9104 if (bed->s->arch_size == 32)
9105 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
9107 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
9109 rel_hdr = &elf_section_data (output_section)->rel_hdr;
9110 erel = rel_hdr->contents;
9111 if (rel_hdr->sh_type == SHT_REL)
9113 erel += (elf_section_data (output_section)->rel_count
9114 * bed->s->sizeof_rel);
9115 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
9119 irel[0].r_addend = addend;
9120 erel += (elf_section_data (output_section)->rel_count
9121 * bed->s->sizeof_rela);
9122 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
9125 ++elf_section_data (output_section)->rel_count;
9131 /* Get the output vma of the section pointed to by the sh_link field. */
9134 elf_get_linked_section_vma (struct bfd_link_order *p)
9136 Elf_Internal_Shdr **elf_shdrp;
9140 s = p->u.indirect.section;
9141 elf_shdrp = elf_elfsections (s->owner);
9142 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
9143 elfsec = elf_shdrp[elfsec]->sh_link;
9145 The Intel C compiler generates SHT_IA_64_UNWIND with
9146 SHF_LINK_ORDER. But it doesn't set the sh_link or
9147 sh_info fields. Hence we could get the situation
9148 where elfsec is 0. */
9151 const struct elf_backend_data *bed
9152 = get_elf_backend_data (s->owner);
9153 if (bed->link_order_error_handler)
9154 bed->link_order_error_handler
9155 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
9160 s = elf_shdrp[elfsec]->bfd_section;
9161 return s->output_section->vma + s->output_offset;
9166 /* Compare two sections based on the locations of the sections they are
9167 linked to. Used by elf_fixup_link_order. */
9170 compare_link_order (const void * a, const void * b)
9175 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
9176 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
9183 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
9184 order as their linked sections. Returns false if this could not be done
9185 because an output section includes both ordered and unordered
9186 sections. Ideally we'd do this in the linker proper. */
9189 elf_fixup_link_order (bfd *abfd, asection *o)
9194 struct bfd_link_order *p;
9196 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9198 struct bfd_link_order **sections;
9199 asection *s, *other_sec, *linkorder_sec;
9203 linkorder_sec = NULL;
9206 for (p = o->map_head.link_order; p != NULL; p = p->next)
9208 if (p->type == bfd_indirect_link_order)
9210 s = p->u.indirect.section;
9212 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
9213 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
9214 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
9215 && elfsec < elf_numsections (sub)
9216 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
9230 if (seen_other && seen_linkorder)
9232 if (other_sec && linkorder_sec)
9233 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9235 linkorder_sec->owner, other_sec,
9238 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
9240 bfd_set_error (bfd_error_bad_value);
9245 if (!seen_linkorder)
9248 sections = (struct bfd_link_order **)
9249 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
9252 for (p = o->map_head.link_order; p != NULL; p = p->next)
9254 sections[seen_linkorder++] = p;
9256 /* Sort the input sections in the order of their linked section. */
9257 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
9258 compare_link_order);
9260 /* Change the offsets of the sections. */
9262 for (n = 0; n < seen_linkorder; n++)
9264 s = sections[n]->u.indirect.section;
9265 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
9266 s->output_offset = offset;
9267 sections[n]->offset = offset;
9268 offset += sections[n]->size;
9275 /* Do the final step of an ELF link. */
9278 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
9280 bfd_boolean dynamic;
9281 bfd_boolean emit_relocs;
9283 struct elf_final_link_info finfo;
9284 register asection *o;
9285 register struct bfd_link_order *p;
9287 bfd_size_type max_contents_size;
9288 bfd_size_type max_external_reloc_size;
9289 bfd_size_type max_internal_reloc_count;
9290 bfd_size_type max_sym_count;
9291 bfd_size_type max_sym_shndx_count;
9293 Elf_Internal_Sym elfsym;
9295 Elf_Internal_Shdr *symtab_hdr;
9296 Elf_Internal_Shdr *symtab_shndx_hdr;
9297 Elf_Internal_Shdr *symstrtab_hdr;
9298 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9299 struct elf_outext_info eoinfo;
9301 size_t relativecount = 0;
9302 asection *reldyn = 0;
9304 asection *attr_section = NULL;
9305 bfd_vma attr_size = 0;
9306 const char *std_attrs_section;
9308 if (! is_elf_hash_table (info->hash))
9312 abfd->flags |= DYNAMIC;
9314 dynamic = elf_hash_table (info)->dynamic_sections_created;
9315 dynobj = elf_hash_table (info)->dynobj;
9317 emit_relocs = (info->relocatable
9318 || info->emitrelocations);
9321 finfo.output_bfd = abfd;
9322 finfo.symstrtab = _bfd_elf_stringtab_init ();
9323 if (finfo.symstrtab == NULL)
9328 finfo.dynsym_sec = NULL;
9329 finfo.hash_sec = NULL;
9330 finfo.symver_sec = NULL;
9334 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
9335 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
9336 BFD_ASSERT (finfo.dynsym_sec != NULL);
9337 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
9338 /* Note that it is OK if symver_sec is NULL. */
9341 finfo.contents = NULL;
9342 finfo.external_relocs = NULL;
9343 finfo.internal_relocs = NULL;
9344 finfo.external_syms = NULL;
9345 finfo.locsym_shndx = NULL;
9346 finfo.internal_syms = NULL;
9347 finfo.indices = NULL;
9348 finfo.sections = NULL;
9349 finfo.symbuf = NULL;
9350 finfo.symshndxbuf = NULL;
9351 finfo.symbuf_count = 0;
9352 finfo.shndxbuf_size = 0;
9354 /* The object attributes have been merged. Remove the input
9355 sections from the link, and set the contents of the output
9357 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
9358 for (o = abfd->sections; o != NULL; o = o->next)
9360 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
9361 || strcmp (o->name, ".gnu.attributes") == 0)
9363 for (p = o->map_head.link_order; p != NULL; p = p->next)
9365 asection *input_section;
9367 if (p->type != bfd_indirect_link_order)
9369 input_section = p->u.indirect.section;
9370 /* Hack: reset the SEC_HAS_CONTENTS flag so that
9371 elf_link_input_bfd ignores this section. */
9372 input_section->flags &= ~SEC_HAS_CONTENTS;
9375 attr_size = bfd_elf_obj_attr_size (abfd);
9378 bfd_set_section_size (abfd, o, attr_size);
9380 /* Skip this section later on. */
9381 o->map_head.link_order = NULL;
9384 o->flags |= SEC_EXCLUDE;
9388 /* Count up the number of relocations we will output for each output
9389 section, so that we know the sizes of the reloc sections. We
9390 also figure out some maximum sizes. */
9391 max_contents_size = 0;
9392 max_external_reloc_size = 0;
9393 max_internal_reloc_count = 0;
9395 max_sym_shndx_count = 0;
9397 for (o = abfd->sections; o != NULL; o = o->next)
9399 struct bfd_elf_section_data *esdo = elf_section_data (o);
9402 for (p = o->map_head.link_order; p != NULL; p = p->next)
9404 unsigned int reloc_count = 0;
9405 struct bfd_elf_section_data *esdi = NULL;
9406 unsigned int *rel_count1;
9408 if (p->type == bfd_section_reloc_link_order
9409 || p->type == bfd_symbol_reloc_link_order)
9411 else if (p->type == bfd_indirect_link_order)
9415 sec = p->u.indirect.section;
9416 esdi = elf_section_data (sec);
9418 /* Mark all sections which are to be included in the
9419 link. This will normally be every section. We need
9420 to do this so that we can identify any sections which
9421 the linker has decided to not include. */
9422 sec->linker_mark = TRUE;
9424 if (sec->flags & SEC_MERGE)
9427 if (info->relocatable || info->emitrelocations)
9428 reloc_count = sec->reloc_count;
9429 else if (bed->elf_backend_count_relocs)
9431 Elf_Internal_Rela * relocs;
9433 relocs = _bfd_elf_link_read_relocs (sec->owner, sec,
9440 = (*bed->elf_backend_count_relocs) (sec, relocs);
9442 if (elf_section_data (sec)->relocs != relocs)
9447 if (sec->rawsize > max_contents_size)
9448 max_contents_size = sec->rawsize;
9449 if (sec->size > max_contents_size)
9450 max_contents_size = sec->size;
9452 /* We are interested in just local symbols, not all
9454 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
9455 && (sec->owner->flags & DYNAMIC) == 0)
9459 if (elf_bad_symtab (sec->owner))
9460 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
9461 / bed->s->sizeof_sym);
9463 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
9465 if (sym_count > max_sym_count)
9466 max_sym_count = sym_count;
9468 if (sym_count > max_sym_shndx_count
9469 && elf_symtab_shndx (sec->owner) != 0)
9470 max_sym_shndx_count = sym_count;
9472 if ((sec->flags & SEC_RELOC) != 0)
9476 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
9477 if (ext_size > max_external_reloc_size)
9478 max_external_reloc_size = ext_size;
9479 if (sec->reloc_count > max_internal_reloc_count)
9480 max_internal_reloc_count = sec->reloc_count;
9485 if (reloc_count == 0)
9488 o->reloc_count += reloc_count;
9490 /* MIPS may have a mix of REL and RELA relocs on sections.
9491 To support this curious ABI we keep reloc counts in
9492 elf_section_data too. We must be careful to add the
9493 relocations from the input section to the right output
9494 count. FIXME: Get rid of one count. We have
9495 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
9496 rel_count1 = &esdo->rel_count;
9499 bfd_boolean same_size;
9500 bfd_size_type entsize1;
9502 entsize1 = esdi->rel_hdr.sh_entsize;
9503 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
9504 || entsize1 == bed->s->sizeof_rela);
9505 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
9508 rel_count1 = &esdo->rel_count2;
9510 if (esdi->rel_hdr2 != NULL)
9512 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
9513 unsigned int alt_count;
9514 unsigned int *rel_count2;
9516 BFD_ASSERT (entsize2 != entsize1
9517 && (entsize2 == bed->s->sizeof_rel
9518 || entsize2 == bed->s->sizeof_rela));
9520 rel_count2 = &esdo->rel_count2;
9522 rel_count2 = &esdo->rel_count;
9524 /* The following is probably too simplistic if the
9525 backend counts output relocs unusually. */
9526 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
9527 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
9528 *rel_count2 += alt_count;
9529 reloc_count -= alt_count;
9532 *rel_count1 += reloc_count;
9535 if (o->reloc_count > 0)
9536 o->flags |= SEC_RELOC;
9539 /* Explicitly clear the SEC_RELOC flag. The linker tends to
9540 set it (this is probably a bug) and if it is set
9541 assign_section_numbers will create a reloc section. */
9542 o->flags &=~ SEC_RELOC;
9545 /* If the SEC_ALLOC flag is not set, force the section VMA to
9546 zero. This is done in elf_fake_sections as well, but forcing
9547 the VMA to 0 here will ensure that relocs against these
9548 sections are handled correctly. */
9549 if ((o->flags & SEC_ALLOC) == 0
9550 && ! o->user_set_vma)
9554 if (! info->relocatable && merged)
9555 elf_link_hash_traverse (elf_hash_table (info),
9556 _bfd_elf_link_sec_merge_syms, abfd);
9558 /* Figure out the file positions for everything but the symbol table
9559 and the relocs. We set symcount to force assign_section_numbers
9560 to create a symbol table. */
9561 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
9562 BFD_ASSERT (! abfd->output_has_begun);
9563 if (! _bfd_elf_compute_section_file_positions (abfd, info))
9566 /* Set sizes, and assign file positions for reloc sections. */
9567 for (o = abfd->sections; o != NULL; o = o->next)
9569 if ((o->flags & SEC_RELOC) != 0)
9571 if (!(_bfd_elf_link_size_reloc_section
9572 (abfd, &elf_section_data (o)->rel_hdr, o)))
9575 if (elf_section_data (o)->rel_hdr2
9576 && !(_bfd_elf_link_size_reloc_section
9577 (abfd, elf_section_data (o)->rel_hdr2, o)))
9581 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
9582 to count upwards while actually outputting the relocations. */
9583 elf_section_data (o)->rel_count = 0;
9584 elf_section_data (o)->rel_count2 = 0;
9587 _bfd_elf_assign_file_positions_for_relocs (abfd);
9589 /* We have now assigned file positions for all the sections except
9590 .symtab and .strtab. We start the .symtab section at the current
9591 file position, and write directly to it. We build the .strtab
9592 section in memory. */
9593 bfd_get_symcount (abfd) = 0;
9594 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9595 /* sh_name is set in prep_headers. */
9596 symtab_hdr->sh_type = SHT_SYMTAB;
9597 /* sh_flags, sh_addr and sh_size all start off zero. */
9598 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
9599 /* sh_link is set in assign_section_numbers. */
9600 /* sh_info is set below. */
9601 /* sh_offset is set just below. */
9602 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
9604 off = elf_tdata (abfd)->next_file_pos;
9605 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
9607 /* Note that at this point elf_tdata (abfd)->next_file_pos is
9608 incorrect. We do not yet know the size of the .symtab section.
9609 We correct next_file_pos below, after we do know the size. */
9611 /* Allocate a buffer to hold swapped out symbols. This is to avoid
9612 continuously seeking to the right position in the file. */
9613 if (! info->keep_memory || max_sym_count < 20)
9614 finfo.symbuf_size = 20;
9616 finfo.symbuf_size = max_sym_count;
9617 amt = finfo.symbuf_size;
9618 amt *= bed->s->sizeof_sym;
9619 finfo.symbuf = bfd_malloc (amt);
9620 if (finfo.symbuf == NULL)
9622 if (elf_numsections (abfd) > SHN_LORESERVE)
9624 /* Wild guess at number of output symbols. realloc'd as needed. */
9625 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
9626 finfo.shndxbuf_size = amt;
9627 amt *= sizeof (Elf_External_Sym_Shndx);
9628 finfo.symshndxbuf = bfd_zmalloc (amt);
9629 if (finfo.symshndxbuf == NULL)
9633 /* Start writing out the symbol table. The first symbol is always a
9635 if (info->strip != strip_all
9638 elfsym.st_value = 0;
9641 elfsym.st_other = 0;
9642 elfsym.st_shndx = SHN_UNDEF;
9643 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
9648 /* Output a symbol for each section. We output these even if we are
9649 discarding local symbols, since they are used for relocs. These
9650 symbols have no names. We store the index of each one in the
9651 index field of the section, so that we can find it again when
9652 outputting relocs. */
9653 if (info->strip != strip_all
9657 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
9658 elfsym.st_other = 0;
9659 elfsym.st_value = 0;
9660 for (i = 1; i < elf_numsections (abfd); i++)
9662 o = bfd_section_from_elf_index (abfd, i);
9665 o->target_index = bfd_get_symcount (abfd);
9666 elfsym.st_shndx = i;
9667 if (!info->relocatable)
9668 elfsym.st_value = o->vma;
9669 if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
9672 if (i == SHN_LORESERVE - 1)
9673 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
9677 /* Allocate some memory to hold information read in from the input
9679 if (max_contents_size != 0)
9681 finfo.contents = bfd_malloc (max_contents_size);
9682 if (finfo.contents == NULL)
9686 if (max_external_reloc_size != 0)
9688 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
9689 if (finfo.external_relocs == NULL)
9693 if (max_internal_reloc_count != 0)
9695 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
9696 amt *= sizeof (Elf_Internal_Rela);
9697 finfo.internal_relocs = bfd_malloc (amt);
9698 if (finfo.internal_relocs == NULL)
9702 if (max_sym_count != 0)
9704 amt = max_sym_count * bed->s->sizeof_sym;
9705 finfo.external_syms = bfd_malloc (amt);
9706 if (finfo.external_syms == NULL)
9709 amt = max_sym_count * sizeof (Elf_Internal_Sym);
9710 finfo.internal_syms = bfd_malloc (amt);
9711 if (finfo.internal_syms == NULL)
9714 amt = max_sym_count * sizeof (long);
9715 finfo.indices = bfd_malloc (amt);
9716 if (finfo.indices == NULL)
9719 amt = max_sym_count * sizeof (asection *);
9720 finfo.sections = bfd_malloc (amt);
9721 if (finfo.sections == NULL)
9725 if (max_sym_shndx_count != 0)
9727 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
9728 finfo.locsym_shndx = bfd_malloc (amt);
9729 if (finfo.locsym_shndx == NULL)
9733 if (elf_hash_table (info)->tls_sec)
9735 bfd_vma base, end = 0;
9738 for (sec = elf_hash_table (info)->tls_sec;
9739 sec && (sec->flags & SEC_THREAD_LOCAL);
9742 bfd_size_type size = sec->size;
9745 && (sec->flags & SEC_HAS_CONTENTS) == 0)
9747 struct bfd_link_order *o = sec->map_tail.link_order;
9749 size = o->offset + o->size;
9751 end = sec->vma + size;
9753 base = elf_hash_table (info)->tls_sec->vma;
9754 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
9755 elf_hash_table (info)->tls_size = end - base;
9758 /* Reorder SHF_LINK_ORDER sections. */
9759 for (o = abfd->sections; o != NULL; o = o->next)
9761 if (!elf_fixup_link_order (abfd, o))
9765 /* Since ELF permits relocations to be against local symbols, we
9766 must have the local symbols available when we do the relocations.
9767 Since we would rather only read the local symbols once, and we
9768 would rather not keep them in memory, we handle all the
9769 relocations for a single input file at the same time.
9771 Unfortunately, there is no way to know the total number of local
9772 symbols until we have seen all of them, and the local symbol
9773 indices precede the global symbol indices. This means that when
9774 we are generating relocatable output, and we see a reloc against
9775 a global symbol, we can not know the symbol index until we have
9776 finished examining all the local symbols to see which ones we are
9777 going to output. To deal with this, we keep the relocations in
9778 memory, and don't output them until the end of the link. This is
9779 an unfortunate waste of memory, but I don't see a good way around
9780 it. Fortunately, it only happens when performing a relocatable
9781 link, which is not the common case. FIXME: If keep_memory is set
9782 we could write the relocs out and then read them again; I don't
9783 know how bad the memory loss will be. */
9785 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9786 sub->output_has_begun = FALSE;
9787 for (o = abfd->sections; o != NULL; o = o->next)
9789 for (p = o->map_head.link_order; p != NULL; p = p->next)
9791 if (p->type == bfd_indirect_link_order
9792 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
9793 == bfd_target_elf_flavour)
9794 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
9796 if (! sub->output_has_begun)
9798 if (! elf_link_input_bfd (&finfo, sub))
9800 sub->output_has_begun = TRUE;
9803 else if (p->type == bfd_section_reloc_link_order
9804 || p->type == bfd_symbol_reloc_link_order)
9806 if (! elf_reloc_link_order (abfd, info, o, p))
9811 if (! _bfd_default_link_order (abfd, info, o, p))
9817 /* Free symbol buffer if needed. */
9818 if (!info->reduce_memory_overheads)
9820 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9821 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
9822 && elf_tdata (sub)->symbuf)
9824 free (elf_tdata (sub)->symbuf);
9825 elf_tdata (sub)->symbuf = NULL;
9829 /* Output any global symbols that got converted to local in a
9830 version script or due to symbol visibility. We do this in a
9831 separate step since ELF requires all local symbols to appear
9832 prior to any global symbols. FIXME: We should only do this if
9833 some global symbols were, in fact, converted to become local.
9834 FIXME: Will this work correctly with the Irix 5 linker? */
9835 eoinfo.failed = FALSE;
9836 eoinfo.finfo = &finfo;
9837 eoinfo.localsyms = TRUE;
9838 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
9843 /* If backend needs to output some local symbols not present in the hash
9844 table, do it now. */
9845 if (bed->elf_backend_output_arch_local_syms)
9847 typedef bfd_boolean (*out_sym_func)
9848 (void *, const char *, Elf_Internal_Sym *, asection *,
9849 struct elf_link_hash_entry *);
9851 if (! ((*bed->elf_backend_output_arch_local_syms)
9852 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
9856 /* That wrote out all the local symbols. Finish up the symbol table
9857 with the global symbols. Even if we want to strip everything we
9858 can, we still need to deal with those global symbols that got
9859 converted to local in a version script. */
9861 /* The sh_info field records the index of the first non local symbol. */
9862 symtab_hdr->sh_info = bfd_get_symcount (abfd);
9865 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
9867 Elf_Internal_Sym sym;
9868 bfd_byte *dynsym = finfo.dynsym_sec->contents;
9869 long last_local = 0;
9871 /* Write out the section symbols for the output sections. */
9872 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
9878 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
9881 for (s = abfd->sections; s != NULL; s = s->next)
9887 dynindx = elf_section_data (s)->dynindx;
9890 indx = elf_section_data (s)->this_idx;
9891 BFD_ASSERT (indx > 0);
9892 sym.st_shndx = indx;
9893 if (! check_dynsym (abfd, &sym))
9895 sym.st_value = s->vma;
9896 dest = dynsym + dynindx * bed->s->sizeof_sym;
9897 if (last_local < dynindx)
9898 last_local = dynindx;
9899 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
9903 /* Write out the local dynsyms. */
9904 if (elf_hash_table (info)->dynlocal)
9906 struct elf_link_local_dynamic_entry *e;
9907 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
9912 sym.st_size = e->isym.st_size;
9913 sym.st_other = e->isym.st_other;
9915 /* Copy the internal symbol as is.
9916 Note that we saved a word of storage and overwrote
9917 the original st_name with the dynstr_index. */
9920 if (e->isym.st_shndx != SHN_UNDEF
9921 && (e->isym.st_shndx < SHN_LORESERVE
9922 || e->isym.st_shndx > SHN_HIRESERVE))
9924 s = bfd_section_from_elf_index (e->input_bfd,
9928 elf_section_data (s->output_section)->this_idx;
9929 if (! check_dynsym (abfd, &sym))
9931 sym.st_value = (s->output_section->vma
9933 + e->isym.st_value);
9936 if (last_local < e->dynindx)
9937 last_local = e->dynindx;
9939 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
9940 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
9944 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
9948 /* We get the global symbols from the hash table. */
9949 eoinfo.failed = FALSE;
9950 eoinfo.localsyms = FALSE;
9951 eoinfo.finfo = &finfo;
9952 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
9957 /* If backend needs to output some symbols not present in the hash
9958 table, do it now. */
9959 if (bed->elf_backend_output_arch_syms)
9961 typedef bfd_boolean (*out_sym_func)
9962 (void *, const char *, Elf_Internal_Sym *, asection *,
9963 struct elf_link_hash_entry *);
9965 if (! ((*bed->elf_backend_output_arch_syms)
9966 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
9970 /* Flush all symbols to the file. */
9971 if (! elf_link_flush_output_syms (&finfo, bed))
9974 /* Now we know the size of the symtab section. */
9975 off += symtab_hdr->sh_size;
9977 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
9978 if (symtab_shndx_hdr->sh_name != 0)
9980 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
9981 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
9982 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
9983 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
9984 symtab_shndx_hdr->sh_size = amt;
9986 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
9989 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
9990 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
9995 /* Finish up and write out the symbol string table (.strtab)
9997 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
9998 /* sh_name was set in prep_headers. */
9999 symstrtab_hdr->sh_type = SHT_STRTAB;
10000 symstrtab_hdr->sh_flags = 0;
10001 symstrtab_hdr->sh_addr = 0;
10002 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
10003 symstrtab_hdr->sh_entsize = 0;
10004 symstrtab_hdr->sh_link = 0;
10005 symstrtab_hdr->sh_info = 0;
10006 /* sh_offset is set just below. */
10007 symstrtab_hdr->sh_addralign = 1;
10009 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
10010 elf_tdata (abfd)->next_file_pos = off;
10012 if (bfd_get_symcount (abfd) > 0)
10014 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
10015 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
10019 /* Adjust the relocs to have the correct symbol indices. */
10020 for (o = abfd->sections; o != NULL; o = o->next)
10022 if ((o->flags & SEC_RELOC) == 0)
10025 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
10026 elf_section_data (o)->rel_count,
10027 elf_section_data (o)->rel_hashes);
10028 if (elf_section_data (o)->rel_hdr2 != NULL)
10029 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
10030 elf_section_data (o)->rel_count2,
10031 (elf_section_data (o)->rel_hashes
10032 + elf_section_data (o)->rel_count));
10034 /* Set the reloc_count field to 0 to prevent write_relocs from
10035 trying to swap the relocs out itself. */
10036 o->reloc_count = 0;
10039 if (dynamic && info->combreloc && dynobj != NULL)
10040 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
10042 /* If we are linking against a dynamic object, or generating a
10043 shared library, finish up the dynamic linking information. */
10046 bfd_byte *dyncon, *dynconend;
10048 /* Fix up .dynamic entries. */
10049 o = bfd_get_section_by_name (dynobj, ".dynamic");
10050 BFD_ASSERT (o != NULL);
10052 dyncon = o->contents;
10053 dynconend = o->contents + o->size;
10054 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10056 Elf_Internal_Dyn dyn;
10060 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10067 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
10069 switch (elf_section_data (reldyn)->this_hdr.sh_type)
10071 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
10072 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
10075 dyn.d_un.d_val = relativecount;
10082 name = info->init_function;
10085 name = info->fini_function;
10088 struct elf_link_hash_entry *h;
10090 h = elf_link_hash_lookup (elf_hash_table (info), name,
10091 FALSE, FALSE, TRUE);
10093 && (h->root.type == bfd_link_hash_defined
10094 || h->root.type == bfd_link_hash_defweak))
10096 dyn.d_un.d_val = h->root.u.def.value;
10097 o = h->root.u.def.section;
10098 if (o->output_section != NULL)
10099 dyn.d_un.d_val += (o->output_section->vma
10100 + o->output_offset);
10103 /* The symbol is imported from another shared
10104 library and does not apply to this one. */
10105 dyn.d_un.d_val = 0;
10112 case DT_PREINIT_ARRAYSZ:
10113 name = ".preinit_array";
10115 case DT_INIT_ARRAYSZ:
10116 name = ".init_array";
10118 case DT_FINI_ARRAYSZ:
10119 name = ".fini_array";
10121 o = bfd_get_section_by_name (abfd, name);
10124 (*_bfd_error_handler)
10125 (_("%B: could not find output section %s"), abfd, name);
10129 (*_bfd_error_handler)
10130 (_("warning: %s section has zero size"), name);
10131 dyn.d_un.d_val = o->size;
10134 case DT_PREINIT_ARRAY:
10135 name = ".preinit_array";
10137 case DT_INIT_ARRAY:
10138 name = ".init_array";
10140 case DT_FINI_ARRAY:
10141 name = ".fini_array";
10148 name = ".gnu.hash";
10157 name = ".gnu.version_d";
10160 name = ".gnu.version_r";
10163 name = ".gnu.version";
10165 o = bfd_get_section_by_name (abfd, name);
10168 (*_bfd_error_handler)
10169 (_("%B: could not find output section %s"), abfd, name);
10172 dyn.d_un.d_ptr = o->vma;
10179 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
10183 dyn.d_un.d_val = 0;
10184 for (i = 1; i < elf_numsections (abfd); i++)
10186 Elf_Internal_Shdr *hdr;
10188 hdr = elf_elfsections (abfd)[i];
10189 if (hdr->sh_type == type
10190 && (hdr->sh_flags & SHF_ALLOC) != 0)
10192 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
10193 dyn.d_un.d_val += hdr->sh_size;
10196 if (dyn.d_un.d_val == 0
10197 || hdr->sh_addr < dyn.d_un.d_val)
10198 dyn.d_un.d_val = hdr->sh_addr;
10204 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
10208 /* If we have created any dynamic sections, then output them. */
10209 if (dynobj != NULL)
10211 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
10214 /* Check for DT_TEXTREL (late, in case the backend removes it). */
10215 if (info->warn_shared_textrel && info->shared)
10217 bfd_byte *dyncon, *dynconend;
10219 /* Fix up .dynamic entries. */
10220 o = bfd_get_section_by_name (dynobj, ".dynamic");
10221 BFD_ASSERT (o != NULL);
10223 dyncon = o->contents;
10224 dynconend = o->contents + o->size;
10225 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10227 Elf_Internal_Dyn dyn;
10229 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10231 if (dyn.d_tag == DT_TEXTREL)
10233 info->callbacks->einfo
10234 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
10240 for (o = dynobj->sections; o != NULL; o = o->next)
10242 if ((o->flags & SEC_HAS_CONTENTS) == 0
10244 || o->output_section == bfd_abs_section_ptr)
10246 if ((o->flags & SEC_LINKER_CREATED) == 0)
10248 /* At this point, we are only interested in sections
10249 created by _bfd_elf_link_create_dynamic_sections. */
10252 if (elf_hash_table (info)->stab_info.stabstr == o)
10254 if (elf_hash_table (info)->eh_info.hdr_sec == o)
10256 if ((elf_section_data (o->output_section)->this_hdr.sh_type
10258 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
10260 if (! bfd_set_section_contents (abfd, o->output_section,
10262 (file_ptr) o->output_offset,
10268 /* The contents of the .dynstr section are actually in a
10270 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
10271 if (bfd_seek (abfd, off, SEEK_SET) != 0
10272 || ! _bfd_elf_strtab_emit (abfd,
10273 elf_hash_table (info)->dynstr))
10279 if (info->relocatable)
10281 bfd_boolean failed = FALSE;
10283 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
10288 /* If we have optimized stabs strings, output them. */
10289 if (elf_hash_table (info)->stab_info.stabstr != NULL)
10291 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
10295 if (info->eh_frame_hdr)
10297 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
10301 if (finfo.symstrtab != NULL)
10302 _bfd_stringtab_free (finfo.symstrtab);
10303 if (finfo.contents != NULL)
10304 free (finfo.contents);
10305 if (finfo.external_relocs != NULL)
10306 free (finfo.external_relocs);
10307 if (finfo.internal_relocs != NULL)
10308 free (finfo.internal_relocs);
10309 if (finfo.external_syms != NULL)
10310 free (finfo.external_syms);
10311 if (finfo.locsym_shndx != NULL)
10312 free (finfo.locsym_shndx);
10313 if (finfo.internal_syms != NULL)
10314 free (finfo.internal_syms);
10315 if (finfo.indices != NULL)
10316 free (finfo.indices);
10317 if (finfo.sections != NULL)
10318 free (finfo.sections);
10319 if (finfo.symbuf != NULL)
10320 free (finfo.symbuf);
10321 if (finfo.symshndxbuf != NULL)
10322 free (finfo.symshndxbuf);
10323 for (o = abfd->sections; o != NULL; o = o->next)
10325 if ((o->flags & SEC_RELOC) != 0
10326 && elf_section_data (o)->rel_hashes != NULL)
10327 free (elf_section_data (o)->rel_hashes);
10330 elf_tdata (abfd)->linker = TRUE;
10334 bfd_byte *contents = bfd_malloc (attr_size);
10335 if (contents == NULL)
10337 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
10338 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
10345 if (finfo.symstrtab != NULL)
10346 _bfd_stringtab_free (finfo.symstrtab);
10347 if (finfo.contents != NULL)
10348 free (finfo.contents);
10349 if (finfo.external_relocs != NULL)
10350 free (finfo.external_relocs);
10351 if (finfo.internal_relocs != NULL)
10352 free (finfo.internal_relocs);
10353 if (finfo.external_syms != NULL)
10354 free (finfo.external_syms);
10355 if (finfo.locsym_shndx != NULL)
10356 free (finfo.locsym_shndx);
10357 if (finfo.internal_syms != NULL)
10358 free (finfo.internal_syms);
10359 if (finfo.indices != NULL)
10360 free (finfo.indices);
10361 if (finfo.sections != NULL)
10362 free (finfo.sections);
10363 if (finfo.symbuf != NULL)
10364 free (finfo.symbuf);
10365 if (finfo.symshndxbuf != NULL)
10366 free (finfo.symshndxbuf);
10367 for (o = abfd->sections; o != NULL; o = o->next)
10369 if ((o->flags & SEC_RELOC) != 0
10370 && elf_section_data (o)->rel_hashes != NULL)
10371 free (elf_section_data (o)->rel_hashes);
10377 /* Garbage collect unused sections. */
10379 /* Default gc_mark_hook. */
10382 _bfd_elf_gc_mark_hook (asection *sec,
10383 struct bfd_link_info *info ATTRIBUTE_UNUSED,
10384 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
10385 struct elf_link_hash_entry *h,
10386 Elf_Internal_Sym *sym)
10390 switch (h->root.type)
10392 case bfd_link_hash_defined:
10393 case bfd_link_hash_defweak:
10394 return h->root.u.def.section;
10396 case bfd_link_hash_common:
10397 return h->root.u.c.p->section;
10404 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
10409 /* The mark phase of garbage collection. For a given section, mark
10410 it and any sections in this section's group, and all the sections
10411 which define symbols to which it refers. */
10414 _bfd_elf_gc_mark (struct bfd_link_info *info,
10416 elf_gc_mark_hook_fn gc_mark_hook)
10420 asection *group_sec;
10424 /* Mark all the sections in the group. */
10425 group_sec = elf_section_data (sec)->next_in_group;
10426 if (group_sec && !group_sec->gc_mark)
10427 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
10430 /* Look through the section relocs. */
10432 is_eh = strcmp (sec->name, ".eh_frame") == 0;
10433 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
10435 Elf_Internal_Rela *relstart, *rel, *relend;
10436 Elf_Internal_Shdr *symtab_hdr;
10437 struct elf_link_hash_entry **sym_hashes;
10440 bfd *input_bfd = sec->owner;
10441 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
10442 Elf_Internal_Sym *isym = NULL;
10445 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10446 sym_hashes = elf_sym_hashes (input_bfd);
10448 /* Read the local symbols. */
10449 if (elf_bad_symtab (input_bfd))
10451 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
10455 extsymoff = nlocsyms = symtab_hdr->sh_info;
10457 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
10458 if (isym == NULL && nlocsyms != 0)
10460 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
10466 /* Read the relocations. */
10467 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
10468 info->keep_memory);
10469 if (relstart == NULL)
10474 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
10476 if (bed->s->arch_size == 32)
10481 for (rel = relstart; rel < relend; rel++)
10483 unsigned long r_symndx;
10485 struct elf_link_hash_entry *h;
10487 r_symndx = rel->r_info >> r_sym_shift;
10491 if (r_symndx >= nlocsyms
10492 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
10494 h = sym_hashes[r_symndx - extsymoff];
10495 while (h->root.type == bfd_link_hash_indirect
10496 || h->root.type == bfd_link_hash_warning)
10497 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10498 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
10502 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
10505 if (rsec && !rsec->gc_mark)
10507 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
10510 rsec->gc_mark_from_eh = 1;
10511 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
10520 if (elf_section_data (sec)->relocs != relstart)
10523 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
10525 if (! info->keep_memory)
10528 symtab_hdr->contents = (unsigned char *) isym;
10535 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
10537 struct elf_gc_sweep_symbol_info
10539 struct bfd_link_info *info;
10540 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
10545 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
10547 if (h->root.type == bfd_link_hash_warning)
10548 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10550 if ((h->root.type == bfd_link_hash_defined
10551 || h->root.type == bfd_link_hash_defweak)
10552 && !h->root.u.def.section->gc_mark
10553 && !(h->root.u.def.section->owner->flags & DYNAMIC))
10555 struct elf_gc_sweep_symbol_info *inf = data;
10556 (*inf->hide_symbol) (inf->info, h, TRUE);
10562 /* The sweep phase of garbage collection. Remove all garbage sections. */
10564 typedef bfd_boolean (*gc_sweep_hook_fn)
10565 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
10568 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
10571 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10572 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
10573 unsigned long section_sym_count;
10574 struct elf_gc_sweep_symbol_info sweep_info;
10576 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10580 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
10583 for (o = sub->sections; o != NULL; o = o->next)
10585 /* Keep debug and special sections. */
10586 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
10587 || elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
10588 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
10594 /* Skip sweeping sections already excluded. */
10595 if (o->flags & SEC_EXCLUDE)
10598 /* Since this is early in the link process, it is simple
10599 to remove a section from the output. */
10600 o->flags |= SEC_EXCLUDE;
10602 if (info->print_gc_sections && o->size != 0)
10603 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
10605 /* But we also have to update some of the relocation
10606 info we collected before. */
10608 && (o->flags & SEC_RELOC) != 0
10609 && o->reloc_count > 0
10610 && !bfd_is_abs_section (o->output_section))
10612 Elf_Internal_Rela *internal_relocs;
10616 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
10617 info->keep_memory);
10618 if (internal_relocs == NULL)
10621 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
10623 if (elf_section_data (o)->relocs != internal_relocs)
10624 free (internal_relocs);
10632 /* Remove the symbols that were in the swept sections from the dynamic
10633 symbol table. GCFIXME: Anyone know how to get them out of the
10634 static symbol table as well? */
10635 sweep_info.info = info;
10636 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
10637 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
10640 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
10644 /* Propagate collected vtable information. This is called through
10645 elf_link_hash_traverse. */
10648 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
10650 if (h->root.type == bfd_link_hash_warning)
10651 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10653 /* Those that are not vtables. */
10654 if (h->vtable == NULL || h->vtable->parent == NULL)
10657 /* Those vtables that do not have parents, we cannot merge. */
10658 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
10661 /* If we've already been done, exit. */
10662 if (h->vtable->used && h->vtable->used[-1])
10665 /* Make sure the parent's table is up to date. */
10666 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
10668 if (h->vtable->used == NULL)
10670 /* None of this table's entries were referenced. Re-use the
10672 h->vtable->used = h->vtable->parent->vtable->used;
10673 h->vtable->size = h->vtable->parent->vtable->size;
10678 bfd_boolean *cu, *pu;
10680 /* Or the parent's entries into ours. */
10681 cu = h->vtable->used;
10683 pu = h->vtable->parent->vtable->used;
10686 const struct elf_backend_data *bed;
10687 unsigned int log_file_align;
10689 bed = get_elf_backend_data (h->root.u.def.section->owner);
10690 log_file_align = bed->s->log_file_align;
10691 n = h->vtable->parent->vtable->size >> log_file_align;
10706 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
10709 bfd_vma hstart, hend;
10710 Elf_Internal_Rela *relstart, *relend, *rel;
10711 const struct elf_backend_data *bed;
10712 unsigned int log_file_align;
10714 if (h->root.type == bfd_link_hash_warning)
10715 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10717 /* Take care of both those symbols that do not describe vtables as
10718 well as those that are not loaded. */
10719 if (h->vtable == NULL || h->vtable->parent == NULL)
10722 BFD_ASSERT (h->root.type == bfd_link_hash_defined
10723 || h->root.type == bfd_link_hash_defweak);
10725 sec = h->root.u.def.section;
10726 hstart = h->root.u.def.value;
10727 hend = hstart + h->size;
10729 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
10731 return *(bfd_boolean *) okp = FALSE;
10732 bed = get_elf_backend_data (sec->owner);
10733 log_file_align = bed->s->log_file_align;
10735 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
10737 for (rel = relstart; rel < relend; ++rel)
10738 if (rel->r_offset >= hstart && rel->r_offset < hend)
10740 /* If the entry is in use, do nothing. */
10741 if (h->vtable->used
10742 && (rel->r_offset - hstart) < h->vtable->size)
10744 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
10745 if (h->vtable->used[entry])
10748 /* Otherwise, kill it. */
10749 rel->r_offset = rel->r_info = rel->r_addend = 0;
10755 /* Mark sections containing dynamically referenced symbols. When
10756 building shared libraries, we must assume that any visible symbol is
10760 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
10762 struct bfd_link_info *info = (struct bfd_link_info *) inf;
10764 if (h->root.type == bfd_link_hash_warning)
10765 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10767 if ((h->root.type == bfd_link_hash_defined
10768 || h->root.type == bfd_link_hash_defweak)
10770 || (!info->executable
10772 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
10773 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
10774 h->root.u.def.section->flags |= SEC_KEEP;
10779 /* Do mark and sweep of unused sections. */
10782 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
10784 bfd_boolean ok = TRUE;
10786 elf_gc_mark_hook_fn gc_mark_hook;
10787 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10789 if (!bed->can_gc_sections
10790 || info->relocatable
10791 || info->emitrelocations
10792 || !is_elf_hash_table (info->hash))
10794 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
10798 /* Apply transitive closure to the vtable entry usage info. */
10799 elf_link_hash_traverse (elf_hash_table (info),
10800 elf_gc_propagate_vtable_entries_used,
10805 /* Kill the vtable relocations that were not used. */
10806 elf_link_hash_traverse (elf_hash_table (info),
10807 elf_gc_smash_unused_vtentry_relocs,
10812 /* Mark dynamically referenced symbols. */
10813 if (elf_hash_table (info)->dynamic_sections_created)
10814 elf_link_hash_traverse (elf_hash_table (info),
10815 bed->gc_mark_dynamic_ref,
10818 /* Grovel through relocs to find out who stays ... */
10819 gc_mark_hook = bed->gc_mark_hook;
10820 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10824 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
10827 for (o = sub->sections; o != NULL; o = o->next)
10828 if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark)
10829 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
10833 /* Allow the backend to mark additional target specific sections. */
10834 if (bed->gc_mark_extra_sections)
10835 bed->gc_mark_extra_sections(info, gc_mark_hook);
10837 /* ... again for sections marked from eh_frame. */
10838 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10842 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
10845 /* Keep .gcc_except_table.* if the associated .text.* (or the
10846 associated .gnu.linkonce.t.* if .text.* doesn't exist) is
10847 marked. This isn't very nice, but the proper solution,
10848 splitting .eh_frame up and using comdat doesn't pan out
10849 easily due to needing special relocs to handle the
10850 difference of two symbols in separate sections.
10851 Don't keep code sections referenced by .eh_frame. */
10852 #define TEXT_PREFIX ".text."
10853 #define TEXT_PREFIX2 ".gnu.linkonce.t."
10854 #define GCC_EXCEPT_TABLE_PREFIX ".gcc_except_table."
10855 for (o = sub->sections; o != NULL; o = o->next)
10856 if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)
10858 if (CONST_STRNEQ (o->name, GCC_EXCEPT_TABLE_PREFIX))
10861 const char *sec_name;
10863 unsigned o_name_prefix_len , fn_name_prefix_len, tmp;
10865 o_name_prefix_len = strlen (GCC_EXCEPT_TABLE_PREFIX);
10866 sec_name = o->name + o_name_prefix_len;
10867 fn_name_prefix_len = strlen (TEXT_PREFIX);
10868 tmp = strlen (TEXT_PREFIX2);
10869 if (tmp > fn_name_prefix_len)
10870 fn_name_prefix_len = tmp;
10872 = bfd_malloc (fn_name_prefix_len + strlen (sec_name) + 1);
10873 if (fn_name == NULL)
10876 /* Try the first prefix. */
10877 sprintf (fn_name, "%s%s", TEXT_PREFIX, sec_name);
10878 fn_text = bfd_get_section_by_name (sub, fn_name);
10880 /* Try the second prefix. */
10881 if (fn_text == NULL)
10883 sprintf (fn_name, "%s%s", TEXT_PREFIX2, sec_name);
10884 fn_text = bfd_get_section_by_name (sub, fn_name);
10888 if (fn_text == NULL || !fn_text->gc_mark)
10892 /* If not using specially named exception table section,
10893 then keep whatever we are using. */
10894 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
10899 /* ... and mark SEC_EXCLUDE for those that go. */
10900 return elf_gc_sweep (abfd, info);
10903 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
10906 bfd_elf_gc_record_vtinherit (bfd *abfd,
10908 struct elf_link_hash_entry *h,
10911 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
10912 struct elf_link_hash_entry **search, *child;
10913 bfd_size_type extsymcount;
10914 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10916 /* The sh_info field of the symtab header tells us where the
10917 external symbols start. We don't care about the local symbols at
10919 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
10920 if (!elf_bad_symtab (abfd))
10921 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
10923 sym_hashes = elf_sym_hashes (abfd);
10924 sym_hashes_end = sym_hashes + extsymcount;
10926 /* Hunt down the child symbol, which is in this section at the same
10927 offset as the relocation. */
10928 for (search = sym_hashes; search != sym_hashes_end; ++search)
10930 if ((child = *search) != NULL
10931 && (child->root.type == bfd_link_hash_defined
10932 || child->root.type == bfd_link_hash_defweak)
10933 && child->root.u.def.section == sec
10934 && child->root.u.def.value == offset)
10938 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
10939 abfd, sec, (unsigned long) offset);
10940 bfd_set_error (bfd_error_invalid_operation);
10944 if (!child->vtable)
10946 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
10947 if (!child->vtable)
10952 /* This *should* only be the absolute section. It could potentially
10953 be that someone has defined a non-global vtable though, which
10954 would be bad. It isn't worth paging in the local symbols to be
10955 sure though; that case should simply be handled by the assembler. */
10957 child->vtable->parent = (struct elf_link_hash_entry *) -1;
10960 child->vtable->parent = h;
10965 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
10968 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
10969 asection *sec ATTRIBUTE_UNUSED,
10970 struct elf_link_hash_entry *h,
10973 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10974 unsigned int log_file_align = bed->s->log_file_align;
10978 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
10983 if (addend >= h->vtable->size)
10985 size_t size, bytes, file_align;
10986 bfd_boolean *ptr = h->vtable->used;
10988 /* While the symbol is undefined, we have to be prepared to handle
10990 file_align = 1 << log_file_align;
10991 if (h->root.type == bfd_link_hash_undefined)
10992 size = addend + file_align;
10996 if (addend >= size)
10998 /* Oops! We've got a reference past the defined end of
10999 the table. This is probably a bug -- shall we warn? */
11000 size = addend + file_align;
11003 size = (size + file_align - 1) & -file_align;
11005 /* Allocate one extra entry for use as a "done" flag for the
11006 consolidation pass. */
11007 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
11011 ptr = bfd_realloc (ptr - 1, bytes);
11017 oldbytes = (((h->vtable->size >> log_file_align) + 1)
11018 * sizeof (bfd_boolean));
11019 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
11023 ptr = bfd_zmalloc (bytes);
11028 /* And arrange for that done flag to be at index -1. */
11029 h->vtable->used = ptr + 1;
11030 h->vtable->size = size;
11033 h->vtable->used[addend >> log_file_align] = TRUE;
11038 struct alloc_got_off_arg {
11040 unsigned int got_elt_size;
11043 /* We need a special top-level link routine to convert got reference counts
11044 to real got offsets. */
11047 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
11049 struct alloc_got_off_arg *gofarg = arg;
11051 if (h->root.type == bfd_link_hash_warning)
11052 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11054 if (h->got.refcount > 0)
11056 h->got.offset = gofarg->gotoff;
11057 gofarg->gotoff += gofarg->got_elt_size;
11060 h->got.offset = (bfd_vma) -1;
11065 /* And an accompanying bit to work out final got entry offsets once
11066 we're done. Should be called from final_link. */
11069 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
11070 struct bfd_link_info *info)
11073 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11075 unsigned int got_elt_size = bed->s->arch_size / 8;
11076 struct alloc_got_off_arg gofarg;
11078 if (! is_elf_hash_table (info->hash))
11081 /* The GOT offset is relative to the .got section, but the GOT header is
11082 put into the .got.plt section, if the backend uses it. */
11083 if (bed->want_got_plt)
11086 gotoff = bed->got_header_size;
11088 /* Do the local .got entries first. */
11089 for (i = info->input_bfds; i; i = i->link_next)
11091 bfd_signed_vma *local_got;
11092 bfd_size_type j, locsymcount;
11093 Elf_Internal_Shdr *symtab_hdr;
11095 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
11098 local_got = elf_local_got_refcounts (i);
11102 symtab_hdr = &elf_tdata (i)->symtab_hdr;
11103 if (elf_bad_symtab (i))
11104 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11106 locsymcount = symtab_hdr->sh_info;
11108 for (j = 0; j < locsymcount; ++j)
11110 if (local_got[j] > 0)
11112 local_got[j] = gotoff;
11113 gotoff += got_elt_size;
11116 local_got[j] = (bfd_vma) -1;
11120 /* Then the global .got entries. .plt refcounts are handled by
11121 adjust_dynamic_symbol */
11122 gofarg.gotoff = gotoff;
11123 gofarg.got_elt_size = got_elt_size;
11124 elf_link_hash_traverse (elf_hash_table (info),
11125 elf_gc_allocate_got_offsets,
11130 /* Many folk need no more in the way of final link than this, once
11131 got entry reference counting is enabled. */
11134 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
11136 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
11139 /* Invoke the regular ELF backend linker to do all the work. */
11140 return bfd_elf_final_link (abfd, info);
11144 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
11146 struct elf_reloc_cookie *rcookie = cookie;
11148 if (rcookie->bad_symtab)
11149 rcookie->rel = rcookie->rels;
11151 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
11153 unsigned long r_symndx;
11155 if (! rcookie->bad_symtab)
11156 if (rcookie->rel->r_offset > offset)
11158 if (rcookie->rel->r_offset != offset)
11161 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
11162 if (r_symndx == SHN_UNDEF)
11165 if (r_symndx >= rcookie->locsymcount
11166 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11168 struct elf_link_hash_entry *h;
11170 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
11172 while (h->root.type == bfd_link_hash_indirect
11173 || h->root.type == bfd_link_hash_warning)
11174 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11176 if ((h->root.type == bfd_link_hash_defined
11177 || h->root.type == bfd_link_hash_defweak)
11178 && elf_discarded_section (h->root.u.def.section))
11185 /* It's not a relocation against a global symbol,
11186 but it could be a relocation against a local
11187 symbol for a discarded section. */
11189 Elf_Internal_Sym *isym;
11191 /* Need to: get the symbol; get the section. */
11192 isym = &rcookie->locsyms[r_symndx];
11193 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
11195 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
11196 if (isec != NULL && elf_discarded_section (isec))
11205 /* Discard unneeded references to discarded sections.
11206 Returns TRUE if any section's size was changed. */
11207 /* This function assumes that the relocations are in sorted order,
11208 which is true for all known assemblers. */
11211 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
11213 struct elf_reloc_cookie cookie;
11214 asection *stab, *eh;
11215 Elf_Internal_Shdr *symtab_hdr;
11216 const struct elf_backend_data *bed;
11218 unsigned int count;
11219 bfd_boolean ret = FALSE;
11221 if (info->traditional_format
11222 || !is_elf_hash_table (info->hash))
11225 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
11227 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
11230 bed = get_elf_backend_data (abfd);
11232 if ((abfd->flags & DYNAMIC) != 0)
11236 if (!info->relocatable)
11238 eh = bfd_get_section_by_name (abfd, ".eh_frame");
11241 || bfd_is_abs_section (eh->output_section)))
11245 stab = bfd_get_section_by_name (abfd, ".stab");
11247 && (stab->size == 0
11248 || bfd_is_abs_section (stab->output_section)
11249 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
11254 && bed->elf_backend_discard_info == NULL)
11257 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11258 cookie.abfd = abfd;
11259 cookie.sym_hashes = elf_sym_hashes (abfd);
11260 cookie.bad_symtab = elf_bad_symtab (abfd);
11261 if (cookie.bad_symtab)
11263 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11264 cookie.extsymoff = 0;
11268 cookie.locsymcount = symtab_hdr->sh_info;
11269 cookie.extsymoff = symtab_hdr->sh_info;
11272 if (bed->s->arch_size == 32)
11273 cookie.r_sym_shift = 8;
11275 cookie.r_sym_shift = 32;
11277 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11278 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
11280 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11281 cookie.locsymcount, 0,
11283 if (cookie.locsyms == NULL)
11285 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11292 cookie.rels = NULL;
11293 count = stab->reloc_count;
11295 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
11296 info->keep_memory);
11297 if (cookie.rels != NULL)
11299 cookie.rel = cookie.rels;
11300 cookie.relend = cookie.rels;
11301 cookie.relend += count * bed->s->int_rels_per_ext_rel;
11302 if (_bfd_discard_section_stabs (abfd, stab,
11303 elf_section_data (stab)->sec_info,
11304 bfd_elf_reloc_symbol_deleted_p,
11307 if (elf_section_data (stab)->relocs != cookie.rels)
11308 free (cookie.rels);
11314 cookie.rels = NULL;
11315 count = eh->reloc_count;
11317 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
11318 info->keep_memory);
11319 cookie.rel = cookie.rels;
11320 cookie.relend = cookie.rels;
11321 if (cookie.rels != NULL)
11322 cookie.relend += count * bed->s->int_rels_per_ext_rel;
11324 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
11325 bfd_elf_reloc_symbol_deleted_p,
11329 if (cookie.rels != NULL
11330 && elf_section_data (eh)->relocs != cookie.rels)
11331 free (cookie.rels);
11334 if (bed->elf_backend_discard_info != NULL
11335 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
11338 if (cookie.locsyms != NULL
11339 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
11341 if (! info->keep_memory)
11342 free (cookie.locsyms);
11344 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
11348 if (info->eh_frame_hdr
11349 && !info->relocatable
11350 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
11357 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section *sec,
11358 struct bfd_link_info *info)
11361 const char *name, *p;
11362 struct bfd_section_already_linked *l;
11363 struct bfd_section_already_linked_hash_entry *already_linked_list;
11365 if (sec->output_section == bfd_abs_section_ptr)
11368 flags = sec->flags;
11370 /* Return if it isn't a linkonce section. A comdat group section
11371 also has SEC_LINK_ONCE set. */
11372 if ((flags & SEC_LINK_ONCE) == 0)
11375 /* Don't put group member sections on our list of already linked
11376 sections. They are handled as a group via their group section. */
11377 if (elf_sec_group (sec) != NULL)
11380 /* FIXME: When doing a relocatable link, we may have trouble
11381 copying relocations in other sections that refer to local symbols
11382 in the section being discarded. Those relocations will have to
11383 be converted somehow; as of this writing I'm not sure that any of
11384 the backends handle that correctly.
11386 It is tempting to instead not discard link once sections when
11387 doing a relocatable link (technically, they should be discarded
11388 whenever we are building constructors). However, that fails,
11389 because the linker winds up combining all the link once sections
11390 into a single large link once section, which defeats the purpose
11391 of having link once sections in the first place.
11393 Also, not merging link once sections in a relocatable link
11394 causes trouble for MIPS ELF, which relies on link once semantics
11395 to handle the .reginfo section correctly. */
11397 name = bfd_get_section_name (abfd, sec);
11399 if (CONST_STRNEQ (name, ".gnu.linkonce.")
11400 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
11405 already_linked_list = bfd_section_already_linked_table_lookup (p);
11407 for (l = already_linked_list->entry; l != NULL; l = l->next)
11409 /* We may have 2 different types of sections on the list: group
11410 sections and linkonce sections. Match like sections. */
11411 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
11412 && strcmp (name, l->sec->name) == 0
11413 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
11415 /* The section has already been linked. See if we should
11416 issue a warning. */
11417 switch (flags & SEC_LINK_DUPLICATES)
11422 case SEC_LINK_DUPLICATES_DISCARD:
11425 case SEC_LINK_DUPLICATES_ONE_ONLY:
11426 (*_bfd_error_handler)
11427 (_("%B: ignoring duplicate section `%A'"),
11431 case SEC_LINK_DUPLICATES_SAME_SIZE:
11432 if (sec->size != l->sec->size)
11433 (*_bfd_error_handler)
11434 (_("%B: duplicate section `%A' has different size"),
11438 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
11439 if (sec->size != l->sec->size)
11440 (*_bfd_error_handler)
11441 (_("%B: duplicate section `%A' has different size"),
11443 else if (sec->size != 0)
11445 bfd_byte *sec_contents, *l_sec_contents;
11447 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
11448 (*_bfd_error_handler)
11449 (_("%B: warning: could not read contents of section `%A'"),
11451 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
11453 (*_bfd_error_handler)
11454 (_("%B: warning: could not read contents of section `%A'"),
11455 l->sec->owner, l->sec);
11456 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
11457 (*_bfd_error_handler)
11458 (_("%B: warning: duplicate section `%A' has different contents"),
11462 free (sec_contents);
11463 if (l_sec_contents)
11464 free (l_sec_contents);
11469 /* Set the output_section field so that lang_add_section
11470 does not create a lang_input_section structure for this
11471 section. Since there might be a symbol in the section
11472 being discarded, we must retain a pointer to the section
11473 which we are really going to use. */
11474 sec->output_section = bfd_abs_section_ptr;
11475 sec->kept_section = l->sec;
11477 if (flags & SEC_GROUP)
11479 asection *first = elf_next_in_group (sec);
11480 asection *s = first;
11484 s->output_section = bfd_abs_section_ptr;
11485 /* Record which group discards it. */
11486 s->kept_section = l->sec;
11487 s = elf_next_in_group (s);
11488 /* These lists are circular. */
11498 /* A single member comdat group section may be discarded by a
11499 linkonce section and vice versa. */
11501 if ((flags & SEC_GROUP) != 0)
11503 asection *first = elf_next_in_group (sec);
11505 if (first != NULL && elf_next_in_group (first) == first)
11506 /* Check this single member group against linkonce sections. */
11507 for (l = already_linked_list->entry; l != NULL; l = l->next)
11508 if ((l->sec->flags & SEC_GROUP) == 0
11509 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
11510 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
11512 first->output_section = bfd_abs_section_ptr;
11513 first->kept_section = l->sec;
11514 sec->output_section = bfd_abs_section_ptr;
11519 /* Check this linkonce section against single member groups. */
11520 for (l = already_linked_list->entry; l != NULL; l = l->next)
11521 if (l->sec->flags & SEC_GROUP)
11523 asection *first = elf_next_in_group (l->sec);
11526 && elf_next_in_group (first) == first
11527 && bfd_elf_match_symbols_in_sections (first, sec, info))
11529 sec->output_section = bfd_abs_section_ptr;
11530 sec->kept_section = first;
11535 /* This is the first section with this name. Record it. */
11536 bfd_section_already_linked_table_insert (already_linked_list, sec);
11540 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
11542 return sym->st_shndx == SHN_COMMON;
11546 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
11552 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
11554 return bfd_com_section_ptr;