1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
33 struct elf_link_hash_entry *h;
34 struct bfd_link_hash_entry *bh;
35 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
38 /* This function may be called more than once. */
39 s = bfd_get_section_by_name (abfd, ".got");
40 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
43 switch (bed->s->arch_size)
54 bfd_set_error (bfd_error_bad_value);
58 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
59 | SEC_LINKER_CREATED);
61 s = bfd_make_section (abfd, ".got");
63 || !bfd_set_section_flags (abfd, s, flags)
64 || !bfd_set_section_alignment (abfd, s, ptralign))
67 if (bed->want_got_plt)
69 s = bfd_make_section (abfd, ".got.plt");
71 || !bfd_set_section_flags (abfd, s, flags)
72 || !bfd_set_section_alignment (abfd, s, ptralign))
76 if (bed->want_got_sym)
78 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
79 (or .got.plt) section. We don't do this in the linker script
80 because we don't want to define the symbol if we are not creating
81 a global offset table. */
83 if (!(_bfd_generic_link_add_one_symbol
84 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
85 bed->got_symbol_offset, NULL, FALSE, bed->collect, &bh)))
87 h = (struct elf_link_hash_entry *) bh;
88 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
91 if (! info->executable
92 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
95 elf_hash_table (info)->hgot = h;
98 /* The first bit of the global offset table is the header. */
99 s->_raw_size += bed->got_header_size + bed->got_symbol_offset;
104 /* Create some sections which will be filled in with dynamic linking
105 information. ABFD is an input file which requires dynamic sections
106 to be created. The dynamic sections take up virtual memory space
107 when the final executable is run, so we need to create them before
108 addresses are assigned to the output sections. We work out the
109 actual contents and size of these sections later. */
112 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
115 register asection *s;
116 struct elf_link_hash_entry *h;
117 struct bfd_link_hash_entry *bh;
118 const struct elf_backend_data *bed;
120 if (! is_elf_hash_table (info->hash))
123 if (elf_hash_table (info)->dynamic_sections_created)
126 /* Make sure that all dynamic sections use the same input BFD. */
127 if (elf_hash_table (info)->dynobj == NULL)
128 elf_hash_table (info)->dynobj = abfd;
130 abfd = elf_hash_table (info)->dynobj;
132 /* Note that we set the SEC_IN_MEMORY flag for all of these
134 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
135 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
137 /* A dynamically linked executable has a .interp section, but a
138 shared library does not. */
139 if (info->executable)
141 s = bfd_make_section (abfd, ".interp");
143 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
147 if (! info->traditional_format)
149 s = bfd_make_section (abfd, ".eh_frame_hdr");
151 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
152 || ! bfd_set_section_alignment (abfd, s, 2))
154 elf_hash_table (info)->eh_info.hdr_sec = s;
157 bed = get_elf_backend_data (abfd);
159 /* Create sections to hold version informations. These are removed
160 if they are not needed. */
161 s = bfd_make_section (abfd, ".gnu.version_d");
163 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
164 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 s = bfd_make_section (abfd, ".gnu.version");
169 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
170 || ! bfd_set_section_alignment (abfd, s, 1))
173 s = bfd_make_section (abfd, ".gnu.version_r");
175 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
176 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
179 s = bfd_make_section (abfd, ".dynsym");
181 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
182 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
185 s = bfd_make_section (abfd, ".dynstr");
187 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
190 /* Create a strtab to hold the dynamic symbol names. */
191 if (elf_hash_table (info)->dynstr == NULL)
193 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
194 if (elf_hash_table (info)->dynstr == NULL)
198 s = bfd_make_section (abfd, ".dynamic");
200 || ! bfd_set_section_flags (abfd, s, flags)
201 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
204 /* The special symbol _DYNAMIC is always set to the start of the
205 .dynamic section. This call occurs before we have processed the
206 symbols for any dynamic object, so we don't have to worry about
207 overriding a dynamic definition. We could set _DYNAMIC in a
208 linker script, but we only want to define it if we are, in fact,
209 creating a .dynamic section. We don't want to define it if there
210 is no .dynamic section, since on some ELF platforms the start up
211 code examines it to decide how to initialize the process. */
213 if (! (_bfd_generic_link_add_one_symbol
214 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE,
215 get_elf_backend_data (abfd)->collect, &bh)))
217 h = (struct elf_link_hash_entry *) bh;
218 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
219 h->type = STT_OBJECT;
221 if (! info->executable
222 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
225 s = bfd_make_section (abfd, ".hash");
227 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
228 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
230 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
232 /* Let the backend create the rest of the sections. This lets the
233 backend set the right flags. The backend will normally create
234 the .got and .plt sections. */
235 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
238 elf_hash_table (info)->dynamic_sections_created = TRUE;
243 /* Create dynamic sections when linking against a dynamic object. */
246 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
248 flagword flags, pltflags;
250 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
253 .rel[a].bss sections. */
255 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
256 | SEC_LINKER_CREATED);
259 pltflags |= SEC_CODE;
260 if (bed->plt_not_loaded)
261 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
262 if (bed->plt_readonly)
263 pltflags |= SEC_READONLY;
265 s = bfd_make_section (abfd, ".plt");
267 || ! bfd_set_section_flags (abfd, s, pltflags)
268 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
271 if (bed->want_plt_sym)
273 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
275 struct elf_link_hash_entry *h;
276 struct bfd_link_hash_entry *bh = NULL;
278 if (! (_bfd_generic_link_add_one_symbol
279 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
280 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
282 h = (struct elf_link_hash_entry *) bh;
283 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
284 h->type = STT_OBJECT;
286 if (! info->executable
287 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
291 s = bfd_make_section (abfd,
292 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
294 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
295 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
298 if (! _bfd_elf_create_got_section (abfd, info))
301 if (bed->want_dynbss)
303 /* The .dynbss section is a place to put symbols which are defined
304 by dynamic objects, are referenced by regular objects, and are
305 not functions. We must allocate space for them in the process
306 image and use a R_*_COPY reloc to tell the dynamic linker to
307 initialize them at run time. The linker script puts the .dynbss
308 section into the .bss section of the final image. */
309 s = bfd_make_section (abfd, ".dynbss");
311 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
314 /* The .rel[a].bss section holds copy relocs. This section is not
315 normally needed. We need to create it here, though, so that the
316 linker will map it to an output section. We can't just create it
317 only if we need it, because we will not know whether we need it
318 until we have seen all the input files, and the first time the
319 main linker code calls BFD after examining all the input files
320 (size_dynamic_sections) the input sections have already been
321 mapped to the output sections. If the section turns out not to
322 be needed, we can discard it later. We will never need this
323 section when generating a shared object, since they do not use
327 s = bfd_make_section (abfd,
328 (bed->default_use_rela_p
329 ? ".rela.bss" : ".rel.bss"));
331 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
332 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
340 /* Record a new dynamic symbol. We record the dynamic symbols as we
341 read the input files, since we need to have a list of all of them
342 before we can determine the final sizes of the output sections.
343 Note that we may actually call this function even though we are not
344 going to output any dynamic symbols; in some cases we know that a
345 symbol should be in the dynamic symbol table, but only if there is
349 _bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
350 struct elf_link_hash_entry *h)
352 if (h->dynindx == -1)
354 struct elf_strtab_hash *dynstr;
359 /* XXX: The ABI draft says the linker must turn hidden and
360 internal symbols into STB_LOCAL symbols when producing the
361 DSO. However, if ld.so honors st_other in the dynamic table,
362 this would not be necessary. */
363 switch (ELF_ST_VISIBILITY (h->other))
367 if (h->root.type != bfd_link_hash_undefined
368 && h->root.type != bfd_link_hash_undefweak)
370 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
378 h->dynindx = elf_hash_table (info)->dynsymcount;
379 ++elf_hash_table (info)->dynsymcount;
381 dynstr = elf_hash_table (info)->dynstr;
384 /* Create a strtab to hold the dynamic symbol names. */
385 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
390 /* We don't put any version information in the dynamic string
392 name = h->root.root.string;
393 p = strchr (name, ELF_VER_CHR);
395 /* We know that the p points into writable memory. In fact,
396 there are only a few symbols that have read-only names, being
397 those like _GLOBAL_OFFSET_TABLE_ that are created specially
398 by the backends. Most symbols will have names pointing into
399 an ELF string table read from a file, or to objalloc memory. */
402 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
407 if (indx == (bfd_size_type) -1)
409 h->dynstr_index = indx;
415 /* Record an assignment to a symbol made by a linker script. We need
416 this in case some dynamic object refers to this symbol. */
419 bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED,
420 struct bfd_link_info *info,
424 struct elf_link_hash_entry *h;
426 if (!is_elf_hash_table (info->hash))
429 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE);
433 /* Since we're defining the symbol, don't let it seem to have not
434 been defined. record_dynamic_symbol and size_dynamic_sections
435 may depend on this. */
436 if (h->root.type == bfd_link_hash_undefweak
437 || h->root.type == bfd_link_hash_undefined)
438 h->root.type = bfd_link_hash_new;
440 if (h->root.type == bfd_link_hash_new)
441 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
443 /* If this symbol is being provided by the linker script, and it is
444 currently defined by a dynamic object, but not by a regular
445 object, then mark it as undefined so that the generic linker will
446 force the correct value. */
448 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
449 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
450 h->root.type = bfd_link_hash_undefined;
452 /* If this symbol is not being provided by the linker script, and it is
453 currently defined by a dynamic object, but not by a regular object,
454 then clear out any version information because the symbol will not be
455 associated with the dynamic object any more. */
457 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
458 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
459 h->verinfo.verdef = NULL;
461 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
463 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
464 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
468 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
471 /* If this is a weak defined symbol, and we know a corresponding
472 real symbol from the same dynamic object, make sure the real
473 symbol is also made into a dynamic symbol. */
474 if (h->weakdef != NULL
475 && h->weakdef->dynindx == -1)
477 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
485 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
486 success, and 2 on a failure caused by attempting to record a symbol
487 in a discarded section, eg. a discarded link-once section symbol. */
490 elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
495 struct elf_link_local_dynamic_entry *entry;
496 struct elf_link_hash_table *eht;
497 struct elf_strtab_hash *dynstr;
498 unsigned long dynstr_index;
500 Elf_External_Sym_Shndx eshndx;
501 char esym[sizeof (Elf64_External_Sym)];
503 if (! is_elf_hash_table (info->hash))
506 /* See if the entry exists already. */
507 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
508 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
511 amt = sizeof (*entry);
512 entry = bfd_alloc (input_bfd, amt);
516 /* Go find the symbol, so that we can find it's name. */
517 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
518 1, input_indx, &entry->isym, esym, &eshndx))
520 bfd_release (input_bfd, entry);
524 if (entry->isym.st_shndx != SHN_UNDEF
525 && (entry->isym.st_shndx < SHN_LORESERVE
526 || entry->isym.st_shndx > SHN_HIRESERVE))
530 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
531 if (s == NULL || bfd_is_abs_section (s->output_section))
533 /* We can still bfd_release here as nothing has done another
534 bfd_alloc. We can't do this later in this function. */
535 bfd_release (input_bfd, entry);
540 name = (bfd_elf_string_from_elf_section
541 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
542 entry->isym.st_name));
544 dynstr = elf_hash_table (info)->dynstr;
547 /* Create a strtab to hold the dynamic symbol names. */
548 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
553 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
554 if (dynstr_index == (unsigned long) -1)
556 entry->isym.st_name = dynstr_index;
558 eht = elf_hash_table (info);
560 entry->next = eht->dynlocal;
561 eht->dynlocal = entry;
562 entry->input_bfd = input_bfd;
563 entry->input_indx = input_indx;
566 /* Whatever binding the symbol had before, it's now local. */
568 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
570 /* The dynindx will be set at the end of size_dynamic_sections. */
575 /* Return the dynindex of a local dynamic symbol. */
578 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
582 struct elf_link_local_dynamic_entry *e;
584 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
585 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
590 /* This function is used to renumber the dynamic symbols, if some of
591 them are removed because they are marked as local. This is called
592 via elf_link_hash_traverse. */
595 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
598 size_t *count = data;
600 if (h->root.type == bfd_link_hash_warning)
601 h = (struct elf_link_hash_entry *) h->root.u.i.link;
603 if (h->dynindx != -1)
604 h->dynindx = ++(*count);
609 /* Assign dynsym indices. In a shared library we generate a section
610 symbol for each output section, which come first. Next come all of
611 the back-end allocated local dynamic syms, followed by the rest of
612 the global symbols. */
615 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
617 unsigned long dynsymcount = 0;
622 for (p = output_bfd->sections; p ; p = p->next)
623 if ((p->flags & SEC_EXCLUDE) == 0)
624 elf_section_data (p)->dynindx = ++dynsymcount;
627 if (elf_hash_table (info)->dynlocal)
629 struct elf_link_local_dynamic_entry *p;
630 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
631 p->dynindx = ++dynsymcount;
634 elf_link_hash_traverse (elf_hash_table (info),
635 elf_link_renumber_hash_table_dynsyms,
638 /* There is an unused NULL entry at the head of the table which
639 we must account for in our count. Unless there weren't any
640 symbols, which means we'll have no table at all. */
641 if (dynsymcount != 0)
644 return elf_hash_table (info)->dynsymcount = dynsymcount;
647 /* This function is called when we want to define a new symbol. It
648 handles the various cases which arise when we find a definition in
649 a dynamic object, or when there is already a definition in a
650 dynamic object. The new symbol is described by NAME, SYM, PSEC,
651 and PVALUE. We set SYM_HASH to the hash table entry. We set
652 OVERRIDE if the old symbol is overriding a new definition. We set
653 TYPE_CHANGE_OK if it is OK for the type to change. We set
654 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
655 change, we mean that we shouldn't warn if the type or size does
656 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
660 _bfd_elf_merge_symbol (bfd *abfd,
661 struct bfd_link_info *info,
663 Elf_Internal_Sym *sym,
666 struct elf_link_hash_entry **sym_hash,
668 bfd_boolean *override,
669 bfd_boolean *type_change_ok,
670 bfd_boolean *size_change_ok,
671 bfd_boolean dt_needed)
674 struct elf_link_hash_entry *h;
675 struct elf_link_hash_entry *flip;
678 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
679 bfd_boolean newweakdef, oldweakdef, newweakundef, oldweakundef;
685 bind = ELF_ST_BIND (sym->st_info);
687 if (! bfd_is_und_section (sec))
688 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
690 h = ((struct elf_link_hash_entry *)
691 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
696 /* This code is for coping with dynamic objects, and is only useful
697 if we are doing an ELF link. */
698 if (info->hash->creator != abfd->xvec)
701 /* For merging, we only care about real symbols. */
703 while (h->root.type == bfd_link_hash_indirect
704 || h->root.type == bfd_link_hash_warning)
705 h = (struct elf_link_hash_entry *) h->root.u.i.link;
707 /* If we just created the symbol, mark it as being an ELF symbol.
708 Other than that, there is nothing to do--there is no merge issue
709 with a newly defined symbol--so we just return. */
711 if (h->root.type == bfd_link_hash_new)
713 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
717 /* OLDBFD is a BFD associated with the existing symbol. */
719 switch (h->root.type)
725 case bfd_link_hash_undefined:
726 case bfd_link_hash_undefweak:
727 oldbfd = h->root.u.undef.abfd;
730 case bfd_link_hash_defined:
731 case bfd_link_hash_defweak:
732 oldbfd = h->root.u.def.section->owner;
735 case bfd_link_hash_common:
736 oldbfd = h->root.u.c.p->section->owner;
740 /* In cases involving weak versioned symbols, we may wind up trying
741 to merge a symbol with itself. Catch that here, to avoid the
742 confusion that results if we try to override a symbol with
743 itself. The additional tests catch cases like
744 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
745 dynamic object, which we do want to handle here. */
747 && ((abfd->flags & DYNAMIC) == 0
748 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
751 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
752 respectively, is from a dynamic object. */
754 if ((abfd->flags & DYNAMIC) != 0)
760 olddyn = (oldbfd->flags & DYNAMIC) != 0;
765 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
766 indices used by MIPS ELF. */
767 switch (h->root.type)
773 case bfd_link_hash_defined:
774 case bfd_link_hash_defweak:
775 hsec = h->root.u.def.section;
778 case bfd_link_hash_common:
779 hsec = h->root.u.c.p->section;
786 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
789 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
790 respectively, appear to be a definition rather than reference. */
792 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
797 if (h->root.type == bfd_link_hash_undefined
798 || h->root.type == bfd_link_hash_undefweak
799 || h->root.type == bfd_link_hash_common)
804 /* We need to remember if a symbol has a definition in a dynamic
805 object or is weak in all dynamic objects. Internal and hidden
806 visibility will make it unavailable to dynamic objects. */
807 if (newdyn && (h->elf_link_hash_flags & ELF_LINK_DYNAMIC_DEF) == 0)
809 if (!bfd_is_und_section (sec))
810 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_DEF;
813 /* Check if this symbol is weak in all dynamic objects. If it
814 is the first time we see it in a dynamic object, we mark
815 if it is weak. Otherwise, we clear it. */
816 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
818 if (bind == STB_WEAK)
819 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_WEAK;
821 else if (bind != STB_WEAK)
822 h->elf_link_hash_flags &= ~ELF_LINK_DYNAMIC_WEAK;
826 /* If the old symbol has non-default visibility, we ignore the new
827 definition from a dynamic object. */
829 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
830 && !bfd_is_und_section (sec))
833 /* Make sure this symbol is dynamic. */
834 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
835 /* A protected symbol has external availability. Make sure it is
838 FIXME: Should we check type and size for protected symbol? */
839 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
840 return _bfd_elf_link_record_dynamic_symbol (info, h);
845 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
846 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
848 /* If the new symbol with non-default visibility comes from a
849 relocatable file and the old definition comes from a dynamic
850 object, we remove the old definition. */
851 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
854 if ((h->root.und_next || info->hash->undefs_tail == &h->root)
855 && bfd_is_und_section (sec))
857 /* If the new symbol is undefined and the old symbol was
858 also undefined before, we need to make sure
859 _bfd_generic_link_add_one_symbol doesn't mess
860 up the linker hash table undefs list. Since the old
861 definition came from a dynamic object, it is still on the
863 h->root.type = bfd_link_hash_undefined;
864 /* FIXME: What if the new symbol is weak undefined? */
865 h->root.u.undef.abfd = abfd;
869 h->root.type = bfd_link_hash_new;
870 h->root.u.undef.abfd = NULL;
873 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
875 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
876 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_DYNAMIC
877 | ELF_LINK_DYNAMIC_DEF);
879 /* FIXME: Should we check type and size for protected symbol? */
885 /* We need to treat weak definition right, depending on if there is a
886 definition from a dynamic object. */
887 if (bind == STB_WEAK)
892 newweakundef = FALSE;
901 newweakdef = newweakundef = FALSE;
903 /* If the new weak definition comes from a relocatable file and the
904 old symbol comes from a dynamic object, we treat the new one as
906 if (newweakdef && !newdyn && olddyn)
909 if (h->root.type == bfd_link_hash_defweak)
912 oldweakundef = FALSE;
914 else if (h->root.type == bfd_link_hash_undefweak)
920 oldweakdef = oldweakundef = FALSE;
922 /* If the old weak definition comes from a relocatable file and the
923 new symbol comes from a dynamic object, we treat the old one as
925 if (oldweakdef && !olddyn && newdyn)
928 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
929 symbol, respectively, appears to be a common symbol in a dynamic
930 object. If a symbol appears in an uninitialized section, and is
931 not weak, and is not a function, then it may be a common symbol
932 which was resolved when the dynamic object was created. We want
933 to treat such symbols specially, because they raise special
934 considerations when setting the symbol size: if the symbol
935 appears as a common symbol in a regular object, and the size in
936 the regular object is larger, we must make sure that we use the
937 larger size. This problematic case can always be avoided in C,
938 but it must be handled correctly when using Fortran shared
941 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
942 likewise for OLDDYNCOMMON and OLDDEF.
944 Note that this test is just a heuristic, and that it is quite
945 possible to have an uninitialized symbol in a shared object which
946 is really a definition, rather than a common symbol. This could
947 lead to some minor confusion when the symbol really is a common
948 symbol in some regular object. However, I think it will be
953 && (sec->flags & SEC_ALLOC) != 0
954 && (sec->flags & SEC_LOAD) == 0
958 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
961 newdyncommon = FALSE;
965 && h->root.type == bfd_link_hash_defined
966 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
967 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
968 && (h->root.u.def.section->flags & SEC_LOAD) == 0
970 && h->type != STT_FUNC)
973 olddyncommon = FALSE;
975 /* It's OK to change the type if either the existing symbol or the
976 new symbol is weak unless it comes from a DT_NEEDED entry of
977 a shared object, in which case, the DT_NEEDED entry may not be
978 required at the run time. The type change is also OK if the
979 old symbol is undefined and the new symbol is defined. */
981 if ((! dt_needed && oldweakdef)
986 && (h->root.type == bfd_link_hash_undefined
987 || h->root.type == bfd_link_hash_undefweak)))
988 *type_change_ok = TRUE;
990 /* It's OK to change the size if either the existing symbol or the
991 new symbol is weak, or if the old symbol is undefined. */
994 || h->root.type == bfd_link_hash_undefined)
995 *size_change_ok = TRUE;
997 /* If both the old and the new symbols look like common symbols in a
998 dynamic object, set the size of the symbol to the larger of the
1003 && sym->st_size != h->size)
1005 /* Since we think we have two common symbols, issue a multiple
1006 common warning if desired. Note that we only warn if the
1007 size is different. If the size is the same, we simply let
1008 the old symbol override the new one as normally happens with
1009 symbols defined in dynamic objects. */
1011 if (! ((*info->callbacks->multiple_common)
1012 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1013 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1016 if (sym->st_size > h->size)
1017 h->size = sym->st_size;
1019 *size_change_ok = TRUE;
1022 /* If we are looking at a dynamic object, and we have found a
1023 definition, we need to see if the symbol was already defined by
1024 some other object. If so, we want to use the existing
1025 definition, and we do not want to report a multiple symbol
1026 definition error; we do this by clobbering *PSEC to be
1027 bfd_und_section_ptr.
1029 We treat a common symbol as a definition if the symbol in the
1030 shared library is a function, since common symbols always
1031 represent variables; this can cause confusion in principle, but
1032 any such confusion would seem to indicate an erroneous program or
1033 shared library. We also permit a common symbol in a regular
1034 object to override a weak symbol in a shared object.
1036 We prefer a non-weak definition in a shared library to a weak
1037 definition in the executable unless it comes from a DT_NEEDED
1038 entry of a shared object, in which case, the DT_NEEDED entry
1039 may not be required at the run time. */
1044 || (h->root.type == bfd_link_hash_common
1047 || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
1055 newdyncommon = FALSE;
1057 *psec = sec = bfd_und_section_ptr;
1058 *size_change_ok = TRUE;
1060 /* If we get here when the old symbol is a common symbol, then
1061 we are explicitly letting it override a weak symbol or
1062 function in a dynamic object, and we don't want to warn about
1063 a type change. If the old symbol is a defined symbol, a type
1064 change warning may still be appropriate. */
1066 if (h->root.type == bfd_link_hash_common)
1067 *type_change_ok = TRUE;
1070 /* Handle the special case of an old common symbol merging with a
1071 new symbol which looks like a common symbol in a shared object.
1072 We change *PSEC and *PVALUE to make the new symbol look like a
1073 common symbol, and let _bfd_generic_link_add_one_symbol will do
1077 && h->root.type == bfd_link_hash_common)
1081 newdyncommon = FALSE;
1082 *pvalue = sym->st_size;
1083 *psec = sec = bfd_com_section_ptr;
1084 *size_change_ok = TRUE;
1087 /* If the old symbol is from a dynamic object, and the new symbol is
1088 a definition which is not from a dynamic object, then the new
1089 symbol overrides the old symbol. Symbols from regular files
1090 always take precedence over symbols from dynamic objects, even if
1091 they are defined after the dynamic object in the link.
1093 As above, we again permit a common symbol in a regular object to
1094 override a definition in a shared object if the shared object
1095 symbol is a function or is weak.
1097 As above, we permit a non-weak definition in a shared object to
1098 override a weak definition in a regular object. */
1103 || (bfd_is_com_section (sec)
1104 && (oldweakdef || h->type == STT_FUNC)))
1107 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1108 && ((!newweakdef && !newweakundef) || oldweakdef))
1110 /* Change the hash table entry to undefined, and let
1111 _bfd_generic_link_add_one_symbol do the right thing with the
1114 h->root.type = bfd_link_hash_undefined;
1115 h->root.u.undef.abfd = h->root.u.def.section->owner;
1116 *size_change_ok = TRUE;
1119 olddyncommon = FALSE;
1121 /* We again permit a type change when a common symbol may be
1122 overriding a function. */
1124 if (bfd_is_com_section (sec))
1125 *type_change_ok = TRUE;
1127 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1130 /* This union may have been set to be non-NULL when this symbol
1131 was seen in a dynamic object. We must force the union to be
1132 NULL, so that it is correct for a regular symbol. */
1133 h->verinfo.vertree = NULL;
1136 /* Handle the special case of a new common symbol merging with an
1137 old symbol that looks like it might be a common symbol defined in
1138 a shared object. Note that we have already handled the case in
1139 which a new common symbol should simply override the definition
1140 in the shared library. */
1143 && bfd_is_com_section (sec)
1146 /* It would be best if we could set the hash table entry to a
1147 common symbol, but we don't know what to use for the section
1148 or the alignment. */
1149 if (! ((*info->callbacks->multiple_common)
1150 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1151 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1154 /* If the presumed common symbol in the dynamic object is
1155 larger, pretend that the new symbol has its size. */
1157 if (h->size > *pvalue)
1160 /* FIXME: We no longer know the alignment required by the symbol
1161 in the dynamic object, so we just wind up using the one from
1162 the regular object. */
1165 olddyncommon = FALSE;
1167 h->root.type = bfd_link_hash_undefined;
1168 h->root.u.undef.abfd = h->root.u.def.section->owner;
1170 *size_change_ok = TRUE;
1171 *type_change_ok = TRUE;
1173 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1176 h->verinfo.vertree = NULL;
1181 /* Handle the case where we had a versioned symbol in a dynamic
1182 library and now find a definition in a normal object. In this
1183 case, we make the versioned symbol point to the normal one. */
1184 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1185 flip->root.type = h->root.type;
1186 h->root.type = bfd_link_hash_indirect;
1187 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1188 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1189 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1190 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1192 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
1193 flip->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1197 /* Handle the special case of a weak definition in a regular object
1198 followed by a non-weak definition in a shared object. In this
1199 case, we prefer the definition in the shared object unless it
1200 comes from a DT_NEEDED entry of a shared object, in which case,
1201 the DT_NEEDED entry may not be required at the run time. */
1210 /* To make this work we have to frob the flags so that the rest
1211 of the code does not think we are using the regular
1213 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
1214 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
1215 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
1216 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1217 h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
1218 | ELF_LINK_HASH_DEF_DYNAMIC);
1220 /* If H is the target of an indirection, we want the caller to
1221 use H rather than the indirect symbol. Otherwise if we are
1222 defining a new indirect symbol we will wind up attaching it
1223 to the entry we are overriding. */
1227 /* Handle the special case of a non-weak definition in a shared
1228 object followed by a weak definition in a regular object. In
1229 this case we prefer the definition in the shared object. To make
1230 this work we have to tell the caller to not treat the new symbol
1237 && (newweakdef || newweakundef))
1243 /* This function is called to create an indirect symbol from the
1244 default for the symbol with the default version if needed. The
1245 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1246 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
1247 indicates if it comes from a DT_NEEDED entry of a shared object. */
1250 _bfd_elf_add_default_symbol (bfd *abfd,
1251 struct bfd_link_info *info,
1252 struct elf_link_hash_entry *h,
1254 Elf_Internal_Sym *sym,
1257 bfd_boolean *dynsym,
1258 bfd_boolean override,
1259 bfd_boolean dt_needed)
1261 bfd_boolean type_change_ok;
1262 bfd_boolean size_change_ok;
1265 struct elf_link_hash_entry *hi;
1266 struct bfd_link_hash_entry *bh;
1267 const struct elf_backend_data *bed;
1268 bfd_boolean collect;
1269 bfd_boolean dynamic;
1271 size_t len, shortlen;
1274 /* If this symbol has a version, and it is the default version, we
1275 create an indirect symbol from the default name to the fully
1276 decorated name. This will cause external references which do not
1277 specify a version to be bound to this version of the symbol. */
1278 p = strchr (name, ELF_VER_CHR);
1279 if (p == NULL || p[1] != ELF_VER_CHR)
1284 /* We are overridden by an old definition. We need to check if we
1285 need to create the indirect symbol from the default name. */
1286 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1288 BFD_ASSERT (hi != NULL);
1291 while (hi->root.type == bfd_link_hash_indirect
1292 || hi->root.type == bfd_link_hash_warning)
1294 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1300 bed = get_elf_backend_data (abfd);
1301 collect = bed->collect;
1302 dynamic = (abfd->flags & DYNAMIC) != 0;
1304 shortlen = p - name;
1305 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1306 if (shortname == NULL)
1308 memcpy (shortname, name, shortlen);
1309 shortname[shortlen] = '\0';
1311 /* We are going to create a new symbol. Merge it with any existing
1312 symbol with this name. For the purposes of the merge, act as
1313 though we were defining the symbol we just defined, although we
1314 actually going to define an indirect symbol. */
1315 type_change_ok = FALSE;
1316 size_change_ok = FALSE;
1318 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1319 &hi, &skip, &override, &type_change_ok,
1320 &size_change_ok, dt_needed))
1329 if (! (_bfd_generic_link_add_one_symbol
1330 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1331 0, name, FALSE, collect, &bh)))
1333 hi = (struct elf_link_hash_entry *) bh;
1337 /* In this case the symbol named SHORTNAME is overriding the
1338 indirect symbol we want to add. We were planning on making
1339 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1340 is the name without a version. NAME is the fully versioned
1341 name, and it is the default version.
1343 Overriding means that we already saw a definition for the
1344 symbol SHORTNAME in a regular object, and it is overriding
1345 the symbol defined in the dynamic object.
1347 When this happens, we actually want to change NAME, the
1348 symbol we just added, to refer to SHORTNAME. This will cause
1349 references to NAME in the shared object to become references
1350 to SHORTNAME in the regular object. This is what we expect
1351 when we override a function in a shared object: that the
1352 references in the shared object will be mapped to the
1353 definition in the regular object. */
1355 while (hi->root.type == bfd_link_hash_indirect
1356 || hi->root.type == bfd_link_hash_warning)
1357 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1359 h->root.type = bfd_link_hash_indirect;
1360 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1361 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1363 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1364 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1365 if (hi->elf_link_hash_flags
1366 & (ELF_LINK_HASH_REF_REGULAR
1367 | ELF_LINK_HASH_DEF_REGULAR))
1369 if (! _bfd_elf_link_record_dynamic_symbol (info, hi))
1374 /* Now set HI to H, so that the following code will set the
1375 other fields correctly. */
1379 /* If there is a duplicate definition somewhere, then HI may not
1380 point to an indirect symbol. We will have reported an error to
1381 the user in that case. */
1383 if (hi->root.type == bfd_link_hash_indirect)
1385 struct elf_link_hash_entry *ht;
1387 /* If the symbol became indirect, then we assume that we have
1388 not seen a definition before. */
1389 BFD_ASSERT ((hi->elf_link_hash_flags
1390 & (ELF_LINK_HASH_DEF_DYNAMIC
1391 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1393 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1394 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1396 /* See if the new flags lead us to realize that the symbol must
1403 || ((hi->elf_link_hash_flags
1404 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1409 if ((hi->elf_link_hash_flags
1410 & ELF_LINK_HASH_REF_REGULAR) != 0)
1416 /* We also need to define an indirection from the nondefault version
1420 len = strlen (name);
1421 shortname = bfd_hash_allocate (&info->hash->table, len);
1422 if (shortname == NULL)
1424 memcpy (shortname, name, shortlen);
1425 memcpy (shortname + shortlen, p + 1, len - shortlen);
1427 /* Once again, merge with any existing symbol. */
1428 type_change_ok = FALSE;
1429 size_change_ok = FALSE;
1431 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1432 &hi, &skip, &override, &type_change_ok,
1433 &size_change_ok, dt_needed))
1441 /* Here SHORTNAME is a versioned name, so we don't expect to see
1442 the type of override we do in the case above unless it is
1443 overridden by a versioned definition. */
1444 if (hi->root.type != bfd_link_hash_defined
1445 && hi->root.type != bfd_link_hash_defweak)
1446 (*_bfd_error_handler)
1447 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1448 bfd_archive_filename (abfd), shortname);
1453 if (! (_bfd_generic_link_add_one_symbol
1454 (info, abfd, shortname, BSF_INDIRECT,
1455 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1457 hi = (struct elf_link_hash_entry *) bh;
1459 /* If there is a duplicate definition somewhere, then HI may not
1460 point to an indirect symbol. We will have reported an error
1461 to the user in that case. */
1463 if (hi->root.type == bfd_link_hash_indirect)
1465 /* If the symbol became indirect, then we assume that we have
1466 not seen a definition before. */
1467 BFD_ASSERT ((hi->elf_link_hash_flags
1468 & (ELF_LINK_HASH_DEF_DYNAMIC
1469 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1471 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1473 /* See if the new flags lead us to realize that the symbol
1480 || ((hi->elf_link_hash_flags
1481 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1486 if ((hi->elf_link_hash_flags
1487 & ELF_LINK_HASH_REF_REGULAR) != 0)
1497 /* This routine is used to export all defined symbols into the dynamic
1498 symbol table. It is called via elf_link_hash_traverse. */
1501 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1503 struct elf_info_failed *eif = data;
1505 /* Ignore indirect symbols. These are added by the versioning code. */
1506 if (h->root.type == bfd_link_hash_indirect)
1509 if (h->root.type == bfd_link_hash_warning)
1510 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1512 if (h->dynindx == -1
1513 && (h->elf_link_hash_flags
1514 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
1516 struct bfd_elf_version_tree *t;
1517 struct bfd_elf_version_expr *d;
1519 for (t = eif->verdefs; t != NULL; t = t->next)
1521 if (t->globals.list != NULL)
1523 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1528 if (t->locals.list != NULL)
1530 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1539 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
1550 /* Look through the symbols which are defined in other shared
1551 libraries and referenced here. Update the list of version
1552 dependencies. This will be put into the .gnu.version_r section.
1553 This function is called via elf_link_hash_traverse. */
1556 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1559 struct elf_find_verdep_info *rinfo = data;
1560 Elf_Internal_Verneed *t;
1561 Elf_Internal_Vernaux *a;
1564 if (h->root.type == bfd_link_hash_warning)
1565 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1567 /* We only care about symbols defined in shared objects with version
1569 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1570 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1572 || h->verinfo.verdef == NULL)
1575 /* See if we already know about this version. */
1576 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1578 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1581 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1582 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1588 /* This is a new version. Add it to tree we are building. */
1593 t = bfd_zalloc (rinfo->output_bfd, amt);
1596 rinfo->failed = TRUE;
1600 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1601 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1602 elf_tdata (rinfo->output_bfd)->verref = t;
1606 a = bfd_zalloc (rinfo->output_bfd, amt);
1608 /* Note that we are copying a string pointer here, and testing it
1609 above. If bfd_elf_string_from_elf_section is ever changed to
1610 discard the string data when low in memory, this will have to be
1612 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1614 a->vna_flags = h->verinfo.verdef->vd_flags;
1615 a->vna_nextptr = t->vn_auxptr;
1617 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1620 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1627 /* Figure out appropriate versions for all the symbols. We may not
1628 have the version number script until we have read all of the input
1629 files, so until that point we don't know which symbols should be
1630 local. This function is called via elf_link_hash_traverse. */
1633 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1635 struct elf_assign_sym_version_info *sinfo;
1636 struct bfd_link_info *info;
1637 const struct elf_backend_data *bed;
1638 struct elf_info_failed eif;
1645 if (h->root.type == bfd_link_hash_warning)
1646 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1648 /* Fix the symbol flags. */
1651 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1654 sinfo->failed = TRUE;
1658 /* We only need version numbers for symbols defined in regular
1660 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1663 bed = get_elf_backend_data (sinfo->output_bfd);
1664 p = strchr (h->root.root.string, ELF_VER_CHR);
1665 if (p != NULL && h->verinfo.vertree == NULL)
1667 struct bfd_elf_version_tree *t;
1672 /* There are two consecutive ELF_VER_CHR characters if this is
1673 not a hidden symbol. */
1675 if (*p == ELF_VER_CHR)
1681 /* If there is no version string, we can just return out. */
1685 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1689 /* Look for the version. If we find it, it is no longer weak. */
1690 for (t = sinfo->verdefs; t != NULL; t = t->next)
1692 if (strcmp (t->name, p) == 0)
1696 struct bfd_elf_version_expr *d;
1698 len = p - h->root.root.string;
1699 alc = bfd_malloc (len);
1702 memcpy (alc, h->root.root.string, len - 1);
1703 alc[len - 1] = '\0';
1704 if (alc[len - 2] == ELF_VER_CHR)
1705 alc[len - 2] = '\0';
1707 h->verinfo.vertree = t;
1711 if (t->globals.list != NULL)
1712 d = (*t->match) (&t->globals, NULL, alc);
1714 /* See if there is anything to force this symbol to
1716 if (d == NULL && t->locals.list != NULL)
1718 d = (*t->match) (&t->locals, NULL, alc);
1722 && ! info->export_dynamic)
1723 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1731 /* If we are building an application, we need to create a
1732 version node for this version. */
1733 if (t == NULL && info->executable)
1735 struct bfd_elf_version_tree **pp;
1738 /* If we aren't going to export this symbol, we don't need
1739 to worry about it. */
1740 if (h->dynindx == -1)
1744 t = bfd_zalloc (sinfo->output_bfd, amt);
1747 sinfo->failed = TRUE;
1752 t->name_indx = (unsigned int) -1;
1756 /* Don't count anonymous version tag. */
1757 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1759 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1761 t->vernum = version_index;
1765 h->verinfo.vertree = t;
1769 /* We could not find the version for a symbol when
1770 generating a shared archive. Return an error. */
1771 (*_bfd_error_handler)
1772 (_("%s: undefined versioned symbol name %s"),
1773 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
1774 bfd_set_error (bfd_error_bad_value);
1775 sinfo->failed = TRUE;
1780 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1783 /* If we don't have a version for this symbol, see if we can find
1785 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1787 struct bfd_elf_version_tree *t;
1788 struct bfd_elf_version_tree *local_ver;
1789 struct bfd_elf_version_expr *d;
1791 /* See if can find what version this symbol is in. If the
1792 symbol is supposed to be local, then don't actually register
1795 for (t = sinfo->verdefs; t != NULL; t = t->next)
1797 if (t->globals.list != NULL)
1799 bfd_boolean matched;
1803 while ((d = (*t->match) (&t->globals, d,
1804 h->root.root.string)) != NULL)
1809 /* There is a version without definition. Make
1810 the symbol the default definition for this
1812 h->verinfo.vertree = t;
1820 /* There is no undefined version for this symbol. Hide the
1822 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1825 if (t->locals.list != NULL)
1828 while ((d = (*t->match) (&t->locals, d,
1829 h->root.root.string)) != NULL)
1832 /* If the match is "*", keep looking for a more
1833 explicit, perhaps even global, match.
1834 XXX: Shouldn't this be !d->wildcard instead? */
1835 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1844 if (local_ver != NULL)
1846 h->verinfo.vertree = local_ver;
1847 if (h->dynindx != -1
1849 && ! info->export_dynamic)
1851 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1859 /* Read and swap the relocs from the section indicated by SHDR. This
1860 may be either a REL or a RELA section. The relocations are
1861 translated into RELA relocations and stored in INTERNAL_RELOCS,
1862 which should have already been allocated to contain enough space.
1863 The EXTERNAL_RELOCS are a buffer where the external form of the
1864 relocations should be stored.
1866 Returns FALSE if something goes wrong. */
1869 elf_link_read_relocs_from_section (bfd *abfd,
1871 Elf_Internal_Shdr *shdr,
1872 void *external_relocs,
1873 Elf_Internal_Rela *internal_relocs)
1875 const struct elf_backend_data *bed;
1876 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1877 const bfd_byte *erela;
1878 const bfd_byte *erelaend;
1879 Elf_Internal_Rela *irela;
1880 Elf_Internal_Shdr *symtab_hdr;
1883 /* Position ourselves at the start of the section. */
1884 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1887 /* Read the relocations. */
1888 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1891 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1892 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1894 bed = get_elf_backend_data (abfd);
1896 /* Convert the external relocations to the internal format. */
1897 if (shdr->sh_entsize == bed->s->sizeof_rel)
1898 swap_in = bed->s->swap_reloc_in;
1899 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1900 swap_in = bed->s->swap_reloca_in;
1903 bfd_set_error (bfd_error_wrong_format);
1907 erela = external_relocs;
1908 erelaend = erela + shdr->sh_size;
1909 irela = internal_relocs;
1910 while (erela < erelaend)
1914 (*swap_in) (abfd, erela, irela);
1915 r_symndx = ELF32_R_SYM (irela->r_info);
1916 if (bed->s->arch_size == 64)
1918 if ((size_t) r_symndx >= nsyms)
1920 (*_bfd_error_handler)
1921 (_("%s: bad reloc symbol index (0x%lx >= 0x%lx) for offset 0x%lx in section `%s'"),
1922 bfd_archive_filename (abfd), (unsigned long) r_symndx,
1923 (unsigned long) nsyms, irela->r_offset, sec->name);
1924 bfd_set_error (bfd_error_bad_value);
1927 irela += bed->s->int_rels_per_ext_rel;
1928 erela += shdr->sh_entsize;
1934 /* Read and swap the relocs for a section O. They may have been
1935 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1936 not NULL, they are used as buffers to read into. They are known to
1937 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1938 the return value is allocated using either malloc or bfd_alloc,
1939 according to the KEEP_MEMORY argument. If O has two relocation
1940 sections (both REL and RELA relocations), then the REL_HDR
1941 relocations will appear first in INTERNAL_RELOCS, followed by the
1942 REL_HDR2 relocations. */
1945 _bfd_elf_link_read_relocs (bfd *abfd,
1947 void *external_relocs,
1948 Elf_Internal_Rela *internal_relocs,
1949 bfd_boolean keep_memory)
1951 Elf_Internal_Shdr *rel_hdr;
1952 void *alloc1 = NULL;
1953 Elf_Internal_Rela *alloc2 = NULL;
1954 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1956 if (elf_section_data (o)->relocs != NULL)
1957 return elf_section_data (o)->relocs;
1959 if (o->reloc_count == 0)
1962 rel_hdr = &elf_section_data (o)->rel_hdr;
1964 if (internal_relocs == NULL)
1968 size = o->reloc_count;
1969 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
1971 internal_relocs = bfd_alloc (abfd, size);
1973 internal_relocs = alloc2 = bfd_malloc (size);
1974 if (internal_relocs == NULL)
1978 if (external_relocs == NULL)
1980 bfd_size_type size = rel_hdr->sh_size;
1982 if (elf_section_data (o)->rel_hdr2)
1983 size += elf_section_data (o)->rel_hdr2->sh_size;
1984 alloc1 = bfd_malloc (size);
1987 external_relocs = alloc1;
1990 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
1994 if (elf_section_data (o)->rel_hdr2
1995 && (!elf_link_read_relocs_from_section
1997 elf_section_data (o)->rel_hdr2,
1998 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
1999 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2000 * bed->s->int_rels_per_ext_rel))))
2003 /* Cache the results for next time, if we can. */
2005 elf_section_data (o)->relocs = internal_relocs;
2010 /* Don't free alloc2, since if it was allocated we are passing it
2011 back (under the name of internal_relocs). */
2013 return internal_relocs;
2023 /* Compute the size of, and allocate space for, REL_HDR which is the
2024 section header for a section containing relocations for O. */
2027 _bfd_elf_link_size_reloc_section (bfd *abfd,
2028 Elf_Internal_Shdr *rel_hdr,
2031 bfd_size_type reloc_count;
2032 bfd_size_type num_rel_hashes;
2034 /* Figure out how many relocations there will be. */
2035 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2036 reloc_count = elf_section_data (o)->rel_count;
2038 reloc_count = elf_section_data (o)->rel_count2;
2040 num_rel_hashes = o->reloc_count;
2041 if (num_rel_hashes < reloc_count)
2042 num_rel_hashes = reloc_count;
2044 /* That allows us to calculate the size of the section. */
2045 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2047 /* The contents field must last into write_object_contents, so we
2048 allocate it with bfd_alloc rather than malloc. Also since we
2049 cannot be sure that the contents will actually be filled in,
2050 we zero the allocated space. */
2051 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2052 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2055 /* We only allocate one set of hash entries, so we only do it the
2056 first time we are called. */
2057 if (elf_section_data (o)->rel_hashes == NULL
2060 struct elf_link_hash_entry **p;
2062 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2066 elf_section_data (o)->rel_hashes = p;
2072 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2073 originated from the section given by INPUT_REL_HDR) to the
2077 _bfd_elf_link_output_relocs (bfd *output_bfd,
2078 asection *input_section,
2079 Elf_Internal_Shdr *input_rel_hdr,
2080 Elf_Internal_Rela *internal_relocs)
2082 Elf_Internal_Rela *irela;
2083 Elf_Internal_Rela *irelaend;
2085 Elf_Internal_Shdr *output_rel_hdr;
2086 asection *output_section;
2087 unsigned int *rel_countp = NULL;
2088 const struct elf_backend_data *bed;
2089 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2091 output_section = input_section->output_section;
2092 output_rel_hdr = NULL;
2094 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2095 == input_rel_hdr->sh_entsize)
2097 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2098 rel_countp = &elf_section_data (output_section)->rel_count;
2100 else if (elf_section_data (output_section)->rel_hdr2
2101 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2102 == input_rel_hdr->sh_entsize))
2104 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2105 rel_countp = &elf_section_data (output_section)->rel_count2;
2109 (*_bfd_error_handler)
2110 (_("%s: relocation size mismatch in %s section %s"),
2111 bfd_get_filename (output_bfd),
2112 bfd_archive_filename (input_section->owner),
2113 input_section->name);
2114 bfd_set_error (bfd_error_wrong_object_format);
2118 bed = get_elf_backend_data (output_bfd);
2119 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2120 swap_out = bed->s->swap_reloc_out;
2121 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2122 swap_out = bed->s->swap_reloca_out;
2126 erel = output_rel_hdr->contents;
2127 erel += *rel_countp * input_rel_hdr->sh_entsize;
2128 irela = internal_relocs;
2129 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2130 * bed->s->int_rels_per_ext_rel);
2131 while (irela < irelaend)
2133 (*swap_out) (output_bfd, irela, erel);
2134 irela += bed->s->int_rels_per_ext_rel;
2135 erel += input_rel_hdr->sh_entsize;
2138 /* Bump the counter, so that we know where to add the next set of
2140 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2145 /* Fix up the flags for a symbol. This handles various cases which
2146 can only be fixed after all the input files are seen. This is
2147 currently called by both adjust_dynamic_symbol and
2148 assign_sym_version, which is unnecessary but perhaps more robust in
2149 the face of future changes. */
2152 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2153 struct elf_info_failed *eif)
2155 /* If this symbol was mentioned in a non-ELF file, try to set
2156 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2157 permit a non-ELF file to correctly refer to a symbol defined in
2158 an ELF dynamic object. */
2159 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
2161 while (h->root.type == bfd_link_hash_indirect)
2162 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2164 if (h->root.type != bfd_link_hash_defined
2165 && h->root.type != bfd_link_hash_defweak)
2166 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2167 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2170 if (h->root.u.def.section->owner != NULL
2171 && (bfd_get_flavour (h->root.u.def.section->owner)
2172 == bfd_target_elf_flavour))
2173 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2174 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2176 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2179 if (h->dynindx == -1
2180 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2181 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
2183 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
2192 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
2193 was first seen in a non-ELF file. Fortunately, if the symbol
2194 was first seen in an ELF file, we're probably OK unless the
2195 symbol was defined in a non-ELF file. Catch that case here.
2196 FIXME: We're still in trouble if the symbol was first seen in
2197 a dynamic object, and then later in a non-ELF regular object. */
2198 if ((h->root.type == bfd_link_hash_defined
2199 || h->root.type == bfd_link_hash_defweak)
2200 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2201 && (h->root.u.def.section->owner != NULL
2202 ? (bfd_get_flavour (h->root.u.def.section->owner)
2203 != bfd_target_elf_flavour)
2204 : (bfd_is_abs_section (h->root.u.def.section)
2205 && (h->elf_link_hash_flags
2206 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
2207 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2210 /* If this is a final link, and the symbol was defined as a common
2211 symbol in a regular object file, and there was no definition in
2212 any dynamic object, then the linker will have allocated space for
2213 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2214 flag will not have been set. */
2215 if (h->root.type == bfd_link_hash_defined
2216 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2217 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
2218 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2219 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2220 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2222 /* If -Bsymbolic was used (which means to bind references to global
2223 symbols to the definition within the shared object), and this
2224 symbol was defined in a regular object, then it actually doesn't
2225 need a PLT entry. Likewise, if the symbol has non-default
2226 visibility. If the symbol has hidden or internal visibility, we
2227 will force it local. */
2228 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
2229 && eif->info->shared
2230 && is_elf_hash_table (eif->info->hash)
2231 && (eif->info->symbolic
2232 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2233 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2235 const struct elf_backend_data *bed;
2236 bfd_boolean force_local;
2238 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2240 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2241 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2242 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2245 /* If a weak undefined symbol has non-default visibility, we also
2246 hide it from the dynamic linker. */
2247 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2248 && h->root.type == bfd_link_hash_undefweak)
2250 const struct elf_backend_data *bed;
2251 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2252 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2255 /* If this is a weak defined symbol in a dynamic object, and we know
2256 the real definition in the dynamic object, copy interesting flags
2257 over to the real definition. */
2258 if (h->weakdef != NULL)
2260 struct elf_link_hash_entry *weakdef;
2262 weakdef = h->weakdef;
2263 if (h->root.type == bfd_link_hash_indirect)
2264 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2266 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2267 || h->root.type == bfd_link_hash_defweak);
2268 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2269 || weakdef->root.type == bfd_link_hash_defweak);
2270 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
2272 /* If the real definition is defined by a regular object file,
2273 don't do anything special. See the longer description in
2274 _bfd_elf_adjust_dynamic_symbol, below. */
2275 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2279 const struct elf_backend_data *bed;
2281 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2282 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2289 /* Make the backend pick a good value for a dynamic symbol. This is
2290 called via elf_link_hash_traverse, and also calls itself
2294 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2296 struct elf_info_failed *eif = data;
2298 const struct elf_backend_data *bed;
2300 if (! is_elf_hash_table (eif->info->hash))
2303 if (h->root.type == bfd_link_hash_warning)
2305 h->plt = elf_hash_table (eif->info)->init_offset;
2306 h->got = elf_hash_table (eif->info)->init_offset;
2308 /* When warning symbols are created, they **replace** the "real"
2309 entry in the hash table, thus we never get to see the real
2310 symbol in a hash traversal. So look at it now. */
2311 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2314 /* Ignore indirect symbols. These are added by the versioning code. */
2315 if (h->root.type == bfd_link_hash_indirect)
2318 /* Fix the symbol flags. */
2319 if (! _bfd_elf_fix_symbol_flags (h, eif))
2322 /* If this symbol does not require a PLT entry, and it is not
2323 defined by a dynamic object, or is not referenced by a regular
2324 object, ignore it. We do have to handle a weak defined symbol,
2325 even if no regular object refers to it, if we decided to add it
2326 to the dynamic symbol table. FIXME: Do we normally need to worry
2327 about symbols which are defined by one dynamic object and
2328 referenced by another one? */
2329 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
2330 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2331 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2332 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
2333 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
2335 h->plt = elf_hash_table (eif->info)->init_offset;
2339 /* If we've already adjusted this symbol, don't do it again. This
2340 can happen via a recursive call. */
2341 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
2344 /* Don't look at this symbol again. Note that we must set this
2345 after checking the above conditions, because we may look at a
2346 symbol once, decide not to do anything, and then get called
2347 recursively later after REF_REGULAR is set below. */
2348 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
2350 /* If this is a weak definition, and we know a real definition, and
2351 the real symbol is not itself defined by a regular object file,
2352 then get a good value for the real definition. We handle the
2353 real symbol first, for the convenience of the backend routine.
2355 Note that there is a confusing case here. If the real definition
2356 is defined by a regular object file, we don't get the real symbol
2357 from the dynamic object, but we do get the weak symbol. If the
2358 processor backend uses a COPY reloc, then if some routine in the
2359 dynamic object changes the real symbol, we will not see that
2360 change in the corresponding weak symbol. This is the way other
2361 ELF linkers work as well, and seems to be a result of the shared
2364 I will clarify this issue. Most SVR4 shared libraries define the
2365 variable _timezone and define timezone as a weak synonym. The
2366 tzset call changes _timezone. If you write
2367 extern int timezone;
2369 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2370 you might expect that, since timezone is a synonym for _timezone,
2371 the same number will print both times. However, if the processor
2372 backend uses a COPY reloc, then actually timezone will be copied
2373 into your process image, and, since you define _timezone
2374 yourself, _timezone will not. Thus timezone and _timezone will
2375 wind up at different memory locations. The tzset call will set
2376 _timezone, leaving timezone unchanged. */
2378 if (h->weakdef != NULL)
2380 /* If we get to this point, we know there is an implicit
2381 reference by a regular object file via the weak symbol H.
2382 FIXME: Is this really true? What if the traversal finds
2383 H->WEAKDEF before it finds H? */
2384 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2386 if (! _bfd_elf_adjust_dynamic_symbol (h->weakdef, eif))
2390 /* If a symbol has no type and no size and does not require a PLT
2391 entry, then we are probably about to do the wrong thing here: we
2392 are probably going to create a COPY reloc for an empty object.
2393 This case can arise when a shared object is built with assembly
2394 code, and the assembly code fails to set the symbol type. */
2396 && h->type == STT_NOTYPE
2397 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
2398 (*_bfd_error_handler)
2399 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2400 h->root.root.string);
2402 dynobj = elf_hash_table (eif->info)->dynobj;
2403 bed = get_elf_backend_data (dynobj);
2404 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2413 /* Adjust all external symbols pointing into SEC_MERGE sections
2414 to reflect the object merging within the sections. */
2417 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2421 if (h->root.type == bfd_link_hash_warning)
2422 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2424 if ((h->root.type == bfd_link_hash_defined
2425 || h->root.type == bfd_link_hash_defweak)
2426 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2427 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2429 bfd *output_bfd = data;
2431 h->root.u.def.value =
2432 _bfd_merged_section_offset (output_bfd,
2433 &h->root.u.def.section,
2434 elf_section_data (sec)->sec_info,
2435 h->root.u.def.value, 0);
2441 /* Returns false if the symbol referred to by H should be considered
2442 to resolve local to the current module, and true if it should be
2443 considered to bind dynamically. */
2446 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2447 struct bfd_link_info *info,
2448 bfd_boolean ignore_protected)
2450 bfd_boolean binding_stays_local_p;
2455 while (h->root.type == bfd_link_hash_indirect
2456 || h->root.type == bfd_link_hash_warning)
2457 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2459 /* If it was forced local, then clearly it's not dynamic. */
2460 if (h->dynindx == -1)
2462 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2465 /* Identify the cases where name binding rules say that a
2466 visible symbol resolves locally. */
2467 binding_stays_local_p = info->executable || info->symbolic;
2469 switch (ELF_ST_VISIBILITY (h->other))
2476 /* Proper resolution for function pointer equality may require
2477 that these symbols perhaps be resolved dynamically, even though
2478 we should be resolving them to the current module. */
2479 if (!ignore_protected)
2480 binding_stays_local_p = TRUE;
2487 /* If it isn't defined locally, then clearly it's dynamic. */
2488 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2491 /* Otherwise, the symbol is dynamic if binding rules don't tell
2492 us that it remains local. */
2493 return !binding_stays_local_p;
2496 /* Return true if the symbol referred to by H should be considered
2497 to resolve local to the current module, and false otherwise. Differs
2498 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2499 undefined symbols and weak symbols. */
2502 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2503 struct bfd_link_info *info,
2504 bfd_boolean local_protected)
2506 /* If it's a local sym, of course we resolve locally. */
2510 /* If we don't have a definition in a regular file, then we can't
2511 resolve locally. The sym is either undefined or dynamic. */
2512 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2515 /* Forced local symbols resolve locally. */
2516 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
2519 /* As do non-dynamic symbols. */
2520 if (h->dynindx == -1)
2523 /* At this point, we know the symbol is defined and dynamic. In an
2524 executable it must resolve locally, likewise when building symbolic
2525 shared libraries. */
2526 if (info->executable || info->symbolic)
2529 /* Now deal with defined dynamic symbols in shared libraries. Ones
2530 with default visibility might not resolve locally. */
2531 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2534 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2535 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2538 /* Function pointer equality tests may require that STV_PROTECTED
2539 symbols be treated as dynamic symbols, even when we know that the
2540 dynamic linker will resolve them locally. */
2541 return local_protected;
2544 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2545 aligned. Returns the first TLS output section. */
2547 struct bfd_section *
2548 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2550 struct bfd_section *sec, *tls;
2551 unsigned int align = 0;
2553 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2554 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2558 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2559 if (sec->alignment_power > align)
2560 align = sec->alignment_power;
2562 elf_hash_table (info)->tls_sec = tls;
2564 /* Ensure the alignment of the first section is the largest alignment,
2565 so that the tls segment starts aligned. */
2567 tls->alignment_power = align;
2572 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2574 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2575 Elf_Internal_Sym *sym)
2577 /* Local symbols do not count, but target specific ones might. */
2578 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2579 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2582 /* Function symbols do not count. */
2583 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2586 /* If the section is undefined, then so is the symbol. */
2587 if (sym->st_shndx == SHN_UNDEF)
2590 /* If the symbol is defined in the common section, then
2591 it is a common definition and so does not count. */
2592 if (sym->st_shndx == SHN_COMMON)
2595 /* If the symbol is in a target specific section then we
2596 must rely upon the backend to tell us what it is. */
2597 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2598 /* FIXME - this function is not coded yet:
2600 return _bfd_is_global_symbol_definition (abfd, sym);
2602 Instead for now assume that the definition is not global,
2603 Even if this is wrong, at least the linker will behave
2604 in the same way that it used to do. */
2610 /* Search the symbol table of the archive element of the archive ABFD
2611 whose archive map contains a mention of SYMDEF, and determine if
2612 the symbol is defined in this element. */
2614 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2616 Elf_Internal_Shdr * hdr;
2617 bfd_size_type symcount;
2618 bfd_size_type extsymcount;
2619 bfd_size_type extsymoff;
2620 Elf_Internal_Sym *isymbuf;
2621 Elf_Internal_Sym *isym;
2622 Elf_Internal_Sym *isymend;
2625 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2629 if (! bfd_check_format (abfd, bfd_object))
2632 /* If we have already included the element containing this symbol in the
2633 link then we do not need to include it again. Just claim that any symbol
2634 it contains is not a definition, so that our caller will not decide to
2635 (re)include this element. */
2636 if (abfd->archive_pass)
2639 /* Select the appropriate symbol table. */
2640 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2641 hdr = &elf_tdata (abfd)->symtab_hdr;
2643 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2645 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2647 /* The sh_info field of the symtab header tells us where the
2648 external symbols start. We don't care about the local symbols. */
2649 if (elf_bad_symtab (abfd))
2651 extsymcount = symcount;
2656 extsymcount = symcount - hdr->sh_info;
2657 extsymoff = hdr->sh_info;
2660 if (extsymcount == 0)
2663 /* Read in the symbol table. */
2664 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2666 if (isymbuf == NULL)
2669 /* Scan the symbol table looking for SYMDEF. */
2671 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2675 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2680 if (strcmp (name, symdef->name) == 0)
2682 result = is_global_data_symbol_definition (abfd, isym);
2692 /* Add symbols from an ELF archive file to the linker hash table. We
2693 don't use _bfd_generic_link_add_archive_symbols because of a
2694 problem which arises on UnixWare. The UnixWare libc.so is an
2695 archive which includes an entry libc.so.1 which defines a bunch of
2696 symbols. The libc.so archive also includes a number of other
2697 object files, which also define symbols, some of which are the same
2698 as those defined in libc.so.1. Correct linking requires that we
2699 consider each object file in turn, and include it if it defines any
2700 symbols we need. _bfd_generic_link_add_archive_symbols does not do
2701 this; it looks through the list of undefined symbols, and includes
2702 any object file which defines them. When this algorithm is used on
2703 UnixWare, it winds up pulling in libc.so.1 early and defining a
2704 bunch of symbols. This means that some of the other objects in the
2705 archive are not included in the link, which is incorrect since they
2706 precede libc.so.1 in the archive.
2708 Fortunately, ELF archive handling is simpler than that done by
2709 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
2710 oddities. In ELF, if we find a symbol in the archive map, and the
2711 symbol is currently undefined, we know that we must pull in that
2714 Unfortunately, we do have to make multiple passes over the symbol
2715 table until nothing further is resolved. */
2718 _bfd_elf_link_add_archive_symbols (bfd *abfd,
2719 struct bfd_link_info *info)
2722 bfd_boolean *defined = NULL;
2723 bfd_boolean *included = NULL;
2728 if (! bfd_has_map (abfd))
2730 /* An empty archive is a special case. */
2731 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
2733 bfd_set_error (bfd_error_no_armap);
2737 /* Keep track of all symbols we know to be already defined, and all
2738 files we know to be already included. This is to speed up the
2739 second and subsequent passes. */
2740 c = bfd_ardata (abfd)->symdef_count;
2744 amt *= sizeof (bfd_boolean);
2745 defined = bfd_zmalloc (amt);
2746 included = bfd_zmalloc (amt);
2747 if (defined == NULL || included == NULL)
2750 symdefs = bfd_ardata (abfd)->symdefs;
2763 symdefend = symdef + c;
2764 for (i = 0; symdef < symdefend; symdef++, i++)
2766 struct elf_link_hash_entry *h;
2768 struct bfd_link_hash_entry *undefs_tail;
2771 if (defined[i] || included[i])
2773 if (symdef->file_offset == last)
2779 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
2780 FALSE, FALSE, FALSE);
2787 /* If this is a default version (the name contains @@),
2788 look up the symbol again with only one `@' as well
2789 as without the version. The effect is that references
2790 to the symbol with and without the version will be
2791 matched by the default symbol in the archive. */
2793 p = strchr (symdef->name, ELF_VER_CHR);
2794 if (p == NULL || p[1] != ELF_VER_CHR)
2797 /* First check with only one `@'. */
2798 len = strlen (symdef->name);
2799 copy = bfd_alloc (abfd, len);
2802 first = p - symdef->name + 1;
2803 memcpy (copy, symdef->name, first);
2804 memcpy (copy + first, symdef->name + first + 1, len - first);
2806 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2807 FALSE, FALSE, FALSE);
2811 /* We also need to check references to the symbol
2812 without the version. */
2814 copy[first - 1] = '\0';
2815 h = elf_link_hash_lookup (elf_hash_table (info),
2816 copy, FALSE, FALSE, FALSE);
2819 bfd_release (abfd, copy);
2825 if (h->root.type == bfd_link_hash_common)
2827 /* We currently have a common symbol. The archive map contains
2828 a reference to this symbol, so we may want to include it. We
2829 only want to include it however, if this archive element
2830 contains a definition of the symbol, not just another common
2833 Unfortunately some archivers (including GNU ar) will put
2834 declarations of common symbols into their archive maps, as
2835 well as real definitions, so we cannot just go by the archive
2836 map alone. Instead we must read in the element's symbol
2837 table and check that to see what kind of symbol definition
2839 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
2842 else if (h->root.type != bfd_link_hash_undefined)
2844 if (h->root.type != bfd_link_hash_undefweak)
2849 /* We need to include this archive member. */
2850 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2851 if (element == NULL)
2854 if (! bfd_check_format (element, bfd_object))
2857 /* Doublecheck that we have not included this object
2858 already--it should be impossible, but there may be
2859 something wrong with the archive. */
2860 if (element->archive_pass != 0)
2862 bfd_set_error (bfd_error_bad_value);
2865 element->archive_pass = 1;
2867 undefs_tail = info->hash->undefs_tail;
2869 if (! (*info->callbacks->add_archive_element) (info, element,
2872 if (! bfd_link_add_symbols (element, info))
2875 /* If there are any new undefined symbols, we need to make
2876 another pass through the archive in order to see whether
2877 they can be defined. FIXME: This isn't perfect, because
2878 common symbols wind up on undefs_tail and because an
2879 undefined symbol which is defined later on in this pass
2880 does not require another pass. This isn't a bug, but it
2881 does make the code less efficient than it could be. */
2882 if (undefs_tail != info->hash->undefs_tail)
2885 /* Look backward to mark all symbols from this object file
2886 which we have already seen in this pass. */
2890 included[mark] = TRUE;
2895 while (symdefs[mark].file_offset == symdef->file_offset);
2897 /* We mark subsequent symbols from this object file as we go
2898 on through the loop. */
2899 last = symdef->file_offset;
2910 if (defined != NULL)
2912 if (included != NULL)